Subversion Repositories wimsdev

Rev

Rev 11125 | Rev 12237 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 reyssat 1
/*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
2
 *
3
 *  This program is free software; you can redistribute it and/or modify
4
 *  it under the terms of the GNU General Public License as published by
5
 *  the Free Software Foundation; either version 2 of the License, or
6
 *  (at your option) any later version.
7
 *
8
 *  This program is distributed in the hope that it will be useful,
9
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 *  GNU General Public License for more details.
12
 *
13
 *  You should have received a copy of the GNU General Public License
14
 *  along with this program; if not, write to the Free Software
15
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
 */
5502 bpr 17
/* subroutines for texmath */
8185 bpr 18
#include "wims.h"
10 reyssat 19
 
8185 bpr 20
/* Careful! increasing this number risks stack overflow. */
10 reyssat 21
#define MAX_FACTORS 256
22
 
23
enum {
24
    type_integer, type_numeric, type_var,
25
      type_poly, type_transcend
26
};
27
 
28
typedef struct afactor{
29
    char *beg, *end;
30
    int type, side;
31
} afactor;
32
 
33
char texmathbuf[MAX_LINELEN+1];
34
char *find_term_end(char *p);
35
void t_onestring(char *p);
36
void t_oneterm(char *p, int num);
37
void t_onefactor(struct afactor *p, int num);
5516 bpr 38
/* never used or defined ?? */
10 reyssat 39
void n_onestring(char *p);
40
void n_oneterm(char *p, int num);
41
void n_onefactor(struct afactor *p, int num);
5516 bpr 42
/* */
10 reyssat 43
void texmath(char *p);
44
 
5502 bpr 45
/* print to texmathbuf */
10 reyssat 46
void tprint(char *s,...)
47
{
48
    va_list vp;
49
    char buf[MAX_LINELEN+1];
50
 
51
    va_start(vp,s); vsnprintf(buf,sizeof(buf),s,vp); va_end(vp);
7673 bpr 52
    if(strlen(buf)+strlen(texmathbuf)>=MAX_LINELEN)
10 reyssat 53
      user_error("cmd_output_too_long");
54
    strcat(texmathbuf,buf);
55
}
5458 bpr 56
/* transforms sum(n,n=1..infinity) , product(n/(n+1),n=1..infinity). */
10 reyssat 57
void _tex_sums(char *p, char *name, int type)
58
{
59
    char *p1,*p2,*p3;
60
    p1=find_item_end(p); if(*p1) *(p1++)=0;
61
    p2=find_item_end(p1); p3=strparstr(p1,"=");
11125 georgesk 62
    if(p3<p2) p2=p3;
63
    if(*p2) *(p2++)=0;
10 reyssat 64
    p3=find_item_end(p2);
65
    if(*p3) *(p3++)=0;
66
    tprint("\\%s ",name);
67
    if(type) {
5502 bpr 68
      if(*p2) {
69
        tprint("_{"); t_onestring(p2); tprint("}");
70
      }
10 reyssat 71
    }
72
    else if(*p1) {
5502 bpr 73
      tprint("_{%s",p1);
74
      if(*p2) { tprint("="); t_onestring(p2); }
75
      tprint("}");
10 reyssat 76
    }
77
    if(*p3) {
5502 bpr 78
      tprint("^{"); t_onestring(p3); tprint("}");
10 reyssat 79
    }
80
    strip_trailing_spaces(p); if(find_term_end(p)<p+strlen(p)) {
5502 bpr 81
      tprint("\\left("); t_onestring(p); tprint("\\right)");
10 reyssat 82
    }
83
    else t_onestring(p);
84
    if(type && *p1) {
5502 bpr 85
      strip_trailing_spaces(p1); tprint("d"); // case of the integrale
86
      if(find_term_end(p1)<p1+strlen(p1)) {
87
        tprint("\\left("); t_onestring(p1); tprint("\\right)");
88
      }
89
      else t_onestring(p1);
10 reyssat 90
    }
91
}
92
 
5502 bpr 93
/* integration, sum and product */
10 reyssat 94
void tex_int(char *p) {  _tex_sums(p,"int",1); }
95
void tex_sum(char *p) {  _tex_sums(p,"sum",0); }
96
void tex_prod(char *p) { _tex_sums(p,"prod",0);}
97
 
