1

When I download citations they sometimes contain the entry http://dx.doi.org/ in the doi field in front of the actual doi - for instance from ScienceDirect (elsevier). I do not want to check and eventually change this every time I download a citation. I actually found a solution that works (even though it actually covers a different question), which uses \DeclareSourcemap (Biblatex: Convert doi-url into doi field). Adding the following code below from the other question into my MWE does the job.

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[ % copies url to doi field if it starts with http://dx.doi.org/
        fieldsource=url,
        match=\regexp{http://dx.doi.org/(.+)},
        fieldtarget=doi,
      ]
    \step[ % removes http://dx.doi.org/ string from doi field
      fieldsource=doi,
      match=\regexp{http://dx.doi.org/(.+)},
      replace=\regexp{$1}
    ]
    }
  \map{ % removes url + urldate field from all entries that have a doi field
   \step[fieldsource=doi, final]
   \step[fieldset=url, null]
   \step[fieldset=urldate, null]
   }
 }
}

The complete MWE is as follows:

\documentclass{scrreprt}
\usepackage[ngerman]{babel}
\usepackage{filecontents}
\usepackage{xcolor}                 
\usepackage[babel]{csquotes}
\usepackage[backend=biber,style=chem-angew,mcite,subentry]{biblatex}
\usepackage{hyperref}               
\hypersetup{%
    colorlinks,
    linkcolor={red!0!black},
    citecolor={blue!0!black},
    urlcolor={blue!80!black}
}

\ExecuteBibliographyOptions{citetracker=true,sorting=none,maxcitenames=10,doi=false,url=false,isbn=false,hyperref=true,backref=false}

\DefineBibliographyStrings{ngerman}{
   andothers = {\textit{et~al\adddot}}            
}

