在下面给出的代码中摘录自Harvey Sheppards 的模板,我想知道如何让标题标签文本与彩色框中的文本对齐?这是我试图获得的效果:
以下是代码:
\documentclass{book}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{tikz}
\DeclareCaptionLabelFormat{yLabel}{%
\tikz{\node[anchor=west, inner sep=2mm, fill=blue, font=\bfseries, text=white]{#1 #2};}
}
\DeclareCaptionStyle{yReportCaptionStyle}{labelsep=none, labelformat=yLabel, singlelinecheck=false}
\captionsetup*[figure]{style=yReportCaptionStyle, justification=RaggedRight, position=bottom}
\begin{document}
\begin{figure}
\includegraphics{example-image-a}
\caption{This is an example figure}
\end{figure}
\end{document}
答案1
您可以强制 tikz 图片的基线为文本节点的基线:
\documentclass{book}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{tikz}
\DeclareCaptionLabelFormat{yLabel}{%
\tikz[baseline=(T.base)]{\node (T)[anchor=west, inner sep=2mm, fill=blue, font=\bfseries, text=white]{#1 #2};}
}
\DeclareCaptionStyle{yReportCaptionStyle}{labelsep=none, labelformat=yLabel, singlelinecheck=false}
\captionsetup*[figure]{style=yReportCaptionStyle, justification=RaggedRight, position=bottom}
\begin{document}
\begin{figure}
\includegraphics{example-image-a}
\caption{This is an example figure}
\end{figure}
\end{document}
答案2
colorbox
既然一个简单的工具就能轻松完成工作,为什么还要使用像 TikZ 这样复杂的工具呢?
\documentclass[svgnames]{book}
\usepackage{graphicx}
\usepackage{xcolor, caption}
\DeclareCaptionLabelFormat{bgLabel}{%
\colorbox{RoyalBlue}{\color{white}\bfseries#1 #2}}
\DeclareCaptionStyle{yReportCaptionStyle}{labelformat=bgLabel, labelsep=space, justification=RaggedRight, position =bottom, singlelinecheck=false}
\captionsetup*[figure]{style=yReportCaptionStyle, justification=RaggedRight, position=bottom}
\begin{document}
\begin{figure}%
\caption{This is an example figure.}
\end{figure}
\end{document}
答案3
您可以使用该\raisebox
命令来存档它。
\documentclass{book}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{tikz}
\DeclareCaptionLabelFormat{yLabel}{%
\tikz{\node[anchor=west, inner sep=2mm, fill=blue, font=\bfseries, text=white]{#1 #2};}
}
\DeclareCaptionStyle{yReportCaptionStyle}{labelsep=none, labelformat=yLabel, singlelinecheck=false}
\captionsetup*[figure]{style=yReportCaptionStyle, justification=RaggedRight, position=bottom}
\begin{document}
\begin{figure}
\includegraphics{example-image-a}
\caption{\raisebox{2.6mm}{This is an example figure}}
\end{figure}
\end{document}