8185 bpr 98
struct tmathfn tmathfn[]={
5502 bpr 99
      {"Arg",      1,  "\\rm{Arg}",      "\\right)"},
100
      {"Int",      2,  "","",        tex_int},
101
      {"Prod",     2,  "","",        tex_prod},
102
      {"Sum",      2,  "","",        tex_sum},
103
      {"abs",      0,  "\\left|",      "\\right|"},
104
      {"acos",     1,  "\\rm{arccos}",  "\\right)"},
105
      {"acosh",    1,  "\\rm{Argch}",      "\\right)"},
106
      {"arg",      1,  "\\rm{Arg}",      "\\right)"},
107
      {"asin",     1,  "\\rm{arcsin}",  "\\right)"},
108
      {"asinh",    1,  "\\rm{Argsh}",      "\\right)"},
109
      {"atan",     1,  "\\rm{arctg}",      "\\right)"},
110
      {"atanh",    1,  "\\rm{Argth}",      "\\right)"},
111
      {"ch",       1,  "\\rm{ch}",      "\\right)"},
112
      {"conj",     0,  "\\overline{",      "}"},
113
      {"conjugate",0,  "\\overline{",      "}"},
114
      {"cos",      1,  "\\cos",      "\\right)"},
115
      {"cosh",     1,  "\\rm{ch}",      "\\right)"},
116
      {"cot",      1,  "\\rm{ctg}",      "\\right)"},
117
      {"cotan",    1,  "\\rm{ctg}",      "\\right)"},
118
      {"cotanh",   1,  "\\rm{cth}",      "\\right)"},
119
      {"csc",      1,  "\\rm{csc}",      "\\right)"},
120
      {"ctg",      1,  "\\rm{ctg}",      "\\right)"},
121
      {"cth",      1,  "\\rm{cth}",      "\\right)"},
122
      {"det",      1,  "\\rm{det}",      "\\right)"},
123
      {"erf",      1,  "\\rm{erf}",      "\\right)"},
124
      {"exp",      1,  "\\exp",      "\\right)"},
125
      {"int",      2,  "","",        tex_int},
126
      {"integrate",2,  "","",        tex_int},
127
      {"lg",       1,  "\\rm{lg}",      "\\right)"},
128
      {"ln",       1,  "\\ln",        "\\right)"},
129
      {"log",      1,  "\\log",      "\\right)"},
130
      {"prod",     2,  "","",        tex_prod},
131
      {"product",  2,  "","",        tex_prod},
132
      {"sec",      1,  "\\rm{sec}",      "\\right)"},
133
      {"sgn",      1,  "\\rm{sgn}",      "\\right)"},
134
      {"sh",       1,  "\\rm{sh}",      "\\right)"},
135
      {"sign",     1,  "\\rm{sign}",      "\\right)"},
136
      {"sin",      1,  "\\sin",      "\\right)"},
137
      {"sinh",     1,  "\\rm{sh}",      "\\right)"},
138
      {"sqrt",     0,  "\\sqrt{",      "}"},
139
      {"sum",      2,  "","",        tex_sum},
140
      {"tan",      1,  "\\tan",      "\\right)"},
141
      {"tanh",     1,  "\\rm{th}",      "\\right)"},
142
      {"tg",       1,  "\\rm{tg}",      "\\right)"},
143
      {"th",       1,  "\\rm{th}",      "\\right)"}
10 reyssat 144
};
145
 
8185 bpr 146
int tmathfn_no=(sizeof(tmathfn)/sizeof(tmathfn[0]));
10 reyssat 147
 
