I have a basic tikzpicture I want to re-use in other tikzpictures and be able to use the coordinates and paths defined within it.
For instance, I have a geometric figure used in a problem and the same geometric figure with labels used in a solution. If I put a tikzpicture inside a \newcommand
, I can insert it in another tikzpicture, but the coordinates are messed up. In simple cases I can redefine them, but with complex figures I would like to be able to refer to the coordinates already defined from another tikzpicture.
In the template below the red node A in the second picture was created from the coordinates defined in the first picture: I would like it to be exactly on top of the lower-left, blue node A.
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
% basic figure to be reused
\newcommand{\basic}{%
\begin{tikzpicture}[remember picture]% doesn't help
\coordinate (A) at (0,0);
\coordinate (B) at (1,1);
\node [blue,below] at (A) {A};
\draw [blue] (A) -- (B);
\end{tikzpicture}
}%
\begin{tabular}{@{}c@{}}
\begin{tikzpicture}
\node at (0,0) (pic1) {\basic};
\end{tikzpicture}
\vspace{1cm} \\
\begin{tikzpicture}
\node at (0,0) (pic2) {\basic};
\node [red,below] at (A) {A};
\end{tikzpicture}
\end{tabular}
\end{document}
Perhaps I can automate a coordinate adjustment in the second picture? Or maybe there is a better approach altogether. I tried to use the remember picture
option, but didn't quite know how to.
I saw this question but Peter Grill, in his answer, writes "I am not recommending nesting tikzpicture environments." So I wonder what a better solution would be. There is also that question, but in my attempts to use a scope
with a yshift
, the coordinates were also shifted. Or did I miss something?