1 ;;; org-export-latex.el --- LaTeX exporter for org-mode
3 ;; Copyright (C) 2007, 2008 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: org-export-latex.el
8 ;; Author: Bastien Guerry <bzg AT altern DOT org>
9 ;; Maintainer: Bastien Guerry <bzg AT altern DOT org>
10 ;; Keywords: org, wp, tex
11 ;; Description: Converts an org-mode buffer into LaTeX
12 ;; URL: http://www.cognition.ens.fr/~guerry/u/org-export-latex.el
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 ;; This library implements a LaTeX exporter for org-mode.
33 ;; Put this file into your load-path and the following into your ~/.emacs:
34 ;; (require 'org-export-latex)
36 ;; The interactive functions are similar to those of the HTML exporter:
38 ;; M-x `org-export-as-latex'
39 ;; M-x `org-export-as-latex-batch'
40 ;; M-x `org-export-as-latex-to-buffer'
41 ;; M-x `org-export-region-as-latex'
42 ;; M-x `org-replace-region-by-latex'
54 (defvar org-export-latex-class nil
)
55 (defvar org-export-latex-header nil
)
56 (defvar org-export-latex-append-header nil
)
57 (defvar org-export-latex-options-plist nil
)
58 (defvar org-export-latex-todo-keywords-1 nil
)
59 (defvar org-export-latex-all-targets-re nil
)
60 (defvar org-export-latex-add-level
0)
61 (defvar org-export-latex-sectioning
"")
62 (defvar org-export-latex-sectioning-depth
0)
63 (defvar org-export-latex-special-string-regexps
68 "A list of regexps to convert as special keywords.")
70 (defvar latexp
) ; dynamically scoped from org.el
71 (defvar re-quote
) ; dynamically scoped from org.el
72 (defvar commentsp
) ; dynamically scoped from org.el
76 (defgroup org-export-latex nil
77 "Options for exporting Org-mode files to LaTeX."
78 :tag
"Org Export LaTeX"
81 (defcustom org-export-latex-default-class
"article"
82 "The default LaTeX class."
83 :group
'org-export-latex
84 :type
'(string :tag
"LaTeX class"))
86 (defcustom org-export-latex-classes
88 "\\documentclass[11pt,a4paper]{article}
89 \\usepackage[utf8]{inputenc}
90 \\usepackage[T1]{fontenc}
91 \\usepackage{hyperref}"
92 ("\\section{%s}" .
"\\section*{%s}")
93 ("\\subsection{%s}" .
"\\subsection*{%s}")
94 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
95 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
96 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
98 "\\documentclass[11pt,a4paper]{report}
99 \\usepackage[utf8]{inputenc}
100 \\usepackage[T1]{fontenc}
101 \\usepackage{hyperref}"
102 ("\\part{%s}" .
"\\part*{%s}")
103 ("\\chapter{%s}" .
"\\chapter*{%s}")
104 ("\\section{%s}" .
"\\section*{%s}")
105 ("\\subsection{%s}" .
"\\subsection*{%s}")
106 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
108 "\\documentclass[11pt,a4paper]{book}
109 \\usepackage[utf8]{inputenc}
110 \\usepackage[T1]{fontenc}
111 \\usepackage{hyperref}"
112 ("\\part{%s}" .
"\\part*{%s}")
113 ("\\chapter{%s}" .
"\\chapter*{%s}")
114 ("\\section{%s}" .
"\\section*{%s}")
115 ("\\subsection{%s}" .
"\\subsection*{%s}")
116 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
117 "Alist of LaTeX classes and associated header and structure.
118 If #+LaTeX_CLASS is set in the buffer, use its value and the
119 associated information. Here is the structure of each cell:
123 (unnumbered-section numbered-section\)
126 A %s formatter is mandatory in each section string and will be
127 replaced by the title of the section."
128 :group
'org-export-latex
130 (list (string :tag
"LaTeX class")
131 (string :tag
"LaTeX header")
133 (string :tag
"Numbered")
134 (string :tag
"Unnumbered"))
136 (string :tag
"Numbered")
137 (string :tag
"Unnumbered"))
139 (string :tag
"Numbered")
140 (string :tag
"Unnumbered"))
142 (string :tag
"Numbered")
143 (string :tag
"Unnumbered"))
145 (string :tag
"Numbered")
146 (string :tag
"Unnumbered")))))
148 (defcustom org-export-latex-emphasis-alist
149 '(("*" "\\textbf{%s}" nil
)
150 ("/" "\\emph{%s}" nil
)
151 ("_" "\\underline{%s}" nil
)
152 ("+" "\\texttt{%s}" nil
)
153 ("=" "\\texttt{%s}" nil
)
154 ("~" "\\texttt{%s}" t
))
155 "Alist of LaTeX expressions to convert emphasis fontifiers.
156 Each element of the list is a list of three elements.
157 The first element is the character used as a marker for fontification.
158 The second element is a formatting string to wrap fontified text with.
159 The third element decides whether to protect converted text from other
161 :group
'org-export-latex
164 (defcustom org-export-latex-title-command
"\\maketitle"
165 "The command used to insert the title just after \\begin{document}.
166 If this string contains the formatting specification \"%s\" then
167 it will be used as a formatting string, passing the title as an
169 :group
'org-export-latex
172 (defcustom org-export-latex-date-format
174 "Format string for \\date{...}."
175 :group
'org-export-latex
178 (defcustom org-export-latex-tables-verbatim nil
179 "When non-nil, tables are exported verbatim."
180 :group
'org-export-latex
183 (defcustom org-export-latex-tables-column-borders nil
184 "When non-nil, group of columns are surrounded with borders."
185 :group
'org-export-latex
188 (defcustom org-export-latex-packages-alist nil
189 "Alist of packages to be inserted in the header.
190 Each cell is of the forma \( \"option\" . \"package\" \)."
191 :group
'org-export-latex
194 (defcustom org-export-latex-low-levels
'description
195 "How to convert sections below the current level of sectioning.
196 This is specified by the `org-export-headline-levels' option or the
197 value of \"H:\" in Org's #+OPTION line.
199 This can be either nil (skip the sections), 'description (convert
200 the sections as descriptive lists) or a string to be used instead
201 of \\section{%s}. In this latter case, the %s stands here for the
202 inserted headline and is mandatory."
203 :group
'org-export-latex
204 :type
'(choice (const :tag
"Ignore" nil
)
205 (symbol :tag
"Convert as descriptive list" description
)
206 (string :tag
"Use a section string" :value
"\\subparagraph{%s}")))
208 (defcustom org-export-latex-remove-from-headlines
209 '(:todo t
:priority t
:tags t
)
210 "A plist of keywords to remove from headlines.
211 Non-nil means remove this keyword type from the headline.
213 Don't remove the keys, just change their values."
215 :group
'org-export-latex
)
217 (defcustom org-export-latex-image-default-option
"width=10em"
218 "Default option for images."
219 :group
'org-export-latex
222 (defcustom org-export-latex-coding-system nil
223 "Coding system for the exported LaTex file."
224 :group
'org-export-latex
225 :type
'coding-system
)
227 ;;; Autoload functions:
230 (defun org-export-as-latex-batch ()
231 "Call `org-export-as-latex', may be used in batch processing.
235 --load=$HOME/lib/emacs/org.el
236 --eval \"(setq org-export-headline-levels 2)\"
237 --visit=MyFile --funcall org-export-as-latex-batch"
238 (org-export-as-latex org-export-headline-levels
'hidden
))
241 (defun org-export-as-latex-to-buffer (arg)
242 "Call `org-exort-as-latex` with output to a temporary buffer.
243 No file is created. The prefix ARG is passed through to `org-export-as-latex'."
245 (org-export-as-latex arg nil nil
"*Org LaTeX Export*")
246 (switch-to-buffer-other-window "*Org LaTeX Export*"))
249 (defun org-replace-region-by-latex (beg end
)
250 "Replace the region from BEG to END with its LaTeX export.
251 It assumes the region has `org-mode' syntax, and then convert it to
252 LaTeX. This can be used in any buffer. For example, you could
253 write an itemized list in `org-mode' syntax in an LaTeX buffer and
254 then use this command to convert it."
257 (save-window-excursion
259 (setq latex
(org-export-region-as-latex
261 (setq reg
(buffer-substring beg end
)
262 buf
(get-buffer-create "*Org tmp*"))
268 (setq latex
(org-export-region-as-latex
269 (point-min) (point-max) t
'string
)))
271 (delete-region beg end
)
275 (defun org-export-region-as-latex (beg end
&optional body-only buffer
)
276 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
277 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
278 contents, and only produce the region of converted text, useful for
279 cut-and-paste operations.
280 If BUFFER is a buffer or a string, use/create that buffer as a target
281 of the converted LaTeX. If BUFFER is the symbol `string', return the
282 produced LaTeX as a string and leave not buffer behind. For example,
283 a Lisp program could call this function in the following way:
285 (setq latex (org-export-region-as-latex beg end t 'string))
287 When called interactively, the output buffer is selected, and shown
288 in a window. A non-interactive call will only retunr the buffer."
290 (when (interactive-p)
291 (setq buffer
"*Org LaTeX Export*"))
292 (let ((transient-mark-mode t
) (zmacs-regions t
)
295 (set-mark (point)) ;; to activate the region
297 (setq rtn
(org-export-as-latex
300 (if (fboundp 'deactivate-mark
) (deactivate-mark))
301 (if (and (interactive-p) (bufferp rtn
))
302 (switch-to-buffer-other-window rtn
)
306 (defun org-export-as-latex (arg &optional hidden ext-plist
307 to-buffer body-only pub-dir
)
308 "Export current buffer to a LaTeX file.
309 If there is an active region, export only the region. The prefix
310 ARG specifies how many levels of the outline should become
311 headlines. The default is 3. Lower levels will be exported
312 depending on `org-export-latex-low-levels'. The default is to
313 convert them as description lists. When HIDDEN is non-nil, don't
314 display the LaTeX buffer. EXT-PLIST is a property list with
315 external parameters overriding org-mode's default settings, but
316 still inferior to file-local settings. When TO-BUFFER is
317 non-nil, create a buffer with that name and export to that
318 buffer. If TO-BUFFER is the symbol `string', don't leave any
319 buffer behind but just return the resulting LaTeX as a string.
320 When BODY-ONLY is set, don't produce the file header and footer,
321 simply return the content of \begin{document}...\end{document},
322 without even the \begin{document} and \end{document} commands.
323 when PUB-DIR is set, use this as the publishing directory."
325 ;; Make sure we have a file name when we need it.
326 (when (and (not (or to-buffer body-only
))
327 (not buffer-file-name
))
328 (if (buffer-base-buffer)
329 (org-set-local 'buffer-file-name
330 (with-current-buffer (buffer-base-buffer)
332 (error "Need a file name to be able to export")))
334 (message "Exporting to LaTeX...")
335 (org-update-radio-target-regexp)
336 (org-export-latex-set-initial-vars ext-plist arg
)
337 (let* ((wcf (current-window-configuration))
338 (opt-plist org-export-latex-options-plist
)
339 (region-p (org-region-active-p))
340 (rbeg (and region-p
(region-beginning)))
341 (rend (and region-p
(region-end)))
346 (and (org-at-heading-p)
347 (>= (org-end-of-subtree t t
) rend
)))))
348 (opt-plist (if subtree-p
349 (org-export-add-subtree-options opt-plist rbeg
)
351 ;; Make sure the variable contains the updated values.
352 (org-export-latex-options-plist opt-plist
)
353 (title (or (and subtree-p
(org-export-get-title-from-subtree))
354 (plist-get opt-plist
:title
)
356 (plist-get opt-plist
:skip-before-1st-heading
))
357 (org-export-grab-title-from-buffer))
358 (file-name-sans-extension
359 (file-name-nondirectory buffer-file-name
))))
360 (filename (concat (file-name-as-directory
362 (org-export-directory :LaTeX ext-plist
)))
363 (file-name-sans-extension
365 (org-entry-get rbeg
"EXPORT_FILE_NAME" t
))
366 (file-name-nondirectory ;sans-extension
369 (filename (if (equal (file-truename filename
)
370 (file-truename buffer-file-name
))
371 (concat filename
".tex")
373 (buffer (if to-buffer
375 ((eq to-buffer
'string
) (get-buffer-create
376 "*Org LaTeX Export*"))
377 (t (get-buffer-create to-buffer
)))
378 (find-file-noselect filename
)))
379 (odd org-odd-levels-only
)
380 (header (org-export-latex-make-header title opt-plist
))
381 (skip (cond (subtree-p nil
)
383 ;; never skip first lines when exporting a subtree
384 (t (plist-get opt-plist
:skip-before-1st-heading
))))
385 (text (plist-get opt-plist
:text
))
386 (first-lines (if skip
"" (org-export-latex-first-lines)))
387 (coding-system (and (boundp 'buffer-file-coding-system
)
388 buffer-file-coding-system
))
389 (coding-system-for-write (or org-export-latex-coding-system
391 (save-buffer-coding-system (or org-export-latex-coding-system
393 (region (buffer-substring
394 (if region-p
(region-beginning) (point-min))
395 (if region-p
(region-end) (point-max))))
397 (org-export-preprocess-string
398 region
:emph-multiline t
401 :add-text
(if (eq to-buffer
'string
) nil text
)
402 :skip-before-1st-heading skip
403 :select-tags
(plist-get opt-plist
:select-tags
)
404 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
405 :LaTeX-fragments nil
)))
410 (and (fboundp 'set-buffer-file-coding-system
)
411 (set-buffer-file-coding-system coding-system-for-write
))
413 ;; insert the header and initial document commands
414 (unless (or (eq to-buffer
'string
) body-only
)
417 ;; insert text found in #+TEXT
418 (when (and text
(not (eq to-buffer
'string
)))
419 (insert (org-export-latex-content
420 text
'(lists tables fixed-width keywords
))
423 ;; insert lines before the first headline
424 (unless (or skip
(eq to-buffer
'string
))
425 (insert first-lines
))
427 ;; handle the case where the region does not begin with a section
429 (insert (with-temp-buffer
430 (insert string-for-export
)
431 (org-export-latex-first-lines))))
433 ;; export the content of headlines
434 (org-export-latex-global
436 (insert string-for-export
)
437 (goto-char (point-min))
438 (when (re-search-forward "^\\(\\*+\\) " nil t
)
439 (let* ((asters (length (match-string 1)))
440 (level (if odd
(- asters
2) (- asters
1))))
441 (setq org-export-latex-add-level
442 (if odd
(1- (/ (1+ asters
) 2)) (1- asters
)))
443 (org-export-latex-parse-global level odd
)))))
446 (unless body-only
(insert "\n\\end{document}"))
447 (or to-buffer
(save-buffer))
448 (goto-char (point-min))
449 (message "Exporting to LaTeX...done")
451 (if (eq to-buffer
'string
)
452 (prog1 (buffer-substring (point-min) (point-max))
453 (kill-buffer (current-buffer)))
455 (set-window-configuration wcf
))))
457 ;;; Parsing functions:
459 (defun org-export-latex-parse-global (level odd
)
460 "Parse the current buffer recursively, starting at LEVEL.
461 If ODD is non-nil, assume the buffer only contains odd sections.
462 Return a list reflecting the document structure."
464 (goto-char (point-min))
465 (let* ((cnt 0) output
466 (depth org-export-latex-sectioning-depth
))
467 (while (re-search-forward
468 (concat "^\\(\\(?:\\*\\)\\{"
469 (number-to-string (+ (if odd
2 1) level
))
471 ;; make sure that there is no upper heading
476 (concat "^\\(\\(?:\\*\\)\\{"
477 (number-to-string level
)
478 "\\}\\) \\(.*\\)$") nil t
)))) t
)
480 (let* ((pos (match-beginning 0))
481 (heading (match-string 2))
482 (nlevel (if odd
(/ (+ 3 level
) 2) (1+ level
))))
487 (if (re-search-forward
488 (concat "^\\(\\(?:\\*\\)\\{"
489 (number-to-string (+ (if odd
2 1) level
))
490 "\\}\\) \\(.*\\)$") nil t
)
493 (goto-char (point-min))
501 `(heading .
,heading
)
502 `(content .
,(org-export-latex-parse-content))
503 `(subcontent .
,(org-export-latex-parse-subcontent
508 (defun org-export-latex-parse-content ()
509 "Extract the content of a section."
511 (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t
)
512 (progn (beginning-of-line) (point))
514 (buffer-substring beg end
)))
516 (defun org-export-latex-parse-subcontent (level odd
)
517 "Extract the subcontent of a section at LEVEL.
518 If ODD Is non-nil, assume subcontent only contains odd sections."
519 (if (not (re-search-forward
520 (concat "^\\(\\(?:\\*\\)\\{"
521 (number-to-string (+ (if odd
4 2) level
))
524 nil
; subcontent is nil
525 (org-export-latex-parse-global (+ (if odd
2 1) level
) odd
)))
527 ;;; Rendering functions:
528 (defun org-export-latex-global (content)
529 "Export CONTENT to LaTeX.
530 CONTENT is an element of the list produced by
531 `org-export-latex-parse-global'."
532 (if (eq (car content
) 'subcontent
)
533 (mapc 'org-export-latex-sub
(cdr content
))
534 (org-export-latex-sub (car content
))))
536 (defun org-export-latex-sub (subcontent)
537 "Export the list SUBCONTENT to LaTeX.
538 SUBCONTENT is an alist containing information about the headline
540 (let ((num (plist-get org-export-latex-options-plist
:section-numbers
)))
541 (mapc (lambda(x) (org-export-latex-subcontent x num
)) subcontent
)))
543 (defun org-export-latex-subcontent (subcontent num
)
544 "Export each cell of SUBCONTENT to LaTeX.
545 If NUM, export sections as numerical sections."
546 (let ((heading (org-export-latex-fontify-headline
547 (cdr (assoc 'heading subcontent
))))
548 (level (- (cdr (assoc 'level subcontent
))
549 org-export-latex-add-level
))
550 (occur (number-to-string (cdr (assoc 'occur subcontent
))))
551 (content (cdr (assoc 'content subcontent
)))
552 (subcontent (cadr (assoc 'subcontent subcontent
))))
555 ((<= level org-export-latex-sectioning-depth
)
556 (let ((sec (nth (1- level
) org-export-latex-sectioning
)))
557 (insert (format (if num
(car sec
) (cdr sec
)) heading
) "\n"))
558 (insert (org-export-latex-content content
))
559 (cond ((stringp subcontent
) (insert subcontent
))
560 ((listp subcontent
) (org-export-latex-sub subcontent
))))
561 ;; At a level under the hl option: we can drop this subsection
562 ((> level org-export-latex-sectioning-depth
)
563 (cond ((eq org-export-latex-low-levels
'description
)
564 (insert (format "\\begin{description}\n\n\\item[%s]\n\n" heading
))
565 (insert (org-export-latex-content content
))
566 (cond ((stringp subcontent
) (insert subcontent
))
567 ((listp subcontent
) (org-export-latex-sub subcontent
)))
568 (insert "\\end{description}\n"))
569 ((stringp org-export-latex-low-levels
)
570 (insert (format org-export-latex-low-levels heading
) "\n")
571 (insert (org-export-latex-content content
))
572 (cond ((stringp subcontent
) (insert subcontent
))
573 ((listp subcontent
) (org-export-latex-sub subcontent
)))))))))
575 ;;; Exporting internals:
576 (defun org-export-latex-set-initial-vars (ext-plist level
)
577 "Store org local variables required for LaTeX export.
578 EXT-PLIST is an optional additional plist.
579 LEVEL indicates the default depth for export."
580 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
581 org-export-latex-all-targets-re
582 (org-make-target-link-regexp (org-all-targets))
583 org-export-latex-options-plist
584 (org-combine-plists (org-default-export-plist) ext-plist
585 (org-infile-export-plist))
586 org-export-latex-class
588 (goto-char (point-min))
589 (if (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t
)
590 (assoc (match-string 1) org-export-latex-classes
))
592 org-export-latex-default-class
))
593 org-export-latex-header
594 (cadr (assoc org-export-latex-class org-export-latex-classes
))
595 org-export-latex-sectioning
596 (cddr (assoc org-export-latex-class org-export-latex-classes
))
597 org-export-latex-sectioning-depth
600 (plist-get org-export-latex-options-plist
:headline-levels
))
601 (sec-depth (length org-export-latex-sectioning
)))
602 (if (> hl-levels sec-depth
) sec-depth hl-levels
)))))
604 (defun org-export-latex-make-header (title opt-plist
)
605 "Make the LaTeX header and return it as a string.
606 TITLE is the current title from the buffer or region.
607 OPT-PLIST is the options plist for current buffer."
608 (let ((toc (plist-get opt-plist
:table-of-contents
))
609 (author (plist-get opt-plist
:author
)))
611 (if (plist-get opt-plist
:time-stamp-file
)
612 (format-time-string "% Created %Y-%m-%d %a %H:%M\n"))
613 ;; insert LaTeX custom header
614 org-export-latex-header
616 ;; insert information on LaTeX packages
617 (when org-export-latex-packages-alist
618 (mapconcat (lambda(p)
619 (if (equal "" (car p
))
620 (format "\\usepackage{%s}" (cadr p
))
621 (format "\\usepackage[%s]{%s}"
623 org-export-latex-packages-alist
"\n"))
624 ;; insert additional commands in the header
625 org-export-latex-append-header
630 (org-export-latex-content
631 title
'(lists tables fixed-width keywords
)))
632 ;; insert author info
633 (if (plist-get opt-plist
:author-info
)
634 (format "\\author{%s}\n"
635 (or author user-full-name
))
636 (format "%%\\author{%s}\n"
637 (or author user-full-name
)))
639 (format "\\date{%s}\n"
641 (or (plist-get opt-plist
:date
)
642 org-export-latex-date-format
)))
643 ;; beginning of the document
644 "\n\\begin{document}\n\n"
645 ;; insert the title command
646 (if (string-match "%s" org-export-latex-title-command
)
647 (format org-export-latex-title-command title
)
648 org-export-latex-title-command
)
651 (when (and org-export-with-toc
652 (plist-get opt-plist
:section-numbers
))
654 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
655 (min toc
(plist-get opt-plist
:headline-levels
))))
656 (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
657 (plist-get opt-plist
:headline-levels
))))))))
659 (defun org-export-latex-first-lines (&optional comments
)
660 "Export the first lines before first headline.
661 COMMENTS is either nil to replace them with the empty string or a
662 formatting string like %%%%s if we want to comment them out."
664 (goto-char (point-min))
665 (if (org-at-heading-p) (beginning-of-line 2))
667 (end (if (and (re-search-forward "^\\* " nil t
)
668 (not (eq pt
(match-beginning 0))))
669 (goto-char (match-beginning 0))
670 (goto-char (point-max)))))
671 (org-export-latex-content
672 (org-export-preprocess-string
673 (buffer-substring (point-min) end
)
678 :skip-before-1st-heading nil
679 :LaTeX-fragments nil
)))))
681 (defun org-export-latex-content (content &optional exclude-list
)
682 "Convert CONTENT string to LaTeX.
683 Don't perform conversions that are in EXCLUDE-LIST. Recognized
684 conversion types are: quotation-marks, emphasis, sub-superscript,
685 links, keywords, lists, tables, fixed-width"
688 (unless (memq 'quotation-marks exclude-list
)
689 (org-export-latex-quotation-marks))
690 (unless (memq 'emphasis exclude-list
)
691 (when (plist-get org-export-latex-options-plist
:emphasize
)
692 (org-export-latex-fontify)))
693 (unless (memq 'sub-superscript exclude-list
)
694 (org-export-latex-special-chars
695 (plist-get org-export-latex-options-plist
:sub-superscript
)))
696 (unless (memq 'links exclude-list
)
697 (org-export-latex-links))
698 (unless (memq 'keywords exclude-list
)
699 (org-export-latex-keywords
700 (plist-get org-export-latex-options-plist
:timestamps
)))
701 (unless (memq 'lists exclude-list
)
702 (org-export-latex-lists))
703 (unless (memq 'tables exclude-list
)
704 (org-export-latex-tables
705 (plist-get org-export-latex-options-plist
:tables
)))
706 (unless (memq 'fixed-width exclude-list
)
707 (org-export-latex-fixed-width
708 (plist-get org-export-latex-options-plist
:fixed-width
)))
710 (buffer-substring (point-min) (point-max))))
712 (defun org-export-latex-protect-string (s)
713 "Add the org-protected property to string S."
714 (add-text-properties 0 (length s
) '(org-protected t
) s
) s
)
716 (defun org-export-latex-protect-char-in-string (char-list string
)
717 "Add org-protected text-property to char from CHAR-LIST in STRING."
721 (goto-char (point-min))
722 (while (re-search-forward (regexp-opt char-list
) nil t
)
723 (add-text-properties (match-beginning 0)
724 (match-end 0) '(org-protected t
)))
727 (defun org-export-latex-keywords-maybe (remove-list)
728 "Maybe remove keywords depending on rules in REMOVE-LIST."
729 (goto-char (point-min))
730 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1
"\\|"))
731 (case-fold-search nil
))
732 ;; convert TODO keywords
733 (when (re-search-forward (concat "^\\(" re-todo
"\\)") nil t
)
734 (if (plist-get remove-list
:todo
)
736 (replace-match (format "\\texttt{%s}" (match-string 1)) t t
)))
737 ;; convert priority string
738 (when (re-search-forward "\\[\\\\#.\\]" nil t
)
739 (if (plist-get remove-list
:priority
)
741 (replace-match (format "\\texttt{%s}" (match-string 0)) t t
)))
743 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t
)
744 (if (or (not org-export-with-tags
)
745 (plist-get remove-list
:tags
))
748 (org-export-latex-protect-string
749 (format "\\texttt{%s}"
751 (replace-regexp-in-string
752 "_" "\\\\_" (match-string 0)))))
755 (defun org-export-latex-fontify-headline (string)
756 "Fontify special words in STRING."
758 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
759 ;; the beginning of the buffer - inserting "\n" is safe here though.
761 (goto-char (point-min))
762 (when (plist-get org-export-latex-options-plist
:emphasize
)
763 (org-export-latex-fontify))
764 (org-export-latex-keywords-maybe
765 org-export-latex-remove-from-headlines
)
766 (org-export-latex-special-chars
767 (plist-get org-export-latex-options-plist
:sub-superscript
))
768 (org-export-latex-links)
769 (org-trim (buffer-substring-no-properties (point-min) (point-max)))))
771 (defun org-export-latex-quotation-marks ()
772 "Export quotation marks depending on language conventions."
773 (let* ((lang (plist-get org-export-latex-options-plist
:language
))
774 (quote-rpl (if (equal lang
"fr")
775 '(("\\(\\s-\\)\"" "«~")
776 ("\\(\\S-\\)\"" "~»")
778 '(("\\(\\s-\\)\"" "``")
779 ("\\(\\S-\\)\"" "''")
780 ("\\(\\s-\\)'" "`")))))
781 (mapc (lambda(l) (goto-char (point-min))
782 (while (re-search-forward (car l
) nil t
)
783 (let ((rpl (concat (match-string 1) (cadr l
))))
784 (org-export-latex-protect-string rpl
)
786 (replace-match rpl t t
))))) quote-rpl
)))
788 (defun org-export-latex-special-chars (sub-superscript)
789 "Export special characters to LaTeX.
790 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
791 See the `org-export-latex.el' code for a complete conversion table."
792 (goto-char (point-min))
794 (goto-char (point-min))
795 (while (re-search-forward c nil t
)
796 ;; Put the point where to check for org-protected
797 (unless (or (get-text-property (match-beginning 2) 'org-protected
)
799 (cond ((member (match-string 2) '("\\$" "$"))
800 (if (equal (match-string 2) "\\$")
801 (replace-match (concat (match-string 1) "$"
802 (match-string 3)) t t
)
803 (replace-match (concat (match-string 1) "\\$"
804 (match-string 3)) t t
)))
805 ((member (match-string 2) '("&" "%" "#"))
806 (if (equal (match-string 1) "\\")
807 (replace-match (match-string 2) t t
)
808 (replace-match (concat (match-string 1) "\\"
809 (match-string 2)) t t
)))
810 ((equal (match-string 2) "...")
812 (concat (match-string 1)
813 (org-export-latex-protect-string "\\ldots{}")) t t
))
814 ((equal (match-string 2) "~")
815 (cond ((equal (match-string 1) "\\") nil
)
816 ((eq 'org-link
(get-text-property 0 'face
(match-string 2)))
817 (replace-match (concat (match-string 1) "\\~") t t
))
819 (org-export-latex-protect-string
820 (concat (match-string 1) "\\~{}")) t t
))))
821 ((member (match-string 2) '("{" "}"))
822 (unless (save-match-data (org-inside-LaTeX-fragment-p))
823 (if (equal (match-string 1) "\\")
824 (replace-match (match-string 2) t t
)
825 (replace-match (concat (match-string 1) "\\"
826 (match-string 2)) t t
)))))
827 (unless (save-match-data (org-inside-LaTeX-fragment-p))
828 (cond ((equal (match-string 2) "\\")
829 (replace-match (or (save-match-data
830 (org-export-latex-treat-backslash-char
832 (match-string 3))) "") t t
))
833 ((member (match-string 2) '("_" "^"))
834 (replace-match (or (save-match-data
835 (org-export-latex-treat-sub-super-char
839 (match-string 3))) "") t t
)))))))
840 '("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
841 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
842 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
849 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
850 ;; (?\< . "\\textless{}")
851 ;; (?\> . "\\textgreater{}")
854 (defun org-export-latex-treat-sub-super-char
855 (subsup char string-before string-after
)
856 "Convert the \"_\" and \"^\" characters to LaTeX.
857 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
858 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
859 (cond ((equal string-before
"\\")
860 (concat string-before char string-after
))
861 ;; this is part of a math formula
862 ((and (string-match "\\S-+" string-before
)
863 (string-match "\\S-+" string-after
))
864 (cond ((eq 'org-link
(get-text-property 0 'face char
))
865 (concat string-before
"\\" char string-after
))
866 ((save-match-data (org-inside-LaTeX-fragment-p))
868 (cond ((eq 1 (length string-after
))
869 (concat string-before char string-after
))
870 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after
)
871 (format "%s%s{%s}" string-before char
872 (match-string 1 string-after
))))))
874 (> (length string-after
) 1)
875 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after
))
876 (format "$%s%s{%s}$" string-before char
877 (match-string 1 string-after
)))
878 (subsup (concat "$" string-before char string-after
"$"))
879 (t (org-export-latex-protect-string
880 (concat string-before
"\\" char
"{}" string-after
)))))
881 (t (org-export-latex-protect-string
882 (concat string-before
"\\" char
"{}" string-after
)))))
884 (defun org-export-latex-treat-backslash-char (string-before string-after
)
885 "Convert the \"$\" special character to LaTeX.
886 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
887 (cond ((member (list string-after
) org-html-entities
)
888 ;; backslash is part of a special entity (like "\alpha")
889 (concat string-before
"$\\"
890 (or (cdar (member (list string-after
) org-html-entities
))
892 ((and (not (string-match "^[ \n\t]" string-after
))
893 (not (string-match "[ \t]\\'\\|^" string-before
)))
894 ;; backslash is inside a word
895 (org-export-latex-protect-string
896 (concat string-before
"\\textbackslash{}" string-after
)))
897 ((not (or (equal string-after
"")
898 (string-match "^[ \t\n]" string-after
)))
899 ;; backslash might escape a character (like \#) or a user TeX
900 ;; macro (like \setcounter)
901 (org-export-latex-protect-string
902 (concat string-before
"\\" string-after
)))
903 ((and (string-match "^[ \t\n]" string-after
)
904 (string-match "[ \t\n]\\'" string-before
))
905 ;; backslash is alone, convert it to $\backslash$
906 (org-export-latex-protect-string
907 (concat string-before
"\\textbackslash{}" string-after
)))
908 (t (org-export-latex-protect-string
909 (concat string-before
"\\textbackslash{}" string-after
)))))
911 (defun org-export-latex-keywords (timestamps)
912 "Convert special keywords to LaTeX.
913 Regexps are those from `org-export-latex-special-string-regexps'.
914 If TIMESTAMPS, convert timestamps, otherwise delete them."
915 (let ((rg org-export-latex-special-string-regexps
) r
)
916 (while (setq r
(pop rg
))
917 (goto-char (point-min))
918 (while (re-search-forward (eval r
) nil t
)
920 (replace-match (format "\\\\texttt{%s}" (match-string 0)) t
)
921 (replace-match ""))))))
923 (defun org-export-latex-fixed-width (opt)
924 "When OPT is non-nil convert fixed-width sections to LaTeX."
925 (goto-char (point-min))
926 (while (re-search-forward "^[ \t]*:" nil t
)
928 (progn (goto-char (match-beginning 0))
929 (insert "\\begin{verbatim}\n")
930 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
931 (replace-match (concat (match-string 1)
932 (match-string 2)) t t
)
934 (insert "\\end{verbatim}\n\n"))
935 (progn (goto-char (match-beginning 0))
936 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
937 (replace-match (concat "%" (match-string 1)
938 (match-string 2)) t t
)
942 (defvar org-table-last-alignment
) ; defined in org-table.el
943 (declare-function orgtbl-to-latex
"org-table" (table params
) t
)
944 (defun org-export-latex-tables (insert)
945 "Convert tables to LaTeX and INSERT it."
946 (goto-char (point-min))
947 (while (re-search-forward "^\\([ \t]*\\)|" nil t
)
948 ;; FIXME really need to save-excursion?
949 (save-excursion (org-table-align))
950 (let* ((beg (org-table-begin))
951 (end (org-table-end))
952 (raw-table (buffer-substring-no-properties beg end
))
953 fnum fields line lines olines gr colgropen line-fmt align
)
954 (if org-export-latex-tables-verbatim
955 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
956 "\\end{verbatim}\n")))
957 (apply 'delete-region
(list beg end
))
958 (insert (org-export-latex-protect-string tbl
)))
960 (setq lines
(split-string raw-table
"\n" t
))
961 (apply 'delete-region
(list beg end
))
962 (when org-export-table-remove-special-lines
963 (setq lines
(org-table-clean-before-export lines
)))
964 ;; make a formatting string to reflect aligment
966 (while (and (not line-fmt
) (setq line
(pop olines
)))
967 (unless (string-match "^[ \t]*|-" line
)
968 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
969 (setq fnum
(make-vector (length fields
) 0))
973 (setq gr
(pop org-table-colgroup-info
))
975 (cond ((eq gr
':start
)
976 (prog1 (if colgropen
"|" "")
979 (prog1 (if colgropen
"|" "|")
980 (setq colgropen nil
)))
982 (if (memq gr
'(:end
:startend
))
983 (progn (setq colgropen nil
) "|")
986 ;; fix double || in line-fmt
987 (setq line-fmt
(replace-regexp-in-string "||" "|" line-fmt
))
988 ;; maybe remove the first and last "|"
989 (when (and (not org-export-latex-tables-column-borders
)
990 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt
))
991 (setq line-fmt
(match-string 2 line-fmt
)))
993 (setq align
(apply 'format
995 (mapcar (lambda (x) (if x
"r" "l"))
996 org-table-last-alignment
))))
997 ;; prepare the table to send to orgtbl-to-latex
1001 (or (and (string-match "[ \t]*|-+" elem
) 'hline
)
1002 (split-string (org-trim elem
) "|" t
)))
1005 (insert (org-export-latex-protect-string
1007 lines
`(:tstart
,(concat "\\begin{tabular}{" align
"}"))))
1010 (defun org-export-latex-fontify ()
1011 "Convert fontification to LaTeX."
1012 (goto-char (point-min))
1013 (while (re-search-forward org-emph-re nil t
)
1014 ;; The match goes one char after the *string*
1015 (let ((emph (assoc (match-string 3)
1016 org-export-latex-emphasis-alist
))
1018 (unless (get-text-property (1- (point)) 'org-protected
)
1019 (setq rpl
(concat (match-string 1)
1020 (format (org-export-latex-protect-char-in-string
1021 '("\\" "{" "}") (cadr emph
))
1025 (setq rpl
(org-export-latex-protect-string rpl
)))
1026 (replace-match rpl t t
)))
1029 (defun org-export-latex-links ()
1030 ;; Make sure to use the LaTeX hyperref and graphicx package
1031 ;; or send some warnings.
1032 "Convert links to LaTeX."
1033 (goto-char (point-min))
1034 (while (re-search-forward org-bracket-link-analytic-regexp nil t
)
1036 (goto-char (match-beginning 0))
1037 (let* ((re-radio org-export-latex-all-targets-re
)
1038 (remove (list (match-beginning 0) (match-end 0)))
1039 (type (match-string 2))
1040 (raw-path (org-extract-attributes (match-string 3)))
1041 (full-raw-path (concat (match-string 1) raw-path
))
1042 (desc (match-string 5))
1044 ;; define the path of the link
1046 ((member type
'("http" "https" "ftp"))
1047 (concat type
":" raw-path
))
1048 ((and re-radio
(string-match re-radio raw-path
))
1050 ((equal type
"mailto")
1051 (concat type
":" raw-path
))
1052 ((equal type
"file")
1053 (if (and (or (org-file-image-p (expand-file-name raw-path
))
1054 (string-match "\\.eps$" raw-path
))
1055 (equal desc full-raw-path
))
1057 (progn (when (string-match "\\(.+\\)::.+" raw-path
)
1058 (setq raw-path
(match-string 1 raw-path
)))
1059 (if (file-exists-p raw-path
)
1060 (concat type
"://" (expand-file-name raw-path
))
1061 (concat type
"://" (org-export-directory
1062 :LaTeX org-export-latex-options-plist
)
1064 ;; process with link inserting
1065 (apply 'delete-region remove
)
1066 (cond ((and imgp
(plist-get org-export-latex-options-plist
:inline-images
))
1067 (insert (format "\\includegraphics[%s]{%s}"
1068 ;; image option should be set be a comment line
1069 org-export-latex-image-default-option
1070 (expand-file-name raw-path
))))
1071 (radiop (insert (format "\\hyperref[%s]{%s}" raw-path desc
)))
1072 (path (insert (format "\\href{%s}{%s}" path desc
)))
1073 (t (insert "\\texttt{" desc
"}")))))))
1075 (defvar org-latex-entities
) ; defined below
1077 (defun org-export-latex-preprocess ()
1078 "Clean stuff in the LaTeX export."
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
)))
1086 ;; Preserve latex environments
1087 (goto-char (point-min))
1088 (while (search-forward "\\begin{" nil t
)
1089 (let ((start (progn (beginning-of-line) (point)))
1090 (end (or (and (search-forward "\\end{" nil t
)
1091 (end-of-line) (point))
1093 (add-text-properties start end
'(org-protected t
))))
1095 ;; Convert LaTeX to \LaTeX{}
1096 (goto-char (point-min))
1097 (let ((case-fold-search nil
) rpl
)
1098 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t
)
1099 (replace-match (org-export-latex-protect-string
1100 (concat (match-string 1) "\\LaTeX{}")) t t
)))
1102 ;; Convert blockquotes
1103 (goto-char (point-min))
1104 (while (search-forward "ORG-BLOCKQUOTE-START" nil t
)
1105 (replace-match "\\begin{quote}" t t
))
1106 (goto-char (point-min))
1107 (while (search-forward "ORG-BLOCKQUOTE-END" nil t
)
1108 (replace-match "\\end{quote}" t t
))
1111 (goto-char (point-min))
1112 (while (search-forward "ORG-VERSE-START" nil t
)
1113 (replace-match "\\begin{verse}" t t
))
1114 (goto-char (point-min))
1115 (while (search-forward "ORG-VERSE-END" nil t
)
1116 (replace-match "\\end{verse}" t t
))
1118 ;; Convert horizontal rules
1119 (goto-char (point-min))
1120 (while (re-search-forward "^----+.$" nil t
)
1121 (replace-match (org-export-latex-protect-string "\\hrule") t t
))
1123 ;; Protect LaTeX commands like \commad[...]{...} or \command{...}
1124 (goto-char (point-min))
1125 (while (re-search-forward "\\\\[a-zA-Z]+\\(?:\\[.*\\]\\)?{.*}" nil t
)
1126 (add-text-properties (match-beginning 0) (match-end 0)
1127 '(org-protected t
)))
1129 ;; Protect LaTeX entities
1130 (goto-char (point-min))
1131 (while (re-search-forward (regexp-opt org-latex-entities
) nil t
)
1132 (add-text-properties (match-beginning 0) (match-end 0)
1133 '(org-protected t
)))
1135 ;; Replace radio links
1136 (goto-char (point-min))
1137 (while (re-search-forward
1138 (concat "<<<?" org-export-latex-all-targets-re
1139 ">>>?\\((INVISIBLE)\\)?") nil t
)
1141 (org-export-latex-protect-string
1142 (format "\\label{%s}%s"(match-string 1)
1143 (if (match-string 2) "" (match-string 1)))) t t
))
1145 ;; Delete @<...> constructs
1146 ;; Thanks to Daniel Clemente for this regexp
1147 (goto-char (point-min))
1148 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t
)
1151 ;; When converting to LaTeX, replace footnotes
1152 ;; FIXME: don't protect footnotes from conversion
1153 (when (plist-get org-export-latex-options-plist
:footnotes
)
1154 (goto-char (point-min))
1155 (while (re-search-forward "\\[[0-9]+\\]" nil t
)
1156 (when (save-match-data
1157 (save-excursion (beginning-of-line)
1158 (looking-at "[^:|#]")))
1159 (let ((foot-beg (match-beginning 0))
1160 (foot-end (match-end 0))
1161 (foot-prefix (match-string 0))
1162 footnote footnote-rpl
)
1164 (when (search-forward foot-prefix nil t
)
1166 (let ((end (save-excursion
1167 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t
)
1168 (match-beginning 0) (point-max)))))
1169 (setq footnote
(concat (org-trim (buffer-substring (point) end
))
1170 " ")) ; prevent last } being part of a link
1171 (delete-region (point) end
))
1172 (goto-char foot-beg
)
1173 (delete-region foot-beg foot-end
)
1174 (unless (null footnote
)
1175 (setq footnote-rpl
(format "\\footnote{%s}" footnote
))
1176 (add-text-properties 0 10 '(org-protected t
) footnote-rpl
)
1177 (add-text-properties (1- (length footnote-rpl
))
1178 (length footnote-rpl
)
1179 '(org-protected t
) footnote-rpl
)
1180 (insert footnote-rpl
)))))))
1182 ;; Replace footnote section tag for LaTeX
1183 (goto-char (point-min))
1184 (while (re-search-forward
1185 (concat "^" footnote-section-tag-regexp
) nil t
)
1186 (replace-match ""))))
1190 (defun org-export-latex-lists ()
1191 "Replace plain text lists in current buffer into LaTeX lists."
1192 "Convert lists to LaTeX."
1193 (goto-char (point-min))
1194 (while (re-search-forward org-list-beginning-re nil t
)
1196 (insert (org-list-to-latex (org-list-parse-list t
)) "\n")))
1198 (defconst org-latex-entities
1227 "\\begin{description}"
1228 "\\begin{enumerate}"
1232 "\\begin{flushleft}"
1233 "\\begin{flushright}"
1238 "\\begin{quotation}"
1243 "\\begin{thebibliography}"
1245 "\\begin{titlepage}"
1372 "A list of LaTeX commands to be protected when performing conversion.")
1374 (provide 'org-export-latex
)
1376 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
1378 ;;; org-export-latex.el ends here