# subfig vs exercise: LaTeX Error: Not in outer par mode

I'm able to use the subfig package in my documents, but not within an exercise environment:

\documentclass{scrreprt}

\usepackage{exercise}
\usepackage[demo]{graphicx}
\usepackage{subfig}

\newcommand{\onefigure}[2]{%
\includegraphics[#1]{../../figures/ece1229/#2}%
}

\newcommand{\fourfigures}[5]{%
\begin{figure}%
\centering%
\subfloat[][]{\onefigure{scale=#1}{#2}}%
\subfloat[][]{\onefigure{scale=#1}{#3}}%
\subfloat[][]{\onefigure{scale=#1}{#4}}%
\subfloat[][]{\onefigure{scale=#1}{#5}}%
\end{figure}%
}

\begin{document}

\begin{Exercise}
%\onefigure{scale=0.2}{blah1}
\fourfigures{0.2}{blah1}{blah2}{blah3}{blah4}
\end{Exercise}

\end{document}


When the subfloat is embedded in an exercise, I get a "Not in outer par mode" error, similar to that of the question ! LaTeX Error: Not in outer par mode. For that question, the error was because a float is not allowed in a minipage.

I'm assuming that there is a similar restriction for floats in exercise environments. Given this restriction, is there a way to make arrange multiple images in groups as with subfig but in some way that is still compatible with exercise?

• Why do you want it in an exercise environment? – cfr Apr 30 '15 at 3:00
• Because the are plots related to solving a problem that I've got in an exercise environment. I actually want the plots in an answer environment of the same package, but I'm sure the idea is the same. – Peeter Joot Apr 30 '15 at 3:06
• But you don't mind that they'll float away? – cfr Apr 30 '15 at 3:07
• I used the [demo] mode for graphicsx in my mwe, and don't actually have a filename called blah in that directory. – Peeter Joot Apr 30 '15 at 3:07
• I'd prefer they be near the answer, but if they aren't I don't mind so long as I can caption them, and refer to the caption in a reference. – Peeter Joot Apr 30 '15 at 3:08

This avoids the use of floats inside the Exercise environment.

\documentclass{scrreprt}
\usepackage[margin=.5in]{geometry}
\usepackage{exercise}
\usepackage[demo]{graphicx}
\usepackage{stackengine,lipsum}

\newcommand\fourfigures[5]{%
\par\medskip{\centering%
\stackunder[6pt]{
\stackunder{\includegraphics[width=#1\textwidth]{#2}}{(a)}
\stackunder{\includegraphics[width=#1\textwidth]{#3}}{(b)}
}{
\stackunder{\includegraphics[width=#1\textwidth]{#4}}{(c)}
\stackunder{\includegraphics[width=#1\textwidth]{#5}}{(d)}
}\par}
}

\begin{document}
Pre-exeercise \lipsum[13]
\begin{Exercise}
In Exercise \lipsum[13]
\fourfigures{0.3}{blah1}{blah2}{blah3}{blah4}
\captionof{figure}{This is my figure caption}
\end{Exercise}

\begin{Exercise}
Next exercise \lipsum[13]
\fourfigures{0.2}{blah1}{blah2}{blah3}{blah4}
\captionof{figure}{This is my figure caption}
\end{Exercise}

\end{document}


Part of the answer was provided in the comments (removing the figure and using the includegraphics directly), but some additional work is required to recover features available in the figure environment. In particular, manual adjustment of the spacing is required, and captions have to be done differently.

On the spacing, by default the spacing doesn't appear to be chosen intelligently with includegraphics, and part of my plots can end up off the edge of the page:

\includegraphics[scale=0.7]{../../figures/ece1229/ps4p1PlotAdEquals0DegreesFig1}


With the spacing adjusted manually, and using the captions package, I can obtain something close to a figure+subfloat look:

\begin{center}
\captionsetup{type=figure}
\par\vspace{5mm}

Unfortunately, I lose the subfig (a), (b), (c), (d) markers, but this is at least close.
• With the stackengine package, you can \stackunder[5pt]{\includegraphics{...}}{(a)}, etc. to recover the subfigure numbers. However, that will still not allow subfigures to be labeled separately. Also, if you are not in a figure environment, you will have to use \captionof{figure}{} instead of \caption. – Steven B. Segletes Apr 30 '15 at 13:53
• You may be able to use \captionof from the caption package together with the subcaption package instead of subfig in order to get the numbering (a), (b), ... again. – cgnieder Apr 30 '15 at 14:06