[Tex/LaTex] Positioning a node relative to the center of another node in tikz

tikz-pgf

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}

Best Answer

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}

centered first zero

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}

centered with vertical arrow