Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
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
show 2 more comments

1 Answer

up vote 4 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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