我想要实现与下一个命令具有相同结构的东西:
\documentclass{article}
\usepackage{xargs}
\providecommandx*{\countryinfo}[4][{3=undefined},{4=In #2 people like drinking #1.}]{%
\noindent
1: #1\\
2: #2\\
3: #3\\
4: #4\\
}
\begin{document}
\countryinfo{beer}{Germany}
\countryinfo{tequila}{Mexico}[Mexico City]
\countryinfo{vodka}{Russia}[Moskow][Russia is the biggest country in the world.]
\end{document}
但由于默认可选的第四个参数的定义,它给出了一个错误。
如果我从默认可选参数的定义中删除 #2 和 #1,一切都会正常工作。
但我想知道,我怎样才能实现这一点?
答案1
这非常简单xparse
:
\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\countryinfo}{
m % mandatory
m % mandatory
O{undefined} % optional, default 'undefined'
O{In #2 people like drinking #1.} % optional, default as defined
}{%
\noindent
1: #1\\
2: #2\\
3: #3\\
4: #4
}
\begin{document}
\countryinfo{beer}{Germany}
\countryinfo{tequila}{Mexico}[Mexico City]
\countryinfo{vodka}{Russia}[Moskow][Russia is the biggest country in the world.]
\end{document}