# How to put a subcaption inside a tikzpicture?

I have put a few images inside a tikzpicture. Now I want to add a subcaption for each of them. How is this possible? I also tried \captionof which didn't work either.

Minimum not working example:

\documentclass[12pt]{article}
\usepackage{caption,subcaption,tikz}
\usetikzlibrary{positioning}

\begin{document}
asdf
\begin{figure}[t]
\centering
\begin{tikzpicture}[font=\small]
\node (a){\includegraphics[width=5cm]{example-image-a}};
\node [below= of a]{\subcaption{hello}\label{subfig:hi}};
\end{tikzpicture}
\caption{caption}
\label{fig:captionlabel}
\end{figure}

\end{document}

• You are loading package subcaption, why don't you use it? You don't need tikz at all to do hat you want. Oct 31 '15 at 12:36
• Because the real image is much more complex. Oct 31 '15 at 12:38
• Doesn't matter, the image is the image, the caption is the caption. A caption should never be in a tikzpicture. Oct 31 '15 at 12:39
• I like the code reduction capability of tikz foreach to arrange all figures in a loop, instead of opening 8 subfigures. Why not use it if \captionof is possible Oct 31 '15 at 13:03

You have to either use text width as the node option or use a \parbox (or minipage) so that the \captionof is inside a group.

\documentclass[12pt]{article}
\usepackage{caption,subcaption,tikz}
\usetikzlibrary{positioning}

\begin{document}
asdf
\begin{figure}[t]
\centering
\begin{tikzpicture}[font=\small]
\node[draw,inner sep=0pt] (a){\includegraphics[width=5cm]{example-image-a}};
\node [below= 1ex of a.south,inner sep=0pt]{\parbox{\linewidth}{\captionof{subfigure}{hello\label{subfig:hi}}}};
\end{tikzpicture}
\caption{caption}
\label{fig:captionlabel}
\end{figure}

\end{document}


• Works like a charme! The only drawback is that the subcaptions are linked to the previous figure. Do you know how to prevent this? Oct 31 '15 at 12:51
• @fmetz: Sorry I don't understand. How they are linked to previous image?
– user11232
Oct 31 '15 at 12:56
• because the figure caption is declared after the tikzpicture, the subcaptions are referenced as 4.4a, 4.4b instead of 4.5a, 4.5b if the current figure is 4.5 Oct 31 '15 at 13:00
• @fmetz: Generally the subfigures won't be displayed as 1.5a but just a. Can you show the code that does this?
– user11232
Oct 31 '15 at 13:06
• I mean if you do a \ref{subfig:abc} in text. Oct 31 '15 at 13:07

Do you mean something like this?

\documentclass{article}

\usepackage{subcaption}
\usepackage{tikz}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\filldraw[red] (0,0) circle (3pt);
\end{tikzpicture}
\caption{Simple \texttt{tikz} picture}
\label{fig:TikzSimple}
\end{figure}

\begin{figure}
\begin{subfigure}[b]{.5\linewidth}
\centering\large A
\caption{A subfigure}
\label{subfig:SubfigureLeft}
\end{subfigure}%
\begin{subfigure}[b]{.5\linewidth}
\centering\large B
\caption{Another subfigure}
\label{subfig:SubfigureRight}
\end{subfigure}
\caption{Example from the \texttt{subcaption} manual, page 5}
\label{fig:SubfigureManuel}
\end{figure}

\begin{figure}
\begin{subfigure}[b]{.5\linewidth}
%
\centering
\begin{tikzpicture}
\filldraw[red] (0,0) circle (3pt);
\end{tikzpicture}
%
\caption{A subfigure}
\label{subfig:CombinationLeft}
\end{subfigure}%
\begin{subfigure}[b]{.5\linewidth}
%
\centering
\begin{tikzpicture}
\filldraw[blue] (0,0) circle (3pt);
\end{tikzpicture}
%
\caption{Another subfigure}
\label{subfig:CombinationRight}
\end{subfigure}
\caption{Combination of \figurename~\ref{fig:TikzSimple} and \ref{fig:SubfigureManuel}.}
\label{fig:Combination}
\end{figure}

\end{document}
`

• Unfortunately, no. Oct 31 '15 at 12:55
• That's a tikz picture into a subcaption, not a subcaption into a tikz picture. Feb 23 '17 at 13:05