You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将不同高度的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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 14:25:08