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