Re: [NTG-context] TeX expansion within lua

2012-09-05 Thread Hans Hagen

On 4-9-2012 19:40, Marco Patzer wrote:

On 2012-09-04 Martin Schröder mar...@oneiros.de wrote:

Hi Martin,


http://tracker.luatex.org/view.php?id=682


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#1tex.dimen.textwidth then
is smaller
  else
is equal or larger
  fi}}

you don't need to store in a dimen, as effectively these are just numbers:

local temp = \number\dimexpr#1 ;

...

if temptex.dimen.textwidth then
   context(is smaller)
else
   context(is equal or larger)
fi




-
  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
-
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___

Re: [NTG-context] TeX expansion within lua

2012-09-05 Thread Marco Patzer
On 2012-09-05 Hans Hagen pra...@wxs.nl wrote:

Hi Hans,

 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#1tex.dimen.textwidth then
  is smaller
else
  is equal or larger
fi}}

This code does not work for me, but I get the idea of injecting
macro parameters into the Lua code.


Marco

___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___


[NTG-context] TeX expansion within lua

2012-09-04 Thread Marco Patzer
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

___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___


Re: [NTG-context] TeX expansion within lua

2012-09-04 Thread Aditya Mahajan

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
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___


Re: [NTG-context] TeX expansion within lua

2012-09-04 Thread Marco Patzer
On 2012-09-04 Aditya Mahajan adit...@umich.edu wrote:

Hi Aditya,

 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

___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___

Re: [NTG-context] TeX expansion within lua

2012-09-04 Thread Martin Schröder
2012/9/4 Marco Patzer home...@lavabit.com:
 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
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___


Re: [NTG-context] TeX expansion within lua

2012-09-04 Thread Marco Patzer
On 2012-09-04 Martin Schröder mar...@oneiros.de wrote:

Hi Martin,

 http://tracker.luatex.org/view.php?id=682

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

___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___

Re: [NTG-context] TeX expansion within lua

2012-09-04 Thread Wolfgang Schuster

Am 04.09.2012 um 19:40 schrieb Marco Patzer home...@lavabit.com:

 On 2012-09-04 Martin Schröder mar...@oneiros.de wrote:
 
 Hi Martin,
 
 http://tracker.luatex.org/view.php?id=682
 
 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
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___


Re: [NTG-context] TeX expansion within lua

2012-09-04 Thread Marco Patzer
On 2012-09-04 Wolfgang Schuster wolfgang.schus...@gmail.com 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.


Marco

___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___


Re: [NTG-context] TeX expansion within lua

2012-09-04 Thread Wolfgang Schuster

Am 04.09.2012 um 20:11 schrieb Marco Patzer home...@lavabit.com:

 On 2012-09-04 Wolfgang Schuster wolfgang.schus...@gmail.com 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
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___


Re: [NTG-context] TeX expansion within lua

2012-09-04 Thread Marco Patzer
On 2012-09-04 Wolfgang Schuster wolfgang.schus...@gmail.com wrote:

 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

___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___