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