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