# \hphantom with \footnotesize, siunitx and unicode-math

Consider the following MWE. It is supposed to print the three equations with space added after the equal signs so that the right edge of the equations (the units) are horizontally aligned.

% !TeX program = lualatex
\documentclass{scrartcl}

\usepackage{fontspec}
\usepackage{unicode-math}

\setmainfont{STIX2Text-Regular}
\setmathfont{STIX2Math}

\usepackage{mathtools}
\usepackage{siunitx}

\begin{document}

\footnotesize

$$X = \SI{4.8}{\henry}$$

$$X = \hphantom{.} \SI{10}{\henry}$$

$$X = \hphantom{0.} \SI{6}{\henry}$$

\end{document}


The alignment of the last equation is clearly off, while the second one seems right. (I'm just eyeballing this, though. Didn't do any precise measurements.)

I played around a bit while creating this MWE and this seems to only happen in \footnotesize, with \SI (if I just type out the numbers, it's fine) and with unicode-math (though I'm less sure about this one). An offset is also present with the default Latin Modern fonts, but it is much easier to spot with the Stix2 fonts, which is why I used those here.

What is the reason for this behavior? How can I avoid it?

The widths of the digits are not the same in STIX Two Math at \footnotesize. Why? Ask STIX.

\documentclass{scrartcl}

\usepackage{fontspec}
\usepackage{unicode-math}

\setmainfont{STIX Two Text}
\setmathfont{STIX Two Math}

\usepackage{mathtools}
\usepackage{siunitx}

\begin{document}

\footnotesize

$$X = \SI{4.8}{\henry}$$

$$X = \hphantom{.} \SI{10}{\henry}$$

$$X = \hphantom{0.} \SI{6}{\henry}$$

$$X = \hphantom{1.} \SI{6}{\henry}$$

\sbox0{$1$}\the\wd0

\sbox0{1}\the\wd0

\sbox0{$0$}\the\wd0

\sbox0{0}\the\wd0

\end{document}


With this code, the drift is very small, but still noticeable. Why?

You should be aware that, by default, siunitx uses text mode digits, so your phantom doesn't get the desired width, because it uses math mode digits.

However, the period is taken from the math font. If you use

\footnotesize

$$X = \SI{4.8}{\henry}$$

$$X = \hphantom{.} \SI{10}{\henry}$$

$$X = \hphantom{\mbox{0}.} \SI{6}{\henry}$$

$$X = \hphantom{\mbox{1}.} \SI{6}{\henry}$$


you get

Why not using an alignment?

\footnotesize

\begin{alignedat}{2} X &={} &\SI{4.8}{\henry}\\ X &={} &\SI{10}{\henry}\\ X &={} &\SI{6}{\henry} \end{alignedat}


Changing it to use 0.6 in all cases

\Si uses text mode

......\hbox(5.814+0.108)x11.475, direction TLT
.......\TU/STIXTwoText(1)/m/n/9 0
.......\TU/STIXTwoMath(1)/m/n/9 .
.......\TU/STIXTwoText(1)/m/n/9 6


but \hphantom is using math mode

.....\hbox(6.021+0.117)x7.785, direction TLT
......\TU/STIXTwoMath(1)/m/n/9 0
......\TU/STIXTwoMath(1)/m/n/9 .


and as you see the widths are different

\documentclass{scrartcl}

\usepackage{fontspec}
\usepackage{unicode-math}

\setmainfont{STIX Two Text}
\setmathfont{STIX Two Math}

\makeatletter
\def\finph@nt{%
\setbox\tw@\null
\ifv@ \ht\tw@\ht\z@ \dp\tw@\dp\z@\fi
\ifh@ \wd\tw@\wd\z@\fi
\leavevmode@ifvmode\box\z@}

\usepackage{siunitx}

\begin{document}

\footnotesize
\showoutput

|$$X = \SI{0.6}{\henry}$$|

|$$X = \hphantom{0.} \SI{6}{\henry}$$|

|$X=0.6\mathrm{H}$|

|$X=\hbox{$0.$}6\mathrm{H}$|

|$X=\hphantom{0.}6\mathrm{H}$|

\end{document}