tl;dr: How can I make standalone crop a pgfplot diagram with fixed distance to the plot axes?
Background:
I have four pgfplot diagrams rendered as PDF images by the standalone class. Due to the different ztick labels, the vertical edges of the boxes, e.g. the z-axis, are not aligned well right now (albeit it's not way off) when I put them in subfigures:
Since my actual data is large, it is not feasible to put the four tikzpictures in my main document. There are two elaborate answers: One based on the use of the external library and one using some fancy trimming.
However, I want to keep it simple (the code of my main document as well as no manual copy-pasting of trim parameters).
Can I just have standalone crop the plots with a fixed distance from the plot axes?
In 2D this should not be impossible. In 3D, the distance needs to be defined from the top/bottom vertices of the boxes around the plots.
Here's the MWE of my main file:
% !TeX program = lualatex
\documentclass{scrbook}
\usepackage[partial=upright]{unicode-math}
\usepackage{fontspec}
\usepackage{caption,subcaption}
\usepackage{graphicx}
\usepackage[main=ngerman,english]{babel}
\begin{document}
\begin{figure}
%
\begin{subfigure}{0.5\linewidth}
\includegraphics{3dplot_a.pdf}
\end{subfigure}
%
\begin{subfigure}{0.5\linewidth}
\includegraphics{3dplot_b.pdf}
\end{subfigure}
%
\begin{subfigure}{0.5\linewidth}
\includegraphics{3dplot_c.pdf}
\end{subfigure}
%
\begin{subfigure}{0.5\linewidth}
\includegraphics{3dplot_d.pdf}
\end{subfigure}
%
\end{figure}
\end{document}
Here is the code that generates the four sample plots (for convenience, all files are available on this GitHub repo):
% !TeX program = lualatex
\RequirePackage{luatex85}
\documentclass[border=1pt]{standalone}
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage[partial=upright]{unicode-math}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage[main=ngerman,english]{babel}
\begin{document}
% plot a
\begin{tikzpicture}
\begin{axis}[
width=7cm,
grid=both,
view={60}{45},
set layers,
every axis plot/.append style={on layer=pre main},
xlabel={$x$},
ylabel={$y$},
zlabel={some quantity $z$},
grid=both,
clip=false,
]
\addplot3 [
mesh,
scatter,
samples=10,
domain=0:1,
] {5*x*sin(2*deg(x)) * y*(1-y)};
\node at (rel axis cs:0.5,1,1) [above,sloped like x axis] {$x$ explanation};
\node at (rel axis cs:0,0.5,1) [above,sloped like y axis] {$y$ explanation};
\end{axis}
\end{tikzpicture}
%% plot b
%\begin{tikzpicture}
% \begin{axis}[
% width=7cm,
% grid=both,
% view={60}{45},
% set layers,
% every axis plot/.append style={on layer=pre main},
% xlabel={$x$},
% ylabel={$y$},
% zlabel={another quantity $z$},
% grid=both,
% clip=false,
% ]
% \addplot3 [
% mesh,
% scatter,
% samples=10,
% domain=0:1,
% ] {5*x*cos (2*deg(x)) * y*(1-y) + 15};
% \node at (rel axis cs:0.5,1,1) [above,sloped like x axis] {$x$ explanation};
% \node at (rel axis cs:0,0.5,1) [above,sloped like y axis] {$y$ explanation};
% \end{axis}
%\end{tikzpicture}
%% plot c
%\begin{tikzpicture}[trim axis left, trim axis right]
% \begin{axis}[
% width=7cm,
% grid=both,
% view={60}{45},
% set layers,
% every axis plot/.append style={on layer=pre main},
% xlabel={$x$},
% ylabel={$y$},
% ztick={0},
% zlabel={third quantity $z$},
% grid=both,
% clip=false,
% ]
% \addplot3 [
% mesh,
% scatter,
% samples=10,
% domain=0:1,
% ] {5*sin (4*deg(x)) * y*(1-y)};
% \node at (rel axis cs:0.5,1,1) [above,sloped like x axis] {$x$ explanation};
% \node at (rel axis cs:0,0.5,1) [above,sloped like y axis] {$y$ explanation};
% \end{axis}
%\end{tikzpicture}
%% plot d
%\begin{tikzpicture}[trim axis left, trim axis right]
% \begin{axis}[
% width=7cm,
% grid=both,
% view={60}{45},
% set layers,
% every axis plot/.append style={on layer=pre main},
% xlabel={$x$},
% ylabel={$y$},
% ztick={1000},
% zlabel={fourth quantity $z$},
% grid=both,
% clip=false,
% ]
% \addplot3 [
% mesh,
% scatter,
% samples=10,
% domain=0:1,
% ] {x*y+1000};
% \node at (rel axis cs:0.5,1,1) [above,sloped like x axis] {$x$ explanation};
% \node at (rel axis cs:0,0.5,1) [above,sloped like y axis] {$y$ explanation};
% \end{axis}
%\end{tikzpicture}
\end{document}

\begin{tikzpicture}[trim axis left,trim axis right]combined with\documentclass[border={1.5cm 2pt 5mm 2pt}]{standalone}(some adjustment of lengths possibly needed) might work, but I haven't tested fully. You might also want to addoverlayto the "explanation" nodes, so they don't affect the bounding box. – Torbjørn T. Mar 24 '18 at 16:59