[Tex/LaTex] Tikz picture flowchart with labels inside blocks and referencing them in the main text

cross-referencinghyperreftikz-pgf

I need a way of putting labels inside the blocks of a flowchart, have them numbered or enumerated in some way and then reference the blocks in the main text. I need the hyperlink to go directly to that node when clicked in the PDF.

I already have a solution that almost does what I want. I'm not an expert at *TeX so I've hacked the following posts together:

custom counter and cross-referencing

TikZ Document Navigation (\label and \ref commands)

\documentclass{article}
\usepackage{hyperref}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}

\newcounter{BlockCounter}

\begin{document}

\newcommand{\labelBlock}[1]{%
\refstepcounter{BlockCounter}%
\hypertarget{#1}{}(\theBlockCounter\label{#1})%
}

\newcommand{\refBlock}[1]{%
\hyperref[#1]{Block~\ref*{#1}}% (see p. 18 of the hyperref manual)
}

\begin{figure}[tbp]
    \centering
    \tikzstyle{block} = [draw, fill=black!20, rectangle, minimum height=2em, minimum width=6em]
    \begin{tikzpicture}[auto, node distance=2cm,>=latex']
        \node [block] (aaa) {\labelBlock{blockA} Step one};
        \node [block, below left=of aaa] (bbb) {\labelBlock{blockB} Step two-left};
        \node [block, below right=of aaa] (ccc) {\labelBlock{blockC} Step two-right};
        \draw [draw,->] (aaa) -- node {left} (bbb);
        \draw [draw,->] (aaa) -- node {right} (ccc);
    \end{tikzpicture}
    \caption{Caption here.}
\end{figure}

Reference to \refBlock{blockA}. Reference to \refBlock{blockB}. Reference to \refBlock{blockC}.

\end{document}

Which produces the following result

enter image description here

Questions:

  1. How can I control the exact position of the hypertarget? I tried using a raisebox but this had no effect. I need it to go a little above the block.
  2. How do I change the enumeration to A,B,C, instead of 1,2,3?

Best Answer

You can put all the commands that activate hyperref into a \raisebox leaving the printed counter in its original position:

\documentclass{article}
\usepackage{hyperref}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}

\newcounter{BlockCounter}
\renewcommand{\theBlockCounter}{\Alph{BlockCounter}}

\begin{document}

\newcommand{\labelBlock}[1]{%
\smash{\raisebox{15pt}{\refstepcounter{BlockCounter}\hypertarget{#1}{}\label{#1}}}%
(\theBlockCounter)%
}

\newcommand{\refBlock}[1]{%
\hyperref[#1]{Block~\ref*{#1}}% (see p. 18 of the hyperref manual)
}

\begin{figure}[tbp]
    \centering
    \tikzstyle{block} = [draw, fill=black!20, rectangle, minimum height=2em, minimum width=6em]
    \begin{tikzpicture}[auto, node distance=2cm,>=latex']
        \node [block] (aaa) {\labelBlock{blockA} Step one};
        \node [block, below left=of aaa] (bbb) {\labelBlock{blockB} Step two-left};
        \node [block, below right=of aaa] (ccc) {\labelBlock{blockC} Step two-right};
        \draw [draw,->] (aaa) -- node {left} (bbb);
        \draw [draw,->] (aaa) -- node {right} (ccc);
    \end{tikzpicture}
    \caption{Caption here.}
\end{figure}

Reference to \refBlock{blockA}. Reference to \refBlock{blockB}. Reference to \refBlock{blockC}.

\end{document}