lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / lisp / ox-beamer.el
blob7811dd6b5cf899887bc5df89f33833e3f7c70db6
1 ;;; ox-beamer.el --- Beamer Back-End for Org Export Engine -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2007-2019 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 <https://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 "26.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[%h]" "\\end{theorem}")
178 ("definition" "d" "\\begin{definition}%a[%h]" "\\end{definition}")
179 ("example" "e" "\\begin{example}%a[%h]" "\\end{example}")
180 ("exampleblock" "E" "\\begin{exampleblock}%a{%h}" "\\end{exampleblock}")
181 ("proof" "p" "\\begin{proof}%a[%h]" "\\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 (format "<%s>" (org-unbracket-string "<" ">" argument)))
207 (defaction
208 (format "[<%s>]"
209 (org-unbracket-string "<" ">" (org-unbracket-string "[" "]" argument))))
210 (option (format "[%s]" (org-unbracket-string "[" "]" argument)))
211 (otherwise (error "Invalid `type' argument to `org-beamer--normalize-argument': %s"
212 type)))))
214 (defun org-beamer--element-has-overlay-p (element)
215 "Non-nil when ELEMENT has an overlay specified.
216 An element has an overlay specification when it starts with an
217 `beamer' export-snippet whose value is between angular brackets.
218 Return overlay specification, as a string, or nil."
219 (let ((first-object (car (org-element-contents element))))
220 (when (eq (org-element-type first-object) 'export-snippet)
221 (let ((value (org-element-property :value first-object)))
222 (and (string-prefix-p "<" value) (string-suffix-p ">" value)
223 value)))))
227 ;;; Define Back-End
229 (org-export-define-derived-backend 'beamer 'latex
230 :menu-entry
231 '(?l 1
232 ((?B "As LaTeX buffer (Beamer)" org-beamer-export-as-latex)
233 (?b "As LaTeX file (Beamer)" org-beamer-export-to-latex)
234 (?P "As PDF file (Beamer)" org-beamer-export-to-pdf)
235 (?O "As PDF file and open (Beamer)"
236 (lambda (a s v b)
237 (if a (org-beamer-export-to-pdf t s v b)
238 (org-open-file (org-beamer-export-to-pdf nil s v b)))))))
239 :options-alist
240 '((:headline-levels nil "H" org-beamer-frame-level)
241 (:latex-class "LATEX_CLASS" nil "beamer" t)
242 (:beamer-subtitle-format nil nil org-beamer-subtitle-format)
243 (:beamer-column-view-format "COLUMNS" nil org-beamer-column-view-format)
244 (:beamer-theme "BEAMER_THEME" nil org-beamer-theme)
245 (:beamer-color-theme "BEAMER_COLOR_THEME" nil nil t)
246 (:beamer-font-theme "BEAMER_FONT_THEME" nil nil t)
247 (:beamer-inner-theme "BEAMER_INNER_THEME" nil nil t)
248 (:beamer-outer-theme "BEAMER_OUTER_THEME" nil nil t)
249 (:beamer-header "BEAMER_HEADER" nil nil newline)
250 (:beamer-environments-extra nil nil org-beamer-environments-extra)
251 (:beamer-frame-default-options nil nil org-beamer-frame-default-options)
252 (:beamer-outline-frame-options nil nil org-beamer-outline-frame-options)
253 (:beamer-outline-frame-title nil nil org-beamer-outline-frame-title))
254 :translate-alist '((bold . org-beamer-bold)
255 (export-block . org-beamer-export-block)
256 (export-snippet . org-beamer-export-snippet)
257 (headline . org-beamer-headline)
258 (item . org-beamer-item)
259 (keyword . org-beamer-keyword)
260 (link . org-beamer-link)
261 (plain-list . org-beamer-plain-list)
262 (radio-target . org-beamer-radio-target)
263 (template . org-beamer-template)))
267 ;;; Transcode Functions
269 ;;;; Bold
271 (defun org-beamer-bold (bold contents _info)
272 "Transcode BLOCK object into Beamer code.
273 CONTENTS is the text being bold. INFO is a plist used as
274 a communication channel."
275 (format "\\alert%s{%s}"
276 (or (org-beamer--element-has-overlay-p bold) "")
277 contents))
280 ;;;; Export Block
282 (defun org-beamer-export-block (export-block _contents _info)
283 "Transcode an EXPORT-BLOCK element into Beamer code.
284 CONTENTS is nil. INFO is a plist used as a communication
285 channel."
286 (when (member (org-element-property :type export-block) '("BEAMER" "LATEX"))
287 (org-remove-indentation (org-element-property :value export-block))))
290 ;;;; Export Snippet
292 (defun org-beamer-export-snippet (export-snippet _contents info)
293 "Transcode an EXPORT-SNIPPET object into Beamer code.
294 CONTENTS is nil. INFO is a plist used as a communication
295 channel."
296 (let ((backend (org-export-snippet-backend export-snippet))
297 (value (org-element-property :value export-snippet)))
298 ;; Only "latex" and "beamer" snippets are retained.
299 (cond ((eq backend 'latex) value)
300 ;; Ignore "beamer" snippets specifying overlays.
301 ((and (eq backend 'beamer)
302 (or (org-export-get-previous-element export-snippet info)
303 (not (string-match "\\`<.*>\\'" value))))
304 value))))
307 ;;;; Headline
309 ;; The main function to translate a headline is
310 ;; `org-beamer-headline'.
312 ;; Depending on the level at which a headline is considered as
313 ;; a frame (given by `org-beamer--frame-level'), the headline is
314 ;; either a section (`org-beamer--format-section'), a frame
315 ;; (`org-beamer--format-frame') or a block
316 ;; (`org-beamer--format-block').
318 ;; `org-beamer-headline' also takes care of special environments
319 ;; like "ignoreheading", "note", "noteNH", "appendix" and
320 ;; "againframe".
322 (defun org-beamer--get-label (headline info)
323 "Return label for HEADLINE, as a string.
325 INFO is a plist used as a communication channel.
327 The value is either the label specified in \"BEAMER_opt\"
328 property, the custom ID, if there is one and
329 `:latex-prefer-user-labels' property has a non nil value, or
330 a unique internal label. This function assumes HEADLINE will be
331 treated as a frame."
332 (cond
333 ((let ((opt (org-element-property :BEAMER_OPT headline)))
334 (and (stringp opt)
335 (string-match "\\(?:^\\|,\\)label=\\(.*?\\)\\(?:$\\|,\\)" opt)
336 (let ((label (match-string 1 opt)))
337 (if (string-match-p "\\`{.*}\\'" label)
338 (substring label 1 -1)
339 label)))))
340 ((and (plist-get info :latex-prefer-user-labels)
341 (org-element-property :CUSTOM_ID headline)))
342 (t (format "sec:%s" (org-export-get-reference headline info)))))
344 (defun org-beamer--frame-level (headline info)
345 "Return frame level in subtree containing HEADLINE.
346 INFO is a plist used as a communication channel."
348 ;; 1. Look for "frame" environment in parents, starting from the
349 ;; farthest.
350 (catch 'exit
351 (dolist (parent (nreverse (org-element-lineage headline)))
352 (let ((env (org-element-property :BEAMER_ENV parent)))
353 (when (and env (member-ignore-case env '("frame" "fullframe")))
354 (throw 'exit (org-export-get-relative-level parent info))))))
355 ;; 2. Look for "frame" environment in HEADLINE.
356 (let ((env (org-element-property :BEAMER_ENV headline)))
357 (and env (member-ignore-case env '("frame" "fullframe"))
358 (org-export-get-relative-level headline info)))
359 ;; 3. Look for "frame" environment in sub-tree.
360 (org-element-map headline 'headline
361 (lambda (hl)
362 (let ((env (org-element-property :BEAMER_ENV hl)))
363 (when (and env (member-ignore-case env '("frame" "fullframe")))
364 (org-export-get-relative-level hl info))))
365 info 'first-match)
366 ;; 4. No "frame" environment in tree: use default value.
367 (plist-get info :headline-levels)))
369 (defun org-beamer--format-section (headline contents info)
370 "Format HEADLINE as a sectioning part.
371 CONTENTS holds the contents of the headline. INFO is a plist
372 used as a communication channel."
373 (let ((latex-headline
374 (org-export-with-backend
375 ;; We create a temporary export back-end which behaves the
376 ;; same as current one, but adds "\protect" in front of the
377 ;; output of some objects.
378 (org-export-create-backend
379 :parent 'latex
380 :transcoders
381 (let ((protected-output
382 (function
383 (lambda (object contents info)
384 (let ((code (org-export-with-backend
385 'beamer object contents info)))
386 (if (org-string-nw-p code) (concat "\\protect" code)
387 code))))))
388 (mapcar #'(lambda (type) (cons type protected-output))
389 '(bold footnote-reference italic strike-through timestamp
390 underline))))
391 headline
392 contents
393 info))
394 (mode-specs (org-element-property :BEAMER_ACT headline)))
395 (if (and mode-specs
396 (string-match "\\`\\\\\\(.*?\\)\\(?:\\*\\|\\[.*\\]\\)?{"
397 latex-headline))
398 ;; Insert overlay specifications.
399 (replace-match (concat (match-string 1 latex-headline)
400 (format "<%s>" mode-specs))
401 nil nil latex-headline 1)
402 latex-headline)))
404 (defun org-beamer--format-frame (headline contents info)
405 "Format HEADLINE as a frame.
406 CONTENTS holds the contents of the headline. INFO is a plist
407 used as a communication channel."
408 (let ((fragilep
409 ;; FRAGILEP is non-nil when HEADLINE contains an element
410 ;; among `org-beamer-verbatim-elements'.
411 (org-element-map headline org-beamer-verbatim-elements 'identity
412 info 'first-match)))
413 (concat "\\begin{frame}"
414 ;; Overlay specification, if any. When surrounded by
415 ;; square brackets, consider it as a default
416 ;; specification.
417 (let ((action (org-element-property :BEAMER_ACT headline)))
418 (cond
419 ((not action) "")
420 ((string-match "\\`\\[.*\\]\\'" action )
421 (org-beamer--normalize-argument action 'defaction))
422 (t (org-beamer--normalize-argument action 'action))))
423 ;; Options, if any.
424 (let* ((beamer-opt (org-element-property :BEAMER_OPT headline))
425 (options
426 ;; Collect nonempty options from default value and
427 ;; headline's properties.
428 (cl-remove-if-not #'org-string-nw-p
429 (append
430 (org-split-string
431 (plist-get info :beamer-frame-default-options) ",")
432 (and beamer-opt
433 (org-split-string
434 ;; Remove square brackets if user provided
435 ;; them.
436 (and (string-match "^\\[?\\(.*\\)\\]?$" beamer-opt)
437 (match-string 1 beamer-opt))
438 ",")))))
439 (fragile
440 ;; Add "fragile" option if necessary.
441 (and fragilep
442 (not (member "fragile" options))
443 (list "fragile")))
444 (label
445 ;; Provide an automatic label for the frame unless
446 ;; the user specified one. Also refrain from
447 ;; labeling `allowframebreaks' frames; this is not
448 ;; allowed by Beamer.
449 (and (not (member "allowframebreaks" options))
450 (not (cl-some (lambda (s) (string-match-p "^label=" s))
451 options))
452 (list
453 (let ((label (org-beamer--get-label headline info)))
454 ;; Labels containing colons need to be
455 ;; wrapped within braces.
456 (format (if (string-match-p ":" label)
457 "label={%s}"
458 "label=%s")
459 label))))))
460 ;; Change options list into a string.
461 (org-beamer--normalize-argument
462 (mapconcat #'identity (append label fragile options) ",")
463 'option))
464 ;; Title.
465 (let ((env (org-element-property :BEAMER_ENV headline)))
466 (format "{%s}"
467 (if (and env (equal (downcase env) "fullframe")) ""
468 (org-export-data
469 (org-element-property :title headline) info))))
470 "\n"
471 ;; The following workaround is required in fragile frames
472 ;; as Beamer will append "\par" to the beginning of the
473 ;; contents. So we need to make sure the command is
474 ;; separated from the contents by at least one space. If
475 ;; it isn't, it will create "\parfirst-word" command and
476 ;; remove the first word from the contents in the PDF
477 ;; output.
478 (if (not fragilep) contents
479 (replace-regexp-in-string "\\`\n*" "\\& " (or contents "")))
480 "\\end{frame}")))
482 (defun org-beamer--format-block (headline contents info)
483 "Format HEADLINE as a block.
484 CONTENTS holds the contents of the headline. INFO is a plist
485 used as a communication channel."
486 (let* ((column-width (org-element-property :BEAMER_COL headline))
487 ;; ENVIRONMENT defaults to "block" if none is specified and
488 ;; there is no column specification. If there is a column
489 ;; specified but still no explicit environment, ENVIRONMENT
490 ;; is "column".
491 (environment (let ((env (org-element-property :BEAMER_ENV headline)))
492 (cond
493 ;; "block" is the fallback environment.
494 ((and (not env) (not column-width)) "block")
495 ;; "column" only.
496 ((not env) "column")
497 ;; Use specified environment.
498 (t env))))
499 (raw-title (org-element-property :raw-value headline))
500 (env-format
501 (cond ((member environment '("column" "columns")) nil)
502 ((assoc environment
503 (append (plist-get info :beamer-environments-extra)
504 org-beamer-environments-default)))
505 (t (user-error "Wrong block type at a headline named \"%s\""
506 raw-title))))
507 (title (org-export-data (org-element-property :title headline) info))
508 (raw-options (org-element-property :BEAMER_OPT headline))
509 (options (if raw-options
510 (org-beamer--normalize-argument raw-options 'option)
511 ""))
512 ;; Start a "columns" environment when explicitly requested or
513 ;; when there is no previous headline or the previous
514 ;; headline do not have a BEAMER_column property.
515 (parent-env (org-element-property
516 :BEAMER_ENV (org-export-get-parent-headline headline)))
517 (start-columns-p
518 (or (equal environment "columns")
519 (and column-width
520 (not (and parent-env
521 (equal (downcase parent-env) "columns")))
522 (or (org-export-first-sibling-p headline info)
523 (not (org-element-property
524 :BEAMER_COL
525 (org-export-get-previous-element
526 headline info)))))))
527 ;; End the "columns" environment when explicitly requested or
528 ;; when there is no next headline or the next headline do not
529 ;; have a BEAMER_column property.
530 (end-columns-p
531 (or (equal environment "columns")
532 (and column-width
533 (not (and parent-env
534 (equal (downcase parent-env) "columns")))
535 (or (org-export-last-sibling-p headline info)
536 (not (org-element-property
537 :BEAMER_COL
538 (org-export-get-next-element headline info))))))))
539 (concat
540 (when start-columns-p
541 ;; Column can accept options only when the environment is
542 ;; explicitly defined.
543 (if (not (equal environment "columns")) "\\begin{columns}\n"
544 (format "\\begin{columns}%s\n" options)))
545 (when column-width
546 (format "\\begin{column}%s{%s}\n"
547 ;; One can specify placement for column only when
548 ;; HEADLINE stands for a column on its own.
549 (if (equal environment "column") options "")
550 (format "%s\\columnwidth" column-width)))
551 ;; Block's opening string.
552 (when (nth 2 env-format)
553 (concat
554 (org-fill-template
555 (nth 2 env-format)
556 (nconc
557 ;; If BEAMER_act property has its value enclosed in square
558 ;; brackets, it is a default overlay specification and
559 ;; overlay specification is empty. Otherwise, it is an
560 ;; overlay specification and the default one is nil.
561 (let ((action (org-element-property :BEAMER_ACT headline)))
562 (cond
563 ((not action) (list (cons "a" "") (cons "A" "") (cons "R" "")))
564 ((and (string-prefix-p "[" action)
565 (string-suffix-p "]" action))
566 (list
567 (cons "A" (org-beamer--normalize-argument action 'defaction))
568 (cons "a" "")
569 (cons "R" action)))
571 (list (cons "a" (org-beamer--normalize-argument action 'action))
572 (cons "A" "")
573 (cons "R" action)))))
574 (list (cons "o" options)
575 (cons "O" (or raw-options ""))
576 (cons "h" title)
577 (cons "r" raw-title)
578 (cons "H" (if (equal raw-title "") ""
579 (format "{%s}" raw-title)))
580 (cons "U" (if (equal raw-title "") ""
581 (format "[%s]" raw-title))))))
582 "\n"))
583 contents
584 ;; Block's closing string, if any.
585 (and (nth 3 env-format) (concat (nth 3 env-format) "\n"))
586 (when column-width "\\end{column}\n")
587 (when end-columns-p "\\end{columns}"))))
589 (defun org-beamer-headline (headline contents info)
590 "Transcode HEADLINE element into Beamer code.
591 CONTENTS is the contents of the headline. INFO is a plist used
592 as a communication channel."
593 (unless (org-element-property :footnote-section-p headline)
594 (let ((level (org-export-get-relative-level headline info))
595 (frame-level (org-beamer--frame-level headline info))
596 (environment (let ((env (org-element-property :BEAMER_ENV headline)))
597 (or (org-string-nw-p env) "block"))))
598 (cond
599 ;; Case 1: Resume frame specified by "BEAMER_ref" property.
600 ((equal environment "againframe")
601 (let ((ref (org-element-property :BEAMER_REF headline)))
602 ;; Reference to frame being resumed is mandatory. Ignore
603 ;; the whole headline if it isn't provided.
604 (when (org-string-nw-p ref)
605 (concat "\\againframe"
606 ;; Overlay specification.
607 (let ((overlay (org-element-property :BEAMER_ACT headline)))
608 (when overlay
609 (org-beamer--normalize-argument
610 overlay
611 (if (string-match "\\`\\[.*\\]\\'" overlay) 'defaction
612 'action))))
613 ;; Options.
614 (let ((options (org-element-property :BEAMER_OPT headline)))
615 (when options
616 (org-beamer--normalize-argument options 'option)))
617 ;; Resolve reference provided by "BEAMER_ref"
618 ;; property. This is done by building a minimal
619 ;; fake link and calling the appropriate resolve
620 ;; function, depending on the reference syntax.
621 (let ((target
622 (if (string-match "\\`\\(id:\\|#\\)" ref)
623 (org-export-resolve-id-link
624 `(link (:path ,(substring ref (match-end 0))))
625 info)
626 (org-export-resolve-fuzzy-link
627 `(link (:path
628 ;; Look for headlines only.
629 ,(if (eq (string-to-char ref) ?*) ref
630 (concat "*" ref))))
631 info))))
632 ;; Now use user-defined label provided in TARGET
633 ;; headline, or fallback to standard one.
634 (format "{%s}" (org-beamer--get-label target info)))))))
635 ;; Case 2: Creation of an appendix is requested.
636 ((equal environment "appendix")
637 (concat "\\appendix"
638 (org-element-property :BEAMER_ACT headline)
639 "\n"
640 (make-string (org-element-property :pre-blank headline) ?\n)
641 contents))
642 ;; Case 3: Ignore heading.
643 ((equal environment "ignoreheading")
644 (concat (make-string (org-element-property :pre-blank headline) ?\n)
645 contents))
646 ;; Case 4: HEADLINE is a note.
647 ((member environment '("note" "noteNH"))
648 (concat "\\note"
649 ;; Overlay specification.
650 (let ((overlay (org-element-property :BEAMER_ACT headline)))
651 (when overlay
652 (org-beamer--normalize-argument
653 overlay
654 (if (string-match "\\`\\[.*\\]\\'" overlay)
655 'defaction 'action))))
656 (format "{%s}"
657 (concat (and (equal environment "note")
658 (concat
659 (org-export-data
660 (org-element-property :title headline)
661 info)
662 "\n"))
663 (org-trim contents)))))
664 ;; Case 5: HEADLINE is a frame.
665 ((= level frame-level)
666 (org-beamer--format-frame headline contents info))
667 ;; Case 6: Regular section, extracted from
668 ;; `org-latex-classes'.
669 ((< level frame-level)
670 (org-beamer--format-section headline contents info))
671 ;; Case 7: Otherwise, HEADLINE is a block.
672 (t (org-beamer--format-block headline contents info))))))
675 ;;;; Item
677 (defun org-beamer-item (item contents info)
678 "Transcode an ITEM element into Beamer code.
679 CONTENTS holds the contents of the item. INFO is a plist holding
680 contextual information."
681 (org-export-with-backend
682 ;; Delegate item export to `latex'. However, we use `beamer'
683 ;; transcoders for objects in the description tag.
684 (org-export-create-backend
685 :parent 'beamer
686 :transcoders
687 (list
688 (cons
689 'item
690 (lambda (item _c _i)
691 (let ((action
692 (let ((first (car (org-element-contents item))))
693 (and (eq (org-element-type first) 'paragraph)
694 (org-beamer--element-has-overlay-p first))))
695 (output (org-latex-item item contents info)))
696 (if (not (and action (string-match "\\\\item" output))) output
697 ;; If the item starts with a paragraph and that paragraph
698 ;; starts with an export snippet specifying an overlay,
699 ;; append it to the \item command.
700 (replace-match (concat "\\\\item" action) nil nil output)))))))
701 item contents info))
704 ;;;; Keyword
706 (defun org-beamer-keyword (keyword contents info)
707 "Transcode a KEYWORD element into Beamer code.
708 CONTENTS is nil. INFO is a plist used as a communication
709 channel."
710 (let ((key (org-element-property :key keyword))
711 (value (org-element-property :value keyword)))
712 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
713 ;; Otherwise, fallback to `latex' back-end.
714 (cond
715 ((equal key "BEAMER") value)
716 ((and (equal key "TOC") (string-match "\\<headlines\\>" value))
717 (let ((depth (or (and (string-match "[0-9]+" value)
718 (string-to-number (match-string 0 value)))
719 (plist-get info :with-toc)))
720 (options (and (string-match "\\[.*?\\]" value)
721 (match-string 0 value))))
722 (concat
723 (when (wholenump depth) (format "\\setcounter{tocdepth}{%s}\n" depth))
724 "\\tableofcontents" options)))
725 (t (org-export-with-backend 'latex keyword contents info)))))
728 ;;;; Link
730 (defun org-beamer-link (link contents info)
731 "Transcode a LINK object into Beamer code.
732 CONTENTS is the description part of the link. INFO is a plist
733 used as a communication channel."
734 (or (org-export-custom-protocol-maybe link contents 'beamer)
735 ;; Fall-back to LaTeX export. However, prefer "\hyperlink" over
736 ;; "\hyperref" since the former handles overlay specifications.
737 (let ((latex-link (org-export-with-backend 'latex link contents info)))
738 (if (string-match "\\`\\\\hyperref\\[\\(.*?\\)\\]" latex-link)
739 (replace-match
740 (format "\\\\hyperlink%s{\\1}"
741 (or (org-beamer--element-has-overlay-p link) ""))
742 nil nil latex-link)
743 latex-link))))
746 ;;;; Plain List
748 ;; Plain lists support `:environment', `:overlay' and `:options'
749 ;; attributes.
751 (defun org-beamer-plain-list (plain-list contents info)
752 "Transcode a PLAIN-LIST element into Beamer code.
753 CONTENTS is the contents of the list. INFO is a plist holding
754 contextual information."
755 (let* ((type (org-element-property :type plain-list))
756 (attributes (org-combine-plists
757 (org-export-read-attribute :attr_latex plain-list)
758 (org-export-read-attribute :attr_beamer plain-list)))
759 (latex-type (let ((env (plist-get attributes :environment)))
760 (cond (env)
761 ((eq type 'ordered) "enumerate")
762 ((eq type 'descriptive) "description")
763 (t "itemize")))))
764 (org-latex--wrap-label
765 plain-list
766 (format "\\begin{%s}%s%s\n%s\\end{%s}"
767 latex-type
768 ;; Default overlay specification, if any.
769 (org-beamer--normalize-argument
770 (or (plist-get attributes :overlay) "")
771 'defaction)
772 ;; Second optional argument depends on the list type.
773 (org-beamer--normalize-argument
774 (or (plist-get attributes :options) "")
775 'option)
776 ;; Eventually insert contents and close environment.
777 contents
778 latex-type)
779 info)))
782 ;;;; Radio Target
784 (defun org-beamer-radio-target (radio-target text info)
785 "Transcode a RADIO-TARGET object into Beamer code.
786 TEXT is the text of the target. INFO is a plist holding
787 contextual information."
788 (format "\\hypertarget%s{%s}{%s}"
789 (or (org-beamer--element-has-overlay-p radio-target) "")
790 (org-export-get-reference radio-target info)
791 text))
794 ;;;; Template
796 ;; Template used is similar to the one used in `latex' back-end,
797 ;; excepted for the table of contents and Beamer themes.
799 (defun org-beamer-template (contents info)
800 "Return complete document string after Beamer conversion.
801 CONTENTS is the transcoded contents string. INFO is a plist
802 holding export options."
803 (let ((title (org-export-data (plist-get info :title) info))
804 (subtitle (org-export-data (plist-get info :subtitle) info)))
805 (concat
806 ;; Time-stamp.
807 (and (plist-get info :time-stamp-file)
808 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
809 ;; LaTeX compiler
810 (org-latex--insert-compiler info)
811 ;; Document class and packages.
812 (org-latex-make-preamble info)
813 ;; Insert themes.
814 (let ((format-theme
815 (function
816 (lambda (prop command)
817 (let ((theme (plist-get info prop)))
818 (when theme
819 (concat command
820 (if (not (string-match "\\[.*\\]" theme))
821 (format "{%s}\n" theme)
822 (format "%s{%s}\n"
823 (match-string 0 theme)
824 (org-trim
825 (replace-match "" nil nil theme)))))))))))
826 (mapconcat (lambda (args) (apply format-theme args))
827 '((:beamer-theme "\\usetheme")
828 (:beamer-color-theme "\\usecolortheme")
829 (:beamer-font-theme "\\usefonttheme")
830 (:beamer-inner-theme "\\useinnertheme")
831 (:beamer-outer-theme "\\useoutertheme"))
832 ""))
833 ;; Possibly limit depth for headline numbering.
834 (let ((sec-num (plist-get info :section-numbers)))
835 (when (integerp sec-num)
836 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
837 ;; Author.
838 (let ((author (and (plist-get info :with-author)
839 (let ((auth (plist-get info :author)))
840 (and auth (org-export-data auth info)))))
841 (email (and (plist-get info :with-email)
842 (org-export-data (plist-get info :email) info))))
843 (cond ((and author email (not (string= "" email)))
844 (format "\\author{%s\\thanks{%s}}\n" author email))
845 ((or author email) (format "\\author{%s}\n" (or author email)))))
846 ;; Date.
847 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
848 (format "\\date{%s}\n" (org-export-data date info)))
849 ;; Title
850 (format "\\title{%s}\n" title)
851 (when (org-string-nw-p subtitle)
852 (concat (format (plist-get info :beamer-subtitle-format) subtitle) "\n"))
853 ;; Beamer-header
854 (let ((beamer-header (plist-get info :beamer-header)))
855 (when beamer-header
856 (format "%s\n" (plist-get info :beamer-header))))
857 ;; 9. Hyperref options.
858 (let ((template (plist-get info :latex-hyperref-template)))
859 (and (stringp template)
860 (format-spec template (org-latex--format-spec info))))
861 ;; Document start.
862 "\\begin{document}\n\n"
863 ;; Title command.
864 (org-element-normalize-string
865 (cond ((not (plist-get info :with-title)) nil)
866 ((string= "" title) nil)
867 ((not (stringp org-latex-title-command)) nil)
868 ((string-match "\\(?:[^%]\\|^\\)%s"
869 org-latex-title-command)
870 (format org-latex-title-command title))
871 (t org-latex-title-command)))
872 ;; Table of contents.
873 (let ((depth (plist-get info :with-toc)))
874 (when depth
875 (concat
876 (format "\\begin{frame}%s{%s}\n"
877 (org-beamer--normalize-argument
878 (plist-get info :beamer-outline-frame-options) 'option)
879 (plist-get info :beamer-outline-frame-title))
880 (when (wholenump depth)
881 (format "\\setcounter{tocdepth}{%d}\n" depth))
882 "\\tableofcontents\n"
883 "\\end{frame}\n\n")))
884 ;; Document's body.
885 contents
886 ;; Creator.
887 (if (plist-get info :with-creator)
888 (concat (plist-get info :creator) "\n")
890 ;; Document end.
891 "\\end{document}")))
895 ;;; Minor Mode
898 (defvar org-beamer-mode-map (make-sparse-keymap)
899 "The keymap for `org-beamer-mode'.")
900 (define-key org-beamer-mode-map "\C-c\C-b" 'org-beamer-select-environment)
902 ;;;###autoload
903 (define-minor-mode org-beamer-mode
904 "Support for editing Beamer oriented Org mode files."
905 nil " Bm" 'org-beamer-mode-map)
907 (when (fboundp 'font-lock-add-keywords)
908 (font-lock-add-keywords
909 'org-mode
910 '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-beamer-tag prepend))
911 'prepend))
913 (defface org-beamer-tag '((t (:box (:line-width 1 :color grey40))))
914 "The special face for beamer tags."
915 :group 'org-export-beamer)
917 (defun org-beamer-property-changed (property value)
918 "Track the BEAMER_env property with tags.
919 PROPERTY is the name of the modified property. VALUE is its new
920 value."
921 (cond
922 ((equal property "BEAMER_env")
923 (save-excursion
924 (org-back-to-heading t)
925 ;; Filter out Beamer-related tags and install environment tag.
926 (let ((tags (cl-remove-if (lambda (x) (string-match "^B_" x))
927 (org-get-tags nil t)))
928 (env-tag (and (org-string-nw-p value) (concat "B_" value))))
929 (org-set-tags (if env-tag (cons env-tag tags) tags))
930 (when env-tag (org-toggle-tag env-tag 'on)))))
931 ((equal property "BEAMER_col")
932 (org-toggle-tag "BMCOL" (if (org-string-nw-p value) 'on 'off)))))
934 (add-hook 'org-property-changed-functions 'org-beamer-property-changed)
936 (defun org-beamer-allowed-property-values (property)
937 "Supply allowed values for PROPERTY."
938 (cond
939 ((and (equal property "BEAMER_env")
940 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
941 ;; If no allowed values for BEAMER_env have been defined,
942 ;; supply all defined environments
943 (mapcar 'car (append org-beamer-environments-special
944 org-beamer-environments-extra
945 org-beamer-environments-default)))
946 ((and (equal property "BEAMER_col")
947 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
948 ;; If no allowed values for BEAMER_col have been defined, supply
949 ;; some.
950 (split-string org-beamer-column-widths " "))))
952 (add-hook 'org-property-allowed-value-functions
953 'org-beamer-allowed-property-values)
957 ;;; Commands
959 ;;;###autoload
960 (defun org-beamer-export-as-latex
961 (&optional async subtreep visible-only body-only ext-plist)
962 "Export current buffer as a Beamer buffer.
964 If narrowing is active in the current buffer, only export its
965 narrowed part.
967 If a region is active, export that region.
969 A non-nil optional argument ASYNC means the process should happen
970 asynchronously. The resulting buffer should be accessible
971 through the `org-export-stack' interface.
973 When optional argument SUBTREEP is non-nil, export the sub-tree
974 at point, extracting information from the headline properties
975 first.
977 When optional argument VISIBLE-ONLY is non-nil, don't export
978 contents of hidden elements.
980 When optional argument BODY-ONLY is non-nil, only write code
981 between \"\\begin{document}\" and \"\\end{document}\".
983 EXT-PLIST, when provided, is a property list with external
984 parameters overriding Org default settings, but still inferior to
985 file-local settings.
987 Export is done in a buffer named \"*Org BEAMER Export*\", which
988 will be displayed when `org-export-show-temporary-export-buffer'
989 is non-nil."
990 (interactive)
991 (org-export-to-buffer 'beamer "*Org BEAMER Export*"
992 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
994 ;;;###autoload
995 (defun org-beamer-export-to-latex
996 (&optional async subtreep visible-only body-only ext-plist)
997 "Export current buffer as a Beamer presentation (tex).
999 If narrowing is active in the current buffer, only export its
1000 narrowed part.
1002 If a region is active, export that region.
1004 A non-nil optional argument ASYNC means the process should happen
1005 asynchronously. The resulting file should be accessible through
1006 the `org-export-stack' interface.
1008 When optional argument SUBTREEP is non-nil, export the sub-tree
1009 at point, extracting information from the headline properties
1010 first.
1012 When optional argument VISIBLE-ONLY is non-nil, don't export
1013 contents of hidden elements.
1015 When optional argument BODY-ONLY is non-nil, only write code
1016 between \"\\begin{document}\" and \"\\end{document}\".
1018 EXT-PLIST, when provided, is a property list with external
1019 parameters overriding Org default settings, but still inferior to
1020 file-local settings.
1022 Return output file's name."
1023 (interactive)
1024 (let ((file (org-export-output-file-name ".tex" subtreep)))
1025 (org-export-to-file 'beamer file
1026 async subtreep visible-only body-only ext-plist)))
1028 ;;;###autoload
1029 (defun org-beamer-export-to-pdf
1030 (&optional async subtreep visible-only body-only ext-plist)
1031 "Export current buffer as a Beamer presentation (PDF).
1033 If narrowing is active in the current buffer, only export its
1034 narrowed part.
1036 If a region is active, export that region.
1038 A non-nil optional argument ASYNC means the process should happen
1039 asynchronously. The resulting file should be accessible through
1040 the `org-export-stack' interface.
1042 When optional argument SUBTREEP is non-nil, export the sub-tree
1043 at point, extracting information from the headline properties
1044 first.
1046 When optional argument VISIBLE-ONLY is non-nil, don't export
1047 contents of hidden elements.
1049 When optional argument BODY-ONLY is non-nil, only write code
1050 between \"\\begin{document}\" and \"\\end{document}\".
1052 EXT-PLIST, when provided, is a property list with external
1053 parameters overriding Org default settings, but still inferior to
1054 file-local settings.
1056 Return PDF file's name."
1057 (interactive)
1058 (let ((file (org-export-output-file-name ".tex" subtreep)))
1059 (org-export-to-file 'beamer file
1060 async subtreep visible-only body-only ext-plist
1061 (lambda (file) (org-latex-compile file)))))
1063 ;;;###autoload
1064 (defun org-beamer-select-environment ()
1065 "Select the environment to be used by beamer for this entry.
1066 While this uses (for convenience) a tag selection interface, the
1067 result of this command will be that the BEAMER_env *property* of
1068 the entry is set.
1070 In addition to this, the command will also set a tag as a visual
1071 aid, but the tag does not have any semantic meaning."
1072 (interactive)
1073 ;; Make sure `org-beamer-environments-special' has a higher
1074 ;; priority than `org-beamer-environments-extra'.
1075 (let* ((envs (append org-beamer-environments-special
1076 org-beamer-environments-extra
1077 org-beamer-environments-default))
1078 (org-current-tag-alist
1079 (append '((:startgroup))
1080 (mapcar (lambda (e) (cons (concat "B_" (car e))
1081 (string-to-char (nth 1 e))))
1082 envs)
1083 '((:endgroup))
1084 '(("BMCOL" . ?|))))
1085 (org-tag-persistent-alist nil)
1086 (org-use-fast-tag-selection t)
1087 (org-fast-tag-selection-single-key t))
1088 (org-set-tags-command)
1089 (let ((tags (org-get-tags nil t)))
1090 (cond
1091 ;; For a column, automatically ask for its width.
1092 ((eq org-last-tag-selection-key ?|)
1093 (if (member "BMCOL" tags)
1094 (org-set-property "BEAMER_col" (read-string "Column width: "))
1095 (org-delete-property "BEAMER_col")))
1096 ;; For an "againframe" section, automatically ask for reference
1097 ;; to resumed frame and overlay specifications.
1098 ((eq org-last-tag-selection-key ?A)
1099 (if (equal (org-entry-get nil "BEAMER_env") "againframe")
1100 (progn (org-entry-delete nil "BEAMER_env")
1101 (org-entry-delete nil "BEAMER_ref")
1102 (org-entry-delete nil "BEAMER_act"))
1103 (org-entry-put nil "BEAMER_env" "againframe")
1104 (org-set-property
1105 "BEAMER_ref"
1106 (read-string "Frame reference (*Title, #custom-id, id:...): "))
1107 (org-set-property "BEAMER_act"
1108 (read-string "Overlay specification: "))))
1109 ((let* ((tags-re (concat "B_" (regexp-opt (mapcar #'car envs) t)))
1110 (env (cl-some (lambda (tag)
1111 (and (string-match tags-re tag)
1112 (match-string 1 tag)))
1113 tags)))
1114 (and env (progn (org-entry-put nil "BEAMER_env" env) t))))
1115 (t (org-entry-delete nil "BEAMER_env"))))))
1117 ;;;###autoload
1118 (defun org-beamer-publish-to-latex (plist filename pub-dir)
1119 "Publish an Org file to a Beamer presentation (LaTeX).
1121 FILENAME is the filename of the Org file to be published. PLIST
1122 is the property list for the given project. PUB-DIR is the
1123 publishing directory.
1125 Return output file name."
1126 (org-publish-org-to 'beamer filename ".tex" plist pub-dir))
1128 ;;;###autoload
1129 (defun org-beamer-publish-to-pdf (plist filename pub-dir)
1130 "Publish an Org file to a Beamer presentation (PDF, via LaTeX).
1132 FILENAME is the filename of the Org file to be published. PLIST
1133 is the property list for the given project. PUB-DIR is the
1134 publishing directory.
1136 Return output file name."
1137 ;; Unlike to `org-beamer-publish-to-latex', PDF file is generated in
1138 ;; working directory and then moved to publishing directory.
1139 (org-publish-attachment
1140 plist
1141 ;; Default directory could be anywhere when this function is
1142 ;; called. We ensure it is set to source file directory during
1143 ;; compilation so as to not break links to external documents.
1144 (let ((default-directory (file-name-directory filename)))
1145 (org-latex-compile
1146 (org-publish-org-to
1147 'beamer filename ".tex" plist (file-name-directory filename))))
1148 pub-dir))
1151 (provide 'ox-beamer)
1153 ;; Local variables:
1154 ;; generated-autoload-file: "org-loaddefs.el"
1155 ;; End:
1157 ;;; ox-beamer.el ends here