4

I am using biblatex with biber from TL2018 for my PhD thesis. Due to various reasons, I am forced to use Mendeley for citation managements and cannot switch to another tool now. Mendeley is capable of generating a bib file - but it is compatible with only the classic natbib/bibtex combo (although it correctly escapes special characters).

I understand that with biblatex/biber combo, the month field is not a string, but rather must be an integer. So, as expected and discussed here, I get the warning

 [784] Utils.pm:193> WARN - month field 'Apr' in entry 'coolauthor2012' is not an integer - this will probably not sort properly.

Taking inspiration from a tangentially related question here, I used the following month -> integer hand-conversion in the preamble.

\DeclareSourcemap{
    \maps[datatype=bibtex]{
        \map[overwrite]{
            \step[fieldsource=month, match={jan}, replace=${1}]
            \step[fieldsource=month, match={feb}, replace=${2}]
            \step[fieldsource=month, match={mar}, replace=${3}]
            \step[fieldsource=month, match={apr}, replace=${4}]
            \step[fieldsource=month, match={may}, replace=${5}]
            \step[fieldsource=month, match={jun}, replace=${6}]
            \step[fieldsource=month, match={jul}, replace=${7}]
            \step[fieldsource=month, match={aug}, replace=${8}]
            \step[fieldsource=month, match={sep}, replace=${9}]
            \step[fieldsource=month, match={oct}, replace=${10}]
            \step[fieldsource=month, match={nov}, replace=${11}]
            \step[fieldsource=month, match={dec}, replace=${12}]
        }
    }
}

I have two nagging questions now.

  1. Will this work? I don't see any more of the aforementioned warnings, but I wonder if the citations are correctly sorted, and the month replacement was indeed accounted for and handled correctly.
  2. Is there a better way to handle this problem? Clearly, hand-mapping each month to an integer is not efficient.

This will help all folks who want to use mendeley especially for re-using their existing mendeley library for manuscript submissions to publishers who are mostly stuck behind in natbib/bibtex era.

  • Note that month = {apr}, is also wrong for most classical BibTeX styles, they just won't complain as loudly as Biber. – moewe Jun 28 '18 at 14:59
  • @moewe what should it be instead? I only looked at couple of examples , 'jan' and 'feb' and proceeded with the conversion assuming that the month fields generated by mendeley shall always be three-character abbreviations. Am I wrong? Is apr the only exception? – Krishna Jun 28 '18 at 15:01
  • The three-letter abbreviations are valid but should be written without braces. So month = apr, would be correct. Naturally apr is not special and all months behave the same. – moewe Jun 28 '18 at 15:02
  • Ha. Got it.... I think I wasn't clear in the question. mendeley does indeed generate the month-fields in all entries in the exported bib file as {jan}, {feb}, {mar} and so on (with the braces). Also, I have no clue of using regexes. Does it mean that my replacement string is wrong? Should it be , replace=1, I also wonder that that $ is doing there. I'd appreciate some help in correctly formulating this month -> integer conversion command. – Krishna Jun 28 '18 at 15:06
  • Sorry, my comment was not clear. I was just saying that Mendeley is wrong to produce these strings with braces even for BibTeX. (This is contradicting your claim that this is (only) compatible with natbib/BibTeX - it usually isn't.) – moewe Jun 28 '18 at 15:08
7
\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map[overwrite]{
      \step[fieldsource=month, match=\regexp{\A(j|J)an(uary)?\Z}, replace=1]
      \step[fieldsource=month, match=\regexp{\A(f|F)eb(ruary)?\Z}, replace=2]
      \step[fieldsource=month, match=\regexp{\A(m|M)ar(ch)?\Z}, replace=3]
      \step[fieldsource=month, match=\regexp{\A(a|A)pr(il)?\Z}, replace=4]
      \step[fieldsource=month, match=\regexp{\A(m|M)ay\Z}, replace=5]
      \step[fieldsource=month, match=\regexp{\A(j|J)un(e)?\Z}, replace=6]
      \step[fieldsource=month, match=\regexp{\A(j|J)ul(y)?\Z}, replace=7]
      \step[fieldsource=month, match=\regexp{\A(a|A)ug(ust)?\Z}, replace=8]
      \step[fieldsource=month, match=\regexp{\A(s|S)ep(tember)?\Z}, replace=9]
      \step[fieldsource=month, match=\regexp{\A(o|O)ct(ober)?\Z}, replace=10]
      \step[fieldsource=month, match=\regexp{\A(n|N)ov(ember)?\Z}, replace=11]
      \step[fieldsource=month, match=\regexp{\A(d|D)ec(ember)?\Z}, replace=12]
    }
  }
}

This replaces either three-letter name abbreviations or full month names with the correct month number. Since Biber's RegExp is case sensitive this matches the lower-case month abbreviation and the version where the first letter is capitalised (i.e. jan and Jan). (biber 2.12 and higher support matchi for case-insensitive matches; see this issue.)

This is only needed to suppress the warnings that Biber throws when it is presented with

month = {mar},

even without this code the month will be correctly recognised.


Note that for most BibTeX styles month = {mar}, is also not the best (correct) form of input. The brace-less month = mar, is usually better because it is then converted to the desired output via BibTeX strings. Q13 of BibTeX Tips and FAQ

Q13: Should I use words or numerals for the month, edition, etc., fields, and why?

You should always use the three letter month macros (undelimited) for the name of the month (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec) as this lets each .bst file format the month names as is required for that bibliography style.

BTXdoc states on p. 10

month The month in which the work was published or, for an unpublished work, in which it was written. You should use the standard three-letter abbreviation, as described in Appendix B.1.3 of the LaTeX book

See also https://www.ece.ucdavis.edu/~jowens/biberrors.html

  • This does not work. I get an error ! Undefined control sequence. \blx@tempf ...e="month" map\string _match="\regex {\Ajan \Z }" map_replace="1" – Krishna Jun 28 '18 at 15:17
  • @Krishna I forgot a p try again, please – moewe Jun 28 '18 at 15:18
  • Yay. Yes, got the regexp fixed. That did work! I wonder why mendeley folks haven't noticed that even their basic bibtex export is broken with the braces and stuff. But thank you for your help. Upvoted and marked as accepted. – Krishna Jun 28 '18 at 15:22
  • Related tex.stackexchange.com/q/286261/35864 – moewe Jun 29 '18 at 8:24
  • This stopped working with a recent update of biblatex/biber. Sigh! I don't know why. Is this still working for you. – Krishna Sep 9 '18 at 14:59

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.