Try-Except 执行扩展时

Try-Except 执行扩展时

是否可以编写一个函数来执行以下操作:

function \tryexpand (\foo):
    try:
         expand(\foo)
    except (Argument of \@following has an extra }):
         store "\relax" in `\result`
    else:
         store expansion of `\foo` in `\result`
    
    return \result

上下文:我正在尝试做\edef\result{\expandafter\empty\foo}(规避),如果不需要任何参数则\protect有效,否则会失败。\foo

测试用例

\newcommand{\needsthree}[3]{#3#2#1}
\protected\def\foo{\needsthree}
% --> \tryexpand{\foo} should lead to \result being macro:->\relax
\newcommand{\needsnone}{42}
\protected\def\bar{\needsnone}
% --> \tryexpand{\bar} should lead to \result being macro:->42

相关内容