如何将不同高度的TikZ节点设置为底部对齐?
TikZ实现不同高度栈形节点底部对齐
以下是原代码,它生成的两个栈形节点为垂直居中对齐:
\documentclass{article} \usepackage{tikz} \usetikzlibrary{shapes.multipart,chains} \begin{document} \begin{figure} \centering \begin{tikzpicture}[stack/.style={rectangle split, rectangle split parts=#1,draw, anchor=base}] \node (s4) [stack=4] { \nodepart{one}12 \nodepart{two}5 \nodepart{three}97 \nodepart{four}86 }; \node (s5) [stack=5, right=of s4] { \nodepart[]{one}42 \nodepart{two}12 \nodepart{three}5 \nodepart{four}97 \nodepart{five}86 }; \end{tikzpicture} \caption{Two stacks.} \end{figure} \end{document}
要实现底部对齐,只需修改两处:
- 将栈样式中的
anchor=base替换为anchor=south,把节点的定位基准设为底部 - 第二个节点定位时,明确以第一个节点的右下角为参照,自身左下角对齐,将
right=of s4改为right=of s4.south east, anchor=south west
修改后的完整代码:
\documentclass{article} \usepackage{tikz} \usetikzlibrary{shapes.multipart,chains} \begin{document} \begin{figure} \centering \begin{tikzpicture}[stack/.style={rectangle split, rectangle split parts=#1,draw, anchor=south}] \node (s4) [stack=4] { \nodepart{one}12 \nodepart{two}5 \nodepart{three}97 \nodepart{four}86 }; \node (s5) [stack=5, right=of s4.south east, anchor=south west] { \nodepart[]{one}42 \nodepart{two}12 \nodepart{three}5 \nodepart{four}97 \nodepart{five}86 }; \end{tikzpicture} \caption{Two stacks (bottom-aligned).} \end{figure} \end{document}
原理说明
anchor=south:将节点的基准点设定为矩形底部中点,确保节点自身的定位锚点落在底部right=of s4.south east, anchor=south west:让第二个节点的左下角与第一个节点的右下角对齐,实现两个节点的底部完全对齐
内容的提问来源于stack exchange,提问作者jkb
相关产品推荐
相关产品推荐

