# How can I nest coordinate calculations in TikZ?

Why does this not work? I'm not using LaTeX and TikZ very much; I don't see it.

\begin{tikzpicture}
\coordinate (Zero)  at (0,0);
\coordinate[label=below:cell a] (vertexA) at (0, 10);
\coordinate[label={above right:cell b}, label={below right:cell c}]
(vertexB) at (5, 8);
\coordinate(vertexC) at (2.5, 0);
\coordinate(vertexD) at (12, 4);

\coordinate (centroidCellA) at  ($1/3*((vertexA)+(vertexB)+(vertexC))$);
\end{tikzpicture}


I get

! Package pgf Error: No shape named (vertexA is known.

-
I tried the code in QTikZ and is OK, something must be wrong with your pgf installation. –  fabikw Mar 30 '11 at 15:26
That code snippet compiles without errors in my system after suitable completion. Please post the complete version (including \documentclass, relevant packages used, etc.) of the problematic code. –  Gonzalo Medina Mar 30 '11 at 15:27

The syntax for coordinate calculations is

<factor>*<coordinate><modifiers>


The problem is that (vertexA)+(vertexB)+(vertexC) is not a coordinate. You have to tell tikz that you want it to calculate the coordinate at (vertexA)+(vertexB)+(vertexC). This will work:

\coordinate (centroidCellA) at  ($1/3*($(vertexA)+(vertexB)+(vertexC)$)$);


(You can nest coordinate calculations).

-
Funny, I tried ($1/3*({(vertexA)+(vertexB)+(vertexC)})$) which doesn't work, while I thought it should. And I was under the impression that you can't nest coordinate calculations (without hiding the inner one in { }). I have to check how TikZ does the parsing in this case... –  Martin Scharrer Mar 30 '11 at 16:21
"The problem is that (vertexA)+(vertexB)+(vertexC) is not a coordinate." How should anybody see this? vector + vector == vector! –  Nils Mar 30 '11 at 16:48
{} is for factors, right? –  Nils Mar 30 '11 at 16:49
@Nils: The {} avoids that the TeX parser is taking the included ) as end of the first (. The same is needed if you have [] inside an optional argument. However, it doesn't work here as I thought. –  Martin Scharrer Mar 30 '11 at 17:21
@Nils: I know that and you know that, but does tikz know that? –  Jan Hlavacek Mar 30 '11 at 17:41

Jan's answer is perfect. Another possibility is

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\coordinate(vertexA) at (0, 10);
\coordinate (vertexB) at (5, 8);
\coordinate(vertexC) at (2.5, 0);

%\coordinate (centroidCellA) at  ($1/3*($(vertexA)+(vertexB)+(vertexC)$)$);
\coordinate (centroidCellA) at (barycentric cs:vertexA=1 ,vertexB=1,vertexC=1);
\end{tikzpicture}

\end{document}

-