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