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