How to Set a Local `\graphicspath` for Gnuplot epslatex Plots
At a glance
- 1. Generate the plot files
Write the epslatex output into a directory, producing a TeX file and its companion graphic.
- 2. Set a scoped search path
Group \graphicspath with the \input command so the path applies only while the plot is included.
- 3. Compile with EPS support
Use a LaTeX-to-DVI workflow for the EPS graphic, or provide a PDF version for pdfLaTeX.
Gnuplot’s epslatex terminal writes a TeX file for the labels and plot layout, plus a separate graphic file. You can include the TeX file with \input and use a local \graphicspath so LaTeX finds the companion graphic without changing the search path for the rest of your document.
The key is to put both \graphicspath and \input inside a TeX group. The path then applies while the generated plot is read and is restored afterward.
Scope the graphics path around the plot
Load graphicx, then group the path setting and the input command:
\begingroup
\graphicspath{{plots/}}
\input{plots/curve.tex}
\endgroup
The braces around plots/ are part of the \graphicspath syntax. Each directory is written as a braced item, and directory names should end with a slash. You can list more than one directory, for example \graphicspath{{plots/}{figures/}}.
The group is what makes the setting local. In normal LaTeX use, \graphicspath changes the paths searched by \includegraphics; grouping it prevents that change from carrying over to later figures. The \input path and the graphics search path do different jobs: plots/ in \input{plots/curve.tex} locates the TeX file, while \graphicspath{{plots/}} helps locate the graphic named from inside that file.
Worked example: generate and include a plot
Create the plots directory before running gnuplot. Save this as make-curve.gp in the project’s main directory:
set terminal epslatex color
set output "plots/curve.tex"
plot cos(x) title "cos(x)"
set output
Running gnuplot make-curve.gp creates the plot’s TeX file and its companion EPS graphic in plots/. With the standard epslatex terminal output, the graphic is typically named curve-inc.eps; check the generated TeX file for the exact name it requests.
Save the following as main.tex, also in the project’s main directory:
\documentclass{article}
\usepackage{graphicx}
\begin{document}
Here is the plot:
\begin{figure}[ht]
\centering
\begingroup
\graphicspath{{plots/}}
\input{plots/curve.tex}
\endgroup
\caption{A cosine curve generated by gnuplot.}
\end{figure}
\end{document}
This example uses the graphicx package. Compile it from the project’s main directory with a DVI-based LaTeX workflow, such as latex main.tex followed by dvips main.dvi -o main.ps. That workflow supports the EPS graphic produced by epslatex. The plots/ directory must exist, and the gnuplot-generated files must be there when you compile.
Where the path is resolved
Graphics paths are interpreted relative to the main document’s working directory, usually the directory from which you run LaTeX. They are not automatically relative to the directory containing an \input file. Thus, if main.tex is in the project root and the plot files are in plots/, \graphicspath{{plots/}} is appropriate even though the plot file is also read as \input{plots/curve.tex}.
If the generated TeX file refers to a graphic name that already includes a directory, or if you move the generated files, inspect the \includegraphics line in that TeX file and adjust the path accordingly. Avoid editing the generated file as your usual solution: gnuplot will overwrite it the next time you generate the plot.
Using pdfLaTeX instead
The ordinary epslatex output uses EPS, which pdfLaTeX does not read directly as a graphic in the usual setup. Use a DVI-based workflow, or convert the EPS companion file to PDF and ensure the generated TeX can resolve the PDF with the same base name. Another option is an EPS-to-PDF conversion setup for pdfLaTeX; its availability and shell-escape requirements depend on your TeX installation.
The local-path technique itself does not depend on the compiler. The graphics file still has to be in a format that the chosen compiler and graphics driver can use.
Common mistakes
- Setting
\graphicspathoutside a group: the changed search path can affect later graphics. Put it and the relevant\inputinside\begingroupand\endgroup. - Omitting the directory’s trailing slash: write
\graphicspath{{plots/}}, not\graphicspath{{plots}}. - Assuming
\graphicspathlocates the TeX file: it is for graphics inclusion. Give\inputits own correct path. - Using a path relative to the included file: resolve the graphics path from the main document’s working directory instead.
- Compiling EPS output directly with pdfLaTeX: use a compatible EPS workflow or provide a usable PDF graphic.
References
- The LaTeX Graphics Bundle
- Gnuplot documentation: `epslatex` terminal
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.