# Reference custom labels of TikZ graphic in text

I have drawn the following diagram using the tikz package. I need to add labels on arrows and reference them back in the text. How can I do that? I would be so grateful if someone could give feedback on this issue.

Meanwhile, the corresponding code is

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{tikz}

\tikzset{every picture/.style={line width=0.75pt}}

\begin{document}
\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]

\draw    (140,145) -- (238.59,243.59) ;
\draw [shift={(240,245)}, rotate = 225] [color={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.75]    (10.93,-3.29) .. controls (6.95,-1.4) and (3.31,-0.3) .. (0,0) .. controls (3.31,0.3) and (6.95,1.4) .. (10.93,3.29)   ;

\draw    (264,237) -- (377.23,98.55) ;
\draw [shift={(378.5,97)}, rotate = 489.28] [color={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.75]    (10.93,-3.29) .. controls (6.95,-1.4) and (3.31,-0.3) .. (0,0) .. controls (3.31,0.3) and (6.95,1.4) .. (10.93,3.29)   ;

\draw    (404,82) .. controls (444,52) and (422.5,383) .. (554.5,244) ;
\draw [shift={(554.5,244)}, rotate = 493.52] [color={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.75]    (10.93,-3.29) .. controls (6.95,-1.4) and (3.31,-0.3) .. (0,0) .. controls (3.31,0.3) and (6.95,1.4) .. (10.93,3.29)   ;

\draw (122,127) node [anchor=north west][inner sep=0.75pt]   [align=left] {};

\draw (122,124) node [anchor=north west][inner sep=0.75pt]   [align=left] {$A$};

\draw (248,241) node [anchor=north west][inner sep=0.75pt]   [align=left] {$B$};

\draw (122,133) node [anchor=north west][inner sep=0.75pt]   [align=left] {};

\draw (382,74) node [anchor=north west][inner sep=0.75pt]   [align=left] {$C$};

\draw (561,226) node [anchor=north west][inner sep=0.75pt]   [align=left] {$D$};

\end{tikzpicture}

\end{document}


## 1 Answer

I cleaned up your code a bit and labeled the arrows, however, I'm currently not sure how to reference the arrows in the text. I'll see if I can come up with a solution to this problem. Until then, here's a cleaned-up version of your graphic. Please note that I have changed your coordinates to something smaller than they currently are as TikZ generally uses cm as the unit. That's why you needed to add x=0.75pt,y=0.75pt as an argument to your tikzpicture environment. If, for whatever reason, you still need those huge coordinates, feel free to change the provided code accordingly.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\node at (0,0) (A) {$A$};
\node at (2,-1) (B) {$B$};
\node at (4,2) (C) {$C$};
\node at (8,0) (D) {$D$};

\draw[->] (A) -- (B) node [midway, above, sloped] (AtoB) {$A\rightarrow B$};

\draw[->] (B) -- (C) node [midway, above, sloped] (BtoC) {$B\rightarrow C$};

\draw[->] (C) .. controls ++(1,1) and ++(-2,-2) .. (D) node [midway, below, sloped] (CtoD) {$C\rightarrow D$};

\end{tikzpicture}

\end{document}


### Edit

I added a method to reference the labels in the text by writing the contents to the aux file. Credits to Henrik Bøgelund Lavstsen for this idea found in this thread.

\documentclass{article}

\usepackage{tikz}
\usepackage{hyperref}

\makeatletter
\newcommand{\customlabel}[2]{%
\protected@write \@auxout {}{%
\string \newlabel {#1}{{#2}{\thepage}{#2}{#1}{}}%
}%
\hypertarget{#1}{#2}%
}
\makeatother

\begin{document}
\begin{tikzpicture}

\node at (0,0) (A) {$A$};
\node at (2,-1) (B) {$B$};
\node at (4,2) (C) {$C$};
\node at (8,0) (D) {$D$};

\draw[->] (A) -- (B) node [midway, above, sloped] (AtoB) {\customlabel{node:AtoB}{$A\rightarrow B$}};

\draw[->] (B) -- (C) node [midway, above, sloped] (BtoC) {\customlabel{node:BtoC}{$B\rightarrow C$}};

\draw[->] (C) .. controls ++(1,1) and ++(-2,-2) .. (D) node [midway, below, sloped] (CtoD) {\customlabel{node:CtoD}{$C\rightarrow D$}};

\end{tikzpicture}

Let's reference the arrow with the identifyer \texttt{AtoB}, shall we?\\
Here it is: \ref{node:AtoB}

\end{document}


• Thank you very much for your help. I truly appreciate it. It would be so great if there would be a way to refer the labels of to the arrows in the text as well. Nov 20, 2020 at 11:43
• You're welcome. PS: I just updated my answer and included a referencing method.
– Sam
Nov 20, 2020 at 12:10
• Thanks a million for your help. Nov 20, 2020 at 12:40
• I'm happy to help. Welcome to TeX.SX, I hope you'll have a great time here our nerdy the community.
– Sam
Nov 20, 2020 at 13:14
• Whoops, my bad. The #2% in the \newcommand{\customlabel} macro was too much. I forgot to remove this when modifying the code to be compatible with hyperref. Sorry about that.
– Sam
Nov 20, 2020 at 13:23