Search code examples
latexbeamer

hyperref package: how to color citations only


I would like to use the hyperref package to color citations only, but it seems that it colors every link.

For example here:

\documentclass{article}
    
\usepackage{hyperref}
\hypersetup{
    citecolor  = blue,
        colorlinks = true,
    }
    
\begin{document}
        
\tableofcontents
\section{This title should be in red in the TOC}
        
This citation \cite{a} is blue.
        
\begin{thebibliography}{99}
   \bibitem{a} Andros, Using hyperref for a citation, StackExchange, 2022.
\end{thebibliography}
        
\end{document} 

I would like the citation to be blue, but I don't want the items in the table of contents to be red.

Thanks


Solution

  • I would not activate colorlinks with beamer. This can break the carefully constructed colour themes in sometimes very surprising places.

    Instead you could change the text colour for the citation:

    \documentclass{beamer}
    
    \usetheme{metropolis}
    
    \usepackage{xpatch}
    \usepackage{xspace}
    
    \makeatletter
    \xpretocmd{\@citex}{\color{blue}}{}{}
    \xapptocmd{\@citex}{\color{normal text.fg}\xspace}{}{}
    \makeatother
    \begin{document}
            
    \begin{frame}
            
    This citation \cite{a} is blue.
            
    \begin{thebibliography}{99}
       \bibitem{a} Andros, Using hyperref for a citation, StackExchange, 2022.
    \end{thebibliography}
            
    \end{frame}        
    \end{document}