TransWikia.com

Alignment for nested matrices

TeX - LaTeX Asked on May 31, 2021

I want to get a matrix of arrays, and I cannot make it aligned. I tried blkarray and bmatrix, but none of them works for me. Could somebody help me out?
Code below is what I wrote:

a = left[ 
begin{array}{c@{}c@{}c}
    left[begin{array}{ccc}
      255 & 0 & 0 
    end{array}right] & left[begin{array}{ccc}
    0 & 94.5 & 94.5 
    end{array}right] 
    left[begin{array}{ccc}
    0 & 99 & 0 
    end{array}right] & left[begin{array}{ccc}
    0 & 0 & 9 
    end{array}right] 
end{array}right]
end{equation*}

And it turn out to be misaligned:

enter image description here

4 Answers

With nicematrix package:

documentclass[margin=3mm, varwidth]{standalone}
usepackage{nicematrix}

begin{document}
    [
a = 
begin{bNiceArray}{c@{}ccc@{}c c@{}ccc@{}c}
      [ & 255 & 0  & 0 &]   &   [& 0 & 94.5 & 94.5 &]   
      [ &   0 & 99 & 0 &]   &   [& 0 & 0    & 9    &]   
end{bNiceArray}
]
end{document}

enter image description here

Addendum: By use of CodeAfter and SubMatrix (you need nicematrix version 5.10 or newer):

documentclass[margin=3mm, varwidth]{standalone}
usepackage{nicematrix}

begin{document}
[
a =
begin{bNiceArray}{ccc@{hspace{1em}}ccc}[margin]
      255 & 0  & 0   &   0 & 94.5 & 94.5 [1ex]
        0 & 99 & 0   &   0 & 0    & 9    
CodeAfter
SubMatrix[{1-1}{1-3}] SubMatrix[{1-4}{1-6}]
SubMatrix[{2-1}{2-3}] SubMatrix[{2-4}{2-6}]
end{bNiceArray}
]
end{document}

enter image description here

Correct answer by Zarko on May 31, 2021

Just for the sake of variety, here's a solution that employs the machinery of the siunitx package -- specifically, the S column type -- to format the inner array environments. Observe that the numbers are not centered but are aligned on the (explicit or implicit) decimal markers.

enter image description here

documentclass{article} % or some other suitable document class
usepackage{amsmath} % for 'bmatrix' env.
usepackage{siunitx} % for 'S' column type
newcolumntype{T}[1]{S[table-format=#1]}

begin{document}
[
a = begin{bmatrix} 
    left[begin{array}{@{} T{3.0} T{2.0} c @{}} % array #1
       255 & 0 & 0 
    end{array}right] & 
    left[begin{array}{@{} c T{2.1} T{2.1} @{}} % array #2
       0 & 94.5 & 94.5 
    end{array}right] [1.5jot]
    left[begin{array}{@{} T{3.0} T{2.0} c @{}} % same format as array #1
       0 & 99 & 0 
    end{array}right] & 
    left[begin{array}{@{} c T{2.1} T{2.1} @{}} % same format as array #2
       0 & 0 & 9 
    end{array}right]
end{bmatrix}
]
end{document}

Answered by Mico on May 31, 2021

There is another solution without nicematrix, only with plain TeX tools:

$$
  def[{[&kern-.9em } def]{kern-.9em&]}
  left[matrix{
      [ 255 & 0  & 0 ]   &   [ 0 & 94.5 & 94.5 ]   cr
      [   0 & 99 & 0 ]   &   [ 0 & 0    & 9    ]   cr
  }right]
$$

Answered by wipet on May 31, 2021

With spalign package with several...adjustments.....

documentclass[a4paper,12pt]{article}
usepackage{amsmath,amssymb}
usepackage{spalign}

begin{document}
[ a= spaligndelims{[}{]} 
spalignmat[c]{spalignmat[c]{255 0 0} spalignmat[c]{0 94.5 94.5}[-1em]; spalignmat[c]{phantom{0}0 99 0} spalignmat[c]{0phantom{mkern1mu 0} 0phantom{00} phantom{0}9phantom{0}}[.3em]}
]

end{document}

enter image description here

Answered by Sebastiano on May 31, 2021

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