tags:

views:

375

answers:

2

I have some trees drawn with tikz and I'd like to have some text associated with them. I can use \caption within figure, but not tikzpicture.

UPDATE: Thanks for your advice. figure seems to put my first picture at the bottom of the page, and the second one on the second page. I would like these to be close to the paragraph like the tikzpicture was. Any ideas?

\par
Suppose we enter keys into a red-black tree in this order: 33, 47, 51, 50.
\begin{figure}
\begin{tikzpicture}
\node [blacknd] {33}
                child{ node [extnd] {}}
                child{ node [extnd] {}
        }
; \end{tikzpicture}
\caption{We start by adding 33 to an empty tree}
\end{figure}

\begin{figure}
\begin{tikzpicture}
\node [blacknd] {33}
                child{ node [extnd] {} }
                child{ node [rednd] {47}
                        child{ node [extnd] {} }
                        child{ node [extnd] {} }
        }
; \end{tikzpicture}
\caption{Then we add node 47. There are no imbalances to fix, since the new node's parent is black.}
\end{figure}

UPDATE: I figured it out from the excellent page here: http://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions#Figures

What I was looking for is the 'h' placement specifier.

+7  A: 

You need to put the tikzpicture in a figure; the following shows the caption fine:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{caption}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}[object/.style={thin,double,<->}]
  \draw[object] (7cm,-1cm) -- (6cm,-2cm) node[midway,above] {a label};
\end{tikzpicture}
\caption{A caption.}
\end{figure}

\end{document} 

If the above doesn't solve your problem, perhaps you could present a small code sample, so we can find your problem.

Ramashalanka
figure is like table just a *wrapper* which you can use to put figure and table related content inside. For example most of the time you will find a tabular environment inside a table. So, in a figure environment, it is most convenient to use something like includegraphics or some drawing package environments. This is additional information to the answer.
Rupert Jones
A: 

If you find yourself using the [h!] option a lot then you need to consider whether you really want to be using a figure. A figure is meant to be able to float so that it can go into a position that looks nice instead of at the place where you reference it. If you know that you definitely want the picture to be where you reference it then consider simply including the picture inline.

Look at how it's done in some books. I use Knuth's The Art of Computer Programming for ideas on placement etc., the TeX book is probably better but I don't have it.

Borbus