4

In this example

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
  \coordinate (a) at (0, 0);
  \coordinate (b) at (1, 1);
  \coordinate (c) at ($(a) + (b)$);
  \coordinate (d) at ($(a)!0.5!(c)$);
  \end{tikzpicture}
\end{document}

one could do away with the \coordinate(c) by defining \coordinate(d) as

\coordinate (d) at ($(a)!0.5!((a) + (b))$);

This substitution does not work out:

! Package pgf Error: No shape named (a is known.

How to rewrite the expression for \coordinate(d) without recurring to \coordinate(c)?

6

You can use:

\coordinate (d) at ($(a)!0.5!($(a) + (b)$)$);

The first image below uses

  \coordinate (c) at ($(a) + (b)$);
  \coordinate (d) at ($(a)!0.5!(c)$);

whereas the second uses the calculation mentioned above without the intermediate (c) coordinate.

enter image description here

Code:

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
  \coordinate (a) at (0, 0);
  \coordinate (b) at (1, 1);

  \coordinate (c) at ($(a) + (b)$);
  \coordinate (d) at ($(a)!0.5!(c)$);

  \node at (a) {a};
  \node at (b) {b};

  \node at (d) {d};
\end{tikzpicture}
\begin{tikzpicture}
  \coordinate (a) at (0, 0);
  \coordinate (b) at (1, 1);

  \coordinate (d) at ($(a)!0.5!($(a) + (b)$)$);

  \node at (a) {a};
  \node at (b) {b};

  \node at (d) {d};
  \end{tikzpicture}
\end{document}
1

A minor remark: since 0.5 a + 0.5 ( a + b ) =a + 0.5 b, you could also do

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
  \coordinate (a) at (0, 0);
  \coordinate (b) at (1, 1);
  \coordinate (d) at ($1*(a)+0.5*(b)$);
  \foreach \X in {a,b,d}
  \node at (\X){\X};
  \end{tikzpicture}
\end{document}

enter image description here

That is, calc allows you to take arbitrary linear combinations of the coordinates.

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.