TransWikia.com

How to include an obligatory argument in the definition of a default optional argument?

TeX - LaTeX Asked by Alvaro9366 on November 1, 2021

I want to implement something with the same structure as the next command:

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}

But it gives an error due to the default optional fourth argument’s definition.

If I remove the #2 and #1 from the default optional argument’s definition everything works just fine.

But I want to know, how can I make this happen?

One Answer

It's very easy with 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}

enter image description here

Answered by egreg on November 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP