# Curious behaviour of nested list environments

Could someone explain what is happening in the example below? It looks like a bug in the exam document class to me.

\documentclass{exam}
\begin{document}
\begin{questions}
\question
This is a question.
\begin{parts}
\part
\begin{subparts}
Text in the wrong place.
\subpart
A subpart.
\end{subparts}
\end{parts}
\end{questions}
\end{document}


The subparts environment is defined using a list, so I expected the 'Text in the wrong place' to trigger a 'missing item' error, but it does not.

-
Could the handing part have something to do with it? –  Sean Allred Jun 3 '13 at 20:02
@SeanAllred --- I certainly wouldn't rule that out. –  Ian Thompson Jun 3 '13 at 20:15
What happens when you remove \part? (I believe these are all wrappers for \item on the inside, and the environments are souped up enums.) I don't have a TeX distribution handy :( it's a sad, sad day (Alternatively, you could add text after \part - that would answer the question just as well.) –  Sean Allred Jun 3 '13 at 20:30
@SeanAllred --- Either one gets you a 'missing item' error. –  Ian Thompson Jun 3 '13 at 20:50

No, it's not a bug in the exam class. It's the (perhaps unexpected?) behaviour for lists, as can be seen in this example with article and the standard itemize environment; the following code doen's produce any errors:

\documentclass{article}

\begin{document}

\begin{itemize}
\item
\begin{itemize}
Some test text here
\item First subitem?
\end{itemize}
\end{itemize}

\end{document}


Notice, however, that "Some test here" and "First subitem?" are now with second-level indentation, and the former has label, but the latter doesn't.

What happens is that the whole inner itemize is taken for the \item in the outer itemize, "Some test text here" will be picked by this outer \item an no error will be triggered.

In the LaTeX kernel one finds (only some relevant parts are shown here):

def\item{%
\@inmatherr\item
\@ifnextchar [\@item{\@noitemargtrue \@item[\@itemlabel]}}

\def\@item[#1]{%
\if@noparitem
\@donoparitem
\else
\if@inlabel
\indent \par
\fi
\ifhmode
\unskip\unskip \par
\fi
\if@newlist
<some more code goes here>
}


when the inner \item is found, TeX is in horizontal mode, so the only effect will be that of a \par command.

Something different happens as you provide some text for the outer \item:

\documentclass{article}

\begin{document}

\begin{itemize}
\item A
\begin{itemize}
Some test text here
\item First subitem?
\end{itemize}
\end{itemize}

\end{document}


This time the familiar error

! LaTeX Error: Something's wrong--perhaps a missing \item.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...

l.9 \item F
irst subitem?
?


is triggered.

The same is happening with your example code, since the involved environments are defined in terms of \list.

-
The behaviour of the labels is bizarre. 'Some test text here' gets an outer label in the inner position, and 'First subitem' gets none at all. –  Ian Thompson Jun 3 '13 at 21:14
@IanThompson of course it's bizarre; the syntax for lists has been abused so some consequences will arise. –  Gonzalo Medina Jun 3 '13 at 21:19
Since it's not the proper syntax I would expect an error message. Is it not possible to detect this type of 'abuse'? –  Ian Thompson Jun 3 '13 at 21:21
Oh the woes of being at work. +1! It does indeed go back to the very definition of list environments---whether intended or not, people are probably using this 'feature' by now.\ –  Sean Allred Jun 3 '13 at 21:51
@IanThompson I've added a little comment for the behaviour of the labels. As to if there's some way to prevent this abuse, perhaps it would be possible, but since the LaTeX2e kernel is frozen, the modification would have to be done through a package, and I wouldn't know if it's worth. –  Gonzalo Medina Jun 3 '13 at 22:16