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