1

I need the first sentence of my captions to appear on its own line in a \large, bold font. I also need to same document to easily produce standard captions (all same font, no line breaks). I have implemented a class file that uses an option to directly modify \@caption and add the necessary formatting; however, my code isn't quite working. This example correctly places the first sentence of the caption on its own line in the correct font, but the text is not left-aligned.

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

\captionsetup{labelfont=bf,labelsep=newline,tableposition=top}

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

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

\begin{document}

\begin{table}
\centering
\caption{A Simple table. It has a caption with multiple sentences. It has a caption with multiple sentences. It has a caption with multiple sentences.}
\begin{tabular}{ l c r }
  1 & 2 & 3 \\
  4 & 5 & 6 \\
  7 & 8 & 9 \\
\end{tabular}
\end{table}

\end{document}
David Carlisle
  • 757,742
  • 2
    removing the spaces before the comments in \StrBefore{#1}{.}[\firstcaption] % and \StrBehind{#1}{.}[\secondcaption] % should fix the problem. – Guido Aug 03 '12 at 22:28
  • @Guido you could turn you comment into an answer (maybe showing the corrected code and an image of the result?) – Gonzalo Medina Aug 04 '12 at 18:33

1 Answers1

1

The indentation is given by the two spaces before the comments in

\StrBefore{#1}{.}[\firstcaption] % 

and

\StrBehind{#1}{.}[\secondcaption] % 

Removing them fixes the problem. Also you can add a condition to ensure there is period . in the caption.

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

\patchcmd{\@caption}{#3}{\formatlabel{#3}}
\makeatother
Guido
  • 30,740
  • Removing the spaces does the trick. The additional condition to check for a period is also a great suggestion. – honeytoast Aug 06 '12 at 14:37