[Tex/LaTex] Lost float in exam class

examfloatspositioningtables

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 environment table you can use a center environment and the \captionof command (from the caption package) to provide the caption (now the tabular material won't float, of course):

\documentclass[answers]{exam}
\usepackage[utf8]{inputenc}
\usepackage{caption}

\begin{document}

\begin{questions}
\question Test.
\begin{solution}
Voici le tableau demandé:
\begin{center}
\captionof{table}{Répartition des collèges publics du Québec selon la langue d'enseignement}
\label{langue2}
\begin{tabular}{|c|c|c|}
\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}
\end{center}
\end{solution}
\end{questions}

\end{document}

enter image description here

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, using booktabs:

\documentclass[answers]{exam}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{caption}

\begin{document}

\begin{questions}
\question Test.
\begin{solution}
Voici le tableau demandé:
\begin{center}
\captionof{table}{Répartition des collèges publics du Québec selon la langue d'enseignement}
\label{langue2}
\begin{tabular}{@{}ccc@{}}
\toprule
Langue d'enseignement & Effectifs & Fréquences\\
\cmidrule(r){1-1}\cmidrule(lr){2-2}\cmidrule(l){3-3}
Français & $41$ &$ 0.854$\\
Anglais  & $ 5$ &$ 0.104$\\
Bilingue & $ 2$ &$ 0.042$\\
\bottomrule
\end{tabular}
\end{center}
\end{solution}
\end{questions}

\end{document}

enter image description here

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 a solution environment matches the one of the solutions; a little example in which \SolutionEmphasis{\color{blue}} is used:

\documentclass[answers]{exam}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{caption}

\DeclareCaptionFont{blue}{\color{blue}}
\SolutionEmphasis{\color{blue}}

\begin{document}

\begin{questions}
\question Test.
\begin{solution}
Voici le tableau demandé:
\begin{center}
\captionsetup{font=blue}
\captionof{table}{Répartition des collèges publics du Québec selon la langue d'enseignement}
\label{langue2}
\begin{tabular}{@{}ccc@{}}
\toprule
Langue d'enseignement & Effectifs & Fréquences\\
\cmidrule(r){1-1}\cmidrule(lr){2-2}\cmidrule(l){3-3}
Français & $41$ &$ 0.854$\\
Anglais  & $ 5$ &$ 0.104$\\
Bilingue & $ 2$ &$ 0.042$\\
\bottomrule
\end{tabular}
\end{center}
\end{solution}
\end{questions}

\end{document}

enter image description here

Instead of font=blue in the above code, one can use labelfont=blue to change only the color for the caption label, or textcolor=blue to change only the color for the caption text.

Related Question