# How can I vertically align an equals sign in a tikz node?

I want to align an equals sign in a tikz node like in the third node of this picture:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}

\newlength\eqheight
\settoheight\eqheight{$=$}

\begin{document}
\begin{tikzpicture}[every node/.style={minimum height=1em,rectangle,draw}]
\node at (0, 0) {$=$};
\node at (1, 0) {$A=$};
\node at (2, 0) {\raisebox{0pt}[\eqheight][0pt]{\raisebox{-1pt}{$=$}}};
\end{tikzpicture}
\end{document}


In the first node the equals sign is not vertically aligned correctly, because its box has more space at the bottom than at the top.

When you add the character "A", the box is higher and thus the equals sign will be aligned correctly.

In the third node I manually raise the box by -1pt, but I don't want to "hard code" the -1pt.

I tried to get the -1pt by using \settodepth to get the depth of the equals sign but that appears to be 0. Is there a way to align it correctly without "hard coding" the value?

• Welcome to TeX.SX! There is no way around manual adjustment. The reason is that TeX can't look into the box of the character, but just knows its height, width, and depth. And the height of = is measured from the baseline, including white space below the sign itself. Tex doesn't know whether the stuff inside the box extends to its borders or not; for TeX there is no difference between '=' and o, even though the first has lots of white space, whereas the second even overshoots its box. – gernot Feb 25 '17 at 14:01
• @gernot good to know, thanks for the answer! – Moritz S. Feb 25 '17 at 14:11

Two issues:

• The text nodes should be centered at their base lines (or math axis lines).
• Equal space above and below the symbols.

The first issue can be solved by anchoring the nodes at their base lines. The second issue is trickier, because TeX does not know, where the black pixels of a glyph are. It does only know the nominal glyph bounding box:

Created with:

\documentclass{standalone}
\usepackage{color}
\setlength{\fboxsep}{0pt}
\setlength{\fboxrule}{.2pt}
\begin{document}
\def\x#1{%
\textcolor{red}{%
\fbox{%
$\color{black}#1$%
}%
}%
}
$\x{=}\;\x{A}$
\end{document}


The glyph bounding box always includes the base line. Symbols above the base line have additional space below to the base line, symbols above the base line as the underscore have additional space above.

The equals sign is centered around the math axis. This can be used to calculate the white space:

First, the symbol is moved to the base line. Then the height of the symbol is half its net height. Because of the symbol's symmetry, the depth has the same amount as the height.

Then, TikZ gets this symbol and can draw a symmetric border.

Finally, the former shift needs to be revered by moving the node up by the amount of the math axis.

Example file:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
every node/.style={
rectangle,
anchor=base,
draw,
}]
\pgfmathsetmacro\MathAxis{height("$\vcenter{}$")}
\node (a) at (0, \MathAxis pt) {%
\sbox0{$=$}%
\sbox0{\raisebox{-\MathAxis pt}{\usebox0}}% Equals sign at base line
\dp0=\ht0 % The equals sign is symmetric
\usebox0%
};
\node (b) at (1, 0) {$A=$};

% Red line at math axis
\draw[thin, red, yshift=\MathAxis, opacity=.5]
(a.west |- 0, 0) -- (b.east |- 0, 0);
\end{tikzpicture}
\end{document}


# Symbol Join of package wasysym

There are two problems with the \Join symbol of package wasysym:

• The reported glyph bounding box has a wrong height (seems to be a bug).
• It is not vertically centered at the math axis but at the upper bar of the equals sign. Maybe, the font designer wanted to avoid that the symbol descends the base line.

The font for the equals sign is cmr10.pfb, the font for the symbol \Join of package wasysym is wasy10.pfb. Both fonts have the same em size of 1000 and the same math axis is at 250. The height of the \Join glyph is 637 and its center is at 342 and the depth is -47 (above the baseline). These values can be looked up in FontForge, for example.

Then the formula for the shift is:

<math axis in TeX> * <symbol center in glyph units> / <math axis in glyph units>
= <math axis in TeX> * 347 / 250


The half height of the symmetric symbol is:

<math axis in TeX units> * (<height in glyph units> - <center in glyph units>) / <math axis in glyph units>
= <math axis in TeX> * (637 - 342) / 250
= <math axis in TeX> * 295 / 250


Full example:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usepackage{wasysym}
\pagestyle{empty}

\begin{document}
\begin{tikzpicture}[
every node/.style={
rectangle,
anchor=base,
draw,
}]
\pgfmathsetmacro\MathAxis{height("$\vcenter{}$")}
\edef\JoinShift{\the\dimexpr\MathAxis pt * 347 / 250}
\node (a) at (0, \JoinShift) {%
\sbox0{\raisebox{-\JoinShift}{$\Join$}}%
\dp0=\dimexpr\MathAxis pt * 295 / 250\relax
\ht0=\dp0 %
\usebox0%
};
\node (b) at (1, 0) {$A=\Join$};

% Red line at math axis
\draw[thin, red, yshift=\MathAxis, opacity=.5]
(a.west |- 0, 0) -- (b.east |- 0, 0);
\end{tikzpicture}
\end{document}


• As you can see in your picture, this doesn't help with aligning within the node at all. – Moritz S. Feb 25 '17 at 14:11
• @MoritzS. I had aligned the vertical position of the equal signs across the nodes. The updated answer also takes care for symmetric inner margins of the left node. – Heiko Oberdiek Feb 25 '17 at 14:48
• This works now for the equals sign. I also tried it with \Join from the wasysym package but that doesn't work. \fbox{$\Join$} with 0pt fboxsep looks like this. Is this a bug in the wasysym package? – Moritz S. Feb 25 '17 at 15:43
• @MoritzS. The symbol \Join requires more work, see updated answer. – Heiko Oberdiek Feb 25 '17 at 16:31

You could simply add a \vphantom.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}

\newlength\eqheight
\settoheight\eqheight{$=$}

\begin{document}
\begin{tikzpicture}[every node/.style={minimum height=1em,rectangle,draw}]
\node at (0, 0) {\vphantom{A}$=$};
\node at (1, 0) {$A=$};
\node at (2, 0) {\raisebox{0pt}[\eqheight][0pt]{\raisebox{-1pt}{$=$}}};
\end{tikzpicture}
\end{document}

• But then the box for the equals sign gets so large. I'd prefer to use the size of the original box. – Moritz S. Feb 25 '17 at 13:04