# Biblatex: ifx inside DeclareFieldFormat

I'm not having any luck using an \ifx command inside a \DeclareFieldFormat block with biblatex (v1.6). Is there something I'm missing? A minimal (non-)working example:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@misc{ref1,
title = {Title1},
eprint = {MR001},
eprinttype = {mrnumber}}
@misc{ref2,
title = {Title},
eprint = {002},
eprinttype = {mrnumber}}
@misc{ref3,
title = {Title},
eprint = {MR003 (aa)},
eprinttype = {mrnumber}}
@misc{ref4,
title = {Title},
eprint = {004 (bb)},
eprinttype = {mrnumber}}
\end{filecontents}
\usepackage{biblatex}

% based on code from http://tex.stackexchange.com/q/1856/245
\def\checkMR MR#1#2#3 #4\relax%
{\ifx#1M%
\ifx#2R%
#3%
\else%
#1#2#3%
\fi
\else
#1#2#3%
\fi}
\def\MR#1{\checkMR MR#1 \relax}

\DeclareFieldFormat{eprint:mrnumber}{%
\MR{#1}}

\begin{document}
It works here \MR{MR001}, \MR{002}, \MR{MR003 (aa)}, \MR{004 (bb)}

\cite{ref1}, \cite{ref2}, \cite{ref3}, \cite{ref4}
\printbibliography
\end{document}


It evaluates correctly in the body, but as false in the bibliography field.

-

This is happening because you are using the \ifx test, which will only be true if the category codes of M and R are 'letter'. biblatex is passing the argument as a detokenized string, so the test fails. What you want here is to compare by character code: use the \if test.

\def\checkMR MR#1#2#3 #4\relax
{%
\if#1M%
\if#2R%
#3%
\else
#1#2#3%
\fi
\else
#1#2#3%
\fi
}

-

It's possible also to avoid a conditional:

\def\checkMR MR#1#2#3 #4\relax%
{\ifnum\pdfstrcmp{#1#2}{MR}=0
#3%
\else
#1#2#3%
\fi}


If one wants compatibility with XeLaTeX and LuaLaTeX, then

\usepackage{pdftexcmds}
\makeatletter
\def\checkMR MR#1#2#3 #4\relax%
{\ifnum\pdf@strcmp{#1#2}{MR}=\z@
#3%
\else
#1#2#3%
\fi}
\makeatother


\pdfstrcmp{<A>}{<B>} expands to 0 if the two strings <A> and <B> are equal (after expansion). Unfortunately it's called \strcmp in XeTeX and is missing in LuaTeX, but H. Oberdiek's pdftexcmds package comes to the rescue.

-
Of course, this needs pdfTeX >= 1.30, or XeTeX >= 0.9994. –  Joseph Wright Sep 12 '11 at 20:26