# Help integrating some LUA code into a Luatex document?

Consider the following MWE:

\documentclass[12pt,a4paper]{amsart}
\usepackage{fontspec}
\usepackage{luacode}

\begin{document} ABC

\begin{luacode} tex.sprint(os.date("compiled on --- %A at %X " ) ) \end{luacode}

\end{document}

As expected the output is ABC compiled on --- Saturday at 2:03:04 .

Now I would like to place this output in a footnote (or other inconspicuous place) in the document. So I tried using footnote{} like this.

\footnote{
\begin{luacode}
tex.sprint(os.date("compiled on --- %A at %X " ) )
\end{luacode}
}

The resulting document doesn't compile in Luatex.

It is my understanding that the error (attached below) seems to be that somehow the % sign in lua is forcing latex to ignore the \end{luacode} . But the whole point of using package luacode was to avoid that problem.

How can I fix it?

Here is part of the error log:

I've run across a }' that doesn't seem to match anything.
For example,\def\a#1{...}' and \a}' would produce
this error. If you simply proceed now, the\par' that
I've just inserted will cause me to report a runaway
argument that might be the root of the problem. But if
your }' was spurious, just type2' and it will go away.

Runaway argument? ex.sprint(os.date("compiled on \end {luacode} \@finalstrut \strutbox ! Paragraph ended before \luacode@grab@lines was complete. \par l.24 }

I suspect you've forgotten a }', causing me to apply this control sequence to too much text. How can we recover? My plan is to forget the whole thing and hope for the best.

! Extra }, or forgotten \endgroup. }

l.24 }

I've deleted a group-closing symbol because it seems to be spurious, as in $x}$'. But perhaps the } is legitimate and you forgot something else, as in\hbox{$x}'. In such cases the way to recover is to insert both the forgotten and the deleted material, e.g., by typing I$}'.

! Extra }, or forgotten \endgroup. \H@@footnotetext ...t \strutbox }\color@endgroup }

l.24 }

I've deleted a group-closing symbol because it seems to be spurious, as in $x}$'. But perhaps the } is legitimate and you forgot something else, as in\hbox{$x}'. In such cases the way to recover is to insert both the forgotten and the deleted material, e.g., by typing `I$}'.

! LaTeX Error: \begin{luacode} on input line 24 ended by \end{document}.

See the LaTeX manual or LaTeX Companion for explanation. Type H for immediate help. ...

l.33 \end{document}

• The luacode environment changes the category code of %, but it can't do it if in the argument of \footnote. – egreg May 6 '16 at 23:56
• @egreg Thank you!. Is there a way around? – DBS May 6 '16 at 23:57
• Can you save the result of the code to a box and then use the box in the footnote? \newsavebox\mybox, \sbox\mybox{...}, \footnote{\usebox\mybox} or something like that? – cfr May 7 '16 at 1:34
• Define a Lua function – egreg May 7 '16 at 6:52

The luacode environment cannot be the argument to another command, similarly to verbatim environment. The reason is the usual one: category codes cannot be changed once the token list has been absorbed as an argument; the culprit here is obviously % that's a comment character when \footnote starts absorbing its argument.

It's recommended to have it in the preamble, in general, and to define Lua functions.

\documentclass[12pt,a4paper]{amsart}
\usepackage{fontspec}
\usepackage{luacode}

\begin{luacode}
function compiledon()
tex.sprint(os.date("compiled on --- %A at %X " ) )
end
\end{luacode}
\newcommand{\compiledon}{\directlua{compiledon()}}

\setlength{\textheight}{2cm} % just for the example

\begin{document}

ABC\footnote{\compiledon}

\end{document}

You don't need to define \compiledon and simply use \directlua{compiledon()}, but this way it's cleaner, in my opinion.

• Thanks! I did it using the following approach: write a separate lua script. Call it by \directlua using dofile option. – DBS May 8 '16 at 0:23