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