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}
Stefan Pinnow
  • 29,535
guest
  • 61

1 Answers1

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.

David Carlisle
  • 757,742