4

Following the solution from this question as a guide - Are there any "if" commands like "\ifnum" in LaTeX? - I have setup a newcommand to perform a string comparison. Unfortunately I can't get greek characters from the textgreek package to compare.

The following MWE shows that a math mode alpha can be compared, but because my Greek characters are part of protein names I can't use math mode definitions because of the italics. I can't override the math mode definitions to remove the italics because I still want this when I actually use mathematical formulas.

I would like either:

  1. a greek character alternative
  2. a string comparison that will work with textgreek

MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\setlength\parindent{0pt}
\usepackage{siunitx,microtype,textcomp,textgreek}

\newcommand{\evaltest}[2]{%
    \ifnum\pdfstrcmp{#1}{#2}=0
        #1 equals #2%
    \else
        #1 does not equal #2%
    \fi
}

\begin{document}
    \begin{itemize}
        \item \evaltest{a}{b}
        \item \evaltest{a}{a}
        \item \evaltest{a a}{a a}
        \item $\alpha$
        \item \evaltest{$\alpha$}{$\alpha$}
        \item \textalpha
        %\item \evaltest{\textalpha}{\textalpha}
    \end{itemize}
\end{document} 
3

The \pdfstrcmp primitive performs expansion: the linked answer does mention that the argument needs to be a 'string'. For comparing arbitrary input, prevent expansion using \unexpanded:

\documentclass{article}
\usepackage[utf8]{inputenc}
\setlength\parindent{0pt}
\usepackage{siunitx,microtype,textcomp,textgreek}

\newcommand{\evaltest}[2]{%
    \ifnum\pdfstrcmp{\unexpanded{#1}}{\unexpanded{#2}}=0
        #1 equals #2%
    \else
        #1 does not equal #2%
    \fi
}

\begin{document}
    \begin{itemize}
        \item \evaltest{a}{b}
        \item \evaltest{a}{a}
        \item \evaltest{a a}{a a}
        \item $\alpha$
        \item \evaltest{$\alpha$}{$\alpha$}
        \item \textalpha
        \item \evaltest{\textalpha}{\textalpha}
    \end{itemize}
\end{document} 

(The expansion is much like \edef so anything that is safe in the latter is also safe in \pdfstrcmp. That can be useful but it's not here!)

  • Thanks for your answer. I did notice in the linked question that the answer said it compared strings, but it was this part of the answer that I was getting stuck on: In the pseudocodes above "string1" and "string2" can be also macros. I was attributing \textalpha the status of a macro that would be expanded as the way I understood it, just like \edef. – EngBIRD Mar 3 '15 at 22:33
  • @EngBIRD They can be macros but only if they expand safely inside an \edef. Like many TeX macros, that's not the case for \textalpha. – Joseph Wright Mar 4 '15 at 7:21
1

Does it have to be pdfstrcmp? If not, you should look into etoolbox. E.g.:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\setlength\parindent{0pt}
\usepackage{etoolbox}
\usepackage{siunitx,microtype,textcomp,textgreek}

\newcommand{\evaltest}[2]{%
    \ifnum\pdfstrcmp{#1}{#2}=0
        #1 equals #2%
    \else
        #1 does not equal #2%
    \fi
}

\newcommand{\etest}[2]{%
  \ifdefequal{#1}{#2}{#1 equals #2}{#1 does not equal #2}}

\begin{document}
    \begin{itemize}
        \item \evaltest{a}{b}
        \item \evaltest{a}{a}
        \item \evaltest{a a}{a a}
        \item $\alpha$
        \item \evaltest{$\alpha$}{$\alpha$}
        \item \textalpha
        \item \etest{\textalpha}{\textalpha}
        \item \etest{\textalpha}{\textbeta}
        \item \etest{\textbeta}{\textbeta}
    \end{itemize}
\end{document}
  • Thanks for your answer. I am willing to switch to ifdefequal. Can this function be made more robust however? While it technically answers my question, I didn't expect the answer to break some currently successful comparisons. As included in the itemized list any of the tests with a space, or in mathmode no longer work. – EngBIRD Mar 2 '15 at 5:45
  • @EngBIRD -- Oh, good point. The 'def' in \ifdefequal indicates it compares macro definitions. \textalpha is not the same as the string a a. I should've put more forethought into this answer..! – jon Mar 2 '15 at 6:00

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.