Expansion of TeX macros inside the Lua function
I came across an interesting issue (for me), which is probably the beginner, but accompanied me for a long time. The problem is probably that I do not understand very well the principles of operation LuaTeX and while I want to do more complex things. I think many beginners will love it when someone indicate solutions. My problem is that I need in my complex application, evaluate parameters of Lua functions, which are a TeX macros (respectively their expanded contents). I did not discover the possibility of expansion of TeX macros, which is a parameter Lua function. Here are three examples in which I demonstrate the problem. 1. In first example is function parameter a text string. \ctxlua{pars('a,b,c,d,e')} Everything works as expected - evaluation of parameter was carried out OK - comma separator was found and replaced. 2. In second example is a function parameter a TeX macro \printaction. Macro expanded before entering the function. Everything works as expected too - comma separator is found and replaced: \ctxlua{pars('\printaction')} 3. The third example of my most interesting. Function parameter in example is a TeX macro \printaction which not expanded, respectively expanded only when I printing it to TeX. Parameter of function is string "\printaction" in which it is futile to look for a comma separator. Comma not be found, although the content of macro face that is so. \ctxlua{pars('\\printaction')} Is there a possibility for this case, as in some way to save to Lua variable content of macro (of course expanded)? I note that this example is important for me and I would like there to be a Lua function (eg "expandtostring"), which expanded a TeX macro into variable until inside function Lua. I hope the my example (with comments) will be comprehensible and I apologize if the solution is too trivial. I searched dozens of examples, but I come across something similar. Thanx very much - Jaroslav Here is example: \startluacode function pars(inppar) strpar=tostring(inppar) -- here I need a function "expandtostring(inppar)" gsubstr=string.gsub(strpar, ',', ';') tex.print("Input parametr (string or macro): ") tex.write(inppar) tex.print("\\par") tex.print("Content of string (macro): "..strpar) -- There will always be expansion tex.print("\\par") tex.print("Length string (macro): "..string.len(inppar).."\\par") tex.print("Length of content : "..string.len(strpar).."\\par") tex.print("Occurrence of the commma separator: "..gsubstr.."\\par") end \stopluacode \def\printaction{x,y,z} \starttext Example 1) Function parameter in first example is a text string. Everything works as expected - comma separator is found and replaced: \ctxlua{pars('a,b,c,d,e')} \blank[big] Example 2) Function parameter in second example is a TeX macro \backslash printaction which expanded before entering the function. Everything works as expected too - comma separator is found and replaced: \ctxlua{pars('\printaction')} \blank[big] Example 3) The third example of my most interesting. Function parameter in example is a TeX macro \backslash printaction which not expanded, respectively expanded only when I printing it to TeX. Parameter of function is still string "\backslash printaction" in which it is futile to look for a comma separator. Comma not be found, although the content of macro face that is so. Is there a possibility for this case, as in some way to save to Lua variable content of macro (of course expanded)? \ctxlua{pars('\\printaction')} \stoptext
On 1-8-2010 11:45, Jaroslav Hajtmar wrote:
\ctxlua{pars('\printaction')}
\blank[big]
Example 3)
The third example of my most interesting. Function parameter in example is a TeX macro \backslash printaction which not expanded, respectively expanded only when I printing it to TeX. Parameter of function is still string "\backslash printaction" in which it is futile to look for a comma separator. Comma not be found, although the content of macro face that is so. Is there a possibility for this case, as in some way to save to Lua variable content of macro (of course expanded)?
\ctxlua{pars('\\printaction')}
currently the tex parsing and expansion code is not available in luatex (future versions might provide some introspection or expansion code, but it's a hairy topic) Anyhow, given that \printaction is defined you can use the token library to see what's in there (you then can sort of reconstruct the content). Another approach is a two-step: function step_1(str) -- str = \whatever tex.print("\ctxlua{step_2('",str,"')}") end It all depends on what you want to do. Why do you want to look into \whatever? Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
Thanx very much to Hans. I see that you understood what I meant. I expected something like this probably will not do. I'll try to experiment according to your advice. You ask why do I need? It is hard for me to explain in English why I need it. Only shortly: I prepared a series of Lua functions, that accesses data from a CSV file in very general level. In fact I can in Luacycle print in ConTeXt (but in LuaLaTeX or LuaPlain too) any reports, forms, mail merge letters, etc. The user creates a report (using data of row of the CSV table). The user can in report use TeX macros, that are automaticaly creating during browsing of the CSV file. Macro names are derived from Excel table columns (\ cA, \ cB, \ cC, ...) or are created under the header (first line) CSV table. I had it working quite well, but I am trying now output from my applications to use as an input to the database module of Mojca Miklavec. The problem is that output of my application appears in the one macro (printmacro \printacion or maybe better \lineaction because it contains the row data of the CSV table). Unfortunately, I have a macro in unexpanded form (as one token), because it constantly changes according to the contents of the current row of CSV table, but Mojca module requires several values separated by separators. I hope at least someone can understand my bad English ... Sorry Taco :-) ... Thank you all for your patience with amateurs such as myself. I have great admiration for you all and I am privileged to be helping me. Thanks Jaroslav Dne 1.8.2010 17:08, Hans Hagen napsal(a):
On 1-8-2010 11:45, Jaroslav Hajtmar wrote:
\ctxlua{pars('\printaction')}
\blank[big]
Example 3)
The third example of my most interesting. Function parameter in example is a TeX macro \backslash printaction which not expanded, respectively expanded only when I printing it to TeX. Parameter of function is still string "\backslash printaction" in which it is futile to look for a comma separator. Comma not be found, although the content of macro face that is so. Is there a possibility for this case, as in some way to save to Lua variable content of macro (of course expanded)?
\ctxlua{pars('\\printaction')}
currently the tex parsing and expansion code is not available in luatex (future versions might provide some introspection or expansion code, but it's a hairy topic)
Anyhow, given that \printaction is defined you can use the token library to see what's in there (you then can sort of reconstruct the content).
Another approach is a two-step:
function step_1(str) -- str = \whatever tex.print("\ctxlua{step_2('",str,"')}") end
It all depends on what you want to do. Why do you want to look into \whatever?
Hans
----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
participants (2)
-
Hans Hagen
-
Jaroslav Hajtmar