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