This is a follow-up to Missing Item number in enumerate. Similar issue is reported, but only under very specific circumstances. In the prior question, the macro DoIfNonEmptyText is defined which executes its second parameter if #1 is non-empty. It has worked fine for me for many cases, but there are a few specific cases where I get
Something's wrong--perhaps a missing \item.
In the MWE below, the first \DoIfNonEmptyText{\ExampleVar}{} is the problem and results in this error message. In actual use case #2 in this call executes other code, but does not produce any output. By disabling the above error message, I get the output shown:
Notice the double item numbers. even though there is only one \item in the enumerate environment.
In this specific case, this problem does not show up if:
wrapfigureis not used, orthe display math in
enumerateis replaced by inline math.
Question:
What changes do I need to make to the DoIfNonEmptyText resolve this issue?
References:
- Missing Item number in enumerate
- How to make itemize/enumerate/description environment robust to missing \item elements?
Code:
\documentclass{article}
\usepackage{wrapfig}
\usepackage[strict]{changepage}
\usepackage{xcolor}\pagecolor{white}
\makeatletter
%% https://tex.stackexchange.com/questions/86547
\newcommand\IgnoreMissingItemError{\let@noitemerr\relax}
\newcommand{\DescriptionVar}{}%
\newcommand{\SetDescription}[1]{\renewcommand{\DescriptionVar}{#1}}
\newcommand{\ExampleVar}{}%
\newcommand{\SetExample}[1]{\renewcommand{\ExampleVar}{#1}}
\newsavebox{@NonEmptyTestBox}
\newcommand{\DoIfNonEmptyText}[1]{% Actually takes two parameters
%% https://tex.stackexchange.com/a/57358/4301 (replaced sbox0 with @NonEmptyTestBox)
\begingroup
\savebox{@NonEmptyTestBox}{%
\vbox{\everypar{}#1}%
}%
\ifdim\wd@NonEmptyTestBox=\z@\relax
\endgroup
\expandafter@gobble
\else
\endgroup
\expandafter@firstofone
\fi
}
\makeatother
\newcommand{\PrintBody}{%
\DescriptionVar%
\IgnoreMissingItemError%
\DoIfNonEmptyText{\ExampleVar}{}% <-- This is problem. Needs \IgnoreMissingItemError
\DoIfNonEmptyText{\ExampleVar}{%
\medskip\par
\textbf{Example:}
\smallskip\par
\begin{adjustwidth}{1.0cm}{0.0pt}
\ExampleVar
\end{adjustwidth}%
}%
}%
\begin{document}
\SetDescription{%
\begin{wrapfigure}{r}{4.0cm}%
\fbox{xxxxxx}%
\end{wrapfigure}%
Some Text
}%
\SetExample{%
\begin{enumerate}
\item [ x + y ]
\end{enumerate}%
}
\PrintBody
\end{document}

wrapfigdoesn't like being too close to a list (or a section). – cfr Oct 17 '23 at 02:05\parafterwrapfigure? And, how doeswragfigureeffect the\DoIfNonEmptyTextmacro? – Peter Grill Oct 17 '23 at 02:09wrapfig'subverts'\@setparand\everypar.wrapfigurechanges\par.... And you're using\everyparetc. I don't know it's a problem. It just rang alarm bells. Andadjustwidthis implemented as a list? Though I'm not sure that itself would be problematic. – cfr Oct 17 '23 at 02:27everypar{}was needed as per egreg's solution at the earlier referenced question. There should be some reset I can do after I am done with thewrapfigure– Peter Grill Oct 17 '23 at 02:35\savebox{\@NonEmptyTestBox}{% \vbox{\everypar{}#1}% }%happens whenwrapfigure's definition of\everyparis in effect. It's reset by\medskip\par. At least, I think so. I don't understand this stuff anything like as well as virtually everybody else. But\show\everyparshows the toks register change. (If that's the term:\toks12,\toks17.) Or is that normal? – cfr Oct 17 '23 at 02:44