7

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?

0

4 Answers 4

10

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}

1
  • That's great, thank you. The arrows aren't necessary but this is perfect. Actually the arrows are not supposed to point at the separators but that's an easy fix.
    – Haffi112
    Feb 7, 2011 at 11:53
7

One could come up with similar results with just some TeX:

\documentclass{article}
\begin{document}
\vbox{
  \offinterlineskip\sf
  \def\mapup{$\Big\uparrow$}
  \halign{&\vrule\vphantom{$\bigg($}\hbox to 1.5in{\hfil#\hfil}\vrule\cr
    \noalign{\hrule}
    $<$pivot&$>=$pivot&?\cr
    \noalign{\hrule}
    \omit\mapup&\omit\llap{\mapup}&\omit\mapup&\omit\llap{\mapup}\cr
    \omit\strut Left&\omit\hidewidth\llap{pivotLocation\qquad}\hidewidth&\omit $i$&\omit\llap{Right}\cr
  }
}
\end{document}

4

run it with xelatex or use latex->dvips->ps2pdf

enter image description here

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{pst-node,tabularx,ragged2e}
\SpecialCoor
\renewcommand\tabularxcolumn[1]{>{\Centering}m{#1}}
\begin{document}

\sffamily\def\arraystretch{2}
\begin{tabularx}{\textwidth}{|X|X|X|}\hline
\rnode[lb]{l1}{}\hfill <pivot \hfill\rnode[rb]{r1}{}   & >=pivot &
\rnode[lb]{l3}{}\hfill ? \hfill\rnode[rb]{r3}{}\\\hline
\end{tabularx}
\psset{arrowscale=2,arrows=<-,offsetA=-10pt,offsetB=-15mm}
\ncline{l1}{l1}\ncline{r1}{r1}\ncline{l3}{l3}\ncline{r3}{r3}
\uput{16mm}[-90](l1){Left}
\uput{16mm}[-90](r1){pivot Location}
\uput{16mm}[-90](l3){i}
\uput{16mm}[-90](r3){Right}

\end{document}
2

Here is a slight variation on Jake's solution, concerning the lines. Instead of drawing the separators separately (!), you can have them drawn up directly, by drawing the shape of the nodes. The only problem is that they will not mesh perfectly without some tweaking, but this can be done by setting the column sep with \pgflinewidth.

The code for this is:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}

\matrix[matrix of nodes,column sep=-\pgflinewidth, name=matrix,every node/.style={draw},
        minimum height=0.75cm,minimum width=2cm,text depth=0.5ex]{
1&2&3\\
};

\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .