4

For demonstration purposes, I want to obtain a graph of C10 shaped as a star. I have been working on this for much longer than I should have. I currently have this:

\begin{figure}
    \centering
    \begin{tikzpicture}[nodes={circle, draw}]
        \foreach \i in {1,...,10}
        {
            \pgfmathisodd{\i};
            \node (N\i) at (360/10*\i:\pgfmathresult?18mm:9mm) {\i};
        }
        \foreach \i in {1,...,10}
        {
            \pgfmathmod{\i}{10}
            \pgfmathadd{\pgfmathresult}{1};
            \path (N\i) edge (N\pgfmathresult);
        }
    \end{tikzpicture}
\end{figure}

Which outputs the following abomination:

enter image description here

I don't know if the same ugliness would happen had I typed out the nodes myself.

  • The "abomination" is simply all numbers aligned the same way. Try instead of defining the points recursively like you did and then in a second step add the lines and then the numbers. This should make it better :) – Superuser27 Mar 7 at 14:36
  • What I mean is: if you just look at how the numbers are positioned, they look fine. It's just the lines starting from weird places. – Superuser27 Mar 7 at 14:37
  • I have given up and written every node one by one. It works! The question is even more curious to me now. – ThoAppelsin Mar 7 at 15:00
3

After the manipulations, the result won't be an integer any more. Rather, you'll get numbers like 1.0, where .0 is interpreted as an anchor. Therefore I suggest

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
 \begin{tikzpicture}[nodes={circle, draw}]
        \foreach \i in {1,...,10}
        {
            \pgfmathisodd{\i};
            \node (N\i) at (360/10*\i:\pgfmathresult?18mm:9mm) {\i};
        }
        \foreach \i in {1,...,10}
        {
            \pgfmathtruncatemacro{\j}{mod(\i,10)+1}
            \path (N\i) edge (N\j);
        }
    \end{tikzpicture}
\end{document}

enter image description here

  • 3
    I am aware of the fact that one can shorten the code. This answer is to explain what happens and to provide a way that works which is very close to the code of the question. – marmot Mar 7 at 16:12
  • Probably a good idea to fix the circle size so the 10 node isn't bigger than the others. – Sandy G Mar 7 at 16:29
  • Great answer, thank you for being to the point. And to @SandyG, I already have fixed the size inconsistency via putting all nodes in boxes with zero width, and giving any size I like to the nodes. – ThoAppelsin Mar 7 at 19:10
1

PGF's foreach has quite powerfull tools that you can use here:

\begin{tikzpicture}[nodes={circle, draw}]
    \foreach[evaluate=\i as \j using isodd(\i)] \i in {1,...,10}
    {
        \node (N\i) at (360/10*\i:\j?18mm:9mm) {\i};
    }
    \foreach[remember=\i as \j (initially 10)] \i in {1,...,10}
    {
        \draw (N\i) -- (N\j);
    }
\end{tikzpicture}

The optional [evaluate=\i as \j using isodd(\i)] in the first loop computes isodd(\i) and stores the result in macro \j.

The optional [remember=\i as \j (initially 10)] in the second loop stores \i's content in macro \j at the end of the iteration, allowing for this content to be available for the next iteration.

  • Thank you for your extended recommendations, but I think marmot's answer is more relevant to the issue/question, since it also explains the cause of the erroneous outcome in the question. – ThoAppelsin Mar 7 at 19:11
  • @ThoAppelsin I agree, this answer is also aimed at those who will find your question later and offer them a simple and robust solution. – Christoph Frings Mar 7 at 19:31

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.