2

I'm trying to center a tikzpicture and a caption inside the figure environment, but when I compile something is off. It seems as if the caption and the tikzpicture are centering with respect to diferent referenes. I'm using Overleaf.

\begin{figure}[H]
\centering
\begin{tikzpicture}[scale=1.0]
\begin{axis}[ ymin=0, xlabel = variáveis aleatórias, ylabel = frequência]

\addplot [
    domain=0:10, 
    samples=100, 
    color=red]
    {exp(-x)};
\end{axis}
\end{tikzpicture}
\caption{Some figure}
\end{figure}
  • Welcome to TeX.SE! Please replace e^-x by exp(-x). – user121799 Sep 29 '18 at 15:43
  • 1
    The whole figure is centered with the caption, including the axis, the legend and so on. This is why the caption is not centered with the x legend. – Sigur Sep 29 '18 at 15:47
3

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.

\documentclass{article}
\usepackage{float}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{figure}[H]
\centering
\begin{tikzpicture}[scale=1.0]
\begin{pgfinterruptboundingbox}
\begin{axis}[ ymin=0, xlabel = variáveis aleatórias, ylabel = frequência]

\addplot [
    domain=0:10, 
    samples=100, 
    color=red]
    {exp(-x)};
\end{axis}
\end{pgfinterruptboundingbox}
\path[use as bounding box] ([yshift=-8mm]current axis.south west) rectangle (current axis.north east);
\end{tikzpicture}
\caption{Some figure}
\end{figure}
\lipsum[2]
\end{document}

enter image description here

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.

\documentclass{article}
\usepackage{float}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{calc}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{figure}[H]
\centering
\begin{tikzpicture}[scale=1.0]
\begin{axis}[ ymin=0, xlabel = variáveis aleatórias, ylabel = frequência,ylabel style={alias=ylab}]

\addplot [
    domain=0:10, 
    samples=100, 
    color=red]
    {exp(-x)};
\end{axis}
\path let \p1=($(current axis.west)-(ylab.north)$) in (current axis.east) -- ++(\x1,0);
axis.north east);
\end{tikzpicture}
\caption{Some figure}
\end{figure}
\lipsum[2]
\end{document}

enter image description here

This saves you from having to add an yshift by hand.

  • Perfect solution. I didn't know about this bb env. – Sigur Sep 29 '18 at 15:55
  • @Sigur I added a second, less harmful solution. Even though I do not expect that interrupting the bounding box is worrisome in this case, this is something which should generally be avoided, if possible. – user121799 Sep 29 '18 at 16:03
  • I liked both. Nice! – Sigur Sep 29 '18 at 16:09
0

Here is a variant of Marmot's solution using the calc tikzlibrary and the [name=...] option for axis.

\documentclass{article}
\usepackage{float}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.16}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{figure}[H]
\centering
\begin{tikzpicture}[scale=1.0]
\begin{axis}[ymin=0, xlabel = variáveis aleatórias, ylabel = frequência,
    name=border]
\addplot [
    domain=0:10, 
    samples=100, 
    color=red]
    {exp(-x)};
\end{axis}
% center tikzpicture relative to axis (border)
\path ($(border.center)!-1!(current bounding box.west)$);
\draw[blue] (current bounding box.south west) rectangle (current bounding box.north east);% demo only
\end{tikzpicture}
\caption{Some figure}
\end{figure}
\lipsum[2]
\end{document}

This solution places the caption inside the tikzpicture, within the axis border (horizontally). This makes the tikzpicture smaller but provides much less space for the caption.

\documentclass{article}
\usepackage{float}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{lipsum}

\newlength{\tempwidth}

\begin{document}
\lipsum[1]
\begin{figure}[H]
\centering
\begin{tikzpicture}[scale=1.0]
\begin{axis}[ymin=0, xlabel = variáveis aleatórias, ylabel = frequência,
    name=border]
\addplot [
    domain=0:10, 
    samples=100, 
    color=red]
    {exp(-x)};
\end{axis}
% center tikzpicture relative to axis (border)
\pgfextractx{\tempwidth}{\pgfpointdiff{\pgfpointanchor{border}{west}}{\pgfpointanchor{border}{east}}}%
\node[below] at (border.south |- current bounding box.south) {\begin{minipage}{\tempwidth}
    \caption{Some figure}
\end{minipage}};
\draw[blue] (current bounding box.south west) rectangle (current bounding box.north east);% demo only
\end{tikzpicture}

\end{figure}
\lipsum[2]
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.