1 ;;; org-latex.el --- LaTeX exporter for org-mode
3 ;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: org-latex.el
8 ;; Author: Bastien Guerry <bzg AT altern DOT org>
9 ;; Maintainer: Carsten Dominik <carsten.dominik AT gmail DOT com>
10 ;; Keywords: org, wp, tex
11 ;; Description: Converts an org-mode buffer into LaTeX
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 ;; This library implements a LaTeX exporter for org-mode.
32 ;; It is part of Org and will be autoloaded
34 ;; The interactive functions are similar to those of the HTML exporter:
36 ;; M-x `org-export-as-latex'
37 ;; M-x `org-export-as-pdf'
38 ;; M-x `org-export-as-pdf-and-open'
39 ;; M-x `org-export-as-latex-batch'
40 ;; M-x `org-export-as-latex-to-buffer'
41 ;; M-x `org-export-region-as-latex'
42 ;; M-x `org-replace-region-by-latex'
56 (defvar org-export-latex-class nil
)
57 (defvar org-export-latex-class-options nil
)
58 (defvar org-export-latex-header nil
)
59 (defvar org-export-latex-append-header nil
)
60 (defvar org-export-latex-options-plist nil
)
61 (defvar org-export-latex-todo-keywords-1 nil
)
62 (defvar org-export-latex-complex-heading-re nil
)
63 (defvar org-export-latex-not-done-keywords nil
)
64 (defvar org-export-latex-done-keywords nil
)
65 (defvar org-export-latex-display-custom-times nil
)
66 (defvar org-export-latex-all-targets-re nil
)
67 (defvar org-export-latex-add-level
0)
68 (defvar org-export-latex-sectioning
"")
69 (defvar org-export-latex-sectioning-depth
0)
70 (defvar org-export-latex-special-keyword-regexp
71 (concat "\\<\\(" org-scheduled-string
"\\|"
72 org-deadline-string
"\\|"
73 org-closed-string
"\\)")
74 "Regexp matching special time planning keywords plus the time after it.")
76 (defvar latexp
) ; dynamically scoped from org.el
77 (defvar re-quote
) ; dynamically scoped from org.el
78 (defvar commentsp
) ; dynamically scoped from org.el
82 (defgroup org-export-latex nil
83 "Options for exporting Org-mode files to LaTeX."
84 :tag
"Org Export LaTeX"
87 (defcustom org-export-latex-default-class
"article"
88 "The default LaTeX class."
89 :group
'org-export-latex
90 :type
'(string :tag
"LaTeX class"))
92 (defcustom org-export-latex-classes
94 "\\documentclass[11pt]{article}
95 \\usepackage[utf8]{inputenc}
96 \\usepackage[T1]{fontenc}
97 \\usepackage{graphicx}
98 \\usepackage{longtable}
100 \\usepackage{wrapfig}
102 \\usepackage{amssymb}
103 \\usepackage{hyperref}"
104 ("\\section{%s}" .
"\\section*{%s}")
105 ("\\subsection{%s}" .
"\\subsection*{%s}")
106 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
107 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
108 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
110 "\\documentclass[11pt]{report}
111 \\usepackage[utf8]{inputenc}
112 \\usepackage[T1]{fontenc}
113 \\usepackage{graphicx}
114 \\usepackage{longtable}
116 \\usepackage{wrapfig}
118 \\usepackage{amssymb}
119 \\usepackage{hyperref}"
120 ("\\part{%s}" .
"\\part*{%s}")
121 ("\\chapter{%s}" .
"\\chapter*{%s}")
122 ("\\section{%s}" .
"\\section*{%s}")
123 ("\\subsection{%s}" .
"\\subsection*{%s}")
124 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
126 "\\documentclass[11pt]{book}
127 \\usepackage[utf8]{inputenc}
128 \\usepackage[T1]{fontenc}
129 \\usepackage{graphicx}
130 \\usepackage{longtable}
132 \\usepackage{wrapfig}
134 \\usepackage{amssymb}
135 \\usepackage{hyperref}"
136 ("\\part{%s}" .
"\\part*{%s}")
137 ("\\chapter{%s}" .
"\\chapter*{%s}")
138 ("\\section{%s}" .
"\\section*{%s}")
139 ("\\subsection{%s}" .
"\\subsection*{%s}")
140 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
142 "\\documentclass{beamer}
143 \\usepackage[utf8]{inputenc}
144 \\usepackage[T1]{fontenc}
145 \\usepackage{graphicx}
146 \\usepackage{longtable}
148 \\usepackage{wrapfig}
150 \\usepackage{amssymb}
151 \\usepackage{hyperref}"
152 org-beamer-sectioning
154 "Alist of LaTeX classes and associated header and structure.
155 If #+LaTeX_CLASS is set in the buffer, use its value and the
156 associated information. Here is the structure of each cell:
160 (numbered-section . unnumbered-section\)
163 A %s formatter is mandatory in each section string and will be
164 replaced by the title of the section.
166 Instead of a cons cell (numbered . unnumbered), you can also provide a list
169 (numbered-open numbered-close)
173 (numbered-open numbered-close unnumbered-open unnumbered-close)
175 providing opening and closing strings for an environment that should
176 represent the document section. The opening clause should have a %s
177 to represent the section title.
179 Instead of a list of sectioning commands, you can also specify a
180 function name. that function will be called with two parameters,
181 the (reduced) level of the headline, and the headline text. The functions
182 returns a cons cell with the (possibly modified) headline text, and the
183 sectioning list in the cdr."
184 :group
'org-export-latex
186 (list (string :tag
"LaTeX class")
187 (string :tag
"LaTeX header")
188 (repeat :tag
"Levels" :inline t
191 (string :tag
" numbered")
192 (string :tag
"unnumbered"))
193 (list :tag
"Environment"
194 (string :tag
"Opening (numbered)")
195 (string :tag
"Closing (numbered)")
196 (string :tag
"Opening (unnumbered)")
197 (string :tag
"Closing (unnumbered)"))
198 (function :tag
"Hook computing sectioning"))))))
200 (defcustom org-export-latex-inputenc-alist nil
201 "Alist of inputenc coding system names, and what should really be used.
202 For example, adding an entry
204 (\"utf8\" . \"utf8x\")
206 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
207 are written as utf8 files."
208 :group
'org-export-latex
211 (string :tag
"Derived from buffer")
212 (string :tag
"Use this instead"))))
215 (defcustom org-export-latex-emphasis-alist
216 '(("*" "\\textbf{%s}" nil
)
217 ("/" "\\emph{%s}" nil
)
218 ("_" "\\underline{%s}" nil
)
222 "Alist of LaTeX expressions to convert emphasis fontifiers.
223 Each element of the list is a list of three elements.
224 The first element is the character used as a marker for fontification.
225 The second element is a formatting string to wrap fontified text with.
226 If it is \"\\verb\", Org will automatically select a delimiter
227 character that is not in the string.
228 The third element decides whether to protect converted text from other
230 :group
'org-export-latex
233 (defcustom org-export-latex-title-command
"\\maketitle"
234 "The command used to insert the title just after \\begin{document}.
235 If this string contains the formatting specification \"%s\" then
236 it will be used as a formatting string, passing the title as an
238 :group
'org-export-latex
241 (defcustom org-export-latex-import-inbuffer-stuff nil
242 "Non-nil means define TeX macros for Org's inbuffer definitions.
243 For example \orgTITLE for #+TITLE."
244 :group
'org-export-latex
247 (defcustom org-export-latex-date-format
249 "Format string for \\date{...}."
250 :group
'org-export-latex
253 (defcustom org-export-latex-todo-keyword-markup
"\\textbf{%s}"
254 "Markup for TODO keywords, as a printf format.
255 This can be a single format for all keywords, a cons cell with separate
256 formats for not-done and done states, or an association list with setup
257 for individual keywords. If a keyword shows up for which there is no
258 markup defined, the first one in the association list will be used."
259 :group
'org-export-latex
261 (string :tag
"Default")
262 (cons :tag
"Distinguish undone and done"
263 (string :tag
"Not-DONE states")
264 (string :tag
"DONE states"))
265 (repeat :tag
"Per keyword markup"
267 (string :tag
"Keyword")
268 (string :tag
"Markup")))))
270 (defcustom org-export-latex-timestamp-markup
"\\textit{%s}"
271 "A printf format string to be applied to time stamps."
272 :group
'org-export-latex
275 (defcustom org-export-latex-timestamp-keyword-markup
"\\texttt{%s}"
276 "A printf format string to be applied to time stamps."
277 :group
'org-export-latex
280 (defcustom org-export-latex-hyperref-format
"\\href{%s}{%s}"
281 "A printf format string to be applied to hyperref links.
282 The format must contain two %s instances. The first will be filled with
283 the link, the second with the link description."
284 :group
'org-export-latex
287 (defcustom org-export-latex-tables-verbatim nil
288 "When non-nil, tables are exported verbatim."
289 :group
'org-export-latex
292 (defcustom org-export-latex-tables-centered t
293 "When non-nil, tables are exported in a center environment."
294 :group
'org-export-latex
297 (defcustom org-export-latex-tables-column-borders nil
298 "When non-nil, grouping columns can cause outer vertical lines in tables.
299 When nil, grouping causes only separation lines between groups."
300 :group
'org-export-latex
303 (defcustom org-export-latex-low-levels
'itemize
304 "How to convert sections below the current level of sectioning.
305 This is specified by the `org-export-headline-levels' option or the
306 value of \"H:\" in Org's #+OPTION line.
308 This can be either nil (skip the sections), `description', `itemize',
309 or `enumerate' (convert the sections as the corresponding list type), or
310 a string to be used instead of \\section{%s}. In this latter case,
311 the %s stands here for the inserted headline and is mandatory.
313 It may also be a list of three string to define a user-defined environment
314 that should be used. The first string should be the like
315 \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
316 to two occurrences of %s for the title and a label, respectively. The third
317 string should be like \"\\end{itemize\"."
318 :group
'org-export-latex
319 :type
'(choice (const :tag
"Ignore" nil
)
320 (const :tag
"Convert as descriptive list" description
)
321 (const :tag
"Convert as itemized list" itemize
)
322 (const :tag
"Convert as enumerated list" enumerate
)
323 (list :tag
"User-defined environment"
324 :value
("\\begin{itemize}" "\\end{itemize}" "\\item %s")
325 (string :tag
"Start")
327 (string :tag
"item"))
328 (string :tag
"Use a section string" :value
"\\subparagraph{%s}")))
330 (defcustom org-export-latex-list-parameters
331 '(:cbon
"$\\boxtimes$" :cboff
"$\\Box$")
332 "Parameters for the LaTeX list exporter.
333 These parameters will be passed on to `org-list-to-latex', which in turn
334 will pass them (combined with the LaTeX default list parameters) to
335 `org-list-to-generic'."
336 :group
'org-export-latex
339 (defcustom org-export-latex-verbatim-wrap
340 '("\\begin{verbatim}\n" .
"\\end{verbatim}\n")
341 "Environment to be wrapped around a fixed-width section in LaTeX export.
342 This is a cons with two strings, to be added before and after the
345 Defaults to \\begin{verbatim} and \\end{verbatim}."
346 :group
'org-export-translation
347 :group
'org-export-latex
348 :type
'(cons (string :tag
"Open")
349 (string :tag
"Close")))
351 (defcustom org-export-latex-listings nil
352 "Non-nil means, export source code using the listings package.
353 This package will fontify source code, possibly even with color.
354 If you want to use this, you also need to make LaTeX use the
355 listings package, and if you want to have color, the color
356 package. Just add these to `org-export-latex-packages-alist',
357 for example using customize, or with something like
360 (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
361 (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))"
362 :group
'org-export-latex
365 (defcustom org-export-latex-listings-langs
366 '((emacs-lisp "Lisp") (lisp "Lisp")
369 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
370 (html "HTML") (xml "XML")
371 (tex "TeX") (latex "TeX")
372 (shell-script "bash")
374 (ocaml "Caml") (caml "Caml")
376 "Alist mapping languages to their listing language counterpart.
377 The key is a symbol, the major mode symbol without the \"-mode\".
378 The value is the string that should be inserted as the language parameter
379 for the listings package. If the mode name and the listings name are
380 the same, the language does not need an entry in this list - but it does not
381 hurt if it is present."
382 :group
'org-export-latex
385 (symbol :tag
"Major mode ")
386 (string :tag
"Listings language"))))
388 (defcustom org-export-latex-remove-from-headlines
389 '(:todo nil
:priority nil
:tags nil
)
390 "A plist of keywords to remove from headlines. OBSOLETE.
391 Non-nil means remove this keyword type from the headline.
393 Don't remove the keys, just change their values.
395 Obsolete, this variable is no longer used. Use the separate
396 variables `org-export-with-todo-keywords', `org-export-with-priority',
397 and `org-export-with-tags' instead."
399 :group
'org-export-latex
)
401 (defcustom org-export-latex-image-default-option
"width=10em"
402 "Default option for images."
403 :group
'org-export-latex
406 (defcustom org-export-latex-inline-image-extensions
407 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
408 "Extensions of image files that can be inlined into LaTeX.
409 Note that the image extension *actually* allowed depend on the way the
410 LaTeX file is processed. When used with pdflatex, pdf, jpg and png images
411 are OK. When processing through dvi to Postscript, only ps and eps are
412 allowed. The default we use here encompasses both."
413 :group
'org-export-latex
414 :type
'(repeat (string :tag
"Extension")))
416 (defcustom org-export-latex-coding-system nil
417 "Coding system for the exported LaTex file."
418 :group
'org-export-latex
419 :type
'coding-system
)
421 (defgroup org-export-pdf nil
422 "Options for exporting Org-mode files to PDF, via LaTeX."
423 :tag
"Org Export PDF"
424 :group
'org-export-latex
427 (defcustom org-latex-to-pdf-process
428 '("pdflatex -interaction nonstopmode %s"
429 "pdflatex -interaction nonstopmode %s")
430 "Commands to process a LaTeX file to a PDF file.
431 This is a list of strings, each of them will be given to the shell
432 as a command. %s in the command will be replaced by the full file name, %b
433 by the file base name (i.e. without extension).
434 The reason why this is a list is that it usually takes several runs of
435 pdflatex, maybe mixed with a call to bibtex. Org does not have a clever
436 mechanism to detect which of these commands have to be run to get to a stable
437 result, and it also does not do any error checking.
439 Alternatively, this may be a Lisp function that does the processing, so you
440 could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
441 This function should accept the file name as its single argument."
442 :group
'org-export-pdf
443 :type
'(choice (repeat :tag
"Shell command sequence"
444 (string :tag
"Shell command"))
447 (defcustom org-export-pdf-logfiles
448 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
449 "The list of file extensions to consider as LaTeX logfiles."
450 :group
'org-export-pdf
451 :type
'(repeat (string :tag
"Extension")))
453 (defcustom org-export-pdf-remove-logfiles t
454 "Non-nil means, remove the logfiles produced by PDF production.
455 These are the .aux, .log, .out, and .toc files."
456 :group
'org-export-pdf
461 (defvar org-export-latex-after-initial-vars-hook nil
462 "Hook run before LaTeX export.
463 The exact moment is after the initial variables like org-export-latex-class
464 have been determined from the environment.")
466 (defvar org-export-latex-after-blockquotes-hook nil
467 "Hook run during LaTeX export, after blockquote, verse, center are done.")
469 (defvar org-export-latex-final-hook nil
470 "Hook run in the finalized LaTeX buffer.")
472 (defvar org-export-latex-after-save-hook nil
473 "Hook run in the finalized LaTeX buffer, after it has been saved.")
475 ;;; Autoload functions:
478 (defun org-export-as-latex-batch ()
479 "Call `org-export-as-latex', may be used in batch processing.
483 --load=$HOME/lib/emacs/org.el
484 --eval \"(setq org-export-headline-levels 2)\"
485 --visit=MyFile --funcall org-export-as-latex-batch"
486 (org-export-as-latex org-export-headline-levels
'hidden
))
489 (defun org-export-as-latex-to-buffer (arg)
490 "Call `org-export-as-latex` with output to a temporary buffer.
491 No file is created. The prefix ARG is passed through to `org-export-as-latex'."
493 (org-export-as-latex arg nil nil
"*Org LaTeX Export*")
494 (when org-export-show-temporary-export-buffer
495 (switch-to-buffer-other-window "*Org LaTeX Export*")))
498 (defun org-replace-region-by-latex (beg end
)
499 "Replace the region from BEG to END with its LaTeX export.
500 It assumes the region has `org-mode' syntax, and then convert it to
501 LaTeX. This can be used in any buffer. For example, you could
502 write an itemized list in `org-mode' syntax in an LaTeX buffer and
503 then use this command to convert it."
506 (save-window-excursion
508 (setq latex
(org-export-region-as-latex
510 (setq reg
(buffer-substring beg end
)
511 buf
(get-buffer-create "*Org tmp*"))
512 (with-current-buffer buf
516 (setq latex
(org-export-region-as-latex
517 (point-min) (point-max) t
'string
)))
519 (delete-region beg end
)
523 (defun org-export-region-as-latex (beg end
&optional body-only buffer
)
524 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
525 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
526 contents, and only produce the region of converted text, useful for
527 cut-and-paste operations.
528 If BUFFER is a buffer or a string, use/create that buffer as a target
529 of the converted LaTeX. If BUFFER is the symbol `string', return the
530 produced LaTeX as a string and leave no buffer behind. For example,
531 a Lisp program could call this function in the following way:
533 (setq latex (org-export-region-as-latex beg end t 'string))
535 When called interactively, the output buffer is selected, and shown
536 in a window. A non-interactive call will only return the buffer."
538 (when (interactive-p)
539 (setq buffer
"*Org LaTeX Export*"))
540 (let ((transient-mark-mode t
) (zmacs-regions t
)
542 (setq ext-plist
(plist-put ext-plist
:ignore-subtree-p t
))
544 (set-mark (point)) ;; to activate the region
546 (setq rtn
(org-export-as-latex
549 (if (fboundp 'deactivate-mark
) (deactivate-mark))
550 (if (and (interactive-p) (bufferp rtn
))
551 (switch-to-buffer-other-window rtn
)
555 (defun org-export-as-latex (arg &optional hidden ext-plist
556 to-buffer body-only pub-dir
)
557 "Export current buffer to a LaTeX file.
558 If there is an active region, export only the region. The prefix
559 ARG specifies how many levels of the outline should become
560 headlines. The default is 3. Lower levels will be exported
561 depending on `org-export-latex-low-levels'. The default is to
562 convert them as description lists.
563 HIDDEN is obsolete and does nothing.
564 EXT-PLIST is a property list with
565 external parameters overriding org-mode's default settings, but
566 still inferior to file-local settings. When TO-BUFFER is
567 non-nil, create a buffer with that name and export to that
568 buffer. If TO-BUFFER is the symbol `string', don't leave any
569 buffer behind but just return the resulting LaTeX as a string.
570 When BODY-ONLY is set, don't produce the file header and footer,
571 simply return the content of \begin{document}...\end{document},
572 without even the \begin{document} and \end{document} commands.
573 when PUB-DIR is set, use this as the publishing directory."
575 (run-hooks 'org-export-first-hook
)
577 ;; Make sure we have a file name when we need it.
578 (when (and (not (or to-buffer body-only
))
579 (not buffer-file-name
))
580 (if (buffer-base-buffer)
581 (org-set-local 'buffer-file-name
582 (with-current-buffer (buffer-base-buffer)
584 (error "Need a file name to be able to export")))
586 (message "Exporting to LaTeX...")
588 (let ((inhibit-read-only t
))
589 (remove-text-properties (point-min) (point-max)
590 '(:org-license-to-kill nil
))))
591 (org-update-radio-target-regexp)
592 (org-export-latex-set-initial-vars ext-plist arg
)
593 (setq org-export-opt-plist org-export-latex-options-plist
)
594 (org-install-letbind)
595 (run-hooks 'org-export-latex-after-initial-vars-hook
)
596 (let* ((wcf (current-window-configuration))
597 (opt-plist org-export-latex-options-plist
)
598 (region-p (org-region-active-p))
599 (rbeg (and region-p
(region-beginning)))
600 (rend (and region-p
(region-end)))
602 (if (plist-get opt-plist
:ignore-subtree-p
)
607 (and (org-at-heading-p)
608 (>= (org-end-of-subtree t t
) rend
))))))
609 (opt-plist (setq org-export-opt-plist
611 (org-export-add-subtree-options opt-plist rbeg
)
613 ;; Make sure the variable contains the updated values.
614 (org-export-latex-options-plist (setq org-export-opt-plist opt-plist
))
615 (title (or (and subtree-p
(org-export-get-title-from-subtree))
616 (plist-get opt-plist
:title
)
618 (plist-get opt-plist
:skip-before-1st-heading
))
619 (org-export-grab-title-from-buffer))
620 (file-name-sans-extension
621 (file-name-nondirectory buffer-file-name
))))
622 (filename (concat (file-name-as-directory
624 (org-export-directory :LaTeX ext-plist
)))
625 (file-name-sans-extension
627 (org-entry-get rbeg
"EXPORT_FILE_NAME" t
))
628 (file-name-nondirectory ;sans-extension
631 (filename (if (equal (file-truename filename
)
632 (file-truename buffer-file-name
))
633 (concat filename
".tex")
635 (buffer (if to-buffer
637 ((eq to-buffer
'string
) (get-buffer-create
638 "*Org LaTeX Export*"))
639 (t (get-buffer-create to-buffer
)))
640 (find-file-noselect filename
)))
641 (odd org-odd-levels-only
)
642 (header (org-export-latex-make-header title opt-plist
))
643 (skip (cond (subtree-p nil
)
645 (t (plist-get opt-plist
:skip-before-1st-heading
))))
646 (text (plist-get opt-plist
:text
))
647 (org-export-preprocess-hook
649 `(lambda () (org-set-local 'org-complex-heading-regexp
650 ,org-export-latex-complex-heading-re
))
651 org-export-preprocess-hook
))
652 (first-lines (if skip
"" (org-export-latex-first-lines
659 (if region-p rend
))))
660 (coding-system (and (boundp 'buffer-file-coding-system
)
661 buffer-file-coding-system
))
662 (coding-system-for-write (or org-export-latex-coding-system
664 (save-buffer-coding-system (or org-export-latex-coding-system
666 (region (buffer-substring
667 (if region-p
(region-beginning) (point-min))
668 (if region-p
(region-end) (point-max))))
670 (and text
(string-match "\\S-" text
)
671 (org-export-preprocess-string
676 :tags
(plist-get opt-plist
:tags
)
677 :priority
(plist-get opt-plist
:priority
)
678 :footnotes
(plist-get opt-plist
:footnotes
)
679 :drawers
(plist-get opt-plist
:drawers
)
680 :timestamps
(plist-get opt-plist
:timestamps
)
681 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
683 :skip-before-1st-heading skip
686 :LaTeX-fragments nil
)))
688 (org-export-preprocess-string
693 :tags
(plist-get opt-plist
:tags
)
694 :priority
(plist-get opt-plist
:priority
)
695 :footnotes
(plist-get opt-plist
:footnotes
)
696 :drawers
(plist-get opt-plist
:drawers
)
697 :timestamps
(plist-get opt-plist
:timestamps
)
698 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
699 :add-text
(if (eq to-buffer
'string
) nil text
)
700 :skip-before-1st-heading skip
701 :select-tags
(plist-get opt-plist
:select-tags
)
702 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
703 :LaTeX-fragments nil
)))
707 (org-install-letbind)
709 (and (fboundp 'set-buffer-file-coding-system
)
710 (set-buffer-file-coding-system coding-system-for-write
))
712 ;; insert the header and initial document commands
713 (unless (or (eq to-buffer
'string
) body-only
)
716 ;; insert text found in #+TEXT
717 (when (and text
(not (eq to-buffer
'string
)))
718 (insert (org-export-latex-content
719 text
'(lists tables fixed-width keywords
))
722 ;; insert lines before the first headline
724 (insert first-lines
))
726 ;; export the content of headlines
727 (org-export-latex-global
729 (insert string-for-export
)
730 (goto-char (point-min))
731 (when (re-search-forward "^\\(\\*+\\) " nil t
)
732 (let* ((asters (length (match-string 1)))
733 (level (if odd
(- asters
2) (- asters
1))))
734 (setq org-export-latex-add-level
735 (if odd
(1- (/ (1+ asters
) 2)) (1- asters
)))
736 (org-export-latex-parse-global level odd
)))))
739 (unless body-only
(insert "\n\\end{document}"))
741 ;; Attach description terms to the \item macro
742 (goto-char (point-min))
743 (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t
)
744 (delete-region (match-beginning 1) (match-end 1)))
746 ;; Relocate the table of contents
747 (goto-char (point-min))
748 (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t
)
749 (goto-char (point-min))
750 (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t
)
752 (goto-char (point-min))
753 (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t
)
754 (replace-match "\\tableofcontents" t t
)))
756 ;; Cleanup forced line ends in items where they are not needed
757 (goto-char (point-min))
758 (while (re-search-forward
759 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin"
761 (delete-region (match-beginning 1) (match-end 1)))
762 (goto-char (point-min))
763 (while (re-search-forward
764 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*"
766 (if (looking-at "[\n \t]+")
767 (replace-match "\n")))
769 (run-hooks 'org-export-latex-final-hook
)
770 (or to-buffer
(save-buffer))
771 (org-export-latex-fix-inputenc)
772 (run-hooks 'org-export-latex-after-save-hook
)
773 (goto-char (point-min))
774 (or (org-export-push-to-kill-ring "LaTeX")
775 (message "Exporting to LaTeX...done"))
777 (if (eq to-buffer
'string
)
778 (prog1 (buffer-substring (point-min) (point-max))
779 (kill-buffer (current-buffer)))
781 (set-window-configuration wcf
))))
784 (defun org-export-as-pdf (arg &optional hidden ext-plist
785 to-buffer body-only pub-dir
)
786 "Export as LaTeX, then process through to PDF."
788 (message "Exporting to PDF...")
789 (let* ((wconfig (current-window-configuration))
790 (lbuf (org-export-as-latex arg hidden ext-plist
791 to-buffer body-only pub-dir
))
792 (file (buffer-file-name lbuf
))
793 (base (file-name-sans-extension (buffer-file-name lbuf
)))
794 (pdffile (concat base
".pdf"))
795 (cmds org-latex-to-pdf-process
)
796 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
797 (bibtex-p (with-current-buffer lbuf
799 (goto-char (point-min))
800 (re-search-forward "\\\\bibliography{" nil t
))))
802 (with-current-buffer outbuf
(erase-buffer))
803 (message "Processing LaTeX file...")
804 (if (and cmds
(symbolp cmds
))
807 (setq cmd
(pop cmds
))
808 (while (string-match "%b" cmd
)
809 (setq cmd
(replace-match
811 (shell-quote-argument base
))
813 (while (string-match "%s" cmd
)
814 (setq cmd
(replace-match
816 (shell-quote-argument file
))
818 (shell-command cmd outbuf outbuf
)))
819 (message "Processing LaTeX file...done")
820 (if (not (file-exists-p pdffile
))
821 (error "PDF file was not produced")
822 (set-window-configuration wconfig
)
823 (when org-export-pdf-remove-logfiles
824 (dolist (ext org-export-pdf-logfiles
)
825 (setq file
(concat base
"." ext
))
826 (and (file-exists-p file
) (delete-file file
))))
827 (message "Exporting to PDF...done")
831 (defun org-export-as-pdf-and-open (arg)
832 "Export as LaTeX, then process through to PDF, and open."
834 (let ((pdffile (org-export-as-pdf arg
)))
836 (org-open-file pdffile
)
837 (error "PDF file was not produced"))))
839 ;;; Parsing functions:
841 (defun org-export-latex-parse-global (level odd
)
842 "Parse the current buffer recursively, starting at LEVEL.
843 If ODD is non-nil, assume the buffer only contains odd sections.
844 Return a list reflecting the document structure."
846 (goto-char (point-min))
847 (let* ((cnt 0) output
848 (depth org-export-latex-sectioning-depth
))
849 (while (org-re-search-forward-unprotected
850 (concat "^\\(\\(?:\\*\\)\\{"
851 (number-to-string (+ (if odd
2 1) level
))
853 ;; make sure that there is no upper heading
857 (org-re-search-forward-unprotected
858 (concat "^\\(\\(?:\\*\\)\\{"
859 (number-to-string level
)
860 "\\}\\) \\(.*\\)$") nil t
)))) t
)
862 (let* ((pos (match-beginning 0))
863 (heading (match-string 2))
864 (nlevel (if odd
(/ (+ 3 level
) 2) (1+ level
))))
869 (if (org-re-search-forward-unprotected
870 (concat "^\\(\\(?:\\*\\)\\{"
871 (number-to-string (+ (if odd
2 1) level
))
872 "\\}\\) \\(.*\\)$") nil t
)
875 (goto-char (point-min))
883 `(heading .
,heading
)
884 `(content .
,(org-export-latex-parse-content))
885 `(subcontent .
,(org-export-latex-parse-subcontent
890 (defun org-export-latex-parse-content ()
891 "Extract the content of a section."
893 (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t
)
894 (progn (beginning-of-line) (point))
896 (buffer-substring beg end
)))
898 (defun org-export-latex-parse-subcontent (level odd
)
899 "Extract the subcontent of a section at LEVEL.
900 If ODD Is non-nil, assume subcontent only contains odd sections."
901 (if (not (org-re-search-forward-unprotected
902 (concat "^\\(\\(?:\\*\\)\\{"
903 (number-to-string (+ (if odd
4 2) level
))
906 nil
; subcontent is nil
907 (org-export-latex-parse-global (+ (if odd
2 1) level
) odd
)))
909 ;;; Rendering functions:
910 (defun org-export-latex-global (content)
911 "Export CONTENT to LaTeX.
912 CONTENT is an element of the list produced by
913 `org-export-latex-parse-global'."
914 (if (eq (car content
) 'subcontent
)
915 (mapc 'org-export-latex-sub
(cdr content
))
916 (org-export-latex-sub (car content
))))
918 (defun org-export-latex-sub (subcontent)
919 "Export the list SUBCONTENT to LaTeX.
920 SUBCONTENT is an alist containing information about the headline
922 (let ((num (plist-get org-export-latex-options-plist
:section-numbers
)))
923 (mapc (lambda(x) (org-export-latex-subcontent x num
)) subcontent
)))
925 (defun org-export-latex-subcontent (subcontent num
)
926 "Export each cell of SUBCONTENT to LaTeX.
927 If NUM, export sections as numerical sections."
928 (let* ((heading (cdr (assoc 'heading subcontent
)))
929 (level (- (cdr (assoc 'level subcontent
))
930 org-export-latex-add-level
))
931 (occur (number-to-string (cdr (assoc 'occur subcontent
))))
932 (content (cdr (assoc 'content subcontent
)))
933 (subcontent (cadr (assoc 'subcontent subcontent
)))
934 (label (org-get-text-property-any 0 'target heading
))
935 (label-list (cons label
(cdr (assoc label
936 org-export-target-aliases
))))
937 (sectioning org-export-latex-sectioning
)
938 (depth org-export-latex-sectioning-depth
)
939 main-heading sub-heading
)
940 (when (symbolp (car sectioning
))
941 (setq sectioning
(funcall (car sectioning
) level heading
))
943 (setq heading
(car sectioning
)
944 sectioning
(cdr sectioning
)
945 ;; target property migh have changed...
946 label
(org-get-text-property-any 0 'target heading
)
947 label-list
(cons label
(cdr (assoc label
948 org-export-target-aliases
)))))
949 (if sectioning
(setq sectioning
(make-list 10 sectioning
)))
950 (setq depth
(if sectioning
10000 0)))
951 (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading
)
952 (setq main-heading
(substring heading
0 (match-beginning 0))
953 sub-heading
(substring heading
(match-end 0))))
954 (setq heading
(org-export-latex-fontify-headline heading
)
955 sub-heading
(and sub-heading
956 (org-export-latex-fontify-headline sub-heading
))
957 main-heading
(and main-heading
958 (org-export-latex-fontify-headline main-heading
)))
962 (let* ((sec (nth (1- level
) sectioning
))
964 (if (consp (cdr sec
))
965 (setq start
(nth (if num
0 2) sec
)
966 end
(nth (if num
1 3) sec
))
967 (setq start
(if num
(car sec
) (cdr sec
))))
968 (insert (format start
(if main-heading main-heading heading
)
969 (or sub-heading
"")))
972 (insert (mapconcat (lambda (l) (format "\\label{%s}" l
))
973 label-list
"\n") "\n"))
974 (insert (org-export-latex-content content
))
975 (cond ((stringp subcontent
) (insert subcontent
))
977 (while (org-looking-back "\n\n") (backward-delete-char 1))
978 (org-export-latex-sub subcontent
)))
979 (when (and end
(string-match "[^ \t]" end
))
980 (let ((hook (org-get-text-property-any 0 'org-insert-hook end
)))
981 (and (functionp hook
) (funcall hook
)))
983 ;; At a level under the hl option: we can drop this subsection
985 (cond ((eq org-export-latex-low-levels
'description
)
986 (if (string-match "% ends low level$"
987 (buffer-substring (point-at-bol 0) (point)))
988 (delete-region (point-at-bol 0) (point))
989 (insert "\\begin{description}\n"))
990 (insert (format "\n\\item[%s]%s~\n"
992 (if label
(format "\\label{%s}" label
) "")))
993 (insert (org-export-latex-content content
))
994 (cond ((stringp subcontent
) (insert subcontent
))
995 ((listp subcontent
) (org-export-latex-sub subcontent
)))
996 (insert "\\end{description} % ends low level\n"))
997 ((memq org-export-latex-low-levels
'(itemize enumerate
))
998 (if (string-match "% ends low level$"
999 (buffer-substring (point-at-bol 0) (point)))
1000 (delete-region (point-at-bol 0) (point))
1001 (insert (format "\\begin{%s}\n"
1002 (symbol-name org-export-latex-low-levels
))))
1003 (insert (format "\n\\item %s\\\\\n%s%%"
1005 (if label
(format "\\label{%s}" label
) "")))
1006 (insert (org-export-latex-content content
))
1007 (cond ((stringp subcontent
) (insert subcontent
))
1008 ((listp subcontent
) (org-export-latex-sub subcontent
)))
1009 (insert (format "\\end{%s} %% ends low level\n"
1010 (symbol-name org-export-latex-low-levels
))))
1012 ((listp org-export-latex-low-levels
)
1013 (if (string-match "% ends low level$"
1014 (buffer-substring (point-at-bol 0) (point)))
1015 (delete-region (point-at-bol 0) (point))
1016 (insert (car org-export-latex-low-levels
) "\n"))
1017 (insert (format (nth 2 org-export-latex-low-levels
)
1019 (if label
(format "\\label{%s}" label
) "")))
1020 (insert (org-export-latex-content content
))
1021 (cond ((stringp subcontent
) (insert subcontent
))
1022 ((listp subcontent
) (org-export-latex-sub subcontent
)))
1023 (insert (nth 1 org-export-latex-low-levels
)
1024 " %% ends low level\n"))
1026 ((stringp org-export-latex-low-levels
)
1027 (insert (format org-export-latex-low-levels heading
) "\n")
1028 (when label
(insert (format "\\label{%s}\n" label
)))
1029 (insert (org-export-latex-content content
))
1030 (cond ((stringp subcontent
) (insert subcontent
))
1031 ((listp subcontent
) (org-export-latex-sub subcontent
)))))))))
1033 ;;; Exporting internals:
1034 (defun org-export-latex-set-initial-vars (ext-plist level
)
1035 "Store org local variables required for LaTeX export.
1036 EXT-PLIST is an optional additional plist.
1037 LEVEL indicates the default depth for export."
1038 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
1039 org-export-latex-done-keywords org-done-keywords
1040 org-export-latex-not-done-keywords org-not-done-keywords
1041 org-export-latex-complex-heading-re org-complex-heading-regexp
1042 org-export-latex-display-custom-times org-display-custom-times
1043 org-export-latex-all-targets-re
1044 (org-make-target-link-regexp (org-all-targets))
1045 org-export-latex-options-plist
1046 (org-combine-plists (org-default-export-plist) ext-plist
1047 (org-infile-export-plist))
1048 org-export-latex-class
1049 (or (and (org-region-active-p)
1051 (goto-char (region-beginning))
1052 (and (looking-at org-complex-heading-regexp
)
1053 (org-entry-get nil
"LaTeX_CLASS" 'selective
))))
1057 (goto-char (point-min))
1058 (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t
)
1060 (plist-get org-export-latex-options-plist
:latex-class
)
1061 org-export-latex-default-class
)
1062 org-export-latex-class-options
1063 (or (and (org-region-active-p)
1065 (goto-char (region-beginning))
1066 (and (looking-at org-complex-heading-regexp
)
1067 (org-entry-get nil
"LaTeX_CLASS_OPTIONS" 'selective
))))
1071 (goto-char (point-min))
1072 (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t
)
1074 (plist-get org-export-latex-options-plist
:latex-class-options
))
1075 org-export-latex-class
1076 (or (car (assoc org-export-latex-class org-export-latex-classes
))
1077 (error "No definition for class `%s' in `org-export-latex-classes'"
1078 org-export-latex-class
))
1079 org-export-latex-header
1080 (cadr (assoc org-export-latex-class org-export-latex-classes
))
1081 org-export-latex-sectioning
1082 (cddr (assoc org-export-latex-class org-export-latex-classes
))
1083 org-export-latex-sectioning-depth
1086 (plist-get org-export-latex-options-plist
:headline-levels
))
1087 (sec-depth (length org-export-latex-sectioning
)))
1088 (if (> hl-levels sec-depth
) sec-depth hl-levels
))))
1089 (when (and org-export-latex-class-options
1090 (string-match "\\S-" org-export-latex-class-options
)
1091 (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?"
1092 org-export-latex-header
))
1093 (setq org-export-latex-header
1094 (concat (substring org-export-latex-header
0 (match-end 1))
1095 org-export-latex-class-options
1096 (substring org-export-latex-header
(match-end 0))))))
1098 (defvar org-export-latex-format-toc-function
1099 'org-export-latex-format-toc-default
1100 "The function formatting returning the string to createthe table of contents.
1101 The function mus take one parameter, the depth of the table of contents.")
1103 (defun org-export-latex-make-header (title opt-plist
)
1104 "Make the LaTeX header and return it as a string.
1105 TITLE is the current title from the buffer or region.
1106 OPT-PLIST is the options plist for current buffer."
1107 (let ((toc (plist-get opt-plist
:table-of-contents
))
1108 (author (plist-get opt-plist
:author
)))
1110 (if (plist-get opt-plist
:time-stamp-file
)
1111 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1112 ;; insert LaTeX custom header
1113 (org-export-apply-macros-in-string org-export-latex-header
)
1115 ;; insert information on LaTeX packages
1116 (when org-export-latex-packages-alist
1118 (mapconcat (lambda(p)
1119 (if (equal "" (car p
))
1120 (format "\\usepackage{%s}" (cadr p
))
1121 (format "\\usepackage[%s]{%s}"
1123 org-export-latex-packages-alist
"\n")
1125 ;; insert additional commands in the header
1126 (org-export-apply-macros-in-string
1127 (plist-get opt-plist
:latex-header-extra
))
1128 (org-export-apply-macros-in-string org-export-latex-append-header
)
1132 ;; convert the title
1133 (org-export-latex-content
1134 title
'(lists tables fixed-width keywords
)))
1135 ;; insert author info
1136 (if (plist-get opt-plist
:author-info
)
1137 (format "\\author{%s}\n"
1138 (org-export-latex-fontify-headline (or author user-full-name
)))
1139 (format "%%\\author{%s}\n"
1140 (or author user-full-name
)))
1142 (format "\\date{%s}\n"
1144 (or (plist-get opt-plist
:date
)
1145 org-export-latex-date-format
)))
1146 ;; beginning of the document
1147 "\n\\begin{document}\n\n"
1148 ;; insert the title command
1149 (when (string-match "\\S-" title
)
1150 (if (string-match "%s" org-export-latex-title-command
)
1151 (format org-export-latex-title-command title
)
1152 org-export-latex-title-command
))
1154 ;; table of contents
1155 (when (and org-export-with-toc
1156 (plist-get opt-plist
:section-numbers
))
1157 (funcall org-export-latex-format-toc-function
1158 (cond ((numberp toc
)
1159 (min toc
(plist-get opt-plist
:headline-levels
)))
1160 (toc (plist-get opt-plist
:headline-levels
))))))))
1162 (defun org-export-latex-format-toc-default (depth)
1164 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1167 (defun org-export-latex-first-lines (opt-plist &optional beg end
)
1168 "Export the first lines before first headline.
1169 If BEG is non-nil, it is the beginning of the region.
1170 If END is non-nil, it is the end of the region."
1172 (goto-char (or beg
(point-min)))
1174 (end (if (re-search-forward "^\\*+ " end t
)
1175 (goto-char (match-beginning 0))
1176 (goto-char (or end
(point-max))))))
1178 (org-export-latex-content
1179 (org-export-preprocess-string
1180 (buffer-substring pt end
)
1185 :skip-before-1st-heading nil
1186 :LaTeX-fragments nil
1187 :timestamps
(plist-get opt-plist
:timestamps
)
1188 :footnotes
(plist-get opt-plist
:footnotes
)))
1190 (let ((inhibit-read-only t
))
1191 (add-text-properties pt
(max pt
(1- end
))
1192 '(:org-license-to-kill t
))))))))
1194 (defvar org-export-latex-header-defs nil
1195 "The header definitions that might be used in the LaTeX body.")
1196 (defvar org-export-latex-header-defs-re nil
1197 "The header definitions that might be used in the LaTeX body.")
1199 (defun org-export-latex-content (content &optional exclude-list
)
1200 "Convert CONTENT string to LaTeX.
1201 Don't perform conversions that are in EXCLUDE-LIST. Recognized
1202 conversion types are: quotation-marks, emphasis, sub-superscript,
1203 links, keywords, lists, tables, fixed-width"
1206 (unless (memq 'timestamps exclude-list
)
1207 (org-export-latex-time-stamps))
1208 (unless (memq 'quotation-marks exclude-list
)
1209 (org-export-latex-quotation-marks))
1210 (unless (memq 'emphasis exclude-list
)
1211 (when (plist-get org-export-latex-options-plist
:emphasize
)
1212 (org-export-latex-fontify)))
1213 (unless (memq 'sub-superscript exclude-list
)
1214 (org-export-latex-special-chars
1215 (plist-get org-export-latex-options-plist
:sub-superscript
)))
1216 (unless (memq 'links exclude-list
)
1217 (org-export-latex-links))
1218 (unless (memq 'keywords exclude-list
)
1219 (org-export-latex-keywords))
1220 (unless (memq 'lists exclude-list
)
1221 (org-export-latex-lists))
1222 (unless (memq 'tables exclude-list
)
1223 (org-export-latex-tables
1224 (plist-get org-export-latex-options-plist
:tables
)))
1225 (unless (memq 'fixed-width exclude-list
)
1226 (org-export-latex-fixed-width
1227 (plist-get org-export-latex-options-plist
:fixed-width
)))
1229 (buffer-substring (point-min) (point-max))))
1231 (defun org-export-latex-protect-string (s)
1232 "Add the org-protected property to string S."
1233 (add-text-properties 0 (length s
) '(org-protected t
) s
) s
)
1235 (defun org-export-latex-protect-char-in-string (char-list string
)
1236 "Add org-protected text-property to char from CHAR-LIST in STRING."
1240 (goto-char (point-min))
1241 (while (re-search-forward (regexp-opt char-list
) nil t
)
1242 (add-text-properties (match-beginning 0)
1243 (match-end 0) '(org-protected t
)))
1246 (defun org-export-latex-keywords-maybe (&optional remove-list
)
1247 "Maybe remove keywords depending on rules in REMOVE-LIST."
1248 (goto-char (point-min))
1249 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1
"\\|"))
1250 (case-fold-search nil
)
1251 (todo-markup org-export-latex-todo-keyword-markup
)
1253 ;; convert TODO keywords
1254 (when (re-search-forward (concat "^\\(" re-todo
"\\)") nil t
)
1255 (if (plist-get remove-list
:todo
)
1258 ((stringp todo-markup
) todo-markup
)
1259 ((and (consp todo-markup
) (stringp (car todo-markup
)))
1260 (if (member (match-string 1) org-export-latex-done-keywords
)
1261 (cdr todo-markup
) (car todo-markup
)))
1262 (t (cdr (or (assoc (match-string 1) todo-markup
)
1263 (car todo-markup
))))))
1264 (replace-match (format fmt
(match-string 1)) t t
)))
1265 ;; convert priority string
1266 (when (re-search-forward "\\[\\\\#.\\]" nil t
)
1267 (if (plist-get remove-list
:priority
)
1269 (replace-match (format "\\textbf{%s}" (match-string 0)) t t
)))
1271 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t
)
1272 (if (or (not org-export-with-tags
)
1273 (plist-get remove-list
:tags
))
1276 (org-export-latex-protect-string
1277 (format "\\textbf{%s}"
1279 (replace-regexp-in-string
1280 "_" "\\\\_" (match-string 0)))))
1283 (defun org-export-latex-fontify-headline (string)
1284 "Fontify special words in STRING."
1286 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
1287 ;; the beginning of the buffer - inserting "\n" is safe here though.
1288 (insert "\n" string
)
1289 (goto-char (point-min))
1290 (let ((re (concat "\\\\[a-zA-Z]+\\(?:"
1293 (org-create-multibrace-regexp "{" "}" 3))))
1294 (while (re-search-forward re nil t
)
1295 (unless (save-excursion (goto-char (match-beginning 0))
1296 (equal (char-after (point-at-bol)) ?
#))
1297 (add-text-properties (match-beginning 0) (match-end 0)
1298 '(org-protected t
)))))
1299 (when (plist-get org-export-latex-options-plist
:emphasize
)
1300 (org-export-latex-fontify))
1301 (org-export-latex-keywords-maybe)
1302 (org-export-latex-special-chars
1303 (plist-get org-export-latex-options-plist
:sub-superscript
))
1304 (org-export-latex-links)
1305 (org-trim (buffer-string))))
1307 (defun org-export-latex-time-stamps ()
1308 "Format time stamps."
1309 (goto-char (point-min))
1310 (let ((org-display-custom-times org-export-latex-display-custom-times
))
1311 (while (re-search-forward org-ts-regexp-both nil t
)
1312 (org-if-unprotected-at (1- (point))
1314 (org-export-latex-protect-string
1315 (format org-export-latex-timestamp-markup
1316 (substring (org-translate-time (match-string 0)) 1 -
1)))
1319 (defun org-export-latex-quotation-marks ()
1320 "Export quotation marks depending on language conventions."
1321 (let* ((lang (plist-get org-export-latex-options-plist
:language
))
1322 (quote-rpl (if (equal lang
"fr")
1323 '(("\\(\\s-\\)\"" "«~")
1324 ("\\(\\S-\\)\"" "~»")
1325 ("\\(\\s-\\)'" "`"))
1326 '(("\\(\\s-\\|[[(]\\)\"" "``")
1327 ("\\(\\S-\\)\"" "''")
1328 ("\\(\\s-\\|(\\)'" "`")))))
1329 (mapc (lambda(l) (goto-char (point-min))
1330 (while (re-search-forward (car l
) nil t
)
1331 (let ((rpl (concat (match-string 1)
1332 (org-export-latex-protect-string
1333 (copy-sequence (cadr l
))))))
1334 (org-if-unprotected-1
1335 (replace-match rpl t t
))))) quote-rpl
)))
1337 (defun org-export-latex-special-chars (sub-superscript)
1338 "Export special characters to LaTeX.
1339 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
1340 See the `org-export-latex.el' code for a complete conversion table."
1341 (goto-char (point-min))
1343 (goto-char (point-min))
1344 (while (re-search-forward c nil t
)
1345 ;; Put the point where to check for org-protected
1346 (unless (get-text-property (match-beginning 2) 'org-protected
)
1347 (cond ((member (match-string 2) '("\\$" "$"))
1348 (if (equal (match-string 2) "\\$")
1350 (replace-match "\\$" t t
)))
1351 ((member (match-string 2) '("&" "%" "#"))
1352 (if (equal (match-string 1) "\\")
1353 (replace-match (match-string 2) t t
)
1354 (replace-match (concat (match-string 1) "\\"
1355 (match-string 2)) t t
)))
1356 ((equal (match-string 2) "...")
1358 (concat (match-string 1)
1359 (org-export-latex-protect-string "\\ldots{}")) t t
))
1360 ((equal (match-string 2) "~")
1361 (cond ((equal (match-string 1) "\\") nil
)
1362 ((eq 'org-link
(get-text-property 0 'face
(match-string 2)))
1363 (replace-match (concat (match-string 1) "\\~") t t
))
1365 (org-export-latex-protect-string
1366 (concat (match-string 1) "\\~{}")) t t
))))
1367 ((member (match-string 2) '("{" "}"))
1368 (unless (save-match-data (org-inside-latex-math-p))
1369 (if (equal (match-string 1) "\\")
1370 (replace-match (match-string 2) t t
)
1371 (replace-match (concat (match-string 1) "\\"
1372 (match-string 2)) t t
)))))
1373 (unless (save-match-data (org-inside-latex-math-p))
1374 (cond ((equal (match-string 2) "\\")
1375 (replace-match (or (save-match-data
1376 (org-export-latex-treat-backslash-char
1378 (or (match-string 3) "")))
1380 ((member (match-string 2) '("_" "^"))
1381 (replace-match (or (save-match-data
1382 (org-export-latex-treat-sub-super-char
1386 (match-string 3))) "") t t
)
1387 (backward-char 1)))))))
1388 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
1389 "\\(\\(\\\\?\\$\\)\\)"
1390 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
1391 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
1392 "\\(.\\|^\\)\\(&\\)"
1393 "\\(.\\|^\\)\\(#\\)"
1394 "\\(.\\|^\\)\\(%\\)"
1395 "\\(.\\|^\\)\\({\\)"
1396 "\\(.\\|^\\)\\(}\\)"
1397 "\\(.\\|^\\)\\(~\\)"
1398 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
1399 ;; (?\< . "\\textless{}")
1400 ;; (?\> . "\\textgreater{}")
1403 (defun org-inside-latex-math-p ()
1404 (get-text-property (point) 'org-latex-math
))
1406 (defun org-export-latex-treat-sub-super-char
1407 (subsup char string-before string-after
)
1408 "Convert the \"_\" and \"^\" characters to LaTeX.
1409 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
1410 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
1411 (cond ((equal string-before
"\\")
1412 (concat string-before char string-after
))
1413 ((and (string-match "\\S-+" string-after
))
1414 ;; this is part of a math formula
1415 (cond ((eq 'org-link
(get-text-property 0 'face char
))
1416 (concat string-before
"\\" char string-after
))
1417 ((save-match-data (org-inside-latex-math-p))
1419 (cond ((eq 1 (length string-after
))
1420 (concat string-before char string-after
))
1421 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after
)
1422 (format "%s%s{%s}" string-before char
1423 (match-string 1 string-after
))))))
1424 ((and (> (length string-after
) 1)
1426 (and (equal subsup
'{}) (eq (string-to-char string-after
) ?\
{)))
1427 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after
))
1428 (org-export-latex-protect-string
1429 (format "%s$%s{%s}$" string-before char
1430 (if (and (> (match-end 1) (1+ (match-beginning 1)))
1431 (not (equal (substring string-after
0 2) "{\\")))
1432 (concat "\\mathrm{" (match-string 1 string-after
) "}")
1433 (match-string 1 string-after
)))))
1434 ((eq subsup t
) (concat string-before
"$" char string-after
"$"))
1435 (t (org-export-latex-protect-string
1436 (concat string-before
"\\" char
"{}" string-after
)))))
1437 (t (org-export-latex-protect-string
1438 (concat string-before
"\\" char
"{}" string-after
)))))
1440 (defun org-export-latex-treat-backslash-char (string-before string-after
)
1441 "Convert the \"$\" special character to LaTeX.
1442 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
1443 (cond ((member (list string-after
) org-html-entities
)
1444 ;; backslash is part of a special entity (like "\alpha")
1445 (concat string-before
"$\\"
1446 (or (cdar (member (list string-after
) org-html-entities
))
1448 ((and (not (string-match "^[ \n\t]" string-after
))
1449 (not (string-match "[ \t]\\'\\|^" string-before
)))
1450 ;; backslash is inside a word
1451 (concat string-before
1452 (org-export-latex-protect-string
1453 (concat "\\textbackslash{}" string-after
))))
1454 ((not (or (equal string-after
"")
1455 (string-match "^[ \t\n]" string-after
)))
1456 ;; backslash might escape a character (like \#) or a user TeX
1457 ;; macro (like \setcounter)
1458 (concat string-before
1459 (org-export-latex-protect-string (concat "\\" string-after
))))
1460 ((and (string-match "^[ \t\n]" string-after
)
1461 (string-match "[ \t\n]\\'" string-before
))
1462 ;; backslash is alone, convert it to $\backslash$
1463 (org-export-latex-protect-string
1464 (concat string-before
"\\textbackslash{}" string-after
)))
1465 (t (org-export-latex-protect-string
1466 (concat string-before
"\\textbackslash{}" string-after
)))))
1468 (defun org-export-latex-keywords ()
1469 "Convert special keywords to LaTeX."
1470 (goto-char (point-min))
1471 (while (re-search-forward org-export-latex-special-keyword-regexp nil t
)
1472 (replace-match (format org-export-latex-timestamp-keyword-markup
1473 (match-string 0)) t t
)
1475 (beginning-of-line 1)
1476 (unless (looking-at ".*\\\\newline[ \t]*$")
1478 (insert "\\newline")))))
1480 (defun org-export-latex-fixed-width (opt)
1481 "When OPT is non-nil convert fixed-width sections to LaTeX."
1482 (goto-char (point-min))
1483 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t
)
1485 (progn (goto-char (match-beginning 0))
1486 (insert "\\begin{verbatim}\n")
1487 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1488 (replace-match (concat (match-string 1)
1489 (match-string 2)) t t
)
1491 (insert "\\end{verbatim}\n\n"))
1492 (progn (goto-char (match-beginning 0))
1493 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1494 (replace-match (concat "%" (match-string 1)
1495 (match-string 2)) t t
)
1499 (defvar org-table-last-alignment
) ; defined in org-table.el
1500 (defvar org-table-last-column-widths
) ; defined in org-table.el
1501 (declare-function orgtbl-to-latex
"org-table" (table params
) t
)
1502 (defun org-export-latex-tables (insert)
1503 "Convert tables to LaTeX and INSERT it."
1504 (goto-char (point-min))
1505 (while (re-search-forward "^\\([ \t]*\\)|" nil t
)
1506 (org-if-unprotected-at (1- (point))
1508 (let* ((beg (org-table-begin))
1509 (end (org-table-end))
1510 (raw-table (buffer-substring beg end
))
1511 (org-table-last-alignment (copy-sequence org-table-last-alignment
))
1512 (org-table-last-column-widths (copy-sequence
1513 org-table-last-column-widths
))
1514 fnum fields line lines olines gr colgropen line-fmt align
1515 caption label attr floatp longtblp
)
1516 (if org-export-latex-tables-verbatim
1517 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1518 "\\end{verbatim}\n")))
1519 (apply 'delete-region
(list beg end
))
1520 (insert (org-export-latex-protect-string tbl
)))
1522 (setq caption
(org-find-text-property-in-string
1523 'org-caption raw-table
)
1524 attr
(org-find-text-property-in-string
1525 'org-attributes raw-table
)
1526 label
(org-find-text-property-in-string
1527 'org-label raw-table
)
1528 longtblp
(and attr
(stringp attr
)
1529 (string-match "\\<longtable\\>" attr
))
1530 align
(and attr
(stringp attr
)
1531 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr
)
1532 (match-string 1 attr
))
1533 floatp
(or caption label
))
1534 (setq lines
(org-split-string raw-table
"\n"))
1535 (apply 'delete-region
(list beg end
))
1536 (when org-export-table-remove-special-lines
1537 (setq lines
(org-table-clean-before-export lines
'maybe-quoted
)))
1538 (when org-table-clean-did-remove-column
1539 (pop org-table-last-alignment
)
1540 (pop org-table-last-column-widths
))
1541 ;; make a formatting string to reflect alignment
1543 (while (and (not line-fmt
) (setq line
(pop olines
)))
1544 (unless (string-match "^[ \t]*|-" line
)
1545 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
1546 (setq fnum
(make-vector (length fields
) 0))
1550 (setq gr
(pop org-table-colgroup-info
))
1552 (cond ((eq gr
:start
)
1553 (prog1 (if colgropen
"|" "|")
1554 (setq colgropen t
)))
1556 (prog1 (if colgropen
"|" "|")
1557 (setq colgropen nil
)))
1559 (if (memq gr
'(:end
:startend
))
1560 (progn (setq colgropen nil
) "|")
1563 ;; fix double || in line-fmt
1564 (setq line-fmt
(replace-regexp-in-string "||" "|" line-fmt
))
1565 ;; maybe remove the first and last "|"
1566 (when (and (not org-export-latex-tables-column-borders
)
1567 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt
))
1568 (setq line-fmt
(match-string 2 line-fmt
)))
1571 (setq align
(apply 'format
1573 (mapcar (lambda (x) (if x
"r" "l"))
1574 org-table-last-alignment
)))))
1575 ;; prepare the table to send to orgtbl-to-latex
1579 (or (and (string-match "[ \t]*|-+" elem
) 'hline
)
1580 (org-split-string (org-trim elem
) "|")))
1583 (insert (org-export-latex-protect-string
1586 (concat "\\begin{longtable}{" align
"}\n")
1587 (if floatp
"\\begin{table}[htb]\n"))
1591 (if label
(concat "\\\label{" label
"}") "")
1593 (if (and longtblp caption
) "\\\\\n" "\n")
1594 (if (and org-export-latex-tables-centered
(not longtblp
))
1595 "\\begin{center}\n")
1596 (if (not longtblp
) (concat "\\begin{tabular}{" align
"}\n"))
1599 `(:tstart nil
:tend nil
1600 :hlend
,(if longtblp
1604 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
1606 \\endlastfoot" (length org-table-last-alignment
))
1608 (if (not longtblp
) (concat "\n\\end{tabular}"))
1609 (if longtblp
"\n" (if org-export-latex-tables-centered
1610 "\n\\end{center}\n" "\n"))
1613 (if floatp
"\\end{table}"))))
1616 (defun org-export-latex-fontify ()
1617 "Convert fontification to LaTeX."
1618 (goto-char (point-min))
1619 (while (re-search-forward org-emph-re nil t
)
1620 ;; The match goes one char after the *string*, except at the end of a line
1621 (let ((emph (assoc (match-string 3)
1622 org-export-latex-emphasis-alist
))
1623 (beg (match-beginning 0))
1627 (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
1629 (unless (or (and (get-text-property (- (point) 2) 'org-protected
)
1630 (not (get-text-property
1631 (- (point) 2) 'org-verbatim-emph
)))
1633 (goto-char (match-beginning 1))
1635 (and (org-at-table-p)
1637 "[|\n]" (buffer-substring beg end
))))))
1638 (setq s
(match-string 4))
1639 (setq rpl
(concat (match-string 1)
1640 (org-export-latex-emph-format (cadr emph
)
1644 (setq rpl
(org-export-latex-protect-string rpl
))
1646 (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl
)
1648 (add-text-properties (match-beginning 1) (match-end 1)
1649 '(org-protected t
) rpl
)
1650 (add-text-properties (match-beginning 3) (match-end 3)
1651 '(org-protected t
) rpl
)))))
1652 (replace-match rpl t t
)))
1655 (defvar org-export-latex-use-verb nil
)
1656 (defun org-export-latex-emph-format (format string
)
1657 "Format an emphasis string and handle the \\verb special case."
1658 (when (equal format
"\\verb")
1660 (if org-export-latex-use-verb
1661 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1663 (loop for i from
0 to
(1- (length ll
)) do
1664 (if (not (string-match (regexp-quote (substring ll i
(1+ i
)))
1667 (setq format
(concat "\\verb" (substring ll i
(1+ i
))
1668 "%s" (substring ll i
(1+ i
))))
1669 (throw 'exit nil
))))))
1671 (trans '(("\\" .
"\\textbackslash{}")
1672 ("~" .
"\\textasciitilde{}")
1673 ("^" .
"\\textasciicircum{}")))
1675 (while (string-match "[\\{}$%&_#~^]" string
)
1676 (setq char
(match-string 0 string
))
1677 (if (> (match-beginning 0) 0)
1678 (setq rtn
(concat rtn
(substring string
1679 0 (match-beginning 0)))))
1680 (setq string
(substring string
(1+ (match-beginning 0))))
1681 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
1682 rtn
(concat rtn char
)))
1683 (setq string
(concat rtn string
) format
"\\texttt{%s}")))))
1684 (format format string
))
1686 (defun org-export-latex-links ()
1687 ;; Make sure to use the LaTeX hyperref and graphicx package
1688 ;; or send some warnings.
1689 "Convert links to LaTeX."
1690 (goto-char (point-min))
1691 (while (re-search-forward org-bracket-link-analytic-regexp
++ nil t
)
1692 (org-if-unprotected-1
1693 (goto-char (match-beginning 0))
1694 (let* ((re-radio org-export-latex-all-targets-re
)
1695 (remove (list (match-beginning 0) (match-end 0)))
1696 (raw-path (org-extract-attributes (match-string 3)))
1697 (full-raw-path (concat (match-string 1) raw-path
))
1698 (desc (match-string 5))
1699 (type (or (match-string 2)
1700 (if (or (file-name-absolute-p raw-path
)
1701 (string-match "^\\.\\.?/" raw-path
))
1703 (coderefp (equal type
"coderef"))
1704 (caption (org-find-text-property-in-string 'org-caption raw-path
))
1705 (attr (or (org-find-text-property-in-string 'org-attributes raw-path
)
1706 (plist-get org-export-latex-options-plist
:latex-image-options
)))
1707 (label (org-find-text-property-in-string 'org-label raw-path
))
1709 ;; define the path of the link
1711 ((member type
'("coderef"))
1713 ((member type
'("http" "https" "ftp"))
1714 (concat type
":" raw-path
))
1715 ((and re-radio
(string-match re-radio raw-path
))
1717 ((equal type
"mailto")
1718 (concat type
":" raw-path
))
1719 ((equal type
"file")
1720 (if (and (org-file-image-p
1723 org-export-latex-inline-image-extensions
)
1724 (or (get-text-property 0 'org-no-description
1726 (equal desc full-raw-path
)))
1728 (progn (when (string-match "\\(.+\\)::.+" raw-path
)
1729 (setq raw-path
(match-string 1 raw-path
)))
1730 (if (file-exists-p raw-path
)
1731 (concat type
"://" (expand-file-name raw-path
))
1732 (concat type
"://" (org-export-directory
1733 :LaTeX org-export-latex-options-plist
)
1735 ;; process with link inserting
1736 (apply 'delete-region remove
)
1738 (plist-get org-export-latex-options-plist
:inline-images
))
1739 ;; OK, we need to inline an image
1741 (org-export-latex-format-image raw-path caption label attr
)))
1744 (org-export-get-coderef-format path desc
)
1745 (cdr (assoc path org-export-code-refs
)))))
1746 (radiop (insert (format "\\hyperref[%s]{%s}"
1747 (org-solidify-link-text raw-path
) desc
)))
1749 (insert (format "\\hyperref[%s]{%s}"
1750 (org-remove-initial-hash
1751 (org-solidify-link-text raw-path
))
1754 (when (org-at-table-p)
1755 ;; There is a strange problem when we have a link in a table,
1756 ;; ampersands then cause a problem. I think this must be
1757 ;; a LaTeX issue, but we here implement a work-around anyway.
1758 (setq path
(org-export-latex-protect-amp path
)
1759 desc
(org-export-latex-protect-amp desc
)))
1760 (insert (format org-export-latex-hyperref-format path desc
)))
1761 (t (insert "\\texttt{" desc
"}")))))))
1764 (defun org-export-latex-format-image (path caption label attr
)
1765 "Format the image element, depending on user settings."
1766 (let (ind floatp wrapp placement figenv
)
1767 (setq floatp
(or caption label
))
1768 (setq ind
(org-get-text-property-any 0 'original-indentation path
))
1769 (when (and attr
(stringp attr
))
1770 (if (string-match "[ \t]*\\<wrap\\>" attr
)
1771 (setq wrapp t floatp nil attr
(replace-match "" t t attr
)))
1772 (if (string-match "[ \t]*\\<float\\>" attr
)
1773 (setq wrapp nil floatp t attr
(replace-match "" t t attr
))))
1777 (wrapp "{l}{0.5\\textwidth}")
1781 (when (and attr
(stringp attr
)
1782 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr
))
1783 (setq placement
(match-string 1 attr
)
1784 attr
(replace-match "" t t attr
)))
1785 (setq attr
(and attr
(org-trim attr
)))
1786 (when (or (not attr
) (= (length attr
) 0))
1787 (setq attr
(cond (floatp "width=0.7\\textwidth")
1788 (wrapp "width=0.48\\textwidth")
1792 (wrapp "\\begin{wrapfigure}%placement
1794 \\includegraphics[%attr]{%path}
1795 \\caption{%labelcmd%caption}
1797 (floatp "\\begin{figure}%placement
1799 \\includegraphics[%attr]{%path}
1800 \\caption{%labelcmd%caption}
1802 (t "\\includegraphics[%attr]{%path}")))
1805 (setq figenv
(mapconcat 'identity
(split-string figenv
"\n")
1806 (save-excursion (beginning-of-line 1)
1807 (looking-at "[ \t]*")
1808 (concat "\n" (match-string 0)))))
1810 (if (and (not label
) (not caption
)
1811 (string-match "^\\\\caption{.*\n" figenv
))
1812 (setq figenv
(replace-match "" t t figenv
)))
1817 (if (file-name-absolute-p path
)
1818 (expand-file-name path
)
1821 (cons "labelcmd" (if label
(format "\\label{%s}"
1823 (cons "caption" (or caption
""))
1824 (cons "placement" (or placement
""))))
1825 nil
'original-indentation ind
)))
1827 (defun org-export-latex-protect-amp (s)
1828 (while (string-match "\\([^\\\\]\\)\\(&\\)" s
)
1829 (setq s
(replace-match (concat (match-string 1 s
) "\\" (match-string 2 s
))
1833 (defun org-remove-initial-hash (s)
1834 (if (string-match "\\`#" s
)
1837 (defvar org-latex-entities
) ; defined below
1838 (defvar org-latex-entities-regexp
) ; defined below
1839 (defvar org-latex-entities-exceptions
) ; defined below
1841 (defun org-export-latex-preprocess (parameters)
1842 "Clean stuff in the LaTeX export."
1843 ;; Preserve line breaks
1844 (goto-char (point-min))
1845 (while (re-search-forward "\\\\\\\\" nil t
)
1846 (add-text-properties (match-beginning 0) (match-end 0)
1847 '(org-protected t
)))
1849 ;; Preserve latex environments
1850 (goto-char (point-min))
1851 (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t
)
1852 (let* ((start (progn (beginning-of-line) (point)))
1853 (end (and (re-search-forward
1854 (concat "^[ \t]*\\\\end{"
1855 (regexp-quote (match-string 1))
1859 (add-text-properties start end
'(org-protected t
))
1860 (goto-char (point-at-eol)))))
1862 ;; Preserve math snippets
1864 (let* ((matchers (plist-get org-format-latex-options
:matchers
))
1865 (re-list org-latex-regexps
)
1866 beg end re e m n block off
)
1867 ;; Check the different regular expressions
1868 (while (setq e
(pop re-list
))
1869 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
1870 block
(if (nth 3 e
) "\n\n" ""))
1871 (setq off
(if (member m
'("$" "$1")) 1 0))
1872 (when (and (member m matchers
) (not (equal m
"begin")))
1873 (goto-char (point-min))
1874 (while (re-search-forward re nil t
)
1875 (setq beg
(+ (match-beginning 0) off
) end
(- (match-end 0) 0))
1876 (add-text-properties beg end
'(org-protected t org-latex-math t
))))))
1878 ;; Convert LaTeX to \LaTeX{}
1879 (goto-char (point-min))
1880 (let ((case-fold-search nil
))
1881 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t
)
1883 (replace-match (org-export-latex-protect-string
1884 (concat (match-string 1) "\\LaTeX{}")) t t
))))
1886 ;; Convert blockquotes
1887 (goto-char (point-min))
1888 (while (search-forward "ORG-BLOCKQUOTE-START" nil t
)
1889 (org-replace-match-keep-properties "\\begin{quote}" t t
))
1890 (goto-char (point-min))
1891 (while (search-forward "ORG-BLOCKQUOTE-END" nil t
)
1892 (org-replace-match-keep-properties "\\end{quote}" t t
))
1895 (goto-char (point-min))
1896 (while (search-forward "ORG-VERSE-START" nil t
)
1897 (org-replace-match-keep-properties "\\begin{verse}" t t
)
1898 (beginning-of-line 2)
1899 (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
1900 (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
1901 (goto-char (match-end 1))
1902 (org-replace-match-keep-properties
1903 (org-export-latex-protect-string
1904 (concat "\\hspace*{1cm}" (match-string 2))) t t
)
1905 (beginning-of-line 1))
1906 (if (looking-at "[ \t]*$")
1907 (insert (org-export-latex-protect-string "\\vspace*{1em}"))
1908 (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
1911 (beginning-of-line 2))
1912 (and (looking-at "[ \t]*ORG-VERSE-END.*")
1913 (org-replace-match-keep-properties "\\end{verse}" t t
)))
1916 (goto-char (point-min))
1917 (while (search-forward "ORG-CENTER-START" nil t
)
1918 (org-replace-match-keep-properties "\\begin{center}" t t
))
1919 (goto-char (point-min))
1920 (while (search-forward "ORG-CENTER-END" nil t
)
1921 (org-replace-match-keep-properties "\\end{center}" t t
))
1923 (run-hooks 'org-export-latex-after-blockquotes-hook
)
1925 ;; Convert horizontal rules
1926 (goto-char (point-min))
1927 (while (re-search-forward "^----+.$" nil t
)
1929 (replace-match (org-export-latex-protect-string "\\hrule") t t
)))
1931 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
1932 (goto-char (point-min))
1935 "\\(?:\\[[^][\n]*?\\]\\)*"
1936 "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}")))
1937 (while (re-search-forward re nil t
)
1938 (unless (save-excursion (goto-char (match-beginning 0))
1939 (equal (char-after (point-at-bol)) ?
#))
1940 (add-text-properties (match-beginning 0) (match-end 0)
1941 '(org-protected t
)))))
1943 ;; Protect LaTeX entities
1944 (goto-char (point-min))
1946 (while (re-search-forward org-latex-entities-regexp nil t
)
1947 (if (setq a
(assoc (match-string 0) org-latex-entities-exceptions
))
1948 (replace-match (org-add-props (nth 1 a
) nil
'org-protected t
)
1950 (add-text-properties (match-beginning 0) (match-end 0)
1951 '(org-protected t
)))))
1953 ;; Replace radio links
1954 (goto-char (point-min))
1955 (while (re-search-forward
1956 (concat "<<<?" org-export-latex-all-targets-re
1957 ">>>?\\((INVISIBLE)\\)?") nil t
)
1958 (org-if-unprotected-at (+ (match-beginning 0) 2)
1961 (org-export-latex-protect-string
1962 (format "\\label{%s}" (save-match-data (org-solidify-link-text
1963 (match-string 1)))))
1964 (if (match-string 2) "" (match-string 1)))
1967 ;; Delete @<...> constructs
1968 ;; Thanks to Daniel Clemente for this regexp
1969 (goto-char (point-min))
1970 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t
)
1972 (replace-match "")))
1974 ;; When converting to LaTeX, replace footnotes
1975 ;; FIXME: don't protect footnotes from conversion
1976 (when (plist-get org-export-latex-options-plist
:footnotes
)
1977 (goto-char (point-min))
1978 (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t
)
1980 (when (and (save-match-data
1981 (save-excursion (beginning-of-line)
1982 (looking-at "[^:|#]")))
1983 (not (org-in-verbatim-emphasis)))
1984 (let ((foot-beg (match-beginning 0))
1985 (foot-end (match-end 0))
1986 (foot-prefix (match-string 0))
1987 footnote footnote-rpl
)
1989 (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix
))
1991 (replace-match (org-export-latex-protect-string
1992 (concat "$^{" (match-string 1) "}$")))
1994 (let ((end (save-excursion
1995 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t
)
1996 (match-beginning 0) (point-max)))))
1997 (setq footnote
(concat (org-trim (buffer-substring (point) end
))
1998 " ")) ; prevent last } being part of a link
1999 (delete-region (point) end
))
2000 (goto-char foot-beg
)
2001 (delete-region foot-beg foot-end
)
2002 (unless (null footnote
)
2003 (setq footnote-rpl
(format "\\footnote{%s}" footnote
))
2004 (add-text-properties 0 10 '(org-protected t
) footnote-rpl
)
2005 (add-text-properties (1- (length footnote-rpl
))
2006 (length footnote-rpl
)
2007 '(org-protected t
) footnote-rpl
)
2008 (insert footnote-rpl
)))
2011 ;; Remove footnote section tag for LaTeX
2012 (goto-char (point-min))
2013 (while (re-search-forward
2014 (concat "^" footnote-section-tag-regexp
) nil t
)
2016 (replace-match "")))))
2018 (defun org-export-latex-fix-inputenc ()
2019 "Set the codingsystem in inputenc to what the buffer is."
2020 (let* ((cs buffer-file-coding-system
)
2021 (opt (latexenc-coding-system-to-inputenc cs
)))
2023 ;; Translate if that is requested
2024 (setq opt
(or (cdr (assoc opt org-export-latex-inputenc-alist
)) opt
))
2025 ;; find the \usepackage statement and replace the option
2026 (goto-char (point-min))
2027 (while (re-search-forward "\\\\usepackage\\[\\(.*?\\)\\]{inputenc}"
2029 (goto-char (match-beginning 1))
2030 (delete-region (match-beginning 1) (match-end 1))
2036 (defun org-export-latex-lists ()
2037 "Convert plain text lists in current buffer into LaTeX lists."
2038 (goto-char (point-min))
2039 (while (re-search-forward org-list-beginning-re nil t
)
2042 (insert (org-list-to-latex (org-list-parse-list t
)
2043 org-export-latex-list-parameters
))
2046 (defconst org-latex-entities
2075 "\\begin{description}"
2076 "\\begin{enumerate}"
2080 "\\begin{flushleft}"
2081 "\\begin{flushright}"
2086 "\\begin{quotation}"
2091 "\\begin{thebibliography}"
2093 "\\begin{titlepage}"
2221 "A list of LaTeX commands to be protected when performing conversion.")
2223 (defvar org-latex-entities-exceptions nil
)
2225 (defconst org-latex-entities-regexp
2227 (dolist (x org-latex-entities
)
2229 (add-to-list 'org-latex-entities-exceptions x
)
2231 (if (string-match "[a-zA-Z]$" x
)
2234 (concat "\\(" (regexp-opt (nreverse names
)) "\\>\\)"
2235 "\\|\\(" (regexp-opt (nreverse rest
)) "\\)")))
2237 (provide 'org-export-latex
)
2238 (provide 'org-latex
)
2240 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
2242 ;;; org-latex.el ends here