1

Considering the following picture:

enter image description here

I would like to connect the two blue paths in order to obtain the red path like in the following picture:

enter image description here

But I would like a solution where I don't have to know the horizontal length of the red path, so Tikz will automatically calculate the good length instead of me.

MWE

I tried something with coordinates but I obtain this result (I'm not able to "shift" the coordinates (A) and (B) vertically):

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand*{\myDistance}{1}%

\begin{document} \begin{tikzpicture} % Preliminary node positionning \draw (0,0) node[rectangle,draw,minimum width=\myDistance] (Nod0) {Node 0}% ($ (Nod0.north west)!0.2\myDistance!(Nod0.north east) $) -- ++(0,2*\myDistance)% -- ++(\myDistance,0)% node[rectangle,draw,minimum width=\myDistance cm,anchor=west] (Nod1) {Node 1}% (Nod1.east) -- ++(\myDistance,0)% node[rectangle,draw,minimum width=\myDistance cm,anchor=west] (Nod2) {Node 2}% ;%

    % Two paths to connect
    \draw[blue] ($ (Nod0.north west)!0.8\myDistance!(Nod0.north east) $) --  ++(0,\myDistance);%
    \draw[blue] ($ (Nod1) !.5! (Nod2) $) -- ++(0,-\myDistance);%

    \coordinate (A) at ($ (Nod0.north west)!0.8\myDistance!(Nod0.north east) $)  +(0,\myDistance);
    \coordinate (B) at ($ (Nod1) !.5! (Nod2) $) +(0,-\myDistance);%

     \draw[red] (A) -- (B);%
\end{tikzpicture}

\end{document}

Why I don't want "absolute coordinates" ?

Because I want this graph to be easily modifiable, for example if I want to change the vertical and horizontal distance between Node 0 and Node 1, I just have to change the \myDistance value and everything will be automatically adapted, no need to manually repositioning the blue paths for example because everything is positioned relatively to Node 0.

Juan Castaño
  • 28,426
zetyty
  • 779
  • 1
    The +(…) isn't part of the coordinate specification, it's just a move to. You'll need to pull that into the coordinate specification of the coordinate. For example ($ (Nod1) !.5! (Nod2) + (0,-\myDistance) $). Though, as you are already at these points in the previous paths, just defining the coordinates there is much easier. – Qrrbrbirlbel Feb 24 '23 at 08:03
  • 1
    You might be interested in the path operation |- and -| as well as something like |-|. – Qrrbrbirlbel Feb 24 '23 at 08:38
  • '|-|' is a very elegant solution thank you. I think I will use this one. BTW, I was not able to use '\usepackage{tikz-ext}' after manual installation on Miktex (Miktex can't find the .sty so I got an error when compiling) but I am able to load the 'ext.paths.ortho' library now (after manual installation of tikz-ext on Miktex) so it works fine! Also, I assume that the package tikz-ext should be loaded after the tikz package but it's not mentioned on the manual. Thank you very much again! – zetyty Feb 24 '23 at 09:05
  • tikz-ext isn't a LaTeX package, it's a CTAN/TeXLive/MikTeX package (I don't like it either). You just need to load the library \usetikzlibrary{ext.paths.portho}. – Qrrbrbirlbel Feb 24 '23 at 09:13

1 Answers1

2

Just define two coordinates at the end of the blue paths, something like this:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand*{\myDistance}{1}%

\begin{document} \begin{tikzpicture} % Preliminary node positionning \draw (0,0) node[rectangle,draw,minimum width=\myDistance] (Nod0) {Node 0}% ($ (Nod0.north west)!0.2\myDistance!(Nod0.north east) $) -- ++(0,2*\myDistance)% -- ++(\myDistance,0)% node[rectangle,draw,minimum width=\myDistance cm,anchor=west] (Nod1) {Node 1}% (Nod1.east) -- ++(\myDistance,0)% node[rectangle,draw,minimum width=\myDistance cm,anchor=west] (Nod2) {Node 2}% ;%

    % Two paths to connect
    \draw[blue] ($ (Nod0.north west)!0.8\myDistance!(Nod0.north east) $) --  ++(0,\myDistance) coordinate (A);% <----
    \draw[blue] ($ (Nod1) !.5! (Nod2) $) -- ++(0,-\myDistance) coordinate (B);% <----

% \coordinate (A) at ($ (Nod0.north west)!0.8\myDistance!(Nod0.north east) $) +(0,\myDistance); % \coordinate (B) at ($ (Nod1) !.5! (Nod2) $) +(0,-\myDistance);%

     \draw[red] (A) -- (B);%
\end{tikzpicture}

\end{document}

enter image description here

Juan Castaño
  • 28,426