Automatic formatting of list of figures based on separator inside \caption

I am currently using caption/subcaption packages to have captions and a list of figures for my thesis.

In my captions, I put a title, but also some legend information as shown by the MWE below.

\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage{subcaption}
\usepackage{graphicx}

% For the sake of example
\DeclareRobustCommand{\Curve}[1]{(\tikz[baseline=-0.6ex,inner sep=0pt] \draw[line width=1pt,#1](0,0) -- (4mm,0);)}

\begin{document}

% Automatic title in list of figures
\begin{figure}
\centering
\includegraphics[width=2cm]{example-image-a}
\caption{My nice figure with legend. Legend~\Curve{red}}
\end{figure}

% Manual title in list of figures
\begin{figure}
\centering
\includegraphics[width=2cm]{example-image-a}
\caption[My nice figure without legend]{My nice figure without legend. Legend~\Curve{red}}
\end{figure}

\listoffigures

\end{document}


I know it is possible to manually define the caption provided in the list of figures with the optional argument of \caption[]{} but is there (it is latex so the question seems dumb) a way to redefine \caption so that Everything before a specific character is used as the optional argument.

To be crystal clear:

I would like the ouptut of

\caption[blabla]{blabla. blibli}


and

\caption{blabla. blibli}


to look the same in the list of figures.

• What about \caption{blabla \ignoretherest blibli}? – Werner Sep 10 '18 at 16:59
• @Werner Well, As I already have a lot of figures in my thesis, I'd prefer not to have to modify everything manually, hence the question... :) . However I think such a solution could still be helpful. – BambOo Sep 10 '18 at 17:03
• @Werner To me it looks like the option lists in pgf-tikz where each option is separated by a comma. But I have no idea how to reproduce such a behaviour. – BambOo Sep 10 '18 at 17:05
• caption doesn't like very much that the meaning of \caption is changed. – egreg Sep 10 '18 at 17:21
• @egreg I am not very fond of modifying manually the contents of all of my captions and I surely understand your point. I am open to using another macro as searching even in a big document for the \caption{ string is not that difficult – BambOo Sep 10 '18 at 17:26

1 Answer

A bit of juggling:

\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage{xparse}

% For the sake of example
\DeclareRobustCommand{\Curve}[1]{%
(\tikz[baseline=-0.6ex,inner sep=0pt] \draw[line width=1pt,#1](0,0) -- (4mm,0);)%
}

\ExplSyntaxOn
\NewDocumentCommand{\bamboocaption}{som}
{
\IfBooleanTF{#1}
{
\captioncaption*{#3}
}
{
\IfValueTF{#2}
{
\captioncaption[#2]{#3}
}
{
\seq_set_split:Nnn \l_tmpa_seq { . } { #3 }
\int_compare:nTF { \seq_count:N \l_tmpa_seq < 2 }
{% no period
\captioncaption{#3}
}
{
\captioncaption[\seq_item:Nn \l_tmpa_seq { 1 }]{#3}
}
}
}
}
\ExplSyntaxOff

\AtBeginDocument{%
\let\captioncaption\caption
\let\caption\bamboocaption
}

\begin{document}

\listoffigures

\section{Body}

% Automatic title in list of figures
\begin{figure}[htp]
\centering
\begin{subfigure}{3cm}
\centering
abc
\caption{Something}
\end{subfigure}\qquad
\begin{subfigure}{3cm}
\centering
abc
\caption{Something}
\end{subfigure}

\bigskip

\includegraphics[width=2cm]{example-image-a}
\caption{My nice figure with legend. Legend~\Curve{red}}

\end{figure}

% Manual title in list of figures
\begin{figure}[htp]
\centering
\includegraphics[width=2cm]{example-image-a}
\caption{My nice figure without legend\label{ops}}
\end{figure}

\end{document}


Note that the optional argument to \caption can still be used with the usual meaning. If not present, the caption will be split at the first period; everything before it is passed as the optional argument for the list of figures. If no period is found, nothing changes from the default.

I also added subcaptions in order to ensure they are not changed. Should you want to add legends also for them it's a bit more complicated.

• Really nice, as expected everything is possible ;) Thanks. – BambOo Sep 10 '18 at 17:38
• There is just a slight remaining glitch. If a \caption has no . , no explicit option and posesses a label{} inside, an error shows up – BambOo Sep 10 '18 at 18:00
• @BambOo I added a check for this case. – egreg Sep 10 '18 at 18:06
• Sorry for your trouble, but now I get a weird error Text line contains an invalid character. l.166 ...865726D6F5C3335316C6173746F64796E616D697. This is really the first time i see this. Any ideas ? Without adding you patch everything works fine. – BambOo Sep 10 '18 at 18:25
• @BambOo That's something different, I guess. My example doesn't raise that error. – egreg Sep 10 '18 at 18:57