Ampersand in Metapost
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}{& Marco
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 ----------------------------------------------------------------- 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 -----------------------------------------------------------------
On Thu, May 13, 2010 at 22:58, Hans Hagen wrote:
On 13-5-2010 9:18, Marco wrote:
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
Or maybe \letterampersand if you want to get the desired glyph :) Alternatively you could try to use label(\sometxt{\&},origin); Mojca
On Thu, 13 May 2010 22:58:46 +0200, Hans Hagen
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).
I discovered that the ugly \char`\& works as a workaround (or is this the final solution?). Thanks for your help. Marco
On Thu, May 13, 2010 at 23:45, Marco
On Thu, 13 May 2010 22:58:46 +0200, Hans Hagen
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 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. Mojca PS: I would say: better use \sometxt which is far more reliable unless you have to use textext to do string manipulation.
On Fri, 14 May 2010, Mojca Miklavec wrote:
On Thu, May 13, 2010 at 23:45, Marco
wrote: On Thu, 13 May 2010 22:58:46 +0200, Hans Hagen
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 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.
So, should all the \letter... commands (basically anything generated from char-def.lua) be unexpandable? You can also try \noexpand\letterampersand Aditya
- label(textext("\letterbackslash\letterampersand"),origin); - label(textext("\letterbackslash\&"),origin);
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.
So, should all the \letter... commands (basically anything generated from char-def.lua) be unexpandable? What are the disadvantages of unexpandable commands? I'm not an expert. Why is this not done by default if it works better?
You can also try \noexpand\letterampersand Produces compile error.
Marco
On 14-5-2010 3:07, Aditya Mahajan wrote:
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.
So, should all the \letter... commands (basically anything generated from char-def.lua) be unexpandable?
You can also try \noexpand\letterampersand
The problem is that some definitions, likt \& and \# expand to letters which is needed when used in mp itself. That also means that resetting them to their normal meaning is not possible as textext is handled in mp and then already an \& has become a &. As & is rather common, I added it to \nonknuthmode, so you can say: \nonknuthmode \starttext \startMPcode draw textext("Underscores, Superscripts & Ampersands" & "?") ; \stopMPcode \stoptext Aditya: I also changed the definition of \_ in math ... it now looks better when used in script(scripts) .. see \fakeunderscore. 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 -----------------------------------------------------------------
On Fri, 14 May 2010 00:37:44 +0200, Mojca Miklavec
On Thu, May 13, 2010 at 23:45, Marco
wrote: On Thu, 13 May 2010 22:58:46 +0200, Hans Hagen
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
On 14-5-2010 12:06, Marco wrote:
iii) Why should I better use \sometxt?
sometxt was introduced in mkii to avoid text processing mp, i.e. it is done at the context end in the current run and mp only gets dimensions; so, it avoids the nested tex run in mkii there is some more trickery and much showed up whem for in instance Mojca wanted gnuplot to spit out mp
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.
in mkiv textext is implemented differently; because we use mplib, processing time is less an issue anyway
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
in mkiv there should be no big difference as it all boils down to the same (sometxt has a command applied which textext doesn't)
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.
you use mkiv
-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.
th emkii variant has some more limitations than the mkiv version
-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.
don't expect mkii and mkiv to be 100% compatible in this area (mkii is somewhat stretching things to the limit) 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 -----------------------------------------------------------------
On 14-5-2010 12:06, Marco wrote:
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;
because transparent overloads the mechanism that sometxt (textext) uses for passing info to tex and back ... maybe some future version will handle that (just comment the withcolor) 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 -----------------------------------------------------------------
On Fri, 14 May 2010 12:44:47 +0200, Hans Hagen
On 14-5-2010 12:06, Marco wrote:
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;
because transparent overloads the mechanism that sometxt (textext) uses for passing info to tex and back ... maybe some future version will handle that (just comment the withcolor) Ah, I see. I don't need it at the moment as I can use textext with the code you provided.
Thanks for your explanation. I hope I'm not completely mistaken if I conclude, that there are some advantages when using \sometxt in MkII, but in MkIV both variants are basically the same. Marco
On 14-5-2010 1:45, Marco wrote:
On Fri, 14 May 2010 12:44:47 +0200, Hans Hagen
wrote: On 14-5-2010 12:06, Marco wrote:
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;
because transparent overloads the mechanism that sometxt (textext) uses for passing info to tex and back ... maybe some future version will handle that (just comment the withcolor) Ah, I see. I don't need it at the moment as I can use textext with the code you provided.
Thanks for your explanation. I hope I'm not completely mistaken if I conclude, that there are some advantages when using \sometxt in MkII, but in MkIV both variants are basically the same.
indeed ----------------------------------------------------------------- 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 -----------------------------------------------------------------
On Fri, May 14, 2010 at 12:06, Marco wrote:
On Fri, 14 May 2010 00:37:44 +0200, Mojca Miklavec wrote:
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?
It's mostly about MkII. In MKIV it should work, but it doesn't make such a huge difference since textext works as well as \sometxt in MkII. textext in MkII can sometimes be very inefficient.
ii) Is it still up-to-date?
Apart from a recently reported bug I don't know of any changes in MkII, but maybe I should write a few words about MkIV.
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.
Dynamic text counts as "feature" for me, not as "something that'"
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
As already mentioned by Hans, the only difference,
Here are my results:
-There is one obvious reason: speed
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.
For gnuplot-generated graphics compile time has been reduced from 10 minutes to 20 seconds. But that's another story (many graphics, many text labels inside each graphic; textext was not optimized).
-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.
But that's only true with MkIV. In MKII it's another story.
-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.
Many math expressions have later been made unexpandable and started working, but it felt like a neverending story. Almost the same as the example that you were asking for.
I know, your MyWay is old. Maybe some things are fixed now. But I don't see many advantages for me using it.
If you are using MkIV and don't run into the same problem again, there is hardly a difference.
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? Yes, in MkII. In MkIV Hans wanted to take a different approach. \chardef\TeXtextcolormode\zerocount
% 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;
Hans replied. All in all - true, the most difference is seen in MkII. Mojca
On Fri, 14 May 2010 17:20:35 +0200, Mojca Miklavec wrote: [...]
Here are my results:
-There is one obvious reason: speed
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.
For gnuplot-generated graphics compile time has been reduced from 10 minutes to 20 seconds. But that's another story (many graphics, many text labels inside each graphic; textext was not optimized). Wow! That's what I'd call optimization. Now I understand your opinion and your advice to use \sometxt (in MkII).
-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.
But that's only true with MkIV. In MKII it's another story. Don't know. I don't use it.
-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.
Many math expressions have later been made unexpandable and started working, but it felt like a neverending story. Almost the same as the example that you were asking for. I think I can comprehend the problems you had with textext.
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? Yes, in MkII. In MkIV Hans wanted to take a different approach. \chardef\TeXtextcolormode\zerocount
% 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;
Hans replied. Yes. I will use textext for this job.
I know, your MyWay is old. Maybe some things are fixed now. But I don't see many advantages for me using it.
If you are using MkIV and don't run into the same problem again, there is hardly a difference.
I conclude by saying that \sometxt ist first choice if using MkII. In MkIV first choice is textext. Thanks for your reply (and your MyWay), it helped me get some insight into proper use of text in metapost. Marco
participants (4)
-
Aditya Mahajan
-
Hans Hagen
-
Marco
-
Mojca Miklavec