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

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

;; 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.

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

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

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

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

John Wiegley's avatar
John Wiegley committed
54
;;; Variables:
55
(defvar org-export-latex-class nil)
56
(defvar org-export-latex-class-options nil)
57 58 59 60
(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)
61 62 63 64
(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)
65 66
(defvar org-export-latex-all-targets-re nil)
(defvar org-export-latex-add-level 0)
67 68
(defvar org-export-latex-footmark-seen nil
  "List of footnotes markers seen so far by exporter.")
69 70
(defvar org-export-latex-sectioning "")
(defvar org-export-latex-sectioning-depth 0)
71 72 73 74 75
(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.")
76 77
(defvar org-re-quote)  ; dynamically scoped from org.el
(defvar org-commentsp) ; dynamically scoped from org.el
John Wiegley's avatar
John Wiegley committed
78

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

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

86 87 88 89
(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
90

91 92
(defcustom org-export-latex-classes
  '(("article"
93
     "\\documentclass[11pt]{article}"
94 95 96 97 98 99
     ("\\section{%s}" . "\\section*{%s}")
     ("\\subsection{%s}" . "\\subsection*{%s}")
     ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
     ("\\paragraph{%s}" . "\\paragraph*{%s}")
     ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
    ("report"
100
     "\\documentclass[11pt]{report}"
101 102 103 104 105 106
     ("\\part{%s}" . "\\part*{%s}")
     ("\\chapter{%s}" . "\\chapter*{%s}")
     ("\\section{%s}" . "\\section*{%s}")
     ("\\subsection{%s}" . "\\subsection*{%s}")
     ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
    ("book"
107
     "\\documentclass[11pt]{book}"
108 109 110 111
     ("\\part{%s}" . "\\part*{%s}")
     ("\\chapter{%s}" . "\\chapter*{%s}")
     ("\\section{%s}" . "\\section*{%s}")
     ("\\subsection{%s}" . "\\subsection*{%s}")
112 113 114 115 116
     ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
    ("beamer"
     "\\documentclass{beamer}"
     org-beamer-sectioning
     ))
117 118 119 120 121 122
  "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
123
    (numbered-section . unnumbered-section\)
124 125
    ...\)

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
The header string
-----------------

The HEADER-STRING is the header that will be inserted into the LaTeX file.
It should contain the \\documentclass macro, and anything else that is needed
for this setup.  To this header, the following commands will be added:

- Calls to \\usepackage for all packages mentioned in the variables
  `org-export-latex-default-packages-alist' and
  `org-export-latex-packages-alist'.  Thus, your header definitions should
  avoid to also request these packages.

- Lines specified via \"#+LaTeX_HEADER:\"

If you need more control about the sequence in which the header is built
up, or if you want to exclude one of these building blocks for a particular
class, you can use the following macro-like placeholders.

 [DEFAULT-PACKAGES]      \\usepackage statements for default packages
 [NO-DEFAULT-PACKAGES]   do not include any of the default packages
146
 [PACKAGES]              \\usepackage statements for packages
147 148 149
 [NO-PACKAGES]           do not include the packages
 [EXTRA]                 the stuff from #+LaTeX_HEADER
 [NO-EXTRA]              do not include #+LaTeX_HEADER stuff
150
 [BEAMER-HEADER-EXTRA]   the beamer extra headers
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

So a header like

  \\documentclass{article}
  [NO-DEFAULT-PACKAGES]
  [EXTRA]
  \\providecommand{\\alert}[1]{\\textbf{#1}}
  [PACKAGES]

will omit the default packages, and will include the #+LaTeX_HEADER lines,
then have a call to \\providecommand, and then place \\usepackage commands
based on the content of `org-export-latex-packages-alist'.

If your header or `org-export-latex-default-packages-alist' inserts
\"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be replaced with
a coding system derived from `buffer-file-coding-system'.  See also the
variable `org-export-latex-inputenc-alist' for a way to influence this
mechanism.

The sectioning structure
------------------------

The sectioning structure of the class is given by the elements following
the header string.  For each sectioning level, a number of strings is
specified.  A %s formatter is mandatory in each section string and will
be replaced by the title of the section.
177 178

Instead of a cons cell (numbered . unnumbered), you can also provide a list
179
of 2 or 4 elements,
180 181 182 183 184 185 186

  (numbered-open numbered-close)

or

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

187
providing opening and closing strings for a LaTeX environment that should
188
represent the document section.  The opening clause should have a %s
189 190 191 192 193 194 195
to represent the section title.

Instead of a list of sectioning commands, you can also specify a
function name.  That function will be called with two parameters,
the (reduced) level of the headline, and the headline text.  The function
must return a cons cell with the (possibly modified) headline text, and the
sectioning list in the cdr."
Carsten Dominik's avatar
Carsten Dominik committed
196
  :group 'org-export-latex
197
  :type '(repeat
198 199
	  (list (string :tag "LaTeX class")
		(string :tag "LaTeX header")
200 201 202
		(repeat :tag "Levels" :inline t
			(choice
			 (cons :tag "Heading"
203 204
			       (string :tag "  numbered")
			       (string :tag "unnumbered"))
205
			 (list :tag "Environment"
206 207
			       (string :tag "Opening   (numbered)")
			       (string :tag "Closing   (numbered)")
208
			       (string :tag "Opening (unnumbered)")
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
			       (string :tag "Closing (unnumbered)"))
			 (function :tag "Hook computing sectioning"))))))

(defcustom org-export-latex-inputenc-alist nil
  "Alist of inputenc coding system names, and what should really be used.
For example, adding an entry

      (\"utf8\" . \"utf8x\")

will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
are written as utf8 files."
  :group 'org-export-latex
  :type '(repeat
	  (cons
	   (string :tag "Derived from buffer")
	   (string :tag "Use this instead"))))

Carsten Dominik's avatar
Carsten Dominik committed
226 227

(defcustom org-export-latex-emphasis-alist
228 229 230
  '(("*" "\\textbf{%s}" nil)
    ("/" "\\emph{%s}" nil)
    ("_" "\\underline{%s}" nil)
231
    ("+" "\\st{%s}" nil)
232
    ("=" "\\protectedtexttt" t)
233
    ("~" "\\verb" t))
234 235 236 237
  "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.
238
If it is \"\\verb\", Org will automatically select a delimiter
239 240
character that is not in the string.  \"\\protectedtexttt\" will use \\texttt
to typeset and try to protect special characters.
241 242
The third element decides whether to protect converted text from other
conversions."
Carsten Dominik's avatar
Carsten Dominik committed
243 244 245
  :group 'org-export-latex
  :type 'alist)

246 247 248 249 250 251 252 253
(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)

254 255 256 257 258 259
(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)

260
(defcustom org-export-latex-date-format
261
  "\\today"
Carsten Dominik's avatar
Carsten Dominik committed
262 263 264 265
  "Format string for \\date{...}."
  :group 'org-export-latex
  :type 'string)

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
(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")))))

Carsten Dominik's avatar
Carsten Dominik committed
283 284 285 286 287
(defcustom org-export-latex-tag-markup "\\textbf{%s}"
  "Markup for tags, as a printf format."
  :group 'org-export-latex
  :type 'string)

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

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

298 299 300 301 302
(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)

Carsten Dominik's avatar
Carsten Dominik committed
303 304
(defcustom org-export-latex-href-format "\\href{%s}{%s}"
  "A printf format string to be applied to href links.
Bastien Guerry's avatar
Bastien Guerry committed
305 306
The format must contain either two %s instances or just one.  
If it contains two %s instances, the first will be filled with 
307 308
the link, the second with the link description.  If it contains
only one, the %s will be filled with the link."
Carsten Dominik's avatar
Carsten Dominik committed
309 310 311 312
  :group 'org-export-latex
  :type 'string)

(defcustom org-export-latex-hyperref-format "\\hyperref[%s]{%s}"
313
  "A printf format string to be applied to hyperref links.
314 315
The format must contain one or two %s instances.  The first one
will be filled with the link, the second with its description."
316 317 318
  :group 'org-export-latex
  :type 'string)

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 344 345 346 347 348 349
(defcustom org-export-latex-footnote-separator "\\textsuperscript{,}\\,"
  "Text used to separate footnotes."
  :group 'org-export-latex
  :type 'string)

(defcustom org-export-latex-quotes
  '(("fr" ("\\(\\s-\\|[[(]\\)\"" . "«~") ("\\(\\S-\\)\"" . "~»") ("\\(\\s-\\|(\\)'" . "'"))
    ("en" ("\\(\\s-\\|[[(]\\)\"" . "``") ("\\(\\S-\\)\"" . "''") ("\\(\\s-\\|(\\)'" . "`")))
  "Alist for quotes to use when converting english double-quotes.

The CAR of each item in this alist is the language code.
The CDR of each item in this alist is a list of three CONS:
- the first CONS defines the opening quote;
- the second CONS defines the closing quote;
- the last CONS defines single quotes.

For each item in a CONS, the first string is a regexp
for allowed characters before/after the quote, the second
string defines the replacement string for this quote."
  :group 'org-export-latex
  :type '(list
	  (cons :tag "Opening quote"
		(string :tag "Regexp for char before")
		(string :tag "Replacement quote     "))
	  (cons :tag "Closing quote"
		(string :tag "Regexp for char after ")
		(string :tag "Replacement quote     "))
	  (cons :tag "Single quote"
		(string :tag "Regexp for char before")
		(string :tag "Replacement quote     "))))

350
(defcustom org-export-latex-tables-verbatim nil
351
  "When non-nil, tables are exported verbatim."
352 353 354
  :group 'org-export-latex
  :type 'boolean)

355 356 357 358 359
(defcustom org-export-latex-tables-centered t
  "When non-nil, tables are exported in a center environment."
  :group 'org-export-latex
  :type 'boolean)

Bastien Guerry's avatar
Bastien Guerry committed
360 361 362 363 364 365
(defcustom org-export-latex-table-caption-above t
  "When non-nil, the caption is set above the table.  When nil,
the caption is set below the table."
  :group 'org-export-latex
  :type 'boolean)

366
(defcustom org-export-latex-tables-column-borders nil
367 368
  "When non-nil, grouping columns can cause outer vertical lines in tables.
When nil, grouping causes only separation lines between groups."
369 370
  :group 'org-export-latex
  :type 'boolean)
Carsten Dominik's avatar
Carsten Dominik committed
371

372
(defcustom org-export-latex-low-levels 'itemize
373 374 375
  "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.
376

377 378 379 380 381 382 383 384
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
385
to two occurrences of %s for the title and a label, respectively.  The third
386
string should be like \"\\end{itemize\"."
Carsten Dominik's avatar
Carsten Dominik committed
387 388
  :group 'org-export-latex
  :type '(choice (const :tag "Ignore" nil)
389 390 391 392 393 394 395 396
		 (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
397 398
		 (string :tag "Use a section string" :value "\\subparagraph{%s}")))

399
(defcustom org-export-latex-list-parameters
400
  '(:cbon "$\\boxtimes$" :cboff "$\\Box$" :cbtrans "$\\boxminus$")
401 402 403 404 405 406 407
  "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)

408
(defcustom org-export-latex-verbatim-wrap
Bastien Guerry's avatar
Bastien Guerry committed
409
  '("\\begin{verbatim}\n" . "\\end{verbatim}")
410 411 412 413 414 415 416 417 418 419
  "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")))

420
(defcustom org-export-latex-listings nil
421
  "Non-nil means export source code using the listings package.
422 423 424 425 426 427 428 429
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\"))
Carsten Dominik's avatar
Carsten Dominik committed
430 431 432 433 434 435 436
  (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))

Alternatively,

  (setq org-export-latex-listings 'minted)

causes source code to be exported using the minted package as
437
opposed to listings.  If you want to use minted, you need to add
Carsten Dominik's avatar
Carsten Dominik committed
438 439 440 441 442 443
the minted package to `org-export-latex-packages-alist', for
example using customize, or with

  (require 'org-latex)
  (add-to-list 'org-export-latex-packages-alist '(\"\" \"minted\"))

444 445
In addition, it is necessary to install
pygments (http://pygments.org), and to configure the variable
Carsten Dominik's avatar
Carsten Dominik committed
446 447 448
`org-latex-to-pdf-process' so that the -shell-escape option is
passed to pdflatex.
"
449 450 451 452
  :group 'org-export-latex
  :type 'boolean)

(defcustom org-export-latex-listings-langs
Carsten Dominik's avatar
Carsten Dominik committed
453
  '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
454 455 456 457 458 459 460 461
    (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")
462
    (sql "SQL") (sqlite "sql"))
463 464 465 466 467 468 469 470 471 472 473 474
  "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"))))

Carsten Dominik's avatar
Carsten Dominik committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
(defcustom org-export-latex-listings-w-names t
  "Non-nil means export names of named code blocks.
Code blocks exported with the listings package (controlled by the
`org-export-latex-listings' variable) can be named in the style
of noweb."
  :group 'org-export-latex
  :type 'boolean)

(defcustom org-export-latex-minted-langs
  '((emacs-lisp "common-lisp")
    (cc "c++")
    (cperl "perl")
    (shell-script "bash")
    (caml "ocaml"))
  "Alist mapping languages to their minted 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 minted 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.

Note that minted uses all lower case for language identifiers,
and that the full list of language identifiers can be obtained
with:
pygmentize -L lexers
"
  :group 'org-export-latex
  :type '(repeat
	  (list
	   (symbol :tag "Major mode       ")
	   (string :tag "Listings language"))))

507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
(defcustom org-export-latex-listings-options nil
  "Association list of options for the latex listings package.

These options are supplied as a comma-separated list to the
\\lstset command. Each element of the association list should be
a list containing two strings: the name of the option, and the
value. For example,

  (setq org-export-latex-listings-options
    '((\"basicstyle\" \"\\small\")
      (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))

will typeset the code in a small size font with underlined, bold
black keywords.

Note that the same options will be applied to blocks of all
languages."
  :group 'org-export-latex
  :type '(repeat
	  (list
	   (string :tag "Listings option name ")
	   (string :tag "Listings option value"))))

(defcustom org-export-latex-minted-options nil
  "Association list of options for the latex minted package.

These options are supplied within square brackets in
\\begin{minted} environments. Each element of the alist should be
a list containing two strings: the name of the option, and the
value. For example,

  (setq org-export-latex-minted-options
    '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))

will result in src blocks being exported with

\\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}

as the start of the minted environment. Note that the same
options will be applied to blocks of all languages."
  :group 'org-export-latex
  :type '(repeat
	  (list
	   (string :tag "Minted option name ")
	   (string :tag "Minted option value"))))

(defvar org-export-latex-custom-lang-environments nil
  "Association list mapping languages to language-specific latex
  environments used during export of src blocks by the listings
  and minted latex packages. For example,

  (setq org-export-latex-custom-lang-environments
     '((python \"pythoncode\")))

  would have the effect that if org encounters begin_src python
  during latex export it will output

  \\begin{pythoncode}
  <src block body>
  \\end{pythoncode}")

568
(defcustom org-export-latex-remove-from-headlines
569
  '(:todo nil :priority nil :tags nil)
570
  "A plist of keywords to remove from headlines.  OBSOLETE.
Carsten Dominik's avatar
Carsten Dominik committed
571 572
Non-nil means remove this keyword type from the headline.

573 574 575 576 577
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
578 579 580
  :type 'plist
  :group 'org-export-latex)

581
(defcustom org-export-latex-image-default-option "width=.9\\linewidth"
Carsten Dominik's avatar
Carsten Dominik committed
582 583
  "Default option for images."
  :group 'org-export-latex
584
  :type 'string)
Carsten Dominik's avatar
Carsten Dominik committed
585

586 587 588 589 590
(defcustom org-latex-default-figure-position "htb"
  "Default position for latex figures."
  :group 'org-export-latex
  :type 'string)

591 592 593 594 595
(defcustom org-export-latex-tabular-environment "tabular"
  "Default environment used to build tables."
  :group 'org-export-latex
  :type 'string)

596
(defcustom org-export-latex-inline-image-extensions
597
  '("pdf" "jpeg" "jpg" "png" "ps" "eps")
598
  "Extensions of image files that can be inlined into LaTeX.
599 600
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
Bastien Guerry's avatar
Bastien Guerry committed
601
are OK.  When processing through dvi to Postscript, only ps and eps are
602 603
allowed.  The default we use here encompasses both."
  :group 'org-export-latex
604 605
  :type '(repeat (string :tag "Extension")))

Carsten Dominik's avatar
Carsten Dominik committed
606
(defcustom org-export-latex-coding-system nil
607
  "Coding system for the exported LaTeX file."
Carsten Dominik's avatar
Carsten Dominik committed
608 609 610
  :group 'org-export-latex
  :type 'coding-system)

611 612
(defgroup org-export-pdf nil
  "Options for exporting Org-mode files to PDF, via LaTeX."
613
  :tag "Org Export PDF"
614 615 616
  :group 'org-export-latex
  :group 'org-export)

617
(defcustom org-latex-to-pdf-process
Carsten Dominik's avatar
Carsten Dominik committed
618 619 620
  '("pdflatex -interaction nonstopmode -output-directory %o %f"
    "pdflatex -interaction nonstopmode -output-directory %o %f"
    "pdflatex -interaction nonstopmode -output-directory %o %f")
621 622
  "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
Carsten Dominik's avatar
Carsten Dominik committed
623 624 625 626
as a command.  %f in the command will be replaced by the full file name, %b
by the file base name (i.e. without extension) and %o by the base directory
of the file.

627
The reason why this is a list is that it usually takes several runs of
Carsten Dominik's avatar
Carsten Dominik committed
628
`pdflatex', maybe mixed with a call to `bibtex'.  Org does not have a clever
629
mechanism to detect which of these commands have to be run to get to a stable
630 631
result, and it also does not do any error checking.

Carsten Dominik's avatar
Carsten Dominik committed
632 633 634 635 636 637 638 639 640
By default, Org uses 3 runs of `pdflatex' to do the processing.  If you
have texi2dvi on your system and if that does not cause the infamous
egrep/locale bug:

     http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html

then `texi2dvi' is the superior choice.  Org does offer it as one
of the customize options.

641 642
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.
643
This function should accept the file name as its single argument."
644
  :group 'org-export-pdf
Carsten Dominik's avatar
Carsten Dominik committed
645 646
  :type '(choice
	  (repeat :tag "Shell command sequence"
647
		  (string :tag "Shell command"))
Carsten Dominik's avatar
Carsten Dominik committed
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
	  (const :tag "2 runs of pdflatex"
		 ("pdflatex -interaction nonstopmode -output-directory %o %f"
		   "pdflatex -interaction nonstopmode -output-directory %o %f"))
	  (const :tag "3 runs of pdflatex"
		 ("pdflatex -interaction nonstopmode -output-directory %o %f"
		   "pdflatex -interaction nonstopmode -output-directory %o %f"
		   "pdflatex -interaction nonstopmode -output-directory %o %f"))
	  (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
		 ("pdflatex -interaction nonstopmode -output-directory %o %f"
		   "bibtex %b"
		   "pdflatex -interaction nonstopmode -output-directory %o %f"
		   "pdflatex -interaction nonstopmode -output-directory %o %f"))
	  (const :tag "texi2dvi"
		 ("texi2dvi -p -b -c -V %f"))
	  (const :tag "rubber"
		 ("rubber -d --into %o %f"))
	  (function)))
665

666 667 668 669 670 671
(defcustom org-export-pdf-logfiles
  '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
  "The list of file extensions to consider as LaTeX logfiles."
  :group 'org-export-pdf
  :type '(repeat (string :tag "Extension")))

672
(defcustom org-export-pdf-remove-logfiles t
673
  "Non-nil means remove the logfiles produced by PDF production.
674
These are the .aux, .log, .out, and .toc files."
675
  :group 'org-export-pdf
676 677
  :type 'boolean)

678 679
;;; Hooks

680 681 682 683 684
(defvar org-export-latex-after-initial-vars-hook nil
  "Hook run before LaTeX export.
The exact moment is after the initial variables like org-export-latex-class
have been determined from the environment.")

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

688 689 690
(defvar org-export-latex-final-hook nil
  "Hook run in the finalized LaTeX buffer.")

691 692 693
(defvar org-export-latex-after-save-hook nil
  "Hook run in the finalized LaTeX buffer, after it has been saved.")

John Wiegley's avatar
John Wiegley committed
694
;;; Autoload functions:
695

Carsten Dominik's avatar
Carsten Dominik committed
696 697
;;;###autoload
(defun org-export-as-latex-batch ()
698 699 700
  "Call `org-export-as-latex', may be used in batch processing.
For example:

701 702 703 704
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
705 706 707 708
  (org-export-as-latex org-export-headline-levels 'hidden))

;;;###autoload
(defun org-export-as-latex-to-buffer (arg)
709
  "Call `org-export-as-latex` with output to a temporary buffer.
Carsten Dominik's avatar
Carsten Dominik committed
710 711 712
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*")
713 714
  (when org-export-show-temporary-export-buffer
    (switch-to-buffer-other-window "*Org LaTeX Export*")))
Carsten Dominik's avatar
Carsten Dominik committed
715 716 717 718 719

;;;###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
720
LaTeX.  This can be used in any buffer.  For example, you could
Carsten Dominik's avatar
Carsten Dominik committed
721 722 723 724 725
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
Bastien Guerry's avatar
Bastien Guerry committed
726
      (if (eq major-mode 'org-mode)
Carsten Dominik's avatar
Carsten Dominik committed
727 728 729 730
	  (setq latex (org-export-region-as-latex
		       beg end t 'string))
	(setq reg (buffer-substring beg end)
	      buf (get-buffer-create "*Org tmp*"))
731
	(with-current-buffer buf
Carsten Dominik's avatar
Carsten Dominik committed
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
	  (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
749
produced LaTeX as a string and leave no buffer behind.  For example,
Carsten Dominik's avatar
Carsten Dominik committed
750 751 752 753 754
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
755
in a window.  A non-interactive call will only return the buffer."
Carsten Dominik's avatar
Carsten Dominik committed
756
  (interactive "r\nP")
757
  (when (org-called-interactively-p 'any)
Carsten Dominik's avatar
Carsten Dominik committed
758 759
    (setq buffer "*Org LaTeX Export*"))
  (let ((transient-mark-mode t) (zmacs-regions t)
760
	ext-plist rtn)
761
    (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
Carsten Dominik's avatar
Carsten Dominik committed
762 763 764 765
    (goto-char end)
    (set-mark (point)) ;; to activate the region
    (goto-char beg)
    (setq rtn (org-export-as-latex
766
	       nil nil ext-plist
Carsten Dominik's avatar
Carsten Dominik committed
767 768
	       buffer body-only))
    (if (fboundp 'deactivate-mark) (deactivate-mark))
769
    (if (and (org-called-interactively-p 'any) (bufferp rtn))
Carsten Dominik's avatar
Carsten Dominik committed
770 771 772 773 774
	(switch-to-buffer-other-window rtn)
      rtn)))

;;;###autoload
(defun org-export-as-latex (arg &optional hidden ext-plist
775
				to-buffer body-only pub-dir)
776 777 778 779 780
  "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
781 782 783
convert them as description lists.
HIDDEN is obsolete and does nothing.
EXT-PLIST is a property list with
784 785 786 787 788 789
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,
790 791
simply return the content of \\begin{document}...\\end{document},
without even the \\begin{document} and \\end{document} commands.
792
when PUB-DIR is set, use this as the publishing directory."
Carsten Dominik's avatar
Carsten Dominik committed
793
  (interactive "P")
794
  (when (and (not body-only) arg (listp arg)) (setq body-only t))
795 796
  (run-hooks 'org-export-first-hook)

Carsten Dominik's avatar
Carsten Dominik committed
797 798 799 800 801 802 803 804 805 806
  ;; 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...")
807
  (org-unmodified
808 809 810
   (let ((inhibit-read-only t))
     (remove-text-properties (point-min) (point-max)
			     '(:org-license-to-kill nil))))
Carsten Dominik's avatar
Carsten Dominik committed
811
  (org-update-radio-target-regexp)
812
  (org-export-latex-set-initial-vars ext-plist arg)
813 814 815 816
  (setq org-export-opt-plist org-export-latex-options-plist
	org-export-footnotes-data (org-footnote-all-labels 'with-defs)
	org-export-footnotes-seen nil
	org-export-latex-footmark-seen nil)
817 818
  (org-install-letbind)
  (run-hooks 'org-export-latex-after-initial-vars-hook)
819
  (let* ((wcf (current-window-configuration))
820 821
	 (opt-plist
	  (org-export-process-option-filters org-export-latex-options-plist))
822
	 (region-p (org-region-active-p))
823 824
	 (rbeg (and region-p (region-beginning)))
	 (rend (and region-p (region-end)))
825
	 (subtree-p
826
	  (if (plist-get opt-plist :ignore-subtree-p)
827 828 829 830 831 832 833 834 835 836
	      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)))
837
	 ;; Make sure the variable contains the updated values.
838 839 840 841 842 843
	 (org-export-latex-options-plist (setq org-export-opt-plist opt-plist))
	 ;; The following two are dynamically scoped into other
	 ;; routines below.
	 (org-current-export-dir
	  (or pub-dir (org-export-directory :html opt-plist)))
	 (org-current-export-file buffer-file-name)
844 845 846 847 848
	 (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))
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
		    (and buffer-file-name
			 (file-name-sans-extension
			  (file-name-nondirectory buffer-file-name)))
		    "No Title"))
	 (filename
	  (and (not to-buffer)
	       (concat
		(file-name-as-directory
		 (or pub-dir
		     (org-export-directory :LaTeX ext-plist)))
		(file-name-sans-extension
		 (or (and subtree-p
			  (org-entry-get rbeg "EXPORT_FILE_NAME" t))
		     (file-name-nondirectory ;sans-extension
		      (or buffer-file-name
864
			  (error "Don't know which export file to use")))))
865 866 867 868 869 870 871
		".tex")))
	 (filename
	  (and filename
	       (if (equal (file-truename filename)
			  (file-truename (or buffer-file-name "dummy.org")))
		   (concat filename ".tex")
		 filename)))
Bastien Guerry's avatar
Bastien Guerry committed
872 873
	 (auto-insert nil); Avoid any auto-insert stuff for the new file
	 (TeX-master t) ; Avoid the Query for TeX master from AUCTeX
Carsten Dominik's avatar
Carsten Dominik committed
874 875 876 877 878 879 880
	 (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)
881
	 (header (org-export-latex-make-header title opt-plist))
882
	 (skip (cond (subtree-p nil)
883
		     (region-p nil)
884
		     (t (plist-get opt-plist :skip-before-1st-heading))))
Carsten Dominik's avatar
Carsten Dominik committed
885
	 (text (plist-get opt-plist :text))
886 887 888 889 890
	 (org-export-preprocess-hook
	  (cons
	   `(lambda () (org-set-local 'org-complex-heading-regexp
				      ,org-export-latex-complex-heading-re))
	   org-export-preprocess-hook))
891
	 (first-lines (if skip "" (org-export-latex-first-lines
892 893 894 895 896 897 898
				   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
899 900 901 902 903 904
	 (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))
905
	 (region (buffer-substring
Carsten Dominik's avatar
Carsten Dominik committed
906 907
		  (if region-p (region-beginning) (point-min))
		  (if region-p (region-end) (point-max))))
908 909 910 911 912
	 (text
	  (and text (string-match "\\S-" text)
	       (org-export-preprocess-string
		text
		:emph-multiline t
913
		:for-backend 'latex
914 915 916 917 918 919 920
		:comments nil
		:tags (plist-get opt-plist :tags)
		:priority (plist-get opt-plist :priority)
		:footnotes (plist-get opt-plist :footnotes)
		:drawers (plist-get opt-plist :drawers)
		:timestamps (plist-get opt-plist :timestamps)
		:todo-keywords (plist-get opt-plist :todo-keywords)
921
		:tasks (plist-get opt-plist :tasks)
922 923 924 925 926
		:add-text nil
		:skip-before-1st-heading skip
		:select-tags nil
		:exclude-tags nil
		:LaTeX-fragments nil)))
Carsten Dominik's avatar
Carsten Dominik committed
927
	 (string-for-export
928
	  (org-export-preprocess-string
929 930
	   region
	   :emph-multiline t
931
	   :for-backend 'latex
932 933 934
	   :comments nil
	   :tags (plist-get opt-plist :tags)
	   :priority (plist-get opt-plist :priority)
935
	   :footnotes (plist-get opt-plist :footnotes)
936
	   :drawers (plist-get opt-plist :drawers)
937
	   :timestamps (plist-get opt-plist :timestamps)
938
	   :todo-keywords (plist-get opt-plist :todo-keywords)
939
	   :tasks (plist-get opt-plist :tasks)
940 941 942 943 944
	   :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)))
945

946
    (set-buffer buffer)
Carsten Dominik's avatar
Carsten Dominik committed
947
    (erase-buffer)
948
    (org-install-letbind)
John Wiegley's avatar
John Wiegley committed
949

950 951 952
    (and (fboundp 'set-buffer-file-coding-system)
	 (set-buffer-file-coding-system coding-system-for-write))

953
    ;; insert the header and initial document commands
954
    (unless (or (eq to-buffer 'string) body-only)
955
      (insert header))
956 957 958

    ;; insert text found in #+TEXT
    (when (and text (not (eq to-buffer 'string)))
959 960 961
      (insert (org-export-latex-content
	       text '(lists tables fixed-width keywords))
	       "\n\n"))
962 963

    ;; insert lines before the first headline
964
    (unless (or skip (string-match "^\\*" first-lines))
965
      (insert first-lines))
Carsten Dominik's avatar
Carsten Dominik committed
966

967
    ;; export the content of headlines
Carsten Dominik's avatar
Carsten Dominik committed
968 969 970 971
    (org-export-latex-global
     (with-temp-buffer
       (insert string-for-export)
       (goto-char (point-min))
972 973 974
       (when (re-search-forward "^\\(\\*+\\) " nil t)
	 (let* ((asters (length (match-string 1)))
		(level (if odd (- asters 2) (- asters 1))))
975
	   (setq org-export-latex-add-level
976 977 978 979
		 (if odd (1- (/ (1+ asters) 2)) (1- asters)))
	   (org-export-latex-parse-global level odd)))))

    ;; finalization
Carsten Dominik's avatar
Carsten Dominik committed
980
    (unless body-only (insert "\n\\end{document}"))
981

982 983 984 985 986
    ;; Attach description terms to the \item macro
    (goto-char (point-min))
    (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t)
      (delete-region (match-beginning 1) (match-end 1)))

987 988 989 990 991 992 993 994 995 996
    ;; 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)))

997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
    ;; Cleanup forced line ends in items where they are not needed
    (goto-char (point-min))
    (while (re-search-forward
	    "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin"
	    nil t)
      (delete-region (match-beginning 1) (match-end 1)))
    (goto-char (point-min))
    (while (re-search-forward
	    "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*"
	    nil t)
      (if (looking-at "[\n \t]+")
	  (replace-match "\n")))

1010
    (run-hooks 'org-export-latex-final-hook)
1011 1012 1013
    (if to-buffer
	(unless (eq major-mode 'latex-mode) (latex-mode))
      (save-buffer))
1014 1015
    (org-export-latex-fix-inputenc)
    (run-hooks 'org-export-latex-after-save-hook)
Carsten Dominik's avatar
Carsten Dominik committed
1016
    (goto-char (point-min))
1017 1018
    (or (org-export-push-to-kill-ring "LaTeX")
	(message "Exporting to LaTeX...done"))
1019 1020 1021 1022 1023 1024 1025
    (prog1
	(if (eq to-buffer 'string)
	    (prog1 (buffer-substring (point-min) (point-max))
	      (kill-buffer (current-buffer)))
	  (current-buffer))
      (set-window-configuration wcf))))

1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
;;;###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)))
1037
	 (pdffile (concat base ".pdf"))
1038 1039 1040 1041 1042 1043 1044
	 (cmds (if (eq org-export-latex-listings 'minted)
		   ;; automatically add -shell-escape when needed
		   (mapcar (lambda (cmd)
			     (replace-regexp-in-string
			      "pdflatex " "pdflatex -shell-escape " cmd))
			   org-latex-to-pdf-process)
		 org-latex-to-pdf-process))
1045 1046 1047 1048 1049
	 (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))))
Carsten Dominik's avatar
Carsten Dominik committed
1050
	 cmd output-dir errors)
1051
    (with-current-buffer outbuf (erase-buffer))
Carsten Dominik's avatar
Carsten Dominik committed
1052 1053
    (message (concat "Processing LaTeX file " file "..."))
    (setq output-dir (file-name-directory file))
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
    (with-current-buffer lbuf
      (save-excursion
	(if (and cmds (symbolp cmds))
	    (funcall cmds (shell-quote-argument 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 "%f" cmd)
	      (setq cmd (replace-match
			 (save-match-data
			   (shell-quote-argument file))
			 t t cmd)))
	    (while (string-match "%o" cmd)
	      (setq cmd (replace-match
			 (save-match-data
			   (shell-quote-argument output-dir))
			 t t cmd)))
	    (shell-command cmd outbuf)))))
Carsten Dominik's avatar
Carsten Dominik committed
1076 1077
    (message (concat "Processing LaTeX file " file "...done"))
    (setq errors (org-export-latex-get-error outbuf))
1078
    (if (not (file-exists-p pdffile))
Carsten Dominik's avatar
Carsten Dominik committed
1079 1080
	(error (concat "PDF file " pdffile " was not produced"
		       (if errors (concat ":" errors "") "")))
1081 1082
      (set-window-configuration wconfig)
      (when org-export-pdf-remove-logfiles
1083
	(dolist (ext org-export-pdf-logfiles)
1084 1085
	  (setq file (concat base "." ext))
	  (and (file-exists-p file) (delete-file file))))
Carsten Dominik's avatar
Carsten Dominik committed
1086 1087 1088 1089 1090
      (message (concat
		"Exporting to PDF...done"
		(if errors
		    (concat ", with some errors:" errors)
		  "")))
1091 1092
      pdffile)))

Carsten Dominik's avatar
Carsten Dominik committed