Create a math matrix in TikZ partition lines
Not to go entirely upstream, but there are also some non-TikZ options using the kbordermatrix
and blkarray
packages:
\documentclass{article}
\usepackage{kbordermatrix}% http://www.hss.caltech.edu/~kcb/TeX/kbordermatrix.sty
\usepackage{blkarray}% http://ctan.org/pkg/blkarray
\begin{document}
\[
\kbordermatrix{%
& x & y & z & w & r & & M \\
& 1 & 2 & 1 & 0 & 0 & \vrule & 10 \\
& 3 & 2 & 0 & 1 & 0 & \vrule & 20 \\ \cline{2-8}
& -2 & -10 & 0 & 0 & 1 & \vrule & 0
}
\]
\[
\begin{blockarray}{crrrrrrc}
& x & y & z & w & r & M &\\
\begin{block}{[crrrrr|rc]}
& 1 & 2 & 1 & 0 & 0 & 10 & \\
& 3 & 2 & 0 & 1 & 0 & 20 & \\ \BAhhline{~------~}
& -2 & -10 & 0 & 0 & 1 & 0 & \\
\end{block}
\end{blockarray}
\]
\end{document}
The code can be modified to accommodate your needs to improve alignment and spacing. In both examples you can add row indices as well.
A basic approach:
Once you named you matrix (mymatrix
in this case), you can refer to it in the mymatrix-row-column
notation. Usual positioning such as above=of
will also work.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\begin{document}
\begin{tikzpicture}[node distance=-1ex]
\matrix (mymatrix) [matrix of math nodes,left delimiter={[},right
delimiter={]}]
{
1 & 2 & 1 & 0 & 0 & 10 \\
3 & 2 & 0 & 1 & 0 & 20 \\
-2 & -10 & 0 & 0 & 1 & 0 \\
};
\draw[red] (mymatrix-2-1.south west) -- (mymatrix-2-6.south east);
\draw[red] (mymatrix-1-5.north east) -- (mymatrix-3-5.south east);
\node [above=of mymatrix-1-1.north] {x};
\node [above=of mymatrix-1-2.north] {y};
\node [above=of mymatrix-1-3.north] {z};
\node [above=of mymatrix-1-4.north] {w};
\node [above=of mymatrix-1-5.north] {r};
\node [above=of mymatrix-1-6.north] {M};
\end{tikzpicture}
\end{document}