Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
2  
removing the spaces before the comments in \StrBefore{#1}{.}[\firstcaption] % and \StrBehind{#1}{.}[\secondcaption] % should fix the problem. –  Guido Aug 3 '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 4 '12 at 18:33
add comment

1 Answer 1

up vote 1 down vote accepted

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
share|improve this answer
    
Removing the spaces does the trick. The additional condition to check for a period is also a great suggestion. –  honeytoast Aug 6 '12 at 14:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.