# Generate a list of figure sources from figure captions containing \source{}

This question stems from the same book project described in Generate a list of figure numbers and filenames from \includegraphics. The answers there provide the basic code to generate a text file of figure numbers and associated file names from LaTeX source. Now, I need to extend this method to a further task of collecting a text file of figure sources.

In this book (nearly) all figure \caption{}s have included in them a source{} command giving the figure source. It is presently defined just to print information about the figure source as text, with Source: in italics

\newcommand{\source}[1]{\emph{Source:} #1.}


Some figures require permissions, others don't, but for publication, the publisher requires a list of all figures, their sources and permission status.

My desire is to extract at least the basis for this from the LaTeX source by modifying \source{} command to also write lines to a List of Sources (los) file.

One example of a figure requiring permission is:

\begin{figure}[htb]
\centering
\includegraphics[width=.6\textwidth]{\theChapter/fig/cholera-monmonier-gilbert}
\caption[\desc{Presentation graphic} Mark Monmonier's re-vision of the Gilbert version of Snow's map, as
a presentation graphic]{\desc{Presentation graphic} Mark Monmonier's re-vision of the Gilbert version of Snow's map, as
a presentation graphic. \source{Monmonier (1996), \emph{How to Lie with
Maps}, p. 158. Image: permission required, University of Chicago Press.}
}


If this is Fig. 4.10 in the book, I'd like the re-defined \source{} command to write a line to an los file something like:

4.10 Monmonier (1996), \emph{How to Lie with Maps}, p. 158. Image: permission required, University of Chicago Press.


In other words: Wanted: A way to modify \source{} so that it also writes lines to an external file \jobname.los, of the form

fignum source


Can someone help with this?

I took from the linked answer (it's my answer) and added the \source command, writing to .los with \addcontentsline and adding the \thefigure such that it is clear which figure and which source belong together.

\addcontentsline{los}{section}{\thefigure: #1}

adds the relevant content to the .los file with formatting like section is done in a regular ToC. \@starttoc{los} reads the .los file and displays it content. The code is quite similar to \tableofcontents.

\documentclass{book}

\usepackage{xparse}
\usepackage{graphicx}
\usepackage{letltxmacro}

\makeatletter
\newcommand{\listofsources}{%
\chapter*{List of Sources}
\@starttoc{los}
\clearpage
}
\makeatother

\LetLtxMacro\davidsincludegraphics\includegraphics

\makeatletter

\RenewDocumentCommand{\includegraphics}{sO{}mo}{%
\IfBooleanTF{#1}{%
\davidsincludegraphics*[#2]{#3}%
}{%
\davidsincludegraphics[#2]{#3}%
}%
\begingroup
% Trying to determine the extension
\def\loc@l@ext{}
\IfValueTF{#4}{%
\def\loc@l@ext{#4}%
}{%
\IfFileExists{#3}{%
}{%
\IfFileExists{#3.pdf}{%
\edef\loc@l@ext{.pdf}%
}{%
\IfFileExists{#3.jpg}{%
\edef\loc@l@ext{.jpg}%
}{%
\edef\loc@l@ext{.png}%
}%
}%
}%
}%
\endgroup
}

\def\@starttocbutdonotshowit#1{%
\begingroup
\makeatletter
\if@filesw
\expandafter\newwrite\csname tf@#1\endcsname
\immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
\fi
\@nobreakfalse
\endgroup}

\newcommand{\listoffigurenumbernames}{%
\@starttocbutdonotshowit{lfn}%
}

\begin{document}
\listoffigurenumbernames

\listofsources

\chapter{First}

\begin{figure}[htb]
\centering
\includegraphics[width=.49\textwidth]{galton-interp2}[.png]
\includegraphics[width=.49\textwidth]{galton-interp3}
\caption{A reconstruction of Galton's method for finding contours of
approximately equal frequency in the relationship between heights of parents
and their children. \source{Foo stuff}
}%
\label{fig:galton-interp2}
\end{figure}

\begin{figure}[htb]
\centering
\includegraphics[width=.49\textwidth]{galton-interp4}
\includegraphics[width=.49\textwidth]{galton-inter5}
\caption{A reconstruction of Galton's method for finding contours of
approximately equal frequency in the relationship between heights of parents
and their children.
\source{Monmonier (1996), \emph{How to Lie with
Maps}, p. 158. Image: permission required, University of Chicago Press.}
}%
\label{fig:galton-interp4}
\end{figure}

\chapter{Second}

\begin{figure}[htb]
\centering
\includegraphics[width=.49\textwidth]{galton-interp6}
\includegraphics[width=.49\textwidth]{galton-inter7}
\caption{A reconstruction of Galton's method for finding contours of
approximately equal frequency in the relationship between heights of parents
and their children.
}%
\label{fig:galton-interp5}
\end{figure}

\end{document}

• That is a good start. I corrected an error, in that the name for the List of Sources information should be los, not lfn. – user101089 Jan 25 '18 at 15:21
• Also: In my use case, there are situations where I use a \url{} inside \source{}, e.g., \source{Library of Congress, \url{http://hdl.loc.gov/loc.pnp/ppmsca.33913}} }. I don't know why, but these generate an undefined control sequence error. – user101089 Jan 25 '18 at 15:28
• Finally: I don't really need a "List of Sources" in the Front section, although that is nice and does no harm. However, in the \jobname.los file it would be best if information for each figure was just fignum figsource, or fignum filename figsource all on on line. The code above gives me two separate lines for each figure: 1.1 ../ch06-scat/fig/galton-interp2.png and \contentsline {section}{1.1: Foo stuff; permission required}{3} – user101089 Jan 25 '18 at 15:37
• @user101089: Sorry, but .los and .lfn is not really an error. And your question is anything but clear. – user31729 Jan 25 '18 at 16:05
• Sorry; you were right about .los and .lfn. What I want is obtained by using just \DeclareRobustCommand{\source}[1]{\emph{Source:} \addtocontents{los}{\thefigure\space #1}#1.} – user101089 Jan 26 '18 at 14:58