想像一下下面的程式碼:
\documentclass{book}
\usepackage{luacode}
\usepackage{xparse}
\NewDocumentCommand{\HelloWorld}{}{Hello World}
\newcommand{\DefaultHelloWorld}{Hello World}
\begin{luacode}
luatexbase.add_to_callback("wrapup_run", function()
io.write("\HelloWorld")
-- io.write("\DefaultHelloWorld")
end, "Post-process pdf")
\end{luacode}
\begin{document}
Hello World!
\end{document}
為什麼 using\newcommand
工作得很好並擴展為io.write("Hello World")
,但是一旦我切換到 ,\NewDocumentCommand
我就會收到以下錯誤(日誌文件摘錄):
Package: xparse 2023-10-10 L3 Experimental document command parser
)[\directlua]:2: invalid escape sequence near '"\H'.
\luacode@dbg@exec ...code@maybe@printdbg {#1} #1 }
l.14 \end{luacode}
The lua interpreter ran into a problem, so the
remainder of this lua chunk will be ignored.
答案1
謝謝烏爾里克·費雪
只需切換到\NewExpandableDocumentCommand
:
\documentclass{book}
\usepackage{luacode}
% \usepackage{xparse} % REMOVED -- see @David Carlisle's comment
\NewDocumentCommand{\OldHelloWorld}{}{Hello World} % OLD
\NewExpandableDocumentCommand{\HelloWorld}{}{Hello World} % NEW -- working
\newcommand{\DefaultHelloWorld}{Hello World}
\begin{luacode}
luatexbase.add_to_callback("wrapup_run", function()
io.write("\HelloWorld")
-- io.write("\DefaultHelloWorld")
end, "Post-process pdf")
\end{luacode}
\begin{document}
Hello World!
\end{document}