org.texi: Fix some @xref usage
[org-mode.git] / lisp / ox-beamer.el
blob9a26dc09adb8beaba8b91fa64a83690ea8655fe2
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 (or (org-export-custom-protocol-maybe link contents 'beamer)
724 ;; Fall-back to LaTeX export. However, if link is becomes
725 ;; a "\hyperlink" macro, try to sneak in Beamer overlay
726 ;; specification, if any.
727 (let ((latex-link (org-export-with-backend 'latex link contents info))
728 (overlay (org-beamer--element-has-overlay-p link)))
729 (if (and overlay (string-match "\\`\\\\hyperlink" latex-link))
730 (replace-match (concat "\\&" overlay) nil nil latex-link)
731 latex-link))))
734 ;;;; Plain List
736 ;; Plain lists support `:environment', `:overlay' and `:options'
737 ;; attributes.
739 (defun org-beamer-plain-list (plain-list contents info)
740 "Transcode a PLAIN-LIST element into Beamer code.
741 CONTENTS is the contents of the list. INFO is a plist holding
742 contextual information."
743 (let* ((type (org-element-property :type plain-list))
744 (attributes (org-combine-plists
745 (org-export-read-attribute :attr_latex plain-list)
746 (org-export-read-attribute :attr_beamer plain-list)))
747 (latex-type (let ((env (plist-get attributes :environment)))
748 (cond (env)
749 ((eq type 'ordered) "enumerate")
750 ((eq type 'descriptive) "description")
751 (t "itemize")))))
752 (org-latex--wrap-label
753 plain-list
754 (format "\\begin{%s}%s%s\n%s\\end{%s}"
755 latex-type
756 ;; Default overlay specification, if any.
757 (org-beamer--normalize-argument
758 (or (plist-get attributes :overlay) "")
759 'defaction)
760 ;; Second optional argument depends on the list type.
761 (org-beamer--normalize-argument
762 (or (plist-get attributes :options) "")
763 'option)
764 ;; Eventually insert contents and close environment.
765 contents
766 latex-type)
767 info)))
770 ;;;; Radio Target
772 (defun org-beamer-radio-target (radio-target text info)
773 "Transcode a RADIO-TARGET object into Beamer code.
774 TEXT is the text of the target. INFO is a plist holding
775 contextual information."
776 (format "\\hypertarget%s{%s}{%s}"
777 (or (org-beamer--element-has-overlay-p radio-target) "")
778 (org-export-get-reference radio-target info)
779 text))
782 ;;;; Target
784 (defun org-beamer-target (target _contents info)
785 "Transcode a TARGET object into Beamer code.
786 CONTENTS is nil. INFO is a plist holding contextual
787 information."
788 (format "\\label{%s}" (org-export-get-reference target info)))
791 ;;;; Template
793 ;; Template used is similar to the one used in `latex' back-end,
794 ;; excepted for the table of contents and Beamer themes.
796 (defun org-beamer-template (contents info)
797 "Return complete document string after Beamer conversion.
798 CONTENTS is the transcoded contents string. INFO is a plist
799 holding export options."
800 (let ((title (org-export-data (plist-get info :title) info))
801 (subtitle (org-export-data (plist-get info :subtitle) info)))
802 (concat
803 ;; Time-stamp.
804 (and (plist-get info :time-stamp-file)
805 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
806 ;; LaTeX compiler
807 (org-latex--insert-compiler info)
808 ;; Document class and packages.
809 (org-latex--make-preamble info)
810 ;; Insert themes.
811 (let ((format-theme
812 (function
813 (lambda (prop command)
814 (let ((theme (plist-get info prop)))
815 (when theme
816 (concat command
817 (if (not (string-match "\\[.*\\]" theme))
818 (format "{%s}\n" theme)
819 (format "%s{%s}\n"
820 (match-string 0 theme)
821 (org-trim
822 (replace-match "" nil nil theme)))))))))))
823 (mapconcat (lambda (args) (apply format-theme args))
824 '((:beamer-theme "\\usetheme")
825 (:beamer-color-theme "\\usecolortheme")
826 (:beamer-font-theme "\\usefonttheme")
827 (:beamer-inner-theme "\\useinnertheme")
828 (:beamer-outer-theme "\\useoutertheme"))
829 ""))
830 ;; Possibly limit depth for headline numbering.
831 (let ((sec-num (plist-get info :section-numbers)))
832 (when (integerp sec-num)
833 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
834 ;; Author.
835 (let ((author (and (plist-get info :with-author)
836 (let ((auth (plist-get info :author)))
837 (and auth (org-export-data auth info)))))
838 (email (and (plist-get info :with-email)
839 (org-export-data (plist-get info :email) info))))
840 (cond ((and author email (not (string= "" email)))
841 (format "\\author{%s\\thanks{%s}}\n" author email))
842 ((or author email) (format "\\author{%s}\n" (or author email)))))
843 ;; Date.
844 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
845 (format "\\date{%s}\n" (org-export-data date info)))
846 ;; Title
847 (format "\\title{%s}\n" title)
848 (when (org-string-nw-p subtitle)
849 (concat (format (plist-get info :beamer-subtitle-format) subtitle) "\n"))
850 ;; Beamer-header
851 (let ((beamer-header (plist-get info :beamer-header)))
852 (when beamer-header
853 (format "%s\n" (plist-get info :beamer-header))))
854 ;; 9. Hyperref options.
855 (let ((template (plist-get info :latex-hyperref-template)))
856 (and (stringp template)
857 (format-spec template (org-latex--format-spec info))))
858 ;; Document start.
859 "\\begin{document}\n\n"
860 ;; Title command.
861 (org-element-normalize-string
862 (cond ((not (plist-get info :with-title)) nil)
863 ((string= "" title) nil)
864 ((not (stringp org-latex-title-command)) nil)
865 ((string-match "\\(?:[^%]\\|^\\)%s"
866 org-latex-title-command)
867 (format org-latex-title-command title))
868 (t org-latex-title-command)))
869 ;; Table of contents.
870 (let ((depth (plist-get info :with-toc)))
871 (when depth
872 (concat
873 (format "\\begin{frame}%s{%s}\n"
874 (org-beamer--normalize-argument
875 (plist-get info :beamer-outline-frame-options) 'option)
876 (plist-get info :beamer-outline-frame-title))
877 (when (wholenump depth)
878 (format "\\setcounter{tocdepth}{%d}\n" depth))
879 "\\tableofcontents\n"
880 "\\end{frame}\n\n")))
881 ;; Document's body.
882 contents
883 ;; Creator.
884 (if (plist-get info :with-creator)
885 (concat (plist-get info :creator) "\n")
887 ;; Document end.
888 "\\end{document}")))
892 ;;; Minor Mode
895 (defvar org-beamer-mode-map (make-sparse-keymap)
896 "The keymap for `org-beamer-mode'.")
897 (define-key org-beamer-mode-map "\C-c\C-b" 'org-beamer-select-environment)
899 ;;;###autoload
900 (define-minor-mode org-beamer-mode
901 "Support for editing Beamer oriented Org mode files."
902 nil " Bm" 'org-beamer-mode-map)
904 (when (fboundp 'font-lock-add-keywords)
905 (font-lock-add-keywords
906 'org-mode
907 '((":\\(B_[a-z]+\\|BMCOL\\):" 1 'org-beamer-tag prepend))
908 'prepend))
910 (defface org-beamer-tag '((t (:box (:line-width 1 :color grey40))))
911 "The special face for beamer tags."
912 :group 'org-export-beamer)
914 (defun org-beamer-property-changed (property value)
915 "Track the BEAMER_env property with tags.
916 PROPERTY is the name of the modified property. VALUE is its new
917 value."
918 (cond
919 ((equal property "BEAMER_env")
920 (save-excursion
921 (org-back-to-heading t)
922 ;; Filter out Beamer-related tags and install environment tag.
923 (let ((tags (cl-remove-if (lambda (x) (string-match "^B_" x))
924 (org-get-tags)))
925 (env-tag (and (org-string-nw-p value) (concat "B_" value))))
926 (org-set-tags-to (if env-tag (cons env-tag tags) tags))
927 (when env-tag (org-toggle-tag env-tag 'on)))))
928 ((equal property "BEAMER_col")
929 (org-toggle-tag "BMCOL" (if (org-string-nw-p value) 'on 'off)))))
931 (add-hook 'org-property-changed-functions 'org-beamer-property-changed)
933 (defun org-beamer-allowed-property-values (property)
934 "Supply allowed values for PROPERTY."
935 (cond
936 ((and (equal property "BEAMER_env")
937 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
938 ;; If no allowed values for BEAMER_env have been defined,
939 ;; supply all defined environments
940 (mapcar 'car (append org-beamer-environments-special
941 org-beamer-environments-extra
942 org-beamer-environments-default)))
943 ((and (equal property "BEAMER_col")
944 (not (org-entry-get nil (concat property "_ALL") 'inherit)))
945 ;; If no allowed values for BEAMER_col have been defined,
946 ;; supply some
947 (org-split-string org-beamer-column-widths " "))))
949 (add-hook 'org-property-allowed-value-functions
950 'org-beamer-allowed-property-values)
954 ;;; Commands
956 ;;;###autoload
957 (defun org-beamer-export-as-latex
958 (&optional async subtreep visible-only body-only ext-plist)
959 "Export current buffer as a Beamer buffer.
961 If narrowing is active in the current buffer, only export its
962 narrowed part.
964 If a region is active, export that region.
966 A non-nil optional argument ASYNC means the process should happen
967 asynchronously. The resulting buffer should be accessible
968 through the `org-export-stack' interface.
970 When optional argument SUBTREEP is non-nil, export the sub-tree
971 at point, extracting information from the headline properties
972 first.
974 When optional argument VISIBLE-ONLY is non-nil, don't export
975 contents of hidden elements.
977 When optional argument BODY-ONLY is non-nil, only write code
978 between \"\\begin{document}\" and \"\\end{document}\".
980 EXT-PLIST, when provided, is a property list with external
981 parameters overriding Org default settings, but still inferior to
982 file-local settings.
984 Export is done in a buffer named \"*Org BEAMER Export*\", which
985 will be displayed when `org-export-show-temporary-export-buffer'
986 is non-nil."
987 (interactive)
988 (org-export-to-buffer 'beamer "*Org BEAMER Export*"
989 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
991 ;;;###autoload
992 (defun org-beamer-export-to-latex
993 (&optional async subtreep visible-only body-only ext-plist)
994 "Export current buffer as a Beamer presentation (tex).
996 If narrowing is active in the current buffer, only export its
997 narrowed part.
999 If a region is active, export that region.
1001 A non-nil optional argument ASYNC means the process should happen
1002 asynchronously. The resulting file should be accessible through
1003 the `org-export-stack' interface.
1005 When optional argument SUBTREEP is non-nil, export the sub-tree
1006 at point, extracting information from the headline properties
1007 first.
1009 When optional argument VISIBLE-ONLY is non-nil, don't export
1010 contents of hidden elements.
1012 When optional argument BODY-ONLY is non-nil, only write code
1013 between \"\\begin{document}\" and \"\\end{document}\".
1015 EXT-PLIST, when provided, is a property list with external
1016 parameters overriding Org default settings, but still inferior to
1017 file-local settings.
1019 Return output file's name."
1020 (interactive)
1021 (let ((file (org-export-output-file-name ".tex" subtreep)))
1022 (org-export-to-file 'beamer file
1023 async subtreep visible-only body-only ext-plist)))
1025 ;;;###autoload
1026 (defun org-beamer-export-to-pdf
1027 (&optional async subtreep visible-only body-only ext-plist)
1028 "Export current buffer as a Beamer presentation (PDF).
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 PDF 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
1058 (lambda (file) (org-latex-compile file)))))
1060 ;;;###autoload
1061 (defun org-beamer-select-environment ()
1062 "Select the environment to be used by beamer for this entry.
1063 While this uses (for convenience) a tag selection interface, the
1064 result of this command will be that the BEAMER_env *property* of
1065 the entry is set.
1067 In addition to this, the command will also set a tag as a visual
1068 aid, but the tag does not have any semantic meaning."
1069 (interactive)
1070 ;; Make sure `org-beamer-environments-special' has a higher
1071 ;; priority than `org-beamer-environments-extra'.
1072 (let* ((envs (append org-beamer-environments-special
1073 org-beamer-environments-extra
1074 org-beamer-environments-default))
1075 (org-current-tag-alist
1076 (append '((:startgroup))
1077 (mapcar (lambda (e) (cons (concat "B_" (car e))
1078 (string-to-char (nth 1 e))))
1079 envs)
1080 '((:endgroup))
1081 '(("BMCOL" . ?|))))
1082 (org-tag-persistent-alist nil)
1083 (org-use-fast-tag-selection t)
1084 (org-fast-tag-selection-single-key t))
1085 (org-set-tags)
1086 (let ((tags (or (ignore-errors (org-get-tags-string)) "")))
1087 (cond
1088 ;; For a column, automatically ask for its width.
1089 ((eq org-last-tag-selection-key ?|)
1090 (if (string-match ":BMCOL:" tags)
1091 (org-set-property "BEAMER_col" (read-string "Column width: "))
1092 (org-delete-property "BEAMER_col")))
1093 ;; For an "againframe" section, automatically ask for reference
1094 ;; to resumed frame and overlay specifications.
1095 ((eq org-last-tag-selection-key ?A)
1096 (if (equal (org-entry-get nil "BEAMER_env") "againframe")
1097 (progn (org-entry-delete nil "BEAMER_env")
1098 (org-entry-delete nil "BEAMER_ref")
1099 (org-entry-delete nil "BEAMER_act"))
1100 (org-entry-put nil "BEAMER_env" "againframe")
1101 (org-set-property
1102 "BEAMER_ref"
1103 (read-string "Frame reference (*Title, #custom-id, id:...): "))
1104 (org-set-property "BEAMER_act"
1105 (read-string "Overlay specification: "))))
1106 ((string-match (concat ":B_\\(" (mapconcat 'car envs "\\|") "\\):") tags)
1107 (org-entry-put nil "BEAMER_env" (match-string 1 tags)))
1108 (t (org-entry-delete nil "BEAMER_env"))))))
1110 ;;;###autoload
1111 (defun org-beamer-publish-to-latex (plist filename pub-dir)
1112 "Publish an Org file to a Beamer presentation (LaTeX).
1114 FILENAME is the filename of the Org file to be published. PLIST
1115 is the property list for the given project. PUB-DIR is the
1116 publishing directory.
1118 Return output file name."
1119 (org-publish-org-to 'beamer filename ".tex" plist pub-dir))
1121 ;;;###autoload
1122 (defun org-beamer-publish-to-pdf (plist filename pub-dir)
1123 "Publish an Org file to a Beamer presentation (PDF, via LaTeX).
1125 FILENAME is the filename of the Org file to be published. PLIST
1126 is the property list for the given project. PUB-DIR is the
1127 publishing directory.
1129 Return output file name."
1130 ;; Unlike to `org-beamer-publish-to-latex', PDF file is generated in
1131 ;; working directory and then moved to publishing directory.
1132 (org-publish-attachment
1133 plist
1134 (org-latex-compile
1135 (org-publish-org-to
1136 'beamer filename ".tex" plist (file-name-directory filename)))
1137 pub-dir))
1140 (provide 'ox-beamer)
1142 ;; Local variables:
1143 ;; generated-autoload-file: "org-loaddefs.el"
1144 ;; End:
1146 ;;; ox-beamer.el ends here