ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Marco <netuse@lavabit.com>
To: ntg-context@ntg.nl
Subject: Re: Ampersand in Metapost
Date: Fri, 14 May 2010 12:06:21 +0200	[thread overview]
Message-ID: <20100514120621.554d595d@vorbis> (raw)
In-Reply-To: <AANLkTilfuaprWpLsRogqRr1ym-lzNw2R8WruwjuS0D_T@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 4957 bytes --]

On Fri, 14 May 2010 00:37:44 +0200, Mojca Miklavec
<mojca.miklavec.lists@gmail.com> wrote:

> On Thu, May 13, 2010 at 23:45, Marco <netuse@lavabit.com> wrote:
> > On Thu, 13 May 2010 22:58:46 +0200, Hans Hagen <pragma@wxs.nl>
> > wrote:
> >> On 13-5-2010 9:18, Marco wrote:
> >> > Hi,
> >> >
> >> > I have a problem producing an ampersand in metapost. In ConTeXt
> >> > it works as expected. Have a look at the following example.
> >> >
> >> > \starttext
> >> > \&  % OK
> >> > \startMPcode
> >> > label(textext("\&"),origin); % produces error
> >> > \stopMPcode
> >> > \stoptext
> >> >
> >> > The compile time error message is as follows:
> >> >    ! Misplaced alignment tab character&.
> >> >    l.1 \MPLIBsettext{1}{&
> >>
> >> \letterpercent
> > Doesn't work. I suppose you mean \letterampersand. If I change \&
> > into \letterampersand I get exactly the same error as \& produces
> > (BTW: \letterpercent also produces an error, but a different one).
> 
> So:
> - label(textext("\letterbackslash\letterampersand"),origin);
> - label(textext("\letterbackslash\&"),origin);
This both works. Thanks, Mojca.

> This is because the commands are interpreted with TeX before they are
> written to mpgraph.mp. The \letterampersand or \& generates the
> character & alone: you need to preceed it with backslash, so that
> metapost finally knows what to do.
That sound like a double-escape. Intuitionally one would simply use &. I know,
TeX doesn't like this, so we escape it, \&. However MetaPost doesn't like
this, so we add a second escape: \letterbackslash\&. That's OK, as I rarely
need ampersands in my graphics.

> PS: I would say: better use \sometxt which is far more reliable unless
> you have to use textext to do string manipulation.
I don't know exactly which transformations are considered as »string
manipulation«. I've read your MyWay on \sometxt. Brilliant piece of doc. But
I've three questions.

   i) Is it about MkII, MkIV or both?
  ii) Is it still up-to-date?
 iii) Why should I better use \sometxt?

What do you mean by »more reliable«? After having read your MyWay I would say
that textext is more reliable because it can do dynamic text.

I created a testfile to compare textext and \sometxt. The points to compare
were those you mentionend in your MyWay as advantages of \sometxt. The
testfile »t.tex« is attached. I processed it with

ConTeXt  ver: 2010.05.08
luatex, version beta-0.60.1-2010042821

Here are my results:

-There is one obvious reason: speed

    $ for i in `seq 5`; do context -mode=textext t | tail -1; done
    MTXrun | total runtime: 128.008
    MTXrun | total runtime: 37.893
    MTXrun | total runtime: 37.900
    MTXrun | total runtime: 38.391
    MTXrun | total runtime: 38.029
    $ rm t.{log,pdf,tuc}
    $ for i in `seq 5`; do context -mode=sometxt t | tail -1; done
    MTXrun | total runtime: 127.195
    MTXrun | total runtime: 40.131
    MTXrun | total runtime: 41.071
    MTXrun | total runtime: 37.424
    MTXrun | total runtime: 39.121

Average runtime:
    textext  variant: 56s
    \sometxt variant: 57s

That is the same. Maybe my test file is not appropriate to test the speed. I'm
sure you have performed more advanced tests.

-Document-wide definitions are seen
  [...] definitions with arguments will fail to work. 

The first line, a document-wide definition with arguments works in both
versions.

-Problems with expansion
  [...]  as far as I remember math expressions (fractions perhaps) never
  worked as they were supposed to

The second definition is a math expressions with fractions. It seems to work.

-Less characters to escape

Yes, that's the topic of this thread. I don't know, I didn't check. The only
check I performed was the ampersand and it needs the same workaround using
\letterbackslash as textext.

