2

Recently tried to compiled some files that used to compile perfectly, but now, generates an error whenever I try to align elements within bmatrix (same for pmatrix, or Vmatrix, etc.), using [] as the option.

In the MWE (below), I'm trying to righht-align the elements of the matrix. The error (for this example) doesn't cause LaTeX (MikTeX under Win 10) to complain, but there is clearly an error in the generated output (example attached), below the MWE.

So, are the alignment options for things like bmatrix no longer supported (i.e., [c], or [l], or [r])? If they are, there must be a syntax change somewhere I need to implement. If not, how the heck to get there from here? In some of my files, I have lots of these sorts of matrices, and I rather need to suss out a solution to this problem.

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

  \usepackage[dvips,top=1.2in,bottom=0.65in,left=1.15in,right=1.15in,includefoot]{geometry}

  \usepackage{amsmath}
  \usepackage{mathtools}

  \begin{document}

  Here is some code that used to compile without error:

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

 \end{document}

I you compile the MWE (above), this is what gets generated (at least, at my end) -- the [r] for algin right isn't being parsed as an option for bmatrix - rather, the [r] is now imbedded in the matrix itself (which is not the desired outcome).

Thanks much in advance...

enter image description here

5
  • 2
    Has it ever been supported? You probably refer to \begin{bmatrix*}[r] with \usepackage{mathtools}.
    – egreg
    Mar 4, 2020 at 18:19
  • Yes, it has been supported, because all of the code I am looking at (including the simple matrix shown in the MWE) used to compile perfectly, eft- or right-aligned as need be. Unless I had some tweak embedded in the preamble that I've forgotten about. Mar 4, 2020 at 18:24
  • I guess you found code that changed the standard behavior of bmatrix. Avoid it and use the mathtools way.
    – egreg
    Mar 4, 2020 at 18:25
  • What is the mathtools way - simple {bmatrix*}? Seems to wrok - I notice mathtools was recently updated in my installs. Mar 4, 2020 at 18:26
  • Sure enough, found the code snippet the preamble I (apparently) used to include - overrode default bmatrix behavior. Mar 4, 2020 at 18:28

2 Answers 2

5

The syntax \begin{bmatrix}[r] has never been supported.

You probably found code somewhere that changes the definition of bmatrix to accept an optional argument. It's not really difficult. All you need is to add to your preamble

\makeatletter
\renewcommand{\env@matrix}[1][c]{%
  \hskip-\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{*\c@MaxMatrixCols #1}%
}
\makeatother

Full example

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\renewcommand{\env@matrix}[1][c]{%
  \hskip-\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{*\c@MaxMatrixCols #1}%
}
\makeatother

\begin{document}

\[
\begin{bmatrix} 1 \\ 22 \\ 333 \end{bmatrix}
\qquad
\begin{bmatrix}[r] 1 \\ 22 \\ 333 \end{bmatrix}
\]

\end{document}

enter image description here

However, I advise against doing this:

  1. you risk to forget having done so and to copy the relevant code in a new document;

  2. a copy editor of the journal you're submitting to dislike documents having code inside \makeatletter and \makeatother (and they've very good reasons to).

Load mathtools and use the predefined bmatrix* environment.

\documentclass{article}
\usepackage{amsmath,mathtools}

\begin{document}

\[
\begin{bmatrix} 1 \\ 22 \\ 333 \end{bmatrix}
\qquad
\begin{bmatrix*}[r] 1 \\ 22 \\ 333 \end{bmatrix*}
\]

\end{document}

Redefining existing environments is not a capital sin, per se, but needs stronger reasons, especially if a solution is already available in a standard package.

1

The \bracketMatrixstack macro of TABstackengine supports right-alignment out of the box.

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

\usepackage[dvips,top=1.2in,bottom=0.65in,left=1.15in,right=1.15in,includefoot]{geometry}

\usepackage{tabstackengine}
\TABstackMath
\usepackage{mathtools}
\begin{document}
   \[
      \setstacktabbedgap{1.5ex}
      \bracketMatrixstack[r]{
       1 &  2 &  1 \\
       0 & -2 & -3 \\
       0 & 3 &  -2
      }
   \]
\end{document}

enter image description here

You must log in to answer this question.

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