Subversion Repositories wimsdev

Rev

Rev 8155 | Rev 9146 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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