Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
\pgfplotstableread{<PATH>}\loadedtable
   ...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?

share|improve this question
    
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
add comment

2 Answers

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}
\pgfplotstableread{\fromRoot{<PATH>}}\loadedtable
   ...some plot
\end{tikzpicture}
\end{document}

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

share|improve this answer
add comment
up vote 1 down vote accepted

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}
\pgfplotstableread{\fromRoot{<PATH>}}\loadedtable
   ...some plot
\end{tikzpicture}
\end{document}

Now everthing works as intended.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.