# Hyperlinks from a TikZ tree

I am trying to combine this answer on how to create hyperlinks with a tree hierarchy taken from the filesystem tree example. While the basic hyperlinking works, I get an error message as soon as I introduce the link style into the tree nodes. The error message is

! Package pgf Error: No shape named  is known.


How can I combine the two partial solutions? The example I use is

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees,calc}
\begin{document}
\tikzset{
alias=sourcenode,
append after command={
let     \p1 = (sourcenode.north west),
\p2=(sourcenode.south east),
\n1={\x2-\x1},
\n2={\y1-\y2} in
node [inner sep=0pt, outer sep=0pt,anchor=north west,at=(\p1)] {\hyperref[#1]{\phantom{\rule{\n1}{\n2}}}}
}
}
}

\tikzstyle{every node}=[draw=black,thick,anchor=west]

% simple drawing - works
\begin{tikzpicture}
\node [draw, inner sep=2ex,hyperlink node=sec:foo] {Go to Page Two};
\end{tikzpicture}

% tree without link - works
\begin{tikzpicture}[%
grow via three points={one child at (0.5,-0.7) and
two children at (0.5,-0.7) and (0.5,-1.4)},
edge from parent path={([xshift=2ex]\tikzparentnode.south west) |- (\tikzchildnode.west)},
growth parent anchor=west]
\node {Root Beer Rag}
child { node {Go to Page Two}};
\end{tikzpicture}

% tree with link - error
\begin{tikzpicture}[%
grow via three points={one child at (0.5,-0.7) and
two children at (0.5,-0.7) and (0.5,-1.4)},
edge from parent path={([xshift=2ex]\tikzparentnode.south west) |- (\tikzchildnode.west)},
growth parent anchor=west]
\node [hyperlink node=sec:foo] {Root Beer Rag}
child { node {Go to Page Two}};
\end{tikzpicture}

\clearpage
\section{Target}
\label{sec:foo}

\end{document}


After some tries I got option label to work, but I haven't found a nice way for getting the size of the node inside the label for \rule except for storing the positions via \pdfsavepos (needs two LaTeX runs). Finally I found something in the source code for option label. It adds a node, but avoids the problem by restoring \tikz@last@fig@name.

The example uses the default box annotations with red frame to show the annotation rectangle. I have used pdflatex with \pdflinkmargin=1pt as default. Thus the rectangle is a little bit larger:

\documentclass{article}
\usepackage{hyperref}
\usepackage{bookmark}
\usepackage{tikz}
\usetikzlibrary{trees,calc}

\begin{document}

\makeatletter
\tikzset{
append after command={%
\bgroup
[current point is local=true]
\pgfextra{%
\let\tikz@save@last@fig@name=\tikz@last@fig@name
}%
let \p1=(\tikzlastnode.south west),
\p2=(\tikzlastnode.north east),
\n1={\x2-\x1},
\n2={\y2-\y1}
in
node[
at=(\p1),
draw=none,
shape=rectangle,
inner sep=0pt,
outer sep=0pt,
minimum width=0pt,
minimum height=0pt,
]{%
\rlap{%
\raisebox{0pt}[0pt][0pt]{%
\hyperref[{#1}]{%
\phantom{\rule{\n1}{\n2}}%
}%
}%
}%
}%
\pgfextra{%
\global\let\tikz@last@fig@name=\tikz@save@last@fig@name
}%
\egroup
},
},
}
\makeatother

\tikzstyle{every node}=[draw=black,thick,anchor=west]

\begin{tikzpicture}[%
grow via three points={one child at (0.5,-0.7) and
two children at (0.5,-0.7) and (0.5,-1.4)},
edge from parent path={([xshift=2ex]\tikzparentnode.south west)
|- (\tikzchildnode.west)},
growth parent anchor=west]
\node [hyperlink node=sec:foo] {Go to Target Foo}
child { node {First Child}}
child { node[hyperlink node=sec:bar] {Go to Target Bar}}
;
\end{tikzpicture}

\clearpage
\section{Target Foo}
\label{sec:foo}

\clearpage
\section{Target Bar}
\label{sec:bar}
\end{document}


• Cool, works - but shouldn't the second \makeatletter be \makeatother? Feb 11 '14 at 8:00
• @vwegert: Yes, answer updated. Feb 11 '14 at 12:08