2

This question already has an answer here:

Hei,

I am trying to add a textbox to my document. The textbox should contain both text and one or more figures, and should be floating itself. It works, as long as I do not include the caption package. However, as soon as I use the caption package, the caption of the figure is moved outside the box. Any ideas? Bug in package caption? Something I missed? Or a good alternative for defining floating textboxes with figures?

Thanks, Theo

MWE:

\documentclass{article}
\usepackage{lipsum,float}
\usepackage[framemethod=TikZ]{mdframed}

\mdtheorem{boxtest}{Box}[section]
\newfloat{floatbox}{tb}{abox}[section]
\usepackage{caption}


\begin{document}

\section{First}
Figure \ref{fig:test} is in Box \ref{box:test}.

\begin{floatbox}[b!]
    \begin{boxtest}[Testbox]
        \label{box:test}    

        \begin{figure}[H]
            \centering
            \includegraphics[width=5cm]{example-image-a}
            \caption{Highly precise description of the example image: it is an A.}
            \label{fig:test}
        \end{figure}

        \lipsum[1]
    \end{boxtest}
\end{floatbox}
\lipsum[2]

\end{document}

marked as duplicate by user121799, Stefan Pinnow, TeXnician, Sebastiano, Phelype Oleinik May 17 '18 at 13:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Have you seen this? tex.stackexchange.com/questions/248370/… it looks exactly like what you're asking for. – G. Bay May 16 '18 at 23:14
  • No, didnt see it and also did not answer my question. – Theo May 17 '18 at 1:04
  • 1
    Why did it not answer your question? Explain it please! – Mensch May 17 '18 at 9:42
  • Read the questions, please! Both the question and the answer are different. I want to put both text and a figure with a caption into a box with a counter. That is different from framing a figure. I needed the \restylefloat, which was not proposed in the other answer. It may have been possible to extend the other answer to my problem, but I honestly dont know how. If you insist on calling this a duplicate, I dont care. – Theo Jun 1 '18 at 9:28
2

MWE

\documentclass{article}
\usepackage{lipsum,float}
\usepackage[framemethod=TikZ]{mdframed}

\mdtheorem{boxtest}{Box}[section]
\newfloat{floatbox}{tb}{abox}[section]
\restylefloat*{floatbox}% <-----------------
\usepackage{caption}

\begin{document}

    \section{First}
    Figure \ref{fig:test} is in Box \ref{box:test}.

    \begin{floatbox}[b!]
        \begin{boxtest}[Testbox]
            \label{box:test}    

            \begin{figure}[H]
                \centering
                \includegraphics[width=5cm]{example-image-a}
                \caption{Highly precise description of the example image: it is an A.\label{fig:test}}
            \end{figure}

            \lipsum[1]
        \end{boxtest}
    \end{floatbox}
    \lipsum[2]

\end{document}

You'll need to use \restylefloat*{floatbox} to prevent it from taking over the \caption command within the figure environment, i.e., so that \caption in figure still remains a figure caption instead of becoming a floatbox caption.


Sample output

enter image description here

  • Fantastic! Thanks for the fast reply. But I must admit that I do not quite understand the technicalities of this. Anyway, works like a charm – Theo May 17 '18 at 0:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.