[Tex/LaTex] Gnuplot subdirectory not found using epstopdf

epsgnuplotpdftex

I'm moving from the gnuplot latex terminal, to the epslatex terminal. I use pdflatex. With the epslatex terminal, I receive two output files, a .tex and a .eps. I can use epstopdf to convert the eps file to pdf, and I can include the graph in my latex file using:

\input{mygraph}

However, I have each graph in a separate directory. If I use:

\input{graphdir1/mygraph}

with the latex terminal, everything is fine. However, if I use the epslatex terminal and convert the eps file to a pdf, I get an error message:

[8] (./graphdir1/mygraph.tex

./graphdir1/mygraph.tex:101: LaTeX Error: File `mygraph' not found.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...

l.101     \put(0,0){\includegraphics{mygraph}}

I have graphdir1 in the same directory as the main latex file. Do I now need to specify to latex that it should search in the subdirectory? Why did I not require to do so previously?

Best Answer

Gnuplot's epslatex terminal generates an encapsulated postscript file containing the graph, and a tex file containing a sequence of commands to import the graph and add labels, etc. The problem occurs when TeX tries to import the graph (converted to pdf or not). The \includegraphics command that is contained within mygraph.tex does not contain the subdirectory name in its argument, so TeX looks in the 'usual' places, including the directory containing your main document, but not the subdirectory. So the answer to your first question is 'yes' (but see edit below).

In contrast, gnuplot's latex terminal generates a tex file containing a sequence of commands that draws everything using the picture environment. There is no second file to find, and no problem occurs.

EDIT

Gnuplot can issue shell commands, so you can use a string replacement utility to fix the problem. Using sed, a simple gnuplot script would look like this.

set term epslatex
set output 'temp.tex'
plot x**3
set output #Closes the temporary output files.
!sed 's|includegraphics{temp}|includegraphics{graphdir1/mygraph}|' < temp.tex > mygraph.tex
!epstopdf temp.eps --outfile='mygraph.pdf'

This way, you don't have to edit 'mygraph.tex' after running gnuplot, because it will contain \includegraphics{graphdir1/mygraph} in place of \includegraphics{temp}, thanks to sed. As a bonus, the pdf version of the graph is generated automatically.