# Making TikZ nodes hyperlinkable

I have a TikZ tree. Now I want to make the nodes click-able directing to relevant sections in the document (or directed to URLs). Some of the content in the nodes are not text. Is it possible to turn the nodes into links, and if so, how? I would prefer the nodes themselves to be links, and not the text they contain.

Addition: I am not really good at it, and still learning, but here is a skeletal outline of the idea I am working on... (a graphical table of contents)

\documentclass{minimal}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
\tikzstyle{level 1}=[sibling distance=60mm]
\tikzstyle{level 2}=[sibling distance=25mm]

\node[circle,draw,grow=down] {\textbf{COOL TOC}}
child {node {installation}
child {node {Linux}}
child {node {Mac}}
child {node {Windows}}
}
child {node {Get Started}
child {node {do A}}
child {node {do B}}
}
child {node {Looking Further}
child {node {manual}}
child {node {online}}
}
child[grow=up] {node {troubleshoot}
child {node {if X happens}}
child {node {if Y happens}}
};
\end{tikzpicture}

\end{document}


There are fancier examples for example this one would make for a great TOC.

-
Please add a minimal working example (MWE) that shows what you've done thus far in terms of creating a tikz tree with some document elements. –  Werner Nov 25 '11 at 4:10
@Werner I added the MWE. –  yayu Nov 25 '11 at 4:47
I added a little code to complete the MWE. An MWE should provide sufficient code for a simple copy-paste-compile so that someone interested in finding a solution doesn't have to do any puzzling over what packages you're using, or type \documentclass etc. which gets boring. This is a nice question! –  qubyte Nov 25 '11 at 14:47

Here's a new TikZ style called hyperlink node=<target> that takes a hypertarget reference. It works by measuring the node it is supplied to, and then placing a new invisible node on top of that. The new node has the content \hyperlink{<target>}{\phantom{\rule{<width of node>}{<height of node>}}, so it has the same size as the original node, but the whole area is clickable.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{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)] {\hyperlink{#1}{\XeTeXLinkBox{\phantom{\rule{\n1}{\n2}}}}}
%xelatex needs \XeTeXLinkBox, won't create a link unless it
%finds text --- rules don't work without \XeTeXLinkBox.
%Still builds correctly with pdflatex and lualatex
}
}
}

\tikz \node [draw, inner sep=2ex,hyperlink node=pagetwo] {Go to Page Two};

\clearpage
\hypertarget{pagetwo}{Page Two}
\end{document}

-
Very nice! I wanted to put a url link into the document so I exchanged \hyperlink with \href. This works as expected. –  Sebastian Mar 4 '13 at 16:15
This doesn't work for me. The output PDF has the node but it is not clickable. There were no errors in compilation, and I tried it with both Okular and Evince. –  jja Mar 12 '13 at 14:50
@jja: Are you using pdflatex for the compilation? –  Jake Mar 12 '13 at 15:05
Ah, that was it. I was using xelatex and it didn't work, but I just compiled with pdflatex and it worked perfectly. I guess a follow-up question would be how to do this with xelatex, if it can be done at all... –  jja Mar 12 '13 at 16:23
I've tried to add this to an existing drawing and get the error message Package pgf Error: No shape named is known. (note the two spaces after named. Any ideas? –  vwegert Feb 10 at 19:59

Apart from Jake's nice idea – here's some code that works fine here in a recent document using the code from Drawing a directory listing a la the tree command in TikZ (answer of A. Stacey) and your MWE. It's not as fancy as your's, but maybe stylish, too.

\documentclass{article}

\usepackage{tikz}
%------------------%
\makeatletter
\newcount\dirtree@lvl
\newcount\dirtree@plvl
\newcount\dirtree@clvl
\def\dirtree@growth{%
\ifnum\tikznumberofcurrentchild=1\relax
\expandafter\xdef\csname dirtree@p@\the\dirtree@plvl\endcsname{\the\dirtree@lvl}
\fi
\dirtree@clvl=\dirtree@lvl
\advance\dirtree@clvl by -\csname dirtree@p@\the\dirtree@plvl\endcsname
\pgf@xa=0.5cm\relax % change the length to your needs
\pgf@ya=-0.75cm\relax % change the length to your needs
\pgf@ya=\dirtree@clvl\pgf@ya
\pgftransformshift{\pgfqpoint{\the\pgf@xa}{\the\pgf@ya}}%
\ifnum\tikznumberofcurrentchild=\tikznumberofchildren
\fi
}
\tikzset{ %definition of a new style "dirtree"
dirtree/.style={
growth function=\dirtree@growth,
every node/.style={anchor=north},
every child node/.style={anchor=west},
edge from parent path={(\tikzparentnode\tikzparentanchor) |- (\tikzchildnode\tikzchildanchor)}
}
}
\makeatother
%------------------%

\usepackage{hyperref}

\begin{document}

\begin{tikzpicture}[dirtree] % it's what we defined above

\node {\textbf{COOL TOC}}
child { node {\hyperref[install]{installation} }
child { node {\hyperref[linux]{Linux}} }
child { node {\hyperref[mac]{Mac}} }
child { node {\hyperref[win]{Windows}} }
}
child {node {\hyperref[start]{Get Started}}
child { node {\hyperref[caseA]{do A}} }
child { node {\hyperref[caseB]{do B}} }
}
child {node {\hyperref[trbl-shoot]{troubleshoot}}
child {node {\hyperref[caseX]{if X happens}}}
child {node {\hyperref[caseY]{if Y happens}}}
}
% I've put the external resources to the end:
child {node {Looking Further}
child { node {\href{file:manual.pdf}{manual}} }% works only, if "manual.pdf" is in
% the same directory as the compiled
% version of this document
child { node {\href{http://website.example.com/online-resource/}{online}} }
};
\end{tikzpicture}

\section*{Installation}\label{install}
\subsection*{Linux}\label{linux}
Some content.

\subsection*{Mac}\label{mac}
Some content.

\subsection*{Windows}\label{win}
Some content.

\section*{Get started}\label{start}
\subsection*{First: Do A}\label{caseA}
Some content.

\subsection*{Second: Do B}\label{caseB}
Some content.

\section*{Trouble shooting}\label{trbl-shoot}
\subsection*{If X happens:}\label{caseX}
Some content.

\subsection*{If Y happens:}\label{caseY}
Some content.

\end{document}

-