我有一个在文档中反复调用的宏
\newcommand\foo[1]{Process #1}
\newcommand\var[2]{Do something with #1 and #2, and then call '\foo{#2}', and again '\foo{#2}', and once again '\foo{#2}'}
宏在foo
内部被多次扩展,var
相同的论点,人们可能会认为传递论点只是多余的,而且是可以避免的。
问题:是否有可能有一个版本,foo
当var
定义为
\newcommand\var[2]{Do something with #1 and #2, and then call '\foo', and again '\foo', and once again '\foo'}
会导致第一个例子的结果吗?
平均能量损失
\documentclass[]{article}
\newcommand\foo[1]{Process #1}
\newcommand\var[2]{Do something with #1 and #2, and then call '\foo{#2}', and again '\foo{#2}', and once again '\foo{#2}'}
\begin{document}
\var{x}{y}
\end{document}
我希望得到相同的结果,定义var
为
\newcommand\var[2]{Do something with #1 and #2, and then call '\foo', and again '\foo', and once again '\foo'}
答案1
原帖者要求我将评论转换成答案。但答案均未给出原帖者所期望的内容。
问题是,我认为没有办法在不执行某些操作的情况下获取#2
或将其等效值存入。但这只能在调用之后或通过某些预宏调用来实现。\foo
#2
\var
\var
我在下面的 MWE 中展示了这两种替代方案。
\documentclass[]{article}
\begin{document}
Original approach of OP
\newcommand\foo[1]{Process #1}
\newcommand\var[2]{Do something with #1 and #2, and then call '\foo{#2}', and again '\foo{#2}', and once again '\foo{#2}'}
\var{x}{y}
\hrulefill
Requires a leading \verb|\def| inside of \verb|\var|
\renewcommand\var[2]{\def\foo{Process #2}Do something with #1 and #2, and then call '\foo', and again '\foo', and once again '\foo'}
\var{x}{y}
\hrulefill
Defines \verb|\var| as desired, but requires invocation of \verb|\setvar|
\renewcommand\var[2]{Do something with #1 and #2, and then call '\foo', and again '\foo', and once again '\foo'}
\newcommand\setvar[2]{\def\foo{Process #2}\var{#1}{#2}}
\setvar{x}{y}
\end{document}