# Nodes being covered when using “right of” for placing nodes in TikZ

I am using Beamer and TikZ for creating some slides, and I am having some problems with positioning nodes. I incrementally display some nodes, and position them using [right of=previousnode]. On the 3rd slide everything still looks fine:

But when I add the next node to the right of the last one, the node is not positioned in the right place:

As you can see it ends up covering some of the previous nodes. Here you have a minimal reproducible example:

EDIT: I have just realized that the part related to displaying/hiding the nodes depending on the slide number, is not related with the covering issue. I added at the end of the question a simplified example where the problem still persists. I am keeping the original example, but you may prefer to directly look at that simplified one.

\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}


Does anyone know how to properly fix this problem?

Simplified version:

\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}

-

Simply using right of uses node distance (whose default value is 1cm and 1cm) to calculate the separation between the center of the nodes, so the center of the last wider node will be 1cm away from the center of the ps_n node and this produced the overlapping.

To prevent this, one option is to use the positioning library and the right= <length> of syntax:

\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}


Perhaps is simpler to use the on grid option for the nodes:

\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}

-
Thanks for the solution! Out of curiosity, I wonder why just using "right of" was not working. Do you know the reason? –  betabandido Sep 15 '12 at 17:26
@betabandido Simply using right of uses node distance (whose default value is 1cm and 1cm) to calculate the separation between the center of the nodes, so the center of the last wider node will be 1cm away from the ps_n node and this produced the overlapping. By the way, I updated my answer with another simpler option. –  Gonzalo Medina Sep 15 '12 at 17:50