# What is the correct way to caption a tikzpicture?

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.

-

## migrated from stackoverflow.comSep 12 '11 at 21:58

This question came from our site for professional and enthusiast programmers.

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.

-
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 Feb 10 '10 at 8:16

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.

-
As @Borbus said above, the philosophy of LaTeX is to "float" figures. You can constrain that to some extent by writing \begin{figure}[<option>], where <option> is one or several of these: h - here, t - top, b - bottom, p - floats page, ! - force, e.g.: \begin{figure}[htb!] will try to insert first the figure right where it was specified, then try top of page, then bottom. It is very bad practice to use h! and some editors will autocorrect it to [ht!]. So, beware! –  Count Zero Sep 13 '11 at 9:36