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} \] |
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.
And to add horizontal lines in your matrix, use hline
That’s brilliant – thanks for posting it.
Very, very useful! Thanks!
Great stuff. Very useful.
Good stuff. You can also use the package arydshln if you want to add dashed lines in an array environment
Great!
Do you have any idea how to make it work with smallmatrix, too?
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.
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
would there be a way to implement this for small matrix?