For some reason, I can't seem to put a figure or a table environment inside a solution environment while using the exam class. It produces a float is lost error. Has anyone found a way to bypass this or has an explanation as to why it does not work. I'm thinking it might have to do with how the solution environment is defined, perhaps as a float itself. Here is a sample of code that produces the error.
\begin{solution}
Voici le tableau demandé:\\
\begin{table}[h!]
\begin{tabular}{|c|c|c|}
\caption{Répartition des collèges publics du Québec selon la langue d'enseignement}
\hline
Langue d'enseignement & Effectifs & Fréquences\\
\hline
Français & $41$ &$ 0.854$\\
\hline
Anglais & $ 5$ &$ 0.104$\\
\hline
Bilingue & $ 2$ &$ 0.042$\\
\hline
\end{tabular}
\caption{Répartition des collèges publics du Québec selon la langue d'enseignement}
\label{langue2}
\end{table}
\end{solution}
Best Answer
The
solution
environment internally uses a\vbox
and this has as a consequence that floats are not allowed inside this environment. Instead of using the floating environmenttable
you can use acenter
environment and the\captionof
command (from thecaption
package) to provide the caption (now the tabular material won't float, of course):Not directly related to the issue of the question, but you might be interested in the
booktabs
package to design your tables (vertical rules won't be allowed, but this is an advantage in most cases). Here's the previous code, usingbooktabs
:A new request has been made in a comment: if
\SolutionEmphasis
has been used to change the solutions formatting,\captionsetup
can be used so that the formatting for the caption used inside asolution
environment matches the one of the solutions; a little example in which\SolutionEmphasis{\color{blue}}
is used:Instead of
font=blue
in the above code, one can uselabelfont=blue
to change only the color for the caption label, ortextcolor=blue
to change only the color for the caption text.