# Writing file names used with graphicx

I'm trying to modify the command \includegraphics{}to write the name of the included file, including its path and extension, to a text file. What I have so far is:

\makeatletter
\newwrite\myfile
\immediate\openout\myfile=myfigures.txt
\let\TempCmd\Ginclude@graphics
\renewcommand{\Ginclude@graphics}[1]{\TempCmd{#1}\immediate\write\myfile{#1}}
\makeatother


The only problem with this is that the file extension won't get written if it's not specified. For example, if I have mypic.eps in a subfolder called figures, and I write \includegraphics{figures/mypic}, then myfigures.txt will only have figures/mypic written to it, when what I want is figures/mypic.eps to be written.

I've tried poking around in graphicx.sty and snapshot.sty to see if I can figure out how they handle file name extensions, and hack something together myself, but I haven't been able to figure it out yet. If I put \filename@parse{#1} in there I get nothing for \filename@ext.

Here's my minimal working example (just supply some image of your choice):

\documentclass{article}
\usepackage{graphicx}

\makeatletter
\newwrite\myfile
\immediate\openout\myfile=myfigures.txt
\let\TempCmd\Ginclude@graphics
\renewcommand{\Ginclude@graphics}[1]{\TempCmd{#1}\immediate\write\myfile{#1}}
\makeatother

\begin{document}
\includegraphics{mypic}
\end{document}

-
graphicx uses more code to scan with a list of possible extensions and paths. You need to replicate that code if you want the same behaviour. As it happens, I'm currently working on a package to provide such lookup and path parsing macros directly to the user. But this package isn't finished yet. The source repository lies at bitbucket.org/martin_scharrer/fileparse/src if you want to have a look at it. – Martin Scharrer Apr 3 '13 at 16:20
Welcome to TeX.sx! – Peter Jansson Apr 3 '13 at 16:20
@MartinScharrer Would welcome thoughts on what a LaTeX3 version should provide in terms of functionality :-) – Joseph Wright Apr 3 '13 at 16:40
Thank you for you suggestion and sharing your code, @MartinScharrer. – Marcus Fontaine Apr 3 '13 at 16:53

graphics is already trying various extensions and depending on the back end chosen working out which file you use, so you want to hook in there just before it calls the back end to actually include the file, not at the top level command.

\documentclass{article}
\usepackage{graphicx}

\makeatletter
\newwrite\myfile
\immediate\openout\myfile=myfigures.txt
\let\TempCmd\Gin@setfile
\def\Gin@setfile#1#2#3{\TempCmd{#1}{#2}{#3}\immediate\write\myfile{#3}}
\makeatother

\begin{document}
\includegraphics{mypic}
\end{document}


Produces

mypic.png

-
This is exactly what I needed, thank you. – Marcus Fontaine Apr 3 '13 at 17:01