2

I have a document where I want to insert a clickable link to a web page, which can be achieved with the below code. The problem is the default behaviour of hyperref also makes things like my table of contents, references, citations, etc, into links as well. I only want this one url to be a link, literally nothing else. How do I turn off all these additional features?

Alternatively, is there another package I could use which will allow me to make links, but not give me all this other behaviour?

\documentclass{book}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\chapter{Test chapter}
text text text
\section{A title}
\url{http://www.google.com}
\section{Another title}
text text text
\end{document}
  • 2
    @TeXnician Actually you can: You should load hyperref with the option implicit=false, which does not redefine the LaTeX internals. – campa Sep 25 '18 at 9:20
  • @campa Oh right, I forgot about this option, because this does not always work on its own (e.g. for citations with biblatex which would need special treatment). – TeXnician Sep 25 '18 at 9:35
  • That option seems to work ok in my MWE, but my main document won't compile with hyperref loaded, regardless of whether it has that option or not. Not sure what's going on there. @TeXnician's suggestion of some postscript code could be a viable alternative. Could anyone talk me through how to get it working? – Ulysses Sep 25 '18 at 9:38
4

A very simple example for pdfLaTeX (border is disabled, just comment out the first line to enable it; see also this post):

\documentclass{article}

\def\myurl#1{%
\leavevmode\pdfstartlink
      attr{/Border [0 0 0]  }%
      user {%
        /Subtype /Link
        /A << /S /URI /URI (#1) >>%
      }\texttt{#1}\pdfendlink}

\begin{document}
    Test
    \myurl{www.example.com}
    Test
\end{document}
  • This seems to work in this very simple case, but if I try to add it to a slightly more complex document, such as my MWE in the original question, I get an error saying pdfstartlink cannot be used in vertical mode. I managed an ad hoc solution by putting it in an mbox, but I don't know if this would create some undesirable behaviour such that it wouldn't work as a general solution, so it may need to be tweaked further. – Ulysses Sep 26 '18 at 10:50
  • My final code that I can use directly in the text is: \mbox{\pdfstartlink attr{/Border[0 0 0]} user{/Subtype /Link /A << /S /URI /URI (www.example.com) >>}{\color{blue}\textbf{\underline{Link Name}}}\pdfendlink} Note that I also call the color package in the preamble. – Ulysses Sep 26 '18 at 10:52
  • @Ulysses I added a fix. – TeXnician Sep 26 '18 at 10:56

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.