5

I am having an issue with Tikz. Here is the code:

\begin{tikzpicture}[scale=0.6]

% Variables
\def\mu{0.1}
\def\R{10}

%Celestial bodies
\draw [thick, fill=yellow] (0,0) circle (1);
\draw [thick, fill=cyan] (\R,0) circle (0.25);

% Lagrangian points
\node at (\R*{1-{\mu/3}^{1/3}},0) {\color{orange}{\huge$\bullet$}}; %L1
\node at (\R*{1+{\mu/3}^{1/3}},0) {\color{orange}{\huge$\bullet$}}; %L2
\node at (-\R*{1+5/12*\mu},0) {\color{orange}{\huge$\bullet$}}; %L3
\node at (\R*{1/2*{1-2*\mu}},\R*sqrt(3)/2) {\color{orange}{\huge$\bullet$}}; %L4
\node at (\R*{1/2*{1-2*\mu}},-\R*sqrt(3)/2) {\color{orange}{\huge$\bullet$}}; %L5


\end{tikzpicture}

Here is the error I get:

! Missing number, treated as zero.
<to be read again>
{
l.87 \node at (\R*{1-{\mu/3}^{1/3}},0)
{\color{orange}{\huge$\bullet$}}; %L1
A number should have been here; I inserted `0'.

I see this is a common error with Tikz but I can't find out why it doesn't work. It is propably a silly mistake, can someone tell me what's wrong ?

Loïc Poncin
  • 319
  • 2
  • 9

1 Answers1

7

There were three problems:

  1. If you are doing a calculation in a coordinate you must enclose it1 with braces: ({\R*sqrt(3)},0)

  2. Inside an operation, you group with parentheses, not braces.

Fixed code:

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=0.6]

% Variables
\newcommand{\muu}{0.1}
\newcommand{\R}{10}

%Celestial bodies
\draw [thick, fill=yellow] (0,0) circle (1);
\draw [thick, fill=cyan] (\R,0) circle (0.25);

\node at ({\R*(1-(\muu/3)^(1/3))},0) {\color{orange}{\huge$\bullet$}}; %L1
\node at ({\R*(1+(\muu/3)^(1/3))},0) {\color{orange}{\huge$\bullet$}}; %L2
\node at ({-\R*(1+5/12*\muu)},0) {\color{orange}{\huge$\bullet$}}; %L3
\node at ({\R*(1/2*(1-2*\muu))},{\R*sqrt(3)/2}) {\color{orange}{\huge$\bullet$}}; %L4
\node at ({\R*(1/2*(1-2*\muu))},{-\R*sqrt(3)/2}) {\color{orange}{\huge$\bullet$}}; %L5

\end{tikzpicture}
\end{document}

The third problem:

As Jasper said, avoid using \def, unless you are absolutely sure of what you're doing. The \mu you are defining already exists (the greek letter μ).

If you use \newcommand instead, LaTeX will tell you that you are redefining an existing macro.


1 That's not the whole story. When TikZ finds a ( it understands that it's reading a coordinate. It also expects that the next ) will end the coordinate.

So when TikZ finds (\R*sqrt(3),0), it understands \R*sqrt(3 as the coordinate, which is bad, and still, it gets a ,0) after, which is worse. But when you group the expression inside braces, everything inside the {...} will be treated as a single token, which will be passed to the math interpreter to do its job.

So whenever you have expressions with parentheses, you have to hide them inside {...}. Although its no harm to enclose an expression without parentheses too.

Thanks to @hpekristiansen for pointing that out!

  • Awesome thank you ! So I juste have to add {...} around the math expression ? What did you edit out \usetikzlibrary{calc} ? – Loïc Poncin Mar 06 '18 at 22:33
  • @LoïcPoncin You have to add the {...} around the math expression and group the operations with (...) (you was grouping with {...}). I included the \usetikzlibrary{calc} at first, but then I realised that it is not necessary for this example, so I removed it. – Phelype Oleinik Mar 06 '18 at 22:36
  • @PhelypeOleinik, I have to mention that you are a little bit lucky... Any greek character that defined by \namecommand has an uppercase defined by \Namecommand... In this case uppercase \mu happens to be same as latin M and so is not defined as \Mu... – koleygr Mar 06 '18 at 22:45
  • @koleygr That's exactly why I used \Mu and not \muu ;) Thanks! – Phelype Oleinik Mar 06 '18 at 22:49
  • 1
    For safety I would use \MU or \muu insrtead... (Someone could use a command to upercase greek letters in math mode...) Any way... (+1) – koleygr Mar 06 '18 at 22:54
  • @koleygr Hmmm... Good point :) – Phelype Oleinik Mar 06 '18 at 22:55
  • 2
    "you must enclose it with braces" is not entirely true the real explanation can be read here : https://tex.stackexchange.com/a/31833/8650 – hpekristiansen Mar 06 '18 at 23:34
  • @hpekristiansen Oooh, now I get it. Thanks! – Phelype Oleinik Mar 07 '18 at 10:36