包装现有的 bibmacro

包装现有的 bibmacro

我正在寻找一种方法,biblatex如果没有指定 DOI,则只打印 URL(在我的例子中,DOI 无论如何都会导致参考文献中的链接,从而使 URL 有点多余)。

我想到了这个(但是由于编译没有完成,它似乎导致了无限循环......):

\renewbibmacro*{url+urldate}{%
  \iffieldundef{doi}
    {\usebibmacro{url+urldate}}
    {}%
}

\let似乎由于+......而无法保存宏。

知道如何正确地做到这一点吗?

M(N)网络:

\documentclass{article}
\usepackage[style=authoryear-comp]{biblatex}
\usepackage{hyperref}

%\renewbibmacro*{url+urldate}{%
%  \iffieldundef{doi}
%    {\usebibmacro{url+urldate}}
%    {}%
%

\addbibresource{\jobname.bib}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{key,
  author = {Author, A.},
  year = {2001},
  title = {Title},
  publisher = {Publisher},
  archivePrefix = {arXiv},
  arxivId = {1607.07249},
  doi = {10.1007/978-3-319-49004-5_22},
  eprint = {1607.07249},
  url = {http://link.springer.com/10.1007/978-3-319-49004-5_22},
}
\end{filecontents}

\begin{document}
\nocite{*}
\printbibliography
\end{document}

例子

答案1

没有\letbibmacro(您可能会提出功能请求),但创建一个并不难:

\documentclass{article}
\usepackage[style=authoryear-comp]{biblatex}
\usepackage{hyperref}

\newcommand{\letbibmacro}[2]{%
  \csletcs{abx@macro@#1}{abx@macro@#2}%
}

\letbibmacro{orig-url+urldate}{url+urldate}
\renewbibmacro*{url+urldate}{%
  \iffieldundef{doi}
    {\usebibmacro{orig-url+urldate}}
    {}%
}

\addbibresource{\jobname.bib}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{key,
  author = {Author, A.},
  year = {2001},
  title = {Title},
  publisher = {Publisher},
  archivePrefix = {arXiv},
  arxivId = {1607.07249},
  doi = {10.1007/978-3-319-49004-5_22},
  eprint = {1607.07249},
  url = {http://link.springer.com/10.1007/978-3-319-49004-5{\_}22},
}
@book{key2,
  author = {Author, A.},
  year = {2001},
  title = {Title},
  publisher = {Publisher},
  archivePrefix = {arXiv},
  arxivId = {1607.07249},
  eprint = {1607.07249},
  url = {http://link.springer.com/10.1007/978-3-319-49004-5{\_}22},
}
\end{filecontents}

\begin{document}
\nocite{*}
\printbibliography
\end{document}

在此处输入图片描述

答案2

您已经得到了针对所提问题的很好的答案,所以我想为您想要解决的问题添加两个解决方案。

您可以使用

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[fieldsource=doi, final]
      \step[fieldset=url, null]
      \step[fieldset=urldate, null]
    }
  }
}

或者

\AtEveryBibitem{%
  \iffieldundef{doi}
    {}
    {\clearfield{url}\clearfield{urlyear}}%
}

如果存在 DOI,则不打印 URL。

答案3

要有条件地打印 URL,你可以将其包装在\iffieldundef{doi}{}{}

\documentclass{article}
\usepackage[style=authoryear-comp]{biblatex}
\usepackage{hyperref}

% print url if no doi
\renewbibmacro*{doi+eprint+url}{%
  \printfield{doi}
  \newunit\newblock
  \iftoggle{bbx:eprint}{
      \usebibmacro{eprint}
  }{}%
  \newunit\newblock
  \iffieldundef{doi}{
      \usebibmacro{url+urldate}}
      {}
}
\addbibresource{\jobname.bib}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@book{key,
  author = {Author, A.},
  year = {2001},
  title = {Title},
  publisher = {Publisher},
  archivePrefix = {arXiv},
  arxivId = {1607.07249},
  doi = {10.1007/978-3-319-49004-5_22},
  eprint = {1607.07249},
  url = {http://link.springer.com/10.1007/978-3-319-49004-5{\_}22},
}

@book{keyx,
  author = {Author, A.},
  year = {2001},
  title = {Title},
  publisher = {Publisher},
  archivePrefix = {arXiv},
  arxivId = {1607.07249},
  url = {http://link.springer.com/10.1007/978-3-319-49004-5{\_}22},
}
\end{filecontents*}

\begin{document}
\nocite{*}
\printbibliography
\end{document}

相关内容