LaTeX export: Preprocess TEXT properly
[org-mode.git] / lisp / org-latex.el
blobe36140eb2c49a29f8e49f514fc5c9f206dcdcd6a
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 (let ((inhibit-read-only t))
564 (remove-text-properties (point-min) (point-max)
565 '(:org-license-to-kill nil))))
566 (org-update-radio-target-regexp)
567 (org-export-latex-set-initial-vars ext-plist arg)
568 (run-hooks 'org-export-latex-after-initial-vars-hook)
569 (let* ((wcf (current-window-configuration))
570 (opt-plist org-export-latex-options-plist)
571 (region-p (org-region-active-p))
572 (rbeg (and region-p (region-beginning)))
573 (rend (and region-p (region-end)))
574 (subtree-p
575 (if (plist-get opt-plist :ignore-subtree-p)
577 (when region-p
578 (save-excursion
579 (goto-char rbeg)
580 (and (org-at-heading-p)
581 (>= (org-end-of-subtree t t) rend))))))
582 (opt-plist (setq org-export-opt-plist
583 (if subtree-p
584 (org-export-add-subtree-options opt-plist rbeg)
585 opt-plist)))
586 ;; Make sure the variable contains the updated values.
587 (org-export-latex-options-plist opt-plist)
588 (title (or (and subtree-p (org-export-get-title-from-subtree))
589 (plist-get opt-plist :title)
590 (and (not
591 (plist-get opt-plist :skip-before-1st-heading))
592 (org-export-grab-title-from-buffer))
593 (file-name-sans-extension
594 (file-name-nondirectory buffer-file-name))))
595 (filename (concat (file-name-as-directory
596 (or pub-dir
597 (org-export-directory :LaTeX ext-plist)))
598 (file-name-sans-extension
599 (or (and subtree-p
600 (org-entry-get rbeg "EXPORT_FILE_NAME" t))
601 (file-name-nondirectory ;sans-extension
602 buffer-file-name)))
603 ".tex"))
604 (filename (if (equal (file-truename filename)
605 (file-truename buffer-file-name))
606 (concat filename ".tex")
607 filename))
608 (buffer (if to-buffer
609 (cond
610 ((eq to-buffer 'string) (get-buffer-create
611 "*Org LaTeX Export*"))
612 (t (get-buffer-create to-buffer)))
613 (find-file-noselect filename)))
614 (odd org-odd-levels-only)
615 (header (org-export-latex-make-header title opt-plist))
616 (skip (cond (subtree-p nil)
617 (region-p nil)
618 (t (plist-get opt-plist :skip-before-1st-heading))))
619 (text (plist-get opt-plist :text))
620 (org-export-preprocess-hook
621 (cons
622 `(lambda () (org-set-local 'org-complex-heading-regexp
623 ,org-export-latex-complex-heading-re))
624 org-export-preprocess-hook))
625 (first-lines (if skip "" (org-export-latex-first-lines
626 opt-plist
627 (if subtree-p
628 (save-excursion
629 (goto-char rbeg)
630 (point-at-bol 2))
631 rbeg)
632 (if region-p rend))))
633 (coding-system (and (boundp 'buffer-file-coding-system)
634 buffer-file-coding-system))
635 (coding-system-for-write (or org-export-latex-coding-system
636 coding-system))
637 (save-buffer-coding-system (or org-export-latex-coding-system
638 coding-system))
639 (region (buffer-substring
640 (if region-p (region-beginning) (point-min))
641 (if region-p (region-end) (point-max))))
642 (text
643 (org-export-preprocess-string
644 text
645 :emph-multiline t
646 :for-LaTeX t
647 :comments nil
648 :tags (plist-get opt-plist :tags)
649 :priority (plist-get opt-plist :priority)
650 :footnotes (plist-get opt-plist :footnotes)
651 :drawers (plist-get opt-plist :drawers)
652 :timestamps (plist-get opt-plist :timestamps)
653 :todo-keywords (plist-get opt-plist :todo-keywords)
654 :add-text nil
655 :skip-before-1st-heading skip
656 :select-tags nil
657 :exclude-tags nil
658 :LaTeX-fragments nil))
659 (string-for-export
660 (org-export-preprocess-string
661 region
662 :emph-multiline t
663 :for-LaTeX t
664 :comments nil
665 :tags (plist-get opt-plist :tags)
666 :priority (plist-get opt-plist :priority)
667 :footnotes (plist-get opt-plist :footnotes)
668 :drawers (plist-get opt-plist :drawers)
669 :timestamps (plist-get opt-plist :timestamps)
670 :todo-keywords (plist-get opt-plist :todo-keywords)
671 :add-text (if (eq to-buffer 'string) nil text)
672 :skip-before-1st-heading skip
673 :select-tags (plist-get opt-plist :select-tags)
674 :exclude-tags (plist-get opt-plist :exclude-tags)
675 :LaTeX-fragments nil)))
677 (set-buffer buffer)
678 (erase-buffer)
679 (org-install-letbind)
681 (and (fboundp 'set-buffer-file-coding-system)
682 (set-buffer-file-coding-system coding-system-for-write))
684 ;; insert the header and initial document commands
685 (unless (or (eq to-buffer 'string) body-only)
686 (insert header))
688 ;; insert text found in #+TEXT
689 (when (and text (not (eq to-buffer 'string)))
690 (insert (org-export-latex-content
691 text '(lists tables fixed-width keywords))
692 "\n\n"))
694 ;; insert lines before the first headline
695 (unless skip
696 (insert first-lines))
698 ;; export the content of headlines
699 (org-export-latex-global
700 (with-temp-buffer
701 (insert string-for-export)
702 (goto-char (point-min))
703 (when (re-search-forward "^\\(\\*+\\) " nil t)
704 (let* ((asters (length (match-string 1)))
705 (level (if odd (- asters 2) (- asters 1))))
706 (setq org-export-latex-add-level
707 (if odd (1- (/ (1+ asters) 2)) (1- asters)))
708 (org-export-latex-parse-global level odd)))))
710 ;; finalization
711 (unless body-only (insert "\n\\end{document}"))
713 ;; Attach description terms to the \item macro
714 (goto-char (point-min))
715 (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t)
716 (delete-region (match-beginning 1) (match-end 1)))
718 ;; Relocate the table of contents
719 (goto-char (point-min))
720 (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
721 (goto-char (point-min))
722 (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t)
723 (replace-match ""))
724 (goto-char (point-min))
725 (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
726 (replace-match "\\tableofcontents" t t)))
728 ;; Cleanup forced line ends in items where they are not needed
729 (goto-char (point-min))
730 (while (re-search-forward
731 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin"
732 nil t)
733 (delete-region (match-beginning 1) (match-end 1)))
734 (goto-char (point-min))
735 (while (re-search-forward
736 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*"
737 nil t)
738 (if (looking-at "[\n \t]+")
739 (replace-match "\n")))
741 (run-hooks 'org-export-latex-final-hook)
742 (or to-buffer (save-buffer))
743 (goto-char (point-min))
744 (or (org-export-push-to-kill-ring "LaTeX")
745 (message "Exporting to LaTeX...done"))
746 (prog1
747 (if (eq to-buffer 'string)
748 (prog1 (buffer-substring (point-min) (point-max))
749 (kill-buffer (current-buffer)))
750 (current-buffer))
751 (set-window-configuration wcf))))
753 ;;;###autoload
754 (defun org-export-as-pdf (arg &optional hidden ext-plist
755 to-buffer body-only pub-dir)
756 "Export as LaTeX, then process through to PDF."
757 (interactive "P")
758 (message "Exporting to PDF...")
759 (let* ((wconfig (current-window-configuration))
760 (lbuf (org-export-as-latex arg hidden ext-plist
761 to-buffer body-only pub-dir))
762 (file (buffer-file-name lbuf))
763 (base (file-name-sans-extension (buffer-file-name lbuf)))
764 (pdffile (concat base ".pdf"))
765 (cmds org-latex-to-pdf-process)
766 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
767 (bibtex-p (with-current-buffer lbuf
768 (save-excursion
769 (goto-char (point-min))
770 (re-search-forward "\\\\bibliography{" nil t))))
771 cmd)
772 (with-current-buffer outbuf (erase-buffer))
773 (message "Processing LaTeX file...")
774 (if (and cmds (symbolp cmds))
775 (funcall cmds file)
776 (while cmds
777 (setq cmd (pop cmds))
778 (while (string-match "%b" cmd)
779 (setq cmd (replace-match
780 (save-match-data
781 (shell-quote-argument base))
782 t t cmd)))
783 (while (string-match "%s" cmd)
784 (setq cmd (replace-match
785 (save-match-data
786 (shell-quote-argument file))
787 t t cmd)))
788 (shell-command cmd outbuf outbuf)))
789 (message "Processing LaTeX file...done")
790 (if (not (file-exists-p pdffile))
791 (error "PDF file was not produced")
792 (set-window-configuration wconfig)
793 (when org-export-pdf-remove-logfiles
794 (dolist (ext org-export-pdf-logfiles)
795 (setq file (concat base "." ext))
796 (and (file-exists-p file) (delete-file file))))
797 (message "Exporting to PDF...done")
798 pdffile)))
800 ;;;###autoload
801 (defun org-export-as-pdf-and-open (arg)
802 "Export as LaTeX, then process through to PDF, and open."
803 (interactive "P")
804 (let ((pdffile (org-export-as-pdf arg)))
805 (if pdffile
806 (org-open-file pdffile)
807 (error "PDF file was not produced"))))
809 ;;; Parsing functions:
811 (defun org-export-latex-parse-global (level odd)
812 "Parse the current buffer recursively, starting at LEVEL.
813 If ODD is non-nil, assume the buffer only contains odd sections.
814 Return a list reflecting the document structure."
815 (save-excursion
816 (goto-char (point-min))
817 (let* ((cnt 0) output
818 (depth org-export-latex-sectioning-depth))
819 (while (org-re-search-forward-unprotected
820 (concat "^\\(\\(?:\\*\\)\\{"
821 (number-to-string (+ (if odd 2 1) level))
822 "\\}\\) \\(.*\\)$")
823 ;; make sure that there is no upper heading
824 (when (> level 0)
825 (save-excursion
826 (save-match-data
827 (org-re-search-forward-unprotected
828 (concat "^\\(\\(?:\\*\\)\\{"
829 (number-to-string level)
830 "\\}\\) \\(.*\\)$") nil t)))) t)
831 (setq cnt (1+ cnt))
832 (let* ((pos (match-beginning 0))
833 (heading (match-string 2))
834 (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
835 (save-excursion
836 (narrow-to-region
837 (point)
838 (save-match-data
839 (if (org-re-search-forward-unprotected
840 (concat "^\\(\\(?:\\*\\)\\{"
841 (number-to-string (+ (if odd 2 1) level))
842 "\\}\\) \\(.*\\)$") nil t)
843 (match-beginning 0)
844 (point-max))))
845 (goto-char (point-min))
846 (setq output
847 (append output
848 (list
849 (list
850 `(pos . ,pos)
851 `(level . ,nlevel)
852 `(occur . ,cnt)
853 `(heading . ,heading)
854 `(content . ,(org-export-latex-parse-content))
855 `(subcontent . ,(org-export-latex-parse-subcontent
856 level odd)))))))
857 (widen)))
858 (list output))))
860 (defun org-export-latex-parse-content ()
861 "Extract the content of a section."
862 (let ((beg (point))
863 (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t)
864 (progn (beginning-of-line) (point))
865 (point-max))))
866 (buffer-substring beg end)))
868 (defun org-export-latex-parse-subcontent (level odd)
869 "Extract the subcontent of a section at LEVEL.
870 If ODD Is non-nil, assume subcontent only contains odd sections."
871 (if (not (org-re-search-forward-unprotected
872 (concat "^\\(\\(?:\\*\\)\\{"
873 (number-to-string (+ (if odd 4 2) level))
874 "\\}\\) \\(.*\\)$")
875 nil t))
876 nil ; subcontent is nil
877 (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
879 ;;; Rendering functions:
880 (defun org-export-latex-global (content)
881 "Export CONTENT to LaTeX.
882 CONTENT is an element of the list produced by
883 `org-export-latex-parse-global'."
884 (if (eq (car content) 'subcontent)
885 (mapc 'org-export-latex-sub (cdr content))
886 (org-export-latex-sub (car content))))
888 (defun org-export-latex-sub (subcontent)
889 "Export the list SUBCONTENT to LaTeX.
890 SUBCONTENT is an alist containing information about the headline
891 and its content."
892 (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
893 (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
895 (defun org-export-latex-subcontent (subcontent num)
896 "Export each cell of SUBCONTENT to LaTeX.
897 If NUM, export sections as numerical sections."
898 (let* ((heading (cdr (assoc 'heading subcontent)))
899 (level (- (cdr (assoc 'level subcontent))
900 org-export-latex-add-level))
901 (occur (number-to-string (cdr (assoc 'occur subcontent))))
902 (content (cdr (assoc 'content subcontent)))
903 (subcontent (cadr (assoc 'subcontent subcontent)))
904 (label (org-get-text-property-any 0 'target heading))
905 (label-list (cons label (cdr (assoc label
906 org-export-target-aliases))))
907 (sectioning org-export-latex-sectioning)
908 (depth org-export-latex-sectioning-depth)
909 main-heading sub-heading)
910 (when (symbolp (car sectioning))
911 (setq sectioning (funcall (car sectioning) level heading))
912 (when sectioning
913 (setq heading (car sectioning)
914 sectioning (cdr sectioning)))
915 (if sectioning (setq sectioning (make-list 10 sectioning)))
916 (setq depth (if sectioning 10000 0)))
917 (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading)
918 (setq main-heading (substring heading 0 (match-beginning 0))
919 sub-heading (substring heading (match-end 0))))
920 (setq heading (org-export-latex-fontify-headline heading)
921 sub-heading (and sub-heading
922 (org-export-latex-fontify-headline sub-heading))
923 main-heading (and main-heading
924 (org-export-latex-fontify-headline main-heading)))
925 (cond
926 ;; Normal conversion
927 ((<= level depth)
928 (let* ((sec (nth (1- level) sectioning))
929 start end)
930 (if (consp (cdr sec))
931 (setq start (nth (if num 0 2) sec)
932 end (nth (if num 1 3) sec))
933 (setq start (if num (car sec) (cdr sec))))
934 (insert (format start (if main-heading main-heading heading)
935 (or sub-heading "")))
936 (insert "\n")
937 (when label
938 (insert (mapconcat (lambda (l) (format "\\label{%s}" l))
939 label-list "\n") "\n"))
940 (insert (org-export-latex-content content))
941 (cond ((stringp subcontent) (insert subcontent))
942 ((listp subcontent)
943 (while (org-looking-back "\n\n") (backward-delete-char 1))
944 (org-export-latex-sub subcontent)))
945 (when (and end (string-match "[^ \t]" end))
946 (let ((hook (org-get-text-property-any 0 'org-insert-hook end)))
947 (and (functionp hook) (funcall hook)))
948 (insert end "\n"))))
949 ;; At a level under the hl option: we can drop this subsection
950 ((> level depth)
951 (cond ((eq org-export-latex-low-levels 'description)
952 (if (string-match "% ends low level$"
953 (buffer-substring (point-at-bol 0) (point)))
954 (delete-region (point-at-bol 0) (point))
955 (insert "\\begin{description}\n"))
956 (insert (format "\n\\item[%s]%s~\n"
957 heading
958 (if label (format "\\label{%s}" label) "")))
959 (insert (org-export-latex-content content))
960 (cond ((stringp subcontent) (insert subcontent))
961 ((listp subcontent) (org-export-latex-sub subcontent)))
962 (insert "\\end{description} % ends low level\n"))
963 ((memq org-export-latex-low-levels '(itemize enumerate))
964 (if (string-match "% ends low level$"
965 (buffer-substring (point-at-bol 0) (point)))
966 (delete-region (point-at-bol 0) (point))
967 (insert (format "\\begin{%s}\n"
968 (symbol-name org-export-latex-low-levels))))
969 (insert (format "\n\\item %s\\\\\n%s%%"
970 heading
971 (if label (format "\\label{%s}" label) "")))
972 (insert (org-export-latex-content content))
973 (cond ((stringp subcontent) (insert subcontent))
974 ((listp subcontent) (org-export-latex-sub subcontent)))
975 (insert (format "\\end{%s} %% ends low level\n"
976 (symbol-name org-export-latex-low-levels))))
978 ((listp org-export-latex-low-levels)
979 (if (string-match "% ends low level$"
980 (buffer-substring (point-at-bol 0) (point)))
981 (delete-region (point-at-bol 0) (point))
982 (insert (car org-export-latex-low-levels) "\n"))
983 (insert (format (nth 2 org-export-latex-low-levels)
984 heading
985 (if label (format "\\label{%s}" label) "")))
986 (insert (org-export-latex-content content))
987 (cond ((stringp subcontent) (insert subcontent))
988 ((listp subcontent) (org-export-latex-sub subcontent)))
989 (insert (nth 1 org-export-latex-low-levels)
990 " %% ends low level\n"))
992 ((stringp org-export-latex-low-levels)
993 (insert (format org-export-latex-low-levels heading) "\n")
994 (when label (insert (format "\\label{%s}\n" label)))
995 (insert (org-export-latex-content content))
996 (cond ((stringp subcontent) (insert subcontent))
997 ((listp subcontent) (org-export-latex-sub subcontent)))))))))
999 ;;; Exporting internals:
1000 (defun org-export-latex-set-initial-vars (ext-plist level)
1001 "Store org local variables required for LaTeX export.
1002 EXT-PLIST is an optional additional plist.
1003 LEVEL indicates the default depth for export."
1004 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
1005 org-export-latex-done-keywords org-done-keywords
1006 org-export-latex-not-done-keywords org-not-done-keywords
1007 org-export-latex-complex-heading-re org-complex-heading-regexp
1008 org-export-latex-display-custom-times org-display-custom-times
1009 org-export-latex-all-targets-re
1010 (org-make-target-link-regexp (org-all-targets))
1011 org-export-latex-options-plist
1012 (org-combine-plists (org-default-export-plist) ext-plist
1013 (org-infile-export-plist))
1014 org-export-latex-class
1015 (or (and (org-region-active-p)
1016 (save-excursion
1017 (goto-char (region-beginning))
1018 (and (looking-at org-complex-heading-regexp)
1019 (org-entry-get nil "LaTeX_CLASS" 'selective))))
1020 (save-excursion
1021 (save-restriction
1022 (widen)
1023 (goto-char (point-min))
1024 (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
1025 (match-string 1))))
1026 (plist-get org-export-latex-options-plist :latex-class)
1027 org-export-latex-default-class)
1028 org-export-latex-class-options
1029 (or (and (org-region-active-p)
1030 (save-excursion
1031 (goto-char (region-beginning))
1032 (and (looking-at org-complex-heading-regexp)
1033 (org-entry-get nil "LaTeX_CLASS_OPTIONS" 'selective))))
1034 (save-excursion
1035 (save-restriction
1036 (widen)
1037 (goto-char (point-min))
1038 (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t)
1039 (match-string 1))))
1040 (plist-get org-export-latex-options-plist :latex-class-options))
1041 org-export-latex-class
1042 (or (car (assoc org-export-latex-class org-export-latex-classes))
1043 (error "No definition for class `%s' in `org-export-latex-classes'"
1044 org-export-latex-class))
1045 org-export-latex-header
1046 (cadr (assoc org-export-latex-class org-export-latex-classes))
1047 org-export-latex-sectioning
1048 (cddr (assoc org-export-latex-class org-export-latex-classes))
1049 org-export-latex-sectioning-depth
1050 (or level
1051 (let ((hl-levels
1052 (plist-get org-export-latex-options-plist :headline-levels))
1053 (sec-depth (length org-export-latex-sectioning)))
1054 (if (> hl-levels sec-depth) sec-depth hl-levels))))
1055 (when (and org-export-latex-class-options
1056 (string-match "\\S-" org-export-latex-class-options)
1057 (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?"
1058 org-export-latex-header))
1059 (setq org-export-latex-header
1060 (concat (substring org-export-latex-header 0 (match-end 1))
1061 org-export-latex-class-options
1062 (substring org-export-latex-header (match-end 0))))))
1064 (defvar org-export-latex-format-toc-function
1065 'org-export-latex-format-toc-default
1066 "The function formatting returning the string to createthe table of contents.
1067 The function mus take one parameter, the depth of the table of contents.")
1069 (defun org-export-latex-make-header (title opt-plist)
1070 "Make the LaTeX header and return it as a string.
1071 TITLE is the current title from the buffer or region.
1072 OPT-PLIST is the options plist for current buffer."
1073 (let ((toc (plist-get opt-plist :table-of-contents))
1074 (author (plist-get opt-plist :author)))
1075 (concat
1076 (if (plist-get opt-plist :time-stamp-file)
1077 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1078 ;; insert LaTeX custom header
1079 (org-export-apply-macros-in-string org-export-latex-header)
1080 "\n"
1081 ;; insert information on LaTeX packages
1082 (when org-export-latex-packages-alist
1083 (concat
1084 (mapconcat (lambda(p)
1085 (if (equal "" (car p))
1086 (format "\\usepackage{%s}" (cadr p))
1087 (format "\\usepackage[%s]{%s}"
1088 (car p) (cadr p))))
1089 org-export-latex-packages-alist "\n")
1090 "\n"))
1091 ;; insert additional commands in the header
1092 (org-export-apply-macros-in-string
1093 (plist-get opt-plist :latex-header-extra))
1094 (org-export-apply-macros-in-string org-export-latex-append-header)
1095 ;; insert the title
1096 (format
1097 "\n\n\\title{%s}\n"
1098 ;; convert the title
1099 (org-export-latex-content
1100 title '(lists tables fixed-width keywords)))
1101 ;; insert author info
1102 (if (plist-get opt-plist :author-info)
1103 (format "\\author{%s}\n"
1104 (org-export-latex-fontify-headline (or author user-full-name)))
1105 (format "%%\\author{%s}\n"
1106 (or author user-full-name)))
1107 ;; insert the date
1108 (format "\\date{%s}\n"
1109 (format-time-string
1110 (or (plist-get opt-plist :date)
1111 org-export-latex-date-format)))
1112 ;; beginning of the document
1113 "\n\\begin{document}\n\n"
1114 ;; insert the title command
1115 (when (string-match "\\S-" title)
1116 (if (string-match "%s" org-export-latex-title-command)
1117 (format org-export-latex-title-command title)
1118 org-export-latex-title-command))
1119 "\n\n"
1120 ;; table of contents
1121 (when (and org-export-with-toc
1122 (plist-get opt-plist :section-numbers))
1123 (funcall org-export-latex-format-toc-function
1124 (cond ((numberp toc)
1125 (min toc (plist-get opt-plist :headline-levels)))
1126 (toc (plist-get opt-plist :headline-levels))))))))
1128 (defun org-export-latex-format-toc-default (depth)
1129 (when depth
1130 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1131 depth)))
1133 (defun org-export-latex-first-lines (opt-plist &optional beg end)
1134 "Export the first lines before first headline.
1135 If BEG is non-nil, it is the beginning of the region.
1136 If END is non-nil, it is the end of the region."
1137 (save-excursion
1138 (goto-char (or beg (point-min)))
1139 (let* ((pt (point))
1140 (end (if (re-search-forward "^\\*+ " end t)
1141 (goto-char (match-beginning 0))
1142 (goto-char (or end (point-max))))))
1143 (prog1
1144 (org-export-latex-content
1145 (org-export-preprocess-string
1146 (buffer-substring pt end)
1147 :for-LaTeX t
1148 :emph-multiline t
1149 :add-text nil
1150 :comments nil
1151 :skip-before-1st-heading nil
1152 :LaTeX-fragments nil
1153 :timestamps (plist-get opt-plist :timestamps)
1154 :footnotes (plist-get opt-plist :footnotes)))
1155 (org-unmodified
1156 (let ((inhibit-read-only t))
1157 (add-text-properties pt (max pt (1- end))
1158 '(:org-license-to-kill t))))))))
1160 (defvar org-export-latex-header-defs nil
1161 "The header definitions that might be used in the LaTeX body.")
1162 (defvar org-export-latex-header-defs-re nil
1163 "The header definitions that might be used in the LaTeX body.")
1165 (defun org-export-latex-content (content &optional exclude-list)
1166 "Convert CONTENT string to LaTeX.
1167 Don't perform conversions that are in EXCLUDE-LIST. Recognized
1168 conversion types are: quotation-marks, emphasis, sub-superscript,
1169 links, keywords, lists, tables, fixed-width"
1170 (with-temp-buffer
1171 (insert content)
1172 (unless (memq 'timestamps exclude-list)
1173 (org-export-latex-time-stamps))
1174 (unless (memq 'quotation-marks exclude-list)
1175 (org-export-latex-quotation-marks))
1176 (unless (memq 'emphasis exclude-list)
1177 (when (plist-get org-export-latex-options-plist :emphasize)
1178 (org-export-latex-fontify)))
1179 (unless (memq 'sub-superscript exclude-list)
1180 (org-export-latex-special-chars
1181 (plist-get org-export-latex-options-plist :sub-superscript)))
1182 (unless (memq 'links exclude-list)
1183 (org-export-latex-links))
1184 (unless (memq 'keywords exclude-list)
1185 (org-export-latex-keywords))
1186 (unless (memq 'lists exclude-list)
1187 (org-export-latex-lists))
1188 (unless (memq 'tables exclude-list)
1189 (org-export-latex-tables
1190 (plist-get org-export-latex-options-plist :tables)))
1191 (unless (memq 'fixed-width exclude-list)
1192 (org-export-latex-fixed-width
1193 (plist-get org-export-latex-options-plist :fixed-width)))
1194 ;; return string
1195 (buffer-substring (point-min) (point-max))))
1197 (defun org-export-latex-protect-string (s)
1198 "Add the org-protected property to string S."
1199 (add-text-properties 0 (length s) '(org-protected t) s) s)
1201 (defun org-export-latex-protect-char-in-string (char-list string)
1202 "Add org-protected text-property to char from CHAR-LIST in STRING."
1203 (with-temp-buffer
1204 (save-match-data
1205 (insert string)
1206 (goto-char (point-min))
1207 (while (re-search-forward (regexp-opt char-list) nil t)
1208 (add-text-properties (match-beginning 0)
1209 (match-end 0) '(org-protected t)))
1210 (buffer-string))))
1212 (defun org-export-latex-keywords-maybe (&optional remove-list)
1213 "Maybe remove keywords depending on rules in REMOVE-LIST."
1214 (goto-char (point-min))
1215 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
1216 (case-fold-search nil)
1217 (todo-markup org-export-latex-todo-keyword-markup)
1218 fmt)
1219 ;; convert TODO keywords
1220 (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
1221 (if (plist-get remove-list :todo)
1222 (replace-match "")
1223 (setq fmt (cond
1224 ((stringp todo-markup) todo-markup)
1225 ((and (consp todo-markup) (stringp (car todo-markup)))
1226 (if (member (match-string 1) org-export-latex-done-keywords)
1227 (cdr todo-markup) (car todo-markup)))
1228 (t (cdr (or (assoc (match-string 1) todo-markup)
1229 (car todo-markup))))))
1230 (replace-match (format fmt (match-string 1)) t t)))
1231 ;; convert priority string
1232 (when (re-search-forward "\\[\\\\#.\\]" nil t)
1233 (if (plist-get remove-list :priority)
1234 (replace-match "")
1235 (replace-match (format "\\textbf{%s}" (match-string 0)) t t)))
1236 ;; convert tags
1237 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t)
1238 (if (or (not org-export-with-tags)
1239 (plist-get remove-list :tags))
1240 (replace-match "")
1241 (replace-match
1242 (org-export-latex-protect-string
1243 (format "\\textbf{%s}"
1244 (save-match-data
1245 (replace-regexp-in-string
1246 "_" "\\\\_" (match-string 0)))))
1247 t t)))))
1249 (defun org-export-latex-fontify-headline (string)
1250 "Fontify special words in STRING."
1251 (with-temp-buffer
1252 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
1253 ;; the beginning of the buffer - inserting "\n" is safe here though.
1254 (insert "\n" string)
1255 (goto-char (point-min))
1256 (let ((re (concat "\\\\[a-zA-Z]+\\(?:"
1257 "\\[.*\\]"
1258 "\\)?"
1259 (org-create-multibrace-regexp "{" "}" 3))))
1260 (while (re-search-forward re nil t)
1261 (unless (save-excursion (goto-char (match-beginning 0))
1262 (equal (char-after (point-at-bol)) ?#))
1263 (add-text-properties (match-beginning 0) (match-end 0)
1264 '(org-protected t)))))
1265 (when (plist-get org-export-latex-options-plist :emphasize)
1266 (org-export-latex-fontify))
1267 (org-export-latex-keywords-maybe)
1268 (org-export-latex-special-chars
1269 (plist-get org-export-latex-options-plist :sub-superscript))
1270 (org-export-latex-links)
1271 (org-trim (buffer-string))))
1273 (defun org-export-latex-time-stamps ()
1274 "Format time stamps."
1275 (goto-char (point-min))
1276 (let ((org-display-custom-times org-export-latex-display-custom-times))
1277 (while (re-search-forward org-ts-regexp-both nil t)
1278 (org-if-unprotected-at (1- (point))
1279 (replace-match
1280 (org-export-latex-protect-string
1281 (format org-export-latex-timestamp-markup
1282 (substring (org-translate-time (match-string 0)) 1 -1)))
1283 t t)))))
1285 (defun org-export-latex-quotation-marks ()
1286 "Export quotation marks depending on language conventions."
1287 (let* ((lang (plist-get org-export-latex-options-plist :language))
1288 (quote-rpl (if (equal lang "fr")
1289 '(("\\(\\s-\\)\"" "«~")
1290 ("\\(\\S-\\)\"" "~»")
1291 ("\\(\\s-\\)'" "`"))
1292 '(("\\(\\s-\\|[[(]\\)\"" "``")
1293 ("\\(\\S-\\)\"" "''")
1294 ("\\(\\s-\\|(\\)'" "`")))))
1295 (mapc (lambda(l) (goto-char (point-min))
1296 (while (re-search-forward (car l) nil t)
1297 (let ((rpl (concat (match-string 1)
1298 (org-export-latex-protect-string
1299 (copy-sequence (cadr l))))))
1300 (org-if-unprotected-1
1301 (replace-match rpl t t))))) quote-rpl)))
1303 (defun org-export-latex-special-chars (sub-superscript)
1304 "Export special characters to LaTeX.
1305 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
1306 See the `org-export-latex.el' code for a complete conversion table."
1307 (goto-char (point-min))
1308 (mapc (lambda(c)
1309 (goto-char (point-min))
1310 (while (re-search-forward c nil t)
1311 ;; Put the point where to check for org-protected
1312 (unless (get-text-property (match-beginning 2) 'org-protected)
1313 (cond ((member (match-string 2) '("\\$" "$"))
1314 (if (equal (match-string 2) "\\$")
1316 (replace-match "\\$" t t)))
1317 ((member (match-string 2) '("&" "%" "#"))
1318 (if (equal (match-string 1) "\\")
1319 (replace-match (match-string 2) t t)
1320 (replace-match (concat (match-string 1) "\\"
1321 (match-string 2)) t t)))
1322 ((equal (match-string 2) "...")
1323 (replace-match
1324 (concat (match-string 1)
1325 (org-export-latex-protect-string "\\ldots{}")) t t))
1326 ((equal (match-string 2) "~")
1327 (cond ((equal (match-string 1) "\\") nil)
1328 ((eq 'org-link (get-text-property 0 'face (match-string 2)))
1329 (replace-match (concat (match-string 1) "\\~") t t))
1330 (t (replace-match
1331 (org-export-latex-protect-string
1332 (concat (match-string 1) "\\~{}")) t t))))
1333 ((member (match-string 2) '("{" "}"))
1334 (unless (save-match-data (org-inside-latex-math-p))
1335 (if (equal (match-string 1) "\\")
1336 (replace-match (match-string 2) t t)
1337 (replace-match (concat (match-string 1) "\\"
1338 (match-string 2)) t t)))))
1339 (unless (save-match-data (org-inside-latex-math-p))
1340 (cond ((equal (match-string 2) "\\")
1341 (replace-match (or (save-match-data
1342 (org-export-latex-treat-backslash-char
1343 (match-string 1)
1344 (or (match-string 3) "")))
1345 "") t t))
1346 ((member (match-string 2) '("_" "^"))
1347 (replace-match (or (save-match-data
1348 (org-export-latex-treat-sub-super-char
1349 sub-superscript
1350 (match-string 2)
1351 (match-string 1)
1352 (match-string 3))) "") t t)
1353 (backward-char 1)))))))
1354 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
1355 "\\(\\(\\\\?\\$\\)\\)"
1356 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
1357 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
1358 "\\(.\\|^\\)\\(&\\)"
1359 "\\(.\\|^\\)\\(#\\)"
1360 "\\(.\\|^\\)\\(%\\)"
1361 "\\(.\\|^\\)\\({\\)"
1362 "\\(.\\|^\\)\\(}\\)"
1363 "\\(.\\|^\\)\\(~\\)"
1364 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
1365 ;; (?\< . "\\textless{}")
1366 ;; (?\> . "\\textgreater{}")
1369 (defun org-inside-latex-math-p ()
1370 (get-text-property (point) 'org-latex-math))
1372 (defun org-export-latex-treat-sub-super-char
1373 (subsup char string-before string-after)
1374 "Convert the \"_\" and \"^\" characters to LaTeX.
1375 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
1376 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
1377 (cond ((equal string-before "\\")
1378 (concat string-before char string-after))
1379 ((and (string-match "\\S-+" string-after))
1380 ;; this is part of a math formula
1381 (cond ((eq 'org-link (get-text-property 0 'face char))
1382 (concat string-before "\\" char string-after))
1383 ((save-match-data (org-inside-latex-math-p))
1384 (if subsup
1385 (cond ((eq 1 (length string-after))
1386 (concat string-before char string-after))
1387 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
1388 (format "%s%s{%s}" string-before char
1389 (match-string 1 string-after))))))
1390 ((and (> (length string-after) 1)
1391 (or (eq subsup t)
1392 (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
1393 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
1394 (org-export-latex-protect-string
1395 (format "%s$%s{%s}$" string-before char
1396 (if (and (> (match-end 1) (1+ (match-beginning 1)))
1397 (not (equal (substring string-after 0 2) "{\\")))
1398 (concat "\\mathrm{" (match-string 1 string-after) "}")
1399 (match-string 1 string-after)))))
1400 ((eq subsup t) (concat string-before "$" char string-after "$"))
1401 (t (org-export-latex-protect-string
1402 (concat string-before "\\" char "{}" string-after)))))
1403 (t (org-export-latex-protect-string
1404 (concat string-before "\\" char "{}" string-after)))))
1406 (defun org-export-latex-treat-backslash-char (string-before string-after)
1407 "Convert the \"$\" special character to LaTeX.
1408 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
1409 (cond ((member (list string-after) org-html-entities)
1410 ;; backslash is part of a special entity (like "\alpha")
1411 (concat string-before "$\\"
1412 (or (cdar (member (list string-after) org-html-entities))
1413 string-after) "$"))
1414 ((and (not (string-match "^[ \n\t]" string-after))
1415 (not (string-match "[ \t]\\'\\|^" string-before)))
1416 ;; backslash is inside a word
1417 (org-export-latex-protect-string
1418 (concat string-before "\\textbackslash{}" string-after)))
1419 ((not (or (equal string-after "")
1420 (string-match "^[ \t\n]" string-after)))
1421 ;; backslash might escape a character (like \#) or a user TeX
1422 ;; macro (like \setcounter)
1423 (org-export-latex-protect-string
1424 (concat string-before "\\" string-after)))
1425 ((and (string-match "^[ \t\n]" string-after)
1426 (string-match "[ \t\n]\\'" string-before))
1427 ;; backslash is alone, convert it to $\backslash$
1428 (org-export-latex-protect-string
1429 (concat string-before "\\textbackslash{}" string-after)))
1430 (t (org-export-latex-protect-string
1431 (concat string-before "\\textbackslash{}" string-after)))))
1433 (defun org-export-latex-keywords ()
1434 "Convert special keywords to LaTeX."
1435 (goto-char (point-min))
1436 (while (re-search-forward org-export-latex-special-keyword-regexp nil t)
1437 (replace-match (format org-export-latex-timestamp-keyword-markup
1438 (match-string 0)) t t)
1439 (save-excursion
1440 (beginning-of-line 1)
1441 (unless (looking-at ".*\\\\newline[ \t]*$")
1442 (end-of-line 1)
1443 (insert "\\newline")))))
1445 (defun org-export-latex-fixed-width (opt)
1446 "When OPT is non-nil convert fixed-width sections to LaTeX."
1447 (goto-char (point-min))
1448 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t)
1449 (if opt
1450 (progn (goto-char (match-beginning 0))
1451 (insert "\\begin{verbatim}\n")
1452 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1453 (replace-match (concat (match-string 1)
1454 (match-string 2)) t t)
1455 (forward-line))
1456 (insert "\\end{verbatim}\n\n"))
1457 (progn (goto-char (match-beginning 0))
1458 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1459 (replace-match (concat "%" (match-string 1)
1460 (match-string 2)) t t)
1461 (forward-line))))))
1464 (defvar org-table-last-alignment) ; defined in org-table.el
1465 (defvar org-table-last-column-widths) ; defined in org-table.el
1466 (declare-function orgtbl-to-latex "org-table" (table params) t)
1467 (defun org-export-latex-tables (insert)
1468 "Convert tables to LaTeX and INSERT it."
1469 (goto-char (point-min))
1470 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
1471 (org-if-unprotected-at (1- (point))
1472 (org-table-align)
1473 (let* ((beg (org-table-begin))
1474 (end (org-table-end))
1475 (raw-table (buffer-substring beg end))
1476 (org-table-last-alignment (copy-sequence org-table-last-alignment))
1477 (org-table-last-column-widths (copy-sequence
1478 org-table-last-column-widths))
1479 fnum fields line lines olines gr colgropen line-fmt align
1480 caption label attr floatp longtblp)
1481 (if org-export-latex-tables-verbatim
1482 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1483 "\\end{verbatim}\n")))
1484 (apply 'delete-region (list beg end))
1485 (insert (org-export-latex-protect-string tbl)))
1486 (progn
1487 (setq caption (org-find-text-property-in-string
1488 'org-caption raw-table)
1489 attr (org-find-text-property-in-string
1490 'org-attributes raw-table)
1491 label (org-find-text-property-in-string
1492 'org-label raw-table)
1493 longtblp (and attr (stringp attr)
1494 (string-match "\\<longtable\\>" attr))
1495 align (and attr (stringp attr)
1496 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
1497 (match-string 1 attr))
1498 floatp (or caption label))
1499 (setq lines (org-split-string raw-table "\n"))
1500 (apply 'delete-region (list beg end))
1501 (when org-export-table-remove-special-lines
1502 (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
1503 (when org-table-clean-did-remove-column
1504 (pop org-table-last-alignment)
1505 (pop org-table-last-column-widths))
1506 ;; make a formatting string to reflect alignment
1507 (setq olines lines)
1508 (while (and (not line-fmt) (setq line (pop olines)))
1509 (unless (string-match "^[ \t]*|-" line)
1510 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1511 (setq fnum (make-vector (length fields) 0))
1512 (setq line-fmt
1513 (mapconcat
1514 (lambda (x)
1515 (setq gr (pop org-table-colgroup-info))
1516 (format "%s%%s%s"
1517 (cond ((eq gr :start)
1518 (prog1 (if colgropen "|" "|")
1519 (setq colgropen t)))
1520 ((eq gr :startend)
1521 (prog1 (if colgropen "|" "|")
1522 (setq colgropen nil)))
1523 (t ""))
1524 (if (memq gr '(:end :startend))
1525 (progn (setq colgropen nil) "|")
1526 "")))
1527 fnum ""))))
1528 ;; fix double || in line-fmt
1529 (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
1530 ;; maybe remove the first and last "|"
1531 (when (and (not org-export-latex-tables-column-borders)
1532 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
1533 (setq line-fmt (match-string 2 line-fmt)))
1534 ;; format alignment
1535 (unless align
1536 (setq align (apply 'format
1537 (cons line-fmt
1538 (mapcar (lambda (x) (if x "r" "l"))
1539 org-table-last-alignment)))))
1540 ;; prepare the table to send to orgtbl-to-latex
1541 (setq lines
1542 (mapcar
1543 (lambda(elem)
1544 (or (and (string-match "[ \t]*|-+" elem) 'hline)
1545 (org-split-string (org-trim elem) "|")))
1546 lines))
1547 (when insert
1548 (insert (org-export-latex-protect-string
1549 (concat
1550 (if longtblp
1551 (concat "\\begin{longtable}{" align "}\n")
1552 (if floatp "\\begin{table}[htb]\n"))
1553 (if floatp
1554 (format
1555 "\\caption{%s%s}"
1556 (if label (concat "\\\label{" label "}") "")
1557 (or caption "")))
1558 (if (and longtblp caption) "\\\\\n" "\n")
1559 (if (and org-export-latex-tables-centered (not longtblp))
1560 "\\begin{center}\n")
1561 (if (not longtblp) (concat "\\begin{tabular}{" align "}\n"))
1562 (orgtbl-to-latex
1563 lines
1564 `(:tstart nil :tend nil
1565 :hlend ,(if longtblp
1566 (format "\\\\
1567 \\hline
1568 \\endhead
1569 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
1570 \\endfoot
1571 \\endlastfoot" (length org-table-last-alignment))
1572 nil)))
1573 (if (not longtblp) (concat "\n\\end{tabular}"))
1574 (if longtblp "\n" (if org-export-latex-tables-centered
1575 "\n\\end{center}\n" "\n"))
1576 (if longtblp
1577 "\\end{longtable}"
1578 (if floatp "\\end{table}"))))
1579 "\n\n"))))))))
1581 (defun org-export-latex-fontify ()
1582 "Convert fontification to LaTeX."
1583 (goto-char (point-min))
1584 (while (re-search-forward org-emph-re nil t)
1585 ;; The match goes one char after the *string*, except at the end of a line
1586 (let ((emph (assoc (match-string 3)
1587 org-export-latex-emphasis-alist))
1588 (beg (match-beginning 0))
1589 (end (match-end 0))
1590 rpl s)
1591 (unless emph
1592 (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
1593 (match-string 3)))
1594 (unless (or (and (get-text-property (- (point) 2) 'org-protected)
1595 (not (get-text-property
1596 (- (point) 2) 'org-verbatim-emph)))
1597 (save-excursion
1598 (goto-char (match-beginning 1))
1599 (save-match-data
1600 (and (org-at-table-p)
1601 (string-match
1602 "[|\n]" (buffer-substring beg end))))))
1603 (setq s (match-string 4))
1604 (setq rpl (concat (match-string 1)
1605 (org-export-latex-emph-format (cadr emph)
1606 (match-string 4))
1607 (match-string 5)))
1608 (if (caddr emph)
1609 (setq rpl (org-export-latex-protect-string rpl))
1610 (save-match-data
1611 (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl)
1612 (progn
1613 (add-text-properties (match-beginning 1) (match-end 1)
1614 '(org-protected t) rpl)
1615 (add-text-properties (match-beginning 3) (match-end 3)
1616 '(org-protected t) rpl)))))
1617 (replace-match rpl t t)))
1618 (backward-char)))
1620 (defvar org-export-latex-use-verb nil)
1621 (defun org-export-latex-emph-format (format string)
1622 "Format an emphasis string and handle the \\verb special case."
1623 (when (equal format "\\verb")
1624 (save-match-data
1625 (if org-export-latex-use-verb
1626 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1627 (catch 'exit
1628 (loop for i from 0 to (1- (length ll)) do
1629 (if (not (string-match (regexp-quote (substring ll i (1+ i)))
1630 string))
1631 (progn
1632 (setq format (concat "\\verb" (substring ll i (1+ i))
1633 "%s" (substring ll i (1+ i))))
1634 (throw 'exit nil))))))
1635 (let ((start 0)
1636 (trans '(("\\" . "\\textbackslash{}")
1637 ("~" . "\\textasciitilde{}")
1638 ("^" . "\\textasciicircum{}")))
1639 (rtn "") char)
1640 (while (string-match "[\\{}$%&_#~^]" string)
1641 (setq char (match-string 0 string))
1642 (if (> (match-beginning 0) 0)
1643 (setq rtn (concat rtn (substring string
1644 0 (match-beginning 0)))))
1645 (setq string (substring string (1+ (match-beginning 0))))
1646 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1647 rtn (concat rtn char)))
1648 (setq string (concat rtn string) format "\\texttt{%s}")))))
1649 (format format string))
1651 (defun org-export-latex-links ()
1652 ;; Make sure to use the LaTeX hyperref and graphicx package
1653 ;; or send some warnings.
1654 "Convert links to LaTeX."
1655 (goto-char (point-min))
1656 (while (re-search-forward org-bracket-link-analytic-regexp++ nil t)
1657 (org-if-unprotected-1
1658 (goto-char (match-beginning 0))
1659 (let* ((re-radio org-export-latex-all-targets-re)
1660 (remove (list (match-beginning 0) (match-end 0)))
1661 (raw-path (org-extract-attributes (match-string 3)))
1662 (full-raw-path (concat (match-string 1) raw-path))
1663 (desc (match-string 5))
1664 (type (or (match-string 2)
1665 (if (or (file-name-absolute-p raw-path)
1666 (string-match "^\\.\\.?/" raw-path))
1667 "file")))
1668 (coderefp (equal type "coderef"))
1669 (caption (org-find-text-property-in-string 'org-caption raw-path))
1670 (attr (or (org-find-text-property-in-string 'org-attributes raw-path)
1671 (plist-get org-export-latex-options-plist :latex-image-options)))
1672 (label (org-find-text-property-in-string 'org-label raw-path))
1673 imgp radiop
1674 ;; define the path of the link
1675 (path (cond
1676 ((member type '("coderef"))
1677 raw-path)
1678 ((member type '("http" "https" "ftp"))
1679 (concat type ":" raw-path))
1680 ((and re-radio (string-match re-radio raw-path))
1681 (setq radiop t))
1682 ((equal type "mailto")
1683 (concat type ":" raw-path))
1684 ((equal type "file")
1685 (if (and (org-file-image-p
1686 (expand-file-name
1687 raw-path)
1688 org-export-latex-inline-image-extensions)
1689 (or (get-text-property 0 'org-no-description
1690 raw-path)
1691 (equal desc full-raw-path)))
1692 (setq imgp t)
1693 (progn (when (string-match "\\(.+\\)::.+" raw-path)
1694 (setq raw-path (match-string 1 raw-path)))
1695 (if (file-exists-p raw-path)
1696 (concat type "://" (expand-file-name raw-path))
1697 (concat type "://" (org-export-directory
1698 :LaTeX org-export-latex-options-plist)
1699 raw-path))))))))
1700 ;; process with link inserting
1701 (apply 'delete-region remove)
1702 (cond ((and imgp
1703 (plist-get org-export-latex-options-plist :inline-images))
1704 ;; OK, we need to inline an image
1705 (insert
1706 (org-export-latex-format-image raw-path caption label attr)))
1707 (coderefp
1708 (insert (format
1709 (org-export-get-coderef-format path desc)
1710 (cdr (assoc path org-export-code-refs)))))
1711 (radiop (insert (format "\\hyperref[%s]{%s}"
1712 (org-solidify-link-text raw-path) desc)))
1713 ((not type)
1714 (insert (format "\\hyperref[%s]{%s}"
1715 (org-remove-initial-hash
1716 (org-solidify-link-text raw-path))
1717 desc)))
1718 (path
1719 (when (org-at-table-p)
1720 ;; There is a strange problem when we have a link in a table,
1721 ;; ampersands then cause a problem. I think this must be
1722 ;; a LaTeX issue, but we here implement a work-around anyway.
1723 (setq path (org-export-latex-protect-amp path)
1724 desc (org-export-latex-protect-amp desc)))
1725 (insert (format "\\href{%s}{%s}" path desc)))
1726 (t (insert "\\texttt{" desc "}")))))))
1729 (defun org-export-latex-format-image (path caption label attr)
1730 "Format the image element, depending on user settings."
1731 (let (ind floatp wrapp placement figenv)
1732 (setq floatp (or caption label))
1733 (setq ind (org-get-text-property-any 0 'original-indentation path))
1734 (when (and attr (stringp attr))
1735 (if (string-match "[ \t]*\\<wrap\\>" attr)
1736 (setq wrapp t floatp nil attr (replace-match "" t t attr)))
1737 (if (string-match "[ \t]*\\<float\\>" attr)
1738 (setq wrapp nil floatp t attr (replace-match "" t t attr))))
1740 (setq placement
1741 (cond
1742 (wrapp "{l}{0.5\\textwidth}")
1743 (floatp "[htb]")
1744 (t "")))
1746 (when (and attr (stringp attr)
1747 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr))
1748 (setq placement (match-string 1 attr)
1749 attr (replace-match "" t t attr)))
1750 (setq attr (and attr (org-trim attr)))
1751 (when (or (not attr) (= (length attr) 0))
1752 (setq attr (cond (floatp "width=0.7\\textwidth")
1753 (wrapp "width=0.48\\textwidth")
1754 (t attr))))
1755 (setq figenv
1756 (cond
1757 (wrapp "\\begin{wrapfigure}%placement
1758 \\centering
1759 \\includegraphics[%attr]{%path}
1760 \\caption{%labelcmd%caption}
1761 \\end{wrapfigure}")
1762 (floatp "\\begin{figure}%placement
1763 \\centering
1764 \\includegraphics[%attr]{%path}
1765 \\caption{%labelcmd%caption}
1766 \\end{figure}")
1767 (t "\\includegraphics[%attr]{%path}")))
1770 (setq figenv (mapconcat 'identity (split-string figenv "\n")
1771 (save-excursion (beginning-of-line 1)
1772 (looking-at "[ \t]*")
1773 (concat "\n" (match-string 0)))))
1775 (if (and (not label) (not caption)
1776 (string-match "^\\\\caption{.*\n" figenv))
1777 (setq figenv (replace-match "" t t figenv)))
1778 (org-add-props
1779 (org-fill-template
1780 figenv
1781 (list (cons "path"
1782 (if (file-name-absolute-p path)
1783 (expand-file-name path)
1784 path))
1785 (cons "attr" attr)
1786 (cons "labelcmd" (if label (format "\\label{%s}"
1787 label)""))
1788 (cons "caption" (or caption ""))
1789 (cons "placement" (or placement ""))))
1790 nil 'original-indentation ind)))
1792 (defun org-export-latex-protect-amp (s)
1793 (while (string-match "\\([^\\\\]\\)\\(&\\)" s)
1794 (setq s (replace-match (concat (match-string 1 s) "\\" (match-string 2 s))
1795 t t s)))
1798 (defun org-remove-initial-hash (s)
1799 (if (string-match "\\`#" s)
1800 (substring s 1)
1802 (defvar org-latex-entities) ; defined below
1803 (defvar org-latex-entities-regexp) ; defined below
1804 (defvar org-latex-entities-exceptions) ; defined below
1806 (defun org-export-latex-preprocess (parameters)
1807 "Clean stuff in the LaTeX export."
1808 ;; Preserve line breaks
1809 (goto-char (point-min))
1810 (while (re-search-forward "\\\\\\\\" nil t)
1811 (add-text-properties (match-beginning 0) (match-end 0)
1812 '(org-protected t)))
1814 ;; Preserve latex environments
1815 (goto-char (point-min))
1816 (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t)
1817 (let* ((start (progn (beginning-of-line) (point)))
1818 (end (and (re-search-forward
1819 (concat "^[ \t]*\\\\end{"
1820 (regexp-quote (match-string 1))
1821 "}") nil t)
1822 (point-at-eol))))
1823 (if end
1824 (add-text-properties start end '(org-protected t))
1825 (goto-char (point-at-eol)))))
1827 ;; Preserve math snippets
1829 (let* ((matchers (plist-get org-format-latex-options :matchers))
1830 (re-list org-latex-regexps)
1831 beg end re e m n block off)
1832 ;; Check the different regular expressions
1833 (while (setq e (pop re-list))
1834 (setq m (car e) re (nth 1 e) n (nth 2 e)
1835 block (if (nth 3 e) "\n\n" ""))
1836 (setq off (if (member m '("$" "$1")) 1 0))
1837 (when (and (member m matchers) (not (equal m "begin")))
1838 (goto-char (point-min))
1839 (while (re-search-forward re nil t)
1840 (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
1841 (add-text-properties beg end '(org-protected t org-latex-math t))))))
1843 ;; Convert LaTeX to \LaTeX{}
1844 (goto-char (point-min))
1845 (let ((case-fold-search nil))
1846 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
1847 (org-if-unprotected
1848 (replace-match (org-export-latex-protect-string
1849 (concat (match-string 1) "\\LaTeX{}")) t t))))
1851 ;; Convert blockquotes
1852 (goto-char (point-min))
1853 (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
1854 (org-replace-match-keep-properties "\\begin{quote}" t t))
1855 (goto-char (point-min))
1856 (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
1857 (org-replace-match-keep-properties "\\end{quote}" t t))
1859 ;; Convert verse
1860 (goto-char (point-min))
1861 (while (search-forward "ORG-VERSE-START" nil t)
1862 (org-replace-match-keep-properties "\\begin{verse}" t t)
1863 (beginning-of-line 2)
1864 (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
1865 (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
1866 (goto-char (match-end 1))
1867 (org-replace-match-keep-properties
1868 (org-export-latex-protect-string
1869 (concat "\\hspace*{1cm}" (match-string 2))) t t)
1870 (beginning-of-line 1))
1871 (if (looking-at "[ \t]*$")
1872 (insert (org-export-latex-protect-string "\\vspace*{1em}"))
1873 (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
1874 (end-of-line 1)
1875 (insert "\\\\")))
1876 (beginning-of-line 2))
1877 (and (looking-at "[ \t]*ORG-VERSE-END.*")
1878 (org-replace-match-keep-properties "\\end{verse}" t t)))
1880 ;; Convert center
1881 (goto-char (point-min))
1882 (while (search-forward "ORG-CENTER-START" nil t)
1883 (org-replace-match-keep-properties "\\begin{center}" t t))
1884 (goto-char (point-min))
1885 (while (search-forward "ORG-CENTER-END" nil t)
1886 (org-replace-match-keep-properties "\\end{center}" t t))
1888 (run-hooks 'org-export-latex-after-blockquotes-hook)
1890 ;; Convert horizontal rules
1891 (goto-char (point-min))
1892 (while (re-search-forward "^----+.$" nil t)
1893 (org-if-unprotected
1894 (replace-match (org-export-latex-protect-string "\\hrule") t t)))
1896 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
1897 (goto-char (point-min))
1898 (let ((re (concat
1899 "\\\\[a-zA-Z]+"
1900 "\\(?:\\[[^][\n]*?\\]\\)*"
1901 "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}")))
1902 (while (re-search-forward re nil t)
1903 (unless (save-excursion (goto-char (match-beginning 0))
1904 (equal (char-after (point-at-bol)) ?#))
1905 (add-text-properties (match-beginning 0) (match-end 0)
1906 '(org-protected t)))))
1908 ;; Protect LaTeX entities
1909 (goto-char (point-min))
1910 (let (a)
1911 (while (re-search-forward org-latex-entities-regexp nil t)
1912 (if (setq a (assoc (match-string 0) org-latex-entities-exceptions))
1913 (replace-match (org-add-props (nth 1 a) nil 'org-protected t)
1914 t t)
1915 (add-text-properties (match-beginning 0) (match-end 0)
1916 '(org-protected t)))))
1918 ;; Replace radio links
1919 (goto-char (point-min))
1920 (while (re-search-forward
1921 (concat "<<<?" org-export-latex-all-targets-re
1922 ">>>?\\((INVISIBLE)\\)?") nil t)
1923 (org-if-unprotected-at (+ (match-beginning 0) 2)
1924 (replace-match
1925 (org-export-latex-protect-string
1926 (format "\\label{%s}%s" (save-match-data (org-solidify-link-text
1927 (match-string 1)))
1928 (if (match-string 2) "" (match-string 1)))) t t)))
1930 ;; Delete @<...> constructs
1931 ;; Thanks to Daniel Clemente for this regexp
1932 (goto-char (point-min))
1933 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
1934 (org-if-unprotected
1935 (replace-match "")))
1937 ;; When converting to LaTeX, replace footnotes
1938 ;; FIXME: don't protect footnotes from conversion
1939 (when (plist-get org-export-latex-options-plist :footnotes)
1940 (goto-char (point-min))
1941 (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t)
1942 (org-if-unprotected
1943 (when (and (save-match-data
1944 (save-excursion (beginning-of-line)
1945 (looking-at "[^:|#]")))
1946 (not (org-in-verbatim-emphasis)))
1947 (let ((foot-beg (match-beginning 0))
1948 (foot-end (match-end 0))
1949 (foot-prefix (match-string 0))
1950 footnote footnote-rpl)
1951 (save-excursion
1952 (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix))
1953 nil t))
1954 (replace-match (org-export-latex-protect-string
1955 (concat "$^{" (match-string 1) "}$")))
1956 (replace-match "")
1957 (let ((end (save-excursion
1958 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
1959 (match-beginning 0) (point-max)))))
1960 (setq footnote (concat (org-trim (buffer-substring (point) end))
1961 " ")) ; prevent last } being part of a link
1962 (delete-region (point) end))
1963 (goto-char foot-beg)
1964 (delete-region foot-beg foot-end)
1965 (unless (null footnote)
1966 (setq footnote-rpl (format "\\footnote{%s}" footnote))
1967 (add-text-properties 0 10 '(org-protected t) footnote-rpl)
1968 (add-text-properties (1- (length footnote-rpl))
1969 (length footnote-rpl)
1970 '(org-protected t) footnote-rpl)
1971 (insert footnote-rpl)))
1972 )))))
1974 ;; Remove footnote section tag for LaTeX
1975 (goto-char (point-min))
1976 (while (re-search-forward
1977 (concat "^" footnote-section-tag-regexp) nil t)
1978 (org-if-unprotected
1979 (replace-match "")))))
1981 ;;; List handling:
1983 (defun org-export-latex-lists ()
1984 "Convert plain text lists in current buffer into LaTeX lists."
1985 (goto-char (point-min))
1986 (while (re-search-forward org-list-beginning-re nil t)
1987 (org-if-unprotected
1988 (beginning-of-line)
1989 (insert (org-list-to-latex (org-list-parse-list t)
1990 org-export-latex-list-parameters))
1991 "\n")))
1993 (defconst org-latex-entities
1994 '("\\!"
1995 "\\'"
1996 "\\+"
1997 "\\,"
1998 "\\-"
1999 "\\:"
2000 "\\;"
2001 "\\<"
2002 "\\="
2003 "\\>"
2004 "\\Huge"
2005 "\\LARGE"
2006 "\\Large"
2007 "\\Styles"
2008 "\\\\"
2009 "\\`"
2010 "\\addcontentsline"
2011 "\\address"
2012 "\\addtocontents"
2013 "\\addtocounter"
2014 "\\addtolength"
2015 "\\addvspace"
2016 "\\alph"
2017 "\\appendix"
2018 "\\arabic"
2019 "\\author"
2020 "\\begin{array}"
2021 "\\begin{center}"
2022 "\\begin{description}"
2023 "\\begin{enumerate}"
2024 "\\begin{eqnarray}"
2025 "\\begin{equation}"
2026 "\\begin{figure}"
2027 "\\begin{flushleft}"
2028 "\\begin{flushright}"
2029 "\\begin{itemize}"
2030 "\\begin{list}"
2031 "\\begin{minipage}"
2032 "\\begin{picture}"
2033 "\\begin{quotation}"
2034 "\\begin{quote}"
2035 "\\begin{tabbing}"
2036 "\\begin{table}"
2037 "\\begin{tabular}"
2038 "\\begin{thebibliography}"
2039 "\\begin{theorem}"
2040 "\\begin{titlepage}"
2041 "\\begin{verbatim}"
2042 "\\begin{verse}"
2043 "\\bf"
2044 "\\bf"
2045 "\\bibitem"
2046 "\\bigskip"
2047 "\\cdots"
2048 "\\centering"
2049 "\\circle"
2050 "\\cite"
2051 "\\cleardoublepage"
2052 "\\clearpage"
2053 "\\cline"
2054 "\\closing"
2055 "\\dashbox"
2056 "\\date"
2057 "\\ddots"
2058 "\\dotfill"
2059 "\\em"
2060 "\\fbox"
2061 "\\flushbottom"
2062 "\\fnsymbol"
2063 "\\footnote"
2064 "\\footnotemark"
2065 "\\footnotesize"
2066 "\\footnotetext"
2067 "\\frac"
2068 "\\frame"
2069 "\\framebox"
2070 "\\hfill"
2071 "\\hline"
2072 "\\hrulespace"
2073 "\\hspace"
2074 "\\huge"
2075 "\\hyphenation"
2076 "\\include"
2077 "\\includeonly"
2078 "\\indent"
2079 "\\input"
2080 "\\it"
2081 "\\kill"
2082 "\\label"
2083 "\\large"
2084 "\\ldots"
2085 "\\line"
2086 "\\linebreak"
2087 "\\linethickness"
2088 "\\listoffigures"
2089 "\\listoftables"
2090 "\\location"
2091 "\\makebox"
2092 "\\maketitle"
2093 "\\mark"
2094 "\\mbox"
2095 "\\medskip"
2096 "\\multicolumn"
2097 "\\multiput"
2098 ("\\nbsp" "~")
2099 "\\newcommand"
2100 "\\newcounter"
2101 "\\newenvironment"
2102 "\\newfont"
2103 "\\newlength"
2104 "\\newline"
2105 "\\newpage"
2106 "\\newsavebox"
2107 "\\newtheorem"
2108 "\\nocite"
2109 "\\nofiles"
2110 "\\noindent"
2111 "\\nolinebreak"
2112 "\\nopagebreak"
2113 "\\normalsize"
2114 "\\onecolumn"
2115 "\\opening"
2116 "\\oval"
2117 "\\overbrace"
2118 "\\overline"
2119 "\\pagebreak"
2120 "\\pagenumbering"
2121 "\\pageref"
2122 "\\pagestyle"
2123 "\\par"
2124 "\\parbox"
2125 "\\put"
2126 "\\raggedbottom"
2127 "\\raggedleft"
2128 "\\raggedright"
2129 "\\raisebox"
2130 "\\ref"
2131 "\\rm"
2132 "\\roman"
2133 "\\rule"
2134 "\\savebox"
2135 "\\sc"
2136 "\\scriptsize"
2137 "\\setcounter"
2138 "\\setlength"
2139 "\\settowidth"
2140 "\\sf"
2141 "\\shortstack"
2142 "\\signature"
2143 "\\sl"
2144 "\\small"
2145 "\\smallskip"
2146 "\\sqrt"
2147 "\\tableofcontents"
2148 "\\telephone"
2149 "\\thanks"
2150 "\\thispagestyle"
2151 "\\tiny"
2152 "\\title"
2153 "\\tt"
2154 "\\twocolumn"
2155 "\\typein"
2156 "\\typeout"
2157 "\\underbrace"
2158 "\\underline"
2159 "\\usebox"
2160 "\\usecounter"
2161 "\\value"
2162 "\\vdots"
2163 "\\vector"
2164 "\\verb"
2165 "\\vfill"
2166 "\\vline"
2167 "\\vspace")
2168 "A list of LaTeX commands to be protected when performing conversion.")
2170 (defvar org-latex-entities-exceptions nil)
2172 (defconst org-latex-entities-regexp
2173 (let (names rest)
2174 (dolist (x org-latex-entities)
2175 (when (consp x)
2176 (add-to-list 'org-latex-entities-exceptions x)
2177 (setq x (car x)))
2178 (if (string-match "[a-zA-Z]$" x)
2179 (push x names)
2180 (push x rest)))
2181 (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
2182 "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
2184 (provide 'org-export-latex)
2185 (provide 'org-latex)
2187 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
2189 ;;; org-latex.el ends here