9

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

12

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

5
  • Awesome!!! Just a reminder for others, I cannot compile with this code for two things:1.texlive should be latest enough (at least 2019); 2. format letters shoule be capital ones ("L","R","C") – 吕和瑾 Mar 23 at 6:43
  • 1
    @吕和瑾, I used recent version of nicematrix: 5.11, from 2021/02/22 :-) – Zarko Mar 23 at 6:51
  • Sure, thanks for your help. – 吕和瑾 Mar 23 at 8:24
  • Can you share how did you install the version 5.11, from 2021/02/22 ? I tried updated nicematrix with tlmgr (package manager of TexLive). However, it updated the package into the latest one which seemed to have bugs. I have no idea how to update the package into the certain version. – 吕和瑾 Mar 24 at 7:27
  • @吕和瑾, I use MikTeX and upgrade nicematrix to v 5.11 by help of MikTeX Console a week or so ago. So far I didn't have any problems with it. – Zarko Mar 24 at 7:40
6

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.5\jot]
    \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}
4

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]
$$
1

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]{0\phantom{\mkern1mu 0} 0\phantom{00} \phantom{0}9\phantom{0}}\\[.3em]}
\]

\end{document}

enter image description here

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.