Caption a tikzpicture on standalone

Title says it all:

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{arrows}
\usepackage{caption}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
title=myTtitle,
xlabel=$x$,
ylabel=$y$]
\addplot coordinates {
(10, 1)
(20, 2)
};
\end{axis}
\end{tikzpicture}
\caption{A caption.}
\end{figure}
\end{document}

Error:

LaTeX Error: Something's wrong--perhaps a missing \item.

on \begin{figure}.

Shall you change standalone to document it will work, but it will make the plot so small in comparison with the standalone version... :/

• Add the varwidth option to the documentclass, but why do you want to do this at all? Sep 12, 2016 at 20:15
• or preview, result differ from case of use varwidth (caption is centerred). Sep 12, 2016 at 20:32
• standalone is meant to provide the smallest document page possible and you are trying to put a float in it which obviously cannot float Sep 12, 2016 at 20:58
• I assume you want to use this to test the figure before copying it into the document WITH NO CHANGES. The simplest fix is to place the figure inside a minipage with some predetermined width. (Note, all the figure does is set \@captype to figure.) Sep 12, 2016 at 21:10

2 Answers

The same result as with John Kormylo answer you obtain with option preview of standalone package (with out use of mini pages), so see extended version of my comment:

\documentclass[preview]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{caption}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
name=border,
title=myTtitle,
xlabel=$x$,
ylabel=$y$]
\addplot coordinates {
(10, 1)
(20, 2)
};
\end{axis}
\end{tikzpicture}
\caption{A caption.}
\end{figure}
\end{document}

Just for fun, I centered the axis box rather than the "axis plus labels" (assuming the left offset is larger than the right offset).

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{arrows}
\usepackage{caption}

\newlength{\offset}

\begin{document}
\begin{minipage}{5in}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
name=border,
title=myTtitle,
xlabel=$x$,
ylabel=$y$]
\addplot coordinates {
(10, 1)
(20, 2)
};
\end{axis}
\pgfextractx{\offset}{\pgfpointanchor{current bounding box}{west}}% center axis, not labels
\path (border.east) +(-\offset,0);
\end{tikzpicture}
\caption{A caption.}
\end{figure}

\end{minipage}
\end{document}
• please, can you explain, what connection has your answer to this question? Sep 12, 2016 at 21:27
• The minipage answers the question. It just looked odd with the caption not aligned with the title. Sep 12, 2016 at 21:32
• Well, I must confess, that I don't understand your answer. By the way, in your MWE is missing \documentclass. And both first comments above somehow solved the OP problem. Apparently OP is happy with your answer, so I have not rights to say anything more :-) Sep 12, 2016 at 21:47
• @Zarko - I added the missing lines. Sorry about that. Sep 12, 2016 at 22:36