39

Possible Duplicate:
What's the best way make an “augmented” coefficient matrix?

I am trying to make a vertical line in a matrix in LaTeXiT. I have read that it should be possible using the following:

\begin{bmatrix}{cccc|c}
1 & 0 & 3 & -1 & 0 \\
0 & 1 & 1 & -1 & 0 \\
0 & 0 & 0 & 0 & 0 \\
\end{bmatrix}

But when doing so, this is my output:

enter image description here

SimonBS
  • 493

2 Answers2

67

The bmatrix environment does not provide this facility (see section 4.1 of the amsmath package documentation). You can use the array environment instead.

\[
\left[
\begin{array}{cccc|c}
1 & 0 & 3 & -1 & 0 \\
0 & 1 & 1 & -1 & 0 \\
0 & 0 & 0 & 0 & 0 \\
\end{array}
\right]
\]
Moriambar
  • 11,466
Ian Thompson
  • 43,767
  • 16
    Possibly adding @{} at the sides: @{}cccc|c@{}, so as to give a similar result to bmatrix. – egreg Nov 03 '11 at 09:33
  • Thank you very much. The output of this is just like a bmatrix. – SimonBS Nov 03 '11 at 09:43
  • 4
    @egreg: Could you explain how the @{} works? I see that it has the effect of reducing the whitespace between the [ and ] and the matrix contents. – user001 Mar 13 '12 at 17:30
  • 3
    With @{...} you tell LaTeX to put ... in place of the default intercolumn space, which is applied also at the start and end of the tabular. – egreg Mar 13 '12 at 17:37
44

That won't work with the matrix environment from amsmath, however Stefan Kottwitz wrote about a workaround for this on his blog.

\documentclass{article}
\usepackage{amsmath}
\makeatletter
\renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
  \hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{#1}}
\makeatother
\begin{document}
\begin{equation}
  \begin{bmatrix}[cccc|c]
   1 & 0 & 3 & -1 & 0 \\
   0 & 1 & 1 & -1 & 0 \\
   0 & 0 & 0 & 0 & 0 \\
\end{bmatrix}
\end{equation}
\end{document}

enter image description here

Torbjørn T.
  • 206,688
  • 19
    @barbarabeeton This would be a nice addition for amsmath, wouldn't it? – egreg Nov 03 '11 at 09:36
  • @Chou It's up to the person asking the question to decide which answer was most helpful for him or her. It's not always the highest voted answer that is accepted. – Torbjørn T. Jun 21 '15 at 07:37
  • I know the rules here. You may take my words as an indirect praise; for your work helped me. :) By the way, I was not aware of the number of votes til your comment coming out. – Yes Jun 21 '15 at 09:30
  • The double-backslash line-break indicators seem to have morphed into single-backslash spacers. You may want to edit the code a bit. – Mico Jan 23 '17 at 12:18
  • @Mico Thanks, will edit. (It's a well known problem as you may know, http://meta.tex.stackexchange.com/questions/7168/double-backslashes-disappear-from-code) – Torbjørn T. Jan 23 '17 at 12:26
  • Aah, I had missed this recent meta.tex posting. I guess I'll need to be on the lookout for this issue in my own answers. Sigh. – Mico Jan 23 '17 at 12:46
  • On overleaf.com the vertical line does not work. It shows [cc|c] on cell 1 of the matrix. – user3123159 Feb 01 '24 at 11:29
  • 1
    @user3123159 The example in my answer worked fine when I tested it on Overleaf just now, so I would guess there is something else causing the problem. I suggest you make a small, complete example demonstrating the problem and ask a new question. – Torbjørn T. Feb 02 '24 at 16:13
  • it works in the pdf view, but not in visual editor. Would be good if it works in the editor as well. – user3123159 Feb 02 '24 at 21:39
  • @user3123159 Oh, the visual editor, that's a different story. I don't think you can expect stuff like this to work there, I believe it uses JavaScript library called MathJax to render the math, and I don't think that could possibly parse all sorts of customizations people might do in their preambles to math stuff. But if you want a definitive answer to whether it is possible, ask Overleaf support. – Torbjørn T. Feb 02 '24 at 23:08