7

Using biblatex, how can I get a URL from the bib file so that I can use it in the \href command?

I am guessing that the \usefield command is what I am looking for and that in order to use that I would need the \entrydata command. But both are undefined.

\documentclass{article}
\usepackage[backend=biber]{biblatex}
\usepackage{hyperref}

\usepackage{filecontents}
\begin{filecontents*}{sources.bib}
@online{biblatex,
    url = {http://mirrors.ctan.org/macros/latex/contrib/biblatex/doc/biblatex.pdf},
    urldate = {2018-02-02},
}
\end{filecontents*}

\addbibresource{sources.bib}

\begin{document}

    % This would work, but I don't want to duplicate the URL from the bib file:
    \href{http://mirrors.ctan.org/macros/latex/contrib/biblatex/doc/biblatex.pdf}{biblatex documentation}

    % Does not work because \citeurl{biblatex} is already a link, not the URL:
    %The \href{\citeurl{biblatex}}{biblatex documentation}

    % Does not work because \usefield and \entrydata are undefined:
    %The \entrydata{biblatex}{\usefield\href{url}{biblatex documentation}}

    \printbibliography
\end{document}
jakun
  • 5,981

2 Answers2

6

Thanks to moewe's helpful link I have come up with the following solution:

\documentclass{article}
\usepackage[backend=biber]{biblatex}
\usepackage{hyperref}

\usepackage{filecontents}
\begin{filecontents*}{sources.bib}
@online{biblatex,
    url = {http://mirrors.ctan.org/macros/latex/contrib/biblatex/doc/biblatex.pdf},
    urldate = {2018-02-02},
    title = {The biblatex Package},
}
\end{filecontents*}

\addbibresource{sources.bib}


\DeclareCiteCommand{\citelink}
    {\boolfalse{citetracker}%
        \boolfalse{pagetracker}%
        \usebibmacro{prenote}}
    {\iffieldundef{postnote}
        {\href{\thefield{url}}{\printfield{title}}}
        {\href{\thefield{url}}{\thefield{postnote}}}}
    {\multicitedelim}
    {}

\begin{document}

    The \citelink[biblatex documentation]{biblatex}.

    \citelink{biblatex}

    \printbibliography
\end{document}
jakun
  • 5,981
3

An alternative solution which is really retrieving the URL instead of directly creating the link, also based on moewe's helpful link:

\documentclass{article}
\usepackage[backend=biber]{biblatex}
\usepackage{hyperref}

\usepackage{filecontents}
\begin{filecontents*}{sources.bib}
@online{biblatex,
    url = {http://mirrors.ctan.org/macros/latex/contrib/biblatex/doc/biblatex.pdf},
    urldate = {2018-02-02},
    title = {The biblatex Package},
}
\end{filecontents*}

\addbibresource{sources.bib}


%WARNING: the command will *not* be defined if bibid is unknown
% for example because biber has not yet run
% therefore getting an "Undefined control sequence" in the first run is to be expected
\DeclareCiteCommand{\geturl}
    {\boolfalse{citetracker}\boolfalse{pagetracker}}
    {\iffieldundef{postnote}
        {\xdef\biburl{\thefield{url}}}
        {%
            \edef\geturlTmpCmd{\csfield{postnote}}%
            \expandafter\xdef\geturlTmpCmd{\thefield{url}}%
        }%
    }
    {}
    {}

\begin{document}
    \geturl{biblatex}
    The \href{\biburl}{biblatex documentation}.

    \geturl[\myurl]{biblatex}
    The \href{\myurl}{biblatex documentation}.

    \printbibliography
\end{document}
jakun
  • 5,981