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 want to test if the expansion of a given macro, say \mystring, is equal to one of two given strings, say string1, string2. I guess it's possible to do this with etoolbox, with something like:

\ifstrequal{\mystring}{string2 or string3}{code for True}{code for False}

How? Or, is there another possible method?

share|improve this question

2 Answers 2

up vote 4 down vote accepted
\usepackage{etoolbox}

\def\mytest#1{%
  \def\tempa{string1}%
  \def\tempb{string2}%
  \ifboolexpr{test {\ifdefstrequal{#1}{\tempa}} or 
              test {\ifdefstrequal{#1}{\tempb}}}
  {True}
  {False}%
}

\mytest\relax % <- False
\def\xyz{string1}
\mytest\xyz % <- True
\def\xyz{string2}
\mytest\xyz % <- True
\def\xyz{string3}
\mytest\xyz % <- False

EDIT

A shorter implementation with expl3:

\usepackage{expl3}
\ExplSyntaxOn
\cs_new:Npn \mytest #1
  {
   \bool_if:nTF
     { \str_if_eq_p:no { string1 } { #1 } || \str_if_eq_p:no { string2 } { #1 } }
     {True}
     {False}
  }
\ExplSyntaxOff

This has some advantages over the solution with etoolbox: it's more easily extendable to do other checks and doesn't require defining temporary control sequences for storing the strings to check. But the main advantage is that this \mytest is expandable, while the one with etoolbox isn't.

share|improve this answer
    
Thanks. This is exactly what I was looking for. –  agodemar Oct 4 '11 at 21:30
    
+1 for the LaTeX3 code. –  agodemar Mar 19 '12 at 22:07

If you are not stuck on using etoolbox, you could use IfStrEqCase from the xstring package. This has the added benefit that in case where you want to allow for numerical equivalency (ie., "0.5" matches ".50") you can use identical syntax with IfEqCase.

Using these macros directly allows you to specify different code to be executed depending on which string was matched. If you just want to execute a particular piece of code for the case where thse is a match I have defined the \StrEqEither macro for you.

\documentclass{article}
\usepackage{xstring}

\begin{document}
This tests for string matches (i.e., $+3$ not equal $3$):\par
\smallskip
\IfStrEqCase{a}{{a}{matched case 1}{b}{matched case 2}}[matched none]\par
\IfStrEqCase{b}{{a}{matched case 1}{b}{matched case 2}}[matched none]\par
\IfStrEqCase{c}{{a}{matched case 1}{b}{matched case 2}}[matched none]\par
\IfStrEqCase{+3}{{3}{matched case 1}{5}{matched case 2}}[matched none]\par
\IfStrEqCase{0.5}{{0}{matched case 1}{.50}{matched case 2}}[matched none]\par

\bigskip% 
This test allows for case where numerically equivalent:\par
\smallskip
\IfEqCase{a}{{a}{matched case 1}{b}{matched case 2}}[matched none]\par
\IfEqCase{b}{{a}{matched case 1}{b}{matched case 2}}[matched none]\par
\IfEqCase{c}{{a}{matched case 1}{b}{matched case 2}}[matched none]\par
\IfEqCase{+3}{{3}{matched case 1}{5}{matched case 2}}[matched none]\par
\IfEqCase{0.5}{{0}{matched case 1}{.50}{matched case 2}}[matched none]\par


\newcommand*{\StrEqEither}[5]{% {string}{target1}{target2}{code for match}{code for no match}
    \IfEqCase{#1}{{#2}{#4}{#3}{#4}}[#5]
}%

\bigskip
This is test using a macro (allows for numerically equivalent):\par
\smallskip
\StrEqEither{a}{a}{b}{matched}{no match}\par
\StrEqEither{b}{a}{b}{matched}{no match}\par
\StrEqEither{c}{a}{b}{matched}{no match}\par
\StrEqEither{+3}{3}{5}{matched}{no match}\par
\StrEqEither{0.5}{0}{.50}{matched}{no match}\par
\end{document}
share|improve this answer
    
Thanks for the long example. I'll definitely look into xstring. –  agodemar Oct 4 '11 at 21:27

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.