TeX expansion within lua

Hi, please have a look at the following example: \starttext \def\cmd{% \def\mymacro{Foobar} \newtoks\mytoks \mytoks={mytoks}} \startluacode context.cmd() context.mymacro() -- this fails -- context(tex.toks.mytoks) \stopluacode -- this works \startluacode context(tex.toks.mytoks) \stopluacode \stoptext Why does the first call to tex.toks.mytoks fail? Apparently the luacode environment has to be closed for cmd to be expanded. I guess that the call to mymacro() succeeds is a quirk due to the face that the macro is expanded later, in contrast to tokens, dimens and counters. Is there a command I can use within Lua to expand a macro immediately or to expand the pending macros to be able to access the values like the token register in the example? Another question which is related: Is there a way to access the contents of the macro from within Lua like counters and token registers? \starttext \newtoks\mytoks \mytoks={Foobar} \startluacode local tok = tex.toks.mytoks -- something like this is what I have in mind -- local mac = tex.macros.somemacro \stopluacode \stoptext I am just interested in a text string, not a box with typeset material. I guess that's more difficult, since macros are expanded and not just simply read. Marco

On 2012-09-04 Aditya Mahajan
Thanks. This works indeed. But what to do in the following case? It's hard to find a proper wording for this problem which makes searching on the net or in the documentation difficult. \starttext \def\cmd {\newdimen\mydimen \mydimen=50pt} \startluacode context.cmd() if tex.dimen.mydimen < tex.dimen.textwidth then context("is smaller") else context("is not smaller") end \stopluacode \stoptext
Thanks. Marco

On 2012-09-04 Martin Schröder
It seems that I just hit a very hard to solve issue which needs not yet available support from the luatex side. Patricks solution seems not very practical and error prone, so I settled with this ugly but still readable workaround: \def\cmd {\newdimen\mydimen \mydimen=50pt} \starttexdefinition action \cmd \startluacode if tex.dimen.mydimen < tex.dimen.textwidth then % context("is smaller") else % context("is not smaller") end % \stopluacode \stoptexdefinition \action Marco

On 2012-09-04 Wolfgang Schuster
Is there a good reason why you put \newdimen because dimen/count etc. registers should always be defined once.
The definitions are not in a macro, they are defined at the top of the file. I messed around while creating a minimal example. However, the problem remains the same since the assignment does not happen within Lua. But, thanks for the hint. I am always happy for improvements and feedback about my code. Marco

On 2012-09-04 Wolfgang Schuster
What prevents you from setting the dimen value in Lua?
Some background: The code is part of a smarter float placement. It takes the size of the float into account and decides for a location. For example, it positions the float in the margin if it fits or it prints the caption underneath the float if the float spans the margin as well (captions are in the margin by default in this layout). Most of the code is written in TeX and I don't feel it's necessary to rewrite it in Lua. I am more fluent in TeX than in Lua. In this case a TeX helper function computes the size and saves the results in dimen registers. I use Lua for calculations especially when it comes to dimensions. It's a nightmare in TeX and hard to find errors easily creep in. Marco
participants (5)
-
Aditya Mahajan
-
Hans Hagen
-
Marco Patzer
-
Martin Schröder
-
Wolfgang Schuster