1

I wanted to make a picture of a numeric semigroup S(p,q) - draw all points that belong to it smaller than p*q. I don't know how to break the loop. My code does not work:

\documentclass{amsart} 
\usepackage{ifthen}
\usepackage{pgfplots} 
\usepackage{pgf} 

\pgfmathsetmacro{\pvar}{4} 
\pgfmathsetmacro{\qvar}{5} 
\pgfmathsetmacro{\helpend}{\pvar*\qvar}

\begin{document}

\resizebox{300 pt}{5 pt}{
\begin{tikzpicture}
\foreach \i in {0,...,\qvar}
      \foreach \l in {0,...,\pvar}
      {     
          \pgfmathsetmacro{\leftsside}{\pvar*\i+\qvar*\l}     
          \ifthenelse{\leftsside<\helpend}
            {\filldraw[blue] (\pvar*\i+\qvar*\l + \pvar*\qvar,0) circle (5pt);  }
            {}          
      }
\end{tikzpicture}
}
\end{document}

If I replace in the loop \leftsside<\helpend by 1<\helpend, it works nicely, but is not the desired outcome. How do I fix it?

I already tried the \breakforeach suggestion from tex.stackexchange.com/questions/39476/while-loops-in-tikz and could not make it work. I liked the idea of solution (to pre-compute an array) of John Kormylo from tex.stackexchange.com/questions/187482/if-then-else-inside-tikz-graph/187514 but I have to admit that I do not know enough to successfully implement it.

MRH
  • 23

1 Answers1

1

You need to remove the trailing .0, which can be achieved by replacing \pgfmathsetmacro by \pgfmathtruncatemacro or using int.

\documentclass{amsart} 
\usepackage{ifthen}
\usepackage{tikz} 

\pgfmathsetmacro{\pvar}{4} 
\pgfmathsetmacro{\qvar}{5} 
\pgfmathsetmacro{\helpend}{\pvar*\qvar}

\begin{document}

\resizebox{300 pt}{5 pt}{
\begin{tikzpicture}
\foreach \i in {0,...,\qvar}
      \foreach \l in {0,...,\pvar}
      {     
          \pgfmathtruncatemacro{\leftsside}{\pvar*\i+\qvar*\l}     
          \ifthenelse{\leftsside<\helpend}
            {\filldraw[blue] (\pvar*\i+\qvar*\l + \pvar*\qvar,0) circle (5pt);  }
            {}          
      }
\end{tikzpicture}
}
\end{document}

enter image description here

You do not need ifthen here.

\documentclass{amsart} 
\usepackage{tikz} 

\pgfmathsetmacro{\pvar}{4} 
\pgfmathsetmacro{\qvar}{5} 
\pgfmathtruncatemacro{\helpend}{\pvar*\qvar}

\begin{document}

\resizebox{300 pt}{5 pt}{
\begin{tikzpicture}
\foreach \i in {0,...,\qvar}
      \foreach \l in {0,...,\pvar}
      {     
          \pgfmathtruncatemacro{\leftsside}{\pvar*\i+\qvar*\l}     
          \ifnum\leftsside<\helpend
            \filldraw[blue] (\pvar*\i+\qvar*\l + \pvar*\qvar,0) circle[radius=5pt];  
          \fi
      }
\end{tikzpicture}
}
\end{document}

and could also get rid of the \pgfmathtruncatemacros.

\documentclass{amsart} 
\usepackage{tikz} 

\pgfmathsetmacro{\pvar}{4} 
\pgfmathsetmacro{\qvar}{5} 

\begin{document}

\resizebox{300 pt}{5 pt}{
\begin{tikzpicture}
\foreach \i in {0,...,\qvar}
      \foreach \l in {0,...,\pvar}
      {     
          \pgfmathtruncatemacro{\leftsside}{\pvar*\i+\qvar*\l}     
          \ifnum\numexpr\pvar*\i+\qvar*\l<\numexpr\pvar*\qvar
            \filldraw[blue] (\pvar*\i+\qvar*\l + \pvar*\qvar,0) circle[radius=5pt];  
          \fi
      }
\end{tikzpicture}
}
\end{document}
  • Thank you for the simple multiple solutions! This was what I'm looking for and am able to understand well. The truncation was not a surprise, but I had no clue how to do it. Could you additionally point me to a good website/book/resource with the \ifthenelse, \ifnum etc functions? (I am not able to find one, but to be honest I don't know even what to look for). – MRH Jun 05 '20 at 19:54
  • 1
    @Maria For the \ifnum stuff you can look up section 7 of TeX by topic (texdoc texbytopic). From this comment one may take that some others aren't too excited about the ifthen package, at least I am not. In particular, when you have pgf at your disposal, you can always use the ifthenelse function to define some integers that can be used in \ifnum or \ifcase structures. Unlike \ifthenelse, they can even be used inside paths. –  Jun 05 '20 at 20:01
  • Is there a way to make \ifthenelse{\i<\qvar \OR \l<\pvar}{ \filldraw[blue] (\pvar*\i+\qvar*\l,0) circle (5pt);}{} work with \ifnum? (in the double loop) Otherwise I'm stuck with `\ifthen'... – MRH Jun 05 '20 at 20:54
  • 1
    @Maria Sure: \documentclass[tikz,border=3mm]{standalone} \begin{document} \begin{tikzpicture} \pgfmathtruncatemacro{\pvar}{4} \pgfmathtruncatemacro{\qvar}{5} \foreach \i in {0,...,\qvar} \foreach \l in {0,...,\pvar} { \pgfmathtruncatemacro{\itest}{(\i<\qvar|| \l<\pvar)} \ifnum\itest=1 \filldraw[blue] (\pvar*\i+\qvar*\l + \pvar*\qvar,0) circle[radius=5pt]; \fi } \end{tikzpicture} \end{document}. –  Jun 05 '20 at 20:59
  • This again begs a question: how do you know how to write \AND in tikz??? Where can I get this knowlegde from??? (In the book TeXbyTopic I don't see a chapter on logic functions, I really tried to find it) – MRH Jun 05 '20 at 21:03
  • 1
    @Maria These logical functions can be found on p. 1029 of pgfmanual v3.1.5. –  Jun 05 '20 at 21:05
  • Thank you so much for help again. But seriously you have to be joking! Again thanks for help, apparently tikz is not for me. – MRH Jun 05 '20 at 21:13
  • 1
    @Maria All I was trying to do is to recommend not to use ifthen. I was not really joking. But I can see how this can be overwhelming. At the very least I recommend switching to xifthen (but I personally do not use this one either). –  Jun 05 '20 at 21:19