# caption in a tikz object

I created markov's chain using tikz package. Now I would add to to it caption and label but I got error.How can I add a caption and a label to a tikz object?

• Please provide a MWE. The question as you stated it is way to broad to give a meaningful answer. By the way, did you include your TikZ picture in a figure environment? – Count Zero Nov 22 '12 at 15:29
• no I didn't include in a figure environment – Mazzy Nov 22 '12 at 15:30
• Then I suggest: \begin{figure}\begin{tikzpicture}...\end{tikzpicture}\caption{whatever}\label{fig:my_fav_tikz}\end{figure}. That should do the trick. :) – Count Zero Nov 22 '12 at 15:32
• Ok now it works...I simply add figure environment – Mazzy Nov 22 '12 at 15:32
• @Mazzy it's not mandatory to use a figure environment; it depends on whether you want the object to float or not. Please see my answer below for more details. – Gonzalo Medina Nov 22 '12 at 15:41

In case you want to use floating environments, you will need to include the TikZ picture in a figure environment:

\begin{figure}
\begin{tikzpicture}
...% TikZ code goes here
\end{tikzpicture}
\caption{whatever}
\label{f‌​ig:my_fav_tikz}
\end{figure}


If you don't want floats, see Gonzalo Medina's answer.

• Well, my answer also includes the float case. – Gonzalo Medina Nov 22 '12 at 15:45
• Ok, I didn't see that... I'll delete my answer. – Count Zero Nov 22 '12 at 15:57
• can't delete - been accepted... – Count Zero Nov 23 '12 at 0:12

If you don't want the object to float, you can use, for example, a minipage and \captionof from the caption package.

If you want the object to be treated as a floating object, you can enclose it within a figure environment.

A simple example illustrating both approaches:

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

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\draw (0,0) circle (1cm);
\end{tikzpicture}
\caption{A tikz circle}
\label{fig:testb}
\end{figure}

\noindent\begin{minipage}{\textwidth}
\centering
\begin{tikzpicture}
\draw (0,0) circle (1cm);
\end{tikzpicture}
\captionof{figure}{A tikz circle}
\label{fig:testa}
\end{minipage}

\end{document}


• minipage may be better than center, as that prohibits a pagebreak between figure and caption. – Torbjørn T. Nov 22 '12 at 15:37
• @TorbjørnT. you're right. Change made. Thanks. – Gonzalo Medina Nov 22 '12 at 15:39
• I tried the first way you in your answer, the tikzpicture and the caption are OK, but strangely the picture appear another place, this is to say it does follow orders of the article. So , can you help me Gonzalo? – Nax Apr 22 '14 at 13:05
• @Alex Sure, but we need the code, otherwise little help can be provided. Please open a follow-up question and provide there a minimal version of a document (like the one in my answer) illustrating the problem; you can also provide a link to this answer to provide some background. – Gonzalo Medina Apr 22 '14 at 13:08
• thX, I know where the problem lies – Nax Apr 22 '14 at 14:30