6

I'm using LuaLaTex, TexShop, class Memoir.

I'm trying to make a hyperlinked text with the destination URL containing Unicode characters. For example, https://namu.wiki/w/대전역.

Manually converting Unicode characters to percent encoding and putting the output into \href works:

\href{https://namu.wiki/w/\%EB\%8C\%80\%EC\%A0\%84\%EC\%97\%AD}{대전역 link} 

One disadvantage is that it is very bothersome. Is there a more convenient way to do this?

Enabling the Unicode option \usepackage[unicode]{hyperref} didn't work, and I'm hearing that some PDF viewers can't handle such PDF files even when it does work.

MWE

\class{memoir} \usepackage{hyperref} \begin{document} \href{https://namu.wiki/w/\%EB\%8C\%80\%EC\%A0\%84\%EC\%97\%AD}{대전역 link} % This correctly produces a working hyperlink to https://namu.wiki/w/대전역 \href{https://namu.wiki/w/대전역}{대전역 link} % Clicking this will get you to https://namu.wiki/w/ (note: 대전역 omitted) \end{document} 
5
  • 2
    Welcome to TeX.SX! Can you please add a minimal (non working) example? Commented Sep 24, 2017 at 9:48
  • When I go to the site with Firefox, then copy from the address bar, and paste into my text editor (Emacs), I get the percent encoding https://namu.wiki/w/%EB%8C%80%EC%A0%84%EC%97%AD. I think this is better solution than using a Lua function, because you can then copy paste your TeX source to some other files using xelatex for example or even pdflatex. Also you do link validity checking while writing your source. And it takes very little extra time. Commented Aug 23, 2018 at 6:33
  • (cont.) and \href{https://namu.wiki/w/%EB%8C%80%EC%A0%84%EC%97%AD}{there} works with no need to TeX-escape the % characters, when this \href{...}{...} is not grabbed as argument of some other macro. Commented Aug 23, 2018 at 6:37
  • In a current texlive, compiled with lualatex, the example works fine. Both links work. Commented Apr 17, 2020 at 12:22
  • See also: pdftex - Hyperref: Scandinavian characters (æø) don't work in \url, hyperlink is wrong - TeX - LaTeX Stack Exchange ■ there's a suggestion using \hrefurl[urlencode] in tex.stackexchange.com/a/673889/250119 Commented Apr 17, 2023 at 6:19

1 Answer 1

9

The lua wiki gives %-encoding as an example string function

which you could use as

\documentclass{article} \makeatletter \directlua{function url_encode(str) if (str) then str = string.gsub (str, "\string\n", "\string\r\string\n") str = string.gsub (str, "([^\@percentchar w \@percentchar -\@percentchar _\@percentchar .\@percentchar \string~])", function (c) return string.format ("\@percentchar \@percentchar \@percentchar 02X", string.byte(c)) end) str = string.gsub (str, " ", "+") end return str end} \makeatother \def\zz#1{\directlua{ print(url_encode('\detokenize{#1}')) }} \begin{document} \zz{https://namu.wiki/w/대전역} \end{document} 

This just uses print() so puts the encoded string on the terminal as

https%3A%2F%2Fnamu.wiki%2Fw%2F%EB%8C%80%EC%A0%84%EC%97%AD 

But you could of course replace print by tex.print and its variants to pass this back to TeX.

3
  • Replacing print with tex.print and doing something like \href{https://namu.wiki/w/\zz{대전역}}{Link} did the job. Great answer! Commented Sep 24, 2017 at 12:23
  • 2
    @David Carlisle, why do you need the \detokenize? Commented Sep 24, 2017 at 15:10
  • 2
    @NVaughan oh in case there are any ~ mostly. Commented Sep 24, 2017 at 15:13

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.