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