Skip to content

Instantly share code, notes, and snippets.

@egstatsml
Last active March 8, 2023 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egstatsml/9d5c573a454b5f0c9f5f6e409af824e5 to your computer and use it in GitHub Desktop.
Save egstatsml/9d5c573a454b5f0c9f5f6e409af824e5 to your computer and use it in GitHub Desktop.

My Latex Setup with Doom. In my init.el file, I have

(latex
 +lsp
 +latexmk
 +cdlatex
 +fold)             ; writing papers in Emacs has never been so fun

Some keybindings for latex

Want to make a keybinding for reftex and citar.

(map!
 :map (LaTeX-mode-map latex-mode-map tex-mode-map)
 :localleader
 (:prefix-map ("i" . "insert")
  ;; citation keybinding also used in org mode. If I change it here I should change
  ;; it there as well for consistency
  :desc "citation" "c" #'citar-insert-citation
  :desc "cleveref" "r" #'reftex-cleveref-cref
  :desc "Cleveref" "R" #'reftex-cleveref-Cref
  :desc "label" "l" #'reftex-label))

LSP and linting my writing

I want some help with writing, just to make sure I am writing to follow conventions when needed, and to give me some feedback on when I am going astray. I also want some help with LaTeX errors that will come my way. These tools (texidote and lsp) are here to help.

(after! tex
  ;; define a checker for texidote
  (flycheck-define-checker tex-textidote
    "A LaTeX grammar/spelling checker using textidote.

     See https://github.com/sylvainhalle/textidote"
    :modes (latex-mode plain-tex-mode)
    :command ("java" "-jar" (eval (expand-file-name "~/.local/bin/textidote.jar"))
              "--read-all"
              "--output" "singleline"
              "--no-color"
              "--check"   (eval (if ispell-current-dictionary (substring ispell-current-dictionary 0 2) "en_UK"))
              ;; Try to honor local aspell dictionary and replacements if they exist
              "--dict"    (eval (expand-file-name "~/.aspell.en.pws"))
              "--replace" (eval (expand-file-name "~/.aspell.en.prepl"))
              ;; Using source ensures that a single temporary file in a different dir is created
              ;; such that textidote won't process other files. This serves as a hacky workaround for
              ;; https://github.com/sylvainhalle/textidote/issues/200.
              source)
    :error-patterns ((warning line-start (file-name)
                              "(L" line "C" column "-" (or (seq "L" end-line "C" end-column) "?") "): "
                              (message (one-or-more (not "\""))) (one-or-more not-newline) line-end)))
  (add-to-list 'flycheck-checkers 'tex-textidote)
  ;; (add-hook 'latex-mode-hook
  ;;           (lambda ()
  ;;             (flycheck-add-next-checker 'tex-textidote 'tex-chktex))))

(add-hook 'lsp-managed-mode-hook
          (lambda ()
            (when (derived-mode-p 'latex-mode)
              (setq ethan/flycheck-local-cache '((lsp . ((next-checkers . (tex-textidote))))))))))

pdf viewing

  ;; Use pdf-tools to open PDF files
(after! tex
  (setq TeX-view-program-selection '((output-pdf "PDF Tools"))
        TeX-source-correlate-start-server t)
  ;; Update PDF buffers after successful LaTeX runs
  (add-hook 'TeX-after-compilation-finished-functions
             #'TeX-revert-document-buffer))

Adding Nomenclature command

;; nomenclature for latex
(after! tex
  '(add-to-list 'TeX-command-list
                '("Nomenclature" "makeindex %s.nlo -s nomencl.ist -o %s.nls"
                  (lambda (name command file)
                    (TeX-run-compile name command file)
                    (TeX-process-set-variable file 'TeX-command-next TeX-command-default))
                  nil t :help "Create nomenclature file")))

Adding -no-escape shell flag to latexmk

(after! tex
  '(add-to-list 'TeX-command-list
                '("LatexMk-shell-escape" "latexmk -shell-escape %(-PDF)%S%(mode) %(file-line-error) %(extraopts) %t" TeX-run-latexmk nil
                 (plain-tex-mode latex-mode doctex-mode)
                 :help "Run LatexMk with -shell escape flag")))

Prettyfying LaTeX mode (amongst other things) using Xenops.

(package! xenops
  :recipe (:host github :repo "dandavison/xenops"))
(use-package! xenops
  :after tex
  :if is/graphical-p
  :config
  (add-hook 'latex-mode-hook #'xenops-mode)
  (add-hook 'LaTeX-mode-hook #'xenops-mode)
  ;; making xenops reveal on entry
  (setq xenops-reveal-on-entry t)
  ;; making the background be inherited from my theme
  (setq org-format-latex-options
    (plist-put org-format-latex-options :background 'default))
  ;; making the equations and displays a little bit bigger
  (setq org-format-latex-options
    (plist-put org-format-latex-options :scale 1.5))
  (setq xenops-math-image-scale-factor 1.3))

With Xenops, the newline operator mapped to “o” in normal mode doesn’t work as expected sometimes when I am in a LaTeX environment. To fix this I have just defined my own.

(defun my/xenops-newline ()
  "my newline command when xenops is out and about
   Basically:
1. go down one
2. insert new line
3. go back up after inserting new line
4. enter insert mode"
  (interactive)
  (evil-next-line)
  (evil-beginning-of-line)
  (newline)
  (evil-previous-line)
  (LaTeX-indent))

;; map it to when xenops is around and only in normal ":n" mode
(map! :mode xenops-mode
      :desc "my/xenops-newline" :n "o" #'my/xenops-newline)

Useful Snippets

(package! laas
  :recipe (:host github :repo "tecosaur/LaTeX-auto-activating-snippets"))
(use-package! laas
  :hook (LaTeX-mode . laas-mode)
  :config ; do whatever here
  (aas-set-snippets 'laas-mode
    ;; some snippets for general use
    ;; glossery
    ;; the missing final curly bracket is because one will automatically be inserted
    "gls" (lambda () (interactive)
            (yas-expand-snippet "\\gls{$0}"))
    "Gls" (lambda () (interactive)
            (yas-expand-snippet "\\Gls{$0}"))
    ;; gps is obviously an acronym that is already used, but not one I use that often
    "gps" (lambda () (interactive)
            (yas-expand-snippet "\\glspl{$0}"))
    "Gps" (lambda () (interactive)
            (yas-expand-snippet "\\Glspl{$0}"))
    ;; set condition!
    :cond #'texmathp ; expand only while in math
    "supp" "\\supp"
    "On" "O(n)"
    "O1" "O(1)"
    "Olog" "O(\\log n)"
    "Olon" "O(n \\log n)"
    ;; bind to functions!
    "Sum" (lambda () (interactive)
            (yas-expand-snippet "\\sum_{$1}^{$2} $0"))
    "Span" (lambda () (interactive)
             (yas-expand-snippet "\\Span($1)$0"))
    ;; add accent snippets
    :cond #'laas-object-on-left-condition
    "qq" (lambda () (interactive) (laas-wrap-previous-object "sqrt"))))

Writeroom for TeX

I want to disable line numbers if I have enabled writeroom mode. Line numbers sort of defeat the purpose. Also want to adjust the scaling the is done.

(defun writeroom-disable-linenumbers ()
  (when display-line-numbers
      (display-line-numbers-mode)))

(after! (tex zen)
  (add-hook 'writeroom-mode-hook 'writeroom-disable-linenumbers)
  (setq +zen-text-scale 1.2)
  (setq writeroom-width 110))

Formatting with Latexindent

I am using apheleia to format all my code now, but I want to have it support linewrapping when it can. For this I just need to add the ‘-m’ flag to latexindent

(after! apheleia
  (setf (alist-get 'latexindent apheleia-formatters)
  '("latexindent" "--logfile=/dev/null" "-m")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment