hyperref icon indicating copy to clipboard operation
hyperref copied to clipboard

Problem with \href and character # in the name of a file

Open petiard opened this issue 1 year ago • 3 comments

Hello (sorry for my bad english...) OS : Windows 10 Pro Adobe Reader updated hyperref version 7.01h

Suppose you have a file named ##abc.pdf and located in C:\Documents\

Then, compile that file with pdflatex:

\documentclass[12pt,a4paper]{article} \usepackage{hyperref} \begin{document} \href{C:/Documents/##abc.pdf}{Fichier} : doesn't work as expected.

\href{{C:/Documents/##abc.pdf}}{Fichier} : doesn't work as expected.

\href{{{C:/Documents/##abc.pdf}}}{Fichier} : works!??? \end{document}

and open the pdf with Adobe Reader; links 1 and 2 don't work but link 3 works! Why?

I don't know if it's similar under Linux or MacOS.

Best regards

François

petiard avatar Feb 03 '24 17:02 petiard

(sorry for my bad english...)

Its fine, and even if not: there would no need to apologize.

links 1 and 2 don't work but link 3 works! Why?

url's uses the # to denote targets on a page. hyperref therefore carefully splits the input at the # in pieces directly at the begin of the processing. (see also https://github.com/latex3/hyperref/issues/254)

There is imho not much one can do. Avoid such file names, or use the braces to protect the argument.

You could also try \hrefpdf from the pdfmanagement. As it is restricted to pdf-files it doesn't have to apply code for urls:

\DocumentMetadata{}
\documentclass[12pt,a4paper]{article}
\usepackage{hyperref}
\begin{document}
\hrefpdf{C:/Documents/##abc.pdf}{Fichier}
\end{document}

u-fischer avatar Feb 03 '24 18:02 u-fischer

you can probably use the correctly encoded URL to refer to such a file which would be to %-encode the # as %23 but why have a # in a file name (other than editor backup files)

davidcarlisle avatar Feb 03 '24 19:02 davidcarlisle

Thank you. I agree: no # in filenames... In fact, it's not for me but there was a question on Liste Gutenberg ([email protected]). I've found the solution with two couples of braces but I really don't understand why it works... And, no, the encode # by %23 doesn't work. François

petiard avatar Feb 05 '24 14:02 petiard