biblatex:使标题超链接到 DOI、URL 或 ISBN

biblatex:使标题超链接到 DOI、URL 或 ISBN

这是一个后续问题biblatex:将标题超链接到 doi url(如果可用)。我的参考数据库包含一些 DOI 代码,但主要是普通 URL 和一些 ISBN/ISSN 代码。我想要实现的是扩展 Herbert 的代码,并按以下优先级超链接条目标题:1. DOI,2. URL,3. ISBN/ISSN。我的意思是,如果 DOI 和 URL 都可用,则使用 DOI。如果 URL 和 ISBN 都可用,则使用 URL。如果只有 ISBN 可用,则使用它。

还有两个问题:

  • URL 字段可能包含多个 URL,每个 URL 之间仅用空格分隔。在这种情况下,只应使用第一个 URL。
  • Herbert 的代码仅扩展到文章。我想将其扩展到所有条目类型。

Herbert 用于将标题与 DOI 超链接的代码是:

\newbibmacro{string+doi}[1]{%
  \iffieldundef{doi}{#1}{\href{http://dx.doi.org/\thefield{doi}}{#1}}}
\DeclareFieldFormat{title}{\usebibmacro{string+doi}{\mkbibemph{#1}}}
\DeclareFieldFormat[article]{title}{\usebibmacro{string+doi}{\mkbibquote{#1}}}

这远远超出了我的技能,所以如果有人能指导我如何实现这一点就太好了。

答案1

一种可能性是将 Herbert 的 bibmacro 扩展为多个嵌套条件。(以下示例仅包含 ISBN/ISSN 字段的占位符链接,因为我不知道这些链接应如何格式化。)

关于您的进一步问题:

  • 我不确定该url字段是否允许指定多个以空格分隔的 URL;如果允许,我不知道如何仅保留第一个 URL。

  • 赫伯特代码实际上首先涵盖了所有的条目类型,然后为文章指定了一种特殊的标题格式(引号而不是强调)。


\documentclass{article}

\usepackage[doi=false,url=false,isbn=false]{biblatex}

\usepackage[colorlinks]{hyperref}

\newbibmacro{string+doiurlisbn}[1]{%
  \iffieldundef{doi}{%
    \iffieldundef{url}{%
      \iffieldundef{isbn}{%
        \iffieldundef{issn}{%
          #1%
        }{%
          \href{http://books.google.com/books?vid=ISSN\thefield{issn}}{#1}%
        }%
      }{%
        \href{http://books.google.com/books?vid=ISBN\thefield{isbn}}{#1}%
      }%
    }{%
      \href{\thefield{url}}{#1}%
    }%
  }{%
    \href{http://dx.doi.org/\thefield{doi}}{#1}%
  }%
}

\DeclareFieldFormat{title}{\usebibmacro{string+doiurlisbn}{\mkbibemph{#1}}}
\DeclareFieldFormat[article,incollection]{title}%
    {\usebibmacro{string+doiurlisbn}{\mkbibquote{#1}}}

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

\addbibresource{\jobname.bib}

\nocite{*}

\begin{document}

\printbibliography

\end{document}

在此处输入图片描述

答案2

另一种方法是定义一种添加链接的新格式。这可以在任何应用该title格式的参考书目宏中使用。在标准样式中,只有两个这样的宏:titleperiodical。您可以使用\DeclareFieldAlias{<new format name>}{default}

url字段仅用于保存一个 URL,因此我们需要设计一种访问第一个 URL 的方法。一种选择是让 biber 通过调整来删除多余的 URL先前的答案。下面的代码演示了一种类似内部 biblatex 格式化命令访问书目数据的替代方法。

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{csquotes}
\usepackage[american]{babel}
\usepackage[style=verbose,url=false,doi=false,isbn=false]{biblatex}
\usepackage[colorlinks]{hyperref}

% Define new format that applies a hypertext reference
\DeclareFieldFormat{linked}{%
  \ifboolexpr{ test {\ifhyperref} and not test {\ifentrytype{online}} }
    {\iffieldundef{doi}
       {\iffieldundef{url}
          {\iffieldundef{isbn}
             {\iffieldundef{issn}
                {#1}
                {\href{\worldcatsearch\thefield{issn}}{#1}}}
             {\href{\worldcatsearch\thefield{isbn}}{#1}}}
          {\href{\thefieldfirstword{url}}{#1}}}
       {\href{http://dx.doi.org/\thefield{doi}}{#1}}}
    {#1}}

% URL prefix for WorldCat query
\def\worldcatsearch{http://www.worldcat.org/search?qt=worldcat_org_all&q=}

% Define new command that returns the first word of a given field
\makeatletter
\def\thefieldfirstword#1{%
  \expandafter\expandafter
  \expandafter\firstword
  \expandafter\expandafter
  \expandafter{\csname abx@field@#1\endcsname}}
\def\firstword#1{\firstword@i#1 \@nil}
\def\firstword@i#1 #2\@nil{#1}
\makeatother

% Redefine url format to print only first URL, omit URL prefix
\DeclareFieldFormat{url}{\url{\firstword{#1}}}

\renewbibmacro*{title}{% Based on generic definition from biblatex.def
  \ifboolexpr{ test {\iffieldundef{title}} and test {\iffieldundef{subtitle}} }
    {}
    {\printtext[title]{\printtext[linked]{%
       \printfield[titlecase]{title}%
       \setunit{\subtitlepunct}%
       \printfield[titlecase]{subtitle}}}%
     \newunit}%
  \printfield{titleaddon}}

\renewbibmacro*{periodical}{% Based on generic definition from biblatex.def
  \iffieldundef{title}
    {}
    {\printtext[title]{\printtext[linked]{%
       \printfield[titlecase]{title}%
       \setunit{\subtitlepunct}%
       \printfield[titlecase]{subtitle}}}}}

\begin{filecontents}{\jobname.bib}
@Online{ctanmirror,
  label = {CTAN},
  title = {CTAN Mirror},
  date = {2006},
  url = {http://mirror.ctan.org http://www.ctan.org},
  urldate = {2006-10-01}}
@Periodical{jcg,
  title = {Computers and Graphics},
  issuetitle = {Semantic {3D} Media and Content},
  volume = {35},
  number = {4},
  year = {2011},
  issn = {0097-8493}}
@Manual{cmso,
  label = {CMS Online},
  title = {The Chicago Manual of Style Online},
  edition = {16},
  publisher = {University of Chicago},
  date = {2010},
  url = {http://www.chicagomanualofstyle.org http://www.chicagomanualofstyle.org/16/contents.html},
  isbn = {0-226-10403-6}}
@Article{sarfraz,
  author = {M. Sarfraz and M. F. A. Razzak},
  title = {An algorithm for automatic capturing of the font outlines},
  journal = {Computers and Graphics},
  volume = {26},
  number = {5},
  pages = {795--804},
  year = {2002},
  issn = {0097-8493},
  doi = {10.1016/S0097-8493(02)00134-6}}
\end{filecontents}

% Don't link titles in citations
\AtEveryCite{\DeclareFieldAlias{linked}{default}}

\addbibresource{\jobname.bib}
\addbibresource{biblatex-examples.bib}
\begin{document}
\null\vfill
Filler text.\footcite{gonzalez,companion,cmso}
Filler text.\footcite{sarfraz,ctan,ctanmirror,jcg}
\printbibliography
\end{document}

在此处输入图片描述

无需重新定义参考书目宏,只需在格式定义中应用新格式即可title

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

答案3

这是对前面答案的补充。使用 LuaLaTeX 时,通过调用 lua 函数可以轻松提取第一个 URL 或 ISBN 号。我在the_first_word.lua文件中放置了一个 lua 脚本,如果字符串是用逗号空格分隔的 ISBN 列表,则返回字符串的第一个单词

-- find the first comma or space
-- return the string before first comma or space if found,
-- otherwise return entire string
function the_first_word( s )
   i = string.find( s, ",? " )
   if i == nil then return s end
   return string.sub( s, 0, i - 1 )
end

然后,调用脚本并将标题字段包装在 href 宏中的 LaTeX 代码如下

\directlua{dofile("the_first_word.lua")}
\newcommand*{\thefirstword}[1]{%
  \directlua{tex.sprint(the_first_word("\luatexluaescapestring{#1}"))}}

\newbibmacro{linktitle}[1]{%
  \iffieldundef{doi}{%
    \iffieldundef{url}{%
      \iffieldundef{isbn}{%
        \iffieldundef{issn}{%
          #1%
        }{\href{http://books.google.com/books?vid=ISSN\thefield{issn}}{#1}}%
      }{\href{http://books.google.com/books?vid=ISBN\thefirstword{\thefield{isbn}}}{#1}}%
    }{\href{\thefirstword{\thefield{url}}}{#1}}%
  }{\href{http://dx.doi.org/\thefield{doi}}{#1}}%
}

相关内容