Ticket #287: latex.lib

File latex.lib, 85.0 KB (added by gorzel, 14 years ago)
Line 
1///////////////////////////////////////////////////////////////////////////////
2version="$Id: latex.lib 12231 2009-11-02 10:12:22Z hannes $";
3category="Visualization";
4info="
5LIBRARY: latex.lib    Typesetting of Singular-Objects in LaTeX2e
6AUTHOR: Christian Gorzel, gorzelc@math.uni-muenster.de
7
8PROCEDURES:
9 closetex(fnm)       writes closing line for LaTeX-document
10 opentex(fnm)        writes header for LaTeX-file fnm
11 tex(fnm)            calls LaTeX2e for LaTeX-file fnm
12 texdemo([n])        produces a file explaining the features of this lib
13 texfactorize(fnm,f) creates string in LaTeX-format for factors of polynomial f
14 texmap(fnm,m,r1,r2) creates string in LaTeX-format for map m:r1->r2
15 texname(fnm,s)      creates string in LaTeX-format for identifier
16 texobj(l)           creates string in LaTeX-format for any (basic) type
17 texpoly(f,n[,l])    creates string in LaTeX-format for poly
18 texproc(fnm,p)      creates string in LaTeX-format of text from proc p
19 texring(fnm,r[,l])  creates string in LaTeX-format for ring/qring
20 rmx(s)              removes .aux and .log files of LaTeX-files
21 xdvi(s)             calls xdvi for dvi-files
22        (parameters in square brackets [] are optional)
23        (Procedures with file output assume sufficient write permissions
24        when trying to append existing or create new files.)
25
26GLOBAL VARIABLES:
27  TeXwidth, TeXnofrac, TeXbrack, TeXproj, TeXaligned, TeXreplace, NoDollars
28  are used to control the typesetting.
29  Call @code{texdemo();} to obtain a LaTeX2e file @code{texlibdemo.tex}
30  explaining the features of @code{latex.lib} and its global variables.
31@format
32  @code{TeXwidth} (int) -1, 0, 1..9, >9:  controls breaking of long polynomials
33  @code{TeXnofrac} (int) flag:  write 1/2 instead of \\frac@{1@}@{2@}
34  @code{TeXbrack} (string) \"@{\", \"(\", \"<\", \"|\", empty string:
35                                   controls brackets around ideals and matrices
36  @code{TeXproj} (int) flag:  write \":\" instead of \",\" in vectors
37  @code{TeXaligned} (int) flag:  write maps (and ideals) aligned
38  @code{TeXreplace} (list) list entries = 2 strings:  replacing symbols
39  @code{NoDollars} (int) flag:  suppresses surrounding $ signs
40@end format
41";
42
43LIB "inout.lib";  // only needed for pause();
44
45///////////////////////////////////////////////////////////////////////////////
46
47proc closetex(string fname, list #)
48"USAGE:   closetex(fname); fname string
49RETURN:  nothing; writes a LaTeX2e closing line into file @code{<fname>}.
50NOTE:    preceding \">>\" are deleted and suffix \".tex\" (if not given)
51         is added to @code{fname}.
52EXAMPLE: example closetex; shows an example
53"
54{
55  string default = "latex2e";
56  string s;
57  int i = 1;
58  int flag;
59
60  if (size(#)) { default = #[1];}
61
62  if (default=="latex2e" or default == "latex")
63   {  s = "\\end{document}"; flag = 1;}
64  if (default=="amstex") {s = "\\enddocument"; flag = 1;}
65  if (default=="tex") {s = "\\bye"; flag = 1;}
66  if (not(flag)) { s = "";}
67
68  if (size(fname))
69  {
70   while (fname[i]==">"){i++;}
71   fname = fname[i,size(fname)-i+1];
72
73   if (size(fname)>=4)               // check if filename is ending with ".tex"
74   { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
75   }
76   else {fname = fname + ".tex";}
77   write(fname, s);
78   write(fname," % - Thanks latex.lib and Singular - ");
79  }
80  else {return(s);}
81}
82example
83{ "EXAMPLE:"; echo=2;
84   opentex("exmpl");
85   texobj("exmpl","{\\large \\bf hello}");
86   closetex("exmpl");
87   echo=0;
88}
89///////////////////////////////////////////////////////////////////////////////
90
91proc tex(string fname, list #)
92"USAGE:   tex(fname); fname string
93RETURN:  nothing; calls latex (LaTeX2e) for compiling the file fname
94NOTE:    preceding \">>\" are deleted and suffix \".tex\" (if not given)
95         is added to @code{fname}.
96EXAMPLE: example tex; shows an example
97"
98{
99  string default = "latex2e";
100  int retval;
101  int i=1;
102  if (size(#)) {default = string(#[1]);}
103  if (size(fname))
104  {
105   while (fname[i]==">"){i++;}
106   fname = fname[i,size(fname)-i+1];
107
108   if (size(fname)>=4)          // check if filename is ending with ".tex"
109   { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
110   }
111   else {fname = fname + ".tex";}
112   "calling "+default+" for:",fname,newline;
113
114   if (default=="latex2e")
115   {
116     retval = system("sh","latex " +  fname);
117   }
118   else
119   {
120     retval = system("sh",default + " " +  fname);
121   }
122  }
123  else
124  { " // -- Need a filename ";
125    return();
126  }
127}
128example
129{ "EXAMPLE:"; echo =2;
130  ring r;
131  ideal I = maxideal(7);
132  opentex("exp001");              // open latex2e document
133  texobj("exp001","An ideal ",I);
134  closetex("exp001");
135  tex("exp001");
136  echo=0;
137  pause("the created files will be deleted after pressing <RETURN>");
138  system("sh","rm exp001.*");
139}
140///////////////////////////////////////////////////////////////////////////////
141
142proc opentex(string fname, list #)
143"USAGE:   opentex(fname); fname string
144RETURN:  nothing; writes a LaTeX2e header into a new file @code{<fname>}.
145NOTE:    preceding \">>\" are deleted and suffix \".tex\" (if not given)
146         is added to @code{fname}.
147EXAMPLE: example opentex; shows an example
148"
149{
150  string default = "latex2e";
151  string s;
152  int i =1;
153  int flag;
154
155  if (size(#)) { default = #[1];}
156
157  if (default == "latex2e")          // the default  latex2e header
158  { s =
159      "\\documentclass{article}" + newline +
160      "\\usepackage{amsmath,amssymb}" + newline +
161     "%\\setlength{\\textwidth}{390pt}" + newline +
162      "\\parindent=0pt" + newline +
163      "\\newcommand{\\C}{\\mathbb{C}}" + newline +
164      "\\newcommand{\\F}{\\mathbb{F}}" + newline +
165      "\\newcommand{\\N}{\\mathbb{N}}" + newline +
166   // "\\renewcommand{\\P}{{\\Bbb P}}" + newline +
167      "\\newcommand{\\Q}{\\mathbb{Q}}" + newline +
168      "\\newcommand{\\R}{\\mathbb{R}}" + newline +
169      "\\newcommand{\\T}{\\mathbb{T}}" + newline +
170      "\\newcommand{\\Z}{\\mathbb{Z}}" + newline + newline +
171      "\\begin{document}";
172    flag = 1;
173  }
174  if (default == "latex")
175  { s =
176      "\\documentstyle[12pt,amstex]{article}" + newline +
177      "\\parindent=0pt" + newline +
178      "\\newcommand{\\C}{\\mathbb{C}}" + newline +
179      "\\newcommand{\\F}{\\mathbb{F}}" + newline +
180      "\\newcommand{\\N}{\\mathbb{N}}" + newline +
181   // "\\newcommand{\\P}{\\mathbb{P}}" + newline +
182      "\\newcommand{\\Q}{\\mathbb{Q}}" + newline +
183      "\\newcommand{\\R}{\\mathbb{R}}" + newline +
184      "\\newcommand{\\T}{\\mathbb{T}}" + newline +
185      "\\newcommand{\\Z}{\\mathbb{Z}}" + newline + newline +
186      "\\begin{document}";
187    flag = 1;
188  }
189  if (default == "amstex")
190  { s =
191     "\\documentstyle{amsppt} " + newline + newline +
192     "\\document";
193    flag = 1;
194  }
195
196  if (default == "tex")
197  { s =
198     "";
199    flag = 1;
200  }
201  if (default == "own")            // the proper own header
202  { s = "";
203    flag = 1;
204  }
205  if (not(flag)) { s = "";}
206
207  if (size(fname))
208  {
209   while (fname[i]==">"){i++;}
210   fname = fname[i,size(fname)-i+1];
211
212   if (size(fname)>=4)               // check if filename is ending with ".tex"
213   { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
214   }
215   else {fname = fname + ".tex";}
216   fname = ">" + fname;
217   write(fname,s);
218  }
219  else {return(s);}
220}
221example
222{ "EXAMPLE:"; echo=2;
223   opentex("exmpl");
224   texobj("exmpl","hello");
225   closetex("exmpl");
226}
227///////////////////////////////////////////////////////////////////////////////
228
229proc texdemo(list #)
230"USAGE:   texdemo();
231RETURN:  nothing; generates a LaTeX2e file called @code{texlibdemo.tex}
232         explaining the features of @code{latex.lib} and its global variables.
233"
234{ int TeXdemostart = system("pid");
235  string fname = "texlibdemo";
236
237  if (size(#))
238  { if (typeof(#[1])=="int") {TeXdemostart = #[1];}
239  }
240  system("--random",TeXdemostart);
241
242  //if (size(#) ==2)
243  //{ if (typeof(#[2]) == "string") { fname = #[2];}
244  //}
245
246    if (size(fname))
247    {
248     if (size(fname)>=4)           // check if filename is ending with ".tex"
249     { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
250     } else {fname = fname + ".tex";}
251    }
252
253  // -------- save global variables ---------
254  if (defined(TeXaligned)) {int Teali=TeXaligned; kill TeXaligned;}
255  if (defined(TeXreplace)) {list Terep=TeXreplace; kill TeXreplace;}
256  if (defined(TeXwidth)) {int Tewid=TeXwidth; kill TeXwidth;}
257  if (defined(TeXbrack)) {string Tebra=TeXbrack; kill TeXbrack;}
258  if (defined(TeXnofrac)) {int Tenof=TeXnofrac; kill TeXnofrac;}
259  if (defined(TeXproj)) {int Tepro=TeXproj; kill TeXproj;}
260  // ----------------------------------------
261   print(" Generating demofile .");
262   part0(fname); print(" .");
263   part1(fname); print(" .");
264   part2(fname); print(" .");
265   part3(fname); print(" .");
266   print(" Demofile generated.");
267   if (size(fname))
268   {
269    print(" Call latex now by tex(\"texlibdemo\");" );
270    print(" To view the file, call xdvi by xdvi(\"texlibdemo\");" );
271    // print(" Call latex now by tex(\"" + fname + "\");" );
272    //print(" To view the file, call xdvi by xdvi(\"" + fname + "\");" );
273    print(" .log and .aux files may be deleted with rmx(\"texlibdemo\");");
274   }
275  // -------- save global variables ---------
276  if (defined(Teali)) {int TeXaligned=Teali; export TeXaligned; kill Teali;}
277  if (defined(Terep)) {list TeXreplace=Terep; export TeXreplace; kill Terep;}
278  if (defined(Tewid)) {int TeXwidth=Tewid; export TeXwidth; kill Tewid;}
279  if (defined(Tebra)) {string TeXbrack=Tebra; export TeXbrack; kill Tebra;}
280  if (defined(Tenof)) {int TeXnofrac=Tenof; export TeXnofrac; kill Tenof;}
281  if (defined(Tepro)) {int TeXproj=Tepro; export TeXproj; kill Tepro;}
282  // ----------------------------------------
283
284  return();
285}
286///////////////////////////////////////////////////////////////////////////////
287
288proc texfactorize(string fname, poly f, list #)
289"USAGE:   texfactorize(fname,f); fname string, f poly
290RETURN:  if @code{fname=\"\"}: string, f as a product of its irreducible
291         factors@*
292         otherwise: append this string to the file @code{<fname>}, and
293         return nothing.
294NOTE:    preceding \">>\" are deleted and suffix \".tex\" (if not given)
295         is added to @code{fname}.
296EXAMPLE: example texfactorize; shows an example
297"
298{
299  def @r = basering;
300  list l;
301  int i,j,k,Tw,TW,ND;;
302  intvec v;
303  string s,t;
304  string D = "$";
305  poly g;
306
307  ND = defined(NoDollars);
308 
309  if (!(ND)) {int NoDollars; export NoDollars;}
310  else { D = ""; }
311  TW = defined(TeXwidth);
312  if (TW) {Tw = TeXwidth; TeXwidth = -1;}
313  else {int TeXwidth = -1; export TeXwidth;}
314
315   l = factorize(f); 
316   if (l[1][1]<>1){s = texpoly("",l[1][1]);}
317   if (size(l[1])>1)
318   {
319    if (l[1][1]!=-1 and l[1][1]!=1) { s = texpoly("",l[1][1]); }
320    if (l[1][1]==-1) { s = "-";}
321   }
322   else
323   {
324     s = texpoly("",l[1]);
325   }
326
327   for(i=2;i<=size(l[1]);i++)
328   {
329    if(size(s)){s = s+"\\cdot ";}
330    g = l[1][i];
331    v = leadexp(g);
332    k=0;
333    for(j=1;j<=size(v);j++){k = k + v[j];}
334    if(size(g)>1 or (size(g)==1 and k>1))
335    { t = "(" + texpoly("",l[1][i]) + ")";}
336    else { t =  texpoly("",l[1][i]);}
337    if (l[2][i]>1)
338    { t = t+"^{" +string(l[2][i]) + "}";}
339    s = s + t;
340   }
341   if (!(ND)) { kill Latex::NoDollars;}
342   s = D + s + D;
343   if (TW) {TeXwidth = Tw;}
344   if (!TW) {kill Latex::TeXwidth;}
345
346  if(size(fname))
347  { i=1;
348    while (fname[i]==">"){i++;}
349    fname = fname[i,size(fname)-i+1];
350
351    if (size(fname)>=4)             // check if filename is ending with ".tex"
352    { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
353    }
354    else {fname = fname + ".tex";}
355    write(fname,s);
356  }
357  else{return(s);}
358}
359example
360{ "EXAMPLE:"; echo=2;
361  ring r2 = 13,(x,y),dp;
362  poly f = (x+1+y)^2*x3y*(2x-2y)*y12;
363  texfactorize("",f);
364  ring R49 = (7,a),x,dp;
365  minpoly = a2+a+3;
366  poly f = (a24x5+x3)*a2x6*(x+1)^2;
367  f;
368  texfactorize("",f);
369}
370///////////////////////////////////////////////////////////////////////////////
371
372proc texmap(string fname, def m, def @r1, def @r2, list #)
373"USAGE:   texmap(fname,m,@r1,@r2); fname string, m string/map, @r1,@r2 rings
374RETURN:  if @code{fname=\"\"}: string, the map m from @r1 to @r2 (preceded
375         by its name if m = string) in TeX-typesetting;@*
376         otherwise: append this string to the file @code{<fname>}, and
377         return nothing.
378NOTE:    preceding \">>\" are deleted in @code{fname}, and suffix \".tex\"
379         (if not given) is added to @code{fname}.
380         If m is a string then it has to be the name of an existing map
381         from @r1 to @r2.
382EXAMPLE: example texmap; shows an example
383"
384{
385  int saveDollars= defined(NoDollars);
386  int TX = defined(TeXwidth);
387  int Tw;
388  int i,n;
389  string r1str,r2str, varr1str, varr2str;
390  string mapname,t,s;
391  string D,DD,vrg = "$","$$",",";
392  def @r = basering;
393  def themap;
394  list l1,l2;
395  string rr1,rr2 = "@r1","@r2";
396
397// --- store all actual informations
398  if(TX) { Tw = TeXwidth; TeXwidth = -1;}
399  else { int TeXwidth = -1; export TeXwidth;}
400  if (!(saveDollars)) { int  NoDollars; export NoDollars;}
401  if (defined(TeXproj)) {vrg = ":";}
402
403  if (size(#))
404  { if (typeof(#[1])=="list")
405    { l1 = #[1];
406      if(size(#)==2) { l2 = #[2];}
407    }
408    else {l1=#; l2 =#;}
409  }
410// --- tex the information in preimring r1
411
412  setring(@r1);
413  r1str = texring("",@r1,l1);
414// --- avoid an execute; hence construct an ideal
415
416  n = nvars(@r1);
417  if (n>1) { t = "\\left(";}
418  ideal @I = var(1);
419  t = t + texpoly("",var(1));
420  for(i=2;i<=n;i++)
421  { @I = @I + var(i);
422    t = t + vrg + texpoly("",var(i));
423  }
424  if (n>1) { t = t + "\\right)";}
425  varr1str = t;
426
427// --- now the things in ring ring r2
428
429  setring(@r2);
430 // listvar();
431
432  if (typeof(m)=="string")
433  { themap = `m`;
434    mapname = m;
435    if (defined(TeXreplace))
436    {
437     //mapname = rp(mapname);   // rp ausschreiben !
438     for(int ii=1;ii<=size(TeXreplace);ii++)
439     {
440      if (TeXreplace[ii][1]==mapname) {mapname= TeXreplace[ii][2]; break;}
441     }
442    }
443    mapname = mapname + ":";
444  }
445  if (typeof(m)=="map") { themap = m;}
446
447  r2str = texring("",@r2,l2);
448  ideal @J  = themap(@I);
449  n = size(matrix(@J));
450  if (n>1) { t = " \\left(";}
451  if (!(defined(TeXaligned)) and (n>1))
452      { t = t + newline + "\\begin{array}{c}" + newline;}
453  t = t + texpoly("",@J[1]);
454  for (i=2;i<=n; i++)
455  {if(defined(TeXaligned))
456   { t = t + vrg + texpoly("",@J[i]); }
457   else { t = t + "\\\\" + newline + texpoly("",@J[i]);}
458  }
459  if (!(defined(TeXaligned)) and (n>1))
460      { t = t + newline + "\\end{array}" + newline;}
461  if (n>1) {t = t + "\\right)";}
462  varr2str = t;
463
464// --- go back to  ring r1 to kill @I
465
466  setring(@r1);
467  kill @I;
468
469// --- now reset the old settings and stick all the information together
470
471  setring(@r);
472  if (!(saveDollars)) { kill Latex::NoDollars;}
473  if (TX) {TeXwidth = Tw;}
474  else { kill Latex::TeXwidth;}
475  if (defined(NoDollars))
476  { D,DD = "",""; }
477
478  if (defined(TeXaligned))
479  { s = D + mapname;
480    s =  s + r1str + "\\longrightarrow" + r2str + ", \\ " +
481        varr1str + "\\longmapsto" + varr2str + D; }
482  else
483  { s = DD;
484    s = s + newline + "\\begin{array}{rcc}" +  newline;
485    s = s + mapname + r1str + " & \\longrightarrow & " +  r2str + "\\\\[2mm]"
486          + newline;
487    s = s + varr1str + " & \\longmapsto & " +  newline + varr2str + newline;
488    s = s + "\\end{array}" + newline;
489    s = s +  DD;
490  }
491
492  if (size(fname))
493  { i=1;
494    while (fname[i]==">"){i++;}
495    fname = fname[i,size(fname)-i+1];
496
497    if (size(fname)>=4)          // check if filename is ending with ".tex"
498    { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
499    }
500    else {fname = fname + ".tex";}
501    write(fname,s);
502  }
503  else {return(s);}
504}
505example
506{
507  echo=0;
508  // -------- prepare for example ---------
509  if (defined(TeXaligned)) {int Teali=TeXaligned; kill TeXaligned;}
510  if (defined(TeXreplace)) {list Terep=TeXreplace; kill TeXreplace;}
511  // -------- the example starts here ---------
512  //
513  "EXAMPLE:"; echo = 2;
514  string fname = "tldemo";
515  ring @r1=0,(x,y,z),dp;
516  export @r1;
517  ring r2=0,(u,v),dp;
518  map @phi =(@r1,u2,uv -v,v2); export @phi;
519  list TeXreplace;
520  TeXreplace[1] = list("@phi","\\phi");    // @phi --> \phi
521  export TeXreplace;
522  texmap("","@phi",@r1,r2);                // standard form
523  //
524  int TeXaligned; export TeXaligned;       // map in one line
525  texmap("",@phi,@r1,r2);
526  //
527  kill @r1,TeXreplace,TeXaligned;
528  echo = 0;
529  //
530  // --- restore global variables if previously defined ---
531  if (defined(Teali)) {int TeXaligned=Teali; export TeXaligned; kill Teali;}
532  if (defined(Terep)) {list TeXreplace=Terep; export TeXreplace; kill Terep;}
533}
534///////////////////////////////////////////////////////////////////////////////
535
536static proc manipul(string s)
537{
538  string st;
539  int i,anf,end,op,bigch;
540  int n;
541  if (s[1]=="{") { return(s[2,size(s)-2]);}
542  if (s=="") { return(s);}
543  s = s + newline;             // add a terminating sign
544  anf=1;
545  while(s[i]!=newline)
546  {
547    i =anf;
548    while(s[i]<"0" or s[i]>"9" and s[i]!="'" and s[i]!= "_" and s[i]!="~" and
549          s[i]!="(" and s[i]!=")" and s[i]!= "[" and s[i]!=newline) {i++;}
550    if (s[i]==newline){st = st + s[anf,i-anf]; n = n+10*(i-anf); return(st);}
551    st = st + s[anf,i-anf];                        // the starting letters
552    if (s[anf]>="A" and s[anf]<="Z") {bigch=1;}
553    if (s[i]=="'") { st = st + "'";i++;}
554    if (s[i]=="~") { st = "\\tilde{" + st + "}"; i++;}
555    if (s[i]=="_") { i++;}
556    if (s[i]=="(") { op =1;i++;}
557    if (s[i]=="[") { anf = i+1;
558    while(s[i]!="]"){i++;}                    // matrices and vectors
559    st = st + "_{" + s[anf,i-anf] + "}"; n = n+ 5*(i-anf); i++;
560   // besser: while s[i]<> nwline : scan forward: end, return
561  }
562  if (s[i]==newline) {return(st);}
563  anf =i;
564  while (s[i]>="0" and s[i]<="9") {i++;}  // parse number after the letters
565  if (bigch and not(op)) { st = st + "^{" + s[anf,i-anf] + "}"; bigch =0;}
566  else { st = st + "_{" + s[anf,i-anf] + "}";}
567  n = n+5*(i-anf);
568  anf =i;            // the next text in ( , ) as exponent
569  if (op)
570  {
571    if (s[i]== ","){anf = anf+1;}
572    while(s[i] !=")"){ i++;}
573    if (i<>anf)
574      {
575        st = st + "^{" + s[anf,i-anf] + "}"; n = n +5*(i-anf);}
576        i++;
577      }
578    anf =i;
579  }
580  return(st);
581}
582///////////////////////////////////////////////////////////////////////////////
583
584proc texname(string fname, string s)
585"USAGE:   texname(fname,s);  fname,s  strings
586RETURN:  if @code{fname=\"\"}: the transformed string s, for which the
587         following rules apply:
588@example
589      s' + \"~\"             -->  \"\\tilde@{\"+ s' +\"@}\"
590     \"_\" + int             -->       \"_@{\" + int +\"@}\"
591  \"[\" + s' + \"]\"           -->      \"_@{\" + s' + \"@}\"
592   \"A..Z\" + int            --> \"A..Z\" + \"^@{\" + int + \"@}\"
593   \"a..z\" + int            --> \"a..z\" + \"_@{\" + int + \"@}\"
594\"(\" + int + \",\" + s' + \")\" --> \"_@{\"+ int +\"@}\" + \"^@{\" + s'+\"@}\"
595@end example
596         Furthermore, strings which begin with a left brace are modified
597         by deleting the first and the last character (which is then assumed to
598         be a right brace).
599
600         if @code{fname!=\"\"}: append the transformed string s to the file
601         @code{<fname>}, and return nothing.
602NOTE:    preceding \">>\" are deleted in @code{fname}, and suffix \".tex\"
603         (if not given) is added to @code{fname}.
604EXAMPLE: example texname; shows an example
605"
606{
607  string st, extr;
608  st=manipul(s);
609  if (size(fname))
610  {
611    int i=1;
612    while (fname[i]==">"){i++;}
613    fname = fname[i,size(fname)-i+1];
614    if (size(fname)>=4)            // check if filename is ending with ".tex"
615    {
616      if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
617    }
618    else {fname = fname + ".tex";}
619    write(fname,st);
620  }
621  else {return(st);}
622}
623example
624{ "EXAMPLE:"; echo =2;
625   ring r = 0,(x,y),lp;
626   poly f = 3xy4 + 2xy2 + x5y3 + x + y6;
627   texname("","{f(10)}");
628   texname("","f(10) =");
629   texname("","n1");
630   texname("","T1_12");
631   texname("","g'_11");
632   texname("","f23");
633   texname("","M[2,3]");
634   texname("","A(0,3);");
635   texname("","E~(3)");
636}
637///////////////////////////////////////////////////////////////////////////////
638
639proc texobj(string fname, list #)
640"USAGE:   texobj(fname,l); fname string, l list
641RETURN:  if @code{fname=\"\"}: string, the entries of l in LaTeX-typesetting;@*
642         otherwise: append this string to the file @code{<fname>}, and
643         return nothing.
644NOTE:    preceding \">>\" are deleted in @code{fname}, and suffix \".tex\"
645         (if not given) is added to @code{fname}.
646EXAMPLE: example texobj; shows an example
647"
648{
649 int i,j,k,nr,nc,linear,Tw,Dollars;
650 int ND = defined(NoDollars);
651 int TW = defined(TeXwidth);
652
653 if(defined(basering)){ poly g,h; matrix M;}
654 string s,t,l,ineq,sg,Iname;
655 string sep= ",";
656 string D,DA,DE = "$","$$"+newline,"$$";
657 string OB,CB = "(",")";
658 if (defined(TeXbrack))
659 {// if (TeXbrack=="(") {OB = "("; CB = ")";}
660   if (TeXbrack=="<") {OB = "<"; CB = ">";}
661   if (TeXbrack=="{") {OB = "{"; CB = "}";}
662   if (TeXbrack=="|") {OB = "|"; CB = "|";}
663   if (TeXbrack=="" ) {OB = "."; CB = ".";}
664 }
665
666 if (!(TW)) { int TeXwidth = -1; export TeXwidth; }
667 Tw = TeXwidth;
668
669 if (defined(TeXproj)){ sep = ":";}
670 if(ND) { D,DA,DE="","","";}
671 else {int NoDollars; export NoDollars;}
672
673 if (size(#)==1)
674 { if (typeof(#[1])=="int" or typeof(#[1])=="intvec" or typeof(#[1])=="vector"
675    or typeof(#[1])=="number" or typeof(#[1])=="bigint" or defined(TeXaligned))
676   { DA = D; DE = D; }
677}
678
679 s = DA;
680
681 for (k=1; k<=size(#); k++)
682 { def obj = #[k];
683   if (typeof(obj) == "string")
684   { if (defined(`obj`))
685     { if (typeof(`obj`)=="ideal")
686       {
687         Iname = obj; def e = `obj`;     //convert to correct type ideal
688         kill obj; def obj = e; kill e;
689       }
690       else {s = s + obj + newline;}
691     }
692    else {s = s + obj + newline;}
693   }
694   if (typeof(obj) == "int" or typeof(#[1])=="bigint")
695   { s = s + "  " + string(obj) + "  ";}
696
697   if (typeof(obj) == "intvec")
698   { s = s + " (";
699     for(j=1; j<size(obj);j++) { s = s + string(obj[j]) + sep;}
700     s = s +  string(obj[j]) + ") ";
701   }
702
703   if (typeof(obj) == "number" )
704   { s = s + texpoly("",obj) + newline;
705   }
706
707   if (typeof(obj) == "poly")
708   { int TeXdisplay; export TeXdisplay;
709     s = s + "\\begin{array}{rl}" + newline;
710     s = s + texpoly("",obj) + "\\\\" + newline;
711     s = s + "\\end{array}" + newline;
712    kill TeXdisplay;
713   }
714
715   if (typeof(obj) == "vector")
716   { if (obj==0) { s = s  + "0" ;}
717     else
718     { if (Tw==0) { TeXwidth = -1;}
719      s = s + "\\left" + OB;
720      for(j=1; j<nrows(obj); j++) {s = s + texpoly("",obj[j]) + sep;}
721      s = s + texpoly("",obj[j])  + "\\right" + CB + newline;
722      TeXwidth = Tw;
723     }
724    }
725
726   if (typeof(obj) == "ideal")
727   { if (size(Iname))   // verwende hier align
728     { if (Tw==0) {TeXwidth = -1;}
729
730      // Lasse hier TeXwidth == 0 zu !
731      // if (!(defined(TeXaligned)))
732      //  { untereinander }
733      // else { hintereinander }
734      //
735      //
736      //  s = s + Iname + "=" + texpoly("",obj,",");
737      //  siehe ebenso noch einmal am Ende : ! (TeXwidth <> 0 !? )
738
739       s =  s + "\\begin{array}{rcl}" + newline;
740       for (i=1;i<=size(matrix(obj));i++)
741       { s =  s + Iname+ "_{" + string(i) + "} & = & "
742               + texpoly("",obj[i]);
743         if (i<size(matrix(obj))){ s = s  + "\\\\" + newline;}
744       }
745       s = s + newline;
746       s = s + "\\end{array}" + newline;
747       TeXwidth = Tw;
748       Iname ="";
749     }
750     else
751     {
752      if (TeXwidth==0)
753      { TeXwidth = -1;
754        obj= simplify(obj,2);
755        linear = 1;
756        for (j=1;j<=size(obj);j++)
757        { if (deg(obj[j])>1){linear =0; break;}
758        }
759        if (!(linear))
760        { s = s + "\\begin{array}{rcl}" + newline;
761          for(j=1;j<=size(obj);j++)
762          { h = jet(obj[j],0);  // absterm
763            ineq = attrib(obj[j],"ineq");
764            if(!(size(ineq))) { ineq = "=" ; }
765            l = texpoly("",obj[j]-h) + " & " + ineq + " & " + texpoly("",-h);
766            if(j<size(obj)) { l = l + " \\\\";}
767            s =s+ l + newline;
768           }
769          s = s + "\\end{array}" + newline;
770        }
771        else   // linear
772        { s = s +
773   "\\begin{array}{*{" + string(2*nvars(basering)-1) + "}{c}cr}" + newline;
774           for(j=1; j<=size(obj);j++)
775           { h = jet(obj[j],0);   // absterm
776             ineq = attrib(obj[j],"ineq");
777             if(!(size(ineq))) { ineq = "=" ; }
778              l = ""; nc = 0;
779              for (i=1; i<=nvars(basering);i++)
780              { t = " "; sg ="";
781                g = obj[j]-subst(obj[j],var(i),0);
782                if (g!=0) { t = texpoly("",g);}
783                if (i>1)
784                { if (t[1]!="-" and t[1]!= " " and nc ){sg = "+";}
785                  if  (t[1]=="-") { sg = "-"; nc =1; t=t[2,size(t)-1];}
786                  if (t==" ") {sg ="";}
787                  l = l + " & " + sg + " & " + t;
788                }
789                else { l = t;}
790                if (g!=0) {nc = 1;}
791               }
792
793               l = l + " & " + ineq + " & " + texpoly("",-h);
794             if (j < size(obj)) { l = l + " \\\\";}
795             s = s + l + newline;
796            } // end for (j)
797          s = s + "\\end{array}";
798         }  // end else linear
799        TeXwidth = 0;
800       } // end TeXwidth == 0
801   else // TeXwidth <> 0
802   { s =  s + "\\left"+ OB;
803     if (defined(TeXaligned))
804     { s = s + texpoly("",obj,",");
805     }
806     else
807     { s = s + newline + "\\begin{array}{c}" + newline +
808               texpoly("",obj,", \\\\" + newline) +
809                newline + "\\end{array}" + newline;
810     }
811    s = s + "\\right" + CB;
812    } // end TeXwidth <> 0
813   }  // not Iname
814// s;
815  }
816
817   if (typeof(obj) == "module")
818   { M = matrix(obj);
819     if (Tw ==0 or Tw > 9) { TeXwidth = -1;}
820     s = s + "\\left" + OB + newline;
821     if (!(defined(TeXaligned)))
822     {  // Naechste Zeile nicht notwendig !
823     // s = s + "\\begin{array}{*{"+ string(ncols(M)) + "}{c}}" + newline;
824      for(j=1;j<=ncols(M);j++)
825      { l = "\\left" + OB + newline + "\\begin{array}{c}" + newline;
826        l = l + texpoly("",ideal(M[1..nrows(M),j]), " \\\\" + newline)
827              + newline + "\\end{array}" +newline + "\\right" + CB + newline;
828        if (j< ncols(M)) { l = l + " , " + newline;}
829        s = s + l ;
830      }
831     }
832     else    // TeXaligned
833     {
834      for(j=1;j<=ncols(M);j++)
835      { s = s + "\\left" + OB + newline +
836           texpoly("",ideal(M[1..nrows(M),j]),",") + newline + "\\right" + CB;
837        if (j<ncols(M)) { s = s + "," + newline; }
838      }
839     }
840    s = s  + "\\right" + CB + newline;
841   } // module part
842
843   if (typeof(obj) == "matrix")
844   { if (Tw==0 or Tw > 9) {TeXwidth = -1;}
845     l = "";
846   //  M = transpose(obj);
847     s = s + "\\left" + OB + newline +
848             "\\begin{array}{*{"+ string(ncols(obj)) + "}{c}" + "}"+ newline;
849     for(i=1;i<=nrows(obj);i++)
850     { l = l + texpoly("",ideal(obj[i,1..ncols(obj)])," & ");
851       if (i<nrows(obj)) { l = l + " \\\\" + newline;}
852     }
853     l = l + newline;
854     s = s + l + "\\end{array}" + newline +
855                 "\\right" + CB + newline;
856    TeXwidth = Tw;
857  }
858
859   if (typeof(obj) == "intmat")
860   { nr,nc = nrows(obj),ncols(obj);
861     l = "";
862     l =  "\\left" + OB + newline +
863          "\\begin{array}{*{"+ string(nc) + "}{r}}"+ newline;
864     for(i=1;i<=nr;i++)
865     { for(j=1;j<=nc;j++)
866       { l = l + string(obj[i,j]);
867         if (j <nc ) { l = l + " & ";}
868         else {if( i < nr) { l = l + "\\\\" + newline;}}
869       }
870     }
871     l = l + newline + "\\end{array}" + newline +
872             "\\right" + CB + newline;
873    s = s + l;
874  }
875
876  if (typeof(obj) == "ring" or
877      typeof(obj) == "qring") { s = s + D + texring("",obj) + D + newline;}
878
879  kill obj;
880 }
881
882 s = s +  DE;// + newline;
883
884 if(!(ND)) { kill Latex::NoDollars;}
885 if(!(TW)) { kill Latex::TeXwidth;}
886
887 if(size(fname))
888 { i=1;
889  while (fname[i]==">"){i++;}
890  fname = fname[i,size(fname)-i+1];
891  if (size(fname)>=4)               // check if filename is ending with ".tex"
892  { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
893  }
894  else {fname = fname + ".tex";}
895  write(fname,s);
896 }
897 else {return(s);}
898}
899example
900{
901   echo=0;
902   // -------- prepare for example ---------
903   if (defined(TeXaligned)) {int Teali=TeXaligned; kill TeXaligned;}
904   if (defined(TeXbrack)){string Tebra=TeXbrack; kill TeXbrack;}
905   "EXAMPLE:"; echo = 2;
906   //
907   //  --------------  typesetting for polynomials ----------
908   ring r = 0,(x,y),lp;
909   poly f = x5y3 + 3xy4 + 2xy2 + y6;
910   f;
911   texobj("",f);
912   pause();
913   //  --------------  typesetting for ideals ----------
914   ideal G = jacob(f);
915   G;
916   texobj("",G);
917   pause();
918   //  --------------  variation of typesetting for ideals ----------
919   int TeXaligned = 1; export TeXaligned;
920   string TeXbrack = "<"; export TeXbrack;
921   texobj("",G);
922   pause();
923   kill TeXaligned, TeXbrack;
924   //  --------------  typesetting for matrices ----------
925   matrix J = jacob(G);
926   texobj("",J);
927   pause();
928   //  --------------  typesetting for intmats ----------
929   intmat m[3][4] = 9,2,4,5,2,5,-2,4,-6,10,-1,2,7;
930   texobj("",m);
931   echo=0;
932   //
933   // --- restore global variables if previously defined ---
934   if (defined(Teali)){int TeXaligned=Teali; export TeXaligned; kill Teali;}
935   if (defined(Tebra)){string TeXbrack=Tebra; export TeXbrack; kill Tebra;}
936}
937///////////////////////////////////////////////////////////////////////////////
938
939proc texproc(string fname,string pname)
940"USAGE:   texproc(fname,pname); fname,pname strings
941ASSUME:  @code{`pname`} is a procedure.
942RETURN:  if @code{fname=\"\"}: string, the proc @code{`pname`} in a verbatim
943         environment in LaTeX-typesetting;@*
944         otherwise: append this string to the file @code{<fname>}, and
945         return nothing.
946NOTE:    preceding \">>\" are deleted in @code{fname}, and suffix \".tex\"
947         (if not given) is added to @code{fname}.@*
948EXAMPLE: example texproc; shows an example
949"
950{
951  int i,j,k,nl;
952  string @p,s,t;
953 
954  j = 1;
955 
956  if (defined(`pname`))
957  { if (typeof(`pname`)=="proc")
958    { @p = string(`pname`);
959      nl = find(@p,newline);
960      s = "\\begin{verbatim}" + newline;
961      s = s + "proc " + pname + "(";
962      i = find(@p,"parameter");       // collecting the parameters
963      k = find(@p,"alias");           // and the alias arguments
964      while((i and i < nl) or (k and k < nl))
965      {
966        if (i and (k==0  or i<k))
967        {
968         j=find(@p,";",i);
969         t = @p[i+10,j-i-10];
970         if(i>1){s = s + ",";};
971         s = s + t;
972        }
973        if (k and (i==0  or k<i))
974        {
975         j=find(@p,";",k);
976         t = @p[k,j-k];
977         if(k>1){s = s + ",";};
978         s = s + t;
979        }
980        i = find(@p,"parameter",j);
981        k = find(@p,"alias",j);
982      }
983      s = s + ")" + newline;
984     j++;                      // skip one for the newline
985     i = find(@p,";"+"return();"+newline,j);
986     if (!(i))
987     { i = find(@p,";"+"RETURN();"+newline,j); }  // j kann hier weg
988     s = s + "{" + @p[j,i-j-1] + "}" + newline;
989     s = s + "\\end{verbatim}" + newline;
990   }
991  }
992  else
993  { print(" // -- Error: No such proc defined");
994    return();
995  }
996  if(size(fname))
997  { i=1;
998    while (fname[i]==">"){i++;}
999    fname = fname[i,size(fname)-i+1];
1000    if (size(fname)>=4)        // check if filename is ending with ".tex"
1001    { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
1002    }
1003    else {fname = fname + ".tex";}
1004    write(fname,s);
1005  }
1006  else{return(s);}
1007}
1008example
1009{ "EXAMPLE:"; echo=2;
1010  texproc("","texproc");
1011}
1012
1013///////////////////////////////////////////////////////////////////////////////
1014
1015static proc tvar(intvec v)
1016{
1017  int i,j,ldots;
1018  string s;
1019  j = 1;
1020  s = texpoly("",var(1));
1021
1022  if (nvars(basering)==1) { return(s);}
1023  if (nvars(basering)==2) { return(s + "," + texpoly("",var(2)));}
1024  if (size(v)==1 and v[1] == 1)
1025     {return(s + ",\\ldots,"+ texpoly("",var(nvars(basering))));}
1026  if (v[1]==1 and size(v) >1) {j++;}
1027  for(i=2;i<nvars(basering);i++)
1028  { if (i<v[j]  and !(ldots))
1029    { s = s + ",\\ldots";
1030      ldots =1;
1031    }
1032    if (i== v[j])
1033    { s = s + "," + texpoly("",var(i));
1034      ldots =0;
1035      if (j< size(v)) {j++;}
1036    }
1037  }
1038  if (v[j]<nvars(basering)-1) { s = s + ",\\ldots";}
1039  return(s + "," + texpoly("",var(nvars(basering))));
1040}
1041///////////////////////////////////////////////////////////////////////////////
1042
1043proc texring(string fname, def r, list #)
1044"USAGE:   texring(fname, r[,L]); fname string, r ring, L list
1045RETURN:  if @code{fname=\"\"}: string, the ring in TeX-typesetting;@*
1046         otherwise: append this string to the file @code{<fname>} and
1047         return nothing.
1048NOTE:    preceding \">>\" are deleted and suffix \".tex\" (if not given)
1049         is added to @code{fname}.@*
1050         The optional list L is assumed to be a list of strings which control,
1051         for instance the symbol for the field of coefficients.@*
1052         For more details call @code{texdemo();} (generates a LaTeX2e
1053         file called @code{texlibdemo.tex} which explains all features of
1054         @code{texring}).
1055EXAMPLE: example texring; shows an example
1056"
1057{
1058  int i,galT,intT,flag,mipo,nopar,Dollars,TB,TA;
1059  string ob,cb,cf,en,s,t,savebrack; //opening bracket, closing br, coef.field
1060  intvec v;
1061
1062  setring r;
1063  if (!(defined(NoDollars))){ Dollars = 1; int NoDollars; export NoDollars;}
1064  ob,cb = "[","]";
1065  if (find(ordstr(r),"s")) { ob,cb="[[","]]";}
1066  if(char(r)==0){cf="\\Q";}
1067  if(find(charstr(r),"integer"))    // 4.10.10
1068  { 
1069    intT=1;
1070    cf="\\Z";
1071    if (char(r)>0) { cf=cf+"_{"+string(char(r))+"}";}
1072  }       
1073  if(find(charstr(r),"real")){cf="\\R";}
1074  if(find(charstr(r),"complex")){cf="\\C"; nopar=1;}   // 28.10.06
1075  if(char(r)==prime(char(r))){cf="\\Z_{"+string(char(r))+"}";}
1076  if(char(r)>0 and !intT)
1077  { i = find(charstr(r),",");
1078    if(i)
1079    { t= charstr(r)[1,i-1];
1080      galT = (t <> string(char(r)));
1081      if (galT) { cf = "\\F_{"+ t + "}";}
1082    }
1083  }     // all other cases are cover already by char(r)=? prime(char)
1084
1085  if (size(#))
1086  { if (typeof(#[1])=="list") { # = #[1];}
1087  }
1088  for (i=1;i<=size(#);i++)
1089  { flag =0;
1090    if(typeof(#[i])=="string")
1091    {
1092     if(#[i][1]=="^" or #[i][1]=="_"){en=en+#[i];flag = 1;}
1093     if(#[i]=="mipo"){mipo=1; flag = 1;}
1094     if(#[i]=="{"){ob,cb="\\{","\\}";flag=1;}
1095     if(#[i]=="{{"){ob,cb="\\{\\{","\\}\\}";flag=1;}
1096     if(#[i]=="["){ob,cb="[","]";flag=1;}
1097     if(#[i]=="[["){ob,cb="[[","]]";flag=1;}
1098     if(#[i]=="<"){ob,cb="<",">";flag=1;}
1099     if(#[i]=="<<"){ob,cb="{\\ll}","{\\gg}";flag=1;}
1100     if(#[i]=="C"){cf="\\C";flag=1;}
1101     if(#[i]=="Q"){cf="\\Q";flag=1;}
1102     if((#[i]=="k" or #[i]=="K" or #[i]=="R") and !(galT))
1103                   {cf=#[i]; flag=1; nopar=1;}
1104     if (flag!=1) {cf = #[i];}  // for all the cases not covered here e.g Z_(p)
1105    }                           // or Q[i]
1106
1107    if ((typeof(#[i])=="intvec") or
1108        (typeof(#[i])=="int")){v=#[i];}
1109   }
1110  s = cf;
1111 // now the parameters
1112 // t;
1113  if(npars(r) and ((t==string(char(r))) or char(r)==0) and !(nopar))
1114  {
1115   s = s + "(";                      // !! mit ideal !!
1116   for(i=1;i<npars(r);i++) {s = s + texpoly("",par(i)) + ",";}
1117   s = s + texpoly("",par(npars(r))) + ")";
1118  }                               // parameters done
1119  if (!(galT) and mipo and minpoly!=0)
1120  { s = s + "/" + list(parsp(string(minpoly),0))[1];}
1121  s = s + ob;
1122  if (v!=0 and nvars(r)>3)
1123  { s = s + tvar(v);}
1124  else
1125  { s = s + texpoly("",maxideal(1),","); }
1126   s = s + cb + en;
1127
1128  if (typeof(r)=="qring")
1129  { ideal @I = ideal(r);
1130    if (defined(TeXbrack))
1131    {
1132      TB =1; savebrack = TeXbrack;
1133      if (TeXbrack!= "<" and TeXbrack!="(") { TeXbrack = "<";}
1134    }
1135    TA = defined(TeXaligned);
1136    if (!(TA)) { int TeXaligned; export TeXaligned; }
1137    t = texobj("",@I);
1138 //   @I;
1139 //   t;
1140    if (TB) { TeXbrack = savebrack;}
1141    if (!(TA)) { kill Latex::TeXaligned;}
1142    s = s + "/" + t;
1143  }
1144
1145  if (Dollars)
1146  { kill Latex::NoDollars;
1147    s =  "$" + s + "$";
1148  }
1149  if (size(fname))
1150  { i=1;
1151     while (fname[i]==">"){i++;}
1152     fname = fname[i,size(fname)-i+1];
1153
1154     if (size(fname)>=4)           // check if filename is ending with ".tex"
1155     { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
1156     }
1157     else {fname = fname + ".tex";}
1158     write(fname,s);
1159  }
1160  else{return(s);}
1161}
1162example
1163{ "EXAMPLE:"; echo=2;
1164  ring r0 = 0,(x,y),dp;                // char = 0, polynomial ordering
1165  texring("",r0);
1166  //
1167  ring r7 =7,(x(0..2)),ds;             // char = 7, local ordering
1168  texring("",r7);
1169  //
1170  ring r1 = 0,(x1,x2,y1,y2),wp(1,2,3,4);
1171  texring("",r1);
1172  //
1173  ring rr = real,(x),dp;               // real numbers
1174  texring("",rr);
1175  //
1176  ring rC = complex,x,dp;              // complex coefficients
1177  texring("",rC);
1178  //
1179  pause();
1180
1181  ring rabc =(0,t1,t2,t3),(x,y),dp;    // ring with parameters
1182  texring("",rabc);
1183  //
1184  ring ralg = (7,a),(x1,x2),ds;        // algebraic extension
1185  minpoly = a2-a+3;
1186  texring("",ralg);
1187  texring("",ralg,"mipo");
1188  //
1189  ring r49=(49,a),x,dp;                // Galois field
1190  texring("",r49);
1191  //
1192  setring r0;                          // quotient ring
1193  ideal i = x2-y3;
1194  qring q = std(i);
1195  texring("",q);
1196  //
1197  pause();
1198
1199  // ------------------ additional features -------------------
1200  ring r9 =0,(x(0..9)),ds;
1201  texring("",r9,1);
1202  texring("",r9,"C","{","^G");
1203  //
1204  ring rxy = 0,(x(1..5),y(1..6)),ds;
1205  intvec v = 5,6;
1206  texring("",rxy,v);
1207}
1208
1209///////////////////////////////////////////////////////////////////////////////
1210
1211proc rmx(string fname)
1212"USAGE:   rmx(fname); fname string
1213RETURN:  nothing; removes the @code{.log} and @code{.aux} files associated to
1214         the LaTeX file <fname>.@*
1215NOTE:    If @code{fname} ends by @code{\".dvi\"} or @code{\".tex\"}, the
1216         @code{.dvi} or @code{.tex} file will be deleted, too.
1217EXAMPLE: example rmx; shows an example
1218"
1219{
1220  int i,suffix= 1,0;
1221  int retval;
1222
1223  if (size(fname))
1224  {
1225   while (fname[i]==">"){i++;}
1226   fname = fname[i,size(fname)-i+1];
1227   if (size(fname)>4)
1228   { if (fname[size(fname)-3,4]==".tex") { suffix = 2;}
1229     if (fname[size(fname)-3,4]==".dvi") { suffix = 1; }
1230     if (suffix) { fname = fname[1,size(fname)-4]; }
1231   }
1232   retval = system("sh","rm " + fname + ".aux");
1233   retval = system("sh","rm " + fname + ".log");
1234   if (suffix==2) {retval = system("sh","/bin/rm -i " + fname +".tex");}
1235   if (suffix>=1) {retval = system("sh","/bin/rm -i " + fname +".dvi");}
1236  }
1237  else
1238  {" -- Need a filename ";
1239    return();
1240  }
1241}
1242example
1243{ "EXAMPLE:"; echo =2;
1244  ring r;
1245  poly f = x+y+z;
1246  opentex("exp001");              // defaulted latex2e document
1247  texobj("exp001","A polynom",f);
1248  closetex("exp001");
1249  tex("exp001");
1250  rmx("exp001");   // removes aux and log file of exp001
1251  echo = 0;
1252  pause("remaining files will be deleted after pressing <RETURN>");
1253  echo = 2;
1254  system("sh","rm exp001.*");
1255}
1256///////////////////////////////////////////////////////////////////////////////
1257
1258proc xdvi(string fname, list #)
1259"USAGE:   xdvi(fname[,style]); fname,style = string
1260RETURN:  nothing; displays dvi-file fname.dvi with previewer xdvi
1261NOTE:    suffix .dvi may be omitted in fname
1262         style captures the program that will be called instead of the default (xdvi)
1263EXAMPLE: example xdvi; shows an example
1264"
1265{
1266  int i=1;
1267  int retval;
1268  string default = "xdvi";
1269
1270  if (size(#)) {default = string(#[1]);}
1271
1272  if (size(fname))
1273  {
1274   while (fname[i]==">") {i++;}
1275   fname = fname[i,size(fname)-i+1];
1276
1277   if (size(fname)>=4)
1278   { if(fname[size(fname)-3,4]==".tex") {fname = fname[1,size(fname)-4];}}
1279
1280   "calling ",default, " for :",fname,newline;
1281
1282   if (default=="latex2e")
1283   {
1284     retval = system("sh","latex " +  fname +" &");
1285   }
1286   else
1287   {
1288     retval = system("sh",default + " " +  fname +" &");
1289   }
1290  }
1291  else
1292  { " -- Need a filename ";
1293    return();
1294  }
1295}
1296example
1297{ "EXAMPLE:"; echo = 2;
1298  intmat m[3][4] = 9,2,4,5,2,5,-2,4,-6,10,-1,2,7;
1299  opentex("exp001");
1300  texobj("exp001","An intmat:  ",m);
1301  closetex("exp001");
1302  tex("exp001");
1303  xdvi("exp001");
1304  echo = 0;
1305  pause("the created files will be deleted after pressing <RETURN>");
1306  echo = 2;
1307  system("sh","rm exp001.*");
1308}
1309///////////////////////////////////////////////////////////////////////////////
1310
1311static proc parsr(string s)                     // parse real
1312{
1313  string t;
1314  if (s=="      Inf") { return("\\infty",3);}
1315  if (s=="     -Inf") { return("\\-infty",6);}
1316  if (s[7]=="-"){t ="-";}
1317  if (s[8]<>"0"){t = t + s[8];}
1318  if (s[9]<>"0" or s[8]<>"0"){t = t + s[9];}
1319  if (size(t))
1320  { if (t=="1") {return(s[1,5]+"*10",21);}
1321    if (size(t)>1) {return(s[1,5]+"*10^{"+t+"}",21+2*size(t));}
1322    else {return(s[1,5]+"*10^"+t,23);}
1323  }
1324  else
1325  {
1326    return(s[1,5],12);
1327  }
1328}
1329///////////////////////////////////////////////////////////////////////////////
1330
1331static proc parsg(string s)                  // parse Galois field
1332{
1333  string t;
1334
1335  if (s=="1") {return("1",5);}
1336  if (short)
1337  {
1338    t =s[1];
1339    if(size(s)>1) {return(t+"^{" + s[2,size(s)-1] + "}",3+2*(size(s)-1));}
1340    else{return(t,5);}
1341  }
1342  else
1343  {
1344    return(parselong(s+"!"));
1345  }
1346}
1347///////////////////////////////////////////////////////////////////////////////
1348
1349proc texpoly(string fname,def p,list #)
1350"USAGE:   texpoly(fname,p); fname string, p poly
1351RETURN:  if @code{fname=\"\"}: string, the polynomial p in LaTeX-typesetting;@*
1352         otherwise: append this string to the file @code{<fname>}, and
1353         return nothing.
1354NOTE:    preceding \">>\" are deleted in @code{fname}, and suffix \".tex\"
1355         (if not given) is added to @code{fname}.
1356EXAMPLE: example texpoly; shows an example
1357"
1358{
1359  def @r = basering;
1360
1361  poly f,monom;
1362  ideal I;
1363  number cfm;
1364  string sign,cfmt,pt,s,bg,t,monomt,lnbreak;
1365  string sep = newline;
1366  int i,b,b2,n, msz,linesz, count,k;
1367  int realT, parT, galT, complT;
1368  int TW = defined(TeXwidth);
1369 
1370  int C = 2 + defined(TeXdisplay);
1371
1372  string notvalid = "intvec intmat vector matrix module map";
1373
1374  if (typeof(p) == "int") { return(p);}
1375  if (typeof(p)  == "ring" or typeof(p) == "qring")
1376  { " -- Call  texring  instead "; return();}
1377  if (find(notvalid,typeof(p)))
1378  { " -- Call  texobj  instead "; return();}
1379  if (typeof(p)  == "map")
1380  { " -- Call  texmap  instead "; return();}
1381  if (typeof(p)  == "proc")
1382  { " -- Call  texmap  instead "; return();}
1383  if (typeof(p)  == "link" or typeof(p) == "list" or typeof(p) == "resolution")
1384  { ERROR(" // -- Object cannot translated into tex "); return();}
1385
1386  if (!(defined(TeXdisplay))){ lnbreak = "\\\\[2mm]" + newline;}
1387  else { lnbreak = "\\\\" + newline;}
1388
1389
1390  if (defined(TeXdisplay)) { bg = "& ";}
1391  if (!TW) { int TeXwidth = -1; export TeXwidth;}
1392
1393// -- Type check
1394
1395 if (typeof(p)=="string")
1396  { if(defined(`p`))
1397    { pt = p + " = ";
1398      p = `p`;
1399    }
1400  }
1401  if (typeof(p)=="poly" or typeof(p)=="number") {I = p;}
1402
1403  if (typeof(p)=="ideal")
1404  { I = p;
1405    if(size(#)){ sep = #[1];}
1406  }
1407
1408  if (I==0)
1409  { if (!(defined(NoDollars))){return("$0$");}
1410    else {return("0");}
1411  }
1412
1413// -- Type check ende
1414
1415//---------------------
1416
1417//------- set flags: --------------------------------------------------------
1418
1419  if (size(#))
1420  { if (typeof(#[1])=="int") { linesz = #[1];}
1421 //   if (typeof(#[1])=="string") { linesz = #[1];}
1422  }
1423
1424  parT = npars(@r);
1425  realT = (charstr(@r)=="real");
1426  complT = find(charstr(@r),"complex");   // 28.10.06
1427  if (complT) {parT=0;}
1428  i = find(charstr(@r),",");
1429  if (i and !realT and !complT)
1430  { t = charstr(@r)[1,i-1];
1431    galT = (t <> string(char(@r)));  // the char is not the same as the ...
1432  }
1433  i = 0;
1434
1435//------- parse the polynomial
1436  pt = bg;
1437
1438 for(k=1;k<=ncols(I);k++)
1439 { i = 0; linesz = 0; count =0;
1440   sign ="";
1441   f = I[k];
1442   if (f==0) { pt = pt + "0";}
1443  while(f<>0)
1444  { count++; msz = 0;
1445
1446// ------ tex the coefficient
1447    monom = lead(f);
1448    f = f - monom;
1449    cfm = leadcoef(monom);
1450    if (cfm*1 != 0) { monom = leadmonom(monom);} // to conform with integer
1451    if (defined(TeXreplace)) { short =0;}  // this is essential //31.5.07
1452    s = string(monom) + "!";              // add a terminating sign
1453    cfmt = "";
1454
1455    cfmt = string(cfm);
1456    if (size(cfmt)>1)                   // check if sign is < 0
1457    { if (cfmt[2]=="-") { cfm = (-1) *cfm; sign = "-";}}
1458    if (cfmt[1] == "-") { cfm = (-1) * cfm; sign = "-";}
1459    if  (cfm!=1 or monom==1) {cfmt = string(cfm);}
1460    else {cfmt="";}
1461
1462    if (defined(TeXwidth) and TeXwidth > 0 and TeXwidth <9 and count> TeXwidth)
1463    { pt = pt + sign + "\\ldots"; break;}
1464   // ----------------------------------------  linesz ??
1465
1466    if (size(cfmt))                            // parse the coefficient
1467    {
1468     monomt = cfmt;                   // (already a good choice for integers)
1469     msz = 3*size(cfmt);
1470
1471     if(realT) { monomt,msz = parsr(cfmt);}
1472     if (galT) { monomt,msz = parsg(cfmt);}
1473     b = find(cfmt,"/(");                     // look if fraction  // 31.5.07
1474     b2 = find(cfmt,"/");
1475     // if (b) {b++;}                         // 31.5.07
1476     n = size(cfmt);
1477     if (!(parT) and  !(realT) and !(galT))
1478     { if( !(b2) or defined(TeXnofrac))
1479       { monomt = cfmt; msz = 3*size(monomt);}
1480       else
1481       { monomt = "\\frac{" + cfmt[1,b2-1] + "}{" + cfmt[b2+1,n-b2] + "}";
1482          if (n-2*b2>0) {msz = C*(n-b2);}
1483          else {msz = C*b2;}
1484       }
1485     }
1486     if (parT and !(galT))
1487     { monomt,msz = parsp(cfmt,b);}
1488    }
1489    if (monom!=1 and monomt!="" and parT) // 21.4.10
1490    { monomt = monomt+"\\cdot ";} // at least a blank is needed for TeXreplace
1491   
1492// -- now parse the monom
1493    if (monom <> 1)
1494    { i = 1;
1495      if(short)
1496      { while(s[i]<>"!")
1497        { monomt = monomt + s[i]; i++;
1498          b = i;
1499          msz = msz + 3; // it was a single lettered var
1500          while(s[i]!="!" and s[i]>="0" and s[i]<="9"){i++;}
1501          if (i-b)
1502          { monomt = monomt + "^{" + s[b,i-b] + "}";
1503            msz = msz + 2*(i-b);
1504          }
1505        }
1506      }
1507      else          //  not short
1508      { t,i = parselong(s);
1509        monomt = monomt + t;
1510        msz = msz + i;
1511      }
1512    }
1513
1514   msz = msz + 6*size(sign);   // Wieso mal 6 ??
1515//  string(msz) + "  ," + string(linesz) + "  " + string(cfm*monom);
1516
1517   if (TeXwidth > 10 and (linesz + msz > 3*TeXwidth) and linesz)
1518   { pt = pt + lnbreak + bg;
1519     linesz = msz;
1520   }
1521   else { linesz = linesz + msz; }  // 3 for sign
1522    pt = pt + sign + monomt;
1523   sign = "+";
1524   monomt = "";
1525  }
1526
1527  if (k<ncols(I)){ pt = pt + sep;}
1528 }
1529
1530  if (TeXwidth==0 and typeof(p)=="poly"){ pt = pt + "= 0";}
1531  if (!TW) { kill Latex::TeXwidth;}
1532  if (not(defined(NoDollars))) { pt = "$"+pt+"$";}
1533
1534  if (size(fname))
1535  { i=1;
1536    while (fname[i]==">"){i++;}
1537    fname = fname[i,size(fname)-i+1];
1538
1539    if (size(fname)>=4)         // check if filename is ending with ".tex"
1540    { if(fname[size(fname)-3,4]!=".tex") {fname = fname +".tex"; }
1541    }
1542    else {fname = fname + ".tex";}
1543    write(fname,pt);
1544   }
1545  else {return(pt);}
1546}
1547example
1548{ "EXAMPLE:"; echo =2;
1549  ring r0=0,(x,y,z),dp;
1550  poly f = -1x^2 + 2;
1551  texpoly("",f);
1552  ring rr= real,(x,y,z),dp;
1553  texpoly("",2x2y23z);
1554  ring r7= 7,(x,y,z),dp;
1555  poly f = 2x2y23z;
1556  texpoly("",f);
1557  ring rab =(0,a,b),(x,y,z),dp;
1558  poly f = (-2a2 +b3 -2)/a * x2y4z5 + (a2+1)*x + a+1;
1559  f;
1560  texpoly("",f);
1561  texpoly("",1/(a2+2)*x+2/b);
1562}
1563///////////////////////////////////////////////////////////////////////////////
1564
1565static proc parsp(string cfmt, int b)
1566{ string mt, nom,denom;
1567  int fl1,fl2,sz1,sz2,msz;
1568
1569  if (!(b))
1570  { mt,fl1 = parst(cfmt,0); msz = size(cfmt)-2;
1571    if (fl1) { mt = "(" + mt + ")"; msz = msz +1; }
1572  }
1573  else
1574  { nom,fl1 = parst(cfmt[1,b-1],1);
1575    denom,fl2 = parst(cfmt[b+1,size(cfmt)-b],1);
1576    if (defined(TeXnofrac))
1577    { if(fl1) { nom = "(" + nom + ")"; sz1++;}
1578      if(fl2) {denom = "(" + denom + ")"; sz2++;}
1579      mt = "(" + nom+ "/"+ denom+ ")"; msz = sz1+sz2 +1;   //31.5.07
1580    }
1581    else
1582    { mt = "\\frac{" + nom + "}{" + denom + "}";
1583      if (sz1-sz2) { msz = 5*sz1;}
1584      else {msz = 5*sz2;}
1585    }
1586   }
1587  return(mt,msz);
1588}
1589example
1590{"EXAMPLE:"; echo =2;
1591  ring r=(0,a,b),x,dp;
1592  int i;
1593  poly f = (a2b12 + 23a2 -b13-1)/(a2+2b -1);
1594  f;
1595  string s;
1596  s= string(f);
1597  i = find(s,")/(");
1598  parsp(s,i);
1599}
1600///////////////////////////////////////////////////////////////////////////////
1601
1602static proc parst(string s,int sec)                // parse parameter
1603// sec parameter to see if in parsp a fraction follows
1604{
1605  int i,j =1,-1;
1606  int b,k,jj,mz,in;                         // begin and end, in for index
1607  int saveshort=short;
1608  string t,c,vn,nom,denom,sg;
1609
1610  if (s[1]=="(") { s = s[2,size(s)-2]; }
1611  s = s + "!";
1612
1613  if (short)
1614  { j = 0;  // 31.5.07
1615    while(s[i]<>"!")
1616    { b=i; if (s[i]=="+" or s[i]=="-") {j++;}  // 31.5.07
1617      while(s[i]>="0" and s[i]<="9" or (s[i]=="+" or s[i]=="-") and s[i]!="!")
1618      {i++;}     // scan the number
1619        t =s[b,i-b];
1620    //  if (t=="-1" and s[i]!="!" and s[i]!="-" and s[i]!="+"){t = "-";}
1621      if (t=="-1" and (s[i]<="0" or s[i]>="9") and s[i]!= "/" and s[i]!="!")
1622      {t = "-";}
1623      if (s[i]=="/")
1624      { i++;
1625        sg = "";
1626        if (t[1]=="+" or t[1]=="-")
1627        { nom = t[2,size(t)-1];
1628          sg = t[1];
1629        }
1630        else { nom = t;}
1631        b =i;
1632        while(s[i]>="0" and s[i]<="9") {i++;}
1633        denom = s[b,i-b];
1634        if (!(sec) and (!(defined(TeXaligned))))
1635        { t = sg + "\\frac{" + nom + "}{" + denom + "}";}
1636        else
1637        { t = sg + "(" + nom + "/" + denom + ")";
1638        }
1639      }
1640      c = c + t;
1641      if(s[i]!="!"){c = c + s[i]; i++;}      // the parameter
1642      b=i;
1643      while(s[i]>="0" and s[i]<="9")
1644      {i++;}  //the exponent
1645      if(i-b){ c = c + "^{" + s[b,i-b]+"}";}
1646     }
1647   }
1648   else                         // if not short ....
1649   { while (s[i] <> "!")
1650   { b=i; j++; in=0;         // 4.10.10
1651       while(s[i]=="-" or s[i]=="+" or (s[i]>="0" and s[i]<="9")){i++;}
1652       t = s[b,i-b];
1653       if (t=="-1" and s[i]=="*" ) {t="-";}
1654       if (s[i]=="/")
1655       { i++;
1656         sg = "";
1657         if (t[1]=="+" or t[1]=="-")
1658         { nom = t[2,size(t)-1];
1659           sg = t[1];
1660         }
1661         else { nom = t;}
1662         b =i;
1663         while(s[i]>="0" and s[i]<="9") {i++;}
1664         denom = s[b,i-b];
1665         if (!(sec) and (!(defined(TeXaligned))))
1666         { t = sg + "\\frac{" + nom + "}{" + denom + "}";}
1667         else
1668         { t = sg + "(" + nom + "/" + denom + ")";
1669         }
1670       }
1671       c = c+t; t="";
1672       if (s[i]=="*"){i++;}
1673       b=i;
1674       while(s[i]!="+" and s[i]!="-" and s[i]!="!")  //pass a monom
1675       { // start with letters
1676        // alternativ:
1677        while((s[i]>="a" and s[i]<="z") or (s[i]>="A" and s[i]<="Z")){i++;}
1678        k = i-b;
1679        vn = s[b,k];
1680        if (defined(TeXreplace))
1681        { for (jj=1; jj<= size(TeXreplace);jj++)
1682         { if (vn == TeXreplace[jj][1])
1683           {vn = TeXreplace[jj][2]; k=1;
1684             if (s[i]=="*") {vn = vn + " ";}
1685            break;} //suppose replacing by a single sign
1686         }
1687        }
1688        t = t + vn;
1689        mz = mz + 10*k;
1690        if (s[i]=="_"  or s[i]=="(") {in++; i++;}    // the index is coming
1691        b = i;
1692        while(s[i]>="0" and s[i]<="9" or s[i]=="-"){ i++;}
1693        k = i-b;
1694        if (k and in<=1) {t = t + "_{";in++;} //and k  i.e. there was an index
1695        if (k){ t = t +s[b,k];}
1696        if(s[i]==")") {i++;}
1697        if(k and s[i]!="(" and s[i]!="_"){ t = t + "}";} // index closed
1698        if(s[i]=="(" or s[i]=="_") {i++; t = t + ",";in++;}
1699        if (s[i]=="^")
1700        { i++; b = i;
1701          while(s[i]>="0" and s[i]<="9"){ i++;} // for neg. expon.
1702          if (b-i) { t = t + "^{" + s[b,i-b] + "}";}
1703        }
1704        if (i-b > k) { mz = mz + 5*(i-b);}
1705        else {mz = mz + 5*k;}
1706        if (s[i]=="*"){i++;in=0;}
1707        b=i;
1708       }
1709       c =c+t;
1710      }
1711   }
1712   short = saveshort;
1713   return(c,j);
1714}
1715example
1716{ "EXAMPLE:"; echo =2;
1717  ring r=(0,a,b),x,dp;
1718  poly f = (a2b12 + 23a2 -b13-1);
1719  f;
1720  parst(string(f),0);
1721
1722  f =(-a +4b2 -2);
1723  f;
1724  parst(string(f),0);
1725
1726  f = a23;
1727  f;
1728  parst(string(f),0);
1729  f = 2a12b3 -4ab15 +2a4b12 -2;
1730  short =0;
1731  f;
1732  parst(string(f),0);
1733   ring r2=(0,a1,b1),x,dp;
1734  poly f = 2*a1^12*b1^3 -4*a1*b1^15 +2*a1^4*b1^12 -2;
1735  f;
1736  parst(string(f),0);
1737}
1738///////////////////////////////////////////////////////////////////////////////
1739
1740static proc parselong(string s)
1741{
1742  int i,j,k,b,mz,in;    // in is a counter for indices
1743  string t,vn;              // varname
1744
1745  i = 1;
1746  while (s[i] <> "!")
1747  { b=i;
1748
1749  // -- scan now the letter ...
1750  //  while(s[i]!="!" and )
1751
1752// alternativ:
1753 while((s[i]>="a" and s[i]<="z") or (s[i]>="A" and s[i]<="Z"))
1754 { i++;}
1755 // s[i]; i;
1756   k = i-b;
1757   vn = s[b,k];
1758
1759   if (defined(TeXreplace))
1760   { for (j=1; j<= size(TeXreplace);j++)
1761     { if (vn == TeXreplace[j][1])
1762       {vn = TeXreplace[j][2]; k=1;
1763        if (s[i]=="*") {vn = vn + " ";}
1764         break;} //suppose replacing by a single sign
1765     }
1766   }
1767   t = t + vn;
1768   mz = mz + 10*k;
1769   if (s[i]=="_"  or s[i]=="(") { in++;i++;}    // the index is coming
1770   b = i;
1771   while(s[i]>="0" and s[i]<="9" or s[i]=="-"){ i++;}  // "-" for neg indices
1772   j = i-b;
1773   // if (j){ t = t + "_{" +s[b,j] + "}";}  // formely
1774   if (j and in<=1) {t = t + "_{";in++;}  // and j  i.e. there was an index
1775   if (j){ t = t  +s[b,j];}
1776   if(s[i]==")") {i++;}
1777   if(j and s[i]!="(" and s[i]!="_"){ t = t + "}";} // index closed
1778   if(s[i]=="(" or s[i]=="_") {i++; t = t + ",";in++;}
1779   if (s[i]=="^")
1780   { i++; b = i;
1781     while(s[i]>="0" and s[i]<="9" or s[i]=="-")
1782     { i++;}  // for neg. expon.
1783     if (b-i) { t = t + "^{" + s[b,i-b] + "}";}
1784   }
1785   if (i-b > j) { mz = mz + 5*(i-b);}
1786   else {mz = mz + 5*j;}
1787   if (s[i]=="*"){i++;in=0;}
1788  }
1789  return(t,mz);
1790}
1791example
1792{ "EXAMPLE:"; echo =2;
1793  ring r =(49,a),x,dp;
1794  number f = a13;
1795  parsg(string(f));
1796  list TeXreplace; export TeXreplace;
1797  TeXreplace[1] = list("b","\\beta");
1798  TeXreplace[2] = list("a","\\alpha");
1799  TeXreplace[3] = list("c","\\gamma");
1800  parselong(string(f)+"!");
1801}
1802///////////////////////////////////////////////////////////////////////////////
1803
1804/* obsolete
1805static proc tktex (def d)
1806{
1807 // calls appropriate proc from latex lib
1808
1809 string typeofd =typeof(d);
1810 if (typeofd=="int" or typeofd=="string" or typeofd=="resolution" or typeofd=="map" or typeofd =="list"){ return(d);}
1811
1812 if (typeofd=="intvec" or typeofd == "intmat" or typeofd =="vector" or
1813     typeofd=="matrix" or typeofd == "module")   { return(texobj("",d));}
1814 if (typeofd=="ring" or typeofd=="qring") { return(texring("",d));}
1815 if (typeofd =="ideal") { return(texobj("",d));}
1816 if (typeofd=="number" or typeofd=="poly" or typeofd=="ideal")
1817                                                 { return(texpoly("",d));}
1818 if (typeofd=="link") {return(d);}
1819
1820}
1821*/
1822///////////////////////////////////////////////////////////////////////////////
1823
1824static proc splt(string s)
1825{ int n,i= size(s),1;
1826  int p;
1827  string t;
1828
1829  while(n-i+1 >pagewidth)
1830  { p = find(s,newline,i);
1831    if (p and (p-i)<pagewidth)
1832    { t = t + s[i,p];
1833      i = i + p;
1834    }
1835    else
1836    {
1837     t = t+ s[i,pagewidth] + newline;
1838     i = i + pagewidth;
1839    }
1840  }
1841  if (n) { t= t + s[i,n-i+1];}
1842  return(t);
1843}
1844
1845/////////////////////////////  PART0 //////////////////////////////////////////
1846
1847static proc part0(string fname)
1848{
1849 int texdemopart =0;
1850 export texdemopart;
1851
1852
1853// Singular script for generating tldemo.tex
1854
1855 string nl = newline;
1856 string nl2 = newline + newline;
1857 string lb = "\\\\";
1858 string bv = "\\begin{verbatim}" + newline ;
1859 string ev = "\\end{verbatim}" ;
1860
1861// "generating part0 of " + fname  + nl;
1862
1863 opentex(fname);
1864
1865write(fname,"\\newcommand{\\Line}{\\rule{\\textwidth}{0.25mm}\\\\[1mm]}");
1866
1867   write(fname,"\\centerline{\\textbf{\\large Demo file for latex.lib }}");
1868   //  write(fname,"\\centerline{\\textbf{\\large Christian Gorzel }}");
1869   write(fname,"\\centerline{07/10/2010}");
1870   write(fname,"\\vspace{1cm}");
1871
1872//--
1873
1874 write(fname,"\\section{Introduction}");
1875 write(fname,"The procedures in \\verb|latex.lib| translate the output of
1876 {\\sc Singular} into \\LaTeX \\ text.
1877 This document illustrates the functionality of the library."+"\\\\" +  nl);
1878 write(fname,"\\begin{tabular}{ll}" + nl +
1879"LIBRARY: {\\tt latex.lib} &   PROCEDURES FOR TYPESETTING SINGULAR" +
1880"\\\\" +  nl +
1881" & OBJECTS IN LATEX2E"+
1882"\\\\" +  nl +
1883"{\\tt closetex(fnm);} & writes closing line for \\LaTeX-document"+
1884"\\\\" +  nl +
1885"{\\tt  opentex(fnm);} & writes header for \\LaTeX-file fnm"+
1886"\\\\" +  nl +
1887"{\\tt  tex(fnm);} & calls \\LaTeX2e for file fnm"+
1888"\\\\" +  nl +
1889"{\\tt  texdemo([n]);} & produces a file explaining the features of this lib"+
1890"\\\\" +  nl +
1891"{\\tt  texfactorize(fnm,f);} & creates string in \\LaTeX-format for
1892factors of polynomial f"+ "\\\\" +  nl +
1893"{\\tt  texmap(fnm,m,r1,r2);} & creates string in \\LaTeX-format for
1894map m:r1$\\rightarrow$r2"+ "\\\\" +  nl +
1895"{\\tt  texname(fnm,s);} &      creates string in \\LaTeX-format for
1896identifier"+ "\\\\" +  nl +
1897"{\\tt  texobj(l);} &           creates string in \\LaTeX-format for
1898any (basic) type"+ "\\\\" +  nl +
1899"{\\tt  texpoly(f,n[,l]);} &    creates string in \\LaTeX-format for poly"+
1900"\\\\" +  nl +
1901"{\\tt  texproc(fnm,p);} &      creates string in \\LaTeX-format of
1902text from proc p"+ "\\\\" +  nl +
1903"{\\tt  texring(fnm,r[,l]);} &  creates string in \\LaTeX-lformat for
1904ring/qring"+ "\\\\" +  nl +
1905"{\\tt  rmx(s);} &              removes .aux and .log files of \\LaTeX-files"+
1906"\\\\" +  nl +
1907"{\\tt  xdvi(s);} &             calls xdvi for dvi-files"+
1908"\\\\" +  nl +
1909" \\end{tabular} " + nl2 + "\\vspace{0.2cm}" + nl2 +
1910"(parameters in square brackets {\\tt [ ]} are optional)"+
1911"\\\\" +  nl2 + "\\vspace{0.2cm}" + nl2 +
1912"The global variables {\\tt TeXwidth}, {\\tt TeXnofrac}, {\\tt
1913 TeXbrack}, {\\tt TeXproj}, {\\tt TeXaligned}, {\\tt TeXreplace}, {\\tt
1914 NoDollars} are used to control the typesetting: "
1915);
1916
1917write(fname,
1918bv +
1919"
1920  TeXwidth   (int) -1, 0, 1..9, >9:  controls breaking of long polynomials
1921  TeXnofrac  (int) flag:  write 1/2 instead of \\frac{1}{2}
1922  TeXbrack   (string) \"{\", \"(\", \"<\", \"|\", empty string:
1923                            controls brackets around ideals and matrices
1924  TeXproj    (int) flag:  write \":\" instead of \",\" in vectors
1925  TeXaligned (int) flag:  write mappings (and ideals) aligned
1926  TeXreplace (list) list entries = 2 strings:  replacing symbols
1927  NoDollars  (int) flag:  suppresses surrounding $ signs
1928
1929" +
1930ev);
1931write(fname,"Notice that none of these global variables are defined when
1932loading \\verb|latex.lib|. A flag variable is set as soon as it is defined.");
1933
1934
1935//% The procs and
1936//% the global variables
1937
1938//----------------------- opentex -----------------------------
1939   write(fname,"\\section{Opening a \\LaTeX\\ file}");
1940   write(fname,"In order to create a \\LaTeX\\ document and write a standard
1941header into it, use the following command."+
1942bv+
1943"> string fname = \"" + fname + "\";" + nl +
1944"> texopen(fname);" +
1945ev + nl);
1946
1947write(fname,"The variable \\verb|fname| is always the first argument when
1948calling one of the procedures of \\verb|latex.lib|. If this string is the
1949empty string, then the output in not written into a file but displayed on
1950the screen.");
1951
1952 //% opentex, defaulted to latex, possibly extension are ... and
1953 //% ``own''
1954
1955
1956pagewidth = 65;
1957int TeXwidth = 100; export TeXwidth;
1958// "part 0 generated " + nl;
1959} //part0
1960
1961
1962/////////////////////////////  PART1 //////////////////////////////////////////
1963
1964static proc part1(string fname)
1965{
1966
1967  int st = defined(texdemopart);
1968  string nl = newline;
1969  string nl2 = newline + newline;
1970  string lb = "\\\\";
1971  string bv = "\\begin{verbatim}" + newline ;
1972  string ev = "\\end{verbatim}" ;
1973
1974  if (not(st) or texdemopart>=1)
1975  { print(" Call part0 first");
1976    return();
1977  }
1978  else { texdemopart=1; }
1979
1980//"Continuing part1 of " + fname + nl;
1981
1982write(fname,
1983"\\section{Rings, polynomials and ideals}"+nl2);
1984
1985// -1a------ a ring in char 0, short varnames and poly. ordering ----------
1986write(fname,
1987" A ring in characteristic 0 with short names of variables and polynomial
1988ordering." +nl);
1989 ring r0=0,(x,y,z),dp;
1990 poly g=-x2y+2y13z+1;
1991write(fname,
1992bv +
1993"> ring r0=0,(x,y,z),dp;" +nl+
1994"> texring(fname,r0);" +
1995ev);
1996  texring(fname,r0);
1997  write(fname,nl2);
1998write(fname,
1999bv +
2000"> poly g=-x2y+2y13z+1;  g;" + nl +
2001"> texpoly(fname,g);" +nl +
2002ev);
2003  texpoly(fname,g);
2004  write(fname,"\\\\"+nl2);
2005
2006// write(fname,"\\Line");
2007
2008// -1b------ still in the same ring, a polynomial with rational coefs --------
2009write(fname,
2010" A polynomial with rational coefficients.
2011" +nl);
2012write(fname,
2013bv +
2014"> texpoly(fname,g/280);" +nl +
2015ev
2016);
2017  texpoly(fname,g/280);
2018  kill r0;
2019
2020write(fname,"\\\\"+nl2);
2021write(fname,"\\Line");
2022// -2-------- a ring in char 7, indexed varnames and series ordering ----------
2023write(fname,
2024" A ring in characteristic 7 with indexed names of variables and local
2025ordering." +nl);
2026 ring r1=7,(x1,x2,x3,x4),Ds;
2027 poly g=-2*x1+x4-1;
2028write(fname,
2029bv +
2030"> ring r1=7,(x1,x2,x3,x4),Ds;" +nl +
2031"> texring(fname,r1);" +nl +
2032ev);
2033texring(fname,r1);
2034write(fname,lb);
2035
2036write(fname, bv +
2037"> poly g=-2*x1+x4-1;  g;" +nl +
2038"> texpoly(fname,g);" +nl +
2039ev);
2040
2041  texpoly(fname,g);
2042
2043write(fname,lb);
2044write(fname,"\\Line");
2045
2046// -3-------- a ring in char 0, indexed varnames and local ordering ----------
2047write(fname,
2048" A ring in characteristic 0 with indexed names of variables and local
2049ordering.
2050" +nl);
2051 ring r2=0,(x(1..5),y(1..2)),(ds(5),ls(2));
2052 poly g=-y(1)^3*x(5) +y(1)*x(2);
2053write(fname,
2054bv +
2055"> ring r2=0,(x(1..5),y(1..2)),(ds(5),ls(2));" + nl +
2056"> texring(fname,r2);" +nl +
2057ev);
2058  texring(fname,r2);
2059
2060write(fname,
2061bv +
2062"> poly g=-y(1)^3*x(5)+y(1)*x(2);  g;" +nl+
2063 string(g) + nl +
2064"> texpoly(fname,g);"  +nl +
2065ev
2066);
2067  texpoly(fname,g);
2068  write(fname,lb);
2069
2070write(fname,"\\Line");
2071
2072// -4-------- a ring in char 0, indexed varnames and weighted ordering ------
2073write(fname,
2074" A ring in characteristic 0 with indexed names of variables and weighted
2075 ordering." +nl);
2076 ring r3=0,(x_1,x_2,x_3),wp(3,2,1);
2077 poly g=-x_1*x_2+2*x_2*x_3+x_1*x_3;
2078write(fname,
2079bv +
2080"> ring r3=0,(x_1,x_2,x_3),wp(3,2,1);" +nl+
2081"> texring(fname,r3);" +nl +
2082ev);
2083  texring(fname,r3);
2084write(fname,
2085bv +
2086"> poly g=-x_1*x_2+2*x_2*x_3+x_1*x_3;  g;" +nl+
2087  string(g) + nl +
2088"> texpoly(fname,g);"  +nl +
2089ev
2090);
2091  texpoly(fname,g); write(fname,lb);
2092
2093write(fname,"\\Line");
2094
2095// -5-------- a ring with real coeff and matrix ordering -------------------
2096write(fname,
2097" A ring with real coefficients and matrix ordering.
2098" +nl);
2099 ring rr=real,(x,y),M(1,2,3,4);
2100 poly g =-1.2e-10*x2+y+1;
2101write(fname,
2102bv +
2103"> ring rr=real,(x,y),M(1,2,3,4);"+nl+
2104"> texring(fname,rr);"+nl+
2105ev);
2106  texring(fname,rr);
2107
2108write(fname,
2109bv +
2110"> poly g=-1.2e-10*x2+y+1;  g;"+nl+
2111  string(g) + nl +
2112"> texpoly(fname,g);" +nl +
2113ev);
2114  texpoly(fname,g);  write(fname,lb);
2115
2116write(fname,"\\Line");
2117
2118// -6a-------- a ring in char 0, and indexed parameters --------- ----------
2119write(fname,
2120" A ring in characteristic 0 with parameters.
2121" +nl);
2122 ring r0t=(0,s,t),(x,y),dp;
2123 poly g=8*(-s+2t)/(st+t3)*x+t2*x-1;
2124write(fname,
2125bv +
2126"> ring r0t=(0,s,t),(x,y),dp;" +nl +
2127"> texring(fname,r0t);" +nl +
2128ev);
2129 texring(fname,r0t);
2130write(fname,
2131bv +
2132"> poly g=8*(-s+2t)/(st+t3)*x+t2*x-1;  g;"+nl+
2133  string(g) +nl +
2134"> texpoly(fname,g);" +nl +
2135ev);
2136  texpoly(fname,g); write(fname,lb);
2137write(fname,"\\Line");
2138
2139// -6b------- a ring in char 11003, and indexed parameters --------- ----------
2140write(fname,
2141" A ring in characteristic 11 and indexed parameters.
2142" +nl);
2143 ring rt=(11003,t1,t2,t3),(X,Y),dp;
2144 poly g=8*(-t1+t2)/(t1+t3)*X+t2*Y-1;
2145write(fname,
2146bv +
2147"> ring rt=(11003,t1,t2,t3),(X,Y),dp;" +nl +
2148"> texring(fname,rt);" +nl+
2149ev);
2150  texring(fname,rt);
2151
2152write(fname,
2153bv +
2154"> poly g=8*(-t1+t2)/(t1+t3)*X+t2*Y-1;  g;" + nl+
2155 string(g) +nl +
2156"> texpoly(fname,g);" +nl +
2157ev);
2158  texpoly(fname,g); write(fname,lb);
2159
2160write(fname,"\\Line");
2161
2162// -7-------- a ring over an algebraic extension in char 7 ---------------
2163write(fname,
2164" A ring over an algebraic extension in char 7.
2165" +nl);
2166
2167  ring ralg = (7,a),x,dp;
2168  minpoly = a2-a+3;
2169  poly g = -(2a13+a)*x2+a2*x-a+1;
2170
2171write(fname,
2172bv +
2173"> ring ralg=(7,a),x,dp;" +nl +
2174"> minpoly=a2-a+3;" +nl +
2175"> texring(fname,ralg);" +nl+
2176ev);
2177 texring(fname,ralg);
2178
2179write(fname,
2180bv +
2181"> poly g=-(2a13+a)*x2+a2*x-a+1;  g;"+nl+
2182 string(g) +nl +
2183"> texpoly(fname,g);" +nl +
2184ev
2185);
2186  texpoly(fname,g); write(fname,lb);
2187
2188write(fname,"\\Line");
2189
2190// -8-------- the same ring a in 7 ralg, defined with gftables -- F_49 -------
2191write(fname,
2192" A ring defined with \\verb|gftables|, the same as \\verb|ralg| before, but
2193with primitive element in the Galois field $\\F_{49}$." +nl);
2194 ring r49 =(49,a),x,dp;
2195 poly g=-(2a13+a)*x2+a2*x-a+1;
2196write(fname,
2197bv +
2198"> ring r49 =(49,a),x,dp;" +nl+
2199"> texring(fname,r49);" +nl+
2200ev);
2201 texring(fname,r49);
2202
2203write(fname,
2204bv +
2205"> poly g=-(2a13+a)*x2+a2*x-a+1;  g;" +nl+
2206 string(g) +nl +
2207"> texpoly(fname,g);" +nl +
2208ev);
2209  texpoly(fname,g); write(fname,lb);
2210
2211write(fname,"\\Line");
2212
2213// -9-------- a ring over the Gaussian numbers  ----------
2214write(fname,
2215" A ring over the Gaussian numbers.
2216" +nl);
2217 ring ri=(0,i),(x,y,z),ls;
2218 minpoly=i2+1;
2219 poly g=-(i+1)*x+2i2y2+i+x;
2220write(fname,
2221bv +
2222"> ring ri=(0,i),(x,y,z),ls;" +nl +
2223"> minpoly=i2+1;" +nl +
2224"> texring(fname,ri);" +nl+
2225ev);
2226 texring(fname,ri);
2227
2228write(fname,
2229bv +
2230"> poly g=-(i+1)*x+2i2y2+i+x;  g;" +nl+
2231 string(g) +nl +
2232"> texpoly(fname,g);" +nl +
2233ev
2234);
2235  texpoly(fname,g); write(fname,lb);
2236
2237write(fname,"\\Line");
2238
2239// -10--------- a quotient ring performed from  ----------
2240write(fname,
2241" A quotient ring performed of \\verb|r0| by an ideal
2242" +nl);
2243 ring r0=0,(x,y,z),dp;
2244 ideal I = x2-y,y+z2, xy;
2245 I = std(I);
2246string sI = string(I);
2247 qring qr = I;
2248write(fname,
2249bv +
2250"> setring r0;" +nl +
2251"> ideal I=x2-y,y+z2, xy;" +nl +
2252"> I=std(I);" +nl +
2253"> string(I);" + nl +
2254splt(sI) + nl +
2255"> qring qr=I;"+ nl+
2256"> texring(fname,qr);" +
2257ev
2258);
2259  texring(fname,qr); write(fname,lb);
2260
2261write(fname,"\\Line");
2262
2263// ------------------------- Features for rings
2264
2265write(fname,"\\subsection{Features for rings}");
2266
2267write(fname,
2268"In many cases it might be convenient to change the standard typesetting
2269of rings. This can be done by passing additional arguments to \\verb|texring|."
2270+nl);
2271
2272// changing the brackets
2273
2274write(fname,nl,"In order to change the displayed brackets one has to give
2275the desired ones as additional argument (accepted brackets are: \\verb|\"\{\"|,
2276\\verb|\"\{\{\"|,\\verb|\"[\"|,\\verb|\"[[\"|,\\verb|\"<\"|,\\verb|\"<<\"|).");
2277
2278write(fname,
2279bv +
2280"> texring(fname,rr,\"{{\");" + nl +
2281ev
2282);
2283
2284texring(fname,rr,"{{");
2285
2286write(fname,
2287bv +
2288"> texring(fname,r2,\"[\");" + nl +
2289ev
2290);
2291
2292texring(fname,r2,"[");
2293
2294write(fname,nl+"\\vspace{0.2cm}" + nl2);
2295
2296write(fname,"The brackets around the ideal in a quotient ring can be
2297changed by setting  the global variable \\verb|TeXbrack| (see section
2298{\\tt Ideals}).",nl);
2299
2300
2301write(fname,
2302bv +
2303"> string TeXbrack = \"<\";" +nl +
2304"> texring(fname,qr);" +nl +
2305ev
2306);
2307
2308  string TeXbrack = "<"; export TeXbrack;
2309  texring(fname,qr);
2310  kill TeXbrack;
2311
2312write(fname,
2313bv +
2314"> kill TeXbrack;" + nl +
2315ev);
2316
2317
2318write(fname,nl2,"\\Line");
2319
2320// changing the ground field
2321// -------------------------------------------------
2322
2323write(fname,
2324"It is possible to display a ground field different from the
2325actual one by passing any letter in \\LaTeX \\ notation as additional
2326argument.  Predefined values are \\verb|\"\\\\C\"|, \\verb|\"\\\\R\"|,
2327\\verb|\"k\"|, \\verb|\"K\"| and \\verb|\"R\"|."+nl+
2328"If for example a ground field of characteristic 0 should be written as
2329$\\C$ instead of $\\Q$ use this as additonal argument.",nl);
2330
2331write(fname,
2332bv +
2333"> texring(fname,r3,\"\\\\C\");" + nl +
2334ev);
2335
2336texring(fname,r3,"\\C");
2337write(fname,nl+ "\\vspace{0.2cm}" + nl2);
2338
2339write(fname,"The values \\verb|\"k\"|, \\verb|\"K\"|, \\verb|\"R\"| play a
2340 special role when the ground field is an algebraic extension. In this case
2341the parameters will be omitted.");
2342
2343write(fname,
2344bv +
2345"> texring(fname,ralg,\"k\");" + nl +
2346ev
2347);
2348
2349texring(fname, ralg,"k");
2350write(fname,nl+"\\vspace{0.2cm}" + nl2);
2351
2352write(fname,"If an algebraic extension should be displayed together with
2353its minimal polynomial the optional parameter \\verb|mipo| has to be used.");
2354
2355write(fname,
2356bv +
2357"> texring(fname,ralg,\"mipo\");" + nl +
2358ev
2359);
2360
2361texring(fname, ralg,"mipo");
2362write(fname,nl,"\\Line");
2363
2364// displaying only certain vars
2365
2366write(fname,"By default all variables of a ring will be displayed. It is
2367possible to print only certain variables with $\\ldots$ between
2368them. The positions of the variables which should be displayed have to be
2369passed to \\verb|texring| as an \\verb|intvec|.");
2370
2371write(fname,
2372bv +
2373"> intvec v=5,6;
2374> texring(fname,r2,v);" +nl +
2375ev
2376);
2377
2378  intvec v = 5,6;
2379  texring(fname,r2,v);
2380  kill v;
2381
2382write(fname,
2383bv +
2384"> kill v;" + nl +
2385ev
2386);
2387
2388write(fname,nl+ "\\vspace{0.2cm}" + nl2);
2389
2390write(fname,"The first and the last variable will always be printed.
2391In order to print only these it is sufficient to give a 1 as third argument.");
2392
2393write(fname,
2394bv +
2395"> texring(fname,r1,1);" + nl +
2396ev
2397);
2398texring(fname,r1,1);
2399
2400write(fname,nl2,"\\Line");
2401
2402// invariant ring under a group action
2403
2404write(fname,"If you want for example to display a ring as the invariant ring
2405under a group, additional information starting with \\verb|^| may be added.");
2406
2407write(fname,
2408bv +
2409"> texring(fname,r0,\"^G\");" + nl +
2410ev
2411);
2412
2413texring(fname, r0,"^G");
2414write(fname,nl2,"\\Line");
2415
2416// passing several optional arguments at once
2417
2418write(fname,"It is also possible to pass several of the arguments described
2419above at once (in any order).");
2420
2421
2422write(fname,
2423bv +
2424"> texring(fname,r3,\"\\\\R\",\"{{\",\"^G\");" + nl +
2425ev
2426);
2427
2428texring(fname, r3,"\\R","{{","^G");
2429write(fname,nl2);
2430//"end part 1" + nl;
2431}
2432
2433
2434/////////////////////////////  PART2 //////////////////////////////////////////
2435
2436
2437static proc part2(string fname)
2438{
2439
2440  int st = defined(texdemopart);
2441  string nl = newline;
2442  string nl2 = newline + newline;
2443  string lb = "\\\\";
2444  string bv = "\\begin{verbatim}" + newline ;
2445  string ev = "\\end{verbatim}" ;
2446
2447  if (not(st) or texdemopart>=2)
2448  { print(" Call part1 first");
2449    return();
2450  }
2451  else { texdemopart=2; }
2452
2453// "Continuing Part2 of " + fname + nl;
2454
2455//-------------------- texfactorize ------------------------------
2456write(fname,"\\subsection{Factorized polynomials}");
2457
2458write(fname,"The command \\verb|texfactorize| calls internally the
2459{\\sc Singular} command \\verb|factorize| and returns the product of the
2460irreducible factors. Note that, at the moment, it is not possible to pass
2461any optional arguments for \\verb|factorize| through \\verb|texfactorize|.");
2462
2463  ring r0=0,(x,y,z),dp;
2464  poly h=(x+1+y)^2*x3y*(2x-2y)*y12;
2465
2466write(fname,
2467bv +
2468"> setring r0;"+nl+
2469"> poly h=(x+1+y)^2*x3y*(2x-2y)*y12;"+nl+
2470"> h;" +nl+
2471 splt(string(h)) + nl +
2472"> texfactorize(fname,h);" +
2473ev);
2474texfactorize(fname,h);
2475
2476//  setring ralg;
2477  ring ralg = (7,a),x,dp;
2478  minpoly = a2-a+3;
2479  poly h = (a24x5+x3)*a2x6*(x+1)^2;
2480
2481write(fname,
2482bv +
2483"> setring ralg;"+nl+
2484"> poly h=(a24x5+x3)*a2x6*(x+1)^2;" +nl+
2485"> h;"+ nl +
2486 splt(string(h)) + nl +
2487"> texfactorize(fname,h);"+ nl +
2488ev);
2489texfactorize(fname,h);
2490
2491//--------------------- features for polynomials -----------------
2492write(fname,"\\subsection{Features for polynomials}");
2493
2494// TeXreplace
2495// ---------------------------------------------
2496write(fname,"By setting the global variable \\verb|TeXreplace| it is possible
2497to define rules for replacing strings or variable names.
2498\\verb|TeXreplace| has to be a list of twoelemented lists where the first
2499entry is the text which should be replaced by the second entry.
2500This may be applied to replace names of variables, but is also used
2501when calling \\verb|texname| or \\verb|texmap|. Note that it
2502is necessary to write a double backslash \\verb|\\\\\| at the beginning of
2503a \\TeX \\ symbol." + lb+nl);
2504
2505write(fname,"Let us denote the primitive element of an algebraic extension
2506by $\\xi$.");
2507
2508list TeXreplace; export TeXreplace;
2509TeXreplace[1] = list("a","\\xi");
2510// setring r49;
2511  ring r49 =(49,a),x,dp;
2512  poly g=-(2a13 +a)*x2+a2*x-a+1;
2513write(fname,
2514bv +
2515"> list TeXreplace;" +nl +
2516"> TeXreplace[1]=list(\"a\",\"\\\\xi\");" +nl+
2517"> setring r49;" +nl+
2518"> texpoly(fname,g);"+ nl +
2519ev);
2520
2521texpoly(fname,g);
2522
2523write(fname,nl+ "\\vspace{0.2cm}" + nl2);
2524write(fname,"Now let us write $\\lambda$ and $\\mu$ for deformation
2525parameters.");
2526TeXreplace[2]= list("s","\\lambda");
2527TeXreplace[3]= list("t","\\mu");
2528// setring(r0t);
2529  ring r0t=(0,s,t),(x,y),dp;
2530  poly g=8*(-s+2t)/(st+t3)*x+t2*x-1;
2531write(fname,
2532bv +
2533"> TeXreplace[2]=list(\"s\",\"\\\\lambda\");"+nl+
2534"> TeXreplace[3]=list(\"t\",\"\\\\mu\");"+nl+
2535"> setring(r0t);"+nl+
2536"> texpoly(fname,g);"+ nl +
2537ev);
2538texpoly(fname,g);
2539
2540kill TeXreplace;
2541write(fname,nl+ "\\vspace{0.4cm}" + nl2);
2542write(fname,nl2+"Note that, if \\verb|TeXreplace| is defined, the translation
2543into \\LaTeX  code runs a little bit slower, because every polynomial is
2544compiled in the \\verb|non short| mode."+ lb );
2545
2546write(fname,nl,"\\Line");
2547
2548//linebreaking   TeXwdith
2549//-----------------------------------------------------------------------
2550write(fname,"The global variable \\verb|TeXwidth| controls the wrapping of
2551polynomials; possible values are:" + lb);
2552
2553write(fname,
2554"\\[ " + nl +
2555"\\text{TeXwidth} = ",
2556"\\begin{cases} ",
2557" -1 & \\text{no linebreaking} \\\\ ",
2558"  0 & \\text{print the polynomial as equation } f=0 \\\\ ",
2559" 1,\\dots,9 & \\text{the first n terms followed by the sign of the next
2560term} \\\\ ",
2561" > 9 & \\text{line break after terms of length n (see below)} ",
2562"\\end{cases}",
2563"\\]",nl);
2564
2565write(fname,"Note that the size of terms is calculated with certain
2566multiplicities.",nl);
2567
2568//----------------------------------------------------------
2569
2570write(fname,"\\begin{itemize}",nl);
2571write(fname,"\\item",nl);
2572
2573write(fname,
2574bv +
2575"> TeXwidth=-1;"+nl+
2576"> setring r0;"+nl+
2577"> poly f=g^2;"+nl+
2578"> texpoly(fname,f);" + nl +
2579ev);
2580
2581  setring r0;
2582  poly g=-x2y+2y13z+1;
2583  poly f=g^2;
2584  texpoly(fname,f);
2585
2586write(fname,"\\item",nl);
2587
2588write(fname,
2589bv +
2590"> TeXwidth=0;"+nl+
2591"> texpoly(fname,f);"+nl+
2592ev);
2593TeXwidth = 0;
2594texpoly(fname,f);
2595
2596write(fname,"\\item",nl);
2597
2598write(fname,
2599bv +
2600"> TeXwidth=2;"+nl+
2601"> texpoly(fname,f);"+nl+
2602ev);
2603TeXwidth=2;
2604texpoly(fname,f);
2605
2606write(fname,"\\item",nl);
2607
2608write(fname,
2609bv +
2610"> TeXwidth=20;"+nl+
2611"> texpoly(fname,f);"+nl+
2612ev);
2613TeXwidth=20;
2614texpoly(fname,f);
2615TeXwidth=-1;
2616
2617write(fname,"\\end{itemize}",nl);
2618
2619write(fname,nl2,"\\Line");
2620
2621write(fname,"There are two possibilities to convert a polynomial into
2622\\LaTeX{} code: either by using \\verb|texpoly| or by calling \\verb|texobj|.
2623The difference is that \\verb|texpoly| puts the polynomial in textmode
2624while \\verb|texobj| uses the display mode."+nl+
2625"The following examples show the different outputs:");
2626
2627write(fname,
2628bv +
2629"> setring r3;
2630> texpoly(fname,g/180);" + nl +
2631ev);
2632
2633// setring r3;
2634
2635  ring r3=0,(x_1,x_2,x_3),wp(3,2,1);
2636  poly g=-x_1*x_2+2*x_2*x_3+x_1*x_3;
2637  texpoly(fname,g/180);
2638
2639write(fname,
2640bv +
2641"> texobj(fname,g/180);
2642" + nl +
2643ev);
2644
2645texobj(fname,g/180);
2646
2647//write(fname,"Some explanation how it works: if \\verb|texobj| is called for
2648//a polynomial, then it defines a global variable \\verb|TeXdisp| which forces
2649//\\verb|texpoly| to count fraction with space corresponding
2650//the displaymode."+lb,nl2);
2651//---------------------texobj for ideal ---------------
2652
2653write(fname,"\\subsection{Ideals}");
2654write(fname,"By default, ideals are displayed as column vectors.");
2655
2656  ring r;
2657  ideal I=3xz5+x2y3-3z,7xz5+y3z+x-z,-xyz2+4yz+2x;
2658
2659write(fname,
2660bv +
2661"> ring r;   // the default ring"+nl+
2662"> ideal I=3xz5+x2y3-3z,7xz5+y3z+x-z,-xyz2+4yz+2x;"+nl+
2663"> texobj(fname,I);" + nl +
2664ev);
2665
2666texobj(fname,I);
2667
2668write(fname,"\\Line");
2669//----------------------------------------------------------------------
2670write(fname,"If the global variable \\verb|Texaligned| is set then the ideal
2671is displayed as a row vector.");
2672
2673write(fname,
2674bv +
2675"> int TeXaligned;
2676> texobj(fname,I);" + nl +
2677ev);
2678
2679int TeXaligned; export TeXaligned;
2680texobj(fname,I);
2681
2682 write(fname,nl+"\\Line");
2683//----------------------------------------------------------------------
2684write(fname,"By setting the global variable \\verb|TeXbrack| it is possible
2685to change the brackets.");
2686
2687write(fname,
2688bv +
2689"> string TeXbrack=\"<\";"+nl+
2690"> texobj(fname,I);"+nl+
2691ev);
2692
2693string TeXbrack="<";  export TeXbrack;
2694texobj(fname,I);
2695
2696write(fname,
2697bv +
2698"> kill TeXbrack, TeXaligned;" + nl +
2699ev);
2700
2701kill TeXbrack,TeXaligned;
2702write(fname,"\\Line");
2703//----------------------------------------------------------------------
2704write(fname,
2705" If \\verb|TeXwidth| is 0, an ideal is displayed as a system of
2706equations.");
2707
2708// ------------- a linear equation system
2709
2710  ring r5=0,x(1..5),dp;
2711  ideal I=-x(1)+2*x(3)+x(5), x(2)-x(4)+2*x(5)-1, 8*x(1)+x(4)+2;
2712  TeXwidth=0;
2713
2714write(fname,
2715bv +
2716"> ring r5=0,x(1..5),dp;"+nl+
2717"> ideal I=-x(1)+2*x(3)+x(5), x(2)-x(4)+2*x(5)-1, 8*x(1)+x(4)+2;"+nl+
2718"> TeXwidth=0;"+nl+
2719"> texobj(fname,I);" +nl+
2720ev);
2721
2722  texobj(fname,I);
2723
2724  setring r;
2725  ideal J=4x4y21+z25y7-y2,x3+y2,y2-z4;
2726
2727write(fname,
2728bv +
2729"> setring r;"+nl+
2730"> ideal J=4x4y21+z25y7-y2,x3+y2,y2-z4;"+nl+
2731"> texobj(fname,J);" +nl+
2732ev
2733);
2734  texobj(fname,J);
2735
2736write(fname,"\\Line");
2737//-----------------------------------------------------------------------
2738write(fname,"Call the ideal by its name and it is printed as follows");
2739
2740write(fname,
2741bv +
2742"> setring r;
2743> ideal I=3xz5+x2y3-3z,7xz5+y3z+x-z,-xyz2+4yz+2x;
2744> texobj(fname,\"I\");" + nl +
2745ev);
2746
2747 setring r; export(I);
2748 texobj(fname,"I");
2749 kill I,r;
2750//" end part 2 " + nl;
2751}
2752
2753/////////////////////////////  PART3 //////////////////////////////////////////
2754
2755static proc part3(string fname)
2756{
2757  int st=defined(texdemopart);
2758  string nl=newline;
2759  string nl2=newline + newline;
2760  string lb="\\\\";
2761  string bv="\\begin{verbatim}" + newline ;
2762  string ev="\\end{verbatim}" ;
2763
2764  if (not(st) or st>=3)
2765  {
2766    print(" Call part2 first");
2767    return();
2768  }
2769  else { texdemopart=3; }
2770
2771// " Continuing part 3 of " + fname +
2772// " : map,matrices,vectors,intvec,intmats,proc";
2773
2774//---------------------- texmap ------------------------------
2775write(fname,"\\section{Typeseting maps between rings}");
2776write(fname,"By default, maps are displayed in the following way:");
2777
2778write(fname,
2779bv +
2780"> ring r4=0,(x,y,z),dp;"+nl+
2781"> ring r5=0,(u,v),dp;"+nl+
2782"> map phi=r1,u2,uv -v,v2;"+nl+
2783"> texmap(fname,phi,r4,r5);" + nl +
2784ev);
2785
2786  ring @r4_h=0,(x,y,z),dp;
2787  export @r4_h;
2788  ring r5=0,(u,v),dp;
2789  map @phi_h=@r4_h,u2,uv -v,v2;  export @phi_h;
2790  texmap(fname,@phi_h,@r4_h,r5);
2791
2792write(fname,"\\Line");
2793//--------------------------------------------------------------------
2794
2795write(fname,"If the global variable \\verb|TeXaligned| is set, then the
2796map is displayed in one line.");
2797
2798write(fname,
2799bv +
2800"> int TeXaligned;"+nl+
2801"> texmap(fname,phi,r4,r5,\"\\\\C\");" + nl +
2802ev );
2803
2804  int TeXaligned; export TeXaligned;
2805  texmap(fname,@phi_h,@r4_h,r5,"\\C");
2806
2807write(fname,nl+"\\Line");
2808//--------------------------------------------------------------------
2809
2810write(fname,"It is possible to pass the same additional arguments to
2811\\verb|texmap| as to \\verb|texring| (see section {\\tt Rings}). This can
2812be done by calling \\verb|texmap| with two lists as additional parameters,
2813where the first list contains the optional parameters for the source and
2814the second one contains the parameters for the domain. Note that if only one
2815list is present then it is applied to both of the rings.");
2816
2817write(fname,
2818bv +
2819"> texmap(fname,phi,r4,r5,\"\\\\C\",\"{\");" + nl +
2820ev );
2821  texmap(fname,@phi_h,@r4_h,r5,"\\C","{");
2822
2823write(fname,nl+ "\\vspace{0.2cm}" + nl);
2824write(fname,"The next example shows how to format the two rings in different
2825ways.");
2826
2827write(fname,
2828bv +
2829"> texmap(fname,phi,r4,r5,list(),list(\"{\"));"+nl+
2830ev );
2831
2832 texmap(fname,@phi_h,@r4_h,r5,list(),list("{"));
2833
2834write(fname,nl+"\\Line");
2835//--------------------------------------------------------------------
2836
2837write(fname,"If the map is called by its name then \\verb|texmap| displays,
2838in addition, also the name of the map.");
2839
2840write(fname,
2841bv +
2842"> list TeXreplace;" +nl+
2843"> TeXreplace[1]=list(\"phi\",\"\\\\phi\")" +nl+
2844"> texmap(fname,\"phi\",r4,r5);" + nl +
2845ev);
2846
2847  list TeXreplace;
2848  TeXreplace[1]=list("@phi_h","\\phi");
2849  export TeXreplace;
2850  texmap(fname,"@phi_h",@r4_h,r5);
2851  kill @phi_h,@r4_h,r5,TeXreplace,TeXaligned;
2852
2853
2854write(fname,
2855bv +
2856"> kill phi,r4,r5,TeXreplace,TeXaligned;" + nl +
2857ev);
2858
2859
2860//% the texobj part
2861write(fname,"\\section{Typesetting composed data structures}");
2862//=======================================================================
2863
2864write(fname, "Complex data structures such as matrices, vectors or modules
2865can be displayed by using the procedure \\verb|texobj|.");
2866
2867write(fname,"\\subsection{Matrices and vectors}");
2868//=======================================================================
2869
2870write(fname,"The following example shows how to typeset the hessian of a
2871polynomial."+
2872bv +
2873"> ring r;",
2874"> poly h=2xy3-x2z+x4z7+y4z2;",
2875"> matrix H=jacob(jacob(h));",
2876"> texobj(fname,H);",
2877ev );
2878
2879ring r;
2880poly h=2xy3-x2z+x4z7+y4z2;
2881matrix H=jacob(jacob(h));
2882
2883texobj(fname,H);
2884
2885write(fname,"By default, \\verb|vectors| are written as row vectors:");
2886
2887vector V = H[2];
2888
2889write(fname,
2890bv +
2891"> vector V=H[2];",
2892"> texobj(fname,V);",
2893ev );
2894
2895texobj(fname,V);
2896
2897write(fname,nl2+"\\vspace{0.2cm}"+nl);
2898write(fname,"In order to transpose it, it has to be converted into a
2899matrix.");
2900write(fname,
2901bv +
2902"> texobj(fname,matrix(V));",
2903ev );
2904
2905texobj(fname,matrix(V));
2906
2907write(fname,nl+"\\Line");
2908//------------------------------------------------------------------------
2909
2910write(fname,"All the features for typesetting polynomials work also for
2911matrices or vectors with polynomial entries.");
2912write(fname,nl+ "\\vspace{0.2cm}" + nl);
2913write(fname,"By setting the global variable \\verb|TeXwidth| it is possible
2914to display only the first terms of the polynomials.");
2915
2916write(fname,bv +
2917"> TeXwidth=1;",
2918"> texobj(fname,H);",
2919"> TeXwidth=-1;",
2920ev );
2921
2922TeXwidth = 1;
2923texobj(fname,H);
2924TeXwidth = -1;
2925
2926write(fname,nl,"\\Line");
2927//------------------------------------------------------------------------
2928write(fname,"The flag variable \\verb|TeXnofrac| controls the typesetting
2929of fractions.");
2930
2931write(fname,
2932bv +
2933"> ring R0=0,x,dp;",
2934"> matrix M[2][3]=1/2, 0, 1/2, 0, 1/3, 2/3;",
2935"> texobj(fname,M);",
2936ev);
2937
2938
2939ring R0 = 0,x,dp;
2940matrix M[2][3] = 1/2, 0, 1/2, 0, 1/3, 2/3;
2941texobj(fname,M);
2942
2943write(fname,bv +
2944"> int TeXnofrac;",
2945"> texobj(fname,M);",
2946ev );
2947
2948  int TeXnofrac; export TeXnofrac;
2949  texobj(fname,M);
2950  kill TeXnofrac;
2951
2952write(fname,bv +
2953"> kill TeXnofrac;",
2954ev );
2955
2956write(fname,nl,"\\Line");
2957//------------------------------------------------------------------------
2958
2959write(fname,"Printing a vector with homogeneous coordinates can be done by
2960setting \\verb|TeXproj|.");
2961write(fname,bv +
2962"> setring r;",
2963"> int TeXproj;",
2964"> texobj(fname,V);",
2965ev );
2966
2967  setring r;
2968  ideal I=3xz5+x2y3-3z,7xz5+y3z+x-z,-xyz2+4yz+2x;
2969  int TeXproj; export TeXproj;
2970
2971  texobj(fname,V);
2972  kill TeXproj;
2973
2974write(fname,
2975bv +
2976"> kill TeXproj;"+nl+
2977ev);
2978
2979write(fname,"\\subsection{Modules}",nl2);
2980
2981write(fname,"By default, modules are displayed in the following way:"+
2982bv +
2983"> setring r;",
2984"> module md=module(H);",
2985"> texobj(fname,md);",
2986ev );
2987
2988setring r;
2989module md = module(H);
2990texobj(fname,md);
2991
2992write(fname,nl,"\\Line");
2993//------------------------------------------------------------------------
2994
2995write(fname,"In order to write the generators of a module aligned, the flag
2996variable \\verb|TeXaligned| has to be defined.",nl2);
2997
2998write(fname,bv +
2999"> int TeXaligned;",
3000"> texobj(fname,md);",
3001ev );
3002
3003int TeXaligned; export TeXaligned;
3004texobj(fname,md);
3005kill TeXaligned;
3006write(fname,bv +
3007"> kill TeXaligned;",
3008ev );
3009
3010//----------------------------------------------------------------
3011write(fname,"\\subsection{Integer matrices and vectors}");
3012
3013write(fname,"Integer matrices are displayed in the following way.");
3014
3015intmat m[3][4]=-1,3,5,2,-2,8,6,0,2,5,8,7;
3016
3017write(fname,
3018bv +
3019"> intmat m[3][4]=-1,3,5,2,-2,8,6,0,2,5,8,7;" + nl +
3020"> m;" + nl +
3021string(m) + nl +
3022"> texobj(fname,m);" + nl2 +
3023ev );
3024
3025texobj(fname,m);
3026write(fname,nl+ "\\vspace{0.2cm}" + nl);
3027write(fname,"Integer vectors are displayed as row vectors.");
3028intvec v = 1..4;
3029write(fname,
3030bv +
3031"> intvec v=1..4;"+nl+
3032"> v;" + nl +
3033string(v) + nl +
3034"> texobj(fname,v);"+nl+
3035ev );
3036
3037texobj(fname,v);
3038
3039write(fname,nl+ "\\vspace{0.2cm}" + nl);
3040write(fname,"To typeset the transposed vector, it has to be converted into
3041an integer matrix."+
3042bv +
3043"> texobj(fname,intmat(v));" + nl2 +
3044ev );
3045
3046texobj(fname,intmat(v));
3047
3048write(fname,nl+ "\\vspace{0.2cm}" + nl);
3049
3050write(fname,"The following example shows how to typeset the multiplication of
3051a matrix with a vector."+
3052bv +
3053"> texobj(fname,intmat(m*v),\"=\",m,\"*\",intmat(v));" + nl2 +
3054ev );
3055
3056texobj(fname,intmat(m*v),"=",m,"*",intmat(v));
3057
3058//-----------------------------------------------------------------
3059write(fname,"\\Line");
3060
3061write(fname,"Of course, the brackets of a \\verb|intmat| can be changed
3062by setting \\verb|TeXbrack|.");
3063
3064write(fname,
3065bv +
3066"> intmat mat[3][3]=1,2,3,4,5,6,7,8,9;" + nl +
3067"> string TeXbrack=\"|\";" + nl +
3068"> texobj(fname,mat,\" = \",det(mat)); " + nl2 +
3069ev );
3070
3071intmat mat[3][3] = 1,2,3,4,5,6,7,8,9;
3072string TeXbrack = "|"; export TeXbrack;
3073texobj(fname,mat," = ",det(mat));
3074kill TeXbrack;
3075
3076//----------------------------------texname-------------------
3077
3078//write(fname,"\\section{Names of identifiers}");
3079
3080
3081//write(fname,"The proc \\verb|texname| is used to write indexed names in a
3082//correct way"+lb,nl);
3083
3084
3085
3086// ------------------------------- texproc -------------------------------
3087  proc milnor_number (poly p)
3088  {
3089    ideal i = std(jacob(p));
3090    int m_nr = vdim(i);
3091    if (m_nr<0)
3092    {
3093      "//--  not an isolated singularity";
3094    }
3095    return(m_nr);
3096  }
3097  export(milnor_number);
3098
3099write(fname,"\\section{Typesetting procedures}");
3100write(fname,"The following procedure allows to include the source code
3101of procedures into a \\LaTeX document.");
3102write(fname,
3103bv +
3104"> texproc(fname,\"milnor\_number\");" +nl+
3105ev);
3106
3107 texproc(fname,"milnor_number");
3108
3109  kill milnor_number;
3110
3111// ------------------------------ closing the tex file -------------------
3112write(fname,"\\section{Closing the \\LaTeX\\ file}");
3113write(fname,"To close the file use \\verb|closetex(fname);|. It should now
3114contain pure \\LaTeX \\ code and may be compiled with " +
3115"\\verb|tex(fname)| and displayed with \\verb|xdvi(fname)|.");
3116
3117// write(fname,"\\section{Remarks}");
3118closetex(fname);
3119//"end of part3" + nl;
3120
3121pagewidth =80;
3122kill texdemopart,TeXwidth;
3123}
3124///////////////////////////////////////////////////////////////////////////////
3125
3126/*
3127
3128
3129   texring extended for Z,Z_m,real,complex
3130
3131
3132// Test for new functionality:
3133// texring:
3134  ring rZ = integer,x,dp;
3135  texring("",rZ);
3136  ring rZ8 = (integer,8),x,dp;
3137  texring("",rZ8);
3138 
3139  ring rR = real,x,dp;
3140  texring("",rR);
3141
3142  ring rR2010 = (real,20,10),x,dp;
3143  texring("",rR2010);
3144
3145  ring rC = complex,x,dp;
3146  texring("",rC);
3147
3148  ring rC2010 = (complex,20,10),x,dp;
3149  texring("",rC2010);
3150
3151// multi-indexed parameters and variables
3152
3153  ring rmultind34 = 0,(a(1..3)(1..4)),dp;
3154  texring("",r34);
3155  matrix A[3][4] = maxideal(1);
3156  texobj("",A);
3157
3158  ring rmultind2 = 0,(x1_2,y_1_1,z),ds;
3159  texring("",rmultind2);
3160
3161  ring rmultind3 = 0,(x(-1..1)(0)(3..4)),ds;
3162  texobj("",maxideal(1));
3163
3164  ring rmultind4 = (0,b1(0..2),b2_1(3)),x,ds;
3165  texpoly("",par(1)*par(2)*par(3)+par(4)*x);
3166
3167*/
3168///////////////////////// end of latex.lib ////////////////////////////////////
3169