1:- module(latex,[latexit/2, latexit/3, latex_math/2, latex_math/3]). 2
3:- meta_predicate letex_math(2,?,?). 4
5:- set_prolog_flag(allow_variable_name_as_functor, true). 6:- use_module(util(prooftree)). 7:- use_module(pac(op)). 8:- use_module(pac(basic)). 9:- use_module(util(file)). 10:- use_module(util(obj)). 11:- use_module(util('emacs-handler')). 12:- use_module(util(misc)). 14
16asy_current_buffer_file :- cur_dir(D),
17 cur_buf(B),
18 atom_trim_tail(B, '.asy', B0),
19 boomerang(D,
20 (asy(-f(pdf), B0);
21 open(-a('Preview'), B0+'.pdf'))).
25pdflatex_current_buffer_file :- cur_dir(D),
26 cur_buf(B),
27 atom_trim_tail(B, '.tex', B0),
28 boomerang([D], (platex(B0);
29 dvipdfmx(B0);
30 open(-a('Preview'), B0+ '.pdf'))).
35platex_buffer :- cur_dir(D), cur_buf(B),
36 atom_trim_tail(B, '.tex', B0),
37 boomerang( D,
38 ( platex(B0);
39 dvipdfmx(B0);
40 open(-a('Preview'), B0+ '.pdf'))).
41
45atom_trim_tail(Atom, Tail, Head):-
46 ( sub_atom(Atom, P, _, 0, Tail)
47 -> sub_atom(Atom, 0, P, _, Head)
48 ; Head = Atom ).
49
50
51
59latex_open_file(Ext) --> obj_get([directory(D), base(B)]),
60 { pshell(open(D+ (/)+ B+ (.)+ Ext)) }.
65latexit(prooftree) --> !, prooftree,
66 current([X|_]),
67 peek(X),
68 latex_math.
69latexit(math) --> latex_math.
70
75
76minimal_math_pdf -->
77 latex_math(
78 ( generate_tex,
79 platex,
81 dvipdfmx,
82 cleaning_tmp_file
83 )),
84 obj_get([base(B)]),
85 { pshell(open(-a('Skim'), '~/local/cgi-bin/images/' + B +'.pdf'))}.
86
88latex_math -->
89 latex_math(
90 ( generate_tex,
91 platex,
93 dvipdfmx,
94 cleaning_tmp_file,
95 pdf2svg,
96 loc(svg),
97 img_src_tag,
98 www_form_encode
99 ) ).
100
102:-meta_predicate latex_math(2, ?, ?). 103
104latex_math(Ph) -->
105 { getenv(home_html_root, HR),
106 atomics_to_string([HR, images], /, D),
107 xsh(mkdir(-p, D)),
108 getenv(host_html_root, UR),
109 atomics_to_string([UR, images], /, E)
110 },
111 peek(X, []),
112 obj_put([text(X)]),
113 obj_put([directory(D)]),
114 obj_put([url_directory(E)]),
115 obj_put([counter_name(pdfcounter), stem(math)]),
116 { working_directory(OldD, D) },
117 new_file,
118 det_phrase(Ph),
119 { working_directory(_, OldD) }.
120
121det_phrase(M:G, X, Y):- det_phrase(G, X, Y, M).
123det_phrase(true, X, X, _):-!.
124det_phrase(M:G, X, Y, _):-!, det_phrase(G, X, Y, M).
125det_phrase((G, H), X, Y, M):-!, det_phrase(G, X, Z, M),
126 det_phrase(H, Z, Y, M).
127det_phrase(G, X, Y, M):- once(call(M:G, X, Y)).
128
129
131
132loc(Ext) --> obj_get([base(B)]),
133 { atomics_to_string([images, (/), B, (.), Ext], Loc)},
134 obj_put([loc(Loc)]).
135
136img_src_tag --> obj_get([loc(Loc)]),
137 peek(["<img width=300 height=300 src='", Loc, "'/>"]),
138 atomics_to_string.
143trim_expr(B, E) --> expr(B), expr(X), expr(E), peek([], X).
150tex_pdf -->
151 generate_tex,
152 !,
153 platex,
155 dvipdfmx,
156 generate_html,
157 cleaning_tmp_file.
158
160cleaning_tmp_file --> obj_get([base(B)]),
161 { maplist(delete_file(B), [dvi,out,log,aux]) }.
162
164delete_file(B, Ext):-
165 concat_atom([B, (.) ,Ext], F),
166 ( exists_file(F) -> delete_file(F)
167 ; true
168 ).
185
186generate_tex --> obj_get([base(B), text(TexCode)]),
187 {
188 concat_atom([B, '.', tex], Ftex),
189 file:cat_text([
190 "\\documentclass{jsarticle}\n",
191 "\\usepackage{amssymb}\n",
192 "\\usepackage{amsmath}\n",
193 "\\usepackage{fmtcount}\n",
194 "\\usepackage{amsfonts}\n",
195 "\\usepackage[all,graph,frame,tips,2cell]{xy}\n",
196 "\\input{minimalmath}\n",
197 "\\input{prooftree}\n",
198 "\\def\\boxfilename{", B, ".size}\n",
199 "\\begin{document}\n",
200 "\\minimalpdf{",
201 TexCode,
202 "}\n",
203 "\\end{document}\n"
204 ], Ftex)
205 }.
210platex --> obj_get([base(B)]),
211 { getenv(platex, Tex) ,
212 xsh([Tex,
213 '-halt-on-error',
214 '-interaction=nonstopmode',
215 B])
216 }.
222pdf2ps --> { getenv(pdf2ps, PDF2PS)},
223 obj_get([base(B)]),
224 { xsh([PDF2PS, B+ ".pdf ", B+ ".ps"]) }.
229ps2jpg --> obj_get([base(B)]), { xsh(convert(B + ".ps", B + ".jpg")) }.
242
244dvi2svg --> dvipdfmx, pdf2svg.
249dvipdfmx --> obj_get([base(B)]),
250 { P = '100%,100%',
251 PDF = B+ ".pdf",
252 getenv(dvipdfmx, DVIPDFMX),
253 xsh([DVIPDFMX, -o(PDF), -p(P), -x('0pt'),-y('0pt'), B])
254 }.
255
257pdf2svg --> { getenv(pdf2svg, PDF2SVG),
258 getenv(home_html_root, HTML_ROOT)
259 },
260 obj_get([base(B)]),
261 { atomic_list_concat([HTML_ROOT, / , images], WD),
262 working_directory(Old, WD),
263 xsh([PDF2SVG, B + ".pdf ", B + ".svg"]),
264 working_directory(_, Old)
265 }.
270generate_html --> obj_get([base(B)]),
271 { H = [ "<html><body>", "<img src=""", B, ".pdf""/>", "</body></html>" ],
272 atomics_to_string([B, '.' , html], HTML),
273 file(HTML, write, smash(H))
274 }.
279open_pdf --> obj_get([directory(D), base(B)]),
280 { xsh(open(D + (/) + B+ '.pdf')) }.
284math_view --> latex_math(
285 ( generate_tex,
286 platex,
288 dvipdfmx,
289 cleaning_tmp_file,
290 obj_put([target('~/public_html/cgi-bin/images')]),
291 loc(pdf)
292 )),
293 current(P),
294 { send_off([ "(find-file-existing \"", P, "\")"]) }.
299latexit --> {latexit}.
300
301latexit :- send_off("(command-execute 'latex-it)"),
302 lisp_atom("default-directory", D),
303 dvipdfmx(D),
304 send_off("(preview-it)")