[Tex/LaTex] way to set colors of hyperref links and urls differently in headings / sections than in the body

colorformattinghyperreftitlesec

I know hypersetup from hyperref needs to be added before \begin{document} but after \usepackage{hyperref} and \usepackage{bookmark}.

For example, you can inject it like the following to ensure this:

\AtBeginDocument{\hypersetup{}}

Problem

I'd like to have links (particularly linkcolor) that appear in section headings inherit the color of the section heading (I am using titlesec to redefine all of my headings). This means I would need to redefine / override the original colors defined within \hypersetup{}.

Code

Note that I only kept bookmark in there because of its fastidious relationship with hyperref.

\documentclass{article}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{bookmark}
\hypersetup{
  colorlinks=true,
  linkcolor=red!80!black,
  urlcolor=green!50!black,
  hyperfootnotes=false,
  hypertexnames,
  bookmarks=true}% Causes clash if hyperref parameters loaded before bookmark, because bookmark loads hyperref without any parameters
\renewcommand{\sectionautorefname}{Section}

\usepackage[compact,explicit,noindentafter]{titlesec}
\titleformat{\section}[hang]{\bookmarksetupnext{bold,color=blue!50!black,open=false}\color{blue!50!black}\large\bfseries}{\thesection}{1em}{\hyphenchar\font=-1 #1}[\hyphenchar\font=\defaulthyphenchar] % \hyphenchar\font=-1 disables hyphenation, which I reset back to 1 after typesetting the section. see https://tex.stackexchange.com/a/44362/13552


\begin{document}
\section{Always remember that you are absolutely unique. Just like everyone else.}
\label{sec:quote}
Margaret Mead

\section{Should not be colored: \nameref{sec:quote}}
This link should be colored: \nameref{sec:quote}. And this one: Section~\ref{sec:quote}. And this one: \autoref{sec:quote}. And this one: \pageref{sec:quote}.
\end{document}

Ouput

enter image description here

Best Answer

You can call \hypersetup{hidelinks} in the "before" part of \titleformat, as this will only affect the section title locally:

\documentclass{article}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{bookmark}
\hypersetup{
  colorlinks=true,
  linkcolor=red!80!black,
  urlcolor=green!50!black,
  hyperfootnotes=false,
  hypertexnames,
  bookmarks=true}% Causes clash if hyperref parameters loaded before bookmark, because bookmark loads hyperref without any parameters
\renewcommand{\sectionautorefname}{Section}

\usepackage[compact,explicit,noindentafter]{titlesec}
\titleformat{\section}[hang]{\bookmarksetupnext{bold,color=blue!50!black,open=false}\hypersetup{hidelinks}\color{blue!50!black}\large\bfseries}{\thesection}{1em}{\hyphenchar\font=-1         #1}[\hyphenchar\font=1]

\begin{document}
\section{Always remember that you are absolutely unique. Just like everyone else.}
\label{sec:quote}
Margaret Mead

\section{Should not be colored: \nameref{sec:quote}}
This link should be colored: \nameref{sec:quote}. And this one: Section~\ref{sec:quote}. And this one: \autoref{sec:quote}. And this one: \pageref{sec:quote}.
\end{document}

Output

Edit: Regarding Henri Menke's comment about \hyphenchar\font=-1 I will let that be up to the original poster, as he/she seems to have some reason to be using this in order to turn off hyphenation.

Related Question