キャプションに短い説明が追加された図

キャプションに短い説明が追加された図

長いキャプションが付いた大きな図があります。キャプションはフォーマットされており (キャプション パッケージを使用)、見た目も問題ありませんが、もう少しカスタマイズして、短い説明を青と太字で追加したいと思っています。

現在、図のキャプションには短い説明が 2 回追加されています。これは冗長で、エラーが発生しやすいようです。短い説明は図のリストでも使用されるため、リストと図自体の両方にまったく同じ短い説明があることを確認したいと思います。

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

\caption次のように再定義できます。注: 現在の状態では、これは に対して適切に機能しません\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}

結果

この定義をプリアンブルに直接配置すると、機能しません (理由はわかりません)。ただし、 を使用する回避策があります\AtBeginDocument。次のように、次のコードをプリアンブルに配置すると、 の直後に実行されます\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

関連情報