grg2tex.red 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. % To convert GRG log file "filein" into LaTeX file "fileout.tex" execute:
  2. %
  3. % grg2tex("filein","fileout.tex");
  4. %
  5. % This code is actually logutil.red with infinitesemal changes.
  6. % All credits to Herbert Melenk.
  7. module grg2tex;
  8. % Author: Herbert Melenk <melenk@sc.zib-berlin.de>.
  9. % log_latex(<infile>,<outfile>);
  10. %
  11. % Transform a REDUCE log file <infile> from XR or Windows with
  12. % output in type setting style to a LATEX source file <outfile>.
  13. fluid '(texstate!* char!-texon!* char!-texoff!* char!-null!*
  14. old!-line!* );
  15. char!-texon!* := '!$ $ %===ZW===
  16. char!-texoff!* := '!$ $ %===ZW===
  17. char!-null!* := int2id 0$
  18. symbolic procedure grg2tex(din,dout); %===ZW===
  19. begin scalar fin,fout,oldfin,oldfout,w;
  20. fin:=open(din,'input); fout:=open(dout,'output);
  21. oldfin:=rds fin; oldfout:=wrs fout;
  22. w:=errorset('(log2latex1),t,t) where !*lower=nil,!*raise=nil;
  23. wrs oldfout; rds oldfin;
  24. close fout; close fin;
  25. end;
  26. symbolic operator grg2tex; %===ZW===
  27. fluid '(l2xprologue!* l2xepilogue!*);
  28. l2xprologue!* :='(
  29. "\documentstyle{article}"
  30. "\setlength{\parindent}{0cm}"
  31. "\sloppy"
  32. "\begin{document}"
  33. );
  34. l2xepilogue!* :='(
  35. "\end{document}"
  36. );
  37. symbolic procedure log2latex1();
  38. begin scalar texstate!*,l,w,c;
  39. integer n;
  40. old!-line!*:=nil;
  41. for each l in l2xprologue!* do prin2t l;
  42. a:
  43. l:=read!-line();
  44. if car l = !$eof!$ then goto done;
  45. if car l = 'tex then
  46. <<
  47. l:=transform2tex cdr l;
  48. mathon();
  49. c:=nil; n:=0;
  50. for each x in l do
  51. <<n:=n+1;
  52. if n>60 and x='!\ and c neq '!\ then <<terpri(); n:=0>>;
  53. prin2 x; c:=x;
  54. >>;
  55. mathoff();
  56. >>
  57. else
  58. <<texton();
  59. for each x in cdr l do prin2 x;
  60. terpri();
  61. >>;
  62. goto a;
  63. done:
  64. if texstate!*=0 then textoff() else
  65. if texstate!*=1 then mathoff();
  66. for each l in l2xepilogue!* do prin2t l;
  67. end;
  68. symbolic procedure transform2tex ll;
  69. begin scalar w,l;
  70. % l2xspace!*:=0;
  71. l := ll;
  72. while l do
  73. <<
  74. if (w:=l2xmatch(l,'(!\ !>))) then l2xsymbtab(l,w) else
  75. if (w:=l2xmatch(l,'(!\ !s !y !m !b !{))) then l2xsymb(l,w) else
  76. if (w:=l2xmatch(l,'(!\ s y m b !{))) then l2xsymb(l,w);
  77. l:=cdr l;
  78. >>;
  79. return ll;
  80. end;
  81. symbolic procedure l2xmatch(s,p);
  82. if null p then s else
  83. if null s then nil else
  84. if car s eq car p then l2xmatch(cdr s,cdr p) else nil;
  85. symbolic procedure l2xsymbtab(l,w);
  86. <<w:=append(explode2 " ",cddr l); %===ZW===
  87. car l:=car w; cdr l:=cdr w;
  88. l>>;
  89. fluid '(tex!-symbols!*);
  90. tex!-symbols!* :=
  91. '(( 182 . "\partial")
  92. ( 198 . "\emptyset")
  93. ( 216 . "\neg")
  94. ( 163 . "\leq")
  95. ( 179 . "\geq")
  96. ( 185 . "\not=")
  97. ( 199 . "\bigcap")
  98. ( 200 . "\bigcup")
  99. ( 206 . "\in")
  100. ( 217 . "\bigwedge")
  101. ( 218 . "\bigvee")
  102. ( 239 . "\vert")
  103. ( 124 . "\vert")
  104. ( 222 . "\Rightarrow")
  105. ( 34 . "\forall")
  106. ( 71 . "\Gamma")
  107. ( 226 . "\dag") % shoud have been (R)
  108. ( 227 . "\copyright")
  109. ( 32 . "\quad")
  110. );
  111. symbolic procedure l2xsymb(l1,l2);
  112. % convert \symb{nnn} to tex symbol.
  113. begin scalar w;integer n;
  114. while digit car l2 do
  115. <<n:=n*10 + id2int car l2 - id2int '!0;
  116. l2 := cdr l2
  117. >>;
  118. w := assoc(n,tex!-symbols!*);
  119. if null w then rederr {"symbol not konw:",n};
  120. l2 := append (explode2 cdr w,'! .cdr l2);
  121. car l1 := car l2; cdr l1 := cdr l2;
  122. end;
  123. symbolic procedure read!-line();
  124. begin scalar w,l;
  125. l:=read!-line0();
  126. if car l=!$eof!$ then return l;
  127. if car l = char!-texon!* then
  128. return
  129. begin
  130. l:=cdr l;
  131. a:
  132. w:=member(char!-texoff!*,l) or member(!$eof!$,l);
  133. if w then
  134. <<old!-line!*:=cdr w; car w:= '! ;
  135. cdr w:=nil; return 'tex . l>>;
  136. l:=append(l,read!-line0());
  137. go to a;
  138. end;
  139. w:=member(char!-texon!*,l);
  140. if w then
  141. <<old!-line!* := car w . cdr w; car w:= '! >>;
  142. return nil.l;
  143. end;
  144. symbolic procedure read!-line0();
  145. begin scalar w,c;
  146. if old!-line!* then
  147. <<w:=old!-line!*; old!-line!*:=nil; return w>>;
  148. while not ((c:=readch())=!$eol!$) and not(c=!$eof!$) do
  149. if id2int c > 3 then w:=c.w; % for ctrlA, ctrl B
  150. if c=!$eof!$ then return {c};
  151. w:=reversip w;
  152. return w or read!-line0();
  153. end;
  154. symbolic procedure mathon();
  155. << textoff(); prin2 "$"; texstate!* :=1; >>;
  156. symbolic procedure mathoff();
  157. << if texstate!*=1 then prin2t "$\\"; texstate!* :=nil>>;
  158. symbolic procedure texton();
  159. if not(texstate!*=0) then
  160. <<mathoff(); prin2t "\begin{verbatim}"; texstate!* := 0>>;
  161. symbolic procedure textoff();
  162. if texstate!*=0 then
  163. <<prin2t "\end{verbatim}"; texstate!*:=nil>>;
  164. endmodule;
  165. end;