# Adding vertical lines around row vectors in block matrices

I would like to produce a 3x3 block matrix with a layout shown in the left figure below. The matrix is split by a row vector d-e-f and a column vector b-e-h (so e represents a 1x1 submatrix). I would like to add two horizontal and two vertical lines to separate the 9 blocks (4 matrices, 4 vectors, 1 scalar).

I am using the NiceArray commands of the nicematrix package, which work as intended if I use two-row blocks (see the left matrix below). However, when I am trying to typeset the row vector d-e-f using one-row blocks (i.e. changing \Block{2-2}{d} to \Block{1-2}{d} and so on), the vertical lines break (see the example on the right). Adding/removing the \hline commands has no effect on the vertical lines. I am using TeX Live 2019 with nicematrix version 3.7.

Is there a way I can keep vertical lines around submatrix e without using two-row blocks?

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

$\begin{pNiceArray}{cc|c|cc}[margin] \Block{2-2}{a} & & \Block{2-1}{b} & \Block{2-2}{c} & \\ & & & & \\\hline \Block{2-2}{d} & & \Block{2-1}{e} & \Block{2-2}{f} & \\ & & & & \\\hline \Block{2-2}{g} & & \Block{2-1}{h} & \Block{2-2}{i} & \\ & & & & \\ \end{pNiceArray} \quad \begin{pNiceArray}{cc|c|cc}[margin] \Block{2-2}{a} & & \Block{2-1}{b} & \Block{2-2}{c} & \\ & & & & \\ \Block{1-2}{d} & & \Block{1-1}{e} & \Block{1-2}{f} & \\\hline \Block{2-2}{g} & & \Block{2-1}{h} & \Block{2-2}{i} & \\ & & & & \\ \end{pNiceArray}$

\end{document}


Update: ultimately, I'd like to substitute the a...i with formulas representing the submatrices, e.g. A_{00}, a_{01}^\mathsf{T}, etc. I'd also like to illustrate that a and i are square matrices but c and g are not necessarily square ones.

Here is an example output with realistic values:

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

\setlength{\extrarowheight}{1mm}
\setlength{\arraycolsep}{8pt}
$\begin{pNiceArray}{ccc|c|ccccc}[margin] \Block{3-3}{A_{00}} & & & \Block{3-1}{a_{01}} & \Block{3-4}{A_{20}^\mathsf{T}} & & & \\ & & & & & & & \\ & & & & & & & \\ \hline % I'd like to use the following line but it doesn't work as expected: %\Block{1-3}{a_{01}^\mathsf{T}} & & & \Block{1-1}{0} & \Block{1-4}{a_{21}^\mathsf{T}} & & & \\ % two-row blocks are fine: \Block{2-3}{a_{01}^\mathsf{T}} & & & \Block{2-1}{0} & \Block{2-4}{a_{21}^\mathsf{T}} & & & \\ & & & & & & & \\ \hline \Block{4-3}{A_{20}} & & & \Block{4-1}{a_{21}} & \Block{4-4}{A_{22}} & & & \\ & & & & & & & \\ & & & & & & & \\ & & & & & & & \\ \end{pNiceArray}$

\end{document}


• What about: \documentclass{article} \begin{document} $\left( \begin{array}{c|c|c} {a} & {b} & {c} \\ \hline {d} & {e} & {f} \\ \hline {g} & {h} & {i} \\ \end{array} \right)$ \end{document} i.stack.imgur.com/0N71i.png Dec 29, 2019 at 15:46
• I've updated the question with a more complex example. Dec 29, 2019 at 16:46

Similar as suggested @leandriis in his comment. Your blocks are simulated with more space around submatrix:

Edit:

Considering matrix content as you provided in edited question

\documentclass{article}
\usepackage{array,
makecell}    % new

\begin{document}
$\setcellgapes{3pt} \makegapedcells \left( \begin{array}{ccc|c|ccc} & && && & \\ & A_{00} && a_{01} && A_{20}^T & \\ & && && & \\ \hline & a_{01}^T && 0 && f & \\ \hline & && && & \\ & A_{20} && a_{21} && A_{22}^T & \\ & && && & \\ \end{array}\right)$
\end{document}


Note: empty rows and column are add for emphasizing where dimension submatrix are bigger than 1 x 1. If this is not necessary, simple dropout them out.

• I'd like to use more lengthy formulas such as A_{00} and a_{01}^T. Centering these (horizontally and vertically) needs quite a lot of fiddling which I'd like to delegate to the nicematrix package. I've updated the question with this information. Dec 29, 2019 at 16:06
• centering of cells' contents in nicematrix is the same as in the ordinary array. Please, edit your question and show more realistic matrix, that we can see your specific, which you believe, that is not possible center inarray. Dec 29, 2019 at 16:33
• I've clarified the question with an additional example. Dec 29, 2019 at 16:45
• Seems good - upvoted. I think the main limitation of this approach is that handling even-sized dimensions such as a 2x2 submatrix is verbose due to the need to use multicolumn and multirow at the same time. The latter also exits math mode so the top-left cell would become something like \multicolumn{2}{c|}{\multirow{2}{*}{$A_{00}$}}. Dec 29, 2019 at 17:43
• @Sebastiano, thank you. i will correct asap ... Dec 29, 2019 at 21:33

It turns out this is a bug in the current version of the nicematrix package. This has been fixed in version 3.8 (2020-01-02) and is available on CTAN.

If you're stuck with an older version for some reason, the bug can be worked around by adding \vline at the end of the affected cells. Applying this on the minimal working example gives the desired output:

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

\setlength{\extrarowheight}{1mm}
\setlength{\arraycolsep}{8pt}
$\begin{pNiceArray}{ccc|c|ccccc}[margin] \Block{3-3}{A_{00}} & & & \Block{3-1}{a_{01}} & \Block{3-4}{A_{20}^\mathsf{T}} & & & \\ & & & & & & & \\ & & & & & & & \\ \hline \Block{1-3}{a_{01}^\mathsf{T}} & & \vline & \Block{1-1}{0} \vline & \Block{1-4}{a_{21}^\mathsf{T}} & & & \\\hline \Block{4-3}{A_{20}} & & & \Block{4-1}{a_{21}} & \Block{4-4}{A_{22}} & & & \\ & & & & & & & \\ & & & & & & & \\ & & & & & & & \\ \end{pNiceArray}$

\end{document}


• Good work. You could put, if you prefer, $\top$ or $\intercal$ commands instead of \mathsf. Jan 4, 2020 at 16:53

A variant solution:

\documentclass{article}
\usepackage{amsmath} %

\begin{document}

$\renewcommand{\arraystretch}{1.4} \left[\,\:\begin{matrix} a & \vrule & b & \vrule & c \\ \hline d & \vrule & e & \vrule & f \\ \hline g & \vrule & h &\vrule & i \end{matrix} \,\: \right]$

\end{document}