Here is a little improvement of Jake's answer.
Thanks to frougon's comment, different values for outer xsep
and outer ysep
are now correctly considered.
Problem
- The options
line width
and outer sep
are not considered. Therefore, the link box is usually too large (only working case is outer sep=0
).
- DrBones addendum does not solve this problem, because it only changes the size of the hyperlink node, but not the hyperlink area itself.
Solution
If outer sep
is not set, it is half of line width
. Therefore, we just have to use outer sep
to solve the following cases.
I defined \n0
and \n1
as outer xsep
and outer ysep
and shifted the two points by that values to the middle of the node:
let \n0={\pgfkeysvalueof{/pgf/outer xsep}},
\n1={\pgfkeysvalueof{/pgf/outer ysep}},
\p1=([shift={(\n0, -\n1)}] sourcenode.north west),
\p2=([shift={(-\n0, \n1)}] sourcenode.south east),
Visualization and Description
- The node has a thick cyan border.
- The black rectangle surrounds the node anchors (
north west
and south east
).
- The yellow rectangle is the visualization of the hyperlink area.
- The passed options are shown in the lower part of the figure.
- If yellow (hyperlink areas) and cyan (node borders) overlap, we get green, as we see in the
hyperlink dexteritas
variant.
Code
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usepackage[hidelinks]{hyperref}
% NOTE: added option "test highlight" for testing (can be removed for normal usage)
\tikzset{
test highlight/.style={draw=yellow, draw opacity=0.5},
hyperlink Jake/.style={
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), test highlight] {\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
}
},
hyperlink dexteritas/.style={
alias=sourcenode,
append after command={
let \n0={\pgfkeysvalueof{/pgf/outer xsep}},
\n1={\pgfkeysvalueof{/pgf/outer ysep}},
\p1=([shift={(\n0, -\n1)}] sourcenode.north west),
\p2=([shift={(-\n0, \n1)}] sourcenode.south east),
\n2={\x2-\x1},
\n3={\y1-\y2} in
node [inner sep=0pt, outer sep=0pt, anchor=north west, at=(\p1), test highlight] {\hyperlink{#1}{\XeTeXLinkBox{\phantom{\rule{\n2}{\n3}}}}}
%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
}
},
}
\begin{document}
\newcommand{\comparison}[1][]{
\begin{tikzpicture}[
line width=1mm,
node distance=5mm,
inner sep=2mm,
blend mode=darken,
#1
]
\foreach \variant [count=\i] in {Jake, dexteritas} {
\node[text width=25mm, font=\small\ttfamily] at (0,-6) {#1};
\node[draw=cyan, draw opacity=0.5, hyperlink \variant={test}] (a) at (0,-2*\i) {\variant};
\draw[very thin] (a.north west) rectangle (a.south east);
}
\end{tikzpicture}
}
\comparison
\comparison[outer sep=0mm]
\comparison[outer sep=3mm]
\comparison[outer xsep=1mm, outer ysep=4mm]
\end{document}
tikz
tree with some document elements.\documentclass
etc. which gets boring. This is a nice question!