8185 bpr 148
struct tmathvar tmathvar[]={
5502 bpr 149
      {"CC",      "\\mathbb{C}"},
150
      {"Delta",   "\\Delta"},
151
      {"Gamma",   "\\Gamma"},
152
      {"Inf",     "\\infty"},
153
      {"Lambda",  "\\Lambda"},
154
      {"NN",      "\\mathbb{N}"},
155
      {"Omega",   "\\Omega"},
156
      {"PI",      "\\pi"},
157
      {"Phi",     "\\Phi"},
158
      {"Pi",      "\\Pi"},
159
      {"Psi",     "\\Psi"},
160
      {"QQ",      "\\mathbb{Q}"},
161
      {"RR",      "\\mathbb{R}"},
162
      {"Sigma",   "\\Sigma"},
163
      {"Theta",   "\\Theta"},
164
      {"Upsilon", "\\Upsilon"},
165
      {"Xi",      "\\Xi"},
166
      {"ZZ",      "\\mathbb{Z}"},
167
      {"aleph",   "\\aleph"},
168
      {"alpha",   "\\alpha"},
169
      {"beta",   "\\beta"},
170
      {"chi",    "\\chi"},
171
      {"delta",  "\\delta"},
172
      {"epsilon","\\epsilon"},
173
      {"eta",    "\\eta"},
174
      {"gamma",  "\\gamma"},
175
      {"inf",    "\\infty"},
176
      {"infinity","\\infty"},
177
      {"infty",  "\\infty"},
178
      {"iota",   "\\iota"},
179
      {"kappa",  "\\kappa"},
180
      {"lambda", "\\lambda"},
181
      {"mu",     "\\mu"},
182
      {"neq",    "\\neq"},
183
      {"nu",     "\\nu"},
184
      {"omega",  "\\omega"},
185
      {"phi",    "\\phi"},
186
      {"pi",     "\\pi"},
187
      {"psi",    "\\psi"},
188
      {"rho",    "\\rho"},
189
      {"sigma",  "\\sigma"},
190
      {"tau",    "\\tau"},
191
      {"theta",  "\\theta"},
192
      {"xi",     "\\xi"},
193
      {"zeta",   "\\zeta"}
10 reyssat 194
};
195
 
8185 bpr 196
int tmathvar_no=(sizeof(tmathvar)/sizeof(tmathvar[0]));
10 reyssat 197
 
198
 
5502 bpr 199
/* find the end of an additive term. */
10 reyssat 200
char *find_term_end(char *p)
201
{
202
    char *pp;
7673 bpr 203
    pp=p;
10 reyssat 204
    if(*pp==',' || *pp==';' || *pp=='=' || *pp=='<') pp++;
205
    while(*pp=='+' || *pp=='-' || *pp=='=' || *pp=='>') pp++;
206
    for(;*pp;pp++) {
5502 bpr 207
      switch(*pp) {
208
        case '(': pp=find_matching(pp+1,')'); goto loopend;
209
        case '[': pp=find_matching(pp+1,']'); goto loopend;
210
/*     case '{': pp=find_matching(pp+1,'}'); goto loopend;*/
7673 bpr 211
 
5502 bpr 212
        case 0:
213
        case '<':
214
        case '>':
215
        case ',':
216
        case ';':
217
        case '=':
218
        case ')':
219
        case ']':
220
        case '}':
221
        case '-':
222
        case '+': return pp;
223
 
224
        case '*':
225
        case '/':
226
        case '^': {
227
          while(*(pp+1)=='+' || *(pp+1)=='-') pp++;
228
          goto loopend;
229
        }
230
      }
231
      if(isalnum(*pp) || *pp=='.') {
232
        pp=find_mathvar_end(pp); pp--; continue;
233
      }
234
      continue;
235
      loopend:
236
      if(pp==NULL) module_error("unmatched_parentheses");
10 reyssat 237
    }
238
    return pp;
239
}
240
 
5502 bpr 241
/* find the end of an multiplicative factor. */
10 reyssat 242
char *find_factor_end(char *p)
243
{
244
    char *pp;
245
    pp=p; if(*pp==',' || *pp==';' || *pp=='=') pp++;
246
    while(*pp=='+' || *pp=='-' || *pp=='*' || *pp=='/') pp++;
247
    for(;*pp;pp++) {
5502 bpr 248
      switch(*pp) {
249
        case '(': pp=find_matching(pp+1,')'); goto loopend;
250
        case '[': pp=find_matching(pp+1,']'); goto loopend;
251
/*    case '{': pp=find_matching(pp+1,'}'); goto loopend;*/
7673 bpr 252
 
5502 bpr 253
        case 0:
254
        case '<':
255
        case '>':
256
        case ',':
257
        case ';':
258
        case '=':
259
        case ')':
260
        case ']':
261
        case '}':
262
        case '+':
263
        case '-':
264
        case '*':
265
        case '/': return pp;
7673 bpr 266
 
5502 bpr 267
        case '^': {
268
          while(*(pp+1)=='+' || *(pp+1)=='-') pp++;
269
          goto loopend;
270
        }
271
      }
272
      if(isalnum(*pp) || *pp=='.') {
273
        pp=find_mathvar_end(pp); pp--; continue;
274
      }
275
      continue;
276
      loopend:
277
      if(pp==NULL) module_error("unmatched_parentheses");
10 reyssat 278
    }
279
    return pp;
280
}
281
 
