![Tikz 节点锚点和对齐](https://linux22.com/image/473235/Tikz%20%E8%8A%82%E7%82%B9%E9%94%9A%E7%82%B9%E5%92%8C%E5%AF%B9%E9%BD%90.png)
在 中tikz
,我有一个node
带有 的指定text width
,因此文本可以分成多行。我还有另一个圆圈,里面有一些文本。
我希望圆圈像第二张图片那样对齐,但是当文本包含多行时,我希望它保持与第一行的中心对齐。
代码如下
\documentclass[aspectratio=169]{beamer}
\usepackage[english]{babel}
\usepackage{tikz}
\begin{document}
\begin{frame}[t]
\begin{minipage}[t]{.2\textwidth}
{\begin{tikzpicture}[baseline={(current bounding box.north)}]
\node[white, text width=.8\paperheight-4.4ex,rotate=90, fill=blue!80!black, inner sep=2.2ex, anchor=mid east]at (0,0){\vphantom{X}\parbox[t]{\dimexpr\linewidth-4.4ex\relax}{Text in the node}};
\node[anchor=center,white] at (0,-1.5*2.2ex) {1};
\draw[white](0,0) --(0,-\paperwidth);
\draw[white] (0,-1.5*2.2ex) circle (2.2ex);
\end{tikzpicture}}
\end{minipage}
\begin{minipage}[t]{.2\textwidth}
{\begin{tikzpicture}[baseline={(current bounding box.north)}]
\node[white, text width=.8\paperheight-4.4ex,rotate=90, fill=blue!80!black, inner sep=2.2ex, anchor=east]at (0,0){\vphantom{X}\parbox[t]{\dimexpr\linewidth-4.4ex\relax}{Text in the node}};
\node[anchor=center,white] at (0,-1.5*2.2ex) {1};
\draw[white](0,0) --(0,-\paperwidth);
\draw[white] (0,-1.5*2.2ex) circle (2.2ex);
\end{tikzpicture}}
\end{minipage}%
\begin{minipage}[t]{.2\textwidth}
{\begin{tikzpicture}[baseline={(current bounding box.north)}]
\node[white, text width=.8\paperheight-4.4ex,rotate=90, fill=blue!80!black, inner sep=2.2ex, anchor=east]at (0,0){\vphantom{X}\parbox[t]{\dimexpr\linewidth-4.4ex\relax}{Node with very very very very very very long text}};
\node[anchor=center,white] at (0,-1.5*2.2ex) {1};
\draw[white](0,0) --(0,-\paperwidth);
\draw[white] (0,-1.5*2.2ex) circle (2.2ex);
\end{tikzpicture}}
\end{minipage}
\end{frame}
\end{document}
如果我使用anchor=mid east
圆圈向右移动一点,因为节点固定在高度x
,而inner sep
考虑大写字母的高度。如果我使用anchor=east
一切都正常工作,除非节点有多行。我想找到一种解决方案,即使我更改节点中的字体或字体大小,也可以工作,而无需每次都修改参数。
答案1
我稍微调整了一下您的代码,在文本节点中添加了“anchor=base east”,这似乎实现了您要求的功能(我认为)。新节点显示为红色,以区别于原始节点。
\documentclass[aspectratio=169]{beamer}
\usepackage[english]{babel}
\usepackage{tikz}
\begin{document}
\begin{frame}[t]
\begin{minipage}[t]{.2\textwidth}
{\begin{tikzpicture}[baseline={(current bounding box.north)}]
\node[white, text width=.8\paperheight-4.4ex,rotate=90, fill=blue!80!black, inner sep=2.2ex, anchor=mid east]at (0,0){\vphantom{X}\parbox[t]{\dimexpr\linewidth-4.4ex\relax}{Text in the node}};
\node[anchor=center,white] at (0,-1.5*2.2ex) {1};
\draw[white](0,0) --(0,-\paperwidth);
\draw[white] (0,-1.5*2.2ex) circle (2.2ex);
\end{tikzpicture}}
\end{minipage}%
\begin{minipage}[t]{.2\textwidth}
{\begin{tikzpicture}[baseline={(current bounding box.north)}]
\node[white, text width=.8\paperheight-4.4ex,rotate=90, fill=blue!80!black, inner sep=2.2ex, anchor=east]at (0,0){\vphantom{X}\parbox[t]{\dimexpr\linewidth-4.4ex\relax}{Text in the node}};
\node[anchor=center,white] at (0,-1.5*2.2ex) {1};
\draw[white](0,0) --(0,-\paperwidth);
\draw[white] (0,-1.5*2.2ex) circle (2.2ex);
\end{tikzpicture}}
\end{minipage}%
\begin{minipage}[t]{.2\textwidth}
{\begin{tikzpicture}[baseline={(current bounding box.north)}]
\node[white, text width=.8\paperheight-4.4ex,rotate=90, fill=blue!80!black, inner sep=2.2ex, anchor=east]at (0,0){\vphantom{X}\parbox[t]{\dimexpr\linewidth-4.4ex\relax}{Node with very very very very very very long text}};
\node[anchor=center,white] at (0,-1.5*2.2ex) {1};
\draw[white](0,0) --(0,-\paperwidth);
\draw[white] (0,-1.5*2.2ex) circle (2.2ex);
\end{tikzpicture}}
\end{minipage}%
\begin{minipage}[t]{.2\textwidth}
{\begin{tikzpicture}[baseline={(current bounding box.north)}]
\node[white, text width=.8\paperheight-4.4ex,rotate=90, fill=red!80!black, inner sep=2.2ex, anchor=base east]at (0,0){\vphantom{X}\parbox[t]{\dimexpr\linewidth-4.4ex\relax}{Node with very very very very very very long text}};
\node[anchor=center,white] at (0,-1.5*2.2ex) {1};
\draw[white](0,0) --(0,-\paperwidth);
\draw[white] (0,-1.5*2.2ex) circle (2.2ex);
\end{tikzpicture}}
\end{minipage}%
\begin{minipage}[t]{.2\textwidth}
{\begin{tikzpicture}[baseline={(current bounding box.north)}]
\node[white, text width=.8\paperheight-4.4ex,rotate=90, fill=red!80!black, inner sep=2.2ex, anchor=base east]at (0,0){\vphantom{X}\parbox[t]{\dimexpr\linewidth-4.4ex\relax}{Node with normal text}};
\node[anchor=center,white] at (0,-1.5*2.2ex) {1};
\draw[white](0,0) --(0,-\paperwidth);
\draw[white] (0,-1.5*2.2ex) circle (2.2ex);
\end{tikzpicture}}
\end{minipage}
\end{frame}
\end{document}
答案2
你可以尝试
text height=1.75ex,
text depth=.5ex,
minimum height=8ex
平均能量损失
\documentclass[aspectratio=169]{beamer}
\usepackage[english]{babel}
\usepackage{tikz}
\tikzset{
mult/.style={draw, white, circle, minimum size=6mm,}}
\begin{document}
\begin{frame}[t]
\begin{minipage}[t]{.2\textwidth}
{\begin{tikzpicture}[baseline={(current bounding box.north)}]
\node[
white,
text width=.8\paperheight-4.4ex,
rotate=90,
fill=blue!80!black,
inner sep=2.2ex,
anchor=east,
% text height=1.75ex,
text depth=.1ex,
minimum height=8ex
](q)
at
(0,0){{Text in the\\ node}};
\node[mult, anchor=center, ](p) at (0,-1.5*2.2ex) {1};
\draw[ white] (q.east) -- (q.west);
\end{tikzpicture}}
\end{minipage}%
\end{frame}
\end{document}