[Tex/LaTex] List spacing is different inside and outside of minipage

#enumerateexamminipagespacing

I'm preparing an exam and I want an illustration next to a multiple-choice problem. Usually for this purpose I put the question and/or choices in a multicols environment; however for this particular question the fifty-fifty split of the page width by multicols makes the text of every option wrap, while the image is fairly narrow.

I thought I could get what I wanted by putting the choices in a minipage, but the minipage changes the spacing between the choices:

    \documentclass{article} %% 'exam' class not needed for MWE
    \begin{document}
    \begin{enumerate}

    \item
    This is a standard nested enumeration.
    \begin{enumerate}
      \item Spacing within wrapped text and between lines of the enumeration
        looks okay to me.
      \item adsflkj adsflkj 
      \item qeworui qeworui 
      \item zcx,vmn zcx,vmn 
      \item lkjasdf lkjasdf 
      \item mbnnert mbnnert 
    \end{enumerate}

    \item
    The possible answers to this question are in a minipage, 
    to accomodate an image to the right

    \begin{minipage}[t]{0.6\linewidth}
      \begin{enumerate}
        \item Spacing within wrapped text is the same, but the spacing
          between items is different.
        \item adsflkj adsflkj 
        \item qeworui qeworui 
        \item zcx,vmn zcx,vmn 
        \item lkjasdf lkjasdf 
        \item mbnnert mbnnert 
      \end{enumerate}
    \end{minipage}
    \hfill
    \fbox{Tikz image here}

    \end{enumerate}
    \end{document}

output

The difference annoys me. How can I repair it?

(I can hack it away, more or less, by doing \addtolength{\itemsep}{-1ex} in the minipage. But displaying \the\itemsep in various places shows that \itemsep isn't actually the length that's being secretly changed. I'd rather understand what's actually happening.)

Best Answer

A minipage does explicitly reset the list depth, see latex.ltx for this, in line 4886 following code can be found:

\let\@listdepth\@mplistdepth \@mplistdepth\z@

The important thing is \@mplistdepth\z@,meaning zero list depth -- The inner enumerate environment behaves like being on the first level again, using the \itemsep value 'appropiate' for this level, being 4.0pt plus 2.0pt minus 1.0pt in this case. (The same is true for itemize). The other spacings are used then as well as if would be on the first level of an enumeration/itemize environment, this is the reason for the indentation of (a) etc. as can be seen from the image in the OP.

Interestingly, the enumeration counter format is not reset, because \@enumdepth still has the value 2 then (being the second level).

One cheap trick is to set the \@mplistdepth counter explicitly to 1, manually, how ever.

\documentclass{article} %% 'exam' class not needed for MWE

\usepackage{enumitem}


\begin{document}


\begin{enumerate}

 \item  This is a standard nested enumeration.
 \begin{enumerate} 
 \item Spacing within wrapped text and between lines of the enumeration
   looks okay to me. 
   \item adsflkj adsflkj 
   \item qeworui qeworui 
   \item zcx,vmn zcx,vmn 
   \item lkjasdf lkjasdf 
   \item mbnnert mbnnert 
 \end{enumerate}

\item The possible answers to this question are in a minipage, 
to accomodate an image to the right   

 \begin{minipage}[t]{0.6\linewidth}
   % minipage does this thing here:       \let\@listdepth\@mplistdepth \@mplistdepth\z@
   \makeatletter
   \@mplistdepth=1
   \makeatother
   \begin{enumerate}
   \item Spacing within wrapped text is the same, but the spacing
     between items is different. 
   \item adsflkj adsflkj 
   \item qeworui qeworui 
   \item zcx,vmn zcx,vmn 
   \item lkjasdf lkjasdf 
   \item mbnnert mbnnert 
   \end{enumerate}
 \end{minipage}
 \hfill
 \fbox{Tikz image here}
\end{enumerate}

\end{document}

A 'better' way is to patch @iiiminipage (the innermost level of the minipage 'environment' and make it aware that it should preserve the list depth, depending on a conditional:

Say \mpsavelistdepthtrue if the preserving should be enabled and \mpsavelistdepthfalse to disable it.

This works for enumerate environments only, since \@enumdepth deals with enumerate, not with itemize (the relevant depth counter is \@itemdepth then)

\documentclass{article} %% 'exam' class not needed for MWE

\usepackage{enumitem}

\usepackage{xpatch}

\newif\ifmpsavelistdepth
\mpsavelistdepthtrue  % Enabling the list depth save for enumerate environments

\makeatletter


\xpatchcmd{\@iiiminipage}{%
  \let\@listdepth\@mplistdepth \@mplistdepth\z@
}{%
  \let\@listdepth\@mplistdepth
  \ifmpsavelistdepth
  \@mplistdepth\@enumdepth  % use the current depth (stored in \@enumdepth
  \fi
}{\typeout{Patching minipage succeeded}}{\typeout{Patching failed}}% End of patching

\makeatother


\begin{document}
\begin{enumerate}
\item  This is a standard nested enumeration.
  \begin{enumerate} 
  \item Spacing within wrapped text and between lines of the enumeration
    looks okay to me. 
  \item adsflkj adsflkj 
  \item qeworui qeworui 
  \item zcx,vmn zcx,vmn 
  \item lkjasdf lkjasdf 
  \item mbnnert mbnnert 
  \end{enumerate}

\item The possible answers to this question are in a minipage, 
  to accomodate an image to the right   

  \begin{minipage}[t]{0.6\linewidth}
    \begin{enumerate}
   \item Spacing within wrapped text is the same, but the spacing
     between items is different. 
   \item adsflkj adsflkj 
   \item qeworui qeworui 
   \item zcx,vmn zcx,vmn 
   \item lkjasdf lkjasdf 
   \item mbnnert mbnnert 
   \end{enumerate}
 \end{minipage}
 \hfill
 \fbox{Tikz image here}
\end{enumerate}

\end{document}

enter image description here

Related Question