1 ;;; ox-beamer.el --- Beamer Back-End for Org Export Engine -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2007-2016 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 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This library implements both a Beamer back-end, derived from the
27 ;; LaTeX one and a minor mode easing structure edition of the
28 ;; document. See Org manual for more information.
32 (eval-when-compile (require 'cl
))
36 ;; Install a default set-up for Beamer export.
37 (unless (assoc "beamer" org-latex-classes
)
38 (add-to-list 'org-latex-classes
40 "\\documentclass[presentation]{beamer}"
41 ("\\section{%s}" .
"\\section*{%s}")
42 ("\\subsection{%s}" .
"\\subsection*{%s}")
43 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))))
47 ;;; User-Configurable Variables
49 (defgroup org-export-beamer nil
50 "Options specific for using the beamer class in LaTeX export."
55 (defcustom org-beamer-frame-level
1
56 "The level at which headlines become frames.
58 Headlines at a lower level will be translated into a sectioning
59 structure. At a higher level, they will be translated into
62 If a headline with a \"BEAMER_env\" property set to \"frame\" is
63 found within a tree, its level locally overrides this number.
65 This variable has no effect on headlines with the \"BEAMER_env\"
66 property set to either \"ignoreheading\", \"appendix\", or
67 \"note\", which will respectively, be invisible, become an
70 This integer is relative to the minimal level of a headline
71 within the parse tree, defined as 1."
72 :group
'org-export-beamer
75 (defcustom org-beamer-frame-default-options
""
76 "Default options string to use for frames.
77 For example, it could be set to \"allowframebreaks\"."
78 :group
'org-export-beamer
79 :type
'(string :tag
"[options]"))
81 (defcustom org-beamer-column-view-format
82 "%45ITEM %10BEAMER_env(Env) %10BEAMER_act(Act) %4BEAMER_col(Col) %8BEAMER_opt(Opt)"
83 "Column view format that should be used to fill the template."
84 :group
'org-export-beamer
86 :package-version
'(Org .
"8.0")
88 (const :tag
"Do not insert Beamer column view format" nil
)
89 (string :tag
"Beamer column view format")))
91 (defcustom org-beamer-theme
"default"
92 "Default theme used in Beamer presentations."
93 :group
'org-export-beamer
95 :package-version
'(Org .
"8.0")
97 (const :tag
"Do not insert a Beamer theme" nil
)
98 (string :tag
"Beamer theme")))
100 (defcustom org-beamer-environments-extra nil
101 "Environments triggered by tags in Beamer export.
102 Each entry has 4 elements:
104 name Name of the environment
105 key Selection key for `org-beamer-select-environment'
106 open The opening template for the environment, with the following escapes
107 %a the action/overlay specification
108 %A the default action/overlay specification
109 %R the raw BEAMER_act value
110 %o the options argument, with square brackets
111 %O the raw BEAMER_opt value
113 %r the raw headline text (i.e. without any processing)
114 %H if there is headline text, that raw text in {} braces
115 %U if there is headline text, that raw text in [] brackets
116 close The closing string of the environment."
117 :group
'org-export-beamer
119 :package-version
'(Org .
"8.1")
122 (string :tag
"Environment")
123 (string :tag
"Selection key")
124 (string :tag
"Begin")
125 (string :tag
"End"))))
127 (defcustom org-beamer-outline-frame-title
"Outline"
128 "Default title of a frame containing an outline."
129 :group
'org-export-beamer
130 :type
'(string :tag
"Outline frame title"))
132 (defcustom org-beamer-outline-frame-options
""
133 "Outline frame options appended after \\begin{frame}.
134 You might want to put e.g. \"allowframebreaks=0.9\" here."
135 :group
'org-export-beamer
136 :type
'(string :tag
"Outline frame options"))
139 (defcustom org-beamer-subtitle-format
"\\subtitle{%s}"
140 "Format string used for transcoded subtitle.
141 The format string should have at most one \"%s\"-expression,
142 which is replaced with the subtitle."
143 :group
'org-export-beamer
145 :package-version
'(Org .
"8.3")
146 :type
'(string :tag
"Format string"))
149 ;;; Internal Variables
151 (defconst org-beamer-column-widths
152 "0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.0 :ETC"
153 "The column widths that should be installed as allowed property values.")
155 (defconst org-beamer-environments-special
162 ("ignoreheading" "i")
165 "Alist of environments treated in a special way by the back-end.
166 Keys are environment names, as strings, values are bindings used
167 in `org-beamer-select-environment'. Environments listed here,
168 along with their binding, are hard coded and cannot be modified
169 through `org-beamer-environments-extra' variable.")
171 (defconst org-beamer-environments-default
172 '(("block" "b" "\\begin{block}%a{%h}" "\\end{block}")
173 ("alertblock" "a" "\\begin{alertblock}%a{%h}" "\\end{alertblock}")
174 ("verse" "v" "\\begin{verse}%a %% %h" "\\end{verse}")
175 ("quotation" "q" "\\begin{quotation}%a %% %h" "\\end{quotation}")
176 ("quote" "Q" "\\begin{quote}%a %% %h" "\\end{quote}")
177 ("structureenv" "s" "\\begin{structureenv}%a %% %h" "\\end{structureenv}")
178 ("theorem" "t" "\\begin{theorem}%a%U" "\\end{theorem}")
179 ("definition" "d" "\\begin{definition}%a%U" "\\end{definition}")
180 ("example" "e" "\\begin{example}%a%U" "\\end{example}")
181 ("exampleblock" "E" "\\begin{exampleblock}%a{%h}" "\\end{exampleblock}")
182 ("proof" "p" "\\begin{proof}%a%U" "\\end{proof}")
183 ("beamercolorbox" "o" "\\begin{beamercolorbox}%o{%h}" "\\end{beamercolorbox}"))
184 "Environments triggered by properties in Beamer export.
185 These are the defaults - for user definitions, see
186 `org-beamer-environments-extra'.")
188 (defconst org-beamer-verbatim-elements
189 '(code example-block fixed-width inline-src-block src-block verbatim
)
190 "List of element or object types producing verbatim text.
191 This is used internally to determine when a frame should have the
192 \"fragile\" option.")
196 ;;; Internal functions
198 (defun org-beamer--normalize-argument (argument type
)
199 "Return ARGUMENT string with proper boundaries.
201 TYPE is a symbol among the following:
202 `action' Return ARGUMENT within angular brackets.
203 `defaction' Return ARGUMENT within both square and angular brackets.
204 `option' Return ARGUMENT within square brackets."
205 (if (not (string-match "\\S-" argument
)) ""
207 (action (if (string-match "\\`<.*>\\'" argument
) argument
208 (format "<%s>" argument
)))
210 ((string-match "\\`\\[<.*>\\]\\'" argument
) argument
)
211 ((string-match "\\`<.*>\\'" argument
)
212 (format "[%s]" argument
))
213 ((string-match "\\`\\[\\(.*\\)\\]\\'" argument
)
214 (format "[<%s>]" (match-string 1 argument
)))
215 (t (format "[<%s>]" argument
))))
216 (option (if (string-match "\\`\\[.*\\]\\'" argument
) argument
217 (format "[%s]" argument
)))
218 (otherwise argument
))))
220 (defun org-beamer--element-has-overlay-p (element)
221 "Non-nil when ELEMENT has an overlay specified.
222 An element has an overlay specification when it starts with an
223 `beamer' export-snippet whose value is between angular brackets.
224 Return overlay specification, as a string, or nil."
225 (let ((first-object (car (org-element-contents element
))))
226 (when (eq (org-element-type first-object
) 'export-snippet
)
227 (let ((value (org-element-property :value first-object
)))
228 (and (string-match "\\`<.*>\\'" value
) value
)))))
234 (org-export-define-derived-backend 'beamer
'latex
237 ((?B
"As LaTeX buffer (Beamer)" org-beamer-export-as-latex
)
238 (?b
"As LaTeX file (Beamer)" org-beamer-export-to-latex
)
239 (?P
"As PDF file (Beamer)" org-beamer-export-to-pdf
)
240 (?O
"As PDF file and open (Beamer)"
242 (if a
(org-beamer-export-to-pdf t s v b
)
243 (org-open-file (org-beamer-export-to-pdf nil s v b
)))))))
245 '((:headline-levels nil
"H" org-beamer-frame-level
)
246 (:latex-class
"LATEX_CLASS" nil
"beamer" t
)
247 (:beamer-subtitle-format nil nil org-beamer-subtitle-format
)
248 (:beamer-column-view-format
"COLUMNS" nil org-beamer-column-view-format
)
249 (:beamer-theme
"BEAMER_THEME" nil org-beamer-theme
)
250 (:beamer-color-theme
"BEAMER_COLOR_THEME" nil nil t
)
251 (:beamer-font-theme
"BEAMER_FONT_THEME" nil nil t
)
252 (:beamer-inner-theme
"BEAMER_INNER_THEME" nil nil t
)
253 (:beamer-outer-theme
"BEAMER_OUTER_THEME" nil nil t
)
254 (:beamer-header
"BEAMER_HEADER" nil nil newline
)
255 (:beamer-environments-extra nil nil org-beamer-environments-extra
)
256 (:beamer-frame-default-options nil nil org-beamer-frame-default-options
)
257 (:beamer-outline-frame-options nil nil org-beamer-outline-frame-options
)
258 (:beamer-outline-frame-title nil nil org-beamer-outline-frame-title
))
259 :translate-alist
'((bold . org-beamer-bold
)
260 (export-block . org-beamer-export-block
)
261 (export-snippet . org-beamer-export-snippet
)
262 (headline . org-beamer-headline
)
263 (item . org-beamer-item
)
264 (keyword . org-beamer-keyword
)
265 (link . org-beamer-link
)
266 (plain-list . org-beamer-plain-list
)
267 (radio-target . org-beamer-radio-target
)
268 (target . org-beamer-target
)
269 (template . org-beamer-template
)))
273 ;;; Transcode Functions
277 (defun org-beamer-bold (bold contents _info
)
278 "Transcode BLOCK object into Beamer code.
279 CONTENTS is the text being bold. INFO is a plist used as
280 a communication channel."
281 (format "\\alert%s{%s}"
282 (or (org-beamer--element-has-overlay-p bold
) "")
288 (defun org-beamer-export-block (export-block _contents _info
)
289 "Transcode an EXPORT-BLOCK element into Beamer code.
290 CONTENTS is nil. INFO is a plist used as a communication
292 (when (member (org-element-property :type export-block
) '("BEAMER" "LATEX"))
293 (org-remove-indentation (org-element-property :value export-block
))))
298 (defun org-beamer-export-snippet (export-snippet _contents info
)
299 "Transcode an EXPORT-SNIPPET object into Beamer code.
300 CONTENTS is nil. INFO is a plist used as a communication
302 (let ((backend (org-export-snippet-backend export-snippet
))
303 (value (org-element-property :value export-snippet
)))
304 ;; Only "latex" and "beamer" snippets are retained.
305 (cond ((eq backend
'latex
) value
)
306 ;; Ignore "beamer" snippets specifying overlays.
307 ((and (eq backend
'beamer
)
308 (or (org-export-get-previous-element export-snippet info
)
309 (not (string-match "\\`<.*>\\'" value
))))
315 ;; The main function to translate a headline is
316 ;; `org-beamer-headline'.
318 ;; Depending on the level at which a headline is considered as
319 ;; a frame (given by `org-beamer--frame-level'), the headline is
320 ;; either a section (`org-beamer--format-section'), a frame
321 ;; (`org-beamer--format-frame') or a block
322 ;; (`org-beamer--format-block').
324 ;; `org-beamer-headline' also takes care of special environments
325 ;; like "ignoreheading", "note", "noteNH", "appendix" and
328 (defun org-beamer--get-label (headline info
)
329 "Return label for HEADLINE, as a string.
331 INFO is a plist used as a communication channel.
333 The value is either the label specified in \"BEAMER_opt\"
334 property, the custom ID, if there is one and
335 `:latex-prefer-user-labels' property has a non nil value, or
336 a unique internal label. This function assumes HEADLINE will be
339 ((let ((opt (org-element-property :BEAMER_OPT headline
)))
341 (string-match "\\(?:^\\|,\\)label=\\(.*?\\)\\(?:$\\|,\\)" opt
)
342 (let ((label (match-string 1 opt
)))
343 (if (string-match-p "\\`{.*}\\'" label
)
344 (substring label
1 -
1)
346 ((and (plist-get info
:latex-prefer-user-labels
)
347 (org-element-property :CUSTOM_ID headline
)))
348 (t (format "sec:%s" (org-export-get-reference headline info
)))))
350 (defun org-beamer--frame-level (headline info
)
351 "Return frame level in subtree containing HEADLINE.
352 INFO is a plist used as a communication channel."
354 ;; 1. Look for "frame" environment in parents, starting from the
357 (dolist (parent (nreverse (org-element-lineage headline
)))
358 (let ((env (org-element-property :BEAMER_ENV parent
)))
359 (when (and env
(member-ignore-case env
'("frame" "fullframe")))
360 (throw 'exit
(org-export-get-relative-level parent info
))))))
361 ;; 2. Look for "frame" environment in HEADLINE.
362 (let ((env (org-element-property :BEAMER_ENV headline
)))
363 (and env
(member-ignore-case env
'("frame" "fullframe"))
364 (org-export-get-relative-level headline info
)))
365 ;; 3. Look for "frame" environment in sub-tree.
366 (org-element-map headline
'headline
368 (let ((env (org-element-property :BEAMER_ENV hl
)))
369 (when (and env
(member-ignore-case env
'("frame" "fullframe")))
370 (org-export-get-relative-level hl info
))))
372 ;; 4. No "frame" environment in tree: use default value.
373 (plist-get info
:headline-levels
)))
375 (defun org-beamer--format-section (headline contents info
)
376 "Format HEADLINE as a sectioning part.
377 CONTENTS holds the contents of the headline. INFO is a plist
378 used as a communication channel."
379 (let ((latex-headline
380 (org-export-with-backend
381 ;; We create a temporary export back-end which behaves the
382 ;; same as current one, but adds "\protect" in front of the
383 ;; output of some objects.
384 (org-export-create-backend
387 (let ((protected-output
389 (lambda (object contents info
)
390 (let ((code (org-export-with-backend
391 'beamer object contents info
)))
392 (if (org-string-nw-p code
) (concat "\\protect" code
)
394 (mapcar #'(lambda (type) (cons type protected-output
))
395 '(bold footnote-reference italic strike-through timestamp
400 (mode-specs (org-element-property :BEAMER_ACT headline
)))
402 (string-match "\\`\\\\\\(.*?\\)\\(?:\\*\\|\\[.*\\]\\)?{"
404 ;; Insert overlay specifications.
405 (replace-match (concat (match-string 1 latex-headline
)
406 (format "<%s>" mode-specs
))
407 nil nil latex-headline
1)
410 (defun org-beamer--format-frame (headline contents info
)
411 "Format HEADLINE as a frame.
412 CONTENTS holds the contents of the headline. INFO is a plist
413 used as a communication channel."
415 ;; FRAGILEP is non-nil when HEADLINE contains an element
416 ;; among `org-beamer-verbatim-elements'.
417 (org-element-map headline org-beamer-verbatim-elements
'identity
419 (concat "\\begin{frame}"
420 ;; Overlay specification, if any. When surrounded by
421 ;; square brackets, consider it as a default
423 (let ((action (org-element-property :BEAMER_ACT headline
)))
426 ((string-match "\\`\\[.*\\]\\'" action
)
427 (org-beamer--normalize-argument action
'defaction
))
428 (t (org-beamer--normalize-argument action
'action
))))
430 (let* ((beamer-opt (org-element-property :BEAMER_OPT headline
))
432 ;; Collect options from default value and headline's
433 ;; properties. Also add a label for links.
436 (plist-get info
:beamer-frame-default-options
) ",")
439 ;; Remove square brackets if user provided
441 (and (string-match "^\\[?\\(.*\\)\\]?$" beamer-opt
)
442 (match-string 1 beamer-opt
))
444 ;; Provide an automatic label for the frame
445 ;; unless the user specified one. Also refrain
446 ;; from labeling `allowframebreaks' frames; this
447 ;; is not allowed by beamer.
448 (unless (and beamer-opt
449 (or (string-match "\\(^\\|,\\)label=" beamer-opt
)
450 (string-match "allowframebreaks" beamer-opt
)))
452 (let ((label (org-beamer--get-label headline info
)))
453 ;; Labels containing colons need to be
454 ;; wrapped within braces.
455 (format (if (org-string-match-p ":" label
)
459 ;; Change options list into a string.
460 (org-beamer--normalize-argument
463 (if (or (not fragilep
) (member "fragile" options
)) options
464 (cons "fragile" options
))
468 (let ((env (org-element-property :BEAMER_ENV headline
)))
470 (if (and env
(equal (downcase env
) "fullframe")) ""
472 (org-element-property :title headline
) info
))))
474 ;; The following workaround is required in fragile frames
475 ;; as Beamer will append "\par" to the beginning of the
476 ;; contents. So we need to make sure the command is
477 ;; separated from the contents by at least one space. If
478 ;; it isn't, it will create "\parfirst-word" command and
479 ;; remove the first word from the contents in the PDF
481 (if (not fragilep
) contents
482 (replace-regexp-in-string "\\`\n*" "\\& " (or contents
"")))
485 (defun org-beamer--format-block (headline contents info
)
486 "Format HEADLINE as a block.
487 CONTENTS holds the contents of the headline. INFO is a plist
488 used as a communication channel."
489 (let* ((column-width (org-element-property :BEAMER_COL headline
))
490 ;; ENVIRONMENT defaults to "block" if none is specified and
491 ;; there is no column specification. If there is a column
492 ;; specified but still no explicit environment, ENVIRONMENT
494 (environment (let ((env (org-element-property :BEAMER_ENV headline
)))
496 ;; "block" is the fallback environment.
497 ((and (not env
) (not column-width
)) "block")
500 ;; Use specified environment.
502 (raw-title (org-element-property :raw-value headline
))
504 (cond ((member environment
'("column" "columns")) nil
)
506 (append (plist-get info
:beamer-environments-extra
)
507 org-beamer-environments-default
)))
508 (t (user-error "Wrong block type at a headline named \"%s\""
510 (title (org-export-data (org-element-property :title headline
) info
))
511 (raw-options (org-element-property :BEAMER_OPT headline
))
512 (options (if raw-options
513 (org-beamer--normalize-argument raw-options
'option
)
515 ;; Start a "columns" environment when explicitly requested or
516 ;; when there is no previous headline or the previous
517 ;; headline do not have a BEAMER_column property.
518 (parent-env (org-element-property
519 :BEAMER_ENV
(org-export-get-parent-headline headline
)))
521 (or (equal environment
"columns")
524 (equal (downcase parent-env
) "columns")))
525 (or (org-export-first-sibling-p headline info
)
526 (not (org-element-property
528 (org-export-get-previous-element
530 ;; End the "columns" environment when explicitly requested or
531 ;; when there is no next headline or the next headline do not
532 ;; have a BEAMER_column property.
534 (or (equal environment
"columns")
537 (equal (downcase parent-env
) "columns")))
538 (or (org-export-last-sibling-p headline info
)
539 (not (org-element-property
541 (org-export-get-next-element headline info
))))))))
543 (when start-columns-p
544 ;; Column can accept options only when the environment is
545 ;; explicitly defined.
546 (if (not (equal environment
"columns")) "\\begin{columns}\n"
547 (format "\\begin{columns}%s\n" options
)))
549 (format "\\begin{column}%s{%s}\n"
550 ;; One can specify placement for column only when
551 ;; HEADLINE stands for a column on its own.
552 (if (equal environment
"column") options
"")
553 (format "%s\\columnwidth" column-width
)))
554 ;; Block's opening string.
555 (when (nth 2 env-format
)
560 ;; If BEAMER_act property has its value enclosed in square
561 ;; brackets, it is a default overlay specification and
562 ;; overlay specification is empty. Otherwise, it is an
563 ;; overlay specification and the default one is nil.
564 (let ((action (org-element-property :BEAMER_ACT headline
)))
566 ((not action
) (list (cons "a" "") (cons "A" "") (cons "R" "")))
567 ((string-match "\\`\\[.*\\]\\'" action
)
569 (cons "A" (org-beamer--normalize-argument action
'defaction
))
573 (list (cons "a" (org-beamer--normalize-argument action
'action
))
575 (cons "R" action
)))))
576 (list (cons "o" options
)
577 (cons "O" (or raw-options
""))
580 (cons "H" (if (equal raw-title
"") ""
581 (format "{%s}" raw-title
)))
582 (cons "U" (if (equal raw-title
"") ""
583 (format "[%s]" raw-title
))))))
586 ;; Block's closing string, if any.
587 (and (nth 3 env-format
) (concat (nth 3 env-format
) "\n"))
588 (when column-width
"\\end{column}\n")
589 (when end-columns-p
"\\end{columns}"))))
591 (defun org-beamer-headline (headline contents info
)
592 "Transcode HEADLINE element into Beamer code.
593 CONTENTS is the contents of the headline. INFO is a plist used
594 as a communication channel."
595 (unless (org-element-property :footnote-section-p headline
)
596 (let ((level (org-export-get-relative-level headline info
))
597 (frame-level (org-beamer--frame-level headline info
))
598 (environment (let ((env (org-element-property :BEAMER_ENV headline
)))
599 (or (org-string-nw-p env
) "block"))))
601 ;; Case 1: Resume frame specified by "BEAMER_ref" property.
602 ((equal environment
"againframe")
603 (let ((ref (org-element-property :BEAMER_REF headline
)))
604 ;; Reference to frame being resumed is mandatory. Ignore
605 ;; the whole headline if it isn't provided.
606 (when (org-string-nw-p ref
)
607 (concat "\\againframe"
608 ;; Overlay specification.
609 (let ((overlay (org-element-property :BEAMER_ACT headline
)))
611 (org-beamer--normalize-argument
613 (if (string-match "\\`\\[.*\\]\\'" overlay
) 'defaction
616 (let ((options (org-element-property :BEAMER_OPT headline
)))
618 (org-beamer--normalize-argument options
'option
)))
619 ;; Resolve reference provided by "BEAMER_ref"
620 ;; property. This is done by building a minimal
621 ;; fake link and calling the appropriate resolve
622 ;; function, depending on the reference syntax.
624 (if (string-match "\\`\\(id:\\|#\\)" ref
)
625 (org-export-resolve-id-link
626 `(link (:path
,(substring ref
(match-end 0))))
628 (org-export-resolve-fuzzy-link
630 ;; Look for headlines only.
631 ,(if (eq (string-to-char ref
) ?
*) ref
634 ;; Now use user-defined label provided in TARGET
635 ;; headline, or fallback to standard one.
636 (format "{%s}" (org-beamer--get-label target info
)))))))
637 ;; Case 2: Creation of an appendix is requested.
638 ((equal environment
"appendix")
640 (org-element-property :BEAMER_ACT headline
)
642 (make-string (org-element-property :pre-blank headline
) ?
\n)
644 ;; Case 3: Ignore heading.
645 ((equal environment
"ignoreheading")
646 (concat (make-string (org-element-property :pre-blank headline
) ?
\n)
648 ;; Case 4: HEADLINE is a note.
649 ((member environment
'("note" "noteNH"))
651 (concat (and (equal environment
"note")
654 (org-element-property :title headline
) info
)
656 (org-trim contents
))))
657 ;; Case 5: HEADLINE is a frame.
658 ((= level frame-level
)
659 (org-beamer--format-frame headline contents info
))
660 ;; Case 6: Regular section, extracted from
661 ;; `org-latex-classes'.
662 ((< level frame-level
)
663 (org-beamer--format-section headline contents info
))
664 ;; Case 7: Otherwise, HEADLINE is a block.
665 (t (org-beamer--format-block headline contents info
))))))
670 (defun org-beamer-item (item contents info
)
671 "Transcode an ITEM element into Beamer code.
672 CONTENTS holds the contents of the item. INFO is a plist holding
673 contextual information."
674 (org-export-with-backend
675 ;; Delegate item export to `latex'. However, we use `beamer'
676 ;; transcoders for objects in the description tag.
677 (org-export-create-backend
685 (let ((first (car (org-element-contents item
))))
686 (and (eq (org-element-type first
) 'paragraph
)
687 (org-beamer--element-has-overlay-p first
))))
688 (output (org-latex-item item contents info
)))
689 (if (not (and action
(string-match "\\\\item" output
))) output
690 ;; If the item starts with a paragraph and that paragraph
691 ;; starts with an export snippet specifying an overlay,
692 ;; append it to the \item command.
693 (replace-match (concat "\\\\item" action
) nil nil output
)))))))
699 (defun org-beamer-keyword (keyword contents info
)
700 "Transcode a KEYWORD element into Beamer code.
701 CONTENTS is nil. INFO is a plist used as a communication
703 (let ((key (org-element-property :key keyword
))
704 (value (org-element-property :value keyword
)))
705 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
706 ;; Otherwise, fallback to `latex' back-end.
708 ((equal key
"BEAMER") value
)
709 ((and (equal key
"TOC") (string-match "\\<headlines\\>" value
))
710 (let ((depth (or (and (string-match "[0-9]+" value
)
711 (string-to-number (match-string 0 value
)))
712 (plist-get info
:with-toc
)))
713 (options (and (string-match "\\[.*?\\]" value
)
714 (match-string 0 value
))))
716 (when (wholenump depth
) (format "\\setcounter{tocdepth}{%s}\n" depth
))
717 "\\tableofcontents" options
)))
718 (t (org-export-with-backend 'latex keyword contents info
)))))
723 (defun org-beamer-link (link contents info
)
724 "Transcode a LINK object into Beamer code.
725 CONTENTS is the description part of the link. INFO is a plist
726 used as a communication channel."
727 (let ((type (org-element-property :type link
)))
729 ;; Link type is handled by a special function.
730 ((org-export-custom-protocol-maybe link contents
'beamer
))
731 ;; Use \hyperlink command for all internal links.
732 ((equal type
"radio")
733 (let ((destination (org-export-resolve-radio-link link info
)))
734 (if (not destination
) contents
735 (format "\\hyperlink%s{%s}{%s}"
736 (or (org-beamer--element-has-overlay-p link
) "")
737 (org-export-get-reference destination info
)
739 ((and (member type
'("custom-id" "fuzzy" "id"))
740 (let ((destination (if (string= type
"fuzzy")
741 (org-export-resolve-fuzzy-link link info
)
742 (org-export-resolve-id-link link info
))))
743 (case (org-element-type destination
)
749 (org-export-get-headline-number
752 (if (and (plist-get info
:section-numbers
) (not contents
))
753 (format "\\ref{%s}" label
)
754 (format "\\hyperlink%s{%s}{%s}"
755 (or (org-beamer--element-has-overlay-p link
) "")
759 (let ((ref (org-export-get-reference destination info
)))
760 (if (not contents
) (format "\\ref{%s}" ref
)
761 (format "\\hyperlink%s{%s}{%s}"
762 (or (org-beamer--element-has-overlay-p link
) "")
765 ;; Otherwise, use `latex' back-end.
766 (t (org-export-with-backend 'latex link contents info
)))))
771 ;; Plain lists support `:environment', `:overlay' and `:options'
774 (defun org-beamer-plain-list (plain-list contents info
)
775 "Transcode a PLAIN-LIST element into Beamer code.
776 CONTENTS is the contents of the list. INFO is a plist holding
777 contextual information."
778 (let* ((type (org-element-property :type plain-list
))
779 (attributes (org-combine-plists
780 (org-export-read-attribute :attr_latex plain-list
)
781 (org-export-read-attribute :attr_beamer plain-list
)))
782 (latex-type (let ((env (plist-get attributes
:environment
)))
784 ((eq type
'ordered
) "enumerate")
785 ((eq type
'descriptive
) "description")
787 (org-latex--wrap-label
789 (format "\\begin{%s}%s%s\n%s\\end{%s}"
791 ;; Default overlay specification, if any.
792 (org-beamer--normalize-argument
793 (or (plist-get attributes
:overlay
) "")
795 ;; Second optional argument depends on the list type.
796 (org-beamer--normalize-argument
797 (or (plist-get attributes
:options
) "")
799 ;; Eventually insert contents and close environment.
807 (defun org-beamer-radio-target (radio-target text info
)
808 "Transcode a RADIO-TARGET object into Beamer code.
809 TEXT is the text of the target. INFO is a plist holding
810 contextual information."
811 (format "\\hypertarget%s{%s}{%s}"
812 (or (org-beamer--element-has-overlay-p radio-target
) "")
813 (org-export-get-reference radio-target info
)
819 (defun org-beamer-target (target _contents info
)
820 "Transcode a TARGET object into Beamer code.
821 CONTENTS is nil. INFO is a plist holding contextual
823 (format "\\label{%s}" (org-export-get-reference target info
)))
828 ;; Template used is similar to the one used in `latex' back-end,
829 ;; excepted for the table of contents and Beamer themes.
831 (defun org-beamer-template (contents info
)
832 "Return complete document string after Beamer conversion.
833 CONTENTS is the transcoded contents string. INFO is a plist
834 holding export options."
835 (let ((title (org-export-data (plist-get info
:title
) info
))
836 (subtitle (org-export-data (plist-get info
:subtitle
) info
)))
839 (and (plist-get info
:time-stamp-file
)
840 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
842 (org-latex--insert-compiler info
)
843 ;; Document class and packages.
844 (org-latex--make-preamble info
)
848 (lambda (prop command
)
849 (let ((theme (plist-get info prop
)))
852 (if (not (string-match "\\[.*\\]" theme
))
853 (format "{%s}\n" theme
)
855 (match-string 0 theme
)
857 (replace-match "" nil nil theme
)))))))))))
858 (mapconcat (lambda (args) (apply format-theme args
))
859 '((:beamer-theme
"\\usetheme")
860 (:beamer-color-theme
"\\usecolortheme")
861 (:beamer-font-theme
"\\usefonttheme")
862 (:beamer-inner-theme
"\\useinnertheme")
863 (:beamer-outer-theme
"\\useoutertheme"))
865 ;; Possibly limit depth for headline numbering.
866 (let ((sec-num (plist-get info
:section-numbers
)))
867 (when (integerp sec-num
)
868 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
870 (let ((author (and (plist-get info
:with-author
)
871 (let ((auth (plist-get info
:author
)))
872 (and auth
(org-export-data auth info
)))))
873 (email (and (plist-get info
:with-email
)
874 (org-export-data (plist-get info
:email
) info
))))
875 (cond ((and author email
(not (string= "" email
)))
876 (format "\\author{%s\\thanks{%s}}\n" author email
))
877 ((or author email
) (format "\\author{%s}\n" (or author email
)))))
879 (let ((date (and (plist-get info
:with-date
) (org-export-get-date info
))))
880 (format "\\date{%s}\n" (org-export-data date info
)))
882 (format "\\title{%s}\n" title
)
883 (when (org-string-nw-p subtitle
)
884 (concat (format (plist-get info
:beamer-subtitle-format
) subtitle
) "\n"))
886 (let ((beamer-header (plist-get info
:beamer-header
)))
888 (format "%s\n" (plist-get info
:beamer-header
))))
889 ;; 9. Hyperref options.
890 (let ((template (plist-get info
:latex-hyperref-template
)))
891 (and (stringp template
)
892 (format-spec template
(org-latex--format-spec info
))))
894 "\\begin{document}\n\n"
896 (org-element-normalize-string
897 (cond ((not (plist-get info
:with-title
)) nil
)
898 ((string= "" title
) nil
)
899 ((not (stringp org-latex-title-command
)) nil
)
900 ((string-match "\\(?:[^%]\\|^\\)%s"
901 org-latex-title-command
)
902 (format org-latex-title-command title
))
903 (t org-latex-title-command
)))
904 ;; Table of contents.
905 (let ((depth (plist-get info
:with-toc
)))
908 (format "\\begin{frame}%s{%s}\n"
909 (org-beamer--normalize-argument
910 (plist-get info
:beamer-outline-frame-options
) 'option
)
911 (plist-get info
:beamer-outline-frame-title
))
912 (when (wholenump depth
)
913 (format "\\setcounter{tocdepth}{%d}\n" depth
))
914 "\\tableofcontents\n"
915 "\\end{frame}\n\n")))
919 (if (plist-get info
:with-creator
)
920 (concat (plist-get info
:creator
) "\n")
930 (defvar org-beamer-mode-map
(make-sparse-keymap)
931 "The keymap for `org-beamer-mode'.")
932 (define-key org-beamer-mode-map
"\C-c\C-b" 'org-beamer-select-environment
)
935 (define-minor-mode org-beamer-mode
936 "Support for editing Beamer oriented Org mode files."
937 nil
" Bm" 'org-beamer-mode-map
)
939 (when (fboundp 'font-lock-add-keywords
)
940 (font-lock-add-keywords
942 '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-beamer-tag prepend
))
945 (defface org-beamer-tag
'((t (:box
(:line-width
1 :color grey40
))))
946 "The special face for beamer tags."
947 :group
'org-export-beamer
)
949 (defun org-beamer-property-changed (property value
)
950 "Track the BEAMER_env property with tags.
951 PROPERTY is the name of the modified property. VALUE is its new
954 ((equal property
"BEAMER_env")
956 (org-back-to-heading t
)
957 ;; Filter out Beamer-related tags and install environment tag.
958 (let ((tags (cl-remove-if (lambda (x) (string-match "^B_" x
))
960 (env-tag (and (org-string-nw-p value
) (concat "B_" value
))))
961 (org-set-tags-to (if env-tag
(cons env-tag tags
) tags
))
962 (when env-tag
(org-toggle-tag env-tag
'on
)))))
963 ((equal property
"BEAMER_col")
964 (org-toggle-tag "BMCOL" (if (org-string-nw-p value
) 'on
'off
)))))
966 (add-hook 'org-property-changed-functions
'org-beamer-property-changed
)
968 (defun org-beamer-allowed-property-values (property)
969 "Supply allowed values for PROPERTY."
971 ((and (equal property
"BEAMER_env")
972 (not (org-entry-get nil
(concat property
"_ALL") 'inherit
)))
973 ;; If no allowed values for BEAMER_env have been defined,
974 ;; supply all defined environments
975 (mapcar 'car
(append org-beamer-environments-special
976 org-beamer-environments-extra
977 org-beamer-environments-default
)))
978 ((and (equal property
"BEAMER_col")
979 (not (org-entry-get nil
(concat property
"_ALL") 'inherit
)))
980 ;; If no allowed values for BEAMER_col have been defined,
982 (org-split-string org-beamer-column-widths
" "))))
984 (add-hook 'org-property-allowed-value-functions
985 'org-beamer-allowed-property-values
)
992 (defun org-beamer-export-as-latex
993 (&optional async subtreep visible-only body-only ext-plist
)
994 "Export current buffer as a Beamer buffer.
996 If narrowing is active in the current buffer, only export its
999 If a region is active, export that region.
1001 A non-nil optional argument ASYNC means the process should happen
1002 asynchronously. The resulting buffer should be accessible
1003 through the `org-export-stack' interface.
1005 When optional argument SUBTREEP is non-nil, export the sub-tree
1006 at point, extracting information from the headline properties
1009 When optional argument VISIBLE-ONLY is non-nil, don't export
1010 contents of hidden elements.
1012 When optional argument BODY-ONLY is non-nil, only write code
1013 between \"\\begin{document}\" and \"\\end{document}\".
1015 EXT-PLIST, when provided, is a property list with external
1016 parameters overriding Org default settings, but still inferior to
1017 file-local settings.
1019 Export is done in a buffer named \"*Org BEAMER Export*\", which
1020 will be displayed when `org-export-show-temporary-export-buffer'
1023 (org-export-to-buffer 'beamer
"*Org BEAMER Export*"
1024 async subtreep visible-only body-only ext-plist
(lambda () (LaTeX-mode))))
1027 (defun org-beamer-export-to-latex
1028 (&optional async subtreep visible-only body-only ext-plist
)
1029 "Export current buffer as a Beamer presentation (tex).
1031 If narrowing is active in the current buffer, only export its
1034 If a region is active, export that region.
1036 A non-nil optional argument ASYNC means the process should happen
1037 asynchronously. The resulting file should be accessible through
1038 the `org-export-stack' interface.
1040 When optional argument SUBTREEP is non-nil, export the sub-tree
1041 at point, extracting information from the headline properties
1044 When optional argument VISIBLE-ONLY is non-nil, don't export
1045 contents of hidden elements.
1047 When optional argument BODY-ONLY is non-nil, only write code
1048 between \"\\begin{document}\" and \"\\end{document}\".
1050 EXT-PLIST, when provided, is a property list with external
1051 parameters overriding Org default settings, but still inferior to
1052 file-local settings.
1054 Return output file's name."
1056 (let ((file (org-export-output-file-name ".tex" subtreep
)))
1057 (org-export-to-file 'beamer file
1058 async subtreep visible-only body-only ext-plist
)))
1061 (defun org-beamer-export-to-pdf
1062 (&optional async subtreep visible-only body-only ext-plist
)
1063 "Export current buffer as a Beamer presentation (PDF).
1065 If narrowing is active in the current buffer, only export its
1068 If a region is active, export that region.
1070 A non-nil optional argument ASYNC means the process should happen
1071 asynchronously. The resulting file should be accessible through
1072 the `org-export-stack' interface.
1074 When optional argument SUBTREEP is non-nil, export the sub-tree
1075 at point, extracting information from the headline properties
1078 When optional argument VISIBLE-ONLY is non-nil, don't export
1079 contents of hidden elements.
1081 When optional argument BODY-ONLY is non-nil, only write code
1082 between \"\\begin{document}\" and \"\\end{document}\".
1084 EXT-PLIST, when provided, is a property list with external
1085 parameters overriding Org default settings, but still inferior to
1086 file-local settings.
1088 Return PDF file's name."
1090 (let ((file (org-export-output-file-name ".tex" subtreep
)))
1091 (org-export-to-file 'beamer file
1092 async subtreep visible-only body-only ext-plist
1093 (lambda (file) (org-latex-compile file
)))))
1096 (defun org-beamer-select-environment ()
1097 "Select the environment to be used by beamer for this entry.
1098 While this uses (for convenience) a tag selection interface, the
1099 result of this command will be that the BEAMER_env *property* of
1102 In addition to this, the command will also set a tag as a visual
1103 aid, but the tag does not have any semantic meaning."
1105 ;; Make sure `org-beamer-environments-special' has a higher
1106 ;; priority than `org-beamer-environments-extra'.
1107 (let* ((envs (append org-beamer-environments-special
1108 org-beamer-environments-extra
1109 org-beamer-environments-default
))
1110 (org-current-tag-alist
1111 (append '((:startgroup
))
1112 (mapcar (lambda (e) (cons (concat "B_" (car e
))
1113 (string-to-char (nth 1 e
))))
1117 (org-tag-persistent-alist nil
)
1118 (org-use-fast-tag-selection t
)
1119 (org-fast-tag-selection-single-key t
))
1121 (let ((tags (or (ignore-errors (org-get-tags-string)) "")))
1123 ;; For a column, automatically ask for its width.
1124 ((eq org-last-tag-selection-key ?|
)
1125 (if (string-match ":BMCOL:" tags
)
1126 (org-set-property "BEAMER_col" (read-string "Column width: "))
1127 (org-delete-property "BEAMER_col")))
1128 ;; For an "againframe" section, automatically ask for reference
1129 ;; to resumed frame and overlay specifications.
1130 ((eq org-last-tag-selection-key ?A
)
1131 (if (equal (org-entry-get nil
"BEAMER_env") "againframe")
1132 (progn (org-entry-delete nil
"BEAMER_env")
1133 (org-entry-delete nil
"BEAMER_ref")
1134 (org-entry-delete nil
"BEAMER_act"))
1135 (org-entry-put nil
"BEAMER_env" "againframe")
1138 (read-string "Frame reference (*Title, #custom-id, id:...): "))
1139 (org-set-property "BEAMER_act"
1140 (read-string "Overlay specification: "))))
1141 ((string-match (concat ":B_\\(" (mapconcat 'car envs
"\\|") "\\):") tags
)
1142 (org-entry-put nil
"BEAMER_env" (match-string 1 tags
)))
1143 (t (org-entry-delete nil
"BEAMER_env"))))))
1146 (defun org-beamer-publish-to-latex (plist filename pub-dir
)
1147 "Publish an Org file to a Beamer presentation (LaTeX).
1149 FILENAME is the filename of the Org file to be published. PLIST
1150 is the property list for the given project. PUB-DIR is the
1151 publishing directory.
1153 Return output file name."
1154 (org-publish-org-to 'beamer filename
".tex" plist pub-dir
))
1157 (defun org-beamer-publish-to-pdf (plist filename pub-dir
)
1158 "Publish an Org file to a Beamer presentation (PDF, via LaTeX).
1160 FILENAME is the filename of the Org file to be published. PLIST
1161 is the property list for the given project. PUB-DIR is the
1162 publishing directory.
1164 Return output file name."
1165 ;; Unlike to `org-beamer-publish-to-latex', PDF file is generated in
1166 ;; working directory and then moved to publishing directory.
1167 (org-publish-attachment
1171 'beamer filename
".tex" plist
(file-name-directory filename
)))
1175 (provide 'ox-beamer
)
1178 ;; generated-autoload-file: "org-loaddefs.el"
1181 ;;; ox-beamer.el ends here