Contents
With use-package
View a demostration here.
;;; Till Mueller, 2024-05-04. No copyright.
;; AUCTeX has good support for LaTeX out-of-the-box. For ConTeXt, not
;; so much.
;;
;; Fortunately much of the functionality, in particular
;; LaTeX-math-mode can be reused as-is in ConTeXt mode.
;; LaTeX-math-mode is a minor mode with easy access to TeX math
;; macros. Such as ` a for \alpha. You can define custom shortcuts
;; via LaTeX-math-list as well.
;;
;; The key is to tweak texmathp-tex-commands for recognizing math
;; environment in a ConTeXt document, or else LaTeX-math will not
;; work.
(use-package context
:hook
((ConTeXt-mode . turn-on-reftex)
;; personal preference
(ConTeXt-mode . variable-pitch-mode)
;; show \alpha as α and \mathbb{R} as ℝ
(ConTeXt-mode . prettify-symbols-mode)
;; shortcuts for symbols
(ConTeXt-mode . LaTeX-math-mode))
:custom
;; AUCTeX defaults to mkii; change to iv for iv and lmtx
(ConTeXt-Mark-version "IV")
;; Enable electric left right brace
(LaTeX-electric-left-right-brace t)
;; Do not unprettify symbol at point
(prettify-symbols-unprettify-at-point nil)
;; Let AUCTeX properly detect formula environment as math mode
(texmathp-tex-commands '(("\\startformula" sw-on) ("\\stopformula" sw-off)))
;; Set PDF viewer
(TeX-view-program-selection '((output-pdf "Zathura")))
;; Don't as for permission, just save all files
(TeX-save-query nil)
;; Auto-save
(TeX-auto-save t)
;; Debug bad boxes and warnings after compilation via
;; C-c ` key
(TeX-debug-bad-boxes t)
(TeX-debug-warnings t)
;; Electric inline math,
(TeX-electric-math '("$" . "$"))
;; Electric sub and superscript, inserts {} after ^ and _
;; such as a^{}.
(TeX-electric-sub-and-superscript t)
;; RefTex
(reftex-plug-into-AUCTeX t)
;; Customize keyboard shortcuts for TeX math macros
(LaTeX-math-list
'(("o r" "mathbb{R}" nil nil)
("o Q" "qquad" nil nil)
("o q" "quad" nil nil)
("o n" "mathbb{N}" nil nil)
(?= "coloneq" nil nil)
("o c" "mathbb{C}" nil nil)))
:bind
;; Electric \left(\right) \left[\right] \left\{\right\}
;; only left brace; there is no right electric brace function
(:map ConTeXt-mode-map ("(" . LaTeX-insert-left-brace))
(:map ConTeXt-mode-map ("[" . LaTeX-insert-left-brace))
(:map ConTeXt-mode-map ("{" . LaTeX-insert-left-brace))
:config
(add-hook 'TeX-after-compilation-finished-functions
#'TeX-revert-document-buffer)
;; Prettify symbols mode, customizable.
(with-eval-after-load "tex-mode"
(dolist (symb
'(("\\colon" . ?:)
("\\mathbb{C}" . ?ℂ)
("\\mathbb{K}" . ?𝕂)))
(add-to-list 'tex--prettify-symbols-alist symb))))
Help with delimiters and math mode
These customizations are mostly for faster insertion of special characters (and math mode).
;;; Useful AUCTeX setup for ConTeXt (for your .emacs)
;;; Sanjoy Mahajan (sanjoy@mrao.cam.ac.uk), 2006-04-20. No copyright.
;;;
;;; With recent AUCTeX (11.50 or later), editing ConTeXt files should
;;; just work, but I use the following elisp as well.
; the AUCTeX manual recommends these settings
(setq TeX-parse-self t) ; Enable parse on load.
(setq TeX-auto-save t) ; Enable parse on save.
; for outline views (hide/show sections, chapters, etc.)
(add-hook 'TeX-mode-hook '(lambda () (TeX-fold-mode 1)))
(add-hook 'TeX-mode-hook '(lambda () (outline-minor-mode 1)))
; make PDF by default (can toggle with C-c C-t C-p
(add-hook 'TeX-mode-hook '(lambda () (TeX-PDF-mode 1)))
; these math abbrevs (` as prefix char) are also useful in TeX/ConTeXt files
(require 'latex) ; defines LaTeX-math-mode
(add-hook 'TeX-mode-hook 'LaTeX-math-mode)
; Emacs help for \label, \ref, \cite. Normally used only with
; LaTeX-mode but also useful with plain TeX + eplain and with ConTeXt, so:
(setq reftex-plug-into-AUCTeX t)
(add-hook 'TeX-mode-hook 'reftex-mode)
(defun insert-balanced (left right)
"Insert a left, right delmiter pair and be poised to type inside them."
(interactive)
(insert left)
(save-excursion
(insert right)))
; When start-context-math() is bound to $:
; Typing one $ gets you $$ with the insertion point between them.
; Typing a second $ turns the $$ into ConTeXt's form for displayed math:
;
; \placeformula\startformula
; [blank line with insertion point at beginning]
; \stopformula
;
; Delete the \placeformula if you don't want equations numbered automatically.
(defun start-context-math ()
(interactive)
(let* ((start (max (point-min) (- (point) 1)))
(stop (min (point-max) (+ (point) 1))))
; if in the middle of a $$, turn inline math into context display math
(if (equal "$$" (buffer-substring-no-properties start stop))
(progn
(delete-region start stop) ;get rid of the $$
; delete preceding spaces, if any
(while (and (< (point-min) (point))
(equal (buffer-substring-no-properties (- (point) 1)
(point))
" "))
(backward-delete-char 1))
; delete a preceding newline, if any
(if (equal (buffer-substring-no-properties (- (point) 1)
(point))
"\n")
(backward-delete-char 1))
; ConTeXt's display math with automatic equation numbering
(insert "\n\\placeformula\\startformula\n")
(save-excursion (insert "\n\\stopformula")))
; else: just doing inline math
(insert-balanced ?\$ ?\$))))
; automatically insert right delimiter for $, {, [, and ( and be
; poised to type inside them.
(add-hook 'TeX-mode-hook
'(lambda ()
(local-set-key "$"
'(lambda ()
(interactive)
(insert-balanced ?\$ ?\$)))
(local-set-key "{"
'(lambda ()
(interactive)
(insert-balanced ?\{ ?\})))
(local-set-key "["
'(lambda ()
(interactive)
(insert-balanced ?\[ ?\])))
(local-set-key "("
'(lambda ()
(interactive)
(insert-balanced ?\( ?\))))))
; For ConTeXt mode, inserting two $ signs needs to behave specially
(add-hook 'ConTeXt-mode-hook
'(lambda ()
(local-set-key "$" 'start-context-math)))
; The TeX-format-list from AUCTeX's tex.el (v11.82) with a few more
; ConTeXt-specific patterns. I've submitted it to the AUCTeX lists,
; so later versions should have them automatically and you won't need
; this regexp mess in your .emacs
;
(setq TeX-format-list
'(("JLATEX" japanese-latex-mode
"\\\\\\(documentstyle\\|documentclass\\)[^%\n]*{\\(j[s-]?\\|t\\)\\(article\\|report\\|book\\|slides\\)")
("JTEX" japanese-plain-tex-mode
"-- string likely in Japanese TeX --")
("AMSTEX" ams-tex-mode
"\\\\document\\b")
("CONTEXT" context-mode
"\\(\\\\\\(start\\(text\\|project\\|environment\\|product\\|typing\\|component\\|tekst\\)\\)\\|%.*?interface=\\)")
("LATEX" latex-mode
"\\\\\\(begin\\|section\\|chapter\\|documentstyle\\|documentclass\\)\\b")
("TEX" plain-tex-mode ".")))
Help with natural tables
These customizations, by Johan Sandblom, make entering |Natural tables with TABLE easy.
(defun context-insert-nattab (rows columns)
;; Johan Sandblom 2006-01-28
"Insert a TABLE skeleton"
(interactive "nNumber of rows: \nnNumber of columns: \n")
(newline)
(insert "\\bTABLE\n\\setupTABLE\[\]\n")
;; First a TABLE header
(insert "\\bTABLEhead\n\\bTR\\bTH \\eTH\n")
(let ((column 1))
(while (< column (- columns 1))
(insert " \\bTH \\eTH\n")
(setq column (1+ column))))
(insert " \\bTH \\eTH\\eTR\n\\eTABLEhead\n\\bTABLEbody\n")
;; The rows and columns
(let ((row 1))
(while (<= row rows)
(insert "\\bTR\\bTD \\eTD\n")
;; The let expression makes sure that each loop starts at the
;; right place
(let ((column 1))
(while (< column (- columns 1))
(insert " \\bTD \\eTD\n")
(setq column (1+ column)))
(insert " \\bTD \\eTD\\eTR\n")
(setq row (1+ row))))
(insert "\\eTABLEbody\n\\eTABLE\n")))
(defun context-insert-nattab-row (columns)
"Insert a row in a TABLE"
(interactive "nNumber of columns: \n")
(newline)
(insert "\\bTR\\bTD \\eTD\n")
(let ((column 1))
(while (< column (- columns 1))
(insert " \\bTD \\eTD\n")
(setq column (1+ column)))
(insert " \\bTD \\eTD\\eTR\n")))
(defun context-insert-nattab-column (&optional arg)
"Insert a column in a TABLE"
(interactive "P")
(insert "\\bTD \\eTD")
(indent-for-tab-command)
(newline)
(backward-char 5))
(add-hook 'ConTeXt-mode-hook
'(lambda ()
(local-set-key "\C-cnr" 'context-insert-nattab-row)
(local-set-key "\C-cnc" 'context-insert-nattab-column)
(local-set-key "\C-cnn" 'context-insert-nattab)))
Help with FLOW charts (from m-chart.tex)
;; Johan Sandblom. No copyright.
(defun context-FLOW-shift-cells (x y beg end)
"Shifts FLOW cells in region right and down"
(interactive "nRight: \nnDown: \nr")
(save-excursion
(goto-char beg)
(while (search-forward-regexp
"{\\([0-9]+\\),\\([0-9]+\\)}" end t)
(replace-match
(concat "{" (number-to-string
(+ (string-to-number (match-string 1)) x))
"," (number-to-string
(+ (string-to-number (match-string 2)) y)) "}")
nil nil))))
(defun context-FLOW-insert-cells (%optional n)
"Insert a FLOWchart cell"
(interactive "P")
(if (not (bolp)) (newline))
(let ((x 1))
(while (<= x (if n n 1))
(insert "\\startFLOWcell\n")
(insert " \\name {}\n")
(insert " \\location {}\n")
(insert " \\shape {action}\n")
(insert " \\text {}\n")
(insert " \\connection[rl]{}\n")
(insert "\\stopFLOWcell\n")
(setq x (1+ x)))))
(add-hook 'ConTeXt-mode-hook
'(lambda ()
(define-key (current-local-map) "\C-cnF" 'context-FLOW-insert-cells)
(define-key (current-local-map) "\C-cnS" 'context-FLOW-shift-cells)))
Metapost-mode and r-mode (from ESS, http://ess.r-project.org ) in context-mode with mmm-mode
;; Johan Sandblom. No copyright.
(require 'mmm-mode)
(setq mmm-global-mode 'maybe)
(setq mmm-submode-decoration-level 2)
(mmm-add-group 'context-plus
'((context-R
:submode r-mode
:face mmm-comment-submode-face
:front ".*\\\\startR\\w*\\({\\w*}\\|\\[\\w*\\]\\|\\)\\W*"
:back ".*\\\\stopR")
(context-MP
:submode metapost-mode
:face mmm-code-submode-face
:front ".*\\\\start\\w*MP\\w*\\({\\w*}\\|\\[\\w*\\]\\|\\)\\W*"
:back ".*\\\\stop\\w*MP")
))
(add-to-list 'mmm-mode-ext-classes-alist '(context-mode nil context-plus))
- AUCTeX, Emacs TeX/ConTeXt/LaTex mode www.gnu.org