luacode funciona com \newcommand mas não com \NewDocumentCommand

luacode funciona com \newcommand mas não com \NewDocumentCommand

Imagine o seguinte código:

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

Por que using \newcommandfunciona perfeitamente e se expande para io.write("Hello World"), mas assim que mudo para \NewDocumentCommandrecebo o seguinte erro (trecho do arquivo de log):

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.

Responder1

ObrigadoUlrike Fischer

Só tive que mudar para \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}

informação relacionada