>From c9084f01e4ff1695d413392609f10bc8d10de8e7 Mon Sep 17 00:00:00 2001 From: Arash Esbati Date: Sat, 24 Oct 2015 16:31:28 +0200 Subject: [PATCH 3/4] Improve style. * style/amsthm.el (LaTeX-amsthm-theoremstyle-list): New variable replacing the deleted function `LaTeX-amsthm-complete-theoremstyle'. (LaTeX-amsthm-env-label): New function to insert user defined environments. ("amsthm"): Improve handling of "newtheorem*" and "newtheoremstyle". --- ChangeLog | 8 ++++ style/amsthm.el | 144 ++++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 126 insertions(+), 26 deletions(-) diff --git a/ChangeLog b/ChangeLog index df6b60e..4178918 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2015-10-24 Arash Esbati + * style/amsthm.el (LaTeX-amsthm-theoremstyle-list): New variable + replacing the deleted function + `LaTeX-amsthm-complete-theoremstyle'. + (LaTeX-amsthm-env-label): New function to insert user defined + environments. + ("amsthm"): Improve handling of "newtheorem*" and + "newtheoremstyle". + * style/AlegreyaSans.el ("AlegreyaSans"): Remove SmallCaps font declaration command. diff --git a/style/amsthm.el b/style/amsthm.el index 57f2315..8bc33d4 100644 --- a/style/amsthm.el +++ b/style/amsthm.el @@ -1,6 +1,6 @@ ;;; amsthm.el --- Style hook for the AMS-LaTeX amsthm package. -;; Copyright (C) 1997, 2013, 2014 Free Software Foundation, Inc. +;; Copyright (C) 1997, 2013--2015 Free Software Foundation, Inc. ;; Author: Carsten Dominik ;; Maintainer: address@hidden @@ -22,51 +22,143 @@ ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA ;; 02110-1301, USA. +;;; Commentary: + +;; The style provides the function `LaTeX-amsthm-env-label' which +;; enables new defined environments with "\newtheoreom" to interact +;; with AUCTeX and RefTeX mechanisms for inserting labels. Check +;; docstring of `LaTeX-amsthm-env-label' for instructions. + ;;; Code: (defvar LaTeX-amsthm-package-options nil "Package options for the amsthm package.") +(defvar LaTeX-amsthm-theoremstyle-list + '(("plain") ("definition") ("remark")) + "List of theorem styles provided by `amsthm.el' and new ones +defined with \"\\newtheoremstyle\".") + +(defvar LaTeX-amsthm-fontdecl + (mapcar (lambda (elt) (concat TeX-esc elt)) + '("itshape" "bfseries" "scshape" + "ttfamily" "upshape" "mdseries" + "rmfamily" "sffamily" "slshape")) + "List of font declaration commands for \"\\newtheoremstyle\".") + +(defun LaTeX-amsthm-env-label (environment) + "Insert ENVIRONMENT, query for an optional argument and prompt +for label. AUCTeX users should add ENVIRONMENT to +`LaTeX-label-alist' via customize or in init-file with: + + (add-to-list 'LaTeX-label-alist '(\"lemma\" . \"lem:\")) + +RefTeX users should customize or add ENVIRONMENT to +`LaTeX-label-alist' and `reftex-label-alist', e.g. + + (add-to-list 'LaTeX-label-alist '(\"lemma\" . \"lem:\")) + (add-to-list 'reftex-label-alist + '(\"lemma\" ?m \"lem:\" \"~\\ref{%s}\" + nil (\"Lemma\" \"lemma\") nil))" + (let ((opthead (TeX-read-string + (TeX-argument-prompt t nil "Heading")))) + (LaTeX-insert-environment environment + (when (and opthead + (not (string= opthead ""))) + (format "[%s]" opthead)))) + (when (LaTeX-label environment 'environment) + (LaTeX-newline) + (indent-according-to-mode))) + +;; Needed for auto-parsing +(require 'tex) + +;; Setup parsing for \newtheorem +(TeX-auto-add-type "amsthm-newtheorem" "LaTeX") + +;; Setup parsing for \newtheoremstyle +(TeX-auto-add-type "amsthm-newtheoremstyle" "LaTeX") + +(defun LaTeX-amsthm-auto-prepare () + "Clear `LaTeX-auto-amsthm-newtheorem' and +`LaTeX-auto-amsthm-newtheoremstyle' before parsing." + (setq LaTeX-auto-amsthm-newtheorem nil) + (setq LaTeX-auto-amsthm-newtheoremstyle nil)) + +(defun LaTeX-amsthm-auto-cleanup () + "Move parsed results from `LaTeX-auto-amsthm-newtheorem' and +make them available as new environments. Update +`LaTeX-amsthm-theoremstyle-list' with styles defined with +\"\\newtheoremstyle\"." + (dolist (newthm (mapcar 'car (LaTeX-amsthm-newtheorem-list))) + (LaTeX-add-environments (list newthm 'LaTeX-amsthm-env-label))) + (dolist (newthmstyle (LaTeX-amsthm-newtheoremstyle-list)) + (add-to-list (make-local-variable 'LaTeX-amsthm-theoremstyle-list) + newthmstyle))) + +(add-hook 'TeX-auto-prepare-hook #'LaTeX-amsthm-auto-prepare t) +(add-hook 'TeX-auto-prepare-hook #'LaTeX-amsthm-auto-cleanup t) +(add-hook 'TeX-update-style-hook #'TeX-auto-parse t) + (TeX-add-style-hook "amsthm" (lambda () (LaTeX-add-environments - '("proof" (lambda (env &rest ignore) - (LaTeX-insert-environment - env - (let ((heading (TeX-read-string "(optional) Heading: "))) - (if (string= heading "") - "" - (format "[%s]" heading)))))) - ) + '("proof" LaTeX-amsthm-env-label)) (TeX-add-symbols - '("newtheorem*" TeX-arg-define-environment "Heading") - '("theoremstyle" LaTeX-amsthm-complete-theoremstyle) + '("newtheorem*" + (TeX-arg-eval + (lambda () + (let ((nthm (TeX-read-string + (TeX-argument-prompt nil nil "Environment"))) + (heading (TeX-read-string + (TeX-argument-prompt nil nil "Heading")))) + (LaTeX-add-amsthm-newtheorems nthm) + (LaTeX-add-environments (list nthm 'LaTeX-amsthm-env-label)) + (insert (concat TeX-grop nthm TeX-grcl)) + (format "%s" heading))))) + + '("theoremstyle" + (TeX-arg-eval completing-read + "Style: " + LaTeX-amsthm-theoremstyle-list)) "qedhere" "swapnumbers" - '("newtheoremstyle" "Style name" (TeX-arg-length nil "Space above") - (TeX-arg-length nil "Space below") "Body font" "Indent amount" - "Theorem head font" "Punctuation after head" - (TeX-arg-length nil "Space after head") "Theorem head spec")) + + '("newtheoremstyle" + (TeX-arg-eval + (lambda () + (let ((nthmstyle (TeX-read-string + (TeX-argument-prompt nil nil "Style name")))) + (LaTeX-add-amsthm-newtheoremstyles nthmstyle) + (add-to-list (make-local-variable 'LaTeX-amsthm-theoremstyle-list) + (list nthmstyle)) + (format "%s" nthmstyle)))) + (TeX-arg-length "Space above") + (TeX-arg-length "Space below") + (TeX-arg-eval completing-read + "Body font: " LaTeX-amsthm-fontdecl) + "Indent amount" + (TeX-arg-eval completing-read + "Theorem head font: " LaTeX-amsthm-fontdecl) + "Punctuation after head" + (TeX-arg-length "Space after head") + "Theorem head spec")) (TeX-auto-add-regexp - `(,(concat "\\\\newtheorem\\*{\\(" TeX-token-char "+\\)}") - 1 LaTeX-auto-environment)) + `(,(concat "\\\\newtheorem\\*?{\\(" TeX-token-char "+\\)}") + 1 LaTeX-auto-amsthm-newtheorem)) + (TeX-auto-add-regexp + `(,(concat "\\\\newtheoremstyle{\\(" TeX-token-char "+\\)}") + 1 LaTeX-auto-amsthm-newtheoremstyle)) ;; Fontification (when (and (featurep 'font-latex) (eq TeX-install-font-lock 'font-latex-setup)) - (font-latex-add-keywords '(("newtheorem" "*{[{[") - ("theoremstyle" "{") + (font-latex-add-keywords '(("newtheorem" "*{[{[") + ("theoremstyle" "{") ("newtheoremstyle" "{{{{{{{{{")) 'function))) LaTeX-dialect) -(defun LaTeX-amsthm-complete-theoremstyle (&rest ignore) - (insert TeX-grop - (completing-read "Style: " '(("plain" . nil) - ("definition" . nil) - ("remark" . nil))) - TeX-grcl)) - ;;; amsthm.el ends here -- 2.6.2