Initial Commit
[packages] / xemacs-packages / auctex / style / amsmath.el
1 ;;; amsmath.el --- Style hook for the AMS-LaTeX amsmath package.
2
3 ;; Copyright (C) 2002, 2005-2007, 2012-2014  Free Software Foundation, Inc.
4 ;; FIXME: What about the copyright for <= 2001?
5
6 ;; Author: Carsten Dominik <dominik@strw.leidenuniv.nl>
7 ;; Maintainer: auctex-devel@gnu.org
8
9 ;; This file is part of AUCTeX.
10
11 ;; AUCTeX is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; AUCTeX is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with AUCTeX; see the file COPYING.  If not, write to the Free
23 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 ;; 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; This will also load the amstext, amsbsy and amsopn style files.
29
30 ;;; Code:
31
32 (TeX-add-style-hook "amsmath"
33  (function
34   (lambda ()
35
36     (LaTeX-add-environments
37      '("align"      LaTeX-env-label)
38      '("gather"     LaTeX-env-label)
39      '("flalign"    LaTeX-env-label)
40      '("multline"   LaTeX-env-label)
41      '("alignat"    LaTeX-amsmath-env-alignat)
42      '("alignat*"   LaTeX-amsmath-env-alignat)
43      '("xalignat"   LaTeX-amsmath-env-alignat)
44      '("xalignat*"  LaTeX-amsmath-env-alignat)
45      '("xxalignat"  LaTeX-amsmath-env-alignat)
46      '("aligned"    ["Vertical position (t or b)"])
47      '("gathered"   ["Vertical position (t or b)"])
48      '("alignedat"  LaTeX-amsmath-env-alignedat)
49      "align*" "gather*" "flalign*" "multline*" "equation*"
50      "split"
51      "cases"
52      "matrix" "smallmatrix" "pmatrix" "bmatrix" "Bmatrix" "vmatrix" "Vmatrix"
53      "subequations"
54      '("subarray" "Alignment"))
55
56     (TeX-add-symbols
57      '("eqref" TeX-arg-ref)
58      '("numberwithin" TeX-arg-counter "Section level")
59      '("raisetag" "Dimension")
60      '("shoveright" t) '("shoveleft" t)
61      '("intertext" t)
62      '("hdotsfor" ["Stretch"] "Number of columns to cover")
63      '("xleftarrow" ["Below"] "Above")
64      '("xrightarrow" ["Below"] "Above")
65      '("overset" "Accent symbol" "Symbol")
66      '("underset" "Accent symbol" "Symbol")
67      '("dfrac" 2)
68      '("tfrac" 2)
69      '("binom" 2)
70      '("dbinom" 2)
71      '("tbinom" 2)
72      '("genfrac" "Left delimiter" "Right delimiter" "Thickness"
73        "Mathstyle" 2)
74      '("cfrac" ["position (l or r)"] t)
75      '("smash" ["where (t or b)"] t)
76      '("sideset" "Left" "Right")
77      '("tag" "(Tag)")
78      '("tag*" "Tag")
79      '("displaybreak" ["Weight (0..4)"])
80      '("allowdisplaybreaks" ["Weight (1..4)"])
81      '("substack" t)
82      '("leftroot" "Push root index left by")
83      '("uproot" "Push root index upward by")
84      '("boxed" t)
85      '("mspace" t)
86      '("mod" t)
87      '("pmod" t)
88      '("pod" t)
89      '("overleftrightarrow" t)
90      '("underleftarrow" t)
91      '("underrightarrow" t)
92      '("underleftrightarrow" t)
93      '("dddot" t)
94      '("ddddot" t)
95      "bmod" "notag"
96      "dots" "dotsb" "dotsc" "dotsi" "dotsm" "dotso" "nobreakdash" 
97      '("lvert" TeX-arg-insert-right-brace-maybe)
98      '("lVert" TeX-arg-insert-right-brace-maybe)
99      "rvert" "rVert"
100      "iint" "iiint" "iiiint" "idotsint"
101      )
102     
103     (setq  LaTeX-item-list 
104            (append '(("split"    . LaTeX-item-equation)
105                      ("multline" . LaTeX-item-equation)
106                      ("multline*" . LaTeX-item-equation)
107                      ("gather"   . LaTeX-item-equation)
108                      ("gather*"  . LaTeX-item-equation)
109                      ("gathered" . LaTeX-item-equation)
110                      ("align"    . LaTeX-item-equation)
111                      ("align*"   . LaTeX-item-equation)
112                      ("aligned"  . LaTeX-item-equation)
113                      ("alignat"  . LaTeX-item-equation-alignat)
114                      ("alignat*" . LaTeX-item-equation-alignat)
115                      ("xalignat"  . LaTeX-item-equation-alignat)
116                      ("xalignat*" . LaTeX-item-equation-alignat)
117                      ("xxalignat" . LaTeX-item-equation-alignat)
118                      ("alignedat" . LaTeX-item-equation-alignat)
119                      ("flalign"  . LaTeX-item-equation)
120                      ("flalign*" . LaTeX-item-equation)
121                      ("matrix" .  LaTeX-item-equation)
122                      ("pmatrix" .  LaTeX-item-equation)
123                      ("bmatrix" .  LaTeX-item-equation)
124                      ("Bmatrix" .  LaTeX-item-equation)
125                      ("vmatrix" .  LaTeX-item-equation)
126                      ("Vmatrix" .  LaTeX-item-equation)
127                      ("subarray" . LaTeX-item-equation)
128                      ("cases"    . LaTeX-item-equation))
129                    LaTeX-item-list))
130
131     ;; When `LaTeX-amsmath-label' is nil, use value of LaTeX-equation-label:
132     (unless LaTeX-amsmath-label
133       (setq LaTeX-amsmath-label LaTeX-equation-label))
134
135     (setq LaTeX-label-alist
136           ;; Append amsmath environments to `LaTeX-label-alist', in order not to
137           ;; override possible custome values.
138           (append LaTeX-label-alist
139                   '(("align"      . LaTeX-amsmath-label)
140                     ("alignat"    . LaTeX-amsmath-label)
141                     ("xalignat"   . LaTeX-amsmath-label)
142                     ("multline"   . LaTeX-amsmath-label)
143                     ("flalign"    . LaTeX-amsmath-label)
144                     ("gather"     . LaTeX-amsmath-label))))
145
146     (set (make-local-variable 'TeX-braces-association)
147          (append '(("\\lvert" . "\\rvert")
148                    ("\\lVert" . "\\rVert"))
149                  TeX-braces-association))
150     (set (make-local-variable 'TeX-left-right-braces)
151          (append '(("\\lvert") ("\\rvert") ("\\lVert") ("\\rVert"))
152                  TeX-left-right-braces))
153
154     ;; amsmath includes amstext, amsbsy, & amsopn.
155     ;; So we run their hooks, too.
156     (TeX-run-style-hooks "amstext" "amsbsy" "amsopn")
157
158     ;; If RefTeX is loaded, make it recognize the amsmath environments.
159     (when (fboundp 'reftex-add-to-label-alist)
160       (reftex-add-to-label-alist '(AMSTeX)))))
161  LaTeX-dialect)
162
163 (defun LaTeX-amsmath-env-alignat (env)
164   "Insert ENV with column number specifications.
165 Insert suitable number of ampersands also if possible."
166   (let ((ncols (read-string "Number of columns: ")))
167     (LaTeX-insert-environment env (concat TeX-grop ncols TeX-grcl))
168     (LaTeX-item-equation-alignat t)))
169
170 (defun LaTeX-amsmath-env-alignedat (env)
171   "Insert ENV with position and column number specifications.
172 Insert suitable number of ampersands also if possible."
173   (let ((where (read-string "(Optional) Vertical position (t or b): "))
174         (ncols (read-string "Number of columns: ")))
175     (unless (string= where "")
176       (setq where (concat LaTeX-optop where LaTeX-optcl)))
177     (LaTeX-insert-environment env (concat where TeX-grop ncols TeX-grcl))
178     (LaTeX-item-equation-alignat t)))
179
180 (defun LaTeX-item-equation (&optional suppress)
181   "Insert contents to terminate a line in multi-line equations environment.
182 Put line break macro on the last line.  If the current environment
183 wants \\label, insert it also.
184
185 If SUPPRESS is non-nil, do not insert line break macro."
186   (unless suppress
187     (end-of-line 0)
188     (just-one-space)
189     (TeX-insert-macro "\\")
190     (forward-line 1)
191     (indent-according-to-mode))
192   (let ((env (LaTeX-current-environment)))
193     (when (and (assoc env LaTeX-label-alist)
194                (LaTeX-label env 'environment))
195       (LaTeX-newline)
196       (indent-according-to-mode))))
197
198 (defun LaTeX-item-equation-alignat (&optional suppress)
199   "Insert contents to terminate a line in multi-line equations environment.
200 Put line break macro on the last line.  Next, if the current
201 environment wants \\label, insert it also.  And insert suitable number
202 of ampersands if possible.
203
204 If SUPPRESS is non-nil, do not insert line break macro."
205   (LaTeX-item-equation suppress)
206   (LaTeX-insert-ampersands
207    (concat "\\(?:"
208            (regexp-quote LaTeX-optop) "[tb]" (regexp-quote LaTeX-optcl)
209            "\\)?")
210    'LaTeX-amsmath-alignat-number-of-ampersands))
211
212 (defun LaTeX-amsmath-alignat-number-of-ampersands (start end)
213   "Return the number of ampersands to insert.
214 The number is 2N-1 where N is the number taken from the text between
215 START and END."
216   (let ((num (string-to-number (buffer-substring-no-properties start end))))
217     (if (integerp num) (+ num num -1))))
218
219 (defvar LaTeX-amsmath-package-options '("intlimits" "nointlimits"
220                                         "sumlimits" "nosumlimits"
221                                         "namelimits" "nonamelimits"
222                                         "leqno" "reqno" "centertags"
223                                         "tbtags" "cmex10" "fleqn" "?")
224     "Package options for the amsmath package.")
225
226 ;;; amsmath.el ends here.