如何使用 biblatex 为每个第二个参考书目条目的背景着色

如何使用 biblatex 为每个第二个参考书目条目的背景着色

我想为参考书目列表中的每条第二条目的背景添加颜色。这是一个简单的例子:

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

以下是文学范例:

@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

我发现唯一可行的着色书目解决方案是使用 TikZ。当其他解决方案需要将文本作为宏参数时,它们被证明是不切实际的。

该方法类似于克劳迪奥·菲安德里诺hf-tikz确实如此。我们标记书目项目的开始(使用\zebrabibstart,表示numeric必须将命令注入书目环境的定义中),并使用 Andrew Stacey 的tikzmark标记条目的结尾(用\zebrabibend)。然后我们在起点和具有-终点的坐标和X定义的值\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-\bibitemsep)
    ;%
}

\newcommand\zebrabibend[2][]{%
  \tikz[remember picture with id=#2] \node {#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}

MWE 生成包含四个条目的数字书目。条目二和条目四的背景颜色为蓝色。

相关内容