3

In my preamble I have the following custom environment that is counted.

\newtheorem{exmp}{Example}[section]

When I call this in the text I always write the following piece of code,

\begin{exmp}
\begin{mdframed}[backgroundcolor=blue!20]

Whatever I want to write ... 

\end{mdframed}
\end{exmp}

I then wish to enter a figure within this environment.

 \begin{exmp}
    \begin{mdframed}[backgroundcolor=blue!20]

\begin{figure}[h!]
\centering
\includegraphics[width=8cm, height=5cm]{name-of-figure}
\caption{The caption. }
\label{fig:RC}
\end{figure}

  \end{mdframed}
    \end{exmp}

I then get the the following two errors:

Latex error: Not in outer par mode. 
Missing number, treated as zero. 

Everything compiles fine when the code for the figure is removed. Unfortunately the figure really does have to be within the environment. I have absolutely zero idea on how to go about fixing this and appreciate any help you can offer! As an aside I then wish to match the colour of the image to the colour of the mdframed environment, however I have not attempted this yet for obvious reasons!

2 Answers2

5

The float specifier [h!] doesn't guarantee that a float will not move around/stay here. See How to influence the position of float environments like figure and table in LaTeX? for more details.

Moreover, the error "Not in outer par mode" is due to the fact that you are inserting a floating environment inside a container - mdframed - that doesn't float. The float has to be placed in a location that is not restricted.

Regardless, you don't need a figure (float) environment in order to insert an image. You can just insert it as-is using \includegraphics, and use caption's \captionof{figure} (capt-of provides something similar):

enter image description here

\documentclass{article}

\usepackage{mdframed,xcolor,amsthm,graphicx,caption}

\newtheorem{exmp}{Example}[section]

\begin{document}

\section{A section}

\begin{exmp}
  \mbox{}\par
  \begin{mdframed}[backgroundcolor=blue!20]
    \centering
    \includegraphics[width=8cm, height=5cm]{example-image}
    \captionof{figure}{The caption.}
  \end{mdframed}
\end{exmp}

\end{document}
Werner
  • 603,163
1

As Werner said in his comment, figure doesn't float. Instead of it inser your image directly in your environment, something like this:

 \begin{exmp}
    \begin{mdframed}[backgroundcolor=blue!20]

\begin{center}
\includegraphics[width=8cm, height=5cm]{name-of-figure}
\captionof{figure}{The caption. }
\label{fig:RC}
\end{center}

  \end{mdframed}
    \end{exmp}

For captionof you need package caption or if you for it use facility of documentclass, there is also small package capt-of.

Zarko
  • 296,517