2

I am modifying a correctly functioning class file to add some capabilities. I need to compare two strings -
a) \@stringtextfromuser and
b) ABC.

I have loaded etoolbox, and xtsring and have tried several possibilities:

  1. \ifstrequal{\@stringtextfromuser}{ABC}{If equal}{If unequal}.

  2. \myifstrequal{\@stringtextfromuser}{ABC}{If equal}{If unequal}, where \newcommand\myifstrequal{\expandafter\ifstrequal\expandafter}.

  3. \IfStrEq{\@stringtextfromuser}{ABC}{If equal}{If unequal}.

  4. \ifdefstring{\@stringtextfromuser}{ABC}{If equal}{If unequal}.

None of these seem to be doing anything (the code inside the true cases is not getting executed when it should). I know that LaTeX is a little finicky about string comparisons, but I am sure I am missing something. It can't be this complicated.

  • You seem to be comparing \@stringtextfromuser with ABC, which is always going to be false. Presumably you mean to compare the expansion of \@stringtextfromuser with ABC? – Joseph Wright Jun 25 '15 at 9:32
  • Yes, I need the expansion, hence my attempts 2, 3 and 4. – user2751530 Jun 25 '15 at 10:19
  • \def\@next{ABC} \ifx\@stringtextfromuser\@next …? Or are there special characters inside the strings? – GuM Jun 25 '15 at 10:37
  • There are no special characters anywhere. These are simple alphabetic strings. – user2751530 Jun 25 '15 at 10:40
  • Do you need an expandable approach? If not, the usual is simply (\def\@tempa{ABC}\ifx\@tempa\@stringtextfromuser <stuff>\else <other stuff>\fi. – Joseph Wright Jun 25 '15 at 12:10
3

The example with \IfStrEq works, provided you have not tampered with its default setting of \expandargs:

\documentclass{article}

\usepackage{xstring}

\makeatletter
\newcommand\userinput[1]{\def\@stringtextfromuser{#1}}

\newcommand{\decide}[3]{%
  \IfStrEq{\@stringtextfromuser}{#1}{#2}{#3}%
}
\makeatother

\begin{document}
\userinput{ABC}

\decide{ABC}{EQUAL}{UNEQUAL}

\decide{DEF}{EQUAL}{UNEQUAL}
\end{document}

enter image description here

If you set \noexpandarg (there are good reasons for doing so), then

\newcommand{\decide}[3]{%
  \expandafter\IfStrEq\expandafter{\@stringtextfromuser}{#1}{#2}{#3}%
}

will do.

An almost package free solution can use \pdfstrcmp:

\documentclass{article}

\usepackage{pdftexcmds}

\makeatletter
\newcommand\userinput[1]{\def\@stringtextfromuser{#1}}

\newcommand{\decide}[3]{%
  \ifnum\pdf@strcmp{\@stringtextfromuser}{#1}=\z@ #2\else#3\fi
}
\makeatother

\begin{document}
\userinput{ABC}

\decide{ABC}{EQUAL}{UNEQUAL}

\decide{DEF}{EQUAL}{UNEQUAL}
\end{document}

Loading pdftexcmds is better because so the same code will also work with XeLaTeX and LuaLaTeX. Note that pdftex has the primitive \pdfstrcmp, so in case you plan only using this engine, the package is not necessary and \pdf@strcmp becomes \pdfstrcmp.

  • I have not used \noexpandarg, but \IfStrEq does not work. – user2751530 Jun 28 '15 at 15:28
  • @user2751530 Without a minimal example it's just guesswork. – egreg Jun 28 '15 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.