Subversion Repositories wimsdev

Rev

Rev 5822 | Rev 8185 | 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.  
  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);
  37. /* never used or defined ?? */
  38. void n_onestring(char *p);
  39. void n_oneterm(char *p, int num);
  40. void n_onefactor(struct afactor *p, int num);
  41. /* */
  42. void texmath(char *p);
  43.  
  44. /* print to texmathbuf */
  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. }
  55. /* transforms sum(n,n=1..infinity) , product(n/(n+1),n=1..infinity). */
  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) {
  66.       if(*p2) {
  67.         tprint("_{"); t_onestring(p2); tprint("}");
  68.       }
  69.     }
  70.     else if(*p1) {
  71.       tprint("_{%s",p1);
  72.       if(*p2) { tprint("="); t_onestring(p2); }
  73.       tprint("}");
  74.     }
  75.     if(*p3) {
  76.       tprint("^{"); t_onestring(p3); tprint("}");
  77.     }
  78.     strip_trailing_spaces(p); if(find_term_end(p)<p+strlen(p)) {
  79.       tprint("\\left("); t_onestring(p); tprint("\\right)");
  80.     }
  81.     else t_onestring(p);
  82.     if(type && *p1) {
  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);
  88.     }
  89. }
  90.  
  91. /* integration, sum and product */
  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[]={
  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)"}
  147. };
  148.  
  149. #define tmathfn_no (sizeof(tmathfn)/sizeof(tmathfn[0]))
  150.  
  151. struct {
  152.     char *name, *tex;
  153. } tmathvar[]={
  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"}
  199. };
  200.  
  201. #define tmathvar_no (sizeof(tmathvar)/sizeof(tmathvar[0]))
  202.  
  203.  
  204. /* find the end of an additive term. */
  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++) {
  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");
  242.     }
  243.     return pp;
  244. }
  245.  
  246. /* find the end of an multiplicative factor. */
  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++) {
  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");
  283.     }
  284.     return pp;
  285. }
  286.  
  287.     /* returns the number of terms */
  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.  
  298. /* Print a number: transform 4E+05 in 4 \times 10^{5} and  4E-05 in 4 \times 10^{-5}
  299. suppress multiple + or 0 ; see rawmath.c
  300. */
  301. void putnumber(char *p)
  302.  {
  303.      char *sgn = "", *pp = strpbrk(p,"Ee");
  304.      if (pp == NULL) { tprint("%s",p); return; }
  305.      *pp++ = 0;
  306.      if (*pp == '-') { sgn = "-"; pp++; }
  307.      while (*pp == '0' || *pp == '+') pp++;
  308.      tprint("%s \\times 10^{%s%s}", p, sgn, pp);
  309.  }
  310.  
  311. /* Print a variable name ; transform abc475 in abc_475 */
  312. void putvar(char *p)
  313. {
  314.     char vbuf[1024];
  315.     char *pp, *p2;
  316.     int i;
  317.  
  318.     vbuf[0]=0;
  319.     if(*(p+1)==0) {tprint("%c",*p); return;}
  320.     for(pp=p;isalpha(*pp);pp++);
  321.     if(myisdigit(*pp)) {
  322.     for(p2=pp+1;myisdigit(*p2);p2++);
  323.     if(*p2==0) {/* subscript */
  324.         mystrncpy(vbuf,pp,sizeof(vbuf));*pp=0;
  325.     }
  326.     }
  327.     i=search_list(tmathvar, tmathvar_no, sizeof(tmathvar[0]), p);
  328.     if(i>=0) tprint("%s ",tmathvar[i].tex);
  329.     else tprint("%s ",p);
  330.     if(vbuf[0]) {
  331.     if(vbuf[1]==0) tprint("_%c ",vbuf[0]);
  332.     else tprint("_{%s} ",vbuf);
  333.     }
  334. }
  335.  
  336. /* sort according to type */
  337. int fsort(const void *p1, const void *p2)
  338. {
  339.     struct afactor *t1, *t2;
  340.     int i1,i2;
  341.  
  342.     t1=*(struct afactor **) p1; t2=*(struct afactor **) p2;
  343.     i1=t1->type; i2=t2->type;
  344.     if(i1>type_var) i1=type_var; if(i2>type_var) i2=type_var;
  345.     return i1-i2;
  346. }
  347.  
  348. void t_oneterm(char *p, int num)
  349. {
  350.     int sign, fcnt, s, i, dentype, rel;
  351.     char *pp, *pe, *pt;
  352.     struct afactor factors[MAX_FACTORS];
  353.     struct afactor *numerator[MAX_FACTORS],
  354.       *denominator[MAX_FACTORS],
  355.       *neutral[MAX_FACTORS];
  356.     int numcnt,dencnt,neucnt;
  357. /* interpret some arrows */
  358.     rel=0; switch(*p) {
  359.       case '<': {
  360.         rel++; p++; if(*p!='=') {tprint(" < "); break;} // <
  361.         do p++; while(*p=='=');
  362.         if(*p!='>') {tprint("\\le ");break;} // <= , <===
  363.         else {tprint("\\iff ");p++; break;} // <==>
  364.       }
  365.       case '>': {
  366.         rel++; p++; if(*p!='=') {tprint(" > "); rel=1; break;} // >
  367.         while(*p=='=') p++; tprint("\\ge "); // >=
  368.         break;
  369.       }
  370.       case '-': {
  371.         for(pp=p;*pp=='-';pp++);
  372.         if(*pp!='>') break;
  373.         rel++; tprint("\\to "); p=++pp; //->
  374.         break;
  375.       }
  376.      case '=': {
  377.         rel++; for(pp=p;*pp=='=';pp++);
  378.         if(*pp!='>') break;
  379.         tprint("\\Rightarrow "); p=++pp; // =>
  380.         break;
  381.       }
  382.     }
  383.     if(*p==',' || *p==';' || *p=='=') {tprint("%c",*p); p++; num=0;}
  384.     sign=1; while(*p=='+' || *p=='-') {
  385.       if(*p=='-') sign*=-1;
  386.       p++;
  387.     }
  388.     for(fcnt=0, pp=p; fcnt<MAX_FACTORS && *pp; fcnt++, pp=pe) {
  389.       s=1;
  390.       while(*pp=='*' || *pp=='/') {
  391.         if(*pp=='/') s=-1;
  392.         pp++;
  393.       }
  394.       factors[fcnt].side=s;
  395.       while(*pp=='+' || *pp=='-') {
  396.         if(*pp=='-') sign*=-1;
  397.         pp++;
  398.       }
  399.       pe=find_factor_end(pp); if(pe<=pp) break;
  400.       factors[fcnt].beg=pp; factors[fcnt].end=pe;
  401.       if(pe-pp==1 && *pp=='1') fcnt--;
  402.       if(*pp=='(') {
  403.         char *pt, *pe2, buf[MAX_LINELEN+1];
  404.         int ss;
  405.         pp++; pt=find_matching(pp,')');
  406.         if(pt>=pe-1) {
  407.           memmove(buf,pp,pt-pp); buf[pt-pp]=0;
  408.           i=term_cnt(buf);
  409.           if(i==1) { /* remove parentheses */
  410.               for(;pp<pt && fcnt<MAX_FACTORS;pp=pe2,fcnt++) {
  411.                 ss=s; while(*pp=='*' || *pp=='/') {
  412.                     if(*pp=='/') ss=-1;
  413.                     pp++;
  414.                 }
  415.                 factors[fcnt].side=ss;
  416.                 while(*pp=='+' || *pp=='-') {
  417.                     if(*pp=='-') sign*=-1;
  418.                     pp++;
  419.                 }
  420.                 pe2=find_factor_end(pp);
  421.                 if(pe2<=pp) goto bailout;
  422.                 factors[fcnt].beg=pp; factors[fcnt].end=pe2;
  423.                 if(pe2-pp==1 && *pp=='1') fcnt--;
  424.               }
  425.               fcnt--;
  426.           }
  427.         }
  428.       }
  429.     }
  430.     bailout:
  431. /* decide if the factor is of type numeric, integer, poly, transcend or variable
  432. *  (see priorities)
  433. */
  434.     for(i=0;i<fcnt;i++) {
  435.       pp=factors[i].beg; pe=factors[i].end;
  436.       if(myisdigit(*pp) || *pp=='.') {
  437.         for(pt=pp;pt<pe && myisdigit(*pt);pt++);
  438.         if(pt<pe) factors[i].type=type_numeric; // digits with a point
  439.         else factors[i].type=type_integer;  // digits without point
  440.         continue;
  441.       }
  442.       if(*pp=='(') {
  443.         factors[i].type=type_poly; continue; //there exists a parenthesis
  444.       }
  445.       pt=strchr(pp,'(');
  446.       if(pt!=NULL && pt<pe) factors[i].type=type_transcend; //??
  447.       else factors[i].type=type_var;  // variable in other cases
  448.     }
  449.     dentype=-1;
  450.     for(i=0;i<fcnt;i++) if(factors[i].side<0 && factors[i].type>dentype)
  451.       dentype=factors[i].type; // denominator type will be compared to the type of the factors
  452.     dencnt=numcnt=neucnt=0;
  453.     for(i=0;i<fcnt;i++) {
  454.       if(factors[i].type>dentype) neutral[neucnt++]=factors+i;
  455.       else {
  456.         if(factors[i].side>0) numerator[numcnt++]=factors+i;
  457.         else denominator[dencnt++]=factors+i;
  458.       }
  459.     }
  460.     if(dencnt>0) qsort(denominator,dencnt,sizeof(denominator[0]),fsort);
  461.     if(numcnt>0) qsort(numerator,numcnt,sizeof(numerator[0]),fsort);
  462.     if(neucnt>0) qsort(neutral,neucnt,sizeof(neutral[0]),fsort);
  463.     if(sign>0 && num>0 && rel==0) tprint(" +");
  464.     if(sign<0) tprint(" -");
  465.     if(fcnt<1) tprint("1 "); // no factors why 1 - don't remove the 1 if [1,2;3,4], the 1 is useful?
  466.     if(dencnt>0) {
  467.       tprint(" {");
  468.       if(numcnt==0) tprint(" 1"); // no numerator ? will write {1 over denominator}
  469.       else {/* numerator */
  470.         if(numcnt==1 && *numerator[0]->beg=='(' &&
  471.            find_matching(numerator[0]->beg+1,')')==(numerator[0]->end)-1) {
  472.           *(numerator[0]->end-1)=0;
  473.           t_onestring(numerator[0]->beg+1);
  474.           *(numerator[0]->end-1)=')';
  475.         }
  476.         else for(i=0; i<numcnt; i++) t_onefactor(numerator[i],i);
  477.       }
  478.       tprint(" \\over ");      /* Now denominator */
  479.       if(dencnt==1 && *denominator[0]->beg=='(' &&
  480.         find_matching(denominator[0]->beg+1,')')==(denominator[0]->end)-1) {
  481.          *(denominator[0]->end-1)=0;
  482.          t_onestring(denominator[0]->beg+1);
  483.          *(denominator[0]->end-1)=')';
  484.       }
  485.       else for(i=0;i<dencnt;i++) t_onefactor(denominator[i],i);
  486.       tprint("} ");
  487.     }
  488.     for(i=0;i<neucnt;i++) t_onefactor(neutral[i],i+dencnt);
  489. }
  490.  
  491.     /* put exponential */
  492. void t_exponential(char *pp)
  493. {
  494.     char *pe, *pt;
  495.     int t=0;
  496.  
  497.     while(*pp && strchr("!'\"",*pp)!=NULL) {
  498.       tprint("%c",*pp); pp++;
  499.     }
  500.     if(*pp=='^') pp++; else return;
  501.     if(*pp=='(') {
  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.       }
  508.     }
  509.     if(strlen(pp)==1 && t==0) tprint("^%s ",pp);
  510.     else {
  511.       tprint(" ^{"); if(t) tprint("(");
  512.       t_onestring(pp);
  513.       if(t) tprint(")"); tprint("} ");
  514.     }
  515. }
  516.  
  517. void t_onefactor(struct afactor *fb, int num)
  518. {
  519.     char *p, *pe, lp, *rp, rp2, rpbuf[128];
  520.     char fbuf[MAX_LINELEN+1], pbuf[MAX_LINELEN+1];
  521.     int i;
  522.  
  523.     memmove(pbuf,fb->beg,fb->end-fb->beg);
  524.     pbuf[fb->end-fb->beg]=0;
  525.     if(num>0 && (myisdigit(pbuf[0]) || pbuf[0]=='.'))
  526.       tprint("\\times ");
  527.     rp2=')'; p=pbuf;
  528.     if(strchr("({[",*p)!=NULL) {
  529.     lp=*p; switch(lp) {
  530.         case '(': rp2=')';  break;
  531.         case '[': { /* verify for matrices */
  532.           char *pt;
  533.           pe=find_matching(p+1,']');
  534.           for(pt=p+1;pt<pe;pt++) {
  535.               switch(*pt) {
  536.                 case '(': pt=find_matching(pt+1,')'); break;
  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.  
  541.                 case ',':
  542.                 case ';': goto out;
  543.               }
  544.           }
  545.           out: if(*pt==';' || *pt==',') { /* is matrix of the form [ 1,2;5,6] */
  546.               char mbuf[MAX_LINELEN+1];
  547.               char *pp, *pt;
  548.  
  549.               p++; if(*pe) *pe++=0;
  550.               tprint(" \\begin{pmatrix}");
  551.               for(pp=p,i=0;*pp;pp=pt,i++) {
  552.                 pt=find_term_end(pp);
  553.                 memmove(mbuf,pp,pt-pp); mbuf[pt-pp]=0;
  554.                 t_oneterm(mbuf,i);
  555.                 if(*pt==',') {
  556.                     tprint(" &"); pt++; i=-1;
  557.                 }
  558.                 if(*pt==';') {
  559.                     tprint("\\cr "); pt++; i=-1;
  560.                 }
  561.               }
  562.               tprint(" \\end{pmatrix}"); goto expon;
  563.           }
  564.           rp2=']'; break;
  565.         }
  566.         case '{': { /* protected */
  567.           pe=find_matching(p+1,'}');
  568.           *pe=0;tprint(" %s} ",p);
  569.           goto expon;
  570.         }
  571.     }
  572.     tprint(" \\left%c",lp);
  573.     snprintf(rpbuf,sizeof(rpbuf),"\\right%c ",rp2); rp=rpbuf;
  574.     paren: p++;pe=find_matching(p,rp2); *pe=0;
  575.     t_onestring(p); tprint(rp); pe++; goto expon;
  576.     }
  577.     pe=find_mathvar_end(p); while(*pe && strchr("'\"!",*pe)!=NULL) pe++;
  578.     memmove(fbuf,p,pe-p); fbuf[pe-p]=0;
  579.     if(myisdigit(*p) || *p=='.') putnumber(fbuf);
  580.     if(isalpha(*p)) {
  581.     pe=find_mathvar_end(p); while(*pe && strchr("'\"!",*pe)!=NULL) pe++;
  582.     if(*pe=='(') {
  583.         p=pe;
  584. /* search in list of math functions*/
  585.         i=search_list(tmathfn, tmathfn_no, sizeof(tmathfn[0]), fbuf);
  586.         if(i>=0) {
  587.           switch(tmathfn[i].expind) {
  588.               case 0: {
  589.                 tprint(" %s",tmathfn[i].left);
  590.                 rp=tmathfn[i].right; break;
  591.               }
  592.               case 1: {
  593.                 tprint(" %s",tmathfn[i].left);
  594.                 pe=find_matching(pe+1,')')+1;
  595.                 if(*pe && strchr("^'\"!",*pe)!=NULL) {
  596.                     t_exponential(pe); *pe=0;
  597.                 }
  598.                 tprint(" \\left("); rp=tmathfn[i].right;
  599.                 break;
  600.               }
  601.               case 2: {  /* routine */
  602.                 p++;pe=find_matching(p,rp2); *pe=0;
  603.                 tmathfn[i].routine(p);
  604.                 pe++; goto expon;
  605.               }
  606.               default: rp=""; break;
  607.           }
  608.         }
  609.         else {
  610.           putvar(fbuf);
  611.           rp="\\right) "; tprint(" \\left(");
  612.         }
  613.         rp2=')'; goto paren;
  614.     }
  615.     else {
  616.         putvar(fbuf);
  617.         if(*pe=='_') {
  618.           char *ptt, buff[256];
  619.           tprint("_"); pe++;
  620.           if(*pe=='(') {
  621.               ptt=find_matching(pe+1,')'); if(ptt) ptt++;
  622.           }
  623.           else {
  624.               if(*pe=='{') {
  625.                 ptt=find_matching(pe+1,'}'); if(ptt) ptt++;
  626.               }
  627.               else ptt=find_mathvar_end(pe);
  628.           }
  629.           if(ptt==NULL || ptt-pe>128) goto expon;
  630.           memmove(buff,pe,ptt-pe); buff[ptt-pe]=0; pe=ptt;
  631.           strip_enclosing_par(buff);
  632.           tprint("{%s}",buff);
  633.         }
  634.     }
  635.     }
  636.     /* exponential */
  637.     expon: if(*pe && strchr("^'\"!",*pe)!=NULL) t_exponential(pe);
  638. }
  639.  
  640. void t_onestring(char *p)
  641. {
  642.     char termbuf[MAX_LINELEN+1];
  643.     char *pp, *pe;
  644.     int i;
  645.  
  646.     for(pp=p,i=0;*pp;pp=pe,i++) {
  647.     pe=find_term_end(pp);
  648.     memmove(termbuf,pp,pe-pp); termbuf[pe-pp]=0;
  649.     t_oneterm(termbuf,i);
  650.     }
  651. }
  652. /* replace \pmatrix{  } by latex syntax \begin{pmatrix} .. \end{pmatrix} */
  653.  
  654. void _replace_matrix ( char *p , char *s_mat1, char *s_mat2 )
  655. { char pbuf[MAX_LINELEN];
  656.   while ( (p = strstr(p,s_mat1)) )
  657.   { char *p2 = find_matching(p+strlen(s_mat1),'}');
  658.     long len = p2-p-strlen(s_mat1);
  659.     if (!p2) { module_error("unmatched_parentheses"); return; }
  660.     memcpy(pbuf, p+strlen(s_mat1), len); pbuf[len]= 0;
  661.     p2 ++ ;
  662.     string_modify(p, p, p2, "\\begin{%s}%s\\end{%s}",s_mat2,pbuf,s_mat2);
  663.   }
  664. }
  665.  
  666. /* translate raw math expression into TeX source */
  667. void texmath(char *p)
  668. {
  669.     char *pp;
  670.     _replace_matrix (p,"\\matrix{","matrix");
  671.     _replace_matrix (p,"\\pmatrix{","pmatrix");
  672.     if(strpbrk(p,"{}\\")!=NULL) return;
  673.     for(pp=strstr(p,"!="); pp; pp=strstr(pp+1,"!=")) {
  674.       if(pp>p && !isspace(*(pp-1))) continue;
  675.       string_modify(p,pp,pp+2,"*neq*");
  676.     }
  677. /* remove spaces */
  678.     for(pp=p; *pp; pp++) {
  679.     if(isspace(*pp)) {ovlstrcpy(pp,pp+1); pp--;}
  680.     }
  681.     /* replace ** by ^  see __replace_badchar(p,"**", "^");*/
  682.     for(pp=strstr(p,"**"); pp!=NULL; pp=strstr(pp,"**")) {
  683.       *pp='^'; ovlstrcpy(pp+1,pp+2);
  684.     }
  685.     if(check_parentheses(p,1)!=0) module_error("unmatched_parentheses");
  686.     texmathbuf[0]=0; t_onestring(p);
  687.     mystrncpy(p,texmathbuf,MAX_LINELEN);
  688. }
  689.