Is it possible to build a macro with an optional parameter enclosed in round brackets () rather than square brackets, where one can say for example:




A more traditional solution!

To build a command with any optional parameter, as you find in many of LaTeX's commands, you will need two things:

(a) a macro with delimited parameters

(b) a way to grab the first non-space token that follows the command

The first part is fairly easy using delimited argument macros, for example we can say

 \def\test(#1)#2#3{#1, #2, #3}

We can then call this macro as:


resulting in a,b,c

To define the () as an optional parameter, we effectively need to define the macro as a conditional a sort of a "yes-no" switch. If TeX finds the "(" bracket the "yes-code" will be called and if it finds only the normal arguments the "no-code" will be executed.

For this we can use the \@ifnextchar macro from the LaTeX kernel. You can say \@ifnextchar{char}{yes-code}{no-code} to test for (. The result then will depend on the token that follows. If this token is the same as the first argument, then the "yes-code" is executed, otherwise the "no-code" is executed. The first argument should be a single token (for instance a character). Spaces are ignored.

As for example we can redefine the LaTeX code for rule to accept an optional parameter in round brackets, rather than the traditional square brackets.


and the full example would look like:


A test \Rule(6.5pt){100pt}{1pt}

Another test \Rule{100pt}{1pt}
  • 1
    Very nice explanation! Of course, this has the same feature as LaTeX's own optional parameters: an option of the form (...(...)...) will only grab to the first parenthesis (except if inner ones are hidden within braces). – Bruno Le Floch Mar 7 '11 at 21:09

A simple xparse-based solution:

    {}% Case where #1 is not given
    {}% Case where #1 is given

or if you want an empty #1 when it is not present:

  % Code here

(This works as d/D indicate an optional argument which is delimited.)

  #1:#2:#3}% the definition 
  #1:#2}% the definition 

\mymacro(foo){bar}{baz} -- \mymacro{BAR}{BAZ} 


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.