1

I use tcolorbox to highlight particular topics in my document, and I want to have figures in these boxes as well. However, I cannot find anywhere how to do this. In the manual it is only described how you can fancify you figures with a box around it, but this is not what I need. What I would like to have is that the following works:

(it does not work right now, but it does when you remove the figure environment)

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{blindtext}
\usepackage{tcolorbox}
\usepackage{caption}

\tcbuselibrary{/tcb/library/breakable}

\begin{document}
\blindtext
\begin{tcolorbox}[colback=gray, colframe=blue, breakable, title={Some specific topic to discuss}] 
    This topic is on bla
    
    \blindtext
    
    \begin{figure}[!ht]
        \centering
        \includegraphics[width=0.8\linewidth]{foo}  
        \caption{Real caption}
        \label{fig:foo}
    \end{figure}
    
    \blindtext
    
    \bigskip
    
    As shown in Fig. \ref{fig:foo}, \blindtext
\end{tcolorbox}

\blindtext

\end{document}

But the whole figure environment induces errors, so right now my only possibility is to put the figure outside of the box, which is not what I want...

Here is an artist impression (paint) of what it should look like:

Artist impression of desired result

3
  • Do you want a tcolorbox to float? (4.13 Floating Objects in the manual). Or do you want a float to contain a tcolorbox?
    – Cicada
    Commented Jun 8, 2021 at 11:16
  • I want a tcolorbox to contain text and a float
    – seaver
    Commented Jun 8, 2021 at 11:25
  • @Cicada As an follow-up on my reaction; Maybe I didn't understand it correctly, but I believe that in section 4.13, that what I want is not included, see the image I added.
    – seaver
    Commented Jun 8, 2021 at 11:38

1 Answer 1

1

Like this?

image in tcolorbox

Assuming the tcolorbox is not supposed to float, just include the image and not the float environment. Then capt-of package's \captionof command to assign a caption and step the figure reference number.

MWE

\documentclass{article}
\usepackage{graphicx}
\usepackage{blindtext}
\usepackage{tcolorbox}
\usepackage{capt-of}
\usepackage{lipsum}
\tcbuselibrary{skins}

\tcbuselibrary{/tcb/library/breakable}
\newtcolorbox[blend into=figures]{myfigure}[2][]{float=htb,capture=hbox,
blend before title=dash hang,title={#2},every float=\centering,#1}

\begin{document}
\begin{tcolorbox}[colback=blue!20, colframe=blue, breakable, title={Some specific topic to discuss}] 
    This topic is on bla
\begin{tcolorbox}[
sharp corners,boxrule=.4pt,colback=green!5]
{
\begin{center}
\includegraphics[width=0.2\linewidth]{goldshade}%
\end{center}
\captionof{figure}{Some caption}\label{fig:foo2}}


    As shown in Fig. \ref{fig:foo2}, ...

\end{tcolorbox}
    As just mentioned, Fig. \ref{fig:foo2}, ...
    
\end{tcolorbox}

\begin{tcolorbox}[colback=blue!20, colframe=blue, breakable, title={Some specific topic to discuss}] 
    This topic is on bla
{
\begin{center}
\includegraphics[width=0.2\linewidth]{goldshade}%
\end{center}
\captionof{figure}{Some caption}\label{fig:foo2a}}


    As shown in Fig. \ref{fig:foo2a}, ...

    
\end{tcolorbox}

\end{document}
2
  • Thanks! One small thing, how can I ensure that the caption and the figure stay on the same page?
    – seaver
    Commented Jun 8, 2021 at 14:31
  • 2
    @seaver Try putting image and caption in an unbreakable box, perhaps parbox, minipage (both take widths), or an unbreakable tcolorbox, so they form a unit.
    – Cicada
    Commented Jun 9, 2021 at 7:35

You must log in to answer this question.

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