# Peculiar tikz-external, pgfplots, tikzscale combination problem

I want to use \tikzexternalize and tikzscale with pgfplots' \addplot graphics command and \clip=false option.
I am using three files: A.png, A.tikz and A.tex.
A.png can be any graphics file.
A.tikz contains the following code:

\begin{tikzpicture}
\begin{axis}[
width=0.5\textwidth,
xmin=0,xmax=5,ymin=0,ymax=5,
clip=false,
]
\node[anchor=south east] at (rel axis cs:-0.05,1) {(a)};
\end{axis}
\end{tikzpicture}


The content of A.tex is:

\documentclass[crop,10pt,border=0]{standalone}
\usepackage[english]{babel}
\usepackage{pgfplots}

\usepackage{tikzscale}

\usepgfplotslibrary{groupplots}
\usetikzlibrary{external}
\tikzexternalize

\pgfplotsset{compat=1.9}

\begin{document}
\tikzsetnextfilename{Figure-A}
\setlength{\textwidth}{510pt}
This is some text.
\includegraphics[width=0.5\textwidth]{A.tikz}
% \input{A.tikz}
\end{document}


Compiling the document like this, I get an error and the external file is not created. I found out, that the problem arises from the combination of of the following lines:
\tikzexternalize
\includegraphics[width=0.5\textwidth]{A.tikz}
clip=false
\addplot graphics [xmin=0,xmax=5,ymin=0,ymax=5] {A.png};
If I omit any one of these commands, there will be no error but obviously I cannot use the coresponding option either:
- If I omit \tikzexternalize, there is no error but also no external file and the tikz code is compiled in every run.
- If I omit clip=false, there is no error, an external file is created but I cannot draw anything outside but relative to the axis.
- If I omit \addplot graphics [xmin=0,xmax=5,ymin=0,ymax=5] {A.png}; e.g. by using the \addplot coordinates instead, I get no error, an external file is created but I cannot use external graphics
- If I omit \includegraphics[width=0.5\textwidth]{A.tikz} and use \input{A.tikz} instead, it works but the result is not scaled properly.
So my question is: Is there any way to make these four commands/options work together?

-
A.Tikz is not a graphics file. It expects an image not code. Also pgfplots has width and height options so you don't need tikzscale –  percusse Mar 28 '14 at 16:57
@percusse When you use tikzscale, it indeed does support code. And as stated in the question, it does work with the .tikz if any of the other three options is omitted. I do know there is width and height in pgfplots, but it is very time consuming to find the right parameters to fit the result to a certain scale, especially when using groupplots. tikzscale is very powerful in achieving this. –  Janek Mar 31 '14 at 12:25

You can get into the axis environment by using every axis/.append style and then you can comment out the clip=false option. I enclose an example and a preview of the result, please try it if it is working for you with all those group plots.

%! *latex mal-A.tex
%   with shell escape on
\documentclass[crop,10pt,border=0]{standalone}
\usepackage[english]{babel}
\usepackage{pgfplots}
\usepackage{mwe}
\usepackage{tikzscale}
\usepackage{filecontents}
\usepgfplotslibrary{groupplots}
\usetikzlibrary{external}
\tikzexternalize
\pgfplotsset{compat=1.9}

\begin{document}
\def\malkeyfile{mal-A.tikz}
\begin{filecontents*}{\malkeyfile}
\begin{tikzpicture}
\begin{axis}[
width=0.5\textwidth,
xmin=0, xmax=5, ymin=0, ymax=5,
%clip=false, % Now, there is no need for this option anymore.
every axis/.append style={after end axis/.code={\node[anchor=south east] at (rel axis cs:-0.05,1) {(a)};}},
]
\addplot graphics [xmin=2.5, xmax=5, ymin=0, ymax=5] {example-image.png}; % This picture is a part of the mwe package.
\addplot coordinates { (1,1) (2,3) (3,2) (4.5,4) };
\end{axis}
\end{tikzpicture}
\end{filecontents*}
\tikzsetnextfilename{Figure-mal-A}
\setlength{\textwidth}{510pt}
This is some text.
\includegraphics[width=0.5\textwidth]{\malkeyfile}
\end{document}


-
Thank you! I wasn't aware, that one had access to the axis cs from outside the axis environment. Now, without the clip=false option, the rest works just fine. Still interesting, that only the combination of the four options will produce the error... –  Janek Apr 4 '14 at 12:06