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