0

Closing bracket in math mode causes error in description item and leaves a space in text mode. What can I do?

This gives error

\documentclass{article}

\begin{document} \begin{description} \item[$[0,1]$] \end{description} \end{document}

l.6 \end{description}

? ! Missing $ inserted. <inserted text> $ l.6 \end{description}

?

And this code leaves a space before the closing bracket

\documentclass{article}

\begin{document} \begin{description} \item[[0,1]] \end{description} \end{document}

space before bracket

  • 1
    you have \item[$[0,1] so the argument has an unclosed math mode, use \item[{$[0,1]$}] – David Carlisle Mar 23 '24 at 19:25
  • Thanks @davidcarlisle, even if I close the math mode, I get error, but yeah, what you suggest works and makes sense. – Máté Wierdl Mar 23 '24 at 19:28
  • 1
    you wouldn't get an error if you close the math before the ] unlike {} tex does not match [] the argument for \item (and all classical latex optional arguments) ends at the first ] – David Carlisle Mar 23 '24 at 19:31
  • I am not sure, I understand: I do have \item[$[0,1]$] not \item[$[0,1]], so I thought I did close the math mode, and I still get error about missing $. The label stands for the 0-1 closed interval. But I'll use grouping from now on... – Máté Wierdl Mar 23 '24 at 21:28
  • @campa, for me the surprise was that closing math mode still throws error. The example you linked to is not about math mode. – Máté Wierdl Mar 23 '24 at 21:32
  • I now get it, @campa, though the linked answer is way over my head. – Máté Wierdl Mar 23 '24 at 21:50

1 Answers1

2

Classical LaTeX optional arguments (defined in the format, or via \newcommand) do not pair matching [..] the argument ends at the first ]

so

You have \item[$[0,1] so the argument $[0,1 has an unclosed math mode, use \item[{$[0,1]$}]

Your second example is similar, you get no error but \item[[0,1]] is an \item with optional argument specifying a label of [0,1 and the start of the item text being ], you would want \item[{[0,1]}] so the optional argument was [0,1]

David Carlisle
  • 757,742
  • So a second $ in itself doesn't close math mode, as in \item[$[0,1]$]. Got it. – Máté Wierdl Mar 23 '24 at 21:35
  • 1
    @MátéWierdl tex never reaches the second $ as it typesets the label $[0,1 so throws a missing $ error, if you try to scroll that it will auto-add a $ to force close the math, so then the $] at the start of the item tex will start a new unwanted math group and put all the following text in math mode. – David Carlisle Mar 23 '24 at 21:46