4

I asked for help to trace this polygon, and someone kindly sent me this code, unfortunately there's still something wrong cause the lines between the nodes are not arrows and I'm absolutely uncapable to understand that code. Is someone can tell me how to replace the lines by arrows as on my picture? Thanks.

\documentclass{article}
\usepackage{tikz}




\usepackage{l3draw}

\ExplSyntaxOn
\NewDocumentCommand { \Polygon } { m m m } {
    \draw_begin:
        \int_step_inline:nn {#1} {
            \draw_path_moveto:n { 
                \draw_point_intersect_line_circle:nnnnn
                    {
                        \draw_point_polar:nn {#2} { 
                            90 - ( ##1 - 1 ) * 360 / #1 
                        }
                    } {
                        \draw_point_polar:nn {#2} { 
                            90 - ##1 * 360 / #1 
                        }
                    } { 
                        \draw_point_polar:nn {#2} { 
                            90 - ( ##1 - 1 ) * 360 / #1 
                        } 
                    } {#3} { 1 }
            } 
            \draw_path_lineto:n { 
                \draw_point_intersect_line_circle:nnnnn
                    {
                        \draw_point_polar:nn {#2} { 
                            90 - ( ##1 - 1 ) * 360 / #1 }
                    } {
                        \draw_point_polar:nn {#2} { 
                            90 - ##1 * 360 / #1 
                        }
                    } { 
                        \draw_point_polar:nn {#2} { 
                            90 - ##1 * 360 / #1 
                        } 
                    } {#3} { 0 }
            } 
            \draw_path_use_clear:n { stroke }
            \draw_path_circle:nn { 
                \draw_point_polar:nn {#2} { 
                    90 - ( ##1 - 1 ) * 360 / #1 
                } 
            } { #3 }
            \draw_path_use_clear:n { stroke }
            \hcoffin_set:Nn \l_tmpa_coffin { \int_to_Alph:n {##1} }
            \draw_scope_begin:
                \draw_transform_shift:n { 
                    \draw_point_polar:nn {#2} { 
                        90 - ( ##1 - 1 ) * 360 / #1 
                    } 
                }
                \draw_coffin_use:Nnn \l_tmpa_coffin { hc } { vc } 
            \draw_scope_end:
        }
    \draw_end:
}
\ExplSyntaxOff

\begin{document}

% Example usage of the \Polygon command
\Polygon{7}{5em}{1em}

\end{document}



enter image description here

3
  • 1
    Welcome! Can you provide a link to the source of the code? (Not if it was emailed or something, obviously.)
    – cfr
    Commented May 23 at 18:45
  • 3
    Hmm ... l3draw is part of the LaTeX 3 team's master plan for world domination, but they are not quite there yet and I don't think they do arrows. At least, searching the documentation for arrow yields no results and neither does skimming the manual. You could, theoretically, create the arrows by drawing very short paths, but I think it would be a whole lot easier to use tikz right now ;). Or picture mode, which has arrows.
    – cfr
    Commented May 23 at 18:56
  • This question can also help.
    – jlab
    Commented May 23 at 20:34

5 Answers 5

4

I not know LaTeX 3 but I think that this could be the solution of your problem done in TikZ.

enter image description here

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[
  every node/.style={circle, draw, minimum size=2mm}, 
  ->, >=Stealth, black, thick 
]
% Define the nodes
\node (A) at (90:2) {A};
\node (B) at (90-360/7:2) {B};
\node (C) at (90-2*360/7:2) {C};
\node (D) at (90-3*360/7:2) {D};
\node (E) at (90-4*360/7:2) {E};
\node (F) at (90-5*360/7:2) {F};
\node (G) at (90-6*360/7:2) {G};

% Draw the arrows with red tips
\draw[->, red] (A) -- (B);
\draw[->, red] (B) -- (C);
\draw[->, red] (C) -- (D);
\draw[->, red] (D) -- (E);
\draw[->, red] (E) -- (F);
\draw[->, red] (F) -- (G);
\draw[->, red] (G) -- (A);
\end{tikzpicture}

\end{document}
8

Here is a possibility with chains.

enter image description here

The usage is

\Polygon{<number of nodes>}{<radius of circle>}{<diameter of individual nodes>}

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{chains, arrows.meta}

\newcommand{\Polygon}[3]{
    \begin{tikzpicture}[start chain=circle placed {at=({90-(\tikzchaincount-1)/#1*360}:#2)}, every join/.style={->}]
        \foreach \i  in {1,...,#1}
            \node [on chain, join, draw, circle, minimum size=#3] {\symbol{\numexpr64+\i}};
        \draw[->] (circle-#1) -- (circle-1);
    \end{tikzpicture}
}

\begin{document}

\Polygon{7}{2cm}{1cm}\qquad
\Polygon{8}{1.5cm}{6mm}

\end{document}
5

Something like this?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\tikzset{% ateb: https://tex.stackexchange.com/a/718690/
  my polygon/.code={%
    \pgfkeys{/my polygon,#1}%
  },
  /my polygon/.search also={/tikz,/pgf},
  /my polygon/.cd,
  sides/.initial=7,
  dist/.initial=20mm,
}
\NewDocumentCommand \mypolygon { O {} }
{%
  \begin{tikzpicture}
    [/my polygon/.cd,#1
    ]
    \foreach \i [evaluate=\i as \iangle using (90-((\i-1)*(360/\pgfkeysvalueof{/my polygon/sides})))] in {1,...,\pgfkeysvalueof{/my polygon/sides}} \node [circle,draw] (p-\i) at (\iangle:\pgfkeysvalueof{/my polygon/dist}) {\setcounter{polygonnode}{\i}\Alph{polygonnode}};
    \foreach \i [remember=\i as \ilast (initially \pgfkeysvalueof{/my polygon/sides})] in {1,2,...,\pgfkeysvalueof{/my polygon/sides}} \draw [-Latex] (p-\ilast) -- (p-\i);
  \end{tikzpicture}%
}
\newcounter{polygonnode}
\begin{document}

\mypolygon

\mypolygon[sides=6,dist=15mm,draw=red]


\end{document}

effect of \mypolygon with and without options

EDIT

Fancier version:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\makeatletter
\tikzset{% ateb: https://tex.stackexchange.com/a/718690/
  my polygon/.code={%
    \pgfkeys{/my polygon,#1}%
  },
  /my polygon/.search also={/tikz,/pgf},
  /my polygon/.cd,
  sides/.initial=7,
  dist/.initial=20mm,
  @nodes/.style={%
    draw,
    circle,
  },
  @lines/.style={%
    draw,
    -Latex,
  },
  nodes/.code={\pgfkeys{/my polygon/@nodes/.style={#1}}},
  lines/.code={\pgfkeys{/my polygon/@lines/.style={#1}}},
  nodes+/.code={\pgfkeys{/my polygon/@nodes/.append style={#1}}},
  lines+/.code={\pgfkeys{/my polygon/@lines/.append style={#1}}},
}
\NewDocumentCommand \mypolygon { s O {} }
{%
  \IfBooleanTF {#1}
  {%
    \def\mypolygon@tempa{scope}%
  }{%
    \def\mypolygon@tempa{tikzpicture}%
  }%
  \begin{\mypolygon@tempa}[/my polygon/.cd,#2]\my@polygon\end{\mypolygon@tempa}%
}
\newcommand \my@polygon {%
  \foreach \i [evaluate=\i as \iangle using (90-((\i-1)*(360/\pgfkeysvalueof{/my polygon/sides})))] in {1,...,\pgfkeysvalueof{/my polygon/sides}} \node [/my polygon/@nodes] (p-\i) at (\iangle:\pgfkeysvalueof{/my polygon/dist}) {\setcounter{polygonnode}{\i}\Alph{polygonnode}};
  \foreach \i [remember=\i as \ilast (initially \pgfkeysvalueof{/my polygon/sides})] in {1,2,...,\pgfkeysvalueof{/my polygon/sides}} \path [/my polygon/@lines] (p-\ilast) -- (p-\i);
}
\makeatother
\newcounter{polygonnode}
\begin{document}

\mypolygon
\mypolygon[sides=6,dist=15mm,draw=red]

\mypolygon[sides=12,nodes={rectangle,draw=none,fill=blue,text=white,font=\sffamily\bfseries},lines={thick,Stealth-Stealth,draw=blue!50!black,double}]
\mypolygon[sides=5,dist=1.5,nodes+={text=red,font=\sffamily},lines+={Stealth-Stealth,double}]


\thispagestyle{empty}
\end{document}

fancier result

Syntax

  • \mypolygon[<key-value list>] and \mypolygon*[<key-value list>] takes a single optional argument which should consist of key-value pairs;
  • \mypolygon[<keys-value list>] is for use outside a tikzpicture;
  • \mypolygon*[<key-value list>] is for use inside a tikzpicture;
  • In addition to standard TikZ/PGF keys, the following are available:
    • sides=<integer> determines the number of sides;
    • dist=<dimen> sets the distance from the centre of the polygon to each vertex;
    • nodes=<key-value list> sets the style of the nodes at the vertices;
    • nodes+=<key-value list> adds to the style of the nodes at the vertices, without overwriting the current value;
    • lines=<key-value list> sets the style of the lines drawn between the vertices;
    • lines+=<key-value list> adds to the style of the lines drawn between the vertices.
5

With modulo 7 counter and two loops: first for positioning of nodes, second for drawing lines between them:

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[
C/.style = {circle, draw, minimum width=1em,inner sep = 2pt}
                    ]
\foreach \a [count=\i from 0] in {B, ...,G, A}
{
\pgfmathsetmacro{\k}{int(Mod(\i+1,7))}
\node (n\k) [C] at (90+\k*360/7:2) {\a};
}
\foreach \i in {0,1,...,6}
{
\pgfmathsetmacro{\k}{int(Mod(\i+1,7))}
\draw[->] (n\i) -- (n\k);
}
\end{tikzpicture}
\end{document}

enter image description here

Addendum:
With modulo 7 counter and one loops with drawing node twice. A bit shorter and faster solution. Result is the same as before

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[
C/.style = {circle, draw, minimum width=1.2em, inner sep = 0pt}
                    ]
\foreach \a [count=\i from 0] in {A, B, ...,G}
{
\node (n\i) [C] at (90+\i*360/7:2) {\a};
\pgfmathsetmacro{\k}{int(Mod(\i+1,7))}
\node (n\k) [C] at (90+\k*360/7:2) {\vphantom{\a}};
\draw[->] (n\i) -- (n\k);
}
\end{tikzpicture}
\end{document}
3
  • Neat ;). But why not name them n0 to n6 rather than n1 to n7. That would save you several pgfmath ops. Or you could count from 1 and adjust the maths.
    – cfr
    Commented May 24 at 6:15
  • @cfr, thank you very much. About naming of nodes: I try to change them, but apparently I was going something wrong. I will try again,, ASAP 😊.
    – Zarko
    Commented May 24 at 9:04
  • It was only a minor comment. I just wondered if there was a reason to set it up as you did. I wouldn't worry about changing it.
    – cfr
    Commented May 24 at 14:58
4

With the Graph Drawing Library circular section 33 Graph Drawing Algorithms: Circular Layouts in the pgfmanual.pdf.

See also Qrrbrbirlbel's answer to this post Simple way to make circular arrow arc?

% !TeX TS-program = lualatex
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{
arrows.meta,%for Stealth arrow tips
graphs,%for \graph syntax 
graphdrawing
}
\usegdlibrary{
circular,        % for simple necklace layout
}        

\begin{document}
\tikz [>={Stealth[round,sep]}]
\graph [
    simple necklace layout,
    node distance=0cm,
    radius=3cm,
    nodes={circle,draw},
    grow'=down,% first node is at the top, ' = clockwise
    ]
{ A->B->C->D->E->F->G->A };
\end{document}

enter image description here

You must log in to answer this question.

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