Subversion Repositories wimsdev

Rev

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