# Using \hyperref inside Tikz (forest)

Here's my cute little forest:

Code:

\documentclass[11pt,a4paper]{article}
\usepackage{forest}
\usepackage{hyperref}

\begin{document}
\begin{forest}
]
\end{forest}

\section{Country}
\label{sec:country}
\subsection{United States}
\label{sec:country:us}
\subsection{United Kingdom}
\label{sec:country:ul}
\end{document}


Now I want to \hyperref the forest's leaves to the corresponding section and subsections, such that when you click on Link to Country you'll get directed to the section Country and so forth. This is what I have tried:

...
\begin{forest}
]
\end{forest}
...


Unfortunately I got a bunch of errors and the code didn't produce anything. My guess is that the macro hyperref expands into something that breaks either Tikz or forest's syntax. But I don't know much about Latex to find the solution myself.

When I use hyperref outside of the forest, it works fine. So obviously no problem with the packages.

\ref{...} appears to work fine so obviously it must be possible to have link inside a Tikz/forest.

How should I fix it?

You have just to make the optional argument to \hyperref “invisible” to forest: use braces.

\documentclass[11pt,a4paper]{article}
\usepackage{forest}
\usepackage{hyperref}

\begin{document}

\begin{forest}
]
\end{forest}

\section{Country}
\label{sec:country}
\subsection{United States}
\label{sec:country:us}
\subsection{United Kingdom}
\label{sec:country:uk}

\end{document}


Here's a convenience wrapper based on egreg's code which makes the tree specification a bit less cumbersome.

\documentclass[11pt,a4paper]{article}
\usepackage{forest}
\usepackage{hyperref}
\forestset{
before typesetting nodes={
where content={}{}{
delay={
}
},
},
},
}
\begin{document}
\begin{forest}