5

I'm using the hyperref package to write an unusual document. It has clickable URL links in the section titles. Unfortunately, when clicking on the section title in the table of contents, one is not brought to that part of the document where the section starts but instead to the linked website.

Is it possible to turn off URL links within a group environment? Here's an MWE:

\documentclass{article}

\usepackage[%
  unicode,
  colorlinks=true,
  linkcolor=blue,
  urlcolor=blue,
  citecolor=blue]%
{hyperref}

\begin{document}

\begingroup
% Turn off URL links here
\tableofcontents
\endgroup

\section{\href{www.google.com}{Google}}

\end{document}
6

Even if the urls are turned off at TOC, they may appear in other places like the headers. Hence it is better to use the optional argument of \section

\documentclass{article}

\usepackage[%
  unicode,
  colorlinks=true,
  linkcolor=blue,
  urlcolor=blue,
  citecolor=blue]%
{hyperref}

\begin{document}
  \tableofcontents

  \section[Google]{\href{www.google.com}{Google}}

\end{document}
1
  • Don't have any headers in my document. Still, now that you mention it, using the optional toc entry seems like the cleanest solution.
    – Casimir
    Sep 2 '15 at 15:05
6

\href can be redefined locally:

\begingroup
  \renewcommand*{\href}[2]{#2}
  \tableofcontents
\endgroup
2
  • Works great. Thanks a lot. So all you're doing is making \href ignore the first argument passed to it, i.e. the url?
    – Casimir
    Sep 2 '15 at 14:58
  • @PacificOrion Yes. Because you want to have the second argument without the link, \href is redefined to just return the second argument. Sep 2 '15 at 16:03

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.