4

I have constructed a polygon using tikz and would like to know how to label the points in the manner shown in the diagram below

enter image description here

I have constructed my polygon as follows:

\documentclass{article}
\usepackage{tikz}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture} [scale = 0.2]
\coordinate (a) at (10,0);
\coordinate (b) at (8,5);
\coordinate (c) at (5,8);
\coordinate (d) at (0,10);
\coordinate (e) at (-5,8);
\coordinate (f) at (-8,5);
\coordinate (g) at (-10,0);
\coordinate (h) at (-8,-5);
\coordinate (i) at (-5,-8);
\coordinate (j) at (0,-10);
\coordinate (k) at (5,-8);
\coordinate (l) at (8,-5);
\draw (a) -- (b) --(c) -- (d) -- (e) -- (f) -- (g) -- (h) -- (i) -- (j) -- (k) -- (l) -- (a);
\draw[thick] (0,12) -- (0,-12);
\draw[thick] (12,0) -- (-12,0);
\node[above] at (7.5,0.2) {\circled{1}};
\node[above] at (12,0.2){(10,0)};
\end{tikzpicture}
\end{figure}
\end{document}

It is really cumbersome to find the right coordinates to add the label as well as the coordinates of the corresponding point on the polytope. The labels often look ugly and aren't well positioned. Is there any particular command that I could use in order to simplify the process?

Thanks!

3

This reduces the amount of code, though the result could probably be better. (If some wizard comes up with a much better solution I'll likely delete this answer.)

The labels are placed with the following loop:

\foreach [count=\i] \x/\y in {10/0,8/5,5/8,0/10,-5/8,-8/5,-10/0,-8/-5,-5/-8,0/-10,5/-8,8/-5}
{
  \path (\x,\y) ++({atan2(\y,\x)}:3.5cm) node [fill=white,inner sep=0pt] {$(\x,\y)$};
  \path (\x,\y) ++({atan2(\y,\x)-180}:2cm) node [draw,circle,inner sep=2pt,fill=white]{\i};
}

What happens here is simply using polar coordinates to place them. ++({atan2(\y,\x)}:3.5cm) means, from the previous position (here, (\x,\y)), move 3.5cm in the direction given by the angle atan2(\y,\x), and make this position the new 'active' position. A node is then placed in this position with node [fill=white,inner sep=0pt] {$(\x,\y)$}.

For the second \path, note that the angle is atan2(\y,\x)-180, meaning that instead of moving away from the origin, you move towards the origin. Hence placing these nodes on the inside of the polygon, instead of the outside.

On second thought, one \path command is actually enough:

\foreach [count=\i] \x/\y in {10/0,8/5,5/8,0/10,-5/8,-8/5,-10/0,-8/-5,-5/-8,0/-10,5/-8,8/-5}
{
  \path (\x,\y) +({atan2(\y,\x)}:3.5cm) node [fill=white,inner sep=0pt] {$(\x,\y)$}
                +({atan2(\y,\x)-180}:2cm) node [draw,circle,inner sep=2pt,fill=white]{\i};
}

The difference here is that with one +, the new position is not made the active position, so when the next position is calculated, the reference position is still (\x,\y).

enter image description here

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture} [scale = 0.2]
\draw (10,0) 
  \foreach \x/\y in {8/5,5/8,0/10,-5/8,-8/5,-10/0,-8/-5,-5/-8,0/-10,5/-8,8/-5,10/0}
      { -- (\x,\y)};

\draw[thick] (0,12) -- (0,-12);
\draw[thick] (12,0) -- (-12,0);

\foreach [count=\i] \x/\y in {10/0,8/5,5/8,0/10,-5/8,-8/5,-10/0,-8/-5,-5/-8,0/-10,5/-8,8/-5}
{
  \path (\x,\y) ++({atan2(\y,\x)}:3.5cm) node [fill=white,inner sep=0pt] {$(\x,\y)$};
  \path (\x,\y) ++({atan2(\y,\x)-180}:2cm) node [draw,circle,inner sep=2pt,fill=white]{\i};
}
\end{tikzpicture}
\end{document}
| improve this answer | |
  • That's perfect!. What exactly do the two \path commands do? – Ad22 Jul 30 '15 at 12:51
  • @Aditya I added some more explanation. – Torbjørn T. Jul 30 '15 at 13:15
3

What about this?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric} 
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture} 
\draw (-3,0) -- (3,0);
\draw (0,-3) -- (0,3);
\draw [blue, dashed] (0,0) circle(2.5cm);
\node[regular polygon, regular polygon sides=12, minimum size=5cm, rotate=-60, draw] at (0,0) (A) {};
\foreach \i in {1,...,12}  {
     \path  (0,0) -- node [draw, circle, inner sep=0mm, minimum size=3ex, pos=0.8]  {\i} (A.corner \i); 
     \path  let \p1=($(A.corner \i)-(0,0)$),
                \n1={\x1*0.0352777778},
                \n2={\y1*0.0352777778}
            in  
            \pgfextra{\edef\mya{\ifdim\x1<0pt left\else right\fi}}
            (0,0) -- node [pos=1.0, \mya,sloped,scale=0.8]  
    {$(
        \pgfmathprintnumber[fixed]{\csname strip@pt\endcsname\dimexpr\n1\relax},
        \pgfmathprintnumber[fixed]{\csname strip@pt\endcsname\dimexpr\n2\relax}
    )$} (\x1,\y1); 
}
\end{tikzpicture}
\end{document}

with the result of....

resulting figure

| improve this answer | |
  • 1
    Well now you do :) – percusse Jul 30 '15 at 14:11
  • @Rmano I'm actually trying to approximate points on the trigonometric moment curve as integers, so this doesn't really work. Rounding these numbers down to integer values might lead to larger integers and might lead to the combinatorial structure of the resulting cyclic polytope falling apart. Thanks a lot anyway! – Ad22 Jul 30 '15 at 15:34
  • @percusse thanks a lot! I had almost done it with tex.stackexchange.com/a/18294/38080, but you beat me... – Rmano Jul 30 '15 at 15:58
  • @percusse I modified the code a bit to add what I think is correct label positioning, although I think it could fail for vertical lines... It was quite difficult for me to have the if working! – Rmano Jul 30 '15 at 17:29

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.