summaryrefslogtreecommitdiff
path: root/elpa/auctex-13.1.3/style/subcaption.el
blob: cd3312d3eb2d8c8e2af60f28452358917f3276ec (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
;;; subcaption.el --- AUCTeX style for `subcaption.sty' (v1.3)  -*- lexical-binding: t; -*-

;; Copyright (C) 2015--2022 Free Software Foundation, Inc.

;; Author: Arash Esbati <arash@gnu.org>
;; Maintainer: auctex-devel@gnu.org
;; Created: 2015-09-19
;; 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 `subcaption.sty' (v1.3) from 2019-08-31.
;; `subcaption.sty' is part of TeXLive.

;;; Code:

(require 'tex)
(require 'latex)

;; Silence the compiler:
(declare-function font-latex-add-keywords
                  "font-latex"
                  (keywords class))
(defvar LaTeX-caption-key-val-options)

(defvar LaTeX-subcaption-key-val-options
  '(("subrefformat" ("default" "empty" "simple" "brace" "parens")))
  "Key=value options for subcaption package.
This key takes the same values as \"labelformat\" from caption
package.")

(defun LaTeX-arg-subcaption-subcaption (optional &optional star)
  "Query for the arguments of \"\\subcaption\" incl. a label and insert them.
If OPTIONAL is non-nil, indicate it while reading the caption.
If STAR is non-nil, then do not query for a \\label and a short
caption, insert only a caption."
  (let* (;; \subcaption needs an environment, "minipage" will be
         ;; popular.  If so, check next higher environment to find out
         ;; where we are
         (currenv (if (string= (LaTeX-current-environment) "minipage")
                      (LaTeX-current-environment 2)
                    (LaTeX-current-environment)))
         (caption (TeX-read-string
                   (TeX-argument-prompt optional nil "Sub-caption")))
         (short-caption
          (when (and (not star)
                     (>= (length caption) LaTeX-short-caption-prompt-length))
            (TeX-read-string
             (TeX-argument-prompt t nil "Short caption")))))
    (indent-according-to-mode)
    (when (and short-caption (not (string= short-caption "")))
      (insert LaTeX-optop short-caption LaTeX-optcl))
    (insert TeX-grop caption TeX-grcl)
    ;; Fill the \subcaption paragraph before inserting the \label:
    (when auto-fill-function (LaTeX-fill-paragraph))
    (when (and (not star)
               (save-excursion (LaTeX-label currenv 'environment)))
      ;; Move \label into next line if we have one:
      (LaTeX-newline)
      (indent-according-to-mode)
      (end-of-line))))

(defun LaTeX-arg-subcaption-subcaptionbox (optional &optional star)
  "Query for the arguments of \"\\subcaptionbox\" incl. a label and insert them.
If OPTIONAL is non-nil, indicate it while reading the caption.
If STAR is non-nil, then do not query for a \\label and a short
caption, insert only a caption."
  (let* ((currenv (LaTeX-current-environment))
         (caption (TeX-read-string
                   (TeX-argument-prompt optional nil "Sub-caption")))
         (short-caption
          (when (and (not star)
                     (>= (length caption) LaTeX-short-caption-prompt-length))
            (TeX-read-string
             (TeX-argument-prompt t nil "Short Sub-caption")))))
    (indent-according-to-mode)
    (when (and short-caption (not (string= short-caption "")))
      (insert LaTeX-optop short-caption LaTeX-optcl))
    (insert TeX-grop caption)
    (unless star (LaTeX-label currenv 'environment))
    (insert TeX-grcl))
  (let* ((TeX-arg-opening-brace "[")
         (TeX-arg-closing-brace "]")
         (width (completing-read (TeX-argument-prompt t nil "Width")
                                 (mapcar (lambda (elt) (concat TeX-esc (car elt)))
                                         (LaTeX-length-list))))
         (inpos (if (and width (not (string-equal width "")))
                    (completing-read (TeX-argument-prompt t nil "Inner position")
                                     '("c" "l" "r" "s"))
                  "")))
    (TeX-argument-insert width t)
    (TeX-argument-insert inpos t))
  ;; Fill the paragraph before inserting {}.  We use this function
  ;; since we add \subcaption to `paragraph-start' in the style hook
  ;; below.
  (when auto-fill-function (LaTeX-fill-paragraph)))

