10

To implement a macro that takes one or more optional arguments that do not have to be placed in the first position, xargs can be used as well as xparse. The latter seems to have more recent updates while the first uses the well-known keyval internally.

Apart from this and their syntax, where do they differ? Can one of them be regarded as superior or deprecated? Is there a guideline which to use in new documents?

  • 1
    Both packages are stable and can be used. So it's your choice which fits your needs more. – Marco Daniel Nov 15 '13 at 13:52
  • 1
    xargs (as far as I can tell) can't (easily) distinguish between the cases when the optional argument is missing or present. Moreover it hasn't the possibility of defining *-variants or arguments with different delimiters than braces and brackets. – egreg Nov 15 '13 at 13:54
7

With xargs you're limited to pass a default value to optional arguments, so it's not easy to distinguish when the argument is missing. Conversely, xparse provides \IFNoValueTF just for this case.

For instance, one can say

\NewDocumentCommand{\foo}{o}{%
  \IfNoValueTF{#1}
    {foo without optional argument}
    {foo with optional argument #1}%
}

With xparse you can also define *-variants of commands and also commands taking arguments delimited in various ways. Arguments can be “long” on an individual basis, while with xargs either all are “long” or none is. An example of *-variant is the following command that has the same syntax as \section:

\NewDocumentCommand{\foo}{ s o m }
 {
  \IfBooleanTF{#1}
    {*-variant called, do something with #3}
    {%
     \IfNoValueTF{#2}
      {normal foo without optional argument, do something with #3}
      {normal foo with optional argument, do something with #2 and #3}%
    }%
 }

So \foo*{Xyz}, \foo{Xyz} or \foo[Abc]{Xyz} are the possible calls. Actually also \foo*[Abc]{Xyz} is allowed, but with the code above, argument #2 would be ignored.

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.