I'd like to write the contents of a macro to an external file. The code below
\documentclass{article}
\begin{document}
\newcommand\foo[3]{1: #1, 2: #2 and 3: #3}
\foo{foo}{bar}{baz}
\foo{See \ref{sec}}{\begin{description}
\item[whatever] Hello
\end{description}}{Some text}
\section{A section}
\label{sec}
\end{document}
writes 1: foo, 2: bar and 3: baz
and 1: See 1, 2: whatever Hello and 3: Some text
into the PDF file. This should go to an external file, perhaps as XML or CSV or something else parsable. I only need the output (line breaks can be ignored) of the macro, not the source (\ref{...}
for example)
I use LuaLaTeX, so I might just go ahead and parse the output nodes, but there might be a solution at the macro level?
Best Answer
Judging from the comments, you know this already but you can open a file with something like
I have used the tex file name
\jobname
for the output file with an "output" extension. You can write to this file usingWith your MWE, things go horribly wrong with expansion. To get around this in the past I have used an
\unexpandedwrite
macro that I found somewhere in TeXbook. Using this I can get your MWE to compile and produce the output:Depending on what you expect for
#1
,#2
, and#3
you could of course use\write
for some of them and\unexpandedwrite
for others. This is what I have done below.EDIT
@jfbu points out in the comments that there is now an
\unexpanded
macro so\unexpandedwrite
is not needed anymore, so my MWe can be shortened to:Here is the original code: