Thomas Meyer schrieb am 02.10.2024 um 15:44:
Hello, what do I have to do so that the values (d1, m1, y1) from the Lua block are returned with Return and the commented out line \date[d=d1,m=m1,y=y1][weekday,{,~},day,{.~},month,{~},year] works in ConTeXt. In other words: how do I do it right?
To call function or pass information from the TeX side to Lua you use the \directlua command (although ConTeXt provides many helpers and you won't use \directlua itself, e.g. the luacode environment is a wrapper for \directlua with a few special features). To get output back from the Lua side to TeX you use the tex.sprint (or tex.print) function. %%%% begin example \startluacode function Date(str) local year = string.sub(str,1,4) local month = string.sub(str,5,6) local day = string.sub(str,7,8) tex.sprint(day,".",month,".",year) -- tex.sprint(day .. "." .. month .. "." .. year) end \stopluacode \def\Date#1{\directlua{Date("#1")}} \starttext \Date{20241005} \stoptext %%%% end example To pass information from Lua to the value of the \date command you need a separate function call for each of them but ConTeXt makes it easier because it provides the possibility to call a TeX command from Lua itself where you now can pass Lua data to the command. %%%% begin example \startluacode function userdata.ddate(str) local year = string.sub(str,1,4) local month = string.sub(str,5,6) local day = string.sub(str,7,8) context.date({d=day,y=year,m=month},{"weekday,space,day,space,month,space,year"}) end \stopluacode \def\DDate#1{\ctxlua{userdata.ddate("#1")}} \starttext \DDate{20241005} \stoptext %%%% end example Wolfgang