1

Please look at the question and the answer given in this link for coloring referenceshere It works with two references. How can it be edited for four or five references?

Here is a copy of the solution. But I am not able to add more nested 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
  }%
 }
4
  • Please clarify what you mean by "I am not able to add more nested \ifstrequal". Are you getting error or warning messages (and, if so, what do the messages say?), or are you not sure how to carry out the additional nesting steps?
    – Mico
    Commented Jan 25, 2020 at 9:19
  • @Mico. Actually the code above works very well with 2 references. However, when I tried to color 3 references, I copy and paste \ifstrequal{#1} with changing the bibtex Key. However, it does not work. My question is how to color more than 2 refeences. Commented Jan 25, 2020 at 17:58
  • Let me rephrase my question: "What exactly does "it does not work" entail?"
    – Mico
    Commented Jan 25, 2020 at 18:05
  • I updated the code above. Simply, what I did to get "BibtexKey2" colored like BibtexKey1 and BibtexKey3. I just want to Syntax that I use Commented Jan 25, 2020 at 18:09

2 Answers 2

1

Nesting multiple \ifstrequal as follows shouldn't be an issue.

The basic syntax is

\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.
    }%
}

Thus, you get for highlighting both <BibtexKey1> and <BibtexKey2>, this gives:

\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}%
        }%
    }%
}

If you want to highlight <BibtexKey3> as well, this gives:

\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}%
            }%
        }%
    }%
}

Et cætera.

This works as a quick solution — yet it is dirty indeed.

3

Nesting multiple \ifstrequal is a pain. Here's a way with expl3 that avoids nesting.

\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}

You can set up as many entries as you want and the colors need not be different.

Due to laziness, the bib file is taken from another answer of mine, see BibTex error that I can't solve

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .