An extension to amsmath matrix environments
September 30th, 2008 by Stefan Kottwitz
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} \]

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} \]

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.
This entry was posted on Dienstag, September 30th, 2008 at 22:41 and is filed under Mathematics. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.





August 12th, 2009 at 20:23
And to add horizontal lines in your matrix, use \hline
Februar 21st, 2010 at 18:17
That’s brilliant - thanks for posting it.
April 7th, 2011 at 15:22
Very, very useful! Thanks!
Juli 21st, 2011 at 11:59
Great stuff. Very useful.
November 13th, 2011 at 11:50
Good stuff. You can also use the package arydshln if you want to add dashed lines in an array environment