如何根据作者注释为参考书目中的特定条目着色?

如何根据作者注释为参考书目中的特定条目着色?

我想将参考书目中的一些参考文献着色为类似这个问题这个, 和这个

然而,这些技术使用了额外的类别。另一个使用关键字。相反,我想使用作者注释,我已经使用它突出显示作者姓名。

以下是作者突出显示作品的示例,但不突出显示参考文献和引文。目标是将注释作者的姓名用粗体显示,并将至少一位作者注释的所有参考文献用红色显示(参考文献和引文均用红色显示)。

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage[usenames,dvipsnames]{xcolor}

\begin{filecontents}{test-example.bib}
@article{a,
  author = {Name, A and Name, B},
  author+an = {1=highlight},
  title = {Title A},
  journal = {Journal A}
}
@inproceedings{b,
  author = {Name, E and Name, F},
  title = {Title B},
  booktitle = {Conference B}
}
@article{c,
  author = {Name, C and Name, D},
  title = {Title C},
  journal = {Journal C}
}
@inproceedings{d,
  author = {Name, E and Name, F},
  author+an = {2=highlight},
  title = {Title D},
  booktitle = {Conference D}
}
\end{filecontents}

\usepackage[style=numeric]{biblatex}
\addbibresource{test-example.bib}

% Highlighting annotated authors
\renewcommand*{\mkbibnamegiven}[1]{%
  \ifitemannotation[author]{highlight}
    {\textbf{#1}}
    {#1}}
\renewcommand*{\mkbibnamefamily}[1]{%
  \ifitemannotation[author]{highlight}
    {\textbf{#1}}
    {#1}}

% Expected result: citations and references with at least 
% one annotated author should be red
\DeclareFieldFormat{labelprefix}{%
  \iffieldannotation[author]{highlight}
    {\textcolor{red}{#1}}
    {#1}}

\DeclareFieldFormat{labelnumber}{%
  \iffieldannotation[author]{highlight}
    {\textcolor{red}{#1}}
    {#1}}

\DeclareFieldFormat{labelnumberwidth}{%
  \iffieldannotation[author]{highlight}
    {\textcolor{red}{\mkbibbrackets{#1}}}
    {\mkbibbrackets{#1}}}

\AtEveryBibitem{%
  \iffieldannotation[author]{highlight}
    {\color{red}}
    {}}

\begin{document}

  \cite{a,b,c,d}
  
  \cite{a}\cite{b}\cite{c}\cite{d}

  \printbibliography
\end{document}

答案1

为了确定是否应该突出显示某个条目,我们需要循环遍历其所有作者,以查看其中是否有任何一位具有所需的注释。由于每次需要决定是否应该突出显示某个条目时都重新运行循环会非常浪费,因此我们使用了biblatex:动态过滤参考文献中特定作者的出版物循环遍历名称一次\AtDataInput,并将条目分配给参考书目类别highlight(如果应突出显示)。然后我们可以检查条目是否应使用红色\ifcategory{highlight}{<true>}{<false>}

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[style=numeric]{biblatex}


\DeclareBibliographyCategory{highlight}

\DeclareIndexNameFormat{IsHighlighted}{%
  \ifitemannotation[author]{highlight}
    {\addtocategory{highlight}{\thefield{entrykey}}}
    {}}

\AtDataInput{%
  \indexnames[IsHighlighted][1-999]{author}}

% Highlighting annotated authors
\renewcommand*{\mkbibnamegiven}[1]{%
  \ifitemannotation[author]{highlight}
    {\textbf{#1}}
    {#1}}
\renewcommand*{\mkbibnamefamily}[1]{%
  \ifitemannotation[author]{highlight}
    {\textbf{#1}}
    {#1}}

% Expected result: citations and references with at least 
% one annotated author should be red
\DeclareFieldFormat{labelprefix}{%
  \ifcategory{highlight}
    {\textcolor{red}{#1}}
    {#1}}

\DeclareFieldFormat{labelnumber}{%
  \ifcategory{highlight}
    {\textcolor{red}{#1}}
    {#1}}

\DeclareFieldFormat{labelnumberwidth}{%
  \ifcategory{highlight}
    {\textcolor{red}{\mkbibbrackets{#1}}}
    {\mkbibbrackets{#1}}}

\AtEveryBibitem{%
  \ifcategory{highlight}
    {\color{red}}
    {}}


\begin{filecontents}{\jobname.bib}
@article{a,
  author = {Name, A and Name, B},
  author+an = {1=highlight},
  title = {Title A},
  journal = {Journal A}
}
@inproceedings{b,
  author = {Name, E and Name, F},
  title = {Title B},
  booktitle = {Conference B}
}
@article{c,
  author = {Name, C and Name, D},
  title = {Title C},
  journal = {Journal C}
}
@inproceedings{d,
  author = {Name, E and Name, F},
  author+an = {2=highlight},
  title = {Title D},
  booktitle = {Conference D}
}
\end{filecontents}
\addbibresource{\jobname.bib}

\begin{document}
  \cite{a,b,c,d}

  \cite{a}\cite{b}\cite{c}\cite{d}

  \printbibliography
\end{document}

[1, 3, 2, 4] [1][3][2][4] [1] A 名称和 B 名称。“标题 A”。在:期刊 A ()。 [2] C 名称和 D 名称。“标题 C”。在:期刊 C ()。 [3] E 名称和 F 名称。“标题 B”。在:会议 B。 [4] E 名称和 F 名称。“标题 D”。在:会议 D。条目“1”和“4”始终以红色突出显示

相关内容