19

How can I get the current path inside an included standalone file?

1. Theoretical Example:

let's assume the following directory structure:

main.tex
img/
 |-standalonefile.tex
 |-data

standalonefile.tex:

\documentclass{standalone}

\begin{document}
    \somecommand{data}
\end{document}

main.tex:

\documentclass{article}

\usepackage{standalone}

\begin{document}
    \includestandalone{img/standalonefile}
\end{document}

Now standalonefile.tex compiles, but main.tex can't find data. I couldn't find a way to use path from \includestandalone somehow inside standalonefile.tex.

2. Practical example with pgfplots (MWE) (I'm looking for a general solution)

directory structure:

main.tex
plots/
 |- plot1/
     |- plot1.tex
     |- data.csv

main.tex

\documentclass{article}

\usepackage[subpreambles]{standalone}

\begin{document}
    \includestandalone{plots/plot1/plot1}
\end{document}

plot1.tex

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}
        \addplot table [x=x, y=y, col sep=semicolon]{data.csv};
    \end{axis}
    \end{tikzpicture}

\end{document}

data.csv

x;y
0;0
1;1

Now plot1.tex compiles fine, but with main.tex the data.csv can't be found.

Ideas for a solution

  • a command like \mystandalonedir that would return
    • the relative path to the main file, if included with \includestandalone
    • nothing, if not included
  • change the base dir somehow with the \includestandalone
  • (or something else ;) )
4
  • 1
    related: tex.stackexchange.com/questions/4602/…
    – jub0bs
    Dec 1, 2013 at 19:59
  • Oh, I see closing votes ;) If I read the suggested page correctly the solutions over there are only for \input an \include commands. But I'll take another look at it.
    – someonr
    Dec 1, 2013 at 20:49
  • I don't think it's a duplicate. A solution to your problem would involve some patching of \includestandalone. If your question gets closed, I'll vote to reopen it. Martin Scharrer is the author of the standalone package and a moderator on the site. If he sees your question, I'm sure he'll come up with some solution.
    – jub0bs
    Dec 1, 2013 at 20:50
  • 1
    With docmute+import packages in the main file you can use \subimport{img/}{standalonefile}. Note that in this case the preamble of the nested file is lost (among another problems like references) but you can \input a common preamble in both files.
    – Fran
    Dec 4, 2013 at 11:56

2 Answers 2

13
+50

My answer is possibly not a very elegant one, but it's roughly the way I handle this problem with my own files. The basic idea is to set up a path macro \datapath.

For your given practical example, I suggest the following:

main.tex

\documentclass{article}

\usepackage[subpreambles]{standalone}

\newcommand{\includestandalonewithpath}[3][]{%
  \begingroup%
  \newcommand{\datapath}{#2}%
  \includestandalone[#1]{\datapath/#3}%
  \endgroup}

\begin{document}
  \includestandalonewithpath{plots/plot1}{plot1}
\end{document}

plot1.tex

\providecommand{\datapath}{.}
\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}
        \addplot table [x=x, y=y, col sep=semicolon]{\datapath/data.csv};
    \end{axis}
    \end{tikzpicture}
\end{document}

This solution will also work, if your data file is in a subdirectory relative to plot1.tex, e.g.

plots/plot1/data/data.csv

Then, you would have:

\addplot table [x=x, y=y, col sep=semicolon]{\datapath/data/data.csv};
1
  • thank you! I just awarded the bounty to you and extended your solution with some string splitting.
    – someonr
    Dec 23, 2013 at 12:14
9

I just combined Thomas F. Sturm answer with automatic string splitting. This way \includestandalonewithpath can be used the same way as \includestandalone.

main.tex

\documentclass{article}

\usepackage[subpreambles]{standalone}
\usepackage{xstring}

\newcommand{\includestandalonewithpath}[2][]{%
    \begingroup%
    \StrCount{#2}{/}[\matches]%
    \StrBefore[\matches]{#2}{/}[\datapath]%
    \includestandalone[#1]{#2}%
    \endgroup%
}

\begin{document}
    \includestandalonewithpath{plots/plot1/plot1}
\end{document}

plot1.tex

\documentclass{standalone}

\providecommand{\datapath}{.}
\usepackage{pgfplots}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}
        \addplot table [x=x, y=y, col sep=semicolon]{\datapath/data.csv};
    \end{axis}
    \end{tikzpicture}

\end{document}
1
  • This solution sadly does not seem to work for me. The \datapath macro seemingly is not transferred to the standalone. Has anything changed in the past ten 10 years?
    – LeonKro
    Jan 11 at 17:29

You must log in to answer this question.

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