1

Slightly convoluted as I generate my TikZ graphics with Emacs org-babel source blocks, but I've reproduced this problem in a .tex file (see below).

Following this post and many related ones, I've tried to insert hyperlinks to a TikZ diagram - specifically using a graph drawing algorithm and compiling with LuaLaTeX. However, the alias=sourcenode in the \tikzset argument seems to create the error Package pgf Error: No shape named 'sourcenode' is known.

The resulting .pdf seems to be look fine, with a functioning hyperlink to the right webpage. But I'd like to remove this compilation error and/or understand how it arises. I've tried many different versions of the LaTeX style, including e.g. \tikz [ ...] instead of wrapping \graph in a \begin{tikzpicture}[...] block, but the error persists.

Here's my minimal(-ish) reproducible example, where I compile with

Running `LaTeX' on `latex-q6CtLl' with 
``lualatex --jobname=latex-q6CtLl  -file-line-error   
--synctex=1 -interaction=nonstopmode latex-q6CtLl.tex''
\documentclass{article}
\usepackage[usenames]{color}
\usepackage{hyperref}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,arrows,backgrounds,calc,positioning}
\usetikzlibrary{arrows.meta,graphs,graphdrawing,quotes}
\usegdlibrary{trees,force} 
\pagestyle{empty}          
\begin{document}
\tikzset{
    background rectangle/.style={fill=white},
    hyperlink node/.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)] 
{\href{#1}{\phantom{\rule{\n1}{\n2}}}}
        }
    }
}
\begin{tikzpicture}[nodes={minimum height=1.5em, text depth=.2em, inner sep=0.5cm, draw=black!20, thick, fill=white, align=center}, >={Stealth[round,sep]}, rounded corners, semithick]
\graph[tree layout, grow'=-10, level distance=3cm, sibling distance=2cm,, edge quotes mid,]
{
a/"Linked node" [draw=red, hyperlink node=www.google.com],
b/"Ordinary node",
a -- b,
};
\end{tikzpicture}
\end{document}

Here is the error message:

./latex-q6CtLl.tex:29: Package pgf Error: No shape named `sourcenode' is known.
  • 1
    Yeah, the graph drawing library saves the nodes internally (instead of drawing it), does its calculation and only then draws it. That's why sourcenode can't be found. You will need to either do this after the \graph or as a label (which has special consideration by the library). – Qrrbrbirlbel Feb 17 '23 at 11:15

1 Answers1

2

The graph drawing library saves the nodes internally (instead of drawing it), does its calculation and only then draws it.

That's why sourcenode can't be found.

You will need to do this this after the \graph command or as a label (which has special consideration by the library).

Here is a label implementation. The parse let key is from my own answer.

I've removed the need for \phantom by using two \rules with no width/height.

The border that's added to the link by hyperref does some weird placement things which look weird which is why I remove it via hidelinks. You could draw = red or similar to the hyperlink node style (not the label!) to have every hyperlinked node automatically show up with a border.

I've added a lot of keys with their default value to the label to offset the settings you did with nodes. It might be better to move the nodes key to the \graph command because that's a different one which won't affect the labels.

Code

\documentclass[tikz,border=5mm]{standalone}
\usepackage{hyperref}
\usetikzlibrary{calc, graphs,graphdrawing}
\usegdlibrary{trees}
\makeatletter
\tikzset{parse let/.code={\def\tikz@cc@stop@let in{}\tikz@let@command et #1in}}
\makeatother
\pagestyle{empty}          
\begin{document}
\tikzset{
  hyperlink node/.style={
    label={[%
      parse let={\p1=($(\tikzlastnode.north east)-(\tikzlastnode.south west)$)},
      minimum size=+0pt, inner sep=+0pt, outer sep=+0pt,
      path only, text depth=, text width=, text height=,
      anchor=center, sharp corners]center:%
        \hypersetup{hidelinks}\href{#1}{\rule{0pt}{\y1}\rule{\x1}{0pt}}}}}
\begin{tikzpicture}[
  nodes={
    minimum height=1.5em, text depth=.2em, inner sep=0.5cm,
    draw=black!20, thick, fill=white, align=center},
    rounded corners, semithick]
\graph[
  tree layout, grow'=-10,
  level distance=3cm, sibling distance=2cm,
  edge quotes mid]{
    a/"Linked node" [draw=red, hyperlink node=https://www.google.com],
    b/"Ordinary node",
    a -- b,
  };
\end{tikzpicture}
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
  • Thanks very much! Before I saw this I found this answer which also seems to work for me. Thanks very much for your explanation and the link to your other answer; I will try to understand more about how and why this works but I often just forget the details as I often go a few months between using TikZ! – nonreligious Feb 19 '23 at 13:36