]> code.delx.au - gnu-emacs/blob - lisp/org/org-export-latex.el
78a5f9d1859ff374440df48d9c85fa765cf68ea4
[gnu-emacs] / lisp / org / org-export-latex.el
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.16
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
13
14 ;; This file is part of GNU Emacs.
15
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.
20
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.
25
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/>.
28
29 ;;; Commentary:
30 ;;
31 ;; This library implements a LaTeX exporter for org-mode.
32 ;;
33 ;; Put this file into your load-path and the following into your ~/.emacs:
34 ;; (require 'org-export-latex)
35 ;;
36 ;; The interactive functions are similar to those of the HTML exporter:
37 ;;
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'
45 ;;
46 ;;; Code:
47
48 (eval-when-compile
49 (require 'cl))
50
51 (require 'footnote)
52 (require 'org)
53 (require 'org-exp)
54
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.")
71
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
75
76 ;;; User variables:
77
78 (defgroup org-export-latex nil
79 "Options for exporting Org-mode files to LaTeX."
80 :tag "Org Export LaTeX"
81 :group 'org-export)
82
83 (defcustom org-export-latex-default-class "article"
84 "The default LaTeX class."
85 :group 'org-export-latex
86 :type '(string :tag "LaTeX class"))
87
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:
128
129 \(class-name
130 header-string
131 (numbered-section . unnumbered-section\)
132 ...\)
133
134 A %s formatter is mandatory in each section string and will be
135 replaced by the title of the section.
136
137 Instead of a cons cell (numbered . unnumbered), you can also provide a list
138 of 2-4 elements,
139
140 (numbered-open numbered-close)
141
142 or
143
144 (numbered-open numbered-close unnumbered-open unnumbered-close)
145
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)")))))))
163
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)
179
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)
187
188 (defcustom org-export-latex-date-format
189 "%d %B %Y"
190 "Format string for \\date{...}."
191 :group 'org-export-latex
192 :type 'string)
193
194 (defcustom org-export-latex-tables-verbatim nil
195 "When non-nil, tables are exported verbatim."
196 :group 'org-export-latex
197 :type 'boolean)
198
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)
203
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)
209
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.
214
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}")))
223
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.
228
229 Don't remove the keys, just change their values."
230 :type 'plist
231 :group 'org-export-latex)
232
233 (defcustom org-export-latex-image-default-option "width=10em"
234 "Default option for images."
235 :group 'org-export-latex
236 :type 'string)
237
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)
242
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)
248
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)
254
255 ;;; Autoload functions:
256
257 ;;;###autoload
258 (defun org-export-as-latex-batch ()
259 "Call `org-export-as-latex', may be used in batch processing.
260 For example:
261
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))
267
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*"))
275
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)))
301
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:
312
313 (setq latex (org-export-region-as-latex beg end t 'string))
314
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)))
332
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")))
361
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)))
439
440 (set-buffer buffer)
441 (erase-buffer)
442
443 (and (fboundp 'set-buffer-file-coding-system)
444 (set-buffer-file-coding-system coding-system-for-write))
445
446 ;; insert the header and initial document commands
447 (unless (or (eq to-buffer 'string) body-only)
448 (insert header))
449
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"))
455
456 ;; insert lines before the first headline
457 (unless (or skip (eq to-buffer 'string))
458 (insert first-lines))
459
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)))))
471
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))))
483
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)))
512
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"))))
521
522 ;;; Parsing functions:
523
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))))
572
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)))
580
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)))
591
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))))
600
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)))
607
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)))))))))
654
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)))))
683
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))))))))
739
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))))))
762
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))))
793
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)
797
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))))
808
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)))))
836
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))))
853
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)))
870
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 (unless (get-text-property (match-beginning 2) 'org-protected)
883 (cond ((member (match-string 2) '("\\$" "$"))
884 (if (equal (match-string 2) "\\$")
885 (replace-match (concat (match-string 1) "$"
886 (match-string 3)) t t)
887 (replace-match (concat (match-string 1) "\\$"
888 (match-string 3)) t t)))
889 ((member (match-string 2) '("&" "%" "#"))
890 (if (equal (match-string 1) "\\")
891 (replace-match (match-string 2) t t)
892 (replace-match (concat (match-string 1) "\\"
893 (match-string 2)) t t)))
894 ((equal (match-string 2) "...")
895 (replace-match
896 (concat (match-string 1)
897 (org-export-latex-protect-string "\\ldots{}")) t t))
898 ((equal (match-string 2) "~")
899 (cond ((equal (match-string 1) "\\") nil)
900 ((eq 'org-link (get-text-property 0 'face (match-string 2)))
901 (replace-match (concat (match-string 1) "\\~") t t))
902 (t (replace-match
903 (org-export-latex-protect-string
904 (concat (match-string 1) "\\~{}")) t t))))
905 ((member (match-string 2) '("{" "}"))
906 (unless (save-match-data (org-inside-LaTeX-fragment-p))
907 (if (equal (match-string 1) "\\")
908 (replace-match (match-string 2) t t)
909 (replace-match (concat (match-string 1) "\\"
910 (match-string 2)) t t)))))
911 (unless (save-match-data (org-inside-LaTeX-fragment-p))
912 (cond ((equal (match-string 2) "\\")
913 (replace-match (or (save-match-data
914 (org-export-latex-treat-backslash-char
915 (match-string 1)
916 (or (match-string 3) "")))
917 "") t t))
918 ((member (match-string 2) '("_" "^"))
919 (replace-match (or (save-match-data
920 (org-export-latex-treat-sub-super-char
921 sub-superscript
922 (match-string 2)
923 (match-string 1)
924 (match-string 3))) "") t t)))))))
925 '("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
926 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
927 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
928 "\\(.\\|^\\)\\(&\\)"
929 "\\(.\\|^\\)\\(#\\)"
930 "\\(.\\|^\\)\\(%\\)"
931 "\\(.\\|^\\)\\({\\)"
932 "\\(.\\|^\\)\\(}\\)"
933 "\\(.\\|^\\)\\(~\\)"
934 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
935 ;; (?\< . "\\textless{}")
936 ;; (?\> . "\\textgreater{}")
937 )))
938
939 (defun org-export-latex-treat-sub-super-char
940 (subsup char string-before string-after)
941 "Convert the \"_\" and \"^\" characters to LaTeX.
942 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
943 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
944 (cond ((equal string-before "\\")
945 (concat string-before char string-after))
946 ;; this is part of a math formula
947 ((and (string-match "\\S-+" string-before)
948 (string-match "\\S-+" string-after))
949 (cond ((eq 'org-link (get-text-property 0 'face char))
950 (concat string-before "\\" char string-after))
951 ((save-match-data (org-inside-LaTeX-fragment-p))
952 (if subsup
953 (cond ((eq 1 (length string-after))
954 (concat string-before char string-after))
955 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
956 (format "%s%s{%s}" string-before char
957 (match-string 1 string-after))))))
958 ((and (> (length string-after) 1)
959 (or (eq subsup t)
960 (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
961 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
962 (format "%s$%s{%s}$" string-before char
963 (if (> (match-end 1) (1+ (match-beginning 1)))
964 (concat "\\mathrm{" (match-string 1 string-after) "}")
965 (match-string 1 string-after))))
966 ((eq subsup t) (concat string-before "$" char string-after "$"))
967 (t (org-export-latex-protect-string
968 (concat string-before "\\" char "{}" string-after)))))
969 (t (org-export-latex-protect-string
970 (concat string-before "\\" char "{}" string-after)))))
971
972 (defun org-export-latex-treat-backslash-char (string-before string-after)
973 "Convert the \"$\" special character to LaTeX.
974 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
975 (cond ((member (list string-after) org-html-entities)
976 ;; backslash is part of a special entity (like "\alpha")
977 (concat string-before "$\\"
978 (or (cdar (member (list string-after) org-html-entities))
979 string-after) "$"))
980 ((and (not (string-match "^[ \n\t]" string-after))
981 (not (string-match "[ \t]\\'\\|^" string-before)))
982 ;; backslash is inside a word
983 (org-export-latex-protect-string
984 (concat string-before "\\textbackslash{}" string-after)))
985 ((not (or (equal string-after "")
986 (string-match "^[ \t\n]" string-after)))
987 ;; backslash might escape a character (like \#) or a user TeX
988 ;; macro (like \setcounter)
989 (org-export-latex-protect-string
990 (concat string-before "\\" string-after)))
991 ((and (string-match "^[ \t\n]" string-after)
992 (string-match "[ \t\n]\\'" string-before))
993 ;; backslash is alone, convert it to $\backslash$
994 (org-export-latex-protect-string
995 (concat string-before "\\textbackslash{}" string-after)))
996 (t (org-export-latex-protect-string
997 (concat string-before "\\textbackslash{}" string-after)))))
998
999 (defun org-export-latex-keywords (timestamps)
1000 "Convert special keywords to LaTeX.
1001 Regexps are those from `org-export-latex-special-string-regexps'.
1002 If TIMESTAMPS, convert timestamps, otherwise delete them."
1003 (let ((rg org-export-latex-special-string-regexps) r)
1004 (while (setq r (pop rg))
1005 (goto-char (point-min))
1006 (while (re-search-forward (eval r) nil t)
1007 (if (not timestamps)
1008 (replace-match (format "\\\\texttt{%s}" (match-string 0)) t)
1009 (replace-match ""))))))
1010
1011 (defun org-export-latex-fixed-width (opt)
1012 "When OPT is non-nil convert fixed-width sections to LaTeX."
1013 (goto-char (point-min))
1014 (while (re-search-forward "^[ \t]*:" nil t)
1015 (if opt
1016 (progn (goto-char (match-beginning 0))
1017 (insert "\\begin{verbatim}\n")
1018 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
1019 (replace-match (concat (match-string 1)
1020 (match-string 2)) t t)
1021 (forward-line))
1022 (insert "\\end{verbatim}\n\n"))
1023 (progn (goto-char (match-beginning 0))
1024 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
1025 (replace-match (concat "%" (match-string 1)
1026 (match-string 2)) t t)
1027 (forward-line))))))
1028
1029
1030 (defvar org-table-last-alignment) ; defined in org-table.el
1031 (declare-function orgtbl-to-latex "org-table" (table params) t)
1032 (defun org-export-latex-tables (insert)
1033 "Convert tables to LaTeX and INSERT it."
1034 (goto-char (point-min))
1035 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
1036 ;; FIXME really need to save-excursion?
1037 (save-excursion (org-table-align))
1038 (let* ((beg (org-table-begin))
1039 (end (org-table-end))
1040 (raw-table (buffer-substring beg end))
1041 fnum fields line lines olines gr colgropen line-fmt align
1042 caption label attr floatp longtblp)
1043 (if org-export-latex-tables-verbatim
1044 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1045 "\\end{verbatim}\n")))
1046 (apply 'delete-region (list beg end))
1047 (insert (org-export-latex-protect-string tbl)))
1048 (progn
1049 (setq caption (org-find-text-property-in-string
1050 'org-caption raw-table)
1051 attr (org-find-text-property-in-string
1052 'org-attributes raw-table)
1053 label (org-find-text-property-in-string
1054 'org-label raw-table)
1055 longtblp (and attr (stringp attr)
1056 (string-match "\\<longtable\\>" attr))
1057 align (and attr (stringp attr)
1058 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
1059 (match-string 1 attr))
1060 floatp (or caption label))
1061 (setq lines (split-string raw-table "\n" t))
1062 (apply 'delete-region (list beg end))
1063 (when org-export-table-remove-special-lines
1064 (setq lines (org-table-clean-before-export lines)))
1065 ;; make a formatting string to reflect aligment
1066 (setq olines lines)
1067 (while (and (not line-fmt) (setq line (pop olines)))
1068 (unless (string-match "^[ \t]*|-" line)
1069 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1070 (setq fnum (make-vector (length fields) 0))
1071 (setq line-fmt
1072 (mapconcat
1073 (lambda (x)
1074 (setq gr (pop org-table-colgroup-info))
1075 (format "%s%%s%s"
1076 (cond ((eq gr ':start)
1077 (prog1 (if colgropen "|" "")
1078 (setq colgropen t)))
1079 ((eq gr ':startend)
1080 (prog1 (if colgropen "|" "|")
1081 (setq colgropen nil)))
1082 (t ""))
1083 (if (memq gr '(:end :startend))
1084 (progn (setq colgropen nil) "|")
1085 "")))
1086 fnum ""))))
1087 ;; fix double || in line-fmt
1088 (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
1089 ;; maybe remove the first and last "|"
1090 (when (and (not org-export-latex-tables-column-borders)
1091 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
1092 (setq line-fmt (match-string 2 line-fmt)))
1093 ;; format alignment
1094 (unless align
1095 (setq align (apply 'format
1096 (cons line-fmt
1097 (mapcar (lambda (x) (if x "r" "l"))
1098 org-table-last-alignment)))))
1099 ;; prepare the table to send to orgtbl-to-latex
1100 (setq lines
1101 (mapcar
1102 (lambda(elem)
1103 (or (and (string-match "[ \t]*|-+" elem) 'hline)
1104 (split-string (org-trim elem) "|" t)))
1105 lines))
1106 (when insert
1107 (insert (org-export-latex-protect-string
1108 (concat
1109 (if longtblp
1110 (concat "\\begin{longtable}{" align "}\n")
1111 (if floatp "\\begin{table}[htb]\n"))
1112 (if (or floatp longtblp)
1113 (format
1114 "\\caption{%s%s}"
1115 (if label (concat "\\\label{" label "}") "")
1116 (or caption "")))
1117 (if longtblp "\\\\\n" "\n")
1118 (if (not longtblp) "\\begin{center}\n")
1119 (if (not longtblp) (concat "\\begin{tabular}{" align "}\n"))
1120 (orgtbl-to-latex
1121 lines
1122 `(:tstart nil :tend nil
1123 :hlend ,(if longtblp
1124 (format "\\\\
1125 \\hline
1126 \\endhead
1127 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
1128 \\endfoot
1129 \\endlastfoot" (length org-table-last-alignment))
1130 nil)))
1131 (if (not longtblp) (concat "\n\\end{tabular}"))
1132 (if longtblp "\n" "\n\\end{center}\n")
1133 (if longtblp
1134 "\\end{longtable}"
1135 (if floatp "\\end{table}"))))
1136 "\n\n")))))))
1137
1138 (defun org-export-latex-fontify ()
1139 "Convert fontification to LaTeX."
1140 (goto-char (point-min))
1141 (while (re-search-forward org-emph-re nil t)
1142 ;; The match goes one char after the *string*
1143 (let ((emph (assoc (match-string 3)
1144 org-export-latex-emphasis-alist))
1145 rpl)
1146 (unless (get-text-property (1- (point)) 'org-protected)
1147 (setq rpl (concat (match-string 1)
1148 (format (org-export-latex-protect-char-in-string
1149 '("\\" "{" "}") (cadr emph))
1150 (match-string 4))
1151 (match-string 5)))
1152 (if (caddr emph)
1153 (setq rpl (org-export-latex-protect-string rpl)))
1154 (replace-match rpl t t)))
1155 (backward-char)))
1156
1157 (defun org-export-latex-links ()
1158 ;; Make sure to use the LaTeX hyperref and graphicx package
1159 ;; or send some warnings.
1160 "Convert links to LaTeX."
1161 (goto-char (point-min))
1162 (while (re-search-forward org-bracket-link-analytic-regexp nil t)
1163 (org-if-unprotected
1164 (goto-char (match-beginning 0))
1165 (let* ((re-radio org-export-latex-all-targets-re)
1166 (remove (list (match-beginning 0) (match-end 0)))
1167 (raw-path (org-extract-attributes (match-string 3)))
1168 (full-raw-path (concat (match-string 1) raw-path))
1169 (desc (match-string 5))
1170 (type (or (match-string 2)
1171 (if (or (file-name-absolute-p raw-path)
1172 (string-match "^\\.\\.?/" raw-path))
1173 "file")))
1174 (caption (org-find-text-property-in-string 'org-caption raw-path))
1175 (attr (org-find-text-property-in-string 'org-attributes raw-path))
1176 (label (org-find-text-property-in-string 'org-label raw-path))
1177 (floatp (or label caption))
1178 imgp radiop
1179 ;; define the path of the link
1180 (path (cond
1181 ((member type '("http" "https" "ftp"))
1182 (concat type ":" raw-path))
1183 ((and re-radio (string-match re-radio raw-path))
1184 (setq radiop t))
1185 ((equal type "mailto")
1186 (concat type ":" raw-path))
1187 ((equal type "file")
1188 (if (and (or (org-file-image-p (expand-file-name raw-path))
1189 (string-match "\\.\\(pdf\\|jpg\\|ps\\|eps\\)$"
1190 raw-path))
1191 (equal desc full-raw-path))
1192 (setq imgp t)
1193 (progn (when (string-match "\\(.+\\)::.+" raw-path)
1194 (setq raw-path (match-string 1 raw-path)))
1195 (if (file-exists-p raw-path)
1196 (concat type "://" (expand-file-name raw-path))
1197 (concat type "://" (org-export-directory
1198 :LaTeX org-export-latex-options-plist)
1199 raw-path))))))))
1200 ;; process with link inserting
1201 (apply 'delete-region remove)
1202 (cond ((and imgp (plist-get org-export-latex-options-plist :inline-images))
1203 (insert
1204 (concat
1205 (if floatp "\\begin{figure}[htb]\n")
1206 (format "\\centerline{\\includegraphics[%s]{%s}}\n"
1207 (or attr org-export-latex-image-default-option)
1208 (expand-file-name raw-path))
1209 (if floatp
1210 (format "\\caption{%s%s}\n"
1211 (if label (concat "\\label{" label "}") "")
1212 (or caption "")))
1213 (if floatp "\\end{figure}\n"))))
1214 (radiop (insert (format "\\hyperref[%s]{%s}"
1215 (org-solidify-link-text raw-path) desc)))
1216 ((not type)
1217 (insert (format "\\hyperref[%s]{%s}"
1218 (org-solidify-link-text raw-path) desc)))
1219 (path (insert (format "\\href{%s}{%s}" path desc)))
1220 (t (insert "\\texttt{" desc "}")))))))
1221
1222 (defvar org-latex-entities) ; defined below
1223 (defvar org-latex-entities-regexp) ; defined below
1224
1225 (defun org-export-latex-preprocess ()
1226 "Clean stuff in the LaTeX export."
1227 ;; Preserve line breaks
1228 (goto-char (point-min))
1229 (while (re-search-forward "\\\\\\\\" nil t)
1230 (add-text-properties (match-beginning 0) (match-end 0)
1231 '(org-protected t)))
1232
1233 ;; Preserve latex environments
1234 (goto-char (point-min))
1235 (while (re-search-forward "^[ \t]*\\begin{\\([a-zA-Z]+\\)}" nil t)
1236 (let* ((start (progn (beginning-of-line) (point)))
1237 (end (or (and (re-search-forward
1238 (concat "^[ \t]*\\end{" (match-string 1) "}" nil t)
1239 (point-at-eol)))
1240 (point-max))))
1241 (add-text-properties start end '(org-protected t))))
1242
1243 ;; Convert LaTeX to \LaTeX{}
1244 (goto-char (point-min))
1245 (let ((case-fold-search nil) rpl)
1246 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
1247 (replace-match (org-export-latex-protect-string
1248 (concat (match-string 1) "\\LaTeX{}")) t t)))
1249
1250 ;; Convert blockquotes
1251 (goto-char (point-min))
1252 (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
1253 (replace-match "\\begin{quote}" t t))
1254 (goto-char (point-min))
1255 (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
1256 (replace-match "\\end{quote}" t t))
1257
1258 ;; Convert verse
1259 (goto-char (point-min))
1260 (while (search-forward "ORG-VERSE-START" nil t)
1261 (replace-match "\\begin{verse}" t t))
1262 (goto-char (point-min))
1263 (while (search-forward "ORG-VERSE-END" nil t)
1264 (replace-match "\\end{verse}" t t))
1265
1266 ;; Convert horizontal rules
1267 (goto-char (point-min))
1268 (while (re-search-forward "^----+.$" nil t)
1269 (replace-match (org-export-latex-protect-string "\\hrule") t t))
1270
1271 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
1272 (goto-char (point-min))
1273 (while (re-search-forward "\\\\[a-zA-Z]+\\(?:\\[.*\\]\\)?{.*}" nil t)
1274 (add-text-properties (match-beginning 0) (match-end 0)
1275 '(org-protected t)))
1276
1277 ;; Protect LaTeX entities
1278 (goto-char (point-min))
1279 (while (re-search-forward org-latex-entities-regexp nil t)
1280 (add-text-properties (match-beginning 0) (match-end 0)
1281 '(org-protected t)))
1282
1283 ;; Replace radio links
1284 (goto-char (point-min))
1285 (while (re-search-forward
1286 (concat "<<<?" org-export-latex-all-targets-re
1287 ">>>?\\((INVISIBLE)\\)?") nil t)
1288 (replace-match
1289 (org-export-latex-protect-string
1290 (format "\\label{%s}%s" (save-match-data (org-solidify-link-text
1291 (match-string 1)))
1292 (if (match-string 2) "" (match-string 1)))) t t))
1293
1294 ;; Delete @<...> constructs
1295 ;; Thanks to Daniel Clemente for this regexp
1296 (goto-char (point-min))
1297 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
1298 (replace-match ""))
1299
1300 ;; When converting to LaTeX, replace footnotes
1301 ;; FIXME: don't protect footnotes from conversion
1302 (when (plist-get org-export-latex-options-plist :footnotes)
1303 (goto-char (point-min))
1304 (while (re-search-forward "\\[[0-9]+\\]" nil t)
1305 (when (save-match-data
1306 (save-excursion (beginning-of-line)
1307 (looking-at "[^:|#]")))
1308 (let ((foot-beg (match-beginning 0))
1309 (foot-end (match-end 0))
1310 (foot-prefix (match-string 0))
1311 footnote footnote-rpl)
1312 (save-excursion
1313 (when (search-forward foot-prefix nil t)
1314 (replace-match "")
1315 (let ((end (save-excursion
1316 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
1317 (match-beginning 0) (point-max)))))
1318 (setq footnote (concat (org-trim (buffer-substring (point) end))
1319 " ")) ; prevent last } being part of a link
1320 (delete-region (point) end))
1321 (goto-char foot-beg)
1322 (delete-region foot-beg foot-end)
1323 (unless (null footnote)
1324 (setq footnote-rpl (format "\\footnote{%s}" footnote))
1325 (add-text-properties 0 10 '(org-protected t) footnote-rpl)
1326 (add-text-properties (1- (length footnote-rpl))
1327 (length footnote-rpl)
1328 '(org-protected t) footnote-rpl)
1329 (insert footnote-rpl)))))))
1330
1331 ;; Replace footnote section tag for LaTeX
1332 (goto-char (point-min))
1333 (while (re-search-forward
1334 (concat "^" footnote-section-tag-regexp) nil t)
1335 (replace-match ""))))
1336
1337 ;;; List handling:
1338
1339 (defun org-export-latex-lists ()
1340 "Replace plain text lists in current buffer into LaTeX lists."
1341 "Convert lists to LaTeX."
1342 (goto-char (point-min))
1343 (while (re-search-forward org-list-beginning-re nil t)
1344 (org-if-unprotected
1345 (beginning-of-line)
1346 (insert (org-list-to-latex (org-list-parse-list t)) "\n"))))
1347
1348 (defconst org-latex-entities
1349 '("\\!"
1350 "\\'"
1351 "\\+"
1352 "\\,"
1353 "\\-"
1354 "\\:"
1355 "\\;"
1356 "\\<"
1357 "\\="
1358 "\\>"
1359 "\\Huge"
1360 "\\LARGE"
1361 "\\Large"
1362 "\\Styles"
1363 "\\\\"
1364 "\\`"
1365 "\\addcontentsline"
1366 "\\address"
1367 "\\addtocontents"
1368 "\\addtocounter"
1369 "\\addtolength"
1370 "\\addvspace"
1371 "\\alph"
1372 "\\appendix"
1373 "\\arabic"
1374 "\\author"
1375 "\\begin{array}"
1376 "\\begin{center}"
1377 "\\begin{description}"
1378 "\\begin{enumerate}"
1379 "\\begin{eqnarray}"
1380 "\\begin{equation}"
1381 "\\begin{figure}"
1382 "\\begin{flushleft}"
1383 "\\begin{flushright}"
1384 "\\begin{itemize}"
1385 "\\begin{list}"
1386 "\\begin{minipage}"
1387 "\\begin{picture}"
1388 "\\begin{quotation}"
1389 "\\begin{quote}"
1390 "\\begin{tabbing}"
1391 "\\begin{table}"
1392 "\\begin{tabular}"
1393 "\\begin{thebibliography}"
1394 "\\begin{theorem}"
1395 "\\begin{titlepage}"
1396 "\\begin{verbatim}"
1397 "\\begin{verse}"
1398 "\\bf"
1399 "\\bf"
1400 "\\bibitem"
1401 "\\bigskip"
1402 "\\cdots"
1403 "\\centering"
1404 "\\circle"
1405 "\\cite"
1406 "\\cleardoublepage"
1407 "\\clearpage"
1408 "\\cline"
1409 "\\closing"
1410 "\\dashbox"
1411 "\\date"
1412 "\\ddots"
1413 "\\dotfill"
1414 "\\em"
1415 "\\fbox"
1416 "\\flushbottom"
1417 "\\fnsymbol"
1418 "\\footnote"
1419 "\\footnotemark"
1420 "\\footnotesize"
1421 "\\footnotetext"
1422 "\\frac"
1423 "\\frame"
1424 "\\framebox"
1425 "\\hfill"
1426 "\\hline"
1427 "\\hrulespace"
1428 "\\hspace"
1429 "\\huge"
1430 "\\hyphenation"
1431 "\\include"
1432 "\\includeonly"
1433 "\\indent"
1434 "\\input"
1435 "\\it"
1436 "\\kill"
1437 "\\label"
1438 "\\large"
1439 "\\ldots"
1440 "\\line"
1441 "\\linebreak"
1442 "\\linethickness"
1443 "\\listoffigures"
1444 "\\listoftables"
1445 "\\location"
1446 "\\makebox"
1447 "\\maketitle"
1448 "\\mark"
1449 "\\mbox"
1450 "\\medskip"
1451 "\\multicolumn"
1452 "\\multiput"
1453 "\\newcommand"
1454 "\\newcounter"
1455 "\\newenvironment"
1456 "\\newfont"
1457 "\\newlength"
1458 "\\newline"
1459 "\\newpage"
1460 "\\newsavebox"
1461 "\\newtheorem"
1462 "\\nocite"
1463 "\\nofiles"
1464 "\\noindent"
1465 "\\nolinebreak"
1466 "\\nopagebreak"
1467 "\\normalsize"
1468 "\\onecolumn"
1469 "\\opening"
1470 "\\oval"
1471 "\\overbrace"
1472 "\\overline"
1473 "\\pagebreak"
1474 "\\pagenumbering"
1475 "\\pageref"
1476 "\\pagestyle"
1477 "\\par"
1478 "\\parbox"
1479 "\\put"
1480 "\\raggedbottom"
1481 "\\raggedleft"
1482 "\\raggedright"
1483 "\\raisebox"
1484 "\\ref"
1485 "\\rm"
1486 "\\roman"
1487 "\\rule"
1488 "\\savebox"
1489 "\\sc"
1490 "\\scriptsize"
1491 "\\setcounter"
1492 "\\setlength"
1493 "\\settowidth"
1494 "\\sf"
1495 "\\shortstack"
1496 "\\signature"
1497 "\\sl"
1498 "\\small"
1499 "\\smallskip"
1500 "\\sqrt"
1501 "\\tableofcontents"
1502 "\\telephone"
1503 "\\thanks"
1504 "\\thispagestyle"
1505 "\\tiny"
1506 "\\title"
1507 "\\tt"
1508 "\\twocolumn"
1509 "\\typein"
1510 "\\typeout"
1511 "\\underbrace"
1512 "\\underline"
1513 "\\usebox"
1514 "\\usecounter"
1515 "\\value"
1516 "\\vdots"
1517 "\\vector"
1518 "\\verb"
1519 "\\vfill"
1520 "\\vline"
1521 "\\vspace")
1522 "A list of LaTeX commands to be protected when performing conversion.")
1523
1524 (defconst org-latex-entities-regexp
1525 (let (names rest)
1526 (dolist (x org-latex-entities)
1527 (if (string-match "[a-z][A-Z]$" x)
1528 (push x names)
1529 (push x rest)))
1530 (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
1531 "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
1532
1533 (provide 'org-export-latex)
1534
1535 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
1536
1537 ;;; org-export-latex.el ends here