Another bug fix in org-publish, related to the FORCE argument.
[org-mode/org-tableheadings.git] / org-export-latex.el
blob7c71b2711f6f1fa428c4788a24261bb9f0e07954
1 ;;; org-export-latex.el --- LaTeX exporter for org-mode
2 ;;
3 ;; Copyright (c) 2007, 2008 Free Software Foundation, Inc.
4 ;;
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: org-export-latex.el
7 ;; Version: 5.19
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
19 ;; later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful, but
22 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 ;; General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the Free
28 ;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
29 ;; MA 02110-1301, USA.
31 ;;; Commentary:
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'
45 ;;
46 ;;; Code:
48 (eval-when-compile
49 (require 'cl))
51 (require 'footnote)
52 (require 'org)
54 ;;; Variables:
55 (defvar org-export-latex-class nil)
56 (defvar org-export-latex-header nil)
57 (defvar org-export-latex-append-header nil)
58 (defvar org-export-latex-options-plist nil)
59 (defvar org-export-latex-todo-keywords-1 nil)
60 (defvar org-export-latex-all-targets-re nil)
61 (defvar org-export-latex-add-level 0)
62 (defvar org-export-latex-sectioning "")
63 (defvar org-export-latex-sectioning-depth 0)
64 (defvar org-export-latex-list-beginning-re
65 "^\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) +?")
67 (defvar org-export-latex-special-string-regexps
68 '(org-ts-regexp
69 org-scheduled-string
70 org-deadline-string
71 org-clock-string)
72 "A list of regexps to convert as special keywords.")
74 (defvar latexp) ; dynamically scoped from org.el
75 (defvar re-quote) ; dynamically scoped from org.el
76 (defvar commentsp) ; dynamically scoped from org.el
78 ;;; User variables:
80 (defcustom org-export-latex-default-class "article"
81 "The default LaTeX class."
82 :group 'org-export-latex
83 :type '(string :tag "LaTeX class"))
85 (defcustom org-export-latex-classes
86 '(("article"
87 "\\documentclass[11pt,a4paper]{article}
88 \\usepackage[utf8]{inputenc}
89 \\usepackage[T1]{fontenc}
90 \\usepackage{hyperref}"
91 ("\\section{%s}" . "\\section*{%s}")
92 ("\\subsection{%s}" . "\\subsection*{%s}")
93 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
94 ("\\paragraph{%s}" . "\\paragraph*{%s}")
95 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
96 ("report"
97 "\\documentclass[11pt,a4paper]{report}
98 \\usepackage[utf8]{inputenc}
99 \\usepackage[T1]{fontenc}
100 \\usepackage{hyperref}"
101 ("\\part{%s}" . "\\part*{%s}")
102 ("\\chapter{%s}" . "\\chapter*{%s}")
103 ("\\section{%s}" . "\\section*{%s}")
104 ("\\subsection{%s}" . "\\subsection*{%s}")
105 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
106 ("book"
107 "\\documentclass[11pt,a4paper]{book}
108 \\usepackage[utf8]{inputenc}
109 \\usepackage[T1]{fontenc}
110 \\usepackage{hyperref}"
111 ("\\part{%s}" . "\\part*{%s}")
112 ("\\chapter{%s}" . "\\chapter*{%s}")
113 ("\\section{%s}" . "\\section*{%s}")
114 ("\\subsection{%s}" . "\\subsection*{%s}")
115 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
116 "Alist of LaTeX classes and associated header and structure.
117 If #+LaTeX_CLASS is set in the buffer, use its value and the
118 associated information. Here is the structure of each cell:
120 \(class-name
121 header-string
122 (unnumbered-section numbered-section\)
123 ...\)
125 A %s formatter is mandatory in each section string and will be
126 replaced by the title of the section."
127 :group 'org-export-latex
128 :type '(repeat
129 (list (string :tag "LaTeX class")
130 (string :tag "LaTeX header")
131 (cons :tag "Level 1"
132 (string :tag "Numbered")
133 (string :tag "Unnumbered"))
134 (cons :tag "Level 2"
135 (string :tag "Numbered")
136 (string :tag "Unnumbered"))
137 (cons :tag "Level 3"
138 (string :tag "Numbered")
139 (string :tag "Unnumbered"))
140 (cons :tag "Level 4"
141 (string :tag "Numbered")
142 (string :tag "Unnumbered"))
143 (cons :tag "Level 5"
144 (string :tag "Numbered")
145 (string :tag "Unnumbered")))))
147 (defcustom org-export-latex-emphasis-alist
148 '(("*" "\\textbf{%s}" nil)
149 ("/" "\\emph{%s}" nil)
150 ("_" "\\underline{%s}" nil)
151 ("+" "\\texttt{%s}" nil)
152 ("=" "\\texttt{%s}" nil)
153 ("~" "\\texttt{%s}" t))
154 "Alist of LaTeX expressions to convert emphasis fontifiers.
155 Each element of the list is a list of three elements.
156 The first element is the character used as a marker for fontification.
157 The second element is a formatting string to wrap fontified text with.
158 The third element decides whether to protect converted text from other
159 conversions."
160 :group 'org-export-latex
161 :type 'alist)
163 (defcustom org-export-latex-title-command "\\maketitle"
164 "The command used to insert the title just after \\begin{document}.
165 If this string contains the formatting specification \"%s\" then
166 it will be used as a formatting string, passing the title as an
167 argument."
168 :group 'org-export-latex
169 :type 'string)
171 (defcustom org-export-latex-date-format
172 "%d %B %Y"
173 "Format string for \\date{...}."
174 :group 'org-export-latex
175 :type 'string)
177 (defcustom org-export-latex-tables-verbatim nil
178 "When non-nil, export tables as verbatim."
179 :group 'org-export-latex
180 :type 'boolean)
182 (defcustom org-export-latex-tables-column-borders nil
183 "When non-nil, group of columns are surrounded with borders,
184 XSeven if these borders are the outside borders of the table."
185 :group 'org-export-latex
186 :type 'boolean)
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
192 :type 'alist)
194 (defcustom org-export-latex-low-levels 'description
195 "How to convert sections below the current level of sectioning,
196 as specified by `org-export-headline-levels' or the value of \"H:\"
197 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."
214 :type 'plist
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
220 :type 'string)
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 (defcustom org-list-radio-list-templates
228 '((latex-mode "% BEGIN RECEIVE ORGLST %n
229 % END RECEIVE ORGLST %n
230 \\begin{comment}
231 #+ORGLST: SEND %n org-list-to-latex
232 | | |
233 \\end{comment}\n")
234 (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
235 @c END RECEIVE ORGLST %n
236 @ignore
237 #+ORGLST: SEND %n org-list-to-texinfo
238 | | |
239 @end ignore\n")
240 (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
241 <!-- END RECEIVE ORGLST %n -->
242 <!--
243 #+ORGLST: SEND %n org-list-to-html
244 | | |
245 -->\n"))
246 "Templates for radio lists in different major modes.
247 All occurrences of %n in a template will be replaced with the name of the
248 list, obtained by prompting the user."
249 :group 'org-plain-lists
250 :type '(repeat
251 (list (symbol :tag "Major mode")
252 (string :tag "Format"))))
254 ;;; Autoload functions:
256 ;;;###autoload
257 (defun org-export-as-latex-batch ()
258 "Call `org-export-as-latex', may be used in batch processing as
259 emacs --batch
260 --load=$HOME/lib/emacs/org.el
261 --eval \"(setq org-export-headline-levels 2)\"
262 --visit=MyFile --funcall org-export-as-latex-batch"
263 (org-export-as-latex org-export-headline-levels 'hidden))
265 ;;;###autoload
266 (defun org-export-as-latex-to-buffer (arg)
267 "Call `org-exort-as-latex` with output to a temporary buffer.
268 No file is created. The prefix ARG is passed through to `org-export-as-latex'."
269 (interactive "P")
270 (org-export-as-latex arg nil nil "*Org LaTeX Export*")
271 (switch-to-buffer-other-window "*Org LaTeX Export*"))
273 ;;;###autoload
274 (defun org-replace-region-by-latex (beg end)
275 "Replace the region from BEG to END with its LaTeX export.
276 It assumes the region has `org-mode' syntax, and then convert it to
277 LaTeX. This can be used in any buffer. For example, you could
278 write an itemized list in `org-mode' syntax in an LaTeX buffer and
279 then use this command to convert it."
280 (interactive "r")
281 (let (reg latex buf)
282 (save-window-excursion
283 (if (org-mode-p)
284 (setq latex (org-export-region-as-latex
285 beg end t 'string))
286 (setq reg (buffer-substring beg end)
287 buf (get-buffer-create "*Org tmp*"))
288 (save-excursion
289 (set-buffer buf)
290 (erase-buffer)
291 (insert reg)
292 (org-mode)
293 (setq latex (org-export-region-as-latex
294 (point-min) (point-max) t 'string)))
295 (kill-buffer buf)))
296 (delete-region beg end)
297 (insert latex)))
299 ;;;###autoload
300 (defun org-export-region-as-latex (beg end &optional body-only buffer)
301 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
302 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
303 contents, and only produce the region of converted text, useful for
304 cut-and-paste operations.
305 If BUFFER is a buffer or a string, use/create that buffer as a target
306 of the converted LaTeX. If BUFFER is the symbol `string', return the
307 produced LaTeX as a string and leave not buffer behind. For example,
308 a Lisp program could call this function in the following way:
310 (setq latex (org-export-region-as-latex beg end t 'string))
312 When called interactively, the output buffer is selected, and shown
313 in a window. A non-interactive call will only retunr the buffer."
314 (interactive "r\nP")
315 (when (interactive-p)
316 (setq buffer "*Org LaTeX Export*"))
317 (let ((transient-mark-mode t) (zmacs-regions t)
318 rtn)
319 (goto-char end)
320 (set-mark (point)) ;; to activate the region
321 (goto-char beg)
322 (setq rtn (org-export-as-latex
323 nil nil nil
324 buffer body-only))
325 (if (fboundp 'deactivate-mark) (deactivate-mark))
326 (if (and (interactive-p) (bufferp rtn))
327 (switch-to-buffer-other-window rtn)
328 rtn)))
330 ;;;###autoload
331 (defun org-export-as-latex (arg &optional hidden ext-plist
332 to-buffer body-only)
333 "Export current buffer to a LaTeX file.
334 If there is an active region, export only the region. The prefix
335 ARG specifies how many levels of the outline should become
336 headlines. The default is 3. Lower levels will be exported
337 depending on `org-export-latex-low-levels'. The default is to
338 convert them as description lists. When HIDDEN is non-nil, don't
339 display the LaTeX buffer. EXT-PLIST is a property list with
340 external parameters overriding org-mode's default settings, but
341 still inferior to file-local settings. When TO-BUFFER is
342 non-nil, create a buffer with that name and export to that
343 buffer. If TO-BUFFER is the symbol `string', don't leave any
344 buffer behind but just return the resulting LaTeX as a string.
345 When BODY-ONLY is set, don't produce the file header and footer,
346 simply return the content of \begin{document}...\end{document},
347 without even the \begin{document} and \end{document} commands."
348 (interactive "P")
349 ;; Make sure we have a file name when we need it.
350 (when (and (not (or to-buffer body-only))
351 (not buffer-file-name))
352 (if (buffer-base-buffer)
353 (org-set-local 'buffer-file-name
354 (with-current-buffer (buffer-base-buffer)
355 buffer-file-name))
356 (error "Need a file name to be able to export")))
358 (message "Exporting to LaTeX...")
359 (org-update-radio-target-regexp)
360 (org-export-latex-set-initial-vars ext-plist arg)
361 (let* ((wcf (current-window-configuration))
362 (opt-plist org-export-latex-options-plist)
363 (region-p (org-region-active-p))
364 (subtree-p
365 (when region-p
366 (save-excursion
367 (goto-char (region-beginning))
368 (and (org-at-heading-p)
369 (>= (org-end-of-subtree t t) (region-end))))))
370 (title (or (and subtree-p (org-export-get-title-from-subtree))
371 (plist-get opt-plist :title)
372 (and (not
373 (plist-get opt-plist :skip-before-1st-heading))
374 (org-export-grab-title-from-buffer))
375 (file-name-sans-extension
376 (file-name-nondirectory buffer-file-name))))
377 (filename (concat (file-name-as-directory
378 (org-export-directory :LaTeX ext-plist))
379 (file-name-sans-extension
380 (file-name-nondirectory ;sans-extension
381 buffer-file-name)) ".tex"))
382 (filename (if (equal (file-truename filename)
383 (file-truename buffer-file-name))
384 (concat filename ".tex")
385 filename))
386 (buffer (if to-buffer
387 (cond
388 ((eq to-buffer 'string) (get-buffer-create
389 "*Org LaTeX Export*"))
390 (t (get-buffer-create to-buffer)))
391 (find-file-noselect filename)))
392 (odd org-odd-levels-only)
393 (header (org-export-latex-make-header title opt-plist))
394 (skip (cond (subtree-p nil)
395 (region-p t)
396 ;; never skip first lines when exporting a subtree
397 (t (plist-get opt-plist :skip-before-1st-heading))))
398 (text (plist-get opt-plist :text))
399 (first-lines (if skip "" (org-export-latex-first-lines)))
400 (coding-system (and (boundp 'buffer-file-coding-system)
401 buffer-file-coding-system))
402 (coding-system-for-write (or org-export-latex-coding-system
403 coding-system))
404 (save-buffer-coding-system (or org-export-latex-coding-system
405 coding-system))
406 (region (buffer-substring
407 (if region-p (region-beginning) (point-min))
408 (if region-p (region-end) (point-max))))
409 (string-for-export
410 (org-cleaned-string-for-export
411 region :emph-multiline t
412 :for-LaTeX t
413 :comments nil
414 :add-text (if (eq to-buffer 'string) nil text)
415 :skip-before-1st-heading skip
416 :LaTeX-fragments nil)))
418 (set-buffer buffer)
419 (erase-buffer)
421 (and (fboundp 'set-buffer-file-coding-system)
422 (set-buffer-file-coding-system coding-system-for-write))
424 ;; insert the header and initial document commands
425 (unless (or (eq to-buffer 'string) body-only)
426 (insert header))
428 ;; insert text found in #+TEXT
429 (when (and text (not (eq to-buffer 'string)))
430 (insert (org-export-latex-content
431 text '(lists tables fixed-width keywords))
432 "\n\n"))
434 ;; insert lines before the first headline
435 (unless (or skip (eq to-buffer 'string))
436 (insert first-lines))
438 ;; handle the case where the region does not begin with a section
439 (when region-p
440 (insert (with-temp-buffer
441 (insert string-for-export)
442 (org-export-latex-first-lines))))
444 ;; export the content of headlines
445 (org-export-latex-global
446 (with-temp-buffer
447 (insert string-for-export)
448 (goto-char (point-min))
449 (when (re-search-forward "^\\(\\*+\\) " nil t)
450 (let* ((asters (length (match-string 1)))
451 (level (if odd (- asters 2) (- asters 1))))
452 (setq org-export-latex-add-level
453 (if odd (1- (/ (1+ asters) 2)) (1- asters)))
454 (org-export-latex-parse-global level odd)))))
456 ;; finalization
457 (unless body-only (insert "\n\\end{document}"))
458 (or to-buffer (save-buffer))
459 (goto-char (point-min))
460 (message "Exporting to LaTeX...done")
461 (prog1
462 (if (eq to-buffer 'string)
463 (prog1 (buffer-substring (point-min) (point-max))
464 (kill-buffer (current-buffer)))
465 (current-buffer))
466 (set-window-configuration wcf))))
468 ;;; Parsing functions:
470 (defun org-export-latex-parse-global (level odd)
471 "Parse the current buffer recursively, starting at LEVEL.
472 If ODD is non-nil, assume the buffer only contains odd sections.
473 Return a list reflecting the document structure."
474 (save-excursion
475 (goto-char (point-min))
476 (let* ((cnt 0) output
477 (depth org-export-latex-sectioning-depth))
478 (while (re-search-forward
479 (concat "^\\(\\(?:\\*\\)\\{"
480 (number-to-string (+ (if odd 2 1) level))
481 "\\}\\) \\(.*\\)$")
482 ;; make sure that there is no upper heading
483 (when (> level 0)
484 (save-excursion
485 (save-match-data
486 (re-search-forward
487 (concat "^\\(\\(?:\\*\\)\\{"
488 (number-to-string level)
489 "\\}\\) \\(.*\\)$") nil t)))) t)
490 (setq cnt (1+ cnt))
491 (let* ((pos (match-beginning 0))
492 (heading (match-string 2))
493 (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
494 (save-excursion
495 (narrow-to-region
496 (point)
497 (save-match-data
498 (if (re-search-forward
499 (concat "^\\(\\(?:\\*\\)\\{"
500 (number-to-string (+ (if odd 2 1) level))
501 "\\}\\) \\(.*\\)$") nil t)
502 (match-beginning 0)
503 (point-max))))
504 (goto-char (point-min))
505 (setq output
506 (append output
507 (list
508 (list
509 `(pos . ,pos)
510 `(level . ,nlevel)
511 `(occur . ,cnt)
512 `(heading . ,heading)
513 `(content . ,(org-export-latex-parse-content))
514 `(subcontent . ,(org-export-latex-parse-subcontent
515 level odd)))))))
516 (widen)))
517 (list output))))
519 (defun org-export-latex-parse-content ()
520 "Extract the content of a section."
521 (let ((beg (point))
522 (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t)
523 (progn (beginning-of-line) (point))
524 (point-max))))
525 (buffer-substring beg end)))
527 (defun org-export-latex-parse-subcontent (level odd)
528 "Extract the subcontent of a section at LEVEL.
529 If ODD Is non-nil, assume subcontent only contains odd sections."
530 (if (not (re-search-forward
531 (concat "^\\(\\(?:\\*\\)\\{"
532 (number-to-string (+ (if odd 4 2) level))
533 "\\}\\) \\(.*\\)$")
534 nil t))
535 nil ; subcontent is nil
536 (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
538 ;;; Rendering functions:
539 (defun org-export-latex-global (content)
540 "Export CONTENT to LaTeX.
541 CONTENT is an element of the list produced by
542 `org-export-latex-parse-global'."
543 (if (eq (car content) 'subcontent)
544 (mapc 'org-export-latex-sub (cdr content))
545 (org-export-latex-sub (car content))))
547 (defun org-export-latex-sub (subcontent)
548 "Export the list SUBCONTENT to LaTeX.
549 SUBCONTENT is an alist containing information about the headline
550 and its content."
551 (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
552 (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
554 (defun org-export-latex-subcontent (subcontent num)
555 "Export each cell of SUBCONTENT to LaTeX."
556 (let ((heading (org-export-latex-fontify-headline
557 (cdr (assoc 'heading subcontent))))
558 (level (- (cdr (assoc 'level subcontent))
559 org-export-latex-add-level))
560 (occur (number-to-string (cdr (assoc 'occur subcontent))))
561 (content (cdr (assoc 'content subcontent)))
562 (subcontent (cadr (assoc 'subcontent subcontent))))
563 (cond
564 ;; Normal conversion
565 ((<= level org-export-latex-sectioning-depth)
566 (let ((sec (nth (1- level) org-export-latex-sectioning)))
567 (insert (format (if num (car sec) (cdr sec)) heading) "\n"))
568 (insert (org-export-latex-content content))
569 (cond ((stringp subcontent) (insert subcontent))
570 ((listp subcontent) (org-export-latex-sub subcontent))))
571 ;; At a level under the hl option: we can drop this subsection
572 ((> level org-export-latex-sectioning-depth)
573 (cond ((eq org-export-latex-low-levels 'description)
574 (insert (format "\\begin{description}\n\n\\item[%s]\n\n" heading))
575 (insert (org-export-latex-content content))
576 (cond ((stringp subcontent) (insert subcontent))
577 ((listp subcontent) (org-export-latex-sub subcontent)))
578 (insert "\\end{description}\n"))
579 ((stringp org-export-latex-low-levels)
580 (insert (format org-export-latex-low-levels heading) "\n")
581 (insert (org-export-latex-content content))
582 (cond ((stringp subcontent) (insert subcontent))
583 ((listp subcontent) (org-export-latex-sub subcontent)))))))))
585 ;;; Exporting internals:
586 (defun org-export-latex-set-initial-vars (ext-plist level)
587 "Store org local variables required for LaTeX export.
588 EXT-PLIST is an optional additional plist.
589 LEVEL indicates the default depth for export."
590 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
591 org-export-latex-all-targets-re
592 (org-make-target-link-regexp (org-all-targets))
593 org-export-latex-options-plist
594 (org-combine-plists (org-default-export-plist) ext-plist
595 (org-infile-export-plist))
596 org-export-latex-class
597 (save-excursion
598 (goto-char (point-min))
599 (if (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
600 (assoc (match-string 1) org-export-latex-classes))
601 (match-string 1)
602 org-export-latex-default-class))
603 org-export-latex-header
604 (cadr (assoc org-export-latex-class org-export-latex-classes))
605 org-export-latex-sectioning
606 (cddr (assoc org-export-latex-class org-export-latex-classes))
607 org-export-latex-sectioning-depth
608 (or level
609 (let ((hl-levels
610 (plist-get org-export-latex-options-plist :headline-levels))
611 (sec-depth (length org-export-latex-sectioning)))
612 (if (> hl-levels sec-depth) sec-depth hl-levels)))))
614 (defun org-export-latex-make-header (title opt-plist)
615 "Make the LaTeX header and return it as a string.
616 TITLE is the current title from the buffer or region.
617 OPT-PLIST is the options plist for current buffer."
618 (let ((toc (plist-get opt-plist :table-of-contents))
619 (author (plist-get opt-plist :author)))
620 (concat
621 (if (plist-get opt-plist :time-stamp-file)
622 (format-time-string "% Created %Y-%m-%d %a %H:%M\n"))
623 ;; insert LaTeX custom header
624 org-export-latex-header
625 "\n"
626 ;; insert information on LaTeX packages
627 (when org-export-latex-packages-alist
628 (mapconcat (lambda(p)
629 (if (equal "" (car p))
630 (format "\\usepackage{%s}" (cadr p))
631 (format "\\usepackage[%s]{%s}"
632 (car p) (cadr p))))
633 org-export-latex-packages-alist "\n"))
634 ;; insert additional commands in the header
635 org-export-latex-append-header
636 ;; insert the title
637 (format
638 "\n\n\\title{%s}\n"
639 ;; convert the title
640 (org-export-latex-content
641 title '(lists tables fixed-width keywords)))
642 ;; insert author info
643 (if (plist-get opt-plist :author-info)
644 (format "\\author{%s}\n"
645 (or author user-full-name))
646 (format "%%\\author{%s}\n"
647 (or author user-full-name)))
648 ;; insert the date
649 (format "\\date{%s}\n"
650 (format-time-string
651 (or (plist-get opt-plist :date)
652 org-export-latex-date-format)))
653 ;; beginning of the document
654 "\n\\begin{document}\n\n"
655 ;; insert the title command
656 (if (string-match "%s" org-export-latex-title-command)
657 (format org-export-latex-title-command title)
658 org-export-latex-title-command)
659 "\n\n"
660 ;; table of contents
661 (when (and org-export-with-toc
662 (plist-get opt-plist :section-numbers))
663 (cond ((numberp toc)
664 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
665 (min toc (plist-get opt-plist :headline-levels))))
666 (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
667 (plist-get opt-plist :headline-levels))))))))
669 (defun org-export-latex-first-lines (&optional comments)
670 "Export the first lines before first headline.
671 COMMENTS is either nil to replace them with the empty string or a
672 formatting string like %%%%s if we want to comment them out."
673 (save-excursion
674 (goto-char (point-min))
675 (if (org-at-heading-p) (beginning-of-line 2))
676 (let* ((pt (point))
677 (end (if (and (re-search-forward "^\\* " nil t)
678 (not (eq pt (match-beginning 0))))
679 (goto-char (match-beginning 0))
680 (goto-char (point-max)))))
681 (org-export-latex-content
682 (org-cleaned-string-for-export
683 (buffer-substring (point-min) end)
684 :for-LaTeX t
685 :emph-multiline t
686 :add-text nil
687 :comments nil
688 :skip-before-1st-heading nil
689 :LaTeX-fragments nil)))))
691 (defun org-export-latex-content (content &optional exclude-list)
692 "Convert CONTENT string to LaTeX.
693 Don't perform conversions that are in EXCLUDE-LIST. Recognized
694 conversion types are: quotation-marks, emphasis, sub-superscript,
695 links, keywords, lists, tables, fixed-width"
696 (with-temp-buffer
697 (insert content)
698 (unless (memq 'quotation-marks exclude-list)
699 (org-export-latex-quotation-marks))
700 (unless (memq 'emphasis exclude-list)
701 (when (plist-get org-export-latex-options-plist :emphasize)
702 (org-export-latex-fontify)))
703 (unless (memq 'sub-superscript exclude-list)
704 (org-export-latex-special-chars
705 (plist-get org-export-latex-options-plist :sub-superscript)))
706 (unless (memq 'links exclude-list)
707 (org-export-latex-links))
708 (unless (memq 'keywords exclude-list)
709 (org-export-latex-keywords
710 (plist-get org-export-latex-options-plist :timestamps)))
711 (unless (memq 'lists exclude-list)
712 (org-export-latex-lists))
713 (unless (memq 'tables exclude-list)
714 (org-export-latex-tables
715 (plist-get org-export-latex-options-plist :tables)))
716 (unless (memq 'fixed-width exclude-list)
717 (org-export-latex-fixed-width
718 (plist-get org-export-latex-options-plist :fixed-width)))
719 ;; return string
720 (buffer-substring (point-min) (point-max))))
722 (defun org-export-latex-protect-string (s)
723 "Prevent further conversion for string S by adding the
724 org-protect property."
725 (add-text-properties 0 (length s) '(org-protected t) s) s)
727 (defun org-export-latex-protect-char-in-string (char-list string)
728 "Add org-protected text-property to char from CHAR-LIST in STRING."
729 (with-temp-buffer
730 (save-match-data
731 (insert string)
732 (goto-char (point-min))
733 (while (re-search-forward (regexp-opt char-list) nil t)
734 (add-text-properties (match-beginning 0)
735 (match-end 0) '(org-protected t)))
736 (buffer-string))))
738 (defun org-export-latex-keywords-maybe (remove-list)
739 "Maybe remove keywords depending on rules in REMOVE-LIST."
740 (goto-char (point-min))
741 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
742 (case-fold-search nil))
743 ;; convert TODO keywords
744 (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
745 (if (plist-get remove-list :todo)
746 (replace-match "")
747 (replace-match (format "\\texttt{%s}" (match-string 1)) t t)))
748 ;; convert priority string
749 (when (re-search-forward "\\[\\\\#.\\]" nil t)
750 (if (plist-get remove-list :priority)
751 (replace-match "")
752 (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))
753 ;; convert tags
754 (when (re-search-forward "\\(:[a-zA-Z0-9]+\\)+:" nil t)
755 (if (or (not org-export-with-tags)
756 (plist-get remove-list :tags))
757 (replace-match "")
758 (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))))
760 (defun org-export-latex-fontify-headline (string)
761 "Fontify special words in string."
762 (with-temp-buffer
763 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
764 ;; the beginning of the buffer - inserting "\n" is safe here though.
765 (insert "\n" string)
766 (goto-char (point-min))
767 (when (plist-get org-export-latex-options-plist :emphasize)
768 (org-export-latex-fontify))
769 (org-export-latex-special-chars
770 (plist-get org-export-latex-options-plist :sub-superscript))
771 (org-export-latex-keywords-maybe
772 org-export-latex-remove-from-headlines)
773 (org-export-latex-links)
774 (org-trim (buffer-substring-no-properties (point-min) (point-max)))))
776 (defun org-export-latex-quotation-marks ()
777 "Export question marks depending on language conventions."
778 (let* ((lang (plist-get org-export-latex-options-plist :language))
779 (quote-rpl (if (equal lang "fr")
780 '(("\\(\\s-\\)\"" "«~")
781 ("\\(\\S-\\)\"" "~»")
782 ("\\(\\s-\\)'" "`"))
783 '(("\\(\\s-\\)\"" "``")
784 ("\\(\\S-\\)\"" "''")
785 ("\\(\\s-\\)'" "`")))))
786 (mapc (lambda(l) (goto-char (point-min))
787 (while (re-search-forward (car l) nil t)
788 (let ((rpl (concat (match-string 1) (cadr l))))
789 (org-export-latex-protect-string rpl)
790 (org-if-unprotected
791 (replace-match rpl t t))))) quote-rpl)))
793 (defun org-export-latex-special-chars (sub-superscript)
794 "Export special characters to LaTeX.
795 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
796 See the `org-export-latex.el' code for a complete conversion table."
797 (goto-char (point-min))
798 (mapc (lambda(c)
799 (goto-char (point-min))
800 (while (re-search-forward c nil t)
801 ;; Put the point where to check for org-protected
802 (unless (or (get-text-property (match-beginning 2) 'org-protected)
803 (org-at-table-p))
804 (cond ((member (match-string 2) '("\\$" "$"))
805 (if (equal (match-string 2) "\\$")
806 (replace-match (concat (match-string 1) "$"
807 (match-string 3)) t t)
808 (replace-match (concat (match-string 1) "\\$"
809 (match-string 3)) t t)))
810 ((member (match-string 2) '("&" "%" "#"))
811 (if (equal (match-string 1) "\\")
812 (replace-match (match-string 2) t t)
813 (replace-match (concat (match-string 1) "\\"
814 (match-string 2)) t t)))
815 ((equal (match-string 2) "...")
816 (replace-match
817 (concat (match-string 1)
818 (org-export-latex-protect-string "\\ldots{}")) t t))
819 ((equal (match-string 2) "~")
820 (cond ((equal (match-string 1) "\\") nil)
821 ((eq 'org-link (get-text-property 0 'face (match-string 2)))
822 (replace-match (concat (match-string 1) "\\~") t t))
823 (t (replace-match
824 (org-export-latex-protect-string
825 (concat (match-string 1) "\\~{}")) t t))))
826 ((member (match-string 2) '("{" "}"))
827 (unless (save-match-data (org-inside-LaTeX-fragment-p))
828 (if (equal (match-string 1) "\\")
829 (replace-match (match-string 2) t t)
830 (replace-match (concat (match-string 1) "\\"
831 (match-string 2)) t t)))))
832 (unless (save-match-data (org-inside-LaTeX-fragment-p))
833 (cond ((equal (match-string 2) "\\")
834 (replace-match (or (save-match-data
835 (org-export-latex-treat-backslash-char
836 (match-string 1)
837 (match-string 3))) "") t t))
838 ((member (match-string 2) '("_" "^"))
839 (replace-match (or (save-match-data
840 (org-export-latex-treat-sub-super-char
841 sub-superscript
842 (match-string 1)
843 (match-string 2)
844 (match-string 3))) "") t t)))))))
845 '("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
846 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
847 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
848 "\\(.\\|^\\)\\(&\\)"
849 "\\(.\\|^\\)\\(#\\)"
850 "\\(.\\|^\\)\\(%\\)"
851 "\\(.\\|^\\)\\({\\)"
852 "\\(.\\|^\\)\\(}\\)"
853 "\\(.\\|^\\)\\(~\\)"
854 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
855 ;; (?\< . "\\textless{}")
856 ;; (?\> . "\\textgreater{}")
859 (defun org-export-latex-treat-sub-super-char
860 (subsup string-before char string-after)
861 "Convert the \"_\" and \"^\" characters to LaTeX.
862 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
863 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
864 (cond ((equal string-before "\\")
865 (concat string-before char string-after))
866 ;; this is part of a math formula
867 ((and (string-match "\\S-+" string-before)
868 (string-match "\\S-+" string-after))
869 (cond ((eq 'org-link (get-text-property 0 'face char))
870 (concat string-before "\\" char string-after))
871 ((save-match-data (org-inside-LaTeX-fragment-p))
872 (if subsup
873 (cond ((eq 1 (length string-after))
874 (concat string-before char string-after))
875 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
876 (format "%s%s{%s}" string-before char
877 (match-string 1 string-after))))))
878 ((and subsup
879 (> (length string-after) 1)
880 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
881 (format "$%s%s{%s}$" string-before char
882 (match-string 1 string-after)))
883 (subsup (concat "$" string-before char string-after "$"))
884 (t (org-export-latex-protect-string
885 (concat string-before "\\" char "{}" string-after)))))
886 (t (org-export-latex-protect-string
887 (concat string-before "\\" char "{}" string-after)))))
889 (defun org-export-latex-treat-backslash-char (string-before string-after)
890 "Convert the \"$\" special character to LaTeX.
891 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
892 (cond ((member (list string-after) org-html-entities)
893 ;; backslash is part of a special entity (like "\alpha")
894 (concat string-before "$\\"
895 (or (cdar (member (list string-after) org-html-entities))
896 string-after) "$"))
897 ((and (not (string-match "^[ \n\t]" string-after))
898 (not (string-match "[ \t]\\'\\|^" string-before)))
899 ;; backslash is inside a word
900 (org-export-latex-protect-string
901 (concat string-before "\\textbackslash{}" string-after)))
902 ((not (or (equal string-after "")
903 (string-match "^[ \t\n]" string-after)))
904 ;; backslash might escape a character (like \#) or a user TeX
905 ;; macro (like \setcounter)
906 (org-export-latex-protect-string
907 (concat string-before "\\" string-after)))
908 ((and (string-match "^[ \t\n]" string-after)
909 (string-match "[ \t\n]\\'" string-before))
910 ;; backslash is alone, convert it to $\backslash$
911 (org-export-latex-protect-string
912 (concat string-before "\\textbackslash{}" string-after)))
913 (t (org-export-latex-protect-string
914 (concat string-before "\\textbackslash{}" string-after)))))
916 (defun org-export-latex-keywords (timestamps)
917 "Convert special keywords to LaTeX.
918 Regexps are those from `org-export-latex-special-string-regexps'."
919 (let ((rg org-export-latex-special-string-regexps) r)
920 (while (setq r (pop rg))
921 (goto-char (point-min))
922 (while (re-search-forward (eval r) nil t)
923 (if (not timestamps)
924 (replace-match (format "\\\\texttt{%s}" (match-string 0)) t)
925 (replace-match ""))))))
927 (defun org-export-latex-fixed-width (opt)
928 "When OPT is non-nil convert fixed-width sections to LaTeX."
929 (goto-char (point-min))
930 (while (re-search-forward "^[ \t]*:" nil t)
931 (if opt
932 (progn (goto-char (match-beginning 0))
933 (insert "\\begin{verbatim}\n")
934 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
935 (replace-match (concat (match-string 1)
936 (match-string 2)) t t)
937 (forward-line))
938 (insert "\\end{verbatim}\n\n"))
939 (progn (goto-char (match-beginning 0))
940 (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
941 (replace-match (concat "%" (match-string 1)
942 (match-string 2)) t t)
943 (forward-line))))))
945 (defun org-export-latex-tables (insert)
946 "Convert tables to LaTeX and INSERT it."
947 (goto-char (point-min))
948 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
949 ;; FIXME really need to save-excursion?
950 (save-excursion (org-table-align))
951 (let* ((beg (org-table-begin))
952 (end (org-table-end))
953 (raw-table (buffer-substring-no-properties beg end))
954 fnum fields line lines olines gr colgropen line-fmt align)
955 (if org-export-latex-tables-verbatim
956 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
957 "\\end{verbatim}\n")))
958 (apply 'delete-region (list beg end))
959 (insert tbl))
960 (progn
961 (setq lines (split-string raw-table "\n" t))
962 (apply 'delete-region (list beg end))
963 (when org-export-table-remove-special-lines
964 (setq lines (org-table-clean-before-export lines)))
965 ;; make a formatting string to reflect aligment
966 (setq olines lines)
967 (while (and (not line-fmt) (setq line (pop olines)))
968 (unless (string-match "^[ \t]*|-" line)
969 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
970 (setq fnum (make-vector (length fields) 0))
971 (setq line-fmt
972 (mapconcat
973 (lambda (x)
974 (setq gr (pop org-table-colgroup-info))
975 (format "%s%%s%s"
976 (cond ((eq gr ':start)
977 (prog1 (if colgropen "|" "")
978 (setq colgropen t)))
979 ((eq gr ':startend)
980 (prog1 (if colgropen "|" "|")
981 (setq colgropen nil)))
982 (t ""))
983 (if (memq gr '(:end :startend))
984 (progn (setq colgropen nil) "|")
985 "")))
986 fnum ""))))
987 ;; fix double || in line-fmt
988 (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
989 ;; maybe remove the first and last "|"
990 (when (and (not org-export-latex-tables-column-borders)
991 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
992 (setq line-fmt (match-string 2 line-fmt)))
993 ;; format alignment
994 (setq align (apply 'format
995 (cons line-fmt
996 (mapcar (lambda (x) (if x "r" "l"))
997 org-table-last-alignment))))
998 ;; prepare the table to send to orgtbl-to-latex
999 (setq lines
1000 (mapcar
1001 (lambda(elem)
1002 (or (and (string-match "[ \t]*|-+" elem) 'hline)
1003 (split-string (org-trim elem) "|" t)))
1004 lines))
1005 (when insert
1006 (insert (orgtbl-to-latex
1007 lines `(:tstart ,(concat "\\begin{tabular}{" align "}")))
1008 "\n\n")))))))
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))
1017 rpl)
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))
1022 (match-string 4))
1023 (match-string 5)))
1024 (if (caddr emph)
1025 (setq rpl (org-export-latex-protect-string rpl)))
1026 (replace-match rpl t t)))
1027 (backward-char)))
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)
1035 (org-if-unprotected
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 (match-string 3))
1041 (full-raw-path (concat (match-string 1) raw-path))
1042 (desc (match-string 5))
1043 imgp radiop
1044 ;; define the path of the link
1045 (path (cond
1046 ((member type '("http" "https" "ftp"))
1047 (concat type ":" raw-path))
1048 ((and re-radio (string-match re-radio raw-path))
1049 (setq radiop t))
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))
1056 (setq imgp t)
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)
1063 raw-path))))))))
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-cleaned-string ()
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 ;; Convert LaTeX to \LaTeX{}
1087 (goto-char (point-min))
1088 (let ((case-fold-search nil) rpl)
1089 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
1090 (replace-match (org-export-latex-protect-string
1091 (concat (match-string 1) "\\LaTeX{}")) t t)))
1093 ;; Convert horizontal rules
1094 (goto-char (point-min))
1095 (while (re-search-forward "^----+.$" nil t)
1096 (replace-match (org-export-latex-protect-string "\\hrule") t t))
1098 ;; Protect LaTeX commands like \commad[...]{...} or \command{...}
1099 (goto-char (point-min))
1100 (while (re-search-forward "\\\\[a-zA-Z]+\\(?:\\[.*\\]\\)?{.*}" nil t)
1101 (add-text-properties (match-beginning 0) (match-end 0)
1102 '(org-protected t)))
1104 ;; Protect LaTeX entities
1105 (goto-char (point-min))
1106 (while (re-search-forward (regexp-opt org-latex-entities) nil t)
1107 (add-text-properties (match-beginning 0) (match-end 0)
1108 '(org-protected t)))
1110 ;; Replace radio links
1111 (goto-char (point-min))
1112 (while (re-search-forward
1113 (concat "<<<?" org-export-latex-all-targets-re
1114 ">>>?\\((INVISIBLE)\\)?") nil t)
1115 (replace-match
1116 (org-export-latex-protect-string
1117 (format "\\label{%s}%s"(match-string 1)
1118 (if (match-string 2) "" (match-string 1)))) t t))
1120 ;; Delete @<...> constructs
1121 ;; Thanks to Daniel Clemente for this regexp
1122 (goto-char (point-min))
1123 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
1124 (replace-match ""))
1126 ;; When converting to LaTeX, replace footnotes
1127 ;; FIXME: don't protect footnotes from conversion
1128 (when (plist-get org-export-latex-options-plist :footnotes)
1129 (goto-char (point-min))
1130 (while (re-search-forward "\\[[0-9]+\\]" nil t)
1131 (when (save-match-data
1132 (save-excursion (beginning-of-line)
1133 (looking-at "[^:|#]")))
1134 (let ((foot-beg (match-beginning 0))
1135 (foot-end (match-end 0))
1136 (foot-prefix (match-string 0))
1137 footnote footnote-rpl)
1138 (save-excursion
1139 (when (search-forward foot-prefix nil t)
1140 (replace-match "")
1141 (let ((end (save-excursion
1142 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
1143 (match-beginning 0) (point-max)))))
1144 (setq footnote (concat (org-trim (buffer-substring (point) end))
1145 " ")) ; prevent last } being part of a link
1146 (delete-region (point) end))
1147 (goto-char foot-beg)
1148 (delete-region foot-beg foot-end)
1149 (unless (null footnote)
1150 (setq footnote-rpl (format "\\footnote{%s}" footnote))
1151 (add-text-properties 0 10 '(org-protected t) footnote-rpl)
1152 (add-text-properties (1- (length footnote-rpl))
1153 (length footnote-rpl)
1154 '(org-protected t) footnote-rpl)
1155 (insert footnote-rpl)))))))
1157 ;; Replace footnote section tag for LaTeX
1158 (goto-char (point-min))
1159 (while (re-search-forward
1160 (concat "^" footnote-section-tag-regexp) nil t)
1161 (replace-match ""))))
1163 ;;; List handling:
1165 (defun org-export-latex-lists ()
1166 "Replace plain text lists in current buffer into LaTeX lists."
1167 "Convert lists to LaTeX."
1168 (goto-char (point-min))
1169 (while (re-search-forward org-export-latex-list-beginning-re nil t)
1170 (beginning-of-line)
1171 (insert (org-list-to-latex (org-list-parse-list t)) "\n")))
1173 (defun org-list-parse-list (&optional delete)
1174 "Parse the list at point.
1175 Return a list containing first level items as strings and
1176 sublevels as a list of strings."
1177 (let ((start (org-list-item-begin))
1178 (end (org-list-end))
1179 output itemsep)
1180 (while (re-search-forward org-export-latex-list-beginning-re end t)
1181 (setq itemsep (if (save-match-data
1182 (string-match "^[0-9]" (match-string 2)))
1183 "[0-9]+\\(?:\\.\\|)\\)" "[-+]"))
1184 (let* ((indent1 (match-string 1))
1185 (nextitem (save-excursion
1186 (save-match-data
1187 (or (and (re-search-forward
1188 (concat "^" indent1 itemsep " *?") end t)
1189 (match-beginning 0)) end))))
1190 (item (buffer-substring
1191 (point)
1192 (or (and (re-search-forward
1193 org-export-latex-list-beginning-re end t)
1194 (goto-char (match-beginning 0)))
1195 (goto-char end))))
1196 (nextindent (match-string 1))
1197 (item (org-trim item))
1198 (item (if (string-match "^\\[.+\\]" item)
1199 (replace-match "\\\\texttt{\\&}"
1200 t nil item) item)))
1201 (push item output)
1202 (when (> (length nextindent)
1203 (length indent1))
1204 (narrow-to-region (point) nextitem)
1205 (push (org-list-parse-list) output)
1206 (widen))))
1207 (when delete (delete-region start end))
1208 (setq output (nreverse output))
1209 (push (if (string-match "^\\[0" itemsep)
1210 'ordered 'unordered) output)))
1212 (defun org-list-item-begin ()
1213 "Find the beginning of the list item and return its position."
1214 (save-excursion
1215 (if (not (or (looking-at org-export-latex-list-beginning-re)
1216 (re-search-backward
1217 org-export-latex-list-beginning-re nil t)))
1218 (progn (goto-char (point-min)) (point))
1219 (match-beginning 0))))
1221 (defun org-list-end ()
1222 "Find the end of the list and return its position."
1223 (save-excursion
1224 (catch 'exit
1225 (while (or (looking-at org-export-latex-list-beginning-re)
1226 (looking-at "^[ \t]+\\|^$"))
1227 (if (eq (point) (point-max))
1228 (throw 'exit (point-max)))
1229 (forward-line 1))) (point)))
1231 (defun org-list-insert-radio-list ()
1232 "Insert a radio list template appropriate for this major mode."
1233 (interactive)
1234 (let* ((e (assq major-mode org-list-radio-list-templates))
1235 (txt (nth 1 e))
1236 name pos)
1237 (unless e (error "No radio list setup defined for %s" major-mode))
1238 (setq name (read-string "List name: "))
1239 (while (string-match "%n" txt)
1240 (setq txt (replace-match name t t txt)))
1241 (or (bolp) (insert "\n"))
1242 (setq pos (point))
1243 (insert txt)
1244 (goto-char pos)))
1246 (defun org-list-send-list (&optional maybe)
1247 "Send a tranformed version of this list to the receiver position.
1248 With argument MAYBE, fail quietly if no transformation is defined for
1249 this list."
1250 (interactive)
1251 (catch 'exit
1252 (unless (org-at-item-p) (error "Not at a list"))
1253 (save-excursion
1254 (goto-char (org-list-item-begin))
1255 (beginning-of-line 0)
1256 (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
1257 (if maybe
1258 (throw 'exit nil)
1259 (error "Don't know how to transform this list"))))
1260 (let* ((name (match-string 1))
1262 (transform (intern (match-string 2)))
1263 (txt (buffer-substring-no-properties
1264 (org-list-item-begin)
1265 (org-list-end)))
1266 (list (org-list-parse-list)))
1267 (unless (fboundp transform)
1268 (error "No such transformation function %s" transform))
1269 (setq txt (funcall transform list))
1270 ;; Find the insertion place
1271 (save-excursion
1272 (goto-char (point-min))
1273 (unless (re-search-forward
1274 (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
1275 (error "Don't know where to insert translated list"))
1276 (goto-char (match-beginning 0))
1277 (beginning-of-line 2)
1278 (setq beg (point))
1279 (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
1280 (error "Cannot find end of insertion region"))
1281 (beginning-of-line 1)
1282 (delete-region beg (point))
1283 (goto-char beg)
1284 (insert txt "\n"))
1285 (message "List converted and installed at receiver location"))))
1287 (defun org-list-to-generic (list params)
1288 "Convert a LIST parsed through `org-list-parse-list' to other formats.
1290 Valid parameters are
1292 :ustart String to start an unordered list
1293 :uend String to end an unordered list
1295 :ostart String to start an ordered list
1296 :oend String to end an ordered list
1298 :splice When set to t, return only list body lines, don't wrap
1299 them into :[u/o]start and :[u/o]end. Default is nil.
1301 :istart String to start a list item
1302 :iend String to end a list item
1303 :isep String to separate items
1304 :lsep String to separate sublists"
1305 (interactive)
1306 (let* ((p params) sublist
1307 (splicep (plist-get p :splice))
1308 (ostart (plist-get p :ostart))
1309 (oend (plist-get p :oend))
1310 (ustart (plist-get p :ustart))
1311 (uend (plist-get p :uend))
1312 (istart (plist-get p :istart))
1313 (iend (plist-get p :iend))
1314 (isep (plist-get p :isep))
1315 (lsep (plist-get p :lsep)))
1316 (let ((wrapper
1317 (cond ((eq (car list) 'ordered)
1318 (concat ostart "\n%s" oend "\n"))
1319 ((eq (car list) 'unordered)
1320 (concat ustart "\n%s" uend "\n"))))
1321 rtn)
1322 (while (setq sublist (pop list))
1323 (cond ((symbolp sublist) nil)
1324 ((stringp sublist)
1325 (setq rtn (concat rtn istart sublist iend isep)))
1327 (setq rtn (concat rtn ;; previous list
1328 lsep ;; list separator
1329 (org-list-to-generic sublist p)
1330 lsep ;; list separator
1331 )))))
1332 (format wrapper rtn))))
1334 (defun org-list-to-latex (list)
1335 "Convert LIST into a LaTeX list."
1336 (org-list-to-generic
1337 list '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
1338 :ustart "\\begin{itemize}" :uend "\\end{itemize}"
1339 :istart "\\item " :iend ""
1340 :isep "\n" :lsep "\n")))
1342 (defun org-list-to-html (list)
1343 "Convert LIST into a HTML list."
1344 (org-list-to-generic
1345 list '(:splicep nil :ostart "<ol>" :oend "</ol>"
1346 :ustart "<ul>" :uend "</ul>"
1347 :istart "<li>" :iend "</li>"
1348 :isep "\n" :lsep "\n")))
1350 (defun org-list-to-texinfo (list)
1351 "Convert LIST into a Texinfo list."
1352 (org-list-to-generic
1353 list '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
1354 :ustart "@enumerate" :uend "@end enumerate"
1355 :istart "@item\n" :iend ""
1356 :isep "\n" :lsep "\n")))
1358 (defconst org-latex-entities
1359 '("\\!"
1360 "\\'"
1361 "\\+"
1362 "\\,"
1363 "\\-"
1364 "\\:"
1365 "\\;"
1366 "\\<"
1367 "\\="
1368 "\\>"
1369 "\\Huge"
1370 "\\LARGE"
1371 "\\Large"
1372 "\\Styles"
1373 "\\\\"
1374 "\\`"
1375 "\\addcontentsline"
1376 "\\address"
1377 "\\addtocontents"
1378 "\\addtocounter"
1379 "\\addtolength"
1380 "\\addvspace"
1381 "\\alph"
1382 "\\appendix"
1383 "\\arabic"
1384 "\\author"
1385 "\\begin{array}"
1386 "\\begin{center}"
1387 "\\begin{description}"
1388 "\\begin{enumerate}"
1389 "\\begin{eqnarray}"
1390 "\\begin{equation}"
1391 "\\begin{figure}"
1392 "\\begin{flushleft}"
1393 "\\begin{flushright}"
1394 "\\begin{itemize}"
1395 "\\begin{list}"
1396 "\\begin{minipage}"
1397 "\\begin{picture}"
1398 "\\begin{quotation}"
1399 "\\begin{quote}"
1400 "\\begin{tabbing}"
1401 "\\begin{table}"
1402 "\\begin{tabular}"
1403 "\\begin{thebibliography}"
1404 "\\begin{theorem}"
1405 "\\begin{titlepage}"
1406 "\\begin{verbatim}"
1407 "\\begin{verse}"
1408 "\\bf"
1409 "\\bf"
1410 "\\bibitem"
1411 "\\bigskip"
1412 "\\cdots"
1413 "\\centering"
1414 "\\circle"
1415 "\\cite"
1416 "\\cleardoublepage"
1417 "\\clearpage"
1418 "\\cline"
1419 "\\closing"
1420 "\\dashbox"
1421 "\\date"
1422 "\\ddots"
1423 "\\dotfill"
1424 "\\em"
1425 "\\fbox"
1426 "\\flushbottom"
1427 "\\fnsymbol"
1428 "\\footnote"
1429 "\\footnotemark"
1430 "\\footnotesize"
1431 "\\footnotetext"
1432 "\\frac"
1433 "\\frame"
1434 "\\framebox"
1435 "\\hfill"
1436 "\\hline"
1437 "\\hrulespace"
1438 "\\hspace"
1439 "\\huge"
1440 "\\hyphenation"
1441 "\\include"
1442 "\\includeonly"
1443 "\\indent"
1444 "\\input"
1445 "\\it"
1446 "\\kill"
1447 "\\label"
1448 "\\large"
1449 "\\ldots"
1450 "\\line"
1451 "\\linebreak"
1452 "\\linethickness"
1453 "\\listoffigures"
1454 "\\listoftables"
1455 "\\location"
1456 "\\makebox"
1457 "\\maketitle"
1458 "\\mark"
1459 "\\mbox"
1460 "\\medskip"
1461 "\\multicolumn"
1462 "\\multiput"
1463 "\\newcommand"
1464 "\\newcounter"
1465 "\\newenvironment"
1466 "\\newfont"
1467 "\\newlength"
1468 "\\newline"
1469 "\\newpage"
1470 "\\newsavebox"
1471 "\\newtheorem"
1472 "\\nocite"
1473 "\\nofiles"
1474 "\\noindent"
1475 "\\nolinebreak"
1476 "\\nopagebreak"
1477 "\\normalsize"
1478 "\\onecolumn"
1479 "\\opening"
1480 "\\oval"
1481 "\\overbrace"
1482 "\\overline"
1483 "\\pagebreak"
1484 "\\pagenumbering"
1485 "\\pageref"
1486 "\\pagestyle"
1487 "\\par"
1488 "\\parbox"
1489 "\\put"
1490 "\\raggedbottom"
1491 "\\raggedleft"
1492 "\\raggedright"
1493 "\\raisebox"
1494 "\\ref"
1495 "\\rm"
1496 "\\roman"
1497 "\\rule"
1498 "\\savebox"
1499 "\\sc"
1500 "\\scriptsize"
1501 "\\setcounter"
1502 "\\setlength"
1503 "\\settowidth"
1504 "\\sf"
1505 "\\shortstack"
1506 "\\signature"
1507 "\\sl"
1508 "\\small"
1509 "\\smallskip"
1510 "\\sqrt"
1511 "\\tableofcontents"
1512 "\\telephone"
1513 "\\thanks"
1514 "\\thispagestyle"
1515 "\\tiny"
1516 "\\title"
1517 "\\tt"
1518 "\\twocolumn"
1519 "\\typein"
1520 "\\typeout"
1521 "\\underbrace"
1522 "\\underline"
1523 "\\usebox"
1524 "\\usecounter"
1525 "\\value"
1526 "\\vdots"
1527 "\\vector"
1528 "\\verb"
1529 "\\vfill"
1530 "\\vline"
1531 "\\vspace")
1532 "A list of LaTeX commands to be protected when performing conversion.")
1534 (provide 'org-export-latex)
1536 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
1537 ;;; org-export-latex.el ends here