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