summaryrefslogtreecommitdiff
path: root/elpa/auctex-13.1.3/style/algorithm.el
blob: 8500f44e2b731bc28adc3e2dff6987e729f28551 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
;;; algorithm.el --- AUCTeX style for the (LaTeX) algorithm package  -*- lexical-binding: t; -*-

;; Copyright (C) 2020 Free Software Foundation, Inc.

;; Author: Uwe Brauer <oub@mat.ucm.es>
;; Created: 2020-01-26
;; Keywords: tex

;; This file is part of AUCTeX.

;; AUCTeX is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.

;; AUCTeX is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with AUCTeX; see the file COPYING.  If not, write to the Free
;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
;; 02110-1301, USA.

;;; Commentary:
;; This file adds support for the algorithm package.

;;; Code:

(require 'tex)
(require 'latex)

(defvar LaTeX-algorithm-package-options
  '(;; Appearance options:
    "plain" "ruled" "boxed"
    ;; Numbering convention:
    "part" "chapter" "section" "subsection" "subsubsection" "nothing")
  "Package options for the algorithm package.")

(defun LaTeX-env-algorithm (environment)
  "Insert a algorithm-like ENVIRONMENT with caption and label."
  (let* ((pos (completing-read (TeX-argument-prompt t nil "Position")
                               '(("H"))))
         (caption (TeX-read-string "Caption: "))
         (short-caption (when (>= (length caption) LaTeX-short-caption-prompt-length)
                          (TeX-read-string "(Optional) Short caption: "))))
    (LaTeX-insert-environment environment
                              (concat
                               (unless (zerop (length pos))
                                 (concat LaTeX-optop pos LaTeX-optcl))))
    ;; top caption -- do nothing if user skips caption
    (unless (zerop (length caption))
      ;; insert `\caption[short-caption]{caption':
      (insert TeX-esc "caption")
      (when (and short-caption (not (string= short-caption "")))
        (insert LaTeX-optop short-caption LaTeX-optcl))
      (insert TeX-grop caption)
      ;; Add } to close `\caption{'
      (insert TeX-grcl)
      ;; fill the caption
      (when auto-fill-function (LaTeX-fill-paragraph))
      ;; Insert a new line and indent
      (LaTeX-newline)
      (indent-according-to-mode)
      ;; Finally add a \label:
      (when (LaTeX-label environment 'environment)
        (LaTeX-newline)
        (indent-according-to-mode)))))

(TeX-add-style-hook
 "algorithm"
 (lambda ()
   (LaTeX-add-environments
    '("algorithm" LaTeX-env-algorithm))
   (TeX-add-symbols
    '("listofalgorithms" 0)))
 TeX-dialect)

;;; algorithm.el ends here