(TeX-add-style-hook
 "subcaption"
 (lambda ()
   ;; Run style hook for caption.el
   (TeX-run-style-hooks "caption")


   (TeX-add-symbols
    ;; Basic commands
    '("subcaption"     (LaTeX-arg-subcaption-subcaption))
    '("subcaption*"    (LaTeX-arg-subcaption-subcaption     t)  )
    '("subcaptionbox"  (LaTeX-arg-subcaption-subcaptionbox) t)
    '("subcaptionbox*" (LaTeX-arg-subcaption-subcaptionbox  t) t)
    '("subref"         TeX-arg-ref)
    ;; \subref* is only available with hyperref.sty loaded, we don't
    ;; check if hyperref.el is loaded and make it available directly.
    '("subref*"        TeX-arg-ref)
    '("phantomcaption"    0)
    '("phantomsubcaption" 0)
    '("subfloat" [ "List entry" ] [ "Sub-caption" ] t))

   ;; The next 2 macros are part of the kernel of caption.sty, but we
   ;; load them within subcaption.el.
   (TeX-add-symbols
    '("DeclareCaptionSubType"
      [TeX-arg-eval
       completing-read (TeX-argument-prompt t nil "Numbering scheme")
       '("arabic" "roman" "Roman" "alph" "Alph" "fnsymbol")]
      (TeX-arg-eval
       completing-read
       (TeX-argument-prompt nil nil "Type")
       (append
        (when (and (fboundp 'LaTeX-newfloat-DeclareFloatingEnvironment-list)
                   (LaTeX-newfloat-DeclareFloatingEnvironment-list))
          (mapcar #'car (LaTeX-newfloat-DeclareFloatingEnvironment-list)))
        '("figure" "table"))))

    '("DeclareCaptionSubType*"
      [TeX-arg-eval completing-read
                    (TeX-argument-prompt t nil "Numbering scheme")
                    '("arabic" "roman" "Roman" "alph" "Alph" "fnsymbol")]
      (TeX-arg-eval
       completing-read
       (TeX-argument-prompt nil nil "Type")
       (append
        (when (and (fboundp 'LaTeX-newfloat-DeclareFloatingEnvironment-list)
                   (LaTeX-newfloat-DeclareFloatingEnvironment-list))
          (mapcar #'car (LaTeX-newfloat-DeclareFloatingEnvironment-list)))
        '("figure" "table")))))

   ;; \subcaption(box)? and \subfloat macros should get their own lines
   (LaTeX-paragraph-commands-add-locally
    '("subcaption" "subcaptionbox" "subfloat"))

   ;; The subfigure & subtable environments
   (LaTeX-add-environments
    '("subfigure" LaTeX-env-minipage)
    '("subtable"  LaTeX-env-minipage))

   ;; Append them to `LaTeX-label-alist':
   (add-to-list 'LaTeX-label-alist '("subfigure" . LaTeX-figure-label) t)
   (add-to-list 'LaTeX-label-alist '("subtable" . LaTeX-table-label) t)

   ;; Introduce env's to RefTeX if loaded
   (when (fboundp 'reftex-add-label-environments)
     (reftex-add-label-environments
      `(("subfigure" ?f ,LaTeX-figure-label "~\\ref{%s}" caption)
        ("subtable"  ?t ,LaTeX-table-label  "~\\ref{%s}" caption))))

   ;; Fontification
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("subcaption"            "*[{")
                                ("subcaptionbox"         "*[{[[")
                                ("phantomcaption"        "")
                                ("phantomsubcaption"     "")
                                ("subfloat"              "[["))
                              'textual)
     (font-latex-add-keywords '(("subref"                "*{"))
                              'reference)
     (font-latex-add-keywords '(("DeclareCaptionSubType" "*[{"))
                              'function)) )
 TeX-dialect)

(defun LaTeX-subcaption-package-options ()
  "Prompt for package options for the subcaption package."
  (TeX-load-style "caption")
  (TeX-read-key-val t (append LaTeX-subcaption-key-val-options
                              LaTeX-caption-key-val-options)))

;;; subcaption.el ends here