5

I would like to print the figure caption with a "tinted box" (a box with a background colour). How to achieve this?

My MWE is :

\documentclass{book}
\usepackage{lipsum,graphics}
\usepackage{caption}
\DeclareCaptionFormat{overlay}{\gdef\capoverlay{#1#2#3\par}}
\DeclareCaptionStyle{overlay}{format=overlay}

\begin{document}

\lipsum[1]
  \begin{figure*}[!h]
  \includegraphics{example-image-a}
  \caption{How many factors are there in a set of 12?}
  \end{figure*}    
 \lipsum[2]      
\end{document}
5

Here's an option using the packages

The caption (with multi-line support) width will automatically adjust to the figure width.

enter image description here

The code:

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{floatrow}
\usepackage{caption}
\usepackage{graphicx}

\definecolor{captionbgcolor}{RGB}{103,143,150}

\DeclareCaptionFormat{tcbcaption}{%
  \begin{tcolorbox}[
    colback=captionbgcolor,
    arc=0pt,
    outer arc=0pt,
    boxrule=0pt,
    colupper=white,
    fontupper=\large\sffamily,
    boxsep=0pt
  ]
  #1#2#3
  \end{tcolorbox}%
}
\captionsetup{format=tcbcaption}

\begin{document}

\begin{figure}
\ffigbox[\FBwidth]
  {\includegraphics[width=.8\linewidth]{eggs}}
  {\caption{how many factors are there in a set of 12?}}
\end{figure}

\end{document}

Update

According to comments, all images should have constant width, so simply add to the preamble

\setkeys{Gin}{width=0.7\textwidth}

changing 0.7\textwidth to the desired value. A complete example:

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{floatrow}
\usepackage{caption}
\usepackage{graphicx}
\usepackage{showframe}% just for the example to have a visual guideline

\definecolor{captionbgcolor}{RGB}{103,143,150}

\DeclareCaptionFormat{tcbcaption}{%
  \begin{tcolorbox}[
    nobeforeafter,
    colback=captionbgcolor,
    arc=0pt,
    outer arc=0pt,
    boxrule=0pt,
    colupper=white,
    fontupper=\large\sffamily,
    boxsep=0pt
  ]
  #1#2#3
  \end{tcolorbox}%
}
\captionsetup{format=tcbcaption}
\setkeys{Gin}{width=0.7\textwidth}

\begin{document}

\begin{figure}
\ffigbox[\FBwidth]
  {\includegraphics{eggs}}
  {\caption{how many factors are there in a set of 12?}}
\end{figure}

\begin{figure}
\ffigbox[\FBwidth]
  {\includegraphics{mushrooms}}
  {\caption{how many factors are there in a set of 12?}}
\end{figure}

\end{document}

enter image description here

And, of course, since the power of tcolorbox is used, you can have fancy backgrounds in your captions (I also suppressed the head for the captions using labelformat=empty):

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{floatrow}
\usepackage{caption}
\usepackage{graphicx}
\usepackage{showframe}% just for the example

\definecolor{captionbgcolor}{RGB}{103,143,150}

\DeclareCaptionFormat{tcbcaption}{%
  \begin{tcolorbox}[
    nobeforeafter,
    enhanced,
    colback=white,
    arc=0pt,
    outer arc=0pt,
    boxrule=0pt,
    colupper=white,
    fontupper=\large\sffamily,
    boxsep=0pt,
    watermark opacity=1,
    watermark graphics=lichtspiel.jpg,
    watermark overzoom=1.0
  ]
  #1#2#3
  \end{tcolorbox}%
}
\captionsetup{format=tcbcaption,labelformat=empty}
\setkeys{Gin}{width=0.7\textwidth}

\begin{document}

\begin{figure}
\ffigbox[\FBwidth]
  {\includegraphics{eggs}}
  {\caption{how many factors are there in a set of 12?}}
\end{figure}

\begin{figure}
\ffigbox[\FBwidth]
  {\includegraphics{mushrooms}}
  {\caption{how many factors are there in a set of 12?}}
\end{figure}

\end{document}

enter image description here

  • Without manual type width=.8\linewidth how to do automatically? Because i have more than 100 figures in my document. – Balaji Feb 5 '15 at 0:21
  • @Balaji Will all your figures use the same width? – Gonzalo Medina Feb 5 '15 at 0:22
  • @Balaji: Gonzalo's width setting is specific to the figure, and \FBwidth will change accordingly. Please tell us how you include your images. Give us some specific examples and some general rules that you use. Currently the problem is just getting more confusing... – Werner Feb 5 '15 at 0:24
  • @Balaji please see my updated answer providing an automatic width for all figures; the caption will adjust automatically too to this width. – Gonzalo Medina Feb 5 '15 at 0:29
  • @Gonzalo: It's possible to use renewcommand in tcolorbox. Because i don't want Figure~\thefigure in the caption. – Balaji Feb 5 '15 at 0:50
4

The following assumes you won't have multi-line captions:

enter image description here

\documentclass{article}
\usepackage{lipsum,graphicx,xcolor,caption}

\newcommand{\captionbackgroundcolor}[1]{\colorlet{cpbgcol}{#1}}
\DeclareCaptionFont{white}{\color{white}}
\captionbackgroundcolor{black!50}
\DeclareCaptionFormat{overlay}{\colorbox{cpbgcol}{#1#2#3}}
\captionsetup{format=overlay,font=white}

\begin{document}

\lipsum[1]

\begin{figure}[ht]
  \includegraphics[width=\linewidth]{example-image}
  \caption{How many factors are there in a set of 12?}
\end{figure}

\lipsum[2]
\end{document}

Adjustments to the background colour may be made using \captionbackgroundcolor{<colour>}.


For multi-line support, you can rely on varwidth:

enter image description here

\documentclass{article}
\usepackage{lipsum,graphicx,xcolor,caption,varwidth}

\newcommand{\captionbackgroundcolor}[1]{\colorlet{cpbgcol}{#1}}
\DeclareCaptionFont{white}{\color{white}}
\captionbackgroundcolor{black!50}
\DeclareCaptionFormat{overlay}{\colorbox{cpbgcol}{\centering%
  \begin{varwidth}{\dimexpr\captionwidth-2\fboxsep-2\fboxrule}
    #1#2#3
  \end{varwidth}}}
\captionsetup{format=overlay,font=white}

\begin{document}

\lipsum[1]

\begin{figure}[ht]
  \centering
  \includegraphics[width=0.7\textwidth]{example-image}
  \setlength{\captionwidth}{0.7\textwidth}%
  \caption{How many factors are there in a set of 12?
           How many factors are there in a set of 12?
           How many factors are there in a set of 12?}
\end{figure}

\lipsum[2]
\end{document}

There is no relationship between the width key of \includegraphics and the width key of \captionsetup. Even if they were tied, some people don't use \includegraphics[width=..] but rather \includegraphics[height=..] or scale to adjust the inherent width of an included image. As such, I'd suggest manually setting \captionwidth to match your image output.


Of course, the above statement holds for the packages or interfaces used. floatrow is an alternative that captures the requirements in a single package - both the caption and the width-of-image requests.

  • The caption width should match with figure width. How do control this and multiline caption is not breaking and how do break automatically. – Balaji Feb 4 '15 at 20:32
  • @Balaji: See the updated answer. – Werner Feb 4 '15 at 22:10
  • Without manual type width=0.7\textwidth how to do automatically? Because i have more than 100 figures in my document. – Balaji Feb 5 '15 at 0:08
  • @Balaji: Do you have all the figures at some fixed width? Do they vary in terms of their width? Should all figures adhere to this requirement? – Werner Feb 5 '15 at 0:17
  • All the figures are not fixed width. It's different width. Yes that's my requirements and how to fix it? – Balaji Feb 5 '15 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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