[Tex/LaTex] Tikz error: Missing number, treated as zero

tikz-pgf

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 ?

Best Answer

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!