1

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}}%
\qquad%
\subfloat[][]{\onefigure{scale=#1}{#3}}%
\qquad%
\subfloat[][]{\onefigure{scale=#1}{#4}}%
\qquad%
\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?

Peeter Joot
  • 3,025
  • 2
  • 25
  • 35
  • Why do you want it in an exercise environment? – cfr Apr 30 '15 at 03: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 03:06
  • But you don't mind that they'll float away? – cfr Apr 30 '15 at 03: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 03: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 03:08
  • Sorry. Missed the demo. – cfr Apr 30 '15 at 03:17
  • subfig is irrelevant, as is the use of KOMA Script. \documentclass{article} \usepackage{exercise} \usepackage[demo]{graphicx} \begin{document} \begin{Exercise} \begin{figure} \onefigure{scale=0.2}{tiger} \end{figure} \end{Exercise} \end{document} reproduces the error. – cfr Apr 30 '15 at 03:28
  • You can use caption to create captions outside floats. So you could just put the images in minipages and use \captionof... to caption them. – cfr Apr 30 '15 at 03:30
  • just remove the figure and use four \includegraphics commands – David Carlisle Apr 30 '15 at 07:30

2 Answers2

2

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)}
\qquad%
\stackunder{\includegraphics[width=#1\textwidth]{#3}}{(b)}
}{
\stackunder{\includegraphics[width=#1\textwidth]{#4}}{(c)}
\qquad%
\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}

enter image description here

1

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}
\includegraphics[scale=0.7]{../../figures/ece1229/ps4p1PlotAdEquals90DegreesFig2}
\includegraphics[scale=0.7]{../../figures/ece1229/ps4p1PlotAdEquals180DegreesFig3}
\includegraphics[scale=0.7]{../../figures/ece1229/ps4p1PlotAdEquals270DegreesFig4}

four figures

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}
\includegraphics[scale=0.7]{../../figures/ece1229/ps4p1PlotAdEquals0DegreesFig1}
\qquad
\includegraphics[scale=0.7]{../../figures/ece1229/ps4p1PlotAdEquals90DegreesFig2}
\par\vspace{5mm}
\includegraphics[scale=0.7]{../../figures/ece1229/ps4p1PlotAdEquals180DegreesFig3}
\qquad
\includegraphics[scale=0.7]{../../figures/ece1229/ps4p1PlotAdEquals270DegreesFig4}
\caption{Plots of ....}
\label{fig:fourangles}
\end{center}

four figures 2

Unfortunately, I lose the subfig (a), (b), (c), (d) markers, but this is at least close.

Peeter Joot
  • 3,025
  • 2
  • 25
  • 35
  • 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
  • If I use \captionof{figure}{(a)} or \subcaption{}, something forces a \par between the figures. Any idea how to suppress that? – Peeter Joot Apr 30 '15 at 16:13