# Can we use functions to compute coordinate X and Y in a tikzpicture?

I would like to compute coords X and Y to build a figure :

\documentclass[a4paper,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}

\usepackage{tikz} % pour dessins
\usetikzlibrary{shapes, calc, arrows} % pour complement tikz

\begin{document}

\begin{tikzpicture}
\def\focale{3}
\def\angleg{32}
\def\angled{90-\angleg}

\coordinate (P1) at (\tan(\angleg)*\focale,1.5cm);
...
\end{tikzpicture}%<<<== must use well balanced curly-braces

\end{document}


but i have got an error :

Missing \endcsname inserted \coordinate (P1) at (\tan(32)


I tried to include the compute within $ but : Paragraph ended before \tikz@cc@parse@factor was complete  - You just need braces, otherwise Tikz cannot recognize a calculation from a point ({tan(\angleg)*\focale},1.5cm). – zeroth Jul 11 '13 at 13:32 many thanks and sorry...i have scanned some introductions notes to tikz, but i should have read in details... – Patrick Dezecache Jul 11 '13 at 13:41 Many thanks for all participations ! I think i will use answers linked to tikz, but pstricks seems to be quite interessant too. great tools ! and a great teams to support it ! - patrick – Patrick Dezecache Jul 11 '13 at 17:27 ## 3 Answers Short answer is: Yes, you can. Your error is that you're typing the function with a backslash \ which is the way to indicate to LaTeX that it's a command in the compilation. When calculating a number, functions shouldn't have a backslash, just tan(angleg) and so on. For more complicated functions, use the curly bracket to indicate they are supposed to be together (as stated by @zeroth )({tan(\angleg)*\focale},1.5cm). - I disagree with the one statement of the reply about the \ (backslash) : • the \tan must be replaced by 'tan' because you want the mathematical function to be aplied to the \angleg and not to write 'tan(58)' • you must keep the '\angleg' because TeX has no idea about 'angleg ' (it's pure text) I agree with the reply about the {} (curly brackets), which are mandatory each time you have some () in the point definition. But the full answer would be to use the$ syntax to compute the point coordinates :

\coordinate (P1) at ($({tan(\angleg)*\focale},1.5cm)$);


Futhermore, it could be better practice to replace

\def\gnat{<some math expresion>}


by

\pfgmathsetmacro{\gnat}{<some math expresion>}


because the calculation is then parsed and performed immediatly when using the latter.

-
You're right about the backslash in \angleg, but it's not necessary to use the \$ syntax from the calc library if you're only evaluating a mathematical expression in a coordinate component. The calc library is only necessary when you're performing calculations that use full coordinates. –  Jake Jul 11 '13 at 14:12

With PSTricks. Just for comparison purposes.

PSTricks equivalent for TikZ ({tan(\complement)*\radius},1.5) is

• (+{Tan(\complement)*\radius},1.5)
• (**{Tan(\complement)*\radius} 1.5)
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node}

\def\angle{45}
\def\complement{90-\angle}

\begin{document}
\begin{pspicture}[showgrid](6,3)
\pscircle*[linecolor=red](P1){2pt}
\end{pspicture}
\end{document}


## Miscellaneous

In PSTricks, there are some ways to define a coordinate. It can be expressed in terms of existing nodes or mathematics expressions. With mathematics expression, we have 4 methods as follows.

• RPN for both abscissa and ordinate.

For example: (!2 3 mul 1 sub 2 0 add) is a point (5,2).

• RPN for abscissa and algebraic for ordinate. The ordinate can be a function of x.

For example: (*{2 3 add} {x-3}) is a point (5,2).

• Algebraic for abscissa and RPN for ordinate. The abscissa can be a function of y.

For example: (**{y*3-1} {1 1 add}) is a point (5,2).

• Algebraic for both abscissa and ordinate.

For example: (+{3+2},1+1) is a point (5,2).

The following example uses \def to express algebraic expression as a function of x, y or a constant.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node}
\def\f[#1]{2*#1-1}

\begin{document}
\begin{pspicture}[showgrid](4,3)
\pscircle*[linecolor=red](+{\f[2],\f[1]}){2pt}% its center is (3,1)
\pscircle*[linecolor=green](*{1 0 add} {\f[x]}){2pt}% its center is (1,1)
\pscircle*[linecolor=blue](**{\f[y]} {2 0 add}){2pt}% its center is (3,2)
\end{pspicture}
\end{document}

-
What's the difference between the + syntax and the ** syntax (apart from the fact that one needs a comma between the components, and the other doesn't)? –  Jake Jul 11 '13 at 14:56
@Jake: I will update my answer to accommodate more general cases. –  cyanide-based food Jul 11 '13 at 14:57