32

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:

minimal example

while in 2.10 I have this:

minimal example

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: alt text

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.

cmhughes
  • 100,947
Matthew Leingang
  • 44,937
  • 14
  • 131
  • 195
  • 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
  • 1
    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. – Andrew Stacey Nov 15 '10 at 18:50
  • 4
    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
  • @Hendrik: that makes sense. – Matthew Leingang Nov 17 '10 at 14:38
  • According to Henrik's link, this is fixed in the CVS (scroll down to the third message). So I guess that the "answer" to your "question" is: Yes, it did. To fix it, update to the very latest version (I've heard it does sliced bread as well). – Andrew Stacey Nov 17 '10 at 15:22
  • If I weren't a moderator, I would now vote to close this. As I said above, I'm not sure that tex.SX is a great place for bug reports and I'm also not sure of its value as a noticeboard. (To be clear, I'm not particularly bothered about this question, it's the precedent that I'm thinking about. Also, it wouldn't bother me if we got the odd bug report, I just don't think that posting a bug report here is the best thing for that person to do to get a solution to their problem. An acceptable alternative would be: "There seems to be a bug, is there an easy fix?".) – Andrew Stacey Nov 17 '10 at 15:25
  • Fair points, all. I can rephrase my bug report in the form of a question. – Matthew Leingang Nov 17 '10 at 15:37
  • @Matthew: much better! – Andrew Stacey Nov 18 '10 at 10:28
  • Also to clarify one thing. It was human error that kept me off the pgf-users list: my error. I'm back on the list. – Matthew Leingang Nov 18 '10 at 18:58

3 Answers3

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.

Jake
  • 232,450
  • 4
    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 01 '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 07 '14 at 00:50
15

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

Hendrik Vogt
  • 37,935
4

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.

Sergio Parreiras
  • 1,786
  • 1
  • 15
  • 32