Save and restore outline visibility when pressing C-c C-c on a meta line
[rgr-org-mode.git] / lisp / org-latex.el
bloba04a9ef6d0cdc90882ad5387de3d584f87e6feec
1 ;;; org-latex.el --- LaTeX exporter for org-mode
2 ;;
3 ;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
4 ;;
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: org-latex.el
7 ;; Version: 6.33trans
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/>.
28 ;;; Commentary:
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'
44 ;;; Code:
46 (eval-when-compile
47 (require 'cl))
49 (require 'footnote)
50 (require 'org)
51 (require 'org-exp)
52 (require 'org-macs)
53 (require 'org-beamer)
55 ;;; Variables:
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
80 ;;; User variables:
82 (defgroup org-export-latex nil
83 "Options for exporting Org-mode files to LaTeX."
84 :tag "Org Export LaTeX"
85 :group 'org-export)
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
93 '(("article"
94 "\\documentclass[11pt]{article}
95 \\usepackage[utf8]{inputenc}
96 \\usepackage[T1]{fontenc}
97 \\usepackage{graphicx}
98 \\usepackage{longtable}
99 \\usepackage{float}
100 \\usepackage{wrapfig}
101 \\usepackage{soul}
102 \\usepackage{amssymb}
103 \\usepackage{hyperref}"
104 ("\\section{%s}" . "\\section*{%s}")
105 ("\\subsection{%s}" . "\\subsection*{%s}")
106 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
107 ("\\paragraph{%s}" . "\\paragraph*{%s}")
108 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
109 ("report"
110 "\\documentclass[11pt]{report}
111 \\usepackage[utf8]{inputenc}
112 \\usepackage[T1]{fontenc}
113 \\usepackage{graphicx}
114 \\usepackage{longtable}
115 \\usepackage{float}
116 \\usepackage{wrapfig}
117 \\usepackage{soul}
118 \\usepackage{amssymb}
119 \\usepackage{hyperref}"
120 ("\\part{%s}" . "\\part*{%s}")
121 ("\\chapter{%s}" . "\\chapter*{%s}")
122 ("\\section{%s}" . "\\section*{%s}")
123 ("\\subsection{%s}" . "\\subsection*{%s}")
124 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
125 ("book"
126 "\\documentclass[11pt]{book}
127 \\usepackage[utf8]{inputenc}
128 \\usepackage[T1]{fontenc}
129 \\usepackage{graphicx}
130 \\usepackage{longtable}
131 \\usepackage{float}
132 \\usepackage{wrapfig}
133 \\usepackage{soul}
134 \\usepackage{amssymb}
135 \\usepackage{hyperref}"
136 ("\\part{%s}" . "\\part*{%s}")
137 ("\\chapter{%s}" . "\\chapter*{%s}")
138 ("\\section{%s}" . "\\section*{%s}")
139 ("\\subsection{%s}" . "\\subsection*{%s}")
140 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
141 ("beamer"
142 "\\documentclass{beamer}
143 \\usepackage[utf8]{inputenc}
144 \\usepackage[T1]{fontenc}
145 \\usepackage{graphicx}
146 \\usepackage{longtable}
147 \\usepackage{float}
148 \\usepackage{wrapfig}
149 \\usepackage{soul}
150 \\usepackage{amssymb}
151 \\usepackage{hyperref}"
152 org-beamer-sectioning
154 "Alist of LaTeX classes and associated header and structure.
155 If #+LaTeX_CLASS is set in the buffer, use its value and the
156 associated information. Here is the structure of each cell:
158 \(class-name
159 header-string
160 (numbered-section . unnumbered-section\)
161 ...\)
163 A %s formatter is mandatory in each section string and will be
164 replaced by the title of the section.
166 Instead of a cons cell (numbered . unnumbered), you can also provide a list
167 of 2-4 elements,
169 (numbered-open numbered-close)
173 (numbered-open numbered-close unnumbered-open unnumbered-close)
175 providing opening and closing strings for an environment that should
176 represent the document section. The opening clause should have a %s
177 to represent the section title.
179 Instead of a list of sectioning commands, you can also specify a
180 function name. that function will be called with two parameters,
181 the (reduced) level of the headline, and the headline text. The functions
182 returns a cons cell with the (possibly modified) headline text, and the
183 sectioning list in the cdr."
184 :group 'org-export-latex
185 :type '(repeat
186 (list (string :tag "LaTeX class")
187 (string :tag "LaTeX header")
188 (repeat :tag "Levels" :inline t
189 (choice
190 (cons :tag "Heading"
191 (string :tag " numbered")
192 (string :tag "unnumbered"))
193 (list :tag "Environment"
194 (string :tag "Opening (numbered)")
195 (string :tag "Closing (numbered)")
196 (string :tag "Opening (unnumbered)")
197 (string :tag "Closing (unnumbered)"))
198 (function :tag "Hook computing sectioning"))))))
200 (defcustom org-export-latex-emphasis-alist
201 '(("*" "\\textbf{%s}" nil)
202 ("/" "\\emph{%s}" nil)
203 ("_" "\\underline{%s}" nil)
204 ("+" "\\st{%s}" nil)
205 ("=" "\\verb" t)
206 ("~" "\\verb" t))
207 "Alist of LaTeX expressions to convert emphasis fontifiers.
208 Each element of the list is a list of three elements.
209 The first element is the character used as a marker for fontification.
210 The second element is a formatting string to wrap fontified text with.
211 If it is \"\\verb\", Org will automatically select a delimiter
212 character that is not in the string.
213 The third element decides whether to protect converted text from other
214 conversions."
215 :group 'org-export-latex
216 :type 'alist)
218 (defcustom org-export-latex-title-command "\\maketitle"
219 "The command used to insert the title just after \\begin{document}.
220 If this string contains the formatting specification \"%s\" then
221 it will be used as a formatting string, passing the title as an
222 argument."
223 :group 'org-export-latex
224 :type 'string)
226 (defcustom org-export-latex-import-inbuffer-stuff nil
227 "Non-nil means define TeX macros for Org's inbuffer definitions.
228 For example \orgTITLE for #+TITLE."
229 :group 'org-export-latex
230 :type 'boolean)
232 (defcustom org-export-latex-date-format
233 "%d %B %Y"
234 "Format string for \\date{...}."
235 :group 'org-export-latex
236 :type 'string)
238 (defcustom org-export-latex-todo-keyword-markup "\\textbf{%s}"
239 "Markup for TODO keywords, as a printf format.
240 This can be a single format for all keywords, a cons cell with separate
241 formats for not-done and done states, or an association list with setup
242 for individual keywords. If a keyword shows up for which there is no
243 markup defined, the first one in the association list will be used."
244 :group 'org-export-latex
245 :type '(choice
246 (string :tag "Default")
247 (cons :tag "Distinguish undone and done"
248 (string :tag "Not-DONE states")
249 (string :tag "DONE states"))
250 (repeat :tag "Per keyword markup"
251 (cons
252 (string :tag "Keyword")
253 (string :tag "Markup")))))
255 (defcustom org-export-latex-timestamp-markup "\\textit{%s}"
256 "A printf format string to be applied to time stamps."
257 :group 'org-export-latex
258 :type 'string)
260 (defcustom org-export-latex-timestamp-keyword-markup "\\texttt{%s}"
261 "A printf format string to be applied to time stamps."
262 :group 'org-export-latex
263 :type 'string)
265 (defcustom org-export-latex-tables-verbatim nil
266 "When non-nil, tables are exported verbatim."
267 :group 'org-export-latex
268 :type 'boolean)
270 (defcustom org-export-latex-tables-centered t
271 "When non-nil, tables are exported in a center environment."
272 :group 'org-export-latex
273 :type 'boolean)
275 (defcustom org-export-latex-tables-column-borders nil
276 "When non-nil, grouping columns can cause outer vertical lines in tables.
277 When nil, grouping causes only separation lines between groups."
278 :group 'org-export-latex
279 :type 'boolean)
281 (defcustom org-export-latex-low-levels 'itemize
282 "How to convert sections below the current level of sectioning.
283 This is specified by the `org-export-headline-levels' option or the
284 value of \"H:\" in Org's #+OPTION line.
286 This can be either nil (skip the sections), `description', `itemize',
287 or `enumerate' (convert the sections as the corresponding list type), or
288 a string to be used instead of \\section{%s}. In this latter case,
289 the %s stands here for the inserted headline and is mandatory.
291 It may also be a list of three string to define a user-defined environment
292 that should be used. The first string should be the like
293 \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
294 to two occurrences of %s for the title and a label, respectively. The third
295 string should be like \"\\end{itemize\"."
296 :group 'org-export-latex
297 :type '(choice (const :tag "Ignore" nil)
298 (const :tag "Convert as descriptive list" description)
299 (const :tag "Convert as itemized list" itemize)
300 (const :tag "Convert as enumerated list" enumerate)
301 (list :tag "User-defined environment"
302 :value ("\\begin{itemize}" "\\end{itemize}" "\\item %s")
303 (string :tag "Start")
304 (string :tag "End")
305 (string :tag "item"))
306 (string :tag "Use a section string" :value "\\subparagraph{%s}")))
308 (defcustom org-export-latex-list-parameters
309 '(:cbon "$\\boxtimes$" :cboff "$\\Box$")
310 "Parameters for the LaTeX list exporter.
311 These parameters will be passed on to `org-list-to-latex', which in turn
312 will pass them (combined with the LaTeX default list parameters) to
313 `org-list-to-generic'."
314 :group 'org-export-latex
315 :type 'plist)
317 (defcustom org-export-latex-verbatim-wrap
318 '("\\begin{verbatim}\n" . "\\end{verbatim}\n")
319 "Environment to be wrapped around a fixed-width section in LaTeX export.
320 This is a cons with two strings, to be added before and after the
321 fixed-with text.
323 Defaults to \\begin{verbatim} and \\end{verbatim}."
324 :group 'org-export-translation
325 :group 'org-export-latex
326 :type '(cons (string :tag "Open")
327 (string :tag "Close")))
329 (defcustom org-export-latex-listings nil
330 "Non-nil means, export source code using the listings package.
331 This package will fontify source code, possibly even with color.
332 If you want to use this, you also need to make LaTeX use the
333 listings package, and if you want to have color, the color
334 package. Just add these to `org-export-latex-packages-alist',
335 for example using customize, or with something like
337 (require 'org-latex)
338 (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
339 (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))"
340 :group 'org-export-latex
341 :type 'boolean)
343 (defcustom org-export-latex-listings-langs
344 '((emacs-lisp "Lisp") (lisp "Lisp")
345 (c "C") (cc "C++")
346 (fortran "fortran")
347 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
348 (html "HTML") (xml "XML")
349 (tex "TeX") (latex "TeX")
350 (shell-script "bash")
351 (gnuplot "Gnuplot")
352 (ocaml "Caml") (caml "Caml")
353 (sql "SQL"))
354 "Alist mapping languages to their listing language counterpart.
355 The key is a symbol, the major mode symbol without the \"-mode\".
356 The value is the string that should be inserted as the language parameter
357 for the listings package. If the mode name and the listings name are
358 the same, the language does not need an entry in this list - but it does not
359 hurt if it is present."
360 :group 'org-export-latex
361 :type '(repeat
362 (list
363 (symbol :tag "Major mode ")
364 (string :tag "Listings language"))))
366 (defcustom org-export-latex-remove-from-headlines
367 '(:todo nil :priority nil :tags nil)
368 "A plist of keywords to remove from headlines. OBSOLETE.
369 Non-nil means remove this keyword type from the headline.
371 Don't remove the keys, just change their values.
373 Obsolete, this variable is no longer used. Use the separate
374 variables `org-export-with-todo-keywords', `org-export-with-priority',
375 and `org-export-with-tags' instead."
376 :type 'plist
377 :group 'org-export-latex)
379 (defcustom org-export-latex-image-default-option "width=10em"
380 "Default option for images."
381 :group 'org-export-latex
382 :type 'string)
384 (defcustom org-export-latex-inline-image-extensions
385 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
386 "Extensions of image files that can be inlined into LaTeX.
387 Note that the image extension *actually* allowed depend on the way the
388 LaTeX file is processed. When used with pdflatex, pdf, jpg and png images
389 are OK. When processing through dvi to Postscript, only ps and eps are
390 allowed. The default we use here encompasses both."
391 :group 'org-export-latex
392 :type '(repeat (string :tag "Extension")))
394 (defcustom org-export-latex-coding-system nil
395 "Coding system for the exported LaTex file."
396 :group 'org-export-latex
397 :type 'coding-system)
399 (defgroup org-export-pdf nil
400 "Options for exporting Org-mode files to PDF, via LaTeX."
401 :tag "Org Export PDF"
402 :group 'org-export-latex
403 :group 'org-export)
405 (defcustom org-latex-to-pdf-process
406 '("pdflatex -interaction nonstopmode %s"
407 "pdflatex -interaction nonstopmode %s")
408 "Commands to process a LaTeX file to a PDF file.
409 This is a list of strings, each of them will be given to the shell
410 as a command. %s in the command will be replaced by the full file name, %b
411 by the file base name (i.e. without extension).
412 The reason why this is a list is that it usually takes several runs of
413 pdflatex, maybe mixed with a call to bibtex. Org does not have a clever
414 mechanism to detect which of these commands have to be run to get to a stable
415 result, and it also does not do any error checking.
417 Alternatively, this may be a Lisp function that does the processing, so you
418 could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
419 This function should accept the file name as its single argument."
420 :group 'org-export-pdf
421 :type '(choice (repeat :tag "Shell command sequence"
422 (string :tag "Shell command"))
423 (function)))
425 (defcustom org-export-pdf-logfiles
426 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
427 "The list of file extensions to consider as LaTeX logfiles."
428 :group 'org-export-pdf
429 :type '(repeat (string :tag "Extension")))
431 (defcustom org-export-pdf-remove-logfiles t
432 "Non-nil means, remove the logfiles produced by PDF production.
433 These are the .aux, .log, .out, and .toc files."
434 :group 'org-export-pdf
435 :type 'boolean)
437 ;;; Hooks
439 (defvar org-export-latex-after-initial-vars-hook nil
440 "Hook run before LaTeX export.
441 The exact moment is after the initial variables like org-export-latex-class
442 have been determined from the environment.")
444 (defvar org-export-latex-after-blockquotes-hook nil
445 "Hook run during LaTeX export, after blockquote, verse, center are done.")
447 (defvar org-export-latex-final-hook nil
448 "Hook run in the finalized LaTeX buffer.")
450 ;;; Autoload functions:
452 ;;;###autoload
453 (defun org-export-as-latex-batch ()
454 "Call `org-export-as-latex', may be used in batch processing.
455 For example:
457 emacs --batch
458 --load=$HOME/lib/emacs/org.el
459 --eval \"(setq org-export-headline-levels 2)\"
460 --visit=MyFile --funcall org-export-as-latex-batch"
461 (org-export-as-latex org-export-headline-levels 'hidden))
463 ;;;###autoload
464 (defun org-export-as-latex-to-buffer (arg)
465 "Call `org-export-as-latex` with output to a temporary buffer.
466 No file is created. The prefix ARG is passed through to `org-export-as-latex'."
467 (interactive "P")
468 (org-export-as-latex arg nil nil "*Org LaTeX Export*")
469 (when org-export-show-temporary-export-buffer
470 (switch-to-buffer-other-window "*Org LaTeX Export*")))
472 ;;;###autoload
473 (defun org-replace-region-by-latex (beg end)
474 "Replace the region from BEG to END with its LaTeX export.
475 It assumes the region has `org-mode' syntax, and then convert it to
476 LaTeX. This can be used in any buffer. For example, you could
477 write an itemized list in `org-mode' syntax in an LaTeX buffer and
478 then use this command to convert it."
479 (interactive "r")
480 (let (reg latex buf)
481 (save-window-excursion
482 (if (org-mode-p)
483 (setq latex (org-export-region-as-latex
484 beg end t 'string))
485 (setq reg (buffer-substring beg end)
486 buf (get-buffer-create "*Org tmp*"))
487 (with-current-buffer buf
488 (erase-buffer)
489 (insert reg)
490 (org-mode)
491 (setq latex (org-export-region-as-latex
492 (point-min) (point-max) t 'string)))
493 (kill-buffer buf)))
494 (delete-region beg end)
495 (insert latex)))
497 ;;;###autoload
498 (defun org-export-region-as-latex (beg end &optional body-only buffer)
499 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
500 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
501 contents, and only produce the region of converted text, useful for
502 cut-and-paste operations.
503 If BUFFER is a buffer or a string, use/create that buffer as a target
504 of the converted LaTeX. If BUFFER is the symbol `string', return the
505 produced LaTeX as a string and leave no buffer behind. For example,
506 a Lisp program could call this function in the following way:
508 (setq latex (org-export-region-as-latex beg end t 'string))
510 When called interactively, the output buffer is selected, and shown
511 in a window. A non-interactive call will only return the buffer."
512 (interactive "r\nP")
513 (when (interactive-p)
514 (setq buffer "*Org LaTeX Export*"))
515 (let ((transient-mark-mode t) (zmacs-regions t)
516 ext-plist rtn)
517 (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
518 (goto-char end)
519 (set-mark (point)) ;; to activate the region
520 (goto-char beg)
521 (setq rtn (org-export-as-latex
522 nil nil ext-plist
523 buffer body-only))
524 (if (fboundp 'deactivate-mark) (deactivate-mark))
525 (if (and (interactive-p) (bufferp rtn))
526 (switch-to-buffer-other-window rtn)
527 rtn)))
529 ;;;###autoload
530 (defun org-export-as-latex (arg &optional hidden ext-plist
531 to-buffer body-only pub-dir)
532 "Export current buffer to a LaTeX file.
533 If there is an active region, export only the region. The prefix
534 ARG specifies how many levels of the outline should become
535 headlines. The default is 3. Lower levels will be exported
536 depending on `org-export-latex-low-levels'. The default is to
537 convert them as description lists.
538 HIDDEN is obsolete and does nothing.
539 EXT-PLIST is a property list with
540 external parameters overriding org-mode's default settings, but
541 still inferior to file-local settings. When TO-BUFFER is
542 non-nil, create a buffer with that name and export to that
543 buffer. If TO-BUFFER is the symbol `string', don't leave any
544 buffer behind but just return the resulting LaTeX as a string.
545 When BODY-ONLY is set, don't produce the file header and footer,
546 simply return the content of \begin{document}...\end{document},
547 without even the \begin{document} and \end{document} commands.
548 when PUB-DIR is set, use this as the publishing directory."
549 (interactive "P")
550 (run-hooks 'org-export-first-hook)
552 ;; Make sure we have a file name when we need it.
553 (when (and (not (or to-buffer body-only))
554 (not buffer-file-name))
555 (if (buffer-base-buffer)
556 (org-set-local 'buffer-file-name
557 (with-current-buffer (buffer-base-buffer)
558 buffer-file-name))
559 (error "Need a file name to be able to export")))
561 (message "Exporting to LaTeX...")
562 (org-unmodified
563 (remove-text-properties (point-min) (point-max)
564 '(:org-license-to-kill nil)))
565 (org-update-radio-target-regexp)
566 (org-export-latex-set-initial-vars ext-plist arg)
567 (run-hooks 'org-export-latex-after-initial-vars-hook)
568 (let* ((wcf (current-window-configuration))
569 (opt-plist org-export-latex-options-plist)
570 (region-p (org-region-active-p))
571 (rbeg (and region-p (region-beginning)))
572 (rend (and region-p (region-end)))
573 (subtree-p
574 (if (plist-get opt-plist :ignore-subtree-p)
576 (when region-p
577 (save-excursion
578 (goto-char rbeg)
579 (and (org-at-heading-p)
580 (>= (org-end-of-subtree t t) rend))))))
581 (opt-plist (setq org-export-opt-plist
582 (if subtree-p
583 (org-export-add-subtree-options opt-plist rbeg)
584 opt-plist)))
585 ;; Make sure the variable contains the updated values.
586 (org-export-latex-options-plist opt-plist)
587 (title (or (and subtree-p (org-export-get-title-from-subtree))
588 (plist-get opt-plist :title)
589 (and (not
590 (plist-get opt-plist :skip-before-1st-heading))
591 (org-export-grab-title-from-buffer))
592 (file-name-sans-extension
593 (file-name-nondirectory buffer-file-name))))
594 (filename (concat (file-name-as-directory
595 (or pub-dir
596 (org-export-directory :LaTeX ext-plist)))
597 (file-name-sans-extension
598 (or (and subtree-p
599 (org-entry-get rbeg "EXPORT_FILE_NAME" t))
600 (file-name-nondirectory ;sans-extension
601 buffer-file-name)))
602 ".tex"))
603 (filename (if (equal (file-truename filename)
604 (file-truename buffer-file-name))
605 (concat filename ".tex")
606 filename))
607 (buffer (if to-buffer
608 (cond
609 ((eq to-buffer 'string) (get-buffer-create
610 "*Org LaTeX Export*"))
611 (t (get-buffer-create to-buffer)))
612 (find-file-noselect filename)))
613 (odd org-odd-levels-only)
614 (header (org-export-latex-make-header title opt-plist))
615 (skip (cond (subtree-p nil)
616 (region-p nil)
617 (t (plist-get opt-plist :skip-before-1st-heading))))
618 (text (plist-get opt-plist :text))
619 (org-export-preprocess-hook
620 (cons
621 `(lambda () (org-set-local 'org-complex-heading-regexp
622 ,org-export-latex-complex-heading-re))
623 org-export-preprocess-hook))
624 (first-lines (if skip "" (org-export-latex-first-lines
625 opt-plist
626 (if subtree-p
627 (save-excursion
628 (goto-char rbeg)
629 (point-at-bol 2))
630 rbeg)
631 (if region-p rend))))
632 (coding-system (and (boundp 'buffer-file-coding-system)
633 buffer-file-coding-system))
634 (coding-system-for-write (or org-export-latex-coding-system
635 coding-system))
636 (save-buffer-coding-system (or org-export-latex-coding-system
637 coding-system))
638 (region (buffer-substring
639 (if region-p (region-beginning) (point-min))
640 (if region-p (region-end) (point-max))))
641 (string-for-export
642 (org-export-preprocess-string
643 region
644 :emph-multiline t
645 :for-LaTeX t
646 :comments nil
647 :tags (plist-get opt-plist :tags)
648 :priority (plist-get opt-plist :priority)
649 :footnotes (plist-get opt-plist :footnotes)
650 :drawers (plist-get opt-plist :drawers)
651 :timestamps (plist-get opt-plist :timestamps)
652 :todo-keywords (plist-get opt-plist :todo-keywords)
653 :add-text (if (eq to-buffer 'string) nil text)
654 :skip-before-1st-heading skip
655 :select-tags (plist-get opt-plist :select-tags)
656 :exclude-tags (plist-get opt-plist :exclude-tags)
657 :LaTeX-fragments nil)))
659 (set-buffer buffer)
660 (erase-buffer)
661 (org-install-letbind)
663 (and (fboundp 'set-buffer-file-coding-system)
664 (set-buffer-file-coding-system coding-system-for-write))
666 ;; insert the header and initial document commands
667 (unless (or (eq to-buffer 'string) body-only)
668 (insert header))
670 ;; insert text found in #+TEXT
671 (when (and text (not (eq to-buffer 'string)))
672 (insert (org-export-latex-content
673 text '(lists tables fixed-width keywords))
674 "\n\n"))
676 ;; insert lines before the first headline
677 (unless skip
678 (insert first-lines))
680 ;; export the content of headlines
681 (org-export-latex-global
682 (with-temp-buffer
683 (insert string-for-export)
684 (goto-char (point-min))
685 (when (re-search-forward "^\\(\\*+\\) " nil t)
686 (let* ((asters (length (match-string 1)))
687 (level (if odd (- asters 2) (- asters 1))))
688 (setq org-export-latex-add-level
689 (if odd (1- (/ (1+ asters) 2)) (1- asters)))
690 (org-export-latex-parse-global level odd)))))
692 ;; finalization
693 (unless body-only (insert "\n\\end{document}"))
695 ;; Attach description terms to the \item macro
696 (goto-char (point-min))
697 (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t)
698 (delete-region (match-beginning 1) (match-end 1)))
700 ;; Relocate the table of contents
701 (goto-char (point-min))
702 (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
703 (goto-char (point-min))
704 (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t)
705 (replace-match ""))
706 (goto-char (point-min))
707 (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
708 (replace-match "\\tableofcontents" t t)))
710 ;; Cleanup forced line ends in itmes where they are not needed
711 (goto-char (point-min))
712 (while (re-search-forward
713 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)?\n\\\\begin"
714 nil t)
715 (delete-region (match-beginning 1) (match-end 1)))
717 (run-hooks 'org-export-latex-final-hook)
718 (or to-buffer (save-buffer))
719 (goto-char (point-min))
720 (or (org-export-push-to-kill-ring "LaTeX")
721 (message "Exporting to LaTeX...done"))
722 (prog1
723 (if (eq to-buffer 'string)
724 (prog1 (buffer-substring (point-min) (point-max))
725 (kill-buffer (current-buffer)))
726 (current-buffer))
727 (set-window-configuration wcf))))
729 ;;;###autoload
730 (defun org-export-as-pdf (arg &optional hidden ext-plist
731 to-buffer body-only pub-dir)
732 "Export as LaTeX, then process through to PDF."
733 (interactive "P")
734 (message "Exporting to PDF...")
735 (let* ((wconfig (current-window-configuration))
736 (lbuf (org-export-as-latex arg hidden ext-plist
737 to-buffer body-only pub-dir))
738 (file (buffer-file-name lbuf))
739 (base (file-name-sans-extension (buffer-file-name lbuf)))
740 (pdffile (concat base ".pdf"))
741 (cmds org-latex-to-pdf-process)
742 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
743 (bibtex-p (with-current-buffer lbuf
744 (save-excursion
745 (goto-char (point-min))
746 (re-search-forward "\\\\bibliography{" nil t))))
747 cmd)
748 (with-current-buffer outbuf (erase-buffer))
749 (message "Processing LaTeX file...")
750 (if (and cmds (symbolp cmds))
751 (funcall cmds file)
752 (while cmds
753 (setq cmd (pop cmds))
754 (while (string-match "%b" cmd)
755 (setq cmd (replace-match
756 (save-match-data
757 (shell-quote-argument base))
758 t t cmd)))
759 (while (string-match "%s" cmd)
760 (setq cmd (replace-match
761 (save-match-data
762 (shell-quote-argument file))
763 t t cmd)))
764 (shell-command cmd outbuf outbuf)))
765 (message "Processing LaTeX file...done")
766 (if (not (file-exists-p pdffile))
767 (error "PDF file was not produced")
768 (set-window-configuration wconfig)
769 (when org-export-pdf-remove-logfiles
770 (dolist (ext org-export-pdf-logfiles)
771 (setq file (concat base "." ext))
772 (and (file-exists-p file) (delete-file file))))
773 (message "Exporting to PDF...done")
774 pdffile)))
776 ;;;###autoload
777 (defun org-export-as-pdf-and-open (arg)
778 "Export as LaTeX, then process through to PDF, and open."
779 (interactive "P")
780 (let ((pdffile (org-export-as-pdf arg)))
781 (if pdffile
782 (org-open-file pdffile)
783 (error "PDF file was not produced"))))
785 ;;; Parsing functions:
787 (defun org-export-latex-parse-global (level odd)
788 "Parse the current buffer recursively, starting at LEVEL.
789 If ODD is non-nil, assume the buffer only contains odd sections.
790 Return a list reflecting the document structure."
791 (save-excursion
792 (goto-char (point-min))
793 (let* ((cnt 0) output
794 (depth org-export-latex-sectioning-depth))
795 (while (org-re-search-forward-unprotected
796 (concat "^\\(\\(?:\\*\\)\\{"
797 (number-to-string (+ (if odd 2 1) level))
798 "\\}\\) \\(.*\\)$")
799 ;; make sure that there is no upper heading
800 (when (> level 0)
801 (save-excursion
802 (save-match-data
803 (org-re-search-forward-unprotected
804 (concat "^\\(\\(?:\\*\\)\\{"
805 (number-to-string level)
806 "\\}\\) \\(.*\\)$") nil t)))) t)
807 (setq cnt (1+ cnt))
808 (let* ((pos (match-beginning 0))
809 (heading (match-string 2))
810 (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
811 (save-excursion
812 (narrow-to-region
813 (point)
814 (save-match-data
815 (if (org-re-search-forward-unprotected
816 (concat "^\\(\\(?:\\*\\)\\{"
817 (number-to-string (+ (if odd 2 1) level))
818 "\\}\\) \\(.*\\)$") nil t)
819 (match-beginning 0)
820 (point-max))))
821 (goto-char (point-min))
822 (setq output
823 (append output
824 (list
825 (list
826 `(pos . ,pos)
827 `(level . ,nlevel)
828 `(occur . ,cnt)
829 `(heading . ,heading)
830 `(content . ,(org-export-latex-parse-content))
831 `(subcontent . ,(org-export-latex-parse-subcontent
832 level odd)))))))
833 (widen)))
834 (list output))))
836 (defun org-export-latex-parse-content ()
837 "Extract the content of a section."
838 (let ((beg (point))
839 (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t)
840 (progn (beginning-of-line) (point))
841 (point-max))))
842 (buffer-substring beg end)))
844 (defun org-export-latex-parse-subcontent (level odd)
845 "Extract the subcontent of a section at LEVEL.
846 If ODD Is non-nil, assume subcontent only contains odd sections."
847 (if (not (org-re-search-forward-unprotected
848 (concat "^\\(\\(?:\\*\\)\\{"
849 (number-to-string (+ (if odd 4 2) level))
850 "\\}\\) \\(.*\\)$")
851 nil t))
852 nil ; subcontent is nil
853 (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
855 ;;; Rendering functions:
856 (defun org-export-latex-global (content)
857 "Export CONTENT to LaTeX.
858 CONTENT is an element of the list produced by
859 `org-export-latex-parse-global'."
860 (if (eq (car content) 'subcontent)
861 (mapc 'org-export-latex-sub (cdr content))
862 (org-export-latex-sub (car content))))
864 (defun org-export-latex-sub (subcontent)
865 "Export the list SUBCONTENT to LaTeX.
866 SUBCONTENT is an alist containing information about the headline
867 and its content."
868 (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
869 (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
871 (defun org-export-latex-subcontent (subcontent num)
872 "Export each cell of SUBCONTENT to LaTeX.
873 If NUM, export sections as numerical sections."
874 (let* ((heading (cdr (assoc 'heading subcontent)))
875 (level (- (cdr (assoc 'level subcontent))
876 org-export-latex-add-level))
877 (occur (number-to-string (cdr (assoc 'occur subcontent))))
878 (content (cdr (assoc 'content subcontent)))
879 (subcontent (cadr (assoc 'subcontent subcontent)))
880 (label (org-get-text-property-any 0 'target heading))
881 (label-list (cons label (cdr (assoc label
882 org-export-target-aliases))))
883 (sectioning org-export-latex-sectioning)
884 (depth org-export-latex-sectioning-depth)
885 main-heading sub-heading)
886 (when (symbolp (car sectioning))
887 (setq sectioning (funcall (car sectioning) level heading))
888 (when sectioning
889 (setq heading (car sectioning)
890 sectioning (cdr sectioning)))
891 (if sectioning (setq sectioning (make-list 10 sectioning)))
892 (setq depth (if sectioning 10000 0)))
893 (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading)
894 (setq main-heading (substring heading 0 (match-beginning 0))
895 sub-heading (substring heading (match-end 0))))
896 (setq heading (org-export-latex-fontify-headline heading)
897 sub-heading (and sub-heading
898 (org-export-latex-fontify-headline sub-heading))
899 main-heading (and main-heading
900 (org-export-latex-fontify-headline main-heading)))
901 (cond
902 ;; Normal conversion
903 ((<= level depth)
904 (let* ((sec (nth (1- level) sectioning))
905 start end)
906 (if (consp (cdr sec))
907 (setq start (nth (if num 0 2) sec)
908 end (nth (if num 1 3) sec))
909 (setq start (if num (car sec) (cdr sec))))
910 (insert (format start (if main-heading main-heading heading)
911 (or sub-heading "")))
912 (insert "\n")
913 (when label
914 (insert (mapconcat (lambda (l) (format "\\label{%s}" l))
915 label-list "\n") "\n"))
916 (insert (org-export-latex-content content))
917 (cond ((stringp subcontent) (insert subcontent))
918 ((listp subcontent)
919 (while (org-looking-back "\n\n") (backward-delete-char 1))
920 (org-export-latex-sub subcontent)))
921 (when (and end (string-match "[^ \t]" end))
922 (let ((hook (org-get-text-property-any 0 'org-insert-hook end)))
923 (and (functionp hook) (funcall hook)))
924 (insert end "\n"))))
925 ;; At a level under the hl option: we can drop this subsection
926 ((> level depth)
927 (cond ((eq org-export-latex-low-levels 'description)
928 (if (string-match "% ends low level$"
929 (buffer-substring (point-at-bol 0) (point)))
930 (delete-region (point-at-bol 0) (point))
931 (insert "\\begin{description}\n"))
932 (insert (format "\n\\item[%s]%s~\n"
933 heading
934 (if label (format "\\label{%s}" label) "")))
935 (insert (org-export-latex-content content))
936 (cond ((stringp subcontent) (insert subcontent))
937 ((listp subcontent) (org-export-latex-sub subcontent)))
938 (insert "\\end{description} % ends low level\n"))
939 ((memq org-export-latex-low-levels '(itemize enumerate))
940 (if (string-match "% ends low level$"
941 (buffer-substring (point-at-bol 0) (point)))
942 (delete-region (point-at-bol 0) (point))
943 (insert (format "\\begin{%s}\n"
944 (symbol-name org-export-latex-low-levels))))
945 (insert (format "\n\\item %s\\\\\n%s%%"
946 heading
947 (if label (format "\\label{%s}" label) "")))
948 (insert (org-export-latex-content content))
949 (cond ((stringp subcontent) (insert subcontent))
950 ((listp subcontent) (org-export-latex-sub subcontent)))
951 (insert (format "\\end{%s} %% ends low level\n"
952 (symbol-name org-export-latex-low-levels))))
954 ((listp org-export-latex-low-levels)
955 (if (string-match "% ends low level$"
956 (buffer-substring (point-at-bol 0) (point)))
957 (delete-region (point-at-bol 0) (point))
958 (insert (car org-export-latex-low-levels) "\n"))
959 (insert (format (nth 2 org-export-latex-low-levels)
960 heading
961 (if label (format "\\label{%s}" label) "")))
962 (insert (org-export-latex-content content))
963 (cond ((stringp subcontent) (insert subcontent))
964 ((listp subcontent) (org-export-latex-sub subcontent)))
965 (insert (nth 1 org-export-latex-low-levels)
966 " %% ends low level\n"))
968 ((stringp org-export-latex-low-levels)
969 (insert (format org-export-latex-low-levels heading) "\n")
970 (when label (insert (format "\\label{%s}\n" label)))
971 (insert (org-export-latex-content content))
972 (cond ((stringp subcontent) (insert subcontent))
973 ((listp subcontent) (org-export-latex-sub subcontent)))))))))
975 ;;; Exporting internals:
976 (defun org-export-latex-set-initial-vars (ext-plist level)
977 "Store org local variables required for LaTeX export.
978 EXT-PLIST is an optional additional plist.
979 LEVEL indicates the default depth for export."
980 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
981 org-export-latex-done-keywords org-done-keywords
982 org-export-latex-not-done-keywords org-not-done-keywords
983 org-export-latex-complex-heading-re org-complex-heading-regexp
984 org-export-latex-display-custom-times org-display-custom-times
985 org-export-latex-all-targets-re
986 (org-make-target-link-regexp (org-all-targets))
987 org-export-latex-options-plist
988 (org-combine-plists (org-default-export-plist) ext-plist
989 (org-infile-export-plist))
990 org-export-latex-class
991 (or (and (org-region-active-p)
992 (save-excursion
993 (goto-char (region-beginning))
994 (and (looking-at org-complex-heading-regexp)
995 (org-entry-get nil "LaTeX_CLASS" 'selective))))
996 (save-excursion
997 (save-restriction
998 (widen)
999 (goto-char (point-min))
1000 (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
1001 (match-string 1))))
1002 (plist-get org-export-latex-options-plist :latex-class)
1003 org-export-latex-default-class)
1004 org-export-latex-class-options
1005 (or (and (org-region-active-p)
1006 (save-excursion
1007 (goto-char (region-beginning))
1008 (and (looking-at org-complex-heading-regexp)
1009 (org-entry-get nil "LaTeX_CLASS_OPTIONS" 'selective))))
1010 (save-excursion
1011 (save-restriction
1012 (widen)
1013 (goto-char (point-min))
1014 (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t)
1015 (match-string 1))))
1016 (plist-get org-export-latex-options-plist :latex-class-options))
1017 org-export-latex-class
1018 (or (car (assoc org-export-latex-class org-export-latex-classes))
1019 (error "No definition for class `%s' in `org-export-latex-classes'"
1020 org-export-latex-class))
1021 org-export-latex-header
1022 (cadr (assoc org-export-latex-class org-export-latex-classes))
1023 org-export-latex-sectioning
1024 (cddr (assoc org-export-latex-class org-export-latex-classes))
1025 org-export-latex-sectioning-depth
1026 (or level
1027 (let ((hl-levels
1028 (plist-get org-export-latex-options-plist :headline-levels))
1029 (sec-depth (length org-export-latex-sectioning)))
1030 (if (> hl-levels sec-depth) sec-depth hl-levels))))
1031 (when (and org-export-latex-class-options
1032 (string-match "\\S-" org-export-latex-class-options)
1033 (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?"
1034 org-export-latex-header))
1035 (setq org-export-latex-header
1036 (concat (substring org-export-latex-header 0 (match-end 1))
1037 org-export-latex-class-options
1038 (substring org-export-latex-header (match-end 0))))))
1040 (defvar org-export-latex-format-toc-function
1041 'org-export-latex-format-toc-default
1042 "The function formatting returning the string to createthe table of contents.
1043 The function mus take one parameter, the depth of the table of contents.")
1045 (defun org-export-latex-make-header (title opt-plist)
1046 "Make the LaTeX header and return it as a string.
1047 TITLE is the current title from the buffer or region.
1048 OPT-PLIST is the options plist for current buffer."
1049 (let ((toc (plist-get opt-plist :table-of-contents))
1050 (author (plist-get opt-plist :author)))
1051 (concat
1052 (if (plist-get opt-plist :time-stamp-file)
1053 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1054 ;; insert LaTeX custom header
1055 (org-export-apply-macros-in-string org-export-latex-header)
1056 "\n"
1057 ;; insert information on LaTeX packages
1058 (when org-export-latex-packages-alist
1059 (concat
1060 (mapconcat (lambda(p)
1061 (if (equal "" (car p))
1062 (format "\\usepackage{%s}" (cadr p))
1063 (format "\\usepackage[%s]{%s}"
1064 (car p) (cadr p))))
1065 org-export-latex-packages-alist "\n")
1066 "\n"))
1067 ;; insert additional commands in the header
1068 (org-export-apply-macros-in-string
1069 (plist-get opt-plist :latex-header-extra))
1070 (org-export-apply-macros-in-string org-export-latex-append-header)
1071 ;; insert the title
1072 (format
1073 "\n\n\\title{%s}\n"
1074 ;; convert the title
1075 (org-export-latex-content
1076 title '(lists tables fixed-width keywords)))
1077 ;; insert author info
1078 (if (plist-get opt-plist :author-info)
1079 (format "\\author{%s}\n"
1080 (org-export-latex-fontify-headline (or author user-full-name)))
1081 (format "%%\\author{%s}\n"
1082 (or author user-full-name)))
1083 ;; insert the date
1084 (format "\\date{%s}\n"
1085 (format-time-string
1086 (or (plist-get opt-plist :date)
1087 org-export-latex-date-format)))
1088 ;; beginning of the document
1089 "\n\\begin{document}\n\n"
1090 ;; insert the title command
1091 (when (string-match "\\S-" title)
1092 (if (string-match "%s" org-export-latex-title-command)
1093 (format org-export-latex-title-command title)
1094 org-export-latex-title-command))
1095 "\n\n"
1096 ;; table of contents
1097 (when (and org-export-with-toc
1098 (plist-get opt-plist :section-numbers))
1099 (funcall org-export-latex-format-toc-function
1100 (cond ((numberp toc)
1101 (min toc (plist-get opt-plist :headline-levels)))
1102 (toc (plist-get opt-plist :headline-levels))))))))
1104 (defun org-export-latex-format-toc-default (depth)
1105 (when depth
1106 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1107 depth)))
1109 (defun org-export-latex-first-lines (opt-plist &optional beg end)
1110 "Export the first lines before first headline.
1111 If BEG is non-nil, it is the beginning of the region.
1112 If END is non-nil, it is the end of the region."
1113 (save-excursion
1114 (goto-char (or beg (point-min)))
1115 (let* ((pt (point))
1116 (end (if (re-search-forward "^\\*+ " end t)
1117 (goto-char (match-beginning 0))
1118 (goto-char (or end (point-max))))))
1119 (prog1
1120 (org-export-latex-content
1121 (org-export-preprocess-string
1122 (buffer-substring pt end)
1123 :for-LaTeX t
1124 :emph-multiline t
1125 :add-text nil
1126 :comments nil
1127 :skip-before-1st-heading nil
1128 :LaTeX-fragments nil
1129 :timestamps (plist-get opt-plist :timestamps)
1130 :footnotes (plist-get opt-plist :footnotes)))
1131 (org-unmodified
1132 (add-text-properties pt (max pt (1- end))
1133 '(:org-license-to-kill t)))))))
1135 (defvar org-export-latex-header-defs nil
1136 "The header definitions that might be used in the LaTeX body.")
1137 (defvar org-export-latex-header-defs-re nil
1138 "The header definitions that might be used in the LaTeX body.")
1140 (defun org-export-latex-content (content &optional exclude-list)
1141 "Convert CONTENT string to LaTeX.
1142 Don't perform conversions that are in EXCLUDE-LIST. Recognized
1143 conversion types are: quotation-marks, emphasis, sub-superscript,
1144 links, keywords, lists, tables, fixed-width"
1145 (with-temp-buffer
1146 (insert content)
1147 (unless (memq 'timestamps exclude-list)
1148 (org-export-latex-time-stamps))
1149 (unless (memq 'quotation-marks exclude-list)
1150 (org-export-latex-quotation-marks))
1151 (unless (memq 'emphasis exclude-list)
1152 (when (plist-get org-export-latex-options-plist :emphasize)
1153 (org-export-latex-fontify)))
1154 (unless (memq 'sub-superscript exclude-list)
1155 (org-export-latex-special-chars
1156 (plist-get org-export-latex-options-plist :sub-superscript)))
1157 (unless (memq 'links exclude-list)
1158 (org-export-latex-links))
1159 (unless (memq 'keywords exclude-list)
1160 (org-export-latex-keywords))
1161 (unless (memq 'lists exclude-list)
1162 (org-export-latex-lists))
1163 (unless (memq 'tables exclude-list)
1164 (org-export-latex-tables
1165 (plist-get org-export-latex-options-plist :tables)))
1166 (unless (memq 'fixed-width exclude-list)
1167 (org-export-latex-fixed-width
1168 (plist-get org-export-latex-options-plist :fixed-width)))
1169 ;; return string
1170 (buffer-substring (point-min) (point-max))))
1172 (defun org-export-latex-protect-string (s)
1173 "Add the org-protected property to string S."
1174 (add-text-properties 0 (length s) '(org-protected t) s) s)
1176 (defun org-export-latex-protect-char-in-string (char-list string)
1177 "Add org-protected text-property to char from CHAR-LIST in STRING."
1178 (with-temp-buffer
1179 (save-match-data
1180 (insert string)
1181 (goto-char (point-min))
1182 (while (re-search-forward (regexp-opt char-list) nil t)
1183 (add-text-properties (match-beginning 0)
1184 (match-end 0) '(org-protected t)))
1185 (buffer-string))))
1187 (defun org-export-latex-keywords-maybe (&optional remove-list)
1188 "Maybe remove keywords depending on rules in REMOVE-LIST."
1189 (goto-char (point-min))
1190 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
1191 (case-fold-search nil)
1192 (todo-markup org-export-latex-todo-keyword-markup)
1193 fmt)
1194 ;; convert TODO keywords
1195 (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
1196 (if (plist-get remove-list :todo)
1197 (replace-match "")
1198 (setq fmt (cond
1199 ((stringp todo-markup) todo-markup)
1200 ((and (consp todo-markup) (stringp (car todo-markup)))
1201 (if (member (match-string 1) org-export-latex-done-keywords)
1202 (cdr todo-markup) (car todo-markup)))
1203 (t (cdr (or (assoc (match-string 1) todo-markup)
1204 (car todo-markup))))))
1205 (replace-match (format fmt (match-string 1)) t t)))
1206 ;; convert priority string
1207 (when (re-search-forward "\\[\\\\#.\\]" nil t)
1208 (if (plist-get remove-list :priority)
1209 (replace-match "")
1210 (replace-match (format "\\textbf{%s}" (match-string 0)) t t)))
1211 ;; convert tags
1212 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t)
1213 (if (or (not org-export-with-tags)
1214 (plist-get remove-list :tags))
1215 (replace-match "")
1216 (replace-match
1217 (org-export-latex-protect-string
1218 (format "\\textbf{%s}"
1219 (save-match-data
1220 (replace-regexp-in-string
1221 "_" "\\\\_" (match-string 0)))))
1222 t t)))))
1224 (defun org-export-latex-fontify-headline (string)
1225 "Fontify special words in STRING."
1226 (with-temp-buffer
1227 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
1228 ;; the beginning of the buffer - inserting "\n" is safe here though.
1229 (insert "\n" string)
1230 (goto-char (point-min))
1231 (let ((re (concat "\\\\[a-zA-Z]+\\(?:"
1232 "\\[.*\\]"
1233 "\\)?"
1234 (org-create-multibrace-regexp "{" "}" 3))))
1235 (while (re-search-forward re nil t)
1236 (unless (save-excursion (goto-char (match-beginning 0))
1237 (equal (char-after (point-at-bol)) ?#))
1238 (add-text-properties (match-beginning 0) (match-end 0)
1239 '(org-protected t)))))
1240 (when (plist-get org-export-latex-options-plist :emphasize)
1241 (org-export-latex-fontify))
1242 (org-export-latex-keywords-maybe)
1243 (org-export-latex-special-chars
1244 (plist-get org-export-latex-options-plist :sub-superscript))
1245 (org-export-latex-links)
1246 (org-trim (buffer-string))))
1248 (defun org-export-latex-time-stamps ()
1249 "Format time stamps."
1250 (goto-char (point-min))
1251 (let ((org-display-custom-times org-export-latex-display-custom-times))
1252 (while (re-search-forward org-ts-regexp-both nil t)
1253 (org-if-unprotected-at (1- (point))
1254 (replace-match
1255 (org-export-latex-protect-string
1256 (format org-export-latex-timestamp-markup
1257 (substring (org-translate-time (match-string 0)) 1 -1)))
1258 t t)))))
1260 (defun org-export-latex-quotation-marks ()
1261 "Export quotation marks depending on language conventions."
1262 (let* ((lang (plist-get org-export-latex-options-plist :language))
1263 (quote-rpl (if (equal lang "fr")
1264 '(("\\(\\s-\\)\"" "«~")
1265 ("\\(\\S-\\)\"" "~»")
1266 ("\\(\\s-\\)'" "`"))
1267 '(("\\(\\s-\\|[[(]\\)\"" "``")
1268 ("\\(\\S-\\)\"" "''")
1269 ("\\(\\s-\\|(\\)'" "`")))))
1270 (mapc (lambda(l) (goto-char (point-min))
1271 (while (re-search-forward (car l) nil t)
1272 (let ((rpl (concat (match-string 1)
1273 (org-export-latex-protect-string
1274 (copy-sequence (cadr l))))))
1275 (org-if-unprotected-1
1276 (replace-match rpl t t))))) quote-rpl)))
1278 (defun org-export-latex-special-chars (sub-superscript)
1279 "Export special characters to LaTeX.
1280 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
1281 See the `org-export-latex.el' code for a complete conversion table."
1282 (goto-char (point-min))
1283 (mapc (lambda(c)
1284 (goto-char (point-min))
1285 (while (re-search-forward c nil t)
1286 ;; Put the point where to check for org-protected
1287 (unless (get-text-property (match-beginning 2) 'org-protected)
1288 (cond ((member (match-string 2) '("\\$" "$"))
1289 (if (equal (match-string 2) "\\$")
1291 (replace-match "\\$" t t)))
1292 ((member (match-string 2) '("&" "%" "#"))
1293 (if (equal (match-string 1) "\\")
1294 (replace-match (match-string 2) t t)
1295 (replace-match (concat (match-string 1) "\\"
1296 (match-string 2)) t t)))
1297 ((equal (match-string 2) "...")
1298 (replace-match
1299 (concat (match-string 1)
1300 (org-export-latex-protect-string "\\ldots{}")) t t))
1301 ((equal (match-string 2) "~")
1302 (cond ((equal (match-string 1) "\\") nil)
1303 ((eq 'org-link (get-text-property 0 'face (match-string 2)))
1304 (replace-match (concat (match-string 1) "\\~") t t))
1305 (t (replace-match
1306 (org-export-latex-protect-string
1307 (concat (match-string 1) "\\~{}")) t t))))
1308 ((member (match-string 2) '("{" "}"))
1309 (unless (save-match-data (org-inside-latex-math-p))
1310 (if (equal (match-string 1) "\\")
1311 (replace-match (match-string 2) t t)
1312 (replace-match (concat (match-string 1) "\\"
1313 (match-string 2)) t t)))))
1314 (unless (save-match-data (org-inside-latex-math-p))
1315 (cond ((equal (match-string 2) "\\")
1316 (replace-match (or (save-match-data
1317 (org-export-latex-treat-backslash-char
1318 (match-string 1)
1319 (or (match-string 3) "")))
1320 "") t t))
1321 ((member (match-string 2) '("_" "^"))
1322 (replace-match (or (save-match-data
1323 (org-export-latex-treat-sub-super-char
1324 sub-superscript
1325 (match-string 2)
1326 (match-string 1)
1327 (match-string 3))) "") t t)
1328 (backward-char 1)))))))
1329 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
1330 "\\(\\(\\\\?\\$\\)\\)"
1331 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
1332 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
1333 "\\(.\\|^\\)\\(&\\)"
1334 "\\(.\\|^\\)\\(#\\)"
1335 "\\(.\\|^\\)\\(%\\)"
1336 "\\(.\\|^\\)\\({\\)"
1337 "\\(.\\|^\\)\\(}\\)"
1338 "\\(.\\|^\\)\\(~\\)"
1339 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
1340 ;; (?\< . "\\textless{}")
1341 ;; (?\> . "\\textgreater{}")
1344 (defun org-inside-latex-math-p ()
1345 (get-text-property (point) 'org-latex-math))
1347 (defun org-export-latex-treat-sub-super-char
1348 (subsup char string-before string-after)
1349 "Convert the \"_\" and \"^\" characters to LaTeX.
1350 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
1351 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
1352 (cond ((equal string-before "\\")
1353 (concat string-before char string-after))
1354 ((and (string-match "\\S-+" string-after))
1355 ;; this is part of a math formula
1356 (cond ((eq 'org-link (get-text-property 0 'face char))
1357 (concat string-before "\\" char string-after))
1358 ((save-match-data (org-inside-latex-math-p))
1359 (if subsup
1360 (cond ((eq 1 (length string-after))
1361 (concat string-before char string-after))
1362 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
1363 (format "%s%s{%s}" string-before char
1364 (match-string 1 string-after))))))
1365 ((and (> (length string-after) 1)
1366 (or (eq subsup t)
1367 (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
1368 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
1369 (org-export-latex-protect-string
1370 (format "%s$%s{%s}$" string-before char
1371 (if (and (> (match-end 1) (1+ (match-beginning 1)))
1372 (not (equal (substring string-after 0 2) "{\\")))
1373 (concat "\\mathrm{" (match-string 1 string-after) "}")
1374 (match-string 1 string-after)))))
1375 ((eq subsup t) (concat string-before "$" char string-after "$"))
1376 (t (org-export-latex-protect-string
1377 (concat string-before "\\" char "{}" string-after)))))
1378 (t (org-export-latex-protect-string
1379 (concat string-before "\\" char "{}" string-after)))))
1381 (defun org-export-latex-treat-backslash-char (string-before string-after)
1382 "Convert the \"$\" special character to LaTeX.
1383 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
1384 (cond ((member (list string-after) org-html-entities)
1385 ;; backslash is part of a special entity (like "\alpha")
1386 (concat string-before "$\\"
1387 (or (cdar (member (list string-after) org-html-entities))
1388 string-after) "$"))
1389 ((and (not (string-match "^[ \n\t]" string-after))
1390 (not (string-match "[ \t]\\'\\|^" string-before)))
1391 ;; backslash is inside a word
1392 (org-export-latex-protect-string
1393 (concat string-before "\\textbackslash{}" string-after)))
1394 ((not (or (equal string-after "")
1395 (string-match "^[ \t\n]" string-after)))
1396 ;; backslash might escape a character (like \#) or a user TeX
1397 ;; macro (like \setcounter)
1398 (org-export-latex-protect-string
1399 (concat string-before "\\" string-after)))
1400 ((and (string-match "^[ \t\n]" string-after)
1401 (string-match "[ \t\n]\\'" string-before))
1402 ;; backslash is alone, convert it to $\backslash$
1403 (org-export-latex-protect-string
1404 (concat string-before "\\textbackslash{}" string-after)))
1405 (t (org-export-latex-protect-string
1406 (concat string-before "\\textbackslash{}" string-after)))))
1408 (defun org-export-latex-keywords ()
1409 "Convert special keywords to LaTeX."
1410 (goto-char (point-min))
1411 (while (re-search-forward org-export-latex-special-keyword-regexp nil t)
1412 (replace-match (format org-export-latex-timestamp-keyword-markup
1413 (match-string 0)) t t)
1414 (save-excursion
1415 (beginning-of-line 1)
1416 (unless (looking-at ".*\\\\newline[ \t]*$")
1417 (end-of-line 1)
1418 (insert "\\newline")))))
1420 (defun org-export-latex-fixed-width (opt)
1421 "When OPT is non-nil convert fixed-width sections to LaTeX."
1422 (goto-char (point-min))
1423 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t)
1424 (if opt
1425 (progn (goto-char (match-beginning 0))
1426 (insert "\\begin{verbatim}\n")
1427 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1428 (replace-match (concat (match-string 1)
1429 (match-string 2)) t t)
1430 (forward-line))
1431 (insert "\\end{verbatim}\n\n"))
1432 (progn (goto-char (match-beginning 0))
1433 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1434 (replace-match (concat "%" (match-string 1)
1435 (match-string 2)) t t)
1436 (forward-line))))))
1439 (defvar org-table-last-alignment) ; defined in org-table.el
1440 (defvar org-table-last-column-widths) ; defined in org-table.el
1441 (declare-function orgtbl-to-latex "org-table" (table params) t)
1442 (defun org-export-latex-tables (insert)
1443 "Convert tables to LaTeX and INSERT it."
1444 (goto-char (point-min))
1445 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
1446 (org-if-unprotected-at (1- (point))
1447 (org-table-align)
1448 (let* ((beg (org-table-begin))
1449 (end (org-table-end))
1450 (raw-table (buffer-substring beg end))
1451 (org-table-last-alignment (copy-sequence org-table-last-alignment))
1452 (org-table-last-column-widths (copy-sequence
1453 org-table-last-column-widths))
1454 fnum fields line lines olines gr colgropen line-fmt align
1455 caption label attr floatp longtblp)
1456 (if org-export-latex-tables-verbatim
1457 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1458 "\\end{verbatim}\n")))
1459 (apply 'delete-region (list beg end))
1460 (insert (org-export-latex-protect-string tbl)))
1461 (progn
1462 (setq caption (org-find-text-property-in-string
1463 'org-caption raw-table)
1464 attr (org-find-text-property-in-string
1465 'org-attributes raw-table)
1466 label (org-find-text-property-in-string
1467 'org-label raw-table)
1468 longtblp (and attr (stringp attr)
1469 (string-match "\\<longtable\\>" attr))
1470 align (and attr (stringp attr)
1471 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
1472 (match-string 1 attr))
1473 floatp (or caption label))
1474 (setq lines (org-split-string raw-table "\n"))
1475 (apply 'delete-region (list beg end))
1476 (when org-export-table-remove-special-lines
1477 (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
1478 (when org-table-clean-did-remove-column
1479 (pop org-table-last-alignment)
1480 (pop org-table-last-column-widths))
1481 ;; make a formatting string to reflect alignment
1482 (setq olines lines)
1483 (while (and (not line-fmt) (setq line (pop olines)))
1484 (unless (string-match "^[ \t]*|-" line)
1485 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1486 (setq fnum (make-vector (length fields) 0))
1487 (setq line-fmt
1488 (mapconcat
1489 (lambda (x)
1490 (setq gr (pop org-table-colgroup-info))
1491 (format "%s%%s%s"
1492 (cond ((eq gr :start)
1493 (prog1 (if colgropen "|" "|")
1494 (setq colgropen t)))
1495 ((eq gr :startend)
1496 (prog1 (if colgropen "|" "|")
1497 (setq colgropen nil)))
1498 (t ""))
1499 (if (memq gr '(:end :startend))
1500 (progn (setq colgropen nil) "|")
1501 "")))
1502 fnum ""))))
1503 ;; fix double || in line-fmt
1504 (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
1505 ;; maybe remove the first and last "|"
1506 (when (and (not org-export-latex-tables-column-borders)
1507 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
1508 (setq line-fmt (match-string 2 line-fmt)))
1509 ;; format alignment
1510 (unless align
1511 (setq align (apply 'format
1512 (cons line-fmt
1513 (mapcar (lambda (x) (if x "r" "l"))
1514 org-table-last-alignment)))))
1515 ;; prepare the table to send to orgtbl-to-latex
1516 (setq lines
1517 (mapcar
1518 (lambda(elem)
1519 (or (and (string-match "[ \t]*|-+" elem) 'hline)
1520 (org-split-string (org-trim elem) "|")))
1521 lines))
1522 (when insert
1523 (insert (org-export-latex-protect-string
1524 (concat
1525 (if longtblp
1526 (concat "\\begin{longtable}{" align "}\n")
1527 (if floatp "\\begin{table}[htb]\n"))
1528 (if (or floatp longtblp)
1529 (format
1530 "\\caption{%s%s}"
1531 (if label (concat "\\\label{" label "}") "")
1532 (or caption "")))
1533 (if longtblp "\\\\\n" "\n")
1534 (if (and org-export-latex-tables-centered (not longtblp))
1535 "\\begin{center}\n")
1536 (if (not longtblp) (concat "\\begin{tabular}{" align "}\n"))
1537 (orgtbl-to-latex
1538 lines
1539 `(:tstart nil :tend nil
1540 :hlend ,(if longtblp
1541 (format "\\\\
1542 \\hline
1543 \\endhead
1544 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
1545 \\endfoot
1546 \\endlastfoot" (length org-table-last-alignment))
1547 nil)))
1548 (if (not longtblp) (concat "\n\\end{tabular}"))
1549 (if longtblp "\n" (if org-export-latex-tables-centered
1550 "\n\\end{center}\n" "\n"))
1551 (if longtblp
1552 "\\end{longtable}"
1553 (if floatp "\\end{table}"))))
1554 "\n\n"))))))))
1556 (defun org-export-latex-fontify ()
1557 "Convert fontification to LaTeX."
1558 (goto-char (point-min))
1559 (while (re-search-forward org-emph-re nil t)
1560 ;; The match goes one char after the *string*, except at the end of a line
1561 (let ((emph (assoc (match-string 3)
1562 org-export-latex-emphasis-alist))
1563 (beg (match-beginning 0))
1564 (end (match-end 0))
1565 rpl s)
1566 (unless emph
1567 (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
1568 (match-string 3)))
1569 (unless (or (and (get-text-property (- (point) 2) 'org-protected)
1570 (not (get-text-property
1571 (- (point) 2) 'org-verbatim-emph)))
1572 (save-excursion
1573 (goto-char (match-beginning 1))
1574 (save-match-data
1575 (and (org-at-table-p)
1576 (string-match
1577 "[|\n]" (buffer-substring beg end))))))
1578 (setq s (match-string 4))
1579 (setq rpl (concat (match-string 1)
1580 (org-export-latex-emph-format (cadr emph)
1581 (match-string 4))
1582 (match-string 5)))
1583 (if (caddr emph)
1584 (setq rpl (org-export-latex-protect-string rpl))
1585 (save-match-data
1586 (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl)
1587 (progn
1588 (add-text-properties (match-beginning 1) (match-end 1)
1589 '(org-protected t) rpl)
1590 (add-text-properties (match-beginning 3) (match-end 3)
1591 '(org-protected t) rpl)))))
1592 (replace-match rpl t t)))
1593 (backward-char)))
1595 (defvar org-export-latex-use-verb nil)
1596 (defun org-export-latex-emph-format (format string)
1597 "Format an emphasis string and handle the \\verb special case."
1598 (when (equal format "\\verb")
1599 (save-match-data
1600 (if org-export-latex-use-verb
1601 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1602 (catch 'exit
1603 (loop for i from 0 to (1- (length ll)) do
1604 (if (not (string-match (regexp-quote (substring ll i (1+ i)))
1605 string))
1606 (progn
1607 (setq format (concat "\\verb" (substring ll i (1+ i))
1608 "%s" (substring ll i (1+ i))))
1609 (throw 'exit nil))))))
1610 (let ((start 0)
1611 (trans '(("\\" . "\\textbackslash{}")
1612 ("~" . "\\textasciitilde{}")
1613 ("^" . "\\textasciicircum{}")))
1614 (rtn "") char)
1615 (while (string-match "[\\{}$%&_#~^]" string)
1616 (setq char (match-string 0 string))
1617 (if (> (match-beginning 0) 0)
1618 (setq rtn (concat rtn (substring string
1619 0 (match-beginning 0)))))
1620 (setq string (substring string (1+ (match-beginning 0))))
1621 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1622 rtn (concat rtn char)))
1623 (setq string (concat rtn string) format "\\texttt{%s}")))))
1624 (format format string))
1626 (defun org-export-latex-links ()
1627 ;; Make sure to use the LaTeX hyperref and graphicx package
1628 ;; or send some warnings.
1629 "Convert links to LaTeX."
1630 (goto-char (point-min))
1631 (while (re-search-forward org-bracket-link-analytic-regexp++ nil t)
1632 (org-if-unprotected-1
1633 (goto-char (match-beginning 0))
1634 (let* ((re-radio org-export-latex-all-targets-re)
1635 (remove (list (match-beginning 0) (match-end 0)))
1636 (raw-path (org-extract-attributes (match-string 3)))
1637 (full-raw-path (concat (match-string 1) raw-path))
1638 (desc (match-string 5))
1639 (type (or (match-string 2)
1640 (if (or (file-name-absolute-p raw-path)
1641 (string-match "^\\.\\.?/" raw-path))
1642 "file")))
1643 (coderefp (equal type "coderef"))
1644 (caption (org-find-text-property-in-string 'org-caption raw-path))
1645 (attr (or (org-find-text-property-in-string 'org-attributes raw-path)
1646 (plist-get org-export-latex-options-plist :latex-image-options)))
1647 (label (org-find-text-property-in-string 'org-label raw-path))
1648 imgp radiop
1649 ;; define the path of the link
1650 (path (cond
1651 ((member type '("coderef"))
1652 raw-path)
1653 ((member type '("http" "https" "ftp"))
1654 (concat type ":" raw-path))
1655 ((and re-radio (string-match re-radio raw-path))
1656 (setq radiop t))
1657 ((equal type "mailto")
1658 (concat type ":" raw-path))
1659 ((equal type "file")
1660 (if (and (org-file-image-p
1661 (expand-file-name
1662 raw-path)
1663 org-export-latex-inline-image-extensions)
1664 (or (get-text-property 0 'org-no-description
1665 raw-path)
1666 (equal desc full-raw-path)))
1667 (setq imgp t)
1668 (progn (when (string-match "\\(.+\\)::.+" raw-path)
1669 (setq raw-path (match-string 1 raw-path)))
1670 (if (file-exists-p raw-path)
1671 (concat type "://" (expand-file-name raw-path))
1672 (concat type "://" (org-export-directory
1673 :LaTeX org-export-latex-options-plist)
1674 raw-path))))))))
1675 ;; process with link inserting
1676 (apply 'delete-region remove)
1677 (cond ((and imgp
1678 (plist-get org-export-latex-options-plist :inline-images))
1679 ;; OK, we need to inline an image
1680 (insert
1681 (org-export-latex-format-image raw-path caption label attr)))
1682 (coderefp
1683 (insert (format
1684 (org-export-get-coderef-format path desc)
1685 (cdr (assoc path org-export-code-refs)))))
1686 (radiop (insert (format "\\hyperref[%s]{%s}"
1687 (org-solidify-link-text raw-path) desc)))
1688 ((not type)
1689 (insert (format "\\hyperref[%s]{%s}"
1690 (org-remove-initial-hash
1691 (org-solidify-link-text raw-path))
1692 desc)))
1693 (path
1694 (when (org-at-table-p)
1695 ;; There is a strange problem when we have a link in a table,
1696 ;; ampersands then cause a problem. I think this must be
1697 ;; a LaTeX issue, but we here implement a work-around anyway.
1698 (setq path (org-export-latex-protect-amp path)
1699 desc (org-export-latex-protect-amp desc)))
1700 (insert (format "\\href{%s}{%s}" path desc)))
1701 (t (insert "\\texttt{" desc "}")))))))
1704 (defun org-export-latex-format-image (path caption label attr)
1705 "Format the image element, depending on user settings."
1706 (let (ind floatp wrapp placement figenv)
1707 (setq floatp (or caption label))
1708 (setq ind (org-get-text-property-any 0 'original-indentation path))
1709 (when (and attr (stringp attr))
1710 (if (string-match "[ \t]*\\<wrap\\>" attr)
1711 (setq wrapp t floatp nil attr (replace-match "" t t attr)))
1712 (if (string-match "[ \t]*\\<float\\>" attr)
1713 (setq wrapp nil floatp t attr (replace-match "" t t attr))))
1715 (setq placement
1716 (cond
1717 (wrapp "{l}{0.5\\textwidth}")
1718 (floatp "[htb]")
1719 (t "")))
1721 (when (and attr (stringp attr)
1722 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr))
1723 (setq placement (match-string 1 attr)
1724 attr (replace-match "" t t attr)))
1725 (setq attr (and attr (org-trim attr)))
1726 (when (or (not attr) (= (length attr) 0))
1727 (setq attr (cond (floatp "width=0.7\\textwidth")
1728 (wrapp "width=0.48\\textwidth")
1729 (t attr))))
1730 (setq figenv
1731 (cond
1732 (wrapp "\\begin{wrapfigure}%placement
1733 \\centering
1734 \\includegraphics[%attr]{%path}
1735 \\caption{%labelcmd%caption}
1736 \\end{wrapfigure}")
1737 (floatp "\\begin{figure}%placement
1738 \\centering
1739 \\includegraphics[%attr]{%path}
1740 \\caption{%labelcmd%caption}
1741 \\end{figure}")
1742 (t "\\includegraphics[%attr]{%path}")))
1745 (setq figenv (mapconcat 'identity (split-string figenv "\n")
1746 (save-excursion (beginning-of-line 1)
1747 (looking-at "[ \t]*")
1748 (concat "\n" (match-string 0)))))
1750 (if (and (not label) (not caption)
1751 (string-match "^\\\\caption{.*\n" figenv))
1752 (setq figenv (replace-match "" t t figenv)))
1753 (org-add-props
1754 (org-fill-template
1755 figenv
1756 (list (cons "path"
1757 (if (file-name-absolute-p path)
1758 (expand-file-name path)
1759 path))
1760 (cons "attr" attr)
1761 (cons "labelcmd" (if label (format "\\label{%s}"
1762 label)""))
1763 (cons "caption" (or caption ""))
1764 (cons "placement" (or placement ""))))
1765 nil 'original-indentation ind)))
1767 (defun org-export-latex-protect-amp (s)
1768 (while (string-match "\\([^\\\\]\\)\\(&\\)" s)
1769 (setq s (replace-match (concat (match-string 1 s) "\\" (match-string 2 s))
1770 t t s)))
1773 (defun org-remove-initial-hash (s)
1774 (if (string-match "\\`#" s)
1775 (substring s 1)
1777 (defvar org-latex-entities) ; defined below
1778 (defvar org-latex-entities-regexp) ; defined below
1779 (defvar org-latex-entities-exceptions) ; defined below
1781 (defun org-export-latex-preprocess (parameters)
1782 "Clean stuff in the LaTeX export."
1783 ;; Preserve line breaks
1784 (goto-char (point-min))
1785 (while (re-search-forward "\\\\\\\\" nil t)
1786 (add-text-properties (match-beginning 0) (match-end 0)
1787 '(org-protected t)))
1789 ;; Preserve latex environments
1790 (goto-char (point-min))
1791 (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t)
1792 (let* ((start (progn (beginning-of-line) (point)))
1793 (end (and (re-search-forward
1794 (concat "^[ \t]*\\\\end{"
1795 (regexp-quote (match-string 1))
1796 "}") nil t)
1797 (point-at-eol))))
1798 (if end
1799 (add-text-properties start end '(org-protected t))
1800 (goto-char (point-at-eol)))))
1802 ;; Preserve math snippets
1804 (let* ((matchers (plist-get org-format-latex-options :matchers))
1805 (re-list org-latex-regexps)
1806 beg end re e m n block off)
1807 ;; Check the different regular expressions
1808 (while (setq e (pop re-list))
1809 (setq m (car e) re (nth 1 e) n (nth 2 e)
1810 block (if (nth 3 e) "\n\n" ""))
1811 (setq off (if (member m '("$" "$1")) 1 0))
1812 (when (and (member m matchers) (not (equal m "begin")))
1813 (goto-char (point-min))
1814 (while (re-search-forward re nil t)
1815 (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
1816 (add-text-properties beg end '(org-protected t org-latex-math t))))))
1818 ;; Convert LaTeX to \LaTeX{}
1819 (goto-char (point-min))
1820 (let ((case-fold-search nil))
1821 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
1822 (org-if-unprotected
1823 (replace-match (org-export-latex-protect-string
1824 (concat (match-string 1) "\\LaTeX{}")) t t))))
1826 ;; Convert blockquotes
1827 (goto-char (point-min))
1828 (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
1829 (org-replace-match-keep-properties "\\begin{quote}" t t))
1830 (goto-char (point-min))
1831 (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
1832 (org-replace-match-keep-properties "\\end{quote}" t t))
1834 ;; Convert verse
1835 (goto-char (point-min))
1836 (while (search-forward "ORG-VERSE-START" nil t)
1837 (org-replace-match-keep-properties "\\begin{verse}" t t)
1838 (beginning-of-line 2)
1839 (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
1840 (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
1841 (goto-char (match-end 1))
1842 (org-replace-match-keep-properties
1843 (org-export-latex-protect-string
1844 (concat "\\hspace*{1cm}" (match-string 2))) t t)
1845 (beginning-of-line 1))
1846 (if (looking-at "[ \t]*$")
1847 (insert (org-export-latex-protect-string "\\vspace*{1em}"))
1848 (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
1849 (end-of-line 1)
1850 (insert "\\\\")))
1851 (beginning-of-line 2))
1852 (and (looking-at "[ \t]*ORG-VERSE-END.*")
1853 (org-replace-match-keep-properties "\\end{verse}" t t)))
1855 ;; Convert center
1856 (goto-char (point-min))
1857 (while (search-forward "ORG-CENTER-START" nil t)
1858 (org-replace-match-keep-properties "\\begin{center}" t t))
1859 (goto-char (point-min))
1860 (while (search-forward "ORG-CENTER-END" nil t)
1861 (org-replace-match-keep-properties "\\end{center}" t t))
1863 (run-hooks 'org-export-latex-after-blockquotes-hook)
1865 ;; Convert horizontal rules
1866 (goto-char (point-min))
1867 (while (re-search-forward "^----+.$" nil t)
1868 (org-if-unprotected
1869 (replace-match (org-export-latex-protect-string "\\hrule") t t)))
1871 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
1872 (let ((re (concat "\\\\[a-zA-Z]+\\(?:"
1873 "\\[.*\\]"
1874 "\\)?"
1875 "\\(" (org-create-multibrace-regexp "{" "}" 3)
1876 "\\)\\{1,3\\}")))
1877 (while (re-search-forward re nil t)
1878 (unless (save-excursion (goto-char (match-beginning 0))
1879 (equal (char-after (point-at-bol)) ?#))
1880 (add-text-properties (match-beginning 0) (match-end 0)
1881 '(org-protected t)))))
1883 ;; Protect LaTeX entities
1884 (goto-char (point-min))
1885 (let (a)
1886 (while (re-search-forward org-latex-entities-regexp nil t)
1887 (if (setq a (assoc (match-string 0) org-latex-entities-exceptions))
1888 (replace-match (org-add-props (nth 1 a) nil 'org-protected t)
1889 t t)
1890 (add-text-properties (match-beginning 0) (match-end 0)
1891 '(org-protected t)))))
1893 ;; Replace radio links
1894 (goto-char (point-min))
1895 (while (re-search-forward
1896 (concat "<<<?" org-export-latex-all-targets-re
1897 ">>>?\\((INVISIBLE)\\)?") nil t)
1898 (org-if-unprotected-at (+ (match-beginning 0) 2)
1899 (replace-match
1900 (org-export-latex-protect-string
1901 (format "\\label{%s}%s" (save-match-data (org-solidify-link-text
1902 (match-string 1)))
1903 (if (match-string 2) "" (match-string 1)))) t t)))
1905 ;; Delete @<...> constructs
1906 ;; Thanks to Daniel Clemente for this regexp
1907 (goto-char (point-min))
1908 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
1909 (org-if-unprotected
1910 (replace-match "")))
1912 ;; When converting to LaTeX, replace footnotes
1913 ;; FIXME: don't protect footnotes from conversion
1914 (when (plist-get org-export-latex-options-plist :footnotes)
1915 (goto-char (point-min))
1916 (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t)
1917 (org-if-unprotected
1918 (when (and (save-match-data
1919 (save-excursion (beginning-of-line)
1920 (looking-at "[^:|#]")))
1921 (not (org-in-verbatim-emphasis)))
1922 (let ((foot-beg (match-beginning 0))
1923 (foot-end (match-end 0))
1924 (foot-prefix (match-string 0))
1925 footnote footnote-rpl)
1926 (save-excursion
1927 (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix))
1928 nil t))
1929 (replace-match (org-export-latex-protect-string
1930 (concat "$^{" (match-string 1) "}$")))
1931 (replace-match "")
1932 (let ((end (save-excursion
1933 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
1934 (match-beginning 0) (point-max)))))
1935 (setq footnote (concat (org-trim (buffer-substring (point) end))
1936 " ")) ; prevent last } being part of a link
1937 (delete-region (point) end))
1938 (goto-char foot-beg)
1939 (delete-region foot-beg foot-end)
1940 (unless (null footnote)
1941 (setq footnote-rpl (format "\\footnote{%s}" footnote))
1942 (add-text-properties 0 10 '(org-protected t) footnote-rpl)
1943 (add-text-properties (1- (length footnote-rpl))
1944 (length footnote-rpl)
1945 '(org-protected t) footnote-rpl)
1946 (insert footnote-rpl)))
1947 )))))
1949 ;; Remove footnote section tag for LaTeX
1950 (goto-char (point-min))
1951 (while (re-search-forward
1952 (concat "^" footnote-section-tag-regexp) nil t)
1953 (org-if-unprotected
1954 (replace-match "")))))
1956 ;;; List handling:
1958 (defun org-export-latex-lists ()
1959 "Convert plain text lists in current buffer into LaTeX lists."
1960 (goto-char (point-min))
1961 (while (re-search-forward org-list-beginning-re nil t)
1962 (org-if-unprotected
1963 (beginning-of-line)
1964 (insert (org-list-to-latex (org-list-parse-list t)
1965 org-export-latex-list-parameters))
1966 "\n")))
1968 (defconst org-latex-entities
1969 '("\\!"
1970 "\\'"
1971 "\\+"
1972 "\\,"
1973 "\\-"
1974 "\\:"
1975 "\\;"
1976 "\\<"
1977 "\\="
1978 "\\>"
1979 "\\Huge"
1980 "\\LARGE"
1981 "\\Large"
1982 "\\Styles"
1983 "\\\\"
1984 "\\`"
1985 "\\addcontentsline"
1986 "\\address"
1987 "\\addtocontents"
1988 "\\addtocounter"
1989 "\\addtolength"
1990 "\\addvspace"
1991 "\\alph"
1992 "\\appendix"
1993 "\\arabic"
1994 "\\author"
1995 "\\begin{array}"
1996 "\\begin{center}"
1997 "\\begin{description}"
1998 "\\begin{enumerate}"
1999 "\\begin{eqnarray}"
2000 "\\begin{equation}"
2001 "\\begin{figure}"
2002 "\\begin{flushleft}"
2003 "\\begin{flushright}"
2004 "\\begin{itemize}"
2005 "\\begin{list}"
2006 "\\begin{minipage}"
2007 "\\begin{picture}"
2008 "\\begin{quotation}"
2009 "\\begin{quote}"
2010 "\\begin{tabbing}"
2011 "\\begin{table}"
2012 "\\begin{tabular}"
2013 "\\begin{thebibliography}"
2014 "\\begin{theorem}"
2015 "\\begin{titlepage}"
2016 "\\begin{verbatim}"
2017 "\\begin{verse}"
2018 "\\bf"
2019 "\\bf"
2020 "\\bibitem"
2021 "\\bigskip"
2022 "\\cdots"
2023 "\\centering"
2024 "\\circle"
2025 "\\cite"
2026 "\\cleardoublepage"
2027 "\\clearpage"
2028 "\\cline"
2029 "\\closing"
2030 "\\dashbox"
2031 "\\date"
2032 "\\ddots"
2033 "\\dotfill"
2034 "\\em"
2035 "\\fbox"
2036 "\\flushbottom"
2037 "\\fnsymbol"
2038 "\\footnote"
2039 "\\footnotemark"
2040 "\\footnotesize"
2041 "\\footnotetext"
2042 "\\frac"
2043 "\\frame"
2044 "\\framebox"
2045 "\\hfill"
2046 "\\hline"
2047 "\\hrulespace"
2048 "\\hspace"
2049 "\\huge"
2050 "\\hyphenation"
2051 "\\include"
2052 "\\includeonly"
2053 "\\indent"
2054 "\\input"
2055 "\\it"
2056 "\\kill"
2057 "\\label"
2058 "\\large"
2059 "\\ldots"
2060 "\\line"
2061 "\\linebreak"
2062 "\\linethickness"
2063 "\\listoffigures"
2064 "\\listoftables"
2065 "\\location"
2066 "\\makebox"
2067 "\\maketitle"
2068 "\\mark"
2069 "\\mbox"
2070 "\\medskip"
2071 "\\multicolumn"
2072 "\\multiput"
2073 ("\\nbsp" "~")
2074 "\\newcommand"
2075 "\\newcounter"
2076 "\\newenvironment"
2077 "\\newfont"
2078 "\\newlength"
2079 "\\newline"
2080 "\\newpage"
2081 "\\newsavebox"
2082 "\\newtheorem"
2083 "\\nocite"
2084 "\\nofiles"
2085 "\\noindent"
2086 "\\nolinebreak"
2087 "\\nopagebreak"
2088 "\\normalsize"
2089 "\\onecolumn"
2090 "\\opening"
2091 "\\oval"
2092 "\\overbrace"
2093 "\\overline"
2094 "\\pagebreak"
2095 "\\pagenumbering"
2096 "\\pageref"
2097 "\\pagestyle"
2098 "\\par"
2099 "\\parbox"
2100 "\\put"
2101 "\\raggedbottom"
2102 "\\raggedleft"
2103 "\\raggedright"
2104 "\\raisebox"
2105 "\\ref"
2106 "\\rm"
2107 "\\roman"
2108 "\\rule"
2109 "\\savebox"
2110 "\\sc"
2111 "\\scriptsize"
2112 "\\setcounter"
2113 "\\setlength"
2114 "\\settowidth"
2115 "\\sf"
2116 "\\shortstack"
2117 "\\signature"
2118 "\\sl"
2119 "\\small"
2120 "\\smallskip"
2121 "\\sqrt"
2122 "\\tableofcontents"
2123 "\\telephone"
2124 "\\thanks"
2125 "\\thispagestyle"
2126 "\\tiny"
2127 "\\title"
2128 "\\tt"
2129 "\\twocolumn"
2130 "\\typein"
2131 "\\typeout"
2132 "\\underbrace"
2133 "\\underline"
2134 "\\usebox"
2135 "\\usecounter"
2136 "\\value"
2137 "\\vdots"
2138 "\\vector"
2139 "\\verb"
2140 "\\vfill"
2141 "\\vline"
2142 "\\vspace")
2143 "A list of LaTeX commands to be protected when performing conversion.")
2145 (defvar org-latex-entities-exceptions nil)
2147 (defconst org-latex-entities-regexp
2148 (let (names rest)
2149 (dolist (x org-latex-entities)
2150 (when (consp x)
2151 (add-to-list 'org-latex-entities-exceptions x)
2152 (setq x (car x)))
2153 (if (string-match "[a-zA-Z]$" x)
2154 (push x names)
2155 (push x rest)))
2156 (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
2157 "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
2159 (provide 'org-export-latex)
2160 (provide 'org-latex)
2162 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
2164 ;;; org-latex.el ends here