10

The following code (an example of using \collect@body as a test case for this specific question) modified bmatrix to not add the surrounding [ and ]. This seems to work fine, but requires that I insert an extra brace group when I attempt to use this within the align environment.

enter image description here

Without the extra brace group I get:

Argument of \bmatrix has an extra }.

Question:

How do I modify this so that I do not need to have an extra brace group added when I attempt to use the bmatrix environment?

Code:

\documentclass{article}
\usepackage{amsmath}

\makeatletter \newcommand{@MatrixWithoutBracs}[1]{%
\begin{matrix}% #1% content \end{matrix} } \renewenvironment{bmatrix}{% \collect@body@MatrixWithoutBracs% }{}% \makeatother

\begin{document}\noindent In \verb|equation| things work \begin{equation} A = \begin{bmatrix} a_{11} & a_{12} \ a_{21} & a_{22} \ \end{bmatrix} \end{equation} and in \verb|align| had to use an extra brace group: \begin{align} A &= {\begin{bmatrix} a_{11} & a_{12} \ a_{21} & a_{22} \ \end{bmatrix}} \end{align} \end{document}

Peter Grill
  • 223,288
  • It's not just in the `align' environment; the same happens in all amstex environments. Strange. – JPi Jul 12 '16 at 01:45

1 Answers1

7

The problem is in the & tokens in the subsidiary environment, that should be protected from the outer environment. One of Knuth's dirty tricks that's often used in LaTeX is available.

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\@MatrixWithoutBracs}[1]{%  
    \begin{matrix}
        #1%  content
    \end{matrix}%
}
\renewenvironment{bmatrix}
   {{\ifnum0=`}\fi\collect@body\@MatrixWithoutBracs}
   {\ifnum0=`{\fi}}
\makeatother

\begin{document}

\noindent
In \verb|equation| things work
\begin{equation*}
    A = \begin{bmatrix}
            a_{11} & a_{12} \\
            a_{21} & a_{22} \\
        \end{bmatrix}
\end{equation*}
and in \verb|align| had to use an extra brace group:
\begin{align*}
   A &= \begin{bmatrix}
            a_{11} & a_{12} \\
            a_{21} & a_{22} \\
        \end{bmatrix}
\end{align*}

\end{document}

enter image description here

egreg
  • 1,121,712