[Tex/LaTex] Switch between long and short caption in List of Figures

table of contents

I have a long thesis in the works, and I am not 100% sure what the requirements are for the display of the List of Figure section. Currently, all my figures have a long caption and a short caption:

\caption[Short Caption]{Long Caption}

and i would like to be able to switch all the captions in my \listoffigures between the two options in case I decide on one style or another.

Best Answer

This code enables either only long captions in the LoF or the regular short ones, as given in the optional argument to the \caption macro (well, actually the [...] delimited argument of \@caption)

I added a switch to control the behaviour:

  • Say \addcaptionsshorttrue to use only the short captions
  • Say \addcaptionsshortfalse to use the long entries

I've tested this with book, article, memoir and scrbook classes.

In addition, the redefined \caption command checks first if the \@captype macro expands to figure, i.e. it is a figure caption. Otherwise every caption (using the \@captype mechanism) would write either long or short entry to its relevant ToC.

No change is done for \captionof from caption package.


\documentclass{book}

\usepackage{caption}
\usepackage{xparse}

\usepackage{pgffor}


\makeatletter
\let\latex@@caption\caption

\newif\ifallcaptionsshort% toggle switch
\allcaptionsshorttrue% Use the short ones


\RenewDocumentCommand{\caption}{+o+m}{%
  \def\@figcaptype{figure}
  \ifx\@captype\@figcaptype
  \ifallcaptionsshort
  \IfValueTF{#1}{% 
    \latex@@caption[#1]{#2}%
  }{%
    \latex@@caption[#2]{#2}% No [#1] given, use the long caption then!
  }
  \else
  \latex@@caption[#2]{#2}%
  \fi
  \else
  \IfValueTF{#1}{% 
    \latex@@caption[#1]{#2}%
  }{%
    \latex@@caption[#2]{#2}% No [#1] given, use the long caption then!
  }
  \fi
}
\makeatother
\begin{document}


\allcaptionsshortfalse% Use the long ones
\listoffigures
\listoftables

\foreach \x in {1,...,10}{%
\begin{figure}
  \caption[Short title \x]{Some long dummy figure caption \x}
\end{figure}

\begin{table}
  \caption[Short table \x]{Some long dummy table caption \x}
\end{table}
}


\end{document}

enter image description here