source: git/Singular/LIB/tex.lib @ 75089b

spielwiese
Last change on this file since 75089b was 75089b, checked in by Kai Krüger <krueger@…>, 26 years ago
made fixes requested by libparse (s.a. brackets, ....) git-svn-id: file:///usr/local/Singular/svn/trunk@1198 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 43.4 KB
Line 
1// $Id: tex.lib,v 1.4 1998-03-06 11:52:59 krueger Exp $   
2//
3// author : Christian Gorzel email: gorzelc@math.uni-muenster.de
4// created ....18.4.97 (lastchange 13.6.97)
5///////////////////////////////////////////////////////////////////////////////
6
7
8LIBRARY: tex.lib      PROCEDURES FOR TYPESET OF SINGULAROBJECTS IN TEX
9                        by Christian Gorzel, send bugs and
10                        comments to gorzelc@math.uni-muenster.de
11 
12 closetex(s);         writes closing line for TeX-document
13 opentex(s);          writes header for TeX-file s
14 tex(s);              call latex for TeX-file s
15 texdemo();           produces a latex file explaining the features of this lib
16 texfactorize(fnm,f); create string in TeX-Symbolformat for factors of poly f
17 texmap(fnm,m,r1,r2); create string in TeX-Symbolformat for map m:r1->r2
18 texname(fnm,s);      create string in TeX-Symbolformat for identifier
19 texobj(l);           creates string in TeX-Symbolformat for any (basic) type
20 texpoly(f,n[,l]);    creates string in TeX-Symbolformat for poly
21 texproc(fnm,p);      create string in TeX-Symbolformat of text from proc p
22 texring(fnm,r[,l]);  create string in TeX-Symbolformat for ring/qring
23 rmx(s);              remove .aux and .log files of TeXfile s
24 xdvi(s);             call xdvi for dvi-file s
25         (parameters in square brackets [] are optional)
26 
27                      Global Variables:
28  TeXwidth, TeXnofrac, TeXbrack, TeXproj, TeXaligned, TeXreplace, NoDollars
29                  are used to control the typesetting 
30    Call example texdemo; to become familiar with the features of tex.lib
31
32
33  TeXwidth      : int: -1,0,1..9, >9  controls the breaking of long polynomials
34  TeXnofrac     : (int) flag,  write 1/2 instead of \\frac{1}{2}
35  TeXbrack      : string: possible values {,(,<,|, \"\"
36                          controls brackets around ideals and matrices
37  TeXproj       : (int) flag, write : instead of , in intvecs and vectors
38  TeXaligned    : (int) flag, write mappings (and ideals) aligned
39  TeXreplace    : list, entries twoelemented list for replacing symbols
40  NoDollars     : (int) flag, suppresses surrounding "$" signs
41
42
43///////////////////////////////////////////////////////////////////////////////
44
45proc closetex(string fname, list #)
46USAGE:   closetex(fname[,style]); fname,style = string
47RETURN:  nothing; writes a LaTeX2e closing line into file fname
48NOTE:    style overwrites the default setting latex2e; maybe latex,amstex,tex
49         preceeding ">>" end ending ".tex" may miss in fname;
50         overwriting an existing file is not possible
51EXAMPLE: example closetex; shows an example
52{
53  string default = "latex2e";       // may be changed appropriatly (C.G.)
54  int i = 1;
55
56  if (size(#)) { default = #[1];}
57  if (not(size(fname)))                      // filename is the empty string
58  { print("-- Error: need a filename");
59     return();
60  }
61  while (fname[i]==">"){i++;}
62  fname = fname[i,size(fname)-i+1];
63
64  if (size(fname)>=4)               // check if filename is ending with ".tex"
65  { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
66  }
67  else {fname = fname + ".tex";}
68 
69  if (default=="tex") {write(fname,"\\bye");}
70  else { write(fname,"\\end{document}");} 
71  return();
72}
73example
74{ "EXAMPLE:"; echo=2;
75   opentex("exmpl");
76   texobj("exmpl","{\\large \\bf hello}");
77   closetex("exmpl");
78}
79///////////////////////////////////////////////////////////////////////////////
80
81proc tex(string fname, list #)
82USAGE:   tex(fname[,style]); fname,style = string
83RETURN:  nothing; calls latex2e for compiling the file fname
84NOTE:    style overwrites the default setting latex2e; maybe latex,amstex,tex
85         ending ".tex" may miss in fname       
86EXAMPLE: example tex; shows an example
87{
88  string default = "latex2e";           // may be changed appropriatly (C.G.)
89  int i=1;
90
91  while (fname[i]==">"){i++;}
92  fname = fname[i,size(fname)-i+1];
93
94  if (size(fname)>=4)               // check if filename is ending with ".tex"
95  { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
96  }
97  else {fname = fname + ".tex";}
98  if (size(#)) {default = #[1];}
99  "calling ",default, " for :",fname,newline;
100
101  system("sh",default + " " +  fname);
102  return();
103}
104example
105{ "EXAMPLE:"; echo =2;
106  ring r;
107  ideal I = maxideal(7);
108  opentex("exp001");              // defaulted latex2e document
109  texobj("exp001","An ideal ",I);
110  closetex("exp001");
111  tex("exp001");
112
113  opentex("exp002","tex");       // create a texdocument
114  texobj("exp002","An ideal",I);
115  closetex("exp002");
116  tex("exp002","tex");
117  echo = 0;
118  print("the created files will be deleted after pressing <RETURN> ");
119  pause;
120  echo = 2;
121  system("sh","rm -i exp00?.*");
122}
123///////////////////////////////////////////////////////////////////////////////
124
125proc opentex(string fname, list #)         
126USAGE:   opentex(fname[,style]); fname,style = string
127RETURN:  nothing; writes as LaTeX2e header into a new file fname
128NOTE:    suffix .tex may miss in fname
129         style overwrites the default setting latex2e; may be latex,amstex,tex
130EXAMPLE: example opentex; shows an example
131{
132  int i =1;
133
134  while (fname[i]==">"){i++;}
135  fname = fname[i,size(fname)-i+1];
136
137  if (size(fname)>=4)               // check if filename is ending with ".tex"
138  { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
139  }
140  else {fname = fname + ".tex";}
141
142  fname = ">" + fname;
143  write(fname, "\\documentstyle[12pt,amstex]{article}");
144  fname = ">" + fname;              // aendern
145  write(fname, "\\parindent=0pt");
146  write(fname,
147  "\\newcommand{\\C}{{\\Bbb C}}",
148  "\\newcommand{\\F}{{\\Bbb F}}",
149  "\\newcommand{\\N}{{\\Bbb N}}",
150 // "\\newcommand{\\P}{{\\Bbb P}}",
151  "\\newcommand{\\Q}{{\\Bbb Q}}",
152  "\\newcommand{\\R}{{\\Bbb R}}",
153  "\\newcommand{\\T}{{\\Bbb T}}",
154  "\\newcommand{\\Z}{{\\Bbb Z}}",newline);
155  write(fname, "\\begin{document}");
156  return();   
157}
158example
159{ "EXAMPLE:"; echo=2;
160   opentex("exmpl");
161   texobj("exmpl","hello");
162   closetex("exmpl");
163}
164///////////////////////////////////////////////////////////////////////////////
165
166proc texdemo(list #)
167USAGE:   texdemo();
168RETURN:  nothing; generates automatically a LaTeX2e file called: texlibdemo.tex
169         explaining the  features of tex.lib and its gloabl variables
170NOTE:    this proc takes some minutes         
171EXAMPLE: example texdemo; executes the generation
172{ int make_demo = size(#);
173
174  print(" Not implemeted yet");
175  return();
176  if (make_demo) {make_demo=(#[1]=="yes");}
177  if(make_demo)
178  {
179   int TeXdemopart = sytem("sh","sh");
180   system("random",TeXdemopart);
181
182   part0();
183   part1();
184   part2();
185   part3();
186   print(" tldemo.tex generated ...");
187   exitall;
188  }
189  else
190  { print("Enter texdemo(\"yes\") to generate the demofile.");
191    return();
192  }
193}
194example
195{ "EXAMPLE:";
196   print("Enter texdemo(\"yes\") to generate the demofile.");
197}
198///////////////////////////////////////////////////////////////////////////////
199
200proc texfactorize(string fname, poly f, list #)
201USAGE:   opentex(fname,f); fname = string; f = poly
202RETURN:  string, the poly as as product of its irreducible factors
203                 in TeX-typesetting if fname == empty string;
204         otherwise append this to file fname.tex; return nothing 
205NOTE:    preceeding ">>" end ending ".tex" may miss in fname
206EXAMPLE: example texfactorize; shows an example
207{
208  def @r = basering;
209  list l;
210  int i,j,k,Tw,TW,ND;;
211  intvec v;
212  string s,t;
213  string D = "$";
214  poly g;
215
216  ND = defined(NoDollars);
217  if (!(ND)) {int NoDollars; export NoDollars;}
218  else { D = ""; }
219  TW = defined(TeXwidth);
220  if (TW) {Tw = TeXwidth; TeXwidth = -1;} 
221  else {int TeXwidth = -1; export TeXwidth;}
222
223  if (f==0) {s= D + "0" + D;}
224  else
225  {
226   l = factorize(f);      // sollte auch fuer f== 0 direkt funktionieren
227   if (l[1][1]<>1){s = texpoly("",l[1][1]);}
228   for(i=2;i<=size(l[1]);i++)
229   {
230    if(size(s)){s = s+"\\cdot ";}
231    g = l[1][i];
232    v = leadexp(g);
233    k=0;
234    for(j=1;j<=size(v);j++){k = k + v[j];}
235    if(size(g)>1 or (size(g)==1 and k>1))
236    { t = "(" + texpoly("",l[1][i]) + ")";}
237    else { t =  texpoly("",l[1][i]);}
238    if (l[2][i]>1)
239    { t = t+"^{" +string(l[2][i]) + "}";}
240    s = s + t;
241   }
242   if (!(ND)) { kill NoDollars;}
243   s = D + s + D;
244   if (TW) {TeXwidth = Tw;}
245  }
246  if(size(fname))
247  { i=1;
248    while (fname[i]==">"){i++;}
249    fname = fname[i,size(fname)-i+1];
250
251    if (size(fname)>=4)             // check if filename is ending with ".tex"
252    { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
253    }
254    else {fname = fname + ".tex";}
255    write(fname,s);
256  }
257  else{return(s);}
258}
259example
260{ "EXAMPLE:"; echo=2;
261  ring r2=13,(x,y),dp;
262  poly f = (x+1+y)^2*x3y*(2x -2y)*y12;
263  texfactorize("",f);
264  ring R49 = (7,a),x,dp;
265  minpoly = a2 +a +3;
266  poly f = (a24x5 + x3)*a2x6*(x+1)^2;
267  f;
268  texfactorize("",f);
269}
270///////////////////////////////////////////////////////////////////////////////
271
272proc texmap(string fname, def m, def @r1, def @r2, list #)
273USAGE:   texmap(fname,f); fname = string; m = string/map, @r1,@r2 = ring
274RETURN:  string, the map m from @r1 to @r2 preeceded by its name if m = string
275                 in TeX-typesetting if fname == empty string;
276         otherwise append this to file fname.tex; return nothing 
277NOTE:    preceeding ">>" end ending ".tex" may miss in fname
278EXAMPLE: example texmap; shows an example
279{
280  int saveDollars= defined(NoDollars);
281  int TX = defined(TeXwidth);
282  int Tw;
283  int i,n;
284  string r1str,r2str, varr1str, varr2str;
285  string mapname,t,s;
286  string D,DD,vrg = "$","$$",",";
287  def @r = basering;
288  def themap;
289  list l1,l2;
290  string rr1,rr2 = "@r1","@r2";
291
292  proc rp(string s)
293  { int i;
294
295    for(i=1;i<=size(TeXreplace);i++)
296    { if (TeXreplace[i][1]==s) {s= TeXreplace[i][2]; break;}}
297    return(s);
298  }
299 
300// --- store all actual informations
301  if(TX) { Tw = TeXwidth; TeXwidth = -1;}
302  else { int TeXwidth = -1; export TeXwidth;}
303  if (!(saveDollars)) { int  NoDollars; export NoDollars;}
304  if (defined(TeXproj)) {vrg = ":";}
305
306  if (size(#))
307  { if (typeof(#[1])=="list")
308    { l1 = #[1];
309      if(size(#)==2) { l2 = #[2];}
310    }
311    else {l1=#; l2 =#;}
312  }
313// --- tex the information in preimring r1
314
315  setring(@r1);
316  r1str = texring("",@r1,l1);
317// --- avoid an execute; hence construct an ideal
318
319  n = nvars(@r1);
320  if (n>1) { t = "\\left(";}
321  ideal @I = var(1);
322  t = t + texpoly("",var(1));
323  for(i=2;i<=n;i++)
324  { @I = @I + var(i);
325    t = t + vrg + texpoly("",var(i));
326  }
327  if (n>1) { t = t + "\\right)";}
328  varr1str = t;
329
330// --- now the things in ring ring r2
331
332  setring(@r2);
333 // listvar();
334
335  if (typeof(m)=="string")
336  { themap = `m`;
337    mapname = m;
338    if (defined(TeXreplace))
339    { mapname = rp(mapname);   // rp ausschreiben !
340    }
341    mapname = mapname + ":";
342  }
343  if (typeof(m)=="map") { themap = m;}
344
345  r2str = texring("",@r2,l2);
346  ideal @J  = themap(@I);
347  n = size(matrix(@J));
348  if (n>1) { t = " \\left(";}
349  if (!(defined(TeXaligned)) and (n>1))
350      { t = t + newline + "\\begin{array}{c}" + newline;}
351  t = t + texpoly("",@J[1]);
352  for (i=2;i<=n; i++)
353  {if(defined(TeXaligned))
354   { t = t + vrg + texpoly("",@J[i]); }
355   else { t = t + "\\\\" + newline + texpoly("",@J[i]);}
356  }
357  if (!(defined(TeXaligned)) and (n>1))
358      { t = t + newline + "\\end{array}" + newline;}
359  if (n>1) {t = t + "\\right)";}
360  varr2str = t;
361
362// --- go back to  ring r1 to kill @I
363
364  setring(@r1);
365  kill @I;
366
367// --- now reset the old settings and stick all the information together
368
369  setring(@r);
370  if (!(saveDollars)) { kill NoDollars;}
371  if (TX) {TeXwidth = Tw;}
372  else { kill TeXwidth;}
373  if (defined(NoDollars))
374  { D,DD = "",""; }
375
376  if (defined(TeXaligned))
377  { s = D + mapname;
378    s =  s + r1str + "\\longrightarrow" + r2str + ", \\ " +
379        varr1str + "\\longmapsto" + varr2str + D; }
380  else
381  { s = DD;
382    s = s + newline + "\\begin{array}{rcc}" +  newline;
383    s = s + mapname + r1str + " & \\longrightarrow & " +  r2str + "\\\\[2mm]"
384          + newline;
385    s = s + varr1str + " & \\longmapsto & " +  newline + varr2str + newline;
386    s = s + "\\end{array}" + newline;
387    s = s +  DD;
388  }
389
390  if (size(fname))
391  { i=1;
392    while (fname[i]==">"){i++;}
393    fname = fname[i,size(fname)-i+1];
394
395    if (size(fname)>=4)          // check if filename is ending with ".tex"
396    { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
397    }
398    else {fname = fname + ".tex";}
399    write(fname,s);
400  }
401  else {return(s);}
402}
403example
404{ "EXAMPLE:"; echo = 2;
405  string fname = "tldemo";
406  ring r1=0,(x,y,z),dp;   export r1;
407  ring r2=0,(u,v),dp;
408  map phi =(r1,u2,uv -v,v2); export phi;
409  list TeXreplace;
410  TeXreplace[1] = list("phi","\\phi");
411  export TeXreplace;
412  texmap("","phi",r1,r2);
413  int TeXaligned; export TeXaligned;
414  texmap("",phi,r1,r2,"\\C");
415  kill r1,r2,TeXreplace,TeXaligned;
416}       
417///////////////////////////////////////////////////////////////////////////////
418
419proc texname(string fname, string s)
420USAGE:   texname(fname,s);  fname,s = string
421RETURN:  the string s if fname == the empty string "" ;
422         otherwise append s to file fname.tex; return nothing
423NOTE:    preceeding ">>" end ending ".tex" may miss in fname;         
424EXAMPLE: example texname; shows an example
425{
426  string st, extr;
427  int i,anf,end,op,bigch;
428  int n;
429
430  if (s[1]=="{") { return(s[2,size(s)-2]);}
431  if (s=="") { return(s);}
432  s = s + newline;             // add a terminating sign
433  anf=1;
434  while(s[i]!=newline)
435  {
436   i =anf;
437
438  while(s[i]<"0" or s[i]>"9" and s[i]!="'" and s[i]!= "_" and s[i]!="~" and
439        s[i]!="(" and s[i]!=")" and s[i]!= "[" and s[i]!=newline) {i++;}
440  if (s[i]==newline){st = st + s[anf,i-anf]; n = n +10*(i-anf); return(st);}
441  st = st + s[anf,i-anf];                        // the starting letters
442 if (s[anf]>="A" and s[anf]<="Z") {bigch=1;}
443  if (s[i]=="'") { st = st + "'";i++;}
444  if (s[i]=="~") { st = "\\tilde{" + st + "}"; i++;}
445  if (s[i]=="_") { i++;}
446  if (s[i]=="(") { op =1;i++;}
447  if (s[i]=="[") { anf = i+1;
448   while(s[i]!="]"){i++;}                    // matrices and vectors
449    st = st + "_{" + s[anf,i-anf] + "}"; n = n+ 5*(i-anf); i++;
450  // besser: while s[i]<> nwline : scan forward: end, return
451  }
452  if (s[i]==newline) {return(st);}
453  anf =i;
454  while (s[i]>="0" and s[i]<="9") {i++;}  // parse the number after the letters
455  if (bigch and not(op)) { st = st + "^{" + s[anf,i-anf] + "}"; bigch =0;}
456  else { st = st + "_{" + s[anf,i-anf] + "}";}
457  n = n+5*(i-anf);
458  anf =i;            // the next text in ( , ) as exponent
459  if (op) { if (s[i]== ","){anf = anf+1;}             
460   while(s[i] !=")"){ i++;}
461   if (i<>anf){st = st + "^{" + s[anf,i-anf] + "}"; n = n +5*(i-anf);}
462  i++;
463  }
464  anf =i;
465  }
466  if (size(fname))
467  { i=1;
468    while (fname[i]==">"){i++;}
469    fname = fname[i,size(fname)-i+1];
470
471    if (size(fname)>=4)            // check if filename is ending with ".tex"
472    { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
473    }
474    else {fname = fname + ".tex";}
475    write(fname,st);
476  }
477  else {return(st);}
478}
479example
480{ "EXAMPLE:"; echo =2;
481   ring r = 0,(x,y),lp;
482   poly f = 3xy4 + 2xy2 + x5y3 + x + y6;
483   texname("","{f(10)}");
484   texname("","f(10) =");
485   texname("","n1");
486   texname("","T1_12");
487   texname("","g'_11");
488   texname("","f23");
489   texname("","M[2,3]");
490   texname("","A(0,3);");
491   texname("","E~(3)");
492}
493///////////////////////////////////////////////////////////////////////////////
494
495proc texobj(string fname, list #)
496USAGE:   texobj(fname,l); fname = string,l = list of Singular dataypes
497RETURN:  string, the objects in TeX-typesetting if fname == empty string;
498         otherwise append this to file fname.tex; return nothing   
499NOTE:    preceeding ">>" end ending ".tex" may miss in fname;         
500EXAMPLE: example texobj; shows an example
501{
502 int i,j,k,nr,nc,linear,Tw,Dollars;
503 int ND = defined(NoDollars);
504 int TW = defined(TeXwidth);
505
506 if(defined(basering)){ poly g,h; matrix M;}
507 string s,t,l,ineq,sg,Iname;
508 string sep= ",";
509 string D,DA,DE = "$","\\begin{equation*}","\\end{equation*}"+ newline;
510 string OB,CB = "(",")";
511 if (defined(TeXbrack))
512 {// if (TeXbrack=="(") {OB = "("; CB = ")";}
513   if (TeXbrack=="<") {OB = "<"; CB = ">";}
514   if (TeXbrack=="{") {OB = "{"; CB = "}";}
515   if (TeXbrack=="|") {OB = "|"; CB = "|";}
516   if (TeXbrack=="" ) {OB = ""; CB = "";}
517 }
518
519
520 if (!(TW)) { int TeXwidth = -1; export TeXwidth; }
521 Tw = TeXwidth;
522
523 if (defined(TeXproj)){ sep = ":";}
524 if(ND) { D,DA,DE="","","";}
525 else {int NoDollars; export NoDollars;}
526
527 proc absterm(poly f)
528 { int k;
529   
530   for (k=1; k<=nvars(basering); k++)
531   { f = subst(f,var(k),0); }
532   return(f);
533 }
534
535
536 if (size(#)==1)
537 { if (typeof(#[1])=="int" or typeof(#[1])=="intvec" or typeof(#[1])=="vector"
538   or typeof(#[1])=="number" or defined(TeXaligned)) { DA = D; DE = D; } }
539
540 s = DA + newline;
541
542 for (k=1; k<=size(#); k++)
543 { def obj = #[k];
544   if (typeof(obj) == "string")
545   { if (defined(`obj`))
546     { if (typeof(`obj`)=="ideal")
547       { Iname = obj; def e = `obj`;
548         kill obj; def obj = e; kill e;}
549       else {s = s + obj + newline;}
550    }
551    else { s = s + obj + newline;}
552   }
553   if (typeof(obj) == "int") { s = s + "  " + string(obj) + "  ";}
554 
555   if (typeof(obj) == "intvec")
556   { s = s + "  (";
557     for(j=1; j<size(obj);j++) { s = s + string(obj[j]) + sep;}
558     s = s +  string(obj[j]) + ")  ";
559   }
560
561   if (typeof(obj) == "number" )
562   { s = s + texpoly("",obj) + newline;
563   }
564
565   if (typeof(obj) == "poly")
566   { int TeXdisplay; export TeXdisplay;
567     s = s + "\\begin{split}" + newline;
568     s = s + texpoly("",obj) + "\\\\" + newline;
569     s = s + "\\end{split}" + newline;
570    kill TeXdisplay;
571   }
572
573   if (typeof(obj) == "vector")
574   { if (obj==0) { s = s + D + "0" + D;}
575     else
576     {
577      s = s + "\\left" + OB;
578      for(j=1; j<nrows(obj); j++) {s = s + texpoly("",obj[j]) + sep;}
579      s = s + texpoly("",obj[j])  + "\\right" + CB + newline;;
580     }
581    }
582
583   if (typeof(obj) == "ideal")
584   { if (size(Iname))   // verwende hier align
585     { if (Tw==0) {TeXwidth = -1;}
586       s =  s + "\\begin{array}{rcl}" + newline;
587       for (i=1;i<=size(matrix(obj));i++)
588       { s =  s + Iname+ "_{" + string(i) + "} & = & "
589               + texpoly("",obj[i]);
590         if (i<size(matrix(obj))){ s = s  + "\\\\" + newline;}
591       }
592       s = s + newline;
593       s = s + "\\end{array}" + newline;
594       TeXwidth = Tw;
595       Iname ="";
596     }
597     else
598     { 
599      if (TeXwidth==0)
600      { obj= simplify(obj,2);
601        linear = 1;
602        for (j=1;j<=size(obj);j++)
603        { if (deg(obj[j])>1){linear =0; break;}
604        }
605        if (!(linear))
606        { s = s + "\\begin{array}{rcl}" + newline;
607          for(j=1;j<=size(obj);j++)
608          { h = absterm(obj[j]);
609            ineq = attrib(obj[j],"ineq");
610            if(!(size(ineq))) { ineq = "=" ; }
611            l = texpoly("",obj[j]-h) + " & " + ineq + " & " + texpoly("",-h);
612            if(j<size(obj)) { l = l + " \\\\";}
613            s =s+ l + newline;
614           }
615          s = s + "\\end{array}" + newline;
616        }
617        else   // linear
618        { s = s + 
619   "\\begin{array}{*{" + string(2*nvars(basering)-1) + "}{c}cr}" + newline;
620           for(j=1; j<=size(obj);j++)
621           { h = absterm(obj[j]);
622             ineq = attrib(obj[j],"ineq");
623             if(!(size(ineq))) { ineq = "=" ; }
624              l = ""; nc = 0;
625              for (i=1; i<=nvars(basering);i++)
626              { t = " "; sg ="";
627                g = obj[j]-subst(obj[j],var(i),0);
628                if (g!=0) { t = texpoly("",g);}
629                if (i>1)
630                { if (t[1]!="-" and t[1]!= " " and nc ){sg = "+";}
631                  if  (t[1]=="-") { sg = "-"; nc =1; t=t[2,size(t)-1];}
632                  if (t==" ") {sg ="";} 
633                  l = l + " & " + sg + " & " + t;
634                }
635                else { l = t;}
636                if (g!=0) {nc = 1;}
637               }
638
639               l = l + " & " + ineq + " & " + texpoly("",-h);
640             if (j < size(obj)) { l = l + " \\\\";}
641             s = s + l + newline;
642            } // end for (j)
643          s = s + "\\end{array}";
644         }  // end else linear
645       } // end TeXwidth == 0
646   else // TeXwidth <> 0
647   { s =  s + "\\left"+ OB;
648     if (defined(TeXaligned))
649     { s = s + texpoly("",obj,",");
650     }
651     else
652     { s = s + newline + "\\begin{array}{c}" + newline +
653               texpoly("",obj,", \\\\" + newline) +
654                newline + "\\end{array}" + newline;
655     }
656    s = s + "\\right" + CB;
657    } // end TeXwidth <> 0
658   }  // not Iname
659// s;
660  }
661
662   if (typeof(obj) == "module")
663   { M = matrix(obj);
664     if (Tw ==0 or Tw > 9) { TeXwidth = -1;}
665     s = s + "\\left" + OB + newline;
666     if (!(defined(TeXaligned)))
667     {  // Naechste Zeile nicht notwendig !
668     // s = s + "\\begin{array}{*{"+ string(ncols(M)) + "}{c}}" + newline;
669      for(j=1;j<=ncols(M);j++)
670      { l = "\\left" + OB + newline + "\\begin{array}{c}" + newline;
671        l = l + texpoly("",ideal(M[j]), " \\\\" + newline)
672              + newline + "\\end{array}" +newline + "\\right" + CB + newline;
673        if (j< ncols(M)) { l = l + " , " + newline;}
674        s = s + l ;
675      }
676     }
677     else    // TeXaligned
678     {
679      for(j=1;j<=ncols(M);j++)
680      { s = s + "\\left" + OB + newline +
681                texpoly("",ideal(M[j]),",") + newline + "\\right" + CB;
682        if (j<ncols(M)) { s = s + "," + newline; }
683      }
684     }
685    s = s  + "\\right" + CB + newline;
686   } // module part
687
688
689   if (typeof(obj) == "matrix")
690   { if (Tw==0 or Tw > 9) {TeXwidth = -1;}
691     M = transpose(obj);
692     s = s + "\\left" + OB + newline +
693             "\\begin{array}{*{"+ string(ncols(obj)) + "}{c}" + "}"+ newline;
694     for(i=1;i<=ncols(M);i++)
695     { l = l + texpoly("",ideal(M[i])," & ");
696       if (i<ncols(M)) { l = l + " \\\\" + newline;}
697     }
698     l = l + newline;
699     s = s + l + "\\end{array}" + newline +
700                 "\\right" + CB + newline;
701    TeXwidth = Tw;
702  }
703 
704   if (typeof(obj) == "intmat")
705   { nr,nc = nrows(obj),ncols(obj);
706     l = "";
707     l =  "\\left" + OB + newline +
708          "\\begin{array}{*{"+ string(nc) + "}{r}}"+ newline;
709     for(i=1;i<=nr;i++)
710     { for(j=1;j<=nc;j++)
711       { l = l + string(obj[i,j]);
712         if (j <nc ) { l = l + " & ";}
713         else {if( i < nr) { l = l + "\\\\" + newline;}}
714       }
715     }
716     l = l + newline + "\\end{array}" + newline +
717             "\\right" + CB + newline;
718    s = s + l;
719  }
720
721  if (typeof(obj) == "ring" or
722      typeof(obj) == "qring") { s = s + D + texring("",obj) + D + newline;}
723
724  kill obj;
725 }
726
727 s = s + DE + newline;
728
729 if(!(ND)) { kill NoDollars;}
730
731// s;
732 if(size(fname))
733 { i=1;
734  while (fname[i]==">"){i++;}
735  fname = fname[i,size(fname)-i+1];
736  if (size(fname)>=4)               // check if filename is ending with ".tex"
737  { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
738  }
739  else {fname = fname + ".tex";}
740  write(fname,s);
741 }
742 else {return(s);}
743}
744example
745{ "EXAMPLE:"; echo =2;
746   ring r = 0,(x,y),lp;
747   poly f = 3xy4 + 2xy2 + x5y3 + x + y6;
748   ideal G = jacob(f);
749   texobj("",G);
750   matrix J = jacob(G);
751   texobj("",J);
752   intmat m[3,4] = 9,2,4,5,2,5,-2,4,-6,10,-1,2,7;
753   texobj("",m);
754   ring r0 = 0,(x,y,z),dp;
755   ideal I = 2x2-4yz3+2-4,zy2+2x,y5z-6x2+7y4z2 +5;
756}
757///////////////////////////////////////////////////////////////////////////////
758
759proc texproc(string fname,string pname)
760USAGE:   opentex(fname,pname); fname,pname = string
761RETURN:  string, the proc in a verbatim environment in TeX-typesetting
762                 if fname == empty string;
763         otherwise append this to file fname.tex; return nothing 
764NOTE:    preceeding ">>" end ending ".tex" may miss in fname;
765CAUTION: texproc cannot applied on itself correctly         
766EXAMPLE: example texproc; shows an example
767{
768  int i,j=1,1;
769  string p,s,t;
770
771  if (defined(pname))
772  { if (typeof(`pname`)=="proc")
773    { p = string(`pname`);
774      s = "\\begin{verbatim}" + newline;
775      s = s + "proc " + pname + "(";
776      i = find(p,"parameter");       // collecting the parameters
777      while(i)
778      { j=find(p,";",i);
779        t = p[i+10,j-i-10];
780        if(i>1){s = s + ",";};
781        s = s + t;
782        i = find(p,"parameter",j);
783      }
784      s = s + ")" + newline;
785     j++;                      // skip one for the newline
786     i = find(p,";RETURN();",j);  // j kann hier weg
787     s = s + "{" + p[j,i-j-1] + "}" + newline;
788     s = s + "\\end{verbatim}" + newline;
789   }
790  }
791  else
792  { print("--Error: No such proc defined");
793    return();
794  }
795  if(size(fname))
796  { i=1;
797    while (fname[i]==">"){i++;}
798    fname = fname[i,size(fname)-i+1];
799    if (size(fname)>=4)        // check if filename is ending with ".tex"
800    { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
801    }
802    else {fname = fname + ".tex";}
803    write(fname,s);
804  }
805  else{return(s);}     
806}
807example
808{ "EXAMPLE:"; echo=2;
809  proc exp(int i,int j,list #)
810  { string s;
811
812    if (size(#))
813    {
814     for(i;i<=j;i++)
815     { s = s + string(j) + string(#); }
816    }
817   return(s);
818  }
819  export exp;
820  texproc("","exp");
821}
822
823///////////////////////////////////////////////////////////////////////////////
824
825proc texring(string fname, def r, list #)
826USAGE:   texring(fname, r[,l]); fname = string; r = ring;
827                                l=list of strings : controls the symbol for
828                                coefficint field etc. see example texdemo();
829RETURN:  string, the ring in TeX-typesetting if fname == empty string;
830         otherwise append this to file fname.tex; return nothing   
831NOTE:    preceeding ">>" end ending ".tex" may miss in fname;       
832EXAMPLE: example texring; shows an example
833{
834  int i,galT,flag,mipo,nopar,Dollars,TB,TA;
835  string ob,cb,cf,en,s,t,savebrack; //opening bracket, closing br, coef.field
836  intvec v;
837
838
839  proc tvar(intvec v)
840  {
841    int i,j,ldots;
842    string s;
843   
844    j = 1;
845    s = texpoly("",var(1));
846   
847    if (nvars(basering)==1) { return(s);}
848    if (nvars(basering)==2) { return(s + "," + texpoly("",var(2)));}
849    if (size(v)==1 and v[1] == 1)
850       {return(s + ",\\ldots,"+ texpoly("",var(nvars(basering))));}
851    if (v[1]==1 and size(v) >1) {j++;}
852
853    for(i=2;i<nvars(basering);i++)
854    { if (i<v[j]  and !(ldots))
855      { s = s + ",\\ldots";
856        ldots =1;
857      }
858      if (i== v[j])
859      { s = s + "," + texpoly("",var(i));
860        ldots =0;
861        if (j< size(v)) {j++;}
862      }
863    }
864   if (v[j]<nvars(basering)-1) { s = s + ",\\dotsc";}
865   return(s + "," + texpoly("",var(nvars(basering))));
866  }
867
868
869  setring r;
870  if (!(defined(NoDollars))){ Dollars = 1; int NoDollars; export NoDollars;}
871  ob,cb = "[","]";
872  if (find(ordstr(r),"s")) { ob,cb="\\{","\\}";}
873  if(char(r)==0){cf="\\Q";}
874  if(charstr(r)=="real"){cf="\\R";}
875  if(char(r)==prime(char(r))){cf="\\Z_{"+string(char(r))+"}";}
876  if(char(r)>0)
877  { i = find(charstr(r),",");
878    if(i)
879    { t= charstr(r)[1,i-1];
880      galT = (t <> string(char(r)));
881      if (galT) { cf = "\\F_{"+ t + "}";}
882    }
883  }     // all other cases are cover already by char(r)=? prime(char)
884
885  if (size(#))
886  { if (typeof(#[1])=="list") { # = #[1];}
887  }
888  for (i=1;i<=size(#);i++)
889  { flag =0;
890    if(typeof(#[i])=="string")
891    {
892     if(#[i][1]=="^" or #[i][1]=="_"){en=en+#[i];flag = 1;}
893     if(#[i]=="mipo"){mipo=1; flag = 1;}
894     if(#[i]=="{"){ob,cb="\\{","\\}";flag=1;}
895     if(#[i]=="{{"){ob,cb="\\{\\{","\\}\\}";flag=1;}
896     if(#[i]=="["){ob,cb="[","]";flag=1;}
897     if(#[i]=="[["){ob,cb="[[","]]";flag=1;}
898     if(#[i]=="<"){ob,cb="<",">";flag=1;}
899     if(#[i]=="<<"){ob,cb="{\\ll}","{\\gg}";flag=1;}
900     if(#[i]=="C"){cf="\\C";flag=1;}
901     if(#[i]=="Q"){cf="\\Q";flag=1;}
902     if((#[i]=="k" or #[i]=="K" or #[i]=="R") and !(galT))
903                   {cf=#[i]; flag=1; nopar=1;}
904     if (flag!=1) {cf = #[i];}  // for all the cases not covered here e.g Z_(p)
905    }                           // or Q[i]
906
907    if ((typeof(#[i])=="intvec") or
908        (typeof(#[i])=="int")){v=#[i];}
909   }
910  s = cf;
911  if (!(galT) and mipo and minpoly!=0) { s = s + "/" + texpoly("",minpoly);}
912 // now the parameters
913 // t;
914  if(npars(r) and ((t==string(char(r))) or char(r)==0) and !(nopar))
915  {
916   s = s + "(";                      // !! mit ideal !!
917   for(i=1;i<npars(r);i++) {s = s + texpoly("",par(i)) + ",";}
918   s = s + texpoly("",par(npars(r))) + ")";
919  }                               // paramters done
920  s = s + ob;
921  if (v!=0 and size(nvars(r))>3)
922  { s = s + tvar(v);}
923  else
924  { for(i=1;i<nvars(r);i++) {s = s + texpoly("",var(i)) + ",";}
925    s = s + texpoly("",var(nvars(r)));
926  }
927   s = s + cb + en;
928
929  if (typeof(r)=="qring")
930  { ideal @I = ideal(r);
931    if (defined(TeXbrack))
932    {
933      TB =1; savebrack = TeXbrack;
934      if (TeXbrack!= "<" and TeXbrack!="(") { TeXbrack = "<";}
935    }
936    TA = defined(TeXaligned);
937    if (!(TA)) { int TeXaligned; export TeXaligned; }
938    t = texobj("",@I);
939    @I;
940    t;
941    if (TB) { TeXbrack = savebrack;}
942    if (!(TA)) { kill TeXaligned;}
943    s = s + "/" + t;
944  }
945
946  if (Dollars)
947  { kill NoDollars;
948    s =  "$" + s + "$";
949  }
950  if (size(fname))
951  { i=1;
952     while (fname[i]==">"){i++;}
953     fname = fname[i,size(fname)-i+1];
954
955     if (size(fname)>=4)           // check if filename is ending with ".tex"
956     { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
957     }
958     else {fname = fname + ".tex";}
959     write(fname,s);
960  }
961  else{return(s);}
962}
963example
964{ "EXAMPLE:"; echo=2;
965  ring r0 = 0,(x,y,z),dp;       // short varnames polynomial ordering
966  texring("",r0);
967  ring r7 =0,(x(0..2)),ds;      // char =7, long varnames
968  texring("",r7);
969  ring r1 = 0,(x1,x2,y1,y2),wp(1,2,3,4);
970  texring("",r1);
971  ring r2= 0,(x_10,x_11),M(1,2,3,4);
972  texring("",r2);
973  ring rr = real,(x),dp;        // real numbers
974  texring("",rr);
975  ring r;
976  texring("",r);
977  ring rabc =(0,t1,t2,t3),(x,y),dp;  // parameters
978  texring("",rabc);
979  ring ralg = (7,a),(x1,x2),(ds,dp);  // algebraic extension
980  minpoly = a2-a+3;
981  texring("",ralg);
982 // texring("",ralg,"mipo");
983  ring r49=(49,a),x,dp;              // Galoisfield
984  texring("",r49); 
985  setring r0;
986  ideal i = x2-z,xy2+1;
987  qring q = std(i);
988  q;
989  texring("",q);
990  // -------- additional features -------------
991  ring r10 =0,(x(0..10)),dp;
992  texring("",r10,1);
993  ring rxy = 0,(x(1..5),y(1..6)),ds;
994  intvec v = 5,6;
995  texring("",rxy,v);
996  texring("",r0,"C","{");
997  texring("",ralg,"k");
998  texring("",r7,"^G");
999  list TeXreplace;
1000  TeXreplace[1] = list("x","\\xi");
1001  TeXreplace[2] = list ("t","\\lambda");
1002  export TeXreplace;
1003  texring("",rabc);
1004  texring("",r7);
1005  kill TeXreplace;
1006}
1007
1008///////////////////////////////////////////////////////////////////////////////
1009
1010proc rmx(string fname)
1011USAGE:   rmx(fname); fname = string
1012RETURN:  nothing; removes .log and .aux files associated to file <fname>     
1013         removes tex and xdvi file too, if suffix ".tex" or ".dvi" is given
1014NOTE:    if fname ends by .dvi or .tex
1015         fname.dvi or fname.dvi and fname.tex will be deleted, too           
1016EXAMPLE: example rmx; shows an example
1017{
1018  int i,suffix= 1,0;
1019
1020  while (fname[i]==">"){i++;}
1021  fname = fname[i,size(fname)-i+1];
1022
1023  if (!(size(fname))) { return();}
1024  if (size(fname)>4)
1025  { if (fname[size(fname)-3,4]==".tex") { suffix = 2;}
1026    if (fname[size(fname)-3,4]==".dvi") { suffix = 1; }
1027    fname = fname[1,size(fname)-4];
1028  }
1029  system("sh","rm " + fname + ".aux");
1030  system("sh","rm " + fname + ".log");
1031  if (suffix==2) {system("sh","\bin\rm -i " + ".tex");}
1032  if (suffix>=1) {system("sh","\bin\rm -i " + ".dvi");}
1033  return();
1034}
1035example
1036{ "EXAMPLE:"; echo =2;
1037  ring r;
1038  poly f = maxideal(7);
1039  opentex("exp001");              // defaulted latex2e document
1040  texobj("exp001","A polynom",f);
1041  closetex("exp001");
1042  tex("exp001");
1043  rmx("exp001");   // removes aux and log file of exp001
1044
1045  opentex("exp002","tex");       // create a texdocument
1046  texobj("exp002","A polynom",f);
1047  closetex("exp002");
1048  tex("exp002","tex");
1049  rmx("exp002.tex");  // removes aux, log, dvi and tex file of exp002
1050  echo = 0;
1051  print("remaining files will be deleted after pressing <RETURN> ");
1052  pause;
1053  echo = 2;
1054  system("sh","rm -i exp00?.*");
1055}
1056///////////////////////////////////////////////////////////////////////////////
1057
1058proc xdvi(string fname, list #)
1059USAGE:   xdvi(fname[,style]); fname,style = string
1060RETURN:  nothing; displays dvi-file fname.dvi with previewer xdvi
1061NOTE:    ending .dvi may miss in fname
1062         style overwrites the default setting xdvi
1063EXAMPLE: example xdvi ; shows an example
1064{
1065  int i=1;
1066  string default = "xdvi";            // may be changed appropriatly (C.G.)
1067 
1068  while (fname[i]==">") {i++;}
1069  fname = fname[i,size(fname)-i+1];
1070
1071  if (size(fname)>=4)
1072  { if(fname[size(fname)-3,4]==".tex") {fname = fname[1,size(fname)-4];}}
1073  if (size(#)) {default = #[1];}
1074  "calling ",default, " for :",fname,newline;
1075
1076  system("sh",default + " " +  fname + " &");
1077  return();
1078}
1079example
1080{ "EXAMPLE:"; echo = 2;
1081  ring r;
1082  ideal i = maxideal(7);
1083  opentex("exp001");              // defaulted latex2e document
1084  texobj("exp001","A ideal",i);
1085  closetex("exp001");
1086  tex("exp001");
1087  xdvi("exp001");
1088  echo = 0;
1089  print("the created files will be deleted after pressing <RETURN> ");
1090  pause;
1091  echo = 2;
1092  system("sh","rm -i exp00?.*"); 
1093}
1094///////////////////////////////////////////////////////////////////////////////
1095
1096proc texpoly(string fname,def p,list #)
1097{
1098  def @r = basering;
1099
1100  poly f,monom;
1101  ideal I;
1102  number cfm;
1103  string sign,cfmt,pt,s,bg,t,monomt;
1104  string sep = newline;
1105  int i,b,b2,n, msz,linesz, count,k;
1106  int realT, parT, galT;
1107  int C = 2 + defined(TeXdisplay);
1108   
1109
1110
1111  proc parsr(string s)                     // parse real
1112  { string t;
1113                             
1114    if (s=="      Inf") { return("\\infty",3);}   
1115    if (s=="     -Inf") { return("\\-infty",6);}
1116    if (s[7]=="-"){t ="-";}
1117    if (s[8]<>"0"){t = t + s[8];}
1118    if (s[9]<>"0" or s[8]<>"0"){t = t + s[9];}
1119    if (size(t))
1120    { if (t=="1") {return(s[1,5]+"*10",21);}
1121      if (size(t)>1) {return(s[1,5]+"*10^{"+t+"}",21+2*size(t));}
1122      else {return(s[1,5]+"*10^"+t,23);}
1123    }
1124    else {return(s[1,5],12);}
1125  }
1126
1127  proc parsg(string s)                  // parse Galoisfield
1128  { int i,j = 1,1;
1129    string t;
1130   
1131    if (short)
1132    { t =s[1];
1133     if(size(s)>1) {return(t+"^{" + s[2,size(s)-1] + "}",3+2*(size(s)-1));}
1134     else{return(t,5);}     
1135    }
1136    else
1137    { return(parselong(s+"!"));}
1138  }
1139 
1140  if (defined(TeXdisplay)) { bg = "& ";}
1141  if (!(defined(TeXwidth))) { int TeXwidth = -1; export TeXwidth;}
1142  if (typeof(p)=="poly" or typeof(p)=="number") {I = p;}
1143  if (typeof(p)=="ideal")
1144  { I = p;
1145    if(size(#)){ sep = #[1];}
1146  }
1147
1148  if (I==0)
1149  { if (!(defined(NoDollars))){return("$0$");}
1150    else {return("0");}
1151  }
1152
1153//---------------------
1154
1155
1156//------- set flags: --------------------------------------------------------
1157   
1158  if (size(#))
1159  { if (typeof(#[1])=="int") { linesz = #[1];}
1160 //   if (typeof(#[1])=="string") { linesz = #[1];}
1161  }
1162
1163  parT = npars(@r);
1164  realT = (charstr(@r)=="real");
1165  i = find(charstr(@r),",");
1166  if (i)
1167  { t = charstr(@r)[1,i-1];
1168    galT = (t <> string(char(@r)));  // the char is not the same as the ...
1169  }
1170  i = 0;
1171 
1172//------- parse the polynom
1173  pt = bg;
1174 
1175 for(k=1;k<=size(matrix(I));k++)
1176 { i = 0; linesz = 0; count =0;
1177   sign ="";
1178   f = I[k];
1179   if (f==0) { pt = pt + "0";}
1180  while(f<>0)
1181  { count++; msz = 0;
1182
1183// ------ tex the coefficient
1184    monom = lead(f);   
1185    f = f - monom;       
1186    cfm = leadcoef(monom);
1187    monom = monom/cfm;                  // the normalized monom
1188    s = string(monom) + "!";            // add an terminating sign
1189    cfmt = "";
1190   
1191    if (defined(TeXreplace)) { short =0;}  // this is essential
1192    cfmt = string(cfm);
1193    if (size(cfmt)>1)                   // check if sign is < 0
1194    { if (cfmt[2]=="-") { cfm = (-1) *cfm; sign = "-";}}
1195    if (cfmt[1] == "-") { cfm = (-1) * cfm; sign = "-";}
1196    if  (cfm!=1 or monom==1) {cfmt = string(cfm);}
1197    else {cfmt="";}
1198
1199    if (defined(TeXwidth) and TeXwidth > 0 and TeXwidth <9 and count> TeXwidth)
1200    { pt = pt + sign + "\\dotsb"; break;}
1201   // ----------------------------------------  linesz ??
1202
1203    if (size(cfmt))                            // parse the coefficient
1204    {
1205     monomt = cfmt;                   // (already a good choice for integers)
1206     msz = size(cfmt);
1207
1208     if(realT) { monomt,msz = parsr(cfmt);}
1209     if (galT) { monomt,msz = parsg(cfmt);}
1210     b = find(cfmt,")/(");                     // look if fraction
1211     b2 = find(cfmt,"/");
1212     if (b) {b++;}
1213     n = size(cfmt);
1214     if (!(parT) and  !(realT) and !(galT))   
1215     { if( !(b2) or defined(TeXnofrac))
1216       {monomt = cfmt; msz = size(monomt);}
1217       else
1218       { monomt = "\\frac{" + cfmt[1,b2-1] + "}{" + cfmt[b2+1,n-b2] + "}";
1219          if (n-2*b2>0) {msz = C*(n-b2);}
1220          else {msz = C*b2;}
1221       }
1222     }
1223     if (parT and !(galT))
1224     { monomt,msz = parsp(cfmt,b);}
1225    }
1226
1227// -- now parse the monom
1228    if (monom <> 1)
1229    { i = 1;   
1230      if(short)
1231      {                   while(s[i]<>"!")
1232        { monomt = monomt + s[i]; i++;
1233          b = i;
1234          msz = msz + 3; //the was the single lettered var
1235          while(s[i]!="!" and s[i]>="0" and s[i]<="9"){i++;}
1236          if (i-b)
1237          { monomt = monomt + "^{" + s[b,i-b] + "}";
1238            msz = msz + 2*(i-b);
1239          }
1240        }
1241      }
1242      else          //  not short
1243      { t,i = parselong(s);
1244        monomt = monomt + t;
1245        msz = msz + i;
1246      }
1247    }
1248   
1249
1250
1251   if (TeXwidth > 10 and (linesz + msz > 2*TeXwidth) and linesz) {
1252   pt = pt + "\\\\" + newline +bg; linesz = 0; }
1253   else { linesz = linesz + msz; }
1254   pt = pt + sign + monomt;
1255   sign = "+";
1256   monomt = "";
1257  }
1258
1259  if (k<size(matrix(I))){ pt = pt + sep;}
1260 }
1261
1262  if (not(defined(NoDollars))) { pt = "$"+pt+"$";}
1263
1264  if (size(fname))
1265  { i=1;
1266    while (fname[i]==">"){i++;}
1267    fname = fname[i,size(fname)-i+1];
1268
1269    if (size(fname)>=4)         // check if filename is ending with ".tex"
1270    { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
1271    }
1272    else {fname = fname + ".tex";}
1273    write(fname,pt);
1274   }
1275  else {return(pt);}
1276}   
1277example
1278{ "EXAMPLE:"; echo =2;
1279  ring r0=0,(x,y,z),dp;
1280  poly f = -1x^2 + 2;
1281  texpoly("",f);
1282  texpoly("",2x2y23z);
1283 
1284
1285  ring rr= real,(x,y),dp;
1286
1287  ring r7= 7,(x,y,z),dp;
1288  poly f = 2x2y23z;
1289  texpoly("",f);
1290  ring rab =(0,a,b),(x,y,z),dp;
1291  poly f = (-2a2 +b3 -2)/a * x2y4z5 + (a2+1)*x + a+1;
1292  f;
1293  texpoly("",f);
1294}     
1295
1296proc parsp(string cfmt, int b)
1297{ string mt, nom,denom;
1298  int fl1,fl2,sz1,sz2,msz;
1299
1300  if (!(b))
1301  { mt,fl1 = parst(cfmt,0); msz = size(cfmt)-2;
1302    if (fl1) { mt = "(" + mt + ")"; msz = msz +1; }
1303  }
1304  else
1305  { nom,fl1 = parst(cfmt[1,b-1],1);
1306    denom,fl2 = parst(cfmt[b+1,size(cfmt)-b],1);
1307    if (defined(TeXnofrac))
1308    { if(fl1) { nom = "(" + nom + ")"; sz1++;}
1309      if(fl2) {denom = "(" + denom + ")"; sz2++;}
1310      mt = nom+ "/"+ denom; msz = sz1+sz2 +1;
1311    }
1312    else
1313    { mt = "\\frac{" + nom + "}{" + denom + "}";
1314      if (sz1-sz2) { msz = 5*sz1;}
1315      else {msz = 5*sz2;}
1316    }
1317   }
1318  return(mt,msz);
1319}
1320example
1321{"EXAMPLE:"; echo =2;
1322  ring r=(0,a,b),x,dp;
1323  int i;
1324  poly f = (a2b12 + 23a2 -b13-1)/(a2+2b -1);
1325  f;
1326  string s;
1327  s= string(f);
1328  i = find(s,")/(");
1329  parsp(s,i);
1330}
1331
1332proc parst(string s,int sec)                // parse parameter
1333// sec parameter to see if in parsp a fraction follows
1334{ int i,j =1,-1;
1335  int b,k,jj,mz;                         // begin and end
1336  int saveshort=short;
1337            string t,c,vn,nom,denom,sg;
1338
1339  s = s[2,size(s)-2];    s = s + "!";
1340
1341  if(defined(TeXreplace)){ short =0;}   // only then replacing works correctly
1342  if (short)
1343  { while(s[i]<>"!")
1344    { b=i; j++;
1345      while(s[i]>="0" and s[i]<="9" or (s[i]=="+" or s[i]=="-") and s[i]!="!")
1346      {i++;}     // scan the number
1347        t =s[b,i-b];
1348    //  if (t=="-1" and s[i]!="!" and s[i]!="-" and s[i]!="+"){t = "-";}
1349      if (t=="-1" and (s[i]<="0" or s[i]>="9") and s[i]!= "/" and s[i]!="!")
1350       {
1351     t = "-";}
1352      if (s[i]=="/")     
1353      { i++;
1354        sg = "";
1355        if (t[1]=="+" or t[1]=="-")
1356        { nom = t[2,size(t)-1];
1357          sg = t[1];
1358        }
1359        else { nom = t;}
1360        b =i;
1361        while(s[i]>="0" and s[i]<="9") {i++;}
1362        denom = s[b,i-b];
1363        if (!(sec) and (!(defined(TeXaligned))))
1364        { t = sg + "\\frac{" + nom + "}{" + denom + "}";}
1365        else
1366        { t = sg + "(" + nom + "/" + denom + ")";
1367        }
1368      } 
1369      c = c + t;
1370      if(s[i]!="!"){c = c + s[i]; i++;}      // the parameter
1371      b=i;
1372      while(s[i]>="0" and s[i]<="9")
1373      {i++;}  //the exponent
1374     if(i-b){ c = c + "^{" + s[b,i-b]+"}";}
1375     }
1376   }
1377   else                         // if not short ....
1378   { while (s[i] <> "!")
1379     { b=i; j++;
1380       while(s[i]=="-" or s[i]=="+" or (s[i]>="0" and s[i]<="9")){i++;}
1381       t = s[b,i-b];
1382       if (t=="-1" and s[i]=="*" ) {t="-";}
1383      if (s[i]=="/")
1384      { i++;
1385        sg = "";
1386        if (t[1]=="+" or t[1]=="-")
1387        { nom = t[2,size(t)-1];
1388          sg = t[1];
1389        }
1390        else { nom = t;}
1391        b =i;
1392        while(s[i]>="0" and s[i]<="9") {i++;}
1393        denom = s[b,i-b];
1394        if (!(sec) and (!(defined(TeXaligned))))
1395        { t = sg + "\\frac{" + nom + "}{" + denom + "}";}
1396        else
1397        { t = sg + "(" + nom + "/" + denom + ")";
1398        }
1399      } 
1400       c = c+t; t="";   
1401       if (s[i]=="*"){i++;}
1402       b=i;
1403       while(s[i]!="+" and s[i]!="-" and s[i]!="!")  //pass a monom
1404       { // start with letters
1405        // alternativ:
1406        while((s[i]>="a" and s[i]<="z") or (s[i]>="A" and s[i]<="Z")){i++;}
1407             k = i-b;
1408        vn = s[b,k];
1409        if (defined(TeXreplace))
1410        { for (jj=1; jj<= size(TeXreplace);jj++)
1411         { if (vn == TeXreplace[jj][1])
1412           {vn = TeXreplace[jj][2]; k=1;
1413             if (s[i]=="*") {vn = vn + " ";}
1414            break;} //suppose replacing by a single sign
1415         }
1416        }
1417        t = t + vn;
1418        mz = mz + 10*k;
1419        if (s[i]=="_"  or s[i]=="(") { i++;}    // the index is coming
1420        b = i;
1421        while(s[i]>="0" and s[i]<="9"){ i++;}
1422        k = i-b;
1423        if (k){ t = t + "_{" +s[b,k] + "}";}
1424        if(s[i]==")") {i++;}
1425            if (s[i]=="^")
1426        { i++; b = i;
1427          while(s[i]>="0" and s[i]<="9"){ i++;} // for neg. expon.
1428          if (b-i) { t = t + "^{" + s[b,i-b] + "}";}
1429        }
1430          if (i-b > k) { mz = mz + 5*(i-b);}
1431        else {mz = mz + 5*k;}
1432       if (s[i]=="*"){i++;}
1433       b=i;
1434        }
1435      c =c+t;
1436      }
1437   }
1438  short = saveshort;
1439  return(c,j);
1440}
1441example
1442{ "EXAMPLE:"; echo =2;
1443  ring r=(0,a,b),x,dp;
1444  poly f = (a2b12 + 23a2 -b13-1);
1445  f;
1446  parst(string(f));
1447 
1448  f =(-a +4b2 -2);
1449  f;
1450  parst(string(f));
1451 
1452  f = a23;
1453  f;
1454  parst(string(f));
1455  f = 2a12b3 -4ab15 +2a4b12 -2;
1456  short =0;
1457  f;
1458  parst(string(f));
1459   ring r2=(0,a1,b1),x,dp;
1460  poly f = 2*a1^12*b1^3 -4*a1*b1^15 +2*a1^4*b1^12 -2;
1461  f;
1462  parst(string(f));
1463}
1464
1465
1466proc parselong(string s)
1467{
1468  int i,j,k,b,mz;
1469  string t,vn;              // varname
1470 
1471 // "s=" + s;
1472  i = 1;
1473  while (s[i] <> "!")
1474  { b=i;     
1475   
1476// -- scan now the letter ...
1477
1478  //  while(s[i]!="!" and )
1479
1480// alternativ:
1481 while((s[i]>="a" and s[i]<="z") or (s[i]>="A" and s[i]<="Z"))
1482 { i++;}
1483 // s[i]; i;
1484   k = i-b;
1485   vn = s[b,k];
1486
1487   if (defined(TeXreplace))
1488   { for (j=1; j<= size(TeXreplace);j++)
1489     { if (vn == TeXreplace[j][1])
1490       {vn = TeXreplace[j][2]; k=1;
1491        if (s[i]=="*") {vn = vn + " ";}
1492         break;} //suppose replacing by a single sign
1493     }
1494   }
1495   t = t + vn;
1496   mz = mz + 10*k;
1497   if (s[i]=="_"  or s[i]=="(") { i++;}    // the index is coming
1498   b = i;
1499   while(s[i]>="0" and s[i]<="9"){ i++;}
1500   j = i-b;
1501   if (j){ t = t + "_{" +s[b,j] + "}";}
1502   if(s[i]==")") {i++;}
1503   if (s[i]=="^")
1504   { i++; b = i;
1505     while(s[i]>="0" and s[i]<="9" or s[i]=="-")
1506     { i++;}  // for neg. expon.
1507     if (b-i) { t = t + "^{" + s[b,i-b] + "}";}
1508   }
1509   if (i-b > j) { mz = mz + 5*(i-b);}
1510   else {mz = mz + 5*j;}
1511   if (s[i]=="*"){i++;}
1512  }
1513  return(t,mz);
1514}
1515example
1516{ "EXAMPLE:"; echo =2; 
1517  ring r =(49,a),x,dp;
1518  number f = a13;
1519  parsg(string(f));
1520  list TeXreplace; export TeXreplace;
1521  TeXreplace[1] = list("b","\\beta");
1522  TeXreplace[2] = list("a","\\alpha");
1523  TeXreplace[3] = list("c","\\gamma");
1524  parselong(string(f)+"!");
1525}
1526///////////////////////////////////////////////////////////////////////////////
1527
Note: See TracBrowser for help on using the repository browser.