15

How to bold automatically the first sentence of the float caption and second sentence is normal?

\begin{figure}[t]
\centering
\caption{First sentence. Second sentence.}
\end{figure}

Output should be:

Figure 1. First sentence. Second sentence.

Torbjørn T.
  • 206,688
jeecabz
  • 1,448

3 Answers3

14

A non-automatic way would be to define:

\newcommand{\bcaption}[2]{\caption{\textbf{#1} #2}}

and using the 'bf' option to the package caption.

Here a working example:

\documentclass{article}
\usepackage[bf]{caption}
\newcommand{\bcaption}[2]{\caption{\textbf{#1} #2}}
\begin{document}
\begin{figure}[t]
\centering
 \bcaption{First sentence.}{Second sentence.}
\end{figure}
\end{document}
egreg
  • 1,121,712
bersanri
  • 970
  • 5
    Nice advantage: You can use this to have the bold first sentence as the short LoF/LoT entry. Just use \newcommand{\bcaption}[2]{\caption[#1]{\textbf{#1} #2}} – NauC Oct 17 '13 at 15:05
7

You can use the approach described in Customize text format of captions by parsing content of \@caption, namelly

\documentclass{article}
\usepackage{xstring}
\usepackage{etoolbox}
\usepackage{caption}

\captionsetup{labelfont=bf,tableposition=top}

\makeatletter
\newcommand\formatlabel[1]{%
    \noexpandarg
    \IfSubStr{#1}{.}{%
      \StrBefore{#1}{.}[\firstcaption]%
      \StrBehind{#1}{.}[\secondcaption]%
      \textbf{\firstcaption.} \secondcaption}{%
      #1}%
      }


\patchcmd{\@caption}{#3}{\formatlabel{#3}}
\makeatother

\begin{document}

\begin{figure}[t]
\centering
test test
\caption{First sentence. Second sentence.}
\end{figure}

\end{document}
Guido
  • 30,740
1

Inspired by @guido's solution, I ended up with the following:

\usepackage{xstring}
\usepackage{etoolbox}
\usepackage{caption}
\newcommand\firstsentencebold[1]{%
    \noexpandarg
    \exploregroups
    \IfSubStr{#1}{. }{%
        \StrBefore{#1}{. }[\firstcaptionsentence]%
        \StrBehind{#1}{. }[\othercaptionsentences]%
        \textbf{\firstcaptionsentence. }\othercaptionsentences%
    }{%
        \textbf{#1.}%
    }%
}
\DeclareCaptionFormat{FirstSentenceBold}{\textbf{#1#2}\firstsentencebold{#3}}
\captionsetup{format=FirstSentenceBold}

It's mostly for my future self.

StSav012
  • 125