Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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

1 Answer 1

up vote 7 down vote accepted

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
share|improve this answer
    
This is exactly what I needed, thank you. –  Marcus Fontaine Apr 3 '13 at 17:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.