[Tex/LaTex] Outsourcing Tikz and relative paths

currfileinputtikz-pgftikzscale

Summary:

How do I get includegraphics to be relative to the current file, when using tikzscale?


Details

Similar to pmav I like to have a .tikz file which contains only the tikzpicture environment. (Main reason beeing, that I am to lazy to wrap all my figures in a full preamble like the standalone class would mandate, and that this is what tikzscale needs as input).

Since I like to use my pictures in different documents my latex folder looks something like this:

pictures
-TEST.tex %contains documentclass and loads tikzpreamble,
          % modified by hand to input the tikz file currently under development
-tikzpreamble.tex
-picture1.tikz
-picture2.tikz
documentA
-documentA.tex
-...
documentB
...

While this flow works without fault for pure tikzpictures, I lately had a tikzpicture using \includegraphics. While it was no problem when I worked from inside the pictures folder (TEST.tex), I soon hit the issue that the input path is relative to the main tex file, rather than the current file.

A quick google search led me to the import package with subimport. While this allows me to load the tikzpicture in a way that the includegraphics inside the tikzpicture works, it does not support tikzscale.

I thought I got lucky when I read the following in the tikzscale documentation, in regard to the problem:

If both tikzscale and currfile are loaded, the limitation is fixed, so
that both [includegraphics relative to main file AND relative to current file] \includegraphics commands succeed.

However, for me it does not work. I.e. the following results in the png not beeing found:

Filetree:

pictures/tikzpicture.tikz
pictures/OtherSources/png.png
document1/doc1.tex

doc1.tex:

\documentclass{report}
\usepackage{tikz}
\usepackage{currfile}
\usepackage{tikzscale}
\begin{document}
    \input{../pictures/tikzpicture.tikz}
\end{document}

tikzpicture.tikz

\begin{tikzpicture}
\includegraphics{OtherSources/png.png}
\end{tikzpicture}

=> Error message:

Package pdftex.def Error: File `OtherSources/png.png' not
found. …ng.png}

Related: Outsourcing TikZ code

Best Answer

In your example you don't need tikzscale, but if you need it in order to import tikz images with \includegraphics I see two solutions.

One is to patch the \includegraphics using etoolbox to make it compatible with import package. But I'm not competent enough to show you how to do this.

The other one is to use a personal macro myinput to specify the path, and then use this path in the subdocument. Here is your MWE :

doc1.tex:

\documentclass{report}
\usepackage{tikz}
\usepackage{currfile}
\usepackage{tikzscale} % not needed in this example
\newcommand{\myinput}[2]{%
  \edef\mypath{#1}
  \input{#1#2}
}
\begin{document}
    \myinput{../pictures/}{tikzpicture.tikz}
\end{document}

tikzpicture.tikz:

\begin{tikzpicture}
  \node {\includegraphics{\mypath OtherSources/png.png}};
\end{tikzpicture}

Note: I'm surprised that you include png inside tikzpicture directly. I modified this by including the image inside a node.