Using pgf
2.10, you can provide the arguments trim axis left
and trim axis right
to the tikzpicture
environment:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\centering
\begin{tikzpicture}[trim axis left, trim axis right]
\begin{axis}[ylabel={$y$},
xlabel={$x$}]
\addplot {x^2};
\end{axis}
\end{tikzpicture}
(a)
\bigskip \bigskip
\begin{tikzpicture} % Example of leaving this out
\begin{axis}[ylabel={$y$},
xlabel={$x$}]
\addplot {x^2};
\end{axis}
\end{tikzpicture}
(a)
\end{document}
gives
This works without disturbing the bounding box for the purposes of image externalisation and so forth (avoiding some of the problems discussed in @Martin's earlier question).
I don't see the need for boxes here, you can simply use:
\documentclass{article}
\usepackage{tikz}
\newcommand\drawline[1][black]{%
\begin{tikzpicture}
\draw[#1] (0pt,0pt) -- (15pt,0pt);
\end{tikzpicture}%
}
\begin{document}
\listoffigures
\begin{figure}
\caption{test \drawline[ultra thick]}
\caption{test \drawline[red,dashed]}
\end{figure}
\end{document}
Since \drawline
has an optional argument, LaTeX protecting mechanism is automatically used; had the definition been given as
\newcommand\drawline[1]{%
\begin{tikzpicture}
\draw[#1] (0pt,0pt) -- (15pt,0pt);
\end{tikzpicture}%
}
then
\caption{test \drawline{ultra thick}}
would fail on the second run. In this case one would have to use
\caption{test \protect\drawline{ultra thick}}
or, better yet, protect the command from the definition itself:
\DeclareRobustCommand\drawline[1]{%
\begin{tikzpicture}
\draw[#1] (0pt,0pt) -- (15pt,0pt);
\end{tikzpicture}%
}
(Thanks to Joseph Wright).
Best Answer
As Sigur points out, the caption is centered w.r.t. the full tikzpicture, not just the plot, but the plot with labels. If you really don't want this, you might mess around with the bounding box.
Other options include shifting the caption to the left, or, what might be more elegant, to add the same space to the bounding box on the right.
This saves you from having to add an
yshift
by hand.