Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How to use function calc in TikZ? Example, my function is f(x)=x^2. I need os f(1) and f(2), \draw f(1) -- f(2);

share|improve this question
3  
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
add comment

3 Answers

up vote 4 down vote accepted

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}

enter image description here

share|improve this answer
    
I use PGF 2.1 and ok. Thanks. –  Regis da Silva Oct 8 '11 at 1:05
add comment

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} 

plot

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

share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.