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