3

Using BibTeX, I need to change the text color of some references. In the .bib file, using {\color{} } for the author names, title, ..., works. But I was wondering, is there a better solution?

Here is a 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}
0
2

Add the following code to your 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 % <========================================================

It defines a new command, for example \newcommand*{\bibitem@test}{blue}, where test is the key of the bib entry and blue is the color to be used for the entry. I also defined \newcommand*{\bibitem@green}{green} to get a green reference. Please see that the key of the bibentry can not use numbers. To test this delete the leading % in line %\newcommand*{\bibitem@A1}{red}. Then you will get an error. Please see that you have to write the command between \makeatletter and \makeatother because the name contains an @!

Compile the following complete 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}

to get the result:

resulting pdf

Reference [3] is your colored bib entry (you see the black reference number?), references [1] and [2] are colored by the new commands ... Because I do not have your file spphys.bst I used style plain instead ...

1
  • is there a way to change the color of those lack parentheses in reference [3]? I can change the color of the right one but not the color of the left one ...
    – QGravity
    Sep 26 at 0:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.