# How can I work around this TikZ bug: (\x)^2 and \x^2 produce different results in TikZ plot?

I have some files with TikZ graphics that have suddenly changed appearance since I upgraded to PGF-2.10. Here is a minimal example:

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\pgfversion\par
\begin{tikzpicture}
\draw[blue] plot[domain=-1:1] (\x,{\x^2 + \x^3});
\draw[xshift=4cm,blue] plot[domain=-1:1] (\x,{(\x)^2 + (\x)^3});
\end{tikzpicture}
\end{document}


In PGF 2.00 I had this:

while in 2.10 I have this:

The one on the left in PGF 2.10 looks like the graph of y=x^3 but could also be y=x^3 + x|x|. This has been reported on sourceforge and discussed on the pgf-users mailing list. Although I haven't checked myself yet, the bug remains open and some of the commenters below report that problems still exist in the CVS version.

Geoffrey Jones notes the bug is in the 2.00 CVS version:

Hi Matthew, not an answer, just an FYI that's too large for the comment box. My compile of your code through pgf v2.00 produces the result you see with v2.10, above:

I.e., the difference you observe seems to pre-date v2.10.

There is a workaround by inserting parentheses. But since I have so many old lecture files I'd rather find a solution that doesn't involve rewriting my TikZ code. And I'm squeamish about using the CVS bleeding edge code even if it were fixed, since I don't want to introduce other problems. Ideally I'd like a simple patch that I can stick at the preamble, or just fix one file in the PGF distro.

• I's say this deserves a "bug" tag. – Hendrik Vogt Nov 15 '10 at 15:37
• I'd agree but "debugging" is the closest one. But I'm surprised if it is a bug since this example isn't too complicated. – Matthew Leingang Nov 15 '10 at 15:45
• I'm strongly tempted to say that bug reports ought to go on the correct bug mailing list. I'm not sure that there's a value in posting them here. There's no real question to answer. – Loop Space Nov 15 '10 at 18:50
• Good point, but I wanted a second opinion on the bug. Also, the pgf-users mailing list is now closed to new subscribers (without human intervention, and my request has been waiting for weeks), so it's not as easy as I thought it would be to check with the PGF community. – Matthew Leingang Nov 15 '10 at 21:01
• From this discussion in the pgf-users list I guess that the incorrect graph is y=x^3+x|x|, not y=x^3. – Hendrik Vogt Nov 17 '10 at 14:36

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.

• This is the only acceptable answer since it reports the resolution of the bug report. Although I disagree with the design choice I agree to work with or around it. Since I've opened this question I've moved on to pgfplots for graphing functions, and haven't faced any gotchas like this. – Matthew Leingang Aug 1 '13 at 19:24
• It is identical to \def\f(#1){#1^2} which will not work for \f(a*x+b) generally unless we write it as \f((a*x+b)). – kiss my armpit Feb 7 '14 at 0:50

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.)

• Is it in fact fixed in CVS? – Matthew Leingang Nov 17 '10 at 16:07
• @Matthew: I don't think so; see the last paragraph of my edited answer. – Hendrik Vogt Nov 18 '10 at 7:37
• @Hendrik: your patch looks promising. I have a more stressful test to apply it on; we'll see. – Matthew Leingang Nov 18 '10 at 14:23
• I found something similar when trying to plot exp(-\x^2). I had to juggle with the parenthesis for it not to throw an overflow error. – fabikw Nov 18 '10 at 15:37
• @Matthew: I'm just being curious: Have you already done some testing? – Hendrik Vogt Nov 20 '10 at 9:31

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.