;;; ravi-init-tex.el --- tex and friends ;; Copyright (C) 2014 ;; Author: ;; Keywords: ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;;; Commentary: ;; TeX and LaTeX support ;;; Code: (use-package tex :ensure auctex :commands (TeX-mode) :config (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 () (prettify-symbols-mode 1) (TeX-PDF-mode t) (TeX-fold-mode t) (TeX-source-correlate-mode 1)) (use-package company-math) (use-package company-auctex) :hook (TeX-mode-hook . ravi/tex-common-hook)) (use-package latex :ensure auctex :mode ("\\.tex\\'" . latex-mode) :hook ((LaTeX-mode-hook . ravi/latex-init)) :bind (:map LaTeX-mode-map ("C-S-e" . karthinks/latex-math-from-calc)) :defer t :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 'consult-dash) (setq-local consult-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 :after latex :hook (LaTeX-mode-hook . turn-on-cdlatex) :bind (:map cdlatex-mode-map ("" . 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 ("" . 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 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