ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Metapost label - Truncating digits after decimal point
@ 2009-11-28 14:56 Curiouslearn
  2009-11-29  8:32 ` Taco Hoekwater
  2009-12-15 10:34 ` Aditya Mahajan
  0 siblings, 2 replies; 5+ messages in thread
From: Curiouslearn @ 2009-11-28 14:56 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi,

Is it possible to truncate the digits after decimal point in Metapost
when using textext()? Please see the minimal example below which
produces 1.84375. Can I just keep the first digit and have it produce
1.8 ?

Thanks.

\setuppapersize[letter][letter]

\setupcolors[state=start]


\starttext

\startuseMPgraphic{Figure}
	u := 1cm;
	pickup pencircle scaled 2pt; % default pen thickness
	%TheFunction
		vardef f(expr x) =
			(10/x)-x*((0.5)**x)
		enddef;
	%Labels	
		label.lft(textext(decimal f(5)),(0,f(5)) scaled u);		
\stopuseMPgraphic
\useMPgraphic{Figure}

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

* Re: Metapost label - Truncating digits after decimal point
  2009-11-28 14:56 Metapost label - Truncating digits after decimal point Curiouslearn
@ 2009-11-29  8:32 ` Taco Hoekwater
  2009-11-29 14:47   ` Curiouslearn
  2009-12-15 10:34 ` Aditya Mahajan
  1 sibling, 1 reply; 5+ messages in thread
From: Taco Hoekwater @ 2009-11-29  8:32 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Curiouslearn wrote:
> Hi,
> 
> Is it possible to truncate the digits after decimal point in Metapost
> when using textext()? Please see the minimal example below which
> produces 1.84375. Can I just keep the first digit and have it produce
> 1.8 ?

Here is a helper macro for you:

def trunc_digits(expr n, origs) =
   hide(
     string s, tmp;
     numeric ii, e;
     e := -1;
     for i=1 upto length(origs):
       ii := i;
       tmp := substring (i-1,i) of origs;
       if tmp = ".": if n=0: ii:=ii-1; exitif true; fi e:=0; fi
       if e>=0: e := e+1; exitif e>n;  fi
     endfor;
     s:=substring(0,ii) of origs; )
   s
enddef;


When run this macro with the result of decimal() as second argument,
it will create a new string as many fractional digits as you specify
in the first argument:

   label.lft(textext(trunc_digits(1,decimal f(5))),(0,f(5)) scaled u);	

Best wishes,
Taco
___________________________________________________________________________________
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] 5+ messages in thread

* Re: Metapost label - Truncating digits after decimal point
  2009-11-29  8:32 ` Taco Hoekwater
@ 2009-11-29 14:47   ` Curiouslearn
  0 siblings, 0 replies; 5+ messages in thread
From: Curiouslearn @ 2009-11-29 14:47 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Thanks so much, Taco. I will try out the code below. Good to know that
Metapost has functions such as substring.




On Sun, Nov 29, 2009 at 3:32 AM, Taco Hoekwater <taco@elvenkind.com> wrote:
> Curiouslearn wrote:
>>
>> Hi,
>>
>> Is it possible to truncate the digits after decimal point in Metapost
>> when using textext()? Please see the minimal example below which
>> produces 1.84375. Can I just keep the first digit and have it produce
>> 1.8 ?
>
> Here is a helper macro for you:
>
> def trunc_digits(expr n, origs) =
>  hide(
>    string s, tmp;
>    numeric ii, e;
>    e := -1;
>    for i=1 upto length(origs):
>      ii := i;
>      tmp := substring (i-1,i) of origs;
>      if tmp = ".": if n=0: ii:=ii-1; exitif true; fi e:=0; fi
>      if e>=0: e := e+1; exitif e>n;  fi
>    endfor;
>    s:=substring(0,ii) of origs; )
>  s
> enddef;
>
>
> When run this macro with the result of decimal() as second argument,
> it will create a new string as many fractional digits as you specify
> in the first argument:
>
>  label.lft(textext(trunc_digits(1,decimal f(5))),(0,f(5)) scaled u);
>
> Best wishes,
> Taco
> ___________________________________________________________________________________
> 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
> ___________________________________________________________________________________
>
___________________________________________________________________________________
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] 5+ messages in thread

* Re: Metapost label - Truncating digits after decimal point
  2009-11-28 14:56 Metapost label - Truncating digits after decimal point Curiouslearn
  2009-11-29  8:32 ` Taco Hoekwater
