I need to generate quite a few LaTeX files in an automated way. I do not want to use any templating engine in Python or Elisp (two languages I'm probably most comfortable with); I'd prefer to do it internally by LaTeX. What I need is to output a fixed text (containing mostly LaTeX commands) to a file, with the exception that there will be some "variables" (like title etc.), where some data (available in TeX macros) should be substituted. I could do it with some \writes and lots of \noexpands, but this is a bit cumbersome. Is there any (probably verbatim-like) package for that? Bonus points if the writing occurs during shipout, so that \thepage will expand to correct (current) page number.
Asked
Active
Viewed 492 times
3
mbork
- 13,385
-
If you post an example showing in more detail what you want you're more likely to get a useful answer. Can you write a TeX document in which you indicate which "variables" should be variable? Some indication of your workflow would be helpful too. – Ethan Bolker Aug 29 '14 at 15:08
-
This answer help? – Fran Aug 29 '14 at 16:02
1 Answers
0
There are bound to be packages for doing this, but I have some code for generating a notational index that is written to a file which is then included at the end of the document. To do this I used the following macros from the TeXbook:
\def\\{\let\stoken= } \\
\long\def\unexpandedwrite#1#2#3{\def\finwrite{\write#1}%
{\aftergroup\finwrite\aftergroup{\sanitize#2\endsanity}%
\ifx#3\relax\else#3\fi}}
\def\sanitize{\futurelet\next\sanswitch}
\def\sanswitch{\ifx\next\endsanity
\else\ifcat\noexpand\next\stoken\aftergroup\space\let\next=\eat
\else\ifcat\noexpand\next\bgroup\aftergroup{\let\next=\eat
\else\ifcat\noexpand\next\egroup\aftergroup}\let\next=\eat
\else\let\next=\copytoken\fi\fi\fi\fi \next}
\def\eat{\afterassignment\sanitize \let\next= }
\long\def\copytoken#1{\ifcat\noexpand#1\relax\aftergroup\noexpand
\else\ifcat\noexpand#1\noexpand~\aftergroup\noexpand\fi\fi
\aftergroup#1\sanitize}
\def\endsanity\endsanity{}
To use this you first need to open a file
\newwrite\myWonderfulFile
\immediate\openout\myWonderfulFile=\jobname.wonder
and then your macro can write to your file using
\unexpandedwrite\myWonderfulFile{stuff to write}
Occasionally, I found that I also needed things like:
\write\myWonderfulFile{\thechapter.\thesection}
Of course, you can also use \immediate\write\myWonderfulFile.
Finally, before including your file back into the document you should issue
\immediate\closeout\myWonderfulFile