2015-03-25 45 views
0

我正在嘗試創建一個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\[email protected]} 

% Check for second argument 
\def\[email protected]{\ifx\next\bgroup\expandafter\[email protected]\else\expandafter\[email protected]\fi} 

% Check for third argument 
\def\[email protected]{\ifx\next\bgroup\expandafter\[email protected]\else\expandafter\[email protected]\fi} 

% Check for fourth argument 
\def\[email protected]{\ifx\next\bgroup\expandafter\[email protected]\else\expandafter\[email protected]\fi} 

\def\[email protected]{\section*{\tempA\ is the only argument}} 
\def\[email protected]#1{\section*{\tempA\ is the first and #1 is the second argument}} 
\def\[email protected]#1#2{\section*{\tempA\ is the first and #1 is the second argument with #2 being the third}} 
\def\[email protected]#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

似乎Werner的answer到一個類似的問題是你在找什麼。

+0

感謝您的回覆,雖然我已經繞過了我的項目中的一個更簡單的功能,也更容易在API中實現的問題。我喜歡你的鏈接,所以我可以看看它,如果我稍後遇到類似的問題。 – 2015-04-18 11:39:10