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.

When can I use functions and do calculations in TikZ? How do I make this work:

\draw (0,0) arc(0:90:sqrt(15)); %not ok

why is this working:

\draw (0,0) arc(0:asin(1):5); %ok

with \usetikzlibrary{calc} is this:

 \draw (0,0) -- ($ (4,0) + sqrt(7)*(0,1) $);  %ok

the only way to do single coordinate calculations? why is this

 \draw (0,0) -- ($ (4,sqrt(7)) $);  %not ok

not working?

Minimal example:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw (0,0) arc(0:90:sqrt(15));              %not ok
\draw (0,0) arc(0:asin(1):5);                %ok
\draw (0,0) -- ($ (4,0) + sqrt(7)*(0,1) $);  %ok
\draw (0,0) -- ($ (4,sqrt(7)) $);            %not ok
\end{tikzpicture}
\end{document}
share|improve this question
1  
Welcome to TeX.sx! Please always try to add a complete minimal working example (MWE) that illustrates your problem. Also post the exact error message you get instead of simply stating "doesn't work". Thanks! –  Martin Scharrer Oct 17 '11 at 15:12
    
BTW, you might want to register your account, otherwise you loose access to your old posts after your session expires. Note that you can now also up-vote answers and other questions. –  Martin Scharrer Oct 17 '11 at 15:41
    
@Hans-Peter: Could you try to find a more specific title for this question? Titles with Problem with usually aren't very helpful for someone who wants to find out if this question will be helpful or not. –  doncherry Jan 9 '12 at 20:26
    
@doncherry: I will change it, if you suggest something better. –  Hans-Peter E. Kristiansen Jan 9 '12 at 22:31
    
I'd love to, but I neither use TikZ nor study math. Maybe @MartinScharrer can help? –  doncherry Jan 9 '12 at 23:21
add comment

2 Answers

up vote 20 down vote accepted

You need to wrap the expression into { } to hide the second pair of ( ) from the TeX parser. Without the { } a ( will be closed by the next ) even if it belongs to another (. This means arc(0:90:sqrt(15)) will be taken as arc(0:90:sqrt(15) without the second ). This causes basically two errors, one in the expression because it misses the ) and another one in the \draw path which doesn't know what to do with a single ). With the { } it works because any { must be closed first with a } before the ) is taken.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}
    \draw (0,0) arc(0:90:{sqrt(15)}); %now ok
    \draw (0,0) arc(0:{asin(1)}:5); %ok
    \draw (0,0) -- ($ (4,0) + sqrt(7)*(0,1) $);  %ok
    \draw (0,0) -- ($ (4,{sqrt(7)}) $);  %now ok
\end{tikzpicture}

\end{document}
share|improve this answer
    
Wow, I didn't know that... The Tikz manual does specify that uou can use curly braces in factors, I should have tried them in coordinates as well. I still don't understand why asin(x) in the arc would cause no problems, while sqrt(x) does. Can you explain this? –  Roelof Spijker Oct 17 '11 at 15:12
2  
@whlt3: This all is rooted in the way TeX handles a parameter text, i.e. is a low-level thing and not from TikZ. The arc arguments is read by the internal macro \def\tikz@@@arcto@check@slashand(#1:#2:#3){ ... } which awaits the three parts between :, so a ) in the second argument can't close the ( because TeX still looks for the : then stores everything up to the next ) as #3. You might want to read The TeXBook to learn about such TeX macros. –  Martin Scharrer Oct 17 '11 at 15:21
    
Actually, arc(0:asin(1):5) works because of the reason stated in my comment above, but arc(0:{asin(1)}:5) is much saver. TikZ reads the whole (...) first (i.e. arc(0:asin(1) with the first form) and expands it using \edef, then passes it to the macro mentioned in my last comment. Using the first form anything after the inner ) is not expanded! –  Martin Scharrer Oct 17 '11 at 15:23
    
Ah, that's awesome :) I'm currently trudging through TeX by Topic, will have a look at the TexBook if I ever get through that. –  Roelof Spijker Oct 17 '11 at 15:31
    
See also TikZ \foreach loop evaluate variable using pgfmath function for a similar problem.. –  Håkon Hægland Mar 12 '13 at 9:33
add comment

Edit: This is incorrect, disregard this.. Well, it's correct up to the point that the coordinates can't have math in them. Disregard it anyway.

It's just how the Tikz calc library is set up. If we have a look at section 13.5 of the manual

The general syntax is the following:
([ options ]$ coordinate computation $).
As you can see, the syntax uses the TEX math symbol $ to indicate that a “mathematical computation” is involved. However, the $ has no other effect, in particular, no mathematical text is typeset.
The coordinate computation has the following structure:
1. It starts with < factor > * < coordinate > < modifiers >
2. This is optionally followed by + or - and then another < factor > * < coordinate > < modifiers >
3. This is once more followed by + or - and another of the above modified coordinate; and so on.
In the following, the syntax of factors and of the different modifiers is explained in detail.

The following you will have to read in the manual for yourself. The important thing here is that the math can only occur in the factor and the modifiers. Not in the coordinate itself, which explains why your second example does not work. The reason the first example doesn't work is explained in the manual as well. It is the same type of restriction in the Tikz grammar.

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.