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