2

Good Afternoon, I would like to know how my Professor did to draw these six polygons in two photos below. 4 polygons 2 exagons

I state that I use \usepackage{stix} and I would like to learn tikz-package.

Thank you very much

2
  • Hi, welcome to TeX.SE! In order to learn how your professor did it you'll have to ask them, but there are several packages that'll make creating such illustrations relatively easy. TikZ, which you mention, is perhaps the biggest and most well-known one. Have you looked at the tutorials contained in its manual? If not, I recommend doing so, and then simply jumping in, trying to create your own TikZ drawings, and asking questions here when you're stuck and Google doesn't turn up an answer. All the best!
    – chsk
    Jun 27, 2021 at 20:28
  • @Puck And feel free to upvote and accept the answer that fits your needs. It helps members to know that you found what you were looking for.
    – SebGlav
    Jun 27, 2021 at 21:25

2 Answers 2

6

enter image description here

Here's a simple way to do it in tikz with some loops and manually connecting the nodes.

\documentclass[tikz, border=20]{standalone}
\begin{document}
    \begin{tikzpicture}
        % 2 nodes
        \draw (0, 0) circle [radius=2];
        \foreach \i in {0, 1} {
            \coordinate (node\i) at (360/2*\i+90:2);
            \draw (node\i) circle [radius=0.1];
        }
        \draw (node0) -- (node1);
        
        \begin{scope}[xshift=5cm]
            % 3 nodes
            \draw (0, 0) circle [radius=2];
            \foreach \i in {0, 1, 2} {
                \coordinate (node\i) at (360/3*\i+90:2);
                \draw (node\i) circle [radius=0.1];
            }
            \draw (node0) -- (node1);
            \draw (node1) -- (node2);
            \draw (node2) -- (node0);
        \end{scope}
        
        \begin{scope}[xshift=10cm]
            % 4 nodes
            \draw (0, 0) circle [radius=2];
            \foreach \i in {0, 1, 2, 3} {
                \coordinate (node\i) at (360/4*\i+90:2);
                \draw (node\i) circle [radius=0.1];
            }
            \draw (node0) -- (node1);
            \draw (node1) -- (node2);
            \draw (node2) -- (node3);
            \draw (node3) -- (node0);
            \draw (node0) -- (node2);
            \draw (node1) -- (node3);
        \end{scope}
        
        \begin{scope}[yshift=-5cm]
            % 5 nodes
            \draw (0, 0) circle [radius=2];
            \foreach \i in {0, 1, 2, 3, 4} {
                \coordinate (node\i) at (360/5*\i+90:2);
                \draw (node\i) circle [radius=0.1];
            }
            \draw (node0) -- (node1);
            \draw (node1) -- (node2);
            \draw (node2) -- (node3);
            \draw (node3) -- (node4);
            \draw (node4) -- (node0);
            \draw (node0) -- (node2);
            \draw (node0) -- (node3);
            \draw (node1) -- (node3);
            \draw (node1) -- (node4);
            \draw (node2) -- (node4);
        \end{scope}
        
        \begin{scope}[xshift=5cm, yshift=-5cm]
            % 6 nodes
            \draw (0, 0) circle [radius=2];
            \foreach \i in {0, 1, 2, 3, 4, 5} {
                \coordinate (node\i) at (360/6*\i+90:2);
                \draw (node\i) circle [radius=0.1];
                % \node at (node\i) {\Huge\color{red}\i};
            }
            \draw (node0) -- (node1);
            \draw (node1) -- (node2);
            \draw (node2) -- (node3);
            \draw (node3) -- (node4);
            \draw (node4) -- (node5);
            \draw (node5) -- (node0);
            \draw (node0) -- (node2);
            \draw (node0) -- (node3);
            \draw (node0) -- (node4);
            \draw (node1) -- (node3);
            \draw (node1) -- (node4);
            \draw (node1) -- (node5);
            \draw (node2) -- (node4);
            \draw (node2) -- (node5);
            \draw (node3) -- (node5);
        \end{scope}
        
        \begin{scope}[xshift=10cm, yshift=-5cm]
            % 6 nodes (again)
            \draw (0, 0) circle [radius=2];
            \foreach \i in {0, 1, 2, 3, 4, 5} {
                \coordinate (node\i) at (360/6*\i+90+rand*20:2);
            }
            \draw (node0) -- (node1);
            \draw (node1) -- (node2);
            \draw (node2) -- (node3);
            \draw (node3) -- (node4);
            \draw (node4) -- (node5);
            \draw (node5) -- (node0);
            \draw (node0) -- (node2);
            \draw (node0) -- (node3);
            \draw (node0) -- (node4);
            \draw (node1) -- (node3);
            \draw (node1) -- (node4);
            \draw (node1) -- (node5);
            \draw (node2) -- (node4);
            \draw (node2) -- (node5);
            \draw (node3) -- (node5);
        \end{scope}
    \end{tikzpicture}
