ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Code Called Twice?
@ 2013-10-11  6:21 Thangalin
  2013-10-11  7:27 ` Marco Patzer
  2013-10-11 11:00 ` Philipp Gesang
  0 siblings, 2 replies; 7+ messages in thread
From: Thangalin @ 2013-10-11  6:21 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi,

I noticed something unexpected with the following code:

\enabletrackers[metapost.showlog]

\startuseMPgraphic{heading:ThemeTitleStyle}
  show "HEADING:THEMETITLESTYLE";
  show \MPstring{heading:title};

  draw textext( \MPstring{heading:title} ) rotated 5
    withcolor blue;
\stopuseMPgraphic

\defineframed[ThemeTitleStyle][
  background=\useMPgraphic{ThemeTitleStyle},
]

\def\processword#1{%
  \setMPtext{heading:title}{#1}%
  \useMPgraphic{heading:ThemeTitleStyle}
}

\define[1]\ThemeChapterTitle{\applytosplitstringword\processword{#1}}
\setuphead[chapter][deeptextcommand={\ThemeChapterTitle}]

\starttext
  \startchapter[title=Why Is heading:ThemeTitleStyle Called Twice?]
  \input knuth
  \stopchapter
\stoptext

It appears as though heading:ThemeTitleStyle is being called twice:

metapost        > info:
>> "HEADING:THEMETITLESTYLE"
>> "Why"
metapost        > info:
>> "HEADING:THEMETITLESTYLE"
>> "Why"

I am trying to produce a random number that is not equal to the
previous random number. However, if the code is called twice, then I'd
have to add code to ignore one of the calls.

Any ideas why the code seems to be called twice and what can be done
to ensure that it is only called once?

Thank you!
___________________________________________________________________________________
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] 7+ messages in thread

* Re: Code Called Twice?
  2013-10-11  6:21 Code Called Twice? Thangalin
@ 2013-10-11  7:27 ` Marco Patzer
  2013-10-11 16:39   ` Thangalin
  2013-10-11 11:00 ` Philipp Gesang
  1 sibling, 1 reply; 7+ messages in thread
From: Marco Patzer @ 2013-10-11  7:27 UTC (permalink / raw)
  To: ntg-context


[-- Attachment #1.1: Type: text/plain, Size: 994 bytes --]

On 2013–10–10 Thangalin wrote:

> I noticed something unexpected with the following code:
> 
> \enabletrackers[metapost.showlog]
> 
> \startuseMPgraphic{heading:ThemeTitleStyle}
>   show "HEADING:THEMETITLESTYLE";
>   show \MPstring{heading:title};
> 
>   draw textext( \MPstring{heading:title} ) rotated 5
>     withcolor blue;
> \stopuseMPgraphic
>
> […]
> 
> It appears as though heading:ThemeTitleStyle is being called twice:

If I'm not mistaken that's the way MP works when text is involved,
it's a two-pass approach. In graphics without text MP has all
information it needs to create the image. The text in MP graphics is
typeset using TeX. After it's typeset MP picks it up in a second
run.

> I am trying to produce a random number that is not equal to the
> previous random number. However, if the code is called twice, then I'd
> have to add code to ignore one of the calls.

Maintain a vector with the last two elements to check against.

Marco

[-- Attachment #1.2: Digital signature --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
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] 7+ messages in thread

* Re: Code Called Twice?
  2013-10-11  6:21 Code Called Twice? Thangalin
  2013-10-11  7:27 ` Marco Patzer
@ 2013-10-11 11:00 ` Philipp Gesang
  2013-10-11 21:29   ` Hans Hagen
  1 sibling, 1 reply; 7+ messages in thread
From: Philipp Gesang @ 2013-10-11 11:00 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 1873 bytes --]

···<date: 2013-10-10, Thursday>···<from: Thangalin>···

