1

In the code given below that is extracted from Harvey Sheppards's template, I wanted to know how to get the caption label text to line up with the text in the colored box? This is the effect that I am trying to get:

enter image description here

Here is the code:

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

You can force the baseline of the tikz picture to be the baseline of the text node:

enter image description here

\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}
| improve this answer | |
4

Why use a hammer-sledge like TikZ when a simple colorbox easily does the job?

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

enter image description here

| improve this answer | |
1

You could use the \raisebox command to archive this.

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

enter image description here

| improve this answer | |

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.