BibLaTex:在每个参考书目条目前添加一个非超链接的作者年份块

BibLaTex:在每个参考书目条目前添加一个非超链接的作者年份块

我想[Author(s) Year]在每个引用的开头添加一个块,正如这个问题所描述的那样:在参考书目条目开头添加 [AuthorYear] 块

我使用了该问题接受的答案中给出的代码。但是,此外,我希望我的引文具有超链接,因此使用包hyperref。以下是一个最小的工作示例,它演示了我的问题:

\documentclass{article}

% Adding a yearauthor block in bibliohraphy: https://tex.stackexchange.com/a/11856/42810
\usepackage[backend=bibtex,citestyle=authoryear,bibstyle=authortitle,sorting=nyt,dashed=false,maxcitenames=2]{biblatex}

\newcounter{mymaxcitenames}
\AtBeginDocument{%
  \setcounter{mymaxcitenames}{\value{maxnames}}%
}

\renewbibmacro*{begentry}{%
  \printtext[brackets]{%
    \begingroup
    \defcounter{maxnames}{\value{mymaxcitenames}}%
    \printnames{labelname}%
    \setunit{\nameyeardelim}%
    \usebibmacro{cite:labelyear+extrayear}%
    \endgroup
    }%
  \addspace%
}

\DeclareNameAlias{sortname}{first-last}

% Use hyperref to hyperlink citations
\usepackage[dvipsnames]{xcolor}
\usepackage[hyperindex=true]{hyperref}
\hypersetup{colorlinks=true,linkcolor=Blue,citecolor=BrickRed}


\begin{filecontents}{\jobname.bib}
@article{foo,
  author    = {Grace Murray Hopper},
  title     = {The Education of a Computer},
  journal   = {{IEEE} Annals of the History of Computing},
  volume    = {9},
  number    = {3/4},
  pages     = {271--281},
  year      = {1987}
}
\end{filecontents}

\bibliography{\jobname}

\begin{document}

Here is a citation: \cite{foo}.

\printbibliography

\end{document}

这将产生以下内容: 在此处输入图片描述

如您所见,参考书目中的作者年份块包含指向其自身的超链接。我不想要这个超链接,尽管我确实希望在运行文本中(即参考书目之前)有超链接。我如何才能从参考书目中的作者年份块中删除超链接?

答案1

我们只需要让bibhyperref格式在“介绍”块中不执行任何操作。为此,我们将\DeclareFieldFormat{bibhyperref}{##1}%(双重#是必要的,因为我们已经在宏定义中)插入到重新定义中begentry

\renewbibmacro*{begentry}{%
  \printtext[brackets]{%
    \begingroup
    \DeclareFieldFormat{bibhyperref}{##1}%
    \defcounter{maxnames}{\value{mymaxcitenames}}%
    \printnames{labelname}%
    \setunit{\nameyeardelim}%
    \usebibmacro{cite:labelyear+extrayear}%
    \endgroup
    }%
  \addspace%
}

这给出了所需的结果

\documentclass{article}

% Adding a yearauthor block in bibliohraphy: http://tex.stackexchange.com/a/11856/42810
\usepackage[backend=bibtex,citestyle=authoryear,bibstyle=authortitle,sorting=nyt,dashed=false,maxcitenames=2]{biblatex}

\newcounter{mymaxcitenames}
\AtBeginDocument{%
  \setcounter{mymaxcitenames}{\value{maxnames}}%
}

\renewbibmacro*{begentry}{%
  \printtext[brackets]{%
    \begingroup
    \DeclareFieldFormat{bibhyperref}{##1}%
    \defcounter{maxnames}{\value{mymaxcitenames}}%
    \printnames{labelname}%
    \setunit{\nameyeardelim}%
    \usebibmacro{cite:labelyear+extrayear}%
    \endgroup
    }%
  \addspace%
}

\DeclareNameAlias{sortname}{first-last}

% Use hyperref to hyperlink citations
\usepackage[dvipsnames]{xcolor}
\usepackage[hyperindex=true]{hyperref}
\hypersetup{colorlinks=true,linkcolor=Blue,citecolor=BrickRed}


\begin{filecontents}{\jobname.bib}
@article{foo,
  author    = {Grace Murray Hopper},
  title     = {The Education of a Computer},
  journal   = {{IEEE} Annals of the History of Computing},
  volume    = {9},
  number    = {3/4},
  pages     = {271--281},
  year      = {1987}
}
\end{filecontents}

\bibliography{\jobname}

\begin{document}
Here is a citation: \cite{foo}.

\printbibliography
\end{document}

在此处输入图片描述

相关内容