Last active
February 5, 2025 06:45
-
-
Save dohyunkim/9de0ef55b15575979a04004a6197b0fe to your computer and use it in GitHub Desktop.
mplib utf8 substring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\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