Implement #+LATEX_HEADER special.
[org-mode.git] / lisp / org-export-latex.el
blobadf0f2908fb2b08359d568aa96174007a34edefd
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: 6.08-pre01
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
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;;; Commentary:
31 ;; This library implements a LaTeX exporter for org-mode.
33 ;; Put this file into your load-path and the following into your ~/.emacs:
34 ;; (require 'org-export-latex)
36 ;; The interactive functions are similar to those of the HTML exporter:
38 ;; M-x `org-export-as-latex'
39 ;; M-x `org-export-as-latex-batch'
40 ;; M-x `org-export-as-latex-to-buffer'
41 ;; M-x `org-export-region-as-latex'
42 ;; M-x `org-replace-region-by-latex'
44 ;;; Code:
46 (eval-when-compile
47 (require 'cl))
49 (require 'footnote)
50 (require 'org)
51 (require 'org-exp)
53 ;;; Variables:
54 (defvar org-export-latex-class nil)
55 (defvar org-export-latex-header nil)
56 (defvar org-export-latex-append-header nil)
57 (defvar org-export-latex-options-plist nil)
58 (defvar org-export-latex-todo-keywords-1 nil)
59 (defvar org-export-latex-all-targets-re nil)
60 (defvar org-export-latex-add-level 0)
61 (defvar org-export-latex-sectioning "")
62 (defvar org-export-latex-sectioning-depth 0)
63 (defvar org-export-latex-special-string-regexps
64 '(org-ts-regexp
65 org-scheduled-string
66 org-deadline-string
67 org-clock-string)
68 "A list of regexps to convert as special keywords.")
70 (defvar latexp) ; dynamically scoped from org.el
71 (defvar re-quote) ; dynamically scoped from org.el
72 (defvar commentsp) ; dynamically scoped from org.el
74 ;;; User variables:
76 (defgroup org-export-latex nil
77 "Options for exporting Org-mode files to LaTeX."
78 :tag "Org Export LaTeX"
79 :group 'org-export)
81 (defcustom org-export-latex-default-class "article"
82 "The default LaTeX class."
83 :group 'org-export-latex
84 :type '(string :tag "LaTeX class"))
86 (defcustom org-export-latex-classes
87 '(("article"
88 "\\documentclass[11pt,a4paper]{article}
89 \\usepackage[utf8]{inputenc}
90 \\usepackage[T1]{fontenc}
91 \\usepackage{hyperref}"
92 ("\\section{%s}" . "\\section*{%s}")
93 ("\\subsection{%s}" . "\\subsection*{%s}")
94 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
95 ("\\paragraph{%s}" . "\\paragraph*{%s}")
96 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
97 ("report"
98 "\\documentclass[11pt,a4paper]{report}
99 \\usepackage[utf8]{inputenc}
100 \\usepackage[T1]{fontenc}
101 \\usepackage{hyperref}"
102 ("\\part{%s}" . "\\part*{%s}")
103 ("\\chapter{%s}" . "\\chapter*{%s}")
104 ("\\section{%s}" . "\\section*{%s}")
105 ("\\subsection{%s}" . "\\subsection*{%s}")
106 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
107 ("book"
108 "\\documentclass[11pt,a4paper]{book}
109 \\usepackage[utf8]{inputenc}
110 \\usepackage[T1]{fontenc}
111 \\usepackage{hyperref}"
112 ("\\part{%s}" . "\\part*{%s}")
113 ("\\chapter{%s}" . "\\chapter*{%s}")
114 ("\\section{%s}" . "\\section*{%s}")
115 ("\\subsection{%s}" . "\\subsection*{%s}")
116 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
117 "Alist of LaTeX classes and associated header and structure.
118 If #+LaTeX_CLASS is set in the buffer, use its value and the
119 associated information. Here is the structure of each cell:
121 \(class-name
122 header-string
123 (unnumbered-section numbered-section\)
124 ...\)
126 A %s formatter is mandatory in each section string and will be
127 replaced by the title of the section."
128 :group 'org-export-latex
129 :type '(repeat
130 (list (string :tag "LaTeX class")
131 (string :tag "LaTeX header")
132 (cons :tag "Level 1"
133 (string :tag "Numbered")
134 (string :tag "Unnumbered"))
135 (cons :tag "Level 2"
136 (string :tag "Numbered")
137 (string :tag "Unnumbered"))
138 (cons :tag "Level 3"
139 (string :tag "Numbered")
140 (string :tag "Unnumbered"))
141 (cons :tag "Level 4"
142 (string :tag "Numbered")
143 (string :tag "Unnumbered"))
144 (cons :tag "Level 5"
145 (string :tag "Numbered")
146 (string :tag "Unnumbered")))))
148 (defcustom org-export-latex-emphasis-alist
149 '(("*" "\\textbf{%s}" nil)
150 ("/" "\\emph{%s}" nil)
151 ("_" "\\underline{%s}" nil)
152 ("+" "\\texttt{%s}" nil)
153 ("=" "\\texttt{%s}" nil)
154 ("~" "\\texttt{%s}" t))
155 "Alist of LaTeX expressions to convert emphasis fontifiers.
156 Each element of the list is a list of three elements.
157 The first element is the character used as a marker for fontification.
158 The second element is a formatting string to wrap fontified text with.
159 The third element decides whether to protect converted text from other
160 conversions."
161 :group 'org-export-latex
162 :type 'alist)
164 (defcustom org-export-latex-title-command "\\maketitle"
165 "The command used to insert the title just after \\begin{document}.
166 If this string contains the formatting specification \"%s\" then
167 it will be used as a formatting string, passing the title as an
168 argument."
169 :group 'org-export-latex
170 :type 'string)
172 (defcustom org-export-latex-date-format
173 "%d %B %Y"
174 "Format string for \\date{...}."
175 :group 'org-export-latex
176 :type 'string)
178 (defcustom org-export-latex-tables-verbatim nil
179 "When non-nil, tables are exported verbatim."
180 :group 'org-export-latex
181 :type 'boolean)
183 (defcustom org-export-latex-tables-column-borders nil
184 "When non-nil, group of columns are surrounded with borders."
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 This is specified by the `org-export-headline-levels' option or the
197 value of \"H:\" 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 ;;; Autoload functions:
229 ;;;###autoload
230 (defun org-export-as-latex-batch ()
231 "Call `org-export-as-latex', may be used in batch processing.
232 For example:
234 emacs --batch
235 --load=$HOME/lib/emacs/org.el
236 --eval \"(setq org-export-headline-levels 2)\"
237 --visit=MyFile --funcall org-export-as-latex-batch"
238 (org-export-as-latex org-export-headline-levels 'hidden))
240 ;;;###autoload
241 (defun org-export-as-latex-to-buffer (arg)
242 "Call `org-exort-as-latex` with output to a temporary buffer.
243 No file is created. The prefix ARG is passed through to `org-export-as-latex'."
244 (interactive "P")
245 (org-export-as-latex arg nil nil "*Org LaTeX Export*")
246 (switch-to-buffer-other-window "*Org LaTeX Export*"))
248 ;;;###autoload
249 (defun org-replace-region-by-latex (beg end)
250 "Replace the region from BEG to END with its LaTeX export.
251 It assumes the region has `org-mode' syntax, and then convert it to
252 LaTeX. This can be used in any buffer. For example, you could
253 write an itemized list in `org-mode' syntax in an LaTeX buffer and
254 then use this command to convert it."
255 (interactive "r")
256 (let (reg latex buf)
257 (save-window-excursion
258 (if (org-mode-p)
259 (setq latex (org-export-region-as-latex
260 beg end t 'string))
261 (setq reg (buffer-substring beg end)
262 buf (get-buffer-create "*Org tmp*"))
263 (save-excursion
264 (set-buffer buf)
265 (erase-buffer)
266 (insert reg)
267 (org-mode)
268 (setq latex (org-export-region-as-latex
269 (point-min) (point-max) t 'string)))
270 (kill-buffer buf)))
271 (delete-region beg end)
272 (insert latex)))
274 ;;;###autoload
275 (defun org-export-region-as-latex (beg end &optional body-only buffer)
276 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
277 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
278 contents, and only produce the region of converted text, useful for
279 cut-and-paste operations.
280 If BUFFER is a buffer or a string, use/create that buffer as a target
281 of the converted LaTeX. If BUFFER is the symbol `string', return the
282 produced LaTeX as a string and leave not buffer behind. For example,
283 a Lisp program could call this function in the following way:
285 (setq latex (org-export-region-as-latex beg end t 'string))
287 When called interactively, the output buffer is selected, and shown
288 in a window. A non-interactive call will only retunr the buffer."
289 (interactive "r\nP")
290 (when (interactive-p)
291 (setq buffer "*Org LaTeX Export*"))
292 (let ((transient-mark-mode t) (zmacs-regions t)
293 rtn)
294 (goto-char end)
295 (set-mark (point)) ;; to activate the region
296 (goto-char beg)
297 (setq rtn (org-export-as-latex
298 nil nil nil
299 buffer body-only))
300 (if (fboundp 'deactivate-mark) (deactivate-mark))
301 (if (and (interactive-p) (bufferp rtn))
302 (switch-to-buffer-other-window rtn)
303 rtn)))
305 ;;;###autoload
306 (defun org-export-as-latex (arg &optional hidden ext-plist
307 to-buffer body-only pub-dir)
308 "Export current buffer to a LaTeX file.
309 If there is an active region, export only the region. The prefix
310 ARG specifies how many levels of the outline should become
311 headlines. The default is 3. Lower levels will be exported
312 depending on `org-export-latex-low-levels'. The default is to
313 convert them as description lists. When HIDDEN is non-nil, don't
314 display the LaTeX buffer. EXT-PLIST is a property list with
315 external parameters overriding org-mode's default settings, but
316 still inferior to file-local settings. When TO-BUFFER is
317 non-nil, create a buffer with that name and export to that
318 buffer. If TO-BUFFER is the symbol `string', don't leave any
319 buffer behind but just return the resulting LaTeX as a string.
320 When BODY-ONLY is set, don't produce the file header and footer,
321 simply return the content of \begin{document}...\end{document},
322 without even the \begin{document} and \end{document} commands.
323 when PUB-DIR is set, use this as the publishing directory."
324 (interactive "P")
325 ;; Make sure we have a file name when we need it.
326 (when (and (not (or to-buffer body-only))
327 (not buffer-file-name))
328 (if (buffer-base-buffer)
329 (org-set-local 'buffer-file-name
330 (with-current-buffer (buffer-base-buffer)
331 buffer-file-name))
332 (error "Need a file name to be able to export")))
334 (message "Exporting to LaTeX...")
335 (org-update-radio-target-regexp)
336 (org-export-latex-set-initial-vars ext-plist arg)
337 (let* ((wcf (current-window-configuration))
338 (opt-plist org-export-latex-options-plist)
339 (region-p (org-region-active-p))
340 (rbeg (and region-p (region-beginning)))
341 (rend (and region-p (region-end)))
342 (subtree-p
343 (when region-p
344 (save-excursion
345 (goto-char rbeg)
346 (and (org-at-heading-p)
347 (>= (org-end-of-subtree t t) rend)))))
348 (opt-plist (if subtree-p
349 (org-export-add-subtree-options opt-plist rbeg)
350 opt-plist))
351 ;; Make sure the variable contains the updated values.
352 (org-export-latex-options-plist opt-plist)
353 (title (or (and subtree-p (org-export-get-title-from-subtree))
354 (plist-get opt-plist :title)
355 (and (not
356 (plist-get opt-plist :skip-before-1st-heading))
357 (org-export-grab-title-from-buffer))
358 (file-name-sans-extension
359 (file-name-nondirectory buffer-file-name))))
360 (filename (concat (file-name-as-directory
361 (or pub-dir
362 (org-export-directory :LaTeX ext-plist)))
363 (file-name-sans-extension
364 (or (and subtree-p
365 (org-entry-get rbeg "EXPORT_FILE_NAME" t))
366 (file-name-nondirectory ;sans-extension
367 buffer-file-name)))
368 ".tex"))
369 (filename (if (equal (file-truename filename)
370 (file-truename buffer-file-name))
371 (concat filename ".tex")
372 filename))
373 (buffer (if to-buffer
374 (cond
375 ((eq to-buffer 'string) (get-buffer-create
376 "*Org LaTeX Export*"))
377 (t (get-buffer-create to-buffer)))
378 (find-file-noselect filename)))
379 (odd org-odd-levels-only)
380 (header (org-export-latex-make-header title opt-plist))
381 (skip (cond (subtree-p nil)
382 (region-p t)
383 ;; never skip first lines when exporting a subtree
384 (t (plist-get opt-plist :skip-before-1st-heading))))
385 (text (plist-get opt-plist :text))
386 (first-lines (if skip "" (org-export-latex-first-lines)))
387 (coding-system (and (boundp 'buffer-file-coding-system)
388 buffer-file-coding-system))
389 (coding-system-for-write (or org-export-latex-coding-system
390 coding-system))
391 (save-buffer-coding-system (or org-export-latex-coding-system
392 coding-system))
393 (region (buffer-substring
394 (if region-p (region-beginning) (point-min))
395 (if region-p (region-end) (point-max))))
396 (string-for-export
397 (org-export-preprocess-string
398 region :emph-multiline t
399 :for-LaTeX t
400 :comments nil
401 :add-text (if (eq to-buffer 'string) nil text)
402 :skip-before-1st-heading skip
403 :select-tags (plist-get opt-plist :select-tags)
404 :exclude-tags (plist-get opt-plist :exclude-tags)
405 :LaTeX-fragments nil)))
407 (set-buffer buffer)
408 (erase-buffer)
410 (and (fboundp 'set-buffer-file-coding-system)
411 (set-buffer-file-coding-system coding-system-for-write))
413 ;; insert the header and initial document commands
414 (unless (or (eq to-buffer 'string) body-only)
415 (insert header))
417 ;; insert text found in #+TEXT
418 (when (and text (not (eq to-buffer 'string)))
419 (insert (org-export-latex-content
420 text '(lists tables fixed-width keywords))
421 "\n\n"))
423 ;; insert lines before the first headline
424 (unless (or skip (eq to-buffer 'string))
425 (insert first-lines))
427 ;; handle the case where the region does not begin with a section
428 (when region-p
429 (insert (with-temp-buffer
430 (insert string-for-export)
431 (org-export-latex-first-lines))))
433 ;; export the content of headlines
434 (org-export-latex-global
435 (with-temp-buffer
436 (insert string-for-export)
437 (goto-char (point-min))
438 (when (re-search-forward "^\\(\\*+\\) " nil t)
439 (let* ((asters (length (match-string 1)))
440 (level (if odd (- asters 2) (- asters 1))))
441 (setq org-export-latex-add-level
442 (if odd (1- (/ (1+ asters) 2)) (1- asters)))
443 (org-export-latex-parse-global level odd)))))
445 ;; finalization
446 (unless body-only (insert "\n\\end{document}"))
447 (or to-buffer (save-buffer))
448 (goto-char (point-min))
449 (message "Exporting to LaTeX...done")
450 (prog1
451 (if (eq to-buffer 'string)
452 (prog1 (buffer-substring (point-min) (point-max))
453 (kill-buffer (current-buffer)))
454 (current-buffer))
455 (set-window-configuration wcf))))
457 ;;; Parsing functions:
459 (defun org-export-latex-parse-global (level odd)
460 "Parse the current buffer recursively, starting at LEVEL.
461 If ODD is non-nil, assume the buffer only contains odd sections.
462 Return a list reflecting the document structure."
463 (save-excursion
464 (goto-char (point-min))
465 (let* ((cnt 0) output
466 (depth org-export-latex-sectioning-depth))
467 (while (re-search-forward
468 (concat "^\\(\\(?:\\*\\)\\{"
469 (number-to-string (+ (if odd 2 1) level))
470 "\\}\\) \\(.*\\)$")
471 ;; make sure that there is no upper heading
472 (when (> level 0)
473 (save-excursion
474 (save-match-data
475 (re-search-forward
476 (concat "^\\(\\(?:\\*\\)\\{"
477 (number-to-string level)
478 "\\}\\) \\(.*\\)$") nil t)))) t)
479 (setq cnt (1+ cnt))
480 (let* ((pos (match-beginning 0))
481 (heading (match-string 2))
482 (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
483 (save-excursion
484 (narrow-to-region
485 (point)
486 (save-match-data
487 (if (re-search-forward
488 (concat "^\\(\\(?:\\*\\)\\{"
489 (number-to-string (+ (if odd 2 1) level))
490 "\\}\\) \\(.*\\)$") nil t)
491 (match-beginning 0)
492 (point-max))))
493 (goto-char (point-min))
494 (setq output
495 (append output
496 (list
497 (list
498 `(pos . ,pos)
499 `(level . ,nlevel)
500 `(occur . ,cnt)
501 `(heading . ,heading)
502 `(content . ,(org-export-latex-parse-content))
503 `(subcontent . ,(org-export-latex-parse-subcontent
504 level odd)))))))
505 (widen)))
506 (list output))))
508 (defun org-export-latex-parse-content ()
509 "Extract the content of a section."
510 (let ((beg (point))
511 (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t)
512 (progn (beginning-of-line) (point))
513 (point-max))))
514 (buffer-substring beg end)))
516 (defun org-export-latex-parse-subcontent (level odd)
517 "Extract the subcontent of a section at LEVEL.
518 If ODD Is non-nil, assume subcontent only contains odd sections."
519 (if (not (re-search-forward
520 (concat "^\\(\\(?:\\*\\)\\{"
521 (number-to-string (+ (if odd 4 2) level))
522 "\\}\\) \\(.*\\)$")
523 nil t))
524 nil ; subcontent is nil
525 (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
527 ;;; Rendering functions:
528 (defun org-export-latex-global (content)
529 "Export CONTENT to LaTeX.
530 CONTENT is an element of the list produced by
531 `org-export-latex-parse-global'."
532 (if (eq (car content) 'subcontent)
533 (mapc 'org-export-latex-sub (cdr content))
534 (org-export-latex-sub (car content))))
536 (defun org-export-latex-sub (subcontent)
537 "Export the list SUBCONTENT to LaTeX.
538 SUBCONTENT is an alist containing information about the headline
539 and its content."
540 (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
541 (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
543 (defun org-export-latex-subcontent (subcontent num)
544 "Export each cell of SUBCONTENT to LaTeX.
545 If NUM, export sections as numerical sections."
546 (let ((heading (org-export-latex-fontify-headline
547 (cdr (assoc 'heading subcontent))))
548 (level (- (cdr (assoc 'level subcontent))
549 org-export-latex-add-level))
550 (occur (number-to-string (cdr (assoc 'occur subcontent))))
551 (content (cdr (assoc 'content subcontent)))
552 (subcontent (cadr (assoc 'subcontent subcontent))))
553 (cond
554 ;; Normal conversion
555 ((<= level org-export-latex-sectioning-depth)
556 (let ((sec (nth (1- level) org-export-latex-sectioning)))
557 (insert (format (if num (car sec) (cdr sec)) heading) "\n"))
558 (insert (org-export-latex-content content))
559 (cond ((stringp subcontent) (insert subcontent))
560 ((listp subcontent) (org-export-latex-sub subcontent))))
561 ;; At a level under the hl option: we can drop this subsection
562 ((> level org-export-latex-sectioning-depth)
563 (cond ((eq org-export-latex-low-levels 'description)
564 (insert (format "\\begin{description}\n\n\\item[%s]\n\n" heading))
565 (insert (org-export-latex-content content))
566 (cond ((stringp subcontent) (insert subcontent))
567 ((listp subcontent) (org-export-latex-sub subcontent)))
568 (insert "\\end{description}\n"))
569 ((stringp org-export-latex-low-levels)
570 (insert (format org-export-latex-low-levels heading) "\n")
571 (insert (org-export-latex-content content))
572 (cond ((stringp subcontent) (insert subcontent))
573 ((listp subcontent) (org-export-latex-sub subcontent)))))))))
575 ;;; Exporting internals:
576 (defun org-export-latex-set-initial-vars (ext-plist level)
577 "Store org local variables required for LaTeX export.
578 EXT-PLIST is an optional additional plist.
579 LEVEL indicates the default depth for export."
580 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
581 org-export-latex-all-targets-re
582 (org-make-target-link-regexp (org-all-targets))
583 org-export-latex-options-plist
584 (org-combine-plists (org-default-export-plist) ext-plist
585 (org-infile-export-plist))
586 org-export-latex-class
587 (save-excursion
588 (goto-char (point-min))
589 (if (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
590 (assoc (match-string 1) org-export-latex-classes))
591 (match-string 1)
592 org-export-latex-default-class))
593 org-export-latex-header
594 (cadr (assoc org-export-latex-class org-export-latex-classes))
595 org-export-latex-sectioning
596 (cddr (assoc org-export-latex-class org-export-latex-classes))
597 org-export-latex-sectioning-depth
598 (or level
599 (let ((hl-levels
600 (plist-get org-export-latex-options-plist :headline-levels))
601 (sec-depth (length org-export-latex-sectioning)))
602 (if (> hl-levels sec-depth) sec-depth hl-levels)))))
604 (defun org-export-latex-make-header (title opt-plist)
605 "Make the LaTeX header and return it as a string.
606 TITLE is the current title from the buffer or region.
607 OPT-PLIST is the options plist for current buffer."
608 (let ((toc (plist-get opt-plist :table-of-contents))
609 (author (plist-get opt-plist :author)))
610 (concat
611 (if (plist-get opt-plist :time-stamp-file)
612 (format-time-string "% Created %Y-%m-%d %a %H:%M\n"))
613 ;; insert LaTeX custom header
614 org-export-latex-header
615 "\n"
616 ;; insert information on LaTeX packages
617 (when org-export-latex-packages-alist
618 (mapconcat (lambda(p)
619 (if (equal "" (car p))
620 (format "\\usepackage{%s}" (cadr p))
621 (format "\\usepackage[%s]{%s}"
622 (car p) (cadr p))))
623 org-export-latex-packages-alist "\n"))
624 ;; insert additional commands in the header
625 (plist-get opt-plist :latex-header-extra)
626 org-export-latex-append-header
627 ;; insert the title
628 (format
629 "\n\n\\title{%s}\n"
630 ;; convert the title
631 (org-export-latex-content
632 title '(lists tables fixed-width keywords)))
633 ;; insert author info
634 (if (plist-get opt-plist :author-info)
635 (format "\\author{%s}\n"
636 (or author user-full-name))
637 (format "%%\\author{%s}\n"
638 (or author user-full-name)))
639 ;; insert the date
640 (format "\\date{%s}\n"
641 (format-time-string
642 (or (plist-get opt-plist :date)
643 org-export-latex-date-format)))
644 ;; beginning of the document
645 "\n\\begin{document}\n\n"
646 ;; insert the title command
647 (if (string-match "%s" org-export-latex-title-command)
648 (format org-export-latex-title-command title)
649 org-export-latex-title-command)
650 "\n\n"
651 ;; table of contents
652 (when (and org-export-with-toc
653 (plist-get opt-plist :section-numbers))
654 (cond ((numberp toc)
655 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
656 (min toc (plist-get opt-plist :headline-levels))))
657 (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
658 (plist-get opt-plist :headline-levels))))))))
660 (defun org-export-latex-first-lines (&optional comments)
661 "Export the first lines before first headline.
662 COMMENTS is either nil to replace them with the empty string or a
663 formatting string like %%%%s if we want to comment them out."
664 (save-excursion
665 (goto-char (point-min))
666 (if (org-at-heading-p) (beginning-of-line 2))
667 (let* ((pt (point))
668 (end (if (and (re-search-forward "^\\* " nil t)
669 (not (eq pt (match-beginning 0))))
670 (goto-char (match-beginning 0))
671 (goto-char (point-max)))))
672 (org-export-latex-content
673 (org-export-preprocess-string
674 (buffer-substring (point-min) end)
675 :for-LaTeX t
676 :emph-multiline t
677 :add-text nil
678 :comments nil
679 :skip-before-1st-heading nil
680 :LaTeX-fragments nil)))))
682 (defun org-export-latex-content (content &optional exclude-list)
683 "Convert CONTENT string to LaTeX.
684 Don't perform conversions that are in EXCLUDE-LIST. Recognized
685 conversion types are: quotation-marks, emphasis, sub-superscript,
686 links, keywords, lists, tables, fixed-width"
687 (with-temp-buffer
688 (insert content)
689 (unless (memq 'quotation-marks exclude-list)
690 (org-export-latex-quotation-marks))
691 (unless (memq 'emphasis exclude-list)
692 (when (plist-get org-export-latex-options-plist :emphasize)
693 (org-export-latex-fontify)))
694 (unless (memq 'sub-superscript exclude-list)
695 (org-export-latex-special-chars
696 (plist-get org-export-latex-options-plist :sub-superscript)))
697 (unless (memq 'links exclude-list)
698 (org-export-latex-links))
699 (unless (memq 'keywords exclude-list)
700 (org-export-latex-keywords
701 (plist-get org-export-latex-options-plist :timestamps)))
702 (unless (memq 'lists exclude-list)
703 (org-export-latex-lists))
704 (unless (memq 'tables exclude-list)
705 (org-export-latex-tables
706 (plist-get org-export-latex-options-plist :tables)))
707 (unless (memq 'fixed-width exclude-list)
708 (org-export-latex-fixed-width
709 (plist-get org-export-latex-options-plist :fixed-width)))
710 ;; return string
711 (buffer-substring (point-min) (point-max))))
713 (defun org-export-latex-protect-string (s)
714 "Add the org-protected property to string S."
715 (add-text-properties 0 (length s) '(org-protected t) s) s)
717 (defun org-export-latex-protect-char-in-string (char-list string)
718 "Add org-protected text-property to char from CHAR-LIST in STRING."
719 (with-temp-buffer
720 (save-match-data
721 (insert string)
722 (goto-char (point-min))
723 (while (re-search-forward (regexp-opt char-list) nil t)
724 (add-text-properties (match-beginning 0)
725 (match-end 0) '(org-protected t)))
726 (buffer-string))))
728 (defun org-export-latex-keywords-maybe (remove-list)
729 "Maybe remove keywords depending on rules in REMOVE-LIST."
730 (goto-char (point-min))
731 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
732 (case-fold-search nil))
733 ;; convert TODO keywords
734 (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
735 (if (plist-get remove-list :todo)
736 (replace-match "")
737 (replace-match (format "\\texttt{%s}" (match-string 1)) t t)))
738 ;; convert priority string
739 (when (re-search-forward "\\[\\\\#.\\]" nil t)
740 (if (plist-get remove-list :priority)
741 (replace-match "")
742 (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))
743 ;; convert tags
744 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t)
745 (if (or (not org-export-with-tags)
746 (plist-get remove-list :tags))
747 (replace-match "")
748 (replace-match
749 (org-export-latex-protect-string
750 (format "\\texttt{%s}"
751 (save-match-data
752 (replace-regexp-in-string
753 "_" "\\\\_" (match-string 0)))))
754 t t)))))
756 (defun org-export-latex-fontify-headline (string)
757 "Fontify special words in STRING."
758 (with-temp-buffer
759 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
760 ;; the beginning of the buffer - inserting "\n" is safe here though.
761 (insert "\n" string)
762 (goto-char (point-min))
763 (when (plist-get org-export-latex-options-plist :emphasize)
764 (org-export-latex-fontify))
765 (org-export-latex-keywords-maybe
766 org-export-latex-remove-from-headlines)
767 (org-export-latex-special-chars
768 (plist-get org-export-latex-options-plist :sub-superscript))
769 (org-export-latex-links)
770 (org-trim (buffer-substring-no-properties (point-min) (point-max)))))
772 (defun org-export-latex-quotation-marks ()
773 "Export quotation marks depending on language conventions."
774 (let* ((lang (plist-get org-export-latex-options-plist :language))
775 (quote-rpl (if (equal lang "fr")
776 '(("\\(\\s-\\)\"" "«~")
777 ("\\(\\S-\\)\"" "~»")
778 ("\\(\\s-\\)'" "`"))
779 '(("\\(\\s-\\)\"" "``")
780 ("\\(\\S-\\)\"" "''")
781 ("\\(\\s-\\)'" "`")))))
782 (mapc (lambda(l) (goto-char (point-min))
783 (while (re-search-forward (car l) nil t)
784 (let ((rpl (concat (match-string 1) (cadr l))))
785 (org-export-latex-protect-string rpl)
786 (org-if-unprotected
787 (replace-match rpl t t))))) quote-rpl)))
789 (defun org-export-latex-special-chars (sub-superscript)
790 "Export special characters to LaTeX.
791 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
792 See the `org-export-latex.el' code for a complete conversion table."
793 (goto-char (point-min))
794 (mapc (lambda(c)
795 (goto-char (point-min))
796 (while (re-search-forward c nil t)
797 ;; Put the point where to check for org-protected
798 (unless (or (get-text-property (match-beginning 2) 'org-protected)
799 (org-at-table-p))
800 (cond ((member (match-string 2) '("\\$" "$"))
801 (if (equal (match-string 2) "\\$")
802 (replace-match (concat (match-string 1) "$"
803 (match-string 3)) t t)
804 (replace-match (concat (match-string 1) "\\$"
805 (match-string 3)) t t)))
806 ((member (match-string 2) '("&" "%" "#"))
807 (if (equal (match-string 1) "\\")
808 (replace-match (match-string 2) t t)
809 (replace-match (concat (match-string 1) "\\"
810 (match-string 2)) t t)))
811 ((equal (match-string 2) "...")
812 (replace-match
813 (concat (match-string 1)
814 (org-export-latex-protect-string "\\ldots{}")) t t))
815 ((equal (match-string 2) "~")
816 (cond ((equal (match-string 1) "\\") nil)
817 ((eq 'org-link (get-text-property 0 'face (match-string 2)))
818 (replace-match (concat (match-string 1) "\\~") t t))
819 (t (replace-match
820 (org-export-latex-protect-string
821 (concat (match-string 1) "\\~{}")) t t))))
822 ((member (match-string 2) '("{" "}"))
823 (unless (save-match-data (org-inside-LaTeX-fragment-p))
824 (if (equal (match-string 1) "\\")
825 (replace-match (match-string 2) t t)
826 (replace-match (concat (match-string 1) "\\"
827 (match-string 2)) t t)))))
828 (unless (save-match-data (org-inside-LaTeX-fragment-p))
829 (cond ((equal (match-string 2) "\\")
830 (replace-match (or (save-match-data
831 (org-export-latex-treat-backslash-char
832 (match-string 1)
833 (match-string 3))) "") t t))
834 ((member (match-string 2) '("_" "^"))
835 (replace-match (or (save-match-data
836 (org-export-latex-treat-sub-super-char
837 sub-superscript
838 (match-string 2)
839 (match-string 1)
840 (match-string 3))) "") t t)))))))
841 '("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
842 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
843 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
844 "\\(.\\|^\\)\\(&\\)"
845 "\\(.\\|^\\)\\(#\\)"
846 "\\(.\\|^\\)\\(%\\)"
847 "\\(.\\|^\\)\\({\\)"
848 "\\(.\\|^\\)\\(}\\)"
849 "\\(.\\|^\\)\\(~\\)"
850 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
851 ;; (?\< . "\\textless{}")
852 ;; (?\> . "\\textgreater{}")
855 (defun org-export-latex-treat-sub-super-char
856 (subsup char string-before string-after)
857 "Convert the \"_\" and \"^\" characters to LaTeX.
858 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
859 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
860 (cond ((equal string-before "\\")
861 (concat string-before char string-after))
862 ;; this is part of a math formula
863 ((and (string-match "\\S-+" string-before)
864 (string-match "\\S-+" string-after))
865 (cond ((eq 'org-link (get-text-property 0 'face char))
866 (concat string-before "\\" char string-after))
867 ((save-match-data (org-inside-LaTeX-fragment-p))
868 (if subsup
869 (cond ((eq 1 (length string-after))
870 (concat string-before char string-after))
871 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
872 (format "%s%s{%s}" string-before char
873 (match-string 1 string-after))))))
874 ((and subsup
875 (> (length string-after) 1)
876 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
877 (format "$%s%s{%s}$" string-before char
878 (match-string 1 string-after)))
879 (subsup (concat "$" string-before char string-after "$"))
880 (t (org-export-latex-protect-string
881 (concat string-before "\\" char "{}" string-after)))))
882 (t (org-export-latex-protect-string
883 (concat string-before "\\" char "{}" string-after)))))
885 (defun org-export-latex-treat-backslash-char (string-before string-after)
886 "Convert the \"$\" special character to LaTeX.
887 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
888 (cond ((member (list string-after) org-html-entities)
889 ;; backslash is part of a special entity (like "\alpha")
890 (concat string-before "$\\"
891 (or (cdar (member (list string-after) org-html-entities))
892 string-after) "$"))
893 ((and (not (string-match "^[ \n\t]" string-after))
894 (not (string-match "[ \t]\\'\\|^" string-before)))
895 ;; backslash is inside a word
896 (org-export-latex-protect-string
897 (concat string-before "\\textbackslash{}" string-after)))
898 ((not (or (equal string-after "")
899 (string-match "^[ \t\n]" string-after)))
900 ;; backslash might escape a character (like \#) or a user TeX
901 ;; macro (like \setcounter)
902 (org-export-latex-protect-string
903 (concat string-before "\\" string-after)))
904 ((and (string-match "^[ \t\n]" string-after)
905 (string-match "[ \t\n]\\'" string-before))
906 ;; backslash is alone, convert it to $\backslash$
907 (org-export-latex-protect-string
908 (concat string-before "\\textbackslash{}" string-after)))
909 (t (org-export-latex-protect-string
910 (concat string-before "\\textbackslash{}" string-after)))))
912 (defun org-export-latex-keywords (timestamps)
913 "Convert special keywords to LaTeX.
914 Regexps are those from `org-export-latex-special-string-regexps'.
915 If TIMESTAMPS, convert timestamps, otherwise delete them."
916 (let ((rg org-export-latex-special-string-regexps) r)
917 (while (setq r (pop rg))
918 (goto-char (point-min))
919 (while (re-search-forward (eval r) nil t)
920 (if (not timestamps)
921 (replace-match (format "\\\\texttt{%s}" (match-string 0)) t)
922 (replace-match ""))))))
924 (defun org-export-latex-fixed-width (opt)
925 "When OPT is non-nil convert fixed-width sections to LaTeX."
926 (goto-char (point-min))
927 (while (re-search-forward "^[ \t]*:" nil t)
928 (if opt
929 (progn (goto-char (match-beginning 0))
930 (insert "\\begin{verbatim}\n")
931 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
932 (replace-match (concat (match-string 1)
933 (match-string 2)) t t)
934 (forward-line))
935 (insert "\\end{verbatim}\n\n"))
936 (progn (goto-char (match-beginning 0))
937 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
938 (replace-match (concat "%" (match-string 1)
939 (match-string 2)) t t)
940 (forward-line))))))
943 (defvar org-table-last-alignment) ; defined in org-table.el
944 (declare-function orgtbl-to-latex "org-table" (table params) t)
945 (defun org-export-latex-tables (insert)
946 "Convert tables to LaTeX and INSERT it."
947 (goto-char (point-min))
948 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
949 ;; FIXME really need to save-excursion?
950 (save-excursion (org-table-align))
951 (let* ((beg (org-table-begin))
952 (end (org-table-end))
953 (raw-table (buffer-substring-no-properties beg end))
954 fnum fields line lines olines gr colgropen line-fmt align)
955 (if org-export-latex-tables-verbatim
956 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
957 "\\end{verbatim}\n")))
958 (apply 'delete-region (list beg end))
959 (insert (org-export-latex-protect-string tbl)))
960 (progn
961 (setq lines (split-string raw-table "\n" t))
962 (apply 'delete-region (list beg end))
963 (when org-export-table-remove-special-lines
964 (setq lines (org-table-clean-before-export lines)))
965 ;; make a formatting string to reflect aligment
966 (setq olines lines)
967 (while (and (not line-fmt) (setq line (pop olines)))
968 (unless (string-match "^[ \t]*|-" line)
969 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
970 (setq fnum (make-vector (length fields) 0))
971 (setq line-fmt
972 (mapconcat
973 (lambda (x)
974 (setq gr (pop org-table-colgroup-info))
975 (format "%s%%s%s"
976 (cond ((eq gr ':start)
977 (prog1 (if colgropen "|" "")
978 (setq colgropen t)))
979 ((eq gr ':startend)
980 (prog1 (if colgropen "|" "|")
981 (setq colgropen nil)))
982 (t ""))
983 (if (memq gr '(:end :startend))
984 (progn (setq colgropen nil) "|")
985 "")))
986 fnum ""))))
987 ;; fix double || in line-fmt
988 (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
989 ;; maybe remove the first and last "|"
990 (when (and (not org-export-latex-tables-column-borders)
991 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
992 (setq line-fmt (match-string 2 line-fmt)))
993 ;; format alignment
994 (setq align (apply 'format
995 (cons line-fmt
996 (mapcar (lambda (x) (if x "r" "l"))
997 org-table-last-alignment))))
998 ;; prepare the table to send to orgtbl-to-latex
999 (setq lines
1000 (mapcar
1001 (lambda(elem)
1002 (or (and (string-match "[ \t]*|-+" elem) 'hline)
1003 (split-string (org-trim elem) "|" t)))
1004 lines))
1005 (when insert
1006 (insert (org-export-latex-protect-string
1007 (orgtbl-to-latex
1008 lines `(:tstart ,(concat "\\begin{tabular}{" align "}"))))
1009 "\n\n")))))))
1011 (defun org-export-latex-fontify ()
1012 "Convert fontification to LaTeX."
1013 (goto-char (point-min))
1014 (while (re-search-forward org-emph-re nil t)
1015 ;; The match goes one char after the *string*
1016 (let ((emph (assoc (match-string 3)
1017 org-export-latex-emphasis-alist))
1018 rpl)
1019 (unless (get-text-property (1- (point)) 'org-protected)
1020 (setq rpl (concat (match-string 1)
1021 (format (org-export-latex-protect-char-in-string
1022 '("\\" "{" "}") (cadr emph))
1023 (match-string 4))
1024 (match-string 5)))
1025 (if (caddr emph)
1026 (setq rpl (org-export-latex-protect-string rpl)))
1027 (replace-match rpl t t)))
1028 (backward-char)))
1030 (defun org-export-latex-links ()
1031 ;; Make sure to use the LaTeX hyperref and graphicx package
1032 ;; or send some warnings.
1033 "Convert links to LaTeX."
1034 (goto-char (point-min))
1035 (while (re-search-forward org-bracket-link-analytic-regexp nil t)
1036 (org-if-unprotected
1037 (goto-char (match-beginning 0))
1038 (let* ((re-radio org-export-latex-all-targets-re)
1039 (remove (list (match-beginning 0) (match-end 0)))
1040 (type (match-string 2))
1041 (raw-path (org-extract-attributes (match-string 3)))
1042 (full-raw-path (concat (match-string 1) raw-path))
1043 (desc (match-string 5))
1044 imgp radiop
1045 ;; define the path of the link
1046 (path (cond
1047 ((member type '("http" "https" "ftp"))
1048 (concat type ":" raw-path))
1049 ((and re-radio (string-match re-radio raw-path))
1050 (setq radiop t))
1051 ((equal type "mailto")
1052 (concat type ":" raw-path))
1053 ((equal type "file")
1054 (if (and (or (org-file-image-p (expand-file-name raw-path))
1055 (string-match "\\.eps$" raw-path))
1056 (equal desc full-raw-path))
1057 (setq imgp t)
1058 (progn (when (string-match "\\(.+\\)::.+" raw-path)
1059 (setq raw-path (match-string 1 raw-path)))
1060 (if (file-exists-p raw-path)
1061 (concat type "://" (expand-file-name raw-path))
1062 (concat type "://" (org-export-directory
1063 :LaTeX org-export-latex-options-plist)
1064 raw-path))))))))
1065 ;; process with link inserting
1066 (apply 'delete-region remove)
1067 (cond ((and imgp (plist-get org-export-latex-options-plist :inline-images))
1068 (insert (format "\\includegraphics[%s]{%s}"
1069 ;; image option should be set be a comment line
1070 org-export-latex-image-default-option
1071 (expand-file-name raw-path))))
1072 (radiop (insert (format "\\hyperref[%s]{%s}" raw-path desc)))
1073 (path (insert (format "\\href{%s}{%s}" path desc)))
1074 (t (insert "\\texttt{" desc "}")))))))
1076 (defvar org-latex-entities) ; defined below
1078 (defun org-export-latex-preprocess ()
1079 "Clean stuff in the LaTeX export."
1081 ;; Preserve line breaks
1082 (goto-char (point-min))
1083 (while (re-search-forward "\\\\\\\\" nil t)
1084 (add-text-properties (match-beginning 0) (match-end 0)
1085 '(org-protected t)))
1087 ;; Preserve latex environments
1088 (goto-char (point-min))
1089 (while (search-forward "\\begin{" nil t)
1090 (let ((start (progn (beginning-of-line) (point)))
1091 (end (or (and (search-forward "\\end{" nil t)
1092 (end-of-line) (point))
1093 (point-max))))
1094 (add-text-properties start end '(org-protected t))))
1096 ;; Convert LaTeX to \LaTeX{}
1097 (goto-char (point-min))
1098 (let ((case-fold-search nil) rpl)
1099 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
1100 (replace-match (org-export-latex-protect-string
1101 (concat (match-string 1) "\\LaTeX{}")) t t)))
1103 ;; Convert blockquotes
1104 (goto-char (point-min))
1105 (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
1106 (replace-match "\\begin{quote}" t t))
1107 (goto-char (point-min))
1108 (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
1109 (replace-match "\\end{quote}" t t))
1111 ;; Convert verse
1112 (goto-char (point-min))
1113 (while (search-forward "ORG-VERSE-START" nil t)
1114 (replace-match "\\begin{verse}" t t))
1115 (goto-char (point-min))
1116 (while (search-forward "ORG-VERSE-END" nil t)
1117 (replace-match "\\end{verse}" t t))
1119 ;; Convert horizontal rules
1120 (goto-char (point-min))
1121 (while (re-search-forward "^----+.$" nil t)
1122 (replace-match (org-export-latex-protect-string "\\hrule") t t))
1124 ;; Protect LaTeX commands like \commad[...]{...} or \command{...}
1125 (goto-char (point-min))
1126 (while (re-search-forward "\\\\[a-zA-Z]+\\(?:\\[.*\\]\\)?{.*}" nil t)
1127 (add-text-properties (match-beginning 0) (match-end 0)
1128 '(org-protected t)))
1130 ;; Protect LaTeX entities
1131 (goto-char (point-min))
1132 (while (re-search-forward (regexp-opt org-latex-entities) nil t)
1133 (add-text-properties (match-beginning 0) (match-end 0)
1134 '(org-protected t)))
1136 ;; Replace radio links
1137 (goto-char (point-min))
1138 (while (re-search-forward
1139 (concat "<<<?" org-export-latex-all-targets-re
1140 ">>>?\\((INVISIBLE)\\)?") nil t)
1141 (replace-match
1142 (org-export-latex-protect-string
1143 (format "\\label{%s}%s"(match-string 1)
1144 (if (match-string 2) "" (match-string 1)))) t t))
1146 ;; Delete @<...> constructs
1147 ;; Thanks to Daniel Clemente for this regexp
1148 (goto-char (point-min))
1149 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
1150 (replace-match ""))
1152 ;; When converting to LaTeX, replace footnotes
1153 ;; FIXME: don't protect footnotes from conversion
1154 (when (plist-get org-export-latex-options-plist :footnotes)
1155 (goto-char (point-min))
1156 (while (re-search-forward "\\[[0-9]+\\]" nil t)
1157 (when (save-match-data
1158 (save-excursion (beginning-of-line)
1159 (looking-at "[^:|#]")))
1160 (let ((foot-beg (match-beginning 0))
1161 (foot-end (match-end 0))
1162 (foot-prefix (match-string 0))
1163 footnote footnote-rpl)
1164 (save-excursion
1165 (when (search-forward foot-prefix nil t)
1166 (replace-match "")
1167 (let ((end (save-excursion
1168 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
1169 (match-beginning 0) (point-max)))))
1170 (setq footnote (concat (org-trim (buffer-substring (point) end))
1171 " ")) ; prevent last } being part of a link
1172 (delete-region (point) end))
1173 (goto-char foot-beg)
1174 (delete-region foot-beg foot-end)
1175 (unless (null footnote)
1176 (setq footnote-rpl (format "\\footnote{%s}" footnote))
1177 (add-text-properties 0 10 '(org-protected t) footnote-rpl)
1178 (add-text-properties (1- (length footnote-rpl))
1179 (length footnote-rpl)
1180 '(org-protected t) footnote-rpl)
1181 (insert footnote-rpl)))))))
1183 ;; Replace footnote section tag for LaTeX
1184 (goto-char (point-min))
1185 (while (re-search-forward
1186 (concat "^" footnote-section-tag-regexp) nil t)
1187 (replace-match ""))))
1189 ;;; List handling:
1191 (defun org-export-latex-lists ()
1192 "Replace plain text lists in current buffer into LaTeX lists."
1193 "Convert lists to LaTeX."
1194 (goto-char (point-min))
1195 (while (re-search-forward org-list-beginning-re nil t)
1196 (beginning-of-line)
1197 (insert (org-list-to-latex (org-list-parse-list t)) "\n")))
1199 (defconst org-latex-entities
1200 '("\\!"
1201 "\\'"
1202 "\\+"
1203 "\\,"
1204 "\\-"
1205 "\\:"
1206 "\\;"
1207 "\\<"
1208 "\\="
1209 "\\>"
1210 "\\Huge"
1211 "\\LARGE"
1212 "\\Large"
1213 "\\Styles"
1214 "\\\\"
1215 "\\`"
1216 "\\addcontentsline"
1217 "\\address"
1218 "\\addtocontents"
1219 "\\addtocounter"
1220 "\\addtolength"
1221 "\\addvspace"
1222 "\\alph"
1223 "\\appendix"
1224 "\\arabic"
1225 "\\author"
1226 "\\begin{array}"
1227 "\\begin{center}"
1228 "\\begin{description}"
1229 "\\begin{enumerate}"
1230 "\\begin{eqnarray}"
1231 "\\begin{equation}"
1232 "\\begin{figure}"
1233 "\\begin{flushleft}"
1234 "\\begin{flushright}"
1235 "\\begin{itemize}"
1236 "\\begin{list}"
1237 "\\begin{minipage}"
1238 "\\begin{picture}"
1239 "\\begin{quotation}"
1240 "\\begin{quote}"
1241 "\\begin{tabbing}"
1242 "\\begin{table}"
1243 "\\begin{tabular}"
1244 "\\begin{thebibliography}"
1245 "\\begin{theorem}"
1246 "\\begin{titlepage}"
1247 "\\begin{verbatim}"
1248 "\\begin{verse}"
1249 "\\bf"
1250 "\\bf"
1251 "\\bibitem"
1252 "\\bigskip"
1253 "\\cdots"
1254 "\\centering"
1255 "\\circle"
1256 "\\cite"
1257 "\\cleardoublepage"
1258 "\\clearpage"
1259 "\\cline"
1260 "\\closing"
1261 "\\dashbox"
1262 "\\date"
1263 "\\ddots"
1264 "\\dotfill"
1265 "\\em"
1266 "\\fbox"
1267 "\\flushbottom"
1268 "\\fnsymbol"
1269 "\\footnote"
1270 "\\footnotemark"
1271 "\\footnotesize"
1272 "\\footnotetext"
1273 "\\frac"
1274 "\\frame"
1275 "\\framebox"
1276 "\\hfill"
1277 "\\hline"
1278 "\\hrulespace"
1279 "\\hspace"
1280 "\\huge"
1281 "\\hyphenation"
1282 "\\include"
1283 "\\includeonly"
1284 "\\indent"
1285 "\\input"
1286 "\\it"
1287 "\\kill"
1288 "\\label"
1289 "\\large"
1290 "\\ldots"
1291 "\\line"
1292 "\\linebreak"
1293 "\\linethickness"
1294 "\\listoffigures"
1295 "\\listoftables"
1296 "\\location"
1297 "\\makebox"
1298 "\\maketitle"
1299 "\\mark"
1300 "\\mbox"
1301 "\\medskip"
1302 "\\multicolumn"
1303 "\\multiput"
1304 "\\newcommand"
1305 "\\newcounter"
1306 "\\newenvironment"
1307 "\\newfont"
1308 "\\newlength"
1309 "\\newline"
1310 "\\newpage"
1311 "\\newsavebox"
1312 "\\newtheorem"
1313 "\\nocite"
1314 "\\nofiles"
1315 "\\noindent"
1316 "\\nolinebreak"
1317 "\\nopagebreak"
1318 "\\normalsize"
1319 "\\onecolumn"
1320 "\\opening"
1321 "\\oval"
1322 "\\overbrace"
1323 "\\overline"
1324 "\\pagebreak"
1325 "\\pagenumbering"
1326 "\\pageref"
1327 "\\pagestyle"
1328 "\\par"
1329 "\\parbox"
1330 "\\put"
1331 "\\raggedbottom"
1332 "\\raggedleft"
1333 "\\raggedright"
1334 "\\raisebox"
1335 "\\ref"
1336 "\\rm"
1337 "\\roman"
1338 "\\rule"
1339 "\\savebox"
1340 "\\sc"
1341 "\\scriptsize"
1342 "\\setcounter"
1343 "\\setlength"
1344 "\\settowidth"
1345 "\\sf"
1346 "\\shortstack"
1347 "\\signature"
1348 "\\sl"
1349 "\\small"
1350 "\\smallskip"
1351 "\\sqrt"
1352 "\\tableofcontents"
1353 "\\telephone"
1354 "\\thanks"
1355 "\\thispagestyle"
1356 "\\tiny"
1357 "\\title"
1358 "\\tt"
1359 "\\twocolumn"
1360 "\\typein"
1361 "\\typeout"
1362 "\\underbrace"
1363 "\\underline"
1364 "\\usebox"
1365 "\\usecounter"
1366 "\\value"
1367 "\\vdots"
1368 "\\vector"
1369 "\\verb"
1370 "\\vfill"
1371 "\\vline"
1372 "\\vspace")
1373 "A list of LaTeX commands to be protected when performing conversion.")
1375 (provide 'org-export-latex)
1377 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
1379 ;;; org-export-latex.el ends here