4

I'd like some help to make a conditional statement in LaTeX and TikZ work. In this M(non-)WE, I'd like the first, third and fifth dots to be larger:

\documentclass[tikz]{standalone}
\usepackage{tikz}

\begin{document}
\def\bigdot{5pt}
\def\littledot{1pt}
\begin{centering}
  \begin{tikzpicture}
    \foreach \x in {0,...,5}{
      \draw (\x,0) circle ({int(\x/2)==0?\bigdot:\littledot});
    }
  \end{tikzpicture}
\end{centering}
\end{document}

What am I not understanding? Thanks for any help or advice.

2 Answers 2

4

Welcome! Are you looking for iseven?

\documentclass[tikz]{standalone}

\begin{document}
  \begin{tikzpicture}
   \def\bigdot{5pt}
   \def\littledot{1pt}
    \foreach \x in {0,...,5}{
      \draw (\x,0) circle [radius={iseven(\x)?\bigdot:\littledot}];
    }
  \end{tikzpicture}
\end{document}

enter image description here

In your code you are asking to make the circles for which int(\x/2) is zero big. These are the circles at \x=0 and \x=1, and this is what you get. However, if you want to have "the first, third and fifth dots to be larger" in a list {0,...,5} then you can make the dots larger for which \x is even.

ADDENDUM: As for the different question raised in the comments, you can use mod, as suggested by AlexG, or Mod, which always returns nonnegative values and helps you to avoid confusion (at least I occasionally wasted a lot of time because I was using mod). Both versions are described on p. 1033 of pgfmanual v3.1.5. As above, I prefer the non-deprecated syntax

 circle[radius=<radius>]

over the older, deprecated syntax

 circle(<radius>)

so the proposal for the question in the comments could be

\documentclass[tikz]{standalone}

\begin{document}
  \begin{tikzpicture}
   \path (0,0) node[circle,inner sep=5cm] (c){};
   \def\bigdot{5pt}
   \def\littledot{1pt}
    \foreach \x in {0,...,359}{
      \draw (c.\x) circle [radius={Mod(\x,5)==0?\bigdot:\littledot}];
    }
  \end{tikzpicture}
\end{document}

Note also that if you use \documentclass[tikz]{standalone} then tikz gets automatically loaded, so \usepackage{tikz} is unnecessary. Also, I generally do not really like \defs but if you want to use them, use them locally, i.e. inside the tikzpicture, as above. Yet personally I'd use something like

\documentclass[tikz]{standalone}

\begin{document}
  \begin{tikzpicture}[declare function={rsmall=1;rbig=5;}]
   \path (0,0) node[circle,inner sep=5cm] (c){};
    \foreach \x in {0,...,359}{
      \draw (c.\x) circle [radius={(Mod(\x,5)==0?rbig:rsmall)*1pt}];
    }
  \end{tikzpicture}
\end{document}
13
  • Sorry, no. I just picked every other dot for the MWE. In reality, I need every fifth dot larger in the range {0,...,359} for a compass circle. Thanks so much for taking the time to answer, though. May 11, 2020 at 21:07
  • 1
    @KevinZembower Well, I was trying to answer the question. You can use \draw (\x,0) circle [radius={Mod(\x,5)==0?\bigdot:\littledot}]; for the "every fifth" case. (Note the upper case in Mod and the syntax [radius={Mod(\x,5)==0?\bigdot:\littledot}] instead of round parentheses.)
    – user194703
    May 11, 2020 at 21:11
  • 1
    @KevinZembower I added an explanation to an addendum of the answer.
    – user194703
    May 11, 2020 at 21:26
  • 1
    @KevinZembower It compiles fine on my updated TeXLive 2020 installation with pdflatex, lualatex and xelatex. (Let me mention that your original code works fine in principle, you just picked an inappropriate condition for getting "he first, third and fifth dots to be larger". In order to answer the original question, one can use iseven(\x), mod(\x,2)==0 or Mod(\x,2)==0 or int(\x/2)==\x/2 and so on, but there was never a problem in implementing conditions in general. I tried to explain why the output of your code is the expected output.)
    – user194703
    May 11, 2020 at 21:38
  • 1
    @KevinZembower Yes, it could well be that on a rather old version there are problems. Of course there are also reasons for the changes in preferred syntax and even how the pgf functions work. (I am not even sure when Mod was added.) What do you get if you put \message{I am using pgf version \pgfversion^^J} in your document? (Regardless of these issues here I'd recommend updating your TeX installation, there have been some important changes, not only in TikZ. For instance, my friend AlexG has created a really stunning package called ocgx2.)
    – user194703
    May 11, 2020 at 22:04
2

You certainly want the mod(x,y) (Modulo) operator:

\documentclass[tikz]{standalone}
\usepackage{tikz}

\begin{document}
\def\bigdot{5pt}
\def\littledot{1pt}
\begin{centering}
  \begin{tikzpicture}
    \foreach \x in {0,...,5}{
      \draw (\x,0) circle ({mod(\x,2)==0?\bigdot:\littledot});
    }
  \end{tikzpicture}
\end{centering}
\end{document}

enter image description here

4
  • Yes, this might be it. I'll try it tomorrow morning in my real code, and if I can get it to work. I'll select your answer. However, I would still like to see a solution using (?:). For instance, in the range {1,...100}, make just the 7th circle large. Thanks for your efforts to help me. May 11, 2020 at 21:12
  • This solution uses the (?:)syntax.
    – AlexG
    May 11, 2020 at 21:13
  • Thanks. You're right. I was thinking of another example, for instance, if I wanted just the 7th dot big in the range {1,...,100}. Would this be just ({\x==7?\bigdot:\littledot})? May 11, 2020 at 21:25
  • Yes, but the range should start with zero. Then, the first dot would be big, followed by 6 small dots, the next being big again and so on.
    – AlexG
    May 12, 2020 at 6:18

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.