TikZ 树中的超链接

TikZ 树中的超链接

我正在尝试结合这个答案如何创建具有树状层次结构的超链接,取自文件系统树示例。虽然基本超链接可以工作,但当我将链接样式引入树节点时,我收到一条错误消息。错误消息是

! Package pgf Error: No shape named  is known.

我怎样才能将这两个部分解决方案结合起来?我使用的示例是

\documentclass{article}
\usepackage[hidelinks]{hyperref}
\usepackage{tikz}
\usetikzlibrary{trees,calc}
\begin{document}
\tikzset{
    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)] {\hyperref[#1]{\phantom{\rule{\n1}{\n2}}}}
        }
    }
}

\tikzstyle{every node}=[draw=black,thick,anchor=west]

% simple drawing - works
\begin{tikzpicture}
  \node [draw, inner sep=2ex,hyperlink node=sec:foo] {Go to Page Two};
\end{tikzpicture}

% tree without link - works 
\begin{tikzpicture}[%
  grow via three points={one child at (0.5,-0.7) and
  two children at (0.5,-0.7) and (0.5,-1.4)},
  edge from parent path={([xshift=2ex]\tikzparentnode.south west) |- (\tikzchildnode.west)},
  growth parent anchor=west]
  \node {Root Beer Rag}
    child { node {Go to Page Two}};
\end{tikzpicture}

% tree with link - error
\begin{tikzpicture}[%
  grow via three points={one child at (0.5,-0.7) and
  two children at (0.5,-0.7) and (0.5,-1.4)},
  edge from parent path={([xshift=2ex]\tikzparentnode.south west) |- (\tikzchildnode.west)},
  growth parent anchor=west]
  \node [hyperlink node=sec:foo] {Root Beer Rag}
    child { node {Go to Page Two}};
\end{tikzpicture}

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

\end{document} 

答案1

经过几次尝试,我让 option工作了,但除了通过存储位置(需要运行两次 LaTeX)之外,label我还没有找到一种获取标签内节点大小的好方法。最后,我在 option 的源代码中找到了一些东西。它添加了一个节点,但通过恢复来避免问题。\rule\pdfsaveposlabel\tikz@last@fig@name

示例使用默认的红框注释来显示注释矩形。我默认使用了pdflatex\pdflinkmargin=1pt因此矩形稍微大一些:

\documentclass{article}
\usepackage{hyperref}
\usepackage{bookmark}
\usepackage{tikz}
\usetikzlibrary{trees,calc}

\begin{document}

\makeatletter
\tikzset{
  hyperlink node/.style={
    append after command={%
      \bgroup
        [current point is local=true]
        \pgfextra{%
          \let\tikz@save@last@fig@name=\tikz@last@fig@name
        }%
        let \p1=(\tikzlastnode.south west),
            \p2=(\tikzlastnode.north east),
            \n1={\x2-\x1},
            \n2={\y2-\y1}
        in
        node[
          at=(\p1),
          draw=none,
          shape=rectangle,
          inner sep=0pt,
          outer sep=0pt,
          minimum width=0pt,
          minimum height=0pt,
        ]{%
          \rlap{%
            \raisebox{0pt}[0pt][0pt]{%
              \hyperref[{#1}]{%
                \phantom{\rule{\n1}{\n2}}%
              }%
            }%
          }%
        }%
        \pgfextra{%
          \global\let\tikz@last@fig@name=\tikz@save@last@fig@name
        }%
      \egroup
    },
  },
}
\makeatother

\tikzstyle{every node}=[draw=black,thick,anchor=west]

\begin{tikzpicture}[%
  grow via three points={one child at (0.5,-0.7) and
  two children at (0.5,-0.7) and (0.5,-1.4)},
  edge from parent path={([xshift=2ex]\tikzparentnode.south west)
      |- (\tikzchildnode.west)},
  growth parent anchor=west]
  \node [hyperlink node=sec:foo] {Go to Target Foo}
    child { node {First Child}}
    child { node[hyperlink node=sec:bar] {Go to Target Bar}}
  ;
\end{tikzpicture}

\clearpage
\section{Target Foo} 
\label{sec:foo}

\clearpage
\section{Target Bar}
\label{sec:bar}
\end{document} 

结果

相关内容