Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

1 Answer 1

up vote 5 down vote accepted

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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