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)$)?

ted
  • 3,377

2 Answers2

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

Torbjørn T.
  • 206,688
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}
Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283