# Compile error with redefined bmatrix and align environment

I just spent a couple of hours finding the reason for compiling errors in the last example in here:

\documentclass[a4paper,draft=true, headsepline=on, twoside
, 11pt,
]{scrbook}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{amsmath}

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

\begin{document}

\begin{equation*}
\begin{bmatrix}
& b
\end{bmatrix}
\end{equation*}

\begin{align*}
\begin{bmatrix}
~  & b
\end{bmatrix}
\end{align*}

\begin{align*}
\begin{array}{cc}
& b
\end{array}
\end{align*}

\begin{align*}
\begin{bmatrix}
& b
\end{bmatrix}
\end{align*}

\end{document}


The error does not appear if I do not redefine bmatrix environment like discussed here: How does this macro for augmented matrices work?

However, the errors only seam to appear in this special case of using the align environment with a matrix that has empty entries in first column. Do you understand where these errors come from? I have no idea, but this could certainly help later on if similar problems occur.

The scanning for the optional argument confuses the alignment parser, due to the empty first cell in the matrix.

You're luckier with xparse:

\documentclass[a4paper,draft=true, headsepline=on, twoside
, 11pt,
]{scrbook}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{amsmath,xparse}

%matrix environment redef
\makeatletter
\RenewDocumentCommand\env@matrix{O{*\c@MaxMatrixCols c}}{%
\hskip -\arraycolsep
\let\@ifnextchar\new@ifnextchar
\array{#1}}
\makeatother

\begin{document}

\begin{align*}
\begin{bmatrix}
& b
\end{bmatrix}
\end{align*}

\end{document}

• the empty first cell problem in any alignment was reported very early on (1995, again in 1997 and '98); it was thought to be fixed in latex 1996/06/01, but actually wasn't, so a fix was applied in amsmath 2.0 (1999). our bugs list says that a "tech note" was issued, but i can't find it; if you could review 'technotes.tex and make suggestions, i'd appreciate it. May 31, 2015 at 13:15

You can add an extra level of bracing to make the lookahead safe.

\documentclass[a4paper,draft=true, headsepline=on, twoside
, 11pt,
]{scrbook}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{amsmath}

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

\renewenvironment{bmatrix}
{{\ifnum}=0 \fi\left[\env@matrix}
{\endmatrix\right]\ifnum{=0 \fi}}
\makeatother

\begin{document}

\begin{equation*}
\begin{bmatrix}
& b
\end{bmatrix}
\end{equation*}

\begin{align*}
\begin{bmatrix}
~  & b
\end{bmatrix}
\end{align*}

\begin{align*}
\begin{array}{cc}
& b
\end{array}
\end{align*}

\begin{align*}
\begin{bmatrix}
& b
\end{bmatrix}
\end{align*}

\end{document}

• Yes, that's something amsmath should do anyway. May 30, 2015 at 22:12
• Can you explain a little more, what exactly your solution does? Why< do you need \ifnum'?
– dba
May 31, 2015 at 12:56
• there's been a standing request on our bugs list since 2000; i've added this question as a reference. May 31, 2015 at 13:22
• @dbacc It's a standard trick (described in the texbook) to get a literal unmatched { into a macro \ifnum}=0 \fi expands to nothing but it allows the preceding { to be matched in the definition. (alignments are fussy and normal alternatives like \bgroup do not work here. Jun 1, 2015 at 12:13