Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

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.

share|improve this answer
add comment

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}

enter image description here

share|improve this answer
    
Would it be possible improve the vertical alignment of the "identification variables" and also position them "outside the [matrix] brackets (top)"? –  Werner Aug 26 '11 at 16:26
    
The position can be changed easily by putting them in a scope and adding e.g. node distance=.3cm as an option to the scope environment. The alignment can be improved by changing the anchor of the nodes to base. For me it didn't work to put this in the options to the scope environment (not sure why), but adding anchor=base to all the nodes worked fine. –  Torbjørn T. Aug 26 '11 at 17:23
    
Nice solution! However for my problem, it looks like the kbordermatrix-package may be the way. –  aagaard Aug 26 '11 at 21:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.