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