5502 bpr 282
    /* returns the number of terms */
10 reyssat 283
int term_cnt(char *p)
284
{
285
    char *pe, *pp;
286
    int i;
7673 bpr 287
 
10 reyssat 288
    pe=p+strlen(p);
289
    for(i=0,pp=p;pp<pe;pp=find_term_end(pp),i++);
290
    return i;
291
}
292
 
5460 bpr 293
/* Print a number: transform 4E+05 in 4 \times 10^{5} and  4E-05 in 4 \times 10^{-5}
5822 bpr 294
suppress multiple + or 0 ; see rawmath.c
5460 bpr 295
*/
10 reyssat 296
void putnumber(char *p)
5822 bpr 297
 {
298
     char *sgn = "", *pp = strpbrk(p,"Ee");
299
     if (pp == NULL) { tprint("%s",p); return; }
300
     *pp++ = 0;
301
     if (*pp == '-') { sgn = "-"; pp++; }
302
     while (*pp == '0' || *pp == '+') pp++;
303
     tprint("%s \\times 10^{%s%s}", p, sgn, pp);
304
 }
7673 bpr 305
 
5502 bpr 306
/* Print a variable name ; transform abc475 in abc_475 */
10 reyssat 307
void putvar(char *p)
308
{
309
    char vbuf[1024];
310
    char *pp, *p2;
311
    int i;
7673 bpr 312
 
10 reyssat 313
    vbuf[0]=0;
314
    if(*(p+1)==0) {tprint("%c",*p); return;}
315
    for(pp=p;isalpha(*pp);pp++);
316
    if(myisdigit(*pp)) {
5502 bpr 317
    for(p2=pp+1;myisdigit(*p2);p2++);
318
    if(*p2==0) {/* subscript */
319
        mystrncpy(vbuf,pp,sizeof(vbuf));*pp=0;
10 reyssat 320
    }
5502 bpr 321
    }
10 reyssat 322
    i=search_list(tmathvar, tmathvar_no, sizeof(tmathvar[0]), p);
323
    if(i>=0) tprint("%s ",tmathvar[i].tex);
324
    else tprint("%s ",p);
325
    if(vbuf[0]) {
5502 bpr 326
    if(vbuf[1]==0) tprint("_%c ",vbuf[0]);
327
    else tprint("_{%s} ",vbuf);
10 reyssat 328
    }
329
}
5514 bpr 330
 
5512 bpr 331
/* sort according to type */
10 reyssat 332
int fsort(const void *p1, const void *p2)
333
{
334
    struct afactor *t1, *t2;
335
    int i1,i2;
7673 bpr 336
 
10 reyssat 337
    t1=*(struct afactor **) p1; t2=*(struct afactor **) p2;
338
    i1=t1->type; i2=t2->type;
11125 georgesk 339
    if(i1>type_var) i1=type_var;
340
    if(i2>type_var) i2=type_var;
10 reyssat 341
    return i1-i2;
342
}
343
 
