1 ;;; org-export-latex.el --- LaTeX exporter for org-mode
3 ;; Copyright (C) 2007 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 it
17 ;; under the terms of the GNU General Public License as published by the
18 ;; Free Software Foundation; either version 3, or (at your option) any
21 ;; GNU Emacs is distributed in the hope that it will be useful, but WITHOUT
22 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
26 ;; You should have received a copy of the GNU General Public License along
27 ;; with GNU Emacs; see the file COPYING. If not, write to the Free Software
28 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33 ;; This library implements a LaTeX exporter for org-mode.
35 ;; Put this file into your load-path and the following into your ~/.emacs:
36 ;; (require 'org-export-latex)
38 ;; The interactive functions are similar to those of the HTML exporter:
40 ;; M-x `org-export-as-latex'
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'
55 (defvar org-latex-options-plist nil
)
56 (defvar org-latex-todo-keywords-1 nil
)
57 (defvar org-latex-all-targets-regexp nil
)
58 (defvar org-latex-add-level
0)
59 (defvar org-latex-sectioning-depth
0)
60 (defvar org-export-latex-list-beginning-re
61 "^\\([ \t]*\\)\\([-+]\\|[0-9]+\\(?:\\.\\|)\\)\\) *?")
63 (defvar org-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
75 (defcustom org-export-latex-sectioning-alist
76 '((1 "\\section{%s}" "\\section*{%s}")
77 (2 "\\subsection{%s}" "\\subsection*{%s}")
78 (3 "\\subsubsection{%s}" "\\subsubsection*{%s}")
79 (4 "\\paragraph{%s}" "\\paragraph*{%s}")
80 (5 "\\subparagraph{%s}" "\\subparagraph*{%s}"))
81 "Alist of LaTeX commands for inserting sections.
82 Here is the structure of each cell:
84 \(level unnumbered-section numbered-section\)
86 The %s formatter will be replaced by the title of the section."
87 :group
'org-export-latex
90 (defcustom org-export-latex-emphasis-alist
91 '(("*" "\\textbf{%s}" nil
)
92 ("/" "\\emph{%s}" nil
)
93 ("_" "\\underline{%s}" nil
)
94 ("+" "\\texttt{%s}" nil
)
95 ("=" "\\texttt{%s}" nil
))
96 "Alist of LaTeX expressions to convert emphasis fontifiers.
97 Each element of the list is a list of three elements.
98 The first element is the character used as a marker for fontification.
99 The second element is a formatting string to wrap fontified text with.
100 The third element decides whether to protect converted text from other
102 :group
'org-export-latex
105 (defcustom org-export-latex-preamble
106 "\\documentclass[11pt,a4paper]{article}
107 \\usepackage[utf8]{inputenc}
108 \\usepackage[T1]{fontenc}
109 \\usepackage{hyperref}"
110 "Preamble to be inserted at the very beginning of the LaTeX export."
111 :group
'org-export-latex
114 (defcustom org-export-latex-title-command
"\\maketitle"
115 "The command used to insert the title just after \\begin{document}.
116 If this string contains the formatting specification \"%s\" then
117 it will be used as a formatting string, passing the title as an
119 :group
'org-export-latex
122 (defcustom org-export-latex-date-format
124 "Format string for \\date{...}."
125 :group
'org-export-latex
128 (defcustom org-export-latex-tables-verbatim nil
129 "When non-nil, export tables as verbatim."
130 :group
'org-export-latex
133 (defcustom org-export-latex-packages-alist nil
134 "Alist of packages to be inserted in the preamble.
135 Each cell is of the forma \( option . package \).
139 \(setq org-export-latex-packages-alist
140 '((\"french\" \"babel\"))"
141 :group
'org-export-latex
144 (defcustom org-export-latex-low-levels
'description
145 "How to convert sections below the current level of sectioning,
146 as specified by `org-export-headline-levels' or the value of \"H:\"
147 in Org's #+OPTION line.
149 This can be either nil (skip the sections), 'description (convert
150 the sections as descriptive lists) or a string to be used instead
151 of \\section{%s}. In this latter case, the %s stands here for the
152 inserted headline and is mandatory."
153 :group
'org-export-latex
154 :type
'(choice (const :tag
"Ignore" nil
)
155 (symbol :tag
"Convert as descriptive list" description
)
156 (string :tag
"Use a section string" :value
"\\subparagraph{%s}")))
158 (defcustom org-export-latex-remove-from-headlines
159 '(:todo t
:priority t
:tags t
)
160 "A plist of keywords to remove from headlines.
161 Non-nil means remove this keyword type from the headline.
163 Don't remove the keys, just change their values."
165 :group
'org-export-latex
)
167 (defcustom org-export-latex-image-default-option
"width=10em"
168 "Default option for images."
169 :group
'org-export-latex
172 (defcustom org-export-latex-coding-system nil
173 "Coding system for the exported LaTex file."
174 :group
'org-export-latex
175 :type
'coding-system
)
177 ;; FIXME Do we want this one?
178 ;; (defun org-export-as-latex-and-open (arg) ...)
180 ;;; Autoload functions:
182 (defun org-export-as-latex-batch ()
183 "Call `org-export-as-latex', may be used in batch processing as
185 --load=$HOME/lib/emacs/org.el
186 --eval \"(setq org-export-headline-levels 2)\"
187 --visit=MyFile --funcall org-export-as-latex-batch"
188 (org-export-as-latex org-export-headline-levels
'hidden
))
191 (defun org-export-as-latex-to-buffer (arg)
192 "Call `org-exort-as-latex` with output to a temporary buffer.
193 No file is created. The prefix ARG is passed through to `org-export-as-latex'."
195 (org-export-as-latex arg nil nil
"*Org LaTeX Export*")
196 (switch-to-buffer-other-window "*Org LaTeX Export*"))
199 (defun org-replace-region-by-latex (beg end
)
200 "Replace the region from BEG to END with its LaTeX export.
201 It assumes the region has `org-mode' syntax, and then convert it to
202 LaTeX. This can be used in any buffer. For example, you could
203 write an itemized list in `org-mode' syntax in an LaTeX buffer and
204 then use this command to convert it."
207 (save-window-excursion
209 (setq latex
(org-export-region-as-latex
211 (setq reg
(buffer-substring beg end
)
212 buf
(get-buffer-create "*Org tmp*"))
218 (setq latex
(org-export-region-as-latex
219 (point-min) (point-max) t
'string
)))
221 (delete-region beg end
)
225 (defun org-export-region-as-latex (beg end
&optional body-only buffer
)
226 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
227 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
228 contents, and only produce the region of converted text, useful for
229 cut-and-paste operations.
230 If BUFFER is a buffer or a string, use/create that buffer as a target
231 of the converted LaTeX. If BUFFER is the symbol `string', return the
232 produced LaTeX as a string and leave not buffer behind. For example,
233 a Lisp program could call this function in the following way:
235 (setq latex (org-export-region-as-latex beg end t 'string))
237 When called interactively, the output buffer is selected, and shown
238 in a window. A non-interactive call will only retunr the buffer."
240 (when (interactive-p)
241 (setq buffer
"*Org LaTeX Export*"))
242 (let ((transient-mark-mode t
) (zmacs-regions t
)
245 (set-mark (point)) ;; to activate the region
247 (setq rtn
(org-export-as-latex
250 (if (fboundp 'deactivate-mark
) (deactivate-mark))
251 (if (and (interactive-p) (bufferp rtn
))
252 (switch-to-buffer-other-window rtn
)
256 (defun org-export-as-latex (arg &optional hidden ext-plist
258 "Export current buffer to a LaTeX file."
260 ;; Make sure we have a file name when we need it.
261 (when (and (not (or to-buffer body-only
))
262 (not buffer-file-name
))
263 (if (buffer-base-buffer)
264 (org-set-local 'buffer-file-name
265 (with-current-buffer (buffer-base-buffer)
267 (error "Need a file name to be able to export")))
269 (message "Exporting to LaTeX...")
270 (org-update-radio-target-regexp)
271 (org-export-latex-set-initial-vars ext-plist
)
272 (let* ((wcf (current-window-configuration))
273 (opt-plist org-latex-options-plist
)
274 (filename (concat (file-name-as-directory
275 (org-export-directory :LaTeX ext-plist
))
276 (file-name-sans-extension
277 (file-name-nondirectory ;sans-extension
278 buffer-file-name
)) ".tex"))
279 (filename (if (equal (file-truename filename
)
280 (file-truename buffer-file-name
))
281 (concat filename
".tex")
283 (buffer (if to-buffer
285 ((eq to-buffer
'string
) (get-buffer-create
286 "*Org LaTeX Export*"))
287 (t (get-buffer-create to-buffer
)))
288 (find-file-noselect filename
)))
289 (region-p (org-region-active-p))
290 (odd org-odd-levels-only
)
291 (preamble (org-export-latex-make-preamble opt-plist
))
292 (skip (plist-get opt-plist
:skip-before-1st-heading
))
293 (text (plist-get opt-plist
:text
))
294 (first-lines (if skip
"" (org-export-latex-first-lines)))
295 (coding-system (and (boundp 'buffer-file-coding-system
)
296 buffer-file-coding-system
))
297 (coding-system-for-write (or org-export-latex-coding-system
299 (save-buffer-coding-system (or org-export-latex-coding-system
301 (region (buffer-substring
302 (if region-p
(region-beginning) (point-min))
303 (if region-p
(region-end) (point-max))))
305 (org-cleaned-string-for-export
306 region
:emph-multiline t
309 :add-text
(if (eq to-buffer
'string
) nil text
)
310 :skip-before-1st-heading skip
311 :LaTeX-fragments nil
)))
316 (and (fboundp 'set-buffer-file-coding-system
)
317 (set-buffer-file-coding-system coding-system-for-write
))
319 ;; insert the preamble and initial document commands
320 (unless (or (eq to-buffer
'string
) body-only
)
323 ;; insert text found in #+TEXT
324 (when (and text
(not (eq to-buffer
'string
)))
325 (insert (org-export-latex-content text
) "\n\n"))
327 ;; insert lines before the first headline
328 (unless (or skip
(eq to-buffer
'string
))
329 (insert first-lines
))
331 ;; handle the case where the region does not begin with a section
333 (insert (with-temp-buffer
334 (insert string-for-export
)
335 (org-export-latex-first-lines))))
337 ;; export the content of headlines
338 (org-export-latex-global
340 (insert string-for-export
)
341 (goto-char (point-min))
342 (when (re-search-forward "^\\(\\*+\\) " nil t
)
343 (let* ((asters (length (match-string 1)))
344 (level (if odd
(- asters
2) (- asters
1))))
345 (setq org-latex-add-level
346 (if odd
(1- (/ (1+ asters
) 2)) (1- asters
)))
347 (org-export-latex-parse-global level odd
)))))
350 (unless body-only
(insert "\n\\end{document}"))
351 (or to-buffer
(save-buffer))
352 (goto-char (point-min))
353 (message "Exporting to LaTeX...done")
355 (if (eq to-buffer
'string
)
356 (prog1 (buffer-substring (point-min) (point-max))
357 (kill-buffer (current-buffer)))
359 (set-window-configuration wcf
))))
362 ;;; Parsing functions:
363 (defun org-export-latex-parse-global (level odd
)
364 "Parse the current buffer recursively, starting at LEVEL.
365 If ODD is non-nil, assume the buffer only contains odd sections.
366 Return A list reflecting the document structure."
368 (goto-char (point-min))
369 (let* ((cnt 0) output
370 (depth org-latex-sectioning-depth
))
371 (while (re-search-forward
372 (concat "^\\(\\(?:\\*\\)\\{"
373 (number-to-string (+ (if odd
2 1) level
))
375 ;; make sure that there is no upper heading
380 (concat "^\\(\\(?:\\*\\)\\{"
381 (number-to-string level
)
382 "\\}\\) \\(.*\\)$") nil t
)))) t
)
384 (let* ((pos (match-beginning 0))
385 (heading (match-string 2))
386 (nlevel (if odd
(/ (+ 3 level
) 2) (1+ level
))))
391 (if (re-search-forward
392 (concat "^\\(\\(?:\\*\\)\\{"
393 (number-to-string (+ (if odd
2 1) level
))
394 "\\}\\) \\(.*\\)$") nil t
)
397 (goto-char (point-min))
405 `(heading .
,heading
)
406 `(content .
,(org-export-latex-parse-content))
407 `(subcontent .
,(org-export-latex-parse-subcontent
412 (defun org-export-latex-parse-list (&optional delete
)
413 "Parse the list at point.
414 Return a list containing first level items as strings and
415 sublevels as list of strings."
416 (let ((start (point))
417 ;; Find the end of the list
420 (while (or (looking-at org-export-latex-list-beginning-re
)
421 (looking-at "^[ \t]+\\|^$"))
422 (if (eq (point) (point-max))
423 (throw 'exit
(point-max)))
424 (forward-line 1))) (point)))
426 (while (re-search-forward org-export-latex-list-beginning-re end t
)
427 (setq itemsep
(if (save-match-data
428 (string-match "^[0-9]" (match-string 2)))
429 "[0-9]+\\(?:\\.\\|)\\)" "[-+]"))
430 (let* ((indent1 (match-string 1))
431 (nextitem (save-excursion
433 (or (and (re-search-forward
434 (concat "^" indent1 itemsep
" *?") end t
)
435 (match-beginning 0)) end
))))
436 (item (buffer-substring
438 (or (and (re-search-forward
439 org-export-latex-list-beginning-re end t
)
440 (goto-char (match-beginning 0)))
442 (nextindent (match-string 1))
443 (item (org-trim item
))
444 (item (if (string-match "^\\[.+\\]" item
)
445 (replace-match "\\\\texttt{\\&}"
448 (when (> (length nextindent
)
450 (narrow-to-region (point) nextitem
)
451 (push (org-export-latex-parse-list) output
)
453 (when delete
(delete-region start end
))
454 (setq output
(nreverse output
))
455 (push (if (string-match "^\\[0" itemsep
)
456 'ordered
'unordered
) output
)))
458 (defun org-export-latex-parse-content ()
459 "Extract the content of a section."
461 (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t
)
462 (progn (beginning-of-line) (point))
464 (buffer-substring beg end
)))
466 (defun org-export-latex-parse-subcontent (level odd
)
467 "Extract the subcontent of a section at LEVEL.
468 If ODD Is non-nil, assume subcontent only contains odd sections."
469 (if (not (re-search-forward
470 (concat "^\\(\\(?:\\*\\)\\{"
471 (number-to-string (+ (if odd
4 2) level
))
474 nil
; subcontent is nil
475 (org-export-latex-parse-global (+ (if odd
2 1) level
) odd
)))
477 ;;; Rendering functions:
478 (defun org-export-latex-global (content)
479 "Export CONTENT to LaTeX.
480 CONTENT is an element of the list produced by
481 `org-export-latex-parse-global'."
482 (if (eq (car content
) 'subcontent
)
483 (mapc 'org-export-latex-sub
(cdr content
))
484 (org-export-latex-sub (car content
))))
486 (defun org-export-latex-sub (subcontent)
487 "Export the list SUBCONTENT to LaTeX.
488 SUBCONTENT is an alist containing information about the headline
490 (let ((num (plist-get org-latex-options-plist
:section-numbers
)))
491 (mapc (lambda(x) (org-export-latex-subcontent x num
)) subcontent
)))
493 (defun org-export-latex-subcontent (subcontent num
)
494 "Export each cell of SUBCONTENT to LaTeX."
495 (let ((heading (org-export-latex-fontify-headline
496 (cdr (assoc 'heading subcontent
))))
497 (level (- (cdr (assoc 'level subcontent
))
498 org-latex-add-level
))
499 (occur (number-to-string (cdr (assoc 'occur subcontent
))))
500 (content (cdr (assoc 'content subcontent
)))
501 (subcontent (cadr (assoc 'subcontent subcontent
))))
504 ((<= level org-latex-sectioning-depth
)
505 (let ((sec (assoc level org-export-latex-sectioning-alist
)))
506 (insert (format (if num
(cadr sec
) (caddr sec
)) heading
) "\n"))
507 (insert (org-export-latex-content content
))
508 (cond ((stringp subcontent
) (insert subcontent
))
509 ((listp subcontent
) (org-export-latex-sub subcontent
))))
510 ;; At a level under the hl option: we can drop this subsection
511 ((> level org-latex-sectioning-depth
)
512 (cond ((eq org-export-latex-low-levels
'description
)
513 (insert (format "\\begin{description}\n\n\\item[%s]\n\n" heading
))
514 (insert (org-export-latex-content content
))
515 (cond ((stringp subcontent
) (insert subcontent
))
516 ((listp subcontent
) (org-export-latex-sub subcontent
)))
517 (insert "\\end{description}\n"))
518 ((stringp org-export-latex-low-levels
)
519 (insert (format org-export-latex-low-levels heading
) "\n")
520 (insert (org-export-latex-content content
))
521 (cond ((stringp subcontent
) (insert subcontent
))
522 ((listp subcontent
) (org-export-latex-sub subcontent
)))))))))
525 ;;; Exporting internals:
526 (defun org-export-latex-protect-string (string)
527 "Prevent further conversion for STRING by adding the
528 org-protect property."
530 0 (length string
) '(org-protected t
) string
) string
)
532 (defun org-export-latex-protect-char-in-string (char-list string
)
533 "Add org-protected text-property to char from CHAR-LIST in STRING."
537 (goto-char (point-min))
538 (while (re-search-forward (regexp-opt char-list
) nil t
)
539 (add-text-properties (match-beginning 0)
540 (match-end 0) '(org-protected t
)))
543 (defun org-export-latex-set-initial-vars (ext-plist)
544 "Store org local variables required for LaTeX export.
545 EXT-PLIST is an optional additional plist."
546 (setq org-latex-todo-keywords-1 org-todo-keywords-1
547 org-latex-all-targets-regexp
548 (org-make-target-link-regexp (org-all-targets))
549 org-latex-options-plist
550 (org-combine-plists (org-default-export-plist) ext-plist
551 (org-infile-export-plist))
552 org-latex-sectioning-depth
553 (let ((hl-levels (plist-get org-latex-options-plist
:headline-levels
))
554 (sec-depth (length org-export-latex-sectioning-alist
)))
555 ;; Fall back on org-export-latex-sectioning-alist length if
556 ;; headline-levels goes beyond it
557 (if (> hl-levels sec-depth
) sec-depth hl-levels
))))
559 (defun org-export-latex-make-preamble (opt-plist)
560 "Make the LaTeX preamble and return it as a string.
561 Argument OPT-PLIST is the options plist for current buffer."
562 (let ((toc (plist-get opt-plist
:table-of-contents
)))
564 (if (plist-get opt-plist
:time-stamp-file
)
565 (format-time-string "% Created %Y-%m-%d %a %H:%M\n"))
567 ;; insert LaTeX custom preamble
568 org-export-latex-preamble
"\n"
570 ;; insert information on LaTeX packages
571 (when org-export-latex-packages-alist
572 (mapconcat (lambda(p)
573 (if (equal "" (car p
))
574 (format "\\usepackage{%s}" (cadr p
))
575 (format "\\usepackage[%s]{%s}"
577 org-export-latex-packages-alist
"\n"))
582 (or (plist-get opt-plist
:title
)
584 (plist-get opt-plist
:skip-before-1st-heading
))
585 (org-export-grab-title-from-buffer))
586 (and buffer-file-name
587 (file-name-sans-extension
588 (file-name-nondirectory buffer-file-name
)))
591 ;; insert author info
592 (if (plist-get opt-plist
:author-info
)
593 (format "\\author{%s}\n"
594 (or (plist-get opt-plist
:author
) user-full-name
))
595 (format "%%\\author{%s}\n"
596 (or (plist-get opt-plist
:author
) user-full-name
)))
599 (format "\\date{%s}\n"
601 (or (plist-get opt-plist
:date
)
602 org-export-latex-date-format
)))
604 ;; beginning of the document
605 "\n\\begin{document}\n\n"
607 ;; insert the title command
608 (if (string-match "%s" org-export-latex-title-command
)
609 (format org-export-latex-title-command
610 (plist-get opt-plist
:title
))
611 org-export-latex-title-command
)
615 (when (and org-export-with-toc
616 (plist-get opt-plist
:section-numbers
))
618 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
619 (min toc
(plist-get opt-plist
:headline-levels
))))
620 (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
621 (plist-get opt-plist
:headline-levels
))))))))
623 (defun org-export-latex-first-lines (&optional comments
)
624 "Export the first lines before first headline.
625 COMMENTS is either nil to replace them with the empty string or a
626 formatting string like %%%%s if we want to comment them out."
628 (goto-char (point-min))
629 (let* ((end (if (re-search-forward "^\\*" nil t
)
630 (goto-char (match-beginning 0))
631 (goto-char (point-max)))))
632 (org-export-latex-content
633 (org-cleaned-string-for-export
634 (buffer-substring (point-min) end
)
639 :skip-before-1st-heading nil
640 :LaTeX-fragments nil
)))))
642 (defun org-export-latex-keywords-maybe (remove-list)
643 "Maybe remove keywords depending on rules in REMOVE-LIST."
644 (goto-char (point-min))
645 (let ((re-todo (mapconcat 'identity org-latex-todo-keywords-1
"\\|")))
646 ;; convert TODO keywords
647 (when (re-search-forward (concat "^\\(" re-todo
"\\)") nil t
)
648 (if (plist-get remove-list
:todo
)
650 (replace-match (format "\\texttt{%s}" (match-string 1)) t t
)))
651 ;; convert priority string
652 (when (re-search-forward "\\[\\\\#.\\]" nil t
)
653 (if (plist-get remove-list
:priority
)
655 (replace-match (format "\\texttt{%s}" (match-string 0)) t t
)))
657 (when (re-search-forward "\\(:[a-zA-Z0-9]+\\)+:" nil t
)
658 (if (or (not org-export-with-tags
)
659 (plist-get remove-list
:tags
))
661 (replace-match (format "\\texttt{%s}" (match-string 0)) t t
)))))
663 (defun org-export-latex-fontify-headline (headline)
664 "Fontify special words in a HEADLINE."
666 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
667 ;; the beginning of the buffer - inserting "\n" is safe here though.
668 (insert "\n" headline
)
669 (goto-char (point-min))
670 (when (plist-get org-latex-options-plist
:emphasize
)
671 (org-export-latex-fontify))
672 (org-export-latex-special-chars
673 (plist-get org-latex-options-plist
:sub-superscript
))
674 (org-export-latex-keywords-maybe
675 org-export-latex-remove-from-headlines
)
676 (org-export-latex-links)
677 (org-trim (buffer-substring-no-properties (point-min) (point-max)))))
679 (defun org-export-latex-content (content)
680 "Convert CONTENT string to LaTeX."
683 (org-export-latex-quotation-marks)
684 (when (plist-get org-latex-options-plist
:emphasize
)
685 (org-export-latex-fontify))
686 (org-export-latex-special-chars
687 (plist-get org-latex-options-plist
:sub-superscript
))
688 (org-export-latex-links)
689 (org-export-latex-keywords
690 (plist-get org-latex-options-plist
:timestamps
))
691 (org-export-latex-lists)
692 (org-export-latex-tables
693 (plist-get org-latex-options-plist
:tables
))
694 (org-export-latex-fixed-width
695 (plist-get org-latex-options-plist
:fixed-width
))
697 (buffer-substring (point-min) (point-max))))
699 (defun org-export-latex-quotation-marks ()
700 "Export question marks depending on language conventions.
701 Local definition of the language overrides
702 `org-export-latex-quotation-marks-convention' which overrides
703 `org-export-default-language'."
704 (let* ((lang (plist-get org-latex-options-plist
:language
))
705 (quote-rpl (if (equal lang
"fr")
706 '(("\\(\\s-\\)\"" "«~")
707 ("\\(\\S-\\)\"" "~»")
709 '(("\\(\\s-\\)\"" "``")
710 ("\\(\\S-\\)\"" "''")
711 ("\\(\\s-\\)'" "`")))))
712 (mapc (lambda(l) (goto-char (point-min))
713 (while (re-search-forward (car l
) nil t
)
714 (let ((rpl (concat (match-string 1) (cadr l
))))
715 (org-export-latex-protect-string rpl
)
717 (replace-match rpl t t
))))) quote-rpl
)))
719 ;; | chars/string in Org | normal environment | math environment |
720 ;; |-----------------------+-----------------------+-----------------------|
721 ;; | & # % $ | \& \# \% \$ | \& \# \% \$ |
722 ;; | { } _ ^ \ | \{ \} \_ \^ \\ | { } _ ^ \ |
723 ;; |-----------------------+-----------------------+-----------------------|
724 ;; | a_b and a^b | $a_b$ and $a^b$ | a_b and a^b |
725 ;; | a_abc and a_{abc} | $a_a$bc and $a_{abc}$ | a_abc and a_{abc} |
726 ;; | \tau and \mu | $\tau$ and $\mu$ | \tau and \mu |
727 ;; |-----------------------+-----------------------+-----------------------|
728 ;; | \_ \^ | \_ \^ | \_ \^ |
729 ;; | \(a=\mu\mbox{m}\) | \(a=\mu\mbox{m}\) | \(a=\mu\mbox{m}\) |
730 ;; | \[\beta^2-a=0\] | \[\beta^2-a=0\] | \[\beta^2-a=0\] |
731 ;; | $x=22\tau$ | $x=22\tau$ | $x=22\tau$ |
732 ;; | $$\alpha=\sqrt{a^3}$$ | $$\alpha=\sqrt{a^3}$$ | $$\alpha=\sqrt{a^3}$$ |
734 (defun org-export-latex-special-chars (sub-superscript)
735 "Export special characters to LaTeX.
736 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
737 See the `org-export-latex.el' code for a complete conversion table."
738 (goto-char (point-min))
740 (goto-char (point-min))
741 (while (re-search-forward c nil t
)
742 ;; Put the point where to check for org-protected
743 (unless (get-text-property (match-beginning 2) 'org-protected
)
744 (cond ((member (match-string 2) '("\\$" "$"))
745 (if (equal (match-string 2) "\\$")
746 (replace-match (concat (match-string 1) "$"
747 (match-string 3)) t t
)
748 (replace-match (concat (match-string 1) "\\$"
749 (match-string 3)) t t
)))
750 ((member (match-string 2) '("&" "%" "#"))
751 (if (equal (match-string 1) "\\")
752 (replace-match (match-string 2) t t
)
753 (replace-match (concat (match-string 1) "\\"
754 (match-string 2)) t t
)))
755 ((equal (match-string 2) "~")
756 (cond ((equal (match-string 1) "\\") nil
)
757 ((eq 'org-link
(get-text-property 0 'face
(match-string 2)))
758 (replace-match (concat (match-string 1) "\\~") t t
))
760 (org-export-latex-protect-string
761 (concat (match-string 1) "\\~{}")) t t
))))
762 ((member (match-string 2) '("{" "}"))
763 (unless (save-match-data (org-inside-LaTeX-fragment-p))
764 (if (equal (match-string 1) "\\")
765 (replace-match (match-string 2) t t
)
766 (replace-match (concat (match-string 1) "\\"
767 (match-string 2)) t t
)))))
768 (unless (save-match-data (org-inside-LaTeX-fragment-p))
769 (cond ((equal (match-string 2) "\\")
770 (replace-match (or (save-match-data
771 (org-export-latex-treat-backslash-char
773 (match-string 3))) "") t t
))
774 ((member (match-string 2) '("_" "^"))
775 (replace-match (or (save-match-data
776 (org-export-latex-treat-sub-super-char
780 (match-string 3))) "") t t
)))))))
781 '("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
782 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
783 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
790 ;; (?\< . "\\textless{}")
791 ;; (?\> . "\\textgreater{}")
794 (defun org-export-latex-treat-sub-super-char
795 (subsup string-before char string-after
)
796 "Convert the \"_\" and \"^\" characters to LaTeX.
797 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
798 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
799 (cond ((equal string-before
"\\")
800 (concat string-before char string-after
))
801 ;; this is part of a math formula
802 ((and (string-match "\\S-+" string-before
)
803 (string-match "\\S-+" string-after
))
804 (cond ((eq 'org-link
(get-text-property 0 'face char
))
805 (concat string-before
"\\" char string-after
))
806 ((save-match-data (org-inside-LaTeX-fragment-p))
808 (cond ((eq 1 (length string-after
))
809 (concat string-before char string-after
))
810 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after
)
811 (format "%s%s{%s}" string-before char
812 (match-string 1 string-after
))))))
814 (> (length string-after
) 1)
815 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after
))
816 (format "$%s%s{%s}$" string-before char
817 (match-string 1 string-after
)))
818 (subsup (concat "$" string-before char string-after
"$"))
819 (t (org-export-latex-protect-string
820 (concat string-before
"\\" char
"{}" string-after
)))))
821 (t (org-export-latex-protect-string
822 (concat string-before
"\\" char
"{}" string-after
)))))
824 (defun org-export-latex-treat-backslash-char (string-before string-after
)
825 "Convert the \"$\" special character to LaTeX.
826 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
827 (cond ((member (list string-after
) org-html-entities
)
828 ;; backslash is part of a special entity (like "\alpha")
829 (concat string-before
"$\\"
830 (or (cdar (member (list string-after
) org-html-entities
))
832 ((and (not (string-match "^[ \n\t]" string-after
))
833 (not (string-match "[ \t]\\'\\|^" string-before
)))
834 ;; backslash is inside a word
835 (org-export-latex-protect-string
836 (concat string-before
"\\textbackslash{}" string-after
)))
837 ((not (or (equal string-after
"")
838 (string-match "^[ \t\n]" string-after
)))
839 ;; backslash might escape a character (like \#) or a user TeX
840 ;; macro (like \setcounter)
841 (org-export-latex-protect-string
842 (concat string-before
"\\" string-after
)))
843 ((and (string-match "^[ \t\n]" string-after
)
844 (string-match "[ \t\n]\\'" string-before
))
845 ;; backslash is alone, convert it to $\backslash$
846 (org-export-latex-protect-string
847 (concat string-before
"\\textbackslash{}" string-after
)))
848 (t (org-export-latex-protect-string
849 (concat string-before
"\\textbackslash{}" string-after
)))))
851 (defun org-export-latex-keywords (timestamps)
852 "Convert special keywords to LaTeX.
853 Regexps are those from `org-latex-special-string-regexps'."
854 (let ((rg org-latex-special-string-regexps
) r
)
855 (while (setq r
(pop rg
))
856 (goto-char (point-min))
857 (while (re-search-forward (eval r
) nil t
)
859 (replace-match (format "\\\\texttt{%s}" (match-string 0)) t
)
860 (replace-match ""))))))
862 (defun org-export-latex-fixed-width (opt)
863 "When OPT is non-nil convert fixed-width sections to LaTeX."
864 (goto-char (point-min))
865 ;; FIXME the search shouldn't be performed on already converted text
866 (while (re-search-forward "^[ \t]*:" nil t
)
868 (progn (goto-char (match-beginning 0))
869 (insert "\\begin{verbatim}\n")
870 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
871 (replace-match (concat (match-string 1)
872 (match-string 2)) t t
)
874 (insert "\\end{verbatim}\n\n"))
875 (progn (goto-char (match-beginning 0))
876 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
877 (replace-match (concat "%" (match-string 1)
878 (match-string 2)) t t
)
881 (defun org-export-latex-lists ()
882 "Convert lists to LaTeX."
883 (goto-char (point-min))
884 (while (re-search-forward org-export-latex-list-beginning-re nil t
)
886 (org-export-list-to-latex
887 (org-export-latex-parse-list t
))))
889 (defun org-export-list-to-generic (list params
)
890 "Convert a LIST parsed through `org-export-latex-parse-list' to other formats.
894 :ustart String to start an unordered list
895 :uend String to end an unordered list
897 :ostart String to start an ordered list
898 :oend String to end an ordered list
900 :splice When set to t, return only list body lines, don't wrap
901 them into :[u/o]start and :[u/o]end. Default is nil.
903 :istart String to start a list item
904 :iend String to end a list item
905 :isep String to separate items
906 :lsep String to separate sublists"
908 (let* ((p params
) sublist
909 (splicep (plist-get p
:splice
))
910 (ostart (plist-get p
:ostart
))
911 (oend (plist-get p
:oend
))
912 (ustart (plist-get p
:ustart
))
913 (uend (plist-get p
:uend
))
914 (istart (plist-get p
:istart
))
915 (iend (plist-get p
:iend
))
916 (isep (plist-get p
:isep
))
917 (lsep (plist-get p
:lsep
)))
919 (cond ((eq (car list
) 'ordered
)
920 (concat ostart
"\n%s" oend
"\n"))
921 ((eq (car list
) 'unordered
)
922 (concat ustart
"\n%s" uend
"\n"))))
924 (while (setq sublist
(pop list
))
925 (cond ((symbolp sublist
) nil
)
927 (setq rtn
(concat rtn istart sublist iend isep
)))
929 (setq rtn
(concat rtn
;; previous list
930 lsep
;; list separator
931 (org-export-list-to-generic sublist p
)
932 lsep
;; list separator
934 (format wrapper rtn
))))
936 (defun org-export-list-to-latex (list)
937 "Convert LIST into a LaTeX list."
939 (org-export-list-to-generic
940 list
'(:splicep nil
:ostart
"\\begin{enumerate}" :oend
"\\end{enumerate}"
941 :ustart
"\\begin{itemize}" :uend
"\\end{itemize}"
942 :istart
"\\item " :iend
""
943 :isep
"\n" :lsep
"\n"))
944 ;; Add a trailing \n after list conversion
947 ;; FIXME Use org-export-highlight-first-table-line ?
948 (defun org-export-latex-tables (insert)
949 "Convert tables to LaTeX and INSERT it."
950 (goto-char (point-min))
951 (while (re-search-forward "^\\([ \t]*\\)|" nil t
)
952 ;; FIXME really need to save-excursion?
953 (save-excursion (org-table-align))
954 (let* ((beg (org-table-begin))
955 (end (org-table-end))
956 (raw-table (buffer-substring-no-properties beg end
))
957 fnum line lines olines gr colgropen line-fmt alignment
)
958 (if org-export-latex-tables-verbatim
959 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
960 "\\end{verbatim}\n")))
961 (apply 'delete-region
(list beg end
))
964 (setq lines
(split-string raw-table
"\n" t
))
965 (apply 'delete-region
(list beg end
))
966 (when org-export-table-remove-special-lines
967 (setq lines
(org-table-clean-before-export lines
)))
968 ;; make a formatting string to reflect aligment
970 (while (and (not line-fmt
) (setq line
(pop olines
)))
971 (unless (string-match "^[ \t]*|-" line
)
972 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
973 (setq fnum
(make-vector (length fields
) 0))
977 (setq gr
(pop org-table-colgroup-info
))
979 (cond ((eq gr
':start
)
980 (prog1 (if colgropen
"|" "")
983 (prog1 (if colgropen
"|" "|")
984 (setq colgropen nil
)))
986 (if (memq gr
'(:end
:startend
))
987 (progn (setq colgropen nil
) "|")
990 ;; maybe remove the first and last "|"
991 (when (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt
)
992 (setq line-fmt
(match-string 2 line-fmt
)))
994 (setq align
(apply 'format
996 (mapcar (lambda (x) (if x
"r" "l"))
997 org-table-last-alignment
))))
998 ;; prepare the table to send to orgtbl-to-latex
1002 (or (and (string-match "[ \t]*|-+" elem
) 'hline
)
1003 (split-string (org-trim elem
) "|" t
)))
1006 (insert (orgtbl-to-latex
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-latex-all-targets-regexp
)
1038 (remove (list (match-beginning 0) (match-end 0)))
1039 (type (match-string 2))
1040 (raw-path (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-latex-options-plist
)
1064 ;; process with link inserting
1065 (apply 'delete-region remove
)
1066 (cond ((and imgp
(plist-get org-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 ;; FIXME: what about caption? image properties?
1072 (radiop (insert (format "\\hyperref[%s]{%s}" raw-path desc
)))
1073 (path (insert (format "\\href{%s}{%s}" path desc
)))
1074 (t (insert "\\texttt{" desc
"}")))))))
1076 (defun org-export-latex-cleaned-string
1077 ;; FIXME remove commentsp call in org.el and here
1078 (&optional commentsp
)
1079 "Clean stuff in the LaTeX export."
1081 ;; Preserve line breaks
1082 (goto-char (point-min))
1083 (while (re-search-forward "\\\\\\\\" nil t
)
1084 (add-text-properties (match-beginning 0) (match-end 0)
1085 '(org-protected t
)))
1087 ;; Convert LaTeX to \LaTeX{}
1088 (goto-char (point-min))
1089 (let ((case-fold-search nil
) rpl
)
1090 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t
)
1091 (replace-match (org-export-latex-protect-string
1092 (concat (match-string 1) "\\LaTeX{}")) t t
)))
1094 ;; Convert horizontal rules
1095 (goto-char (point-min))
1096 (while (re-search-forward "^----+.$" nil t
)
1097 (replace-match (org-export-latex-protect-string "\\hrule") t t
))
1099 ;; Protect LaTeX \commands{...}
1100 (goto-char (point-min))
1101 (while (re-search-forward "\\\\[a-zA-Z]+\\(?:\\[.*\\]\\)?{.*}" nil t
)
1102 (add-text-properties (match-beginning 0) (match-end 0)
1103 '(org-protected t
)))
1105 ;; Replace radio links
1106 (goto-char (point-min))
1107 (while (re-search-forward
1108 (concat "<<<?" org-latex-all-targets-regexp
1109 ">>>?\\((INVISIBLE)\\)?") nil t
)
1111 (org-export-latex-protect-string
1112 (format "\\label{%s}%s"(match-string 1)
1113 (if (match-string 2) "" (match-string 1)))) t t
))
1115 ;; Delete @<...> constructs
1116 (goto-char (point-min))
1117 ;; Thanks to Daniel Clemente for this regexp
1118 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t
)
1121 ;; When converting to LaTeX, replace footnotes
1122 ;; FIXME: don't protect footnotes from conversion
1123 (when (plist-get org-latex-options-plist
:footnotes
)
1124 (goto-char (point-min))
1125 (while (re-search-forward "\\[[0-9]+\\]" nil t
)
1126 (when (save-match-data
1127 (save-excursion (beginning-of-line)
1128 (looking-at "[^:|#]")))
1129 (let ((foot-beg (match-beginning 0))
1130 (foot-end (match-end 0))
1131 (foot-prefix (match-string 0))
1132 footnote footnote-rpl
)
1133 (when (and (re-search-forward (regexp-quote foot-prefix
) nil t
))
1135 (let ((end (save-excursion
1136 (if (re-search-forward "^$\\|\\[[0-9]+\\]" nil t
)
1137 (match-beginning 0) (point-max)))))
1140 (org-trim (buffer-substring (point) end
))
1141 ;; FIXME stupid workaround for cases where
1142 ;; `org-bracket-link-analytic-regexp' matches
1143 ;; }. as part of the link.
1145 (delete-region (point) end
)))
1146 (goto-char foot-beg
)
1147 (delete-region foot-beg foot-end
)
1148 (setq footnote-rpl
(format "\\footnote{%s}" footnote
))
1149 (add-text-properties 0 10 '(org-protected t
) footnote-rpl
)
1150 (add-text-properties (1- (length footnote-rpl
))
1151 (length footnote-rpl
)
1152 '(org-protected t
) footnote-rpl
)
1153 (insert footnote-rpl
))))
1155 ;; Replace footnote section tag for LaTeX
1156 (goto-char (point-min))
1157 (while (re-search-forward
1158 (concat "^" footnote-section-tag-regexp
) nil t
)
1159 (replace-match ""))))
1161 (provide 'org-export-latex
)
1163 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
1164 ;;; org-export-latex.el ends here