2

I am new using Tikz, and I am trying to know how to draw graphs, my try is:

\documentclass[11pt]{article}

\usepackage{tikz}


\begin{document}

\begin{tikzpicture}[y=.4cm, x=.4cm,font=\normalsize]
\draw (0,5)  --  (1,2);
\draw (1,2)  --  (2.3,0.42) ;
\draw (5,0) --  (2.3,0.42) ;
\draw (9,2)  --  (10,5);
\draw (7.3,9.4)  --  (5,10);
\draw (9,8)  --  (7.3,9.4);
\draw (10,5)  --  (9,8);
\draw (5,10)  --  (2.3,9.4);
\draw (0,5)  --  (0.7,7);
\draw (0.9,7.7)  --  (2.3,9.5);
\draw (5,0)  --  (7.3,0.42);
\draw (7.3,0.42)  --  (9,2);
\filldraw[fill=black!40,draw=black!80] (0,5) circle (2pt) node[anchor=east] {10};
\filldraw[fill=black!40,draw=black!80] (5,0) circle (2pt) node[anchor=north] {7};
\filldraw[fill=black!40,draw=black!80] (5,10) circle (2pt) node[anchor=south] {1};
\filldraw[fill=black!40,draw=black!80] (10,5) circle (2pt)node[anchor=west] {4};
\filldraw[fill=black!40,draw=black!80] (9,2) circle (2pt) node[anchor=west] {5};
\filldraw[fill=black!40,draw=black!80] (9,8) circle (2pt) node[anchor=west] {3};
\filldraw[fill=black!40,draw=black!80] (1,2) circle (2pt) node[anchor=east] {9};
\filldraw[fill=black!40,draw=black!80] (7.3,0.42) circle (2pt) node[anchor=west] {6};
\filldraw[fill=black!40,draw=black!80] (7.3,9.4) circle (2pt) node[anchor=west] {2};
\filldraw[fill=black!40,draw=black!80] (2.3,9.4) circle (2pt) node[anchor=south] {n};
\filldraw[fill=black!40,draw=black!80] (2.3,0.42) circle (2pt) node[anchor=north] {8};
\filldraw[fill=white!40,draw=white!80] (1.7,7.5) circle (2pt) node[anchor=east] {\dots};

\end{tikzpicture}

\end{document}

and it gives me something similar to what I want, but I only need that the graph is directed but when I put something like:

\draw \[->\](7.3,9.4)  --  (5,10);

It allways give errors. I have tryied to install other packages and user other compilators (TeXworks,TeXmaker and Overleaf) but I haven't managed to fix this. Please help.

  • Welcome to TeX.SE! The errors may come from \[...\] instead of [...], i.e. try \draw \[->\](7.3,9.4) -- (5,10);. Yet you can simplify the graph a lot. – user121799 Feb 7 '19 at 2:44
  • Yes! I know it can be simplified a lot, I am new at this and trying to learn. Actually it worked with \draw [ latex-] – J.Rodriguez Feb 7 '19 at 2:48
4

Here is a proposal to simplify things.

  1. A \foreach loop helps to avoid repetition.
  2. Then polar coordinates are used. In (120:5) 120 denotes the angle and 5 the radius. In the loop, the angles are {120-\X*360/12}, i.e. clockwise in steps of 360/12=30 degrees.
  3. The labels are attached at the same angles, label={120-\X*360/12}: makes sure that they are "outside".

One may condense the code further but then it might become even harder to understand.

\documentclass[11pt]{article}

\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[y=.4cm, x=.4cm,font=\normalsize,>=latex]
\foreach \X [remember=\X as \LastX] in {0,...,10}
 {\ifnum\X=0
  \node [circle,fill=black!40,label=120:$n$,inner sep=2pt](X\X) at (120:5){};
 \else
  \node [circle,fill=black!40,label={120-\X*360/12}:$\X$,inner sep=2pt](X\X) at
  ({120-\X*360/12}:5){};
  \draw[black!80,->]  (X\LastX) -- (X\X);
 \fi}
 \node[rotate={30+360/12}]  (X11) at ({120+360/12}:5) {$\cdots$};
 \draw[black!80,->]  (X10) -- (X11);
 \draw[black!80,->]  (X11) -- (X0);
\end{tikzpicture}
\end{document}

enter image description here

  • 1
    WOW, it is amazing, thanks a lot!!! I wish I could understand everything there, but I will try. Thanks – J.Rodriguez Feb 7 '19 at 2:59
  • 1
    @J.Rodriguez You're welcome! This site is full of examples. I added some explanations for what I did. – user121799 Feb 7 '19 at 3:05

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.