Improve LaTeX export of the words LaTeX and TeX
[org-mode.git] / lisp / org-latex.el
blob472d406b38bf4d0c78e49f5a497d71be06674f5c
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 ;; insert the title
1150 (format
1151 "\n\n\\title{%s}\n"
1152 ;; convert the title
1153 (org-export-latex-content
1154 title '(lists tables fixed-width keywords)))
1155 ;; insert author info
1156 (if (plist-get opt-plist :author-info)
1157 (format "\\author{%s}\n"
1158 (org-export-latex-fontify-headline (or author user-full-name)))
1159 (format "%%\\author{%s}\n"
1160 (or author user-full-name)))
1161 ;; insert the date
1162 (format "\\date{%s}\n"
1163 (format-time-string
1164 (or (plist-get opt-plist :date)
1165 org-export-latex-date-format)))
1166 ;; beginning of the document
1167 "\n\\begin{document}\n\n"
1168 ;; insert the title command
1169 (when (string-match "\\S-" title)
1170 (if (string-match "%s" org-export-latex-title-command)
1171 (format org-export-latex-title-command title)
1172 org-export-latex-title-command))
1173 "\n\n"
1174 ;; table of contents
1175 (when (and org-export-with-toc
1176 (plist-get opt-plist :section-numbers))
1177 (funcall org-export-latex-format-toc-function
1178 (cond ((numberp toc)
1179 (min toc (plist-get opt-plist :headline-levels)))
1180 (toc (plist-get opt-plist :headline-levels))))))))
1182 (defun org-export-latex-format-toc-default (depth)
1183 (when depth
1184 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1185 depth)))
1187 (defun org-export-latex-first-lines (opt-plist &optional beg end)
1188 "Export the first lines before first headline.
1189 If BEG is non-nil, it is the beginning of the region.
1190 If END is non-nil, it is the end of the region."
1191 (save-excursion
1192 (goto-char (or beg (point-min)))
1193 (let* ((pt (point))
1194 (end (if (re-search-forward "^\\*+ " end t)
1195 (goto-char (match-beginning 0))
1196 (goto-char (or end (point-max))))))
1197 (prog1
1198 (org-export-latex-content
1199 (org-export-preprocess-string
1200 (buffer-substring pt end)
1201 :for-LaTeX t
1202 :emph-multiline t
1203 :add-text nil
1204 :comments nil
1205 :skip-before-1st-heading nil
1206 :LaTeX-fragments nil
1207 :timestamps (plist-get opt-plist :timestamps)
1208 :footnotes (plist-get opt-plist :footnotes)))
1209 (org-unmodified
1210 (let ((inhibit-read-only t))
1211 (add-text-properties pt (max pt (1- end))
1212 '(:org-license-to-kill t))))))))
1214 (defvar org-export-latex-header-defs nil
1215 "The header definitions that might be used in the LaTeX body.")
1216 (defvar org-export-latex-header-defs-re nil
1217 "The header definitions that might be used in the LaTeX body.")
1219 (defun org-export-latex-content (content &optional exclude-list)
1220 "Convert CONTENT string to LaTeX.
1221 Don't perform conversions that are in EXCLUDE-LIST. Recognized
1222 conversion types are: quotation-marks, emphasis, sub-superscript,
1223 links, keywords, lists, tables, fixed-width"
1224 (with-temp-buffer
1225 (insert content)
1226 (unless (memq 'timestamps exclude-list)
1227 (org-export-latex-time-stamps))
1228 (unless (memq 'quotation-marks exclude-list)
1229 (org-export-latex-quotation-marks))
1230 (unless (memq 'emphasis exclude-list)
1231 (when (plist-get org-export-latex-options-plist :emphasize)
1232 (org-export-latex-fontify)))
1233 (unless (memq 'sub-superscript exclude-list)
1234 (org-export-latex-special-chars
1235 (plist-get org-export-latex-options-plist :sub-superscript)))
1236 (unless (memq 'links exclude-list)
1237 (org-export-latex-links))
1238 (unless (memq 'keywords exclude-list)
1239 (org-export-latex-keywords))
1240 (unless (memq 'lists exclude-list)
1241 (org-export-latex-lists))
1242 (unless (memq 'tables exclude-list)
1243 (org-export-latex-tables
1244 (plist-get org-export-latex-options-plist :tables)))
1245 (unless (memq 'fixed-width exclude-list)
1246 (org-export-latex-fixed-width
1247 (plist-get org-export-latex-options-plist :fixed-width)))
1248 ;; return string
1249 (buffer-substring (point-min) (point-max))))
1251 (defun org-export-latex-protect-string (s)
1252 "Add the org-protected property to string S."
1253 (add-text-properties 0 (length s) '(org-protected t) s) s)
1255 (defun org-export-latex-protect-char-in-string (char-list string)
1256 "Add org-protected text-property to char from CHAR-LIST in STRING."
1257 (with-temp-buffer
1258 (save-match-data
1259 (insert string)
1260 (goto-char (point-min))
1261 (while (re-search-forward (regexp-opt char-list) nil t)
1262 (add-text-properties (match-beginning 0)
1263 (match-end 0) '(org-protected t)))
1264 (buffer-string))))
1266 (defun org-export-latex-keywords-maybe (&optional remove-list)
1267 "Maybe remove keywords depending on rules in REMOVE-LIST."
1268 (goto-char (point-min))
1269 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
1270 (case-fold-search nil)
1271 (todo-markup org-export-latex-todo-keyword-markup)
1272 fmt)
1273 ;; convert TODO keywords
1274 (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
1275 (if (plist-get remove-list :todo)
1276 (replace-match "")
1277 (setq fmt (cond
1278 ((stringp todo-markup) todo-markup)
1279 ((and (consp todo-markup) (stringp (car todo-markup)))
1280 (if (member (match-string 1) org-export-latex-done-keywords)
1281 (cdr todo-markup) (car todo-markup)))
1282 (t (cdr (or (assoc (match-string 1) todo-markup)
1283 (car todo-markup))))))
1284 (replace-match (format fmt (match-string 1)) t t)))
1285 ;; convert priority string
1286 (when (re-search-forward "\\[\\\\#.\\]" nil t)
1287 (if (plist-get remove-list :priority)
1288 (replace-match "")
1289 (replace-match (format "\\textbf{%s}" (match-string 0)) t t)))
1290 ;; convert tags
1291 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t)
1292 (if (or (not org-export-with-tags)
1293 (plist-get remove-list :tags))
1294 (replace-match "")
1295 (replace-match
1296 (org-export-latex-protect-string
1297 (format "\\textbf{%s}"
1298 (save-match-data
1299 (replace-regexp-in-string
1300 "_" "\\\\_" (match-string 0)))))
1301 t t)))))
1303 (defun org-export-latex-fontify-headline (string)
1304 "Fontify special words in STRING."
1305 (with-temp-buffer
1306 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
1307 ;; the beginning of the buffer - inserting "\n" is safe here though.
1308 (insert "\n" string)
1309 (goto-char (point-min))
1310 (let ((re (concat "\\\\[a-zA-Z]+\\(?:"
1311 "\\[.*\\]"
1312 "\\)?"
1313 (org-create-multibrace-regexp "{" "}" 3))))
1314 (while (re-search-forward re nil t)
1315 (unless (save-excursion (goto-char (match-beginning 0))
1316 (equal (char-after (point-at-bol)) ?#))
1317 (add-text-properties (match-beginning 0) (match-end 0)
1318 '(org-protected t)))))
1319 (when (plist-get org-export-latex-options-plist :emphasize)
1320 (org-export-latex-fontify))
1321 (org-export-latex-keywords-maybe)
1322 (org-export-latex-special-chars
1323 (plist-get org-export-latex-options-plist :sub-superscript))
1324 (org-export-latex-links)
1325 (org-trim (buffer-string))))
1327 (defun org-export-latex-time-stamps ()
1328 "Format time stamps."
1329 (goto-char (point-min))
1330 (let ((org-display-custom-times org-export-latex-display-custom-times))
1331 (while (re-search-forward org-ts-regexp-both nil t)
1332 (org-if-unprotected-at (1- (point))
1333 (replace-match
1334 (org-export-latex-protect-string
1335 (format org-export-latex-timestamp-markup
1336 (substring (org-translate-time (match-string 0)) 1 -1)))
1337 t t)))))
1339 (defun org-export-latex-quotation-marks ()
1340 "Export quotation marks depending on language conventions."
1341 (let* ((lang (plist-get org-export-latex-options-plist :language))
1342 (quote-rpl (if (equal lang "fr")
1343 '(("\\(\\s-\\)\"" "«~")
1344 ("\\(\\S-\\)\"" "~»")
1345 ("\\(\\s-\\)'" "`"))
1346 '(("\\(\\s-\\|[[(]\\)\"" "``")
1347 ("\\(\\S-\\)\"" "''")
1348 ("\\(\\s-\\|(\\)'" "`")))))
1349 (mapc (lambda(l) (goto-char (point-min))
1350 (while (re-search-forward (car l) nil t)
1351 (let ((rpl (concat (match-string 1)
1352 (org-export-latex-protect-string
1353 (copy-sequence (cadr l))))))
1354 (org-if-unprotected-1
1355 (replace-match rpl t t))))) quote-rpl)))
1357 (defun org-export-latex-special-chars (sub-superscript)
1358 "Export special characters to LaTeX.
1359 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
1360 See the `org-export-latex.el' code for a complete conversion table."
1361 (goto-char (point-min))
1362 (mapc (lambda(c)
1363 (goto-char (point-min))
1364 (while (re-search-forward c nil t)
1365 ;; Put the point where to check for org-protected
1366 (unless (get-text-property (match-beginning 2) 'org-protected)
1367 (cond ((member (match-string 2) '("\\$" "$"))
1368 (if (equal (match-string 2) "\\$")
1370 (replace-match "\\$" t t)))
1371 ((member (match-string 2) '("&" "%" "#"))
1372 (if (equal (match-string 1) "\\")
1373 (replace-match (match-string 2) t t)
1374 (replace-match (concat (match-string 1) "\\"
1375 (match-string 2)) t t)))
1376 ((equal (match-string 2) "...")
1377 (replace-match
1378 (concat (match-string 1)
1379 (org-export-latex-protect-string "\\ldots{}")) t t))
1380 ((equal (match-string 2) "~")
1381 (cond ((equal (match-string 1) "\\") nil)
1382 ((eq 'org-link (get-text-property 0 'face (match-string 2)))
1383 (replace-match (concat (match-string 1) "\\~") t t))
1384 (t (replace-match
1385 (org-export-latex-protect-string
1386 (concat (match-string 1) "\\~{}")) t t))))
1387 ((member (match-string 2) '("{" "}"))
1388 (unless (save-match-data (org-inside-latex-math-p))
1389 (if (equal (match-string 1) "\\")
1390 (replace-match (match-string 2) t t)
1391 (replace-match (concat (match-string 1) "\\"
1392 (match-string 2)) t t)))))
1393 (unless (save-match-data (org-inside-latex-math-p))
1394 (cond ((equal (match-string 2) "\\")
1395 (replace-match (or (save-match-data
1396 (org-export-latex-treat-backslash-char
1397 (match-string 1)
1398 (or (match-string 3) "")))
1399 "") t t))
1400 ((member (match-string 2) '("_" "^"))
1401 (replace-match (or (save-match-data
1402 (org-export-latex-treat-sub-super-char
1403 sub-superscript
1404 (match-string 2)
1405 (match-string 1)
1406 (match-string 3))) "") t t)
1407 (backward-char 1)))))))
1408 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
1409 "\\(\\(\\\\?\\$\\)\\)"
1410 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
1411 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
1412 "\\(.\\|^\\)\\(&\\)"
1413 "\\(.\\|^\\)\\(#\\)"
1414 "\\(.\\|^\\)\\(%\\)"
1415 "\\(.\\|^\\)\\({\\)"
1416 "\\(.\\|^\\)\\(}\\)"
1417 "\\(.\\|^\\)\\(~\\)"
1418 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
1419 ;; (?\< . "\\textless{}")
1420 ;; (?\> . "\\textgreater{}")
1423 (defun org-inside-latex-math-p ()
1424 (get-text-property (point) 'org-latex-math))
1426 (defun org-export-latex-treat-sub-super-char
1427 (subsup char string-before string-after)
1428 "Convert the \"_\" and \"^\" characters to LaTeX.
1429 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
1430 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
1431 (cond ((equal string-before "\\")
1432 (concat string-before char string-after))
1433 ((and (string-match "\\S-+" string-after))
1434 ;; this is part of a math formula
1435 (cond ((eq 'org-link (get-text-property 0 'face char))
1436 (concat string-before "\\" char string-after))
1437 ((save-match-data (org-inside-latex-math-p))
1438 (if subsup
1439 (cond ((eq 1 (length string-after))
1440 (concat string-before char string-after))
1441 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
1442 (format "%s%s{%s}" string-before char
1443 (match-string 1 string-after))))))
1444 ((and (> (length string-after) 1)
1445 (or (eq subsup t)
1446 (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
1447 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
1448 (org-export-latex-protect-string
1449 (format "%s$%s{%s}$" string-before char
1450 (if (and (> (match-end 1) (1+ (match-beginning 1)))
1451 (not (equal (substring string-after 0 2) "{\\")))
1452 (concat "\\mathrm{" (match-string 1 string-after) "}")
1453 (match-string 1 string-after)))))
1454 ((eq subsup t) (concat string-before "$" char string-after "$"))
1455 (t (org-export-latex-protect-string
1456 (concat string-before "\\" char "{}" string-after)))))
1457 (t (org-export-latex-protect-string
1458 (concat string-before "\\" char "{}" string-after)))))
1460 (defun org-export-latex-treat-backslash-char (string-before string-after)
1461 "Convert the \"$\" special character to LaTeX.
1462 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
1463 (cond ((member (list string-after) org-html-entities)
1464 ;; backslash is part of a special entity (like "\alpha")
1465 (concat string-before "$\\"
1466 (or (cdar (member (list string-after) org-html-entities))
1467 string-after) "$"))
1468 ((and (not (string-match "^[ \n\t]" string-after))
1469 (not (string-match "[ \t]\\'\\|^" string-before)))
1470 ;; backslash is inside a word
1471 (concat string-before
1472 (org-export-latex-protect-string
1473 (concat "\\textbackslash{}" string-after))))
1474 ((not (or (equal string-after "")
1475 (string-match "^[ \t\n]" string-after)))
1476 ;; backslash might escape a character (like \#) or a user TeX
1477 ;; macro (like \setcounter)
1478 (concat string-before
1479 (org-export-latex-protect-string (concat "\\" string-after))))
1480 ((and (string-match "^[ \t\n]" string-after)
1481 (string-match "[ \t\n]\\'" string-before))
1482 ;; backslash is alone, convert it to $\backslash$
1483 (org-export-latex-protect-string
1484 (concat string-before "\\textbackslash{}" string-after)))
1485 (t (org-export-latex-protect-string
1486 (concat string-before "\\textbackslash{}" string-after)))))
1488 (defun org-export-latex-keywords ()
1489 "Convert special keywords to LaTeX."
1490 (goto-char (point-min))
1491 (while (re-search-forward org-export-latex-special-keyword-regexp nil t)
1492 (replace-match (format org-export-latex-timestamp-keyword-markup
1493 (match-string 0)) t t)
1494 (save-excursion
1495 (beginning-of-line 1)
1496 (unless (looking-at ".*\\\\newline[ \t]*$")
1497 (end-of-line 1)
1498 (insert "\\newline")))))
1500 (defun org-export-latex-fixed-width (opt)
1501 "When OPT is non-nil convert fixed-width sections to LaTeX."
1502 (goto-char (point-min))
1503 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t)
1504 (if opt
1505 (progn (goto-char (match-beginning 0))
1506 (insert "\\begin{verbatim}\n")
1507 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1508 (replace-match (concat (match-string 1)
1509 (match-string 2)) t t)
1510 (forward-line))
1511 (insert "\\end{verbatim}\n\n"))
1512 (progn (goto-char (match-beginning 0))
1513 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1514 (replace-match (concat "%" (match-string 1)
1515 (match-string 2)) t t)
1516 (forward-line))))))
1519 (defvar org-table-last-alignment) ; defined in org-table.el
1520 (defvar org-table-last-column-widths) ; defined in org-table.el
1521 (declare-function orgtbl-to-latex "org-table" (table params) t)
1522 (defun org-export-latex-tables (insert)
1523 "Convert tables to LaTeX and INSERT it."
1524 (goto-char (point-min))
1525 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
1526 (org-if-unprotected-at (1- (point))
1527 (org-table-align)
1528 (let* ((beg (org-table-begin))
1529 (end (org-table-end))
1530 (raw-table (buffer-substring beg end))
1531 (org-table-last-alignment (copy-sequence org-table-last-alignment))
1532 (org-table-last-column-widths (copy-sequence
1533 org-table-last-column-widths))
1534 fnum fields line lines olines gr colgropen line-fmt align
1535 caption label attr floatp longtblp)
1536 (if org-export-latex-tables-verbatim
1537 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1538 "\\end{verbatim}\n")))
1539 (apply 'delete-region (list beg end))
1540 (insert (org-export-latex-protect-string tbl)))
1541 (progn
1542 (setq caption (org-find-text-property-in-string
1543 'org-caption raw-table)
1544 attr (org-find-text-property-in-string
1545 'org-attributes raw-table)
1546 label (org-find-text-property-in-string
1547 'org-label raw-table)
1548 longtblp (and attr (stringp attr)
1549 (string-match "\\<longtable\\>" attr))
1550 align (and attr (stringp attr)
1551 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
1552 (match-string 1 attr))
1553 floatp (or caption label))
1554 (setq lines (org-split-string raw-table "\n"))
1555 (apply 'delete-region (list beg end))
1556 (when org-export-table-remove-special-lines
1557 (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
1558 (when org-table-clean-did-remove-column
1559 (pop org-table-last-alignment)
1560 (pop org-table-last-column-widths))
1561 ;; make a formatting string to reflect alignment
1562 (setq olines lines)
1563 (while (and (not line-fmt) (setq line (pop olines)))
1564 (unless (string-match "^[ \t]*|-" line)
1565 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1566 (setq fnum (make-vector (length fields) 0))
1567 (setq line-fmt
1568 (mapconcat
1569 (lambda (x)
1570 (setq gr (pop org-table-colgroup-info))
1571 (format "%s%%s%s"
1572 (cond ((eq gr :start)
1573 (prog1 (if colgropen "|" "|")
1574 (setq colgropen t)))
1575 ((eq gr :startend)
1576 (prog1 (if colgropen "|" "|")
1577 (setq colgropen nil)))
1578 (t ""))
1579 (if (memq gr '(:end :startend))
1580 (progn (setq colgropen nil) "|")
1581 "")))
1582 fnum ""))))
1583 ;; fix double || in line-fmt
1584 (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
1585 ;; maybe remove the first and last "|"
1586 (when (and (not org-export-latex-tables-column-borders)
1587 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
1588 (setq line-fmt (match-string 2 line-fmt)))
1589 ;; format alignment
1590 (unless align
1591 (setq align (apply 'format
1592 (cons line-fmt
1593 (mapcar (lambda (x) (if x "r" "l"))
1594 org-table-last-alignment)))))
1595 ;; prepare the table to send to orgtbl-to-latex
1596 (setq lines
1597 (mapcar
1598 (lambda(elem)
1599 (or (and (string-match "[ \t]*|-+" elem) 'hline)
1600 (org-split-string (org-trim elem) "|")))
1601 lines))
1602 (when insert
1603 (insert (org-export-latex-protect-string
1604 (concat
1605 (if longtblp
1606 (concat "\\begin{longtable}{" align "}\n")
1607 (if floatp "\\begin{table}[htb]\n"))
1608 (if floatp
1609 (format
1610 "\\caption{%s%s}"
1611 (if label (concat "\\\label{" label "}") "")
1612 (or caption "")))
1613 (if (and longtblp caption) "\\\\\n" "\n")
1614 (if (and org-export-latex-tables-centered (not longtblp))
1615 "\\begin{center}\n")
1616 (if (not longtblp) (concat "\\begin{tabular}{" align "}\n"))
1617 (orgtbl-to-latex
1618 lines
1619 `(:tstart nil :tend nil
1620 :hlend ,(if longtblp
1621 (format "\\\\
1622 \\hline
1623 \\endhead
1624 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
1625 \\endfoot
1626 \\endlastfoot" (length org-table-last-alignment))
1627 nil)))
1628 (if (not longtblp) (concat "\n\\end{tabular}"))
1629 (if longtblp "\n" (if org-export-latex-tables-centered
1630 "\n\\end{center}\n" "\n"))
1631 (if longtblp
1632 "\\end{longtable}"
1633 (if floatp "\\end{table}"))))
1634 "\n\n"))))))))
1636 (defun org-export-latex-fontify ()
1637 "Convert fontification to LaTeX."
1638 (goto-char (point-min))
1639 (while (re-search-forward org-emph-re nil t)
1640 ;; The match goes one char after the *string*, except at the end of a line
1641 (let ((emph (assoc (match-string 3)
1642 org-export-latex-emphasis-alist))
1643 (beg (match-beginning 0))
1644 (end (match-end 0))
1645 rpl s)
1646 (unless emph
1647 (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
1648 (match-string 3)))
1649 (unless (or (and (get-text-property (- (point) 2) 'org-protected)
1650 (not (get-text-property
1651 (- (point) 2) 'org-verbatim-emph)))
1652 (save-excursion
1653 (goto-char (match-beginning 1))
1654 (save-match-data
1655 (and (org-at-table-p)
1656 (string-match
1657 "[|\n]" (buffer-substring beg end))))))
1658 (setq s (match-string 4))
1659 (setq rpl (concat (match-string 1)
1660 (org-export-latex-emph-format (cadr emph)
1661 (match-string 4))
1662 (match-string 5)))
1663 (if (caddr emph)
1664 (setq rpl (org-export-latex-protect-string rpl))
1665 (save-match-data
1666 (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl)
1667 (progn
1668 (add-text-properties (match-beginning 1) (match-end 1)
1669 '(org-protected t) rpl)
1670 (add-text-properties (match-beginning 3) (match-end 3)
1671 '(org-protected t) rpl)))))
1672 (replace-match rpl t t)))
1673 (backward-char)))
1675 (defvar org-export-latex-use-verb nil)
1676 (defun org-export-latex-emph-format (format string)
1677 "Format an emphasis string and handle the \\verb special case."
1678 (when (equal format "\\verb")
1679 (save-match-data
1680 (if org-export-latex-use-verb
1681 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1682 (catch 'exit
1683 (loop for i from 0 to (1- (length ll)) do
1684 (if (not (string-match (regexp-quote (substring ll i (1+ i)))
1685 string))
1686 (progn
1687 (setq format (concat "\\verb" (substring ll i (1+ i))
1688 "%s" (substring ll i (1+ i))))
1689 (throw 'exit nil))))))
1690 (let ((start 0)
1691 (trans '(("\\" . "\\textbackslash{}")
1692 ("~" . "\\textasciitilde{}")
1693 ("^" . "\\textasciicircum{}")))
1694 (rtn "") char)
1695 (while (string-match "[\\{}$%&_#~^]" string)
1696 (setq char (match-string 0 string))
1697 (if (> (match-beginning 0) 0)
1698 (setq rtn (concat rtn (substring string
1699 0 (match-beginning 0)))))
1700 (setq string (substring string (1+ (match-beginning 0))))
1701 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1702 rtn (concat rtn char)))
1703 (setq string (concat rtn string) format "\\texttt{%s}")))))
1704 (format format string))
1706 (defun org-export-latex-links ()
1707 ;; Make sure to use the LaTeX hyperref and graphicx package
1708 ;; or send some warnings.
1709 "Convert links to LaTeX."
1710 (goto-char (point-min))
1711 (while (re-search-forward org-bracket-link-analytic-regexp++ nil t)
1712 (org-if-unprotected-1
1713 (goto-char (match-beginning 0))
1714 (let* ((re-radio org-export-latex-all-targets-re)
1715 (remove (list (match-beginning 0) (match-end 0)))
1716 (raw-path (org-extract-attributes (match-string 3)))
1717 (full-raw-path (concat (match-string 1) raw-path))
1718 (desc (match-string 5))
1719 (type (or (match-string 2)
1720 (if (or (file-name-absolute-p raw-path)
1721 (string-match "^\\.\\.?/" raw-path))
1722 "file")))
1723 (coderefp (equal type "coderef"))
1724 (caption (org-find-text-property-in-string 'org-caption raw-path))
1725 (attr (or (org-find-text-property-in-string 'org-attributes raw-path)
1726 (plist-get org-export-latex-options-plist :latex-image-options)))
1727 (label (org-find-text-property-in-string 'org-label raw-path))
1728 imgp radiop
1729 ;; define the path of the link
1730 (path (cond
1731 ((member type '("coderef"))
1732 raw-path)
1733 ((member type '("http" "https" "ftp"))
1734 (concat type ":" raw-path))
1735 ((and re-radio (string-match re-radio raw-path))
1736 (setq radiop t))
1737 ((equal type "mailto")
1738 (concat type ":" raw-path))
1739 ((equal type "file")
1740 (if (and (org-file-image-p
1741 (expand-file-name
1742 raw-path)
1743 org-export-latex-inline-image-extensions)
1744 (or (get-text-property 0 'org-no-description
1745 raw-path)
1746 (equal desc full-raw-path)))
1747 (setq imgp t)
1748 (progn (when (string-match "\\(.+\\)::.+" raw-path)
1749 (setq raw-path (match-string 1 raw-path)))
1750 (if (file-exists-p raw-path)
1751 (concat type "://" (expand-file-name raw-path))
1752 (concat type "://" (org-export-directory
1753 :LaTeX org-export-latex-options-plist)
1754 raw-path))))))))
1755 ;; process with link inserting
1756 (apply 'delete-region remove)
1757 (cond ((and imgp
1758 (plist-get org-export-latex-options-plist :inline-images))
1759 ;; OK, we need to inline an image
1760 (insert
1761 (org-export-latex-format-image raw-path caption label attr)))
1762 (coderefp
1763 (insert (format
1764 (org-export-get-coderef-format path desc)
1765 (cdr (assoc path org-export-code-refs)))))
1766 (radiop (insert (format "\\hyperref[%s]{%s}"
1767 (org-solidify-link-text raw-path) desc)))
1768 ((not type)
1769 (insert (format "\\hyperref[%s]{%s}"
1770 (org-remove-initial-hash
1771 (org-solidify-link-text raw-path))
1772 desc)))
1773 (path
1774 (when (org-at-table-p)
1775 ;; There is a strange problem when we have a link in a table,
1776 ;; ampersands then cause a problem. I think this must be
1777 ;; a LaTeX issue, but we here implement a work-around anyway.
1778 (setq path (org-export-latex-protect-amp path)
1779 desc (org-export-latex-protect-amp desc)))
1780 (insert (format org-export-latex-hyperref-format path desc)))
1781 (t (insert "\\texttt{" desc "}")))))))
1784 (defun org-export-latex-format-image (path caption label attr)
1785 "Format the image element, depending on user settings."
1786 (let (ind floatp wrapp placement figenv)
1787 (setq floatp (or caption label))
1788 (setq ind (org-get-text-property-any 0 'original-indentation path))
1789 (when (and attr (stringp attr))
1790 (if (string-match "[ \t]*\\<wrap\\>" attr)
1791 (setq wrapp t floatp nil attr (replace-match "" t t attr)))
1792 (if (string-match "[ \t]*\\<float\\>" attr)
1793 (setq wrapp nil floatp t attr (replace-match "" t t attr))))
1795 (setq placement
1796 (cond
1797 (wrapp "{l}{0.5\\textwidth}")
1798 (floatp "[htb]")
1799 (t "")))
1801 (when (and attr (stringp attr)
1802 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr))
1803 (setq placement (match-string 1 attr)
1804 attr (replace-match "" t t attr)))
1805 (setq attr (and attr (org-trim attr)))
1806 (when (or (not attr) (= (length attr) 0))
1807 (setq attr (cond (floatp "width=0.7\\textwidth")
1808 (wrapp "width=0.48\\textwidth")
1809 (t attr))))
1810 (setq figenv
1811 (cond
1812 (wrapp "\\begin{wrapfigure}%placement
1813 \\centering
1814 \\includegraphics[%attr]{%path}
1815 \\caption{%labelcmd%caption}
1816 \\end{wrapfigure}")
1817 (floatp "\\begin{figure}%placement
1818 \\centering
1819 \\includegraphics[%attr]{%path}
1820 \\caption{%labelcmd%caption}
1821 \\end{figure}")
1822 (t "\\includegraphics[%attr]{%path}")))
1825 (setq figenv (mapconcat 'identity (split-string figenv "\n")
1826 (save-excursion (beginning-of-line 1)
1827 (looking-at "[ \t]*")
1828 (concat "\n" (match-string 0)))))
1830 (if (and (not label) (not caption)
1831 (string-match "^\\\\caption{.*\n" figenv))
1832 (setq figenv (replace-match "" t t figenv)))
1833 (org-add-props
1834 (org-fill-template
1835 figenv
1836 (list (cons "path"
1837 (if (file-name-absolute-p path)
1838 (expand-file-name path)
1839 path))
1840 (cons "attr" attr)
1841 (cons "labelcmd" (if label (format "\\label{%s}"
1842 label)""))
1843 (cons "caption" (or caption ""))
1844 (cons "placement" (or placement ""))))
1845 nil 'original-indentation ind)))
1847 (defun org-export-latex-protect-amp (s)
1848 (while (string-match "\\([^\\\\]\\)\\(&\\)" s)
1849 (setq s (replace-match (concat (match-string 1 s) "\\" (match-string 2 s))
1850 t t s)))
1853 (defun org-remove-initial-hash (s)
1854 (if (string-match "\\`#" s)
1855 (substring s 1)
1857 (defvar org-latex-entities) ; defined below
1858 (defvar org-latex-entities-regexp) ; defined below
1859 (defvar org-latex-entities-exceptions) ; defined below
1861 (defun org-export-latex-preprocess (parameters)
1862 "Clean stuff in the LaTeX export."
1863 ;; Preserve line breaks
1864 (goto-char (point-min))
1865 (while (re-search-forward "\\\\\\\\" nil t)
1866 (add-text-properties (match-beginning 0) (match-end 0)
1867 '(org-protected t)))
1869 ;; Preserve latex environments
1870 (goto-char (point-min))
1871 (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t)
1872 (let* ((start (progn (beginning-of-line) (point)))
1873 (end (and (re-search-forward
1874 (concat "^[ \t]*\\\\end{"
1875 (regexp-quote (match-string 1))
1876 "}") nil t)
1877 (point-at-eol))))
1878 (if end
1879 (add-text-properties start end '(org-protected t))
1880 (goto-char (point-at-eol)))))
1882 ;; Preserve math snippets
1884 (let* ((matchers (plist-get org-format-latex-options :matchers))
1885 (re-list org-latex-regexps)
1886 beg end re e m n block off)
1887 ;; Check the different regular expressions
1888 (while (setq e (pop re-list))
1889 (setq m (car e) re (nth 1 e) n (nth 2 e)
1890 block (if (nth 3 e) "\n\n" ""))
1891 (setq off (if (member m '("$" "$1")) 1 0))
1892 (when (and (member m matchers) (not (equal m "begin")))
1893 (goto-char (point-min))
1894 (while (re-search-forward re nil t)
1895 (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
1896 (add-text-properties beg end '(org-protected t org-latex-math t))))))
1898 ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{}
1899 (goto-char (point-min))
1900 (let ((case-fold-search nil))
1901 (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t)
1902 (unless (eq (char-before (match-beginning 1)) ?\\)
1903 (org-if-unprotected-1
1904 (replace-match (org-export-latex-protect-string
1905 (concat "\\" (match-string 1)
1906 "{}")) t t)))))
1908 ;; Convert blockquotes
1909 (goto-char (point-min))
1910 (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
1911 (org-replace-match-keep-properties "\\begin{quote}" t t))
1912 (goto-char (point-min))
1913 (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
1914 (org-replace-match-keep-properties "\\end{quote}" t t))
1916 ;; Convert verse
1917 (goto-char (point-min))
1918 (while (search-forward "ORG-VERSE-START" nil t)
1919 (org-replace-match-keep-properties "\\begin{verse}" t t)
1920 (beginning-of-line 2)
1921 (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
1922 (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
1923 (goto-char (match-end 1))
1924 (org-replace-match-keep-properties
1925 (org-export-latex-protect-string
1926 (concat "\\hspace*{1cm}" (match-string 2))) t t)
1927 (beginning-of-line 1))
1928 (if (looking-at "[ \t]*$")
1929 (insert (org-export-latex-protect-string "\\vspace*{1em}"))
1930 (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
1931 (end-of-line 1)
1932 (insert "\\\\")))
1933 (beginning-of-line 2))
1934 (and (looking-at "[ \t]*ORG-VERSE-END.*")
1935 (org-replace-match-keep-properties "\\end{verse}" t t)))
1937 ;; Convert center
1938 (goto-char (point-min))
1939 (while (search-forward "ORG-CENTER-START" nil t)
1940 (org-replace-match-keep-properties "\\begin{center}" t t))
1941 (goto-char (point-min))
1942 (while (search-forward "ORG-CENTER-END" nil t)
1943 (org-replace-match-keep-properties "\\end{center}" t t))
1945 (run-hooks 'org-export-latex-after-blockquotes-hook)
1947 ;; Convert horizontal rules
1948 (goto-char (point-min))
1949 (while (re-search-forward "^----+.$" nil t)
1950 (org-if-unprotected
1951 (replace-match (org-export-latex-protect-string "\\hrule") t t)))
1953 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
1954 (goto-char (point-min))
1955 (let ((re (concat
1956 "\\\\[a-zA-Z]+"
1957 "\\(?:\\[[^][\n]*?\\]\\)*"
1958 "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}")))
1959 (while (re-search-forward re nil t)
1960 (unless (save-excursion (goto-char (match-beginning 0))
1961 (equal (char-after (point-at-bol)) ?#))
1962 (add-text-properties (match-beginning 0) (match-end 0)
1963 '(org-protected t)))))
1965 ;; Protect LaTeX entities
1966 (goto-char (point-min))
1967 (let (a)
1968 (while (re-search-forward org-latex-entities-regexp nil t)
1969 (if (setq a (assoc (match-string 0) org-latex-entities-exceptions))
1970 (replace-match (org-add-props (nth 1 a) nil 'org-protected t)
1971 t t)
1972 (add-text-properties (match-beginning 0) (match-end 0)
1973 '(org-protected t)))))
1975 ;; Replace radio links
1976 (goto-char (point-min))
1977 (while (re-search-forward
1978 (concat "<<<?" org-export-latex-all-targets-re
1979 ">>>?\\((INVISIBLE)\\)?") nil t)
1980 (org-if-unprotected-at (+ (match-beginning 0) 2)
1981 (replace-match
1982 (concat
1983 (org-export-latex-protect-string
1984 (format "\\label{%s}" (save-match-data (org-solidify-link-text
1985 (match-string 1)))))
1986 (if (match-string 2) "" (match-string 1)))
1987 t t)))
1989 ;; Delete @<...> constructs
1990 ;; Thanks to Daniel Clemente for this regexp
1991 (goto-char (point-min))
1992 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
1993 (org-if-unprotected
1994 (replace-match "")))
1996 ;; When converting to LaTeX, replace footnotes
1997 ;; FIXME: don't protect footnotes from conversion
1998 (when (plist-get org-export-latex-options-plist :footnotes)
1999 (goto-char (point-min))
2000 (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t)
2001 (org-if-unprotected
2002 (when (and (save-match-data
2003 (save-excursion (beginning-of-line)
2004 (looking-at "[^:|#]")))
2005 (not (org-in-verbatim-emphasis)))
2006 (let ((foot-beg (match-beginning 0))
2007 (foot-end (match-end 0))
2008 (foot-prefix (match-string 0))
2009 footnote footnote-rpl)
2010 (save-excursion
2011 (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix))
2012 nil t))
2013 (replace-match (org-export-latex-protect-string
2014 (concat "$^{" (match-string 1) "}$")))
2015 (replace-match "")
2016 (let ((end (save-excursion
2017 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
2018 (match-beginning 0) (point-max)))))
2019 (setq footnote (concat (org-trim (buffer-substring (point) end))
2020 " ")) ; prevent last } being part of a link
2021 (delete-region (point) end))
2022 (goto-char foot-beg)
2023 (delete-region foot-beg foot-end)
2024 (unless (null footnote)
2025 (setq footnote-rpl (format "\\footnote{%s}" footnote))
2026 (add-text-properties 0 10 '(org-protected t) footnote-rpl)
2027 (add-text-properties (1- (length footnote-rpl))
2028 (length footnote-rpl)
2029 '(org-protected t) footnote-rpl)
2030 (insert footnote-rpl)))
2031 )))))
2033 ;; Remove footnote section tag for LaTeX
2034 (goto-char (point-min))
2035 (while (re-search-forward
2036 (concat "^" footnote-section-tag-regexp) nil t)
2037 (org-if-unprotected
2038 (replace-match "")))))
2040 (defun org-export-latex-fix-inputenc ()
2041 "Set the codingsystem in inputenc to what the buffer is."
2042 (let* ((cs buffer-file-coding-system)
2043 (opt (latexenc-coding-system-to-inputenc cs)))
2044 (when opt
2045 ;; Translate if that is requested
2046 (setq opt (or (cdr (assoc opt org-export-latex-inputenc-alist)) opt))
2047 ;; find the \usepackage statement and replace the option
2048 (goto-char (point-min))
2049 (while (re-search-forward "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
2050 nil t)
2051 (goto-char (match-beginning 1))
2052 (delete-region (match-beginning 1) (match-end 1))
2053 (insert opt))
2054 (save-buffer))))
2056 ;;; List handling:
2058 (defun org-export-latex-lists ()
2059 "Convert plain text lists in current buffer into LaTeX lists."
2060 (goto-char (point-min))
2061 (while (re-search-forward org-list-beginning-re nil t)
2062 (org-if-unprotected
2063 (beginning-of-line)
2064 (insert (org-list-to-latex (org-list-parse-list t)
2065 org-export-latex-list-parameters))
2066 "\n")))
2068 (defconst org-latex-entities
2069 '("\\!"
2070 "\\'"
2071 "\\+"
2072 "\\,"
2073 "\\-"
2074 "\\:"
2075 "\\;"
2076 "\\<"
2077 "\\="
2078 "\\>"
2079 "\\Huge"
2080 "\\LARGE"
2081 "\\Large"
2082 "\\Styles"
2083 "\\\\"
2084 "\\`"
2085 "\\addcontentsline"
2086 "\\address"
2087 "\\addtocontents"
2088 "\\addtocounter"
2089 "\\addtolength"
2090 "\\addvspace"
2091 "\\alph"
2092 "\\appendix"
2093 "\\arabic"
2094 "\\author"
2095 "\\begin{array}"
2096 "\\begin{center}"
2097 "\\begin{description}"
2098 "\\begin{enumerate}"
2099 "\\begin{eqnarray}"
2100 "\\begin{equation}"
2101 "\\begin{figure}"
2102 "\\begin{flushleft}"
2103 "\\begin{flushright}"
2104 "\\begin{itemize}"
2105 "\\begin{list}"
2106 "\\begin{minipage}"
2107 "\\begin{picture}"
2108 "\\begin{quotation}"
2109 "\\begin{quote}"
2110 "\\begin{tabbing}"
2111 "\\begin{table}"
2112 "\\begin{tabular}"
2113 "\\begin{thebibliography}"
2114 "\\begin{theorem}"
2115 "\\begin{titlepage}"
2116 "\\begin{verbatim}"
2117 "\\begin{verse}"
2118 "\\bf"
2119 "\\bf"
2120 "\\bibitem"
2121 "\\bigskip"
2122 "\\cdots"
2123 "\\centering"
2124 "\\circle"
2125 "\\cite"
2126 "\\cleardoublepage"
2127 "\\clearpage"
2128 "\\cline"
2129 "\\closing"
2130 "\\dashbox"
2131 "\\date"
2132 "\\ddots"
2133 "\\dotfill"
2134 "\\em"
2135 "\\fbox"
2136 "\\flushbottom"
2137 "\\fnsymbol"
2138 "\\footnote"
2139 "\\footnotemark"
2140 "\\footnotesize"
2141 "\\footnotetext"
2142 "\\frac"
2143 "\\frame"
2144 "\\framebox"
2145 "\\hfill"
2146 "\\hline"
2147 "\\hrulespace"
2148 "\\hspace"
2149 "\\huge"
2150 "\\hyphenation"
2151 "\\include"
2152 "\\includeonly"
2153 "\\indent"
2154 "\\input"
2155 "\\it"
2156 "\\kill"
2157 "\\label"
2158 "\\large"
2159 "\\ldots"
2160 "\\line"
2161 "\\linebreak"
2162 "\\linethickness"
2163 "\\listoffigures"
2164 "\\listoftables"
2165 "\\location"
2166 "\\makebox"
2167 "\\maketitle"
2168 "\\mark"
2169 "\\mbox"
2170 "\\medskip"
2171 "\\multicolumn"
2172 "\\multiput"
2173 ("\\nbsp" "~")
2174 "\\newcommand"
2175 "\\newcounter"
2176 "\\newenvironment"
2177 "\\newfont"
2178 "\\newlength"
2179 "\\newline"
2180 "\\newpage"
2181 "\\newsavebox"
2182 "\\newtheorem"
2183 "\\nocite"
2184 "\\nofiles"
2185 "\\noindent"
2186 "\\nolinebreak"
2187 "\\nopagebreak"
2188 "\\normalsize"
2189 "\\onecolumn"
2190 "\\opening"
2191 "\\oval"
2192 "\\overbrace"
2193 "\\overline"
2194 "\\pagebreak"
2195 "\\pagenumbering"
2196 "\\pageref"
2197 "\\pagestyle"
2198 "\\par"
2199 "\\parbox"
2200 "\\put"
2201 "\\raggedbottom"
2202 "\\raggedleft"
2203 "\\raggedright"
2204 "\\raisebox"
2205 "\\ref"
2206 "\\rm"
2207 "\\roman"
2208 "\\rule"
2209 "\\savebox"
2210 "\\sc"
2211 "\\scriptsize"
2212 "\\setcounter"
2213 "\\setlength"
2214 "\\settowidth"
2215 "\\sf"
2216 "\\shortstack"
2217 "\\signature"
2218 "\\sl"
2219 "\\small"
2220 "\\smallskip"
2221 "\\sqrt"
2222 "\\tableofcontents"
2223 "\\telephone"
2224 "\\thanks"
2225 "\\thispagestyle"
2226 "\\tiny"
2227 "\\title"
2228 "\\tt"
2229 "\\twocolumn"
2230 "\\typein"
2231 "\\typeout"
2232 "\\underbrace"
2233 "\\underline"
2234 "\\usebox"
2235 "\\usecounter"
2236 "\\value"
2237 "\\vdots"
2238 "\\vector"
2239 "\\verb"
2240 "\\vfill"
2241 "\\vline"
2242 "\\vspace")
2243 "A list of LaTeX commands to be protected when performing conversion.")
2245 (defvar org-latex-entities-exceptions nil)
2247 (defconst org-latex-entities-regexp
2248 (let (names rest)
2249 (dolist (x org-latex-entities)
2250 (when (consp x)
2251 (add-to-list 'org-latex-entities-exceptions x)
2252 (setq x (car x)))
2253 (if (string-match "[a-zA-Z]$" x)
2254 (push x names)
2255 (push x rest)))
2256 (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
2257 "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
2259 (provide 'org-export-latex)
2260 (provide 'org-latex)
2262 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
2264 ;;; org-latex.el ends here