> 
> I noticed something unexpected with the following code:
> 
> \enabletrackers[metapost.showlog]
> 
> \startuseMPgraphic{heading:ThemeTitleStyle}
>   show "HEADING:THEMETITLESTYLE";
>   show \MPstring{heading:title};
> 
>   draw textext( \MPstring{heading:title} ) rotated 5
>     withcolor blue;
> \stopuseMPgraphic
> 
> \defineframed[ThemeTitleStyle][
>   background=\useMPgraphic{ThemeTitleStyle},
> ]
> 
> \def\processword#1{%
>   \setMPtext{heading:title}{#1}%
>   \useMPgraphic{heading:ThemeTitleStyle}
> }
> 
> \define[1]\ThemeChapterTitle{\applytosplitstringword\processword{#1}}
> \setuphead[chapter][deeptextcommand={\ThemeChapterTitle}]
> 
> \starttext
>   \startchapter[title=Why Is heading:ThemeTitleStyle Called Twice?]
>   \input knuth
>   \stopchapter
> \stoptext
> 
> It appears as though heading:ThemeTitleStyle is being called twice:
> 
> metapost        > info:
> >> "HEADING:THEMETITLESTYLE"
> >> "Why"
> metapost        > info:
> >> "HEADING:THEMETITLESTYLE"
> >> "Why"
> 
> I am trying to produce a random number that is not equal to the
> previous random number. However, if the code is called twice, then I'd
> have to add code to ignore one of the calls.
> 
> Any ideas why the code seems to be called twice and what can be done
> to ensure that it is only called once?

Don’t use text ;-) That’s a known issue: as soon as your Metapost
code contains text, it will be processed twice. E.g.:


  \starttext
    \setMPtext{heading:title}{foo}
    \startMPcode
      show "This gets printed twice.";
      draw textext( \MPstring{heading:title} );
    \stopMPcode
  \stoptext

Same for labels, {b,e}tex. See also this thread:

  http://www.ntg.nl/pipermail/ntg-context/2011/056047.html

Best regards,
Philipp


[-- Attachment #1.2: Type: application/pgp-signature, Size: 490 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
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] 7+ messages in thread

* Re: Code Called Twice?
  2013-10-11  7:27 ` Marco Patzer
@ 2013-10-11 16:39   ` Thangalin
  0 siblings, 0 replies; 7+ messages in thread
From: Thangalin @ 2013-10-11 16:39 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi,

> typeset using TeX. After it's typeset MP picks it up in a second
> run.

That also explains why the spacing between words is correct (I didn't
think MP could typeset text).

> Maintain a vector with the last two elements to check against.

An interesting approach; I used a Boolean flag as per this thread:

http://www.ntg.nl/pipermail/ntg-context/2011/056078.html

Works as expected.

Thank you for the all help!
___________________________________________________________________________________
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] 7+ messages in thread

* Re: Code Called Twice?
  2013-10-11 11:00 ` Philipp Gesang
@ 2013-10-11 21:29   ` Hans Hagen
  2013-10-12  0:09     ` Philipp Gesang
  0 siblings, 1 reply; 7+ messages in thread
From: Hans Hagen @ 2013-10-11 21:29 UTC (permalink / raw)
  To: ntg-context

On 10/11/2013 1:00 PM, Philipp Gesang wrote:
> ···<date: 2013-10-10, Thursday>···<from: Thangalin>···
>
>>
>> I noticed something unexpected with the following code:
>>
>> \enabletrackers[metapost.showlog]
>>
>> \startuseMPgraphic{heading:ThemeTitleStyle}
>>    show "HEADING:THEMETITLESTYLE";
>>    show \MPstring{heading:title};
>>
>>    draw textext( \MPstring{heading:title} ) rotated 5
>>      withcolor blue;
>> \stopuseMPgraphic
>>
>> \defineframed[ThemeTitleStyle][
>>    background=\useMPgraphic{ThemeTitleStyle},
>> ]
>>
>> \def\processword#1{%
>>    \setMPtext{heading:title}{#1}%
>>    \useMPgraphic{heading:ThemeTitleStyle}
>> }
>>
>> \define[1]\ThemeChapterTitle{\applytosplitstringword\processword{#1}}
>> \setuphead[chapter][deeptextcommand={\ThemeChapterTitle}]
>>
>> \starttext
>>    \startchapter[title=Why Is heading:ThemeTitleStyle Called Twice?]
>>    \input knuth
>>    \stopchapter
>> \stoptext
>>
>> It appears as though heading:ThemeTitleStyle is being called twice:
>>
>> metapost        > info:
>>>> "HEADING:THEMETITLESTYLE"
>>>> "Why"
>> metapost        > info:
>>>> "HEADING:THEMETITLESTYLE"
>>>> "Why"
>>
>> I am trying to produce a random number that is not equal to the
>> previous random number. However, if the code is called twice, then I'd
>> have to add code to ignore one of the calls.
>>
>> Any ideas why the code seems to be called twice and what can be done
>> to ensure that it is only called once?
>
> Don’t use text ;-) That’s a known issue: as soon as your Metapost
> code contains text, it will be processed twice. E.g.:
>
>
>    \starttext
>      \setMPtext{heading:title}{foo}
>      \startMPcode
>        show "This gets printed twice.";
>        draw textext( \MPstring{heading:title} );
>      \stopMPcode
>    \stoptext
>
> Same for labels, {b,e}tex. See also this thread:
>
>    http://www.ntg.nl/pipermail/ntg-context/2011/056047.html

you can check for it:

\enabletrackers[metapost.showlog]

and in the mp code:

message(if mfun_first_run : "first run" else : "final run" fi) ;
message(if mfun_trial_run : "trial run" else : "final run" fi) ;

Hans

-----------------------------------------------------------------
                                           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] 7+ messages in thread

* Re: Code Called Twice?
  2013-10-11 21:29   ` Hans Hagen
@ 2013-10-12  0:09     ` Philipp Gesang
  2013-10-12  0:16       ` Hans Hagen
  0 siblings, 1 reply; 7+ messages in thread
From: Philipp Gesang @ 2013-10-12  0:09 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 3275 bytes --]

···<date: 2013-10-11, Friday>···<from: Hans Hagen>···

