# Using calc in TikZ

How to use function calc in TikZ?

For example, suppose I have a function f(x)=x^2.

Now, I want to find f(1) and f(2), and then I want \draw f(1) -- f(2);.

-
Could you clarify what you mean by "I need os f(1) and f(2)"? –  Matthew Leingang Nov 16 '10 at 19:37
Is the question Jacques asked what you want? If it is, please tell us. If not, please post an additional explanation in Portuguese. Maybe someone can translate. –  Caramdir Nov 20 '10 at 3:56

A possibility with pgf 2.1

\documentclass{article}
\usepackage{tikz}
\makeatletter
\pgfmathdeclarefunction{square}{1}{%
\begingroup
\pgfmathparse{#1*#1}
\pgfmath@smuggleone\pgfmathresult%
\endgroup}

\begin{document}
\begin{tikzpicture}
\draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,8.1);
\draw[->] (-0.2,0) -- (3.2,0) node[right] {$x$};
\draw[->] (0,-0.2) -- (0,4.2) node[above] {$f(x)$};
\draw[domain=0:3] plot (\x,{square(\x)-1}) node[below right] {$f(x) = x^2$};
\end{tikzpicture}
\end{document}


-
I use PGF 2.1 and ok. Thanks. –  Regis da Silva Oct 8 '11 at 1:05

I'm not quite sure what you want to do.

If you want to plot a function, then the TikZ \plot command is what you are looking for:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw[very thin,color=gray] (-0.1,-0.1) grid (2.9,4.1);
\draw[->] (-0.2,0) -- (3.2,0) node[right] {$x$};
\draw[->] (0,-0.2) -- (0,4.2) node[above] {$f(x)$};

\draw[domain=1:2] plot (\x,{\x^2}) node[below right] {$f(x) = x^2$};
\end{tikzpicture}
\end{document}


The first few commands just draw the axes and the grid, the last command draws the plot. Of course the node[below right] {$f(x) = x^2$} part is entirely optional.

See the chapter "Plots of Functions" of the TikZ manual for details (it's chapter 19 in the 2.10 manual).

EDIT: An answer to Jacques's version of the question:

It probably doesn't work because the order of expansion of the macros is not right. I don’t know how to fix that directly, but pgf actually provides its own facility to create math functions: \pgfmathdeclarefunction. It is described in chapter 65 of the v2.10 manual (“Customizing the Mathematical Engine”). Here is a short description: The macro takes 3 aruments:

• the name of the function that should be created,
• the number of arguments the function takes
• some code that does the actual computation and sets the \pgfmathresult macro. The easiest way to do this is by a call to \pgfmathparse (this is of course not the most computationally efficient way to compute stuff, but it is the easiest way).

For example, a function square might be define by

\pgfmathdeclarefunction{square}{1}{\pgfmathparse{#1*#1}}


Then you can write code like \draw (1,{square(1)}) -- (2,{square(2)});.

-
you need to add \usepackage{tikz} in your code. –  Alain Matthes Apr 10 '11 at 9:16
@Altermundus: Thank you. –  Caramdir Apr 10 '11 at 15:50
@ Caramdir: Hi, could you post the tikz manual link where it mentioned in your post. Ch.19 in 2.1. Thanks –  night owl Jul 24 '11 at 20:10

This is not really an answer, but an attempt to make the question that I think Regis is asking more precise. If I do

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\square}[1]{\pgfmathparse{#1*#1}\pgfmathresult}
\begin{document}
\begin{tikzpicture}
\node {\square{1}};
\end{tikzpicture}
\end{document}


I do get a figure with just 1.0 at (0,0). On the other hand if I try to use my function \square to compute coordinates, I get errors which I do not understand:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\square}[1]{\pgfmathparse{#1*#1}\pgfmathresult}
\begin{document}
\begin{tikzpicture}
\draw (1,\square{1}) -- (2,\square{2});
\end{tikzpicture}
\end{document}


Replacing the \draw command by %\draw ($(1,\square{1})$) -- ($(2,\square{2})$); does not help.

-

With PSTricks it becomes much much easier. One method (among others) to represent the point in question is to use the following syntax.

(*<const RPN expression> {<infix expression in x>})


For example, point (*3 {x^2}) represents point (3,9).

## Minimal Working Example

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\def\f{x^2}

\begin{document}
\begin{pspicture}(-3,-1)(3,5)
\psaxes(0,0)(-3,-1)(3,5)
\psplot[linecolor=blue,algebraic]{-2}{2}{\f}
\psline[linecolor=red](*{-3 2 div} {\f})(*1 {\f})
\end{pspicture}
\end{document}


## Miscellaneous

If x is a function of y, we have another syntax as follows.

(**{<infix expression in y>} <const RPN expression>)


For example, point (**{-sqrt(y)} 4) represents point (-2,4).

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\def\f{x^2}

\begin{document}
\begin{pspicture}(-3,-1)(3,5)
\psaxes(0,0)(-3,-1)(3,5)
\psplot[linecolor=blue,algebraic]{-2}{2}{\f}
\psline[linecolor=red](**{-sqrt(y)} 9 4 div)(*1 {\f})
\end{pspicture}
\end{document}

-