# Add optional argument to the caption to get into figure list entry

I would like to add the optional list entry, which is used for a listoffigures, also to the caption.

In this basic example:

\documentclass{scrreprt}
\usepackage{caption}
\begin{document}

\begin{figure}
\includegraphics{baz}
\caption[Baz]{Foo Bar}
\end{figure}

\input{foo}
\end{document}


the caption should read "Figure 1: Baz- Foo Bar" instead of the default "Figure 1: Foo Bar". Any ideas?

To add the optional argument, you can patch \@caption which passes its contents on to \@makecaption:

\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\patchcmd{\@caption}{#3}{#2~--~#3}{}{}
\makeatother


This just inserts the second argument of \@caption (containing the optional argument to \caption as part of the main caption:

\documentclass{scrreprt}% http://ctan.org/pkg/koma-script
\usepackage{caption,graphicx}% http://ctan.org/pkg/{caption,graphicx}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\patchcmd{\@caption}{#3}{#2~--~#3}{}{}
\makeatother
\begin{document}

\begin{figure}
\centering\includegraphics{example-image-a}
\caption[Baz]{Foo Bar}
\end{figure}

\end{document}


Note the requirement to use a \makeatletter...\makeatother pair. For more on this see What do \makeatletter and \makeatother do?.

Alternatively, if you don't want to do this globally, you can just use

\caption[Baz]{Baz~--~Foo Bar}


which would be a local (verbatim) version of the above.

For this to work successfully with hyperref, a larger-scale replacement of contents within \@caption is required, as offered by regexpatch's \xpatchcmd* replace-all patch:

\documentclass{scrreprt}% http://ctan.org/pkg/koma-script
\usepackage{caption,graphicx}% http://ctan.org/pkg/{caption,graphicx}
\usepackage{regexpatch,hyperref}% http://ctan.org/pkg/{regexpatch,hyperref}
\makeatletter
\xpatchcmd*{\@caption}{#3}{#2~--~#3}{}{}
\makeatother
\begin{document}

\begin{figure}
\centering\includegraphics{example-image-a}
\caption[Baz]{Foo Bar}\label{myfig}
\end{figure}

See Figure~\ref{myfig} with title~\nameref{myfig}.
\end{document}

• This answers the question perfectly, but is there also a way to use it with the nameref and/or the hyperref package? – panther Aug 14 '13 at 21:08
• @panther: I've updated my answer to include support for hyperref and nameref. – Werner Aug 15 '13 at 7:03
• Works like charm, thanks. For other people it should be notified, that the regexpatch is not part of TeXLive 2009. IMHO at least TeXLive 2012 is required. In my personal experience, the installation of TexLive2013, to a extra directory with the install script from tug.org/texlive, was less hassle than using a package based update. Many TeX editors can be configured which version to use. – panther Aug 15 '13 at 16:41
• this doesn't work for me :(. There is no error, just that the short option is not shown. Could it be because I'm also using the subcaption package ? But I load it before patching – Ciprian Tomoiagă Feb 28 '18 at 15:23
• actually, for the second version, \usepackage{regexpatch} throws an error saying that l3regex is obsolete – Ciprian Tomoiagă Mar 3 '18 at 20:27