[Tex/LaTex] tcolorbox and figures – adding description

graphicstcolorbox

I want to style my figures and I found a way to do so with tcolorbox.

\newtcolorbox[blend into=figures]{myfigure}[2][]{float=htb,capture=hbox,blend before title=colon hang,title={#2},every float=\centering,#1,sharp corners,colback=white,colbacktitle=blue!30!black}

\begin{myfigure}{Title}
\includegraphics[width=0.6\textwidth]{Graphic} 
\end{myfigure}

Is there a possibility to add some text besides the graphic?


Thank you for your answers.

So here's the MWE showing one standard figure with a subtext and a tcolorbox-figure. What I want to do is adding some text inside the tcolorbox, too. This text should be under (maybe the best way) or on one side the graphic – but inside the colorbox.

\documentclass{article}
\usepackage{graphicx}
\usepackage{ngerman}
\usepackage[theorems,skins,breakable]{tcolorbox}

\newtcolorbox[blend into=figures]{myfigure}[2][]{float=htb,capture=hbox, blend before title=colon hang,title={#2},every float=\centering,#1,sharp corners,colback=white,colbacktitle=blue!30!black}

\begin{document}

\begin{figure}[h]
\centering \includegraphics[width=0.6\textwidth]{Graphic} 
\caption{Title}
\footnotesize{Subtext Line 1 \\ Subtext Line 2}
\end{figure}

\begin{myfigure}{Title}
\includegraphics[width=0.6\textwidth]{Graphic} 
\end{myfigure}

\end{document}

enter image description here

Best Answer

There's no problem adding any text to a myfigure contents. So OP could use something like:

\begin{myfigure}{Title}
\centering \includegraphics[width=0.6\textwidth]{Graphic} 

\footnotesize{Subtext Line 1 \\ Subtext Line 2}
\end{myfigure}

but the result is not as desired because OP's myfigure contains option capture=hbox which means:

This is the default <mode> for \tcbox. The content cannot have a lower part and cannot be broken. The colored box is sized according to the dimensions of the content. A shortcut to set this mode is /tcb/hbox.

We have two solutions, use capture=hbox and include and manually format image and contents or change to capture=minipage (the default mode) and let tcolorbox do the work for us. With the second solution,

  • it's still possible (although manually) to adjust box width: width=...
  • it's possible to use the lower part of a tcolorbox for comments
  • option sidebyside moves comments from lower part to figure side

Following code shows a possible declaration for myfigure and how particular options change the defined behaviour.

\documentclass[a4paper]{article}
\usepackage[vmargin={2cm,2cm}]{geometry}
\usepackage[most]{tcolorbox}

\newtcolorbox[blend into=figures]{myfigure}[2][]{%
    float=htb,
    blend before title=colon hang,
    sharp corners,
    colback=white,
    colbacktitle=blue!30!black,
    title={#2},
    every float=\centering,
    halign=flush center,
    halign lower=flush center,
    lower separated=false,
    #1}

\begin{document}

\begin{myfigure}{A tcolorbox figure}
\includegraphics[height=4cm]{example-image}
\tcblower
This is a comment to the figure placed inside the colored box
\end{myfigure}

\begin{myfigure}[sidebyside]{A tcolorbox figure}
\includegraphics[height=4cm]{example-image}
\tcblower
This is a comment to the figure placed inside the colored box
\end{myfigure}

\begin{myfigure}[width=8cm]{A tcolorbox figure}
\includegraphics[height=4cm]{example-image}
\tcblower
This is a comment to the figure placed inside the colored box
\end{myfigure}
\end{document}

enter image description here

Related Question