3

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

enter image description here

\documentclass{article}
\usepackage{mathtools}

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

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

enter image description here

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?

  • 1
    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
4

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}
\begin{equation}
    D_1 = \begin{pmatrix}
        1 & -i & i & -1\\
        1 & -i & i & -1\\
        1 & -i & i & -1\\
        1 & -i & i & -1
    \end{pmatrix},
\end{equation}


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

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

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

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


\makeatletter
\g@addto@macro{\endbmatrix}{\mkern-6mu}%
\makeatother

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

\end{document}

enter image description here

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.