Latex \newcommand 参数的可变数量

Latex \newcommand variable number of arguments

我正在尝试创建一个 LaTeX 宏,它基本上根据传递给它的参数数量做不同的事情。

基本上它的功能是:

\MyMacro{4}
% Processes the argument and returns a set number of objects depending on the number

\MyMacro{4}{3}
% Processes the arguments in a slightly different way, including adding the numbers together

\MyMacro{4}{3}{6}
% Same thing as before, but the result is again processed slightly differently as before

\MyMacro{4}{3}{6}{2}{1}
% The maximum number of arguments the macro needs to handle is 5

我尝试根据我在网上找到的最多 2 个属性的示例来实现以下 TeX 解决方案,但它不喜欢我。

\makeatletter
% Save first argument as \tempA
\def\MyMacro#1{\def\tempA{#1}\futurelet\next\MyMacro@i}

% Check for second argument
\def\MyMacro@i{\ifx\next\bgroup\expandafter\MyMacro@ii\else\expandafter\MyMacro@one\fi}

% Check for third argument
\def\MyMacro@ii{\ifx\next\bgroup\expandafter\MyMacro@iii\else\expandafter\MyMacro@two\fi}

% Check for fourth argument
\def\MyMacro@iii{\ifx\next\bgroup\expandafter\MyMacro@four\else\expandafter\MyMacro@three\fi}

\def\MyMacro@one{\section*{\tempA\ is the only argument}}
\def\MyMacro@two#1{\section*{\tempA\ is the first and #1 is the second argument}}
\def\MyMacro@three#1#2{\section*{\tempA\ is the first and #1 is the second argument with #2 being the third}}
\def\MyMacro@four#1#2#3{\section*{\tempA\ is the first and #1 is the second argument, #2 is the third and #3 is the fourth}}

\makeatother

它适用于 1 或 4,但在 2-3 个参数时,它给了我 "Paragraph ended before \MyMacro@four was complete."

我对基本 TeX 的掌握很少,所以如果可能的话,我也很喜欢使用 \newcommand 的 LaTeX 解决方案。

谢谢!

Werner 对类似问题的 answer 似乎就是您要找的内容。