77

When studying systems of linear equations, it's nice to remind people that the last column of the coefficient matrix holds the constants. This is often done in books by putting a vertical line between the last column and the next to last column. What is a good way to do this in LaTex?

10 Answers 10

62

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.

4
  • 2
    This example doesn't work for me. pdflatex says Missing $ inserted. Commented Mar 12, 2017 at 14:27
  • 8
    For latex newbies (like me): You have to put the example inside \[ and \] latex codes. Commented Mar 12, 2017 at 14:40
  • 1
    I wanted to make it so the argument matched the total number of columns. A solution from my question here: tex.stackexchange.com/q/571599/37581 showed that replacing {#1} with {\numexpr#1-1} does exactly what I wanted. I thought it would be helpful to others using this solution so thought I'd mention it here. Commented Nov 20, 2020 at 12:21
  • Here is a related question to the @{} used.
    – fmatt
    Commented Oct 28, 2021 at 10:12
67

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:

alt text

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.

4
  • 1
    From some quick testing it seems to do nothing to the starred versions of the matrix commands from mathtools.sty, but it's not like that matters much when you have full-featured array options available for all your matrix commands
    – kahen
    Commented Nov 6, 2011 at 2:27
  • I doubt anyone will see this, but is there a way to have a default alignment? E.g. Default to aligning the columns right even if no arguments are specified? If not, is there a way to specify general right alignment without putting as many r's as there are columns? Commented Apr 21, 2020 at 11:20
  • Note that this method will occasionally change the behavior -- \begin{matrix} <newline> [s] & [t] <newline> \end{matrix} will (despite the newline) interpret the [s] as the optional parameter. It's possible to wrap [s] in braces, or add \relax before [s] to fix the issue ____________________________________________________________________________________________ For documentation of @ifnextchar, see tex.stackexchange.com/q/4790 and new@ifnextchar see ctan.math.washington.edu/tex-archive/macros/latex/required/…
    – user202729
    Commented Oct 20, 2021 at 14:04
  • Oh, I found an older comment from me. Anyway, you can change the default alignment to right by simply replacing [*\c@MaxMatrixCols c] with [*\c@MaxMatrixCols r] in the above declaration. Go figure. Commented Jan 21, 2022 at 2:47
26

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)
4
  • 3
    That's not a good way to do it. The spacing is wrong between the parentheses and the matrix.
    – TH.
    Commented Aug 24, 2010 at 4:42
  • Wouldn't \bigl and \bigr be better here?
    – user914
    Commented Aug 24, 2010 at 5:17
  • 4
    As Andrew Stacey points out, @{} is all that's needed to fix up the spacing. That was eluding me.
    – TH.
    Commented Aug 24, 2010 at 7:27
  • 2
    I like that this answer does not require any unusual packages or difficult to understand hacks.
    – shuhalo
    Commented Jan 19, 2020 at 23:39
21

If you would rather not override styles, you can create an augmented matrix using standard matrix commands like so:

\begin{pmatrix}
    1 & 0 & 0 & 0 &\bigm| & 0 \\
    0 & 1 & 0 & 0 &\bigm| & 5 \\
    0 & 0 & 1 & 0 &\bigm| & -4 \\ 
    0 & 0 & 0 & 1 &\bigm| & -2
\end{pmatrix}

generating the following.

An augmented matrix.

1
  • Thank you! This will work in KaTeX.
    – ifconfig
    Commented Oct 5, 2020 at 22:03
11

If for some reason (e.g. you try to make augmented matrix in Pages) other answers do not work, you can make an augmented matrix with two matrices inside delimiters of your choice:

\left[
  \begin{matrix}
    1 & 2 & 3 \\
    1 & 2 & 3 \\
    1 & 2 & 3 \\
    1 & 2 & 3 \\
  \end{matrix}
  \left|
    \,
    \begin{matrix}
      4  \\
      4  \\
      4  \\
      4  \\
    \end{matrix}
  \right.
\right]

This code produces a matrix like this:
Matrix example

You can modify space between delimiters by adding \, or other spacers.

2
  • Thank you! Works perfectly, +1 Commented Apr 17, 2020 at 20:04
  • I joined this community purely to upvote this answer. Commented Feb 8 at 2:17
9

The easybmat package provides a way to add all sorts of lines between cells in a matrix, including dashed lines which I seem to recall being popular in my undergraduate linear algebra text.

Edit: Here's an example. I'm actually not thrilled about the spacing.

\[\left(\begin{BMAT}(@){cc.c}{cc}
a_{11} & a_{12} & b_1\\
a_{21} & a_{22} & b_2
\end{BMAT}\right)\]
1
  • The spacing between entries are adjusted using the optional argument of BMAT environment. For example, using \begin{BMAT}[10pt] gives a much bigger matrix.
    – AgentSmith
    Commented Sep 3, 2020 at 2:18
