在传递给 lua 函数之前将命令参数扩展两次(例如 \luastringO)

在传递给 lua 函数之前将命令参数扩展两次(例如 \luastringO)

我想制作一个类似 的辅助宏luacode\luastringO但要将参数展开两次而不是一次。总有一堆\expandafter/\unexapnded\noexpand黑客技巧让我无法理解!

我想要的输出是:

VVVVVVVVVVVVVVVVVVVVVVVVVVVVV
\Acmd  | No
\ONEexp  | Once
\TWOexp  | Twice
Full Exp | Full
VVVVVVVVVVVVVVVVVVVVVVVVVVVVV 

我得到:

VVVVVVVVVVVVVVVVVVVVVVVVVVVVV
\Acmd  | No
\ONEexp  | Once
{\ONEexp } | Twice
Full Exp | Full
VVVVVVVVVVVVVVVVVVVVVVVVVVVVV 
\documentclass{scrreprt}
\usepackage{luacode}

\def\Acmd{\ONEexp}
\def\ONEexp{\TWOexp}
\def\TWOexp{\TREexp}
\def\TREexp{Full Exp}

% as per luacode.sty
% 149 \newcommand \luastring [1] {"\luatexluaescapestring{#1}"}
% 150 \newcommand \luastringO [1] {\luastring{\unexpanded\expandafter{#1}}}
% 151 \newcommand \luastringN [1] {\luastring{\unexpanded{#1}}}

\newcommand\luastringT[1]{\luastring{\unexpanded\expandafter{\expandafter{#1}}}}

\NewDocumentCommand\MYcmd{m}{
\luadirect{texio.write_nl('VVVVVVVVVVVVVVVVVVVVVVVVVVVVV')}
\luadirect{texio.write_nl(\luastringN{#1}..' | No')}
\luadirect{texio.write_nl(\luastringO{#1}..' | Once')}
\luadirect{texio.write_nl(\luastringT{#1}..' | Twice')}
\luadirect{texio.write_nl(\luastring{#1}..' | Full')}
\luadirect{texio.write_nl('VVVVVVVVVVVVVVVVVVVVVVVVVVVVV')}
}

\MYcmd{\Acmd}

\begin{document}
Hello world.
\end{document}

答案1

你的

\luastring{\unexpanded\expandafter{\expandafter{\Acmd}}}

运行\expandafter链,结果为:

\luastring{\unexpanded{{\ONEexp}}}

然后\unexpanded获取其参数(不包含 outer {...}),但{...}保留 inner ,结果是:

{\ONEexp}

如果你定义

\def\luastringT #1{\luastring{\unexpanded\expandafter\expandafter\expandafter{#1}}}

使用参数运行它\Acmd,然后第一个\expandafter链跳过中间\expandafter并转到结果:

\luastring{\unexpanded\expandafter{\ONEexp}}

然后剩下的\expandafter工作:

\luastring{\unexpanded{\TWOexp}}

\unexpanded获得论点\TWOexp,保护它,结果是

\TWOexp

相关内容