7

I'm trying to compile the exact same code suggested in the answer to How to Label and Caption TikzpictureS inside a tabular environment:

\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}
\listoffigures
\bigskip
\section*{Test table}
\begin{tabular}{p{4cm}p{4cm}}
   \begin{tikzpicture}
         \draw [red](0,0) -> (4,0);
   \end{tikzpicture}
   \captionof{figure}{Picture 1}
   \label{tikz1}
&
   \begin{tikzpicture}
         \draw (0,0) -> (4,0);
   \end{tikzpicture}
   \captionof{figure}{Picture 2}
   \label{tikz2}
\end{tabular}

See figs.~\ref{tikz1} and \ref{tikz2}.

\bigskip

\begin{tabular}{cc} \begin{tikzpicture} \draw red -> (4,0); \path[caption={Picture 3.\label{tikz3}}]; % <-- error at this line \end{tikzpicture}
& \begin{tikzpicture} \draw (0,0) -> (4,0); \path[caption={Picture 4.\label{tikz4}}]; % <-- error at this line \end{tikzpicture} \end{tabular}

See figs.~\ref{tikz3} and \ref{tikz4}. \end{document}

But I end up with ! Package tikz Error: Giving up on this path. Did you forget a semicolon?. at \path[caption={Picture 3.\label{tikz3}}];. I'm sure I already compiled successfully this code (and others using the same technique for adding the caption to a tikzpicture) a while back. The only thing I can think of is that newer versions of tikz (3.1.9a) or caption (3.6b) package are not compatible with this solution... Also the similar solution suggested by user194703 in Tikzpicture with caption causes the same problem.

Any suggestion would be appreciated!

Ninoett
  • 179

1 Answers1

8

It is always safest to use \captionof in a group (typically from a minipage or a similar enviroment). A change in the caption package means a group is needed here now.

\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}}}}}}% extra {}
\usepackage{caption}
\begin{document}
\listoffigures
\bigskip
\section*{Test table}
\begin{tabular}{p{4cm}p{4cm}}
   \begin{tikzpicture}
         \draw [red](0,0) -> (4,0);
   \end{tikzpicture}
   \captionof{figure}{Picture 1}
   \label{tikz1}
&
   \begin{tikzpicture}
         \draw (0,0) -> (4,0);
   \end{tikzpicture}
   \captionof{figure}{Picture 2}
   \label{tikz2}
\end{tabular}

See figs.~\ref{tikz1} and \ref{tikz2}.

\bigskip

\begin{tabular}{cc} \begin{tikzpicture} \draw red -> (4,0); \path[caption={Picture 3.\label{tikz3}}]; % <-- error at this line \end{tikzpicture}
& \begin{tikzpicture} \draw (0,0) -> (4,0); \path[caption={Picture 4.\label{tikz4}}]; % <-- error at this line \end{tikzpicture} \end{tabular}

See figs.~\ref{tikz3} and \ref{tikz4}. \end{document}

runs without error.

I do not know if this is an intended change, it may be worth raising with the caption package author.

David Carlisle
  • 757,742
  • Perfect! You saved me from finding a workaround and potentially having to modify tens of figures across several files! :) Thank you very much! – Ninoett Jul 25 '22 at 09:54
  • do you want to raise it with Axel, or I could? as I commented the package has always advised using captionof in a "safe" enviroment but the extra \aftergroup added in the latest version is a bit brutal to tikz here, he may be able to adjust. @Ninoett – David Carlisle Jul 25 '22 at 10:12
  • I hate to give you extra work, but my knowledge of LaTeX and of what's going on behind the curtains of the different packages is very limited so I'm not sure I'd be able to point him out the actual problem. If it would be enough to show him this page with your considerations, I could obviously take care of it, otherwise I'd say it's better if you contact him, if you don't mind! – Ninoett Jul 25 '22 at 11:32
  • 4
    OK I'll do it @Ninoett – David Carlisle Jul 25 '22 at 11:34
  • Thank you very much, again! – Ninoett Jul 25 '22 at 11:49