如何获取所有提供 URL 的条目中的可链接标题?

如何获取所有提供 URL 的条目中的可链接标题?

我希望我的参考书目中每个条目的标题都有指向其各自 URL 的链接,当且仅当提供了某个 URL 时,同时不让 URL 可见。我发现这个其他类似的问题,我根据他们的答案制定了我的方法。不幸的是,我无法得到想要的结果。请参阅以下 MWE:

\documentclass{article}

\usepackage[url=false]{biblatex}
\usepackage[colorlinks]{hyperref}

\newbibmacro{string+doi}[1]{%
  \iffieldundef{url}{#1}{\href{\thefield{url}}{#1}}}
\DeclareFieldFormat{title}{\usebibmacro{string+doi}{\mkbibemph{#1}}}
\DeclareFieldFormat[article]{title}{\usebibmacro{string+doi}{\mkbibquote{#1}}}

\begin{filecontents}{\jobname.bib}
@article{A01,
  author = {Author, A.},
  year = {2001},
  title = {Alpha},
  url = {http://tex.stackexchange.com/}
}
@book{B02,
  author = {Buthor, B.},
  year = {2002},
  title = {Bravo},
  url = {http://tex.stackexchange.com/},
}
@incollection{C03,
  author = {Cuthor, C.},
  year = {2003},
  title = {Charlie},
  url = {http://tex.stackexchange.com/},
}
@phdthesis{D04,
  author = {Duthor, D.},
  year = {2004},
  title = {Delta},
  url = {http://tex.stackexchange.com/},
}
\end{filecontents}

\addbibresource{\jobname.bib}

\nocite{*}

\begin{document}

\printbibliography

\end{document}

正如您在下面的屏幕截图中看到的,问题在于,@book @article尽管提供了 URL,但除 之外的条目类型都没有获得可链接的标题。我如何才能获得所需的效果,不仅对@book@article类有效,而且对和@incollection类也有效?@phdthesis@inbook

在此处输入图片描述

谢谢你们!

答案1

默认title格式为(见biblatex.def,第 3.18 版第 579-585 行

\DeclareFieldFormat{title}{\mkbibemph{#1}}
\DeclareFieldFormat
  [article,inbook,incollection,inproceedings,patent,thesis,unpublished]
  {title}{\mkbibquote{#1\isdot}}
\DeclareFieldFormat
  [suppbook,suppcollection,suppperiodical]
  {title}{#1}

为了复制它们,你需要

\documentclass{article}
\usepackage[url=false]{biblatex}
\usepackage[colorlinks]{hyperref}

\newbibmacro{string+doi}[1]{%
  \iffieldundef{url}{#1}{\href{\thefield{url}}{#1}}}

\DeclareFieldFormat{title}{\usebibmacro{string+doi}{\mkbibemph{#1}}}
\DeclareFieldFormat
  [article,inbook,incollection,inproceedings,patent,thesis,unpublished]
  {title}{\usebibmacro{string+doi}{\mkbibquote{#1\isdot}}}
\DeclareFieldFormat
  [suppbook,suppcollection,suppperiodical]
  {title}{\usebibmacro{string+doi}{#1}}

\begin{filecontents}{\jobname.bib}
@article{A01,
  author = {Author, A.},
  year = {2001},
  title = {Alpha},
  url = {http://tex.stackexchange.com/}
}
@book{B02,
  author = {Buthor, B.},
  year = {2002},
  title = {Bravo},
  url = {http://tex.stackexchange.com/},
}
@incollection{C03,
  author = {Cuthor, C.},
  year = {2003},
  title = {Charlie},
  url = {http://tex.stackexchange.com/},
}
@phdthesis{D04,
  author = {Duthor, D.},
  year = {2004},
  title = {Delta},
  url = {http://tex.stackexchange.com/},
}
\end{filecontents}
\addbibresource{\jobname.bib}

\nocite{*}

\begin{document}
\printbibliography
\end{document}

所有标题都以粉红色显示链接。

答案2

添加

\DeclareFieldFormat[incollection]{title}{\usebibmacro{string+doi}{\mkbibquote{#1}}} 
\DeclareFieldFormat[thesis]{title}{\usebibmacro{string+doi}{\mkbibquote{#1}}}%
\DeclareFieldFormat[inbook]{title}{\usebibmacro{string+doi}{\mkbibquote{#1}}}

(或者 \DeclareFieldFormat[incollection,thesis, inbook]{title}{\usebibmacro{string+doi}{\mkbibquote{#1}}}

并将最后一次引用之前的

@thesis{D04,
        author = {Duthor, D.},
        type   = {phdthesis},
        year = {2004},
        title = {Delta},
        url = {http://tex.stackexchange.com/},          
    }

C

\documentclass{article}

\usepackage[url=false]{biblatex}    
\usepackage[colorlinks]{hyperref}

\newbibmacro{string+doi}[1]{%
    \iffieldundef{url}{#1}{\href{\thefield{url}}{#1}}}
\DeclareFieldFormat{title}{\usebibmacro{string+doi}{\mkbibemph{#1}}}
\DeclareFieldFormat[article]{title}{\usebibmacro{string+doi}{\mkbibquote{#1}}}

\DeclareFieldFormat[incollection]{title}{\usebibmacro{string+doi}{\mkbibquote{#1}}} % added
\DeclareFieldFormat[thesis]{title}{\usebibmacro{string+doi}{\mkbibquote{#1}}}% added
\DeclareFieldFormat[inbook]{title}{\usebibmacro{string+doi}{\mkbibquote{#1}}}% added
    
\begin{filecontents}[overwrite]{\jobname.bib}
    @article{A01,
        author = {Author, A.},
        year = {2001},
        title = {Alpha},
        url = {http://tex.stackexchange.com/}
    }
    @book{B02,
        author = {Buthor, B.},
        year = {2002},
        title = {Bravo},
        url = {http://tex.stackexchange.com/},
    }
    @incollection{C03,
        author = {Cuthor, C.},
        year = {2003},
        title = {Charlie},
        booktitle     = "Fiber Optic Test and Measurement",
        url = {http://tex.stackexchange.com/},
    }
    @thesis{D04,
        author = {Duthor, D.},
        type   = {phdthesis},
        year = {2004},
        title = {Delta},
        url = {http://tex.stackexchange.com/},  
    }

    @inbook{E05,
        author        = "H. E. Rose",
        title         = "A Course in Number Theory",
        publisher     = "Oxford Univ. Press",
        address       = "New York, NY",
        year          = "1988",
        url = {http://tex.stackexchange.com/},
        chapter       = "3"
    }
    
\end{filecontents}

\addbibresource{\jobname.bib}

\nocite{*}

\begin{document}
    
    \printbibliography
    
\end{document}

最后引用自biblatex-examples.bib

相关内容