Use the normal overlay API, not Org's one
[org-mode/org-tableheadings.git] / lisp / org-latex.el
blob67d97457030573bf34c0019b6a78540b4946f254
1 ;;; org-latex.el --- LaTeX exporter for org-mode
2 ;;
3 ;; Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 ;;
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: org-latex.el
7 ;; Version: 6.35trans
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)
53 (require 'org-beamer)
55 ;;; Variables:
56 (defvar org-export-latex-class nil)
57 (defvar org-export-latex-class-options nil)
58 (defvar org-export-latex-header nil)
59 (defvar org-export-latex-append-header nil)
60 (defvar org-export-latex-options-plist nil)
61 (defvar org-export-latex-todo-keywords-1 nil)
62 (defvar org-export-latex-complex-heading-re nil)
63 (defvar org-export-latex-not-done-keywords nil)
64 (defvar org-export-latex-done-keywords nil)
65 (defvar org-export-latex-display-custom-times nil)
66 (defvar org-export-latex-all-targets-re nil)
67 (defvar org-export-latex-add-level 0)
68 (defvar org-export-latex-sectioning "")
69 (defvar org-export-latex-sectioning-depth 0)
70 (defvar org-export-latex-special-keyword-regexp
71 (concat "\\<\\(" org-scheduled-string "\\|"
72 org-deadline-string "\\|"
73 org-closed-string"\\)")
74 "Regexp matching special time planning keywords plus the time after it.")
76 (defvar latexp) ; dynamically scoped from org.el
77 (defvar re-quote) ; dynamically scoped from org.el
78 (defvar commentsp) ; dynamically scoped from org.el
80 ;;; User variables:
82 (defgroup org-export-latex nil
83 "Options for exporting Org-mode files to LaTeX."
84 :tag "Org Export LaTeX"
85 :group 'org-export)
87 (defcustom org-export-latex-default-class "article"
88 "The default LaTeX class."
89 :group 'org-export-latex
90 :type '(string :tag "LaTeX class"))
92 (defcustom org-export-latex-classes
93 '(("article"
94 "\\documentclass[11pt]{article}"
95 ("\\section{%s}" . "\\section*{%s}")
96 ("\\subsection{%s}" . "\\subsection*{%s}")
97 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
98 ("\\paragraph{%s}" . "\\paragraph*{%s}")
99 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
100 ("report"
101 "\\documentclass[11pt]{report}"
102 ("\\part{%s}" . "\\part*{%s}")
103 ("\\chapter{%s}" . "\\chapter*{%s}")
104 ("\\section{%s}" . "\\section*{%s}")
105 ("\\subsection{%s}" . "\\subsection*{%s}")
106 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
107 ("book"
108 "\\documentclass[11pt]{book}"
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 ("beamer"
115 "\\documentclass{beamer}"
116 org-beamer-sectioning
118 "Alist of LaTeX classes and associated header and structure.
119 If #+LaTeX_CLASS is set in the buffer, use its value and the
120 associated information. Here is the structure of each cell:
122 \(class-name
123 header-string
124 (numbered-section . unnumbered-section\)
125 ...\)
127 The header string
128 -----------------
130 The HEADER-STRING is the header that will be inserted into the LaTeX file.
131 It should contain the \\documentclass macro, and anything else that is needed
132 for this setup. To this header, the following commands will be added:
134 - Calls to \\usepackage for all packages mentioned in the variables
135 `org-export-latex-default-packages-alist' and
136 `org-export-latex-packages-alist'. Thus, your header definitions should
137 avoid to also request these packages.
139 - Lines specified via \"#+LaTeX_HEADER:\"
141 If you need more control about the sequence in which the header is built
142 up, or if you want to exclude one of these building blocks for a particular
143 class, you can use the following macro-like placeholders.
145 [DEFAULT-PACKAGES] \\usepackage statements for default packages
146 [NO-DEFAULT-PACKAGES] do not include any of the default packages
147 [PACKAGES] \\usepackage statements for packages
148 [NO-PACKAGES] do not include the packages
149 [EXTRA] the stuff from #+LaTeX_HEADER
150 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
152 So a header like
154 \\documentclass{article}
155 [NO-DEFAULT-PACKAGES]
156 [EXTRA]
157 \\providecommand{\\alert}[1]{\\textbf{#1}}
158 [PACKAGES]
160 will omit the default packages, and will include the #+LaTeX_HEADER lines,
161 then have a call to \\providecommand, and then place \\usepackage commands
162 based on the content of `org-export-latex-packages-alist'.
164 If your header or `org-export-latex-default-packages-alist' inserts
165 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be replaced with
166 a coding system derived from `buffer-file-coding-system'. See also the
167 variable `org-export-latex-inputenc-alist' for a way to influence this
168 mechanism.
170 The sectioning structure
171 ------------------------
173 The sectioning structure of the class is given by the elements following
174 the header string. For each sectioning level, a number of strings is
175 specified. A %s formatter is mandatory in each section string and will
176 be replaced by the title of the section.
178 Instead of a cons cell (numbered . unnumbered), you can also provide a list
179 of 2 or 4 elements,
181 (numbered-open numbered-close)
185 (numbered-open numbered-close unnumbered-open unnumbered-close)
187 providing opening and closing strings for a LaTeX environment that should
188 represent the document section. The opening clause should have a %s
189 to represent the section title.
191 Instead of a list of sectioning commands, you can also specify a
192 function name. That function will be called with two parameters,
193 the (reduced) level of the headline, and the headline text. The function
194 must return a cons cell with the (possibly modified) headline text, and the
195 sectioning list in the cdr."
196 :group 'org-export-latex
197 :type '(repeat
198 (list (string :tag "LaTeX class")
199 (string :tag "LaTeX header")
200 (repeat :tag "Levels" :inline t
201 (choice
202 (cons :tag "Heading"
203 (string :tag " numbered")
204 (string :tag "unnumbered"))
205 (list :tag "Environment"
206 (string :tag "Opening (numbered)")
207 (string :tag "Closing (numbered)")
208 (string :tag "Opening (unnumbered)")
209 (string :tag "Closing (unnumbered)"))
210 (function :tag "Hook computing sectioning"))))))
212 (defcustom org-export-latex-inputenc-alist nil
213 "Alist of inputenc coding system names, and what should really be used.
214 For example, adding an entry
216 (\"utf8\" . \"utf8x\")
218 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
219 are written as utf8 files."
220 :group 'org-export-latex
221 :type '(repeat
222 (cons
223 (string :tag "Derived from buffer")
224 (string :tag "Use this instead"))))
227 (defcustom org-export-latex-emphasis-alist
228 '(("*" "\\textbf{%s}" nil)
229 ("/" "\\emph{%s}" nil)
230 ("_" "\\underline{%s}" nil)
231 ("+" "\\st{%s}" nil)
232 ("=" "\\verb" t)
233 ("~" "\\verb" t))
234 "Alist of LaTeX expressions to convert emphasis fontifiers.
235 Each element of the list is a list of three elements.
236 The first element is the character used as a marker for fontification.
237 The second element is a formatting string to wrap fontified text with.
238 If it is \"\\verb\", Org will automatically select a delimiter
239 character that is not in the string.
240 The third element decides whether to protect converted text from other
241 conversions."
242 :group 'org-export-latex
243 :type 'alist)
245 (defcustom org-export-latex-title-command "\\maketitle"
246 "The command used to insert the title just after \\begin{document}.
247 If this string contains the formatting specification \"%s\" then
248 it will be used as a formatting string, passing the title as an
249 argument."
250 :group 'org-export-latex
251 :type 'string)
253 (defcustom org-export-latex-import-inbuffer-stuff nil
254 "Non-nil means define TeX macros for Org's inbuffer definitions.
255 For example \orgTITLE for #+TITLE."
256 :group 'org-export-latex
257 :type 'boolean)
259 (defcustom org-export-latex-date-format
260 "%d %B %Y"
261 "Format string for \\date{...}."
262 :group 'org-export-latex
263 :type 'string)
265 (defcustom org-export-latex-todo-keyword-markup "\\textbf{%s}"
266 "Markup for TODO keywords, as a printf format.
267 This can be a single format for all keywords, a cons cell with separate
268 formats for not-done and done states, or an association list with setup
269 for individual keywords. If a keyword shows up for which there is no
270 markup defined, the first one in the association list will be used."
271 :group 'org-export-latex
272 :type '(choice
273 (string :tag "Default")
274 (cons :tag "Distinguish undone and done"
275 (string :tag "Not-DONE states")
276 (string :tag "DONE states"))
277 (repeat :tag "Per keyword markup"
278 (cons
279 (string :tag "Keyword")
280 (string :tag "Markup")))))
282 (defcustom org-export-latex-timestamp-markup "\\textit{%s}"
283 "A printf format string to be applied to time stamps."
284 :group 'org-export-latex
285 :type 'string)
287 (defcustom org-export-latex-timestamp-keyword-markup "\\texttt{%s}"
288 "A printf format string to be applied to time stamps."
289 :group 'org-export-latex
290 :type 'string)
292 (defcustom org-export-latex-hyperref-format "\\href{%s}{%s}"
293 "A printf format string to be applied to hyperref links.
294 The format must contain two %s instances. The first will be filled with
295 the link, the second with the link description."
296 :group 'org-export-latex
297 :type 'string)
299 (defcustom org-export-latex-tables-verbatim nil
300 "When non-nil, tables are exported verbatim."
301 :group 'org-export-latex
302 :type 'boolean)
304 (defcustom org-export-latex-tables-centered t
305 "When non-nil, tables are exported in a center environment."
306 :group 'org-export-latex
307 :type 'boolean)
309 (defcustom org-export-latex-tables-column-borders nil
310 "When non-nil, grouping columns can cause outer vertical lines in tables.
311 When nil, grouping causes only separation lines between groups."
312 :group 'org-export-latex
313 :type 'boolean)
315 (defcustom org-export-latex-low-levels 'itemize
316 "How to convert sections below the current level of sectioning.
317 This is specified by the `org-export-headline-levels' option or the
318 value of \"H:\" in Org's #+OPTION line.
320 This can be either nil (skip the sections), `description', `itemize',
321 or `enumerate' (convert the sections as the corresponding list type), or
322 a string to be used instead of \\section{%s}. In this latter case,
323 the %s stands here for the inserted headline and is mandatory.
325 It may also be a list of three string to define a user-defined environment
326 that should be used. The first string should be the like
327 \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
328 to two occurrences of %s for the title and a label, respectively. The third
329 string should be like \"\\end{itemize\"."
330 :group 'org-export-latex
331 :type '(choice (const :tag "Ignore" nil)
332 (const :tag "Convert as descriptive list" description)
333 (const :tag "Convert as itemized list" itemize)
334 (const :tag "Convert as enumerated list" enumerate)
335 (list :tag "User-defined environment"
336 :value ("\\begin{itemize}" "\\end{itemize}" "\\item %s")
337 (string :tag "Start")
338 (string :tag "End")
339 (string :tag "item"))
340 (string :tag "Use a section string" :value "\\subparagraph{%s}")))
342 (defcustom org-export-latex-list-parameters
343 '(:cbon "$\\boxtimes$" :cboff "$\\Box$")
344 "Parameters for the LaTeX list exporter.
345 These parameters will be passed on to `org-list-to-latex', which in turn
346 will pass them (combined with the LaTeX default list parameters) to
347 `org-list-to-generic'."
348 :group 'org-export-latex
349 :type 'plist)
351 (defcustom org-export-latex-verbatim-wrap
352 '("\\begin{verbatim}\n" . "\\end{verbatim}\n")
353 "Environment to be wrapped around a fixed-width section in LaTeX export.
354 This is a cons with two strings, to be added before and after the
355 fixed-with text.
357 Defaults to \\begin{verbatim} and \\end{verbatim}."
358 :group 'org-export-translation
359 :group 'org-export-latex
360 :type '(cons (string :tag "Open")
361 (string :tag "Close")))
363 (defcustom org-export-latex-listings nil
364 "Non-nil means export source code using the listings package.
365 This package will fontify source code, possibly even with color.
366 If you want to use this, you also need to make LaTeX use the
367 listings package, and if you want to have color, the color
368 package. Just add these to `org-export-latex-packages-alist',
369 for example using customize, or with something like
371 (require 'org-latex)
372 (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
373 (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))"
374 :group 'org-export-latex
375 :type 'boolean)
377 (defcustom org-export-latex-listings-langs
378 '((emacs-lisp "Lisp") (lisp "Lisp")
379 (c "C") (cc "C++")
380 (fortran "fortran")
381 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
382 (html "HTML") (xml "XML")
383 (tex "TeX") (latex "TeX")
384 (shell-script "bash")
385 (gnuplot "Gnuplot")
386 (ocaml "Caml") (caml "Caml")
387 (sql "SQL"))
388 "Alist mapping languages to their listing language counterpart.
389 The key is a symbol, the major mode symbol without the \"-mode\".
390 The value is the string that should be inserted as the language parameter
391 for the listings package. If the mode name and the listings name are
392 the same, the language does not need an entry in this list - but it does not
393 hurt if it is present."
394 :group 'org-export-latex
395 :type '(repeat
396 (list
397 (symbol :tag "Major mode ")
398 (string :tag "Listings language"))))
400 (defcustom org-export-latex-remove-from-headlines
401 '(:todo nil :priority nil :tags nil)
402 "A plist of keywords to remove from headlines. OBSOLETE.
403 Non-nil means remove this keyword type from the headline.
405 Don't remove the keys, just change their values.
407 Obsolete, this variable is no longer used. Use the separate
408 variables `org-export-with-todo-keywords', `org-export-with-priority',
409 and `org-export-with-tags' instead."
410 :type 'plist
411 :group 'org-export-latex)
413 (defcustom org-export-latex-image-default-option "width=10em"
414 "Default option for images."
415 :group 'org-export-latex
416 :type 'string)
418 (defcustom org-export-latex-inline-image-extensions
419 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
420 "Extensions of image files that can be inlined into LaTeX.
421 Note that the image extension *actually* allowed depend on the way the
422 LaTeX file is processed. When used with pdflatex, pdf, jpg and png images
423 are OK. When processing through dvi to Postscript, only ps and eps are
424 allowed. The default we use here encompasses both."
425 :group 'org-export-latex
426 :type '(repeat (string :tag "Extension")))
428 (defcustom org-export-latex-coding-system nil
429 "Coding system for the exported LaTex file."
430 :group 'org-export-latex
431 :type 'coding-system)
433 (defgroup org-export-pdf nil
434 "Options for exporting Org-mode files to PDF, via LaTeX."
435 :tag "Org Export PDF"
436 :group 'org-export-latex
437 :group 'org-export)
439 (defcustom org-latex-to-pdf-process
440 '("pdflatex -interaction nonstopmode %s"
441 "pdflatex -interaction nonstopmode %s")
442 "Commands to process a LaTeX file to a PDF file.
443 This is a list of strings, each of them will be given to the shell
444 as a command. %s in the command will be replaced by the full file name, %b
445 by the file base name (i.e. without extension).
446 The reason why this is a list is that it usually takes several runs of
447 pdflatex, maybe mixed with a call to bibtex. Org does not have a clever
448 mechanism to detect which of these commands have to be run to get to a stable
449 result, and it also does not do any error checking.
451 Alternatively, this may be a Lisp function that does the processing, so you
452 could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
453 This function should accept the file name as its single argument."
454 :group 'org-export-pdf
455 :type '(choice (repeat :tag "Shell command sequence"
456 (string :tag "Shell command"))
457 (function)))
459 (defcustom org-export-pdf-logfiles
460 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
461 "The list of file extensions to consider as LaTeX logfiles."
462 :group 'org-export-pdf
463 :type '(repeat (string :tag "Extension")))
465 (defcustom org-export-pdf-remove-logfiles t
466 "Non-nil means remove the logfiles produced by PDF production.
467 These are the .aux, .log, .out, and .toc files."
468 :group 'org-export-pdf
469 :type 'boolean)
471 ;;; Hooks
473 (defvar org-export-latex-after-initial-vars-hook nil
474 "Hook run before LaTeX export.
475 The exact moment is after the initial variables like org-export-latex-class
476 have been determined from the environment.")
478 (defvar org-export-latex-after-blockquotes-hook nil
479 "Hook run during LaTeX export, after blockquote, verse, center are done.")
481 (defvar org-export-latex-final-hook nil
482 "Hook run in the finalized LaTeX buffer.")
484 (defvar org-export-latex-after-save-hook nil
485 "Hook run in the finalized LaTeX buffer, after it has been saved.")
487 ;;; Autoload functions:
489 ;;;###autoload
490 (defun org-export-as-latex-batch ()
491 "Call `org-export-as-latex', may be used in batch processing.
492 For example:
494 emacs --batch
495 --load=$HOME/lib/emacs/org.el
496 --eval \"(setq org-export-headline-levels 2)\"
497 --visit=MyFile --funcall org-export-as-latex-batch"
498 (org-export-as-latex org-export-headline-levels 'hidden))
500 ;;;###autoload
501 (defun org-export-as-latex-to-buffer (arg)
502 "Call `org-export-as-latex` with output to a temporary buffer.
503 No file is created. The prefix ARG is passed through to `org-export-as-latex'."
504 (interactive "P")
505 (org-export-as-latex arg nil nil "*Org LaTeX Export*")
506 (when org-export-show-temporary-export-buffer
507 (switch-to-buffer-other-window "*Org LaTeX Export*")))
509 ;;;###autoload
510 (defun org-replace-region-by-latex (beg end)
511 "Replace the region from BEG to END with its LaTeX export.
512 It assumes the region has `org-mode' syntax, and then convert it to
513 LaTeX. This can be used in any buffer. For example, you could
514 write an itemized list in `org-mode' syntax in an LaTeX buffer and
515 then use this command to convert it."
516 (interactive "r")
517 (let (reg latex buf)
518 (save-window-excursion
519 (if (org-mode-p)
520 (setq latex (org-export-region-as-latex
521 beg end t 'string))
522 (setq reg (buffer-substring beg end)
523 buf (get-buffer-create "*Org tmp*"))
524 (with-current-buffer buf
525 (erase-buffer)
526 (insert reg)
527 (org-mode)
528 (setq latex (org-export-region-as-latex
529 (point-min) (point-max) t 'string)))
530 (kill-buffer buf)))
531 (delete-region beg end)
532 (insert latex)))
534 ;;;###autoload
535 (defun org-export-region-as-latex (beg end &optional body-only buffer)
536 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
537 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
538 contents, and only produce the region of converted text, useful for
539 cut-and-paste operations.
540 If BUFFER is a buffer or a string, use/create that buffer as a target
541 of the converted LaTeX. If BUFFER is the symbol `string', return the
542 produced LaTeX as a string and leave no buffer behind. For example,
543 a Lisp program could call this function in the following way:
545 (setq latex (org-export-region-as-latex beg end t 'string))
547 When called interactively, the output buffer is selected, and shown
548 in a window. A non-interactive call will only return the buffer."
549 (interactive "r\nP")
550 (when (interactive-p)
551 (setq buffer "*Org LaTeX Export*"))
552 (let ((transient-mark-mode t) (zmacs-regions t)
553 ext-plist rtn)
554 (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
555 (goto-char end)
556 (set-mark (point)) ;; to activate the region
557 (goto-char beg)
558 (setq rtn (org-export-as-latex
559 nil nil ext-plist
560 buffer body-only))
561 (if (fboundp 'deactivate-mark) (deactivate-mark))
562 (if (and (interactive-p) (bufferp rtn))
563 (switch-to-buffer-other-window rtn)
564 rtn)))
566 ;;;###autoload
567 (defun org-export-as-latex (arg &optional hidden ext-plist
568 to-buffer body-only pub-dir)
569 "Export current buffer to a LaTeX file.
570 If there is an active region, export only the region. The prefix
571 ARG specifies how many levels of the outline should become
572 headlines. The default is 3. Lower levels will be exported
573 depending on `org-export-latex-low-levels'. The default is to
574 convert them as description lists.
575 HIDDEN is obsolete and does nothing.
576 EXT-PLIST is a property list with
577 external parameters overriding org-mode's default settings, but
578 still inferior to file-local settings. When TO-BUFFER is
579 non-nil, create a buffer with that name and export to that
580 buffer. If TO-BUFFER is the symbol `string', don't leave any
581 buffer behind but just return the resulting LaTeX as a string.
582 When BODY-ONLY is set, don't produce the file header and footer,
583 simply return the content of \begin{document}...\end{document},
584 without even the \begin{document} and \end{document} commands.
585 when PUB-DIR is set, use this as the publishing directory."
586 (interactive "P")
587 (when (and (not body-only) arg (listp arg)) (setq body-only t))
588 (run-hooks 'org-export-first-hook)
590 ;; Make sure we have a file name when we need it.
591 (when (and (not (or to-buffer body-only))
592 (not buffer-file-name))
593 (if (buffer-base-buffer)
594 (org-set-local 'buffer-file-name
595 (with-current-buffer (buffer-base-buffer)
596 buffer-file-name))
597 (error "Need a file name to be able to export")))
599 (message "Exporting to LaTeX...")
600 (org-unmodified
601 (let ((inhibit-read-only t))
602 (remove-text-properties (point-min) (point-max)
603 '(:org-license-to-kill nil))))
604 (org-update-radio-target-regexp)
605 (org-export-latex-set-initial-vars ext-plist arg)
606 (setq org-export-opt-plist org-export-latex-options-plist)
607 (org-install-letbind)
608 (run-hooks 'org-export-latex-after-initial-vars-hook)
609 (let* ((wcf (current-window-configuration))
610 (opt-plist org-export-latex-options-plist)
611 (region-p (org-region-active-p))
612 (rbeg (and region-p (region-beginning)))
613 (rend (and region-p (region-end)))
614 (subtree-p
615 (if (plist-get opt-plist :ignore-subtree-p)
617 (when region-p
618 (save-excursion
619 (goto-char rbeg)
620 (and (org-at-heading-p)
621 (>= (org-end-of-subtree t t) rend))))))
622 (opt-plist (setq org-export-opt-plist
623 (if subtree-p
624 (org-export-add-subtree-options opt-plist rbeg)
625 opt-plist)))
626 ;; Make sure the variable contains the updated values.
627 (org-export-latex-options-plist (setq org-export-opt-plist opt-plist))
628 ;; The following two are dynamically scoped into other
629 ;; routines below.
630 (org-current-export-dir
631 (or pub-dir (org-export-directory :html opt-plist)))
632 (org-current-export-file buffer-file-name)
633 (title (or (and subtree-p (org-export-get-title-from-subtree))
634 (plist-get opt-plist :title)
635 (and (not
636 (plist-get opt-plist :skip-before-1st-heading))
637 (org-export-grab-title-from-buffer))
638 (and buffer-file-name
639 (file-name-sans-extension
640 (file-name-nondirectory buffer-file-name)))
641 "No Title"))
642 (filename
643 (and (not to-buffer)
644 (concat
645 (file-name-as-directory
646 (or pub-dir
647 (org-export-directory :LaTeX ext-plist)))
648 (file-name-sans-extension
649 (or (and subtree-p
650 (org-entry-get rbeg "EXPORT_FILE_NAME" t))
651 (file-name-nondirectory ;sans-extension
652 (or buffer-file-name
653 (error "Don't know which export file to use.")))))
654 ".tex")))
655 (filename
656 (and filename
657 (if (equal (file-truename filename)
658 (file-truename (or buffer-file-name "dummy.org")))
659 (concat filename ".tex")
660 filename)))
661 (buffer (if to-buffer
662 (cond
663 ((eq to-buffer 'string) (get-buffer-create
664 "*Org LaTeX Export*"))
665 (t (get-buffer-create to-buffer)))
666 (find-file-noselect filename)))
667 (odd org-odd-levels-only)
668 (header (org-export-latex-make-header title opt-plist))
669 (skip (cond (subtree-p nil)
670 (region-p nil)
671 (t (plist-get opt-plist :skip-before-1st-heading))))
672 (text (plist-get opt-plist :text))
673 (org-export-preprocess-hook
674 (cons
675 `(lambda () (org-set-local 'org-complex-heading-regexp
676 ,org-export-latex-complex-heading-re))
677 org-export-preprocess-hook))
678 (first-lines (if skip "" (org-export-latex-first-lines
679 opt-plist
680 (if subtree-p
681 (save-excursion
682 (goto-char rbeg)
683 (point-at-bol 2))
684 rbeg)
685 (if region-p rend))))
686 (coding-system (and (boundp 'buffer-file-coding-system)
687 buffer-file-coding-system))
688 (coding-system-for-write (or org-export-latex-coding-system
689 coding-system))
690 (save-buffer-coding-system (or org-export-latex-coding-system
691 coding-system))
692 (region (buffer-substring
693 (if region-p (region-beginning) (point-min))
694 (if region-p (region-end) (point-max))))
695 (text
696 (and text (string-match "\\S-" text)
697 (org-export-preprocess-string
698 text
699 :emph-multiline t
700 :for-LaTeX t
701 :comments nil
702 :tags (plist-get opt-plist :tags)
703 :priority (plist-get opt-plist :priority)
704 :footnotes (plist-get opt-plist :footnotes)
705 :drawers (plist-get opt-plist :drawers)
706 :timestamps (plist-get opt-plist :timestamps)
707 :todo-keywords (plist-get opt-plist :todo-keywords)
708 :add-text nil
709 :skip-before-1st-heading skip
710 :select-tags nil
711 :exclude-tags nil
712 :LaTeX-fragments nil)))
713 (string-for-export
714 (org-export-preprocess-string
715 region
716 :emph-multiline t
717 :for-LaTeX t
718 :comments nil
719 :tags (plist-get opt-plist :tags)
720 :priority (plist-get opt-plist :priority)
721 :footnotes (plist-get opt-plist :footnotes)
722 :drawers (plist-get opt-plist :drawers)
723 :timestamps (plist-get opt-plist :timestamps)
724 :todo-keywords (plist-get opt-plist :todo-keywords)
725 :add-text (if (eq to-buffer 'string) nil text)
726 :skip-before-1st-heading skip
727 :select-tags (plist-get opt-plist :select-tags)
728 :exclude-tags (plist-get opt-plist :exclude-tags)
729 :LaTeX-fragments nil)))
731 (set-buffer buffer)
732 (erase-buffer)
733 (org-install-letbind)
735 (and (fboundp 'set-buffer-file-coding-system)
736 (set-buffer-file-coding-system coding-system-for-write))
738 ;; insert the header and initial document commands
739 (unless (or (eq to-buffer 'string) body-only)
740 (insert header))
742 ;; insert text found in #+TEXT
743 (when (and text (not (eq to-buffer 'string)))
744 (insert (org-export-latex-content
745 text '(lists tables fixed-width keywords))
746 "\n\n"))
748 ;; insert lines before the first headline
749 (unless skip
750 (insert first-lines))
752 ;; export the content of headlines
753 (org-export-latex-global
754 (with-temp-buffer
755 (insert string-for-export)
756 (goto-char (point-min))
757 (when (re-search-forward "^\\(\\*+\\) " nil t)
758 (let* ((asters (length (match-string 1)))
759 (level (if odd (- asters 2) (- asters 1))))
760 (setq org-export-latex-add-level
761 (if odd (1- (/ (1+ asters) 2)) (1- asters)))
762 (org-export-latex-parse-global level odd)))))
764 ;; finalization
765 (unless body-only (insert "\n\\end{document}"))
767 ;; Attach description terms to the \item macro
768 (goto-char (point-min))
769 (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t)
770 (delete-region (match-beginning 1) (match-end 1)))
772 ;; Relocate the table of contents
773 (goto-char (point-min))
774 (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
775 (goto-char (point-min))
776 (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t)
777 (replace-match ""))
778 (goto-char (point-min))
779 (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
780 (replace-match "\\tableofcontents" t t)))
782 ;; Cleanup forced line ends in items where they are not needed
783 (goto-char (point-min))
784 (while (re-search-forward
785 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin"
786 nil t)
787 (delete-region (match-beginning 1) (match-end 1)))
788 (goto-char (point-min))
789 (while (re-search-forward
790 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*"
791 nil t)
792 (if (looking-at "[\n \t]+")
793 (replace-match "\n")))
795 (run-hooks 'org-export-latex-final-hook)
796 (or to-buffer (save-buffer))
797 (org-export-latex-fix-inputenc)
798 (run-hooks 'org-export-latex-after-save-hook)
799 (goto-char (point-min))
800 (or (org-export-push-to-kill-ring "LaTeX")
801 (message "Exporting to LaTeX...done"))
802 (prog1
803 (if (eq to-buffer 'string)
804 (prog1 (buffer-substring (point-min) (point-max))
805 (kill-buffer (current-buffer)))
806 (current-buffer))
807 (set-window-configuration wcf))))
809 ;;;###autoload
810 (defun org-export-as-pdf (arg &optional hidden ext-plist
811 to-buffer body-only pub-dir)
812 "Export as LaTeX, then process through to PDF."
813 (interactive "P")
814 (message "Exporting to PDF...")
815 (let* ((wconfig (current-window-configuration))
816 (lbuf (org-export-as-latex arg hidden ext-plist
817 to-buffer body-only pub-dir))
818 (file (buffer-file-name lbuf))
819 (base (file-name-sans-extension (buffer-file-name lbuf)))
820 (pdffile (concat base ".pdf"))
821 (cmds org-latex-to-pdf-process)
822 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
823 (bibtex-p (with-current-buffer lbuf
824 (save-excursion
825 (goto-char (point-min))
826 (re-search-forward "\\\\bibliography{" nil t))))
827 cmd)
828 (with-current-buffer outbuf (erase-buffer))
829 (message "Processing LaTeX file...")
830 (if (and cmds (symbolp cmds))
831 (funcall cmds file)
832 (while cmds
833 (setq cmd (pop cmds))
834 (while (string-match "%b" cmd)
835 (setq cmd (replace-match
836 (save-match-data
837 (shell-quote-argument base))
838 t t cmd)))
839 (while (string-match "%s" cmd)
840 (setq cmd (replace-match
841 (save-match-data
842 (shell-quote-argument file))
843 t t cmd)))
844 (shell-command cmd outbuf outbuf)))
845 (message "Processing LaTeX file...done")
846 (if (not (file-exists-p pdffile))
847 (error "PDF file was not produced")
848 (set-window-configuration wconfig)
849 (when org-export-pdf-remove-logfiles
850 (dolist (ext org-export-pdf-logfiles)
851 (setq file (concat base "." ext))
852 (and (file-exists-p file) (delete-file file))))
853 (message "Exporting to PDF...done")
854 pdffile)))
856 ;;;###autoload
857 (defun org-export-as-pdf-and-open (arg)
858 "Export as LaTeX, then process through to PDF, and open."
859 (interactive "P")
860 (let ((pdffile (org-export-as-pdf arg)))
861 (if pdffile
862 (progn
863 (org-open-file pdffile)
864 (when org-export-kill-product-buffer-when-displayed
865 (kill-buffer (find-buffer-visiting
866 (concat (file-name-sans-extension (buffer-file-name))
867 ".tex")))))
868 (error "PDF file was not produced"))))
870 ;;; Parsing functions:
872 (defun org-export-latex-parse-global (level odd)
873 "Parse the current buffer recursively, starting at LEVEL.
874 If ODD is non-nil, assume the buffer only contains odd sections.
875 Return a list reflecting the document structure."
876 (save-excursion
877 (goto-char (point-min))
878 (let* ((cnt 0) output
879 (depth org-export-latex-sectioning-depth))
880 (while (org-re-search-forward-unprotected
881 (concat "^\\(\\(?:\\*\\)\\{"
882 (number-to-string (+ (if odd 2 1) level))
883 "\\}\\) \\(.*\\)$")
884 ;; make sure that there is no upper heading
885 (when (> level 0)
886 (save-excursion
887 (save-match-data
888 (org-re-search-forward-unprotected
889 (concat "^\\(\\(?:\\*\\)\\{"
890 (number-to-string level)
891 "\\}\\) \\(.*\\)$") nil t)))) t)
892 (setq cnt (1+ cnt))
893 (let* ((pos (match-beginning 0))
894 (heading (match-string 2))
895 (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
896 (save-excursion
897 (narrow-to-region
898 (point)
899 (save-match-data
900 (if (org-re-search-forward-unprotected
901 (concat "^\\(\\(?:\\*\\)\\{"
902 (number-to-string (+ (if odd 2 1) level))
903 "\\}\\) \\(.*\\)$") nil t)
904 (match-beginning 0)
905 (point-max))))
906 (goto-char (point-min))
907 (setq output
908 (append output
909 (list
910 (list
911 `(pos . ,pos)
912 `(level . ,nlevel)
913 `(occur . ,cnt)
914 `(heading . ,heading)
915 `(content . ,(org-export-latex-parse-content))
916 `(subcontent . ,(org-export-latex-parse-subcontent
917 level odd)))))))
918 (widen)))
919 (list output))))
921 (defun org-export-latex-parse-content ()
922 "Extract the content of a section."
923 (let ((beg (point))
924 (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t)
925 (progn (beginning-of-line) (point))
926 (point-max))))
927 (buffer-substring beg end)))
929 (defun org-export-latex-parse-subcontent (level odd)
930 "Extract the subcontent of a section at LEVEL.
931 If ODD Is non-nil, assume subcontent only contains odd sections."
932 (if (not (org-re-search-forward-unprotected
933 (concat "^\\(\\(?:\\*\\)\\{"
934 (number-to-string (+ (if odd 4 2) level))
935 "\\}\\) \\(.*\\)$")
936 nil t))
937 nil ; subcontent is nil
938 (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
940 ;;; Rendering functions:
941 (defun org-export-latex-global (content)
942 "Export CONTENT to LaTeX.
943 CONTENT is an element of the list produced by
944 `org-export-latex-parse-global'."
945 (if (eq (car content) 'subcontent)
946 (mapc 'org-export-latex-sub (cdr content))
947 (org-export-latex-sub (car content))))
949 (defun org-export-latex-sub (subcontent)
950 "Export the list SUBCONTENT to LaTeX.
951 SUBCONTENT is an alist containing information about the headline
952 and its content."
953 (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
954 (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
956 (defun org-export-latex-subcontent (subcontent num)
957 "Export each cell of SUBCONTENT to LaTeX.
958 If NUM, export sections as numerical sections."
959 (let* ((heading (cdr (assoc 'heading subcontent)))
960 (level (- (cdr (assoc 'level subcontent))
961 org-export-latex-add-level))
962 (occur (number-to-string (cdr (assoc 'occur subcontent))))
963 (content (cdr (assoc 'content subcontent)))
964 (subcontent (cadr (assoc 'subcontent subcontent)))
965 (label (org-get-text-property-any 0 'target heading))
966 (label-list (cons label (cdr (assoc label
967 org-export-target-aliases))))
968 (sectioning org-export-latex-sectioning)
969 (depth org-export-latex-sectioning-depth)
970 main-heading sub-heading)
971 (when (symbolp (car sectioning))
972 (setq sectioning (funcall (car sectioning) level heading))
973 (when sectioning
974 (setq heading (car sectioning)
975 sectioning (cdr sectioning)
976 ;; target property migh have changed...
977 label (org-get-text-property-any 0 'target heading)
978 label-list (cons label (cdr (assoc label
979 org-export-target-aliases)))))
980 (if sectioning (setq sectioning (make-list 10 sectioning)))
981 (setq depth (if sectioning 10000 0)))
982 (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading)
983 (setq main-heading (substring heading 0 (match-beginning 0))
984 sub-heading (substring heading (match-end 0))))
985 (setq heading (org-export-latex-fontify-headline heading)
986 sub-heading (and sub-heading
987 (org-export-latex-fontify-headline sub-heading))
988 main-heading (and main-heading
989 (org-export-latex-fontify-headline main-heading)))
990 (cond
991 ;; Normal conversion
992 ((<= level depth)
993 (let* ((sec (nth (1- level) sectioning))
994 start end)
995 (if (consp (cdr sec))
996 (setq start (nth (if num 0 2) sec)
997 end (nth (if num 1 3) sec))
998 (setq start (if num (car sec) (cdr sec))))
999 (insert (format start (if main-heading main-heading heading)
1000 (or sub-heading "")))
1001 (insert "\n")
1002 (when label
1003 (insert (mapconcat (lambda (l) (format "\\label{%s}" l))
1004 label-list "\n") "\n"))
1005 (insert (org-export-latex-content content))
1006 (cond ((stringp subcontent) (insert subcontent))
1007 ((listp subcontent)
1008 (while (org-looking-back "\n\n") (backward-delete-char 1))
1009 (org-export-latex-sub subcontent)))
1010 (when (and end (string-match "[^ \t]" end))
1011 (let ((hook (org-get-text-property-any 0 'org-insert-hook end)))
1012 (and (functionp hook) (funcall hook)))
1013 (insert end "\n"))))
1014 ;; At a level under the hl option: we can drop this subsection
1015 ((> level depth)
1016 (cond ((eq org-export-latex-low-levels 'description)
1017 (if (string-match "% ends low level$"
1018 (buffer-substring (point-at-bol 0) (point)))
1019 (delete-region (point-at-bol 0) (point))
1020 (insert "\\begin{description}\n"))
1021 (insert (format "\n\\item[%s]%s~\n"
1022 heading
1023 (if label (format "\\label{%s}" label) "")))
1024 (insert (org-export-latex-content content))
1025 (cond ((stringp subcontent) (insert subcontent))
1026 ((listp subcontent) (org-export-latex-sub subcontent)))
1027 (insert "\\end{description} % ends low level\n"))
1028 ((memq org-export-latex-low-levels '(itemize enumerate))
1029 (if (string-match "% ends low level$"
1030 (buffer-substring (point-at-bol 0) (point)))
1031 (delete-region (point-at-bol 0) (point))
1032 (insert (format "\\begin{%s}\n"
1033 (symbol-name org-export-latex-low-levels))))
1034 (insert (format "\n\\item %s\\\\\n%s%%"
1035 heading
1036 (if label (format "\\label{%s}" label) "")))
1037 (insert (org-export-latex-content content))
1038 (cond ((stringp subcontent) (insert subcontent))
1039 ((listp subcontent) (org-export-latex-sub subcontent)))
1040 (insert (format "\\end{%s} %% ends low level\n"
1041 (symbol-name org-export-latex-low-levels))))
1043 ((listp org-export-latex-low-levels)
1044 (if (string-match "% ends low level$"
1045 (buffer-substring (point-at-bol 0) (point)))
1046 (delete-region (point-at-bol 0) (point))
1047 (insert (car org-export-latex-low-levels) "\n"))
1048 (insert (format (nth 2 org-export-latex-low-levels)
1049 heading
1050 (if label (format "\\label{%s}" label) "")))
1051 (insert (org-export-latex-content content))
1052 (cond ((stringp subcontent) (insert subcontent))
1053 ((listp subcontent) (org-export-latex-sub subcontent)))
1054 (insert (nth 1 org-export-latex-low-levels)
1055 " %% ends low level\n"))
1057 ((stringp org-export-latex-low-levels)
1058 (insert (format org-export-latex-low-levels heading) "\n")
1059 (when label (insert (format "\\label{%s}\n" label)))
1060 (insert (org-export-latex-content content))
1061 (cond ((stringp subcontent) (insert subcontent))
1062 ((listp subcontent) (org-export-latex-sub subcontent)))))))))
1064 ;;; Exporting internals:
1065 (defun org-export-latex-set-initial-vars (ext-plist level)
1066 "Store org local variables required for LaTeX export.
1067 EXT-PLIST is an optional additional plist.
1068 LEVEL indicates the default depth for export."
1069 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
1070 org-export-latex-done-keywords org-done-keywords
1071 org-export-latex-not-done-keywords org-not-done-keywords
1072 org-export-latex-complex-heading-re org-complex-heading-regexp
1073 org-export-latex-display-custom-times org-display-custom-times
1074 org-export-latex-all-targets-re
1075 (org-make-target-link-regexp (org-all-targets))
1076 org-export-latex-options-plist
1077 (org-combine-plists (org-default-export-plist) ext-plist
1078 (org-infile-export-plist))
1079 org-export-latex-class
1080 (or (and (org-region-active-p)
1081 (save-excursion
1082 (goto-char (region-beginning))
1083 (and (looking-at org-complex-heading-regexp)
1084 (org-entry-get nil "LaTeX_CLASS" 'selective))))
1085 (save-excursion
1086 (save-restriction
1087 (widen)
1088 (goto-char (point-min))
1089 (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
1090 (match-string 1))))
1091 (plist-get org-export-latex-options-plist :latex-class)
1092 org-export-latex-default-class)
1093 org-export-latex-class-options
1094 (or (and (org-region-active-p)
1095 (save-excursion
1096 (goto-char (region-beginning))
1097 (and (looking-at org-complex-heading-regexp)
1098 (org-entry-get nil "LaTeX_CLASS_OPTIONS" 'selective))))
1099 (save-excursion
1100 (save-restriction
1101 (widen)
1102 (goto-char (point-min))
1103 (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t)
1104 (match-string 1))))
1105 (plist-get org-export-latex-options-plist :latex-class-options))
1106 org-export-latex-class
1107 (or (car (assoc org-export-latex-class org-export-latex-classes))
1108 (error "No definition for class `%s' in `org-export-latex-classes'"
1109 org-export-latex-class))
1110 org-export-latex-header
1111 (cadr (assoc org-export-latex-class org-export-latex-classes))
1112 org-export-latex-sectioning
1113 (cddr (assoc org-export-latex-class org-export-latex-classes))
1114 org-export-latex-sectioning-depth
1115 (or level
1116 (let ((hl-levels
1117 (plist-get org-export-latex-options-plist :headline-levels))
1118 (sec-depth (length org-export-latex-sectioning)))
1119 (if (> hl-levels sec-depth) sec-depth hl-levels))))
1120 (when (and org-export-latex-class-options
1121 (string-match "\\S-" org-export-latex-class-options)
1122 (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?"
1123 org-export-latex-header))
1124 (setq org-export-latex-header
1125 (concat (substring org-export-latex-header 0 (match-end 1))
1126 org-export-latex-class-options
1127 (substring org-export-latex-header (match-end 0))))))
1129 (defvar org-export-latex-format-toc-function
1130 'org-export-latex-format-toc-default
1131 "The function formatting returning the string to createthe table of contents.
1132 The function mus take one parameter, the depth of the table of contents.")
1134 (defun org-export-latex-make-header (title opt-plist)
1135 "Make the LaTeX header and return it as a string.
1136 TITLE is the current title from the buffer or region.
1137 OPT-PLIST is the options plist for current buffer."
1138 (let ((toc (plist-get opt-plist :table-of-contents))
1139 (author (org-export-apply-macros-in-string
1140 (plist-get opt-plist :author))))
1141 (concat
1142 (if (plist-get opt-plist :time-stamp-file)
1143 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1144 ;; insert LaTeX custom header and packages from the list
1145 (org-splice-latex-header
1146 (org-export-apply-macros-in-string org-export-latex-header)
1147 org-export-latex-default-packages-alist
1148 org-export-latex-packages-alist
1149 (org-export-apply-macros-in-string
1150 (plist-get opt-plist :latex-header-extra)))
1151 ;; append another special variable
1152 (org-export-apply-macros-in-string org-export-latex-append-header)
1153 ;; define align if not yet defined
1154 "\n\\providecommand{\\alert}[1]{\\textbf{#1}}"
1155 ;; insert the title
1156 (format
1157 "\n\n\\title{%s}\n"
1158 ;; convert the title
1159 (org-export-latex-content
1160 title '(lists tables fixed-width keywords)))
1161 ;; insert author info
1162 (if (plist-get opt-plist :author-info)
1163 (format "\\author{%s}\n"
1164 (org-export-latex-fontify-headline (or author user-full-name)))
1165 (format "%%\\author{%s}\n"
1166 (org-export-latex-fontify-headline (or author user-full-name))))
1167 ;; insert the date
1168 (format "\\date{%s}\n"
1169 (format-time-string
1170 (or (plist-get opt-plist :date)
1171 org-export-latex-date-format)))
1172 ;; beginning of the document
1173 "\n\\begin{document}\n\n"
1174 ;; insert the title command
1175 (when (string-match "\\S-" title)
1176 (if (string-match "%s" org-export-latex-title-command)
1177 (format org-export-latex-title-command title)
1178 org-export-latex-title-command))
1179 "\n\n"
1180 ;; table of contents
1181 (when (and org-export-with-toc
1182 (plist-get opt-plist :section-numbers))
1183 (funcall org-export-latex-format-toc-function
1184 (cond ((numberp toc)
1185 (min toc (plist-get opt-plist :headline-levels)))
1186 (toc (plist-get opt-plist :headline-levels))))))))
1188 (defun org-export-latex-format-toc-default (depth)
1189 (when depth
1190 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1191 depth)))
1193 (defun org-export-latex-first-lines (opt-plist &optional beg end)
1194 "Export the first lines before first headline.
1195 If BEG is non-nil, it is the beginning of the region.
1196 If END is non-nil, it is the end of the region."
1197 (save-excursion
1198 (goto-char (or beg (point-min)))
1199 (let* ((pt (point))
1200 (end (if (re-search-forward "^\\*+ " end t)
1201 (goto-char (match-beginning 0))
1202 (goto-char (or end (point-max))))))
1203 (prog1
1204 (org-export-latex-content
1205 (org-export-preprocess-string
1206 (buffer-substring pt end)
1207 :for-LaTeX t
1208 :emph-multiline t
1209 :add-text nil
1210 :comments nil
1211 :skip-before-1st-heading nil
1212 :LaTeX-fragments nil
1213 :timestamps (plist-get opt-plist :timestamps)
1214 :footnotes (plist-get opt-plist :footnotes)))
1215 (org-unmodified
1216 (let ((inhibit-read-only t))
1217 (add-text-properties pt (max pt (1- end))
1218 '(:org-license-to-kill t))))))))
1220 (defvar org-export-latex-header-defs nil
1221 "The header definitions that might be used in the LaTeX body.")
1222 (defvar org-export-latex-header-defs-re nil
1223 "The header definitions that might be used in the LaTeX body.")
1225 (defun org-export-latex-content (content &optional exclude-list)
1226 "Convert CONTENT string to LaTeX.
1227 Don't perform conversions that are in EXCLUDE-LIST. Recognized
1228 conversion types are: quotation-marks, emphasis, sub-superscript,
1229 links, keywords, lists, tables, fixed-width"
1230 (with-temp-buffer
1231 (insert content)
1232 (unless (memq 'timestamps exclude-list)
1233 (org-export-latex-time-stamps))
1234 (unless (memq 'quotation-marks exclude-list)
1235 (org-export-latex-quotation-marks))
1236 (unless (memq 'emphasis exclude-list)
1237 (when (plist-get org-export-latex-options-plist :emphasize)
1238 (org-export-latex-fontify)))
1239 (unless (memq 'sub-superscript exclude-list)
1240 (org-export-latex-special-chars
1241 (plist-get org-export-latex-options-plist :sub-superscript)))
1242 (unless (memq 'links exclude-list)
1243 (org-export-latex-links))
1244 (unless (memq 'keywords exclude-list)
1245 (org-export-latex-keywords))
1246 (unless (memq 'lists exclude-list)
1247 (org-export-latex-lists))
1248 (unless (memq 'tables exclude-list)
1249 (org-export-latex-tables
1250 (plist-get org-export-latex-options-plist :tables)))
1251 (unless (memq 'fixed-width exclude-list)
1252 (org-export-latex-fixed-width
1253 (plist-get org-export-latex-options-plist :fixed-width)))
1254 ;; return string
1255 (buffer-substring (point-min) (point-max))))
1257 (defun org-export-latex-protect-string (s)
1258 "Add the org-protected property to string S."
1259 (add-text-properties 0 (length s) '(org-protected t) s) s)
1261 (defun org-export-latex-protect-char-in-string (char-list string)
1262 "Add org-protected text-property to char from CHAR-LIST in STRING."
1263 (with-temp-buffer
1264 (save-match-data
1265 (insert string)
1266 (goto-char (point-min))
1267 (while (re-search-forward (regexp-opt char-list) nil t)
1268 (add-text-properties (match-beginning 0)
1269 (match-end 0) '(org-protected t)))
1270 (buffer-string))))
1272 (defun org-export-latex-keywords-maybe (&optional remove-list)
1273 "Maybe remove keywords depending on rules in REMOVE-LIST."
1274 (goto-char (point-min))
1275 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
1276 (case-fold-search nil)
1277 (todo-markup org-export-latex-todo-keyword-markup)
1278 fmt)
1279 ;; convert TODO keywords
1280 (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
1281 (if (plist-get remove-list :todo)
1282 (replace-match "")
1283 (setq fmt (cond
1284 ((stringp todo-markup) todo-markup)
1285 ((and (consp todo-markup) (stringp (car todo-markup)))
1286 (if (member (match-string 1) org-export-latex-done-keywords)
1287 (cdr todo-markup) (car todo-markup)))
1288 (t (cdr (or (assoc (match-string 1) todo-markup)
1289 (car todo-markup))))))
1290 (replace-match (format fmt (match-string 1)) t t)))
1291 ;; convert priority string
1292 (when (re-search-forward "\\[\\\\#.\\]" nil t)
1293 (if (plist-get remove-list :priority)
1294 (replace-match "")
1295 (replace-match (format "\\textbf{%s}" (match-string 0)) t t)))
1296 ;; convert tags
1297 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t)
1298 (if (or (not org-export-with-tags)
1299 (plist-get remove-list :tags))
1300 (replace-match "")
1301 (replace-match
1302 (org-export-latex-protect-string
1303 (format "\\textbf{%s}"
1304 (save-match-data
1305 (replace-regexp-in-string
1306 "_" "\\\\_" (match-string 0)))))
1307 t t)))))
1309 (defun org-export-latex-fontify-headline (string)
1310 "Fontify special words in STRING."
1311 (with-temp-buffer
1312 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
1313 ;; the beginning of the buffer - inserting "\n" is safe here though.
1314 (insert "\n" string)
1315 (goto-char (point-min))
1316 (let ((re (concat "\\\\\\([a-zA-Z]+\\)"
1317 "\\(?:<[^<>\n]*>\\)*"
1318 "\\(?:\\[[^][\n]*?\\]\\)*"
1319 "\\(?:<[^<>\n]*>\\)*"
1320 "\\("
1321 (org-create-multibrace-regexp "{" "}" 3)
1322 "\\)\\{1,3\\}")))
1323 (while (re-search-forward re nil t)
1324 (unless (or
1325 ;; check for comment line
1326 (save-excursion (goto-char (match-beginning 0))
1327 (equal (char-after (point-at-bol)) ?#))
1328 ;; Check if this is a defined entity, so that is may need conversion
1329 (org-entity-get (match-string 1)))
1330 (add-text-properties (match-beginning 0) (match-end 0)
1331 '(org-protected t)))))
1332 (when (plist-get org-export-latex-options-plist :emphasize)
1333 (org-export-latex-fontify))
1334 (org-export-latex-keywords-maybe)
1335 (org-export-latex-special-chars
1336 (plist-get org-export-latex-options-plist :sub-superscript))
1337 (org-export-latex-links)
1338 (org-trim (buffer-string))))
1340 (defun org-export-latex-time-stamps ()
1341 "Format time stamps."
1342 (goto-char (point-min))
1343 (let ((org-display-custom-times org-export-latex-display-custom-times))
1344 (while (re-search-forward org-ts-regexp-both nil t)
1345 (org-if-unprotected-at (1- (point))
1346 (replace-match
1347 (org-export-latex-protect-string
1348 (format org-export-latex-timestamp-markup
1349 (substring (org-translate-time (match-string 0)) 1 -1)))
1350 t t)))))
1352 (defun org-export-latex-quotation-marks ()
1353 "Export quotation marks depending on language conventions."
1354 (let* ((lang (plist-get org-export-latex-options-plist :language))
1355 (quote-rpl (if (equal lang "fr")
1356 '(("\\(\\s-\\)\"" "«~")
1357 ("\\(\\S-\\)\"" "~»")
1358 ("\\(\\s-\\)'" "`"))
1359 '(("\\(\\s-\\|[[(]\\)\"" "``")
1360 ("\\(\\S-\\)\"" "''")
1361 ("\\(\\s-\\|(\\)'" "`")))))
1362 (mapc (lambda(l) (goto-char (point-min))
1363 (while (re-search-forward (car l) nil t)
1364 (let ((rpl (concat (match-string 1)
1365 (org-export-latex-protect-string
1366 (copy-sequence (cadr l))))))
1367 (org-if-unprotected-1
1368 (replace-match rpl t t))))) quote-rpl)))
1370 (defun org-export-latex-special-chars (sub-superscript)
1371 "Export special characters to LaTeX.
1372 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
1373 See the `org-export-latex.el' code for a complete conversion table."
1374 (goto-char (point-min))
1375 (mapc (lambda(c)
1376 (goto-char (point-min))
1377 (while (re-search-forward c nil t)
1378 ;; Put the point where to check for org-protected
1379 (unless (get-text-property (match-beginning 2) 'org-protected)
1380 (cond ((member (match-string 2) '("\\$" "$"))
1381 (if (equal (match-string 2) "\\$")
1383 (replace-match "\\$" t t)))
1384 ((member (match-string 2) '("&" "%" "#"))
1385 (if (equal (match-string 1) "\\")
1386 (replace-match (match-string 2) t t)
1387 (replace-match (concat (match-string 1) "\\"
1388 (match-string 2)) t t)
1389 (backward-char 1)))
1390 ((equal (match-string 2) "...")
1391 (replace-match
1392 (concat (match-string 1)
1393 (org-export-latex-protect-string "\\ldots{}")) t t))
1394 ((equal (match-string 2) "~")
1395 (cond ((equal (match-string 1) "\\") nil)
1396 ((eq 'org-link (get-text-property 0 'face (match-string 2)))
1397 (replace-match (concat (match-string 1) "\\~") t t))
1398 (t (replace-match
1399 (org-export-latex-protect-string
1400 (concat (match-string 1) "\\~{}")) t t))))
1401 ((member (match-string 2) '("{" "}"))
1402 (unless (save-match-data (org-inside-latex-math-p))
1403 (if (equal (match-string 1) "\\")
1404 (replace-match (match-string 2) t t)
1405 (replace-match (concat (match-string 1) "\\"
1406 (match-string 2)) t t)))))
1407 (unless (save-match-data (org-inside-latex-math-p))
1408 (cond ((equal (match-string 2) "\\")
1409 (replace-match (or (save-match-data
1410 (org-export-latex-treat-backslash-char
1411 (match-string 1)
1412 (or (match-string 3) "")))
1413 "") t t)
1414 (when (and (get-text-property (1- (point)) 'org-entity)
1415 (looking-at "{}"))
1416 ;; OK, this was an entity replacement, and the user
1417 ;; had terminated the entity with {}. Make sure
1418 ;; {} is protected as well, and remove the extra {}
1419 ;; inserted by the conversion.
1420 (put-text-property (point) (+ 2 (point)) 'org-protected t)
1421 (if (save-excursion (goto-char (max (- (point) 2) (point-min)))
1422 (looking-at "{}"))
1423 (replace-match ""))
1424 (forward-char 2))
1425 (backward-char 1))
1426 ((member (match-string 2) '("_" "^"))
1427 (replace-match (or (save-match-data
1428 (org-export-latex-treat-sub-super-char
1429 sub-superscript
1430 (match-string 2)
1431 (match-string 1)
1432 (match-string 3))) "") t t)
1433 (backward-char 1)))))))
1434 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
1435 "\\(\\(\\\\?\\$\\)\\)"
1436 "\\([a-zA-Z0-9()]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-zA-Z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-zA-Z0-9]+}\\|([a-zA-Z0-9]+)\\)"
1437 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|\\([&#%{}\"]\\|[a-zA-Z][a-zA-Z0-9]*\\)\\)"
1438 "\\(.\\|^\\)\\(&\\)"
1439 "\\(.\\|^\\)\\(#\\)"
1440 "\\(.\\|^\\)\\(%\\)"
1441 "\\(.\\|^\\)\\({\\)"
1442 "\\(.\\|^\\)\\(}\\)"
1443 "\\(.\\|^\\)\\(~\\)"
1444 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
1445 ;; (?\< . "\\textless{}")
1446 ;; (?\> . "\\textgreater{}")
1449 (defun org-inside-latex-math-p ()
1450 (get-text-property (point) 'org-latex-math))
1452 (defun org-export-latex-treat-sub-super-char
1453 (subsup char string-before string-after)
1454 "Convert the \"_\" and \"^\" characters to LaTeX.
1455 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
1456 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
1457 (cond ((equal string-before "\\")
1458 (concat string-before char string-after))
1459 ((and (string-match "\\S-+" string-after))
1460 ;; this is part of a math formula
1461 (cond ((eq 'org-link (get-text-property 0 'face char))
1462 (concat string-before "\\" char string-after))
1463 ((save-match-data (org-inside-latex-math-p))
1464 (if subsup
1465 (cond ((eq 1 (length string-after))
1466 (concat string-before char string-after))
1467 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
1468 (format "%s%s{%s}" string-before char
1469 (match-string 1 string-after))))))
1470 ((and (> (length string-after) 1)
1471 (or (eq subsup t)
1472 (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
1473 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
1474 (org-export-latex-protect-string
1475 (format "%s$%s{%s}$" string-before char
1476 (if (and (> (match-end 1) (1+ (match-beginning 1)))
1477 (not (equal (substring string-after 0 2) "{\\")))
1478 (concat "\\mathrm{" (match-string 1 string-after) "}")
1479 (match-string 1 string-after)))))
1480 ((eq subsup t) (concat string-before "$" char string-after "$"))
1481 (t (org-export-latex-protect-string
1482 (concat string-before "\\" char "{}" string-after)))))
1483 (t (org-export-latex-protect-string
1484 (concat string-before "\\" char "{}" string-after)))))
1486 (defun org-export-latex-treat-backslash-char (string-before string-after)
1487 "Convert the \"$\" special character to LaTeX.
1488 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
1489 (let ((ass (org-entity-get string-after)))
1490 (cond
1491 (ass (org-add-props
1492 (if (nth 2 ass)
1493 (concat string-before
1494 (org-export-latex-protect-string
1495 (concat "$" (nth 1 ass) "$")))
1496 (concat string-before (org-export-latex-protect-string
1497 (nth 1 ass))))
1498 nil 'org-entity t))
1499 ((and (not (string-match "^[ \n\t]" string-after))
1500 (not (string-match "[ \t]\\'\\|^" string-before)))
1501 ;; backslash is inside a word
1502 (concat string-before
1503 (org-export-latex-protect-string
1504 (concat "\\textbackslash{}" string-after))))
1505 ((not (or (equal string-after "")
1506 (string-match "^[ \t\n]" string-after)))
1507 ;; backslash might escape a character (like \#) or a user TeX
1508 ;; macro (like \setcounter)
1509 (concat string-before
1510 (org-export-latex-protect-string (concat "\\" string-after))))
1511 ((and (string-match "^[ \t\n]" string-after)
1512 (string-match "[ \t\n]\\'" string-before))
1513 ;; backslash is alone, convert it to $\backslash$
1514 (org-export-latex-protect-string
1515 (concat string-before "\\textbackslash{}" string-after)))
1516 (t (org-export-latex-protect-string
1517 (concat string-before "\\textbackslash{}" string-after))))))
1519 (defun org-export-latex-keywords ()
1520 "Convert special keywords to LaTeX."
1521 (goto-char (point-min))
1522 (while (re-search-forward org-export-latex-special-keyword-regexp nil t)
1523 (replace-match (format org-export-latex-timestamp-keyword-markup
1524 (match-string 0)) t t)
1525 (save-excursion
1526 (beginning-of-line 1)
1527 (unless (looking-at ".*\n[ \t]*\n")
1528 (end-of-line 1)
1529 (insert "\n")))))
1531 (defun org-export-latex-fixed-width (opt)
1532 "When OPT is non-nil convert fixed-width sections to LaTeX."
1533 (goto-char (point-min))
1534 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t)
1535 (if opt
1536 (progn (goto-char (match-beginning 0))
1537 (insert "\\begin{verbatim}\n")
1538 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1539 (replace-match (concat (match-string 1)
1540 (match-string 2)) t t)
1541 (forward-line))
1542 (insert "\\end{verbatim}\n\n"))
1543 (progn (goto-char (match-beginning 0))
1544 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1545 (replace-match (concat "%" (match-string 1)
1546 (match-string 2)) t t)
1547 (forward-line))))))
1550 (defvar org-table-last-alignment) ; defined in org-table.el
1551 (defvar org-table-last-column-widths) ; defined in org-table.el
1552 (declare-function orgtbl-to-latex "org-table" (table params) t)
1553 (defun org-export-latex-tables (insert)
1554 "Convert tables to LaTeX and INSERT it."
1555 ;; First, get the table.el tables
1556 (goto-char (point-min))
1557 (while (re-search-forward "^[ \t]*\\(\\+-[-+]*\\+\\)[ \t]*\n[ \t]*|" nil t)
1558 (org-if-unprotected
1559 (require 'table)
1560 (org-export-latex-convert-table.el-table)))
1562 ;; And now the Org-mode tables
1563 (goto-char (point-min))
1564 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
1565 (org-if-unprotected-at (1- (point))
1566 (org-table-align)
1567 (let* ((beg (org-table-begin))
1568 (end (org-table-end))
1569 (raw-table (buffer-substring beg end))
1570 (org-table-last-alignment (copy-sequence org-table-last-alignment))
1571 (org-table-last-column-widths (copy-sequence
1572 org-table-last-column-widths))
1573 fnum fields line lines olines gr colgropen line-fmt align
1574 caption label attr floatp longtblp)
1575 (if org-export-latex-tables-verbatim
1576 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1577 "\\end{verbatim}\n")))
1578 (apply 'delete-region (list beg end))
1579 (insert (org-export-latex-protect-string tbl)))
1580 (progn
1581 (setq caption (org-find-text-property-in-string
1582 'org-caption raw-table)
1583 attr (org-find-text-property-in-string
1584 'org-attributes raw-table)
1585 label (org-find-text-property-in-string
1586 'org-label raw-table)
1587 longtblp (and attr (stringp attr)
1588 (string-match "\\<longtable\\>" attr))
1589 align (and attr (stringp attr)
1590 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
1591 (match-string 1 attr))
1592 floatp (or caption label))
1593 (setq caption (and caption (org-export-latex-fontify-headline caption)))
1594 (setq lines (org-split-string raw-table "\n"))
1595 (apply 'delete-region (list beg end))
1596 (when org-export-table-remove-special-lines
1597 (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
1598 (when org-table-clean-did-remove-column
1599 (pop org-table-last-alignment)
1600 (pop org-table-last-column-widths))
1601 ;; make a formatting string to reflect alignment
1602 (setq olines lines)
1603 (while (and (not line-fmt) (setq line (pop olines)))
1604 (unless (string-match "^[ \t]*|-" line)
1605 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1606 (setq fnum (make-vector (length fields) 0))
1607 (setq line-fmt
1608 (mapconcat
1609 (lambda (x)
1610 (setq gr (pop org-table-colgroup-info))
1611 (format "%s%%s%s"
1612 (cond ((eq gr :start)
1613 (prog1 (if colgropen "|" "|")
1614 (setq colgropen t)))
1615 ((eq gr :startend)
1616 (prog1 (if colgropen "|" "|")
1617 (setq colgropen nil)))
1618 (t ""))
1619 (if (memq gr '(:end :startend))
1620 (progn (setq colgropen nil) "|")
1621 "")))
1622 fnum ""))))
1623 ;; fix double || in line-fmt
1624 (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
1625 ;; maybe remove the first and last "|"
1626 (when (and (not org-export-latex-tables-column-borders)
1627 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
1628 (setq line-fmt (match-string 2 line-fmt)))
1629 ;; format alignment
1630 (unless align
1631 (setq align (apply 'format
1632 (cons line-fmt
1633 (mapcar (lambda (x) (if x "r" "l"))
1634 org-table-last-alignment)))))
1635 ;; prepare the table to send to orgtbl-to-latex
1636 (setq lines
1637 (mapcar
1638 (lambda(elem)
1639 (or (and (string-match "[ \t]*|-+" elem) 'hline)
1640 (org-split-string (org-trim elem) "|")))
1641 lines))
1642 (when insert
1643 (insert (org-export-latex-protect-string
1644 (concat
1645 (if longtblp
1646 (concat "\\begin{longtable}{" align "}\n")
1647 (if floatp "\\begin{table}[htb]\n"))
1648 (if floatp
1649 (format
1650 "\\caption{%s%s}"
1651 (if label (concat "\\\label{" label "}") "")
1652 (or caption "")))
1653 (if (and longtblp caption) "\\\\\n" "\n")
1654 (if (and org-export-latex-tables-centered (not longtblp))
1655 "\\begin{center}\n")
1656 (if (not longtblp) (concat "\\begin{tabular}{" align "}\n"))
1657 (orgtbl-to-latex
1658 lines
1659 `(:tstart nil :tend nil
1660 :hlend ,(if longtblp
1661 (format "\\\\
1662 \\hline
1663 \\endhead
1664 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
1665 \\endfoot
1666 \\endlastfoot" (length org-table-last-alignment))
1667 nil)))
1668 (if (not longtblp) (concat "\n\\end{tabular}"))
1669 (if longtblp "\n" (if org-export-latex-tables-centered
1670 "\n\\end{center}\n" "\n"))
1671 (if longtblp
1672 "\\end{longtable}"
1673 (if floatp "\\end{table}"))))
1674 "\n\n"))))))))
1676 (defun org-export-latex-convert-table.el-table ()
1677 "Replace table.el table at point with LaTeX code."
1678 (let (tbl caption label line floatp attr align rmlines)
1679 (setq line (buffer-substring (point-at-bol) (point-at-eol))
1680 label (org-get-text-property-any 0 'org-label line)
1681 caption (org-get-text-property-any 0 'org-caption line)
1682 attr (org-get-text-property-any 0 'org-attributes line)
1683 align (and attr (stringp attr)
1684 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
1685 (match-string 1 attr))
1686 rmlines (and attr (stringp attr)
1687 (string-match "\\<rmlines\\>" attr))
1688 floatp (or label caption))
1689 (and (get-buffer "*org-export-table*")
1690 (kill-buffer (get-buffer "*org-export-table*")))
1691 (table-generate-source 'latex "*org-export-table*" "caption")
1692 (setq tbl (with-current-buffer "*org-export-table*"
1693 (buffer-string)))
1694 (while (string-match "^%.*\n" tbl)
1695 (setq tbl (replace-match "" t t tbl)))
1696 ;; fix the hlines
1697 (when rmlines
1698 (let ((n 0) lines)
1699 (setq lines (mapcar (lambda (x)
1700 (if (string-match "^\\\\hline$" x)
1701 (progn
1702 (setq n (1+ n))
1703 (if (= n 2) x nil))
1705 (org-split-string tbl "\n")))
1706 (setq tbl (mapconcat 'identity (delq nil lines) "\n"))))
1707 (when (and align (string-match "\\\\begin{tabular}{.*}" tbl))
1708 (setq tbl (replace-match (concat "\\begin{tabular}{" align "}")
1709 t t tbl)))
1710 (and (get-buffer "*org-export-table*")
1711 (kill-buffer (get-buffer "*org-export-table*")))
1712 (beginning-of-line 0)
1713 (while (looking-at "[ \t]*\\(|\\|\\+-\\)")
1714 (delete-region (point) (1+ (point-at-eol))))
1715 (when org-export-latex-tables-centered
1716 (setq tbl (concat "\\begin{center}\n" tbl "\\end{center}")))
1717 (when floatp
1718 (setq tbl (concat "\\begin{table}\n"
1719 (format "\\caption{%s%s}\n"
1720 (if label (format "\\label{%s}" label) "")
1721 (or caption ""))
1723 "\n\\end{table}\n")))
1724 (insert (org-export-latex-protect-string tbl))))
1726 (defun org-export-latex-fontify ()
1727 "Convert fontification to LaTeX."
1728 (goto-char (point-min))
1729 (while (re-search-forward org-emph-re nil t)
1730 ;; The match goes one char after the *string*, except at the end of a line
1731 (let ((emph (assoc (match-string 3)
1732 org-export-latex-emphasis-alist))
1733 (beg (match-beginning 0))
1734 (end (match-end 0))
1735 rpl s)
1736 (unless emph
1737 (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
1738 (match-string 3)))
1739 (unless (or (and (get-text-property (- (point) 2) 'org-protected)
1740 (not (get-text-property
1741 (- (point) 2) 'org-verbatim-emph)))
1742 (save-excursion
1743 (goto-char (match-beginning 1))
1744 (save-match-data
1745 (and (org-at-table-p)
1746 (string-match
1747 "[|\n]" (buffer-substring beg end)))))
1748 (and (equal (match-string 3) "+")
1749 (save-match-data
1750 (string-match "\\`-+\\'" (match-string 4)))))
1751 (setq s (match-string 4))
1752 (setq rpl (concat (match-string 1)
1753 (org-export-latex-emph-format (cadr emph)
1754 (match-string 4))
1755 (match-string 5)))
1756 (if (caddr emph)
1757 (setq rpl (org-export-latex-protect-string rpl))
1758 (save-match-data
1759 (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl)
1760 (progn
1761 (add-text-properties (match-beginning 1) (match-end 1)
1762 '(org-protected t) rpl)
1763 (add-text-properties (match-beginning 3) (match-end 3)
1764 '(org-protected t) rpl)))))
1765 (replace-match rpl t t)))
1766 (backward-char)))
1768 (defvar org-export-latex-use-verb nil)
1769 (defun org-export-latex-emph-format (format string)
1770 "Format an emphasis string and handle the \\verb special case."
1771 (when (equal format "\\verb")
1772 (save-match-data
1773 (if org-export-latex-use-verb
1774 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1775 (catch 'exit
1776 (loop for i from 0 to (1- (length ll)) do
1777 (if (not (string-match (regexp-quote (substring ll i (1+ i)))
1778 string))
1779 (progn
1780 (setq format (concat "\\verb" (substring ll i (1+ i))
1781 "%s" (substring ll i (1+ i))))
1782 (throw 'exit nil))))))
1783 (let ((start 0)
1784 (trans '(("\\" . "\\textbackslash{}")
1785 ("~" . "\\textasciitilde{}")
1786 ("^" . "\\textasciicircum{}")))
1787 (rtn "") char)
1788 (while (string-match "[\\{}$%&_#~^]" string)
1789 (setq char (match-string 0 string))
1790 (if (> (match-beginning 0) 0)
1791 (setq rtn (concat rtn (substring string
1792 0 (match-beginning 0)))))
1793 (setq string (substring string (1+ (match-beginning 0))))
1794 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1795 rtn (concat rtn char)))
1796 (setq string (concat rtn string) format "\\texttt{%s}")))))
1797 (format format string))
1799 (defun org-export-latex-links ()
1800 ;; Make sure to use the LaTeX hyperref and graphicx package
1801 ;; or send some warnings.
1802 "Convert links to LaTeX."
1803 (goto-char (point-min))
1804 (while (re-search-forward org-bracket-link-analytic-regexp++ nil t)
1805 (org-if-unprotected-1
1806 (goto-char (match-beginning 0))
1807 (let* ((re-radio org-export-latex-all-targets-re)
1808 (remove (list (match-beginning 0) (match-end 0)))
1809 (raw-path (org-extract-attributes (match-string 3)))
1810 (full-raw-path (concat (match-string 1) raw-path))
1811 (desc (match-string 5))
1812 (type (or (match-string 2)
1813 (if (or (file-name-absolute-p raw-path)
1814 (string-match "^\\.\\.?/" raw-path))
1815 "file")))
1816 (coderefp (equal type "coderef"))
1817 (caption (org-find-text-property-in-string 'org-caption raw-path))
1818 (attr (or (org-find-text-property-in-string 'org-attributes raw-path)
1819 (plist-get org-export-latex-options-plist :latex-image-options)))
1820 (label (org-find-text-property-in-string 'org-label raw-path))
1821 imgp radiop
1822 ;; define the path of the link
1823 (path (cond
1824 ((member type '("coderef"))
1825 raw-path)
1826 ((member type '("http" "https" "ftp"))
1827 (concat type ":" raw-path))
1828 ((and re-radio (string-match re-radio raw-path))
1829 (setq radiop t))
1830 ((equal type "mailto")
1831 (concat type ":" raw-path))
1832 ((equal type "file")
1833 (if (and (org-file-image-p
1834 (expand-file-name
1835 raw-path)
1836 org-export-latex-inline-image-extensions)
1837 (or (get-text-property 0 'org-no-description
1838 raw-path)
1839 (equal desc full-raw-path)))
1840 (setq imgp t)
1841 (progn (when (string-match "\\(.+\\)::.+" raw-path)
1842 (setq raw-path (match-string 1 raw-path)))
1843 (if (file-exists-p raw-path)
1844 (concat type "://" (expand-file-name raw-path))
1845 (concat type "://" (org-export-directory
1846 :LaTeX org-export-latex-options-plist)
1847 raw-path))))))))
1848 ;; process with link inserting
1849 (apply 'delete-region remove)
1850 (setq caption (and caption (org-export-latex-fontify-headline caption)))
1851 (cond ((and imgp
1852 (plist-get org-export-latex-options-plist :inline-images))
1853 ;; OK, we need to inline an image
1854 (insert
1855 (org-export-latex-format-image raw-path caption label attr)))
1856 (coderefp
1857 (insert (format
1858 (org-export-get-coderef-format path desc)
1859 (cdr (assoc path org-export-code-refs)))))
1860 (radiop (insert (format "\\hyperref[%s]{%s}"
1861 (org-solidify-link-text raw-path) desc)))
1862 ((not type)
1863 (insert (format "\\hyperref[%s]{%s}"
1864 (org-remove-initial-hash
1865 (org-solidify-link-text raw-path))
1866 desc)))
1867 (path
1868 (when (org-at-table-p)
1869 ;; There is a strange problem when we have a link in a table,
1870 ;; ampersands then cause a problem. I think this must be
1871 ;; a LaTeX issue, but we here implement a work-around anyway.
1872 (setq path (org-export-latex-protect-amp path)
1873 desc (org-export-latex-protect-amp desc)))
1874 (insert (format org-export-latex-hyperref-format path desc)))
1875 (t (insert "\\texttt{" desc "}")))))))
1878 (defun org-export-latex-format-image (path caption label attr)
1879 "Format the image element, depending on user settings."
1880 (let (ind floatp wrapp placement figenv)
1881 (setq floatp (or caption label))
1882 (setq ind (org-get-text-property-any 0 'original-indentation path))
1883 (when (and attr (stringp attr))
1884 (if (string-match "[ \t]*\\<wrap\\>" attr)
1885 (setq wrapp t floatp nil attr (replace-match "" t t attr)))
1886 (if (string-match "[ \t]*\\<float\\>" attr)
1887 (setq wrapp nil floatp t attr (replace-match "" t t attr))))
1889 (setq placement
1890 (cond
1891 (wrapp "{l}{0.5\\textwidth}")
1892 (floatp "[htb]")
1893 (t "")))
1895 (when (and attr (stringp attr)
1896 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr))
1897 (setq placement (match-string 1 attr)
1898 attr (replace-match "" t t attr)))
1899 (setq attr (and attr (org-trim attr)))
1900 (when (or (not attr) (= (length attr) 0))
1901 (setq attr (cond (floatp "width=0.7\\textwidth")
1902 (wrapp "width=0.48\\textwidth")
1903 (t attr))))
1904 (setq figenv
1905 (cond
1906 (wrapp "\\begin{wrapfigure}%placement
1907 \\centering
1908 \\includegraphics[%attr]{%path}
1909 \\caption{%labelcmd%caption}
1910 \\end{wrapfigure}")
1911 (floatp "\\begin{figure}%placement
1912 \\centering
1913 \\includegraphics[%attr]{%path}
1914 \\caption{%labelcmd%caption}
1915 \\end{figure}")
1916 (t "\\includegraphics[%attr]{%path}")))
1919 (setq figenv (mapconcat 'identity (split-string figenv "\n")
1920 (save-excursion (beginning-of-line 1)
1921 (looking-at "[ \t]*")
1922 (concat "\n" (match-string 0)))))
1924 (if (and (not label) (not caption)
1925 (string-match "^\\\\caption{.*\n" figenv))
1926 (setq figenv (replace-match "" t t figenv)))
1927 (org-add-props
1928 (org-fill-template
1929 figenv
1930 (list (cons "path"
1931 (if (file-name-absolute-p path)
1932 (expand-file-name path)
1933 path))
1934 (cons "attr" attr)
1935 (cons "labelcmd" (if label (format "\\label{%s}"
1936 label)""))
1937 (cons "caption" (or caption ""))
1938 (cons "placement" (or placement ""))))
1939 nil 'original-indentation ind)))
1941 (defun org-export-latex-protect-amp (s)
1942 (while (string-match "\\([^\\\\]\\)\\(&\\)" s)
1943 (setq s (replace-match (concat (match-string 1 s) "\\" (match-string 2 s))
1944 t t s)))
1947 (defun org-remove-initial-hash (s)
1948 (if (string-match "\\`#" s)
1949 (substring s 1)
1951 (defvar org-latex-entities) ; defined below
1952 (defvar org-latex-entities-regexp) ; defined below
1953 (defvar org-latex-entities-exceptions) ; defined below
1955 (defun org-export-latex-preprocess (parameters)
1956 "Clean stuff in the LaTeX export."
1957 ;; Preserve line breaks
1958 (goto-char (point-min))
1959 (while (re-search-forward "\\\\\\\\" nil t)
1960 (add-text-properties (match-beginning 0) (match-end 0)
1961 '(org-protected t)))
1963 ;; Preserve latex environments
1964 (goto-char (point-min))
1965 (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t)
1966 (let* ((start (progn (beginning-of-line) (point)))
1967 (end (and (re-search-forward
1968 (concat "^[ \t]*\\\\end{"
1969 (regexp-quote (match-string 1))
1970 "}") nil t)
1971 (point-at-eol))))
1972 (if end
1973 (add-text-properties start end '(org-protected t))
1974 (goto-char (point-at-eol)))))
1976 ;; Preserve math snippets
1978 (let* ((matchers (plist-get org-format-latex-options :matchers))
1979 (re-list org-latex-regexps)
1980 beg end re e m n block off)
1981 ;; Check the different regular expressions
1982 (while (setq e (pop re-list))
1983 (setq m (car e) re (nth 1 e) n (nth 2 e)
1984 block (if (nth 3 e) "\n\n" ""))
1985 (setq off (if (member m '("$" "$1")) 1 0))
1986 (when (and (member m matchers) (not (equal m "begin")))
1987 (goto-char (point-min))
1988 (while (re-search-forward re nil t)
1989 (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
1990 (add-text-properties beg end '(org-protected t org-latex-math t))))))
1992 ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{}
1993 (goto-char (point-min))
1994 (let ((case-fold-search nil))
1995 (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t)
1996 (unless (eq (char-before (match-beginning 1)) ?\\)
1997 (org-if-unprotected-1
1998 (replace-match (org-export-latex-protect-string
1999 (concat "\\" (match-string 1)
2000 "{}")) t t)))))
2002 ;; Convert blockquotes
2003 (goto-char (point-min))
2004 (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
2005 (org-replace-match-keep-properties "\\begin{quote}" t t))
2006 (goto-char (point-min))
2007 (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
2008 (org-replace-match-keep-properties "\\end{quote}" t t))
2010 ;; Convert verse
2011 (goto-char (point-min))
2012 (while (search-forward "ORG-VERSE-START" nil t)
2013 (org-replace-match-keep-properties "\\begin{verse}" t t)
2014 (beginning-of-line 2)
2015 (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
2016 (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
2017 (goto-char (match-end 1))
2018 (org-replace-match-keep-properties
2019 (org-export-latex-protect-string
2020 (concat "\\hspace*{1cm}" (match-string 2))) t t)
2021 (beginning-of-line 1))
2022 (if (looking-at "[ \t]*$")
2023 (insert (org-export-latex-protect-string "\\vspace*{1em}"))
2024 (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
2025 (end-of-line 1)
2026 (insert "\\\\")))
2027 (beginning-of-line 2))
2028 (and (looking-at "[ \t]*ORG-VERSE-END.*")
2029 (org-replace-match-keep-properties "\\end{verse}" t t)))
2031 ;; Convert center
2032 (goto-char (point-min))
2033 (while (search-forward "ORG-CENTER-START" nil t)
2034 (org-replace-match-keep-properties "\\begin{center}" t t))
2035 (goto-char (point-min))
2036 (while (search-forward "ORG-CENTER-END" nil t)
2037 (org-replace-match-keep-properties "\\end{center}" t t))
2039 (run-hooks 'org-export-latex-after-blockquotes-hook)
2041 ;; Convert horizontal rules
2042 (goto-char (point-min))
2043 (while (re-search-forward "^----+.$" nil t)
2044 (org-if-unprotected
2045 (replace-match (org-export-latex-protect-string "\\hrule") t t)))
2047 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
2048 (goto-char (point-min))
2049 (let ((re (concat
2050 "\\\\\\([a-zA-Z]+\\)"
2051 "\\(?:<[^<>\n]*>\\)*"
2052 "\\(?:\\[[^][\n]*?\\]\\)*"
2053 "\\(?:<[^<>\n]*>\\)*"
2054 "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}")))
2055 (while (re-search-forward re nil t)
2056 (unless (or
2057 ;; check for comment line
2058 (save-excursion (goto-char (match-beginning 0))
2059 (equal (char-after (point-at-bol)) ?#))
2060 ;; Check if this is a defined entity, so that is may need conversion
2061 (org-entity-get (match-string 1))
2063 (add-text-properties (match-beginning 0) (match-end 0)
2064 '(org-protected t)))))
2066 ;; Protect LaTeX entities
2067 (goto-char (point-min))
2068 (let (a)
2069 (while (re-search-forward org-latex-entities-regexp nil t)
2070 (if (setq a (assoc (match-string 0) org-latex-entities-exceptions))
2071 (replace-match (org-add-props (nth 1 a) nil 'org-protected t)
2072 t t)
2073 (add-text-properties (match-beginning 0) (match-end 0)
2074 '(org-protected t)))))
2076 ;; Replace radio links
2077 (goto-char (point-min))
2078 (while (re-search-forward
2079 (concat "<<<?" org-export-latex-all-targets-re
2080 ">>>?\\((INVISIBLE)\\)?") nil t)
2081 (org-if-unprotected-at (+ (match-beginning 0) 2)
2082 (replace-match
2083 (concat
2084 (org-export-latex-protect-string
2085 (format "\\label{%s}" (save-match-data (org-solidify-link-text
2086 (match-string 1)))))
2087 (if (match-string 2) "" (match-string 1)))
2088 t t)))
2090 ;; Delete @<...> constructs
2091 ;; Thanks to Daniel Clemente for this regexp
2092 (goto-char (point-min))
2093 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
2094 (org-if-unprotected
2095 (replace-match "")))
2097 ;; When converting to LaTeX, replace footnotes
2098 ;; FIXME: don't protect footnotes from conversion
2099 (when (plist-get org-export-latex-options-plist :footnotes)
2100 (goto-char (point-min))
2101 (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t)
2102 (org-if-unprotected
2103 (when (and (save-match-data
2104 (save-excursion (beginning-of-line)
2105 (looking-at "[^:|#]")))
2106 (not (org-in-verbatim-emphasis)))
2107 (let ((foot-beg (match-beginning 0))
2108 (foot-end (match-end 0))
2109 (foot-prefix (match-string 0))
2110 footnote footnote-rpl)
2111 (save-excursion
2112 (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix))
2113 nil t))
2114 (replace-match (org-export-latex-protect-string
2115 (concat "$^{" (match-string 1) "}$")))
2116 (replace-match "")
2117 (let ((end (save-excursion
2118 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
2119 (match-beginning 0) (point-max)))))
2120 (setq footnote (concat (org-trim (buffer-substring (point) end))
2121 " ")) ; prevent last } being part of a link
2122 (delete-region (point) end))
2123 (goto-char foot-beg)
2124 (delete-region foot-beg foot-end)
2125 (unless (null footnote)
2126 (setq footnote-rpl (format "\\footnote{%s}" footnote))
2127 (add-text-properties 0 10 '(org-protected t) footnote-rpl)
2128 (add-text-properties (1- (length footnote-rpl))
2129 (length footnote-rpl)
2130 '(org-protected t) footnote-rpl)
2131 (if (org-on-heading-p)
2132 (setq footnote-rpl
2133 (concat (org-export-latex-protect-string "\\protect")
2134 footnote-rpl)))
2135 (insert footnote-rpl)))
2136 )))))
2138 ;; Remove footnote section tag for LaTeX
2139 (goto-char (point-min))
2140 (while (re-search-forward
2141 (concat "^" footnote-section-tag-regexp) nil t)
2142 (org-if-unprotected
2143 (replace-match "")))))
2145 (defun org-export-latex-fix-inputenc ()
2146 "Set the codingsystem in inputenc to what the buffer is."
2147 (let* ((cs buffer-file-coding-system)
2148 (opt (or (ignore-errors (latexenc-coding-system-to-inputenc cs))
2149 "utf8")))
2150 (when opt
2151 ;; Translate if that is requested
2152 (setq opt (or (cdr (assoc opt org-export-latex-inputenc-alist)) opt))
2153 ;; find the \usepackage statement and replace the option
2154 (goto-char (point-min))
2155 (while (re-search-forward "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
2156 nil t)
2157 (goto-char (match-beginning 1))
2158 (delete-region (match-beginning 1) (match-end 1))
2159 (insert opt))
2160 (and buffer-file-name
2161 (save-buffer)))))
2163 ;;; List handling:
2165 (defun org-export-latex-lists ()
2166 "Convert plain text lists in current buffer into LaTeX lists."
2167 (goto-char (point-min))
2168 (while (re-search-forward org-list-beginning-re nil t)
2169 (org-if-unprotected
2170 (beginning-of-line)
2171 (insert (org-list-to-latex (org-list-parse-list t)
2172 org-export-latex-list-parameters))
2173 "\n")))
2175 (defconst org-latex-entities
2176 '("\\!"
2177 "\\'"
2178 "\\+"
2179 "\\,"
2180 "\\-"
2181 "\\:"
2182 "\\;"
2183 "\\<"
2184 "\\="
2185 "\\>"
2186 "\\Huge"
2187 "\\LARGE"
2188 "\\Large"
2189 "\\Styles"
2190 "\\\\"
2191 "\\`"
2192 "\\addcontentsline"
2193 "\\address"
2194 "\\addtocontents"
2195 "\\addtocounter"
2196 "\\addtolength"
2197 "\\addvspace"
2198 "\\alph"
2199 "\\appendix"
2200 "\\arabic"
2201 "\\author"
2202 "\\begin{array}"
2203 "\\begin{center}"
2204 "\\begin{description}"
2205 "\\begin{enumerate}"
2206 "\\begin{eqnarray}"
2207 "\\begin{equation}"
2208 "\\begin{figure}"
2209 "\\begin{flushleft}"
2210 "\\begin{flushright}"
2211 "\\begin{itemize}"
2212 "\\begin{list}"
2213 "\\begin{minipage}"
2214 "\\begin{picture}"
2215 "\\begin{quotation}"
2216 "\\begin{quote}"
2217 "\\begin{tabbing}"
2218 "\\begin{table}"
2219 "\\begin{tabular}"
2220 "\\begin{thebibliography}"
2221 "\\begin{theorem}"
2222 "\\begin{titlepage}"
2223 "\\begin{verbatim}"
2224 "\\begin{verse}"
2225 "\\bf"
2226 "\\bf"
2227 "\\bibitem"
2228 "\\bigskip"
2229 "\\cdots"
2230 "\\centering"
2231 "\\circle"
2232 "\\cite"
2233 "\\cleardoublepage"
2234 "\\clearpage"
2235 "\\cline"
2236 "\\closing"
2237 "\\dashbox"
2238 "\\date"
2239 "\\ddots"
2240 "\\dotfill"
2241 "\\em"
2242 "\\fbox"
2243 "\\flushbottom"
2244 "\\fnsymbol"
2245 "\\footnote"
2246 "\\footnotemark"
2247 "\\footnotesize"
2248 "\\footnotetext"
2249 "\\frac"
2250 "\\frame"
2251 "\\framebox"
2252 "\\hfill"
2253 "\\hline"
2254 "\\hrulespace"
2255 "\\hspace"
2256 "\\huge"
2257 "\\hyphenation"
2258 "\\include"
2259 "\\includeonly"
2260 "\\indent"
2261 "\\input"
2262 "\\it"
2263 "\\kill"
2264 "\\label"
2265 "\\large"
2266 "\\ldots"
2267 "\\line"
2268 "\\linebreak"
2269 "\\linethickness"
2270 "\\listoffigures"
2271 "\\listoftables"
2272 "\\location"
2273 "\\makebox"
2274 "\\maketitle"
2275 "\\mark"
2276 "\\mbox"
2277 "\\medskip"
2278 "\\multicolumn"
2279 "\\multiput"
2280 ("\\nbsp" "~")
2281 "\\newcommand"
2282 "\\newcounter"
2283 "\\newenvironment"
2284 "\\newfont"
2285 "\\newlength"
2286 "\\newline"
2287 "\\newpage"
2288 "\\newsavebox"
2289 "\\newtheorem"
2290 "\\nocite"
2291 "\\nofiles"
2292 "\\noindent"
2293 "\\nolinebreak"
2294 "\\nopagebreak"
2295 "\\normalsize"
2296 "\\onecolumn"
2297 "\\opening"
2298 "\\oval"
2299 "\\overbrace"
2300 "\\overline"
2301 "\\pagebreak"
2302 "\\pagenumbering"
2303 "\\pageref"
2304 "\\pagestyle"
2305 "\\par"
2306 "\\parbox"
2307 "\\put"
2308 "\\raggedbottom"
2309 "\\raggedleft"
2310 "\\raggedright"
2311 "\\raisebox"
2312 "\\ref"
2313 "\\rm"
2314 "\\roman"
2315 "\\rule"
2316 "\\savebox"
2317 "\\sc"
2318 "\\scriptsize"
2319 "\\setcounter"
2320 "\\setlength"
2321 "\\settowidth"
2322 "\\sf"
2323 "\\shortstack"
2324 "\\signature"
2325 "\\sl"
2326 "\\small"
2327 "\\smallskip"
2328 "\\sqrt"
2329 "\\tableofcontents"
2330 "\\telephone"
2331 "\\thanks"
2332 "\\thispagestyle"
2333 "\\tiny"
2334 "\\title"
2335 "\\tt"
2336 "\\twocolumn"
2337 "\\typein"
2338 "\\typeout"
2339 "\\underbrace"
2340 "\\underline"
2341 "\\usebox"
2342 "\\usecounter"
2343 "\\value"
2344 "\\vdots"
2345 "\\vector"
2346 "\\verb"
2347 "\\vfill"
2348 "\\vline"
2349 "\\vspace")
2350 "A list of LaTeX commands to be protected when performing conversion.")
2352 (defvar org-latex-entities-exceptions nil)
2354 (defconst org-latex-entities-regexp
2355 (let (names rest)
2356 (dolist (x org-latex-entities)
2357 (when (consp x)
2358 (add-to-list 'org-latex-entities-exceptions x)
2359 (setq x (car x)))
2360 (if (string-match "[a-zA-Z]$" x)
2361 (push x names)
2362 (push x rest)))
2363 (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
2364 "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
2366 (provide 'org-export-latex)
2367 (provide 'org-latex)
2369 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
2371 ;;; org-latex.el ends here