Mercurial > emacs
annotate lisp/textmodes/org-export-latex.el @ 83982:50edbbeae27a
Move to ../doc/lispref
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Thu, 06 Sep 2007 04:10:01 +0000 |
parents | 1d75f075c32a |
children | a32093d94b4a |
rev | line source |
---|---|
82752 | 1 ;;; org-export-latex.el --- LaTeX exporter for Org-mode |
2 ;; Copyright (C) 2007 Free Software Foundation, Inc. | |
3 ;; | |
4 ;; Author: Bastien Guerry <bzg AT altern DOT org> | |
5 ;; Keywords: org organizer latex export convert | |
6 ;; X-URL: <http://www.cognition.ens.fr/~guerry/u/org-export-latex.el> | |
7 ;; | |
8 ;; This file is part of GNU Emacs. | |
9 ;; | |
10 ;; This program is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
83725
1d75f075c32a
License should be GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
82918
diff
changeset
|
12 ;; the Free Software Foundation; either version 3, or (at your option) |
82752 | 13 ;; any later version. |
14 ;; | |
15 ;; This program is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 ;; | |
20 ;; You should have received a copy of the GNU General Public License | |
21 ;; along with this program; if not, write to the Free Software | |
22 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | |
24 ;;; Commentary: | |
25 | |
26 ;; This library is a LaTeX exporter for org-mode. | |
27 ;; | |
28 ;; Put this file into your load-path and the following into your ~/.emacs: | |
29 ;; (require 'org-export-latex) | |
30 ;; | |
31 ;; The interactive functions are similar to those of the HTML exporter: | |
32 ;; | |
33 ;; M-x `org-export-as-latex' | |
34 ;; M-x `org-export-as-latex-batch' | |
35 ;; M-x `org-export-as-latex-to-buffer' | |
36 ;; M-x `org-export-region-as-latex' | |
37 ;; M-x `org-replace-region-by-latex' | |
38 | |
39 ;;; History: | |
40 ;; | |
41 ;; I started this piece of code in may 2007. Special thanks to Carsten | |
42 ;; Dominik for helping me on this. | |
43 ;; | |
44 | |
45 ;;; Code: | |
46 | |
47 (require 'org) | |
48 (require 'footnote) | |
49 | |
50 (defvar org-latex-options-plist nil) | |
51 (defvar org-latex-todo-keywords-1 nil) | |
52 (defvar org-latex-all-targets-regexp nil) | |
53 (defvar org-latex-add-level 0) | |
54 (defvar org-latex-sectioning-depth 0) | |
55 | |
56 (defvar org-latex-special-string-regexps | |
57 '(org-ts-regexp | |
58 org-scheduled-string | |
59 org-deadline-string | |
60 org-clock-string) | |
61 "A list of regexps to convert as special keywords.") | |
62 | |
63 (defcustom org-export-latex-sectioning-alist | |
64 '((1 "\\section{%s}" "\\section*{%s}") | |
65 (2 "\\subsection{%s}" "\\subsection*{%s}") | |
66 (3 "\\subsubsection{%s}" "\\subsubsection*{%s}") | |
67 (4 "\\paragraph{%s}" "\\paragraph*{%s}") | |
68 (5 "\\subparagraph{%s}" "\\subparagraph*{%s}")) | |
69 "Alist of LaTeX commands for inserting sections. | |
70 Here is the structure of each cell: | |
71 | |
72 \(level unnumbered-section numbered-section\) | |
73 | |
74 The %s formatter will be replaced by the title of the section." | |
75 :group 'org-export-latex | |
76 :type 'alist) | |
77 | |
78 (defcustom org-export-latex-emphasis-alist | |
79 '(("*" "\\textbf{%s}") | |
80 ("/" "\\emph{%s}") | |
81 ("_" "\\underline{%s}") | |
82 ("+" "\\texttt{%s}") | |
83 ("=" "\\texttt{%s}")) | |
84 "Alist of LaTeX expressions to convert emphasis fontifiers." | |
85 :group 'org-export-latex | |
86 :type 'alist) | |
87 | |
88 (defcustom org-export-latex-preamble | |
89 "\\documentclass[11pt,a4paper]{article} | |
90 \\usepackage[utf8]{inputenc} | |
91 \\usepackage[T1]{fontenc} | |
92 \\usepackage{hyperref}" | |
93 "Preamble to be inserted at the very beginning of the LaTeX export." | |
94 :group 'org-export-latex | |
95 :type 'string) | |
96 | |
97 (defcustom org-export-latex-date-format nil | |
98 "Format string for \\date{...}." | |
99 :group 'org-export-latex | |
100 :type 'string) | |
101 | |
102 (defcustom org-export-latex-packages-alist nil | |
103 "Alist of packages to be inserted in the preamble. | |
104 Each cell is of the forma \( option . package \). | |
105 | |
106 For example: | |
107 | |
108 \(setq org-export-latex-packages-alist | |
109 '((\"french\" \"babel\"))" | |
110 :group 'org-export-latex | |
111 :type 'alist) | |
112 | |
113 (defcustom org-export-latex-low-levels 'description | |
114 "Choice for converting sections that are below the current | |
115 admitted level of sectioning. This can be either nil (ignore the | |
116 sections), 'description (convert them as description lists) or a | |
117 string to be used instead of \\section{%s} (a %s for inserted the | |
118 headline is mandatory)." | |
119 :group 'org-export-latex | |
120 :type '(choice (const :tag "Ignore" nil) | |
121 (symbol :tag "Convert as descriptive list" description) | |
122 (string :tag "Use a section string" :value "\\subparagraph{%s}"))) | |
123 | |
124 (defcustom org-export-latex-remove-from-headines | |
125 '(:todo t :priority t :tags t) | |
126 "A plist of keywords to remove from headlines. | |
127 Non-nil means remove this keyword type from the headline. | |
128 | |
129 Don't remove the keys, just change their values." | |
130 :type 'plist | |
131 :group 'org-export-latex) | |
132 | |
133 (defcustom org-export-latex-quotation-marks-convention "en" | |
134 "Convention for conversion of the quotation marks. | |
135 This value is overriden by any infile language setup." | |
136 :group 'org-export-latex | |
137 :type '(choice (string :tag "english" "en") | |
138 (string :tag "french" "fr"))) | |
139 | |
140 (defcustom org-export-latex-image-default-option "width=10em" | |
141 "Default option for images." | |
142 :group 'org-export-latex | |
143 :type '(string)) | |
144 | |
145 (defcustom org-export-latex-coding-system nil | |
146 "Coding system for the exported LaTex file." | |
147 :group 'org-export-latex | |
148 :type 'coding-system) | |
149 | |
150 ;; FIXME Do we want this one? | |
151 ;; (defun org-export-as-latex-and-open (arg) ...) | |
152 | |
153 ;;;###autoload | |
154 (defun org-export-as-latex-batch () | |
155 "Call `org-export-as-latex', may be used in batch processing as | |
156 emacs --batch | |
157 --load=$HOME/lib/emacs/org.el | |
158 --eval \"(setq org-export-headline-levels 2)\" | |
159 --visit=MyFile --funcall org-export-as-latex-batch" | |
160 (org-export-as-latex org-export-headline-levels 'hidden)) | |
161 | |
162 ;;;###autoload | |
163 (defun org-export-as-latex-to-buffer (arg) | |
164 "Call `org-exort-as-latex` with output to a temporary buffer. | |
165 No file is created. The prefix ARG is passed through to `org-export-as-latex'." | |
166 (interactive "P") | |
167 (org-export-as-latex arg nil nil "*Org LaTeX Export*") | |
168 (switch-to-buffer-other-window "*Org LaTeX Export*")) | |
169 | |
170 ;;;###autoload | |
171 (defun org-replace-region-by-latex (beg end) | |
172 "Replace the region from BEG to END with its LaTeX export. | |
173 It assumes the region has `org-mode' syntax, and then convert it to | |
174 LaTeX. This can be used in any buffer. For example, you could | |
175 write an itemized list in `org-mode' syntax in an LaTeX buffer and | |
176 then use this command to convert it." | |
177 (interactive "r") | |
178 (let (reg latex buf) | |
179 (save-window-excursion | |
180 (if (org-mode-p) | |
181 (setq latex (org-export-region-as-latex | |
182 beg end t 'string)) | |
183 (setq reg (buffer-substring beg end) | |
184 buf (get-buffer-create "*Org tmp*")) | |
185 (save-excursion | |
186 (set-buffer buf) | |
187 (erase-buffer) | |
188 (insert reg) | |
189 (org-mode) | |
190 (setq latex (org-export-region-as-latex | |
191 (point-min) (point-max) t 'string))) | |
192 (kill-buffer buf))) | |
193 (delete-region beg end) | |
194 (insert latex))) | |
195 | |
196 ;;;###autoload | |
197 (defun org-export-region-as-latex (beg end &optional body-only buffer) | |
198 "Convert region from BEG to END in `org-mode' buffer to LaTeX. | |
199 If prefix arg BODY-ONLY is set, omit file header, footer, and table of | |
200 contents, and only produce the region of converted text, useful for | |
201 cut-and-paste operations. | |
202 If BUFFER is a buffer or a string, use/create that buffer as a target | |
203 of the converted LaTeX. If BUFFER is the symbol `string', return the | |
204 produced LaTeX as a string and leave not buffer behind. For example, | |
205 a Lisp program could call this function in the following way: | |
206 | |
207 (setq latex (org-export-region-as-latex beg end t 'string)) | |
208 | |
209 When called interactively, the output buffer is selected, and shown | |
210 in a window. A non-interactive call will only retunr the buffer." | |
211 (interactive "r\nP") | |
212 (when (interactive-p) | |
213 (setq buffer "*Org LaTeX Export*")) | |
214 (let ((transient-mark-mode t) (zmacs-regions t) | |
215 rtn) | |
216 (goto-char end) | |
217 (set-mark (point)) ;; to activate the region | |
218 (goto-char beg) | |
219 (setq rtn (org-export-as-latex | |
220 nil nil nil | |
221 buffer body-only)) | |
222 (if (fboundp 'deactivate-mark) (deactivate-mark)) | |
223 (if (and (interactive-p) (bufferp rtn)) | |
224 (switch-to-buffer-other-window rtn) | |
225 rtn))) | |
226 | |
227 ;;;###autoload | |
228 (defun org-export-as-latex (arg &optional hidden ext-plist | |
229 to-buffer body-only) | |
230 "Export current buffer to a LaTeX file." | |
231 (interactive "P") | |
232 ;; Make sure we have a file name when we need it. | |
233 (when (and (not (or to-buffer body-only)) | |
234 (not buffer-file-name)) | |
235 (if (buffer-base-buffer) | |
236 (org-set-local 'buffer-file-name | |
237 (with-current-buffer (buffer-base-buffer) | |
238 buffer-file-name)) | |
239 (error "Need a file name to be able to export"))) | |
240 | |
241 (message "Exporting to LaTeX...") | |
242 (org-update-radio-target-regexp) | |
243 (org-export-latex-set-initial-vars ext-plist) | |
244 (let* ((opt-plist org-latex-options-plist) | |
245 (filename (concat (file-name-as-directory | |
246 (org-export-directory :LaTeX ext-plist)) | |
247 (file-name-sans-extension | |
248 (file-name-nondirectory ;sans-extension | |
249 buffer-file-name)) ".tex")) | |
250 (filename (if (equal (file-truename filename) | |
251 (file-truename buffer-file-name)) | |
252 (concat filename ".tex") | |
253 filename)) | |
254 (buffer (if to-buffer | |
255 (cond | |
256 ((eq to-buffer 'string) (get-buffer-create | |
257 "*Org LaTeX Export*")) | |
258 (t (get-buffer-create to-buffer))) | |
259 (find-file-noselect filename))) | |
260 (region-p (org-region-active-p)) | |
261 (odd org-odd-levels-only) | |
262 (preamble (org-export-latex-make-preamble opt-plist)) | |
263 (skip (plist-get opt-plist :skip-before-1st-heading)) | |
264 (text (plist-get opt-plist :text)) | |
265 (first-lines (if skip "" (org-export-latex-first-lines))) | |
266 (coding-system (and (boundp 'buffer-file-coding-system) | |
267 buffer-file-coding-system)) | |
268 (coding-system-for-write (or org-export-latex-coding-system | |
269 coding-system)) | |
270 (save-buffer-coding-system (or org-export-latex-coding-system | |
271 coding-system)) | |
272 (region (buffer-substring | |
273 (if region-p (region-beginning) (point-min)) | |
274 (if region-p (region-end) (point-max)))) | |
275 (string-for-export | |
276 ;; FIXME Use org-cleaned-string-for-export instead, only when | |
277 ;; everyone uses Org >5.04 | |
278 (org-latex-cleaned-string-for-export | |
279 region :for-html nil | |
280 :comments nil | |
281 :for-LaTeX t | |
282 :skip-before-1st-heading nil | |
283 :LaTeX-fragments nil))) | |
284 (set-buffer buffer) | |
285 (erase-buffer) | |
286 (unless body-only (insert preamble)) | |
287 (when text (insert (org-export-latex-content text) "\n\n")) | |
288 (unless skip (insert first-lines)) | |
289 | |
290 ;; handle the case where the region does not begin with a section | |
291 (when region-p | |
292 (insert (with-temp-buffer | |
293 (insert string-for-export) | |
294 (org-export-latex-first-lines)))) | |
295 | |
296 (org-export-latex-global | |
297 (with-temp-buffer | |
298 (insert string-for-export) | |
299 (goto-char (point-min)) | |
300 (re-search-forward "^\\(\\*+\\) " nil t) | |
301 (let* ((asters (length (match-string 1))) | |
302 (level (if odd (- asters 2) (- asters 1)))) | |
303 (setq org-latex-add-level | |
304 (if odd (1- (/ (1+ asters) 2)) (1- asters))) | |
305 (org-export-latex-parse-global level odd)))) | |
306 | |
307 (unless body-only (insert "\n\\end{document}")) | |
308 (or to-buffer (write-file filename)) | |
309 (goto-char (point-min)) | |
310 (message "Exporting to LaTeX...done") | |
311 (if (eq to-buffer 'string) | |
312 (prog1 (buffer-substring (point-min) (point-max)) | |
313 (kill-buffer (current-buffer))) | |
314 (current-buffer)))) | |
315 | |
316 (defun org-export-latex-set-initial-vars (ext-plist) | |
317 "Store org local variables required for LaTeX export. | |
318 EXT-PLIST is an optional additional plist." | |
319 (setq org-latex-todo-keywords-1 org-todo-keywords-1 | |
320 org-latex-all-targets-regexp | |
321 (org-make-target-link-regexp (org-all-targets)) | |
322 org-latex-options-plist | |
323 (org-combine-plists (org-default-export-plist) ext-plist | |
324 (org-infile-export-plist)) | |
325 org-latex-sectioning-depth | |
326 (let ((hl-levels (plist-get org-latex-options-plist :headline-levels)) | |
327 (sec-depth (length org-export-latex-sectioning-alist))) | |
328 ;; Fall back on org-export-latex-sectioning-alist length if | |
329 ;; headline-levels goes beyond it | |
330 (if (> hl-levels sec-depth) sec-depth hl-levels)))) | |
331 | |
332 (defun org-export-latex-make-preamble (opt-plist) | |
333 "Make the LaTeX preamble and return it as a string. | |
334 Argument OPT-PLIST is the options plist for current buffer." | |
335 (let ((toc (plist-get opt-plist :table-of-contents))) | |
336 (format (concat org-export-latex-preamble | |
337 " | |
338 %s | |
339 | |
340 \\begin{document} | |
341 | |
342 \\title{%s} | |
343 %s | |
344 %s | |
345 \\maketitle | |
346 %s | |
347 %s | |
348 ") | |
349 (if org-export-latex-packages-alist | |
350 (mapconcat (lambda(p) | |
351 (if (equal "" (car p)) | |
352 (format "\\usepackage{%s}" (cadr p)) | |
353 (format "\\usepackage[%s]{%s}" | |
354 (car p) (cadr p)))) | |
355 org-export-latex-packages-alist "\n") "") | |
356 (or (plist-get opt-plist :title) | |
357 (and (not | |
358 (plist-get opt-plist :skip-before-1st-heading)) | |
359 (org-export-grab-title-from-buffer)) | |
360 (and buffer-file-name | |
361 (file-name-sans-extension | |
362 (file-name-nondirectory buffer-file-name))) | |
363 "UNTITLED") | |
364 (if (plist-get opt-plist :author-info) | |
365 (format "\\author{%s}" | |
366 (or (plist-get opt-plist :author) user-full-name)) | |
367 (format "%%\\author{%s}" | |
368 (or (plist-get opt-plist :author) user-full-name))) | |
369 (if (plist-get opt-plist :timestamps) | |
370 (format "\\date{%s}" | |
371 (format-time-string (or org-export-latex-date-format | |
372 (car org-time-stamp-formats)))) | |
373 "%\\date{}") | |
374 (if (and (plist-get opt-plist :section-numbers) toc) | |
375 (format "\\setcounter{tocdepth}{%s}" | |
376 (plist-get opt-plist :headline-levels)) "") | |
377 (if (and (plist-get opt-plist :section-numbers) toc) | |
378 "\\tableofcontents" "")))) | |
379 | |
380 (defun org-export-latex-first-lines (&optional comments) | |
381 "Export the first lines before first headline. | |
382 COMMENTS is either nil to replace them with the empty string or a | |
383 formatting string like %%%%s if we want to comment them out." | |
384 (save-excursion | |
385 (goto-char (point-min)) | |
386 (let* ((end (if (re-search-forward "^\\*" nil t) | |
387 (goto-char (match-beginning 0)) | |
388 (goto-char (point-max))))) | |
389 (org-export-latex-content | |
390 (org-latex-cleaned-string-for-export | |
391 (buffer-substring (point-min) end) | |
392 :for-html nil | |
393 :for-LaTeX t | |
394 :comments nil | |
395 :skip-before-1st-heading nil | |
396 :LaTeX-fragments nil))))) | |
397 | |
398 (defun org-export-latex-parse-global (level odd) | |
399 "Parse the current buffer recursively, starting at LEVEL. | |
400 If ODD is non-nil, assume the buffer only contains odd sections. | |
401 Return A list reflecting the document structure." | |
402 (save-excursion | |
403 (goto-char (point-min)) | |
404 (let* ((cnt 0) output | |
405 (depth org-latex-sectioning-depth)) | |
406 (while (re-search-forward | |
407 (concat "^\\(\\(?:\\*\\)\\{" | |
408 (number-to-string (+ (if odd 2 1) level)) | |
409 "\\}\\) \\(.*\\)$") | |
410 ;; make sure that there is no upper heading | |
411 (when (> level 0) | |
412 (save-excursion | |
413 (save-match-data | |
414 (re-search-forward | |
415 (concat "^\\(\\(?:\\*\\)\\{" | |
416 (number-to-string level) | |
417 "\\}\\) \\(.*\\)$") nil t)))) t) | |
418 (setq cnt (1+ cnt)) | |
419 (let* ((pos (match-beginning 0)) | |
420 (heading (match-string 2)) | |
421 (nlevel (if odd (/ (+ 3 level) 2) (1+ level)))) | |
422 (save-excursion | |
423 (narrow-to-region | |
424 (point) | |
425 (save-match-data | |
426 (if (re-search-forward | |
427 (concat "^\\(\\(?:\\*\\)\\{" | |
428 (number-to-string (+ (if odd 2 1) level)) | |
429 "\\}\\) \\(.*\\)$") nil t) | |
430 (match-beginning 0) | |
431 (point-max)))) | |
432 (goto-char (point-min)) | |
433 (setq output | |
434 (append output | |
435 (list | |
436 (list | |
437 `(pos . ,pos) | |
438 `(level . ,nlevel) | |
439 `(occur . ,cnt) | |
440 `(heading . ,heading) | |
441 `(content . ,(org-export-latex-parse-content)) | |
442 `(subcontent . ,(org-export-latex-parse-subcontent | |
443 level odd))))))) | |
444 (widen))) | |
445 (list output)))) | |
446 | |
447 (defun org-export-latex-parse-content () | |
448 "Extract the content of a section." | |
449 (let ((beg (point)) | |
450 (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t) | |
451 (progn (beginning-of-line) (point)) | |
452 (point-max)))) | |
453 (buffer-substring beg end))) | |
454 | |
455 (defun org-export-latex-parse-subcontent (level odd) | |
456 "Extract the subcontent of a section at LEVEL. | |
457 If ODD Is non-nil, assume subcontent only contains odd sections." | |
458 (if (not (re-search-forward | |
459 (concat "^\\(\\(?:\\*\\)\\{" | |
460 (number-to-string (+ (if odd 4 2) level)) | |
461 "\\}\\) \\(.*\\)$") | |
462 nil t)) | |
463 nil ; subcontent is nil | |
464 (org-export-latex-parse-global (+ (if odd 2 1) level) odd))) | |
465 | |
466 (defun org-export-latex-global (content) | |
467 "Export CONTENT to LaTeX. | |
468 CONTENT is an element of the list produced by | |
469 `org-export-latex-parse-global'." | |
470 (if (eq (car content) 'subcontent) | |
471 (mapc 'org-export-latex-sub (cdr content)) | |
472 (org-export-latex-sub (car content)))) | |
473 | |
474 (defun org-export-latex-sub (subcontent) | |
475 "Export the list SUBCONTENT to LaTeX. | |
476 SUBCONTENT is an alist containing information about the headline | |
477 and its content." | |
478 (mapc (lambda(x) (org-export-latex-subcontent x)) subcontent)) | |
479 | |
480 (defun org-export-latex-subcontent (subcontent) | |
481 "Export each cell of SUBCONTENT to LaTeX." | |
482 (let ((heading (org-export-latex-fontify-headline | |
483 (cdr (assoc 'heading subcontent)))) | |
484 (level (- (cdr (assoc 'level subcontent)) | |
485 org-latex-add-level)) | |
486 (occur (number-to-string (cdr (assoc 'occur subcontent)))) | |
487 (content (cdr (assoc 'content subcontent))) | |
488 (subcontent (cadr (assoc 'subcontent subcontent))) | |
489 (num (plist-get org-latex-options-plist :section-numbers))) | |
490 (cond | |
491 ;; Normal conversion | |
492 ((<= level org-latex-sectioning-depth) | |
493 (let ((sec (assoc level org-export-latex-sectioning-alist))) | |
494 (insert (format (if num (cadr sec) (caddr sec)) heading) "\n")) | |
495 (insert (org-export-latex-content content)) | |
496 (cond ((stringp subcontent) (insert subcontent)) | |
497 ((listp subcontent) (org-export-latex-sub subcontent)))) | |
498 ;; At a level under the hl option: we can drop this subsection | |
499 ((> level org-latex-sectioning-depth) | |
500 (cond ((eq org-export-latex-low-levels 'description) | |
501 (insert (format "\\begin{description}\n\n\\item[%s]\n\n" heading)) | |
502 (insert (org-export-latex-content content)) | |
503 (cond ((stringp subcontent) (insert subcontent)) | |
504 ((listp subcontent) (org-export-latex-sub subcontent))) | |
505 (insert "\\end{description}\n")) | |
506 ((stringp org-export-latex-low-levels) | |
507 (insert (format org-export-latex-low-levels heading) "\n") | |
508 (insert (org-export-latex-content content)) | |
509 (cond ((stringp subcontent) (insert subcontent)) | |
510 ((listp subcontent) (org-export-latex-sub subcontent))))))))) | |
511 | |
512 (defun org-export-latex-special-keywords-maybe (remove-list) | |
513 "Maybe remove keywords depending on rules in REMOVE-LIST." | |
514 (goto-char (point-min)) | |
515 (let ((re-todo (mapconcat 'identity org-latex-todo-keywords-1 "\\|"))) | |
516 ;; convert TODO keywords | |
517 (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t) | |
518 (if (plist-get remove-list :todo) | |
519 (replace-match "") | |
520 (replace-match (format "\\texttt{%s}" (match-string 1)) t t))) | |
521 ;; convert priority string | |
522 (when (re-search-forward "\\[\\\\#.\\]" nil t) | |
523 (if (plist-get remove-list :priority) | |
524 (replace-match "") | |
525 (replace-match (format "\\texttt{%s}" (match-string 0)) t t))) | |
526 ;; convert tags | |
527 (when (re-search-forward "\\(:[a-zA-Z0-9]+\\)+:" nil t) | |
528 (if (plist-get remove-list :tags) | |
529 (replace-match "") | |
530 (replace-match (format "\\texttt{%s}" (match-string 0)) t t))))) | |
531 | |
532 (defun org-export-latex-fontify-headline (headline) | |
533 "Fontify special words in a HEADLINE." | |
534 (with-temp-buffer | |
535 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at | |
536 ;; the beginning of the buffer - inserting "\n" is safe here though. | |
537 (insert "\n" headline) | |
538 (goto-char (point-min)) | |
539 (org-export-latex-fontify) | |
540 (org-export-latex-special-chars | |
541 (plist-get org-latex-options-plist :sub-superscript)) | |
542 (org-export-latex-special-keywords-maybe | |
543 org-export-latex-remove-from-headines) | |
544 (org-export-latex-links) | |
545 (org-trim (buffer-substring-no-properties (point-min) (point-max))))) | |
546 | |
547 (defun org-export-latex-content (content) | |
548 "Convert CONTENT string to LaTeX." | |
549 (with-temp-buffer | |
550 (insert content) | |
551 (org-export-latex-quotation-marks) | |
552 (when (plist-get org-latex-options-plist :emphasize) | |
553 (org-export-latex-fontify)) | |
554 (org-export-latex-special-chars | |
555 (plist-get org-latex-options-plist :sub-superscript)) | |
556 (org-export-latex-links) | |
557 (org-export-latex-special-keywords) | |
558 (org-export-latex-itemize) | |
559 (org-export-latex-enumerate) | |
560 (org-export-latex-tables | |
561 (plist-get org-latex-options-plist :tables)) | |
562 (org-export-latex-fixed-width | |
563 (plist-get org-latex-options-plist :fixed-width)) | |
564 (org-export-fix-invisible-strings) | |
565 (buffer-substring (point-min) (point-max)))) | |
566 | |
567 (defun org-export-fix-invisible-strings () | |
568 "Comment out (INVISIBLE) warnings." | |
569 (goto-char (point-min)) | |
570 (while (re-search-forward "(INVISIBLE)" nil t) | |
571 (replace-match "%\\&"))) | |
572 | |
573 (defun org-export-latex-quotation-marks () | |
574 "Export question marks depending on language conventions. | |
575 Local definition of the language overrides | |
576 `org-export-latex-quotation-marks-convention' which overrides | |
577 `org-export-default-language'." | |
578 (let* ((lang (or (plist-get org-latex-options-plist :language) | |
579 org-export-latex-quotation-marks-convention)) | |
580 (quote-rpl (if (equal lang "fr") | |
581 '(("\\(\\s-\\)\"" "«~") | |
582 ("\\(\\S-\\)\"" "~»") | |
583 ("\\(\\s-\\)'" "`")) | |
584 '(("\\(\\s-\\)\"" "``") | |
585 ("\\(\\S-\\)\"" "''") | |
586 ("\\(\\s-\\)'" "`"))))) | |
587 (mapc (lambda(l) (goto-char (point-min)) | |
588 (while (re-search-forward (car l) nil t) | |
589 (let ((rpl (concat (match-string 1) (cadr l)))) | |
590 (org-latex-protect rpl) | |
591 (org-if-unprotected | |
592 (replace-match rpl t t))))) quote-rpl))) | |
593 | |
594 ;; | chars/string in Org | normal environment | math environment | | |
595 ;; |-----------------------+-----------------------+-----------------------| | |
596 ;; | & # % $ | \& \# \% \$ | \& \# \% \$ | | |
597 ;; | { } _ ^ \ | \ { \ } \_ \^ \\ | { } _ ^ \ | | |
598 ;; |-----------------------+-----------------------+-----------------------| | |
599 ;; | a_b and a^b | $a_b$ and $a^b$ | a_b and a^b | | |
600 ;; | a_abc and a_{abc} | $a_a$bc and $a_{abc}$ | a_abc and a_{abc} | | |
601 ;; | \tau and \mu | $\tau$ and $\mu$ | \tau and \mu | | |
602 ;; |-----------------------+-----------------------+-----------------------| | |
603 ;; | \_ \^ | \_ \^ | \_ \^ | | |
604 ;; | \(a=\mu\mbox{m}\) | \(a=\mu\mbox{m}\) | \(a=\mu\mbox{m}\) | | |
605 ;; | \[\beta^2-a=0\] | \[\beta^2-a=0\] | \[\beta^2-a=0\] | | |
606 ;; | $x=22\tau$ | $x=22\tau$ | $x=22\tau$ | | |
607 ;; | $$\alpha=\sqrt{a^3}$$ | $$\alpha=\sqrt{a^3}$$ | $$\alpha=\sqrt{a^3}$$ | | |
608 | |
609 (defun org-export-latex-special-chars (sub-superscript) | |
610 "Export special characters to LaTeX. | |
611 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^. | |
612 See the `org-export-latex.el' code for a complete conversion table." | |
613 (goto-char (point-min)) | |
614 (mapc (lambda(c) | |
615 (goto-char (point-min)) | |
616 (while (re-search-forward c nil t) | |
617 ;; Put the point where to check for org-protected | |
618 (unless (get-text-property (match-beginning 2) 'org-protected) | |
619 (cond ((member (match-string 2) '("\\$" "$")) | |
620 (if (equal (match-string 2) "\\$") | |
621 (replace-match (concat (match-string 1) "$" | |
622 (match-string 3)) t t) | |
623 (replace-match (concat (match-string 1) "\\$" | |
624 (match-string 3)) t t))) | |
625 ((member (match-string 2) '("&" "#" "%")) | |
626 (if (equal (match-string 1) "\\") | |
627 (replace-match (match-string 2) t t) | |
628 (replace-match (concat (match-string 1) "\\" | |
629 (match-string 2)) t t))) | |
630 ((equal (match-string 2) "~") | |
631 (unless (get-text-property 0 'org-protected (match-string 2)) | |
632 (if (equal (match-string 1) "\\") nil | |
633 (replace-match | |
634 (org-latex-protect | |
635 (concat (match-string 1) "\\textasciitilde{}")) t t)))) | |
636 ((member (match-string 2) '("{" "}")) | |
637 (unless (save-match-data (org-inside-LaTeX-fragment-p)) | |
638 (if (equal (match-string 1) "\\") | |
639 (replace-match (match-string 2) t t) | |
640 (replace-match (concat (match-string 1) "\\" | |
641 (match-string 2)) t t))))) | |
642 (unless (save-match-data (org-inside-LaTeX-fragment-p)) | |
643 (cond ((equal (match-string 2) "\\") | |
644 (replace-match (or (save-match-data | |
645 (org-export-latex-treat-backslash-char | |
646 (match-string 1) | |
647 (match-string 3))) "") t t)) | |
648 ((member (match-string 2) '("_" "^")) | |
649 (replace-match (or (save-match-data | |
650 (org-export-latex-treat-sub-super-char | |
651 sub-superscript | |
652 (match-string 1) | |
653 (match-string 2) | |
654 (match-string 3))) "") t t))))))) | |
655 '("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$" | |
656 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)" | |
657 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-za-z&#%{}]+\\)" | |
658 "\\(.\\|^\\)\\(&\\)" | |
659 "\\(.\\|^\\)\\(#\\)" | |
660 "\\(.\\|^\\)\\(%\\)" | |
661 "\\(.\\|^\\)\\({\\)" | |
662 "\\(.\\|^\\)\\(}\\)" | |
663 "\\(.\\|^\\)\\(~\\)"))) | |
664 | |
665 (defun org-export-latex-treat-sub-super-char | |
666 (subsup string-before char string-after) | |
667 "Convert the \"_\" and \"^\" characters to LaTeX. | |
668 SUBSUP corresponds to the ^: option in the #+OPTIONS line. | |
669 Convert CHAR depending on STRING-BEFORE and STRING-AFTER." | |
670 (cond ((equal string-before "\\") | |
671 (concat string-before char string-after)) | |
672 ;; this is part of a math formula | |
673 ((and (string-match "\\S-+" string-before) | |
674 (string-match "\\S-+" string-after)) | |
675 (cond ((get-text-property 0 'org-protected char) | |
676 (concat string-before "\\" char string-after)) | |
677 ((save-match-data (org-inside-LaTeX-fragment-p)) | |
678 (if subsup | |
679 (cond ((eq 1 (length string-after)) | |
680 (concat string-before char string-after)) | |
681 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after) | |
682 (format "%s%s{%s}" string-before char | |
683 (match-string 1 string-after)))))) | |
684 ((and subsup | |
685 (> (length string-after) 1) | |
686 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after)) | |
687 (format "$%s%s{%s}$" string-before char | |
688 (match-string 1 string-after))) | |
689 (subsup (concat "$" string-before char string-after "$")) | |
690 (t (concat string-before char string-after)))) | |
691 (t (concat string-before "\\" char string-after)))) | |
692 | |
693 (defun org-export-latex-treat-backslash-char (string-before string-after) | |
694 "Convert the \"$\" special character to LaTeX. | |
695 The conversion is made depending of STRING-BEFORE and STRING-AFTER." | |
696 (cond ((member (list string-after) org-html-entities) | |
697 ;; backslash is part of a special entity (like "\alpha") | |
698 (concat string-before "$\\" | |
699 (or (cdar (member (list string-after) org-html-entities)) | |
700 string-after) "$")) | |
701 ((and (not (string-match "^[ \n\t]" string-after)) | |
702 (not (string-match "[ \n\t]\\'" string-before))) | |
703 ;; backslash is inside a word | |
704 (concat string-before "$\\backslash$" string-after)) | |
705 ((not (or (equal string-after "") | |
706 (string-match "^[ \t\n]" string-after))) | |
707 ;; backslash might escape a character (like \#) or a user TeX | |
708 ;; macro (like \setcounter) | |
709 (concat string-before "\\" string-after)) | |
710 ((and (string-match "^[ \t\n]" string-after) | |
711 (string-match "[ \t\n]\\'" string-before)) | |
712 ;; backslash is alone, convert it to $\backslash$ | |
713 (concat string-before "$\\backslash$" string-after)) | |
714 (t (concat string-before "$\\backslash$" string-after)))) | |
715 | |
716 (defun org-export-latex-fixed-width (opt) | |
717 "When OPT is non-nil convert fixed-width sections to LaTeX." | |
718 (goto-char (point-min)) | |
719 (while (re-search-forward "^[ \t]*:" nil t) | |
720 (if opt | |
721 (progn (goto-char (match-beginning 0)) | |
722 (insert "\\begin{verbatim}\n") | |
723 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$") | |
724 (replace-match (concat (match-string 1) | |
725 (match-string 2)) t t) | |
726 (forward-line)) | |
727 (insert "\\end{verbatim}\n\n")) | |
728 (progn (goto-char (match-beginning 0)) | |
729 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$") | |
730 (replace-match (concat "%" (match-string 1) | |
731 (match-string 2)) t t) | |
732 (forward-line)))))) | |
733 | |
734 (defun org-export-latex-tables (opt) | |
735 "When OPT is non-nil convert tables to LaTeX." | |
736 (goto-char (point-min)) | |
737 (while (re-search-forward "^\\([ \t]*\\)|" nil t) | |
738 ;; Re-align the table to update org-table-last-alignment | |
739 (save-excursion (save-match-data (org-table-align))) | |
740 (let (tbl-list | |
741 (beg (match-beginning 0)) | |
742 (end (save-excursion | |
743 (re-search-forward | |
744 (concat "^" (regexp-quote (match-string 1)) | |
745 "[^|]\\|\\'") nil t) (match-beginning 0)))) | |
746 (beginning-of-line) | |
747 (while (not (eq end (point))) | |
748 (if (looking-at "[ \t]*|\\([^-|].+\\)|[ \t]*$") | |
749 (push (split-string (org-trim (match-string 1)) "|") tbl-list) | |
750 (push 'hline tbl-list)) | |
751 (forward-line)) | |
752 ;; comment region out instead of deleting it ? | |
753 (apply 'delete-region (list beg end)) | |
754 (when opt (insert (orgtbl-to-latex (nreverse tbl-list) | |
755 nil) "\n\n"))))) | |
756 | |
757 (defun org-export-latex-special-keywords () | |
758 "Convert special keywords to LaTeX. | |
759 Regexps are those from `org-latex-special-string-regexps'." | |
760 (let ((rg org-latex-special-string-regexps) r) | |
761 (while (setq r (pop rg)) | |
762 (goto-char (point-min)) | |
763 (while (re-search-forward (eval r) nil t) | |
764 (replace-match (format "\\\\texttt{%s}" (match-string 0)) t))))) | |
765 | |
766 ;; FIXME - we need better implementation for nested lists | |
767 (defun org-export-latex-list (srch0 srch1 srch2 rpl0 rpl1) | |
768 "Convert lists to LaTeX." | |
769 (goto-char (point-min)) | |
770 (while (re-search-forward srch0 nil t) | |
771 (let* ((beg (match-beginning 0)) | |
772 (prefix (regexp-quote (match-string 1))) | |
773 (end-string (when (re-search-forward srch1 nil t) | |
774 (match-string 0)))) | |
775 (goto-char beg) (insert rpl0) | |
776 (while (re-search-forward | |
777 (concat "^" prefix srch2) | |
778 (if (not end-string) | |
779 (point-max) | |
780 (save-match-data | |
781 (save-excursion | |
782 (re-search-forward | |
783 (regexp-quote end-string) nil t)))) t) | |
784 (replace-match | |
785 (concat "\\item " | |
786 (if (match-string 1) | |
787 (format "\\texttt{%s}" (match-string 1)))) | |
788 t t)) | |
789 (goto-char (if end-string | |
790 (progn (re-search-forward | |
791 (regexp-quote end-string) nil t) | |
792 (match-beginning 0)) | |
793 (point-max))) | |
794 (skip-chars-backward "\n") (forward-line 2) | |
795 (insert rpl1)))) | |
796 | |
797 (defun org-export-latex-itemize () | |
798 "Convert item list to LaTeX." | |
799 (org-export-latex-list | |
800 "^\\([ \t]*\\)-" | |
801 "^[^ \n\t-]+.*$" | |
802 "- ?\\(\\[.+\\]\\)?" | |
803 "\\begin{itemize}\n" | |
804 "\\end{itemize}\n")) | |
805 | |
806 (defun org-export-latex-enumerate () | |
807 "Convert numeric list to LaTeX." | |
808 (org-export-latex-list | |
809 "^\\([ \t]*\\)[0-9]+[\.)] \\(\\[.+\\]\\)? ?" | |
810 "^[^ \n\t0-9]+.*$" | |
811 "[0-9]+[\.)] ?\\(\\[.+\\]\\)?" | |
812 "\\begin{enumerate}\n" | |
813 "\\end{enumerate}\n")) | |
814 | |
815 (defun org-export-latex-fontify () | |
816 "Convert fontification to LaTeX." | |
817 (goto-char (point-min)) | |
818 (while (re-search-forward org-emph-re nil t) | |
819 ;; The match goes one char after the *string* | |
820 (unless (get-text-property (1- (point)) 'org-protected) | |
821 (replace-match | |
822 (concat (match-string 1) | |
823 (format | |
824 (org-export-latex-protect-char-in-string | |
825 '("\\" "{" "}") | |
826 (cadr (assoc (match-string 3) | |
827 org-export-latex-emphasis-alist))) | |
828 (match-string 4)) | |
829 (match-string 5)) t t) | |
830 (backward-char)))) | |
831 | |
832 (defun org-export-latex-protect-char-in-string (char-list string) | |
833 "Add org-protected text-property to char from CHAR-LIST in STRING." | |
834 (with-temp-buffer | |
835 (save-match-data | |
836 (insert string) | |
837 (goto-char (point-min)) | |
838 (while (re-search-forward (regexp-opt char-list) nil t) | |
839 (add-text-properties (match-beginning 0) | |
840 (match-end 0) '(org-protected t))) | |
841 (buffer-string)))) | |
842 | |
843 (defun org-export-latex-links () | |
844 ;; Make sure to use the LaTeX hyperref and graphicx package | |
845 ;; or send some warnings. | |
846 "Convert links to LaTeX." | |
847 (goto-char (point-min)) | |
848 (while (re-search-forward org-bracket-link-analytic-regexp nil t) | |
849 (org-if-unprotected | |
850 (goto-char (match-beginning 0)) | |
851 (let* ((re-radio org-latex-all-targets-regexp) | |
852 (remove (list (match-beginning 0) (match-end 0))) | |
853 (type (match-string 2)) | |
854 (raw-path (match-string 3)) | |
855 (full-raw-path (concat (match-string 1) raw-path)) | |
856 (desc (match-string 5)) | |
857 imgp radiop | |
858 ;; define the path of the link | |
859 (path (cond | |
860 ((member type '("http" "https" "ftp")) | |
861 (concat type ":" raw-path)) | |
862 ((and re-radio (string-match re-radio raw-path)) | |
863 (setq radiop t)) | |
864 ((equal type "mailto") | |
865 (concat type ":" raw-path)) | |
866 ((equal type "file") | |
867 (if (and (or (org-file-image-p (expand-file-name raw-path)) | |
868 (string-match "\\.eps$" raw-path)) | |
869 (equal desc full-raw-path)) | |
870 (setq imgp t) | |
871 (progn (when (string-match "\\(.+\\)::.+" raw-path) | |
872 (setq raw-path (match-string 1 raw-path))) | |
873 (if (file-exists-p raw-path) | |
874 (concat type "://" (expand-file-name raw-path)) | |
875 (concat type "://" (org-export-directory | |
876 :LaTeX org-latex-options-plist) | |
877 raw-path)))))))) | |
878 ;; process with link inserting | |
879 (apply 'delete-region remove) | |
880 (cond ((and imgp (plist-get org-latex-options-plist :inline-images)) | |
881 (insert (format "\\includegraphics[%s]{%s}" | |
882 ;; image option should be set be a comment line | |
883 org-export-latex-image-default-option | |
884 (expand-file-name raw-path)))) | |
885 ;; FIXME: what about caption? image properties? | |
886 (radiop (insert (format "\\hyperref[%s]{%s}" raw-path desc))) | |
887 (path (insert (format "\\href{%s}{%s}" path desc))) | |
888 (t (insert "\\texttt{" desc "}"))))))) | |
889 | |
890 | |
891 (defun org-latex-cleaned-string-for-export (string &rest parameters) | |
892 "Cleanup a buffer STRING so that links can be created safely." | |
893 (interactive) | |
894 (let* ((re-radio (and org-target-link-regexp | |
895 (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)"))) | |
896 (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re)) | |
897 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re)) | |
898 (re-archive (concat ":" org-archive-tag ":")) | |
899 (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>")) | |
900 (htmlp (plist-get parameters :for-html)) | |
901 (latexp (plist-get parameters :for-LaTeX)) | |
902 (commentsp (plist-get parameters :comments)) | |
903 (inhibit-read-only t) | |
904 (outline-regexp "\\*+ ") | |
905 a b xx | |
906 rtn p) | |
907 (save-excursion | |
908 (set-buffer (get-buffer-create " org-mode-tmp")) | |
909 (erase-buffer) | |
910 (insert string) | |
911 ;; Remove license-to-kill stuff | |
912 (while (setq p (text-property-any (point-min) (point-max) | |
913 :org-license-to-kill t)) | |
914 (delete-region p (next-single-property-change p :org-license-to-kill))) | |
915 | |
916 (let ((org-inhibit-startup t)) (org-mode)) | |
917 (untabify (point-min) (point-max)) | |
918 | |
919 ;; Get the correct stuff before the first headline | |
920 (when (plist-get parameters :skip-before-1st-heading) | |
921 (goto-char (point-min)) | |
922 (when (re-search-forward "^\\*+[ \t]" nil t) | |
923 (delete-region (point-min) (match-beginning 0)) | |
924 (goto-char (point-min)) | |
925 (insert "\n"))) | |
926 (when (plist-get parameters :add-text) | |
927 (goto-char (point-min)) | |
928 (insert (plist-get parameters :add-text) "\n")) | |
929 | |
930 ;; Get rid of archived trees | |
931 (when (not (eq org-export-with-archived-trees t)) | |
932 (goto-char (point-min)) | |
933 (while (re-search-forward re-archive nil t) | |
934 (if (not (org-on-heading-p t)) | |
935 (org-end-of-subtree t) | |
936 (beginning-of-line 1) | |
937 (setq a (if org-export-with-archived-trees | |
938 (1+ (point-at-eol)) (point)) | |
939 b (org-end-of-subtree t)) | |
940 (if (> b a) (delete-region a b))))) | |
941 | |
942 ;; Get rid of property drawers | |
943 (unless org-export-with-property-drawer | |
944 (goto-char (point-min)) | |
945 (while (re-search-forward "^[ \t]*:PROPERTIES:[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n" nil t) | |
946 (replace-match ""))) | |
947 | |
948 ;; Find targets in comments and move them out of comments, | |
949 ;; but mark them as targets that should be invisible | |
950 (goto-char (point-min)) | |
951 (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t) | |
952 (replace-match "\\1(INVISIBLE)")) | |
953 | |
954 ;; Specific LaTeX cleaning | |
955 (when latexp | |
956 (require 'org-export-latex nil t) | |
957 (org-export-latex-cleaned-string)) | |
958 | |
959 ;; Protect stuff from HTML processing | |
960 (goto-char (point-min)) | |
961 (let ((formatters `((,htmlp "HTML" "BEGIN_HTML" "END_HTML"))) fmt) | |
962 (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t) | |
963 (add-text-properties (match-beginning 0) (match-end 0) | |
964 '(org-protected t))) | |
965 (while formatters | |
966 (setq fmt (pop formatters)) | |
967 (when (car fmt) | |
968 (goto-char (point-min)) | |
969 (while (re-search-forward (concat "^#\\+" (cadr fmt) | |
970 ":[ \t]*\\(.*\\)") nil t) | |
971 (replace-match "\\1" t) | |
972 (add-text-properties | |
973 (point-at-bol) (min (1+ (point-at-eol)) (point-max)) | |
974 '(org-protected t)))) | |
975 (goto-char (point-min)) | |
976 (while (re-search-forward | |
977 (concat "^#\\+" | |
978 (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+" | |
979 (cadddr fmt) "\\>.*\n?") nil t) | |
980 (if (car fmt) | |
981 (add-text-properties (match-beginning 1) (1+ (match-end 1)) | |
982 '(org-protected t)) | |
983 (delete-region (match-beginning 0) (match-end 0)))) | |
984 (goto-char (point-min)) | |
985 (while (re-search-forward re-quote nil t) | |
986 (goto-char (match-beginning 0)) | |
987 (end-of-line 1) | |
988 (add-text-properties (point) (org-end-of-subtree t) | |
989 '(org-protected t))))) | |
990 | |
991 ;; Remove or replace comments | |
992 ;; If :comments is set, use this char for commenting out comments and | |
993 ;; protect them. otherwise delete them | |
994 (goto-char (point-min)) | |
995 (while (re-search-forward "^#\\(.*\n?\\)" nil t) | |
996 (if commentsp | |
997 (progn (add-text-properties | |
998 (match-beginning 0) (match-end 0) '(org-protected t)) | |
999 (replace-match (format commentsp (match-string 1)) t t)) | |
1000 (replace-match ""))) | |
1001 | |
1002 ;; Find matches for radio targets and turn them into internal links | |
1003 (goto-char (point-min)) | |
1004 (when re-radio | |
1005 (while (re-search-forward re-radio nil t) | |
1006 (org-if-unprotected | |
1007 (replace-match "\\1[[\\2]]")))) | |
1008 | |
1009 ;; Find all links that contain a newline and put them into a single line | |
1010 (goto-char (point-min)) | |
1011 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t) | |
1012 (org-if-unprotected | |
1013 (replace-match "\\1 \\3") | |
1014 (goto-char (match-beginning 0)))) | |
1015 | |
1016 ;; Convert LaTeX fragments to images | |
1017 (when (plist-get parameters :LaTeX-fragments) | |
1018 (org-format-latex | |
1019 (concat "ltxpng/" (file-name-sans-extension | |
1020 (file-name-nondirectory | |
1021 org-current-export-file))) | |
1022 org-current-export-dir nil "Creating LaTeX image %s")) | |
1023 (message "Exporting...") | |
1024 | |
1025 ;; Normalize links: Convert angle and plain links into bracket links | |
1026 ;; Expand link abbreviations | |
1027 (goto-char (point-min)) | |
1028 (while (re-search-forward re-plain-link nil t) | |
1029 (goto-char (1- (match-end 0))) | |
1030 (org-if-unprotected | |
1031 (let* ((s (concat (match-string 1) "[[" (match-string 2) | |
1032 ":" (match-string 3) "]]"))) | |
1033 ;; added 'org-protected property to links | |
1034 (add-text-properties 0 (length s) '(org-protected t) s) | |
1035 (replace-match s t t)))) | |
1036 (goto-char (point-min)) | |
1037 (while (re-search-forward re-angle-link nil t) | |
1038 (goto-char (1- (match-end 0))) | |
1039 (org-if-unprotected | |
1040 (let* ((s (concat (match-string 1) "[[" (match-string 2) | |
1041 ":" (match-string 3) "]]"))) | |
1042 (add-text-properties 0 (length s) '(org-protected t) s) | |
1043 (replace-match s t t)))) | |
1044 (goto-char (point-min)) | |
1045 (while (re-search-forward org-bracket-link-regexp nil t) | |
1046 (org-if-unprotected | |
1047 (let* ((s (concat "[[" (setq xx (save-match-data | |
1048 (org-link-expand-abbrev (match-string 1)))) | |
1049 "]" | |
1050 (if (match-end 3) | |
1051 (match-string 2) | |
1052 (concat "[" xx "]")) | |
1053 "]"))) | |
1054 (add-text-properties 0 (length s) '(org-protected t) s) | |
1055 (replace-match s t t)))) | |
1056 | |
1057 ;; Find multiline emphasis and put them into single line | |
1058 (when (plist-get parameters :emph-multiline) | |
1059 (goto-char (point-min)) | |
1060 (while (re-search-forward org-emph-re nil t) | |
1061 (if (not (= (char-after (match-beginning 3)) | |
1062 (char-after (match-beginning 4)))) | |
1063 (org-if-unprotected | |
1064 (subst-char-in-region (match-beginning 0) (match-end 0) | |
1065 ?\n ?\ t) | |
1066 (goto-char (1- (match-end 0)))) | |
1067 (goto-char (1+ (match-beginning 0)))))) | |
1068 | |
1069 (setq rtn (buffer-string))) | |
1070 (kill-buffer " org-mode-tmp") | |
1071 rtn)) | |
1072 | |
1073 (defsubst org-latex-protect (string) | |
1074 (add-text-properties 0 (length string) '(org-protected t) string) | |
1075 string) | |
1076 | |
1077 (defun org-export-latex-cleaned-string () | |
1078 "Clean stuff in the LaTeX export." | |
1079 | |
1080 ;; preserve line breaks | |
1081 (goto-char (point-min)) | |
1082 (while (re-search-forward "\\\\\\\\" nil t) | |
1083 (add-text-properties (match-beginning 0) (match-end 0) | |
1084 '(org-protected t))) | |
1085 | |
1086 ;; convert LaTeX to @LaTeX{} | |
1087 (goto-char (point-min)) | |
1088 (let ((case-fold-search nil) rpl) | |
1089 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t) | |
1090 (replace-match (org-latex-protect | |
1091 (concat (match-string 1) "\\LaTeX{}")) t t))) | |
1092 | |
1093 ;; convert horizontal rules | |
1094 (goto-char (point-min)) | |
1095 (while (re-search-forward "^----+.$" nil t) | |
1096 (replace-match (org-latex-protect "\\hrule") t t)) | |
1097 | |
1098 ;; Remove COMMENT subtrees | |
1099 ;; What about QUOTE subtrees? | |
1100 (goto-char (point-min)) | |
1101 (while (re-search-forward | |
1102 (concat "^\\*+ \\(" org-comment-string "\\)") | |
1103 nil t) | |
1104 (beginning-of-line) | |
1105 (org-cut-subtree)) | |
1106 | |
1107 ;; protect LaTeX \commands{...} | |
1108 (goto-char (point-min)) | |
1109 (while (re-search-forward "\\\\[a-z]+{.+}" nil t) | |
1110 (add-text-properties (match-beginning 0) (match-end 0) | |
1111 '(org-protected t))) | |
1112 | |
1113 ;; Replace radio links | |
1114 (goto-char (point-min)) | |
1115 (let ((search (concat "<<<?" org-latex-all-targets-regexp ">?>>"))) | |
1116 (while (re-search-forward search nil t) | |
1117 (replace-match | |
1118 (org-latex-protect (format "\\label{%s}" (match-string 1))) t t))) | |
1119 | |
1120 ;; delete @<br /> cookies | |
1121 (goto-char (point-min)) | |
1122 (while (re-search-forward "@<[^<>\n]*>" nil t) | |
1123 (replace-match "")) | |
1124 | |
1125 ;; add #+BEGIN_LaTeX before any \begin{...} | |
1126 (goto-char (point-min)) | |
1127 (while (re-search-forward "^ *\\\\begin{" nil t) | |
1128 (replace-match "#+BEGIN_LaTeX:\n\\&" t)) | |
1129 | |
1130 ;; add #+END_LaTeX after any \end{...} | |
1131 (goto-char (point-min)) | |
1132 (while (re-search-forward "^ *\\\\end{.+}.*$" nil t) | |
1133 (replace-match "\\&\n#+END_LaTeX" t)) | |
1134 | |
1135 ;; When converting to LaTeX, replace footnotes | |
1136 ;; FIXME: don't protect footnotes from conversion | |
1137 (when (plist-get org-latex-options-plist :footnotes) | |
1138 (goto-char (point-min)) | |
1139 (while (re-search-forward "\\[[0-9]+\\]" nil t) | |
1140 (when (save-match-data | |
1141 (save-excursion (beginning-of-line) | |
1142 (looking-at "[^:|]"))) | |
1143 (let ((foot-beg (match-beginning 0)) | |
1144 (foot-end (match-end 0)) | |
1145 (foot-prefix (match-string 0)) | |
1146 footnote footnote-rpl) | |
1147 (when (and (re-search-forward (regexp-quote foot-prefix) nil t)) | |
1148 (replace-match "") | |
1149 (let ((end (save-excursion | |
1150 (if (re-search-forward "^$\\|\\[[0-9]+\\]" nil t) | |
1151 (match-beginning 0) (point-max))))) | |
1152 (setq footnote (concat | |
1153 (org-trim (buffer-substring (point) end)) | |
1154 ;; FIXME stupid workaround for cases where | |
1155 ;; `org-bracket-link-analytic-regexp' matches | |
1156 ;; }. as part of the link. | |
1157 " ")) | |
1158 (delete-region (point) end))) | |
1159 (goto-char foot-beg) | |
1160 (delete-region foot-beg foot-end) | |
1161 (setq footnote-rpl (format "\\footnote{%s}" footnote)) | |
1162 (add-text-properties 0 1 '(org-protected t) footnote-rpl) | |
1163 (add-text-properties 9 10 '(org-protected t) footnote-rpl) | |
1164 (add-text-properties (1- (length footnote-rpl)) | |
1165 (length footnote-rpl) | |
1166 '(org-protected t) footnote-rpl) | |
1167 (insert footnote-rpl)))) | |
1168 | |
1169 ;; Replace footnote section tag for LaTeX | |
1170 (goto-char (point-min)) | |
1171 (while (re-search-forward | |
1172 (concat "^" footnote-section-tag-regexp) nil t) | |
1173 (replace-match ""))) | |
1174 | |
1175 ;; Protect stuff from LaTeX processing. | |
1176 ;; We will get rid on this once org.el integrate org-export-latex.el | |
1177 ;; FIXME: #+LaTeX should be aware of the preceeding indentation in lists | |
1178 (goto-char (point-min)) | |
1179 (let ((formatters `((,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX"))) fmt) | |
1180 (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t) | |
1181 (add-text-properties (match-beginning 0) (match-end 0) | |
1182 '(org-protected t))) | |
1183 (while formatters | |
1184 (setq fmt (pop formatters)) | |
1185 (when (car fmt) | |
1186 (goto-char (point-min)) | |
1187 (while (re-search-forward (concat "^#\\+" (cadr fmt) | |
1188 ":[ \t]*\\(.*\\)") nil t) | |
1189 (replace-match "\\1" t) | |
1190 (add-text-properties | |
1191 (point-at-bol) (min (1+ (point-at-eol)) (point-max)) | |
1192 '(org-protected t)))) | |
1193 (goto-char (point-min)) | |
1194 (while (re-search-forward | |
1195 (concat "^#\\+" | |
1196 (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+" | |
1197 (cadddr fmt) "\\>.*\n?") nil t) | |
1198 (if (car fmt) | |
1199 (add-text-properties (match-beginning 1) (1+ (match-end 1)) | |
1200 '(org-protected t)) | |
1201 (delete-region (match-beginning 0) (match-end 0)))) | |
1202 (goto-char (point-min)) | |
1203 (while (re-search-forward re-quote nil t) | |
1204 (goto-char (match-beginning 0)) | |
1205 (end-of-line 1) | |
1206 (add-text-properties (point) (org-end-of-subtree t) | |
1207 '(org-protected t)))))) | |
1208 | |
1209 (provide 'org-export-latex) | |
1210 | |
82753 | 1211 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad |
82752 | 1212 ;;; org-export-latex.el ends here |