10

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?

4
  • 3
    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.
    – egreg
    Oct 6 '20 at 10:07
  • It's not a table. It's a fairly small (4x4) matrix.
    – mbork
    Oct 6 '20 at 15:58
  • It makes no difference. Center the numbers and be happy.
    – egreg
    Oct 6 '20 at 16:00
  • But then it looks terrible.
    – mbork
    Oct 7 '20 at 5:04
9

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}

enter image description here

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

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}

Output of the above code

3

The \phantom can be used:

\def\0{\phantom{0}} \def\+{\phantom{+}}
$$
  \pmatrix{ 1.41\0 &  -1.73 \cr
            2.718  & \+3.14 }
$$
1
  • 1
    Well, in general you are right, but I explicitly stated that this approach won't work for me in this case.
    – mbork
    Oct 6 '20 at 6:55
3

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

\documentclass{article}

\usepackage{mathtools}
\usepackage{siunitx}

\begin{document}

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

\end{document}

output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.