4

I'd like to highlight particular bibliography entries by changing the background color. I really like the way this looks in moewe's solution here, but I want to specify which entries are being highlighted by setting a keyword, rather than highlighting every second entry.

The highlighting methods in these answers allow me to select bibliography entries based on keywords, but I can only figure out how to use to it to highlight the text color, not the background color.

Can someone suggest a way to highlight entries with a specific keyword by changing the background color (with the linked method or some other one)? I use Biblatex, but am bound to the bibtex backend and regular pdflatex.

Thanks!

Here's a (trivial) MWE:

\documentclass[fontsize=11pt,twoside,numbers=noendperiod]{scrartcl}

\usepackage[ citestyle=numeric, bibstyle=authoryear, sorting=none, ,uniquename=false ,uniquelist=false ,backref=false ,block=space ,maxbibnames=99 ,maxcitenames=2 ,doi=false ,url=false ,date=year ,backend=bibtex ]{biblatex}

\makeatletter \input{numeric.bbx} \makeatother

\addbibresource{highlighting.bib}

\begin{document}

\cite{bender-koller-2020-climbing,hovy21biases}

\printbibliography

\end{document}

Here's highlighting.bib:


@article{hovy21biases,
  author          = {Dirk Hovy and Shrimai Prabhumoye},
  journal         = {Language and Linguistics Compass},
  number          = {15},
  issue = {8},
  title           = {Five sources of bias in natural language processing},
  year            = {2021},
  doi = {10.1111/lnc3.12432}
}

@inproceedings{bender-koller-2020-climbing, title = "Climbing towards {NLU}: {On} meaning, form, and understanding in the {Age} of {Data}", author = "Bender, Emily M. and Koller, Alexander", booktitle = "Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics (ACL)", month = jul, year = "2020", keywords = {pi}, url = "https://www.aclweb.org/anthology/2020.acl-main.463", doi = "10.18653/v1/2020.acl-main.463", }

1 Answers1

3

We can copy most of the approach from How can I colorise the background of every second bibliography entry with biblatex and just guard the bits that do the highlighting with \ifkeyword{pi}.

\documentclass[fontsize=11pt,twoside,numbers=noendperiod]{scrartcl}

\usepackage[ citestyle=numeric, bibstyle=authoryear, sorting=none, ,uniquename=false ,uniquelist=false ,backref=false ,block=space ,maxbibnames=99 ,maxcitenames=2 ,doi=false ,url=false ,date=year ,backend=bibtex ]{biblatex} \usepackage{tikz} \usetikzlibrary{tikzmark} \usetikzlibrary{calc}

\makeatletter \input{numeric.bbx} \makeatother \setlength{\bibitemsep}{\itemsep}

\newcounter{zebrabibentry} \newcounter{zebrabibbibenv}

\defbibenvironment{bibliography} {\list {\ifkeyword{pi} {\stepcounter{zebrabibentry}% \zebrabibstart{zebrabib-% \the\value{zebrabibbibenv}-% \the\value{zebrabibentry}}} {}% \printtext[labelnumberwidth]{% \printfield{labelprefix}% \printfield{labelnumber}}} {\stepcounter{zebrabibbibenv}% \setcounter{zebrabibentry}{0}% \setlength{\labelwidth}{\labelnumberwidth}% \setlength{\leftmargin}{\labelwidth}% \setlength{\labelsep}{\biblabelsep}% \addtolength{\leftmargin}{\labelsep}% \setlength{\itemsep}{\bibitemsep}% \setlength{\parsep}{\bibparsep}}% \renewcommand*{\makelabel}[1]{\hss##1}} {\endlist} {\item}

\newcommand*{\zebrabibstart}[2][]{% \tikz[remember picture,overlay] \draw[line width=1pt,rectangle, draw=blue!20, fill=blue!10,] let \p1=(pic cs:#2) in ({0pt,10pt}) node [anchor=base] (#2){} rectangle (\columnwidth+2pt,\y1-\bibitemsep);% }

\newcommand\zebrabibend[2][]{% \tikz[remember picture with id=#2] \node {#1};}

\renewbibmacro{finentry}{% \finentry \ifkeyword{pi} {\zebrabibend{zebrabib-% \the\value{zebrabibbibenv}-% \the\value{zebrabibentry}}} {}}

\begin{filecontents}{\jobname.bib} @article{hovy21biases, author = {Dirk Hovy and Shrimai Prabhumoye}, journal = {Language and Linguistics Compass}, number = {15}, issue = {8}, title = {Five sources of bias in natural language processing}, year = {2021}, doi = {10.1111/lnc3.12432} } @inproceedings{bender-koller-2020-climbing, title = "Climbing towards {NLU}: {On} meaning, form, and understanding in the {Age} of {Data}", author = "Bender, Emily M. and Koller, Alexander", booktitle = "Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics (ACL)", month = jul, year = "2020", keywords = {pi}, url = "https://www.aclweb.org/anthology/2020.acl-main.463", doi = "10.18653/v1/2020.acl-main.463", } \end{filecontents} \addbibresource{\jobname.bib}

\begin{document} \autocite{bender-koller-2020-climbing,hovy21biases}

\printbibliography \end{document}

Bibliography with two entries: one is highlighted.

moewe
  • 175,683
  • This is fantastic, thank you for the quick response! – TheoryOfRain Aug 17 '22 at 15:53
  • I do have a followup question. The highlighted box seems to be positioned relative to the leftmost character of the bib entry, rather than any particular horizontal position on the page.

    If the width of the numeric label changes, this shifts the highlighted box to the side. Can we set it up in such a way that all boxes are horizontally aligned with each other?

    Screenshot

    (Sorry, for some reason my screenshot won't show as a picture - still learning how to post to Stack Exchange.)

    – TheoryOfRain Aug 17 '22 at 17:00
  • @TheoryOfRain Add a \hfill before the \zebrabibend call in finentry. – moewe Aug 17 '22 at 19:25
  • You mean like this?
    \renewbibmacro{finentry}{%
      \finentry
      \ifkeyword{pi}
        {\hfill\zebrabibend{zebrabib-%
           \the\value{zebrabibbibenv}-%
           \the\value{zebrabibentry}}}
        {}}
    

    That didn't make a difference, as far as I can tell.

    – TheoryOfRain Aug 18 '22 at 04:52
  • Here's an MWE with 100 citations to test it: highlighting.tex.

    The interesting part is the step from citation 99 to citation 100.

    – TheoryOfRain Aug 18 '22 at 04:57