# Make parts of a pgfplots outside of the bounding box visible

I have created a graph with pgfplots and adjusted the bounding box to the axes. When I include the pdf of the graph in my main tex-document with includegraphics, I want to see the axes labels which are outside of the bounding box. Afaik, the axes labels are still within the pdf however are clipped.

pgfplotsclipped.tex

\documentclass[class=elsarticle,preprint,5p,twocolumn, 10pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{backgrounds}
\usetikzlibrary{pgfplots.groupplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}[inner frame sep=0]
\begin{axis}[%
clip=false,
width=\columnwidth-0.4in,
height=4in-0.5in,
scale only axis,
axis x line=bottom,
axis y discontinuity=parallel,
xmin=360, xmax=600,
ymin=0, ymax=7,
enlargelimits=false,
]
(420,2)
(500,6)
(590,4)
};
\end{axis}
\pgfresetboundingbox
\path
(current axis.south west) -- ++(-0.4in,-0.4in)
rectangle (current axis.north east) -- ++(0.0in,0.1in);
\end{tikzpicture}
\end{document}


main.tex

\documentclass[5p]{elsarticle}
\usepackage[]{graphicx}
\begin{document}
\fbox{\includegraphics[viewport=0 0 400 300,clip=false]{pgfplotsclipped.pdf}}
\end{document}


I was hoping the clip option of graphicx what allow this:

\includegraphics[clip=false]{pgfplotsclipped.pdf}

\$ texdoc grfguide.pdf

clip Either ‘true’ or ‘false’ (or no value, which is equivalent to ‘true’). Clip the graphic to the bounding box.

'Make visible node text outside of pgfplot axes range' suggests to add to the axis command in my pgfplotsclipped.tex clip=false which did not help either.

A similar question was asked regarding inkscape: Including graphic outside bounding box using graphicx and pdflatex

• Your conjecture is, unfortunately, false: the PDF produced with standalone doesn't contain the cropped parts. You increase the margin on each of the four sides, though. – egreg Jun 3 '15 at 10:52
• So the zero in 600 (x-axis) is actually cut in half? – Hotschke Jun 3 '15 at 10:53
• And second question would be: The clipping functionality in pgfplots is not of any help? – Hotschke Jun 3 '15 at 10:55
• The process has two phases: first the plot is drawn and is assigned a bounding box; then standalone uses this bounding box for cropping the page to contain exactly the bounding box. This cropping seems to be destructive of everything outside the bounding box; I also tried with preview=true,crop=false, but the result is the same. – egreg Jun 3 '15 at 11:04
• I was expecting that text is still kept intact and not being vectorized. – Hotschke Jun 3 '15 at 11:07

The job, which generates the plot can also calculate the parameters for the trim option for later inclusion via \includegraphics.

\documentclass[class=elsarticle,preprint,twocolumn, 10pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{backgrounds}
\usetikzlibrary{pgfplots.groupplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}[inner frame sep=0]
\begin{axis}[%
clip=false,
width=\columnwidth,
height=4in-0.5in,
scale only axis,
axis x line=bottom,
axis y discontinuity=parallel,
xmin=360, xmax=600,
ymin=0, ymax=7,
enlargelimits=false,
]
(420,2)
(500,6)
(590,4)
};
\end{axis}

\path[thin]
let
\p{bb south west} = (current bounding box.south west),
\p{bb north east} = (current bounding box.north east),
\p{axis south west} = (rel axis cs:0, 0),
\p{axis north east} = (rel axis cs:1, 1),
\n{lw/2} = {.5\pgflinewidth},
\n{left} = {\x{axis south west} - \x{bb south west} - \n{lw/2}},
\n{bottom} = {\y{axis south west} - \y{bb south west} - \n{lw/2}},
\n{right} = {\x{bb north east} - \x{axis north east} - \n{lw/2}},
\n{top} = {\y{bb north east} - \y{axis north east} - \n{lw/2}}
in
\pgfextra{
\typeout{ \space trim=\n{left} \n{bottom} \n{right} \n{top}}%
}
;
\end{tikzpicture}
\end{document}


The output for option trim is:

trim=11.66595pt 15.24434pt 10.633pt 6.3552pt


Then the image can be included with parts of the image sticking outside. The red frame shows the space, TeX uses for the image.

\documentclass[twocolumn,10pt]{elsarticle}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{color}
\setlength{\fboxsep}{0pt}
\setlength{\fboxsep}{.1pt}
\setlength{\parindent}{0pt}

\begin{document}

\color{red}

\fbox{%
\includegraphics[
trim=11.66595pt 0pt 10.633pt 0pt,
]{test}%
}

\textcolor{black}{\lipsum[103]}

\fbox{%
\includegraphics[
trim=11.66595pt 15.24434pt 10.633pt 6.3552pt,
width=50mm,
]{test}%
}
\end{document}


