27

How can I add a source to a figure? I need to add \ref, \cite or just free text. Something like that:

\begin{figure} [ht]
  \centering
  \includegraphics[width=0.95\textwidth]{res/figure.pdf}
    \caption{Caption}
    \source{\ref{},\cite{} or free Text}
  \label{fig:gliederung}
\end{figure}

Should give me:

Result

The source shouldn't appear in \listoffigures.

1
  • 7
    Have you tried \caption[Caption]{Caption. Source: here the source of the picture}?
    – Daniel
    Commented Jan 23, 2013 at 16:26

3 Answers 3

19

There are many ways to do this. The source could be put inside the caption. LaTeX puts the caption in one line, if it fits there. This can be prevented by \hspace{\linewidth} in the following example. When LaTeX tests the width by putting it in a \hbox, then \hspace is taken into account. LaTeX concludes that the caption does not fit into one line and by setting it in several lines, the line break is executed and the white space right after it (\hspace{\linewidth}) is ignored:

\documentclass{article}

\newcommand*{\captionsource}[2]{%
  \caption[{#1}]{%
    #1%
    \\\hspace{\linewidth}%
    \textbf{Source:} #2%
  }%
}

\begin{document}
\begin{figure} [ht]
  \centering
  \captionsource{Caption}{ref, cite or free Text}
  \label{fig:gliederung}
\end{figure}
\end{document}

Alternatively package caption provides an option singlelinecheck, where the testing for fitting into a line can be disabled, using possible multi-line mode always.

2
  • What is the purpose of the asterisk (*) just after the command \newcommand ?
    – Randerson
    Commented Jul 10, 2017 at 19:09
  • 1
    @Randerson If a macro is defined via the star form \newcommand*, then the arguments must not contain empty lines or \par tokens. Omit the star, if you expect caption titles to contain paragraphs. Otherwise, TeX is able to throw an error message earlier, if the user has forgotten the closing curly brace of the argument. Commented Jul 10, 2017 at 19:12
62

I know it makes a lot of time from this post but I had the same problem and found the solution you where looking for. I will post it just in case someone founds it useful.

I took the idea from Gonzalo Medina's post A: How to write a source description under the loaded picture?.

Solution: load the caption package (i.e. write \usepackage{caption} at the preamble of your document) and add this line:

\newcommand{\source}[1]{\caption*{Source: {#1}} }

How to use it: The way I defined this command, it couldn't be more intuitive:

\begin{figure}
    \includegraphics{./imageName}
    \caption{Caption of the image.} \label{imageLabel}
    \source{Source of the image.}
\end{figure}

How it works: The added line creates a new command \source{} with a single input parameter (your source). The * in \caption*{} prevents the figure caption to be accounted for LaTeX and shown in \listoffigures In fact if you type:

\begin{figure}
    \includegraphics{./imageName}
    \caption{Caption of the image.}\label{imageLabel}
    \caption*{Source: Source of the image.}
\end{figure}

Style modification:

I personaly like having all my sources aligned to the right, so I usually define the command above like that:

\newcommand{\source}[1]{\caption*{\hfill Source: {#1}} }

Also the vertical spacing between Caption and Source can be modified adding a \vspace{} command like that:

\newcommand{\source}[1]{\vspace{-3pt} \caption*{ Source: {#1}} }

([+] value will add space and [-] value will reduce it)

I hope it helps!

11
  • 1
    @Xavi in connection with the answer, how can I reduce the spacing between caption and source? I am using Memoir, but I do not think that it matters in the circumstances. Commented Dec 13, 2015 at 0:10
  • 4
    Do make sure you use \usepackage{caption} at the start of your document! I skipped over that part the first time and it started displaying the star and doing other weird things. Leaving this here in case someone else forgets; maybe they'll see this comment.
    – Pro Q
    Commented May 2, 2018 at 19:41
  • 1
    I encountered the same problem like @ProQ. This comment by Axel Sommerfeldt proved to be rather enlightening.
    – Mr. Tao
    Commented May 28, 2018 at 17:45
  • 1
    Thanks for your feedback @ProQ and @Mr. Tao! I made some changes to better explain how to load the caption package.
    – Xavi
    Commented May 30, 2018 at 6:37
  • 2
    Great answer, but somehow this is not working with Beamer. Anyone ran into the same problem? Commented Aug 15, 2018 at 14:35
0

Alternative command that could be used for achieving similar results based on @Xavi's answer could look like that:

\usepackage{caption}
\newcommand{\captionwithsource}[2]{
    \caption{#1}
    \vspace{-0.5\baselineskip}
    \caption*{Źródło: {#2}}
}

Then when one needs a caption with source one can use a command \captionwithsource{Caption text}{Source text} otherwise normal \caption{Caption text}. The benefit of that command is that you don't have 2 separate commands to use.

One can also rename the command to whatever they want as I understand that captionwithsource can be quite a mouthful. :)

Additionally in my solution instead of using negative \vspace with hardcoded value I've decided to use \vspace{-0.5\baselineskip} which should look just fine for the most font sizes. (Given that one changes it using \captionsetup[font=<size>] command.)

MWE:

\documentclass{article}

\usepackage{blindtext}
\usepackage{caption}
\captionsetup{font=footnotesize}
\newcommand{\captionwithsource}[2]{
    \caption{#1}
    \vspace{-0.5\baselineskip}
    \caption*{Source: {#2}}
}

\begin{document}

\begin{table}[h]
    \centering
    \captionwithsource{This is a table}{https://example.com/}
    \begin{tabular}{|c|c|c|}
        \hline
        Lorem ipsum dolor & Lorem ipsum & dolor sit amet \\
        \hline
        1                          & 2           & 3              \\
        \hline
    \end{tabular}
\end{table}

\blindtext

\begin{figure}[h]
    \centering
    \rule{0.5\textwidth}{0.5\textwidth}
    \captionwithsource{This is a figure}{https://example.com/}
\end{figure}

\end{document}

Result:

Results of MWE

1
  • Welcome to TeX.SE! Please, as usual here, show a short compilable TeX code for a fast proof of your solution!
    – Mensch
    Commented Feb 16 at 19:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .