# Macro definition: Ignoring leading “MR” and trailing garbage begun by non-numeric / Parsing the mrnumber returned by MathSciNet

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

"\MR{" mrnumber "}" * *


in order to produce things like:

\MR{MR123456789 (99 \#9999)}


or:

\MR{MR123456789}


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:

\href{http://www.ams.org/mathscinet-getitem?mr=123456789}{MR123456789}


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

\MR{123456789 (99 \#9999)}


or:

\MR{123456789}


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%
{\ifx#1M%
\ifx#2R\message{#3}\else\message{#1#2#3}\fi
\else
\message{#1#2#3}%
\fi}
\def\MR#1{\checkMR MR#1 \relax}


If the input is

 \MR{MR10000}


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

 \MR{10000}


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 –  Taco Hoekwater 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. –  Jack Schmidt Aug 17 '10 at 2:46

The following seems to do what you want.

\def\MR#1{\futurelet\lettoken\checkMR#1 \endMRgarbage}
\def\checkMR{%
\if\lettoken M\expandafter\doMR
\else \expandafter\doMR\expandafter M\expandafter R%
\fi
}
\def\doMR MR{%
\afterassignment\skipMRgarbage
\begingroup
\count255=%
}
\def\skipMRgarbage#1\endMRgarbage{%
MR%
\the\count255
\endgroup
}

\MR{MR10000}

\MR{12345689 (99 \#9999)}

\MR{MR323(99)}
\bye


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. –  Jack Schmidt Aug 17 '10 at 2:47

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

\bgroup
\catcode\%=11
\gdef\MR#1{
\directlua{
local str = ('#1'):gsub("^%a?%a?(%d+) ?.*\$","%1")
tex.sprint(str)
}}
\egroup

\MR{MR123456789}

\MR{MR123456789 (99 \#9999)}

\MR{123456789 (99 \#9999)}

\MR{123456789}


yields 4 times 123456789`

-

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}}

\newcommand{\MRhref}[2]{\href{http://www.ams.org/mathscinet-getitem?mr=#1}{#2}}

-