如何使用自定义文本创建 bib 文件中提供的 URL 链接

如何使用自定义文本创建 bib 文件中提供的 URL 链接

使用 biblatex,如何从 bib 文件中获取 URL 以便在命令中使用它\href

我猜想这个\usefield命令就是我要找的,并且为了使用它,我需要这个\entrydata命令。但两者都未定义。

\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}

答案1

感谢 moewe 的帮助关联我想出了以下解决方案:

\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}

答案2

另一种解决方案是检索 URL,而不是直接创建链接,这也是基于 moewe 的帮助关联

\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}

相关内容