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.
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.
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}
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}
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}
tikzpicture environment such as tikzcd or smartdiagram. "Hack" is not meant to be negative here, it just means "customize".
–
Mar 30 '20 at 21:41
articleandbook? – pga11 Mar 30 '20 at 19:46bookthere are two errors, "Not in outer par mode" and "Undefined control sequence" – pga11 Mar 30 '20 at 19:52$...$. The above answer is correct, wrapping the code in a math environment is not. – Mar 30 '20 at 21:32\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