1

I have refined \citep so that the colour of the hyperlink turns "Teal":

\let\oldcitep=\citep 
\def\citep#1{\hypersetup{citecolor=Teal}\oldcitep{#1}} %changes colour of citep link to Teal.

NB: I have done it this way as I want \citep to be a different colour from \citet.

Overall, this system works great:

\citep{Author2000}

becomes:

(Author, 2000)

coloured in Teal as expected.


However if I try and use a prefix, it doesn't work:

\citep[see:][]{Author2000}

becomes

(?)see:][]Author2000

and all the text remains black.

Why does this happen? Can anyone help me fix this problem?

Cheers.

  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – Stefan Pinnow May 28 '19 at 16:21

2 Answers2

2

You can use the #{ argument to grab everything until the first {, and then pass that to \oldcitep. You also need an extra set of braces to contain the color change to that citation only:

\let\oldcitep=\citep
\def\citep#1#{\INNERcitep{#1}}
\def\INNERcitep#1#2{{\hypersetup{citecolor=Teal}\oldcitep#1{#2}}}

enter image description here

MWE:

\documentclass{article}
\usepackage{natbib}
\usepackage[svgnames]{xcolor}
\usepackage[colorlinks]{hyperref}

\let\oldcitep=\citep
\def\citep#1#{\INNERcitep{#1}}
\def\INNERcitep#1#2{{\hypersetup{citecolor=Teal}\oldcitep#1{#2}}}

\begin{document}

\citet{article-full}

\citep{article-full}

\citep[][]{article-full}

\citep[see:][]{article-full}

\citet{article-full}

\bibliographystyle{apalike}
\bibliography{xampl}

\end{document}
1

If you want to simplify the redefinition syntax you can also use the xpatch package, which takes care of the lower level implementation details. The package defines (among other things) a command \xpretocmd, which has four arguments: the command you want to change (here: \citep), the code you want to put in front (here: \hypersetup{citecolor=Teal}, code that needs to be executed when the redefinition is successful (here: nothing) and code that needs to be executed when the redefinition fails (here: also nothing).

Note that, in contrast to the other answer, the color change is global so it also affects \citet. A simple solution to this issue is to also redefine \citet.

MWE:

\documentclass{article}
\usepackage{natbib}
\usepackage[svgnames]{xcolor}
\usepackage[colorlinks]{hyperref}
\usepackage{xpatch}
\xpretocmd{\citep}{\hypersetup{citecolor=Teal}}{}{}
\xpretocmd{\citet}{\hypersetup{citecolor=Magenta}}{}{}
\begin{document}
Some text \citet{regular} and in parentheses \citep{regular}

and with prefix, \citep[see:][]{regular}
\bibliographystyle{apalike}
\bibliography{sample}
\end{document}

Result: enter image description here

Marijn
  • 37,699