[Tex/LaTex] What’s the best way to draw loop invariants in LaTeX

diagramstikz-pgf

I want to draw a picture similar to this one for divide part of Quicksort.

Quicksort Loop Invariant

What's the best method to do so? Is there a package for it or should I use Tikz to do it? If Tikz can you point me to some reference on drawing similar pictures as the one above?

Best Answer

Here's one way to do it with TikZ (am I wrong in assuming the arrows are supposed to point at the separators between the boxes?):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
\matrix[matrix of nodes,
  name=matrix,
  draw, % Outer border
  inner sep=0pt,
  minimum width=2cm, % Width of boxes
  text depth=.5ex, % To ensure that lower edges line up correctly
  minimum height=.75cm, % Height of boxes
]{
  $<$ pivot & $\ge$ pivot & ?\\ % The nodes
};

% Separators
\draw (matrix-1-2.south west) -- (matrix-1-2.north west); 
\draw (matrix-1-3.south west) -- (matrix-1-3.north west);

% Define a style for all arrows, then draw the arrows with nodes
% This can be used to change the thickness or the tips for all arrows at once
\tikzstyle{arrowstyle}=[latex-]
\draw [arrowstyle] (matrix-1-1.south west) -- +(-90:0.8) node [anchor=north] {left};
\draw [arrowstyle] (matrix-1-2.south west) -- +(-90:0.8) node [anchor=north] {pivotLocation};
\draw [arrowstyle] (matrix-1-3.south west) -- +(-90:0.8) node [anchor=north] {i};
\draw [arrowstyle] (matrix-1-3.south east) -- +(-90:0.8) node [anchor=north] {right};-
\end{tikzpicture}

\end{document}