# How to expand the \caption-command to pre-insert the affiliated source of figure/table?

In scientific works it is quite common to specify the source for every statement. This is also necessary for tables or images of course.

Usually I'm always using the \caption-command for adding some description to tables and figures. Unfortunately, I have to put in the source of that table/figure manually by myself all the time.

In the past, I tricked it like that (what is really annoying time by time):

\caption[Caption]{Caption\newline\footnotesize{Source: Own representation}}


Here is some MWE:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage[justification=justified,singlelinecheck=false]{caption}

\begin{document}

\begin{figure}[ht]
\centering
\includegraphics[width=1\textwidth]{figs/test1.png}
\caption[Caption for lof]{Caption for figure\newline\footnotesize{Source: Own representation}}
\end{figure}%

\end{document}


What if one could insert an additional {}-variable to the \caption-command to get a separate field for adding the source?

Right now, it looks like that (standard-command):

\caption[Caption for list of figures]{Caption for figure itself}


What if we could expand that \caption-command to get something like that:

\caption[Caption for list of figures]{Caption for figure itself}{Source}


The dreamlike solution would be to auto-fill the text Source: Own representation if one doesn't fill in some other text into that field. That could be handled with some if/else-commands like that:

If the field is empty: Insert "Source: Own representation"

If the field is NOT empty: Insert "Source: {Inserted text}"


Do you have any idea how one could get that?

Thank you very much for your help!

I suggest not to change the syntax of \caption, but defining a \Caption command with the help of xparse. The syntax is

\Caption[<caption for lof>]{Caption}[<optional source>]


The first optional argument has the usual meaning; the trailing optional argument specifies the source; if empty, “Own representation” is used.

Here are some examples of usage.

\documentclass{article}
\usepackage{graphicx}
\usepackage[justification=justified,singlelinecheck=false]{caption}
\usepackage{xparse}

\NewDocumentCommand{\Caption}{
O{#2}                 % optional (default = value of mandatory argument)
m                     % mandatory
O{Own representation} % optional (default = given text)
}{%
\caption[#1]{#2\newline\footnotesize Source: #3}%
}

\begin{document}

\begin{figure}[!htp]
\centering

\includegraphics[width=\textwidth,height=2cm]{example-image}

\Caption
[Caption for lof]
{Caption for figure}[Some author]
\end{figure}

\begin{figure}[!htp]
\centering

\includegraphics[width=\textwidth,height=2cm]{example-image}

\Caption
{Caption for figure}[Some author]
\end{figure}

\begin{figure}[!htp]
\centering

\includegraphics[width=\textwidth,height=2cm]{example-image}

\Caption{Caption for figure}
\end{figure}

\end{document}


• You are right! Furthermore, my system missed the first [] before the {}. After inserting that, everything worked fine! :-) Thanks a lot! – Dave Mar 7 '17 at 12:55

I suggest another approach: define a variant of \includegraphics to add a second optional argument which incorporates the source, thanks to the copyrightbox package and xparse.

\documentclass{article}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{xparse}

\DeclareDocumentCommand\myincludegraphics{o O{Source: Own representation} m }{%
}

\begin{document}

\begin{figure}[!htb]
\centering
\myincludegraphics[width=0.85\linewidth][Yuri Norstein]{loupnorstein2}
\caption[Caption for lof]{Tale of tales (1979)}
\end{figure}%

\begin{figure}[!htb]
\centering
\myincludegraphics{Hedgehog-in-the-Fog}
\caption[Caption for lof]{Hedgehog in the fog (1975)}
\end{figure}%
\end{document}


• Thanks a lot for this great solution, it's looking perfect! Unfortunately, one can not use that for tables... There is no option to get something like that with the \caption-command? – Dave Mar 7 '17 at 12:32
• I don't see how it could be done. But you can use \copyrightbox with tables. The only problem is you do not have the equivalent of \includegraphics for tables. That said, you can obtain a very similar result with threepartable and its tablenotes. The only superiority of copyrightbox is you can place the affiliated source on any side of the table/figure. – Bernard Mar 7 '17 at 12:50