How can I work around this TikZ bug: (\x)^2 and \x^2 produce different results in TikZ plot?
The bug ticket has been rejected on 2013-07-12, with the following comment from Till Tantau:
This is not a bug. TeX does a pure textual replacement, so
{\x^2}
for\x
being-2
gets replaced by{-2^2}
, which correctly evaluates to-4
because, indeed, the power operator takes precedence over unary negation (as it should).What you are looking for is
{(\x)^2}
, which works as expected.
OK, here's a new improved patch for the bug (now tested in more cases):
\documentclass{minimal}
\usepackage{tikz}
\makeatletter
\def\tikz@scan@no@calculator#1(#2){%
\patch@tikz(#2)%
\expandafter\tikz@@scan@@no@calculator\expandafter#1\tikz@temp
}
\def\patch@tikz(#1,#2){%
{\expandafter\let\expandafter\save@tikz@plot@var\tikz@plot@var
\expandafter\def\tikz@plot@var{(\save@tikz@plot@var)}%
\xdef\tikz@temp@i{#1}%
\xdef\tikz@temp@ii{#2}%
}%
\edef\tikz@temp{({\tikz@temp@i},{\tikz@temp@ii})}%
}
\makeatother
\begin{document}
\pgfversion\par
\begin{tikzpicture}
\draw[blue,variable=\t] plot[domain=-1:1] (\t,{\t^2 + \t^3});
\draw[xshift=4cm,blue] plot[domain=-1:1] (\x,{(\x)^2 + (\x)^3});
\draw[xshift=8cm,scale=0.5,domain=-3.141:3.141,smooth,variable=\t]
plot ({\t*sin(\t r)},{\t*cos(\t r)});
\end{tikzpicture}
\end{document}
In \patch@tikz
, parentheses are put around the \x
(or the \t
, whichever the plot variable is).
Here's part of my initial answer:
The following short code reveals the problem.
\documentclass{minimal}
\usepackage{pgf}
\begin{document}
\pgfversion\par
\pgfmathparse{-1^2}\pgfmathresult
\end{document}
In v 2.00
the result is 1.0
(which yields the correct graph!), in v 2.10
it is -1.0
(which is mathematically correct, but apparently incompatible with other pgf/TikZ components).
Indeed, in the pgf-users list I linked to in a comment to the question, I find
The problem arose in an earlier version because the precedence of the prefix negation was incorrect.
So, the mathematical bug -1.0^2 = 1.0
was corrected, but the correction (naturally) lead to another bug.
Beware! Some things written in the posts at the above link are misleading. It says, e.g.,
The CVS version gives -0.008 for both -0.2^3 and (-0.2)^3
Well, of course it does. The problem only arises for even powers.
(Yet another) Edit:
The mathematical bug in PGF 2.00 also has consequences for drawing graphs: If you want -\x^2
, then that's not so easy.
\draw plot[domain=-1:1] (\x,-\x^2);
and
\draw plot[domain=-1:1] (\x,{-(\x)^2});
both yield the graph of y=x^2
(for the second version this is still a miracle to me), and
\draw plot[domain=-1:1] (\x,{-\x^2});
throws an error (which I also don't understand). The workaround is to use
\draw plot[domain=-1:1] (\x,0-\x^2);
(In PGF 2.10 all 4 versions compile, but only the second version yields the correct result; with my patch all 4 versions are OK.)
The proposed patch in solution 1 did not work for me. It seems the problem is fixed in the CVS. The alternative patch https://tex.stackexchange.com/a/31791/14564 worked for me, I would suggest it as an alternative to the patch in solution 1.