# Append environment definition

Is there a way to append an environment definition, something like \appendenvironment akin to \(re)newenvironment?

Specifically, I'd like to reduce the horizontal spacing after the pmatrix environment to have punctuation symbols nice and snug with the closing parenthesis. The default spacing is

\documentclass{article}
\usepackage{mathtools}

\begin{document}
$$D_1 = \begin{pmatrix} 1 & -i & i & -1\\ 1 & -i & i & -1\\ 1 & -i & i & -1\\ 1 & -i & i & -1 \end{pmatrix},$$
\end{document}


Adding \mkern-6mu or \!\! after \end{pmatrix} results in the much nicer appearance.

Is there some way to have this negative space inserted after every pmatrix without using \renewenvironment and copying the whole environment definition to my preamble?

• Quick and dirty: \usepackage{xpatch}\xapptocmd{\endpmatrix}{\mkern-6mu}{}{}. – campa Jun 1 '17 at 14:06
• The etoolbox package has \AtEndEnvironment{} command – user31729 Jun 1 '17 at 14:32
• @ChristianHupfer \AfterEndEnvironment would be better. The argument of \AtEndEnvironment is appended before \endXXX, and in this case it would add the negative space to the last entry of the matrix, not after the parenthesis :-) – campa Jun 1 '17 at 15:29
• @campa: True, I forgot that it should be done after the environment – user31729 Jun 1 '17 at 15:30
• @campa: Willing to add an answer? – user31729 Aug 5 '17 at 20:59

Whereas campa suggested a \xpatchcmd way I use \AfterEndEnvironment from etoolbox package and as another alternative method the \g@addto@macro which does not need an extra package because it is a LaTeX kernel macro.

I prefer the \AfterEndEnvironment method since it is a little bit cleaner.

\documentclass{article}
\usepackage{mathtools}

\usepackage{etoolbox}

\begin{document}
$$D_1 = \begin{pmatrix} 1 & -i & i & -1\\ 1 & -i & i & -1\\ 1 & -i & i & -1\\ 1 & -i & i & -1 \end{pmatrix},$$

\AfterEndEnvironment{pmatrix}{\mkern-6mu}

$$D_1 = \begin{pmatrix} 1 & -i & i & -1\\ 1 & -i & i & -1\\ 1 & -i & i & -1\\ 1 & -i & i & -1 \end{pmatrix},$$

Now with bmatrix and then with \verb!\g@addto@macro\endbmatrix!

$$D_1 = \begin{bmatrix} 1 & -i & i & -1\\ 1 & -i & i & -1\\ 1 & -i & i & -1\\ 1 & -i & i & -1 \end{bmatrix},$$

\makeatletter
$$D_1 = \begin{bmatrix} 1 & -i & i & -1\\ 1 & -i & i & -1\\ 1 & -i & i & -1\\ 1 & -i & i & -1 \end{bmatrix},$$