forked from jkitchin/scimax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scimax-org-latex.el
419 lines (347 loc) · 14.3 KB
/
scimax-org-latex.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
;;; scimax-org-latex.el --- scimax customizations for latex in org-mode
;;; Commentary:
;; These are scimax modifications to how org generates Latex or Latex equation fragments.
(require 'ox-latex)
(defvar scimax-toggle-latex-fragment-func
'org-latex-preview
"Function to toggle latex fragments.
`org-toggle-latex-fragment' is obsolete in 9.3 and is replaced by
`org-latex-preview'.")
;; * Font-lock
;; ** Latex fragments
;;; Code:
(setq org-highlight-latex-and-related '(latex script entities))
(set-face-foreground 'org-latex-and-related "blue")
;; * Latex Export settings
;; Interpret "_" and "^" for export when braces are used.
(setq org-export-with-sub-superscripts '{})
(setq org-latex-default-packages-alist
'(("AUTO" "inputenc" t)
;; this is for having good fonts
("" "lmodern" nil)
;; This is for handling accented characters
("T1" "fontenc" t)
;; This makes standard margins
("top=1in, bottom=1.in, left=1in, right=1in" "geometry" nil)
("" "graphicx" t)
("" "longtable" nil)
("" "float" nil)
("" "wrapfig" nil) ;makes it possible to wrap text around figures
("" "rotating" nil)
("normalem" "ulem" t)
;; These provide math symbols
("" "amsmath" t)
("" "textcomp" t)
("" "marvosym" t)
("" "wasysym" t)
("" "amssymb" t)
("" "amsmath" t)
("theorems, skins" "tcolorbox" t)
;; used for marking up chemical formulas
("version=3" "mhchem" t)
("numbers,super,sort&compress" "natbib" nil)
("" "natmove" nil)
("" "url" nil)
;; this is used for syntax highlighting of code
("cache=false" "minted" nil)
;; this allows you to use underscores in places like filenames. I still
;; wouldn't do it.
("strings" "underscore" nil)
("linktocpage,pdfstartview=FitH,colorlinks,
linkcolor=blue,anchorcolor=blue,
citecolor=blue,filecolor=blue,menucolor=blue,urlcolor=blue"
"hyperref" nil)
;; enables you to embed files in pdfs
("" "attachfile" nil)
;; set default spacing
("" "setspace" nil)
))
;; do not put in \hypersetup. Use your own if you want it e.g.
;; \hypersetup{pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%}}
(setq org-latex-hyperref-template nil)
;; this is for code syntax highlighting in export. you need to use
;; -shell-escape with latex, and install pygments.
(setq org-latex-listings 'minted)
(setq org-latex-minted-options
'(("frame" "lines")
("fontsize" "\\scriptsize")
("linenos" "")))
;; avoid getting \maketitle right after begin{document}
;; you should put \maketitle if and where you want it.
(setq org-latex-title-command "")
(setq org-latex-prefer-user-labels t)
;; ** Custom new classes
;; customized article. better margins
(add-to-list 'org-latex-classes
'("article-1" ;class-name
"\\documentclass{article}
\\usepackage[top=1in, bottom=1.in, left=1in, right=1in]{geometry}
[PACKAGES]
[EXTRA]" ;;header-string
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*a{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
;; This is for when you don't want any default packages, and you want
;; to declare them all yourself.
(add-to-list 'org-latex-classes
'("article-no-defaults" ;class-name
"\\documentclass{article}
[NO-DEFAULT-PACKAGES]
[PACKAGES]
[EXTRA]" ;;header-string
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*a{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
;; * Fragment overlays
(defun scimax-org-latex-fragment-tooltip (beg end image imagetype)
"Add the fragment tooltip to the overlay and set click function to toggle it."
(overlay-put (ov-at) 'help-echo
(concat (buffer-substring beg end)
"\nmouse-1 to toggle."))
(overlay-put (ov-at) 'local-map (let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-x C-l")
(lambda (interactive)
(funcall scimax-toggle-latex-fragment-func)))
(define-key map [mouse-1]
`(lambda ()
(interactive)
(org-remove-latex-fragment-image-overlays ,beg ,end)))
map)))
(defun scimax-toggle-org-latex-fragment-tooltip ()
"Toggle if latex fragment tooltips are used."
(if (not (get 'scimax-org-latex-fragment-tooltip 'enabled))
(progn
(advice-add 'org--make-preview-overlay :after 'scimax-org-latex-fragment-tooltip)
(put 'scimax-org-latex-fragment-tooltip 'enabled t)
(message "LaTeX fragment tooltips are enabled."))
(advice-remove 'org--make-preview-overlay 'scimax-org-latex-fragment-tooltip)
(put 'scimax-org-latex-fragment-tooltip 'enabled nil)
(message "LaTeX fragment tooltips are disabled.")))
;; * Fragment justification
(defun scimax-org-latex-fragment-justify (justification)
"Justify the latex fragment at point with JUSTIFICATION.
JUSTIFICATION is a symbol for 'left, 'center or 'right."
(interactive
(list (intern-soft
(completing-read "Justification (left): " '(left center right)
nil t nil nil 'left))))
(let* ((ov (ov-at))
(beg (ov-beg ov))
(end (ov-end ov))
(shift (- beg (line-beginning-position)))
(img (overlay-get ov 'display))
(img (and (and img (consp img) (eq (car img) 'image)
(image-type-available-p (plist-get (cdr img) :type)))
img))
space-left offset)
(when (and img
;; This means the equation is at the start of the line
(= beg (line-beginning-position))
(or
(string= "" (s-trim (buffer-substring end (line-end-position))))
(eq 'latex-environment (car (org-element-context)))))
(setq space-left (- (window-max-chars-per-line) (car (image-size img)))
offset (floor (cond
((eq justification 'center)
(- (/ space-left 2) shift))
((eq justification 'right)
(- space-left shift))
(t
0))))
(when (>= offset 0)
(overlay-put ov 'before-string (make-string offset ?\ ))))))
(defun scimax-org-latex-fragment-justify-advice (beg end image imagetype)
"After advice function to justify fragments."
(scimax-org-latex-fragment-justify (or (plist-get org-format-latex-options :justify) 'left)))
(defun scimax-toggle-latex-fragment-justification ()
"Toggle if LaTeX fragment justification options can be used."
(interactive)
(if (not (get 'scimax-org-latex-fragment-justify-advice 'enabled))
(progn
(advice-add 'org--make-preview-overlay :after 'scimax-org-latex-fragment-justify-advice)
(put 'scimax-org-latex-fragment-justify-advice 'enabled t)
(message "Latex fragment justification enabled"))
(advice-remove 'org--make-preview-overlay 'scimax-org-latex-fragment-justify-advice)
(put 'scimax-org-latex-fragment-justify-advice 'enabled nil)
(message "Latex fragment justification disabled")))
;; ** numbering latex equations
;; Numbered equations all have (1) as the number for fragments with vanilla
;; org-mode. This code injects the correct numbers into the previews so they
;; look good.
(defun scimax-org-renumber-environment (orig-func &rest args)
"A function to inject numbers in LaTeX fragment previews."
(let ((results '())
(counter -1)
(numberp))
(setq results (cl-loop for (begin . env) in
(org-element-map (org-element-parse-buffer) 'latex-environment
(lambda (env)
(cons
(org-element-property :begin env)
(org-element-property :value env))))
collect
(cond
((and (string-match "\\\\begin{equation}" env)
(not (string-match "\\\\tag{" env)))
(cl-incf counter)
(cons begin counter))
((string-match "\\\\begin{align}" env)
(prog2
(cl-incf counter)
(cons begin counter)
(with-temp-buffer
(insert env)
(goto-char (point-min))
;; \\ is used for a new line. Each one leads to a number
(cl-incf counter (count-matches "\\\\$"))
;; unless there are nonumbers.
(goto-char (point-min))
(cl-decf counter (count-matches "\\nonumber")))))
(t
(cons begin nil)))))
(when (setq numberp (cdr (assoc (point) results)))
(setf (car args)
(concat
(format "\\setcounter{equation}{%s}\n" numberp)
(car args)))))
(apply orig-func args))
(defun scimax-toggle-latex-equation-numbering ()
"Toggle whether LaTeX fragments are numbered."
(interactive)
(if (not (get 'scimax-org-renumber-environment 'enabled))
(progn
(advice-add 'org-create-formula-image :around #'scimax-org-renumber-environment)
(put 'scimax-org-renumber-environment 'enabled t)
(message "Latex numbering enabled"))
(advice-remove 'org-create-formula-image #'scimax-org-renumber-environment)
(put 'scimax-org-renumber-environment 'enabled nil)
(message "Latex numbering disabled.")))
(defun scimax-org-inject-latex-fragment (orig-func &rest args)
"Advice function to inject latex code before and/or after the equation in a latex fragment.
You can use this to set \\mathversion{bold} for example to make
it bolder. The way it works is by defining
:latex-fragment-pre-body and/or :latex-fragment-post-body in the
variable `org-format-latex-options'. These strings will then be
injected before and after the code for the fragment before it is
made into an image."
(setf (car args)
(concat
(or (plist-get org-format-latex-options :latex-fragment-pre-body) "")
(car args)
(or (plist-get org-format-latex-options :latex-fragment-post-body) "")))
(apply orig-func args))
(defun scimax-toggle-inject-latex ()
"Toggle whether you can insert latex in fragments."
(interactive)
(if (not (get 'scimax-org-inject-latex-fragment 'enabled))
(progn
(advice-add 'org-create-formula-image :around #'scimax-org-inject-latex-fragment)
(put 'scimax-org-inject-latex-fragment 'enabled t)
(message "Inject latex enabled"))
(advice-remove 'org-create-formula-image #'scimax-org-inject-latex-fragment)
(put 'scimax-org-inject-latex-fragment 'enabled nil)
(message "Inject latex disabled")))
;; * automatic latex toggling
;; https://ivanaf.com/Automatic_Latex_Fragment_Toggling_in_org-mode.html This
;; makes fragments automatically appear when your cursor leaves them, and
;; disappear when your cursor enters them.
(defvar org-latex-fragment-last nil
"Holds last fragment/environment you were on.")
(defun my/org-latex-fragment--get-current-latex-fragment ()
"Return the overlay associated with the image under point."
(car (--select (eq (overlay-get it 'org-overlay-type) 'org-latex-overlay) (overlays-at (point)))))
(defun my/org-in-latex-fragment-p ()
"Return the point where the latex fragment begins, if inside
a latex fragment. Else return false"
(let* ((el (org-element-context))
(el-type (car el)))
(and (or (eq 'latex-fragment el-type) (eq 'latex-environment el-type))
(org-element-property :begin el))))
(defun org-latex-fragment-toggle-auto ()
"Run an idle timer to toggle fragments."
;; Wait for the s
(interactive)
(while-no-input
(run-with-idle-timer 0.05 nil 'org-latex-fragment-toggle-helper)))
(defun org-latex-fragment-toggle-helper ()
"Toggle a latex fragment image "
(condition-case nil
(and (eq 'org-mode major-mode)
(let* ((begin (my/org-in-latex-fragment-p)))
(cond
;; were on a fragment and now on a new fragment
((and
;; fragment we were on
org-latex-fragment-last
;; and are on a fragment now
begin
;; but not on the last one this is a little tricky. as you edit the
;; fragment, it is not equal to the last one. We use the begin
;; property which is less likely to change for the comparison.
(not (= begin
org-latex-fragment-last)))
;; go back to last one and put image back
(save-excursion
(goto-char org-latex-fragment-last)
(when (my/org-in-latex-fragment-p)
(funcall scimax-toggle-latex-fragment-func))
;; now remove current imagea
(goto-char begin)
(let ((ov (my/org-latex-fragment--get-current-latex-fragment)))
(when ov
(delete-overlay ov)))
;; and save new fragment
(setq org-latex-fragment-last begin)))
;; were on a fragment and now are not on a fragment
((and
;; not on a fragment now
(not begin)
;; but we were on one
org-latex-fragment-last)
;; put image back on
(save-excursion
(goto-char org-latex-fragment-last)
(when (my/org-in-latex-fragment-p)
(funcall scimax-toggle-latex-fragment-func)))
;; unset last fragment
(setq org-latex-fragment-last nil))
;; were not on a fragment, and now are
((and
;; we were not one one
(not org-latex-fragment-last)
;; but now we are
begin)
(save-excursion
(goto-char begin)
;; remove image
(let ((ov (my/org-latex-fragment--get-current-latex-fragment)))
(when ov
(delete-overlay ov)))
(setq org-latex-fragment-last begin)))
;; else not on a fragment
((not begin)
(setq org-latex-fragment-last nil)))))
(error nil)))
(defun scimax-latex-toggle-auto-fragment ()
"Turn toggling fragments automatically to on.
Note: When scrolling, this sometimes causes problems where you
get a lot of Quit messages. Turning this on globally is probably
the wrong thing to do since we only need it in an org file, so we
make the hook local. "
(interactive)
(if (not (get 'org-latex-fragment-toggle-auto 'enabled))
(progn
(add-hook 'post-command-hook 'org-latex-fragment-toggle-auto nil t)
(put 'org-latex-fragment-toggle-auto 'enabled t)
(message "Auto fragment enabled."))
(remove-hook 'post-command-hook 'org-latex-fragment-toggle-auto t)
(put 'org-latex-fragment-toggle-auto 'enabled nil)
(message "Auto fragment disabled.")))
(byte-compile 'org-latex-fragment-toggle-helper)
(byte-compile 'org-latex-fragment-toggle-auto)
(provide 'scimax-org-latex)
;;; scimax-org-latex.el ends here