9

tikzscale and trim axis do not play well together, with the produced figure being larger than requested because it ignores the size of the labels.

I was wondering whether it is possible to use both tikzscale to get exact sizing of the figure and the trim axis left and trim axis right parameters in the tikzpicture environment so that the figure captions are centered with respect to the axis rather than the axis+labels. It'd be great to retain the ability to do this even when the figures are externalized, if possible.

Here is what currently happens:

enter image description here

And here's a MWE:

Main file:

\documentclass{article}

\usepackage{tikz,pgfplots,tikzscale}

\begin{document}
  This is just some random text that will span over two lines. This is just some random text that will span over two lines.
  \begin{figure}[h!]
    \centering
    \begin{tikzpicture}[trim axis left,trim axis right]
    \begin{axis}[%
      xlabel={x},
      ylabel={y},
      width=\textwidth,
      height=0.25\textwidth]
    \addplot {x};
    \end{axis}
    \end{tikzpicture}%
    \caption{No tikzscale and trim axis left/right}
  \end{figure}

  This is just some random text that will span over two lines. This is just some random text that will span over two lines.
  \begin{figure}[h!]
    \centering
    \includegraphics[width=\textwidth,axisratio=4]{no-trim.tikz}
    \caption{tikzscale only}
  \end{figure}

  This is just some random text that will span over two lines. This is just some random text that will span over two lines.
  \begin{figure}[h!]
    \centering
    \includegraphics[width=\textwidth,axisratio=4]{trim.tikz}
    \caption{tikzscale and trim axis left/right}
  \end{figure}
\end{document}

no-trim.tikz

\begin{tikzpicture}
\begin{axis}[%
  xlabel={x},
  ylabel={y},]
\addplot {x};
\end{axis}
\end{tikzpicture}%

trim.tikz

\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[%
  xlabel={x},
  ylabel={y},]
\addplot {x};
\end{axis}
\end{tikzpicture}%

Edit:

Jake suggested that the question would be better phrased as how to move the captions so that they are aligned to the axis of the figure rather than the opposite. So, assuming that the typical scenario is the one in Figure 2, what would I have to change to center the caption with respect to the axis? Again, I'll be producing tens of figures like this so an automated solution is what I'm aiming for (that's why I assumed that the way to go was with trim axis left and trim axis right).

9
  • 1
    Just to clarify: You want to be able to specify an overall width for the axes, say, 10cm, including the labels, and you want the axes to be centered with regard to the axis box. If you have one axis with very large labels, the axis box should shrink so that the overall width is still 10cm. Is that correct?
    – Jake
    Jul 23, 2013 at 13:58
  • Yes. I want the width of the figure to be exact like in Figure 2, but I also want the figure caption to be centered only with respect to the axis like in Figure 3. But I'd prefer to have this done automagically and not lose the ability to externalize if possible.
    – sudosensei
    Jul 23, 2013 at 14:05
  • I'd like to try it before I contact the maintainer. I've added the line you told me to tikzscale.sty and I am seeing no difference. Is there anything else I need to do before the changes are applied?
    – sudosensei
    Jul 23, 2013 at 15:00
  • Thanks for all the help, Jake. I did that and I see something weird. The width is obviously right; the alignment of the caption is right; but the figure is not at the right place --- it's outside the \textwidth margin. This is what happens: dropbox.com/s/kc22v0abk63x7p1/almost-there.png
    – sudosensei
    Jul 23, 2013 at 15:17
  • 1
    Okay, that will require a very different approach, and is really independent of whether you use tikzscale or not. The caption is always centered with regard to the text area on the page, it doesn't care about the content of the figure float. Personally, I think it would look pretty weird if the captions weren't centered on the page, though. Maybe you can edit your question to make it clearer that you want to adjust the positions of the captions, and not of the plots?
    – Jake
    Jul 23, 2013 at 15:36

1 Answer 1

2

Step 1: steal the coordinatess of axes from the TikZ picture.

Step 2: apply an offset to the caption

\documentclass{article}
\usepackage{tikz,pgfplots,lipsum}

\makeatletter
\newdimen\pgf@xd

\begin{document}
    \lipsum[1]
    \begin{figure}[h!]\centering
        \begin{tikzpicture}
            \begin{axis}[name=my plot,xlabel={x},ylabel={y},
                         width=\textwidth,height=0.25\textwidth]
                \addplot{x};
            \end{axis}
            \tikz@parse@node(my plot.south)       \pgf@xd=\pgf@x
            \tikz@parse@node(my plot.outer south) \global\advance\pgf@xd by-\pgf@x
        \end{tikzpicture}
        \caption{Some caption. .noitpac emoS :1 erugiF\kern-2\pgf@xd}
    \end{figure}
    \lipsum[2]
\end{document}

You must log in to answer this question.

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