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-emphasis-alist
201 '(("*" "\\textbf{%s}" nil
)
202 ("/" "\\emph{%s}" nil
)
203 ("_" "\\underline{%s}" nil
)
207 "Alist of LaTeX expressions to convert emphasis fontifiers.
208 Each element of the list is a list of three elements.
209 The first element is the character used as a marker for fontification.
210 The second element is a formatting string to wrap fontified text with.
211 If it is \"\\verb\", Org will automatically select a delimiter
212 character that is not in the string.
213 The third element decides whether to protect converted text from other
215 :group
'org-export-latex
218 (defcustom org-export-latex-title-command
"\\maketitle"
219 "The command used to insert the title just after \\begin{document}.
220 If this string contains the formatting specification \"%s\" then
221 it will be used as a formatting string, passing the title as an
223 :group
'org-export-latex
226 (defcustom org-export-latex-import-inbuffer-stuff nil
227 "Non-nil means define TeX macros for Org's inbuffer definitions.
228 For example \orgTITLE for #+TITLE."
229 :group
'org-export-latex
232 (defcustom org-export-latex-date-format
234 "Format string for \\date{...}."
235 :group
'org-export-latex
238 (defcustom org-export-latex-todo-keyword-markup
"\\textbf{%s}"
239 "Markup for TODO keywords, as a printf format.
240 This can be a single format for all keywords, a cons cell with separate
241 formats for not-done and done states, or an association list with setup
242 for individual keywords. If a keyword shows up for which there is no
243 markup defined, the first one in the association list will be used."
244 :group
'org-export-latex
246 (string :tag
"Default")
247 (cons :tag
"Distinguish undone and done"
248 (string :tag
"Not-DONE states")
249 (string :tag
"DONE states"))
250 (repeat :tag
"Per keyword markup"
252 (string :tag
"Keyword")
253 (string :tag
"Markup")))))
255 (defcustom org-export-latex-timestamp-markup
"\\textit{%s}"
256 "A printf format string to be applied to time stamps."
257 :group
'org-export-latex
260 (defcustom org-export-latex-timestamp-keyword-markup
"\\texttt{%s}"
261 "A printf format string to be applied to time stamps."
262 :group
'org-export-latex
265 (defcustom org-export-latex-tables-verbatim nil
266 "When non-nil, tables are exported verbatim."
267 :group
'org-export-latex
270 (defcustom org-export-latex-tables-centered t
271 "When non-nil, tables are exported in a center environment."
272 :group
'org-export-latex
275 (defcustom org-export-latex-tables-column-borders nil
276 "When non-nil, grouping columns can cause outer vertical lines in tables.
277 When nil, grouping causes only separation lines between groups."
278 :group
'org-export-latex
281 (defcustom org-export-latex-low-levels
'itemize
282 "How to convert sections below the current level of sectioning.
283 This is specified by the `org-export-headline-levels' option or the
284 value of \"H:\" in Org's #+OPTION line.
286 This can be either nil (skip the sections), `description', `itemize',
287 or `enumerate' (convert the sections as the corresponding list type), or
288 a string to be used instead of \\section{%s}. In this latter case,
289 the %s stands here for the inserted headline and is mandatory.
291 It may also be a list of three string to define a user-defined environment
292 that should be used. The first string should be the like
293 \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
294 to two occurrences of %s for the title and a label, respectively. The third
295 string should be like \"\\end{itemize\"."
296 :group
'org-export-latex
297 :type
'(choice (const :tag
"Ignore" nil
)
298 (const :tag
"Convert as descriptive list" description
)
299 (const :tag
"Convert as itemized list" itemize
)
300 (const :tag
"Convert as enumerated list" enumerate
)
301 (list :tag
"User-defined environment"
302 :value
("\\begin{itemize}" "\\end{itemize}" "\\item %s")
303 (string :tag
"Start")
305 (string :tag
"item"))
306 (string :tag
"Use a section string" :value
"\\subparagraph{%s}")))
308 (defcustom org-export-latex-list-parameters
309 '(:cbon
"$\\boxtimes$" :cboff
"$\\Box$")
310 "Parameters for the LaTeX list exporter.
311 These parameters will be passed on to `org-list-to-latex', which in turn
312 will pass them (combined with the LaTeX default list parameters) to
313 `org-list-to-generic'."
314 :group
'org-export-latex
317 (defcustom org-export-latex-verbatim-wrap
318 '("\\begin{verbatim}\n" .
"\\end{verbatim}\n")
319 "Environment to be wrapped around a fixed-width section in LaTeX export.
320 This is a cons with two strings, to be added before and after the
323 Defaults to \\begin{verbatim} and \\end{verbatim}."
324 :group
'org-export-translation
325 :group
'org-export-latex
326 :type
'(cons (string :tag
"Open")
327 (string :tag
"Close")))
329 (defcustom org-export-latex-listings nil
330 "Non-nil means, export source code using the listings package.
331 This package will fontify source code, possibly even with color.
332 If you want to use this, you also need to make LaTeX use the
333 listings package, and if you want to have color, the color
334 package. Just add these to `org-export-latex-packages-alist',
335 for example using customize, or with something like
338 (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
339 (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))"
340 :group
'org-export-latex
343 (defcustom org-export-latex-listings-langs
344 '((emacs-lisp "Lisp") (lisp "Lisp")
347 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
348 (html "HTML") (xml "XML")
349 (tex "TeX") (latex "TeX")
350 (shell-script "bash")
352 (ocaml "Caml") (caml "Caml")
354 "Alist mapping languages to their listing language counterpart.
355 The key is a symbol, the major mode symbol without the \"-mode\".
356 The value is the string that should be inserted as the language parameter
357 for the listings package. If the mode name and the listings name are
358 the same, the language does not need an entry in this list - but it does not
359 hurt if it is present."
360 :group
'org-export-latex
363 (symbol :tag
"Major mode ")
364 (string :tag
"Listings language"))))
366 (defcustom org-export-latex-remove-from-headlines
367 '(:todo nil
:priority nil
:tags nil
)
368 "A plist of keywords to remove from headlines. OBSOLETE.
369 Non-nil means remove this keyword type from the headline.
371 Don't remove the keys, just change their values.
373 Obsolete, this variable is no longer used. Use the separate
374 variables `org-export-with-todo-keywords', `org-export-with-priority',
375 and `org-export-with-tags' instead."
377 :group
'org-export-latex
)
379 (defcustom org-export-latex-image-default-option
"width=10em"
380 "Default option for images."
381 :group
'org-export-latex
384 (defcustom org-export-latex-inline-image-extensions
385 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
386 "Extensions of image files that can be inlined into LaTeX.
387 Note that the image extension *actually* allowed depend on the way the
388 LaTeX file is processed. When used with pdflatex, pdf, jpg and png images
389 are OK. When processing through dvi to Postscript, only ps and eps are
390 allowed. The default we use here encompasses both."
391 :group
'org-export-latex
392 :type
'(repeat (string :tag
"Extension")))
394 (defcustom org-export-latex-coding-system nil
395 "Coding system for the exported LaTex file."
396 :group
'org-export-latex
397 :type
'coding-system
)
399 (defgroup org-export-pdf nil
400 "Options for exporting Org-mode files to PDF, via LaTeX."
401 :tag
"Org Export PDF"
402 :group
'org-export-latex
405 (defcustom org-latex-to-pdf-process
406 '("pdflatex -interaction nonstopmode %s"
407 "pdflatex -interaction nonstopmode %s")
408 "Commands to process a LaTeX file to a PDF file.
409 This is a list of strings, each of them will be given to the shell
410 as a command. %s in the command will be replaced by the full file name, %b
411 by the file base name (i.e. without extension).
412 The reason why this is a list is that it usually takes several runs of
413 pdflatex, maybe mixed with a call to bibtex. Org does not have a clever
414 mechanism to detect which of these commands have to be run to get to a stable
415 result, and it also does not do any error checking.
417 Alternatively, this may be a Lisp function that does the processing, so you
418 could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
419 This function should accept the file name as its single argument."
420 :group
'org-export-pdf
421 :type
'(choice (repeat :tag
"Shell command sequence"
422 (string :tag
"Shell command"))
425 (defcustom org-export-pdf-logfiles
426 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
427 "The list of file extensions to consider as LaTeX logfiles."
428 :group
'org-export-pdf
429 :type
'(repeat (string :tag
"Extension")))
431 (defcustom org-export-pdf-remove-logfiles t
432 "Non-nil means, remove the logfiles produced by PDF production.
433 These are the .aux, .log, .out, and .toc files."
434 :group
'org-export-pdf
439 (defvar org-export-latex-after-initial-vars-hook nil
440 "Hook run before LaTeX export.
441 The exact moment is after the initial variables like org-export-latex-class
442 have been determined from the environment.")
444 (defvar org-export-latex-after-blockquotes-hook nil
445 "Hook run during LaTeX export, after blockquote, verse, center are done.")
447 (defvar org-export-latex-final-hook nil
448 "Hook run in the finalized LaTeX buffer.")
450 (defvar org-export-latex-after-save-hook nil
451 "Hook run in the finalized LaTeX buffer, after it has been saved.")
453 ;;; Autoload functions:
456 (defun org-export-as-latex-batch ()
457 "Call `org-export-as-latex', may be used in batch processing.
461 --load=$HOME/lib/emacs/org.el
462 --eval \"(setq org-export-headline-levels 2)\"
463 --visit=MyFile --funcall org-export-as-latex-batch"
464 (org-export-as-latex org-export-headline-levels
'hidden
))
467 (defun org-export-as-latex-to-buffer (arg)
468 "Call `org-export-as-latex` with output to a temporary buffer.
469 No file is created. The prefix ARG is passed through to `org-export-as-latex'."
471 (org-export-as-latex arg nil nil
"*Org LaTeX Export*")
472 (when org-export-show-temporary-export-buffer
473 (switch-to-buffer-other-window "*Org LaTeX Export*")))
476 (defun org-replace-region-by-latex (beg end
)
477 "Replace the region from BEG to END with its LaTeX export.
478 It assumes the region has `org-mode' syntax, and then convert it to
479 LaTeX. This can be used in any buffer. For example, you could
480 write an itemized list in `org-mode' syntax in an LaTeX buffer and
481 then use this command to convert it."
484 (save-window-excursion
486 (setq latex
(org-export-region-as-latex
488 (setq reg
(buffer-substring beg end
)
489 buf
(get-buffer-create "*Org tmp*"))
490 (with-current-buffer buf
494 (setq latex
(org-export-region-as-latex
495 (point-min) (point-max) t
'string
)))
497 (delete-region beg end
)
501 (defun org-export-region-as-latex (beg end
&optional body-only buffer
)
502 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
503 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
504 contents, and only produce the region of converted text, useful for
505 cut-and-paste operations.
506 If BUFFER is a buffer or a string, use/create that buffer as a target
507 of the converted LaTeX. If BUFFER is the symbol `string', return the
508 produced LaTeX as a string and leave no buffer behind. For example,
509 a Lisp program could call this function in the following way:
511 (setq latex (org-export-region-as-latex beg end t 'string))
513 When called interactively, the output buffer is selected, and shown
514 in a window. A non-interactive call will only return the buffer."
516 (when (interactive-p)
517 (setq buffer
"*Org LaTeX Export*"))
518 (let ((transient-mark-mode t
) (zmacs-regions t
)
520 (setq ext-plist
(plist-put ext-plist
:ignore-subtree-p t
))
522 (set-mark (point)) ;; to activate the region
524 (setq rtn
(org-export-as-latex
527 (if (fboundp 'deactivate-mark
) (deactivate-mark))
528 (if (and (interactive-p) (bufferp rtn
))
529 (switch-to-buffer-other-window rtn
)
533 (defun org-export-as-latex (arg &optional hidden ext-plist
534 to-buffer body-only pub-dir
)
535 "Export current buffer to a LaTeX file.
536 If there is an active region, export only the region. The prefix
537 ARG specifies how many levels of the outline should become
538 headlines. The default is 3. Lower levels will be exported
539 depending on `org-export-latex-low-levels'. The default is to
540 convert them as description lists.
541 HIDDEN is obsolete and does nothing.
542 EXT-PLIST is a property list with
543 external parameters overriding org-mode's default settings, but
544 still inferior to file-local settings. When TO-BUFFER is
545 non-nil, create a buffer with that name and export to that
546 buffer. If TO-BUFFER is the symbol `string', don't leave any
547 buffer behind but just return the resulting LaTeX as a string.
548 When BODY-ONLY is set, don't produce the file header and footer,
549 simply return the content of \begin{document}...\end{document},
550 without even the \begin{document} and \end{document} commands.
551 when PUB-DIR is set, use this as the publishing directory."
553 (run-hooks 'org-export-first-hook
)
555 ;; Make sure we have a file name when we need it.
556 (when (and (not (or to-buffer body-only
))
557 (not buffer-file-name
))
558 (if (buffer-base-buffer)
559 (org-set-local 'buffer-file-name
560 (with-current-buffer (buffer-base-buffer)
562 (error "Need a file name to be able to export")))
564 (message "Exporting to LaTeX...")
566 (let ((inhibit-read-only t
))
567 (remove-text-properties (point-min) (point-max)
568 '(:org-license-to-kill nil
))))
569 (org-update-radio-target-regexp)
570 (org-export-latex-set-initial-vars ext-plist arg
)
571 (setq org-export-opt-plist org-export-latex-options-plist
)
572 (org-install-letbind)
573 (run-hooks 'org-export-latex-after-initial-vars-hook
)
574 (let* ((wcf (current-window-configuration))
575 (opt-plist org-export-latex-options-plist
)
576 (region-p (org-region-active-p))
577 (rbeg (and region-p
(region-beginning)))
578 (rend (and region-p
(region-end)))
580 (if (plist-get opt-plist
:ignore-subtree-p
)
585 (and (org-at-heading-p)
586 (>= (org-end-of-subtree t t
) rend
))))))
587 (opt-plist (setq org-export-opt-plist
589 (org-export-add-subtree-options opt-plist rbeg
)
591 ;; Make sure the variable contains the updated values.
592 (org-export-latex-options-plist (setq org-export-opt-plist opt-plist
))
593 (title (or (and subtree-p
(org-export-get-title-from-subtree))
594 (plist-get opt-plist
:title
)
596 (plist-get opt-plist
:skip-before-1st-heading
))
597 (org-export-grab-title-from-buffer))
598 (file-name-sans-extension
599 (file-name-nondirectory buffer-file-name
))))
600 (filename (concat (file-name-as-directory
602 (org-export-directory :LaTeX ext-plist
)))
603 (file-name-sans-extension
605 (org-entry-get rbeg
"EXPORT_FILE_NAME" t
))
606 (file-name-nondirectory ;sans-extension
609 (filename (if (equal (file-truename filename
)
610 (file-truename buffer-file-name
))
611 (concat filename
".tex")
613 (buffer (if to-buffer
615 ((eq to-buffer
'string
) (get-buffer-create
616 "*Org LaTeX Export*"))
617 (t (get-buffer-create to-buffer
)))
618 (find-file-noselect filename
)))
619 (odd org-odd-levels-only
)
620 (header (org-export-latex-make-header title opt-plist
))
621 (skip (cond (subtree-p nil
)
623 (t (plist-get opt-plist
:skip-before-1st-heading
))))
624 (text (plist-get opt-plist
:text
))
625 (org-export-preprocess-hook
627 `(lambda () (org-set-local 'org-complex-heading-regexp
628 ,org-export-latex-complex-heading-re
))
629 org-export-preprocess-hook
))
630 (first-lines (if skip
"" (org-export-latex-first-lines
637 (if region-p rend
))))
638 (coding-system (and (boundp 'buffer-file-coding-system
)
639 buffer-file-coding-system
))
640 (coding-system-for-write (or org-export-latex-coding-system
642 (save-buffer-coding-system (or org-export-latex-coding-system
644 (region (buffer-substring
645 (if region-p
(region-beginning) (point-min))
646 (if region-p
(region-end) (point-max))))
648 (and text
(string-match "\\S-" text
)
649 (org-export-preprocess-string
654 :tags
(plist-get opt-plist
:tags
)
655 :priority
(plist-get opt-plist
:priority
)
656 :footnotes
(plist-get opt-plist
:footnotes
)
657 :drawers
(plist-get opt-plist
:drawers
)
658 :timestamps
(plist-get opt-plist
:timestamps
)
659 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
661 :skip-before-1st-heading skip
664 :LaTeX-fragments nil
)))
666 (org-export-preprocess-string
671 :tags
(plist-get opt-plist
:tags
)
672 :priority
(plist-get opt-plist
:priority
)
673 :footnotes
(plist-get opt-plist
:footnotes
)
674 :drawers
(plist-get opt-plist
:drawers
)
675 :timestamps
(plist-get opt-plist
:timestamps
)
676 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
677 :add-text
(if (eq to-buffer
'string
) nil text
)
678 :skip-before-1st-heading skip
679 :select-tags
(plist-get opt-plist
:select-tags
)
680 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
681 :LaTeX-fragments nil
)))
685 (org-install-letbind)
687 (and (fboundp 'set-buffer-file-coding-system
)
688 (set-buffer-file-coding-system coding-system-for-write
))
690 ;; insert the header and initial document commands
691 (unless (or (eq to-buffer
'string
) body-only
)
694 ;; insert text found in #+TEXT
695 (when (and text
(not (eq to-buffer
'string
)))
696 (insert (org-export-latex-content
697 text
'(lists tables fixed-width keywords
))
700 ;; insert lines before the first headline
702 (insert first-lines
))
704 ;; export the content of headlines
705 (org-export-latex-global
707 (insert string-for-export
)
708 (goto-char (point-min))
709 (when (re-search-forward "^\\(\\*+\\) " nil t
)
710 (let* ((asters (length (match-string 1)))
711 (level (if odd
(- asters
2) (- asters
1))))
712 (setq org-export-latex-add-level
713 (if odd
(1- (/ (1+ asters
) 2)) (1- asters
)))
714 (org-export-latex-parse-global level odd
)))))
717 (unless body-only
(insert "\n\\end{document}"))
719 ;; Attach description terms to the \item macro
720 (goto-char (point-min))
721 (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t
)
722 (delete-region (match-beginning 1) (match-end 1)))
724 ;; Relocate the table of contents
725 (goto-char (point-min))
726 (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t
)
727 (goto-char (point-min))
728 (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t
)
730 (goto-char (point-min))
731 (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t
)
732 (replace-match "\\tableofcontents" t t
)))
734 ;; Cleanup forced line ends in items where they are not needed
735 (goto-char (point-min))
736 (while (re-search-forward
737 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin"
739 (delete-region (match-beginning 1) (match-end 1)))
740 (goto-char (point-min))
741 (while (re-search-forward
742 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*"
744 (if (looking-at "[\n \t]+")
745 (replace-match "\n")))
747 (run-hooks 'org-export-latex-final-hook
)
748 (or to-buffer
(save-buffer))
749 (run-hooks 'org-export-latex-after-save-hook
)
750 (goto-char (point-min))
751 (or (org-export-push-to-kill-ring "LaTeX")
752 (message "Exporting to LaTeX...done"))
754 (if (eq to-buffer
'string
)
755 (prog1 (buffer-substring (point-min) (point-max))
756 (kill-buffer (current-buffer)))
758 (set-window-configuration wcf
))))
761 (defun org-export-as-pdf (arg &optional hidden ext-plist
762 to-buffer body-only pub-dir
)
763 "Export as LaTeX, then process through to PDF."
765 (message "Exporting to PDF...")
766 (let* ((wconfig (current-window-configuration))
767 (lbuf (org-export-as-latex arg hidden ext-plist
768 to-buffer body-only pub-dir
))
769 (file (buffer-file-name lbuf
))
770 (base (file-name-sans-extension (buffer-file-name lbuf
)))
771 (pdffile (concat base
".pdf"))
772 (cmds org-latex-to-pdf-process
)
773 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
774 (bibtex-p (with-current-buffer lbuf
776 (goto-char (point-min))
777 (re-search-forward "\\\\bibliography{" nil t
))))
779 (with-current-buffer outbuf
(erase-buffer))
780 (message "Processing LaTeX file...")
781 (if (and cmds
(symbolp cmds
))
784 (setq cmd
(pop cmds
))
785 (while (string-match "%b" cmd
)
786 (setq cmd
(replace-match
788 (shell-quote-argument base
))
790 (while (string-match "%s" cmd
)
791 (setq cmd
(replace-match
793 (shell-quote-argument file
))
795 (shell-command cmd outbuf outbuf
)))
796 (message "Processing LaTeX file...done")
797 (if (not (file-exists-p pdffile
))
798 (error "PDF file was not produced")
799 (set-window-configuration wconfig
)
800 (when org-export-pdf-remove-logfiles
801 (dolist (ext org-export-pdf-logfiles
)
802 (setq file
(concat base
"." ext
))
803 (and (file-exists-p file
) (delete-file file
))))
804 (message "Exporting to PDF...done")
808 (defun org-export-as-pdf-and-open (arg)
809 "Export as LaTeX, then process through to PDF, and open."
811 (let ((pdffile (org-export-as-pdf arg
)))
813 (org-open-file pdffile
)
814 (error "PDF file was not produced"))))
816 ;;; Parsing functions:
818 (defun org-export-latex-parse-global (level odd
)
819 "Parse the current buffer recursively, starting at LEVEL.
820 If ODD is non-nil, assume the buffer only contains odd sections.
821 Return a list reflecting the document structure."
823 (goto-char (point-min))
824 (let* ((cnt 0) output
825 (depth org-export-latex-sectioning-depth
))
826 (while (org-re-search-forward-unprotected
827 (concat "^\\(\\(?:\\*\\)\\{"
828 (number-to-string (+ (if odd
2 1) level
))
830 ;; make sure that there is no upper heading
834 (org-re-search-forward-unprotected
835 (concat "^\\(\\(?:\\*\\)\\{"
836 (number-to-string level
)
837 "\\}\\) \\(.*\\)$") nil t
)))) t
)
839 (let* ((pos (match-beginning 0))
840 (heading (match-string 2))
841 (nlevel (if odd
(/ (+ 3 level
) 2) (1+ level
))))
846 (if (org-re-search-forward-unprotected
847 (concat "^\\(\\(?:\\*\\)\\{"
848 (number-to-string (+ (if odd
2 1) level
))
849 "\\}\\) \\(.*\\)$") nil t
)
852 (goto-char (point-min))
860 `(heading .
,heading
)
861 `(content .
,(org-export-latex-parse-content))
862 `(subcontent .
,(org-export-latex-parse-subcontent
867 (defun org-export-latex-parse-content ()
868 "Extract the content of a section."
870 (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t
)
871 (progn (beginning-of-line) (point))
873 (buffer-substring beg end
)))
875 (defun org-export-latex-parse-subcontent (level odd
)
876 "Extract the subcontent of a section at LEVEL.
877 If ODD Is non-nil, assume subcontent only contains odd sections."
878 (if (not (org-re-search-forward-unprotected
879 (concat "^\\(\\(?:\\*\\)\\{"
880 (number-to-string (+ (if odd
4 2) level
))
883 nil
; subcontent is nil
884 (org-export-latex-parse-global (+ (if odd
2 1) level
) odd
)))
886 ;;; Rendering functions:
887 (defun org-export-latex-global (content)
888 "Export CONTENT to LaTeX.
889 CONTENT is an element of the list produced by
890 `org-export-latex-parse-global'."
891 (if (eq (car content
) 'subcontent
)
892 (mapc 'org-export-latex-sub
(cdr content
))
893 (org-export-latex-sub (car content
))))
895 (defun org-export-latex-sub (subcontent)
896 "Export the list SUBCONTENT to LaTeX.
897 SUBCONTENT is an alist containing information about the headline
899 (let ((num (plist-get org-export-latex-options-plist
:section-numbers
)))
900 (mapc (lambda(x) (org-export-latex-subcontent x num
)) subcontent
)))
902 (defun org-export-latex-subcontent (subcontent num
)
903 "Export each cell of SUBCONTENT to LaTeX.
904 If NUM, export sections as numerical sections."
905 (let* ((heading (cdr (assoc 'heading subcontent
)))
906 (level (- (cdr (assoc 'level subcontent
))
907 org-export-latex-add-level
))
908 (occur (number-to-string (cdr (assoc 'occur subcontent
))))
909 (content (cdr (assoc 'content subcontent
)))
910 (subcontent (cadr (assoc 'subcontent subcontent
)))
911 (label (org-get-text-property-any 0 'target heading
))
912 (label-list (cons label
(cdr (assoc label
913 org-export-target-aliases
))))
914 (sectioning org-export-latex-sectioning
)
915 (depth org-export-latex-sectioning-depth
)
916 main-heading sub-heading
)
917 (when (symbolp (car sectioning
))
918 (setq sectioning
(funcall (car sectioning
) level heading
))
920 (setq heading
(car sectioning
)
921 sectioning
(cdr sectioning
)
922 ;; target property migh have changed...
923 label
(org-get-text-property-any 0 'target heading
)
924 label-list
(cons label
(cdr (assoc label
925 org-export-target-aliases
)))))
926 (if sectioning
(setq sectioning
(make-list 10 sectioning
)))
927 (setq depth
(if sectioning
10000 0)))
928 (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading
)
929 (setq main-heading
(substring heading
0 (match-beginning 0))
930 sub-heading
(substring heading
(match-end 0))))
931 (setq heading
(org-export-latex-fontify-headline heading
)
932 sub-heading
(and sub-heading
933 (org-export-latex-fontify-headline sub-heading
))
934 main-heading
(and main-heading
935 (org-export-latex-fontify-headline main-heading
)))
939 (let* ((sec (nth (1- level
) sectioning
))
941 (if (consp (cdr sec
))
942 (setq start
(nth (if num
0 2) sec
)
943 end
(nth (if num
1 3) sec
))
944 (setq start
(if num
(car sec
) (cdr sec
))))
945 (insert (format start
(if main-heading main-heading heading
)
946 (or sub-heading
"")))
949 (insert (mapconcat (lambda (l) (format "\\label{%s}" l
))
950 label-list
"\n") "\n"))
951 (insert (org-export-latex-content content
))
952 (cond ((stringp subcontent
) (insert subcontent
))
954 (while (org-looking-back "\n\n") (backward-delete-char 1))
955 (org-export-latex-sub subcontent
)))
956 (when (and end
(string-match "[^ \t]" end
))
957 (let ((hook (org-get-text-property-any 0 'org-insert-hook end
)))
958 (and (functionp hook
) (funcall hook
)))
960 ;; At a level under the hl option: we can drop this subsection
962 (cond ((eq org-export-latex-low-levels
'description
)
963 (if (string-match "% ends low level$"
964 (buffer-substring (point-at-bol 0) (point)))
965 (delete-region (point-at-bol 0) (point))
966 (insert "\\begin{description}\n"))
967 (insert (format "\n\\item[%s]%s~\n"
969 (if label
(format "\\label{%s}" label
) "")))
970 (insert (org-export-latex-content content
))
971 (cond ((stringp subcontent
) (insert subcontent
))
972 ((listp subcontent
) (org-export-latex-sub subcontent
)))
973 (insert "\\end{description} % ends low level\n"))
974 ((memq org-export-latex-low-levels
'(itemize enumerate
))
975 (if (string-match "% ends low level$"
976 (buffer-substring (point-at-bol 0) (point)))
977 (delete-region (point-at-bol 0) (point))
978 (insert (format "\\begin{%s}\n"
979 (symbol-name org-export-latex-low-levels
))))
980 (insert (format "\n\\item %s\\\\\n%s%%"
982 (if label
(format "\\label{%s}" label
) "")))
983 (insert (org-export-latex-content content
))
984 (cond ((stringp subcontent
) (insert subcontent
))
985 ((listp subcontent
) (org-export-latex-sub subcontent
)))
986 (insert (format "\\end{%s} %% ends low level\n"
987 (symbol-name org-export-latex-low-levels
))))
989 ((listp org-export-latex-low-levels
)
990 (if (string-match "% ends low level$"
991 (buffer-substring (point-at-bol 0) (point)))
992 (delete-region (point-at-bol 0) (point))
993 (insert (car org-export-latex-low-levels
) "\n"))
994 (insert (format (nth 2 org-export-latex-low-levels
)
996 (if label
(format "\\label{%s}" label
) "")))
997 (insert (org-export-latex-content content
))
998 (cond ((stringp subcontent
) (insert subcontent
))
999 ((listp subcontent
) (org-export-latex-sub subcontent
)))
1000 (insert (nth 1 org-export-latex-low-levels
)
1001 " %% ends low level\n"))
1003 ((stringp org-export-latex-low-levels
)
1004 (insert (format org-export-latex-low-levels heading
) "\n")
1005 (when label
(insert (format "\\label{%s}\n" label
)))
1006 (insert (org-export-latex-content content
))
1007 (cond ((stringp subcontent
) (insert subcontent
))
1008 ((listp subcontent
) (org-export-latex-sub subcontent
)))))))))
1010 ;;; Exporting internals:
1011 (defun org-export-latex-set-initial-vars (ext-plist level
)
1012 "Store org local variables required for LaTeX export.
1013 EXT-PLIST is an optional additional plist.
1014 LEVEL indicates the default depth for export."
1015 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
1016 org-export-latex-done-keywords org-done-keywords
1017 org-export-latex-not-done-keywords org-not-done-keywords
1018 org-export-latex-complex-heading-re org-complex-heading-regexp
1019 org-export-latex-display-custom-times org-display-custom-times
1020 org-export-latex-all-targets-re
1021 (org-make-target-link-regexp (org-all-targets))
1022 org-export-latex-options-plist
1023 (org-combine-plists (org-default-export-plist) ext-plist
1024 (org-infile-export-plist))
1025 org-export-latex-class
1026 (or (and (org-region-active-p)
1028 (goto-char (region-beginning))
1029 (and (looking-at org-complex-heading-regexp
)
1030 (org-entry-get nil
"LaTeX_CLASS" 'selective
))))
1034 (goto-char (point-min))
1035 (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t
)
1037 (plist-get org-export-latex-options-plist
:latex-class
)
1038 org-export-latex-default-class
)
1039 org-export-latex-class-options
1040 (or (and (org-region-active-p)
1042 (goto-char (region-beginning))
1043 (and (looking-at org-complex-heading-regexp
)
1044 (org-entry-get nil
"LaTeX_CLASS_OPTIONS" 'selective
))))
1048 (goto-char (point-min))
1049 (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t
)
1051 (plist-get org-export-latex-options-plist
:latex-class-options
))
1052 org-export-latex-class
1053 (or (car (assoc org-export-latex-class org-export-latex-classes
))
1054 (error "No definition for class `%s' in `org-export-latex-classes'"
1055 org-export-latex-class
))
1056 org-export-latex-header
1057 (cadr (assoc org-export-latex-class org-export-latex-classes
))
1058 org-export-latex-sectioning
1059 (cddr (assoc org-export-latex-class org-export-latex-classes
))
1060 org-export-latex-sectioning-depth
1063 (plist-get org-export-latex-options-plist
:headline-levels
))
1064 (sec-depth (length org-export-latex-sectioning
)))
1065 (if (> hl-levels sec-depth
) sec-depth hl-levels
))))
1066 (when (and org-export-latex-class-options
1067 (string-match "\\S-" org-export-latex-class-options
)
1068 (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?"
1069 org-export-latex-header
))
1070 (setq org-export-latex-header
1071 (concat (substring org-export-latex-header
0 (match-end 1))
1072 org-export-latex-class-options
1073 (substring org-export-latex-header
(match-end 0))))))
1075 (defvar org-export-latex-format-toc-function
1076 'org-export-latex-format-toc-default
1077 "The function formatting returning the string to createthe table of contents.
1078 The function mus take one parameter, the depth of the table of contents.")
1080 (defun org-export-latex-make-header (title opt-plist
)
1081 "Make the LaTeX header and return it as a string.
1082 TITLE is the current title from the buffer or region.
1083 OPT-PLIST is the options plist for current buffer."
1084 (let ((toc (plist-get opt-plist
:table-of-contents
))
1085 (author (plist-get opt-plist
:author
)))
1087 (if (plist-get opt-plist
:time-stamp-file
)
1088 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1089 ;; insert LaTeX custom header
1090 (org-export-apply-macros-in-string org-export-latex-header
)
1092 ;; insert information on LaTeX packages
1093 (when org-export-latex-packages-alist
1095 (mapconcat (lambda(p)
1096 (if (equal "" (car p
))
1097 (format "\\usepackage{%s}" (cadr p
))
1098 (format "\\usepackage[%s]{%s}"
1100 org-export-latex-packages-alist
"\n")
1102 ;; insert additional commands in the header
1103 (org-export-apply-macros-in-string
1104 (plist-get opt-plist
:latex-header-extra
))
1105 (org-export-apply-macros-in-string org-export-latex-append-header
)
1109 ;; convert the title
1110 (org-export-latex-content
1111 title
'(lists tables fixed-width keywords
)))
1112 ;; insert author info
1113 (if (plist-get opt-plist
:author-info
)
1114 (format "\\author{%s}\n"
1115 (org-export-latex-fontify-headline (or author user-full-name
)))
1116 (format "%%\\author{%s}\n"
1117 (or author user-full-name
)))
1119 (format "\\date{%s}\n"
1121 (or (plist-get opt-plist
:date
)
1122 org-export-latex-date-format
)))
1123 ;; beginning of the document
1124 "\n\\begin{document}\n\n"
1125 ;; insert the title command
1126 (when (string-match "\\S-" title
)
1127 (if (string-match "%s" org-export-latex-title-command
)
1128 (format org-export-latex-title-command title
)
1129 org-export-latex-title-command
))
1131 ;; table of contents
1132 (when (and org-export-with-toc
1133 (plist-get opt-plist
:section-numbers
))
1134 (funcall org-export-latex-format-toc-function
1135 (cond ((numberp toc
)
1136 (min toc
(plist-get opt-plist
:headline-levels
)))
1137 (toc (plist-get opt-plist
:headline-levels
))))))))
1139 (defun org-export-latex-format-toc-default (depth)
1141 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1144 (defun org-export-latex-first-lines (opt-plist &optional beg end
)
1145 "Export the first lines before first headline.
1146 If BEG is non-nil, it is the beginning of the region.
1147 If END is non-nil, it is the end of the region."
1149 (goto-char (or beg
(point-min)))
1151 (end (if (re-search-forward "^\\*+ " end t
)
1152 (goto-char (match-beginning 0))
1153 (goto-char (or end
(point-max))))))
1155 (org-export-latex-content
1156 (org-export-preprocess-string
1157 (buffer-substring pt end
)
1162 :skip-before-1st-heading nil
1163 :LaTeX-fragments nil
1164 :timestamps
(plist-get opt-plist
:timestamps
)
1165 :footnotes
(plist-get opt-plist
:footnotes
)))
1167 (let ((inhibit-read-only t
))
1168 (add-text-properties pt
(max pt
(1- end
))
1169 '(:org-license-to-kill t
))))))))
1171 (defvar org-export-latex-header-defs nil
1172 "The header definitions that might be used in the LaTeX body.")
1173 (defvar org-export-latex-header-defs-re nil
1174 "The header definitions that might be used in the LaTeX body.")
1176 (defun org-export-latex-content (content &optional exclude-list
)
1177 "Convert CONTENT string to LaTeX.
1178 Don't perform conversions that are in EXCLUDE-LIST. Recognized
1179 conversion types are: quotation-marks, emphasis, sub-superscript,
1180 links, keywords, lists, tables, fixed-width"
1183 (unless (memq 'timestamps exclude-list
)
1184 (org-export-latex-time-stamps))
1185 (unless (memq 'quotation-marks exclude-list
)
1186 (org-export-latex-quotation-marks))
1187 (unless (memq 'emphasis exclude-list
)
1188 (when (plist-get org-export-latex-options-plist
:emphasize
)
1189 (org-export-latex-fontify)))
1190 (unless (memq 'sub-superscript exclude-list
)
1191 (org-export-latex-special-chars
1192 (plist-get org-export-latex-options-plist
:sub-superscript
)))
1193 (unless (memq 'links exclude-list
)
1194 (org-export-latex-links))
1195 (unless (memq 'keywords exclude-list
)
1196 (org-export-latex-keywords))
1197 (unless (memq 'lists exclude-list
)
1198 (org-export-latex-lists))
1199 (unless (memq 'tables exclude-list
)
1200 (org-export-latex-tables
1201 (plist-get org-export-latex-options-plist
:tables
)))
1202 (unless (memq 'fixed-width exclude-list
)
1203 (org-export-latex-fixed-width
1204 (plist-get org-export-latex-options-plist
:fixed-width
)))
1206 (buffer-substring (point-min) (point-max))))
1208 (defun org-export-latex-protect-string (s)
1209 "Add the org-protected property to string S."
1210 (add-text-properties 0 (length s
) '(org-protected t
) s
) s
)
1212 (defun org-export-latex-protect-char-in-string (char-list string
)
1213 "Add org-protected text-property to char from CHAR-LIST in STRING."
1217 (goto-char (point-min))
1218 (while (re-search-forward (regexp-opt char-list
) nil t
)
1219 (add-text-properties (match-beginning 0)
1220 (match-end 0) '(org-protected t
)))
1223 (defun org-export-latex-keywords-maybe (&optional remove-list
)
1224 "Maybe remove keywords depending on rules in REMOVE-LIST."
1225 (goto-char (point-min))
1226 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1
"\\|"))
1227 (case-fold-search nil
)
1228 (todo-markup org-export-latex-todo-keyword-markup
)
1230 ;; convert TODO keywords
1231 (when (re-search-forward (concat "^\\(" re-todo
"\\)") nil t
)
1232 (if (plist-get remove-list
:todo
)
1235 ((stringp todo-markup
) todo-markup
)
1236 ((and (consp todo-markup
) (stringp (car todo-markup
)))
1237 (if (member (match-string 1) org-export-latex-done-keywords
)
1238 (cdr todo-markup
) (car todo-markup
)))
1239 (t (cdr (or (assoc (match-string 1) todo-markup
)
1240 (car todo-markup
))))))
1241 (replace-match (format fmt
(match-string 1)) t t
)))
1242 ;; convert priority string
1243 (when (re-search-forward "\\[\\\\#.\\]" nil t
)
1244 (if (plist-get remove-list
:priority
)
1246 (replace-match (format "\\textbf{%s}" (match-string 0)) t t
)))
1248 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t
)
1249 (if (or (not org-export-with-tags
)
1250 (plist-get remove-list
:tags
))
1253 (org-export-latex-protect-string
1254 (format "\\textbf{%s}"
1256 (replace-regexp-in-string
1257 "_" "\\\\_" (match-string 0)))))
1260 (defun org-export-latex-fontify-headline (string)
1261 "Fontify special words in STRING."
1263 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
1264 ;; the beginning of the buffer - inserting "\n" is safe here though.
1265 (insert "\n" string
)
1266 (goto-char (point-min))
1267 (let ((re (concat "\\\\[a-zA-Z]+\\(?:"
1270 (org-create-multibrace-regexp "{" "}" 3))))
1271 (while (re-search-forward re nil t
)
1272 (unless (save-excursion (goto-char (match-beginning 0))
1273 (equal (char-after (point-at-bol)) ?
#))
1274 (add-text-properties (match-beginning 0) (match-end 0)
1275 '(org-protected t
)))))
1276 (when (plist-get org-export-latex-options-plist
:emphasize
)
1277 (org-export-latex-fontify))
1278 (org-export-latex-keywords-maybe)
1279 (org-export-latex-special-chars
1280 (plist-get org-export-latex-options-plist
:sub-superscript
))
1281 (org-export-latex-links)
1282 (org-trim (buffer-string))))
1284 (defun org-export-latex-time-stamps ()
1285 "Format time stamps."
1286 (goto-char (point-min))
1287 (let ((org-display-custom-times org-export-latex-display-custom-times
))
1288 (while (re-search-forward org-ts-regexp-both nil t
)
1289 (org-if-unprotected-at (1- (point))
1291 (org-export-latex-protect-string
1292 (format org-export-latex-timestamp-markup
1293 (substring (org-translate-time (match-string 0)) 1 -
1)))
1296 (defun org-export-latex-quotation-marks ()
1297 "Export quotation marks depending on language conventions."
1298 (let* ((lang (plist-get org-export-latex-options-plist
:language
))
1299 (quote-rpl (if (equal lang
"fr")
1300 '(("\\(\\s-\\)\"" "«~")
1301 ("\\(\\S-\\)\"" "~»")
1302 ("\\(\\s-\\)'" "`"))
1303 '(("\\(\\s-\\|[[(]\\)\"" "``")
1304 ("\\(\\S-\\)\"" "''")
1305 ("\\(\\s-\\|(\\)'" "`")))))
1306 (mapc (lambda(l) (goto-char (point-min))
1307 (while (re-search-forward (car l
) nil t
)
1308 (let ((rpl (concat (match-string 1)
1309 (org-export-latex-protect-string
1310 (copy-sequence (cadr l
))))))
1311 (org-if-unprotected-1
1312 (replace-match rpl t t
))))) quote-rpl
)))
1314 (defun org-export-latex-special-chars (sub-superscript)
1315 "Export special characters to LaTeX.
1316 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
1317 See the `org-export-latex.el' code for a complete conversion table."
1318 (goto-char (point-min))
1320 (goto-char (point-min))
1321 (while (re-search-forward c nil t
)
1322 ;; Put the point where to check for org-protected
1323 (unless (get-text-property (match-beginning 2) 'org-protected
)
1324 (cond ((member (match-string 2) '("\\$" "$"))
1325 (if (equal (match-string 2) "\\$")
1327 (replace-match "\\$" t t
)))
1328 ((member (match-string 2) '("&" "%" "#"))
1329 (if (equal (match-string 1) "\\")
1330 (replace-match (match-string 2) t t
)
1331 (replace-match (concat (match-string 1) "\\"
1332 (match-string 2)) t t
)))
1333 ((equal (match-string 2) "...")
1335 (concat (match-string 1)
1336 (org-export-latex-protect-string "\\ldots{}")) t t
))
1337 ((equal (match-string 2) "~")
1338 (cond ((equal (match-string 1) "\\") nil
)
1339 ((eq 'org-link
(get-text-property 0 'face
(match-string 2)))
1340 (replace-match (concat (match-string 1) "\\~") t t
))
1342 (org-export-latex-protect-string
1343 (concat (match-string 1) "\\~{}")) t t
))))
1344 ((member (match-string 2) '("{" "}"))
1345 (unless (save-match-data (org-inside-latex-math-p))
1346 (if (equal (match-string 1) "\\")
1347 (replace-match (match-string 2) t t
)
1348 (replace-match (concat (match-string 1) "\\"
1349 (match-string 2)) t t
)))))
1350 (unless (save-match-data (org-inside-latex-math-p))
1351 (cond ((equal (match-string 2) "\\")
1352 (replace-match (or (save-match-data
1353 (org-export-latex-treat-backslash-char
1355 (or (match-string 3) "")))
1357 ((member (match-string 2) '("_" "^"))
1358 (replace-match (or (save-match-data
1359 (org-export-latex-treat-sub-super-char
1363 (match-string 3))) "") t t
)
1364 (backward-char 1)))))))
1365 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
1366 "\\(\\(\\\\?\\$\\)\\)"
1367 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
1368 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
1369 "\\(.\\|^\\)\\(&\\)"
1370 "\\(.\\|^\\)\\(#\\)"
1371 "\\(.\\|^\\)\\(%\\)"
1372 "\\(.\\|^\\)\\({\\)"
1373 "\\(.\\|^\\)\\(}\\)"
1374 "\\(.\\|^\\)\\(~\\)"
1375 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
1376 ;; (?\< . "\\textless{}")
1377 ;; (?\> . "\\textgreater{}")
1380 (defun org-inside-latex-math-p ()
1381 (get-text-property (point) 'org-latex-math
))
1383 (defun org-export-latex-treat-sub-super-char
1384 (subsup char string-before string-after
)
1385 "Convert the \"_\" and \"^\" characters to LaTeX.
1386 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
1387 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
1388 (cond ((equal string-before
"\\")
1389 (concat string-before char string-after
))
1390 ((and (string-match "\\S-+" string-after
))
1391 ;; this is part of a math formula
1392 (cond ((eq 'org-link
(get-text-property 0 'face char
))
1393 (concat string-before
"\\" char string-after
))
1394 ((save-match-data (org-inside-latex-math-p))
1396 (cond ((eq 1 (length string-after
))
1397 (concat string-before char string-after
))
1398 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after
)
1399 (format "%s%s{%s}" string-before char
1400 (match-string 1 string-after
))))))
1401 ((and (> (length string-after
) 1)
1403 (and (equal subsup
'{}) (eq (string-to-char string-after
) ?\
{)))
1404 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after
))
1405 (org-export-latex-protect-string
1406 (format "%s$%s{%s}$" string-before char
1407 (if (and (> (match-end 1) (1+ (match-beginning 1)))
1408 (not (equal (substring string-after
0 2) "{\\")))
1409 (concat "\\mathrm{" (match-string 1 string-after
) "}")
1410 (match-string 1 string-after
)))))
1411 ((eq subsup t
) (concat string-before
"$" char string-after
"$"))
1412 (t (org-export-latex-protect-string
1413 (concat string-before
"\\" char
"{}" string-after
)))))
1414 (t (org-export-latex-protect-string
1415 (concat string-before
"\\" char
"{}" string-after
)))))
1417 (defun org-export-latex-treat-backslash-char (string-before string-after
)
1418 "Convert the \"$\" special character to LaTeX.
1419 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
1420 (cond ((member (list string-after
) org-html-entities
)
1421 ;; backslash is part of a special entity (like "\alpha")
1422 (concat string-before
"$\\"
1423 (or (cdar (member (list string-after
) org-html-entities
))
1425 ((and (not (string-match "^[ \n\t]" string-after
))
1426 (not (string-match "[ \t]\\'\\|^" string-before
)))
1427 ;; backslash is inside a word
1428 (org-export-latex-protect-string
1429 (concat string-before
"\\textbackslash{}" string-after
)))
1430 ((not (or (equal string-after
"")
1431 (string-match "^[ \t\n]" string-after
)))
1432 ;; backslash might escape a character (like \#) or a user TeX
1433 ;; macro (like \setcounter)
1434 (org-export-latex-protect-string
1435 (concat string-before
"\\" string-after
)))
1436 ((and (string-match "^[ \t\n]" string-after
)
1437 (string-match "[ \t\n]\\'" string-before
))
1438 ;; backslash is alone, convert it to $\backslash$
1439 (org-export-latex-protect-string
1440 (concat string-before
"\\textbackslash{}" string-after
)))
1441 (t (org-export-latex-protect-string
1442 (concat string-before
"\\textbackslash{}" string-after
)))))
1444 (defun org-export-latex-keywords ()
1445 "Convert special keywords to LaTeX."
1446 (goto-char (point-min))
1447 (while (re-search-forward org-export-latex-special-keyword-regexp nil t
)
1448 (replace-match (format org-export-latex-timestamp-keyword-markup
1449 (match-string 0)) t t
)
1451 (beginning-of-line 1)
1452 (unless (looking-at ".*\\\\newline[ \t]*$")
1454 (insert "\\newline")))))
1456 (defun org-export-latex-fixed-width (opt)
1457 "When OPT is non-nil convert fixed-width sections to LaTeX."
1458 (goto-char (point-min))
1459 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t
)
1461 (progn (goto-char (match-beginning 0))
1462 (insert "\\begin{verbatim}\n")
1463 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1464 (replace-match (concat (match-string 1)
1465 (match-string 2)) t t
)
1467 (insert "\\end{verbatim}\n\n"))
1468 (progn (goto-char (match-beginning 0))
1469 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1470 (replace-match (concat "%" (match-string 1)
1471 (match-string 2)) t t
)
1475 (defvar org-table-last-alignment
) ; defined in org-table.el
1476 (defvar org-table-last-column-widths
) ; defined in org-table.el
1477 (declare-function orgtbl-to-latex
"org-table" (table params
) t
)
1478 (defun org-export-latex-tables (insert)
1479 "Convert tables to LaTeX and INSERT it."
1480 (goto-char (point-min))
1481 (while (re-search-forward "^\\([ \t]*\\)|" nil t
)
1482 (org-if-unprotected-at (1- (point))
1484 (let* ((beg (org-table-begin))
1485 (end (org-table-end))
1486 (raw-table (buffer-substring beg end
))
1487 (org-table-last-alignment (copy-sequence org-table-last-alignment
))
1488 (org-table-last-column-widths (copy-sequence
1489 org-table-last-column-widths
))
1490 fnum fields line lines olines gr colgropen line-fmt align
1491 caption label attr floatp longtblp
)
1492 (if org-export-latex-tables-verbatim
1493 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1494 "\\end{verbatim}\n")))
1495 (apply 'delete-region
(list beg end
))
1496 (insert (org-export-latex-protect-string tbl
)))
1498 (setq caption
(org-find-text-property-in-string
1499 'org-caption raw-table
)
1500 attr
(org-find-text-property-in-string
1501 'org-attributes raw-table
)
1502 label
(org-find-text-property-in-string
1503 'org-label raw-table
)
1504 longtblp
(and attr
(stringp attr
)
1505 (string-match "\\<longtable\\>" attr
))
1506 align
(and attr
(stringp attr
)
1507 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr
)
1508 (match-string 1 attr
))
1509 floatp
(or caption label
))
1510 (setq lines
(org-split-string raw-table
"\n"))
1511 (apply 'delete-region
(list beg end
))
1512 (when org-export-table-remove-special-lines
1513 (setq lines
(org-table-clean-before-export lines
'maybe-quoted
)))
1514 (when org-table-clean-did-remove-column
1515 (pop org-table-last-alignment
)
1516 (pop org-table-last-column-widths
))
1517 ;; make a formatting string to reflect alignment
1519 (while (and (not line-fmt
) (setq line
(pop olines
)))
1520 (unless (string-match "^[ \t]*|-" line
)
1521 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
1522 (setq fnum
(make-vector (length fields
) 0))
1526 (setq gr
(pop org-table-colgroup-info
))
1528 (cond ((eq gr
:start
)
1529 (prog1 (if colgropen
"|" "|")
1530 (setq colgropen t
)))
1532 (prog1 (if colgropen
"|" "|")
1533 (setq colgropen nil
)))
1535 (if (memq gr
'(:end
:startend
))
1536 (progn (setq colgropen nil
) "|")
1539 ;; fix double || in line-fmt
1540 (setq line-fmt
(replace-regexp-in-string "||" "|" line-fmt
))
1541 ;; maybe remove the first and last "|"
1542 (when (and (not org-export-latex-tables-column-borders
)
1543 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt
))
1544 (setq line-fmt
(match-string 2 line-fmt
)))
1547 (setq align
(apply 'format
1549 (mapcar (lambda (x) (if x
"r" "l"))
1550 org-table-last-alignment
)))))
1551 ;; prepare the table to send to orgtbl-to-latex
1555 (or (and (string-match "[ \t]*|-+" elem
) 'hline
)
1556 (org-split-string (org-trim elem
) "|")))
1559 (insert (org-export-latex-protect-string
1562 (concat "\\begin{longtable}{" align
"}\n")
1563 (if floatp
"\\begin{table}[htb]\n"))
1567 (if label
(concat "\\\label{" label
"}") "")
1569 (if (and longtblp caption
) "\\\\\n" "\n")
1570 (if (and org-export-latex-tables-centered
(not longtblp
))
1571 "\\begin{center}\n")
1572 (if (not longtblp
) (concat "\\begin{tabular}{" align
"}\n"))
1575 `(:tstart nil
:tend nil
1576 :hlend
,(if longtblp
1580 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
1582 \\endlastfoot" (length org-table-last-alignment
))
1584 (if (not longtblp
) (concat "\n\\end{tabular}"))
1585 (if longtblp
"\n" (if org-export-latex-tables-centered
1586 "\n\\end{center}\n" "\n"))
1589 (if floatp
"\\end{table}"))))
1592 (defun org-export-latex-fontify ()
1593 "Convert fontification to LaTeX."
1594 (goto-char (point-min))
1595 (while (re-search-forward org-emph-re nil t
)
1596 ;; The match goes one char after the *string*, except at the end of a line
1597 (let ((emph (assoc (match-string 3)
1598 org-export-latex-emphasis-alist
))
1599 (beg (match-beginning 0))
1603 (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
1605 (unless (or (and (get-text-property (- (point) 2) 'org-protected
)
1606 (not (get-text-property
1607 (- (point) 2) 'org-verbatim-emph
)))
1609 (goto-char (match-beginning 1))
1611 (and (org-at-table-p)
1613 "[|\n]" (buffer-substring beg end
))))))
1614 (setq s
(match-string 4))
1615 (setq rpl
(concat (match-string 1)
1616 (org-export-latex-emph-format (cadr emph
)
1620 (setq rpl
(org-export-latex-protect-string rpl
))
1622 (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl
)
1624 (add-text-properties (match-beginning 1) (match-end 1)
1625 '(org-protected t
) rpl
)
1626 (add-text-properties (match-beginning 3) (match-end 3)
1627 '(org-protected t
) rpl
)))))
1628 (replace-match rpl t t
)))
1631 (defvar org-export-latex-use-verb nil
)
1632 (defun org-export-latex-emph-format (format string
)
1633 "Format an emphasis string and handle the \\verb special case."
1634 (when (equal format
"\\verb")
1636 (if org-export-latex-use-verb
1637 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1639 (loop for i from
0 to
(1- (length ll
)) do
1640 (if (not (string-match (regexp-quote (substring ll i
(1+ i
)))
1643 (setq format
(concat "\\verb" (substring ll i
(1+ i
))
1644 "%s" (substring ll i
(1+ i
))))
1645 (throw 'exit nil
))))))
1647 (trans '(("\\" .
"\\textbackslash{}")
1648 ("~" .
"\\textasciitilde{}")
1649 ("^" .
"\\textasciicircum{}")))
1651 (while (string-match "[\\{}$%&_#~^]" string
)
1652 (setq char
(match-string 0 string
))
1653 (if (> (match-beginning 0) 0)
1654 (setq rtn
(concat rtn
(substring string
1655 0 (match-beginning 0)))))
1656 (setq string
(substring string
(1+ (match-beginning 0))))
1657 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
1658 rtn
(concat rtn char
)))
1659 (setq string
(concat rtn string
) format
"\\texttt{%s}")))))
1660 (format format string
))
1662 (defun org-export-latex-links ()
1663 ;; Make sure to use the LaTeX hyperref and graphicx package
1664 ;; or send some warnings.
1665 "Convert links to LaTeX."
1666 (goto-char (point-min))
1667 (while (re-search-forward org-bracket-link-analytic-regexp
++ nil t
)
1668 (org-if-unprotected-1
1669 (goto-char (match-beginning 0))
1670 (let* ((re-radio org-export-latex-all-targets-re
)
1671 (remove (list (match-beginning 0) (match-end 0)))
1672 (raw-path (org-extract-attributes (match-string 3)))
1673 (full-raw-path (concat (match-string 1) raw-path
))
1674 (desc (match-string 5))
1675 (type (or (match-string 2)
1676 (if (or (file-name-absolute-p raw-path
)
1677 (string-match "^\\.\\.?/" raw-path
))
1679 (coderefp (equal type
"coderef"))
1680 (caption (org-find-text-property-in-string 'org-caption raw-path
))
1681 (attr (or (org-find-text-property-in-string 'org-attributes raw-path
)
1682 (plist-get org-export-latex-options-plist
:latex-image-options
)))
1683 (label (org-find-text-property-in-string 'org-label raw-path
))
1685 ;; define the path of the link
1687 ((member type
'("coderef"))
1689 ((member type
'("http" "https" "ftp"))
1690 (concat type
":" raw-path
))
1691 ((and re-radio
(string-match re-radio raw-path
))
1693 ((equal type
"mailto")
1694 (concat type
":" raw-path
))
1695 ((equal type
"file")
1696 (if (and (org-file-image-p
1699 org-export-latex-inline-image-extensions
)
1700 (or (get-text-property 0 'org-no-description
1702 (equal desc full-raw-path
)))
1704 (progn (when (string-match "\\(.+\\)::.+" raw-path
)
1705 (setq raw-path
(match-string 1 raw-path
)))
1706 (if (file-exists-p raw-path
)
1707 (concat type
"://" (expand-file-name raw-path
))
1708 (concat type
"://" (org-export-directory
1709 :LaTeX org-export-latex-options-plist
)
1711 ;; process with link inserting
1712 (apply 'delete-region remove
)
1714 (plist-get org-export-latex-options-plist
:inline-images
))
1715 ;; OK, we need to inline an image
1717 (org-export-latex-format-image raw-path caption label attr
)))
1720 (org-export-get-coderef-format path desc
)
1721 (cdr (assoc path org-export-code-refs
)))))
1722 (radiop (insert (format "\\hyperref[%s]{%s}"
1723 (org-solidify-link-text raw-path
) desc
)))
1725 (insert (format "\\hyperref[%s]{%s}"
1726 (org-remove-initial-hash
1727 (org-solidify-link-text raw-path
))
1730 (when (org-at-table-p)
1731 ;; There is a strange problem when we have a link in a table,
1732 ;; ampersands then cause a problem. I think this must be
1733 ;; a LaTeX issue, but we here implement a work-around anyway.
1734 (setq path
(org-export-latex-protect-amp path
)
1735 desc
(org-export-latex-protect-amp desc
)))
1736 (insert (format "\\href{%s}{%s}" path desc
)))
1737 (t (insert "\\texttt{" desc
"}")))))))
1740 (defun org-export-latex-format-image (path caption label attr
)
1741 "Format the image element, depending on user settings."
1742 (let (ind floatp wrapp placement figenv
)
1743 (setq floatp
(or caption label
))
1744 (setq ind
(org-get-text-property-any 0 'original-indentation path
))
1745 (when (and attr
(stringp attr
))
1746 (if (string-match "[ \t]*\\<wrap\\>" attr
)
1747 (setq wrapp t floatp nil attr
(replace-match "" t t attr
)))
1748 (if (string-match "[ \t]*\\<float\\>" attr
)
1749 (setq wrapp nil floatp t attr
(replace-match "" t t attr
))))
1753 (wrapp "{l}{0.5\\textwidth}")
1757 (when (and attr
(stringp attr
)
1758 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr
))
1759 (setq placement
(match-string 1 attr
)
1760 attr
(replace-match "" t t attr
)))
1761 (setq attr
(and attr
(org-trim attr
)))
1762 (when (or (not attr
) (= (length attr
) 0))
1763 (setq attr
(cond (floatp "width=0.7\\textwidth")
1764 (wrapp "width=0.48\\textwidth")
1768 (wrapp "\\begin{wrapfigure}%placement
1770 \\includegraphics[%attr]{%path}
1771 \\caption{%labelcmd%caption}
1773 (floatp "\\begin{figure}%placement
1775 \\includegraphics[%attr]{%path}
1776 \\caption{%labelcmd%caption}
1778 (t "\\includegraphics[%attr]{%path}")))
1781 (setq figenv
(mapconcat 'identity
(split-string figenv
"\n")
1782 (save-excursion (beginning-of-line 1)
1783 (looking-at "[ \t]*")
1784 (concat "\n" (match-string 0)))))
1786 (if (and (not label
) (not caption
)
1787 (string-match "^\\\\caption{.*\n" figenv
))
1788 (setq figenv
(replace-match "" t t figenv
)))
1793 (if (file-name-absolute-p path
)
1794 (expand-file-name path
)
1797 (cons "labelcmd" (if label
(format "\\label{%s}"
1799 (cons "caption" (or caption
""))
1800 (cons "placement" (or placement
""))))
1801 nil
'original-indentation ind
)))
1803 (defun org-export-latex-protect-amp (s)
1804 (while (string-match "\\([^\\\\]\\)\\(&\\)" s
)
1805 (setq s
(replace-match (concat (match-string 1 s
) "\\" (match-string 2 s
))
1809 (defun org-remove-initial-hash (s)
1810 (if (string-match "\\`#" s
)
1813 (defvar org-latex-entities
) ; defined below
1814 (defvar org-latex-entities-regexp
) ; defined below
1815 (defvar org-latex-entities-exceptions
) ; defined below
1817 (defun org-export-latex-preprocess (parameters)
1818 "Clean stuff in the LaTeX export."
1819 ;; Preserve line breaks
1820 (goto-char (point-min))
1821 (while (re-search-forward "\\\\\\\\" nil t
)
1822 (add-text-properties (match-beginning 0) (match-end 0)
1823 '(org-protected t
)))
1825 ;; Preserve latex environments
1826 (goto-char (point-min))
1827 (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t
)
1828 (let* ((start (progn (beginning-of-line) (point)))
1829 (end (and (re-search-forward
1830 (concat "^[ \t]*\\\\end{"
1831 (regexp-quote (match-string 1))
1835 (add-text-properties start end
'(org-protected t
))
1836 (goto-char (point-at-eol)))))
1838 ;; Preserve math snippets
1840 (let* ((matchers (plist-get org-format-latex-options
:matchers
))
1841 (re-list org-latex-regexps
)
1842 beg end re e m n block off
)
1843 ;; Check the different regular expressions
1844 (while (setq e
(pop re-list
))
1845 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
1846 block
(if (nth 3 e
) "\n\n" ""))
1847 (setq off
(if (member m
'("$" "$1")) 1 0))
1848 (when (and (member m matchers
) (not (equal m
"begin")))
1849 (goto-char (point-min))
1850 (while (re-search-forward re nil t
)
1851 (setq beg
(+ (match-beginning 0) off
) end
(- (match-end 0) 0))
1852 (add-text-properties beg end
'(org-protected t org-latex-math t
))))))
1854 ;; Convert LaTeX to \LaTeX{}
1855 (goto-char (point-min))
1856 (let ((case-fold-search nil
))
1857 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t
)
1859 (replace-match (org-export-latex-protect-string
1860 (concat (match-string 1) "\\LaTeX{}")) t t
))))
1862 ;; Convert blockquotes
1863 (goto-char (point-min))
1864 (while (search-forward "ORG-BLOCKQUOTE-START" nil t
)
1865 (org-replace-match-keep-properties "\\begin{quote}" t t
))
1866 (goto-char (point-min))
1867 (while (search-forward "ORG-BLOCKQUOTE-END" nil t
)
1868 (org-replace-match-keep-properties "\\end{quote}" t t
))
1871 (goto-char (point-min))
1872 (while (search-forward "ORG-VERSE-START" nil t
)
1873 (org-replace-match-keep-properties "\\begin{verse}" t t
)
1874 (beginning-of-line 2)
1875 (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
1876 (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
1877 (goto-char (match-end 1))
1878 (org-replace-match-keep-properties
1879 (org-export-latex-protect-string
1880 (concat "\\hspace*{1cm}" (match-string 2))) t t
)
1881 (beginning-of-line 1))
1882 (if (looking-at "[ \t]*$")
1883 (insert (org-export-latex-protect-string "\\vspace*{1em}"))
1884 (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
1887 (beginning-of-line 2))
1888 (and (looking-at "[ \t]*ORG-VERSE-END.*")
1889 (org-replace-match-keep-properties "\\end{verse}" t t
)))
1892 (goto-char (point-min))
1893 (while (search-forward "ORG-CENTER-START" nil t
)
1894 (org-replace-match-keep-properties "\\begin{center}" t t
))
1895 (goto-char (point-min))
1896 (while (search-forward "ORG-CENTER-END" nil t
)
1897 (org-replace-match-keep-properties "\\end{center}" t t
))
1899 (run-hooks 'org-export-latex-after-blockquotes-hook
)
1901 ;; Convert horizontal rules
1902 (goto-char (point-min))
1903 (while (re-search-forward "^----+.$" nil t
)
1905 (replace-match (org-export-latex-protect-string "\\hrule") t t
)))
1907 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
1908 (goto-char (point-min))
1911 "\\(?:\\[[^][\n]*?\\]\\)*"
1912 "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}")))
1913 (while (re-search-forward re nil t
)
1914 (unless (save-excursion (goto-char (match-beginning 0))
1915 (equal (char-after (point-at-bol)) ?
#))
1916 (add-text-properties (match-beginning 0) (match-end 0)
1917 '(org-protected t
)))))
1919 ;; Protect LaTeX entities
1920 (goto-char (point-min))
1922 (while (re-search-forward org-latex-entities-regexp nil t
)
1923 (if (setq a
(assoc (match-string 0) org-latex-entities-exceptions
))
1924 (replace-match (org-add-props (nth 1 a
) nil
'org-protected t
)
1926 (add-text-properties (match-beginning 0) (match-end 0)
1927 '(org-protected t
)))))
1929 ;; Replace radio links
1930 (goto-char (point-min))
1931 (while (re-search-forward
1932 (concat "<<<?" org-export-latex-all-targets-re
1933 ">>>?\\((INVISIBLE)\\)?") nil t
)
1934 (org-if-unprotected-at (+ (match-beginning 0) 2)
1937 (org-export-latex-protect-string
1938 (format "\\label{%s}" (save-match-data (org-solidify-link-text
1939 (match-string 1)))))
1940 (if (match-string 2) "" (match-string 1)))
1943 ;; Delete @<...> constructs
1944 ;; Thanks to Daniel Clemente for this regexp
1945 (goto-char (point-min))
1946 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t
)
1948 (replace-match "")))
1950 ;; When converting to LaTeX, replace footnotes
1951 ;; FIXME: don't protect footnotes from conversion
1952 (when (plist-get org-export-latex-options-plist
:footnotes
)
1953 (goto-char (point-min))
1954 (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t
)
1956 (when (and (save-match-data
1957 (save-excursion (beginning-of-line)
1958 (looking-at "[^:|#]")))
1959 (not (org-in-verbatim-emphasis)))
1960 (let ((foot-beg (match-beginning 0))
1961 (foot-end (match-end 0))
1962 (foot-prefix (match-string 0))
1963 footnote footnote-rpl
)
1965 (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix
))
1967 (replace-match (org-export-latex-protect-string
1968 (concat "$^{" (match-string 1) "}$")))
1970 (let ((end (save-excursion
1971 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t
)
1972 (match-beginning 0) (point-max)))))
1973 (setq footnote
(concat (org-trim (buffer-substring (point) end
))
1974 " ")) ; prevent last } being part of a link
1975 (delete-region (point) end
))
1976 (goto-char foot-beg
)
1977 (delete-region foot-beg foot-end
)
1978 (unless (null footnote
)
1979 (setq footnote-rpl
(format "\\footnote{%s}" footnote
))
1980 (add-text-properties 0 10 '(org-protected t
) footnote-rpl
)
1981 (add-text-properties (1- (length footnote-rpl
))
1982 (length footnote-rpl
)
1983 '(org-protected t
) footnote-rpl
)
1984 (insert footnote-rpl
)))
1987 ;; Remove footnote section tag for LaTeX
1988 (goto-char (point-min))
1989 (while (re-search-forward
1990 (concat "^" footnote-section-tag-regexp
) nil t
)
1992 (replace-match "")))))
1996 (defun org-export-latex-lists ()
1997 "Convert plain text lists in current buffer into LaTeX lists."
1998 (goto-char (point-min))
1999 (while (re-search-forward org-list-beginning-re nil t
)
2002 (insert (org-list-to-latex (org-list-parse-list t
)
2003 org-export-latex-list-parameters
))
2006 (defconst org-latex-entities
2035 "\\begin{description}"
2036 "\\begin{enumerate}"
2040 "\\begin{flushleft}"
2041 "\\begin{flushright}"
2046 "\\begin{quotation}"
2051 "\\begin{thebibliography}"
2053 "\\begin{titlepage}"
2181 "A list of LaTeX commands to be protected when performing conversion.")
2183 (defvar org-latex-entities-exceptions nil
)
2185 (defconst org-latex-entities-regexp
2187 (dolist (x org-latex-entities
)
2189 (add-to-list 'org-latex-entities-exceptions x
)
2191 (if (string-match "[a-zA-Z]$" x
)
2194 (concat "\\(" (regexp-opt (nreverse names
)) "\\>\\)"
2195 "\\|\\(" (regexp-opt (nreverse rest
)) "\\)")))
2197 (provide 'org-export-latex
)
2198 (provide 'org-latex
)
2200 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
2202 ;;; org-latex.el ends here