org-latex.el 68.7 KB
Newer Older
1
;;; org-latex.el --- LaTeX exporter for org-mode
Carsten Dominik's avatar
Carsten Dominik committed
2
;;
Glenn Morris's avatar
Glenn Morris committed
3
;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
4 5
;;
;; Emacs Lisp Archive Entry
6
;; Filename: org-latex.el
7
;; Version: 6.31a
Carsten Dominik's avatar
Carsten Dominik committed
8
;; Author: Bastien Guerry <bzg AT altern DOT org>
9
;; Maintainer: Carsten Dominik <carsten.dominik AT gmail DOT com>
10 11
;; Keywords: org, wp, tex
;; Description: Converts an org-mode buffer into LaTeX
12

Carsten Dominik's avatar
Carsten Dominik committed
13
;; This file is part of GNU Emacs.
14 15 16 17 18 19 20 21 22 23 24

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

25
;; You should have received a copy of the GNU General Public License
26 27
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

Carsten Dominik's avatar
Carsten Dominik committed
28
;;; Commentary:
John Wiegley's avatar
John Wiegley committed
29 30
;;
;; This library implements a LaTeX exporter for org-mode.
31
;;
32
;; It is part of Org and will be autoloaded
33
;;
Carsten Dominik's avatar
Carsten Dominik committed
34
;; The interactive functions are similar to those of the HTML exporter:
35
;;
Carsten Dominik's avatar
Carsten Dominik committed
36
;; M-x `org-export-as-latex'
37 38
;; M-x `org-export-as-pdf'
;; M-x `org-export-as-pdf-and-open'
Carsten Dominik's avatar
Carsten Dominik committed
39 40 41 42
;; M-x `org-export-as-latex-batch'
;; M-x `org-export-as-latex-to-buffer'
;; M-x `org-export-region-as-latex'
;; M-x `org-replace-region-by-latex'
43
;;
Carsten Dominik's avatar
Carsten Dominik committed
44 45
;;; Code:

John Wiegley's avatar
John Wiegley committed
46 47 48
(eval-when-compile
  (require 'cl))

Carsten Dominik's avatar
Carsten Dominik committed
49
(require 'footnote)
John Wiegley's avatar
John Wiegley committed
50
(require 'org)
51
(require 'org-exp)
52
(require 'org-macs)
Carsten Dominik's avatar
Carsten Dominik committed
53

John Wiegley's avatar
John Wiegley committed
54
;;; Variables:
55 56 57 58 59
(defvar org-export-latex-class nil)
(defvar org-export-latex-header nil)
(defvar org-export-latex-append-header nil)
(defvar org-export-latex-options-plist nil)
(defvar org-export-latex-todo-keywords-1 nil)
60 61 62 63
(defvar org-export-latex-complex-heading-re nil)
(defvar org-export-latex-not-done-keywords nil)
(defvar org-export-latex-done-keywords nil)
(defvar org-export-latex-display-custom-times nil)
64 65 66 67
(defvar org-export-latex-all-targets-re nil)
(defvar org-export-latex-add-level 0)
(defvar org-export-latex-sectioning "")
(defvar org-export-latex-sectioning-depth 0)
68 69 70 71 72
(defvar org-export-latex-special-keyword-regexp
  (concat "\\<\\(" org-scheduled-string "\\|"
	  org-deadline-string "\\|"
	  org-closed-string"\\)")
  "Regexp matching special time planning keywords plus the time after it.")
Carsten Dominik's avatar
Carsten Dominik committed
73

John Wiegley's avatar
John Wiegley committed
74 75 76 77
(defvar latexp)    ; dynamically scoped from org.el
(defvar re-quote)  ; dynamically scoped from org.el
(defvar commentsp) ; dynamically scoped from org.el

78
;;; User variables:
Carsten Dominik's avatar
Carsten Dominik committed
79

80 81 82 83 84
(defgroup org-export-latex nil
  "Options for exporting Org-mode files to LaTeX."
  :tag "Org Export LaTeX"
  :group 'org-export)

85 86 87 88
(defcustom org-export-latex-default-class "article"
  "The default LaTeX class."
  :group 'org-export-latex
  :type '(string :tag "LaTeX class"))
Carsten Dominik's avatar
Carsten Dominik committed
89

90 91
(defcustom org-export-latex-classes
  '(("article"
92
     "\\documentclass[11pt]{article}
93 94
\\usepackage[utf8]{inputenc}
\\usepackage[T1]{fontenc}
95
\\usepackage{graphicx}
96
\\usepackage{longtable}
97
\\usepackage{soul}
98 99 100 101 102 103 104
\\usepackage{hyperref}"
     ("\\section{%s}" . "\\section*{%s}")
     ("\\subsection{%s}" . "\\subsection*{%s}")
     ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
     ("\\paragraph{%s}" . "\\paragraph*{%s}")
     ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
    ("report"
105
     "\\documentclass[11pt]{report}
106 107
\\usepackage[utf8]{inputenc}
\\usepackage[T1]{fontenc}
108
\\usepackage{graphicx}
109
\\usepackage{longtable}
110
\\usepackage{soul}
111 112 113 114 115 116 117
\\usepackage{hyperref}"
     ("\\part{%s}" . "\\part*{%s}")
     ("\\chapter{%s}" . "\\chapter*{%s}")
     ("\\section{%s}" . "\\section*{%s}")
     ("\\subsection{%s}" . "\\subsection*{%s}")
     ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
    ("book"
118
     "\\documentclass[11pt]{book}
119 120
\\usepackage[utf8]{inputenc}
\\usepackage[T1]{fontenc}
121
\\usepackage{graphicx}
122
\\usepackage{longtable}
123
\\usepackage{soul}
124 125 126 127 128 129 130 131 132 133 134 135
\\usepackage{hyperref}"
     ("\\part{%s}" . "\\part*{%s}")
     ("\\chapter{%s}" . "\\chapter*{%s}")
     ("\\section{%s}" . "\\section*{%s}")
     ("\\subsection{%s}" . "\\subsection*{%s}")
     ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
  "Alist of LaTeX classes and associated header and structure.
If #+LaTeX_CLASS is set in the buffer, use its value and the
associated information.  Here is the structure of each cell:

  \(class-name
    header-string
136
    (numbered-section . unnumbered-section\)
137 138 139
    ...\)

A %s formatter is mandatory in each section string and will be
140 141 142 143 144 145 146 147 148 149 150 151 152 153
replaced by the title of the section.

Instead of a cons cell (numbered . unnumbered), you can also provide a list
of 2-4 elements,

  (numbered-open numbered-close)

or

  (numbered-open numbered-close unnumbered-open unnumbered-close)

providing opening and closing strings for an environment that should
represent the document section.  The opening clause should have a %s
to represent the section title."
Carsten Dominik's avatar
Carsten Dominik committed
154
  :group 'org-export-latex
155
  :type '(repeat
156 157
	  (list (string :tag "LaTeX class")
		(string :tag "LaTeX header")
158 159 160 161 162 163 164 165 166 167
		(repeat :tag "Levels" :inline t
			(choice
			 (cons :tag "Heading"
			       (string :tag "numbered")
			       (string :tag "unnumbered)"))
			 (list :tag "Environment"
			       (string :tag "Opening (numbered)  ")
			       (string :tag "Closing (numbered)  ")
			       (string :tag "Opening (unnumbered)")
			       (string :tag "Closing (unnumbered)")))))))
Carsten Dominik's avatar
Carsten Dominik committed
168 169

(defcustom org-export-latex-emphasis-alist
170 171 172
  '(("*" "\\textbf{%s}" nil)
    ("/" "\\emph{%s}" nil)
    ("_" "\\underline{%s}" nil)
173
    ("+" "\\st{%s}" nil)
174 175
    ("=" "\\verb" t)
    ("~" "\\verb" t))
176 177 178 179
  "Alist of LaTeX expressions to convert emphasis fontifiers.
Each element of the list is a list of three elements.
The first element is the character used as a marker for fontification.
The second element is a formatting string to wrap fontified text with.
180 181
If it is \"\\verb\", Org will automatically select a deimiter
character that is not in the string.
182 183
The third element decides whether to protect converted text from other
conversions."
Carsten Dominik's avatar
Carsten Dominik committed
184 185 186
  :group 'org-export-latex
  :type 'alist)

187 188 189 190 191 192 193 194
(defcustom org-export-latex-title-command "\\maketitle"
  "The command used to insert the title just after \\begin{document}.
If this string contains the formatting specification \"%s\" then
it will be used as a formatting string, passing the title as an
argument."
  :group 'org-export-latex
  :type 'string)

195 196 197 198 199 200
(defcustom org-export-latex-import-inbuffer-stuff nil
  "Non-nil means define TeX macros for Org's inbuffer definitions.
For example \orgTITLE for #+TITLE."
  :group 'org-export-latex
  :type 'boolean)

201
(defcustom org-export-latex-date-format
202
  "%d %B %Y"
Carsten Dominik's avatar
Carsten Dominik committed
203 204 205 206
  "Format string for \\date{...}."
  :group 'org-export-latex
  :type 'string)

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
(defcustom org-export-latex-todo-keyword-markup "\\textbf{%s}"
  "Markup for TODO keywords, as a printf format.
This can be a single format for all keywords, a cons cell with separate
formats for not-done and done states, or an association list with setup
for individual keywords.  If a keyword shows up for which there is no
markup defined, the first one in the association list will be used."
  :group 'org-export-latex
  :type '(choice
	  (string :tag "Default")
	  (cons :tag "Distinguish undone and done"
		(string :tag "Not-DONE states")
		(string :tag "DONE states"))
	  (repeat :tag "Per keyword markup"
		  (cons
		   (string :tag "Keyword")
		   (string :tag "Markup")))))

(defcustom org-export-latex-timestamp-markup "\\textit{%s}"
  "A printf format string to be applied to time stamps."
  :group 'org-export-latex
  :type 'string)

(defcustom org-export-latex-timestamp-keyword-markup "\\texttt{%s}"
  "A printf format string to be applied to time stamps."
  :group 'org-export-latex
  :type 'string)

234
(defcustom org-export-latex-tables-verbatim nil
235
  "When non-nil, tables are exported verbatim."
236 237 238
  :group 'org-export-latex
  :type 'boolean)

239 240 241 242 243
(defcustom org-export-latex-tables-centered t
  "When non-nil, tables are exported in a center environment."
  :group 'org-export-latex
  :type 'boolean)

244
(defcustom org-export-latex-tables-column-borders nil
245 246
  "When non-nil, grouping columns can cause outer vertical lines in tables.
When nil, grouping causes only separation lines between groups."
247 248
  :group 'org-export-latex
  :type 'boolean)
Carsten Dominik's avatar
Carsten Dominik committed
249

250 251
(defcustom org-export-latex-packages-alist nil
  "Alist of packages to be inserted in the header.
252
Each cell is of the format \( \"option\" . \"package\" \)."
Carsten Dominik's avatar
Carsten Dominik committed
253
  :group 'org-export-latex
254 255 256 257
  :type '(repeat
	  (list
	   (string :tag "option")
	   (string :tag "package"))))
Carsten Dominik's avatar
Carsten Dominik committed
258

259
(defcustom org-export-latex-low-levels 'itemize
260 261 262
  "How to convert sections below the current level of sectioning.
This is specified by the `org-export-headline-levels' option or the
value of \"H:\" in Org's #+OPTION line.
263

