├── README.org └── lazytab.el /README.org: -------------------------------------------------------------------------------- 1 | #+title: LazyTab - Easy Matrix, Array or Tabular input in LaTeX 2 | 3 | Little piece of configuration that uses =orgtbl-mode= for Emacs to simplify typing in arrays and amsmath matrix environments, along with text tables. 4 | 5 | Instructions forthcoming... at some point. For now you can just look at the code. Or this demo: 6 | 7 | https://user-images.githubusercontent.com/8607532/139576107-2df5e380-ccc0-44c1-b94f-91576cea7ee1.mp4 8 | 9 | -------------------------------------------------------------------------------- /lazytab.el: -------------------------------------------------------------------------------- 1 | ;; -*- lexical-binding: t; -*- 2 | (require 'cdlatex) 3 | (require 'org-table) 4 | 5 | (defun lazytab-position-cursor-and-edit () 6 | ;; (if (search-backward "\?" (- (point) 100) t) 7 | ;; (delete-char 1)) 8 | (cdlatex-position-cursor) 9 | (lazytab-orgtbl-edit)) 10 | 11 | (defun lazytab-orgtbl-edit () 12 | (when (memq major-mode '(LaTeX-mode latex-mode)) 13 | (advice-add 'orgtbl-ctrl-c-ctrl-c :after #'lazytab-orgtbl-replace) 14 | (orgtbl-mode 1) 15 | (open-line 1) 16 | (insert "\n|"))) 17 | 18 | (defun lazytab-orgtbl-replace (_) 19 | (interactive "P") 20 | (unless (org-at-table-p) (user-error "Not at a table")) 21 | (let* ((table (org-table-to-lisp)) 22 | (params '(:backend latex :raw t)) 23 | (replacement-table 24 | (if (texmathp) 25 | (lazytab-orgtbl-to-amsmath table params) 26 | (orgtbl-to-latex table params)))) 27 | (kill-region (org-table-begin) (org-table-end)) 28 | (open-line 1) 29 | (push-mark) 30 | (insert replacement-table) 31 | (align-regexp (region-beginning) (region-end) "\\([:space:]*\\)& ") 32 | (orgtbl-mode -1) 33 | (advice-remove 'orgtbl-ctrl-c-ctrl-c #'lazytab-orgtbl-replace))) 34 | 35 | (defun lazytab-orgtbl-to-amsmath (table params) 36 | (orgtbl-to-generic 37 | table 38 | (org-combine-plists 39 | '(:splice t 40 | :lstart "" 41 | :lend " \\\\" 42 | :sep " & " 43 | :hline nil 44 | :llend "") 45 | params))) 46 | 47 | (defun lazytab-cdlatex-or-orgtbl-next-field () 48 | (when (and (bound-and-true-p orgtbl-mode) 49 | (org-table-p) 50 | (looking-at "[[:space:]]*\\(?:|\\|$\\)") 51 | (let ((s (thing-at-point 'sexp))) 52 | (not (and s (assoc s cdlatex-command-alist-comb))))) 53 | (call-interactively #'org-table-next-field) 54 | t)) 55 | 56 | ;;;###autoload 57 | (defun lazytab-org-table-next-field-maybe () 58 | (interactive) 59 | (if (bound-and-true-p cdlatex-mode) 60 | (cdlatex-tab) 61 | (org-table-next-field))) 62 | 63 | 64 | ;;;###autoload 65 | (define-minor-mode lazytab-mode 66 | "Type in matrices, arrays and tables in LaTeX buffers with 67 | orgtbl syntax." 68 | :global nil 69 | (if lazytab-mode 70 | (progn (require 'org-table) 71 | (define-key orgtbl-mode-map (kbd "") 'lazytab-org-table-next-field-maybe) 72 | (define-key orgtbl-mode-map (kbd "TAB") 'lazytab-org-table-next-field-maybe) 73 | (add-hook 'cdlatex-tab-hook 'lazytab-cdlatex-or-orgtbl-next-field)) 74 | (define-key orgtbl-mode-map (kbd "") 'org-table-next-field) 75 | (define-key orgtbl-mode-map (kbd "TAB") 'org-table-next-field) 76 | (remove-hook 'cdlatex-tab-hook 'lazytab-cdlatex-or-orgtbl-next-field))) 77 | 78 | 79 | (provide 'lazytab) 80 | ;;; lazytab.el ends here 81 | --------------------------------------------------------------------------------