[Tex/LaTex] tikzpicture padding

bounding boxfboxtikz-pgf

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.
Complete picture
mini gaps

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.

minimal gap all round caused by ...?

\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.

lines around standard and outer border anchors

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.

Related Question