TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The \newcommand macro allows to use an optional argument for the first parameter #1 with:

\newcommand{\mycommand}[3][defaultfor1]{blah blah blah}

Is it possible to have more than one option with \newcommand?

share|improve this question
No-one said it explicitly, but the answer to the question Is it possible to have more than one option with \newcommand? appears to be No, not without extra packages. – PatrickT May 21 at 17:06
up vote 60 down vote accepted

Try the LaTeX 3 package xparse. Example

\DeclareDocumentCommand{\foocmd}{ O{default1} O{default2} m }{#1~#2~#3}

  \foocmd{foo} \par
  \foocmd[nondefault1]{foo} \par
  \foocmd[nondefault2][notfoo2]{foo} \par

You may read the documents for more information.

share|improve this answer
Agreed - the flexibility with xparse even allows for an optional starred * version of the command \foocmd by using the s parameter. See the xparse documentation. – Werner Sep 29 '11 at 20:59
Does that also remove the 9 parameters arguments limit? – ℝaphink Sep 29 '11 at 20:59
Note that optional arguments defined by xparse work properly when nested, so \foo[[bar]]{stuff} works to parse [bar] as the optional argument. That is not true for \newcommand. – Joseph Wright Sep 29 '11 at 21:01
@Raphink No, as the underlying mechanism is still TeX based. We really don't want to encourage >9 arguments for any macro: use keyval input! – Joseph Wright Sep 29 '11 at 21:02
Selecting this answer, since it shows a nice example of \DeclareDocumentCommand and egreg already reached rep cap today anyway ;-) – ℝaphink Sep 29 '11 at 21:05

You've got answers for other approaches, so here is the basics using the kernel only. You'll need to define your macros by hand, something like

  % Do stuff

This can of course be extended to more complex cases. (Here, I'm leaving it up to you whether to make your function \long or not. If you want part of it to be long, then all of the internal macros should be long. The xparse approach allows this to vary between arguments, based on some internal shuffling.)

share|improve this answer
Thanks, that's interesting. I've seen such constructions in some packages already, but I'd really like to avoid it :-) – ℝaphink Sep 29 '11 at 21:00
I was just writing a solution like this one! – Gonzalo Medina Sep 29 '11 at 21:00
Well I'd just got to the end of this, and was starting on twoopt and xparse, when the other two answers popped up :-) – Joseph Wright Sep 29 '11 at 21:03
\newcommandtwoopt{\xyz}[3][Def1][Def2]{Something with #1, #2 and #3}

There are also \renewcommandtwoopt and \providecommandtwoopt.

However the xparse package (part of the LaTeX3 package tree, but works also with LaTeX2e) provides \DeclareDocumentCommand that gives great flexibility in defining commands with optional arguments in every position and also *-versions.

Historical note

In TeX Live there is a newcommand.py Python script accompanied by documentation accessible by texdoc newcommand. For example, to obtain code for defining a command with two optional arguments and a mandatory one, one launches

python /usr/local/texlive/2011/texmf-dist/doc/latex/newcommand/newcommand.py

and at the prompt % Prototype: answers with

MACRO twoopt OPT[#1={Def1}] OPT[#2={Def2}] #3

where \twoopt is the macro we want to define. The script will build a skeleton for defining the macro:


  % Put your code here.
  % You can refer to the arguments as #1 through #3.

The commented lines should be replaced by the actual code.

With the specification

MACRO finalopt #1 OPT[#2={default}]

one defines a macro with a mandatory argument followed by an optional one:


  % Put your code here.
  % You can refer to the arguments as #1 and #2.
share|improve this answer
@egreg: So twoopt is only limited to two optional arguments I guess? – ℝaphink Sep 29 '11 at 20:59
Yes. See Yan Zhou's answer for more optional arguments. – egreg Sep 29 '11 at 21:01

There is a disadvantage using two or more optional arguments. If the user wants to change the default for the second argument, then he must specify the default value of the first argument explicitly. As an example, the optional depth argument of \raisebox should be set, but the resulting height should not be changed. That makes the first optional argument cumbersome:


An alternative approach is to design the macro with one optional argument exactly, but supporting the key value interface. Nice would be:


Also it is easy to define new keys to support new features without changing the signature of the command (number and type of options).

There are many packages that support defining and parsing keys (e.g., keyval and similar packages, pgfkeys, …).

share|improve this answer

One more resolution for several optional arguments problem.


\newcommandx{\foo}[4][1=defaultFirstArg, 3=defaultThirdArg]{#1~#2~#3~#4}


I hope there is nothing to comment here. Also \renewcommandx, \newenvironmentx, \renewenvironmentx, \providecommandx, \DeclareRobustCommandx, \CheckCommandx and their starred versions are available.

Upd. As far as you can see in my example two optional arguments with madatory one in between give you possibility to specify any one of them.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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