# Making hyperlinked graphs with the TikZ graphdrawing package

This question here garnered an interesting response on how to make TikZ node text clickable.

I am using the graphdrawing package in order to automatically set up the graph, so I am unable to use the \hyperlink command in the \graph "environment. My file at the moment looks like this:

\documentclass{article}
\usepackage{hyperref}
\usepackage{bookmark}
\usepackage{tikz}
\usetikzlibrary{trees,calc}
\usepackage{tikz}
\usetikzlibrary{graphs, graphdrawing}
\usegdlibrary{trees, layered}

\begin{document}

\begin{tikzpicture}
\tikz [rounded corners]
\graph [layered layout, sibling distance=8mm, level distance=8mm]
{
A -> B -> C -> A
};
\end{tikzpicture}

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

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

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

\end{document}


How could I set up hyperlinks to be such that if click on A in the graph, it takes me to the section titled "A" if I am using graphdrawing?

-

Good timing! — I just made a similar “graph of contents” for my own document. :o)

How this works: I assign labels to all sections, and use those as the names of the graph nodes. The nodes are then typeset using the command \myref, which outputs three hyperlinks to the given label as part of the node text.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,graphs,graphdrawing}
\usegdlibrary{layered}
% Use frenchlinks for nifty small caps
\begin{document}

% Hyperlinks to:     Section #         Section name                    Page  number
%                    —————————         ————————————                    ————————————
\newcommand\myref[1]{\ref{#1}. \textbf{\nameref{#1}}\\\footnotesize p. \pageref{#1}}
%                            — —————————————————————  —————————————
%                     Period ^         Bold            Small  text

\section*{Graph of contents}
\begin{tikzpicture}[>=latex']
% (Note that you don’t need both \begin{tikzpicture} and \tikz)
\graph [
layered layout, level distance=4em, sibling distance=3em,
nodes={
% Draw nodes as rectangles with centered text and a bit of horizontal padding
draw, align=center, inner xsep=0.5em,
% Use \myref to typeset nodes using their name (viz. section label) as input
typeset={\myref{\tikzgraphnodefullname}}
},
] { a -> { b -> c, d } };
\end{tikzpicture}

\section{The first section}  \label{a}
\section{The second section} \label{b}
\section{The third section}  \label{c}
\section{The fourth section} \label{d}
\end{document}


If you want to keep the sec: prefix in your section labels, you could just stick it at the beginning of the \ref, \nameref, and \pageref commands.

If you want the entire rectangle to be clickable, maybe take a look at this question.

-
Looks cool. This could be automated. –  1010011010 Jun 28 at 17:13
Hm? How much more automated could you possibly make this? –  hftf Jun 28 at 17:57
It's totally doable to hijack the \tableofcontents command to produce that tikzpicture that you show here. –  1010011010 Jun 28 at 19:06