# How to redefine (via environ package) the pmatrix environment?

I want to redefine the pmatrix environment provided by the amsmath package (and changed by the mathtools package) for adding good-looking delimiters. The code is below:

\documentclass{article}
\usepackage{amsmath,mathtools,environ}
\RenewEnviron{pmatrix}{\PARENS{\begin{matrix}\BODY\end{matrix}}}


The \PARENS command is provided by the mtpro2 package to get curved left/right parentheses. But it does not work. Furthermore, there's a starred version pmatrix* (with an optional arg l/c/r): I don't have any idea either. The code I can give is below:

\documentclass{article}
\usepackage{amsmath,mathtools,environ}
\RenewEnviron{pmatrix*}[1][c]{\PARENS{\begin{matrix*}[c]\BODY\end{matrix*}}}


I mailed to environ package's author Will Robertson, but there's no reply. He may come here usually. So I logged in and posted.

(The question is copied from http://stackoverflow.com/questions/4802117/how-to-redifine-via-environ-package-the-pmatrix-environment.)

-
I edited your post. Please don't just put a link there. –  Leo Liu Jan 27 '11 at 7:53

My solution is based on Leo Liu's, but quicker if you want to redefine many environments in such a way:

\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*}[1][c]{\PARENS{\begin{matrix*}[#1]\BODY\end{matrix*}}}

-

\NewEnviron can define a environment that is never defined before, and \RenewEnviron can only redefine a environment that is defined by \NewEnviron. If we use \RenewEnviron to redefine a predefined environment, we'll get an error:

! LaTeX Error: \env@pmatrix@save@env undefined.

It seems a bug of environ. Here is the solution:

\usepackage{amsmath,mathtools}
\usepackage{mtpro2}
\usepackage{environ}

\let\pmatrix\relax
\let\endpmatrix\relax
\NewEnviron{pmatrix}{\PARENS{\begin{matrix}\BODY\end{matrix}}}

\expandafter\let\csname pmatrix*\endcsname\relax
\expandafter\let\csname endpmatrix*\endcsname\relax
\NewEnviron{pmatrix*}[1][c]{\PARENS{\begin{matrix*}[#1]\BODY\end{matrix*}}}

-
the environ package uses the same internal command to define an environment or to redefine one. To do that, it has a placeholder for \(re)newcommand and \(re)newenvironment. In a \NewEnviron, everything is defined using \new.... In a \RenewEnviron, everything is defined using \renew.... What we would like here is renew some macros (\pmatrix and \endpmatrix), but create new (internal) macros. –  Bruno Le Floch Jan 27 '11 at 7:24
@Bruno: Thanks, I got it. But it seems a bug (or unpleasing feature) of environ. –  Leo Liu Jan 27 '11 at 7:34
These worked perfectly. Thanks you for your efforts on my problem! –  luochaodong Jan 27 '11 at 8:27