3

Is there a way to add arithmetic operations to \ifthenelse statements? Here is a minimal (non)example:

\documentclass{amsart} \usepackage{tikz, ifthen} \begin{document} \begin{tikzpicture} \foreach \a in {-6,...,6} { \foreach \b in {-6,...,6} { \ifthenelse{ (\a + \b) < 4 } { \node at ( (\a, \b ) {(\a, \b)}; } {} } } \end{tikzpicture} \end{document} 

It is not working because \ifthenelse does not allow \a+\b.

Bonus question: Is there a way to e.g. define \c = \a + \b in advance so I can use \c elsewhere, in \ifthenelse and subsequence \draw commands?

EDIT:

Actually what I needed is \a + 1.414*\b < 4 AND \a - 1.414*\b > 0. I thought I could handle the rest once I know how to insert arithmetic operations into \ifthenelse, but apparently it's more complicated than that. Apologies for the mixup and thanks for your help.

4
  • To set \c to \a + \b, just use \pgfmathtruncatemacro{\c}{\a+\b} (or \pgfmathsetmacro{\c}{\a+\b} if you need floating numbers instead of integers). Commented Nov 23 at 18:21
  • 1
    I would use pgfmathparse on a boolean, then \infum Commented Nov 23 at 18:39
  • I wrote an answer to your updated question, but realised your edit invalidates existing answers. please don't do this. instead, ask another question and link this one. if you do so, I will re-post my answer there. (I have deleted my answer here.) Commented Nov 24 at 5:05
  • @cfr: Apologies about the editing. I just created a new post linking this one. Thanks for your help. Commented Nov 24 at 6:51

2 Answers 2

7

For simple integer comparisons (which you seemed to need based on in your original question), using the relevant expl3 function with \UseName as wrapper is probably the easiest solution for this:

\documentclass{amsart} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach \a in {-6,...,6} { \foreach \b in {-6,...,6} { \UseName{int_compare:nNnT}{ \a + \b } < { 4 } { \node at ( (\a, \b ) {(\a, \b)}; } } } \end{tikzpicture} \end{document} 

For a more general solution that also allows for floating point numbers, use PGF's math capabilities:

\documentclass{amsart} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach \a in {-6,...,6} { \foreach \b in {-6,...,6} { \pgfmathsetmacro{\numc}{ \a + 1.414*\b < 4 && \a - 1.414*\b > 0 } \ifnum\numc=1\relax \node at ( (\a, \b ) {(\a, \b)}; \fi } } \end{tikzpicture} \end{document} 
3
  • Thanks @Jasper. Actually I need to handle floating points, and I have a boolean operation -- cf end the edited OP. How might I modify your codes to handle these? Apologies for not stating these at the outside (it didn't occur to me that floating & boolean would cause problems). Commented Nov 23 at 21:53
  • 1
    @underflow See my edit: This is the general way, you would solve such things using PGF's built-in math. Commented Nov 24 at 5:50
  • Two nice answers. +2 if I could. Commented Nov 24 at 15:36
3

Use \inteval (for integer) or \fpeval (for floating point) arithmetic.

\documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach \numA in {-6,...,6}{% \foreach \numB in {-6,...,6}{% \ifnum \inteval{\numA + \numB} < 4 \node at ( (\numA, \numB ) {$(\numA, \numB)$}; \fi }% }% \end{tikzpicture} \begin{tikzpicture} \foreach \numA in {-6,...,6}{% \foreach \numB in {-6,...,6}{% \edef\numC{\inteval{\numA + \numB}}% c = a + b \ifnum \numC < 4 \node at ( (\numA, \numB ) {$(\numA, \numB)$}; \fi }% }% \end{tikzpicture} \end{document} 

Also, try to avoid single-character macros like \a, \b, \c, as they are often used for other things.

2
  • Thanks@Werner. Actually what I needed is (\numA + 1.414*\numB) < 4 AND (\numA - 1.414*\numB)> 0. When I put in the decimals I got errors, and without the decmial \ifnum ignores the \and. What is the correct syntax for handling the decmial PLUS the \and? I have edited my post to reflect this. Thanks! Commented Nov 23 at 21:44
  • @underflow: \ifnum\fpeval{(\numA + sqrt(2) * \numB > 4) && (\numA - sqrt(2) * \numB < 0)} = 1 <true> \fi. Look at the xfp documentation for an overview of all the options provided. Commented Nov 24 at 17:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.