Errors – Fixing the ‘Argument of \@item has an Extra }’ Error in LaTeX with Sphinx

errorssphinx

I get the error

! Argument of \@item has an extra }.
<inserted text> 
                \par 
l.135 \emph{class} \bfcode{C}}

when translating a LaTeX file generated by Sphinx documentation system.

I've managed to reduce it to the following self-contained example:

\documentclass{article}

\usepackage{hyperref}

\newcommand{\pysigline}[1]{\item[#1]\nopagebreak}

\newenvironment{fulllineitems}{
  \begin{list}{}{\labelwidth \leftmargin \labelsep 0pt
                 \rightmargin 0pt \topsep -\parskip \partopsep \parskip
                 \itemsep -\parsep}
}{\end{list}}

\begin{document}

\begin{fulllineitems}
\pysigline{\phantomsection\label{index:project0class_c}\item[] template \textless{}typename T\textgreater{}
\emph{class} \bfcode{C}}
Test class. 
\end{fulllineitems}

\end{document}

As you can see the '{' and '}' are balanced.

What does this error mean and how can I fix it?

Best Answer

A LaTeX optional argument ends at the first ] (unless it is inside a {} group. It does not match [ ] (unless you use the declarations provided by xparse) so in

... \item[...\foo[] ...]  zzz

the argument of \item is not ...\foo[] ... but ...\foo[ and things go wrong.

\pysigline puts its argument in an optional argument so use

\pysigline{{...}}

to hide the []

Or better (if you have access to the definition in your real case) fix the definition to add the extra {} in all cases:

\newcommand{\pysigline}[1]{\item[{#1}]\nopagebreak}
                                 %  %

now a stray ] in the argument to \pysigline is protected from terminating the optional argument of the inner \item