我有以下命令定義(它是條目列表的條目),效果很好:
\newcommand{\entry}[4]{%
#1&\parbox[t]{11.2cm}{%
#2%
\hfill%
#3%
\\#4%
}\\}
現在我有一個測試案例,當第四個參數可以為空時。在這種情況下,我不想顯示之前定義的新行。我嘗試過以下方法:
\newcommand{\entry}[4]{%
#1&\parbox[t]{11.2cm}{%
#2%
\hfill%
#3%
\ifthenelse{\isempty{#4}}{}{\\#4}%
}\\}
但它給了我一個錯誤:
Undefined control sequence. ^^I{test test2}
Missing number, treated as zero. ^^I{test test2}
Missing = inserted for \ifnum. ^^I{test test2}
Missing number, treated as zero. ^^I{test test2}
能否請你幫忙?
答案1
該問題似乎已在評論中解決,但我想建議兩種不同的方法。
首先,\newcommand
可以輕鬆定義具有一個可選參數的命令,前提是它是第一個參數。使用此功能,您可以將巨集重寫為:
\newcommand{\entry}[4][]{%
#2&\parbox[t]{11.2cm}{%
#3%
\hfill%
#4%
\if\relax\detokenize{#1}\relax\else\\#1\fi
}\\}
表示[]
預設#1
為空。這個巨集本質上與你的相同,只是我改變了參數編號。主要區別在於如何使用巨集:
\entry{second}{third}{fourth}
不帶可選參數,或者,如果您想給出可選參數:
\entry[first]{second}{third}{fourth}
第二個選擇是\NewDocumentCommand
使用解析包裹。的優點\NewDocumentCommand
是它可以讓你將可選參數放在你想要的任何地方,包括最後:
\NewDocumentCommand{\entry}{ mmmo }{%
#1&\parbox[t]{11.2cm}{%
#2%
\hfill%
#3%
\IfNoValueF{#4}{\\#4}% print #4 when it is given
}\\}
據說mmmo
有三個米附加論點和一個哦選擇性論證。同樣,程式碼的唯一真正區別在於巨集的使用方式:
\entry{first}{second}{third}% without optional argument
\entry{first}{second}{third}[fourth]% with optional argument