Overwrite matrix to work inside align (to use mtpro2 parentheses and braces)

You can do it without environ.

\documentclass{article}
\usepackage{amsmath}
\usepackage{newtxtext}
\usepackage[lite]{mtpro2}
\usepackage{environ}

\newsavebox{\matrixbox}

\renewenvironment{pmatrix}
 {\setbox\matrixbox=\hbox\bgroup$\begin{matrix}}
 {\end{matrix}$\egroup\PARENS{\copy\matrixbox}}
\renewenvironment{Bmatrix}
 {\setbox\matrixbox=\hbox\bgroup$\begin{matrix}}
 {\end{matrix}$\egroup\LEFTRIGHT\{\}{\copy\matrixbox}}
\renewenvironment{cases}
 {\setbox\matrixbox=\hbox\bgroup$\def\arraystretch{1.2}%
  \begin{array}{@{\,}l@{\quad}l@{}}}
 {\end{array}$\egroup\LEFTRIGHT\lbrace.{\copy\matrixbox}}

\begin{document}
\section{Works!}
\[
\begin{pmatrix}
a & b \\
c & d \\
\end{pmatrix} \quad
\begin{Bmatrix}
a & b \\
c & d \\
\end{Bmatrix} \quad
\begin{cases}
a & b \\
c & d \\
\end{cases}
\]

\section{Works as well}
\begin{align*}
\begin{pmatrix}
a & b \\
c & d \\
\end{pmatrix}\quad
\begin{Bmatrix}
a & b \\
c & d \\
\end{Bmatrix} \quad
\begin{cases}
a & b \\
c & d \\
\end{cases}
\end{align*}

\[
\begin{pmatrix}
a & \begin{pmatrix} c \\ d \end{pmatrix} \\
e & f
\end{pmatrix}
\]

\end{document}

enter image description here


The following code is heavily inspired by @egreg’s answer. In particular, the technique of box manipulation is used. There are two major improvements:

  1. The \matrix@check feature of amsmath is preserved for pmatrix and cases.
  2. Large curly braces are used for Bmatrix and cases if and only if the package option curlybraces is passed to mtpro2. For options morphedbraces and straightbraces, it makes much more sense to use large straight braces.

Additionally, similar patches are applied to pmatrix* and Bmatrix* from the mathtools package (which I did not mention in my original question). Finally, as pointed out by a previous answer by @egreg, I have removed \mskip-\thinmuskip from the definition of \LEFTRIGHT. I think sharing my code would be helpful to others.

Here are my results:

\documentclass{article}
\usepackage{mathtools}% loads `amsmath'
\usepackage{newtxtext}
\usepackage[scaled=0.861,lining]{FiraMono}
\def\bracesshape{curlybraces}% change here to obtain different braces
% curlybraces
% morphedbraces
% straightbraces
\usepackage[lite,\bracesshape]{mtpro2}

% Patches begin
\makeatletter
% Fix weird space
\usepackage{etoolbox}
\patchcmd{\LEFTRIGHT}
  {\kern-2\nulldelimiterspace\mskip-\thinmuskip}
  {\kern-2\nulldelimiterspace}
  {}{}
% Two new boxes
\newsavebox{\mtp@matrixbox}
\newsavebox{\mtp@casesbox}
% Round parentheses should always be used by default
% `pmatrix' from `amsmath'
\renewenvironment{pmatrix}{%
  \matrix@check\pmatrix\setbox\mtp@matrixbox=\hbox\bgroup$\env@matrix
}{%
  \endmatrix$\egroup\PARENS{\copy\mtp@matrixbox}%
}
% Curly braces are used only if `curlybraces' is set
% From `mtpro2.sty': \DeclareOption{curlybraces}{\let\mtp@br=c}
\ifx\mtp@br c
  % `Bmatrix' from `amsmath'
  \renewenvironment{Bmatrix}{%
    \setbox\mtp@matrixbox=\hbox\bgroup$\env@matrix
  }{%
    \endmatrix$\egroup\LEFTRIGHT\lbrace\rbrace{\copy\mtp@matrixbox}%
  }
  % `cases' from `amsmath'
  \renewcommand*\env@cases{%
    \let\@ifnextchar\new@ifnextchar
    \setbox\mtp@casesbox=\hbox\bgroup$%
    \def\arraystretch{1.2}%
    \array{@{}l@{\quad}l@{}}%
  }
  \renewenvironment{cases}{%
    \matrix@check\cases\env@cases
  }{%
    \endarray$\egroup\LEFTRIGHT\lbrace.{\copy\mtp@casesbox}%
  }
\fi
% Now, the matrices from `mathtools'
\MHInternalSyntaxOn
\MaybeMHPrecedingSpacesOff
% `pmatrix*' from `mathtools'
\renewenvironment{pmatrix*}[1][c]
  {\setbox\mtp@matrixbox=\hbox\bgroup$\MT_matrix_begin:N #1}
  {\MT_matrix_end:$\egroup\PARENS{\copy\mtp@matrixbox}}
\MH_if_meaning:NN \mtp@br c
  % `Bmatrix*' from `mathtools'
  \renewenvironment{Bmatrix*}[1][c]
    {\setbox\mtp@matrixbox=\hbox\bgroup$\MT_matrix_begin:N #1}
    {\MT_matrix_end:$\egroup\LEFTRIGHT\lbrace\rbrace{\copy\mtp@matrixbox}}
\MH_fi:
\MHPrecedingSpacesOn
\MHInternalSyntaxOff
\makeatother
% Patches end

\newcommand*\showopendelimitersizes[1]{%
  #1\bigl#1\Bigl#1\biggl#1\Biggl#1}

\begin{document}

\section*{Matrices and cases in \texttt{align} now work!}
Matrices from \verb|amsmath| and \verb|mathtools| work;
\verb|cases| from \verb|amsmath| works.
\subsection*{Package \texttt{mtpro2} options: \texttt{lite,\bracesshape}}
\begin{align*}
\showopendelimitersizes{(}
\begin{pmatrix}
-a &  b \\
 c & -d \\
-a &  b \\
 c & -d \\
\end{pmatrix} \quad
\showopendelimitersizes{\lbrace}
\begin{Bmatrix*}[r]
-a &  b \\
 c & -d \\
-a &  b \\
 c & -d \\
\end{Bmatrix*} \quad
\begin{cases}
-a &  b \\
 c & -d \\
-a &  b \\
 c & -d \\
\end{cases}
\end{align*}

\end{document}

matrices and cases with mtpro2