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}
6
  • 2
    maybe this answer helps? tex.stackexchange.com/questions/269444/…
    – naphaneal
    May 31, 2018 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, 2018 at 11:17
  • @Mico I think you are overreaching a little. There are many legitimate reasons why a caption might extend beyond a sentence May 31, 2018 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, 2018 at 12:28
  • 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. May 31, 2018 at 13:14

2 Answers 2

6

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}
5
  • Thanks all for your efforts and solution proposals, I tried this one and it works perfect!
    – telegott
    May 31, 2018 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. May 31, 2018 at 13:12
  • I tried to use this with \renewcommand{\caption}, but it doesn't seem to work.
    – Amin
    Aug 1, 2021 at 0: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} Aug 1, 2021 at 0:21
  • Thank you so much! This is a game changer for me! :) Jan 2 at 9: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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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