2

Context

I have the following image being produced with the following code:

Image

entire tikzpicture figure centered

Code

\documentclass{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}

\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{spy}

\usepackage{siunitx}

\usepackage{float}
\usepackage{adjustbox}  % This is to scale the entire figure so it doesn't mess up the spy

\begin{document}
    \begin{enumerate}
        
        \item I'm talking about a figure. It's a great figure. Glorious, I might add\dots Here's the figure:
        \begin{figure}[H]
            \centering
            \begin{adjustbox}{width=0.65\textwidth}
                \begin{tikzpicture}[spy using outlines={circle, magnification=3, connect spies}]
                    \begin{axis}
                    [
                        xlabel={Temperature ($\unit{\celsius}$)},
                        ylabel={Consumption (litres per hour)},
                        grid,
                        xmin=0,
                        xmax=110,
                        ymin=0,
                        ymax=60,
                        axis lines=left,
                        legend style={at={(0.5,-0.3)}, anchor=north},
                        tick align=center
                    ]
                    
                    \addplot[black, dotted, ultra thick, domain=15:105, samples=100] {(770 / x)-1.325};
                    \addlegendentry{$f(x)=\frac{770}{x}-1.325$}
                    
                    \addplot[orange, dotted, ultra thick, domain=15:105, samples=100] {71.1 * exp((-0.0235)*x)};
                    \addlegendentry{$g(x)=71.1e^{-0.0235x}$}
                    
                    \coordinate (spypoint) at (axis cs:102.5,6);
                    \coordinate (magnifier) at (axis cs:150,30);
                    
                    \end{axis}
                    
                    \spy[gray, size=3cm] on (spypoint) in node[] at (magnifier);
                    
                \end{tikzpicture}
            \end{adjustbox}
        \end{figure}
        
        \hrule  % This is just to give perspective on how the figure is centered.
        
        See? I said it was glorious, didn't I?
        
    \end{enumerate}
\end{document}

Problem

What I really want is only for the graph (the axes) to be centered, but the spy to be uncentered (relative to the page).

0

1 Answer 1

3

Great figure!

If you add overlay to a TikZ scope, that scope bounding box (the physical space it occupies) is not considered when computing the graphic's size.

So writing

 \spy[gray, size=3cm,overlay] on (spypoint) in node[] at (magnifier);

Will give you

enter image description here

Notice that the "spilling out" of the graph from the border goes unnoticed because you asked to ignore it... so you have to take care not to have it going outside the page area.

1
  • Thanks for this! It works perfectly (I did have to adjust the adjustbox width parameter to 0.5\textwidth to make the figure display correctly). I've marked this as the answer. Jun 1 at 8:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .