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