How to Combine TikZ Orthogonal Edges with Coordinate Calculations
At a glance
- 1. Load calc
Add \usetikzlibrary{calc} when your coordinates use arithmetic such as midpoints or interpolated positions.
- 2. Calculate the endpoint
Write a calculated coordinate in TikZ’s coordinate form, such as ((B)!0.5!(C)).
- 3. Choose the route
Use -| for a horizontal-then-vertical path, or |- for a vertical-then-horizontal path.
- 4. Check the corner
Remember that the first coordinate supplies one axis and the second coordinate supplies the other.
TikZ’s orthogonal path operators and its coordinate-calculation syntax can be used together. The main distinction is that -| and |- describe how a path travels, while a calculated coordinate such as a midpoint describes where a path starts or ends.
For calculations such as midpoints and interpolated points, load the calc library. Then put the calculated coordinate in the usual TikZ coordinate parentheses and use it as an endpoint in the path.
Combine a calculated point with an orthogonal path
The operators choose which direction the path takes first:
(A) -| (B)goes horizontally from A, then vertically to B.(A) |- (B)goes vertically from A, then horizontally to B.
The corner is chosen automatically. With -|, its horizontal position comes from the second coordinate and its vertical position comes from the first. With |-, those roles are reversed.
A calculated coordinate can replace either endpoint. For example, ($(B)!0.5!(C)$) is the point halfway from B to C. The complete coordinate form includes the outer parentheses and the $...$ delimiters used by the calc library.
Worked example: route to a midpoint
This complete example draws an orthogonal arrow from node A to the midpoint between nodes B and C:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node (A) at (0,0) {A};
\node (B) at (4,2) {B};
\node (C) at (2,4) {C};
\draw[->] (A) -| ($(B)!0.5!(C)$);
\end{tikzpicture}
\end{document}
The midpoint of B at and C at is . The -| operator makes the arrow travel horizontally from A first, then vertically to that midpoint. The path starts at the named node A, so TikZ handles that endpoint using A’s shape boundary. The calculated midpoint is an ordinary coordinate and is not clipped to the shape of either B or C.
To travel vertically first and horizontally second, change -| to |-:
\draw[->] (A) |- ($(B)!0.5!(C)$);
Calculate the corner explicitly
Sometimes you want to name or reuse the turn point. TikZ can form the orthogonal corner as a coordinate with the same operators:
\coordinate (M) at ($(B)!0.5!(C)$);
\coordinate (turn) at (A -| M);
\draw[->] (A) -- (turn) -- (M);
Here (A -| M) places the turn at the horizontal position of M and the vertical position of A. This is the same corner used by (A) -| (M). For a vertical-then-horizontal corner, use (A |- M) instead. Naming the corner is useful if you need to attach a label, draw another segment from it, or inspect the route while debugging.
Common mistakes
- Omitting
calcfor arithmetic. Basic orthogonal paths like(A) -| (B)do not require thecalclibrary. Expressions such as($(B)!0.5!(C)$)do, so include\\usetikzlibrary{calc}in the preamble. - Leaving out the calculated-coordinate delimiters. Write
($(B)!0.5!(C)$)as the path endpoint, not just(B)!0.5!(C). The$delimiters tell TikZ to parse a coordinate calculation. - Confusing path syntax with coordinate syntax. In
\\draw (A) -| (M);,-|connects two points. In\\coordinate (turn) at (A -| M);, it computes a coordinate representing the corner. Both are useful, but they occur in different places in the command. - Choosing the wrong first leg.
-|means horizontal then vertical;|-means vertical then horizontal. If the route bends the wrong way, switch the operator. - Assuming the corner is a third endpoint. The orthogonal operator inserts the corner automatically. You need to calculate and name it only when you want to reuse or annotate it.
When the path needs more than one calculated point
You can chain ordinary segments and orthogonal segments in one path. For example, calculate an intermediate point, travel orthogonally to it, then continue to another point:
\\draw (A) -| ($(B)!0.5!(C)$) -- (D);
The first portion uses the -| routing rule; the -- portion is a straight segment. If you need to control the corner independently of both endpoints, compute that corner as a named coordinate and draw the route with two -- segments instead.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.