# Compilation error when a command argument contains a math formula with a bracket ]

When I try to compile the following document:

\documentclass{article}

\newenvironment{modelDefinitions}{%
\begin{description}
}{%
\end{description}
}
\newcommand{\modelDef}[2]{\item[#1] \hfill \\ #2}

\begin{document}
\begin{modelDefinitions}
\modelDef{$test[a, b]$}{tmp}
\end{modelDefinitions}
\end{document}


I get a compilation error saying:

! Extra }, or forgotten $. \sbox ...hbox {\color@setgroup #2\color@endgroup } l.12 \modelDef{$test[a, b]$}{tmp}  However, if I remove the ] from the first argument of \modelDef, then it works. What's the problem, and how do I fix it? - TeX isn't as good at matching square brackets [ as it is at matching curly braces {. You best bet might be to define a macro \def\foo{[a,b]} and then do \modelDef{$test\foo$}… – Seamus Feb 15 '12 at 12:02 Don't write test in mathmode, because it is taken as a product of four variables. Use at least \mathrm{test} or \mathit{test}. – Andrey Vihrov Feb 15 '12 at 12:05 @AndreyVihrov: I know - just didn't bother since this was an example. But thanks anyway. =) – gablin Feb 15 '12 at 13:26 ## 1 Answer The problem arises due to instant grabbing of the entire [] part in \item. In your case it will grab $test[a, b as the argument, leaving you with an unfinished $ and a floating $ afterwards. Thus you need to tell LaTeX what belongs where.

You can circumvent this issue by adding a group like this

\newcommand{\modelDef}[2]{\item[{#1}] \hfill \\ #2}


Then it will not complain.

-
Ah of course. Thanks! – gablin Feb 15 '12 at 13:26