Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
You currently have two \caption statements: one of them is redundant, right? It would also be helpful if you converted your code into a Minimum (non)Working Example (MWE). –  Mico Oct 1 '12 at 1:19
    
yeah, I moved things around trying to solve the problem, one of them has a % before it in my code –  Jean-Sébastien Oct 1 '12 at 1:25
    
A different method might be to put the float outside the solution environment (Voir Tableau~\ref{langue2}) so that it's allowed to float. I don't think that, if the table is not floating, a caption is necessary. –  egreg Oct 1 '12 at 9:24

1 Answer 1

up vote 11 down vote accepted

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.

share|improve this answer
    
Thats about what I had resolved to, but I didn't know about captionof so thanks. I was thinking this was related to the solution environment. And yes I agree I need to learn how to design better tables, but for now, this will suffice. Thank you –  Jean-Sébastien Oct 1 '12 at 1:27
    
Nothing too bad, but I have a solutionemphasis that puts the text color to blue when I print solutions. The label "Table 2"appears black. Do you know of anyway simple way I could change that to blue? –  Jean-Sébastien Oct 1 '12 at 1:46
    
@Jean-Sébastien of course, it's dinner time here, so in 30minutes I will provide the code. The idea is to use \captionsetup to add the blue color to the label. –  Gonzalo Medina Oct 1 '12 at 1:47
    
Alright Thanks alot –  Jean-Sébastien Oct 1 '12 at 1:50
1  
@Jean-Sébastien I updated my answer. –  Gonzalo Medina Oct 1 '12 at 2:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.