# A new figure environment for the exam class

The incompatibility of having a float inside the solutions box of the exam class is somewhat well known. Multiple solutions for this exist, almost all of which use some variant of the caption package and the minipage environment.

Why doesn't simply wrapping the figure inside a minipage environment work?

I tried

\renewenvironment{figure}{\begin{minipage}[c]{\linewidth}}{\end{minipage}}

-
Does the exam class define a figure environment which you can redefine? –  Johannes_B Aug 23 '14 at 17:43
No. But graphicx(?) does. –  user2751530 Aug 23 '14 at 18:27
I guess its time for a minimal working example (MWE) –  Johannes_B Aug 23 '14 at 18:36

exam is based on article, and thus the figure environment is defined. You can define a new environment using a minipage, but i do not recommend to redefine the original figure-environment. But doing this, the order of the figures (and the captions) can be messed up pretty badly. You need to decide, let them all float, or let none float.

--Edit-- @tohecz made me aware of the solution environment. This is a serious thing. Traditional floats will just be lost. The just defined questionfigure environment works, but there is another thing. There is a switch to print answers, or not. If not, the counter will still be refstepped and can make everything look more than odd if no answers are printed. I would recommend to define separate environments for questions (always printed) and answers/solutions (only printed if toggled). This would take care of the numbering. But then a question arises: Do you need figures that can be referenced if inside a framed (default) environment?

I defined two separate environments and gave them pretty dumb names. Referencing to figures in question will always work, no matter if called from a question or an answer. Referencing from answers to other answers (or rather the pics in them) works as well. Referencing an answerfigure in a question will not work if printanswers is false, but what would be the use-case? »Ha Ha, there will be a real great image in the answer, but can't see it!«

Disclaimer: If you compile the following (and you should) you need to use your imagination. No ducks were harmed while writing this answer.

\documentclass[
]{exam}%based on article
\usepackage{xcolor}
\usepackage{tocbasic}
\usepackage[tocindentauto]{tocstyle}
\DeclareNewTOC[
name=questionFigure,
listname={List of questionfigures},
type=qfigure,
nonfloat]{loq}
\DeclareNewTOC[
type=afigure,
nonfloat]{loa}

\makeatletter
\renewcommand\theqfigure{\thenumquestions}
\newenvironment{questionfigure}{%
\begin{center}%
\begin{minipage}[c]{\linewidth}%
\def\@captype{qfigure}%
}{%
\end{minipage}%
\end{center}%
}
\renewcommand\theafigure{\thenumquestions-\alph{afigure}}
\begin{center}%
\begin{minipage}[c]{\linewidth}%
\def\@captype{afigure}%
}{%
\end{minipage}%
\end{center}%
}
\makeatother
\newcommand\rplcPic{\rule{4cm}{3cm}}
\usepackage{hyperref}
\begin{document}
\listofqfigures
%Print the list of afigures only if answers toggled
\listofafigures
\fi
\begin{questions}
\question Name a place where the lights are always on.
\begin{solution} The light in my fridge is always
on.
\rplcPic
\caption{Almost all refridgerators
provide the feature of \emph{eternal}
light.}
\end{solution}
\question Look at the following picture. What do
you conclude?
\begin{questionfigure}
\rplcPic
\caption{An open fridge. What do you conclude?}
\label{fridge}
\end{questionfigure}
\begin{solution}
Seeing the footprints in the butter, i can conclude that there must
have been an elephant in there earlier.
\end{solution}
\question Homer was one of the greatest greek
philosophers. Sum up his philosophy in as few words as
possible.
\begin{solution}
Beer, Donuts.
\end{solution}
\question Nucleaar weapons are terrible. Describe and
explain the physical reactions of an atomic bomb.
\begin{solution}
A picture is worth more than a thousand words.
\begin{verbatim}
_-^--^=-_
_.-^^          -~_
_--                  --_
<                       >)
|                        |
\._                   _./
--. . , ; .--'''
| |   |
.-=||  | |=-.
-=#$%&%$#=-'
| ;  :|
_____.,-#%&\$@%#&#~,._____
\end{verbatim}
\caption{BOOOM}
\label{boom}
\end{solution}
\question When to \emph{Duck and cover}?
\begin{solution} See \autoref{boom}!
\end{solution}
\question{Is it easy to change a refridgerator light bulb?}
\begin{solution} Sure, according to \autoref{fridge} an elephant
lives in there. Just place the light bulb inside, the
elephant will do the work.
\end{solution}
\question You see a block of iron glowing with an orange color
(see the picture).
What does that tell you about the temperature?
\begin{questionfigure}
{\color{orange!40!white}{\rplcPic}}
\caption{A gloowing block of iron}
\end{questionfigure}
\begin{solution} Oh, that's hot. Can't touch this!
\end{solution}
\question Did you ever use Plan 9 from Bell Labs?
\begin{solution} 42! Glenda is cool. See \autoref{boom}.
\end{solution}
\end{questions}
\end{document}

-
Thanks!! I was away from work for a few days. Yes, I do need the ability to refer to these figures. –  user2751530 Sep 1 '14 at 9:46
@user2751530 I'll have a look at it. –  Johannes_B Sep 1 '14 at 9:50
@user2751530 Sorry, your request somehow got lost in my stack. I extended my answer a little bit. –  Johannes_B Sep 20 '14 at 15:22