LaTeX export: Make quotes exported right before a footnote reference.
[org-mode.git] / lisp / org-export-latex.el
blobff3444feb0efd38e9626b228b7b62d2e940bb980
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.17trans
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 (save-excursion
692 (goto-char (point-min))
693 (if (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
694 (assoc (match-string 1) org-export-latex-classes))
695 (match-string 1)
696 org-export-latex-default-class))
697 org-export-latex-header
698 (cadr (assoc org-export-latex-class org-export-latex-classes))
699 org-export-latex-sectioning
700 (cddr (assoc org-export-latex-class org-export-latex-classes))
701 org-export-latex-sectioning-depth
702 (or level
703 (let ((hl-levels
704 (plist-get org-export-latex-options-plist :headline-levels))
705 (sec-depth (length org-export-latex-sectioning)))
706 (if (> hl-levels sec-depth) sec-depth hl-levels)))))
708 (defun org-export-latex-make-header (title opt-plist)
709 "Make the LaTeX header and return it as a string.
710 TITLE is the current title from the buffer or region.
711 OPT-PLIST is the options plist for current buffer."
712 (let ((toc (plist-get opt-plist :table-of-contents))
713 (author (plist-get opt-plist :author)))
714 (concat
715 (if (plist-get opt-plist :time-stamp-file)
716 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
717 ;; insert LaTeX custom header
718 org-export-latex-header
719 "\n"
720 ;; insert information on LaTeX packages
721 (when org-export-latex-packages-alist
722 (mapconcat (lambda(p)
723 (if (equal "" (car p))
724 (format "\\usepackage{%s}" (cadr p))
725 (format "\\usepackage[%s]{%s}"
726 (car p) (cadr p))))
727 org-export-latex-packages-alist "\n"))
728 ;; insert additional commands in the header
729 (plist-get opt-plist :latex-header-extra)
730 org-export-latex-append-header
731 ;; insert the title
732 (format
733 "\n\n\\title{%s}\n"
734 ;; convert the title
735 (org-export-latex-content
736 title '(lists tables fixed-width keywords)))
737 ;; insert author info
738 (if (plist-get opt-plist :author-info)
739 (format "\\author{%s}\n"
740 (or author user-full-name))
741 (format "%%\\author{%s}\n"
742 (or author user-full-name)))
743 ;; insert the date
744 (format "\\date{%s}\n"
745 (format-time-string
746 (or (plist-get opt-plist :date)
747 org-export-latex-date-format)))
748 ;; beginning of the document
749 "\n\\begin{document}\n\n"
750 ;; insert the title command
751 (if (string-match "%s" org-export-latex-title-command)
752 (format org-export-latex-title-command title)
753 org-export-latex-title-command)
754 "\n\n"
755 ;; table of contents
756 (when (and org-export-with-toc
757 (plist-get opt-plist :section-numbers))
758 (cond ((numberp toc)
759 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
760 (min toc (plist-get opt-plist :headline-levels))))
761 (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
762 (plist-get opt-plist :headline-levels))))))))
764 (defun org-export-latex-first-lines (&optional beg)
765 "Export the first lines before first headline.
766 If BEG is non-nil, the is the beginning of he region."
767 (save-excursion
768 (goto-char (or beg (point-min)))
769 (if (org-at-heading-p) (beginning-of-line 2))
770 (let* ((pt (point))
771 (end (if (re-search-forward "^\\*+ " nil t)
772 (goto-char (match-beginning 0))
773 (goto-char (point-max)))))
774 (prog1
775 (org-export-latex-content
776 (org-export-preprocess-string
777 (buffer-substring pt end)
778 :for-LaTeX t
779 :emph-multiline t
780 :add-text nil
781 :comments nil
782 :skip-before-1st-heading nil
783 :LaTeX-fragments nil))
784 (add-text-properties pt (max pt (1- end))
785 '(:org-license-to-kill t))))))
787 (defun org-export-latex-content (content &optional exclude-list)
788 "Convert CONTENT string to LaTeX.
789 Don't perform conversions that are in EXCLUDE-LIST. Recognized
790 conversion types are: quotation-marks, emphasis, sub-superscript,
791 links, keywords, lists, tables, fixed-width"
792 (with-temp-buffer
793 (insert content)
794 (unless (memq 'quotation-marks exclude-list)
795 (org-export-latex-quotation-marks))
796 (unless (memq 'emphasis exclude-list)
797 (when (plist-get org-export-latex-options-plist :emphasize)
798 (org-export-latex-fontify)))
799 (unless (memq 'sub-superscript exclude-list)
800 (org-export-latex-special-chars
801 (plist-get org-export-latex-options-plist :sub-superscript)))
802 (unless (memq 'links exclude-list)
803 (org-export-latex-links))
804 (unless (memq 'keywords exclude-list)
805 (org-export-latex-keywords))
806 (unless (memq 'lists exclude-list)
807 (org-export-latex-lists))
808 (unless (memq 'tables exclude-list)
809 (org-export-latex-tables
810 (plist-get org-export-latex-options-plist :tables)))
811 (unless (memq 'fixed-width exclude-list)
812 (org-export-latex-fixed-width
813 (plist-get org-export-latex-options-plist :fixed-width)))
814 ;; return string
815 (buffer-substring (point-min) (point-max))))
817 (defun org-export-latex-protect-string (s)
818 "Add the org-protected property to string S."
819 (add-text-properties 0 (length s) '(org-protected t) s) s)
821 (defun org-export-latex-protect-char-in-string (char-list string)
822 "Add org-protected text-property to char from CHAR-LIST in STRING."
823 (with-temp-buffer
824 (save-match-data
825 (insert string)
826 (goto-char (point-min))
827 (while (re-search-forward (regexp-opt char-list) nil t)
828 (add-text-properties (match-beginning 0)
829 (match-end 0) '(org-protected t)))
830 (buffer-string))))
832 (defun org-export-latex-keywords-maybe (&optional remove-list)
833 "Maybe remove keywords depending on rules in REMOVE-LIST."
834 (goto-char (point-min))
835 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
836 (case-fold-search nil))
837 ;; convert TODO keywords
838 (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
839 (if (plist-get remove-list :todo)
840 (replace-match "")
841 (replace-match (format "\\textbf{%s}" (match-string 1)) t t)))
842 ;; convert priority string
843 (when (re-search-forward "\\[\\\\#.\\]" nil t)
844 (if (plist-get remove-list :priority)
845 (replace-match "")
846 (replace-match (format "\\textbf{%s}" (match-string 0)) t t)))
847 ;; convert tags
848 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t)
849 (if (or (not org-export-with-tags)
850 (plist-get remove-list :tags))
851 (replace-match "")
852 (replace-match
853 (org-export-latex-protect-string
854 (format "\\textbf{%s}"
855 (save-match-data
856 (replace-regexp-in-string
857 "_" "\\\\_" (match-string 0)))))
858 t t)))))
860 (defun org-export-latex-fontify-headline (string)
861 "Fontify special words in STRING."
862 (with-temp-buffer
863 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
864 ;; the beginning of the buffer - inserting "\n" is safe here though.
865 (insert "\n" string)
866 (goto-char (point-min))
867 (when (plist-get org-export-latex-options-plist :emphasize)
868 (org-export-latex-fontify))
869 (org-export-latex-keywords-maybe)
870 (org-export-latex-special-chars
871 (plist-get org-export-latex-options-plist :sub-superscript))
872 (org-export-latex-links)
873 ; (org-trim (buffer-substring-no-properties (point-min) (point-max)))))
874 (org-trim (buffer-string))))
876 (defun org-export-latex-quotation-marks ()
877 "Export quotation marks depending on language conventions."
878 (let* ((lang (plist-get org-export-latex-options-plist :language))
879 (quote-rpl (if (equal lang "fr")
880 '(("\\(\\s-\\)\"" "«~")
881 ("\\(\\S-\\)\"" "~»")
882 ("\\(\\s-\\)'" "`"))
883 '(("\\(\\s-\\)\"" "``")
884 ("\\(\\S-\\)\"" "''")
885 ("\\(\\s-\\)'" "`")))))
886 (mapc (lambda(l) (goto-char (point-min))
887 (while (re-search-forward (car l) nil t)
888 (let ((rpl (concat (match-string 1) (cadr l))))
889 (org-export-latex-protect-string rpl)
890 (org-if-unprotected-1
891 (replace-match rpl t t))))) quote-rpl)))
893 (defun org-export-latex-special-chars (sub-superscript)
894 "Export special characters to LaTeX.
895 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
896 See the `org-export-latex.el' code for a complete conversion table."
897 (goto-char (point-min))
898 (mapc (lambda(c)
899 (goto-char (point-min))
900 (while (re-search-forward c nil t)
901 ;; Put the point where to check for org-protected
902 ; (unless (or (get-text-property (match-beginning 2) 'org-protected);
903 ; (org-at-table-p))
904 (unless (get-text-property (match-beginning 2) 'org-protected)
905 (cond ((member (match-string 2) '("\\$" "$"))
906 (if (equal (match-string 2) "\\$")
908 (replace-match "\\$" t t)))
909 ((member (match-string 2) '("&" "%" "#"))
910 (if (equal (match-string 1) "\\")
911 (replace-match (match-string 2) t t)
912 (replace-match (concat (match-string 1) "\\"
913 (match-string 2)) t t)))
914 ((equal (match-string 2) "...")
915 (replace-match
916 (concat (match-string 1)
917 (org-export-latex-protect-string "\\ldots{}")) t t))
918 ((equal (match-string 2) "~")
919 (cond ((equal (match-string 1) "\\") nil)
920 ((eq 'org-link (get-text-property 0 'face (match-string 2)))
921 (replace-match (concat (match-string 1) "\\~") t t))
922 (t (replace-match
923 (org-export-latex-protect-string
924 (concat (match-string 1) "\\~{}")) t t))))
925 ((member (match-string 2) '("{" "}"))
926 (unless (save-match-data (org-inside-latex-math-p))
927 (if (equal (match-string 1) "\\")
928 (replace-match (match-string 2) t t)
929 (replace-match (concat (match-string 1) "\\"
930 (match-string 2)) t t)))))
931 (unless (save-match-data (org-inside-latex-math-p))
932 (cond ((equal (match-string 2) "\\")
933 (replace-match (or (save-match-data
934 (org-export-latex-treat-backslash-char
935 (match-string 1)
936 (or (match-string 3) "")))
937 "") t t))
938 ((member (match-string 2) '("_" "^"))
939 (replace-match (or (save-match-data
940 (org-export-latex-treat-sub-super-char
941 sub-superscript
942 (match-string 2)
943 (match-string 1)
944 (match-string 3))) "") t t)))))))
945 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
946 "\\(\\(\\\\?\\$\\)\\)"
947 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
948 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
949 "\\(.\\|^\\)\\(&\\)"
950 "\\(.\\|^\\)\\(#\\)"
951 "\\(.\\|^\\)\\(%\\)"
952 "\\(.\\|^\\)\\({\\)"
953 "\\(.\\|^\\)\\(}\\)"
954 "\\(.\\|^\\)\\(~\\)"
955 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
956 ;; (?\< . "\\textless{}")
957 ;; (?\> . "\\textgreater{}")
960 (defun org-inside-latex-math-p ()
961 (get-text-property (point) 'org-latex-math))
963 (defun org-export-latex-treat-sub-super-char
964 (subsup char string-before string-after)
965 "Convert the \"_\" and \"^\" characters to LaTeX.
966 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
967 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
968 (cond ((equal string-before "\\")
969 (concat string-before char string-after))
970 ;; this is part of a math formula
971 ((and (string-match "\\S-+" string-before)
972 (string-match "\\S-+" string-after))
973 (cond ((eq 'org-link (get-text-property 0 'face char))
974 (concat string-before "\\" char string-after))
975 ((save-match-data (org-inside-latex-math-p))
976 (if subsup
977 (cond ((eq 1 (length string-after))
978 (concat string-before char string-after))
979 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
980 (format "%s%s{%s}" string-before char
981 (match-string 1 string-after))))))
982 ((and (> (length string-after) 1)
983 (or (eq subsup t)
984 (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
985 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
986 (format "%s$%s{%s}$" string-before char
987 (if (> (match-end 1) (1+ (match-beginning 1)))
988 (concat "\\mathrm{" (match-string 1 string-after) "}")
989 (match-string 1 string-after))))
990 ((eq subsup t) (concat string-before "$" char string-after "$"))
991 (t (org-export-latex-protect-string
992 (concat string-before "\\" char "{}" string-after)))))
993 (t (org-export-latex-protect-string
994 (concat string-before "\\" char "{}" string-after)))))
996 (defun org-export-latex-treat-backslash-char (string-before string-after)
997 "Convert the \"$\" special character to LaTeX.
998 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
999 (cond ((member (list string-after) org-html-entities)
1000 ;; backslash is part of a special entity (like "\alpha")
1001 (concat string-before "$\\"
1002 (or (cdar (member (list string-after) org-html-entities))
1003 string-after) "$"))
1004 ((and (not (string-match "^[ \n\t]" string-after))
1005 (not (string-match "[ \t]\\'\\|^" string-before)))
1006 ;; backslash is inside a word
1007 (org-export-latex-protect-string
1008 (concat string-before "\\textbackslash{}" string-after)))
1009 ((not (or (equal string-after "")
1010 (string-match "^[ \t\n]" string-after)))
1011 ;; backslash might escape a character (like \#) or a user TeX
1012 ;; macro (like \setcounter)
1013 (org-export-latex-protect-string
1014 (concat string-before "\\" string-after)))
1015 ((and (string-match "^[ \t\n]" string-after)
1016 (string-match "[ \t\n]\\'" string-before))
1017 ;; backslash is alone, convert it to $\backslash$
1018 (org-export-latex-protect-string
1019 (concat string-before "\\textbackslash{}" string-after)))
1020 (t (org-export-latex-protect-string
1021 (concat string-before "\\textbackslash{}" string-after)))))
1023 (defun org-export-latex-keywords ()
1024 "Convert special keywords to LaTeX."
1025 (goto-char (point-min))
1026 (let ((re (concat org-export-latex-special-keyword-regexp
1027 ".*" ; including the time stamp....
1029 (while (re-search-forward re nil t)
1030 (replace-match (format "\\\\texttt{%s}" (match-string 0)) t))))
1032 (defun org-export-latex-fixed-width (opt)
1033 "When OPT is non-nil convert fixed-width sections to LaTeX."
1034 (goto-char (point-min))
1035 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t)
1036 (if opt
1037 (progn (goto-char (match-beginning 0))
1038 (insert "\\begin{verbatim}\n")
1039 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1040 (replace-match (concat (match-string 1)
1041 (match-string 2)) t t)
1042 (forward-line))
1043 (insert "\\end{verbatim}\n\n"))
1044 (progn (goto-char (match-beginning 0))
1045 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1046 (replace-match (concat "%" (match-string 1)
1047 (match-string 2)) t t)
1048 (forward-line))))))
1051 (defvar org-table-last-alignment) ; defined in org-table.el
1052 (declare-function orgtbl-to-latex "org-table" (table params) t)
1053 (defun org-export-latex-tables (insert)
1054 "Convert tables to LaTeX and INSERT it."
1055 (goto-char (point-min))
1056 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
1057 ;; FIXME really need to save-excursion?
1058 (save-excursion (org-table-align))
1059 (let* ((beg (org-table-begin))
1060 (end (org-table-end))
1061 (raw-table (buffer-substring beg end))
1062 fnum fields line lines olines gr colgropen line-fmt align
1063 caption label attr floatp longtblp)
1064 (if org-export-latex-tables-verbatim
1065 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1066 "\\end{verbatim}\n")))
1067 (apply 'delete-region (list beg end))
1068 (insert (org-export-latex-protect-string tbl)))
1069 (progn
1070 (setq caption (org-find-text-property-in-string
1071 'org-caption raw-table)
1072 attr (org-find-text-property-in-string
1073 'org-attributes raw-table)
1074 label (org-find-text-property-in-string
1075 'org-label raw-table)
1076 longtblp (and attr (stringp attr)
1077 (string-match "\\<longtable\\>" attr))
1078 align (and attr (stringp attr)
1079 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
1080 (match-string 1 attr))
1081 floatp (or caption label))
1082 (setq lines (split-string raw-table "\n" t))
1083 (apply 'delete-region (list beg end))
1084 (when org-export-table-remove-special-lines
1085 (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
1086 ;; make a formatting string to reflect aligment
1087 (setq olines lines)
1088 (while (and (not line-fmt) (setq line (pop olines)))
1089 (unless (string-match "^[ \t]*|-" line)
1090 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1091 (setq fnum (make-vector (length fields) 0))
1092 (setq line-fmt
1093 (mapconcat
1094 (lambda (x)
1095 (setq gr (pop org-table-colgroup-info))
1096 (format "%s%%s%s"
1097 (cond ((eq gr ':start)
1098 (prog1 (if colgropen "|" "")
1099 (setq colgropen t)))
1100 ((eq gr ':startend)
1101 (prog1 (if colgropen "|" "|")
1102 (setq colgropen nil)))
1103 (t ""))
1104 (if (memq gr '(:end :startend))
1105 (progn (setq colgropen nil) "|")
1106 "")))
1107 fnum ""))))
1108 ;; fix double || in line-fmt
1109 (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
1110 ;; maybe remove the first and last "|"
1111 (when (and (not org-export-latex-tables-column-borders)
1112 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
1113 (setq line-fmt (match-string 2 line-fmt)))
1114 ;; format alignment
1115 (unless align
1116 (setq align (apply 'format
1117 (cons line-fmt
1118 (mapcar (lambda (x) (if x "r" "l"))
1119 org-table-last-alignment)))))
1120 ;; prepare the table to send to orgtbl-to-latex
1121 (setq lines
1122 (mapcar
1123 (lambda(elem)
1124 (or (and (string-match "[ \t]*|-+" elem) 'hline)
1125 (split-string (org-trim elem) "|" t)))
1126 lines))
1127 (when insert
1128 (insert (org-export-latex-protect-string
1129 (concat
1130 (if longtblp
1131 (concat "\\begin{longtable}{" align "}\n")
1132 (if floatp "\\begin{table}[htb]\n"))
1133 (if (or floatp longtblp)
1134 (format
1135 "\\caption{%s%s}"
1136 (if label (concat "\\\label{" label "}") "")
1137 (or caption "")))
1138 (if longtblp "\\\\\n" "\n")
1139 (if (not longtblp) "\\begin{center}\n")
1140 (if (not longtblp) (concat "\\begin{tabular}{" align "}\n"))
1141 (orgtbl-to-latex
1142 lines
1143 `(:tstart nil :tend nil
1144 :hlend ,(if longtblp
1145 (format "\\\\
1146 \\hline
1147 \\endhead
1148 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
1149 \\endfoot
1150 \\endlastfoot" (length org-table-last-alignment))
1151 nil)))
1152 (if (not longtblp) (concat "\n\\end{tabular}"))
1153 (if longtblp "\n" "\n\\end{center}\n")
1154 (if longtblp
1155 "\\end{longtable}"
1156 (if floatp "\\end{table}"))))
1157 "\n\n")))))))
1159 (defun org-export-latex-fontify ()
1160 "Convert fontification to LaTeX."
1161 (goto-char (point-min))
1162 (while (re-search-forward org-emph-re nil t)
1163 ;; The match goes one char after the *string*
1164 (let ((emph (assoc (match-string 3)
1165 org-export-latex-emphasis-alist))
1166 (beg (match-beginning 0))
1167 (end (match-end 0))
1168 rpl)
1169 (unless (or (get-text-property (1- (point)) 'org-protected)
1170 (save-excursion
1171 (goto-char (match-beginning 1))
1172 (save-match-data
1173 (and (org-at-table-p)
1174 (string-match
1175 "[|\n]" (buffer-substring beg end))))))
1176 (setq rpl (concat (match-string 1)
1177 (format (org-export-latex-protect-char-in-string
1178 '("\\" "{" "}") (cadr emph))
1179 (match-string 4))
1180 (match-string 5)))
1181 (if (caddr emph)
1182 (setq rpl (org-export-latex-protect-string rpl)))
1183 (replace-match rpl t t)))
1184 (backward-char)))
1186 (defun org-export-latex-links ()
1187 ;; Make sure to use the LaTeX hyperref and graphicx package
1188 ;; or send some warnings.
1189 "Convert links to LaTeX."
1190 (goto-char (point-min))
1191 (while (re-search-forward org-bracket-link-analytic-regexp++ nil t)
1192 (org-if-unprotected
1193 (goto-char (match-beginning 0))
1194 (let* ((re-radio org-export-latex-all-targets-re)
1195 (remove (list (match-beginning 0) (match-end 0)))
1196 (raw-path (org-extract-attributes (match-string 3)))
1197 (full-raw-path (concat (match-string 1) raw-path))
1198 (desc (match-string 5))
1199 (type (or (match-string 2)
1200 (if (or (file-name-absolute-p raw-path)
1201 (string-match "^\\.\\.?/" raw-path))
1202 "file")))
1203 (coderefp (equal type "coderef"))
1204 (caption (org-find-text-property-in-string 'org-caption raw-path))
1205 (attr (org-find-text-property-in-string 'org-attributes raw-path))
1206 (label (org-find-text-property-in-string 'org-label raw-path))
1207 (floatp (or label caption))
1208 imgp radiop
1209 ;; define the path of the link
1210 (path (cond
1211 ((member type '("coderef"))
1212 raw-path)
1213 ((member type '("http" "https" "ftp"))
1214 (concat type ":" raw-path))
1215 ((and re-radio (string-match re-radio raw-path))
1216 (setq radiop t))
1217 ((equal type "mailto")
1218 (concat type ":" raw-path))
1219 ((equal type "file")
1220 (if (and (org-file-image-p
1221 (expand-file-name
1222 raw-path)
1223 org-export-latex-inline-image-extensions)
1224 (equal desc full-raw-path))
1225 (setq imgp t)
1226 (progn (when (string-match "\\(.+\\)::.+" raw-path)
1227 (setq raw-path (match-string 1 raw-path)))
1228 (if (file-exists-p raw-path)
1229 (concat type "://" (expand-file-name raw-path))
1230 (concat type "://" (org-export-directory
1231 :LaTeX org-export-latex-options-plist)
1232 raw-path))))))))
1233 ;; process with link inserting
1234 (apply 'delete-region remove)
1235 (cond ((and imgp (plist-get org-export-latex-options-plist :inline-images))
1236 (insert
1237 (concat
1238 (if floatp "\\begin{figure}[htb]\n")
1239 (format "\\centerline{\\includegraphics[%s]{%s}}\n"
1240 (or attr org-export-latex-image-default-option)
1241 (if (file-name-absolute-p raw-path)
1242 (expand-file-name raw-path)
1243 raw-path))
1244 (if floatp
1245 (format "\\caption{%s%s}\n"
1246 (if label (concat "\\label{" label "}") "")
1247 (or caption "")))
1248 (if floatp "\\end{figure}\n"))))
1249 (coderefp
1250 (insert (format
1251 (org-export-get-coderef-format path desc)
1252 (cdr (assoc path org-export-code-refs)))))
1253 (radiop (insert (format "\\hyperref[%s]{%s}"
1254 (org-solidify-link-text raw-path) desc)))
1255 ((not type)
1256 (insert (format "\\hyperref[%s]{%s}"
1257 (org-solidify-link-text raw-path) desc)))
1258 (path (insert (format "\\href{%s}{%s}" path desc)))
1259 (t (insert "\\texttt{" desc "}")))))))
1261 (defvar org-latex-entities) ; defined below
1262 (defvar org-latex-entities-regexp) ; defined below
1264 (defun org-export-latex-preprocess ()
1265 "Clean stuff in the LaTeX export."
1266 ;; Preserve line breaks
1267 (goto-char (point-min))
1268 (while (re-search-forward "\\\\\\\\" nil t)
1269 (add-text-properties (match-beginning 0) (match-end 0)
1270 '(org-protected t)))
1272 ;; Preserve latex environments
1273 (goto-char (point-min))
1274 (while (re-search-forward "^[ \t]*\\begin{\\([a-zA-Z]+\\)}" nil t)
1275 (let* ((start (progn (beginning-of-line) (point)))
1276 (end (or (and (re-search-forward
1277 (concat "^[ \t]*\\end{" (match-string 1) "}" nil t)
1278 (point-at-eol)))
1279 (point-max))))
1280 (add-text-properties start end '(org-protected t))))
1282 ;; Preserve math snippets
1284 (let* ((matchers (plist-get org-format-latex-options :matchers))
1285 (re-list org-latex-regexps)
1286 beg end re e m n block off)
1287 ;; Check the different regular expressions
1288 (while (setq e (pop re-list))
1289 (setq m (car e) re (nth 1 e) n (nth 2 e)
1290 block (if (nth 3 e) "\n\n" ""))
1291 (setq off (if (member m '("$" "$1")) 1 0))
1292 (when (and (member m matchers) (not (equal m "begin")))
1293 (goto-char (point-min))
1294 (while (re-search-forward re nil t)
1295 (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
1296 (add-text-properties beg end '(org-protected t org-latex-math t))))))
1298 ;; Convert LaTeX to \LaTeX{}
1299 (goto-char (point-min))
1300 (let ((case-fold-search nil) rpl)
1301 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
1302 (replace-match (org-export-latex-protect-string
1303 (concat (match-string 1) "\\LaTeX{}")) t t)))
1305 ;; Convert blockquotes
1306 (goto-char (point-min))
1307 (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
1308 (replace-match "\\begin{quote}" t t))
1309 (goto-char (point-min))
1310 (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
1311 (replace-match "\\end{quote}" t t))
1313 ;; Convert verse
1314 (goto-char (point-min))
1315 (while (search-forward "ORG-VERSE-START" nil t)
1316 (replace-match "\\begin{verse}" t t))
1317 (goto-char (point-min))
1318 (while (search-forward "ORG-VERSE-END" nil t)
1319 (replace-match "\\end{verse}" t t))
1321 ;; Convert horizontal rules
1322 (goto-char (point-min))
1323 (while (re-search-forward "^----+.$" nil t)
1324 (replace-match (org-export-latex-protect-string "\\hrule") t t))
1326 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
1327 (goto-char (point-min))
1328 (while (re-search-forward "\\\\[a-zA-Z]+\\(?:\\[.*\\]\\)?{.*}" nil t)
1329 (add-text-properties (match-beginning 0) (match-end 0)
1330 '(org-protected t)))
1332 ;; Protect LaTeX entities
1333 (goto-char (point-min))
1334 (while (re-search-forward org-latex-entities-regexp nil t)
1335 (add-text-properties (match-beginning 0) (match-end 0)
1336 '(org-protected t)))
1338 ;; Replace radio links
1339 (goto-char (point-min))
1340 (while (re-search-forward
1341 (concat "<<<?" org-export-latex-all-targets-re
1342 ">>>?\\((INVISIBLE)\\)?") nil t)
1343 (replace-match
1344 (org-export-latex-protect-string
1345 (format "\\label{%s}%s" (save-match-data (org-solidify-link-text
1346 (match-string 1)))
1347 (if (match-string 2) "" (match-string 1)))) t t))
1349 ;; Delete @<...> constructs
1350 ;; Thanks to Daniel Clemente for this regexp
1351 (goto-char (point-min))
1352 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
1353 (replace-match ""))
1355 ;; When converting to LaTeX, replace footnotes
1356 ;; FIXME: don't protect footnotes from conversion
1357 (when (plist-get org-export-latex-options-plist :footnotes)
1358 (goto-char (point-min))
1359 (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t)
1360 (when (save-match-data
1361 (save-excursion (beginning-of-line)
1362 (looking-at "[^:|#]")))
1363 (let ((foot-beg (match-beginning 0))
1364 (foot-end (match-end 0))
1365 (foot-prefix (match-string 0))
1366 footnote footnote-rpl)
1367 (save-excursion
1368 (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix))
1369 nil t))
1370 (replace-match "$^{\\1}$")
1371 (replace-match "")
1372 (let ((end (save-excursion
1373 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
1374 (match-beginning 0) (point-max)))))
1375 (setq footnote (concat (org-trim (buffer-substring (point) end))
1376 " ")) ; prevent last } being part of a link
1377 (delete-region (point) end))
1378 (goto-char foot-beg)
1379 (delete-region foot-beg foot-end)
1380 (unless (null footnote)
1381 (setq footnote-rpl (format "\\footnote{%s}" footnote))
1382 (add-text-properties 0 10 '(org-protected t) footnote-rpl)
1383 (add-text-properties (1- (length footnote-rpl))
1384 (length footnote-rpl)
1385 '(org-protected t) footnote-rpl)
1386 (insert footnote-rpl)))
1387 ))))
1389 ;; Remove footnote section tag for LaTeX
1390 (goto-char (point-min))
1391 (while (re-search-forward
1392 (concat "^" footnote-section-tag-regexp) nil t)
1393 (replace-match ""))))
1395 ;;; List handling:
1397 (defun org-export-latex-lists ()
1398 "Convert plain text lists in current buffer into LaTeX lists."
1399 (goto-char (point-min))
1400 (while (re-search-forward org-list-beginning-re nil t)
1401 (org-if-unprotected
1402 (beginning-of-line)
1403 (insert (org-list-to-latex (org-list-parse-list t)
1404 org-export-latex-list-parameters))
1405 "\n")))
1407 (defconst org-latex-entities
1408 '("\\!"
1409 "\\'"
1410 "\\+"
1411 "\\,"
1412 "\\-"
1413 "\\:"
1414 "\\;"
1415 "\\<"
1416 "\\="
1417 "\\>"
1418 "\\Huge"
1419 "\\LARGE"
1420 "\\Large"
1421 "\\Styles"
1422 "\\\\"
1423 "\\`"
1424 "\\addcontentsline"
1425 "\\address"
1426 "\\addtocontents"
1427 "\\addtocounter"
1428 "\\addtolength"
1429 "\\addvspace"
1430 "\\alph"
1431 "\\appendix"
1432 "\\arabic"
1433 "\\author"
1434 "\\begin{array}"
1435 "\\begin{center}"
1436 "\\begin{description}"
1437 "\\begin{enumerate}"
1438 "\\begin{eqnarray}"
1439 "\\begin{equation}"
1440 "\\begin{figure}"
1441 "\\begin{flushleft}"
1442 "\\begin{flushright}"
1443 "\\begin{itemize}"
1444 "\\begin{list}"
1445 "\\begin{minipage}"
1446 "\\begin{picture}"
1447 "\\begin{quotation}"
1448 "\\begin{quote}"
1449 "\\begin{tabbing}"
1450 "\\begin{table}"
1451 "\\begin{tabular}"
1452 "\\begin{thebibliography}"
1453 "\\begin{theorem}"
1454 "\\begin{titlepage}"
1455 "\\begin{verbatim}"
1456 "\\begin{verse}"
1457 "\\bf"
1458 "\\bf"
1459 "\\bibitem"
1460 "\\bigskip"
1461 "\\cdots"
1462 "\\centering"
1463 "\\circle"
1464 "\\cite"
1465 "\\cleardoublepage"
1466 "\\clearpage"
1467 "\\cline"
1468 "\\closing"
1469 "\\dashbox"
1470 "\\date"
1471 "\\ddots"
1472 "\\dotfill"
1473 "\\em"
1474 "\\fbox"
1475 "\\flushbottom"
1476 "\\fnsymbol"
1477 "\\footnote"
1478 "\\footnotemark"
1479 "\\footnotesize"
1480 "\\footnotetext"
1481 "\\frac"
1482 "\\frame"
1483 "\\framebox"
1484 "\\hfill"
1485 "\\hline"
1486 "\\hrulespace"
1487 "\\hspace"
1488 "\\huge"
1489 "\\hyphenation"
1490 "\\include"
1491 "\\includeonly"
1492 "\\indent"
1493 "\\input"
1494 "\\it"
1495 "\\kill"
1496 "\\label"
1497 "\\large"
1498 "\\ldots"
1499 "\\line"
1500 "\\linebreak"
1501 "\\linethickness"
1502 "\\listoffigures"
1503 "\\listoftables"
1504 "\\location"
1505 "\\makebox"
1506 "\\maketitle"
1507 "\\mark"
1508 "\\mbox"
1509 "\\medskip"
1510 "\\multicolumn"
1511 "\\multiput"
1512 "\\newcommand"
1513 "\\newcounter"
1514 "\\newenvironment"
1515 "\\newfont"
1516 "\\newlength"
1517 "\\newline"
1518 "\\newpage"
1519 "\\newsavebox"
1520 "\\newtheorem"
1521 "\\nocite"
1522 "\\nofiles"
1523 "\\noindent"
1524 "\\nolinebreak"
1525 "\\nopagebreak"
1526 "\\normalsize"
1527 "\\onecolumn"
1528 "\\opening"
1529 "\\oval"
1530 "\\overbrace"
1531 "\\overline"
1532 "\\pagebreak"
1533 "\\pagenumbering"
1534 "\\pageref"
1535 "\\pagestyle"
1536 "\\par"
1537 "\\parbox"
1538 "\\put"
1539 "\\raggedbottom"
1540 "\\raggedleft"
1541 "\\raggedright"
1542 "\\raisebox"
1543 "\\ref"
1544 "\\rm"
1545 "\\roman"
1546 "\\rule"
1547 "\\savebox"
1548 "\\sc"
1549 "\\scriptsize"
1550 "\\setcounter"
1551 "\\setlength"
1552 "\\settowidth"
1553 "\\sf"
1554 "\\shortstack"
1555 "\\signature"
1556 "\\sl"
1557 "\\small"
1558 "\\smallskip"
1559 "\\sqrt"
1560 "\\tableofcontents"
1561 "\\telephone"
1562 "\\thanks"
1563 "\\thispagestyle"
1564 "\\tiny"
1565 "\\title"
1566 "\\tt"
1567 "\\twocolumn"
1568 "\\typein"
1569 "\\typeout"
1570 "\\underbrace"
1571 "\\underline"
1572 "\\usebox"
1573 "\\usecounter"
1574 "\\value"
1575 "\\vdots"
1576 "\\vector"
1577 "\\verb"
1578 "\\vfill"
1579 "\\vline"
1580 "\\vspace")
1581 "A list of LaTeX commands to be protected when performing conversion.")
1583 (defconst org-latex-entities-regexp
1584 (let (names rest)
1585 (dolist (x org-latex-entities)
1586 (if (string-match "[a-z][A-Z]$" x)
1587 (push x names)
1588 (push x rest)))
1589 (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
1590 "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
1592 (provide 'org-export-latex)
1594 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
1596 ;;; org-export-latex.el ends here