# Automatically convert CJK Unicode to percent encoding in HREF

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/대전역}{대전역 link} % Clicking this will get you to https://namu.wiki/w/ (note: 대전역 omitted)

\end{document}

• Welcome to TeX.SX! Can you please add a minimal (non working) example? – egreg Sep 24 '17 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. – user4686 Aug 23 '18 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. – user4686 Aug 23 '18 at 6:37

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.

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

## protected by Community♦Sep 3 '18 at 12:58

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).