# increase space between delimiter and adjacent column in matrix

Suppose I have the following simple matrix, constructed in the usual way using bmatrix:

Anal-retentive editor asks if the spacing between the delimiter and the adjacent column can be increased for "aesthetic reasons". Say, perhaps yielding something like the following:

While there are a number of ways to increase spacing among rows/columns in a matrix, as much as I could find these always apply to rows with regard to other rows, columns with regard to other columns. I wasn't able to suss out how you can tweak spacing between matrix elements and the delimiters.

I came up with a kludge in the following MWE (which yielded the embedded images, above), but for MS with lots of matrices you might want to tweak this way, doing this 'by hand' spacing tweak is laborious, and offends the sense I work with that some clever person has a better, more general (and flexible) way. I had a quick dive into nicematrix (didn't see anything obvious), and thought about reverting to an array approach, but in the end, figured I'd see what other folks might recommend.

Thanks in advance for suggestions. [Coding suggestions only. Suggestion concerning editorial aesthetics? Well...]

    \documentclass[11pt,letterpaper,oneside]{article}

\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}
Here is an example matrix:
$\begin{bmatrix*}[r] 1 & 2 & 1 \\ 0 & -2 & -3 \\ 0 & 3 & -2 \end{bmatrix*}$

Seeking a way to increase the separation between the left
delimiter and elements of the first column, and the right
delimiter from the right-most column The following is a
total blunt-weapon kludge.

$\begin{bmatrix*}[r] \ 1 & 2 & 1 \ \\ \ 0 & -2 & -3 \ \\ \ 0 & 3 & -2 \ \end{bmatrix*}$

Kludge works, but is there a better way?
[In other words, a particular package of setting' I can
tweak so that it auto-magically applies to bmatrix constructs?].

\end{document}


It's possible to redefine existing environments

\RenewDocumentEnvironment{<name>}{<paramters> b}{<body>}


In case such environment is still needed, it is also possible to make a copy

\NewEnvironmentCopy{<new name>}{<name>}


Given the information, I copied the existing bmatrix* to oldbmatrix* and then redefine it based on matrix*, also from mathtools but with some additional spacing at brackets.

The code

\documentclass[11pt,letterpaper,oneside]{article}
\usepackage{mathtools}

\NewEnvironmentCopy{oldbmatrix*}{bmatrix*}
\RenewDocumentEnvironment{bmatrix*}{O{c} b}{%
\left[\hspace{1em}\begin{matrix*}[#1]#2\end{matrix*}\hspace{1em}\right]}{}

\begin{document}
Here is an example matrix:
$\begin{bmatrix*}[r] 1 & 2 & 1 \\ 0 & -2 & -3 \\ 0 & 3 & -2 \end{bmatrix*}$
Seeking a way to increase the separation between the left delimiter and elements of the first
column, and the right delimiter from the right-most column The following is a total blunt-
weapon kludge.

$\begin{oldbmatrix*}[r] 1 & 2 & 1 \\ 0 & -2 & -3 \\ 0 & 3 & -2 \end{oldbmatrix*}$

Kludge works but, is there a better way? [In other words, a particular package of setting' I
can tweak so that it auto-magically applies to bmatrix constructs?].
\end{document}


and screenshot

• Excellent -- simple, and something I can understand (meaning, I can tweak/turn on-off) as needed while not breaking anything else. Commented Aug 2, 2023 at 14:40

Patch the relevant commands.

\documentclass[11pt,letterpaper,oneside]{article}

\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{xpatch}

% patch amsmath matrix
\makeatletter
\xpatchcmd\env@matrix
{\arraycolsep}% search
{0.25\arraycolsep}% replace
{}{\FAIL}
\xpatchcmd\endmatrix
{\arraycolsep}% search
{0.25\arraycolsep}% replace
{}{\FAIL}
\makeatother

% patch mathtools matrix
\MHInternalSyntaxOn
\xpatchcmd\MT_matrix_begin:N
{\arraycolsep}% search
{0.25\arraycolsep}% replace
{}{\FAIL}
\xpatchcmd\MT_matrix_end:
{\arraycolsep}% search
{0.25\arraycolsep}% replace
{}{\FAIL}
\MHInternalSyntaxOff

\begin{document}

Here is an example matrix:
$\begin{bmatrix} 1 & 2 & 1 \\ 0 & -2 & -3 \\ 0 & 3 & -2 \end{bmatrix} \qquad \begin{bmatrix*}[r] 1 & 2 & 1 \\ 0 & -2 & -3 \\ 0 & 3 & -2 \end{bmatrix*}$

\end{document}


Reason: both amsmath and mathtools matrices do \hskip-\arraycolsep at the left and at the right. With the patches, the amount of backing up is reduced by 3/4.

Comment: everybody is entitled their sense of aesthetics. 🤔

• Elegant -- ultimately the most flexible way to go, but perhaps overkill for one-off immediate needs. Thanks for sharing. I learned something from working through your patches. Commented Aug 2, 2023 at 14:41

Since the OP has mentioned nicematrix, I give a solution with nicematrix. However, it's probably overkilling the problem (except if nicematrix is used for other features provided by that package).

\documentclass{article}
\usepackage{nicematrix}

\NiceMatrixOptions{margin=5pt,renew-matrix}

\begin{document}

$\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}$

\end{document}


• Excellent -- apparently I completely missed the margin argument when browsing through the nicematrix documentation. Many thanks. Commented Aug 10, 2023 at 13:13