TransWikia.com

Set phantom{-} for all columns of a matrix that has at least one negative entry

TeX - LaTeX Asked by Jason1923 on March 1, 2021

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
defenv@matrix[#1]{hskip -arraycolsep
    let@ifnextcharnew@ifnextchar
    array{*c@MaxMatrixCols #1}}
@tfor@temp:=matrixpmatrixbmatrixBmatrixvmatrixVmatrixdo
{expandafterpatchcmd@temp
{env@matrix}
{@ifnextchar[
env@matrix{env@matrix[C]}}
{}{FAILED}}
newcolumntype{C}{>{CheckSign}c}
defCheckSignignorespaces{%
@ifnextchar-{}{@ifnextchar+{}{phantom{-}}}}
makeatother

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

Output of my working example:

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