Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / ox-beamer.el
blob4505e2c135072d21875e0e5e928962b8e32c1be1
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/>.
24 ;;; Commentary:
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.
30 ;;; Code:
32 (require 'cl-lib)
33 (require 'ox-latex)
35 ;; Install a default set-up for Beamer export.
36 (unless (assoc "beamer" org-latex-classes)
37 (add-to-list 'org-latex-classes
38 '("beamer"
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."
50 :tag "Org Beamer"
51 :group 'org-export
52 :version "24.2")
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
59 blocks.
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
67 appendix or a note.
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
72 :type 'integer)
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
84 :version "24.4"
85 :package-version '(Org . "8.0")
86 :type '(choice
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
93 :version "24.4"
94 :package-version '(Org . "8.0")
95 :type '(choice
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 %R the raw BEAMER_act value
109 %o the options argument, with square brackets
110 %O the raw BEAMER_opt value
111 %h the headline text
112 %r the raw headline text (i.e. without any processing)
113 %H if there is headline text, that raw text in {} braces
114 %U if there is headline text, that raw text in [] brackets
115 close The closing string of the environment."
116 :group 'org-export-beamer
117 :version "24.4"
118 :package-version '(Org . "8.1")
119 :type '(repeat
120 (list
121 (string :tag "Environment")
122 (string :tag "Selection key")
123 (string :tag "Begin")
124 (string :tag "End"))))
126 (defcustom org-beamer-outline-frame-title "Outline"
127 "Default title of a frame containing an outline."
128 :group 'org-export-beamer
129 :type '(string :tag "Outline frame title"))
131 (defcustom org-beamer-outline-frame-options ""
132 "Outline frame options appended after \\begin{frame}.
133 You might want to put e.g. \"allowframebreaks=0.9\" here."
134 :group 'org-export-beamer
135 :type '(string :tag "Outline frame options"))
138 (defcustom org-beamer-subtitle-format "\\subtitle{%s}"
139 "Format string used for transcoded subtitle.
140 The format string should have at most one \"%s\"-expression,
141 which is replaced with the subtitle."
142 :group 'org-export-beamer
143 :version "25.1"
144 :package-version '(Org . "8.3")
145 :type '(string :tag "Format string"))
148 ;;; Internal Variables
150 (defconst org-beamer-column-widths
151 "0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.0 :ETC"
152 "The column widths that should be installed as allowed property values.")
154 (defconst org-beamer-environments-special
155 '(("againframe" "A")
156 ("appendix" "x")
157 ("column" "c")
158 ("columns" "C")
159 ("frame" "f")
160 ("fullframe" "F")
161 ("ignoreheading" "i")
162 ("note" "n")
163 ("noteNH" "N"))
164 "Alist of environments treated in a special way by the back-end.
165 Keys are environment names, as strings, values are bindings used
166 in `org-beamer-select-environment'. Environments listed here,
167 along with their binding, are hard coded and cannot be modified
168 through `org-beamer-environments-extra' variable.")
170 (defconst org-beamer-environments-default
171 '(("block" "b" "\\begin{block}%a{%h}" "\\end{block}")
172 ("alertblock" "a" "\\begin{alertblock}%a{%h}" "\\end{alertblock}")
173 ("verse" "v" "\\begin{verse}%a %% %h" "\\end{verse}")
174 ("quotation" "q" "\\begin{quotation}%a %% %h" "\\end{quotation}")
175 ("quote" "Q" "\\begin{quote}%a %% %h" "\\end{quote}")
176 ("structureenv" "s" "\\begin{structureenv}%a %% %h" "\\end{structureenv}")
177 ("theorem" "t" "\\begin{theorem}%a%U" "\\end{theorem}")
178 ("definition" "d" "\\begin{definition}%a%U" "\\end{definition}")
179 ("example" "e" "\\begin{example}%a%U" "\\end{example}")
180 ("exampleblock" "E" "\\begin{exampleblock}%a{%h}" "\\end{exampleblock}")
181 ("proof" "p" "\\begin{proof}%a%U" "\\end{proof}")
182 ("beamercolorbox" "o" "\\begin{beamercolorbox}%o{%h}" "\\end{beamercolorbox}"))
183 "Environments triggered by properties in Beamer export.
184 These are the defaults - for user definitions, see
185 `org-beamer-environments-extra'.")
187 (defconst org-beamer-verbatim-elements
188 '(code example-block fixed-width inline-src-block src-block verbatim)
189 "List of element or object types producing verbatim text.
190 This is used internally to determine when a frame should have the
191 \"fragile\" option.")
195 ;;; Internal functions
197 (defun org-beamer--normalize-argument (argument type)
198 "Return ARGUMENT string with proper boundaries.
200 TYPE is a symbol among the following:
201 `action' Return ARGUMENT within angular brackets.
202 `defaction' Return ARGUMENT within both square and angular brackets.
203 `option' Return ARGUMENT within square brackets."
204 (if (not (string-match "\\S-" argument)) ""
205 (cl-case type
206 (action (if (string-match "\\`<.*>\\'" argument) argument
207 (format "<%s>" argument)))
208 (defaction (cond
209 ((string-match "\\`\\[<.*>\\]\\'" argument) argument)
210 ((string-match "\\`<.*>\\'" argument)
211 (format "[%s]" argument))
212 ((string-match "\\`\\[\\(.*\\)\\]\\'" argument)
213 (format "[<%s>]" (match-string 1 argument)))
214 (t (format "[<%s>]" argument))))
215 (option (if (string-match "\\`\\[.*\\]\\'" argument) argument
216 (format "[%s]" argument)))
217 (otherwise argument))))
219 (defun org-beamer--element-has-overlay-p (element)
220 "Non-nil when ELEMENT has an overlay specified.
221 An element has an overlay specification when it starts with an
222 `beamer' export-snippet whose value is between angular brackets.
223 Return overlay specification, as a string, or nil."
224 (let ((first-object (car (org-element-contents element))))
225 (when (eq (org-element-type first-object) 'export-snippet)
226 (let ((value (org-element-property :value first-object)))
227 (and (string-match "\\`<.*>\\'" value) value)))))
231 ;;; Define Back-End
233 (org-export-define-derived-backend 'beamer 'latex
234 :menu-entry
235 '(?l 1
236 ((?B "As LaTeX buffer (Beamer)" org-beamer-export-as-latex)
237 (?b "As LaTeX file (Beamer)" org-beamer-export-to-latex)
238 (?P "As PDF file (Beamer)" org-beamer-export-to-pdf)
239 (?O "As PDF file and open (Beamer)"
240 (lambda (a s v b)
241 (if a (org-beamer-export-to-pdf t s v b)
242 (org-open-file (org-beamer-export-to-pdf nil s v b)))))))
243 :options-alist
244 '((:headline-levels nil "H" org-beamer-frame-level)
245 (:latex-class "LATEX_CLASS" nil "beamer" t)
246 (:beamer-subtitle-format nil nil org-beamer-subtitle-format)
247 (:beamer-column-view-format "COLUMNS" nil org-beamer-column-view-format)
248 (:beamer-theme "BEAMER_THEME" nil org-beamer-theme)
249 (:beamer-color-theme "BEAMER_COLOR_THEME" nil nil t)
250 (:beamer-font-theme "BEAMER_FONT_THEME" nil nil t)
251 (:beamer-inner-theme "BEAMER_INNER_THEME" nil nil t)
252 (:beamer-outer-theme "BEAMER_OUTER_THEME" nil nil t)
253 (:beamer-header "BEAMER_HEADER" nil nil newline)
254 (:beamer-environments-extra nil nil org-beamer-environments-extra)
255 (:beamer-frame-default-options nil nil org-beamer-frame-default-options)
256 (:beamer-outline-frame-options nil nil org-beamer-outline-frame-options)
257 (:beamer-outline-frame-title nil nil org-beamer-outline-frame-title))
258 :translate-alist '((bold . org-beamer-bold)
259 (export-block . org-beamer-export-block)
260 (export-snippet . org-beamer-export-snippet)
261 (headline . org-beamer-headline)
262 (item . org-beamer-item)
263 (keyword . org-beamer-keyword)
264 (link . org-beamer-link)
265 (plain-list . org-beamer-plain-list)
266 (radio-target . org-beamer-radio-target)
267 (target . org-beamer-target)
268 (template . org-beamer-template)))
272 ;;; Transcode Functions
274 ;;;; Bold
276 (defun org-beamer-bold (bold contents _info)
277 "Transcode BLOCK object into Beamer code.
278 CONTENTS is the text being bold. INFO is a plist used as
279 a communication channel."
280 (format "\\alert%s{%s}"
281 (or (org-beamer--element-has-overlay-p bold) "")
282 contents))
285 ;;;; Export Block
287 (defun org-beamer-export-block (export-block _contents _info)
288 "Transcode an EXPORT-BLOCK element into Beamer code.
289 CONTENTS is nil. INFO is a plist used as a communication
290 channel."
291 (when (member (org-element-property :type export-block) '("BEAMER" "LATEX"))
292 (org-remove-indentation (org-element-property :value export-block))))
295 ;;;; Export Snippet
297 (defun org-beamer-export-snippet (export-snippet _contents info)
298 "Transcode an EXPORT-SNIPPET object into Beamer code.
299 CONTENTS is nil. INFO is a plist used as a communication
300 channel."
301 (let ((backend (org-export-snippet-backend export-snippet))
302 (value (org-element-property :value export-snippet)))
303 ;; Only "latex" and "beamer" snippets are retained.
304 (cond ((eq backend 'latex) value)
305 ;; Ignore "beamer" snippets specifying overlays.
306 ((and (eq backend 'beamer)
307 (or (org-export-get-previous-element export-snippet info)
308 (not (string-match "\\`<.*>\\'" value))))
309 value))))
312 ;;;; Headline
314 ;; The main function to translate a headline is
315 ;; `org-beamer-headline'.
317 ;; Depending on the level at which a headline is considered as
318 ;; a frame (given by `org-beamer--frame-level'), the headline is
319 ;; either a section (`org-beamer--format-section'), a frame
320 ;; (`org-beamer--format-frame') or a block
321 ;; (`org-beamer--format-block').
323 ;; `org-beamer-headline' also takes care of special environments
324 ;; like "ignoreheading", "note", "noteNH", "appendix" and
325 ;; "againframe".
327 (defun org-beamer--get-label (headline info)
328 "Return label for HEADLINE, as a string.
330 INFO is a plist used as a communication channel.
332 The value is either the label specified in \"BEAMER_opt\"
333 property, the custom ID, if there is one and
334 `:latex-prefer-user-labels' property has a non nil value, or
335 a unique internal label. This function assumes HEADLINE will be
336 treated as a frame."
337 (cond
338 ((let ((opt (org-element-property :BEAMER_OPT headline)))
339 (and (stringp opt)
340 (string-match "\\(?:^\\|,\\)label=\\(.*?\\)\\(?:$\\|,\\)" opt)
341 (let ((label (match-string 1 opt)))
342 (if (string-match-p "\\`{.*}\\'" label)
343 (substring label 1 -1)
344 label)))))
345 ((and (plist-get info :latex-prefer-user-labels)
346 (org-element-property :CUSTOM_ID headline)))
347 (t (format "sec:%s" (org-export-get-reference headline info)))))
349 (defun org-beamer--frame-level (headline info)
350 "Return frame level in subtree containing HEADLINE.
351 INFO is a plist used as a communication channel."
353 ;; 1. Look for "frame" environment in parents, starting from the
354 ;; farthest.
355 (catch 'exit
356 (dolist (parent (nreverse (org-element-lineage headline)))
357 (let ((env (org-element-property :BEAMER_ENV parent)))
358 (when (and env (member-ignore-case env '("frame" "fullframe")))
359 (throw 'exit (org-export-get-relative-level parent info))))))
360 ;; 2. Look for "frame" environment in HEADLINE.
361 (let ((env (org-element-property :BEAMER_ENV headline)))
362 (and env (member-ignore-case env '("frame" "fullframe"))
363 (org-export-get-relative-level headline info)))
364 ;; 3. Look for "frame" environment in sub-tree.
365 (org-element-map headline 'headline
366 (lambda (hl)
367 (let ((env (org-element-property :BEAMER_ENV hl)))
368 (when (and env (member-ignore-case env '("frame" "fullframe")))
369 (org-export-get-relative-level hl info))))
370 info 'first-match)
371 ;; 4. No "frame" environment in tree: use default value.
372 (plist-get info :headline-levels)))
374 (defun org-beamer--format-section (headline contents info)
375 "Format HEADLINE as a sectioning part.
376 CONTENTS holds the contents of the headline. INFO is a plist
377 used as a communication channel."
378 (let ((latex-headline
379 (org-export-with-backend
380 ;; We create a temporary export back-end which behaves the
381 ;; same as current one, but adds "\protect" in front of the
382 ;; output of some objects.
383 (org-export-create-backend
384 :parent 'latex
385 :transcoders
386 (let ((protected-output
387 (function
388 (lambda (object contents info)
389 (let ((code (org-export-with-backend
390 'beamer object contents info)))
391 (if (org-string-nw-p code) (concat "\\protect" code)
392 code))))))
393 (mapcar #'(lambda (type) (cons type protected-output))
394 '(bold footnote-reference italic strike-through timestamp
395 underline))))
396 headline
397 contents
398 info))
399 (mode-specs (org-element-property :BEAMER_ACT headline)))
400 (if (and mode-specs
401 (string-match "\\`\\\\\\(.*?\\)\\(?:\\*\\|\\[.*\\]\\)?{"
402 latex-headline))
403 ;; Insert overlay specifications.
404 (replace-match (concat (match-string 1 latex-headline)
405 (format "<%s>" mode-specs))
406 nil nil latex-headline 1)
407 latex-headline)))
409 (defun org-beamer--format-frame (headline contents info)
410 "Format HEADLINE as a frame.
411 CONTENTS holds the contents of the headline. INFO is a plist
412 used as a communication channel."
413 (let ((fragilep
414 ;; FRAGILEP is non-nil when HEADLINE contains an element
415 ;; among `org-beamer-verbatim-elements'.
416 (org-element-map headline org-beamer-verbatim-elements 'identity
417 info 'first-match)))
418 (concat "\\begin{frame}"
419 ;; Overlay specification, if any. When surrounded by
420 ;; square brackets, consider it as a default
421 ;; specification.
422 (let ((action (org-element-property :BEAMER_ACT headline)))
423 (cond
424 ((not action) "")
425 ((string-match "\\`\\[.*\\]\\'" action )
426 (org-beamer--normalize-argument action 'defaction))
427 (t (org-beamer--normalize-argument action 'action))))
428 ;; Options, if any.
429 (let* ((beamer-opt (org-element-property :BEAMER_OPT headline))
430 (options
431 ;; Collect options from default value and headline's
432 ;; properties. Also add a label for links.
433 (append
434 (org-split-string
435 (plist-get info :beamer-frame-default-options) ",")
436 (and beamer-opt
437 (org-split-string
438 ;; Remove square brackets if user provided
439 ;; them.
440 (and (string-match "^\\[?\\(.*\\)\\]?$" beamer-opt)
441 (match-string 1 beamer-opt))
442 ","))
443 ;; Provide an automatic label for the frame
444 ;; unless the user specified one. Also refrain
445 ;; from labeling `allowframebreaks' frames; this
446 ;; is not allowed by beamer.
447 (unless (and beamer-opt
448 (or (string-match "\\(^\\|,\\)label=" beamer-opt)
449 (string-match "allowframebreaks" beamer-opt)))
450 (list
451 (let ((label (org-beamer--get-label headline info)))
452 ;; Labels containing colons need to be
453 ;; wrapped within braces.
454 (format (if (org-string-match-p ":" label)
455 "label={%s}"
456 "label=%s")
457 label)))))))
458 ;; Change options list into a string.
459 (org-beamer--normalize-argument
460 (mapconcat
461 'identity
462 (if (or (not fragilep) (member "fragile" options)) options
463 (cons "fragile" options))
464 ",")
465 'option))
466 ;; Title.
467 (let ((env (org-element-property :BEAMER_ENV headline)))
468 (format "{%s}"
469 (if (and env (equal (downcase env) "fullframe")) ""
470 (org-export-data
471 (org-element-property :title headline) info))))
472 "\n"
473 ;; The following workaround is required in fragile frames
474 ;; as Beamer will append "\par" to the beginning of the
475 ;; contents. So we need to make sure the command is
476 ;; separated from the contents by at least one space. If
477 ;; it isn't, it will create "\parfirst-word" command and
478 ;; remove the first word from the contents in the PDF
479 ;; output.
480 (if (not fragilep) contents
481 (replace-regexp-in-string "\\`\n*" "\\& " (or contents "")))
482 "\\end{frame}")))
484 (defun org-beamer--format-block (headline contents info)
485 "Format HEADLINE as a block.
486 CONTENTS holds the contents of the headline. INFO is a plist
487 used as a communication channel."
488 (let* ((column-width (org-element-property :BEAMER_COL headline))
489 ;; ENVIRONMENT defaults to "block" if none is specified and
490 ;; there is no column specification. If there is a column
491 ;; specified but still no explicit environment, ENVIRONMENT
492 ;; is "column".
493 (environment (let ((env (org-element-property :BEAMER_ENV headline)))
494 (cond
495 ;; "block" is the fallback environment.
496 ((and (not env) (not column-width)) "block")
497 ;; "column" only.
498 ((not env) "column")
499 ;; Use specified environment.
500 (t env))))
501 (raw-title (org-element-property :raw-value headline))
502 (env-format
503 (cond ((member environment '("column" "columns")) nil)
504 ((assoc environment
505 (append (plist-get info :beamer-environments-extra)
506 org-beamer-environments-default)))
507 (t (user-error "Wrong block type at a headline named \"%s\""
508 raw-title))))
509 (title (org-export-data (org-element-property :title headline) info))
510 (raw-options (org-element-property :BEAMER_OPT headline))
511 (options (if raw-options
512 (org-beamer--normalize-argument raw-options 'option)
513 ""))
514 ;; Start a "columns" environment when explicitly requested or
515 ;; when there is no previous headline or the previous
516 ;; headline do not have a BEAMER_column property.
517 (parent-env (org-element-property
518 :BEAMER_ENV (org-export-get-parent-headline headline)))
519 (start-columns-p
520 (or (equal environment "columns")
521 (and column-width
522 (not (and parent-env
523 (equal (downcase parent-env) "columns")))
524 (or (org-export-first-sibling-p headline info)
525 (not (org-element-property
526 :BEAMER_COL
527 (org-export-get-previous-element
528 headline info)))))))
529 ;; End the "columns" environment when explicitly requested or
530 ;; when there is no next headline or the next headline do not
531 ;; have a BEAMER_column property.
532 (end-columns-p
533 (or (equal environment "columns")
534 (and column-width
535 (not (and parent-env
536 (equal (downcase parent-env) "columns")))
537 (or (org-export-last-sibling-p headline info)
538 (not (org-element-property
539 :BEAMER_COL
540 (org-export-get-next-element headline info))))))))
541 (concat
542 (when start-columns-p
543 ;; Column can accept options only when the environment is
544 ;; explicitly defined.
545 (if (not (equal environment "columns")) "\\begin{columns}\n"
546 (format "\\begin{columns}%s\n" options)))
547 (when column-width
548 (format "\\begin{column}%s{%s}\n"
549 ;; One can specify placement for column only when
550 ;; HEADLINE stands for a column on its own.
551 (if (equal environment "column") options "")
552 (format "%s\\columnwidth" column-width)))
553 ;; Block's opening string.
554 (when (nth 2 env-format)
555 (concat
556 (org-fill-template
557 (nth 2 env-format)
558 (nconc
559 ;; If BEAMER_act property has its value enclosed in square
560 ;; brackets, it is a default overlay specification and
561 ;; overlay specification is empty. Otherwise, it is an
562 ;; overlay specification and the default one is nil.
563 (let ((action (org-element-property :BEAMER_ACT headline)))
564 (cond
565 ((not action) (list (cons "a" "") (cons "A" "") (cons "R" "")))
566 ((string-match "\\`\\[.*\\]\\'" action)
567 (list
568 (cons "A" (org-beamer--normalize-argument action 'defaction))
569 (cons "a" "")
570 (cons "R" action)))
572 (list (cons "a" (org-beamer--normalize-argument action 'action))
573 (cons "A" "")
574 (cons "R" action)))))
575 (list (cons "o" options)
576 (cons "O" (or raw-options ""))
577 (cons "h" title)
578 (cons "r" raw-title)
579 (cons "H" (if (equal raw-title "") ""
580 (format "{%s}" raw-title)))
581 (cons "U" (if (equal raw-title "") ""
582 (format "[%s]" raw-title))))))
583 "\n"))
584 contents
585 ;; Block's closing string, if any.
586 (and (nth 3 env-format) (concat (nth 3 env-format) "\n"))
587 (when column-width "\\end{column}\n")
588 (when end-columns-p "\\end{columns}"))))
590 (defun org-beamer-headline (headline contents info)
591 "Transcode HEADLINE element into Beamer code.
592 CONTENTS is the contents of the headline. INFO is a plist used
593 as a communication channel."
594 (unless (org-element-property :footnote-section-p headline)
595 (let ((level (org-export-get-relative-level headline info))
596 (frame-level (org-beamer--frame-level headline info))
597 (environment (let ((env (org-element-property :BEAMER_ENV headline)))
598 (or (org-string-nw-p env) "block"))))
599 (cond
600 ;; Case 1: Resume frame specified by "BEAMER_ref" property.
601 ((equal environment "againframe")
602 (let ((ref (org-element-property :BEAMER_REF headline)))
603 ;; Reference to frame being resumed is mandatory. Ignore
604 ;; the whole headline if it isn't provided.
605 (when (org-string-nw-p ref)
606 (concat "\\againframe"
607 ;; Overlay specification.
608 (let ((overlay (org-element-property :BEAMER_ACT headline)))
609 (when overlay
610 (org-beamer--normalize-argument
611 overlay
612 (if (string-match "\\`\\[.*\\]\\'" overlay) 'defaction
613 'action))))
614 ;; Options.
615 (let ((options (org-element-property :BEAMER_OPT headline)))
616 (when options
617 (org-beamer--normalize-argument options 'option)))
618 ;; Resolve reference provided by "BEAMER_ref"
619 ;; property. This is done by building a minimal
620 ;; fake link and calling the appropriate resolve
621 ;; function, depending on the reference syntax.
622 (let ((target
623 (if (string-match "\\`\\(id:\\|#\\)" ref)
624 (org-export-resolve-id-link
625 `(link (:path ,(substring ref (match-end 0))))
626 info)
627 (org-export-resolve-fuzzy-link
628 `(link (:path
629 ;; Look for headlines only.
630 ,(if (eq (string-to-char ref) ?*) ref
631 (concat "*" ref))))
632 info))))
633 ;; Now use user-defined label provided in TARGET
634 ;; headline, or fallback to standard one.
635 (format "{%s}" (org-beamer--get-label target info)))))))
636 ;; Case 2: Creation of an appendix is requested.
637 ((equal environment "appendix")
638 (concat "\\appendix"
639 (org-element-property :BEAMER_ACT headline)
640 "\n"
641 (make-string (org-element-property :pre-blank headline) ?\n)
642 contents))
643 ;; Case 3: Ignore heading.
644 ((equal environment "ignoreheading")
645 (concat (make-string (org-element-property :pre-blank headline) ?\n)
646 contents))
647 ;; Case 4: HEADLINE is a note.
648 ((member environment '("note" "noteNH"))
649 (format "\\note{%s}"
650 (concat (and (equal environment "note")
651 (concat
652 (org-export-data
653 (org-element-property :title headline) info)
654 "\n"))
655 (org-trim contents))))
656 ;; Case 5: HEADLINE is a frame.
657 ((= level frame-level)
658 (org-beamer--format-frame headline contents info))
659 ;; Case 6: Regular section, extracted from
660 ;; `org-latex-classes'.
661 ((< level frame-level)
662 (org-beamer--format-section headline contents info))
663 ;; Case 7: Otherwise, HEADLINE is a block.
664 (t (org-beamer--format-block headline contents info))))))
667 ;;;; Item
669 (defun org-beamer-item (item contents info)
670 "Transcode an ITEM element into Beamer code.
671 CONTENTS holds the contents of the item. INFO is a plist holding
672 contextual information."
673 (org-export-with-backend
674 ;; Delegate item export to `latex'. However, we use `beamer'
675 ;; transcoders for objects in the description tag.
676 (org-export-create-backend
677 :parent 'beamer
678 :transcoders
679 (list
680 (cons
681 'item
682 (lambda (item _c _i)
683 (let ((action
684 (let ((first (car (org-element-contents item))))
685 (and (eq (org-element-type first) 'paragraph)
686 (org-beamer--element-has-overlay-p first))))
687 (output (org-latex-item item contents info)))
688 (if (not (and action (string-match "\\\\item" output))) output
689 ;; If the item starts with a paragraph and that paragraph
690 ;; starts with an export snippet specifying an overlay,
691 ;; append it to the \item command.
692 (replace-match (concat "\\\\item" action) nil nil output)))))))
693 item contents info))
696 ;;;; Keyword
698 (defun org-beamer-keyword (keyword contents info)
699 "Transcode a KEYWORD element into Beamer code.
700 CONTENTS is nil. INFO is a plist used as a communication
701 channel."
702 (let ((key (org-element-property :key keyword))
703 (value (org-element-property :value keyword)))
704 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
705 ;; Otherwise, fallback to `latex' back-end.
706 (cond
707 ((equal key "BEAMER") value)
708 ((and (equal key "TOC") (string-match "\\<headlines\\>" value))
709 (let ((depth (or (and (string-match "[0-9]+" value)
710 (string-to-number (match-string 0 value)))
711 (plist-get info :with-toc)))
712 (options (and (string-match "\\[.*?\\]" value)
713 (match-string 0 value))))
714 (concat
715 (when (wholenump depth) (format "\\setcounter{tocdepth}{%s}\n" depth))
716 "\\tableofcontents" options)))
717 (t (org-export-with-backend 'latex keyword contents info)))))
720 ;;;; Link
722 (defun org-beamer-link (link contents info)
723 "Transcode a LINK object into Beamer code.
724 CONTENTS is the description part of the link. INFO is a plist
725 used as a communication channel."
726 (let ((type (org-element-property :type link)))
727 (cond
728 ;; Link type is handled by a special function.
729 ((org-export-custom-protocol-maybe link contents 'beamer))
730 ;; Use \hyperlink command for all internal links.
731 ((equal type "radio")
732 (let ((destination (org-export-resolve-radio-link link info)))
733 (if (not destination) contents
734 (format "\\hyperlink%s{%s}{%s}"
735 (or (org-beamer--element-has-overlay-p link) "")
736 (org-export-get-reference destination info)
737 contents))))
738 ((and (member type '("custom-id" "fuzzy" "id"))
739 (let ((destination (if (string= type "fuzzy")
740 (org-export-resolve-fuzzy-link link info)
741 (org-export-resolve-id-link link info))))
742 (cl-case (org-element-type destination)
743 (headline
744 (let ((label
745 (format "sec-%s"
746 (mapconcat
747 'number-to-string
748 (org-export-get-headline-number
749 destination info)
750 "-"))))
751 (if (and (plist-get info :section-numbers) (not contents))
752 (format "\\ref{%s}" label)
753 (format "\\hyperlink%s{%s}{%s}"
754 (or (org-beamer--element-has-overlay-p link) "")
755 label
756 contents))))
757 (target
758 (let ((ref (org-export-get-reference destination info)))
759 (if (not contents) (format "\\ref{%s}" ref)
760 (format "\\hyperlink%s{%s}{%s}"
761 (or (org-beamer--element-has-overlay-p link) "")
763 contents))))))))
764 ;; Otherwise, use `latex' back-end.
765 (t (org-export-with-backend 'latex link contents info)))))
768 ;;;; Plain List
770 ;; Plain lists support `:environment', `:overlay' and `:options'
771 ;; attributes.
773 (defun org-beamer-plain-list (plain-list contents info)
774 "Transcode a PLAIN-LIST element into Beamer code.
775 CONTENTS is the contents of the list. INFO is a plist holding
776 contextual information."
777 (let* ((type (org-element-property :type plain-list))
778 (attributes (org-combine-plists
779 (org-export-read-attribute :attr_latex plain-list)
780 (org-export-read-attribute :attr_beamer plain-list)))
781 (latex-type (let ((env (plist-get attributes :environment)))
782 (cond (env)
783 ((eq type 'ordered) "enumerate")
784 ((eq type 'descriptive) "description")
785 (t "itemize")))))
786 (org-latex--wrap-label
787 plain-list
788 (format "\\begin{%s}%s%s\n%s\\end{%s}"
789 latex-type
790 ;; Default overlay specification, if any.
791 (org-beamer--normalize-argument
792 (or (plist-get attributes :overlay) "")
793 'defaction)
794 ;; Second optional argument depends on the list type.
795 (org-beamer--normalize-argument
796 (or (plist-get attributes :options) "")
797 'option)
798 ;; Eventually insert contents and close environment.
799 contents
800 latex-type)
801 info)))
804 ;;;; Radio Target
806 (defun org-beamer-radio-target (radio-target text info)
807 "Transcode a RADIO-TARGET object into Beamer code.
808 TEXT is the text of the target. INFO is a plist holding
809 contextual information."
810 (format "\\hypertarget%s{%s}{%s}"
811 (or (org-beamer--element-has-overlay-p radio-target) "")
812 (org-export-get-reference radio-target info)
813 text))
816 ;;;; Target
818 (defun org-beamer-target (target _contents info)
819 "Transcode a TARGET object into Beamer code.
820 CONTENTS is nil. INFO is a plist holding contextual
821 information."
822 (format "\\label{%s}" (org-export-get-reference target info)))
825 ;;;; Template
827 ;; Template used is similar to the one used in `latex' back-end,
828 ;; excepted for the table of contents and Beamer themes.
830 (defun org-beamer-template (contents info)
831 "Return complete document string after Beamer conversion.
832 CONTENTS is the transcoded contents string. INFO is a plist
833 holding export options."
834 (let ((title (org-export-data (plist-get info :title) info))
835 (subtitle (org-export-data (plist-get info :subtitle) info)))
836 (concat
837 ;; Time-stamp.
838 (and (plist-get info :time-stamp-file)
839 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
840 ;; LaTeX compiler
841 (org-latex--insert-compiler info)
842 ;; Document class and packages.
843 (org-latex--make-preamble info)
844 ;; Insert themes.
845 (let ((format-theme
846 (function
847 (lambda (prop command)
848 (let ((theme (plist-get info prop)))
849 (when theme
850 (concat command
851 (if (not (string-match "\\[.*\\]" theme))
852 (format "{%s}\n" theme)
853 (format "%s{%s}\n"
854 (match-string 0 theme)
855 (org-trim
856 (replace-match "" nil nil theme)))))))))))
857 (mapconcat (lambda (args) (apply format-theme args))
858 '((:beamer-theme "\\usetheme")
859 (:beamer-color-theme "\\usecolortheme")
860 (:beamer-font-theme "\\usefonttheme")
861 (:beamer-inner-theme "\\useinnertheme")
862 (:beamer-outer-theme "\\useoutertheme"))
863 ""))
864 ;; Possibly limit depth for headline numbering.
865 (let ((sec-num (plist-get info :section-numbers)))
866 (when (integerp sec-num)
867 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
868 ;; Author.
869 (let ((author (and (plist-get info :with-author)
870 (let ((auth (plist-get info :author)))
871 (and auth (org-export-data auth info)))))
872 (email (and (plist-get info :with-email)
873 (org-export-data (plist-get info :email) info))))
874 (cond ((and author email (not (string= "" email)))
875 (format "\\author{%s\\thanks{%s}}\n" author email))
876 ((or author email) (format "\\author{%s}\n" (or author email)))))
877 ;; Date.
878 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
879 (format "\\date{%s}\n" (org-export-data date info)))
880 ;; Title
881 (format "\\title{%s}\n" title)
882 (when (org-string-nw-p subtitle)
883 (concat (format (plist-get info :beamer-subtitle-format) subtitle) "\n"))
884 ;; Beamer-header
885 (let ((beamer-header (plist-get info :beamer-header)))
886 (when beamer-header
887 (format "%s\n" (plist-get info :beamer-header))))
888 ;; 9. Hyperref options.
889 (let ((template (plist-get info :latex-hyperref-template)))
890 (and (stringp template)
891 (format-spec template (org-latex--format-spec info))))
892 ;; Document start.
893 "\\begin{document}\n\n"
894 ;; Title command.
895 (org-element-normalize-string
896 (cond ((not (plist-get info :with-title)) nil)
897 ((string= "" title) nil)
898 ((not (stringp org-latex-title-command)) nil)
899 ((string-match "\\(?:[^%]\\|^\\)%s"
900 org-latex-title-command)
901 (format org-latex-title-command title))
902 (t org-latex-title-command)))
903 ;; Table of contents.
904 (let ((depth (plist-get info :with-toc)))
905 (when depth
906 (concat
907 (format "\\begin{frame}%s{%s}\n"
908 (org-beamer--normalize-argument
909 (plist-get info :beamer-outline-frame-options) 'option)
910 (plist-get info :beamer-outline-frame-title))
911 (when (wholenump depth)
912 (format "\\setcounter{tocdepth}{%d}\n" depth))
913 "\\tableofcontents\n"
914 "\\end{frame}\n\n")))
915 ;; Document's body.
916 contents
917 ;; Creator.
918 (if (plist-get info :with-creator)
919 (concat (plist-get info :creator) "\n")
921 ;; Document end.
922 "\\end{document}")))
926 ;;; Minor Mode
929 (defvar org-beamer-mode-map (make-sparse-keymap)
930 "The keymap for `org-beamer-mode'.")
931 (define-key org-beamer-mode-map "\C-c\C-b" 'org-beamer-select-environment)
933 ;;;###autoload
934 (define-minor-mode org-beamer-mode
935 "Support for editing Beamer oriented Org mode files."
936 nil " Bm" 'org-beamer-mode-map)
938 (when (fboundp 'font-lock-add-keywords)
939 (font-lock-add-keywords
940 'org-mode
941 '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-beamer-tag prepend))
942 'prepend))
944 (defface org-beamer-tag '((t (:box (:line-width 1 :color grey40))))
945 "The special face for beamer tags."
946 :group 'org-export-beamer)
948 (defun org-beamer-property-changed (property value)
949 "Track the BEAMER_env property with tags.
950 PROPERTY is the name of the modified property. VALUE is its new
951 value."
952 (cond
953 ((equal property "BEAMER_env")
954 (save-excursion
955 (org-back-to-heading t)
956 ;; Filter out Beamer-related tags and install environment tag.
957 (let ((tags (cl-remove-if (lambda (x) (string-match "^B_" x))
958 (org-get-tags)))
959 (env-tag (and (org-string-nw-p value) (concat "B_" value))))
960 (org-set-tags-to (if env-tag (cons env-tag tags) tags))
961 (when env-tag (org-toggle-tag env-tag 'on)))))
962 ((equal property "BEAMER_col")
963 (org-toggle-tag "BMCOL" (if (org-string-nw-p value) 'on 'off)))))
965 (add-hook 'org-property-changed-functions 'org-beamer-property-changed)
967 (defun org-beamer-allowed-property-values (property)
968 "Supply allowed values for PROPERTY."
969 (cond
970 ((and (equal property "BEAMER_env")
971 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
972 ;; If no allowed values for BEAMER_env have been defined,
973 ;; supply all defined environments
974 (mapcar 'car (append org-beamer-environments-special
975 org-beamer-environments-extra
976 org-beamer-environments-default)))
977 ((and (equal property "BEAMER_col")
978 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
979 ;; If no allowed values for BEAMER_col have been defined,
980 ;; supply some
981 (org-split-string org-beamer-column-widths " "))))
983 (add-hook 'org-property-allowed-value-functions
984 'org-beamer-allowed-property-values)
988 ;;; Commands
990 ;;;###autoload
991 (defun org-beamer-export-as-latex
992 (&optional async subtreep visible-only body-only ext-plist)
993 "Export current buffer as a Beamer buffer.
995 If narrowing is active in the current buffer, only export its
996 narrowed part.
998 If a region is active, export that region.
1000 A non-nil optional argument ASYNC means the process should happen
1001 asynchronously. The resulting buffer should be accessible
1002 through the `org-export-stack' interface.
1004 When optional argument SUBTREEP is non-nil, export the sub-tree
1005 at point, extracting information from the headline properties
1006 first.
1008 When optional argument VISIBLE-ONLY is non-nil, don't export
1009 contents of hidden elements.
1011 When optional argument BODY-ONLY is non-nil, only write code
1012 between \"\\begin{document}\" and \"\\end{document}\".
1014 EXT-PLIST, when provided, is a property list with external
1015 parameters overriding Org default settings, but still inferior to
1016 file-local settings.
1018 Export is done in a buffer named \"*Org BEAMER Export*\", which
1019 will be displayed when `org-export-show-temporary-export-buffer'
1020 is non-nil."
1021 (interactive)
1022 (org-export-to-buffer 'beamer "*Org BEAMER Export*"
1023 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
1025 ;;;###autoload
1026 (defun org-beamer-export-to-latex
1027 (&optional async subtreep visible-only body-only ext-plist)
1028 "Export current buffer as a Beamer presentation (tex).
1030 If narrowing is active in the current buffer, only export its
1031 narrowed part.
1033 If a region is active, export that region.
1035 A non-nil optional argument ASYNC means the process should happen
1036 asynchronously. The resulting file should be accessible through
1037 the `org-export-stack' interface.
1039 When optional argument SUBTREEP is non-nil, export the sub-tree
1040 at point, extracting information from the headline properties
1041 first.
1043 When optional argument VISIBLE-ONLY is non-nil, don't export
1044 contents of hidden elements.
1046 When optional argument BODY-ONLY is non-nil, only write code
1047 between \"\\begin{document}\" and \"\\end{document}\".
1049 EXT-PLIST, when provided, is a property list with external
1050 parameters overriding Org default settings, but still inferior to
1051 file-local settings.
1053 Return output file's name."
1054 (interactive)
1055 (let ((file (org-export-output-file-name ".tex" subtreep)))
1056 (org-export-to-file 'beamer file
1057 async subtreep visible-only body-only ext-plist)))
1059 ;;;###autoload
1060 (defun org-beamer-export-to-pdf
1061 (&optional async subtreep visible-only body-only ext-plist)
1062 "Export current buffer as a Beamer presentation (PDF).
1064 If narrowing is active in the current buffer, only export its
1065 narrowed part.
1067 If a region is active, export that region.
1069 A non-nil optional argument ASYNC means the process should happen
1070 asynchronously. The resulting file should be accessible through
1071 the `org-export-stack' interface.
1073 When optional argument SUBTREEP is non-nil, export the sub-tree
1074 at point, extracting information from the headline properties
1075 first.
1077 When optional argument VISIBLE-ONLY is non-nil, don't export
1078 contents of hidden elements.
1080 When optional argument BODY-ONLY is non-nil, only write code
1081 between \"\\begin{document}\" and \"\\end{document}\".
1083 EXT-PLIST, when provided, is a property list with external
1084 parameters overriding Org default settings, but still inferior to
1085 file-local settings.
1087 Return PDF file's name."
1088 (interactive)
1089 (let ((file (org-export-output-file-name ".tex" subtreep)))
1090 (org-export-to-file 'beamer file
1091 async subtreep visible-only body-only ext-plist
1092 (lambda (file) (org-latex-compile file)))))
1094 ;;;###autoload
1095 (defun org-beamer-select-environment ()
1096 "Select the environment to be used by beamer for this entry.
1097 While this uses (for convenience) a tag selection interface, the
1098 result of this command will be that the BEAMER_env *property* of
1099 the entry is set.
1101 In addition to this, the command will also set a tag as a visual
1102 aid, but the tag does not have any semantic meaning."
1103 (interactive)
1104 ;; Make sure `org-beamer-environments-special' has a higher
1105 ;; priority than `org-beamer-environments-extra'.
1106 (let* ((envs (append org-beamer-environments-special
1107 org-beamer-environments-extra
1108 org-beamer-environments-default))
1109 (org-current-tag-alist
1110 (append '((:startgroup))
1111 (mapcar (lambda (e) (cons (concat "B_" (car e))
1112 (string-to-char (nth 1 e))))
1113 envs)
1114 '((:endgroup))
1115 '(("BMCOL" . ?|))))
1116 (org-tag-persistent-alist nil)
1117 (org-use-fast-tag-selection t)
1118 (org-fast-tag-selection-single-key t))
1119 (org-set-tags)
1120 (let ((tags (or (ignore-errors (org-get-tags-string)) "")))
1121 (cond
1122 ;; For a column, automatically ask for its width.
1123 ((eq org-last-tag-selection-key ?|)
1124 (if (string-match ":BMCOL:" tags)
1125 (org-set-property "BEAMER_col" (read-string "Column width: "))
1126 (org-delete-property "BEAMER_col")))
1127 ;; For an "againframe" section, automatically ask for reference
1128 ;; to resumed frame and overlay specifications.
1129 ((eq org-last-tag-selection-key ?A)
1130 (if (equal (org-entry-get nil "BEAMER_env") "againframe")
1131 (progn (org-entry-delete nil "BEAMER_env")
1132 (org-entry-delete nil "BEAMER_ref")
1133 (org-entry-delete nil "BEAMER_act"))
1134 (org-entry-put nil "BEAMER_env" "againframe")
1135 (org-set-property
1136 "BEAMER_ref"
1137 (read-string "Frame reference (*Title, #custom-id, id:...): "))
1138 (org-set-property "BEAMER_act"
1139 (read-string "Overlay specification: "))))
1140 ((string-match (concat ":B_\\(" (mapconcat 'car envs "\\|") "\\):") tags)
1141 (org-entry-put nil "BEAMER_env" (match-string 1 tags)))
1142 (t (org-entry-delete nil "BEAMER_env"))))))
1144 ;;;###autoload
1145 (defun org-beamer-publish-to-latex (plist filename pub-dir)
1146 "Publish an Org file to a Beamer presentation (LaTeX).
1148 FILENAME is the filename of the Org file to be published. PLIST
1149 is the property list for the given project. PUB-DIR is the
1150 publishing directory.
1152 Return output file name."
1153 (org-publish-org-to 'beamer filename ".tex" plist pub-dir))
1155 ;;;###autoload
1156 (defun org-beamer-publish-to-pdf (plist filename pub-dir)
1157 "Publish an Org file to a Beamer presentation (PDF, via LaTeX).
1159 FILENAME is the filename of the Org file to be published. PLIST
1160 is the property list for the given project. PUB-DIR is the
1161 publishing directory.
1163 Return output file name."
1164 ;; Unlike to `org-beamer-publish-to-latex', PDF file is generated in
1165 ;; working directory and then moved to publishing directory.
1166 (org-publish-attachment
1167 plist
1168 (org-latex-compile
1169 (org-publish-org-to
1170 'beamer filename ".tex" plist (file-name-directory filename)))
1171 pub-dir))
1174 (provide 'ox-beamer)
1176 ;; Local variables:
1177 ;; generated-autoload-file: "org-loaddefs.el"
1178 ;; End:
1180 ;;; ox-beamer.el ends here