请查看此链接中给出的问题和答案以获取颜色参考这里 它适用于两个参考文献。如何编辑它以适用于四个或五个参考文献?
这是解决方案的副本。但我无法添加更多嵌套的 ifstrequal:
\let\mybibitem\bibitem
\renewcommand{\bibitem}[1]{%
\ifstrequal{#1}{<BibtexKey1>}
{\color{blue}\mybibitem{#1}}% IF #1==<BibtexKey1>
\ifstrequal{#1}{<BibtexKey2>}
{\color{blue}\mybibitem{#1}}% IF #1==<BibtexKey1>
{% ELSE
\ifstrequal{#1}{<BibtexKey3>}
{\color{blue}\mybibitem{#1}}% IF #1==<BibtexKey1>
{\color{black}\mybibitem{#1}}% ELSE
}%
}
答案1
如下嵌套多个\ifstrequal
应该不是问题。
基本语法是
\let\mybibitem\bibitem
\renewcommand{\bibitem}[1]{%
\ifstrequal{#1}{<BibtexKey1>}%
{%
% what happens if you key == <BibtexKey1>
}%
{%
% what happens if it's another key
% this is where you want to define your next `\ifstrequal` statement.
}%
}
因此,通过突出显示<BibtexKey1>
和<BibtexKey2>
,您可以获得:
\let\mybibitem\bibitem
\renewcommand{\bibitem}[1]{%
\ifstrequal{#1}{<BibtexKey1>}%
{%
% what happens if you key == <BibtexKey1>
\color{red}\mybibitem{#1}%
}%
{%
\ifstrequal{#1}{<BibtexKey2>}%
{%
% what happens if you key == <BibtexKey2>
\color{red}\mybibitem{#1}%
}%
{%
% what happens if it's another key
\color{black}\mybibitem{#1}%
}%
}%
}
<BibtexKey3>
如果你也想突出显示,则可以给出:
\let\mybibitem\bibitem
\renewcommand{\bibitem}[1]{%
\ifstrequal{#1}{<BibtexKey1>}%
{%
% what happens if you key == <BibtexKey1>
\color{red}\mybibitem{#1}%
}%
{%
\ifstrequal{#1}{<BibtexKey2>}%
{%
% what happens if you key == <BibtexKey2>
\color{red}\mybibitem{#1}%
}%
{%
\ifstrequal{#1}{<BibtexKey3>}%
{%
% what happens if you key == <BibtexKey3>
\color{red}\mybibitem{#1}%
}%
{%
% what happens if it is another key
\color{black}\mybibitem{#1}%
}%
}%
}%
}
等等。
这是一个快速的解决方案 - 但确实很肮脏。
答案2
嵌套多个\ifstrequal
很麻烦。这里有一个expl3
避免嵌套的方法。
\documentclass{article}
\usepackage{xparse,xcolor}
\ExplSyntaxOn
\NewDocumentCommand{\setupbibcolors}{m}
{
\cs_set_protected:Npn \bibitem ##1
{
\color{ \str_case:nnF { ##1 } { #1 } { black } }
\heba_bibitem:n { ##1 }
}
}
\cs_set_eq:NN \heba_bibitem:n \bibitem
\ExplSyntaxOff
\setupbibcolors{
{Pinter2019}{green!70!red}
{JainAndWallace2019}{red!60}
{SurveyOfAttentionModels}{yellow!40!black}
{Vig2019}{blue!40}
{TransformerXL}{cyan!60!yellow}
}
\begin{document}
\nocite{*}
\bibliographystyle{plain}
\bibliography{chico}
\end{document}
您可以根据需要设置任意数量的条目,且颜色无需不同。
由于懒惰,bib文件取自我的另一个答案,请参阅我无法解决的 BibTex 错误