# Automatically Attach Graphic Files (to the PDF Output) that are Used in \includegraphics Command With Options

This is a follow-up question to (not only) beamer: Automatically Attach Graphic Files (to the PDF Output) that are Used in \includegraphics Command answered beautifully by David Carlisle.

## 1st Question Block

1. I would like to attach a text file with the same file name (and same destination) as the picture that contains the source for example.

2. It would be perfect if this could be "missing file tolerant" (only attach the text file if it's avaialble).

Here's a pseudocode for that:

\includegraphics[width=0.5\textwidth]{example-image.png}
\textattachfile{example-image.png}{}
% If possible
\textattachfile{example-image.txt}{} % <-- Text file containing the source for example.


## 2nd Question Block

1. Is there a way to have an additional option in the \includegraphics command like myAttach=false so that I can deactivate the attaching for specific pictures?
2. Default (no option given) should be that the picture and the text file are attached.

Here's a pseudocode for that:

\includegraphics[width=0.5\textwidth, myAttach = false]{example-image.png}
% If "myAttach = false" is not mentioned
\textattachfile{example-image.png}{}
% If possible AND if "myAttach = false" is not mentioned
\textattachfile{example-image.txt}{} % <-- Text file containing the source for example.


## MWE so far (result from previous question)

\documentclass{beamer}
\usepackage{graphicx}
\usepackage{attachfile}

\makeatletter
\let\saved@Gin@setfile\Gin@setfile
\def\Gin@setfile#1#2#3{%
\saved@Gin@setfile{#1}{#2}{#3}%
\textattachfile{#3}{}}
\makeatother

\begin{document}

% For example image, see https://tex.stackexchange.com/questions/231738

\begin{frame}[c]
\frametitle{Frame With Image}
\centering
\includegraphics[width=0.5\textwidth]{example-image.png}
\textattachfile{example-image.png}{}
\end{frame}

\end{document}


something like

\documentclass{beamer}
\usepackage{graphicx}
\usepackage{attachfile}

\makeatletter
\let\saved@Gin@setfile\Gin@setfile

\def\Gin@setfile#1#2#3{%
\saved@Gin@setfile{#1}{#2}{#3}%
\ifGin@attach
\textattachfile{#3}{}%
{\def\Gin@ext{.txt}%
\IfFileExists{#3}{%
\textattachfile{#3}{}%
}{\PackageWarning{graphicx}{no #3 to attach}}%
}%
\fi
}
\newif\ifGin@attach\Gin@attachtrue
\define@key{Gin}{attach}[true]{%
\lowercase{\Gin@boolkey{#1}}{attach}}

\makeatother

\begin{document}

% For example image, see http://tex.stackexchange.com/questions/231738

\begin{frame}[c]
\frametitle{Frame With Image}
\centering
\includegraphics[width=0.3\textwidth]{example-image.png}
\includegraphics[width=0.3\textwidth]{example-image-a.png}
\includegraphics[width=0.3\textwidth,attach=false]{example-image-b.png}
\end{frame}

\end{document}


(Example where only example-image-a.txt was available.)

• Works perfectly! The feature with including a text file (if available) is not implemented yet. Are you also trying to achieve it? – Dr. Manuel Kuehner Jan 24 '17 at 19:46
• @Dr.ManuelKuehner try now – David Carlisle Jan 24 '17 at 19:56
• Great! I modified the example and added a screenshot - hope that's ok! – Dr. Manuel Kuehner Jan 24 '17 at 20:00
• @Dr.ManuelKuehner sure :-) – David Carlisle Jan 24 '17 at 20:01
• @Dr.ManuelKuehner inside your makeatletter area define \newcommand\mypdfpages[2][]{{\let\Gin@setfile\saved@Gin@setfile\pdfpages[#1]{#2}}} then use \mypdfpages probably works (untested:-) – David Carlisle Feb 15 '17 at 23:01