# align elements of matrix with a bmatrix inside

I have made the following matrix:

\newcommand{\mtx}[2]{\begin{#1matrix}#2\end{#1matrix}}
\newcommand{\mtxR}[2]{\begin{#1matrix*}[r]#2\end{#1matrix*}}
\begin{equation*}
A=\mtx{}{\mtx{}{e_1&e_2&e_3&e_4&e_5&e_6&e_7&e_8}&\\\mtxR{b}{1&&1&&&&&\\-1&1&&1&&&&\\&-1&&&1&1&&\\&&&&-1&-1&&1\\&&&-1&&&1&-1\\&&-1&&&&-1&}&\mtx{}{v_1\\v_2\\v_3\\v_4\\v_5\\v_6}}
\end{equation*}


Is there a way to align the e's in the top row with the numbers in the bmatrix, and the same for the v's in the most right column?

Like this?

\documentclass{article}
\usepackage{blkarray}
\begin{document}

$A=\begin{blockarray}{cccccc} e_1 & e_2 & e_3 & e_4 & e_5 \\ \begin{block}{[ccccc]l} 1 & -1 & 1 & 1 & 1 & v_1 \\ & 1 & & & 1 & v_2 \\ & & 1 & & 1 & v_3 \\ & & & 1 & 1 & v_4 \\ & & & & 1 & v_5 \\ \end{block} \end{blockarray}$
\end{document}


If you wanted the 1 and the -1 entries to line up so that the numerals were exactly under each other, you could try defining the inner block with [rrrrr]l. But you might think (for example) that the right hand column was too close to the right bracket.

Or you can "cheat" with \smash to set the minus signs in zero-width boxes. Personally I don't mind the -1 entries sticking out to the right a bit, as I think it might make them easier to read.

Or you can use nicematrix as the other answer shows.

• Unfortunately, any negative element won't be nicely aligned. That's why the starred form of matrix (requiring mathtools) was used in the question. Commented Mar 10, 2020 at 16:04
• @barbarabeeton true, but I was trying to keep things as simple as possible and just answer the bit about the labels. I know tastes differ, but personally I don't mind the centred -1 sticking out a bit -- I think it can actually be easier to read. But I would not want to die in a ditch about it. Commented Mar 10, 2020 at 16:12
• The reason I mentioned it is that the OP has negatives scattered heavily throughout, and went to great effort to have the digits aligned. My inclination is to observe and honor that. Commented Mar 11, 2020 at 3:43

You can use nicematrix.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}
$\begin{bNiceArray}{*{8}{R}}[ first-row, last-col, ] e_1 & e_2 & e_3 & e_4 & e_5 & e_6 & e_7 & e_8 & \\ 1 & & 1 & & & & & & v_1 \\ -1 & 1 & & 1 & & & & & v_2 \\ & -1 & & & 1 & 1 & & & v_3 \\ & & & & -1 & -1 & & 1 & v_4 \\ & & & -1 & & & 1 & -1 & v_5 \\ & & -1 & & & & -1 & & v_6 \\ \end{bNiceArray}$

\end{document}


• Can you make the column labels centred, while keeping the elements right aligned? Commented Mar 10, 2020 at 16:21
• @Thruston \multicolumn{1}{c}{...}? Commented Mar 10, 2020 at 17:45

Here are two ways: with blkarray and with bigdelim:

\documentclass{article}
\usepackage{array, blkarray}
\usepackage{bigdelim}

\begin{document}

$A = \begin{blockarray}{*{8}{r}l} e_1 & e_2 & e_3 & e_4 & e_5 & e_6 & e-7 & e_8 & \\ \begin{block}{[*{8}{r}]@{\quad}l} 1 & & 1 & & & & & & v_1 \\ -1 & 1 & &1 & & & & & v_2 \\ & -1 & & & 1 & 1 & & & v_3 \\ & & & & -1 & -1 & & 1 & v_4 \\ & & & -1 & & & 1 & -1 & v_5 \\ & & -1 & & & & -1 & &v_6 \\ \end{block} \end{blockarray}$

$A = \begin{array}{r@{\,}*{8}{r}@{\,}l@{}l} & e_1 & e_2 & e_3 & e_4 & e_5 & e_6 & e-7 & e_8 & \\ \ldelim[{6}{*} & 1 & & 1 & & & & & & \rdelim]{6}{*} & v_1 \\ & -1 & 1 & &1 & & & & & & v_2 \\ & & -1 & & & 1 & 1 & & & & v_3 \\ & & & & & -1 & -1 & & 1 & & v_4 \\ & & & & -1 & & & 1 & -1 & & v_5 \\ & & & -1 & & & & -1 & & & v_6 \end{array}$

\end{document}


• Do you really mean $e-7$ or $e_7$? (I think the latter.) Commented Mar 10, 2020 at 16:05

You can use nicematrix and its nice keys like code-for-first-row and code-for-last-col. That way you do not have to type e_1, e_2 etc. nor v_1, v_2 and so on. This also allows you to center the entries of a column or row.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}
$\pgfmathsetmacro{\myw}{width("-1")} \begin{bNiceArray}{*{8}{R}}[ first-row, last-col, code-for-first-row = \makebox[\myw pt][c]{e_{\arabic{jCol}}}, code-for-last-col = v_{\arabic{iRow}} ] & & & & & & & & \\ 1 & & 1 & & & & & & \\ -1 & 1 & & 1 & & & & & \\ & -1 & & & 1 & 1 & & & \\ & & & & -1 & -1 & & 1 & \\ & & & -1 & & & 1 & -1 & \\ & & -1 & & & & -1 & & \\ \end{bNiceArray}$

\end{document}