# Tikz overlay with subfig, axis & externalize

I am trying to draw lines between two different tikz-drawings, placed in subfig-environments, for example the corners of the small blue rectangle in the left image and the corners of the axis environment of the right image.

As the two figures will become very large, I need to export them with externalize. Therefore I'd also like to use the subfig library and not groupplots.

I thought it would work with the remember picture and the overlay keywords, but first attempts only brought errors, so I fear there might be a conflict with remember picture and externalize. I already found a good description how to reference corners of a rectangle.

Is it possible to draw lines as indicated in the sample picture? Is it possible in combination of externalize and subfig (if not I would prefer groupplots and externalize before subfig and no externalize, though the overlay image don't need to be 'externalized')?

Here is my minimum example:

\documentclass[parskip]{scrartcl}
\usepackage{tikz,pgfplots,subfig, lipsum}
% \usetikzlibrary{external}
% \tikzexternalize[figure list=true]
\begin{document}

\lipsum[1]
\begin{figure}[h]
\centering
\subfloat[Overview.]{
\begin{tikzpicture}[trim axis left, trim axis right]
\begin{axis}[domain=-10:10,x post scale=0.5]
\draw[fill=blue,opacity=0.3] (axis cs:-2,-0.5) rectangle (axis cs:2,5);
\end{axis}
\end{tikzpicture}
\subfloat[Detail.]{
\begin{tikzpicture}[trim axis left, trim axis right]
\begin{axis}[xmin=-2,xmax=2,ymin=-0.5,ymax=5,domain=-2:2,mark=none]
\end{axis}
\end{tikzpicture}
}
\caption{Curve.}
\end{figure}
\lipsum[2]

\end{document}


Thanks a lot!

• I think you are looking for this : tex.stackexchange.com/questions/42585/… – percusse Aug 19 '12 at 11:29
• As far as I understood they are creating one image with another image within. If there is no solution for my question I try to add a small overview in my detailed plot, but I'd rather place them side by side and compute them independently. In the end the two pictures look like this: i.imgur.com/tCoIj.png – kromuchi Aug 19 '12 at 11:54
• You could try to put both plots in the same tikzpicture environment. That should work with your overlays. Only your 'subfloat'-caption will not work as easy as it does now. But you could maybe use a 'hphantom'-box to get it right? – Matz Aug 19 '12 at 12:04
• Is it true that it's not possible to use remember picture with two externalized pictures or can it still work somehow? As a work-around your proposition @Matz could work, I'll try it later if there's no different approach. – kromuchi Aug 19 '12 at 12:55
• @kromuchi Externalization makes standalone .pdf images of the graphics so that you can add them as external files. What you want is to make images standalone but still draw over them which still requires yet another tikzpicture. Instead of group plots you can use two axis environments in one picture. Scope the second and shift it to the right. – percusse Aug 19 '12 at 13:36

For the sake of completeness I present a short version of the groupplots-approach with faked sub-captions and possible externalization. There are different approaches (see comments under question), but this seemed the easiest for me. However, for the original grafic I realized that additional lines only make everything messy...

And here the code:

\documentclass[parskip]{scrartcl}
\usepackage{tikz,pgfplots,subfig, lipsum}
\usetikzlibrary{pgfplots.groupplots}
\usetikzlibrary{fit,intersections,}
\makeatletter
\tikzset{ % fitting node, see http://goo.gl/KOvpQ
fitting node/.style={
inner sep=0pt,
fill=none,
draw=none,
reset transform,
fit={(\pgf@pathminx,\pgf@pathminy) (\pgf@pathmaxx,\pgf@pathmaxy)}
},
reset transform/.code={\pgftransformreset}
}
\makeatother
% \usetikzlibrary{external}
% \tikzexternalize[figure list=true]
\begin{document}

\lipsum[1]
\begin{figure}[h]
\centering
\begin{tikzpicture}
\begin{groupplot}[domain=-10:10,group style={group size=2 by 1,vertical sep=0.7cm,horizontal sep=1.05cm,xlabels at=edge bottom,ylabels at=edge left},x label style={font=\footnotesize}]
\nextgroupplot[x post scale=0.5,xlabel={(a) Overview.},]
\draw[fill=blue,opacity=0.3] (axis cs:-2,-0.5) rectangle (axis cs:2,5) node[fitting node] (rect) {};
\nextgroupplot[xmin=-2,xmax=2,ymin=-0.5,ymax=5,domain=-2:2,mark=none,x post scale=0.9, xlabel={(b) Detail.}]
\end{groupplot}

\draw[dashed] (rect.north west) -- (group c2r1.north west);
\draw[dashed] (rect.south west) -- (group c2r1.south west);
\path[name path=lineA] (rect.south east) -- (group c2r1.south east);
\path[name path=lineB] (rect.north east) -- (group c2r1.north east);
\path[name path=lineC] (group c2r1.north west) -- (group c2r1.south west);

\draw [dashed,name intersections={of=lineA and lineC, by=pA}] (rect.south east) -- (pA);
\draw [dashed,name intersections={of=lineB and lineC, by=pB}] (rect.north east) -- (pB);
\draw [dashed,gray!90,opacity=0.7] (pA) -- (group c2r1.south east);
\draw [dashed,gray!90,opacity=0.7] (pB) -- (group c2r1.north east);

\end{tikzpicture}
\end{figure}

\lipsum[2]
\end{document}

• plus there is a small 3D-effect... – kromuchi Sep 1 '12 at 21:31