I have a bibtex .bst file that issues something like:

"\MR{" mrnumber "}" * *

in order to produce things like:

\MR{MR123456789 (99 \#9999)}



inside the .bbl file.

This is then parsed by one of the many MR implementations out there to produce a little URL to the review:


Unfortunately, it no longer works too reliably as some records I am getting now look like:

\MR{123456789 (99 \#9999)}



without the inital MR.

In terms of (perl compatible) regexes I want to capture $1 from /^(?:MR)([0-9]*)[^0-9]*.*$/.

What sort of magic do I put in the \def in order to omit an optional leading "MR" and optional trailing garbage begun by a non-numeric character?


You need to process this input in two stages. Something like this should do the trick:

\def\checkMR MR#1#2#3 #4\relax%
\def\MR#1{\checkMR MR#1 \relax}

If the input is


the macros \checkMR gets fed these arguments:

 \checkMR MRMR10000\relax

in that case, #1 is M, #2 is R, and the number is in argument #3 (because \checkMR is a delimited macro whose call has to start with MR, so the first MR appearance is deleted by the macro scanner inside TeX).

If the input is


the number is all three arguments concatenated, because it expands to \checkMR MR1000\relax. The \ifx tests in \checkMR differentiate between those cases.

The \relax is there only to end \checkMR argument scanning. It could be anything, as long as it does not appear in the original \MR argument at all.

Edit: sorry, I missed the space problem originally, I have adjusted the macro definitions to handle that as well. As an exercise to the reader: see if you configure out how that works on your own :)

  • Finally, testing for the [0-9] set is (I think) just too hard in TeX macros. If you really need that, I suggest running an external perl script to fix the input Aug 16 '10 at 22:17
  • Thanks sounds pretty good. I understand it, understand its limitations, it has no side-effects, and it happens to work on the 50 or so actual references it needed to work on, so sounds good. Aug 17 '10 at 2:46

The following seems to do what you want.

\def\MR#1{\futurelet\lettoken\checkMR#1 \endMRgarbage}
        \if\lettoken M\expandafter\doMR
        \else \expandafter\doMR\expandafter M\expandafter R%
\def\doMR MR{%


\MR{12345689 (99 \#9999)}


The three example expand to MR10000, MR123456789, and MR232. (Actually, it's not just expansion due to assignments, but that's the basic idea.) It should be easy enough to change the definition of \skipMRgarbage to produce an \href or whatever else you want.

  • +1. Thanks, I think I understand this one, and it will probably help in the future, but I'm going with the other answer for now because it has no side-effects. Aug 17 '10 at 2:47

Or you could just clean your bibtex and cut all MR numbers down to just the first numerical component, e.g. 123456789 instead of MR123456789 (23423). That is all that really matters about a MR number and is more than sufficent to generate a unique link to the item.

I just keep my bib file clean in that fashion and then define the following two macros right before the bibliography for those classes that don't provide them already:

\newcommand{\MR}[1]{\MRhref{#1}{MR #1}}



I can't resist: a LuaTeX solution. The \bgroup/\catcode is not necessary if you put the definition in a separate lua-file, wich I'd always recommend. See that Lua has regular expressions (although limited compared to perl).

  local str = ('#1'):gsub("^%a?%a?(%d+) ?.*$","%1")


\MR{MR123456789 (99 \#9999)}

\MR{123456789 (99 \#9999)}


yields 4 times 123456789

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.