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