1 ;;; ox-beamer.el --- Beamer Back-End for Org Export Engine
3 ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten.dominik AT gmail DOT com>
6 ;; Nicolas Goaziou <n.goaziou AT gmail DOT com>
7 ;; Keywords: org, wp, tex
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; This library implements both a Beamer back-end, derived from the
25 ;; LaTeX one and a minor mode easing structure edition of the
28 ;; Depending on the desired output format, three commands are provided
29 ;; for export: `org-beamer-export-as-latex' (temporary buffer),
30 ;; `org-beamer-export-to-latex' ("tex" file) and
31 ;; `org-beamer-export-to-pdf' ("pdf" file).
33 ;; This back-end supports every buffer keyword, attribute and options
34 ;; items (see `org-latex-options-alist') already supported by `latex'
35 ;; back-end. As such, it is suggested to add an entry in
36 ;; `org-latex-classes' variable which is appropriate for Beamer
39 ;; On top of this, the `beamer' back-end also introduces the following
40 ;; keywords: "BEAMER_THEME", "BEAMER_COLOR_THEME",
41 ;; "BEAMER_FONT_THEME", "BEAMER_INNER_THEME", "BEAMER_OUTER_THEME" and
42 ;; "BEAMER_HEADER". All but the latter accept options in square
45 ;; Moreover, headlines now fall into three categories: sectioning
46 ;; elements, frames and blocks.
48 ;; - Headlines become frames when their level is equal to
49 ;; `org-beamer-frame-level' (or "H" value in the OPTIONS line).
50 ;; Though, if a headline in the current tree has a "BEAMER_env"
51 ;; (see below) property set to either "frame" or "fullframe", its
52 ;; level overrides the variable. A "fullframe" is a frame with an
53 ;; empty (ignored) title.
55 ;; - All frames' children become block environments. Special block
56 ;; types can be enforced by setting headline's "BEAMER_env" property
57 ;; to an appropriate value (see `org-beamer-environments-default'
58 ;; for supported value and `org-beamer-environments-extra' for
61 ;; - As a special case, if the "BEAMER_env" property is set to either
62 ;; "appendix", "note", "noteNH" or "againframe", the headline will
63 ;; become, respectively, an appendix, a note (within frame or
64 ;; between frame, depending on its level), a note with its title
65 ;; ignored or an againframe command. In the latter case,
66 ;; a "BEAMER_ref" property is mandatory in order to refer to the
67 ;; frame being resumed, and contents are ignored.
69 ;; Also, a headline with an "ignoreheading" environment will have
70 ;; its contents only inserted in the output. This special value is
71 ;; useful to have data between frames, or to properly close
72 ;; a "column" environment.
74 ;; Along with "BEAMER_env", headlines also support the "BEAMER_act"
75 ;; and "BEAMER_opt" properties. The former is translated as an
76 ;; overlay/action specification (or a default overlay specification
77 ;; when enclosed within square brackets) whereas the latter specifies
78 ;; options for the current frame ("fragile" option is added
79 ;; automatically, though).
81 ;; Moreover, headlines handle the "BEAMER_col" property. Its value
82 ;; should be a decimal number representing the width of the column as
83 ;; a fraction of the total text width. If the headline has no
84 ;; specific environment, its title will be ignored and its contents
85 ;; will fill the column created. Otherwise, the block will fill the
86 ;; whole column and the title will be preserved. Two contiguous
87 ;; headlines with a non-nil "BEAMER_col" value share the same
88 ;; "columns" LaTeX environment. It will end before the next headline
89 ;; without such a property. This environment is generated
90 ;; automatically. Although, it can also be explicitly created, with
91 ;; a special "columns" value for "BEAMER_env" property (if it needs to
92 ;; be set up with some specific options, for example).
94 ;; Every plain list has support for `:environment', `:overlay' and
95 ;; `:options' attributes (through ATTR_BEAMER affiliated keyword).
96 ;; The first one allows to use a different environment, the second
97 ;; sets overlay specifications and the last one inserts optional
98 ;; arguments in current list environment.
100 ;; Table of contents generated from "toc:t" option item are wrapped
101 ;; within a "frame" environment. Those generated from a TOC keyword
102 ;; aren't. TOC keywords accept options enclosed within square
103 ;; brackets (e.g. #+TOC: headlines [currentsection]).
105 ;; Eventually, an export snippet with a value enclosed within angular
106 ;; brackets put at the beginning of an element or object whose type is
107 ;; among `bold', `item', `link', `radio-target' and `target' will
108 ;; control its overlay specifications.
110 ;; On the minor mode side, `org-beamer-select-environment' (bound by
111 ;; default to "C-c C-b") and `org-beamer-insert-options-template' are
112 ;; the two entry points.
116 (eval-when-compile (require 'cl
))
121 ;;; User-Configurable Variables
123 (defgroup org-export-beamer nil
124 "Options specific for using the beamer class in LaTeX export."
129 (defcustom org-beamer-frame-level
1
130 "The level at which headlines become frames.
132 Headlines at a lower level will be translated into a sectioning
133 structure. At a higher level, they will be translated into
136 If a headline with a \"BEAMER_env\" property set to \"frame\" is
137 found within a tree, its level locally overrides this number.
139 This variable has no effect on headlines with the \"BEAMER_env\"
140 property set to either \"ignoreheading\", \"appendix\", or
141 \"note\", which will respectively, be invisible, become an
144 This integer is relative to the minimal level of a headline
145 within the parse tree, defined as 1."
146 :group
'org-export-beamer
149 (defcustom org-beamer-frame-default-options
""
150 "Default options string to use for frames.
151 For example, it could be set to \"allowframebreaks\"."
152 :group
'org-export-beamer
153 :type
'(string :tag
"[options]"))
155 (defcustom org-beamer-column-view-format
156 "%45ITEM %10BEAMER_env(Env) %10BEAMER_act(Act) %4BEAMER_col(Col) %8BEAMER_opt(Opt)"
157 "Column view format that should be used to fill the template."
158 :group
'org-export-beamer
160 :package-version
'(Org .
"8.0")
162 (const :tag
"Do not insert Beamer column view format" nil
)
163 (string :tag
"Beamer column view format")))
165 (defcustom org-beamer-theme
"default"
166 "Default theme used in Beamer presentations."
167 :group
'org-export-beamer
169 :package-version
'(Org .
"8.0")
171 (const :tag
"Do not insert a Beamer theme" nil
)
172 (string :tag
"Beamer theme")))
174 (defcustom org-beamer-environments-extra nil
175 "Environments triggered by tags in Beamer export.
176 Each entry has 4 elements:
178 name Name of the environment
179 key Selection key for `org-beamer-select-environment'
180 open The opening template for the environment, with the following escapes
181 %a the action/overlay specification
182 %A the default action/overlay specification
183 %o the options argument of the template
185 %H if there is headline text, that text in {} braces
186 %U if there is headline text, that text in [] brackets
187 close The closing string of the environment."
188 :group
'org-export-beamer
190 :package-version
'(Org .
"8.0")
193 (string :tag
"Environment")
194 (string :tag
"Selection key")
195 (string :tag
"Begin")
196 (string :tag
"End"))))
198 (defcustom org-beamer-outline-frame-title
"Outline"
199 "Default title of a frame containing an outline."
200 :group
'org-export-beamer
201 :type
'(string :tag
"Outline frame title"))
203 (defcustom org-beamer-outline-frame-options
""
204 "Outline frame options appended after \\begin{frame}.
205 You might want to put e.g. \"allowframebreaks=0.9\" here."
206 :group
'org-export-beamer
207 :type
'(string :tag
"Outline frame options"))
211 ;;; Internal Variables
213 (defconst org-beamer-column-widths
214 "0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.0 :ETC"
215 "The column widths that should be installed as allowed property values.")
217 (defconst org-beamer-environments-special
224 ("ignoreheading" "i")
227 "Alist of environments treated in a special way by the back-end.
228 Keys are environment names, as strings, values are bindings used
229 in `org-beamer-select-environment'. Environments listed here,
230 along with their binding, are hard coded and cannot be modified
231 through `org-beamer-environments-extra' variable.")
233 (defconst org-beamer-environments-default
234 '(("block" "b" "\\begin{block}%a{%h}" "\\end{block}")
235 ("alertblock" "a" "\\begin{alertblock}%a{%h}" "\\end{alertblock}")
236 ("verse" "v" "\\begin{verse}%a %% %h" "\\end{verse}")
237 ("quotation" "q" "\\begin{quotation}%a %% %h" "\\end{quotation}")
238 ("quote" "Q" "\\begin{quote}%a %% %h" "\\end{quote}")
239 ("structureenv" "s" "\\begin{structureenv}%a %% %h" "\\end{structureenv}")
240 ("theorem" "t" "\\begin{theorem}%a%U" "\\end{theorem}")
241 ("definition" "d" "\\begin{definition}%a%U" "\\end{definition}")
242 ("example" "e" "\\begin{example}%a%U" "\\end{example}")
243 ("exampleblock" "E" "\\begin{exampleblock}%a{%h}" "\\end{exampleblock}")
244 ("proof" "p" "\\begin{proof}%a%U" "\\end{proof}")
245 ("beamercolorbox" "o" "\\begin{beamercolorbox}%o{%h}" "\\end{beamercolorbox}"))
246 "Environments triggered by properties in Beamer export.
247 These are the defaults - for user definitions, see
248 `org-beamer-environments-extra'.")
250 (defconst org-beamer-verbatim-elements
251 '(code example-block fixed-width inline-src-block src-block verbatim
)
252 "List of element or object types producing verbatim text.
253 This is used internally to determine when a frame should have the
254 \"fragile\" option.")
258 ;;; Internal functions
260 (defun org-beamer--normalize-argument (argument type
)
261 "Return ARGUMENT string with proper boundaries.
263 TYPE is a symbol among the following:
264 `action' Return ARGUMENT within angular brackets.
265 `defaction' Return ARGUMENT within both square and angular brackets.
266 `option' Return ARGUMENT within square brackets."
267 (if (not (string-match "\\S-" argument
)) ""
269 (action (if (string-match "\\`<.*>\\'" argument
) argument
270 (format "<%s>" argument
)))
272 ((string-match "\\`\\[<.*>\\]\\'" argument
) argument
)
273 ((string-match "\\`<.*>\\'" argument
)
274 (format "[%s]" argument
))
275 ((string-match "\\`\\[\\(.*\\)\\]\\'" argument
)
276 (format "[<%s>]" (match-string 1 argument
)))
277 (t (format "[<%s>]" argument
))))
278 (option (if (string-match "\\`\\[.*\\]\\'" argument
) argument
279 (format "[%s]" argument
)))
280 (otherwise argument
))))
282 (defun org-beamer--element-has-overlay-p (element)
283 "Non-nil when ELEMENT has an overlay specified.
284 An element has an overlay specification when it starts with an
285 `beamer' export-snippet whose value is between angular brackets.
286 Return overlay specification, as a string, or nil."
287 (let ((first-object (car (org-element-contents element
))))
288 (when (eq (org-element-type first-object
) 'export-snippet
)
289 (let ((value (org-element-property :value first-object
)))
290 (and (string-match "\\`<.*>\\'" value
) value
)))))
296 (org-export-define-derived-backend beamer latex
297 :export-block
"BEAMER"
300 ((?B
"As LaTeX buffer (Beamer)" org-beamer-export-as-latex
)
301 (?b
"As LaTeX file (Beamer)" org-beamer-export-to-latex
)
302 (?P
"As PDF file (Beamer)" org-beamer-export-to-pdf
)
303 (?O
"As PDF file and open (Beamer)"
305 (if a
(org-beamer-export-to-pdf t s v b
)
306 (org-open-file (org-beamer-export-to-pdf nil s v b
)))))))
308 ((:beamer-theme
"BEAMER_THEME" nil org-beamer-theme
)
309 (:beamer-color-theme
"BEAMER_COLOR_THEME" nil nil t
)
310 (:beamer-font-theme
"BEAMER_FONT_THEME" nil nil t
)
311 (:beamer-inner-theme
"BEAMER_INNER_THEME" nil nil t
)
312 (:beamer-outer-theme
"BEAMER_OUTER_THEME" nil nil t
)
313 (:beamer-header-extra
"BEAMER_HEADER" nil nil newline
)
314 (:headline-levels nil
"H" org-beamer-frame-level
))
315 :translate-alist
((bold . org-beamer-bold
)
316 (export-block . org-beamer-export-block
)
317 (export-snippet . org-beamer-export-snippet
)
318 (headline . org-beamer-headline
)
319 (item . org-beamer-item
)
320 (keyword . org-beamer-keyword
)
321 (link . org-beamer-link
)
322 (plain-list . org-beamer-plain-list
)
323 (radio-target . org-beamer-radio-target
)
324 (target . org-beamer-target
)
325 (template . org-beamer-template
)))
329 ;;; Transcode Functions
333 (defun org-beamer-bold (bold contents info
)
334 "Transcode BLOCK object into Beamer code.
335 CONTENTS is the text being bold. INFO is a plist used as
336 a communication channel."
337 (format "\\alert%s{%s}"
338 (or (org-beamer--element-has-overlay-p bold
) "")
344 (defun org-beamer-export-block (export-block contents info
)
345 "Transcode an EXPORT-BLOCK element into Beamer code.
346 CONTENTS is nil. INFO is a plist used as a communication
348 (when (member (org-element-property :type export-block
) '("BEAMER" "LATEX"))
349 (org-remove-indentation (org-element-property :value export-block
))))
354 (defun org-beamer-export-snippet (export-snippet contents info
)
355 "Transcode an EXPORT-SNIPPET object into Beamer code.
356 CONTENTS is nil. INFO is a plist used as a communication
358 (let ((backend (org-export-snippet-backend export-snippet
))
359 (value (org-element-property :value export-snippet
)))
360 ;; Only "latex" and "beamer" snippets are retained.
361 (cond ((eq backend
'latex
) value
)
362 ;; Ignore "beamer" snippets specifying overlays.
363 ((and (eq backend
'beamer
)
364 (or (org-export-get-previous-element export-snippet info
)
365 (not (string-match "\\`<.*>\\'" value
))))
371 ;; The main function to translate a headline is
372 ;; `org-beamer-headline'.
374 ;; Depending on the level at which a headline is considered as
375 ;; a frame (given by `org-beamer--frame-level'), the headline is
376 ;; either a section (`org-beamer--format-section'), a frame
377 ;; (`org-beamer--format-frame') or a block
378 ;; (`org-beamer--format-block').
380 ;; `org-beamer-headline' also takes care of special environments
381 ;; like "ignoreheading", "note", "noteNH", "appendix" and
384 (defun org-beamer--get-label (headline info
)
385 "Return label for HEADLINE, as a string.
387 INFO is a plist used as a communication channel.
389 The value is either the label specified in \"BEAMER_opt\"
390 property, or a fallback value built from headline's number. This
391 function assumes HEADLINE will be treated as a frame."
392 (let ((opt (org-element-property :BEAMER_OPT headline
)))
393 (if (and (org-string-nw-p opt
)
394 (string-match "\\(?:^\\|,\\)label=\\(.*?\\)\\(?:$\\|,\\)" opt
))
397 (mapconcat 'number-to-string
398 (org-export-get-headline-number headline info
)
401 (defun org-beamer--frame-level (headline info
)
402 "Return frame level in subtree containing HEADLINE.
403 INFO is a plist used as a communication channel."
405 ;; 1. Look for "frame" environment in parents, starting from the
408 (mapc (lambda (parent)
409 (let ((env (org-element-property :BEAMER_ENV parent
)))
410 (when (and env
(member (downcase env
) '("frame" "fullframe")))
411 (throw 'exit
(org-export-get-relative-level parent info
)))))
412 (nreverse (org-export-get-genealogy headline
)))
414 ;; 2. Look for "frame" environment in HEADLINE.
415 (let ((env (org-element-property :BEAMER_ENV headline
)))
416 (and env
(member (downcase env
) '("frame" "fullframe"))
417 (org-export-get-relative-level headline info
)))
418 ;; 3. Look for "frame" environment in sub-tree.
419 (org-element-map headline
'headline
421 (let ((env (org-element-property :BEAMER_ENV hl
)))
422 (when (and env
(member (downcase env
) '("frame" "fullframe")))
423 (org-export-get-relative-level hl info
))))
425 ;; 4. No "frame" environment in tree: use default value.
426 (plist-get info
:headline-levels
)))
428 (defun org-beamer--format-section (headline contents info
)
429 "Format HEADLINE as a sectioning part.
430 CONTENTS holds the contents of the headline. INFO is a plist
431 used as a communication channel."
432 ;; Use `latex' back-end output, inserting overlay specifications
434 (let ((latex-headline (org-export-with-backend 'latex headline contents info
))
435 (mode-specs (org-element-property :BEAMER_ACT headline
)))
437 (string-match "\\`\\\\\\(.*?\\)\\(?:\\*\\|\\[.*\\]\\)?{"
439 (replace-match (concat (match-string 1 latex-headline
)
440 (format "<%s>" mode-specs
))
441 nil nil latex-headline
1)
444 (defun org-beamer--format-frame (headline contents info
)
445 "Format HEADLINE as a frame.
446 CONTENTS holds the contents of the headline. INFO is a plist
447 used as a communication channel."
449 ;; FRAGILEP is non-nil when HEADLINE contains an element
450 ;; among `org-beamer-verbatim-elements'.
451 (org-element-map headline org-beamer-verbatim-elements
'identity
453 (concat "\\begin{frame}"
454 ;; Overlay specification, if any. When surrounded by
455 ;; square brackets, consider it as a default
457 (let ((action (org-element-property :BEAMER_ACT headline
)))
460 ((string-match "\\`\\[.*\\]\\'" action
)
461 (org-beamer--normalize-argument action
'defaction
))
462 (t (org-beamer--normalize-argument action
'action
))))
464 (let* ((beamer-opt (org-element-property :BEAMER_OPT headline
))
466 ;; Collect options from default value and headline's
467 ;; properties. Also add a label for links.
469 (org-split-string org-beamer-frame-default-options
",")
472 ;; Remove square brackets if user provided
474 (and (string-match "^\\[?\\(.*\\)\\]?$" beamer-opt
)
475 (match-string 1 beamer-opt
))
477 ;; Provide an automatic label for the frame
478 ;; unless the user specified one.
479 (unless (and beamer-opt
480 (string-match "\\(^\\|,\\)label=" beamer-opt
))
483 (org-beamer--get-label headline info
)))))))
484 ;; Change options list into a string.
485 (org-beamer--normalize-argument
488 (if (or (not fragilep
) (member "fragile" options
)) options
489 (cons "fragile" options
))
493 (let ((env (org-element-property :BEAMER_ENV headline
)))
495 (if (and env
(equal (downcase env
) "fullframe")) ""
497 (org-element-property :title headline
) info
))))
499 ;; The following workaround is required in fragile frames
500 ;; as Beamer will append "\par" to the beginning of the
501 ;; contents. So we need to make sure the command is
502 ;; separated from the contents by at least one space. If
503 ;; it isn't, it will create "\parfirst-word" command and
504 ;; remove the first word from the contents in the PDF
506 (if (not fragilep
) contents
507 (replace-regexp-in-string "\\`\n*" "\\& " contents
))
510 (defun org-beamer--format-block (headline contents info
)
511 "Format HEADLINE as a block.
512 CONTENTS holds the contents of the headline. INFO is a plist
513 used as a communication channel."
514 (let* ((column-width (org-element-property :BEAMER_COL headline
))
515 ;; ENVIRONMENT defaults to "block" if none is specified and
516 ;; there is no column specification. If there is a column
517 ;; specified but still no explicit environment, ENVIRONMENT
519 (environment (let ((env (org-element-property :BEAMER_ENV headline
)))
521 ;; "block" is the fallback environment.
522 ((and (not env
) (not column-width
)) "block")
525 ;; Use specified environment.
526 (t (downcase env
)))))
527 (env-format (unless (member environment
'("column" "columns"))
529 (append org-beamer-environments-special
530 org-beamer-environments-extra
531 org-beamer-environments-default
))))
532 (title (org-export-data (org-element-property :title headline
) info
))
533 (options (let ((options (org-element-property :BEAMER_OPT headline
)))
535 (org-beamer--normalize-argument options
'option
))))
536 ;; Start a "columns" environment when explicitly requested or
537 ;; when there is no previous headline or the previous
538 ;; headline do not have a BEAMER_column property.
539 (parent-env (org-element-property
540 :BEAMER_ENV
(org-export-get-parent-headline headline
)))
542 (or (equal environment
"columns")
545 (equal (downcase parent-env
) "columns")))
546 (or (org-export-first-sibling-p headline info
)
547 (not (org-element-property
549 (org-export-get-previous-element
551 ;; End the "columns" environment when explicitly requested or
552 ;; when there is no next headline or the next headline do not
553 ;; have a BEAMER_column property.
555 (or (equal environment
"columns")
558 (equal (downcase parent-env
) "columns")))
559 (or (org-export-last-sibling-p headline info
)
560 (not (org-element-property
562 (org-export-get-next-element headline info
))))))))
564 (when start-columns-p
565 ;; Column can accept options only when the environment is
566 ;; explicitly defined.
567 (if (not (equal environment
"columns")) "\\begin{columns}\n"
568 (format "\\begin{columns}%s\n" options
)))
570 (format "\\begin{column}%s{%s}\n"
571 ;; One can specify placement for column only when
572 ;; HEADLINE stands for a column on its own.
573 (if (equal environment
"column") options
"")
574 (format "%s\\textwidth" column-width
)))
575 ;; Block's opening string.
581 ;; If BEAMER_act property has its value enclosed in square
582 ;; brackets, it is a default overlay specification and
583 ;; overlay specification is empty. Otherwise, it is an
584 ;; overlay specification and the default one is nil.
585 (let ((action (org-element-property :BEAMER_ACT headline
)))
587 ((not action
) (list (cons "a" "") (cons "A" "")))
588 ((string-match "\\`\\[.*\\]\\'" action
)
590 (cons "A" (org-beamer--normalize-argument action
'defaction
))
593 (list (cons "a" (org-beamer--normalize-argument action
'action
))
595 (list (cons "o" options
)
597 (cons "H" (if (equal title
"") "" (format "{%s}" title
)))
598 (cons "U" (if (equal title
"") "" (format "[%s]" title
))))))
601 ;; Block's closing string.
602 (when environment
(concat (nth 3 env-format
) "\n"))
603 (when column-width
"\\end{column}\n")
604 (when end-columns-p
"\\end{columns}"))))
606 (defun org-beamer-headline (headline contents info
)
607 "Transcode HEADLINE element into Beamer code.
608 CONTENTS is the contents of the headline. INFO is a plist used
609 as a communication channel."
610 (unless (org-element-property :footnote-section-p headline
)
611 (let ((level (org-export-get-relative-level headline info
))
612 (frame-level (org-beamer--frame-level headline info
))
613 (environment (let ((env (org-element-property :BEAMER_ENV headline
)))
614 (if (stringp env
) (downcase env
) "block"))))
616 ;; Case 1: Resume frame specified by "BEAMER_ref" property.
617 ((equal environment
"againframe")
618 (let ((ref (org-element-property :BEAMER_REF headline
)))
619 ;; Reference to frame being resumed is mandatory. Ignore
620 ;; the whole headline if it isn't provided.
621 (when (org-string-nw-p ref
)
622 (concat "\\againframe"
623 ;; Overlay specification.
624 (let ((overlay (org-element-property :BEAMER_ACT headline
)))
626 (org-beamer--normalize-argument
628 (if (string-match "^\\[.*\\]$" overlay
) 'defaction
631 (let ((options (org-element-property :BEAMER_OPT headline
)))
633 (org-beamer--normalize-argument options
'option
)))
634 ;; Resolve reference provided by "BEAMER_ref"
635 ;; property. This is done by building a minimal fake
636 ;; link and calling the appropriate resolve function,
637 ;; depending on the reference syntax.
640 (string-match "^\\(id:\\|#\\|\\*\\)?\\(.*\\)" ref
)
642 ((or (not (match-string 1 ref
))
643 (equal (match-string 1 ref
) "*")) 'fuzzy
)
644 ((equal (match-string 1 ref
) "id:") 'id
)
646 (link (list 'link
(list :path
(match-string 2 ref
))))
647 (target (if (eq type
'fuzzy
)
648 (org-export-resolve-fuzzy-link link info
)
649 (org-export-resolve-id-link link info
))))
650 ;; Now use user-defined label provided in TARGET
651 ;; headline, or fallback to standard one.
652 (format "{%s}" (org-beamer--get-label target info
)))))))
653 ;; Case 2: Creation of an appendix is requested.
654 ((equal environment
"appendix")
656 (org-element-property :BEAMER_ACT headline
)
658 (make-string (org-element-property :pre-blank headline
) ?
\n)
660 ;; Case 3: Ignore heading.
661 ((equal environment
"ignoreheading")
662 (concat (make-string (org-element-property :pre-blank headline
) ?
\n)
664 ;; Case 4: HEADLINE is a note.
665 ((member environment
'("note" "noteNH"))
667 (concat (and (equal environment
"note")
670 (org-element-property :title headline
) info
)
672 (org-trim contents
))))
673 ;; Case 5: HEADLINE is a frame.
674 ((= level frame-level
)
675 (org-beamer--format-frame headline contents info
))
676 ;; Case 6: Regular section, extracted from
677 ;; `org-latex-classes'.
678 ((< level frame-level
)
679 (org-beamer--format-section headline contents info
))
680 ;; Case 7: Otherwise, HEADLINE is a block.
681 (t (org-beamer--format-block headline contents info
))))))
686 (defun org-beamer-item (item contents info
)
687 "Transcode an ITEM element into Beamer code.
688 CONTENTS holds the contents of the item. INFO is a plist holding
689 contextual information."
690 (let ((action (let ((first-element (car (org-element-contents item
))))
691 (and (eq (org-element-type first-element
) 'paragraph
)
692 (org-beamer--element-has-overlay-p first-element
))))
693 (output (org-export-with-backend 'latex item contents info
)))
694 (if (not action
) output
695 ;; If the item starts with a paragraph and that paragraph starts
696 ;; with an export snippet specifying an overlay, insert it after
698 (replace-regexp-in-string "\\\\item" (concat "\\\\item" action
) output
))))
703 (defun org-beamer-keyword (keyword contents info
)
704 "Transcode a KEYWORD element into Beamer code.
705 CONTENTS is nil. INFO is a plist used as a communication
707 (let ((key (org-element-property :key keyword
))
708 (value (org-element-property :value keyword
)))
709 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
710 ;; Otherwise, fallback to `latex' back-end.
712 ((equal key
"BEAMER") value
)
713 ((and (equal key
"TOC") (string-match "\\<headlines\\>" value
))
714 (let ((depth (or (and (string-match "[0-9]+" value
)
715 (string-to-number (match-string 0 value
)))
716 (plist-get info
:with-toc
)))
717 (options (and (string-match "\\[.*?\\]" value
)
718 (match-string 0 value
))))
720 (when (wholenump depth
) (format "\\setcounter{tocdepth}{%s}\n" depth
))
721 "\\tableofcontents" options
)))
722 (t (org-export-with-backend 'latex keyword contents info
)))))
727 (defun org-beamer-link (link contents info
)
728 "Transcode a LINK object into Beamer code.
729 CONTENTS is the description part of the link. INFO is a plist
730 used as a communication channel."
731 (let ((type (org-element-property :type link
))
732 (path (org-element-property :path link
)))
733 ;; Use \hyperlink command for all internal links.
735 ((equal type
"radio")
736 (let ((destination (org-export-resolve-radio-link link info
)))
738 (format "\\hyperlink%s{%s}{%s}"
739 (or (org-beamer--element-has-overlay-p link
) "")
740 (org-export-solidify-link-text path
)
741 (org-export-data (org-element-contents destination
) info
)))))
742 ((and (member type
'("custom-id" "fuzzy" "id"))
743 (let ((destination (if (string= type
"fuzzy")
744 (org-export-resolve-fuzzy-link link info
)
745 (org-export-resolve-id-link link info
))))
746 (case (org-element-type destination
)
752 (org-export-get-headline-number
755 (if (and (plist-get info
:section-numbers
) (not contents
))
756 (format "\\ref{%s}" label
)
757 (format "\\hyperlink%s{%s}{%s}"
758 (or (org-beamer--element-has-overlay-p link
) "")
762 (let ((path (org-export-solidify-link-text path
)))
763 (if (not contents
) (format "\\ref{%s}" path
)
764 (format "\\hyperlink%s{%s}{%s}"
765 (or (org-beamer--element-has-overlay-p link
) "")
768 ;; Otherwise, use `latex' back-end.
769 (t (org-export-with-backend 'latex link contents info
)))))
774 ;; Plain lists support `:environment', `:overlay' and `:options'
777 (defun org-beamer-plain-list (plain-list contents info
)
778 "Transcode a PLAIN-LIST element into Beamer code.
779 CONTENTS is the contents of the list. INFO is a plist holding
780 contextual information."
781 (let* ((type (org-element-property :type plain-list
))
782 (attributes (org-combine-plists
783 (org-export-read-attribute :attr_latex plain-list
)
784 (org-export-read-attribute :attr_beamer plain-list
)))
785 (latex-type (let ((env (plist-get attributes
:environment
)))
786 (cond (env (format "%s" env
))
787 ((eq type
'ordered
) "enumerate")
788 ((eq type
'descriptive
) "description")
790 (org-latex--wrap-label
792 (format "\\begin{%s}%s%s\n%s\\end{%s}"
794 ;; Default overlay specification, if any.
795 (org-beamer--normalize-argument
796 (format "%s" (or (plist-get attributes
:overlay
) ""))
798 ;; Second optional argument depends on the list type.
799 (org-beamer--normalize-argument
800 (format "%s" (or (plist-get attributes
:options
) ""))
802 ;; Eventually insert contents and close environment.
809 (defun org-beamer-radio-target (radio-target text info
)
810 "Transcode a RADIO-TARGET object into Beamer code.
811 TEXT is the text of the target. INFO is a plist holding
812 contextual information."
813 (format "\\hypertarget%s{%s}{%s}"
814 (or (org-beamer--element-has-overlay-p radio-target
) "")
815 (org-export-solidify-link-text
816 (org-element-property :value radio-target
))
822 (defun org-beamer-target (target contents info
)
823 "Transcode a TARGET object into Beamer code.
824 CONTENTS is nil. INFO is a plist holding contextual
826 (format "\\hypertarget{%s}{}"
827 (org-export-solidify-link-text (org-element-property :value target
))))
832 ;; Template used is similar to the one used in `latex' back-end,
833 ;; excepted for the table of contents and Beamer themes.
835 (defun org-beamer-template (contents info
)
836 "Return complete document string after Beamer conversion.
837 CONTENTS is the transcoded contents string. INFO is a plist
838 holding export options."
839 (let ((title (org-export-data (plist-get info
:title
) info
)))
842 (and (plist-get info
:time-stamp-file
)
843 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
844 ;; 2. Document class and packages.
845 (let ((class (plist-get info
:latex-class
))
846 (class-options (plist-get info
:latex-class-options
)))
847 (org-element-normalize-string
848 (let* ((header (nth 1 (assoc class org-latex-classes
)))
849 (document-class-string
850 (and (stringp header
)
851 (if (not class-options
) header
852 (replace-regexp-in-string
853 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
854 class-options header t nil
1)))))
855 (if (not document-class-string
)
856 (user-error "Unknown LaTeX class `%s'" class
)
857 (org-latex-guess-babel-language
858 (org-latex-guess-inputenc
859 (org-splice-latex-header
860 document-class-string
861 org-latex-default-packages-alist
862 org-latex-packages-alist nil
863 (concat (plist-get info
:latex-header
)
864 (plist-get info
:latex-header-extra
)
865 (plist-get info
:beamer-header-extra
))))
870 (lambda (prop command
)
871 (let ((theme (plist-get info prop
)))
874 (if (not (string-match "\\[.*\\]" theme
))
875 (format "{%s}\n" theme
)
877 (match-string 0 theme
)
879 (replace-match "" nil nil theme
)))))))))))
880 (mapconcat (lambda (args) (apply format-theme args
))
881 '((:beamer-theme
"\\usetheme")
882 (:beamer-color-theme
"\\usecolortheme")
883 (:beamer-font-theme
"\\usefonttheme")
884 (:beamer-inner-theme
"\\useinnertheme")
885 (:beamer-outer-theme
"\\useoutertheme"))
887 ;; 4. Possibly limit depth for headline numbering.
888 (let ((sec-num (plist-get info
:section-numbers
)))
889 (when (integerp sec-num
)
890 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
892 (let ((author (and (plist-get info
:with-author
)
893 (let ((auth (plist-get info
:author
)))
894 (and auth
(org-export-data auth info
)))))
895 (email (and (plist-get info
:with-email
)
896 (org-export-data (plist-get info
:email
) info
))))
897 (cond ((and author email
(not (string= "" email
)))
898 (format "\\author{%s\\thanks{%s}}\n" author email
))
899 (author (format "\\author{%s}\n" author
))
902 (let ((date (and (plist-get info
:with-date
) (plist-get info
:date
))))
903 (format "\\date{%s}\n"
904 (cond ((not date
) "")
905 ;; If `:date' consists in a single timestamp and
906 ;; `:date-format' is provided, apply it.
907 ((and (plist-get info
:date-format
)
909 (eq (org-element-type (car date
)) 'timestamp
))
910 (org-timestamp-format
911 (car date
) (plist-get info
:date-format
)))
912 (t (org-export-data date info
)))))
914 (format "\\title{%s}\n" title
)
915 ;; 8. Hyperref options.
916 (when (plist-get info
:latex-hyperref-p
)
917 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
918 (or (plist-get info
:keywords
) "")
919 (or (plist-get info
:description
) "")
920 (if (not (plist-get info
:with-creator
)) ""
921 (plist-get info
:creator
))))
922 ;; 9. Document start.
923 "\\begin{document}\n\n"
924 ;; 10. Title command.
925 (org-element-normalize-string
926 (cond ((string= "" title
) nil
)
927 ((not (stringp org-latex-title-command
)) nil
)
928 ((string-match "\\(?:[^%]\\|^\\)%s"
929 org-latex-title-command
)
930 (format org-latex-title-command title
))
931 (t org-latex-title-command
)))
932 ;; 11. Table of contents.
933 (let ((depth (plist-get info
:with-toc
)))
936 (format "\\begin{frame}%s{%s}\n"
937 (org-beamer--normalize-argument
938 org-beamer-outline-frame-options
'option
)
939 org-beamer-outline-frame-title
)
940 (when (wholenump depth
)
941 (format "\\setcounter{tocdepth}{%d}\n" depth
))
942 "\\tableofcontents\n"
943 "\\end{frame}\n\n")))
944 ;; 12. Document's body.
947 (let ((creator-info (plist-get info
:with-creator
)))
949 ((not creator-info
) "")
950 ((eq creator-info
'comment
)
951 (format "%% %s\n" (plist-get info
:creator
)))
952 (t (concat (plist-get info
:creator
) "\n"))))
961 (defvar org-beamer-mode-map
(make-sparse-keymap)
962 "The keymap for `org-beamer-mode'.")
963 (define-key org-beamer-mode-map
"\C-c\C-b" 'org-beamer-select-environment
)
966 (define-minor-mode org-beamer-mode
967 "Support for editing Beamer oriented Org mode files."
968 nil
" Bm" 'org-beamer-mode-map
)
970 (when (fboundp 'font-lock-add-keywords
)
971 (font-lock-add-keywords
973 '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-beamer-tag prepend
))
976 (defface org-beamer-tag
'((t (:box
(:line-width
1 :color grey40
))))
977 "The special face for beamer tags."
978 :group
'org-export-beamer
)
980 (defun org-beamer-property-changed (property value
)
981 "Track the BEAMER_env property with tags.
982 PROPERTY is the name of the modified property. VALUE is its new
985 ((equal property
"BEAMER_env")
987 (org-back-to-heading t
)
988 ;; Filter out Beamer-related tags and install environment tag.
989 (let ((tags (org-remove-if (lambda (x) (string-match "^B_" x
))
991 (env-tag (and (org-string-nw-p value
) (concat "B_" value
))))
992 (org-set-tags-to (if env-tag
(cons env-tag tags
) tags
))
993 (when env-tag
(org-toggle-tag env-tag
'on
)))))
994 ((equal property
"BEAMER_col")
995 (org-toggle-tag "BMCOL" (if (org-string-nw-p value
) 'on
'off
)))))
997 (add-hook 'org-property-changed-functions
'org-beamer-property-changed
)
999 (defun org-beamer-allowed-property-values (property)
1000 "Supply allowed values for PROPERTY."
1002 ((and (equal property
"BEAMER_env")
1003 (not (org-entry-get nil
(concat property
"_ALL") 'inherit
)))
1004 ;; If no allowed values for BEAMER_env have been defined,
1005 ;; supply all defined environments
1006 (mapcar 'car
(append org-beamer-environments-special
1007 org-beamer-environments-extra
1008 org-beamer-environments-default
)))
1009 ((and (equal property
"BEAMER_col")
1010 (not (org-entry-get nil
(concat property
"_ALL") 'inherit
)))
1011 ;; If no allowed values for BEAMER_col have been defined,
1013 (org-split-string org-beamer-column-widths
" "))))
1015 (add-hook 'org-property-allowed-value-functions
1016 'org-beamer-allowed-property-values
)
1023 (defun org-beamer-export-as-latex
1024 (&optional async subtreep visible-only body-only ext-plist
)
1025 "Export current buffer as a Beamer buffer.
1027 If narrowing is active in the current buffer, only export its
1030 If a region is active, export that region.
1032 A non-nil optional argument ASYNC means the process should happen
1033 asynchronously. The resulting buffer should be accessible
1034 through the `org-export-stack' interface.
1036 When optional argument SUBTREEP is non-nil, export the sub-tree
1037 at point, extracting information from the headline properties
1040 When optional argument VISIBLE-ONLY is non-nil, don't export
1041 contents of hidden elements.
1043 When optional argument BODY-ONLY is non-nil, only write code
1044 between \"\\begin{document}\" and \"\\end{document}\".
1046 EXT-PLIST, when provided, is a property list with external
1047 parameters overriding Org default settings, but still inferior to
1048 file-local settings.
1050 Export is done in a buffer named \"*Org BEAMER Export*\", which
1051 will be displayed when `org-export-show-temporary-export-buffer'
1055 (org-export-async-start
1057 (with-current-buffer (get-buffer-create "*Org BEAMER Export*")
1060 (goto-char (point-min))
1062 (org-export-add-to-stack (current-buffer) 'beamer
)))
1063 `(org-export-as 'beamer
,subtreep
,visible-only
,body-only
1065 (let ((outbuf (org-export-to-buffer
1066 'beamer
"*Org BEAMER Export*"
1067 subtreep visible-only body-only ext-plist
)))
1068 (with-current-buffer outbuf
(LaTeX-mode))
1069 (when org-export-show-temporary-export-buffer
1070 (switch-to-buffer-other-window outbuf
)))))
1073 (defun org-beamer-export-to-latex
1074 (&optional async subtreep visible-only body-only ext-plist
)
1075 "Export current buffer as a Beamer presentation (tex).
1077 If narrowing is active in the current buffer, only export its
1080 If a region is active, export that region.
1082 A non-nil optional argument ASYNC means the process should happen
1083 asynchronously. The resulting file should be accessible through
1084 the `org-export-stack' interface.
1086 When optional argument SUBTREEP is non-nil, export the sub-tree
1087 at point, extracting information from the headline properties
1090 When optional argument VISIBLE-ONLY is non-nil, don't export
1091 contents of hidden elements.
1093 When optional argument BODY-ONLY is non-nil, only write code
1094 between \"\\begin{document}\" and \"\\end{document}\".
1096 EXT-PLIST, when provided, is a property list with external
1097 parameters overriding Org default settings, but still inferior to
1098 file-local settings.
1100 Return output file's name."
1102 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
1104 (org-export-async-start
1105 (lambda (f) (org-export-add-to-stack f
'beamer
))
1108 'beamer
,outfile
,subtreep
,visible-only
,body-only
1111 'beamer outfile subtreep visible-only body-only ext-plist
))))
1114 (defun org-beamer-export-to-pdf
1115 (&optional async subtreep visible-only body-only ext-plist
)
1116 "Export current buffer as a Beamer presentation (PDF).
1118 If narrowing is active in the current buffer, only export its
1121 If a region is active, export that region.
1123 A non-nil optional argument ASYNC means the process should happen
1124 asynchronously. The resulting file should be accessible through
1125 the `org-export-stack' interface.
1127 When optional argument SUBTREEP is non-nil, export the sub-tree
1128 at point, extracting information from the headline properties
1131 When optional argument VISIBLE-ONLY is non-nil, don't export
1132 contents of hidden elements.
1134 When optional argument BODY-ONLY is non-nil, only write code
1135 between \"\\begin{document}\" and \"\\end{document}\".
1137 EXT-PLIST, when provided, is a property list with external
1138 parameters overriding Org default settings, but still inferior to
1139 file-local settings.
1141 Return PDF file's name."
1144 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
1145 (org-export-async-start
1146 (lambda (f) (org-export-add-to-stack f
'beamer
))
1150 'beamer
,outfile
,subtreep
,visible-only
,body-only
1153 (org-beamer-export-to-latex
1154 nil subtreep visible-only body-only ext-plist
))))
1157 (defun org-beamer-select-environment ()
1158 "Select the environment to be used by beamer for this entry.
1159 While this uses (for convenience) a tag selection interface, the
1160 result of this command will be that the BEAMER_env *property* of
1163 In addition to this, the command will also set a tag as a visual
1164 aid, but the tag does not have any semantic meaning."
1166 ;; Make sure `org-beamer-environments-special' has a higher
1167 ;; priority than `org-beamer-environments-extra'.
1168 (let* ((envs (append org-beamer-environments-special
1169 org-beamer-environments-extra
1170 org-beamer-environments-default
))
1172 (append '((:startgroup
))
1173 (mapcar (lambda (e) (cons (concat "B_" (car e
))
1174 (string-to-char (nth 1 e
))))
1178 (org-fast-tag-selection-single-key t
))
1180 (let ((tags (or (ignore-errors (org-get-tags-string)) "")))
1182 ;; For a column, automatically ask for its width.
1183 ((eq org-last-tag-selection-key ?|
)
1184 (if (string-match ":BMCOL:" tags
)
1185 (org-set-property "BEAMER_col" (read-string "Column width: "))
1186 (org-delete-property "BEAMER_col")))
1187 ;; For an "againframe" section, automatically ask for reference
1188 ;; to resumed frame and overlay specifications.
1189 ((eq org-last-tag-selection-key ?A
)
1190 (if (equal (org-entry-get nil
"BEAMER_env") "againframe")
1191 (progn (org-entry-delete nil
"BEAMER_env")
1192 (org-entry-delete nil
"BEAMER_ref")
1193 (org-entry-delete nil
"BEAMER_act"))
1194 (org-entry-put nil
"BEAMER_env" "againframe")
1197 (read-string "Frame reference (*Title, #custom-id, id:...): "))
1198 (org-set-property "BEAMER_act"
1199 (read-string "Overlay specification: "))))
1200 ((string-match (concat ":B_\\(" (mapconcat 'car envs
"\\|") "\\):") tags
)
1201 (org-entry-put nil
"BEAMER_env" (match-string 1 tags
)))
1202 (t (org-entry-delete nil
"BEAMER_env"))))))
1205 (defun org-beamer-insert-options-template (&optional kind
)
1206 "Insert a settings template, to make sure users do this right."
1208 (message "Current [s]ubtree or [g]lobal?")
1209 (if (eq (read-char-exclusive) ?g
) (list 'global
)
1211 (if (eq kind
'subtree
)
1213 (org-back-to-heading t
)
1215 (org-entry-put nil
"EXPORT_LaTeX_CLASS" "beamer")
1216 (org-entry-put nil
"EXPORT_LaTeX_CLASS_OPTIONS" "[presentation]")
1217 (org-entry-put nil
"EXPORT_FILE_NAME" "presentation.pdf")
1218 (when org-beamer-column-view-format
1219 (org-entry-put nil
"COLUMNS" org-beamer-column-view-format
))
1220 (org-entry-put nil
"BEAMER_col_ALL" org-beamer-column-widths
))
1221 (insert "#+LaTeX_CLASS: beamer\n")
1222 (insert "#+LaTeX_CLASS_OPTIONS: [presentation]\n")
1223 (when org-beamer-theme
(insert "#+BEAMER_THEME: " org-beamer-theme
"\n"))
1224 (when org-beamer-column-view-format
1225 (insert "#+COLUMNS: " org-beamer-column-view-format
"\n"))
1226 (insert "#+PROPERTY: BEAMER_col_ALL " org-beamer-column-widths
"\n")))
1229 (defun org-beamer-publish-to-latex (plist filename pub-dir
)
1230 "Publish an Org file to a Beamer presentation (LaTeX).
1232 FILENAME is the filename of the Org file to be published. PLIST
1233 is the property list for the given project. PUB-DIR is the
1234 publishing directory.
1236 Return output file name."
1237 (org-publish-org-to 'beamer filename
".tex" plist pub-dir
))
1240 (defun org-beamer-publish-to-pdf (plist filename pub-dir
)
1241 "Publish an Org file to a Beamer presentation (PDF, via LaTeX).
1243 FILENAME is the filename of the Org file to be published. PLIST
1244 is the property list for the given project. PUB-DIR is the
1245 publishing directory.
1247 Return output file name."
1248 ;; Unlike to `org-beamer-publish-to-latex', PDF file is generated in
1249 ;; working directory and then moved to publishing directory.
1250 (org-publish-attachment
1252 (org-latex-compile (org-publish-org-to 'beamer filename
".tex" plist
))
1256 (provide 'ox-beamer
)
1259 ;; generated-autoload-file: "org-loaddefs.el"
1262 ;;; ox-beamer.el ends here