17

In hyperref package, is it possible to specify a specific color for equation links different than other link colors ?

Following are present color specifications that I am using for hyperref package

colorlinks=true,       % false: boxed links; true: colored links
linkcolor=red,          % color of internal links
citecolor=blue,        % color of links to bibliography
filecolor=magenta,      % color of file links
urlcolor=cyan   

(Is it possible to do it with \cref ?)

Thanks.

2
  • Are you using amsmath or any other cross-referencing package?
    – Werner
    Sep 13, 2013 at 6:22
  • @Werner Yes I am using amsmath as one of the packages.
    – cosmicraga
    Sep 13, 2013 at 6:39

1 Answer 1

19

You can define your own link command for equations and change the (border) color for links locally.

LaTeX with \ref

\documentclass[a5paper]{article}

\usepackage{xcolor}
\colorlet{linkequation}{blue}
\usepackage[colorlinks]{hyperref}

\newcommand*{\refeq}[1]{%
  \begingroup
    \hypersetup{
      linkcolor=linkequation,
      linkbordercolor=linkequation,
    }%
    \ref{#1}%
  \endgroup
}

\begin{document}
\tableofcontents
\section{Example}\label{sec:example}
\begin{equation}
\label{eq:einstein}
E=mc^2
\end{equation}
See section \ref{sec:example} and equation \refeq{eq:einstein}.
\end{document}

Result \refeq

Package amsmath and \eqref

And an example for the redefinition of \eqref from package amsmath:

\documentclass[a5paper]{article}

\usepackage{amsmath}

\usepackage{xcolor}
\colorlet{linkequation}{blue}
\usepackage[colorlinks]{hyperref}

\newcommand*{\SavedEqref}{}
\let\SavedEqref\eqref
\renewcommand*{\eqref}[1]{%
  \begingroup
    \hypersetup{
      linkcolor=linkequation,
      linkbordercolor=linkequation,
    }%
    \SavedEqref{#1}%
  \endgroup
}

\begin{document}
\tableofcontents
\section{Example}\label{sec:example}
\begin{equation}
\label{eq:einstein}
E=mc^2
\end{equation}
See section \ref{sec:example} and equation \eqref{eq:einstein}.
\end{document}

Result \eqref

Package cleveref and \cref

\documentclass[a5paper]{article}

\usepackage{xcolor}
\colorlet{linkequation}{blue}
\usepackage[colorlinks]{hyperref}
\usepackage{cleveref}

\makeatletter
\creflabelformat{equation}{%
  \textup{%
    \hypersetup{
      linkcolor=linkequation,
      linkbordercolor=linkequation,
    }%
    (#2#1#3)%
  }%
}
\makeatother

\begin{document}
\tableofcontents
\section{Example}\label{sec:example}
\begin{equation}
\label{eq:einstein}
E=mc^2
\end{equation}
See section \ref{sec:example} and \cref{eq:einstein}.
\end{document}

Result \cref

10
  • Thanks a lot. At present I am using cleveref package to refer to equations and all others (for example, tables, figures, sections). Is it possible to do it with \cref ?
    – cosmicraga
    Sep 13, 2013 at 6:58
  • please get rid of the redundant "eq." in the \cref example. in fact, with the parentheses, the "equation" isn't needed either, but isn't as annoying. Sep 13, 2013 at 16:53
  • @barbarabeeton: Thanks, redundant "equation" removed. Sep 13, 2013 at 20:02
  • 1
    @user19832 See hyperref's option linkcolor. Jul 15, 2016 at 16:46
  • 1
    @Wallflower The argument of macro \caption usually moves to the .aux and .lof file. Fragile stuff can break and need protection. E.g. \protect\hypersetup or define a protected macro using \DeclareRobustCommand. May 25, 2023 at 22:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .