Changing timestamps granularly.
[org-mode/org-tableheadings.git] / org-export-latex.el
blobf98094e645a2eb38509495b046d2c4fbedba21e6
1 ;;; org-export-latex.el --- LaTeX exporter for org-mode
2 ;;
3 ;; Copyright (c) 2007, 2008 Free Software Foundation, Inc.
4 ;;
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: org-export-latex.el
7 ;; Version: 5.19
8 ;; Author: Bastien Guerry <bzg AT altern DOT org>
9 ;; Maintainer: Bastien Guerry <bzg AT altern DOT org>
10 ;; Keywords: org, wp, tex
11 ;; Description: Converts an org-mode buffer into LaTeX
12 ;; URL: http://www.cognition.ens.fr/~guerry/u/org-export-latex.el
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify it
17 ;; under the terms of the GNU General Public License as published by the
18 ;; Free Software Foundation; either version 3, or (at your option) any
19 ;; later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful, but
22 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 ;; General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the Free
28 ;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
29 ;; MA 02110-1301, USA.
31 ;;; Commentary:
33 ;; This library implements a LaTeX exporter for org-mode.
35 ;; Put this file into your load-path and the following into your ~/.emacs:
36 ;; (require 'org-export-latex)
38 ;; The interactive functions are similar to those of the HTML exporter:
40 ;; M-x `org-export-as-latex'
41 ;; M-x `org-export-as-latex-batch'
42 ;; M-x `org-export-as-latex-to-buffer'
43 ;; M-x `org-export-region-as-latex'
44 ;; M-x `org-replace-region-by-latex'
45 ;;
46 ;;; Code:
48 (eval-when-compile
49 (require 'cl))
51 (require 'footnote)
52 (require 'org)
54 ;;; Variables:
55 (defvar org-export-latex-class nil)
56 (defvar org-export-latex-header nil)
57 (defvar org-export-latex-append-header nil)
58 (defvar org-export-latex-options-plist nil)
59 (defvar org-export-latex-todo-keywords-1 nil)
60 (defvar org-export-latex-all-targets-re nil)
61 (defvar org-export-latex-add-level 0)
62 (defvar org-export-latex-sectioning "")
63 (defvar org-export-latex-sectioning-depth 0)
64 (defvar org-export-latex-list-beginning-re
65 "^\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) +?")
67 (defvar org-export-latex-special-string-regexps
68 '(org-ts-regexp
69 org-scheduled-string
70 org-deadline-string
71 org-clock-string)
72 "A list of regexps to convert as special keywords.")
74 (defvar latexp) ; dynamically scoped from org.el
75 (defvar re-quote) ; dynamically scoped from org.el
76 (defvar commentsp) ; dynamically scoped from org.el
78 ;;; User variables:
80 (defcustom org-export-latex-default-class "article"
81 "The default LaTeX class."
82 :group 'org-export-latex
83 :type '(string :tag "LaTeX class"))
85 (defcustom org-export-latex-classes
86 '(("article"
87 "\\documentclass[11pt,a4paper]{article}
88 \\usepackage[utf8]{inputenc}
89 \\usepackage[T1]{fontenc}
90 \\usepackage{hyperref}"
91 ("\\section{%s}" . "\\section*{%s}")
92 ("\\subsection{%s}" . "\\subsection*{%s}")
93 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
94 ("\\paragraph{%s}" . "\\paragraph*{%s}")
95 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
96 ("report"
97 "\\documentclass[11pt,a4paper]{report}
98 \\usepackage[utf8]{inputenc}
99 \\usepackage[T1]{fontenc}
100 \\usepackage{hyperref}"
101 ("\\part{%s}" . "\\part*{%s}")
102 ("\\chapter{%s}" . "\\chapter*{%s}")
103 ("\\section{%s}" . "\\section*{%s}")
104 ("\\subsection{%s}" . "\\subsection*{%s}")
105 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
106 ("book"
107 "\\documentclass[11pt,a4paper]{book}
108 \\usepackage[utf8]{inputenc}
109 \\usepackage[T1]{fontenc}
110 \\usepackage{hyperref}"
111 ("\\part{%s}" . "\\part*{%s}")
112 ("\\chapter{%s}" . "\\chapter*{%s}")
113 ("\\section{%s}" . "\\section*{%s}")
114 ("\\subsection{%s}" . "\\subsection*{%s}")
115 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
116 "Alist of LaTeX classes and associated header and structure.
117 If #+LaTeX_CLASS is set in the buffer, use its value and the
118 associated information. Here is the structure of each cell:
120 \(class-name
121 header-string
122 (unnumbered-section numbered-section\)
123 ...\)
125 A %s formatter is mandatory in each section string and will be
126 replaced by the title of the section."
127 :group 'org-export-latex
128 :type '(repeat
129 (list (string :tag "LaTeX class")
130 (string :tag "LaTeX header")
131 (cons :tag "Level 1"
132 (string :tag "Numbered")
133 (string :tag "Unnumbered"))
134 (cons :tag "Level 2"
135 (string :tag "Numbered")
136 (string :tag "Unnumbered"))
137 (cons :tag "Level 3"
138 (string :tag "Numbered")
139 (string :tag "Unnumbered"))
140 (cons :tag "Level 4"
141 (string :tag "Numbered")
142 (string :tag "Unnumbered"))
143 (cons :tag "Level 5"
144 (string :tag "Numbered")
145 (string :tag "Unnumbered")))))
147 (defcustom org-export-latex-emphasis-alist
148 '(("*" "\\textbf{%s}" nil)
149 ("/" "\\emph{%s}" nil)
150 ("_" "\\underline{%s}" nil)
151 ("+" "\\texttt{%s}" nil)
152 ("=" "\\texttt{%s}" nil)
153 ("~" "\\texttt{%s}" t))
154 "Alist of LaTeX expressions to convert emphasis fontifiers.
155 Each element of the list is a list of three elements.
156 The first element is the character used as a marker for fontification.
157 The second element is a formatting string to wrap fontified text with.
158 The third element decides whether to protect converted text from other
159 conversions."
160 :group 'org-export-latex
161 :type 'alist)
163 (defcustom org-export-latex-title-command "\\maketitle"
164 "The command used to insert the title just after \\begin{document}.
165 If this string contains the formatting specification \"%s\" then
166 it will be used as a formatting string, passing the title as an
167 argument."
168 :group 'org-export-latex
169 :type 'string)
171 (defcustom org-export-latex-date-format
172 "%d %B %Y"
173 "Format string for \\date{...}."
174 :group 'org-export-latex
175 :type 'string)
177 (defcustom org-export-latex-tables-verbatim nil
178 "When non-nil, export tables as verbatim."
179 :group 'org-export-latex
180 :type 'boolean)
182 (defcustom org-export-latex-tables-column-borders nil
183 "When non-nil, group of columns are surrounded with borders,
184 XSeven if these borders are the outside borders of the table."
185 :group 'org-export-latex
186 :type 'boolean)
188 (defcustom org-export-latex-packages-alist nil
189 "Alist of packages to be inserted in the header.
190 Each cell is of the forma \( \"option\" . \"package\" \)."
191 :group 'org-export-latex
192 :type 'alist)
194 (defcustom org-export-latex-low-levels 'description
195 "How to convert sections below the current level of sectioning,
196 as specified by `org-export-headline-levels' or the value of \"H:\"
197 in Org's #+OPTION line.
199 This can be either nil (skip the sections), 'description (convert
200 the sections as descriptive lists) or a string to be used instead
201 of \\section{%s}. In this latter case, the %s stands here for the
202 inserted headline and is mandatory."
203 :group 'org-export-latex
204 :type '(choice (const :tag "Ignore" nil)
205 (symbol :tag "Convert as descriptive list" description)
206 (string :tag "Use a section string" :value "\\subparagraph{%s}")))
208 (defcustom org-export-latex-remove-from-headlines
209 '(:todo t :priority t :tags t)
210 "A plist of keywords to remove from headlines.
211 Non-nil means remove this keyword type from the headline.
213 Don't remove the keys, just change their values."
214 :type 'plist
215 :group 'org-export-latex)
217 (defcustom org-export-latex-image-default-option "width=10em"
218 "Default option for images."
219 :group 'org-export-latex
220 :type 'string)
222 (defcustom org-export-latex-coding-system nil
223 "Coding system for the exported LaTex file."
224 :group 'org-export-latex
225 :type 'coding-system)
227 (defcustom org-list-radio-list-templates
228 '((latex-mode "% BEGIN RECEIVE ORGLST %n
229 % END RECEIVE ORGLST %n
230 \\begin{comment}
231 #+ORGLST: SEND %n org-list-to-latex
232 | | |
233 \\end{comment}\n")
234 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
235 @c END RECEIVE ORGLST %n
236 @ignore
237 #+ORGLST: SEND %n org-list-to-texinfo
238 | | |
239 @end ignore\n")
240 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
241 <!-- END RECEIVE ORGLST %n -->
242 <!--
243 #+ORGLST: SEND %n org-list-to-html
244 | | |
245 -->\n"))
246 "Templates for radio lists in different major modes.
247 All occurrences of %n in a template will be replaced with the name of the
248 list, obtained by prompting the user."
249 :group 'org-plain-lists
250 :type '(repeat
251 (list (symbol :tag "Major mode")
252 (string :tag "Format"))))
254 ;;; Autoload functions:
256 ;;;###autoload
257 (defun org-export-as-latex-batch ()
258 "Call `org-export-as-latex', may be used in batch processing as
259 emacs --batch
260 --load=$HOME/lib/emacs/org.el
261 --eval \"(setq org-export-headline-levels 2)\"
262 --visit=MyFile --funcall org-export-as-latex-batch"
263 (org-export-as-latex org-export-headline-levels 'hidden))
265 ;;;###autoload
266 (defun org-export-as-latex-to-buffer (arg)
267 "Call `org-exort-as-latex` with output to a temporary buffer.
268 No file is created. The prefix ARG is passed through to `org-export-as-latex'."
269 (interactive "P")
270 (org-export-as-latex arg nil nil "*Org LaTeX Export*")
271 (switch-to-buffer-other-window "*Org LaTeX Export*"))
273 ;;;###autoload
274 (defun org-replace-region-by-latex (beg end)
275 "Replace the region from BEG to END with its LaTeX export.
276 It assumes the region has `org-mode' syntax, and then convert it to
277 LaTeX. This can be used in any buffer. For example, you could
278 write an itemized list in `org-mode' syntax in an LaTeX buffer and
279 then use this command to convert it."
280 (interactive "r")
281 (let (reg latex buf)
282 (save-window-excursion
283 (if (org-mode-p)
284 (setq latex (org-export-region-as-latex
285 beg end t 'string))
286 (setq reg (buffer-substring beg end)
287 buf (get-buffer-create "*Org tmp*"))
288 (save-excursion
289 (set-buffer buf)
290 (erase-buffer)
291 (insert reg)
292 (org-mode)
293 (setq latex (org-export-region-as-latex
294 (point-min) (point-max) t 'string)))
295 (kill-buffer buf)))
296 (delete-region beg end)
297 (insert latex)))
299 ;;;###autoload
300 (defun org-export-region-as-latex (beg end &optional body-only buffer)
301 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
302 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
303 contents, and only produce the region of converted text, useful for
304 cut-and-paste operations.
305 If BUFFER is a buffer or a string, use/create that buffer as a target
306 of the converted LaTeX. If BUFFER is the symbol `string', return the
307 produced LaTeX as a string and leave not buffer behind. For example,
308 a Lisp program could call this function in the following way:
310 (setq latex (org-export-region-as-latex beg end t 'string))
312 When called interactively, the output buffer is selected, and shown
313 in a window. A non-interactive call will only retunr the buffer."
314 (interactive "r\nP")
315 (when (interactive-p)
316 (setq buffer "*Org LaTeX Export*"))
317 (let ((transient-mark-mode t) (zmacs-regions t)
318 rtn)
319 (goto-char end)
320 (set-mark (point)) ;; to activate the region
321 (goto-char beg)
322 (setq rtn (org-export-as-latex
323 nil nil nil
324 buffer body-only))
325 (if (fboundp 'deactivate-mark) (deactivate-mark))
326 (if (and (interactive-p) (bufferp rtn))
327 (switch-to-buffer-other-window rtn)
328 rtn)))
330 ;;;###autoload
331 (defun org-export-as-latex (arg &optional hidden ext-plist
332 to-buffer body-only pub-dir)
333 "Export current buffer to a LaTeX file.
334 If there is an active region, export only the region. The prefix
335 ARG specifies how many levels of the outline should become
336 headlines. The default is 3. Lower levels will be exported
337 depending on `org-export-latex-low-levels'. The default is to
338 convert them as description lists. When HIDDEN is non-nil, don't
339 display the LaTeX buffer. EXT-PLIST is a property list with
340 external parameters overriding org-mode's default settings, but
341 still inferior to file-local settings. When TO-BUFFER is
342 non-nil, create a buffer with that name and export to that
343 buffer. If TO-BUFFER is the symbol `string', don't leave any
344 buffer behind but just return the resulting LaTeX as a string.
345 When BODY-ONLY is set, don't produce the file header and footer,
346 simply return the content of \begin{document}...\end{document},
347 without even the \begin{document} and \end{document} commands.
348 when PUB-DIR is set, use this as the publishing directory."
349 (interactive "P")
350 ;; Make sure we have a file name when we need it.
351 (when (and (not (or to-buffer body-only))
352 (not buffer-file-name))
353 (if (buffer-base-buffer)
354 (org-set-local 'buffer-file-name
355 (with-current-buffer (buffer-base-buffer)
356 buffer-file-name))
357 (error "Need a file name to be able to export")))
359 (message "Exporting to LaTeX...")
360 (org-update-radio-target-regexp)
361 (org-export-latex-set-initial-vars ext-plist arg)
362 (let* ((wcf (current-window-configuration))
363 (opt-plist org-export-latex-options-plist)
364 (region-p (org-region-active-p))
365 (subtree-p
366 (when region-p
367 (save-excursion
368 (goto-char (region-beginning))
369 (and (org-at-heading-p)
370 (>= (org-end-of-subtree t t) (region-end))))))
371 (title (or (and subtree-p (org-export-get-title-from-subtree))
372 (plist-get opt-plist :title)
373 (and (not
374 (plist-get opt-plist :skip-before-1st-heading))
375 (org-export-grab-title-from-buffer))
376 (file-name-sans-extension
377 (file-name-nondirectory buffer-file-name))))
378 (filename (concat (file-name-as-directory
379 (or pub-dir
380 (org-export-directory :LaTeX ext-plist)))
381 (file-name-sans-extension
382 (file-name-nondirectory ;sans-extension
383 buffer-file-name)) ".tex"))
384 (filename (if (equal (file-truename filename)
385 (file-truename buffer-file-name))
386 (concat filename ".tex")
387 filename))
388 (buffer (if to-buffer
389 (cond
390 ((eq to-buffer 'string) (get-buffer-create
391 "*Org LaTeX Export*"))
392 (t (get-buffer-create to-buffer)))
393 (find-file-noselect filename)))
394 (odd org-odd-levels-only)
395 (header (org-export-latex-make-header title opt-plist))
396 (skip (cond (subtree-p nil)
397 (region-p t)
398 ;; never skip first lines when exporting a subtree
399 (t (plist-get opt-plist :skip-before-1st-heading))))
400 (text (plist-get opt-plist :text))
401 (first-lines (if skip "" (org-export-latex-first-lines)))
402 (coding-system (and (boundp 'buffer-file-coding-system)
403 buffer-file-coding-system))
404 (coding-system-for-write (or org-export-latex-coding-system
405 coding-system))
406 (save-buffer-coding-system (or org-export-latex-coding-system
407 coding-system))
408 (region (buffer-substring
409 (if region-p (region-beginning) (point-min))
410 (if region-p (region-end) (point-max))))
411 (string-for-export
412 (org-cleaned-string-for-export
413 region :emph-multiline t
414 :for-LaTeX t
415 :comments nil
416 :add-text (if (eq to-buffer 'string) nil text)
417 :skip-before-1st-heading skip
418 :LaTeX-fragments nil)))
420 (set-buffer buffer)
421 (erase-buffer)
423 (and (fboundp 'set-buffer-file-coding-system)
424 (set-buffer-file-coding-system coding-system-for-write))
426 ;; insert the header and initial document commands
427 (unless (or (eq to-buffer 'string) body-only)
428 (insert header))
430 ;; insert text found in #+TEXT
431 (when (and text (not (eq to-buffer 'string)))
432 (insert (org-export-latex-content
433 text '(lists tables fixed-width keywords))
434 "\n\n"))
436 ;; insert lines before the first headline
437 (unless (or skip (eq to-buffer 'string))
438 (insert first-lines))
440 ;; handle the case where the region does not begin with a section
441 (when region-p
442 (insert (with-temp-buffer
443 (insert string-for-export)
444 (org-export-latex-first-lines))))
446 ;; export the content of headlines
447 (org-export-latex-global
448 (with-temp-buffer
449 (insert string-for-export)
450 (goto-char (point-min))
451 (when (re-search-forward "^\\(\\*+\\) " nil t)
452 (let* ((asters (length (match-string 1)))
453 (level (if odd (- asters 2) (- asters 1))))
454 (setq org-export-latex-add-level
455 (if odd (1- (/ (1+ asters) 2)) (1- asters)))
456 (org-export-latex-parse-global level odd)))))
458 ;; finalization
459 (unless body-only (insert "\n\\end{document}"))
460 (or to-buffer (progn (save-buffer) (kill-buffer (current-buffer))))
461 (goto-char (point-min))
462 (message "Exporting to LaTeX...done")
463 (prog1
464 (if (eq to-buffer 'string)
465 (prog1 (buffer-substring (point-min) (point-max))
466 (kill-buffer (current-buffer)))
467 (current-buffer))
468 (set-window-configuration wcf))))
470 ;;; Parsing functions:
472 (defun org-export-latex-parse-global (level odd)
473 "Parse the current buffer recursively, starting at LEVEL.
474 If ODD is non-nil, assume the buffer only contains odd sections.
475 Return a list reflecting the document structure."
476 (save-excursion
477 (goto-char (point-min))
478 (let* ((cnt 0) output
479 (depth org-export-latex-sectioning-depth))
480 (while (re-search-forward
481 (concat "^\\(\\(?:\\*\\)\\{"
482 (number-to-string (+ (if odd 2 1) level))
483 "\\}\\) \\(.*\\)$")
484 ;; make sure that there is no upper heading
485 (when (> level 0)
486 (save-excursion
487 (save-match-data
488 (re-search-forward
489 (concat "^\\(\\(?:\\*\\)\\{"
490 (number-to-string level)
491 "\\}\\) \\(.*\\)$") nil t)))) t)
492 (setq cnt (1+ cnt))
493 (let* ((pos (match-beginning 0))
494 (heading (match-string 2))
495 (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
496 (save-excursion
497 (narrow-to-region
498 (point)
499 (save-match-data
500 (if (re-search-forward
501 (concat "^\\(\\(?:\\*\\)\\{"
502 (number-to-string (+ (if odd 2 1) level))
503 "\\}\\) \\(.*\\)$") nil t)
504 (match-beginning 0)
505 (point-max))))
506 (goto-char (point-min))
507 (setq output
508 (append output
509 (list
510 (list
511 `(pos . ,pos)
512 `(level . ,nlevel)
513 `(occur . ,cnt)
514 `(heading . ,heading)
515 `(content . ,(org-export-latex-parse-content))
516 `(subcontent . ,(org-export-latex-parse-subcontent
517 level odd)))))))
518 (widen)))
519 (list output))))
521 (defun org-export-latex-parse-content ()
522 "Extract the content of a section."
523 (let ((beg (point))
524 (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t)
525 (progn (beginning-of-line) (point))
526 (point-max))))
527 (buffer-substring beg end)))
529 (defun org-export-latex-parse-subcontent (level odd)
530 "Extract the subcontent of a section at LEVEL.
531 If ODD Is non-nil, assume subcontent only contains odd sections."
532 (if (not (re-search-forward
533 (concat "^\\(\\(?:\\*\\)\\{"
534 (number-to-string (+ (if odd 4 2) level))
535 "\\}\\) \\(.*\\)$")
536 nil t))
537 nil ; subcontent is nil
538 (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
540 ;;; Rendering functions:
541 (defun org-export-latex-global (content)
542 "Export CONTENT to LaTeX.
543 CONTENT is an element of the list produced by
544 `org-export-latex-parse-global'."
545 (if (eq (car content) 'subcontent)
546 (mapc 'org-export-latex-sub (cdr content))
547 (org-export-latex-sub (car content))))
549 (defun org-export-latex-sub (subcontent)
550 "Export the list SUBCONTENT to LaTeX.
551 SUBCONTENT is an alist containing information about the headline
552 and its content."
553 (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
554 (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
556 (defun org-export-latex-subcontent (subcontent num)
557 "Export each cell of SUBCONTENT to LaTeX."
558 (let ((heading (org-export-latex-fontify-headline
559 (cdr (assoc 'heading subcontent))))
560 (level (- (cdr (assoc 'level subcontent))
561 org-export-latex-add-level))
562 (occur (number-to-string (cdr (assoc 'occur subcontent))))
563 (content (cdr (assoc 'content subcontent)))
564 (subcontent (cadr (assoc 'subcontent subcontent))))
565 (cond
566 ;; Normal conversion
567 ((<= level org-export-latex-sectioning-depth)
568 (let ((sec (nth (1- level) org-export-latex-sectioning)))
569 (insert (format (if num (car sec) (cdr sec)) heading) "\n"))
570 (insert (org-export-latex-content content))
571 (cond ((stringp subcontent) (insert subcontent))
572 ((listp subcontent) (org-export-latex-sub subcontent))))
573 ;; At a level under the hl option: we can drop this subsection
574 ((> level org-export-latex-sectioning-depth)
575 (cond ((eq org-export-latex-low-levels 'description)
576 (insert (format "\\begin{description}\n\n\\item[%s]\n\n" heading))
577 (insert (org-export-latex-content content))
578 (cond ((stringp subcontent) (insert subcontent))
579 ((listp subcontent) (org-export-latex-sub subcontent)))
580 (insert "\\end{description}\n"))
581 ((stringp org-export-latex-low-levels)
582 (insert (format org-export-latex-low-levels heading) "\n")
583 (insert (org-export-latex-content content))
584 (cond ((stringp subcontent) (insert subcontent))
585 ((listp subcontent) (org-export-latex-sub subcontent)))))))))
587 ;;; Exporting internals:
588 (defun org-export-latex-set-initial-vars (ext-plist level)
589 "Store org local variables required for LaTeX export.
590 EXT-PLIST is an optional additional plist.
591 LEVEL indicates the default depth for export."
592 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
593 org-export-latex-all-targets-re
594 (org-make-target-link-regexp (org-all-targets))
595 org-export-latex-options-plist
596 (org-combine-plists (org-default-export-plist) ext-plist
597 (org-infile-export-plist))
598 org-export-latex-class
599 (save-excursion
600 (goto-char (point-min))
601 (if (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
602 (assoc (match-string 1) org-export-latex-classes))
603 (match-string 1)
604 org-export-latex-default-class))
605 org-export-latex-header
606 (cadr (assoc org-export-latex-class org-export-latex-classes))
607 org-export-latex-sectioning
608 (cddr (assoc org-export-latex-class org-export-latex-classes))
609 org-export-latex-sectioning-depth
610 (or level
611 (let ((hl-levels
612 (plist-get org-export-latex-options-plist :headline-levels))
613 (sec-depth (length org-export-latex-sectioning)))
614 (if (> hl-levels sec-depth) sec-depth hl-levels)))))
616 (defun org-export-latex-make-header (title opt-plist)
617 "Make the LaTeX header and return it as a string.
618 TITLE is the current title from the buffer or region.
619 OPT-PLIST is the options plist for current buffer."
620 (let ((toc (plist-get opt-plist :table-of-contents))
621 (author (plist-get opt-plist :author)))
622 (concat
623 (if (plist-get opt-plist :time-stamp-file)
624 (format-time-string "% Created %Y-%m-%d %a %H:%M\n"))
625 ;; insert LaTeX custom header
626 org-export-latex-header
627 "\n"
628 ;; insert information on LaTeX packages
629 (when org-export-latex-packages-alist
630 (mapconcat (lambda(p)
631 (if (equal "" (car p))
632 (format "\\usepackage{%s}" (cadr p))
633 (format "\\usepackage[%s]{%s}"
634 (car p) (cadr p))))
635 org-export-latex-packages-alist "\n"))
636 ;; insert additional commands in the header
637 org-export-latex-append-header
638 ;; insert the title
639 (format
640 "\n\n\\title{%s}\n"
641 ;; convert the title
642 (org-export-latex-content
643 title '(lists tables fixed-width keywords)))
644 ;; insert author info
645 (if (plist-get opt-plist :author-info)
646 (format "\\author{%s}\n"
647 (or author user-full-name))
648 (format "%%\\author{%s}\n"
649 (or author user-full-name)))
650 ;; insert the date
651 (format "\\date{%s}\n"
652 (format-time-string
653 (or (plist-get opt-plist :date)
654 org-export-latex-date-format)))
655 ;; beginning of the document
656 "\n\\begin{document}\n\n"
657 ;; insert the title command
658 (if (string-match "%s" org-export-latex-title-command)
659 (format org-export-latex-title-command title)
660 org-export-latex-title-command)
661 "\n\n"
662 ;; table of contents
663 (when (and org-export-with-toc
664 (plist-get opt-plist :section-numbers))
665 (cond ((numberp toc)
666 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
667 (min toc (plist-get opt-plist :headline-levels))))
668 (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
669 (plist-get opt-plist :headline-levels))))))))
671 (defun org-export-latex-first-lines (&optional comments)
672 "Export the first lines before first headline.
673 COMMENTS is either nil to replace them with the empty string or a
674 formatting string like %%%%s if we want to comment them out."
675 (save-excursion
676 (goto-char (point-min))
677 (if (org-at-heading-p) (beginning-of-line 2))
678 (let* ((pt (point))
679 (end (if (and (re-search-forward "^\\* " nil t)
680 (not (eq pt (match-beginning 0))))
681 (goto-char (match-beginning 0))
682 (goto-char (point-max)))))
683 (org-export-latex-content
684 (org-cleaned-string-for-export
685 (buffer-substring (point-min) end)
686 :for-LaTeX t
687 :emph-multiline t
688 :add-text nil
689 :comments nil
690 :skip-before-1st-heading nil
691 :LaTeX-fragments nil)))))
693 (defun org-export-latex-content (content &optional exclude-list)
694 "Convert CONTENT string to LaTeX.
695 Don't perform conversions that are in EXCLUDE-LIST. Recognized
696 conversion types are: quotation-marks, emphasis, sub-superscript,
697 links, keywords, lists, tables, fixed-width"
698 (with-temp-buffer
699 (insert content)
700 (unless (memq 'quotation-marks exclude-list)
701 (org-export-latex-quotation-marks))
702 (unless (memq 'emphasis exclude-list)
703 (when (plist-get org-export-latex-options-plist :emphasize)
704 (org-export-latex-fontify)))
705 (unless (memq 'sub-superscript exclude-list)
706 (org-export-latex-special-chars
707 (plist-get org-export-latex-options-plist :sub-superscript)))
708 (unless (memq 'links exclude-list)
709 (org-export-latex-links))
710 (unless (memq 'keywords exclude-list)
711 (org-export-latex-keywords
712 (plist-get org-export-latex-options-plist :timestamps)))
713 (unless (memq 'lists exclude-list)
714 (org-export-latex-lists))
715 (unless (memq 'tables exclude-list)
716 (org-export-latex-tables
717 (plist-get org-export-latex-options-plist :tables)))
718 (unless (memq 'fixed-width exclude-list)
719 (org-export-latex-fixed-width
720 (plist-get org-export-latex-options-plist :fixed-width)))
721 ;; return string
722 (buffer-substring (point-min) (point-max))))
724 (defun org-export-latex-protect-string (s)
725 "Prevent further conversion for string S by adding the
726 org-protect property."
727 (add-text-properties 0 (length s) '(org-protected t) s) s)
729 (defun org-export-latex-protect-char-in-string (char-list string)
730 "Add org-protected text-property to char from CHAR-LIST in STRING."
731 (with-temp-buffer
732 (save-match-data
733 (insert string)
734 (goto-char (point-min))
735 (while (re-search-forward (regexp-opt char-list) nil t)
736 (add-text-properties (match-beginning 0)
737 (match-end 0) '(org-protected t)))
738 (buffer-string))))
740 (defun org-export-latex-keywords-maybe (remove-list)
741 "Maybe remove keywords depending on rules in REMOVE-LIST."
742 (goto-char (point-min))
743 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
744 (case-fold-search nil))
745 ;; convert TODO keywords
746 (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
747 (if (plist-get remove-list :todo)
748 (replace-match "")
749 (replace-match (format "\\texttt{%s}" (match-string 1)) t t)))
750 ;; convert priority string
751 (when (re-search-forward "\\[\\\\#.\\]" nil t)
752 (if (plist-get remove-list :priority)
753 (replace-match "")
754 (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))
755 ;; convert tags
756 (when (re-search-forward "\\(:[a-zA-Z0-9]+\\)+:" nil t)
757 (if (or (not org-export-with-tags)
758 (plist-get remove-list :tags))
759 (replace-match "")
760 (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))))
762 (defun org-export-latex-fontify-headline (string)
763 "Fontify special words in string."
764 (with-temp-buffer
765 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
766 ;; the beginning of the buffer - inserting "\n" is safe here though.
767 (insert "\n" string)
768 (goto-char (point-min))
769 (when (plist-get org-export-latex-options-plist :emphasize)
770 (org-export-latex-fontify))
771 (org-export-latex-special-chars
772 (plist-get org-export-latex-options-plist :sub-superscript))
773 (org-export-latex-keywords-maybe
774 org-export-latex-remove-from-headlines)
775 (org-export-latex-links)
776 (org-trim (buffer-substring-no-properties (point-min) (point-max)))))
778 (defun org-export-latex-quotation-marks ()
779 "Export question marks depending on language conventions."
780 (let* ((lang (plist-get org-export-latex-options-plist :language))
781 (quote-rpl (if (equal lang "fr")
782 '(("\\(\\s-\\)\"" "«~")
783 ("\\(\\S-\\)\"" "~»")
784 ("\\(\\s-\\)'" "`"))
785 '(("\\(\\s-\\)\"" "``")
786 ("\\(\\S-\\)\"" "''")
787 ("\\(\\s-\\)'" "`")))))
788 (mapc (lambda(l) (goto-char (point-min))
789 (while (re-search-forward (car l) nil t)
790 (let ((rpl (concat (match-string 1) (cadr l))))
791 (org-export-latex-protect-string rpl)
792 (org-if-unprotected
793 (replace-match rpl t t))))) quote-rpl)))
795 (defun org-export-latex-special-chars (sub-superscript)
796 "Export special characters to LaTeX.
797 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
798 See the `org-export-latex.el' code for a complete conversion table."
799 (goto-char (point-min))
800 (mapc (lambda(c)
801 (goto-char (point-min))
802 (while (re-search-forward c nil t)
803 ;; Put the point where to check for org-protected
804 (unless (or (get-text-property (match-beginning 2) 'org-protected)
805 (org-at-table-p))
806 (cond ((member (match-string 2) '("\\$" "$"))
807 (if (equal (match-string 2) "\\$")
808 (replace-match (concat (match-string 1) "$"
809 (match-string 3)) t t)
810 (replace-match (concat (match-string 1) "\\$"
811 (match-string 3)) t t)))
812 ((member (match-string 2) '("&" "%" "#"))
813 (if (equal (match-string 1) "\\")
814 (replace-match (match-string 2) t t)
815 (replace-match (concat (match-string 1) "\\"
816 (match-string 2)) t t)))
817 ((equal (match-string 2) "...")
818 (replace-match
819 (concat (match-string 1)
820 (org-export-latex-protect-string "\\ldots{}")) t t))
821 ((equal (match-string 2) "~")
822 (cond ((equal (match-string 1) "\\") nil)
823 ((eq 'org-link (get-text-property 0 'face (match-string 2)))
824 (replace-match (concat (match-string 1) "\\~") t t))
825 (t (replace-match
826 (org-export-latex-protect-string
827 (concat (match-string 1) "\\~{}")) t t))))
828 ((member (match-string 2) '("{" "}"))
829 (unless (save-match-data (org-inside-LaTeX-fragment-p))
830 (if (equal (match-string 1) "\\")
831 (replace-match (match-string 2) t t)
832 (replace-match (concat (match-string 1) "\\"
833 (match-string 2)) t t)))))
834 (unless (save-match-data (org-inside-LaTeX-fragment-p))
835 (cond ((equal (match-string 2) "\\")
836 (replace-match (or (save-match-data
837 (org-export-latex-treat-backslash-char
838 (match-string 1)
839 (match-string 3))) "") t t))
840 ((member (match-string 2) '("_" "^"))
841 (replace-match (or (save-match-data
842 (org-export-latex-treat-sub-super-char
843 sub-superscript
844 (match-string 1)
845 (match-string 2)
846 (match-string 3))) "") t t)))))))
847 '("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
848 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
849 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
850 "\\(.\\|^\\)\\(&\\)"
851 "\\(.\\|^\\)\\(#\\)"
852 "\\(.\\|^\\)\\(%\\)"
853 "\\(.\\|^\\)\\({\\)"
854 "\\(.\\|^\\)\\(}\\)"
855 "\\(.\\|^\\)\\(~\\)"
856 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
857 ;; (?\< . "\\textless{}")
858 ;; (?\> . "\\textgreater{}")
861 (defun org-export-latex-treat-sub-super-char
862 (subsup string-before char string-after)
863 "Convert the \"_\" and \"^\" characters to LaTeX.
864 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
865 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
866 (cond ((equal string-before "\\")
867 (concat string-before char string-after))
868 ;; this is part of a math formula
869 ((and (string-match "\\S-+" string-before)
870 (string-match "\\S-+" string-after))
871 (cond ((eq 'org-link (get-text-property 0 'face char))
872 (concat string-before "\\" char string-after))
873 ((save-match-data (org-inside-LaTeX-fragment-p))
874 (if subsup
875 (cond ((eq 1 (length string-after))
876 (concat string-before char string-after))
877 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
878 (format "%s%s{%s}" string-before char
879 (match-string 1 string-after))))))
880 ((and subsup
881 (> (length string-after) 1)
882 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
883 (format "$%s%s{%s}$" string-before char
884 (match-string 1 string-after)))
885 (subsup (concat "$" string-before char string-after "$"))
886 (t (org-export-latex-protect-string
887 (concat string-before "\\" char "{}" string-after)))))
888 (t (org-export-latex-protect-string
889 (concat string-before "\\" char "{}" string-after)))))
891 (defun org-export-latex-treat-backslash-char (string-before string-after)
892 "Convert the \"$\" special character to LaTeX.
893 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
894 (cond ((member (list string-after) org-html-entities)
895 ;; backslash is part of a special entity (like "\alpha")
896 (concat string-before "$\\"
897 (or (cdar (member (list string-after) org-html-entities))
898 string-after) "$"))
899 ((and (not (string-match "^[ \n\t]" string-after))
900 (not (string-match "[ \t]\\'\\|^" string-before)))
901 ;; backslash is inside a word
902 (org-export-latex-protect-string
903 (concat string-before "\\textbackslash{}" string-after)))
904 ((not (or (equal string-after "")
905 (string-match "^[ \t\n]" string-after)))
906 ;; backslash might escape a character (like \#) or a user TeX
907 ;; macro (like \setcounter)
908 (org-export-latex-protect-string
909 (concat string-before "\\" string-after)))
910 ((and (string-match "^[ \t\n]" string-after)
911 (string-match "[ \t\n]\\'" string-before))
912 ;; backslash is alone, convert it to $\backslash$
913 (org-export-latex-protect-string
914 (concat string-before "\\textbackslash{}" string-after)))
915 (t (org-export-latex-protect-string
916 (concat string-before "\\textbackslash{}" string-after)))))
918 (defun org-export-latex-keywords (timestamps)
919 "Convert special keywords to LaTeX.
920 Regexps are those from `org-export-latex-special-string-regexps'."
921 (let ((rg org-export-latex-special-string-regexps) r)
922 (while (setq r (pop rg))
923 (goto-char (point-min))
924 (while (re-search-forward (eval r) nil t)
925 (if (not timestamps)
926 (replace-match (format "\\\\texttt{%s}" (match-string 0)) t)
927 (replace-match ""))))))
929 (defun org-export-latex-fixed-width (opt)
930 "When OPT is non-nil convert fixed-width sections to LaTeX."
931 (goto-char (point-min))
932 (while (re-search-forward "^[ \t]*:" nil t)
933 (if opt
934 (progn (goto-char (match-beginning 0))
935 (insert "\\begin{verbatim}\n")
936 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
937 (replace-match (concat (match-string 1)
938 (match-string 2)) t t)
939 (forward-line))
940 (insert "\\end{verbatim}\n\n"))
941 (progn (goto-char (match-beginning 0))
942 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
943 (replace-match (concat "%" (match-string 1)
944 (match-string 2)) t t)
945 (forward-line))))))
947 (defun org-export-latex-tables (insert)
948 "Convert tables to LaTeX and INSERT it."
949 (goto-char (point-min))
950 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
951 ;; FIXME really need to save-excursion?
952 (save-excursion (org-table-align))
953 (let* ((beg (org-table-begin))
954 (end (org-table-end))
955 (raw-table (buffer-substring-no-properties beg end))
956 fnum fields line lines olines gr colgropen line-fmt align)
957 (if org-export-latex-tables-verbatim
958 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
959 "\\end{verbatim}\n")))
960 (apply 'delete-region (list beg end))
961 (insert tbl))
962 (progn
963 (setq lines (split-string raw-table "\n" t))
964 (apply 'delete-region (list beg end))
965 (when org-export-table-remove-special-lines
966 (setq lines (org-table-clean-before-export lines)))
967 ;; make a formatting string to reflect aligment
968 (setq olines lines)
969 (while (and (not line-fmt) (setq line (pop olines)))
970 (unless (string-match "^[ \t]*|-" line)
971 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
972 (setq fnum (make-vector (length fields) 0))
973 (setq line-fmt
974 (mapconcat
975 (lambda (x)
976 (setq gr (pop org-table-colgroup-info))
977 (format "%s%%s%s"
978 (cond ((eq gr ':start)
979 (prog1 (if colgropen "|" "")
980 (setq colgropen t)))
981 ((eq gr ':startend)
982 (prog1 (if colgropen "|" "|")
983 (setq colgropen nil)))
984 (t ""))
985 (if (memq gr '(:end :startend))
986 (progn (setq colgropen nil) "|")
987 "")))
988 fnum ""))))
989 ;; fix double || in line-fmt
990 (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
991 ;; maybe remove the first and last "|"
992 (when (and (not org-export-latex-tables-column-borders)
993 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
994 (setq line-fmt (match-string 2 line-fmt)))
995 ;; format alignment
996 (setq align (apply 'format
997 (cons line-fmt
998 (mapcar (lambda (x) (if x "r" "l"))
999 org-table-last-alignment))))
1000 ;; prepare the table to send to orgtbl-to-latex
1001 (setq lines
1002 (mapcar
1003 (lambda(elem)
1004 (or (and (string-match "[ \t]*|-+" elem) 'hline)
1005 (split-string (org-trim elem) "|" t)))
1006 lines))
1007 (when insert
1008 (insert (orgtbl-to-latex
1009 lines `(:tstart ,(concat "\\begin{tabular}{" align "}")))
1010 "\n\n")))))))
1012 (defun org-export-latex-fontify ()
1013 "Convert fontification to LaTeX."
1014 (goto-char (point-min))
1015 (while (re-search-forward org-emph-re nil t)
1016 ;; The match goes one char after the *string*
1017 (let ((emph (assoc (match-string 3)
1018 org-export-latex-emphasis-alist))
1019 rpl)
1020 (unless (get-text-property (1- (point)) 'org-protected)
1021 (setq rpl (concat (match-string 1)
1022 (format (org-export-latex-protect-char-in-string
1023 '("\\" "{" "}") (cadr emph))
1024 (match-string 4))
1025 (match-string 5)))
1026 (if (caddr emph)
1027 (setq rpl (org-export-latex-protect-string rpl)))
1028 (replace-match rpl t t)))
1029 (backward-char)))
1031 (defun org-export-latex-links ()
1032 ;; Make sure to use the LaTeX hyperref and graphicx package
1033 ;; or send some warnings.
1034 "Convert links to LaTeX."
1035 (goto-char (point-min))
1036 (while (re-search-forward org-bracket-link-analytic-regexp nil t)
1037 (org-if-unprotected
1038 (goto-char (match-beginning 0))
1039 (let* ((re-radio org-export-latex-all-targets-re)
1040 (remove (list (match-beginning 0) (match-end 0)))
1041 (type (match-string 2))
1042 (raw-path (match-string 3))
1043 (full-raw-path (concat (match-string 1) raw-path))
1044 (desc (match-string 5))
1045 imgp radiop
1046 ;; define the path of the link
1047 (path (cond
1048 ((member type '("http" "https" "ftp"))
1049 (concat type ":" raw-path))
1050 ((and re-radio (string-match re-radio raw-path))
1051 (setq radiop t))
1052 ((equal type "mailto")
1053 (concat type ":" raw-path))
1054 ((equal type "file")
1055 (if (and (or (org-file-image-p (expand-file-name raw-path))
1056 (string-match "\\.eps$" raw-path))
1057 (equal desc full-raw-path))
1058 (setq imgp t)
1059 (progn (when (string-match "\\(.+\\)::.+" raw-path)
1060 (setq raw-path (match-string 1 raw-path)))
1061 (if (file-exists-p raw-path)
1062 (concat type "://" (expand-file-name raw-path))
1063 (concat type "://" (org-export-directory
1064 :LaTeX org-export-latex-options-plist)
1065 raw-path))))))))
1066 ;; process with link inserting
1067 (apply 'delete-region remove)
1068 (cond ((and imgp (plist-get org-export-latex-options-plist :inline-images))
1069 (insert (format "\\includegraphics[%s]{%s}"
1070 ;; image option should be set be a comment line
1071 org-export-latex-image-default-option
1072 (expand-file-name raw-path))))
1073 (radiop (insert (format "\\hyperref[%s]{%s}" raw-path desc)))
1074 (path (insert (format "\\href{%s}{%s}" path desc)))
1075 (t (insert "\\texttt{" desc "}")))))))
1077 (defvar org-latex-entities) ; defined below
1079 (defun org-export-latex-cleaned-string ()
1080 "Clean stuff in the LaTeX export."
1082 ;; Preserve line breaks
1083 (goto-char (point-min))
1084 (while (re-search-forward "\\\\\\\\" nil t)
1085 (add-text-properties (match-beginning 0) (match-end 0)
1086 '(org-protected t)))
1088 ;; Convert LaTeX to \LaTeX{}
1089 (goto-char (point-min))
1090 (let ((case-fold-search nil) rpl)
1091 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
1092 (replace-match (org-export-latex-protect-string
1093 (concat (match-string 1) "\\LaTeX{}")) t t)))
1095 ;; Convert horizontal rules
1096 (goto-char (point-min))
1097 (while (re-search-forward "^----+.$" nil t)
1098 (replace-match (org-export-latex-protect-string "\\hrule") t t))
1100 ;; Protect LaTeX commands like \commad[...]{...} or \command{...}
1101 (goto-char (point-min))
1102 (while (re-search-forward "\\\\[a-zA-Z]+\\(?:\\[.*\\]\\)?{.*}" nil t)
1103 (add-text-properties (match-beginning 0) (match-end 0)
1104 '(org-protected t)))
1106 ;; Protect LaTeX entities
1107 (goto-char (point-min))
1108 (while (re-search-forward (regexp-opt org-latex-entities) nil t)
1109 (add-text-properties (match-beginning 0) (match-end 0)
1110 '(org-protected t)))
1112 ;; Replace radio links
1113 (goto-char (point-min))
1114 (while (re-search-forward
1115 (concat "<<<?" org-export-latex-all-targets-re
1116 ">>>?\\((INVISIBLE)\\)?") nil t)
1117 (replace-match
1118 (org-export-latex-protect-string
1119 (format "\\label{%s}%s"(match-string 1)
1120 (if (match-string 2) "" (match-string 1)))) t t))
1122 ;; Delete @<...> constructs
1123 ;; Thanks to Daniel Clemente for this regexp
1124 (goto-char (point-min))
1125 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
1126 (replace-match ""))
1128 ;; When converting to LaTeX, replace footnotes
1129 ;; FIXME: don't protect footnotes from conversion
1130 (when (plist-get org-export-latex-options-plist :footnotes)
1131 (goto-char (point-min))
1132 (while (re-search-forward "\\[[0-9]+\\]" nil t)
1133 (when (save-match-data
1134 (save-excursion (beginning-of-line)
1135 (looking-at "[^:|#]")))
1136 (let ((foot-beg (match-beginning 0))
1137 (foot-end (match-end 0))
1138 (foot-prefix (match-string 0))
1139 footnote footnote-rpl)
1140 (save-excursion
1141 (when (search-forward foot-prefix nil t)
1142 (replace-match "")
1143 (let ((end (save-excursion
1144 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
1145 (match-beginning 0) (point-max)))))
1146 (setq footnote (concat (org-trim (buffer-substring (point) end))
1147 " ")) ; prevent last } being part of a link
1148 (delete-region (point) end))
1149 (goto-char foot-beg)
1150 (delete-region foot-beg foot-end)
1151 (unless (null footnote)
1152 (setq footnote-rpl (format "\\footnote{%s}" footnote))
1153 (add-text-properties 0 10 '(org-protected t) footnote-rpl)
1154 (add-text-properties (1- (length footnote-rpl))
1155 (length footnote-rpl)
1156 '(org-protected t) footnote-rpl)
1157 (insert footnote-rpl)))))))
1159 ;; Replace footnote section tag for LaTeX
1160 (goto-char (point-min))
1161 (while (re-search-forward
1162 (concat "^" footnote-section-tag-regexp) nil t)
1163 (replace-match ""))))
1165 ;;; List handling:
1167 (defun org-export-latex-lists ()
1168 "Replace plain text lists in current buffer into LaTeX lists."
1169 "Convert lists to LaTeX."
1170 (goto-char (point-min))
1171 (while (re-search-forward org-export-latex-list-beginning-re nil t)
1172 (beginning-of-line)
1173 (insert (org-list-to-latex (org-list-parse-list t)) "\n")))
1175 (defun org-list-parse-list (&optional delete)
1176 "Parse the list at point.
1177 Return a list containing first level items as strings and
1178 sublevels as a list of strings."
1179 (let ((start (org-list-item-begin))
1180 (end (org-list-end))
1181 output itemsep)
1182 (while (re-search-forward org-export-latex-list-beginning-re end t)
1183 (setq itemsep (if (save-match-data
1184 (string-match "^[0-9]" (match-string 2)))
1185 "[0-9]+\\(?:\\.\\|)\\)" "[-+]"))
1186 (let* ((indent1 (match-string 1))
1187 (nextitem (save-excursion
1188 (save-match-data
1189 (or (and (re-search-forward
1190 (concat "^" indent1 itemsep " *?") end t)
1191 (match-beginning 0)) end))))
1192 (item (buffer-substring
1193 (point)
1194 (or (and (re-search-forward
1195 org-export-latex-list-beginning-re end t)
1196 (goto-char (match-beginning 0)))
1197 (goto-char end))))
1198 (nextindent (match-string 1))
1199 (item (org-trim item))
1200 (item (if (string-match "^\\[.+\\]" item)
1201 (replace-match "\\\\texttt{\\&}"
1202 t nil item) item)))
1203 (push item output)
1204 (when (> (length nextindent)
1205 (length indent1))
1206 (narrow-to-region (point) nextitem)
1207 (push (org-list-parse-list) output)
1208 (widen))))
1209 (when delete (delete-region start end))
1210 (setq output (nreverse output))
1211 (push (if (string-match "^\\[0" itemsep)
1212 'ordered 'unordered) output)))
1214 (defun org-list-item-begin ()
1215 "Find the beginning of the list item and return its position."
1216 (save-excursion
1217 (if (not (or (looking-at org-export-latex-list-beginning-re)
1218 (re-search-backward
1219 org-export-latex-list-beginning-re nil t)))
1220 (progn (goto-char (point-min)) (point))
1221 (match-beginning 0))))
1223 (defun org-list-end ()
1224 "Find the end of the list and return its position."
1225 (save-excursion
1226 (catch 'exit
1227 (while (or (looking-at org-export-latex-list-beginning-re)
1228 (looking-at "^[ \t]+\\|^$"))
1229 (if (eq (point) (point-max))
1230 (throw 'exit (point-max)))
1231 (forward-line 1))) (point)))
1233 (defun org-list-insert-radio-list ()
1234 "Insert a radio list template appropriate for this major mode."
1235 (interactive)
1236 (let* ((e (assq major-mode org-list-radio-list-templates))
1237 (txt (nth 1 e))
1238 name pos)
1239 (unless e (error "No radio list setup defined for %s" major-mode))
1240 (setq name (read-string "List name: "))
1241 (while (string-match "%n" txt)
1242 (setq txt (replace-match name t t txt)))
1243 (or (bolp) (insert "\n"))
1244 (setq pos (point))
1245 (insert txt)
1246 (goto-char pos)))
1248 (defun org-list-send-list (&optional maybe)
1249 "Send a tranformed version of this list to the receiver position.
1250 With argument MAYBE, fail quietly if no transformation is defined for
1251 this list."
1252 (interactive)
1253 (catch 'exit
1254 (unless (org-at-item-p) (error "Not at a list"))
1255 (save-excursion
1256 (goto-char (org-list-item-begin))
1257 (beginning-of-line 0)
1258 (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
1259 (if maybe
1260 (throw 'exit nil)
1261 (error "Don't know how to transform this list"))))
1262 (let* ((name (match-string 1))
1264 (transform (intern (match-string 2)))
1265 (txt (buffer-substring-no-properties
1266 (org-list-item-begin)
1267 (org-list-end)))
1268 (list (org-list-parse-list)))
1269 (unless (fboundp transform)
1270 (error "No such transformation function %s" transform))
1271 (setq txt (funcall transform list))
1272 ;; Find the insertion place
1273 (save-excursion
1274 (goto-char (point-min))
1275 (unless (re-search-forward
1276 (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
1277 (error "Don't know where to insert translated list"))
1278 (goto-char (match-beginning 0))
1279 (beginning-of-line 2)
1280 (setq beg (point))
1281 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
1282 (error "Cannot find end of insertion region"))
1283 (beginning-of-line 1)
1284 (delete-region beg (point))
1285 (goto-char beg)
1286 (insert txt "\n"))
1287 (message "List converted and installed at receiver location"))))
1289 (defun org-list-to-generic (list params)
1290 "Convert a LIST parsed through `org-list-parse-list' to other formats.
1292 Valid parameters are
1294 :ustart String to start an unordered list
1295 :uend String to end an unordered list
1297 :ostart String to start an ordered list
1298 :oend String to end an ordered list
1300 :splice When set to t, return only list body lines, don't wrap
1301 them into :[u/o]start and :[u/o]end. Default is nil.
1303 :istart String to start a list item
1304 :iend String to end a list item
1305 :isep String to separate items
1306 :lsep String to separate sublists"
1307 (interactive)
1308 (let* ((p params) sublist
1309 (splicep (plist-get p :splice))
1310 (ostart (plist-get p :ostart))
1311 (oend (plist-get p :oend))
1312 (ustart (plist-get p :ustart))
1313 (uend (plist-get p :uend))
1314 (istart (plist-get p :istart))
1315 (iend (plist-get p :iend))
1316 (isep (plist-get p :isep))
1317 (lsep (plist-get p :lsep)))
1318 (let ((wrapper
1319 (cond ((eq (car list) 'ordered)
1320 (concat ostart "\n%s" oend "\n"))
1321 ((eq (car list) 'unordered)
1322 (concat ustart "\n%s" uend "\n"))))
1323 rtn)
1324 (while (setq sublist (pop list))
1325 (cond ((symbolp sublist) nil)
1326 ((stringp sublist)
1327 (setq rtn (concat rtn istart sublist iend isep)))
1329 (setq rtn (concat rtn ;; previous list
1330 lsep ;; list separator
1331 (org-list-to-generic sublist p)
1332 lsep ;; list separator
1333 )))))
1334 (format wrapper rtn))))
1336 (defun org-list-to-latex (list)
1337 "Convert LIST into a LaTeX list."
1338 (org-list-to-generic
1339 list '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
1340 :ustart "\\begin{itemize}" :uend "\\end{itemize}"
1341 :istart "\\item " :iend ""
1342 :isep "\n" :lsep "\n")))
1344 (defun org-list-to-html (list)
1345 "Convert LIST into a HTML list."
1346 (org-list-to-generic
1347 list '(:splicep nil :ostart "<ol>" :oend "</ol>"
1348 :ustart "<ul>" :uend "</ul>"
1349 :istart "<li>" :iend "</li>"
1350 :isep "\n" :lsep "\n")))
1352 (defun org-list-to-texinfo (list)
1353 "Convert LIST into a Texinfo list."
1354 (org-list-to-generic
1355 list '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
1356 :ustart "@enumerate" :uend "@end enumerate"
1357 :istart "@item\n" :iend ""
1358 :isep "\n" :lsep "\n")))
1360 (defconst org-latex-entities
1361 '("\\!"
1362 "\\'"
1363 "\\+"
1364 "\\,"
1365 "\\-"
1366 "\\:"
1367 "\\;"
1368 "\\<"
1369 "\\="
1370 "\\>"
1371 "\\Huge"
1372 "\\LARGE"
1373 "\\Large"
1374 "\\Styles"
1375 "\\\\"
1376 "\\`"
1377 "\\addcontentsline"
1378 "\\address"
1379 "\\addtocontents"
1380 "\\addtocounter"
1381 "\\addtolength"
1382 "\\addvspace"
1383 "\\alph"
1384 "\\appendix"
1385 "\\arabic"
1386 "\\author"
1387 "\\begin{array}"
1388 "\\begin{center}"
1389 "\\begin{description}"
1390 "\\begin{enumerate}"
1391 "\\begin{eqnarray}"
1392 "\\begin{equation}"
1393 "\\begin{figure}"
1394 "\\begin{flushleft}"
1395 "\\begin{flushright}"
1396 "\\begin{itemize}"
1397 "\\begin{list}"
1398 "\\begin{minipage}"
1399 "\\begin{picture}"
1400 "\\begin{quotation}"
1401 "\\begin{quote}"
1402 "\\begin{tabbing}"
1403 "\\begin{table}"
1404 "\\begin{tabular}"
1405 "\\begin{thebibliography}"
1406 "\\begin{theorem}"
1407 "\\begin{titlepage}"
1408 "\\begin{verbatim}"
1409 "\\begin{verse}"
1410 "\\bf"
1411 "\\bf"
1412 "\\bibitem"
1413 "\\bigskip"
1414 "\\cdots"
1415 "\\centering"
1416 "\\circle"
1417 "\\cite"
1418 "\\cleardoublepage"
1419 "\\clearpage"
1420 "\\cline"
1421 "\\closing"
1422 "\\dashbox"
1423 "\\date"
1424 "\\ddots"
1425 "\\dotfill"
1426 "\\em"
1427 "\\fbox"
1428 "\\flushbottom"
1429 "\\fnsymbol"
1430 "\\footnote"
1431 "\\footnotemark"
1432 "\\footnotesize"
1433 "\\footnotetext"
1434 "\\frac"
1435 "\\frame"
1436 "\\framebox"
1437 "\\hfill"
1438 "\\hline"
1439 "\\hrulespace"
1440 "\\hspace"
1441 "\\huge"
1442 "\\hyphenation"
1443 "\\include"
1444 "\\includeonly"
1445 "\\indent"
1446 "\\input"
1447 "\\it"
1448 "\\kill"
1449 "\\label"
1450 "\\large"
1451 "\\ldots"
1452 "\\line"
1453 "\\linebreak"
1454 "\\linethickness"
1455 "\\listoffigures"
1456 "\\listoftables"
1457 "\\location"
1458 "\\makebox"
1459 "\\maketitle"
1460 "\\mark"
1461 "\\mbox"
1462 "\\medskip"
1463 "\\multicolumn"
1464 "\\multiput"
1465 "\\newcommand"
1466 "\\newcounter"
1467 "\\newenvironment"
1468 "\\newfont"
1469 "\\newlength"
1470 "\\newline"
1471 "\\newpage"
1472 "\\newsavebox"
1473 "\\newtheorem"
1474 "\\nocite"
1475 "\\nofiles"
1476 "\\noindent"
1477 "\\nolinebreak"
1478 "\\nopagebreak"
1479 "\\normalsize"
1480 "\\onecolumn"
1481 "\\opening"
1482 "\\oval"
1483 "\\overbrace"
1484 "\\overline"
1485 "\\pagebreak"
1486 "\\pagenumbering"
1487 "\\pageref"
1488 "\\pagestyle"
1489 "\\par"
1490 "\\parbox"
1491 "\\put"
1492 "\\raggedbottom"
1493 "\\raggedleft"
1494 "\\raggedright"
1495 "\\raisebox"
1496 "\\ref"
1497 "\\rm"
1498 "\\roman"
1499 "\\rule"
1500 "\\savebox"
1501 "\\sc"
1502 "\\scriptsize"
1503 "\\setcounter"
1504 "\\setlength"
1505 "\\settowidth"
1506 "\\sf"
1507 "\\shortstack"
1508 "\\signature"
1509 "\\sl"
1510 "\\small"
1511 "\\smallskip"
1512 "\\sqrt"
1513 "\\tableofcontents"
1514 "\\telephone"
1515 "\\thanks"
1516 "\\thispagestyle"
1517 "\\tiny"
1518 "\\title"
1519 "\\tt"
1520 "\\twocolumn"
1521 "\\typein"
1522 "\\typeout"
1523 "\\underbrace"
1524 "\\underline"
1525 "\\usebox"
1526 "\\usecounter"
1527 "\\value"
1528 "\\vdots"
1529 "\\vector"
1530 "\\verb"
1531 "\\vfill"
1532 "\\vline"
1533 "\\vspace")
1534 "A list of LaTeX commands to be protected when performing conversion.")
1536 (provide 'org-export-latex)
1538 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
1539 ;;; org-export-latex.el ends here