如何将节点“s2”定位在“s1”上方,以便最左边的 0(节点“s2”)正好位于“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}
答案1
一种方法是将节点锚定在西南方向(如您所做的那样),然后将其向左移动必要的量以对齐第一个零:
\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}
如果您希望箭头垂直:
\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}
答案2
试试这个。我把包含文本的节点分成了两个,这样就可以根据需要定位它们。注意\smallskipamount
对应于零之间的空格。
代码:
\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}
结果: