1

I have a problem about calculator package, I need check if #1 is a number or not, could I use conditional command? ex:

\def\CHECK#1{

% if #1 a number

\if...?

 #1 is a number

\else

 #1 is not a number

\fi

}

\CHECK{3}

\CHECK{n}
phanttam
  • 133
  • 1
  • 5
  • 2
    Welcome to TeX.SX! There's no need to 'shout' (i.e. using uppercase words ;-)) –  Feb 03 '16 at 21:43
  • number isn't unique -- I think, you mean an integer number? Knowing more of the context would be helpful. –  Feb 03 '16 at 21:53
  • It's the first time that I ask my question. I'm bad at english. Forgive me! Thanks, I can check \ifnum #1 = 0,1, 2 ... etc, but I can't check #1 is a interger or not. Which command if could I use? – phanttam Feb 03 '16 at 22:09
  • @phanttam: calculator seems to accept both integer and floating point numbers. As such, the linked post should suffice to check whether the input to calculator functions are (not) acceptable. – Werner Feb 03 '16 at 22:15

1 Answers1

1

This is perhaps too much, but I tried to use l3regex and checking whether the argument of #1 contains any non-digit using the 'negative class' [^0-9] as regular expression.

A better approach would use a conditional, but until the context isn't clear, I restrict to this solution.

\documentclass{article}


\usepackage{xparse}
\usepackage{l3regex}

\ExplSyntaxOn
\newcommand{\CHECK}[1]{
  \regex_match:nnTF {[^0-9]} {#1}{
    #1~is~not~an~integer}{
    #1~is~a~integer}
}% End of \CHECK definition
\ExplSyntaxOff

\begin{document}

\CHECK{1234234}

\CHECK{foo}

\CHECK{234.4523}


\end{document}

enter image description here

  • I need if #1 is not a integer number

    [latex]\sum\limits_{k=0}^{n}2^k=1+2+2^2+\ldots+2^{n-1}+2^n[/latex]

    else

    [latex]\sum\limits_{k=0}^{2016}2^k=1+2+2^2+\ldots+2^{2015}+2^2016 [/latex]

    I need check a number that I can

    [code]\def \SUM#1{

    % check #1 is a number or not, if #1 is a number

    $\sum\limits_{k=0}^{#1}2^k=1+2+2^2+\ldots+2^{\SUBTRACT{#1}{1} {\sol}\sol}+2^{#1}$ \else $\sum\limits_{k=0}^{n}2^k=1+2+2^2+\ldots+2^{n-1}+2^n$ \fi } \SUM{n} \SUM{2016} [/code]

    Which conditional command can use?

    – phanttam Feb 03 '16 at 22:26
  • @phanttam: Please add this to your question, it would clearify the question (and perhaps closing it) –  Feb 03 '16 at 22:27