Latex \ newcommand参数的变量数

时间:2015-03-25 13:02:36

标签: latex tex

我试图创建一个基本上做不同事情的LaTeX -macro,具体取决于传递给它的参数数量。

基本上它的功能是:

\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个参数中,它给了我"段落在\ MyMacro @ four完成之前结束。"

我对基本TeX的掌握非常小,所以我也很欣赏LaTeX解决方案以及使用\ newcommand,如果可能的话。

谢谢!

1 个答案:

答案 0 :(得分:1)

似乎Werner answer提出的类似问题就是您正在寻找的内容。