ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* expansion problem: random location
@ 2019-09-13 16:51 Henning Hraban Ramm
  2019-09-13 17:44 ` Wolfgang Schuster
  0 siblings, 1 reply; 4+ messages in thread
From: Henning Hraban Ramm @ 2019-09-13 16:51 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi, in an example document I’m trying to get pictures of random size and placement.
Unfortunately the expansion is over my head:

%%%%%%%%%%%%%%%%%
\useMPlibrary[dum]
\usemodule[visual]

\startluacode

local locations = {
  'top',
  'bottom',
  'left',
  'right',
}

function RandomLocation()
  context(locations[math.random(1,#locations)])
end

\stopluacode

\newdimen\PicWidth
\PicWidth=\textwidth

\define[1]\Blindbild{
\getrandomnumber\imgH{1}{9}
\PicWidth=\textwidth
\doif{#1}{left}{\PicWidth=.4\textwidth}
\doif{#1}{right}{\PicWidth=.4\textwidth}
\startplacefigure[location={#1},title={#1 \fakewords{3}{20}}]
\externalfigure[place #1][width=\PicWidth,height=\dimexpr\textheight * \imgH / 10 \relax]
\stopplacefigure
}

\starttext
   \dorecurse{3}
     {\chapter{\fakewords{5}{10}}
       \dorecurse{3}
         {\dorecurse{3}{\fakewords{50}{100}\endgraf}
            \Blindbild{\ctxlua{RandomLocation()}} % expansion!?
            \dorecurse{2}{\fakewords{50}{100}\endgraf}}}
\stoptext
%%%%%%%%%%%%%%%%%%

So the parameter of \Blindbild should get generated at the time of calling the macro, so that #1 stays the same within the macro.
In this state the Lua function gets called at every use of #1.


Greetlings, Hraban
---
https://www.fiee.net
http://wiki.contextgarden.net
https://www.dreiviertelhaus.de
GPG Key ID 1C9B22FD

___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: expansion problem: random location
  2019-09-13 16:51 expansion problem: random location Henning Hraban Ramm
@ 2019-09-13 17:44 ` Wolfgang Schuster
  2019-09-13 18:41   ` Henning Hraban Ramm
  0 siblings, 1 reply; 4+ messages in thread
From: Wolfgang Schuster @ 2019-09-13 17:44 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Henning Hraban Ramm

Henning Hraban Ramm schrieb am 13.09.2019 um 18:51:
> Hi, in an example document I’m trying to get pictures of random size and placement.
> Unfortunately the expansion is over my head:
>
> %%%%%%%%%%%%%%%%%
> \useMPlibrary[dum]
> \usemodule[visual]
>
> \startluacode
>
> local locations = {
>    'top',
>    'bottom',
>    'left',
>    'right',
> }
>
> function RandomLocation()
>    context(locations[math.random(1,#locations)])
> end
>
> \stopluacode
>
> \newdimen\PicWidth
> \PicWidth=\textwidth
>
> \define[1]\Blindbild{
> \getrandomnumber\imgH{1}{9}
> \PicWidth=\textwidth
> \doif{#1}{left}{\PicWidth=.4\textwidth}
> \doif{#1}{right}{\PicWidth=.4\textwidth}
> \startplacefigure[location={#1},title={#1 \fakewords{3}{20}}]
> \externalfigure[place #1][width=\PicWidth,height=\dimexpr\textheight * \imgH / 10 \relax]
> \stopplacefigure
> }
Put the value of the random location in a macro.

\define[1]\Blindbild
   {\edef\PicLocation{#1}%
    \getrandomnumber\imgH{1}{9}%
    \PicWidth=\textwidth
    \doifinset{\PicLocation}{left,right}{\PicWidth=.4\textwidth}%
\startplacefigure[location={\PicLocation},title={\PicLocation\space\fakewords{3}{20}}]
    \externalfigure
      [place \PicLocation]
      [width=\PicWidth,
       height=\dimexpr\textheight * \imgH / 10 \relax]
    \stopplacefigure}

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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: expansion problem: random location
  2019-09-13 17:44 ` Wolfgang Schuster
@ 2019-09-13 18:41   ` Henning Hraban Ramm
  2019-09-14  6:47     ` Wolfgang Schuster
  0 siblings, 1 reply; 4+ messages in thread
From: Henning Hraban Ramm @ 2019-09-13 18:41 UTC (permalink / raw)
  To: mailing list for ConTeXt users


> Am 2019-09-13 um 19:44 schrieb Wolfgang Schuster <wolfgang.schuster.lists@gmail.com>:
> 
> Henning Hraban Ramm schrieb am 13.09.2019 um 18:51:
>> Hi, in an example document I’m trying to get pictures of random size and placement.
>> Unfortunately the expansion is over my head:
> Put the value of the random location in a macro.

Thank you!
I thought I had tried that, but probably only defining the macro outside of the other…


Greetlings, Hraban
---
https://www.fiee.net
http://wiki.contextgarden.net
https://www.dreiviertelhaus.de
GPG Key ID 1C9B22FD

___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: expansion problem: random location
  2019-09-13 18:41   ` Henning Hraban Ramm
@ 2019-09-14  6:47     ` Wolfgang Schuster
  0 siblings, 0 replies; 4+ messages in thread
From: Wolfgang Schuster @ 2019-09-14  6:47 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Henning Hraban Ramm

Henning Hraban Ramm schrieb am 13.09.2019 um 20:41:
>> Am 2019-09-13 um 19:44 schrieb Wolfgang Schuster <wolfgang.schuster.lists@gmail.com>:
>>
>> Henning Hraban Ramm schrieb am 13.09.2019 um 18:51:
>>> Hi, in an example document I’m trying to get pictures of random size and placement.
>>> Unfortunately the expansion is over my head:
>> Put the value of the random location in a macro.
> Thank you!
> I thought I had tried that, but probably only defining the macro outside of the other…
It could have been that you used \def which doesn't work because this 
will always
call the Lua function and you get a new random value but with \edef the 
result
of the first random value is stored in the macro.

  \def\RandomA{\cldcontext{math.random(1,4)}}
\edef\RandomB{\cldcontext{math.random(1,4)}}

\starttext
\dorecurse{10}{A: \RandomA \quad B: \RandomB\par}
\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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

end of thread, other threads:[~2019-09-14  6:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-13 16:51 expansion problem: random location Henning Hraban Ramm
2019-09-13 17:44 ` Wolfgang Schuster
2019-09-13 18:41   ` Henning Hraban Ramm
2019-09-14  6:47     ` Wolfgang Schuster

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).