如何使用 biblatex 为参考书目条目完全着色

如何使用 biblatex 为参考书目条目完全着色

我想为某些参考书目条目添加颜色,例如“重要”和“获奖”论文。目前我拥有:

\documentclass{article}
\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}
\renewbibmacro{in:}{}

\usepackage[usenames,dvipsnames]{xcolor}

\DeclareBibliographyCategory{important}
\DeclareBibliographyCategory{award}

\addtocategory{important}{small}
\addtocategory{award}{big}

\AtEveryBibitem{
\ifcategory{award}%
    {\color{blue}}%
    {}%
\ifcategory{important}%
    {\color{orange}}%
    {}%
}

\begin{document}

\section{Main text}

\cite{small}

\cite{big}

\section{Bibliography}
\printbibliography

\end{document}

但是,只有 bibentry 本身是橙色(或蓝色,下面未显示),而不是旁边的参考编号。我该如何实现?

在此处输入图片描述

答案1

您可以简单地使用\AtBeginBibliography\AtEveryBibitem设置仅为该类别的参考书目条目着色的代码important

PS = 由于您没有添加文件示例.bib,因此我使用了biblatex-examples.bib

\documentclass{article}
\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{biblatex-examples.bib}
\renewbibmacro{in:}{}

\usepackage[usenames,dvipsnames]{xcolor}

\DeclareBibliographyCategory{important}
\addtocategory{important}{knuth:ct:a}
\addtocategory{important}{knuth:ct:c}

\AtBeginBibliography{%
    \DeclareFieldFormat{labelnumberwidth}{\ifcategory{important}%
        {\color{orange}\mkbibbrackets{#1}}%
        {\mkbibbrackets{#1}}%
    }}
\AtEveryBibitem{\ifcategory{important}
    {\color{orange}}
    {}}

\begin{document}

    \section{Main text}

    \cite{knuth:ct}
    \cite{knuth:ct:a}
    \cite{knuth:ct:b}       
    \cite{knuth:ct:c}       
    \cite{companion}

    \section{Bibliography}

    \printbibliography

\end{document}

在此处输入图片描述

编辑:

\ifcategory命令的语法如下:

\ifcategory{hcategoryi}{htruei}{hfalsei}

\if与其他命令类似,请参阅biblatex手册第 4.6.2 节“独立测试”。

htruei或之内hfalsei你可以嵌套任意数量的\ifcategorys(或其他\ifs),你只需要注意匹配所有的括号。

以下是对三个不同类别使用三种不同颜色的示例:

\documentclass{article}
\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{biblatex-examples.bib}
\renewbibmacro{in:}{}

\usepackage[usenames,dvipsnames]{xcolor}

\DeclareBibliographyCategory{important}
\addtocategory{important}{knuth:ct:a}
\addtocategory{important}{knuth:ct:c}
\DeclareBibliographyCategory{awards}
\addtocategory{awards}{knuth:ct:b}
\DeclareBibliographyCategory{ducks}
\addtocategory{ducks}{companion}

\AtBeginBibliography{%
    \DeclareFieldFormat{labelnumberwidth}%
        {\ifcategory{important}% if
            {\color{orange}\mkbibbrackets{#1}}% then
            {\ifcategory{awards}% else if
                {\color{blue}\mkbibbrackets{#1}}% then
                {\ifcategory{ducks}% else if
                    {\color{green}\mkbibbrackets{#1}}% then
                    {{\mkbibbrackets{#1}}%else
                }% end if
            }% end if
        }% end if
}}
\AtEveryBibitem%
    {\ifcategory{important}% if
        {\color{orange}}% then
        {\ifcategory{awards}% else if
            {\color{blue}}% then
            {\ifcategory{ducks}% else if
                {\color{green}}% then
                {}%else
            }% end if
        }% end if
    }% end if

\begin{document}

    \section{Main text}

    \cite{knuth:ct}
    \cite{knuth:ct:a}
    \cite{knuth:ct:b}       
    \cite{knuth:ct:c}       
    \cite{companion}

    \section{Bibliography}

    \printbibliography

\end{document}

在此处输入图片描述

相关内容