Make latex preview also use the packages in `org-export-latex-packages-alist'.
[org-mode.git] / lisp / org-latex.el
blobdf7212e71d6f2d589a4690cb788a3602219f6923
1 ;;; org-latex.el --- LaTeX exporter for org-mode
2 ;;
3 ;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
4 ;;
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: org-latex.el
7 ;; Version: 6.29trans
8 ;; Author: Bastien Guerry <bzg AT altern DOT org>
9 ;; Maintainer: Carsten Dominik <carsten.dominik AT gmail DOT com>
10 ;; Keywords: org, wp, tex
11 ;; Description: Converts an org-mode buffer into LaTeX
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;;; Commentary:
30 ;; This library implements a LaTeX exporter for org-mode.
32 ;; It is part of Org and will be autoloaded
34 ;; The interactive functions are similar to those of the HTML exporter:
36 ;; M-x `org-export-as-latex'
37 ;; M-x `org-export-as-pdf'
38 ;; M-x `org-export-as-pdf-and-open'
39 ;; M-x `org-export-as-latex-batch'
40 ;; M-x `org-export-as-latex-to-buffer'
41 ;; M-x `org-export-region-as-latex'
42 ;; M-x `org-replace-region-by-latex'
44 ;;; Code:
46 (eval-when-compile
47 (require 'cl))
49 (require 'footnote)
50 (require 'org)
51 (require 'org-exp)
53 ;;; Variables:
54 (defvar org-export-latex-class nil)
55 (defvar org-export-latex-header nil)
56 (defvar org-export-latex-append-header nil)
57 (defvar org-export-latex-options-plist nil)
58 (defvar org-export-latex-todo-keywords-1 nil)
59 (defvar org-export-latex-complex-heading-re nil)
60 (defvar org-export-latex-not-done-keywords nil)
61 (defvar org-export-latex-done-keywords nil)
62 (defvar org-export-latex-display-custom-times nil)
63 (defvar org-export-latex-all-targets-re nil)
64 (defvar org-export-latex-add-level 0)
65 (defvar org-export-latex-sectioning "")
66 (defvar org-export-latex-sectioning-depth 0)
67 (defvar org-export-latex-special-keyword-regexp
68 (concat "\\<\\(" org-scheduled-string "\\|"
69 org-deadline-string "\\|"
70 org-closed-string"\\)")
71 "Regexp matching special time planning keywords plus the time after it.")
73 (defvar latexp) ; dynamically scoped from org.el
74 (defvar re-quote) ; dynamically scoped from org.el
75 (defvar commentsp) ; dynamically scoped from org.el
77 ;;; User variables:
79 (defgroup org-export-latex nil
80 "Options for exporting Org-mode files to LaTeX."
81 :tag "Org Export LaTeX"
82 :group 'org-export)
84 (defcustom org-export-latex-default-class "article"
85 "The default LaTeX class."
86 :group 'org-export-latex
87 :type '(string :tag "LaTeX class"))
89 (defcustom org-export-latex-classes
90 '(("article"
91 "\\documentclass[11pt]{article}
92 \\usepackage[utf8]{inputenc}
93 \\usepackage[T1]{fontenc}
94 \\usepackage{graphicx}
95 \\usepackage{longtable}
96 \\usepackage{hyperref}"
97 ("\\section{%s}" . "\\section*{%s}")
98 ("\\subsection{%s}" . "\\subsection*{%s}")
99 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
100 ("\\paragraph{%s}" . "\\paragraph*{%s}")
101 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
102 ("report"
103 "\\documentclass[11pt]{report}
104 \\usepackage[utf8]{inputenc}
105 \\usepackage[T1]{fontenc}
106 \\usepackage{graphicx}
107 \\usepackage{longtable}
108 \\usepackage{hyperref}"
109 ("\\part{%s}" . "\\part*{%s}")
110 ("\\chapter{%s}" . "\\chapter*{%s}")
111 ("\\section{%s}" . "\\section*{%s}")
112 ("\\subsection{%s}" . "\\subsection*{%s}")
113 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
114 ("book"
115 "\\documentclass[11pt]{book}
116 \\usepackage[utf8]{inputenc}
117 \\usepackage[T1]{fontenc}
118 \\usepackage{graphicx}
119 \\usepackage{longtable}
120 \\usepackage{hyperref}"
121 ("\\part{%s}" . "\\part*{%s}")
122 ("\\chapter{%s}" . "\\chapter*{%s}")
123 ("\\section{%s}" . "\\section*{%s}")
124 ("\\subsection{%s}" . "\\subsection*{%s}")
125 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
126 "Alist of LaTeX classes and associated header and structure.
127 If #+LaTeX_CLASS is set in the buffer, use its value and the
128 associated information. Here is the structure of each cell:
130 \(class-name
131 header-string
132 (numbered-section . unnumbered-section\)
133 ...\)
135 A %s formatter is mandatory in each section string and will be
136 replaced by the title of the section.
138 Instead of a cons cell (numbered . unnumbered), you can also provide a list
139 of 2-4 elements,
141 (numbered-open numbered-close)
145 (numbered-open numbered-close unnumbered-open unnumbered-close)
147 providing opening and closing strings for an environment that should
148 represent the document section. The opening clause should have a %s
149 to represent the section title."
150 :group 'org-export-latex
151 :type '(repeat
152 (list (string :tag "LaTeX class")
153 (string :tag "LaTeX header")
154 (repeat :tag "Levels" :inline t
155 (choice
156 (cons :tag "Heading"
157 (string :tag "numbered")
158 (string :tag "unnumbered)"))
159 (list :tag "Environment"
160 (string :tag "Opening (numbered) ")
161 (string :tag "Closing (numbered) ")
162 (string :tag "Opening (unnumbered)")
163 (string :tag "Closing (unnumbered)")))))))
165 (defcustom org-export-latex-emphasis-alist
166 '(("*" "\\textbf{%s}" nil)
167 ("/" "\\emph{%s}" nil)
168 ("_" "\\underline{%s}" nil)
169 ("+" "\\texttt{%s}" nil)
170 ("=" "\\verb" t)
171 ("~" "\\verb" t))
172 "Alist of LaTeX expressions to convert emphasis fontifiers.
173 Each element of the list is a list of three elements.
174 The first element is the character used as a marker for fontification.
175 The second element is a formatting string to wrap fontified text with.
176 If it is \"\\verb\", Org will automatically select a deimiter
177 character that is not in the string.
178 The third element decides whether to protect converted text from other
179 conversions."
180 :group 'org-export-latex
181 :type 'alist)
183 (defcustom org-export-latex-title-command "\\maketitle"
184 "The command used to insert the title just after \\begin{document}.
185 If this string contains the formatting specification \"%s\" then
186 it will be used as a formatting string, passing the title as an
187 argument."
188 :group 'org-export-latex
189 :type 'string)
191 (defcustom org-export-latex-import-inbuffer-stuff nil
192 "Non-nil means define TeX macros for Org's inbuffer definitions.
193 For example \orgTITLE for #+TITLE."
194 :group 'org-export-latex
195 :type 'boolean)
197 (defcustom org-export-latex-date-format
198 "%d %B %Y"
199 "Format string for \\date{...}."
200 :group 'org-export-latex
201 :type 'string)
203 (defcustom org-export-latex-todo-keyword-markup "\\textbf{%s}"
204 "Markup for TODO keywords, as a printf format.
205 This can be a single format for all keywords, a cons cell with separate
206 formats for not-done and done states, or an association list with setup
207 for individual keywords. If a keyword shows up for which there is no
208 markup defined, the first one in the association list will be used."
209 :group 'org-export-latex
210 :type '(choice
211 (string :tag "Default")
212 (cons :tag "Distinguish undone and done"
213 (string :tag "Not-DONE states")
214 (string :tag "DONE states"))
215 (repeat :tag "Per keyword markup"
216 (cons
217 (string :tag "Keyword")
218 (string :tag "Markup")))))
220 (defcustom org-export-latex-timestamp-markup "\\textit{%s}"
221 "A printf format string to be applied to time stamps."
222 :group 'org-export-latex
223 :type 'string)
225 (defcustom org-export-latex-timestamp-keyword-markup "\\texttt{%s}"
226 "A printf format string to be applied to time stamps."
227 :group 'org-export-latex
228 :type 'string)
230 (defcustom org-export-latex-tables-verbatim nil
231 "When non-nil, tables are exported verbatim."
232 :group 'org-export-latex
233 :type 'boolean)
235 (defcustom org-export-latex-tables-centered t
236 "When non-nil, tables are exported in a center environment."
237 :group 'org-export-latex
238 :type 'boolean)
240 (defcustom org-export-latex-tables-column-borders nil
241 "When non-nil, grouping columns can cause outer vertical lines in tables.
242 When nil, grouping causes only separation lines between groups."
243 :group 'org-export-latex
244 :type 'boolean)
246 (defcustom org-export-latex-packages-alist nil
247 "Alist of packages to be inserted in the header.
248 Each cell is of the format \( \"option\" . \"package\" \)."
249 :group 'org-export-latex
250 :type '(repeat
251 (list
252 (string :tag "option")
253 (string :tag "package"))))
255 (defcustom org-export-latex-low-levels 'itemize
256 "How to convert sections below the current level of sectioning.
257 This is specified by the `org-export-headline-levels' option or the
258 value of \"H:\" in Org's #+OPTION line.
260 This can be either nil (skip the sections), `description', `itemize',
261 or `enumerate' (convert the sections as the corresponding list type), or
262 a string to be used instead of \\section{%s}. In this latter case,
263 the %s stands here for the inserted headline and is mandatory.
265 It may also be a list of three string to define a user-defined environment
266 that should be used. The first string should be the like
267 \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
268 to two occurrences of %s for the title and a lable, respectively. The third
269 string should be like \"\\end{itemize\"."
270 :group 'org-export-latex
271 :type '(choice (const :tag "Ignore" nil)
272 (const :tag "Convert as descriptive list" description)
273 (const :tag "Convert as itemized list" itemize)
274 (const :tag "Convert as enumerated list" enumerate)
275 (list :tag "User-defined environment"
276 :value ("\\begin{itemize}" "\\end{itemize}" "\\item %s")
277 (string :tag "Start")
278 (string :tag "End")
279 (string :tag "item"))
280 (string :tag "Use a section string" :value "\\subparagraph{%s}")))
282 (defcustom org-export-latex-list-parameters
283 '(:cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
284 "Parameters for the LaTeX list exporter.
285 These parameters will be passed on to `org-list-to-latex', which in turn
286 will pass them (combined with the LaTeX default list parameters) to
287 `org-list-to-generic'."
288 :group 'org-export-latex
289 :type 'plist)
291 (defcustom org-export-latex-verbatim-wrap
292 '("\\begin{verbatim}\n" . "\\end{verbatim}\n")
293 "Environment to be wrapped around a fixed-width section in LaTeX export.
294 This is a cons with two strings, to be added before and after the
295 fixed-with text.
297 Defaults to \\begin{verbatim} and \\end{verbatim}."
298 :group 'org-export-translation
299 :group 'org-export-latex
300 :type '(cons (string :tag "Open")
301 (string :tag "Close")))
303 (defcustom org-export-latex-listings nil
304 "Non-nil means, export source code using the listings package.
305 This package will fontify source code, possibly even with color.
306 If you want to use this, you also need to make LaTeX use the
307 listings package, and if you want to have color, the color
308 package. Just add these to `org-export-latex-packages-alist',
309 for example using customize, or with something like
311 (require 'org-latex)
312 (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
313 (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))"
314 :group 'org-export-latex
315 :type 'boolean)
317 (defcustom org-export-latex-listings-langs
318 '(:emacs-lisp "Lisp" :lisp "Lisp"
319 :c "C" :cc "C++"
320 :fortran "fortran"
321 :perl "Perl" :cperl "Perl" :python "Python" :ruby "Ruby"
322 :html "HTML" :xml "XML"
323 :tex "TeX" :latex "TeX"
324 :shell-script "bash"
325 :gnuplot "Gnuplot"
326 :ocaml "Caml" :caml "Caml"
327 :sql "SQL")
328 "Property list mapping languages to their listing language counterpart.
329 The key is the major mode symbol, the value is the string that should be
330 inserted as the language parameter for the listings package."
331 :group 'org-export-latex
332 :type 'plist)
334 (defcustom org-export-latex-remove-from-headlines
335 '(:todo nil :priority nil :tags nil)
336 "A plist of keywords to remove from headlines. OBSOLETE.
337 Non-nil means remove this keyword type from the headline.
339 Don't remove the keys, just change their values.
341 Obsolete, this variable is no longer used. Use the separate
342 variables `org-export-with-todo-keywords', `org-export-with-priority',
343 and `org-export-with-tags' instead."
344 :type 'plist
345 :group 'org-export-latex)
347 (defcustom org-export-latex-image-default-option "width=10em"
348 "Default option for images."
349 :group 'org-export-latex
350 :type 'string)
352 (defcustom org-export-latex-inline-image-extensions
353 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
354 "Extensions of image files that can be inlined into LaTeX.
355 Note that the image extension *actually* allowed depend on the way the
356 LaTeX file is processed. When used with pdflatex, pdf, jpg and png images
357 are OK. When processing through dvi to Postscript, only ps and eps are
358 allowed. The default we use here encompasses both."
359 :group 'org-export-latex
360 :type '(repeat (string :tag "Extension")))
362 (defcustom org-export-latex-coding-system nil
363 "Coding system for the exported LaTex file."
364 :group 'org-export-latex
365 :type 'coding-system)
367 (defgroup org-export-pdf nil
368 "Options for exporting Org-mode files to PDF, via LaTeX."
369 :tag "Org Export LaTeX"
370 :group 'org-export-latex
371 :group 'org-export)
373 (defcustom org-latex-to-pdf-process
374 '("pdflatex -interaction nonstopmode %s"
375 "pdflatex -interaction nonstopmode %s")
376 "Commands to process a LaTeX file to a PDF file.
377 This is a list of strings, each of them will be given to the shell
378 as a command. %s in the command will be replaced by the full file name, %b
379 by the file base name (i.e. without extension).
380 The reason why this is a list is that it usually takes several runs of
381 pdflatex, maybe mixed with a call to bibtex. Org does not have a clever
382 mechanism to detect whihc of these commands have to be run to get to a stable
383 result, and it also does not do any error checking.
385 Alternatively, this may be a Lisp function that does the processing, so you
386 could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
387 THis function should accept the file name as its single argument."
388 :group 'org-export-latex
389 :type '(choice (repeat :tag "Shell command sequence"
390 (string :tag "Shell command"))
391 (function)))
393 (defcustom org-export-pdf-remove-logfiles t
394 "Non-nil means, remove the logfiles produced by PDF production.
395 These are the .aux, .log, .out, and .toc files."
396 :group 'org-export-pdf
397 :type 'boolean)
399 ;;; Hooks
401 (defvar org-export-latex-after-blockquotes-hook nil
402 "Hook run during LaTeX export, after blockquote, verse, center are done.")
404 ;;; Autoload functions:
406 ;;;###autoload
407 (defun org-export-as-latex-batch ()
408 "Call `org-export-as-latex', may be used in batch processing.
409 For example:
411 emacs --batch
412 --load=$HOME/lib/emacs/org.el
413 --eval \"(setq org-export-headline-levels 2)\"
414 --visit=MyFile --funcall org-export-as-latex-batch"
415 (org-export-as-latex org-export-headline-levels 'hidden))
417 ;;;###autoload
418 (defun org-export-as-latex-to-buffer (arg)
419 "Call `org-export-as-latex` with output to a temporary buffer.
420 No file is created. The prefix ARG is passed through to `org-export-as-latex'."
421 (interactive "P")
422 (org-export-as-latex arg nil nil "*Org LaTeX Export*")
423 (when org-export-show-temporary-export-buffer
424 (switch-to-buffer-other-window "*Org LaTeX Export*")))
426 ;;;###autoload
427 (defun org-replace-region-by-latex (beg end)
428 "Replace the region from BEG to END with its LaTeX export.
429 It assumes the region has `org-mode' syntax, and then convert it to
430 LaTeX. This can be used in any buffer. For example, you could
431 write an itemized list in `org-mode' syntax in an LaTeX buffer and
432 then use this command to convert it."
433 (interactive "r")
434 (let (reg latex buf)
435 (save-window-excursion
436 (if (org-mode-p)
437 (setq latex (org-export-region-as-latex
438 beg end t 'string))
439 (setq reg (buffer-substring beg end)
440 buf (get-buffer-create "*Org tmp*"))
441 (save-excursion
442 (set-buffer buf)
443 (erase-buffer)
444 (insert reg)
445 (org-mode)
446 (setq latex (org-export-region-as-latex
447 (point-min) (point-max) t 'string)))
448 (kill-buffer buf)))
449 (delete-region beg end)
450 (insert latex)))
452 ;;;###autoload
453 (defun org-export-region-as-latex (beg end &optional body-only buffer)
454 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
455 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
456 contents, and only produce the region of converted text, useful for
457 cut-and-paste operations.
458 If BUFFER is a buffer or a string, use/create that buffer as a target
459 of the converted LaTeX. If BUFFER is the symbol `string', return the
460 produced LaTeX as a string and leave no buffer behind. For example,
461 a Lisp program could call this function in the following way:
463 (setq latex (org-export-region-as-latex beg end t 'string))
465 When called interactively, the output buffer is selected, and shown
466 in a window. A non-interactive call will only return the buffer."
467 (interactive "r\nP")
468 (when (interactive-p)
469 (setq buffer "*Org LaTeX Export*"))
470 (let ((transient-mark-mode t) (zmacs-regions t)
471 ext-plist rtn)
472 (setq ext-plist (plist-put ext-plist :ignore-subree-p t))
473 (goto-char end)
474 (set-mark (point)) ;; to activate the region
475 (goto-char beg)
476 (setq rtn (org-export-as-latex
477 nil nil ext-plist
478 buffer body-only))
479 (if (fboundp 'deactivate-mark) (deactivate-mark))
480 (if (and (interactive-p) (bufferp rtn))
481 (switch-to-buffer-other-window rtn)
482 rtn)))
484 ;;;###autoload
485 (defun org-export-as-latex (arg &optional hidden ext-plist
486 to-buffer body-only pub-dir)
487 "Export current buffer to a LaTeX file.
488 If there is an active region, export only the region. The prefix
489 ARG specifies how many levels of the outline should become
490 headlines. The default is 3. Lower levels will be exported
491 depending on `org-export-latex-low-levels'. The default is to
492 convert them as description lists.
493 HIDDEN is obsolete and does nothing.
494 EXT-PLIST is a property list with
495 external parameters overriding org-mode's default settings, but
496 still inferior to file-local settings. When TO-BUFFER is
497 non-nil, create a buffer with that name and export to that
498 buffer. If TO-BUFFER is the symbol `string', don't leave any
499 buffer behind but just return the resulting LaTeX as a string.
500 When BODY-ONLY is set, don't produce the file header and footer,
501 simply return the content of \begin{document}...\end{document},
502 without even the \begin{document} and \end{document} commands.
503 when PUB-DIR is set, use this as the publishing directory."
504 (interactive "P")
505 ;; Make sure we have a file name when we need it.
506 (when (and (not (or to-buffer body-only))
507 (not buffer-file-name))
508 (if (buffer-base-buffer)
509 (org-set-local 'buffer-file-name
510 (with-current-buffer (buffer-base-buffer)
511 buffer-file-name))
512 (error "Need a file name to be able to export")))
514 (message "Exporting to LaTeX...")
515 (org-unmodified
516 (remove-text-properties (point-min) (point-max)
517 '(:org-license-to-kill nil)))
518 (org-update-radio-target-regexp)
519 (org-export-latex-set-initial-vars ext-plist arg)
520 (let* ((wcf (current-window-configuration))
521 (opt-plist org-export-latex-options-plist)
522 (region-p (org-region-active-p))
523 (rbeg (and region-p (region-beginning)))
524 (rend (and region-p (region-end)))
525 (subtree-p
526 (if (plist-get opt-plist :ignore-subree-p)
528 (when region-p
529 (save-excursion
530 (goto-char rbeg)
531 (and (org-at-heading-p)
532 (>= (org-end-of-subtree t t) rend))))))
533 (opt-plist (setq org-export-opt-plist
534 (if subtree-p
535 (org-export-add-subtree-options opt-plist rbeg)
536 opt-plist)))
537 ;; Make sure the variable contains the updated values.
538 (org-export-latex-options-plist opt-plist)
539 (title (or (and subtree-p (org-export-get-title-from-subtree))
540 (plist-get opt-plist :title)
541 (and (not
542 (plist-get opt-plist :skip-before-1st-heading))
543 (org-export-grab-title-from-buffer))
544 (file-name-sans-extension
545 (file-name-nondirectory buffer-file-name))))
546 (filename (concat (file-name-as-directory
547 (or pub-dir
548 (org-export-directory :LaTeX ext-plist)))
549 (file-name-sans-extension
550 (or (and subtree-p
551 (org-entry-get rbeg "EXPORT_FILE_NAME" t))
552 (file-name-nondirectory ;sans-extension
553 buffer-file-name)))
554 ".tex"))
555 (filename (if (equal (file-truename filename)
556 (file-truename buffer-file-name))
557 (concat filename ".tex")
558 filename))
559 (buffer (if to-buffer
560 (cond
561 ((eq to-buffer 'string) (get-buffer-create
562 "*Org LaTeX Export*"))
563 (t (get-buffer-create to-buffer)))
564 (find-file-noselect filename)))
565 (odd org-odd-levels-only)
566 (header (org-export-latex-make-header title opt-plist))
567 (skip (cond (subtree-p nil)
568 (region-p nil)
569 (t (plist-get opt-plist :skip-before-1st-heading))))
570 (text (plist-get opt-plist :text))
571 (org-export-preprocess-hook
572 (cons
573 `(lambda () (org-set-local 'org-complex-heading-regexp
574 ,org-export-latex-complex-heading-re))
575 org-export-preprocess-hook))
576 (first-lines (if skip "" (org-export-latex-first-lines
577 opt-plist
578 (if subtree-p
579 (save-excursion
580 (goto-char rbeg)
581 (point-at-bol 2))
582 rbeg)
583 (if region-p rend))))
584 (coding-system (and (boundp 'buffer-file-coding-system)
585 buffer-file-coding-system))
586 (coding-system-for-write (or org-export-latex-coding-system
587 coding-system))
588 (save-buffer-coding-system (or org-export-latex-coding-system
589 coding-system))
590 (region (buffer-substring
591 (if region-p (region-beginning) (point-min))
592 (if region-p (region-end) (point-max))))
593 (string-for-export
594 (org-export-preprocess-string
595 region
596 :emph-multiline t
597 :for-LaTeX t
598 :comments nil
599 :tags (plist-get opt-plist :tags)
600 :priority (plist-get opt-plist :priority)
601 :footnotes (plist-get opt-plist :footnotes)
602 :timestamps (plist-get opt-plist :timestamps)
603 :todo-keywords (plist-get opt-plist :todo-keywords)
604 :add-text (if (eq to-buffer 'string) nil text)
605 :skip-before-1st-heading skip
606 :select-tags (plist-get opt-plist :select-tags)
607 :exclude-tags (plist-get opt-plist :exclude-tags)
608 :LaTeX-fragments nil)))
610 (set-buffer buffer)
611 (erase-buffer)
612 (org-install-letbind)
614 (and (fboundp 'set-buffer-file-coding-system)
615 (set-buffer-file-coding-system coding-system-for-write))
617 ;; insert the header and initial document commands
618 (unless (or (eq to-buffer 'string) body-only)
619 (insert header))
621 ;; insert text found in #+TEXT
622 (when (and text (not (eq to-buffer 'string)))
623 (insert (org-export-latex-content
624 text '(lists tables fixed-width keywords))
625 "\n\n"))
627 ;; insert lines before the first headline
628 (unless skip
629 (insert first-lines))
631 ;; export the content of headlines
632 (org-export-latex-global
633 (with-temp-buffer
634 (insert string-for-export)
635 (goto-char (point-min))
636 (when (re-search-forward "^\\(\\*+\\) " nil t)
637 (let* ((asters (length (match-string 1)))
638 (level (if odd (- asters 2) (- asters 1))))
639 (setq org-export-latex-add-level
640 (if odd (1- (/ (1+ asters) 2)) (1- asters)))
641 (org-export-latex-parse-global level odd)))))
643 ;; finalization
644 (unless body-only (insert "\n\\end{document}"))
646 ;; Relocate the table of contents
647 (goto-char (point-min))
648 (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
649 (goto-char (point-min))
650 (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t)
651 (replace-match ""))
652 (goto-char (point-min))
653 (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
654 (replace-match "\\tableofcontents" t t)))
656 (or to-buffer (save-buffer))
657 (goto-char (point-min))
658 (or (org-export-push-to-kill-ring "LaTeX")
659 (message "Exporting to LaTeX...done"))
660 (prog1
661 (if (eq to-buffer 'string)
662 (prog1 (buffer-substring (point-min) (point-max))
663 (kill-buffer (current-buffer)))
664 (current-buffer))
665 (set-window-configuration wcf))))
667 ;;;###autoload
668 (defun org-export-as-pdf (arg &optional hidden ext-plist
669 to-buffer body-only pub-dir)
670 "Export as LaTeX, then process through to PDF."
671 (interactive "P")
672 (message "Exporting to PDF...")
673 (let* ((wconfig (current-window-configuration))
674 (lbuf (org-export-as-latex arg hidden ext-plist
675 to-buffer body-only pub-dir))
676 (file (buffer-file-name lbuf))
677 (base (file-name-sans-extension (buffer-file-name lbuf)))
678 (pdffile (concat base ".pdf"))
679 (cmds org-latex-to-pdf-process)
680 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
681 (bibtex-p (with-current-buffer lbuf
682 (save-excursion
683 (goto-char (point-min))
684 (re-search-forward "\\\\bibliography{" nil t))))
685 cmd)
686 (with-current-buffer outbuf (erase-buffer))
687 (and (file-exists-p pdffile) (delete-file pdffile))
688 (message "Processing LaTeX file...")
689 (if (and cmds (symbolp cmds))
690 (funcall cmds file)
691 (while cmds
692 (setq cmd (pop cmds))
693 (while (string-match "%b" cmd)
694 (setq cmd (replace-match
695 (save-match-data
696 (shell-quote-argument base))
697 t t cmd)))
698 (while (string-match "%s" cmd)
699 (setq cmd (replace-match
700 (save-match-data
701 (shell-quote-argument file))
702 t t cmd)))
703 (shell-command cmd outbuf outbuf)))
704 (message "Processing LaTeX file...done")
705 (if (not (file-exists-p pdffile))
706 (error "PDF file was not produced")
707 (set-window-configuration wconfig)
708 (when org-export-pdf-remove-logfiles
709 (dolist (ext '("aux" "log" "out" "toc"))
710 (setq file (concat base "." ext))
711 (and (file-exists-p file) (delete-file file))))
712 (message "Exporting to PDF...done")
713 pdffile)))
715 ;;;###autoload
716 (defun org-export-as-pdf-and-open (arg)
717 "Export as LaTeX, then process through to PDF, and open."
718 (interactive "P")
719 (let ((pdffile (org-export-as-pdf arg)))
720 (if pdffile
721 (org-open-file pdffile)
722 (error "PDF file was not produced"))))
724 ;;; Parsing functions:
726 (defun org-export-latex-parse-global (level odd)
727 "Parse the current buffer recursively, starting at LEVEL.
728 If ODD is non-nil, assume the buffer only contains odd sections.
729 Return a list reflecting the document structure."
730 (save-excursion
731 (goto-char (point-min))
732 (let* ((cnt 0) output
733 (depth org-export-latex-sectioning-depth))
734 (while (re-search-forward
735 (concat "^\\(\\(?:\\*\\)\\{"
736 (number-to-string (+ (if odd 2 1) level))
737 "\\}\\) \\(.*\\)$")
738 ;; make sure that there is no upper heading
739 (when (> level 0)
740 (save-excursion
741 (save-match-data
742 (re-search-forward
743 (concat "^\\(\\(?:\\*\\)\\{"
744 (number-to-string level)
745 "\\}\\) \\(.*\\)$") nil t)))) t)
746 (setq cnt (1+ cnt))
747 (let* ((pos (match-beginning 0))
748 (heading (match-string 2))
749 (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
750 (save-excursion
751 (narrow-to-region
752 (point)
753 (save-match-data
754 (if (re-search-forward
755 (concat "^\\(\\(?:\\*\\)\\{"
756 (number-to-string (+ (if odd 2 1) level))
757 "\\}\\) \\(.*\\)$") nil t)
758 (match-beginning 0)
759 (point-max))))
760 (goto-char (point-min))
761 (setq output
762 (append output
763 (list
764 (list
765 `(pos . ,pos)
766 `(level . ,nlevel)
767 `(occur . ,cnt)
768 `(heading . ,heading)
769 `(content . ,(org-export-latex-parse-content))
770 `(subcontent . ,(org-export-latex-parse-subcontent
771 level odd)))))))
772 (widen)))
773 (list output))))
775 (defun org-export-latex-parse-content ()
776 "Extract the content of a section."
777 (let ((beg (point))
778 (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t)
779 (progn (beginning-of-line) (point))
780 (point-max))))
781 (buffer-substring beg end)))
783 (defun org-export-latex-parse-subcontent (level odd)
784 "Extract the subcontent of a section at LEVEL.
785 If ODD Is non-nil, assume subcontent only contains odd sections."
786 (if (not (re-search-forward
787 (concat "^\\(\\(?:\\*\\)\\{"
788 (number-to-string (+ (if odd 4 2) level))
789 "\\}\\) \\(.*\\)$")
790 nil t))
791 nil ; subcontent is nil
792 (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
794 ;;; Rendering functions:
795 (defun org-export-latex-global (content)
796 "Export CONTENT to LaTeX.
797 CONTENT is an element of the list produced by
798 `org-export-latex-parse-global'."
799 (if (eq (car content) 'subcontent)
800 (mapc 'org-export-latex-sub (cdr content))
801 (org-export-latex-sub (car content))))
803 (defun org-export-latex-sub (subcontent)
804 "Export the list SUBCONTENT to LaTeX.
805 SUBCONTENT is an alist containing information about the headline
806 and its content."
807 (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
808 (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
810 (defun org-export-latex-subcontent (subcontent num)
811 "Export each cell of SUBCONTENT to LaTeX.
812 If NUM, export sections as numerical sections."
813 (let* ((heading (org-export-latex-fontify-headline
814 (cdr (assoc 'heading subcontent))))
815 (level (- (cdr (assoc 'level subcontent))
816 org-export-latex-add-level))
817 (occur (number-to-string (cdr (assoc 'occur subcontent))))
818 (content (cdr (assoc 'content subcontent)))
819 (subcontent (cadr (assoc 'subcontent subcontent)))
820 (label (org-get-text-property-any 0 'target heading))
821 (label-list (cons label (cdr (assoc label
822 org-export-target-aliases)))))
823 (cond
824 ;; Normal conversion
825 ((<= level org-export-latex-sectioning-depth)
826 (let* ((sec (nth (1- level) org-export-latex-sectioning))
827 start end)
828 (if (consp (cdr sec))
829 (setq start (nth (if num 0 2) sec)
830 end (nth (if num 1 3) sec))
831 (setq start (if num (car sec) (cdr sec))))
832 (insert (format start heading) "\n")
833 (when label
834 (insert (mapconcat (lambda (l) (format "\\label{%s}" l))
835 label-list "\n") "\n"))
836 (insert (org-export-latex-content content))
837 (cond ((stringp subcontent) (insert subcontent))
838 ((listp subcontent) (org-export-latex-sub subcontent)))
839 (if end (insert end "\n"))))
840 ;; At a level under the hl option: we can drop this subsection
841 ((> level org-export-latex-sectioning-depth)
842 (cond ((eq org-export-latex-low-levels 'description)
843 (if (string-match "% ends low level$"
844 (buffer-substring (point-at-bol 0) (point)))
845 (delete-region (point-at-bol 0) (point))
846 (insert "\\begin{description}\n"))
847 (insert (format "\n\\item[%s]%s~\n\n"
848 heading
849 (if label (format "\\label{%s}" label) "")))
850 (insert (org-export-latex-content content))
851 (cond ((stringp subcontent) (insert subcontent))
852 ((listp subcontent) (org-export-latex-sub subcontent)))
853 (insert "\\end{description} % ends low level\n"))
854 ((memq org-export-latex-low-levels '(itemize enumerate))
855 (if (string-match "% ends low level$"
856 (buffer-substring (point-at-bol 0) (point)))
857 (delete-region (point-at-bol 0) (point))
858 (insert (format "\\begin{%s}\n"
859 (symbol-name org-export-latex-low-levels))))
860 (insert (format "\n\\item %s\\\\\n%s\n"
861 heading
862 (if label (format "\\label{%s}" label) "")))
863 (insert (org-export-latex-content content))
864 (cond ((stringp subcontent) (insert subcontent))
865 ((listp subcontent) (org-export-latex-sub subcontent)))
866 (insert (format "\\end{%s} %% ends low level\n"
867 (symbol-name org-export-latex-low-levels))))
869 ((listp org-export-latex-low-levels)
870 (if (string-match "% ends low level$"
871 (buffer-substring (point-at-bol 0) (point)))
872 (delete-region (point-at-bol 0) (point))
873 (insert (car org-export-latex-low-levels) "\n"))
874 (insert (format (nth 2 org-export-latex-low-levels)
875 heading
876 (if label (format "\\label{%s}" label) "")))
877 (insert (org-export-latex-content content))
878 (cond ((stringp subcontent) (insert subcontent))
879 ((listp subcontent) (org-export-latex-sub subcontent)))
880 (insert (nth 1 org-export-latex-low-levels)
881 " %% ends low level\n"))
883 ((stringp org-export-latex-low-levels)
884 (insert (format org-export-latex-low-levels heading) "\n")
885 (when label (insert (format "\\label{%s}\n" label)))
886 (insert (org-export-latex-content content))
887 (cond ((stringp subcontent) (insert subcontent))
888 ((listp subcontent) (org-export-latex-sub subcontent)))))))))
890 ;;; Exporting internals:
891 (defun org-export-latex-set-initial-vars (ext-plist level)
892 "Store org local variables required for LaTeX export.
893 EXT-PLIST is an optional additional plist.
894 LEVEL indicates the default depth for export."
895 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
896 org-export-latex-done-keywords org-done-keywords
897 org-export-latex-not-done-keywords org-not-done-keywords
898 org-export-latex-complex-heading-re org-complex-heading-regexp
899 org-export-latex-display-custom-times org-display-custom-times
900 org-export-latex-all-targets-re
901 (org-make-target-link-regexp (org-all-targets))
902 org-export-latex-options-plist
903 (org-combine-plists (org-default-export-plist) ext-plist
904 (org-infile-export-plist))
905 org-export-latex-class
906 (or (and (org-region-active-p)
907 (save-excursion
908 (goto-char (region-beginning))
909 (and (looking-at org-complex-heading-regexp)
910 (org-entry-get nil "LaTeX_CLASS" 'selective))))
911 (save-excursion
912 (save-restriction
913 (widen)
914 (goto-char (point-min))
915 (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
916 (match-string 1))))
917 org-export-latex-default-class)
918 org-export-latex-class
919 (or (car (assoc org-export-latex-class org-export-latex-classes))
920 (error "No definition for class `%s' in `org-export-latex-classes'"
921 org-export-latex-class))
922 org-export-latex-header
923 (cadr (assoc org-export-latex-class org-export-latex-classes))
924 org-export-latex-sectioning
925 (cddr (assoc org-export-latex-class org-export-latex-classes))
926 org-export-latex-sectioning-depth
927 (or level
928 (let ((hl-levels
929 (plist-get org-export-latex-options-plist :headline-levels))
930 (sec-depth (length org-export-latex-sectioning)))
931 (if (> hl-levels sec-depth) sec-depth hl-levels)))))
933 (defun org-export-latex-make-header (title opt-plist)
934 "Make the LaTeX header and return it as a string.
935 TITLE is the current title from the buffer or region.
936 OPT-PLIST is the options plist for current buffer."
937 (let ((toc (plist-get opt-plist :table-of-contents))
938 (author (plist-get opt-plist :author)))
939 (concat
940 (if (plist-get opt-plist :time-stamp-file)
941 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
942 ;; insert LaTeX custom header
943 (org-export-apply-macros-in-string org-export-latex-header)
944 "\n"
945 ;; insert information on LaTeX packages
946 (when org-export-latex-packages-alist
947 (mapconcat (lambda(p)
948 (if (equal "" (car p))
949 (format "\\usepackage{%s}" (cadr p))
950 (format "\\usepackage[%s]{%s}"
951 (car p) (cadr p))))
952 org-export-latex-packages-alist "\n"))
953 ;; insert additional commands in the header
954 (org-export-apply-macros-in-string
955 (plist-get opt-plist :latex-header-extra))
956 (org-export-apply-macros-in-string org-export-latex-append-header)
957 ;; insert the title
958 (format
959 "\n\n\\title{%s}\n"
960 ;; convert the title
961 (org-export-latex-content
962 title '(lists tables fixed-width keywords)))
963 ;; insert author info
964 (if (plist-get opt-plist :author-info)
965 (format "\\author{%s}\n"
966 (org-export-latex-fontify-headline (or author user-full-name)));????????????????????
967 (format "%%\\author{%s}\n"
968 (or author user-full-name)))
969 ;; insert the date
970 (format "\\date{%s}\n"
971 (format-time-string
972 (or (plist-get opt-plist :date)
973 org-export-latex-date-format)))
974 ;; beginning of the document
975 "\n\\begin{document}\n\n"
976 ;; insert the title command
977 (when (string-match "\\S-" title)
978 (if (string-match "%s" org-export-latex-title-command)
979 (format org-export-latex-title-command title)
980 org-export-latex-title-command))
981 "\n\n"
982 ;; table of contents
983 (when (and org-export-with-toc
984 (plist-get opt-plist :section-numbers))
985 (cond ((numberp toc)
986 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
987 (min toc (plist-get opt-plist :headline-levels))))
988 (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
989 (plist-get opt-plist :headline-levels))))))))
991 (defun org-export-latex-first-lines (opt-plist &optional beg end)
992 "Export the first lines before first headline.
993 If BEG is non-nil, it is the beginning of the region.
994 If END is non-nil, it is the end of the region."
995 (save-excursion
996 (goto-char (or beg (point-min)))
997 (let* ((pt (point))
998 (end (if (re-search-forward "^\\*+ " end t)
999 (goto-char (match-beginning 0))
1000 (goto-char end))))
1001 (prog1
1002 (org-export-latex-content
1003 (org-export-preprocess-string
1004 (buffer-substring pt end)
1005 :for-LaTeX t
1006 :emph-multiline t
1007 :add-text nil
1008 :comments nil
1009 :skip-before-1st-heading nil
1010 :LaTeX-fragments nil
1011 :timestamps (plist-get opt-plist :timestamps)
1012 :footnotes (plist-get opt-plist :footnotes)))
1013 (org-unmodified
1014 (add-text-properties pt (max pt (1- end))
1015 '(:org-license-to-kill t)))))))
1017 (defvar org-export-latex-header-defs nil
1018 "The header definitions that might be used in the LaTeX body.")
1019 (defvar org-export-latex-header-defs-re nil
1020 "The header definitions that might be used in the LaTeX body.")
1022 (defun org-export-latex-content (content &optional exclude-list)
1023 "Convert CONTENT string to LaTeX.
1024 Don't perform conversions that are in EXCLUDE-LIST. Recognized
1025 conversion types are: quotation-marks, emphasis, sub-superscript,
1026 links, keywords, lists, tables, fixed-width"
1027 (with-temp-buffer
1028 (insert content)
1029 (unless (memq 'timestamps exclude-list)
1030 (org-export-latex-time-stamps))
1031 (unless (memq 'quotation-marks exclude-list)
1032 (org-export-latex-quotation-marks))
1033 (unless (memq 'emphasis exclude-list)
1034 (when (plist-get org-export-latex-options-plist :emphasize)
1035 (org-export-latex-fontify)))
1036 (unless (memq 'sub-superscript exclude-list)
1037 (org-export-latex-special-chars
1038 (plist-get org-export-latex-options-plist :sub-superscript)))
1039 (unless (memq 'links exclude-list)
1040 (org-export-latex-links))
1041 (unless (memq 'keywords exclude-list)
1042 (org-export-latex-keywords))
1043 (unless (memq 'lists exclude-list)
1044 (org-export-latex-lists))
1045 (unless (memq 'tables exclude-list)
1046 (org-export-latex-tables
1047 (plist-get org-export-latex-options-plist :tables)))
1048 (unless (memq 'fixed-width exclude-list)
1049 (org-export-latex-fixed-width
1050 (plist-get org-export-latex-options-plist :fixed-width)))
1051 ;; return string
1052 (buffer-substring (point-min) (point-max))))
1054 (defun org-export-latex-protect-string (s)
1055 "Add the org-protected property to string S."
1056 (add-text-properties 0 (length s) '(org-protected t) s) s)
1058 (defun org-export-latex-protect-char-in-string (char-list string)
1059 "Add org-protected text-property to char from CHAR-LIST in STRING."
1060 (with-temp-buffer
1061 (save-match-data
1062 (insert string)
1063 (goto-char (point-min))
1064 (while (re-search-forward (regexp-opt char-list) nil t)
1065 (add-text-properties (match-beginning 0)
1066 (match-end 0) '(org-protected t)))
1067 (buffer-string))))
1069 (defun org-export-latex-keywords-maybe (&optional remove-list)
1070 "Maybe remove keywords depending on rules in REMOVE-LIST."
1071 (goto-char (point-min))
1072 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
1073 (case-fold-search nil)
1074 (todo-markup org-export-latex-todo-keyword-markup)
1075 fmt)
1076 ;; convert TODO keywords
1077 (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
1078 (if (plist-get remove-list :todo)
1079 (replace-match "")
1080 (setq fmt (cond
1081 ((stringp todo-markup) todo-markup)
1082 ((and (consp todo-markup) (stringp (car todo-markup)))
1083 (if (member (match-string 1) org-export-latex-done-keywords)
1084 (cdr todo-markup) (car todo-markup)))
1085 (t (cdr (or (assoc (match-string 1) todo-markup)
1086 (car todo-markup))))))
1087 (replace-match (format fmt (match-string 1)) t t)))
1088 ;; convert priority string
1089 (when (re-search-forward "\\[\\\\#.\\]" nil t)
1090 (if (plist-get remove-list :priority)
1091 (replace-match "")
1092 (replace-match (format "\\textbf{%s}" (match-string 0)) t t)))
1093 ;; convert tags
1094 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t)
1095 (if (or (not org-export-with-tags)
1096 (plist-get remove-list :tags))
1097 (replace-match "")
1098 (replace-match
1099 (org-export-latex-protect-string
1100 (format "\\textbf{%s}"
1101 (save-match-data
1102 (replace-regexp-in-string
1103 "_" "\\\\_" (match-string 0)))))
1104 t t)))))
1106 (defun org-export-latex-fontify-headline (string)
1107 "Fontify special words in STRING."
1108 (with-temp-buffer
1109 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
1110 ;; the beginning of the buffer - inserting "\n" is safe here though.
1111 (insert "\n" string)
1112 (goto-char (point-min))
1113 (when (plist-get org-export-latex-options-plist :emphasize)
1114 (org-export-latex-fontify))
1115 (org-export-latex-keywords-maybe)
1116 (org-export-latex-special-chars
1117 (plist-get org-export-latex-options-plist :sub-superscript))
1118 (org-export-latex-links)
1119 (org-trim (buffer-string))))
1121 (defun org-export-latex-time-stamps ()
1122 "Format time stamps."
1123 (goto-char (point-min))
1124 (let ((org-display-custom-times org-export-latex-display-custom-times))
1125 (while (re-search-forward org-ts-regexp-both nil t)
1126 (org-if-unprotected-at (1- (point))
1127 (replace-match
1128 (org-export-latex-protect-string
1129 (format org-export-latex-timestamp-markup
1130 (substring (org-translate-time (match-string 0)) 1 -1)))
1131 t t)))))
1133 (defun org-export-latex-quotation-marks ()
1134 "Export quotation marks depending on language conventions."
1135 (let* ((lang (plist-get org-export-latex-options-plist :language))
1136 (quote-rpl (if (equal lang "fr")
1137 '(("\\(\\s-\\)\"" "«~")
1138 ("\\(\\S-\\)\"" "~»")
1139 ("\\(\\s-\\)'" "`"))
1140 '(("\\(\\s-\\|(\\)\"" "``")
1141 ("\\(\\S-\\)\"" "''")
1142 ("\\(\\s-\\|(\\)'" "`")))))
1143 (mapc (lambda(l) (goto-char (point-min))
1144 (while (re-search-forward (car l) nil t)
1145 (let ((rpl (concat (match-string 1) (cadr l))))
1146 (org-export-latex-protect-string rpl)
1147 (org-if-unprotected-1
1148 (replace-match rpl t t))))) quote-rpl)))
1150 (defun org-export-latex-special-chars (sub-superscript)
1151 "Export special characters to LaTeX.
1152 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
1153 See the `org-export-latex.el' code for a complete conversion table."
1154 (goto-char (point-min))
1155 (mapc (lambda(c)
1156 (goto-char (point-min))
1157 (while (re-search-forward c nil t)
1158 ;; Put the point where to check for org-protected
1159 (unless (get-text-property (match-beginning 2) 'org-protected)
1160 (cond ((member (match-string 2) '("\\$" "$"))
1161 (if (equal (match-string 2) "\\$")
1163 (replace-match "\\$" t t)))
1164 ((member (match-string 2) '("&" "%" "#"))
1165 (if (equal (match-string 1) "\\")
1166 (replace-match (match-string 2) t t)
1167 (replace-match (concat (match-string 1) "\\"
1168 (match-string 2)) t t)))
1169 ((equal (match-string 2) "...")
1170 (replace-match
1171 (concat (match-string 1)
1172 (org-export-latex-protect-string "\\ldots{}")) t t))
1173 ((equal (match-string 2) "~")
1174 (cond ((equal (match-string 1) "\\") nil)
1175 ((eq 'org-link (get-text-property 0 'face (match-string 2)))
1176 (replace-match (concat (match-string 1) "\\~") t t))
1177 (t (replace-match
1178 (org-export-latex-protect-string
1179 (concat (match-string 1) "\\~{}")) t t))))
1180 ((member (match-string 2) '("{" "}"))
1181 (unless (save-match-data (org-inside-latex-math-p))
1182 (if (equal (match-string 1) "\\")
1183 (replace-match (match-string 2) t t)
1184 (replace-match (concat (match-string 1) "\\"
1185 (match-string 2)) t t)))))
1186 (unless (save-match-data (org-inside-latex-math-p))
1187 (cond ((equal (match-string 2) "\\")
1188 (replace-match (or (save-match-data
1189 (org-export-latex-treat-backslash-char
1190 (match-string 1)
1191 (or (match-string 3) "")))
1192 "") t t))
1193 ((member (match-string 2) '("_" "^"))
1194 (replace-match (or (save-match-data
1195 (org-export-latex-treat-sub-super-char
1196 sub-superscript
1197 (match-string 2)
1198 (match-string 1)
1199 (match-string 3))) "") t t)
1200 (backward-char 1)))))))
1201 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
1202 "\\(\\(\\\\?\\$\\)\\)"
1203 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
1204 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
1205 "\\(.\\|^\\)\\(&\\)"
1206 "\\(.\\|^\\)\\(#\\)"
1207 "\\(.\\|^\\)\\(%\\)"
1208 "\\(.\\|^\\)\\({\\)"
1209 "\\(.\\|^\\)\\(}\\)"
1210 "\\(.\\|^\\)\\(~\\)"
1211 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
1212 ;; (?\< . "\\textless{}")
1213 ;; (?\> . "\\textgreater{}")
1216 (defun org-inside-latex-math-p ()
1217 (get-text-property (point) 'org-latex-math))
1219 (defun org-export-latex-treat-sub-super-char
1220 (subsup char string-before string-after)
1221 "Convert the \"_\" and \"^\" characters to LaTeX.
1222 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
1223 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
1224 (cond ((equal string-before "\\")
1225 (concat string-before char string-after))
1226 ;; this is part of a math formula
1227 ((and (string-match "\\S-+" string-before)
1228 (string-match "\\S-+" string-after))
1229 (cond ((eq 'org-link (get-text-property 0 'face char))
1230 (concat string-before "\\" char string-after))
1231 ((save-match-data (org-inside-latex-math-p))
1232 (if subsup
1233 (cond ((eq 1 (length string-after))
1234 (concat string-before char string-after))
1235 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
1236 (format "%s%s{%s}" string-before char
1237 (match-string 1 string-after))))))
1238 ((and (> (length string-after) 1)
1239 (or (eq subsup t)
1240 (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
1241 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
1242 (org-export-latex-protect-string
1243 (format "%s$%s{%s}$" string-before char
1244 (if (and (> (match-end 1) (1+ (match-beginning 1)))
1245 (not (equal (substring string-after 0 2) "{\\")))
1246 (concat "\\mathrm{" (match-string 1 string-after) "}")
1247 (match-string 1 string-after)))))
1248 ((eq subsup t) (concat string-before "$" char string-after "$"))
1249 (t (org-export-latex-protect-string
1250 (concat string-before "\\" char "{}" string-after)))))
1251 (t (org-export-latex-protect-string
1252 (concat string-before "\\" char "{}" string-after)))))
1254 (defun org-export-latex-treat-backslash-char (string-before string-after)
1255 "Convert the \"$\" special character to LaTeX.
1256 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
1257 (cond ((member (list string-after) org-html-entities)
1258 ;; backslash is part of a special entity (like "\alpha")
1259 (concat string-before "$\\"
1260 (or (cdar (member (list string-after) org-html-entities))
1261 string-after) "$"))
1262 ((and (not (string-match "^[ \n\t]" string-after))
1263 (not (string-match "[ \t]\\'\\|^" string-before)))
1264 ;; backslash is inside a word
1265 (org-export-latex-protect-string
1266 (concat string-before "\\textbackslash{}" string-after)))
1267 ((not (or (equal string-after "")
1268 (string-match "^[ \t\n]" string-after)))
1269 ;; backslash might escape a character (like \#) or a user TeX
1270 ;; macro (like \setcounter)
1271 (org-export-latex-protect-string
1272 (concat string-before "\\" string-after)))
1273 ((and (string-match "^[ \t\n]" string-after)
1274 (string-match "[ \t\n]\\'" string-before))
1275 ;; backslash is alone, convert it to $\backslash$
1276 (org-export-latex-protect-string
1277 (concat string-before "\\textbackslash{}" string-after)))
1278 (t (org-export-latex-protect-string
1279 (concat string-before "\\textbackslash{}" string-after)))))
1281 (defun org-export-latex-keywords ()
1282 "Convert special keywords to LaTeX."
1283 (goto-char (point-min))
1284 (while (re-search-forward org-export-latex-special-keyword-regexp nil t)
1285 (replace-match (format org-export-latex-timestamp-keyword-markup
1286 (match-string 0)) t t)
1287 (save-excursion
1288 (beginning-of-line 1)
1289 (unless (looking-at ".*\\\\newline[ \t]*$")
1290 (end-of-line 1)
1291 (insert "\\newline")))))
1293 (defun org-export-latex-fixed-width (opt)
1294 "When OPT is non-nil convert fixed-width sections to LaTeX."
1295 (goto-char (point-min))
1296 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t)
1297 (if opt
1298 (progn (goto-char (match-beginning 0))
1299 (insert "\\begin{verbatim}\n")
1300 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1301 (replace-match (concat (match-string 1)
1302 (match-string 2)) t t)
1303 (forward-line))
1304 (insert "\\end{verbatim}\n\n"))
1305 (progn (goto-char (match-beginning 0))
1306 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1307 (replace-match (concat "%" (match-string 1)
1308 (match-string 2)) t t)
1309 (forward-line))))))
1312 (defvar org-table-last-alignment) ; defined in org-table.el
1313 (declare-function orgtbl-to-latex "org-table" (table params) t)
1314 (defun org-export-latex-tables (insert)
1315 "Convert tables to LaTeX and INSERT it."
1316 (goto-char (point-min))
1317 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
1318 (org-table-align)
1319 (let* ((beg (org-table-begin))
1320 (end (org-table-end))
1321 (raw-table (buffer-substring beg end))
1322 fnum fields line lines olines gr colgropen line-fmt align
1323 caption label attr floatp longtblp)
1324 (if org-export-latex-tables-verbatim
1325 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1326 "\\end{verbatim}\n")))
1327 (apply 'delete-region (list beg end))
1328 (insert (org-export-latex-protect-string tbl)))
1329 (progn
1330 (setq caption (org-find-text-property-in-string
1331 'org-caption raw-table)
1332 attr (org-find-text-property-in-string
1333 'org-attributes raw-table)
1334 label (org-find-text-property-in-string
1335 'org-label raw-table)
1336 longtblp (and attr (stringp attr)
1337 (string-match "\\<longtable\\>" attr))
1338 align (and attr (stringp attr)
1339 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
1340 (match-string 1 attr))
1341 floatp (or caption label))
1342 (setq lines (org-split-string raw-table "\n"))
1343 (apply 'delete-region (list beg end))
1344 (when org-export-table-remove-special-lines
1345 (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
1346 ;; make a formatting string to reflect aligment
1347 (setq olines lines)
1348 (while (and (not line-fmt) (setq line (pop olines)))
1349 (unless (string-match "^[ \t]*|-" line)
1350 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1351 (setq fnum (make-vector (length fields) 0))
1352 (setq line-fmt
1353 (mapconcat
1354 (lambda (x)
1355 (setq gr (pop org-table-colgroup-info))
1356 (format "%s%%s%s"
1357 (cond ((eq gr :start)
1358 (prog1 (if colgropen "|" "|")
1359 (setq colgropen t)))
1360 ((eq gr :startend)
1361 (prog1 (if colgropen "|" "|")
1362 (setq colgropen nil)))
1363 (t ""))
1364 (if (memq gr '(:end :startend))
1365 (progn (setq colgropen nil) "|")
1366 "")))
1367 fnum ""))))
1368 ;; fix double || in line-fmt
1369 (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
1370 ;; maybe remove the first and last "|"
1371 (when (and (not org-export-latex-tables-column-borders)
1372 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
1373 (setq line-fmt (match-string 2 line-fmt)))
1374 ;; format alignment
1375 (unless align
1376 (setq align (apply 'format
1377 (cons line-fmt
1378 (mapcar (lambda (x) (if x "r" "l"))
1379 org-table-last-alignment)))))
1380 ;; prepare the table to send to orgtbl-to-latex
1381 (setq lines
1382 (mapcar
1383 (lambda(elem)
1384 (or (and (string-match "[ \t]*|-+" elem) 'hline)
1385 (org-split-string (org-trim elem) "|")))
1386 lines))
1387 (when insert
1388 (insert (org-export-latex-protect-string
1389 (concat
1390 (if longtblp
1391 (concat "\\begin{longtable}{" align "}\n")
1392 (if floatp "\\begin{table}[htb]\n"))
1393 (if (or floatp longtblp)
1394 (format
1395 "\\caption{%s%s}"
1396 (if label (concat "\\\label{" label "}") "")
1397 (or caption "")))
1398 (if longtblp "\\\\\n" "\n")
1399 (if (and org-export-latex-tables-centered (not longtblp))
1400 "\\begin{center}\n")
1401 (if (not longtblp) (concat "\\begin{tabular}{" align "}\n"))
1402 (orgtbl-to-latex
1403 lines
1404 `(:tstart nil :tend nil
1405 :hlend ,(if longtblp
1406 (format "\\\\
1407 \\hline
1408 \\endhead
1409 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
1410 \\endfoot
1411 \\endlastfoot" (length org-table-last-alignment))
1412 nil)))
1413 (if (not longtblp) (concat "\n\\end{tabular}"))
1414 (if longtblp "\n" (if org-export-latex-tables-centered
1415 "\n\\end{center}\n" "\n"))
1416 (if longtblp
1417 "\\end{longtable}"
1418 (if floatp "\\end{table}"))))
1419 "\n\n")))))))
1421 (defun org-export-latex-fontify ()
1422 "Convert fontification to LaTeX."
1423 (goto-char (point-min))
1424 (while (re-search-forward org-emph-re nil t)
1425 ;; The match goes one char after the *string*
1426 (let ((emph (assoc (match-string 3)
1427 org-export-latex-emphasis-alist))
1428 (beg (match-beginning 0))
1429 (end (match-end 0))
1430 rpl)
1431 (unless emph
1432 (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
1433 (match-string 3)))
1434 (unless (or (get-text-property (1- (point)) 'org-protected)
1435 (save-excursion
1436 (goto-char (match-beginning 1))
1437 (save-match-data
1438 (and (org-at-table-p)
1439 (string-match
1440 "[|\n]" (buffer-substring beg end))))))
1441 (setq rpl (concat (match-string 1)
1442 (org-export-latex-emph-format (cadr emph)
1443 (match-string 4))
1444 (match-string 5)))
1445 (if (caddr emph)
1446 (setq rpl (org-export-latex-protect-string rpl)))
1447 (replace-match rpl t t)))
1448 (backward-char)))
1450 (defvar org-export-latex-use-verb nil)
1451 (defun org-export-latex-emph-format (format string)
1452 "Format an emphasis string and handle the \\verb special case."
1453 (when (equal format "\\verb")
1454 (save-match-data
1455 (if org-export-latex-use-verb
1456 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1457 (catch 'exit
1458 (loop for i from 0 to (1- (length ll)) do
1459 (if (not (string-match (regexp-quote (substring ll i (1+ i)))
1460 string))
1461 (progn
1462 (setq format (concat "\\verb" (substring ll i (1+ i))
1463 "%s" (substring ll i (1+ i))))
1464 (throw 'exit nil))))))
1465 (let ((start 0)
1466 (trans '(("\\" . "\\backslash")
1467 ("~" . "\\ensuremath{\\sim}")
1468 ("^" . "\\ensuremath{\\wedge}")))
1469 (rtn "") char)
1470 (while (string-match "[\\{}$%&_#~^]" string)
1471 (setq char (match-string 0 string))
1472 (if (> (match-beginning 0) 0)
1473 (setq rtn (concat rtn (substring string
1474 0 (match-beginning 0)))))
1475 (setq string (substring string (1+ (match-beginning 0))))
1476 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1477 rtn (concat rtn char)))
1478 (setq string (concat rtn string) format "\\texttt{%s}")))))
1479 (setq string (org-export-latex-protect-string
1480 (format format string))))
1482 (defun org-export-latex-links ()
1483 ;; Make sure to use the LaTeX hyperref and graphicx package
1484 ;; or send some warnings.
1485 "Convert links to LaTeX."
1486 (goto-char (point-min))
1487 (while (re-search-forward org-bracket-link-analytic-regexp++ nil t)
1488 (org-if-unprotected
1489 (goto-char (match-beginning 0))
1490 (let* ((re-radio org-export-latex-all-targets-re)
1491 (remove (list (match-beginning 0) (match-end 0)))
1492 (raw-path (org-extract-attributes (match-string 3)))
1493 (full-raw-path (concat (match-string 1) raw-path))
1494 (desc (match-string 5))
1495 (type (or (match-string 2)
1496 (if (or (file-name-absolute-p raw-path)
1497 (string-match "^\\.\\.?/" raw-path))
1498 "file")))
1499 (coderefp (equal type "coderef"))
1500 (caption (org-find-text-property-in-string 'org-caption raw-path))
1501 (attr (or (org-find-text-property-in-string 'org-attributes raw-path)
1502 (plist-get org-export-latex-options-plist :latex-image-options)))
1503 (label (org-find-text-property-in-string 'org-label raw-path))
1504 (floatp (or label caption))
1505 imgp radiop
1506 ;; define the path of the link
1507 (path (cond
1508 ((member type '("coderef"))
1509 raw-path)
1510 ((member type '("http" "https" "ftp"))
1511 (concat type ":" raw-path))
1512 ((and re-radio (string-match re-radio raw-path))
1513 (setq radiop t))
1514 ((equal type "mailto")
1515 (concat type ":" raw-path))
1516 ((equal type "file")
1517 (if (and (org-file-image-p
1518 (expand-file-name
1519 raw-path)
1520 org-export-latex-inline-image-extensions)
1521 (or (get-text-property 0 'org-no-description
1522 raw-path)
1523 (equal desc full-raw-path)))
1524 (setq imgp t)
1525 (progn (when (string-match "\\(.+\\)::.+" raw-path)
1526 (setq raw-path (match-string 1 raw-path)))
1527 (if (file-exists-p raw-path)
1528 (concat type "://" (expand-file-name raw-path))
1529 (concat type "://" (org-export-directory
1530 :LaTeX org-export-latex-options-plist)
1531 raw-path))))))))
1532 ;; process with link inserting
1533 (apply 'delete-region remove)
1534 (cond ((and imgp (plist-get org-export-latex-options-plist :inline-images))
1535 (insert
1536 (concat
1537 (if floatp "\\begin{figure}[htb]\n")
1538 (format "\\centerline{\\includegraphics[%s]{%s}}\n"
1539 attr
1540 (if (file-name-absolute-p raw-path)
1541 (expand-file-name raw-path)
1542 raw-path))
1543 (if floatp
1544 (format "\\caption{%s%s}\n"
1545 (if label (concat "\\label{" label "}") "")
1546 (or caption "")))
1547 (if floatp "\\end{figure}\n"))))
1548 (coderefp
1549 (insert (format
1550 (org-export-get-coderef-format path desc)
1551 (cdr (assoc path org-export-code-refs)))))
1552 (radiop (insert (format "\\hyperref[%s]{%s}"
1553 (org-solidify-link-text raw-path) desc)))
1554 ((not type)
1555 (insert (format "\\hyperref[%s]{%s}"
1556 (org-remove-initial-hash
1557 (org-solidify-link-text raw-path)) desc)))
1558 (path (insert (format "\\href{%s}{%s}" path desc)))
1559 (t (insert "\\texttt{" desc "}")))))))
1561 (defun org-remove-initial-hash (s)
1562 (if (string-match "\\`#" s)
1563 (substring s 1)
1565 (defvar org-latex-entities) ; defined below
1566 (defvar org-latex-entities-regexp) ; defined below
1567 (defvar org-latex-entities-exceptions) ; defined below
1569 (defun org-export-latex-preprocess (parameters)
1570 "Clean stuff in the LaTeX export."
1571 ;; Preserve line breaks
1572 (goto-char (point-min))
1573 (while (re-search-forward "\\\\\\\\" nil t)
1574 (add-text-properties (match-beginning 0) (match-end 0)
1575 '(org-protected t)))
1577 ;; Preserve latex environments
1578 (goto-char (point-min))
1579 (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t)
1580 (let* ((start (progn (beginning-of-line) (point)))
1581 (end (and (re-search-forward
1582 (concat "^[ \t]*\\\\end{"
1583 (regexp-quote (match-string 1))
1584 "}") nil t)
1585 (point-at-eol))))
1586 (if end
1587 (add-text-properties start end '(org-protected t))
1588 (goto-char (point-at-eol)))))
1590 ;; Preserve math snippets
1592 (let* ((matchers (plist-get org-format-latex-options :matchers))
1593 (re-list org-latex-regexps)
1594 beg end re e m n block off)
1595 ;; Check the different regular expressions
1596 (while (setq e (pop re-list))
1597 (setq m (car e) re (nth 1 e) n (nth 2 e)
1598 block (if (nth 3 e) "\n\n" ""))
1599 (setq off (if (member m '("$" "$1")) 1 0))
1600 (when (and (member m matchers) (not (equal m "begin")))
1601 (goto-char (point-min))
1602 (while (re-search-forward re nil t)
1603 (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
1604 (add-text-properties beg end '(org-protected t org-latex-math t))))))
1606 ;; Convert LaTeX to \LaTeX{}
1607 (goto-char (point-min))
1608 (let ((case-fold-search nil))
1609 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
1610 (org-if-unprotected
1611 (replace-match (org-export-latex-protect-string
1612 (concat (match-string 1) "\\LaTeX{}")) t t))))
1614 ;; Convert blockquotes
1615 (goto-char (point-min))
1616 (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
1617 (org-replace-match-keep-properties "\\begin{quote}" t t))
1618 (goto-char (point-min))
1619 (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
1620 (org-replace-match-keep-properties "\\end{quote}" t t))
1622 ;; Convert verse
1623 (goto-char (point-min))
1624 (while (search-forward "ORG-VERSE-START" nil t)
1625 (org-replace-match-keep-properties "\\begin{verse}" t t)
1626 (beginning-of-line 2)
1627 (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
1628 (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
1629 (goto-char (match-end 1))
1630 (org-replace-match-keep-properties
1631 (org-export-latex-protect-string
1632 (concat "\\hspace*{1cm}" (match-string 2))) t t)
1633 (beginning-of-line 1))
1634 (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
1635 (end-of-line 1)
1636 (insert "\\\\"))
1637 (beginning-of-line 2))
1638 (and (looking-at "[ \t]*ORG-VERSE-END.*")
1639 (org-replace-match-keep-properties "\\end{verse}" t t)))
1641 ;; Convert center
1642 (goto-char (point-min))
1643 (while (search-forward "ORG-CENTER-START" nil t)
1644 (org-replace-match-keep-properties "\\begin{center}" t t))
1645 (goto-char (point-min))
1646 (while (search-forward "ORG-CENTER-END" nil t)
1647 (org-replace-match-keep-properties "\\end{center}" t t))
1649 (run-hooks 'org-export-latex-after-blockquotes-hook)
1651 ;; Convert horizontal rules
1652 (goto-char (point-min))
1653 (while (re-search-forward "^----+.$" nil t)
1654 (org-if-unprotected
1655 (replace-match (org-export-latex-protect-string "\\hrule") t t)))
1657 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
1658 (let ((re (concat "\\\\[a-zA-Z]+\\(?:"
1659 "\\[.*\\]"
1660 "\\)?"
1661 (org-create-multibrace-regexp "{" "}" 3))))
1662 (while (re-search-forward re nil t)
1663 (add-text-properties (match-beginning 0) (match-end 0)
1664 '(org-protected t))))
1666 ;; Protect LaTeX entities
1667 (goto-char (point-min))
1668 (let (a)
1669 (while (re-search-forward org-latex-entities-regexp nil t)
1670 (if (setq a (assoc (match-string 0) org-latex-entities-exceptions))
1671 (replace-match (org-add-props (nth 1 a) nil 'org-protected t)
1672 t t)
1673 (add-text-properties (match-beginning 0) (match-end 0)
1674 '(org-protected t)))))
1676 ;; Replace radio links
1677 (goto-char (point-min))
1678 (while (re-search-forward
1679 (concat "<<<?" org-export-latex-all-targets-re
1680 ">>>?\\((INVISIBLE)\\)?") nil t)
1681 (org-if-unprotected
1682 (replace-match
1683 (org-export-latex-protect-string
1684 (format "\\label{%s}%s" (save-match-data (org-solidify-link-text
1685 (match-string 1)))
1686 (if (match-string 2) "" (match-string 1)))) t t)))
1688 ;; Delete @<...> constructs
1689 ;; Thanks to Daniel Clemente for this regexp
1690 (goto-char (point-min))
1691 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
1692 (org-if-unprotected
1693 (replace-match "")))
1695 ;; When converting to LaTeX, replace footnotes
1696 ;; FIXME: don't protect footnotes from conversion
1697 (when (plist-get org-export-latex-options-plist :footnotes)
1698 (goto-char (point-min))
1699 (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t)
1700 (org-if-unprotected
1701 (when (save-match-data
1702 (save-excursion (beginning-of-line)
1703 (looking-at "[^:|#]")))
1704 (let ((foot-beg (match-beginning 0))
1705 (foot-end (match-end 0))
1706 (foot-prefix (match-string 0))
1707 footnote footnote-rpl)
1708 (save-excursion
1709 (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix))
1710 nil t))
1711 (replace-match "$^{\\1}$")
1712 (replace-match "")
1713 (let ((end (save-excursion
1714 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
1715 (match-beginning 0) (point-max)))))
1716 (setq footnote (concat (org-trim (buffer-substring (point) end))
1717 " ")) ; prevent last } being part of a link
1718 (delete-region (point) end))
1719 (goto-char foot-beg)
1720 (delete-region foot-beg foot-end)
1721 (unless (null footnote)
1722 (setq footnote-rpl (format "\\footnote{%s}" footnote))
1723 (add-text-properties 0 10 '(org-protected t) footnote-rpl)
1724 (add-text-properties (1- (length footnote-rpl))
1725 (length footnote-rpl)
1726 '(org-protected t) footnote-rpl)
1727 (insert footnote-rpl)))
1728 )))))
1730 ;; Remove footnote section tag for LaTeX
1731 (goto-char (point-min))
1732 (while (re-search-forward
1733 (concat "^" footnote-section-tag-regexp) nil t)
1734 (org-if-unprotected
1735 (replace-match "")))))
1737 ;;; List handling:
1739 (defun org-export-latex-lists ()
1740 "Convert plain text lists in current buffer into LaTeX lists."
1741 (goto-char (point-min))
1742 (while (re-search-forward org-list-beginning-re nil t)
1743 (org-if-unprotected
1744 (beginning-of-line)
1745 (insert (org-list-to-latex (org-list-parse-list t)
1746 org-export-latex-list-parameters))
1747 "\n")))
1749 (defconst org-latex-entities
1750 '("\\!"
1751 "\\'"
1752 "\\+"
1753 "\\,"
1754 "\\-"
1755 "\\:"
1756 "\\;"
1757 "\\<"
1758 "\\="
1759 "\\>"
1760 "\\Huge"
1761 "\\LARGE"
1762 "\\Large"
1763 "\\Styles"
1764 "\\\\"
1765 "\\`"
1766 "\\addcontentsline"
1767 "\\address"
1768 "\\addtocontents"
1769 "\\addtocounter"
1770 "\\addtolength"
1771 "\\addvspace"
1772 "\\alph"
1773 "\\appendix"
1774 "\\arabic"
1775 "\\author"
1776 "\\begin{array}"
1777 "\\begin{center}"
1778 "\\begin{description}"
1779 "\\begin{enumerate}"
1780 "\\begin{eqnarray}"
1781 "\\begin{equation}"
1782 "\\begin{figure}"
1783 "\\begin{flushleft}"
1784 "\\begin{flushright}"
1785 "\\begin{itemize}"
1786 "\\begin{list}"
1787 "\\begin{minipage}"
1788 "\\begin{picture}"
1789 "\\begin{quotation}"
1790 "\\begin{quote}"
1791 "\\begin{tabbing}"
1792 "\\begin{table}"
1793 "\\begin{tabular}"
1794 "\\begin{thebibliography}"
1795 "\\begin{theorem}"
1796 "\\begin{titlepage}"
1797 "\\begin{verbatim}"
1798 "\\begin{verse}"
1799 "\\bf"
1800 "\\bf"
1801 "\\bibitem"
1802 "\\bigskip"
1803 "\\cdots"
1804 "\\centering"
1805 "\\circle"
1806 "\\cite"
1807 "\\cleardoublepage"
1808 "\\clearpage"
1809 "\\cline"
1810 "\\closing"
1811 "\\dashbox"
1812 "\\date"
1813 "\\ddots"
1814 "\\dotfill"
1815 "\\em"
1816 "\\fbox"
1817 "\\flushbottom"
1818 "\\fnsymbol"
1819 "\\footnote"
1820 "\\footnotemark"
1821 "\\footnotesize"
1822 "\\footnotetext"
1823 "\\frac"
1824 "\\frame"
1825 "\\framebox"
1826 "\\hfill"
1827 "\\hline"
1828 "\\hrulespace"
1829 "\\hspace"
1830 "\\huge"
1831 "\\hyphenation"
1832 "\\include"
1833 "\\includeonly"
1834 "\\indent"
1835 "\\input"
1836 "\\it"
1837 "\\kill"
1838 "\\label"
1839 "\\large"
1840 "\\ldots"
1841 "\\line"
1842 "\\linebreak"
1843 "\\linethickness"
1844 "\\listoffigures"
1845 "\\listoftables"
1846 "\\location"
1847 "\\makebox"
1848 "\\maketitle"
1849 "\\mark"
1850 "\\mbox"
1851 "\\medskip"
1852 "\\multicolumn"
1853 "\\multiput"
1854 ("\\nbsp" "~")
1855 "\\newcommand"
1856 "\\newcounter"
1857 "\\newenvironment"
1858 "\\newfont"
1859 "\\newlength"
1860 "\\newline"
1861 "\\newpage"
1862 "\\newsavebox"
1863 "\\newtheorem"
1864 "\\nocite"
1865 "\\nofiles"
1866 "\\noindent"
1867 "\\nolinebreak"
1868 "\\nopagebreak"
1869 "\\normalsize"
1870 "\\onecolumn"
1871 "\\opening"
1872 "\\oval"
1873 "\\overbrace"
1874 "\\overline"
1875 "\\pagebreak"
1876 "\\pagenumbering"
1877 "\\pageref"
1878 "\\pagestyle"
1879 "\\par"
1880 "\\parbox"
1881 "\\put"
1882 "\\raggedbottom"
1883 "\\raggedleft"
1884 "\\raggedright"
1885 "\\raisebox"
1886 "\\ref"
1887 "\\rm"
1888 "\\roman"
1889 "\\rule"
1890 "\\savebox"
1891 "\\sc"
1892 "\\scriptsize"
1893 "\\setcounter"
1894 "\\setlength"
1895 "\\settowidth"
1896 "\\sf"
1897 "\\shortstack"
1898 "\\signature"
1899 "\\sl"
1900 "\\small"
1901 "\\smallskip"
1902 "\\sqrt"
1903 "\\tableofcontents"
1904 "\\telephone"
1905 "\\thanks"
1906 "\\thispagestyle"
1907 "\\tiny"
1908 "\\title"
1909 "\\tt"
1910 "\\twocolumn"
1911 "\\typein"
1912 "\\typeout"
1913 "\\underbrace"
1914 "\\underline"
1915 "\\usebox"
1916 "\\usecounter"
1917 "\\value"
1918 "\\vdots"
1919 "\\vector"
1920 "\\verb"
1921 "\\vfill"
1922 "\\vline"
1923 "\\vspace")
1924 "A list of LaTeX commands to be protected when performing conversion.")
1926 (defvar org-latex-entities-exceptions nil)
1928 (defconst org-latex-entities-regexp
1929 (let (names rest)
1930 (dolist (x org-latex-entities)
1931 (when (consp x)
1932 (add-to-list 'org-latex-entities-exceptions x)
1933 (setq x (car x)))
1934 (if (string-match "[a-z][A-Z]$" x)
1935 (push x names)
1936 (push x rest)))
1937 (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
1938 "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
1940 (provide 'org-export-latex)
1941 (provide 'org-latex)
1943 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
1945 ;;; org-latex.el ends here