4

How do I add a caption to a tikz picture? This is a snippet of my latex:

\documentclass{article}


\usepackage{graphicx}
\usepackage{tikz}
\usepackage[paperwidth=40cm,paperheight=16cm]{geometry}
\usepackage{atbegshi}% http://ctan.org/pkg/atbegshi
\AtBeginDocument{\AtBeginShipoutNext{\AtBeginShipoutDiscard}}


\geometry{
 left=5mm,
 right=5mm,
 top=5mm,
 bottom=5mm,
 }


\begin{document}
\begin{tikzpicture}
%real picture
\node[inner sep=0pt] (realcoccus) at (0,12)
    {\includegraphics[scale=0.3]{realcoccuscropped.png}};
\node[inner sep=0pt] (realbacillus) at (0,7)
    {\includegraphics[scale=0.3]{realbacilluscropped.png}};
\node[inner sep=0pt] (realspirillum) at (0,2)
    {\includegraphics[scale=0.22]{realspirillumcropped.png}};



\end{tikzpicture}
\end{document}

I want to add a caption under each of the pictures:

\node[inner sep=0pt] (realcoccus) at (0,12)
    {\includegraphics[scale=0.3]{realcoccuscropped.png}};

\caption

doesn't work. Adding text looks bad.

How do I add captions?

Thanks

2
  • 1
    \caption must be used inside a float like figure or table. \captionof{figure} from the caption package can be used almost anywhere. The usual method is to put a tikzpicture inside a figure using \centering. Jul 13, 2015 at 22:13
  • I'm sorry I don't really understand what you mean. I've tried putting the node in a figure but that doesn't work :(
    – RNs_Ghost
    Jul 13, 2015 at 22:31

1 Answer 1

5

You can use \captionof from the capt-of or caption packages and place the captions inside nodes of the desired width (I chose \linewidth, but make the adjustments that best suit your needs); the positioning library was used to easily place the captions under the images:

enter image description here

The code:

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{capt-of}

\begin{document}

\noindent\begin{minipage}{\linewidth}
\begin{center}
\begin{tikzpicture}
\node[inner sep=0pt] (realcoccus) at (0,12)
    {\includegraphics[scale=0.3]{example-image-a}};
\node[inner sep=0pt,below=\belowcaptionskip of realcoccus,text width=\linewidth]
    {\captionof{figure}{the first image}};
\node[inner sep=0pt] (realbacillus) at (0,7)
    {\includegraphics[scale=0.3]{example-image-b}};
\node[inner sep=0pt,below=\belowcaptionskip of realbacillus,text width=\linewidth]
    {\captionof{figure}{the second image}};
\node[inner sep=0pt] (realspirillum) at (0,2)
    {\includegraphics[scale=0.3]{example-image-c}};
\node[inner sep=0pt,below=\belowcaptionskip of realspirillum,text width=\linewidth]
    {\captionof{figure}{the third image}};
\end{tikzpicture}
\end{center}
\end{minipage}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .