3

I want to do some plots using TikZ, however the following code doesn't work:

\documentclass{minimal}
\usepackage{amsmath}

\usepackage{pgf,tikz}%
\begin{document}
\begin{tikzpicture}
      \draw[->] (-3,0) -- (2.2,0) node[right] {$x$}; %
      \draw[->] (0,-0.5) -- (0,3.2) node[above] {$y$};%
      \draw plot (\x,{\x^2});%
\end{tikzpicture}
\end{document}

Any idea what's wrong?

How to plot functions like x \mapsto a^x (a > 0)?

Edit: Thanks for the patch, however ploting 2^x doesn't work, I get the following picture using Jakes answer:

enter image description here

4
  • 2
    Could you please change the image to not include a large white space on top? Also please describe your issue more precise. "Doesn't work" is not very descriptive. May 19, 2011 at 19:11
  • possible duplicate of How can I work around this TikZ bug?
    – Jake
    May 19, 2011 at 19:19
  • Could you add the code you used to generate the image?
    – Jake
    May 19, 2011 at 19:47
  • @Jake: I copied exactly your code from below.
    – student
    May 19, 2011 at 20:23

1 Answer 1

5

As Martin Scharrer pointed out, this is a bug that Hendrik Vogt has provided a patch for in his answer to the question How can I work around this TikZ bug: (\x)^2 and \x^2 produce different results in TikZ plot?

On a side note: tikz loads pgf automatically, so you don't need to load it explicitly.

\documentclass{article}

\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}
\begin{tikzpicture}
      \draw[->] (-2.2,0) -- (2.2,0) node[right] {$x$}; %
      \draw[->] (0,-0.5) -- (0,3.2) node[above] {$y$};%
      \draw [domain=-2:2,orange] plot (\x,\x^2);
      \draw [domain=-2:2,blue] plot (\x,2^\x);%
\end{tikzpicture}
\end{document}

tikz plot

9
  • 1
    Is this that PGF bug for v2.1 (or was it v2.x)? May 19, 2011 at 19:10
  • @Martin: Indeed, you're right. Thanks for pointing that out. So this should be closed as a duplicate?
    – Jake
    May 19, 2011 at 19:18
  • Thanks, however plotting 2^\x seems not to work
    – student
    May 19, 2011 at 19:29
  • @user4011: It does for me, see the edited answer. Maybe you'se using an old version of TikZ?
    – Jake
    May 19, 2011 at 19:37
  • @Jake: How to find out the tikz version? See my edit above
    – student
    May 19, 2011 at 19:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .