標題中加入了簡短描述的圖

標題中加入了簡短描述的圖

我有帶有長標題的大人物。我的標題已格式化(使用標題包)並且看起來不錯,但我想對其進行更多自定義,以便將我的簡短描述添加為藍色和粗體。

目前,圖形標題已添加兩次簡短描述。這似乎是多餘的並且容易出錯。簡短描述也用於圖形列表中,我想確保列表和圖形本身俱有完全相同的簡短描述。

微量元素:

\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

相關內容