# Standalone in build mode forget command

I would like to know if it is possible to give a command that includestandalone would remember when building the pdf.

To explain a bit more, I like to put all my extra data like my standalone subfiles in a subfolder. I would like to do a plot with pgfplots from a CSV file itself in a subsubfolder (from the main). Therefore the structure is the following:

main.tex
Supplies/
|-standaloneplot.tex
|-Data/
|-file.csv


And the files are the file.csv:

# Coord_X, Coord_Y
0.0 , 1.0
1.0 , 2.0


the standaloneplot.tex:

\documentclass[]{standalone}
\providecommand{\PathS}{./}

\usepackage{tikz}                       %
\usepackage{pgfplots}                   %

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={Space },    %
ylabel={\PathS }
]

table[x=Coord_X, y=Coord_Y, ignore chars={\#}, col sep=comma]
{\PathS \detokenize{Data/file}.csv};

\end{axis}
\end{tikzpicture}
\end{document}


and the main.tex:

\documentclass{article}
\providecommand{\PathS}{./Supplies/}

\usepackage{standalone}             %
\usepackage{tikz}                   %
\usepackage{pgfplots}               %

\begin{document}
the path is \PathS
\begin{figure}
\includestandalone[mode=build, width=0.5\linewidth]{\PathS standaloneplot.tex}
\caption{test}
\label{lab}
\end{figure}
\end{document}


I deal with this structure by defining a command in the main and in the standalone file. And it works correctly when I include the standalone file in a tex mode. This is, I think, close to the solution suggested here. But when I use the build mode it forgets this command, which seems natural from my understanding of this mode.

Thus, I would like to know if it possible to work-around this problem and keep this structure and the use of standalone in build mode and pgfplots. Maybe there is a way not to forget a command before to build the standalone file or a way for standalone to act more like the tex mode.

Do you think it is possible?

If you use mode=build, the standalone file is compiled separately. Therefore, commands defined in the main file are unknown in the standalone file. But commands can be defined before a TeX file by calling the compiler like this:

pdflatex <some options> '\newcommand{\SomeMacro}{content}\input{texfile}'


Then \SomeMacro is defined in texfile.tex.

standalone offers the option build to setup the build command (see section 5.4 and table 3 of the manual). Here command sets the whole command (by default: build={command=\latex\space\latexoptions\space\file}). The last part (\file, containing the file name given to \includestandalone) has to be changed to define the macro before compiling the file. The necessary quotes are provided by \quote, so this is independent from the used OS. And some commands must be protected against expansion with \string. So by writing

build={command={\latex\space\latexoptions\space\quote\string\newcommand{\string\PathS}{\PathS}\string\input{\file}\quote}}


you can achieve your goal. In principle, you can define many commands this way by repeating \string\newcommand{\string...}{...} as often as needed. Note that you have to protect the command to be defined, but on the other hand, you can use \PathS instead of repeating the path.

The build option can be used with includestandalone or with \standaloneconfig{...} either somewhere in the document or in the preamble.

Remark: in the log file of main.tex near the end there is a line starting with runsystem( containing the command used to build the plot. This is useful to check, if the build option is set correctly.

Here is the new main.tex (other files are unchanged):

\documentclass{article}
\providecommand{\PathS}{./Supplies/}

\usepackage{standalone}             %
\usepackage{tikz}                   %
\usepackage{pgfplots}               %

% set option build in the preamble, good for global path
\standaloneconfig{build={command={\latex\space\latexoptions\space\quote\string\newcommand{\string\PathS}{\PathS}\string\input{\file}\quote}}}

\begin{document}
% set option build somewhere in the document, good if path changes every couple of figures
%\standaloneconfig{build={command={\latex\space\latexoptions\space\quote\string\newcommand{\string\PathS}{\PathS}\string\input{\file}\quote}}}
the path is \PathS
\begin{figure}
% use option build directly, good for individual paths
%\includestandalone[build={command={\latex\space\latexoptions\space\quote\string\newcommand{\string\PathS}{\PathS}\string\input{\file}\quote}},mode=build, width=0.5\linewidth]{\PathS standaloneplot}
\includestandalone[mode=build, width=0.5\linewidth]{\PathS standaloneplot}
\caption{test}
\label{lab}
\end{figure}
\end{document}