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