# Change white-space border in pgfplot, or crop PDF figure?

I am toying a bit with the pgfplot way of presenting data. It makes pretty nice plots, but unfortunately it is not very economical with my space. This is a problem when using a multi-column layout. Does anyone know how to decrease the extra white space margin around the plot, and possibly move the axis labels closer to the axes? As of now, I get much more "plot for my money" by doing it in matlab and just exporting to PDF.

If someone knows of a handy way of cropping PDF-images when inserting in LaTeX, that might also work, as I could export the pgf-plot to a PDF-file and then insert a cropped version of that.

-
Welcome to TeX.sx! If you add a minimal working example (MWE) that illustrates your spacing problem, people could maybe see where the problem is introduced. Normally there should be no large margin. –  Martin Scharrer Jun 28 '11 at 11:35

As Martin correctly said, usually there shouldn't be any unnecessary whitespace around the graph. Here's an example of a graph in a two column layout, with the background rectangle of the enclosing tikzpicture drawn using the backgrounds library:

\documentclass[twocolumn]{article}
\usepackage{lipsum}
\usepackage{float}
\usepackage{pgfplots}
\usetikzlibrary{backgrounds}
\begin{document}
\lipsum[1]
\begin{figure}[H]
\begin{tikzpicture}[show background rectangle,tight background]

\begin{axis}[
width=\columnwidth,
xlabel=Independent Variable,
ylabel=Dependent Variable]
\end{axis}

\end{tikzpicture}
\caption{Standard options}
\end{figure}

\lipsum[2-6]
\end{document}


To move the axis labels closer to the axes, you can use the options xlabel style={yshift=<length>}} and ylabel style={yshift=<length>}}. Note that you need to use yshift in both cases, even though the y axis label will be shifted horizontally. This is necessary because the shift will be applied before the label is rotated.

...
\begin{axis}[
width=\columnwidth,
xlabel style={yshift=0.75em},
ylabel style={yshift=-0.5em},
xlabel=Independent Variable,
ylabel=Dependent Variable]
\end{axis}
...


In some cases, it might be desirable to change the apparent width of the plot, to allow the tick labels and axis label to exceed the image area. This can be achieved using trim axis left as an option to the tikzpicture that contains the axis environment. To still get the maximum plot width, you can use the option scale only axis, which means that size options only take into account the plot area, but ignore labels. This will only make sense in the left column.

...
\begin{tikzpicture}[show background rectangle,tight background,
trim axis left]

\begin{axis}[
scale only axis,
width=\columnwidth,
xlabel=Independent Variable,
ylabel=Dependent Variable]
\end{axis}
...


-

AFAIK normally there should be no extra large margin around pgfplots (I don't use it myself this often). Anyway, you could trim any space around it manually by placing it between \begin{adjustbox}[trim=<left> <bottom> <right> <top>] ... \end{adjustbox} of the adjustbox package. This only trims the official size of the plot, if you also add the clip option any overhang is also visibly removed.

PDF images can be cropped using the pdfcrop tool which got created extra for use with LaTeX.

For the sake of completeness I also mentioned the standalone class which can be used to generate tightly cropped PDF (or DVI/PS) images from LaTeX code (using the great preview package). Turning complex pgfplots or tikzpictures into PDF can decrease the compile time a lot. See also the related external TikZ library. The next version of standalone will include some auto-compile and use-TeX-or-PDF feature.

-
@Jake: Thanks. Fixed. –  Martin Scharrer Jun 28 '11 at 13:08

You can crop your PDF images right in your LaTeX file as in the following example.

\begin{figure}[tb]
\centering
\includegraphics[width=80mm,trim=5 4 2 5 mm, clip=true]{image.pdf} %Left, bottom, right, top
\caption{Cropped image}
\label{crop}
\end{figure}

-
Welcome to TeX.sx! –  Peter Jansson May 3 '13 at 8:34