Skip to content

Instantly share code, notes, and snippets.

@dohyunkim
Last active February 5, 2025 06:45
Show Gist options
  • Save dohyunkim/9de0ef55b15575979a04004a6197b0fe to your computer and use it in GitHub Desktop.
Save dohyunkim/9de0ef55b15575979a04004a6197b0fe to your computer and use it in GitHub Desktop.
mplib utf8 substring
\documentclass{article}
\usepackage{luamplib}
\begin{document}
\directlua{
function luamplib.utf8substr (t,b,e)
local ob = utf8.offset(t,b+1)
local oe = utf8.offset(t,e)
local ms = utf8.char(utf8.codepoint(t,ob,oe)):gsub('"','"&ditto&"')
return string.format('"\csstring\%s"', ms)
end
}
\mplibtextextlabel{true}
\begin{mplibcode}
def utfoolength primary t =
runscript("return utf8.len[==["&t&"]==]")
enddef;
def utfoosubstring expr p of t =
runscript("return luamplib.utf8substr([==["&t&"]==],"&decimal xpart p&","&decimal ypart p&")")
enddef;
beginfig(1);
string Test;
Test="AaeéB";
for k=0 upto utfoolength(Test)-2:
label.top(utfoosubstring (k,k+2) of Test, (20k,0));
endfor;
endfig;
\end{mplibcode}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment