1

I thought I would find the answer HERE, but the solution does not seem to work in this particular case...

In general: How do you place captions for figures inside tikz nodes? I made the following MWE, but this is not the layout I use in my presentation, just a random one, so I need a general solution, not just for this example...

In this example, I just want to place a caption right below figure B, how should I do it?

MWE:

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\usepackage[labelformat=empty,font=scriptsize,skip=0pt,
justification=raggedright,singlelinecheck=false]{caption}

\begin{document}

\begin{frame}
    \begin{figure}
    \begin{tikzpicture}[remember picture, every node/.style={inner sep=0,outer sep=0}]

    \node[anchor=west] (A) at (0,0) {\includegraphics[width=2.25cm]{example-image-a}};

    \node[right=0.75cm of A.east, anchor=west] (B) {\includegraphics[width=2.25cm]{example-image-b}
        %\captionof{figure}{my caption here}%I was hoping this would work...
    };
    \node[below=\belowcaptionskip of B,text width=\linewidth]
    {\captionof{figure}{my caption here}};%And this doesn't work either...

    \node [right=0.75cm of B.east, anchor=west] (C) {\includegraphics[width=2.25cm]{example-image-c}};

    \node [below=0.75cm of A.south east, anchor=north west] (D) {\includegraphics[width=2.25cm]{example-image}};

    \node [right=0.75cm of D.east, anchor=west] (E) {\includegraphics[width=2.25cm]{example-image}};

    \node [right=0.75cm of E.east, anchor=west] (F) {\includegraphics[width=2.25cm]{example-image}};
    \end{tikzpicture}
    \end{figure}
\end{frame}

\end{document}

Produces:

fig

2 Answers 2

4

In a case, when your caption has only one line, you can obtain desired result without use of caption package and figure environment:

\documentclass{beamer}
\usepackage{lmodern,tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{frame}
    \begin{center}
    \begin{tikzpicture}[node distance=2mm and 5mm]
\node              (A)  {\includegraphics[width=2.25cm]{example-image-a}};
\node [right=of A] (B)  {\includegraphics[width=2.25cm]{example-image-b}};
\node [right=of B] (C)  {\includegraphics[width=2.25cm]{example-image-c}};
    \node (capt) [below=of B,text width=\linewidth, align=center,font=\scriptsize]
                        {my caption here};
\node [below=of A |- capt.south] (D) {\includegraphics[width=2.25cm]{example-image}};
\node [right=of D] (E)  {\includegraphics[width=2.25cm]{example-image}};
\node [right=of E] (F)  {\includegraphics[width=2.25cm]{example-image}};
    \end{tikzpicture}
    \end{center}
\end{frame}
\end{document}

enter image description here

However, you can split TikZ picture into two parts:

\documentclass{beamer}
\usepackage{lmodern,tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{frame}
    \begin{figure}\centering
    \begin{tikzpicture}[node distance=2mm and 5mm]
\node              (A)  {\includegraphics[width=2.25cm]{example-image-a}};
\node [right=of A] (B)  {\includegraphics[width=2.25cm]{example-image-b}};
\node [right=of B] (C)  {\includegraphics[width=2.25cm]{example-image-c}};
    \end{tikzpicture}
\caption{my caption here}
    \begin{tikzpicture}[node distance=2mm and 5mm]
\node              (D) {\includegraphics[width=2.25cm]{example-image}};
\node [right=of D] (E)  {\includegraphics[width=2.25cm]{example-image}};
\node [right=of E] (F)  {\includegraphics[width=2.25cm]{example-image}};
    \end{tikzpicture}
    \end{figure}
\end{frame}
\end{document}

If you not like to have Figure label at caption, you only need to set captiontemplate accordingly, for example add to preamble:

\setbeamerfont{caption}{size=\scriptsize}
\setbeamertemplate{caption}{\insertcaption\par}

Addadendum: It is not clear to me, what is purpose of TikZ nodes. Images in nodes you can arrange in the same frame form as before without them:

\documentclass{beamer}
\setbeamerfont{caption}{size=\scriptsize}
\setbeamertemplate{caption}{\insertcaption\par}

\begin{document}
\begin{frame}
    \begin{figure}\centering
\includegraphics[width=2.25cm]{example-image-a}\hfil
\includegraphics[width=2.25cm]{example-image-b}\hfil
\includegraphics[width=2.25cm]{example-image-c} 

\caption{my caption here}

\includegraphics[width=2.25cm]{example-image}\hfil
\includegraphics[width=2.25cm]{example-image}\hfil
\includegraphics[width=2.25cm]{example-image}
    \end{figure}
\end{frame}
\end{document}

The image of frame is the same as before, but code is more simple and concise.

3
  • Yes that was just an example that can be easily reproduced without nodes, but I have more complicated diagrams that include images that required an article citation under them (cause I took them from there, they are not mine...). So I guess the only solution in these cases would be to place another (non caption) node with the same characteristics as my captions under those images, which is what I wanted to avoid...
    – DaniCee
    Feb 2, 2017 at 4:14
  • as in your first solution
    – DaniCee
    Feb 2, 2017 at 4:14
  • I guess I could apply it in this other problem I have... tex.stackexchange.com/questions/351673/…
    – DaniCee
    Feb 2, 2017 at 4:15
2

Use the justification=centering option in the caption package.

\documentclass{beamer}
\usepackage{lmodern,tikz}
\usetikzlibrary{positioning,calc}
\usepackage[labelformat=empty,font=scriptsize,skip=0pt,
justification=centering,singlelinecheck=false]{caption}

\begin{document}
\begin{frame}
    \begin{figure}\centering    
    \begin{tikzpicture}
    \node [anchor=west] (A) at (0,0) {\includegraphics[width=2.25cm]{example-image-a}};
    \node [right=0.75cm of A.east, anchor=west] (B) {\includegraphics[width=2.25cm]{example-image-b}};
    \node [below=\belowcaptionskip of B,text width=\linewidth]{\captionof{figure}{my caption here}};    
    \node [right=0.75cm of B.east, anchor=west] (C) {\includegraphics[width=2.25cm]{example-image-c}};
    \node [below=0.75cm of A.south, anchor=north] (D) {\includegraphics[width=2.25cm]{example-image}};
    \node [right=0.75cm of D.east, anchor=west] (E) {\includegraphics[width=2.25cm]{example-image}};
    \node [right=0.75cm of E.east, anchor=west] (F) {\includegraphics[width=2.25cm]{example-image}};    
    \end{tikzpicture}
    \end{figure}
\end{frame}
\end{document}

enter image description here

1
  • This is not a universal solution... what if the node is just floating around in any random place? I am facing this problem too tex.stackexchange.com/questions/351673/… which could be solved if I could just put a caption under any node anywhere...
    – DaniCee
    Feb 2, 2017 at 4:10

You must log in to answer this question.

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