7

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

  • 2
    Simple workaround: \IfFileExists{art/example.pdf}{\pdfdeclareimage[...]{test}{art/myexample.pdf}}{\pdfdeclareimage[...]{test}{myexample.pdf}}, which could of course be further improved by moving this into a macro and maybe also checking the second path with \IfFileExists. – Daniel May 4 '15 at 15:44
  • Is there a reason you are using pgfimage... rather than graphicx? You need this if you are declaring masks etc. but otherwise, graphicx is recommended. – cfr May 4 '15 at 16:38
  • @Daniel Great idea! I have to test thoroughly, but it looks, like this might solve my problem. Would you turn your comment into an answer? – samcarter_is_at_topanswers.xyz May 4 '15 at 23:53
  • Well, the TikZ manual recommends using graphicx unless you especially need something the pgfimage... stuff handles. Page 1066: 'To be quite frank, LaTeX’s \includegraphics is designed better than pgf’s image mechanism. For this reason, I recommend that you use the standard image inclusion mechanism of your format. Thus, LaTeX users are encouraged to use \includegraphics to include images.' – cfr May 5 '15 at 0:03
  • @cfr Thanks for your quick replay and pointing to the relevant section of the manual. I think "The advantage of this two-phase approach is that, at least for pdf, the image data will only be included once in the file. This can drastically reduce the file size if you use an image repeatedly" is some really big advantage for my project. – samcarter_is_at_topanswers.xyz May 5 '15 at 0:14
7
+100

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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