[Tex/LaTex] Make parts of a pgfplots outside of the bounding box visible

bounding boxgraphicspgfplots

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,
  ]
\addplot coordinates {
(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}

enter image description here

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

Best Answer

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,
  ]
\addplot coordinates {
(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}

Result

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.