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