# Write to aux file directly from lua

Is it possible to write to the aux file directly from within a \directlua command?

I am converting an older package to LuaLaTeX, which at some point builds a large string and writes it to the aux file (using \immediate\write\@auxout ...). Now with lua, it is a lot cleaner and easier to build this string, but how can I write it to the aux file?

You can write to a file by

\directlua{
f=io.open("\jobname.aux2","w")
f:write("\string\\def\string\\hello{zzz}")
f:close()
}


It ought to be possible to get the file handle of the aux file opened with \openout from the tex side, instead of using f as above but I don't currently see an interface for that, unless I missed something...

Alternatively of course you could push it all back to tex with

\directlua{
tex.print("\string\\write\string\\@auxout{....}")
}


which gets the job done but doesn't really answer the question of how to write from Lua.

It is also possible to (re)open the same file for appending, although the timing is a bit tricky,

\documentclass{article}

\begin{document}
\section{zzz\label{z}}

\makeatletter
\directlua{
f=io.open("\jobname.aux2","w")
f:write("\string\\def\string\\hello{zzz}")
f:close()
%
%
}
\latelua{
f=io.open("\jobname.aux","a")
f:write("\string\\def\string\\hello{zzz append}")
%
%
}

\section{qqqq\label{q}}

\end{document}


the above writes the following to the .aux file

\relax
\newlabel{z}{{1}{1}}
\@writefile{toc}{\contentsline {section}{\numberline {1}zzz}{1}}
\newlabel{q}{{2}{1}}
\@writefile{toc}{\contentsline {section}{\numberline {2}qqqq}{1}}
\def\hello{zzz append}


so \def\hello{zzz append} is written but after all the tex writing from that page.

It also writes a .aux2 file with

\def\hello{zzz}

• The second solution you suggested (pushing back to tex) also came to my mind, but I didn't like it for the following reason. In the first place, I wanted to replace my old code by Lua to make it more readable. However, with this pushing back and forth strings I have to double escape things, for example in order to write \def\hello{zzz} to the aux file, I would need to write tex.print("\string\\write\string\\@auxout{\string\\noexpand\string\\def\string\\noexpand\string\\hello{zzz}}"), which really does not help to make the code readable... – Michael Fütterer Nov 4 '16 at 16:46
• But timing is not important in my case, so your last solution should work for me. Anyway, it would be great to have a command to directly write to the aux file from Lua. – Michael Fütterer Nov 4 '16 at 16:48

You can use an undocumented extension of texio.write: Instead of passing a string as first parameter, you can also pass a TeX output file handle, e.g. \the\@auxout:

\documentclass{article}

\begin{document}
\section{zzz}\label{z}

\makeatletter
\directlua{
texio.write(\the\@auxout, [[\string\def\string\hello{zzz}]])
}
\latelua{
texio.write(\the\@auxout, [[\string\def\string\hello{zzz append}]])
}
\makeatother

\section{qqqq}\label{q}
\end{document}


\relax