3

I have the following coordinate calculation

\coordinate (a0) at ($(c1)!0.5!(c2)$);
\coordinate (a1) at ($(c3 -| a0) + (1,3)$);
\draw (a1) rectangle (b1);

I wanted to eliminate the first coordinate and use this code instead

\coordinate (a1) at ($(c3 -| ($(c1)!0.5!(c2)$)) + (1,3)$);
\draw (a1) rectangle (b1);

But it doesn't work, even when I put curly braces around the coordinate ($(c1)!0.5!(c2)$). What is the problem here?

Reza
  • 1,798

1 Answers1

2

The line

\coordinate (a1) at ($(c3 -| ($(c1)!0.5!(c2)$)) + (1,3)$);

gives you this error message:

Package pgf Error: No shape named `($(c1' is known.

which is a clue what's happening. Notice two things:

The name PGF/TikZ is looking up starts with an ( and end right before the first ).

In fact, let's remove one layer and try

\coordinate (a1) at (c3 -| ($(c1)!0.5!(c2)$));

This gives the same error message.

Two things are wrong here, the first is simple. When you specify a coordinate at the intersection of perpendicular lines through two other points, you do not specify them enclosed in ( and ). You didn't do it with c3, either!
Let's remove one set of parentheses:

 \coordinate (a1) at (c3 -| $(c1)!0.5!(c2)$);

Still won't work:

! Package tikz Error: + or - expected.

l.9 \coordinate (a1) at (c3 -| $(c1) !0.5!(c2)$);

The message itself isn't very helpful but the next line shows us where TikZ tripped up.

When TikZ encounters a ( it will – with the exception of ($, actually – grab everything until the next ) and assumes that to be the coordinate. Here it will have c3 -| $(c1 as the coordinate and everything breaks down. As with mathematical function, the ) needs to be hidden from the greedy parser:

\coordinate (a1) at (c3 -| {$(c1)!0.5!(c2)$});

And that's actually all, now that can be put as one coordinate of your coordinate sum:

\coordinate (a1) at ($(c3 -| {$(c1)!0.5!(c2)$}) + (1,3)$);

When the parser finds ($ it will hand of the coordinate parsing to the calc library which will find another ( and hands the parsing of the number back to TikZ (which will grab greedily, finds c3 and $(c1)!0.5!(c2)$ where the latter will be handed back to calc because of the $.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (b1) at (rnd, rnd)
 coordinate (c1) at (rnd, rnd)
 coordinate (c2) at (rnd, rnd)
 coordinate (c3) at (rnd, rnd);
\coordinate (a1) at ($(c3 -| {$(c1)!0.5!(c2)$}) + (1,3)$);
% alternatives:
% \coordinate (a1) at ([shift={(1,3)}] c3 -| {$(c1)!0.5!(c2)$});
% \coordinate (a1) at ([shift={(1,3)}] perpendicular cs:
%   horizontal line through = {(c3)},
%   vertical line through   = {($(c1)!0.5!(c2)$)});
\draw (a1) rectangle (b1);
\end{tikzpicture}
\end{document}
Qrrbrbirlbel
  • 119,821