I don't get where the mysterious white space around this mwe-tikzpicture
comes from:
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\newcommand{\img}{
\begin{tikzpicture}[outer sep=0.3333em, inner sep=0]
\begin{axis}[
x = 1cm,
y = 2cm,
axis lines=middle,
axis line style={-Stealth,thick},
xmin=-.625,xmax=6,ymin=-.3125,ymax=1.25,
xtick={-1,0,1,2,3,4,5},
ytick={0,0.5,1},
extra x ticks={5.5},
extra y ticks={-0.25},
extra x tick style={xticklabel=\empty},
extra y tick style={yticklabel=\empty},
xtick distance=1,
ytick distance=1,
xlabel=$t$,
ylabel=$f(t)$,
minor tick num= 1,
grid style={thin,densely dotted,black!20}]
\end{axis}
\end{tikzpicture}
}
\pagestyle{empty}
\begin{document}
\begin{center}
\begin{figure}
\setlength{\fboxsep}{0pt}
\fbox{\img}
\end{figure}
\end{center}
\end{document}
Not only is there massive white space on the left and right, but also small white space above and below. In some cases the big space vanishes, but mini gaps between fbox
and tikzpicture
always remain.
I need the exact bounding box for an application and therefore this result is somewhat problematic. Of course, if it where for some complicated Bezier-graph, I would manually draw the bounding box, but in this case, this shouldn't be necessary.
Best Answer
Most of the horizontal spacing comes from spaces added by
\img
in the form of uncommented new lines. There is a remaining gap top, bottom, left and right of the contents of the\fbox
.The remaining gap comes, I suspect from the fact that the axis environment is a node. If we change
\img
as followsthen we can see that it is the 'outer border anchors' which lie on the boundary of the box.
As this shows
\fbox
is drawing around the image bounding box, but that box is not the same as the box given by thenorth west
andsouth east
anchors of theaxis
node. It is, rather, one given by theouter north east
andouter south west
border anchors.