8

How can I define a command that forwards arguments to another command?

Specifically, I'd like to create a command that forwards arguments to \pdfcomment and adds the author optional argument with a fixed value. How do I fill in the blanks?

\newcommand{\prax}[?][?]{\pdfcomment[author=Praxeolitic, ?]{?}}

The desired behavior is that this:

\prax[abc]{xyz}

will expand to this:

\pdfcomment[author=Praxeolitic, abc]{xyz}
5

You have a number of options. Here are two (inherently similar):

\newcommand{\prax}[1][]{\pdfcomment[author=Praxeolitic, #1]}

or

\newcommand{\prax}[2][]{\pdfcomment[author=Praxeolitic, #1]{#2}}

The former ignores the mandatory argument and just processed the optional argument, if it's there. The second grabs the mandatory argument and passes it straight to \pdfcomment.

Key-value evaluation is fine with blank items, so even if \prax[<opt>]{<man>} yields a blank/non-existent [<opt>], this should be fine.

2
  • I've always wondered about this sort of eta contraction. It feels silly to me to grab an argument and pass it to another command when the other command would be perfectly able to grab it on its own, and so I prefer the first example as a coder; but, on the other hand, it seems to me that the second example is clearer about its intent, so that I might prefer it as a reader. I guess that there can be parsing differences, too, or at least differences in error messages. Is there an established best practice here? – LSpice May 11 '15 at 19:54
  • @LSpice: There is no real best practive (in my opinion) and depends on the coder. Even if you were to say that a change in the interface of \pdfcomment would require a redefinition of \prax, that would hold for both methods of coding... – Werner May 11 '15 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.