考虑以下 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}
正如预期的那样,输出是ABC compiled on --- Saturday at 2:03:04
。
现在我想将此输出放在文档的脚注中(或其他不显眼的地方)。所以我尝试使用footnote{}
这样的方法。
\footnote{
\begin{luacode}
tex.sprint(os.date("compiled on --- %A at %X " ) )
\end{luacode}
}
生成的文档无法在 Luatex 中编译。
据我了解,错误(附在下面)似乎是由于%
lua 中的符号以某种方式迫使 latex 忽略\end{luacode}
。但使用 luacode 包的全部目的就是避免这个问题。
我该如何修复它?
以下是错误日志的一部分:
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 type
2' 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}
答案1
环境luacode
不能作为另一个命令的参数,与逐字环境类似。原因很常见:一旦标记列表被吸收为参数,类别代码就无法更改;这里的罪魁祸首显然是%
当\footnote
开始吸收其参数时,这是一个注释字符。
一般情况下建议将其放在序言中,并定义 Lua 函数。
\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}
您不需要定义\compiledon
并简单地使用\directlua{compiledon()}
,但在我看来,这样更干净。