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}

luacodeenvironment changes the category code of%, but it can't do it if in the argument of\footnote.\newsavebox\mybox,\sbox\mybox{...},\footnote{\usebox\mybox}or something like that?