我在尝试垂直对齐两个节点的标签时遇到问题。其中一个标签有上标,因此它与另一个标签不在同一级别。
下面是用来解释我的意思的代码。
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\tikzstyle{sum} = [draw, shape=circle, node distance=1.5cm, line width=1pt, minimum width=1.5em]
%Creating Blocks and Connection Nodes
\node at (0,0) (input) {${\widetilde{X}}$};
\node [sum, right of=input] (sum) {};
\node at (sum) (plus) {{\footnotesize$+$}};
\node at (1.5,-1) (noise) {$Z$};
\node at (3,0) (output) {$X$};
%Lines
\draw[->] (input) -- (sum);
\draw[->] (noise) -- (sum.south);
\draw[->] (sum) -- (output);
\end{tikzpicture}
\end{document}
我想垂直对齐 $\wildetilde{X}$ 和 $X$。更确切地说,我希望将 $X$ 和 $\wildetilde{X}$ 都对齐到底部。谢谢!
答案1
一种方法是在左侧使用[anchor=base]
并添加一个节点以获得正确的垂直间距,并添加一个节点以获得正确的水平间距:\vphantom{X}
\hphantom{\widetilde{X}
\node [anchor=base] at (0,0) {$\widetilde{X}$};
\node [anchor=base] at (0,0) (input) {$\vphantom{X}\hphantom{\widetilde{X}}$};
得出的结果是:
笔记:
- 我用应该使用的
\tikzstyle
替换了:\tikzset
应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式?
代码:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\tikzset{sum/.style={draw, shape=circle, node distance=1.5cm, line width=1pt, minimum width=1.5em}}
%Creating Blocks and Connection Nodes
\node [anchor=base] at (0,0) {$\widetilde{X}$};
\node [anchor=base] at (0,0) (input) {$\vphantom{X}\hphantom{\widetilde{X}}$};
\node [sum, right of=input] (sum) {};
\node at (sum) (plus) {{\footnotesize$+$}};
\node at (1.5,-1) (noise) {$Z$};
\node [anchor=base] at (3,0) (output) {$X$};
%Lines
\draw[->] (input.east) -- (sum);
\draw[->] (noise) -- (sum.south);
\draw[->] (sum) -- (output);
\end{tikzpicture}
\end{document}
答案2
另一种可能性:定义text height
和text depth
:
\begin{document}
\begin{tikzpicture}
\tikzset{
node distance = 8mm and 12mm,
sum/.style = {shape=circle, draw, line width=1pt,
node contents={\huge$+$}},
N/.style = {text height=2ex, text depth=0.5ex}
}
% nodes
\node (in) [N] {$\widetilde{X}$};
\node (sum) [sum, right=of in];
\node (out) [N,right=of sum] {\vphantom{$\widetilde{X}$}$X$};
\node (noise) [below=of sum] {$Z$};
% lines
\draw[->] (in) -- (sum);
\draw[->] (noise) -- (sum);
\draw[->] (sum) -- (out);
\end{tikzpicture}
\end{document}