TransWikia.com

How can I colorise the background of every second bibliography entry with biblatex

TeX - LaTeX Asked by Doan on July 24, 2021

I want colorize the background of every second entry in the bibliography list.
Here is a minimal example:

documentclass{scrbook}
usepackage[utf8]{inputenc}

usepackage[backend=biber,
        isbn=true,
        giveninits=true,
        style=numeric,
        maxnames=99,
        sorting=ydnt,
        defernumbers=true,
        autocite=superscript]{biblatex}
defbibheading{bibliography}[refname]{}
addbibresource{references.bib}


usepackage{xcolor}


begin{document}

section{Main text}

cite{small}

cite{big}

section{Bibliography}
printbibliography

end{document}

Here is an example of Literature examples:

@book{bal2009,    
     shorthand = bal2009,
     title={Lehrbuch der Objektmodellierung: Analyse und Entwurf mit der UML 2},
  author={Balzert, Heide},
  year={2009},
  publisher={Spektrum Akademischer Verlag}
}

@book{Dem2013,
    shorthand = Dem2013,
  title={Raspberry Pi - Das Handbuch},
  author={Dembowski, K.},
  year={2013},
  publisher={Springer Fachmedien Wiesbaden}
}

@book{Kof2015,
  title={Raspberry Pi: Das umfassende Handbuch, komplett in Farbe - aktuell zu Raspberry Pi 2 - inkl. Schnittstellen, Schaltungsaufbau, Steuerung mit Python und den Erweiterungen Gertboard, PiFace und Quick2Wire},
  author={Kofler, M. and K{"u}hnast, C. and Scherbeck, C.},
  isbn={9783836237956},
  year={2015},
  publisher={Galileo Press},
    adress={Bonn},
    edition = {1. Aufl.},
    number={1. korrigierter Nachdr. Ed.},
}

@book{Upt2014,
    shorthand = Upt2014,
  title={Raspberry Pi User Guide},
  author={Upton, E. and Halfacree, G.},
  isbn={9781118921678},
  year={2014},
    publisher={John Wiley & Sons Ltd},
    adress={Chichester, West Sussex},
    edition = {2. Aufl.}
}

One Answer

The only viable solution for colouring bibliographies that I could find uses TikZ. Other solutions turned out to be impractical when they required the text as a macro argument.

The approach is similar to what Clau­dio Fian­drino's hf-tikz does. We mark the beginning of a bibliography item (with zebrabibstart, for numeric styles that command must be injected into the definition of the bibliography environment) and use An­drew Stacey's tikzmark to mark the end of an entry (with zebrabibend). Then we draw a rectangle between the starting point and a point that has the y-coordinate of the end point and the x value defined by textwidth.

documentclass{article}

usepackage[backend=biber,
  style=numeric,
  sorting=ydnt,
  defernumbers=true,
  maxnames=99,
  giveninits=true,
  autocite=superscript,
  isbn=true,]{biblatex}

addbibresource{biblatex-examples.bib}

usepackage{tikz}
usetikzlibrary{tikzmark}
usetikzlibrary{calc}

newcounter{zebrabibentry}
newcounter{zebrabibbibenv}

defbibenvironment{bibliography}
  {list
     {stepcounter{zebrabibentry}%
      zebrabibstart{zebrabib-%
        thevalue{zebrabibbibenv}-%
        thevalue{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=ifoddvalue{zebrabibentry}whiteelse blue!20fi,
    fill=ifoddvalue{zebrabibentry}whiteelse blue!10fi,]
    let p1=(pic cs:#2) in
    ({0pt,10pt}) node [anchor=base] (#2){} rectangle  (columnwidth+2pt,y1-itemsep)
    ;%
}

newcommandzebrabibend[2][]{%
  tikz[remember picture with id=#2] #1;}

renewbibmacro{finentry}{%
  finentry
  zebrabibend{zebrabib-%
    thevalue{zebrabibbibenv}-%
    thevalue{zebrabibentry}}}

begin{document}
section{Main text}
cite{sigfridsson,worman,geer,nussbaum}

printbibliography[heading=bibnumbered]
end{document}

The MWE produces a numeric bibliography with four entries. Entry two and four have a bluish background colour.

Answered by moewe on July 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP