# Generate a list of figure numbers and filenames from \includegraphics

For a book project with lots of figures, I need to generate a list of the filenames of all figures included via includegraphics{} and their figure numbers. Something like:

1.1 ch01-intro/fig/lascaux.jpg
1.2 ch01-intro/fig/plot-weather-new.png
1.3 ch01-intro/fig/huygens1669.png
1.4 ch01-intro/fig/priestley-chart-biography-edit.png


There is a perl script on CTAN, texdepend, that will find the filenames, but it doesn't necessarily list them in order and doesn't grok the figure numbers. (It was designed for generating a Makefile, or preparing a ZIP file for a LaTeX project.)

> texdepend -format=1 -print=f TOGS.tex |head
# C:\batchfiles\texdepend.pl, v0.96 (Michael Friendly (friendly@yorku.ca))
# commandline: C:\batchfiles\texdepend.pl -format=1 -print=f TOGS.tex
# FIGS =
ch05-playfair/fig/playfair1805-inquiry-crop.jpg
ch01-intro/fig/lascaux.jpg
ch01-intro/fig/plot-weather-new.png
ch01-intro/fig/huygens1669.png
ch01-intro/fig/priestley-chart-biography-edit.png
ch01-intro/fig/marey-trains.png
ch01-intro/fig/Minard-cotton3.png


Similar questions have be asked here and there but none have the requirement of including the figure numbers.

Wanted: A way to modify includegraphics itself so that it also writes lines to an external file, of the form

fignum filename


One small complication is that a given begin{figure} environment can contain two or more \includegraphics commands, for example:

\begin{figure}[htb]
\centering
\includegraphics[width=.49\textwidth]{\theChapter/fig/galton-interp2}
\includegraphics[width=.49\textwidth]{\theChapter/fig/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.
}%
\label{fig:galton-interp2}
\end{figure}


If this generates Fig. 6.16, the result should look like

6.16 ch06-scat/fig/galton-interp2.png
6.16 ch06-scat/fig/galton-interp3.png

• The redefinition of the \includegraphics macro is not the real problem concerning the writing of the figure number and the file name, but determination of the figure number itself which is increased by \caption which is done later than \includegraphics here, unless you use it before... – user31729 Sep 20 '17 at 20:04
• There is another, more difficult, problem here: includegraphics() by itself, doesn't have access to the filename extensions-- they could be .png, .jpg or pdf for pdflatex. – user101089 Sep 21 '17 at 2:20
• That's true...... – user31729 Sep 21 '17 at 6:26

This redefines \includegraphics by adding more code at the end, artifically increasing the figure number (within a group) and writing to a ToC like file with \addtocontents to have the benefit of the ToC mechanism with the \tf@... file handles.

The file will be named \jobname.lfn and is only written if \listoffigurenumbernames is used.

Remove the demo option for the real use - case, I put it in there since I don't have the various graphics files on my disk.

\documentclass{book}

\usepackage{xparse}
\usepackage[demo]{graphicx}
\usepackage{letltxmacro}

\LetLtxMacro\davidsincludegraphics\includegraphics

\makeatletter
\RenewDocumentCommand{\includegraphics}{O{}m}{%
\davidsincludegraphics[#1]{#2}%
\begingroup
\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

\chapter{First}

\begin{figure}[htb]
\centering
\includegraphics[width=.49\textwidth]{\thechapter/fig/galton-interp2}
\includegraphics[width=.49\textwidth]{\thechapter/fig/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.
}%
\label{fig:galton-interp2}
\end{figure}

\begin{figure}[htb]
\centering
\includegraphics[width=.49\textwidth]{\thechapter/fig/galton-interp4}
\includegraphics[width=.49\textwidth]{\thechapter/fig/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.
}%
\label{fig:galton-interp4}
\end{figure}

\chapter{Second}

\begin{figure}[htb]
\centering
\includegraphics[width=.49\textwidth]{\thechapter/fig/galton-interp6}
\includegraphics[width=.49\textwidth]{\thechapter/fig/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}


This gives

1.1 1/fig/galton-interp2
1.1 1/fig/galton-interp3
1.2 1/fig/galton-interp4
1.2 1/fig/galton-inter5
2.1 2/fig/galton-interp6
2.1 2/fig/galton-inter7


Improved version

With 'heuristic' determination of file extension for .jpg, .pdf or .pdf

The \includegraphics macro has a 4th optional argument which can be used in order to override the file extension.

\documentclass{book}

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

\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

\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.
}%
\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.
}%
\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}

• Excellent! I didn't think it would be that easy to solve the file extension problem. – user101089 Sep 21 '17 at 13:03
• @user101089: yes, but a nested test with \IfFileExists is really tedious. Luckily there are only three possible extensions ;-) – user31729 Sep 21 '17 at 17:19