7

I am trying to add extra space after the left bracket and before the right bracket in a matrix. I've changed the column spacing with \arraycolsep, but I also want the entries right aligned.

I can make it work for the left bracket by using \ for the first column, but the right alignment overrides this in the last column:

\begin{vmatrix*}[r] 
    \vec{i} &   \vec{j} &   \vec{k} \ \\
    \ #1        &   #2      &   #3  \\  
    \ #4        &   #5      &   #6 
\end{vmatrix*}

I'm using the following command to input the entries of this matrix:

\def\crossmatrix#1{\crossmatrixentries(#1)}
\def\crossmatrixentries(#1,#2,#3,#4,#5,#6)

I've inserted

\renewcommand\arraystretch{1.5} \arraycolsep=5pt

to help with spacing. I then called the matrix below by

\crossmatrix{-3,-6,9,2,-7,-4}

matrix

0

5 Answers 5

4

Instead of trying to modify the inner workings of the vmatrix* environment, it may be more straightforward to start over with a basic array environment and simply fine-tune some of its settings.

I gather that your main concern is with the spacing between the vertical bars and the contents of the arrays. The following example shows how the spacing may be adjusted. The adjustments amounts used in the example -- 2mu on the left and 5mu on the right -- can, of course, be changed to suit your needs and tastes. (The spacing used internally by vmatrix is 0mu on both sides.)

enter image description here

\documentclass{article}
\usepackage{mathtools,array}
\begin{document}

\begin{tabular}{l>{$}l<{$}}
\texttt{vmatrix*[r]} &
\begin{vmatrix*}[r] 
    \vec{i} &   \vec{j} &   \vec{k} \\
    -3       &  -6      &  9  \\  
    2        &  -7     &   -4
\end{vmatrix*} \\[5ex]
\texttt{array} & 
\left\lvert\begin{array}{@{\mkern3mu} rrr @{\mkern5mu}}
    \vec{i} &   \vec{j} &   \vec{k} \\
    -3       &  -6      &  9  \\  
    2        &  -7     &   -4
\end{array}\right\rvert\\
\end{tabular}

\end{document}
1
  • Thank you, both this and Steven B. Segletes answers work great, although this seems to require less work. Jun 4, 2015 at 22:01
2

I also encountered the same problem recently. I am not quite satisfied with the array environment because you need to specify the number of columns every time you make a matrix. It can be very tedious to replace every single instance of matrices in a document with the array environment.

Therefore, instead of using array, I dig a little bit into the source code of mathtools and found the definition of vmatrix*

\newenvironment{vmatrix*}[1][c]
  {\left\lvert\MT_matrix_begin:N #1}
  {\MT_matrix_end:\right\rvert}

This is quite easy to override. We just need to redefine the environment in the preamble and add some kerning before and after vertical line

\MHInternalSyntaxOn
\renewenvironment{vmatrix*}[1][c]
  {\left\lvert\mkern5mu\MT_matrix_begin:N #1}
  {\MT_matrix_end:\mkern5mu\right\rvert}
\MHInternalSyntaxOff

My original intention was to increase the spacing of bmatrix and vmatrix under newpxmath. For anyone who just wants to use bmatrix and vmatrix environment from amsmath, you can do something similar

\makeatletter
\renewenvironment{bmatrix}
{\left[\mkern3.5mu\env@matrix}
{\endmatrix\mkern3.5mu\right]}

\renewenvironment{vmatrix}
{\left\lvert\mkern5mu\env@matrix}
{\endmatrix\mkern5mu\right\rvert}
\makeatother

Here is a minimal working example

\documentclass{article}
\usepackage{mathtools}
\usepackage{newpxmath}

\MHInternalSyntaxOn
\renewenvironment{vmatrix*}[1][c]
  {\left\lvert\mkern5mu\MT_matrix_begin:N #1}
  {\MT_matrix_end:\mkern5mu\right\rvert}
\MHInternalSyntaxOff

\makeatletter
\renewenvironment{bmatrix}
  {\left[\mkern3.5mu\env@matrix}
  {\endmatrix\mkern3.5mu\right]}

\renewenvironment{vmatrix}
  {\left\lvert\mkern5mu\env@matrix}
  {\endmatrix\mkern5mu\right\rvert}
\makeatother

\begin{document}
\begin{align*}
  \begin{vmatrix*}[r]
    \vec{i} & \vec{j} & \vec{k} \\
    -3      & -6      & 9       \\
    2       & -7      & -4
  \end{vmatrix*} \\
  \begin{vmatrix}
    \vec{i} & \vec{j} & \vec{k} \\
    -3      & -6      & 9       \\
    2       & -7      & -4
  \end{vmatrix} \\
  \begin{bmatrix}
    \vec{i} & \vec{j} & \vec{k} \\
    -3      & -6      & 9       \\
    2       & -7      & -4
  \end{bmatrix}
\end{align*}
\end{document}

and a preview preview

1

I see the difficulty arising from two things: 1) the matrix columns are right aligned, which tends to weigh heavily on the right side of the matrix; and 2) the columns are free to assume unequal widths (the OP's MWE didn't stress this issue, but I believe that selecting terms that do so would show additional imbalance).

So my solution was to force matrix columns to be of equal width (which I can achieve with the use of TABstacks) and also to pad the right hand of each entry with a 3pt \kern, and pad the left hand of column 1 with an identical \kern.

In the MWE below, I give three examples: what the OP showed; an example with different width data in the columns; and an example without any negative numbers.

\documentclass{article}
\usepackage{tabstackengine}
\def\crossmatrix#1{\crossmatrixentries(#1)}
\def\crossmatrixentries(#1,#2,#3,#4,#5,#6){%
\fixTABwidth{T}% FIXED COLUMN WIDTH
\setstacktabbedgap{6pt}% INTERCOLUMN SPACING
\setstackgap{L}{18pt}% INTERROW BASELINESKIP
\def\>{\kern3pt}% EXTRA KERN APPLIED VARIOUSLY THROUGHOUT
 \vertMatrixstack[r]{
  \>\vec i\> & \vec j\> & \vec k\> \\
  \>#1\> & #2\> & #3\>\\
  \>#4\> & #5\> & #6\>
 }
 =
 \vertMatrixstack[r]{
  \>#2\> & #3\>\\
  \>#5\> & #6\>
 } \vec i
 -
 \vertMatrixstack[r]{
  \>#1\> & #3\>\\
  \>#4\> & #6\>
 } \vec j
 +
 \vertMatrixstack[r]{
  \>#1\> & #2\>\\
  \>#4\> & #5\>
 } \vec j
}
\begin{document}
The cross product $\vec{PQ} \times \vec{PR}$ generates the normal vector
\[
  \vec n = \crossmatrixentries(-3,-6,9,2,-7,-4)
\]
\[
  \vec n = \crossmatrixentries(-3,-63,9,2,-7,-40)
\]
\[
  \vec n = \crossmatrixentries(3,6,9,2,7,4)
\]
\end{document}

enter image description here

1

With use of the nicematrix package:

\documentclass{article}
\usepackage{nicematrix}
\NiceMatrixOptions{margin=2pt}

\begin{document}
\[
    \begin{vNiceMatrix}[r]
\vec{i} &   \vec{j} &    \vec{k}    \\
-3      &  -6       &    9          \\
 2      &  -7       &   -4
    \end{vNiceMatrix} 
\]   
\end{document}

For final result are needed several compilations.

enter image description here

0

The easiest and cheapest solution is to manually add \, (or whatever amount of space you need) at the beginning and at the end of every row in your matrix:

\begin{vmatrix*}[r] 
\,    \vec{i} &   \vec{j} &   \vec{k} \, \\
\,    1        &   2      &   3  \, \\  
\,    4        &   5      &   6  \,
\end{vmatrix*}
\]

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .