4

I want to know if there is a way to define a variable that depends on a trigonometric function, like this.

 \newcommand{\cosTheta}{cos(30)}

Such that it is parsed before using it in a tikz segment, like this

 \begin{tikzpicture}[scale=3.5]
      \draw (0,0) -- (\cosTheta,0);
 \end{tikzpicture}

Right now, I'm getting the error

 Package pgf Error: No shape named -1*cos(30 is known.

Sorry if the tags were not chosen well, please feel free to add to them. Any help is greatly appreciated!

  • 1
    Welcome to TeX.SX! It's a problem with the parentheses: ({\cosTheta},0) might work. – egreg Apr 15 '15 at 22:39
  • Does that force parcing at compile time? What if I wanted to define another newcommand based on \cosTheta? – Charles Apr 15 '15 at 22:45
  • Please, supplement your question with an example of what you want to do. – egreg Apr 15 '15 at 22:46
  • Actually, this works just fine, thanks again! – Charles Apr 15 '15 at 22:49
  • 1
    @egreg Please drop an answer. This will be 200th :-) – user11232 Apr 16 '15 at 0:00
5

When \cosTheta is expanded, TeX sees a ) that matches the opening one, because only {} pairs are matched by counting them. So just do

\draw (0,0) -- ({\cosTheta},0);

and the problem should be solved.

  • This looks great, and I would like to accept it, but I can't help but notice that \draw (0,0) -- (1-{\cosTheta},0); doesn't work. Can you elaborate on this too? When I try this, the error I get is "Missing number, treated as zero" – Charles Apr 16 '15 at 15:41
  • @Charlie You have to brace the whole expression: \draw(0,0)--({1-\cosTheta},0); – egreg Apr 16 '15 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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