9

The package nicematrix provides tools to draw mathematical matrices. There is no environment for augmented matrices in this package but it's easy to create one (compatible with the other features of nicematrix: dotted lines, blocks, exterior rows and columns, etc.).

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}

\makeatletter
\ExplSyntaxOn
\NewDocumentEnvironment { pNiceMatrixAug } { }
  { \begin { pNiceMatrix } }
  { 
    \CodeAfter \tikz \draw (1-|\arabic{jCol}) -- (last-|\arabic{jCol}) ; 
    \end { pNiceMatrix } 
  }

\ExplSyntaxOff
\makeatother

\begin{document}

$\begin{pNiceMatrixAug}
a_{11} & a_{12} & \Cdots & a_{1n} & 0 \\
a_{21} & a_{22} & \Cdots & a_{2n} & 0 \\
\Vdots &       &        & \Vdots & \Vdots \\
a_{n1} & a_{n2} & \Cdots & a_{nn} & 0 \\
\end{pNiceMatrixAug}$

\end{document}

However, you need several compilations.

Output of the first code


EDIT: In order to have the ability to use \CodeBefore (and \Body) and \CodeAfter as in all the environment of nicematrix, and a color of the vertical rule which respects the key rules\color (and \arrayrulecolor), we must write an environment {pNiceMatrixAux} a bit more complicated:

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}

\makeatletter
\ExplSyntaxOn
\NewDocumentEnvironment { pNiceMatrixAug } { }
  { \pNiceMatrix } 
  { 
    \tl_gput_right:Nn \g_nicematrix_code_after_tl
      { 
        \group_begin:
        \CT@arc@ % for the color of the rule
        \tikz \draw (1-|\arabic{jCol}) -- (last-|\arabic{jCol}) ; 
        \group_end:
      }
    \endpNiceMatrix
  }
\ExplSyntaxOff
\makeatother

\begin{document}

$\begin{pNiceMatrixAug}[rules/color=blue,left-margin]
\CodeBefore 
  \cellcolor{red!15}{1-1,2-2,4-4}
\Body  
a_{11} & a_{12}  & \Cdots & a_{1n} & 0 \\
a_{21} & a_{22}  & \Cdots & a_{2n} & 0 \\
\Vdots & \Vdots &        & \Vdots & \Vdots \\
a_{n1} & a_{n2}  & \Cdots & a_{nn} & 0 \\
\CodeAfter
  \line{2-2}{4-4}
\end{pNiceMatrixAug}$

\end{document}

Output of the second code

5

I would do something like this:

{
\centering
$$
\left(
\begin{array}{ccc|c}
1 & 1 & 1 & 1 \\ 
8 & 4 & 2 & 1 \\ 
64 & 16 & 4 & 1 \\ 
    \end{array}
\right)
$$
}
4
  • 2
    What's \centering doing? Also, $$ is best avoided in LaTeX; prefer \[..\].
    – egreg
    Commented Sep 3, 2013 at 13:59
  • Without the \centering, the matrix would be aligned to the left.
    – qed
    Commented Sep 3, 2013 at 14:21
  • 4
    In math mode there is no alignment it's centered by default. See also tex.stackexchange.com/questions/503/why-is-preferable-to .
    – percusse
    Commented Sep 3, 2013 at 19:36
  • Indeed. But last time I used this, I could not have the matrix centered without using the \centering command. Very strange.
    – qed
    Commented Sep 4, 2013 at 13:52
1

A simple and clean solution for an augmented matrix can be obtained with the tabularray package.

result

\documentclass{article}

\usepackage{tabularray}

\NewDocumentEnvironment{Abmatrix}{+b}{
\left[
\begin{tblr}{rowsep = {1pt}, colsep = {5pt}, column{1} = {leftsep = 0pt}, column{Z} = {rightsep = 0pt}, cells = {c}, vline{Y} = {dashed, 0.75pt}}
#1
\end{tblr}
\right]
}{}

\begin{document}

An augmented bmatrix:

\[
\begin{Abmatrix}
2 & -1 & 2 & 9 \\
1 & -2 & 4 & 12 \\
3 & 1 & -1 & 2
\end{Abmatrix}
\]

And another one:

\[
\begin{Abmatrix}
3 & 5 & 7 & 8 & 3 \\
6 & -2 & -3 & 12 & 5
\end{Abmatrix}
\]
\end{document}
0
\left[\begin{array}{cccc|c}
1 & 0 & 1 & -1 & 0 \\
-1 & 1 & -2 & -1 & 0 \\
1 & -2 & 3 & 2 & 0
\end{array}\right]
1
  • 1
    How is this answer different form fabikw's answer? Commented Aug 1, 2022 at 8:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .