10

How can I use math operators within PGFPlots coordinates?

When I write something like \draw (axis cs: 0,0) -- (axis cs: sqrt(3),1), I get the following error:

¡ Argument of \pgfplots@evalute@tikz@coord@system@interface@ has an extra }.

However, \draw (0,0) -> ({sqrt(3)},1) works fine in Tikz, as long as sqrt(3) is written inside of braces {}.

If I add the braces to the PGFPlots code, I get the follwing error:

¡ Package PGF Math Error: Could not parse input '-{sqrt(3)}/2' as a floating po int number, sorry. The unreadable part was near 'sqrt(3)/2'..

MWE:

\documentclass{memoir}

\usepackage{pgfplots}
\pgfplotsset{
    compat=1.8,
    standard/.style={
        axis x line=center,
        axis y line=center,
        axis z line=center,
        xlabel={$x$},
        ylabel={$y$},
        zlabel={$z$},
        axis line style=help lines,
        every axis/.append style={font=\tiny},
    }
}

\begin{document}

{\centering
\begin{tikzpicture}[scale=1]

    \begin{axis}[
        standard,
        hide z axis,
        xmin=-1.25, xmax=1.25,
        ymin=-1.25, ymax=1.25,
        zmin=0, zmax=2.5,
        xtick={-1,1},
        xticklabels={$-2r$,$2r$},
        ytick=\empty,
    ]
        % Draw Circle
        \draw[blue] (axis cs: 0,0,0)
            ellipse [
            x radius=1, y radius=1];

        % Draw Square
        \draw[green]    (axis cs: -0.86602540378,0.5,0) --
                    (axis cs: -0.86602540378,-0.5,0) --
                    (axis cs: -0.86602540378,-0.5,1) --
                    (axis cs: -0.86602540378,0.5,1) --
                    (axis cs: -0.86602540378,0.5,0);
%   I can't use math operators within PGFPlots apparently?
%       \draw[green]    (axis cs: -sqrt(3)/2,1/2,0) --
%                   (axis cs: -sqrt(3)/2,-1/2,0) --
%                   (axis cs: -sqrt(3)/2,-1/2,1) --
%                   (axis cs: -sqrt(3)/2,1/2,1) --
%                   (axis cs: -sqrt(3)/2,1/2,0);
    \end{axis}

% However, I am able to do so in regular Tikz, as long as I put functions like sqrt() in braces...
    \draw[green]    (-{sqrt(3)/2},1/2,0) ->
                (-{sqrt(3)/2},-1/2,0) ->
                (-{sqrt(3)/2},-1/2,1) ->
                (-{sqrt(3)/2},1/2,1) ->
                (-{sqrt(3)/2},1/2,0);
\end{tikzpicture}
}

\end{document}
Stefan Pinnow
  • 29,535
JDG
  • 537

1 Answers1

8

It appears that you can't write arbitrary expressions in PGFPlots functions. The parser is looking for a single floating-point number, not an expression that it must evaluate in order to obtain a single floating-point number. What you can do is save the result of -sqrt(3)/2 in a macro, and then call that macro each time. You have to do the same thing with your 1/2 and -1/2 expressions, or else just replace those with 0.5 and -0.5.

Fixed code:

\documentclass{memoir}

\usepackage{pgfplots}
\pgfplotsset{
    compat=1.8,
    standard/.style={
        axis x line=center,
        axis y line=center,
        axis z line=center,
        xlabel={$x$},
        ylabel={$y$},
        zlabel={$z$},
        axis line style=help lines,
        every axis/.append style={font=\tiny},
    }
}

\begin{document}

{\centering
\begin{tikzpicture}[scale=1]

    \begin{axis}[
        standard,
        hide z axis,
        xmin=-1.25, xmax=1.25,
        ymin=-1.25, ymax=1.25,
        zmin=0, zmax=2.5,
        xtick={-1,1},
        xticklabels={$-2r$,$2r$},
        ytick=\empty,
    ]
        % Draw Circle
        \draw[blue] (axis cs: 0,0,0)
            ellipse [
            x radius=1, y radius=1];

        % Draw Square
        \draw[green]    (axis cs: -0.86602540378,0.5,0) --
                    (axis cs: -0.86602540378,-0.5,0) --
                    (axis cs: -0.86602540378,-0.5,1) --
                    (axis cs: -0.86602540378,0.5,1) --
                    (axis cs: -0.86602540378,0.5,0);
%   I can't use math operators within PGFPlots apparently?
      \pgfmathsetmacro\foo{-sqrt(3)/2}
      \draw[green]    (axis cs: \foo,0.5,0) --
                  (axis cs: \foo,-0.5,0) --
                  (axis cs:\foo,-0.5,1) --
                  (axis cs:\foo,0.5,1) --
                  (axis cs:\foo,0.5,0);
    \end{axis}

% However, I am able to do so in regular Tikz, as long as I put functions like sqrt() in braces...
    \draw[green]    (-{sqrt(3)/2},1/2,0) ->
                (-{sqrt(3)/2},-1/2,0) ->
                (-{sqrt(3)/2},-1/2,1) ->
                (-{sqrt(3)/2},1/2,1) ->
                (-{sqrt(3)/2},1/2,0);
\end{tikzpicture}
}

\end{document}