我有一棵树(见示例),其中第 1 级的文本未对齐,因为其中一个文本使用了一些带指数的数学表达式(即$\mathtt{int}^2$
)。有没有什么办法可以让位于树同一级别的所有文本对齐?
\documentclass{book}
\usepackage{tikz}
\begin{document}
{\tiny
\begin{tikzpicture}
\begin{scope}[level distance=6mm]
\tikzstyle{level 1}=[sibling distance=8mm]
\node (1){$\mathtt{atom}$}
child {node (2){$\mathtt{atom}$}}
child {node (3){$\mathtt{int}^2$}}
child {node (4){$\mathtt{collection}$}
child {node (5){$\mathtt{int}$}}};
\end{scope}
\end{tikzpicture}
}
\end{document}
答案1
您可以简单地添加every node/.append style={anchor=base}
,以便所有节点都与其基点对齐
代码
\documentclass{book}
\usepackage{tikz}
\begin{document}
{\tiny
\begin{tikzpicture}
\begin{scope}[level distance=6mm,every node/.append style={anchor=base}]
\tikzstyle{level 1}=[sibling distance=8mm]
\node (1){$\mathtt{atom}$}
child {node (2){$\mathtt{atom}$}}
child {node (3){$\mathtt{int}^2$}}
child {node (4){$\mathtt{collection}$}
child {node (5){$\mathtt{int}$}}};
\end{scope}
\end{tikzpicture}
}
\end{document}
输出
答案2
使用anchor=base
钥匙打开树。
例子
\documentclass{book}
\usepackage{tikz}
\begin{document}
{\tiny
\begin{tikzpicture}
\begin{scope}[level distance=6mm, anchor=base]
\tikzstyle{level 1}=[sibling distance=8mm]
\node (1){$\mathtt{atom}$}
child {node (2){$\mathtt{atom}$}}
child {node (3){$\mathtt{int}^2$}}
child {node (4){$\mathtt{collection}$}
child {node (5){$\mathtt{int}$}}};
\end{scope}
\end{tikzpicture}
}
\end{document}