有没有什么方法可以使用 biblatex 来获取某些 bib-keys / bib-entries 的引用,并且只为它们获取不同的颜色?
在下面的 MWE 中,我制作了一个自定义命令(灵感来自这个答案) 以显示不同颜色的引文。然而,我正在寻找一种让 biblatex/biber自动为引用某些条目的引文添加颜色,并且只为它们添加颜色,无需使用任何自定义命令。在文本中,将使用相同的命令(例如\parencite
),但对于指定的条目(例如articlecoloured
),输出将是彩色的;对于其他条目,则不会。
换句话说,使用 MWE 中“无自定义命令”部分中的引用命令,但获得与“使用自定义命令”部分相同的输出。
该示例包括一个具有两个条目的 bib-content,articlenormal
和articlecoloured
。
我希望能够使用正常的引用命令(\parencite
、\citeyear
等\textcite
),但对于某些 bib-keys(articlecoloured
)输出会带颜色;对于所有其他(例如articlenormal
)则不会。(在我的工作文档中,我为使用的各种引用命令定制了命令,它们都像在 MWE 中一样带颜色输出。在 MWE 中,我只包含了\parencite
。)
是否有可能获得articlecoloured
且仅articlecoloured
拥有自定义颜色不使用自定义引用命令?
也许要对哪些文件条目进行着色可以基于键列表(即articlecoloured
),或者在文件条目中包含相关关键字?
(如果指定要着色的条目在参考书目中也是彩色的,则可以获得加分。)
\documentclass{article}
\usepackage{xcolor}
\definecolor{citecolor}{RGB}{12,127,172}
\begin{filecontents*}{mybib.bib}
@ARTICLE{articlenormal,
author = {Normal, Article},
title = {Normal},
year = {2020}
}
@ARTICLE{articlecoloured,
author = {Coloured, Article},
title = {Coloured},
year = {2020}
}
\end{filecontents*}
\usepackage[style=apa,backend=biber]{biblatex}
\DeclareLanguageMapping{english}{british-apa}
\bibliography{mybib}
% Using custom command
\newcommand{\mkbibparenscoloured}[1]{\textcolor{citecolor}{\mkbibparens{#1}}}
\DeclareDelimFormat[parencite]{finalnamedelim}
{\ifnum\value{liststop}>2 \finalandcomma\fi\addspace\&\space}
\DeclareCiteCommand{\colparencite}[\mkbibparenscoloured]
{\usebibmacro{cite:init}%
\usebibmacro{prenote}%
\toggletrue{apa:inpcite}}
{\usebibmacro{citeindex}%
\usebibmacro{cite}%
\usebibmacro{cite:post}}
{}
{\usebibmacro{postnote}%
\togglefalse{apa:inpcite}}
\begin{document}
\section*{No custom commands}
Citation, should be normal colour \parencite{articlenormal}.
Citation, should be differently coloured \parencite{articlecoloured}.
\section*{Using custom commands}
Citation, should be normal colour \parencite{articlenormal}.
Citation, should be differently coloured \colparencite{articlecoloured}.
\printbibliography{}
\end{document}
答案1
cite
本质上我们‘只是’需要采用和bibmacros的原始定义textcite
并在其中添加一些颜色。
原始定义可以在apa.cbx
(ll. 189-261 在 v9.6 中)。
在此示例中,你可以定义要用颜色标记哪些引文
\addtocategory{coloured}{<key_1,key_2,...,key_n>}
因此,如果条目仅在某个文档的上下文中着色而不是其本身着色,则这很有用。
\documentclass{article}
\usepackage{xcolor}
\definecolor{citecolour}{RGB}{12,127,172}
\usepackage[style=apa,backend=biber]{biblatex}
\DeclareBibliographyCategory{coloured}
\addtocategory{coloured}{articlecoloured}
\DeclareFieldFormat{citecolour:conditional}{%
\ifcategory{coloured}
{\textcolor{citecolour}{#1}}
{#1}}
\makeatletter
\renewbibmacro*{cite}{%
\iffieldequals{fullhash}{\cbx@lasthash}
{\setunit{\compcitedelim}%
\printtext[citecolour:conditional]{%
\usebibmacro{cite:plabelyear+extradate}}}%
{\printtext[citecolour:conditional]{%
\ifnameundef{labelname}
{\usebibmacro{cite:noname}%
\setunit{\printdelim{nameyeardelim}}%
\usebibmacro{cite:plabelyear+extradate}%
\savefield{fullhash}{\cbx@lasthash}}
{\ifnameundef{shortauthor}
{\printnames{labelname}}%
{\cbx@apa@ifnamesaved
{\printnames{shortauthor}}
{\ifnameundef{groupauthor}
{\printnames[labelname]{author}}
{\printnames[labelname]{groupauthor}}%
\addspace\printnames[sabrackets]{shortauthor}}}%
\setunit{\printdelim{nameyeardelim}}%
\usebibmacro{cite:plabelyear+extradate}%
\savefield{fullhash}{\cbx@lasthash}}}}%
\setunit{\multicitedelim}}
\renewbibmacro*{textcite}{%
\iffieldequals{fullhash}{\cbx@lasthash}
{\setunit{\compcitedelim}%
\printtext[citecolour:conditional]{%
\usebibmacro{cite:plabelyear+extradate}}}
{%
\ifbool{cbx:parens}
{\bibcloseparen\global\boolfalse{cbx:parens}}
{}%
\setunit{\compcitedelim}%
\ifnameundef{labelname}
{\iffieldundef{shorthand}%
{\printtext[citecolour:conditional]{%
\usebibmacro{cite:noname}}%
\setunit{\ifbool{cbx:np}%
{\printdelim{nameyeardelim}}%
{\global\booltrue{cbx:parens}\addspace\bibopenparen}}%
\printtext[citecolour:conditional]{%
\usebibmacro{cite:plabelyear+extradate}}}
{\printtext[citecolour:conditional]{%
\usebibmacro{cite:shorthand}}}}
{\printtext[citecolour:conditional]{%
\ifnameundef{shortauthor}%
{\printnames{labelname}}
{\cbx@apa@ifnamesaved
{\printnames{shortauthor}}
{\ifnameundef{groupauthor}
{\printnames[labelname]{author}}
{\printnames[labelname]{groupauthor}}}}}%
\setunit{\ifbool{cbx:np}
{\printdelim{nameyeardelim}}
{\global\booltrue{cbx:parens}\addspace\bibopenparen}}%
\printtext[citecolour:conditional]{%
\ifnameundef{shortauthor}
{}
{\cbx@apa@ifnamesaved
{}
{\printnames{shortauthor}\setunit{\printdelim{nameyeardelim}}}}%
\usebibmacro{cite:plabelyear+extradate}}%
\savefield{fullhash}{\cbx@lasthash}}}}
\DeclareCiteCommand{\fullcite}
{\usebibmacro{prenote}}
{\usedriver
{\ifcategory{coloured}
{\color{citecolour}}
{}%
\DeclareNameAlias{sortname}{default}%
\global\boolfalse{bbx:in}%
\global\let\blx@related@loop\@empty}
{\thefield{entrytype}}%
\usebibmacro{cite:post}}
{\multicitedelim}
{\usebibmacro{postnote}}
\DeclareCiteCommand{\fullcitebib}
{\list{}
{\setlength{\leftmargin}{\bibhang}%
\setlength{\itemindent}{-\leftmargin}%
\setlength{\itemsep}{\bibitemsep}%
\setlength{\parsep}{\bibparsep}}\item}
{\usedriver
{\ifcategory{coloured}
{\color{citecolour}}
{}%
\DeclareNameAlias{sortname}{default}%
\global\boolfalse{bbx:in}}
{\thefield{entrytype}}%
\finentry
\usebibmacro{cite:post}}
{\item}
{\endlist}
\makeatother
% for the bibliography
\AtEveryBibitem{%
\ifcategory{coloured}
{\color{citecolour}}
{}}
\begin{filecontents*}{\jobname.bib}
@article{articlenormal,
author = {Normal, Article},
title = {Normal},
year = {2020},
}
@article{articlecoloured,
author = {Coloured, Article},
title = {Coloured},
year = {2020},
}
\end{filecontents*}
\addbibresource{\jobname.bib}
\begin{document}
Citation, should be normal colour \parencite{articlenormal}.
Citation, should be differently coloured \parencite{articlecoloured}.
Citation, should be normal colour \textcite{articlenormal}.
Citation, should be differently coloured \textcite{articlecoloured}.
\printbibliography
\end{document}
如果条目本身就带有色彩,那么使用keyowrds
只需更换
\ifcategory{coloured}
以上
\ifkeyword{coloured}
并使用
keywords = {coloured},
在.bib
文件中。