8

I am trying to draw every 12th circle in red. Currently I have this code:

\documentclass{article}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}
    \draw (0,0) -- (\textwidth,0);
    \foreach \x in {0,...,24} %
    { %
        \if ??
            \draw[red](\x\textwidth/10,0) circle (1);
        \else
            \draw (\x\textwidth/10,0) circle (0.5);
        \fi
    }
    \end{tikzpicture}
\end{document}

I am not sure how to test if \x % 12 == 0. How do I do that in Latex?

  • 1
    In general, \ifnum \x=12\relax ...\else ...\fi will check its value for 12. – Steven B. Segletes Feb 17 '17 at 16:30
  • Actually, your circles are only 10, where do you want to draw the others? – CarLaTeX Feb 17 '17 at 16:31
  • Supposed I am drawing 24 circles, I want to draw the 12th and 24th circle in red. – Pascal Feb 17 '17 at 16:32
5

A different version:

\documentclass{article}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}
    \newcommand\Ncirc{24} % number of circles
    \draw (0,0) -- (\textwidth,0);
    \foreach [count=\i from 0,evaluate=\i as \y using {int(mod(\i,\Ncirc/2))}] \x in {0,...,\Ncirc} %
    { %
        \ifnum\y=0
            \draw[red](\x\textwidth/\Ncirc,0) circle (1);
        \else
            \draw (\x\textwidth/\Ncirc,0) circle (0.5);
        \fi
    }
    \end{tikzpicture}
\end{document}

enter image description here

  • This appears to be the TikZ/PGF way and should probably be preferred; it's also (at least for me) a lot easier to read. – Big-Blue Feb 17 '17 at 17:44
  • Thanks, this looks interesting. But as I tested it, the first node will not be drawn red. But shouldn't the modulo of 0 % 12 to be equal 0? – Pascal Feb 17 '17 at 17:49
  • 1
    @Pascal Counting starts at 1, use count=\i from 0 to start at 0. – Torbjørn T. Feb 17 '17 at 17:50
  • @Pascal, if I understand you correctly, than you looking for the following variation of the Torbjørn T. answer: \foreach [count=\i,evaluate=\i as \y using {int(mod(\i,12))}] \x in {0,...,24} % { \ifnum\y=0 \draw[red](\x\textwidth/25,0) circle (1) \else \draw (\x\textwidth/25,0) circle (0.5) \fi; } – Zarko Feb 17 '17 at 18:04
8

As I said in my comment, \ifnum \x=12\relax...\else...\fi can be used to determine the 12th loop. But if you need to find multiples of 12, I here use a separate index cindex, that tracks with \x, but is reset to 0 whenever it reaches 12.

\documentclass{article}
\usepackage{tikz}
\newcounter{cindex}
\begin{document}
    \begin{tikzpicture}
    \draw (0,0) -- (\textwidth,0);
    \setcounter{cindex}{0};
    \foreach \x in {0,...,24} %
    {%
        \stepcounter{cindex};
        \ifnum \thecindex=12\relax
            \setcounter{cindex}{0};
            \draw[red](\x\textwidth/30,0) circle (1);
        \else
            \draw (\x\textwidth/30,0) circle (0.5);
        \fi%
    }
    \end{tikzpicture}
\end{document}

enter image description here

  • 2
    so I guess there is no command in Latex for the modulo operator? – Pascal Feb 17 '17 at 16:43
  • @Pascal there may be, but it is not my forte. Perhaps wait to see if someone provides a better answer. – Steven B. Segletes Feb 17 '17 at 16:44
  • 2
    \intcalcMod from intcal might be interesting. :) – Paulo Cereda Feb 17 '17 at 16:59
  • 1
    And Mod(x, y) from TikZ itself. – Paulo Cereda Feb 17 '17 at 17:00
  • 1
    @Pascal The first circle is numbered 0 by \x, therefore, the example has 25 circles and each 12th circle is caught by: \pgfmathparse{int(mod(\x+1, 12))}\ifnum\pgfmathresult=0 ...\else ...\fi. – Heiko Oberdiek Feb 17 '17 at 17:28

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.