TransWikia.com

What is the correct syntax for a macro with >1 parameter where 2nd etc have defaults but not the 1st?

TeX - LaTeX Asked by Macro Defaults on February 25, 2021

The document that I am working with has a macro with 1 parameter:

newcommand{emailTo}[1]{The email address that #1 is sent to.}

I have to add another parameter with a default value of email address to remain backwards-compatible. I tried:

newcommand{emailTo}2][][email address]{The #2 that #1 is sent to.}

But I am getting this error:

You can't use `macro parameter character #' in horizontal mode. ...emailToNew}[2][qeee][email address] {The #

documentclass[10pt,a4paper]{article}
usepackage[utf8]{inputenc}

newcommand{emailTo}[1] {The email address that #1 is sent to.}

newcommand{emailToNew}[2][][email address] {The #2 that #1 is sent to.}

begin{document}
    emailTo{notification}
    
    emailToNew{notification}%{email address}
end{document}

Is there any way to stay backwards-compatible and not have to change hundreds of instances where the existing macro is used with 1 parameter?

……

One Answer

I'm not sure this is what you're looking for:

documentclass[10pt,a4paper]{article}

NewDocumentCommand{emailTo}{mO{email address}}{%
  The #2 that #1 is sent to.
}

begin{document}

emailTo{somebody}

emailTo{somebody}[parcel]

end{document}

enter image description here

The first argument is mandatory, the second argument is optional with default value email address.

Answered by egreg on February 25, 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