2

I created tikset based on the following example HERE to draw regular polygon. But I've got compilation error and i'm unable to know why :/ .

In addition:

  • the pentagon label doesn't appear, while the others are
  • I've got crashs when I declare the node as in the example

    \draw (0:\R) \foreach \x in {120,240} { -- (\x:\R) } -- cycle (90:\R) node[above] {$n=3$} ;

enter image description here

main.tex

%%%%%%%%%%%%%%%%%% INTRODUCTION %%%%%%%%%%%%%%%%%%
\documentclass[border=10pt]{standalone}

%%%%%%%%%%%%%%%%%% PACKAGE %%%%%%%%%%%%%%%%%%
\usepackage{tikz, tkz-euclide}%  permet de dessiner des figures, des graphiques
\usepackage{adjustbox}% permet de déterminer une taille de fenêtre

%%  FONT
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{tgadventor}% paquet de police de caractère TGadventor
\usepackage{sansmath}%  Copie-colle la police active dans 
%                       \sfdefault (/!\ N'EST PAS UNE POLICE DE CARACTÈRES)

\usepackage{xcolor}
%%%%%%%%%%%%%%%%%% INPUT %%%%%%%%%%%%%%%%%%
%\input{preamble.tex}
%\input{parameters.tex}

%\input{types/f2d_fig}
%%%%%%%%%%%%%%%%%% SETUP %%%%%%%%%%%%%%%%%%
\tikzset{pics/triangloid_regular/.style={code={%
    \tikzset{triangloid_regular/.cd,#1}%
    \def\pv##1{\pgfkeysvalueof{/tikz/triangloid_regular/##1}}%
    \draw[thick, pic actions] (-30:\pv{radius}) \foreach \x in {90,210}%
        {
            -- (\x:\pv{radius})}%
            -- cycle (90:\pv{radius})%
            (0,\pv{radius}) coordinate node[above, scale=2.5] {\pv{lab}}%
            ;
        }
    },
  triangloid_regular/.cd,
  radius/.initial=3cm,
  lab/.initial=,
  ;
}

\tikzset{pics/squaroid_regular/.style={code={%
    \tikzset{squaroid_regular/.cd,#1}%
    \def\pv##1{\pgfkeysvalueof{/tikz/squaroid_regular/##1}}
    \draw[thick, pic actions] (45:\pv{radius}) \foreach \x in {135,225,...,359}
        {
            -- (\x:\pv{radius})}%
            -- cycle (90:\pv{radius})%
            (0,\pv{radius}) coordinate node[above, scale=2.5] {\pv{lab}}
            ;
        }
    },
  squaroid_regular/.cd,
  radius/.initial=3cm,
  lab/.initial=,
  ;
}

\tikzset{pics/pentagoid_regular/.style={code={%
    \tikzset{pentagoid_regular/.cd,#1}%
    \def\pv##1{\pgfkeysvalueof{/tikz/squaroid_regular/##1}}
    \draw[thick, pic actions] (18:\pv{radius}) \foreach \x in {90,162,...,359}
        {
            -- (\x:\pv{radius})}%
            -- cycle (90:\pv{radius})%
            (0,\pv{radius}) coordinate node[above, scale=2.5] {\pv{lab}}
            ;
        }
    },
  pentagoid_regular/.cd,
  radius/.initial=3cm,
  lab/.initial=,
  ;
}
%%%%%%%%%%%%%%%%%% DOCUMENT %%%%%%%%%%%%%%%%%%

% Radius of regular polygons
\newdimen\R
\R=2cm

\begin{document}
\begin{tikzpicture}[font={\sansmath\sffamily},thick, line cap=round, line join=round, >=latex, x=1.0cm, y=1.0cm]

% Indicate the boundary of the regular polygons
\pic {triangloid_regular={lab=Triangle}};
\pic[xshift=7cm] {squaroid_regular={lab=Square}};
\pic[yshift=-7cm] {pentagoid_regular={lab=Pentagone}};
\end{tikzpicture}
\end{document}

I guess it's business as usual, I'm missing a little detail, making me go around in cirle >-< .

  • You have a copy paste error -- you are referring to squaroid in your pentagon definition – Thruston Mar 2 at 11:13
  • thanks, I've just changed it. for the rest, the problem remains – Nilcouv Mar 2 at 11:27
  • Isn't it in practical use of regular polygons simpler to use tikz library shapes.geometric and its shape regular polygon? – Zarko Mar 2 at 12:26
3

It seems much easier to use polar coordinates. This way the code:

  \documentclass{article}

  \usepackage{tikz}
  \usepackage{pgfplots}
  \usepackage{xcolor}

  \tikzset{pics/polygon/.style 2 args={
      code = {
        \foreach \a [evaluate=\a as \an using (270+(\a+0.5)*360/#1)] in {1, ..., #1} {
              \draw(\an-360/#1:1)--(\an:1);
        }
        \node at (90:1.3){#2};
      }
    }
  }

  \begin{document}

    \begin{tikzpicture}
      \pic at (0,0) {polygon=3{Triangle}};
      \pic at (3,0) {polygon=4{Square}};
      \pic at (6,0) {polygon=5{Pentagon}};
      \pic at (9,0) {polygon=6{Hexagon}};
    \end{tikzpicture}

  \end{document}

produces:

enter image description here

The only mildly tricky bit is working out the appropriate angles so that the "bottom" edge is horizontal as in the OP. It would be easy to add other features to control the size of the polygons, their fill etc etc. (The other option is to use shape=regular polygon from \usetikzlibrary{decorations.shapes}; see section 51.5.3 of the tikz manual.)

Edit

As for the code in the OP, the problem seems to be that you are referring to triangloid_regular etc before they are defined. If you put the initial definitions a little higher up then the code compiles. The "Pentagone" (Pentagon?) did not appear because you have squareoid, instead of pentagoid, in the \pv##1{\pgfkeysvalueof{...}} statement. A third, extremely minor point, is that I would not use names with an underscore as TeX tends not to like them. In this case they work, of course, so I am just being precious and you are more than welcome to ignore me:).

Here is the updated code from the OP with these changes, which now compiles.

\documentclass[border=10pt]{standalone}

%%%%%%%%%%%%%%%%%% PACKAGE %%%%%%%%%%%%%%%%%%
\usepackage{tikz, tkz-euclide}%  permet de dessiner des figures, des graphiques
\usepackage{adjustbox}% permet de déterminer une taille de fenêtre

%%  FONT
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{tgadventor}% paquet de police de caractère TGadventor
\usepackage{sansmath}%  Copie-colle la police active dans
%                       \sfdefault (/!\ N'EST PAS UNE POLICE DE CARACTÈRES)

\usepackage{xcolor}
%%%%%%%%%%%%%%%%%% INPUT %%%%%%%%%%%%%%%%%%
%\input{preamble.tex}
%\input{parameters.tex}

%\input{types/f2d_fig}
%%%%%%%%%%%%%%%%%% SETUP %%%%%%%%%%%%%%%%%%
\tikzset{triangloid regular/.cd,
  radius/.initial=3cm,
  lab/.initial=,
}
\tikzset{pics/triangloid regular/.style={code={%
    \tikzset{triangloid regular/.cd,#1}%
    \def\pv##1{\pgfkeysvalueof{/tikz/triangloid regular/##1}}%
    \draw[thick, pic actions] (-30:\pv{radius}) \foreach \x in {90,210}%
        {
            -- (\x:\pv{radius})}%
            -- cycle (90:\pv{radius})%
            (0,\pv{radius}) coordinate node[above, scale=2.5] {\pv{lab}}%
            ;
        }
    },
}

\tikzset{squaroid regular/.cd,
  radius/.initial=3cm,
  lab/.initial=,
}

\tikzset{pics/squaroid regular/.style={code={%
    \tikzset{squaroid regular/.cd,#1}%
    \def\pv##1{\pgfkeysvalueof{/tikz/squaroid regular/##1}}
    \draw[thick, pic actions] (45:\pv{radius}) \foreach \x in {135,225,...,359}
        {
            -- (\x:\pv{radius})}%
            -- cycle (90:\pv{radius})%
            (0,\pv{radius}) coordinate node[above, scale=2.5] {\pv{lab}}
            ;
        }
    },
}
\tikzset{
  pentagoid regular/.cd,
  radius/.initial=3cm,
  lab/.initial=,
}

\tikzset{pics/pentagoid regular/.style={code={%
    \tikzset{pentagoid regular/.cd,#1}%
    \def\pv##1{\pgfkeysvalueof{/tikz/pentagoid regular/##1}}
    \draw[thick, pic actions] (18:\pv{radius}) \foreach \x in {90,162,...,359}
        {
            -- (\x:\pv{radius})}%
            -- cycle (90:\pv{radius})%
            (0,\pv{radius}) coordinate node[above, scale=2.5] {\pv{lab}}
            ;
        }
    },
}
%%%%%%%%%%%%%%%%%% DOCUMENT %%%%%%%%%%%%%%%%%%

% Radius of regular polygons
\newdimen\R
\R=2cm

\begin{document}
\begin{tikzpicture}[font={\sansmath\sffamily},thick, line cap=round, line join=round, >=latex, x=1.0cm, y=1.0cm]

% Indicate the boundary of the regular polygons
\pic {triangloid regular={lab=Triangle}};
\pic[xshift=7cm] {squaroid regular={lab=Square}};
\pic[yshift=-7cm] {pentagoid regular={lab=Pentagone}};
\end{tikzpicture}
\end{document}

with output:

enter image description here

| improve this answer | |
  • didn't hear about the polar coordinates. I'm currently looking into it. – Nilcouv Mar 2 at 14:50
3

While you are waiting for the TikZ debugging service (which is frankly beyond me), here is a version in Metapost purely for comparison.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\usepackage{fontspec}
\setmainfont{TeX Gyre Adventor}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
string name[];
name[3] = "Triangle";
name[4] = "Square";
name[5] = "Pentagon";
name[6] = "Hexagon";
name[7] = "Heptagon";
name[8] = "Octagon";
vardef polygon(expr n, r) =
    for i=0 upto n-1: up scaled r rotated (360/n*i) -- endfor cycle
enddef;
beginfig(1);
    for i=3 upto 8:
        path p;
        p = polygon(i, 60) shifted -144 (i mod 2, floor ((i-1)/2));
        fill p withcolor 7/8[blue, white];
        draw p;
        label.top(name[i], point 0 of p);
    endfor
endfig;
\end{mplibcode}
\end{document}

This is wrapped up in luamplib, so please compile it with lualatex.

| improve this answer | |
  • thx. I have to talk to my boss about switching to LuaLaTeX >-< – Nilcouv Mar 2 at 11:39
2

The shapes.geometric library has these polygons built in.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
\foreach \X [count=\Y starting from 3] in 
    {Triangle,Square,Pentagon,Hexagon,Heptagon,Octagon,Nonagon,Decagon}
    {\pgfmathsetmacro{\myx}{2.5*Mod(\Y-1,2)}
    \pgfmathsetmacro{\myy}{-3*int((\Y-3)/2)}
    \node[regular polygon,regular polygon sides=\Y,draw,
        minimum size=2cm,label=below:\X] at (\myx,\myy){};
         }
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |

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.