0

I would like to add \phantom{-} to every column of a matrix that contains at least one entry with a - in front. Here is an answer that my current code is based off. It inserts \phantom{-} in front of every entry that isn't negative, but that's not really what I am looking for.

My current attempt is attached below. Notice how in an array without negatives, it inserts phantoms in front of each entry, increasing the overall width.

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}
\usepackage{etoolbox}

\begin{document} [ \begin{bmatrix} 1 & 2 & 12345 \ 5 & 2 & 5 \ 0 & 1 & 1 \end{bmatrix} ]

% code that inserts \phantom{-} \makeatletter \def\env@matrix[#1]{\hskip -\arraycolsep \let@ifnextchar\new@ifnextchar \array{*\c@MaxMatrixCols #1}} @tfor@temp:=\matrix\pmatrix\bmatrix\Bmatrix\vmatrix\Vmatrix\do {\expandafter\patchcmd@temp {\env@matrix} {@ifnextchar[ \env@matrix{\env@matrix[C]}} {}{\FAILED}} \newcolumntype{C}{>{\CheckSign}c} \def\CheckSign\ignorespaces{% @ifnextchar-{}{@ifnextchar+{}{\phantom{-}}}} \makeatother

[ \begin{bmatrix} 1 & 2 & 12345 \ 5 & 2 & 5 \ 0 & 1 & 1 \end{bmatrix} ] \end{document}

Output of my working example:

Jason1923
  • 149
  • I would use right aligned columns, then the numbers are aligned on decimal places and the minus signs will automatically sort themselves out (mathtools package provides a variant of bmatrix where you can specify the alignment) – David Carlisle Dec 05 '20 at 12:32
  • I'm not sure you really want this: if a column contains, say 12, -2 and 1, adding the phantom - to the top entry would be too much. – egreg Dec 05 '20 at 14:10
  • @egreg That's a good point. What would you recommend to align negative numbers then? Would right aligned columns like David suggested be better than phantoms? – Jason1923 Dec 05 '20 at 16:55

0 Answers0