344
void t_oneterm(char *p, int num)
345
{
346
    int sign, fcnt, s, i, dentype, rel;
347
    char *pp, *pe, *pt;
348
    struct afactor factors[MAX_FACTORS];
7673 bpr 349
    struct afactor *numerator[MAX_FACTORS],
350
      *denominator[MAX_FACTORS],
10 reyssat 351
      *neutral[MAX_FACTORS];
352
    int numcnt,dencnt,neucnt;
5502 bpr 353
/* interpret some arrows */
10 reyssat 354
    rel=0; switch(*p) {
5502 bpr 355
      case '<': {
7673 bpr 356
        rel++; p++; if(*p!='=') {tprint(" < "); break;} // <
5502 bpr 357
        do p++; while(*p=='=');
358
        if(*p!='>') {tprint("\\le ");break;} // <= , <===
7673 bpr 359
        else {tprint("\\iff ");p++; break;} // <==>
5502 bpr 360
      }
361
      case '>': {
362
        rel++; p++; if(*p!='=') {tprint(" > "); rel=1; break;} // >
11125 georgesk 363
        while(*p=='=') p++;
11132 bpr 364
        tprint("\\ge "); // >=
5502 bpr 365
        break;
366
      }
367
      case '-': {
368
        for(pp=p;*pp=='-';pp++);
369
        if(*pp!='>') break;
370
        rel++; tprint("\\to "); p=++pp; //->
371
        break;
372
      }
373
     case '=': {
374
        rel++; for(pp=p;*pp=='=';pp++);
375
        if(*pp!='>') break;
376
        tprint("\\Rightarrow "); p=++pp; // =>
377
        break;
378
      }
10 reyssat 379
    }
380
    if(*p==',' || *p==';' || *p=='=') {tprint("%c",*p); p++; num=0;}
381
    sign=1; while(*p=='+' || *p=='-') {
5502 bpr 382
      if(*p=='-') sign*=-1;
383
      p++;
10 reyssat 384
    }
385
    for(fcnt=0, pp=p; fcnt<MAX_FACTORS && *pp; fcnt++, pp=pe) {
5502 bpr 386
      s=1;
387
      while(*pp=='*' || *pp=='/') {
7673 bpr 388
        if(*pp=='/') s=-1;
5502 bpr 389
        pp++;
390
      }
391
      factors[fcnt].side=s;
392
      while(*pp=='+' || *pp=='-') {
393
        if(*pp=='-') sign*=-1;
394
        pp++;
395
      }
396
      pe=find_factor_end(pp); if(pe<=pp) break;
397
      factors[fcnt].beg=pp; factors[fcnt].end=pe;
398
      if(pe-pp==1 && *pp=='1') fcnt--;
399
      if(*pp=='(') {
400
        char *pt, *pe2, buf[MAX_LINELEN+1];
401
        int ss;
402
        pp++; pt=find_matching(pp,')');
403
        if(pt>=pe-1) {
404
          memmove(buf,pp,pt-pp); buf[pt-pp]=0;
405
          i=term_cnt(buf);
406
          if(i==1) { /* remove parentheses */
407
              for(;pp<pt && fcnt<MAX_FACTORS;pp=pe2,fcnt++) {
408
                ss=s; while(*pp=='*' || *pp=='/') {
8568 reyssat 409
                    if(*pp=='/') ss=-s;
5502 bpr 410
                    pp++;
411
                }
412
                factors[fcnt].side=ss;
413
                while(*pp=='+' || *pp=='-') {
414
                    if(*pp=='-') sign*=-1;
415
                    pp++;
416
                }
417
                pe2=find_factor_end(pp);
418
                if(pe2<=pp) goto bailout;
419
                factors[fcnt].beg=pp; factors[fcnt].end=pe2;
420
                if(pe2-pp==1 && *pp=='1') fcnt--;
421
              }
422
              fcnt--;
423
          }
424
        }
425
      }
10 reyssat 426
    }
427
    bailout:
7673 bpr 428
/* decide if the factor is of type numeric, integer, poly, transcend or variable
5512 bpr 429
*  (see priorities)
430
*/
10 reyssat 431
    for(i=0;i<fcnt;i++) {
5502 bpr 432
      pp=factors[i].beg; pe=factors[i].end;
433
      if(myisdigit(*pp) || *pp=='.') {
434
        for(pt=pp;pt<pe && myisdigit(*pt);pt++);
5512 bpr 435
        if(pt<pe) factors[i].type=type_numeric; // digits with a point
436
        else factors[i].type=type_integer;  // digits without point
5502 bpr 437
        continue;
438
      }
439
      if(*pp=='(') {
5512 bpr 440
        factors[i].type=type_poly; continue; //there exists a parenthesis
5502 bpr 441
      }
7673 bpr 442
      pt=strchr(pp,'(');
5512 bpr 443
      if(pt!=NULL && pt<pe) factors[i].type=type_transcend; //??
444
      else factors[i].type=type_var;  // variable in other cases
10 reyssat 445
    }
446
    dentype=-1;
447
    for(i=0;i<fcnt;i++) if(factors[i].side<0 && factors[i].type>dentype)
5512 bpr 448
      dentype=factors[i].type; // denominator type will be compared to the type of the factors
10 reyssat 449
    dencnt=numcnt=neucnt=0;
450
    for(i=0;i<fcnt;i++) {
5502 bpr 451
      if(factors[i].type>dentype) neutral[neucnt++]=factors+i;
452
      else {
453
        if(factors[i].side>0) numerator[numcnt++]=factors+i;
454
        else denominator[dencnt++]=factors+i;
455
      }
10 reyssat 456
    }
457
    if(dencnt>0) qsort(denominator,dencnt,sizeof(denominator[0]),fsort);
458
    if(numcnt>0) qsort(numerator,numcnt,sizeof(numerator[0]),fsort);
459
    if(neucnt>0) qsort(neutral,neucnt,sizeof(neutral[0]),fsort);
460
    if(sign>0 && num>0 && rel==0) tprint(" +");
461
    if(sign<0) tprint(" -");
7673 bpr 462
    if(fcnt<1) tprint("1 "); // no factors why 1 - don't remove the 1 if [1,2;3,4], the 1 is useful?
10 reyssat 463
    if(dencnt>0) {
5502 bpr 464
      tprint(" {");
5512 bpr 465
      if(numcnt==0) tprint(" 1"); // no numerator ? will write {1 over denominator}
466
      else {/* numerator */
7673 bpr 467
        if(numcnt==1 && *numerator[0]->beg=='(' &&
5502 bpr 468
           find_matching(numerator[0]->beg+1,')')==(numerator[0]->end)-1) {
469
          *(numerator[0]->end-1)=0;
470
          t_onestring(numerator[0]->beg+1);
471
          *(numerator[0]->end-1)=')';
472
        }
9146 reyssat 473
        else for(i=0; i<numcnt; i++) {t_onefactor(numerator[i],i);
474
                if(i<numcnt-1) tprint(" ");} /* add space between factors */
5512 bpr 475
      }
476
      tprint(" \\over ");      /* Now denominator */
7673 bpr 477
      if(dencnt==1 && *denominator[0]->beg=='(' &&
5512 bpr 478
        find_matching(denominator[0]->beg+1,')')==(denominator[0]->end)-1) {
479
         *(denominator[0]->end-1)=0;
480
         t_onestring(denominator[0]->beg+1);
481
         *(denominator[0]->end-1)=')';
482
      }
9146 reyssat 483
      else for(i=0;i<dencnt;i++) {t_onefactor(denominator[i],i);
484
                        if(i<dencnt-1) tprint(" ");} /* add space between factors */
5512 bpr 485
      tprint("} ");
10 reyssat 486
    }
9147 bpr 487
    for(i=0;i<neucnt;i++) {t_onefactor(neutral[i],i+dencnt);
488
                if(i<neucnt-1) tprint(" ");} /* add space between factors */
10 reyssat 489
}
490
 