264 265 266 267 268 269 270 271 272 273
This can be either nil (skip the sections), `description', `itemize',
or `enumerate' (convert the sections as the corresponding list type), or
a string to be used instead of \\section{%s}.  In this latter case,
the %s stands here for the inserted headline and is mandatory.

It may also be a list of three string to define a user-defined environment
that should be used.  The first string should be the like
\"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
to two occurrences of %s for the title and a lable, respectively.  The third
string should be like \"\\end{itemize\"."
Carsten Dominik's avatar
Carsten Dominik committed
274 275
  :group 'org-export-latex
  :type '(choice (const :tag "Ignore" nil)
276 277 278 279 280 281 282 283
		 (const :tag "Convert as descriptive list" description)
		 (const :tag "Convert as itemized list" itemize)
		 (const :tag "Convert as enumerated list" enumerate)
		 (list  :tag "User-defined environment"
			:value ("\\begin{itemize}" "\\end{itemize}" "\\item %s")
			(string :tag "Start")
			(string :tag "End")
			(string :tag "item"))
Carsten Dominik's avatar
Carsten Dominik committed
284 285
		 (string :tag "Use a section string" :value "\\subparagraph{%s}")))

286
(defcustom org-export-latex-list-parameters
287
  '(:cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
288 289 290 291 292 293 294
  "Parameters for the LaTeX list exporter.
These parameters will be passed on to `org-list-to-latex', which in turn
will pass them (combined with the LaTeX default list parameters) to
`org-list-to-generic'."
  :group 'org-export-latex
  :type 'plist)

295 296 297 298 299 300 301 302 303 304 305 306
(defcustom org-export-latex-verbatim-wrap
  '("\\begin{verbatim}\n" . "\\end{verbatim}\n")
  "Environment to be wrapped around a fixed-width section in LaTeX export.
This is a cons with two strings, to be added before and after the
fixed-with text.

Defaults to \\begin{verbatim} and \\end{verbatim}."
  :group 'org-export-translation
  :group 'org-export-latex
  :type '(cons (string :tag "Open")
	       (string :tag "Close")))

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
(defcustom org-export-latex-listings nil
  "Non-nil means, export source code using the listings package.
This package will fontify source code, possibly even with color.
If you want to use this, you also need to make LaTeX use the
listings package, and if you want to have color, the color
package.  Just add these to `org-export-latex-packages-alist',
for example using customize, or with something like

  (require 'org-latex)
  (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
  (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))"
  :group 'org-export-latex
  :type 'boolean)

(defcustom org-export-latex-listings-langs
  '((emacs-lisp "Lisp") (lisp "Lisp")
    (c "C") (cc "C++")
    (fortran "fortran")
    (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
    (html "HTML") (xml "XML")
    (tex "TeX") (latex "TeX")
    (shell-script "bash")
    (gnuplot "Gnuplot")
    (ocaml "Caml") (caml "Caml")
    (sql "SQL"))
  "Alist mapping languages to their listing language counterpart.
The key is a symbol, the major mode symbol without the \"-mode\".
The value is the string that should be inserted as the language parameter
for the listings package.  If the mode name and the listings name are
the same, the language does not need an entry in this list - but it does not
hurt if it is present."
  :group 'org-export-latex
  :type '(repeat
	  (list
	   (symbol :tag "Major mode       ")
	   (string :tag "Listings language"))))

