0

I have the following code that I am using in pgfplots in a standalone tikz document:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{positioning, arrows.meta}

\pgfplotsset{every axis/.append style={
                     tick label style={font=\footnotesize},
                 }}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[grid=both,
                 xmin=-1.5, xmax=9.997,
                 ymin=-1.9975, ymax=5.9975,
                 axis lines=middle,
                 xlabel=\(x\), ylabel=\(y\),
                 xticklabels={}, yticklabels={},
                 clip=false,
                 y axis line style={Stealth-Stealth},
                 x axis line style={Stealth-Stealth}
            ]

        % graph curve
        \addplot[stealth-stealth, domain=-2.15:4.15, samples=50] ({x^2-2*x}, {x+1});

        % points on curve
        \node [circle, fill=black, scale=0.4, label=below left:{\(t=0\)}] at (0, 1) {};
        \node [circle, fill=black, scale=0.4, label=below left:{\(t=-2\)}] at (8, -1) {};
        \node [circle, fill=black, scale=0.4, label=above left:{\(t=2\)}] at (0, 3) {};
        \node [circle, fill=black, scale=0.4, label=above left:{\(t=4\)}] at (8, 5) {};

        % orientation of curve, code from https://tex.stackexchange.com/a/142650/296881
        \def\NORM{sqrt((2*t-2)^2 + 1^2)}
        \addplot[thick,-latex, , samples=6, domain=-1.5:3.5, variable=\t,quiver={
            u=(2*t-2)/\NORM, v=1/\NORM,
                scale arrows=0.1,
        }]
        ({t^2-2*t}, {t+1});
    \end{axis}
\end{tikzpicture}

\end{document}

This is the entire document. I expected it to look like this: enter image description here When I run the code within the tikzpicture environment with \documentclass{article}, that is what I get, the correct result. However, when I run the above code with \documentclass[tikz]{standalone}, I get that all of the nodes are clumped together at the bottom left like this: enter image description here

How do I fix this?

3
  • 2
    Did you try adding \pgfplotsset{compat=newest} (you should get a warning that suggests doing this actually)? The reason is that only in newer versions, the coordinates inside an axis environment are also valid for \draw or other "normal" TikZ commands, but for older versions they are not and you would need to type (axis cs:1,1) instead for just (1,1). Commented May 24, 2023 at 12:59
  • BTW, the MWE also messes up for \documentclass{article}. Your article code must be slightly different. Commented May 24, 2023 at 19:44
  • Yeah, you are right. I had \pgfplotsset{compat=newest} in the \documentclass{article} but not the \documentclass[tikz]{standalone}
    – Rango
    Commented May 25, 2023 at 0:39

1 Answer 1

1

If you use PGFPlots, you are advised to always set the compat option, because this will affect the way how certain parts of the syntax are interpreted. For older versions of PGFPlots, you would have to type (axis cs:1,1) instead of (1,1) if you want to place some coordinate of a TikZ path at the relevant position inside the axis environment. For newer versions, this is not needed anymore, but to actually make use of the new syntax, you need to set the compat option to a newer version. Check the PGFPlots manual for more information about this.

Hence, you either need to change the coordinates for the TikZ commands in your code to meed the requirements of the older syntax (with axis cs), or you need to set the value for the compat option to a relatively new version (or just use newest). I would recommend setting the compat option.

So, a working solution would look like this:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}

\pgfplotsset{
    compat=newest, % or at least 1.11
    every axis/.append style={
        tick label style={font=\footnotesize},
    }
}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        grid=both,
         xmin=-1.5, xmax=9.997,
         ymin=-1.9975, ymax=5.9975,
         axis lines=middle,
         xlabel=\(x\), ylabel=\(y\),
         xticklabels={}, yticklabels={},
         clip=false,
         y axis line style={Stealth-Stealth},
         x axis line style={Stealth-Stealth}
    ]

        % graph curve
        \addplot[stealth-stealth, domain=-2.15:4.15, samples=50] ({x^2-2*x}, {x+1});

        % points on curve
        \node[circle, fill=black, scale=0.4, label=below left:{\(t=0\)}] at (0, 1) {};
        \node[circle, fill=black, scale=0.4, label=below left:{\(t=-2\)}] at (8, -1) {};
        \node[circle, fill=black, scale=0.4, label=above left:{\(t=2\)}] at (0, 3) {};
        \node[circle, fill=black, scale=0.4, label=above left:{\(t=4\)}] at (8, 5) {};

        % orientation of curve, code from https://tex.stackexchange.com/a/142650/296881
        \def\NORM{sqrt((2*t-2)^2 + 1^2)}
        \addplot[thick,-latex, samples=6, domain=-1.5:3.5, variable=\t, quiver={
            u=(2*t-2)/\NORM, v=1/\NORM,
            scale arrows=0.1,
        }]
        ({t^2-2*t}, {t+1});
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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