[Tex/LaTex] How to get the gnuplottex/epstopdf package to work with “-output-dir” and “-aux-dir” option

epstopdfgnuplotpdftex

I can't seem to get the gnuplottex package to work with my preferred compile options. This is a (not too) minimal example of what I mean:

\documentclass[a4paper,10pt]{scrreprt}

%     \usepackage{etoolbox}    % proposed fix (not working)
\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{gnuplottex}

\graphicspath{{./tmp/}}
\epstopdfsetup{outdir=./tmp/}

% proposed fix (not working)
%     \let\nodirfigname\figname
%     \def\figname{./tmp/\nodirfigname}
% 
%     \expandafter\patchcmd\csname\string\gnuplot\endcsname
%     {\figname}{\nodirfigname}{}{}

\begin{document}

\begin{figure}[htbp]
    \begin{gnuplot}[terminal=epslatex,terminaloptions=color]
        reset
        plot sin(x)
    \end{gnuplot}
    \caption[Gnuplot]{Gnuplot direkt in \LaTeX}
    \label{fig:gnu}
\end{figure}

\end{document}

If I compile the above file via:

pdflatex -shell-escape -output-directory=./tmp -aux-directory=./tmp bla.tex

I get the following error messages:

[Loading MPS to PDF converter (version 2006.09.02).] ) Opening gnuplot
stream bla-gnuplottex-fig1.gnuplotCannot open load file
'bla-gnuplottex-fig1.gnuplot' line 0: util.c: No such file or
directory

(./tmp/bla-gnuplottex-fig1.texepstopdf ($Id: epstopdf.pl 17496
2010-03-18 17:57:31Z karl $) 2.15 !!! Error: Cannot open
bla-gnuplottex-fig1.eps: No such file or directory

! Package pdftex.def Error: File
`./tmp/bla-gnuplottex-fig1-eps-converted-to.pd f' not found.

So obviously epstopdf doesn't like my tmp-dir too much. How do I get the damn thing to convert the .eps files to pdf so that gnuplottex can include it? Any ideas?

Best Answer

You have to patch the \figname command of gnuplottex, but now the gnuplot environment must not use \figname and it's necessary to patch also this one

\usepackage{etoolbox}

\let\nodirfigname\figname
\def\figname{./tmp/\nodirfigname}

\expandafter\patchcmd\csname\string\gnuplot\endcsname
  {\figname}{\nodirfigname}{}{}

The curious way to patch \gnuplot is necessary because it's a command having an optional argument.

The code must go after loading gnuplottex (of course \usepackage{etoolbox} can go anywhere before).


Update

With xpatch it's easier

\usepackage{xpatch}

\let\nodirfigname\figname
\def\figname{./tmp/\nodirfigname}

\xpatchcmd\gnuplot{\figname}{\nodirfigname}{}{}