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