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 Tue, 4 Sep 2012, Marco Patzer wrote:
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?
Instead of context(tex.toks.mytoks) use context(function () context(tex.toks.mytoks) end) See the ConTeXt Lua Document manual for explanation.
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.
AFAIK, this is not possible. Aditya
On 2012-09-04 Aditya Mahajan
Instead of
context(tex.toks.mytoks)
use
context(function () context(tex.toks.mytoks) end)
See the ConTeXt Lua Document manual for explanation.
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
Is there a way to access the contents of the macro from within Lua like counters and token registers?
[…]
AFAIK, this is not possible.
Thanks. Marco
2012/9/4 Marco Patzer
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.
http://tracker.luatex.org/view.php?id=682 Best Martin
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
Am 04.09.2012 um 19:40 schrieb Marco Patzer
On 2012-09-04 Martin Schröder
wrote: Hi Martin,
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
Is there a good reason why you put \newdimen because dimen/count etc. registers should always be defined once. Wolfgang
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
Am 04.09.2012 um 20:11 schrieb Marco Patzer
On 2012-09-04 Wolfgang Schuster
wrote: 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.
What prevents you from setting the dimen value in Lua? \starttext \scratchdimen = 20pt \the\scratchdimen \ctxlua{tex.dimen.scratchdimen = "10pt"} \the\scratchdimen \stoptext Wolfgang
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
On 4-9-2012 19:40, Marco Patzer wrote:
On 2012-09-04 Martin Schröder
wrote: Hi Martin,
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
you probably over code things ... instead of storing you can pass the
valus directly
\def\largerthantextwidth#1%
{\cldcontext
{if \number\dimexpr#1
On 2012-09-05 Hans Hagen
you probably over code things
Yes, that's not unlikely, I tend to do that (unintentional).
instead of storing you can pass the valus directly
\def\largerthantextwidth#1% {\cldcontext {if \number\dimexpr#1
This code does not work for me, but I get the idea of injecting macro parameters into the Lua code. Marco
participants (5)
-
Aditya Mahajan
-
Hans Hagen
-
Marco Patzer
-
Martin Schröder
-
Wolfgang Schuster