2

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?

5
  • 1
    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, 2012 at 15:29
  • no I didn't include in a figure environment
    – Mazzy
    Nov 22, 2012 at 15:30
  • 1
    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, 2012 at 15:32
  • Ok now it works...I simply add figure environment
    – Mazzy
    Nov 22, 2012 at 15:32
  • 1
    @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. Nov 22, 2012 at 15:41

2 Answers 2

2

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.

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

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}

enter image description here

5
  • 1
    minipage may be better than center, as that prohibits a pagebreak between figure and caption. Nov 22, 2012 at 15:37
  • @TorbjørnT. you're right. Change made. Thanks. Nov 22, 2012 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, 2014 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. Apr 22, 2014 at 13:08
  • thX, I know where the problem lies
    – Nax
    Apr 22, 2014 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.