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"))
583 (org-export-latex-content
584 (or (plist-get opt-plist
:title
)
586 (plist-get opt-plist
:skip-before-1st-heading
))
587 (org-export-grab-title-from-buffer))
588 (and buffer-file-name
589 (file-name-sans-extension
590 (file-name-nondirectory buffer-file-name
)))
593 ;; insert author info
594 (if (plist-get opt-plist
:author-info
)
595 (format "\\author{%s}\n"
596 (or (plist-get opt-plist
:author
) user-full-name
))
597 (format "%%\\author{%s}\n"
598 (or (plist-get opt-plist
:author
) user-full-name
)))
601 (format "\\date{%s}\n"
603 (or (plist-get opt-plist
:date
)
604 org-export-latex-date-format
)))
606 ;; beginning of the document
607 "\n\\begin{document}\n\n"
609 ;; insert the title command
610 (if (string-match "%s" org-export-latex-title-command
)
611 (format org-export-latex-title-command
612 (plist-get opt-plist
:title
))
613 org-export-latex-title-command
)
617 (when (and org-export-with-toc
618 (plist-get opt-plist
:section-numbers
))
620 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
621 (min toc
(plist-get opt-plist
:headline-levels
))))
622 (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
623 (plist-get opt-plist
:headline-levels
))))))))
625 (defun org-export-latex-first-lines (&optional comments
)
626 "Export the first lines before first headline.
627 COMMENTS is either nil to replace them with the empty string or a
628 formatting string like %%%%s if we want to comment them out."
630 (goto-char (point-min))
632 (end (if (and (re-search-forward "^\\*" nil t
)
633 (not (eq pt
(match-beginning 0))))
634 (goto-char (match-beginning 0))
635 (goto-char (point-max)))))
636 (org-export-latex-content
637 (org-cleaned-string-for-export
638 (buffer-substring (point-min) end
)
643 :skip-before-1st-heading nil
644 :LaTeX-fragments nil
)))))
646 (defun org-export-latex-keywords-maybe (remove-list)
647 "Maybe remove keywords depending on rules in REMOVE-LIST."
648 (goto-char (point-min))
649 (let ((re-todo (mapconcat 'identity org-latex-todo-keywords-1
"\\|")))
650 ;; convert TODO keywords
651 (when (re-search-forward (concat "^\\(" re-todo
"\\)") nil t
)
652 (if (plist-get remove-list
:todo
)
654 (replace-match (format "\\texttt{%s}" (match-string 1)) t t
)))
655 ;; convert priority string
656 (when (re-search-forward "\\[\\\\#.\\]" nil t
)
657 (if (plist-get remove-list
:priority
)
659 (replace-match (format "\\texttt{%s}" (match-string 0)) t t
)))
661 (when (re-search-forward "\\(:[a-zA-Z0-9]+\\)+:" nil t
)
662 (if (or (not org-export-with-tags
)
663 (plist-get remove-list
:tags
))
665 (replace-match (format "\\texttt{%s}" (match-string 0)) t t
)))))
667 (defun org-export-latex-fontify-headline (headline)
668 "Fontify special words in a HEADLINE."
670 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
671 ;; the beginning of the buffer - inserting "\n" is safe here though.
672 (insert "\n" headline
)
673 (goto-char (point-min))
674 (when (plist-get org-latex-options-plist
:emphasize
)
675 (org-export-latex-fontify))
676 (org-export-latex-special-chars
677 (plist-get org-latex-options-plist
:sub-superscript
))
678 (org-export-latex-keywords-maybe
679 org-export-latex-remove-from-headlines
)
680 (org-export-latex-links)
681 (org-trim (buffer-substring-no-properties (point-min) (point-max)))))
683 (defun org-export-latex-content (content)
684 "Convert CONTENT string to LaTeX."
687 (org-export-latex-quotation-marks)
688 (when (plist-get org-latex-options-plist
:emphasize
)
689 (org-export-latex-fontify))
690 (org-export-latex-special-chars
691 (plist-get org-latex-options-plist
:sub-superscript
))
692 (org-export-latex-links)
693 (org-export-latex-keywords
694 (plist-get org-latex-options-plist
:timestamps
))
695 (org-export-latex-lists)
696 (org-export-latex-tables
697 (plist-get org-latex-options-plist
:tables
))
698 (org-export-latex-fixed-width
699 (plist-get org-latex-options-plist
:fixed-width
))
701 (buffer-substring (point-min) (point-max))))
703 (defun org-export-latex-quotation-marks ()
704 "Export question marks depending on language conventions.
705 Local definition of the language overrides
706 `org-export-latex-quotation-marks-convention' which overrides
707 `org-export-default-language'."
708 (let* ((lang (plist-get org-latex-options-plist
:language
))
709 (quote-rpl (if (equal lang
"fr")
710 '(("\\(\\s-\\)\"" "«~")
711 ("\\(\\S-\\)\"" "~»")
713 '(("\\(\\s-\\)\"" "``")
714 ("\\(\\S-\\)\"" "''")
715 ("\\(\\s-\\)'" "`")))))
716 (mapc (lambda(l) (goto-char (point-min))
717 (while (re-search-forward (car l
) nil t
)
718 (let ((rpl (concat (match-string 1) (cadr l
))))
719 (org-export-latex-protect-string rpl
)
721 (replace-match rpl t t
))))) quote-rpl
)))
723 ;; | chars/string in Org | normal environment | math environment |
724 ;; |-----------------------+-----------------------+-----------------------|
725 ;; | & # % $ | \& \# \% \$ | \& \# \% \$ |
726 ;; | { } _ ^ \ | \{ \} \_ \^ \\ | { } _ ^ \ |
727 ;; |-----------------------+-----------------------+-----------------------|
728 ;; | a_b and a^b | $a_b$ and $a^b$ | a_b and a^b |
729 ;; | a_abc and a_{abc} | $a_a$bc and $a_{abc}$ | a_abc and a_{abc} |
730 ;; | \tau and \mu | $\tau$ and $\mu$ | \tau and \mu |
731 ;; |-----------------------+-----------------------+-----------------------|
732 ;; | \_ \^ | \_ \^ | \_ \^ |
733 ;; | \(a=\mu\mbox{m}\) | \(a=\mu\mbox{m}\) | \(a=\mu\mbox{m}\) |
734 ;; | \[\beta^2-a=0\] | \[\beta^2-a=0\] | \[\beta^2-a=0\] |
735 ;; | $x=22\tau$ | $x=22\tau$ | $x=22\tau$ |
736 ;; | $$\alpha=\sqrt{a^3}$$ | $$\alpha=\sqrt{a^3}$$ | $$\alpha=\sqrt{a^3}$$ |
738 (defun org-export-latex-special-chars (sub-superscript)
739 "Export special characters to LaTeX.
740 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
741 See the `org-export-latex.el' code for a complete conversion table."
742 (goto-char (point-min))
744 (goto-char (point-min))
745 (while (re-search-forward c nil t
)
746 ;; Put the point where to check for org-protected
747 (unless (get-text-property (match-beginning 2) 'org-protected
)
748 (cond ((member (match-string 2) '("\\$" "$"))
749 (if (equal (match-string 2) "\\$")
750 (replace-match (concat (match-string 1) "$"
751 (match-string 3)) t t
)
752 (replace-match (concat (match-string 1) "\\$"
753 (match-string 3)) t t
)))
754 ((member (match-string 2) '("&" "%" "#"))
755 (if (equal (match-string 1) "\\")
756 (replace-match (match-string 2) t t
)
757 (replace-match (concat (match-string 1) "\\"
758 (match-string 2)) t t
)))
759 ((equal (match-string 2) "~")
760 (cond ((equal (match-string 1) "\\") nil
)
761 ((eq 'org-link
(get-text-property 0 'face
(match-string 2)))
762 (replace-match (concat (match-string 1) "\\~") t t
))
764 (org-export-latex-protect-string
765 (concat (match-string 1) "\\~{}")) t t
))))
766 ((member (match-string 2) '("{" "}"))
767 (unless (save-match-data (org-inside-LaTeX-fragment-p))
768 (if (equal (match-string 1) "\\")
769 (replace-match (match-string 2) t t
)
770 (replace-match (concat (match-string 1) "\\"
771 (match-string 2)) t t
)))))
772 (unless (save-match-data (org-inside-LaTeX-fragment-p))
773 (cond ((equal (match-string 2) "\\")
774 (replace-match (or (save-match-data
775 (org-export-latex-treat-backslash-char
777 (match-string 3))) "") t t
))
778 ((member (match-string 2) '("_" "^"))
779 (replace-match (or (save-match-data
780 (org-export-latex-treat-sub-super-char
784 (match-string 3))) "") t t
)))))))
785 '("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
786 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
787 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
794 ;; (?\< . "\\textless{}")
795 ;; (?\> . "\\textgreater{}")
798 (defun org-export-latex-treat-sub-super-char
799 (subsup string-before char string-after
)
800 "Convert the \"_\" and \"^\" characters to LaTeX.
801 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
802 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
803 (cond ((equal string-before
"\\")
804 (concat string-before char string-after
))
805 ;; this is part of a math formula
806 ((and (string-match "\\S-+" string-before
)
807 (string-match "\\S-+" string-after
))
808 (cond ((eq 'org-link
(get-text-property 0 'face char
))
809 (concat string-before
"\\" char string-after
))
810 ((save-match-data (org-inside-LaTeX-fragment-p))
812 (cond ((eq 1 (length string-after
))
813 (concat string-before char string-after
))
814 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after
)
815 (format "%s%s{%s}" string-before char
816 (match-string 1 string-after
))))))
818 (> (length string-after
) 1)
819 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after
))
820 (format "$%s%s{%s}$" string-before char
821 (match-string 1 string-after
)))
822 (subsup (concat "$" string-before char string-after
"$"))
823 (t (org-export-latex-protect-string
824 (concat string-before
"\\" char
"{}" string-after
)))))
825 (t (org-export-latex-protect-string
826 (concat string-before
"\\" char
"{}" string-after
)))))
828 (defun org-export-latex-treat-backslash-char (string-before string-after
)
829 "Convert the \"$\" special character to LaTeX.
830 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
831 (cond ((member (list string-after
) org-html-entities
)
832 ;; backslash is part of a special entity (like "\alpha")
833 (concat string-before
"$\\"
834 (or (cdar (member (list string-after
) org-html-entities
))
836 ((and (not (string-match "^[ \n\t]" string-after
))
837 (not (string-match "[ \t]\\'\\|^" string-before
)))
838 ;; backslash is inside a word
839 (org-export-latex-protect-string
840 (concat string-before
"\\textbackslash{}" string-after
)))
841 ((not (or (equal string-after
"")
842 (string-match "^[ \t\n]" string-after
)))
843 ;; backslash might escape a character (like \#) or a user TeX
844 ;; macro (like \setcounter)
845 (org-export-latex-protect-string
846 (concat string-before
"\\" string-after
)))
847 ((and (string-match "^[ \t\n]" string-after
)
848 (string-match "[ \t\n]\\'" string-before
))
849 ;; backslash is alone, convert it to $\backslash$
850 (org-export-latex-protect-string
851 (concat string-before
"\\textbackslash{}" string-after
)))
852 (t (org-export-latex-protect-string
853 (concat string-before
"\\textbackslash{}" string-after
)))))
855 (defun org-export-latex-keywords (timestamps)
856 "Convert special keywords to LaTeX.
857 Regexps are those from `org-latex-special-string-regexps'."
858 (let ((rg org-latex-special-string-regexps
) r
)
859 (while (setq r
(pop rg
))
860 (goto-char (point-min))
861 (while (re-search-forward (eval r
) nil t
)
863 (replace-match (format "\\\\texttt{%s}" (match-string 0)) t
)
864 (replace-match ""))))))
866 (defun org-export-latex-fixed-width (opt)
867 "When OPT is non-nil convert fixed-width sections to LaTeX."
868 (goto-char (point-min))
869 ;; FIXME the search shouldn't be performed on already converted text
870 (while (re-search-forward "^[ \t]*:" nil t
)
872 (progn (goto-char (match-beginning 0))
873 (insert "\\begin{verbatim}\n")
874 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
875 (replace-match (concat (match-string 1)
876 (match-string 2)) t t
)
878 (insert "\\end{verbatim}\n\n"))
879 (progn (goto-char (match-beginning 0))
880 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
881 (replace-match (concat "%" (match-string 1)
882 (match-string 2)) t t
)
885 (defun org-export-latex-lists ()
886 "Convert lists to LaTeX."
887 (goto-char (point-min))
888 (while (re-search-forward org-export-latex-list-beginning-re nil t
)
890 (org-export-list-to-latex
891 (org-export-latex-parse-list t
))))
893 (defun org-export-list-to-generic (list params
)
894 "Convert a LIST parsed through `org-export-latex-parse-list' to other formats.
898 :ustart String to start an unordered list
899 :uend String to end an unordered list
901 :ostart String to start an ordered list
902 :oend String to end an ordered list
904 :splice When set to t, return only list body lines, don't wrap
905 them into :[u/o]start and :[u/o]end. Default is nil.
907 :istart String to start a list item
908 :iend String to end a list item
909 :isep String to separate items
910 :lsep String to separate sublists"
912 (let* ((p params
) sublist
913 (splicep (plist-get p
:splice
))
914 (ostart (plist-get p
:ostart
))
915 (oend (plist-get p
:oend
))
916 (ustart (plist-get p
:ustart
))
917 (uend (plist-get p
:uend
))
918 (istart (plist-get p
:istart
))
919 (iend (plist-get p
:iend
))
920 (isep (plist-get p
:isep
))
921 (lsep (plist-get p
:lsep
)))
923 (cond ((eq (car list
) 'ordered
)
924 (concat ostart
"\n%s" oend
"\n"))
925 ((eq (car list
) 'unordered
)
926 (concat ustart
"\n%s" uend
"\n"))))
928 (while (setq sublist
(pop list
))
929 (cond ((symbolp sublist
) nil
)
931 (setq rtn
(concat rtn istart sublist iend isep
)))
933 (setq rtn
(concat rtn
;; previous list
934 lsep
;; list separator
935 (org-export-list-to-generic sublist p
)
936 lsep
;; list separator
938 (format wrapper rtn
))))
940 (defun org-export-list-to-latex (list)
941 "Convert LIST into a LaTeX list."
943 (org-export-list-to-generic
944 list
'(:splicep nil
:ostart
"\\begin{enumerate}" :oend
"\\end{enumerate}"
945 :ustart
"\\begin{itemize}" :uend
"\\end{itemize}"
946 :istart
"\\item " :iend
""
947 :isep
"\n" :lsep
"\n"))
948 ;; Add a trailing \n after list conversion
951 ;; FIXME Use org-export-highlight-first-table-line ?
952 (defun org-export-latex-tables (insert)
953 "Convert tables to LaTeX and INSERT it."
954 (goto-char (point-min))
955 (while (re-search-forward "^\\([ \t]*\\)|" nil t
)
956 ;; FIXME really need to save-excursion?
957 (save-excursion (org-table-align))
958 (let* ((beg (org-table-begin))
959 (end (org-table-end))
960 (raw-table (buffer-substring-no-properties beg end
))
961 fnum fields line lines olines gr colgropen line-fmt align
)
962 (if org-export-latex-tables-verbatim
963 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
964 "\\end{verbatim}\n")))
965 (apply 'delete-region
(list beg end
))
968 (setq lines
(split-string raw-table
"\n" t
))
969 (apply 'delete-region
(list beg end
))
970 (when org-export-table-remove-special-lines
971 (setq lines
(org-table-clean-before-export lines
)))
972 ;; make a formatting string to reflect aligment
974 (while (and (not line-fmt
) (setq line
(pop olines
)))
975 (unless (string-match "^[ \t]*|-" line
)
976 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
977 (setq fnum
(make-vector (length fields
) 0))
981 (setq gr
(pop org-table-colgroup-info
))
983 (cond ((eq gr
':start
)
984 (prog1 (if colgropen
"|" "")
987 (prog1 (if colgropen
"|" "|")
988 (setq colgropen nil
)))
990 (if (memq gr
'(:end
:startend
))
991 (progn (setq colgropen nil
) "|")
994 ;; maybe remove the first and last "|"
995 (when (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt
)
996 (setq line-fmt
(match-string 2 line-fmt
)))
998 (setq align
(apply 'format
1000 (mapcar (lambda (x) (if x
"r" "l"))
1001 org-table-last-alignment
))))
1002 ;; prepare the table to send to orgtbl-to-latex
1006 (or (and (string-match "[ \t]*|-+" elem
) 'hline
)
1007 (split-string (org-trim elem
) "|" t
)))
1010 (insert (orgtbl-to-latex
1011 lines
`(:tstart
,(concat "\\begin{tabular}{" align
"}")))
1014 (defun org-export-latex-fontify ()
1015 "Convert fontification to LaTeX."
1016 (goto-char (point-min))
1017 (while (re-search-forward org-emph-re nil t
)
1018 ;; The match goes one char after the *string*
1019 (let ((emph (assoc (match-string 3)
1020 org-export-latex-emphasis-alist
))
1022 (unless (get-text-property (1- (point)) 'org-protected
)
1023 (setq rpl
(concat (match-string 1)
1024 (format (org-export-latex-protect-char-in-string
1025 '("\\" "{" "}") (cadr emph
))
1029 (setq rpl
(org-export-latex-protect-string rpl
)))
1030 (replace-match rpl t t
)))
1033 (defun org-export-latex-links ()
1034 ;; Make sure to use the LaTeX hyperref and graphicx package
1035 ;; or send some warnings.
1036 "Convert links to LaTeX."
1037 (goto-char (point-min))
1038 (while (re-search-forward org-bracket-link-analytic-regexp nil t
)
1040 (goto-char (match-beginning 0))
1041 (let* ((re-radio org-latex-all-targets-regexp
)
1042 (remove (list (match-beginning 0) (match-end 0)))
1043 (type (match-string 2))
1044 (raw-path (match-string 3))
1045 (full-raw-path (concat (match-string 1) raw-path
))
1046 (desc (match-string 5))
1048 ;; define the path of the link
1050 ((member type
'("http" "https" "ftp"))
1051 (concat type
":" raw-path
))
1052 ((and re-radio
(string-match re-radio raw-path
))
1054 ((equal type
"mailto")
1055 (concat type
":" raw-path
))
1056 ((equal type
"file")
1057 (if (and (or (org-file-image-p (expand-file-name raw-path
))
1058 (string-match "\\.eps$" raw-path
))
1059 (equal desc full-raw-path
))
1061 (progn (when (string-match "\\(.+\\)::.+" raw-path
)
1062 (setq raw-path
(match-string 1 raw-path
)))
1063 (if (file-exists-p raw-path
)
1064 (concat type
"://" (expand-file-name raw-path
))
1065 (concat type
"://" (org-export-directory
1066 :LaTeX org-latex-options-plist
)
1068 ;; process with link inserting
1069 (apply 'delete-region remove
)
1070 (cond ((and imgp
(plist-get org-latex-options-plist
:inline-images
))
1071 (insert (format "\\includegraphics[%s]{%s}"
1072 ;; image option should be set be a comment line
1073 org-export-latex-image-default-option
1074 (expand-file-name raw-path
))))
1075 ;; FIXME: what about caption? image properties?
1076 (radiop (insert (format "\\hyperref[%s]{%s}" raw-path desc
)))
1077 (path (insert (format "\\href{%s}{%s}" path desc
)))
1078 (t (insert "\\texttt{" desc
"}")))))))
1080 (defun org-export-latex-cleaned-string
1081 ;; FIXME remove commentsp call in org.el and here
1082 (&optional commentsp
)
1083 "Clean stuff in the LaTeX export."
1085 ;; Preserve line breaks
1086 (goto-char (point-min))
1087 (while (re-search-forward "\\\\\\\\" nil t
)
1088 (add-text-properties (match-beginning 0) (match-end 0)
1089 '(org-protected t
)))
1091 ;; Convert LaTeX to \LaTeX{}
1092 (goto-char (point-min))
1093 (let ((case-fold-search nil
) rpl
)
1094 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t
)
1095 (replace-match (org-export-latex-protect-string
1096 (concat (match-string 1) "\\LaTeX{}")) t t
)))
1098 ;; Convert horizontal rules
1099 (goto-char (point-min))
1100 (while (re-search-forward "^----+.$" nil t
)
1101 (replace-match (org-export-latex-protect-string "\\hrule") t t
))
1103 ;; Protect LaTeX \commands{...}
1104 (goto-char (point-min))
1105 (while (re-search-forward "\\\\[a-zA-Z]+\\(?:\\[.*\\]\\)?{.*}" nil t
)
1106 (add-text-properties (match-beginning 0) (match-end 0)
1107 '(org-protected t
)))
1109 ;; Replace radio links
1110 (goto-char (point-min))
1111 (while (re-search-forward
1112 (concat "<<<?" org-latex-all-targets-regexp
1113 ">>>?\\((INVISIBLE)\\)?") nil t
)
1115 (org-export-latex-protect-string
1116 (format "\\label{%s}%s"(match-string 1)
1117 (if (match-string 2) "" (match-string 1)))) t t
))
1119 ;; Delete @<...> constructs
1120 (goto-char (point-min))
1121 ;; Thanks to Daniel Clemente for this regexp
1122 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t
)
1125 ;; When converting to LaTeX, replace footnotes
1126 ;; FIXME: don't protect footnotes from conversion
1127 (when (plist-get org-latex-options-plist
:footnotes
)
1128 (goto-char (point-min))
1129 (while (re-search-forward "\\[[0-9]+\\]" nil t
)
1130 (when (save-match-data
1131 (save-excursion (beginning-of-line)
1132 (looking-at "[^:|#]")))
1133 (let ((foot-beg (match-beginning 0))
1134 (foot-end (match-end 0))
1135 (foot-prefix (match-string 0))
1136 footnote footnote-rpl
)
1137 (when (and (re-search-forward (regexp-quote foot-prefix
) nil t
))
1139 (let ((end (save-excursion
1140 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t
)
1141 (match-beginning 0) (point-max)))))
1144 (org-trim (buffer-substring (point) end
))
1145 ;; FIXME stupid workaround for cases where
1146 ;; `org-bracket-link-analytic-regexp' matches
1147 ;; }. as part of the link.
1149 (delete-region (point) end
)))
1150 (goto-char foot-beg
)
1151 (delete-region foot-beg foot-end
)
1152 (setq footnote-rpl
(format "\\footnote{%s}" footnote
))
1153 (add-text-properties 0 10 '(org-protected t
) footnote-rpl
)
1154 (add-text-properties (1- (length footnote-rpl
))
1155 (length footnote-rpl
)
1156 '(org-protected t
) footnote-rpl
)
1157 (insert footnote-rpl
))))
1159 ;; Replace footnote section tag for LaTeX
1160 (goto-char (point-min))
1161 (while (re-search-forward
1162 (concat "^" footnote-section-tag-regexp
) nil t
)
1163 (replace-match ""))))
1165 (provide 'org-export-latex
)
1167 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
1168 ;;; org-export-latex.el ends here