minitex.red 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. module minitex; % support of minimal tex syntax for
  2. % reduce help file compilation.
  3. % author: Herbert Melenk, ZIB Berlin
  4. % input: list of single characters in Latex subset syntax
  5. %
  6. % output: list of rows, tagged by line number and max column
  7. % length.
  8. %
  9. % supported syntax elements:
  10. %
  11. % ^ exponent
  12. % _ index
  13. % \frac{ ... }{ ... }
  14. %
  15. % escape sequences \{ \} \_ \^ \\
  16. fluid '(ESCAPE RAISE LOWER X0 Y0 CON);
  17. fluid '(xlo xhi yhi ylo minitex_input minitex_page);
  18. ESCAPE := '!\;
  19. RAISE := '!^;
  20. LOWER := '!_;
  21. X0 := 2;
  22. Y0 := 3;
  23. CON := 4;
  24. symbolic procedure mintex_convert0 s;
  25. if null s then s else
  26. if null cdr s then s else
  27. if car s ='!\ and cadr s = '!\ then !$eol!$ . mintex_convert0 cddr s
  28. else car s . mintex_convert0 cdr s;
  29. symbolic procedure mintex_convert s; mintex_convert0 s;
  30. symbolic procedure minitex(string);
  31. begin scalar q,w; integer r,c;
  32. minitex_page := for i:=-20:20 collect {i};
  33. minitex_input := mintex_convert string;
  34. q := make_chain(0,'hugo);
  35. if null q then return nil;
  36. minitex_collect q;
  37. for each l in minitex_page do
  38. if (l:=cdr l) then
  39. <<r:=r+1; if length(l)>c then c:=length(l);
  40. w:=l.w;
  41. >>;
  42. return r.c. reverse w;
  43. end;
  44. symbolic procedure minitex_pop_char();
  45. if minitex_input then
  46. begin scalar c;
  47. c := car minitex_input;
  48. minitex_input := cdr minitex_input;
  49. return c;
  50. end;
  51. symbolic procedure minitex_skip(cc);
  52. begin scalar c;
  53. c := nil;
  54. while c neq cc do c:=minitex_pop_char();
  55. end;
  56. symbolic procedure minitex_next_char();
  57. if minitex_input then car minitex_input;
  58. symbolic procedure struct(type);
  59. {type,0,0,0};
  60. symbolic procedure make_chain(font,term);
  61. begin integer indpos,xact,d,fh,
  62. lxlo, lxhi, lylo,lyhi,
  63. yindhi,yindlo;
  64. scalar c,cc,cell,new,end_code;
  65. fh:=1; % font height
  66. end_code := 0;
  67. cell := struct('chain);
  68. loop:
  69. c := minitex_pop_char();
  70. cc := minitex_next_char();
  71. if(c = '!{ and null term) then
  72. <<term := '!}; goto loop>>;
  73. if (c = term or c = nil) then goto finish;
  74. % if (c = '! ) then goto loop;
  75. if (c = !$eol!$) then goto loop;
  76. % handle escaped single characters
  77. if(c = ESCAPE and
  78. (cc = '! or cc = '!_ or cc = '!{ or cc = '!} or cc = '!$))
  79. then
  80. <<
  81. indpos := -1;
  82. new := make_char(font,0,minitex_pop_char());
  83. goto after_syntax;
  84. >>;
  85. if(c = ESCAPE) then
  86. <<
  87. indpos := -1;
  88. new := make_escape(font,term);
  89. if(new = -999) then goto loop; % /* ignore? */
  90. >>
  91. else
  92. if(c = LOWER or c = RAISE) then
  93. <<
  94. if(indpos > -1) then
  95. << xact:=indpos; yhi := yindhi; ylo = yindlo;>>
  96. else
  97. << indpos:=xact; yindhi:= yhi; yindlo := ylo;>>;
  98. new := make_chain(1,nil);
  99. if(c = RAISE) then
  100. d := - 1
  101. else
  102. d := + 1;
  103. nth(new,Y0) := d;
  104. ylo := ylo+d; yhi := yhi+d;
  105. >>
  106. else
  107. if(c = '!{) then new := make_chain(font,'!})
  108. else
  109. << indpos := -1;
  110. new := make_char(font,0,c);
  111. >>;
  112. after_syntax:
  113. if not pairp new then <<end_code := new; goto finish>>;
  114. nth(cell,CON):=append(nth(cell,CON),{new});
  115. nth(new,X0) := xact;
  116. xact := xact + xhi;
  117. if(xact>lxhi) then lxhi := xact else xact:=lxhi;
  118. if(ylo<lylo) then lylo:=ylo;
  119. if(yhi>lyhi) then lyhi:=yhi;
  120. if(term) then goto loop;
  121. finish:
  122. ylo := lylo; yhi := lyhi;
  123. xlo := lxlo; xhi := lxhi;
  124. if pairp nth(cell,CON) then return(cell);
  125. end;
  126. symbolic procedure make_char(font,cs,c);
  127. begin scalar cell;
  128. cell := struct('char);
  129. nth(cell,CON) := c;
  130. ylo := 0; yhi:=1;
  131. xlo := 0; xhi:=1;
  132. return(cell);
  133. end;
  134. symbolic procedure make_frac(font);
  135. begin scalar cell;
  136. scalar numr,denr,line;
  137. integer nxhi,dxhi,nyhi,dyhi,nylo,dylo;
  138. integer lxhi;
  139. integer yline,ydist;
  140. ydist := 1;
  141. cell := struct('chain);
  142. while minitex_input and car minitex_input neq '!{ do
  143. minitex_input:=cdr minitex_input;
  144. yline := 0;
  145. numr := make_chain(font,nil);
  146. nxhi := xhi; nyhi := yhi; nylo := ylo;
  147. while minitex_input and car minitex_input neq '!{ do
  148. minitex_input:=cdr minitex_input;
  149. xhi := 0; xlo := 0; yhi := 0; ylo := 0;
  150. denr := make_chain(font,nil);
  151. dxhi := xhi; dyhi := yhi; dylo := ylo;
  152. % /* move the shorter one to the middle */
  153. if(dxhi > nxhi) then
  154. <<
  155. lxhi := dxhi;
  156. nth(numr,X0) := (dxhi - nxhi)/2;
  157. >>
  158. else
  159. <<
  160. lxhi := nxhi;
  161. nth(denr,X0) := (nxhi - dxhi)/2;
  162. >>;
  163. % /* make line */
  164. line := make_line(0,yline,lxhi,yline);
  165. % /* put num on top */
  166. nth(numr,Y0) := yline - ydist - (nyhi-1);
  167. % /* put denr below */
  168. nth(denr,Y0) := yline + ydist - dylo;
  169. % /* total frame */
  170. xlo := 0; xhi := lxhi;
  171. ylo := yline - ydist -(nyhi-nylo);
  172. yhi := yline + ydist +(dyhi-dylo);
  173. % /* make chain */
  174. nth(cell,CON) := {line,numr,denr};
  175. return cell;
  176. end;
  177. symbolic procedure make_line(x,y,x1,y1);
  178. <<nth(cell,X0):=x;
  179. nth(cell,Y0):=y;
  180. nth(cell,CON):=x1;
  181. cell>> where cell=struct('line);
  182. symbolic procedure make_multi(font);
  183. begin scalar cell,new; integer base;
  184. minitex_skip('!});
  185. minitex_skip('!});
  186. cell := struct('chain); nth(cell,CON) :=nil;
  187. while pairp (new :=make_chain(font,!$eol!$)) do
  188. << nth(cell,CON) := append(nth(cell,CON),{new});
  189. nth(new,Y0) := base;
  190. base:=base + (yhi-ylo) + 1;
  191. >>;
  192. yhi := base;
  193. return cell;
  194. end;
  195. symbolic procedure make_end(font);
  196. <<minitex_skip('!}); -1>>;
  197. %---------------------- dispatch -----------------------------------
  198. fluid '(nullum);
  199. nullum := struct('chain);
  200. nth(nullum,CON):= nil;
  201. symbolic procedure make_escape(font,term);
  202. if my_compare('(!f !r !a !c)) then make_frac(font)
  203. else
  204. if my_compare('(!r !f !r !a !c)) then make_frac(font)
  205. else
  206. if my_compare('(!b !e !g !i !n { !m !u !l !t !i )) then make_multi(font)
  207. else
  208. if my_compare('(!e !n !d)) then make_end(font)
  209. else
  210. if my_compare('(!e !m)) then nullum
  211. else
  212. if my_compare('(!n !a !m !e)) then nullum
  213. else
  214. if my_compare('(!i !t)) then nullum
  215. else
  216. <<prin2 "######## \";
  217. for each c in minitex_input do prin2 c;
  218. rederr "Mini-TEX: function not implemented";
  219. >>;
  220. symbolic procedure my_compare s;
  221. begin scalar i,c;
  222. i := minitex_input;
  223. while s and (c := minitex_pop_char()) and
  224. c=car s do s:= cdr s;
  225. if null s then return t;
  226. minitex_input := i;
  227. return nil;
  228. end;
  229. %-------------- interprete structure: fill into page ---------------
  230. symbolic procedure minitex_collect u;
  231. minitex_do(0,0,0,u);
  232. symbolic procedure minitex_do(x,y,font,box);
  233. <<if null get(car box,'minitex) then
  234. <<print box; rederr "minitex: cannot expand object">>;
  235. apply(get(car box,'minitex),list(x,y,font,box));
  236. >>;
  237. put('chain,'minitex,'minitex_chain);
  238. symbolic procedure minitex_chain(x,y,font,box);
  239. << x:=x+nth(box,X0); y := y+nth(box,Y0);
  240. for each u in nth(box,CON) do minitex_do(x,y,font,u)
  241. >>;
  242. put('char,'minitex,'minitex_char);
  243. symbolic procedure minitex_char(x,y,font,box);
  244. begin
  245. x:=x+nth(box,X0); y := y+nth(box,Y0);
  246. minitex_putchar(x,y,nth(box,CON));
  247. end;
  248. put('line,'minitex,'minitex_line);
  249. symbolic procedure minitex_line(x,y,font,box);
  250. begin
  251. x:=x+nth(box,X0); y := y+nth(box,Y0);
  252. for i:=x:x+nth(box,CON) do
  253. minitex_putchar(i,y,'!-);
  254. end;
  255. symbolic procedure minitex_putchar(x,y,c);
  256. begin scalar r;
  257. x:=x+2;
  258. r:=assoc(y,minitex_page);
  259. while length r<x do r:=nconc(r,{'! });
  260. nth(r,x):=c;
  261. end;
  262. end;
  263. minitex '(a b c ^ { d e f } g);
  264. minitex
  265. append(explode2 "\begin{multilineoutput}{1cm}" ,
  266. append({!$eol!$, 1, !$eol!$,2, !$eol!$ , 3, !$eol!$},
  267. explode2 "\end{multilineoutput}"));