# substitution in tikz calc

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

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.

## 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}


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}


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