[Tex/LaTex] Create a math matrix in TikZ partition lines

math-modematricestikz-pgf

I want to draw a math matrix in TikZ, where I can draw helping partition lines. So far I found it pretty simple to just draw a matrix with brackets.

\begin{tikzpicture}
  \matrix [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  \\
  };
\end{tikzpicture}

However, I would like to partition the matrix with a vertical line between columns 5 and 6, and a horizontal line between rows 2 and 3. Furthermore it could nice to have "identification variables" outside the brackets (top).

Matrix with formatting

Best Answer

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}

Matrices made using kbordermatrix and blkarray

The code can be modified to accommodate your needs to improve alignment and spacing. In both examples you can add row indices as well.

Related Question