4

I am not sure if I use the wrong approach or if this is an issue in LuaLaTex in general. I tried to integrate some internationalization (I18n) to our documentation framework using Lua. Long story, short: I want to read the caption of general sections like 'references', 'appendix' and other from a resource file. But using a luaexec in \chapter or \section writes useless entries to .aux and therefore also in .toc .lof and .lot files.

This is my small sample:

\documentclass[]{article}
\usepackage{luacode}

\newcommand{\intString}[1]{\luaexec{tex.print("#1")}}

\begin{document}
\section{\intString{test}} %this breaks
\intString{lofCaption}  % this works

\cleardoublepage
\addcontentsline{toc}{chapter}{\intString{lofCaption}} % this breaks
\protect\listoffigures

\cleardoublepage
\addcontentsline{toc}{chapter}{\intString{lotCaption}} % this breaks
\protect\listoftables
\end{document}

I have no idea why this causes some entries like this in my .aux file:

\@writefile{toc}{\contentsline {section}{\numberline {1}\begingroup \escapechar 92 \newlinechar 10 \edef \\{\\}\edef \nobreakspace  {}{~}\let \%=%\let \#=#\endgroup test}{1}}

It looks like during creation of .aux file the lua code is neither executed nor written to the .aux but replaced by something looking like a reference.

Does anyone know if this is a malfunction of LuaLaTeX or if there is a mistake on my part?

Thank you for your attention.

3
  • Welcome to TeX.SX! It mostly depends on what you want to write in the .toc file. With \addcontentsline{toc}{chapter}{<tokens>} you write in the .toc file a line \contentsline{chapter}{<expansion of tokens>} and that' in fact the expansion of \luaexec – egreg Jul 23 '14 at 10:43
  • Hi Egreg, thanks for this clarification, the \contentsline works perfect, but while using \section it still crashes. Is there any way to force expansion? – Jörn Schlingensiepen Jul 23 '14 at 11:46
  • Are there any news here? Lua seems a bit complicated for that stuff, you could as well use the babel mechanism for that. – Johannes_B Feb 18 '15 at 21:11
3

The luacode commands are not expandable or robust, so need \protect. But \protect needs to be applied in the moving argument, using it at the top level like

\protect\listoftables

does nothing as \protect is \relax at that point.

So this works

\documentclass[]{article}
\usepackage{luacode}

\newcommand{\intString}[1]{\luaexec{tex.print("#1")}}

\begin{document}
\section{\protect\intString{test}} %this breaks
\intString{lofCaption}  % this works

\cleardoublepage
\addcontentsline{toc}{chapter}{\protect\intString{lofCaption}} % this breaks
\listoffigures

\cleardoublepage
\addcontentsline{toc}{chapter}{\protect\intString{lotCaption}} % this breaks
\listoftables
\end{document}

or more simply you can use \directlua not the luacode package wrapper as that is expandable.

So this also works

\documentclass[]{article}


\newcommand{\intString}[1]{\directlua{tex.print("#1")}}

\begin{document}
\section{\intString{test}} %this breaks
\intString{lofCaption}  % this works

\cleardoublepage
\addcontentsline{toc}{chapter}{\intString{lofCaption}} % this breaks
\listoffigures

\cleardoublepage
\addcontentsline{toc}{chapter}{\intString{lotCaption}} % this breaks
\listoftables
\end{document}

the two verions are not equivalent, in the second version intString is expanded as the aux file is written out, in the first version \intString is written as \intString to the aux file and expanded when that file is read back in. The end result is the same in this example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.