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