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