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