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
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityGood 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
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}
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)
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
foreach
on couples suchas to draw the segments.
Jun 27, 2021 at 18:28
stix
and I use \documentclass{book}
. Do these tips also apply to tex-code
s with book
and stix
? Thanks again
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}
\pgfmathtruncatemacro
.
Jun 27, 2021 at 18:30
\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.