14

In a description like

\begin{description}
\item[\texttt{DoSomething(items[])}:]
...
\end{description}

, how do I escape the "]" character?

lockstep
  • 250,273
ptikobj
  • 735
  • 1
    I was obviously having some kind of meltdown here, I was reading this question even as I was suggesting \[\]. I blame the lack of coffee. – You Jul 19 '11 at 10:43
  • 1
    In this particular case you don't need any special precaution, of course, since the ] is already protected by braces. In general \item[...{...[]...}...] – egreg Jul 19 '11 at 10:51

1 Answers1

23

Group only the brackets or the whole expression with additional curly braces.

\documentclass{article}

\begin{document}
  \begin{description}
    \item[\texttt{DoSomething(items{[]})}:]
    \item[{\texttt{DoSomethingElse(items[])}:}]
  \end{description}
\end{document}
  • 1
    This is the correct answer which I would have posted as well. I therefore undeleted it. – Martin Scharrer Jul 19 '11 at 10:44
  • 3
    The reason a ] breaks and adding { } works is that optional arguments are not handled like normal ones (which can be cascaded) but are read as part of a macros parameter text from the [ to the very next ]. Placing the ] inside { } effectively hides it from TeX token scanner which is forced to obey the group. Note that macros defined with xparse handle cascaded [ ] correctly (through some extra effort done by that package). – Martin Scharrer Jul 19 '11 at 10:49
  • @Martin: Thanks. I wasn't sure since \item[\texttt{DoSomething(items[])}:] works as is and doesn't need grouping. Additional braces are only necessary when writing \item[{DoSomething(items[]):}]. – Thorsten Donig Jul 19 '11 at 15:51
  • @Thorsten: Any braces around ] will do. It doesn't matter if they are part of a macro argument or not. – Martin Scharrer Jul 19 '11 at 15:52