344
(defcustom org-export-latex-remove-from-headlines
345 346
  '(:todo nil :priority nil :tags nil)
  "A plist of keywords to remove from headlines. OBSOLETE.
Carsten Dominik's avatar
Carsten Dominik committed
347 348
Non-nil means remove this keyword type from the headline.

349 350 351 352 353
Don't remove the keys, just change their values.

Obsolete, this variable is no longer used.  Use the separate
variables `org-export-with-todo-keywords', `org-export-with-priority',
and `org-export-with-tags' instead."
Carsten Dominik's avatar
Carsten Dominik committed
354 355 356 357 358 359
  :type 'plist
  :group 'org-export-latex)

(defcustom org-export-latex-image-default-option "width=10em"
  "Default option for images."
  :group 'org-export-latex
360
  :type 'string)
Carsten Dominik's avatar
Carsten Dominik committed
361

362
(defcustom org-export-latex-inline-image-extensions
363
  '("pdf" "jpeg" "jpg" "png" "ps" "eps")
364
  "Extensions of image files that can be inlined into LaTeX.
365 366 367 368 369
Note that the image extension *actually* allowed depend on the way the
LaTeX file is processed.  When used with pdflatex, pdf, jpg and png images
are OK.  When processing through dvi to Postscript, only ps and eps are
allowed.  The default we use here encompasses both."
  :group 'org-export-latex
370 371
  :type '(repeat (string :tag "Extension")))

Carsten Dominik's avatar
Carsten Dominik committed
372 373 374 375 376
(defcustom org-export-latex-coding-system nil
  "Coding system for the exported LaTex file."
  :group 'org-export-latex
  :type 'coding-system)

377 378 379 380 381 382
(defgroup org-export-pdf nil
  "Options for exporting Org-mode files to PDF, via LaTeX."
  :tag "Org Export LaTeX"
  :group 'org-export-latex
  :group 'org-export)

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
(defcustom org-latex-to-pdf-process
  '("pdflatex -interaction nonstopmode %s"
    "pdflatex -interaction nonstopmode %s")
  "Commands to process a LaTeX file to a PDF file.
This is a list of strings, each of them will be given to the shell
as a command.  %s in the command will be replaced by the full file name, %b
by the file base name (i.e. without extension).
The reason why this is a list is that it usually takes several runs of
pdflatex, maybe mixed with a call to bibtex.  Org does not have a clever
mechanism to detect whihc of these commands have to be run to get to a stable
result, and it also does not do any error checking.

Alternatively, this may be a Lisp function that does the processing, so you
could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
THis function should accept the file name as its single argument."
  :group 'org-export-latex
  :type '(choice (repeat :tag "Shell command sequence"
		  (string :tag "Shell command"))
		 (function)))

403 404 405
(defcustom org-export-pdf-remove-logfiles t
  "Non-nil means, remove the logfiles produced by PDF production.
These are the .aux, .log, .out, and .toc files."
406
  :group 'org-export-pdf
407 408
  :type 'boolean)

409 410 411 412 413
;;; Hooks

(defvar org-export-latex-after-blockquotes-hook nil
  "Hook run during LaTeX export, after blockquote, verse, center are done.")

414 415 416
(defvar org-export-latex-final-hook nil
  "Hook run in the finalized LaTeX buffer.")

John Wiegley's avatar
John Wiegley committed
417
;;; Autoload functions:
418

Carsten Dominik's avatar
Carsten Dominik committed
419 420
;;;###autoload
(defun org-export-as-latex-batch ()
421 422 423
  "Call `org-export-as-latex', may be used in batch processing.
For example:

424 425 426 427
emacs   --batch
        --load=$HOME/lib/emacs/org.el
        --eval \"(setq org-export-headline-levels 2)\"
        --visit=MyFile --funcall org-export-as-latex-batch"
Carsten Dominik's avatar
Carsten Dominik committed
428 429 430 431
  (org-export-as-latex org-export-headline-levels 'hidden))

;;;###autoload
(defun org-export-as-latex-to-buffer (arg)
432
  "Call `org-export-as-latex` with output to a temporary buffer.
Carsten Dominik's avatar
Carsten Dominik committed
433 434 435
No file is created.  The prefix ARG is passed through to `org-export-as-latex'."
  (interactive "P")
  (org-export-as-latex arg nil nil "*Org LaTeX Export*")
436 437
  (when org-export-show-temporary-export-buffer
    (switch-to-buffer-other-window "*Org LaTeX Export*")))
Carsten Dominik's avatar
Carsten Dominik committed
438 439 440 441 442

;;;###autoload
(defun org-replace-region-by-latex (beg end)
  "Replace the region from BEG to END with its LaTeX export.
It assumes the region has `org-mode' syntax, and then convert it to
443
LaTeX.  This can be used in any buffer.  For example, you could
Carsten Dominik's avatar
Carsten Dominik committed
444 445 446 447 448 449 450 451 452 453
write an itemized list in `org-mode' syntax in an LaTeX buffer and
then use this command to convert it."
  (interactive "r")
  (let (reg latex buf)
    (save-window-excursion
      (if (org-mode-p)
	  (setq latex (org-export-region-as-latex
		       beg end t 'string))
	(setq reg (buffer-substring beg end)
	      buf (get-buffer-create "*Org tmp*"))
454
	(with-current-buffer buf
Carsten Dominik's avatar
Carsten Dominik committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
	  (erase-buffer)
	  (insert reg)
	  (org-mode)
	  (setq latex (org-export-region-as-latex
		       (point-min) (point-max) t 'string)))
	(kill-buffer buf)))
    (delete-region beg end)
    (insert latex)))

;;;###autoload
(defun org-export-region-as-latex (beg end &optional body-only buffer)
  "Convert region from BEG to END in `org-mode' buffer to LaTeX.
If prefix arg BODY-ONLY is set, omit file header, footer, and table of
contents, and only produce the region of converted text, useful for
cut-and-paste operations.
If BUFFER is a buffer or a string, use/create that buffer as a target
of the converted LaTeX.  If BUFFER is the symbol `string', return the
472
produced LaTeX as a string and leave no buffer behind.  For example,
Carsten Dominik's avatar
Carsten Dominik committed
473 474 475 476 477
a Lisp program could call this function in the following way:

  (setq latex (org-export-region-as-latex beg end t 'string))

When called interactively, the output buffer is selected, and shown
478
in a window.  A non-interactive call will only return the buffer."
Carsten Dominik's avatar
Carsten Dominik committed
479 480 481 482
  (interactive "r\nP")
  (when (interactive-p)
    (setq buffer "*Org LaTeX Export*"))
  (let ((transient-mark-mode t) (zmacs-regions t)
483 484
	ext-plist rtn)
    (setq ext-plist (plist-put ext-plist :ignore-subree-p t))
Carsten Dominik's avatar
Carsten Dominik committed
485 486 487 488
    (goto-char end)
    (set-mark (point)) ;; to activate the region
    (goto-char beg)
    (setq rtn (org-export-as-latex
489
	       nil nil ext-plist
Carsten Dominik's avatar
Carsten Dominik committed
490 491 492 493 494 495 496 497
	       buffer body-only))
    (if (fboundp 'deactivate-mark) (deactivate-mark))
    (if (and (interactive-p) (bufferp rtn))
	(switch-to-buffer-other-window rtn)
      rtn)))

;;;###autoload
(defun org-export-as-latex (arg &optional hidden ext-plist
498
				to-buffer body-only pub-dir)
499 500 501 502 503
  "Export current buffer to a LaTeX file.
If there is an active region, export only the region.  The prefix
ARG specifies how many levels of the outline should become
headlines.  The default is 3.  Lower levels will be exported
depending on `org-export-latex-low-levels'.  The default is to
504 505 506
convert them as description lists.
HIDDEN is obsolete and does nothing.
EXT-PLIST is a property list with
507 508 509 510 511 512 513
external parameters overriding org-mode's default settings, but
still inferior to file-local settings.  When TO-BUFFER is
non-nil, create a buffer with that name and export to that
buffer.  If TO-BUFFER is the symbol `string', don't leave any
buffer behind but just return the resulting LaTeX as a string.
When BODY-ONLY is set, don't produce the file header and footer,
simply return the content of \begin{document}...\end{document},
514 515
without even the \begin{document} and \end{document} commands.
when PUB-DIR is set, use this as the publishing directory."
Carsten Dominik's avatar
Carsten Dominik committed
516 517 518 519 520 521 522 523 524 525 526
  (interactive "P")
  ;; Make sure we have a file name when we need it.
  (when (and (not (or to-buffer body-only))
	     (not buffer-file-name))
    (if (buffer-base-buffer)
	(org-set-local 'buffer-file-name
		       (with-current-buffer (buffer-base-buffer)
			 buffer-file-name))
      (error "Need a file name to be able to export")))

  (message "Exporting to LaTeX...")
527 528 529
  (org-unmodified
   (remove-text-properties (point-min) (point-max)
			   '(:org-license-to-kill nil)))
Carsten Dominik's avatar
Carsten Dominik committed
530
  (org-update-radio-target-regexp)
531
  (org-export-latex-set-initial-vars ext-plist arg)
532
  (let* ((wcf (current-window-configuration))
533 534
	 (opt-plist org-export-latex-options-plist)
	 (region-p (org-region-active-p))
535 536
	 (rbeg (and region-p (region-beginning)))
	 (rend (and region-p (region-end)))
537
	 (subtree-p
538 539 540 541 542 543 544 545 546 547 548
	  (if (plist-get opt-plist :ignore-subree-p)
	      nil
	    (when region-p
	      (save-excursion
		(goto-char rbeg)
		(and (org-at-heading-p)
		     (>= (org-end-of-subtree t t) rend))))))
	 (opt-plist (setq org-export-opt-plist
			  (if subtree-p
			      (org-export-add-subtree-options opt-plist rbeg)
			    opt-plist)))
549 550
	 ;; Make sure the variable contains the updated values.
	 (org-export-latex-options-plist opt-plist)
551 552 553 554 555 556 557
	 (title (or (and subtree-p (org-export-get-title-from-subtree))
		    (plist-get opt-plist :title)
		    (and (not
			  (plist-get opt-plist :skip-before-1st-heading))
			 (org-export-grab-title-from-buffer))
		    (file-name-sans-extension
		     (file-name-nondirectory buffer-file-name))))
Carsten Dominik's avatar
Carsten Dominik committed
558
	 (filename (concat (file-name-as-directory
559 560
			    (or pub-dir
				(org-export-directory :LaTeX ext-plist)))
Carsten Dominik's avatar
Carsten Dominik committed
561
			   (file-name-sans-extension
562 563 564 565 566
			    (or (and subtree-p
				     (org-entry-get rbeg "EXPORT_FILE_NAME" t))
				(file-name-nondirectory ;sans-extension
				 buffer-file-name)))
			   ".tex"))
Carsten Dominik's avatar
Carsten Dominik committed
567 568 569 570 571 572 573 574 575 576 577
	 (filename (if (equal (file-truename filename)
			      (file-truename buffer-file-name))
		       (concat filename ".tex")
		     filename))
	 (buffer (if to-buffer
		     (cond
		      ((eq to-buffer 'string) (get-buffer-create
					       "*Org LaTeX Export*"))
		      (t (get-buffer-create to-buffer)))
		   (find-file-noselect filename)))
	 (odd org-odd-levels-only)
578
	 (header (org-export-latex-make-header title opt-plist))
579
	 (skip (cond (subtree-p nil)
580
		     (region-p nil)
581
		     (t (plist-get opt-plist :skip-before-1st-heading))))
Carsten Dominik's avatar
Carsten Dominik committed
582
	 (text (plist-get opt-plist :text))
583 584 585 586 587
	 (org-export-preprocess-hook
	  (cons
	   `(lambda () (org-set-local 'org-complex-heading-regexp
				      ,org-export-latex-complex-heading-re))
	   org-export-preprocess-hook))
588
	 (first-lines (if skip "" (org-export-latex-first-lines
589 590 591 592 593 594 595
				   opt-plist
				   (if subtree-p
				       (save-excursion
					 (goto-char rbeg)
					 (point-at-bol 2))
				     rbeg)
				   (if region-p rend))))
Carsten Dominik's avatar
Carsten Dominik committed
596 597 598 599 600 601
	 (coding-system (and (boundp 'buffer-file-coding-system)
			     buffer-file-coding-system))
	 (coding-system-for-write (or org-export-latex-coding-system
				      coding-system))
	 (save-buffer-coding-system (or org-export-latex-coding-system
					coding-system))
602
	 (region (buffer-substring
Carsten Dominik's avatar
Carsten Dominik committed
603 604 605
		  (if region-p (region-beginning) (point-min))
		  (if region-p (region-end) (point-max))))
	 (string-for-export
606
	  (org-export-preprocess-string
607 608 609 610 611 612
	   region
	   :emph-multiline t
	   :for-LaTeX t
	   :comments nil
	   :tags (plist-get opt-plist :tags)
	   :priority (plist-get opt-plist :priority)
613 614
	   :footnotes (plist-get opt-plist :footnotes)
	   :timestamps (plist-get opt-plist :timestamps)
615 616 617 618 619 620
	   :todo-keywords (plist-get opt-plist :todo-keywords)
	   :add-text (if (eq to-buffer 'string) nil text)
	   :skip-before-1st-heading skip
	   :select-tags (plist-get opt-plist :select-tags)
	   :exclude-tags (plist-get opt-plist :exclude-tags)
	   :LaTeX-fragments nil)))
621

622
    (set-buffer buffer)
Carsten Dominik's avatar
Carsten Dominik committed
623
    (erase-buffer)
624
    (org-install-letbind)
John Wiegley's avatar
John Wiegley committed
625

626 627 628
    (and (fboundp 'set-buffer-file-coding-system)
	 (set-buffer-file-coding-system coding-system-for-write))

629
    ;; insert the header and initial document commands
630
    (unless (or (eq to-buffer 'string) body-only)
631
      (insert header))
632 633 634

    ;; insert text found in #+TEXT
    (when (and text (not (eq to-buffer 'string)))
635 636 637
      (insert (org-export-latex-content
	       text '(lists tables fixed-width keywords))
	       "\n\n"))
638 639

    ;; insert lines before the first headline
640
    (unless skip
641
      (insert first-lines))
Carsten Dominik's avatar
Carsten Dominik committed
642

643
    ;; export the content of headlines
Carsten Dominik's avatar
Carsten Dominik committed
644 645 646 647
    (org-export-latex-global
     (with-temp-buffer
       (insert string-for-export)
       (goto-char (point-min))
648 649 650
       (when (re-search-forward "^\\(\\*+\\) " nil t)
	 (let* ((asters (length (match-string 1)))
		(level (if odd (- asters 2) (- asters 1))))
651
	   (setq org-export-latex-add-level
652 653 654 655
		 (if odd (1- (/ (1+ asters) 2)) (1- asters)))
	   (org-export-latex-parse-global level odd)))))

    ;; finalization
Carsten Dominik's avatar
Carsten Dominik committed
656
    (unless body-only (insert "\n\\end{document}"))
657 658 659 660 661 662 663 664 665 666 667

    ;; Relocate the table of contents
    (goto-char (point-min))
    (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
      (goto-char (point-min))
      (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t)
	(replace-match ""))
      (goto-char (point-min))
      (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
	   (replace-match "\\tableofcontents" t t)))

668
    (run-hooks 'org-export-latex-final-hook)
John Wiegley's avatar
John Wiegley committed
669
    (or to-buffer (save-buffer))
Carsten Dominik's avatar
Carsten Dominik committed
670
    (goto-char (point-min))
671 672
    (or (org-export-push-to-kill-ring "LaTeX")
	(message "Exporting to LaTeX...done"))
673 674 675 676 677 678 679
    (prog1
	(if (eq to-buffer 'string)
	    (prog1 (buffer-substring (point-min) (point-max))
	      (kill-buffer (current-buffer)))
	  (current-buffer))
      (set-window-configuration wcf))))

680 681 682 683 684 685 686 687 688 689 690
;;;###autoload
(defun org-export-as-pdf (arg &optional hidden ext-plist
			      to-buffer body-only pub-dir)
  "Export as LaTeX, then process through to PDF."
  (interactive "P")
  (message "Exporting to PDF...")
  (let* ((wconfig (current-window-configuration))
	 (lbuf (org-export-as-latex arg hidden ext-plist
				    to-buffer body-only pub-dir))
	 (file (buffer-file-name lbuf))
	 (base (file-name-sans-extension (buffer-file-name lbuf)))
691 692 693 694 695 696 697 698 699
	 (pdffile (concat base ".pdf"))
	 (cmds org-latex-to-pdf-process)
	 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
	 (bibtex-p (with-current-buffer lbuf
		     (save-excursion
		       (goto-char (point-min))
		       (re-search-forward "\\\\bibliography{" nil t))))
	 cmd)
    (with-current-buffer outbuf (erase-buffer))
700 701
    (and (file-exists-p pdffile) (delete-file pdffile))
    (message "Processing LaTeX file...")
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
    (if (and cmds (symbolp cmds))
	(funcall cmds file)
      (while cmds
	(setq cmd (pop cmds))
	(while (string-match "%b" cmd)
	  (setq cmd (replace-match
		     (save-match-data
		       (shell-quote-argument base))
		     t t cmd)))
	(while (string-match "%s" cmd)
	  (setq cmd (replace-match
		     (save-match-data
		       (shell-quote-argument file))
		     t t cmd)))
	(shell-command cmd outbuf outbuf)))
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
    (message "Processing LaTeX file...done")
    (if (not (file-exists-p pdffile))
	(error "PDF file was not produced")
      (set-window-configuration wconfig)
      (when org-export-pdf-remove-logfiles
	(dolist (ext '("aux" "log" "out" "toc"))
	  (setq file (concat base "." ext))
	  (and (file-exists-p file) (delete-file file))))
      (message "Exporting to PDF...done")
      pdffile)))

;;;###autoload
(defun org-export-as-pdf-and-open (arg)
  "Export as LaTeX, then process through to PDF, and open."
  (interactive "P")
  (let ((pdffile (org-export-as-pdf arg)))
    (if pdffile
	(org-open-file pdffile)
      (error "PDF file was not produced"))))

John Wiegley's avatar
John Wiegley committed
737
;;; Parsing functions:
738

Carsten Dominik's avatar
Carsten Dominik committed
739 740 741
(defun org-export-latex-parse-global (level odd)
  "Parse the current buffer recursively, starting at LEVEL.
If ODD is non-nil, assume the buffer only contains odd sections.
742
Return a list reflecting the document structure."
Carsten Dominik's avatar
Carsten Dominik committed
743 744 745
  (save-excursion
    (goto-char (point-min))
    (let* ((cnt 0) output
746
	   (depth org-export-latex-sectioning-depth))
Carsten Dominik's avatar
Carsten Dominik committed
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
      (while (re-search-forward
	      (concat "^\\(\\(?:\\*\\)\\{"
		      (number-to-string (+ (if odd 2 1) level))
		      "\\}\\) \\(.*\\)$")
	      ;; make sure that there is no upper heading
	      (when (> level 0)
		(save-excursion
		  (save-match-data
		    (re-search-forward
		     (concat "^\\(\\(?:\\*\\)\\{"
			     (number-to-string level)
			     "\\}\\) \\(.*\\)$") nil t)))) t)
	(setq cnt (1+ cnt))
	(let* ((pos (match-beginning 0))
	       (heading (match-string 2))
	       (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
	  (save-excursion
	    (narrow-to-region
	     (point)
	     (save-match-data
	       (if (re-search-forward
		    (concat "^\\(\\(?:\\*\\)\\{"
			    (number-to-string (+ (if odd 2 1) level))
			    "\\}\\) \\(.*\\)$") nil t)
		   (match-beginning 0)
		 (point-max))))
	    (goto-char (point-min))
	    (setq output
		  (append output
			  (list
			   (list
			    `(pos . ,pos)
			    `(level . ,nlevel)
			    `(occur . ,cnt)
			    `(heading . ,heading)
			    `(content . ,(org-export-latex-parse-content))
783
			    `(subcontent . ,(org-export-latex-parse-subcontent
Carsten Dominik's avatar
Carsten Dominik committed
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
					     level odd)))))))
	  (widen)))
      (list output))))

(defun org-export-latex-parse-content ()
  "Extract the content of a section."
  (let ((beg (point))
	(end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t)
		 (progn (beginning-of-line) (point))
	       (point-max))))
    (buffer-substring beg end)))

(defun org-export-latex-parse-subcontent (level odd)
  "Extract the subcontent of a section at LEVEL.
If ODD Is non-nil, assume subcontent only contains odd sections."
  (if (not (re-search-forward
	    (concat "^\\(\\(?:\\*\\)\\{"
		    (number-to-string (+ (if odd 4 2) level))
		    "\\}\\) \\(.*\\)$")
	    nil t))
      nil ; subcontent is nil
    (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))

John Wiegley's avatar
John Wiegley committed
807
;;; Rendering functions:
Carsten Dominik's avatar
Carsten Dominik committed
808 809 810 811 812 813 814 815 816 817 818 819
(defun org-export-latex-global (content)
  "Export CONTENT to LaTeX.
CONTENT is an element of the list produced by
`org-export-latex-parse-global'."
  (if (eq (car content) 'subcontent)
      (mapc 'org-export-latex-sub (cdr content))
    (org-export-latex-sub (car content))))

(defun org-export-latex-sub (subcontent)
  "Export the list SUBCONTENT to LaTeX.
SUBCONTENT is an alist containing information about the headline
and its content."
820
  (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
821
    (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
Carsten Dominik's avatar
Carsten Dominik committed
822

823
(defun org-export-latex-subcontent (subcontent num)
824 825 826 827 828 829 830 831 832
  "Export each cell of SUBCONTENT to LaTeX.
If NUM, export sections as numerical sections."
  (let* ((heading (org-export-latex-fontify-headline
		   (cdr (assoc 'heading subcontent))))
	 (level (- (cdr (assoc 'level subcontent))
		   org-export-latex-add-level))
	 (occur (number-to-string (cdr (assoc 'occur subcontent))))
	 (content (cdr (assoc 'content subcontent)))
	 (subcontent (cadr (assoc 'subcontent subcontent)))
833 834 835
	 (label (org-get-text-property-any 0 'target heading))
	 (label-list (cons label (cdr (assoc label
					     org-export-target-aliases)))))
836
    (cond
Carsten Dominik's avatar
Carsten Dominik committed
837
     ;; Normal conversion
838
     ((<= level org-export-latex-sectioning-depth)
839 840 841 842 843 844 845
      (let* ((sec (nth (1- level) org-export-latex-sectioning))
	     start end)
	(if (consp (cdr sec))
	    (setq start (nth (if num 0 2) sec)
		  end (nth (if num 1 3) sec))
	  (setq start (if num (car sec) (cdr sec))))
	(insert (format start heading) "\n")
846 847 848
	(when label
	  (insert (mapconcat (lambda (l) (format "\\label{%s}" l))
			     label-list "\n") "\n"))
849 850 851 852
	(insert (org-export-latex-content content))
	(cond ((stringp subcontent) (insert subcontent))
	      ((listp subcontent) (org-export-latex-sub subcontent)))
	(if end (insert end "\n"))))
Carsten Dominik's avatar
Carsten Dominik committed
853
     ;; At a level under the hl option: we can drop this subsection
854
     ((> level org-export-latex-sectioning-depth)
Carsten Dominik's avatar
Carsten Dominik committed
855
      (cond ((eq org-export-latex-low-levels 'description)
856 857 858 859 860
	     (if (string-match "% ends low level$"
			       (buffer-substring (point-at-bol 0) (point)))
		 (delete-region (point-at-bol 0) (point))
	       (insert "\\begin{description}\n"))
	     (insert (format "\n\\item[%s]%s~\n\n"
861 862
			     heading
			     (if label (format "\\label{%s}" label) "")))
Carsten Dominik's avatar
Carsten Dominik committed
863 864 865
	     (insert (org-export-latex-content content))
	     (cond ((stringp subcontent) (insert subcontent))
		   ((listp subcontent) (org-export-latex-sub subcontent)))
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
	     (insert "\\end{description} % ends low level\n"))
	    ((memq org-export-latex-low-levels '(itemize enumerate))
	     (if (string-match "% ends low level$"
			       (buffer-substring (point-at-bol 0) (point)))
		 (delete-region (point-at-bol 0) (point))
	       (insert (format "\\begin{%s}\n"
			       (symbol-name org-export-latex-low-levels))))
	     (insert (format "\n\\item %s\\\\\n%s\n"
			     heading
			     (if label (format "\\label{%s}" label) "")))
	     (insert (org-export-latex-content content))
	     (cond ((stringp subcontent) (insert subcontent))
		   ((listp subcontent) (org-export-latex-sub subcontent)))
	     (insert (format "\\end{%s} %% ends low level\n"
			     (symbol-name org-export-latex-low-levels))))

	    ((listp org-export-latex-low-levels)
	     (if (string-match "% ends low level$"
			       (buffer-substring (point-at-bol 0) (point)))
		 (delete-region (point-at-bol 0) (point))
	       (insert (car org-export-latex-low-levels) "\n"))
	     (insert (format (nth 2 org-export-latex-low-levels)
			     heading
			     (if label (format "\\label{%s}" label) "")))
	     (insert (org-export-latex-content content))
	     (cond ((stringp subcontent) (insert subcontent))
		   ((listp subcontent) (org-export-latex-sub subcontent)))
	     (insert (nth 1 org-export-latex-low-levels)
		     " %% ends low level\n"))

Carsten Dominik's avatar
Carsten Dominik committed
896 897
	    ((stringp org-export-latex-low-levels)
	     (insert (format org-export-latex-low-levels heading) "\n")
898
	     (when label (insert (format "\\label{%s}\n" label)))
Carsten Dominik's avatar
Carsten Dominik committed
899 900 901 902
	     (insert (org-export-latex-content content))
	     (cond ((stringp subcontent) (insert subcontent))
		   ((listp subcontent) (org-export-latex-sub subcontent)))))))))

John Wiegley's avatar
John Wiegley committed
903
;;; Exporting internals:
904
(defun org-export-latex-set-initial-vars (ext-plist level)
John Wiegley's avatar
John Wiegley committed
905
  "Store org local variables required for LaTeX export.
906 907 908
EXT-PLIST is an optional additional plist.
LEVEL indicates the default depth for export."
  (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
909 910 911 912
	org-export-latex-done-keywords org-done-keywords
	org-export-latex-not-done-keywords org-not-done-keywords
	org-export-latex-complex-heading-re org-complex-heading-regexp
	org-export-latex-display-custom-times org-display-custom-times
913
	org-export-latex-all-targets-re
John Wiegley's avatar
John Wiegley committed
914
	(org-make-target-link-regexp (org-all-targets))
915
	org-export-latex-options-plist
John Wiegley's avatar
John Wiegley committed
916 917
	(org-combine-plists (org-default-export-plist) ext-plist
			    (org-infile-export-plist))
918
	org-export-latex-class
919 920 921 922 923 924 925 926 927 928 929
	(or (and (org-region-active-p)
		 (save-excursion
		   (goto-char (region-beginning))
		   (and (looking-at org-complex-heading-regexp)
			(org-entry-get nil "LaTeX_CLASS" 'selective))))
	    (save-excursion
	      (save-restriction
		(widen)
		(goto-char (point-min))
		(and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
		     (match-string 1))))
930
	    (plist-get org-export-latex-options-plist :latex-class)
931 932 933 934 935
	    org-export-latex-default-class)
	org-export-latex-class
	(or (car (assoc org-export-latex-class org-export-latex-classes))
	    (error "No definition for class `%s' in `org-export-latex-classes'"
		   org-export-latex-class))
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
	org-export-latex-header
	(cadr (assoc org-export-latex-class org-export-latex-classes))
	org-export-latex-sectioning
	(cddr (assoc org-export-latex-class org-export-latex-classes))
	org-export-latex-sectioning-depth
	(or level
	    (let ((hl-levels
		   (plist-get org-export-latex-options-plist :headline-levels))
		  (sec-depth (length org-export-latex-sectioning)))
	      (if (> hl-levels sec-depth) sec-depth hl-levels)))))

(defun org-export-latex-make-header (title opt-plist)
  "Make the LaTeX header and return it as a string.
TITLE is the current title from the buffer or region.
OPT-PLIST is the options plist for current buffer."
  (let ((toc (plist-get opt-plist :table-of-contents))
	(author (plist-get opt-plist :author)))
    (concat
954
     (if (plist-get opt-plist :time-stamp-file)
955
	 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
956
     ;; insert LaTeX custom header
957
     (org-export-apply-macros-in-string org-export-latex-header)
958
     "\n"
959 960 961 962 963 964 965 966
     ;; insert information on LaTeX packages
     (when org-export-latex-packages-alist
       (mapconcat (lambda(p)
		    (if (equal "" (car p))
			(format "\\usepackage{%s}" (cadr p))
		      (format "\\usepackage[%s]{%s}"
			      (car p) (cadr p))))
		  org-export-latex-packages-alist "\n"))
967
     ;; insert additional commands in the header
968 969 970
     (org-export-apply-macros-in-string
      (plist-get opt-plist :latex-header-extra))
     (org-export-apply-macros-in-string org-export-latex-append-header)
971
     ;; insert the title
972 973
     (format
      "\n\n\\title{%s}\n"
John Wiegley's avatar
John Wiegley committed
974 975
      ;; convert the title
      (org-export-latex-content
976
       title '(lists tables fixed-width keywords)))
977 978
     ;; insert author info
     (if (plist-get opt-plist :author-info)
979
	 (format "\\author{%s}\n"
980
		 (org-export-latex-fontify-headline (or author user-full-name)));????????????????????
981
       (format "%%\\author{%s}\n"
982
	       (or author user-full-name)))
983 984
     ;; insert the date
     (format "\\date{%s}\n"
985
	     (format-time-string
986 987 988 989 990
	      (or (plist-get opt-plist :date)
		  org-export-latex-date-format)))
     ;; beginning of the document
     "\n\\begin{document}\n\n"
     ;; insert the title command
991 992 993 994
     (when (string-match "\\S-" title)
       (if (string-match "%s" org-export-latex-title-command)
	   (format org-export-latex-title-command title)
	 org-export-latex-title-command))
995 996
     "\n\n"
     ;; table of contents
997
     (when (and org-export-with-toc
998 999
		(plist-get opt-plist :section-numbers))
       (cond ((numberp toc)
1000
	      (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1001
		      (min toc (plist-get opt-plist :headline-levels))))
1002
	     (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1003
			  (plist-get opt-plist :headline-levels))))))))
John Wiegley's avatar
John Wiegley committed
1004

1005
(defun org-export-latex-first-lines (opt-plist &optional beg end)
John Wiegley's avatar
John Wiegley committed
1006
  "Export the first lines before first headline.
1007 1008
If BEG is non-nil, it is the beginning of the region.
If END is non-nil, it is the end of the region."
John Wiegley's avatar
John Wiegley committed
1009
  (save-excursion
1010
    (goto-char (or beg (point-min)))
1011 1012 1013 1014
    (let* ((pt (point))
	   (end (if (re-search-forward "^\\*+ " end t)
		    (goto-char (match-beginning 0))
		  (goto-char (or end (point-max))))))
1015 1016 1017 1018 1019 1020 1021 1022 1023
      (prog1
	  (org-export-latex-content
	   (org-export-preprocess-string
	    (buffer-substring pt end)
	    :for-LaTeX t
	    :emph-multiline t
	    :add-text nil
	    :comments nil
	    :skip-before-1st-heading nil
1024 1025 1026
	    :LaTeX-fragments nil
	    :timestamps (plist-get opt-plist :timestamps)
	    :footnotes (plist-get opt-plist :footnotes)))
1027 1028 1029 1030 1031 1032 1033 1034
	(org-unmodified
	 (add-text-properties pt (max pt (1- end))
			      '(:org-license-to-kill t)))))))

(defvar org-export-latex-header-defs nil
  "The header definitions that might be used in the LaTeX body.")
(defvar org-export-latex-header-defs-re nil
  "The header definitions that might be used in the LaTeX body.")
John Wiegley's avatar
John Wiegley committed
1035

1036 1037 1038 1039 1040 1041 1042
(defun org-export-latex-content (content &optional exclude-list)
  "Convert CONTENT string to LaTeX.
Don't perform conversions that are in EXCLUDE-LIST.  Recognized
conversion types are: quotation-marks, emphasis, sub-superscript,
links, keywords, lists, tables, fixed-width"
  (with-temp-buffer
   (insert content)
1043 1044
   (unless (memq 'timestamps exclude-list)
     (org-export-latex-time-stamps))
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
   (unless (memq 'quotation-marks exclude-list)
     (org-export-latex-quotation-marks))
   (unless (memq 'emphasis exclude-list)
     (when (plist-get org-export-latex-options-plist :emphasize)
       (org-export-latex-fontify)))
   (unless (memq 'sub-superscript exclude-list)
     (org-export-latex-special-chars
      (plist-get org-export-latex-options-plist :sub-superscript)))
   (unless (memq 'links exclude-list)
     (org-export-latex-links))
   (unless (memq 'keywords exclude-list)
1056
     (org-export-latex-keywords))
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
   (unless (memq 'lists exclude-list)
     (org-export-latex-lists))
   (unless (memq 'tables exclude-list)
     (org-export-latex-tables
      (plist-get org-export-latex-options-plist :tables)))
   (unless (memq 'fixed-width exclude-list)
     (org-export-latex-fixed-width
      (plist-get org-export-latex-options-plist :fixed-width)))
   ;; return string
   (buffer-substring (point-min) (point-max))))

(defun org-export-latex-protect-string (s)
1069
  "Add the org-protected property to string S."
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
  (add-text-properties 0 (length s) '(org-protected t) s) s)

(defun org-export-latex-protect-char-in-string (char-list string)
  "Add org-protected text-property to char from CHAR-LIST in STRING."
  (with-temp-buffer
    (save-match-data
      (insert string)
      (goto-char (point-min))
      (while (re-search-forward (regexp-opt char-list) nil t)
	(add-text-properties (match-beginning 0)
			     (match-end 0) '(org-protected t)))
      (buffer-string))))

1083
(defun org-export-latex-keywords-maybe (&optional remove-list)
Carsten Dominik's avatar
Carsten Dominik committed
1084 1085
  "Maybe remove keywords depending on rules in REMOVE-LIST."
  (goto-char (point-min))
1086
  (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
1087 1088 1089
	(case-fold-search nil)
	(todo-markup org-export-latex-todo-keyword-markup)
	fmt)
Carsten Dominik's avatar
Carsten Dominik committed
1090 1091 1092 1093
    ;; convert TODO keywords
    (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
      (if (plist-get remove-list :todo)
	  (replace-match "")
1094 1095 1096 1097 1098 1099 1100 1101
	(setq fmt (cond
		   ((stringp todo-markup) todo-markup)
		   ((and (consp todo-markup) (stringp (car todo-markup)))
		    (if (member (match-string 1) org-export-latex-done-keywords)
			(cdr todo-markup) (car todo-markup)))