# Colouring citep/citet/citeauthor differently and based on environment

I'm writing my thesis report in which citations inserted through citep en citet are blue, and the output of the citeauthor-command is the regular text color. I've achieved this using a combination of these answers.

The problem is that this negatively interferes with these commands in a separate environment (sidebox, created using tcolorbox) I've defined, and overrules specific hypersetup-settings defined for that environment. As the name suggests, I'm using this environment as a sort of sidebar/box, in which I'd like all the text (including citations) to be another color.

## MWE

\documentclass[12pt]{report}

\usepackage{natbib}
\usepackage{filecontents}
\usepackage{xpatch}
\usepackage[most]{tcolorbox}

\xpretocmd{\citeauthor}{\hypersetup{citecolor=black}}{}{}
\xpretocmd{\citeyear}{\hypersetup{citecolor=black}}{}{}
\xpretocmd{\citep}{\hypersetup{citecolor=blue}}{}{}
\xpretocmd{\citet}{\hypersetup{citecolor=blue}}{}{}

\newcounter{sidebox}
\newenvironment{sidebox}[1]{
\def\sideboxtext{Box}
\hypersetup{citecolor=red}

\newtcolorbox[use counter=sidebox]{sideboxinner}{%
empty,title={#1},
minipage boxed title,
boxed title style={empty,size=minimal,toprule=0pt,top=4pt,left=3mm,overlay={}},
enlarge left by=8mm,
width=\textwidth-8mm,
before upper=\csname @totalleftmargin\endcsname0pt
}
\begin{sideboxinner}
}
{
\end{sideboxinner}}

\begin{filecontents}{main.bib}
@article{myref,
title = {Fancy title},
journal = {Journal of Fancy Publications},
author = {Jane Dee and John Doe},
year = {2017},
pages = {293--311},
}
\end{filecontents}

\begin{document}

The work by \citeauthor{myref} was a fancy read~\citep{myref}. \citet{myref} was published in the text-coloured year of \citeyear{myref}.

\begin{sidebox}{This is a sidebox}
The work by \citeauthor{myref} was a fancy read~\citep{myref}. \citet{myref} was published in the text-coloured year of \citeyear{myref}.
\end{sidebox}

\bibliographystyle{plainnat}
\bibliography{main}

\end{document}


This compiles to the following, in which the citations in the sidebox are blue and black.

Removing the xpretocmd-lines yields this, in which the sidebox is formatted as I want it, but citeauthor and citeyear in the regular text are blue -- rather than black.

## Attempts so far

Before coming here, I've made a few attempts to solve this on my own. As I reckon the xpretocmd-commands affect all citep-, citet-, citeauthor- and citeyear-commands, I've tried to redefine these conditionally, based on the environment (using \@currenvir, which is tcb@savebox for the sidebox, by the way). That doesn't work, presumably because these commands are executed in the preamble -- where the environment of use is still unknown.

I've also tried to redefine these xpretocmd-commands at the start of the sidebox-code (where I also change the hyperref-settinsg) and reset them at the end, without success.

## Question

My question is fairly straightforward: how can I achieve the desired situation?

Not change hypersetup, but define new color which can be changed:

\documentclass[12pt]{report}

\usepackage{natbib}
\usepackage{filecontents}
\usepackage{xpatch}
\usepackage[most]{tcolorbox}

\colorlet{citeauthor}{black}
\colorlet{citeyear}{black}
\colorlet{citep}{blue}
\colorlet{citet}{blue}

\xpretocmd{\citeauthor}{\hypersetup{citecolor=citeauthor}}{}{}
\xpretocmd{\citeyear}{\hypersetup{citecolor=citeyear}}{}{}
\xpretocmd{\citep}{\hypersetup{citecolor=citep}}{}{}
\xpretocmd{\citet}{\hypersetup{citecolor=citet}}{}{}

\newcounter{sidebox}
\newenvironment{sidebox}[1]{
\def\sideboxtext{Box}
\hypersetup{citecolor=red}

\newtcolorbox[use counter=sidebox]{sideboxinner}{%
empty,title={#1},
minipage boxed title,
boxed title style={empty,size=minimal,toprule=0pt,top=4pt,left=3mm,overlay={}},
enlarge left by=8mm,
width=\textwidth-8mm,
coltitle=red,coltext=red,fonttitle=,fontupper=\small,fontlower=,
before upper=\csname @totalleftmargin\endcsname0pt
}
\begin{sideboxinner}
\colorlet{citeauthor}{red}
\colorlet{citeyear}{red}
\colorlet{citep}{red}
\colorlet{citet}{red}
}
{
\end{sideboxinner}}

\begin{filecontents}{main.bib}
@article{myref,
title = {Fancy title},
journal = {Journal of Fancy Publications},
author = {Jane Dee and John Doe},
year = {2017},
pages = {293--311},
}
\end{filecontents}

\begin{document}

The work by \citeauthor{myref} was a fancy read~\citep{myref}. \citet{myref} was published in the text-coloured year of \citeyear{myref}.

\begin{sidebox}{This is a sidebox}
The work by \citeauthor{myref} was a fancy read~\citep{myref}. \citet{myref} was published in the text-coloured year of \citeyear{myref}.
\end{sidebox}

The work by \citeauthor{myref} was a fancy read~\citep{myref}. \citet{myref} was published in the text-coloured year of \citeyear{myref}.

\bibliographystyle{plainnat}
\bibliography{main}

\end{document}


Based on the answer by @coffeetable, I've found the solution below. This is slightly different (rather than defining the colors in the sideboxinner-part, I do so higher up), in order not to indent the first line of the sidebox-content. Adding %-signs and/or removing linebreaks (based on this thread) didn't solve that problem.

In any case, this removes the need for the additional hypersetup-command.

\documentclass[12pt]{report}

\usepackage{natbib}
\usepackage{filecontents}
\usepackage{xpatch}
\usepackage[most]{tcolorbox}

\colorlet{citeauthor}{black}
\colorlet{citeyear}{black}
\colorlet{citep}{blue}
\colorlet{citet}{blue}

\xpretocmd{\citeauthor}{\hypersetup{citecolor=citeauthor}}{}{}
\xpretocmd{\citeyear}{\hypersetup{citecolor=citeyear}}{}{}
\xpretocmd{\citep}{\hypersetup{citecolor=citep}}{}{}
\xpretocmd{\citet}{\hypersetup{citecolor=citet}}{}{}

\newcounter{sidebox}
\newenvironment{sidebox}[1]{
\def\sideboxtext{Box}
\colorlet{citeauthor}{red}
\colorlet{citeyear}{red}
\colorlet{citep}{red}
\colorlet{citet}{red}

\newtcolorbox[use counter=sidebox]{sideboxinner}{%
empty,title={#1},
minipage boxed title,
boxed title style={empty,size=minimal,toprule=0pt,top=4pt,left=3mm,overlay={}},
enlarge left by=8mm,
width=\textwidth-8mm,
coltitle=red,coltext=red,fonttitle=,fontupper=\small,fontlower=,
before upper=\csname @totalleftmargin\endcsname0pt
}
\begin{sideboxinner}
}
{
\end{sideboxinner}}

\begin{filecontents}{main.bib}
@article{myref,
title = {Fancy title},
journal = {Journal of Fancy Publications},
author = {Jane Dee and John Doe},
year = {2017},
pages = {293--311},
}
\end{filecontents}

\begin{document}

The work by \citeauthor{myref} was a fancy read~\citep{myref}. \citet{myref} was published in the text-coloured year of \citeyear{myref}.

\begin{sidebox}{This is a sidebox}
The work by \citeauthor{myref} was a fancy read~\citep{myref}. \citet{myref} was published in the text-coloured year of \citeyear{myref}.
\end{sidebox}

The work by \citeauthor{myref} was a fancy read~\citep{myref}. \citet{myref} was published in the text-coloured year of \citeyear{myref}.

\bibliographystyle{plainnat}
\bibliography{main}

\end{document}