7

I am trying to use an \href linking to a web URL including an HTML anchor pointer. Hence I am using a hash sign (#) in the URL. For example to link the page https://github.com/latex3/hyperref#bug-reports.

Here is a minimal example:

\documentclass{article}
\usepackage{hyperref}
\begin{document}

\href{https://github.com/latex3/hyperref#bug-reports}{link with hash that doesn't work}

\href{https://en.wikibooks.org/wiki/LaTeX/Hyperlinks#Commands}{link with hash where it works}

\end{document}

The problem is that the hash sign gets replaced by the URL escape character %23 when the PDF is generated. If I click on the link in the PDF the browser will get the link in the following form:

https://github.com/latex3/hyperref%23bug-reports

But this leads to an 404-Error on GitHub.

If I replace the %23 with # in the address bar, the proper page can be loaded.

On other pages (for example wikibooks) links with URL-escaped # work (shown in the example above).

Is it possible to switch off url-escaping of the hash sign in links?

I am using pdfTeX 3.14159265-2.6-1.40.21 (TeX Live 2020) on MacOS Catalina.

nnn
  • 333
  • Maybe try loading xurl? – Bernard Jul 29 '20 at 14:31
  • I'm a bit confused. You've found that the wikibooks link works with # unescaped. Why not leave it unescaped in the github link? My understanding is that hyperref does some stuff behind the scenes so that you don't need to escape # in the first place. And in fact, with TeXLive 2020, both of your examples work fine, so it seems that you can choose whether to escape # or not. – Teepeemm Jul 29 '20 at 14:41
  • with Overleaf in Firefox 78.0.2 I do not get any problems - see: https://www.overleaf.com/read/rbqtfrbzvgvz. Maybe you can add some information which LaTeX Version, which pdf-reader and which browser do you use? – Jakob Jul 29 '20 at 14:50
  • 6
    are you on mac? some pdf viewer are broken there and misinterprete the hash. https://github.com/latex3/hyperref/issues/110. There is nothing what hyperref can do here, the viewer must be fixed. – Ulrike Fischer Jul 29 '20 at 14:57
  • @UlrikeFischer - I'm on a Mac, and I experience no problems compiling the OP's code with pdflatex of the MacTeX2020 distribution -- and accessing the URLs using Acrobat Reader as the pdf viewer. :-) – Mico Jul 29 '20 at 15:09
  • 1
    @Mico acrobat should be fine, the reported issues were with preview and safari. – Ulrike Fischer Jul 29 '20 at 15:16
  • 3
    hyperref does some hard work to allow you not to escape the # and still work correctly, so in general I'd prefer the unescaped # over \# anyway. In my tests all viewers resolved both links from the MWE correctly (PDF compiled with pdfLaTeX on MikTeX, checked the links with TeXworks' built-in viewer, SumatraPDF, Adobe Reader and Firefox). – moewe Jul 29 '20 at 15:17
  • Sometimes the unescaped hash sign brought up errors in the document compilation. But you are right, here it doesn't. I think that is a different thing not related to the %23 problem. So I will edit it out of the quesion. – nnn Jul 30 '20 at 07:03
  • Yes, I didn't mention in my comment that there are some situation where hyperref's trickery won't work and where an unescaped # throws errors (if you are using \url/\href within the argument of another command for example). In those cases you can either escape the # or use a workaround like \urldef. – moewe Jul 30 '20 at 07:18
  • I'll just add that I stumbled onto this problem last night, and the solution consisted of a tweak on the server housing the URL I needed. One of the sysadmins changed a configuration option and now both # and %23 work. I run up-to-date Apple devices and OSs here FWIW. None of the above issues happen for me now. – LaTeXereXeTaL Jan 04 '21 at 20:55

3 Answers3

9

As was layed out in the comments the problem here is not LaTeX and not the Browser but the pdf viewer. Some PDF viewers on MacOS seem to convert the hash sign in the PDF that is correctly inserted by LaTeX.

There is a table in this bug report showing which pdf viewers on MacOS create the problem:

Table of PDF viewers with hash problem

The bug seems to be on the side of Apple and was reported on 2 March this year. No fixes yet. I think the workaround for now is to use a different PDF viewer.

Also this question might be a duplicate of this question.

nnn
  • 333
  • It also does not work in TeXShop.app's PDF viewer (v4.66) (macOS 10.15.7) as of 9 August 2021: the hash is converted to %23 so the link is broken. – jvriesem Aug 09 '21 at 18:34
0

I do not have the reputation to comment on the earlier reply, but I want to point out that Google Docs also makes this error, when you select the menu option to download a document in PDF format. I used a URL shortener to get around the problem.

Brad
  • 11
  • 1
0

I'd like to point out that something more complex is going on. @nnn is correct that some of OSX's viewers are broken.

Compiling their code with pdfTeX 3.141592653-2.6-1.40.24 on OSX 10.15.7 and opening with Preview 11.0, the first link fails with the # replaced with %23, however the second link works without issue. I see no difference between the two links and the usage of the #. Also note that 23 is the ASCII hexadecimal value for #. Something more complex must be going on.

To provide a useful answer the question, Google Chrome (v.99.0.4844.83) on OSX 10.15.7, will open these files correctly.

  • "Something more complex must be going on." Because on Wikibooks, the URL with %23 is redirected by the server to the URL with # (you can see in your browser that the address with %23 becomes, after a quick refresh by the browser the address with %). On Github, they don't redirect, and the page with %23 doesn't exist. Wikibooks (and Wikipedia) has a lot of redirections. For exemple the wrong https://en.wikipedia.org/wiki/infared (see the lowercase and the missing "r") is redirected to https://en.wikipedia.org/wiki/Infrared. – quark67 Jan 07 '23 at 12:56