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.

• \newcommand{\img}{ adds a space - the new line is uncommented. \end{tikzpicture} } adds another - the new line is uncommented. Combining center and figure will add too much vertical space. Use \begin{figure}\centering ... \end{figure} instead. – cfr Nov 10 at 23:05
• Not sure where the tiny gap above the arrow and $f(t)$ comes from.I checked \rule{1pt}{\ht\strutbox}, nor does the [outer sep] have any effect. I suspect you wanted [every node/.style={outer sep=0.333pt), inner sep=0pt}]. – John Kormylo Nov 10 at 23:15
• You guys are right, the big space results (of course) from the missing %. Nope, I didn't want [every node/.style={outer sep=0.333pt), inner sep=0pt}] because in this case the t and f(t) are misplaced. – Ultimate Trick Nov 10 at 23:24

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.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}

\newcommand{\img}{%
\begin{tikzpicture}[outer sep=0, inner sep=0]
\begin{axis}[
outer sep=.3333em,
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{figure}\centering
\setlength{\fboxsep}{0pt}
\fbox{\img}
\end{figure}
\end{document}


The remaining gap comes, I suspect from the fact that the axis environment is a node. If we change \img as follows

\newcommand{\img}{%
\begin{tikzpicture}[outer sep=0pt, inner sep=0pt]
\begin{axis}[
outer sep=.3333em,
name=mab,
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}
\draw [red] (mab.north west) |- (mab.south east) |- cycle;
\draw [blue] (mab.outer north west) |- (mab.outer south east) |- cycle;
\end{tikzpicture}%
}


then 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 the north west and south east anchors of the axis node. It is, rather, one given by the outer north east and outer south west border anchors.

• Thanks so far, but in fact I had this big space issue only this one time (as I was to dumb to find the missing %). More relevant to me is this small-gap-issue. – Ultimate Trick Nov 10 at 23:25
• @UltimateTrick There is no small gap issue. To see this, add line cap=rect to the options of the tikzpicture. TikZ dials a bounding box that includes most generous choices of the line caps and joins, everything else would be next-to-impossible to code. – Schrödinger's cat Nov 10 at 23:40
• @UltimateTrick See explanation above. I'm not sure how mine works with Schrödinger's cat's. – cfr Nov 10 at 23:48
• If I add line cap=rect to the options of the tikzpicture, i.e. \begin{tikzpicture}[outer sep=0pt, inner sep=0pt,line cap=rect], the small gaps at the lower left corner are gone, as expected. – Schrödinger's cat Nov 10 at 23:50
• I meant \documentclass{article} \usepackage{tikz} \begin{document} \setlength{\fboxsep}{0pt} \fbox{\begin{tikzpicture} \draw (0,0) -- (1,1); \end{tikzpicture}} \fbox{\begin{tikzpicture}[line cap=rect] \draw (0,0) -- (1,1); \end{tikzpicture}} \end{document}. (Need to run now...) – Schrödinger's cat Nov 10 at 23:54