[Tex/LaTex] External graphics and tikz externalize won’t work together

external filespgfplotstikz-externaltikz-pgf

I am using the tikz library external a lot and with success – so far. Now, I need to import an external file (eps, png, pdf… I don't care), and add the axes. I know I can do this with \addplot graphics, but it only works in documents without externalize. If I use this library, the figure remains empty or produces some weird stuff. Here's an MWE:

\documentclass[a4paper,fontsize=10pt]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[optimize=false,prefix=pics/] 
\tikzset{external/force remake=false}
\usepackage{pgfplots}
\usepackage{graphicx}

\begin{document}

\begin{tikzpicture}
\begin{axis}[axis on top,title=Test]
\addplot graphics[xmin=0,xmax=1,ymin=0,ymax=1]
{testfile};
\end{axis}
\end{tikzpicture}

\end{document}

If I remove the three lines concerning externalization, it works fine, but not like this.

Is there any way I can use both in the same document or do I have to get the axes around my surf plots by compiling them in a separate document?

Edit: Just found out that dropping the option prefix=pics/ works. I don't know why. Plus it's not really an option because I have all my other externalized pics in this folder…

Best Answer

This problem occurs because the testfile.png is expected to be in the same folder as the .tex file. When you're using the external library with the prefix=pics/ option, that's where the .tex file is, so testfile.png is expected to be in the pics folder as well.

A couple of different ways of fixing this spring to mind:

  • Move the testfile.png to the pics folder.
  • Add \graphicspath{..} to the preamble to tell LaTeX that the images are one folder above the .tex file.
  • Use \addplot graphics ... {../testfile}; to specify that the image is in the folder above.
  • Use an absolute path when specifying the image file name.