4

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}
naphaneal
  • 2,614
telegott
  • 73
  • 3
  • 2
    maybe this answer helps? https://tex.stackexchange.com/questions/269444/referring-to-delimiter-separated-arguments-in-new-commands – naphaneal May 31 '18 at 11:05
  • 1
    How long are your captions at present? In general typographic experience, captions should always be short and snappy, as their purpose is to introduce and state the subject of the figure or table at hand. If your captions are longer than one full sentence, you may be abusing the caption machinery. – Mico May 31 '18 at 11:17
  • @Mico I think you are overreaching a little. There are many legitimate reasons why a caption might extend beyond a sentence – Steven B. Segletes May 31 '18 at 12:15
  • I use the caption to explain the technicalities of a graph, what's shown and how it is depicted, there is no interpretation or anything like that there, I wouldn't know where else a description of the figure would make sense – telegott May 31 '18 at 12:28
  • @StevenB.Segletes - If I was overreaching, it was only a mild case. :-) For sure, I've come across quite a few working papers with multi-line multi-sentence captions. (My own field is economics; thankfully, long captions are sort of frowned upon among economists.) For sure, I can't think of a single instance where the readability and intelligibility of the figure or table material wouldn't have improved significantly if a long caption had been broken up into (a) a short, pithy caption that introduces the material and (b) a separate legend that takes care of explaining all details. – Mico May 31 '18 at 12:32
  • 1
    @Mico I agree it was at most a mild overreach. Conciseness (or is that concision??) should be valued. As you point out, some styles would prefer explanatory info to go in a legend, rather than a caption. – Steven B. Segletes May 31 '18 at 13:14

2 Answers2

7

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}
  • Thanks all for your efforts and solution proposals, I tried this one and it works perfect! – telegott May 31 '18 at 12:29
  • @telegott If it truly satisfies your requirement best of the answers given, you should click the arrow to the left of this answer to "accept it", which tells other readers which answer solves best the question you asked. Once you have enough reputation (15 if I recall), you may also click the uparrow to the left of any question or answer (other than your own) to indicate that you found that question or answer to be useful. – Steven B. Segletes May 31 '18 at 13:12
  • I tried to use this with \renewcommand{\caption}, but it doesn't seem to work. – Amin Ya Aug 01 '21 at 00:08
  • @Amin Well, I don't know what you tried, but with my second approach, this seems to work: \let\svcaption\caption \renewcommand\caption[1]{\svcaption[\getfirst#1\relax.]{#1}} \def\getfirst#1.#2\relax{#1} – Steven B. Segletes Aug 01 '21 at 00:21
  • Thank you so much! This is a game changer for me! :) – Johannes Lemonde Jan 02 '22 at 09:39
0

I tried with xparse partially successful (if somebody could correct my attempt). The solution linked by @naphanael is far better and simpler… and it works :)

\documentclass{article}
\usepackage{xparse}

\newcommand{\ajout}[1]{%
    \ifx\court\@empty\xdef\court{#1}\fi
    \xdef\long{\long #1.}
}
\NewDocumentCommand{\SplitFirstDot}{>{\SplitList{.}}m}{%
    \let\long\@empty
    \let\court\@empty
    \ProcessList{#1}{\ajout}
    \caption[\court]{\long}
}
%\def\getfirst#1.#2\relax{{#1}}
%\def\getsecond#1.#2\relax{{#2}}

\newcommand{\includetikz}[4]{
    \begin{figure}[!h]
        \centering 
        #1 #2%
        %\caption[\getfirst#3\relax]{\getsecond#3\relax}
        \SplitFirstDot{#3}
        \label{#4}
    \end{figure}
}

\begin{document}

    Essai

    \includetikz{folder}{file}{Caption.1.long}{label}

    \listoffigures

\end{document}
naphaneal
  • 2,614
NBur
  • 4,326
  • 10
  • 27