4

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

  • 1
    Have you tried setting \graphicspath{}? That might work. If it were me, I'd probably just use sym links. – cfr Jul 16 '15 at 1:08
  • @cfr: since I work from both Windows and Linux machines, I am reluctant about using symlinks. Even though modern versions of windows support them, git does not. (There is a comment from April this year, indicating it is still unsupported). As for the graphicspath I did not even know that it exists, so I will try that, however I would love the native feel of just amking all input and co commands relative to the current file. – ted Jul 16 '15 at 1:35
  • If I used Windows, I'd be reluctant to do anything ;). I'm just used to doing it so thought I'd mention it. I suspect the directory structure shown in the package documentation's example is significant so that if you want to use the feature, you probably need a similar structure. – cfr Jul 16 '15 at 1:47
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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