在 TikZ 中使用“右侧”放置节点时节点被覆盖

在 TikZ 中使用“右侧”放置节点时节点被覆盖

我正在使用 Beamer 和 TikZ 创建一些幻灯片,但在定位节点方面遇到了一些问题。我逐步显示一些节点,并使用 定位它们[right of=previousnode]。在第三张幻灯片上,一切看起来仍然很好:

在此处输入图片描述

但是,当我将下一个节点添加到最后一个节点的右侧时,该节点没有放置在正确的位置:

在此处输入图片描述

如您所见,它最终覆盖了一些先前的节点。这里有一个最小的可重现示例:

编辑:我刚刚意识到,根据幻灯片编号显示/隐藏节点的部分与覆盖问题无关。我在问题的末尾添加了一个简化的示例,其中问题仍然存在。我保留了原始示例,但您可能更喜欢直接查看该简化示例。

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows}

\begin{document}

% Keys to support piece-wise uncovering of elements in TikZ pictures:
% \node[visible on=<2->](foo){Foo}
%
% Internally works by setting opacity=0 when invisible, which has the 
% adavantage (compared to \node<2->(foo){Foo} that the node is always there, hence
% always consumes space that (foo) is always available.
%
% The actual command that implements the invisibility can be overriden
% by altering the style invisible. For instance \tikzsset{invisible/.style={opacity=0.2}}
% would dim the ``invisible'' parts. Alternatively, the color might be set to white, if the
% output driver does not support transparencies (e.g., PS) 
%
% Reference: http://tex.stackexchange.com/a/55849/16933
%
\tikzset{
    invisible/.style={opacity=0},
    visible on/.style={alt=#1{}{invisible}},
    alt/.code args={<#1>#2#3}{%
        \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
    },
}

\tikzstyle{ps.block} = [draw, rectangle, minimum size=1cm, fill=olive!60]
\tikzstyle{ps.pin} = [pin distance=2mm, pin edge={->}]
\tikzstyle{best.ps.block} = [draw, rectangle, minimum width=5cm, minimum height=1cm, fill=olive!60]

\begin{frame}[fragile]{Test Frame}
    \begin{block}{Test block}
        \begin{itemize}
            \item xxx
            \item yyy
        \end{itemize}
    \end{block}
    \begin{tikzpicture}
        \node [ps.block, pin={[style=ps.pin]above:$IPC_{1}$}] (ps1) {$ps_{1}$};
        \node [ps.block, pin={[style=ps.pin]above:$IPC_{2}$}, right of=ps1, visible on=<2->] (ps2) {$ps_{2}$};
        \node [ps.block, right of=ps2, visible on=<3->] (psdots) {$\ldots$};
        \node [ps.block, pin={[style=ps.pin]above:$IPC_{n}$}, right of=psdots, visible on=<3->] (psn) {$ps_{n}$};
        \node [best.ps.block, right of=psn, visible on=<4->] (psbest) {$ps_{best}$};
    \end{tikzpicture}
\end{frame}

\end{document}

有谁知道如何正确解决这个问题?

简化版本:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows}

\begin{document}

\tikzstyle{ps.block} = [draw, rectangle, minimum size=1cm, fill=olive!60]
\tikzstyle{ps.pin} = [pin distance=2mm, pin edge={->}]
\tikzstyle{best.ps.block} = [draw, rectangle, minimum width=3cm, minimum height=1cm, fill=olive!60]

\begin{frame}[fragile]{Test Frame}
    \begin{block}{Test block}
        \begin{itemize}
            \item xxx
            \item yyy
        \end{itemize}
    \end{block}
    \begin{tikzpicture}
        \node [ps.block, pin={[style=ps.pin]above:$IPC_{1}$}] (ps1) {$ps_{1}$};
        \node [ps.block, pin={[style=ps.pin]above:$IPC_{2}$}, right of=ps1] (ps2) {$ps_{2}$};
        \node [ps.block, right of=ps2] (psdots) {$\ldots$};
        \node [ps.block, pin={[style=ps.pin]above:$IPC_{n}$}, right of=psdots] (psn) {$ps_{n}$};
        \node [best.ps.block, right of=psn] (psbest) {$ps_{best}$};
    \end{tikzpicture}
\end{frame}

\end{document}

答案1

只需使用right ofnode distance其默认值为1cm and 1cm)来计算节点中心之间的分离,因此最后一个较宽节点的中心将1cm远离节点的中心ps_n,从而产生重叠。

为了防止这种情况,一种选择是使用positioning库和right= <length> of语法:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes, arrows}

\begin{document}

\tikzstyle{ps.block} = [draw, rectangle, minimum size=1cm, fill=olive!60]
\tikzstyle{ps.pin} = [pin distance=2mm, pin edge={->}]
\tikzstyle{best.ps.block} = [draw,rectangle, minimum width=5cm, minimum height=1cm, fill=olive!60]

\begin{frame}[fragile]{Test Frame}
    \begin{tikzpicture}[node distance=1cm]
        \node [ps.block, pin={[style=ps.pin]above:$IPC_{1}$}] (ps1) {$ps_{1}$};
        \node [ps.block, pin={[style=ps.pin]above:$IPC_{2}$}, right = -\pgflinewidth of ps1] (ps2) {$ps_{2}$};
        \node [ps.block, right = -\pgflinewidth of ps2] (psdots) {$\ldots$};
        \node [ps.block, pin={[style=ps.pin]above:$IPC_{n}$}, right = -\pgflinewidth  of psdots] (psn) {$ps_{n}$};
        \node [best.ps.block, right = -\pgflinewidth of psn] (psbest) {$ps_{best}$};
    \end{tikzpicture}
\end{frame}

\end{document}

在此处输入图片描述

也许使用on grid节点选项更简单:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes, arrows}

\begin{document}

\tikzstyle{ps.block} = [draw, rectangle, minimum size=1cm, fill=olive!60]
\tikzstyle{ps.pin} = [pin distance=2mm, pin edge={->}]
\tikzstyle{best.ps.block} = [draw, rectangle, minimum width=3cm, minimum height=1cm, fill=olive!60]

\begin{frame}[fragile]{Test Frame}
    \begin{block}{Test block}
        \begin{itemize}
            \item xxx
            \item yyy
        \end{itemize}
    \end{block}
    \begin{tikzpicture}[every node/.style={on grid}]
        \node [ps.block, pin={[style=ps.pin]above:$IPC_{1}$}] (ps1) {$ps_{1}$};
        \node [ps.block, pin={[style=ps.pin]above:$IPC_{2}$}, right = of ps1] (ps2) {$ps_{2}$};
        \node [ps.block, right of=ps2] (psdots) {$\ldots$};
        \node [ps.block, pin={[style=ps.pin]above:$IPC_{n}$}, right = of psdots] (psn) {$ps_{n}$};
        \node [best.ps.block, right = 2cm of psn] (psbest) {$ps_{best}$};
    \end{tikzpicture}
\end{frame}

\end{document}

相关内容