[Tex/LaTex] hyperlinks in biblatex

biblatexhyperrefspacing

I'm making a biblatex style to solve an issue with the standard styles: if you have a reference with a doi field, it just prints the DOI number, with a clickable link if hyperref is enabled.

But a DOI number is big and ugly; no one wants to see it. I want to make the link anchored behind the journal reference: beginning with the journal name and ending with the pages. So far I managed to make the following code:

\DeclareBibliographyDriver{article}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/translator+others}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{title}%
  \newunit
  \printlist{language}%
  \newunit\newblock
  \usebibmacro{byauthor}%
  \newunit\newblock
  \usebibmacro{bytranslator+others}%
  \newunit\newblock
  \printfield{version}%
  \newunit\newblock
  \href{http://dx.doi.org/\thefield{doi}}{\usebibmacro{journal+issuetitle}%
  \newunit\newblock
  \usebibmacro{byeditor+others}%
  \newunit\newblock
  \usebibmacro{note+pages}}%
  \newunit\newblock
  \iftoggle{bbx:isbn}
    {\printfield{issn}}
    {}%
  \newunit\newblock
  \usebibmacro{eprint+url}%
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \newunit\newblock
  \usebibmacro{pageref}%
  \usebibmacro{finentry}}

But there's a problem: as the punctuation and spacing is actually produced by the first \printfield after \newunit\newblock, they are captured by the \href, and the result is very ugly. Is there a way to force them to be produced before the journal title, and the next one exactly after \usebibmacro{note+pages}? In other words, can I make the link environment to begin exactly at the journal name, and include the punctuation after note+pages?

Best Answer

I've found a way (thanks to Mr. Lehman!)

As \href changes the formatting, it's best to use it inside a biblatex formatting primitive, so it will get handled consistently. The code is

\DeclareFieldFormat{doilink}{\href{http://dx.doi.org/\thefield{doi}}{#1}}

\DeclareBibliographyDriver{article}{% \usebibmacro{bibindex}% \usebibmacro{begentry}% \usebibmacro{author/translator+others}% \setunit{\labelnamepunct}\newblock \usebibmacro{title}% \newunit \printlist{language}% \newunit\newblock \usebibmacro{byauthor}% \newunit\newblock \usebibmacro{bytranslator+others}% \newunit\newblock \printfield{version}% \newunit\newblock \printtext[doilink]{\usebibmacro{journal+issuetitle}% \newunit\newblock \usebibmacro{byeditor+others}% \newunit\newblock \usebibmacro{note+pages}}% \newunit\newblock \iftoggle{bbx:isbn} {\printfield{issn}} {}% \newunit\newblock \usebibmacro{eprint+url}% \newunit\newblock \usebibmacro{addendum+pubstate}% \newunit\newblock \usebibmacro{pageref}% \usebibmacro{finentry}}

Of course, one should add some logic (\iffieldundef{doi}, \ifhyperref); I only decided to keep the example simple.