6

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.

3
  • 1
    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. Nov 28, 2018 at 11:00
  • 1
    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, 2018 at 13:00
  • @MartinScharrer Hello Martin, has this issue been resolved recently?
    – Diaa
    Jan 5, 2020 at 16:38

1 Answer 1

1

I used the currfile package to solve this problem. You can use \currfiledir in your sub.tex to construct an absolute path.

figures/sub.tex:

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

Make sure to also import currfile in your main.tex.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .