# Positioning a node relative to the center of another node in tikz

How can I position the node 's2' above 's1' so that leftmost 0 (of node 's2') is exactly above and at center of 's1'

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\begin{frame}
\begin{tikzpicture}[overlay,remember picture]
\node[draw,fill=cyan!40,minimum size=1.2cm,rectangle,yshift=.5cm] at (current page.center)
(s1) {q};

\node[draw=none,above=of s1,anchor=south west] (s2) {0 0 0 1 1 1};
\draw[->] (s1) -- (s2);
\end{tikzpicture}
\end{frame}
\end{document}

-

One way is to anchor the node to the south west (as you have done), then shift it to the left by the amount necessary to align the first zero:

\documentclass{beamer}
\usepackage{tikz}
\usepackage{calc}
\usetikzlibrary{positioning}
\begin{document}

\newlength{\zerooffset}
\begin{frame}
\begin{tikzpicture}[overlay,remember picture]
\node[draw,fill=cyan!40,minimum size=1.2cm,rectangle,yshift=.5cm] at (current page.center)
(s1) {q};
\setlength{\zerooffset}{\widthof{0}}%<=== get the width of the 0
\node[draw=none,above= of s1,anchor=south west,xshift={\dimexpr-0.5\zerooffset-\pgfkeysvalueof{/pgf/inner xsep}}] (s2) {0 0 0 1 1 1};
\draw[->] (s1) -- (s2);
\end{tikzpicture}
\end{frame}
\end{document}


And if you want the arrow to be vertical:

\documentclass{beamer}

\usepackage{tikz}
\usepackage{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\begin{document}

\newlength{\zerooffset}
\begin{frame}
\begin{tikzpicture}[overlay,remember picture]
\node[draw,fill=cyan!40,minimum size=1.2cm,rectangle,yshift=.5cm] at (current page.center)
(s1) {q};
\setlength{\zerooffset}{\widthof{0}}%<=== get the width of the 0
\node[draw=none,above= of s1,anchor=south west,xshift={\dimexpr-0.5\zerooffset-\pgfkeysvalueof{/pgf/inner xsep}}] (s2) {0 0 0 1 1 1};
\draw[->] let \p1=(s1.north),\p2=(s2.south) in (\x1,\y1)--(\x1,\y2);%<=== vertical arrow
\end{tikzpicture}
\end{frame}
\end{document}


-
+1 I think this is the only solution. Because in the original code the left edge of the node is aligned with the centre of the lower node, so aligning with something inside the node must be done (semi-)manually. – Alan Munn Feb 20 at 20:46
@AlanMunn: Given the flexibility of TeX in general, I'm always hesitant to say only. That being said, I agree that some degree of manual intervention is necessary for this effect. – Guho Feb 20 at 21:00
Yes, you're right, never say 'never'. :) (And a new answer has already appeared.) – Alan Munn Feb 20 at 21:01

Try this. I have split the node with the text into two, which allows to position them as required. Notice the \smallskipamount which corresponds to the space between the zeros.

Code:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{frame}
\begin{tikzpicture}[overlay,remember picture]
\node[draw,fill=cyan!40,minimum size=1.2cm,rectangle,yshift=.5cm] at (current page.center) (s1) {q};

\node[draw=none,above=of s1] (s2) {0};
\node[draw=none,right=\smallskipamount] at (s2) {0 0 1 1 1};

\draw[->] (s1) -- (s2);
\end{tikzpicture}
\end{frame}
\end{document}


Result:

-