# White space problem using multiple \ifnumequal

I am trying to produce slightly different documents based on a single numerical input. But when I use multiple \ifnumequal commands to get the correct bit of text it produces really strange white space around the text. Can you help? I have tried (as below) one \ifnumequal after another, and in nested form, but still the white-space issue.

Here is a small example that reproduces the problem.

\documentclass{article}
\usepackage{etoolbox}
\newcounter{N}

\begin{document}

\setcounter{N}{1}
The counter N has been set to
\ifnumequal{\value{N}}{1}{\emph{one}}{}
\ifnumequal{\value{N}}{2}{\emph{two}}{}
\ifnumequal{\value{N}}{3}{\emph{three}}{}
which is nice.

\setcounter{N}{2}
The counter N has been set to
\ifnumequal{\value{N}}{1}{\emph{one}}{}
\ifnumequal{\value{N}}{2}{\emph{two}}{}
\ifnumequal{\value{N}}{3}{\emph{three}}{}
which is nice.

\setcounter{N}{3}
The counter N has been set to
\ifnumequal{\value{N}}{1}{\emph{one}}{}
\ifnumequal{\value{N}}{2}{\emph{two}}{}
\ifnumequal{\value{N}}{3}{\emph{three}}{}
which is nice.

\setcounter{N}{3}
The counter N has been set to
\ifnumequal{\value{N}}{1}{\emph{one}}{
\ifnumequal{\value{N}}{2}{\emph{two}}{
\ifnumequal{\value{N}}{3}{\emph{three}}{}}}
which is nice.

\end{document}

-
Welcome to TeX.SX! End of lines count as spaces. You have to protect them with % (except the last). – egreg Sep 1 '14 at 15:38
Thanks, I have added the % and all is now aligned. – Peter Sloan Sep 1 '14 at 15:40

In TeX end-of-lines count as spaces, so you get four of them instead of one, so you should type

The counter N has been set to
\ifnumequal{\value{N}}{1}{\emph{one}}{}%
\ifnumequal{\value{N}}{2}{\emph{two}}{}%
\ifnumequal{\value{N}}{3}{\emph{three}}{}
which is nice.


By the way, there are other methods for achieving the same result:

The counter N has been set to
\ifcase\value{N}\or
\emph{one}\or
\emph{two}\or
\emph{three}\fi
which is nice


The syntax is

\ifcase<number><text for case 0>\or<text for case 1>\or...\else<none of the above>\fi


and the \else part is optional.

Of course you want to hide the code in a macro:

\newcommand{\testcount}[1]{%
\ifnumequal{\value{#1}{1}{\emph{one}}{}%
\ifnumequal{\value{#1}{1}{\emph{two}}{}%
\ifnumequal{\value{#1}{1}{\emph{three}}{}%
}


and type The counter N has been set to \testcount{N} which is nice.

You can get a more flexible macro with expl3:

\usepackage{expl3}
\ExplSyntaxOn
\NewDocumentCommand{\testcount}{m}
{
\int_case:nnF { \value{#1} }
{
{1}{\emph{one}}
{2}{\emph{two}}
{3}{\emph{three}}