1 ;;; org-latex.el --- LaTeX exporter for org-mode
3 ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: org-latex.el
7 ;; Author: Bastien Guerry <bzg AT gnu DOT org>
8 ;; Maintainer: Carsten Dominik <carsten.dominik AT gmail DOT com>
9 ;; Keywords: org, wp, tex
10 ;; Description: Converts an org-mode buffer into LaTeX
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; This library implements a LaTeX exporter for org-mode.
31 ;; It is part of Org and will be autoloaded
33 ;; The interactive functions are similar to those of the HTML exporter:
35 ;; M-x `org-export-as-latex'
36 ;; M-x `org-export-as-pdf'
37 ;; M-x `org-export-as-pdf-and-open'
38 ;; M-x `org-export-as-latex-batch'
39 ;; M-x `org-export-as-latex-to-buffer'
40 ;; M-x `org-export-region-as-latex'
41 ;; M-x `org-replace-region-by-latex'
55 (defvar org-export-latex-class nil
)
56 (defvar org-export-latex-class-options nil
)
57 (defvar org-export-latex-header nil
)
58 (defvar org-export-latex-append-header nil
)
59 (defvar org-export-latex-options-plist nil
)
60 (defvar org-export-latex-todo-keywords-1 nil
)
61 (defvar org-export-latex-complex-heading-re nil
)
62 (defvar org-export-latex-not-done-keywords nil
)
63 (defvar org-export-latex-done-keywords nil
)
64 (defvar org-export-latex-display-custom-times nil
)
65 (defvar org-export-latex-all-targets-re nil
)
66 (defvar org-export-latex-add-level
0)
67 (defvar org-export-latex-footmark-seen nil
68 "List of footnotes markers seen so far by exporter.")
69 (defvar org-export-latex-sectioning
"")
70 (defvar org-export-latex-sectioning-depth
0)
71 (defvar org-export-latex-special-keyword-regexp
72 (concat "\\<\\(" org-scheduled-string
"\\|"
73 org-deadline-string
"\\|"
74 org-closed-string
"\\)")
75 "Regexp matching special time planning keywords plus the time after it.")
76 (defvar org-re-quote
) ; dynamically scoped from org.el
77 (defvar org-commentsp
) ; dynamically scoped from org.el
81 (defgroup org-export-latex nil
82 "Options for exporting Org-mode files to LaTeX."
83 :tag
"Org Export LaTeX"
86 (defcustom org-export-latex-default-class
"article"
87 "The default LaTeX class."
88 :group
'org-export-latex
89 :type
'(string :tag
"LaTeX class"))
91 (defcustom org-export-latex-classes
93 "\\documentclass[11pt]{article}"
94 ("\\section{%s}" .
"\\section*{%s}")
95 ("\\subsection{%s}" .
"\\subsection*{%s}")
96 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
97 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
98 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
100 "\\documentclass[11pt]{report}"
101 ("\\part{%s}" .
"\\part*{%s}")
102 ("\\chapter{%s}" .
"\\chapter*{%s}")
103 ("\\section{%s}" .
"\\section*{%s}")
104 ("\\subsection{%s}" .
"\\subsection*{%s}")
105 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
107 "\\documentclass[11pt]{book}"
108 ("\\part{%s}" .
"\\part*{%s}")
109 ("\\chapter{%s}" .
"\\chapter*{%s}")
110 ("\\section{%s}" .
"\\section*{%s}")
111 ("\\subsection{%s}" .
"\\subsection*{%s}")
112 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
114 "\\documentclass{beamer}"
115 org-beamer-sectioning
117 "Alist of LaTeX classes and associated header and structure.
118 If #+LaTeX_CLASS is set in the buffer, use its value and the
119 associated information. Here is the structure of each cell:
123 (numbered-section . unnumbered-section\)
129 The HEADER-STRING is the header that will be inserted into the LaTeX file.
130 It should contain the \\documentclass macro, and anything else that is needed
131 for this setup. To this header, the following commands will be added:
133 - Calls to \\usepackage for all packages mentioned in the variables
134 `org-export-latex-default-packages-alist' and
135 `org-export-latex-packages-alist'. Thus, your header definitions should
136 avoid to also request these packages.
138 - Lines specified via \"#+LaTeX_HEADER:\"
140 If you need more control about the sequence in which the header is built
141 up, or if you want to exclude one of these building blocks for a particular
142 class, you can use the following macro-like placeholders.
144 [DEFAULT-PACKAGES] \\usepackage statements for default packages
145 [NO-DEFAULT-PACKAGES] do not include any of the default packages
146 [PACKAGES] \\usepackage statements for packages
147 [NO-PACKAGES] do not include the packages
148 [EXTRA] the stuff from #+LaTeX_HEADER
149 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
150 [BEAMER-HEADER-EXTRA] the beamer extra headers
154 \\documentclass{article}
155 [NO-DEFAULT-PACKAGES]
157 \\providecommand{\\alert}[1]{\\textbf{#1}}
160 will omit the default packages, and will include the #+LaTeX_HEADER lines,
161 then have a call to \\providecommand, and then place \\usepackage commands
162 based on the content of `org-export-latex-packages-alist'.
164 If your header or `org-export-latex-default-packages-alist' inserts
165 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be replaced with
166 a coding system derived from `buffer-file-coding-system'. See also the
167 variable `org-export-latex-inputenc-alist' for a way to influence this
170 The sectioning structure
171 ------------------------
173 The sectioning structure of the class is given by the elements following
174 the header string. For each sectioning level, a number of strings is
175 specified. A %s formatter is mandatory in each section string and will
176 be replaced by the title of the section.
178 Instead of a cons cell (numbered . unnumbered), you can also provide a list
181 (numbered-open numbered-close)
185 (numbered-open numbered-close unnumbered-open unnumbered-close)
187 providing opening and closing strings for a LaTeX environment that should
188 represent the document section. The opening clause should have a %s
189 to represent the section title.
191 Instead of a list of sectioning commands, you can also specify a
192 function name. That function will be called with two parameters,
193 the (reduced) level of the headline, and the headline text. The function
194 must return a cons cell with the (possibly modified) headline text, and the
195 sectioning list in the cdr."
196 :group
'org-export-latex
198 (list (string :tag
"LaTeX class")
199 (string :tag
"LaTeX header")
200 (repeat :tag
"Levels" :inline t
203 (string :tag
" numbered")
204 (string :tag
"unnumbered"))
205 (list :tag
"Environment"
206 (string :tag
"Opening (numbered)")
207 (string :tag
"Closing (numbered)")
208 (string :tag
"Opening (unnumbered)")
209 (string :tag
"Closing (unnumbered)"))
210 (function :tag
"Hook computing sectioning"))))))
212 (defcustom org-export-latex-inputenc-alist nil
213 "Alist of inputenc coding system names, and what should really be used.
214 For example, adding an entry
216 (\"utf8\" . \"utf8x\")
218 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
219 are written as utf8 files."
220 :group
'org-export-latex
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
)
233 ("=" "\\protectedtexttt" 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 format string to wrap fontified text with.
239 If it is \"\\verb\", Org will automatically select a delimiter
240 character that is not in the string. \"\\protectedtexttt\" will use \\texttt
241 to typeset and try to protect special characters.
242 The third element decides whether to protect converted text from other
244 :group
'org-export-latex
247 (defcustom org-export-latex-title-command
"\\maketitle"
248 "The command used to insert the title just after \\begin{document}.
249 If this string contains the formatting specification \"%s\" then
250 it will be used as a format string, passing the title as an
252 :group
'org-export-latex
255 (defcustom org-export-latex-import-inbuffer-stuff nil
256 "Non-nil means define TeX macros for Org's inbuffer definitions.
257 For example \orgTITLE for #+TITLE."
258 :group
'org-export-latex
261 (defcustom org-export-latex-date-format
263 "Format string for \\date{...}."
264 :group
'org-export-latex
267 (defcustom org-export-latex-todo-keyword-markup
"\\textbf{%s}"
268 "Markup for TODO keywords, as a printf format.
269 This can be a single format for all keywords, a cons cell with separate
270 formats for not-done and done states, or an association list with setup
271 for individual keywords. If a keyword shows up for which there is no
272 markup defined, the first one in the association list will be used."
273 :group
'org-export-latex
275 (string :tag
"Default")
276 (cons :tag
"Distinguish undone and done"
277 (string :tag
"Not-DONE states")
278 (string :tag
"DONE states"))
279 (repeat :tag
"Per keyword markup"
281 (string :tag
"Keyword")
282 (string :tag
"Markup")))))
284 (defcustom org-export-latex-tag-markup
"\\textbf{%s}"
285 "Markup for tags, as a printf format."
286 :group
'org-export-latex
290 (defcustom org-export-latex-timestamp-markup
"\\textit{%s}"
291 "A printf format string to be applied to time stamps."
292 :group
'org-export-latex
295 (defcustom org-export-latex-timestamp-inactive-markup
"\\textit{%s}"
296 "A printf format string to be applied to inactive time stamps."
297 :group
'org-export-latex
301 (defcustom org-export-latex-timestamp-keyword-markup
"\\texttt{%s}"
302 "A printf format string to be applied to time stamps."
303 :group
'org-export-latex
306 (defcustom org-export-latex-href-format
"\\href{%s}{%s}"
307 "A printf format string to be applied to href links.
308 The format must contain either two %s instances or just one.
309 If it contains two %s instances, the first will be filled with
310 the link, the second with the link description. If it contains
311 only one, the %s will be filled with the link."
312 :group
'org-export-latex
316 (defcustom org-export-latex-hyperref-format
"\\hyperref[%s]{%s}"
317 "A printf format string to be applied to hyperref links.
318 The format must contain one or two %s instances. The first one
319 will be filled with the link, the second with its description."
320 :group
'org-export-latex
324 (defcustom org-export-latex-hyperref-options-format
325 "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={Emacs Org-mode version %s}}\n"
326 "A format string for hyperref options.
327 When non-nil, it must contain three %s format specifications
328 which will respectively be replaced by the document's keywords,
329 its description and the Org's version number, as a string. Set
330 this option to the empty string if you don't want to include
331 hyperref options altogether."
334 :group
'org-export-latex
)
336 (defcustom org-export-latex-footnote-separator
"\\textsuperscript{,}\\,"
337 "Text used to separate footnotes."
338 :group
'org-export-latex
342 (defcustom org-export-latex-quotes
343 '(("fr" ("\\(\\s-\\|[[(]\\)\"" .
"«~") ("\\(\\S-\\)\"" .
"~»") ("\\(\\s-\\|(\\)'" .
"'"))
344 ("en" ("\\(\\s-\\|[[(]\\)\"" .
"``") ("\\(\\S-\\)\"" .
"''") ("\\(\\s-\\|(\\)'" .
"`")))
345 "Alist for quotes to use when converting english double-quotes.
347 The CAR of each item in this alist is the language code.
348 The CDR of each item in this alist is a list of three CONS:
349 - the first CONS defines the opening quote;
350 - the second CONS defines the closing quote;
351 - the last CONS defines single quotes.
353 For each item in a CONS, the first string is a regexp
354 for allowed characters before/after the quote, the second
355 string defines the replacement string for this quote."
356 :group
'org-export-latex
359 (cons :tag
"Opening quote"
360 (string :tag
"Regexp for char before")
361 (string :tag
"Replacement quote "))
362 (cons :tag
"Closing quote"
363 (string :tag
"Regexp for char after ")
364 (string :tag
"Replacement quote "))
365 (cons :tag
"Single quote"
366 (string :tag
"Regexp for char before")
367 (string :tag
"Replacement quote "))))
369 (defcustom org-export-latex-tables-verbatim nil
370 "When non-nil, tables are exported verbatim."
371 :group
'org-export-latex
374 (defcustom org-export-latex-tables-centered t
375 "When non-nil, tables are exported in a center environment."
376 :group
'org-export-latex
379 (defcustom org-export-latex-table-caption-above t
380 "When non-nil, the caption is set above the table. When nil,
381 the caption is set below the table."
382 :group
'org-export-latex
386 (defcustom org-export-latex-tables-column-borders nil
387 "When non-nil, grouping columns can cause outer vertical lines in tables.
388 When nil, grouping causes only separation lines between groups."
389 :group
'org-export-latex
392 (defcustom org-export-latex-tables-tstart nil
393 "LaTeX command for top rule for tables."
394 :group
'org-export-latex
397 (const :tag
"Nothing" nil
)
398 (string :tag
"String")
399 (const :tag
"Booktabs default: \\toprule" "\\toprule")))
401 (defcustom org-export-latex-tables-hline
"\\hline"
402 "LaTeX command to use for a rule somewhere in the middle of a table."
403 :group
'org-export-latex
406 (string :tag
"String")
407 (const :tag
"Standard: \\hline" "\\hline")
408 (const :tag
"Booktabs default: \\midrule" "\\midrule")))
410 (defcustom org-export-latex-tables-tend nil
411 "LaTeX command for bottom rule for tables."
412 :group
'org-export-latex
415 (const :tag
"Nothing" nil
)
416 (string :tag
"String")
417 (const :tag
"Booktabs default: \\bottomrule" "\\bottomrule")))
419 (defcustom org-export-latex-low-levels
'itemize
420 "How to convert sections below the current level of sectioning.
421 This is specified by the `org-export-headline-levels' option or the
422 value of \"H:\" in Org's #+OPTION line.
424 This can be either nil (skip the sections), `description', `itemize',
425 or `enumerate' (convert the sections as the corresponding list type), or
426 a string to be used instead of \\section{%s}. In this latter case,
427 the %s stands here for the inserted headline and is mandatory.
429 It may also be a list of three string to define a user-defined environment
430 that should be used. The first string should be the like
431 \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
432 to two occurrences of %s for the title and a label, respectively. The third
433 string should be like \"\\end{itemize\"."
434 :group
'org-export-latex
435 :type
'(choice (const :tag
"Ignore" nil
)
436 (const :tag
"Convert as descriptive list" description
)
437 (const :tag
"Convert as itemized list" itemize
)
438 (const :tag
"Convert as enumerated list" enumerate
)
439 (list :tag
"User-defined environment"
440 :value
("\\begin{itemize}" "\\end{itemize}" "\\item %s")
441 (string :tag
"Start")
443 (string :tag
"item"))
444 (string :tag
"Use a section string" :value
"\\subparagraph{%s}")))
446 (defcustom org-export-latex-list-parameters
447 '(:cbon
"$\\boxtimes$" :cboff
"$\\Box$" :cbtrans
"$\\boxminus$")
448 "Parameters for the LaTeX list exporter.
449 These parameters will be passed on to `org-list-to-latex', which in turn
450 will pass them (combined with the LaTeX default list parameters) to
451 `org-list-to-generic'."
452 :group
'org-export-latex
455 (defcustom org-export-latex-verbatim-wrap
456 '("\\begin{verbatim}\n" .
"\\end{verbatim}")
457 "Environment to be wrapped around a fixed-width section in LaTeX export.
458 This is a cons with two strings, to be added before and after the
461 Defaults to \\begin{verbatim} and \\end{verbatim}."
462 :group
'org-export-translation
463 :group
'org-export-latex
464 :type
'(cons (string :tag
"Open")
465 (string :tag
"Close")))
467 (defcustom org-export-latex-listings nil
468 "Non-nil means export source code using the listings package.
469 This package will fontify source code, possibly even with color.
470 If you want to use this, you also need to make LaTeX use the
471 listings package, and if you want to have color, the color
472 package. Just add these to `org-export-latex-packages-alist',
473 for example using customize, or with something like
476 (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
477 (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))
481 (setq org-export-latex-listings 'minted)
483 causes source code to be exported using the minted package as
484 opposed to listings. If you want to use minted, you need to add
485 the minted package to `org-export-latex-packages-alist', for
486 example using customize, or with
489 (add-to-list 'org-export-latex-packages-alist '(\"\" \"minted\"))
491 In addition, it is necessary to install
492 pygments (http://pygments.org), and to configure the variable
493 `org-latex-to-pdf-process' so that the -shell-escape option is
496 :group
'org-export-latex
499 (defcustom org-export-latex-listings-langs
500 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
503 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
504 (html "HTML") (xml "XML")
505 (tex "TeX") (latex "TeX")
506 (shell-script "bash")
508 (ocaml "Caml") (caml "Caml")
509 (sql "SQL") (sqlite "sql"))
510 "Alist mapping languages to their listing language counterpart.
511 The key is a symbol, the major mode symbol without the \"-mode\".
512 The value is the string that should be inserted as the language parameter
513 for the listings package. If the mode name and the listings name are
514 the same, the language does not need an entry in this list - but it does not
515 hurt if it is present."
516 :group
'org-export-latex
519 (symbol :tag
"Major mode ")
520 (string :tag
"Listings language"))))
522 (defcustom org-export-latex-listings-w-names t
523 "Non-nil means export names of named code blocks.
524 Code blocks exported with the listings package (controlled by the
525 `org-export-latex-listings' variable) can be named in the style
527 :group
'org-export-latex
531 (defcustom org-export-latex-minted-langs
532 '((emacs-lisp "common-lisp")
535 (shell-script "bash")
537 "Alist mapping languages to their minted language counterpart.
538 The key is a symbol, the major mode symbol without the \"-mode\".
539 The value is the string that should be inserted as the language parameter
540 for the minted package. If the mode name and the listings name are
541 the same, the language does not need an entry in this list - but it does not
542 hurt if it is present.
544 Note that minted uses all lower case for language identifiers,
545 and that the full list of language identifiers can be obtained
549 :group
'org-export-latex
553 (symbol :tag
"Major mode ")
554 (string :tag
"Listings language"))))
556 (defcustom org-export-latex-listings-options nil
557 "Association list of options for the latex listings package.
559 These options are supplied as a comma-separated list to the
560 \\lstset command. Each element of the association list should be
561 a list containing two strings: the name of the option, and the
564 (setq org-export-latex-listings-options
565 '((\"basicstyle\" \"\\small\")
566 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
568 will typeset the code in a small size font with underlined, bold
571 Note that the same options will be applied to blocks of all
573 :group
'org-export-latex
577 (string :tag
"Listings option name ")
578 (string :tag
"Listings option value"))))
580 (defcustom org-export-latex-minted-options nil
581 "Association list of options for the latex minted package.
583 These options are supplied within square brackets in
584 \\begin{minted} environments. Each element of the alist should be
585 a list containing two strings: the name of the option, and the
588 (setq org-export-latex-minted-options
589 '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
591 will result in src blocks being exported with
593 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
595 as the start of the minted environment. Note that the same
596 options will be applied to blocks of all languages."
597 :group
'org-export-latex
601 (string :tag
"Minted option name ")
602 (string :tag
"Minted option value"))))
604 (defvar org-export-latex-custom-lang-environments nil
605 "Association list mapping languages to language-specific latex
606 environments used during export of src blocks by the listings
607 and minted latex packages. For example,
609 (setq org-export-latex-custom-lang-environments
610 '((python \"pythoncode\")))
612 would have the effect that if org encounters begin_src python
613 during latex export it will output
619 (defcustom org-export-latex-remove-from-headlines
620 '(:todo nil
:priority nil
:tags nil
)
621 "A plist of keywords to remove from headlines. OBSOLETE.
622 Non-nil means remove this keyword type from the headline.
624 Don't remove the keys, just change their values.
626 Obsolete, this variable is no longer used. Use the separate
627 variables `org-export-with-todo-keywords', `org-export-with-priority',
628 and `org-export-with-tags' instead."
630 :group
'org-export-latex
)
632 (defcustom org-export-latex-image-default-option
"width=.9\\linewidth"
633 "Default option for images."
634 :group
'org-export-latex
637 (defcustom org-latex-default-figure-position
"htb"
638 "Default position for latex figures."
639 :group
'org-export-latex
643 (defcustom org-export-latex-tabular-environment
"tabular"
644 "Default environment used to build tables."
645 :group
'org-export-latex
649 (defcustom org-export-latex-link-with-unknown-path-format
"\\texttt{%s}"
650 "Format string for links with unknown path type."
651 :group
'org-export-latex
655 (defcustom org-export-latex-inline-images
'maybe
656 "Non-nil means inline images into exported LaTeX pages.
657 If this option is `maybe', then images in links with an empty
658 description will be inlined, while images with a description will
660 :group
'org-export-html
662 :type
'(choice (const :tag
"Never" nil
)
663 (const :tag
"Always" t
)
664 (const :tag
"When there is no description" maybe
)))
666 (defcustom org-export-latex-inline-image-extensions
667 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
668 "Extensions of image files that can be inlined into LaTeX.
669 Note that the image extension *actually* allowed depend on the way the
670 LaTeX file is processed. When used with pdflatex, pdf, jpg and png images
671 are OK. When processing through dvi to Postscript, only ps and eps are
672 allowed. The default we use here encompasses both."
673 :group
'org-export-latex
674 :type
'(repeat (string :tag
"Extension")))
676 (defcustom org-export-latex-coding-system nil
677 "Coding system for the exported LaTeX file."
678 :group
'org-export-latex
679 :type
'coding-system
)
681 (defgroup org-export-pdf nil
682 "Options for exporting Org-mode files to PDF, via LaTeX."
683 :tag
"Org Export PDF"
684 :group
'org-export-latex
687 (defcustom org-latex-to-pdf-process
688 '("pdflatex -interaction nonstopmode -output-directory %o %f"
689 "pdflatex -interaction nonstopmode -output-directory %o %f"
690 "pdflatex -interaction nonstopmode -output-directory %o %f")
691 "Commands to process a LaTeX file to a PDF file and process latex
692 fragments to pdf files.By default,this is a list of strings,and each of
693 strings will be given to the shell as a command. %f in the command will
694 be replaced by the full file name, %b by the file base name (i.e. without
695 extension) and %o by the base directory of the file.
697 If you set `org-create-formula-image-program'
698 `org-export-with-LaTeX-fragments' to 'imagemagick, you can add a
699 sublist which contains your own command(s) for LaTeX fragments
700 previewing, like this:
702 '(\"xelatex -interaction nonstopmode -output-directory %o %f\"
703 \"xelatex -interaction nonstopmode -output-directory %o %f\"
704 ;; use below command(s) to convert latex fragments
707 With no such sublist, the default command used to convert LaTeX
708 fragments will be the first string in the list.
710 The reason why this is a list is that it usually takes several runs of
711 `pdflatex', maybe mixed with a call to `bibtex'. Org does not have a clever
712 mechanism to detect which of these commands have to be run to get to a stable
713 result, and it also does not do any error checking.
715 By default, Org uses 3 runs of `pdflatex' to do the processing. If you
716 have texi2dvi on your system and if that does not cause the infamous
719 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
721 then `texi2dvi' is the superior choice. Org does offer it as one
722 of the customize options.
724 Alternatively, this may be a Lisp function that does the processing, so you
725 could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
726 This function should accept the file name as its single argument."
727 :group
'org-export-pdf
729 (repeat :tag
"Shell command sequence"
730 (string :tag
"Shell command"))
731 (const :tag
"2 runs of pdflatex"
732 ("pdflatex -interaction nonstopmode -output-directory %o %f"
733 "pdflatex -interaction nonstopmode -output-directory %o %f"))
734 (const :tag
"3 runs of pdflatex"
735 ("pdflatex -interaction nonstopmode -output-directory %o %f"
736 "pdflatex -interaction nonstopmode -output-directory %o %f"
737 "pdflatex -interaction nonstopmode -output-directory %o %f"))
738 (const :tag
"pdflatex,bibtex,pdflatex,pdflatex"
739 ("pdflatex -interaction nonstopmode -output-directory %o %f"
741 "pdflatex -interaction nonstopmode -output-directory %o %f"
742 "pdflatex -interaction nonstopmode -output-directory %o %f"))
743 (const :tag
"2 runs of xelatex"
744 ("xelatex -interaction nonstopmode -output-directory %o %f"
745 "xelatex -interaction nonstopmode -output-directory %o %f"))
746 (const :tag
"3 runs of xelatex"
747 ("xelatex -interaction nonstopmode -output-directory %o %f"
748 "xelatex -interaction nonstopmode -output-directory %o %f"
749 "xelatex -interaction nonstopmode -output-directory %o %f"))
750 (const :tag
"xelatex,bibtex,xelatex,xelatex"
751 ("xelatex -interaction nonstopmode -output-directory %o %f"
753 "xelatex -interaction nonstopmode -output-directory %o %f"
754 "xelatex -interaction nonstopmode -output-directory %o %f"))
755 (const :tag
"texi2dvi"
756 ("texi2dvi -p -b -c -V %f"))
758 ("rubber -d --into %o %f"))
761 (defcustom org-export-pdf-logfiles
762 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
763 "The list of file extensions to consider as LaTeX logfiles."
764 :group
'org-export-pdf
766 :type
'(repeat (string :tag
"Extension")))
768 (defcustom org-export-pdf-remove-logfiles t
769 "Non-nil means remove the logfiles produced by PDF production.
770 These are the .aux, .log, .out, and .toc files."
771 :group
'org-export-pdf
776 (defvar org-export-latex-after-initial-vars-hook nil
777 "Hook run before LaTeX export.
778 The exact moment is after the initial variables like org-export-latex-class
779 have been determined from the environment.")
781 (defvar org-export-latex-after-blockquotes-hook nil
782 "Hook run during LaTeX export, after blockquote, verse, center are done.")
784 (defvar org-export-latex-final-hook nil
785 "Hook run in the finalized LaTeX buffer.")
787 (defvar org-export-latex-after-save-hook nil
788 "Hook run in the finalized LaTeX buffer, after it has been saved.")
790 ;;; Autoload functions:
792 (defun org-export-as-latex-batch ()
793 "Call `org-export-as-latex', may be used in batch processing.
797 --load=$HOME/lib/emacs/org.el
798 --eval \"(setq org-export-headline-levels 2)\"
799 --visit=MyFile --funcall org-export-as-latex-batch"
800 (org-export-as-latex org-export-headline-levels
))
802 (defun org-export-as-latex-to-buffer (arg)
803 "Call `org-export-as-latex` with output to a temporary buffer.
804 No file is created. The prefix ARG is passed through to `org-export-as-latex'."
806 (org-export-as-latex arg nil
"*Org LaTeX Export*")
807 (when org-export-show-temporary-export-buffer
808 (switch-to-buffer-other-window "*Org LaTeX Export*")))
810 (defun org-replace-region-by-latex (beg end
)
811 "Replace the region from BEG to END with its LaTeX export.
812 It assumes the region has `org-mode' syntax, and then convert it to
813 LaTeX. This can be used in any buffer. For example, you could
814 write an itemized list in `org-mode' syntax in an LaTeX buffer and
815 then use this command to convert it."
818 (save-window-excursion
819 (if (derived-mode-p 'org-mode
)
820 (setq latex
(org-export-region-as-latex
822 (setq reg
(buffer-substring beg end
)
823 buf
(get-buffer-create "*Org tmp*"))
824 (with-current-buffer buf
828 (setq latex
(org-export-region-as-latex
829 (point-min) (point-max) t
'string
)))
831 (delete-region beg end
)
834 (defun org-export-region-as-latex (beg end
&optional body-only buffer
)
835 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
836 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
837 contents, and only produce the region of converted text, useful for
838 cut-and-paste operations.
839 If BUFFER is a buffer or a string, use/create that buffer as a target
840 of the converted LaTeX. If BUFFER is the symbol `string', return the
841 produced LaTeX as a string and leave no buffer behind. For example,
842 a Lisp program could call this function in the following way:
844 (setq latex (org-export-region-as-latex beg end t 'string))
846 When called interactively, the output buffer is selected, and shown
847 in a window. A non-interactive call will only return the buffer."
849 (when (org-called-interactively-p 'any
)
850 (setq buffer
"*Org LaTeX Export*"))
851 (let ((transient-mark-mode t
) (zmacs-regions t
)
853 (setq ext-plist
(plist-put ext-plist
:ignore-subtree-p t
))
855 (set-mark (point)) ;; to activate the region
857 (setq rtn
(org-export-as-latex
860 (if (fboundp 'deactivate-mark
) (deactivate-mark))
861 (if (and (org-called-interactively-p 'any
) (bufferp rtn
))
862 (switch-to-buffer-other-window rtn
)
865 (defun org-export-as-latex (arg &optional ext-plist to-buffer body-only pub-dir
)
866 "Export current buffer to a LaTeX file.
867 If there is an active region, export only the region. The prefix
868 ARG specifies how many levels of the outline should become
869 headlines. The default is 3. Lower levels will be exported
870 depending on `org-export-latex-low-levels'. The default is to
871 convert them as description lists.
872 EXT-PLIST is a property list with external parameters overriding
873 org-mode's default settings, but still inferior to file-local settings.
874 When TO-BUFFER is non-nil, create a buffer with that name and export
875 to that buffer. If TO-BUFFER is the symbol `string', don't leave any
876 buffer behind and just return the resulting LaTeX as a string, with
878 When BODY-ONLY is set, don't produce the file header and footer,
879 simply return the content of \\begin{document}...\\end{document},
880 without even the \\begin{document} and \\end{document} commands.
881 When PUB-DIR is set, use this as the publishing directory."
883 (when (and (not body-only
) arg
(listp arg
)) (setq body-only t
))
884 (run-hooks 'org-export-first-hook
)
886 ;; Make sure we have a file name when we need it.
887 (when (and (not (or to-buffer body-only
))
888 (not buffer-file-name
))
889 (if (buffer-base-buffer)
890 (org-set-local 'buffer-file-name
891 (with-current-buffer (buffer-base-buffer)
893 (error "Need a file name to be able to export")))
895 (message "Exporting to LaTeX...")
897 (let ((inhibit-read-only t
))
898 (remove-text-properties (point-min) (point-max)
899 '(:org-license-to-kill nil
))))
900 (org-update-radio-target-regexp)
901 (org-export-latex-set-initial-vars ext-plist arg
)
902 (setq org-export-opt-plist org-export-latex-options-plist
903 org-export-footnotes-data
(org-footnote-all-labels 'with-defs
)
904 org-export-footnotes-seen nil
905 org-export-latex-footmark-seen nil
)
906 (org-install-letbind)
907 (run-hooks 'org-export-latex-after-initial-vars-hook
)
908 (let* ((wcf (current-window-configuration))
910 (org-export-process-option-filters org-export-latex-options-plist
))
911 (region-p (org-region-active-p))
912 (rbeg (and region-p
(region-beginning)))
913 (rend (and region-p
(region-end)))
915 (if (plist-get opt-plist
:ignore-subtree-p
)
920 (and (org-at-heading-p)
921 (>= (org-end-of-subtree t t
) rend
))))))
922 (opt-plist (setq org-export-opt-plist
924 (org-export-add-subtree-options opt-plist rbeg
)
926 ;; Make sure the variable contains the updated values.
927 (org-export-latex-options-plist (setq org-export-opt-plist opt-plist
))
928 ;; The following two are dynamically scoped into other
930 (org-current-export-dir
931 (or pub-dir
(org-export-directory :html opt-plist
)))
932 (org-current-export-file buffer-file-name
)
933 (title (or (and subtree-p
(org-export-get-title-from-subtree))
934 (plist-get opt-plist
:title
)
936 (plist-get opt-plist
:skip-before-1st-heading
))
937 (org-export-grab-title-from-buffer))
938 (and buffer-file-name
939 (file-name-sans-extension
940 (file-name-nondirectory buffer-file-name
)))
945 (file-name-as-directory
947 (org-export-directory :LaTeX org-export-latex-options-plist
)))
948 (file-name-sans-extension
950 (org-entry-get rbeg
"EXPORT_FILE_NAME" t
))
951 (file-name-nondirectory ;sans-extension
953 (error "Don't know which export file to use")))))
957 (if (equal (file-truename filename
)
958 (file-truename (or buffer-file-name
"dummy.org")))
959 (concat filename
".tex")
961 (auto-insert nil
); Avoid any auto-insert stuff for the new file
962 (TeX-master (boundp 'TeX-master
))
963 (buffer (if to-buffer
964 (if (eq to-buffer
'string
)
965 (get-buffer-create "*Org LaTeX Export*")
966 (get-buffer-create to-buffer
))
967 (find-file-noselect filename
)))
968 (odd org-odd-levels-only
)
969 (header (org-export-latex-make-header title opt-plist
))
970 (skip (cond (subtree-p nil
)
972 (t (plist-get opt-plist
:skip-before-1st-heading
))))
973 (text (plist-get opt-plist
:text
))
974 (org-export-preprocess-hook
976 `(lambda () (org-set-local 'org-complex-heading-regexp
977 ,org-export-latex-complex-heading-re
))
978 org-export-preprocess-hook
))
979 (first-lines (if skip
"" (org-export-latex-first-lines
986 (if region-p rend
))))
987 (coding-system (and (boundp 'buffer-file-coding-system
)
988 buffer-file-coding-system
))
989 (coding-system-for-write (or org-export-latex-coding-system
991 (save-buffer-coding-system (or org-export-latex-coding-system
993 (region (buffer-substring
994 (if region-p
(region-beginning) (point-min))
995 (if region-p
(region-end) (point-max))))
997 (and text
(string-match "\\S-" text
)
998 (org-export-preprocess-string
1003 :tags
(plist-get opt-plist
:tags
)
1004 :priority
(plist-get opt-plist
:priority
)
1005 :footnotes
(plist-get opt-plist
:footnotes
)
1006 :drawers
(plist-get opt-plist
:drawers
)
1007 :timestamps
(plist-get opt-plist
:timestamps
)
1008 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
1009 :tasks
(plist-get opt-plist
:tasks
)
1011 :skip-before-1st-heading skip
1014 :LaTeX-fragments nil
)))
1016 (org-export-preprocess-string
1021 :tags
(plist-get opt-plist
:tags
)
1022 :priority
(plist-get opt-plist
:priority
)
1023 :footnotes
(plist-get opt-plist
:footnotes
)
1024 :drawers
(plist-get opt-plist
:drawers
)
1025 :timestamps
(plist-get opt-plist
:timestamps
)
1026 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
1027 :tasks
(plist-get opt-plist
:tasks
)
1028 :add-text
(if (eq to-buffer
'string
) nil text
)
1029 :skip-before-1st-heading skip
1030 :select-tags
(plist-get opt-plist
:select-tags
)
1031 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
1032 :LaTeX-fragments nil
)))
1036 (org-install-letbind)
1038 (and (fboundp 'set-buffer-file-coding-system
)
1039 (set-buffer-file-coding-system coding-system-for-write
))
1041 ;; insert the header and initial document commands
1042 (unless (or (eq to-buffer
'string
) body-only
)
1045 ;; insert text found in #+TEXT
1046 (when (and text
(not (eq to-buffer
'string
)))
1047 (insert (org-export-latex-content
1048 text
'(lists tables fixed-width keywords
))
1051 ;; insert lines before the first headline
1052 (unless (or skip
(string-match "^\\*" first-lines
))
1053 (insert first-lines
))
1055 ;; export the content of headlines
1056 (org-export-latex-global
1058 (insert string-for-export
)
1059 (goto-char (point-min))
1060 (when (re-search-forward "^\\(\\*+\\) " nil t
)
1061 (let* ((asters (length (match-string 1)))
1062 (level (if odd
(- asters
2) (- asters
1))))
1063 (setq org-export-latex-add-level
1064 (if odd
(1- (/ (1+ asters
) 2)) (1- asters
)))
1065 (org-export-latex-parse-global level odd
)))))
1068 (unless body-only
(insert "\n\\end{document}"))
1070 ;; Attach description terms to the \item macro
1071 (goto-char (point-min))
1072 (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t
)
1073 (delete-region (match-beginning 1) (match-end 1)))
1075 ;; Relocate the table of contents
1076 (goto-char (point-min))
1077 (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t
)
1078 (goto-char (point-min))
1079 (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t
)
1081 (goto-char (point-min))
1082 (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t
)
1083 (replace-match "\\tableofcontents" t t
)))
1085 ;; Cleanup forced line ends in items where they are not needed
1086 (goto-char (point-min))
1087 (while (re-search-forward
1088 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin"
1090 (delete-region (match-beginning 1) (match-end 1)))
1091 (goto-char (point-min))
1092 (while (re-search-forward
1093 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*"
1095 (if (looking-at "[\n \t]+")
1096 (replace-match "\n")))
1098 ;; Ensure we have a final newline
1099 (goto-char (point-max))
1100 (or (eq (char-before) ?
\n)
1103 (run-hooks 'org-export-latex-final-hook
)
1105 (unless (eq major-mode
'latex-mode
) (latex-mode))
1107 (org-export-latex-fix-inputenc)
1108 (run-hooks 'org-export-latex-after-save-hook
)
1109 (goto-char (point-min))
1110 (or (org-export-push-to-kill-ring "LaTeX")
1111 (message "Exporting to LaTeX...done"))
1113 (if (eq to-buffer
'string
)
1114 (prog1 (buffer-substring (point-min) (point-max))
1115 (kill-buffer (current-buffer)))
1117 (set-window-configuration wcf
))))
1119 (defun org-export-as-pdf (arg &optional hidden ext-plist
1120 to-buffer body-only pub-dir
)
1121 "Export as LaTeX, then process through to PDF."
1123 (message "Exporting to PDF...")
1124 (let* ((wconfig (current-window-configuration))
1125 (lbuf (org-export-as-latex arg ext-plist to-buffer body-only pub-dir
))
1126 (file (buffer-file-name lbuf
))
1127 (base (file-name-sans-extension (buffer-file-name lbuf
)))
1128 (pdffile (concat base
".pdf"))
1129 (cmds (if (eq org-export-latex-listings
'minted
)
1130 ;; automatically add -shell-escape when needed
1131 (mapcar (lambda (cmd)
1132 (replace-regexp-in-string
1133 "pdflatex " "pdflatex -shell-escape " cmd
))
1134 org-latex-to-pdf-process
)
1135 org-latex-to-pdf-process
))
1136 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
1137 (bibtex-p (with-current-buffer lbuf
1139 (goto-char (point-min))
1140 (re-search-forward "\\\\bibliography{" nil t
))))
1141 cmd output-dir errors
)
1142 (with-current-buffer outbuf
(erase-buffer))
1143 (message (concat "Processing LaTeX file " file
"..."))
1144 (setq output-dir
(file-name-directory file
))
1145 (with-current-buffer lbuf
1147 (if (and cmds
(symbolp cmds
))
1148 (funcall cmds
(shell-quote-argument file
))
1150 (setq cmd
(pop cmds
))
1153 (while (string-match "%b" cmd
)
1154 (setq cmd
(replace-match
1156 (shell-quote-argument base
))
1158 (while (string-match "%f" cmd
)
1159 (setq cmd
(replace-match
1161 (shell-quote-argument file
))
1163 (while (string-match "%o" cmd
)
1164 (setq cmd
(replace-match
1166 (shell-quote-argument output-dir
))
1168 (shell-command cmd outbuf
)))))))
1169 (message (concat "Processing LaTeX file " file
"...done"))
1170 (setq errors
(org-export-latex-get-error outbuf
))
1171 (if (not (file-exists-p pdffile
))
1172 (error (concat "PDF file " pdffile
" was not produced"
1173 (if errors
(concat ":" errors
"") "")))
1174 (set-window-configuration wconfig
)
1175 (when org-export-pdf-remove-logfiles
1176 (dolist (ext org-export-pdf-logfiles
)
1177 (setq file
(concat base
"." ext
))
1178 (and (file-exists-p file
) (delete-file file
))))
1180 "Exporting to PDF...done"
1182 (concat ", with some errors:" errors
)
1186 (defun org-export-latex-get-error (buf)
1187 "Collect the kinds of errors that remain in pdflatex processing."
1188 (with-current-buffer buf
1190 (goto-char (point-max))
1191 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t
)
1192 ;; OK, we are at the location of the final run
1193 (let ((pos (point)) (errors "") (case-fold-search t
))
1194 (if (re-search-forward "Reference.*?undefined" nil t
)
1195 (setq errors
(concat errors
" [undefined reference]")))
1197 (if (re-search-forward "Citation.*?undefined" nil t
)
1198 (setq errors
(concat errors
" [undefined citation]")))
1200 (if (re-search-forward "Undefined control sequence" nil t
)
1201 (setq errors
(concat errors
" [undefined control sequence]")))
1202 (and (org-string-nw-p errors
) errors
))))))
1204 (defun org-export-as-pdf-and-open (arg)
1205 "Export as LaTeX, then process through to PDF, and open."
1207 (let ((pdffile (org-export-as-pdf arg
)))
1210 (org-open-file pdffile
)
1211 (when org-export-kill-product-buffer-when-displayed
1212 (kill-buffer (find-buffer-visiting
1213 (concat (file-name-sans-extension (buffer-file-name))
1215 (error "PDF file was not produced"))))
1217 ;;; Parsing functions:
1219 (defun org-export-latex-parse-global (level odd
)
1220 "Parse the current buffer recursively, starting at LEVEL.
1221 If ODD is non-nil, assume the buffer only contains odd sections.
1222 Return a list reflecting the document structure."
1224 (goto-char (point-min))
1225 (let* ((cnt 0) output
1226 (depth org-export-latex-sectioning-depth
))
1227 (while (org-re-search-forward-unprotected
1228 (concat "^\\(\\(?:\\*\\)\\{"
1229 (number-to-string (+ (if odd
2 1) level
))
1231 ;; make sure that there is no upper heading
1235 (org-re-search-forward-unprotected
1236 (concat "^\\(\\(?:\\*\\)\\{"
1237 (number-to-string level
)
1238 "\\}\\) \\(.*\\)$") nil t
)))) t
)
1240 (let* ((pos (match-beginning 0))
1241 (heading (match-string 2))
1242 (nlevel (if odd
(/ (+ 3 level
) 2) (1+ level
))))
1247 (if (org-re-search-forward-unprotected
1248 (concat "^\\(\\(?:\\*\\)\\{"
1249 (number-to-string (+ (if odd
2 1) level
))
1250 "\\}\\) \\(.*\\)$") nil t
)
1253 (goto-char (point-min))
1261 `(heading .
,heading
)
1262 `(content .
,(org-export-latex-parse-content))
1263 `(subcontent .
,(org-export-latex-parse-subcontent
1268 (defun org-export-latex-parse-content ()
1269 "Extract the content of a section."
1271 (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t
)
1272 (progn (beginning-of-line) (point))
1274 (buffer-substring beg end
)))
1276 (defun org-export-latex-parse-subcontent (level odd
)
1277 "Extract the subcontent of a section at LEVEL.
1278 If ODD Is non-nil, assume subcontent only contains odd sections."
1279 (if (not (org-re-search-forward-unprotected
1280 (concat "^\\(\\(?:\\*\\)\\{"
1281 (number-to-string (+ (if odd
4 2) level
))
1284 nil
; subcontent is nil
1285 (org-export-latex-parse-global (+ (if odd
2 1) level
) odd
)))
1287 ;;; Rendering functions:
1288 (defun org-export-latex-global (content)
1289 "Export CONTENT to LaTeX.
1290 CONTENT is an element of the list produced by
1291 `org-export-latex-parse-global'."
1292 (if (eq (car content
) 'subcontent
)
1293 (mapc 'org-export-latex-sub
(cdr content
))
1294 (org-export-latex-sub (car content
))))
1296 (defun org-export-latex-sub (subcontent)
1297 "Export the list SUBCONTENT to LaTeX.
1298 SUBCONTENT is an alist containing information about the headline
1300 (let ((num (plist-get org-export-latex-options-plist
:section-numbers
)))
1301 (mapc (lambda(x) (org-export-latex-subcontent x num
)) subcontent
)))
1303 (defun org-export-latex-subcontent (subcontent num
)
1304 "Export each cell of SUBCONTENT to LaTeX.
1305 If NUM is non-nil export numbered sections, otherwise use unnumbered
1306 sections. If NUM is an integer, export the highest NUM levels as
1307 numbered sections and lower levels as unnumbered sections."
1308 (let* ((heading (cdr (assoc 'heading subcontent
)))
1309 (level (- (cdr (assoc 'level subcontent
))
1310 org-export-latex-add-level
))
1311 (occur (number-to-string (cdr (assoc 'occur subcontent
))))
1312 (content (cdr (assoc 'content subcontent
)))
1313 (subcontent (cadr (assoc 'subcontent subcontent
)))
1314 (label (org-get-text-property-any 0 'target heading
))
1315 (label-list (cons label
(cdr (assoc label
1316 org-export-target-aliases
))))
1317 (sectioning org-export-latex-sectioning
)
1318 (depth org-export-latex-sectioning-depth
)
1319 main-heading sub-heading ctnt
)
1320 (when (symbolp (car sectioning
))
1321 (setq sectioning
(funcall (car sectioning
) level heading
))
1323 (setq heading
(car sectioning
)
1324 sectioning
(cdr sectioning
)
1325 ;; target property migh have changed...
1326 label
(org-get-text-property-any 0 'target heading
)
1327 label-list
(cons label
(cdr (assoc label
1328 org-export-target-aliases
)))))
1329 (if sectioning
(setq sectioning
(make-list 10 sectioning
)))
1330 (setq depth
(if sectioning
10000 0)))
1331 (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading
)
1332 (setq main-heading
(substring heading
0 (match-beginning 0))
1333 sub-heading
(substring heading
(match-end 0))))
1334 (setq heading
(org-export-latex-fontify-headline heading
)
1335 sub-heading
(and sub-heading
1336 (org-export-latex-fontify-headline sub-heading
))
1337 main-heading
(and main-heading
1338 (org-export-latex-fontify-headline main-heading
)))
1340 ;; Normal conversion
1342 (let* ((sec (nth (1- level
) sectioning
))
1343 (num (if (integerp num
)
1347 (if (consp (cdr sec
))
1348 (setq start
(nth (if num
0 2) sec
)
1349 end
(nth (if num
1 3) sec
))
1350 (setq start
(if num
(car sec
) (cdr sec
))))
1351 (insert (format start
(if main-heading main-heading heading
)
1352 (or sub-heading
"")))
1355 (insert (mapconcat (lambda (l) (format "\\label{%s}" l
))
1356 label-list
"\n") "\n"))
1357 (insert (org-export-latex-content content
))
1358 (cond ((stringp subcontent
) (insert subcontent
))
1360 (while (org-looking-back "\n\n") (backward-delete-char 1))
1361 (org-export-latex-sub subcontent
)))
1362 (when (and end
(string-match "[^ \t]" end
))
1363 (let ((hook (org-get-text-property-any 0 'org-insert-hook end
)))
1364 (and (functionp hook
) (funcall hook
)))
1365 (insert end
"\n"))))
1366 ;; At a level under the hl option: we can drop this subsection
1368 (cond ((eq org-export-latex-low-levels
'description
)
1369 (if (string-match "% ends low level$"
1370 (buffer-substring (point-at-bol 0) (point)))
1371 (delete-region (point-at-bol 0) (point))
1372 (insert "\\begin{description}\n"))
1373 (insert (format "\n\\item[%s]%s~\n"
1375 (if label
(format "\\label{%s}" label
) "")))
1376 (insert (org-export-latex-content content
))
1377 (cond ((stringp subcontent
) (insert subcontent
))
1378 ((listp subcontent
) (org-export-latex-sub subcontent
)))
1379 (insert "\\end{description} % ends low level\n"))
1380 ((memq org-export-latex-low-levels
'(itemize enumerate
))
1381 (if (string-match "% ends low level$"
1382 (buffer-substring (point-at-bol 0) (point)))
1383 (delete-region (point-at-bol 0) (point))
1384 (insert (format "\\begin{%s}\n"
1385 (symbol-name org-export-latex-low-levels
))))
1386 (let ((ctnt (org-export-latex-content content
)))
1387 (insert (format (if (not (equal (replace-regexp-in-string "\n" "" ctnt
) ""))
1388 "\n\\item %s\\\\\n%s%%"
1389 "\n\\item %s\n%s%%")
1391 (if label
(format "\\label{%s}" label
) "")))
1393 (cond ((stringp subcontent
) (insert subcontent
))
1394 ((listp subcontent
) (org-export-latex-sub subcontent
)))
1395 (insert (format "\\end{%s} %% ends low level\n"
1396 (symbol-name org-export-latex-low-levels
))))
1398 ((and (listp org-export-latex-low-levels
)
1399 org-export-latex-low-levels
)
1400 (if (string-match "% ends low level$"
1401 (buffer-substring (point-at-bol 0) (point)))
1402 (delete-region (point-at-bol 0) (point))
1403 (insert (car org-export-latex-low-levels
) "\n"))
1404 (insert (format (nth 2 org-export-latex-low-levels
)
1406 (if label
(format "\\label{%s}" label
) "")))
1407 (insert (org-export-latex-content content
))
1408 (cond ((stringp subcontent
) (insert subcontent
))
1409 ((listp subcontent
) (org-export-latex-sub subcontent
)))
1410 (insert (nth 1 org-export-latex-low-levels
)
1411 " %% ends low level\n"))
1413 ((stringp org-export-latex-low-levels
)
1414 (insert (format org-export-latex-low-levels heading
) "\n")
1415 (when label
(insert (format "\\label{%s}\n" label
)))
1416 (insert (org-export-latex-content content
))
1417 (cond ((stringp subcontent
) (insert subcontent
))
1418 ((listp subcontent
) (org-export-latex-sub subcontent
)))))))))
1420 ;;; Exporting internals:
1421 (defun org-export-latex-set-initial-vars (ext-plist level
)
1422 "Store org local variables required for LaTeX export.
1423 EXT-PLIST is an optional additional plist.
1424 LEVEL indicates the default depth for export."
1425 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
1426 org-export-latex-done-keywords org-done-keywords
1427 org-export-latex-not-done-keywords org-not-done-keywords
1428 org-export-latex-complex-heading-re org-complex-heading-regexp
1429 org-export-latex-display-custom-times org-display-custom-times
1430 org-export-latex-all-targets-re
1431 (org-make-target-link-regexp (org-all-targets))
1432 org-export-latex-options-plist
1433 (org-combine-plists (org-default-export-plist) ext-plist
1434 (org-infile-export-plist))
1435 org-export-latex-class
1436 (or (and (org-region-active-p)
1438 (goto-char (region-beginning))
1439 (and (looking-at org-complex-heading-regexp
)
1440 (org-entry-get nil
"LaTeX_CLASS" 'selective
))))
1444 (goto-char (point-min))
1445 (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([-/a-zA-Z]+\\)" nil t
)
1447 (plist-get org-export-latex-options-plist
:latex-class
)
1448 org-export-latex-default-class
)
1449 org-export-latex-class-options
1450 (or (and (org-region-active-p)
1452 (goto-char (region-beginning))
1453 (and (looking-at org-complex-heading-regexp
)
1454 (org-entry-get nil
"LaTeX_CLASS_OPTIONS" 'selective
))))
1458 (goto-char (point-min))
1459 (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t
)
1461 (plist-get org-export-latex-options-plist
:latex-class-options
))
1462 org-export-latex-class
1463 (or (car (assoc org-export-latex-class org-export-latex-classes
))
1464 (error "No definition for class `%s' in `org-export-latex-classes'"
1465 org-export-latex-class
))
1466 org-export-latex-header
1467 (cadr (assoc org-export-latex-class org-export-latex-classes
))
1468 org-export-latex-sectioning
1469 (cddr (assoc org-export-latex-class org-export-latex-classes
))
1470 org-export-latex-sectioning-depth
1473 (plist-get org-export-latex-options-plist
:headline-levels
))
1474 (sec-depth (length org-export-latex-sectioning
)))
1475 (if (> hl-levels sec-depth
) sec-depth hl-levels
))))
1476 (when (and org-export-latex-class-options
1477 (string-match "\\S-" org-export-latex-class-options
)
1478 (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?"
1479 org-export-latex-header
))
1480 (setq org-export-latex-header
1481 (concat (substring org-export-latex-header
0 (match-end 1))
1482 org-export-latex-class-options
1483 (substring org-export-latex-header
(match-end 0))))))
1485 (defvar org-export-latex-format-toc-function
1486 'org-export-latex-format-toc-default
1487 "The function formatting returning the string to create the table of contents.
1488 The function mus take one parameter, the depth of the table of contents.")
1490 (defun org-export-latex-make-header (title opt-plist
)
1491 "Make the LaTeX header and return it as a string.
1492 TITLE is the current title from the buffer or region.
1493 OPT-PLIST is the options plist for current buffer."
1494 (let ((toc (plist-get opt-plist
:table-of-contents
))
1495 (author (org-export-apply-macros-in-string
1496 (plist-get opt-plist
:author
)))
1497 (email (replace-regexp-in-string
1499 (org-export-apply-macros-in-string
1500 (plist-get opt-plist
:email
))))
1501 (description (org-export-apply-macros-in-string
1502 (plist-get opt-plist
:description
)))
1503 (keywords (org-export-apply-macros-in-string
1504 (plist-get opt-plist
:keywords
))))
1506 (if (plist-get opt-plist
:time-stamp-file
)
1507 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1508 ;; insert LaTeX custom header and packages from the list
1509 (org-splice-latex-header
1510 (org-export-apply-macros-in-string org-export-latex-header
)
1511 org-export-latex-default-packages-alist
1512 org-export-latex-packages-alist nil
1513 (org-export-apply-macros-in-string
1514 (plist-get opt-plist
:latex-header-extra
)))
1515 ;; append another special variable
1516 (org-export-apply-macros-in-string org-export-latex-append-header
)
1517 ;; define alert if not yet defined
1518 "\n\\providecommand{\\alert}[1]{\\textbf{#1}}"
1522 (org-export-latex-fontify-headline title
))
1523 ;; insert author info
1524 (if (plist-get opt-plist
:author-info
)
1525 (format "\\author{%s%s}\n"
1526 (org-export-latex-fontify-headline (or author user-full-name
))
1527 (if (and (plist-get opt-plist
:email-info
) email
1528 (string-match "\\S-" email
))
1529 (format "\\thanks{%s}" email
)
1531 (format "%%\\author{%s}\n"
1532 (org-export-latex-fontify-headline (or author user-full-name
))))
1534 (format "\\date{%s}\n"
1536 (or (plist-get opt-plist
:date
)
1537 org-export-latex-date-format
)))
1538 ;; add some hyperref options
1539 (format org-export-latex-hyperref-options-format
1540 (org-export-latex-fontify-headline keywords
)
1541 (org-export-latex-fontify-headline description
)
1543 ;; beginning of the document
1544 "\n\\begin{document}\n\n"
1545 ;; insert the title command
1546 (when (string-match "\\S-" title
)
1547 (if (string-match "%s" org-export-latex-title-command
)
1548 (format org-export-latex-title-command title
)
1549 org-export-latex-title-command
))
1551 ;; table of contents
1552 (when (and org-export-with-toc
1553 (plist-get opt-plist
:section-numbers
))
1554 (funcall org-export-latex-format-toc-function
1555 (cond ((numberp toc
)
1556 (min toc
(plist-get opt-plist
:headline-levels
)))
1557 (toc (plist-get opt-plist
:headline-levels
))))))))
1559 (defun org-export-latex-format-toc-default (depth)
1561 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1564 (defun org-export-latex-first-lines (opt-plist &optional beg end
)
1565 "Export the first lines before first headline.
1566 If BEG is non-nil, it is the beginning of the region.
1567 If END is non-nil, it is the end of the region."
1569 (goto-char (or beg
(point-min)))
1571 (end (if (re-search-forward
1572 (concat "^" (org-get-limited-outline-regexp)) end t
)
1573 (goto-char (match-beginning 0))
1574 (goto-char (or end
(point-max))))))
1576 (org-export-latex-content
1577 (org-export-preprocess-string
1578 (buffer-substring pt end
)
1583 :skip-before-1st-heading nil
1584 :LaTeX-fragments nil
1585 :timestamps
(plist-get opt-plist
:timestamps
)
1586 :footnotes
(plist-get opt-plist
:footnotes
)))
1588 (let ((inhibit-read-only t
)
1589 (limit (max pt
(1- end
))))
1590 (add-text-properties pt limit
1591 '(:org-license-to-kill t
))
1594 (while (re-search-forward "^[ \t]*#\\+.*\n?" limit t
)
1595 (let ((case-fold-search t
))
1596 (unless (org-string-match-p
1597 "^[ \t]*#\\+\\(attr_\\|caption\\>\\|label\\>\\)"
1599 (remove-text-properties (match-beginning 0) (match-end 0)
1600 '(:org-license-to-kill t
))))))))))))
1603 (defvar org-export-latex-header-defs nil
1604 "The header definitions that might be used in the LaTeX body.")
1606 (defun org-export-latex-content (content &optional exclude-list
)
1607 "Convert CONTENT string to LaTeX.
1608 Don't perform conversions that are in EXCLUDE-LIST. Recognized
1609 conversion types are: quotation-marks, emphasis, sub-superscript,
1610 links, keywords, lists, tables, fixed-width"
1612 (org-install-letbind)
1614 (unless (memq 'timestamps exclude-list
)
1615 (org-export-latex-time-stamps))
1616 (unless (memq 'quotation-marks exclude-list
)
1617 (org-export-latex-quotation-marks))
1618 (unless (memq 'emphasis exclude-list
)
1619 (when (plist-get org-export-latex-options-plist
:emphasize
)
1620 (org-export-latex-fontify)))
1621 (unless (memq 'sub-superscript exclude-list
)
1622 (org-export-latex-special-chars
1623 (plist-get org-export-latex-options-plist
:sub-superscript
)))
1624 (unless (memq 'links exclude-list
)
1625 (org-export-latex-links))
1626 (unless (memq 'keywords exclude-list
)
1627 (org-export-latex-keywords))
1628 (unless (memq 'lists exclude-list
)
1629 (org-export-latex-lists))
1630 (unless (memq 'tables exclude-list
)
1631 (org-export-latex-tables
1632 (plist-get org-export-latex-options-plist
:tables
)))
1633 (unless (memq 'fixed-width exclude-list
)
1634 (org-export-latex-fixed-width
1635 (plist-get org-export-latex-options-plist
:fixed-width
)))
1637 (buffer-substring (point-min) (point-max))))
1639 (defun org-export-latex-protect-string (s)
1640 "Add the org-protected property to string S."
1641 (add-text-properties 0 (length s
) '(org-protected t
) s
) s
)
1643 (defun org-export-latex-protect-char-in-string (char-list string
)
1644 "Add org-protected text-property to char from CHAR-LIST in STRING."
1648 (goto-char (point-min))
1649 (while (re-search-forward (regexp-opt char-list
) nil t
)
1650 (add-text-properties (match-beginning 0)
1651 (match-end 0) '(org-protected t
)))
1654 (defun org-export-latex-keywords-maybe (&optional remove-list
)
1655 "Maybe remove keywords depending on rules in REMOVE-LIST."
1656 (goto-char (point-min))
1657 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1
"\\|"))
1658 (case-fold-search nil
)
1659 (todo-markup org-export-latex-todo-keyword-markup
)
1661 ;; convert TODO keywords
1662 (when (re-search-forward (concat "^\\(" re-todo
"\\)") nil t
)
1663 (if (plist-get remove-list
:todo
)
1666 ((stringp todo-markup
) todo-markup
)
1667 ((and (consp todo-markup
) (stringp (car todo-markup
)))
1668 (if (member (match-string 1) org-export-latex-done-keywords
)
1669 (cdr todo-markup
) (car todo-markup
)))
1670 (t (cdr (or (assoc (match-string 1) todo-markup
)
1671 (car todo-markup
))))))
1672 (replace-match (org-export-latex-protect-string
1673 (format fmt
(match-string 1))) t t
)))
1674 ;; convert priority string
1675 (when (re-search-forward "\\[\\\\#.\\]" nil t
)
1676 (if (plist-get remove-list
:priority
)
1678 (replace-match (format "\\textbf{%s}" (match-string 0)) t t
)))
1680 (when (re-search-forward "\\(:[a-zA-Z0-9_@#%]+\\)+:" nil t
)
1681 (if (or (not org-export-with-tags
)
1682 (plist-get remove-list
:tags
))
1685 (org-export-latex-protect-string
1686 (format org-export-latex-tag-markup
1688 (replace-regexp-in-string
1689 "\\([_#]\\)" "\\\\\\1" (match-string 0)))))
1692 (defun org-export-latex-fontify-headline (string)
1693 "Fontify special words in STRING."
1695 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
1696 ;; the beginning of the buffer - inserting "\n" is safe here though.
1697 (insert "\n" string
)
1699 ;; Preserve math snippets
1701 (let* ((matchers (plist-get org-format-latex-options
:matchers
))
1702 (re-list org-latex-regexps
)
1703 beg end re e m n block off
)
1704 ;; Check the different regular expressions
1705 (while (setq e
(pop re-list
))
1706 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
1707 block
(if (nth 3 e
) "\n\n" ""))
1708 (setq off
(if (member m
'("$" "$1")) 1 0))
1709 (when (and (member m matchers
) (not (equal m
"begin")))
1710 (goto-char (point-min))
1711 (while (re-search-forward re nil t
)
1712 (setq beg
(+ (match-beginning 0) off
) end
(- (match-end 0) 0))
1713 (add-text-properties beg end
1714 '(org-protected t org-latex-math t
))))))
1716 ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{}
1717 (goto-char (point-min))
1718 (let ((case-fold-search nil
))
1719 (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t
)
1720 (unless (eq (char-before (match-beginning 1)) ?
\\)
1721 (org-if-unprotected-1
1722 (replace-match (org-export-latex-protect-string
1723 (concat "\\" (match-string 1)
1725 (goto-char (point-min))
1726 (let ((re (concat "\\\\\\([a-zA-Z]+\\)"
1727 "\\(?:<[^<>\n]*>\\)*"
1728 "\\(?:\\[[^][\n]*?\\]\\)*"
1729 "\\(?:<[^<>\n]*>\\)*"
1731 (org-create-multibrace-regexp "{" "}" 3)
1733 (while (re-search-forward re nil t
)
1735 ;; check for comment line
1736 (save-excursion (goto-char (match-beginning 0))
1737 (org-in-indented-comment-line))
1738 ;; Check if this is a defined entity, so that is may need conversion
1739 (org-entity-get (match-string 1)))
1740 (add-text-properties (match-beginning 0) (match-end 0)
1741 '(org-protected t
)))))
1742 (when (plist-get org-export-latex-options-plist
:emphasize
)
1743 (org-export-latex-fontify))
1744 (org-export-latex-time-stamps)
1745 (org-export-latex-quotation-marks)
1746 (org-export-latex-keywords-maybe)
1747 (org-export-latex-special-chars
1748 (plist-get org-export-latex-options-plist
:sub-superscript
))
1749 (org-export-latex-links)
1750 (org-trim (buffer-string))))
1752 (defun org-export-latex-time-stamps ()
1753 "Format time stamps."
1754 (goto-char (point-min))
1755 (let ((org-display-custom-times org-export-latex-display-custom-times
))
1756 (while (re-search-forward org-ts-regexp-both nil t
)
1757 (org-if-unprotected-at (1- (point))
1759 (org-export-latex-protect-string
1760 (format (if (string= "<" (substring (match-string 0) 0 1))
1761 org-export-latex-timestamp-markup
1762 org-export-latex-timestamp-inactive-markup
)
1763 (substring (org-translate-time (match-string 0)) 1 -
1)))
1766 (defun org-export-latex-quotation-marks ()
1767 "Export quotation marks depending on language conventions."
1769 (goto-char (point-min))
1770 (while (re-search-forward (car l
) nil t
)
1771 (let ((rpl (concat (match-string 1)
1772 (org-export-latex-protect-string
1773 (copy-sequence (cdr l
))))))
1774 (org-if-unprotected-1
1775 (replace-match rpl t t
)))))
1776 (cdr (or (assoc (plist-get org-export-latex-options-plist
:language
)
1777 org-export-latex-quotes
)
1778 ;; falls back on english
1779 (assoc "en" org-export-latex-quotes
)))))
1781 (defun org-export-latex-special-chars (sub-superscript)
1782 "Export special characters to LaTeX.
1783 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
1784 See the `org-export-latex.el' code for a complete conversion table."
1785 (goto-char (point-min))
1787 (goto-char (point-min))
1788 (while (re-search-forward c nil t
)
1789 ;; Put the point where to check for org-protected
1790 (unless (get-text-property (match-beginning 2) 'org-protected
)
1791 (cond ((member (match-string 2) '("\\$" "$"))
1792 (if (equal (match-string 2) "\\$")
1794 (replace-match "\\$" t t
)))
1795 ((member (match-string 2) '("&" "%" "#"))
1796 (if (equal (match-string 1) "\\")
1797 (replace-match (match-string 2) t t
)
1798 (replace-match (concat (match-string 1) "\\"
1799 (match-string 2)) t t
)
1801 ((equal (match-string 2) "...")
1803 (concat (match-string 1)
1804 (org-export-latex-protect-string "\\ldots{}")) t t
))
1805 ((equal (match-string 2) "~")
1806 (cond ((equal (match-string 1) "\\") nil
)
1807 ((eq 'org-link
(get-text-property 0 'face
(match-string 2)))
1808 (replace-match (concat (match-string 1) "\\~") t t
))
1810 (org-export-latex-protect-string
1811 (concat (match-string 1) "\\~{}")) t t
))))
1812 ((member (match-string 2) '("{" "}"))
1813 (unless (save-match-data (org-inside-latex-math-p))
1814 (if (equal (match-string 1) "\\")
1815 (replace-match (match-string 2) t t
)
1816 (replace-match (concat (match-string 1) "\\"
1817 (match-string 2)) t t
)))))
1818 (unless (save-match-data (or (org-inside-latex-math-p) (org-at-table-p)))
1819 (cond ((equal (match-string 2) "\\")
1820 (replace-match (or (save-match-data
1821 (org-export-latex-treat-backslash-char
1823 (or (match-string 3) "")))
1825 (when (and (get-text-property (1- (point)) 'org-entity
)
1827 ;; OK, this was an entity replacement, and the user
1828 ;; had terminated the entity with {}. Make sure
1829 ;; {} is protected as well, and remove the extra {}
1830 ;; inserted by the conversion.
1831 (put-text-property (point) (+ 2 (point)) 'org-protected t
)
1832 (if (save-excursion (goto-char (max (- (point) 2) (point-min)))
1837 ((member (match-string 2) '("_" "^"))
1838 (replace-match (or (save-match-data
1839 (org-export-latex-treat-sub-super-char
1843 (match-string 3))) "") t t
)
1844 (backward-char 1)))))))
1845 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
1846 "\\(\\(\\\\?\\$\\)\\)"
1847 "\\([a-zA-Z0-9()]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-zA-Z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-zA-Z0-9]+}\\|([a-zA-Z0-9]+)\\)"
1848 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|\\([&#%{}\"]\\|[a-zA-Z][a-zA-Z0-9]*\\)\\)"
1849 "\\(^\\|.\\)\\([&#%{}~]\\|\\.\\.\\.\\)"
1850 ;; (?\< . "\\textless{}")
1851 ;; (?\> . "\\textgreater{}")
1854 (defun org-inside-latex-math-p ()
1855 (get-text-property (point) 'org-latex-math
))
1857 (defun org-export-latex-treat-sub-super-char
1858 (subsup char string-before string-after
)
1859 "Convert the \"_\" and \"^\" characters to LaTeX.
1860 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
1861 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
1862 (cond ((equal string-before
"\\")
1863 (concat string-before char string-after
))
1864 ((and (string-match "\\S-+" string-after
))
1865 ;; this is part of a math formula
1866 (cond ((eq 'org-link
(get-text-property 0 'face char
))
1867 (concat string-before
"\\" char string-after
))
1868 ((save-match-data (org-inside-latex-math-p))
1870 (cond ((eq 1 (length string-after
))
1871 (concat string-before char string-after
))
1872 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after
)
1873 (format "%s%s{%s}" string-before char
1874 (match-string 1 string-after
))))))
1875 ((and (> (length string-after
) 1)
1877 (and (equal subsup
'{}) (eq (string-to-char string-after
) ?\
{)))
1878 (or (string-match "[{]?\\([^}]+\\)[}]?" string-after
)
1879 (string-match "[(]?\\([^)]+\\)[)]?" string-after
)))
1881 (org-export-latex-protect-string
1882 (format "%s$%s{%s}$" string-before char
1883 (if (and (> (match-end 1) (1+ (match-beginning 1)))
1884 (not (equal (substring string-after
0 2) "{\\")))
1885 (concat "\\mathrm{" (match-string 1 string-after
) "}")
1886 (match-string 1 string-after
)))))
1887 ((eq subsup t
) (concat string-before
"$" char string-after
"$"))
1888 (t (org-export-latex-protect-string
1889 (concat string-before
"\\" char
"{}" string-after
)))))
1890 (t (org-export-latex-protect-string
1891 (concat string-before
"\\" char
"{}" string-after
)))))
1893 (defun org-export-latex-treat-backslash-char (string-before string-after
)
1894 "Convert the \"$\" special character to LaTeX.
1895 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
1896 (let ((ass (org-entity-get string-after
)))
1900 (concat string-before
1901 (org-export-latex-protect-string
1902 (concat "$" (nth 1 ass
) "$")))
1903 (concat string-before
(org-export-latex-protect-string
1906 ((and (not (string-match "^[ \n\t]" string-after
))
1907 (not (string-match "[ \t]\\'\\|^" string-before
)))
1908 ;; backslash is inside a word
1909 (concat string-before
1910 (org-export-latex-protect-string
1911 (concat "\\textbackslash{}" string-after
))))
1912 ((not (or (equal string-after
"")
1913 (string-match "^[ \t\n]" string-after
)))
1914 ;; backslash might escape a character (like \#) or a user TeX
1915 ;; macro (like \setcounter)
1916 (concat string-before
1917 (org-export-latex-protect-string (concat "\\" string-after
))))
1918 ((and (string-match "^[ \t\n]" string-after
)
1919 (string-match "[ \t\n]\\'" string-before
))
1920 ;; backslash is alone, convert it to $\backslash$
1921 (org-export-latex-protect-string
1922 (concat string-before
"\\textbackslash{}" string-after
)))
1923 (t (org-export-latex-protect-string
1924 (concat string-before
"\\textbackslash{}" string-after
))))))
1926 (defun org-export-latex-keywords ()
1927 "Convert special keywords to LaTeX."
1928 (goto-char (point-min))
1929 (while (re-search-forward org-export-latex-special-keyword-regexp nil t
)
1930 (replace-match (format org-export-latex-timestamp-keyword-markup
1931 (match-string 0)) t t
)
1933 (beginning-of-line 1)
1934 (unless (looking-at ".*\n[ \t]*\n")
1938 (defun org-export-latex-fixed-width (opt)
1939 "When OPT is non-nil convert fixed-width sections to LaTeX."
1940 (goto-char (point-min))
1941 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t
)
1942 (unless (get-text-property (point) 'org-example
)
1944 (progn (goto-char (match-beginning 0))
1945 (insert "\\begin{verbatim}\n")
1946 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1947 (replace-match (concat (match-string 1)
1948 (match-string 2)) t t
)
1950 (insert "\\end{verbatim}\n"))
1951 (progn (goto-char (match-beginning 0))
1952 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1953 (replace-match (concat "%" (match-string 1)
1954 (match-string 2)) t t
)
1955 (forward-line)))))))
1957 (defvar org-table-last-alignment
) ; defined in org-table.el
1958 (defvar org-table-last-column-widths
) ; defined in org-table.el
1959 (declare-function orgtbl-to-latex
"org-table" (table params
) t
)
1960 (defun org-export-latex-tables (insert)
1961 "Convert tables to LaTeX and INSERT it."
1962 ;; First, get the table.el tables
1963 (goto-char (point-min))
1964 (while (re-search-forward "^[ \t]*\\(\\+-[-+]*\\+\\)[ \t]*\n[ \t]*|" nil t
)
1967 (org-export-latex-convert-table.el-table
)))
1969 ;; And now the Org-mode tables
1970 (goto-char (point-min))
1971 (while (re-search-forward "^\\([ \t]*\\)|" nil t
)
1972 (org-if-unprotected-at (1- (point))
1974 (let* ((beg (org-table-begin))
1975 (end (org-table-end))
1976 (raw-table (buffer-substring beg end
))
1977 (org-table-last-alignment (copy-sequence org-table-last-alignment
))
1978 (org-table-last-column-widths (copy-sequence
1979 org-table-last-column-widths
))
1980 fnum fields line lines olines gr colgropen line-fmt align
1981 caption width shortn label attr hfmt floatp placement
1982 longtblp tblenv tabular-env
)
1983 (if org-export-latex-tables-verbatim
1984 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1985 "\\end{verbatim}\n")))
1986 (apply 'delete-region
(list beg end
))
1987 (insert (org-export-latex-protect-string tbl
)))
1989 (setq caption
(org-find-text-property-in-string
1990 'org-caption raw-table
)
1991 shortn
(org-find-text-property-in-string
1992 'org-caption-shortn raw-table
)
1993 attr
(org-find-text-property-in-string
1994 'org-attributes raw-table
)
1995 label
(org-find-text-property-in-string
1996 'org-label raw-table
)
1997 longtblp
(and attr
(stringp attr
)
1998 (string-match "\\<longtable\\>" attr
))
1999 tblenv
(if (and attr
(stringp attr
))
2000 (cond ((string-match "\\<sidewaystable\\>" attr
)
2002 ((or (string-match (regexp-quote "table*") attr
)
2003 (string-match "\\<multicolumn\\>" attr
))
2008 (if (and attr
(stringp attr
)
2009 (string-match "\\(tabular.\\)" attr
))
2010 (match-string 1 attr
)
2011 org-export-latex-tabular-environment
)
2012 width
(and attr
(stringp attr
)
2013 (string-match "\\<width=\\([^ \t\n\r]+\\)" attr
)
2014 (match-string 1 attr
))
2015 align
(and attr
(stringp attr
)
2016 (string-match "\\<align=\\([^ \t\n\r]+\\)" attr
)
2017 (match-string 1 attr
))
2018 hfmt
(and attr
(stringp attr
)
2019 (string-match "\\<hfmt=\\(\\S-+\\)" attr
)
2020 (match-string 1 attr
))
2021 floatp
(or caption label
(string= "table*" tblenv
))
2022 placement
(if (and attr
2024 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr
))
2025 (match-string 1 attr
)
2027 "[" org-latex-default-figure-position
"]")))
2028 (setq caption
(and caption
(org-export-latex-fontify-headline caption
)))
2029 (setq lines
(org-split-string raw-table
"\n"))
2030 (apply 'delete-region
(list beg end
))
2031 (when org-export-table-remove-special-lines
2032 (setq lines
(org-table-clean-before-export lines
'maybe-quoted
)))
2033 (when org-table-clean-did-remove-column
2034 (pop org-table-last-alignment
)
2035 (pop org-table-last-column-widths
))
2036 ;; make a format string to reflect alignment
2038 (while (and (not line-fmt
) (setq line
(pop olines
)))
2039 (unless (string-match "^[ \t]*|-" line
)
2040 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
2041 (setq fnum
(make-vector (length fields
) 0))
2045 (setq gr
(pop org-table-colgroup-info
))
2047 (cond ((eq gr
:start
)
2048 (prog1 (if colgropen
"|" "|")
2049 (setq colgropen t
)))
2051 (prog1 (if colgropen
"|" "|")
2052 (setq colgropen nil
)))
2054 (if (memq gr
'(:end
:startend
))
2055 (progn (setq colgropen nil
) "|")
2058 ;; fix double || in line-fmt
2059 (setq line-fmt
(replace-regexp-in-string "||" "|" line-fmt
))
2060 ;; maybe remove the first and last "|"
2061 (when (and (not org-export-latex-tables-column-borders
)
2062 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt
))
2063 (setq line-fmt
(match-string 2 line-fmt
)))
2066 (setq align
(apply 'format
2068 (mapcar (lambda (x) (if x
"r" "l"))
2069 org-table-last-alignment
)))))
2070 ;; prepare the table to send to orgtbl-to-latex
2074 (or (and (string-match "[ \t]*|-+" elem
) 'hline
)
2076 (progn (set-text-properties 0 (length elem
) nil elem
)
2077 (org-trim elem
)) "|")))
2080 (insert (org-export-latex-protect-string
2083 (concat "\\begin{longtable}{" align
"}\n")
2085 (format "\\begin{%s}%s\n" tblenv placement
)))
2086 (if (and floatp org-export-latex-table-caption-above
)
2088 "\\caption%s{%s} %s"
2089 (if shortn
(concat "[" shortn
"]") "")
2091 (if label
(format "\\label{%s}" label
) "")))
2092 (if (and longtblp caption org-export-latex-table-caption-above
)
2094 (if (and org-export-latex-tables-centered
(not longtblp
))
2095 "\\begin{center}\n")
2097 (format "\\begin{%s}%s{%s}\n"
2099 (if width
(format "{%s}" width
) "")
2103 `(:tstart
,org-export-latex-tables-tstart
2104 :tend
,org-export-latex-tables-tend
2105 :hline
,org-export-latex-tables-hline
2106 :skipheadrule
,longtblp
2108 :hlend
,(if longtblp
2112 %s\\multicolumn{%d}{r}{Continued on next page}\\
2115 org-export-latex-tables-hline
2116 org-export-latex-tables-hline
2117 (length org-table-last-alignment
))
2119 (if (not longtblp
) (format "\n\\end{%s}" tabular-env
))
2120 (if longtblp
"\n" (if org-export-latex-tables-centered
2121 "\n\\end{center}\n" "\n"))
2122 (if (and floatp
(not org-export-latex-table-caption-above
))
2124 "\\caption%s{%s} %s"
2125 (if shortn
(concat "[" shortn
"]") "")
2127 (if label
(format "\\label{%s}" label
) "")))
2130 (if floatp
(format "\\end{%s}" tblenv
)))))
2133 (defun org-export-latex-convert-table.el-table
()
2134 "Replace table.el table at point with LaTeX code."
2135 (let (tbl caption shortn label line floatp attr align rmlines
)
2136 (setq line
(buffer-substring (point-at-bol) (point-at-eol))
2137 label
(org-get-text-property-any 0 'org-label line
)
2138 caption
(org-get-text-property-any 0 'org-caption line
)
2139 shortn
(org-get-text-property-any 0 'org-caption-shortn line
)
2140 attr
(org-get-text-property-any 0 'org-attributes line
)
2141 align
(and attr
(stringp attr
)
2142 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr
)
2143 (match-string 1 attr
))
2144 rmlines
(and attr
(stringp attr
)
2145 (string-match "\\<rmlines\\>" attr
))
2146 floatp
(or label caption
))
2147 (and (get-buffer "*org-export-table*")
2148 (kill-buffer (get-buffer "*org-export-table*")))
2149 (table-generate-source 'latex
"*org-export-table*" "caption")
2150 (setq tbl
(with-current-buffer "*org-export-table*"
2152 (while (string-match "^%.*\n" tbl
)
2153 (setq tbl
(replace-match "" t t tbl
)))
2157 (setq lines
(mapcar (lambda (x)
2158 (if (string-match "^\\\\hline$" x
)
2163 (org-split-string tbl
"\n")))
2164 (setq tbl
(mapconcat 'identity
(delq nil lines
) "\n"))))
2165 (when (and align
(string-match "\\\\begin{tabular}{.*}" tbl
))
2166 (setq tbl
(replace-match (concat "\\begin{tabular}{" align
"}")
2168 (and (get-buffer "*org-export-table*")
2169 (kill-buffer (get-buffer "*org-export-table*")))
2170 (beginning-of-line 0)
2171 (while (looking-at "[ \t]*\\(|\\|\\+-\\)")
2172 (delete-region (point) (1+ (point-at-eol))))
2173 (when org-export-latex-tables-centered
2174 (setq tbl
(concat "\\begin{center}\n" tbl
"\\end{center}")))
2176 (setq tbl
(concat "\\begin{table}\n"
2177 (if (not org-export-latex-table-caption-above
) tbl
)
2178 (format "\\caption%s{%s%s}\n"
2179 (if shortn
(format "[%s]" shortn
) "")
2180 (if label
(format "\\label{%s}" label
) "")
2182 (if org-export-latex-table-caption-above tbl
)
2183 "\n\\end{table}\n")))
2184 (insert (org-export-latex-protect-string tbl
))))
2186 (defun org-export-latex-fontify ()
2187 "Convert fontification to LaTeX."
2188 (goto-char (point-min))
2189 (while (re-search-forward org-emph-re nil t
)
2190 ;; The match goes one char after the *string*, except at the end of a line
2191 (let ((emph (assoc (match-string 3)
2192 org-export-latex-emphasis-alist
))
2193 (beg (match-beginning 0))
2197 (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
2199 (unless (or (and (get-text-property (- (point) 2) 'org-protected
)
2200 (not (get-text-property
2201 (- (point) 2) 'org-verbatim-emph
)))
2202 (equal (char-after (match-beginning 3))
2203 (char-after (1+ (match-beginning 3))))
2205 (goto-char (match-beginning 1))
2207 (and (org-at-table-p)
2209 "[|\n]" (buffer-substring beg end
)))))
2210 (and (equal (match-string 3) "+")
2212 (string-match "\\`-+\\'" (match-string 4)))))
2213 (setq s
(match-string 4))
2214 (setq rpl
(concat (match-string 1)
2215 (org-export-latex-emph-format (cadr emph
)
2219 (setq rpl
(org-export-latex-protect-string rpl
))
2221 (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl
)
2223 (add-text-properties (match-beginning 1) (match-end 1)
2224 '(org-protected t
) rpl
)
2225 (add-text-properties (match-beginning 3) (match-end 3)
2226 '(org-protected t
) rpl
)))))
2227 (replace-match rpl t t
)))
2230 (defun org-export-latex-emph-format (format string
)
2231 "Format an emphasis string and handle the \\verb special case."
2232 (when (member format
'("\\verb" "\\protectedtexttt"))
2234 (if (equal format
"\\verb")
2235 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
2237 (loop for i from
0 to
(1- (length ll
)) do
2238 (if (not (string-match (regexp-quote (substring ll i
(1+ i
)))
2241 (setq format
(concat "\\verb" (substring ll i
(1+ i
))
2242 "%s" (substring ll i
(1+ i
))))
2243 (throw 'exit nil
))))))
2245 (trans '(("\\" .
"\\textbackslash{}")
2246 ("~" .
"\\textasciitilde{}")
2247 ("^" .
"\\textasciicircum{}")))
2249 (while (string-match "[\\{}$%&_#~^]" string
)
2250 (setq char
(match-string 0 string
))
2251 (if (> (match-beginning 0) 0)
2252 (setq rtn
(concat rtn
(substring string
2253 0 (match-beginning 0)))))
2254 (setq string
(substring string
(1+ (match-beginning 0))))
2255 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
2256 rtn
(concat rtn char
)))
2257 (setq string
(concat rtn string
) format
"\\texttt{%s}")
2258 (while (string-match "--" string
)
2259 (setq string
(replace-match "-{}-" t t string
)))))))
2260 (format format string
))
2262 (defun org-export-latex-links ()
2263 ;; Make sure to use the LaTeX hyperref and graphicx package
2264 ;; or send some warnings.
2265 "Convert links to LaTeX."
2266 (goto-char (point-min))
2267 (while (re-search-forward org-bracket-link-analytic-regexp
++ nil t
)
2268 (org-if-unprotected-1
2269 (goto-char (match-beginning 0))
2270 (let* ((re-radio org-export-latex-all-targets-re
)
2271 (remove (list (match-beginning 0) (match-end 0)))
2272 (raw-path (org-extract-attributes (match-string 3)))
2273 (full-raw-path (concat (match-string 1) raw-path
))
2274 (desc (match-string 5))
2275 (type (or (match-string 2)
2276 (if (or (file-name-absolute-p raw-path
)
2277 (string-match "^\\.\\.?/" raw-path
))
2279 (coderefp (equal type
"coderef"))
2280 (caption (org-find-text-property-in-string 'org-caption raw-path
))
2281 (shortn (org-find-text-property-in-string 'org-caption-shortn raw-path
))
2282 (attr (or (org-find-text-property-in-string 'org-attributes raw-path
)
2283 (plist-get org-export-latex-options-plist
:latex-image-options
)))
2284 (label (org-find-text-property-in-string 'org-label raw-path
))
2286 ;; define the path of the link
2288 ((member type
'("coderef"))
2290 ((member type
'("http" "https" "ftp"))
2291 (concat type
":" raw-path
))
2292 ((and re-radio
(string-match re-radio raw-path
))
2294 ((equal type
"mailto")
2295 (concat type
":" raw-path
))
2296 ((equal type
"file")
2297 (if (and (org-file-image-p
2298 (expand-file-name (org-link-unescape raw-path
))
2299 org-export-latex-inline-image-extensions
)
2300 (or (get-text-property 0 'org-no-description raw-path
)
2301 (equal desc full-raw-path
)))
2303 (progn (setq raw-path
(org-link-unescape raw-path
))
2304 (when (string-match "\\(.+\\)::.+" raw-path
)
2305 (setq raw-path
(match-string 1 raw-path
)))
2306 (if (file-exists-p raw-path
)
2307 (concat type
"://" (expand-file-name raw-path
))
2308 (concat type
"://" (org-export-directory
2309 :LaTeX org-export-latex-options-plist
)
2311 ;; process with link inserting
2312 (apply 'delete-region remove
)
2313 (setq caption
(and caption
(org-export-latex-fontify-headline caption
)))
2314 (cond ((and imgp
(plist-get org-export-latex-options-plist
:latex-inline-images
))
2315 ;; OK, we need to inline an image
2317 (org-export-latex-format-image raw-path caption label attr shortn
)))
2320 (org-export-get-coderef-format path desc
)
2321 (cdr (assoc path org-export-code-refs
)))))
2322 (radiop (insert (format org-export-latex-hyperref-format
2323 (org-solidify-link-text raw-path
) desc
)))
2325 (insert (format org-export-latex-hyperref-format
2326 (org-remove-initial-hash
2327 (org-solidify-link-text raw-path
))
2330 (when (org-at-table-p)
2331 ;; There is a strange problem when we have a link in a table,
2332 ;; ampersands then cause a problem. I think this must be
2333 ;; a LaTeX issue, but we here implement a work-around anyway.
2334 (setq path
(org-export-latex-protect-amp path
)
2335 desc
(org-export-latex-protect-amp desc
)))
2337 (if (string-match "%s.*%s" org-export-latex-href-format
)
2338 (format org-export-latex-href-format path desc
)
2339 (format org-export-latex-href-format path
))))
2341 ((functionp (setq fnc
(nth 2 (assoc type org-link-protocols
))))
2342 ;; The link protocol has a function for formatting the link
2345 (funcall fnc
(org-link-unescape raw-path
) desc
'latex
))))
2346 ;; Unrecognized path type
2347 (t (insert (format org-export-latex-link-with-unknown-path-format desc
))))))))
2350 (defun org-export-latex-format-image (path caption label attr
&optional shortn
)
2351 "Format the image element, depending on user settings."
2352 (let (ind floatp wrapp multicolumnp placement figenv
)
2353 (setq floatp
(or caption label
))
2354 (setq ind
(org-get-text-property-any 0 'original-indentation path
))
2355 (when (and attr
(stringp attr
))
2356 (if (string-match "[ \t]*\\<wrap\\>" attr
)
2357 (setq wrapp t floatp nil attr
(replace-match "" t t attr
)))
2358 (if (string-match "[ \t]*\\<float\\>" attr
)
2359 (setq wrapp nil floatp t attr
(replace-match "" t t attr
)))
2360 (if (string-match "[ \t]*\\<multicolumn\\>" attr
)
2361 (setq multicolumnp t attr
(replace-match "" t t attr
))))
2365 (wrapp "{l}{0.5\\textwidth}")
2366 (floatp (concat "[" org-latex-default-figure-position
"]"))
2369 (when (and attr
(stringp attr
)
2370 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr
))
2371 (setq placement
(match-string 1 attr
)
2372 attr
(replace-match "" t t attr
)))
2373 (setq attr
(and attr
(org-trim attr
)))
2374 (when (or (not attr
) (= (length attr
) 0))
2375 (setq attr
(cond (floatp "width=0.7\\textwidth")
2376 (wrapp "width=0.48\\textwidth")
2380 (wrapp "\\begin{wrapfigure}%placement
2382 \\includegraphics[%attr]{%path}
2383 \\caption%shortn{%labelcmd%caption}
2385 (multicolumnp "\\begin{figure*}%placement
2387 \\includegraphics[%attr]{%path}
2388 \\caption%shortn{%labelcmd%caption}
2390 (floatp "\\begin{figure}%placement
2392 \\includegraphics[%attr]{%path}
2393 \\caption%shortn{%labelcmd%caption}
2395 (t "\\includegraphics[%attr]{%path}")))
2398 (setq figenv
(mapconcat 'identity
(split-string figenv
"\n")
2399 (save-excursion (beginning-of-line 1)
2400 (looking-at "[ \t]*")
2401 (concat "\n" (match-string 0)))))
2403 (if (and (not label
) (not caption
)
2404 (string-match "^\\\\caption{.*\n" figenv
))
2405 (setq figenv
(replace-match "" t t figenv
)))
2410 (if (file-name-absolute-p path
)
2411 (expand-file-name path
)
2414 (cons "shortn" (if shortn
(format "[%s]" shortn
) ""))
2415 (cons "labelcmd" (if label
(format "\\label{%s}"
2417 (cons "caption" (or caption
""))
2418 (cons "placement" (or placement
""))))
2419 nil
'original-indentation ind
)))
2421 (defun org-export-latex-protect-amp (s)
2422 (while (string-match "\\([^\\\\]\\)\\(&\\)" s
)
2423 (setq s
(replace-match (concat (match-string 1 s
) "\\" (match-string 2 s
))
2427 (defun org-remove-initial-hash (s)
2428 (if (string-match "\\`#" s
)
2431 (defvar org-latex-entities
) ; defined below
2432 (defvar org-latex-entities-regexp
) ; defined below
2434 (defun org-export-latex-preprocess (parameters)
2435 "Clean stuff in the LaTeX export."
2436 ;; Replace footnotes.
2437 (when (plist-get parameters
:footnotes
)
2438 (goto-char (point-min))
2440 (while (setq ref
(org-footnote-get-next-reference))
2441 (let* ((beg (nth 1 ref
))
2443 (def (nth 1 (assoc (string-to-number lbl
)
2444 (mapcar (lambda (e) (cdr e
))
2445 org-export-footnotes-seen
)))))
2446 ;; Fix body for footnotes ending on a link or a list and
2447 ;; remove definition from buffer.
2450 (if (string-match "ORG-LIST-END-MARKER\\'" def
)
2452 (org-footnote-delete-definitions lbl
)
2453 ;; Compute string to insert (FNOTE), and protect the outside
2454 ;; macro from further transformation. When footnote at
2455 ;; point is referring to a previously defined footnote, use
2456 ;; \footnotemark. Otherwise, use \footnote.
2457 (let ((fnote (if (member lbl org-export-latex-footmark-seen
)
2458 (org-export-latex-protect-string
2459 (format "\\footnotemark[%s]" lbl
))
2460 (push lbl org-export-latex-footmark-seen
)
2461 (concat (org-export-latex-protect-string "\\footnote{")
2463 (org-export-latex-protect-string "}"))))
2464 ;; Check if another footnote is immediately following.
2465 ;; If so, add a separator in-between.
2466 (sep (org-export-latex-protect-string
2467 (if (save-excursion (goto-char (1- (nth 2 ref
)))
2468 (let ((next (org-footnote-get-next-reference)))
2469 (and next
(= (nth 1 next
) (nth 2 ref
)))))
2470 org-export-latex-footnote-separator
""))))
2471 (when (org-at-heading-p)
2472 (setq fnote
(concat (org-export-latex-protect-string "\\protect")
2474 ;; Ensure a footnote at column 0 cannot end a list
2476 (put-text-property 0 (length fnote
) 'original-indentation
1000 fnote
)
2477 ;; Replace footnote reference with FNOTE and, maybe, SEP.
2478 ;; `save-excursion' is required if there are two footnotes
2479 ;; in a row. In that case, point would be left at the
2480 ;; beginning of the second one, and
2481 ;; `org-footnote-get-next-reference' would then skip it.
2483 (delete-region beg
(nth 2 ref
))
2484 (save-excursion (insert fnote sep
)))))))
2486 ;; Remove footnote section tag for LaTeX
2487 (goto-char (point-min))
2488 (while (re-search-forward
2489 (concat "^" footnote-section-tag-regexp
) nil t
)
2491 (replace-match "")))
2492 ;; Remove any left-over footnote definition.
2493 (mapc (lambda (fn) (org-footnote-delete-definitions (car fn
)))
2494 org-export-footnotes-data
)
2495 (mapc (lambda (fn) (org-footnote-delete-definitions fn
))
2496 org-export-latex-footmark-seen
)
2498 ;; Preserve line breaks
2499 (goto-char (point-min))
2500 (while (re-search-forward "\\\\\\\\" nil t
)
2501 (add-text-properties (match-beginning 0) (match-end 0)
2502 '(org-protected t
)))
2504 ;; Preserve latex environments
2505 (goto-char (point-min))
2506 (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t
)
2508 (let* ((start (progn (beginning-of-line) (point)))
2509 (end (and (re-search-forward
2510 (concat "^[ \t]*\\\\end{"
2511 (regexp-quote (match-string 1))
2515 (add-text-properties start end
'(org-protected t
))
2516 (goto-char (point-at-eol))))))
2518 ;; Preserve math snippets
2519 (let* ((matchers (plist-get org-format-latex-options
:matchers
))
2520 (re-list org-latex-regexps
)
2521 beg end re e m n block off
)
2522 ;; Check the different regular expressions
2523 (while (setq e
(pop re-list
))
2524 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
2525 block
(if (nth 3 e
) "\n\n" ""))
2526 (setq off
(if (member m
'("$" "$1")) 1 0))
2527 (when (and (member m matchers
) (not (equal m
"begin")))
2528 (goto-char (point-min))
2529 (while (re-search-forward re nil t
)
2530 (setq beg
(+ (match-beginning 0) off
) end
(- (match-end 0) 0))
2531 (add-text-properties beg end
'(org-protected t org-latex-math t
))))))
2533 ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{}
2534 (goto-char (point-min))
2535 (let ((case-fold-search nil
))
2536 (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t
)
2537 (unless (eq (char-before (match-beginning 1)) ?
\\)
2538 (org-if-unprotected-1
2539 (replace-match (org-export-latex-protect-string
2540 (concat "\\" (match-string 1)
2543 ;; Convert blockquotes
2544 (goto-char (point-min))
2545 (while (search-forward "ORG-BLOCKQUOTE-START" nil t
)
2546 (org-replace-match-keep-properties "\\begin{quote}" t t
))
2547 (goto-char (point-min))
2548 (while (search-forward "ORG-BLOCKQUOTE-END" nil t
)
2549 (org-replace-match-keep-properties "\\end{quote}" t t
))
2552 (goto-char (point-min))
2553 (while (search-forward "ORG-VERSE-START" nil t
)
2554 (org-replace-match-keep-properties "\\begin{verse}" t t
)
2555 (beginning-of-line 2)
2556 (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
2557 (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
2558 (goto-char (match-end 1))
2559 (org-replace-match-keep-properties
2560 (org-export-latex-protect-string
2561 (concat "\\hspace*{1cm}" (match-string 2))) t t
)
2562 (beginning-of-line 1))
2563 (if (looking-at "[ \t]*$")
2564 (insert (org-export-latex-protect-string "\\vspace*{1em}"))
2565 (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
2568 (beginning-of-line 2))
2569 (and (looking-at "[ \t]*ORG-VERSE-END.*")
2570 (org-replace-match-keep-properties "\\end{verse}" t t
)))
2572 ;; Convert #+INDEX to LaTeX \\index.
2573 (goto-char (point-min))
2574 (let ((case-fold-search t
) entry
)
2575 (while (re-search-forward
2576 "^[ \t]*#\\+index:[ \t]*\\([^ \t\r\n].*?\\)[ \t]*$"
2580 (org-export-latex-protect-string
2581 (org-export-latex-fontify-headline (match-string 1)))))
2582 (replace-match (format "\\index{%s}" entry
) t t
)))
2585 (goto-char (point-min))
2586 (while (search-forward "ORG-CENTER-START" nil t
)
2587 (org-replace-match-keep-properties "\\begin{center}" t t
))
2588 (goto-char (point-min))
2589 (while (search-forward "ORG-CENTER-END" nil t
)
2590 (org-replace-match-keep-properties "\\end{center}" t t
))
2592 (run-hooks 'org-export-latex-after-blockquotes-hook
)
2594 ;; Convert horizontal rules
2595 (goto-char (point-min))
2596 (while (re-search-forward "^[ \t]*-\\{5,\\}[ \t]*$" nil t
)
2598 (replace-match (org-export-latex-protect-string "\\hrule") t t
)))
2600 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
2601 (goto-char (point-min))
2603 "\\\\\\([a-zA-Z]+\\*?\\)"
2604 "\\(?:<[^<>\n]*>\\)*"
2605 "\\(?:\\[[^][\n]*?\\]\\)*"
2606 "\\(?:<[^<>\n]*>\\)*"
2607 "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}")))
2608 (while (re-search-forward re nil t
)
2610 ;; Check for comment line.
2611 (save-excursion (goto-char (match-beginning 0))
2612 (org-in-indented-comment-line))
2613 ;; Check if this is a defined entity, so that is may
2615 (org-entity-get (match-string 1))
2616 ;; Do not protect interior of footnotes. Those have
2617 ;; already been taken care of earlier in the function.
2618 ;; Yet, keep looking inside them for more commands.
2619 (and (equal (match-string 1) "footnote")
2620 (goto-char (match-end 1))))
2621 (add-text-properties (match-beginning 0) (match-end 0)
2622 '(org-protected t
)))))
2624 ;; Special case for \nbsp
2625 (goto-char (point-min))
2626 (while (re-search-forward "\\\\nbsp\\({}\\|\\>\\)" nil t
)
2628 (replace-match (org-export-latex-protect-string "~"))))
2630 ;; Protect LaTeX entities
2631 (goto-char (point-min))
2632 (while (re-search-forward org-latex-entities-regexp nil t
)
2634 (add-text-properties (match-beginning 0) (match-end 0)
2635 '(org-protected t
))))
2637 ;; Replace radio links
2638 (goto-char (point-min))
2639 (while (re-search-forward
2640 (concat "<<<?" org-export-latex-all-targets-re
2641 ">>>?\\((INVISIBLE)\\)?") nil t
)
2642 (org-if-unprotected-at (+ (match-beginning 0) 2)
2645 (org-export-latex-protect-string
2646 (format "\\label{%s}" (save-match-data (org-solidify-link-text
2647 (match-string 1)))))
2648 (if (match-string 2) "" (match-string 1)))
2651 ;; Delete @<...> constructs
2652 ;; Thanks to Daniel Clemente for this regexp
2653 (goto-char (point-min))
2654 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t
)
2656 (replace-match ""))))
2658 (defun org-export-latex-fix-inputenc ()
2659 "Set the coding system in inputenc to what the buffer is."
2660 (let* ((cs buffer-file-coding-system
)
2661 (opt (or (ignore-errors (latexenc-coding-system-to-inputenc cs
))
2664 ;; Translate if that is requested
2665 (setq opt
(or (cdr (assoc opt org-export-latex-inputenc-alist
)) opt
))
2666 ;; find the \usepackage statement and replace the option
2667 (goto-char (point-min))
2668 (while (re-search-forward "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
2670 (goto-char (match-beginning 1))
2671 (delete-region (match-beginning 1) (match-end 1))
2673 (and buffer-file-name
2678 (defun org-export-latex-lists ()
2679 "Convert plain text lists in current buffer into LaTeX lists."
2680 ;; `org-list-end-re' output has changed since preprocess from
2681 ;; org-exp.el. Make sure it is taken into account.
2682 (let ((org-list-end-re "^ORG-LIST-END-MARKER\n"))
2685 ;; For each type of context allowed for list export (E), find
2686 ;; every list, parse it, delete it and insert resulting
2687 ;; conversion to latex (RES), while keeping the same
2688 ;; `original-indentation' property.
2690 (goto-char (point-min))
2691 (while (re-search-forward (org-item-beginning-re) nil t
)
2692 (when (and (eq (get-text-property (point) 'list-context
) e
)
2693 (not (get-text-property (point) 'org-example
)))
2697 ;; Narrowing is needed because we're converting
2698 ;; from inner functions to outer ones.
2700 (narrow-to-region (point) (point-max))
2701 (org-list-parse-list t
))
2702 org-export-latex-list-parameters
))
2703 ;; Extend previous value of original-indentation to the
2705 (insert (org-add-props res nil
'original-indentation
2706 (org-find-text-property-in-string
2707 'original-indentation res
)))))))
2708 ;; List of allowed contexts for export, and the default one.
2709 (append org-list-export-context
'(nil)))))
2711 (defconst org-latex-entities
2741 "\\begin{description}"
2742 "\\begin{enumerate}"
2746 "\\begin{flushleft}"
2747 "\\begin{flushright}"
2752 "\\begin{quotation}"
2757 "\\begin{thebibliography}"
2759 "\\begin{titlepage}"
2886 "A list of LaTeX commands to be protected when performing conversion.")
2888 (defconst org-latex-entities-regexp
2890 (dolist (x org-latex-entities
)
2891 (if (string-match "[a-zA-Z]$" x
)
2894 (concat "\\(" (regexp-opt (nreverse names
)) "\\>\\)"
2895 "\\|\\(" (regexp-opt (nreverse rest
)) "\\)")))
2897 (provide 'org-export-latex
)
2898 (provide 'org-latex
)
2901 ;; generated-autoload-file: "org-loaddefs.el"
2904 ;;; org-latex.el ends here