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