[Tex/LaTex] Compile error with redefined bmatrix and align environment

alignenvironmentsmatrices

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.

Best Answer

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}
Related Question