% (find-angg "LATEX/2018tug-dednat6-abs2.tex")
% (defun c () (interactive) (find-LATEXsh "lualatex -record 2018tug-dednat6-abs2.tex"))
% (defun d () (interactive) (find-xpdfpage "~/LATEX/2018tug-dednat6-abs2.pdf"))
% (defun e () (interactive) (find-LATEX "2018tug-dednat6-abs2.tex"))
% (defun u () (interactive) (find-latex-upload-links "2018tug-dednat6-abs2"))
% (find-xpdfpage "~/LATEX/2018tug-dednat6-abs2.pdf")
% (find-sh0 "cp -v  ~/LATEX/2018tug-dednat6-abs2.pdf /tmp/")
% (find-sh0 "cp -v  ~/LATEX/2018tug-dednat6-abs2.pdf /tmp/pen/")
%   file:///home/edrx/LATEX/2018tug-dednat6-abs2.pdf
%               file:///tmp/2018tug-dednat6-abs2.pdf
%           file:///tmp/pen/2018tug-dednat6-abs2.pdf
% http://angg.twu.net/LATEX/2018tug-dednat6-abs2.pdf
\documentclass[oneside]{book}
\begin{document}

\def\ASCII{ASCII}
\def\AllTeX{(La)\TeX}
\def\acro{}
\def\Dash{---}




{Dednat6: An extensible (semi-)preprocessor for Lua\LaTeX\
 that~understands diagrams in \ASCII\ art}
{Eduardo Ochs}
{\def\code#1{\texttt{#1}}%
 \def\qcode#1{``\texttt{#1}''}%
 \let\qcode=\code % tt font seems sufficient?
 \def\bsl{\char92 }%
 \def\{{\char`\{ }%
 \def\}{\char`\} }%
\noindent
\AllTeX\ treats lines starting with \qcode{\%} as comments, and ignores
them. This means that we can put anything we want in these \qcode{\%}
lines, even code to be processed by other programs besides \TeX.

In this talk we describe a ``semi-preprocessor'', called
\code{dednat6}, that makes blocks of lines starting with \qcode{\%L}
be executed as Lua code, treats blocks of lines starting with
\qcode{\%:} as \acro{2D} representations of derivation trees, and treats
blocks of lines starting with \qcode{\%D} as diagrams in which a \acro{2D}
representation specifies where the nodes are to be placed and a
stack-based language inspired by Forth is used to connect these nodes
with arrows.

A predecessor of \code{dednat6}, called \code{dednat4}, was a
%preprocessor in the more usual sense: running ``\code{dednat4.lua
%  foo.tex}'' on a shell would convert the trees and diagrams in
%\qcode{\%:} and \qcode{\%D}-blocks in \code{foo.tex} to \qcode{\bsl
preprocessor in the more usual sense: running \code{dednat4.lua
  foo.tex} on a shell would convert the trees and diagrams in
\qcode{\%:}- and \qcode{\%D}-blocks in \code{foo.tex} to \qcode{\bsl
  def}s that \LaTeX\ can understand, and would put these \qcode{\bsl
% def}s in a file \code{foo.dnt}. Then in \code{foo.tex} we put an
%\qcode{\bsl input "foo.dnt"} to load those definitions.
  def}s in a file \code{foo.dnt}; we had to put in \code{foo.tex} an
\qcode{\bsl input "foo.dnt"} that would load those definitions.

Dednat6 does something almost equivalent to that, but using
Lua\LaTeX\ to avoid the needs for an external preprocessor and for an
auxiliary \qcode{.dnt} file. Here is how; the workflow is unusual,
so let's see it in detail.

Put a line\\
\code{\bsl directlua\{dofile("loaddednat6.lua")\}}
\\in a
file \code{bar.tex}. When we run ``\code{lualatex bar.tex}'' that line
loads the \code{dednat6} library, initializes the global variable \code{tf}
in the Lua interpreter with a \code{TexFile} object, and sets
\code{tf.nline=1} to indicate that nothing in \code{bar.tex} has been
processed with Dednat6 yet.

A (low-level) command like\\
  \code{\bsl directlua\{processlines(200, 300)\}}\\
in \code{bar.tex} would ``process the lines 200 to 300 in \code{bar.tex}
with \code{dednat6}'', which means to take all the blocks of
\qcode{\%L}-lines, \qcode{\%:}-lines, and \qcode{\%D}-lines between the
lines 200 to 300 in \code{bar.tex}, run them in the necessary
interpreters, and then send the resulting \LaTeX\ code\Dash{}usually
\qcode{\bsl def}s\Dash{}to the \code{latex} interpreter.

The high-level
macro \code{\bsl pu} runs\\
  \code{\bsl directlua(processuntil\{tex.inputlineno\})}\\
which runs \code{processlines} on the source lines between
%\code{tf.nline=1} and the line where the current \qcode{\bsl pu} is, and
\code{tf.nline} and the line where the current \qcode{\bsl pu} is, and
advances \code{tf.nline}. That is, it processes with \code{dednat6}
the lines in the current file between the previous \qcode{\bsl pu} and
the current one.

The strings \qcode{\%L}, \qcode{\%:}, and \qcode{\%D} are called
``heads'' in \code{dednat6}, and it's easy to add support for new heads; this
can even be done in a \qcode{\%L} block.

With \code{dednat4}, all the \qcode{\bsl def}s had to be loaded at once;
in \code{dednat6} idioms like \qcode{\{\bsl pu ...\}}, \qcode{\$\bsl pu
  ...\$}, and \qcode{\$\$\bsl pu ...\$\$} can be used to make the
\qcode{\bsl def}s between the last \qcode{\bsl pu} and the current one
be local.}

\end{document}

% Local Variables:
% coding: utf-8-unix
% End:
