-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlatex-utils.el
474 lines (405 loc) · 11.6 KB
/
latex-utils.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
(require 'utils)
(require 'defn)
(require 'monads)
(defn next-% []
(let ((start-point (point)))
(forward-char 1)
(loop with found = nil and
done = nil
while (not (or found done))
do
(cond
((at-end?)
(setf done t)
(setf found nil))
((= ?% (char-at-point))
(setf done t)
(setf found t))
(t
(forward-char 1)))
finally (return
(if found (point)
(progn (goto-char start-point) nil))))))
(defn number-figs []
(let ((chapter 0)
(fig-num 1))
(loop while
(search-forward-regexp (rx line-start "\\" (group (or "chapter" "begin{figure}"))) nil t)
do
(let ((m (match-string 1)))
(print m)
(print chapter)
(print fig-num)
(cond ((string= m "chapter")
(setf chapter (+ 1 chapter))
(setf fig-num 1))
((string= m "begin{figure}")
(next-%)
(kill-line)
(insertf "%% Figure %d.%d" chapter fig-num)
(setf fig-num (+ 1 fig-num))))))))
(defn is-epsfile? [filename]
(dlet [len (length filename) ext-start (- len 4)]
(and ($ len >= 4)
(string= (substring filename ext-start len) ".eps"))))
;(is-epsfile? "test.eps")
;(is-epsfile? "test.tex")
(defn get-eps-files []
(mapcar (fn [n] (concat "./stand-in-figures/" n)) (directory-files "./stand-in-figures/" nil "\.eps$")))
(defn generate-figure-tex-lifter [files]
(dlet [here (point)]
(loop for f in files do
(insertf "
\\begin{figure}[htp]
\\centering
\\includegraphics[width=0.9\\textwidth]{%s}
\\caption{%s}
\\label{%s}
\\end{figure}
\\pagebreak[4]
\\clearpage
" f (replace-regexp-in-string "_" "-" f) f))
))
(defn insert-fig [f]
(interactive "Ffile:")
(insertf "
\\begin{figure}[htp]
\\centering
\\includegraphics[width=0.9\\textwidth]{%s}
\\caption{}
\\label{%s}
\\end{figure}
"
(replace-regexp-in-string (regexp-quote "~/work/thesis_writing/") "./" f)
(concat "fig:"
(first (split-string (car (last (split-string f (regexp-quote "/")))) (regexp-quote "."))))))
(defn insert-stand-in-figures []
(interactive)
(generate-figure-tex-lifter (get-eps-files)))
(defn inkscape [filename]
(interactive "F")
(with-current-buffer (shell)
(dlet [p (get-buffer-process (shell))]
(comint-send-string p (concat "inkscape " filename " &>/dev/null&
")))))
(defn gimp [filename]
(interactive "F")
(with-current-buffer (shell)
(dlet [p (get-buffer-process (shell))]
(comint-send-string p (concat "gimp " filename " &>/dev/null&
")))))
(defn latex-em-region [p m]
(interactive "r")
(save-excursion
(goto-char m)
(insert "}")
(goto-char p)
(backward-char)
(insert " {\\em ")))
(defn latex-em-bf-region [p m]
(interactive "r")
(save-excursion
(goto-char m)
(insert "}}")
(goto-char p)
(backward-char)
(insert " {\\em {\\bf")))
(defn collect-all-labels []
(save-excursion
(goto-char (point-min))
(loop with done = nil and
i = 0
while (not done) do
(setf done (not (search-forward-regexp "\\\\label{\\(.*\\)}" nil t)))
(setf i (+ i 1))
(if (> i 1000) (setf done t))
when (not done)
collect (match-string 1))))
(defn collect-refs-uniquely []
(save-excursion
(goto-char (point-min))
(loop with done = nil and
i = 0 and
refs = nil
while (not done) do
(setf done (not (search-forward-regexp "\\\\ref{\\(.*\\)}" nil t)))
(if (> i 1000) (setf done t))
when (not done) do
(let ((m (match-string 1)))
(if (not (member m refs))
(push m refs)))
finally (return (reverse refs)))))
(defn extract-figures []
(save-excursion
(goto-char (point-min))
(loop with done = nil and
i = 0 and
figs = () and
labs = ()
while (not done) do
(print i)
(setf i (+ 1 i))
(let ((more? (search-forward-regexp (regexp-quote "\\begin{figure}") nil t))
(start (progn (beginning-of-line) (point))))
(if more?
(progn
(search-forward-regexp "\\\\label{\\(.*\\)}" nil t)
(push (match-string 1) labs)
(search-forward-regexp (regexp-quote "\\end{figure}") nil t)
(let* ((end (progn (end-of-line) (point)))
(ss (buffer-substring start end)))
(kill-region start end)
(push ss figs)))
(setf done t)))
finally (return (list (reverse figs) (reverse labs))))))
(defn collect-fig-refs-uniquely []
(save-excursion
(goto-char (point-min))
(loop with done = nil and
i = 0 and
refs = nil
while (not done) do
(setf done (not (search-forward-regexp "\\\\ref{\\(fig:.*\\)}" nil t)))
(if (> i 1000) (setf done t))
when (not done) do
(let ((m (match-string 1)))
(if (not (member m refs))
(push m refs)))
finally (return (reverse refs)))))
(defn collect-all-fig-labels []
(save-excursion
(goto-char (point-min))
(loop with done = nil and
i = 0
while (not done) do
(setf done (not (search-forward-regexp "\\\\label{\\(fig:.*\\)}" nil t)))
(setf i (+ i 1))
(if (> i 1000) (setf done t))
when (not done)
collect (match-string 1))))
(defn insert-fig-by-number [n]
(interactive "p")
(let* ((lbls (collect-all-fig-labels))
(lbl (elt lbls (- n 1))))
(insert (concat "\\ref{" lbl "}"))))
(defn insert-label []
(interactive "")
(let ((ref (completing-read "ref:" (collect-all-labels))))
(insert (concat "\\ref{" ref "}"))))
(defn insert-label-from-file [from]
(interactive "fwhich file?:")
(let ((ref (completing-read "ref:" (with-current-buffer (find-file-noselect from) (collect-all-labels)))))
(insert (concat "\\ref{" ref "}"))))
(defn get-bib-file []
(save-excursion
(goto-char (point-min))
(let ((found (search-forward-regexp "\\\\bibliography{\\(.*\\)}")))
(if (not found) (error "Can't find bib file.")
(concat (match-string 1) ".bib")))))
(defn proper [cc]
(list (car cc) (cdr cc)))
(defn testing []
(dlet [[s e] (proper (bibtex-valid-entry))]
(print (list s e))))
(defn char-at-point []
(first (coerce (buffer-substring (point) (+ 1 (point))) 'list)))
(defn at-end? []
(= (point) (point-max)))
(defn next-@ []
(let ((start-point (point)))
(forward-char 1)
(loop with found = nil and
done = nil
while (not (or found done))
do
(cond
((at-end?)
(setf done t)
(setf found nil))
((= ?@ (char-at-point))
(setf done t)
(setf found t))
(t
(forward-char 1)))
finally (return
(if found (point)
(progn (goto-char start-point) nil))))))
(defn next-{ []
(let ((start-point (point)))
(forward-char 1)
(loop with found = nil and
done = nil
while (not (or found done))
do
(cond
((at-end?)
(setf done t)
(setf found nil))
((= ?{ (char-at-point))
(setf done t)
(setf found t))
(t
(forward-char 1)))
finally (return
(if found (point)
(progn (goto-char start-point) nil))))))
(defn get-bibtex-entries [buffer]
(interactive)
(with-current-buffer buffer
(save-excursion
(goto-char (point-min))
(loop while (re-search-forward "@\\(.*\\)[
]*{\\(.*\\)," nil t) collect (match-string 2)))))
(lexical-let ((entries nil))
(defn get-bibtex-entries-cached [filename]
(with-current-buffer (get-buffer filename)
(if (or (buffer-modified-p)
(eq nil entries))
(progn
(setf entries (get-bibtex-entries (get-buffer filename)))
entries)
entries))))
(defn insert-citation []
(interactive "")
(let ((bibbuf (find-file-noselect (get-bib-file))))
(insert
(concat "~\\cite{"
(completing-read "which: " (get-bibtex-entries (get-bib-file)))
"}"))))
(defn insert-citeps []
(interactive "")
(let ((bibbuf (find-file-noselect (get-bib-file))))
(insert (loop with cites = () and
read = () while (progn (setf read
(completing-read
(concat
"which ('' terminates) {"
(join cites ", ")
"} :")
(get-bibtex-entries (get-bib-file))))
(not (or (string= "" read) (string= "_" read))))
collect read into cites
finally (return
(concat "\\citep{"
(join cites ", ")
"}"))))))
(defn insert-cite-keys []
(interactive "")
(let ((bibbuf (find-file-noselect (get-bib-file))))
(insert (loop with cites = () and
read = () while (progn (setf read
(completing-read
(concat
"which ('' terminates) {"
(join cites ", ")
"} :")
(get-bibtex-entries (get-bib-file))))
(not (or (string= "" read) (string= "_" read))))
collect read into cites
finally (return
(concat ""
(join cites ", ")
""))))))
(defn reformat-paragraphs [start end]
(interactive "r")
(dlet [[start end] (sort* (list start end) #'<)]
(print "This is slow, but works")
(save-excursion
(forward-char 1)
(loop with buflen = (point-max)
while ($ (point) betweeni? start end) do
(print "looping")
(forward-paragraph 1)
(backward-char 3)
(fill-paragraph)
(setf end (+ end (- (point-max) buflen)))
(setf buflen (point-max))
(forward-paragraph 1)))))
(dont-do
(domonad monad-seq
[part (split-string "(bob et al; ted and sandy; dogs)" "[;()]")
part (list (chomp part))
part (if (string= "" part) nil (list part))]
part))
(defn text->bib [ctn]
(dlet [[raw-parts year] (mapcar #'chomp (split-string ctn "[,]"))
name (join (mapcar #'capitalize (split-string raw-parts "[ .]")) "")]
(concat name year)))
(dont-do
(text->bib "Markram et al, 2009"))
(defn texts->bibs [str]
(dlet [bibs (domonad monad-seq
[part (split-string str "[;()]")
trimmed (list (chomp part))
text-citation (if
(string= "" trimmed) nil
(list trimmed))
bib-citation (list (text->bib text-citation))]
bib-citation)]
(concat "\\citep{" (join bibs ", ") "}")))
(defn replace-textual-citation [start end]
(interactive "r")
(let ((citep (texts->bibs (buffer-substring start end))))
(kill-region start end)
(insert citep)))
(defn goto-fig-n [n]
(interactive "p")
(goto-char (point-min))
(loop for i from 1 to n do
(re-search-forward (regexp-quote "\\begin{figure}")))
(print (format "Figure %d" n)))
(defn next-fig []
(interactive "")
(re-search-forward (regexp-quote "\\begin{figure}")))
(dont-do
(insert (texts->bibs "(Markram et al, 2009; James,
1492; Mainend and Sejnowsky, 1996)")))
(defn goto-first [start end]
(interactive "r")
(let ((ss (buffer-substring-no-properties start end)))
(with-current-buffer (get-buffer "dissertation.tex")
(search-forward-regexp (regexp-quote ss) nil t))))
(defn insert-latex-block [block-name]
(interactive "s")
(let ((start (point)))
(insertf "\\begin{%s}\n" block-name)
(let ((inside (point)))
(insertf "\n\\end{%s}\n" block-name)
(let ((end (point)))
(goto-char inside)
(indent-region start end)))))
(defun* create-tex-presentation-of-files (files &optional (file "/tmp/recent-figures.tex"))
(let ((buf (find-file file)))
(with-current-buffer buf
(clear-buffer)
(insert "\\documentclass{beamer}
% \\documentclass{article}
% (tex-pdf-mode t)
% \\mode<handout>
\\mode<presentation>
{
% or ...
\\setbeamercovered{transparent}
% or whatever (possibly just delete it)
}
\\usepackage{graphics}
\\usepackage{natbib}
\\usepackage{amsmath}
\\usepackage{beamerarticle}
\\bibliographystyle{apalike}
\\def\\newblock{\\hskip .11em plus.33em minus.07em}
\\useoutertheme{default}
\\title{Ripple Detection}
\\author{Vincent Toups}
\\date{\\today}
\\setbeamertemplate{navigation symbols}{}
\\setbeamertemplate{footline}[frame number]
\\begin{document}
")
(loop for filename in
files
do
(insert-slide-with-figure filename))
(insert "
\\end{document}"))))
(provide 'latex-utils)