2

Question

How can I use hypersetup{hidelinks} globally, such that the scope is the TOC, LOF, and LOT only?

Situation

I have many documents that share a common preamble. I am using the \hyperref package for linkage and tocloft for customization of the ToC, LoF, and LoT. I would like the links in the body to be visible (color change). I would like the ToC, LoF, and LoT to have links, yet I do not want any changes to the formatting (no color change).

\usepackage[%
    bookmarks=true,
    bookmarksnumbered=true,
    colorlinks=true,
    linkcolor=blue,
    urlcolor=red,
    hyperfootnotes=false]{hyperref}

Attempt

My attempt makes global changes, because I do not how to insert the \endgroup/\egroup at the end of the ToC.

\addtocontents{toc}{\bgroup\protect\hypersetup{hidelinks}}
5
  • 1
    Just add \let\oldtableofcontents\tableofcontents \renewcommand{\tableofcontents}{\oldtableofcontents\addtocontents{toc}{\egroup}}.
    – Werner
    Commented Feb 9, 2015 at 6:51
  • That does not seem to work :/ Maybe I implemented it wrong. Commented Feb 9, 2015 at 6:56
  • Doesn't my answer work for you, or I've misunderstood your question? Commented Feb 9, 2015 at 10:40
  • It does indeed! Thanks. I'd still like to know how to implement Werner's comment for the sake of learning. Commented Feb 9, 2015 at 13:48
  • 1
    @macmadness86 Werner's code works only if you put it inside the document and if \tableofcontents is issued at the end of the document. Commented Feb 9, 2015 at 17:20

1 Answer 1

3

You don't need a \bgroup in your definition since the change is local to the ToC.

You should issue the following three commands to hide the colors in ToC, LoT and LoF:

\addtocontents{toc}{\protect\hypersetup{hidelinks}}   
\addtocontents{lot}{\protect\hypersetup{hidelinks}}
\addtocontents{lof}{\protect\hypersetup{hidelinks}}

MWE:

\documentclass{article}
\usepackage{tocloft}

\usepackage[%
    bookmarks=true,
    bookmarksnumbered=true,
    colorlinks=true,
    linkcolor=blue,
    urlcolor=red,
    hyperfootnotes=false]{hyperref}

\begin{document}
\addtocontents{toc}{\protect\hypersetup{hidelinks}}
\tableofcontents
\addtocontents{lot}{\protect\hypersetup{hidelinks}}
\listoftables
\addtocontents{lof}{\protect\hypersetup{hidelinks}}
\listoffigures
\section{A section}\label{sec}
\begin{table}[h]
  \centering
  \caption{A table}\label{table}
\end{table}
\begin{figure}[h]
  \centering
  \caption{A figure}\label{figure}
\end{figure}

\subsection{First}
\subsection{Second}

\section{Another section}
A reference to section \ref{sec} and to table \ref{table} and to figure \ref{figure}.
\subsection{First}
\subsection{Second}

\end{document} 

Output:

enter image description here

You must log in to answer this question.

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