8

I had to do some calculations like:

\node (A) {A};
\node (b) at (x,y) {B};

((A) + (B))*2

However I could not figure out how to group it in a calc statement, I tried ($((A)+(B))*2$) and (${(A)+(B)}*2$). How can I express this? (Currently I use ($(A)*2+(B)*2$), so I have to adjust the factor in two places)

A similar issue occurs with the |- Operator. I can use it in calculations as long as I don't try pulling of mathematic operations on one of the operands. Is there a way to achieve ($((A)+(B)) |- (4,7)$)?

10

For the first question, you can nest coordinate calculations to first calculate A+B, and then multiply this result by 2:

\node at ($2*($(A)+(B)$)$) {C};

Another way is mentioned by Qrrbrbirlbel in a comment below:

\node at ([scale=2]$(A)+(B)$) {C};

For the second question, see Andrew Stacey's answer on Combining |- and !.5! in TikZ. In short, use two pairs of braces around the calculation of the first coordinate:

\node at ({{$(A)+(B)$}} |- 4,7) {D};

Complete example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}

\draw [help lines] (0,0) grid (2,3); 

\node (A) at (0,1) {A};
\node (B) at (1,0) {B};

\node at ($2*($(A)+(B)$)$) {C};

\node at ({{$(A)+(B)$}} |- 4,3) {D};
\end{tikzpicture}
\end{document}

enter image description here

  • Another possibility for C is \node at ([scale=2]$(A)+(B)$) {C};. – Qrrbrbirlbel Feb 4 '13 at 19:58
  • Indeed that was the answer I was mentioning. Great. – percusse Feb 4 '13 at 20:39
  • @Qrrbrbirlbel Thanks, I added that to my answer as well. – Torbjørn T. Feb 5 '13 at 8:40
7

The let action is very useful in theses cases or for more complex calculations:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node (A) at (0,1) {A};
\node (B) at (1,0) {B};
\path let \p1=($(A)+(B)$), \p2=($2*(\p1)$) in node at (\p2) {C};
\path let \p1=($(A)+(B)$), \p2=(\p1 |- 4,7) in node at (\p2) {D};
\end{tikzpicture}
\end{document}

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.