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