summaryrefslogtreecommitdiff
path: root/tex/context/base/m-graph.mkiv
blob: 35f487775a8605b5b0db885b8c981ec1328ed0ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
%D \module
%D   [       file=m-graph,
%D        version=2008.09.08,
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=\METAPOST\ graph module support,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% We just assume lua specification instead of the graph ones that
% 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.

\startluacode
    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)
        e = gsub(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 = 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\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;

  % string Autoform_x ; Autoform_x := "@.0e" ;
  % string Autoform_y ; Autoform_y := "@.0e" ;

    vardef autogrid(suffix tx, ty) text w =
        Gneedgr_ := false ;
        if str tx <> "" :
            for x=auto.x :
                tx(if known Autoform_x : Autoform_x else : Autoform fi,x) w ;
            endfor
        fi ;
        if str ty <> "" :
            for y=auto.y :
                ty(if known Autoform_y : Autoform_y else : Autoform fi,y) w ;
            endfor
        fi ;
    enddef ;

\stopMPinclusions

\continueifinputfile{m-graph.mkiv}

\starttext

\startMPpage
    label(format("@g","1e-10"), (0,      0)) ;
    label(format("@g","1e+10"), (2cm,    0)) ;
    label(format("@g","1e-12"), (0,  -0.5cm)) ;
    label(format("@g","1e+12"), (2cm,-0.5cm)) ;
    label(format("@g","1e-0"),  (0,  -1.0cm)) ;
    label(format("@g","1e+0"),  (2cm,-1.0cm)) ;
    label(format("@g","1"),     (0,  -1.5cm)) ;
    label(format("@g","1"),     (2cm,-1.5cm)) ;
    label(format("@g","1e-102"),(0,  -2.0cm)) ;
    label(format("@g","1e+102"),(2cm,-2.0cm)) ;
\stopMPpage

% \startMPpage
% draw begingraph(3in,2in);
%     gdraw "t:/metapost/grphdata/agepop91.d";
% endgraph;
% \stopMPpage

% \startMPpage
% draw begingraph(3in,2in);
%     gdraw "agepop91.d" plot btex $\bullet$ etex;
% endgraph;
% \stopMPpage

% \startMPpage
% draw begingraph(3in,2in);
%     glabel.lft(btex \vbox{\hbox{Population} \hbox{in millions}} etex, OUT);
%     glabel.bot(btex Age in years etex, OUT);
%     gdraw "agepopm.d";
% endgraph;
% \stopMPpage

% \startMPpage
% draw begingraph(3in,2in);
%     glabel.lft(btex \vbox{\hbox{Population} \hbox{in millions}} etex, OUT);
%     glabel.bot(btex Age in years etex, OUT);
%     setrange(origin, whatever,whatever);
%     gdraw "agepopm.d";
% endgraph;
% \stopMPpage

% \startMPpage
% draw begingraph(2.3in,2in);
%     setcoords(log,log);
%     glabel.lft(btex Seconds etex,OUT);
%     glabel.bot(btex Matrix size etex,
%     OUT);
%     gdraw "matmul.d" dashed evenly;
%     glabel.ulft(btex Standard etex,8);
%     gdraw "matmul.d";
%     glabel.lrt(btex Strassen etex,7);
% endgraph;
% \stopMPpage

% \startMPpage
% draw begingraph(6.5cm,4.5cm);
%     setrange(80,0, 90,whatever);
%     glabel.bot(btex Year etex, OUT);
%     glabel.lft(btex \vbox{\hbox{Emissions in} \hbox{thousands of}
%     \hbox{metric tons} \hbox{(heavy line)}}etex, OUT);
%     gdraw "lead.d" withpen pencircle scaled 1.5pt;
%     autogrid(,otick.lft);
%     setcoords(linear,linear);
%     setrange(80,0, 90,whatever);
%     glabel.rt(btex \vbox{\hbox{Micrograms} \hbox{per cubic}
%     \hbox{meter of air} \hbox{(thin line)}}etex, OUT);
%     gdraw "lead.d";
%     autogrid(otick.bot,otick.rt);
% endgraph;
% \stopMPpage

\stoptext