# Background color for citations in text

What I want is that the citation in text has a yellow background (\hly{\cite{A01}} ). The color of the may be black and what is also important is that the aforementioned effect still works when there is a line break.

MWE:

 \documentclass{article}
\usepackage{xcolor,ifthen,filecontents,soul}
\usepackage[authoryear,colon,square]{natbib}
\bibliographystyle{dcu}
\def\refcolor#1#2{\expandafter\xdef\csname#1color\endcsname{#2}}
\refcolor{A01}{green}
\def\setbibcolor#1{%
\expandafter\ifx\csname#1color\endcsname\relax%
\color{black}%
\else%
\color{\csname#1color\endcsname}%
\fi%
}
\makeatletter
\renewcommand\harvarditem[4][]{%
\if\relax#1\relax
\setbibcolor{#4}\bibitem[\setbibcolor{#4}#2(#3)]{#4}%
\else
\bibitem[#1(#3)#2]{#4}%
\fi
}%
\newboolean{highlighted} %Deklaration
\setboolean{highlighted}{true} %Zuweisung
\ifthenelse{\boolean{highlighted}}{
\DeclareRobustCommand{\hly}[1]{{\sethlcolor{yellow}\hl{#1}}}
}
{
\DeclareRobustCommand{\hly}[1]{#1}
}
\makeatother
\begin{filecontents}{mybib1.bib}
@misc{A01,
author = {Author, A.},
year = {2001},
title = {Alpha}
}
\end{filecontents}
\begin{document}
\hly{green} \citep{A01}   %\hly{\cite{A01}}
\bibliography{mybib1}
\end{document}


Paul

• This answer suggests a fix for single citations, but it does not yet seem to be working for multiple citations. It works for multiple if you use an \mbox but then there is no linebreak. – oliversm Feb 21 at 10:31
• But this is unfortunately what I have to solve :-( – Paul Feb 21 at 12:52

With lualatex and the new lua-ul package you can use highlighting around cite commands without problems:

\documentclass{article}
\usepackage{luacolor}
\usepackage{lua-ul}
\usepackage[authoryear,colon,square]{natbib}
\bibliographystyle{dcu}

\DeclareRobustCommand{\hly}[1]{\highLight{#1}}

\begin{filecontents}{mybib1.bib}
@misc{A01,
author = {Author, A.},
year = {2001},
title = {Alpha}
}
\end{filecontents}
\begin{document}
\hly{green} \citep{A01}   \hly{\cite{A01}}
\bibliography{mybib1}
\end{document}