How to save and restore the nodes between tikzpictures?
If I understand correctly, you want to use the relative positions of the saved nodes in your new picture. That is, each node should refer to a position in the new picture relative to the new origin.
Here's some code that saves all the data for a list of specified nodes which can then be restored at a later time in the document. It uses LaTeX3 stuff internally since it's more about programming and expansion and L3 makes that sooo much easier.
\documentclass{article}
%\url{http://tex.stackexchange.com/q/307356/86}
\usepackage{tikz}
\usepackage{xparse}
\ExplSyntaxOn
% We save our information in a ``property list'', which is L3's
% version of an associative array or dictionary. They keys will give
% the ability to store several groups of nodes and restore them at
% will.
\prop_new:N \g__sn_prop
% We'll need a token list for constructing the saved data.
\tl_new:N \l__sn_tmpa_tl
% This is the command that actually does the work. It constructs a
% token list which contains the code that will restore the node data
% when invoked. The two arguments are the name of this group (for
% reference later) and a comma separated list of the node names to be
% saved.
\cs_new_nopar:Npn \save_nodes:nn #1#2
{
% Clear our token list
\tl_clear:N \l__sn_tmpa_tl
% Iterate over the list of node names
\clist_map_inline:nn {#2}
{
% Before we start trying to save the node, check that it exists.
% The macro \pgf@sh@ns@nodename is only defined if that node exists.
\tl_if_exist:cT {pgf@sh@ns@##1}
{
% The node information is stored in a series of macros of the form
% \pgf@sh@XX@nodename where XX is one of the following.
\clist_map_inline:nn {ns,np,ma,nt,pi}
{
% Our token list will look like:
%
% \tl_set:cn {pgf@sh@XX@nodename} {<current contents of that macro>}
%
% This will restore \pgf@sh@XX@nodename to its current value
% when this list is invoked.
%
% This part puts the \tl_set:cn {pgf@sh@XX@nodename} in place
\tl_put_right:Nn \l__sn_tmpa_tl
{
\tl_set:cn {pgf@sh@####1@##1}
}
% Now we put the current contents in place. We're doing this in
% an expansive context to get at the contents. The \exp_not:v
% part takes the current value of \pgf@sh@XX@nodename and puts
% it in place, preventing further expansion.
\tl_put_right:Nx \l__sn_tmpa_tl {{\exp_not:v {pgf@sh@####1@##1}}}
}
}
}
% Once we've assembled our token list, we store it in the property
% list using the key we were given.
\prop_gput:NnV \g__sn_prop {#1} \l__sn_tmpa_tl
}
\cs_new_nopar:Npn \restore_nodes:n #1
{
% Restoring nodes is simple: look in the property list for the key
% and if it exists, invoke the macro stored there.
\prop_get:NnNT \g__sn_prop {#1} \l__sn_tmpa_tl
{
\tl_use:N \l__sn_tmpa_tl
}
}
% These two are wrappers around our internal commands.
%
% The first argument is the label for our group of nodes (so that we
% can refer to them later) and the second argument is a comma
% separated list of nodes to save.
\DeclareDocumentCommand \SaveNodes {m m}
{
\save_nodes:nn {#1}{#2}
}
% The argument to this is the label for our group of nodes to restore.
\DeclareDocumentCommand \RestoreNodes {m}
{
\restore_nodes:n {#1}
}
\ExplSyntaxOff
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (1,2);
\coordinate (b) at (1,3);
\coordinate (c) at (1,4);
% save
\SaveNodes{here}{a,b,c}
\end{tikzpicture}
\begin{tikzpicture}
\fill[black] (0,0) circle (1.5 pt);
\foreach \c in {a,...,c}{ \fill[red] (\c) circle (1.5 pt); }
\end{tikzpicture}
\begin{tikzpicture}
\coordinate (a) at (2,2);
\coordinate (b) at (2,3);
\coordinate (c) at (2,4);
\fill[black] (0,0) circle (1.5 pt);
\foreach \c in {a,...,c}{ \fill[blue] (\c) circle (1.5 pt); }
\end{tikzpicture}
%restore I would like to use the same nodes like at the beginning
\begin{tikzpicture}
\RestoreNodes{here}
\fill[black] (0,0) circle (1.5 pt);
\foreach \c in {a,...,c}{ \fill[red] (\c) circle (1.5 pt); } % with
\end{tikzpicture}
\end{document}
I added the black circles at the origin to show that it is the original nodes that are used, not the new ones. I think I'm saving all the information about the nodes, not just their coordinates, so this solution would work with general nodes not just coordinates. With a little more work, it would be possible to take a list of nodes at the restore stage and only restore those.
Here's a non-L3 version. It works on a per-node basis (but you could probably wrap it if the nodes always have the same names).
\makeatletter
\def\@savecoord#1#2{%
\expandafter\let\expandafter\sn@temp\csname pgf@sh@#1@#2\endcsname
\expandafter\def\expandafter\sn@tempb\expandafter{%
\expandafter\gdef\csname pgf@sh@#1@#2\expandafter\endcsname\expandafter{%
\sn@temp
}%
}%
\expandafter\g@addto@macro\expandafter\sn@tempa\expandafter{\sn@tempb}%
}
% {ns,np,ma,nt,pi}
\def\savecoordinate#1#2{%
\def\sn@tempa{}%
%
\@savecoord{ns}{#2}%
\@savecoord{np}{#2}%
\@savecoord{ma}{#2}%
\@savecoord{nt}{#2}%
\@savecoord{pi}{#2}%
%
\expandafter\global\expandafter\let\csname sn@#1\endcsname\sn@tempa
}
\def\restorecoordinate#1{%
\csname sn@#1\endcsname
}
\makeatother