forked from SystemCrafters/crafted-emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrafted-latex.el
137 lines (106 loc) · 4.99 KB
/
crafted-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
;;; crafted-latex.el -*- lexical-binding: t; -*-
;; Copyright (C) 2022
;; SPDX-License-Identifier: MIT
;; Author: System Crafters Community
;; Commentary
;; Configure AUCTEX for editing LaTeX files. Provides customization
;; for various environments to provide some useful additions related
;; to drawing graphs and mathematical diagrams, and code listings.
;;; Code:
(defgroup crafted-latex '()
"LaTeX configuration for Crafted Emacs."
:tag "Crafted LaTeX"
:group 'crafted)
(defcustom crafted-latex-latexp (executable-find "latex")
"Fully qualified path to the `latex' executable"
:tag "`latex' executable location"
:group 'crafted-latex
:type 'string)
(defcustom crafted-latex-latexmkp (executable-find "latexmk")
"Fully qualified path to the `latexmk' executable"
:tag "`latexmk' executable location"
:group 'crafted-latex
:type 'string)
(defcustom crafted-latex-use-pdf-tools nil
"Use pdf-tools as the pdf reader
(this is automatic if you load `crafted-pdf-reader')"
:tag "Use pdf-tools as pdf reader"
:group 'crafted-latex
:type 'boolean)
(defcustom crafted-latex-inhibit-latexmk t
"When set to `nil', the package auctex-latexmk gets installed if the
latex and latexmk executable are found
This package contains a bug which might make it crash during loading
(with a bug related to tex-buf) on newer systems. For this reason, we inhibit
the installation of this package by default.
If you encounter the bug, you keep this package inhibited. You can install
a fix (not on melpa) with the following recipe, and the configuration in this file
will still work
'(auctex-latexmk :fetcher git :host github :repo \"wang1zhen/auctex-latexmk\")"
:tag "Inhibit using `latexmk' command from auxtex-latexmk"
:group 'crafted-latex
:type 'boolean)
;; only install and load auctex when the latex executable is found,
;; otherwise it crashes when loading
(when crafted-latex-latexp
(crafted-package-install-package 'auctex)
(with-eval-after-load 'latex
(customize-set-variable 'TeX-auto-save t)
(customize-set-variable 'TeX-parse-self t)
(setq-default TeX-master nil)
;; compile to pdf
(tex-pdf-mode)
;; correlate the source and the output
(TeX-source-correlate-mode)
;; set a correct indentation in a few additional environments
(add-to-list 'LaTeX-indent-environment-list '("lstlisting" current-indentation))
(add-to-list 'LaTeX-indent-environment-list '("tikzcd" LaTeX-indent-tabular))
(add-to-list 'LaTeX-indent-environment-list '("tikzpicture" current-indentation))
;; add a few macros and environment as verbatim
(add-to-list 'LaTeX-verbatim-environments "lstlisting")
(add-to-list 'LaTeX-verbatim-environments "Verbatim")
(add-to-list 'LaTeX-verbatim-macros-with-braces "lstinline")
(add-to-list 'LaTeX-verbatim-macros-with-delims "lstinline")
;; to use pdfview with auctex
(when crafted-latex-use-pdf-tools
(customize-set-variable 'TeX-view-program-selection '((output-pdf "PDF Tools")))
(customize-set-variable 'TeX-view-program-list '(("PDF Tools" TeX-pdf-tools-sync-view)))
(customize-set-variable 'TeX-source-correlate-start-server t))
;; electric pairs in auctex
(customize-set-variable 'TeX-electric-sub-and-superscript t)
(customize-set-variable 'LaTeX-electric-left-right-brace t)
(customize-set-variable 'TeX-electric-math (cons "$" "$"))
;; open all buffers with the math mode and auto-fill mode
(add-hook 'LaTeX-mode-hook #'auto-fill-mode)
(add-hook 'LaTeX-mode-hook #'LaTeX-math-mode)
;; add support for references
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)
(customize-set-variable 'reftex-plug-into-AUCTeX t)
;; to have the buffer refresh after compilation
(add-hook 'TeX-after-compilation-finished-functions #'TeX-revert-document-buffer)))
;; message the user if the latex executable is not found
(add-hook 'tex-mode-hook
#'(lambda () (unless crafted-latex-latexp (message "latex executable not found"))))
;; The following is to use auctex with latexmk.
(defun crafted-latex--install-latexmk ()
"install the auctex-latexmk package when the latex and latexmk executable
are found (see `crafted-latex-inhibit-latexmk' )"
(when (and crafted-latex-latexp
crafted-latex-latexmkp)
(crafted-package-install-package 'auctex-latexmk)))
(defun crafted-latex--watch-inhibit-latexmk (sym val op buf)
"watcher for the `crafted-latex-inhibit-latexmk' variable"
(unless val
(crafted-latex--install-latexmk)))
(add-variable-watcher 'crafted-latex-inhibit-latexmk
#'crafted-latex--watch-inhibit-latexmk)
(when (and crafted-latex-latexp
crafted-latex-latexmkp)
(with-eval-after-load 'latex
(when (require 'auctex-latexmk nil 'noerror)
(with-eval-after-load 'auctex-latexmk
(auctex-latexmk-setup)
(customize-set-variable 'auctex-latexmk-inherit-TeX-PDF-mode t))
(add-hook 'TeX-mode-hook #'(lambda () (setq TeX-command-default "LatexMk"))))))
(provide 'crafted-latex)
;;; crafted-latex.el ends here