Merge from emacs-24; up to 2012-12-26T22:30:58Z!yamaoka@jpl.org
[bpt/emacs.git] / lisp / org / org-latex.el
CommitLineData
c8d0cf5c 1;;; org-latex.el --- LaTeX exporter for org-mode
72a81656 2;;
ab422c4d 3;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
15841868
JW
4;;
5;; Emacs Lisp Archive Entry
c8d0cf5c 6;; Filename: org-latex.el
e66ba1df 7;; Author: Bastien Guerry <bzg AT gnu DOT org>
c8d0cf5c 8;; Maintainer: Carsten Dominik <carsten.dominik AT gmail DOT com>
15841868
JW
9;; Keywords: org, wp, tex
10;; Description: Converts an org-mode buffer into LaTeX
b1fc2b50 11
72a81656 12;; This file is part of GNU Emacs.
b1fc2b50
GM
13
14;; GNU Emacs is free software: you can redistribute it and/or modify
15;; it under the terms of the GNU General Public License as published by
16;; the Free Software Foundation, either version 3 of the License, or
17;; (at your option) any later version.
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
db9c3fb1 24;; You should have received a copy of the GNU General Public License
b1fc2b50
GM
25;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
72a81656 27;;; Commentary:
0b8568f5
JW
28;;
29;; This library implements a LaTeX exporter for org-mode.
db9c3fb1 30;;
c8d0cf5c 31;; It is part of Org and will be autoloaded
db9c3fb1 32;;
72a81656 33;; The interactive functions are similar to those of the HTML exporter:
db9c3fb1 34;;
72a81656 35;; M-x `org-export-as-latex'
71d35b24
CD
36;; M-x `org-export-as-pdf'
37;; M-x `org-export-as-pdf-and-open'
72a81656
CD
38;; M-x `org-export-as-latex-batch'
39;; M-x `org-export-as-latex-to-buffer'
40;; M-x `org-export-region-as-latex'
41;; M-x `org-replace-region-by-latex'
4b67ea89 42;;
72a81656
CD
43;;; Code:
44
0b8568f5
JW
45(eval-when-compile
46 (require 'cl))
47
72a81656 48(require 'footnote)
0b8568f5 49(require 'org)
20908596 50(require 'org-exp)
8d642074 51(require 'org-macs)
ed21c5c8 52(require 'org-beamer)
72a81656 53
0b8568f5 54;;; Variables:
db9c3fb1 55(defvar org-export-latex-class nil)
ed21c5c8 56(defvar org-export-latex-class-options nil)
db9c3fb1
CD
57(defvar org-export-latex-header nil)
58(defvar org-export-latex-append-header nil)
59(defvar org-export-latex-options-plist nil)
60(defvar org-export-latex-todo-keywords-1 nil)
c8d0cf5c
CD
61(defvar org-export-latex-complex-heading-re nil)
62(defvar org-export-latex-not-done-keywords nil)
63(defvar org-export-latex-done-keywords nil)
64(defvar org-export-latex-display-custom-times nil)
db9c3fb1
CD
65(defvar org-export-latex-all-targets-re nil)
66(defvar org-export-latex-add-level 0)
3ab2c837
BG
67(defvar org-export-latex-footmark-seen nil
68 "List of footnotes markers seen so far by exporter.")
db9c3fb1
CD
69(defvar org-export-latex-sectioning "")
70(defvar org-export-latex-sectioning-depth 0)
0bd48b37
CD
71(defvar org-export-latex-special-keyword-regexp
72 (concat "\\<\\(" org-scheduled-string "\\|"
73 org-deadline-string "\\|"
74 org-closed-string"\\)")
75 "Regexp matching special time planning keywords plus the time after it.")
3ab2c837
BG
76(defvar org-re-quote) ; dynamically scoped from org.el
77(defvar org-commentsp) ; dynamically scoped from org.el
0b8568f5 78
db9c3fb1 79;;; User variables:
72a81656 80
621f83e4
CD
81(defgroup org-export-latex nil
82 "Options for exporting Org-mode files to LaTeX."
83 :tag "Org Export LaTeX"
84 :group 'org-export)
85
db9c3fb1
CD
86(defcustom org-export-latex-default-class "article"
87 "The default LaTeX class."
88 :group 'org-export-latex
89 :type '(string :tag "LaTeX class"))
72a81656 90
db9c3fb1
CD
91(defcustom org-export-latex-classes
92 '(("article"
ed21c5c8 93 "\\documentclass[11pt]{article}"
db9c3fb1
CD
94 ("\\section{%s}" . "\\section*{%s}")
95 ("\\subsection{%s}" . "\\subsection*{%s}")
96 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
97 ("\\paragraph{%s}" . "\\paragraph*{%s}")
98 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
99 ("report"
ed21c5c8 100 "\\documentclass[11pt]{report}"
db9c3fb1
CD
101 ("\\part{%s}" . "\\part*{%s}")
102 ("\\chapter{%s}" . "\\chapter*{%s}")
103 ("\\section{%s}" . "\\section*{%s}")
104 ("\\subsection{%s}" . "\\subsection*{%s}")
105 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
106 ("book"
ed21c5c8 107 "\\documentclass[11pt]{book}"
db9c3fb1
CD
108 ("\\part{%s}" . "\\part*{%s}")
109 ("\\chapter{%s}" . "\\chapter*{%s}")
110 ("\\section{%s}" . "\\section*{%s}")
111 ("\\subsection{%s}" . "\\subsection*{%s}")
ed21c5c8
CD
112 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
113 ("beamer"
114 "\\documentclass{beamer}"
115 org-beamer-sectioning
116 ))
db9c3fb1
CD
117 "Alist of LaTeX classes and associated header and structure.
118If #+LaTeX_CLASS is set in the buffer, use its value and the
119associated information. Here is the structure of each cell:
120
121 \(class-name
122 header-string
71d35b24 123 (numbered-section . unnumbered-section\)
db9c3fb1
CD
124 ...\)
125
ed21c5c8
CD
126The header string
127-----------------
128
129The HEADER-STRING is the header that will be inserted into the LaTeX file.
130It should contain the \\documentclass macro, and anything else that is needed
131for this setup. To this header, the following commands will be added:
132
133- Calls to \\usepackage for all packages mentioned in the variables
134 `org-export-latex-default-packages-alist' and
135 `org-export-latex-packages-alist'. Thus, your header definitions should
136 avoid to also request these packages.
137
138- Lines specified via \"#+LaTeX_HEADER:\"
139
140If you need more control about the sequence in which the header is built
141up, or if you want to exclude one of these building blocks for a particular
142class, you can use the following macro-like placeholders.
143
144 [DEFAULT-PACKAGES] \\usepackage statements for default packages
145 [NO-DEFAULT-PACKAGES] do not include any of the default packages
86fbb8ca 146 [PACKAGES] \\usepackage statements for packages
ed21c5c8
CD
147 [NO-PACKAGES] do not include the packages
148 [EXTRA] the stuff from #+LaTeX_HEADER
149 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
86fbb8ca 150 [BEAMER-HEADER-EXTRA] the beamer extra headers
ed21c5c8
CD
151
152So a header like
153
154 \\documentclass{article}
155 [NO-DEFAULT-PACKAGES]
156 [EXTRA]
157 \\providecommand{\\alert}[1]{\\textbf{#1}}
158 [PACKAGES]
159
160will omit the default packages, and will include the #+LaTeX_HEADER lines,
161then have a call to \\providecommand, and then place \\usepackage commands
162based on the content of `org-export-latex-packages-alist'.
163
164If your header or `org-export-latex-default-packages-alist' inserts
165\"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be replaced with
166a coding system derived from `buffer-file-coding-system'. See also the
167variable `org-export-latex-inputenc-alist' for a way to influence this
168mechanism.
169
170The sectioning structure
171------------------------
172
173The sectioning structure of the class is given by the elements following
174the header string. For each sectioning level, a number of strings is
175specified. A %s formatter is mandatory in each section string and will
176be replaced by the title of the section.
71d35b24
CD
177
178Instead of a cons cell (numbered . unnumbered), you can also provide a list
ed21c5c8 179of 2 or 4 elements,
71d35b24
CD
180
181 (numbered-open numbered-close)
182
183or
184
185 (numbered-open numbered-close unnumbered-open unnumbered-close)
186
ed21c5c8 187providing opening and closing strings for a LaTeX environment that should
71d35b24 188represent the document section. The opening clause should have a %s
ed21c5c8
CD
189to represent the section title.
190
191Instead of a list of sectioning commands, you can also specify a
192function name. That function will be called with two parameters,
193the (reduced) level of the headline, and the headline text. The function
194must return a cons cell with the (possibly modified) headline text, and the
195sectioning list in the cdr."
72a81656 196 :group 'org-export-latex
ff4be292 197 :type '(repeat
db9c3fb1
CD
198 (list (string :tag "LaTeX class")
199 (string :tag "LaTeX header")
71d35b24
CD
200 (repeat :tag "Levels" :inline t
201 (choice
202 (cons :tag "Heading"
ed21c5c8
CD
203 (string :tag " numbered")
204 (string :tag "unnumbered"))
71d35b24 205 (list :tag "Environment"
ed21c5c8
CD
206 (string :tag "Opening (numbered)")
207 (string :tag "Closing (numbered)")
71d35b24 208 (string :tag "Opening (unnumbered)")
ed21c5c8
CD
209 (string :tag "Closing (unnumbered)"))
210 (function :tag "Hook computing sectioning"))))))
211
212(defcustom org-export-latex-inputenc-alist nil
213 "Alist of inputenc coding system names, and what should really be used.
214For example, adding an entry
215
216 (\"utf8\" . \"utf8x\")
217
218will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
219are written as utf8 files."
220 :group 'org-export-latex
153ae947 221 :version "24.1"
ed21c5c8
CD
222 :type '(repeat
223 (cons
224 (string :tag "Derived from buffer")
225 (string :tag "Use this instead"))))
226
72a81656
CD
227
228(defcustom org-export-latex-emphasis-alist
15841868
JW
229 '(("*" "\\textbf{%s}" nil)
230 ("/" "\\emph{%s}" nil)
231 ("_" "\\underline{%s}" nil)
54a0dee5 232 ("+" "\\st{%s}" nil)
3ab2c837 233 ("=" "\\protectedtexttt" t)
c8d0cf5c 234 ("~" "\\verb" t))
15841868
JW
235 "Alist of LaTeX expressions to convert emphasis fontifiers.
236Each element of the list is a list of three elements.
237The first element is the character used as a marker for fontification.
8223b1d2 238The second element is a format string to wrap fontified text with.
8bfe682a 239If it is \"\\verb\", Org will automatically select a delimiter
3ab2c837
BG
240character that is not in the string. \"\\protectedtexttt\" will use \\texttt
241to typeset and try to protect special characters.
15841868
JW
242The third element decides whether to protect converted text from other
243conversions."
72a81656
CD
244 :group 'org-export-latex
245 :type 'alist)
246
15841868
JW
247(defcustom org-export-latex-title-command "\\maketitle"
248 "The command used to insert the title just after \\begin{document}.
249If this string contains the formatting specification \"%s\" then
8223b1d2 250it will be used as a format string, passing the title as an
15841868
JW
251argument."
252 :group 'org-export-latex
253 :type 'string)
254
c8d0cf5c
CD
255(defcustom org-export-latex-import-inbuffer-stuff nil
256 "Non-nil means define TeX macros for Org's inbuffer definitions.
257For example \orgTITLE for #+TITLE."
258 :group 'org-export-latex
259 :type 'boolean)
260
db9c3fb1 261(defcustom org-export-latex-date-format
3ab2c837 262 "\\today"
72a81656
CD
263 "Format string for \\date{...}."
264 :group 'org-export-latex
265 :type 'string)
266
c8d0cf5c
CD
267(defcustom org-export-latex-todo-keyword-markup "\\textbf{%s}"
268 "Markup for TODO keywords, as a printf format.
269This can be a single format for all keywords, a cons cell with separate
270formats for not-done and done states, or an association list with setup
271for individual keywords. If a keyword shows up for which there is no
272markup defined, the first one in the association list will be used."
273 :group 'org-export-latex
274 :type '(choice
275 (string :tag "Default")
276 (cons :tag "Distinguish undone and done"
277 (string :tag "Not-DONE states")
278 (string :tag "DONE states"))
279 (repeat :tag "Per keyword markup"
280 (cons
281 (string :tag "Keyword")
282 (string :tag "Markup")))))
283
afe98dfa
CD
284(defcustom org-export-latex-tag-markup "\\textbf{%s}"
285 "Markup for tags, as a printf format."
286 :group 'org-export-latex
153ae947 287 :version "24.1"
afe98dfa
CD
288 :type 'string)
289
c8d0cf5c
CD
290(defcustom org-export-latex-timestamp-markup "\\textit{%s}"
291 "A printf format string to be applied to time stamps."
292 :group 'org-export-latex
293 :type 'string)
294
3ab2c837
BG
295(defcustom org-export-latex-timestamp-inactive-markup "\\textit{%s}"
296 "A printf format string to be applied to inactive time stamps."
297 :group 'org-export-latex
153ae947 298 :version "24.1"
3ab2c837
BG
299 :type 'string)
300
c8d0cf5c
CD
301(defcustom org-export-latex-timestamp-keyword-markup "\\texttt{%s}"
302 "A printf format string to be applied to time stamps."
303 :group 'org-export-latex
304 :type 'string)
305
acedf35c
CD
306(defcustom org-export-latex-href-format "\\href{%s}{%s}"
307 "A printf format string to be applied to href links.
14e1337f
BG
308The format must contain either two %s instances or just one.
309If it contains two %s instances, the first will be filled with
3ab2c837
BG
310the link, the second with the link description. If it contains
311only one, the %s will be filled with the link."
acedf35c 312 :group 'org-export-latex
153ae947 313 :version "24.1"
acedf35c
CD
314 :type 'string)
315
316(defcustom org-export-latex-hyperref-format "\\hyperref[%s]{%s}"
ed21c5c8 317 "A printf format string to be applied to hyperref links.
3ab2c837
BG
318The format must contain one or two %s instances. The first one
319will be filled with the link, the second with its description."
ed21c5c8 320 :group 'org-export-latex
153ae947 321 :version "24.1"
ed21c5c8
CD
322 :type 'string)
323
8223b1d2
BG
324(defcustom org-export-latex-hyperref-options-format
325 "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={Emacs Org-mode version %s}}\n"
326 "A format string for hyperref options.
327When non-nil, it must contain three %s format specifications
328which will respectively be replaced by the document's keywords,
329its description and the Org's version number, as a string. Set
330this option to the empty string if you don't want to include
331hyperref options altogether."
332 :type 'string
333 :version "24.3"
334 :group 'org-export-latex)
335
3ab2c837
BG
336(defcustom org-export-latex-footnote-separator "\\textsuperscript{,}\\,"
337 "Text used to separate footnotes."
338 :group 'org-export-latex
153ae947 339 :version "24.1"
3ab2c837
BG
340 :type 'string)
341
342(defcustom org-export-latex-quotes
343 '(("fr" ("\\(\\s-\\|[[(]\\)\"" . "«~") ("\\(\\S-\\)\"" . "~»") ("\\(\\s-\\|(\\)'" . "'"))
344 ("en" ("\\(\\s-\\|[[(]\\)\"" . "``") ("\\(\\S-\\)\"" . "''") ("\\(\\s-\\|(\\)'" . "`")))
345 "Alist for quotes to use when converting english double-quotes.
346
347The CAR of each item in this alist is the language code.
348The CDR of each item in this alist is a list of three CONS:
349- the first CONS defines the opening quote;
350- the second CONS defines the closing quote;
351- the last CONS defines single quotes.
352
353For each item in a CONS, the first string is a regexp
354for allowed characters before/after the quote, the second
355string defines the replacement string for this quote."
356 :group 'org-export-latex
153ae947 357 :version "24.1"
3ab2c837
BG
358 :type '(list
359 (cons :tag "Opening quote"
360 (string :tag "Regexp for char before")
361 (string :tag "Replacement quote "))
362 (cons :tag "Closing quote"
363 (string :tag "Regexp for char after ")
364 (string :tag "Replacement quote "))
365 (cons :tag "Single quote"
366 (string :tag "Regexp for char before")
367 (string :tag "Replacement quote "))))
368
d5098885 369(defcustom org-export-latex-tables-verbatim nil
621f83e4 370 "When non-nil, tables are exported verbatim."
d5098885
JW
371 :group 'org-export-latex
372 :type 'boolean)
373
c8d0cf5c
CD
374(defcustom org-export-latex-tables-centered t
375 "When non-nil, tables are exported in a center environment."
376 :group 'org-export-latex
377 :type 'boolean)
378
e66ba1df
BG
379(defcustom org-export-latex-table-caption-above t
380 "When non-nil, the caption is set above the table. When nil,
381the caption is set below the table."
382 :group 'org-export-latex
153ae947 383 :version "24.1"
e66ba1df
BG
384 :type 'boolean)
385
db9c3fb1 386(defcustom org-export-latex-tables-column-borders nil
c8d0cf5c
CD
387 "When non-nil, grouping columns can cause outer vertical lines in tables.
388When nil, grouping causes only separation lines between groups."
db9c3fb1
CD
389 :group 'org-export-latex
390 :type 'boolean)
72a81656 391
8223b1d2
BG
392(defcustom org-export-latex-tables-tstart nil
393 "LaTeX command for top rule for tables."
394 :group 'org-export-latex
395 :version "24.1"
396 :type '(choice
397 (const :tag "Nothing" nil)
398 (string :tag "String")
399 (const :tag "Booktabs default: \\toprule" "\\toprule")))
400
401(defcustom org-export-latex-tables-hline "\\hline"
402 "LaTeX command to use for a rule somewhere in the middle of a table."
403 :group 'org-export-latex
404 :version "24.1"
405 :type '(choice
406 (string :tag "String")
407 (const :tag "Standard: \\hline" "\\hline")
408 (const :tag "Booktabs default: \\midrule" "\\midrule")))
409
410(defcustom org-export-latex-tables-tend nil
411 "LaTeX command for bottom rule for tables."
412 :group 'org-export-latex
413 :version "24.1"
414 :type '(choice
415 (const :tag "Nothing" nil)
416 (string :tag "String")
417 (const :tag "Booktabs default: \\bottomrule" "\\bottomrule")))
418
c8d0cf5c 419(defcustom org-export-latex-low-levels 'itemize
621f83e4
CD
420 "How to convert sections below the current level of sectioning.
421This is specified by the `org-export-headline-levels' option or the
422value of \"H:\" in Org's #+OPTION line.
15841868 423
c8d0cf5c
CD
424This can be either nil (skip the sections), `description', `itemize',
425or `enumerate' (convert the sections as the corresponding list type), or
426a string to be used instead of \\section{%s}. In this latter case,
427the %s stands here for the inserted headline and is mandatory.
428
429It may also be a list of three string to define a user-defined environment
430that should be used. The first string should be the like
431\"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
8bfe682a 432to two occurrences of %s for the title and a label, respectively. The third
c8d0cf5c 433string should be like \"\\end{itemize\"."
72a81656
CD
434 :group 'org-export-latex
435 :type '(choice (const :tag "Ignore" nil)
c8d0cf5c
CD
436 (const :tag "Convert as descriptive list" description)
437 (const :tag "Convert as itemized list" itemize)
438 (const :tag "Convert as enumerated list" enumerate)
439 (list :tag "User-defined environment"
440 :value ("\\begin{itemize}" "\\end{itemize}" "\\item %s")
441 (string :tag "Start")
442 (string :tag "End")
443 (string :tag "item"))
72a81656
CD
444 (string :tag "Use a section string" :value "\\subparagraph{%s}")))
445
0bd48b37 446(defcustom org-export-latex-list-parameters
3ab2c837 447 '(:cbon "$\\boxtimes$" :cboff "$\\Box$" :cbtrans "$\\boxminus$")
0bd48b37
CD
448 "Parameters for the LaTeX list exporter.
449These parameters will be passed on to `org-list-to-latex', which in turn
450will pass them (combined with the LaTeX default list parameters) to
451`org-list-to-generic'."
452 :group 'org-export-latex
453 :type 'plist)
454
c8d0cf5c 455(defcustom org-export-latex-verbatim-wrap
e66ba1df 456 '("\\begin{verbatim}\n" . "\\end{verbatim}")
c8d0cf5c
CD
457 "Environment to be wrapped around a fixed-width section in LaTeX export.
458This is a cons with two strings, to be added before and after the
459fixed-with text.
460
461Defaults to \\begin{verbatim} and \\end{verbatim}."
462 :group 'org-export-translation
463 :group 'org-export-latex
464 :type '(cons (string :tag "Open")
465 (string :tag "Close")))
466
54a0dee5 467(defcustom org-export-latex-listings nil
ed21c5c8 468 "Non-nil means export source code using the listings package.
54a0dee5
CD
469This package will fontify source code, possibly even with color.
470If you want to use this, you also need to make LaTeX use the
471listings package, and if you want to have color, the color
472package. Just add these to `org-export-latex-packages-alist',
473for example using customize, or with something like
474
475 (require 'org-latex)
476 (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
afe98dfa
CD
477 (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))
478
479Alternatively,
480
481 (setq org-export-latex-listings 'minted)
482
483causes source code to be exported using the minted package as
3ab2c837 484opposed to listings. If you want to use minted, you need to add
afe98dfa
CD
485the minted package to `org-export-latex-packages-alist', for
486example using customize, or with
487
488 (require 'org-latex)
489 (add-to-list 'org-export-latex-packages-alist '(\"\" \"minted\"))
490
3ab2c837
BG
491In addition, it is necessary to install
492pygments (http://pygments.org), and to configure the variable
afe98dfa
CD
493`org-latex-to-pdf-process' so that the -shell-escape option is
494passed to pdflatex.
495"
54a0dee5
CD
496 :group 'org-export-latex
497 :type 'boolean)
498
499(defcustom org-export-latex-listings-langs
afe98dfa 500 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
54a0dee5
CD
501 (c "C") (cc "C++")
502 (fortran "fortran")
503 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
504 (html "HTML") (xml "XML")
505 (tex "TeX") (latex "TeX")
506 (shell-script "bash")
507 (gnuplot "Gnuplot")
508 (ocaml "Caml") (caml "Caml")
86fbb8ca 509 (sql "SQL") (sqlite "sql"))
54a0dee5
CD
510 "Alist mapping languages to their listing language counterpart.
511The key is a symbol, the major mode symbol without the \"-mode\".
512The value is the string that should be inserted as the language parameter
513for the listings package. If the mode name and the listings name are
514the same, the language does not need an entry in this list - but it does not
515hurt if it is present."
516 :group 'org-export-latex
517 :type '(repeat
518 (list
519 (symbol :tag "Major mode ")
520 (string :tag "Listings language"))))
521
afe98dfa
CD
522(defcustom org-export-latex-listings-w-names t
523 "Non-nil means export names of named code blocks.
524Code blocks exported with the listings package (controlled by the
525`org-export-latex-listings' variable) can be named in the style
526of noweb."
527 :group 'org-export-latex
153ae947 528 :version "24.1"
afe98dfa
CD
529 :type 'boolean)
530
531(defcustom org-export-latex-minted-langs
532 '((emacs-lisp "common-lisp")
533 (cc "c++")
534 (cperl "perl")
535 (shell-script "bash")
536 (caml "ocaml"))
537 "Alist mapping languages to their minted language counterpart.
538The key is a symbol, the major mode symbol without the \"-mode\".
539The value is the string that should be inserted as the language parameter
540for the minted package. If the mode name and the listings name are
541the same, the language does not need an entry in this list - but it does not
542hurt if it is present.
543
544Note that minted uses all lower case for language identifiers,
545and that the full list of language identifiers can be obtained
546with:
547pygmentize -L lexers
548"
549 :group 'org-export-latex
153ae947 550 :version "24.1"
afe98dfa
CD
551 :type '(repeat
552 (list
553 (symbol :tag "Major mode ")
554 (string :tag "Listings language"))))
555
3ab2c837
BG
556(defcustom org-export-latex-listings-options nil
557 "Association list of options for the latex listings package.
558
559These options are supplied as a comma-separated list to the
8223b1d2 560\\lstset command. Each element of the association list should be
3ab2c837 561a list containing two strings: the name of the option, and the
8223b1d2 562value. For example,
3ab2c837
BG
563
564 (setq org-export-latex-listings-options
565 '((\"basicstyle\" \"\\small\")
566 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
567
568will typeset the code in a small size font with underlined, bold
569black keywords.
570
571Note that the same options will be applied to blocks of all
572languages."
573 :group 'org-export-latex
153ae947 574 :version "24.1"
3ab2c837
BG
575 :type '(repeat
576 (list
577 (string :tag "Listings option name ")
578 (string :tag "Listings option value"))))
579
580(defcustom org-export-latex-minted-options nil
581 "Association list of options for the latex minted package.
582
583These options are supplied within square brackets in
8223b1d2 584\\begin{minted} environments. Each element of the alist should be
3ab2c837 585a list containing two strings: the name of the option, and the
8223b1d2 586value. For example,
3ab2c837
BG
587
588 (setq org-export-latex-minted-options
589 '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
590
591will result in src blocks being exported with
592
593\\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
594
8223b1d2 595as the start of the minted environment. Note that the same
3ab2c837
BG
596options will be applied to blocks of all languages."
597 :group 'org-export-latex
153ae947 598 :version "24.1"
3ab2c837
BG
599 :type '(repeat
600 (list
601 (string :tag "Minted option name ")
602 (string :tag "Minted option value"))))
603
604(defvar org-export-latex-custom-lang-environments nil
605 "Association list mapping languages to language-specific latex
606 environments used during export of src blocks by the listings
8223b1d2 607 and minted latex packages. For example,
3ab2c837
BG
608
609 (setq org-export-latex-custom-lang-environments
610 '((python \"pythoncode\")))
611
612 would have the effect that if org encounters begin_src python
613 during latex export it will output
614
615 \\begin{pythoncode}
616 <src block body>
617 \\end{pythoncode}")
618
d5098885 619(defcustom org-export-latex-remove-from-headlines
0bd48b37 620 '(:todo nil :priority nil :tags nil)
86fbb8ca 621 "A plist of keywords to remove from headlines. OBSOLETE.
72a81656
CD
622Non-nil means remove this keyword type from the headline.
623
0bd48b37
CD
624Don't remove the keys, just change their values.
625
626Obsolete, this variable is no longer used. Use the separate
627variables `org-export-with-todo-keywords', `org-export-with-priority',
628and `org-export-with-tags' instead."
72a81656
CD
629 :type 'plist
630 :group 'org-export-latex)
631
3ab2c837 632(defcustom org-export-latex-image-default-option "width=.9\\linewidth"
72a81656
CD
633 "Default option for images."
634 :group 'org-export-latex
db9c3fb1 635 :type 'string)
72a81656 636
3ab2c837
BG
637(defcustom org-latex-default-figure-position "htb"
638 "Default position for latex figures."
639 :group 'org-export-latex
153ae947 640 :version "24.1"
3ab2c837
BG
641 :type 'string)
642
86fbb8ca
CD
643(defcustom org-export-latex-tabular-environment "tabular"
644 "Default environment used to build tables."
645 :group 'org-export-latex
153ae947 646 :version "24.1"
86fbb8ca
CD
647 :type 'string)
648
8223b1d2
BG
649(defcustom org-export-latex-link-with-unknown-path-format "\\texttt{%s}"
650 "Format string for links with unknown path type."
651 :group 'org-export-latex
652 :version "24.3"
653 :type 'string)
654
0bd48b37 655(defcustom org-export-latex-inline-image-extensions
c8d0cf5c 656 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
0bd48b37 657 "Extensions of image files that can be inlined into LaTeX.
c8d0cf5c
CD
658Note that the image extension *actually* allowed depend on the way the
659LaTeX file is processed. When used with pdflatex, pdf, jpg and png images
e66ba1df 660are OK. When processing through dvi to Postscript, only ps and eps are
c8d0cf5c
CD
661allowed. The default we use here encompasses both."
662 :group 'org-export-latex
0bd48b37
CD
663 :type '(repeat (string :tag "Extension")))
664
72a81656 665(defcustom org-export-latex-coding-system nil
86fbb8ca 666 "Coding system for the exported LaTeX file."
72a81656
CD
667 :group 'org-export-latex
668 :type 'coding-system)
669
71d35b24
CD
670(defgroup org-export-pdf nil
671 "Options for exporting Org-mode files to PDF, via LaTeX."
ed21c5c8 672 :tag "Org Export PDF"
71d35b24
CD
673 :group 'org-export-latex
674 :group 'org-export)
675
c8d0cf5c 676(defcustom org-latex-to-pdf-process
afe98dfa
CD
677 '("pdflatex -interaction nonstopmode -output-directory %o %f"
678 "pdflatex -interaction nonstopmode -output-directory %o %f"
679 "pdflatex -interaction nonstopmode -output-directory %o %f")
8223b1d2
BG
680 "Commands to process a LaTeX file to a PDF file and process latex
681fragments to pdf files.By default,this is a list of strings,and each of
682strings will be given to the shell as a command. %f in the command will
683be replaced by the full file name, %b by the file base name (i.e. without
684extension) and %o by the base directory of the file.
685
686If you set `org-create-formula-image-program'
687`org-export-with-LaTeX-fragments' to 'imagemagick, you can add a
688sublist which contains your own command(s) for LaTeX fragments
689previewing, like this:
690
691 '(\"xelatex -interaction nonstopmode -output-directory %o %f\"
692 \"xelatex -interaction nonstopmode -output-directory %o %f\"
693 ;; use below command(s) to convert latex fragments
694 (\"xelatex %f\"))
695
696With no such sublist, the default command used to convert LaTeX
697fragments will be the first string in the list.
afe98dfa 698
c8d0cf5c 699The reason why this is a list is that it usually takes several runs of
afe98dfa 700`pdflatex', maybe mixed with a call to `bibtex'. Org does not have a clever
8bfe682a 701mechanism to detect which of these commands have to be run to get to a stable
c8d0cf5c
CD
702result, and it also does not do any error checking.
703
afe98dfa
CD
704By default, Org uses 3 runs of `pdflatex' to do the processing. If you
705have texi2dvi on your system and if that does not cause the infamous
706egrep/locale bug:
707
708 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
709
710then `texi2dvi' is the superior choice. Org does offer it as one
711of the customize options.
712
c8d0cf5c
CD
713Alternatively, this may be a Lisp function that does the processing, so you
714could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
8bfe682a 715This function should accept the file name as its single argument."
ed21c5c8 716 :group 'org-export-pdf
afe98dfa
CD
717 :type '(choice
718 (repeat :tag "Shell command sequence"
c8d0cf5c 719 (string :tag "Shell command"))
afe98dfa
CD
720 (const :tag "2 runs of pdflatex"
721 ("pdflatex -interaction nonstopmode -output-directory %o %f"
8223b1d2 722 "pdflatex -interaction nonstopmode -output-directory %o %f"))
afe98dfa
CD
723 (const :tag "3 runs of pdflatex"
724 ("pdflatex -interaction nonstopmode -output-directory %o %f"
8223b1d2
BG
725 "pdflatex -interaction nonstopmode -output-directory %o %f"
726 "pdflatex -interaction nonstopmode -output-directory %o %f"))
afe98dfa
CD
727 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
728 ("pdflatex -interaction nonstopmode -output-directory %o %f"
8223b1d2
BG
729 "bibtex %b"
730 "pdflatex -interaction nonstopmode -output-directory %o %f"
731 "pdflatex -interaction nonstopmode -output-directory %o %f"))
153ae947
BG
732 (const :tag "2 runs of xelatex"
733 ("xelatex -interaction nonstopmode -output-directory %o %f"
8223b1d2 734 "xelatex -interaction nonstopmode -output-directory %o %f"))
153ae947
BG
735 (const :tag "3 runs of xelatex"
736 ("xelatex -interaction nonstopmode -output-directory %o %f"
8223b1d2
BG
737 "xelatex -interaction nonstopmode -output-directory %o %f"
738 "xelatex -interaction nonstopmode -output-directory %o %f"))
153ae947
BG
739 (const :tag "xelatex,bibtex,xelatex,xelatex"
740 ("xelatex -interaction nonstopmode -output-directory %o %f"
8223b1d2
BG
741 "bibtex %b"
742 "xelatex -interaction nonstopmode -output-directory %o %f"
743 "xelatex -interaction nonstopmode -output-directory %o %f"))
afe98dfa
CD
744 (const :tag "texi2dvi"
745 ("texi2dvi -p -b -c -V %f"))
746 (const :tag "rubber"
747 ("rubber -d --into %o %f"))
748 (function)))
c8d0cf5c 749
ed21c5c8
CD
750(defcustom org-export-pdf-logfiles
751 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
752 "The list of file extensions to consider as LaTeX logfiles."
753 :group 'org-export-pdf
153ae947 754 :version "24.1"
ed21c5c8
CD
755 :type '(repeat (string :tag "Extension")))
756
71d35b24 757(defcustom org-export-pdf-remove-logfiles t
ed21c5c8 758 "Non-nil means remove the logfiles produced by PDF production.
71d35b24 759These are the .aux, .log, .out, and .toc files."
c8d0cf5c 760 :group 'org-export-pdf
71d35b24
CD
761 :type 'boolean)
762
c8d0cf5c
CD
763;;; Hooks
764
ed21c5c8
CD
765(defvar org-export-latex-after-initial-vars-hook nil
766 "Hook run before LaTeX export.
767The exact moment is after the initial variables like org-export-latex-class
768have been determined from the environment.")
769
c8d0cf5c
CD
770(defvar org-export-latex-after-blockquotes-hook nil
771 "Hook run during LaTeX export, after blockquote, verse, center are done.")
772
8d642074
CD
773(defvar org-export-latex-final-hook nil
774 "Hook run in the finalized LaTeX buffer.")
775
ed21c5c8
CD
776(defvar org-export-latex-after-save-hook nil
777 "Hook run in the finalized LaTeX buffer, after it has been saved.")
778
0b8568f5 779;;; Autoload functions:
db9c3fb1 780
72a81656
CD
781;;;###autoload
782(defun org-export-as-latex-batch ()
621f83e4
CD
783 "Call `org-export-as-latex', may be used in batch processing.
784For example:
785
33306645
CD
786emacs --batch
787 --load=$HOME/lib/emacs/org.el
788 --eval \"(setq org-export-headline-levels 2)\"
789 --visit=MyFile --funcall org-export-as-latex-batch"
bdebdb64 790 (org-export-as-latex org-export-headline-levels))
72a81656
CD
791
792;;;###autoload
793(defun org-export-as-latex-to-buffer (arg)
33306645 794 "Call `org-export-as-latex` with output to a temporary buffer.
72a81656
CD
795No file is created. The prefix ARG is passed through to `org-export-as-latex'."
796 (interactive "P")
bdebdb64 797 (org-export-as-latex arg nil "*Org LaTeX Export*")
c8d0cf5c
CD
798 (when org-export-show-temporary-export-buffer
799 (switch-to-buffer-other-window "*Org LaTeX Export*")))
72a81656
CD
800
801;;;###autoload
802(defun org-replace-region-by-latex (beg end)
803 "Replace the region from BEG to END with its LaTeX export.
804It assumes the region has `org-mode' syntax, and then convert it to
db9c3fb1 805LaTeX. This can be used in any buffer. For example, you could
72a81656
CD
806write an itemized list in `org-mode' syntax in an LaTeX buffer and
807then use this command to convert it."
808 (interactive "r")
809 (let (reg latex buf)
810 (save-window-excursion
8223b1d2 811 (if (derived-mode-p 'org-mode)
72a81656
CD
812 (setq latex (org-export-region-as-latex
813 beg end t 'string))
814 (setq reg (buffer-substring beg end)
815 buf (get-buffer-create "*Org tmp*"))
81ad75af 816 (with-current-buffer buf
72a81656
CD
817 (erase-buffer)
818 (insert reg)
819 (org-mode)
820 (setq latex (org-export-region-as-latex
821 (point-min) (point-max) t 'string)))
822 (kill-buffer buf)))
823 (delete-region beg end)
824 (insert latex)))
825
826;;;###autoload
827(defun org-export-region-as-latex (beg end &optional body-only buffer)
828 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
829If prefix arg BODY-ONLY is set, omit file header, footer, and table of
830contents, and only produce the region of converted text, useful for
831cut-and-paste operations.
832If BUFFER is a buffer or a string, use/create that buffer as a target
833of the converted LaTeX. If BUFFER is the symbol `string', return the
c8d0cf5c 834produced LaTeX as a string and leave no buffer behind. For example,
72a81656
CD
835a Lisp program could call this function in the following way:
836
837 (setq latex (org-export-region-as-latex beg end t 'string))
838
839When called interactively, the output buffer is selected, and shown
c8d0cf5c 840in a window. A non-interactive call will only return the buffer."
72a81656 841 (interactive "r\nP")
3ab2c837 842 (when (org-called-interactively-p 'any)
72a81656
CD
843 (setq buffer "*Org LaTeX Export*"))
844 (let ((transient-mark-mode t) (zmacs-regions t)
c8d0cf5c 845 ext-plist rtn)
8bfe682a 846 (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
72a81656
CD
847 (goto-char end)
848 (set-mark (point)) ;; to activate the region
849 (goto-char beg)
850 (setq rtn (org-export-as-latex
bdebdb64 851 nil ext-plist
72a81656
CD
852 buffer body-only))
853 (if (fboundp 'deactivate-mark) (deactivate-mark))
3ab2c837 854 (if (and (org-called-interactively-p 'any) (bufferp rtn))
72a81656
CD
855 (switch-to-buffer-other-window rtn)
856 rtn)))
857
858;;;###autoload
bdebdb64 859(defun org-export-as-latex (arg &optional ext-plist to-buffer body-only pub-dir)
db9c3fb1
CD
860 "Export current buffer to a LaTeX file.
861If there is an active region, export only the region. The prefix
862ARG specifies how many levels of the outline should become
863headlines. The default is 3. Lower levels will be exported
864depending on `org-export-latex-low-levels'. The default is to
c8d0cf5c 865convert them as description lists.
bdebdb64
BG
866EXT-PLIST is a property list with external parameters overriding
867org-mode's default settings, but still inferior to file-local settings.
868When TO-BUFFER is non-nil, create a buffer with that name and export
869to that buffer. If TO-BUFFER is the symbol `string', don't leave any
870buffer behind and just return the resulting LaTeX as a string, with
871no LaTeX header.
db9c3fb1 872When BODY-ONLY is set, don't produce the file header and footer,
86fbb8ca
CD
873simply return the content of \\begin{document}...\\end{document},
874without even the \\begin{document} and \\end{document} commands.
c7cf0ebc 875When PUB-DIR is set, use this as the publishing directory."
72a81656 876 (interactive "P")
86fbb8ca 877 (when (and (not body-only) arg (listp arg)) (setq body-only t))
ed21c5c8
CD
878 (run-hooks 'org-export-first-hook)
879
72a81656
CD
880 ;; Make sure we have a file name when we need it.
881 (when (and (not (or to-buffer body-only))
882 (not buffer-file-name))
883 (if (buffer-base-buffer)
884 (org-set-local 'buffer-file-name
885 (with-current-buffer (buffer-base-buffer)
886 buffer-file-name))
887 (error "Need a file name to be able to export")))
888
889 (message "Exporting to LaTeX...")
c8d0cf5c 890 (org-unmodified
ed21c5c8
CD
891 (let ((inhibit-read-only t))
892 (remove-text-properties (point-min) (point-max)
893 '(:org-license-to-kill nil))))
72a81656 894 (org-update-radio-target-regexp)
db9c3fb1 895 (org-export-latex-set-initial-vars ext-plist arg)
3ab2c837
BG
896 (setq org-export-opt-plist org-export-latex-options-plist
897 org-export-footnotes-data (org-footnote-all-labels 'with-defs)
898 org-export-footnotes-seen nil
899 org-export-latex-footmark-seen nil)
ed21c5c8
CD
900 (org-install-letbind)
901 (run-hooks 'org-export-latex-after-initial-vars-hook)
15841868 902 (let* ((wcf (current-window-configuration))
3ab2c837
BG
903 (opt-plist
904 (org-export-process-option-filters org-export-latex-options-plist))
db9c3fb1 905 (region-p (org-region-active-p))
b349f79f
CD
906 (rbeg (and region-p (region-beginning)))
907 (rend (and region-p (region-end)))
db9c3fb1 908 (subtree-p
8bfe682a 909 (if (plist-get opt-plist :ignore-subtree-p)
c8d0cf5c
CD
910 nil
911 (when region-p
912 (save-excursion
913 (goto-char rbeg)
914 (and (org-at-heading-p)
915 (>= (org-end-of-subtree t t) rend))))))
916 (opt-plist (setq org-export-opt-plist
917 (if subtree-p
918 (org-export-add-subtree-options opt-plist rbeg)
919 opt-plist)))
b349f79f 920 ;; Make sure the variable contains the updated values.
ed21c5c8
CD
921 (org-export-latex-options-plist (setq org-export-opt-plist opt-plist))
922 ;; The following two are dynamically scoped into other
923 ;; routines below.
924 (org-current-export-dir
925 (or pub-dir (org-export-directory :html opt-plist)))
926 (org-current-export-file buffer-file-name)
db9c3fb1
CD
927 (title (or (and subtree-p (org-export-get-title-from-subtree))
928 (plist-get opt-plist :title)
929 (and (not
930 (plist-get opt-plist :skip-before-1st-heading))
931 (org-export-grab-title-from-buffer))
ed21c5c8
CD
932 (and buffer-file-name
933 (file-name-sans-extension
934 (file-name-nondirectory buffer-file-name)))
935 "No Title"))
936 (filename
937 (and (not to-buffer)
938 (concat
939 (file-name-as-directory
940 (or pub-dir
153ae947 941 (org-export-directory :LaTeX org-export-latex-options-plist)))
ed21c5c8
CD
942 (file-name-sans-extension
943 (or (and subtree-p
944 (org-entry-get rbeg "EXPORT_FILE_NAME" t))
945 (file-name-nondirectory ;sans-extension
946 (or buffer-file-name
86fbb8ca 947 (error "Don't know which export file to use")))))
ed21c5c8
CD
948 ".tex")))
949 (filename
950 (and filename
951 (if (equal (file-truename filename)
952 (file-truename (or buffer-file-name "dummy.org")))
953 (concat filename ".tex")
954 filename)))
e66ba1df 955 (auto-insert nil); Avoid any auto-insert stuff for the new file
153ae947 956 (TeX-master (boundp 'TeX-master))
72a81656 957 (buffer (if to-buffer
bdebdb64
BG
958 (if (eq to-buffer 'string)
959 (get-buffer-create "*Org LaTeX Export*")
960 (get-buffer-create to-buffer))
72a81656 961 (find-file-noselect filename)))
72a81656 962 (odd org-odd-levels-only)
db9c3fb1 963 (header (org-export-latex-make-header title opt-plist))
4b67ea89 964 (skip (cond (subtree-p nil)
db55f368 965 (region-p nil)
4b67ea89 966 (t (plist-get opt-plist :skip-before-1st-heading))))
72a81656 967 (text (plist-get opt-plist :text))
c8d0cf5c
CD
968 (org-export-preprocess-hook
969 (cons
970 `(lambda () (org-set-local 'org-complex-heading-regexp
971 ,org-export-latex-complex-heading-re))
972 org-export-preprocess-hook))
65c439fd 973 (first-lines (if skip "" (org-export-latex-first-lines
c8d0cf5c
CD
974 opt-plist
975 (if subtree-p
976 (save-excursion
977 (goto-char rbeg)
978 (point-at-bol 2))
979 rbeg)
980 (if region-p rend))))
72a81656
CD
981 (coding-system (and (boundp 'buffer-file-coding-system)
982 buffer-file-coding-system))
983 (coding-system-for-write (or org-export-latex-coding-system
984 coding-system))
985 (save-buffer-coding-system (or org-export-latex-coding-system
986 coding-system))
33306645 987 (region (buffer-substring
72a81656
CD
988 (if region-p (region-beginning) (point-min))
989 (if region-p (region-end) (point-max))))
ed21c5c8
CD
990 (text
991 (and text (string-match "\\S-" text)
992 (org-export-preprocess-string
993 text
994 :emph-multiline t
3ab2c837 995 :for-backend 'latex
ed21c5c8
CD
996 :comments nil
997 :tags (plist-get opt-plist :tags)
998 :priority (plist-get opt-plist :priority)
999 :footnotes (plist-get opt-plist :footnotes)
1000 :drawers (plist-get opt-plist :drawers)
1001 :timestamps (plist-get opt-plist :timestamps)
1002 :todo-keywords (plist-get opt-plist :todo-keywords)
3ab2c837 1003 :tasks (plist-get opt-plist :tasks)
ed21c5c8
CD
1004 :add-text nil
1005 :skip-before-1st-heading skip
1006 :select-tags nil
1007 :exclude-tags nil
1008 :LaTeX-fragments nil)))
72a81656 1009 (string-for-export
20908596 1010 (org-export-preprocess-string
db55f368
CD
1011 region
1012 :emph-multiline t
3ab2c837 1013 :for-backend 'latex
db55f368
CD
1014 :comments nil
1015 :tags (plist-get opt-plist :tags)
1016 :priority (plist-get opt-plist :priority)
0bd48b37 1017 :footnotes (plist-get opt-plist :footnotes)
1bcdebed 1018 :drawers (plist-get opt-plist :drawers)
0bd48b37 1019 :timestamps (plist-get opt-plist :timestamps)
db55f368 1020 :todo-keywords (plist-get opt-plist :todo-keywords)
3ab2c837 1021 :tasks (plist-get opt-plist :tasks)
db55f368
CD
1022 :add-text (if (eq to-buffer 'string) nil text)
1023 :skip-before-1st-heading skip
1024 :select-tags (plist-get opt-plist :select-tags)
1025 :exclude-tags (plist-get opt-plist :exclude-tags)
1026 :LaTeX-fragments nil)))
15841868 1027
db9c3fb1 1028 (set-buffer buffer)
72a81656 1029 (erase-buffer)
c8d0cf5c 1030 (org-install-letbind)
0b8568f5 1031
15841868
JW
1032 (and (fboundp 'set-buffer-file-coding-system)
1033 (set-buffer-file-coding-system coding-system-for-write))
1034
db9c3fb1 1035 ;; insert the header and initial document commands
15841868 1036 (unless (or (eq to-buffer 'string) body-only)
db9c3fb1 1037 (insert header))
15841868
JW
1038
1039 ;; insert text found in #+TEXT
1040 (when (and text (not (eq to-buffer 'string)))
db9c3fb1
CD
1041 (insert (org-export-latex-content
1042 text '(lists tables fixed-width keywords))
8223b1d2 1043 "\n\n"))
15841868
JW
1044
1045 ;; insert lines before the first headline
3ab2c837 1046 (unless (or skip (string-match "^\\*" first-lines))
15841868 1047 (insert first-lines))
72a81656 1048
15841868 1049 ;; export the content of headlines
72a81656
CD
1050 (org-export-latex-global
1051 (with-temp-buffer
1052 (insert string-for-export)
1053 (goto-char (point-min))
15841868
JW
1054 (when (re-search-forward "^\\(\\*+\\) " nil t)
1055 (let* ((asters (length (match-string 1)))
1056 (level (if odd (- asters 2) (- asters 1))))
db9c3fb1 1057 (setq org-export-latex-add-level
15841868
JW
1058 (if odd (1- (/ (1+ asters) 2)) (1- asters)))
1059 (org-export-latex-parse-global level odd)))))
1060
1061 ;; finalization
72a81656 1062 (unless body-only (insert "\n\\end{document}"))
c8d0cf5c 1063
ed21c5c8
CD
1064 ;; Attach description terms to the \item macro
1065 (goto-char (point-min))
1066 (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t)
1067 (delete-region (match-beginning 1) (match-end 1)))
1068
c8d0cf5c
CD
1069 ;; Relocate the table of contents
1070 (goto-char (point-min))
1071 (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
1072 (goto-char (point-min))
1073 (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t)
1074 (replace-match ""))
1075 (goto-char (point-min))
1076 (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
1077 (replace-match "\\tableofcontents" t t)))
1078
ed21c5c8
CD
1079 ;; Cleanup forced line ends in items where they are not needed
1080 (goto-char (point-min))
1081 (while (re-search-forward
1082 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin"
1083 nil t)
1084 (delete-region (match-beginning 1) (match-end 1)))
1085 (goto-char (point-min))
1086 (while (re-search-forward
1087 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*"
1088 nil t)
1089 (if (looking-at "[\n \t]+")
1090 (replace-match "\n")))
1091
8223b1d2
BG
1092 ;; Ensure we have a final newline
1093 (goto-char (point-max))
1094 (or (eq (char-before) ?\n)
1095 (insert ?\n))
1096
8d642074 1097 (run-hooks 'org-export-latex-final-hook)
86fbb8ca
CD
1098 (if to-buffer
1099 (unless (eq major-mode 'latex-mode) (latex-mode))
1100 (save-buffer))
ed21c5c8
CD
1101 (org-export-latex-fix-inputenc)
1102 (run-hooks 'org-export-latex-after-save-hook)
72a81656 1103 (goto-char (point-min))
c8d0cf5c
CD
1104 (or (org-export-push-to-kill-ring "LaTeX")
1105 (message "Exporting to LaTeX...done"))
15841868
JW
1106 (prog1
1107 (if (eq to-buffer 'string)
1108 (prog1 (buffer-substring (point-min) (point-max))
1109 (kill-buffer (current-buffer)))
1110 (current-buffer))
1111 (set-window-configuration wcf))))
1112
71d35b24
CD
1113;;;###autoload
1114(defun org-export-as-pdf (arg &optional hidden ext-plist
1115 to-buffer body-only pub-dir)
1116 "Export as LaTeX, then process through to PDF."
1117 (interactive "P")
1118 (message "Exporting to PDF...")
1119 (let* ((wconfig (current-window-configuration))
bdebdb64 1120 (lbuf (org-export-as-latex arg ext-plist to-buffer body-only pub-dir))
71d35b24
CD
1121 (file (buffer-file-name lbuf))
1122 (base (file-name-sans-extension (buffer-file-name lbuf)))
c8d0cf5c 1123 (pdffile (concat base ".pdf"))
3ab2c837
BG
1124 (cmds (if (eq org-export-latex-listings 'minted)
1125 ;; automatically add -shell-escape when needed
1126 (mapcar (lambda (cmd)
1127 (replace-regexp-in-string
1128 "pdflatex " "pdflatex -shell-escape " cmd))
1129 org-latex-to-pdf-process)
1130 org-latex-to-pdf-process))
c8d0cf5c
CD
1131 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
1132 (bibtex-p (with-current-buffer lbuf
1133 (save-excursion
1134 (goto-char (point-min))
1135 (re-search-forward "\\\\bibliography{" nil t))))
afe98dfa 1136 cmd output-dir errors)
c8d0cf5c 1137 (with-current-buffer outbuf (erase-buffer))
afe98dfa
CD
1138 (message (concat "Processing LaTeX file " file "..."))
1139 (setq output-dir (file-name-directory file))
3ab2c837
BG
1140 (with-current-buffer lbuf
1141 (save-excursion
1142 (if (and cmds (symbolp cmds))
1143 (funcall cmds (shell-quote-argument file))
1144 (while cmds
1145 (setq cmd (pop cmds))
8223b1d2
BG
1146 (cond
1147 ((not (listp cmd))
1148 (while (string-match "%b" cmd)
1149 (setq cmd (replace-match
1150 (save-match-data
1151 (shell-quote-argument base))
1152 t t cmd)))
1153 (while (string-match "%f" cmd)
1154 (setq cmd (replace-match
1155 (save-match-data
1156 (shell-quote-argument file))
1157 t t cmd)))
1158 (while (string-match "%o" cmd)
1159 (setq cmd (replace-match
1160 (save-match-data
1161 (shell-quote-argument output-dir))
1162 t t cmd)))
1163 (shell-command cmd outbuf)))))))
afe98dfa
CD
1164 (message (concat "Processing LaTeX file " file "...done"))
1165 (setq errors (org-export-latex-get-error outbuf))
71d35b24 1166 (if (not (file-exists-p pdffile))
afe98dfa
CD
1167 (error (concat "PDF file " pdffile " was not produced"
1168 (if errors (concat ":" errors "") "")))
71d35b24
CD
1169 (set-window-configuration wconfig)
1170 (when org-export-pdf-remove-logfiles
ed21c5c8 1171 (dolist (ext org-export-pdf-logfiles)
71d35b24
CD
1172 (setq file (concat base "." ext))
1173 (and (file-exists-p file) (delete-file file))))
afe98dfa
CD
1174 (message (concat
1175 "Exporting to PDF...done"
1176 (if errors
1177 (concat ", with some errors:" errors)
1178 "")))
71d35b24
CD
1179 pdffile)))
1180
afe98dfa
CD
1181(defun org-export-latex-get-error (buf)
1182 "Collect the kinds of errors that remain in pdflatex processing."
1183 (with-current-buffer buf
1184 (save-excursion
1185 (goto-char (point-max))
1186 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
1187 ;; OK, we are at the location of the final run
1188 (let ((pos (point)) (errors "") (case-fold-search t))
1189 (if (re-search-forward "Reference.*?undefined" nil t)
1190 (setq errors (concat errors " [undefined reference]")))
1191 (goto-char pos)
1192 (if (re-search-forward "Citation.*?undefined" nil t)
1193 (setq errors (concat errors " [undefined citation]")))
1194 (goto-char pos)
1195 (if (re-search-forward "Undefined control sequence" nil t)
1196 (setq errors (concat errors " [undefined control sequence]")))
1197 (and (org-string-nw-p errors) errors))))))
1198
71d35b24
CD
1199;;;###autoload
1200(defun org-export-as-pdf-and-open (arg)
1201 "Export as LaTeX, then process through to PDF, and open."
1202 (interactive "P")
1203 (let ((pdffile (org-export-as-pdf arg)))
1204 (if pdffile
ed21c5c8
CD
1205 (progn
1206 (org-open-file pdffile)
1207 (when org-export-kill-product-buffer-when-displayed
1208 (kill-buffer (find-buffer-visiting
1209 (concat (file-name-sans-extension (buffer-file-name))
1210 ".tex")))))
71d35b24
CD
1211 (error "PDF file was not produced"))))
1212
0b8568f5 1213;;; Parsing functions:
db9c3fb1 1214
72a81656
CD
1215(defun org-export-latex-parse-global (level odd)
1216 "Parse the current buffer recursively, starting at LEVEL.
1217If ODD is non-nil, assume the buffer only contains odd sections.
db9c3fb1 1218Return a list reflecting the document structure."
72a81656
CD
1219 (save-excursion
1220 (goto-char (point-min))
1221 (let* ((cnt 0) output
db9c3fb1 1222 (depth org-export-latex-sectioning-depth))
ed21c5c8 1223 (while (org-re-search-forward-unprotected
72a81656
CD
1224 (concat "^\\(\\(?:\\*\\)\\{"
1225 (number-to-string (+ (if odd 2 1) level))
1226 "\\}\\) \\(.*\\)$")
1227 ;; make sure that there is no upper heading
1228 (when (> level 0)
1229 (save-excursion
1230 (save-match-data
ed21c5c8 1231 (org-re-search-forward-unprotected
72a81656
CD
1232 (concat "^\\(\\(?:\\*\\)\\{"
1233 (number-to-string level)
1234 "\\}\\) \\(.*\\)$") nil t)))) t)
1235 (setq cnt (1+ cnt))
1236 (let* ((pos (match-beginning 0))
1237 (heading (match-string 2))
1238 (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
1239 (save-excursion
1240 (narrow-to-region
1241 (point)
1242 (save-match-data
ed21c5c8 1243 (if (org-re-search-forward-unprotected
72a81656
CD
1244 (concat "^\\(\\(?:\\*\\)\\{"
1245 (number-to-string (+ (if odd 2 1) level))
1246 "\\}\\) \\(.*\\)$") nil t)
1247 (match-beginning 0)
1248 (point-max))))
1249 (goto-char (point-min))
1250 (setq output
1251 (append output
1252 (list
1253 (list
1254 `(pos . ,pos)
1255 `(level . ,nlevel)
1256 `(occur . ,cnt)
1257 `(heading . ,heading)
1258 `(content . ,(org-export-latex-parse-content))
db9c3fb1 1259 `(subcontent . ,(org-export-latex-parse-subcontent
72a81656
CD
1260 level odd)))))))
1261 (widen)))
1262 (list output))))
1263
1264(defun org-export-latex-parse-content ()
1265 "Extract the content of a section."
1266 (let ((beg (point))
ed21c5c8 1267 (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t)
72a81656
CD
1268 (progn (beginning-of-line) (point))
1269 (point-max))))
1270 (buffer-substring beg end)))
1271
1272(defun org-export-latex-parse-subcontent (level odd)
1273 "Extract the subcontent of a section at LEVEL.
1274If ODD Is non-nil, assume subcontent only contains odd sections."
ed21c5c8 1275 (if (not (org-re-search-forward-unprotected
72a81656
CD
1276 (concat "^\\(\\(?:\\*\\)\\{"
1277 (number-to-string (+ (if odd 4 2) level))
1278 "\\}\\) \\(.*\\)$")
1279 nil t))
1280 nil ; subcontent is nil
1281 (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
1282
0b8568f5 1283;;; Rendering functions:
72a81656
CD
1284(defun org-export-latex-global (content)
1285 "Export CONTENT to LaTeX.
1286CONTENT is an element of the list produced by
1287`org-export-latex-parse-global'."
1288 (if (eq (car content) 'subcontent)
1289 (mapc 'org-export-latex-sub (cdr content))
1290 (org-export-latex-sub (car content))))
1291
1292(defun org-export-latex-sub (subcontent)
1293 "Export the list SUBCONTENT to LaTeX.
1294SUBCONTENT is an alist containing information about the headline
1295and its content."
db9c3fb1 1296 (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
d5098885 1297 (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
72a81656 1298
d5098885 1299(defun org-export-latex-subcontent (subcontent num)
621f83e4 1300 "Export each cell of SUBCONTENT to LaTeX.
3ab2c837
BG
1301If NUM is non-nil export numbered sections, otherwise use unnumbered
1302sections. If NUM is an integer, export the highest NUM levels as
1303numbered sections and lower levels as unnumbered sections."
ed21c5c8 1304 (let* ((heading (cdr (assoc 'heading subcontent)))
621f83e4
CD
1305 (level (- (cdr (assoc 'level subcontent))
1306 org-export-latex-add-level))
1307 (occur (number-to-string (cdr (assoc 'occur subcontent))))
1308 (content (cdr (assoc 'content subcontent)))
1309 (subcontent (cadr (assoc 'subcontent subcontent)))
ce4fdcb9
CD
1310 (label (org-get-text-property-any 0 'target heading))
1311 (label-list (cons label (cdr (assoc label
ed21c5c8
CD
1312 org-export-target-aliases))))
1313 (sectioning org-export-latex-sectioning)
1314 (depth org-export-latex-sectioning-depth)
153ae947 1315 main-heading sub-heading ctnt)
ed21c5c8
CD
1316 (when (symbolp (car sectioning))
1317 (setq sectioning (funcall (car sectioning) level heading))
1318 (when sectioning
1319 (setq heading (car sectioning)
1320 sectioning (cdr sectioning)
1321 ;; target property migh have changed...
1322 label (org-get-text-property-any 0 'target heading)
1323 label-list (cons label (cdr (assoc label
1324 org-export-target-aliases)))))
1325 (if sectioning (setq sectioning (make-list 10 sectioning)))
1326 (setq depth (if sectioning 10000 0)))
1327 (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading)
1328 (setq main-heading (substring heading 0 (match-beginning 0))
1329 sub-heading (substring heading (match-end 0))))
1330 (setq heading (org-export-latex-fontify-headline heading)
1331 sub-heading (and sub-heading
1332 (org-export-latex-fontify-headline sub-heading))
1333 main-heading (and main-heading
1334 (org-export-latex-fontify-headline main-heading)))
db9c3fb1 1335 (cond
72a81656 1336 ;; Normal conversion
ed21c5c8
CD
1337 ((<= level depth)
1338 (let* ((sec (nth (1- level) sectioning))
3ab2c837
BG
1339 (num (if (integerp num)
1340 (>= num level)
1341 num))
71d35b24
CD
1342 start end)
1343 (if (consp (cdr sec))
1344 (setq start (nth (if num 0 2) sec)
1345 end (nth (if num 1 3) sec))
1346 (setq start (if num (car sec) (cdr sec))))
ed21c5c8
CD
1347 (insert (format start (if main-heading main-heading heading)
1348 (or sub-heading "")))
1349 (insert "\n")
ce4fdcb9
CD
1350 (when label
1351 (insert (mapconcat (lambda (l) (format "\\label{%s}" l))
1352 label-list "\n") "\n"))
71d35b24
CD
1353 (insert (org-export-latex-content content))
1354 (cond ((stringp subcontent) (insert subcontent))
ed21c5c8
CD
1355 ((listp subcontent)
1356 (while (org-looking-back "\n\n") (backward-delete-char 1))
1357 (org-export-latex-sub subcontent)))
1358 (when (and end (string-match "[^ \t]" end))
1359 (let ((hook (org-get-text-property-any 0 'org-insert-hook end)))
1360 (and (functionp hook) (funcall hook)))
1361 (insert end "\n"))))
72a81656 1362 ;; At a level under the hl option: we can drop this subsection
ed21c5c8 1363 ((> level depth)
72a81656 1364 (cond ((eq org-export-latex-low-levels 'description)
c8d0cf5c
CD
1365 (if (string-match "% ends low level$"
1366 (buffer-substring (point-at-bol 0) (point)))
1367 (delete-region (point-at-bol 0) (point))
1368 (insert "\\begin{description}\n"))
ed21c5c8 1369 (insert (format "\n\\item[%s]%s~\n"
621f83e4
CD
1370 heading
1371 (if label (format "\\label{%s}" label) "")))
72a81656
CD
1372 (insert (org-export-latex-content content))
1373 (cond ((stringp subcontent) (insert subcontent))
1374 ((listp subcontent) (org-export-latex-sub subcontent)))
c8d0cf5c
CD
1375 (insert "\\end{description} % ends low level\n"))
1376 ((memq org-export-latex-low-levels '(itemize enumerate))
1377 (if (string-match "% ends low level$"
1378 (buffer-substring (point-at-bol 0) (point)))
1379 (delete-region (point-at-bol 0) (point))
1380 (insert (format "\\begin{%s}\n"
1381 (symbol-name org-export-latex-low-levels))))
153ae947
BG
1382 (let ((ctnt (org-export-latex-content content)))
1383 (insert (format (if (not (equal (replace-regexp-in-string "\n" "" ctnt) ""))
1384 "\n\\item %s\\\\\n%s%%"
1385 "\n\\item %s\n%s%%")
1386 heading
1387 (if label (format "\\label{%s}" label) "")))
1388 (insert ctnt))
c8d0cf5c
CD
1389 (cond ((stringp subcontent) (insert subcontent))
1390 ((listp subcontent) (org-export-latex-sub subcontent)))
1391 (insert (format "\\end{%s} %% ends low level\n"
1392 (symbol-name org-export-latex-low-levels))))
1393
153ae947
BG
1394 ((and (listp org-export-latex-low-levels)
1395 org-export-latex-low-levels)
c8d0cf5c
CD
1396 (if (string-match "% ends low level$"
1397 (buffer-substring (point-at-bol 0) (point)))
1398 (delete-region (point-at-bol 0) (point))
1399 (insert (car org-export-latex-low-levels) "\n"))
1400 (insert (format (nth 2 org-export-latex-low-levels)
1401 heading
1402 (if label (format "\\label{%s}" label) "")))
1403 (insert (org-export-latex-content content))
1404 (cond ((stringp subcontent) (insert subcontent))
1405 ((listp subcontent) (org-export-latex-sub subcontent)))
1406 (insert (nth 1 org-export-latex-low-levels)
1407 " %% ends low level\n"))
1408
72a81656
CD
1409 ((stringp org-export-latex-low-levels)
1410 (insert (format org-export-latex-low-levels heading) "\n")
621f83e4 1411 (when label (insert (format "\\label{%s}\n" label)))
72a81656
CD
1412 (insert (org-export-latex-content content))
1413 (cond ((stringp subcontent) (insert subcontent))
1414 ((listp subcontent) (org-export-latex-sub subcontent)))))))))
1415
0b8568f5 1416;;; Exporting internals:
db9c3fb1 1417(defun org-export-latex-set-initial-vars (ext-plist level)
0b8568f5 1418 "Store org local variables required for LaTeX export.
db9c3fb1
CD
1419EXT-PLIST is an optional additional plist.
1420LEVEL indicates the default depth for export."
1421 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
c8d0cf5c
CD
1422 org-export-latex-done-keywords org-done-keywords
1423 org-export-latex-not-done-keywords org-not-done-keywords
1424 org-export-latex-complex-heading-re org-complex-heading-regexp
1425 org-export-latex-display-custom-times org-display-custom-times
db9c3fb1 1426 org-export-latex-all-targets-re
0b8568f5 1427 (org-make-target-link-regexp (org-all-targets))
db9c3fb1 1428 org-export-latex-options-plist
0b8568f5
JW
1429 (org-combine-plists (org-default-export-plist) ext-plist
1430 (org-infile-export-plist))
db9c3fb1 1431 org-export-latex-class
0bd48b37
CD
1432 (or (and (org-region-active-p)
1433 (save-excursion
1434 (goto-char (region-beginning))
1435 (and (looking-at org-complex-heading-regexp)
1436 (org-entry-get nil "LaTeX_CLASS" 'selective))))
1437 (save-excursion
1438 (save-restriction
1439 (widen)
1440 (goto-char (point-min))
e66ba1df 1441 (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([-/a-zA-Z]+\\)" nil t)
0bd48b37 1442 (match-string 1))))
8d642074 1443 (plist-get org-export-latex-options-plist :latex-class)
0bd48b37 1444 org-export-latex-default-class)
ed21c5c8
CD
1445 org-export-latex-class-options
1446 (or (and (org-region-active-p)
1447 (save-excursion
1448 (goto-char (region-beginning))
1449 (and (looking-at org-complex-heading-regexp)
1450 (org-entry-get nil "LaTeX_CLASS_OPTIONS" 'selective))))
1451 (save-excursion
1452 (save-restriction
1453 (widen)
1454 (goto-char (point-min))
1455 (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t)
1456 (match-string 1))))
1457 (plist-get org-export-latex-options-plist :latex-class-options))
0bd48b37
CD
1458 org-export-latex-class
1459 (or (car (assoc org-export-latex-class org-export-latex-classes))
1460 (error "No definition for class `%s' in `org-export-latex-classes'"
1461 org-export-latex-class))
db9c3fb1
CD
1462 org-export-latex-header
1463 (cadr (assoc org-export-latex-class org-export-latex-classes))
1464 org-export-latex-sectioning
1465 (cddr (assoc org-export-latex-class org-export-latex-classes))
1466 org-export-latex-sectioning-depth
1467 (or level
1468 (let ((hl-levels
1469 (plist-get org-export-latex-options-plist :headline-levels))
1470 (sec-depth (length org-export-latex-sectioning)))
ed21c5c8
CD
1471 (if (> hl-levels sec-depth) sec-depth hl-levels))))
1472 (when (and org-export-latex-class-options
1473 (string-match "\\S-" org-export-latex-class-options)
1474 (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?"
1475 org-export-latex-header))
1476 (setq org-export-latex-header
1477 (concat (substring org-export-latex-header 0 (match-end 1))
1478 org-export-latex-class-options
1479 (substring org-export-latex-header (match-end 0))))))
1480
1481(defvar org-export-latex-format-toc-function
1482 'org-export-latex-format-toc-default
86fbb8ca 1483 "The function formatting returning the string to create the table of contents.
ed21c5c8 1484The function mus take one parameter, the depth of the table of contents.")
db9c3fb1
CD
1485
1486(defun org-export-latex-make-header (title opt-plist)
1487 "Make the LaTeX header and return it as a string.
1488TITLE is the current title from the buffer or region.
1489OPT-PLIST is the options plist for current buffer."
1490 (let ((toc (plist-get opt-plist :table-of-contents))
ed21c5c8 1491 (author (org-export-apply-macros-in-string
3ab2c837
BG
1492 (plist-get opt-plist :author)))
1493 (email (replace-regexp-in-string
1494 "_" "\\\\_"
1495 (org-export-apply-macros-in-string
e66ba1df
BG
1496 (plist-get opt-plist :email))))
1497 (description (org-export-apply-macros-in-string
1498 (plist-get opt-plist :description)))
1499 (keywords (org-export-apply-macros-in-string
1500 (plist-get opt-plist :keywords))))
db9c3fb1 1501 (concat
15841868 1502 (if (plist-get opt-plist :time-stamp-file)
6671980f 1503 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
ed21c5c8
CD
1504 ;; insert LaTeX custom header and packages from the list
1505 (org-splice-latex-header
1506 (org-export-apply-macros-in-string org-export-latex-header)
1507 org-export-latex-default-packages-alist
86fbb8ca 1508 org-export-latex-packages-alist nil
ed21c5c8
CD
1509 (org-export-apply-macros-in-string
1510 (plist-get opt-plist :latex-header-extra)))
1511 ;; append another special variable
c8d0cf5c 1512 (org-export-apply-macros-in-string org-export-latex-append-header)
afe98dfa 1513 ;; define alert if not yet defined
ed21c5c8 1514 "\n\\providecommand{\\alert}[1]{\\textbf{#1}}"
15841868 1515 ;; insert the title
db9c3fb1
CD
1516 (format
1517 "\n\n\\title{%s}\n"
acedf35c 1518 (org-export-latex-fontify-headline title))
15841868
JW
1519 ;; insert author info
1520 (if (plist-get opt-plist :author-info)
3ab2c837
BG
1521 (format "\\author{%s%s}\n"
1522 (org-export-latex-fontify-headline (or author user-full-name))
1523 (if (and (plist-get opt-plist :email-info) email
1524 (string-match "\\S-" email))
1525 (format "\\thanks{%s}" email)
1526 ""))
15841868 1527 (format "%%\\author{%s}\n"
ed21c5c8 1528 (org-export-latex-fontify-headline (or author user-full-name))))
15841868
JW
1529 ;; insert the date
1530 (format "\\date{%s}\n"
db9c3fb1 1531 (format-time-string
15841868
JW
1532 (or (plist-get opt-plist :date)
1533 org-export-latex-date-format)))
e66ba1df 1534 ;; add some hyperref options
8223b1d2
BG
1535 (format org-export-latex-hyperref-options-format
1536 (org-export-latex-fontify-headline keywords)
1537 (org-export-latex-fontify-headline description)
1538 (org-version))
3ab2c837
BG
1539 ;; beginning of the document
1540 "\n\\begin{document}\n\n"
15841868 1541 ;; insert the title command
c8d0cf5c
CD
1542 (when (string-match "\\S-" title)
1543 (if (string-match "%s" org-export-latex-title-command)
1544 (format org-export-latex-title-command title)
1545 org-export-latex-title-command))
15841868 1546 "\n\n"
15841868 1547 ;; table of contents
db9c3fb1 1548 (when (and org-export-with-toc
15841868 1549 (plist-get opt-plist :section-numbers))
ed21c5c8
CD
1550 (funcall org-export-latex-format-toc-function
1551 (cond ((numberp toc)
1552 (min toc (plist-get opt-plist :headline-levels)))
1553 (toc (plist-get opt-plist :headline-levels))))))))
1554
1555(defun org-export-latex-format-toc-default (depth)
1556 (when depth
1557 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1558 depth)))
0b8568f5 1559
c8d0cf5c 1560(defun org-export-latex-first-lines (opt-plist &optional beg end)
0b8568f5 1561 "Export the first lines before first headline.
c8d0cf5c
CD
1562If BEG is non-nil, it is the beginning of the region.
1563If END is non-nil, it is the end of the region."
0b8568f5 1564 (save-excursion
db55f368 1565 (goto-char (or beg (point-min)))
8d642074 1566 (let* ((pt (point))
3ab2c837
BG
1567 (end (if (re-search-forward
1568 (concat "^" (org-get-limited-outline-regexp)) end t)
8d642074
CD
1569 (goto-char (match-beginning 0))
1570 (goto-char (or end (point-max))))))
db55f368
CD
1571 (prog1
1572 (org-export-latex-content
1573 (org-export-preprocess-string
1574 (buffer-substring pt end)
3ab2c837 1575 :for-backend 'latex
db55f368
CD
1576 :emph-multiline t
1577 :add-text nil
1578 :comments nil
1579 :skip-before-1st-heading nil
1e4f816a
CD
1580 :LaTeX-fragments nil
1581 :timestamps (plist-get opt-plist :timestamps)
1582 :footnotes (plist-get opt-plist :footnotes)))
c8d0cf5c 1583 (org-unmodified
86fbb8ca
CD
1584 (let ((inhibit-read-only t)
1585 (limit (max pt (1- end))))
1586 (add-text-properties pt limit
1587 '(:org-license-to-kill t))
1588 (save-excursion
1589 (goto-char pt)
afe98dfa
CD
1590 (while (re-search-forward "^[ \t]*#\\+.*\n?" limit t)
1591 (let ((case-fold-search t))
1592 (unless (org-string-match-p
1593 "^[ \t]*#\\+\\(attr_\\|caption\\>\\|label\\>\\)"
1594 (match-string 0))
1595 (remove-text-properties (match-beginning 0) (match-end 0)
1596 '(:org-license-to-kill t))))))))))))
3ab2c837 1597
c8d0cf5c
CD
1598
1599(defvar org-export-latex-header-defs nil
1600 "The header definitions that might be used in the LaTeX body.")
0b8568f5 1601
db9c3fb1
CD
1602(defun org-export-latex-content (content &optional exclude-list)
1603 "Convert CONTENT string to LaTeX.
1604Don't perform conversions that are in EXCLUDE-LIST. Recognized
1605conversion types are: quotation-marks, emphasis, sub-superscript,
1606links, keywords, lists, tables, fixed-width"
1607 (with-temp-buffer
3ab2c837
BG
1608 (org-install-letbind)
1609 (insert content)
1610 (unless (memq 'timestamps exclude-list)
1611 (org-export-latex-time-stamps))
1612 (unless (memq 'quotation-marks exclude-list)
1613 (org-export-latex-quotation-marks))
1614 (unless (memq 'emphasis exclude-list)
1615 (when (plist-get org-export-latex-options-plist :emphasize)
1616 (org-export-latex-fontify)))
1617 (unless (memq 'sub-superscript exclude-list)
1618 (org-export-latex-special-chars
1619 (plist-get org-export-latex-options-plist :sub-superscript)))
1620 (unless (memq 'links exclude-list)
1621 (org-export-latex-links))
1622 (unless (memq 'keywords exclude-list)
1623 (org-export-latex-keywords))
1624 (unless (memq 'lists exclude-list)
1625 (org-export-latex-lists))
1626 (unless (memq 'tables exclude-list)
1627 (org-export-latex-tables
1628 (plist-get org-export-latex-options-plist :tables)))
1629 (unless (memq 'fixed-width exclude-list)
1630 (org-export-latex-fixed-width
1631 (plist-get org-export-latex-options-plist :fixed-width)))
8223b1d2 1632 ;; return string
3ab2c837 1633 (buffer-substring (point-min) (point-max))))
db9c3fb1
CD
1634
1635(defun org-export-latex-protect-string (s)
621f83e4 1636 "Add the org-protected property to string S."
db9c3fb1
CD
1637 (add-text-properties 0 (length s) '(org-protected t) s) s)
1638
1639(defun org-export-latex-protect-char-in-string (char-list string)
1640 "Add org-protected text-property to char from CHAR-LIST in STRING."
1641 (with-temp-buffer
1642 (save-match-data
1643 (insert string)
1644 (goto-char (point-min))
1645 (while (re-search-forward (regexp-opt char-list) nil t)
1646 (add-text-properties (match-beginning 0)
1647 (match-end 0) '(org-protected t)))
1648 (buffer-string))))
1649
0bd48b37 1650(defun org-export-latex-keywords-maybe (&optional remove-list)
72a81656
CD
1651 "Maybe remove keywords depending on rules in REMOVE-LIST."
1652 (goto-char (point-min))
db9c3fb1 1653 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
c8d0cf5c
CD
1654 (case-fold-search nil)
1655 (todo-markup org-export-latex-todo-keyword-markup)
1656 fmt)
72a81656
CD
1657 ;; convert TODO keywords
1658 (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
1659 (if (plist-get remove-list :todo)
1660 (replace-match "")
c8d0cf5c
CD
1661 (setq fmt (cond
1662 ((stringp todo-markup) todo-markup)
1663 ((and (consp todo-markup) (stringp (car todo-markup)))
1664 (if (member (match-string 1) org-export-latex-done-keywords)
1665 (cdr todo-markup) (car todo-markup)))
1666 (t (cdr (or (assoc (match-string 1) todo-markup)
1667 (car todo-markup))))))
86fbb8ca
CD
1668 (replace-match (org-export-latex-protect-string
1669 (format fmt (match-string 1))) t t)))
72a81656
CD
1670 ;; convert priority string
1671 (when (re-search-forward "\\[\\\\#.\\]" nil t)
1672 (if (plist-get remove-list :priority)
1673 (replace-match "")
0bd48b37 1674 (replace-match (format "\\textbf{%s}" (match-string 0)) t t)))
72a81656 1675 ;; convert tags
afe98dfa 1676 (when (re-search-forward "\\(:[a-zA-Z0-9_@#%]+\\)+:" nil t)
0b8568f5
JW
1677 (if (or (not org-export-with-tags)
1678 (plist-get remove-list :tags))
72a81656 1679 (replace-match "")
621f83e4 1680 (replace-match
2c3ad40d 1681 (org-export-latex-protect-string
afe98dfa 1682 (format org-export-latex-tag-markup
33306645
CD
1683 (save-match-data
1684 (replace-regexp-in-string
3ab2c837 1685 "\\([_#]\\)" "\\\\\\1" (match-string 0)))))
33306645 1686 t t)))))
72a81656 1687
db9c3fb1 1688(defun org-export-latex-fontify-headline (string)
621f83e4 1689 "Fontify special words in STRING."
72a81656
CD
1690 (with-temp-buffer
1691 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
1692 ;; the beginning of the buffer - inserting "\n" is safe here though.
db9c3fb1 1693 (insert "\n" string)
acedf35c
CD
1694
1695 ;; Preserve math snippets
3ab2c837 1696
acedf35c
CD
1697 (let* ((matchers (plist-get org-format-latex-options :matchers))
1698 (re-list org-latex-regexps)
1699 beg end re e m n block off)
1700 ;; Check the different regular expressions
1701 (while (setq e (pop re-list))
1702 (setq m (car e) re (nth 1 e) n (nth 2 e)
1703 block (if (nth 3 e) "\n\n" ""))
1704 (setq off (if (member m '("$" "$1")) 1 0))
1705 (when (and (member m matchers) (not (equal m "begin")))
1706 (goto-char (point-min))
1707 (while (re-search-forward re nil t)
1708 (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
1709 (add-text-properties beg end
1710 '(org-protected t org-latex-math t))))))
1711
1712 ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{}
1713 (goto-char (point-min))
1714 (let ((case-fold-search nil))
1715 (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t)
1716 (unless (eq (char-before (match-beginning 1)) ?\\)
1717 (org-if-unprotected-1
1718 (replace-match (org-export-latex-protect-string
1719 (concat "\\" (match-string 1)
1720 "{}")) t t)))))
72a81656 1721 (goto-char (point-min))
ed21c5c8
CD
1722 (let ((re (concat "\\\\\\([a-zA-Z]+\\)"
1723 "\\(?:<[^<>\n]*>\\)*"
1724 "\\(?:\\[[^][\n]*?\\]\\)*"
1725 "\\(?:<[^<>\n]*>\\)*"
1726 "\\("
1727 (org-create-multibrace-regexp "{" "}" 3)
1728 "\\)\\{1,3\\}")))
8bfe682a 1729 (while (re-search-forward re nil t)
ed21c5c8
CD
1730 (unless (or
1731 ;; check for comment line
1732 (save-excursion (goto-char (match-beginning 0))
86fbb8ca 1733 (org-in-indented-comment-line))
ed21c5c8
CD
1734 ;; Check if this is a defined entity, so that is may need conversion
1735 (org-entity-get (match-string 1)))
8bfe682a
CD
1736 (add-text-properties (match-beginning 0) (match-end 0)
1737 '(org-protected t)))))
db9c3fb1 1738 (when (plist-get org-export-latex-options-plist :emphasize)
0b8568f5 1739 (org-export-latex-fontify))
3ab2c837
BG
1740 (org-export-latex-time-stamps)
1741 (org-export-latex-quotation-marks)
0bd48b37 1742 (org-export-latex-keywords-maybe)
2c3ad40d
CD
1743 (org-export-latex-special-chars
1744 (plist-get org-export-latex-options-plist :sub-superscript))
72a81656 1745 (org-export-latex-links)
621f83e4 1746 (org-trim (buffer-string))))
72a81656 1747
c8d0cf5c
CD
1748(defun org-export-latex-time-stamps ()
1749 "Format time stamps."
1750 (goto-char (point-min))
1751 (let ((org-display-custom-times org-export-latex-display-custom-times))
1752 (while (re-search-forward org-ts-regexp-both nil t)
1753 (org-if-unprotected-at (1- (point))
8223b1d2
BG
1754 (replace-match
1755 (org-export-latex-protect-string
1756 (format (if (string= "<" (substring (match-string 0) 0 1))
1757 org-export-latex-timestamp-markup
1758 org-export-latex-timestamp-inactive-markup)
1759 (substring (org-translate-time (match-string 0)) 1 -1)))
1760 t t)))))
c8d0cf5c 1761
72a81656 1762(defun org-export-latex-quotation-marks ()
621f83e4 1763 "Export quotation marks depending on language conventions."
3ab2c837
BG
1764 (mapc (lambda(l)
1765 (goto-char (point-min))
1766 (while (re-search-forward (car l) nil t)
1767 (let ((rpl (concat (match-string 1)
1768 (org-export-latex-protect-string
1769 (copy-sequence (cdr l))))))
1770 (org-if-unprotected-1
1771 (replace-match rpl t t)))))
1772 (cdr (or (assoc (plist-get org-export-latex-options-plist :language)
1773 org-export-latex-quotes)
1774 ;; falls back on english
1775 (assoc "en" org-export-latex-quotes)))))
72a81656 1776
72a81656
CD
1777(defun org-export-latex-special-chars (sub-superscript)
1778 "Export special characters to LaTeX.
1779If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
1780See the `org-export-latex.el' code for a complete conversion table."
1781 (goto-char (point-min))
1782 (mapc (lambda(c)
1783 (goto-char (point-min))
1784 (while (re-search-forward c nil t)
1785 ;; Put the point where to check for org-protected
8223b1d2 1786 (unless (get-text-property (match-beginning 2) 'org-protected)
72a81656 1787 (cond ((member (match-string 2) '("\\$" "$"))
15841868 1788 (if (equal (match-string 2) "\\$")
0bd48b37
CD
1789 nil
1790 (replace-match "\\$" t t)))
15841868
JW
1791 ((member (match-string 2) '("&" "%" "#"))
1792 (if (equal (match-string 1) "\\")
1793 (replace-match (match-string 2) t t)
1794 (replace-match (concat (match-string 1) "\\"
ed21c5c8
CD
1795 (match-string 2)) t t)
1796 (backward-char 1)))
db9c3fb1
CD
1797 ((equal (match-string 2) "...")
1798 (replace-match
1799 (concat (match-string 1)
1800 (org-export-latex-protect-string "\\ldots{}")) t t))
15841868
JW
1801 ((equal (match-string 2) "~")
1802 (cond ((equal (match-string 1) "\\") nil)
1803 ((eq 'org-link (get-text-property 0 'face (match-string 2)))
1804 (replace-match (concat (match-string 1) "\\~") t t))
db9c3fb1 1805 (t (replace-match
15841868
JW
1806 (org-export-latex-protect-string
1807 (concat (match-string 1) "\\~{}")) t t))))
1808 ((member (match-string 2) '("{" "}"))
0bd48b37 1809 (unless (save-match-data (org-inside-latex-math-p))
15841868
JW
1810 (if (equal (match-string 1) "\\")
1811 (replace-match (match-string 2) t t)
1812 (replace-match (concat (match-string 1) "\\"
1813 (match-string 2)) t t)))))
8223b1d2 1814 (unless (save-match-data (or (org-inside-latex-math-p) (org-at-table-p)))
15841868
JW
1815 (cond ((equal (match-string 2) "\\")
1816 (replace-match (or (save-match-data
1817 (org-export-latex-treat-backslash-char
1818 (match-string 1)
e9aab93b 1819 (or (match-string 3) "")))
ed21c5c8
CD
1820 "") t t)
1821 (when (and (get-text-property (1- (point)) 'org-entity)
1822 (looking-at "{}"))
1823 ;; OK, this was an entity replacement, and the user
1824 ;; had terminated the entity with {}. Make sure
1825 ;; {} is protected as well, and remove the extra {}
1826 ;; inserted by the conversion.
1827 (put-text-property (point) (+ 2 (point)) 'org-protected t)
1828 (if (save-excursion (goto-char (max (- (point) 2) (point-min)))
1829 (looking-at "{}"))
1830 (replace-match ""))
1831 (forward-char 2))
1832 (backward-char 1))
15841868
JW
1833 ((member (match-string 2) '("_" "^"))
1834 (replace-match (or (save-match-data
1835 (org-export-latex-treat-sub-super-char
1836 sub-superscript
15841868 1837 (match-string 2)
621f83e4 1838 (match-string 1)
c8d0cf5c
CD
1839 (match-string 3))) "") t t)
1840 (backward-char 1)))))))
0bd48b37
CD
1841 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
1842 "\\(\\(\\\\?\\$\\)\\)"
ed21c5c8
CD
1843 "\\([a-zA-Z0-9()]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-zA-Z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-zA-Z0-9]+}\\|([a-zA-Z0-9]+)\\)"
1844 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|\\([&#%{}\"]\\|[a-zA-Z][a-zA-Z0-9]*\\)\\)"
3ab2c837 1845 "\\(^\\|.\\)\\([&#%{}~]\\|\\.\\.\\.\\)"
15841868
JW
1846 ;; (?\< . "\\textless{}")
1847 ;; (?\> . "\\textgreater{}")
1848 )))
72a81656 1849
0bd48b37
CD
1850(defun org-inside-latex-math-p ()
1851 (get-text-property (point) 'org-latex-math))
1852
72a81656 1853(defun org-export-latex-treat-sub-super-char
621f83e4 1854 (subsup char string-before string-after)
72a81656
CD
1855 "Convert the \"_\" and \"^\" characters to LaTeX.
1856SUBSUP corresponds to the ^: option in the #+OPTIONS line.
1857Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
1858 (cond ((equal string-before "\\")
1859 (concat string-before char string-after))
8d642074
CD
1860 ((and (string-match "\\S-+" string-after))
1861 ;; this is part of a math formula
0b8568f5 1862 (cond ((eq 'org-link (get-text-property 0 'face char))
72a81656 1863 (concat string-before "\\" char string-after))
0bd48b37 1864 ((save-match-data (org-inside-latex-math-p))
72a81656
CD
1865 (if subsup
1866 (cond ((eq 1 (length string-after))
1867 (concat string-before char string-after))
1868 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
db9c3fb1 1869 (format "%s%s{%s}" string-before char
72a81656 1870 (match-string 1 string-after))))))
71d35b24
CD
1871 ((and (> (length string-after) 1)
1872 (or (eq subsup t)
1873 (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
86fbb8ca
CD
1874 (or (string-match "[{]?\\([^}]+\\)[}]?" string-after)
1875 (string-match "[(]?\\([^)]+\\)[)]?" string-after)))
1876
c8d0cf5c
CD
1877 (org-export-latex-protect-string
1878 (format "%s$%s{%s}$" string-before char
1879 (if (and (> (match-end 1) (1+ (match-beginning 1)))
1880 (not (equal (substring string-after 0 2) "{\\")))
1881 (concat "\\mathrm{" (match-string 1 string-after) "}")
1882 (match-string 1 string-after)))))
71d35b24 1883 ((eq subsup t) (concat string-before "$" char string-after "$"))
15841868 1884 (t (org-export-latex-protect-string
d5098885 1885 (concat string-before "\\" char "{}" string-after)))))
15841868 1886 (t (org-export-latex-protect-string
d5098885 1887 (concat string-before "\\" char "{}" string-after)))))
72a81656
CD
1888
1889(defun org-export-latex-treat-backslash-char (string-before string-after)
1890 "Convert the \"$\" special character to LaTeX.
1891The conversion is made depending of STRING-BEFORE and STRING-AFTER."
ed21c5c8
CD
1892 (let ((ass (org-entity-get string-after)))
1893 (cond
1894 (ass (org-add-props
1895 (if (nth 2 ass)
1896 (concat string-before
1897 (org-export-latex-protect-string
1898 (concat "$" (nth 1 ass) "$")))
1899 (concat string-before (org-export-latex-protect-string
1900 (nth 1 ass))))
1901 nil 'org-entity t))
1902 ((and (not (string-match "^[ \n\t]" string-after))
1903 (not (string-match "[ \t]\\'\\|^" string-before)))
1904 ;; backslash is inside a word
1905 (concat string-before
1906 (org-export-latex-protect-string
1907 (concat "\\textbackslash{}" string-after))))
1908 ((not (or (equal string-after "")
1909 (string-match "^[ \t\n]" string-after)))
1910 ;; backslash might escape a character (like \#) or a user TeX
1911 ;; macro (like \setcounter)
1912 (concat string-before
1913 (org-export-latex-protect-string (concat "\\" string-after))))
1914 ((and (string-match "^[ \t\n]" string-after)
1915 (string-match "[ \t\n]\\'" string-before))
1916 ;; backslash is alone, convert it to $\backslash$
1917 (org-export-latex-protect-string
1918 (concat string-before "\\textbackslash{}" string-after)))
1919 (t (org-export-latex-protect-string
1920 (concat string-before "\\textbackslash{}" string-after))))))
72a81656 1921
0bd48b37
CD
1922(defun org-export-latex-keywords ()
1923 "Convert special keywords to LaTeX."
1924 (goto-char (point-min))
c8d0cf5c
CD
1925 (while (re-search-forward org-export-latex-special-keyword-regexp nil t)
1926 (replace-match (format org-export-latex-timestamp-keyword-markup
1927 (match-string 0)) t t)
1928 (save-excursion
1929 (beginning-of-line 1)
ed21c5c8 1930 (unless (looking-at ".*\n[ \t]*\n")
c8d0cf5c 1931 (end-of-line 1)
ed21c5c8 1932 (insert "\n")))))
db9c3fb1 1933
72a81656
CD
1934(defun org-export-latex-fixed-width (opt)
1935 "When OPT is non-nil convert fixed-width sections to LaTeX."
1936 (goto-char (point-min))
0bd48b37 1937 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t)
86fbb8ca 1938 (unless (get-text-property (point) 'org-example)
8223b1d2
BG
1939 (if opt
1940 (progn (goto-char (match-beginning 0))
1941 (insert "\\begin{verbatim}\n")
1942 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1943 (replace-match (concat (match-string 1)
1944 (match-string 2)) t t)
1945 (forward-line))
1946 (insert "\\end{verbatim}\n"))
1947 (progn (goto-char (match-beginning 0))
1948 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1949 (replace-match (concat "%" (match-string 1)
1950 (match-string 2)) t t)
1951 (forward-line)))))))
20908596
CD
1952
1953(defvar org-table-last-alignment) ; defined in org-table.el
54a0dee5 1954(defvar org-table-last-column-widths) ; defined in org-table.el
20908596 1955(declare-function orgtbl-to-latex "org-table" (table params) t)
15841868
JW
1956(defun org-export-latex-tables (insert)
1957 "Convert tables to LaTeX and INSERT it."
ed21c5c8
CD
1958 ;; First, get the table.el tables
1959 (goto-char (point-min))
1960 (while (re-search-forward "^[ \t]*\\(\\+-[-+]*\\+\\)[ \t]*\n[ \t]*|" nil t)
1961 (org-if-unprotected
1962 (require 'table)
1963 (org-export-latex-convert-table.el-table)))
1964
1965 ;; And now the Org-mode tables
72a81656
CD
1966 (goto-char (point-min))
1967 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
8bfe682a
CD
1968 (org-if-unprotected-at (1- (point))
1969 (org-table-align)
1970 (let* ((beg (org-table-begin))
1971 (end (org-table-end))
1972 (raw-table (buffer-substring beg end))
1973 (org-table-last-alignment (copy-sequence org-table-last-alignment))
1974 (org-table-last-column-widths (copy-sequence
1975 org-table-last-column-widths))
1976 fnum fields line lines olines gr colgropen line-fmt align
8223b1d2 1977 caption width shortn label attr hfmt floatp placement
3ab2c837 1978 longtblp tblenv tabular-env)
8bfe682a
CD
1979 (if org-export-latex-tables-verbatim
1980 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1981 "\\end{verbatim}\n")))
1982 (apply 'delete-region (list beg end))
1983 (insert (org-export-latex-protect-string tbl)))
1984 (progn
1985 (setq caption (org-find-text-property-in-string
1986 'org-caption raw-table)
86fbb8ca
CD
1987 shortn (org-find-text-property-in-string
1988 'org-caption-shortn raw-table)
8bfe682a
CD
1989 attr (org-find-text-property-in-string
1990 'org-attributes raw-table)
1991 label (org-find-text-property-in-string
1992 'org-label raw-table)
1993 longtblp (and attr (stringp attr)
1994 (string-match "\\<longtable\\>" attr))
153ae947
BG
1995 tblenv (if (and attr (stringp attr))
1996 (cond ((string-match "\\<sidewaystable\\>" attr)
1997 "sidewaystable")
1998 ((or (string-match (regexp-quote "table*") attr)
1999 (string-match "\\<multicolumn\\>" attr))
2000 "table*")
2001 (t "table"))
2002 "table")
3ab2c837
BG
2003 tabular-env
2004 (if (and attr (stringp attr)
2005 (string-match "\\(tabular.\\)" attr))
2006 (match-string 1 attr)
2007 org-export-latex-tabular-environment)
2008 width (and attr (stringp attr)
2009 (string-match "\\<width=\\([^ \t\n\r]+\\)" attr)
2010 (match-string 1 attr))
8bfe682a 2011 align (and attr (stringp attr)
86fbb8ca 2012 (string-match "\\<align=\\([^ \t\n\r]+\\)" attr)
8bfe682a 2013 (match-string 1 attr))
8223b1d2
BG
2014 hfmt (and attr (stringp attr)
2015 (string-match "\\<hfmt=\\(\\S-+\\)" attr)
2016 (match-string 1 attr))
3ab2c837
BG
2017 floatp (or caption label (string= "table*" tblenv))
2018 placement (if (and attr
afe98dfa
CD
2019 (stringp attr)
2020 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr))
2021 (match-string 1 attr)
3ab2c837
BG
2022 (concat
2023 "[" org-latex-default-figure-position "]")))
ed21c5c8 2024 (setq caption (and caption (org-export-latex-fontify-headline caption)))
8bfe682a
CD
2025 (setq lines (org-split-string raw-table "\n"))
2026 (apply 'delete-region (list beg end))
2027 (when org-export-table-remove-special-lines
2028 (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
2029 (when org-table-clean-did-remove-column
54a0dee5
CD
2030 (pop org-table-last-alignment)
2031 (pop org-table-last-column-widths))
8223b1d2 2032 ;; make a format string to reflect alignment
8bfe682a
CD
2033 (setq olines lines)
2034 (while (and (not line-fmt) (setq line (pop olines)))
2035 (unless (string-match "^[ \t]*|-" line)
2036 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
2037 (setq fnum (make-vector (length fields) 0))
2038 (setq line-fmt
2039 (mapconcat
2040 (lambda (x)
2041 (setq gr (pop org-table-colgroup-info))
2042 (format "%s%%s%s"
2043 (cond ((eq gr :start)
2044 (prog1 (if colgropen "|" "|")
2045 (setq colgropen t)))
2046 ((eq gr :startend)
2047 (prog1 (if colgropen "|" "|")
2048 (setq colgropen nil)))
2049 (t ""))
2050 (if (memq gr '(:end :startend))
2051 (progn (setq colgropen nil) "|")
2052 "")))
2053 fnum ""))))
2054 ;; fix double || in line-fmt
2055 (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
2056 ;; maybe remove the first and last "|"
2057 (when (and (not org-export-latex-tables-column-borders)
2058 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
2059 (setq line-fmt (match-string 2 line-fmt)))
2060 ;; format alignment
2061 (unless align
2062 (setq align (apply 'format
2063 (cons line-fmt
2064 (mapcar (lambda (x) (if x "r" "l"))
2065 org-table-last-alignment)))))
2066 ;; prepare the table to send to orgtbl-to-latex
2067 (setq lines
2068 (mapcar
2069 (lambda(elem)
2070 (or (and (string-match "[ \t]*|-+" elem) 'hline)
3ab2c837
BG
2071 (org-split-string
2072 (progn (set-text-properties 0 (length elem) nil elem)
2073 (org-trim elem)) "|")))
8bfe682a
CD
2074 lines))
2075 (when insert
2076 (insert (org-export-latex-protect-string
2077 (concat
2078 (if longtblp
2079 (concat "\\begin{longtable}{" align "}\n")
3ab2c837
BG
2080 (if floatp
2081 (format "\\begin{%s}%s\n" tblenv placement)))
e66ba1df 2082 (if (and floatp org-export-latex-table-caption-above)
8bfe682a 2083 (format
afe98dfa 2084 "\\caption%s{%s} %s"
86fbb8ca 2085 (if shortn (concat "[" shortn "]") "")
afe98dfa
CD
2086 (or caption "")
2087 (if label (format "\\label{%s}" label) "")))
153ae947
BG
2088 (if (and longtblp caption org-export-latex-table-caption-above)
2089 "\\\\\n" "\n")
8bfe682a
CD
2090 (if (and org-export-latex-tables-centered (not longtblp))
2091 "\\begin{center}\n")
86fbb8ca 2092 (if (not longtblp)
3ab2c837
BG
2093 (format "\\begin{%s}%s{%s}\n"
2094 tabular-env
2095 (if width (format "{%s}" width) "")
2096 align))
8bfe682a
CD
2097 (orgtbl-to-latex
2098 lines
8223b1d2
BG
2099 `(:tstart ,org-export-latex-tables-tstart
2100 :tend ,org-export-latex-tables-tend
2101 :hline ,org-export-latex-tables-hline
2102 :skipheadrule ,longtblp
2103 :hfmt ,hfmt
8bfe682a
CD
2104 :hlend ,(if longtblp
2105 (format "\\\\
8223b1d2 2106%s
db55f368 2107\\endhead
8223b1d2 2108%s\\multicolumn{%d}{r}{Continued on next page}\\
db55f368 2109\\endfoot
8223b1d2
BG
2110\\endlastfoot"
2111 org-export-latex-tables-hline
2112 org-export-latex-tables-hline
2113 (length org-table-last-alignment))
8bfe682a 2114 nil)))
3ab2c837 2115 (if (not longtblp) (format "\n\\end{%s}" tabular-env))
8bfe682a
CD
2116 (if longtblp "\n" (if org-export-latex-tables-centered
2117 "\n\\end{center}\n" "\n"))
e66ba1df
BG
2118 (if (and floatp (not org-export-latex-table-caption-above))
2119 (format
2120 "\\caption%s{%s} %s"
2121 (if shortn (concat "[" shortn "]") "")
2122 (or caption "")
2123 (if label (format "\\label{%s}" label) "")))
8bfe682a
CD
2124 (if longtblp
2125 "\\end{longtable}"
3ab2c837 2126 (if floatp (format "\\end{%s}" tblenv)))))
8bfe682a 2127 "\n\n"))))))))
72a81656 2128
ed21c5c8
CD
2129(defun org-export-latex-convert-table.el-table ()
2130 "Replace table.el table at point with LaTeX code."
86fbb8ca 2131 (let (tbl caption shortn label line floatp attr align rmlines)
ed21c5c8
CD
2132 (setq line (buffer-substring (point-at-bol) (point-at-eol))
2133 label (org-get-text-property-any 0 'org-label line)
2134 caption (org-get-text-property-any 0 'org-caption line)
86fbb8ca 2135 shortn (org-get-text-property-any 0 'org-caption-shortn line)
ed21c5c8
CD
2136 attr (org-get-text-property-any 0 'org-attributes line)
2137 align (and attr (stringp attr)
2138 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
2139 (match-string 1 attr))
2140 rmlines (and attr (stringp attr)
2141 (string-match "\\<rmlines\\>" attr))
2142 floatp (or label caption))
2143 (and (get-buffer "*org-export-table*")
2144 (kill-buffer (get-buffer "*org-export-table*")))
2145 (table-generate-source 'latex "*org-export-table*" "caption")
2146 (setq tbl (with-current-buffer "*org-export-table*"
2147 (buffer-string)))
2148 (while (string-match "^%.*\n" tbl)
2149 (setq tbl (replace-match "" t t tbl)))
2150 ;; fix the hlines
2151 (when rmlines
2152 (let ((n 0) lines)
2153 (setq lines (mapcar (lambda (x)
2154 (if (string-match "^\\\\hline$" x)
2155 (progn
2156 (setq n (1+ n))
2157 (if (= n 2) x nil))
2158 x))
2159 (org-split-string tbl "\n")))
2160 (setq tbl (mapconcat 'identity (delq nil lines) "\n"))))
2161 (when (and align (string-match "\\\\begin{tabular}{.*}" tbl))
2162 (setq tbl (replace-match (concat "\\begin{tabular}{" align "}")
2163 t t tbl)))
2164 (and (get-buffer "*org-export-table*")
2165 (kill-buffer (get-buffer "*org-export-table*")))
2166 (beginning-of-line 0)
2167 (while (looking-at "[ \t]*\\(|\\|\\+-\\)")
2168 (delete-region (point) (1+ (point-at-eol))))
2169 (when org-export-latex-tables-centered
2170 (setq tbl (concat "\\begin{center}\n" tbl "\\end{center}")))
2171 (when floatp
2172 (setq tbl (concat "\\begin{table}\n"
e66ba1df 2173 (if (not org-export-latex-table-caption-above) tbl)
3ab2c837 2174 (format "\\caption%s{%s%s}\n"
86fbb8ca 2175 (if shortn (format "[%s]" shortn) "")
ed21c5c8
CD
2176 (if label (format "\\label{%s}" label) "")
2177 (or caption ""))
e66ba1df 2178 (if org-export-latex-table-caption-above tbl)
ed21c5c8
CD
2179 "\n\\end{table}\n")))
2180 (insert (org-export-latex-protect-string tbl))))
2181
72a81656
CD
2182(defun org-export-latex-fontify ()
2183 "Convert fontification to LaTeX."
2184 (goto-char (point-min))
2185 (while (re-search-forward org-emph-re nil t)
5dec9555 2186 ;; The match goes one char after the *string*, except at the end of a line
db9c3fb1
CD
2187 (let ((emph (assoc (match-string 3)
2188 org-export-latex-emphasis-alist))
0bd48b37
CD
2189 (beg (match-beginning 0))
2190 (end (match-end 0))
8bfe682a 2191 rpl s)
c8d0cf5c
CD
2192 (unless emph
2193 (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
2194 (match-string 3)))
8bfe682a
CD
2195 (unless (or (and (get-text-property (- (point) 2) 'org-protected)
2196 (not (get-text-property
2197 (- (point) 2) 'org-verbatim-emph)))
86fbb8ca
CD
2198 (equal (char-after (match-beginning 3))
2199 (char-after (1+ (match-beginning 3))))
0bd48b37
CD
2200 (save-excursion
2201 (goto-char (match-beginning 1))
2202 (save-match-data
2203 (and (org-at-table-p)
2204 (string-match
ed21c5c8
CD
2205 "[|\n]" (buffer-substring beg end)))))
2206 (and (equal (match-string 3) "+")
2207 (save-match-data
2208 (string-match "\\`-+\\'" (match-string 4)))))
8bfe682a 2209 (setq s (match-string 4))
15841868 2210 (setq rpl (concat (match-string 1)
c8d0cf5c
CD
2211 (org-export-latex-emph-format (cadr emph)
2212 (match-string 4))
15841868 2213 (match-string 5)))
db9c3fb1 2214 (if (caddr emph)
8bfe682a
CD
2215 (setq rpl (org-export-latex-protect-string rpl))
2216 (save-match-data
ed21c5c8 2217 (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl)
8bfe682a
CD
2218 (progn
2219 (add-text-properties (match-beginning 1) (match-end 1)
2220 '(org-protected t) rpl)
2221 (add-text-properties (match-beginning 3) (match-end 3)
2222 '(org-protected t) rpl)))))
15841868
JW
2223 (replace-match rpl t t)))
2224 (backward-char)))
72a81656 2225
c8d0cf5c
CD
2226(defun org-export-latex-emph-format (format string)
2227 "Format an emphasis string and handle the \\verb special case."
3ab2c837 2228 (when (member format '("\\verb" "\\protectedtexttt"))
c8d0cf5c 2229 (save-match-data
3ab2c837 2230 (if (equal format "\\verb")
c8d0cf5c
CD
2231 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
2232 (catch 'exit
2233 (loop for i from 0 to (1- (length ll)) do
2234 (if (not (string-match (regexp-quote (substring ll i (1+ i)))
2235 string))
2236 (progn
2237 (setq format (concat "\\verb" (substring ll i (1+ i))
2238 "%s" (substring ll i (1+ i))))
2239 (throw 'exit nil))))))
2240 (let ((start 0)
8d642074
CD
2241 (trans '(("\\" . "\\textbackslash{}")
2242 ("~" . "\\textasciitilde{}")
2243 ("^" . "\\textasciicircum{}")))
c8d0cf5c
CD
2244 (rtn "") char)
2245 (while (string-match "[\\{}$%&_#~^]" string)
2246 (setq char (match-string 0 string))
2247 (if (> (match-beginning 0) 0)
2248 (setq rtn (concat rtn (substring string
2249 0 (match-beginning 0)))))
2250 (setq string (substring string (1+ (match-beginning 0))))
2251 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
2252 rtn (concat rtn char)))
3ab2c837
BG
2253 (setq string (concat rtn string) format "\\texttt{%s}")
2254 (while (string-match "--" string)
2255 (setq string (replace-match "-{}-" t t string)))))))
8bfe682a 2256 (format format string))
c8d0cf5c 2257
72a81656
CD
2258(defun org-export-latex-links ()
2259 ;; Make sure to use the LaTeX hyperref and graphicx package
2260 ;; or send some warnings.
2261 "Convert links to LaTeX."
2262 (goto-char (point-min))
0bd48b37 2263 (while (re-search-forward org-bracket-link-analytic-regexp++ nil t)
8bfe682a 2264 (org-if-unprotected-1
72a81656 2265 (goto-char (match-beginning 0))
db9c3fb1 2266 (let* ((re-radio org-export-latex-all-targets-re)
72a81656 2267 (remove (list (match-beginning 0) (match-end 0)))
2c3ad40d 2268 (raw-path (org-extract-attributes (match-string 3)))
72a81656
CD
2269 (full-raw-path (concat (match-string 1) raw-path))
2270 (desc (match-string 5))
db55f368
CD
2271 (type (or (match-string 2)
2272 (if (or (file-name-absolute-p raw-path)
2273 (string-match "^\\.\\.?/" raw-path))
2274 "file")))
0bd48b37 2275 (coderefp (equal type "coderef"))
db55f368 2276 (caption (org-find-text-property-in-string 'org-caption raw-path))
86fbb8ca 2277 (shortn (org-find-text-property-in-string 'org-caption-shortn raw-path))
c8d0cf5c
CD
2278 (attr (or (org-find-text-property-in-string 'org-attributes raw-path)
2279 (plist-get org-export-latex-options-plist :latex-image-options)))
db55f368 2280 (label (org-find-text-property-in-string 'org-label raw-path))
86fbb8ca 2281 imgp radiop fnc
72a81656
CD
2282 ;; define the path of the link
2283 (path (cond
0bd48b37
CD
2284 ((member type '("coderef"))
2285 raw-path)
72a81656
CD
2286 ((member type '("http" "https" "ftp"))
2287 (concat type ":" raw-path))
2288 ((and re-radio (string-match re-radio raw-path))
33306645 2289 (setq radiop t))
72a81656
CD
2290 ((equal type "mailto")
2291 (concat type ":" raw-path))
2292 ((equal type "file")
0bd48b37 2293 (if (and (org-file-image-p
63aa0982 2294 (expand-file-name (org-link-unescape raw-path))
0bd48b37 2295 org-export-latex-inline-image-extensions)
63aa0982 2296 (or (get-text-property 0 'org-no-description raw-path)
c8d0cf5c 2297 (equal desc full-raw-path)))
72a81656 2298 (setq imgp t)
63aa0982
BG
2299 (progn (setq raw-path (org-link-unescape raw-path))
2300 (when (string-match "\\(.+\\)::.+" raw-path)
72a81656
CD
2301 (setq raw-path (match-string 1 raw-path)))
2302 (if (file-exists-p raw-path)
2303 (concat type "://" (expand-file-name raw-path))
2304 (concat type "://" (org-export-directory
db9c3fb1 2305 :LaTeX org-export-latex-options-plist)
72a81656
CD
2306 raw-path))))))))
2307 ;; process with link inserting
2308 (apply 'delete-region remove)
ed21c5c8 2309 (setq caption (and caption (org-export-latex-fontify-headline caption)))
8bfe682a
CD
2310 (cond ((and imgp
2311 (plist-get org-export-latex-options-plist :inline-images))
2312 ;; OK, we need to inline an image
db55f368 2313 (insert
86fbb8ca 2314 (org-export-latex-format-image raw-path caption label attr shortn)))
0bd48b37
CD
2315 (coderefp
2316 (insert (format
2317 (org-export-get-coderef-format path desc)
2318 (cdr (assoc path org-export-code-refs)))))
acedf35c 2319 (radiop (insert (format org-export-latex-hyperref-format
621f83e4
CD
2320 (org-solidify-link-text raw-path) desc)))
2321 ((not type)
acedf35c 2322 (insert (format org-export-latex-hyperref-format
c8d0cf5c 2323 (org-remove-initial-hash
54a0dee5
CD
2324 (org-solidify-link-text raw-path))
2325 desc)))
ed21c5c8 2326 (path
54a0dee5
CD
2327 (when (org-at-table-p)
2328 ;; There is a strange problem when we have a link in a table,
2329 ;; ampersands then cause a problem. I think this must be
2330 ;; a LaTeX issue, but we here implement a work-around anyway.
2331 (setq path (org-export-latex-protect-amp path)
2332 desc (org-export-latex-protect-amp desc)))
14e1337f 2333 (insert
3ab2c837
BG
2334 (if (string-match "%s.*%s" org-export-latex-href-format)
2335 (format org-export-latex-href-format path desc)
2336 (format org-export-latex-href-format path))))
86fbb8ca
CD
2337
2338 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
2339 ;; The link protocol has a function for formatting the link
2340 (insert
2341 (save-match-data
2342 (funcall fnc (org-link-unescape raw-path) desc 'latex))))
8223b1d2
BG
2343 ;; Unrecognized path type
2344 (t (insert (format org-export-latex-link-with-unknown-path-format desc))))))))
72a81656 2345
8bfe682a 2346
86fbb8ca 2347(defun org-export-latex-format-image (path caption label attr &optional shortn)
8bfe682a 2348 "Format the image element, depending on user settings."
86fbb8ca 2349 (let (ind floatp wrapp multicolumnp placement figenv)
8bfe682a 2350 (setq floatp (or caption label))
ed21c5c8 2351 (setq ind (org-get-text-property-any 0 'original-indentation path))
8bfe682a
CD
2352 (when (and attr (stringp attr))
2353 (if (string-match "[ \t]*\\<wrap\\>" attr)
2354 (setq wrapp t floatp nil attr (replace-match "" t t attr)))
2355 (if (string-match "[ \t]*\\<float\\>" attr)
86fbb8ca
CD
2356 (setq wrapp nil floatp t attr (replace-match "" t t attr)))
2357 (if (string-match "[ \t]*\\<multicolumn\\>" attr)
2358 (setq multicolumnp t attr (replace-match "" t t attr))))
ed21c5c8 2359
8bfe682a
CD
2360 (setq placement
2361 (cond
2362 (wrapp "{l}{0.5\\textwidth}")
3ab2c837 2363 (floatp (concat "[" org-latex-default-figure-position "]"))
8bfe682a
CD
2364 (t "")))
2365
2366 (when (and attr (stringp attr)
2367 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr))
2368 (setq placement (match-string 1 attr)
2369 attr (replace-match "" t t attr)))
2370 (setq attr (and attr (org-trim attr)))
2371 (when (or (not attr) (= (length attr) 0))
2372 (setq attr (cond (floatp "width=0.7\\textwidth")
2373 (wrapp "width=0.48\\textwidth")
2374 (t attr))))
2375 (setq figenv
2376 (cond
2377 (wrapp "\\begin{wrapfigure}%placement
2378\\centering
2379\\includegraphics[%attr]{%path}
86fbb8ca 2380\\caption%shortn{%labelcmd%caption}
8bfe682a 2381\\end{wrapfigure}")
86fbb8ca
CD
2382 (multicolumnp "\\begin{figure*}%placement
2383\\centering
2384\\includegraphics[%attr]{%path}
3ab2c837 2385\\caption%shortn{%labelcmd%caption}
86fbb8ca 2386\\end{figure*}")
8bfe682a
CD
2387 (floatp "\\begin{figure}%placement
2388\\centering
2389\\includegraphics[%attr]{%path}
3ab2c837 2390\\caption%shortn{%labelcmd%caption}
8bfe682a
CD
2391\\end{figure}")
2392 (t "\\includegraphics[%attr]{%path}")))
2393
ed21c5c8
CD
2394
2395 (setq figenv (mapconcat 'identity (split-string figenv "\n")
2396 (save-excursion (beginning-of-line 1)
2397 (looking-at "[ \t]*")
2398 (concat "\n" (match-string 0)))))
2399
8bfe682a
CD
2400 (if (and (not label) (not caption)
2401 (string-match "^\\\\caption{.*\n" figenv))
2402 (setq figenv (replace-match "" t t figenv)))
ed21c5c8
CD
2403 (org-add-props
2404 (org-fill-template
2405 figenv
2406 (list (cons "path"
2407 (if (file-name-absolute-p path)
2408 (expand-file-name path)
2409 path))
2410 (cons "attr" attr)
86fbb8ca 2411 (cons "shortn" (if shortn (format "[%s]" shortn) ""))
ed21c5c8
CD
2412 (cons "labelcmd" (if label (format "\\label{%s}"
2413 label)""))
2414 (cons "caption" (or caption ""))
2415 (cons "placement" (or placement ""))))
2416 nil 'original-indentation ind)))
8bfe682a 2417
54a0dee5
CD
2418(defun org-export-latex-protect-amp (s)
2419 (while (string-match "\\([^\\\\]\\)\\(&\\)" s)
2420 (setq s (replace-match (concat (match-string 1 s) "\\" (match-string 2 s))
2421 t t s)))
2422 s)
2423
c8d0cf5c
CD
2424(defun org-remove-initial-hash (s)
2425 (if (string-match "\\`#" s)
2426 (substring s 1)
2427 s))
db9c3fb1 2428(defvar org-latex-entities) ; defined below
71d35b24 2429(defvar org-latex-entities-regexp) ; defined below
db9c3fb1 2430
c8d0cf5c 2431(defun org-export-latex-preprocess (parameters)
72a81656 2432 "Clean stuff in the LaTeX export."
3ab2c837
BG
2433 ;; Replace footnotes.
2434 (when (plist-get parameters :footnotes)
2435 (goto-char (point-min))
2436 (let (ref)
2437 (while (setq ref (org-footnote-get-next-reference))
2438 (let* ((beg (nth 1 ref))
2439 (lbl (car ref))
2440 (def (nth 1 (assoc (string-to-number lbl)
2441 (mapcar (lambda (e) (cdr e))
2442 org-export-footnotes-seen)))))
2443 ;; Fix body for footnotes ending on a link or a list and
2444 ;; remove definition from buffer.
2445 (setq def
2446 (concat def
2447 (if (string-match "ORG-LIST-END-MARKER\\'" def)
2448 "\n" " ")))
2449 (org-footnote-delete-definitions lbl)
2450 ;; Compute string to insert (FNOTE), and protect the outside
2451 ;; macro from further transformation. When footnote at
2452 ;; point is referring to a previously defined footnote, use
8223b1d2 2453 ;; \footnotemark. Otherwise, use \footnote.
3ab2c837
BG
2454 (let ((fnote (if (member lbl org-export-latex-footmark-seen)
2455 (org-export-latex-protect-string
2456 (format "\\footnotemark[%s]" lbl))
2457 (push lbl org-export-latex-footmark-seen)
2458 (concat (org-export-latex-protect-string "\\footnote{")
2459 def
2460 (org-export-latex-protect-string "}"))))
2461 ;; Check if another footnote is immediately following.
2462 ;; If so, add a separator in-between.
2463 (sep (org-export-latex-protect-string
2464 (if (save-excursion (goto-char (1- (nth 2 ref)))
2465 (let ((next (org-footnote-get-next-reference)))
2466 (and next (= (nth 1 next) (nth 2 ref)))))
2467 org-export-latex-footnote-separator ""))))
e66ba1df 2468 (when (org-at-heading-p)
3ab2c837
BG
2469 (setq fnote (concat (org-export-latex-protect-string "\\protect")
2470 fnote)))
2471 ;; Ensure a footnote at column 0 cannot end a list
2472 ;; containing it.
2473 (put-text-property 0 (length fnote) 'original-indentation 1000 fnote)
2474 ;; Replace footnote reference with FNOTE and, maybe, SEP.
2475 ;; `save-excursion' is required if there are two footnotes
2476 ;; in a row. In that case, point would be left at the
2477 ;; beginning of the second one, and
2478 ;; `org-footnote-get-next-reference' would then skip it.
2479 (goto-char beg)
2480 (delete-region beg (nth 2 ref))
2481 (save-excursion (insert fnote sep)))))))
2482
2483 ;; Remove footnote section tag for LaTeX
2484 (goto-char (point-min))
2485 (while (re-search-forward
2486 (concat "^" footnote-section-tag-regexp) nil t)
2487 (org-if-unprotected
2488 (replace-match "")))
2489 ;; Remove any left-over footnote definition.
2490 (mapc (lambda (fn) (org-footnote-delete-definitions (car fn)))
2491 org-export-footnotes-data)
2492 (mapc (lambda (fn) (org-footnote-delete-definitions fn))
2493 org-export-latex-footmark-seen)
2494
0b8568f5 2495 ;; Preserve line breaks
72a81656
CD
2496 (goto-char (point-min))
2497 (while (re-search-forward "\\\\\\\\" nil t)
2498 (add-text-properties (match-beginning 0) (match-end 0)
2499 '(org-protected t)))
2500
621f83e4
CD
2501 ;; Preserve latex environments
2502 (goto-char (point-min))
c8d0cf5c 2503 (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t)
86fbb8ca
CD
2504 (org-if-unprotected
2505 (let* ((start (progn (beginning-of-line) (point)))
3ab2c837
BG
2506 (end (and (re-search-forward
2507 (concat "^[ \t]*\\\\end{"
2508 (regexp-quote (match-string 1))
2509 "}") nil t)
2510 (point-at-eol))))
2511 (if end
2512 (add-text-properties start end '(org-protected t))
2513 (goto-char (point-at-eol))))))
621f83e4 2514
0bd48b37 2515 ;; Preserve math snippets
0bd48b37
CD
2516 (let* ((matchers (plist-get org-format-latex-options :matchers))
2517 (re-list org-latex-regexps)
2518 beg end re e m n block off)
2519 ;; Check the different regular expressions
2520 (while (setq e (pop re-list))
2521 (setq m (car e) re (nth 1 e) n (nth 2 e)
2522 block (if (nth 3 e) "\n\n" ""))
2523 (setq off (if (member m '("$" "$1")) 1 0))
2524 (when (and (member m matchers) (not (equal m "begin")))
2525 (goto-char (point-min))
2526 (while (re-search-forward re nil t)
2527 (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
2528 (add-text-properties beg end '(org-protected t org-latex-math t))))))
2529
ed21c5c8 2530 ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{}
72a81656 2531 (goto-char (point-min))
65c439fd 2532 (let ((case-fold-search nil))
ed21c5c8
CD
2533 (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t)
2534 (unless (eq (char-before (match-beginning 1)) ?\\)
2535 (org-if-unprotected-1
2536 (replace-match (org-export-latex-protect-string
2537 (concat "\\" (match-string 1)
2538 "{}")) t t)))))
72a81656 2539
b349f79f
CD
2540 ;; Convert blockquotes
2541 (goto-char (point-min))
621f83e4 2542 (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
c8d0cf5c 2543 (org-replace-match-keep-properties "\\begin{quote}" t t))
b349f79f 2544 (goto-char (point-min))
621f83e4 2545 (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
c8d0cf5c 2546 (org-replace-match-keep-properties "\\end{quote}" t t))
b349f79f
CD
2547
2548 ;; Convert verse
2549 (goto-char (point-min))
621f83e4 2550 (while (search-forward "ORG-VERSE-START" nil t)
c8d0cf5c
CD
2551 (org-replace-match-keep-properties "\\begin{verse}" t t)
2552 (beginning-of-line 2)
2553 (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
2554 (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
2555 (goto-char (match-end 1))
2556 (org-replace-match-keep-properties
2557 (org-export-latex-protect-string
2558 (concat "\\hspace*{1cm}" (match-string 2))) t t)
2559 (beginning-of-line 1))
8d642074 2560 (if (looking-at "[ \t]*$")
8bfe682a 2561 (insert (org-export-latex-protect-string "\\vspace*{1em}"))
8d642074
CD
2562 (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
2563 (end-of-line 1)
2564 (insert "\\\\")))
c8d0cf5c
CD
2565 (beginning-of-line 2))
2566 (and (looking-at "[ \t]*ORG-VERSE-END.*")
2567 (org-replace-match-keep-properties "\\end{verse}" t t)))
2568
3ab2c837
BG
2569 ;; Convert #+INDEX to LaTeX \\index.
2570 (goto-char (point-min))
2571 (let ((case-fold-search t) entry)
2572 (while (re-search-forward
2573 "^[ \t]*#\\+index:[ \t]*\\([^ \t\r\n].*?\\)[ \t]*$"
2574 nil t)
2575 (setq entry
2576 (save-match-data
2577 (org-export-latex-protect-string
2578 (org-export-latex-fontify-headline (match-string 1)))))
2579 (replace-match (format "\\index{%s}" entry) t t)))
2580
c8d0cf5c
CD
2581 ;; Convert center
2582 (goto-char (point-min))
2583 (while (search-forward "ORG-CENTER-START" nil t)
2584 (org-replace-match-keep-properties "\\begin{center}" t t))
b349f79f 2585 (goto-char (point-min))
c8d0cf5c
CD
2586 (while (search-forward "ORG-CENTER-END" nil t)
2587 (org-replace-match-keep-properties "\\end{center}" t t))
2588
2589 (run-hooks 'org-export-latex-after-blockquotes-hook)
b349f79f 2590
0b8568f5 2591 ;; Convert horizontal rules
72a81656 2592 (goto-char (point-min))
3ab2c837 2593 (while (re-search-forward "^[ \t]*-\\{5,\\}[ \t]*$" nil t)
0bd48b37
CD
2594 (org-if-unprotected
2595 (replace-match (org-export-latex-protect-string "\\hrule") t t)))
72a81656 2596
33306645 2597 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
ed21c5c8
CD
2598 (goto-char (point-min))
2599 (let ((re (concat
3ab2c837 2600 "\\\\\\([a-zA-Z]+\\*?\\)"
ed21c5c8
CD
2601 "\\(?:<[^<>\n]*>\\)*"
2602 "\\(?:\\[[^][\n]*?\\]\\)*"
2603 "\\(?:<[^<>\n]*>\\)*"
2604 "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}")))
c8d0cf5c 2605 (while (re-search-forward re nil t)
86fbb8ca 2606 (unless (or
3ab2c837 2607 ;; Check for comment line.
ed21c5c8 2608 (save-excursion (goto-char (match-beginning 0))
86fbb8ca 2609 (org-in-indented-comment-line))
3ab2c837
BG
2610 ;; Check if this is a defined entity, so that is may
2611 ;; need conversion.
ed21c5c8 2612 (org-entity-get (match-string 1))
3ab2c837
BG
2613 ;; Do not protect interior of footnotes. Those have
2614 ;; already been taken care of earlier in the function.
2615 ;; Yet, keep looking inside them for more commands.
2616 (and (equal (match-string 1) "footnote")
2617 (goto-char (match-end 1))))
8d642074
CD
2618 (add-text-properties (match-beginning 0) (match-end 0)
2619 '(org-protected t)))))
d5098885 2620
86fbb8ca
CD
2621 ;; Special case for \nbsp
2622 (goto-char (point-min))
2623 (while (re-search-forward "\\\\nbsp\\({}\\|\\>\\)" nil t)
2624 (org-if-unprotected
2625 (replace-match (org-export-latex-protect-string "~"))))
2626
db9c3fb1
CD
2627 ;; Protect LaTeX entities
2628 (goto-char (point-min))
86fbb8ca
CD
2629 (while (re-search-forward org-latex-entities-regexp nil t)
2630 (org-if-unprotected
2631 (add-text-properties (match-beginning 0) (match-end 0)
2632 '(org-protected t))))
db9c3fb1 2633
72a81656
CD
2634 ;; Replace radio links
2635 (goto-char (point-min))
d5098885 2636 (while (re-search-forward
db9c3fb1 2637 (concat "<<<?" org-export-latex-all-targets-re
d5098885 2638 ">>>?\\((INVISIBLE)\\)?") nil t)
8bfe682a 2639 (org-if-unprotected-at (+ (match-beginning 0) 2)
3ab2c837
BG
2640 (replace-match
2641 (concat
2642 (org-export-latex-protect-string
2643 (format "\\label{%s}" (save-match-data (org-solidify-link-text
2644 (match-string 1)))))
2645 (if (match-string 2) "" (match-string 1)))
2646 t t)))
d5098885 2647
0b8568f5 2648 ;; Delete @<...> constructs
0b8568f5 2649 ;; Thanks to Daniel Clemente for this regexp
4b67ea89 2650 (goto-char (point-min))
0b8568f5 2651 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
0bd48b37 2652 (org-if-unprotected
3ab2c837 2653 (replace-match ""))))
72a81656 2654
ed21c5c8 2655(defun org-export-latex-fix-inputenc ()
86fbb8ca 2656 "Set the coding system in inputenc to what the buffer is."
ed21c5c8
CD
2657 (let* ((cs buffer-file-coding-system)
2658 (opt (or (ignore-errors (latexenc-coding-system-to-inputenc cs))
2659 "utf8")))
2660 (when opt
2661 ;; Translate if that is requested
2662 (setq opt (or (cdr (assoc opt org-export-latex-inputenc-alist)) opt))
2663 ;; find the \usepackage statement and replace the option
2664 (goto-char (point-min))
2665 (while (re-search-forward "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
2666 nil t)
2667 (goto-char (match-beginning 1))
2668 (delete-region (match-beginning 1) (match-end 1))
2669 (insert opt))
2670 (and buffer-file-name
2671 (save-buffer)))))
2672
db9c3fb1
CD
2673;;; List handling:
2674
2675(defun org-export-latex-lists ()
0bd48b37 2676 "Convert plain text lists in current buffer into LaTeX lists."
3ab2c837 2677 ;; `org-list-end-re' output has changed since preprocess from
8223b1d2 2678 ;; org-exp.el. Make sure it is taken into account.
153ae947 2679 (let ((org-list-end-re "^ORG-LIST-END-MARKER\n"))
3ab2c837
BG
2680 (mapc
2681 (lambda (e)
2682 ;; For each type of context allowed for list export (E), find
2683 ;; every list, parse it, delete it and insert resulting
2684 ;; conversion to latex (RES), while keeping the same
2685 ;; `original-indentation' property.
2686 (let (res)
2687 (goto-char (point-min))
2688 (while (re-search-forward (org-item-beginning-re) nil t)
2689 (when (and (eq (get-text-property (point) 'list-context) e)
2690 (not (get-text-property (point) 'org-example)))
2691 (beginning-of-line)
2692 (setq res
2693 (org-list-to-latex
2694 ;; Narrowing is needed because we're converting
2695 ;; from inner functions to outer ones.
2696 (save-restriction
2697 (narrow-to-region (point) (point-max))
2698 (org-list-parse-list t))
2699 org-export-latex-list-parameters))
2700 ;; Extend previous value of original-indentation to the
2701 ;; whole string
2702 (insert (org-add-props res nil 'original-indentation
2703 (org-find-text-property-in-string
2704 'original-indentation res)))))))
2705 ;; List of allowed contexts for export, and the default one.
2706 (append org-list-export-context '(nil)))))
db9c3fb1 2707
db9c3fb1 2708(defconst org-latex-entities
8223b1d2
BG
2709 '("\\!"
2710 "\\'"
2711 "\\+"
2712 "\\,"
2713 "\\-"
2714 "\\:"
2715 "\\;"
2716 "\\<"
2717 "\\="
2718 "\\>"
2719 "\\Huge"
2720 "\\LARGE"
2721 "\\Large"
2722 "\\Styles"
2723 "\\\\"
2724 "\\`"
2725 "\\\""
2726 "\\addcontentsline"
2727 "\\address"
2728 "\\addtocontents"
2729 "\\addtocounter"
2730 "\\addtolength"
2731 "\\addvspace"
2732 "\\alph"
2733 "\\appendix"
2734 "\\arabic"
2735 "\\author"
2736 "\\begin{array}"
2737 "\\begin{center}"
2738 "\\begin{description}"
2739 "\\begin{enumerate}"
2740 "\\begin{eqnarray}"
2741 "\\begin{equation}"
2742 "\\begin{figure}"
2743 "\\begin{flushleft}"
2744 "\\begin{flushright}"
2745 "\\begin{itemize}"
2746 "\\begin{list}"
2747 "\\begin{minipage}"
2748 "\\begin{picture}"
2749 "\\begin{quotation}"
2750 "\\begin{quote}"
2751 "\\begin{tabbing}"
2752 "\\begin{table}"
2753 "\\begin{tabular}"
2754 "\\begin{thebibliography}"
2755 "\\begin{theorem}"
2756 "\\begin{titlepage}"
2757 "\\begin{verbatim}"
2758 "\\begin{verse}"
2759 "\\bf"
2760 "\\bf"
2761 "\\bibitem"
2762 "\\bigskip"
2763 "\\cdots"
2764 "\\centering"
2765 "\\circle"
2766 "\\cite"
2767 "\\cleardoublepage"
2768 "\\clearpage"
2769 "\\cline"
2770 "\\closing"
2771 "\\dashbox"
2772 "\\date"
2773 "\\ddots"
2774 "\\dotfill"
2775 "\\em"
2776 "\\fbox"
2777 "\\flushbottom"
2778 "\\fnsymbol"
2779 "\\footnote"
2780 "\\footnotemark"
2781 "\\footnotesize"
2782 "\\footnotetext"
2783 "\\frac"
2784 "\\frame"
2785 "\\framebox"
2786 "\\hfill"
2787 "\\hline"
2788 "\\hrulespace"
2789 "\\hspace"
2790 "\\huge"
2791 "\\hyphenation"
2792 "\\include"
2793 "\\includeonly"
2794 "\\indent"
2795 "\\input"
2796 "\\it"
2797 "\\kill"
2798 "\\label"
2799 "\\large"
2800 "\\ldots"
2801 "\\line"
2802 "\\linebreak"
2803 "\\linethickness"
2804 "\\listoffigures"
2805 "\\listoftables"
2806 "\\location"
2807 "\\makebox"
2808 "\\maketitle"
2809 "\\mark"
2810 "\\mbox"
2811 "\\medskip"
2812 "\\multicolumn"
2813 "\\multiput"
2814 "\\newcommand"
2815 "\\newcounter"
2816 "\\newenvironment"
2817 "\\newfont"
2818 "\\newlength"
2819 "\\newline"
2820 "\\newpage"
2821 "\\newsavebox"
2822 "\\newtheorem"
2823 "\\nocite"
2824 "\\nofiles"
2825 "\\noindent"
2826 "\\nolinebreak"
2827 "\\nopagebreak"
2828 "\\normalsize"
2829 "\\onecolumn"
2830 "\\opening"
2831 "\\oval"
2832 "\\overbrace"
2833 "\\overline"
2834 "\\pagebreak"
2835 "\\pagenumbering"
2836 "\\pageref"
2837 "\\pagestyle"
2838 "\\par"
2839 "\\parbox"
2840 "\\put"
2841 "\\raggedbottom"
2842 "\\raggedleft"
2843 "\\raggedright"
2844 "\\raisebox"
2845 "\\ref"
2846 "\\rm"
2847 "\\roman"
2848 "\\rule"
2849 "\\savebox"
2850 "\\sc"
2851 "\\scriptsize"
2852 "\\setcounter"
2853 "\\setlength"
2854 "\\settowidth"
2855 "\\sf"
2856 "\\shortstack"
2857 "\\signature"
2858 "\\sl"
2859 "\\small"
2860 "\\smallskip"
2861 "\\sqrt"
2862 "\\tableofcontents"
2863 "\\telephone"
2864 "\\thanks"
2865 "\\thispagestyle"
2866 "\\tiny"
2867 "\\title"
2868 "\\tt"
2869 "\\twocolumn"
2870 "\\typein"
2871 "\\typeout"
2872 "\\underbrace"
2873 "\\underline"
2874 "\\usebox"
2875 "\\usecounter"
2876 "\\value"
2877 "\\vdots"
2878 "\\vector"
2879 "\\verb"
2880 "\\vfill"
2881 "\\vline"
2882 "\\vspace")
2883 "A list of LaTeX commands to be protected when performing conversion.")
db9c3fb1 2884
71d35b24
CD
2885(defconst org-latex-entities-regexp
2886 (let (names rest)
2887 (dolist (x org-latex-entities)
8bfe682a 2888 (if (string-match "[a-zA-Z]$" x)
71d35b24
CD
2889 (push x names)
2890 (push x rest)))
2891 (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
2892 "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
2893
72a81656 2894(provide 'org-export-latex)
c8d0cf5c 2895(provide 'org-latex)
72a81656 2896
bdebdb64
BG
2897;; Local variables:
2898;; generated-autoload-file: "org-loaddefs.el"
2899;; End:
2900
c8d0cf5c 2901;;; org-latex.el ends here