Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Using etoolbox, ifthen, or another form of conditional checking, how can I check if a value is a multiple of 4? E.g. if the number is 4, 8, 12, 16, etc., then it returns "true".

share|improve this question
add comment

6 Answers

up vote 15 down vote accepted

You can use pgfmath for this:

\documentclass{article}
\usepackage{pgf}

\begin{document}
\newcommand\ifismultiple[4]{%
    \pgfmathparse{mod(#1,#2)==0}
    \ifnum \pgfmathresult=1
    #3%
    \else
    #4%
    \fi
}
\ifismultiple{15}{4}{true}{false}
\end{document}
share|improve this answer
4  
If you swap the first two arguments, then you can define a new command \newcommand\ifismultipleoffour{\ifismultiple{4}}, etc. –  Aditya Dec 19 '11 at 5:01
add comment

\ifodd is enough to do this

\documentclass[]{scrartcl} 

\newcount\myint

\def\IsFourMultiple#1{% 
\myint#1\relax  
\ifodd\myint 
   false
\else 
  \divide\myint by2\relax
  \ifodd\myint
    false 
  \else 
    true
  \fi
\fi}% 

\begin{document} 

\IsFourMultiple{4}    
\IsFourMultiple{32} 
\IsFourMultiple{15} 

\end{document}
share|improve this answer
    
Ah, that's very clever! –  Jake Dec 19 '11 at 6:05
    
@Jake But my first idea was to use pgfmath' like you, because I always have \usepackage{tikz}` in my preamble. Your solution is more general. –  Alain Matthes Dec 19 '11 at 6:44
    
+1 for cleverness –  Marco Daniel Dec 19 '11 at 7:57
1  
One drawback (shared by many of the solutions here) is that it is not expandable. –  Bruno Le Floch Dec 19 '11 at 12:39
    
@BrunoLeFloch Yes but it is sometimes a desirable and useful feature but it's not always desirable. Now I would like to know what is the cost in terms of efficiency. –  Alain Matthes Dec 19 '11 at 13:05
show 4 more comments

With eTeX's \numexpr, you can do that easily.

\makeatletter
\newcommand{\doifmultipleof}[2]{%
  \ifnum\numexpr((#2)/(#1))*(#1)-(#2)=0
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}
\makeatother

\documentclass{article}
\begin{document}
  \doifmultipleof{4} {6}{true}{false} % => false
  \doifmultipleof{5}{10}{true}{false} % => true
\end{document}
share|improve this answer
1  
One should note that e-TeX's division rounds, so this method is not suitable to compute the remainder. This doesn't invalidate this solution, of course, as the difference between a multiple of 4 and a non-multiple of 4 can never be 0. –  egreg Dec 19 '11 at 10:18
    
@egreg. Yes, and the LaTeX3 function \int_mod:nn uses some very clever code (by Heiko I believe) to work around that, while avoiding overflow in all cases. –  Bruno Le Floch Dec 19 '11 at 12:38
add comment

If you can use luatex, then the comparison can be easily done at the lua end. Here is a ConTeXt solution (using named macro arguments):

% macros=mkvi

\def\doifmultipleof#{mod}#{num}%
     {\ctxcommand{doifelse(math.mod(#{num}, #{mod}) == 0)}}

\starttext
\dorecurse{10}
  {\doifmultipleof{4}{\recurselevel}{yes}{no}}
\stoptext        
share|improve this answer
    
+1 because this is the only expandable answer so far (it is expandable, right?). But I have to say, I find the named macro argument syntax quite outlandish. :) –  Bruno Le Floch Dec 19 '11 at 5:02
    
@BrunoLeFloch: Yes, the code is expandable. As for the named arguments, one can also use #mod and #num; in fact, those are the recommended alternatives, but I prefer the #{...} variants. –  Aditya Dec 19 '11 at 6:09
    
just curious: with the #mod variant, how is the end of the argument name detects? Can you have delimited arguments that way? –  Bruno Le Floch Dec 19 '11 at 6:31
add comment
\documentclass{minimal} 
\def\IsFourMultiple#1{% 
  \ifdim\dimexpr#1pt/4=\numexpr#1/4pt true\else false\fi}
\begin{document} 

\IsFourMultiple{4}    
\IsFourMultiple{32} 
\IsFourMultiple{15} 

\end{document}
share|improve this answer
    
\IsFourMultiple{2147483647} –  Andrey Vihrov Dec 19 '11 at 8:21
2  
every solution has a limit! not really surprising ... ;-) –  Herbert Dec 19 '11 at 9:15
1  
Interesting approach. If eTeX is available, you'd better do \ifdim\dimexpr#1pt/4=\numexpr#1/4pt : that makes your macro expandable, and probably a tiny bit faster. –  Bruno Le Floch Dec 19 '11 at 13:37
    
true, thanks ... –  Herbert Dec 19 '11 at 16:09
add comment

The fp package can be used to calculate stuff and perform modular arithmetic. Here's a minimal example:

enter image description here

\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\newcommand{\multoffour}[1]{%
  \FPeval{\result}{trunc(#1-(4*trunc(#1/4,0)),0)}% \result contains #1 mod 4
  \ifnumequal{\result}{0}{true}{false}% true = multiple of four; false = not a multiple of four
}
\begin{document}
1 is a multiple of~4: \multoffour{1} \par
2 is a multiple of~4: \multoffour{2} \par
3 is a multiple of~4: \multoffour{3} \par
4 is a multiple of~4: \multoffour{4} \par
5 is a multiple of~4: \multoffour{5} \par
6 is a multiple of~4: \multoffour{6} \par
7 is a multiple of~4: \multoffour{7} \par
8 is a multiple of~4: \multoffour{8} \par
9 is a multiple of~4: \multoffour{9} \par
\end{document}

etoolbox was used for numeric comparisons.


This works just as well with counters. For example, using

\newcounter{mycntr} \setcounter{mycntr}{10}
\multoffour{\themycntr}

(provided that \themycntr is defined using \arabic{mycntr} or not modified).

share|improve this answer
    
Isn't fp package (or, for that matter, any package) an overkill for this task? And is this fp solution expandable? fp solutions are hardly expandable. For non-context and non-lua users, I think Bruno's solution is more than enough, IMHO. And it's superfast. I often get scared by fp's trace log :) –  Ahmed Musa Dec 19 '11 at 16:46
2  
@AhmedMusa: Sure, there are at least three other answers that produce a package-less solution. Honestly, speed for me is not an issue, since the time difference between fp's output and some other package-less output is most likely less than 0.1 seconds. I rely on code readability, which fp provides through its function shorthand/names. If I (or anyone for that matter) is relying on performing major calculations within TeX and want speed, they are perhaps better off using a different environment altogether. ;) –  Werner Dec 19 '11 at 16:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.