How to Set a PGFPlots Search Path for Data Files
At a glance
- 1. Set the table path
Use PGFPlots’ table/search path key to tell table-reading commands where to look.
- 2. Keep the filename short
Put the data file in a listed directory, then refer to it by filename.
- 3. Check the working directory
Relative search paths are resolved from the directory where you run LaTeX.
- 4. Compile and verify
If the file is not found, check the folder name, path spelling, and compilation directory.
PGFPlots can search a directory for external table files, but \graphicspath is not the setting for that job. \graphicspath configures graphics file lookup, such as for \includegraphics. For data read by \addplot table or \pgfplotstableread, set PGFPlots’ table/search path key.
Set the table search path
Set the key with \pgfplotsset in your document preamble:
\pgfplotsset{table/search path={data/}}
With this setting, PGFPlots looks in the data/ directory when it searches for a table file. You can then write the filename without repeating the directory in each plot command:
\addplot table {measurements.dat};
The path is relative to the directory from which the LaTeX compiler is run. In many editors that is the project directory, but build settings can change it. Use forward slashes in paths, including when compiling on Windows.
Worked example
Arrange the files like this, with main.tex in the project directory:
project/
├── main.tex
└── data/
└── measurements.dat
Put these two columns of data in data/measurements.dat:
0 1
1 2
2 1.5
3 3
This sample is whitespace-separated and has no header, so specify those details in the plot options. Then use this complete document in main.tex:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{
compat=1.18,
table/search path={data/}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$}
]
\addplot table [col sep=space, header=false, x index=0, y index=1] {measurements.dat};
\end{axis}
\end{tikzpicture}
\end{document}
Compile main.tex from the project/ directory. PGFPlots searches the configured data/ directory for measurements.dat, reads the two columns as coordinates, and draws the plot. The compat setting selects a PGFPlots compatibility level; it is independent of file lookup.
If you would rather make the file location explicit at the point of use, write \addplot table {data/measurements.dat}; and omit the search-path setting. That approach is often simplest for one or two files. A search path is useful when many table files share a directory.
Using a table-reading command directly
The same search path is useful when loading a table into a named macro before plotting it:
\pgfplotstableread[col sep=space, header=false]{measurements.dat}\mytable
With table/search path={data/} set, this can read data/measurements.dat. You can then plot the loaded table, for example with \addplot table [x index=0, y index=1] {\mytable};.
Common mistakes
- Using
\graphicspathfor table files. It affects graphics lookup, not PGFPlots table lookup. Usetable/search pathfor table data. - Assuming paths start beside the
.texfile. Relative paths are generally interpreted from the compiler’s working directory. If an editor builds from a different directory, adjust the path or build configuration. - Including the folder twice. With
table/search path={data/}, use{measurements.dat}as the table filename, not{data/measurements.dat}. The explicit path may still work, but it defeats the point of keeping the directory in one setting and can be confusing. - Putting a trailing slash nowhere. Writing
{data/}makes the intended directory clear and avoids relying on path-joining behavior. - Confusing table input with
\input. PGFPlots’ table search setting is intended for table files. It does not replace LaTeX’s input-file search settings for ordinary\inputcommands.
If PGFPlots still reports that the file cannot be opened, first confirm that the file exists at the expected path relative to the build directory, then check the filename’s spelling and capitalization.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.