4

I am drawing a regular pentadecagon (i.e. 15-gon) using tikz and the shapes.geometric TikZ library. And I went with this (simplified):

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture} 
    \node [
        draw, regular polygon,
        regular polygon sides = 15
    ] at (0,0) {};
\end{tikzpicture}
\end{document}

Everything is fine, until I came up with the idea of wrapping the "magic number" 15 into macro. I did this:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}
    \def\sides{15}    
    \node [
        draw, regular polygon,
        regular polygon sides = \sides
    ] at (0,0) {};
\end{tikzpicture}
\end{document}

Boom!!!

! Missing number, treated as zero.
<to be read again> 
                   \relax 
l.12     ] at (0,0) {};

I also tried this:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}
    \newcommand*\sides{15}    
    \node [
        draw, regular polygon,
        regular polygon sides = \sides
    ] at (0,0) {};
\end{tikzpicture}
\end{document}

But it boomed exactly the same way. So:

  1. Why is this not working? I do not quite understand :(

  2. What, then, should I do?

I considered putting a \tikzset{regular polygon sides = 15}, but since I need to use the magic number somewhere else, I can't use this approach unless I can later extract this number.

Any help is appreciated :)

1
  • 1
    For your information, \newcommand* is essentially just \def with ensuring that the macro is not already defined.
    – Skillmon
    Commented Jan 26, 2019 at 11:32

3 Answers 3

4

This is the solution of the puzzle: look in pgflibraryshapes.geometric.code.tex and find that \sides is already used internally:

683 % Shape Regular Polygon.
684 %
685 \pgfdeclareshape{regular polygon}{%
686     \savedmacro\sides{%
687         \pgfmathtruncatemacro\sides{\pgfkeysvalueof{/pgf/regular polygon sides}}%
688     }%

(line numbers added for locating the code). Thus redefining \sides will most likely have adverse effects on processing the code. On page 1112 of the PGF manual we see

enter image description here

0
4

Why not use the math tikzlibrary

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric,math}
\tikzmath{
let \n=15;} 

\begin{document}
\begin{tikzpicture}

    \node [
        draw, regular polygon,
        regular polygon sides = \n
    ] at (0,0) {};
\end{tikzpicture}
\end{document}
3
  • Sorry, but it still booms when I compile it :(
    – L. F.
    Commented Jan 26, 2019 at 11:28
  • However, all is good if I use another name like \sidesss instead of \sides... Interesting behavior :P
    – L. F.
    Commented Jan 26, 2019 at 11:29
  • @L.F. Initially I have used \n for side numbers and it has compiled without error. Later I have decided to edit my answer to use the name you have chosen but without trying to compile to code. So sorry! Commented Jan 26, 2019 at 11:57
3

@egreg explained you very nicely why the error is there, and here is a simple way how to use \sides nevertheless. You only need to add the .expanded key.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}
    \newcommand*\sides{15}    
    \node [
        draw, regular polygon,
        regular polygon sides/.expanded=\sides
    ] at (0,0) {};
\end{tikzpicture}
\end{document}

enter image description here

1
  • 1
    Thank you, it works! If only I were able to accept two answers!
    – L. F.
    Commented Jan 27, 2019 at 2:06

You must log in to answer this question.

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