4

How I do a caption in Tikz? e.g.

\begin{tikzpicture}
\draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,0);
\caption{Cuadrado}
\end{tikzpicture}

is an error.

pga11
  • 51

2 Answers2

11

A caption is a thing you attach to a figure; a tikzpicture is like an image, or a block of text, or whatever you want to put into a picture.

Try:

\begin{figure}
    \begin{tikzpicture}
        \draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,0);
    \end{tikzpicture}
    \caption{Un cuadrado}
\end{figure}
Rmano
  • 40,848
  • 3
  • 64
  • 125
  • Okay thanks but, does it work in class article and book? – pga11 Mar 30 '20 at 19:46
  • ...have you tried? ;-) – Rmano Mar 30 '20 at 19:47
  • Yes, I've tried, but in class book there are two errors, "Not in outer par mode" and "Undefined control sequence" – pga11 Mar 30 '20 at 19:52
  • If you insert code provided in @Rmano answer., mentioned erors cannot happen. – Zarko Mar 30 '20 at 19:55
  • 1
    @luis.pga please prepare a minimal working example (MWE) that shows the errors and post it, better if in another question. The problem in posting snippets, as you did, is that it's impossible to know what packages and options you have --- my cristall ball is broken and I can go out to repair it... ;-) – Rmano Mar 30 '20 at 20:35
  • 1
    @luis.pga I can reproduce your errors if I wrap the above code into $...$. The above answer is correct, wrapping the code in a math environment is not. –  Mar 30 '20 at 21:32
  • In more detail, \documentclass{article} \usepackage{tikz} \begin{document} $ \begin{figure} \begin{tikzpicture} \draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,0); \end{tikzpicture} \caption{Un cuadrado} \end{figure} $ \end{document} reproduces the error messages, but this is not the fault of the answer here, rather, it is because of an incorrect usage. –  Mar 30 '20 at 21:34
  • 1
    Okay guys, forget it, I had an error in my code, your solution is correct, Thanks and sorry for bothering you.! – pga11 Mar 30 '20 at 21:42
5

To be clear, Rmano's answer is the way to go in almost all realistic cases. However, exceptionally there can be good reasons to just add a caption to some tikzpicture, e.g. when you arrange them in a tabular. In this case you can do

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{caption/.style={insert path={
let \p1=($(current bounding box.east)-(current bounding box.west)$) in
(current bounding box.south) node[below,text width=\x1-4pt,align=center] 
{\captionof{figure}{#1}}}}}
\usepackage{caption}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,0);
\path[caption=Cuadrado];
\end{tikzpicture}
\end{document}

enter image description here

Or you can define a style that automatically puts the caption at the end of the tikzpicture.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{caption/.style={execute at end picture={\path
let \p1=($(current bounding box.east)-(current bounding box.west)$) in
(current bounding box.south) node[below,text width=\x1-4pt,align=center] 
{\captionof{figure}{#1}};}}}
\usepackage{caption}
\begin{document}
\begin{tikzpicture}[caption=Cuadrado]
\draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,0);
\end{tikzpicture}
\end{document}