[LaTeX attempt] change the color of the specified reference entry

Occasionally, when you encounter a need, you want to highlight a specific reference item in a specified color. One scenario is to respond to the reviewer's request to add references.

One solution is to manually modify the. bbl file after normal compilation, and then run pdflatex/xelatex again.
This scheme is very free, but not convenient, because. bbl is regenerated every time bibtex/biber is run.
This article provides a more automatic attempt through macros.

Train of thought combing

Each reference entry is generated by a \ bibitem in the bibliography environment. Among them,
The bibliography environment is a special list environment,
\bibitem is an encapsulated \ item command.
If the. bib document database is used, the. bbl file is a complete thebibliography environment.
According to the usage \ bibitem [] {} and \ cite {}, if it is used in the specification, it corresponds to the literature one by one.
Let's patch \bibitem to use additional color output for a specific, whole \ bibitem.
\The bibitem command calls @ bibitem and @ lbibitem internally, so the last two commands of patch are actually required, and the position of patch is before \ item

Concrete implementation

2021-09-01 update: for each definition\ bib@colored @, replace \ in @{} \ ifin @... \ else... \ fi
2021-09-05 update:
Multiple colors are supported. The usage is changed to \ bibColoredItems {}
Specific implementation, store the color in\ bib@colored @Inside

// An highlighted block
\usepackage{xpatch}

\makeatletter
\ExplSyntaxOn
% #1 = color
% #2 = list of bib items
\cs_new:Npn \bibColoredItems #1#2
  {
    \clist_map_inline:nn {#2} { \cs_new:cpn {bib@colored@##1} {#1} } 
  }
\ExplSyntaxOff

% #1 = one bib item
\newcommand\bib@setcolor[1]{%
  \ifcsname bib@colored@#1\endcsname
    \expandafter\color\expandafter{\csname bib@colored@#1\endcsname}
  \else
    \normalcolor
  \fi
}

\xpatchcmd\@bibitem
  {\item}
  {\bib@setcolor{#1}\item}
  {}{\fail}

\xpatchcmd\@lbibitem
  {\item}
  {\bib@setcolor{#2}\item}
  {}{\fail}
\makeatother

% usage
\bibColoredItems{<color1>}{<item1a>, <item1b>}
\bibColoredItems{<color2>}{<item2a>, <item2b>}

Among them,

\The definition of bibitem in latex2e format is
\def\bibitem{@ifnextchar[@lbibitem@bibitem}
\in @ {} {} is a tool macro provided by latex2e to check whether it is included. If it contains, then \ ifin @ is true, otherwise it is false.
\normalcolor is a macro provided by latex2e to restore to the default color. Similar commands include \ normalfont, \normalsize, etc.

Test effect

Complete documentation for testing

\begin{filecontents}[force]{bibtex.bib}
@inproceedings{ren2015faster,
  title={Faster r-cnn: Towards real-time object detection with region proposal networks},
  author={Ren, Shaoqing and He, Kaiming and Girshick, Ross and Sun, Jian},
  booktitle={Advances in neural information processing systems},
  pages={91--99},
  year={2015}
}

@inproceedings{girshick2015fast,
  title={Fast r-cnn},
  author={Girshick, Ross},
  booktitle={Proceedings of the IEEE international conference on computer vision},
  pages={1440--1448},
  year={2015}
}

@inproceedings{sharif2014cnn,
  title={CNN features off-the-shelf: an astounding baseline for recognition},
  author={Sharif Razavian, Ali and Azizpour, Hossein and Sullivan, Josephine and Carlsson, Stefan},
  booktitle={Proceedings of the IEEE conference on computer vision and pattern recognition workshops},
  pages={806--813},
  year={2014}
}

@misc{misc,
  author       = {X Peter Isley}, 
  title        = {The title of the work},
  howpublished = {How it was published},
  month        = 7,
  year         = 1993,
  note         = {An optional note}
}
\end{filecontents}

\documentclass{article}
\usepackage{xcolor}
\usepackage{xpatch}

\makeatletter
\ExplSyntaxOn
% #1 = color
% #2 = list of bib items
\cs_new:Npn \bibColoredItems #1#2
  {
    \clist_map_inline:nn {#2} { \cs_new:cpn {bib@colored@##1} {#1} } 
  }
\ExplSyntaxOff

% #1 = one bib item
\newcommand\bib@setcolor[1]{%
  \ifcsname bib@colored@#1\endcsname
    \expandafter\color\expandafter{\csname bib@colored@#1\endcsname}
  \else
    \normalcolor
  \fi
}

\xpatchcmd\@bibitem
  {\item}
  {\bib@setcolor{#1}\item}
  {}{\fail}

\xpatchcmd\@lbibitem
  {\item}
  {\bib@setcolor{#2}\item}
  {}{\fail}
\makeatother

\begin{document}
\nocite{*}
\bibliographystyle{plain}
\bibColoredItems{red}{misc, sharif2014cnn}
\bibColoredItems{blue}{ren2015faster}
aaa

\bibliography{bibtex}
bbb
\end{document}

Among them,

The filecontents environment writes the environment contents to an external file named bibtex.bib (located in the same directory as the main file).
It is used here to provide. tex and. bib files in one example at the same time, so as to facilitate copy paste local operation.

Posted by tomdumont on Tue, 09 Nov 2021 03:57:02 -0800