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