# Patch \includegraphics to call \embedfile

I want to make a package that automatically attaches all essential*) source files to the ouput PDF. I started with the embedfile, filehook and currfile packages and put

\embedfile\currfilename
\AtBeginOfInputs{\embedfile\currfilename}
\AtBeginOfIncludes{\embedfile\currfilename}


in the preamble. So far so easy.

The next step would be to attach image files on invocation of \includegraphics. My first attempt was similar to the solution in embedall:

\LetLtxMacro\embedall@latex@includegraphics\includegraphics
\newcommand\embedall@includegraphics[2][]{%
\embedfile[desc=image]{#2}
\embedall@latex@includegraphics[#1]{#2}%
}


which has the restriction to only work if the full image file name (like image.png) is given.

I imagine that I would have to patch some macro of graphicx to call \embedfile (or some appropriate hook to be defined) after the full file name has been determined. I had a look into graphics.sty and graphicx.sty, but I was not able to figure out the right patch.

Does anyone have any ideas about that?

*) At this point, "essential" essentially means such files that are considered to contain the document's content.

Sorry, that was easy:

\let\es@Gin@setfile\Gin@setfile
\def\Gin@setfile#1#2#3{\es@Gin@setfile{#1}{#2}{#3}\embedfile{#3}}


seems to do the job.

I'd use a different patch, hooking where \Gin@setfile adds the file to the file list:

\documentclass{article}
\usepackage{graphicx,embedfile,etoolbox}

\makeatletter
\patchcmd{\Gin@setfile}
{}{}
\makeatother

\begin{document}

\includegraphics{example-image}

\includegraphics{example-image-a.png}

\end{document}


• What would be the implications/advantages? I reckon this part of the code is not expanded in draft mode. But this is not necessarily what I want. – DLichti May 12 at 20:10
• @DLichti Isn't that a good reason? Would you embed a file also when draft mode is used? I can't see why. – egreg May 12 at 20:54
• Even in draft mode, the image file is necessary to reproduce the output including the correctly sized place holder. And if another person wants to finalize the document, then the image file is definitely necessary. Or am I missing something? – DLichti May 12 at 21:05
• @DLichti In my opinion, if you distribute a version in draft mode, you don't want people to see the image. – egreg May 12 at 21:18