3

In the following code

\documentclass{article}
\thispagestyle{empty}
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
\begin{figure}
  \begin{tikzpicture}
    \node(inset) {
      \begin{tikzpicture}
        \coordinate(v) at (3, 4);
        \node(t) at (v){v};
        \draw[->] (1,2) -- (v);
        \end{tikzpicture}
    };
    \node(t2)[blue] at (v){v};
    \draw[->, red](5, -2) -- (v);
  \end{tikzpicture}
\end{figure}
\end{document}

I try to draw an arrow to a specific point corresponding to \coordinate "v" in a nested \tikzpicture.

From the result it appears that \coordinate "v" has walked.

enter image description here

How to fix?

Viesturs
  • 7,895

3 Answers3

7

Instead of nesting tikzpictures, use a local bounding box.

\documentclass{article}
\thispagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{scope}[local bounding box=inset]
        \coordinate(v) at (3, 4);
        \node(t) at (v){v};
        \draw[->] (1,2) -- (v);
\end{scope}
  \node(t2)[blue] at (v){v};
  \draw[->, red](5, -2) -- (v);

\draw [red] (inset.east) -- (inset.west);
\end{tikzpicture}
\end{figure}
\end{document}
Torbjørn T.
  • 206,688
3

You can use saveboxes to safely nest tikzpictures, and the coordinates will be remembered. The way [remember picture] works is to save the origin location in the aux file, so you have to run the code twice.

It should be noted that by placing the picture inside a node you are centering it (default) at the node location (origin).

\documentclass{article}
\thispagestyle{empty}
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
\begin{figure}
\sbox0{\begin{tikzpicture}[remember picture]
        \coordinate(v) at (3, 4);
        \node(t) at (v){v};
        \draw[->] (1,2) -- (v);
        \end{tikzpicture}
}
  \begin{tikzpicture}[remember picture]
    \node(inset) {\usebox0};
    \node(t2)[blue] at (v){v};
    \draw[->, red](5, -2) -- (v);
  \end{tikzpicture}
\end{figure}
\end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
1

Try \tikzstyle{every picture}+=[remember picture]

\documentclass{article}
\thispagestyle{empty}
\usepackage{tikz}

\begin{document}
  \tikzstyle{every picture}+=[remember picture]
  \begin{tikzpicture}
    \node(inset) {
      \begin{tikzpicture}[remember picture]
       \coordinate(v) at (3, 4);
       \node(t) at (v){v};
       \draw[->] (1,2) -- (v);
      \end{tikzpicture}
    };
    \node(t2)[blue] at (v){v};
    \draw[->, red](5, -2) -- (v);
  \end{tikzpicture}
\end{document}
fatts
  • 157