5

To use large round parentheses and large curly braces provided by the MathTime Pro 2 font, I’m using the code from this answer by @BrunoLeFloch to redefine pmatrix, Bmatrix and cases. However, this implementation does not work when the matrices (or cases) are inside the align (and friends) environment.

\documentclass{article}
\usepackage{amsmath}
\usepackage{newtxtext}
\usepackage[lite]{mtpro2}
% in case you don't have mtpro2, use the following
%\usepackage{newtxmath}
%\providecommand*\PARENS[1]{\left(#1\right)}
%\providecommand*\LEFTRIGHT[3]{\left#1#3\right#2}
\usepackage{environ}
% from https://tex.stackexchange.com/a/9785/164314
\newcommand{\OverwriteEnviron}[1]{%
  \expandafter\let\csname #1\endcsname\relax%
  \expandafter\let\csname end#1\endcsname\relax%
  \expandafter\let\csname env@#1@parse\endcsname\relax%
  \expandafter\let\csname env@#1@save@env\endcsname\relax%
  \expandafter\let\csname env@#1@process\endcsname\relax%
  \NewEnviron{#1}%
}
\OverwriteEnviron{pmatrix}{\PARENS{%
  \begin{matrix}\BODY\end{matrix}}}
\OverwriteEnviron{Bmatrix}{\LEFTRIGHT\{\}{%
  \begin{matrix}\BODY\end{matrix}}}
\OverwriteEnviron{cases}{\LEFTRIGHT\lbrace.{\def\arraystretch{1.2}%
  \begin{array}{@{\,}l@{\quad}l@{}}\BODY\end{array}}}

\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{Do not work}
\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*}
\end{document}

new definitions of matrices and cases

A very similar question has been asked before, but no answers have been provided. I think the problem lies in the nested tabulars where TeX gets confused by the alignment tab &. I am also aware of the “dirty tricks” of \ifnum`{=0\fi and \ifnum`}=0\fi. But I have no clue how to implement these tricks. Any ideas?

  • 1
    Note you have the same problem if you define a new environment such as \NewEnviron{xx}{\PARENS{\begin{matrix}\BODY\end{matrix}}}. Putting the result inside a group inside the align, {\begin{xx}a&b\end{xx}}, makes it compilable again. – Andrew Swann Aug 17 '18 at 16:24
  • 1
    By the way, you can simply do \RenewEnviron{pmatrix} instead of the tricky code that was written before a new version of environ was issued. – egreg Aug 17 '18 at 16:46
4

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

  • This is so much more elegant!!! Thank you for providing this neat trick! I took your technique and came up with my own version of the answer. I was wondering if you’d make some comments on mine (before I accept your answer). :-) I’m going to ask a separate follow-up question if you find no problem in my answer. – Ruixi Zhang Aug 18 '18 at 2:25
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.