Release 6.33b
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-latex.el
blob89822b24fbf88ccc4446d10c763b1de28bfaa8f3
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.33b
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 :timestamps (plist-get opt-plist :timestamps)
624 :todo-keywords (plist-get opt-plist :todo-keywords)
625 :add-text (if (eq to-buffer 'string) nil text)
626 :skip-before-1st-heading skip
627 :select-tags (plist-get opt-plist :select-tags)
628 :exclude-tags (plist-get opt-plist :exclude-tags)
629 :LaTeX-fragments nil)))
631 (set-buffer buffer)
632 (erase-buffer)
633 (org-install-letbind)
635 (and (fboundp 'set-buffer-file-coding-system)
636 (set-buffer-file-coding-system coding-system-for-write))
638 ;; insert the header and initial document commands
639 (unless (or (eq to-buffer 'string) body-only)
640 (insert header))
642 ;; insert text found in #+TEXT
643 (when (and text (not (eq to-buffer 'string)))
644 (insert (org-export-latex-content
645 text '(lists tables fixed-width keywords))
646 "\n\n"))
648 ;; insert lines before the first headline
649 (unless skip
650 (insert first-lines))
652 ;; export the content of headlines
653 (org-export-latex-global
654 (with-temp-buffer
655 (insert string-for-export)
656 (goto-char (point-min))
657 (when (re-search-forward "^\\(\\*+\\) " nil t)
658 (let* ((asters (length (match-string 1)))
659 (level (if odd (- asters 2) (- asters 1))))
660 (setq org-export-latex-add-level
661 (if odd (1- (/ (1+ asters) 2)) (1- asters)))
662 (org-export-latex-parse-global level odd)))))
664 ;; finalization
665 (unless body-only (insert "\n\\end{document}"))
667 ;; Relocate the table of contents
668 (goto-char (point-min))
669 (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
670 (goto-char (point-min))
671 (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t)
672 (replace-match ""))
673 (goto-char (point-min))
674 (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
675 (replace-match "\\tableofcontents" t t)))
677 (run-hooks 'org-export-latex-final-hook)
678 (or to-buffer (save-buffer))
679 (goto-char (point-min))
680 (or (org-export-push-to-kill-ring "LaTeX")
681 (message "Exporting to LaTeX...done"))
682 (prog1
683 (if (eq to-buffer 'string)
684 (prog1 (buffer-substring (point-min) (point-max))
685 (kill-buffer (current-buffer)))
686 (current-buffer))
687 (set-window-configuration wcf))))
689 ;;;###autoload
690 (defun org-export-as-pdf (arg &optional hidden ext-plist
691 to-buffer body-only pub-dir)
692 "Export as LaTeX, then process through to PDF."
693 (interactive "P")
694 (message "Exporting to PDF...")
695 (let* ((wconfig (current-window-configuration))
696 (lbuf (org-export-as-latex arg hidden ext-plist
697 to-buffer body-only pub-dir))
698 (file (buffer-file-name lbuf))
699 (base (file-name-sans-extension (buffer-file-name lbuf)))
700 (pdffile (concat base ".pdf"))
701 (cmds org-latex-to-pdf-process)
702 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
703 (bibtex-p (with-current-buffer lbuf
704 (save-excursion
705 (goto-char (point-min))
706 (re-search-forward "\\\\bibliography{" nil t))))
707 cmd)
708 (with-current-buffer outbuf (erase-buffer))
709 (and (file-exists-p pdffile) (delete-file pdffile))
710 (message "Processing LaTeX file...")
711 (if (and cmds (symbolp cmds))
712 (funcall cmds file)
713 (while cmds
714 (setq cmd (pop cmds))
715 (while (string-match "%b" cmd)
716 (setq cmd (replace-match
717 (save-match-data
718 (shell-quote-argument base))
719 t t cmd)))
720 (while (string-match "%s" cmd)
721 (setq cmd (replace-match
722 (save-match-data
723 (shell-quote-argument file))
724 t t cmd)))
725 (shell-command cmd outbuf outbuf)))
726 (message "Processing LaTeX file...done")
727 (if (not (file-exists-p pdffile))
728 (error "PDF file was not produced")
729 (set-window-configuration wconfig)
730 (when org-export-pdf-remove-logfiles
731 (dolist (ext '("aux" "log" "out" "toc"))
732 (setq file (concat base "." ext))
733 (and (file-exists-p file) (delete-file file))))
734 (message "Exporting to PDF...done")
735 pdffile)))
737 ;;;###autoload
738 (defun org-export-as-pdf-and-open (arg)
739 "Export as LaTeX, then process through to PDF, and open."
740 (interactive "P")
741 (let ((pdffile (org-export-as-pdf arg)))
742 (if pdffile
743 (org-open-file pdffile)
744 (error "PDF file was not produced"))))
746 ;;; Parsing functions:
748 (defun org-export-latex-parse-global (level odd)
749 "Parse the current buffer recursively, starting at LEVEL.
750 If ODD is non-nil, assume the buffer only contains odd sections.
751 Return a list reflecting the document structure."
752 (save-excursion
753 (goto-char (point-min))
754 (let* ((cnt 0) output
755 (depth org-export-latex-sectioning-depth))
756 (while (re-search-forward
757 (concat "^\\(\\(?:\\*\\)\\{"
758 (number-to-string (+ (if odd 2 1) level))
759 "\\}\\) \\(.*\\)$")
760 ;; make sure that there is no upper heading
761 (when (> level 0)
762 (save-excursion
763 (save-match-data
764 (re-search-forward
765 (concat "^\\(\\(?:\\*\\)\\{"
766 (number-to-string level)
767 "\\}\\) \\(.*\\)$") nil t)))) t)
768 (setq cnt (1+ cnt))
769 (let* ((pos (match-beginning 0))
770 (heading (match-string 2))
771 (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
772 (save-excursion
773 (narrow-to-region
774 (point)
775 (save-match-data
776 (if (re-search-forward
777 (concat "^\\(\\(?:\\*\\)\\{"
778 (number-to-string (+ (if odd 2 1) level))
779 "\\}\\) \\(.*\\)$") nil t)
780 (match-beginning 0)
781 (point-max))))
782 (goto-char (point-min))
783 (setq output
784 (append output
785 (list
786 (list
787 `(pos . ,pos)
788 `(level . ,nlevel)
789 `(occur . ,cnt)
790 `(heading . ,heading)
791 `(content . ,(org-export-latex-parse-content))
792 `(subcontent . ,(org-export-latex-parse-subcontent
793 level odd)))))))
794 (widen)))
795 (list output))))
797 (defun org-export-latex-parse-content ()
798 "Extract the content of a section."
799 (let ((beg (point))
800 (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t)
801 (progn (beginning-of-line) (point))
802 (point-max))))
803 (buffer-substring beg end)))
805 (defun org-export-latex-parse-subcontent (level odd)
806 "Extract the subcontent of a section at LEVEL.
807 If ODD Is non-nil, assume subcontent only contains odd sections."
808 (if (not (re-search-forward
809 (concat "^\\(\\(?:\\*\\)\\{"
810 (number-to-string (+ (if odd 4 2) level))
811 "\\}\\) \\(.*\\)$")
812 nil t))
813 nil ; subcontent is nil
814 (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
816 ;;; Rendering functions:
817 (defun org-export-latex-global (content)
818 "Export CONTENT to LaTeX.
819 CONTENT is an element of the list produced by
820 `org-export-latex-parse-global'."
821 (if (eq (car content) 'subcontent)
822 (mapc 'org-export-latex-sub (cdr content))
823 (org-export-latex-sub (car content))))
825 (defun org-export-latex-sub (subcontent)
826 "Export the list SUBCONTENT to LaTeX.
827 SUBCONTENT is an alist containing information about the headline
828 and its content."
829 (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
830 (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
832 (defun org-export-latex-subcontent (subcontent num)
833 "Export each cell of SUBCONTENT to LaTeX.
834 If NUM, export sections as numerical sections."
835 (let* ((heading (org-export-latex-fontify-headline
836 (cdr (assoc 'heading subcontent))))
837 (level (- (cdr (assoc 'level subcontent))
838 org-export-latex-add-level))
839 (occur (number-to-string (cdr (assoc 'occur subcontent))))
840 (content (cdr (assoc 'content subcontent)))
841 (subcontent (cadr (assoc 'subcontent subcontent)))
842 (label (org-get-text-property-any 0 'target heading))
843 (label-list (cons label (cdr (assoc label
844 org-export-target-aliases)))))
845 (cond
846 ;; Normal conversion
847 ((<= level org-export-latex-sectioning-depth)
848 (let* ((sec (nth (1- level) org-export-latex-sectioning))
849 start end)
850 (if (consp (cdr sec))
851 (setq start (nth (if num 0 2) sec)
852 end (nth (if num 1 3) sec))
853 (setq start (if num (car sec) (cdr sec))))
854 (insert (format start heading) "\n")
855 (when label
856 (insert (mapconcat (lambda (l) (format "\\label{%s}" l))
857 label-list "\n") "\n"))
858 (insert (org-export-latex-content content))
859 (cond ((stringp subcontent) (insert subcontent))
860 ((listp subcontent) (org-export-latex-sub subcontent)))
861 (if end (insert end "\n"))))
862 ;; At a level under the hl option: we can drop this subsection
863 ((> level org-export-latex-sectioning-depth)
864 (cond ((eq org-export-latex-low-levels 'description)
865 (if (string-match "% ends low level$"
866 (buffer-substring (point-at-bol 0) (point)))
867 (delete-region (point-at-bol 0) (point))
868 (insert "\\begin{description}\n"))
869 (insert (format "\n\\item[%s]%s~\n\n"
870 heading
871 (if label (format "\\label{%s}" label) "")))
872 (insert (org-export-latex-content content))
873 (cond ((stringp subcontent) (insert subcontent))
874 ((listp subcontent) (org-export-latex-sub subcontent)))
875 (insert "\\end{description} % ends low level\n"))
876 ((memq org-export-latex-low-levels '(itemize enumerate))
877 (if (string-match "% ends low level$"
878 (buffer-substring (point-at-bol 0) (point)))
879 (delete-region (point-at-bol 0) (point))
880 (insert (format "\\begin{%s}\n"
881 (symbol-name org-export-latex-low-levels))))
882 (insert (format "\n\\item %s\\\\\n%s\n"
883 heading
884 (if label (format "\\label{%s}" label) "")))
885 (insert (org-export-latex-content content))
886 (cond ((stringp subcontent) (insert subcontent))
887 ((listp subcontent) (org-export-latex-sub subcontent)))
888 (insert (format "\\end{%s} %% ends low level\n"
889 (symbol-name org-export-latex-low-levels))))
891 ((listp org-export-latex-low-levels)
892 (if (string-match "% ends low level$"
893 (buffer-substring (point-at-bol 0) (point)))
894 (delete-region (point-at-bol 0) (point))
895 (insert (car org-export-latex-low-levels) "\n"))
896 (insert (format (nth 2 org-export-latex-low-levels)
897 heading
898 (if label (format "\\label{%s}" label) "")))
899 (insert (org-export-latex-content content))
900 (cond ((stringp subcontent) (insert subcontent))
901 ((listp subcontent) (org-export-latex-sub subcontent)))
902 (insert (nth 1 org-export-latex-low-levels)
903 " %% ends low level\n"))
905 ((stringp org-export-latex-low-levels)
906 (insert (format org-export-latex-low-levels heading) "\n")
907 (when label (insert (format "\\label{%s}\n" label)))
908 (insert (org-export-latex-content content))
909 (cond ((stringp subcontent) (insert subcontent))
910 ((listp subcontent) (org-export-latex-sub subcontent)))))))))
912 ;;; Exporting internals:
913 (defun org-export-latex-set-initial-vars (ext-plist level)
914 "Store org local variables required for LaTeX export.
915 EXT-PLIST is an optional additional plist.
916 LEVEL indicates the default depth for export."
917 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
918 org-export-latex-done-keywords org-done-keywords
919 org-export-latex-not-done-keywords org-not-done-keywords
920 org-export-latex-complex-heading-re org-complex-heading-regexp
921 org-export-latex-display-custom-times org-display-custom-times
922 org-export-latex-all-targets-re
923 (org-make-target-link-regexp (org-all-targets))
924 org-export-latex-options-plist
925 (org-combine-plists (org-default-export-plist) ext-plist
926 (org-infile-export-plist))
927 org-export-latex-class
928 (or (and (org-region-active-p)
929 (save-excursion
930 (goto-char (region-beginning))
931 (and (looking-at org-complex-heading-regexp)
932 (org-entry-get nil "LaTeX_CLASS" 'selective))))
933 (save-excursion
934 (save-restriction
935 (widen)
936 (goto-char (point-min))
937 (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
938 (match-string 1))))
939 (plist-get org-export-latex-options-plist :latex-class)
940 org-export-latex-default-class)
941 org-export-latex-class
942 (or (car (assoc org-export-latex-class org-export-latex-classes))
943 (error "No definition for class `%s' in `org-export-latex-classes'"
944 org-export-latex-class))
945 org-export-latex-header
946 (cadr (assoc org-export-latex-class org-export-latex-classes))
947 org-export-latex-sectioning
948 (cddr (assoc org-export-latex-class org-export-latex-classes))
949 org-export-latex-sectioning-depth
950 (or level
951 (let ((hl-levels
952 (plist-get org-export-latex-options-plist :headline-levels))
953 (sec-depth (length org-export-latex-sectioning)))
954 (if (> hl-levels sec-depth) sec-depth hl-levels)))))
956 (defun org-export-latex-make-header (title opt-plist)
957 "Make the LaTeX header and return it as a string.
958 TITLE is the current title from the buffer or region.
959 OPT-PLIST is the options plist for current buffer."
960 (let ((toc (plist-get opt-plist :table-of-contents))
961 (author (plist-get opt-plist :author)))
962 (concat
963 (if (plist-get opt-plist :time-stamp-file)
964 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
965 ;; insert LaTeX custom header
966 (org-export-apply-macros-in-string org-export-latex-header)
967 "\n"
968 ;; insert information on LaTeX packages
969 (when org-export-latex-packages-alist
970 (mapconcat (lambda(p)
971 (if (equal "" (car p))
972 (format "\\usepackage{%s}" (cadr p))
973 (format "\\usepackage[%s]{%s}"
974 (car p) (cadr p))))
975 org-export-latex-packages-alist "\n"))
976 ;; insert additional commands in the header
977 (org-export-apply-macros-in-string
978 (plist-get opt-plist :latex-header-extra))
979 (org-export-apply-macros-in-string org-export-latex-append-header)
980 ;; insert the title
981 (format
982 "\n\n\\title{%s}\n"
983 ;; convert the title
984 (org-export-latex-content
985 title '(lists tables fixed-width keywords)))
986 ;; insert author info
987 (if (plist-get opt-plist :author-info)
988 (format "\\author{%s}\n"
989 (org-export-latex-fontify-headline (or author user-full-name)))
990 (format "%%\\author{%s}\n"
991 (or author user-full-name)))
992 ;; insert the date
993 (format "\\date{%s}\n"
994 (format-time-string
995 (or (plist-get opt-plist :date)
996 org-export-latex-date-format)))
997 ;; beginning of the document
998 "\n\\begin{document}\n\n"
999 ;; insert the title command
1000 (when (string-match "\\S-" title)
1001 (if (string-match "%s" org-export-latex-title-command)
1002 (format org-export-latex-title-command title)
1003 org-export-latex-title-command))
1004 "\n\n"
1005 ;; table of contents
1006 (when (and org-export-with-toc
1007 (plist-get opt-plist :section-numbers))
1008 (cond ((numberp toc)
1009 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1010 (min toc (plist-get opt-plist :headline-levels))))
1011 (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1012 (plist-get opt-plist :headline-levels)))))
1013 (when (plist-get opt-plist :preserve-breaks)
1014 "\\obeylines\n"))))
1016 (defun org-export-latex-first-lines (opt-plist &optional beg end)
1017 "Export the first lines before first headline.
1018 If BEG is non-nil, it is the beginning of the region.
1019 If END is non-nil, it is the end of the region."
1020 (save-excursion
1021 (goto-char (or beg (point-min)))
1022 (let* ((pt (point))
1023 (end (if (re-search-forward "^\\*+ " end t)
1024 (goto-char (match-beginning 0))
1025 (goto-char (or end (point-max))))))
1026 (prog1
1027 (org-export-latex-content
1028 (org-export-preprocess-string
1029 (buffer-substring pt end)
1030 :for-LaTeX t
1031 :emph-multiline t
1032 :add-text nil
1033 :comments nil
1034 :skip-before-1st-heading nil
1035 :LaTeX-fragments nil
1036 :timestamps (plist-get opt-plist :timestamps)
1037 :footnotes (plist-get opt-plist :footnotes)))
1038 (org-unmodified
1039 (add-text-properties pt (max pt (1- end))
1040 '(:org-license-to-kill t)))))))
1042 (defvar org-export-latex-header-defs nil
1043 "The header definitions that might be used in the LaTeX body.")
1044 (defvar org-export-latex-header-defs-re nil
1045 "The header definitions that might be used in the LaTeX body.")
1047 (defun org-export-latex-content (content &optional exclude-list)
1048 "Convert CONTENT string to LaTeX.
1049 Don't perform conversions that are in EXCLUDE-LIST. Recognized
1050 conversion types are: quotation-marks, emphasis, sub-superscript,
1051 links, keywords, lists, tables, fixed-width"
1052 (with-temp-buffer
1053 (insert content)
1054 (unless (memq 'timestamps exclude-list)
1055 (org-export-latex-time-stamps))
1056 (unless (memq 'quotation-marks exclude-list)
1057 (org-export-latex-quotation-marks))
1058 (unless (memq 'emphasis exclude-list)
1059 (when (plist-get org-export-latex-options-plist :emphasize)
1060 (org-export-latex-fontify)))
1061 (unless (memq 'sub-superscript exclude-list)
1062 (org-export-latex-special-chars
1063 (plist-get org-export-latex-options-plist :sub-superscript)))
1064 (unless (memq 'links exclude-list)
1065 (org-export-latex-links))
1066 (unless (memq 'keywords exclude-list)
1067 (org-export-latex-keywords))
1068 (unless (memq 'lists exclude-list)
1069 (org-export-latex-lists))
1070 (unless (memq 'tables exclude-list)
1071 (org-export-latex-tables
1072 (plist-get org-export-latex-options-plist :tables)))
1073 (unless (memq 'fixed-width exclude-list)
1074 (org-export-latex-fixed-width
1075 (plist-get org-export-latex-options-plist :fixed-width)))
1076 ;; return string
1077 (buffer-substring (point-min) (point-max))))
1079 (defun org-export-latex-protect-string (s)
1080 "Add the org-protected property to string S."
1081 (add-text-properties 0 (length s) '(org-protected t) s) s)
1083 (defun org-export-latex-protect-char-in-string (char-list string)
1084 "Add org-protected text-property to char from CHAR-LIST in STRING."
1085 (with-temp-buffer
1086 (save-match-data
1087 (insert string)
1088 (goto-char (point-min))
1089 (while (re-search-forward (regexp-opt char-list) nil t)
1090 (add-text-properties (match-beginning 0)
1091 (match-end 0) '(org-protected t)))
1092 (buffer-string))))
1094 (defun org-export-latex-keywords-maybe (&optional remove-list)
1095 "Maybe remove keywords depending on rules in REMOVE-LIST."
1096 (goto-char (point-min))
1097 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
1098 (case-fold-search nil)
1099 (todo-markup org-export-latex-todo-keyword-markup)
1100 fmt)
1101 ;; convert TODO keywords
1102 (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
1103 (if (plist-get remove-list :todo)
1104 (replace-match "")
1105 (setq fmt (cond
1106 ((stringp todo-markup) todo-markup)
1107 ((and (consp todo-markup) (stringp (car todo-markup)))
1108 (if (member (match-string 1) org-export-latex-done-keywords)
1109 (cdr todo-markup) (car todo-markup)))
1110 (t (cdr (or (assoc (match-string 1) todo-markup)
1111 (car todo-markup))))))
1112 (replace-match (format fmt (match-string 1)) t t)))
1113 ;; convert priority string
1114 (when (re-search-forward "\\[\\\\#.\\]" nil t)
1115 (if (plist-get remove-list :priority)
1116 (replace-match "")
1117 (replace-match (format "\\textbf{%s}" (match-string 0)) t t)))
1118 ;; convert tags
1119 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t)
1120 (if (or (not org-export-with-tags)
1121 (plist-get remove-list :tags))
1122 (replace-match "")
1123 (replace-match
1124 (org-export-latex-protect-string
1125 (format "\\textbf{%s}"
1126 (save-match-data
1127 (replace-regexp-in-string
1128 "_" "\\\\_" (match-string 0)))))
1129 t t)))))
1131 (defun org-export-latex-fontify-headline (string)
1132 "Fontify special words in STRING."
1133 (with-temp-buffer
1134 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
1135 ;; the beginning of the buffer - inserting "\n" is safe here though.
1136 (insert "\n" string)
1137 (goto-char (point-min))
1138 (let ((re (concat "\\\\[a-zA-Z]+\\(?:"
1139 "\\[.*\\]"
1140 "\\)?"
1141 (org-create-multibrace-regexp "{" "}" 3))))
1142 (while (re-search-forward re nil t)
1143 (unless (save-excursion (goto-char (match-beginning 0))
1144 (equal (char-after (point-at-bol)) ?#))
1145 (add-text-properties (match-beginning 0) (match-end 0)
1146 '(org-protected t)))))
1147 (when (plist-get org-export-latex-options-plist :emphasize)
1148 (org-export-latex-fontify))
1149 (org-export-latex-keywords-maybe)
1150 (org-export-latex-special-chars
1151 (plist-get org-export-latex-options-plist :sub-superscript))
1152 (org-export-latex-links)
1153 (org-trim (buffer-string))))
1155 (defun org-export-latex-time-stamps ()
1156 "Format time stamps."
1157 (goto-char (point-min))
1158 (let ((org-display-custom-times org-export-latex-display-custom-times))
1159 (while (re-search-forward org-ts-regexp-both nil t)
1160 (org-if-unprotected-at (1- (point))
1161 (replace-match
1162 (org-export-latex-protect-string
1163 (format org-export-latex-timestamp-markup
1164 (substring (org-translate-time (match-string 0)) 1 -1)))
1165 t t)))))
1167 (defun org-export-latex-quotation-marks ()
1168 "Export quotation marks depending on language conventions."
1169 (let* ((lang (plist-get org-export-latex-options-plist :language))
1170 (quote-rpl (if (equal lang "fr")
1171 '(("\\(\\s-\\)\"" "«~")
1172 ("\\(\\S-\\)\"" "~»")
1173 ("\\(\\s-\\)'" "`"))
1174 '(("\\(\\s-\\|[[(]\\)\"" "``")
1175 ("\\(\\S-\\)\"" "''")
1176 ("\\(\\s-\\|(\\)'" "`")))))
1177 (mapc (lambda(l) (goto-char (point-min))
1178 (while (re-search-forward (car l) nil t)
1179 (let ((rpl (concat (match-string 1)
1180 (org-export-latex-protect-string
1181 (copy-sequence (cadr l))))))
1182 (org-if-unprotected-1
1183 (replace-match rpl t t))))) quote-rpl)))
1185 (defun org-export-latex-special-chars (sub-superscript)
1186 "Export special characters to LaTeX.
1187 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
1188 See the `org-export-latex.el' code for a complete conversion table."
1189 (goto-char (point-min))
1190 (mapc (lambda(c)
1191 (goto-char (point-min))
1192 (while (re-search-forward c nil t)
1193 ;; Put the point where to check for org-protected
1194 (unless (get-text-property (match-beginning 2) 'org-protected)
1195 (cond ((member (match-string 2) '("\\$" "$"))
1196 (if (equal (match-string 2) "\\$")
1198 (replace-match "\\$" t t)))
1199 ((member (match-string 2) '("&" "%" "#"))
1200 (if (equal (match-string 1) "\\")
1201 (replace-match (match-string 2) t t)
1202 (replace-match (concat (match-string 1) "\\"
1203 (match-string 2)) t t)))
1204 ((equal (match-string 2) "...")
1205 (replace-match
1206 (concat (match-string 1)
1207 (org-export-latex-protect-string "\\ldots{}")) t t))
1208 ((equal (match-string 2) "~")
1209 (cond ((equal (match-string 1) "\\") nil)
1210 ((eq 'org-link (get-text-property 0 'face (match-string 2)))
1211 (replace-match (concat (match-string 1) "\\~") t t))
1212 (t (replace-match
1213 (org-export-latex-protect-string
1214 (concat (match-string 1) "\\~{}")) t t))))
1215 ((member (match-string 2) '("{" "}"))
1216 (unless (save-match-data (org-inside-latex-math-p))
1217 (if (equal (match-string 1) "\\")
1218 (replace-match (match-string 2) t t)
1219 (replace-match (concat (match-string 1) "\\"
1220 (match-string 2)) t t)))))
1221 (unless (save-match-data (org-inside-latex-math-p))
1222 (cond ((equal (match-string 2) "\\")
1223 (replace-match (or (save-match-data
1224 (org-export-latex-treat-backslash-char
1225 (match-string 1)
1226 (or (match-string 3) "")))
1227 "") t t))
1228 ((member (match-string 2) '("_" "^"))
1229 (replace-match (or (save-match-data
1230 (org-export-latex-treat-sub-super-char
1231 sub-superscript
1232 (match-string 2)
1233 (match-string 1)
1234 (match-string 3))) "") t t)
1235 (backward-char 1)))))))
1236 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
1237 "\\(\\(\\\\?\\$\\)\\)"
1238 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
1239 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
1240 "\\(.\\|^\\)\\(&\\)"
1241 "\\(.\\|^\\)\\(#\\)"
1242 "\\(.\\|^\\)\\(%\\)"
1243 "\\(.\\|^\\)\\({\\)"
1244 "\\(.\\|^\\)\\(}\\)"
1245 "\\(.\\|^\\)\\(~\\)"
1246 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
1247 ;; (?\< . "\\textless{}")
1248 ;; (?\> . "\\textgreater{}")
1251 (defun org-inside-latex-math-p ()
1252 (get-text-property (point) 'org-latex-math))
1254 (defun org-export-latex-treat-sub-super-char
1255 (subsup char string-before string-after)
1256 "Convert the \"_\" and \"^\" characters to LaTeX.
1257 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
1258 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
1259 (cond ((equal string-before "\\")
1260 (concat string-before char string-after))
1261 ((and (string-match "\\S-+" string-after))
1262 ;; this is part of a math formula
1263 (cond ((eq 'org-link (get-text-property 0 'face char))
1264 (concat string-before "\\" char string-after))
1265 ((save-match-data (org-inside-latex-math-p))
1266 (if subsup
1267 (cond ((eq 1 (length string-after))
1268 (concat string-before char string-after))
1269 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
1270 (format "%s%s{%s}" string-before char
1271 (match-string 1 string-after))))))
1272 ((and (> (length string-after) 1)
1273 (or (eq subsup t)
1274 (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
1275 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
1276 (org-export-latex-protect-string
1277 (format "%s$%s{%s}$" string-before char
1278 (if (and (> (match-end 1) (1+ (match-beginning 1)))
1279 (not (equal (substring string-after 0 2) "{\\")))
1280 (concat "\\mathrm{" (match-string 1 string-after) "}")
1281 (match-string 1 string-after)))))
1282 ((eq subsup t) (concat string-before "$" char string-after "$"))
1283 (t (org-export-latex-protect-string
1284 (concat string-before "\\" char "{}" string-after)))))
1285 (t (org-export-latex-protect-string
1286 (concat string-before "\\" char "{}" string-after)))))
1288 (defun org-export-latex-treat-backslash-char (string-before string-after)
1289 "Convert the \"$\" special character to LaTeX.
1290 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
1291 (cond ((member (list string-after) org-html-entities)
1292 ;; backslash is part of a special entity (like "\alpha")
1293 (concat string-before "$\\"
1294 (or (cdar (member (list string-after) org-html-entities))
1295 string-after) "$"))
1296 ((and (not (string-match "^[ \n\t]" string-after))
1297 (not (string-match "[ \t]\\'\\|^" string-before)))
1298 ;; backslash is inside a word
1299 (org-export-latex-protect-string
1300 (concat string-before "\\textbackslash{}" string-after)))
1301 ((not (or (equal string-after "")
1302 (string-match "^[ \t\n]" string-after)))
1303 ;; backslash might escape a character (like \#) or a user TeX
1304 ;; macro (like \setcounter)
1305 (org-export-latex-protect-string
1306 (concat string-before "\\" string-after)))
1307 ((and (string-match "^[ \t\n]" string-after)
1308 (string-match "[ \t\n]\\'" string-before))
1309 ;; backslash is alone, convert it to $\backslash$
1310 (org-export-latex-protect-string
1311 (concat string-before "\\textbackslash{}" string-after)))
1312 (t (org-export-latex-protect-string
1313 (concat string-before "\\textbackslash{}" string-after)))))
1315 (defun org-export-latex-keywords ()
1316 "Convert special keywords to LaTeX."
1317 (goto-char (point-min))
1318 (while (re-search-forward org-export-latex-special-keyword-regexp nil t)
1319 (replace-match (format org-export-latex-timestamp-keyword-markup
1320 (match-string 0)) t t)
1321 (save-excursion
1322 (beginning-of-line 1)
1323 (unless (looking-at ".*\\\\newline[ \t]*$")
1324 (end-of-line 1)
1325 (insert "\\newline")))))
1327 (defun org-export-latex-fixed-width (opt)
1328 "When OPT is non-nil convert fixed-width sections to LaTeX."
1329 (goto-char (point-min))
1330 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t)
1331 (if opt
1332 (progn (goto-char (match-beginning 0))
1333 (insert "\\begin{verbatim}\n")
1334 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1335 (replace-match (concat (match-string 1)
1336 (match-string 2)) t t)
1337 (forward-line))
1338 (insert "\\end{verbatim}\n\n"))
1339 (progn (goto-char (match-beginning 0))
1340 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1341 (replace-match (concat "%" (match-string 1)
1342 (match-string 2)) t t)
1343 (forward-line))))))
1346 (defvar org-table-last-alignment) ; defined in org-table.el
1347 (defvar org-table-last-column-widths) ; defined in org-table.el
1348 (declare-function orgtbl-to-latex "org-table" (table params) t)
1349 (defun org-export-latex-tables (insert)
1350 "Convert tables to LaTeX and INSERT it."
1351 (goto-char (point-min))
1352 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
1353 (org-if-unprotected-at (1- (point))
1354 (org-table-align)
1355 (let* ((beg (org-table-begin))
1356 (end (org-table-end))
1357 (raw-table (buffer-substring beg end))
1358 (org-table-last-alignment (copy-sequence org-table-last-alignment))
1359 (org-table-last-column-widths (copy-sequence
1360 org-table-last-column-widths))
1361 fnum fields line lines olines gr colgropen line-fmt align
1362 caption label attr floatp longtblp)
1363 (if org-export-latex-tables-verbatim
1364 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1365 "\\end{verbatim}\n")))
1366 (apply 'delete-region (list beg end))
1367 (insert (org-export-latex-protect-string tbl)))
1368 (progn
1369 (setq caption (org-find-text-property-in-string
1370 'org-caption raw-table)
1371 attr (org-find-text-property-in-string
1372 'org-attributes raw-table)
1373 label (org-find-text-property-in-string
1374 'org-label raw-table)
1375 longtblp (and attr (stringp attr)
1376 (string-match "\\<longtable\\>" attr))
1377 align (and attr (stringp attr)
1378 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
1379 (match-string 1 attr))
1380 floatp (or caption label))
1381 (setq lines (org-split-string raw-table "\n"))
1382 (apply 'delete-region (list beg end))
1383 (when org-export-table-remove-special-lines
1384 (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
1385 (when org-table-clean-did-remove-column
1386 (pop org-table-last-alignment)
1387 (pop org-table-last-column-widths))
1388 ;; make a formatting string to reflect alignment
1389 (setq olines lines)
1390 (while (and (not line-fmt) (setq line (pop olines)))
1391 (unless (string-match "^[ \t]*|-" line)
1392 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1393 (setq fnum (make-vector (length fields) 0))
1394 (setq line-fmt
1395 (mapconcat
1396 (lambda (x)
1397 (setq gr (pop org-table-colgroup-info))
1398 (format "%s%%s%s"
1399 (cond ((eq gr :start)
1400 (prog1 (if colgropen "|" "|")
1401 (setq colgropen t)))
1402 ((eq gr :startend)
1403 (prog1 (if colgropen "|" "|")
1404 (setq colgropen nil)))
1405 (t ""))
1406 (if (memq gr '(:end :startend))
1407 (progn (setq colgropen nil) "|")
1408 "")))
1409 fnum ""))))
1410 ;; fix double || in line-fmt
1411 (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
1412 ;; maybe remove the first and last "|"
1413 (when (and (not org-export-latex-tables-column-borders)
1414 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
1415 (setq line-fmt (match-string 2 line-fmt)))
1416 ;; format alignment
1417 (unless align
1418 (setq align (apply 'format
1419 (cons line-fmt
1420 (mapcar (lambda (x) (if x "r" "l"))
1421 org-table-last-alignment)))))
1422 ;; prepare the table to send to orgtbl-to-latex
1423 (setq lines
1424 (mapcar
1425 (lambda(elem)
1426 (or (and (string-match "[ \t]*|-+" elem) 'hline)
1427 (org-split-string (org-trim elem) "|")))
1428 lines))
1429 (when insert
1430 (insert (org-export-latex-protect-string
1431 (concat
1432 (if longtblp
1433 (concat "\\begin{longtable}{" align "}\n")
1434 (if floatp "\\begin{table}[htb]\n"))
1435 (if (or floatp longtblp)
1436 (format
1437 "\\caption{%s%s}"
1438 (if label (concat "\\\label{" label "}") "")
1439 (or caption "")))
1440 (if longtblp "\\\\\n" "\n")
1441 (if (and org-export-latex-tables-centered (not longtblp))
1442 "\\begin{center}\n")
1443 (if (not longtblp) (concat "\\begin{tabular}{" align "}\n"))
1444 (orgtbl-to-latex
1445 lines
1446 `(:tstart nil :tend nil
1447 :hlend ,(if longtblp
1448 (format "\\\\
1449 \\hline
1450 \\endhead
1451 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
1452 \\endfoot
1453 \\endlastfoot" (length org-table-last-alignment))
1454 nil)))
1455 (if (not longtblp) (concat "\n\\end{tabular}"))
1456 (if longtblp "\n" (if org-export-latex-tables-centered
1457 "\n\\end{center}\n" "\n"))
1458 (if longtblp
1459 "\\end{longtable}"
1460 (if floatp "\\end{table}"))))
1461 "\n\n"))))))))
1463 (defun org-export-latex-fontify ()
1464 "Convert fontification to LaTeX."
1465 (goto-char (point-min))
1466 (while (re-search-forward org-emph-re nil t)
1467 ;; The match goes one char after the *string*
1468 (let ((emph (assoc (match-string 3)
1469 org-export-latex-emphasis-alist))
1470 (beg (match-beginning 0))
1471 (end (match-end 0))
1472 rpl s)
1473 (unless emph
1474 (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
1475 (match-string 3)))
1476 (unless (or (and (get-text-property (- (point) 2) 'org-protected)
1477 (not (get-text-property
1478 (- (point) 2) 'org-verbatim-emph)))
1479 (save-excursion
1480 (goto-char (match-beginning 1))
1481 (save-match-data
1482 (and (org-at-table-p)
1483 (string-match
1484 "[|\n]" (buffer-substring beg end))))))
1485 (setq s (match-string 4))
1486 (setq rpl (concat (match-string 1)
1487 (org-export-latex-emph-format (cadr emph)
1488 (match-string 4))
1489 (match-string 5)))
1490 (if (caddr emph)
1491 (setq rpl (org-export-latex-protect-string rpl))
1492 (save-match-data
1493 (if (string-match "\\`.\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).\\'" rpl)
1494 (progn
1495 (add-text-properties (match-beginning 1) (match-end 1)
1496 '(org-protected t) rpl)
1497 (add-text-properties (match-beginning 3) (match-end 3)
1498 '(org-protected t) rpl)))))
1499 (replace-match rpl t t)))
1500 (backward-char)))
1502 (defvar org-export-latex-use-verb nil)
1503 (defun org-export-latex-emph-format (format string)
1504 "Format an emphasis string and handle the \\verb special case."
1505 (when (equal format "\\verb")
1506 (save-match-data
1507 (if org-export-latex-use-verb
1508 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1509 (catch 'exit
1510 (loop for i from 0 to (1- (length ll)) do
1511 (if (not (string-match (regexp-quote (substring ll i (1+ i)))
1512 string))
1513 (progn
1514 (setq format (concat "\\verb" (substring ll i (1+ i))
1515 "%s" (substring ll i (1+ i))))
1516 (throw 'exit nil))))))
1517 (let ((start 0)
1518 (trans '(("\\" . "\\textbackslash{}")
1519 ("~" . "\\textasciitilde{}")
1520 ("^" . "\\textasciicircum{}")))
1521 (rtn "") char)
1522 (while (string-match "[\\{}$%&_#~^]" string)
1523 (setq char (match-string 0 string))
1524 (if (> (match-beginning 0) 0)
1525 (setq rtn (concat rtn (substring string
1526 0 (match-beginning 0)))))
1527 (setq string (substring string (1+ (match-beginning 0))))
1528 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1529 rtn (concat rtn char)))
1530 (setq string (concat rtn string) format "\\texttt{%s}")))))
1531 (format format string))
1533 (defun org-export-latex-links ()
1534 ;; Make sure to use the LaTeX hyperref and graphicx package
1535 ;; or send some warnings.
1536 "Convert links to LaTeX."
1537 (goto-char (point-min))
1538 (while (re-search-forward org-bracket-link-analytic-regexp++ nil t)
1539 (org-if-unprotected-1
1540 (goto-char (match-beginning 0))
1541 (let* ((re-radio org-export-latex-all-targets-re)
1542 (remove (list (match-beginning 0) (match-end 0)))
1543 (raw-path (org-extract-attributes (match-string 3)))
1544 (full-raw-path (concat (match-string 1) raw-path))
1545 (desc (match-string 5))
1546 (type (or (match-string 2)
1547 (if (or (file-name-absolute-p raw-path)
1548 (string-match "^\\.\\.?/" raw-path))
1549 "file")))
1550 (coderefp (equal type "coderef"))
1551 (caption (org-find-text-property-in-string 'org-caption raw-path))
1552 (attr (or (org-find-text-property-in-string 'org-attributes raw-path)
1553 (plist-get org-export-latex-options-plist :latex-image-options)))
1554 (label (org-find-text-property-in-string 'org-label raw-path))
1555 imgp radiop
1556 ;; define the path of the link
1557 (path (cond
1558 ((member type '("coderef"))
1559 raw-path)
1560 ((member type '("http" "https" "ftp"))
1561 (concat type ":" raw-path))
1562 ((and re-radio (string-match re-radio raw-path))
1563 (setq radiop t))
1564 ((equal type "mailto")
1565 (concat type ":" raw-path))
1566 ((equal type "file")
1567 (if (and (org-file-image-p
1568 (expand-file-name
1569 raw-path)
1570 org-export-latex-inline-image-extensions)
1571 (or (get-text-property 0 'org-no-description
1572 raw-path)
1573 (equal desc full-raw-path)))
1574 (setq imgp t)
1575 (progn (when (string-match "\\(.+\\)::.+" raw-path)
1576 (setq raw-path (match-string 1 raw-path)))
1577 (if (file-exists-p raw-path)
1578 (concat type "://" (expand-file-name raw-path))
1579 (concat type "://" (org-export-directory
1580 :LaTeX org-export-latex-options-plist)
1581 raw-path))))))))
1582 ;; process with link inserting
1583 (apply 'delete-region remove)
1584 (cond ((and imgp
1585 (plist-get org-export-latex-options-plist :inline-images))
1586 ;; OK, we need to inline an image
1587 (insert
1588 (org-export-latex-format-image raw-path caption label attr)))
1589 (coderefp
1590 (insert (format
1591 (org-export-get-coderef-format path desc)
1592 (cdr (assoc path org-export-code-refs)))))
1593 (radiop (insert (format "\\hyperref[%s]{%s}"
1594 (org-solidify-link-text raw-path) desc)))
1595 ((not type)
1596 (insert (format "\\hyperref[%s]{%s}"
1597 (org-remove-initial-hash
1598 (org-solidify-link-text raw-path))
1599 desc)))
1600 (path
1601 (when (org-at-table-p)
1602 ;; There is a strange problem when we have a link in a table,
1603 ;; ampersands then cause a problem. I think this must be
1604 ;; a LaTeX issue, but we here implement a work-around anyway.
1605 (setq path (org-export-latex-protect-amp path)
1606 desc (org-export-latex-protect-amp desc)))
1607 (insert (format "\\href{%s}{%s}" path desc)))
1608 (t (insert "\\texttt{" desc "}")))))))
1611 (defun org-export-latex-format-image (path caption label attr)
1612 "Format the image element, depending on user settings."
1613 (let (floatp wrapp placement figenv)
1614 (setq floatp (or caption label))
1615 (when (and attr (stringp attr))
1616 (if (string-match "[ \t]*\\<wrap\\>" attr)
1617 (setq wrapp t floatp nil attr (replace-match "" t t attr)))
1618 (if (string-match "[ \t]*\\<float\\>" attr)
1619 (setq wrapp nil floatp t attr (replace-match "" t t attr))))
1621 (setq placement
1622 (cond
1623 (wrapp "{l}{0.5\\textwidth}")
1624 (floatp "[htb]")
1625 (t "")))
1627 (when (and attr (stringp attr)
1628 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr))
1629 (setq placement (match-string 1 attr)
1630 attr (replace-match "" t t attr)))
1631 (setq attr (and attr (org-trim attr)))
1632 (when (or (not attr) (= (length attr) 0))
1633 (setq attr (cond (floatp "width=0.7\\textwidth")
1634 (wrapp "width=0.48\\textwidth")
1635 (t attr))))
1636 (setq figenv
1637 (cond
1638 (wrapp "\\begin{wrapfigure}%placement
1639 \\centering
1640 \\includegraphics[%attr]{%path}
1641 \\caption{%labelcmd%caption}
1642 \\end{wrapfigure}")
1643 (floatp "\\begin{figure}%placement
1644 \\centering
1645 \\includegraphics[%attr]{%path}
1646 \\caption{%labelcmd%caption}
1647 \\end{figure}")
1648 (t "\\includegraphics[%attr]{%path}")))
1650 (if (and (not label) (not caption)
1651 (string-match "^\\\\caption{.*\n" figenv))
1652 (setq figenv (replace-match "" t t figenv)))
1653 (org-fill-template
1654 figenv
1655 (list (cons "path"
1656 (if (file-name-absolute-p path)
1657 (expand-file-name path)
1658 path))
1659 (cons "attr" attr)
1660 (cons "labelcmd" (if label (format "\\label{%s}"
1661 label)""))
1662 (cons "caption" (or caption ""))
1663 (cons "placement" (or placement ""))))))
1665 (defun org-export-latex-protect-amp (s)
1666 (while (string-match "\\([^\\\\]\\)\\(&\\)" s)
1667 (setq s (replace-match (concat (match-string 1 s) "\\" (match-string 2 s))
1668 t t s)))
1671 (defun org-remove-initial-hash (s)
1672 (if (string-match "\\`#" s)
1673 (substring s 1)
1675 (defvar org-latex-entities) ; defined below
1676 (defvar org-latex-entities-regexp) ; defined below
1677 (defvar org-latex-entities-exceptions) ; defined below
1679 (defun org-export-latex-preprocess (parameters)
1680 "Clean stuff in the LaTeX export."
1681 ;; Preserve line breaks
1682 (goto-char (point-min))
1683 (while (re-search-forward "\\\\\\\\" nil t)
1684 (add-text-properties (match-beginning 0) (match-end 0)
1685 '(org-protected t)))
1687 ;; Preserve latex environments
1688 (goto-char (point-min))
1689 (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t)
1690 (let* ((start (progn (beginning-of-line) (point)))
1691 (end (and (re-search-forward
1692 (concat "^[ \t]*\\\\end{"
1693 (regexp-quote (match-string 1))
1694 "}") nil t)
1695 (point-at-eol))))
1696 (if end
1697 (add-text-properties start end '(org-protected t))
1698 (goto-char (point-at-eol)))))
1700 ;; Preserve math snippets
1702 (let* ((matchers (plist-get org-format-latex-options :matchers))
1703 (re-list org-latex-regexps)
1704 beg end re e m n block off)
1705 ;; Check the different regular expressions
1706 (while (setq e (pop re-list))
1707 (setq m (car e) re (nth 1 e) n (nth 2 e)
1708 block (if (nth 3 e) "\n\n" ""))
1709 (setq off (if (member m '("$" "$1")) 1 0))
1710 (when (and (member m matchers) (not (equal m "begin")))
1711 (goto-char (point-min))
1712 (while (re-search-forward re nil t)
1713 (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
1714 (add-text-properties beg end '(org-protected t org-latex-math t))))))
1716 ;; Convert LaTeX to \LaTeX{}
1717 (goto-char (point-min))
1718 (let ((case-fold-search nil))
1719 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
1720 (org-if-unprotected
1721 (replace-match (org-export-latex-protect-string
1722 (concat (match-string 1) "\\LaTeX{}")) t t))))
1724 ;; Convert blockquotes
1725 (goto-char (point-min))
1726 (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
1727 (org-replace-match-keep-properties "\\begin{quote}" t t))
1728 (goto-char (point-min))
1729 (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
1730 (org-replace-match-keep-properties "\\end{quote}" t t))
1732 ;; Convert verse
1733 (goto-char (point-min))
1734 (while (search-forward "ORG-VERSE-START" nil t)
1735 (org-replace-match-keep-properties "\\begin{verse}" t t)
1736 (beginning-of-line 2)
1737 (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
1738 (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
1739 (goto-char (match-end 1))
1740 (org-replace-match-keep-properties
1741 (org-export-latex-protect-string
1742 (concat "\\hspace*{1cm}" (match-string 2))) t t)
1743 (beginning-of-line 1))
1744 (if (looking-at "[ \t]*$")
1745 (insert (org-export-latex-protect-string "\\vspace*{1em}"))
1746 (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
1747 (end-of-line 1)
1748 (insert "\\\\")))
1749 (beginning-of-line 2))
1750 (and (looking-at "[ \t]*ORG-VERSE-END.*")
1751 (org-replace-match-keep-properties "\\end{verse}" t t)))
1753 ;; Convert center
1754 (goto-char (point-min))
1755 (while (search-forward "ORG-CENTER-START" nil t)
1756 (org-replace-match-keep-properties "\\begin{center}" t t))
1757 (goto-char (point-min))
1758 (while (search-forward "ORG-CENTER-END" nil t)
1759 (org-replace-match-keep-properties "\\end{center}" t t))
1761 (run-hooks 'org-export-latex-after-blockquotes-hook)
1763 ;; Convert horizontal rules
1764 (goto-char (point-min))
1765 (while (re-search-forward "^----+.$" nil t)
1766 (org-if-unprotected
1767 (replace-match (org-export-latex-protect-string "\\hrule") t t)))
1769 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
1770 (let ((re (concat "\\\\[a-zA-Z]+\\(?:"
1771 "\\[.*\\]"
1772 "\\)?"
1773 (org-create-multibrace-regexp "{" "}" 3))))
1774 (while (re-search-forward re nil t)
1775 (unless (save-excursion (goto-char (match-beginning 0))
1776 (equal (char-after (point-at-bol)) ?#))
1777 (add-text-properties (match-beginning 0) (match-end 0)
1778 '(org-protected t)))))
1780 ;; Protect LaTeX entities
1781 (goto-char (point-min))
1782 (let (a)
1783 (while (re-search-forward org-latex-entities-regexp nil t)
1784 (if (setq a (assoc (match-string 0) org-latex-entities-exceptions))
1785 (replace-match (org-add-props (nth 1 a) nil 'org-protected t)
1786 t t)
1787 (add-text-properties (match-beginning 0) (match-end 0)
1788 '(org-protected t)))))
1790 ;; Replace radio links
1791 (goto-char (point-min))
1792 (while (re-search-forward
1793 (concat "<<<?" org-export-latex-all-targets-re
1794 ">>>?\\((INVISIBLE)\\)?") nil t)
1795 (org-if-unprotected-at (+ (match-beginning 0) 2)
1796 (replace-match
1797 (org-export-latex-protect-string
1798 (format "\\label{%s}%s" (save-match-data (org-solidify-link-text
1799 (match-string 1)))
1800 (if (match-string 2) "" (match-string 1)))) t t)))
1802 ;; Delete @<...> constructs
1803 ;; Thanks to Daniel Clemente for this regexp
1804 (goto-char (point-min))
1805 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
1806 (org-if-unprotected
1807 (replace-match "")))
1809 ;; When converting to LaTeX, replace footnotes
1810 ;; FIXME: don't protect footnotes from conversion
1811 (when (plist-get org-export-latex-options-plist :footnotes)
1812 (goto-char (point-min))
1813 (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t)
1814 (org-if-unprotected
1815 (when (and (save-match-data
1816 (save-excursion (beginning-of-line)
1817 (looking-at "[^:|#]")))
1818 (not (org-in-verbatim-emphasis)))
1819 (let ((foot-beg (match-beginning 0))
1820 (foot-end (match-end 0))
1821 (foot-prefix (match-string 0))
1822 footnote footnote-rpl)
1823 (save-excursion
1824 (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix))
1825 nil t))
1826 (replace-match "$^{\\1}$")
1827 (replace-match "")
1828 (let ((end (save-excursion
1829 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
1830 (match-beginning 0) (point-max)))))
1831 (setq footnote (concat (org-trim (buffer-substring (point) end))
1832 " ")) ; prevent last } being part of a link
1833 (delete-region (point) end))
1834 (goto-char foot-beg)
1835 (delete-region foot-beg foot-end)
1836 (unless (null footnote)
1837 (setq footnote-rpl (format "\\footnote{%s}" footnote))
1838 (add-text-properties 0 10 '(org-protected t) footnote-rpl)
1839 (add-text-properties (1- (length footnote-rpl))
1840 (length footnote-rpl)
1841 '(org-protected t) footnote-rpl)
1842 (insert footnote-rpl)))
1843 )))))
1845 ;; Remove footnote section tag for LaTeX
1846 (goto-char (point-min))
1847 (while (re-search-forward
1848 (concat "^" footnote-section-tag-regexp) nil t)
1849 (org-if-unprotected
1850 (replace-match "")))))
1852 ;;; List handling:
1854 (defun org-export-latex-lists ()
1855 "Convert plain text lists in current buffer into LaTeX lists."
1856 (goto-char (point-min))
1857 (while (re-search-forward org-list-beginning-re nil t)
1858 (org-if-unprotected
1859 (beginning-of-line)
1860 (insert (org-list-to-latex (org-list-parse-list t)
1861 org-export-latex-list-parameters))
1862 "\n")))
1864 (defconst org-latex-entities
1865 '("\\!"
1866 "\\'"
1867 "\\+"
1868 "\\,"
1869 "\\-"
1870 "\\:"
1871 "\\;"
1872 "\\<"
1873 "\\="
1874 "\\>"
1875 "\\Huge"
1876 "\\LARGE"
1877 "\\Large"
1878 "\\Styles"
1879 "\\\\"
1880 "\\`"
1881 "\\addcontentsline"
1882 "\\address"
1883 "\\addtocontents"
1884 "\\addtocounter"
1885 "\\addtolength"
1886 "\\addvspace"
1887 "\\alph"
1888 "\\appendix"
1889 "\\arabic"
1890 "\\author"
1891 "\\begin{array}"
1892 "\\begin{center}"
1893 "\\begin{description}"
1894 "\\begin{enumerate}"
1895 "\\begin{eqnarray}"
1896 "\\begin{equation}"
1897 "\\begin{figure}"
1898 "\\begin{flushleft}"
1899 "\\begin{flushright}"
1900 "\\begin{itemize}"
1901 "\\begin{list}"
1902 "\\begin{minipage}"
1903 "\\begin{picture}"
1904 "\\begin{quotation}"
1905 "\\begin{quote}"
1906 "\\begin{tabbing}"
1907 "\\begin{table}"
1908 "\\begin{tabular}"
1909 "\\begin{thebibliography}"
1910 "\\begin{theorem}"
1911 "\\begin{titlepage}"
1912 "\\begin{verbatim}"
1913 "\\begin{verse}"
1914 "\\bf"
1915 "\\bf"
1916 "\\bibitem"
1917 "\\bigskip"
1918 "\\cdots"
1919 "\\centering"
1920 "\\circle"
1921 "\\cite"
1922 "\\cleardoublepage"
1923 "\\clearpage"
1924 "\\cline"
1925 "\\closing"
1926 "\\dashbox"
1927 "\\date"
1928 "\\ddots"
1929 "\\dotfill"
1930 "\\em"
1931 "\\fbox"
1932 "\\flushbottom"
1933 "\\fnsymbol"
1934 "\\footnote"
1935 "\\footnotemark"
1936 "\\footnotesize"
1937 "\\footnotetext"
1938 "\\frac"
1939 "\\frame"
1940 "\\framebox"
1941 "\\hfill"
1942 "\\hline"
1943 "\\hrulespace"
1944 "\\hspace"
1945 "\\huge"
1946 "\\hyphenation"
1947 "\\include"
1948 "\\includeonly"
1949 "\\indent"
1950 "\\input"
1951 "\\it"
1952 "\\kill"
1953 "\\label"
1954 "\\large"
1955 "\\ldots"
1956 "\\line"
1957 "\\linebreak"
1958 "\\linethickness"
1959 "\\listoffigures"
1960 "\\listoftables"
1961 "\\location"
1962 "\\makebox"
1963 "\\maketitle"
1964 "\\mark"
1965 "\\mbox"
1966 "\\medskip"
1967 "\\multicolumn"
1968 "\\multiput"
1969 ("\\nbsp" "~")
1970 "\\newcommand"
1971 "\\newcounter"
1972 "\\newenvironment"
1973 "\\newfont"
1974 "\\newlength"
1975 "\\newline"
1976 "\\newpage"
1977 "\\newsavebox"
1978 "\\newtheorem"
1979 "\\nocite"
1980 "\\nofiles"
1981 "\\noindent"
1982 "\\nolinebreak"
1983 "\\nopagebreak"
1984 "\\normalsize"
1985 "\\onecolumn"
1986 "\\opening"
1987 "\\oval"
1988 "\\overbrace"
1989 "\\overline"
1990 "\\pagebreak"
1991 "\\pagenumbering"
1992 "\\pageref"
1993 "\\pagestyle"
1994 "\\par"
1995 "\\parbox"
1996 "\\put"
1997 "\\raggedbottom"
1998 "\\raggedleft"
1999 "\\raggedright"
2000 "\\raisebox"
2001 "\\ref"
2002 "\\rm"
2003 "\\roman"
2004 "\\rule"
2005 "\\savebox"
2006 "\\sc"
2007 "\\scriptsize"
2008 "\\setcounter"
2009 "\\setlength"
2010 "\\settowidth"
2011 "\\sf"
2012 "\\shortstack"
2013 "\\signature"
2014 "\\sl"
2015 "\\small"
2016 "\\smallskip"
2017 "\\sqrt"
2018 "\\tableofcontents"
2019 "\\telephone"
2020 "\\thanks"
2021 "\\thispagestyle"
2022 "\\tiny"
2023 "\\title"
2024 "\\tt"
2025 "\\twocolumn"
2026 "\\typein"
2027 "\\typeout"
2028 "\\underbrace"
2029 "\\underline"
2030 "\\usebox"
2031 "\\usecounter"
2032 "\\value"
2033 "\\vdots"
2034 "\\vector"
2035 "\\verb"
2036 "\\vfill"
2037 "\\vline"
2038 "\\vspace")
2039 "A list of LaTeX commands to be protected when performing conversion.")
2041 (defvar org-latex-entities-exceptions nil)
2043 (defconst org-latex-entities-regexp
2044 (let (names rest)
2045 (dolist (x org-latex-entities)
2046 (when (consp x)
2047 (add-to-list 'org-latex-entities-exceptions x)
2048 (setq x (car x)))
2049 (if (string-match "[a-zA-Z]$" x)
2050 (push x names)
2051 (push x rest)))
2052 (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
2053 "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
2055 (provide 'org-export-latex)
2056 (provide 'org-latex)
2058 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
2060 ;;; org-latex.el ends here