How to align node labels in tikz vertically if one of the labels has a superscript?
One way is to use [anchor=base]
and add a \vphantom{X}
node on the left to get the correct vertical spacing and a \hphantom{\widetilde{X}
to get the correct horizontal spacing:
\node [anchor=base] at (0,0) {$\widetilde{X}$};
\node [anchor=base] at (0,0) (input) {$\vphantom{X}\hphantom{\widetilde{X}}$};
which yields:
Note:
- I replaced the
\tikzstyle
with\tikzset
which should be used instead: Should \tikzset or \tikzstyle be used to define TikZ styles?
Code:
\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}
Another possibilities: defining text height
and 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}