1

In the document end hook I'm trying to write \luaexec{x = 1} to the aux file, where the 1 is computed in Lua itself (and more complex in the real document, of course). MWE:

\documentclass{article}

\usepackage{luacode}

\luaexec{x = 0}

\makeatletter
\AtEndDocument{%
  \immediate\write\@auxout{%
    \noexpand\luaexec{x = \luaexec{tex.sprint(1)}}}
}
\makeatother

\begin{document}

x = \luaexec{tex.sprint(x)}

\end{document}

Here are just some of the many errors I get from lualatex:

[...]
Runaway argument?
{x = \begingroup \escapechar 92 \newlinechar 10 \edef \protect \let \relax \ETC.
! Paragraph ended before \luaexec was complete.
<to be read again>
\par
l.5


! LaTeX Error: Missing \begin{document}.
[...]
l.6 S
   ee the LaTeX manual or LaTeX Companion for explanation.
! Missing control sequence inserted.
<inserted text>
\inaccessible
l.7 ...ken \let \edef \def document{document}\edef {
                                                   on input line 18}\protect...
! Improper \spacefactor.
\@o-\spacefactor
                 \@m
[...]

I assume this is because of the amusing stuff being written into my aux file, such as:

\relax
\luaexec {x = \begingroup \escapechar 92 \newlinechar 10 \edef \protect \let \relax \let \relax \let \reserved@d =*\def \def document{document}\edef { on input line 18}\protect \begingroup \immediate \write \@unused   \def \MessageBreak
 \let \protect \edef  Your command was ignored.\MessageBreak Type  I <command> <return>  to replace it with another command,\MessageBreak or  <return>  to continue without it.  \errhelp \let \def \MessageBreak
                \def   \errmessage  LaTeX Error: Can be used only in preamble.

See the LaTeX manual or LaTeX Companion for explanation.

I tried this as well, but it fails in its own way:

\AtEndDocument{%
  \edef\myCode{\noexpand\luaexec{x = \luaexec{tex.sprint(1)}}}
  \immediate\write\@auxout{\myCode}
}

What am I doing wrong? Is it just hopeless to think that I can use \luaexec here? Thanks!

  • What would you want to be written in the .aux file? Note that \write doesn't do command execution, but just expansion. – egreg Oct 9 '14 at 17:41
  • I expected my MWE to write \luaexec{x = 1} into the .aux file, where the 1 is computed in some Lua code. – dsedivec Oct 9 '14 at 17:44
  • 1
    \luaexec is not expandable: if you want to use Lua in such a context you'll need the \directlua primitive. – Joseph Wright Oct 9 '14 at 18:16
  • I have just now noticed the footnote in the luacode documentation, saying that \luaexec is "not purely expandable." I assume that's what @JosephWright is referring to as well. I don't understand "expandable-ness" and when/where it matters, but I can certainly understand "you need something expandable there and \luaexec is not." I'd be happy to select this as the answer if you'd post it. Thank you! – dsedivec Oct 9 '14 at 18:36
  • 1
    \luadirect works. – egreg Oct 9 '14 at 19:49
2

\luaexec is not expandable, but \luadirect is:

\documentclass{article}

\usepackage{luacode}

\luaexec{x = 0}

\makeatletter
\AtEndDocument{%
  \immediate\write\@auxout{%
    \noexpand\luaexec{x = \luadirect{tex.sprint(1)}}}
}
\makeatother

\begin{document}

x = \luaexec{tex.sprint(x)}

\end{document}

At the second run,

x = 1

is printed.

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.