1

Is there any way, using biblatex, to get citations for certain bib-keys / bib-entries, and only them, to be differently coloured?

In the MWE below, I've made a custom command (inspired by this answer) for differently coloured citations. However, what I am looking for is a way of having biblatex/biber automatically colour citations referencing certain entries, and only them, without using any custom commands. In the text, the same command would be used (e.g. \parencite) but for specified entries (e.g. articlecoloured) the output would be coloured; for other entries, it would not.

In other words, using the citation commands as in section "No custom commands" in the MWE but getting the same output as in section "Using custom commands".

enter image description here

The example includes a bib-content with two entries, articlenormal and articlecoloured.

I'd like to be able to use the normal citation commands (\parencite, \citeyear, \textcite etc.) but for some bib-keys (articlecoloured) output would be coloured; for all others (e.g. articlenormal) it would not. (In my working document, I have custom commands for the various citation commands I use, which all colour output as in the MWE. In the MWE, I've only included \parencite.)

Is it possible to get articlecoloured, and only articlecoloured, to have a custom colour without using a custom citation command?

Perhaps which bibentries to colour could be based either on a list of keys (i.e. articlecoloured), or to have a relevant keyword in the bibentry?

(Bonus points if specified entry to be coloured is also coloured in the bibliography.)

\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}
8
  • Is it OK if the parentheses around the citation are not coloured? That would make things a lot easier for cases like \parencite{articlenormal,articlecoloured}.
    – moewe
    Apr 4, 2020 at 13:54
  • @moewe Yes! Not only would it be OK but even preferred, given that I would have cases such as the one in your example (mixing non-coloured references with coloured in citations)! Hadn't thought of that.
    – krissen
    Apr 4, 2020 at 13:58
  • Phew! Still \textcite is going to be tricky, I'll see what can be done. (Of course biblatex-apa is quite a complex style, so things are going to be more tricky than with your average authoryear. Can you please check the version of biblatex-apa that you have installed? You can add \listfiles to the beginning of your file and then read off the version in the .log file output.)
    – moewe
    Apr 4, 2020 at 14:01
  • biblatex.def 2019/12/01 v3.14 biblatex compatibility (PK/MW) standard.bbx 2019/12/01 v3.14 biblatex bibliography style (PK/MW) apa.bbx 2017/01/06 v7.4 APA biblatex references style Hmm. That apa.bbx looks old. I think it's an old remnant in my local-texmf. I'll remove that and re-run with texmf-dist biblatex-apa.
    – krissen
    Apr 4, 2020 at 14:05
  • Mhhh, why is your biblatex-apa so old, but your biblatex is current? Note that your version of biblatex-apa produces the (now outdated) 6th-edition APA style. A current versions of biblatex-apa (v9.6) will produce 7th-edition APA style. If you want 6th-edition APA style on an up-to-date system, you would be using biblatex-apa6's style=apa6,.
    – moewe
    Apr 4, 2020 at 14:09

1 Answer 1

2

Essentially we 'just' need to take the original definition of the cite and textcite bibmacros and sprinkle some colour in there.

The original definitions can be found in apa.cbx (ll. 189-261 in v9.6).

In this example you can define which citations are going to be coloured with

\addtocategory{coloured}{<key_1,key_2,...,key_n>}

so this is useful if the entry is only coloured in the context of a certain document and not in and of itself.

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

Coloured citations.


If the entry is coloured by its very nature, it would be more natural to work with keyowrds

Simply replace the

\ifcategory{coloured}

above with

\ifkeyword{coloured}

and mark coloured entries up with

keywords = {coloured},

in the .bib file.

5
  • 🎉 Brilliant! Many thanks for taking the time. Much, much appreciated! I've been trying to use your code to implement colouring for \fullcite as well, but as far as I can tell it seems to follow a different kind of logic. I'll see if I can get it working myself and if not, perhaps I'll resort to a \colourfullcite, as there are only a handful instances of \fullcite in the document. Seeing all the correct references being coloured throughout the whole thesis was a joy! Thank you again.
    – krissen
    Apr 4, 2020 at 16:16
  • @krissen Ah yes, \fullcite is not implemented via a dedicated bibmacro, it is declared directly with \DeclareCiteCommand. Somehow the straightforward solution there seems to create an unwanted space, I'll have to investigate (but probably only tomorrow).
    – moewe
    Apr 4, 2020 at 16:27
  • 1
    @krissen I added the required code for a coloured \fullcite. You may see an undesirable space in sparsely populated entries (such as the ones in the MWE), see github.com/plk/biblatex-apa/issues/97 for details. Hopefully this is going to be fixed soon.
    – moewe
    Apr 5, 2020 at 7:50
  • Thank you! Works like a charm. In my actual document, the bib-entries are more populated so the undesirerable space is not an issue. At first, I only inserted the fullcite-code in my document, which led to odd output. Then, I inserted the full code, and ended up with no colour. Still, happy to have been able to figure out by myself, that in the current code you posted you used the keyword -approach, not category. Reverting relevant code from keyword to category and everything works as expected. Thank you again for taking the time! Much, much appreciated!
    – krissen
    Apr 6, 2020 at 8:59
  • 1
    @krissen Oh yeah, sorry about the copy-and-paste screw-up. Should hopefully be fixed now.
    – moewe
    Apr 6, 2020 at 10:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .