0

Assume the MWE listed below.

This will produce the following picture. As you can see the vertical line is not 100% vertical.

How to get this line vertically?

Question 1: How do I say 'from point x go vertical up and connect to node y where exact anchor point at node y is unknown.' ?

Question 2: More general question: see screenshot below. What is the easiest way to make 'advanced' connections based on only vertical or horizontal lines? See the Blue line.

enter image description here

    \documentclass{minimal}
    \usepackage{tikz}
    \begin{document}
    \usetikzlibrary{positioning}
    \tikzstyle{st1}=[rectangle, fill=white,draw=black,text centered, anchor=north, text=black, text width=4cm]
    \begin{center}
    \begin{tikzpicture}[node distance=1cm]
        \node (Parent) [st1] { \textbf{Parent} };
      \node (hidden1) [text width=0.5cm, below=of Parent] {};    
        \node (Child) [st1,  right=of hidden1,xshift=-2cm] {\textbf{Child}};
        \draw (Child.west) -- ++(-0.2,0) -- ([ xshift=-0.8cm] Parent.south);
    \end{tikzpicture}
    \end{center}
    \end{document}
3
  • Very related: tex.stackexchange.com/questions/45347/… Dec 7 '15 at 7:48
  • Henri: Thanks for this link, however this one does not address my issue of the red line in the picture above. The vertical line is not 100% straight. So I am looking for a way to go straight up and connect to node X where it does reach it.
    – robert
    Dec 7 '15 at 8:02
  • Probably [this question](tex.stackexchange.com/questions/279012/a-good-way-to-do-routing-in-logic-circuits-with-tikz-circuitikz) will help? Dec 7 '15 at 8:37
2

If you load the calc library for some calculations and use Tikz operators, you can draw orthogonal paths with the required distances (2mm as in your question).

By the way:

  • draw=black is usually not necessary, since it defaults to black, unless something else is creating conflicts. In that case, draw=black will help with the exception.
  • No need to set a hidden node for positioning, just use the positioning library tools, like below right.
  • Prefer the standalone class.
  • Use tikzset for setting options, see Should \tikzset or \tikzstyle be used to define TikZ styles?

Output

example image

Code

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}

\usetikzlibrary{calc,positioning}

\tikzset{
    st1/.style={draw,rectangle, fill=white, text centered, anchor=north, text width=4cm, font=\bfseries}
}

\begin{document}

\begin{tikzpicture}%[node distance=1cm]

    \node (parent) [st1] {Parent}; 
    \node (child) [st1, below right=1cm and -2.5cm of parent] {Child};

    \draw[red, ultra thick] (child.west) -| ($(child.west|-parent.south)+(-2mm,0)$);
    \draw[blue!50!black, ultra thick] (child.east) -|
        ($(parent.north-|child.east)+(2mm,2mm)$) -|
        ($(parent.west)+(-2mm,0)$) |-
        (parent.west);

\end{tikzpicture}
\end{document}
4
  • 1
    Are there any advantages of using the calc library over xshift/yshift in this particular case?
    – jak123
    Dec 7 '15 at 9:13
  • @jak123 Not sure about actual differences, but I prefer the syntax: the code is shorter and easier to write/read.
    – Alenanno
    Dec 7 '15 at 9:15
  • I guess it is subjective then.
    – jak123
    Dec 7 '15 at 9:26
  • @jak123 Well, apart from the fact that you can do calculations you couldn't do otherwise. In this particular case, like I said, I can't think of any particular difference.
    – Alenanno
    Dec 7 '15 at 9:29
0
  \documentclass{article}
    \usepackage{tikz}
    \begin{document}
    \usetikzlibrary{positioning}
    \tikzstyle{st1}=[rectangle, fill=white,draw=black,text centered, anchor=north, text=black, text width=4cm]
    \begin{center}
    \begin{tikzpicture}[node distance=1cm]
        \node (Parent) [st1] { \textbf{Parent} };
      \node (hidden1) [text width=0.5cm, below=of Parent] {};    
        \node (Child) [st1,  right=of hidden1,xshift=-2cm] {\textbf{Child}};
        \draw (Child.west) -| ([xshift=-.8cm]Parent.south); %Question 1
        \draw (Parent.west) -| ++(-.5cm,1cm) -| ([xshift=.5cm] Child.east) -- (Child.east); %Question 2
    \end{tikzpicture}
    \end{center}
    \end{document}

enter image description here

3
  • 2
    Please do not use the minimal class. Dec 7 '15 at 7:49
  • Thanks but this answer does not answer question 1. The 'direct' line from Child to Parent is still not straight vertical.
    – robert
    Dec 7 '15 at 7:59
  • @robert I modified your code for this connector a little bit to address this point.
    – jak123
    Dec 7 '15 at 8:04

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.