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 saw these command when i searching sth. I wonder if we can increase numbers if conditions.

it's like if{0} = a, if{1}=b, if{2}=c, if{3}=d etc.

\newcommand{mycommand}[1]{
\if{#1 != 0}
Some text, because it's nonzero
\else
Some other text, because it's zero
\fi
}

\mycommand{0}
\mycommand{1}
share|improve this question
1  
TeX has the \ifnum conditional (and also \ifcase), but you may be better off using etoolbox's arithmetic tests. –  Nicola Talbot Dec 17 '13 at 18:28
add comment

2 Answers

up vote 8 down vote accepted

You can do much better: a \newcasecommand that can deal with arbitrary labels, not only numbers.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\newcasecommand}{ m m O{No~default~value} }
 {
  \cs_new_protected:Npn #1 ##1
   {
    \str_case:nnF { ##1 } { #2 } { #3 }
   }
 }
\ExplSyntaxOff

\newcasecommand{\mycommand}{%
 {0}{The number is zero}
 {1}{The number is one}
 {x}{We have x}
}[Boo!]

\begin{document}

\mycommand{0}

\mycommand{1}

\mycommand{x}

\mycommand{foo}

\end{document}

The syntax is

\newcasecommand{<command>}{
  {<string-a>}{<text for case a>}
  {<string-b>}{<text for case b>}
  ...
 }[<text for no match>]

The final argument is optional; the default is printing an informative text, which could be turned into a warning.

enter image description here

share|improve this answer
    
thats exacly what i need. –  mert Dec 17 '13 at 18:48
add comment

You might like \ifcase ...\or...\fi for this situation- here's a complete example to play with

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}

\newcommand{\mycommand}[1]{%
    \ifcase#1
    Some text, because it's zero
    \or 
    Some other text, because it's one
    \or
    Some other text, because it's two
    % keep going if you need 3, 4, 5, etc
    \fi
}

\begin{document}
\mycommand{0}\par
\mycommand{1}\par
\mycommand{2}
\end{document}
share|improve this answer
    
+1. thanks for your interest. –  mert Dec 17 '13 at 18:47
add comment

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.