Changelog entry for org-gnus patch.
[org-mode/org-tableheadings.git] / lisp / org-export-latex.el
blobb23340137dd8604778f34a78c0406a59febdc57c
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.14trans
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 ("=" "\\verb|%s|" nil)
167 ("~" "\\verb|%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 (remove-text-properties (point-min) (point-max)
361 '(:org-license-to-kill nil))
362 (org-update-radio-target-regexp)
363 (org-export-latex-set-initial-vars ext-plist arg)
364 (let* ((wcf (current-window-configuration))
365 (opt-plist org-export-latex-options-plist)
366 (region-p (org-region-active-p))
367 (rbeg (and region-p (region-beginning)))
368 (rend (and region-p (region-end)))
369 (subtree-p
370 (when region-p
371 (save-excursion
372 (goto-char rbeg)
373 (and (org-at-heading-p)
374 (>= (org-end-of-subtree t t) rend)))))
375 (opt-plist (if subtree-p
376 (org-export-add-subtree-options opt-plist rbeg)
377 opt-plist))
378 ;; Make sure the variable contains the updated values.
379 (org-export-latex-options-plist opt-plist)
380 (title (or (and subtree-p (org-export-get-title-from-subtree))
381 (plist-get opt-plist :title)
382 (and (not
383 (plist-get opt-plist :skip-before-1st-heading))
384 (org-export-grab-title-from-buffer))
385 (file-name-sans-extension
386 (file-name-nondirectory buffer-file-name))))
387 (filename (concat (file-name-as-directory
388 (or pub-dir
389 (org-export-directory :LaTeX ext-plist)))
390 (file-name-sans-extension
391 (or (and subtree-p
392 (org-entry-get rbeg "EXPORT_FILE_NAME" t))
393 (file-name-nondirectory ;sans-extension
394 buffer-file-name)))
395 ".tex"))
396 (filename (if (equal (file-truename filename)
397 (file-truename buffer-file-name))
398 (concat filename ".tex")
399 filename))
400 (buffer (if to-buffer
401 (cond
402 ((eq to-buffer 'string) (get-buffer-create
403 "*Org LaTeX Export*"))
404 (t (get-buffer-create to-buffer)))
405 (find-file-noselect filename)))
406 (odd org-odd-levels-only)
407 (header (org-export-latex-make-header title opt-plist))
408 (skip (cond (subtree-p nil)
409 (region-p t)
410 ;; never skip first lines when exporting a subtree
411 (t (plist-get opt-plist :skip-before-1st-heading))))
412 (text (plist-get opt-plist :text))
413 (first-lines (if skip "" (org-export-latex-first-lines rbeg)))
414 (coding-system (and (boundp 'buffer-file-coding-system)
415 buffer-file-coding-system))
416 (coding-system-for-write (or org-export-latex-coding-system
417 coding-system))
418 (save-buffer-coding-system (or org-export-latex-coding-system
419 coding-system))
420 (region (buffer-substring
421 (if region-p (region-beginning) (point-min))
422 (if region-p (region-end) (point-max))))
423 (string-for-export
424 (org-export-preprocess-string
425 region
426 :emph-multiline t
427 :for-LaTeX t
428 :comments nil
429 :tags (plist-get opt-plist :tags)
430 :priority (plist-get opt-plist :priority)
431 :todo-keywords (plist-get opt-plist :todo-keywords)
432 :add-text (if (eq to-buffer 'string) nil text)
433 :skip-before-1st-heading skip
434 :select-tags (plist-get opt-plist :select-tags)
435 :exclude-tags (plist-get opt-plist :exclude-tags)
436 :LaTeX-fragments nil)))
438 (set-buffer buffer)
439 (erase-buffer)
441 (and (fboundp 'set-buffer-file-coding-system)
442 (set-buffer-file-coding-system coding-system-for-write))
444 ;; insert the header and initial document commands
445 (unless (or (eq to-buffer 'string) body-only)
446 (insert header))
448 ;; insert text found in #+TEXT
449 (when (and text (not (eq to-buffer 'string)))
450 (insert (org-export-latex-content
451 text '(lists tables fixed-width keywords))
452 "\n\n"))
454 ;; insert lines before the first headline
455 (unless (or skip (eq to-buffer 'string))
456 (insert first-lines))
458 ;; handle the case where the region does not begin with a section
459 (when region-p
460 (insert (with-temp-buffer
461 (insert string-for-export)
462 (org-export-latex-first-lines))))
464 ;; export the content of headlines
465 (org-export-latex-global
466 (with-temp-buffer
467 (insert string-for-export)
468 (goto-char (point-min))
469 (when (re-search-forward "^\\(\\*+\\) " nil t)
470 (let* ((asters (length (match-string 1)))
471 (level (if odd (- asters 2) (- asters 1))))
472 (setq org-export-latex-add-level
473 (if odd (1- (/ (1+ asters) 2)) (1- asters)))
474 (org-export-latex-parse-global level odd)))))
476 ;; finalization
477 (unless body-only (insert "\n\\end{document}"))
478 (or to-buffer (save-buffer))
479 (goto-char (point-min))
480 (message "Exporting to LaTeX...done")
481 (prog1
482 (if (eq to-buffer 'string)
483 (prog1 (buffer-substring (point-min) (point-max))
484 (kill-buffer (current-buffer)))
485 (current-buffer))
486 (set-window-configuration wcf))))
488 ;;;###autoload
489 (defun org-export-as-pdf (arg &optional hidden ext-plist
490 to-buffer body-only pub-dir)
491 "Export as LaTeX, then process through to PDF."
492 (interactive "P")
493 (message "Exporting to PDF...")
494 (let* ((wconfig (current-window-configuration))
495 (lbuf (org-export-as-latex arg hidden ext-plist
496 to-buffer body-only pub-dir))
497 (file (buffer-file-name lbuf))
498 (base (file-name-sans-extension (buffer-file-name lbuf)))
499 (pdffile (concat base ".pdf")))
500 (and (file-exists-p pdffile) (delete-file pdffile))
501 (message "Processing LaTeX file...")
502 (shell-command (format "pdflatex -interaction nonstopmode %s"
503 (shell-quote-argument file)))
504 (shell-command (format "pdflatex -interaction nonstopmode %s"
505 (shell-quote-argument file)))
506 (message "Processing LaTeX file...done")
507 (if (not (file-exists-p pdffile))
508 (error "PDF file was not produced")
509 (set-window-configuration wconfig)
510 (when org-export-pdf-remove-logfiles
511 (dolist (ext '("aux" "log" "out" "toc"))
512 (setq file (concat base "." ext))
513 (and (file-exists-p file) (delete-file file))))
514 (message "Exporting to PDF...done")
515 pdffile)))
517 ;;;###autoload
518 (defun org-export-as-pdf-and-open (arg)
519 "Export as LaTeX, then process through to PDF, and open."
520 (interactive "P")
521 (let ((pdffile (org-export-as-pdf arg)))
522 (if pdffile
523 (org-open-file pdffile)
524 (error "PDF file was not produced"))))
526 ;;; Parsing functions:
528 (defun org-export-latex-parse-global (level odd)
529 "Parse the current buffer recursively, starting at LEVEL.
530 If ODD is non-nil, assume the buffer only contains odd sections.
531 Return a list reflecting the document structure."
532 (save-excursion
533 (goto-char (point-min))
534 (let* ((cnt 0) output
535 (depth org-export-latex-sectioning-depth))
536 (while (re-search-forward
537 (concat "^\\(\\(?:\\*\\)\\{"
538 (number-to-string (+ (if odd 2 1) level))
539 "\\}\\) \\(.*\\)$")
540 ;; make sure that there is no upper heading
541 (when (> level 0)
542 (save-excursion
543 (save-match-data
544 (re-search-forward
545 (concat "^\\(\\(?:\\*\\)\\{"
546 (number-to-string level)
547 "\\}\\) \\(.*\\)$") nil t)))) t)
548 (setq cnt (1+ cnt))
549 (let* ((pos (match-beginning 0))
550 (heading (match-string 2))
551 (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
552 (save-excursion
553 (narrow-to-region
554 (point)
555 (save-match-data
556 (if (re-search-forward
557 (concat "^\\(\\(?:\\*\\)\\{"
558 (number-to-string (+ (if odd 2 1) level))
559 "\\}\\) \\(.*\\)$") nil t)
560 (match-beginning 0)
561 (point-max))))
562 (goto-char (point-min))
563 (setq output
564 (append output
565 (list
566 (list
567 `(pos . ,pos)
568 `(level . ,nlevel)
569 `(occur . ,cnt)
570 `(heading . ,heading)
571 `(content . ,(org-export-latex-parse-content))
572 `(subcontent . ,(org-export-latex-parse-subcontent
573 level odd)))))))
574 (widen)))
575 (list output))))
577 (defun org-export-latex-parse-content ()
578 "Extract the content of a section."
579 (let ((beg (point))
580 (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t)
581 (progn (beginning-of-line) (point))
582 (point-max))))
583 (buffer-substring beg end)))
585 (defun org-export-latex-parse-subcontent (level odd)
586 "Extract the subcontent of a section at LEVEL.
587 If ODD Is non-nil, assume subcontent only contains odd sections."
588 (if (not (re-search-forward
589 (concat "^\\(\\(?:\\*\\)\\{"
590 (number-to-string (+ (if odd 4 2) level))
591 "\\}\\) \\(.*\\)$")
592 nil t))
593 nil ; subcontent is nil
594 (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
596 ;;; Rendering functions:
597 (defun org-export-latex-global (content)
598 "Export CONTENT to LaTeX.
599 CONTENT is an element of the list produced by
600 `org-export-latex-parse-global'."
601 (if (eq (car content) 'subcontent)
602 (mapc 'org-export-latex-sub (cdr content))
603 (org-export-latex-sub (car content))))
605 (defun org-export-latex-sub (subcontent)
606 "Export the list SUBCONTENT to LaTeX.
607 SUBCONTENT is an alist containing information about the headline
608 and its content."
609 (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
610 (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
612 (defun org-export-latex-subcontent (subcontent num)
613 "Export each cell of SUBCONTENT to LaTeX.
614 If NUM, export sections as numerical sections."
615 (let* ((heading (org-export-latex-fontify-headline
616 (cdr (assoc 'heading subcontent))))
617 (level (- (cdr (assoc 'level subcontent))
618 org-export-latex-add-level))
619 (occur (number-to-string (cdr (assoc 'occur subcontent))))
620 (content (cdr (assoc 'content subcontent)))
621 (subcontent (cadr (assoc 'subcontent subcontent)))
622 (label (org-get-text-property-any 0 'target heading))
623 (label-list (cons label (cdr (assoc label
624 org-export-target-aliases)))))
625 (cond
626 ;; Normal conversion
627 ((<= level org-export-latex-sectioning-depth)
628 (let* ((sec (nth (1- level) org-export-latex-sectioning))
629 start end)
630 (if (consp (cdr sec))
631 (setq start (nth (if num 0 2) sec)
632 end (nth (if num 1 3) sec))
633 (setq start (if num (car sec) (cdr sec))))
634 (insert (format start heading) "\n")
635 (when label
636 (insert (mapconcat (lambda (l) (format "\\label{%s}" l))
637 label-list "\n") "\n"))
638 (insert (org-export-latex-content content))
639 (cond ((stringp subcontent) (insert subcontent))
640 ((listp subcontent) (org-export-latex-sub subcontent)))
641 (if end (insert end "\n"))))
642 ;; At a level under the hl option: we can drop this subsection
643 ((> level org-export-latex-sectioning-depth)
644 (cond ((eq org-export-latex-low-levels 'description)
645 (insert (format "\\begin{description}\n\n\\item[%s]%s\n\n"
646 heading
647 (if label (format "\\label{%s}" label) "")))
648 (insert (org-export-latex-content content))
649 (cond ((stringp subcontent) (insert subcontent))
650 ((listp subcontent) (org-export-latex-sub subcontent)))
651 (insert "\\end{description}\n"))
652 ((stringp org-export-latex-low-levels)
653 (insert (format org-export-latex-low-levels heading) "\n")
654 (when label (insert (format "\\label{%s}\n" label)))
655 (insert (org-export-latex-content content))
656 (cond ((stringp subcontent) (insert subcontent))
657 ((listp subcontent) (org-export-latex-sub subcontent)))))))))
659 ;;; Exporting internals:
660 (defun org-export-latex-set-initial-vars (ext-plist level)
661 "Store org local variables required for LaTeX export.
662 EXT-PLIST is an optional additional plist.
663 LEVEL indicates the default depth for export."
664 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
665 org-export-latex-all-targets-re
666 (org-make-target-link-regexp (org-all-targets))
667 org-export-latex-options-plist
668 (org-combine-plists (org-default-export-plist) ext-plist
669 (org-infile-export-plist))
670 org-export-latex-class
671 (save-excursion
672 (goto-char (point-min))
673 (if (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
674 (assoc (match-string 1) org-export-latex-classes))
675 (match-string 1)
676 org-export-latex-default-class))
677 org-export-latex-header
678 (cadr (assoc org-export-latex-class org-export-latex-classes))
679 org-export-latex-sectioning
680 (cddr (assoc org-export-latex-class org-export-latex-classes))
681 org-export-latex-sectioning-depth
682 (or level
683 (let ((hl-levels
684 (plist-get org-export-latex-options-plist :headline-levels))
685 (sec-depth (length org-export-latex-sectioning)))
686 (if (> hl-levels sec-depth) sec-depth hl-levels)))))
688 (defun org-export-latex-make-header (title opt-plist)
689 "Make the LaTeX header and return it as a string.
690 TITLE is the current title from the buffer or region.
691 OPT-PLIST is the options plist for current buffer."
692 (let ((toc (plist-get opt-plist :table-of-contents))
693 (author (plist-get opt-plist :author)))
694 (concat
695 (if (plist-get opt-plist :time-stamp-file)
696 (format-time-string "% Created %Y-%m-%d %a %H:%M\n"))
697 ;; insert LaTeX custom header
698 org-export-latex-header
699 "\n"
700 ;; insert information on LaTeX packages
701 (when org-export-latex-packages-alist
702 (mapconcat (lambda(p)
703 (if (equal "" (car p))
704 (format "\\usepackage{%s}" (cadr p))
705 (format "\\usepackage[%s]{%s}"
706 (car p) (cadr p))))
707 org-export-latex-packages-alist "\n"))
708 ;; insert additional commands in the header
709 (plist-get opt-plist :latex-header-extra)
710 org-export-latex-append-header
711 ;; insert the title
712 (format
713 "\n\n\\title{%s}\n"
714 ;; convert the title
715 (org-export-latex-content
716 title '(lists tables fixed-width keywords)))
717 ;; insert author info
718 (if (plist-get opt-plist :author-info)
719 (format "\\author{%s}\n"
720 (or author user-full-name))
721 (format "%%\\author{%s}\n"
722 (or author user-full-name)))
723 ;; insert the date
724 (format "\\date{%s}\n"
725 (format-time-string
726 (or (plist-get opt-plist :date)
727 org-export-latex-date-format)))
728 ;; beginning of the document
729 "\n\\begin{document}\n\n"
730 ;; insert the title command
731 (if (string-match "%s" org-export-latex-title-command)
732 (format org-export-latex-title-command title)
733 org-export-latex-title-command)
734 "\n\n"
735 ;; table of contents
736 (when (and org-export-with-toc
737 (plist-get opt-plist :section-numbers))
738 (cond ((numberp toc)
739 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
740 (min toc (plist-get opt-plist :headline-levels))))
741 (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
742 (plist-get opt-plist :headline-levels))))))))
744 (defun org-export-latex-first-lines (&optional beg)
745 "Export the first lines before first headline.
746 COMMENTS is either nil to replace them with the empty string or a
747 formatting string like %%%%s if we want to comment them out."
748 (save-excursion
749 (goto-char (or beg (point-min)))
750 (if (org-at-heading-p) (beginning-of-line 2))
751 (let* ((pt (point))
752 (end (if (and (re-search-forward "^\\*+ " nil t)
753 (not (eq pt (match-beginning 0))))
754 (goto-char (match-beginning 0))
755 (goto-char (point-max)))))
756 (prog1
757 (org-export-latex-content
758 (org-export-preprocess-string
759 (buffer-substring (point-min) end)
760 :for-LaTeX t
761 :emph-multiline t
762 :add-text nil
763 :comments nil
764 :skip-before-1st-heading nil
765 :LaTeX-fragments nil))
766 (add-text-properties (point-min) (max (point-min) (1- end))
767 '(:org-license-to-kill t))))))
769 (defun org-export-latex-content (content &optional exclude-list)
770 "Convert CONTENT string to LaTeX.
771 Don't perform conversions that are in EXCLUDE-LIST. Recognized
772 conversion types are: quotation-marks, emphasis, sub-superscript,
773 links, keywords, lists, tables, fixed-width"
774 (with-temp-buffer
775 (insert content)
776 (unless (memq 'quotation-marks exclude-list)
777 (org-export-latex-quotation-marks))
778 (unless (memq 'emphasis exclude-list)
779 (when (plist-get org-export-latex-options-plist :emphasize)
780 (org-export-latex-fontify)))
781 (unless (memq 'sub-superscript exclude-list)
782 (org-export-latex-special-chars
783 (plist-get org-export-latex-options-plist :sub-superscript)))
784 (unless (memq 'links exclude-list)
785 (org-export-latex-links))
786 (unless (memq 'keywords exclude-list)
787 (org-export-latex-keywords
788 (plist-get org-export-latex-options-plist :timestamps)))
789 (unless (memq 'lists exclude-list)
790 (org-export-latex-lists))
791 (unless (memq 'tables exclude-list)
792 (org-export-latex-tables
793 (plist-get org-export-latex-options-plist :tables)))
794 (unless (memq 'fixed-width exclude-list)
795 (org-export-latex-fixed-width
796 (plist-get org-export-latex-options-plist :fixed-width)))
797 ;; return string
798 (buffer-substring (point-min) (point-max))))
800 (defun org-export-latex-protect-string (s)
801 "Add the org-protected property to string S."
802 (add-text-properties 0 (length s) '(org-protected t) s) s)
804 (defun org-export-latex-protect-char-in-string (char-list string)
805 "Add org-protected text-property to char from CHAR-LIST in STRING."
806 (with-temp-buffer
807 (save-match-data
808 (insert string)
809 (goto-char (point-min))
810 (while (re-search-forward (regexp-opt char-list) nil t)
811 (add-text-properties (match-beginning 0)
812 (match-end 0) '(org-protected t)))
813 (buffer-string))))
815 (defun org-export-latex-keywords-maybe (remove-list)
816 "Maybe remove keywords depending on rules in REMOVE-LIST."
817 (goto-char (point-min))
818 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
819 (case-fold-search nil))
820 ;; convert TODO keywords
821 (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
822 (if (plist-get remove-list :todo)
823 (replace-match "")
824 (replace-match (format "\\texttt{%s}" (match-string 1)) t t)))
825 ;; convert priority string
826 (when (re-search-forward "\\[\\\\#.\\]" nil t)
827 (if (plist-get remove-list :priority)
828 (replace-match "")
829 (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))
830 ;; convert tags
831 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t)
832 (if (or (not org-export-with-tags)
833 (plist-get remove-list :tags))
834 (replace-match "")
835 (replace-match
836 (org-export-latex-protect-string
837 (format "\\texttt{%s}"
838 (save-match-data
839 (replace-regexp-in-string
840 "_" "\\\\_" (match-string 0)))))
841 t t)))))
843 (defun org-export-latex-fontify-headline (string)
844 "Fontify special words in STRING."
845 (with-temp-buffer
846 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
847 ;; the beginning of the buffer - inserting "\n" is safe here though.
848 (insert "\n" string)
849 (goto-char (point-min))
850 (when (plist-get org-export-latex-options-plist :emphasize)
851 (org-export-latex-fontify))
852 (org-export-latex-keywords-maybe
853 org-export-latex-remove-from-headlines)
854 (org-export-latex-special-chars
855 (plist-get org-export-latex-options-plist :sub-superscript))
856 (org-export-latex-links)
857 ; (org-trim (buffer-substring-no-properties (point-min) (point-max)))))
858 (org-trim (buffer-string))))
860 (defun org-export-latex-quotation-marks ()
861 "Export quotation marks depending on language conventions."
862 (let* ((lang (plist-get org-export-latex-options-plist :language))
863 (quote-rpl (if (equal lang "fr")
864 '(("\\(\\s-\\)\"" "«~")
865 ("\\(\\S-\\)\"" "~»")
866 ("\\(\\s-\\)'" "`"))
867 '(("\\(\\s-\\)\"" "``")
868 ("\\(\\S-\\)\"" "''")
869 ("\\(\\s-\\)'" "`")))))
870 (mapc (lambda(l) (goto-char (point-min))
871 (while (re-search-forward (car l) nil t)
872 (let ((rpl (concat (match-string 1) (cadr l))))
873 (org-export-latex-protect-string rpl)
874 (org-if-unprotected
875 (replace-match rpl t t))))) quote-rpl)))
877 (defun org-export-latex-special-chars (sub-superscript)
878 "Export special characters to LaTeX.
879 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
880 See the `org-export-latex.el' code for a complete conversion table."
881 (goto-char (point-min))
882 (mapc (lambda(c)
883 (goto-char (point-min))
884 (while (re-search-forward c nil t)
885 ;; Put the point where to check for org-protected
886 (unless (or (get-text-property (match-beginning 2) 'org-protected)
887 (org-at-table-p))
888 (cond ((member (match-string 2) '("\\$" "$"))
889 (if (equal (match-string 2) "\\$")
890 (replace-match (concat (match-string 1) "$"
891 (match-string 3)) t t)
892 (replace-match (concat (match-string 1) "\\$"
893 (match-string 3)) t t)))
894 ((member (match-string 2) '("&" "%" "#"))
895 (if (equal (match-string 1) "\\")
896 (replace-match (match-string 2) t t)
897 (replace-match (concat (match-string 1) "\\"
898 (match-string 2)) t t)))
899 ((equal (match-string 2) "...")
900 (replace-match
901 (concat (match-string 1)
902 (org-export-latex-protect-string "\\ldots{}")) t t))
903 ((equal (match-string 2) "~")
904 (cond ((equal (match-string 1) "\\") nil)
905 ((eq 'org-link (get-text-property 0 'face (match-string 2)))
906 (replace-match (concat (match-string 1) "\\~") t t))
907 (t (replace-match
908 (org-export-latex-protect-string
909 (concat (match-string 1) "\\~{}")) t t))))
910 ((member (match-string 2) '("{" "}"))
911 (unless (save-match-data (org-inside-LaTeX-fragment-p))
912 (if (equal (match-string 1) "\\")
913 (replace-match (match-string 2) t t)
914 (replace-match (concat (match-string 1) "\\"
915 (match-string 2)) t t)))))
916 (unless (save-match-data (org-inside-LaTeX-fragment-p))
917 (cond ((equal (match-string 2) "\\")
918 (replace-match (or (save-match-data
919 (org-export-latex-treat-backslash-char
920 (match-string 1)
921 (match-string 3))) "") t t))
922 ((member (match-string 2) '("_" "^"))
923 (replace-match (or (save-match-data
924 (org-export-latex-treat-sub-super-char
925 sub-superscript
926 (match-string 2)
927 (match-string 1)
928 (match-string 3))) "") t t)))))))
929 '("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
930 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
931 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
932 "\\(.\\|^\\)\\(&\\)"
933 "\\(.\\|^\\)\\(#\\)"
934 "\\(.\\|^\\)\\(%\\)"
935 "\\(.\\|^\\)\\({\\)"
936 "\\(.\\|^\\)\\(}\\)"
937 "\\(.\\|^\\)\\(~\\)"
938 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
939 ;; (?\< . "\\textless{}")
940 ;; (?\> . "\\textgreater{}")
943 (defun org-export-latex-treat-sub-super-char
944 (subsup char string-before string-after)
945 "Convert the \"_\" and \"^\" characters to LaTeX.
946 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
947 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
948 (cond ((equal string-before "\\")
949 (concat string-before char string-after))
950 ;; this is part of a math formula
951 ((and (string-match "\\S-+" string-before)
952 (string-match "\\S-+" string-after))
953 (cond ((eq 'org-link (get-text-property 0 'face char))
954 (concat string-before "\\" char string-after))
955 ((save-match-data (org-inside-LaTeX-fragment-p))
956 (if subsup
957 (cond ((eq 1 (length string-after))
958 (concat string-before char string-after))
959 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
960 (format "%s%s{%s}" string-before char
961 (match-string 1 string-after))))))
962 ((and (> (length string-after) 1)
963 (or (eq subsup t)
964 (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
965 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
966 (format "%s$%s{%s}$" string-before char
967 (if (> (match-end 1) (1+ (match-beginning 1)))
968 (concat "\\mathrm{" (match-string 1 string-after) "}")
969 (match-string 1 string-after))))
970 ((eq subsup t) (concat string-before "$" char string-after "$"))
971 (t (org-export-latex-protect-string
972 (concat string-before "\\" char "{}" string-after)))))
973 (t (org-export-latex-protect-string
974 (concat string-before "\\" char "{}" string-after)))))
976 (defun org-export-latex-treat-backslash-char (string-before string-after)
977 "Convert the \"$\" special character to LaTeX.
978 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
979 (cond ((member (list string-after) org-html-entities)
980 ;; backslash is part of a special entity (like "\alpha")
981 (concat string-before "$\\"
982 (or (cdar (member (list string-after) org-html-entities))
983 string-after) "$"))
984 ((and (not (string-match "^[ \n\t]" string-after))
985 (not (string-match "[ \t]\\'\\|^" string-before)))
986 ;; backslash is inside a word
987 (org-export-latex-protect-string
988 (concat string-before "\\textbackslash{}" string-after)))
989 ((not (or (equal string-after "")
990 (string-match "^[ \t\n]" string-after)))
991 ;; backslash might escape a character (like \#) or a user TeX
992 ;; macro (like \setcounter)
993 (org-export-latex-protect-string
994 (concat string-before "\\" string-after)))
995 ((and (string-match "^[ \t\n]" string-after)
996 (string-match "[ \t\n]\\'" string-before))
997 ;; backslash is alone, convert it to $\backslash$
998 (org-export-latex-protect-string
999 (concat string-before "\\textbackslash{}" string-after)))
1000 (t (org-export-latex-protect-string
1001 (concat string-before "\\textbackslash{}" string-after)))))
1003 (defun org-export-latex-keywords (timestamps)
1004 "Convert special keywords to LaTeX.
1005 Regexps are those from `org-export-latex-special-string-regexps'.
1006 If TIMESTAMPS, convert timestamps, otherwise delete them."
1007 (let ((rg org-export-latex-special-string-regexps) r)
1008 (while (setq r (pop rg))
1009 (goto-char (point-min))
1010 (while (re-search-forward (eval r) nil t)
1011 (if (not timestamps)
1012 (replace-match (format "\\\\texttt{%s}" (match-string 0)) t)
1013 (replace-match ""))))))
1015 (defun org-export-latex-fixed-width (opt)
1016 "When OPT is non-nil convert fixed-width sections to LaTeX."
1017 (goto-char (point-min))
1018 (while (re-search-forward "^[ \t]*:" nil t)
1019 (if opt
1020 (progn (goto-char (match-beginning 0))
1021 (insert "\\begin{verbatim}\n")
1022 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
1023 (replace-match (concat (match-string 1)
1024 (match-string 2)) t t)
1025 (forward-line))
1026 (insert "\\end{verbatim}\n\n"))
1027 (progn (goto-char (match-beginning 0))
1028 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
1029 (replace-match (concat "%" (match-string 1)
1030 (match-string 2)) t t)
1031 (forward-line))))))
1034 (defvar org-table-last-alignment) ; defined in org-table.el
1035 (declare-function orgtbl-to-latex "org-table" (table params) t)
1036 (defun org-export-latex-tables (insert)
1037 "Convert tables to LaTeX and INSERT it."
1038 (goto-char (point-min))
1039 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
1040 ;; FIXME really need to save-excursion?
1041 (save-excursion (org-table-align))
1042 (let* ((beg (org-table-begin))
1043 (end (org-table-end))
1044 (raw-table (buffer-substring beg end))
1045 fnum fields line lines olines gr colgropen line-fmt align
1046 caption label attr floatp)
1047 (if org-export-latex-tables-verbatim
1048 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1049 "\\end{verbatim}\n")))
1050 (apply 'delete-region (list beg end))
1051 (insert (org-export-latex-protect-string tbl)))
1052 (progn
1053 (setq caption (org-find-text-property-in-string
1054 'org-caption raw-table)
1055 attr (org-find-text-property-in-string
1056 'org-attributes raw-table)
1057 label (org-find-text-property-in-string
1058 'org-attributes raw-table)
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 (setq align (apply 'format
1094 (cons line-fmt
1095 (mapcar (lambda (x) (if x "r" "l"))
1096 org-table-last-alignment))))
1097 ;; prepare the table to send to orgtbl-to-latex
1098 (setq lines
1099 (mapcar
1100 (lambda(elem)
1101 (or (and (string-match "[ \t]*|-+" elem) 'hline)
1102 (split-string (org-trim elem) "|" t)))
1103 lines))
1104 (when insert
1105 (insert (org-export-latex-protect-string
1106 (concat
1107 (if floatp "\\begin{table}[htb]\n")
1108 (if floatp (format
1109 "\\caption{%s%s}\n"
1110 (if label (concat "\\\label{" label "}"))
1111 (or caption "")))
1112 "\\begin{center}\n"
1113 (orgtbl-to-latex
1114 lines `(:tstart ,(concat "\\begin{tabular}{" align "}")))
1115 "\n\\end{center}\n"
1116 (if floatp "\\end{table}")))
1117 "\n\n")))))))
1119 (defun org-export-latex-fontify ()
1120 "Convert fontification to LaTeX."
1121 (goto-char (point-min))
1122 (while (re-search-forward org-emph-re nil t)
1123 ;; The match goes one char after the *string*
1124 (let ((emph (assoc (match-string 3)
1125 org-export-latex-emphasis-alist))
1126 rpl)
1127 (unless (get-text-property (1- (point)) 'org-protected)
1128 (setq rpl (concat (match-string 1)
1129 (format (org-export-latex-protect-char-in-string
1130 '("\\" "{" "}") (cadr emph))
1131 (match-string 4))
1132 (match-string 5)))
1133 (if (caddr emph)
1134 (setq rpl (org-export-latex-protect-string rpl)))
1135 (replace-match rpl t t)))
1136 (backward-char)))
1138 (defun org-export-latex-links ()
1139 ;; Make sure to use the LaTeX hyperref and graphicx package
1140 ;; or send some warnings.
1141 "Convert links to LaTeX."
1142 (goto-char (point-min))
1143 (while (re-search-forward org-bracket-link-analytic-regexp nil t)
1144 (org-if-unprotected
1145 (goto-char (match-beginning 0))
1146 (let* ((re-radio org-export-latex-all-targets-re)
1147 (remove (list (match-beginning 0) (match-end 0)))
1148 (raw-path (org-extract-attributes (match-string 3)))
1149 (full-raw-path (concat (match-string 1) raw-path))
1150 (desc (match-string 5))
1151 (type (or (match-string 2)
1152 (if (or (file-name-absolute-p raw-path)
1153 (string-match "^\\.\\.?/" raw-path))
1154 "file")))
1155 (caption (org-find-text-property-in-string 'org-caption raw-path))
1156 (attr (org-find-text-property-in-string 'org-attributes raw-path))
1157 (label (org-find-text-property-in-string 'org-label raw-path))
1158 (floatp (or label caption))
1159 imgp radiop
1160 ;; define the path of the link
1161 (path (cond
1162 ((member type '("http" "https" "ftp"))
1163 (concat type ":" raw-path))
1164 ((and re-radio (string-match re-radio raw-path))
1165 (setq radiop t))
1166 ((equal type "mailto")
1167 (concat type ":" raw-path))
1168 ((equal type "file")
1169 (if (and (or (org-file-image-p (expand-file-name raw-path))
1170 (string-match "\\.\\(pdf\\|jpg\\|ps\\|eps\\)$"
1171 raw-path))
1172 (equal desc full-raw-path))
1173 (setq imgp t)
1174 (progn (when (string-match "\\(.+\\)::.+" raw-path)
1175 (setq raw-path (match-string 1 raw-path)))
1176 (if (file-exists-p raw-path)
1177 (concat type "://" (expand-file-name raw-path))
1178 (concat type "://" (org-export-directory
1179 :LaTeX org-export-latex-options-plist)
1180 raw-path))))))))
1181 ;; process with link inserting
1182 (apply 'delete-region remove)
1183 (cond ((and imgp (plist-get org-export-latex-options-plist :inline-images))
1184 (insert
1185 (concat
1186 (if floatp "\\begin{figure}[htb]\n")
1187 (format "\\centerline{\\includegraphics[%s]{%s}}\n"
1188 (or attr org-export-latex-image-default-option)
1189 (expand-file-name raw-path))
1190 (if floatp
1191 (format "\\caption{%s%s}\n"
1192 (if label (concat "\\label{" label "}"))
1193 (or caption "")))
1194 (if floatp "\\end{figure}\n"))))
1195 (radiop (insert (format "\\hyperref[%s]{%s}"
1196 (org-solidify-link-text raw-path) desc)))
1197 ((not type)
1198 (insert (format "\\hyperref[%s]{%s}"
1199 (org-solidify-link-text raw-path) desc)))
1200 (path (insert (format "\\href{%s}{%s}" path desc)))
1201 (t (insert "\\texttt{" desc "}")))))))
1203 (defvar org-latex-entities) ; defined below
1204 (defvar org-latex-entities-regexp) ; defined below
1206 (defun org-export-latex-preprocess ()
1207 "Clean stuff in the LaTeX export."
1208 ;; Preserve line breaks
1209 (goto-char (point-min))
1210 (while (re-search-forward "\\\\\\\\" nil t)
1211 (add-text-properties (match-beginning 0) (match-end 0)
1212 '(org-protected t)))
1214 ;; Preserve latex environments
1215 (goto-char (point-min))
1216 (while (re-search-forward "^[ \t]*\\begin{\\([a-zA-Z]+\\)}" nil t)
1217 (let* ((start (progn (beginning-of-line) (point)))
1218 (end (or (and (re-search-forward
1219 (concat "^[ \t]*\\end{" (match-string 1) "}" nil t)
1220 (point-at-eol)))
1221 (point-max))))
1222 (add-text-properties start end '(org-protected t))))
1224 ;; Convert LaTeX to \LaTeX{}
1225 (goto-char (point-min))
1226 (let ((case-fold-search nil) rpl)
1227 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
1228 (replace-match (org-export-latex-protect-string
1229 (concat (match-string 1) "\\LaTeX{}")) t t)))
1231 ;; Convert blockquotes
1232 (goto-char (point-min))
1233 (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
1234 (replace-match "\\begin{quote}" t t))
1235 (goto-char (point-min))
1236 (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
1237 (replace-match "\\end{quote}" t t))
1239 ;; Convert verse
1240 (goto-char (point-min))
1241 (while (search-forward "ORG-VERSE-START" nil t)
1242 (replace-match "\\begin{verse}" t t))
1243 (goto-char (point-min))
1244 (while (search-forward "ORG-VERSE-END" nil t)
1245 (replace-match "\\end{verse}" t t))
1247 ;; Convert horizontal rules
1248 (goto-char (point-min))
1249 (while (re-search-forward "^----+.$" nil t)
1250 (replace-match (org-export-latex-protect-string "\\hrule") t t))
1252 ;; Protect LaTeX commands like \commad[...]{...} or \command{...}
1253 (goto-char (point-min))
1254 (while (re-search-forward "\\\\[a-zA-Z]+\\(?:\\[.*\\]\\)?{.*}" nil t)
1255 (add-text-properties (match-beginning 0) (match-end 0)
1256 '(org-protected t)))
1258 ;; Protect LaTeX entities
1259 (goto-char (point-min))
1260 (while (re-search-forward org-latex-entities-regexp nil t)
1261 (add-text-properties (match-beginning 0) (match-end 0)
1262 '(org-protected t)))
1264 ;; Replace radio links
1265 (goto-char (point-min))
1266 (while (re-search-forward
1267 (concat "<<<?" org-export-latex-all-targets-re
1268 ">>>?\\((INVISIBLE)\\)?") nil t)
1269 (replace-match
1270 (org-export-latex-protect-string
1271 (format "\\label{%s}%s" (save-match-data (org-solidify-link-text
1272 (match-string 1)))
1273 (if (match-string 2) "" (match-string 1)))) t t))
1275 ;; Delete @<...> constructs
1276 ;; Thanks to Daniel Clemente for this regexp
1277 (goto-char (point-min))
1278 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
1279 (replace-match ""))
1281 ;; When converting to LaTeX, replace footnotes
1282 ;; FIXME: don't protect footnotes from conversion
1283 (when (plist-get org-export-latex-options-plist :footnotes)
1284 (goto-char (point-min))
1285 (while (re-search-forward "\\[[0-9]+\\]" nil t)
1286 (when (save-match-data
1287 (save-excursion (beginning-of-line)
1288 (looking-at "[^:|#]")))
1289 (let ((foot-beg (match-beginning 0))
1290 (foot-end (match-end 0))
1291 (foot-prefix (match-string 0))
1292 footnote footnote-rpl)
1293 (save-excursion
1294 (when (search-forward foot-prefix nil t)
1295 (replace-match "")
1296 (let ((end (save-excursion
1297 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
1298 (match-beginning 0) (point-max)))))
1299 (setq footnote (concat (org-trim (buffer-substring (point) end))
1300 " ")) ; prevent last } being part of a link
1301 (delete-region (point) end))
1302 (goto-char foot-beg)
1303 (delete-region foot-beg foot-end)
1304 (unless (null footnote)
1305 (setq footnote-rpl (format "\\footnote{%s}" footnote))
1306 (add-text-properties 0 10 '(org-protected t) footnote-rpl)
1307 (add-text-properties (1- (length footnote-rpl))
1308 (length footnote-rpl)
1309 '(org-protected t) footnote-rpl)
1310 (insert footnote-rpl)))))))
1312 ;; Replace footnote section tag for LaTeX
1313 (goto-char (point-min))
1314 (while (re-search-forward
1315 (concat "^" footnote-section-tag-regexp) nil t)
1316 (replace-match ""))))
1318 ;;; List handling:
1320 (defun org-export-latex-lists ()
1321 "Replace plain text lists in current buffer into LaTeX lists."
1322 "Convert lists to LaTeX."
1323 (goto-char (point-min))
1324 (while (re-search-forward org-list-beginning-re nil t)
1325 (org-if-unprotected
1326 (beginning-of-line)
1327 (insert (org-list-to-latex (org-list-parse-list t)) "\n"))))
1329 (defconst org-latex-entities
1330 '("\\!"
1331 "\\'"
1332 "\\+"
1333 "\\,"
1334 "\\-"
1335 "\\:"
1336 "\\;"
1337 "\\<"
1338 "\\="
1339 "\\>"
1340 "\\Huge"
1341 "\\LARGE"
1342 "\\Large"
1343 "\\Styles"
1344 "\\\\"
1345 "\\`"
1346 "\\addcontentsline"
1347 "\\address"
1348 "\\addtocontents"
1349 "\\addtocounter"
1350 "\\addtolength"
1351 "\\addvspace"
1352 "\\alph"
1353 "\\appendix"
1354 "\\arabic"
1355 "\\author"
1356 "\\begin{array}"
1357 "\\begin{center}"
1358 "\\begin{description}"
1359 "\\begin{enumerate}"
1360 "\\begin{eqnarray}"
1361 "\\begin{equation}"
1362 "\\begin{figure}"
1363 "\\begin{flushleft}"
1364 "\\begin{flushright}"
1365 "\\begin{itemize}"
1366 "\\begin{list}"
1367 "\\begin{minipage}"
1368 "\\begin{picture}"
1369 "\\begin{quotation}"
1370 "\\begin{quote}"
1371 "\\begin{tabbing}"
1372 "\\begin{table}"
1373 "\\begin{tabular}"
1374 "\\begin{thebibliography}"
1375 "\\begin{theorem}"
1376 "\\begin{titlepage}"
1377 "\\begin{verbatim}"
1378 "\\begin{verse}"
1379 "\\bf"
1380 "\\bf"
1381 "\\bibitem"
1382 "\\bigskip"
1383 "\\cdots"
1384 "\\centering"
1385 "\\circle"
1386 "\\cite"
1387 "\\cleardoublepage"
1388 "\\clearpage"
1389 "\\cline"
1390 "\\closing"
1391 "\\dashbox"
1392 "\\date"
1393 "\\ddots"
1394 "\\dotfill"
1395 "\\em"
1396 "\\fbox"
1397 "\\flushbottom"
1398 "\\fnsymbol"
1399 "\\footnote"
1400 "\\footnotemark"
1401 "\\footnotesize"
1402 "\\footnotetext"
1403 "\\frac"
1404 "\\frame"
1405 "\\framebox"
1406 "\\hfill"
1407 "\\hline"
1408 "\\hrulespace"
1409 "\\hspace"
1410 "\\huge"
1411 "\\hyphenation"
1412 "\\include"
1413 "\\includeonly"
1414 "\\indent"
1415 "\\input"
1416 "\\it"
1417 "\\kill"
1418 "\\label"
1419 "\\large"
1420 "\\ldots"
1421 "\\line"
1422 "\\linebreak"
1423 "\\linethickness"
1424 "\\listoffigures"
1425 "\\listoftables"
1426 "\\location"
1427 "\\makebox"
1428 "\\maketitle"
1429 "\\mark"
1430 "\\mbox"
1431 "\\medskip"
1432 "\\multicolumn"
1433 "\\multiput"
1434 "\\newcommand"
1435 "\\newcounter"
1436 "\\newenvironment"
1437 "\\newfont"
1438 "\\newlength"
1439 "\\newline"
1440 "\\newpage"
1441 "\\newsavebox"
1442 "\\newtheorem"
1443 "\\nocite"
1444 "\\nofiles"
1445 "\\noindent"
1446 "\\nolinebreak"
1447 "\\nopagebreak"
1448 "\\normalsize"
1449 "\\onecolumn"
1450 "\\opening"
1451 "\\oval"
1452 "\\overbrace"
1453 "\\overline"
1454 "\\pagebreak"
1455 "\\pagenumbering"
1456 "\\pageref"
1457 "\\pagestyle"
1458 "\\par"
1459 "\\parbox"
1460 "\\put"
1461 "\\raggedbottom"
1462 "\\raggedleft"
1463 "\\raggedright"
1464 "\\raisebox"
1465 "\\ref"
1466 "\\rm"
1467 "\\roman"
1468 "\\rule"
1469 "\\savebox"
1470 "\\sc"
1471 "\\scriptsize"
1472 "\\setcounter"
1473 "\\setlength"
1474 "\\settowidth"
1475 "\\sf"
1476 "\\shortstack"
1477 "\\signature"
1478 "\\sl"
1479 "\\small"
1480 "\\smallskip"
1481 "\\sqrt"
1482 "\\tableofcontents"
1483 "\\telephone"
1484 "\\thanks"
1485 "\\thispagestyle"
1486 "\\tiny"
1487 "\\title"
1488 "\\tt"
1489 "\\twocolumn"
1490 "\\typein"
1491 "\\typeout"
1492 "\\underbrace"
1493 "\\underline"
1494 "\\usebox"
1495 "\\usecounter"
1496 "\\value"
1497 "\\vdots"
1498 "\\vector"
1499 "\\verb"
1500 "\\vfill"
1501 "\\vline"
1502 "\\vspace")
1503 "A list of LaTeX commands to be protected when performing conversion.")
1505 (defconst org-latex-entities-regexp
1506 (let (names rest)
1507 (dolist (x org-latex-entities)
1508 (if (string-match "[a-z][A-Z]$" x)
1509 (push x names)
1510 (push x rest)))
1511 (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
1512 "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
1514 (provide 'org-export-latex)
1516 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
1518 ;;; org-export-latex.el ends here