2

I am wondering how to reference a Tikz node outside a Tikz Picture, as if referring to an image. This page lists the possible \ref{} objects not including nodes, but maybe there is a package that it ignores. Anyway, I was wondering how it is usually done. Example:

\begin{tikzpicture}
\node (a) at (0,0){example node};
\end{tikzpicture}
Desired function: ~\ref{node: a}, or something like this

I am currently using a node's label to explicitly reference it, but it doesn't seem like the best method.

Lara
  • 23
  • 2
    Welcome to TeX.SX! Could you elaborate what you expect the \ref should return? – Stefan Pinnow Feb 05 '19 at 18:34
  • Hello and thank you! I expect behavior similar to a \ref{fig:example}, where a number is displayed based on the image label. In this case, I am wondering how to label a node and then reference it in this manner. – Lara Feb 05 '19 at 19:03
  • Here is a way to remember the content of a node. (I am not sure what you mean by number, do you want to auto-number all nodes? If so, should the node number also be inserted in the node?) –  Feb 05 '19 at 19:06
  • Yes, auto-numbering them for referencing. The way presented in the link is what I am doing as of now, I was wondering if there is a more ''elegant'' way to do so :) – Lara Feb 05 '19 at 19:10

1 Answers1

1

If I understand correctly, you may be looking for this.

  1. \customlabel from this post is used to create a new referencable label. There you can also find what you could do if you don't want to use hyperref. Whether or not there is a version that works both with and without hyperref, I do not know.
  2. A new style referencable node is created that steps the node counter and also shows it as label.

And this is how you may use it.

\documentclass{article}
\usepackage{tikz}
\usepackage{hyperref}
\newcounter{node}
\makeatletter% from https://tex.stackexchange.com/a/160035/121799
\newcommand{\customlabel}[2]{%
   \protected@write \@auxout {}{\string \newlabel {#1}{{#2}{\thepage}{#2}{#1}{}} }%
   \hypertarget{#1}{#2}
}
\tikzset{autonumbered node/.style={/utils/exec={\stepcounter{node}},
label=above:{\arabic{node}}}}
\newcommand{\nodelabel}[1]{%
\protect\customlabel{#1}{\arabic{node}}}
\makeatother
\begin{document}
\begin{tikzpicture}
\node[autonumbered node] (a) at (0,0) {example node\nodelabel{node:a}};
\node[autonumbered node] (b) at (2,1) {another example node\nodelabel{node:b}};
\end{tikzpicture}

Node~\ref{node:a} is a node and so is \ref{node:b}.
\clearpage
\begin{tikzpicture}
\node[autonumbered node] (c) at (0,0) {yet another example node\nodelabel{node:c}};
\end{tikzpicture}

Node~\ref{node:a} is on page~\pageref{node:a} and node~\ref{node:c} is on
page~\pageref{node:c}.
\end{document}

enter image description here