[Tex/LaTex] How to turn on/off certain types (e.g. citation, url, file) of hyperref links

hyperreflinks

The hyperref manual doesn't seem to document a way of turning different types of links (citation, url, cross-reference, file, ToC, footnote) on/off independently.

One possible scenario would be to enable all links created with \href (url links) but disable all other types of links. Another scenario would be to enable all citations links but disable all other types of links.

How can that be done?

One related question is Selectively turn off hyperref links, but it only addresses how to turn cross-reference links off.

Best Answer

Package hyperref supports only a subset of possible options to turn off "hyperfeatures":

  • bookmarks=false: bookmarks/outlines
  • pdfpagelabels=false: PDF page labels
  • hyperfootnotes=false: footnotes
  • hyperindex=false: index
  • pageanchor=false: page anchors
  • ...

Also hyperref provides environment NoHyper.

URL links only

A modified version of NoHyper could be used to disable all, but URI links.

For example:

\documentclass[12pt]{article}
\usepackage[a5paper,landscape]{geometry}% smaller test image for TeX.sx
\usepackage[
  bookmarks=false,
  pdfpagelabels=false,
  hyperfootnotes=false,
  hyperindex=false,
  pageanchor=false,
  colorlinks,
]{hyperref}

\makeatletter
\let\saved@hyper@linkurl\hyper@linkurl
%\let\saved@hyper@linkfile\hyper@linkfile
\let\saved@hyper@link@\hyper@link@
\AtBeginDocument{%
  % Since the whole document is affected, only the \begin part of
  % environment `NoHyper' is needed.
  \NoHyper
  \let\hyper@linkurl\saved@hyper@linkurl % needed by \url
  %\let\hyper@linkfile\saved@hyper@linkfile % needed by \href{<file>}
  \let\hyper@link@\saved@hyper@link@ % needed by \href{<url>}
}
\makeatother

\begin{document}
\tableofcontents
\section{Hello World}
\label{sec:hello}
This is section \ref{sec:hello} on page \pageref{sec:hello}.%
\footnote{This is a footnote}\\
\url{http://tex.stackexchange.com/}\\
\href{http://tex.stackexchange.com/}{\TeX\ Stack Exchange}\\
\IfFileExists{t.pdf}{\href{t.pdf}{Local PDF file}}{(No local file.)}\\
\cite{abc} is a good book.
\begin{thebibliography}{9}
\bibitem{abc} A good book.
\end{thebibliography}
\end{document}

Result

Citation links only

The following example tries to enable citation links (not tested with natbib). Also anchors need to be enabled to get link targets for the citations.

\documentclass[12pt]{article}
\usepackage[a5paper,landscape]{geometry}% smaller test image for TeX.sx
\usepackage[
  bookmarks=false,
  pdfpagelabels=false,
  hyperfootnotes=false,
  hyperindex=false,
  pageanchor=false,
  colorlinks,
]{hyperref}

\makeatletter
\let\saved@hyper@link@\hyper@link@
\let\saved@hyper@link\hyper@link
% anchors are needed for the citations
\let\saved@hyper@anchorstart\hyper@anchorstart
\let\saved@hyper@anchorend\hyper@anchorend
\def\textstring@cite{cite}
\def\new@hyper@link@[#1]{%
  \def\@temp{#1}%
  \ifx\@temp\textstring@cite
    \expandafter\saved@hyper@link@
  \else
    \expandafter\disabled@hyper@link@
  \fi
  [{#1}]%
}
\def\new@hyper@link#1{%
  \def\@temp{#1}%
  \ifx\@temp\textstring@cite
    \expandafter\saved@hyper@link
  \else
    \expandafter\disabled@hyper@link
  \fi
  {#1}%
}
\AtBeginDocument{%
  \NoHyper
  \let\disabled@hyper@link@\hyper@link@
  \let\disabled@hyper@link\hyper@link  
  \let\hyper@link@\new@hyper@link@     
  \let\hyper@link\new@hyper@link       
  \let\hyper@anchorstart\saved@hyper@anchorstart
  \let\hyper@anchorend\saved@hyper@anchorend
}
\makeatother

\begin{document}
\tableofcontents
\section{Hello World}
\label{sec:hello}
This is section \ref{sec:hello} on page \pageref{sec:hello}.%
\footnote{This is a footnote}\\
\url{http://tex.stackexchange.com/}\\
\href{http://tex.stackexchange.com/}{\TeX\ Stack Exchange}\\
\IfFileExists{t.pdf}{\href{t.pdf}{Local PDF file}}{(No local file.)}\\
\cite{abc} is a good book.
\begin{thebibliography}{9}
\bibitem{abc} A good book.
\end{thebibliography}
\end{document}

Result