0

I have big figures with long captions. My captions are formatted (using the captions package) and look fine, but I would like to customize it a bit more to also have my short description added in blue and bold.

Currently a figure caption has the short description added twice. This seems redundant and prone to errors. The short description also gets used in the list of figures and I want to make sure to have exactly the same short description in both the list and figure itself.

MWE:

\documentclass{book}
\usepackage{caption}
\usepackage{graphicx}
\usepackage{xcolor}
\captionsetup[figure]{font={small, singlespacing, sf},labelfont={color=blue, bf, sf}, indention=.5cm, labelsep=quad}

\begin{document}
\listoffigures
\begin{figure}
\includegraphics[width=0.8\textwidth]{black.png}
\centering
\caption[Short description]{\textbf{\color{blue}Short description} Very long description spanning several lines.}
\label{label}
\end{figure}
\end{document}

1 Answer 1

2

You could redefine \caption in the following way. Note: In the current state this will not work properly for \caption*.

\documentclass{book}
\usepackage{caption}
\usepackage{graphicx}
\usepackage{xcolor}
\captionsetup[figure]{font={small, singlespacing, sf},labelfont={color=blue, bf, sf}, indention=.5cm, labelsep=quad}

\begin{document}

\makeatletter
    \let\oldcaption\caption
    \def\caption{\@ifnextchar[{\caption@with}{\caption@without}}
    \def\caption@with[#1]#2{\oldcaption[#1]{\textcolor{blue}{\bfseries#1} #2}}
    \def\caption@without#1{\oldcaption{#1}}
\makeatother

\listoffigures
\begin{figure}
    \includegraphics[width=0.8\textwidth]{example-image-duck}
    \centering
    \caption[Short description]{Very long description spanning several lines.}
    \label{label}
\end{figure}
\end{document}

Result

If you directly place this definition into your preamble, this won't work (and I don't know why). But there is a workaround making use of \AtBeginDocument. Like this you can put the following into your preamble and it will be executed directly after \begin{document}.

\makeatletter
\AtBeginDocument{
    \let\oldcaption\caption
    \def\caption{\@ifnextchar[{\caption@with}{\caption@without}}
    \def\caption@with[#1]#2{\oldcaption[#1]{\textcolor{blue}{\bfseries#1} #2}}
    \def\caption@without#1{\oldcaption{#1}}
}
\makeatother
3
  • 1
    Can I somehow modify it so that it also works when the new definition is before the \begin{document}? I am using a .sty file for all the formatting and when I put it in there, it doesn't work.
    – ask_ask
    Mar 15 at 10:26
  • @ask_ask Please see my updated answer. Mar 15 at 11:20
  • Thank you. I could not figure out myself how to modify it but your edit works for me and I will use it. I don't know either why the \AtBeginDocument is needed but as long as it works, I am happy with it for now.
    – ask_ask
    Mar 15 at 12:32

You must log in to answer this question.

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