3

Using the package hyperref, I can use \url to produce clickable URLs. But in the viewers I have their boundaries are very badly set. Example:enter image description here

Hence, I prefer to use \href{}{}, which works most of the time. But in the examples in this picture it crashes. This is a near minimal crashing example:

\documentclass{article}

\usepackage{hyperref}

\begin{document}

 %\url{https://en.m.wikipedia.org/wiki/Spirit_way}
\href{https://en.m.wikipedia.org/wiki/Spirit_way}{https://en.m.wikipedia.org/wiki/Spirit_way}

\end{document}

Here's the log: enter image description here

What should I change?

Mico
  • 506,678
Ludi
  • 283

1 Answers1

3

You're mis-using the \href macro: The second argument should be a human-readable string, not some URL which may contain characters -- such as _ ("underscore") -- that are special to TeX.

If you insist on showing the full URL, just use the \url macro, not the \href macro.

enter image description here

\documentclass{article}
\usepackage[hyphens,spaces,obeyspaces]{url}
\usepackage[colorlinks,allcolors=blue]{hyperref}

\begin{document}
\url{https://en.m.wikipedia.org/wiki/Spirit_way}

\href{https://en.m.wikipedia.org/wiki/Spirit_way}{Wikipedia page: Spirit Way}
\end{document}
Mico
  • 506,678
  • If you (a) insist on showing a full URL string that contains TeX-special characters and (b) for some reason don't want to use \url{...} directly, you could type (though I wouldn't recommend it!) \href{https://en.m.wikipedia.org/wiki/Spirit_way}{\url{https://en.m.wikipedia.org/wiki/Spirit_way}}. – Mico Jul 31 '17 at 20:51
  • Ok, but as stated, the boundaries \url produces are messed up - in any of my viewers. – Ludi Jul 31 '17 at 20:52
  • @Ludi - Two questions: (1) Are you loading the url package with the options hyphens, spaces, and obeyspaces? (2) Why do you insist on showing the full URL? The whole point of using \href is not to have to show the underlying URL in the first place. – Mico Jul 31 '17 at 20:54
  • @Ludi - Incidentally, by "boundaries", do you mean the margins of the text block? Please clarify. – Mico Jul 31 '17 at 20:55
  • No. how to do it? 2) I don't know whether my professor will immediately realise the URLs are clickable. If he thinks I am withholding the URLs he is bound to be angry.
  • – Ludi Jul 31 '17 at 20:56
  • I mean the blue boundaries shown in the pic produced by both my viewers. When using \url they interfere with the text of the URL. That is the only reason I chose \href. Those same boundaries are correct when using \href. – Ludi Jul 31 '17 at 20:59
  • 2
    \nolinkurl instead of \url inside the second argument of \href avoids a nested link. – Heiko Oberdiek Jul 31 '17 at 21:02
  • @Ludi - Regarding (1): just run \usepackage[hyphens,spaces,obeyspaces]{url} -- as is done in the code I provided. Regarding (2): You are not "withholding URLs": If the mouse pointer hovers over the link produced by \href, the URL will be shown -- at least that's the case with Acrobat. – Mico Jul 31 '17 at 21:04
  • @Ludi - Is anything preventing you from specifying the option colorlinks when loading the hyperref package? – Mico Jul 31 '17 at 21:05
  • @HeikoOberdiek - Thanks! A really good [!!] suggestion! – Mico Jul 31 '17 at 21:19
  • @Mico colorlinks works like a charm! – Ludi Jul 31 '17 at 21:23
  • What is the difference between \href and hyperref? – hola Nov 30 '20 at 06:32
  • @hola - A very good question. :-) Please post a new query, so that more potential answer-givers may see it and seize the chance to provide an answer. Thanks. – Mico Nov 30 '20 at 06:40