The first image sticks into the left and right margin and is scaled to the available width. The second example limits the box to the axis area with a larger scaling of the image. The parameters for trim remains the same regardless of the scaling.

• Thank you for your helpful answer. It answers exactly what I had in mind. IMHO one should almost never be interested in rescaling plots in final documents. So I do not want to use a scaling option of \includegraphics. I am not sure whether the workflow could be improved. Copying values manually can lead to inconsistencies, when only the plot is updated. But I like the separation of graph and document compilation. Do you think I should rephrase my title? – Hotschke Jun 8 '15 at 14:27
• @Hotschke You can write the values in a file, which is included by the other document. – Heiko Oberdiek Jun 8 '15 at 14:29
• How can I do this? I can not find the appropriate documentation for such tasks. – Hotschke Jun 8 '15 at 15:22
• And another question is: I'd like to have protusion only on the right side not on the left side where I have my y-label. Should I use again 'funny' values, to set the x-axis width accordingly? – Hotschke Jun 8 '15 at 15:27
• @Hotschke The "funny" values would just be 0pt, e.g. trim=0pt 0pt 10.633pt 0pt, then the protrusion is on the right side only. – Heiko Oberdiek Jun 8 '15 at 15:55

From what I understand, your purpose is to align the frame of your axis with the column boundaries, right?

I am unsure whether you are bound to standalone for the separate images. If so, my answer is inadequate. In fact, I am unsure whether you need separate pdfs at all.... ?

My idea is to use \usetikzlibrary{external} in order to achieve the image externalization (assuming that you need them) and trim axis left, trim axis right in order to truncate the bounding box.

It results in the following output:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{lipsum}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{backgrounds}
\usetikzlibrary{external}

\tikzexternalize

\usetikzlibrary{pgfplots.groupplots}
\pgfplotsset{compat=1.12}
\begin{document}
% Fix overful hboxes automatically:
\tolerance=2000
\emergencystretch=10pt

\twocolumn

\begin{figure}
\centering
\begin{tikzpicture}[trim axis left, trim axis right,inner frame sep=0]
\begin{axis}[%
width=\columnwidth-15pt,
height=4in-0.5in,
scale only axis,
axis x line=bottom,
axis y discontinuity=parallel,
xmin=360, xmax=600,
ymin=0, ymax=7,
enlargelimits=false,
]
(420,2)
(500,6)
(590,4)
};
\end{axis}
\end{tikzpicture}

\end{figure}

\lipsum[1-2]

\begin{figure}
\centering
\begin{tikzpicture}[trim axis left, trim axis right,inner frame sep=0]
\begin{axis}[%
clip=false,
width=\columnwidth-15pt,
height=4in-0.5in,
scale only axis,
axis x line=bottom,
axis y discontinuity=parallel,
xmin=360, xmax=600,
ymin=0, ymax=7,
enlargelimits=false,
]
(420,2)
(500,6)
(590,4)
};
\end{axis}
\end{tikzpicture}

\end{figure}
\end{document}


The keys trim axis left, trim axis right need to be given to a tikzpicture. Their purpose is to trim the generated bounding box such that it ends at the left or right border of the axis, i.e. precisely what you need.

That approach works with and without external images because the external library has a special magic which computes the extends and reimports them automagically together with the image. The extends are available as P-figure0.dpth which contains

0.0pt
\pgfexternal@restore {\def \pgfexternaltrimleft {-11.86595pt}}
\pgfexternal@restore {\def \pgfexternaltrimright {-10.833pt}}


on my system. These values could also be used as arguments for \includegraphics, I suppose.

Note that I have actually reduced the width since my twocolumn layout has too few space between the two columns and the axis descriptions would overlap with the adjacent column(s). This can be adopted to your needs by restoring proper width arguments.

• Yes, aligning the axis with the text column boundaries is what I want. I wanted to give the plot as much space as possible and it seemed to be sensible to allow protrusion of markers at the axis into the margin. – Hotschke Jun 9 '15 at 8:52
• No, I am not bound to the package standalone. But I like to have the main document to be as simple as possible, so that journal submission or sharing with colleagues is easier. – Hotschke Jun 9 '15 at 9:18
• I do not have the experience whether it is a good idea to submit tikz/pgfplots sources to journals. Usually they only mention \includegraphics as the method to insert images/graphs etc. Possibly they do not allow protrusion into margins at all. BTW: why do you need to adjust tolerance and emergency stretch? How does this fix overful boxes automatically? – Hotschke Jun 9 '15 at 9:44
• OK. Regarding your question about the parameters: they allow TeX to relax its strict handling of overful boxes, see tex.stackexchange.com/questions/31301/… for a related question. You can also uncomment these two lines to see what happens. – Christian Feuersänger Jun 11 '15 at 15:50