[Tex/LaTex] Figure inside leftbar environment generates error “Not in outer par mode”

errorsfloatsframed

! LaTeX Error: Not in outer par
mode.

I seek a solution to be able to make this figure inside leftbar environment work:

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{framed}

\begin{document}

\begin{leftbar}

\begin{figure}[h!]
  \caption{Mean data points enclosed by shapes are contained within the same group, within 95\% confidence.}
\includegraphics[scale=0.7]{"../Data Analysis Files/InteractionPlotforTransLog10(X+1)Count"}
\end{figure}

\end{leftbar}


\end{document}

@Stefan Kottwitz, your solution is otherwise perfect, but it does introduce this problem of cropping at the end of pages:

enter image description here

Best Answer

Use leftbar inside the figure environment. By changin the nesting you still can let it float. If you don't need floating, you don't need a figure environment: captions can be done then using the caption package (or capt-of).

\begin{figure}[h!]
   \begin{leftbar}
    \caption{Mean data points enclosed by shapes are contained within the same group, within 95\% confidence.}
    \includegraphics[scale=0.7]{"../Data Analysis Files/InteractionPlotforTransLog10(X+1)Count"}
  \end{leftbar}
\end{figure}

A solution without figure, so you can put this and everything else into an outer leftbar environment:

\usepackage{caption}
...
\begin{leftbar}
\begin{minipage}{\columnwidth}
  \captionof{figure}{Mean data points enclosed by shapes are contained within the same group, within 95\% confidence.}
  \includegraphics[scale=0.7]{"../Data Analysis Files/InteractionPlotforTransLog10(X+1)Count"}
\end{minipage}