3

How can I change the square brackets color of references in text using biblatex (numeric style)?

I know it can be done with natbib, but not using this package anymore.

\documentclass[hidelinks,spanish]{book}
\usepackage[usenames,dvipsnames]{color}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\definecolor{CeruleanRef}{RGB}{12,127,172}
\usepackage[colorlinks=true,linkcolor=black,citecolor=CeruleanRef,urlcolor=black]{hyperref}
\usepackage[numbers,sort&compress]{natbib}
\bibpunct{\color{CeruleanRef}[}{\color{CeruleanRef}]}{,}{n}{}{;}

\bibliographystyle{unsrtnat}

\begin{document}

\frontmatter

\mainmatter

A reference: \cite{RBoehler1996}

\appendix

\backmatter

\bibliography{articles,reports,books,reviews}

\end{document}

With natbib I get this: enter image description here

I want the same with biblatex and biber.

Mario
  • 645
  • 3
    You mean the square brackets around the entries in the bibliography? Or those in the text? Or both? In answering this question, please provide us with a minimal working example (MWE) that we can use. Helping us really does help you. – Werner Apr 25 '14 at 00:38
  • 1
    Quickly searching through the natbib documentation, I didn't find "color" there, so I don't know what kind of functionality that is.

    (Something related and common is to color the reference with hyperref since it's a link, with \usepackage[colorlinks]{hyperref}, but in "[1]" that will color only "1", and not the brackets.)

    – pst Apr 25 '14 at 05:23
  • 1
    Do you want the brackets to also be hyperlinked? – moewe Apr 25 '14 at 08:07
  • @pst with the \bibpunct command – Mario Apr 25 '14 at 17:25
  • Please add a MWE of the fix you used before (and maybe of what you get with natbib). – moewe Apr 25 '14 at 18:16
  • 2
    If you used to use \bibpunct and have no intention to have hyperlinking, you can go with \makeatletter \renewcommand*{\bibleftbracket}{\blx@postpunct\textcolor{red}{[}} \renewcommand*{\bibrightbracket}{\blx@postpunct\textcolor{red}{]}\midsentence} \makeatother. (Probably this will colour more more things red, if you do not want that we will need a more sophisticated answer.) – moewe Apr 25 '14 at 19:06
  • OK, so what happens here, is that natbib links the citation number (that's why it is turned turquoise-ish) and you colour the brackets to pretend they are linked as well, although they are not. You can get the "pretend look" by using the code in my comment above. But note that this really is not a thing you should be doing. hyperref colours links so we know they're there and you make your reader think there is a link even though there is none. – moewe Apr 28 '14 at 18:03
  • @moewe Excellent! – Mario Apr 28 '14 at 20:26
  • @Werner, I'd like to do your 1st option, I mean, change [XX] in the bibliography. Is it possible with biber? I know editing bbl but not too robust since biber changes it. – Sigur Mar 16 '19 at 00:07
  • @Sigur: Consider this example which delays \formatentry's definition to the first call to \printbibliography. At that time, you can change the colour - example output. – Werner Mar 16 '19 at 04:48
  • @Werner, thanks. Nice! Also, moewe told me about \DeclareFieldFormat{labelalphawidth}{\textcolor{red}{\bibleftbracket#1\bibrightbracket}}. My problem now is to change the color of some items, not all. For example, only items from specific .bib. – Sigur Mar 16 '19 at 12:04
  • @Sigur: Ask the question here on the site... with an appropriate minimal example, if course. – Werner Mar 16 '19 at 15:18
  • @Werner, it was solved with discussion here. Thanks. – Sigur Mar 16 '19 at 16:28

1 Answers1

5

What you see there is that hyperref colours (cite) links (because you told it so in colorlinks=true, citecolor=CeruleanRef). Normally, when you cite only a certain part of that citation is actually turned into a link, with the numeric style only the number itself, not the brackets are linked. Your natbib fix did not extend the link to the brackets, it just coloured them in.

We can emulate that behaviour by adding \color{CeruleanRef} to the wrapper of the \cite command (I took it from numeric.cbx, if you use numeric-comp.cbx or another style, copy the definition of \cite from there)

\DeclareCiteCommand{\cite}[\color{CeruleanRef}\mkbibbrackets]% <--- this is new
  {\usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{cite}}
  {\multicitedelim}
  {\usebibmacro{postnote}}

for numeric-comp

\DeclareCiteCommand{\cite}[\color{CeruleanRef}\mkbibbrackets]
  {\usebibmacro{cite:init}%
   \usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{cite:comp}}
  {}
  {\usebibmacro{cite:dump}%
   \usebibmacro{postnote}}

Remember: This will not actually create a hyperlink, the text will just look like one. Hyperlinks with ranges that big can only be created in very special cases with a huge amount of work. (With numeric-comp, linking the brackets would not even make sense: Where is the link from the bracket supposed to link to.)

MWE

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[backend=biber, style=numeric-comp]{biblatex}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\definecolor{CeruleanRef}{RGB}{12,127,172}
\usepackage[colorlinks=true, citecolor=CeruleanRef]{hyperref}
\addbibresource{biblatex-examples.bib}

\DeclareCiteCommand{\cite}[\color{CeruleanRef}\mkbibbrackets]
  {\usebibmacro{cite:init}%
   \usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{cite:comp}}
  {}
  {\usebibmacro{cite:dump}%
   \usebibmacro{postnote}}

\begin{document}
  \cite{companion,knuth:ct:b,knuth:ct:c} and \cite{baez/article} \cite{aksin}
  \printbibliography
\end{document}

enter image description here


One could also use a kludge as suggested in my comment

\makeatletter
\renewcommand*{\bibleftbracket}{\blx@postpunct\textcolor{red}{[}}
\renewcommand*{\bibrightbracket}{\blx@postpunct\textcolor{red}{]}\midsentence}
\makeatother

This does have side-effects though: All opening brackets will be coloured red.

moewe
  • 175,683
  • 1
    the idea of having colored brackets is not just for linking, but for reading a printed document (aesthetic reasons). – Mario Apr 29 '14 at 14:09