An extension to amsmath matrix environments

Inspired by a question on matheplanet.com and remembering the cases redefinition I’ve shown some days ago I got the idea to extend the internal macro \env@matrix of amsmath.sty. I wanted to use the matrix environments together with array features like alignment, vertical lines, formatting and special commands.
The mathtools package provides something similar by its starred matrix environments that support one optional parameter that will be applied to all matrix columns. The following redefinition will introduce an optional parameter to amsmath array environments that allows column-specific customization:

\makeatletter
\renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
  \hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{#1}}
\makeatother

If you put these lines into your document preamble the pmatrix, bmatrix, vmatrix, Bmatrix, Vmatrix etc. environments will accept an optional parameter. If you don’t provide this parameter those environments will work like usual. Here’s one example showing an augmented matrix containing a vertical line:

\[
\begin{pmatrix}[cc|c]
  1 & 2 & 3\\
  4 & 5 & 9
\end{pmatrix}
\]
augmented matrix

Another more complex example just showing some array features like different alignment because of the signs, color change and bold font:

\[
\begin{bmatrix}[*2cr@{\quad}|@{\quad}>{\bf\color{red}}r]
  a & b & 1  &  4 \\
  c & d & -2 & -3
\end{bmatrix}
\]
augmented matrix

Though \bf is an obsolete font command standard classes still support it and I’ve just used it because \boldmath is invalid in math mode, in general I advice against using \bf.

30. September 2008 by stefan
Categories: Mathematics | 9 comments

Comments (9)

  1. And to add horizontal lines in your matrix, use hline

  2. That’s brilliant – thanks for posting it.

  3. Very, very useful! Thanks!

  4. Great stuff. Very useful.

  5. Good stuff. You can also use the package arydshln if you want to add dashed lines in an array environment

  6. Great!
    Do you have any idea how to make it work with smallmatrix, too?

  7. Thanx. Very handy. I wanted to change ‘(‘ to ‘\lgroup’, so I opened amsmath.sty and found ‘pmatrix’. There I changed

    ‘\left(‘ to ‘\left\lgroup’
    and
    ‘\right)’ to ‘\right\rgroup’

    This may be done in a much smarter way by some codeeagle :)

    Found out, that ‘c’ does not mean ‘column’, but ‘center’.

    Instead of [cc|c] try [rr|r] for right alignment and [ll|l] for leftalignment.

  8. Very nice.

    Would it be possible to adapt your macro to the gmatrix types given by the gauss-package?

    As a total newbie I can’t do it myself yet but I’m desperately looking for a way to solve equation-systems which look like your matrices but work with row/col ops like offered by the gauss-package

  9. would there be a way to implement this for small matrix?

Leave a Reply to Stephen Siklos Cancel reply

Required fields are marked *