@ 2009-12-15 10:34 ` Aditya Mahajan
  2009-12-16  3:38   ` Curiouslearn
  1 sibling, 1 reply; 5+ messages in thread
From: Aditya Mahajan @ 2009-12-15 10:34 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Sat, 28 Nov 2009, Curiouslearn wrote:

> Hi,
>
> Is it possible to truncate the digits after decimal point in Metapost
> when using textext()? Please see the minimal example below which
> produces 1.84375. Can I just keep the first digit and have it produce
> 1.8 ?
>
> Thanks.

If you are using mkiv, you can use lua to truncate a number.

\unexpanded\def\truncate#1{\ctxlua{context("\%.1f", #1)}}

\startMPdefinitions
   def truncatedtext(expr s) =
     textext("\truncate{" & s & "}")
   enddef ;
\stopMPdefinitions

\starttext
\startuseMPgraphic{Figure}
   u := 1cm;
   pickup pencircle scaled 2pt; % default pen thickness
   %TheFunction
   vardef f(expr x) =
           (10/x)-x*((0.5)**x)
   enddef;
   %Labels
   %label.lft(textext("\truncate{" & decimal f(5) & "}"),(0,f(5)) scaled u);
   label.lft(truncatedtext(decimal f(5)),(0,f(5)) scaled u);
\stopuseMPgraphic
\useMPgraphic{Figure}


\stoptext

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

* Re: Metapost label - Truncating digits after decimal point
  2009-12-15 10:34 ` Aditya Mahajan
@ 2009-12-16  3:38   ` Curiouslearn
  0 siblings, 0 replies; 5+ messages in thread
From: Curiouslearn @ 2009-12-16  3:38 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Thanks for this solution. Sounds small and clean. I need to learn Lua I guess.



On Tue, Dec 15, 2009 at 5:34 AM, Aditya Mahajan <adityam@umich.edu> wrote:
> On Sat, 28 Nov 2009, Curiouslearn wrote:
>
>> Hi,
>>
>> Is it possible to truncate the digits after decimal point in Metapost
>> when using textext()? Please see the minimal example below which
>> produces 1.84375. Can I just keep the first digit and have it produce
>> 1.8 ?
>>
>> Thanks.
>
> If you are using mkiv, you can use lua to truncate a number.
>
> \unexpanded\def\truncate#1{\ctxlua{context("\%.1f", #1)}}
>
> \startMPdefinitions
>  def truncatedtext(expr s) =
>    textext("\truncate{" & s & "}")
>  enddef ;
> \stopMPdefinitions
>
> \starttext
> \startuseMPgraphic{Figure}
>  u := 1cm;
>  pickup pencircle scaled 2pt; % default pen thickness
>  %TheFunction
>  vardef f(expr x) =
>          (10/x)-x*((0.5)**x)
>  enddef;
>  %Labels
>  %label.lft(textext("\truncate{" & decimal f(5) & "}"),(0,f(5)) scaled u);
>  label.lft(truncatedtext(decimal f(5)),(0,f(5)) scaled u);
> \stopuseMPgraphic
> \useMPgraphic{Figure}
>
>
> \stoptext
>
> Aditya
> ___________________________________________________________________________________
> 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
> ___________________________________________________________________________________
>
___________________________________________________________________________________
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] 5+ messages in thread

end of thread, other threads:[~2009-12-16  3:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-28 14:56 Metapost label - Truncating digits after decimal point Curiouslearn
2009-11-29  8:32 ` Taco Hoekwater
2009-11-29 14:47   ` Curiouslearn
2009-12-15 10:34 ` Aditya Mahajan
2009-12-16  3:38   ` Curiouslearn

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