ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* MetaPost question
@ 2001-11-17 19:54 Jose Luis Diaz
  2001-11-18  6:18 ` Johannes H?sing
  2001-11-18 11:57 ` Denis B. Roegel
  0 siblings, 2 replies; 4+ messages in thread
From: Jose Luis Diaz @ 2001-11-17 19:54 UTC (permalink / raw)


Hello ConTeXt users,

 Sorry  for posing a question not about ConTeXt, but I realize that in
 this  list there also are people very fluent in Metapost. My question
 is:

 How  can I convert a number in a string, concatenate this string with
 other strings and have TeX to typeset the whole result?

 I.e., suppose that I have an array of numbers:

    pair t[];
    t[1]=3; t[2]=20; t[3]=25;

 For example, these numbers are some points along an abcise axis. Then
 I want to put a big dot at each of these points. Not difficult:

  for i=1 upto 3: draw (0u, t[i]*u) withpen circlepen scaled 4mm;
  endfor;

 But  then  suppose  that  I also want a label below each dot, typeset
 with  TeX,  which  reads:  $t_1=3$,  $t_2=20$,  $t_3=25$. I can't use
 btex/etex  because I want to construct the text $t_1=3$ from the loop
 variable, something like:

  for i=1 upto 3:
    sprintf(string,"$f_%d=%d$",i,t[i]);  %This is not Metapost, obviusly
    label.bot(TEX(string), (0u, t[i]*u)); % This is
  endfor;

  How can be done?

  Thanks.

-- 
  Jose Luis Diaz


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

* Re: MetaPost question
  2001-11-17 19:54 MetaPost question Jose Luis Diaz
@ 2001-11-18  6:18 ` Johannes H?sing
  2001-11-18 11:57 ` Denis B. Roegel
  1 sibling, 0 replies; 4+ messages in thread
From: Johannes H?sing @ 2001-11-18  6:18 UTC (permalink / raw)


On Sat, Nov 17, 2001 at 08:54:42PM +0100, Jose Luis Diaz wrote:
>   How can be done?

Can't be done within MetaPost.

Saludos

Johannes
-- 
Johannes Hüsing | "Die einzige deutsche Stadt, die an zwei
hannes@ruhrau.de|  Flüssen liegt." Ole v. Beust über Hamburg


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

* Re: MetaPost question
  2001-11-17 19:54 MetaPost question Jose Luis Diaz
  2001-11-18  6:18 ` Johannes H?sing
@ 2001-11-18 11:57 ` Denis B. Roegel
  2001-11-18 13:17   ` Re[2]: " Jose Luis Diaz
  1 sibling, 1 reply; 4+ messages in thread
From: Denis B. Roegel @ 2001-11-18 11:57 UTC (permalink / raw)
  Cc: ConTeXt Mailing List

On Sat, Nov 17, 2001 at 08:54:42PM +0100, Jose Luis Diaz wrote:
>  But  then  suppose  that  I also want a label below each dot, typeset
>  with  TeX,  which  reads:  $t_1=3$,  $t_2=20$,  $t_3=25$. I can't use
>  btex/etex  because I want to construct the text $t_1=3$ from the loop
>  variable, something like:
> 
>   for i=1 upto 3:
>     sprintf(string,"$f_%d=%d$",i,t[i]);  %This is not Metapost, obviusly
>     label.bot(TEX(string), (0u, t[i]*u)); % This is
>   endfor;
> 
> 
>   How can be done?

Build your string s with something like 

  s="$t_" & decimal(i) & "=" & decimal(t[i]) & "$";

and then write

  label.bot(TEX s,(0,t[i]*u))

and don't forget input TEX at the beginning of your file.

Denis


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

* Re[2]: MetaPost question
  2001-11-18 11:57 ` Denis B. Roegel
