1

I predefined the nodes and now the edges are connecting the nodes as follows:

\documentclass {scrartcl}
\usepackage{tikz}
\begin{document}
\begin{center}

\begin{tikzpicture}
\tikzstyle{vertex} = [ color=black, text=black, fill=black!10] \tikzstyle{edge} = [thick]

% place nodes
\node[draw]  [vertex] at (0, 3)   (a) {Node 6};
\node[draw] [vertex] at (1, 4)   (b) {Node 12};
\node[draw] [vertex] at (2, 3)  (c)     {Node 19};
\node[draw] [vertex] at (3, 1)  (e)     {Node 20};
\node[draw] [vertex] at (4,3)  (d)     {Node 18};
\node[draw] [vertex] at (4,7)  (f)     {Node 10};
\node[draw] [vertex] at (6,1)  (g)     {Node 4};
\node[draw] [vertex] at (6,3)  (h)     {Node 2};
\node[draw] [vertex] at (6,6)  (i)     {Node 13};
\node[draw] [vertex] at (6,8)  (j)     {Node 14};
\node[draw] [vertex] at (7,3)  (k)     {Node 21};
\node[draw] [vertex] at (7,4)  (l)     {Node 1};
\node[draw] [vertex] at (7,7)  (m)     {Node 16};
\node[draw] [vertex] at (8,3)  (n)     {Node 3};
\node[draw] [vertex] at (8,9)  (o)     {Node 8};
\node[draw] [vertex] at (9,2)  (p)     {Node 5};
\node[draw] [vertex] at (9,5)  (q)     {Node 9};
\node[draw] [vertex] at (5,1)  (r)     {Node 11};
\node[draw] [vertex] at (7,-1)  (s)     {Node 7};
\node[draw] [vertex] at (8,-3)  (t)     {Node 17};
\node[draw] [vertex] at (9,-5)  (u)     {Node 22};
\node[draw] [vertex] at (5,-3)  (v)     {Node 15};


%Draw edges \draw[edge][color=red] (o)--(m)--(i)--(h)--(d)--(e)--(r)--(v); \draw[edge][color=purple] (j)--(i)--(h)--(g)--(s)--(t)--(u); \draw[edge][color=blue] (f)--(i)--(h)--(k)--(n)--(q); \draw[edge][color=green] (l)--(h)--(d)--(b); \draw[edge][color=yellow] (b)--(d)--(e)--(r)--(g)--(s)--(t)--(u);

\end{tikzpicture}
\end{center}

\end{document}

\end{document}

When two edges connect the same nodes, I would like to see both lines and not just one, so I want to shift one line for some mm. How can I achieve that?

Black Mild
  • 17,569
Laura
  • 11
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – Rmano Jan 26 '22 at 11:27
  • Do you want to have this automatically, or are you OK with shifting manually starting and end points of your edges ? If the latter, you can xshift and yshift the coordinates of these points as you prefer. Also, after typesetting your sample, I notice that some vertices overlap. You could use the positioning tikz library in order to give the positions of your vertices relative to one another and specify a single distance between all of them. Lastly, there's one \end{document} too many in your MWE... – v_manz Jan 26 '22 at 13:48
  • 1
    https://tex.stackexchange.com/a/199069/197451 -- also better to have relative positioning for the nodes rather than absolute – js bibra Jan 26 '22 at 13:57
  • Yes, shifting manually is fine for me. I tried to include it to my code, but it does not work for the edges, only the nodes. Could you give me an example, because I am not sure where to include xshift and yshift? @v_manz – Laura Jan 26 '22 at 13:58
  • 1
    There are so much overlapping nodes... You may clean the vertices placement before going any further. Then use either the calc library to shift your edges, or xshift/yshift. – SebGlav Jan 26 '22 at 21:39

1 Answers1

1

[Expanded from my comment below, after some further testing :] It is possible to shift the starting and endpoints using xshift and yshift, but also the north/south/east/west (NSWE) anchors (you are "anchoring" these points to the NSEW sides of the vertices) :

\draw[edge, color=red] ([xshift=1pt]i.south)--([xshift=1pt]h.north);
\draw[edge, color=purple] (i)--(h);
\draw[edge, color=blue] ([xshift=-1pt]i.south)--([xshift=-1pt]h.north);

This above code will shift the red line 1pt to the right and the blue line 1pt to the left.

One downfall of this is that you must probably specify each shifted path separate of the general paths. In the example below, the shifted paths are drawn with a dashed pattern.

Then, I mentioned the positioning tikz library. You could avoid node overlaps with relative positioning; a quick example of the core of the figure :

    \node[vertex] (h) at (0,0) {Node 2h};
    \node[vertex] (i) [above= 3cm of h] {Node 13i};
    \node[vertex] (f) [above left= of i] {node 10f};
    \node[vertex] (j) [above= of i] {Node 14j};
    \node[vertex] (k) [right= of h] {Node 21k};
    \node[vertex] (l) [above right= of h] {Node 1l};
    \node[vertex] (m) [above right= of i] {Node 16m};
    \node[vertex] (n) [right= of k] {Node 3n};
    \node[vertex] (g) [below= of h] {Node 4g};
    \node[vertex] (d) [left=of h] {Node 18d};

%%%%%% Paths are grouped by color %%%%%%%

\draw[edge, color=red] (m) -- (i);
\draw[edge, color=red, dashed] ([xshift=1pt]i.south) -- ([xshift=1pt]h.north);
\draw[edge, color=red, dashed] ([yshift=1pt]h.west) -- ([yshift=1pt]d.east);

\draw[edge, color=purple] (j) -- (i) -- (h) -- (g);

\draw[edge, color=blue] (f) -- (i);
\draw[edge, color=blue, dashed] ([xshift=-1pt]i.south) -- ([xshift=-1pt]h.north);
\draw[edge, color=blue] (h) -- (k) -- (n);

\draw[edge, color=green] (l) -- (h) -- (d);

gives the following figure :

here's an edgy figure!

As one can see, the distance between nodes is pretty much always the same, except for node 13i where it is specified (any unit will do). One can also set this distance for the whole figure by appending [node distance = .5em, auto] after your \begin{tikzpicture}.

I hope this covers pretty much all !

v_manz
  • 305
  • Also one thing I forged is that you can specify the x and y shifts in relative positioning, e.g. below left= 1cm and 3cm of a will position the node 1 cm below and 3 cm to the left of a. – v_manz Jan 26 '22 at 23:06