I know, your MyWay is old. Maybe some things are fixed now. But I don't see
many advantages for me using it.

In fact, I have a problem getting random colors working. Take the following
example (taken from my earlier thread).

% This line is needed to get »withcolor« to work?
\chardef\TeXtextcolormode\zerocount

\setupbodyfont [sans,36pt]
\starttext

\startMPcode

numeric r, g, b ;
def ran=
r := uniformdeviate 1 ;
g := uniformdeviate 1 ;
b := uniformdeviate 1 ;
enddef;

% Works as expected
for i=0 downto -3:
  ran;
  label(textext("\bold\colored[r=" & decimal(r) & ",g=" & decimal(g) &
		",b=" & decimal(b) & ",a=1, t=.2]{Test}"), (7cm,-1cm))
    rotatedaround ((7cm,-1cm),i*20);
endfor;

% Produces empty rectangles
for i=0 downto -3:
  ran;
  label(\sometxt{\bold{Test}}, (7cm,-4cm))
    rotatedaround ((7cm,-4cm),i*20)
    withcolor transparent("normal", .2, (r,g,b));
endfor;

\stopMPcode
\stoptext

Maybe there's a mistake in my code. It compiles without error but the
result is incorrect.

Marco

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: t.tex --]
[-- Type: text/x-tex, Size: 1388 bytes --]

\def\oklepaj#1{\left(#1\right)}
\def\t{\dot N(t)=
  A\oklepaj{\exp\oklepaj{-\frac{t-t_0}{τ_{\text{eksp.}}}}-\exp
    \oklepaj{-\frac{t-t_0}{τ_{\text{fast}}}}}+
  B\oklepaj{\exp\oklepaj{-\frac{t-t_0}{τ_{\text{eksp.}}}}-\exp
    \oklepaj{-\frac{t-t_0}{τ_{\text{slow}}}}}}
\def\T{Test}

\starttext
\doifmode{textext}{Mode: textext}
\doifmode{sometxt}{Mode: \letterbackslash sometxt}

\startmode [textext]
  \startMPcode
%   label(textext("$\t$\&"),origin); % doesn't work
    label(textext("$\t$\letterbackslash\&"),origin);
  \stopMPcode
\stopmode
\startmode [sometxt]
  \startMPcode
%   label(\sometxt{$\t$\&},origin); % doesn't work, either
    label(\sometxt{$\t$\letterbackslash\&},origin);
  \stopMPcode
\stopmode

\switchtobodyfont [sans, 36pt] \bf

\startmode [textext]
\startMPcode
for i=0 upto 100:
  label(textext("\colored [b=.7, a=1, t=.3]{\T}"),(i*1mm,0cm))
    rotatedaround ((i*1mm,0cm),i*2);
  label(textext("\colored [g=.7, a=1, t=.2]{\T}"),(i*1.1mm,0cm))
    rotatedaround ((i*1.1mm,0cm),i*2);
endfor;
\stopMPcode
\stopmode

\startmode [sometxt]
\startMPcode
for i=0 upto 100:
  label(\sometxt{\colored [b=.7, a=1, t=.3]{\T}},(i*1mm,0cm))
    rotatedaround ((i*1mm,0cm),i*2);
  label(\sometxt{\colored [g=.7, a=1, t=.2]{\T}},(i*1.1mm,0cm))
    rotatedaround ((i*1.1mm,0cm),i*2);
endfor;
\stopMPcode
\stopmode
\stoptext

[-- Attachment #3: Type: text/plain, Size: 486 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
___________________________________________________________________________________

  parent reply	other threads:[~2010-05-14 10:06 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-13 19:18 Marco
2010-05-13 20:58 ` Hans Hagen
2010-05-13 21:40   ` Mojca Miklavec
2010-05-13 21:45   ` Marco
2010-05-13 22:37     ` Mojca Miklavec
2010-05-14  1:07       ` Aditya Mahajan
2010-05-14 10:09         ` Marco
2010-05-14 13:32         ` Hans Hagen
2010-05-14 10:06       ` Marco [this message]
2010-05-14 10:39         ` Hans Hagen
2010-05-14 10:44         ` Hans Hagen
2010-05-14 11:45           ` Marco
2010-05-14 12:17             ` Hans Hagen
2010-05-14 15:20         ` Mojca Miklavec
2010-05-14 19:34           ` Marco

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100514120621.554d595d@vorbis \
    --to=netuse@lavabit.com \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).