In the following answer to draw a graph, is there a way to use (x,y) coordinate rather than angle to determine the position of the vertices to draw the same graph?


What I expect is to draw a graph similar to the following image:

enter image description here

Edite: Compilation errors:

I use the following document class and the compile is done by Overleaf. The screenshot shows the received errors after compilation:

enter image description here

As you can see the begin document is inserted after:

enter image description here

  • You can convert from polar to rectangular coordinates, but what would the advantage be?
    – Sandy G
    Aug 12, 2022 at 17:03
  • @Sandy G , Assume we want to put a vertex close to (and not exactly) the center of the Hexagon, how to do it?
    – Questioner
    Aug 12, 2022 at 17:05
  • @Sandy G , I edited the question by adding an image.
    – Questioner
    Aug 12, 2022 at 17:11

2 Answers 2


The answer by Sandy G already answers your initial question but I want to give another way to draw ths picture with TikZ. The key decide color might be usable with that other answer, too, since you won't need to specify the color implicitly in the list.

With the support of LuaLaTeX we can use TikZ' graphdrawing and its force library.

The force library (\usegdlibrary{force}) provides the spring electrical layout.

The graphs library (\usetikzlibrary{graphs}) provides the typeset key that is used to define the node text dependent on the node's name.

This name will also be used in decide color to apply the appropriate color style:

decide color/.style 2 args={
  /utils/TeX if=c#1
    {% if the first letter is c
     % and the number after is is smaller than 5 → bluelight
     %                                 otherwise → bluedark
    }{% if the first letter is not c (i.e. p)
      % and the number after it is smaller than 8 → light
      %                                 otherwuse → dark


% arara: lualatex
  /utils/if/.code n args={3}{%
  \pgfmathparse{#1}\ifdim\pgfmathresult pt=0pt\relax
  /utils/TeX if/.code n args={4}{%
    circle, minimum size=10mm, draw, densely dashdotted, thick,
    decide color/.expand once=#1},
  decide color/.style 2 args={
    /utils/TeX if=c#1% c1-c4 / c5- / ?1-?7 / ?8-
  light/.style={fill=gray!20},  bluelight/.style={fill=blue!10},
  dark/.style ={fill=gray!60},  bluedark/.style ={fill=blue!30}}
  spring electrical layout, horizontal=c2 to p13,
  node distance=1.5cm, typeset=$n_{\tikzgraphnodetext}$,
  nodes={mynode=\tikzgraphnodetext}] {
  % outer ring
  c2 -- {p1, p11, p6};
    p1 -- {p8, c6, p11};
      p8 -- {p3, p10, c6};
       p3 -- {p13, p15, p10};
         p13 -- {p15, c7};
           c7  -- {c3, c4, p15};
           c3  -- {p14, c4};
           p14 -- {p7, c4};
         p7 -- {p9, p2, c4};
       p9 -- {c5, p12, p2};
     c5 -- {c1, p4, p12};
   c1 -- {p6, p4};
  p6 -- {p11, p4};
  % inner ring
  p11 -- {c6, p12, p4};
  p5 -- {c6 -- {p10, p12}, p10 -- p15, p15 -- c4, c4 -- p2, p2 -- p12, p12 -- p4};
  % last line is equivalent to:
  % c6 -- {p10, p5, p12}; p10 -- {p15, p5}; p15 -- {c4, p5};
  % c4 -- {p2, p5};       p2  -- {p12, p5}; p12 -- {p4, p5};


enter image description here

  • Thanks. Since I received errors after compilation, I added related screenshots to the question.
    – Questioner
    Aug 12, 2022 at 23:57
  • And how to use LuaLaTeX ? (I am using Overleaf online compiler)
    – Questioner
    Aug 13, 2022 at 0:26
  • Done, but i don't know what's the conflict with algorithm2e, because I get now this error: \usepackage [ruled]{algorithm2e} The package xcolor has already been loaded with options: [] There has now been an attempt to load it with options [dvipsnames] Adding the global options: ,dvipsnames to your \documentclass declaration may fix this. Try typing <return> to proceed.
    – Questioner
    Aug 13, 2022 at 1:58
  • 1
    @Questioner That's the same problem as recently. Has nothing to do with this answer. Easiest would be to move \usepackage[rules]{algorithm2e} before \usepackage{tikz}. Aug 13, 2022 at 2:17
  • 1
    @Questioner decide color/.style 2 args={graphs/typeset={$#1_{#2}$}, /utils/TeX if=…} should work. Feb 21, 2023 at 23:16

This should get you started. Make two for loops: the first to name and draw the nodes, the second to draw the edges. Each entry in the node list should have the form <x>/<y>/<fillcolor>/<label>/<name>. For the edge list, use the names from the first for loop.

enter image description here


\tikzset{mynode/.style={circle, minimum size=12mm, draw, densely dashdotted}}




\foreach \x/\y/\col/\lab/\nam in 
    {0/0/color1/n_{p1}/p1, -1/3/color2/n_{p2}/p2, 1/5/color1/n_{p5}/p5, 4/4/color2/n_{c5}/c5, 5/1/color3/n_{c1}/c1, 3/0/color3/n_{p11}/p11, 2/2/color1/n_{c2}/c2}
    {\node[mynode, fill=\col] (\nam) at (\x,\y){$\lab$};}
\foreach \p/\q in {p5/c5, c5/c1, c1/p11, p11/p1, p1/p2, p2/p5, c2/c5, c2/p11, c2/c1, c2/p1, c2/p2, c2/p5}
    {\draw (\p)--(\q);}

  • PGFfor allows the first variable in the list to be a coordinate (even if there's a , in it): \foreach \pos/\col/\lab/\nam in {(0,0)/color1/n_{p1}/p1, …, (2,2)/color1/n_{c2}/c2} \node[mynode, fill=\col] (\nam) at \pos {$\lab$}; which might be a more natural way to specify the coordinates. And it allows (60:2) or even ([xshift=5pt]0,0) without relying solely on \x and \y coordinates. Aug 12, 2022 at 19:46
  • @Qrrbrbirlbel: That's a good point. I will let OP decide which to use.
    – Sandy G
    Aug 12, 2022 at 20:00

You must log in to answer this question.

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