ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* TeX expansion within lua
@ 2012-09-04 15:12 Marco Patzer
  2012-09-04 15:28 ` Aditya Mahajan
  2012-09-04 17:06 ` Martin Schröder
  0 siblings, 2 replies; 11+ messages in thread
From: Marco Patzer @ 2012-09-04 15:12 UTC (permalink / raw)
  To: context

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
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: TeX expansion within lua
  2012-09-04 15:12 TeX expansion within lua Marco Patzer
@ 2012-09-04 15:28 ` Aditya Mahajan
  2012-09-04 16:54   ` Marco Patzer
  2012-09-04 17:06 ` Martin Schröder
  1 sibling, 1 reply; 11+ messages in thread
From: Aditya Mahajan @ 2012-09-04 15:28 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: TeX expansion within lua
  2012-09-04 15:28 ` Aditya Mahajan
@ 2012-09-04 16:54   ` Marco Patzer
  0 siblings, 0 replies; 11+ messages in thread
From: Marco Patzer @ 2012-09-04 16:54 UTC (permalink / raw)
  To: ntg-context

On 2012-09-04 Aditya Mahajan <adityam@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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: TeX expansion within lua
  2012-09-04 15:12 TeX expansion within lua Marco Patzer
  2012-09-04 15:28 ` Aditya Mahajan
@ 2012-09-04 17:06 ` Martin Schröder
  2012-09-04 17:40   ` Marco Patzer
  1 sibling, 1 reply; 11+ messages in thread
From: Martin Schröder @ 2012-09-04 17:06 UTC (permalink / raw)
  To: mailing list for ConTeXt users

2012/9/4 Marco Patzer <homerow@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
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: TeX expansion within lua
  2012-09-04 17:06 ` Martin Schröder
@ 2012-09-04 17:40   ` Marco Patzer
  2012-09-04 17:53     ` Wolfgang Schuster
  2012-09-05  9:18     ` Hans Hagen
  0 siblings, 2 replies; 11+ messages in thread
From: Marco Patzer @ 2012-09-04 17:40 UTC (permalink / raw)
  To: ntg-context

On 2012-09-04 Martin Schröder <martin@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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: TeX expansion within lua
  2012-09-04 17:40   ` Marco Patzer
@ 2012-09-04 17:53     ` Wolfgang Schuster
  2012-09-04 18:11       ` Marco Patzer
  2012-09-05  9:18     ` Hans Hagen
  1 sibling, 1 reply; 11+ messages in thread
From: Wolfgang Schuster @ 2012-09-04 17:53 UTC (permalink / raw)
  To: mailing list for ConTeXt users


Am 04.09.2012 um 19:40 schrieb Marco Patzer <homerow@lavabit.com>:

> On 2012-09-04 Martin Schröder <martin@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
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: TeX expansion within lua
  2012-09-04 17:53     ` Wolfgang Schuster
@ 2012-09-04 18:11       ` Marco Patzer
  2012-09-04 18:25         ` Wolfgang Schuster
  0 siblings, 1 reply; 11+ messages in thread
From: Marco Patzer @ 2012-09-04 18:11 UTC (permalink / raw)
  To: ntg-context

On 2012-09-04 Wolfgang Schuster <wolfgang.schuster@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
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: TeX expansion within lua
  2012-09-04 18:11       ` Marco Patzer
@ 2012-09-04 18:25         ` Wolfgang Schuster
  2012-09-04 18:58           ` Marco Patzer
  0 siblings, 1 reply; 11+ messages in thread
From: Wolfgang Schuster @ 2012-09-04 18:25 UTC (permalink / raw)
  To: mailing list for ConTeXt users


Am 04.09.2012 um 20:11 schrieb Marco Patzer <homerow@lavabit.com>:

> On 2012-09-04 Wolfgang Schuster <wolfgang.schuster@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
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: TeX expansion within lua
  2012-09-04 18:25         ` Wolfgang Schuster
@ 2012-09-04 18:58           ` Marco Patzer
  0 siblings, 0 replies; 11+ messages in thread
From: Marco Patzer @ 2012-09-04 18:58 UTC (permalink / raw)
  To: ntg-context

On 2012-09-04 Wolfgang Schuster <wolfgang.schuster@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
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: TeX expansion within lua
  2012-09-04 17:40   ` Marco Patzer
  2012-09-04 17:53     ` Wolfgang Schuster
@ 2012-09-05  9:18     ` Hans Hagen
  2012-09-05  9:42       ` Marco Patzer
  1 sibling, 1 reply; 11+ messages in thread
From: Hans Hagen @ 2012-09-05  9:18 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On 4-9-2012 19:40, Marco Patzer wrote:
> On 2012-09-04 Martin Schröder <martin@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#1<tex.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 temp<tex.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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: TeX expansion within lua
  2012-09-05  9:18     ` Hans Hagen
@ 2012-09-05  9:42       ` Marco Patzer
  0 siblings, 0 replies; 11+ messages in thread
From: Marco Patzer @ 2012-09-05  9:42 UTC (permalink / raw)
  To: context

On 2012-09-05 Hans Hagen <pragma@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#1<tex.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
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2012-09-05  9:42 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-09-04 15:12 TeX expansion within lua Marco Patzer
2012-09-04 15:28 ` Aditya Mahajan
2012-09-04 16:54   ` Marco Patzer
2012-09-04 17:06 ` Martin Schröder
2012-09-04 17:40   ` Marco Patzer
2012-09-04 17:53     ` Wolfgang Schuster
2012-09-04 18:11       ` Marco Patzer
2012-09-04 18:25         ` Wolfgang Schuster
2012-09-04 18:58           ` Marco Patzer
2012-09-05  9:18     ` Hans Hagen
2012-09-05  9:42       ` Marco Patzer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).