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