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