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