[null, {"document_url": "https://tex.stackexchange.com/questions/127730/draw-polygonal-number-using-pstricks-or-tikz", "unformatted_src": "https://i.stack.imgur.com/xBBvS.jpg", "src": "https://i.stack.imgur.com/xBBvS.jpg", "formatted_filename": "xBBvS", "alt_text": "polygonal-numbers"}, null, null, {"document_url": "https://tex.stackexchange.com/questions/127730/draw-polygonal-number-using-pstricks-or-tikz", "unformatted_src": "https://i.stack.imgur.com/CqXQa.png", "src": "https://i.stack.imgur.com/CqXQa.png", "formatted_filename": "CqXQa", "alt_text": "enter image description here"}, {"document_url": "https://tex.stackexchange.com/questions/127730/draw-polygonal-number-using-pstricks-or-tikz", "unformatted_src": "https://i.stack.imgur.com/IruBd.png", "src": "https://i.stack.imgur.com/IruBd.png", "formatted_filename": "IruBd", "alt_text": "enter image description here"}, {"document_url": "https://tex.stackexchange.com/questions/127730/draw-polygonal-number-using-pstricks-or-tikz", "unformatted_src": "https://i.stack.imgur.com/8RdVz.png", "src": "https://i.stack.imgur.com/8RdVz.png", "formatted_filename": "RdVz", "alt_text": "enter image description here"}, {"document_url": "https://tex.stackexchange.com/questions/127730/draw-polygonal-number-using-pstricks-or-tikz", "unformatted_src": "https://i.stack.imgur.com/b8RIJ.png", "src": "https://i.stack.imgur.com/b8RIJ.png", "formatted_filename": "b RIJ", "alt_text": "enter image description here"}, {"document_url": "https://tex.stackexchange.com/questions/127730/draw-polygonal-number-using-pstricks-or-tikz", "unformatted_src": "https://i.stack.imgur.com/y49r4.png", "src": "https://i.stack.imgur.com/y49r4.png", "formatted_filename": "y r", "alt_text": "Picture for <code>sides=7</code>"}, null, null, {"document_url": "https://tex.stackexchange.com/questions/127730/draw-polygonal-number-using-pstricks-or-tikz", "unformatted_src": "https://i.stack.imgur.com/y4XBy.png", "src": "https://i.stack.imgur.com/y4XBy.png", "formatted_filename": "y XBy", "alt_text": "enter image description here"}, null, null, {"document_url": "https://tex.stackexchange.com/questions/127730/draw-polygonal-number-using-pstricks-or-tikz", "unformatted_src": "https://i.stack.imgur.com/oj2jD.png", "src": "https://i.stack.imgur.com/oj2jD.png", "formatted_filename": "oj jD", "alt_text": "enter image description here"}, null, {"document_url": "https://tex.stackexchange.com/questions/127730/draw-polygonal-number-using-pstricks-or-tikz", "unformatted_src": "https://i.stack.imgur.com/qBumz.jpg", "src": "https://i.stack.imgur.com/qBumz.jpg", "formatted_filename": "qBumz", "alt_text": "output"}, null, null, null]




I would like to draw the following using PSTricks (or TikZ): (Found on http://i1.wp.com/mathandmultimedia.com/wp-content/uploads/2012/08/polygonal-numbers.png.)

I can do it myself using an extremely low-tech soluton where I draw every blue and yellow dot maunally, but I can't figure out how to do it' automatically'.

P.S. Sorry for not giving it a try myself; I really don't know where to start (other than the fact that is probably has to involve \multidos).


I would like a representation of the triangle numbers, too.


Consider the following code (from Qrrbrbirlbel):

How to I create a command where \polygonnumbers takes the two arguments #1 = <number of dots in each row> and #2 = <maximal n-gon>? (Now, I just have a command with the single argument #1 = <maximal n-gon>.)

Update 3

Unfortunately, I get an error if I change \polygonnumbers{5}{6} to \polygonnumbers{4}{6} in the following code:



Here is what I ende up with (after another slight improvement):

tikz-pgf diagrams pstricks automation
edited May 19 '16 at 13:54
asked Aug 11 '13 at 18:19 Svend Tveskæg


up vote 17 down vote accepted

Here is a solution using TikZ.

There exist a few keys that change the graphics parameter:

The polygon is drawn with the center node at the point where the path currently is where you use the polynum key. The âpolynumâ can be rotated by rotating the path. The polygons stand on their center corner if you rotate the path about 360/<corners>/2.

The bar is drawn with an active rotation in mind (that is the reason for the \pgfpointshapeborder), this makes it possible to include the nodes as they are without the need to rotate them or to use transform shape. The bar-drawing algorithm though will create a bad result if the nodes are not of the same size.

The macros

can be used to affect the styles, this is used inside the every corner edge style (actually a .code key) that turns the drawing of the edge between certain corner nodes explicitly off.

The nodes are named: pn@<corner>@<level>@<sublevel>. The red center node is named pn@0@1@0, it is the only 0th corner node. edited Aug 12 '13 at 4:02
answered Aug 11 '13 at 21:53 This solution is based on the object-oriented feature of the Asymptote. The basic structure struct PolygonalNumber is defined in asydef environment, and can be used later in asy pictures to create and draw objects of this kind. For example, a triangular object named P3 with 5 layers is defined as PolygonalNumber P3=PolygonalNumber(3,5); and command draw(P3); will draw it. PolygonalNumber objects can be transformed as usual, for example draw(shift(6,0)*P4); draws a P4 object shifted to the right. Or they can be used directly in the draw commands: the code to produce the image above was just

A complete MWE polynum.tex:

To process it with latexmk, create file latexmkrc:

and run latexmk -pdf polynum.tex.

answered Aug 12 '13 at 5:01 
I started with this but then I got bored :) Always nice to steal Jake's ncbarfrom Is there a TikZ equivalent to the PSTricks \ncbar command?

\numofsides and \numofiters define the polygon shape and how many times it is repeated. I don't have Qrrbrbirlbel's patience. So it's just for fun.

A truly ridiculous image With \def\numofsides{24} and \def\numofiters{24}: 
edited Apr 13 '17 at 12:34 answered Aug 12 '13 at 21:12 
Not the answer you're looking for? Browse other questions tagged tikz-pgf diagrams pstricks automation or ask your own question.


Is there a TikZ equivalent to the PSTricks \ncbar command?
Key that takes a list of other keys as argument and sets them


Draw tangents from point to ellipse using PSTricks
Draw a prism in TikZ or PSTricks
Using Pstricks to draw white boxes with white boarders fails?
Draw tally marks using PSTricks
How can I draw a wave diagram in Tikz/PSTricks
How should I read the current TikZ “label distance”?
TikZ/ERD: node (=Entity) label on the inside
Positioning scopes in tikz
How to draw this control flow graph using pstricks/tikz

