What's the best way make an "augmented" coefficient matrix?
One way to do this is implemented in the (free, in both senses!) online linear algebra textbook Linear Algebra by Jim Hefferon. It's written in LaTeX and is open-source so one can download the book and its attendant style files. One of them, called linalgjh.sty
is about typesetting common linear algebra stuff such as augmented matrices and row reductions and the like. The code for the augmented matrices is:
\newenvironment{amatrix}[1]{%
\left(\begin{array}{@{}*{#1}{c}|c@{}}
}{%
\end{array}\right)
}
and is used as:
\begin{amatrix}{2}
1 & 2 & 3 \\ a & b & c
\end{amatrix}
(note that the argument is one less than the total number of columns). I guess that the @{}
s at the start and end are to get the spacing right with the parentheses (mentioned by TH in a comment to fabikw's similar answer).
That style file has several other useful linear algebra macros that may be useful.
If you build your own environment using array, you're on the safe side. I would extend an internal macro of amsmath
using an optional argument.
Advantages:
It extends several matrix environments at the same time (
matrix
,pmatrix
,bmatrix
,Bmatrix
,vmatrix
,Vmatrix
).The names and meanings of those environments remain (not
apmatrix
etc.)Spacing etc. is the same like in
amsmath
.You could do more than just insert a vertical line (use color and alignment, for instance right aligned columns because of minus signs).
If you omit the optional argument, it acts exactly like the
amsmath
environment.
Caution:
- Since you redefine an internal macro, it might not work if the original package changes its code. But
amsmath.sty
has not been changed for more than 10 years. If there's a change in the matrices later, you could adjust your own macro.
Code:
Here's the redefinition, just put it in your preamble after loading amsmath
:
\makeatletter
\renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
\hskip -\arraycolsep
\let\@ifnextchar\new@ifnextchar
\array{#1}}
\makeatother
I had to use \makeatletter
... \makeatother
because of the @
in macro names.
The optional argument is the standard amsmath
argument to \array
. The original definition in amsmath
is:
\def\env@matrix{\hskip -\arraycolsep
\let\@ifnextchar\new@ifnextchar
\array{*\c@MaxMatrixCols c}}
So, there's not much that's been changed.
Examples:
Simple augmented matrix:
\begin{pmatrix}[cc|c]
1 & 2 & 3\\
4 & 5 & 9
\end{pmatrix}
More complex use, with different alignment, spacing and color:
\begin{bmatrix}[*2cr@{\quad}|@{\quad}>{\color{red}}r]
a & b & 1 & 4 \\
c & d & -2 & -3
\end{bmatrix}
Output:
Just remove the optional argument in brackets and you will get the standard pmatrix
or bmatrix
.
I've used this code and examples in a blog post in 2008: An extension to amsmath
matrix environments.
If you don't wish to redefine that internal macro, you could give it a different name and build your amatrix
environment on it exactly the same way that amsmath
does with pmatrix
.
If you are using an array to input the matrix, you just have to specify something like
\left(\begin{array}{cc|c}
2 & 0 & 1\\
0 & 1 & 1
\end{array}\right)