Release 6.07a.
[org-mode.git] / lisp / org-export-latex.el
blob20ff5987245adfdb959c1c82d9c4f98f7e486642
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.07a
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 :select-tags (plist-get opt-plist :select-tags)
433 :exclude-tags (plist-get opt-plist :exclude-tags)
434 :LaTeX-fragments nil)))
436 (set-buffer buffer)
437 (erase-buffer)
439 (and (fboundp 'set-buffer-file-coding-system)
440 (set-buffer-file-coding-system coding-system-for-write))
442 ;; insert the header and initial document commands
443 (unless (or (eq to-buffer 'string) body-only)
444 (insert header))
446 ;; insert text found in #+TEXT
447 (when (and text (not (eq to-buffer 'string)))
448 (insert (org-export-latex-content
449 text '(lists tables fixed-width keywords))
450 "\n\n"))
452 ;; insert lines before the first headline
453 (unless (or skip (eq to-buffer 'string))
454 (insert first-lines))
456 ;; handle the case where the region does not begin with a section
457 (when region-p
458 (insert (with-temp-buffer
459 (insert string-for-export)
460 (org-export-latex-first-lines))))
462 ;; export the content of headlines
463 (org-export-latex-global
464 (with-temp-buffer
465 (insert string-for-export)
466 (goto-char (point-min))
467 (when (re-search-forward "^\\(\\*+\\) " nil t)
468 (let* ((asters (length (match-string 1)))
469 (level (if odd (- asters 2) (- asters 1))))
470 (setq org-export-latex-add-level
471 (if odd (1- (/ (1+ asters) 2)) (1- asters)))
472 (org-export-latex-parse-global level odd)))))
474 ;; finalization
475 (unless body-only (insert "\n\\end{document}"))
476 (or to-buffer (save-buffer))
477 (goto-char (point-min))
478 (message "Exporting to LaTeX...done")
479 (prog1
480 (if (eq to-buffer 'string)
481 (prog1 (buffer-substring (point-min) (point-max))
482 (kill-buffer (current-buffer)))
483 (current-buffer))
484 (set-window-configuration wcf))))
486 ;;; Parsing functions:
488 (defun org-export-latex-parse-global (level odd)
489 "Parse the current buffer recursively, starting at LEVEL.
490 If ODD is non-nil, assume the buffer only contains odd sections.
491 Return a list reflecting the document structure."
492 (save-excursion
493 (goto-char (point-min))
494 (let* ((cnt 0) output
495 (depth org-export-latex-sectioning-depth))
496 (while (re-search-forward
497 (concat "^\\(\\(?:\\*\\)\\{"
498 (number-to-string (+ (if odd 2 1) level))
499 "\\}\\) \\(.*\\)$")
500 ;; make sure that there is no upper heading
501 (when (> level 0)
502 (save-excursion
503 (save-match-data
504 (re-search-forward
505 (concat "^\\(\\(?:\\*\\)\\{"
506 (number-to-string level)
507 "\\}\\) \\(.*\\)$") nil t)))) t)
508 (setq cnt (1+ cnt))
509 (let* ((pos (match-beginning 0))
510 (heading (match-string 2))
511 (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
512 (save-excursion
513 (narrow-to-region
514 (point)
515 (save-match-data
516 (if (re-search-forward
517 (concat "^\\(\\(?:\\*\\)\\{"
518 (number-to-string (+ (if odd 2 1) level))
519 "\\}\\) \\(.*\\)$") nil t)
520 (match-beginning 0)
521 (point-max))))
522 (goto-char (point-min))
523 (setq output
524 (append output
525 (list
526 (list
527 `(pos . ,pos)
528 `(level . ,nlevel)
529 `(occur . ,cnt)
530 `(heading . ,heading)
531 `(content . ,(org-export-latex-parse-content))
532 `(subcontent . ,(org-export-latex-parse-subcontent
533 level odd)))))))
534 (widen)))
535 (list output))))
537 (defun org-export-latex-parse-content ()
538 "Extract the content of a section."
539 (let ((beg (point))
540 (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t)
541 (progn (beginning-of-line) (point))
542 (point-max))))
543 (buffer-substring beg end)))
545 (defun org-export-latex-parse-subcontent (level odd)
546 "Extract the subcontent of a section at LEVEL.
547 If ODD Is non-nil, assume subcontent only contains odd sections."
548 (if (not (re-search-forward
549 (concat "^\\(\\(?:\\*\\)\\{"
550 (number-to-string (+ (if odd 4 2) level))
551 "\\}\\) \\(.*\\)$")
552 nil t))
553 nil ; subcontent is nil
554 (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
556 ;;; Rendering functions:
557 (defun org-export-latex-global (content)
558 "Export CONTENT to LaTeX.
559 CONTENT is an element of the list produced by
560 `org-export-latex-parse-global'."
561 (if (eq (car content) 'subcontent)
562 (mapc 'org-export-latex-sub (cdr content))
563 (org-export-latex-sub (car content))))
565 (defun org-export-latex-sub (subcontent)
566 "Export the list SUBCONTENT to LaTeX.
567 SUBCONTENT is an alist containing information about the headline
568 and its content."
569 (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
570 (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
572 (defun org-export-latex-subcontent (subcontent num)
573 "Export each cell of SUBCONTENT to LaTeX."
574 (let ((heading (org-export-latex-fontify-headline
575 (cdr (assoc 'heading subcontent))))
576 (level (- (cdr (assoc 'level subcontent))
577 org-export-latex-add-level))
578 (occur (number-to-string (cdr (assoc 'occur subcontent))))
579 (content (cdr (assoc 'content subcontent)))
580 (subcontent (cadr (assoc 'subcontent subcontent))))
581 (cond
582 ;; Normal conversion
583 ((<= level org-export-latex-sectioning-depth)
584 (let ((sec (nth (1- level) org-export-latex-sectioning)))
585 (insert (format (if num (car sec) (cdr sec)) heading) "\n"))
586 (insert (org-export-latex-content content))
587 (cond ((stringp subcontent) (insert subcontent))
588 ((listp subcontent) (org-export-latex-sub subcontent))))
589 ;; At a level under the hl option: we can drop this subsection
590 ((> level org-export-latex-sectioning-depth)
591 (cond ((eq org-export-latex-low-levels 'description)
592 (insert (format "\\begin{description}\n\n\\item[%s]\n\n" heading))
593 (insert (org-export-latex-content content))
594 (cond ((stringp subcontent) (insert subcontent))
595 ((listp subcontent) (org-export-latex-sub subcontent)))
596 (insert "\\end{description}\n"))
597 ((stringp org-export-latex-low-levels)
598 (insert (format org-export-latex-low-levels heading) "\n")
599 (insert (org-export-latex-content content))
600 (cond ((stringp subcontent) (insert subcontent))
601 ((listp subcontent) (org-export-latex-sub subcontent)))))))))
603 ;;; Exporting internals:
604 (defun org-export-latex-set-initial-vars (ext-plist level)
605 "Store org local variables required for LaTeX export.
606 EXT-PLIST is an optional additional plist.
607 LEVEL indicates the default depth for export."
608 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
609 org-export-latex-all-targets-re
610 (org-make-target-link-regexp (org-all-targets))
611 org-export-latex-options-plist
612 (org-combine-plists (org-default-export-plist) ext-plist
613 (org-infile-export-plist))
614 org-export-latex-class
615 (save-excursion
616 (goto-char (point-min))
617 (if (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
618 (assoc (match-string 1) org-export-latex-classes))
619 (match-string 1)
620 org-export-latex-default-class))
621 org-export-latex-header
622 (cadr (assoc org-export-latex-class org-export-latex-classes))
623 org-export-latex-sectioning
624 (cddr (assoc org-export-latex-class org-export-latex-classes))
625 org-export-latex-sectioning-depth
626 (or level
627 (let ((hl-levels
628 (plist-get org-export-latex-options-plist :headline-levels))
629 (sec-depth (length org-export-latex-sectioning)))
630 (if (> hl-levels sec-depth) sec-depth hl-levels)))))
632 (defun org-export-latex-make-header (title opt-plist)
633 "Make the LaTeX header and return it as a string.
634 TITLE is the current title from the buffer or region.
635 OPT-PLIST is the options plist for current buffer."
636 (let ((toc (plist-get opt-plist :table-of-contents))
637 (author (plist-get opt-plist :author)))
638 (concat
639 (if (plist-get opt-plist :time-stamp-file)
640 (format-time-string "% Created %Y-%m-%d %a %H:%M\n"))
641 ;; insert LaTeX custom header
642 org-export-latex-header
643 "\n"
644 ;; insert information on LaTeX packages
645 (when org-export-latex-packages-alist
646 (mapconcat (lambda(p)
647 (if (equal "" (car p))
648 (format "\\usepackage{%s}" (cadr p))
649 (format "\\usepackage[%s]{%s}"
650 (car p) (cadr p))))
651 org-export-latex-packages-alist "\n"))
652 ;; insert additional commands in the header
653 org-export-latex-append-header
654 ;; insert the title
655 (format
656 "\n\n\\title{%s}\n"
657 ;; convert the title
658 (org-export-latex-content
659 title '(lists tables fixed-width keywords)))
660 ;; insert author info
661 (if (plist-get opt-plist :author-info)
662 (format "\\author{%s}\n"
663 (or author user-full-name))
664 (format "%%\\author{%s}\n"
665 (or author user-full-name)))
666 ;; insert the date
667 (format "\\date{%s}\n"
668 (format-time-string
669 (or (plist-get opt-plist :date)
670 org-export-latex-date-format)))
671 ;; beginning of the document
672 "\n\\begin{document}\n\n"
673 ;; insert the title command
674 (if (string-match "%s" org-export-latex-title-command)
675 (format org-export-latex-title-command title)
676 org-export-latex-title-command)
677 "\n\n"
678 ;; table of contents
679 (when (and org-export-with-toc
680 (plist-get opt-plist :section-numbers))
681 (cond ((numberp toc)
682 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
683 (min toc (plist-get opt-plist :headline-levels))))
684 (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
685 (plist-get opt-plist :headline-levels))))))))
687 (defun org-export-latex-first-lines (&optional comments)
688 "Export the first lines before first headline.
689 COMMENTS is either nil to replace them with the empty string or a
690 formatting string like %%%%s if we want to comment them out."
691 (save-excursion
692 (goto-char (point-min))
693 (if (org-at-heading-p) (beginning-of-line 2))
694 (let* ((pt (point))
695 (end (if (and (re-search-forward "^\\* " nil t)
696 (not (eq pt (match-beginning 0))))
697 (goto-char (match-beginning 0))
698 (goto-char (point-max)))))
699 (org-export-latex-content
700 (org-export-preprocess-string
701 (buffer-substring (point-min) end)
702 :for-LaTeX t
703 :emph-multiline t
704 :add-text nil
705 :comments nil
706 :skip-before-1st-heading nil
707 :LaTeX-fragments nil)))))
709 (defun org-export-latex-content (content &optional exclude-list)
710 "Convert CONTENT string to LaTeX.
711 Don't perform conversions that are in EXCLUDE-LIST. Recognized
712 conversion types are: quotation-marks, emphasis, sub-superscript,
713 links, keywords, lists, tables, fixed-width"
714 (with-temp-buffer
715 (insert content)
716 (unless (memq 'quotation-marks exclude-list)
717 (org-export-latex-quotation-marks))
718 (unless (memq 'emphasis exclude-list)
719 (when (plist-get org-export-latex-options-plist :emphasize)
720 (org-export-latex-fontify)))
721 (unless (memq 'sub-superscript exclude-list)
722 (org-export-latex-special-chars
723 (plist-get org-export-latex-options-plist :sub-superscript)))
724 (unless (memq 'links exclude-list)
725 (org-export-latex-links))
726 (unless (memq 'keywords exclude-list)
727 (org-export-latex-keywords
728 (plist-get org-export-latex-options-plist :timestamps)))
729 (unless (memq 'lists exclude-list)
730 (org-export-latex-lists))
731 (unless (memq 'tables exclude-list)
732 (org-export-latex-tables
733 (plist-get org-export-latex-options-plist :tables)))
734 (unless (memq 'fixed-width exclude-list)
735 (org-export-latex-fixed-width
736 (plist-get org-export-latex-options-plist :fixed-width)))
737 ;; return string
738 (buffer-substring (point-min) (point-max))))
740 (defun org-export-latex-protect-string (s)
741 "Prevent further conversion for string S by adding the
742 org-protect property."
743 (add-text-properties 0 (length s) '(org-protected t) s) s)
745 (defun org-export-latex-protect-char-in-string (char-list string)
746 "Add org-protected text-property to char from CHAR-LIST in STRING."
747 (with-temp-buffer
748 (save-match-data
749 (insert string)
750 (goto-char (point-min))
751 (while (re-search-forward (regexp-opt char-list) nil t)
752 (add-text-properties (match-beginning 0)
753 (match-end 0) '(org-protected t)))
754 (buffer-string))))
756 (defun org-export-latex-keywords-maybe (remove-list)
757 "Maybe remove keywords depending on rules in REMOVE-LIST."
758 (goto-char (point-min))
759 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
760 (case-fold-search nil))
761 ;; convert TODO keywords
762 (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
763 (if (plist-get remove-list :todo)
764 (replace-match "")
765 (replace-match (format "\\texttt{%s}" (match-string 1)) t t)))
766 ;; convert priority string
767 (when (re-search-forward "\\[\\\\#.\\]" nil t)
768 (if (plist-get remove-list :priority)
769 (replace-match "")
770 (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))
771 ;; convert tags
772 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t)
773 (if (or (not org-export-with-tags)
774 (plist-get remove-list :tags))
775 (replace-match "")
776 (replace-match
777 (org-export-latex-protect-string
778 (format "\\texttt{%s}" (save-match-data
779 (org-quote-chars (match-string 0)))))
780 t t)))))
782 (defun org-quote-chars (s)
783 (let ((start 0))
784 (while (string-match "_" s start)
785 (setq start (+ 2 (match-beginning 0))
786 s (replace-match "\\_" t t s))))
789 (defun org-export-latex-fontify-headline (string)
790 "Fontify special words in string."
791 (with-temp-buffer
792 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
793 ;; the beginning of the buffer - inserting "\n" is safe here though.
794 (insert "\n" string)
795 (goto-char (point-min))
796 (when (plist-get org-export-latex-options-plist :emphasize)
797 (org-export-latex-fontify))
798 (org-export-latex-keywords-maybe
799 org-export-latex-remove-from-headlines)
800 (org-export-latex-special-chars
801 (plist-get org-export-latex-options-plist :sub-superscript))
802 (org-export-latex-links)
803 (org-trim (buffer-substring-no-properties (point-min) (point-max)))))
805 (defun org-export-latex-quotation-marks ()
806 "Export question marks depending on language conventions."
807 (let* ((lang (plist-get org-export-latex-options-plist :language))
808 (quote-rpl (if (equal lang "fr")
809 '(("\\(\\s-\\)\"" "«~")
810 ("\\(\\S-\\)\"" "~»")
811 ("\\(\\s-\\)'" "`"))
812 '(("\\(\\s-\\)\"" "``")
813 ("\\(\\S-\\)\"" "''")
814 ("\\(\\s-\\)'" "`")))))
815 (mapc (lambda(l) (goto-char (point-min))
816 (while (re-search-forward (car l) nil t)
817 (let ((rpl (concat (match-string 1) (cadr l))))
818 (org-export-latex-protect-string rpl)
819 (org-if-unprotected
820 (replace-match rpl t t))))) quote-rpl)))
822 (defun org-export-latex-special-chars (sub-superscript)
823 "Export special characters to LaTeX.
824 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
825 See the `org-export-latex.el' code for a complete conversion table."
826 (goto-char (point-min))
827 (mapc (lambda(c)
828 (goto-char (point-min))
829 (while (re-search-forward c nil t)
830 ;; Put the point where to check for org-protected
831 (unless (or (get-text-property (match-beginning 2) 'org-protected)
832 (org-at-table-p))
833 (cond ((member (match-string 2) '("\\$" "$"))
834 (if (equal (match-string 2) "\\$")
835 (replace-match (concat (match-string 1) "$"
836 (match-string 3)) t t)
837 (replace-match (concat (match-string 1) "\\$"
838 (match-string 3)) t t)))
839 ((member (match-string 2) '("&" "%" "#"))
840 (if (equal (match-string 1) "\\")
841 (replace-match (match-string 2) t t)
842 (replace-match (concat (match-string 1) "\\"
843 (match-string 2)) t t)))
844 ((equal (match-string 2) "...")
845 (replace-match
846 (concat (match-string 1)
847 (org-export-latex-protect-string "\\ldots{}")) t t))
848 ((equal (match-string 2) "~")
849 (cond ((equal (match-string 1) "\\") nil)
850 ((eq 'org-link (get-text-property 0 'face (match-string 2)))
851 (replace-match (concat (match-string 1) "\\~") t t))
852 (t (replace-match
853 (org-export-latex-protect-string
854 (concat (match-string 1) "\\~{}")) t t))))
855 ((member (match-string 2) '("{" "}"))
856 (unless (save-match-data (org-inside-LaTeX-fragment-p))
857 (if (equal (match-string 1) "\\")
858 (replace-match (match-string 2) t t)
859 (replace-match (concat (match-string 1) "\\"
860 (match-string 2)) t t)))))
861 (unless (save-match-data (org-inside-LaTeX-fragment-p))
862 (cond ((equal (match-string 2) "\\")
863 (replace-match (or (save-match-data
864 (org-export-latex-treat-backslash-char
865 (match-string 1)
866 (match-string 3))) "") t t))
867 ((member (match-string 2) '("_" "^"))
868 (replace-match (or (save-match-data
869 (org-export-latex-treat-sub-super-char
870 sub-superscript
871 (match-string 1)
872 (match-string 2)
873 (match-string 3))) "") t t)))))))
874 '("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
875 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
876 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
877 "\\(.\\|^\\)\\(&\\)"
878 "\\(.\\|^\\)\\(#\\)"
879 "\\(.\\|^\\)\\(%\\)"
880 "\\(.\\|^\\)\\({\\)"
881 "\\(.\\|^\\)\\(}\\)"
882 "\\(.\\|^\\)\\(~\\)"
883 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
884 ;; (?\< . "\\textless{}")
885 ;; (?\> . "\\textgreater{}")
888 (defun org-export-latex-treat-sub-super-char
889 (subsup string-before char string-after)
890 "Convert the \"_\" and \"^\" characters to LaTeX.
891 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
892 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
893 (cond ((equal string-before "\\")
894 (concat string-before char string-after))
895 ;; this is part of a math formula
896 ((and (string-match "\\S-+" string-before)
897 (string-match "\\S-+" string-after))
898 (cond ((eq 'org-link (get-text-property 0 'face char))
899 (concat string-before "\\" char string-after))
900 ((save-match-data (org-inside-LaTeX-fragment-p))
901 (if subsup
902 (cond ((eq 1 (length string-after))
903 (concat string-before char string-after))
904 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
905 (format "%s%s{%s}" string-before char
906 (match-string 1 string-after))))))
907 ((and subsup
908 (> (length string-after) 1)
909 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
910 (format "$%s%s{%s}$" string-before char
911 (match-string 1 string-after)))
912 (subsup (concat "$" string-before char string-after "$"))
913 (t (org-export-latex-protect-string
914 (concat string-before "\\" char "{}" string-after)))))
915 (t (org-export-latex-protect-string
916 (concat string-before "\\" char "{}" string-after)))))
918 (defun org-export-latex-treat-backslash-char (string-before string-after)
919 "Convert the \"$\" special character to LaTeX.
920 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
921 (cond ((member (list string-after) org-html-entities)
922 ;; backslash is part of a special entity (like "\alpha")
923 (concat string-before "$\\"
924 (or (cdar (member (list string-after) org-html-entities))
925 string-after) "$"))
926 ((and (not (string-match "^[ \n\t]" string-after))
927 (not (string-match "[ \t]\\'\\|^" string-before)))
928 ;; backslash is inside a word
929 (org-export-latex-protect-string
930 (concat string-before "\\textbackslash{}" string-after)))
931 ((not (or (equal string-after "")
932 (string-match "^[ \t\n]" string-after)))
933 ;; backslash might escape a character (like \#) or a user TeX
934 ;; macro (like \setcounter)
935 (org-export-latex-protect-string
936 (concat string-before "\\" string-after)))
937 ((and (string-match "^[ \t\n]" string-after)
938 (string-match "[ \t\n]\\'" string-before))
939 ;; backslash is alone, convert it to $\backslash$
940 (org-export-latex-protect-string
941 (concat string-before "\\textbackslash{}" string-after)))
942 (t (org-export-latex-protect-string
943 (concat string-before "\\textbackslash{}" string-after)))))
945 (defun org-export-latex-keywords (timestamps)
946 "Convert special keywords to LaTeX.
947 Regexps are those from `org-export-latex-special-string-regexps'."
948 (let ((rg org-export-latex-special-string-regexps) r)
949 (while (setq r (pop rg))
950 (goto-char (point-min))
951 (while (re-search-forward (eval r) nil t)
952 (if (not timestamps)
953 (replace-match (format "\\\\texttt{%s}" (match-string 0)) t)
954 (replace-match ""))))))
956 (defun org-export-latex-fixed-width (opt)
957 "When OPT is non-nil convert fixed-width sections to LaTeX."
958 (goto-char (point-min))
959 (while (re-search-forward "^[ \t]*:" nil t)
960 (if opt
961 (progn (goto-char (match-beginning 0))
962 (insert "\\begin{verbatim}\n")
963 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
964 (replace-match (concat (match-string 1)
965 (match-string 2)) t t)
966 (forward-line))
967 (insert "\\end{verbatim}\n\n"))
968 (progn (goto-char (match-beginning 0))
969 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
970 (replace-match (concat "%" (match-string 1)
971 (match-string 2)) t t)
972 (forward-line))))))
975 (defvar org-table-last-alignment) ; defined in org-table.el
976 (declare-function orgtbl-to-latex "org-table" (table params) t)
977 (defun org-export-latex-tables (insert)
978 "Convert tables to LaTeX and INSERT it."
979 (goto-char (point-min))
980 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
981 ;; FIXME really need to save-excursion?
982 (save-excursion (org-table-align))
983 (let* ((beg (org-table-begin))
984 (end (org-table-end))
985 (raw-table (buffer-substring-no-properties beg end))
986 fnum fields line lines olines gr colgropen line-fmt align)
987 (if org-export-latex-tables-verbatim
988 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
989 "\\end{verbatim}\n")))
990 (apply 'delete-region (list beg end))
991 (insert tbl))
992 (progn
993 (setq lines (split-string raw-table "\n" t))
994 (apply 'delete-region (list beg end))
995 (when org-export-table-remove-special-lines
996 (setq lines (org-table-clean-before-export lines)))
997 ;; make a formatting string to reflect aligment
998 (setq olines lines)
999 (while (and (not line-fmt) (setq line (pop olines)))
1000 (unless (string-match "^[ \t]*|-" line)
1001 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1002 (setq fnum (make-vector (length fields) 0))
1003 (setq line-fmt
1004 (mapconcat
1005 (lambda (x)
1006 (setq gr (pop org-table-colgroup-info))
1007 (format "%s%%s%s"
1008 (cond ((eq gr ':start)
1009 (prog1 (if colgropen "|" "")
1010 (setq colgropen t)))
1011 ((eq gr ':startend)
1012 (prog1 (if colgropen "|" "|")
1013 (setq colgropen nil)))
1014 (t ""))
1015 (if (memq gr '(:end :startend))
1016 (progn (setq colgropen nil) "|")
1017 "")))
1018 fnum ""))))
1019 ;; fix double || in line-fmt
1020 (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
1021 ;; maybe remove the first and last "|"
1022 (when (and (not org-export-latex-tables-column-borders)
1023 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
1024 (setq line-fmt (match-string 2 line-fmt)))
1025 ;; format alignment
1026 (setq align (apply 'format
1027 (cons line-fmt
1028 (mapcar (lambda (x) (if x "r" "l"))
1029 org-table-last-alignment))))
1030 ;; prepare the table to send to orgtbl-to-latex
1031 (setq lines
1032 (mapcar
1033 (lambda(elem)
1034 (or (and (string-match "[ \t]*|-+" elem) 'hline)
1035 (split-string (org-trim elem) "|" t)))
1036 lines))
1037 (when insert
1038 (insert (orgtbl-to-latex
1039 lines `(:tstart ,(concat "\\begin{tabular}{" align "}")))
1040 "\n\n")))))))
1042 (defun org-export-latex-fontify ()
1043 "Convert fontification to LaTeX."
1044 (goto-char (point-min))
1045 (while (re-search-forward org-emph-re nil t)
1046 ;; The match goes one char after the *string*
1047 (let ((emph (assoc (match-string 3)
1048 org-export-latex-emphasis-alist))
1049 rpl)
1050 (unless (get-text-property (1- (point)) 'org-protected)
1051 (setq rpl (concat (match-string 1)
1052 (format (org-export-latex-protect-char-in-string
1053 '("\\" "{" "}") (cadr emph))
1054 (match-string 4))
1055 (match-string 5)))
1056 (if (caddr emph)
1057 (setq rpl (org-export-latex-protect-string rpl)))
1058 (replace-match rpl t t)))
1059 (backward-char)))
1061 (defun org-export-latex-links ()
1062 ;; Make sure to use the LaTeX hyperref and graphicx package
1063 ;; or send some warnings.
1064 "Convert links to LaTeX."
1065 (goto-char (point-min))
1066 (while (re-search-forward org-bracket-link-analytic-regexp nil t)
1067 (org-if-unprotected
1068 (goto-char (match-beginning 0))
1069 (let* ((re-radio org-export-latex-all-targets-re)
1070 (remove (list (match-beginning 0) (match-end 0)))
1071 (type (match-string 2))
1072 (raw-path (org-extract-attributes (match-string 3)))
1073 (full-raw-path (concat (match-string 1) raw-path))
1074 (desc (match-string 5))
1075 imgp radiop
1076 ;; define the path of the link
1077 (path (cond
1078 ((member type '("http" "https" "ftp"))
1079 (concat type ":" raw-path))
1080 ((and re-radio (string-match re-radio raw-path))
1081 (setq radiop t))
1082 ((equal type "mailto")
1083 (concat type ":" raw-path))
1084 ((equal type "file")
1085 (if (and (or (org-file-image-p (expand-file-name raw-path))
1086 (string-match "\\.eps$" raw-path))
1087 (equal desc full-raw-path))
1088 (setq imgp t)
1089 (progn (when (string-match "\\(.+\\)::.+" raw-path)
1090 (setq raw-path (match-string 1 raw-path)))
1091 (if (file-exists-p raw-path)
1092 (concat type "://" (expand-file-name raw-path))
1093 (concat type "://" (org-export-directory
1094 :LaTeX org-export-latex-options-plist)
1095 raw-path))))))))
1096 ;; process with link inserting
1097 (apply 'delete-region remove)
1098 (cond ((and imgp (plist-get org-export-latex-options-plist :inline-images))
1099 (insert (format "\\includegraphics[%s]{%s}"
1100 ;; image option should be set be a comment line
1101 org-export-latex-image-default-option
1102 (expand-file-name raw-path))))
1103 (radiop (insert (format "\\hyperref[%s]{%s}" raw-path desc)))
1104 (path (insert (format "\\href{%s}{%s}" path desc)))
1105 (t (insert "\\texttt{" desc "}")))))))
1107 (defvar org-latex-entities) ; defined below
1109 (defun org-export-latex-preprocess ()
1110 "Clean stuff in the LaTeX export."
1112 ;; Preserve line breaks
1113 (goto-char (point-min))
1114 (while (re-search-forward "\\\\\\\\" nil t)
1115 (add-text-properties (match-beginning 0) (match-end 0)
1116 '(org-protected t)))
1118 ;; Convert LaTeX to \LaTeX{}
1119 (goto-char (point-min))
1120 (let ((case-fold-search nil) rpl)
1121 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
1122 (replace-match (org-export-latex-protect-string
1123 (concat (match-string 1) "\\LaTeX{}")) t t)))
1125 ;; Convert blockquotes
1126 (goto-char (point-min))
1127 (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
1128 (replace-match "\\begin{quote}" t t))
1129 (goto-char (point-min))
1130 (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
1131 (replace-match "\\end{quote}" t t))
1133 ;; Convert verse
1134 (goto-char (point-min))
1135 (while (search-forward "ORG-VERSE-START" nil t)
1136 (replace-match "\\begin{verse}" t t))
1137 (goto-char (point-min))
1138 (while (search-forward "ORG-VERSE-END" nil t)
1139 (replace-match "\\end{verse}" t t))
1141 ;; Convert horizontal rules
1142 (goto-char (point-min))
1143 (while (re-search-forward "^----+.$" nil t)
1144 (replace-match (org-export-latex-protect-string "\\hrule") t t))
1146 ;; Protect LaTeX commands like \commad[...]{...} or \command{...}
1147 (goto-char (point-min))
1148 (while (re-search-forward "\\\\[a-zA-Z]+\\(?:\\[.*\\]\\)?{.*}" nil t)
1149 (add-text-properties (match-beginning 0) (match-end 0)
1150 '(org-protected t)))
1152 ;; Protect LaTeX entities
1153 (goto-char (point-min))
1154 (while (re-search-forward (regexp-opt org-latex-entities) nil t)
1155 (add-text-properties (match-beginning 0) (match-end 0)
1156 '(org-protected t)))
1158 ;; Replace radio links
1159 (goto-char (point-min))
1160 (while (re-search-forward
1161 (concat "<<<?" org-export-latex-all-targets-re
1162 ">>>?\\((INVISIBLE)\\)?") nil t)
1163 (replace-match
1164 (org-export-latex-protect-string
1165 (format "\\label{%s}%s"(match-string 1)
1166 (if (match-string 2) "" (match-string 1)))) t t))
1168 ;; Delete @<...> constructs
1169 ;; Thanks to Daniel Clemente for this regexp
1170 (goto-char (point-min))
1171 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
1172 (replace-match ""))
1174 ;; When converting to LaTeX, replace footnotes
1175 ;; FIXME: don't protect footnotes from conversion
1176 (when (plist-get org-export-latex-options-plist :footnotes)
1177 (goto-char (point-min))
1178 (while (re-search-forward "\\[[0-9]+\\]" nil t)
1179 (when (save-match-data
1180 (save-excursion (beginning-of-line)
1181 (looking-at "[^:|#]")))
1182 (let ((foot-beg (match-beginning 0))
1183 (foot-end (match-end 0))
1184 (foot-prefix (match-string 0))
1185 footnote footnote-rpl)
1186 (save-excursion
1187 (when (search-forward foot-prefix nil t)
1188 (replace-match "")
1189 (let ((end (save-excursion
1190 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
1191 (match-beginning 0) (point-max)))))
1192 (setq footnote (concat (org-trim (buffer-substring (point) end))
1193 " ")) ; prevent last } being part of a link
1194 (delete-region (point) end))
1195 (goto-char foot-beg)
1196 (delete-region foot-beg foot-end)
1197 (unless (null footnote)
1198 (setq footnote-rpl (format "\\footnote{%s}" footnote))
1199 (add-text-properties 0 10 '(org-protected t) footnote-rpl)
1200 (add-text-properties (1- (length footnote-rpl))
1201 (length footnote-rpl)
1202 '(org-protected t) footnote-rpl)
1203 (insert footnote-rpl)))))))
1205 ;; Replace footnote section tag for LaTeX
1206 (goto-char (point-min))
1207 (while (re-search-forward
1208 (concat "^" footnote-section-tag-regexp) nil t)
1209 (replace-match ""))))
1211 ;;; List handling:
1213 (defun org-export-latex-lists ()
1214 "Replace plain text lists in current buffer into LaTeX lists."
1215 "Convert lists to LaTeX."
1216 (goto-char (point-min))
1217 (while (re-search-forward org-export-latex-list-beginning-re nil t)
1218 (beginning-of-line)
1219 (insert (org-list-to-latex (org-list-parse-list t)) "\n")))
1221 (defun org-list-parse-list (&optional delete)
1222 "Parse the list at point.
1223 Return a list containing first level items as strings and
1224 sublevels as a list of strings."
1225 (let ((start (org-list-item-begin))
1226 (end (org-list-end))
1227 output itemsep)
1228 (while (re-search-forward org-export-latex-list-beginning-re end t)
1229 (setq itemsep (if (save-match-data
1230 (string-match "^[0-9]" (match-string 2)))
1231 "[0-9]+\\(?:\\.\\|)\\)" "[-+]"))
1232 (let* ((indent1 (match-string 1))
1233 (nextitem (save-excursion
1234 (save-match-data
1235 (or (and (re-search-forward
1236 (concat "^" indent1 itemsep " *?") end t)
1237 (match-beginning 0)) end))))
1238 (item (buffer-substring
1239 (point)
1240 (or (and (re-search-forward
1241 org-export-latex-list-beginning-re end t)
1242 (goto-char (match-beginning 0)))
1243 (goto-char end))))
1244 (nextindent (match-string 1))
1245 (item (org-trim item))
1246 (item (if (string-match "^\\[.+\\]" item)
1247 (replace-match "\\\\texttt{\\&}"
1248 t nil item) item)))
1249 (push item output)
1250 (when (> (length nextindent)
1251 (length indent1))
1252 (narrow-to-region (point) nextitem)
1253 (push (org-list-parse-list) output)
1254 (widen))))
1255 (when delete (delete-region start end))
1256 (setq output (nreverse output))
1257 (push (if (string-match "^\\[0" itemsep)
1258 'ordered 'unordered) output)))
1260 (defun org-list-item-begin ()
1261 "Find the beginning of the list item and return its position."
1262 (save-excursion
1263 (if (not (or (looking-at org-export-latex-list-beginning-re)
1264 (re-search-backward
1265 org-export-latex-list-beginning-re nil t)))
1266 (progn (goto-char (point-min)) (point))
1267 (match-beginning 0))))
1269 (defun org-list-end ()
1270 "Find the end of the list and return its position."
1271 (save-excursion
1272 (catch 'exit
1273 (while (or (looking-at org-export-latex-list-beginning-re)
1274 (looking-at "^[ \t]+\\|^$"))
1275 (if (eq (point) (point-max))
1276 (throw 'exit (point-max)))
1277 (forward-line 1))) (point)))
1279 (defun org-list-insert-radio-list ()
1280 "Insert a radio list template appropriate for this major mode."
1281 (interactive)
1282 (let* ((e (assq major-mode org-list-radio-list-templates))
1283 (txt (nth 1 e))
1284 name pos)
1285 (unless e (error "No radio list setup defined for %s" major-mode))
1286 (setq name (read-string "List name: "))
1287 (while (string-match "%n" txt)
1288 (setq txt (replace-match name t t txt)))
1289 (or (bolp) (insert "\n"))
1290 (setq pos (point))
1291 (insert txt)
1292 (goto-char pos)))
1294 (defun org-list-send-list (&optional maybe)
1295 "Send a tranformed version of this list to the receiver position.
1296 With argument MAYBE, fail quietly if no transformation is defined for
1297 this list."
1298 (interactive)
1299 (catch 'exit
1300 (unless (org-at-item-p) (error "Not at a list"))
1301 (save-excursion
1302 (goto-char (org-list-item-begin))
1303 (beginning-of-line 0)
1304 (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
1305 (if maybe
1306 (throw 'exit nil)
1307 (error "Don't know how to transform this list"))))
1308 (let* ((name (match-string 1))
1310 (transform (intern (match-string 2)))
1311 (txt (buffer-substring-no-properties
1312 (org-list-item-begin)
1313 (org-list-end)))
1314 (list (org-list-parse-list)))
1315 (unless (fboundp transform)
1316 (error "No such transformation function %s" transform))
1317 (setq txt (funcall transform list))
1318 ;; Find the insertion place
1319 (save-excursion
1320 (goto-char (point-min))
1321 (unless (re-search-forward
1322 (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
1323 (error "Don't know where to insert translated list"))
1324 (goto-char (match-beginning 0))
1325 (beginning-of-line 2)
1326 (setq beg (point))
1327 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
1328 (error "Cannot find end of insertion region"))
1329 (beginning-of-line 1)
1330 (delete-region beg (point))
1331 (goto-char beg)
1332 (insert txt "\n"))
1333 (message "List converted and installed at receiver location"))))
1335 (defun org-list-to-generic (list params)
1336 "Convert a LIST parsed through `org-list-parse-list' to other formats.
1338 Valid parameters are
1340 :ustart String to start an unordered list
1341 :uend String to end an unordered list
1343 :ostart String to start an ordered list
1344 :oend String to end an ordered list
1346 :splice When set to t, return only list body lines, don't wrap
1347 them into :[u/o]start and :[u/o]end. Default is nil.
1349 :istart String to start a list item
1350 :iend String to end a list item
1351 :isep String to separate items
1352 :lsep String to separate sublists"
1353 (interactive)
1354 (let* ((p params) sublist
1355 (splicep (plist-get p :splice))
1356 (ostart (plist-get p :ostart))
1357 (oend (plist-get p :oend))
1358 (ustart (plist-get p :ustart))
1359 (uend (plist-get p :uend))
1360 (istart (plist-get p :istart))
1361 (iend (plist-get p :iend))
1362 (isep (plist-get p :isep))
1363 (lsep (plist-get p :lsep)))
1364 (let ((wrapper
1365 (cond ((eq (car list) 'ordered)
1366 (concat ostart "\n%s" oend "\n"))
1367 ((eq (car list) 'unordered)
1368 (concat ustart "\n%s" uend "\n"))))
1369 rtn)
1370 (while (setq sublist (pop list))
1371 (cond ((symbolp sublist) nil)
1372 ((stringp sublist)
1373 (setq rtn (concat rtn istart sublist iend isep)))
1375 (setq rtn (concat rtn ;; previous list
1376 lsep ;; list separator
1377 (org-list-to-generic sublist p)
1378 lsep ;; list separator
1379 )))))
1380 (format wrapper rtn))))
1382 (defun org-list-to-latex (list)
1383 "Convert LIST into a LaTeX list."
1384 (org-list-to-generic
1385 list '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
1386 :ustart "\\begin{itemize}" :uend "\\end{itemize}"
1387 :istart "\\item " :iend ""
1388 :isep "\n" :lsep "\n")))
1390 (defun org-list-to-html (list)
1391 "Convert LIST into a HTML list."
1392 (org-list-to-generic
1393 list '(:splicep nil :ostart "<ol>" :oend "</ol>"
1394 :ustart "<ul>" :uend "</ul>"
1395 :istart "<li>" :iend "</li>"
1396 :isep "\n" :lsep "\n")))
1398 (defun org-list-to-texinfo (list)
1399 "Convert LIST into a Texinfo list."
1400 (org-list-to-generic
1401 list '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
1402 :ustart "@enumerate" :uend "@end enumerate"
1403 :istart "@item\n" :iend ""
1404 :isep "\n" :lsep "\n")))
1406 (defconst org-latex-entities
1407 '("\\!"
1408 "\\'"
1409 "\\+"
1410 "\\,"
1411 "\\-"
1412 "\\:"
1413 "\\;"
1414 "\\<"
1415 "\\="
1416 "\\>"
1417 "\\Huge"
1418 "\\LARGE"
1419 "\\Large"
1420 "\\Styles"
1421 "\\\\"
1422 "\\`"
1423 "\\addcontentsline"
1424 "\\address"
1425 "\\addtocontents"
1426 "\\addtocounter"
1427 "\\addtolength"
1428 "\\addvspace"
1429 "\\alph"
1430 "\\appendix"
1431 "\\arabic"
1432 "\\author"
1433 "\\begin{array}"
1434 "\\begin{center}"
1435 "\\begin{description}"
1436 "\\begin{enumerate}"
1437 "\\begin{eqnarray}"
1438 "\\begin{equation}"
1439 "\\begin{figure}"
1440 "\\begin{flushleft}"
1441 "\\begin{flushright}"
1442 "\\begin{itemize}"
1443 "\\begin{list}"
1444 "\\begin{minipage}"
1445 "\\begin{picture}"
1446 "\\begin{quotation}"
1447 "\\begin{quote}"
1448 "\\begin{tabbing}"
1449 "\\begin{table}"
1450 "\\begin{tabular}"
1451 "\\begin{thebibliography}"
1452 "\\begin{theorem}"
1453 "\\begin{titlepage}"
1454 "\\begin{verbatim}"
1455 "\\begin{verse}"
1456 "\\bf"
1457 "\\bf"
1458 "\\bibitem"
1459 "\\bigskip"
1460 "\\cdots"
1461 "\\centering"
1462 "\\circle"
1463 "\\cite"
1464 "\\cleardoublepage"
1465 "\\clearpage"
1466 "\\cline"
1467 "\\closing"
1468 "\\dashbox"
1469 "\\date"
1470 "\\ddots"
1471 "\\dotfill"
1472 "\\em"
1473 "\\fbox"
1474 "\\flushbottom"
1475 "\\fnsymbol"
1476 "\\footnote"
1477 "\\footnotemark"
1478 "\\footnotesize"
1479 "\\footnotetext"
1480 "\\frac"
1481 "\\frame"
1482 "\\framebox"
1483 "\\hfill"
1484 "\\hline"
1485 "\\hrulespace"
1486 "\\hspace"
1487 "\\huge"
1488 "\\hyphenation"
1489 "\\include"
1490 "\\includeonly"
1491 "\\indent"
1492 "\\input"
1493 "\\it"
1494 "\\kill"
1495 "\\label"
1496 "\\large"
1497 "\\ldots"
1498 "\\line"
1499 "\\linebreak"
1500 "\\linethickness"
1501 "\\listoffigures"
1502 "\\listoftables"
1503 "\\location"
1504 "\\makebox"
1505 "\\maketitle"
1506 "\\mark"
1507 "\\mbox"
1508 "\\medskip"
1509 "\\multicolumn"
1510 "\\multiput"
1511 "\\newcommand"
1512 "\\newcounter"
1513 "\\newenvironment"
1514 "\\newfont"
1515 "\\newlength"
1516 "\\newline"
1517 "\\newpage"
1518 "\\newsavebox"
1519 "\\newtheorem"
1520 "\\nocite"
1521 "\\nofiles"
1522 "\\noindent"
1523 "\\nolinebreak"
1524 "\\nopagebreak"
1525 "\\normalsize"
1526 "\\onecolumn"
1527 "\\opening"
1528 "\\oval"
1529 "\\overbrace"
1530 "\\overline"
1531 "\\pagebreak"
1532 "\\pagenumbering"
1533 "\\pageref"
1534 "\\pagestyle"
1535 "\\par"
1536 "\\parbox"
1537 "\\put"
1538 "\\raggedbottom"
1539 "\\raggedleft"
1540 "\\raggedright"
1541 "\\raisebox"
1542 "\\ref"
1543 "\\rm"
1544 "\\roman"
1545 "\\rule"
1546 "\\savebox"
1547 "\\sc"
1548 "\\scriptsize"
1549 "\\setcounter"
1550 "\\setlength"
1551 "\\settowidth"
1552 "\\sf"
1553 "\\shortstack"
1554 "\\signature"
1555 "\\sl"
1556 "\\small"
1557 "\\smallskip"
1558 "\\sqrt"
1559 "\\tableofcontents"
1560 "\\telephone"
1561 "\\thanks"
1562 "\\thispagestyle"
1563 "\\tiny"
1564 "\\title"
1565 "\\tt"
1566 "\\twocolumn"
1567 "\\typein"
1568 "\\typeout"
1569 "\\underbrace"
1570 "\\underline"
1571 "\\usebox"
1572 "\\usecounter"
1573 "\\value"
1574 "\\vdots"
1575 "\\vector"
1576 "\\verb"
1577 "\\vfill"
1578 "\\vline"
1579 "\\vspace")
1580 "A list of LaTeX commands to be protected when performing conversion.")
1582 (provide 'org-export-latex)
1584 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
1586 ;;; org-export-latex.el ends here