# How to put cross-referencing number in tikzpicture

I am trying to get theorem reference number generated automatically using the "\label{thm1}" and "\ref{thm1}" environment in the following diagram. But it shows error. If I replace "text={Theorem \ref{thm1}}" by manually inputting the theorem number "text={Theorem 1.1}" in the following, it runs perfectly. How to use the command \ref{...} in the decorations environment ?

\documentclass[]{amsart}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm,amsxtra}
\usepackage{cite}
\usepackage{enumerate}
\usepackage[mathscr]{eucal}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{tikz-cd}
\usepackage{tkz-graph}
\usepackage{tkz-berge}
\usetikzlibrary{positioning,arrows,shapes.geometric,trees}

\usepackage{mathtools}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.text}
\usepackage{graphicx}

\newtheorem{Th}{Theorem}[section]
\begin{document}

\section{Section 1}
\begin{Th}
\label{thm1}
This is a theorem.
\end{Th}

\section{Section 2}

\begin{tikzpicture}[scale=2,node distance=3cm and 2.5cm]
\node (k1) {$\underset{(1)} {Step1}$};
\node (k2) [below=of k1] {$\underset{(2)} {Step2}$};
\draw [->,postaction={decorate,decoration={raise=-2ex,text along path,text align=center,text={Theorem \ref{thm1}}}}](k1) to  (k2);
\end{tikzpicture}

\end{document}


Unless using the calc package, \ref is not an expandable command. However, \getrefnumber from refcount package is expandable, so replace \ref by \getrefnumber.

Why must it be expandable? Well, the decoration library needs information about the content and not only a box width and height.

I also suggest to replace Step1 by \text{Step 1} etc.

\documentclass[]{amsart}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm,amsxtra}
\usepackage{cite}
\usepackage{enumerate}
\usepackage[mathscr]{eucal}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{tikz-cd}
\usepackage{tkz-graph}
\usepackage{tkz-berge}
\usetikzlibrary{positioning,arrows,shapes.geometric,trees}

\usepackage{mathtools}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.text}
\usepackage{graphicx}

\usepackage{refcount}
\newtheorem{Th}{Theorem}[section]
\begin{document}

\section{Section 1}
\begin{Th}
\label{thm1}
This is a theorem.
\end{Th}

\section{Section 2}

\begin{tikzpicture}[scale=2,node distance=3cm and 2.5cm]
\node (k1) {$\underset{(1)} {\text{Step 1}}$};
\node (k2) [below=of k1] {$\underset{(2)} {\text{Step 2}}$};
\draw [->,postaction={decorate,decoration={raise=-2ex,text align=center,text along path,text={Theorem \getrefnumber{thm1}}}}](k1) to  (k2);
\end{tikzpicture}

\end{document}


• How this can be modified using \usetikzlibrary{calc} – user149418 Jun 17 '17 at 22:48
• @user149418: I don't understand your question. The tikz calc library has nothing to do with your issue about the references – user31729 Jun 17 '17 at 22:49