8155 bpr 491
/* put exponential */
10 reyssat 492
void t_exponential(char *pp)
493
{
494
    char *pe, *pt;
495
    int t=0;
7673 bpr 496
 
10 reyssat 497
    while(*pp && strchr("!'\"",*pp)!=NULL) {
5502 bpr 498
      tprint("%c",*pp); pp++;
10 reyssat 499
    }
500
    if(*pp=='^') pp++; else return;
501
    if(*pp=='(') {
5502 bpr 502
      pe=find_matching(pp+1,')');
503
      if(*(pe+1)==0) {
504
        pp++;*pe=0;
505
        for(pt=pp;*pt && (isalnum(*pt) || *pt=='.');pt++);
506
        if(*pt==0) t=1;
507
      }
10 reyssat 508
    }
509
    if(strlen(pp)==1 && t==0) tprint("^%s ",pp);
510
    else {
5502 bpr 511
      tprint(" ^{"); if(t) tprint("(");
512
      t_onestring(pp);
11125 georgesk 513
      if(t) tprint(")");
514
      tprint("} ");
10 reyssat 515
    }
516
}
517
 
518
void t_onefactor(struct afactor *fb, int num)
519
{
520
    char *p, *pe, lp, *rp, rp2, rpbuf[128];
521
    char fbuf[MAX_LINELEN+1], pbuf[MAX_LINELEN+1];
522
    int i;
523
 
524
    memmove(pbuf,fb->beg,fb->end-fb->beg);
525
    pbuf[fb->end-fb->beg]=0;
526
    if(num>0 && (myisdigit(pbuf[0]) || pbuf[0]=='.'))
527
      tprint("\\times ");
528
    rp2=')'; p=pbuf;
529
    if(strchr("({[",*p)!=NULL) {
5502 bpr 530
    lp=*p; switch(lp) {
531
        case '(': rp2=')';  break;
532
        case '[': { /* verify for matrices */
533
          char *pt;
534
          pe=find_matching(p+1,']');
535
          for(pt=p+1;pt<pe;pt++) {
536
              switch(*pt) {
537
                case '(': pt=find_matching(pt+1,')'); break;
538
                case '[': pt=find_matching(pt+1,']'); break;
539
                case '{': pt=find_matching(pt+1,'}'); break;
540
                case '|': pt=find_matching(pt+1,'|'); break;
7673 bpr 541
 
5502 bpr 542
                case ',':
543
                case ';': goto out;
544
              }
545
          }
546
          out: if(*pt==';' || *pt==',') { /* is matrix of the form [ 1,2;5,6] */
547
              char mbuf[MAX_LINELEN+1];
548
              char *pp, *pt;
549
 
550
              p++; if(*pe) *pe++=0;
551
              tprint(" \\begin{pmatrix}");
552
              for(pp=p,i=0;*pp;pp=pt,i++) {
553
                pt=find_term_end(pp);
554
                memmove(mbuf,pp,pt-pp); mbuf[pt-pp]=0;
555
                t_oneterm(mbuf,i);
556
                if(*pt==',') {
557
                    tprint(" &"); pt++; i=-1;
558
                }
559
                if(*pt==';') {
560
                    tprint("\\cr "); pt++; i=-1;
561
                }
562
              }
563
              tprint(" \\end{pmatrix}"); goto expon;
564
          }
565
          rp2=']'; break;
566
        }
567
        case '{': { /* protected */
568
          pe=find_matching(p+1,'}');
569
          *pe=0;tprint(" %s} ",p);
570
          goto expon;
571
        }
10 reyssat 572
    }
7673 bpr 573
    tprint(" \\left%c",lp);
5502 bpr 574
    snprintf(rpbuf,sizeof(rpbuf),"\\right%c ",rp2); rp=rpbuf;
575
    paren: p++;pe=find_matching(p,rp2); *pe=0;
576
    t_onestring(p); tprint(rp); pe++; goto expon;
577
    }
10 reyssat 578
    pe=find_mathvar_end(p); while(*pe && strchr("'\"!",*pe)!=NULL) pe++;
579
    memmove(fbuf,p,pe-p); fbuf[pe-p]=0;
580
    if(myisdigit(*p) || *p=='.') putnumber(fbuf);
581
    if(isalpha(*p)) {
5502 bpr 582
    pe=find_mathvar_end(p); while(*pe && strchr("'\"!",*pe)!=NULL) pe++;
583
    if(*pe=='(') {
584
        p=pe;
585
/* search in list of math functions*/
586
        i=search_list(tmathfn, tmathfn_no, sizeof(tmathfn[0]), fbuf);
587
        if(i>=0) {
588
          switch(tmathfn[i].expind) {
589
              case 0: {
590
                tprint(" %s",tmathfn[i].left);
591
                rp=tmathfn[i].right; break;
592
              }
593
              case 1: {
594
                tprint(" %s",tmathfn[i].left);
595
                pe=find_matching(pe+1,')')+1;
596
                if(*pe && strchr("^'\"!",*pe)!=NULL) {
597
                    t_exponential(pe); *pe=0;
598
                }
599
                tprint(" \\left("); rp=tmathfn[i].right;
600
                break;
601
              }
602
              case 2: {  /* routine */
603
                p++;pe=find_matching(p,rp2); *pe=0;
604
                tmathfn[i].routine(p);
605
                pe++; goto expon;
606
              }
607
              default: rp=""; break;
608
          }
609
        }
610
        else {
611
          putvar(fbuf);
612
          rp="\\right) "; tprint(" \\left(");
613
        }
614
        rp2=')'; goto paren;
10 reyssat 615
    }
5502 bpr 616
    else {
617
        putvar(fbuf);
618
        if(*pe=='_') {
619
          char *ptt, buff[256];
620
          tprint("_"); pe++;
621
          if(*pe=='(') {
622
              ptt=find_matching(pe+1,')'); if(ptt) ptt++;
623
          }
624
          else {
625
              if(*pe=='{') {
626
                ptt=find_matching(pe+1,'}'); if(ptt) ptt++;
627
              }
628
              else ptt=find_mathvar_end(pe);
629
          }
630
          if(ptt==NULL || ptt-pe>128) goto expon;
631
          memmove(buff,pe,ptt-pe); buff[ptt-pe]=0; pe=ptt;
632
          strip_enclosing_par(buff);
633
          tprint("{%s}",buff);
634
        }
635
    }
636
    }
637
    /* exponential */
10 reyssat 638
    expon: if(*pe && strchr("^'\"!",*pe)!=NULL) t_exponential(pe);
639
}
640
 
