[Tex/LaTex] Formatting only the DOI using biblatex

biblatexbibliographiesdoi

I'm using biblatex and would like my DOIs to be printed (achieved thanks to doi=true) in a different color and font in the bibliography section.

Basically, they are formatted the same way as URL, which are quite large in my style. I would like both to reduce the font of DOIs, and display them in a lighter color.

Here is a minimal working example:

\documentclass[a4paper]{article}

\usepackage{libertine}

\usepackage[T1]{fontenc}

\usepackage[utf8]{inputenc}

\usepackage[backend=biber,doi=true]{biblatex}
\addbibresource{mybib.bib}


\begin{document}

Hello \cite{Smith12}

\printbibliography[title={References}]

\end{document}

With the following mybib.bib:

@article{Smith12,
    author    = {John Smith},
    title     = {A great article},
    journal   = {Journal of something},
    volume    = {1},
    number    = {1-3},
    pages     = {42--50},
    year      = {2012},
    doi       = {12.345/123456789},
}

Note 1: package libertine is not necessary, just it makes the URL font even larger.

Note 2: I do not want to change the URL font (which is easy), but just the DOI color and font.

Best Answer

You could adjust the doi+eprint+url macro:

\documentclass[a4paper]{article}

\usepackage{libertine}

\usepackage[T1]{fontenc}

\usepackage[utf8]{inputenc}


\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@article{Smith12,
    author    = {John Smith},
    title     = {A great article},
    journal   = {Journal of something},
    volume    = {1},
    number    = {1-3},
    pages     = {42--50},
    year      = {2012},
    doi       = {12.345/123456789},
}

\end{filecontents*}

\usepackage{xcolor}
\usepackage[backend=biber,doi=true]{biblatex}
\addbibresource{\jobname.bib}

\renewbibmacro*{doi+eprint+url}{%
  \iftoggle{bbx:doi}
    {\color{red}\tiny\printfield{doi}}
    {}%
  \newunit\newblock
  \iftoggle{bbx:eprint}
    {\usebibmacro{eprint}}
    {}%
  \newunit\newblock
  \iftoggle{bbx:url}
    {\usebibmacro{url+urldate}}
    {}}


\begin{document}

Hello \cite{Smith12}

\printbibliography[title={References}]

\end{document}

enter image description here


EDIT

Thanks to the great comment by @moewe it is easier to just change the field format of the doi:

\documentclass[a4paper]{article}

\usepackage{libertine}

\usepackage[T1]{fontenc}

\usepackage[utf8]{inputenc}


\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@article{Smith12,
    author    = {John Smith},
    title     = {A great article},
    journal   = {Journal of something},
    volume    = {1},
    number    = {1-3},
    pages     = {42--50},
    year      = {2012},
    doi       = {12.345/123456789},
}

\end{filecontents*}

\usepackage{xcolor}
\usepackage[backend=biber,doi=true]{biblatex}
\addbibresource{\jobname.bib}

\DeclareFieldFormat{doi}{%
    \color{red}\tiny%
  \mkbibacro{DOI}\addcolon\space
  \ifhyperref
    {\href{https://doi.org/#1}{\nolinkurl{#1}}}
    {\nolinkurl{#1}}}

\begin{document}

Hello \cite{Smith12}

\printbibliography[title={References}]

\end{document}
Related Question