tikzpicture 中的超链接框大小不正确

tikzpicture 中的超链接框大小不正确

我在 tikzpicture 中使用超链接时遇到了非常奇怪的行为。以下是代码片段:

\documentclass{article}

\usepackage{tikz}
\usepackage[linktocpage=true]{hyperref}
\usepackage[capitalize]{cleveref}

\usetikzlibrary{arrows}
\tikzstyle{rect} = [rectangle, rounded corners, minimum width=8em,%
minimum height=3em, text width=8em, text centered, draw=black]
\tikzstyle{rect2} = [rectangle, rounded corners, minimum width=5.5em,%
minimum height=3em, text width=5.5em, text centered, draw=black]
\tikzstyle{anch} = [inner sep=0em, outer sep=0em]
\tikzstyle{arrow} = [thick,->,>=stealth]

% Create a spacer to make sure box around link isn't too close to text.
\newcommand{\spacerrule}[3][0 em]{\textcolor{white}{\rule[#2]{#1}{#3}}}

\begin{document}

% So the hyperlinks link to something.
\label{signedranktest}
\label{ranksumtest}

% Verify that the commands work outside of tikzpicture

\hyperref[signedranktest]{\parbox{9em}{\center\vspace{-1 em}\spacerrule{.1 em}{1 em}%
\textbf{Wilcoxon Signed Rank Test}\spacerrule{-.4 em}{1 em}}}
\vspace{1 em}

\hyperref[ranksumtest]{\parbox{9em}{\center\vspace{-1 em}\spacerrule{.1 em}{1 em}%
\textbf{Wilcoxon Rank Sum Test}\spacerrule{-.4 em}{1 em}}}

\begin{center}
\begin{tikzpicture}[node distance=3 em]

\node (samp2) [rect] {2 groups};

\node (lev2b) [anch, below of=samp2]{};
\node (paired1) [rect, below left of=lev2b, xshift=-3 em, yshift=-1.5 em]{Paired/matched samples};
\node (indep1) [rect, below right of=lev2b, xshift=3 em, yshift=-1.5 em]{Independent samples};

\node (pairtest) [below of=paired1, yshift=-2 em, text width=9em, text centered]%
{\hyperref[signedranktest]{\parbox{9em}{\center\vspace{-1 em}\spacerrule{.1 em}{1 em}%
\textbf{Wilcoxon Signed Rank Test}\spacerrule{-.4 em}{1 em}}}};

\node (indtest) [below of=indep1, yshift=-2 em, text width=9em, text centered]%
{\hyperref[ranksumtest]{\parbox{9em}{\center\vspace{-1 em}\spacerrule{.1 em}{1 em}%
\textbf{Wilcoxon Rank Sum Test}\spacerrule{-.4 em}{1 em}}}};

\draw [arrow] (samp2) -- (lev2b);
\draw [arrow] (lev2b) -| (paired1);
\draw [arrow] (lev2b) -| (indep1);
\draw [arrow] (paired1) -- (pairtest);
\draw [arrow] (indep1) -- (indtest);

\end{tikzpicture}
\end{center}

\end{document}

当我运行此程序时,“Wilcoxon 符号秩检验”周围的框大小错误,程序认为它只有一行高,而不是两行。真正奇怪的是,如果您注释掉以“\node (pairtest)”开头的行,那么“Wilcoxon 秩和检验”框的大小就会像另一个框一样错误;如果您交换以“\node (pairtest)”和“\node (indtest)”开头的行的顺序,那么 WRST 框大小错误,而 WSRT 框是正确的!所以它似乎总是遇到第一个大小错误的框。

有什么想法吗?

答案1

您的代码源自 TikZ 的第一个版本,现在已被弃用。

您到处添加的额外框会干扰 TikZ 的正常工作。这里最简单的方法是使用新的 3.0.1a 版 TikZ 完成所有操作,而无需执行使工作复杂化的额外命令。

因此,我删除了这些框并更新了代码。

因此,您的个人宏\spacerrule是无用的,要解决所有问题,只需加载库positioning(阅读手册 3.0.1a 第 229 页第 17.5.3 节)。

\tikzstyle弃用,取而代之的是\tikzset。对于箭头,该库现在称为arrows.meta

截屏

\documentclass{article}

\usepackage{tikz}
\usepackage[linktocpage=true]{hyperref}
\usepackage[capitalize]{cleveref}

\usetikzlibrary{arrows}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning}
\tikzset{rect/.style={rectangle, rounded corners, minimum width=8em,%
minimum height=3em, text width=8em, text centered, draw=black},
        rect2/.style={rectangle, rounded corners, minimum width=5.5em,%
minimum height=3em, text width=5.5em, text centered, draw=black},
        anch/.style={inner sep=0em, outer sep=0em}
        }

% Create a spacer to make sure box around link isn't too close to text.
%\newcommand{\spacerrule}[3][0 em]{\textcolor{white}{\rule[#2]{#1}{#3}}}

\begin{document}

% So the hyperlinks link to something.
\label{signedranktest}
\label{ranksumtest}

% Verify that the commands work outside of tikzpicture

\hyperref[signedranktest]{\parbox{9em}{\center\vspace{-1 em}%
\textbf{Wilcoxon Signed Rank Test}}}
\vspace{1 em}

\hyperref[ranksumtest]{\parbox{9em}{\center\vspace{-1 em}%
\textbf{Wilcoxon Rank Sum Test}}}

\begin{center}
\begin{tikzpicture}[node distance=3 em,>={Stealth}]

\node (samp2) [rect] {2 groups};

\node (lev2b) [anch, below of=samp2]{};
\node (paired1) [rect, below left=  of lev2b]{Paired/matched samples};
\node (indep1) [rect, below right= of lev2b]{Independent samples};


\node (pairtest) [below = of paired1, text width=9em, text centered]%
{\hyperref[signedranktest]{\textbf{Wilcoxon Signed Rank Test}}};

\node (indtest) [below = of indep1, text width=9em, text centered]%
{\hyperref[ranksumtest]{\parbox{9em}{\textbf{Wilcoxon Rank Sum Test}}}};

\draw [->] (samp2) -- (lev2b);
\draw [->] (lev2b) -| (paired1);
\draw [->] (lev2b) -| (indep1);
\draw [->] (paired1) -- (pairtest);
\draw [->] (indep1) -- (indtest);

\end{tikzpicture}
\end{center}

\end{document}

答案2

正如承诺的那样,这是对我有用的最终代码;感谢 AndréC 为我指明了正确的方向。我从 WRST 框中删除了间距宏,以说明我使用它的原因。对于那些没有关注我们的评论线程的人,您需要创建一个 PDF 才能看到链接周围的框。

\documentclass{article}

\usepackage{tikz}
\usepackage[linktocpage=true,linkbordercolor={0 0 1}]{hyperref} % Makes blue box in PDF
\usepackage[capitalize]{cleveref}

%\usetikzlibrary{arrows}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning}
\tikzset{rect/.style={rectangle, rounded corners, minimum width=8em,%
minimum height=3em, text width=8em, text centered, draw=black},
        rect2/.style={rectangle, rounded corners, minimum width=5.5em,%
minimum height=3em, text width=5.5em, text centered, draw=black},
        anch/.style={inner sep=0em, outer sep=0em}
        }

% Create a spacer to make sure box around link in PDF isn't too close to text.
\newcommand{\spacerrule}[3][0 em]{\textcolor{white}{\rule[#2]{#1}{#3}}}

\begin{document}

% So the hyperlinks link to something.
\label{signedranktest}
\label{ranksumtest}

\begin{center}
\begin{tikzpicture}[node distance=3 em,>={Stealth}]

\node (samp2) [rect] {2 groups};

\node (lev2b) [anch, below of=samp2]{};
\node (paired1) [rect, below left=  of lev2b]{Paired/matched samples};
\node (indep1) [rect, below right= of lev2b]{Independent samples};

% With spacing macro:

\node (pairtest) [below = of paired1, text width=9em, text centered]%
{\hyperref[signedranktest]{\parbox{9em}{\centering\spacerrule{.1 em}{1 em}\textbf{Wilcoxon Signed Rank Test}\spacerrule{-.4 em}{1 em}}}};

% Without spacing macro:

\node (indtest) [below = of indep1, text width=9em, text centered]%
{\hyperref[ranksumtest]{\parbox{9em}{\centering\textbf{Wilcoxon Rank Sum Test}}}};

\draw [->] (samp2) -- (lev2b);
\draw [->] (lev2b) -| (paired1);
\draw [->] (lev2b) -| (indep1);
\draw [->] (paired1) -- (pairtest);
\draw [->] (indep1) -- (indtest);

\end{tikzpicture}
\end{center}

\end{document}

相关内容