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.

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}
share|improve this question
2  
Welcome to TeX.SX! End of lines count as spaces. You have to protect them with % (except the last). –  egreg Sep 1 at 15:38
    
Thanks, I have added the % and all is now aligned. –  Peter Sloan Sep 1 at 15:40

1 Answer 1

up vote 10 down vote accepted

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}}
    {42}{\emph{the answer}}
   }
   {\textbf{A weird value}}
 }
\ExplSyntaxOff
share|improve this answer
4  
+1 (as you need the rep) –  David Carlisle Sep 1 at 16:01

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.