2

My goal is to draw the following graph: enter image description here I tried to write the code with the package TikZ, but the final graph was as follows: enter image description here Its code is:

\documentclass{standalone}

\usepackage{tikz}

\usetikzlibrary{decorations.pathreplacing,decorations.markings}
\usetikzlibrary{arrows,automata}
\usetikzlibrary{calc}

\tikzset{dot/.style = 
{circle,
draw=blue,
line width=.5pt},
dot/.default = 4pt
}

\tikzset{
  % style to apply some styles to each segment of a path
  on each segment/.style={
    decorate,
    decoration={
      show path construction,
      moveto code={},
      lineto code={
        \path [#1]
        (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
      },
      curveto code={
        \path [#1] (\tikzinputsegmentfirst)
        .. controls
        (\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb)
        ..
        (\tikzinputsegmentlast);
      },
      closepath code={
        \path [#1]
        (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
      },
    },
  },
  % style to add an arrow in the middle of a path
  mid arrow/.style={postaction={decorate,decoration={
        markings,
        mark=at position .5 with {\arrow[#1]{stealth}}
      }}},
}

\begin{document}


\begin{tikzpicture}
  % Axes
%\draw[help lines, color=gray!30, dashed] (-7, -7) grid (7, 7);
%\draw[->,ultra thick] (-7,0)--(7,0) node[right]{$x$};
%\draw[->,ultra thick] (0, -7)--(0, 7) node[above]{$y$};

  % First, define nodes
  \draw (-5, 0) node[
                            circle,
                            inner sep=0pt,
                            fill=black,
                            label={left:{$A$}}
                            ] (A) {};  
  \draw (-2, 2) node[
                            circle,
                            inner sep=0pt,
                            fill=black,
                            label={above:{$B$}}
                            ] (B) {};  
  \draw (2, 2) node (C) {$C$}; 
  \draw (5, 0) node (D) {$D$}; 
  \draw (2, -2) node (E) {$E$}; 
  \draw (-2, -2) node (F) {$F$};  

  % Draw curved path
  \path [thick, draw=black, postaction={very thick, on each segment={mid arrow=black}}]
  (A) to [bend left] (B)
  (A) to [bend right] (B)
  (B) to [bend left] (C)
  (B) to [bend right] (C)
  (C) to [bend left] (D)
  (C) to [bend right] (D)
  (E) to [bend left] (D)
  (E) to [bend right] (D)
  (E) to (D)
  (F) to [bend left] (E)
  (F) to [bend right] (E)
  (F) to (E)
  (A) to [bend left] (F)
  (A) to [bend right] (F)
  (A) to (F);
\end{tikzpicture}

\end{document}

How to change it to draw the figure above?

1
  • 2
    Why are you using a coordinate for A and B (which you label with a node) and a node for the rest? That is (partly) the reason why your figure looks different than your intended picture. Dec 9, 2022 at 8:14

2 Answers 2

4

Really no reason to overcomplicate things.

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{decorations.markings, arrows.meta}
\tikzset{
mid arrow/.style={postaction={decorate, decoration={
markings,
mark=at position .5 with {\arrow{Straight Barb}}
}}},
}

\begin{document}
\begin{tikzpicture}
\node[inner sep=2pt, circle] (A) at (-5,0) {$A$};
\node[inner sep=2pt, circle] (B) at (-2,2) {$B$};
\node[inner sep=2pt, circle] (C) at (2,2) {$C$}; 
\node[inner sep=2pt, circle] (D) at (5,0) {$D$}; 
\node[inner sep=2pt, circle] (E) at (2,-2) {$E$}; 
\node[inner sep=2pt, circle] (F) at (-2,-2) {$F$};  

\draw[thick, mid arrow] (A.north east) to[bend left] (B.south west);
\draw[thick, mid arrow] (A.north east) to[bend right] (B.south west);
\draw[thick, mid arrow] (B.east) to[bend left] (C.west);
\draw[thick, mid arrow] (B.east) to[bend right] (C.west);
\draw[thick, mid arrow] (C.east) to[bend left] (D.north west);
\draw[thick, mid arrow] (C.east) to[bend right] (D.north west);
\draw[thick, mid arrow] (A.south east) to[bend left] (F.north west);
\draw[thick, mid arrow] (A.south east) to[bend right] (F.north west);
\draw[thick, mid arrow] (A.south east) to (F.north west);
\draw[thick, mid arrow] (F.east) to[bend left] (E.west);
\draw[thick, mid arrow] (F.east) to[bend right] (E.west);
\draw[thick, mid arrow] (F.east) to (E.west);
\draw[thick, mid arrow] (E.east) to[bend left] (D.south west);
\draw[thick, mid arrow] (E.east) to[bend right] (D.south west);
\draw[thick, mid arrow] (E.east) to (D.south west);

\end{tikzpicture}
\end{document}

Nodes in a circle with bend arrows

4

Just name the nodes with the letters inside them and don't use coordinates with labels, then you can use each node (with its name) as part of a connection (A) -- (B) and TikZ finds the points on their borders on its own. It does the same for bend left and bend right but uses other points on the border than the straight part.

But with a normal (undrawn) -- path you can find easily the points on the nodes' borders and save them. (We could also calculate angles or just specify the anchors manually, of course).

I give the first node an alias G so that it can simply be accessed in a loop.

The LaTeX function \@Alph helps to turn a number (\cnt) into a letter. We could also use numbers instead of letters to name the nodes which would make this a bit easier. For more than 26 nodes, you will need another way to name your nodes.

I'm using the uptodate arrows.meta library and its Stealth arrow which doesn't need additional scaling. If you want another arrow you can choose the one you want, the library offers plenty tip styles.

Code

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{arrows.meta}
\tikzset{% style to add an arrow in the middle of a path
  mid arrow/.style={postaction={decorate,decoration={
        markings,
        mark=at position .5 with {\arrow[#1]{Stealth}}}}}}
\makeatletter
\newcommand*\intToChar[1]{\@Alph{#1}}
\makeatother
\begin{document}

\begin{tikzpicture}
% Axes
%\draw[help lines, color=gray!30, dashed] (-7, -7) grid (7, 7);
%\draw[->,ultra thick] (-7,0)--(7,0) node[right]{$x$};
%\draw[->,ultra thick] (0, -7)--(0, 7) node[above]{$y$};

% First define nodes
\foreach[count=\cnt] \pnt/\lab in {(-5, 0)/A,
                                   (-2, 2)/B,
                                   ( 2, 2)/C,
                                   ( 5, 0)/D,
                                   ( 2,-2)/E,
                                   (-2,-2)/F}
  \node (\lab) at \pnt {$\lab$};
\path node also [alias=G] (A);

% Find points on their border to the other nodes
\foreach \lab[count=\cnt from 2] in {A, ..., F}
  \path (\lab) -- coordinate[at start] (\lab-to-\intToChar{\cnt})
                  coordinate[at end]   (\intToChar{\cnt}-to-\lab)
                  (\intToChar{\cnt});

% Draw the lines and curves
\path [thick, every edge/.append style={mid arrow}]
  foreach \A/\B in {A/B, B/C, C/D, G/F, F/E, E/D} {
   (\A-to-\B) edge [bend left] (\B-to-\A)
              edge [bend right] (\B-to-\A)
  }
  foreach \A/\B in {G/F, F/E, E/D} {
    (\A) edge (\B)
  }
  ;
\end{tikzpicture}
\end{document}

Output

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .