0

Following this thread I have tried to change the color of citation for a document using natbib and biblatex

\documentclass{article}
\usepackage{xcolor}
\usepackage{hyperref} 
\hypersetup{colorlinks=true, 
            citecolor=red}
\usepackage[natbib=true]{biblatex} 
\addbibresource{test.bib}
\newcommand\colcitep[2]{\hypersetup{citecolor=#1}\citep{‌​#2}}
\begin{document}
The first reference should be in red \citep{dupont_2007}\par
The second reference should be in blue \colcitep{blue}{dupont_2007}\par
\hypersetup{citecolor=blue}
The third reference is OK in blue \citep{dupont_2007}\par
\printbibliography
\end{document}

test.bib

@article{dupont_2007,
  title = {test title},
  author = {Dupont},
  date = {2007},
  journaltitle = {dummy title},
  shortjournal = {Short. dummy.},
  volume = {64},
  number = {7},
  pages = {1402--1413},
 }

This is run using xelatex, biber and xelatex.

latex output

Somehow the programs inserts some space before the reference

Package biblatex Warning: The following entry could not be found
(biblatex)                in the database:
(biblatex)                ‌​dupont_2007
(biblatex)                Please verify the spelling and rerun
(biblatex)                LaTeX afterwards.

This thread seems to indicate that newcommand might insert a space, but using xspace doesn't solve the issue. There seems to be some characters inserted by newcommand. What proper syntax should I use to avoid this ?

Cedric
  • 139

1 Answers1

3

You have stray invisible characters before #2 in the \newcommand line. If I paste the definition body in a converter, I see

\hypersetup{citecolor=#1}\citep{U+200CU+200B#2}

and the warning you get should raise a suspect.

Remove the two wrong characters; better is to retype the line.

Next, add \begingroup and \endgroup in order to localize the color change.

\begin{filecontents*}{\jobname.bib}
@article{dupont_2007,
  title = {test title},
  author = {Dupont},
  date = {2007},
  journaltitle = {dummy title},
  shortjournal = {Short. dummy.},
  volume = {64},
  number = {7},
  pages = {1402--1413},
 }
\end{filecontents*}

\documentclass{article} \usepackage{xcolor} \usepackage[natbib=true]{biblatex} \usepackage{hyperref} \hypersetup{ colorlinks=true, citecolor=red }

\addbibresource{\jobname.bib}

\newcommand\colcitep[2]{\begingroup\hypersetup{citecolor=#1}\citep{#2}\endgroup}

\begin{document}

The first reference should be in red \citep{dupont_2007}

The second reference should be in blue \colcitep{green}{dupont_2007}

The third reference is OK in blue \citep{dupont_2007}

\printbibliography

\end{document}

enter image description here

I used the filecontents* environment just to make the example self-contained.

egreg
  • 1,121,712
  • This is an awsome answer and I thank you very much, I was stuck. In addition I searched for something like filecontent* and I'm really happy to know it. So when stuck with strange character I need to rewrite from the keyboard. – Cedric Mar 04 '22 at 17:04