1

Using the graphdrawing library for TikZ, is it possible to draw the following graph

with this formatting?

I have used the following code to draw it "manually":

\tikz[nodes={circle, draw}]  {
\node (1)  at (0,0) {1};
\node (2)  at (-1.5,-1.5) {2};
\node (5)  at (0, -1.5) {5};
\node (4)  at (1.5, -1.5) {4};
\node (3)  at (0, -3) {3};

\graph {
    (1)--(2)--(3)--(4)--(1); (2)--(5)--(4);
};
}

However, I have to draw a lot of similar graphs, and this becomes very tedious. And it seems to me that force-based layouts in the graphdrawing library must be suited to achieve this particular formatting of the graph (since it is very "balanced").

But I have not had any success using something like

\tikz \graph [spring layout, nodes={circle, draw}, node distance=1.5cm, horizontal=2 to 4]{
1--2--3--4--1; 2--5--4; 
};

and not with spring electrical layout either. I have tried tuning different parameters (see section 32.1 in the TikZ manual), but nothing seems to produce the right result. I can't even prevent the edges from overlapping.

Have I misunderstood what the graphdrawing library should be used for? And is there not any shorter way to draw the graph, in this natural formatting, other than manually defining the positions of the nodes?

2
  • with tkz-graph package -- please see if the example below suits the requirement
    – js bibra
    Oct 15, 2020 at 5:07
  • did the answer suit your requirement
    – js bibra
    Oct 15, 2020 at 11:13

2 Answers 2

1

This may point you in the right direction

with tkz-graph package

enter image description here

\documentclass{standalone}
\usepackage{tkz-graph}

\begin{document}
\begin{tikzpicture}

    \draw[help lines] (0,-2) grid (4,2);
    \SetGraphUnit{2}
    \GraphInit[vstyle=Normal]
    \Vertex{A}
    \EA(A){B} \NO(B){C} \SO(B){D} \EA(B){E}
    \Edges(A,D,E,C,A,B,E)
\end{tikzpicture}
\end{document}
1
  • 1
    Hi, thanks a lot! This seems like a much better solution for my purpose. In general, is it preferred to use the tkz-graph package over the internal graph functionality in TikZ (in particular for drawing graphs in graph theory)? I considered using it from the start, but was a little turned off by the french documentation (as I don't know any french). Also it seemed to me that the algorithmic graph drawing provided by the graphdrawing library was really useful. Edit: Reading the code in your example it seems very simple, so I will start using tkz-graph instead.
    – Laffen
    Oct 15, 2020 at 12:03
3

An alternative with tikz-cd.

Output

enter image description here

Code

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

\usepackage{tikz-cd} % if tikz is already loaded, you can also use \usetikzlibrary{cd}

\tikzcdset{arrows={thick}}

\begin{document}
    \begin{tikzcd}[%
        row sep=1cm, 
        column sep=1cm, 
        cells={nodes={draw, circle, thick}},
    ]
                & 1 \arrow[dl,-] \arrow[dr,-] & \\
        2 \arrow[r,-] \arrow[dr,-]  & 5 \arrow[r,-]   & 4  \arrow[dl,-]\\
                & 3  & 
    \end{tikzcd}
\end{document}
1
  • +1 to have joined the segments to the circles with tikz-cd.
    – Sebastiano
    Oct 15, 2020 at 15:27

You must log in to answer this question.

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