(use-package latex :ensure auctex :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 cdlatex :ensure t :hook (LaTeX-mode . turn-on-cdlatex) :bind (:map cdlatex-mode-map ("" . cdlatex-tab))) (use-package cdlatex :hook ((cdlatex-tab . yas-expand) (cdlatex-tab . cdlatex-in-yas-field)) :config (use-package yasnippet :bind (:map yas-keymap ("" . yas-next-field-or-cdlatex) ("TAB" . yas-next-field-or-cdlatex)) :config (defun cdlatex-in-yas-field () ;; Check if we're at the end of the Yas field (when-let* ((_ (overlayp yas--active-field-overlay)) (end (overlay-end yas--active-field-overlay))) (if (>= (point) end) ;; Call yas-next-field if cdlatex can't expand here (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)))))