Document Classes[hyperref] Change color of TOC-links only

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
-Robin-
Posts: 31
Joined: Tue May 20, 2008 3:07 pm

[hyperref] Change color of TOC-links only

Post by -Robin- »

I'm trying to change the color of the links, generated by the hyperref package, without affecting the colors of other links.
The only command I found changes all the links their color:

Code: Select all

     \usepackage{color}
     \usepackage[
	colorlinks=true,
	linkcolor=linkcolor
     ]{hyperref}
with

Code: Select all

\definecolor{linkcolor}{rgb}{0.149019,0,0.439215}
This changes the color of all normal internal links. There exists commands to change the color of anchor text, bibligraphical citations, URLs to local files, Acrobat menu items, link to other pages, linked URL's according to the documentation of the hyperref package.

Anybody has any suggestions how to tackle this problem ?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

[hyperref] Change color of TOC-links only

Post by phi »

you can make local changes to these settings using \hypersetup:

Code: Select all

\documentclass{article}

\usepackage{hyperref}

\hypersetup{colorlinks=true, linkcolor=red}


\begin{document}

\section{abc}
\label{def}

\ref{def}

\begingroup
\hypersetup{linkcolor=blue}
\ref{def}
\endgroup

\ref{def}

\end{document}
-Robin-
Posts: 31
Joined: Tue May 20, 2008 3:07 pm

[hyperref] Change color of TOC-links only

Post by -Robin- »

Thanks. That solved my problem. By surrounding the table of contents command only the links in the TOC are displayed in a different color :-)

Code: Select all

\begingroup
\hypersetup{linkcolor=black}
\tableofcontents
\endgroup
dfmalh
Posts: 19
Joined: Fri Jul 25, 2008 1:12 pm

Re: [hyperref] Change color of TOC-links only

Post by dfmalh »

Hallo, this worked for me as well. In addition, I was also able to add the List of Figures/Tables to my group and the color is all black.

My question is: Can I change the color of the link inside the text of a Figure.

E.g. Figure 4.2 ,the 4.2 is red in my case. I also want it black.

The reason it is red, if I want my backref page numbers more visible.

Thanks for any help.
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Re: [hyperref] Change color of TOC-links only

Post by frabjous »

You could use \hypersetup{linkcolor=black} right before the figure link, and then \hypersetup{linkcolor=red} right after it to set it back to red. (Probably it's easiest to define a command for this if you're going to do it often.)

Or someone might have a better suggestion.
dfmalh
Posts: 19
Joined: Fri Jul 25, 2008 1:12 pm

Re: [hyperref] Change color of TOC-links only

Post by dfmalh »

This is interesting, do you mean I should use it in my text?

E.g.

bla bla bla \hypersetup{linkcolor=black} \ref{fig:xxxxx} \hypersetup{linkcolor=red} bla bal bla

How would I create a rule that can do that for all my figures? I have about 80 in my thesis... so the rule would def be the way to go :-)

Thanks for the idea!
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

[hyperref] Change color of TOC-links only

Post by frabjous »

I had in mind a definition like:

Code: Select all

\newcommand{\figref}[1]%
{\hypersetup{linkcolor=black}%
\ref{#1}%
\hypersetup{linkcolor=red}}
And then you could just use it like:

bla bla bla \figref{fig:XXXX} bla bla bla

The most tedious thing, I suppose could be to change all your \ref tags for figures to \figref. If these are your only \ref tags, of course, it can easily be done with search and replace. Or if all and only your figure labels begin with fig: you could of course search and replace "\ref{fig:" to \figref{fig:"... or maybe something with a RegEx search/replace if your editor does that.

But it's possible someone else on these boards will have a better idea. (With expandafter or somesuch.)
dfmalh
Posts: 19
Joined: Fri Jul 25, 2008 1:12 pm

Re: [hyperref] Change color of TOC-links only

Post by dfmalh »

Great, I will try that, my other idea did not work...

Thanks for you help!
dfmalh
Posts: 19
Joined: Fri Jul 25, 2008 1:12 pm

[hyperref] Change color of TOC-links only

Post by dfmalh »

This works, but I had to change the first color (black) to red. Otherwise the number is in black. Also, the last color (red), I changed to black, other wise all other objects that were \ref changed to red.

So this works, and change the "whatever is \figcolref{xxxx} to red and then back to black until the next \figcolref{xxxx}.

Code: Select all

\newcommand{\figcolref}[1]%
{\hypersetup{linkcolor=red}%
\ref{#1}%
\hypersetup{linkcolor=black}}
For my final thesis I will change the red to gray (it does not matter really), but with this rule I can change the color in seconds to whatever I want! Excellent!!!

I will also setup a rule for my tables, sections, and chapter \ref's

Is it maybe possible to include all of these in one rule?

I use \ref{fig:xxxxx} or \ref{table:xxxxx} or \ref{chap:xxxxx} or \ref{sec:xxxxx}
dfmalh
Posts: 19
Joined: Fri Jul 25, 2008 1:12 pm

[hyperref] Change color of TOC-links only

Post by dfmalh »

This works, but I had to change the first color (black) to red. Otherwise the number is in black. Also, the last color (red), I changed to black, other wise all other objects that were \ref changed to red.

So this works, and change the "whatever is \figcolref{xxxx} to red and then back to black until the next \figcolref{xxxx}.

Code: Select all

\newcommand{\figcolref}[1]%
{\hypersetup{linkcolor=red}%
\ref{#1}%
\hypersetup{linkcolor=black}}
For my final thesis I will change the red to gray (it does not matter really), but with this rule I can change the color in seconds to whatever I want! Excellent!!!

I will also setup a rule for my tables, sections, and chapter \ref's

Is it maybe possible to include all of these in one rule?

I use \ref{fig:xxxxx} or \ref{table:xxxxx} or \ref{chap:xxxxx} or \ref{sec:xxxxx}
Post Reply