7

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.}
}
1
6

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-%
        \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=\ifodd\value{zebrabibentry}white\else blue!20\fi,
    fill=\ifodd\value{zebrabibentry}white\else blue!10\fi,]
    let \p1=(pic cs:#2) in
    ({0pt,10pt}) node [anchor=base] (#2){} rectangle  (\columnwidth+2pt,\y1-\itemsep)
    ;%
}

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

\renewbibmacro{finentry}{%
  \finentry
  \zebrabibend{zebrabib-%
    \the\value{zebrabibbibenv}-%
    \the\value{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.

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.