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