[Tex/LaTex] Paths of `pgfimages` in beamer templates

beamertikz-pgf

I am working on a beamer template for my institute in which I am using \pgfdeclareimage for images. I have quite a lot images and logos for different projects/experiments/groups and I organised the files as

mainfolder/

-example.tex

-beamerthememy.sty

-art/myexample.pdf

Now the problem is how to declare the images. Using

\pgfdeclareimage[width=.5\paperwidth]{test}{art/myexample.pdf} 

works for the .tex file in the same folder as the .sty file and the art directory.

But when I put the .sty file and the art directory inside my $TEXMFHOME directory (and the .tex file somewhere else) I have to use

\pgfdeclareimage[width=.5\paperwidth]{test}{myexample.pdf}

I there a way to declare the image which works in both cases?


\documentclass{beamer}

% works if .tex file is in the same directory as the .sty file and `testdir`
\pgfdeclareimage[width=.5\paperwidth]{test}{art/myexample.pdf}  

% works if .sty file and `textdir` are in my lokal `texmf` folder
%\pgfdeclareimage[width=.5\paperwidth]{test}{myexample.pdf}     

\begin{document}

    \begin{frame}
         \pgfuseimage{test}
    \end{frame} 

\end{document}

To make the example minimal I moved \pgfdeclareimage from the .sty file to the .tex file. If you have the feeling, this is too minimal, I will add a .sty file

Best Answer

In a nutshell, if you pass more than a file name (a path) to pgfdeclareimage, pdflatex looks just there. Otherwise, it looks for it in the complete search path, which traditionally is specified in the TEXINPUTS environment variable.

So a simple solution would be to add ./art to TEXINPUTS and then just use the file name in pdfdeclareimage.

However, as this is for a reusable template, you have to keep your users in mind. MikTeX, for instance, handles search paths differently and many users have difficulties to fiddle with environment variables, especially when using some LaTeX-IDE. So For ease of use, I would try to handle this in the template itself:

\documentclass{beamer}

\newcommand{\samdeclareimage}[3][]{%
  % first check local art directory to prefer it over TEXINPUTS paths
  \IfFileExists{./art/#3}{%
    \pgfdeclareimage[#1]{#2}{./art/#3}%
  }{%
    % check if we can found it in TEXINPUTS
    \IfFileExists{#3}{%
      \pgfdeclareimage[#1]{#2}{#3}%
    }{%
      \GenericError{}{Theme image #3 not found!}{Consult Sam for all the details, but pressing 'H' might give you the right hint.}{The file '#3' has to be available either in a local 'art' directory or somehwere in your TEXMF tree.}
    }%
  }%
}%

\samdeclareimage[width=.5\paperwidth]{test}{myexample.pdf}  

\begin{document}

    \begin{frame}
         \pgfuseimage{test}
    \end{frame} 

\end{document}
Related Question