7

I am trying to create a macro to calculate the center of a shape. Consider the following code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\getcellcent}[4]{
  \coordinate (#1) at ($1/3*(V#2)+1/3*(V#3)+1/3*V(#4)$);
}
\begin{document}
  \begin{tikzpicture}
    \coordinate (V1) at (0,0);
    \coordinate (V2) at (2,0);
    \coordinate (V3) at (1,1);
    \getcellcent{C1}{1}{2}{3}
    \draw[black] (V1) -- (V2) -- (V3) -- cycle;
    \node[circle,radius=1,fill=red] at (C1)  {};
  \end{tikzpicture}
\end{document}

Here I get the following error message:

Runaway argument?
1/3*V(3)$);  \draw [black] (V1) -- (V2) -- (V3) -- cycle; \node [circ\ETC.
! File ended while scanning use of \tikz@cc@parse@factor.

If I change the line with \getcellcent to

\coordinate (C1) at ($1/3*(V1)+1/3*(V2)+1/3*(V3)$);

It works fine. What am I missing here?

  • 6
    the brace in V(#4) is wrong, it should be before the "V". – Ulrike Fischer Feb 2 '13 at 11:49
  • 1
    I confirm this, it works with \coordinate (#1) at ($1/3*(V#2)+1/3*(V#3)+1/3*(V#4)$);. – Stefan Kottwitz Feb 2 '13 at 11:56
  • @UlrikeFischer You have the eye of a "hawk". Will you post this as an answer to close the question? – Yiannis Lazarides Feb 2 '13 at 12:09
  • Thank you! I should of course have spotted this mistake myself.. – Håkon Hægland Feb 2 '13 at 12:13
  • 3
    You can also do a similar calculation without the calc library using \coordinate (#1) at (barycentric cs:V#2=1/3,V#3=1/3,V#4=1/3); – percusse Feb 2 '13 at 12:25
4

the opening brace in V(#4) is wrong, it should be before the "V"

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.