Consider the following MWE:
\documentclass{memoir}
%% Create 123 box registers
\newcount\X
\X=1
\loop
\expandafter\newbox\csname mybox\the\X\endcsname
\advance \X by 1
\ifnum \X<124
\repeat
%% Create a box register and globally store ans empty hbox
\newcommand{\storebox}{%
\advance \X by 1
\expandafter\newbox\csname tempbox\the\X\endcsname
\expandafter\global\expandafter\setbox\csname tempbox\the\X\endcsname\hbox{}
}%
\begin{document}
%% Call \storebox 11 times in a group a total of 5 times
\begingroup
\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox
\endgroup
\begingroup
\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox
\endgroup
\begingroup
\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox
\endgroup
\begingroup
\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox
\endgroup
\begingroup
\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox\storebox
\endgroup
%% Some text so the document isn't empty if it compiles
foo
\end{document}
Compiling with a current TeX Live 2015 with either pdflatex or lualatex yields the following error message:
! Incompatible list can't be unboxed.
\sidecontents ... \z@ {\m@mwhich@margin {\m@msidebar@margin }\ifmemtortm \m@sideb@right \else \m@sideb@left \fi \vtop to0pt{\normalsize \normalfont \sidebarfont \vskip \topskip \vskip -\ht \strutbox \vskip \sidebartopsep \unvbox \sideins
\vss }\hss }
The above example is a simplified version of a much larger (and more useful) document and is extremely sensitive to change. So, for example, calling \newbox a total of 122 or 124 times instead of 123 times in the preamble eliminates the error.
Looking at the log, the box registers behave strangely around 195 as shown below:
\tempbox132=\box192
\tempbox133=\box193
\tempbox134=\box194
\tempbox135=\box256
\tempbox125=\box196
\tempbox126=\box197
\tempbox127=\box198
\tempbox128=\box199
\tempbox129=\box200
\tempbox130=\box201
\tempbox131=\box202
\tempbox132=\box203
\tempbox133=\box204
\tempbox134=\box205
\tempbox135=\box206
! Incompatible list can't be unboxed.
\sidecontents ... \z@ {\m@mwhich@margin {\m@msidebar@margin }\ifmemtortm \m@sideb@right \else \m@sideb@left \fi \vtop to0pt{\normalsize \normalfont \sidebarfont \vskip \topskip \vskip -\ht \strutbox \vskip \sidebartopsep \unvbox \sideins
\vss }\hss }
I first noticed this issue after the recent update to the LaTeX kernel (2015/10/01), and loading \usepackage[2015/01/01]{latexrelease} in the production document did allow the original document to compile. However, the MWE above shows that the kernel update is unrelated as adding the latexrelease package has no effect.
So what is the root cause of this error in this example and how can I properly prevent it from happening in similar circumstances? We can obviously get the error to go away easily by making almost any change to the document, but that is not really an option on the original production document.
LaTeX2e <2015/10/01> patch level 2(which uses the fix as in the answer below) (that format hasn't gone to ctan yet). – David Carlisle Oct 27 '15 at 15:00