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.

  • 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 0: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 5:23
  • 1
    Do you want the brackets to also be hyperlinked? – moewe Apr 25 '14 at 8:07
  • @pst with the \bibpunct command – Mario Apr 25 '14 at 17:25
  • 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
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.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.