2

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:

enter image description here

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}{}{%
       \addplot ... ;
    }{
       \addplot ... node {<node text>};
    }

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>}}%
    }

    \addplot ... \NodeCode;

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.

References:

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]
        \addplot [My Plot Style] ({x},{x*x});
    \end{axis}

    \DrawBoundingBox;

\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}[My Axis Style]
        \addplot [My Plot Style] ({x},{x*x});
        \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]
        \addplot [My Plot Style] ({x},{x*x})
            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
4

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]
        \addplot [My Plot Style] ({x},{x*x})
            node [above=5pt, overlay] {\GraphLabel} ;
    \end{axis}

    \DrawBoundingBox;

\end{tikzpicture}
\end{document}

enter image description here

  • Thanks, works great. It did not occur to me to try overlay on a node. – Peter Grill May 1 at 22:47
  • @PeterGrill I think I learned this from percusse, if I remember correctly. – user121799 May 1 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.