Change color of equation hyperrefs with each section

Similar to two previous posts of mine, I want the color of the reference to a formula to be the same color as the theorems etc in that section.

MWE

\documentclass[a4paper,twoside,ngerman]{article}
\usepackage{amsmath,amsfonts,cancel,mathtools,framed,mathabx,array,amssymb}
\usepackage{enumitem} % einfacher Nummerierung verÃ¤ndern
\usepackage{tikz} % zeichnen
\usetikzlibrary{cd} % kommutierende Diagramme
\usepackage{listofitems} % fÃ¼r Farben fÃ¼r Kapitel
\usepackage[tikz]{bclogo} % Kästen für Errinerung etc.

\usepackage{babel} % neue Rechtschreibung, Worttrennung
\usepackage[utf8]{inputenc} % Inputencoding
\usepackage[T1]{fontenc} % Umlaute

\definecolor{mycol1}{HTML}{a82337}
\definecolor{mycol2}{HTML}{016FB9}
\definecolor{mycol3}{HTML}{BF7F00}
\definecolor{mycol4}{HTML}{603C96}

% formatting
\usepackage[explicit]{titlesec}

\titleformat{\section}
{\normalfont\LARGE\bfseries\raggedright}
{\llap{\colorbox{mycol\number\value{section}}{\makebox[3em][r]    {\textcolor{white}{\number\value{section}}}}\hspace{1em}}}
{0pt}
{#1}

\begin{document}
\section{first, red section}

This is the first equation
\begin{align} \label{eq:firsteqn}
\ln(e^x) = x \
\forall x \in \R
\end{align}
And here is a reference to it: \eqref{eq:firsteqn}, which should be red.

\section{next, blue section}
This is an equation
\begin{align} \label{eq:secondeqn} \tag{$\star$)}
\forall a < b < c \ \exists ! x \in \mathbb{R}: a^x + b^x = c^x.
\end{align}
And here is a reference to it: \eqref{eq:secondeqn}, which should be blue.
\end{document}


If you're only referring to elements within the same section, you can switch the colour at the start of \section:

\documentclass{article}

\usepackage{amsmath,amsfonts,xcolor}

\definecolor{mycol1}{HTML}{a82337}
\definecolor{mycol2}{HTML}{016FB9}
\definecolor{mycol3}{HTML}{BF7F00}
\definecolor{mycol4}{HTML}{603C96}

% formatting
\usepackage[explicit]{titlesec}

\titleformat{\section}
{\normalfont\LARGE\bfseries\raggedright}
{\llap{\colorbox{mycol\number\value{section}}{\makebox[3em][r]{\textcolor{white}{\number\value{section}}}}\hspace{1em}}}
{0pt}
{#1}

\usepackage{xparse}
\let\oldsection\section
\newif\ifupdaterefcolour

\RenewDocumentCommand{\section}{ s o m }{%
\IfBooleanTF{#1}
{\oldsection*{#3}% \section*{..}
\def\updaterefcolour{}%
}
{\IfValueTF{#2}
{\oldsection[#2]{#3}}% \section[.]{..}
{\oldsection{#3}}% \section{..}
}%
\ifupdaterefcolour\updaterefcolour\fi
}

\begin{document}

\updaterefcolourfalse % Turn of reference colouring
\tableofcontents
\updaterefcolourtrue % Turn on reference colouring

\section{first, red section}

This is the first equation
$$\ln(e^x) = x \ \forall x \in \mathbb{R} \label{eq:firsteqn}$$
And here is a reference to it: \eqref{eq:firsteqn}, which should be red.

\section{next, blue section}
This is an equation
$$\forall a < b < c \ \exists ! x \in \mathbb{R}: a^x + b^x = c^x. \tag{\star} \label{eq:secondeqn}$$
And here is a reference to it: \eqref{eq:secondeqn}, which should be blue.

\end{document}

• This looks very complicated, what does it do exactly? Also, when I use a table of contents, the title appears twice and the toc is treated as section. Feb 4, 2019 at 19:46
• @ViktorGlombik: Well, you can manually add \hypersetup{linkcolor=mycol2} after section 2, and \hypersetup{linkcolor=mycol3} after section 3 and so forth. I just automated that process. That is, to insert \hypersetup{linkcolor=mycol\thesection} after \section. \thesection prints the number of the section, so mycol\thesection would be mycol3 in section 3. Complication is added because you can use \section* or \section[.]{..} or \section{..}, so I've had to capture that. By the way, \section* would incorrectly use a successive colour, but you don't have those I presume.
– Werner
Feb 4, 2019 at 19:51
• @ViktorGlombik: Riiight. Okay, so you don't want that to happen with the ToC.
– Werner
Feb 4, 2019 at 19:51
• I don't use \section* , that is correct. Feb 4, 2019 at 19:51
• @ViktorGlombik: I've updated the answer to accommodate for that. It now provides a conditional \updaterefcolourtrue to show references in colour and \updaterefcolourfalse to remove the colour reference. I've placed that around \tableofcontents (one could also automate that for the ToC, or other document elements). You can place it around your index and LoF/LoT as well. For every section numbered X, you would need a colour mycolX defined.
– Werner
Feb 4, 2019 at 20:02

If you use a named color it will change automatically after a \colorlet. So you only need to add a suitable colorlet at the begin of every section:

\documentclass[a4paper,twoside,ngerman]{article}
\usepackage[T1]{fontenc}
\usepackage{xcolor,amsmath}

\definecolor{mycol1}{HTML}{a82337}
\definecolor{mycol2}{HTML}{016FB9}
\definecolor{mycol3}{HTML}{BF7F00}
\definecolor{mycol4}{HTML}{603C96}
\colorlet{sectioncolor}{mycol1}

% formatting
\usepackage[explicit]{titlesec}

\titleformat{\section}
{\normalfont\LARGE\bfseries\raggedright}
{\llap{\colorbox{sectioncolor}{\makebox[3em][r]
{\textcolor{white}{\number\value{section}}}}\hspace{1em}}}
{0pt}
{#1}

\usepackage{xpatch}
\pretocmd\section{\colorlet{sectioncolor}{mycol\the\numexpr\value{section}+1}}

\begin{document}

\section{first, red section}

This is the first equation
\begin{align} \label{eq:firsteqn}
x=y
\end{align}
And here is a reference to it: \eqref{eq:firsteqn}, which should be red.

\section{next, blue section}
This is an equation
\begin{align} \label{eq:secondeqn}
x=y
\end{align}
And here is a reference to it: \eqref{eq:secondeqn}, which should be blue.
\end{document}