1

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}


\usepackage[bookmarks=true,colorlinks=true,linkcolor=black]{hyperref}

\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}
2

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

enter image description here

\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[bookmarks=true,colorlinks=true,linkcolor=black]{hyperref}

\usepackage{xparse}
\let\oldsection\section
\newcommand{\updaterefcolour}{\hypersetup{linkcolor=mycol\thesection}}
\newif\ifupdaterefcolour

\RenewDocumentCommand{\section}{ s o m }{%
  \IfBooleanTF{#1}
    {\oldsection*{#3}% \section*{..}
     \def\updaterefcolour{}%
    }
    {\IfValueTF{#2}
      {\oldsection[#2]{#3}}% \section[.]{..}
      {\oldsection{#3}}% \section{..}
      \def\updaterefcolour{\hypersetup{linkcolor=mycol\thesection}}%
    }%
  \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
\begin{equation}
  \ln(e^x) = x \ \forall x \in \mathbb{R} \label{eq:firsteqn}
\end{equation}
And here is a reference to it: \eqref{eq:firsteqn}, which should be red.

\section{next, blue section}
This is an equation
\begin{equation}  
  \forall a < b < c \ \exists ! x \in \mathbb{R}: a^x + b^x = c^x. \tag{$\star$} \label{eq:secondeqn}
\end{equation}
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. – Viktor Glombik Feb 4 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 at 19:51
  • @ViktorGlombik: Riiight. Okay, so you don't want that to happen with the ToC. – Werner Feb 4 at 19:51
  • I don't use \section* , that is correct. – Viktor Glombik Feb 4 at 19:51
  • 1
    @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 at 20:02
1

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[colorlinks=true,linkcolor=sectioncolor]{hyperref}
\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.