9

As it is described there, the following does not compile:

\documentclass{report}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\node at (0,  0) (node1) {Hello};
\node at (0, -2) (node2) {World};

% working:
\draw ($ (node1.south) + (1,0) $) to ( node1.south |- node2.north);

% not working:
\draw ($ (node1.south) + (1,0) $) to ( ($ (node1.south) + (1,0) $) |- node2.north);

\end{tikzpicture}
\end{document}

Why? How to make it work?

3 Answers 3

9

The syntax of the line to operation given in the manual is to place |- between two coordinates:

Sometimes you want to connect two points via straight lines that are only horizontal and vertical. For this, you can use two path construction operations.

\path . . . -|< coordinate or cycle> . . . ;

This operation means “first horizontal, then vertical.”

Following by these example:

\begin{tikzpicture}
\draw (0,0) node(a) [draw] {A} (1,1) node(b) [draw] {B};
\draw (a.north) |- (b.west);
\draw[color=red] (a.east) -| (2,1.5) -| (b.north);
\end{tikzpicture}

and these drawing: two-path

Thus, this two-path operation is not intended to make a translation from one point to another. This is a handy shortcut when you need to draw horizontal lines followed by vertical lines or vice versa.

You just have to write (syntactically) in your code:

\draw ($ (node1.south) + (1,0) $) to ($ (node1.south) + (1,0) $) |- (node2.north);

instead of

\draw ($ (node1.south) + (1,0) $) to ( ( ( $ (node1.south) + (1,0) $) |- node2.north);`

Your code becomes like this:

\documentclass{report}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\node at (0,  0) (node1) {Hello};
\node at (0, -2) (node2) {World};

% working:
%\draw ($ (node1.south) + (1,0) $) to ( node1.south |- node2.north);

% now working too:
\draw ($ (node1.south) + (1,0) $) to  ($ (node1.south) + (1,0) $) |- (node2.north);
\end{tikzpicture}
\end{document}

To have the same path as the one you want, you must build your path as indicated by @marmot or @ignasi.

2
  • Not clear what is the use of ($ (node1.south) + (1,0) $) to ($ (node1.south) + (1,0) $) in the second path.
    – Kpym
    Aug 27, 2018 at 12:00
  • @Kpym yes, i edited my answer
    – AndréC
    Aug 27, 2018 at 15:18
8

Just for completeness: a version that is really the equivalent of the first. AndreC's nice answer is correct but I don't see how the second path, which has a corner, is the shifted version of the first one.

\documentclass{report}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\node at (0,  0) (node1) {Hello};
\node at (0, -2) (node2) {World};

% working:
\draw ($ (node1.south) + (1,0) $) to ( node1.south |- node2.north);

% not working:
\draw ($ (node1.south) + (1,0) $) to ([xshift=1cm] node1.south |- node2.north);

\end{tikzpicture}
\end{document}
1
  • 1
    Interesting, this adds semantics to the syntactic correction, thanks :)
    – iago-lito
    Aug 27, 2018 at 11:05
5

You can always declare an auxiliary coordinate and use it. This way you don't need to remember which is the working syntax ;-)

\documentclass{report}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\node at (0,  0) (node1) {Hello};
\node at (0, -2) (node2) {World};

% working:
\draw ($ (node1.south) + (1,0) $) to ( node1.south |- node2.north);

% working:
\draw ($ (node1.south) + (1,0) $) coordinate (aux) to (aux |- node2.north);

\end{tikzpicture}
\end{document}
4
  • That is true. I was rather willing to avoid this (the lazyness is about 'picking an unused name for the auxiliary coordinate' XD), but it is a useful workaround. Cheers :)
    – iago-lito
    Aug 27, 2018 at 12:13
  • 1
    @iago-lito you just need to declare a non used name if all previously declared are relevant later on, otherwise you can reuse them.
    – Ignasi
    Aug 27, 2018 at 12:20
  • Yes, this is how I also do it in the general case.... ;-) +1
    – user121799
    Aug 27, 2018 at 12:23
  • Correct. This is an easy choice when working on a neat document. Not when rushing to produce a big dirty one. I hate this but sometimes your environment dictates it's better you don't ponder anything :'( I usually enforce aux, tp, etc. never to be "relevant later on".. until one day I'll wonder "what's happening?!" XD
    – iago-lito
    Aug 27, 2018 at 12:29

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.