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