5

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}
\addbibresource{\jobname.bib}

% 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.

lockstep
  • 250,273
Simon Byrne
  • 2,905

2 Answers2

4

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
  }
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
4

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.

egreg
  • 1,121,712