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