% Number of each bibliography entry in brackets
\DeclareFieldFormat{labelnumberwidth}{\mkbibbrackets{#1}}

\makeatletter

\newtoggle{cbx@togcite}

% Citation number superscript in brackets
\renewcommand\@makefntext[1]{%
  \iftoggle{cbx@togcite}
    {\@textsuperscript{\normalfont[\@thefnmark]}\enspace #1}
    {\@textsuperscript{\normalfont\@thefnmark}\enspace #1}%
  \global\togglefalse{cbx@togcite}}

% Citation number superscript in brackets (for babel french)
\ifdef{\@makefntextFB}{%
\renewcommand\@makefntextFB[1]{%
  \iftoggle{cbx@togcite}
    {\@textsuperscript{\normalfont[\@thefnmark]}\enspace #1}
    {\@textsuperscript{\normalfont\@thefnmark}\enspace #1}%
  \global\togglefalse{cbx@togcite}}}{}

%---------------------------------------------------------------
% Mostly verbatim from Joseph Wright
% http://www.texdev.net/2010/03/08/biblatex-numbered-citations-as-footnotes/

\DeclareCiteCommand{\sfcite}[\cbx@superscript]%
  {\usebibmacro{cite:init}%
   \let\multicitedelim=\supercitedelim
   \iffieldundef{prenote}
     {}
     {\BibliographyWarning{Ignoring prenote argument}}%
   \iffieldundef{postnote}
     {}
     {\BibliographyWarning{Ignoring postnote argument}}}
  {\usebibmacro{citeindex}%
   \usebibmacro{sfcite}%
   \usebibmacro{cite:comp}}
  {}
  {\usebibmacro{cite:dump}}

\newbibmacro*{sfcite}{%
  \ifciteseen
  {}
  {\xappto\cbx@citehook{%
   \global\toggletrue{cbx@togcite}%
   \noexpand\footnotetext[\thefield{labelnumber}]{%
     \fullcite{\thefield{entrykey}}\addperiod}}}}

\newrobustcmd{\cbx@superscript}[1]{%
  \mkbibsuperscript{\mkbibbrackets{#1}}%
  \cbx@citehook%
  \global\let\cbx@citehook=\empty}

\let\cbx@citehook=\empty
%---------------------------------------------------------------

\makeatother


\begin{filecontents}{\jobname.bib}
@Article{Frank1953,
  author  = {Frank, F. C.},
  title   = {On spontaneous asymmetric synthesis},
  journal = {Biochim. Biophys. Acta},
  year    = {1953},
  volume  = {11},
  pages   = {459-463},
  doi     = {http://dx.doi.org/10.1016/0006-3002(53)90082-1},
  url     = {http://www.sciencedirect.com/science/article/pii/0006300253900821},
}
\end{filecontents}

\addbibresource{\jobname.bib}

\newbibmacro{string+doiurlisbn}[1]{%
  \iffieldundef{doi}{%
    \iffieldundef{url}{%
      \iffieldundef{isbn}{%
        \iffieldundef{issn}{%
          #1%
        }{%
          \href{http://books.google.com/books?vid=ISSN\thefield{issn}}{#1}%
        }%
      }{%
        \href{http://books.google.com/books?vid=ISBN\thefield{isbn}}{#1}%
      }%
    }{%
      \href{\thefield{url}}{#1}%
    }%
  }{%
    \href{https://doi.org/\thefield{doi}}{#1}%
  }%
}

\DeclareFieldFormat{journaltitle}{\usebibmacro{string+doiurlisbn}{\mkbibemph{#1}}}

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[ % copies url to doi field if it starts with http://dx.doi.org/
        fieldsource=url,
        match=\regexp{http://dx.doi.org/(.+)},
        fieldtarget=doi,
      ]
    \step[ % removes http://dx.doi.org/ string from doi field
      fieldsource=doi,
      match=\regexp{http://dx.doi.org/(.+)},
      replace=\regexp{$1}
    ]
    }
  \map{ % removes url + urldate field from all entries that have a doi field
   \step[fieldsource=doi, final]
   \step[fieldset=url, null]
   \step[fieldset=urldate, null]
   }
 }
}


\begin{document}
\chapter{Title}
\null\vfill\noindent
Citation.\sfcite{Frank1953}
\printbibliography
\end{document}

My question: Is there another solution that just cuts the http://dx.doi.org/ if present in the doi field? And (since I want to learn more and could not find a precise description) what does the $1 do? By trial and error I understood that it refers to the actual doi. But I thought a $ would always require another sign (math mode). Texmaker colors all my code in green after this and it is annoying me. Is there a way to change this?

  • 1
    Most editors can do search-and-replace by field (JabRef certainly can): is that acceptable? (Really the prefix is not part of the DOI so conceptually should go.) – Joseph Wright Dec 07 '16 at 16:32
  • @Joseph Wright I prefer (and was hoping for) the solution of moewe. I do not want to manually edit references. I agree with both of you that the doi field should not contain any prefix or information beside the doi, but this is the result I get from the export (so it is a mistake of the publisher in my opinion). You can try it using my mwe and export the citation for the given article. – Sacharja Dec 08 '16 at 09:28

1 Answers1

3
\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[ % removes http://dx.doi.org/ string from doi field
        fieldsource=doi,
        match=\regexp{http://dx.doi.org/(.+)},
        replace=\regexp{$1}
      ]
    }
  }
}

would be enough.

This works by regex matching. If the doi field contains the string http://dx.doi.org/ everything after that is collected by the (.+), the collected string is 'saved' in the variable $1. This has nothing to do with TeX and its math mode, this is Perl regex (in fact this code is executed by Biber who runs on Perl, what you have in your document is just the instruction for Biber to run its regex). That is why we need to 'protect' the special characters in the \regex macro.

If TeXmaker thinks this starts math mode, that is unfortunate. Maybe you can trick it by adding a %$ after the \regexp{$1}. But really this is just your editor's syntax highlighting going slightly wrong, not TeX.


But of course Joseph is right with his comment that your .bib file should not contain the http://dx.doi.org/ in the doi field in the first place - it should hold only the bare DOI.


With the following you can also filter https://doi.org/, https://dx.doi.org/.

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[
        fieldsource=doi,
        match=\regexp{https?://(dx.)?doi.org/(.+)},
        replace=\regexp{$2}
      ]
    }
  }
}

Here $1 is either dx. or empty and the real DOI is saved in $2.

moewe
  • 175,683