10

Trying to use figure inside an Exercise environment (exercise.sty) gives me "LaTeX Error: Not in outer par mode."

Example:

\documentclass{article}

\usepackage[]{exercise}
\begin{document}

\begin{Exercise}[title={foo}, label=ex1]
A tough question.
\end{Exercise}

\begin{Answer}[ref={ex1}]
A smart answer.

\begin{figure}[htp]
   \centering
   \includegraphics[totalheight=0.2\textheight]{couetteFlowFig1.png}
   \label{fig:ex1}
\end{figure}
\end{Answer}

\end{document}

I see in Is it possible to use the figure environment with the standalone package? that there was a similar problem with a different package, and that limitation in the package was eventually handled. It appears the same limitation exists in the exercise environment. Is there a work around that allows for inclusion of a figure with that package, or a different way of including a figure when using this package?

Peeter Joot
  • 3,025
  • 2
  • 25
  • 35

2 Answers2

14

There is no requirement to place an image (using \includegraphics, say) inside a figure environment. The figure environment is merely a floating placeholder. So, you can just put your image inside a center environment to centre it on the page, and even add a caption using \captionof from the caption package.

enter image description here

\documentclass{article}
\usepackage{caption}% http://ctan.org/pkg/caption
\usepackage{exercise}% http://ctan.org/pkg/exercise
\begin{document}

\begin{Exercise}[title={foo}, label=ex1] A tough question. \end{Exercise}

\begin{Answer}[ref={ex1}] A smart answer.

\begin{center} %\includegraphics[totalheight=0.2\textheight]{couetteFlowFig1.png} \rule{150pt}{100pt} \par \captionof{figure}{This is a figure caption.} \label{fig:ex1} \end{center} \end{Answer}

\end{document}

In the above example, I've replaced the image with a rectangular block to simulate the image, but it will work with \includegraphics as well (for which you will require \usepackage{graphicx} - the graphicx package).

The problem originates from inserting figure inside Exercise. Or, more generally, a float inside a non-floating (or restrictive) environment. Therefore, removing the floating environment enables the compilation to succeed.

Werner
  • 603,163
  • Martin scharrer has a new package called mwe now @ Werner. I like it as it will save me from typing all those details regarding rectangular block. –  May 11 '12 at 05:54
  • 2
    This gave me hypcap=true warnings (and odd link placement), but otherwise worked very nicely. I was able to avoid the hypcap and link placement issue by using an alternate caption syntax:

    \newcommand{\imageCentered}[4]{% \begin{center}% \captionsetup{type=figure}% \includegraphics[totalheight=#4\textheight]{#1}% \caption{#2}% \label{#3}% \end{center}% }

    – Peeter Joot May 11 '12 at 06:08
0

A simple option is to use float package as follows. In this case figure environment works fine together with \caption. The code is quite similar to @Werner's answer, with only a few lines changed.

\documentclass{article}
\usepackage{caption}% http://ctan.org/pkg/caption
\usepackage{exercise}% http://ctan.org/pkg/exercise
\usepackage{float}
\begin{document}

\begin{Exercise}[title={foo}, label=ex1] A tough question. \end{Exercise}

\begin{Answer}[ref={ex1}] A smart answer in Fig. \ref{fig:ex1}.

\begin{figure}[H] %\includegraphics[totalheight=0.2\textheight]{couetteFlowFig1.png} \rule{150pt}{100pt} \par \caption{This is a figure caption.} \label{fig:ex1} \end{figure} \end{Answer}

\end{document}

Moshe Gueta
  • 435
  • 1
  • 4
  • 15