Is there a way to use a folder structure with subfiles and \pgfplotstableread?

I have the following structure:

main.tex
\documentclass{article}
\usepackage{pgfplots}
\usepackage{subfiles}
\begin{document}
\subfile{chapters/chapter1.tex}
\end{document}


and the second file:

chapter1.tex
\documentclass[../main.tex]{subfiles}
\begin{document}
\begin{tikzpicture}
...some plot
\end{tikzpicture}
\end{document}


Now <PATH> can either be set so that main.tex works or chapter1.tex but not both because the are in different directories. Is there a way to use this feature without giving up folder structures?

• So the main problem is to go up in the folder tree and enter another subfolder for the data right? Does it have to be subfiles solution? – percusse Jul 16 '13 at 13:35
• The main problem is to be aware whether the subfile is compiled by itself or as an include for the main file. The <PATH> has to be different for both alternatives. – papabravo Jul 16 '13 at 13:37
• Maybe then you should nest the tikzpicture too as a fixed path so it doesn't matter whether main or chapter is called it. Otherwise you need to check each time who is pulling the strings. – percusse Jul 16 '13 at 13:39
• can you expand on that. Do you mean that I should move the file or can I do something with tikz there? – papabravo Jul 16 '13 at 13:44

I solved that problem by switching to standalone because it has the macro \onlyifstandalone. The following setup works:

main.tex
\documentclass{article}
\usepackage{standalone}
\usepackage{pgfplots}
\newcommand{\fromRoot}[1]{./#1}
\begin{document}
\subfile{chapters/chapter1.tex}
\end{document}


and the second file:

chapter1.tex
\documentclass{subfiles}
\usepackage{tikz,pgfplots}
\onlyifstandalone{\newcommand{\fromRoot}[1]{../../#1}}
\begin{document}
\begin{tikzpicture}
...some plot
\end{tikzpicture}
\end{document}


Now <PATH> is the relative path from the root directory of the project.

As the standalone package is not the perfect solution, I found a better way with subfiles

main.tex
\documentclass{article}
\usepackage{subfiles}
\usepackage{pgfplots}
\begin{document}
\newcommand{\fromRoot}[1]{./#1}%will not be read by subfiles
\subfile{chapters/chapter1.tex}
\end{document}


and the second file:

chapter1.tex
\documentclass[../main.tex]{subfiles}

\makeatletter
\@ifundefined{fromRoot}{\newcommand{\fromRoot}[1]{../#1}}{}
\makeatother
\begin{document}
\begin{tikzpicture}
...some plot
\end{tikzpicture}
\end{document}


Now everthing works as intended.

I knew this is an ancient question but i have an other convinient way based on answer of OP so that we don't have to retype the same command definition in every subfiles using pgfplotstable.

Supposing the folder structure like this :

root
--main.tex
++sections
--subfile.tex
++res
--table.csv


In main.tex

\newcommand{\getResources}[1]{../res/#1}%relative to sections folder

\begin{document}
\renewcommand{\getResources}[1]{./res/#1}%relative to root folder
\end{document}


In subfile.tex, call your csv file with getResources

\pgfplotstabletypeset[...]{\getResources{table.csv}}