> On 10/11/2013 1:00 PM, Philipp Gesang wrote:
> > ···<date: 2013-10-10, Thursday>···<from: Thangalin>···
> >
> >>
> >> I noticed something unexpected with the following code:
> >>
> >> \enabletrackers[metapost.showlog]
> >>
> >> \startuseMPgraphic{heading:ThemeTitleStyle}
> >>    show "HEADING:THEMETITLESTYLE";
> >>    show \MPstring{heading:title};
> >>
> >>    draw textext( \MPstring{heading:title} ) rotated 5
> >>      withcolor blue;
> >> \stopuseMPgraphic
> >>
> >> \defineframed[ThemeTitleStyle][
> >>    background=\useMPgraphic{ThemeTitleStyle},
> >> ]
> >>
> >> \def\processword#1{%
> >>    \setMPtext{heading:title}{#1}%
> >>    \useMPgraphic{heading:ThemeTitleStyle}
> >> }
> >>
> >> \define[1]\ThemeChapterTitle{\applytosplitstringword\processword{#1}}
> >> \setuphead[chapter][deeptextcommand={\ThemeChapterTitle}]
> >>
> >> \starttext
> >>    \startchapter[title=Why Is heading:ThemeTitleStyle Called Twice?]
> >>    \input knuth
> >>    \stopchapter
> >> \stoptext
> >>
> >> It appears as though heading:ThemeTitleStyle is being called twice:
> >>
> >> metapost        > info:
> >>>> "HEADING:THEMETITLESTYLE"
> >>>> "Why"
> >> metapost        > info:
> >>>> "HEADING:THEMETITLESTYLE"
> >>>> "Why"
> >>
> >> I am trying to produce a random number that is not equal to the
> >> previous random number. However, if the code is called twice, then I'd
> >> have to add code to ignore one of the calls.
> >>
> >> Any ideas why the code seems to be called twice and what can be done
> >> to ensure that it is only called once?
> >
> > Don’t use text ;-) That’s a known issue: as soon as your Metapost
> > code contains text, it will be processed twice. E.g.:
> >
> >
> >    \starttext
> >      \setMPtext{heading:title}{foo}
> >      \startMPcode
> >        show "This gets printed twice.";
> >        draw textext( \MPstring{heading:title} );
> >      \stopMPcode
> >    \stoptext
> >
> > Same for labels, {b,e}tex. See also this thread:
> >
> >    http://www.ntg.nl/pipermail/ntg-context/2011/056047.html
> 
> you can check for it:
> 
> \enabletrackers[metapost.showlog]
> 
> and in the mp code:
> 
> message(if mfun_first_run : "first run" else : "final run" fi) ;
> message(if mfun_trial_run : "trial run" else : "final run" fi) ;
> 
> Hans

That’s handy! I added a section on dealing with multi-pass to the
“gotchas” page:

    http://wiki.contextgarden.net/Unexpected_behavior#Multipass

There is a subsection about MP at the end.

Btw. the behavior of mfun_trial_run is not very intuitive. I’d
expect it to work like \iftrialtypesetting, but in a single-pass
context it evaluates to true:

    \enabletrackers[metapost.showlog]

    \starttext
      \startMPcode
        if mfun_trial_run :
          show "This should be printed during the trial pass.";
        else :
          show "This should be printed during the final pass.";
        fi;
        fill unitcircle scaled 1cm withcolor blue;
        %label (btex Some label text etex, (0,0));
      \stopMPcode
    \stoptext

Uncommenting the label yields two passes, as expected.

Best regards,
Philipp


[-- Attachment #1.2: Type: application/pgp-signature, Size: 490 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
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] 7+ messages in thread

* Re: Code Called Twice?
  2013-10-12  0:09     ` Philipp Gesang
@ 2013-10-12  0:16       ` Hans Hagen
  0 siblings, 0 replies; 7+ messages in thread
From: Hans Hagen @ 2013-10-12  0:16 UTC (permalink / raw)
  To: ntg-context

On 10/12/2013 2:09 AM, Philipp Gesang wrote:

> Btw. the behavior of mfun_trial_run is not very intuitive. I’d
> expect it to work like \iftrialtypesetting, but in a single-pass
> context it evaluates to true:

keep in mind that one really needs to process the text in order to get 
the dimensions right ... so trialtypesetting is something else

so, the trial run flag has to be used with care

also, texts are cached, so if you typeset for instance labels, 500 times 
a '1' or '2' will only be typeset once (over the years lots of small 
optimizations took place)

Hans


-----------------------------------------------------------------
                                           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] 7+ messages in thread

end of thread, other threads:[~2013-10-12  0:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-10-11  6:21 Code Called Twice? Thangalin
2013-10-11  7:27 ` Marco Patzer
2013-10-11 16:39   ` Thangalin
2013-10-11 11:00 ` Philipp Gesang
2013-10-11 21:29   ` Hans Hagen
2013-10-12  0:09     ` Philipp Gesang
2013-10-12  0:16       ` Hans Hagen

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