if test for 1 not 11

Here is an \ifnum comparison that can handle non-numeric inputs. It does it by comparing 1 with 0#1. If #1 is a number, the \ifnum comparison works in the standard way, since the leading 0 does not affect the numerical value.

If #1 is not a number, then the \ifnum argument expansion stops after the 0 with a false comparison (having already printed out #1), and then prints out things.

\documentclass{article}
\newcommand{\sth}[1]{\textbf{(#1 \ifnum1=0#1\relax thing\else things\fi)}}
\begin{document}
\sth{1}  vs. \sth{2} vs. \sth{11} vs. \sth{no}
\end{document}

enter image description here


\pdfstrcmp of pdfTeX expands two token groups and compares them as strings. If they are equal, \pdfstrcmp expands to 0, which can be tested with \ifnum or \if:

\ifnum\pdfstrcmp{#1}{1}=0 % space ends the number zero here
  ...
\else
  ...
\fi

or

\if0\pdfstrcmp{#1}{1}%
  ...
\else
  ...
\fi

Package pdftexcmds' \pdf@strcmp hides the different names and methods of the different TeX engines (pdfTeX, XeTeX, LuaTeX). "Vanilla" TeX is not supported.

If #1 can contain fragile stuff, then the LaTeX way is a protected expansion, e.g.:

\protected@edef\param{#1}%
\def\one{1}%
\ifx\one\param
  ...
\else
  ...
\fi

Or if the argument should not be expanded at all:

\def\param{#1}%
\def\one{1}%
\ifx\param\one
  ...
\else
  ...
\fi

Tags:

Macros

Xifthen