3

Some URLs include unicode characters. For example https://en.wikipedia.org/wiki/Durbin–Watson_statistics. All (most?) webbrowsers are able to handle such URLs but some PDF viewers (like Mac's Preview and Skim) aren't. How would one show such a URL in a LaTeX document?

You can obviously use \href{encoded URL}{original URL} but this one fails to linebreak the "original URL" part.

The plain \url{original URL} command does print the correct URL but this hyperlink doesn't work because it contains unicode characters.

Using the encoded URL \url{encoded URL} does work and it linebreaks but it's too ugly to print.

I think it would be nice if the \url command encoded the URL for the hyperlink without altering the text it prints.

\documentclass{article}
\usepackage{xurl}
\usepackage[breaklinks]{hyperref}
\begin{document}
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah

\noindent%
1. blah blah blah blah blah blah\href{https://en.wikipedia.org/wiki/Durbin\%E2\%80\%93Watson_statistic}%
{https://en.wikipedia.org/wiki/Durbin–Watson\_statistic}

\noindent%
2. blah blah blah blah blah blah\url{https://en.wikipedia.org/wiki/Durbin\%E2\%80\%93Watson_statistic}

\noindent%
3. blah blah blah blah blah blah\url{https://en.wikipedia.org/wiki/Durbin–Watson\_statistic}
\end{document}

Edit: Thank you for the solution below, where \nolinkurl solves the problem even with pdflatex, but this discussion has made me realize that pdflatex is not quite able to handle unicode gracefully. If you copy the URL text from the generated PDF, the text in the clipboard is not always "correct":

\documentclass{article}
\usepackage[colorlinks=true,urlcolor=blue]{hyperref}
\begin{document}
\href{https://en.wikipedia.org/wiki/Schr\%C3\%B6dinger}%
{\nolinkurl{https://en.wikipedia.org/wiki/Erwin\_Schr}ö\nolinkurl{dinger}}
\end{document}

pdflatex this document, open it with a PDF viewer, copy the text of the URL, and paste it into the address box of a webbrowser. The character ö is copied as * ̈o* and this URL doesn't work.

Unfortunately, this isn't a contrived complaint because you sometimes want to copy the URL instead of clicking on it. (Sometimes you just don't want to click on it.)

lualatex produces ö in the PDF. This problem isn't directly relevant to my original problem. I should've used lualatex in the first place.

2

2 Answers 2

4

The easiest is to use lualatex, where it works more or less out-of-the-box (or at least it is solvable by defining a suitable font):

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

\noindent%
1. blah blah blah blah blah blah\href{https://en.wikipedia.org/wiki/Durbin\%E2\%80\%93Watson_statistic}%
{\nolinkurl{https://en.wikipedia.org/wiki/Durbin–Watson_statistic}}

\end{document}

When using the new PDF management by starting with \DocumentMetadata you can even let the code do the percent encoding for you:

\DocumentMetadata{}

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

\noindent%
1. blah blah blah blah blah blah\href{https://en.wikipedia.org/wiki/Durbin\%E2\%80\%93Watson_statistic}%
{\nolinkurl{https://en.wikipedia.org/wiki/Durbin–Watson_statistic}}

\url[urlencode]{https://en.wikipedia.org/wiki/Durbin–Watson_statistic}

\end{document}

enter image description here

With pdflatex there is no good solution. The way the url package works (by switching to math) makes it up-to impossible to properly support unicode. See e.g. https://tex.stackexchange.com/a/670811/2388. So you will have to insert break points manually in such cases.

3
  • Thanks! But, I was surprised that with the luacolor package and with the colorlink=true option of the hyperref package, \DocumentMetadata{} renders the links in blank! (I meant to ask why DocumentMetadata isn't set by default, but I guess that's because there is some unwanted interaction like this?)
    – Ryo
    Jan 21 at 17:16
  • The pdfmanagement code is quite stable, but there are packages which aren't compatible with, see the documentation of pdfmanagement-testphase. If you find some unwanted interaction please report it at github.com/latex3/pdfresources. (color problems are quite possible, with the new code: hyperref uses l3color, but that isn't fully integrated with xcolor and luacolor and unifying everything isn't trivial but if I get a report I will try to resolve it.) Jan 21 at 17:53
  • 1
    With \nolinkurl, there is a solution with pdflatex (see my answer).
    – quark67
    Jan 21 at 18:24
4

Here's a solution (I add \urlstyle{rm} because when \nolinkurl is used, by default, the URL is displayed in typewriter font):

1 is your first attempt.

2 is almost the solution, but the en dash isn't displayed.

3 is the solution.

\documentclass{article}
\usepackage{xurl}
\urlstyle{rm}
\usepackage[breaklinks]{hyperref}
\begin{document}
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah

\noindent%
1. blah blah blah blah blah blah\href{https://en.wikipedia.org/wiki/Durbin\%E2\%80\%93Watson_statistic}%
{https://en.wikipedia.org/wiki/Durbin–Watson\_statistic}

\noindent%
2. blah blah blah blah blah blah\href{https://en.wikipedia.org/wiki/Durbin\%E2\%80\%93Watson_statistic}%
{\nolinkurl{https://en.wikipedia.org/wiki/Durbin–Watson\_statistic}}

\noindent%
3. blah blah blah blah blah blah\href{https://en.wikipedia.org/wiki/Durbin\%E2\%80\%93Watson_statistic}%
{\nolinkurl{https://en.wikipedia.org/wiki/Durbi}--\nolinkurl{Watson\_statistic}}
\end{document}

enter image description here

Edit

In answer to the edited question:

Replace:

\documentclass{article}
\usepackage[colorlinks=true,urlcolor=blue]{hyperref}
\begin{document}
\href{https://en.wikipedia.org/wiki/Schr\%C3\%B6dinger}%
{\nolinkurl{https://en.wikipedia.org/wiki/Erwin\_Schr}ö\nolinkurl{dinger}}
\end{document}

with:

\documentclass{article}

\usepackage[T1]{fontenc} % Necessary for using things like `\"o` in `\nolinkurl`
\usepackage{url} % For the style of the displayed URL
\urlstyle{rm} % For using the roman style (in place of typewriter)

\usepackage[colorlinks=true,urlcolor=blue]{hyperref}
\begin{document}
\href{https://en.wikipedia.org/wiki/Schr\%C3\%B6dinger}%
{\nolinkurl{https://en.wikipedia.org/wiki/Erwin\_Schr\"odinger}} % replacing ö by \"o, you can put everything in \nolinkurl
\end{document}

By adding \usepackage[T1]{fontenc} and replacing ö by \"o, you can put everything in \nolinkurl.

(\usepackage{url} and \urlstyle{rm} is optional, you can avoid it if you like typewriter style).

You obtain then:

enter image description here

and if you copy it in the PDF reader, you will obtain https://en.wikipedia.org/wiki/Erwin_Schrödinger as expected.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .