4

I want to be able to refer to nodes inside a scope from outside the scope by specifying their scope and name as in TiKZ node name prefixes in scopes The solution proposed there renames the nodes so that it's not possible to refer to the nodes inside of the scope without knowing in which scope they're in.

The code below is what I'm trying to achieve, but I'm unable to figure out how to add an alias inside the name/.code argument. My attempt of pasting the alias/.code text from the tikz source didn't work and broke the naming as well.

\documentclass{article}
%\url{https://tex.stackexchange.com/q/128049/86}

\usepackage{tikz}

\begin{document}

\tikzstyle{vertex}=[circle,draw,fill=black!20]

\makeatletter
\tikzset{%
  prefix node name/.code={%
    \tikzset{%
      name/.code={{\edef\tikz@fig@name{##1}}} %don't know how to create alias from ##1 (node name) to #1 ##1 (scope name node name)
    }%
  }%
}
\makeatother

\begin{tikzpicture}

% ---- Copy 1
\begin{scope}[yshift=-32pt,prefix node name=G1]
  \node[vertex] (u) at (0, 0) {u};
  \node[vertex] (v) at (1, 0) {v};
  \draw (u) -- (v);
\end{scope}

% ---- Copy 2
\begin{scope}[yshift=32pt,prefix node name=G2]
  \node[vertex] (u) at (0, 0) {u};
  \node[vertex] (v) at (1, 0) {v};
  \draw (u) -- (v);
\end{scope}

\draw (G1 u) -- (G2 v);

\end{tikzpicture}

\end{document}
Lukas
  • 43
  • Could you consider using pics instead of scopes? Every pic can have its name asprefix for inner nodes. An example: http://tex.stackexchange.com/a/170686/1952 – Ignasi Oct 16 '14 at 17:54
  • I've added an example with pics to the linked question: http://tex.stackexchange.com/a/207513/1952 – Ignasi Oct 16 '14 at 18:11

2 Answers2

4

As the comments to the OPs question indicate, in the latest PGF release node names can be prefixed within a pic. However, this mechanism can also be used in scopes. Note that in order to get the space the OP requires the prefix must end with \space.

\documentclass[tikz,border=5]{standalone}
\begin{document}

\begin{tikzpicture}[vertex/.style={circle, draw, fill=black!20}]

% ---- Copy 1
\begin{scope}[yshift=-32pt, name prefix=G1\space]
  \node[vertex] (u) at (0, 0) {u};
  \node[vertex] (v) at (1, 0) {v};
  \draw (u) -- (v);
\end{scope}

% ---- Copy 2
\begin{scope}[yshift=32pt, name prefix=G2\space] 
  \node[vertex] (u) at (0, 0) {u};
  \node[vertex] (v) at (1, 0) {v};
  \draw (u) -- (v);
\end{scope}

\draw (G1 u) -- (G2 v);

\end{tikzpicture}

\end{document}

enter image description here

Mark Wibrow
  • 70,437
3

For completeness, this looks like what you wanted to have with the alias code

\documentclass[tikz]{standalone}
\makeatletter
\tikzset{%
  vertex/.style={circle,draw,fill=black!20},
  prefix node name/.code={%
    \tikzset{%
      name/.code={\edef\tikz@fig@name{#1 ##1}%
        \tikz@fig@mustbenamed%
          \expandafter\def\expandafter\tikz@alias\expandafter{%
        \tikz@alias\pgfnodealias{##1}{\tikz@fig@name}%
        }%
      }%
    }%
  }
}
\makeatother

\begin{document}
\begin{tikzpicture}
% ---- Copy 1
\begin{scope}[yshift=-32pt,prefix node name=G1]
  \node[vertex] (u) at (0, 0) {u};
  \node[vertex] (v) at (1, 0) {v};
  \draw (u) -- (v);
\end{scope}

% ---- Copy 2
\begin{scope}[yshift=32pt,prefix node name=G2]
  \node[vertex] (u) at (0, 0) {u};
  \node[vertex] (v) at (1, 0) {v};
  \draw (u) -- (v);
  \draw (G1 u) to[bend right] (v);
\end{scope}

\draw (G1 u) -- (G2 v);
\end{tikzpicture}
\end{document}

enter image description here

percusse
  • 157,807