# Include standalone files in subdirectories which read other files

I want to use package standalone together with PGFPlots and TikZ to generate figures. In particular, I want figures to be generated only if missing, using buildmissing package option for standalone when building the main document, but at the same time I want to generate them separately if I am working on them. However, I cannot seem to make it work if the figures are in a subdirectory.

./
├── figures/
│   ├── sub.csv
│   └── sub.tex
├── main.tex
├── same.csv
└── same.tex


The main document is main.tex:

\documentclass{article}
\usepackage[mode=buildmissing]{standalone}
\begin{document}
Same directory:
\includestandalone{same}

Sub directory:
\includestandalone{figures/sub}
\end{document}


The standalone figure in the same directory is same.tex:

\documentclass[tikz,crop]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+ table [col sep=comma, x=x, y=y] {same.csv};
\end{axis}
\end{tikzpicture}
\end{document}


The standalone figure in the same directory reads CSV data from same.csv:

x,y
1,2
3,4
5,6
7,8
9,0


The standalone figure in the subdirectory is figures/sub.tex:

\documentclass[tikz,crop]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+ table [col sep=comma, x=x, y=y] {sub.csv};
\end{axis}
\end{tikzpicture}
\end{document}


The standalone figure in the subdirectory reads CSV data from figures/sub.csv:

x,y
1,-2
3,-4
5,-6
7,-8
9,-0


If I compile manually the figures everything works fine and PDF files are generated. If I then compile the main document, those PDF files are included.

However, if I compile the main document before manually compiling the figures, only the first one is generate correctly. The second one generates an empty plot, because it cannot access file sub.csv. From what I understand, relative paths are always considered relative to the initial document, but in this case the initial document is in two different directories, depending if I compile the main document or the standalone figure.

I used PGFPlots as example, but this issue also arise if I want to include any other file with \input{} or \include{}. For instance, I sometimes have a common preamble containing acronyms and fonts I want to use in both main document and figures, and I cannot include it from both files and have it working.

• Hi Claudio, I'm the author of standalone. The problem here is indeed that the paths are different and the original files are not found. At the moment the bundle does not provide a solution for this out-of-the-box. If I find out how to handle this internal I might add a feature for this in future versions. You could work around this issue, e.g. by writing a suitable Makefile (gnu make) for your project which handles the generation of your subfiles. – Martin Scharrer Nov 28 '18 at 11:00
• Hi Martin, thanks for your reply. I ended up writing a tiny wrapper script that cds in the figure's directory and builds it, and configure standalone with \standaloneconfig{mode = buildmissing, build = {latex = lualatex, command = {./buildfigure.sh \latex \space \file}}}. This way I can Ctrl+B from my editor to build the standalone figure, or build the main document together with all figures. – Claudio Dec 1 '18 at 13:00