6

enter image description here

About this, I'd like to numerate vertices of pentagon with a cyclic tikz function (e.g. \pgfmathtruncatemacro). How is it possible? Thank you!

The code

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\usetikzlibrary{shapes.geometric}

\usepackage{pst-node,pst-plot}

\title{Polygon with numbered vertices}
\author{PUCK}
\date{\today}


\begin{document}

\maketitle

\section{Introduction}

\begin{center}
\def\R{2} \def\N{5}
\begin{tikzpicture}[baseline ={(0,0)}]
\foreach \i in {1,...,\N} { \coordinate (P-\i) at (\i*360/\N+90/\N:\R); }
\pgfmathtruncatemacro\n{\N-1} 
\foreach \i in {1,...,\n}
   {
      \pgfmathtruncatemacro\j{\i+1}       
      \draw (P-\i) -- (P-\j) ;
   } 
\draw (P-\N) -- (P-1) ;
\draw (0,0) node {\Huge{$P_n$}} ;
\draw (0,0) circle (2cm) ;
\draw[->] (-2.5,0) -- (3.5,0) coordinate (x axis);
\draw (2.75,0) circle (0pt) node[anchor=north] {asse $x$};
\draw[->] (0,-2.5) -- (0,2.5) coordinate (y axis);
\draw (0,2.4) circle (0pt) node[anchor=north west] {asse $y$};
\end{tikzpicture}
\end{center}

\end{document}
5

Simplest way I can think off is to declare another coordianate for the (P-\i') at which to place the label, where we tweak the label for point 2:

\foreach \i in {0,...,\N} { 
    \coordinate (P-\i) at (\i*360/\N+90/\N:\R); 
    \ifnum\i=1
        \def\LabeAngle{105}
    \fi
    \coordinate (P-\i') at (\i*360/\N+\LabeAngle/\N:1.1*\R); 
}

and then place the label via

\coordinate (P-\i') at (\i*360/\N+\LabeAngle/\N:1.1*\R)

A better solution is to use the regular polygon from the shapes.geometric:

\node [
    regular polygon, 
    regular polygon sides=5, 
    minimum size=2*\R cm, 
    draw=black, fill=gray!25, thick
] 
at (0,0) {};

This is the code below, with the commented portions representing the first version.

enter image description here

References:

Code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\usetikzlibrary{shapes.geometric}

\usepackage{pst-node,pst-plot}

\title{Polygon with numbered vertices}
\author{PUCK}
\date{\today}
\pagecolor{white}

\begin{document}

\maketitle

\section{Introduction}

\begin{center}
\def\R{2} \def\N{5}
\def\LabeAngle{90}
\begin{tikzpicture}[baseline ={(0,0)}]
\foreach \i in {0,...,\N} { 
    \coordinate (P-\i) at (\i*360/\N+90/\N:\R); 
    \ifnum\i=1
        \def\LabeAngle{105}
    \fi
    \coordinate (P-\i') at (\i*360/\N+\LabeAngle/\N:1.1*\R); 
}
\pgfmathtruncatemacro\n{\N-1} 
\node [regular polygon, regular polygon sides=5, minimum size=2*\R cm, draw=black, fill=gray!25, thick] at (0,0) {};
\foreach \i in {0,...,\n}
   {
      %\pgfmathtruncatemacro\j{\i+1}       
      %\draw (P-\i) -- (P-\j) ;
      \pgfmathtruncatemacro\NodeLabel{1+\i}
      \node at  (P-\i') {$\NodeLabel$};
   } 
%\draw (P-\N) -- (P-1) ;
\draw (0,0) node {\Huge{$P_n$}} ;
\draw (0,0) circle (2cm) ;
\draw[->] (-2.5,0) -- (3.5,0) coordinate (x axis);
\draw (2.75,0) circle (0pt) node[anchor=north] {asse $x$};
\draw[->] (0,-2.5) -- (0,2.5) coordinate (y axis);
\draw (0,2.4) circle (0pt) node[anchor=north west] {asse $y$};
\end{tikzpicture}
\end{center}
\end{document}
3
  • Thank you very much!!! If I wanted to color the pentagon gray what can I do? (Or do I need to create a new post?)
    – Puck
    Oct 20 at 17:15
  • 1
    In that case I would recommend using the regular polygon from the shapes.geometric library. Have updated solution. Oct 20 at 17:28
  • Thank you very very much
    – Puck
    Oct 20 at 22:39
6

For comparison, here is a way of doing that in Metapost.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
input colorbrewer-rgb
numeric r; r = 42;
path xx, yy, C;
xx = (left--right) scaled 3/2 r;
yy = xx rotated 90;
C = fullcircle scaled 2r;
ahangle := 25;
beginfig(1);
for n=3 upto 8:
    path poly; poly = for i=0 upto n-1: r * up rotated (360/n*(i-1)) -- endfor cycle;
    picture P; P = image(
        fill poly withcolor PastelOne[9][n];
        drawarrow xx withcolor 3/4;
        drawarrow yy withcolor 3/4;
        draw C withcolor 3/4;
        draw poly;
        label("$P_{" & decimal n & "}$", origin);
        % this scaling trick works because the poly is centered at (0,0)
        for i=1 upto n:
            label("$" & decimal i & "$", point i of poly scaled (1+6/r));
        endfor
    );
    draw P shifted ((n mod 3, -floor(n/3)) scaled 3.5r);
endfor
endfig;
\end{mplibcode}
\end{document}

You need to compile this with lualatex so that the luamplib wrapper works.

2
  • Very nice truly. I like very much.
    – Sebastiano
    Oct 22 at 21:06
  • I very like this!! Thank you very much!!
    – Puck
    Oct 23 at 12:43

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.