How do I write a macro that will return a path or a coordinate?
Here's an example:
\documentclass[border=2em]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
% \circleByPoint{centre}{point} draws a circle with centre at centre through point
\newcommand\circleByPoint[2]{
\draw let \p1 = (#1),\p2 = (#2) in
\pgfextra{\pgfmathveclen{\x1-\x2}{\y1-\y2}}
(#1) circle (\pgfmathresult pt);
}
\begin{document}
\begin{tikzpicture}
\node at (0,0) (a) {};
\node at (1,0) (b) {};
\node at (0,3) (c) {};
\circleByPoint{a}{b}
\circleByPoint{b}{c}
\circleByPoint{c}{a}
\end{tikzpicture}
\end{document}
Here I define a macro that draws a circle. Likewise I can define a macro that draws the perpendicular bisector of a line segment (given two points). The question is, what if I want the outcome of the macro to be the path rather than having it function to just draw the path? Many program languages have something like a return which allows you to specify what the function returns. I can't work out how to get this to work in TikZ...
For example, the following MWE fails to compile properly (in fact, it hangs):
\documentclass[border=2em]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand\apath[2]{
\path (#1) -- (#2);
}
\begin{document}
\begin{tikzpicture}
\node at (0,0) (a) {};
\node at (1,0) (b) {};
\node at (0,3) (c) {};
\node at (intersection of \apath{a}{b} and \apath{a}{c}) {x};
\end{tikzpicture}
\end{document}
So my question is, how do I write helper functions for TikZ like this?
(I know that Altermundus' tkz bundle actually defines most of the functions I could possibly want for plane geometry, but the point of this question is to learn more about how to write TikZ code.)
\usepackage{tikz} \usetikzlibrary{intersections}. Then I think the parser is not very happy with\apathbecause afterintersection ofa node is necessary and withname intersections={of= ....}you need to use names for the paths. I'm not a great expert of tikz'syntax so I think other user can help you to find how to do this only with Tikz. An idea is to use\pgfextraand to modify your macro to name the paths. – Alain Matthes Jun 02 '11 at 17:55