summaryrefslogtreecommitdiffstats
path: root/lisp/ravi-init-tex.el
diff options
context:
space:
mode:
authorRavi R Kiran <aine.marina@gmail.com>2021-11-15 04:23:57 (GMT)
committerRavi R Kiran <aine.marina@gmail.com>2021-11-15 04:23:57 (GMT)
commitfae2c3016c6d36bfdf19e615680fd486ea4a1b74 (patch)
tree955e924abb6331fc33ab46fcb7fcdd33eb1c2470 /lisp/ravi-init-tex.el
parentb19558153e9ee3ea1fb76ca569d9c7fe1dbadc26 (diff)
downloaddotemacs-fae2c3016c6d36bfdf19e615680fd486ea4a1b74.zip
dotemacs-fae2c3016c6d36bfdf19e615680fd486ea4a1b74.tar.gz
dotemacs-fae2c3016c6d36bfdf19e615680fd486ea4a1b74.tar.bz2
Completely revamp tex support
Diffstat (limited to 'lisp/ravi-init-tex.el')
-rw-r--r--lisp/ravi-init-tex.el330
1 files changed, 159 insertions, 171 deletions
diff --git a/lisp/ravi-init-tex.el b/lisp/ravi-init-tex.el
index b33c260..892aa0e 100644
--- a/lisp/ravi-init-tex.el
+++ b/lisp/ravi-init-tex.el
@@ -24,180 +24,168 @@
;;; Code:
-(use-package tex-site
+(use-package tex
:ensure auctex
- :mode ("\\.tex\\'" . TeX-latex-mode)
- :commands (TeX-latex-mode
- TeX-mode
- tex-mode
- LaTeX-mode
- latex-mode)
+ :commands (TeX-mode)
:config
- (progn
- (use-package latex
- :config
- (progn
- (use-package reftex
- :defer t
- :diminish reftex-mode
- :ensure nil)
- ;; fix the "bug" in SP regexp wrap that treats ' as "word"
- (modify-syntax-entry ?' ".")
-
- (require 'smartparens-latex)
- (sp-local-pair 'latex-mode "\\begin" "\\end")
- (sp-local-tag 'latex-mode "\\ba" "\\begin{align*}" "\\end{align*}")
-
- (use-package preview
- :ensure nil
- :if window-system
- :config
- (defun my-LaTeX-preview-math ()
- (interactive)
- (let ((b (save-excursion (while (texmathp) (backward-char 1)) (1- (point))))
- (e (save-excursion (while (texmathp) (forward-char 1)) (point))))
- (preview-region b e)))
- (bind-key "C-<m-key>" 'my-LaTeX-preview-math preview-map))
-
- (use-package font-latex
- :ensure nil
- :if window-system
- :config
- ;; fix italian quote highlight
- (push '("\"<" "\">") font-latex-quote-list))
- (fset 'tex-font-lock-subscript 'ignore)
-
- (sp-with-modes '(tex-mode plain-tex-mode latex-mode)
- (sp-local-pair "\\[" nil :post-handlers '(my-latex-math-block-indent)))
-
- (defun my-latex-math-block-indent (a action c)
- (when (eq action 'insert)
- (newline-and-indent)
- (save-excursion (newline))))
-
- (defun my-latex-compile ()
- (interactive)
- (save-buffer)
- (TeX-command "LaTeX" 'TeX-master-file nil))
- (bind-key "C-M-x" 'my-latex-compile LaTeX-mode-map)
-
- (defvar my-latex-wrap-choices '("emph"
- "textsc"))
- (defvar my-latex-wrap-history nil)
-
- (defun my-latex-wrap (macro-name)
- (interactive (list (ido-completing-read
- "Macro> "
- my-latex-wrap-choices
- nil 'confirm nil my-latex-wrap-history)))
- (when (use-region-p)
- (let ((b (region-beginning))
- (e (region-end)))
- (goto-char e)
- (insert "}")
- (goto-char b)
- (insert "\\" macro-name "{"))))
- (bind-key "C-c w" 'my-latex-wrap LaTeX-mode-map)
-
- (defun my-end-of-environment ()
- (interactive)
- (LaTeX-mark-environment)
- (end-of-region))
-
- (defun my-beginning-of-environment ()
- (interactive)
- (LaTeX-mark-environment)
- (beginning-of-region)
- (deactivate-mark))
-
- (bind-key "M-n" 'my-end-of-environment LaTeX-mode-map)
- (bind-key "M-p" 'my-beginning-of-environment LaTeX-mode-map)
-
- ;; Use okular rather than evince
- (setq TeX-view-program-selection
- '((output-dvi "Okular")
- ;; (output-pdf "Okular")
- (output-pdf "PDF Tools")))
-
- (defun th/pdf-view-revert-buffer-maybe (file)
- (let ((buf (find-buffer-visiting file)))
- (when buf
- (with-current-buffer buf
- (when (derived-mode-p 'pdf-view-mode)
- (pdf-view-revert-buffer nil t))))))
-
- (add-hook 'TeX-after-TeX-LaTeX-command-finished-hook
- #'th/pdf-view-revert-buffer-maybe)
-
- (defun my-latex-remove-command ()
- "Unwrap the expression that point is in or before, also
-removing the command name. By command we understand a symbol
-starting with \\ and followed by a block of text enclosed in {}."
- (interactive)
- (let ((ok (sp-get-enclosing-sexp)))
- (cond
- ;; we're inside the { } block
- (ok
- (progn
- (save-excursion
- (goto-char (sp-get ok :beg))
- (zap-to-char -1 ?\\ ))
- (sp-splice-sexp)))
- ;; test if we are in looking at the command fromt he front
- ((looking-at "\\\\")
- (zap-up-to-char 1 ?{)
- (sp-unwrap-sexp))
- ;; otherwise we're inside the command name
- (t
- (zap-to-char -1 ?\\ )
- (zap-up-to-char 1 ?{)
- (sp-unwrap-sexp)))))
- (bind-key "C-c d" 'my-latex-remove-command LaTeX-mode-map)
- (bind-key "M-RET" 'LaTeX-insert-item LaTeX-mode-map)
-
- (use-package company-math
- :config
- (progn
- (defun ravi/company-math-setup ()
- (setq-local company-backends
- (append '(company-math-symbols-latex company-latex-commands)
- company-backends)))
- (add-hook 'TeX-mode-hook 'ravi/company-math-setup)))
-
- (use-package latex-extra
- :hook (LaTex-mode-hook . latex-extra-mode)
- :diminish latex-extra-mode)
-
- (defun my-LaTeX-mode-init ()
- (setq TeX-auto-save t)
- (setq TeX-parse-self t)
- (TeX-PDF-mode t)
- (setq reftex-plug-into-AUCTeX t)
- (reftex-mode t)
- (TeX-fold-mode t)
- (TeX-source-correlate-mode 1)
-
- (smartparens-mode 1)
-
- (LaTeX-add-environments
- '("derivation" LaTeX-env-label))
- (TeX-add-symbols '("emph" 1))
-
- (setq fill-column 88)
-
- (setq TeX-save-query nil)
- (setq TeX-show-compilation t)
-
- (when (functionp 'helm-dash) (setq-local dash-docs-docsets '("LaTeX")))
- (when (functionp 'ravi/dash) (setq-local ravi/dash-docsets '("LaTeX")))
-
- (message "LaTeX mode init complete."))
- ;; ACUTeX replaces latex-mode-hook with LaTeX-mode-hook
- (add-hook 'LaTeX-mode-hook 'my-LaTeX-mode-init)
- )
- :ensure nil)
- )
+ (setq TeX-auto-save t)
+ (setq TeX-parse-self t)
+ (setq-default TeX-master nil)
+ (setq TeX-save-query nil)
+ (setq TeX-show-compilation t)
+ (defun ravi/tex-common-hook ()
+ (message "Running tex mode common hook")
+ (TeX-PDF-mode t)
+ (TeX-fold-mode t)
+ (TeX-source-correlate-mode 1))
+ :hook (TeX-mode-hook . ravi/tex-common-hook))
+
+(use-package latex
+ :ensure auctex
+ :mode ("\\.tex\\'" . latex-mode)
+ :hook ((LaTeX-mode-hook . prettify-symbols-mode)
+ (LaTeX-mode-hook . ravi/latex-init))
+ :bind (:map LaTeX-mode-map
+ ("C-S-e" . karthinks/latex-math-from-calc))
+ :config
+ ;; Format math as a Latex string with Calc
+ (defun karthinks/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)))))))
+ (defun ravi/latex-init ()
+ (setq fill-column 88)
+ (when (functionp 'helm-dash) (setq-local dash-docs-docsets '("LaTeX")))
+ (when (functionp 'ravi/dash) (setq-local ravi/dash-docsets '("LaTeX"))))
)
+(use-package preview
+ :ensure auctex
+ :after latex
+ :hook ((LaTeX-mode-hook . karthinks/preview-larger-previews))
+ :config
+ (defun karthinks/preview-larger-previews ()
+ (setq preview-scale-function
+ (lambda () (* 1.25
+ (funcall (preview-scale-from-face)))))))
+
+(use-package cdlatex
+ :ensure t
+ :hook (LaTeX-mode-hook . turn-on-cdlatex)
+ :bind (:map cdlatex-mode-map
+ ("<tab>" . cdlatex-tab)))
+
+;; All lazytab functions from https://karthinks.com/software/latex-input-for-impatient-scholars/
+(use-package org-table
+ :ensure nil
+ :after cdlatex
+ :bind (:map orgtbl-mode-map
+ ("<tab>" . lazytab-org-table-next-field-maybe)
+ ("TAB" . lazytab-org-table-next-field-maybe))
+ :hook ((cdlatex-tab-hook . lazytab-cdlatex-or-orgtbl-next-field))
+ :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-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))))
+
+(use-package latex-extra
+ :hook (LaTeX-mode-hook . latex-extra-mode)
+ :diminish latex-extra-mode)
+
+(use-package company-math)
+(use-package company-auctex)
+
+(use-package reftex
+ :after (latex)
+ :hook ((LaTeX-mode-hook . turn-on-reftex))
+ :config
+ (setq reftex-plug-into-AUCTeX t))
+(use-package company-reftex)
+
(provide 'ravi-init-tex)
;;; ravi-init-tex.el ends here