@ 2001-11-18 13:17   ` Jose Luis Diaz
  0 siblings, 0 replies; 4+ messages in thread
From: Jose Luis Diaz @ 2001-11-18 13:17 UTC (permalink / raw)
  Cc: ConTeXt Mailing List

Hi Denis,

Denis> Build your string s with something like
Denis>   s="$t_" & decimal(i) & "=" & decimal(t[i]) & "$";

Aha!  I didn't know the decimal() operator, that is what I was looking
for. Thank you very much.

Denis> and then write
Denis>   label.bot(TEX s,(0,t[i]*u))
Denis> and don't forget input TEX at the beginning of your file.

This works fine. However it has several drawbacks:

 1)  It doesn't work if I want to use LaTeX macros or packages for the
 typeset  material. I managed to work around this with a macro similar
 to TEX, as follows:

  vardef LATEX primary s =
    write "verbatimtex" to "mptextmp.mp";
    write "\documentclass[10pt]{book}" to "mptextmp.mp";
    %
    % write the required \usepackage here
    %
    write "\begin{document}" to "mptextmp.mp";
    write "etex" to "mptextmp.mp";
    write "btex "&s&" etex" to "mptextmp.mp";
    write EOF to "mptextmp.mp";
    scantokens "input mptextmp"
  enddef;

 2)  The  process for typesetting the labels is very slow. It requires
 the  generation  and  processing  of an independent metapost file for
 each  label.  And  these  can't be reutilized for subsequent metapost
 runnings (since the same .mpx is overwritten for each label).

 3)  The  results given by my macro LATEX or by the metapost macro TEX
 are  typeset,  in  general,  in a different font than the rest of the
 metapost  document. For example, assume that in my main metapost file
 I  include  some  verbatimtex for changing the default fonts. I would
 have to repeat this code inside the TEX/LATEX macro for achieving the
 same fonts in the labels generated via these macros.

So I would like to avoid is possible the methods using the TEX macros.
I  was  studying  the  source  code  of  "format.mp", which is used by
mpgraph  for  typesetting the labels for the plots. The method used by
"format.mp"  does  not  require  the  TEX  macro,  and  then  it  uses
consistently  the  same  typefaces  among  all  the figures, and it is
processed  faster.  However it is very tricky and not very general. It
seems  to  be  targeted  explicitly for writing numbers in scientific
notation.

I  was  able  to  adapt  some  of  the  code  that  format.mp used for
typesetting   the   exponents  of  the  scientific  notation,  for  my
particular  case  of typesetting subindexes. This is the code, just in
case someone could be interested:

%-------------------------------------------------------
input format;  % For having the macro Fline_up_

vardef subindexed(expr t, i) =
% t is the text to be subindexed
% i is an integer number which will appear in the subindex
  save scalesub, lowersub,fontsub, e, p;
  numeric lowersub, scalesub; picture e, p; string fontsub;

  e:=btex ${}_1$ etex; % Build a "sample" subindex
  for p within e:      % for looking up some parameters
    fontsub := fontpart p;         % look up the font
    scalesub := xxpart p;          % the scale
    lowersub := ypart llcorner p;  % and the baseline
    exitif true;
  endfor;
  % Create a picture with the text for our subindex
  p:=nullpicture;
  addto  p  also  (decimal  i  infont  fontsub scaled scalesub
                   shifted (0,lowersub));
  % Build the complete text, subindex included
  Fline_up_(t,p)  % This is a macro defined in format.mp which
                  % link together several pictures
enddef;
%-----------------------------------------------------------

Example of use:

  for i=1 upto 10:
    label(subindexed(btex $\beta$ etex, i), (i*1cm, i*.5cm));
  endfor;

This  works as expected, but is very specific and rather clumsy. Well,
I  guess  that there is no a better way of doing this, since "mpgraph"
itself uses this method...

Thanks again for your solution, Denis.

-- 
Saludos,
 Jose                            mailto:jldiaz@telecable.es


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

end of thread, other threads:[~2001-11-18 13:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-17 19:54 MetaPost question Jose Luis Diaz
2001-11-18  6:18 ` Johannes H?sing
2001-11-18 11:57 ` Denis B. Roegel
2001-11-18 13:17   ` Re[2]: " Jose Luis Diaz

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