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

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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
  }
share|improve this answer

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.

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

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.