641
void t_onestring(char *p)
642
{
643
    char termbuf[MAX_LINELEN+1];
644
    char *pp, *pe;
645
    int i;
646
 
647
    for(pp=p,i=0;*pp;pp=pe,i++) {
5502 bpr 648
    pe=find_term_end(pp);
649
    memmove(termbuf,pp,pe-pp); termbuf[pe-pp]=0;
650
    t_oneterm(termbuf,i);
10 reyssat 651
    }
652
}
5459 bpr 653
/* replace \pmatrix{  } by latex syntax \begin{pmatrix} .. \end{pmatrix} */
654
 
655
void _replace_matrix ( char *p , char *s_mat1, char *s_mat2 )
7673 bpr 656
{ char pbuf[MAX_LINELEN];
5459 bpr 657
  while ( (p = strstr(p,s_mat1)) )
658
  { char *p2 = find_matching(p+strlen(s_mat1),'}');
659
    long len = p2-p-strlen(s_mat1);
660
    if (!p2) { module_error("unmatched_parentheses"); return; }
661
    memcpy(pbuf, p+strlen(s_mat1), len); pbuf[len]= 0;
662
    p2 ++ ;
663
    string_modify(p, p, p2, "\\begin{%s}%s\\end{%s}",s_mat2,pbuf,s_mat2);
664
  }
665
}
666
 
