[Tex/LaTex] How to automatically create a short caption for the list of figures

captionstable of contents

I'm writing my thesis using figures drawn with tikzDevice, which I include with the following function:

\newcommand{\includetikz}[4]{

\begin{figure}[!h]

\centering  
\begin{adjustbox}{width=\textwidth}
\input{#1#2.tikz}%
\end{adjustbox} 
\caption{#3}
\label{#4}
\end{figure}


}

I now want to get short versions for the list of figures, which would simply be the first sentence of the whole caption.
I could rewrite the function using an additional argument in the function above:

\newcommand\slcaption[2]{\caption[#1]{#1. #2}}

However this would be a lot of adjustment given the number of figures. Is there a simple option to just split the argument 3 at the first dot inside the function?

I imagine something like (in pseudocode):

\caption[ str_split(#3, '.')[1] ]{#3}

Best Answer

Here I use listofitems to parse the caption at the first occurrence of a dot . and provide that as the optional argument to \caption.

\documentclass{article}
\usepackage{listofitems}
\newcommand\slcaption[1]{\setsepchar{.}\readlist*\pdots{#1}\caption[{\pdots[1].}]{#1}}
\begin{document}
\listoffigures
\begin{figure}[ht]
\slcaption{This is a test.}
\end{figure}
\begin{figure}[ht]
\slcaption{Sentence one. This is a test.}
\end{figure}
\begin{figure}[ht]
\slcaption{Let's try again. Sentence two. This is a test.}
\end{figure}
\end{document}

enter image description here

Without any packages:

\documentclass{article}
\newcommand\slcaption[1]{\caption[\getfirst#1\relax.]{#1}}
\def\getfirst#1.#2\relax{#1}
\begin{document}
\listoffigures
\begin{figure}[ht]
\slcaption{This is a test.}
\end{figure}
\begin{figure}[ht]
\slcaption{Sentence one. This is a test.}
\end{figure}
\begin{figure}[ht]
\slcaption{Let's try again. Sentence two. This is a test.}
\end{figure}
\end{document}
Related Question