1

I have a large number of pgfplots generated figures that I would like to include in my main document from their location in a figures folder.

The figures and corresponding data are stored in folders of the structure:

figure
├───data
|     └───data.csv
└───plot
      └───figure.txt

with

% figure.tex

\begin{tikzpicture}
\begin{axis}[]
   \addplot [] table [...] {../data/data.csv};
\end{axis}[]
\end{tikzpicture}

Is there any way to include the figures in the main document main.tex?

parent
├───main.tex
└───figures
    └───figure-1
        ├───data
        |   └───data.csv
        └───plot
            └───figure.txt

If I use a the subimportcommand from the importpackage, the relative path in the figure.texsubfile leads to errors.

I am aware of the discussion import input pgfplots with relative paths, but was unable to implement the suggestion by Christian Feuersänger.

Wasserwaage
  • 233
  • 3
  • 12

1 Answers1

1

Too long for a comment.


I am not 100% sure if I have understood your question right, but here is an example of how I usually structure my stuff and that works fine. That is

  • store PGFPlots diagrams in the folder plots
  • store data used in PGFPlots diagrams in the folder plots/data
    (regardless of the source (external or internal from e.g. gnuplot))
  • store figures used with \includegraphics are stored in the folder Pics (or a subfolder of Pics)
  • store externalized images from TikZ and PGFPlots in folder Pics/pgf-export

As long as you want to write something to a folder on the same level as \jobname.tex or lower, everything will work fine (without applying any tricks).

% used PGFPlots v1.16
\begin{filecontents*}{plots/data/WaterDensity.txt}
    % density of liquid water at 1 atm
    % (from <https://en.wikipedia.org/w/index.php?title=Density&oldid=935683038#Water>)
    % °C    kg/m3
    T   rho
    0   999.8395
    4   999.9720
    10  999.7026
    15  999.1026
    20  998.2071
    22  997.7735
    25  997.0479
    30  995.6502
    40  992.2
    60  983.2
    80  971.8
    100 958.4
\end{filecontents*}
\begin{filecontents*}{plots/WaterDensity}
\tikzsetnextfilename{WaterDensity}
\begin{tikzpicture}
    \begin{axis}[
        xlabel={$T$ / \si{\celsius}},
        ylabel={$\rho$ / \si{\kg\per\cubic\metre}},
        smooth,
    ]
        \addplot+ [only marks] table [x=T,y=rho] {plots/data/WaterDensity.txt};

        % equation (6) from <https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4909168/>
        \addplot+ [
            domain=0:40,
            no markers,
        ] gnuplot [id=WaterDensity] {
            999.84847
            + 6.337563e-2 * x
            - 8.523829e-3 * x^2
            + 6.943248e-5 * x^3
            - 3.821216e-7 * x^4
        };

            \pgfplotsset{cycle list shift=-1}
        % equation (6) from <https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4909168/>
        \addplot+ [
            domain=40:100,
            no markers,
            dashed,
        ] gnuplot [id=WaterDensityExtrapolated] {
            999.84847
            + 6.337563e-2 * x
            - 8.523829e-3 * x^2
            + 6.943248e-5 * x^3
            - 3.821216e-7 * x^4
        };
    \end{axis}
\end{tikzpicture}
\end{filecontents*}
\documentclass{article}
\usepackage{siunitx}
    \DeclareSIUnit{\atm}{atm}
\usepackage{pgfplots}
    \usetikzlibrary{
        external,
    }
    \pgfplotsset{
        compat=1.16,
        /tikz/font=\small,
        /tikz/prefix=plots/data/,
    }
    \tikzexternalize[
        prefix=Pics/pgf-export/,
        only named=true,        % prevent externalization of none named figures/plots
%        force remake,           % force all tikz images to be externalized (again)
    ]
\begin{document}
\begin{figure}
        \centering
    \input{plots/WaterDensity}
    \caption{Density of water at \SI{1}{\atm}}
        \label{plot:WaterDensity}
\end{figure}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535