Skip to content

Latest commit

 

History

History
352 lines (261 loc) · 13.1 KB

starter-kit-latex.org

File metadata and controls

352 lines (261 loc) · 13.1 KB

Starter Kit LaTeX

This is part of the Emacs Starter Kit.

Set the engine running latex

With tex comes auctex

Following advice from:

https://emacs.stackexchange.com/questions/41321/when-to-specify-a-package-name-in-use-packages-ensure-tag

Also see: https://emacs.stackexchange.com/questions/41332/how-to-automatically-install-auctex-if-certain-package-is-unable-to-be-activated

Once installed, auctex overrides the tex package. This means that to get auctex working, you need the following:

(use-package tex
  :after org
  :ensure auctex
  :init
  ;; Take this out to compile to DVI instead.
  (add-hook 'LaTeX-mode-hook 'TeX-PDF-mode)

  ;; Biber under AUCTeX (as well as/instead of bibtex
  (defun TeX-run-Biber (name command file)
    "Create a process for NAME using COMMAND to format FILE with Biber."
    (let ((process (TeX-run-command name command file)))
      (setq TeX-sentinel-function 'TeX-Biber-sentinel)
      (if TeX-process-asynchronous
          process
        (TeX-synchronous-sentinel name file process))))

  (defun TeX-Biber-sentinel (process name)
    "Cleanup TeX output buffer after running Biber."
    (goto-char (point-max))
    (cond
     ;; Check whether Biber reports any warnings or errors.
     ((re-search-backward (concat
                           "^(There \\(?:was\\|were\\) \\([0-9]+\\) "
                           "\\(warnings?\\|error messages?\\))") nil t)
      ;; Tell the user their number so that she sees whether the
      ;; situation is getting better or worse.
      (message (concat "Biber finished with %s %s. "
                       "Type `%s' to display output.")
               (match-string 1) (match-string 2)
               (substitute-command-keys
                "\\\\[TeX-recenter-output-buffer]")))
     (t
      (message (concat "Biber finished successfully. "
                       "Run LaTeX again to get citations right."))))
    (setq TeX-command-next TeX-command-default))

  :config
  (setq TeX-parse-self t)  ; Enable parse on load.
  (setq TeX-auto-save t)   ; Enable parse on save.

  (eval-after-load "tex"
    '(add-to-list 'TeX-command-list '("Biber" "biber %s" TeX-run-Biber nil t :help "Run Biber"))))

The Reftex Package

(use-package reftex
  :commands turn-on-reftex
  :init
  (add-hook 'LaTeX-mode-hook 'turn-on-reftex)   ; with AUCTeX LaTeX mode
  (add-hook 'pandoc-mode-hook 'turn-on-reftex)  ; with Pandoc mode
  (autoload 'reftex-mode     "reftex" "RefTeX Minor Mode" t)
  (autoload 'turn-on-reftex  "reftex" "RefTeX Minor Mode" t)
  (autoload 'reftex-citation "reftex-cite" "Make citation" t)
  (autoload 'reftex-index-phrase-mode "reftex-index" "Phrase mode" t)
  (add-hook 'LaTeX-mode-hook 'turn-on-reftex)   ; with AUCTeX LaTeX mode
  (add-hook 'latex-mode-hook 'turn-on-reftex)   ; with Emacs latex mode

  :config
  ;;;; RefTeX formats for biblatex (not natbib), and for pandoc
  (setq reftex-cite-format
        '(
          (?\C-m . "\\cite[]{%l}")
          (?t . "\\textcite{%l}")
          (?a . "\\autocite[]{%l}")
          (?p . "\\parencite{%l}")
          (?f . "\\footcite[][]{%l}")
          (?F . "\\fullcite[]{%l}")
          (?P . "[@%l]")
          (?T . "@%l [p. ]")
          (?x . "[]{%l}")
          (?X . "{%l}")
          ))

  (setq font-latex-match-reference-keywords
        '(("cite" "[{")
          ("cites" "[{}]")
          ("footcite" "[{")
          ("footcites" "[{")
          ("parencite" "[{")
          ("textcite" "[{")
          ("fullcite" "[{")
          ("citetitle" "[{")
          ("citetitles" "[{")
          ("headlessfullcite" "[{")))

  (setq reftex-enable-partial-scans t)
  (setq reftex-save-parse-info t)
  (setq reftex-use-multiple-selection-buffers t)
  (setq reftex-plug-into-AUCTeX t)

  (setq reftex-cite-prompt-optional-args t)
  (setq reftex-cite-cleanup-optional-args t)

  (setq reftex-default-bibliography
        '("~/documents/bibs/socbib.bib"))

  ;; Think here you need the path to a directory
  (setq reftex-bibpath-environment-variables
        '("~/documents/bibs")))

Color Theme Tweaks

The theme package is loaded in starter-kit-misc.org. The settings here adjust the height of some headings in Org and LaTeX mode, as well as turning off the sans-serif heading style that AucTeX defaults to.

(add-hook 'latex-mode-hook
          (lambda ()
            (set-face-attribute 'font-latex-sectioning-5-face nil :inherit nil :foreground "#b58900")
            (set-face-attribute 'font-latex-sectioning-0-face nil :height 3)
            (set-face-attribute 'font-latex-sectioning-1-face nil :height 2)
            (set-face-attribute 'font-latex-sectioning-2-face nil :height 1.5)
            (set-face-attribute 'font-latex-sectioning-3-face nil :height 1.2)
            (set-face-attribute 'font-latex-sectioning-4-face nil :height 1.0)))

Local LaTeX settings

In-text Smart Quotes (XeLaTeX only)

Redefine TeX-open-quote and TeX-close-quote to be smart quotes by default. Below, in the local org-mode settings, we’ll also map the relevant function to a key we can use in org-mode, too. If you don’t use XeLaTeX for processing all your .tex files, you should disable this option.

(setq TeX-open-quote "")
(setq TeX-close-quote "")

SyncTeX, PDF mode, Evince

Set up AUCTeX to work with the Evince PDF viewer.

10 Jan 21: TeX-output-view-style produces error:

Debugger entered--Lisp error: (void-variable TeX-output-view-style)
  (member '("^pdf$" "." "/usr/bin/evince  %n %o %b") TeX-output-view-style)
  (if (member '("^pdf$" "." "/usr/bin/evince  %n %o %b") TeX-output-view-style) TeX-output-view-style (setq TeX-output-view-style (cons '("^pdf$" "." "/usr/bin/evince  %n %o %b") TeX-output-view-style)))
  (lambda nil (if (member '("^pdf$" "." "/usr/bin/evince  %n %o %b") TeX-output-view-style) TeX-output-view-style (setq TeX-output-view-style (cons '("^pdf$" "." "/usr/bin/evince  %n %o %b") TeX-output-view-style))))()
  run-hooks(change-major-mode-after-body-hook text-mode-hook TeX-mode-hook LaTeX-mode-hook)

So I temporarily disable this:

    ;; Synctex with Evince
    (add-hook 'TeX-mode-hook
    (lambda ()
    (add-to-list 'TeX-output-view-style
    '("^pdf$" "."
     "/usr/bin/evince  %n %o %b")))
     )

  (setq TeX-view-program-selection '((output-pdf "PDF Viewer")))
  (setq TeX-view-program-list
       '(("PDF Viewer" "/usr/bin/evince -b -g %n %o %b")))

  ;; Make emacs aware of multi-file projects
  ;; (setq-default TeX-master nil)

    ;; Auto-raise Emacs on activation (from Skim, usually)
;;    (defun raise-emacs-on-aqua()
;;    (shell-command "osascript -e 'tell application \"Emacs\" to activate' &"))
;;    (add-hook 'server-switch-hook 'raise-emacs-on-aqua)

Company-AucTeX

Following advice from alexeyr/company-auctex#27

(use-package company-auctex
  :after (auctex company)
  :config (company-auctex-init))

Bibtex

From https://tex.stackexchange.com/questions/454966/biblatex-custom-entries-with-emacs-bibtex-mode

Read https://tex.stackexchange.com/questions/454966/biblatex-custom-entries-with-emacs-bibtex-mode to customize bibtex

Use Biblatex key template by default in bib files

(use-package bibtex
  :after (tex org)
  :bind ("s-\]" . crossref-add-bibtex-entry) ;; download resource found
  :custom
  (bibtex-dialect (quote biblatex))
  :config
  (setq bibtex-completion-format-citation-functions
        '((org-mode      . bibtex-completion-format-citation-org-link-to-PDF)
          (latex-mode    . bibtex-completion-format-citation-cite)
          (markdown-mode . bibtex-completion-format-citation-pandoc-citeproc)
          (default       . bibtex-completion-format-citation-default)))

  (setq bibtex-maintain-sorted-entries t)

  (setq bibtex-autokey-titleword-separator "_")
  (setq bibtex-autokey-year-title-separator ":_")

  (setq bibtex-entry-format
        `(page-dashes required-fields
                      numerical-fields whitespace last-comma delimiters
                      unify-case sort-fields))

  (setq bibtex-field-delimiters 'double-quotes)
  (setq bibtex-entry-delimiters 'braces))
                                        ; (setq bibtex-dialect "BibTeX")
                                        ; (setq bibtex-maintain-sorted-entries t)
                                        ;  (setq bibtex-autokey-titleword-separator "_")
                                        ;  (setq bibtex-autokey-year-title-separator ":_")

;; Keep all entries in sorted order
                                        ; (setq bibtex-maintain-sorted-entries t)

;; Customize BibTeX bibtex-clean-entry as in https://github.com/higham/dot-emacs/blob/master/.emacs
;; (setq bibtex-entry-format
;;      `(page-dashes required-fields
;;         numerical-fields whitespace last-comma delimiters
;;         unify-case sort-fields))

;; (setq bibtex-field-delimiters 'double-quotes)
;; (setq bibtex-entry-delimiters 'braces)

Ox files

Moved to <starter-kit-org.org>

;; export citations (load after org-ref)
;; (require 'ox-bibtex)

;; Required by tufted org mode (https://github.com/tsdye/tufte-org-mode) 
(require 'ox-extra)
(ox-extras-activate '(latex-header-blocks ignore-headlines))
(require 'ox-tufte-latex)

(require 'ox-latex)
(require 'ox-beamer)

Stuff originally in <starter-kit-latex-org.org>

    (eval-after-load "tex"
      '(add-to-list 'TeX-command-list '("latexmk" "latexmk -bibtex -synctex=1 -shell-escape -pdf %s" TeX-run-TeX nil t :help "Process file with latexmk")))

    (eval-after-load "tex"
      '(add-to-list 'TeX-command-list '("xelatexmk" "latexmk -synctex=1 -shell-escape -xelatex %s" TeX-run-TeX nil t :help "Process file with xelatexmk")))

  (add-hook 'TeX-mode-hook '(lambda () (setq TeX-command-default "latexmk")))  

(defun my-auto-tex-cmd (backend)
  "When exporting from .org with latex,
  automatically run latex, pdflatex, or xelatex as appropriate,
  using latexmk."
  (let ((texcmd))
    (setq texcmd "latexmk -bibtex -f -pdf %f")
    (if (string-match "LATEX_CMD: pdflatex" (buffer-string))
        (progn
          (setq texcmd "latexmk -pdf -pdflatex='pdflatex -file-line-error --shell-escape -synctex=1 -bibtex -f' %f")))
    (if (string-match "LATEX_CMD: xelatex" (buffer-string))
        (progn
          (setq texcmd "latexmk -pdflatex='xelatex -file-line-error --shell-escape -synctex=1' -pdf %f")))  ;;  -bibtex -f between -synctex=1 and ' taken out as they were not recognised according to the log file. DGM 11 Jan 21 added <bibtex> and got <xelatex: unrecognized option '-bibtex'> in the log file

    (setq org-latex-pdf-process (list texcmd))))
(add-hook 'org-export-before-parsing-hook 'my-auto-tex-cmd)

Provide

(provide 'starter-kit-latex)

Final message

(message "Starter Kit LaTeX loaded.")