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