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