2

I'm having an issue with adding a frame around the solutions environment while using the exsheets package. I have a bit of my code below. My problem is that I'm greeted with an error if I choose to add a \begin{figure} environment within the boxed solution. I've tried a number of workarounds, including modifying my \begin{figure}\end{figure} to, instead, be a \begin{center}\end{center}. However, I also have instances where I wish to use \begin{subfigure}\end{subfigure}. As such, I've tried modifying these subfigures to, instead, use \begin{minipage}\end{minipage}; however, this overly complicates the layout and then the captions are not labelled as I would desire. I've also tried the suggestion given for list environments found here and here. The error I receive while using the figure environment within the mdframe is:

! LaTeX Error: Not in outer par mode.

...

! Undefined control sequence.
\@xfloat ...@fltovf \fi \global \setbox \@currbox 
                                                  \color@vbox \normalcolor \...

I would like to include figures (and ultimately subfigures) in the mdframed solutions. Thanks in advance, and any/all help is much appreciated.

MWE

\documentclass[10pt,twoside,onecolumn]{book}
\usepackage[framemethod=TikZ]{mdframed} % Frames For exsheets
\usepackage{exsheets}                   % Creating Exercises and Solutions

\begin{document}

\chapter{Some Chapter}
\section{Some Section}
\subsection{Some Sub-Section}

\begin{question}
    Here's a question
\end{question}
\mdframed
\begin{solution}[print=true]
    Here's a solutions.
    \begin{figure}
        \includegraphics[width=\textwidth]{logo}
        \caption{diagram}
        \label{fig:solution}
    \end{figure}
\end{solution}
\endmdframed

\end{document}

Per the comment from @leandriis, I've managed to get something working. This is not ideal, as I cannot seem to caption my "sub-images" appropriately; however, here is a close work around. Still hopeful for a solid solution that would keep subfloats and subcaptions intact.

\documentclass[10pt,twoside,onecolumn]{book}
\usepackage[framemethod=TikZ]{mdframed} % Frames For exsheets
\usepackage{exsheets}                   % Creating Exercises and Solutions
\usepackage{capt-of}                    % Add Captions to Images

\begin{document}

\chapter{Some Chapter}
\section{Some Section}
\subsection{Some Sub-Section}

\begin{question}
    Here's a question
\end{question}
\mdframed
\begin{solution}[print=true]
    Here's a solutions.

    \begin{minipage}{\textwidth}
        \begin{minipage}{0.45\textwidth}
            \includegraphics[width=\textwidth]{logo}
            \label{fig:solution-left}
        \end{minipage}
        \begin{minipage}{0.45\textwidth}
            \includegraphics[width=\textwidth]{logo}
            \label{fig:solution-right}
        \end{minipage}
    \end{minipage}
    \captionof{figure}{one caption for all images, just not subs}
\end{solution}
\endmdframed

\end{document}
2
  • 1
    Maybe you can remove the figure environment and replace caption with captionof from the caption package.
    – leandriis
    Sep 13, 2019 at 16:34
  • Actually, using the \capt-of package might be the answer. I've just had success with the mdframed solutions, center environment, and captionof. I'll see if I can get the subfloats---i.e., minipage---working. I might just have to write a new command or environment to make a short cut for my subfigures. Thanks for the tip @leandriis.
    – user641699
    Sep 13, 2019 at 17:30

1 Answer 1

2

The following MWE contains two different possibilities on how you can get subfigures with referenceable captions inside of your framed solution block. In the first one, I have used the float package in combination with the H option for the figure environment Subfigures are inside of the subfigure environment by the subcaption package. In the second example I have used minipages as well as \captionsetup{figure} and \subcaptionbox(also from the subcaption) package. The output of both approaches is similar:

enter image description here

\documentclass[10pt,twoside,onecolumn]{book}
\usepackage[framemethod=TikZ]{mdframed} % Frames For exsheets
\usepackage{exsheets}                   % Creating Exercises and Solutions
\usepackage{subcaption}                 % For subfigures with captions (subcaptionbox or subfigure)
\usepackage{float}                      % For the H option (disables floating of figure environment)
%\usepackage{changepage}                 % For the adjustbox environment

%\usepackage{showframe}                  % Shows the border of the textblock. Don't use in real document
%\renewcommand*\ShowFrameColor{\color{red}}


\begin{document}

\chapter{Some Chapter}
\section{Some Section}
\subsection{Some Sub-Section}


\begin{question}
    Here's a question
\end{question}
%\begin{adjustwidth}{-10pt}{-10pt}
\mdframed
\begin{solution}[print=true]
    Here's a solutions.

    \begin{figure}[H]
        \begin{subfigure}{0.45\textwidth}
            \includegraphics[width=\textwidth]{example-image}
            \caption{my left subfigure}
            \label{fig:solution-left}
        \end{subfigure}\hfill
        \begin{subfigure}{0.45\textwidth}
            \includegraphics[width=\textwidth]{example-image}
            \caption{my right subfigure}
            \label{fig:solution-right}
        \end{subfigure}
        \caption{one caption for all images, just not subs}
    \end{figure}

\end{solution}
\endmdframed
%\end{adjustwidth}

\ref{fig:solution-left}

\ref{fig:solution-right}


\chapter{Some Chapter}
\section{Some Section}
\subsection{Some Sub-Section}


\begin{question}
    Here's a question
\end{question}
%\begin{adjustwidth}{-10pt}{-10pt}
\mdframed
\begin{solution}[print=true]
    Here's a solutions.

\noindent
    \begin{minipage}{\textwidth}
      \captionsetup{type=figure}
      \subcaptionbox{my left subfigure\label{fig:solution-left2}}{\includegraphics[width=0.45\textwidth]{example-image}} \hfill
      \subcaptionbox{my right subfigure\label{fig:solution-right2}}{\includegraphics[width=0.45\textwidth]{example-image}}
        \caption{one caption for all images, just not subs}
    \end{minipage}

\end{solution}
\endmdframed
%\end{adjustwidth}

\ref{fig:solution-left2}

\ref{fig:solution-right2}



\end{document}

Side note regarding parts of the code that are currently commented out: As you can see from the above MWE (better visible if you comment in showframe), the left margin of the mdframed box is aligned with the left margin:

enter image description here

Since there is a small horizontal white space between the frame and the beginning of the text inside of the box (innerleftmargin, default = 10 pt according to the manual) the word "Soution" is not aligned with the word "Exercise" any more. If you prefer thouse worde to be aligned and want the frame to occupy a little bit of space in the left (and right) margin, you can comment in the changepage package and the corresponding adjustwidth environment. This will allow the mdframed box to use an additional 10 pt of the left and the right margin respectively leading to the following alignment:

enter image description here

1
  • 1
    Thanks for the above clarification and accepted solution, @leandriis. This is exactly what I am looking for. Also, I love the added touch of the margin issues with solution boxes. I actually prefer the original method of keeping the box inline with the section numbers; however, I can easily see that others might prefer the adjustment.
    – user641699
    Sep 16, 2019 at 19:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .