-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path+latex.el
572 lines (527 loc) · 20.6 KB
/
+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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
;;; +latex.el -*- lexical-binding: t; -*-
(use-package! latex
:hook ((LaTeX-mode . prettify-symbols-mode))
:bind (:map LaTeX-mode-map
("C-S-e" . latex-math-from-calc))
:config
;; Format math as a Latex string with Calc
(defun latex-math-from-calc ()
"Evaluate calc on the contents of line at point."
(interactive)
(cond ((region-active-p)
(let* ((beg (region-beginning))
(end (region-end))
(string (buffer-substring-no-properties beg end)))
(kill-region beg end)
(insert (calc-eval `(,string calc-language latex
calc-prefer-frac t
calc-angle-mode rad)))))
(t (let ((l (thing-at-point 'line)))
(end-of-line 1) (kill-line 0)
(insert (calc-eval `(,l
calc-language latex
calc-prefer-frac t
calc-angle-mode rad))))))))
(use-package! preview
:after latex
:hook ((LaTeX-mode . preview-larger-previews))
:config
(defun preview-larger-previews ()
(setq preview-scale-function
(lambda () (* 1.25
(funcall (preview-scale-from-face)))))))
;; CDLatex settings
(use-package! cdlatex
:hook (LaTeX-mode . turn-on-cdlatex)
:bind (:map cdlatex-mode-map
("<tab>" . cdlatex-tab)))
;; Yasnippet settings
(use-package! yasnippet
:hook ((LaTeX-mode . yas-minor-mode)
(post-self-insert . my/yas-try-expanding-auto-snippets))
:config
(use-package! warnings
:config
(cl-pushnew '(yasnippet backquote-change)
warning-suppress-types
:test 'equal))
(setq yas-triggers-in-field t)
;; Function that tries to autoexpand YaSnippets
;; The double quoting is NOT a typo!
(defun my/yas-try-expanding-auto-snippets ()
(when (and (boundp 'yas-minor-mode) yas-minor-mode)
(let ((yas-buffer-local-condition ''(require-snippet-condition . auto)))
(yas-expand)))))
;; CDLatex integration with YaSnippet: Allow cdlatex tab to work inside Yas
;; fields
(use-package! cdlatex
:hook ((cdlatex-tab . yas-expand)
(cdlatex-tab . cdlatex-in-yas-field))
:config
(use-package! yasnippet
:bind (:map yas-keymap
("<tab>" . yas-next-field-or-cdlatex)
("TAB" . yas-next-field-or-cdlatex))
:config
(defun cdlatex-in-yas-field ()
(when-let* ((_ (overlayp yas--active-field-overlay))
(end (overlay-end yas--active-field-overlay)))
(if (>= (point) end)
(let ((s (thing-at-point 'sexp)))
(unless (and s (assoc (substring-no-properties s)
cdlatex-command-alist-comb))
(yas-next-field-or-maybe-expand)
t))
;; otherwise expand and jump to the correct location
(let (cdlatex-tab-hook minp)
(setq minp
(min (save-excursion (cdlatex-tab)
(point))
(overlay-end yas--active-field-overlay)))
(goto-char minp) t))))
(defun yas-next-field-or-cdlatex nil
(interactive)
"Jump to the next Yas field correctly with cdlatex active."
(if
(or (bound-and-true-p cdlatex-mode)
(bound-and-true-p org-cdlatex-mode))
(cdlatex-tab)
(yas-next-field-or-maybe-expand)))))
;; Array/tabular input with org-tables and cdlatex
(use-package! org-table
:after cdlatex
:bind (:map orgtbl-mode-map
("<tab>" . lazytab-org-table-next-field-maybe)
("TAB" . lazytab-org-table-next-field-maybe))
:init
(add-hook 'cdlatex-tab-hook 'lazytab-cdlatex-or-orgtbl-next-field 90)
;; Tabular environments using cdlatex
(add-to-list 'cdlatex-command-alist '("smat" "Insert smallmatrix env"
"\\left( \\begin{smallmatrix} ? \\end{smallmatrix} \\right)"
lazytab-position-cursor-and-edit
nil nil t))
(add-to-list 'cdlatex-command-alist '("bmat" "Insert bmatrix env"
"\\begin{bmatrix} ? \\end{bmatrix}"
lazytab-position-cursor-and-edit
nil nil t))
(add-to-list 'cdlatex-command-alist '("pmat" "Insert pmatrix env"
"\\begin{pmatrix} ? \\end{pmatrix}"
lazytab-position-cursor-and-edit
nil nil t))
(add-to-list 'cdlatex-command-alist '("tbl" "Insert table"
"\\begin{table}\n\\centering ? \\caption{}\n\\end{table}\n"
lazytab-position-cursor-and-edit
nil t nil))
:config
;; Tab handling in org tables
(defun lazytab-position-cursor-and-edit ()
;; (if (search-backward "\?" (- (point) 100) t)
;; (delete-char 1))
(cdlatex-position-cursor)
(lazytab-orgtbl-edit))
(defun lazytab-orgtbl-edit ()
(advice-add 'orgtbl-ctrl-c-ctrl-c :after #'lazytab-orgtbl-replace)
(orgtbl-mode 1)
(open-line 1)
(insert "\n|"))
(defun lazytab-orgtbl-replace (_)
(interactive "P")
(unless (org-at-table-p) (user-error "Not at a table"))
(let* ((table (org-table-to-lisp))
params
(replacement-table
(if (texmathp)
(lazytab-orgtbl-to-amsmath table params)
(orgtbl-to-latex table params))))
(kill-region (org-table-begin) (org-table-end))
(open-line 1)
(push-mark)
(insert replacement-table)
(align-regexp (region-beginning) (region-end) "\\([:space:]*\\)& ")
(orgtbl-mode -1)
(advice-remove 'orgtbl-ctrl-c-ctrl-c #'lazytab-orgtbl-replace)))
(defun lazytab-orgtbl-to-amsmath (table params)
(orgtbl-to-generic
table
(org-combine-plists
'(:splice t
:lstart ""
:lend " \\\\"
:sep " & "
:hline nil
:llend "")
params)))
(defun lazytab-cdlatex-or-orgtbl-next-field ()
(when (and (bound-and-true-p orgtbl-mode)
(org-at-table-p)
(looking-at "[[:space:]]*\\(?:|\\|$\\)")
(let ((s (thing-at-point 'sexp)))
(not (and s (assoc s cdlatex-command-alist-comb)))))
(call-interactively #'org-table-next-field)
t))
(defun lazytab-org-table-next-field-maybe ()
(interactive)
(if (bound-and-true-p cdlatex-mode)
(cdlatex-tab)
(org-table-next-field))))
;; Calctex
(use-package! calctex
:commands calctex-mode
:init
(add-hook 'calc-mode-hook #'calctex-mode)
:config
(setq calctex-additional-latex-packages "
\\usepackage[usenames]{xcolor}
\\usepackage{soul}
\\usepackage{adjustbox}
\\usepackage{amsmath}
\\usepackage{amssymb}
\\usepackage{siunitx}
\\usepackage{cancel}
\\usepackage{mathtools}
\\usepackage{mathalpha}
\\usepackage{xparse}
\\usepackage{arevmath}"
calctex-additional-latex-macros
(concat calctex-additional-latex-macros
"\n\\let\\evalto\\Rightarrow"))
(defadvice! no-messaging-a (orig-fn &rest args)
:around #'calctex-default-dispatching-render-process
(let ((inhibit-message t) message-log-max)
(apply orig-fn args)))
;; Fix hardcoded dvichop path (whyyyyyyy)
(let ((vendor-folder (concat (file-truename doom-local-dir)
"straight/"
(format "build-%s" emacs-version)
"/calctex/vendor/")))
(setq calctex-dvichop-sty (concat vendor-folder "texd/dvichop")
calctex-dvichop-bin (concat vendor-folder "texd/dvichop")))
(unless (file-exists-p calctex-dvichop-bin)
(message "CalcTeX: Building dvichop binary")
(let ((default-directory (file-name-directory calctex-dvichop-bin)))
(call-process "make" nil nil nil))))
(map! :map calc-mode-map
:after calc
:localleader
:desc "Embedded calc (toggle)" "e" #'calc-embedded)
(map! :map org-mode-map
:after org
:localleader
:desc "Embedded calc (toggle)" "E" #'calc-embedded)
(map! :map latex-mode-map
:after latex
:localleader
:desc "Embedded calc (toggle)" "e" #'calc-embedded)
(defvar calc-embedded-trail-window nil)
(defvar calc-embedded-calculator-window nil)
(defadvice! calc-embedded-with-side-pannel (&rest _)
:after #'calc-do-embedded
(when calc-embedded-trail-window
(ignore-errors
(delete-window calc-embedded-trail-window))
(setq calc-embedded-trail-window nil))
(when calc-embedded-calculator-window
(ignore-errors
(delete-window calc-embedded-calculator-window))
(setq calc-embedded-calculator-window nil))
(when (and calc-embedded-info
(> (* (window-width) (window-height)) 1200))
(let ((main-window (selected-window))
(vertical-p (> (window-width) 80)))
(select-window
(setq calc-embedded-trail-window
(if vertical-p
(split-window-horizontally (- (max 30 (/ (window-width) 3))))
(split-window-vertically (- (max 8 (/ (window-height) 4)))))))
(switch-to-buffer "*Calc Trail*")
(select-window
(setq calc-embedded-calculator-window
(if vertical-p
(split-window-vertically -6)
(split-window-horizontally (- (/ (window-width) 2))))))
(switch-to-buffer "*Calculator*")
(select-window main-window))))
(setq TeX-save-query nil
TeX-show-compilation t
TeX-command-extra-options "-shell-escape")
(after! latex
(add-to-list 'TeX-command-list '("XeLaTeX" "%`xelatex%(mode)%' %t" TeX-run-TeX nil t)))
(setq tec/yas-latex-template-preamble "
<<latex-nice-preamble>>
")
(defun tec/yas-latex-get-class-choice ()
"Prompt user for LaTeX class choice"
(setq tec/yas-latex-class-choice (completing-read "Select document class: " '("article" "scrartcl" "bmc"))))
(defun tec/yas-latex-preamble-if ()
"Based on class choice prompt for insertion of default preamble"
(if (equal tec/yas-latex-class-choice "bmc") 'nil
(eq (read-char-choice "Include default preamble? [Type y/n]" '(?y ?n)) ?y)))
(after! tex
(defvar tec/tex-last-delim-char nil)
(defvar tec/tex-delim-dot-second t)
(defun tec/get-open-delim-char ()
"Exclusivly read next char to tec/tex-last-delim-char"
(setq tec/tex-delim-dot-second nil)
(setq tec/tex-last-delim-char (read-char-exclusive "Opening deliminator, recognises: 9 ( [ { < | ."))
(when (eql ?. tec/tex-last-delim-char)
(setq tec/tex-delim-dot-second (read-char-exclusive "Other deliminator, recognises: 0 9 ( ) [ ] { } < > |"))))
(defun tec/tex-open-delim-from-char (&optional open-char)
"Find the associated opening delim as string"
(unless open-char (setq open-char (if (eql ?. tec/tex-last-delim-char)
tec/tex-delim-dot-second
tec/tex-last-delim-char)))
(pcase open-char
(?\( "(")
(?9 "(")
(?\[ "[")
(?\{ "\\{")
(?< "<")
(?| (if tec/tex-delim-dot-second "." "|"))
(_ ".")))
(defun tec/tex-close-delim-from-char (&optional open-char)
"Find the associated closing delim as string"
(if tec/tex-delim-dot-second
(pcase tec/tex-delim-dot-second
(?\) ")")
(?0 ")")
(?\] "]")
(?\} "\\}")
(?\> ">")
(?| "|")
(_ "."))
(pcase (or open-char tec/tex-last-delim-char)
(?\( ")")
(?9 ")")
(?\[ "]")
(?\{ "\\}")
(?< ">")
(?\) ")")
(?0 ")")
(?\] "]")
(?\} "\\}")
(?\> ">")
(?| "|")
(_ "."))))
(defun tec/tex-next-char-smart-close-delim (&optional open-char)
(and (bound-and-true-p smartparens-mode)
(eql (char-after) (pcase (or open-char tec/tex-last-delim-char)
(?\( ?\))
(?\[ ?\])
(?{ ?})
(?< ?>)))))
(defun tec/tex-delim-yas-expand (&optional open-char)
(yas-expand-snippet (yas-lookup-snippet "_deliminators" 'latex-mode) (point) (+ (point) (if (tec/tex-next-char-smart-close-delim open-char) 2 1)))))
(after! latex
(setcar (assoc "⋆" LaTeX-fold-math-spec-list) "★")) ;; make \star bigger
(setq TeX-fold-math-spec-list
`(;; missing/better symbols
("≤" ("le"))
("≥" ("ge"))
("≠" ("ne"))
;; convenience shorts -- these do not work nicely ATM
;; ("‹" ("left"))
;; ("›" ("right"))
;; private macros
("ℝ" ("RR"))
("ℕ" ("NN"))
("ℤ" ("ZZ"))
("ℚ" ("QQ"))
("ℂ" ("CC"))
("ℙ" ("PP"))
("ℍ" ("HH"))
("𝔼" ("EE"))
("𝑑" ("dd"))
;; known commands
("" ("phantom"))
(,(lambda (num den) (if (and (TeX-string-single-token-p num) (TeX-string-single-token-p den))
(concat num "/" den)
(concat "❪" num "/" den "❫"))) ("frac"))
(,(lambda (arg) (concat "√" (TeX-fold-parenthesize-as-necessary arg))) ("sqrt"))
(,(lambda (arg) (concat "⭡" (TeX-fold-parenthesize-as-necessary arg))) ("vec"))
("‘{1}’" ("text"))
;; private commands
("|{1}|" ("abs"))
("‖{1}‖" ("norm"))
("⌊{1}⌋" ("floor"))
("⌈{1}⌉" ("ceil"))
("⌊{1}⌉" ("round"))
("𝑑{1}/𝑑{2}" ("dv"))
("∂{1}/∂{2}" ("pdv"))
;; fancification
("{1}" ("mathrm"))
(,(lambda (word) (string-offset-roman-chars 119743 word)) ("mathbf"))
(,(lambda (word) (string-offset-roman-chars 119951 word)) ("mathcal"))
(,(lambda (word) (string-offset-roman-chars 120003 word)) ("mathfrak"))
(,(lambda (word) (string-offset-roman-chars 120055 word)) ("mathbb"))
(,(lambda (word) (string-offset-roman-chars 120159 word)) ("mathsf"))
(,(lambda (word) (string-offset-roman-chars 120367 word)) ("mathtt")))
TeX-fold-macro-spec-list
'(
;; as the defaults
("[f]" ("footnote" "marginpar"))
("[c]" ("cite"))
("[l]" ("label"))
("[r]" ("ref" "pageref" "eqref"))
("[i]" ("index" "glossary"))
("..." ("dots"))
("{1}" ("emph" "textit" "textsl" "textmd" "textrm" "textsf" "texttt"
"textbf" "textsc" "textup"))
;; tweaked defaults
("©" ("copyright"))
("®" ("textregistered"))
("™" ("texttrademark"))
("[1]:||►" ("item"))
("❡❡ {1}" ("part" "part*"))
("❡ {1}" ("chapter" "chapter*"))
("§ {1}" ("section" "section*"))
("§§ {1}" ("subsection" "subsection*"))
("§§§ {1}" ("subsubsection" "subsubsection*"))
("¶ {1}" ("paragraph" "paragraph*"))
("¶¶ {1}" ("subparagraph" "subparagraph*"))
;; extra
("⬖ {1}" ("begin"))
("⬗ {1}" ("end"))))
(defun string-offset-roman-chars (offset word)
"Shift the codepoint of each character in WORD by OFFSET with an extra -6 shift if the letter is lowercase"
(apply 'string
(mapcar (lambda (c)
(string-offset-apply-roman-char-exceptions
(+ (if (>= c 97) (- c 6) c) offset)))
word)))
(defvar string-offset-roman-char-exceptions
'(;; lowercase serif
(119892 . 8462) ; ℎ
;; lowercase caligraphic
(119994 . 8495) ; ℯ
(119996 . 8458) ; ℊ
(120004 . 8500) ; ℴ
;; caligraphic
(119965 . 8492) ; ℬ
(119968 . 8496) ; ℰ
(119969 . 8497) ; ℱ
(119971 . 8459) ; ℋ
(119972 . 8464) ; ℐ
(119975 . 8466) ; ℒ
(119976 . 8499) ; ℳ
(119981 . 8475) ; ℛ
;; fraktur
(120070 . 8493) ; ℭ
(120075 . 8460) ; ℌ
(120076 . 8465) ; ℑ
(120085 . 8476) ; ℜ
(120092 . 8488) ; ℨ
;; blackboard
(120122 . 8450) ; ℂ
(120127 . 8461) ; ℍ
(120133 . 8469) ; ℕ
(120135 . 8473) ; ℙ
(120136 . 8474) ; ℚ
(120137 . 8477) ; ℝ
(120145 . 8484)) ; ℤ
"An alist of deceptive codepoints, and then where the glyph actually resides.")
(defun string-offset-apply-roman-char-exceptions (char)
(if (assoc char string-offset-roman-char-exceptions)
(cdr (assoc char string-offset-roman-char-exceptions))
char))
(defun TeX-fold-parenthesize-as-necessary (tokens &optional suppress-left suppress-right)
(if (TeX-string-single-token-p tokens) tokens
(concat (if suppress-left "" "❪")
tokens
(if suppress-right "" "❫"))))
(defun TeX-string-single-token-p (teststring)
"Return t if TESTSTRING appears to be a single token, nil otherwise"
(if (string-match-p "^\\\\?\\w+$" teststring) t nil))
(after! tex
(map!
:map LaTeX-mode-map
:ei [C-return] #'LaTeX-insert-item)
(setq TeX-electric-math '("\\(" . "")))
;; Making \( \) less visible
(defface unimportant-latex-face
'((t :inherit font-lock-comment-face :weight extra-light))
"Face used to make \\(\\), \\[\\] less visible."
:group 'LaTeX-math)
(font-lock-add-keywords
'latex-mode
`(("\\\\[]()[]" 0 'unimportant-latex-face prepend))
'end)
(setq preview-LaTeX-command '("%`%l \"\\nonstopmode\\nofiles\
\\PassOptionsToPackage{" ("," . preview-required-option-list) "}{preview}\
\\AtBeginDocument{\\ifx\\ifPreview\\undefined"
preview-default-preamble "\\fi}\"%' \"\\detokenize{\" %t \"}\""))
(after! cdlatex
(setq cdlatex-env-alist
'(("bmatrix" "\\begin{bmatrix}\n?\n\\end{bmatrix}" nil)
("equation*" "\\begin{equation*}\n?\n\\end{equation*}" nil)))
(setq ;; cdlatex-math-symbol-prefix ?\; ;; does not work at the moment :(
cdlatex-math-symbol-alist
'( ;; adding missing functions to 3rd level symbols
(?_ ("\\downarrow" "" "\\inf"))
(?2 ("^2" "\\sqrt{?}" ""))
(?3 ("^3" "\\sqrt[3]{?}" ""))
(?^ ("\\uparrow" "" "\\sup"))
(?k ("\\kappa" "" "\\ker"))
(?m ("\\mu" "" "\\lim"))
(?c ("" "\\circ" "\\cos"))
(?d ("\\delta" "\\partial" "\\dim"))
(?D ("\\Delta" "\\nabla" "\\deg"))
;; no idea why \Phi isnt on F in first place, \phi is on f.
(?F ("\\Phi"))
;; now just convenience
(?. ("\\cdot" "\\dots"))
(?: ("\\vdots" "\\ddots"))
(?* ("\\times" "\\star" "\\ast")))
cdlatex-math-modify-alist
'( ;; my own stuff
(?B "\\mathbb" nil t nil nil)
(?a "\\abs" nil t nil nil))))
(use-package! laas
:hook (LaTeX-mode . laas-mode)
:config
(defun laas-tex-fold-maybe ()
(unless (equal "/" aas-transient-snippet-key)
(+latex-fold-last-macro-a)))
(add-hook 'aas-post-snippet-expand-hook #'laas-tex-fold-maybe))
(when (>= emacs-major-version 28)
(add-hook 'latex-mode-hook #'TeX-latex-mode))
(after! org
(require 'org)
(require 'ox-latex)
(add-to-list 'org-latex-classes
'("catster-article" "\\documentclass{scrartcl}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
;; Default packages
(setq org-latex-default-packages-alist
'(("AUTO" "inputenc" t
("pdflatex"))
("T1" "fontenc" t
("pdflatex"))
("" "times" t)
("" "fontspec" t)
("" "graphicx" t)
("letterpaper" "geometry" t)
("" "grffile" t)
("" "longtable" nil)
("" "wrapfig" nil)
("" "rotating" nil)
("normalem" "ulem" t)
("" "amsmath" t)
("" "textcomp" t)
("" "amssymb" t)
("" "capt-of" nil)
("dvipsnames" "xcolor" nil)
("colorlinks=true, linkcolor=Blue, citecolor=BrickRed, urlcolor=PineGreen" "hyperref" nil)
("" "indentfirst" nil)))
(setq org-latex-listings 'minted
org-latex-packages-alist '(("" "minted"))
org-latex-minted-options '(("breaklines" "true")
("breakanywhere" "true")
("mathescape")
("frame" "lines")
("bgcolor" "yellow!5")))
(setq org-latex-pdf-process
'("latexmk -pdflatex='%latex -shell-escape -interaction nonstopmode' -pdf -output-directory=%o -f %f")))