1 ;;; org-latex.el --- LaTeX exporter for org-mode
3 ;; Copyright (C) 2007, 2008, 2009, 2010 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 ("\\section{%s}" .
"\\section*{%s}")
96 ("\\subsection{%s}" .
"\\subsection*{%s}")
97 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
98 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
99 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
101 "\\documentclass[11pt]{report}"
102 ("\\part{%s}" .
"\\part*{%s}")
103 ("\\chapter{%s}" .
"\\chapter*{%s}")
104 ("\\section{%s}" .
"\\section*{%s}")
105 ("\\subsection{%s}" .
"\\subsection*{%s}")
106 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
108 "\\documentclass[11pt]{book}"
109 ("\\part{%s}" .
"\\part*{%s}")
110 ("\\chapter{%s}" .
"\\chapter*{%s}")
111 ("\\section{%s}" .
"\\section*{%s}")
112 ("\\subsection{%s}" .
"\\subsection*{%s}")
113 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
115 "\\documentclass{beamer}"
116 org-beamer-sectioning
118 "Alist of LaTeX classes and associated header and structure.
119 If #+LaTeX_CLASS is set in the buffer, use its value and the
120 associated information. Here is the structure of each cell:
124 (numbered-section . unnumbered-section\)
130 The HEADER-STRING is the header that will be inserted into the LaTeX file.
131 It should contain the \\documentclass macro, and anything else that is needed
132 for this setup. To this header, the following commands will be added:
134 - Calls to \\usepackage for all packages mentioned in the variables
135 `org-export-latex-default-packages-alist' and
136 `org-export-latex-packages-alist'. Thus, your header definitions should
137 avoid to also request these packages.
139 - Lines specified via \"#+LaTeX_HEADER:\"
141 If you need more control about the sequence in which the header is built
142 up, or if you want to exclude one of these building blocks for a particular
143 class, you can use the following macro-like placeholders.
145 [DEFAULT-PACKAGES] \\usepackage statements for default packages
146 [NO-DEFAULT-PACKAGES] do not include any of the default packages
147 [PACKAGES] \\usepackage statements for packages
148 [NO-PACKAGES] do not include the packages
149 [EXTRA] the stuff from #+LaTeX_HEADER
150 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
151 [BEAMER-HEADER-EXTRA] the beamer extra headers
155 \\documentclass{article}
156 [NO-DEFAULT-PACKAGES]
158 \\providecommand{\\alert}[1]{\\textbf{#1}}
161 will omit the default packages, and will include the #+LaTeX_HEADER lines,
162 then have a call to \\providecommand, and then place \\usepackage commands
163 based on the content of `org-export-latex-packages-alist'.
165 If your header or `org-export-latex-default-packages-alist' inserts
166 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be replaced with
167 a coding system derived from `buffer-file-coding-system'. See also the
168 variable `org-export-latex-inputenc-alist' for a way to influence this
171 The sectioning structure
172 ------------------------
174 The sectioning structure of the class is given by the elements following
175 the header string. For each sectioning level, a number of strings is
176 specified. A %s formatter is mandatory in each section string and will
177 be replaced by the title of the section.
179 Instead of a cons cell (numbered . unnumbered), you can also provide a list
182 (numbered-open numbered-close)
186 (numbered-open numbered-close unnumbered-open unnumbered-close)
188 providing opening and closing strings for a LaTeX environment that should
189 represent the document section. The opening clause should have a %s
190 to represent the section title.
192 Instead of a list of sectioning commands, you can also specify a
193 function name. That function will be called with two parameters,
194 the (reduced) level of the headline, and the headline text. The function
195 must return a cons cell with the (possibly modified) headline text, and the
196 sectioning list in the cdr."
197 :group
'org-export-latex
199 (list (string :tag
"LaTeX class")
200 (string :tag
"LaTeX header")
201 (repeat :tag
"Levels" :inline t
204 (string :tag
" numbered")
205 (string :tag
"unnumbered"))
206 (list :tag
"Environment"
207 (string :tag
"Opening (numbered)")
208 (string :tag
"Closing (numbered)")
209 (string :tag
"Opening (unnumbered)")
210 (string :tag
"Closing (unnumbered)"))
211 (function :tag
"Hook computing sectioning"))))))
213 (defcustom org-export-latex-inputenc-alist nil
214 "Alist of inputenc coding system names, and what should really be used.
215 For example, adding an entry
217 (\"utf8\" . \"utf8x\")
219 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
220 are written as utf8 files."
221 :group
'org-export-latex
224 (string :tag
"Derived from buffer")
225 (string :tag
"Use this instead"))))
228 (defcustom org-export-latex-emphasis-alist
229 '(("*" "\\textbf{%s}" nil
)
230 ("/" "\\emph{%s}" nil
)
231 ("_" "\\underline{%s}" nil
)
235 "Alist of LaTeX expressions to convert emphasis fontifiers.
236 Each element of the list is a list of three elements.
237 The first element is the character used as a marker for fontification.
238 The second element is a formatting string to wrap fontified text with.
239 If it is \"\\verb\", Org will automatically select a delimiter
240 character that is not in the string.
241 The third element decides whether to protect converted text from other
243 :group
'org-export-latex
246 (defcustom org-export-latex-title-command
"\\maketitle"
247 "The command used to insert the title just after \\begin{document}.
248 If this string contains the formatting specification \"%s\" then
249 it will be used as a formatting string, passing the title as an
251 :group
'org-export-latex
254 (defcustom org-export-latex-import-inbuffer-stuff nil
255 "Non-nil means define TeX macros for Org's inbuffer definitions.
256 For example \orgTITLE for #+TITLE."
257 :group
'org-export-latex
260 (defcustom org-export-latex-date-format
262 "Format string for \\date{...}."
263 :group
'org-export-latex
266 (defcustom org-export-latex-todo-keyword-markup
"\\textbf{%s}"
267 "Markup for TODO keywords, as a printf format.
268 This can be a single format for all keywords, a cons cell with separate
269 formats for not-done and done states, or an association list with setup
270 for individual keywords. If a keyword shows up for which there is no
271 markup defined, the first one in the association list will be used."
272 :group
'org-export-latex
274 (string :tag
"Default")
275 (cons :tag
"Distinguish undone and done"
276 (string :tag
"Not-DONE states")
277 (string :tag
"DONE states"))
278 (repeat :tag
"Per keyword markup"
280 (string :tag
"Keyword")
281 (string :tag
"Markup")))))
283 (defcustom org-export-latex-timestamp-markup
"\\textit{%s}"
284 "A printf format string to be applied to time stamps."
285 :group
'org-export-latex
288 (defcustom org-export-latex-timestamp-keyword-markup
"\\texttt{%s}"
289 "A printf format string to be applied to time stamps."
290 :group
'org-export-latex
293 (defcustom org-export-latex-hyperref-format
"\\href{%s}{%s}"
294 "A printf format string to be applied to hyperref links.
295 The format must contain two %s instances. The first will be filled with
296 the link, the second with the link description."
297 :group
'org-export-latex
300 (defcustom org-export-latex-tables-verbatim nil
301 "When non-nil, tables are exported verbatim."
302 :group
'org-export-latex
305 (defcustom org-export-latex-tables-centered t
306 "When non-nil, tables are exported in a center environment."
307 :group
'org-export-latex
310 (defcustom org-export-latex-tables-column-borders nil
311 "When non-nil, grouping columns can cause outer vertical lines in tables.
312 When nil, grouping causes only separation lines between groups."
313 :group
'org-export-latex
316 (defcustom org-export-latex-low-levels
'itemize
317 "How to convert sections below the current level of sectioning.
318 This is specified by the `org-export-headline-levels' option or the
319 value of \"H:\" in Org's #+OPTION line.
321 This can be either nil (skip the sections), `description', `itemize',
322 or `enumerate' (convert the sections as the corresponding list type), or
323 a string to be used instead of \\section{%s}. In this latter case,
324 the %s stands here for the inserted headline and is mandatory.
326 It may also be a list of three string to define a user-defined environment
327 that should be used. The first string should be the like
328 \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
329 to two occurrences of %s for the title and a label, respectively. The third
330 string should be like \"\\end{itemize\"."
331 :group
'org-export-latex
332 :type
'(choice (const :tag
"Ignore" nil
)
333 (const :tag
"Convert as descriptive list" description
)
334 (const :tag
"Convert as itemized list" itemize
)
335 (const :tag
"Convert as enumerated list" enumerate
)
336 (list :tag
"User-defined environment"
337 :value
("\\begin{itemize}" "\\end{itemize}" "\\item %s")
338 (string :tag
"Start")
340 (string :tag
"item"))
341 (string :tag
"Use a section string" :value
"\\subparagraph{%s}")))
343 (defcustom org-export-latex-list-parameters
344 '(:cbon
"$\\boxtimes$" :cboff
"$\\Box$")
345 "Parameters for the LaTeX list exporter.
346 These parameters will be passed on to `org-list-to-latex', which in turn
347 will pass them (combined with the LaTeX default list parameters) to
348 `org-list-to-generic'."
349 :group
'org-export-latex
352 (defcustom org-export-latex-verbatim-wrap
353 '("\\begin{verbatim}\n" .
"\\end{verbatim}\n")
354 "Environment to be wrapped around a fixed-width section in LaTeX export.
355 This is a cons with two strings, to be added before and after the
358 Defaults to \\begin{verbatim} and \\end{verbatim}."
359 :group
'org-export-translation
360 :group
'org-export-latex
361 :type
'(cons (string :tag
"Open")
362 (string :tag
"Close")))
364 (defcustom org-export-latex-listings nil
365 "Non-nil means export source code using the listings package.
366 This package will fontify source code, possibly even with color.
367 If you want to use this, you also need to make LaTeX use the
368 listings package, and if you want to have color, the color
369 package. Just add these to `org-export-latex-packages-alist',
370 for example using customize, or with something like
373 (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
374 (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))"
375 :group
'org-export-latex
378 (defcustom org-export-latex-listings-langs
379 '((emacs-lisp "Lisp") (lisp "Lisp")
382 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
383 (html "HTML") (xml "XML")
384 (tex "TeX") (latex "TeX")
385 (shell-script "bash")
387 (ocaml "Caml") (caml "Caml")
388 (sql "SQL") (sqlite "sql"))
389 "Alist mapping languages to their listing language counterpart.
390 The key is a symbol, the major mode symbol without the \"-mode\".
391 The value is the string that should be inserted as the language parameter
392 for the listings package. If the mode name and the listings name are
393 the same, the language does not need an entry in this list - but it does not
394 hurt if it is present."
395 :group
'org-export-latex
398 (symbol :tag
"Major mode ")
399 (string :tag
"Listings language"))))
401 (defcustom org-export-latex-listings-w-names t
402 "Non-nil means export names of named code blocks.
403 Code blocks exported with the listings package (controlled by the
404 `org-export-latex-listings' variable) can be named in the style
406 :group
'org-export-latex
409 (defcustom org-export-latex-remove-from-headlines
410 '(:todo nil
:priority nil
:tags nil
)
411 "A plist of keywords to remove from headlines. OBSOLETE.
412 Non-nil means remove this keyword type from the headline.
414 Don't remove the keys, just change their values.
416 Obsolete, this variable is no longer used. Use the separate
417 variables `org-export-with-todo-keywords', `org-export-with-priority',
418 and `org-export-with-tags' instead."
420 :group
'org-export-latex
)
422 (defcustom org-export-latex-image-default-option
"width=10em"
423 "Default option for images."
424 :group
'org-export-latex
427 (defcustom org-export-latex-tabular-environment
"tabular"
428 "Default environment used to build tables."
429 :group
'org-export-latex
432 (defcustom org-export-latex-inline-image-extensions
433 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
434 "Extensions of image files that can be inlined into LaTeX.
435 Note that the image extension *actually* allowed depend on the way the
436 LaTeX file is processed. When used with pdflatex, pdf, jpg and png images
437 are OK. When processing through dvi to Postscript, only ps and eps are
438 allowed. The default we use here encompasses both."
439 :group
'org-export-latex
440 :type
'(repeat (string :tag
"Extension")))
442 (defcustom org-export-latex-coding-system nil
443 "Coding system for the exported LaTeX file."
444 :group
'org-export-latex
445 :type
'coding-system
)
447 (defgroup org-export-pdf nil
448 "Options for exporting Org-mode files to PDF, via LaTeX."
449 :tag
"Org Export PDF"
450 :group
'org-export-latex
453 (defcustom org-latex-to-pdf-process
454 '("pdflatex -interaction nonstopmode -output-directory %o %f"
455 "pdflatex -interaction nonstopmode -output-directory %o %f")
456 "Commands to process a LaTeX file to a PDF file.
457 This is a list of strings, each of them will be given to the shell
458 as a command. %f in the command will be replaced by the full file name, %b
459 by the file base name (i.e. without extension) and %o by the base directory
461 The reason why this is a list is that it usually takes several runs of
462 pdflatex, maybe mixed with a call to bibtex. Org does not have a clever
463 mechanism to detect which of these commands have to be run to get to a stable
464 result, and it also does not do any error checking.
466 Alternatively, this may be a Lisp function that does the processing, so you
467 could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
468 This function should accept the file name as its single argument."
469 :group
'org-export-pdf
470 :type
'(choice (repeat :tag
"Shell command sequence"
471 (string :tag
"Shell command"))
474 (defcustom org-export-pdf-logfiles
475 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
476 "The list of file extensions to consider as LaTeX logfiles."
477 :group
'org-export-pdf
478 :type
'(repeat (string :tag
"Extension")))
480 (defcustom org-export-pdf-remove-logfiles t
481 "Non-nil means remove the logfiles produced by PDF production.
482 These are the .aux, .log, .out, and .toc files."
483 :group
'org-export-pdf
488 (defvar org-export-latex-after-initial-vars-hook nil
489 "Hook run before LaTeX export.
490 The exact moment is after the initial variables like org-export-latex-class
491 have been determined from the environment.")
493 (defvar org-export-latex-after-blockquotes-hook nil
494 "Hook run during LaTeX export, after blockquote, verse, center are done.")
496 (defvar org-export-latex-final-hook nil
497 "Hook run in the finalized LaTeX buffer.")
499 (defvar org-export-latex-after-save-hook nil
500 "Hook run in the finalized LaTeX buffer, after it has been saved.")
502 ;;; Autoload functions:
505 (defun org-export-as-latex-batch ()
506 "Call `org-export-as-latex', may be used in batch processing.
510 --load=$HOME/lib/emacs/org.el
511 --eval \"(setq org-export-headline-levels 2)\"
512 --visit=MyFile --funcall org-export-as-latex-batch"
513 (org-export-as-latex org-export-headline-levels
'hidden
))
516 (defun org-export-as-latex-to-buffer (arg)
517 "Call `org-export-as-latex` with output to a temporary buffer.
518 No file is created. The prefix ARG is passed through to `org-export-as-latex'."
520 (org-export-as-latex arg nil nil
"*Org LaTeX Export*")
521 (when org-export-show-temporary-export-buffer
522 (switch-to-buffer-other-window "*Org LaTeX Export*")))
525 (defun org-replace-region-by-latex (beg end
)
526 "Replace the region from BEG to END with its LaTeX export.
527 It assumes the region has `org-mode' syntax, and then convert it to
528 LaTeX. This can be used in any buffer. For example, you could
529 write an itemized list in `org-mode' syntax in an LaTeX buffer and
530 then use this command to convert it."
533 (save-window-excursion
535 (setq latex
(org-export-region-as-latex
537 (setq reg
(buffer-substring beg end
)
538 buf
(get-buffer-create "*Org tmp*"))
539 (with-current-buffer buf
543 (setq latex
(org-export-region-as-latex
544 (point-min) (point-max) t
'string
)))
546 (delete-region beg end
)
550 (defun org-export-region-as-latex (beg end
&optional body-only buffer
)
551 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
552 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
553 contents, and only produce the region of converted text, useful for
554 cut-and-paste operations.
555 If BUFFER is a buffer or a string, use/create that buffer as a target
556 of the converted LaTeX. If BUFFER is the symbol `string', return the
557 produced LaTeX as a string and leave no buffer behind. For example,
558 a Lisp program could call this function in the following way:
560 (setq latex (org-export-region-as-latex beg end t 'string))
562 When called interactively, the output buffer is selected, and shown
563 in a window. A non-interactive call will only return the buffer."
565 (when (interactive-p)
566 (setq buffer
"*Org LaTeX Export*"))
567 (let ((transient-mark-mode t
) (zmacs-regions t
)
569 (setq ext-plist
(plist-put ext-plist
:ignore-subtree-p t
))
571 (set-mark (point)) ;; to activate the region
573 (setq rtn
(org-export-as-latex
576 (if (fboundp 'deactivate-mark
) (deactivate-mark))
577 (if (and (interactive-p) (bufferp rtn
))
578 (switch-to-buffer-other-window rtn
)
582 (defun org-export-as-latex (arg &optional hidden ext-plist
583 to-buffer body-only pub-dir
)
584 "Export current buffer to a LaTeX file.
585 If there is an active region, export only the region. The prefix
586 ARG specifies how many levels of the outline should become
587 headlines. The default is 3. Lower levels will be exported
588 depending on `org-export-latex-low-levels'. The default is to
589 convert them as description lists.
590 HIDDEN is obsolete and does nothing.
591 EXT-PLIST is a property list with
592 external parameters overriding org-mode's default settings, but
593 still inferior to file-local settings. When TO-BUFFER is
594 non-nil, create a buffer with that name and export to that
595 buffer. If TO-BUFFER is the symbol `string', don't leave any
596 buffer behind but just return the resulting LaTeX as a string.
597 When BODY-ONLY is set, don't produce the file header and footer,
598 simply return the content of \\begin{document}...\\end{document},
599 without even the \\begin{document} and \\end{document} commands.
600 when PUB-DIR is set, use this as the publishing directory."
602 (when (and (not body-only
) arg
(listp arg
)) (setq body-only t
))
603 (run-hooks 'org-export-first-hook
)
605 ;; Make sure we have a file name when we need it.
606 (when (and (not (or to-buffer body-only
))
607 (not buffer-file-name
))
608 (if (buffer-base-buffer)
609 (org-set-local 'buffer-file-name
610 (with-current-buffer (buffer-base-buffer)
612 (error "Need a file name to be able to export")))
614 (message "Exporting to LaTeX...")
616 (let ((inhibit-read-only t
))
617 (remove-text-properties (point-min) (point-max)
618 '(:org-license-to-kill nil
))))
619 (org-update-radio-target-regexp)
620 (org-export-latex-set-initial-vars ext-plist arg
)
621 (setq org-export-opt-plist org-export-latex-options-plist
)
622 (org-install-letbind)
623 (run-hooks 'org-export-latex-after-initial-vars-hook
)
624 (let* ((wcf (current-window-configuration))
625 (opt-plist org-export-latex-options-plist
)
626 (region-p (org-region-active-p))
627 (rbeg (and region-p
(region-beginning)))
628 (rend (and region-p
(region-end)))
630 (if (plist-get opt-plist
:ignore-subtree-p
)
635 (and (org-at-heading-p)
636 (>= (org-end-of-subtree t t
) rend
))))))
637 (opt-plist (setq org-export-opt-plist
639 (org-export-add-subtree-options opt-plist rbeg
)
641 ;; Make sure the variable contains the updated values.
642 (org-export-latex-options-plist (setq org-export-opt-plist opt-plist
))
643 ;; The following two are dynamically scoped into other
645 (org-current-export-dir
646 (or pub-dir
(org-export-directory :html opt-plist
)))
647 (org-current-export-file buffer-file-name
)
648 (title (or (and subtree-p
(org-export-get-title-from-subtree))
649 (plist-get opt-plist
:title
)
651 (plist-get opt-plist
:skip-before-1st-heading
))
652 (org-export-grab-title-from-buffer))
653 (and buffer-file-name
654 (file-name-sans-extension
655 (file-name-nondirectory buffer-file-name
)))
660 (file-name-as-directory
662 (org-export-directory :LaTeX ext-plist
)))
663 (file-name-sans-extension
665 (org-entry-get rbeg
"EXPORT_FILE_NAME" t
))
666 (file-name-nondirectory ;sans-extension
668 (error "Don't know which export file to use")))))
672 (if (equal (file-truename filename
)
673 (file-truename (or buffer-file-name
"dummy.org")))
674 (concat filename
".tex")
676 (buffer (if to-buffer
678 ((eq to-buffer
'string
) (get-buffer-create
679 "*Org LaTeX Export*"))
680 (t (get-buffer-create to-buffer
)))
681 (find-file-noselect filename
)))
682 (odd org-odd-levels-only
)
683 (header (org-export-latex-make-header title opt-plist
))
684 (skip (cond (subtree-p nil
)
686 (t (plist-get opt-plist
:skip-before-1st-heading
))))
687 (text (plist-get opt-plist
:text
))
688 (org-export-preprocess-hook
690 `(lambda () (org-set-local 'org-complex-heading-regexp
691 ,org-export-latex-complex-heading-re
))
692 org-export-preprocess-hook
))
693 (first-lines (if skip
"" (org-export-latex-first-lines
700 (if region-p rend
))))
701 (coding-system (and (boundp 'buffer-file-coding-system
)
702 buffer-file-coding-system
))
703 (coding-system-for-write (or org-export-latex-coding-system
705 (save-buffer-coding-system (or org-export-latex-coding-system
707 (region (buffer-substring
708 (if region-p
(region-beginning) (point-min))
709 (if region-p
(region-end) (point-max))))
711 (and text
(string-match "\\S-" text
)
712 (org-export-preprocess-string
717 :tags
(plist-get opt-plist
:tags
)
718 :priority
(plist-get opt-plist
:priority
)
719 :footnotes
(plist-get opt-plist
:footnotes
)
720 :drawers
(plist-get opt-plist
:drawers
)
721 :timestamps
(plist-get opt-plist
:timestamps
)
722 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
724 :skip-before-1st-heading skip
727 :LaTeX-fragments nil
)))
729 (org-export-preprocess-string
734 :tags
(plist-get opt-plist
:tags
)
735 :priority
(plist-get opt-plist
:priority
)
736 :footnotes
(plist-get opt-plist
:footnotes
)
737 :drawers
(plist-get opt-plist
:drawers
)
738 :timestamps
(plist-get opt-plist
:timestamps
)
739 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
740 :add-text
(if (eq to-buffer
'string
) nil text
)
741 :skip-before-1st-heading skip
742 :select-tags
(plist-get opt-plist
:select-tags
)
743 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
744 :LaTeX-fragments nil
)))
748 (org-install-letbind)
750 (and (fboundp 'set-buffer-file-coding-system
)
751 (set-buffer-file-coding-system coding-system-for-write
))
753 ;; insert the header and initial document commands
754 (unless (or (eq to-buffer
'string
) body-only
)
757 ;; insert text found in #+TEXT
758 (when (and text
(not (eq to-buffer
'string
)))
759 (insert (org-export-latex-content
760 text
'(lists tables fixed-width keywords
))
763 ;; insert lines before the first headline
765 (insert first-lines
))
767 ;; export the content of headlines
768 (org-export-latex-global
770 (insert string-for-export
)
771 (goto-char (point-min))
772 (when (re-search-forward "^\\(\\*+\\) " nil t
)
773 (let* ((asters (length (match-string 1)))
774 (level (if odd
(- asters
2) (- asters
1))))
775 (setq org-export-latex-add-level
776 (if odd
(1- (/ (1+ asters
) 2)) (1- asters
)))
777 (org-export-latex-parse-global level odd
)))))
780 (unless body-only
(insert "\n\\end{document}"))
782 ;; Attach description terms to the \item macro
783 (goto-char (point-min))
784 (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t
)
785 (delete-region (match-beginning 1) (match-end 1)))
787 ;; Relocate the table of contents
788 (goto-char (point-min))
789 (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t
)
790 (goto-char (point-min))
791 (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t
)
793 (goto-char (point-min))
794 (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t
)
795 (replace-match "\\tableofcontents" t t
)))
797 ;; Cleanup forced line ends in items where they are not needed
798 (goto-char (point-min))
799 (while (re-search-forward
800 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin"
802 (delete-region (match-beginning 1) (match-end 1)))
803 (goto-char (point-min))
804 (while (re-search-forward
805 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*"
807 (if (looking-at "[\n \t]+")
808 (replace-match "\n")))
810 (run-hooks 'org-export-latex-final-hook
)
812 (unless (eq major-mode
'latex-mode
) (latex-mode))
814 (org-export-latex-fix-inputenc)
815 (run-hooks 'org-export-latex-after-save-hook
)
816 (goto-char (point-min))
817 (or (org-export-push-to-kill-ring "LaTeX")
818 (message "Exporting to LaTeX...done"))
820 (if (eq to-buffer
'string
)
821 (prog1 (buffer-substring (point-min) (point-max))
822 (kill-buffer (current-buffer)))
824 (set-window-configuration wcf
))))
827 (defun org-export-as-pdf (arg &optional hidden ext-plist
828 to-buffer body-only pub-dir
)
829 "Export as LaTeX, then process through to PDF."
831 (message "Exporting to PDF...")
832 (let* ((wconfig (current-window-configuration))
833 (lbuf (org-export-as-latex arg hidden ext-plist
834 to-buffer body-only pub-dir
))
835 (file (buffer-file-name lbuf
))
836 (base (file-name-sans-extension (buffer-file-name lbuf
)))
837 (pdffile (concat base
".pdf"))
838 (cmds org-latex-to-pdf-process
)
839 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
840 (bibtex-p (with-current-buffer lbuf
842 (goto-char (point-min))
843 (re-search-forward "\\\\bibliography{" nil t
))))
845 (with-current-buffer outbuf
(erase-buffer))
846 (message (concat "Processing LaTeX file " file
"..."))
847 (setq output-dir
(file-name-directory file
))
848 (if (and cmds
(symbolp cmds
))
849 (funcall cmds
(shell-quote-argument file
))
851 (setq cmd
(pop cmds
))
852 (while (string-match "%b" cmd
)
853 (setq cmd
(replace-match
855 (shell-quote-argument base
))
857 (while (string-match "%f" cmd
)
858 (setq cmd
(replace-match
860 (shell-quote-argument file
))
862 (while (string-match "%o" cmd
)
863 (setq cmd
(replace-match
865 (shell-quote-argument output-dir
))
867 (shell-command cmd outbuf outbuf
)))
868 (message (concat "Processing LaTeX file " file
"...done"))
869 (if (not (file-exists-p pdffile
))
870 (error (concat "PDF file " pdffile
" was not produced"))
871 (set-window-configuration wconfig
)
872 (when org-export-pdf-remove-logfiles
873 (dolist (ext org-export-pdf-logfiles
)
874 (setq file
(concat base
"." ext
))
875 (and (file-exists-p file
) (delete-file file
))))
876 (message "Exporting to PDF...done")
880 (defun org-export-as-pdf-and-open (arg)
881 "Export as LaTeX, then process through to PDF, and open."
883 (let ((pdffile (org-export-as-pdf arg
)))
886 (org-open-file pdffile
)
887 (when org-export-kill-product-buffer-when-displayed
888 (kill-buffer (find-buffer-visiting
889 (concat (file-name-sans-extension (buffer-file-name))
891 (error "PDF file was not produced"))))
893 ;;; Parsing functions:
895 (defun org-export-latex-parse-global (level odd
)
896 "Parse the current buffer recursively, starting at LEVEL.
897 If ODD is non-nil, assume the buffer only contains odd sections.
898 Return a list reflecting the document structure."
900 (goto-char (point-min))
901 (let* ((cnt 0) output
902 (depth org-export-latex-sectioning-depth
))
903 (while (org-re-search-forward-unprotected
904 (concat "^\\(\\(?:\\*\\)\\{"
905 (number-to-string (+ (if odd
2 1) level
))
907 ;; make sure that there is no upper heading
911 (org-re-search-forward-unprotected
912 (concat "^\\(\\(?:\\*\\)\\{"
913 (number-to-string level
)
914 "\\}\\) \\(.*\\)$") nil t
)))) t
)
916 (let* ((pos (match-beginning 0))
917 (heading (match-string 2))
918 (nlevel (if odd
(/ (+ 3 level
) 2) (1+ level
))))
923 (if (org-re-search-forward-unprotected
924 (concat "^\\(\\(?:\\*\\)\\{"
925 (number-to-string (+ (if odd
2 1) level
))
926 "\\}\\) \\(.*\\)$") nil t
)
929 (goto-char (point-min))
937 `(heading .
,heading
)
938 `(content .
,(org-export-latex-parse-content))
939 `(subcontent .
,(org-export-latex-parse-subcontent
944 (defun org-export-latex-parse-content ()
945 "Extract the content of a section."
947 (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t
)
948 (progn (beginning-of-line) (point))
950 (buffer-substring beg end
)))
952 (defun org-export-latex-parse-subcontent (level odd
)
953 "Extract the subcontent of a section at LEVEL.
954 If ODD Is non-nil, assume subcontent only contains odd sections."
955 (if (not (org-re-search-forward-unprotected
956 (concat "^\\(\\(?:\\*\\)\\{"
957 (number-to-string (+ (if odd
4 2) level
))
960 nil
; subcontent is nil
961 (org-export-latex-parse-global (+ (if odd
2 1) level
) odd
)))
963 ;;; Rendering functions:
964 (defun org-export-latex-global (content)
965 "Export CONTENT to LaTeX.
966 CONTENT is an element of the list produced by
967 `org-export-latex-parse-global'."
968 (if (eq (car content
) 'subcontent
)
969 (mapc 'org-export-latex-sub
(cdr content
))
970 (org-export-latex-sub (car content
))))
972 (defun org-export-latex-sub (subcontent)
973 "Export the list SUBCONTENT to LaTeX.
974 SUBCONTENT is an alist containing information about the headline
976 (let ((num (plist-get org-export-latex-options-plist
:section-numbers
)))
977 (mapc (lambda(x) (org-export-latex-subcontent x num
)) subcontent
)))
979 (defun org-export-latex-subcontent (subcontent num
)
980 "Export each cell of SUBCONTENT to LaTeX.
981 If NUM, export sections as numerical sections."
982 (let* ((heading (cdr (assoc 'heading subcontent
)))
983 (level (- (cdr (assoc 'level subcontent
))
984 org-export-latex-add-level
))
985 (occur (number-to-string (cdr (assoc 'occur subcontent
))))
986 (content (cdr (assoc 'content subcontent
)))
987 (subcontent (cadr (assoc 'subcontent subcontent
)))
988 (label (org-get-text-property-any 0 'target heading
))
989 (label-list (cons label
(cdr (assoc label
990 org-export-target-aliases
))))
991 (sectioning org-export-latex-sectioning
)
992 (depth org-export-latex-sectioning-depth
)
993 main-heading sub-heading
)
994 (when (symbolp (car sectioning
))
995 (setq sectioning
(funcall (car sectioning
) level heading
))
997 (setq heading
(car sectioning
)
998 sectioning
(cdr sectioning
)
999 ;; target property migh have changed...
1000 label
(org-get-text-property-any 0 'target heading
)
1001 label-list
(cons label
(cdr (assoc label
1002 org-export-target-aliases
)))))
1003 (if sectioning
(setq sectioning
(make-list 10 sectioning
)))
1004 (setq depth
(if sectioning
10000 0)))
1005 (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading
)
1006 (setq main-heading
(substring heading
0 (match-beginning 0))
1007 sub-heading
(substring heading
(match-end 0))))
1008 (setq heading
(org-export-latex-fontify-headline heading
)
1009 sub-heading
(and sub-heading
1010 (org-export-latex-fontify-headline sub-heading
))
1011 main-heading
(and main-heading
1012 (org-export-latex-fontify-headline main-heading
)))
1014 ;; Normal conversion
1016 (let* ((sec (nth (1- level
) sectioning
))
1018 (if (consp (cdr sec
))
1019 (setq start
(nth (if num
0 2) sec
)
1020 end
(nth (if num
1 3) sec
))
1021 (setq start
(if num
(car sec
) (cdr sec
))))
1022 (insert (format start
(if main-heading main-heading heading
)
1023 (or sub-heading
"")))
1026 (insert (mapconcat (lambda (l) (format "\\label{%s}" l
))
1027 label-list
"\n") "\n"))
1028 (insert (org-export-latex-content content
))
1029 (cond ((stringp subcontent
) (insert subcontent
))
1031 (while (org-looking-back "\n\n") (backward-delete-char 1))
1032 (org-export-latex-sub subcontent
)))
1033 (when (and end
(string-match "[^ \t]" end
))
1034 (let ((hook (org-get-text-property-any 0 'org-insert-hook end
)))
1035 (and (functionp hook
) (funcall hook
)))
1036 (insert end
"\n"))))
1037 ;; At a level under the hl option: we can drop this subsection
1039 (cond ((eq org-export-latex-low-levels
'description
)
1040 (if (string-match "% ends low level$"
1041 (buffer-substring (point-at-bol 0) (point)))
1042 (delete-region (point-at-bol 0) (point))
1043 (insert "\\begin{description}\n"))
1044 (insert (format "\n\\item[%s]%s~\n"
1046 (if label
(format "\\label{%s}" label
) "")))
1047 (insert (org-export-latex-content content
))
1048 (cond ((stringp subcontent
) (insert subcontent
))
1049 ((listp subcontent
) (org-export-latex-sub subcontent
)))
1050 (insert "\\end{description} % ends low level\n"))
1051 ((memq org-export-latex-low-levels
'(itemize enumerate
))
1052 (if (string-match "% ends low level$"
1053 (buffer-substring (point-at-bol 0) (point)))
1054 (delete-region (point-at-bol 0) (point))
1055 (insert (format "\\begin{%s}\n"
1056 (symbol-name org-export-latex-low-levels
))))
1057 (insert (format "\n\\item %s\\\\\n%s%%"
1059 (if label
(format "\\label{%s}" label
) "")))
1060 (insert (org-export-latex-content content
))
1061 (cond ((stringp subcontent
) (insert subcontent
))
1062 ((listp subcontent
) (org-export-latex-sub subcontent
)))
1063 (insert (format "\\end{%s} %% ends low level\n"
1064 (symbol-name org-export-latex-low-levels
))))
1066 ((listp org-export-latex-low-levels
)
1067 (if (string-match "% ends low level$"
1068 (buffer-substring (point-at-bol 0) (point)))
1069 (delete-region (point-at-bol 0) (point))
1070 (insert (car org-export-latex-low-levels
) "\n"))
1071 (insert (format (nth 2 org-export-latex-low-levels
)
1073 (if label
(format "\\label{%s}" label
) "")))
1074 (insert (org-export-latex-content content
))
1075 (cond ((stringp subcontent
) (insert subcontent
))
1076 ((listp subcontent
) (org-export-latex-sub subcontent
)))
1077 (insert (nth 1 org-export-latex-low-levels
)
1078 " %% ends low level\n"))
1080 ((stringp org-export-latex-low-levels
)
1081 (insert (format org-export-latex-low-levels heading
) "\n")
1082 (when label
(insert (format "\\label{%s}\n" label
)))
1083 (insert (org-export-latex-content content
))
1084 (cond ((stringp subcontent
) (insert subcontent
))
1085 ((listp subcontent
) (org-export-latex-sub subcontent
)))))))))
1087 ;;; Exporting internals:
1088 (defun org-export-latex-set-initial-vars (ext-plist level
)
1089 "Store org local variables required for LaTeX export.
1090 EXT-PLIST is an optional additional plist.
1091 LEVEL indicates the default depth for export."
1092 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
1093 org-export-latex-done-keywords org-done-keywords
1094 org-export-latex-not-done-keywords org-not-done-keywords
1095 org-export-latex-complex-heading-re org-complex-heading-regexp
1096 org-export-latex-display-custom-times org-display-custom-times
1097 org-export-latex-all-targets-re
1098 (org-make-target-link-regexp (org-all-targets))
1099 org-export-latex-options-plist
1100 (org-combine-plists (org-default-export-plist) ext-plist
1101 (org-infile-export-plist))
1102 org-export-latex-class
1103 (or (and (org-region-active-p)
1105 (goto-char (region-beginning))
1106 (and (looking-at org-complex-heading-regexp
)
1107 (org-entry-get nil
"LaTeX_CLASS" 'selective
))))
1111 (goto-char (point-min))
1112 (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\(-[a-zA-Z]+\\)" nil t
)
1114 (plist-get org-export-latex-options-plist
:latex-class
)
1115 org-export-latex-default-class
)
1116 org-export-latex-class-options
1117 (or (and (org-region-active-p)
1119 (goto-char (region-beginning))
1120 (and (looking-at org-complex-heading-regexp
)
1121 (org-entry-get nil
"LaTeX_CLASS_OPTIONS" 'selective
))))
1125 (goto-char (point-min))
1126 (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t
)
1128 (plist-get org-export-latex-options-plist
:latex-class-options
))
1129 org-export-latex-class
1130 (or (car (assoc org-export-latex-class org-export-latex-classes
))
1131 (error "No definition for class `%s' in `org-export-latex-classes'"
1132 org-export-latex-class
))
1133 org-export-latex-header
1134 (cadr (assoc org-export-latex-class org-export-latex-classes
))
1135 org-export-latex-sectioning
1136 (cddr (assoc org-export-latex-class org-export-latex-classes
))
1137 org-export-latex-sectioning-depth
1140 (plist-get org-export-latex-options-plist
:headline-levels
))
1141 (sec-depth (length org-export-latex-sectioning
)))
1142 (if (> hl-levels sec-depth
) sec-depth hl-levels
))))
1143 (when (and org-export-latex-class-options
1144 (string-match "\\S-" org-export-latex-class-options
)
1145 (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?"
1146 org-export-latex-header
))
1147 (setq org-export-latex-header
1148 (concat (substring org-export-latex-header
0 (match-end 1))
1149 org-export-latex-class-options
1150 (substring org-export-latex-header
(match-end 0))))))
1152 (defvar org-export-latex-format-toc-function
1153 'org-export-latex-format-toc-default
1154 "The function formatting returning the string to create the table of contents.
1155 The function mus take one parameter, the depth of the table of contents.")
1157 (defun org-export-latex-make-header (title opt-plist
)
1158 "Make the LaTeX header and return it as a string.
1159 TITLE is the current title from the buffer or region.
1160 OPT-PLIST is the options plist for current buffer."
1161 (let ((toc (plist-get opt-plist
:table-of-contents
))
1162 (author (org-export-apply-macros-in-string
1163 (plist-get opt-plist
:author
))))
1165 (if (plist-get opt-plist
:time-stamp-file
)
1166 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1167 ;; insert LaTeX custom header and packages from the list
1168 (org-splice-latex-header
1169 (org-export-apply-macros-in-string org-export-latex-header
)
1170 org-export-latex-default-packages-alist
1171 org-export-latex-packages-alist nil
1172 (org-export-apply-macros-in-string
1173 (plist-get opt-plist
:latex-header-extra
)))
1174 ;; append another special variable
1175 (org-export-apply-macros-in-string org-export-latex-append-header
)
1176 ;; define align if not yet defined
1177 "\n\\providecommand{\\alert}[1]{\\textbf{#1}}"
1181 ;; convert the title
1182 (org-export-latex-content
1183 title
'(lists tables fixed-width keywords
)))
1184 ;; insert author info
1185 (if (plist-get opt-plist
:author-info
)
1186 (format "\\author{%s}\n"
1187 (org-export-latex-fontify-headline (or author user-full-name
)))
1188 (format "%%\\author{%s}\n"
1189 (org-export-latex-fontify-headline (or author user-full-name
))))
1191 (format "\\date{%s}\n"
1193 (or (plist-get opt-plist
:date
)
1194 org-export-latex-date-format
)))
1195 ;; beginning of the document
1196 "\n\\begin{document}\n\n"
1197 ;; insert the title command
1198 (when (string-match "\\S-" title
)
1199 (if (string-match "%s" org-export-latex-title-command
)
1200 (format org-export-latex-title-command title
)
1201 org-export-latex-title-command
))
1203 ;; table of contents
1204 (when (and org-export-with-toc
1205 (plist-get opt-plist
:section-numbers
))
1206 (funcall org-export-latex-format-toc-function
1207 (cond ((numberp toc
)
1208 (min toc
(plist-get opt-plist
:headline-levels
)))
1209 (toc (plist-get opt-plist
:headline-levels
))))))))
1211 (defun org-export-latex-format-toc-default (depth)
1213 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1216 (defun org-export-latex-first-lines (opt-plist &optional beg end
)
1217 "Export the first lines before first headline.
1218 If BEG is non-nil, it is the beginning of the region.
1219 If END is non-nil, it is the end of the region."
1221 (goto-char (or beg
(point-min)))
1223 (end (if (re-search-forward "^\\*+ " end t
)
1224 (goto-char (match-beginning 0))
1225 (goto-char (or end
(point-max))))))
1227 (org-export-latex-content
1228 (org-export-preprocess-string
1229 (buffer-substring pt end
)
1234 :skip-before-1st-heading nil
1235 :LaTeX-fragments nil
1236 :timestamps
(plist-get opt-plist
:timestamps
)
1237 :footnotes
(plist-get opt-plist
:footnotes
)))
1239 (let ((inhibit-read-only t
)
1240 (limit (max pt
(1- end
))))
1241 (add-text-properties pt limit
1242 '(:org-license-to-kill t
))
1245 (while (re-search-forward "^[ \t]*#\\+.*\n?" limit t
)
1246 (let ((case-fold-search t
))
1247 (unless (org-string-match-p
1248 "^[ \t]*#\\+\\(attr_\\|caption\\>\\|label\\>\\)"
1250 (remove-text-properties (match-beginning 0) (match-end 0)
1251 '(:org-license-to-kill t
))))))))))))
1254 (defvar org-export-latex-header-defs nil
1255 "The header definitions that might be used in the LaTeX body.")
1256 (defvar org-export-latex-header-defs-re nil
1257 "The header definitions that might be used in the LaTeX body.")
1259 (defun org-export-latex-content (content &optional exclude-list
)
1260 "Convert CONTENT string to LaTeX.
1261 Don't perform conversions that are in EXCLUDE-LIST. Recognized
1262 conversion types are: quotation-marks, emphasis, sub-superscript,
1263 links, keywords, lists, tables, fixed-width"
1266 (unless (memq 'timestamps exclude-list
)
1267 (org-export-latex-time-stamps))
1268 (unless (memq 'quotation-marks exclude-list
)
1269 (org-export-latex-quotation-marks))
1270 (unless (memq 'emphasis exclude-list
)
1271 (when (plist-get org-export-latex-options-plist
:emphasize
)
1272 (org-export-latex-fontify)))
1273 (unless (memq 'sub-superscript exclude-list
)
1274 (org-export-latex-special-chars
1275 (plist-get org-export-latex-options-plist
:sub-superscript
)))
1276 (unless (memq 'links exclude-list
)
1277 (org-export-latex-links))
1278 (unless (memq 'keywords exclude-list
)
1279 (org-export-latex-keywords))
1280 (unless (memq 'lists exclude-list
)
1281 (org-export-latex-lists))
1282 (unless (memq 'tables exclude-list
)
1283 (org-export-latex-tables
1284 (plist-get org-export-latex-options-plist
:tables
)))
1285 (unless (memq 'fixed-width exclude-list
)
1286 (org-export-latex-fixed-width
1287 (plist-get org-export-latex-options-plist
:fixed-width
)))
1289 (buffer-substring (point-min) (point-max))))
1291 (defun org-export-latex-protect-string (s)
1292 "Add the org-protected property to string S."
1293 (add-text-properties 0 (length s
) '(org-protected t
) s
) s
)
1295 (defun org-export-latex-protect-char-in-string (char-list string
)
1296 "Add org-protected text-property to char from CHAR-LIST in STRING."
1300 (goto-char (point-min))
1301 (while (re-search-forward (regexp-opt char-list
) nil t
)
1302 (add-text-properties (match-beginning 0)
1303 (match-end 0) '(org-protected t
)))
1306 (defun org-export-latex-keywords-maybe (&optional remove-list
)
1307 "Maybe remove keywords depending on rules in REMOVE-LIST."
1308 (goto-char (point-min))
1309 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1
"\\|"))
1310 (case-fold-search nil
)
1311 (todo-markup org-export-latex-todo-keyword-markup
)
1313 ;; convert TODO keywords
1314 (when (re-search-forward (concat "^\\(" re-todo
"\\)") nil t
)
1315 (if (plist-get remove-list
:todo
)
1318 ((stringp todo-markup
) todo-markup
)
1319 ((and (consp todo-markup
) (stringp (car todo-markup
)))
1320 (if (member (match-string 1) org-export-latex-done-keywords
)
1321 (cdr todo-markup
) (car todo-markup
)))
1322 (t (cdr (or (assoc (match-string 1) todo-markup
)
1323 (car todo-markup
))))))
1324 (replace-match (org-export-latex-protect-string
1325 (format fmt
(match-string 1))) t t
)))
1326 ;; convert priority string
1327 (when (re-search-forward "\\[\\\\#.\\]" nil t
)
1328 (if (plist-get remove-list
:priority
)
1330 (replace-match (format "\\textbf{%s}" (match-string 0)) t t
)))
1332 (when (re-search-forward "\\(:[a-zA-Z0-9_@#%]+\\)+:" nil t
)
1333 (if (or (not org-export-with-tags
)
1334 (plist-get remove-list
:tags
))
1337 (org-export-latex-protect-string
1338 (format "\\textbf{%s}"
1340 (replace-regexp-in-string
1341 "_" "\\\\_" (match-string 0)))))
1344 (defun org-export-latex-fontify-headline (string)
1345 "Fontify special words in STRING."
1347 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
1348 ;; the beginning of the buffer - inserting "\n" is safe here though.
1349 (insert "\n" string
)
1350 (goto-char (point-min))
1351 (let ((re (concat "\\\\\\([a-zA-Z]+\\)"
1352 "\\(?:<[^<>\n]*>\\)*"
1353 "\\(?:\\[[^][\n]*?\\]\\)*"
1354 "\\(?:<[^<>\n]*>\\)*"
1356 (org-create-multibrace-regexp "{" "}" 3)
1358 (while (re-search-forward re nil t
)
1360 ;; check for comment line
1361 (save-excursion (goto-char (match-beginning 0))
1362 (org-in-indented-comment-line))
1363 ;; Check if this is a defined entity, so that is may need conversion
1364 (org-entity-get (match-string 1)))
1365 (add-text-properties (match-beginning 0) (match-end 0)
1366 '(org-protected t
)))))
1367 (when (plist-get org-export-latex-options-plist
:emphasize
)
1368 (org-export-latex-fontify))
1369 (org-export-latex-keywords-maybe)
1370 (org-export-latex-special-chars
1371 (plist-get org-export-latex-options-plist
:sub-superscript
))
1372 (org-export-latex-links)
1373 (org-trim (buffer-string))))
1375 (defun org-export-latex-time-stamps ()
1376 "Format time stamps."
1377 (goto-char (point-min))
1378 (let ((org-display-custom-times org-export-latex-display-custom-times
))
1379 (while (re-search-forward org-ts-regexp-both nil t
)
1380 (org-if-unprotected-at (1- (point))
1382 (org-export-latex-protect-string
1383 (format org-export-latex-timestamp-markup
1384 (substring (org-translate-time (match-string 0)) 1 -
1)))
1387 (defun org-export-latex-quotation-marks ()
1388 "Export quotation marks depending on language conventions."
1389 (let* ((lang (plist-get org-export-latex-options-plist
:language
))
1390 (quote-rpl (if (equal lang
"fr")
1391 '(("\\(\\s-\\)\"" "«~")
1392 ("\\(\\S-\\)\"" "~»")
1393 ("\\(\\s-\\)'" "`"))
1394 '(("\\(\\s-\\|[[(]\\)\"" "``")
1395 ("\\(\\S-\\)\"" "''")
1396 ("\\(\\s-\\|(\\)'" "`")))))
1397 (mapc (lambda(l) (goto-char (point-min))
1398 (while (re-search-forward (car l
) nil t
)
1399 (let ((rpl (concat (match-string 1)
1400 (org-export-latex-protect-string
1401 (copy-sequence (cadr l
))))))
1402 (org-if-unprotected-1
1403 (replace-match rpl t t
))))) quote-rpl
)))
1405 (defun org-export-latex-special-chars (sub-superscript)
1406 "Export special characters to LaTeX.
1407 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
1408 See the `org-export-latex.el' code for a complete conversion table."
1409 (goto-char (point-min))
1411 (goto-char (point-min))
1412 (while (re-search-forward c nil t
)
1413 ;; Put the point where to check for org-protected
1414 (unless (get-text-property (match-beginning 2) 'org-protected
)
1415 (cond ((member (match-string 2) '("\\$" "$"))
1416 (if (equal (match-string 2) "\\$")
1418 (replace-match "\\$" t t
)))
1419 ((member (match-string 2) '("&" "%" "#"))
1420 (if (equal (match-string 1) "\\")
1421 (replace-match (match-string 2) t t
)
1422 (replace-match (concat (match-string 1) "\\"
1423 (match-string 2)) t t
)
1425 ((equal (match-string 2) "...")
1427 (concat (match-string 1)
1428 (org-export-latex-protect-string "\\ldots{}")) t t
))
1429 ((equal (match-string 2) "~")
1430 (cond ((equal (match-string 1) "\\") nil
)
1431 ((eq 'org-link
(get-text-property 0 'face
(match-string 2)))
1432 (replace-match (concat (match-string 1) "\\~") t t
))
1434 (org-export-latex-protect-string
1435 (concat (match-string 1) "\\~{}")) t t
))))
1436 ((member (match-string 2) '("{" "}"))
1437 (unless (save-match-data (org-inside-latex-math-p))
1438 (if (equal (match-string 1) "\\")
1439 (replace-match (match-string 2) t t
)
1440 (replace-match (concat (match-string 1) "\\"
1441 (match-string 2)) t t
)))))
1442 (unless (save-match-data (org-inside-latex-math-p))
1443 (cond ((equal (match-string 2) "\\")
1444 (replace-match (or (save-match-data
1445 (org-export-latex-treat-backslash-char
1447 (or (match-string 3) "")))
1449 (when (and (get-text-property (1- (point)) 'org-entity
)
1451 ;; OK, this was an entity replacement, and the user
1452 ;; had terminated the entity with {}. Make sure
1453 ;; {} is protected as well, and remove the extra {}
1454 ;; inserted by the conversion.
1455 (put-text-property (point) (+ 2 (point)) 'org-protected t
)
1456 (if (save-excursion (goto-char (max (- (point) 2) (point-min)))
1461 ((member (match-string 2) '("_" "^"))
1462 (replace-match (or (save-match-data
1463 (org-export-latex-treat-sub-super-char
1467 (match-string 3))) "") t t
)
1468 (backward-char 1)))))))
1469 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
1470 "\\(\\(\\\\?\\$\\)\\)"
1471 "\\([a-zA-Z0-9()]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-zA-Z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-zA-Z0-9]+}\\|([a-zA-Z0-9]+)\\)"
1472 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|\\([&#%{}\"]\\|[a-zA-Z][a-zA-Z0-9]*\\)\\)"
1473 "\\(.\\|^\\)\\(&\\)"
1474 "\\(.\\|^\\)\\(#\\)"
1475 "\\(.\\|^\\)\\(%\\)"
1476 "\\(.\\|^\\)\\({\\)"
1477 "\\(.\\|^\\)\\(}\\)"
1478 "\\(.\\|^\\)\\(~\\)"
1479 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
1480 ;; (?\< . "\\textless{}")
1481 ;; (?\> . "\\textgreater{}")
1484 (defun org-inside-latex-math-p ()
1485 (get-text-property (point) 'org-latex-math
))
1487 (defun org-export-latex-treat-sub-super-char
1488 (subsup char string-before string-after
)
1489 "Convert the \"_\" and \"^\" characters to LaTeX.
1490 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
1491 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
1492 (cond ((equal string-before
"\\")
1493 (concat string-before char string-after
))
1494 ((and (string-match "\\S-+" string-after
))
1495 ;; this is part of a math formula
1496 (cond ((eq 'org-link
(get-text-property 0 'face char
))
1497 (concat string-before
"\\" char string-after
))
1498 ((save-match-data (org-inside-latex-math-p))
1500 (cond ((eq 1 (length string-after
))
1501 (concat string-before char string-after
))
1502 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after
)
1503 (format "%s%s{%s}" string-before char
1504 (match-string 1 string-after
))))))
1505 ((and (> (length string-after
) 1)
1507 (and (equal subsup
'{}) (eq (string-to-char string-after
) ?\
{)))
1508 (or (string-match "[{]?\\([^}]+\\)[}]?" string-after
)
1509 (string-match "[(]?\\([^)]+\\)[)]?" string-after
)))
1511 (org-export-latex-protect-string
1512 (format "%s$%s{%s}$" string-before char
1513 (if (and (> (match-end 1) (1+ (match-beginning 1)))
1514 (not (equal (substring string-after
0 2) "{\\")))
1515 (concat "\\mathrm{" (match-string 1 string-after
) "}")
1516 (match-string 1 string-after
)))))
1517 ((eq subsup t
) (concat string-before
"$" char string-after
"$"))
1518 (t (org-export-latex-protect-string
1519 (concat string-before
"\\" char
"{}" string-after
)))))
1520 (t (org-export-latex-protect-string
1521 (concat string-before
"\\" char
"{}" string-after
)))))
1523 (defun org-export-latex-treat-backslash-char (string-before string-after
)
1524 "Convert the \"$\" special character to LaTeX.
1525 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
1526 (let ((ass (org-entity-get string-after
)))
1530 (concat string-before
1531 (org-export-latex-protect-string
1532 (concat "$" (nth 1 ass
) "$")))
1533 (concat string-before
(org-export-latex-protect-string
1536 ((and (not (string-match "^[ \n\t]" string-after
))
1537 (not (string-match "[ \t]\\'\\|^" string-before
)))
1538 ;; backslash is inside a word
1539 (concat string-before
1540 (org-export-latex-protect-string
1541 (concat "\\textbackslash{}" string-after
))))
1542 ((not (or (equal string-after
"")
1543 (string-match "^[ \t\n]" string-after
)))
1544 ;; backslash might escape a character (like \#) or a user TeX
1545 ;; macro (like \setcounter)
1546 (concat string-before
1547 (org-export-latex-protect-string (concat "\\" string-after
))))
1548 ((and (string-match "^[ \t\n]" string-after
)
1549 (string-match "[ \t\n]\\'" string-before
))
1550 ;; backslash is alone, convert it to $\backslash$
1551 (org-export-latex-protect-string
1552 (concat string-before
"\\textbackslash{}" string-after
)))
1553 (t (org-export-latex-protect-string
1554 (concat string-before
"\\textbackslash{}" string-after
))))))
1556 (defun org-export-latex-keywords ()
1557 "Convert special keywords to LaTeX."
1558 (goto-char (point-min))
1559 (while (re-search-forward org-export-latex-special-keyword-regexp nil t
)
1560 (replace-match (format org-export-latex-timestamp-keyword-markup
1561 (match-string 0)) t t
)
1563 (beginning-of-line 1)
1564 (unless (looking-at ".*\n[ \t]*\n")
1568 (defun org-export-latex-fixed-width (opt)
1569 "When OPT is non-nil convert fixed-width sections to LaTeX."
1570 (goto-char (point-min))
1571 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t
)
1572 (unless (get-text-property (point) 'org-example
)
1574 (progn (goto-char (match-beginning 0))
1575 (insert "\\begin{verbatim}\n")
1576 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1577 (replace-match (concat (match-string 1)
1578 (match-string 2)) t t
)
1580 (insert "\\end{verbatim}\n\n"))
1581 (progn (goto-char (match-beginning 0))
1582 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1583 (replace-match (concat "%" (match-string 1)
1584 (match-string 2)) t t
)
1585 (forward-line)))))))
1587 (defvar org-table-last-alignment
) ; defined in org-table.el
1588 (defvar org-table-last-column-widths
) ; defined in org-table.el
1589 (declare-function orgtbl-to-latex
"org-table" (table params
) t
)
1590 (defun org-export-latex-tables (insert)
1591 "Convert tables to LaTeX and INSERT it."
1592 ;; First, get the table.el tables
1593 (goto-char (point-min))
1594 (while (re-search-forward "^[ \t]*\\(\\+-[-+]*\\+\\)[ \t]*\n[ \t]*|" nil t
)
1597 (org-export-latex-convert-table.el-table
)))
1599 ;; And now the Org-mode tables
1600 (goto-char (point-min))
1601 (while (re-search-forward "^\\([ \t]*\\)|" nil t
)
1602 (org-if-unprotected-at (1- (point))
1604 (let* ((beg (org-table-begin))
1605 (end (org-table-end))
1606 (raw-table (buffer-substring beg end
))
1607 (org-table-last-alignment (copy-sequence org-table-last-alignment
))
1608 (org-table-last-column-widths (copy-sequence
1609 org-table-last-column-widths
))
1610 fnum fields line lines olines gr colgropen line-fmt align
1611 caption shortn label attr floatp placement longtblp
)
1612 (if org-export-latex-tables-verbatim
1613 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1614 "\\end{verbatim}\n")))
1615 (apply 'delete-region
(list beg end
))
1616 (insert (org-export-latex-protect-string tbl
)))
1618 (setq caption
(org-find-text-property-in-string
1619 'org-caption raw-table
)
1620 shortn
(org-find-text-property-in-string
1621 'org-caption-shortn raw-table
)
1622 attr
(org-find-text-property-in-string
1623 'org-attributes raw-table
)
1624 label
(org-find-text-property-in-string
1625 'org-label raw-table
)
1626 longtblp
(and attr
(stringp attr
)
1627 (string-match "\\<longtable\\>" attr
))
1628 align
(and attr
(stringp attr
)
1629 (string-match "\\<align=\\([^ \t\n\r]+\\)" attr
)
1630 (match-string 1 attr
))
1631 floatp
(or caption label
)
1632 placement
(if (and attr
1634 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr
))
1635 (match-string 1 attr
)
1637 (setq caption
(and caption
(org-export-latex-fontify-headline caption
)))
1638 (setq lines
(org-split-string raw-table
"\n"))
1639 (apply 'delete-region
(list beg end
))
1640 (when org-export-table-remove-special-lines
1641 (setq lines
(org-table-clean-before-export lines
'maybe-quoted
)))
1642 (when org-table-clean-did-remove-column
1643 (pop org-table-last-alignment
)
1644 (pop org-table-last-column-widths
))
1645 ;; make a formatting string to reflect alignment
1647 (while (and (not line-fmt
) (setq line
(pop olines
)))
1648 (unless (string-match "^[ \t]*|-" line
)
1649 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
1650 (setq fnum
(make-vector (length fields
) 0))
1654 (setq gr
(pop org-table-colgroup-info
))
1656 (cond ((eq gr
:start
)
1657 (prog1 (if colgropen
"|" "|")
1658 (setq colgropen t
)))
1660 (prog1 (if colgropen
"|" "|")
1661 (setq colgropen nil
)))
1663 (if (memq gr
'(:end
:startend
))
1664 (progn (setq colgropen nil
) "|")
1667 ;; fix double || in line-fmt
1668 (setq line-fmt
(replace-regexp-in-string "||" "|" line-fmt
))
1669 ;; maybe remove the first and last "|"
1670 (when (and (not org-export-latex-tables-column-borders
)
1671 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt
))
1672 (setq line-fmt
(match-string 2 line-fmt
)))
1675 (setq align
(apply 'format
1677 (mapcar (lambda (x) (if x
"r" "l"))
1678 org-table-last-alignment
)))))
1679 ;; prepare the table to send to orgtbl-to-latex
1683 (or (and (string-match "[ \t]*|-+" elem
) 'hline
)
1684 (org-split-string (org-trim elem
) "|")))
1687 (insert (org-export-latex-protect-string
1690 (concat "\\begin{longtable}{" align
"}\n")
1691 (if floatp
(format "\\begin{table}%s\n" placement
)))
1694 "\\caption%s{%s} %s"
1695 (if shortn
(concat "[" shortn
"]") "")
1697 (if label
(format "\\label{%s}" label
) "")))
1698 (if (and longtblp caption
) "\\\\\n" "\n")
1699 (if (and org-export-latex-tables-centered
(not longtblp
))
1700 "\\begin{center}\n")
1702 (format "\\begin{%s}{%s}\n"
1703 org-export-latex-tabular-environment align
))
1706 `(:tstart nil
:tend nil
1707 :hlend
,(if longtblp
1711 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
1713 \\endlastfoot" (length org-table-last-alignment
))
1716 (format "\n\\end{%s}"
1717 org-export-latex-tabular-environment
))
1718 (if longtblp
"\n" (if org-export-latex-tables-centered
1719 "\n\\end{center}\n" "\n"))
1722 (if floatp
"\\end{table}"))))
1725 (defun org-export-latex-convert-table.el-table
()
1726 "Replace table.el table at point with LaTeX code."
1727 (let (tbl caption shortn label line floatp attr align rmlines
)
1728 (setq line
(buffer-substring (point-at-bol) (point-at-eol))
1729 label
(org-get-text-property-any 0 'org-label line
)
1730 caption
(org-get-text-property-any 0 'org-caption line
)
1731 shortn
(org-get-text-property-any 0 'org-caption-shortn line
)
1732 attr
(org-get-text-property-any 0 'org-attributes line
)
1733 align
(and attr
(stringp attr
)
1734 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr
)
1735 (match-string 1 attr
))
1736 rmlines
(and attr
(stringp attr
)
1737 (string-match "\\<rmlines\\>" attr
))
1738 floatp
(or label caption
))
1739 (and (get-buffer "*org-export-table*")
1740 (kill-buffer (get-buffer "*org-export-table*")))
1741 (table-generate-source 'latex
"*org-export-table*" "caption")
1742 (setq tbl
(with-current-buffer "*org-export-table*"
1744 (while (string-match "^%.*\n" tbl
)
1745 (setq tbl
(replace-match "" t t tbl
)))
1749 (setq lines
(mapcar (lambda (x)
1750 (if (string-match "^\\\\hline$" x
)
1755 (org-split-string tbl
"\n")))
1756 (setq tbl
(mapconcat 'identity
(delq nil lines
) "\n"))))
1757 (when (and align
(string-match "\\\\begin{tabular}{.*}" tbl
))
1758 (setq tbl
(replace-match (concat "\\begin{tabular}{" align
"}")
1760 (and (get-buffer "*org-export-table*")
1761 (kill-buffer (get-buffer "*org-export-table*")))
1762 (beginning-of-line 0)
1763 (while (looking-at "[ \t]*\\(|\\|\\+-\\)")
1764 (delete-region (point) (1+ (point-at-eol))))
1765 (when org-export-latex-tables-centered
1766 (setq tbl
(concat "\\begin{center}\n" tbl
"\\end{center}")))
1768 (setq tbl
(concat "\\begin{table}\n"
1769 (format "\\caption%s{%s}%s\n"
1770 (if shortn
(format "[%s]" shortn
) "")
1771 (if label
(format "\\label{%s}" label
) "")
1774 "\n\\end{table}\n")))
1775 (insert (org-export-latex-protect-string tbl
))))
1777 (defun org-export-latex-fontify ()
1778 "Convert fontification to LaTeX."
1779 (goto-char (point-min))
1780 (while (re-search-forward org-emph-re nil t
)
1781 ;; The match goes one char after the *string*, except at the end of a line
1782 (let ((emph (assoc (match-string 3)
1783 org-export-latex-emphasis-alist
))
1784 (beg (match-beginning 0))
1788 (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
1790 (unless (or (and (get-text-property (- (point) 2) 'org-protected
)
1791 (not (get-text-property
1792 (- (point) 2) 'org-verbatim-emph
)))
1793 (equal (char-after (match-beginning 3))
1794 (char-after (1+ (match-beginning 3))))
1796 (goto-char (match-beginning 1))
1798 (and (org-at-table-p)
1800 "[|\n]" (buffer-substring beg end
)))))
1801 (and (equal (match-string 3) "+")
1803 (string-match "\\`-+\\'" (match-string 4)))))
1804 (setq s
(match-string 4))
1805 (setq rpl
(concat (match-string 1)
1806 (org-export-latex-emph-format (cadr emph
)
1810 (setq rpl
(org-export-latex-protect-string rpl
))
1812 (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl
)
1814 (add-text-properties (match-beginning 1) (match-end 1)
1815 '(org-protected t
) rpl
)
1816 (add-text-properties (match-beginning 3) (match-end 3)
1817 '(org-protected t
) rpl
)))))
1818 (replace-match rpl t t
)))
1821 (defvar org-export-latex-use-verb nil
)
1822 (defun org-export-latex-emph-format (format string
)
1823 "Format an emphasis string and handle the \\verb special case."
1824 (when (equal format
"\\verb")
1826 (if org-export-latex-use-verb
1827 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1829 (loop for i from
0 to
(1- (length ll
)) do
1830 (if (not (string-match (regexp-quote (substring ll i
(1+ i
)))
1833 (setq format
(concat "\\verb" (substring ll i
(1+ i
))
1834 "%s" (substring ll i
(1+ i
))))
1835 (throw 'exit nil
))))))
1837 (trans '(("\\" .
"\\textbackslash{}")
1838 ("~" .
"\\textasciitilde{}")
1839 ("^" .
"\\textasciicircum{}")))
1841 (while (string-match "[\\{}$%&_#~^]" string
)
1842 (setq char
(match-string 0 string
))
1843 (if (> (match-beginning 0) 0)
1844 (setq rtn
(concat rtn
(substring string
1845 0 (match-beginning 0)))))
1846 (setq string
(substring string
(1+ (match-beginning 0))))
1847 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
1848 rtn
(concat rtn char
)))
1849 (setq string
(concat rtn string
) format
"\\texttt{%s}")))))
1850 (format format string
))
1852 (defun org-export-latex-links ()
1853 ;; Make sure to use the LaTeX hyperref and graphicx package
1854 ;; or send some warnings.
1855 "Convert links to LaTeX."
1856 (goto-char (point-min))
1857 (while (re-search-forward org-bracket-link-analytic-regexp
++ nil t
)
1858 (org-if-unprotected-1
1859 (goto-char (match-beginning 0))
1860 (let* ((re-radio org-export-latex-all-targets-re
)
1861 (remove (list (match-beginning 0) (match-end 0)))
1862 (raw-path (org-extract-attributes (match-string 3)))
1863 (full-raw-path (concat (match-string 1) raw-path
))
1864 (desc (match-string 5))
1865 (type (or (match-string 2)
1866 (if (or (file-name-absolute-p raw-path
)
1867 (string-match "^\\.\\.?/" raw-path
))
1869 (coderefp (equal type
"coderef"))
1870 (caption (org-find-text-property-in-string 'org-caption raw-path
))
1871 (shortn (org-find-text-property-in-string 'org-caption-shortn raw-path
))
1872 (attr (or (org-find-text-property-in-string 'org-attributes raw-path
)
1873 (plist-get org-export-latex-options-plist
:latex-image-options
)))
1874 (label (org-find-text-property-in-string 'org-label raw-path
))
1876 ;; define the path of the link
1878 ((member type
'("coderef"))
1880 ((member type
'("http" "https" "ftp"))
1881 (concat type
":" raw-path
))
1882 ((and re-radio
(string-match re-radio raw-path
))
1884 ((equal type
"mailto")
1885 (concat type
":" raw-path
))
1886 ((equal type
"file")
1887 (if (and (org-file-image-p
1890 org-export-latex-inline-image-extensions
)
1891 (or (get-text-property 0 'org-no-description
1893 (equal desc full-raw-path
)))
1895 (progn (when (string-match "\\(.+\\)::.+" raw-path
)
1896 (setq raw-path
(match-string 1 raw-path
)))
1897 (if (file-exists-p raw-path
)
1898 (concat type
"://" (expand-file-name raw-path
))
1899 (concat type
"://" (org-export-directory
1900 :LaTeX org-export-latex-options-plist
)
1902 ;; process with link inserting
1903 (apply 'delete-region remove
)
1904 (setq caption
(and caption
(org-export-latex-fontify-headline caption
)))
1906 (plist-get org-export-latex-options-plist
:inline-images
))
1907 ;; OK, we need to inline an image
1909 (org-export-latex-format-image raw-path caption label attr shortn
)))
1912 (org-export-get-coderef-format path desc
)
1913 (cdr (assoc path org-export-code-refs
)))))
1914 (radiop (insert (format "\\hyperref[%s]{%s}"
1915 (org-solidify-link-text raw-path
) desc
)))
1917 (insert (format "\\hyperref[%s]{%s}"
1918 (org-remove-initial-hash
1919 (org-solidify-link-text raw-path
))
1922 (when (org-at-table-p)
1923 ;; There is a strange problem when we have a link in a table,
1924 ;; ampersands then cause a problem. I think this must be
1925 ;; a LaTeX issue, but we here implement a work-around anyway.
1926 (setq path
(org-export-latex-protect-amp path
)
1927 desc
(org-export-latex-protect-amp desc
)))
1928 (insert (format org-export-latex-hyperref-format path desc
)))
1930 ((functionp (setq fnc
(nth 2 (assoc type org-link-protocols
))))
1931 ;; The link protocol has a function for formatting the link
1934 (funcall fnc
(org-link-unescape raw-path
) desc
'latex
))))
1936 (t (insert "\\texttt{" desc
"}")))))))
1939 (defun org-export-latex-format-image (path caption label attr
&optional shortn
)
1940 "Format the image element, depending on user settings."
1941 (let (ind floatp wrapp multicolumnp placement figenv
)
1942 (setq floatp
(or caption label
))
1943 (setq ind
(org-get-text-property-any 0 'original-indentation path
))
1944 (when (and attr
(stringp attr
))
1945 (if (string-match "[ \t]*\\<wrap\\>" attr
)
1946 (setq wrapp t floatp nil attr
(replace-match "" t t attr
)))
1947 (if (string-match "[ \t]*\\<float\\>" attr
)
1948 (setq wrapp nil floatp t attr
(replace-match "" t t attr
)))
1949 (if (string-match "[ \t]*\\<multicolumn\\>" attr
)
1950 (setq multicolumnp t attr
(replace-match "" t t attr
))))
1954 (wrapp "{l}{0.5\\textwidth}")
1958 (when (and attr
(stringp attr
)
1959 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr
))
1960 (setq placement
(match-string 1 attr
)
1961 attr
(replace-match "" t t attr
)))
1962 (setq attr
(and attr
(org-trim attr
)))
1963 (when (or (not attr
) (= (length attr
) 0))
1964 (setq attr
(cond (floatp "width=0.7\\textwidth")
1965 (wrapp "width=0.48\\textwidth")
1969 (wrapp "\\begin{wrapfigure}%placement
1971 \\includegraphics[%attr]{%path}
1972 \\caption%shortn{%labelcmd%caption}
1974 (multicolumnp "\\begin{figure*}%placement
1976 \\includegraphics[%attr]{%path}
1977 \\caption{%labelcmd%caption}
1979 (floatp "\\begin{figure}%placement
1981 \\includegraphics[%attr]{%path}
1982 \\caption{%labelcmd%caption}
1984 (t "\\includegraphics[%attr]{%path}")))
1987 (setq figenv
(mapconcat 'identity
(split-string figenv
"\n")
1988 (save-excursion (beginning-of-line 1)
1989 (looking-at "[ \t]*")
1990 (concat "\n" (match-string 0)))))
1992 (if (and (not label
) (not caption
)
1993 (string-match "^\\\\caption{.*\n" figenv
))
1994 (setq figenv
(replace-match "" t t figenv
)))
1999 (if (file-name-absolute-p path
)
2000 (expand-file-name path
)
2003 (cons "shortn" (if shortn
(format "[%s]" shortn
) ""))
2004 (cons "labelcmd" (if label
(format "\\label{%s}"
2006 (cons "caption" (or caption
""))
2007 (cons "placement" (or placement
""))))
2008 nil
'original-indentation ind
)))
2010 (defun org-export-latex-protect-amp (s)
2011 (while (string-match "\\([^\\\\]\\)\\(&\\)" s
)
2012 (setq s
(replace-match (concat (match-string 1 s
) "\\" (match-string 2 s
))
2016 (defun org-remove-initial-hash (s)
2017 (if (string-match "\\`#" s
)
2020 (defvar org-latex-entities
) ; defined below
2021 (defvar org-latex-entities-regexp
) ; defined below
2023 (defun org-export-latex-preprocess (parameters)
2024 "Clean stuff in the LaTeX export."
2025 ;; Preserve line breaks
2026 (goto-char (point-min))
2027 (while (re-search-forward "\\\\\\\\" nil t
)
2028 (add-text-properties (match-beginning 0) (match-end 0)
2029 '(org-protected t
)))
2031 ;; Preserve latex environments
2032 (goto-char (point-min))
2033 (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t
)
2035 (let* ((start (progn (beginning-of-line) (point)))
2036 (end (and (re-search-forward
2037 (concat "^[ \t]*\\\\end{"
2038 (regexp-quote (match-string 1))
2042 (add-text-properties start end
'(org-protected t
))
2043 (goto-char (point-at-eol))))))
2045 ;; Preserve math snippets
2047 (let* ((matchers (plist-get org-format-latex-options
:matchers
))
2048 (re-list org-latex-regexps
)
2049 beg end re e m n block off
)
2050 ;; Check the different regular expressions
2051 (while (setq e
(pop re-list
))
2052 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
2053 block
(if (nth 3 e
) "\n\n" ""))
2054 (setq off
(if (member m
'("$" "$1")) 1 0))
2055 (when (and (member m matchers
) (not (equal m
"begin")))
2056 (goto-char (point-min))
2057 (while (re-search-forward re nil t
)
2058 (setq beg
(+ (match-beginning 0) off
) end
(- (match-end 0) 0))
2059 (add-text-properties beg end
'(org-protected t org-latex-math t
))))))
2061 ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{}
2062 (goto-char (point-min))
2063 (let ((case-fold-search nil
))
2064 (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t
)
2065 (unless (eq (char-before (match-beginning 1)) ?
\\)
2066 (org-if-unprotected-1
2067 (replace-match (org-export-latex-protect-string
2068 (concat "\\" (match-string 1)
2071 ;; Convert blockquotes
2072 (goto-char (point-min))
2073 (while (search-forward "ORG-BLOCKQUOTE-START" nil t
)
2074 (org-replace-match-keep-properties "\\begin{quote}" t t
))
2075 (goto-char (point-min))
2076 (while (search-forward "ORG-BLOCKQUOTE-END" nil t
)
2077 (org-replace-match-keep-properties "\\end{quote}" t t
))
2080 (goto-char (point-min))
2081 (while (search-forward "ORG-VERSE-START" nil t
)
2082 (org-replace-match-keep-properties "\\begin{verse}" t t
)
2083 (beginning-of-line 2)
2084 (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
2085 (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
2086 (goto-char (match-end 1))
2087 (org-replace-match-keep-properties
2088 (org-export-latex-protect-string
2089 (concat "\\hspace*{1cm}" (match-string 2))) t t
)
2090 (beginning-of-line 1))
2091 (if (looking-at "[ \t]*$")
2092 (insert (org-export-latex-protect-string "\\vspace*{1em}"))
2093 (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
2096 (beginning-of-line 2))
2097 (and (looking-at "[ \t]*ORG-VERSE-END.*")
2098 (org-replace-match-keep-properties "\\end{verse}" t t
)))
2101 (goto-char (point-min))
2102 (while (search-forward "ORG-CENTER-START" nil t
)
2103 (org-replace-match-keep-properties "\\begin{center}" t t
))
2104 (goto-char (point-min))
2105 (while (search-forward "ORG-CENTER-END" nil t
)
2106 (org-replace-match-keep-properties "\\end{center}" t t
))
2108 (run-hooks 'org-export-latex-after-blockquotes-hook
)
2110 ;; Convert horizontal rules
2111 (goto-char (point-min))
2112 (while (re-search-forward "^----+.$" nil t
)
2114 (replace-match (org-export-latex-protect-string "\\hrule") t t
)))
2116 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
2117 (goto-char (point-min))
2119 "\\\\\\([a-zA-Z]+\\)"
2120 "\\(?:<[^<>\n]*>\\)*"
2121 "\\(?:\\[[^][\n]*?\\]\\)*"
2122 "\\(?:<[^<>\n]*>\\)*"
2123 "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}")))
2124 (while (re-search-forward re nil t
)
2126 ;; check for comment line
2127 (save-excursion (goto-char (match-beginning 0))
2128 (org-in-indented-comment-line))
2129 ;; Check if this is a defined entity, so that is may need conversion
2130 (org-entity-get (match-string 1))
2132 (add-text-properties (match-beginning 0) (match-end 0)
2133 '(org-protected t
)))))
2135 ;; Special case for \nbsp
2136 (goto-char (point-min))
2137 (while (re-search-forward "\\\\nbsp\\({}\\|\\>\\)" nil t
)
2139 (replace-match (org-export-latex-protect-string "~"))))
2141 ;; Protect LaTeX entities
2142 (goto-char (point-min))
2143 (while (re-search-forward org-latex-entities-regexp nil t
)
2145 (add-text-properties (match-beginning 0) (match-end 0)
2146 '(org-protected t
))))
2148 ;; Replace radio links
2149 (goto-char (point-min))
2150 (while (re-search-forward
2151 (concat "<<<?" org-export-latex-all-targets-re
2152 ">>>?\\((INVISIBLE)\\)?") nil t
)
2153 (org-if-unprotected-at (+ (match-beginning 0) 2)
2156 (org-export-latex-protect-string
2157 (format "\\label{%s}" (save-match-data (org-solidify-link-text
2158 (match-string 1)))))
2159 (if (match-string 2) "" (match-string 1)))
2162 ;; Delete @<...> constructs
2163 ;; Thanks to Daniel Clemente for this regexp
2164 (goto-char (point-min))
2165 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t
)
2167 (replace-match "")))
2169 ;; When converting to LaTeX, replace footnotes
2170 ;; FIXME: don't protect footnotes from conversion
2171 (when (plist-get org-export-latex-options-plist
:footnotes
)
2172 (goto-char (point-min))
2173 (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t
)
2175 (when (and (save-match-data
2176 (save-excursion (beginning-of-line)
2177 (looking-at "[^:|#]")))
2178 (not (org-in-verbatim-emphasis)))
2179 (let ((foot-beg (match-beginning 0))
2180 (foot-end (match-end 0))
2181 (foot-prefix (match-string 0))
2182 footnote footnote-rpl
)
2184 (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix
))
2186 (replace-match (org-export-latex-protect-string
2187 (concat "$^{" (match-string 1) "}$")))
2189 (let ((end (save-excursion
2190 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t
)
2191 (match-beginning 0) (point-max)))))
2192 (setq footnote
(concat (org-trim (buffer-substring (point) end
))
2193 " ")) ; prevent last } being part of a link
2194 (delete-region (point) end
))
2195 (goto-char foot-beg
)
2196 (delete-region foot-beg foot-end
)
2197 (unless (null footnote
)
2198 (setq footnote-rpl
(format "\\footnote{%s}" footnote
))
2199 (add-text-properties 0 10 '(org-protected t
) footnote-rpl
)
2200 (add-text-properties (1- (length footnote-rpl
))
2201 (length footnote-rpl
)
2202 '(org-protected t
) footnote-rpl
)
2203 (if (org-on-heading-p)
2205 (concat (org-export-latex-protect-string "\\protect")
2207 (insert footnote-rpl
)))
2210 ;; Remove footnote section tag for LaTeX
2211 (goto-char (point-min))
2212 (while (re-search-forward
2213 (concat "^" footnote-section-tag-regexp
) nil t
)
2215 (replace-match "")))))
2217 (defun org-export-latex-fix-inputenc ()
2218 "Set the coding system in inputenc to what the buffer is."
2219 (let* ((cs buffer-file-coding-system
)
2220 (opt (or (ignore-errors (latexenc-coding-system-to-inputenc cs
))
2223 ;; Translate if that is requested
2224 (setq opt
(or (cdr (assoc opt org-export-latex-inputenc-alist
)) opt
))
2225 ;; find the \usepackage statement and replace the option
2226 (goto-char (point-min))
2227 (while (re-search-forward "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
2229 (goto-char (match-beginning 1))
2230 (delete-region (match-beginning 1) (match-end 1))
2232 (and buffer-file-name
2237 (defun org-export-latex-lists ()
2238 "Convert plain text lists in current buffer into LaTeX lists."
2240 (goto-char (point-min))
2241 (while (org-search-forward-unenclosed org-item-beginning-re nil t
)
2243 (setq res
(org-list-to-latex (org-list-parse-list t
)
2244 org-export-latex-list-parameters
))
2245 (while (string-match "^\\(\\\\item[ \t]+\\)\\[@\\(?:start:\\)?\\([0-9]+\\)\\]"
2247 (setq res
(replace-match
2248 (concat (format "\\setcounter{enumi}{%d}"
2249 (1- (string-to-number
2250 (match-string 2 res
))))
2252 (match-string 1 res
))
2254 (insert res
"\n"))))
2256 (defconst org-latex-entities
2285 "\\begin{description}"
2286 "\\begin{enumerate}"
2290 "\\begin{flushleft}"
2291 "\\begin{flushright}"
2296 "\\begin{quotation}"
2301 "\\begin{thebibliography}"
2303 "\\begin{titlepage}"
2430 "A list of LaTeX commands to be protected when performing conversion.")
2432 (defconst org-latex-entities-regexp
2434 (dolist (x org-latex-entities
)
2435 (if (string-match "[a-zA-Z]$" x
)
2438 (concat "\\(" (regexp-opt (nreverse names
)) "\\>\\)"
2439 "\\|\\(" (regexp-opt (nreverse rest
)) "\\)")))
2441 (provide 'org-export-latex
)
2442 (provide 'org-latex
)
2444 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
2446 ;;; org-latex.el ends here