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