# Pgfplots Interrupt Bounding Box by applying a style to a node

I would like to be able to use

\addplot ... node {<node text>};


and have the node not affect the bounding box. That is, the bounding box should be what it would be with just:

\addplot ... ;


The ideal solution (see code for third graph) would be to have a style Interrupt Bounding Box and use it as

\addplot ... node [Interrupt Bounding Box] {<node text>};


A failed solution (see code for second graph) was of the form:

 \addplot ... ;
\begin{pgfinterruptboundingbox}
\addplot [draw=none] ... node {<node text>};
\end{pgfinterruptboundingbox}


but I really would prefer to not have to do the \addplot twice if possible.

The MWE below produces the following:

The first graph is the desired bounding box size.

## Use Case:

I have a macro that does all the graphing of functions which accepts a graph label (that is to be placed at the end of a graph). If the given graph label is empty I would like that to not affect the bounding box. I could of course do this with something like

    \IfStrEq{\GraphLabel}{}{%
}{
}


which I might resort to, but don't like that I have to duplicate the \addplot twice.

Another failed case (not included in the MWE) was of the form

    \IfStrEq{\GraphLabel}{}{%
\def\NodeCode{}%
}{
\def\NodeCode{node {<node text>}}%
}



This works for the case there is only invocation my macro that does the graphing. I suspect the problem with this approach is related to the nodes being drawn at the end of the axis environment.

## Code:

\documentclass{article}
\usepackage[paperwidth=30cm]{geometry}% so graphs all fit side-bys-side
\usepackage{pgfplots}

\newcommand{\DrawBoundingBox}{%
\draw [draw=blue, thin]
(current bounding box.south west)
rectangle
(current bounding box.north east)
}%

\newcommand*{\GraphLabel}{$y = x^2$}

\pgfplotsset{My Axis Style/.style={
axis lines=middle,
clip=false,
}}
\pgfplotsset{My Plot Style/.style={
mark=none,
domain=-2:2,
draw=red, thick,
x=1cm, y=1cm,
}}

\tikzset{Interrupt Bounding Box/.style={
%% ???? This would be ideal solution, but not sure how to do this.
}}

\begin{document}
\noindent
\begin{tikzpicture}
\begin{axis}[My Axis Style]
\end{axis}

\DrawBoundingBox;

\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[My Axis Style]
\begin{pgfinterruptboundingbox}
\addplot [My Plot Style, draw=none] ({x},{x*x})% <---  Style Modified
node [above=5pt] {\GraphLabel};
\end{pgfinterruptboundingbox}
\end{axis}

\useasboundingbox
(current axis.below south west)
rectangle
(current axis.above north east);

\DrawBoundingBox;

\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[My Axis Style]
node [above=5pt, Interrupt Bounding Box] {\GraphLabel} ;
\end{axis}

\DrawBoundingBox;

\end{tikzpicture}
\end{document}

• Strictly limited to your use case: With \addplot [My Plot Style] ({x},{x*x}) \ifx\@empty\GraphLabel\else node [above=5pt] {\GraphLabel} \fi; you would not have to type \addplot twice. – schtandard May 1 at 21:22
• node {\smash{\raisebox{5pt}{\GraphLabel}}} is not a pgfplots solution, but it works for your usecase. – schtandard May 1 at 21:30
• \tikzset{Interrupt Bounding Box/.style={overlay}}? What I want to say is that overlay issues a \pgfinterruptboundingbox AFAIK. – user121799 May 1 at 21:32
• @schtandard: Yeah, \smash works, but does require one to account for the specific style applied to the node. – Peter Grill May 1 at 22:36

If I understand your aim correctly, you are looking for overlay.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16} % <- not important HERE and NOW but good to add IMHO
\newcommand{\DrawBoundingBox}{%
\draw [draw=blue, thin]
(current bounding box.south west)
rectangle
(current bounding box.north east)
}%

\newcommand*{\GraphLabel}{$y = x^2$}

\pgfplotsset{My Axis Style/.style={
axis lines=middle,
clip=false,
}}
\pgfplotsset{My Plot Style/.style={
mark=none,
domain=-2:2,
draw=red, thick,
x=1cm, y=1cm,
}}

\begin{document}
\noindent
\begin{tikzpicture}
\begin{axis}[My Axis Style]

• Thanks, works great. It did not occur to me to try overlay on a node. – Peter Grill May 1 at 22:47