Release 6.35c
[org-mode.git] / lisp / org-latex.el
blob460d928e85b5dec74a6a046ea6d594aa3c8c0258
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: TAG=6.35c
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 (run-hooks 'org-export-first-hook)
589 ;; Make sure we have a file name when we need it.
590 (when (and (not (or to-buffer body-only))
591 (not buffer-file-name))
592 (if (buffer-base-buffer)
593 (org-set-local 'buffer-file-name
594 (with-current-buffer (buffer-base-buffer)
595 buffer-file-name))
596 (error "Need a file name to be able to export")))
598 (message "Exporting to LaTeX...")
599 (org-unmodified
600 (let ((inhibit-read-only t))
601 (remove-text-properties (point-min) (point-max)
602 '(:org-license-to-kill nil))))
603 (org-update-radio-target-regexp)
604 (org-export-latex-set-initial-vars ext-plist arg)
605 (setq org-export-opt-plist org-export-latex-options-plist)
606 (org-install-letbind)
607 (run-hooks 'org-export-latex-after-initial-vars-hook)
608 (let* ((wcf (current-window-configuration))
609 (opt-plist org-export-latex-options-plist)
610 (region-p (org-region-active-p))
611 (rbeg (and region-p (region-beginning)))
612 (rend (and region-p (region-end)))
613 (subtree-p
614 (if (plist-get opt-plist :ignore-subtree-p)
616 (when region-p
617 (save-excursion
618 (goto-char rbeg)
619 (and (org-at-heading-p)
620 (>= (org-end-of-subtree t t) rend))))))
621 (opt-plist (setq org-export-opt-plist
622 (if subtree-p
623 (org-export-add-subtree-options opt-plist rbeg)
624 opt-plist)))
625 ;; Make sure the variable contains the updated values.
626 (org-export-latex-options-plist (setq org-export-opt-plist opt-plist))
627 ;; The following two are dynamically scoped into other
628 ;; routines below.
629 (org-current-export-dir
630 (or pub-dir (org-export-directory :html opt-plist)))
631 (org-current-export-file buffer-file-name)
632 (title (or (and subtree-p (org-export-get-title-from-subtree))
633 (plist-get opt-plist :title)
634 (and (not
635 (plist-get opt-plist :skip-before-1st-heading))
636 (org-export-grab-title-from-buffer))
637 (and buffer-file-name
638 (file-name-sans-extension
639 (file-name-nondirectory buffer-file-name)))
640 "No Title"))
641 (filename
642 (and (not to-buffer)
643 (concat
644 (file-name-as-directory
645 (or pub-dir
646 (org-export-directory :LaTeX ext-plist)))
647 (file-name-sans-extension
648 (or (and subtree-p
649 (org-entry-get rbeg "EXPORT_FILE_NAME" t))
650 (file-name-nondirectory ;sans-extension
651 (or buffer-file-name
652 (error "Don't know which export file to use.")))))
653 ".tex")))
654 (filename
655 (and filename
656 (if (equal (file-truename filename)
657 (file-truename (or buffer-file-name "dummy.org")))
658 (concat filename ".tex")
659 filename)))
660 (buffer (if to-buffer
661 (cond
662 ((eq to-buffer 'string) (get-buffer-create
663 "*Org LaTeX Export*"))
664 (t (get-buffer-create to-buffer)))
665 (find-file-noselect filename)))
666 (odd org-odd-levels-only)
667 (header (org-export-latex-make-header title opt-plist))
668 (skip (cond (subtree-p nil)
669 (region-p nil)
670 (t (plist-get opt-plist :skip-before-1st-heading))))
671 (text (plist-get opt-plist :text))
672 (org-export-preprocess-hook
673 (cons
674 `(lambda () (org-set-local 'org-complex-heading-regexp
675 ,org-export-latex-complex-heading-re))
676 org-export-preprocess-hook))
677 (first-lines (if skip "" (org-export-latex-first-lines
678 opt-plist
679 (if subtree-p
680 (save-excursion
681 (goto-char rbeg)
682 (point-at-bol 2))
683 rbeg)
684 (if region-p rend))))
685 (coding-system (and (boundp 'buffer-file-coding-system)
686 buffer-file-coding-system))
687 (coding-system-for-write (or org-export-latex-coding-system
688 coding-system))
689 (save-buffer-coding-system (or org-export-latex-coding-system
690 coding-system))
691 (region (buffer-substring
692 (if region-p (region-beginning) (point-min))
693 (if region-p (region-end) (point-max))))
694 (text
695 (and text (string-match "\\S-" text)
696 (org-export-preprocess-string
697 text
698 :emph-multiline t
699 :for-LaTeX t
700 :comments nil
701 :tags (plist-get opt-plist :tags)
702 :priority (plist-get opt-plist :priority)
703 :footnotes (plist-get opt-plist :footnotes)
704 :drawers (plist-get opt-plist :drawers)
705 :timestamps (plist-get opt-plist :timestamps)
706 :todo-keywords (plist-get opt-plist :todo-keywords)
707 :add-text nil
708 :skip-before-1st-heading skip
709 :select-tags nil
710 :exclude-tags nil
711 :LaTeX-fragments nil)))
712 (string-for-export
713 (org-export-preprocess-string
714 region
715 :emph-multiline t
716 :for-LaTeX t
717 :comments nil
718 :tags (plist-get opt-plist :tags)
719 :priority (plist-get opt-plist :priority)
720 :footnotes (plist-get opt-plist :footnotes)
721 :drawers (plist-get opt-plist :drawers)
722 :timestamps (plist-get opt-plist :timestamps)
723 :todo-keywords (plist-get opt-plist :todo-keywords)
724 :add-text (if (eq to-buffer 'string) nil text)
725 :skip-before-1st-heading skip
726 :select-tags (plist-get opt-plist :select-tags)
727 :exclude-tags (plist-get opt-plist :exclude-tags)
728 :LaTeX-fragments nil)))
730 (set-buffer buffer)
731 (erase-buffer)
732 (org-install-letbind)
734 (and (fboundp 'set-buffer-file-coding-system)
735 (set-buffer-file-coding-system coding-system-for-write))
737 ;; insert the header and initial document commands
738 (unless (or (eq to-buffer 'string) body-only)
739 (insert header))
741 ;; insert text found in #+TEXT
742 (when (and text (not (eq to-buffer 'string)))
743 (insert (org-export-latex-content
744 text '(lists tables fixed-width keywords))
745 "\n\n"))
747 ;; insert lines before the first headline
748 (unless skip
749 (insert first-lines))
751 ;; export the content of headlines
752 (org-export-latex-global
753 (with-temp-buffer
754 (insert string-for-export)
755 (goto-char (point-min))
756 (when (re-search-forward "^\\(\\*+\\) " nil t)
757 (let* ((asters (length (match-string 1)))
758 (level (if odd (- asters 2) (- asters 1))))
759 (setq org-export-latex-add-level
760 (if odd (1- (/ (1+ asters) 2)) (1- asters)))
761 (org-export-latex-parse-global level odd)))))
763 ;; finalization
764 (unless body-only (insert "\n\\end{document}"))
766 ;; Attach description terms to the \item macro
767 (goto-char (point-min))
768 (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t)
769 (delete-region (match-beginning 1) (match-end 1)))
771 ;; Relocate the table of contents
772 (goto-char (point-min))
773 (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
774 (goto-char (point-min))
775 (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t)
776 (replace-match ""))
777 (goto-char (point-min))
778 (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
779 (replace-match "\\tableofcontents" t t)))
781 ;; Cleanup forced line ends in items where they are not needed
782 (goto-char (point-min))
783 (while (re-search-forward
784 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin"
785 nil t)
786 (delete-region (match-beginning 1) (match-end 1)))
787 (goto-char (point-min))
788 (while (re-search-forward
789 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*"
790 nil t)
791 (if (looking-at "[\n \t]+")
792 (replace-match "\n")))
794 (run-hooks 'org-export-latex-final-hook)
795 (or to-buffer (save-buffer))
796 (org-export-latex-fix-inputenc)
797 (run-hooks 'org-export-latex-after-save-hook)
798 (goto-char (point-min))
799 (or (org-export-push-to-kill-ring "LaTeX")
800 (message "Exporting to LaTeX...done"))
801 (prog1
802 (if (eq to-buffer 'string)
803 (prog1 (buffer-substring (point-min) (point-max))
804 (kill-buffer (current-buffer)))
805 (current-buffer))
806 (set-window-configuration wcf))))
808 ;;;###autoload
809 (defun org-export-as-pdf (arg &optional hidden ext-plist
810 to-buffer body-only pub-dir)
811 "Export as LaTeX, then process through to PDF."
812 (interactive "P")
813 (message "Exporting to PDF...")
814 (let* ((wconfig (current-window-configuration))
815 (lbuf (org-export-as-latex arg hidden ext-plist
816 to-buffer body-only pub-dir))
817 (file (buffer-file-name lbuf))
818 (base (file-name-sans-extension (buffer-file-name lbuf)))
819 (pdffile (concat base ".pdf"))
820 (cmds org-latex-to-pdf-process)
821 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
822 (bibtex-p (with-current-buffer lbuf
823 (save-excursion
824 (goto-char (point-min))
825 (re-search-forward "\\\\bibliography{" nil t))))
826 cmd)
827 (with-current-buffer outbuf (erase-buffer))
828 (message "Processing LaTeX file...")
829 (if (and cmds (symbolp cmds))
830 (funcall cmds file)
831 (while cmds
832 (setq cmd (pop cmds))
833 (while (string-match "%b" cmd)
834 (setq cmd (replace-match
835 (save-match-data
836 (shell-quote-argument base))
837 t t cmd)))
838 (while (string-match "%s" cmd)
839 (setq cmd (replace-match
840 (save-match-data
841 (shell-quote-argument file))
842 t t cmd)))
843 (shell-command cmd outbuf outbuf)))
844 (message "Processing LaTeX file...done")
845 (if (not (file-exists-p pdffile))
846 (error "PDF file was not produced")
847 (set-window-configuration wconfig)
848 (when org-export-pdf-remove-logfiles
849 (dolist (ext org-export-pdf-logfiles)
850 (setq file (concat base "." ext))
851 (and (file-exists-p file) (delete-file file))))
852 (message "Exporting to PDF...done")
853 pdffile)))
855 ;;;###autoload
856 (defun org-export-as-pdf-and-open (arg)
857 "Export as LaTeX, then process through to PDF, and open."
858 (interactive "P")
859 (let ((pdffile (org-export-as-pdf arg)))
860 (if pdffile
861 (progn
862 (org-open-file pdffile)
863 (when org-export-kill-product-buffer-when-displayed
864 (kill-buffer (find-buffer-visiting
865 (concat (file-name-sans-extension (buffer-file-name))
866 ".tex")))))
867 (error "PDF file was not produced"))))
869 ;;; Parsing functions:
871 (defun org-export-latex-parse-global (level odd)
872 "Parse the current buffer recursively, starting at LEVEL.
873 If ODD is non-nil, assume the buffer only contains odd sections.
874 Return a list reflecting the document structure."
875 (save-excursion
876 (goto-char (point-min))
877 (let* ((cnt 0) output
878 (depth org-export-latex-sectioning-depth))
879 (while (org-re-search-forward-unprotected
880 (concat "^\\(\\(?:\\*\\)\\{"
881 (number-to-string (+ (if odd 2 1) level))
882 "\\}\\) \\(.*\\)$")
883 ;; make sure that there is no upper heading
884 (when (> level 0)
885 (save-excursion
886 (save-match-data
887 (org-re-search-forward-unprotected
888 (concat "^\\(\\(?:\\*\\)\\{"
889 (number-to-string level)
890 "\\}\\) \\(.*\\)$") nil t)))) t)
891 (setq cnt (1+ cnt))
892 (let* ((pos (match-beginning 0))
893 (heading (match-string 2))
894 (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
895 (save-excursion
896 (narrow-to-region
897 (point)
898 (save-match-data
899 (if (org-re-search-forward-unprotected
900 (concat "^\\(\\(?:\\*\\)\\{"
901 (number-to-string (+ (if odd 2 1) level))
902 "\\}\\) \\(.*\\)$") nil t)
903 (match-beginning 0)
904 (point-max))))
905 (goto-char (point-min))
906 (setq output
907 (append output
908 (list
909 (list
910 `(pos . ,pos)
911 `(level . ,nlevel)
912 `(occur . ,cnt)
913 `(heading . ,heading)
914 `(content . ,(org-export-latex-parse-content))
915 `(subcontent . ,(org-export-latex-parse-subcontent
916 level odd)))))))
917 (widen)))
918 (list output))))
920 (defun org-export-latex-parse-content ()
921 "Extract the content of a section."
922 (let ((beg (point))
923 (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t)
924 (progn (beginning-of-line) (point))
925 (point-max))))
926 (buffer-substring beg end)))
928 (defun org-export-latex-parse-subcontent (level odd)
929 "Extract the subcontent of a section at LEVEL.
930 If ODD Is non-nil, assume subcontent only contains odd sections."
931 (if (not (org-re-search-forward-unprotected
932 (concat "^\\(\\(?:\\*\\)\\{"
933 (number-to-string (+ (if odd 4 2) level))
934 "\\}\\) \\(.*\\)$")
935 nil t))
936 nil ; subcontent is nil
937 (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
939 ;;; Rendering functions:
940 (defun org-export-latex-global (content)
941 "Export CONTENT to LaTeX.
942 CONTENT is an element of the list produced by
943 `org-export-latex-parse-global'."
944 (if (eq (car content) 'subcontent)
945 (mapc 'org-export-latex-sub (cdr content))
946 (org-export-latex-sub (car content))))
948 (defun org-export-latex-sub (subcontent)
949 "Export the list SUBCONTENT to LaTeX.
950 SUBCONTENT is an alist containing information about the headline
951 and its content."
952 (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
953 (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
955 (defun org-export-latex-subcontent (subcontent num)
956 "Export each cell of SUBCONTENT to LaTeX.
957 If NUM, export sections as numerical sections."
958 (let* ((heading (cdr (assoc 'heading subcontent)))
959 (level (- (cdr (assoc 'level subcontent))
960 org-export-latex-add-level))
961 (occur (number-to-string (cdr (assoc 'occur subcontent))))
962 (content (cdr (assoc 'content subcontent)))
963 (subcontent (cadr (assoc 'subcontent subcontent)))
964 (label (org-get-text-property-any 0 'target heading))
965 (label-list (cons label (cdr (assoc label
966 org-export-target-aliases))))
967 (sectioning org-export-latex-sectioning)
968 (depth org-export-latex-sectioning-depth)
969 main-heading sub-heading)
970 (when (symbolp (car sectioning))
971 (setq sectioning (funcall (car sectioning) level heading))
972 (when sectioning
973 (setq heading (car sectioning)
974 sectioning (cdr sectioning)
975 ;; target property migh have changed...
976 label (org-get-text-property-any 0 'target heading)
977 label-list (cons label (cdr (assoc label
978 org-export-target-aliases)))))
979 (if sectioning (setq sectioning (make-list 10 sectioning)))
980 (setq depth (if sectioning 10000 0)))
981 (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading)
982 (setq main-heading (substring heading 0 (match-beginning 0))
983 sub-heading (substring heading (match-end 0))))
984 (setq heading (org-export-latex-fontify-headline heading)
985 sub-heading (and sub-heading
986 (org-export-latex-fontify-headline sub-heading))
987 main-heading (and main-heading
988 (org-export-latex-fontify-headline main-heading)))
989 (cond
990 ;; Normal conversion
991 ((<= level depth)
992 (let* ((sec (nth (1- level) sectioning))
993 start end)
994 (if (consp (cdr sec))
995 (setq start (nth (if num 0 2) sec)
996 end (nth (if num 1 3) sec))
997 (setq start (if num (car sec) (cdr sec))))
998 (insert (format start (if main-heading main-heading heading)
999 (or sub-heading "")))
1000 (insert "\n")
1001 (when label
1002 (insert (mapconcat (lambda (l) (format "\\label{%s}" l))
1003 label-list "\n") "\n"))
1004 (insert (org-export-latex-content content))
1005 (cond ((stringp subcontent) (insert subcontent))
1006 ((listp subcontent)
1007 (while (org-looking-back "\n\n") (backward-delete-char 1))
1008 (org-export-latex-sub subcontent)))
1009 (when (and end (string-match "[^ \t]" end))
1010 (let ((hook (org-get-text-property-any 0 'org-insert-hook end)))
1011 (and (functionp hook) (funcall hook)))
1012 (insert end "\n"))))
1013 ;; At a level under the hl option: we can drop this subsection
1014 ((> level depth)
1015 (cond ((eq org-export-latex-low-levels 'description)
1016 (if (string-match "% ends low level$"
1017 (buffer-substring (point-at-bol 0) (point)))
1018 (delete-region (point-at-bol 0) (point))
1019 (insert "\\begin{description}\n"))
1020 (insert (format "\n\\item[%s]%s~\n"
1021 heading
1022 (if label (format "\\label{%s}" label) "")))
1023 (insert (org-export-latex-content content))
1024 (cond ((stringp subcontent) (insert subcontent))
1025 ((listp subcontent) (org-export-latex-sub subcontent)))
1026 (insert "\\end{description} % ends low level\n"))
1027 ((memq org-export-latex-low-levels '(itemize enumerate))
1028 (if (string-match "% ends low level$"
1029 (buffer-substring (point-at-bol 0) (point)))
1030 (delete-region (point-at-bol 0) (point))
1031 (insert (format "\\begin{%s}\n"
1032 (symbol-name org-export-latex-low-levels))))
1033 (insert (format "\n\\item %s\\\\\n%s%%"
1034 heading
1035 (if label (format "\\label{%s}" label) "")))
1036 (insert (org-export-latex-content content))
1037 (cond ((stringp subcontent) (insert subcontent))
1038 ((listp subcontent) (org-export-latex-sub subcontent)))
1039 (insert (format "\\end{%s} %% ends low level\n"
1040 (symbol-name org-export-latex-low-levels))))
1042 ((listp org-export-latex-low-levels)
1043 (if (string-match "% ends low level$"
1044 (buffer-substring (point-at-bol 0) (point)))
1045 (delete-region (point-at-bol 0) (point))
1046 (insert (car org-export-latex-low-levels) "\n"))
1047 (insert (format (nth 2 org-export-latex-low-levels)
1048 heading
1049 (if label (format "\\label{%s}" label) "")))
1050 (insert (org-export-latex-content content))
1051 (cond ((stringp subcontent) (insert subcontent))
1052 ((listp subcontent) (org-export-latex-sub subcontent)))
1053 (insert (nth 1 org-export-latex-low-levels)
1054 " %% ends low level\n"))
1056 ((stringp org-export-latex-low-levels)
1057 (insert (format org-export-latex-low-levels heading) "\n")
1058 (when label (insert (format "\\label{%s}\n" label)))
1059 (insert (org-export-latex-content content))
1060 (cond ((stringp subcontent) (insert subcontent))
1061 ((listp subcontent) (org-export-latex-sub subcontent)))))))))
1063 ;;; Exporting internals:
1064 (defun org-export-latex-set-initial-vars (ext-plist level)
1065 "Store org local variables required for LaTeX export.
1066 EXT-PLIST is an optional additional plist.
1067 LEVEL indicates the default depth for export."
1068 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
1069 org-export-latex-done-keywords org-done-keywords
1070 org-export-latex-not-done-keywords org-not-done-keywords
1071 org-export-latex-complex-heading-re org-complex-heading-regexp
1072 org-export-latex-display-custom-times org-display-custom-times
1073 org-export-latex-all-targets-re
1074 (org-make-target-link-regexp (org-all-targets))
1075 org-export-latex-options-plist
1076 (org-combine-plists (org-default-export-plist) ext-plist
1077 (org-infile-export-plist))
1078 org-export-latex-class
1079 (or (and (org-region-active-p)
1080 (save-excursion
1081 (goto-char (region-beginning))
1082 (and (looking-at org-complex-heading-regexp)
1083 (org-entry-get nil "LaTeX_CLASS" 'selective))))
1084 (save-excursion
1085 (save-restriction
1086 (widen)
1087 (goto-char (point-min))
1088 (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
1089 (match-string 1))))
1090 (plist-get org-export-latex-options-plist :latex-class)
1091 org-export-latex-default-class)
1092 org-export-latex-class-options
1093 (or (and (org-region-active-p)
1094 (save-excursion
1095 (goto-char (region-beginning))
1096 (and (looking-at org-complex-heading-regexp)
1097 (org-entry-get nil "LaTeX_CLASS_OPTIONS" 'selective))))
1098 (save-excursion
1099 (save-restriction
1100 (widen)
1101 (goto-char (point-min))
1102 (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t)
1103 (match-string 1))))
1104 (plist-get org-export-latex-options-plist :latex-class-options))
1105 org-export-latex-class
1106 (or (car (assoc org-export-latex-class org-export-latex-classes))
1107 (error "No definition for class `%s' in `org-export-latex-classes'"
1108 org-export-latex-class))
1109 org-export-latex-header
1110 (cadr (assoc org-export-latex-class org-export-latex-classes))
1111 org-export-latex-sectioning
1112 (cddr (assoc org-export-latex-class org-export-latex-classes))
1113 org-export-latex-sectioning-depth
1114 (or level
1115 (let ((hl-levels
1116 (plist-get org-export-latex-options-plist :headline-levels))
1117 (sec-depth (length org-export-latex-sectioning)))
1118 (if (> hl-levels sec-depth) sec-depth hl-levels))))
1119 (when (and org-export-latex-class-options
1120 (string-match "\\S-" org-export-latex-class-options)
1121 (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?"
1122 org-export-latex-header))
1123 (setq org-export-latex-header
1124 (concat (substring org-export-latex-header 0 (match-end 1))
1125 org-export-latex-class-options
1126 (substring org-export-latex-header (match-end 0))))))
1128 (defvar org-export-latex-format-toc-function
1129 'org-export-latex-format-toc-default
1130 "The function formatting returning the string to createthe table of contents.
1131 The function mus take one parameter, the depth of the table of contents.")
1133 (defun org-export-latex-make-header (title opt-plist)
1134 "Make the LaTeX header and return it as a string.
1135 TITLE is the current title from the buffer or region.
1136 OPT-PLIST is the options plist for current buffer."
1137 (let ((toc (plist-get opt-plist :table-of-contents))
1138 (author (org-export-apply-macros-in-string
1139 (plist-get opt-plist :author))))
1140 (concat
1141 (if (plist-get opt-plist :time-stamp-file)
1142 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1143 ;; insert LaTeX custom header and packages from the list
1144 (org-splice-latex-header
1145 (org-export-apply-macros-in-string org-export-latex-header)
1146 org-export-latex-default-packages-alist
1147 org-export-latex-packages-alist
1148 (org-export-apply-macros-in-string
1149 (plist-get opt-plist :latex-header-extra)))
1150 ;; append another special variable
1151 (org-export-apply-macros-in-string org-export-latex-append-header)
1152 ;; define align if not yet defined
1153 "\n\\providecommand{\\alert}[1]{\\textbf{#1}}"
1154 ;; insert the title
1155 (format
1156 "\n\n\\title{%s}\n"
1157 ;; convert the title
1158 (org-export-latex-content
1159 title '(lists tables fixed-width keywords)))
1160 ;; insert author info
1161 (if (plist-get opt-plist :author-info)
1162 (format "\\author{%s}\n"
1163 (org-export-latex-fontify-headline (or author user-full-name)))
1164 (format "%%\\author{%s}\n"
1165 (org-export-latex-fontify-headline (or author user-full-name))))
1166 ;; insert the date
1167 (format "\\date{%s}\n"
1168 (format-time-string
1169 (or (plist-get opt-plist :date)
1170 org-export-latex-date-format)))
1171 ;; beginning of the document
1172 "\n\\begin{document}\n\n"
1173 ;; insert the title command
1174 (when (string-match "\\S-" title)
1175 (if (string-match "%s" org-export-latex-title-command)
1176 (format org-export-latex-title-command title)
1177 org-export-latex-title-command))
1178 "\n\n"
1179 ;; table of contents
1180 (when (and org-export-with-toc
1181 (plist-get opt-plist :section-numbers))
1182 (funcall org-export-latex-format-toc-function
1183 (cond ((numberp toc)
1184 (min toc (plist-get opt-plist :headline-levels)))
1185 (toc (plist-get opt-plist :headline-levels))))))))
1187 (defun org-export-latex-format-toc-default (depth)
1188 (when depth
1189 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1190 depth)))
1192 (defun org-export-latex-first-lines (opt-plist &optional beg end)
1193 "Export the first lines before first headline.
1194 If BEG is non-nil, it is the beginning of the region.
1195 If END is non-nil, it is the end of the region."
1196 (save-excursion
1197 (goto-char (or beg (point-min)))
1198 (let* ((pt (point))
1199 (end (if (re-search-forward "^\\*+ " end t)
1200 (goto-char (match-beginning 0))
1201 (goto-char (or end (point-max))))))
1202 (prog1
1203 (org-export-latex-content
1204 (org-export-preprocess-string
1205 (buffer-substring pt end)
1206 :for-LaTeX t
1207 :emph-multiline t
1208 :add-text nil
1209 :comments nil
1210 :skip-before-1st-heading nil
1211 :LaTeX-fragments nil
1212 :timestamps (plist-get opt-plist :timestamps)
1213 :footnotes (plist-get opt-plist :footnotes)))
1214 (org-unmodified
1215 (let ((inhibit-read-only t))
1216 (add-text-properties pt (max pt (1- end))
1217 '(:org-license-to-kill t))))))))
1219 (defvar org-export-latex-header-defs nil
1220 "The header definitions that might be used in the LaTeX body.")
1221 (defvar org-export-latex-header-defs-re nil
1222 "The header definitions that might be used in the LaTeX body.")
1224 (defun org-export-latex-content (content &optional exclude-list)
1225 "Convert CONTENT string to LaTeX.
1226 Don't perform conversions that are in EXCLUDE-LIST. Recognized
1227 conversion types are: quotation-marks, emphasis, sub-superscript,
1228 links, keywords, lists, tables, fixed-width"
1229 (with-temp-buffer
1230 (insert content)
1231 (unless (memq 'timestamps exclude-list)
1232 (org-export-latex-time-stamps))
1233 (unless (memq 'quotation-marks exclude-list)
1234 (org-export-latex-quotation-marks))
1235 (unless (memq 'emphasis exclude-list)
1236 (when (plist-get org-export-latex-options-plist :emphasize)
1237 (org-export-latex-fontify)))
1238 (unless (memq 'sub-superscript exclude-list)
1239 (org-export-latex-special-chars
1240 (plist-get org-export-latex-options-plist :sub-superscript)))
1241 (unless (memq 'links exclude-list)
1242 (org-export-latex-links))
1243 (unless (memq 'keywords exclude-list)
1244 (org-export-latex-keywords))
1245 (unless (memq 'lists exclude-list)
1246 (org-export-latex-lists))
1247 (unless (memq 'tables exclude-list)
1248 (org-export-latex-tables
1249 (plist-get org-export-latex-options-plist :tables)))
1250 (unless (memq 'fixed-width exclude-list)
1251 (org-export-latex-fixed-width
1252 (plist-get org-export-latex-options-plist :fixed-width)))
1253 ;; return string
1254 (buffer-substring (point-min) (point-max))))
1256 (defun org-export-latex-protect-string (s)
1257 "Add the org-protected property to string S."
1258 (add-text-properties 0 (length s) '(org-protected t) s) s)
1260 (defun org-export-latex-protect-char-in-string (char-list string)
1261 "Add org-protected text-property to char from CHAR-LIST in STRING."
1262 (with-temp-buffer
1263 (save-match-data
1264 (insert string)
1265 (goto-char (point-min))
1266 (while (re-search-forward (regexp-opt char-list) nil t)
1267 (add-text-properties (match-beginning 0)
1268 (match-end 0) '(org-protected t)))
1269 (buffer-string))))
1271 (defun org-export-latex-keywords-maybe (&optional remove-list)
1272 "Maybe remove keywords depending on rules in REMOVE-LIST."
1273 (goto-char (point-min))
1274 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
1275 (case-fold-search nil)
1276 (todo-markup org-export-latex-todo-keyword-markup)
1277 fmt)
1278 ;; convert TODO keywords
1279 (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
1280 (if (plist-get remove-list :todo)
1281 (replace-match "")
1282 (setq fmt (cond
1283 ((stringp todo-markup) todo-markup)
1284 ((and (consp todo-markup) (stringp (car todo-markup)))
1285 (if (member (match-string 1) org-export-latex-done-keywords)
1286 (cdr todo-markup) (car todo-markup)))
1287 (t (cdr (or (assoc (match-string 1) todo-markup)
1288 (car todo-markup))))))
1289 (replace-match (format fmt (match-string 1)) t t)))
1290 ;; convert priority string
1291 (when (re-search-forward "\\[\\\\#.\\]" nil t)
1292 (if (plist-get remove-list :priority)
1293 (replace-match "")
1294 (replace-match (format "\\textbf{%s}" (match-string 0)) t t)))
1295 ;; convert tags
1296 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t)
1297 (if (or (not org-export-with-tags)
1298 (plist-get remove-list :tags))
1299 (replace-match "")
1300 (replace-match
1301 (org-export-latex-protect-string
1302 (format "\\textbf{%s}"
1303 (save-match-data
1304 (replace-regexp-in-string
1305 "_" "\\\\_" (match-string 0)))))
1306 t t)))))
1308 (defun org-export-latex-fontify-headline (string)
1309 "Fontify special words in STRING."
1310 (with-temp-buffer
1311 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
1312 ;; the beginning of the buffer - inserting "\n" is safe here though.
1313 (insert "\n" string)
1314 (goto-char (point-min))
1315 (let ((re (concat "\\\\[a-zA-Z]+"
1316 "\\(?:<[^<>\n]*>\\)*"
1317 "\\(?:\\[[^][\n]*?\\]\\)*"
1318 "\\(?:<[^<>\n]*>\\)*"
1319 "\\("
1320 (org-create-multibrace-regexp "{" "}" 3)
1321 "\\)\\{1,3\\}")))
1322 (while (re-search-forward re nil t)
1323 (unless (save-excursion (goto-char (match-beginning 0))
1324 (equal (char-after (point-at-bol)) ?#))
1325 (add-text-properties (match-beginning 0) (match-end 0)
1326 '(org-protected t)))))
1327 (when (plist-get org-export-latex-options-plist :emphasize)
1328 (org-export-latex-fontify))
1329 (org-export-latex-keywords-maybe)
1330 (org-export-latex-special-chars
1331 (plist-get org-export-latex-options-plist :sub-superscript))
1332 (org-export-latex-links)
1333 (org-trim (buffer-string))))
1335 (defun org-export-latex-time-stamps ()
1336 "Format time stamps."
1337 (goto-char (point-min))
1338 (let ((org-display-custom-times org-export-latex-display-custom-times))
1339 (while (re-search-forward org-ts-regexp-both nil t)
1340 (org-if-unprotected-at (1- (point))
1341 (replace-match
1342 (org-export-latex-protect-string
1343 (format org-export-latex-timestamp-markup
1344 (substring (org-translate-time (match-string 0)) 1 -1)))
1345 t t)))))
1347 (defun org-export-latex-quotation-marks ()
1348 "Export quotation marks depending on language conventions."
1349 (let* ((lang (plist-get org-export-latex-options-plist :language))
1350 (quote-rpl (if (equal lang "fr")
1351 '(("\\(\\s-\\)\"" "«~")
1352 ("\\(\\S-\\)\"" "~»")
1353 ("\\(\\s-\\)'" "`"))
1354 '(("\\(\\s-\\|[[(]\\)\"" "``")
1355 ("\\(\\S-\\)\"" "''")
1356 ("\\(\\s-\\|(\\)'" "`")))))
1357 (mapc (lambda(l) (goto-char (point-min))
1358 (while (re-search-forward (car l) nil t)
1359 (let ((rpl (concat (match-string 1)
1360 (org-export-latex-protect-string
1361 (copy-sequence (cadr l))))))
1362 (org-if-unprotected-1
1363 (replace-match rpl t t))))) quote-rpl)))
1365 (defun org-export-latex-special-chars (sub-superscript)
1366 "Export special characters to LaTeX.
1367 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
1368 See the `org-export-latex.el' code for a complete conversion table."
1369 (goto-char (point-min))
1370 (mapc (lambda(c)
1371 (goto-char (point-min))
1372 (while (re-search-forward c nil t)
1373 ;; Put the point where to check for org-protected
1374 (unless (get-text-property (match-beginning 2) 'org-protected)
1375 (cond ((member (match-string 2) '("\\$" "$"))
1376 (if (equal (match-string 2) "\\$")
1378 (replace-match "\\$" t t)))
1379 ((member (match-string 2) '("&" "%" "#"))
1380 (if (equal (match-string 1) "\\")
1381 (replace-match (match-string 2) t t)
1382 (replace-match (concat (match-string 1) "\\"
1383 (match-string 2)) t t)
1384 (backward-char 1)))
1385 ((equal (match-string 2) "...")
1386 (replace-match
1387 (concat (match-string 1)
1388 (org-export-latex-protect-string "\\ldots{}")) t t))
1389 ((equal (match-string 2) "~")
1390 (cond ((equal (match-string 1) "\\") nil)
1391 ((eq 'org-link (get-text-property 0 'face (match-string 2)))
1392 (replace-match (concat (match-string 1) "\\~") t t))
1393 (t (replace-match
1394 (org-export-latex-protect-string
1395 (concat (match-string 1) "\\~{}")) t t))))
1396 ((member (match-string 2) '("{" "}"))
1397 (unless (save-match-data (org-inside-latex-math-p))
1398 (if (equal (match-string 1) "\\")
1399 (replace-match (match-string 2) t t)
1400 (replace-match (concat (match-string 1) "\\"
1401 (match-string 2)) t t)))))
1402 (unless (save-match-data (org-inside-latex-math-p))
1403 (cond ((equal (match-string 2) "\\")
1404 (replace-match (or (save-match-data
1405 (org-export-latex-treat-backslash-char
1406 (match-string 1)
1407 (or (match-string 3) "")))
1408 "") t t)
1409 (backward-char 1))
1410 ((member (match-string 2) '("_" "^"))
1411 (replace-match (or (save-match-data
1412 (org-export-latex-treat-sub-super-char
1413 sub-superscript
1414 (match-string 2)
1415 (match-string 1)
1416 (match-string 3))) "") t t)
1417 (backward-char 1)))))))
1418 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
1419 "\\(\\(\\\\?\\$\\)\\)"
1420 "\\([a-zA-Z0-9()]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-zA-Z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-zA-Z0-9]+}\\|([a-zA-Z0-9]+)\\)"
1421 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|\\([&#%{}\"]\\|[a-zA-Z][a-zA-Z0-9]*\\)\\)"
1422 "\\(.\\|^\\)\\(&\\)"
1423 "\\(.\\|^\\)\\(#\\)"
1424 "\\(.\\|^\\)\\(%\\)"
1425 "\\(.\\|^\\)\\({\\)"
1426 "\\(.\\|^\\)\\(}\\)"
1427 "\\(.\\|^\\)\\(~\\)"
1428 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
1429 ;; (?\< . "\\textless{}")
1430 ;; (?\> . "\\textgreater{}")
1433 (defun org-inside-latex-math-p ()
1434 (get-text-property (point) 'org-latex-math))
1436 (defun org-export-latex-treat-sub-super-char
1437 (subsup char string-before string-after)
1438 "Convert the \"_\" and \"^\" characters to LaTeX.
1439 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
1440 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
1441 (cond ((equal string-before "\\")
1442 (concat string-before char string-after))
1443 ((and (string-match "\\S-+" string-after))
1444 ;; this is part of a math formula
1445 (cond ((eq 'org-link (get-text-property 0 'face char))
1446 (concat string-before "\\" char string-after))
1447 ((save-match-data (org-inside-latex-math-p))
1448 (if subsup
1449 (cond ((eq 1 (length string-after))
1450 (concat string-before char string-after))
1451 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
1452 (format "%s%s{%s}" string-before char
1453 (match-string 1 string-after))))))
1454 ((and (> (length string-after) 1)
1455 (or (eq subsup t)
1456 (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
1457 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
1458 (org-export-latex-protect-string
1459 (format "%s$%s{%s}$" string-before char
1460 (if (and (> (match-end 1) (1+ (match-beginning 1)))
1461 (not (equal (substring string-after 0 2) "{\\")))
1462 (concat "\\mathrm{" (match-string 1 string-after) "}")
1463 (match-string 1 string-after)))))
1464 ((eq subsup t) (concat string-before "$" char string-after "$"))
1465 (t (org-export-latex-protect-string
1466 (concat string-before "\\" char "{}" string-after)))))
1467 (t (org-export-latex-protect-string
1468 (concat string-before "\\" char "{}" string-after)))))
1470 (defun org-export-latex-treat-backslash-char (string-before string-after)
1471 "Convert the \"$\" special character to LaTeX.
1472 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
1473 (let ((ass (org-entity-get string-after)))
1474 (cond
1475 (ass (if (nth 2 ass)
1476 (concat string-before
1477 (org-export-latex-protect-string
1478 (concat "$" (nth 1 ass) "$")))
1479 (concat string-before (org-export-latex-protect-string
1480 (nth 1 ass)))))
1481 ((and (not (string-match "^[ \n\t]" string-after))
1482 (not (string-match "[ \t]\\'\\|^" string-before)))
1483 ;; backslash is inside a word
1484 (concat string-before
1485 (org-export-latex-protect-string
1486 (concat "\\textbackslash{}" string-after))))
1487 ((not (or (equal string-after "")
1488 (string-match "^[ \t\n]" string-after)))
1489 ;; backslash might escape a character (like \#) or a user TeX
1490 ;; macro (like \setcounter)
1491 (concat string-before
1492 (org-export-latex-protect-string (concat "\\" string-after))))
1493 ((and (string-match "^[ \t\n]" string-after)
1494 (string-match "[ \t\n]\\'" string-before))
1495 ;; backslash is alone, convert it to $\backslash$
1496 (org-export-latex-protect-string
1497 (concat string-before "\\textbackslash{}" string-after)))
1498 (t (org-export-latex-protect-string
1499 (concat string-before "\\textbackslash{}" string-after))))))
1501 (defun org-export-latex-keywords ()
1502 "Convert special keywords to LaTeX."
1503 (goto-char (point-min))
1504 (while (re-search-forward org-export-latex-special-keyword-regexp nil t)
1505 (replace-match (format org-export-latex-timestamp-keyword-markup
1506 (match-string 0)) t t)
1507 (save-excursion
1508 (beginning-of-line 1)
1509 (unless (looking-at ".*\n[ \t]*\n")
1510 (end-of-line 1)
1511 (insert "\n")))))
1513 (defun org-export-latex-fixed-width (opt)
1514 "When OPT is non-nil convert fixed-width sections to LaTeX."
1515 (goto-char (point-min))
1516 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t)
1517 (if opt
1518 (progn (goto-char (match-beginning 0))
1519 (insert "\\begin{verbatim}\n")
1520 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1521 (replace-match (concat (match-string 1)
1522 (match-string 2)) t t)
1523 (forward-line))
1524 (insert "\\end{verbatim}\n\n"))
1525 (progn (goto-char (match-beginning 0))
1526 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1527 (replace-match (concat "%" (match-string 1)
1528 (match-string 2)) t t)
1529 (forward-line))))))
1532 (defvar org-table-last-alignment) ; defined in org-table.el
1533 (defvar org-table-last-column-widths) ; defined in org-table.el
1534 (declare-function orgtbl-to-latex "org-table" (table params) t)
1535 (defun org-export-latex-tables (insert)
1536 "Convert tables to LaTeX and INSERT it."
1537 ;; First, get the table.el tables
1538 (goto-char (point-min))
1539 (while (re-search-forward "^[ \t]*\\(\\+-[-+]*\\+\\)[ \t]*\n[ \t]*|" nil t)
1540 (org-if-unprotected
1541 (require 'table)
1542 (org-export-latex-convert-table.el-table)))
1544 ;; And now the Org-mode tables
1545 (goto-char (point-min))
1546 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
1547 (org-if-unprotected-at (1- (point))
1548 (org-table-align)
1549 (let* ((beg (org-table-begin))
1550 (end (org-table-end))
1551 (raw-table (buffer-substring beg end))
1552 (org-table-last-alignment (copy-sequence org-table-last-alignment))
1553 (org-table-last-column-widths (copy-sequence
1554 org-table-last-column-widths))
1555 fnum fields line lines olines gr colgropen line-fmt align
1556 caption label attr floatp longtblp)
1557 (if org-export-latex-tables-verbatim
1558 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1559 "\\end{verbatim}\n")))
1560 (apply 'delete-region (list beg end))
1561 (insert (org-export-latex-protect-string tbl)))
1562 (progn
1563 (setq caption (org-find-text-property-in-string
1564 'org-caption raw-table)
1565 attr (org-find-text-property-in-string
1566 'org-attributes raw-table)
1567 label (org-find-text-property-in-string
1568 'org-label raw-table)
1569 longtblp (and attr (stringp attr)
1570 (string-match "\\<longtable\\>" attr))
1571 align (and attr (stringp attr)
1572 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
1573 (match-string 1 attr))
1574 floatp (or caption label))
1575 (setq caption (and caption (org-export-latex-content caption)))
1576 (setq lines (org-split-string raw-table "\n"))
1577 (apply 'delete-region (list beg end))
1578 (when org-export-table-remove-special-lines
1579 (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
1580 (when org-table-clean-did-remove-column
1581 (pop org-table-last-alignment)
1582 (pop org-table-last-column-widths))
1583 ;; make a formatting string to reflect alignment
1584 (setq olines lines)
1585 (while (and (not line-fmt) (setq line (pop olines)))
1586 (unless (string-match "^[ \t]*|-" line)
1587 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1588 (setq fnum (make-vector (length fields) 0))
1589 (setq line-fmt
1590 (mapconcat
1591 (lambda (x)
1592 (setq gr (pop org-table-colgroup-info))
1593 (format "%s%%s%s"
1594 (cond ((eq gr :start)
1595 (prog1 (if colgropen "|" "|")
1596 (setq colgropen t)))
1597 ((eq gr :startend)
1598 (prog1 (if colgropen "|" "|")
1599 (setq colgropen nil)))
1600 (t ""))
1601 (if (memq gr '(:end :startend))
1602 (progn (setq colgropen nil) "|")
1603 "")))
1604 fnum ""))))
1605 ;; fix double || in line-fmt
1606 (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
1607 ;; maybe remove the first and last "|"
1608 (when (and (not org-export-latex-tables-column-borders)
1609 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
1610 (setq line-fmt (match-string 2 line-fmt)))
1611 ;; format alignment
1612 (unless align
1613 (setq align (apply 'format
1614 (cons line-fmt
1615 (mapcar (lambda (x) (if x "r" "l"))
1616 org-table-last-alignment)))))
1617 ;; prepare the table to send to orgtbl-to-latex
1618 (setq lines
1619 (mapcar
1620 (lambda(elem)
1621 (or (and (string-match "[ \t]*|-+" elem) 'hline)
1622 (org-split-string (org-trim elem) "|")))
1623 lines))
1624 (when insert
1625 (insert (org-export-latex-protect-string
1626 (concat
1627 (if longtblp
1628 (concat "\\begin{longtable}{" align "}\n")
1629 (if floatp "\\begin{table}[htb]\n"))
1630 (if floatp
1631 (format
1632 "\\caption{%s%s}"
1633 (if label (concat "\\\label{" label "}") "")
1634 (or caption "")))
1635 (if (and longtblp caption) "\\\\\n" "\n")
1636 (if (and org-export-latex-tables-centered (not longtblp))
1637 "\\begin{center}\n")
1638 (if (not longtblp) (concat "\\begin{tabular}{" align "}\n"))
1639 (orgtbl-to-latex
1640 lines
1641 `(:tstart nil :tend nil
1642 :hlend ,(if longtblp
1643 (format "\\\\
1644 \\hline
1645 \\endhead
1646 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
1647 \\endfoot
1648 \\endlastfoot" (length org-table-last-alignment))
1649 nil)))
1650 (if (not longtblp) (concat "\n\\end{tabular}"))
1651 (if longtblp "\n" (if org-export-latex-tables-centered
1652 "\n\\end{center}\n" "\n"))
1653 (if longtblp
1654 "\\end{longtable}"
1655 (if floatp "\\end{table}"))))
1656 "\n\n"))))))))
1658 (defun org-export-latex-convert-table.el-table ()
1659 "Replace table.el table at point with LaTeX code."
1660 (let (tbl caption label line floatp attr align rmlines)
1661 (setq line (buffer-substring (point-at-bol) (point-at-eol))
1662 label (org-get-text-property-any 0 'org-label line)
1663 caption (org-get-text-property-any 0 'org-caption line)
1664 attr (org-get-text-property-any 0 'org-attributes line)
1665 align (and attr (stringp attr)
1666 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
1667 (match-string 1 attr))
1668 rmlines (and attr (stringp attr)
1669 (string-match "\\<rmlines\\>" attr))
1670 floatp (or label caption))
1671 (and (get-buffer "*org-export-table*")
1672 (kill-buffer (get-buffer "*org-export-table*")))
1673 (table-generate-source 'latex "*org-export-table*" "caption")
1674 (setq tbl (with-current-buffer "*org-export-table*"
1675 (buffer-string)))
1676 (while (string-match "^%.*\n" tbl)
1677 (setq tbl (replace-match "" t t tbl)))
1678 ;; fix the hlines
1679 (when rmlines
1680 (let ((n 0) lines)
1681 (setq lines (mapcar (lambda (x)
1682 (if (string-match "^\\\\hline$" x)
1683 (progn
1684 (setq n (1+ n))
1685 (if (= n 2) x nil))
1687 (org-split-string tbl "\n")))
1688 (setq tbl (mapconcat 'identity (delq nil lines) "\n"))))
1689 (when (and align (string-match "\\\\begin{tabular}{.*}" tbl))
1690 (setq tbl (replace-match (concat "\\begin{tabular}{" align "}")
1691 t t tbl)))
1692 (and (get-buffer "*org-export-table*")
1693 (kill-buffer (get-buffer "*org-export-table*")))
1694 (beginning-of-line 0)
1695 (while (looking-at "[ \t]*\\(|\\|\\+-\\)")
1696 (delete-region (point) (1+ (point-at-eol))))
1697 (when org-export-latex-tables-centered
1698 (setq tbl (concat "\\begin{center}\n" tbl "\\end{center}")))
1699 (when floatp
1700 (setq tbl (concat "\\begin{table}\n"
1701 (format "\\caption{%s%s}\n"
1702 (if label (format "\\label{%s}" label) "")
1703 (or caption ""))
1705 "\n\\end{table}\n")))
1706 (insert (org-export-latex-protect-string tbl))))
1708 (defun org-export-latex-fontify ()
1709 "Convert fontification to LaTeX."
1710 (goto-char (point-min))
1711 (while (re-search-forward org-emph-re nil t)
1712 ;; The match goes one char after the *string*, except at the end of a line
1713 (let ((emph (assoc (match-string 3)
1714 org-export-latex-emphasis-alist))
1715 (beg (match-beginning 0))
1716 (end (match-end 0))
1717 rpl s)
1718 (unless emph
1719 (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
1720 (match-string 3)))
1721 (unless (or (and (get-text-property (- (point) 2) 'org-protected)
1722 (not (get-text-property
1723 (- (point) 2) 'org-verbatim-emph)))
1724 (save-excursion
1725 (goto-char (match-beginning 1))
1726 (save-match-data
1727 (and (org-at-table-p)
1728 (string-match
1729 "[|\n]" (buffer-substring beg end)))))
1730 (and (equal (match-string 3) "+")
1731 (save-match-data
1732 (string-match "\\`-+\\'" (match-string 4)))))
1733 (setq s (match-string 4))
1734 (setq rpl (concat (match-string 1)
1735 (org-export-latex-emph-format (cadr emph)
1736 (match-string 4))
1737 (match-string 5)))
1738 (if (caddr emph)
1739 (setq rpl (org-export-latex-protect-string rpl))
1740 (save-match-data
1741 (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl)
1742 (progn
1743 (add-text-properties (match-beginning 1) (match-end 1)
1744 '(org-protected t) rpl)
1745 (add-text-properties (match-beginning 3) (match-end 3)
1746 '(org-protected t) rpl)))))
1747 (replace-match rpl t t)))
1748 (backward-char)))
1750 (defvar org-export-latex-use-verb nil)
1751 (defun org-export-latex-emph-format (format string)
1752 "Format an emphasis string and handle the \\verb special case."
1753 (when (equal format "\\verb")
1754 (save-match-data
1755 (if org-export-latex-use-verb
1756 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1757 (catch 'exit
1758 (loop for i from 0 to (1- (length ll)) do
1759 (if (not (string-match (regexp-quote (substring ll i (1+ i)))
1760 string))
1761 (progn
1762 (setq format (concat "\\verb" (substring ll i (1+ i))
1763 "%s" (substring ll i (1+ i))))
1764 (throw 'exit nil))))))
1765 (let ((start 0)
1766 (trans '(("\\" . "\\textbackslash{}")
1767 ("~" . "\\textasciitilde{}")
1768 ("^" . "\\textasciicircum{}")))
1769 (rtn "") char)
1770 (while (string-match "[\\{}$%&_#~^]" string)
1771 (setq char (match-string 0 string))
1772 (if (> (match-beginning 0) 0)
1773 (setq rtn (concat rtn (substring string
1774 0 (match-beginning 0)))))
1775 (setq string (substring string (1+ (match-beginning 0))))
1776 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1777 rtn (concat rtn char)))
1778 (setq string (concat rtn string) format "\\texttt{%s}")))))
1779 (format format string))
1781 (defun org-export-latex-links ()
1782 ;; Make sure to use the LaTeX hyperref and graphicx package
1783 ;; or send some warnings.
1784 "Convert links to LaTeX."
1785 (goto-char (point-min))
1786 (while (re-search-forward org-bracket-link-analytic-regexp++ nil t)
1787 (org-if-unprotected-1
1788 (goto-char (match-beginning 0))
1789 (let* ((re-radio org-export-latex-all-targets-re)
1790 (remove (list (match-beginning 0) (match-end 0)))
1791 (raw-path (org-extract-attributes (match-string 3)))
1792 (full-raw-path (concat (match-string 1) raw-path))
1793 (desc (match-string 5))
1794 (type (or (match-string 2)
1795 (if (or (file-name-absolute-p raw-path)
1796 (string-match "^\\.\\.?/" raw-path))
1797 "file")))
1798 (coderefp (equal type "coderef"))
1799 (caption (org-find-text-property-in-string 'org-caption raw-path))
1800 (attr (or (org-find-text-property-in-string 'org-attributes raw-path)
1801 (plist-get org-export-latex-options-plist :latex-image-options)))
1802 (label (org-find-text-property-in-string 'org-label raw-path))
1803 imgp radiop
1804 ;; define the path of the link
1805 (path (cond
1806 ((member type '("coderef"))
1807 raw-path)
1808 ((member type '("http" "https" "ftp"))
1809 (concat type ":" raw-path))
1810 ((and re-radio (string-match re-radio raw-path))
1811 (setq radiop t))
1812 ((equal type "mailto")
1813 (concat type ":" raw-path))
1814 ((equal type "file")
1815 (if (and (org-file-image-p
1816 (expand-file-name
1817 raw-path)
1818 org-export-latex-inline-image-extensions)
1819 (or (get-text-property 0 'org-no-description
1820 raw-path)
1821 (equal desc full-raw-path)))
1822 (setq imgp t)
1823 (progn (when (string-match "\\(.+\\)::.+" raw-path)
1824 (setq raw-path (match-string 1 raw-path)))
1825 (if (file-exists-p raw-path)
1826 (concat type "://" (expand-file-name raw-path))
1827 (concat type "://" (org-export-directory
1828 :LaTeX org-export-latex-options-plist)
1829 raw-path))))))))
1830 ;; process with link inserting
1831 (apply 'delete-region remove)
1832 (setq caption (and caption (org-export-latex-content caption)))
1833 (cond ((and imgp
1834 (plist-get org-export-latex-options-plist :inline-images))
1835 ;; OK, we need to inline an image
1836 (insert
1837 (org-export-latex-format-image raw-path caption label attr)))
1838 (coderefp
1839 (insert (format
1840 (org-export-get-coderef-format path desc)
1841 (cdr (assoc path org-export-code-refs)))))
1842 (radiop (insert (format "\\hyperref[%s]{%s}"
1843 (org-solidify-link-text raw-path) desc)))
1844 ((not type)
1845 (insert (format "\\hyperref[%s]{%s}"
1846 (org-remove-initial-hash
1847 (org-solidify-link-text raw-path))
1848 desc)))
1849 (path
1850 (when (org-at-table-p)
1851 ;; There is a strange problem when we have a link in a table,
1852 ;; ampersands then cause a problem. I think this must be
1853 ;; a LaTeX issue, but we here implement a work-around anyway.
1854 (setq path (org-export-latex-protect-amp path)
1855 desc (org-export-latex-protect-amp desc)))
1856 (insert (format org-export-latex-hyperref-format path desc)))
1857 (t (insert "\\texttt{" desc "}")))))))
1860 (defun org-export-latex-format-image (path caption label attr)
1861 "Format the image element, depending on user settings."
1862 (let (ind floatp wrapp placement figenv)
1863 (setq floatp (or caption label))
1864 (setq ind (org-get-text-property-any 0 'original-indentation path))
1865 (when (and attr (stringp attr))
1866 (if (string-match "[ \t]*\\<wrap\\>" attr)
1867 (setq wrapp t floatp nil attr (replace-match "" t t attr)))
1868 (if (string-match "[ \t]*\\<float\\>" attr)
1869 (setq wrapp nil floatp t attr (replace-match "" t t attr))))
1871 (setq placement
1872 (cond
1873 (wrapp "{l}{0.5\\textwidth}")
1874 (floatp "[htb]")
1875 (t "")))
1877 (when (and attr (stringp attr)
1878 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr))
1879 (setq placement (match-string 1 attr)
1880 attr (replace-match "" t t attr)))
1881 (setq attr (and attr (org-trim attr)))
1882 (when (or (not attr) (= (length attr) 0))
1883 (setq attr (cond (floatp "width=0.7\\textwidth")
1884 (wrapp "width=0.48\\textwidth")
1885 (t attr))))
1886 (setq figenv
1887 (cond
1888 (wrapp "\\begin{wrapfigure}%placement
1889 \\centering
1890 \\includegraphics[%attr]{%path}
1891 \\caption{%labelcmd%caption}
1892 \\end{wrapfigure}")
1893 (floatp "\\begin{figure}%placement
1894 \\centering
1895 \\includegraphics[%attr]{%path}
1896 \\caption{%labelcmd%caption}
1897 \\end{figure}")
1898 (t "\\includegraphics[%attr]{%path}")))
1901 (setq figenv (mapconcat 'identity (split-string figenv "\n")
1902 (save-excursion (beginning-of-line 1)
1903 (looking-at "[ \t]*")
1904 (concat "\n" (match-string 0)))))
1906 (if (and (not label) (not caption)
1907 (string-match "^\\\\caption{.*\n" figenv))
1908 (setq figenv (replace-match "" t t figenv)))
1909 (org-add-props
1910 (org-fill-template
1911 figenv
1912 (list (cons "path"
1913 (if (file-name-absolute-p path)
1914 (expand-file-name path)
1915 path))
1916 (cons "attr" attr)
1917 (cons "labelcmd" (if label (format "\\label{%s}"
1918 label)""))
1919 (cons "caption" (or caption ""))
1920 (cons "placement" (or placement ""))))
1921 nil 'original-indentation ind)))
1923 (defun org-export-latex-protect-amp (s)
1924 (while (string-match "\\([^\\\\]\\)\\(&\\)" s)
1925 (setq s (replace-match (concat (match-string 1 s) "\\" (match-string 2 s))
1926 t t s)))
1929 (defun org-remove-initial-hash (s)
1930 (if (string-match "\\`#" s)
1931 (substring s 1)
1933 (defvar org-latex-entities) ; defined below
1934 (defvar org-latex-entities-regexp) ; defined below
1935 (defvar org-latex-entities-exceptions) ; defined below
1937 (defun org-export-latex-preprocess (parameters)
1938 "Clean stuff in the LaTeX export."
1939 ;; Preserve line breaks
1940 (goto-char (point-min))
1941 (while (re-search-forward "\\\\\\\\" nil t)
1942 (add-text-properties (match-beginning 0) (match-end 0)
1943 '(org-protected t)))
1945 ;; Preserve latex environments
1946 (goto-char (point-min))
1947 (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t)
1948 (let* ((start (progn (beginning-of-line) (point)))
1949 (end (and (re-search-forward
1950 (concat "^[ \t]*\\\\end{"
1951 (regexp-quote (match-string 1))
1952 "}") nil t)
1953 (point-at-eol))))
1954 (if end
1955 (add-text-properties start end '(org-protected t))
1956 (goto-char (point-at-eol)))))
1958 ;; Preserve math snippets
1960 (let* ((matchers (plist-get org-format-latex-options :matchers))
1961 (re-list org-latex-regexps)
1962 beg end re e m n block off)
1963 ;; Check the different regular expressions
1964 (while (setq e (pop re-list))
1965 (setq m (car e) re (nth 1 e) n (nth 2 e)
1966 block (if (nth 3 e) "\n\n" ""))
1967 (setq off (if (member m '("$" "$1")) 1 0))
1968 (when (and (member m matchers) (not (equal m "begin")))
1969 (goto-char (point-min))
1970 (while (re-search-forward re nil t)
1971 (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
1972 (add-text-properties beg end '(org-protected t org-latex-math t))))))
1974 ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{}
1975 (goto-char (point-min))
1976 (let ((case-fold-search nil))
1977 (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t)
1978 (unless (eq (char-before (match-beginning 1)) ?\\)
1979 (org-if-unprotected-1
1980 (replace-match (org-export-latex-protect-string
1981 (concat "\\" (match-string 1)
1982 "{}")) t t)))))
1984 ;; Convert blockquotes
1985 (goto-char (point-min))
1986 (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
1987 (org-replace-match-keep-properties "\\begin{quote}" t t))
1988 (goto-char (point-min))
1989 (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
1990 (org-replace-match-keep-properties "\\end{quote}" t t))
1992 ;; Convert verse
1993 (goto-char (point-min))
1994 (while (search-forward "ORG-VERSE-START" nil t)
1995 (org-replace-match-keep-properties "\\begin{verse}" t t)
1996 (beginning-of-line 2)
1997 (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
1998 (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
1999 (goto-char (match-end 1))
2000 (org-replace-match-keep-properties
2001 (org-export-latex-protect-string
2002 (concat "\\hspace*{1cm}" (match-string 2))) t t)
2003 (beginning-of-line 1))
2004 (if (looking-at "[ \t]*$")
2005 (insert (org-export-latex-protect-string "\\vspace*{1em}"))
2006 (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
2007 (end-of-line 1)
2008 (insert "\\\\")))
2009 (beginning-of-line 2))
2010 (and (looking-at "[ \t]*ORG-VERSE-END.*")
2011 (org-replace-match-keep-properties "\\end{verse}" t t)))
2013 ;; Convert center
2014 (goto-char (point-min))
2015 (while (search-forward "ORG-CENTER-START" nil t)
2016 (org-replace-match-keep-properties "\\begin{center}" t t))
2017 (goto-char (point-min))
2018 (while (search-forward "ORG-CENTER-END" nil t)
2019 (org-replace-match-keep-properties "\\end{center}" t t))
2021 (run-hooks 'org-export-latex-after-blockquotes-hook)
2023 ;; Convert horizontal rules
2024 (goto-char (point-min))
2025 (while (re-search-forward "^----+.$" nil t)
2026 (org-if-unprotected
2027 (replace-match (org-export-latex-protect-string "\\hrule") t t)))
2029 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
2030 (goto-char (point-min))
2031 (let ((re (concat
2032 "\\\\[a-zA-Z]+"
2033 "\\(?:<[^<>\n]*>\\)*"
2034 "\\(?:\\[[^][\n]*?\\]\\)*"
2035 "\\(?:<[^<>\n]*>\\)*"
2036 "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}")))
2037 (while (re-search-forward re nil t)
2038 (unless (save-excursion (goto-char (match-beginning 0))
2039 (equal (char-after (point-at-bol)) ?#))
2040 (add-text-properties (match-beginning 0) (match-end 0)
2041 '(org-protected t)))))
2043 ;; Protect LaTeX entities
2044 (goto-char (point-min))
2045 (let (a)
2046 (while (re-search-forward org-latex-entities-regexp nil t)
2047 (if (setq a (assoc (match-string 0) org-latex-entities-exceptions))
2048 (replace-match (org-add-props (nth 1 a) nil 'org-protected t)
2049 t t)
2050 (add-text-properties (match-beginning 0) (match-end 0)
2051 '(org-protected t)))))
2053 ;; Replace radio links
2054 (goto-char (point-min))
2055 (while (re-search-forward
2056 (concat "<<<?" org-export-latex-all-targets-re
2057 ">>>?\\((INVISIBLE)\\)?") nil t)
2058 (org-if-unprotected-at (+ (match-beginning 0) 2)
2059 (replace-match
2060 (concat
2061 (org-export-latex-protect-string
2062 (format "\\label{%s}" (save-match-data (org-solidify-link-text
2063 (match-string 1)))))
2064 (if (match-string 2) "" (match-string 1)))
2065 t t)))
2067 ;; Delete @<...> constructs
2068 ;; Thanks to Daniel Clemente for this regexp
2069 (goto-char (point-min))
2070 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
2071 (org-if-unprotected
2072 (replace-match "")))
2074 ;; When converting to LaTeX, replace footnotes
2075 ;; FIXME: don't protect footnotes from conversion
2076 (when (plist-get org-export-latex-options-plist :footnotes)
2077 (goto-char (point-min))
2078 (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t)
2079 (org-if-unprotected
2080 (when (and (save-match-data
2081 (save-excursion (beginning-of-line)
2082 (looking-at "[^:|#]")))
2083 (not (org-in-verbatim-emphasis)))
2084 (let ((foot-beg (match-beginning 0))
2085 (foot-end (match-end 0))
2086 (foot-prefix (match-string 0))
2087 footnote footnote-rpl)
2088 (save-excursion
2089 (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix))
2090 nil t))
2091 (replace-match (org-export-latex-protect-string
2092 (concat "$^{" (match-string 1) "}$")))
2093 (replace-match "")
2094 (let ((end (save-excursion
2095 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
2096 (match-beginning 0) (point-max)))))
2097 (setq footnote (concat (org-trim (buffer-substring (point) end))
2098 " ")) ; prevent last } being part of a link
2099 (delete-region (point) end))
2100 (goto-char foot-beg)
2101 (delete-region foot-beg foot-end)
2102 (unless (null footnote)
2103 (setq footnote-rpl (format "\\footnote{%s}" footnote))
2104 (add-text-properties 0 10 '(org-protected t) footnote-rpl)
2105 (add-text-properties (1- (length footnote-rpl))
2106 (length footnote-rpl)
2107 '(org-protected t) footnote-rpl)
2108 (if (org-on-heading-p)
2109 (setq footnote-rpl
2110 (concat (org-export-latex-protect-string "\\protect")
2111 footnote-rpl)))
2112 (insert footnote-rpl)))
2113 )))))
2115 ;; Remove footnote section tag for LaTeX
2116 (goto-char (point-min))
2117 (while (re-search-forward
2118 (concat "^" footnote-section-tag-regexp) nil t)
2119 (org-if-unprotected
2120 (replace-match "")))))
2122 (defun org-export-latex-fix-inputenc ()
2123 "Set the codingsystem in inputenc to what the buffer is."
2124 (let* ((cs buffer-file-coding-system)
2125 (opt (or (ignore-errors (latexenc-coding-system-to-inputenc cs))
2126 "utf8")))
2127 (when opt
2128 ;; Translate if that is requested
2129 (setq opt (or (cdr (assoc opt org-export-latex-inputenc-alist)) opt))
2130 ;; find the \usepackage statement and replace the option
2131 (goto-char (point-min))
2132 (while (re-search-forward "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
2133 nil t)
2134 (goto-char (match-beginning 1))
2135 (delete-region (match-beginning 1) (match-end 1))
2136 (insert opt))
2137 (and buffer-file-name
2138 (save-buffer)))))
2140 ;;; List handling:
2142 (defun org-export-latex-lists ()
2143 "Convert plain text lists in current buffer into LaTeX lists."
2144 (goto-char (point-min))
2145 (while (re-search-forward org-list-beginning-re nil t)
2146 (org-if-unprotected
2147 (beginning-of-line)
2148 (insert (org-list-to-latex (org-list-parse-list t)
2149 org-export-latex-list-parameters))
2150 "\n")))
2152 (defconst org-latex-entities
2153 '("\\!"
2154 "\\'"
2155 "\\+"
2156 "\\,"
2157 "\\-"
2158 "\\:"
2159 "\\;"
2160 "\\<"
2161 "\\="
2162 "\\>"
2163 "\\Huge"
2164 "\\LARGE"
2165 "\\Large"
2166 "\\Styles"
2167 "\\\\"
2168 "\\`"
2169 "\\addcontentsline"
2170 "\\address"
2171 "\\addtocontents"
2172 "\\addtocounter"
2173 "\\addtolength"
2174 "\\addvspace"
2175 "\\alph"
2176 "\\appendix"
2177 "\\arabic"
2178 "\\author"
2179 "\\begin{array}"
2180 "\\begin{center}"
2181 "\\begin{description}"
2182 "\\begin{enumerate}"
2183 "\\begin{eqnarray}"
2184 "\\begin{equation}"
2185 "\\begin{figure}"
2186 "\\begin{flushleft}"
2187 "\\begin{flushright}"
2188 "\\begin{itemize}"
2189 "\\begin{list}"
2190 "\\begin{minipage}"
2191 "\\begin{picture}"
2192 "\\begin{quotation}"
2193 "\\begin{quote}"
2194 "\\begin{tabbing}"
2195 "\\begin{table}"
2196 "\\begin{tabular}"
2197 "\\begin{thebibliography}"
2198 "\\begin{theorem}"
2199 "\\begin{titlepage}"
2200 "\\begin{verbatim}"
2201 "\\begin{verse}"
2202 "\\bf"
2203 "\\bf"
2204 "\\bibitem"
2205 "\\bigskip"
2206 "\\cdots"
2207 "\\centering"
2208 "\\circle"
2209 "\\cite"
2210 "\\cleardoublepage"
2211 "\\clearpage"
2212 "\\cline"
2213 "\\closing"
2214 "\\dashbox"
2215 "\\date"
2216 "\\ddots"
2217 "\\dotfill"
2218 "\\em"
2219 "\\fbox"
2220 "\\flushbottom"
2221 "\\fnsymbol"
2222 "\\footnote"
2223 "\\footnotemark"
2224 "\\footnotesize"
2225 "\\footnotetext"
2226 "\\frac"
2227 "\\frame"
2228 "\\framebox"
2229 "\\hfill"
2230 "\\hline"
2231 "\\hrulespace"
2232 "\\hspace"
2233 "\\huge"
2234 "\\hyphenation"
2235 "\\include"
2236 "\\includeonly"
2237 "\\indent"
2238 "\\input"
2239 "\\it"
2240 "\\kill"
2241 "\\label"
2242 "\\large"
2243 "\\ldots"
2244 "\\line"
2245 "\\linebreak"
2246 "\\linethickness"
2247 "\\listoffigures"
2248 "\\listoftables"
2249 "\\location"
2250 "\\makebox"
2251 "\\maketitle"
2252 "\\mark"
2253 "\\mbox"
2254 "\\medskip"
2255 "\\multicolumn"
2256 "\\multiput"
2257 ("\\nbsp" "~")
2258 "\\newcommand"
2259 "\\newcounter"
2260 "\\newenvironment"
2261 "\\newfont"
2262 "\\newlength"
2263 "\\newline"
2264 "\\newpage"
2265 "\\newsavebox"
2266 "\\newtheorem"
2267 "\\nocite"
2268 "\\nofiles"
2269 "\\noindent"
2270 "\\nolinebreak"
2271 "\\nopagebreak"
2272 "\\normalsize"
2273 "\\onecolumn"
2274 "\\opening"
2275 "\\oval"
2276 "\\overbrace"
2277 "\\overline"
2278 "\\pagebreak"
2279 "\\pagenumbering"
2280 "\\pageref"
2281 "\\pagestyle"
2282 "\\par"
2283 "\\parbox"
2284 "\\put"
2285 "\\raggedbottom"
2286 "\\raggedleft"
2287 "\\raggedright"
2288 "\\raisebox"
2289 "\\ref"
2290 "\\rm"
2291 "\\roman"
2292 "\\rule"
2293 "\\savebox"
2294 "\\sc"
2295 "\\scriptsize"
2296 "\\setcounter"
2297 "\\setlength"
2298 "\\settowidth"
2299 "\\sf"
2300 "\\shortstack"
2301 "\\signature"
2302 "\\sl"
2303 "\\small"
2304 "\\smallskip"
2305 "\\sqrt"
2306 "\\tableofcontents"
2307 "\\telephone"
2308 "\\thanks"
2309 "\\thispagestyle"
2310 "\\tiny"
2311 "\\title"
2312 "\\tt"
2313 "\\twocolumn"
2314 "\\typein"
2315 "\\typeout"
2316 "\\underbrace"
2317 "\\underline"
2318 "\\usebox"
2319 "\\usecounter"
2320 "\\value"
2321 "\\vdots"
2322 "\\vector"
2323 "\\verb"
2324 "\\vfill"
2325 "\\vline"
2326 "\\vspace")
2327 "A list of LaTeX commands to be protected when performing conversion.")
2329 (defvar org-latex-entities-exceptions nil)
2331 (defconst org-latex-entities-regexp
2332 (let (names rest)
2333 (dolist (x org-latex-entities)
2334 (when (consp x)
2335 (add-to-list 'org-latex-entities-exceptions x)
2336 (setq x (car x)))
2337 (if (string-match "[a-zA-Z]$" x)
2338 (push x names)
2339 (push x rest)))
2340 (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
2341 "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
2343 (provide 'org-export-latex)
2344 (provide 'org-latex)
2346 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
2348 ;;; org-latex.el ends here