1 ;;; tcolorbox.el --- AUCTeX style for `tcolorbox.sty' (v4.00)
3 ;; Copyright (C) 2015, 2016 Free Software Foundation, Inc.
5 ;; Author: Tassilo Horn <tsdh@gnu.org>
6 ;; Maintainer: auctex-devel@gnu.org
10 ;; This file is part of AUCTeX.
12 ;; AUCTeX is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; AUCTeX is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with AUCTeX; see the file COPYING. If not, write to the Free
24 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
29 ;; This file adds support for `tcolorbox.sty' (v4.00) from 2017/02/16.
31 ;; This style file adds support for core macros and environments and
32 ;; their options provided by `tcolorbox.sty'. Macros and environments
33 ;; provided by libraries should go in their own style files where the
34 ;; file is prefixed with `tcolorboxlib-',
35 ;; e.g. `tcolorboxlib-raster.el'.
37 ;; Libraries should also append their key=val option to variable
38 ;; `LaTeX-tcolorbox-keyval-options-full'. This variable is called
39 ;; with macro `\tcbset'.
43 ;; Needed for compiling `pushnew':
44 (eval-when-compile (require 'cl))
46 ;; Needed for auto-parsing.
49 ;; FIXME: Anything missing?
50 (defvar LaTeX-tcolorbox-keyval-options
60 ("attach title to upper")
64 ("upperbox" ("visible" "invisible"))
69 ("lowerbox" ("visible" "invisible" "ignored"))
71 ("lower separated" ("true" "false"))
73 ;; 4.5 Colors and Fonts
76 ("title filled" ("true" "false"))
82 ("fontupper" ("\\rmfamily" "\\sffamily" "\\ttfamily" "\\mdseries" "\\bfseries"
83 "\\upshape" "\\itshape" "\\slshape" "\\scshape"
84 "\\tiny" "\\scriptsize" "\\footnotesize"
85 "\\small" "\\normalsize" "\\large"
86 "\\Large" "\\LARGE" "\\huge" "\\Huge" "\\normalfont"))
87 ("fontlower" ("\\rmfamily" "\\sffamily" "\\ttfamily" "\\mdseries" "\\bfseries"
88 "\\upshape" "\\itshape" "\\slshape" "\\scshape"
89 "\\tiny" "\\scriptsize" "\\footnotesize"
90 "\\small" "\\normalsize" "\\large"
91 "\\Large" "\\LARGE" "\\huge" "\\Huge" "\\normalfont"))
92 ("fonttitle" ("\\rmfamily" "\\sffamily" "\\ttfamily" "\\mdseries" "\\bfseries"
93 "\\upshape" "\\itshape" "\\slshape" "\\scshape"
94 "\\tiny" "\\scriptsize" "\\footnotesize"
95 "\\small" "\\normalsize" "\\large"
96 "\\Large" "\\LARGE" "\\huge" "\\Huge" "\\normalfont"))
98 ("halign" ("justify" "left" "flush left" "right"
99 "flush right" "center" "flush center"))
100 ("halign lower" ("justify" "left" "flush left" "right"
101 "flush right" "center" "flush center"))
102 ("halign title" ("justify" "left" "flush left" "right"
103 "flush right" "center" "flush center"))
113 ("valign" ("top" "center" "bottom" "scale" "scale*"))
114 ("valign upper" ("top" "center" "bottom" "scale" "scale*"))
115 ("valign lower" ("top" "center" "bottom" "scale" "scale*"))
116 ("valign scale limit")
155 ;; 4.7.5 Size Shortcuts
156 ("size" ("normal" "title" "small" "fbox" "tight" "minimal"))
158 ("toggle left and right" ("none" "forced" "evenpage"))
160 ("sharp corners" ("northwest" "northeast" "southwest" "southeast"
161 "north" "south" "east" "west" "downhill" "uphill" "all"))
162 ("rounded corners" ("northwest" "northeast" "southwest" "southeast"
163 "north" "south" "east" "west" "downhill" "uphill" "all"))
174 ;; 4.10 Height Control
181 ("add to natural height")
182 ("height fill" ("true" "false" "maximum"))
190 ("equal height group")
191 ("minimum for equal height group")
192 ("minimum for current equal height group")
193 ("use height from group")
194 ;; 4.11 Box Content Additions
220 ("overlay unbroken and first")
221 ("overlay middle and last")
222 ("overlay unbroken and last")
223 ("overlay first and middle")
224 ;; 4.13 Floating Objects
225 ("floatplacement" ("htbp" "t" "b" "h" "p"))
226 ("float" ("htbp" "t" "b" "h" "p"))
227 ("float*" ("htbp" "t" "b" "h" "p"))
229 ("every float" ("\\centering" "\\raggedleft" "\\raggedright"))
230 ;; 4.14 Embedding into the Surroundings
237 ("forces nobeforeafter")
239 ("box align" ("bottom" "top" "center" "base" ))
246 ("ignore nobreak" ("true" "false"))
249 ("enlarge top initially by")
250 ("enlarge bottom finally by")
251 ("enlarge top at break by")
252 ("enlarge bottom at break by")
254 ("enlarge bottom by")
263 ("toggle enlargement" ("none" "forced" "evenpage"))
268 ;; FIXME: This one should be added w/ `breakable' lib:
269 ;; ("fill downwards")
279 ("extrude bottom by")
281 ;; 4.16 Layered Boxes and Every Box Settings
283 ("every box on layer 1")
284 ("every box on layer 2")
285 ("every box on layer 3")
286 ("every box on layer 4")
287 ("every box on higher layers")
289 ("capture" ("minipage" "hbox" "fitbox"))
292 ;; 4.18 Text Characteristics
293 ("parbox" ("true" "false"))
294 ("hyphenationfix" ("true" "false"))
297 ;; 4.21 Counters, Labels, and References
302 ;; FIXME: Are these types documented somewhere in cleveref.pdf?
311 ;; 4.22 Even and Odd Pages
312 ("check odd page" ("true" "false"))
314 ("if odd page or oneside")
315 ;; FIXME: These two should be added w/ `breakable' lib:
317 ;; ("if odd page or oneside*")
318 ;; 4.24 Miscellaneous
324 ("sidebyside" ("true" "false"))
325 ("sidebyside align" ("center" "top" "bottom" "center seam"
326 "top seam" "bottom seam"))
332 ;; 8 Recording options
335 "Key=value options for tcolorbox macros and environments.")
337 (defvar LaTeX-tcolorbox-keyval-options-local nil
338 "Buffer-local key=value options for tcolorbox macros and environments.")
339 (make-variable-buffer-local 'LaTeX-tcolorbox-keyval-options-local)
341 (defvar LaTeX-tcolorbox-keyval-options-full nil
342 "Key=value options of tcolorbox core and all loaded libraries.")
343 (make-variable-buffer-local 'LaTeX-tcolorbox-keyval-options-full)
345 (defvar LaTeX-tcolorbox-tcbox-options
346 '(;; 4.20 \tcbox Specials
350 ("tcbox width" ("auto" "auto limited" "forced center"
351 "forced left" "forced right" "minimum center"
352 "minimum left" "minimum right")))
353 "Key=value options only for \\tcbox and \\tcboxmath from tcolorbox.sty.")
355 (defvar LaTeX-tcolorbox-init-options
356 '(;; 5 Initialization Option Keys
357 ;; 5.1 Numbered Boxes
363 ("number within" ("part" "chapter" "section" "subsection"))
364 ("number format" ("\\arabic" "\\roman" "\\Roman" "\\Alph" "\\alph"))
368 ("blend into" ("figures" "tables" "listings"))
369 ("blend before title" ("colon" "dash" "colon hang" "dash hang"))
370 ("blend before title code")
371 ;; 5.2 Lists of tcolorboxes
374 "Initialization key=value options for tcolorbox macros.")
376 (defvar LaTeX-tcolorbox-library-list
392 "List with libraries provided by tcolorbox package.")
394 ;; Setup for \newtcolorbox:
395 (TeX-auto-add-type "tcolorbox-newtcolorbox" "LaTeX" "tcolorbox-newtcolorboxes")
397 (defvar LaTeX-tcolorbox-newtcolorbox-regexp
398 `(,(concat "\\\\\\(re\\)?newtcolorbox"
403 "\\(?:{[^}{]*}[^}{]*\\)*"
408 "{\\([a-zA-Z0-9]+\\)}"
410 "\\(?:\\[\\([0-9]*\\)\\]\\)?"
413 (2 3 4 1) LaTeX-auto-tcolorbox-newtcolorbox)
414 "Matches the arguments of \\newtcolorbox from tcolorbox package.")
416 ;; Setup for \newtcbox:
417 (TeX-auto-add-type "tcolorbox-newtcbox" "LaTeX" "tcolorbox-newtcboxes")
419 (defvar LaTeX-tcolorbox-newtcbox-regexp
420 `(,(concat "\\\\\\(re\\)?newtcbox"
425 "\\(?:{[^}{]*}[^}{]*\\)*"
430 "{\\\\\\([a-zA-Z]+\\)}"
432 "\\(?:\\[\\([0-9]*\\)\\]\\)?"
435 (2 3 4 1) LaTeX-auto-tcolorbox-newtcbox)
436 "Matches the arguments of \\newtcbox from tcolorbox package.")
438 ;; Setup for \tcbuselibrary:
439 (TeX-auto-add-type "tcolorbox-tcbuselibrary" "LaTeX" "tcbuselibraries")
441 (defvar LaTeX-tcolorbox-tcbuselibrary-regexp
442 '("\\\\tcbuselibrary{\\([^}]+\\)}"
443 1 LaTeX-auto-tcolorbox-tcbuselibrary)
444 "Matches the arguments of \\tcbuselibrary from tcolorbox package.")
446 (defun LaTeX-tcolorbox-load-used-libraries ()
447 "Check which tcolorbox libraries are loaded and run respective style hooks.
448 This functions checks the arguments of \\tcbuselibrary and the
449 name of libraries given in the optional argument of \\usepackage
450 call for tcolorbox and runs the style hook for them. The file
451 for style must have the prefix \"tcolorboxlib-\" in the name,
452 e.g. \"tcolorboxlib-raster.el\"."
453 (when (LaTeX-tcolorbox-tcbuselibrary-list)
455 (dolist (x (LaTeX-tcolorbox-tcbuselibrary-list))
456 (push (TeX-replace-regexp-in-string "[ %\n\r\t]" "" (car x)) libs))
457 (setq libs (mapconcat #'identity libs ","))
458 (dolist (x (split-string libs "," t))
459 (TeX-run-style-hooks (concat "tcolorboxlib-" x)))))
460 (when (assoc "tcolorbox" LaTeX-provided-package-options)
461 (let ((opts (cdr (assoc "tcolorbox" LaTeX-provided-package-options))))
463 (when (member x LaTeX-tcolorbox-library-list)
464 (TeX-run-style-hooks (concat "tcolorboxlib-" x)))))))
466 (defun LaTeX-tcolorbox-update-style-key ()
467 "Update some key=values in `LaTeX-tcolorbox-keyval-options-local'."
468 ;; Update the key=values for coloring.
469 (let* ((keys '("colframe"
476 (tmp (copy-alist LaTeX-tcolorbox-keyval-options-local)))
478 (setq tmp (assq-delete-all (car (assoc key tmp)) tmp))
480 (list key (mapcar #'car (LaTeX-xcolor-definecolor-list))) tmp :test #'equal))
481 (setq LaTeX-tcolorbox-keyval-options-local (copy-alist tmp)))
482 (setq LaTeX-tcolorbox-keyval-options-full
483 (copy-alist LaTeX-tcolorbox-keyval-options-local)))
485 (defun LaTeX-tcolorbox-auto-prepare ()
486 "Clear various LaTeX-tcolorbox-* variables before parsing."
487 (setq LaTeX-auto-tcolorbox-newtcolorbox nil
488 LaTeX-auto-tcolorbox-newtcbox nil
489 LaTeX-auto-tcolorbox-tcbuselibrary nil))
491 (defun LaTeX-tcolorbox-auto-cleanup ()
492 "Process parsed results."
493 ;; Process new env's from \newtcolorbox
494 (dolist (newtcbox (apply #'append LaTeX-tcolorbox-newtcolorbox-list))
495 (let ((box (nth 0 newtcbox))
496 (arg (nth 1 newtcbox))
497 (opt (nth 2 newtcbox))
498 (renew (when (string= (nth 3 newtcbox) "re")
500 ;; When renew'ing, delete any entry from
501 ;; `LaTeX-environment-list' first:
503 (setq LaTeX-environment-list
505 (car (assoc box (LaTeX-environment-list)))
506 LaTeX-environment-list)))
507 (cond (;; opt. 1st argument and mandatory argument(s)
508 (and arg (not (string= arg ""))
509 opt (not (string= opt "")))
510 (LaTeX-add-environments
513 (vector 'TeX-arg-key-val 'LaTeX-tcolorbox-keyval-options-local)
514 (1- (string-to-number arg)))))
515 (;; mandatory argument(s) only
516 (and arg (not (string= arg ""))
517 (string-equal opt ""))
518 (LaTeX-add-environments
519 (list box (string-to-number arg))))
521 (LaTeX-add-environments (list box))))))
523 ;; Process new macros from \newtcbox
524 (dolist (newtcbox (apply #'append LaTeX-tcolorbox-newtcbox-list))
525 (let ((box (nth 0 newtcbox))
526 (arg (nth 1 newtcbox))
527 (opt (nth 2 newtcbox))
528 (renew (when (string= (nth 3 newtcbox) "re")
530 ;; When renew'ing, delete any entry from `TeX-symbol-list'
533 (setq TeX-symbol-list
535 (car (assoc box (TeX-symbol-list)))
537 (cond (;; opt. 1st argument and mandatory argument(s)
538 (and arg (not (string= arg ""))
539 opt (not (string= opt "")))
540 (TeX-add-symbols (list box
541 (vector 'TeX-arg-key-val 'LaTeX-tcolorbox-keyval-options-local)
542 (1- (string-to-number arg)))))
543 (;; mandatory argument(s) only
544 (and arg (not (string= arg ""))
545 (string-equal opt ""))
546 (TeX-add-symbols (list box (string-to-number arg))))
547 (t ; No args -- in pratice, this will probably never happen
548 (TeX-add-symbols (list box))))))
551 (LaTeX-tcolorbox-update-style-key)
553 ;; Load style hooks for libraries, if any.
554 (LaTeX-tcolorbox-load-used-libraries))
556 (add-hook 'TeX-auto-prepare-hook #'LaTeX-tcolorbox-auto-prepare t)
557 (add-hook 'TeX-auto-cleanup-hook #'LaTeX-tcolorbox-auto-cleanup t)
558 (add-hook 'TeX-update-style-hook #'TeX-auto-parse t)
564 ;; Activate the buffer-local version of key-vals.
565 (setq LaTeX-tcolorbox-keyval-options-local
566 (copy-alist LaTeX-tcolorbox-keyval-options))
568 ;; Collect key=val's from libraries in
569 ;; `LaTeX-tcolorbox-keyval-options-full'; \tcbset needs this:
570 (setq LaTeX-tcolorbox-keyval-options-full
571 (copy-alist LaTeX-tcolorbox-keyval-options-local))
573 ;; Add tcolorbox to the parser.
574 (TeX-auto-add-regexp LaTeX-tcolorbox-newtcolorbox-regexp)
575 (TeX-auto-add-regexp LaTeX-tcolorbox-newtcbox-regexp)
576 (TeX-auto-add-regexp LaTeX-tcolorbox-tcbuselibrary-regexp)
578 ;; We just run the style hook for `xcolor' which is loaded by pgf.sty
579 (TeX-run-style-hooks "xcolor")
581 ;; FIXME: Anything missing?
586 (TeX-arg-eval mapconcat #'identity
587 (TeX-completing-read-multiple
588 (TeX-argument-prompt optional nil "Libraries")
589 LaTeX-tcolorbox-library-list) ","))
591 ;; 3 Macros for Box Creation
595 (TeX-arg-key-val LaTeX-tcolorbox-keyval-options-full))
597 '("tcbsetforeverylayer"
598 (TeX-arg-key-val LaTeX-tcolorbox-keyval-options-local))
601 [ TeX-arg-eval TeX-read-key-val optional
603 LaTeX-tcolorbox-tcbox-options
604 LaTeX-tcolorbox-keyval-options-local) ]
608 [ TeX-arg-key-val LaTeX-tcolorbox-init-options ]
610 [ TeX-arg-define-macro-arguments ]
611 (TeX-arg-key-val LaTeX-tcolorbox-keyval-options-local))
614 [ TeX-arg-key-val LaTeX-tcolorbox-init-options ]
615 (TeX-arg-eval completing-read
616 (TeX-argument-prompt optional nil "Color box")
617 (LaTeX-tcolorbox-newtcolorbox-list))
618 [ TeX-arg-define-macro-arguments ]
619 (TeX-arg-key-val LaTeX-tcolorbox-keyval-options-local))
622 [ TeX-arg-key-val LaTeX-tcolorbox-init-options ]
624 [ TeX-arg-define-macro-arguments ]
625 (TeX-arg-key-val LaTeX-tcolorbox-keyval-options-local))
628 [ TeX-arg-key-val LaTeX-tcolorbox-init-options ]
631 (let ((macro (completing-read
632 (TeX-argument-prompt optional nil "Macro: \\" t)
633 (LaTeX-tcolorbox-newtcbox-list))))
634 (concat TeX-esc macro))))
635 [ TeX-arg-define-macro-arguments ]
636 (TeX-arg-key-val LaTeX-tcolorbox-keyval-options-local))
638 '("tcolorboxenvironment"
640 (TeX-arg-key-val LaTeX-tcolorbox-keyval-options-local))
642 ;; 4.16 Layered Boxes and Every Box Settings
643 '("tcbsetmanagedlayer" "Number")
645 ;; 4.22 Even and Odd Pages
647 '("tcbifoddpageoroneside" 2)
648 '("thetcolorboxnumber" 0)
649 '("thetcolorboxpage" 0)
651 ;; 5.2 Lists of tcolorboxes
653 [ TeX-arg-eval completing-read
654 (TeX-argument-prompt optional nil "Macro")
655 (if (< (LaTeX-largest-level) 2)
656 '("\\chapter" "\\section" "\\subsection" "\\subsubsection")
657 '("\\section" "\\subsection" "\\subsubsection")) ]
660 ;; 7 Saving and Loading of Verbatim Texts
664 '("tcbstartrecording" [ "File name" ])
665 '("tcbstoprecording")
667 '("tcbinputrecords" [ TeX-arg-file ]) ) ; Terminate TeX-add-symbols
669 (LaTeX-add-environments
670 ;; 3 Macros for Box Creation: Main env
671 '("tcolorbox" LaTeX-env-args
672 [ TeX-arg-key-val LaTeX-tcolorbox-keyval-options-local ])
674 ;; 7 Saving and Loading of Verbatim Texts
675 '("tcbverbatimwrite" "File name")
678 ;; Do not indent text in verbatim environments:
679 (make-local-variable 'LaTeX-indent-environment-list)
680 (add-to-list 'LaTeX-indent-environment-list
681 '("tcbverbatimwrite" current-indentation) t)
682 (add-to-list 'LaTeX-indent-environment-list
683 '("tcbwritetemp" current-indentation) t)
686 (when (and (featurep 'font-latex)
687 (eq TeX-install-font-lock 'font-latex-setup))
688 (font-latex-add-keywords '(("tcbuselibrary" "{")
690 ("tcbsetforeverylayer" "{")
692 ("newtcolorbox" "[{[[{")
693 ("renewtcolorbox" "[{[[{")
695 ("renewtcbox" "[{[[{")
696 ("tcolorboxenvironment" "{{")
697 ("tcbsetmanagedlayer" "{"))
701 (defvar LaTeX-tcolorbox-package-options LaTeX-tcolorbox-library-list
702 "Package options for the tcolorbox package.")
704 ;;; tcolorbox.el ends here