2

The following gives me LaTeX Error: Float(s) lost. I saw on Lost float in exam class that this might have to do with the \vbox. Replacing it with an \hbox gives Something's wrong--perhaps a missing \item. How can this be solved?

\documentclass{scrartcl}

\usepackage[american]{babel}

\usepackage{ifthen}
\usepackage{xparse}

\newboolean{sol}
\setboolean{sol}{false}

% solution environment
% see https://tex.stackexchange.com/questions/172404/solution-environment-via-true-false-switch?noredirect=1#172408
\NewDocumentEnvironment{solution}{O{sol}}{%
  \ifthenelse{\boolean{#1}}{\par\noindent{\sffamily\bfseries Solution}\par}{\setbox0=\vbox\bgroup}
}{\ifthenelse{\boolean{#1}}{}{\egroup}}

\begin{document}
\begin{solution}
  \begin{enumerate}
  \item foo
    \begin{figure}[!htbp]
    \end{figure}
  \end{enumerate}
\end{solution}
\end{document}
  • Why do you need a figure float in the solution environment? If it is to include an image, you can use \includegraphics as-is without wrapping it inside a figure float. – Werner Apr 23 '14 at 21:20

1 Answers1

5

You're using the wrong tool.

\documentclass{scrartcl}

\usepackage[american]{babel}

\usepackage{ifthen}
\usepackage{environ}

\newboolean{sol}
\setboolean{sol}{false}

\NewEnviron{solution}[1][false]{%
  \csname if#1\endcsname
    \trivlist
    \item\relax\textbf{Solution.}\enspace
    \BODY
    \endtrivlist
  \fi
}


\begin{document}
\begin{solution}
  \begin{enumerate}
  \item foo
    \begin{figure}[!htbp]
    \end{figure}
  \end{enumerate}
\end{solution}

\begin{solution}[true]
  \begin{enumerate}
  \item foo
    \begin{figure}[!htbp]
    \centering
    \Huge A
    \caption{An A}
    \end{figure}
  \end{enumerate}
\end{solution}
\end{document}
egreg
  • 1,121,712