[Tex/LaTex] put a figure with caption and label inside a theorem (newmdtheoremenv)

floatstheorems

The following MWE doesn't compile if I uncomment the commented lines:

\documentclass[a4paper,10pt]{book}

\usepackage{amsmath}
\usepackage{mdframed}
\usepackage{graphicx}
\usepackage{xcolor}

\mdfdefinestyle{theoremSty}{backgroundcolor=yellow!10, linewidth=0pt, innerleftmargin=3ex, innerrightmargin=3ex, innertopmargin=1ex, innerbottommargin=1ex, innermargin =+1cm, outermargin =+1cm}
\newcounter{theoremCounter}[chapter]
\numberwithin{theoremCounter}{chapter}
\newmdtheoremenv[style=theoremSty]{theorem}[theoremCounter]{Theorem}

\begin{document}

\begin{theorem}

\lipsum[1]

%\begin{figure}
  \includegraphics[width=0.6\linewidth]{example-image-a}
%  \caption{Example image A.}
%  \label{fig:example-image-a}
%\end{figure}

\lipsum[2]

\end{theorem}

\end{document}

The question is why can't I put the figure inside the theorem and if there is a workaround.

(I've checked that the figure works outside the theorem.)

Best Answer

It is possible with \usepackage{floatrow} that defines a new Option H for \begin{figure}[H].

With this, the figure is not floating and just placed where you include it.

You can use caption and label like for floating figures.

\documentclass[a4paper,10pt]{book}

\usepackage{amsmath}
\usepackage{mdframed}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{floatrow}
\usepackage{lipsum}


\mdfdefinestyle{theoremSty}{backgroundcolor=yellow!10, linewidth=0pt, innerleftmargin=3ex, innerrightmargin=3ex, innertopmargin=1ex, innerbottommargin=1ex, innermargin =+1cm, outermargin =+1cm}
\newcounter{theoremCounter}[chapter]
\numberwithin{theoremCounter}{chapter}
\newmdtheoremenv[style=theoremSty]{theorem}[theoremCounter]{Theorem}

\begin{document}

\begin{theorem}

\lipsum[1]

\begin{figure}[H]
%  \includegraphics[width=0.6\linewidth]{example-image-a}
  THE IMAGE
  \caption{Example image A.}
  \label{fig:example-image-a}
\end{figure}

\lipsum[2]

\end{theorem}

Have a look at fig. \ref{fig:example-image-a}.

\end{document}

document