I would like to know how to add an arbitrary number of coordinates to the default shapes of TikZ—particularly to the side of a polygon.


This is similar to https://tex.stackexchange.com/a/47856/13552

TikZ Polygon Shape


If you're looking for an arbitrary coordinate around the perimeter you can use (name.number), where name is the shape name and number is an angle in degrees measured from name.east.

\node[name=s, shape=regular polygon, inner sep=1cm,draw,color=black!30,thick] {};
\draw[shift=(s.east),blue!70] plot[mark=o] coordinates{(0,0)} {};
  \foreach \anchor in
\draw[shift=(s.\anchor),red] plot[mark=x] coordinates{(0,0)} {};

enter image description here


When using shape=regular polygon, TikZ names all the sides and corners, starting from the top of the shape (see TikZ 3.0.0 manual Sec. 67.3 Geometric Shapes for details.) In order to specify an arbitrary point along an arbitrary side, you could define a command that places a point some distance between two consecutive corners (i.e., along a side) of the regular polygon. This requires \usetikzlibrary{calc}.

In the example below, I've defined the command mysidemark, which has the following syntax: \mysidemark{<start corner>}{<end corner>}{<fraction of distance along path (between 0 and 1)>}.

\draw[red,shift=($(s.corner #1)!#3!(s.corner #2)$)] plot[mark=*] coordinates{(0,0)} {};
\node[name=s, shape=regular polygon, regular polygon sides=6, inner sep=1cm,draw,color=black!30,thick] {};

\node[name=s, shape=regular polygon, regular polygon sides=7, inner sep=1cm,draw,color=black!30,thick] {};

enter image description here


This command can be used explicitly, as shown, but more work is necessary to allow it to be used in a \foreach command. See, e.g., \foreach with \newcommand how to use them together?.

  • How would you recommend doing exactly that but for a single side..say from {1,2,..100} on a side, so that I can connect to any shape's side in the same position regardless of size (as shape size increases, the perimeter units loose their resolution--effectively moving anchor points around the shape in a counter-clockwise direction). I had considered making a path using the predefined anchor points of the shape in question. Do you have any ideas? – Jonathan Komar Nov 26 '14 at 14:13
  • In other words, could I apply your code to a segment? Given segment A-B, add an arbitrary number (likely 100) of coordinates to that segment (a line). – Jonathan Komar Nov 28 '14 at 12:28
  • 1
    @macmadness86 Please see my edit. – erik Nov 29 '14 at 6:56

Defining new anchors is tricky and basically impossible without looking at the PGF code used to define the shape. But aliasing anchors is quite straightforward (once you have the command to do it):

  \expandafter\gdef\csname pgf@anchor@#1@#2\endcsname{%
    \pgf@sh@reanchor{\csname pgf@sh@ns@\pgfreferencednodename\endcsname}{#3}%

\pgfaliasanchor{regular polygon}{peter}{90}
\pgfaliasanchor{regular polygon}{paul}{330}
\pgfaliasanchor{regular polygon}{mary}{south west}
\node [fill=gray!50, regular polygon, minimum size=1.5in] (shape) at (2,2) {};
\foreach \anchor in {peter, paul, mary}
  \draw [draw=red] (shape.\anchor) 
    circle [radius=1/20] node [above] {\tt\anchor};

enter image description here

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.