1

I want to create a zoom view on a figure. Since the zoomed part will not only contain the zoomed region but other elements too, I don't use the spy library. Here is a MWE. The nodes defined in the axis environments are not recognized outside them. That causes the error. How can I make them global?

\documentclass[12pt]{standalone}

\usepackage{pgfplots}

\tikzset{every picture/.append style={remember picture}}

\begin{document}

\begin{tikzpicture}

\begin{axis}[%
width=4cm,
height=4cm,
scale only axis,
xmin=0,
xmax=150,
ymin=0,
ymax=150,
axis x line*=bottom,
axis y line*=left
]

% Draw the zoom box
\draw [draw=red, thick] (axis cs: 133,100) rectangle (axis cs:149,114);
% Define nodes, being the first end points of two lines
\node (topleftAxis1) at (axis cs: 133,114) {};
\node(bottomleftAxis1) at (axis cs: 133,100) {};
\end{axis}


\begin{axis}[%
width=4cm,
height=4cm,
at={(6cm,0cm)},
scale only axis,
xmin=133,
xmax=149,
ymin=100,
ymax=114,
]
% Define nodes, being the second end points of two lines
\node (topleftAxis2) at (axis cs: 133,114) {};
\node (bottomleftAxis2) at (axis cs: 133,100) {};
\end{axis}

% Draw the lines (error happens here)
\draw [draw=red, thick] (topLeftAxis1) -- (topLeftAxis2);
\draw [draw=red, thick] (bottomLeftAxis1) -- (bottomLeftAxis2);

\end{tikzpicture}

\end{document}

EDIT

It turns out from the comments below the post that the behaviour was caused by a typo. By the way, using coordinate instead of node solves the issue of the lines not emanating from the nodes. So the definitions in the first and second axis environments should be

\coordinate (topLeftAxis1) at (axis cs: 133,114);
\coordinate (bottomLeftAxis1) at (axis cs: 133,100);

and

\coordinate (topLeftAxis2) at (axis cs: 133,114);
\coordinate (bottomLeftAxis2) at (axis cs: 133,100);

respectively.

  • Nodes are always global. You only change the spelling: the l in left is uppercase when you refer to the nodes and lowercase when you define them. Use \path (axis cs: 133,114) coordinate (topLeftAxis1) (axis cs: 133,100) coordinate (bottomLeftAxis1); and \path (axis cs: 133,114) coordinate (topLeftAxis2) (axis cs: 133,100) coordinate (bottomLeftAxis2) ; for the upper and lower coordinates. – user194703 Sep 6 '19 at 14:08
  • Thanks, stupid mistake. However, the lines do not start and end at the defined nodes, but slightly further. – Zoltán Csáti Sep 6 '19 at 14:14
  • Not if you use coordinate instead of node, as in my above comment. A node has a finite size (by default), which is why it is a bit off. – user194703 Sep 6 '19 at 14:16
  • Works perfectly. Thank you for the quick help. I guess, this stupid question can be deleted. What do you think? – Zoltán Csáti Sep 6 '19 at 14:20
  • This is up to you. I just think that "The nodes defined in the axis environments are not recognized outside them." is not entirely correct, and hence the question how to make nodes outside the axis "global" does not arise. BTW, use \tikzset{every picture/.append style={remember picture}} instead of \tikzstyle{every picture}+=[remember picture]. To me this question looks like "solved by fixing a typo". – user194703 Sep 6 '19 at 14:27

Your Answer

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

Browse other questions tagged or ask your own question.