更改参考书目中某些参考文献的文本颜色(使用 Bibtex)

更改参考书目中某些参考文献的文本颜色(使用 Bibtex)

使用 BibTeX,我需要更改一些参考文献的文本颜色。在文件中.bib,使用{\color{} }作者姓名、标题等可以。但我想知道,有没有更好的解决方案?

以下是 MWE:

\documentclass{article}
\usepackage{color}
\begin{filecontents}{refs.bib}
    @ARTICLE{A1, 
        author={{\color{blue} Author A1}}, 
        journal={{\color{blue}Journal A1}}, 
        title={{\color{blue}Title A1}}, 
        year={{\color{blue}2015}}, 
        volume={{\color{blue}1}}, 
        number={{\color{blue}3}}, 
        pages={{\color{blue}1-5}},} 
    @ARTICLE{A2, 
        author={{\color{blue} Author A2}}, 
        journal={{\color{blue}Journal A2}}, 
        title={{\color{blue}Title A2}}, 
        year={{\color{blue}2016}}, 
        volume={{\color{blue}1}}, 
        number={{\color{blue}3}}, 
        pages={{\color{blue}5-10}},}    
    @ARTICLE{A3, 
        author={Author A3}, 
        journal={Journal A3}, 
        title={Title A3}, 
        year={2017}, 
        volume={3}, 
        number={1}, 
        pages={11-15}, }
\end{filecontents}
\begin{document}
    In the bibliography, I need to highlight the references~\cite{A1} and~\cite{A2} in a different color. The reference~\cite{A3} is not required to be highlighted.
    \bibliographystyle{spphys}       
    \bibliography{refs}
\end{document}

答案1

将以下代码添加到您的 MWE:

\makeatletter % <=======================================================
\let\myorg@bibitem\bibitem
\def\bibitem#1#2\par{%
  \@ifundefined{bibitem@#1}{%
    \myorg@bibitem{#1}#2\par
  }{%
    \begingroup
      \color{\csname bibitem@#1\endcsname}%
      \myorg@bibitem{#1}#2\par
    \endgroup
  }%
}
\newcommand*{\bibitem@test}{blue}    % <==================
\newcommand*{\bibitem@green}{green}  % <==================
%\newcommand*{\bibitem@A1}{red}      % <================== error A1
\makeatother % <========================================================

它定义了一个新命令,例如 \newcommand*{\bibitem@test}{blue},其中test是 bib 条目的键,blue是条目要使用的颜色。我还定义了\newcommand*{\bibitem@green}{green}获取绿色引用。请注意,bibentry 的键不能使用数字。要测试这一点,请删除行首%%\newcommand*{\bibitem@A1}{red}然后您将收到错误。请注意,您必须在和之间写入命令\makeatletter\makeatother因为名称包含@

编译以下完整的 MWE

\RequirePackage{filecontents}
\begin{filecontents}{\jobname.bib}
@ARTICLE{A1, 
  author={{\color{blue} Author A1}}, 
  journal={{\color{blue}Journal A1}}, 
  title={{\color{blue}Title A1}}, 
  year={{\color{blue}2015}}, 
  volume={{\color{blue}1}}, 
  number={{\color{blue}3}}, 
  pages={{\color{blue}1-5}},
} 
@ARTICLE{green, 
  author={Author A2}, 
  journal={Journal A2}, 
  title={Title A2}, 
  year={2016}, 
  volume={1}, 
  number={3}, 
  pages={5-10},
}    
@ARTICLE{test, 
  author={Author A3}, 
  journal={Journal A3}, 
  title={Title A3}, 
  year={2017}, 
  volume={3}, 
  number={1}, 
  pages={11-15}, 
}
\end{filecontents}


\documentclass{article}

\usepackage{color}

\makeatletter % <=======================================================
\let\myorg@bibitem\bibitem
\def\bibitem#1#2\par{%
  \@ifundefined{bibitem@#1}{%
    \myorg@bibitem{#1}#2\par
  }{%
    \begingroup
      \color{\csname bibitem@#1\endcsname}%
      \myorg@bibitem{#1}#2\par
    \endgroup
  }%
}
\newcommand*{\bibitem@test}{blue}    % <==================
\newcommand*{\bibitem@green}{green}  % <==================
%\newcommand*{\bibitem@A1}{red}      % <================== error A1
\makeatother % <========================================================


\begin{document}
In the bibliography, I need to highlight the references~\cite{A1} 
and~\cite{green} in a different color. 
The reference~\cite{test} is not required to be highlighted.

\bibliographystyle{plain} % spphys
\bibliography{\jobname}
\end{document}

得到结果:

生成的 pdf

参考文献[3]是您的彩色书目条目(您看到黑色的参考编号吗?),参考文献[1]并由[2]新命令着色...因为我没有您的文件,所以spphys.bst我使用样式plain来代替...

相关内容