5502 bpr 667
/* translate raw math expression into TeX source */
10 reyssat 668
void texmath(char *p)
669
{
670
    char *pp;
7673 bpr 671
    _replace_matrix (p,"\\matrix{","matrix");
5459 bpr 672
    _replace_matrix (p,"\\pmatrix{","pmatrix");
10 reyssat 673
    if(strpbrk(p,"{}\\")!=NULL) return;
674
    for(pp=strstr(p,"!="); pp; pp=strstr(pp+1,"!=")) {
5502 bpr 675
      if(pp>p && !isspace(*(pp-1))) continue;
676
      string_modify(p,pp,pp+2,"*neq*");
10 reyssat 677
    }
5502 bpr 678
/* remove spaces */
10 reyssat 679
    for(pp=p; *pp; pp++) {
5502 bpr 680
    if(isspace(*pp)) {ovlstrcpy(pp,pp+1); pp--;}
10 reyssat 681
    }
8155 bpr 682
/* replace ** by ^  see __replace_badchar(p,"**", "^");*/
10 reyssat 683
    for(pp=strstr(p,"**"); pp!=NULL; pp=strstr(pp,"**")) {
3718 reyssat 684
      *pp='^'; ovlstrcpy(pp+1,pp+2);
10 reyssat 685
    }
686
    if(check_parentheses(p,1)!=0) module_error("unmatched_parentheses");
687
    texmathbuf[0]=0; t_onestring(p);
9147 bpr 688
    singlespace(texmathbuf);strip_trailing_spaces(texmathbuf);
10 reyssat 689
    mystrncpy(p,texmathbuf,MAX_LINELEN);
690
}