1

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}

1 Answers1

3

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}

enter image description here

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

David Carlisle
  • 757,742