# Generalize TikZ Fraction Diagrams to any n-sided Polygon

I would like to extend Mark Wibrow's answer in this question to general polygons. The user would be able to type something like

\begin{tikzpicture}
\end{tikzpicture}


to get an octagon with segments drawn as radii, or

\begin{tikzpicture}
\pic {fraction={style=5-gon, segment=apothem, color=gray, fraction={12/5}}};
\end{tikzpicture}


to get a pentagon with segments drawn as apothems.

In Mark Wibrow's answer he has described how to create styles for circles, triangles, and flower petals. I would like to be able to generate the following types of fraction diagrams for any n-gon:

(Left: segments drawn as radii. Right: segments drawn as apothems)

I have very little experience with TikZ - I created the above graphics using Microsoft Paint. Thank you in advance for your help!

Just use a regular polygon node and add a path picture. There is a option for nontrivial greatest common divisors and the case in which the fraction equals 1/2.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{shapes.geometric}
\newif\ifgcd
\begin{document}
\begin{tikzpicture}[ngon fraction/.style args={#1/#2}{regular polygon,
minimum size=\pgfkeysvalueof{/tikz/ngon size},
regular polygon sides=#2,draw,path picture={\ifodd#2
\pgfmathsetmacro{\mystartangle}{90-360/#2}
\else
\pgfmathsetmacro{\mystartangle}{0}
\fi
\pgfmathtruncatemacro{\itest}{ifthenelse(#2/#1==2,1,0)}
\ifnum\itest=1
\foreach \X in {0,2,...,#2}
{\draw[fill=gray!20]  (\mystartangle+\X*360/#2:\pgfkeysvalueof{/tikz/ngon size})
-- (0,0) --  (\mystartangle+\X*360/#2+360/#2:\pgfkeysvalueof{/tikz/ngon size});}
\else
\fill[gray!20] (0,0) -- (\mystartangle:\pgfkeysvalueof{/tikz/ngon size}) arc[start angle=\mystartangle,end
\ifgcd
\pgfmathtruncatemacro{\mygcd}{gcd(#1,#2)}
\pgfmathtruncatemacro{\myupper}{#2/\mygcd}
\foreach \X in {1,...,\myupper}
{\draw (0,0) -- (\mystartangle+\mygcd*\X*360/#2:\pgfkeysvalueof{/tikz/ngon size});}
\else
\foreach \X in {1,...,#2}
{\draw (0,0) -- (\mystartangle+\X*360/#2:\pgfkeysvalueof{/tikz/ngon size});}
\fi
\fi
}},gcd/.is if=gcd,apothem/.style={shape border rotate=180/#1},
ngon size/.initial=2cm
]
\path (0,0) node[ngon fraction=1/4,]{}
(3,0) node[ngon fraction=1/4,apothem=4]{}
(0,-3) node[ngon fraction=4/5,rotate=108]{}
(3,-3) node[ngon fraction=4/5,rotate=108,apothem=5]{}
(0,-6) node[ngon fraction=4/6,rotate=150]{}
(3,-6) node[gcd,ngon fraction=4/6,rotate=150,apothem=6]{}
(0,-9) node[ngon fraction=4/8,shape border rotate=360/16,rotate=360/16]{}
(3,-9) node[ngon fraction=4/8]{}
;
\end{tikzpicture}
\end{document}


• Hi Schrödinger's cat, I love your solution! Thank you so much! Would you recommend I put the ngon fraction/.style=... and everything up to \path in the preamble using \tikzset{}? Also, is it correct that I should use \pic{circle fraction={1/4}}; to insert circle fractions, and use \node[ngon fraction=1/4]{}; to insert ngon fractions? I don't think \node works for circle fractions, does it? Thanks! – Mathemanic Jan 15 at 7:20
• @Mathemanic No, these are slightly different approaches. Mark uses pics, which I really love and use a lot, but for the regular polygons it so happens that the regular polygons already exist as nodes, and they come with the shape border rotate key, which helps a lot. So, yes, for these you need to use node. – Schrödinger's cat Jan 15 at 7:25
• @Mathemanic You could define a pic via \tikzset{pics/ngon fraction/.style args={#1/#2}{code={\coordinate[ngon fraction=#1/#2];}}} and then use \path (6,0) pic{ngon fraction=1/4};, say. Whether it is better to use \tikzset or the preamble of a tikzpicture depends on your use case. If you use them in several pictures, use \tikzset. The problem with that is if someone else is defining a style of the same name, and you copy their code to your document, then you may overwrite the definitions. – Schrödinger's cat Jan 15 at 7:27
• Thank you for the extremely helpful answer. You went above and beyond. I sincerely appreciate it! – Mathemanic Jan 15 at 7:39
• @Mathemanic You're welcome! – Schrödinger's cat Jan 15 at 7:40

Schrödinger's cat answer is perfect. I give only a way with tkz-euclide to avoid complications. It's a test to see if it's possible... There is some work to complete and get the link between fraction and polygon. The solution with apothem can be made in the same way.

\documentclass[]{article}
\usepackage{tkz-euclide}
\parindent=0pt
\begin{document}

\foreach \i in {3,...,7}
{   \begin{tikzpicture}
\tkzDefPoints{0/0/P0,2/0/P1}
\tkzDefRegPolygon[center,sides=\i](P0,P1)
\tkzDrawPolygon(P1,P...,P\i)
\tkzFillPolygon[gray!20](P0,P...,P\i)
\foreach \j in {1,...,\i} {\tkzDrawSegment[black](P0,P\j)}
\end{tikzpicture}\\}
\end{document}


Now with this :

\documentclass[]{article}
\usepackage{tkz-euclide}
\parindent=0pt
\begin{document}

\foreach \i  in {3,...,7}
{   \begin{tikzpicture}
\tkzDefPoints{0/0/P0,0/0/Q0,2/0/P1}
\tkzDefMidPoint(P0,P1) \tkzGetPoint{Q1}
\tkzDefRegPolygon[center,sides=\i](P0,P1)
\tkzDefMidPoint(P1,P2) \tkzGetPoint{Q1}
\tkzDefRegPolygon[center,sides=\i,name=Q](P0,Q1)
\tkzDrawPolygon(P1,P...,P\i)
\tkzFillPolygon[gray!20](Q0,Q1,P2,Q2)
\foreach \j in {1,...,\i} {\tkzDrawSegment[black](P0,Q\j)}
\end{tikzpicture}\\}
\end{document}


• Thanks, Alain! Is there a way to put this into the preamble, and turn it into a macro, so you can type something short like \begin{tikzpicture}\pic{fraction={style=5-gon, segment=apothem, fraction={12/5}}};\end{tikzpicture}? – Mathemanic Jan 16 at 18:15
• @Mathemanic Yes this is possible but I have no time to do this. I work no my last version of tkz-euclide. – Alain Matthes Jan 18 at 5:54