Two matrices one on top of the other in an equation

How to create two matrix one on top of the other in a similar way done here?

The problem of the proposed solution is that only one column matrix are allowed.

Here my code:

$$\Gamma = \begin{matrix} { } \\ AB \\ AC \\ CC \\ BD \\ CD \end{matrix} \begin{matrix} A & B & C & D \\ \begin{bmatrix} 3 & -1 & 0 & 0 \\ 1 & 0 & -1 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 2 & 0 & -3 \\ 0 & 0 & 4 & -2 \end{bmatrix} \end{matrix} \label{eq:networkMatrix}$$


Which gives me this result:

I would like to have A, B, C, and D aligned with the column. Any suggestion? Please note that I would like to continue to use equation so align is not allowed within it. Also, the use of nicematrix is not possible. Following the problem in details:

When using nicematrix the error prompt is:

Environment bNiceArray undefined. \begin{bNiceArray}
[...]
Misplaced alignment tab character &. &
[...]


Looking here and here, it seems that the problem can be the version of Latex I am using. So:

$sudo apt-get install texstudio ... texstudio is already the newest version (2.12.6+debian-2). ...  and $ sudo apt-get install texlive-full
...
texlive-full is already the newest version (2017.20180305-1).
...


And, if I try to manually install the program from the official website here, this is the result: Unable to install TexStudio and No software repo included.

The reason for this is here explained.

The conclusion is that I cannot use nicematrix on this workstation until I do not change OS.

@F.Pantigny, does this information match with yours? Do I need the newst TeXstudio version to use nicematrix? I think I will move this question to a new one.

• What you want is a "border matrix". Look here: \bordermatrix with brackets [ ] instead of parentheses – barbara beeton Apr 30 at 16:44
• wonderful solution!! It solves my problem. However what happens if I want to place two matrices on top of the other? :) I will keep this question open because I am curious. And also because a solution to this problem will solve also my problem. – Leos313 Apr 30 at 16:48
• @Leos313: As you see, your problem has nothing to do with nicematrix. You should try to uninstall TeXLive 2017 and, then, install TeXLive 2020. Then, try to install TeXStudio. You may also ask your question on AskUbuntu. – F. Pantigny May 1 at 14:19

You might be interested in nicematrix.

\documentclass{article}
\usepackage{nicematrix}
\begin{document}
$\Gamma= \begin{bNiceArray}{CCCC}[first-row,first-col] & A & B & C & D \\ AB &3 & -1 & 0 & 0 \\ AC& 1 & 0 & -1 & 0 \\ CC& 0 & 0 & 0 & 0 \\ BD& 0 & 2 & 0 & -3 \\ CD& 0 & 0 & 4 & -2\\ \end{bNiceArray}$
\end{document}


• thank but absolutely not. It breaks a lot (a lot, believe me) of code everywhere in the book. However, it can be a solution for someone else but not for me. Thank you (i have already the same code working in a separate latex project) :) – Leos313 Apr 30 at 16:49
• and, in any case, +1 :) – Leos313 Apr 30 at 16:55
• @Leos313: I'm the author ot the package nicematrix. I'm interested to know which code is broken by the use of nicematrix (except in the case of the option renew-matrix). – F. Pantigny Apr 30 at 16:58
• in this case, I will try to give you more details asap – Leos313 Apr 30 at 17:01
• @Leos313, so far I haven't any bad experiences with use nicematrix. You only need to compile your document twice. – Zarko Apr 30 at 17:01

Some progress can be made using arrays and a bit of fine tuning. Not an ideal solution, but given some of the constraints it seems palatable.

\documentclass{extarticle}

\usepackage{amsmath,array}
\usepackage{multicol}
\usepackage{ragged2e}

\newlength{\rowwidth}
\setlength{\rowwidth}{1.2em}

\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}

$$\Gamma = \begin{matrix} { } \\ AB \\ AC \\ CC \\ BD \\ CD \end{matrix} \begin{array}{l} \hphantom{\left[\right.\,}% \begin{array}{C{\rowwidth}C{\rowwidth}C{\rowwidth}C{\rowwidth}} A & B & C & D \\ \end{array} \\ \left[\begin{array}{C{\rowwidth}C{\rowwidth}C{\rowwidth}C{\rowwidth}} 3 & -1 & 0 & 0 \\ 1 & 0 & -1 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 2 & 0 & -3 \\ 0 & 0 & 4 & -2 \end{array}\right] \end{array}$$

\end{document}