1 ;;; org-beamer.el --- Beamer-specific LaTeX export for org-mode
3 ;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten.dominik AT gmail DOT com>
7 ;; Maintainer: Carsten Dominik <carsten.dominik AT gmail DOT com>
8 ;; Keywords: org, wp, tex
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This library implement the special treatment needed by using the
28 ;; beamer class during LaTeX export.
32 (defvar org-export-latex-header
)
33 (defvar org-export-latex-options-plist
)
34 (defvar org-export-opt-plist
)
36 (defgroup org-beamer nil
37 "Options specific for using the beamer class in LaTeX export."
39 :group
'org-export-latex
)
41 (defcustom org-beamer-use-parts nil
46 (defcustom org-beamer-frame-level
1
47 "The level that should be interpreted as a frame.
48 The levels above this one will be translated into a sectioning structure.
49 Setting this to 2 will allow sections, 3 will allow subsections as well.
50 You can se this to 4 as well, if you at the same time set
51 `org-beamer-use-parts' to make the top levels `\part'."
54 (const :tag
"Frames need a BEAMER_env property" nil
)
55 (integer :tag
"Specific level makes a frame")))
57 (defcustom org-beamer-column-view-format
58 "%45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra)"
59 "Default column view format that should be used to fill the template."
61 :type
'(string :tag
"Beamer column view format"))
63 (defcustom org-beamer-themes
64 "\\usetheme{default}\\usecolortheme{default}"
65 "Default string to be used for extra heading stuff in beamer presentations.
66 When a beamer template is filled, this will be the default for
67 BEAMER_HEADER_EXTRA, which will be inserted just before \\begin{document}."
69 :type
'(string :tag
"Beamer column view format"))
72 (defconst org-beamer-column-widths
73 "0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC"
74 "The column widths that should be installed as allowed property values.")
76 (defconst org-beamer-transitions
77 "\transblindsvertical \transblindshorizontal \transboxin \transboxout \transdissolve \transduration \transglitter \transsplithorizontalin \transsplithorizontalout \transsplitverticalin \transsplitverticalout \transwipe :ETC"
78 "Transitions available for beamer.
79 These are just a completion help.")
81 (defconst org-beamer-environments-default
82 '(("frame" "f" "dummy- special handling hard coded" "dummy")
83 ("columns" "C" "\\begin{columns}%o %% %h%x" "\\end{columns}")
84 ("column" "c" "\\begin{column}%o{%h\\textwidth}%x" "\\end{column}")
85 ("block" "b" "\\begin{block}%a{%h}%x" "\\end{block}")
86 ("alertblock" "a" "\\begin{alertblock}%a{%h}%x" "\\end{alertblock}")
87 ("verse" "v" "\\begin{verse}%a %% %h%x" "\\end{verse}")
88 ("quotation" "q" "\\begin{quotation}%a %% %h%x" "\\end{quotation}")
89 ("quote" "Q" "\\begin{quote}%a %% %h%x" "\\end{quote}")
90 ("structureenv" "s" "\\begin{structureenv}%a %% %h%x" "\\end{structureenv}")
91 ("theorem" "t" "\\begin{theorem}%a%U%x" "\\end{theorem}")
92 ("definition" "d" "\\begin{definition}%a%U%x" "\\end{definition}")
93 ("example" "e" "\\begin{example}%a%U%x" "\\end{example}")
94 ("proof" "p" "\\begin{proof}%a%U%x" "\\end{proof}")
95 ("beamercolorbox" "o" "\\begin{beamercolorbox}%o{%h}%x" "\\end{beamercolorbox}")
96 ("normal" "n" "%h" "")
97 ("ignoreheading" "i" "%%%% %h" ""))
98 "Environments triggered by properties in Beamer export.
99 These are the defaults - for user definitions, see
100 `org-beamer-environments-extra'.
101 \"normal\" is a special fake environment. It is needed when an environment
102 should be surrounded by normal text. Since beamer export converts sections
103 into environments, you need to have a headline to end the environment.
108 *** Blocktitle :B_block:
110 *** After the block :B_normal:
114 (defcustom org-beamer-environments-extra nil
115 "Environments triggered by tags in Beamer export.
116 Each entry has 4 elements:
118 name Name of the environment
119 key Selection key for `org-beamer-set-environment-tag'
120 open The opening template for the environment, with the following excapes
121 %a the action/overlay specification
122 %A the default action/overlay specification
123 %o the options argument of the template
125 %H if there is headline text, that text in {} braces
126 %U if there is headline text, that text in [] brackets
127 close The closing string of the environment."
132 (string :tag
"Environment")
133 (string :tag
"Selection key")
134 (string :tag
"Begin")
135 (string :tag
"End"))))
137 (defvar org-beamer-frame-level-now nil
)
138 (defvar org-beamer-header-extra nil
)
139 (defvar org-beamer-export-is-beamer-p nil
)
140 (defvar org-beamer-inside-frame-at-level nil
)
141 (defvar org-beamer-columns-open nil
)
142 (defvar org-beamer-column-open nil
)
144 (defun org-beamer-cleanup-column-width (width)
145 "Make sure the width is not empty, and that it has a unit."
146 (setq width
(org-trim (or width
"")))
147 (unless (string-match "\\S-" width
) (setq width
"0.5"))
148 (if (string-match "\\`[.0-9]+\\'" width
)
149 (setq width
(concat width
"\\textwidth")))
152 (defun org-beamer-open-column (&optional width opt
)
153 (org-beamer-close-column-maybe)
154 (setq org-beamer-column-open t
)
155 (setq width
(org-beamer-cleanup-column-width width
))
156 (insert (format "\\begin{column}%s{%s}\n" (or opt
"") width
)))
157 (defun org-beamer-close-column-maybe ()
158 (when org-beamer-column-open
159 (setq org-beamer-column-open nil
)
160 (insert "\\end{column}\n")))
161 (defun org-beamer-close-column-maybe-1 ()
162 (when org-beamer-column-open
163 (setq org-beamer-column-open nil
)
164 (insert "\\end{column}\n"))
165 (setq org-beamer-columns-open nil
))
166 (defun org-beamer-open-columns-maybe (&optional opts
)
167 (unless org-beamer-columns-open
168 (setq org-beamer-columns-open t
)
169 (insert (format "\\begin{columns}%s\n" (or opts
"")))))
170 (defun org-beamer-close-columns-maybe ()
171 (org-beamer-close-column-maybe)
172 (when org-beamer-columns-open
173 (setq org-beamer-columns-open nil
)
174 (insert "\\end{columns}\n")))
176 (defun org-beamer-set-environment-tag ()
177 "Set an environment tag, to determine the beamer environment to be used.
178 This makes use of the fast tag selection interface."
180 (let* ((envs (append org-beamer-environments-extra
181 org-beamer-environments-default
))
183 (append '((:startgroup
))
184 (mapcar (lambda (e) (cons (concat "B_" (car e
))
185 (string-to-char (nth 1 e
))))
189 (org-fast-tag-selection-single-key t
))
191 (let ((tags (or (ignore-errors (org-get-tags-string)) "")))
193 ((equal org-last-tag-selection-key ?|
)
194 (if (string-match ":BMCOL:" tags
)
195 (org-set-property "BEAMER_col" (read-string "Column width: "))
196 (org-delete-property "BEAMER_col")))
197 ((string-match (concat ":B_\\("
198 (mapconcat 'car envs
"\\|")
201 (org-entry-put nil
"BEAMER_env" (match-string 1 tags
)))
202 (t (org-entry-delete nil
"BEAMER_env"))))))
205 (defun org-beamer-sectioning (level text
)
206 "Return the sectioning entry for the current headline.
207 LEVEL is the reduced level of the headline.
208 TEXT is the text of the headline, everything except the leading stars.
209 The return value is a cons cell. The car is the headline text, usually
210 just TEXT, but possibly modified if options have been extracted from the
211 text. The cdr is the sectioning entry, similar to what is given
212 in org-export-latex-classes."
213 (let* ((frame-level (or org-beamer-frame-level-now org-beamer-frame-level
))
215 (if org-beamer-use-parts
216 '((1 .
("\\part{%s}" .
"\\part*{%s}"))
217 (2 .
("\\section{%s}" .
"\\section*{%s}"))
218 (3 .
("\\subsection{%s}" .
"\\subsection*{%s}")))
219 '((1 .
("\\section{%s}" .
"\\section*{%s}"))
220 (2 .
("\\subsection{%s}" .
"\\subsection*{%s}")))))
221 (envs (append org-beamer-environments-extra
222 org-beamer-environments-default
))
223 (props (org-get-text-property-any 0 'org-props text
))
224 (in "") (out "") option action defaction environment extra
225 columns-option column-option
226 env have-text ass tmp
)
227 (if (= frame-level
0) (setq frame-level nil
))
228 (when (and org-beamer-inside-frame-at-level
229 (<= level org-beamer-inside-frame-at-level
))
230 (setq org-beamer-inside-frame-at-level nil
))
231 (when (setq tmp
(org-beamer-assoc-not-empty "BEAMER_col" props
))
232 (when (setq ass
(assoc "BEAMER_envargs" props
))
233 (let (case-fold-search)
234 (when (string-match "C\\(\\[[^][]*\\]\\)" (cdr ass
))
235 (setq columns-option
(match-string 1 (cdr ass
)))
236 (setcdr ass
(replace-match "" t t
(cdr ass
))))
237 (when (string-match "c\\(\\[[^][]*\\]\\)" (cdr ass
))
238 (setq column-option
(match-string 1 (cdr ass
)))
239 (setcdr ass
(replace-match "" t t
(cdr ass
))))))
240 (org-beamer-open-columns-maybe columns-option
)
241 (org-beamer-open-column tmp column-option
))
243 ((or (equal (cdr (assoc "BEAMER_env" props
)) "frame")
244 (and frame-level
(= level frame-level
)))
246 (org-beamer-get-special props
)
248 (setq in
(org-fill-template
249 "\\begin{frame}%a%A%o%T%S%x"
250 (list (cons "a" (or action
""))
251 (cons "A" (or defaction
""))
252 (cons "o" (or option
""))
253 (cons "x" (if extra
(concat "\n" extra
) ""))
255 (cons "T" (if (string-match "\\S-" text
)
256 "\n\\frametitle{%s}" ""))
257 (cons "S" (if (string-match "\\\\\\\\" text
)
258 "\n\\framesubtitle{%s}" ""))))
259 out
(copy-sequence "\\end{frame}"))
261 '(org-insert-hook org-beamer-close-columns-maybe
))
262 (setq org-beamer-inside-frame-at-level level
)
263 (cons text
(list in out in out
)))
264 ((and (setq env
(cdr (assoc "BEAMER_env" props
)))
265 (setq ass
(assoc env envs
)))
266 ;; A beamer environment selected by the BEAMER_env property
267 (if (string-match "[ \t]+:[ \t]*$" text
)
268 (setq text
(replace-match "" t t text
)))
269 (org-beamer-get-special props
)
270 (setq text
(org-trim text
))
271 (setq have-text
(string-match "\\S-" text
))
272 (setq in
(org-fill-template
274 (list (cons "a" (or action
""))
275 (cons "A" (or defaction
""))
276 (cons "o" (or option
""))
277 (cons "x" (if extra
(concat "\n" extra
) ""))
279 (cons "H" (if have-text
(concat "{" text
"}") ""))
280 (cons "U" (if have-text
(concat "[" text
"]") ""))))
283 ((equal out
"\\end{columns}")
284 (setq org-beamer-columns-open t
)
285 (setq out
(org-add-props (copy-sequence out
)
288 (org-beamer-close-column-maybe)
289 (setq org-beamer-columns-open nil
))))))
290 ((equal out
"\\end{column}")
291 (org-beamer-open-columns-maybe)))
292 (cons text
(list in out in out
)))
293 ((and (not org-beamer-inside-frame-at-level
)
294 (or (not frame-level
)
295 (< level frame-level
))
296 (assoc level default
))
298 (cons text
(cdr (assoc level default
))))
306 (defun org-beamer-get-special (props)
307 "Extract an option, action, and default action string from text.
308 The variables option, action, defaction, extra are all scoped into
309 this function dynamically."
311 (setq environment
(org-beamer-assoc-not-empty "BEAMER_env" props
))
312 (setq extra
(org-beamer-assoc-not-empty "BEAMER_extra" props
))
314 (setq extra
(replace-regexp-in-string "\\\\n" "\n" extra
)))
315 (setq tmp
(org-beamer-assoc-not-empty "BEAMER_envargs" props
))
317 (setq tmp
(copy-sequence tmp
))
318 (if (string-match "\\[<[^][<>]*>\\]" tmp
)
319 (setq defaction
(match-string 0 tmp
)
320 tmp
(replace-match "" t t tmp
)))
321 (if (string-match "\\[[^][]*\\]" tmp
)
322 (setq option
(match-string 0 tmp
)
323 tmp
(replace-match "" t t tmp
)))
324 (if (string-match "<[^<>]*>" tmp
)
325 (setq action
(match-string 0 tmp
)
326 tmp
(replace-match "" t t tmp
))))))
328 (defun org-beamer-assoc-not-empty (elt list
)
329 (let ((tmp (cdr (assoc elt list
))))
330 (and tmp
(string-match "\\S-" tmp
) tmp
)))
333 (defvar org-beamer-mode-map
(make-sparse-keymap)
334 "The keymap for `org-beamer-mode'.")
335 (define-key org-beamer-mode-map
"\C-c\C-b" 'org-beamer-set-environment-tag
)
337 (define-minor-mode org-beamer-mode
338 "Special support for editing Org-mode files made to export to beamer."
340 (font-lock-add-keywords
342 '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-beamer-tag prepend
))
345 (defun org-beamer-place-default-actions-for-lists ()
346 "Find default overlay specifications in items, and move them.
347 The need to be after the begin statement of the environment."
348 (when org-beamer-export-is-beamer-p
350 (goto-char (point-min))
351 (while (re-search-forward
352 "^[ \t]*\\\\begin{\\(itemize\\|enumerate\\|desctiption\\)}[ \t\n]*\\\\item\\>\\( ?\\(<[^<>\n]*>\\|\\[[^][\n*]\\]\\)\\)?[ \t]*\\S-" nil t
)
353 (if (setq dovl
(cdr (assoc "BEAMER_dovl" (get-text-property (match-end 0) 'org-props
))))
355 (goto-char (1+ (match-end 1)))
358 (defun org-beamer-amend-header ()
359 (when (and org-beamer-export-is-beamer-p
360 org-beamer-header-extra
)
361 (goto-char (point-min))
362 (when (re-search-forward "^[ \t]*\\\\begin{document}" nil t
)
363 (goto-char (match-beginning 0))
364 (insert org-beamer-header-extra
)
365 (or (bolp) (insert "\n")))))
367 (defcustom org-beamer-fragile-re
"^[ \t]*\\\\begin{verbatim}"
368 "If this regexp matches in a frame, the frame is marked as fragile."
372 (defface org-beamer-tag
'((t (:box
(:line-width
1 :color grey40
))))
373 "The special face for beamer tags."
377 ;; Functions to initialize and post-process
378 ;; These fuctions will be hooked into various places in the export process
380 (defun org-beamer-initialize-open-trackers ()
381 "Reset variables that track if certain environments are open during export."
382 (setq org-beamer-columns-open nil
)
383 (setq org-beamer-column-open nil
)
384 (setq org-beamer-inside-frame-at-level nil
)
385 (setq org-beamer-export-is-beamer-p nil
))
387 (defun org-beamer-after-initial-vars ()
388 "Find special setings for beamer and store them.
389 The effect is that these values will be accessible during export."
390 ;; First verify that we are exporting using the beamer class
391 (setq org-beamer-export-is-beamer-p
392 (string-match "\\\\documentclass\\(\\[[^][]*?\\]\\)?{beamer}"
393 org-export-latex-header
))
394 (when org-beamer-export-is-beamer-p
395 ;; Find the frame level
396 (setq org-beamer-frame-level-now
397 (or (and (org-region-active-p)
399 (goto-char (region-beginning))
400 (and (looking-at org-complex-heading-regexp
)
401 (org-entry-get nil
"BEAMER_FRAME_LEVEL" 'selective
))))
405 (goto-char (point-min))
406 (and (re-search-forward
407 "^#\\+BEAMER_FRAME_LEVEL:[ \t]*\\(.*?\\)[ \t]*$" nil t
)
409 (plist-get org-export-latex-options-plist
:beamer-frame-level
)
410 org-beamer-frame-level
))
411 ;; Normalize the value so that the functions can trust the value
413 ((not org-beamer-frame-level-now
)
414 (setq org-beamer-frame-level-now nil
))
415 ((stringp org-beamer-frame-level-now
)
416 (setq org-beamer-frame-level-now
417 (string-to-number org-beamer-frame-level-now
))))
418 ;; Find the header additons, most likely theme commands
419 (setq org-beamer-header-extra
420 (or (and (org-region-active-p)
422 (goto-char (region-beginning))
423 (and (looking-at org-complex-heading-regexp
)
424 (org-entry-get nil
"BEAMER_HEADER_EXTRA"
429 (goto-char (point-min))
430 (and (re-search-forward
431 "^#\\+BEAMER_HEADER_EXTRA:[ \t]*\\(.*?\\)[ \t]*$" nil t
)
433 (plist-get org-export-latex-options-plist
434 :beamer-header-extra
)))
435 (remove-text-properties (point-min) (point-max) '(org-props nil
))
437 '(put-text-property (point-at-bol) (point-at-eol) 'org-props
438 (org-entry-properties nil
'standard
)))
439 (setq org-export-latex-options-plist
440 (plist-put org-export-latex-options-plist
:tags nil
))
441 (remove-text-properties (point-min) (point-max) '(org-props nil
))
443 '(put-text-property (point-at-bol) (point-at-eol) 'org-props
444 (org-entry-properties nil
'standard
)))))
446 (defun org-beamer-auto-fragile-frames ()
447 "Mark any frames containing verbatim environments as fragile.
448 This funcion will run in the final LaTeX document."
449 (when org-beamer-export-is-beamer-p
451 (goto-char (point-min))
452 ;; Find something that might be fragile
453 (while (re-search-forward org-beamer-fragile-re nil t
)
455 ;; Are we inside a frame here?
456 (when (and (re-search-backward "^[ \t]*\\\\\\(begin\\|end\\){frame}"
458 (equal (match-string 1) "begin"))
459 ;; yes, inside a frame, make sure "fragile" is one of the options
460 (goto-char (match-end 0))
461 (if (not (looking-at "\\[.*?\\]"))
463 (setq opts
(substring (match-string 0) 1 -
1))
464 (delete-region (match-beginning 0) (match-end 0))
465 (setq opts
(org-split-string opts
","))
466 (add-to-list 'opts
"fragile")
467 (insert "[" (mapconcat 'identity opts
",") "]"))))))))
469 (defun org-beamer-fix-toc ()
470 "Fix the table of contents by removing the vspace line."
471 (when org-beamer-export-is-beamer-p
473 (goto-char (point-min))
474 (when (re-search-forward "\\\\setcounter{tocdepth.*\n\\\\tableofcontents.*\n\\(\\\\vspace\\*.*\\)" nil t
)
475 (delete-region (match-beginning 1) (match-end 1))))))
477 (defun org-beamer-property-changed (property value
)
478 "Track the BEAMER_env property with tags."
480 ((equal property
"BEAMER_env")
482 (org-back-to-heading t
)
483 (let ((tags (org-get-tags)))
484 (setq tags
(delq nil
(mapcar (lambda (x)
485 (if (string-match "^B_" x
) nil x
))
487 (org-set-tags-to tags
))
488 (when (and value
(stringp value
) (string-match "\\S-" value
))
489 (org-toggle-tag (concat "B_" value
) 'on
))))
490 ((equal property
"BEAMER_col")
491 (org-toggle-tag "BMCOL" (if (and value
(string-match "\\S-" value
))
494 (defun org-beamer-select-beamer-code ()
495 "Take code marked for BEAMER and turn it into marked for LaTeX."
496 (when org-beamer-export-is-beamer-p
497 (goto-char (point-min))
498 (while (re-search-forward
499 "^\\([ \]*#\\+\\(begin_\\|end_\\)?\\)\\(beamer\\)\\>" nil t
)
500 (replace-match "\\1latex"))))
502 ;; OK, hook all these functions into appropriate places
503 (add-hook 'org-export-first-hook
504 'org-beamer-initialize-open-trackers
)
505 (add-hook 'org-property-changed-functions
506 'org-beamer-property-changed
)
507 (add-hook 'org-export-latex-after-initial-vars-hook
508 'org-beamer-after-initial-vars
)
509 (add-hook 'org-export-latex-final-hook
510 'org-beamer-place-default-actions-for-lists
)
511 (add-hook 'org-export-latex-final-hook
512 'org-beamer-auto-fragile-frames
)
513 (add-hook 'org-export-latex-final-hook
515 (add-hook 'org-export-latex-final-hook
516 'org-beamer-amend-header
)
517 (add-hook 'org-export-preprocess-before-selecting-backend-code-hook
518 'org-beamer-select-beamer-code
)
520 (defun org-beamer-settings-template (kind)
521 "Insert a settings template, to make sure users do this right."
523 (message "Current [s]ubtree or [g]lobal?")
524 (if (equal (read-char-exclusive) ?g
)
527 (if (eq kind
'subtree
)
529 (org-back-to-heading t
)
531 (org-entry-put nil
"LaTeX_CLASS" "beamer")
532 (org-entry-put nil
"LaTeX_CLASS_OPTIONS" "[presentation]")
533 (org-entry-put nil
"EXPORT_FILE_NAME" "presentation.pdf")
534 (org-entry-put nil
"BEAMER_FRAME_LEVEL" (number-to-string
535 org-beamer-frame-level
))
536 (org-entry-put nil
"BEAMER_HEADER_EXTRA" org-beamer-themes
)
537 (org-entry-put nil
"COLUMNS" org-beamer-column-view-format
)
538 (org-entry-put nil
"BEAMER_col_ALL" "0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC"))
539 (insert "#+LaTeX_CLASS: beamer\n")
540 (insert "#+LaTeX_CLASS_OPTIONS: [presentation]\n")
541 (insert (format "#+BEAMER_FRAME_LEVEL: %d\n" org-beamer-frame-level
) "\n")
542 (insert "#+BEAMER_HEADER_EXTRA: " org-beamer-themes
"\n")
543 (insert "#+COLUMNS: " org-beamer-column-view-format
"\n")
544 (insert "#+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC\n")))
547 (defun org-beamer-allowed-property-values (property)
548 "Supply allowed values for BEAMER properties."
550 ((and (equal property
"BEAMER_env")
551 (not (org-entry-get nil
(concat property
"_ALL") 'inherit
)))
552 ;; If no allowed values for BEAMER_env have been defined,
553 ;; supply all defined environments
554 (mapcar 'car
(append org-beamer-environments-extra
555 org-beamer-environments-default
)))
556 ((and (equal property
"BEAMER_col")
557 (not (org-entry-get nil
(concat property
"_ALL") 'inherit
)))
558 ;; If no allowed values for BEAMER_col have been defined,
560 '("0.1" "0.2" "0.3" "0.4" "0.5" "0.6" "0.7" "0.8" "0.9" "" ":ETC"))
563 (add-hook 'org-property-allowed-value-functions
564 'org-beamer-allowed-property-values
)
566 (provide 'org-beamer
)
568 ;; arch-tag: 68bac91a-a946-43a3-8173-a9269306f67c
570 ;;; org-beamer.el ends here