4

This question already has an answer here:

I have an error when I take a non-integer value for may argument:

\documentclass{article}

\def\marks#1{{%
\hskip20pt plus 1 fil (#1 mark\ifnum#1=1\else s\fi)
\parfillskip=0pt\par}}

\begin{document}

Your mark is: \marks{5}  % fine 
Your mark is: \marks{1}  % fine 
Your mark is: \marks{0.5}   % error !

\end{document}

marked as duplicate by Werner macros May 16 '16 at 17:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    \ifnum is only meant for integer numbers, not for floating points. – user31729 May 16 '16 at 15:49
6

Classic ways to do this would be to use \ifdim

\ifdim#1pt=1pt \else s\fi

or \ifx

\def\tempa{#1}\tempb{1}\ifx\tempa\tempb\else s\fi
5

\ifnum can only be used for integer numbers in the range -231-1 to 231-1. Dimensions can be used for numbers with decimal point, also with limited range (\maxdimen = 16383.99999 pt) and precision (the smallest unit is 1 sp = 2-16 pt).

Example:

\documentclass[a5paper]{article}

\def\marks#1{{%
\hskip20pt plus 1 fil (#1 mark\ifdim#1pt=1pt \else s\fi)
\parfillskip=0pt\par}}

\begin{document}

Your mark is: \marks{5}  % fine
Your mark is: \marks{1}  % fine
Your mark is: \marks{0.5}   % fine, now

\end{document}

Result

4

\ifnum can't be used with floating point numbers, i.e. it is meant for integer values only.

There are some ways: Using a \ifdim by-pass (like in the answers by David Carlisle and Heiko Oberdiek) or the expl3 style with \fp_compare:nNnF which checks for the floating point 'entity'

This is easily achieved and can be extended, if needed, for more complicate issues, i.e. adding/multiplying marks etc.

\documentclass{article}


\usepackage{expl3}

\ExplSyntaxOn
% expl3 direct access
\cs_new:Nn \check_marks:n{ 
  \fp_compare:nNnF {#1} = {1} {s}
}

% expl3 wrapper/importer macro that can be used outside of \ExplSyntaxOn...\ExplSyntaxOff
\newcommand{\checkmarks}[1]{%
  \check_marks:n{#1}
}

\ExplSyntaxOff

\def\marks#1{{%
    \hskip20pt plus 1 fil (#1 mark\checkmarks{#1})
    \parfillskip=0pt\par}
}

\begin{document}

Your mark is: \marks{5}  % fine 
Your mark is: \marks{1}  % fine 
Your mark is: \marks{0.5}   % fine
Your mark is: \marks{1.4141362}  % fine 

\end{document}

enter image description here

  • As you use xparse you coud use NewDocumentCommand ? – Tarass May 16 '16 at 16:04
  • @Tarass: Well, I restricted to \newcommand – user31729 May 16 '16 at 16:05
  • What does that mean ? For LaTeX reasons or personal ? – Tarass May 16 '16 at 16:06
  • @Tarass: The switch to expl3 is confusion enough ;-) – user31729 May 16 '16 at 16:07
  • Why not the simpler \fp_compare:nF {#1 = 1} {s}? The abundance of braces tends to confuse me. – Henri Menke May 16 '16 at 22:05

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