Take the 2-minute tour ×
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.

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?

share|improve this question
    
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
add comment

1 Answer 1

up vote 11 down vote accepted

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.

share|improve this answer
    
Ah of course. Thanks! –  gablin Feb 15 '12 at 13:26
add comment

Your Answer

 
discard

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.