1 ;;; ox-beamer.el --- Beamer Back-End for Org Export Engine
3 ;; Copyright (C) 2007-2015 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
))
35 ;; Install a default set-up for Beamer export.
36 (unless (assoc "beamer" org-latex-classes
)
37 (add-to-list 'org-latex-classes
39 "\\documentclass[presentation]{beamer}"
40 ("\\section{%s}" .
"\\section*{%s}")
41 ("\\subsection{%s}" .
"\\subsection*{%s}")
42 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))))
46 ;;; User-Configurable Variables
48 (defgroup org-export-beamer nil
49 "Options specific for using the beamer class in LaTeX export."
54 (defcustom org-beamer-frame-level
1
55 "The level at which headlines become frames.
57 Headlines at a lower level will be translated into a sectioning
58 structure. At a higher level, they will be translated into
61 If a headline with a \"BEAMER_env\" property set to \"frame\" is
62 found within a tree, its level locally overrides this number.
64 This variable has no effect on headlines with the \"BEAMER_env\"
65 property set to either \"ignoreheading\", \"appendix\", or
66 \"note\", which will respectively, be invisible, become an
69 This integer is relative to the minimal level of a headline
70 within the parse tree, defined as 1."
71 :group
'org-export-beamer
74 (defcustom org-beamer-frame-default-options
""
75 "Default options string to use for frames.
76 For example, it could be set to \"allowframebreaks\"."
77 :group
'org-export-beamer
78 :type
'(string :tag
"[options]"))
80 (defcustom org-beamer-column-view-format
81 "%45ITEM %10BEAMER_env(Env) %10BEAMER_act(Act) %4BEAMER_col(Col) %8BEAMER_opt(Opt)"
82 "Column view format that should be used to fill the template."
83 :group
'org-export-beamer
85 :package-version
'(Org .
"8.0")
87 (const :tag
"Do not insert Beamer column view format" nil
)
88 (string :tag
"Beamer column view format")))
90 (defcustom org-beamer-theme
"default"
91 "Default theme used in Beamer presentations."
92 :group
'org-export-beamer
94 :package-version
'(Org .
"8.0")
96 (const :tag
"Do not insert a Beamer theme" nil
)
97 (string :tag
"Beamer theme")))
99 (defcustom org-beamer-environments-extra nil
100 "Environments triggered by tags in Beamer export.
101 Each entry has 4 elements:
103 name Name of the environment
104 key Selection key for `org-beamer-select-environment'
105 open The opening template for the environment, with the following escapes
106 %a the action/overlay specification
107 %A the default action/overlay specification
108 %o the options argument of the template
110 %r the raw headline text (i.e. without any processing)
111 %H if there is headline text, that raw text in {} braces
112 %U if there is headline text, that raw text in [] brackets
113 close The closing string of the environment."
114 :group
'org-export-beamer
116 :package-version
'(Org .
"8.1")
119 (string :tag
"Environment")
120 (string :tag
"Selection key")
121 (string :tag
"Begin")
122 (string :tag
"End"))))
124 (defcustom org-beamer-outline-frame-title
"Outline"
125 "Default title of a frame containing an outline."
126 :group
'org-export-beamer
127 :type
'(string :tag
"Outline frame title"))
129 (defcustom org-beamer-outline-frame-options
""
130 "Outline frame options appended after \\begin{frame}.
131 You might want to put e.g. \"allowframebreaks=0.9\" here."
132 :group
'org-export-beamer
133 :type
'(string :tag
"Outline frame options"))
136 (defcustom org-beamer-subtitle-format
"\\subtitle{%s}"
137 "Format string used for transcoded subtitle.
138 The format string should have at most one \"%s\"-expression,
139 which is replaced with the subtitle."
140 :group
'org-export-beamer
142 :package-version
'(Org .
"8.3")
143 :type
'(string :tag
"Format string"))
146 ;;; Internal Variables
148 (defconst org-beamer-column-widths
149 "0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.0 :ETC"
150 "The column widths that should be installed as allowed property values.")
152 (defconst org-beamer-environments-special
159 ("ignoreheading" "i")
162 "Alist of environments treated in a special way by the back-end.
163 Keys are environment names, as strings, values are bindings used
164 in `org-beamer-select-environment'. Environments listed here,
165 along with their binding, are hard coded and cannot be modified
166 through `org-beamer-environments-extra' variable.")
168 (defconst org-beamer-environments-default
169 '(("block" "b" "\\begin{block}%a{%h}" "\\end{block}")
170 ("alertblock" "a" "\\begin{alertblock}%a{%h}" "\\end{alertblock}")
171 ("verse" "v" "\\begin{verse}%a %% %h" "\\end{verse}")
172 ("quotation" "q" "\\begin{quotation}%a %% %h" "\\end{quotation}")
173 ("quote" "Q" "\\begin{quote}%a %% %h" "\\end{quote}")
174 ("structureenv" "s" "\\begin{structureenv}%a %% %h" "\\end{structureenv}")
175 ("theorem" "t" "\\begin{theorem}%a%U" "\\end{theorem}")
176 ("definition" "d" "\\begin{definition}%a%U" "\\end{definition}")
177 ("example" "e" "\\begin{example}%a%U" "\\end{example}")
178 ("exampleblock" "E" "\\begin{exampleblock}%a{%h}" "\\end{exampleblock}")
179 ("proof" "p" "\\begin{proof}%a%U" "\\end{proof}")
180 ("beamercolorbox" "o" "\\begin{beamercolorbox}%o{%h}" "\\end{beamercolorbox}"))
181 "Environments triggered by properties in Beamer export.
182 These are the defaults - for user definitions, see
183 `org-beamer-environments-extra'.")
185 (defconst org-beamer-verbatim-elements
186 '(code example-block fixed-width inline-src-block src-block verbatim
)
187 "List of element or object types producing verbatim text.
188 This is used internally to determine when a frame should have the
189 \"fragile\" option.")
193 ;;; Internal functions
195 (defun org-beamer--normalize-argument (argument type
)
196 "Return ARGUMENT string with proper boundaries.
198 TYPE is a symbol among the following:
199 `action' Return ARGUMENT within angular brackets.
200 `defaction' Return ARGUMENT within both square and angular brackets.
201 `option' Return ARGUMENT within square brackets."
202 (if (not (string-match "\\S-" argument
)) ""
204 (action (if (string-match "\\`<.*>\\'" argument
) argument
205 (format "<%s>" argument
)))
207 ((string-match "\\`\\[<.*>\\]\\'" argument
) argument
)
208 ((string-match "\\`<.*>\\'" argument
)
209 (format "[%s]" argument
))
210 ((string-match "\\`\\[\\(.*\\)\\]\\'" argument
)
211 (format "[<%s>]" (match-string 1 argument
)))
212 (t (format "[<%s>]" argument
))))
213 (option (if (string-match "\\`\\[.*\\]\\'" argument
) argument
214 (format "[%s]" argument
)))
215 (otherwise argument
))))
217 (defun org-beamer--element-has-overlay-p (element)
218 "Non-nil when ELEMENT has an overlay specified.
219 An element has an overlay specification when it starts with an
220 `beamer' export-snippet whose value is between angular brackets.
221 Return overlay specification, as a string, or nil."
222 (let ((first-object (car (org-element-contents element
))))
223 (when (eq (org-element-type first-object
) 'export-snippet
)
224 (let ((value (org-element-property :value first-object
)))
225 (and (string-match "\\`<.*>\\'" value
) value
)))))
231 (org-export-define-derived-backend 'beamer
'latex
232 :export-block
"BEAMER"
235 ((?B
"As LaTeX buffer (Beamer)" org-beamer-export-as-latex
)
236 (?b
"As LaTeX file (Beamer)" org-beamer-export-to-latex
)
237 (?P
"As PDF file (Beamer)" org-beamer-export-to-pdf
)
238 (?O
"As PDF file and open (Beamer)"
240 (if a
(org-beamer-export-to-pdf t s v b
)
241 (org-open-file (org-beamer-export-to-pdf nil s v b
)))))))
243 '((:headline-levels nil
"H" org-beamer-frame-level
)
244 (:latex-class
"LATEX_CLASS" nil
"beamer" t
)
245 (:beamer-subtitle-format nil nil org-beamer-subtitle-format
)
246 (:beamer-column-view-format
"COLUMNS" nil org-beamer-column-view-format
)
247 (:beamer-theme
"BEAMER_THEME" nil org-beamer-theme
)
248 (:beamer-color-theme
"BEAMER_COLOR_THEME" nil nil t
)
249 (:beamer-font-theme
"BEAMER_FONT_THEME" nil nil t
)
250 (:beamer-inner-theme
"BEAMER_INNER_THEME" nil nil t
)
251 (:beamer-outer-theme
"BEAMER_OUTER_THEME" nil nil t
)
252 (:beamer-header
"BEAMER_HEADER" nil nil newline
)
253 (:beamer-environments-extra nil nil org-beamer-environments-extra
)
254 (:beamer-frame-default-options nil nil org-beamer-frame-default-options
)
255 (:beamer-outline-frame-options nil nil org-beamer-outline-frame-options
)
256 (:beamer-outline-frame-title nil nil org-beamer-outline-frame-title
))
257 :translate-alist
'((bold . org-beamer-bold
)
258 (export-block . org-beamer-export-block
)
259 (export-snippet . org-beamer-export-snippet
)
260 (headline . org-beamer-headline
)
261 (item . org-beamer-item
)
262 (keyword . org-beamer-keyword
)
263 (link . org-beamer-link
)
264 (plain-list . org-beamer-plain-list
)
265 (radio-target . org-beamer-radio-target
)
266 (target . org-beamer-target
)
267 (template . org-beamer-template
)))
271 ;;; Transcode Functions
275 (defun org-beamer-bold (bold contents info
)
276 "Transcode BLOCK object into Beamer code.
277 CONTENTS is the text being bold. INFO is a plist used as
278 a communication channel."
279 (format "\\alert%s{%s}"
280 (or (org-beamer--element-has-overlay-p bold
) "")
286 (defun org-beamer-export-block (export-block contents info
)
287 "Transcode an EXPORT-BLOCK element into Beamer code.
288 CONTENTS is nil. INFO is a plist used as a communication
290 (when (member (org-element-property :type export-block
) '("BEAMER" "LATEX"))
291 (org-remove-indentation (org-element-property :value export-block
))))
296 (defun org-beamer-export-snippet (export-snippet contents info
)
297 "Transcode an EXPORT-SNIPPET object into Beamer code.
298 CONTENTS is nil. INFO is a plist used as a communication
300 (let ((backend (org-export-snippet-backend export-snippet
))
301 (value (org-element-property :value export-snippet
)))
302 ;; Only "latex" and "beamer" snippets are retained.
303 (cond ((eq backend
'latex
) value
)
304 ;; Ignore "beamer" snippets specifying overlays.
305 ((and (eq backend
'beamer
)
306 (or (org-export-get-previous-element export-snippet info
)
307 (not (string-match "\\`<.*>\\'" value
))))
313 ;; The main function to translate a headline is
314 ;; `org-beamer-headline'.
316 ;; Depending on the level at which a headline is considered as
317 ;; a frame (given by `org-beamer--frame-level'), the headline is
318 ;; either a section (`org-beamer--format-section'), a frame
319 ;; (`org-beamer--format-frame') or a block
320 ;; (`org-beamer--format-block').
322 ;; `org-beamer-headline' also takes care of special environments
323 ;; like "ignoreheading", "note", "noteNH", "appendix" and
326 (defun org-beamer--get-label (headline info
)
327 "Return label for HEADLINE, as a string.
329 INFO is a plist used as a communication channel.
331 The value is either the label specified in \"BEAMER_opt\"
332 property, or a fallback value built from headline's number. This
333 function assumes HEADLINE will be treated as a frame."
334 (let ((opt (org-element-property :BEAMER_OPT headline
)))
335 (if (and (org-string-nw-p opt
)
336 (string-match "\\(?:^\\|,\\)label=\\(.*?\\)\\(?:$\\|,\\)" opt
))
339 (mapconcat 'number-to-string
340 (org-export-get-headline-number headline info
)
343 (defun org-beamer--frame-level (headline info
)
344 "Return frame level in subtree containing HEADLINE.
345 INFO is a plist used as a communication channel."
347 ;; 1. Look for "frame" environment in parents, starting from the
350 (dolist (parent (nreverse (org-element-lineage headline
)))
351 (let ((env (org-element-property :BEAMER_ENV parent
)))
352 (when (and env
(member-ignore-case env
'("frame" "fullframe")))
353 (throw 'exit
(org-export-get-relative-level parent info
))))))
354 ;; 2. Look for "frame" environment in HEADLINE.
355 (let ((env (org-element-property :BEAMER_ENV headline
)))
356 (and env
(member-ignore-case env
'("frame" "fullframe"))
357 (org-export-get-relative-level headline info
)))
358 ;; 3. Look for "frame" environment in sub-tree.
359 (org-element-map headline
'headline
361 (let ((env (org-element-property :BEAMER_ENV hl
)))
362 (when (and env
(member-ignore-case env
'("frame" "fullframe")))
363 (org-export-get-relative-level hl info
))))
365 ;; 4. No "frame" environment in tree: use default value.
366 (plist-get info
:headline-levels
)))
368 (defun org-beamer--format-section (headline contents info
)
369 "Format HEADLINE as a sectioning part.
370 CONTENTS holds the contents of the headline. INFO is a plist
371 used as a communication channel."
372 (let ((latex-headline
373 (org-export-with-backend
374 ;; We create a temporary export back-end which behaves the
375 ;; same as current one, but adds "\protect" in front of the
376 ;; output of some objects.
377 (org-export-create-backend
380 (let ((protected-output
382 (lambda (object contents info
)
383 (let ((code (org-export-with-backend
384 'beamer object contents info
)))
385 (if (org-string-nw-p code
) (concat "\\protect" code
)
387 (mapcar #'(lambda (type) (cons type protected-output
))
388 '(bold footnote-reference italic strike-through timestamp
393 (mode-specs (org-element-property :BEAMER_ACT headline
)))
395 (string-match "\\`\\\\\\(.*?\\)\\(?:\\*\\|\\[.*\\]\\)?{"
397 ;; Insert overlay specifications.
398 (replace-match (concat (match-string 1 latex-headline
)
399 (format "<%s>" mode-specs
))
400 nil nil latex-headline
1)
403 (defun org-beamer--format-frame (headline contents info
)
404 "Format HEADLINE as a frame.
405 CONTENTS holds the contents of the headline. INFO is a plist
406 used as a communication channel."
408 ;; FRAGILEP is non-nil when HEADLINE contains an element
409 ;; among `org-beamer-verbatim-elements'.
410 (org-element-map headline org-beamer-verbatim-elements
'identity
412 (concat "\\begin{frame}"
413 ;; Overlay specification, if any. When surrounded by
414 ;; square brackets, consider it as a default
416 (let ((action (org-element-property :BEAMER_ACT headline
)))
419 ((string-match "\\`\\[.*\\]\\'" action
)
420 (org-beamer--normalize-argument action
'defaction
))
421 (t (org-beamer--normalize-argument action
'action
))))
423 (let* ((beamer-opt (org-element-property :BEAMER_OPT headline
))
425 ;; Collect options from default value and headline's
426 ;; properties. Also add a label for links.
429 (plist-get info
:beamer-frame-default-options
) ",")
432 ;; Remove square brackets if user provided
434 (and (string-match "^\\[?\\(.*\\)\\]?$" beamer-opt
)
435 (match-string 1 beamer-opt
))
437 ;; Provide an automatic label for the frame
438 ;; unless the user specified one. Also refrain
439 ;; from labeling `allowframebreaks' frames; this
440 ;; is not allowed by beamer.
441 (unless (and beamer-opt
442 (or (string-match "\\(^\\|,\\)label=" beamer-opt
)
443 (string-match "allowframebreaks" beamer-opt
)))
446 (org-beamer--get-label headline info
)))))))
447 ;; Change options list into a string.
448 (org-beamer--normalize-argument
451 (if (or (not fragilep
) (member "fragile" options
)) options
452 (cons "fragile" options
))
456 (let ((env (org-element-property :BEAMER_ENV headline
)))
458 (if (and env
(equal (downcase env
) "fullframe")) ""
460 (org-element-property :title headline
) info
))))
462 ;; The following workaround is required in fragile frames
463 ;; as Beamer will append "\par" to the beginning of the
464 ;; contents. So we need to make sure the command is
465 ;; separated from the contents by at least one space. If
466 ;; it isn't, it will create "\parfirst-word" command and
467 ;; remove the first word from the contents in the PDF
469 (if (not fragilep
) contents
470 (replace-regexp-in-string "\\`\n*" "\\& " (or contents
"")))
473 (defun org-beamer--format-block (headline contents info
)
474 "Format HEADLINE as a block.
475 CONTENTS holds the contents of the headline. INFO is a plist
476 used as a communication channel."
477 (let* ((column-width (org-element-property :BEAMER_COL headline
))
478 ;; ENVIRONMENT defaults to "block" if none is specified and
479 ;; there is no column specification. If there is a column
480 ;; specified but still no explicit environment, ENVIRONMENT
482 (environment (let ((env (org-element-property :BEAMER_ENV headline
)))
484 ;; "block" is the fallback environment.
485 ((and (not env
) (not column-width
)) "block")
488 ;; Use specified environment.
490 (raw-title (org-element-property :raw-value headline
))
492 (cond ((member environment
'("column" "columns")) nil
)
494 (append (plist-get info
:beamer-environments-extra
)
495 org-beamer-environments-default
)))
496 (t (user-error "Wrong block type at a headline named \"%s\""
498 (title (org-export-data (org-element-property :title headline
) info
))
499 (options (let ((options (org-element-property :BEAMER_OPT headline
)))
501 (org-beamer--normalize-argument options
'option
))))
502 ;; Start a "columns" environment when explicitly requested or
503 ;; when there is no previous headline or the previous
504 ;; headline do not have a BEAMER_column property.
505 (parent-env (org-element-property
506 :BEAMER_ENV
(org-export-get-parent-headline headline
)))
508 (or (equal environment
"columns")
511 (equal (downcase parent-env
) "columns")))
512 (or (org-export-first-sibling-p headline info
)
513 (not (org-element-property
515 (org-export-get-previous-element
517 ;; End the "columns" environment when explicitly requested or
518 ;; when there is no next headline or the next headline do not
519 ;; have a BEAMER_column property.
521 (or (equal environment
"columns")
524 (equal (downcase parent-env
) "columns")))
525 (or (org-export-last-sibling-p headline info
)
526 (not (org-element-property
528 (org-export-get-next-element headline info
))))))))
530 (when start-columns-p
531 ;; Column can accept options only when the environment is
532 ;; explicitly defined.
533 (if (not (equal environment
"columns")) "\\begin{columns}\n"
534 (format "\\begin{columns}%s\n" options
)))
536 (format "\\begin{column}%s{%s}\n"
537 ;; One can specify placement for column only when
538 ;; HEADLINE stands for a column on its own.
539 (if (equal environment
"column") options
"")
540 (format "%s\\columnwidth" column-width
)))
541 ;; Block's opening string.
542 (when (nth 2 env-format
)
547 ;; If BEAMER_act property has its value enclosed in square
548 ;; brackets, it is a default overlay specification and
549 ;; overlay specification is empty. Otherwise, it is an
550 ;; overlay specification and the default one is nil.
551 (let ((action (org-element-property :BEAMER_ACT headline
)))
553 ((not action
) (list (cons "a" "") (cons "A" "")))
554 ((string-match "\\`\\[.*\\]\\'" action
)
556 (cons "A" (org-beamer--normalize-argument action
'defaction
))
559 (list (cons "a" (org-beamer--normalize-argument action
'action
))
561 (list (cons "o" options
)
564 (cons "H" (if (equal raw-title
"") ""
565 (format "{%s}" raw-title
)))
566 (cons "U" (if (equal raw-title
"") ""
567 (format "[%s]" raw-title
))))))
570 ;; Block's closing string, if any.
571 (and (nth 3 env-format
) (concat (nth 3 env-format
) "\n"))
572 (when column-width
"\\end{column}\n")
573 (when end-columns-p
"\\end{columns}"))))
575 (defun org-beamer-headline (headline contents info
)
576 "Transcode HEADLINE element into Beamer code.
577 CONTENTS is the contents of the headline. INFO is a plist used
578 as a communication channel."
579 (unless (org-element-property :footnote-section-p headline
)
580 (let ((level (org-export-get-relative-level headline info
))
581 (frame-level (org-beamer--frame-level headline info
))
582 (environment (let ((env (org-element-property :BEAMER_ENV headline
)))
583 (or (org-string-nw-p env
) "block"))))
585 ;; Case 1: Resume frame specified by "BEAMER_ref" property.
586 ((equal environment
"againframe")
587 (let ((ref (org-element-property :BEAMER_REF headline
)))
588 ;; Reference to frame being resumed is mandatory. Ignore
589 ;; the whole headline if it isn't provided.
590 (when (org-string-nw-p ref
)
591 (concat "\\againframe"
592 ;; Overlay specification.
593 (let ((overlay (org-element-property :BEAMER_ACT headline
)))
595 (org-beamer--normalize-argument
597 (if (string-match "^\\[.*\\]$" overlay
) 'defaction
600 (let ((options (org-element-property :BEAMER_OPT headline
)))
602 (org-beamer--normalize-argument options
'option
)))
603 ;; Resolve reference provided by "BEAMER_ref"
604 ;; property. This is done by building a minimal fake
605 ;; link and calling the appropriate resolve function,
606 ;; depending on the reference syntax.
609 (string-match "^\\(id:\\|#\\|\\*\\)?\\(.*\\)" ref
)
611 ((or (not (match-string 1 ref
))
612 (equal (match-string 1 ref
) "*")) 'fuzzy
)
613 ((equal (match-string 1 ref
) "id:") 'id
)
615 (link (list 'link
(list :path
(match-string 2 ref
))))
616 (target (if (eq type
'fuzzy
)
617 (org-export-resolve-fuzzy-link link info
)
618 (org-export-resolve-id-link link info
))))
619 ;; Now use user-defined label provided in TARGET
620 ;; headline, or fallback to standard one.
621 (format "{%s}" (org-beamer--get-label target info
)))))))
622 ;; Case 2: Creation of an appendix is requested.
623 ((equal environment
"appendix")
625 (org-element-property :BEAMER_ACT headline
)
627 (make-string (org-element-property :pre-blank headline
) ?
\n)
629 ;; Case 3: Ignore heading.
630 ((equal environment
"ignoreheading")
631 (concat (make-string (org-element-property :pre-blank headline
) ?
\n)
633 ;; Case 4: HEADLINE is a note.
634 ((member environment
'("note" "noteNH"))
636 (concat (and (equal environment
"note")
639 (org-element-property :title headline
) info
)
641 (org-trim contents
))))
642 ;; Case 5: HEADLINE is a frame.
643 ((= level frame-level
)
644 (org-beamer--format-frame headline contents info
))
645 ;; Case 6: Regular section, extracted from
646 ;; `org-latex-classes'.
647 ((< level frame-level
)
648 (org-beamer--format-section headline contents info
))
649 ;; Case 7: Otherwise, HEADLINE is a block.
650 (t (org-beamer--format-block headline contents info
))))))
655 (defun org-beamer-item (item contents info
)
656 "Transcode an ITEM element into Beamer code.
657 CONTENTS holds the contents of the item. INFO is a plist holding
658 contextual information."
659 (org-export-with-backend
660 ;; Delegate item export to `latex'. However, we use `beamer'
661 ;; transcoders for objects in the description tag.
662 (org-export-create-backend
670 (let ((first (car (org-element-contents item
))))
671 (and (eq (org-element-type first
) 'paragraph
)
672 (org-beamer--element-has-overlay-p first
))))
673 (output (org-latex-item item contents info
)))
674 (if (not (and action
(string-match "\\\\item" output
))) output
675 ;; If the item starts with a paragraph and that paragraph
676 ;; starts with an export snippet specifying an overlay,
677 ;; append it to the \item command.
678 (replace-match (concat "\\\\item" action
) nil nil output
)))))))
684 (defun org-beamer-keyword (keyword contents info
)
685 "Transcode a KEYWORD element into Beamer code.
686 CONTENTS is nil. INFO is a plist used as a communication
688 (let ((key (org-element-property :key keyword
))
689 (value (org-element-property :value keyword
)))
690 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
691 ;; Otherwise, fallback to `latex' back-end.
693 ((equal key
"BEAMER") value
)
694 ((and (equal key
"TOC") (string-match "\\<headlines\\>" value
))
695 (let ((depth (or (and (string-match "[0-9]+" value
)
696 (string-to-number (match-string 0 value
)))
697 (plist-get info
:with-toc
)))
698 (options (and (string-match "\\[.*?\\]" value
)
699 (match-string 0 value
))))
701 (when (wholenump depth
) (format "\\setcounter{tocdepth}{%s}\n" depth
))
702 "\\tableofcontents" options
)))
703 (t (org-export-with-backend 'latex keyword contents info
)))))
708 (defun org-beamer-link (link contents info
)
709 "Transcode a LINK object into Beamer code.
710 CONTENTS is the description part of the link. INFO is a plist
711 used as a communication channel."
712 (let ((type (org-element-property :type link
))
713 (path (org-element-property :path link
)))
715 ;; Link type is handled by a special function.
716 ((org-export-custom-protocol-maybe link contents
'beamer
))
717 ;; Use \hyperlink command for all internal links.
718 ((equal type
"radio")
719 (let ((destination (org-export-resolve-radio-link link info
)))
720 (if (not destination
) contents
721 (format "\\hyperlink%s{%s}{%s}"
722 (or (org-beamer--element-has-overlay-p link
) "")
723 (org-export-solidify-link-text
724 (org-element-property :value destination
))
726 ((and (member type
'("custom-id" "fuzzy" "id"))
727 (let ((destination (if (string= type
"fuzzy")
728 (org-export-resolve-fuzzy-link link info
)
729 (org-export-resolve-id-link link info
))))
730 (case (org-element-type destination
)
736 (org-export-get-headline-number
739 (if (and (plist-get info
:section-numbers
) (not contents
))
740 (format "\\ref{%s}" label
)
741 (format "\\hyperlink%s{%s}{%s}"
742 (or (org-beamer--element-has-overlay-p link
) "")
746 (let ((path (org-export-solidify-link-text path
)))
747 (if (not contents
) (format "\\ref{%s}" path
)
748 (format "\\hyperlink%s{%s}{%s}"
749 (or (org-beamer--element-has-overlay-p link
) "")
752 ;; Otherwise, use `latex' back-end.
753 (t (org-export-with-backend 'latex link contents info
)))))
758 ;; Plain lists support `:environment', `:overlay' and `:options'
761 (defun org-beamer-plain-list (plain-list contents info
)
762 "Transcode a PLAIN-LIST element into Beamer code.
763 CONTENTS is the contents of the list. INFO is a plist holding
764 contextual information."
765 (let* ((type (org-element-property :type plain-list
))
766 (attributes (org-combine-plists
767 (org-export-read-attribute :attr_latex plain-list
)
768 (org-export-read-attribute :attr_beamer plain-list
)))
769 (latex-type (let ((env (plist-get attributes
:environment
)))
771 ((eq type
'ordered
) "enumerate")
772 ((eq type
'descriptive
) "description")
774 (org-latex--wrap-label
776 (format "\\begin{%s}%s%s\n%s\\end{%s}"
778 ;; Default overlay specification, if any.
779 (org-beamer--normalize-argument
780 (or (plist-get attributes
:overlay
) "")
782 ;; Second optional argument depends on the list type.
783 (org-beamer--normalize-argument
784 (or (plist-get attributes
:options
) "")
786 ;; Eventually insert contents and close environment.
793 (defun org-beamer-radio-target (radio-target text info
)
794 "Transcode a RADIO-TARGET object into Beamer code.
795 TEXT is the text of the target. INFO is a plist holding
796 contextual information."
797 (format "\\hypertarget%s{%s}{%s}"
798 (or (org-beamer--element-has-overlay-p radio-target
) "")
799 (org-export-solidify-link-text
800 (org-element-property :value radio-target
))
806 (defun org-beamer-target (target contents info
)
807 "Transcode a TARGET object into Beamer code.
808 CONTENTS is nil. INFO is a plist holding contextual
810 (format "\\label{%s}"
811 (org-export-solidify-link-text (org-element-property :value target
))))
816 ;; Template used is similar to the one used in `latex' back-end,
817 ;; excepted for the table of contents and Beamer themes.
819 (defun org-beamer-template (contents info
)
820 "Return complete document string after Beamer conversion.
821 CONTENTS is the transcoded contents string. INFO is a plist
822 holding export options."
823 (let ((title (org-export-data (plist-get info
:title
) info
))
824 (subtitle (org-export-data (plist-get info
:subtitle
) info
)))
827 (and (plist-get info
:time-stamp-file
)
828 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
829 ;; 2. Document class and packages.
830 (let* ((class (plist-get info
:latex-class
))
831 (class-options (plist-get info
:latex-class-options
))
832 (header (nth 1 (assoc class org-latex-classes
)))
833 (document-class-string
834 (and (stringp header
)
835 (if (not class-options
) header
836 (replace-regexp-in-string
837 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
838 class-options header t nil
1)))))
839 (if (not document-class-string
)
840 (user-error "Unknown LaTeX class `%s'" class
)
841 (org-latex-guess-babel-language
842 (org-latex-guess-inputenc
843 (org-element-normalize-string
844 (org-splice-latex-header
845 document-class-string
846 org-latex-default-packages-alist
847 org-latex-packages-alist nil
848 (concat (org-element-normalize-string
849 (plist-get info
:latex-header
))
850 (org-element-normalize-string
851 (plist-get info
:latex-header-extra
))))))
856 (lambda (prop command
)
857 (let ((theme (plist-get info prop
)))
860 (if (not (string-match "\\[.*\\]" theme
))
861 (format "{%s}\n" theme
)
863 (match-string 0 theme
)
865 (replace-match "" nil nil theme
)))))))))))
866 (mapconcat (lambda (args) (apply format-theme args
))
867 '((:beamer-theme
"\\usetheme")
868 (:beamer-color-theme
"\\usecolortheme")
869 (:beamer-font-theme
"\\usefonttheme")
870 (:beamer-inner-theme
"\\useinnertheme")
871 (:beamer-outer-theme
"\\useoutertheme"))
873 ;; 4. Possibly limit depth for headline numbering.
874 (let ((sec-num (plist-get info
:section-numbers
)))
875 (when (integerp sec-num
)
876 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
878 (let ((author (and (plist-get info
:with-author
)
879 (let ((auth (plist-get info
:author
)))
880 (and auth
(org-export-data auth info
)))))
881 (email (and (plist-get info
:with-email
)
882 (org-export-data (plist-get info
:email
) info
))))
883 (cond ((and author email
(not (string= "" email
)))
884 (format "\\author{%s\\thanks{%s}}\n" author email
))
885 ((or author email
) (format "\\author{%s}\n" (or author email
)))))
887 (let ((date (and (plist-get info
:with-date
) (org-export-get-date info
))))
888 (format "\\date{%s}\n" (org-export-data date info
)))
890 (format "\\title{%s}\n" title
)
891 (when (org-string-nw-p subtitle
)
892 (concat (format (plist-get info
:beamer-subtitle-format
) subtitle
) "\n"))
894 (let ((beamer-header (plist-get info
:beamer-header
)))
896 (format "%s\n" (plist-get info
:beamer-header
))))
897 ;; 9. Hyperref options.
898 (let ((template (plist-get info
:latex-hyperref-template
)))
899 (and (stringp template
)
900 (format-spec template
(org-latex--format-spec info
))))
901 ;; 10. Document start.
902 "\\begin{document}\n\n"
903 ;; 11. Title command.
904 (org-element-normalize-string
905 (cond ((not (plist-get info
:with-title
)) nil
)
906 ((string= "" title
) nil
)
907 ((not (stringp org-latex-title-command
)) nil
)
908 ((string-match "\\(?:[^%]\\|^\\)%s"
909 org-latex-title-command
)
910 (format org-latex-title-command title
))
911 (t org-latex-title-command
)))
912 ;; 12. Table of contents.
913 (let ((depth (plist-get info
:with-toc
)))
916 (format "\\begin{frame}%s{%s}\n"
917 (org-beamer--normalize-argument
918 (plist-get info
:beamer-outline-frame-options
) 'option
)
919 (plist-get info
:beamer-outline-frame-title
))
920 (when (wholenump depth
)
921 (format "\\setcounter{tocdepth}{%d}\n" depth
))
922 "\\tableofcontents\n"
923 "\\end{frame}\n\n")))
924 ;; 13. Document's body.
927 (if (plist-get info
:with-creator
)
928 (concat (plist-get info
:creator
) "\n")
938 (defvar org-beamer-mode-map
(make-sparse-keymap)
939 "The keymap for `org-beamer-mode'.")
940 (define-key org-beamer-mode-map
"\C-c\C-b" 'org-beamer-select-environment
)
943 (define-minor-mode org-beamer-mode
944 "Support for editing Beamer oriented Org mode files."
945 nil
" Bm" 'org-beamer-mode-map
)
947 (when (fboundp 'font-lock-add-keywords
)
948 (font-lock-add-keywords
950 '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-beamer-tag prepend
))
953 (defface org-beamer-tag
'((t (:box
(:line-width
1 :color grey40
))))
954 "The special face for beamer tags."
955 :group
'org-export-beamer
)
957 (defun org-beamer-property-changed (property value
)
958 "Track the BEAMER_env property with tags.
959 PROPERTY is the name of the modified property. VALUE is its new
962 ((equal property
"BEAMER_env")
964 (org-back-to-heading t
)
965 ;; Filter out Beamer-related tags and install environment tag.
966 (let ((tags (org-remove-if (lambda (x) (string-match "^B_" x
))
968 (env-tag (and (org-string-nw-p value
) (concat "B_" value
))))
969 (org-set-tags-to (if env-tag
(cons env-tag tags
) tags
))
970 (when env-tag
(org-toggle-tag env-tag
'on
)))))
971 ((equal property
"BEAMER_col")
972 (org-toggle-tag "BMCOL" (if (org-string-nw-p value
) 'on
'off
)))))
974 (add-hook 'org-property-changed-functions
'org-beamer-property-changed
)
976 (defun org-beamer-allowed-property-values (property)
977 "Supply allowed values for PROPERTY."
979 ((and (equal property
"BEAMER_env")
980 (not (org-entry-get nil
(concat property
"_ALL") 'inherit
)))
981 ;; If no allowed values for BEAMER_env have been defined,
982 ;; supply all defined environments
983 (mapcar 'car
(append org-beamer-environments-special
984 org-beamer-environments-extra
985 org-beamer-environments-default
)))
986 ((and (equal property
"BEAMER_col")
987 (not (org-entry-get nil
(concat property
"_ALL") 'inherit
)))
988 ;; If no allowed values for BEAMER_col have been defined,
990 (org-split-string org-beamer-column-widths
" "))))
992 (add-hook 'org-property-allowed-value-functions
993 'org-beamer-allowed-property-values
)
1000 (defun org-beamer-export-as-latex
1001 (&optional async subtreep visible-only body-only ext-plist
)
1002 "Export current buffer as a Beamer buffer.
1004 If narrowing is active in the current buffer, only export its
1007 If a region is active, export that region.
1009 A non-nil optional argument ASYNC means the process should happen
1010 asynchronously. The resulting buffer should be accessible
1011 through the `org-export-stack' interface.
1013 When optional argument SUBTREEP is non-nil, export the sub-tree
1014 at point, extracting information from the headline properties
1017 When optional argument VISIBLE-ONLY is non-nil, don't export
1018 contents of hidden elements.
1020 When optional argument BODY-ONLY is non-nil, only write code
1021 between \"\\begin{document}\" and \"\\end{document}\".
1023 EXT-PLIST, when provided, is a property list with external
1024 parameters overriding Org default settings, but still inferior to
1025 file-local settings.
1027 Export is done in a buffer named \"*Org BEAMER Export*\", which
1028 will be displayed when `org-export-show-temporary-export-buffer'
1031 (org-export-to-buffer 'beamer
"*Org BEAMER Export*"
1032 async subtreep visible-only body-only ext-plist
(lambda () (LaTeX-mode))))
1035 (defun org-beamer-export-to-latex
1036 (&optional async subtreep visible-only body-only ext-plist
)
1037 "Export current buffer as a Beamer presentation (tex).
1039 If narrowing is active in the current buffer, only export its
1042 If a region is active, export that region.
1044 A non-nil optional argument ASYNC means the process should happen
1045 asynchronously. The resulting file should be accessible through
1046 the `org-export-stack' interface.
1048 When optional argument SUBTREEP is non-nil, export the sub-tree
1049 at point, extracting information from the headline properties
1052 When optional argument VISIBLE-ONLY is non-nil, don't export
1053 contents of hidden elements.
1055 When optional argument BODY-ONLY is non-nil, only write code
1056 between \"\\begin{document}\" and \"\\end{document}\".
1058 EXT-PLIST, when provided, is a property list with external
1059 parameters overriding Org default settings, but still inferior to
1060 file-local settings.
1062 Return output file's name."
1064 (let ((file (org-export-output-file-name ".tex" subtreep
)))
1065 (org-export-to-file 'beamer file
1066 async subtreep visible-only body-only ext-plist
)))
1069 (defun org-beamer-export-to-pdf
1070 (&optional async subtreep visible-only body-only ext-plist
)
1071 "Export current buffer as a Beamer presentation (PDF).
1073 If narrowing is active in the current buffer, only export its
1076 If a region is active, export that region.
1078 A non-nil optional argument ASYNC means the process should happen
1079 asynchronously. The resulting file should be accessible through
1080 the `org-export-stack' interface.
1082 When optional argument SUBTREEP is non-nil, export the sub-tree
1083 at point, extracting information from the headline properties
1086 When optional argument VISIBLE-ONLY is non-nil, don't export
1087 contents of hidden elements.
1089 When optional argument BODY-ONLY is non-nil, only write code
1090 between \"\\begin{document}\" and \"\\end{document}\".
1092 EXT-PLIST, when provided, is a property list with external
1093 parameters overriding Org default settings, but still inferior to
1094 file-local settings.
1096 Return PDF file's name."
1098 (let ((file (org-export-output-file-name ".tex" subtreep
)))
1099 (org-export-to-file 'beamer file
1100 async subtreep visible-only body-only ext-plist
1101 (lambda (file) (org-latex-compile file
)))))
1104 (defun org-beamer-select-environment ()
1105 "Select the environment to be used by beamer for this entry.
1106 While this uses (for convenience) a tag selection interface, the
1107 result of this command will be that the BEAMER_env *property* of
1110 In addition to this, the command will also set a tag as a visual
1111 aid, but the tag does not have any semantic meaning."
1113 ;; Make sure `org-beamer-environments-special' has a higher
1114 ;; priority than `org-beamer-environments-extra'.
1115 (let* ((envs (append org-beamer-environments-special
1116 org-beamer-environments-extra
1117 org-beamer-environments-default
))
1119 (append '((:startgroup
))
1120 (mapcar (lambda (e) (cons (concat "B_" (car e
))
1121 (string-to-char (nth 1 e
))))
1125 (org-tag-persistent-alist nil
)
1126 (org-use-fast-tag-selection t
)
1127 (org-fast-tag-selection-single-key t
))
1129 (let ((tags (or (ignore-errors (org-get-tags-string)) "")))
1131 ;; For a column, automatically ask for its width.
1132 ((eq org-last-tag-selection-key ?|
)
1133 (if (string-match ":BMCOL:" tags
)
1134 (org-set-property "BEAMER_col" (read-string "Column width: "))
1135 (org-delete-property "BEAMER_col")))
1136 ;; For an "againframe" section, automatically ask for reference
1137 ;; to resumed frame and overlay specifications.
1138 ((eq org-last-tag-selection-key ?A
)
1139 (if (equal (org-entry-get nil
"BEAMER_env") "againframe")
1140 (progn (org-entry-delete nil
"BEAMER_env")
1141 (org-entry-delete nil
"BEAMER_ref")
1142 (org-entry-delete nil
"BEAMER_act"))
1143 (org-entry-put nil
"BEAMER_env" "againframe")
1146 (read-string "Frame reference (*Title, #custom-id, id:...): "))
1147 (org-set-property "BEAMER_act"
1148 (read-string "Overlay specification: "))))
1149 ((string-match (concat ":B_\\(" (mapconcat 'car envs
"\\|") "\\):") tags
)
1150 (org-entry-put nil
"BEAMER_env" (match-string 1 tags
)))
1151 (t (org-entry-delete nil
"BEAMER_env"))))))
1154 (defun org-beamer-publish-to-latex (plist filename pub-dir
)
1155 "Publish an Org file to a Beamer presentation (LaTeX).
1157 FILENAME is the filename of the Org file to be published. PLIST
1158 is the property list for the given project. PUB-DIR is the
1159 publishing directory.
1161 Return output file name."
1162 (org-publish-org-to 'beamer filename
".tex" plist pub-dir
))
1165 (defun org-beamer-publish-to-pdf (plist filename pub-dir
)
1166 "Publish an Org file to a Beamer presentation (PDF, via LaTeX).
1168 FILENAME is the filename of the Org file to be published. PLIST
1169 is the property list for the given project. PUB-DIR is the
1170 publishing directory.
1172 Return output file name."
1173 ;; Unlike to `org-beamer-publish-to-latex', PDF file is generated in
1174 ;; working directory and then moved to publishing directory.
1175 (org-publish-attachment
1179 'beamer filename
".tex" plist
(file-name-directory filename
)))
1183 (provide 'ox-beamer
)
1186 ;; generated-autoload-file: "org-loaddefs.el"
1189 ;;; ox-beamer.el ends here