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