\end{document}

Explanation

The key elements being used here are

\foreach \i in {0, 1, 2} {
    <do something with \i>
}

This is a simple loop in tikz. The code <do something with \i> is executed three times with \i taking the values 0, 1, and 2 in that order. The code used in this case is

\coordinate (node\i) at (360/2*\i+90:2);
\draw (node\i) circle [radius=0.1];

which places a coordinate marker at a position and names it node\i (with the current value of \i being substituted). The position is given in polar coordinates (using degrees), the general syntax for polar coordinates in tikz is (a:r) where r is the radius and a is the angle (measured above the horizontal axis, hence the constant offset of + 90 to measure from the top of the circle). The second line here draws a small circle at the coordinates position.

After the loop simply connect all of the nodes that you wish to be connected.

The final irregular drawing is done by adding a random offset to each angle, which can be done using rand which is then scaled by a factor of 20 to get a noticeable effect.

Each drawing (apart from the first) is placed in a scope environment and every element of this scope has a xshift and/or yshift applied. This moves the origin by the specified amount (you must put units here or it defaults to pt)

Hints

tikz loops can parse things like

\foreach \i in {0, ..., 5} {}

which will execute 6 times for \i taking the values 0, 1, 2, 3, 4, and 5. Or you can use

\foreach \i in {0, 0.5, ..., 2} {}

which will execute 5 times for \i taking the values 0, 0.5, 1, 1.5, and 2.

Don't be afraid to draw things that won't be in the final picture, for example when connecting the nodes I had

\node at (node\i) {\Huge \i};

within the loop which numbers each node so I can see easily what I need to connect.

Finally the best way to learn tikz is to just keep using it and look things up as and when you need them. Have fun

6
  • +1 Nice that you took time to explain the process. And for the last one, a good idea to randomize a bit. But connecting manually is such a pain when you can do it with loops.
    – SebGlav
    Jun 27, 2021 at 14:07
  • 1
    Thanks, I agree about the manual connections being a pain but for a small number of points its not too bad, hopefully anyone who needs can combine our two answers to produce their desired output without having to do that.
    – Willoughby
    Jun 27, 2021 at 14:10
  • Thank you very much
    – Puck
    Jun 27, 2021 at 16:01
  • @Willoughby You can fatorize your code by using foreach on couples suchas to draw the segments.
    – projetmbc
    Jun 27, 2021 at 18:28
  • I forgot to say that I use stix and I use \documentclass{book}. Do these tips also apply to tex-codes with book and stix? Thanks again
    – Puck
    Jun 28, 2021 at 6:55
7

I think that you could find more than an answer by searching on the site, but here's a way to draw whatever complete graph you need (designed as a regular polygon).

\documentclass[tikz,border=3.14mm]{standalone}
          
\begin{document}
    \begin{tikzpicture}
        \def\R{3} \def\N{5}
        \draw (0,0) circle(\R);
        \foreach \i in {1,...,\N} 
            {
            \coordinate (P-\i) at (\i*360/\N:\R);
            \draw (P-\i) circle(5pt);
            }
        \pgfmathtruncatemacro\n{\N-1}
        \foreach \i in {1,...,\n}
            {
            \pgfmathtruncatemacro\j{\i+1}
            \foreach \k in {\j,...,\N} \draw (P-\i) -- (P-\k);
            }
            
    \end{tikzpicture}       
\end{document}

Result for N=5:
K5

Result for N=8:
K8

Result for N=19:
K19

5
  • It would be very cool to explain a little the use of \pgfmathtruncatemacro.
    – projetmbc
    Jun 27, 2021 at 18:30
  • @projetmbc I thought it was really self explanatory in this case.
    – SebGlav
    Jun 27, 2021 at 18:48
  • 1
    You are right. Sorry for my excessive wish for pedagogy. :-)
    – projetmbc
    Jun 27, 2021 at 18:53
  • @projetmbc Honestly, as someone who's starting out with Latex and tikz, I would like to know. Especally considering that Googling it leads me to random websites completely unrelated to the command.
    – Ciprum
    Sep 18, 2021 at 14:47
  • \pgfmathtruncatemacro computes the formula inside the curly brackets and returns the truncated value of the result, i.e. always an integer. On the contrary, \pgfmathsetmacro returns a float, which is not suitable for, say, naming nodes.
    – SebGlav
    Sep 18, 2021 at 14:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.