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