5

The below code shows that the graph of function xn is incorrect when n is even (unless typed as \x*\x instead of \x^2). So I'm curious what math formulas are allowed in plot to make sure that it's correct?

Also I've got an error when trying to use brackets: \draw plot (\x,\x*(1-\x));. Package tikz Error: Giving up on this path. Did you forget a semicolon? What is the syntax for math brackets or it's not allowed at all for plot function's argument?

The manual pgfmanual v3.1.10 on page 344 gives examples with functions sin(\x) and 0.05*exp(\x). Is there a comprehensive list of math functions, which are valid for latex function plot? For example, is it possible to draw function (x+1/3)^(1/x) on interval [1,4] using plot (i. e. without using pgfplots or gnuplot)?

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[domain=-1:1]
\draw plot (\x,\x*\x);
\draw[red, dashed] plot (\x,\x^2);
\draw[blue] plot (\x,\x*\x*\x*\x);
\draw[green, dashed] plot (\x,\x^4);
\end{tikzpicture}
\end{document}

EDIT. After reading pgfmanual (p. 1034) I realized that it's safer to use pow(\x,2) instead of \x^2 (especially for some complicated functions). I mean it might be even safer option than (\x)^2.

4
  • 1
    You are asking two questions in one. Maybe you should edit your title or split it into two separate questions (I'm writing an answer nonetheless).
    – Fritz
    Feb 17 at 14:19
  • 1
    See chapter 94 Mathematical Expressions of the pgfmanual.
    – dexteritas
    Feb 17 at 14:21
  • 1
    Concerning ) in a coordinate expression: You need to protect them. (Similar how you have to protect a ] in an optional argment.) Regarding \x^4 getting into the negative, see another answer. PGFmath is just dumbly expanding \x before it can be considered a single thing. You will need to use(\x)^4. Feb 17 at 14:43
  • This is just the orders of operation in mathematics, which we remember by PEMDAS: parentheses, exponentiation, multiplication/division (left to right), addition/subtraction (left to right). - is a unary operator which multiplies the result by -1. So -3^2 means (-1)*(3)^2 and then exponentiation comes first, so (-1)*9=-9. Putting (-3)^2 is ((-1)*(3))^2 which is (-3)^2=9.
    – DJP
    Feb 17 at 22:56

1 Answer 1

6

To answer the first question: The list of functions understood by the pgfmath engine is listed in section 94.3 Syntax for Mathematical Expressions: Functions of the pgfmanual.


As for your problem with \x^2: It appears that TikZ first expands \x and then parses the ^2. For example with \x=-3 TikZ would see -3^2, which a math parser typically understands as -(3^2). Other programming languages typically square the value of x directly, without first expanding it into -3, so that is where the confusion comes from.

Edit: There is a related bug report showing similar behavior for division.

You can fix this by putting parentheses around \x, but then you need braces around the Y part of your coordinate: \draw[red, dashed] plot (\x,{(\x)^2});

Picture showing 4 curves

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[domain=-1:1]
\draw plot (\x,\x*\x);
\draw[red, dashed] plot (\x,{(\x)^2});
\draw[blue] plot (\x,\x*\x*\x*\x);
\draw[green, dashed] plot (\x,{(\x)^4});
\end{tikzpicture}
\end{document}
5
  • PGFMath's first step is to expand everything. It will never consider any macros to be single thing. Google also thinks -3^4 = -(3^4). So does WolframAlpha. Feb 17 at 14:45
  • @Fritz Thank you very much! This is exactly what I needed to know! Finally, this \draw plot (\x,{\x*(1-\x)}); is working!
    – Robai
    Feb 17 at 14:46
  • @Qrrbrbirlbel: Thanks, I stand corrected and have edited my answer accordingly. Python also calculates -3**2 as -9.
    – Fritz
    Feb 17 at 15:24
  • 1
    @Fritz It's a tricky thing (and a common one to trip over). \x looks like a variable but it isn't. 😔 Feb 17 at 15:50
  • @Qrrbrbirlbel The non variable part explains it, thanks.
    – Robai
    Feb 18 at 12:24

You must log in to answer this question.

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