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