TransWikia.com

How to align numbers in a matrix by decimal point?

TeX - LaTeX Asked on December 1, 2020

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 Answers

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

Answered by Zarko on December 1, 2020

The phantom can be used:

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

Answered by wipet on December 1, 2020

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

Answered by F. Pantigny on December 1, 2020

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

Answered by Máté Wierdl on December 1, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP