[Tex/LaTex] List of figures – image source in captions

captionsfloats

When I insert a image I usually do it this way

\begin{figure}
    \includegraphics[width=\textwidth]{bilder/duese_halb}
    \caption[Image-Test \cite{source1}]{Image-Test}
    \label{fig:image-test}
\end{figure}

I want the source only to appear in the list of figures and the caption both in the text and in the list.

The code shown above works well but if you have lots of text in the caption you always have to copy and paste it and if you change something in the caption it happens that you forget to change the second text
So is it possible to use the argument in the {} part and just add the source in the [] part and both will be shown in the list of figures?

\caption[\cite{source1}]{Image-Test}

Best Answer

\documentclass{article}

\usepackage{xparse}

\NewDocumentCommand{\captionsource}{omm}{%
  \IfNoValueTF{#1}
   {% no leading optional argument
    \caption[#2\formatsource{#3}]{#2}%
   }
   {% leading optional argument
    \caption[#1\formatsource{#3}]{#2}%
   }
}
\NewDocumentCommand{\formatsource}{m}{%
  \unskip\ (Source:~\cite{#1})%
}

\begin{document}

\listoffigures

\begin{figure}[htp]
\Huge X
\captionsource{Test caption}{source1}
\end{figure}

\begin{figure}[htp]
\Huge Y
\captionsource[Short text]{Long test caption}{source2}
\end{figure}

\begin{thebibliography}{9}

\bibitem{source1} Something

\bibitem{source2} Else

\end{thebibliography}

\end{document}

I defined \formatsource in this way just to give the flavor; there are two reasons for doing so: the main one is that you're not tied to some format hardwired in the definition of \captionsource; also important is that \formatsource is robust, so no matter whether \cite is robust or fragile (depending on whether you load some packages it may become fragile), there will be no problem. You can simply use

\NewDocumentCommand{\formatsource}{m}{%
  \unskip~\cite{#1}%
}

if you don't want to specially format the citation.

enter image description here