TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

LaTeX seems to be having problems handling nested optional arguments. I've included a minimal code example that generates the error:







If you run this code, it works. If you remove the % in the second to last line, it fails with:

Runaway argument?

Tart{Pop} \par \par \end {document}

!File ended while scanning use of \dd.

After further tinkering, I have discovered the problem: LaTeX is reading the ] for the inner optional argument as the end of the outer optional argument and then passing a malformed argument in. So it is passing in \dd[Tart as the optional argument to \termbad instead of \dd[Tart].

This still feels like a bug to me. Nesting braces in arguments works, brackets should as well. The following are workarounds:

  • Enclose the inner optional argument in braces (as in the line before %).

  • Redefine your inner function to require its argument instead of using optional arguments.

  • Redefine your outer function to require its argument instead of using optional arguments.

Any one of the three works around the issue.

(Reposted here; question was originally posted to stackexchange and then closed instead of being migrated.)

share|improve this question
As far as I can see, the main problem is that LaTeX doesn't define the notion of nested optional arguments. This is why different implementations have different semantics. – Marc van Dongen Oct 20 '12 at 11:57
up vote 22 down vote accepted

When TeX reads arguments, then TeX only checks for matching curly braces (characters with catcode 1 and 2). Square brackets are not special in this sense. The first ] that is not hidden inside curly braces is taken as the end of the optional argument. Therefore an additional set of braces is the usual solution:


It is only a bug, if this does not work, e.g. if the definition passes optional arguments to other commands:


This should be


Then #1 may contain square brackets, especially ].

share|improve this answer
Worth noting that xparse-defined functions do not suffer from this – Joseph Wright Oct 20 '12 at 11:41
Thanks for the answers. I'm looking at xparse now, since I'm using some complex code to create a highly linked and formatted document. This may solve a few of the problems I've been having. – JKreft Oct 23 '12 at 12:33
an answer explicitly showing how xparse is used has been posted at How do I nest a command with an optional argument? – barbara beeton Apr 15 at 15:55

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.