3

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

3
  • 3
    Welcome to TeX.SX! Please make your code compilable (if possible) to form a complete minimal working example (MWE), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – Dai Bowen
    Commented Nov 25, 2016 at 17:12
  • What does besides the graphics mean? Inside the colorbox? Outside it?
    – Ignasi
    Commented Nov 25, 2016 at 17:30
  • 1
    Does 'besides' mean 'in addition to' or 'to one side of' the graphic?
    – jon
    Commented Nov 25, 2016 at 17:55

1 Answer 1

6

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

4
  • Great answer @Ignasi. How would you reference the figure? I tried putting \label{fig} inside the environment, inside the title, and below \tcblower without success. If it's worth a new question, I'll make one. Thanks.
    – PatrickT
    Commented Sep 9, 2021 at 6:31
  • I found a workaround, but it's not based on tcolorbox, so there's probably better. Preamble: \makeatletter \newcommand\addcaption[1]{\def\@captype{#1}\caption} \makeatother Inside figure: \addcaption{figure}{This is a title to the figure}\label{fig1}
    – PatrickT
    Commented Sep 9, 2021 at 6:54
  • 1
    @PatrickT After including label=whateverlabelyoulike into the optional parameter, you can use \red{whateverlabelyoulike. You can see examples in section 5 of tcolorbox documentation
    – Ignasi
    Commented Sep 9, 2021 at 7:07
  • Got it thanks Ignasi!
    – PatrickT
    Commented Sep 9, 2021 at 7:10

You must log in to answer this question.

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