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'
54 (defvar org-export-latex-class nil
)
55 (defvar org-export-latex-header nil
)
56 (defvar org-export-latex-append-header nil
)
57 (defvar org-export-latex-options-plist nil
)
58 (defvar org-export-latex-todo-keywords-1 nil
)
59 (defvar org-export-latex-complex-heading-re nil
)
60 (defvar org-export-latex-not-done-keywords nil
)
61 (defvar org-export-latex-done-keywords nil
)
62 (defvar org-export-latex-display-custom-times nil
)
63 (defvar org-export-latex-all-targets-re nil
)
64 (defvar org-export-latex-add-level
0)
65 (defvar org-export-latex-sectioning
"")
66 (defvar org-export-latex-sectioning-depth
0)
67 (defvar org-export-latex-special-keyword-regexp
68 (concat "\\<\\(" org-scheduled-string
"\\|"
69 org-deadline-string
"\\|"
70 org-closed-string
"\\)")
71 "Regexp matching special time planning keywords plus the time after it.")
73 (defvar latexp
) ; dynamically scoped from org.el
74 (defvar re-quote
) ; dynamically scoped from org.el
75 (defvar commentsp
) ; dynamically scoped from org.el
79 (defgroup org-export-latex nil
80 "Options for exporting Org-mode files to LaTeX."
81 :tag
"Org Export LaTeX"
84 (defcustom org-export-latex-default-class
"article"
85 "The default LaTeX class."
86 :group
'org-export-latex
87 :type
'(string :tag
"LaTeX class"))
89 (defcustom org-export-latex-classes
91 "\\documentclass[11pt]{article}
92 \\usepackage[utf8]{inputenc}
93 \\usepackage[T1]{fontenc}
94 \\usepackage{graphicx}
95 \\usepackage{longtable}
97 \\usepackage{hyperref}"
98 ("\\section{%s}" .
"\\section*{%s}")
99 ("\\subsection{%s}" .
"\\subsection*{%s}")
100 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
101 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
102 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
104 "\\documentclass[11pt]{report}
105 \\usepackage[utf8]{inputenc}
106 \\usepackage[T1]{fontenc}
107 \\usepackage{graphicx}
108 \\usepackage{longtable}
110 \\usepackage{hyperref}"
111 ("\\part{%s}" .
"\\part*{%s}")
112 ("\\chapter{%s}" .
"\\chapter*{%s}")
113 ("\\section{%s}" .
"\\section*{%s}")
114 ("\\subsection{%s}" .
"\\subsection*{%s}")
115 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
117 "\\documentclass[11pt]{book}
118 \\usepackage[utf8]{inputenc}
119 \\usepackage[T1]{fontenc}
120 \\usepackage{graphicx}
121 \\usepackage{longtable}
123 \\usepackage{hyperref}"
124 ("\\part{%s}" .
"\\part*{%s}")
125 ("\\chapter{%s}" .
"\\chapter*{%s}")
126 ("\\section{%s}" .
"\\section*{%s}")
127 ("\\subsection{%s}" .
"\\subsection*{%s}")
128 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
129 "Alist of LaTeX classes and associated header and structure.
130 If #+LaTeX_CLASS is set in the buffer, use its value and the
131 associated information. Here is the structure of each cell:
135 (numbered-section . unnumbered-section\)
138 A %s formatter is mandatory in each section string and will be
139 replaced by the title of the section.
141 Instead of a cons cell (numbered . unnumbered), you can also provide a list
144 (numbered-open numbered-close)
148 (numbered-open numbered-close unnumbered-open unnumbered-close)
150 providing opening and closing strings for an environment that should
151 represent the document section. The opening clause should have a %s
152 to represent the section title."
153 :group
'org-export-latex
155 (list (string :tag
"LaTeX class")
156 (string :tag
"LaTeX header")
157 (repeat :tag
"Levels" :inline t
160 (string :tag
"numbered")
161 (string :tag
"unnumbered)"))
162 (list :tag
"Environment"
163 (string :tag
"Opening (numbered) ")
164 (string :tag
"Closing (numbered) ")
165 (string :tag
"Opening (unnumbered)")
166 (string :tag
"Closing (unnumbered)")))))))
168 (defcustom org-export-latex-emphasis-alist
169 '(("*" "\\textbf{%s}" nil
)
170 ("/" "\\emph{%s}" nil
)
171 ("_" "\\underline{%s}" nil
)
175 "Alist of LaTeX expressions to convert emphasis fontifiers.
176 Each element of the list is a list of three elements.
177 The first element is the character used as a marker for fontification.
178 The second element is a formatting string to wrap fontified text with.
179 If it is \"\\verb\", Org will automatically select a deimiter
180 character that is not in the string.
181 The third element decides whether to protect converted text from other
183 :group
'org-export-latex
186 (defcustom org-export-latex-title-command
"\\maketitle"
187 "The command used to insert the title just after \\begin{document}.
188 If this string contains the formatting specification \"%s\" then
189 it will be used as a formatting string, passing the title as an
191 :group
'org-export-latex
194 (defcustom org-export-latex-import-inbuffer-stuff nil
195 "Non-nil means define TeX macros for Org's inbuffer definitions.
196 For example \orgTITLE for #+TITLE."
197 :group
'org-export-latex
200 (defcustom org-export-latex-date-format
202 "Format string for \\date{...}."
203 :group
'org-export-latex
206 (defcustom org-export-latex-todo-keyword-markup
"\\textbf{%s}"
207 "Markup for TODO keywords, as a printf format.
208 This can be a single format for all keywords, a cons cell with separate
209 formats for not-done and done states, or an association list with setup
210 for individual keywords. If a keyword shows up for which there is no
211 markup defined, the first one in the association list will be used."
212 :group
'org-export-latex
214 (string :tag
"Default")
215 (cons :tag
"Distinguish undone and done"
216 (string :tag
"Not-DONE states")
217 (string :tag
"DONE states"))
218 (repeat :tag
"Per keyword markup"
220 (string :tag
"Keyword")
221 (string :tag
"Markup")))))
223 (defcustom org-export-latex-timestamp-markup
"\\textit{%s}"
224 "A printf format string to be applied to time stamps."
225 :group
'org-export-latex
228 (defcustom org-export-latex-timestamp-keyword-markup
"\\texttt{%s}"
229 "A printf format string to be applied to time stamps."
230 :group
'org-export-latex
233 (defcustom org-export-latex-tables-verbatim nil
234 "When non-nil, tables are exported verbatim."
235 :group
'org-export-latex
238 (defcustom org-export-latex-tables-centered t
239 "When non-nil, tables are exported in a center environment."
240 :group
'org-export-latex
243 (defcustom org-export-latex-tables-column-borders nil
244 "When non-nil, grouping columns can cause outer vertical lines in tables.
245 When nil, grouping causes only separation lines between groups."
246 :group
'org-export-latex
249 (defcustom org-export-latex-packages-alist nil
250 "Alist of packages to be inserted in the header.
251 Each cell is of the format \( \"option\" . \"package\" \)."
252 :group
'org-export-latex
255 (string :tag
"option")
256 (string :tag
"package"))))
258 (defcustom org-export-latex-low-levels
'itemize
259 "How to convert sections below the current level of sectioning.
260 This is specified by the `org-export-headline-levels' option or the
261 value of \"H:\" in Org's #+OPTION line.
263 This can be either nil (skip the sections), `description', `itemize',
264 or `enumerate' (convert the sections as the corresponding list type), or
265 a string to be used instead of \\section{%s}. In this latter case,
266 the %s stands here for the inserted headline and is mandatory.
268 It may also be a list of three string to define a user-defined environment
269 that should be used. The first string should be the like
270 \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
271 to two occurrences of %s for the title and a lable, respectively. The third
272 string should be like \"\\end{itemize\"."
273 :group
'org-export-latex
274 :type
'(choice (const :tag
"Ignore" nil
)
275 (const :tag
"Convert as descriptive list" description
)
276 (const :tag
"Convert as itemized list" itemize
)
277 (const :tag
"Convert as enumerated list" enumerate
)
278 (list :tag
"User-defined environment"
279 :value
("\\begin{itemize}" "\\end{itemize}" "\\item %s")
280 (string :tag
"Start")
282 (string :tag
"item"))
283 (string :tag
"Use a section string" :value
"\\subparagraph{%s}")))
285 (defcustom org-export-latex-list-parameters
286 '(:cbon
"\\texttt{[X]}" :cboff
"\\texttt{[ ]}")
287 "Parameters for the LaTeX list exporter.
288 These parameters will be passed on to `org-list-to-latex', which in turn
289 will pass them (combined with the LaTeX default list parameters) to
290 `org-list-to-generic'."
291 :group
'org-export-latex
294 (defcustom org-export-latex-verbatim-wrap
295 '("\\begin{verbatim}\n" .
"\\end{verbatim}\n")
296 "Environment to be wrapped around a fixed-width section in LaTeX export.
297 This is a cons with two strings, to be added before and after the
300 Defaults to \\begin{verbatim} and \\end{verbatim}."
301 :group
'org-export-translation
302 :group
'org-export-latex
303 :type
'(cons (string :tag
"Open")
304 (string :tag
"Close")))
306 (defcustom org-export-latex-listings nil
307 "Non-nil means, export source code using the listings package.
308 This package will fontify source code, possibly even with color.
309 If you want to use this, you also need to make LaTeX use the
310 listings package, and if you want to have color, the color
311 package. Just add these to `org-export-latex-packages-alist',
312 for example using customize, or with something like
315 (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
316 (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))"
317 :group
'org-export-latex
320 (defcustom org-export-latex-listings-langs
321 '((emacs-lisp "Lisp") (lisp "Lisp")
324 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
325 (html "HTML") (xml "XML")
326 (tex "TeX") (latex "TeX")
327 (shell-script "bash")
329 (ocaml "Caml") (caml "Caml")
331 "Alist mapping languages to their listing language counterpart.
332 The key is a symbol, the major mode symbol without the \"-mode\".
333 The value is the string that should be inserted as the language parameter
334 for the listings package. If the mode name and the listings name are
335 the same, the language does not need an entry in this list - but it does not
336 hurt if it is present."
337 :group
'org-export-latex
340 (symbol :tag
"Major mode ")
341 (string :tag
"Listings language"))))
343 (defcustom org-export-latex-remove-from-headlines
344 '(:todo nil
:priority nil
:tags nil
)
345 "A plist of keywords to remove from headlines. OBSOLETE.
346 Non-nil means remove this keyword type from the headline.
348 Don't remove the keys, just change their values.
350 Obsolete, this variable is no longer used. Use the separate
351 variables `org-export-with-todo-keywords', `org-export-with-priority',
352 and `org-export-with-tags' instead."
354 :group
'org-export-latex
)
356 (defcustom org-export-latex-image-default-option
"width=10em"
357 "Default option for images."
358 :group
'org-export-latex
361 (defcustom org-export-latex-inline-image-extensions
362 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
363 "Extensions of image files that can be inlined into LaTeX.
364 Note that the image extension *actually* allowed depend on the way the
365 LaTeX file is processed. When used with pdflatex, pdf, jpg and png images
366 are OK. When processing through dvi to Postscript, only ps and eps are
367 allowed. The default we use here encompasses both."
368 :group
'org-export-latex
369 :type
'(repeat (string :tag
"Extension")))
371 (defcustom org-export-latex-coding-system nil
372 "Coding system for the exported LaTex file."
373 :group
'org-export-latex
374 :type
'coding-system
)
376 (defgroup org-export-pdf nil
377 "Options for exporting Org-mode files to PDF, via LaTeX."
378 :tag
"Org Export LaTeX"
379 :group
'org-export-latex
382 (defcustom org-latex-to-pdf-process
383 '("pdflatex -interaction nonstopmode %s"
384 "pdflatex -interaction nonstopmode %s")
385 "Commands to process a LaTeX file to a PDF file.
386 This is a list of strings, each of them will be given to the shell
387 as a command. %s in the command will be replaced by the full file name, %b
388 by the file base name (i.e. without extension).
389 The reason why this is a list is that it usually takes several runs of
390 pdflatex, maybe mixed with a call to bibtex. Org does not have a clever
391 mechanism to detect whihc of these commands have to be run to get to a stable
392 result, and it also does not do any error checking.
394 Alternatively, this may be a Lisp function that does the processing, so you
395 could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
396 THis function should accept the file name as its single argument."
397 :group
'org-export-latex
398 :type
'(choice (repeat :tag
"Shell command sequence"
399 (string :tag
"Shell command"))
402 (defcustom org-export-pdf-remove-logfiles t
403 "Non-nil means, remove the logfiles produced by PDF production.
404 These are the .aux, .log, .out, and .toc files."
405 :group
'org-export-pdf
410 (defvar org-export-latex-after-blockquotes-hook nil
411 "Hook run during LaTeX export, after blockquote, verse, center are done.")
413 ;;; Autoload functions:
416 (defun org-export-as-latex-batch ()
417 "Call `org-export-as-latex', may be used in batch processing.
421 --load=$HOME/lib/emacs/org.el
422 --eval \"(setq org-export-headline-levels 2)\"
423 --visit=MyFile --funcall org-export-as-latex-batch"
424 (org-export-as-latex org-export-headline-levels
'hidden
))
427 (defun org-export-as-latex-to-buffer (arg)
428 "Call `org-export-as-latex` with output to a temporary buffer.
429 No file is created. The prefix ARG is passed through to `org-export-as-latex'."
431 (org-export-as-latex arg nil nil
"*Org LaTeX Export*")
432 (when org-export-show-temporary-export-buffer
433 (switch-to-buffer-other-window "*Org LaTeX Export*")))
436 (defun org-replace-region-by-latex (beg end
)
437 "Replace the region from BEG to END with its LaTeX export.
438 It assumes the region has `org-mode' syntax, and then convert it to
439 LaTeX. This can be used in any buffer. For example, you could
440 write an itemized list in `org-mode' syntax in an LaTeX buffer and
441 then use this command to convert it."
444 (save-window-excursion
446 (setq latex
(org-export-region-as-latex
448 (setq reg
(buffer-substring beg end
)
449 buf
(get-buffer-create "*Org tmp*"))
455 (setq latex
(org-export-region-as-latex
456 (point-min) (point-max) t
'string
)))
458 (delete-region beg end
)
462 (defun org-export-region-as-latex (beg end
&optional body-only buffer
)
463 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
464 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
465 contents, and only produce the region of converted text, useful for
466 cut-and-paste operations.
467 If BUFFER is a buffer or a string, use/create that buffer as a target
468 of the converted LaTeX. If BUFFER is the symbol `string', return the
469 produced LaTeX as a string and leave no buffer behind. For example,
470 a Lisp program could call this function in the following way:
472 (setq latex (org-export-region-as-latex beg end t 'string))
474 When called interactively, the output buffer is selected, and shown
475 in a window. A non-interactive call will only return the buffer."
477 (when (interactive-p)
478 (setq buffer
"*Org LaTeX Export*"))
479 (let ((transient-mark-mode t
) (zmacs-regions t
)
481 (setq ext-plist
(plist-put ext-plist
:ignore-subree-p t
))
483 (set-mark (point)) ;; to activate the region
485 (setq rtn
(org-export-as-latex
488 (if (fboundp 'deactivate-mark
) (deactivate-mark))
489 (if (and (interactive-p) (bufferp rtn
))
490 (switch-to-buffer-other-window rtn
)
494 (defun org-export-as-latex (arg &optional hidden ext-plist
495 to-buffer body-only pub-dir
)
496 "Export current buffer to a LaTeX file.
497 If there is an active region, export only the region. The prefix
498 ARG specifies how many levels of the outline should become
499 headlines. The default is 3. Lower levels will be exported
500 depending on `org-export-latex-low-levels'. The default is to
501 convert them as description lists.
502 HIDDEN is obsolete and does nothing.
503 EXT-PLIST is a property list with
504 external parameters overriding org-mode's default settings, but
505 still inferior to file-local settings. When TO-BUFFER is
506 non-nil, create a buffer with that name and export to that
507 buffer. If TO-BUFFER is the symbol `string', don't leave any
508 buffer behind but just return the resulting LaTeX as a string.
509 When BODY-ONLY is set, don't produce the file header and footer,
510 simply return the content of \begin{document}...\end{document},
511 without even the \begin{document} and \end{document} commands.
512 when PUB-DIR is set, use this as the publishing directory."
514 ;; Make sure we have a file name when we need it.
515 (when (and (not (or to-buffer body-only
))
516 (not buffer-file-name
))
517 (if (buffer-base-buffer)
518 (org-set-local 'buffer-file-name
519 (with-current-buffer (buffer-base-buffer)
521 (error "Need a file name to be able to export")))
523 (message "Exporting to LaTeX...")
525 (remove-text-properties (point-min) (point-max)
526 '(:org-license-to-kill nil
)))
527 (org-update-radio-target-regexp)
528 (org-export-latex-set-initial-vars ext-plist arg
)
529 (let* ((wcf (current-window-configuration))
530 (opt-plist org-export-latex-options-plist
)
531 (region-p (org-region-active-p))
532 (rbeg (and region-p
(region-beginning)))
533 (rend (and region-p
(region-end)))
535 (if (plist-get opt-plist
:ignore-subree-p
)
540 (and (org-at-heading-p)
541 (>= (org-end-of-subtree t t
) rend
))))))
542 (opt-plist (setq org-export-opt-plist
544 (org-export-add-subtree-options opt-plist rbeg
)
546 ;; Make sure the variable contains the updated values.
547 (org-export-latex-options-plist opt-plist
)
548 (title (or (and subtree-p
(org-export-get-title-from-subtree))
549 (plist-get opt-plist
:title
)
551 (plist-get opt-plist
:skip-before-1st-heading
))
552 (org-export-grab-title-from-buffer))
553 (file-name-sans-extension
554 (file-name-nondirectory buffer-file-name
))))
555 (filename (concat (file-name-as-directory
557 (org-export-directory :LaTeX ext-plist
)))
558 (file-name-sans-extension
560 (org-entry-get rbeg
"EXPORT_FILE_NAME" t
))
561 (file-name-nondirectory ;sans-extension
564 (filename (if (equal (file-truename filename
)
565 (file-truename buffer-file-name
))
566 (concat filename
".tex")
568 (buffer (if to-buffer
570 ((eq to-buffer
'string
) (get-buffer-create
571 "*Org LaTeX Export*"))
572 (t (get-buffer-create to-buffer
)))
573 (find-file-noselect filename
)))
574 (odd org-odd-levels-only
)
575 (header (org-export-latex-make-header title opt-plist
))
576 (skip (cond (subtree-p nil
)
578 (t (plist-get opt-plist
:skip-before-1st-heading
))))
579 (text (plist-get opt-plist
:text
))
580 (org-export-preprocess-hook
582 `(lambda () (org-set-local 'org-complex-heading-regexp
583 ,org-export-latex-complex-heading-re
))
584 org-export-preprocess-hook
))
585 (first-lines (if skip
"" (org-export-latex-first-lines
592 (if region-p rend
))))
593 (coding-system (and (boundp 'buffer-file-coding-system
)
594 buffer-file-coding-system
))
595 (coding-system-for-write (or org-export-latex-coding-system
597 (save-buffer-coding-system (or org-export-latex-coding-system
599 (region (buffer-substring
600 (if region-p
(region-beginning) (point-min))
601 (if region-p
(region-end) (point-max))))
603 (org-export-preprocess-string
608 :tags
(plist-get opt-plist
:tags
)
609 :priority
(plist-get opt-plist
:priority
)
610 :footnotes
(plist-get opt-plist
:footnotes
)
611 :timestamps
(plist-get opt-plist
:timestamps
)
612 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
613 :add-text
(if (eq to-buffer
'string
) nil text
)
614 :skip-before-1st-heading skip
615 :select-tags
(plist-get opt-plist
:select-tags
)
616 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
617 :LaTeX-fragments nil
)))
621 (org-install-letbind)
623 (and (fboundp 'set-buffer-file-coding-system
)
624 (set-buffer-file-coding-system coding-system-for-write
))
626 ;; insert the header and initial document commands
627 (unless (or (eq to-buffer
'string
) body-only
)
630 ;; insert text found in #+TEXT
631 (when (and text
(not (eq to-buffer
'string
)))
632 (insert (org-export-latex-content
633 text
'(lists tables fixed-width keywords
))
636 ;; insert lines before the first headline
638 (insert first-lines
))
640 ;; export the content of headlines
641 (org-export-latex-global
643 (insert string-for-export
)
644 (goto-char (point-min))
645 (when (re-search-forward "^\\(\\*+\\) " nil t
)
646 (let* ((asters (length (match-string 1)))
647 (level (if odd
(- asters
2) (- asters
1))))
648 (setq org-export-latex-add-level
649 (if odd
(1- (/ (1+ asters
) 2)) (1- asters
)))
650 (org-export-latex-parse-global level odd
)))))
653 (unless body-only
(insert "\n\\end{document}"))
655 ;; Relocate the table of contents
656 (goto-char (point-min))
657 (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t
)
658 (goto-char (point-min))
659 (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t
)
661 (goto-char (point-min))
662 (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t
)
663 (replace-match "\\tableofcontents" t t
)))
665 (or to-buffer
(save-buffer))
666 (goto-char (point-min))
667 (or (org-export-push-to-kill-ring "LaTeX")
668 (message "Exporting to LaTeX...done"))
670 (if (eq to-buffer
'string
)
671 (prog1 (buffer-substring (point-min) (point-max))
672 (kill-buffer (current-buffer)))
674 (set-window-configuration wcf
))))
677 (defun org-export-as-pdf (arg &optional hidden ext-plist
678 to-buffer body-only pub-dir
)
679 "Export as LaTeX, then process through to PDF."
681 (message "Exporting to PDF...")
682 (let* ((wconfig (current-window-configuration))
683 (lbuf (org-export-as-latex arg hidden ext-plist
684 to-buffer body-only pub-dir
))
685 (file (buffer-file-name lbuf
))
686 (base (file-name-sans-extension (buffer-file-name lbuf
)))
687 (pdffile (concat base
".pdf"))
688 (cmds org-latex-to-pdf-process
)
689 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
690 (bibtex-p (with-current-buffer lbuf
692 (goto-char (point-min))
693 (re-search-forward "\\\\bibliography{" nil t
))))
695 (with-current-buffer outbuf
(erase-buffer))
696 (and (file-exists-p pdffile
) (delete-file pdffile
))
697 (message "Processing LaTeX file...")
698 (if (and cmds
(symbolp cmds
))
701 (setq cmd
(pop cmds
))
702 (while (string-match "%b" cmd
)
703 (setq cmd
(replace-match
705 (shell-quote-argument base
))
707 (while (string-match "%s" cmd
)
708 (setq cmd
(replace-match
710 (shell-quote-argument file
))
712 (shell-command cmd outbuf outbuf
)))
713 (message "Processing LaTeX file...done")
714 (if (not (file-exists-p pdffile
))
715 (error "PDF file was not produced")
716 (set-window-configuration wconfig
)
717 (when org-export-pdf-remove-logfiles
718 (dolist (ext '("aux" "log" "out" "toc"))
719 (setq file
(concat base
"." ext
))
720 (and (file-exists-p file
) (delete-file file
))))
721 (message "Exporting to PDF...done")
725 (defun org-export-as-pdf-and-open (arg)
726 "Export as LaTeX, then process through to PDF, and open."
728 (let ((pdffile (org-export-as-pdf arg
)))
730 (org-open-file pdffile
)
731 (error "PDF file was not produced"))))
733 ;;; Parsing functions:
735 (defun org-export-latex-parse-global (level odd
)
736 "Parse the current buffer recursively, starting at LEVEL.
737 If ODD is non-nil, assume the buffer only contains odd sections.
738 Return a list reflecting the document structure."
740 (goto-char (point-min))
741 (let* ((cnt 0) output
742 (depth org-export-latex-sectioning-depth
))
743 (while (re-search-forward
744 (concat "^\\(\\(?:\\*\\)\\{"
745 (number-to-string (+ (if odd
2 1) level
))
747 ;; make sure that there is no upper heading
752 (concat "^\\(\\(?:\\*\\)\\{"
753 (number-to-string level
)
754 "\\}\\) \\(.*\\)$") nil t
)))) t
)
756 (let* ((pos (match-beginning 0))
757 (heading (match-string 2))
758 (nlevel (if odd
(/ (+ 3 level
) 2) (1+ level
))))
763 (if (re-search-forward
764 (concat "^\\(\\(?:\\*\\)\\{"
765 (number-to-string (+ (if odd
2 1) level
))
766 "\\}\\) \\(.*\\)$") nil t
)
769 (goto-char (point-min))
777 `(heading .
,heading
)
778 `(content .
,(org-export-latex-parse-content))
779 `(subcontent .
,(org-export-latex-parse-subcontent
784 (defun org-export-latex-parse-content ()
785 "Extract the content of a section."
787 (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t
)
788 (progn (beginning-of-line) (point))
790 (buffer-substring beg end
)))
792 (defun org-export-latex-parse-subcontent (level odd
)
793 "Extract the subcontent of a section at LEVEL.
794 If ODD Is non-nil, assume subcontent only contains odd sections."
795 (if (not (re-search-forward
796 (concat "^\\(\\(?:\\*\\)\\{"
797 (number-to-string (+ (if odd
4 2) level
))
800 nil
; subcontent is nil
801 (org-export-latex-parse-global (+ (if odd
2 1) level
) odd
)))
803 ;;; Rendering functions:
804 (defun org-export-latex-global (content)
805 "Export CONTENT to LaTeX.
806 CONTENT is an element of the list produced by
807 `org-export-latex-parse-global'."
808 (if (eq (car content
) 'subcontent
)
809 (mapc 'org-export-latex-sub
(cdr content
))
810 (org-export-latex-sub (car content
))))
812 (defun org-export-latex-sub (subcontent)
813 "Export the list SUBCONTENT to LaTeX.
814 SUBCONTENT is an alist containing information about the headline
816 (let ((num (plist-get org-export-latex-options-plist
:section-numbers
)))
817 (mapc (lambda(x) (org-export-latex-subcontent x num
)) subcontent
)))
819 (defun org-export-latex-subcontent (subcontent num
)
820 "Export each cell of SUBCONTENT to LaTeX.
821 If NUM, export sections as numerical sections."
822 (let* ((heading (org-export-latex-fontify-headline
823 (cdr (assoc 'heading subcontent
))))
824 (level (- (cdr (assoc 'level subcontent
))
825 org-export-latex-add-level
))
826 (occur (number-to-string (cdr (assoc 'occur subcontent
))))
827 (content (cdr (assoc 'content subcontent
)))
828 (subcontent (cadr (assoc 'subcontent subcontent
)))
829 (label (org-get-text-property-any 0 'target heading
))
830 (label-list (cons label
(cdr (assoc label
831 org-export-target-aliases
)))))
834 ((<= level org-export-latex-sectioning-depth
)
835 (let* ((sec (nth (1- level
) org-export-latex-sectioning
))
837 (if (consp (cdr sec
))
838 (setq start
(nth (if num
0 2) sec
)
839 end
(nth (if num
1 3) sec
))
840 (setq start
(if num
(car sec
) (cdr sec
))))
841 (insert (format start heading
) "\n")
843 (insert (mapconcat (lambda (l) (format "\\label{%s}" l
))
844 label-list
"\n") "\n"))
845 (insert (org-export-latex-content content
))
846 (cond ((stringp subcontent
) (insert subcontent
))
847 ((listp subcontent
) (org-export-latex-sub subcontent
)))
848 (if end
(insert end
"\n"))))
849 ;; At a level under the hl option: we can drop this subsection
850 ((> level org-export-latex-sectioning-depth
)
851 (cond ((eq org-export-latex-low-levels
'description
)
852 (if (string-match "% ends low level$"
853 (buffer-substring (point-at-bol 0) (point)))
854 (delete-region (point-at-bol 0) (point))
855 (insert "\\begin{description}\n"))
856 (insert (format "\n\\item[%s]%s~\n\n"
858 (if label
(format "\\label{%s}" label
) "")))
859 (insert (org-export-latex-content content
))
860 (cond ((stringp subcontent
) (insert subcontent
))
861 ((listp subcontent
) (org-export-latex-sub subcontent
)))
862 (insert "\\end{description} % ends low level\n"))
863 ((memq org-export-latex-low-levels
'(itemize enumerate
))
864 (if (string-match "% ends low level$"
865 (buffer-substring (point-at-bol 0) (point)))
866 (delete-region (point-at-bol 0) (point))
867 (insert (format "\\begin{%s}\n"
868 (symbol-name org-export-latex-low-levels
))))
869 (insert (format "\n\\item %s\\\\\n%s\n"
871 (if label
(format "\\label{%s}" label
) "")))
872 (insert (org-export-latex-content content
))
873 (cond ((stringp subcontent
) (insert subcontent
))
874 ((listp subcontent
) (org-export-latex-sub subcontent
)))
875 (insert (format "\\end{%s} %% ends low level\n"
876 (symbol-name org-export-latex-low-levels
))))
878 ((listp org-export-latex-low-levels
)
879 (if (string-match "% ends low level$"
880 (buffer-substring (point-at-bol 0) (point)))
881 (delete-region (point-at-bol 0) (point))
882 (insert (car org-export-latex-low-levels
) "\n"))
883 (insert (format (nth 2 org-export-latex-low-levels
)
885 (if label
(format "\\label{%s}" label
) "")))
886 (insert (org-export-latex-content content
))
887 (cond ((stringp subcontent
) (insert subcontent
))
888 ((listp subcontent
) (org-export-latex-sub subcontent
)))
889 (insert (nth 1 org-export-latex-low-levels
)
890 " %% ends low level\n"))
892 ((stringp org-export-latex-low-levels
)
893 (insert (format org-export-latex-low-levels heading
) "\n")
894 (when label
(insert (format "\\label{%s}\n" label
)))
895 (insert (org-export-latex-content content
))
896 (cond ((stringp subcontent
) (insert subcontent
))
897 ((listp subcontent
) (org-export-latex-sub subcontent
)))))))))
899 ;;; Exporting internals:
900 (defun org-export-latex-set-initial-vars (ext-plist level
)
901 "Store org local variables required for LaTeX export.
902 EXT-PLIST is an optional additional plist.
903 LEVEL indicates the default depth for export."
904 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
905 org-export-latex-done-keywords org-done-keywords
906 org-export-latex-not-done-keywords org-not-done-keywords
907 org-export-latex-complex-heading-re org-complex-heading-regexp
908 org-export-latex-display-custom-times org-display-custom-times
909 org-export-latex-all-targets-re
910 (org-make-target-link-regexp (org-all-targets))
911 org-export-latex-options-plist
912 (org-combine-plists (org-default-export-plist) ext-plist
913 (org-infile-export-plist))
914 org-export-latex-class
915 (or (and (org-region-active-p)
917 (goto-char (region-beginning))
918 (and (looking-at org-complex-heading-regexp
)
919 (org-entry-get nil
"LaTeX_CLASS" 'selective
))))
923 (goto-char (point-min))
924 (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t
)
926 org-export-latex-default-class
)
927 org-export-latex-class
928 (or (car (assoc org-export-latex-class org-export-latex-classes
))
929 (error "No definition for class `%s' in `org-export-latex-classes'"
930 org-export-latex-class
))
931 org-export-latex-header
932 (cadr (assoc org-export-latex-class org-export-latex-classes
))
933 org-export-latex-sectioning
934 (cddr (assoc org-export-latex-class org-export-latex-classes
))
935 org-export-latex-sectioning-depth
938 (plist-get org-export-latex-options-plist
:headline-levels
))
939 (sec-depth (length org-export-latex-sectioning
)))
940 (if (> hl-levels sec-depth
) sec-depth hl-levels
)))))
942 (defun org-export-latex-make-header (title opt-plist
)
943 "Make the LaTeX header and return it as a string.
944 TITLE is the current title from the buffer or region.
945 OPT-PLIST is the options plist for current buffer."
946 (let ((toc (plist-get opt-plist
:table-of-contents
))
947 (author (plist-get opt-plist
:author
)))
949 (if (plist-get opt-plist
:time-stamp-file
)
950 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
951 ;; insert LaTeX custom header
952 (org-export-apply-macros-in-string org-export-latex-header
)
954 ;; insert information on LaTeX packages
955 (when org-export-latex-packages-alist
956 (mapconcat (lambda(p)
957 (if (equal "" (car p
))
958 (format "\\usepackage{%s}" (cadr p
))
959 (format "\\usepackage[%s]{%s}"
961 org-export-latex-packages-alist
"\n"))
962 ;; insert additional commands in the header
963 (org-export-apply-macros-in-string
964 (plist-get opt-plist
:latex-header-extra
))
965 (org-export-apply-macros-in-string org-export-latex-append-header
)
970 (org-export-latex-content
971 title
'(lists tables fixed-width keywords
)))
972 ;; insert author info
973 (if (plist-get opt-plist
:author-info
)
974 (format "\\author{%s}\n"
975 (org-export-latex-fontify-headline (or author user-full-name
)));????????????????????
976 (format "%%\\author{%s}\n"
977 (or author user-full-name
)))
979 (format "\\date{%s}\n"
981 (or (plist-get opt-plist
:date
)
982 org-export-latex-date-format
)))
983 ;; beginning of the document
984 "\n\\begin{document}\n\n"
985 ;; insert the title command
986 (when (string-match "\\S-" title
)
987 (if (string-match "%s" org-export-latex-title-command
)
988 (format org-export-latex-title-command title
)
989 org-export-latex-title-command
))
992 (when (and org-export-with-toc
993 (plist-get opt-plist
:section-numbers
))
995 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
996 (min toc
(plist-get opt-plist
:headline-levels
))))
997 (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
998 (plist-get opt-plist
:headline-levels
))))))))
1000 (defun org-export-latex-first-lines (opt-plist &optional beg end
)
1001 "Export the first lines before first headline.
1002 If BEG is non-nil, it is the beginning of the region.
1003 If END is non-nil, it is the end of the region."
1005 (goto-char (or beg
(point-min)))
1006 (let* ((pt (point)))
1008 (and (re-search-forward "^\\*+ " end t
)
1009 (setq end
(match-beginning 0)))
1010 (setq end
(point-max)))
1012 (org-export-latex-content
1013 (org-export-preprocess-string
1014 (buffer-substring pt end
)
1019 :skip-before-1st-heading nil
1020 :LaTeX-fragments nil
1021 :timestamps
(plist-get opt-plist
:timestamps
)
1022 :footnotes
(plist-get opt-plist
:footnotes
)))
1024 (add-text-properties pt
(max pt
(1- end
))
1025 '(:org-license-to-kill t
)))))))
1027 (defvar org-export-latex-header-defs nil
1028 "The header definitions that might be used in the LaTeX body.")
1029 (defvar org-export-latex-header-defs-re nil
1030 "The header definitions that might be used in the LaTeX body.")
1032 (defun org-export-latex-content (content &optional exclude-list
)
1033 "Convert CONTENT string to LaTeX.
1034 Don't perform conversions that are in EXCLUDE-LIST. Recognized
1035 conversion types are: quotation-marks, emphasis, sub-superscript,
1036 links, keywords, lists, tables, fixed-width"
1039 (unless (memq 'timestamps exclude-list
)
1040 (org-export-latex-time-stamps))
1041 (unless (memq 'quotation-marks exclude-list
)
1042 (org-export-latex-quotation-marks))
1043 (unless (memq 'emphasis exclude-list
)
1044 (when (plist-get org-export-latex-options-plist
:emphasize
)
1045 (org-export-latex-fontify)))
1046 (unless (memq 'sub-superscript exclude-list
)
1047 (org-export-latex-special-chars
1048 (plist-get org-export-latex-options-plist
:sub-superscript
)))
1049 (unless (memq 'links exclude-list
)
1050 (org-export-latex-links))
1051 (unless (memq 'keywords exclude-list
)
1052 (org-export-latex-keywords))
1053 (unless (memq 'lists exclude-list
)
1054 (org-export-latex-lists))
1055 (unless (memq 'tables exclude-list
)
1056 (org-export-latex-tables
1057 (plist-get org-export-latex-options-plist
:tables
)))
1058 (unless (memq 'fixed-width exclude-list
)
1059 (org-export-latex-fixed-width
1060 (plist-get org-export-latex-options-plist
:fixed-width
)))
1062 (buffer-substring (point-min) (point-max))))
1064 (defun org-export-latex-protect-string (s)
1065 "Add the org-protected property to string S."
1066 (add-text-properties 0 (length s
) '(org-protected t
) s
) s
)
1068 (defun org-export-latex-protect-char-in-string (char-list string
)
1069 "Add org-protected text-property to char from CHAR-LIST in STRING."
1073 (goto-char (point-min))
1074 (while (re-search-forward (regexp-opt char-list
) nil t
)
1075 (add-text-properties (match-beginning 0)
1076 (match-end 0) '(org-protected t
)))
1079 (defun org-export-latex-keywords-maybe (&optional remove-list
)
1080 "Maybe remove keywords depending on rules in REMOVE-LIST."
1081 (goto-char (point-min))
1082 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1
"\\|"))
1083 (case-fold-search nil
)
1084 (todo-markup org-export-latex-todo-keyword-markup
)
1086 ;; convert TODO keywords
1087 (when (re-search-forward (concat "^\\(" re-todo
"\\)") nil t
)
1088 (if (plist-get remove-list
:todo
)
1091 ((stringp todo-markup
) todo-markup
)
1092 ((and (consp todo-markup
) (stringp (car todo-markup
)))
1093 (if (member (match-string 1) org-export-latex-done-keywords
)
1094 (cdr todo-markup
) (car todo-markup
)))
1095 (t (cdr (or (assoc (match-string 1) todo-markup
)
1096 (car todo-markup
))))))
1097 (replace-match (format fmt
(match-string 1)) t t
)))
1098 ;; convert priority string
1099 (when (re-search-forward "\\[\\\\#.\\]" nil t
)
1100 (if (plist-get remove-list
:priority
)
1102 (replace-match (format "\\textbf{%s}" (match-string 0)) t t
)))
1104 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t
)
1105 (if (or (not org-export-with-tags
)
1106 (plist-get remove-list
:tags
))
1109 (org-export-latex-protect-string
1110 (format "\\textbf{%s}"
1112 (replace-regexp-in-string
1113 "_" "\\\\_" (match-string 0)))))
1116 (defun org-export-latex-fontify-headline (string)
1117 "Fontify special words in STRING."
1119 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
1120 ;; the beginning of the buffer - inserting "\n" is safe here though.
1121 (insert "\n" string
)
1122 (goto-char (point-min))
1123 (when (plist-get org-export-latex-options-plist
:emphasize
)
1124 (org-export-latex-fontify))
1125 (org-export-latex-keywords-maybe)
1126 (org-export-latex-special-chars
1127 (plist-get org-export-latex-options-plist
:sub-superscript
))
1128 (org-export-latex-links)
1129 (org-trim (buffer-string))))
1131 (defun org-export-latex-time-stamps ()
1132 "Format time stamps."
1133 (goto-char (point-min))
1134 (let ((org-display-custom-times org-export-latex-display-custom-times
))
1135 (while (re-search-forward org-ts-regexp-both nil t
)
1136 (org-if-unprotected-at (1- (point))
1138 (org-export-latex-protect-string
1139 (format org-export-latex-timestamp-markup
1140 (substring (org-translate-time (match-string 0)) 1 -
1)))
1143 (defun org-export-latex-quotation-marks ()
1144 "Export quotation marks depending on language conventions."
1145 (let* ((lang (plist-get org-export-latex-options-plist
:language
))
1146 (quote-rpl (if (equal lang
"fr")
1147 '(("\\(\\s-\\)\"" "«~")
1148 ("\\(\\S-\\)\"" "~»")
1149 ("\\(\\s-\\)'" "`"))
1150 '(("\\(\\s-\\|(\\)\"" "``")
1151 ("\\(\\S-\\)\"" "''")
1152 ("\\(\\s-\\|(\\)'" "`")))))
1153 (mapc (lambda(l) (goto-char (point-min))
1154 (while (re-search-forward (car l
) nil t
)
1155 (let ((rpl (concat (match-string 1) (cadr l
))))
1156 (org-export-latex-protect-string rpl
)
1157 (org-if-unprotected-1
1158 (replace-match rpl t t
))))) quote-rpl
)))
1160 (defun org-export-latex-special-chars (sub-superscript)
1161 "Export special characters to LaTeX.
1162 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
1163 See the `org-export-latex.el' code for a complete conversion table."
1164 (goto-char (point-min))
1166 (goto-char (point-min))
1167 (while (re-search-forward c nil t
)
1168 ;; Put the point where to check for org-protected
1169 (unless (get-text-property (match-beginning 2) 'org-protected
)
1170 (cond ((member (match-string 2) '("\\$" "$"))
1171 (if (equal (match-string 2) "\\$")
1173 (replace-match "\\$" t t
)))
1174 ((member (match-string 2) '("&" "%" "#"))
1175 (if (equal (match-string 1) "\\")
1176 (replace-match (match-string 2) t t
)
1177 (replace-match (concat (match-string 1) "\\"
1178 (match-string 2)) t t
)))
1179 ((equal (match-string 2) "...")
1181 (concat (match-string 1)
1182 (org-export-latex-protect-string "\\ldots{}")) t t
))
1183 ((equal (match-string 2) "~")
1184 (cond ((equal (match-string 1) "\\") nil
)
1185 ((eq 'org-link
(get-text-property 0 'face
(match-string 2)))
1186 (replace-match (concat (match-string 1) "\\~") t t
))
1188 (org-export-latex-protect-string
1189 (concat (match-string 1) "\\~{}")) t t
))))
1190 ((member (match-string 2) '("{" "}"))
1191 (unless (save-match-data (org-inside-latex-math-p))
1192 (if (equal (match-string 1) "\\")
1193 (replace-match (match-string 2) t t
)
1194 (replace-match (concat (match-string 1) "\\"
1195 (match-string 2)) t t
)))))
1196 (unless (save-match-data (org-inside-latex-math-p))
1197 (cond ((equal (match-string 2) "\\")
1198 (replace-match (or (save-match-data
1199 (org-export-latex-treat-backslash-char
1201 (or (match-string 3) "")))
1203 ((member (match-string 2) '("_" "^"))
1204 (replace-match (or (save-match-data
1205 (org-export-latex-treat-sub-super-char
1209 (match-string 3))) "") t t
)
1210 (backward-char 1)))))))
1211 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
1212 "\\(\\(\\\\?\\$\\)\\)"
1213 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
1214 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
1215 "\\(.\\|^\\)\\(&\\)"
1216 "\\(.\\|^\\)\\(#\\)"
1217 "\\(.\\|^\\)\\(%\\)"
1218 "\\(.\\|^\\)\\({\\)"
1219 "\\(.\\|^\\)\\(}\\)"
1220 "\\(.\\|^\\)\\(~\\)"
1221 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
1222 ;; (?\< . "\\textless{}")
1223 ;; (?\> . "\\textgreater{}")
1226 (defun org-inside-latex-math-p ()
1227 (get-text-property (point) 'org-latex-math
))
1229 (defun org-export-latex-treat-sub-super-char
1230 (subsup char string-before string-after
)
1231 "Convert the \"_\" and \"^\" characters to LaTeX.
1232 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
1233 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
1234 (cond ((equal string-before
"\\")
1235 (concat string-before char string-after
))
1236 ;; this is part of a math formula
1237 ((and (string-match "\\S-+" string-before
)
1238 (string-match "\\S-+" string-after
))
1239 (cond ((eq 'org-link
(get-text-property 0 'face char
))
1240 (concat string-before
"\\" char string-after
))
1241 ((save-match-data (org-inside-latex-math-p))
1243 (cond ((eq 1 (length string-after
))
1244 (concat string-before char string-after
))
1245 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after
)
1246 (format "%s%s{%s}" string-before char
1247 (match-string 1 string-after
))))))
1248 ((and (> (length string-after
) 1)
1250 (and (equal subsup
'{}) (eq (string-to-char string-after
) ?\
{)))
1251 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after
))
1252 (org-export-latex-protect-string
1253 (format "%s$%s{%s}$" string-before char
1254 (if (and (> (match-end 1) (1+ (match-beginning 1)))
1255 (not (equal (substring string-after
0 2) "{\\")))
1256 (concat "\\mathrm{" (match-string 1 string-after
) "}")
1257 (match-string 1 string-after
)))))
1258 ((eq subsup t
) (concat string-before
"$" char string-after
"$"))
1259 (t (org-export-latex-protect-string
1260 (concat string-before
"\\" char
"{}" string-after
)))))
1261 (t (org-export-latex-protect-string
1262 (concat string-before
"\\" char
"{}" string-after
)))))
1264 (defun org-export-latex-treat-backslash-char (string-before string-after
)
1265 "Convert the \"$\" special character to LaTeX.
1266 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
1267 (cond ((member (list string-after
) org-html-entities
)
1268 ;; backslash is part of a special entity (like "\alpha")
1269 (concat string-before
"$\\"
1270 (or (cdar (member (list string-after
) org-html-entities
))
1272 ((and (not (string-match "^[ \n\t]" string-after
))
1273 (not (string-match "[ \t]\\'\\|^" string-before
)))
1274 ;; backslash is inside a word
1275 (org-export-latex-protect-string
1276 (concat string-before
"\\textbackslash{}" string-after
)))
1277 ((not (or (equal string-after
"")
1278 (string-match "^[ \t\n]" string-after
)))
1279 ;; backslash might escape a character (like \#) or a user TeX
1280 ;; macro (like \setcounter)
1281 (org-export-latex-protect-string
1282 (concat string-before
"\\" string-after
)))
1283 ((and (string-match "^[ \t\n]" string-after
)
1284 (string-match "[ \t\n]\\'" string-before
))
1285 ;; backslash is alone, convert it to $\backslash$
1286 (org-export-latex-protect-string
1287 (concat string-before
"\\textbackslash{}" string-after
)))
1288 (t (org-export-latex-protect-string
1289 (concat string-before
"\\textbackslash{}" string-after
)))))
1291 (defun org-export-latex-keywords ()
1292 "Convert special keywords to LaTeX."
1293 (goto-char (point-min))
1294 (while (re-search-forward org-export-latex-special-keyword-regexp nil t
)
1295 (replace-match (format org-export-latex-timestamp-keyword-markup
1296 (match-string 0)) t t
)
1298 (beginning-of-line 1)
1299 (unless (looking-at ".*\\\\newline[ \t]*$")
1301 (insert "\\newline")))))
1303 (defun org-export-latex-fixed-width (opt)
1304 "When OPT is non-nil convert fixed-width sections to LaTeX."
1305 (goto-char (point-min))
1306 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t
)
1308 (progn (goto-char (match-beginning 0))
1309 (insert "\\begin{verbatim}\n")
1310 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1311 (replace-match (concat (match-string 1)
1312 (match-string 2)) t t
)
1314 (insert "\\end{verbatim}\n\n"))
1315 (progn (goto-char (match-beginning 0))
1316 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1317 (replace-match (concat "%" (match-string 1)
1318 (match-string 2)) t t
)
1322 (defvar org-table-last-alignment
) ; defined in org-table.el
1323 (defvar org-table-last-column-widths
) ; defined in org-table.el
1324 (declare-function orgtbl-to-latex
"org-table" (table params
) t
)
1325 (defun org-export-latex-tables (insert)
1326 "Convert tables to LaTeX and INSERT it."
1327 (goto-char (point-min))
1328 (while (re-search-forward "^\\([ \t]*\\)|" nil t
)
1330 (let* ((beg (org-table-begin))
1331 (end (org-table-end))
1332 (raw-table (buffer-substring beg end
))
1333 (org-table-last-alignment (copy-sequence org-table-last-alignment
))
1334 (org-table-last-column-widths (copy-sequence
1335 org-table-last-column-widths
))
1336 fnum fields line lines olines gr colgropen line-fmt align
1337 caption label attr floatp longtblp
)
1338 (if org-export-latex-tables-verbatim
1339 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1340 "\\end{verbatim}\n")))
1341 (apply 'delete-region
(list beg end
))
1342 (insert (org-export-latex-protect-string tbl
)))
1344 (setq caption
(org-find-text-property-in-string
1345 'org-caption raw-table
)
1346 attr
(org-find-text-property-in-string
1347 'org-attributes raw-table
)
1348 label
(org-find-text-property-in-string
1349 'org-label raw-table
)
1350 longtblp
(and attr
(stringp attr
)
1351 (string-match "\\<longtable\\>" attr
))
1352 align
(and attr
(stringp attr
)
1353 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr
)
1354 (match-string 1 attr
))
1355 floatp
(or caption label
))
1356 (setq lines
(org-split-string raw-table
"\n"))
1357 (apply 'delete-region
(list beg end
))
1358 (when org-export-table-remove-special-lines
1359 (setq lines
(org-table-clean-before-export lines
'maybe-quoted
)))
1360 (when org-table-clean-did-remove-column
1361 (pop org-table-last-alignment
)
1362 (pop org-table-last-column-widths
))
1363 ;; make a formatting string to reflect aligment
1365 (while (and (not line-fmt
) (setq line
(pop olines
)))
1366 (unless (string-match "^[ \t]*|-" line
)
1367 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
1368 (setq fnum
(make-vector (length fields
) 0))
1372 (setq gr
(pop org-table-colgroup-info
))
1374 (cond ((eq gr
:start
)
1375 (prog1 (if colgropen
"|" "|")
1376 (setq colgropen t
)))
1378 (prog1 (if colgropen
"|" "|")
1379 (setq colgropen nil
)))
1381 (if (memq gr
'(:end
:startend
))
1382 (progn (setq colgropen nil
) "|")
1385 ;; fix double || in line-fmt
1386 (setq line-fmt
(replace-regexp-in-string "||" "|" line-fmt
))
1387 ;; maybe remove the first and last "|"
1388 (when (and (not org-export-latex-tables-column-borders
)
1389 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt
))
1390 (setq line-fmt
(match-string 2 line-fmt
)))
1393 (setq align
(apply 'format
1395 (mapcar (lambda (x) (if x
"r" "l"))
1396 org-table-last-alignment
)))))
1397 ;; prepare the table to send to orgtbl-to-latex
1401 (or (and (string-match "[ \t]*|-+" elem
) 'hline
)
1402 (org-split-string (org-trim elem
) "|")))
1405 (insert (org-export-latex-protect-string
1408 (concat "\\begin{longtable}{" align
"}\n")
1409 (if floatp
"\\begin{table}[htb]\n"))
1410 (if (or floatp longtblp
)
1413 (if label
(concat "\\\label{" label
"}") "")
1415 (if longtblp
"\\\\\n" "\n")
1416 (if (and org-export-latex-tables-centered
(not longtblp
))
1417 "\\begin{center}\n")
1418 (if (not longtblp
) (concat "\\begin{tabular}{" align
"}\n"))
1421 `(:tstart nil
:tend nil
1422 :hlend
,(if longtblp
1426 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
1428 \\endlastfoot" (length org-table-last-alignment
))
1430 (if (not longtblp
) (concat "\n\\end{tabular}"))
1431 (if longtblp
"\n" (if org-export-latex-tables-centered
1432 "\n\\end{center}\n" "\n"))
1435 (if floatp
"\\end{table}"))))
1438 (defun org-export-latex-fontify ()
1439 "Convert fontification to LaTeX."
1440 (goto-char (point-min))
1441 (while (re-search-forward org-emph-re nil t
)
1442 ;; The match goes one char after the *string*
1443 (let ((emph (assoc (match-string 3)
1444 org-export-latex-emphasis-alist
))
1445 (beg (match-beginning 0))
1449 (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
1451 (unless (or (get-text-property (1- (point)) 'org-protected
)
1453 (goto-char (match-beginning 1))
1455 (and (org-at-table-p)
1457 "[|\n]" (buffer-substring beg end
))))))
1458 (setq rpl
(concat (match-string 1)
1459 (org-export-latex-emph-format (cadr emph
)
1463 (setq rpl
(org-export-latex-protect-string rpl
)))
1464 (replace-match rpl t t
)))
1467 (defvar org-export-latex-use-verb nil
)
1468 (defun org-export-latex-emph-format (format string
)
1469 "Format an emphasis string and handle the \\verb special case."
1470 (when (equal format
"\\verb")
1472 (if org-export-latex-use-verb
1473 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1475 (loop for i from
0 to
(1- (length ll
)) do
1476 (if (not (string-match (regexp-quote (substring ll i
(1+ i
)))
1479 (setq format
(concat "\\verb" (substring ll i
(1+ i
))
1480 "%s" (substring ll i
(1+ i
))))
1481 (throw 'exit nil
))))))
1483 (trans '(("\\" .
"\\backslash")
1484 ("~" .
"\\ensuremath{\\sim}")
1485 ("^" .
"\\ensuremath{\\wedge}")))
1487 (while (string-match "[\\{}$%&_#~^]" string
)
1488 (setq char
(match-string 0 string
))
1489 (if (> (match-beginning 0) 0)
1490 (setq rtn
(concat rtn
(substring string
1491 0 (match-beginning 0)))))
1492 (setq string
(substring string
(1+ (match-beginning 0))))
1493 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
1494 rtn
(concat rtn char
)))
1495 (setq string
(concat rtn string
) format
"\\texttt{%s}")))))
1496 (setq string
(org-export-latex-protect-string
1497 (format format string
))))
1499 (defun org-export-latex-links ()
1500 ;; Make sure to use the LaTeX hyperref and graphicx package
1501 ;; or send some warnings.
1502 "Convert links to LaTeX."
1503 (goto-char (point-min))
1504 (while (re-search-forward org-bracket-link-analytic-regexp
++ nil t
)
1506 (goto-char (match-beginning 0))
1507 (let* ((re-radio org-export-latex-all-targets-re
)
1508 (remove (list (match-beginning 0) (match-end 0)))
1509 (raw-path (org-extract-attributes (match-string 3)))
1510 (full-raw-path (concat (match-string 1) raw-path
))
1511 (desc (match-string 5))
1512 (type (or (match-string 2)
1513 (if (or (file-name-absolute-p raw-path
)
1514 (string-match "^\\.\\.?/" raw-path
))
1516 (coderefp (equal type
"coderef"))
1517 (caption (org-find-text-property-in-string 'org-caption raw-path
))
1518 (attr (or (org-find-text-property-in-string 'org-attributes raw-path
)
1519 (plist-get org-export-latex-options-plist
:latex-image-options
)))
1520 (label (org-find-text-property-in-string 'org-label raw-path
))
1521 (floatp (or label caption
))
1523 ;; define the path of the link
1525 ((member type
'("coderef"))
1527 ((member type
'("http" "https" "ftp"))
1528 (concat type
":" raw-path
))
1529 ((and re-radio
(string-match re-radio raw-path
))
1531 ((equal type
"mailto")
1532 (concat type
":" raw-path
))
1533 ((equal type
"file")
1534 (if (and (org-file-image-p
1537 org-export-latex-inline-image-extensions
)
1538 (or (get-text-property 0 'org-no-description
1540 (equal desc full-raw-path
)))
1542 (progn (when (string-match "\\(.+\\)::.+" raw-path
)
1543 (setq raw-path
(match-string 1 raw-path
)))
1544 (if (file-exists-p raw-path
)
1545 (concat type
"://" (expand-file-name raw-path
))
1546 (concat type
"://" (org-export-directory
1547 :LaTeX org-export-latex-options-plist
)
1549 ;; process with link inserting
1550 (apply 'delete-region remove
)
1551 (cond ((and imgp
(plist-get org-export-latex-options-plist
:inline-images
))
1554 (if floatp
"\\begin{figure}[htb]\n")
1555 (format "\\centerline{\\includegraphics[%s]{%s}}\n"
1557 (if (file-name-absolute-p raw-path
)
1558 (expand-file-name raw-path
)
1561 (format "\\caption{%s%s}\n"
1562 (if label
(concat "\\label{" label
"}") "")
1564 (if floatp
"\\end{figure}\n"))))
1567 (org-export-get-coderef-format path desc
)
1568 (cdr (assoc path org-export-code-refs
)))))
1569 (radiop (insert (format "\\hyperref[%s]{%s}"
1570 (org-solidify-link-text raw-path
) desc
)))
1572 (insert (format "\\hyperref[%s]{%s}"
1573 (org-remove-initial-hash
1574 (org-solidify-link-text raw-path
))
1577 (when (org-at-table-p)
1578 ;; There is a strange problem when we have a link in a table,
1579 ;; ampersands then cause a problem. I think this must be
1580 ;; a LaTeX issue, but we here implement a work-around anyway.
1581 (setq path
(org-export-latex-protect-amp path
)
1582 desc
(org-export-latex-protect-amp desc
)))
1583 (insert (format "\\href{%s}{%s}" path desc
)))
1584 (t (insert "\\texttt{" desc
"}")))))))
1586 (defun org-export-latex-protect-amp (s)
1587 (while (string-match "\\([^\\\\]\\)\\(&\\)" s
)
1588 (setq s
(replace-match (concat (match-string 1 s
) "\\" (match-string 2 s
))
1592 (defun org-remove-initial-hash (s)
1593 (if (string-match "\\`#" s
)
1596 (defvar org-latex-entities
) ; defined below
1597 (defvar org-latex-entities-regexp
) ; defined below
1598 (defvar org-latex-entities-exceptions
) ; defined below
1600 (defun org-export-latex-preprocess (parameters)
1601 "Clean stuff in the LaTeX export."
1602 ;; Preserve line breaks
1603 (goto-char (point-min))
1604 (while (re-search-forward "\\\\\\\\" nil t
)
1605 (add-text-properties (match-beginning 0) (match-end 0)
1606 '(org-protected t
)))
1608 ;; Preserve latex environments
1609 (goto-char (point-min))
1610 (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t
)
1611 (let* ((start (progn (beginning-of-line) (point)))
1612 (end (and (re-search-forward
1613 (concat "^[ \t]*\\\\end{"
1614 (regexp-quote (match-string 1))
1618 (add-text-properties start end
'(org-protected t
))
1619 (goto-char (point-at-eol)))))
1621 ;; Preserve math snippets
1623 (let* ((matchers (plist-get org-format-latex-options
:matchers
))
1624 (re-list org-latex-regexps
)
1625 beg end re e m n block off
)
1626 ;; Check the different regular expressions
1627 (while (setq e
(pop re-list
))
1628 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
1629 block
(if (nth 3 e
) "\n\n" ""))
1630 (setq off
(if (member m
'("$" "$1")) 1 0))
1631 (when (and (member m matchers
) (not (equal m
"begin")))
1632 (goto-char (point-min))
1633 (while (re-search-forward re nil t
)
1634 (setq beg
(+ (match-beginning 0) off
) end
(- (match-end 0) 0))
1635 (add-text-properties beg end
'(org-protected t org-latex-math t
))))))
1637 ;; Convert LaTeX to \LaTeX{}
1638 (goto-char (point-min))
1639 (let ((case-fold-search nil
))
1640 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t
)
1642 (replace-match (org-export-latex-protect-string
1643 (concat (match-string 1) "\\LaTeX{}")) t t
))))
1645 ;; Convert blockquotes
1646 (goto-char (point-min))
1647 (while (search-forward "ORG-BLOCKQUOTE-START" nil t
)
1648 (org-replace-match-keep-properties "\\begin{quote}" t t
))
1649 (goto-char (point-min))
1650 (while (search-forward "ORG-BLOCKQUOTE-END" nil t
)
1651 (org-replace-match-keep-properties "\\end{quote}" t t
))
1654 (goto-char (point-min))
1655 (while (search-forward "ORG-VERSE-START" nil t
)
1656 (org-replace-match-keep-properties "\\begin{verse}" t t
)
1657 (beginning-of-line 2)
1658 (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
1659 (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
1660 (goto-char (match-end 1))
1661 (org-replace-match-keep-properties
1662 (org-export-latex-protect-string
1663 (concat "\\hspace*{1cm}" (match-string 2))) t t
)
1664 (beginning-of-line 1))
1665 (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
1668 (beginning-of-line 2))
1669 (and (looking-at "[ \t]*ORG-VERSE-END.*")
1670 (org-replace-match-keep-properties "\\end{verse}" t t
)))
1673 (goto-char (point-min))
1674 (while (search-forward "ORG-CENTER-START" nil t
)
1675 (org-replace-match-keep-properties "\\begin{center}" t t
))
1676 (goto-char (point-min))
1677 (while (search-forward "ORG-CENTER-END" nil t
)
1678 (org-replace-match-keep-properties "\\end{center}" t t
))
1680 (run-hooks 'org-export-latex-after-blockquotes-hook
)
1682 ;; Convert horizontal rules
1683 (goto-char (point-min))
1684 (while (re-search-forward "^----+.$" nil t
)
1686 (replace-match (org-export-latex-protect-string "\\hrule") t t
)))
1688 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
1689 (let ((re (concat "\\\\[a-zA-Z]+\\(?:"
1692 (org-create-multibrace-regexp "{" "}" 3))))
1693 (while (re-search-forward re nil t
)
1694 (add-text-properties (match-beginning 0) (match-end 0)
1695 '(org-protected t
))))
1697 ;; Protect LaTeX entities
1698 (goto-char (point-min))
1700 (while (re-search-forward org-latex-entities-regexp nil t
)
1701 (if (setq a
(assoc (match-string 0) org-latex-entities-exceptions
))
1702 (replace-match (org-add-props (nth 1 a
) nil
'org-protected t
)
1704 (add-text-properties (match-beginning 0) (match-end 0)
1705 '(org-protected t
)))))
1707 ;; Replace radio links
1708 (goto-char (point-min))
1709 (while (re-search-forward
1710 (concat "<<<?" org-export-latex-all-targets-re
1711 ">>>?\\((INVISIBLE)\\)?") nil t
)
1714 (org-export-latex-protect-string
1715 (format "\\label{%s}%s" (save-match-data (org-solidify-link-text
1717 (if (match-string 2) "" (match-string 1)))) t t
)))
1719 ;; Delete @<...> constructs
1720 ;; Thanks to Daniel Clemente for this regexp
1721 (goto-char (point-min))
1722 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t
)
1724 (replace-match "")))
1726 ;; When converting to LaTeX, replace footnotes
1727 ;; FIXME: don't protect footnotes from conversion
1728 (when (plist-get org-export-latex-options-plist
:footnotes
)
1729 (goto-char (point-min))
1730 (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t
)
1732 (when (save-match-data
1733 (save-excursion (beginning-of-line)
1734 (looking-at "[^:|#]")))
1735 (let ((foot-beg (match-beginning 0))
1736 (foot-end (match-end 0))
1737 (foot-prefix (match-string 0))
1738 footnote footnote-rpl
)
1740 (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix
))
1742 (replace-match "$^{\\1}$")
1744 (let ((end (save-excursion
1745 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t
)
1746 (match-beginning 0) (point-max)))))
1747 (setq footnote
(concat (org-trim (buffer-substring (point) end
))
1748 " ")) ; prevent last } being part of a link
1749 (delete-region (point) end
))
1750 (goto-char foot-beg
)
1751 (delete-region foot-beg foot-end
)
1752 (unless (null footnote
)
1753 (setq footnote-rpl
(format "\\footnote{%s}" footnote
))
1754 (add-text-properties 0 10 '(org-protected t
) footnote-rpl
)
1755 (add-text-properties (1- (length footnote-rpl
))
1756 (length footnote-rpl
)
1757 '(org-protected t
) footnote-rpl
)
1758 (insert footnote-rpl
)))
1761 ;; Remove footnote section tag for LaTeX
1762 (goto-char (point-min))
1763 (while (re-search-forward
1764 (concat "^" footnote-section-tag-regexp
) nil t
)
1766 (replace-match "")))))
1770 (defun org-export-latex-lists ()
1771 "Convert plain text lists in current buffer into LaTeX lists."
1772 (goto-char (point-min))
1773 (while (re-search-forward org-list-beginning-re nil t
)
1776 (insert (org-list-to-latex (org-list-parse-list t
)
1777 org-export-latex-list-parameters
))
1780 (defconst org-latex-entities
1809 "\\begin{description}"
1810 "\\begin{enumerate}"
1814 "\\begin{flushleft}"
1815 "\\begin{flushright}"
1820 "\\begin{quotation}"
1825 "\\begin{thebibliography}"
1827 "\\begin{titlepage}"
1955 "A list of LaTeX commands to be protected when performing conversion.")
1957 (defvar org-latex-entities-exceptions nil
)
1959 (defconst org-latex-entities-regexp
1961 (dolist (x org-latex-entities
)
1963 (add-to-list 'org-latex-entities-exceptions x
)
1965 (if (string-match "[a-z][A-Z]$" x
)
1968 (concat "\\(" (regexp-opt (nreverse names
)) "\\>\\)"
1969 "\\|\\(" (regexp-opt (nreverse rest
)) "\\)")))
1971 (provide 'org-export-latex
)
1972 (provide 'org-latex
)
1974 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
1976 ;;; org-latex.el ends here