彩色框内的图题

彩色框内的图题

我想打印figure caption一个“有色框”(带有背景颜色的框)。如何实现?

我的 MWE 是:

\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}

答案1

这是一个使用包的选项

标题(支持多行)宽度将自动调整为图形宽度。

在此处输入图片描述

代码:

\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}

更新

根据评论,所有图像应具有恒定的宽度,因此只需添加到序言中

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

更改0.7\textwidth为所需值。完整示例:

\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}

在此处输入图片描述

当然,由于tcolorbox使用了 的功能,您可以在字幕中添加精美的背景(我还使用 隐藏了字幕的头部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}

在此处输入图片描述

答案2

以下假设您没有多行字幕:

在此处输入图片描述

\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}

可以使用 来调整背景颜色\captionbackgroundcolor{<colour>}


对于多行支持,你可以依赖varwidth

在此处输入图片描述

\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}

width的键\includegraphicswidth的键之间没有任何关系\captionsetup。即使它们相关联,有些人也不会使用 ,\includegraphics[width=..]而是使用\includegraphics[height=..]scale来调整所包含图像的固有宽度。因此,我建议手动设置\captionwidth以匹配您的图像输出。


当然,上述说法对于所使用的包或接口也适用。floatrow是一种在单个包中捕获需求的替代方案 - 包括标题和图像宽度请求。

相关内容