# How to align numbers in a matrix by decimal point?

Here is a matrix:

\documentclass{article}

\usepackage{amsmath}

\begin{document}
\begin{equation*}
\begin{pmatrix}
1.41 & -1.73\\
2.718 & 3.14
\end{pmatrix}
\end{equation*}
\end{document}


As you can see, the number of decimal points may be different, and some numbers are negative. I'd like to have the columns aligned on the decimal points.

Normally, I'd use a half-em space for "missing" digits and a \hphantom- - but I use a font where the digits are not the same width, so that is ruled out.

I skimmed through TeX.SE, but only found answers for tables, not matrices. I have also looked in the docs of amsmath and mathtools, to no avail.

I suspect I could use TikZ matrices for that, but I'd prefer to stay simple if possible.

Any suggestions?

• Using proportional figures in a table is not the best service to your readers. Even if the decimal points are aligned, the numbers will be difficult to read anyway. Oct 6 '20 at 10:07
• It's not a table. It's a fairly small (4x4) matrix. Oct 6 '20 at 15:58
• It makes no difference. Center the numbers and be happy. Oct 6 '20 at 16:00
• But then it looks terrible. Oct 7 '20 at 5:04

With matrix is not possible (at least easy), try with array with S columns, which are defined in the siunitx package.

\documentclass{article}
\usepackage{siunitx}

\begin{document}
$\left(\begin{array}{@{} S[table-format=1.3]S[table-format=-1.2] @{}} 1.41 & -1.73\\ 2.718 & 3.14 \end{array}\right)$
\end{document}


• It's possible with matrix as updated by mathtools; just use S columntype. Oct 7 '20 at 16:35

With nicematrix and siunitx:

\documentclass{article}
\usepackage{nicematrix}
\usepackage{siunitx}

\begin{document}
$\begin{pNiceArray}{S[table-format=1.3]S[table-format=-1.2]} 1.41 & -1.73\\ 2.718 & 3.14 \end{pNiceArray}$
\end{document}


The \phantom can be used:

\def\0{\phantom{0}} \def\+{\phantom{+}}
$$\pmatrix{ 1.41\0 & -1.73 \cr 2.718 & \+3.14 }$$

• Well, in general you are right, but I explicitly stated that this approach won't work for me in this case. Oct 6 '20 at 6:55

The only modification needed to the original post is the use of the S columntype.

\documentclass{article}

\usepackage{mathtools}
\usepackage{siunitx}

\begin{document}

$$\label{eq:1} \begin{pmatrix*}[S] 1.41 & -1.73 \\ 2.718 & 3.14 \end{pmatrix*}$$

\end{document}