在写入 aux 文件时评估 \luaexec

在写入 aux 文件时评估 \luaexec

在文档结束钩子中,我尝试写入\luaexec{x = 1}辅助文件,其中1在 Lua 本身中计算(当然,在实际文档中更复杂)。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}

以下仅列举了我遇到的众多错误中的一部分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
[...]

我认为这是因为我的 aux 文件中写入了一些有趣的内容,例如:

\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.

我也尝试过这个,但它以自己的方式失败了:

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

我做错了什么?我以为\luaexec在这里用它是毫无希望的吗?谢谢!

答案1

\luaexec不可扩展,但是\luadirect

\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}

第二次运行时,

x = 1

已打印。

相关内容