11

Is there any automatic way to use the first sentence of a caption as the short caption to be displayed in table of contents? Precisely I want to write:
\caption{First Sentence. Second Sentence.} to be compiled as: \caption[First Sentence]{First Sentence. Second Sentence.} After the first sentence I don't have necessary just one sentence.

  • Is enough automatic some like \Caption{First sentence}{Second sentence} ? – Fran May 09 '15 at 11:33
  • Unfortunately no. I have written my long report and now I find out that I should rewrite the first sentence of each caption for list of figures. I need a way to not change every of the caption commands. – Mehran Ebrahimian May 09 '15 at 11:40
  • It would be possible using TeX's general parameter parsing mechanism. But it would be easier to suggest something appropriate if you provided us with a MWE to work on. I take it from your comment that you need a quick hack rather than a general mechanism, and that you can't simply use your editor to make some global changes to your captions? – Thruston May 09 '15 at 11:48
  • Yes its true. Also, I think its too commonplace for scientific texts that the first sentence of captions is also the title of figure that would be shown in table of figures and a new caption code with this feature may save time in writing. – Mehran Ebrahimian May 09 '15 at 11:54

2 Answers2

8

As a quick-and-dirty hack, try this in your pre-amble:

\let\oldcaption\caption
\def\splitter #1. #2@@{\oldcaption[#1]{#1. #2}}
\def\caption#1{\splitter #1@@}

but beware that I'm assuming you have a very simple environment.

Thruston
  • 42,268
  • Thanks; it works by adding just before \begin{document}, after adding babel package. I think your code is also a quick answer to this question: http://tex.stackexchange.com/questions/65680/automatically-bold-first-sentence-of-a-floats-caption?rq=1 by changing your second line with: \def\splitter #1. #2@@{\oldcaption[#1]{\textbf{#1}. #2}} – Mehran Ebrahimian May 09 '15 at 12:11
  • Does not work with subcaption: ! Package caption Error: The 'subcaption' package does not work correctly in compatibility mode. Is there a more reliable option? – user877329 Jul 01 '15 at 11:10
  • @user877329, as I said, I was assuming a very simple environment... – Thruston Jul 01 '15 at 19:42
  • 1
    This is great thank you. For anyone who wants to use this in a document that has some one sentence captions already be sure to add a space after the full-stop in order for the code to function correctly across the document. – Docconcoct Dec 07 '15 at 03:26
1

If you wrote all your labels directly after caption as I do:

sed 's/\(\\caption\){\(\\label{[^}]\+}\)\([^.]*\)\.\(.\+\)/\1[\3]{\2\3. \4/' report.tex

This works in some cases. It uses the GNU sed extension \+ otherwise it matches to much.