diff options
Diffstat (limited to 'Master/texmf-dist/tex/context/base/m-graph.mkiv')
-rw-r--r-- | Master/texmf-dist/tex/context/base/m-graph.mkiv | 153 |
1 files changed, 73 insertions, 80 deletions
diff --git a/Master/texmf-dist/tex/context/base/m-graph.mkiv b/Master/texmf-dist/tex/context/base/m-graph.mkiv index db72927fbb4..25933d9ebae 100644 --- a/Master/texmf-dist/tex/context/base/m-graph.mkiv +++ b/Master/texmf-dist/tex/context/base/m-graph.mkiv @@ -15,106 +15,99 @@ % are limited by what mp can do. We support @ as replacement for % the percent symbol. We also add a specifier when no one is given. +\unprotect + \startluacode - local function strip(s) - return "\\times10^{"..(s:gsub("%+*0*","")).."}" + local format, gsub, find, match = string.format, string.gsub, string.find, string.match + + local simplify = true + + local function strip(n,e) + -- get rid of e(0) + -- get rid of e(+*) + e = gsub(e,"^+","") + -- remove leading zeros + e = gsub(e,"^([+-]*)0+(%d)","%1%2") + if not simplify then + -- take it as it is + elseif n == "1" then + return format("10^{%s}",e) + end + return format("%s\\times10^{%s}",n,e) end + function metapost.format_n(fmt,str) - fmt = fmt:gsub("@","%%") - local initial, format, final = fmt:match("^(.-)(%%.-[%a])(.-)$") - if format then - str = fmt:format(str) - str = str:gsub("e(.-)$",strip) - str = ("%s\\mathematics{%s}%s"):format(initial,str,final) - elseif not fmt:find("%%") then - str = ("%"..fmt):format(str) - str = str:gsub("e(.-)$",strip) - str = ("\\mathematics{%s}"):format(str) + fmt = gsub(fmt,"@","%%") + local initial, hasformat, final = match(fmt,"^(.-)(%%.-[%a])(.-)$") + if hasformat then + str = format(fmt,str) + str = gsub(str,"(.-)e(.-)$",strip) + str = format("%s\\mathematics{%s}%s",initial,str,final) + elseif not find(fmt,"%%") then + str = format("%"..fmt,str) + str = gsub(str,"(.-)e(.-)$",strip) + str = format("\\mathematics{%s}",str) end context(str) end \stopluacode -\unexpanded\long\def\MPgraphformat#1#2{\ctxlua{metapost.format_n("#1","#2")}} +\unexpanded\def\MPgraphformat#1#2{\ctxlua{metapost.format_n("#1","#2")}} % We could also delegate parsing using lower level plugins. -\startMPinclusions - % input string ; - % input marith ; - input graph.mp ; - - vardef roundd(expr x, d) = - if abs d > 4 : - if d > 0 : - x - else : - 0 - fi - elseif d > 0 : - save i ; i = floor x ; - i + round(Ten_to[d]*(x-i))/Ten_to[d] - else : - round(x/Ten_to[-d])*Ten_to[-d] - fi - enddef ; - - Ten_to0 = 1 ; - Ten_to1 = 10 ; - Ten_to2 = 100 ; - Ten_to3 = 1000 ; - Ten_to4 = 10000 ; - - def sFe_base = - enddef ; - - picture Fe_plus ; Fe_plus := btex + etex ; - - vardef format (expr f,x) = dofmt_.Feform_(f,x) enddef ; - vardef Mformat (expr f,x) = dofmt_.Meform (f,x) enddef ; - vardef formatstr (expr f,x) = dofmt_.Feform_(f,x) enddef ; - vardef Mformatstr(expr f,x) = dofmt_.Meform(f,x) enddef ; - - vardef escaped_format(expr s) = - "" for n=1 upto length(s) : & - if ASCII substring (n,n+1) of s = 37 : - "@" - else : - substring (n,n+1) of s - fi - endfor - enddef ; - - vardef dofmt_@\#(expr f, x) = - textext("\MPgraphformat{" & escaped_format(f) & "}{" & (if string x : x else: decimal x fi) & "}") - enddef ; - - % vardef format(expr f, x) = - % textext("\MPgraphformatN{"&escaped_format(f)&"}{"&(if string x : x else: decimal x fi)&"}") - % enddef; - % vardef Mformat(expr f, x) = - % format(f,x) - % enddef; - -\stopMPinclusions +\defineMPinstance + [graph] + [\s!format=metafun, + \s!extensions=\v!yes, + \s!initializations=\v!yes, + \c!method=\s!double] + +\startMPdefinitions{graph} + if unknown context_grap: input "mp-grap.mpiv" ; fi ; +\stopMPdefinitions + +% For backwards compatibility (for the moment), also load the graph macros in +% the standard MP instance (scaled integer): + +\startMPdefinitions + if unknown context_grap: input "mp-grap.mpiv" ; fi ; +\stopMPdefinitions + +\protect \continueifinputfile{m-graph.mkiv} \starttext -\startMPpage -draw begingraph(3in,2in); - gdraw "t:/metapost/grphdata/agepop91.d"; -endgraph; +\startMPpage[instance=graph] + label(format("@g","1e-8"), (0, 0)) ; + label(format("@g","1e+8"), (2cm, 0)) ; + label(format("@g","1e-10"), (0, -0.5cm)) ; + label(format("@g","1e+10"), (2cm,-0.5cm)) ; + label(format("@g","1e-12"), (0, -1.0cm)) ; + label(format("@g","1e+12"), (2cm,-1.0cm)) ; + label(format("@g","1e-0"), (0, -1.5cm)) ; + label(format("@g","1e+0"), (2cm,-1.5cm)) ; + label(format("@g","1"), (0, -2.0cm)) ; + label(format("@g","1"), (2cm,-2.0cm)) ; + label(format("@g","1e-102"),(0, -2.5cm)) ; + label(format("@g","1e+102"),(2cm,-2.5cm)) ; \stopMPpage -% \startMPpage +% \startMPpage[instance=graph] +% draw begingraph(3in,2in); +% gdraw "t:/metapost/grphdata/agepop91.d"; +% endgraph; +% \stopMPpage + +% \startMPpage[instance=graph] % draw begingraph(3in,2in); % gdraw "agepop91.d" plot btex $\bullet$ etex; % endgraph; % \stopMPpage -% \startMPpage +% \startMPpage[instance=graph] % draw begingraph(3in,2in); % glabel.lft(btex \vbox{\hbox{Population} \hbox{in millions}} etex, OUT); % glabel.bot(btex Age in years etex, OUT); @@ -122,7 +115,7 @@ endgraph; % endgraph; % \stopMPpage -% \startMPpage +% \startMPpage[instance=graph] % draw begingraph(3in,2in); % glabel.lft(btex \vbox{\hbox{Population} \hbox{in millions}} etex, OUT); % glabel.bot(btex Age in years etex, OUT); @@ -131,7 +124,7 @@ endgraph; % endgraph; % \stopMPpage -% \startMPpage +% \startMPpage[instance=graph] % draw begingraph(2.3in,2in); % setcoords(log,log); % glabel.lft(btex Seconds etex,OUT); @@ -144,7 +137,7 @@ endgraph; % endgraph; % \stopMPpage -% \startMPpage +% \startMPpage[instance=graph] % draw begingraph(6.5cm,4.5cm); % setrange(80,0, 90,whatever); % glabel.bot(btex Year etex, OUT); |