1 ;;; org-latex.el --- LaTeX exporter for org-mode
3 ;; Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: org-latex.el
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/>.
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'
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
82 (defgroup org-export-latex nil
83 "Options for exporting Org-mode files to LaTeX."
84 :tag
"Org Export LaTeX"
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
94 "\\documentclass[11pt]{article}"
95 ("\\section{%s}" .
"\\section*{%s}")
96 ("\\subsection{%s}" .
"\\subsection*{%s}")
97 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
98 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
99 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
101 "\\documentclass[11pt]{report}"
102 ("\\part{%s}" .
"\\part*{%s}")
103 ("\\chapter{%s}" .
"\\chapter*{%s}")
104 ("\\section{%s}" .
"\\section*{%s}")
105 ("\\subsection{%s}" .
"\\subsection*{%s}")
106 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
108 "\\documentclass[11pt]{book}"
109 ("\\part{%s}" .
"\\part*{%s}")
110 ("\\chapter{%s}" .
"\\chapter*{%s}")
111 ("\\section{%s}" .
"\\section*{%s}")
112 ("\\subsection{%s}" .
"\\subsection*{%s}")
113 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
115 "\\documentclass{beamer}"
116 org-beamer-sectioning
118 "Alist of LaTeX classes and associated header and structure.
119 If #+LaTeX_CLASS is set in the buffer, use its value and the
120 associated information. Here is the structure of each cell:
124 (numbered-section . unnumbered-section\)
130 The HEADER-STRING is the header that will be inserted into the LaTeX file.
131 It should contain the \\documentclass macro, and anything else that is needed
132 for this setup. To this header, the following commands will be added:
134 - Calls to \\usepackage for all packages mentioned in the variables
135 `org-export-latex-default-packages-alist' and
136 `org-export-latex-packages-alist'. Thus, your header definitions should
137 avoid to also request these packages.
139 - Lines specified via \"#+LaTeX_HEADER:\"
141 If you need more control about the sequence in which the header is built
142 up, or if you want to exclude one of these building blocks for a particular
143 class, you can use the following macro-like placeholders.
145 [DEFAULT-PACKAGES] \\usepackage statements for default packages
146 [NO-DEFAULT-PACKAGES] do not include any of the default packages
147 [PACKAGES] \\usepackage statements for packages
148 [NO-PACKAGES] do not include the packages
149 [EXTRA] the stuff from #+LaTeX_HEADER
150 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
151 [BEAMER-HEADER-EXTRA] the beamer extra headers
155 \\documentclass{article}
156 [NO-DEFAULT-PACKAGES]
158 \\providecommand{\\alert}[1]{\\textbf{#1}}
161 will omit the default packages, and will include the #+LaTeX_HEADER lines,
162 then have a call to \\providecommand, and then place \\usepackage commands
163 based on the content of `org-export-latex-packages-alist'.
165 If your header or `org-export-latex-default-packages-alist' inserts
166 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be replaced with
167 a coding system derived from `buffer-file-coding-system'. See also the
168 variable `org-export-latex-inputenc-alist' for a way to influence this
171 The sectioning structure
172 ------------------------
174 The sectioning structure of the class is given by the elements following
175 the header string. For each sectioning level, a number of strings is
176 specified. A %s formatter is mandatory in each section string and will
177 be replaced by the title of the section.
179 Instead of a cons cell (numbered . unnumbered), you can also provide a list
182 (numbered-open numbered-close)
186 (numbered-open numbered-close unnumbered-open unnumbered-close)
188 providing opening and closing strings for a LaTeX environment that should
189 represent the document section. The opening clause should have a %s
190 to represent the section title.
192 Instead of a list of sectioning commands, you can also specify a
193 function name. That function will be called with two parameters,
194 the (reduced) level of the headline, and the headline text. The function
195 must return a cons cell with the (possibly modified) headline text, and the
196 sectioning list in the cdr."
197 :group
'org-export-latex
199 (list (string :tag
"LaTeX class")
200 (string :tag
"LaTeX header")
201 (repeat :tag
"Levels" :inline t
204 (string :tag
" numbered")
205 (string :tag
"unnumbered"))
206 (list :tag
"Environment"
207 (string :tag
"Opening (numbered)")
208 (string :tag
"Closing (numbered)")
209 (string :tag
"Opening (unnumbered)")
210 (string :tag
"Closing (unnumbered)"))
211 (function :tag
"Hook computing sectioning"))))))
213 (defcustom org-export-latex-inputenc-alist nil
214 "Alist of inputenc coding system names, and what should really be used.
215 For example, adding an entry
217 (\"utf8\" . \"utf8x\")
219 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
220 are written as utf8 files."
221 :group
'org-export-latex
224 (string :tag
"Derived from buffer")
225 (string :tag
"Use this instead"))))
228 (defcustom org-export-latex-emphasis-alist
229 '(("*" "\\textbf{%s}" nil
)
230 ("/" "\\emph{%s}" nil
)
231 ("_" "\\underline{%s}" nil
)
235 "Alist of LaTeX expressions to convert emphasis fontifiers.
236 Each element of the list is a list of three elements.
237 The first element is the character used as a marker for fontification.
238 The second element is a formatting string to wrap fontified text with.
239 If it is \"\\verb\", Org will automatically select a delimiter
240 character that is not in the string.
241 The third element decides whether to protect converted text from other
243 :group
'org-export-latex
246 (defcustom org-export-latex-title-command
"\\maketitle"
247 "The command used to insert the title just after \\begin{document}.
248 If this string contains the formatting specification \"%s\" then
249 it will be used as a formatting string, passing the title as an
251 :group
'org-export-latex
254 (defcustom org-export-latex-import-inbuffer-stuff nil
255 "Non-nil means define TeX macros for Org's inbuffer definitions.
256 For example \orgTITLE for #+TITLE."
257 :group
'org-export-latex
260 (defcustom org-export-latex-date-format
262 "Format string for \\date{...}."
263 :group
'org-export-latex
266 (defcustom org-export-latex-todo-keyword-markup
"\\textbf{%s}"
267 "Markup for TODO keywords, as a printf format.
268 This can be a single format for all keywords, a cons cell with separate
269 formats for not-done and done states, or an association list with setup
270 for individual keywords. If a keyword shows up for which there is no
271 markup defined, the first one in the association list will be used."
272 :group
'org-export-latex
274 (string :tag
"Default")
275 (cons :tag
"Distinguish undone and done"
276 (string :tag
"Not-DONE states")
277 (string :tag
"DONE states"))
278 (repeat :tag
"Per keyword markup"
280 (string :tag
"Keyword")
281 (string :tag
"Markup")))))
283 (defcustom org-export-latex-tag-markup
"\\textbf{%s}"
284 "Markup for tags, as a printf format."
285 :group
'org-export-latex
288 (defcustom org-export-latex-timestamp-markup
"\\textit{%s}"
289 "A printf format string to be applied to time stamps."
290 :group
'org-export-latex
293 (defcustom org-export-latex-timestamp-keyword-markup
"\\texttt{%s}"
294 "A printf format string to be applied to time stamps."
295 :group
'org-export-latex
298 (defcustom org-export-latex-hyperref-format
"\\href{%s}{%s}"
299 "A printf format string to be applied to hyperref links.
300 The format must contain two %s instances. The first will be filled with
301 the link, the second with the link description."
302 :group
'org-export-latex
305 (defcustom org-export-latex-tables-verbatim nil
306 "When non-nil, tables are exported verbatim."
307 :group
'org-export-latex
310 (defcustom org-export-latex-tables-centered t
311 "When non-nil, tables are exported in a center environment."
312 :group
'org-export-latex
315 (defcustom org-export-latex-tables-column-borders nil
316 "When non-nil, grouping columns can cause outer vertical lines in tables.
317 When nil, grouping causes only separation lines between groups."
318 :group
'org-export-latex
321 (defcustom org-export-latex-low-levels
'itemize
322 "How to convert sections below the current level of sectioning.
323 This is specified by the `org-export-headline-levels' option or the
324 value of \"H:\" in Org's #+OPTION line.
326 This can be either nil (skip the sections), `description', `itemize',
327 or `enumerate' (convert the sections as the corresponding list type), or
328 a string to be used instead of \\section{%s}. In this latter case,
329 the %s stands here for the inserted headline and is mandatory.
331 It may also be a list of three string to define a user-defined environment
332 that should be used. The first string should be the like
333 \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
334 to two occurrences of %s for the title and a label, respectively. The third
335 string should be like \"\\end{itemize\"."
336 :group
'org-export-latex
337 :type
'(choice (const :tag
"Ignore" nil
)
338 (const :tag
"Convert as descriptive list" description
)
339 (const :tag
"Convert as itemized list" itemize
)
340 (const :tag
"Convert as enumerated list" enumerate
)
341 (list :tag
"User-defined environment"
342 :value
("\\begin{itemize}" "\\end{itemize}" "\\item %s")
343 (string :tag
"Start")
345 (string :tag
"item"))
346 (string :tag
"Use a section string" :value
"\\subparagraph{%s}")))
348 (defcustom org-export-latex-list-parameters
349 '(:cbon
"$\\boxtimes$" :cboff
"$\\Box$")
350 "Parameters for the LaTeX list exporter.
351 These parameters will be passed on to `org-list-to-latex', which in turn
352 will pass them (combined with the LaTeX default list parameters) to
353 `org-list-to-generic'."
354 :group
'org-export-latex
357 (defcustom org-export-latex-verbatim-wrap
358 '("\\begin{verbatim}\n" .
"\\end{verbatim}\n")
359 "Environment to be wrapped around a fixed-width section in LaTeX export.
360 This is a cons with two strings, to be added before and after the
363 Defaults to \\begin{verbatim} and \\end{verbatim}."
364 :group
'org-export-translation
365 :group
'org-export-latex
366 :type
'(cons (string :tag
"Open")
367 (string :tag
"Close")))
369 (defcustom org-export-latex-listings nil
370 "Non-nil means export source code using the listings package.
371 This package will fontify source code, possibly even with color.
372 If you want to use this, you also need to make LaTeX use the
373 listings package, and if you want to have color, the color
374 package. Just add these to `org-export-latex-packages-alist',
375 for example using customize, or with something like
378 (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
379 (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))
383 (setq org-export-latex-listings 'minted)
385 causes source code to be exported using the minted package as
386 opposed to listings. If you want to use minted, you need to add
387 the minted package to `org-export-latex-packages-alist', for
388 example using customize, or with
391 (add-to-list 'org-export-latex-packages-alist '(\"\" \"minted\"))
393 In addition, it is neccessary to install
394 pygments (http://pygments.org), and to configure
395 `org-latex-to-pdf-process' so that the -shell-escape option is
398 :group
'org-export-latex
401 (defcustom org-export-latex-listings-langs
402 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
405 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
406 (html "HTML") (xml "XML")
407 (tex "TeX") (latex "TeX")
408 (shell-script "bash")
410 (ocaml "Caml") (caml "Caml")
411 (sql "SQL") (sqlite "sql"))
412 "Alist mapping languages to their listing language counterpart.
413 The key is a symbol, the major mode symbol without the \"-mode\".
414 The value is the string that should be inserted as the language parameter
415 for the listings package. If the mode name and the listings name are
416 the same, the language does not need an entry in this list - but it does not
417 hurt if it is present."
418 :group
'org-export-latex
421 (symbol :tag
"Major mode ")
422 (string :tag
"Listings language"))))
424 (defcustom org-export-latex-listings-w-names t
425 "Non-nil means export names of named code blocks.
426 Code blocks exported with the listings package (controlled by the
427 `org-export-latex-listings' variable) can be named in the style
429 :group
'org-export-latex
432 (defcustom org-export-latex-minted-langs
433 '((emacs-lisp "common-lisp")
436 (shell-script "bash")
438 "Alist mapping languages to their minted language counterpart.
439 The key is a symbol, the major mode symbol without the \"-mode\".
440 The value is the string that should be inserted as the language parameter
441 for the minted package. If the mode name and the listings name are
442 the same, the language does not need an entry in this list - but it does not
443 hurt if it is present.
445 Note that minted uses all lower case for language identifiers,
446 and that the full list of language identifiers can be obtained
450 :group
'org-export-latex
453 (symbol :tag
"Major mode ")
454 (string :tag
"Listings language"))))
456 (defcustom org-export-latex-minted-with-line-numbers nil
457 "Should source code line numbers be included when exporting
458 with the latex minted package?"
459 :group
'org-export-latex
462 (defcustom org-export-latex-remove-from-headlines
463 '(:todo nil
:priority nil
:tags nil
)
464 "A plist of keywords to remove from headlines. OBSOLETE.
465 Non-nil means remove this keyword type from the headline.
467 Don't remove the keys, just change their values.
469 Obsolete, this variable is no longer used. Use the separate
470 variables `org-export-with-todo-keywords', `org-export-with-priority',
471 and `org-export-with-tags' instead."
473 :group
'org-export-latex
)
475 (defcustom org-export-latex-image-default-option
"width=10em"
476 "Default option for images."
477 :group
'org-export-latex
480 (defcustom org-export-latex-tabular-environment
"tabular"
481 "Default environment used to build tables."
482 :group
'org-export-latex
485 (defcustom org-export-latex-inline-image-extensions
486 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
487 "Extensions of image files that can be inlined into LaTeX.
488 Note that the image extension *actually* allowed depend on the way the
489 LaTeX file is processed. When used with pdflatex, pdf, jpg and png images
490 are OK. When processing through dvi to Postscript, only ps and eps are
491 allowed. The default we use here encompasses both."
492 :group
'org-export-latex
493 :type
'(repeat (string :tag
"Extension")))
495 (defcustom org-export-latex-coding-system nil
496 "Coding system for the exported LaTeX file."
497 :group
'org-export-latex
498 :type
'coding-system
)
500 (defgroup org-export-pdf nil
501 "Options for exporting Org-mode files to PDF, via LaTeX."
502 :tag
"Org Export PDF"
503 :group
'org-export-latex
506 (defcustom org-latex-to-pdf-process
507 '("pdflatex -interaction nonstopmode -output-directory %o %f"
508 "pdflatex -interaction nonstopmode -output-directory %o %f"
509 "pdflatex -interaction nonstopmode -output-directory %o %f")
510 "Commands to process a LaTeX file to a PDF file.
511 This is a list of strings, each of them will be given to the shell
512 as a command. %f in the command will be replaced by the full file name, %b
513 by the file base name (i.e. without extension) and %o by the base directory
516 The reason why this is a list is that it usually takes several runs of
517 `pdflatex', maybe mixed with a call to `bibtex'. Org does not have a clever
518 mechanism to detect which of these commands have to be run to get to a stable
519 result, and it also does not do any error checking.
521 By default, Org uses 3 runs of `pdflatex' to do the processing. If you
522 have texi2dvi on your system and if that does not cause the infamous
525 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
527 then `texi2dvi' is the superior choice. Org does offer it as one
528 of the customize options.
530 Alternatively, this may be a Lisp function that does the processing, so you
531 could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
532 This function should accept the file name as its single argument."
533 :group
'org-export-pdf
535 (repeat :tag
"Shell command sequence"
536 (string :tag
"Shell command"))
537 (const :tag
"2 runs of pdflatex"
538 ("pdflatex -interaction nonstopmode -output-directory %o %f"
539 "pdflatex -interaction nonstopmode -output-directory %o %f"))
540 (const :tag
"3 runs of pdflatex"
541 ("pdflatex -interaction nonstopmode -output-directory %o %f"
542 "pdflatex -interaction nonstopmode -output-directory %o %f"
543 "pdflatex -interaction nonstopmode -output-directory %o %f"))
544 (const :tag
"pdflatex,bibtex,pdflatex,pdflatex"
545 ("pdflatex -interaction nonstopmode -output-directory %o %f"
547 "pdflatex -interaction nonstopmode -output-directory %o %f"
548 "pdflatex -interaction nonstopmode -output-directory %o %f"))
549 (const :tag
"texi2dvi"
550 ("texi2dvi -p -b -c -V %f"))
552 ("rubber -d --into %o %f"))
555 (defcustom org-export-pdf-logfiles
556 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
557 "The list of file extensions to consider as LaTeX logfiles."
558 :group
'org-export-pdf
559 :type
'(repeat (string :tag
"Extension")))
561 (defcustom org-export-pdf-remove-logfiles t
562 "Non-nil means remove the logfiles produced by PDF production.
563 These are the .aux, .log, .out, and .toc files."
564 :group
'org-export-pdf
569 (defvar org-export-latex-after-initial-vars-hook nil
570 "Hook run before LaTeX export.
571 The exact moment is after the initial variables like org-export-latex-class
572 have been determined from the environment.")
574 (defvar org-export-latex-after-blockquotes-hook nil
575 "Hook run during LaTeX export, after blockquote, verse, center are done.")
577 (defvar org-export-latex-final-hook nil
578 "Hook run in the finalized LaTeX buffer.")
580 (defvar org-export-latex-after-save-hook nil
581 "Hook run in the finalized LaTeX buffer, after it has been saved.")
583 ;;; Autoload functions:
586 (defun org-export-as-latex-batch ()
587 "Call `org-export-as-latex', may be used in batch processing.
591 --load=$HOME/lib/emacs/org.el
592 --eval \"(setq org-export-headline-levels 2)\"
593 --visit=MyFile --funcall org-export-as-latex-batch"
594 (org-export-as-latex org-export-headline-levels
'hidden
))
597 (defun org-export-as-latex-to-buffer (arg)
598 "Call `org-export-as-latex` with output to a temporary buffer.
599 No file is created. The prefix ARG is passed through to `org-export-as-latex'."
601 (org-export-as-latex arg nil nil
"*Org LaTeX Export*")
602 (when org-export-show-temporary-export-buffer
603 (switch-to-buffer-other-window "*Org LaTeX Export*")))
606 (defun org-replace-region-by-latex (beg end
)
607 "Replace the region from BEG to END with its LaTeX export.
608 It assumes the region has `org-mode' syntax, and then convert it to
609 LaTeX. This can be used in any buffer. For example, you could
610 write an itemized list in `org-mode' syntax in an LaTeX buffer and
611 then use this command to convert it."
614 (save-window-excursion
616 (setq latex
(org-export-region-as-latex
618 (setq reg
(buffer-substring beg end
)
619 buf
(get-buffer-create "*Org tmp*"))
620 (with-current-buffer buf
624 (setq latex
(org-export-region-as-latex
625 (point-min) (point-max) t
'string
)))
627 (delete-region beg end
)
631 (defun org-export-region-as-latex (beg end
&optional body-only buffer
)
632 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
633 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
634 contents, and only produce the region of converted text, useful for
635 cut-and-paste operations.
636 If BUFFER is a buffer or a string, use/create that buffer as a target
637 of the converted LaTeX. If BUFFER is the symbol `string', return the
638 produced LaTeX as a string and leave no buffer behind. For example,
639 a Lisp program could call this function in the following way:
641 (setq latex (org-export-region-as-latex beg end t 'string))
643 When called interactively, the output buffer is selected, and shown
644 in a window. A non-interactive call will only return the buffer."
646 (when (interactive-p)
647 (setq buffer
"*Org LaTeX Export*"))
648 (let ((transient-mark-mode t
) (zmacs-regions t
)
650 (setq ext-plist
(plist-put ext-plist
:ignore-subtree-p t
))
652 (set-mark (point)) ;; to activate the region
654 (setq rtn
(org-export-as-latex
657 (if (fboundp 'deactivate-mark
) (deactivate-mark))
658 (if (and (interactive-p) (bufferp rtn
))
659 (switch-to-buffer-other-window rtn
)
663 (defun org-export-as-latex (arg &optional hidden ext-plist
664 to-buffer body-only pub-dir
)
665 "Export current buffer to a LaTeX file.
666 If there is an active region, export only the region. The prefix
667 ARG specifies how many levels of the outline should become
668 headlines. The default is 3. Lower levels will be exported
669 depending on `org-export-latex-low-levels'. The default is to
670 convert them as description lists.
671 HIDDEN is obsolete and does nothing.
672 EXT-PLIST is a property list with
673 external parameters overriding org-mode's default settings, but
674 still inferior to file-local settings. When TO-BUFFER is
675 non-nil, create a buffer with that name and export to that
676 buffer. If TO-BUFFER is the symbol `string', don't leave any
677 buffer behind but just return the resulting LaTeX as a string.
678 When BODY-ONLY is set, don't produce the file header and footer,
679 simply return the content of \\begin{document}...\\end{document},
680 without even the \\begin{document} and \\end{document} commands.
681 when PUB-DIR is set, use this as the publishing directory."
683 (when (and (not body-only
) arg
(listp arg
)) (setq body-only t
))
684 (run-hooks 'org-export-first-hook
)
686 ;; Make sure we have a file name when we need it.
687 (when (and (not (or to-buffer body-only
))
688 (not buffer-file-name
))
689 (if (buffer-base-buffer)
690 (org-set-local 'buffer-file-name
691 (with-current-buffer (buffer-base-buffer)
693 (error "Need a file name to be able to export")))
695 (message "Exporting to LaTeX...")
697 (let ((inhibit-read-only t
))
698 (remove-text-properties (point-min) (point-max)
699 '(:org-license-to-kill nil
))))
700 (org-update-radio-target-regexp)
701 (org-export-latex-set-initial-vars ext-plist arg
)
702 (setq org-export-opt-plist org-export-latex-options-plist
)
703 (org-install-letbind)
704 (run-hooks 'org-export-latex-after-initial-vars-hook
)
705 (let* ((wcf (current-window-configuration))
706 (opt-plist org-export-latex-options-plist
)
707 (region-p (org-region-active-p))
708 (rbeg (and region-p
(region-beginning)))
709 (rend (and region-p
(region-end)))
711 (if (plist-get opt-plist
:ignore-subtree-p
)
716 (and (org-at-heading-p)
717 (>= (org-end-of-subtree t t
) rend
))))))
718 (opt-plist (setq org-export-opt-plist
720 (org-export-add-subtree-options opt-plist rbeg
)
722 ;; Make sure the variable contains the updated values.
723 (org-export-latex-options-plist (setq org-export-opt-plist opt-plist
))
724 ;; The following two are dynamically scoped into other
726 (org-current-export-dir
727 (or pub-dir
(org-export-directory :html opt-plist
)))
728 (org-current-export-file buffer-file-name
)
729 (title (or (and subtree-p
(org-export-get-title-from-subtree))
730 (plist-get opt-plist
:title
)
732 (plist-get opt-plist
:skip-before-1st-heading
))
733 (org-export-grab-title-from-buffer))
734 (and buffer-file-name
735 (file-name-sans-extension
736 (file-name-nondirectory buffer-file-name
)))
741 (file-name-as-directory
743 (org-export-directory :LaTeX ext-plist
)))
744 (file-name-sans-extension
746 (org-entry-get rbeg
"EXPORT_FILE_NAME" t
))
747 (file-name-nondirectory ;sans-extension
749 (error "Don't know which export file to use")))))
753 (if (equal (file-truename filename
)
754 (file-truename (or buffer-file-name
"dummy.org")))
755 (concat filename
".tex")
757 (buffer (if to-buffer
759 ((eq to-buffer
'string
) (get-buffer-create
760 "*Org LaTeX Export*"))
761 (t (get-buffer-create to-buffer
)))
762 (find-file-noselect filename
)))
763 (odd org-odd-levels-only
)
764 (header (org-export-latex-make-header title opt-plist
))
765 (skip (cond (subtree-p nil
)
767 (t (plist-get opt-plist
:skip-before-1st-heading
))))
768 (text (plist-get opt-plist
:text
))
769 (org-export-preprocess-hook
771 `(lambda () (org-set-local 'org-complex-heading-regexp
772 ,org-export-latex-complex-heading-re
))
773 org-export-preprocess-hook
))
774 (first-lines (if skip
"" (org-export-latex-first-lines
781 (if region-p rend
))))
782 (coding-system (and (boundp 'buffer-file-coding-system
)
783 buffer-file-coding-system
))
784 (coding-system-for-write (or org-export-latex-coding-system
786 (save-buffer-coding-system (or org-export-latex-coding-system
788 (region (buffer-substring
789 (if region-p
(region-beginning) (point-min))
790 (if region-p
(region-end) (point-max))))
792 (and text
(string-match "\\S-" text
)
793 (org-export-preprocess-string
798 :tags
(plist-get opt-plist
:tags
)
799 :priority
(plist-get opt-plist
:priority
)
800 :footnotes
(plist-get opt-plist
:footnotes
)
801 :drawers
(plist-get opt-plist
:drawers
)
802 :timestamps
(plist-get opt-plist
:timestamps
)
803 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
805 :skip-before-1st-heading skip
808 :LaTeX-fragments nil
)))
810 (org-export-preprocess-string
815 :tags
(plist-get opt-plist
:tags
)
816 :priority
(plist-get opt-plist
:priority
)
817 :footnotes
(plist-get opt-plist
:footnotes
)
818 :drawers
(plist-get opt-plist
:drawers
)
819 :timestamps
(plist-get opt-plist
:timestamps
)
820 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
821 :add-text
(if (eq to-buffer
'string
) nil text
)
822 :skip-before-1st-heading skip
823 :select-tags
(plist-get opt-plist
:select-tags
)
824 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
825 :LaTeX-fragments nil
)))
829 (org-install-letbind)
831 (and (fboundp 'set-buffer-file-coding-system
)
832 (set-buffer-file-coding-system coding-system-for-write
))
834 ;; insert the header and initial document commands
835 (unless (or (eq to-buffer
'string
) body-only
)
838 ;; insert text found in #+TEXT
839 (when (and text
(not (eq to-buffer
'string
)))
840 (insert (org-export-latex-content
841 text
'(lists tables fixed-width keywords
))
844 ;; insert lines before the first headline
846 (insert first-lines
))
848 ;; export the content of headlines
849 (org-export-latex-global
851 (insert string-for-export
)
852 (goto-char (point-min))
853 (when (re-search-forward "^\\(\\*+\\) " nil t
)
854 (let* ((asters (length (match-string 1)))
855 (level (if odd
(- asters
2) (- asters
1))))
856 (setq org-export-latex-add-level
857 (if odd
(1- (/ (1+ asters
) 2)) (1- asters
)))
858 (org-export-latex-parse-global level odd
)))))
861 (unless body-only
(insert "\n\\end{document}"))
863 ;; Attach description terms to the \item macro
864 (goto-char (point-min))
865 (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t
)
866 (delete-region (match-beginning 1) (match-end 1)))
868 ;; Relocate the table of contents
869 (goto-char (point-min))
870 (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t
)
871 (goto-char (point-min))
872 (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t
)
874 (goto-char (point-min))
875 (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t
)
876 (replace-match "\\tableofcontents" t t
)))
878 ;; Cleanup forced line ends in items where they are not needed
879 (goto-char (point-min))
880 (while (re-search-forward
881 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin"
883 (delete-region (match-beginning 1) (match-end 1)))
884 (goto-char (point-min))
885 (while (re-search-forward
886 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*"
888 (if (looking-at "[\n \t]+")
889 (replace-match "\n")))
891 (run-hooks 'org-export-latex-final-hook
)
893 (unless (eq major-mode
'latex-mode
) (latex-mode))
895 (org-export-latex-fix-inputenc)
896 (run-hooks 'org-export-latex-after-save-hook
)
897 (goto-char (point-min))
898 (or (org-export-push-to-kill-ring "LaTeX")
899 (message "Exporting to LaTeX...done"))
901 (if (eq to-buffer
'string
)
902 (prog1 (buffer-substring (point-min) (point-max))
903 (kill-buffer (current-buffer)))
905 (set-window-configuration wcf
))))
908 (defun org-export-as-pdf (arg &optional hidden ext-plist
909 to-buffer body-only pub-dir
)
910 "Export as LaTeX, then process through to PDF."
912 (message "Exporting to PDF...")
913 (let* ((wconfig (current-window-configuration))
914 (lbuf (org-export-as-latex arg hidden ext-plist
915 to-buffer body-only pub-dir
))
916 (file (buffer-file-name lbuf
))
917 (base (file-name-sans-extension (buffer-file-name lbuf
)))
918 (pdffile (concat base
".pdf"))
919 (cmds org-latex-to-pdf-process
)
920 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
921 (bibtex-p (with-current-buffer lbuf
923 (goto-char (point-min))
924 (re-search-forward "\\\\bibliography{" nil t
))))
925 cmd output-dir errors
)
926 (with-current-buffer outbuf
(erase-buffer))
927 (message (concat "Processing LaTeX file " file
"..."))
928 (setq output-dir
(file-name-directory file
))
929 (if (and cmds
(symbolp cmds
))
930 (funcall cmds
(shell-quote-argument file
))
932 (setq cmd
(pop cmds
))
933 (while (string-match "%b" cmd
)
934 (setq cmd
(replace-match
936 (shell-quote-argument base
))
938 (while (string-match "%f" cmd
)
939 (setq cmd
(replace-match
941 (shell-quote-argument file
))
943 (while (string-match "%o" cmd
)
944 (setq cmd
(replace-match
946 (shell-quote-argument output-dir
))
948 (shell-command cmd outbuf
)))
949 (message (concat "Processing LaTeX file " file
"...done"))
950 (setq errors
(org-export-latex-get-error outbuf
))
951 (if (not (file-exists-p pdffile
))
952 (error (concat "PDF file " pdffile
" was not produced"
953 (if errors
(concat ":" errors
"") "")))
954 (set-window-configuration wconfig
)
955 (when org-export-pdf-remove-logfiles
956 (dolist (ext org-export-pdf-logfiles
)
957 (setq file
(concat base
"." ext
))
958 (and (file-exists-p file
) (delete-file file
))))
960 "Exporting to PDF...done"
962 (concat ", with some errors:" errors
)
966 (defun org-export-latex-get-error (buf)
967 "Collect the kinds of errors that remain in pdflatex processing."
968 (with-current-buffer buf
970 (goto-char (point-max))
971 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t
)
972 ;; OK, we are at the location of the final run
973 (let ((pos (point)) (errors "") (case-fold-search t
))
974 (if (re-search-forward "Reference.*?undefined" nil t
)
975 (setq errors
(concat errors
" [undefined reference]")))
977 (if (re-search-forward "Citation.*?undefined" nil t
)
978 (setq errors
(concat errors
" [undefined citation]")))
980 (if (re-search-forward "Undefined control sequence" nil t
)
981 (setq errors
(concat errors
" [undefined control sequence]")))
982 (and (org-string-nw-p errors
) errors
))))))
985 (defun org-export-as-pdf-and-open (arg)
986 "Export as LaTeX, then process through to PDF, and open."
988 (let ((pdffile (org-export-as-pdf arg
)))
991 (org-open-file pdffile
)
992 (when org-export-kill-product-buffer-when-displayed
993 (kill-buffer (find-buffer-visiting
994 (concat (file-name-sans-extension (buffer-file-name))
996 (error "PDF file was not produced"))))
998 ;;; Parsing functions:
1000 (defun org-export-latex-parse-global (level odd
)
1001 "Parse the current buffer recursively, starting at LEVEL.
1002 If ODD is non-nil, assume the buffer only contains odd sections.
1003 Return a list reflecting the document structure."
1005 (goto-char (point-min))
1006 (let* ((cnt 0) output
1007 (depth org-export-latex-sectioning-depth
))
1008 (while (org-re-search-forward-unprotected
1009 (concat "^\\(\\(?:\\*\\)\\{"
1010 (number-to-string (+ (if odd
2 1) level
))
1012 ;; make sure that there is no upper heading
1016 (org-re-search-forward-unprotected
1017 (concat "^\\(\\(?:\\*\\)\\{"
1018 (number-to-string level
)
1019 "\\}\\) \\(.*\\)$") nil t
)))) t
)
1021 (let* ((pos (match-beginning 0))
1022 (heading (match-string 2))
1023 (nlevel (if odd
(/ (+ 3 level
) 2) (1+ level
))))
1028 (if (org-re-search-forward-unprotected
1029 (concat "^\\(\\(?:\\*\\)\\{"
1030 (number-to-string (+ (if odd
2 1) level
))
1031 "\\}\\) \\(.*\\)$") nil t
)
1034 (goto-char (point-min))
1042 `(heading .
,heading
)
1043 `(content .
,(org-export-latex-parse-content))
1044 `(subcontent .
,(org-export-latex-parse-subcontent
1049 (defun org-export-latex-parse-content ()
1050 "Extract the content of a section."
1052 (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t
)
1053 (progn (beginning-of-line) (point))
1055 (buffer-substring beg end
)))
1057 (defun org-export-latex-parse-subcontent (level odd
)
1058 "Extract the subcontent of a section at LEVEL.
1059 If ODD Is non-nil, assume subcontent only contains odd sections."
1060 (if (not (org-re-search-forward-unprotected
1061 (concat "^\\(\\(?:\\*\\)\\{"
1062 (number-to-string (+ (if odd
4 2) level
))
1065 nil
; subcontent is nil
1066 (org-export-latex-parse-global (+ (if odd
2 1) level
) odd
)))
1068 ;;; Rendering functions:
1069 (defun org-export-latex-global (content)
1070 "Export CONTENT to LaTeX.
1071 CONTENT is an element of the list produced by
1072 `org-export-latex-parse-global'."
1073 (if (eq (car content
) 'subcontent
)
1074 (mapc 'org-export-latex-sub
(cdr content
))
1075 (org-export-latex-sub (car content
))))
1077 (defun org-export-latex-sub (subcontent)
1078 "Export the list SUBCONTENT to LaTeX.
1079 SUBCONTENT is an alist containing information about the headline
1081 (let ((num (plist-get org-export-latex-options-plist
:section-numbers
)))
1082 (mapc (lambda(x) (org-export-latex-subcontent x num
)) subcontent
)))
1084 (defun org-export-latex-subcontent (subcontent num
)
1085 "Export each cell of SUBCONTENT to LaTeX.
1086 If NUM, export sections as numerical sections."
1087 (let* ((heading (cdr (assoc 'heading subcontent
)))
1088 (level (- (cdr (assoc 'level subcontent
))
1089 org-export-latex-add-level
))
1090 (occur (number-to-string (cdr (assoc 'occur subcontent
))))
1091 (content (cdr (assoc 'content subcontent
)))
1092 (subcontent (cadr (assoc 'subcontent subcontent
)))
1093 (label (org-get-text-property-any 0 'target heading
))
1094 (label-list (cons label
(cdr (assoc label
1095 org-export-target-aliases
))))
1096 (sectioning org-export-latex-sectioning
)
1097 (depth org-export-latex-sectioning-depth
)
1098 main-heading sub-heading
)
1099 (when (symbolp (car sectioning
))
1100 (setq sectioning
(funcall (car sectioning
) level heading
))
1102 (setq heading
(car sectioning
)
1103 sectioning
(cdr sectioning
)
1104 ;; target property migh have changed...
1105 label
(org-get-text-property-any 0 'target heading
)
1106 label-list
(cons label
(cdr (assoc label
1107 org-export-target-aliases
)))))
1108 (if sectioning
(setq sectioning
(make-list 10 sectioning
)))
1109 (setq depth
(if sectioning
10000 0)))
1110 (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading
)
1111 (setq main-heading
(substring heading
0 (match-beginning 0))
1112 sub-heading
(substring heading
(match-end 0))))
1113 (setq heading
(org-export-latex-fontify-headline heading
)
1114 sub-heading
(and sub-heading
1115 (org-export-latex-fontify-headline sub-heading
))
1116 main-heading
(and main-heading
1117 (org-export-latex-fontify-headline main-heading
)))
1119 ;; Normal conversion
1121 (let* ((sec (nth (1- level
) sectioning
))
1123 (if (consp (cdr sec
))
1124 (setq start
(nth (if num
0 2) sec
)
1125 end
(nth (if num
1 3) sec
))
1126 (setq start
(if num
(car sec
) (cdr sec
))))
1127 (insert (format start
(if main-heading main-heading heading
)
1128 (or sub-heading
"")))
1131 (insert (mapconcat (lambda (l) (format "\\label{%s}" l
))
1132 label-list
"\n") "\n"))
1133 (insert (org-export-latex-content content
))
1134 (cond ((stringp subcontent
) (insert subcontent
))
1136 (while (org-looking-back "\n\n") (backward-delete-char 1))
1137 (org-export-latex-sub subcontent
)))
1138 (when (and end
(string-match "[^ \t]" end
))
1139 (let ((hook (org-get-text-property-any 0 'org-insert-hook end
)))
1140 (and (functionp hook
) (funcall hook
)))
1141 (insert end
"\n"))))
1142 ;; At a level under the hl option: we can drop this subsection
1144 (cond ((eq org-export-latex-low-levels
'description
)
1145 (if (string-match "% ends low level$"
1146 (buffer-substring (point-at-bol 0) (point)))
1147 (delete-region (point-at-bol 0) (point))
1148 (insert "\\begin{description}\n"))
1149 (insert (format "\n\\item[%s]%s~\n"
1151 (if label
(format "\\label{%s}" label
) "")))
1152 (insert (org-export-latex-content content
))
1153 (cond ((stringp subcontent
) (insert subcontent
))
1154 ((listp subcontent
) (org-export-latex-sub subcontent
)))
1155 (insert "\\end{description} % ends low level\n"))
1156 ((memq org-export-latex-low-levels
'(itemize enumerate
))
1157 (if (string-match "% ends low level$"
1158 (buffer-substring (point-at-bol 0) (point)))
1159 (delete-region (point-at-bol 0) (point))
1160 (insert (format "\\begin{%s}\n"
1161 (symbol-name org-export-latex-low-levels
))))
1162 (insert (format "\n\\item %s\\\\\n%s%%"
1164 (if label
(format "\\label{%s}" label
) "")))
1165 (insert (org-export-latex-content content
))
1166 (cond ((stringp subcontent
) (insert subcontent
))
1167 ((listp subcontent
) (org-export-latex-sub subcontent
)))
1168 (insert (format "\\end{%s} %% ends low level\n"
1169 (symbol-name org-export-latex-low-levels
))))
1171 ((listp org-export-latex-low-levels
)
1172 (if (string-match "% ends low level$"
1173 (buffer-substring (point-at-bol 0) (point)))
1174 (delete-region (point-at-bol 0) (point))
1175 (insert (car org-export-latex-low-levels
) "\n"))
1176 (insert (format (nth 2 org-export-latex-low-levels
)
1178 (if label
(format "\\label{%s}" label
) "")))
1179 (insert (org-export-latex-content content
))
1180 (cond ((stringp subcontent
) (insert subcontent
))
1181 ((listp subcontent
) (org-export-latex-sub subcontent
)))
1182 (insert (nth 1 org-export-latex-low-levels
)
1183 " %% ends low level\n"))
1185 ((stringp org-export-latex-low-levels
)
1186 (insert (format org-export-latex-low-levels heading
) "\n")
1187 (when label
(insert (format "\\label{%s}\n" label
)))
1188 (insert (org-export-latex-content content
))
1189 (cond ((stringp subcontent
) (insert subcontent
))
1190 ((listp subcontent
) (org-export-latex-sub subcontent
)))))))))
1192 ;;; Exporting internals:
1193 (defun org-export-latex-set-initial-vars (ext-plist level
)
1194 "Store org local variables required for LaTeX export.
1195 EXT-PLIST is an optional additional plist.
1196 LEVEL indicates the default depth for export."
1197 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
1198 org-export-latex-done-keywords org-done-keywords
1199 org-export-latex-not-done-keywords org-not-done-keywords
1200 org-export-latex-complex-heading-re org-complex-heading-regexp
1201 org-export-latex-display-custom-times org-display-custom-times
1202 org-export-latex-all-targets-re
1203 (org-make-target-link-regexp (org-all-targets))
1204 org-export-latex-options-plist
1205 (org-combine-plists (org-default-export-plist) ext-plist
1206 (org-infile-export-plist))
1207 org-export-latex-class
1208 (or (and (org-region-active-p)
1210 (goto-char (region-beginning))
1211 (and (looking-at org-complex-heading-regexp
)
1212 (org-entry-get nil
"LaTeX_CLASS" 'selective
))))
1216 (goto-char (point-min))
1217 (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\(-[a-zA-Z]+\\)" nil t
)
1219 (plist-get org-export-latex-options-plist
:latex-class
)
1220 org-export-latex-default-class
)
1221 org-export-latex-class-options
1222 (or (and (org-region-active-p)
1224 (goto-char (region-beginning))
1225 (and (looking-at org-complex-heading-regexp
)
1226 (org-entry-get nil
"LaTeX_CLASS_OPTIONS" 'selective
))))
1230 (goto-char (point-min))
1231 (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t
)
1233 (plist-get org-export-latex-options-plist
:latex-class-options
))
1234 org-export-latex-class
1235 (or (car (assoc org-export-latex-class org-export-latex-classes
))
1236 (error "No definition for class `%s' in `org-export-latex-classes'"
1237 org-export-latex-class
))
1238 org-export-latex-header
1239 (cadr (assoc org-export-latex-class org-export-latex-classes
))
1240 org-export-latex-sectioning
1241 (cddr (assoc org-export-latex-class org-export-latex-classes
))
1242 org-export-latex-sectioning-depth
1245 (plist-get org-export-latex-options-plist
:headline-levels
))
1246 (sec-depth (length org-export-latex-sectioning
)))
1247 (if (> hl-levels sec-depth
) sec-depth hl-levels
))))
1248 (when (and org-export-latex-class-options
1249 (string-match "\\S-" org-export-latex-class-options
)
1250 (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?"
1251 org-export-latex-header
))
1252 (setq org-export-latex-header
1253 (concat (substring org-export-latex-header
0 (match-end 1))
1254 org-export-latex-class-options
1255 (substring org-export-latex-header
(match-end 0))))))
1257 (defvar org-export-latex-format-toc-function
1258 'org-export-latex-format-toc-default
1259 "The function formatting returning the string to create the table of contents.
1260 The function mus take one parameter, the depth of the table of contents.")
1262 (defun org-export-latex-make-header (title opt-plist
)
1263 "Make the LaTeX header and return it as a string.
1264 TITLE is the current title from the buffer or region.
1265 OPT-PLIST is the options plist for current buffer."
1266 (let ((toc (plist-get opt-plist
:table-of-contents
))
1267 (author (org-export-apply-macros-in-string
1268 (plist-get opt-plist
:author
))))
1270 (if (plist-get opt-plist
:time-stamp-file
)
1271 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1272 ;; insert LaTeX custom header and packages from the list
1273 (org-splice-latex-header
1274 (org-export-apply-macros-in-string org-export-latex-header
)
1275 org-export-latex-default-packages-alist
1276 org-export-latex-packages-alist nil
1277 (org-export-apply-macros-in-string
1278 (plist-get opt-plist
:latex-header-extra
)))
1279 ;; append another special variable
1280 (org-export-apply-macros-in-string org-export-latex-append-header
)
1281 ;; define alert if not yet defined
1282 "\n\\providecommand{\\alert}[1]{\\textbf{#1}}"
1286 ;; convert the title
1287 (org-export-latex-content
1288 title
'(lists tables fixed-width keywords
)))
1289 ;; insert author info
1290 (if (plist-get opt-plist
:author-info
)
1291 (format "\\author{%s}\n"
1292 (org-export-latex-fontify-headline (or author user-full-name
)))
1293 (format "%%\\author{%s}\n"
1294 (org-export-latex-fontify-headline (or author user-full-name
))))
1296 (format "\\date{%s}\n"
1298 (or (plist-get opt-plist
:date
)
1299 org-export-latex-date-format
)))
1300 ;; beginning of the document
1301 "\n\\begin{document}\n\n"
1302 ;; insert the title command
1303 (when (string-match "\\S-" title
)
1304 (if (string-match "%s" org-export-latex-title-command
)
1305 (format org-export-latex-title-command title
)
1306 org-export-latex-title-command
))
1308 ;; table of contents
1309 (when (and org-export-with-toc
1310 (plist-get opt-plist
:section-numbers
))
1311 (funcall org-export-latex-format-toc-function
1312 (cond ((numberp toc
)
1313 (min toc
(plist-get opt-plist
:headline-levels
)))
1314 (toc (plist-get opt-plist
:headline-levels
))))))))
1316 (defun org-export-latex-format-toc-default (depth)
1318 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1321 (defun org-export-latex-first-lines (opt-plist &optional beg end
)
1322 "Export the first lines before first headline.
1323 If BEG is non-nil, it is the beginning of the region.
1324 If END is non-nil, it is the end of the region."
1326 (goto-char (or beg
(point-min)))
1328 (end (if (re-search-forward "^\\*+ " end t
)
1329 (goto-char (match-beginning 0))
1330 (goto-char (or end
(point-max))))))
1332 (org-export-latex-content
1333 (org-export-preprocess-string
1334 (buffer-substring pt end
)
1339 :skip-before-1st-heading nil
1340 :LaTeX-fragments nil
1341 :timestamps
(plist-get opt-plist
:timestamps
)
1342 :footnotes
(plist-get opt-plist
:footnotes
)))
1344 (let ((inhibit-read-only t
)
1345 (limit (max pt
(1- end
))))
1346 (add-text-properties pt limit
1347 '(:org-license-to-kill t
))
1350 (while (re-search-forward "^[ \t]*#\\+.*\n?" limit t
)
1351 (let ((case-fold-search t
))
1352 (unless (org-string-match-p
1353 "^[ \t]*#\\+\\(attr_\\|caption\\>\\|label\\>\\)"
1355 (remove-text-properties (match-beginning 0) (match-end 0)
1356 '(:org-license-to-kill t
))))))))))))
1359 (defvar org-export-latex-header-defs nil
1360 "The header definitions that might be used in the LaTeX body.")
1361 (defvar org-export-latex-header-defs-re nil
1362 "The header definitions that might be used in the LaTeX body.")
1364 (defun org-export-latex-content (content &optional exclude-list
)
1365 "Convert CONTENT string to LaTeX.
1366 Don't perform conversions that are in EXCLUDE-LIST. Recognized
1367 conversion types are: quotation-marks, emphasis, sub-superscript,
1368 links, keywords, lists, tables, fixed-width"
1371 (unless (memq 'timestamps exclude-list
)
1372 (org-export-latex-time-stamps))
1373 (unless (memq 'quotation-marks exclude-list
)
1374 (org-export-latex-quotation-marks))
1375 (unless (memq 'emphasis exclude-list
)
1376 (when (plist-get org-export-latex-options-plist
:emphasize
)
1377 (org-export-latex-fontify)))
1378 (unless (memq 'sub-superscript exclude-list
)
1379 (org-export-latex-special-chars
1380 (plist-get org-export-latex-options-plist
:sub-superscript
)))
1381 (unless (memq 'links exclude-list
)
1382 (org-export-latex-links))
1383 (unless (memq 'keywords exclude-list
)
1384 (org-export-latex-keywords))
1385 (unless (memq 'lists exclude-list
)
1386 (org-export-latex-lists))
1387 (unless (memq 'tables exclude-list
)
1388 (org-export-latex-tables
1389 (plist-get org-export-latex-options-plist
:tables
)))
1390 (unless (memq 'fixed-width exclude-list
)
1391 (org-export-latex-fixed-width
1392 (plist-get org-export-latex-options-plist
:fixed-width
)))
1394 (buffer-substring (point-min) (point-max))))
1396 (defun org-export-latex-protect-string (s)
1397 "Add the org-protected property to string S."
1398 (add-text-properties 0 (length s
) '(org-protected t
) s
) s
)
1400 (defun org-export-latex-protect-char-in-string (char-list string
)
1401 "Add org-protected text-property to char from CHAR-LIST in STRING."
1405 (goto-char (point-min))
1406 (while (re-search-forward (regexp-opt char-list
) nil t
)
1407 (add-text-properties (match-beginning 0)
1408 (match-end 0) '(org-protected t
)))
1411 (defun org-export-latex-keywords-maybe (&optional remove-list
)
1412 "Maybe remove keywords depending on rules in REMOVE-LIST."
1413 (goto-char (point-min))
1414 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1
"\\|"))
1415 (case-fold-search nil
)
1416 (todo-markup org-export-latex-todo-keyword-markup
)
1418 ;; convert TODO keywords
1419 (when (re-search-forward (concat "^\\(" re-todo
"\\)") nil t
)
1420 (if (plist-get remove-list
:todo
)
1423 ((stringp todo-markup
) todo-markup
)
1424 ((and (consp todo-markup
) (stringp (car todo-markup
)))
1425 (if (member (match-string 1) org-export-latex-done-keywords
)
1426 (cdr todo-markup
) (car todo-markup
)))
1427 (t (cdr (or (assoc (match-string 1) todo-markup
)
1428 (car todo-markup
))))))
1429 (replace-match (org-export-latex-protect-string
1430 (format fmt
(match-string 1))) t t
)))
1431 ;; convert priority string
1432 (when (re-search-forward "\\[\\\\#.\\]" nil t
)
1433 (if (plist-get remove-list
:priority
)
1435 (replace-match (format "\\textbf{%s}" (match-string 0)) t t
)))
1437 (when (re-search-forward "\\(:[a-zA-Z0-9_@#%]+\\)+:" nil t
)
1438 (if (or (not org-export-with-tags
)
1439 (plist-get remove-list
:tags
))
1442 (org-export-latex-protect-string
1443 (format org-export-latex-tag-markup
1445 (replace-regexp-in-string
1446 "_" "\\\\_" (match-string 0)))))
1449 (defun org-export-latex-fontify-headline (string)
1450 "Fontify special words in STRING."
1452 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
1453 ;; the beginning of the buffer - inserting "\n" is safe here though.
1454 (insert "\n" string
)
1455 (goto-char (point-min))
1456 (let ((re (concat "\\\\\\([a-zA-Z]+\\)"
1457 "\\(?:<[^<>\n]*>\\)*"
1458 "\\(?:\\[[^][\n]*?\\]\\)*"
1459 "\\(?:<[^<>\n]*>\\)*"
1461 (org-create-multibrace-regexp "{" "}" 3)
1463 (while (re-search-forward re nil t
)
1465 ;; check for comment line
1466 (save-excursion (goto-char (match-beginning 0))
1467 (org-in-indented-comment-line))
1468 ;; Check if this is a defined entity, so that is may need conversion
1469 (org-entity-get (match-string 1)))
1470 (add-text-properties (match-beginning 0) (match-end 0)
1471 '(org-protected t
)))))
1472 (when (plist-get org-export-latex-options-plist
:emphasize
)
1473 (org-export-latex-fontify))
1474 (org-export-latex-keywords-maybe)
1475 (org-export-latex-special-chars
1476 (plist-get org-export-latex-options-plist
:sub-superscript
))
1477 (org-export-latex-links)
1478 (org-trim (buffer-string))))
1480 (defun org-export-latex-time-stamps ()
1481 "Format time stamps."
1482 (goto-char (point-min))
1483 (let ((org-display-custom-times org-export-latex-display-custom-times
))
1484 (while (re-search-forward org-ts-regexp-both nil t
)
1485 (org-if-unprotected-at (1- (point))
1487 (org-export-latex-protect-string
1488 (format org-export-latex-timestamp-markup
1489 (substring (org-translate-time (match-string 0)) 1 -
1)))
1492 (defun org-export-latex-quotation-marks ()
1493 "Export quotation marks depending on language conventions."
1494 (let* ((lang (plist-get org-export-latex-options-plist
:language
))
1495 (quote-rpl (if (equal lang
"fr")
1496 '(("\\(\\s-\\)\"" "«~")
1497 ("\\(\\S-\\)\"" "~»")
1498 ("\\(\\s-\\)'" "`"))
1499 '(("\\(\\s-\\|[[(]\\)\"" "``")
1500 ("\\(\\S-\\)\"" "''")
1501 ("\\(\\s-\\|(\\)'" "`")))))
1502 (mapc (lambda(l) (goto-char (point-min))
1503 (while (re-search-forward (car l
) nil t
)
1504 (let ((rpl (concat (match-string 1)
1505 (org-export-latex-protect-string
1506 (copy-sequence (cadr l
))))))
1507 (org-if-unprotected-1
1508 (replace-match rpl t t
))))) quote-rpl
)))
1510 (defun org-export-latex-special-chars (sub-superscript)
1511 "Export special characters to LaTeX.
1512 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
1513 See the `org-export-latex.el' code for a complete conversion table."
1514 (goto-char (point-min))
1516 (goto-char (point-min))
1517 (while (re-search-forward c nil t
)
1518 ;; Put the point where to check for org-protected
1519 (unless (get-text-property (match-beginning 2) 'org-protected
)
1520 (cond ((member (match-string 2) '("\\$" "$"))
1521 (if (equal (match-string 2) "\\$")
1523 (replace-match "\\$" t t
)))
1524 ((member (match-string 2) '("&" "%" "#"))
1525 (if (equal (match-string 1) "\\")
1526 (replace-match (match-string 2) t t
)
1527 (replace-match (concat (match-string 1) "\\"
1528 (match-string 2)) t t
)
1530 ((equal (match-string 2) "...")
1532 (concat (match-string 1)
1533 (org-export-latex-protect-string "\\ldots{}")) t t
))
1534 ((equal (match-string 2) "~")
1535 (cond ((equal (match-string 1) "\\") nil
)
1536 ((eq 'org-link
(get-text-property 0 'face
(match-string 2)))
1537 (replace-match (concat (match-string 1) "\\~") t t
))
1539 (org-export-latex-protect-string
1540 (concat (match-string 1) "\\~{}")) t t
))))
1541 ((member (match-string 2) '("{" "}"))
1542 (unless (save-match-data (org-inside-latex-math-p))
1543 (if (equal (match-string 1) "\\")
1544 (replace-match (match-string 2) t t
)
1545 (replace-match (concat (match-string 1) "\\"
1546 (match-string 2)) t t
)))))
1547 (unless (save-match-data (org-inside-latex-math-p))
1548 (cond ((equal (match-string 2) "\\")
1549 (replace-match (or (save-match-data
1550 (org-export-latex-treat-backslash-char
1552 (or (match-string 3) "")))
1554 (when (and (get-text-property (1- (point)) 'org-entity
)
1556 ;; OK, this was an entity replacement, and the user
1557 ;; had terminated the entity with {}. Make sure
1558 ;; {} is protected as well, and remove the extra {}
1559 ;; inserted by the conversion.
1560 (put-text-property (point) (+ 2 (point)) 'org-protected t
)
1561 (if (save-excursion (goto-char (max (- (point) 2) (point-min)))
1566 ((member (match-string 2) '("_" "^"))
1567 (replace-match (or (save-match-data
1568 (org-export-latex-treat-sub-super-char
1572 (match-string 3))) "") t t
)
1573 (backward-char 1)))))))
1574 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
1575 "\\(\\(\\\\?\\$\\)\\)"
1576 "\\([a-zA-Z0-9()]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-zA-Z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-zA-Z0-9]+}\\|([a-zA-Z0-9]+)\\)"
1577 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|\\([&#%{}\"]\\|[a-zA-Z][a-zA-Z0-9]*\\)\\)"
1578 "\\(.\\|^\\)\\(&\\)"
1579 "\\(.\\|^\\)\\(#\\)"
1580 "\\(.\\|^\\)\\(%\\)"
1581 "\\(.\\|^\\)\\({\\)"
1582 "\\(.\\|^\\)\\(}\\)"
1583 "\\(.\\|^\\)\\(~\\)"
1584 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
1585 ;; (?\< . "\\textless{}")
1586 ;; (?\> . "\\textgreater{}")
1589 (defun org-inside-latex-math-p ()
1590 (get-text-property (point) 'org-latex-math
))
1592 (defun org-export-latex-treat-sub-super-char
1593 (subsup char string-before string-after
)
1594 "Convert the \"_\" and \"^\" characters to LaTeX.
1595 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
1596 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
1597 (cond ((equal string-before
"\\")
1598 (concat string-before char string-after
))
1599 ((and (string-match "\\S-+" string-after
))
1600 ;; this is part of a math formula
1601 (cond ((eq 'org-link
(get-text-property 0 'face char
))
1602 (concat string-before
"\\" char string-after
))
1603 ((save-match-data (org-inside-latex-math-p))
1605 (cond ((eq 1 (length string-after
))
1606 (concat string-before char string-after
))
1607 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after
)
1608 (format "%s%s{%s}" string-before char
1609 (match-string 1 string-after
))))))
1610 ((and (> (length string-after
) 1)
1612 (and (equal subsup
'{}) (eq (string-to-char string-after
) ?\
{)))
1613 (or (string-match "[{]?\\([^}]+\\)[}]?" string-after
)
1614 (string-match "[(]?\\([^)]+\\)[)]?" string-after
)))
1616 (org-export-latex-protect-string
1617 (format "%s$%s{%s}$" string-before char
1618 (if (and (> (match-end 1) (1+ (match-beginning 1)))
1619 (not (equal (substring string-after
0 2) "{\\")))
1620 (concat "\\mathrm{" (match-string 1 string-after
) "}")
1621 (match-string 1 string-after
)))))
1622 ((eq subsup t
) (concat string-before
"$" char string-after
"$"))
1623 (t (org-export-latex-protect-string
1624 (concat string-before
"\\" char
"{}" string-after
)))))
1625 (t (org-export-latex-protect-string
1626 (concat string-before
"\\" char
"{}" string-after
)))))
1628 (defun org-export-latex-treat-backslash-char (string-before string-after
)
1629 "Convert the \"$\" special character to LaTeX.
1630 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
1631 (let ((ass (org-entity-get string-after
)))
1635 (concat string-before
1636 (org-export-latex-protect-string
1637 (concat "$" (nth 1 ass
) "$")))
1638 (concat string-before
(org-export-latex-protect-string
1641 ((and (not (string-match "^[ \n\t]" string-after
))
1642 (not (string-match "[ \t]\\'\\|^" string-before
)))
1643 ;; backslash is inside a word
1644 (concat string-before
1645 (org-export-latex-protect-string
1646 (concat "\\textbackslash{}" string-after
))))
1647 ((not (or (equal string-after
"")
1648 (string-match "^[ \t\n]" string-after
)))
1649 ;; backslash might escape a character (like \#) or a user TeX
1650 ;; macro (like \setcounter)
1651 (concat string-before
1652 (org-export-latex-protect-string (concat "\\" string-after
))))
1653 ((and (string-match "^[ \t\n]" string-after
)
1654 (string-match "[ \t\n]\\'" string-before
))
1655 ;; backslash is alone, convert it to $\backslash$
1656 (org-export-latex-protect-string
1657 (concat string-before
"\\textbackslash{}" string-after
)))
1658 (t (org-export-latex-protect-string
1659 (concat string-before
"\\textbackslash{}" string-after
))))))
1661 (defun org-export-latex-keywords ()
1662 "Convert special keywords to LaTeX."
1663 (goto-char (point-min))
1664 (while (re-search-forward org-export-latex-special-keyword-regexp nil t
)
1665 (replace-match (format org-export-latex-timestamp-keyword-markup
1666 (match-string 0)) t t
)
1668 (beginning-of-line 1)
1669 (unless (looking-at ".*\n[ \t]*\n")
1673 (defun org-export-latex-fixed-width (opt)
1674 "When OPT is non-nil convert fixed-width sections to LaTeX."
1675 (goto-char (point-min))
1676 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t
)
1677 (unless (get-text-property (point) 'org-example
)
1679 (progn (goto-char (match-beginning 0))
1680 (insert "\\begin{verbatim}\n")
1681 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1682 (replace-match (concat (match-string 1)
1683 (match-string 2)) t t
)
1685 (insert "\\end{verbatim}\n\n"))
1686 (progn (goto-char (match-beginning 0))
1687 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1688 (replace-match (concat "%" (match-string 1)
1689 (match-string 2)) t t
)
1690 (forward-line)))))))
1692 (defvar org-table-last-alignment
) ; defined in org-table.el
1693 (defvar org-table-last-column-widths
) ; defined in org-table.el
1694 (declare-function orgtbl-to-latex
"org-table" (table params
) t
)
1695 (defun org-export-latex-tables (insert)
1696 "Convert tables to LaTeX and INSERT it."
1697 ;; First, get the table.el tables
1698 (goto-char (point-min))
1699 (while (re-search-forward "^[ \t]*\\(\\+-[-+]*\\+\\)[ \t]*\n[ \t]*|" nil t
)
1702 (org-export-latex-convert-table.el-table
)))
1704 ;; And now the Org-mode tables
1705 (goto-char (point-min))
1706 (while (re-search-forward "^\\([ \t]*\\)|" nil t
)
1707 (org-if-unprotected-at (1- (point))
1709 (let* ((beg (org-table-begin))
1710 (end (org-table-end))
1711 (raw-table (buffer-substring beg end
))
1712 (org-table-last-alignment (copy-sequence org-table-last-alignment
))
1713 (org-table-last-column-widths (copy-sequence
1714 org-table-last-column-widths
))
1715 fnum fields line lines olines gr colgropen line-fmt align
1716 caption shortn label attr floatp placement longtblp
)
1717 (if org-export-latex-tables-verbatim
1718 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1719 "\\end{verbatim}\n")))
1720 (apply 'delete-region
(list beg end
))
1721 (insert (org-export-latex-protect-string tbl
)))
1723 (setq caption
(org-find-text-property-in-string
1724 'org-caption raw-table
)
1725 shortn
(org-find-text-property-in-string
1726 'org-caption-shortn raw-table
)
1727 attr
(org-find-text-property-in-string
1728 'org-attributes raw-table
)
1729 label
(org-find-text-property-in-string
1730 'org-label raw-table
)
1731 longtblp
(and attr
(stringp attr
)
1732 (string-match "\\<longtable\\>" attr
))
1733 align
(and attr
(stringp attr
)
1734 (string-match "\\<align=\\([^ \t\n\r]+\\)" attr
)
1735 (match-string 1 attr
))
1736 floatp
(or caption label
)
1737 placement
(if (and attr
1739 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr
))
1740 (match-string 1 attr
)
1742 (setq caption
(and caption
(org-export-latex-fontify-headline caption
)))
1743 (setq lines
(org-split-string raw-table
"\n"))
1744 (apply 'delete-region
(list beg end
))
1745 (when org-export-table-remove-special-lines
1746 (setq lines
(org-table-clean-before-export lines
'maybe-quoted
)))
1747 (when org-table-clean-did-remove-column
1748 (pop org-table-last-alignment
)
1749 (pop org-table-last-column-widths
))
1750 ;; make a formatting string to reflect alignment
1752 (while (and (not line-fmt
) (setq line
(pop olines
)))
1753 (unless (string-match "^[ \t]*|-" line
)
1754 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
1755 (setq fnum
(make-vector (length fields
) 0))
1759 (setq gr
(pop org-table-colgroup-info
))
1761 (cond ((eq gr
:start
)
1762 (prog1 (if colgropen
"|" "|")
1763 (setq colgropen t
)))
1765 (prog1 (if colgropen
"|" "|")
1766 (setq colgropen nil
)))
1768 (if (memq gr
'(:end
:startend
))
1769 (progn (setq colgropen nil
) "|")
1772 ;; fix double || in line-fmt
1773 (setq line-fmt
(replace-regexp-in-string "||" "|" line-fmt
))
1774 ;; maybe remove the first and last "|"
1775 (when (and (not org-export-latex-tables-column-borders
)
1776 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt
))
1777 (setq line-fmt
(match-string 2 line-fmt
)))
1780 (setq align
(apply 'format
1782 (mapcar (lambda (x) (if x
"r" "l"))
1783 org-table-last-alignment
)))))
1784 ;; prepare the table to send to orgtbl-to-latex
1788 (or (and (string-match "[ \t]*|-+" elem
) 'hline
)
1789 (org-split-string (org-trim elem
) "|")))
1792 (insert (org-export-latex-protect-string
1795 (concat "\\begin{longtable}{" align
"}\n")
1796 (if floatp
(format "\\begin{table}%s\n" placement
)))
1799 "\\caption%s{%s} %s"
1800 (if shortn
(concat "[" shortn
"]") "")
1802 (if label
(format "\\label{%s}" label
) "")))
1803 (if (and longtblp caption
) "\\\\\n" "\n")
1804 (if (and org-export-latex-tables-centered
(not longtblp
))
1805 "\\begin{center}\n")
1807 (format "\\begin{%s}{%s}\n"
1808 org-export-latex-tabular-environment align
))
1811 `(:tstart nil
:tend nil
1812 :hlend
,(if longtblp
1816 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
1818 \\endlastfoot" (length org-table-last-alignment
))
1821 (format "\n\\end{%s}"
1822 org-export-latex-tabular-environment
))
1823 (if longtblp
"\n" (if org-export-latex-tables-centered
1824 "\n\\end{center}\n" "\n"))
1827 (if floatp
"\\end{table}"))))
1830 (defun org-export-latex-convert-table.el-table
()
1831 "Replace table.el table at point with LaTeX code."
1832 (let (tbl caption shortn label line floatp attr align rmlines
)
1833 (setq line
(buffer-substring (point-at-bol) (point-at-eol))
1834 label
(org-get-text-property-any 0 'org-label line
)
1835 caption
(org-get-text-property-any 0 'org-caption line
)
1836 shortn
(org-get-text-property-any 0 'org-caption-shortn line
)
1837 attr
(org-get-text-property-any 0 'org-attributes line
)
1838 align
(and attr
(stringp attr
)
1839 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr
)
1840 (match-string 1 attr
))
1841 rmlines
(and attr
(stringp attr
)
1842 (string-match "\\<rmlines\\>" attr
))
1843 floatp
(or label caption
))
1844 (and (get-buffer "*org-export-table*")
1845 (kill-buffer (get-buffer "*org-export-table*")))
1846 (table-generate-source 'latex
"*org-export-table*" "caption")
1847 (setq tbl
(with-current-buffer "*org-export-table*"
1849 (while (string-match "^%.*\n" tbl
)
1850 (setq tbl
(replace-match "" t t tbl
)))
1854 (setq lines
(mapcar (lambda (x)
1855 (if (string-match "^\\\\hline$" x
)
1860 (org-split-string tbl
"\n")))
1861 (setq tbl
(mapconcat 'identity
(delq nil lines
) "\n"))))
1862 (when (and align
(string-match "\\\\begin{tabular}{.*}" tbl
))
1863 (setq tbl
(replace-match (concat "\\begin{tabular}{" align
"}")
1865 (and (get-buffer "*org-export-table*")
1866 (kill-buffer (get-buffer "*org-export-table*")))
1867 (beginning-of-line 0)
1868 (while (looking-at "[ \t]*\\(|\\|\\+-\\)")
1869 (delete-region (point) (1+ (point-at-eol))))
1870 (when org-export-latex-tables-centered
1871 (setq tbl
(concat "\\begin{center}\n" tbl
"\\end{center}")))
1873 (setq tbl
(concat "\\begin{table}\n"
1874 (format "\\caption%s{%s}%s\n"
1875 (if shortn
(format "[%s]" shortn
) "")
1876 (if label
(format "\\label{%s}" label
) "")
1879 "\n\\end{table}\n")))
1880 (insert (org-export-latex-protect-string tbl
))))
1882 (defun org-export-latex-fontify ()
1883 "Convert fontification to LaTeX."
1884 (goto-char (point-min))
1885 (while (re-search-forward org-emph-re nil t
)
1886 ;; The match goes one char after the *string*, except at the end of a line
1887 (let ((emph (assoc (match-string 3)
1888 org-export-latex-emphasis-alist
))
1889 (beg (match-beginning 0))
1893 (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
1895 (unless (or (and (get-text-property (- (point) 2) 'org-protected
)
1896 (not (get-text-property
1897 (- (point) 2) 'org-verbatim-emph
)))
1898 (equal (char-after (match-beginning 3))
1899 (char-after (1+ (match-beginning 3))))
1901 (goto-char (match-beginning 1))
1903 (and (org-at-table-p)
1905 "[|\n]" (buffer-substring beg end
)))))
1906 (and (equal (match-string 3) "+")
1908 (string-match "\\`-+\\'" (match-string 4)))))
1909 (setq s
(match-string 4))
1910 (setq rpl
(concat (match-string 1)
1911 (org-export-latex-emph-format (cadr emph
)
1915 (setq rpl
(org-export-latex-protect-string rpl
))
1917 (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl
)
1919 (add-text-properties (match-beginning 1) (match-end 1)
1920 '(org-protected t
) rpl
)
1921 (add-text-properties (match-beginning 3) (match-end 3)
1922 '(org-protected t
) rpl
)))))
1923 (replace-match rpl t t
)))
1926 (defvar org-export-latex-use-verb nil
)
1927 (defun org-export-latex-emph-format (format string
)
1928 "Format an emphasis string and handle the \\verb special case."
1929 (when (equal format
"\\verb")
1931 (if org-export-latex-use-verb
1932 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1934 (loop for i from
0 to
(1- (length ll
)) do
1935 (if (not (string-match (regexp-quote (substring ll i
(1+ i
)))
1938 (setq format
(concat "\\verb" (substring ll i
(1+ i
))
1939 "%s" (substring ll i
(1+ i
))))
1940 (throw 'exit nil
))))))
1942 (trans '(("\\" .
"\\textbackslash{}")
1943 ("~" .
"\\textasciitilde{}")
1944 ("^" .
"\\textasciicircum{}")))
1946 (while (string-match "[\\{}$%&_#~^]" string
)
1947 (setq char
(match-string 0 string
))
1948 (if (> (match-beginning 0) 0)
1949 (setq rtn
(concat rtn
(substring string
1950 0 (match-beginning 0)))))
1951 (setq string
(substring string
(1+ (match-beginning 0))))
1952 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
1953 rtn
(concat rtn char
)))
1954 (setq string
(concat rtn string
) format
"\\texttt{%s}")))))
1955 (format format string
))
1957 (defun org-export-latex-links ()
1958 ;; Make sure to use the LaTeX hyperref and graphicx package
1959 ;; or send some warnings.
1960 "Convert links to LaTeX."
1961 (goto-char (point-min))
1962 (while (re-search-forward org-bracket-link-analytic-regexp
++ nil t
)
1963 (org-if-unprotected-1
1964 (goto-char (match-beginning 0))
1965 (let* ((re-radio org-export-latex-all-targets-re
)
1966 (remove (list (match-beginning 0) (match-end 0)))
1967 (raw-path (org-extract-attributes (match-string 3)))
1968 (full-raw-path (concat (match-string 1) raw-path
))
1969 (desc (match-string 5))
1970 (type (or (match-string 2)
1971 (if (or (file-name-absolute-p raw-path
)
1972 (string-match "^\\.\\.?/" raw-path
))
1974 (coderefp (equal type
"coderef"))
1975 (caption (org-find-text-property-in-string 'org-caption raw-path
))
1976 (shortn (org-find-text-property-in-string 'org-caption-shortn raw-path
))
1977 (attr (or (org-find-text-property-in-string 'org-attributes raw-path
)
1978 (plist-get org-export-latex-options-plist
:latex-image-options
)))
1979 (label (org-find-text-property-in-string 'org-label raw-path
))
1981 ;; define the path of the link
1983 ((member type
'("coderef"))
1985 ((member type
'("http" "https" "ftp"))
1986 (concat type
":" raw-path
))
1987 ((and re-radio
(string-match re-radio raw-path
))
1989 ((equal type
"mailto")
1990 (concat type
":" raw-path
))
1991 ((equal type
"file")
1992 (if (and (org-file-image-p
1995 org-export-latex-inline-image-extensions
)
1996 (or (get-text-property 0 'org-no-description
1998 (equal desc full-raw-path
)))
2000 (progn (when (string-match "\\(.+\\)::.+" raw-path
)
2001 (setq raw-path
(match-string 1 raw-path
)))
2002 (if (file-exists-p raw-path
)
2003 (concat type
"://" (expand-file-name raw-path
))
2004 (concat type
"://" (org-export-directory
2005 :LaTeX org-export-latex-options-plist
)
2007 ;; process with link inserting
2008 (apply 'delete-region remove
)
2009 (setq caption
(and caption
(org-export-latex-fontify-headline caption
)))
2011 (plist-get org-export-latex-options-plist
:inline-images
))
2012 ;; OK, we need to inline an image
2014 (org-export-latex-format-image raw-path caption label attr shortn
)))
2017 (org-export-get-coderef-format path desc
)
2018 (cdr (assoc path org-export-code-refs
)))))
2019 (radiop (insert (format "\\hyperref[%s]{%s}"
2020 (org-solidify-link-text raw-path
) desc
)))
2022 (insert (format "\\hyperref[%s]{%s}"
2023 (org-remove-initial-hash
2024 (org-solidify-link-text raw-path
))
2027 (when (org-at-table-p)
2028 ;; There is a strange problem when we have a link in a table,
2029 ;; ampersands then cause a problem. I think this must be
2030 ;; a LaTeX issue, but we here implement a work-around anyway.
2031 (setq path
(org-export-latex-protect-amp path
)
2032 desc
(org-export-latex-protect-amp desc
)))
2033 (insert (format org-export-latex-hyperref-format path desc
)))
2035 ((functionp (setq fnc
(nth 2 (assoc type org-link-protocols
))))
2036 ;; The link protocol has a function for formatting the link
2039 (funcall fnc
(org-link-unescape raw-path
) desc
'latex
))))
2041 (t (insert "\\texttt{" desc
"}")))))))
2044 (defun org-export-latex-format-image (path caption label attr
&optional shortn
)
2045 "Format the image element, depending on user settings."
2046 (let (ind floatp wrapp multicolumnp placement figenv
)
2047 (setq floatp
(or caption label
))
2048 (setq ind
(org-get-text-property-any 0 'original-indentation path
))
2049 (when (and attr
(stringp attr
))
2050 (if (string-match "[ \t]*\\<wrap\\>" attr
)
2051 (setq wrapp t floatp nil attr
(replace-match "" t t attr
)))
2052 (if (string-match "[ \t]*\\<float\\>" attr
)
2053 (setq wrapp nil floatp t attr
(replace-match "" t t attr
)))
2054 (if (string-match "[ \t]*\\<multicolumn\\>" attr
)
2055 (setq multicolumnp t attr
(replace-match "" t t attr
))))
2059 (wrapp "{l}{0.5\\textwidth}")
2063 (when (and attr
(stringp attr
)
2064 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr
))
2065 (setq placement
(match-string 1 attr
)
2066 attr
(replace-match "" t t attr
)))
2067 (setq attr
(and attr
(org-trim attr
)))
2068 (when (or (not attr
) (= (length attr
) 0))
2069 (setq attr
(cond (floatp "width=0.7\\textwidth")
2070 (wrapp "width=0.48\\textwidth")
2074 (wrapp "\\begin{wrapfigure}%placement
2076 \\includegraphics[%attr]{%path}
2077 \\caption%shortn{%labelcmd%caption}
2079 (multicolumnp "\\begin{figure*}%placement
2081 \\includegraphics[%attr]{%path}
2082 \\caption{%labelcmd%caption}
2084 (floatp "\\begin{figure}%placement
2086 \\includegraphics[%attr]{%path}
2087 \\caption{%labelcmd%caption}
2089 (t "\\includegraphics[%attr]{%path}")))
2092 (setq figenv
(mapconcat 'identity
(split-string figenv
"\n")
2093 (save-excursion (beginning-of-line 1)
2094 (looking-at "[ \t]*")
2095 (concat "\n" (match-string 0)))))
2097 (if (and (not label
) (not caption
)
2098 (string-match "^\\\\caption{.*\n" figenv
))
2099 (setq figenv
(replace-match "" t t figenv
)))
2104 (if (file-name-absolute-p path
)
2105 (expand-file-name path
)
2108 (cons "shortn" (if shortn
(format "[%s]" shortn
) ""))
2109 (cons "labelcmd" (if label
(format "\\label{%s}"
2111 (cons "caption" (or caption
""))
2112 (cons "placement" (or placement
""))))
2113 nil
'original-indentation ind
)))
2115 (defun org-export-latex-protect-amp (s)
2116 (while (string-match "\\([^\\\\]\\)\\(&\\)" s
)
2117 (setq s
(replace-match (concat (match-string 1 s
) "\\" (match-string 2 s
))
2121 (defun org-remove-initial-hash (s)
2122 (if (string-match "\\`#" s
)
2125 (defvar org-latex-entities
) ; defined below
2126 (defvar org-latex-entities-regexp
) ; defined below
2128 (defun org-export-latex-preprocess (parameters)
2129 "Clean stuff in the LaTeX export."
2130 ;; Preserve line breaks
2131 (goto-char (point-min))
2132 (while (re-search-forward "\\\\\\\\" nil t
)
2133 (add-text-properties (match-beginning 0) (match-end 0)
2134 '(org-protected t
)))
2136 ;; Preserve latex environments
2137 (goto-char (point-min))
2138 (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t
)
2140 (let* ((start (progn (beginning-of-line) (point)))
2141 (end (and (re-search-forward
2142 (concat "^[ \t]*\\\\end{"
2143 (regexp-quote (match-string 1))
2147 (add-text-properties start end
'(org-protected t
))
2148 (goto-char (point-at-eol))))))
2150 ;; Preserve math snippets
2152 (let* ((matchers (plist-get org-format-latex-options
:matchers
))
2153 (re-list org-latex-regexps
)
2154 beg end re e m n block off
)
2155 ;; Check the different regular expressions
2156 (while (setq e
(pop re-list
))
2157 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
2158 block
(if (nth 3 e
) "\n\n" ""))
2159 (setq off
(if (member m
'("$" "$1")) 1 0))
2160 (when (and (member m matchers
) (not (equal m
"begin")))
2161 (goto-char (point-min))
2162 (while (re-search-forward re nil t
)
2163 (setq beg
(+ (match-beginning 0) off
) end
(- (match-end 0) 0))
2164 (add-text-properties beg end
'(org-protected t org-latex-math t
))))))
2166 ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{}
2167 (goto-char (point-min))
2168 (let ((case-fold-search nil
))
2169 (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t
)
2170 (unless (eq (char-before (match-beginning 1)) ?
\\)
2171 (org-if-unprotected-1
2172 (replace-match (org-export-latex-protect-string
2173 (concat "\\" (match-string 1)
2176 ;; Convert blockquotes
2177 (goto-char (point-min))
2178 (while (search-forward "ORG-BLOCKQUOTE-START" nil t
)
2179 (org-replace-match-keep-properties "\\begin{quote}" t t
))
2180 (goto-char (point-min))
2181 (while (search-forward "ORG-BLOCKQUOTE-END" nil t
)
2182 (org-replace-match-keep-properties "\\end{quote}" t t
))
2185 (goto-char (point-min))
2186 (while (search-forward "ORG-VERSE-START" nil t
)
2187 (org-replace-match-keep-properties "\\begin{verse}" t t
)
2188 (beginning-of-line 2)
2189 (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
2190 (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
2191 (goto-char (match-end 1))
2192 (org-replace-match-keep-properties
2193 (org-export-latex-protect-string
2194 (concat "\\hspace*{1cm}" (match-string 2))) t t
)
2195 (beginning-of-line 1))
2196 (if (looking-at "[ \t]*$")
2197 (insert (org-export-latex-protect-string "\\vspace*{1em}"))
2198 (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
2201 (beginning-of-line 2))
2202 (and (looking-at "[ \t]*ORG-VERSE-END.*")
2203 (org-replace-match-keep-properties "\\end{verse}" t t
)))
2206 (goto-char (point-min))
2207 (while (search-forward "ORG-CENTER-START" nil t
)
2208 (org-replace-match-keep-properties "\\begin{center}" t t
))
2209 (goto-char (point-min))
2210 (while (search-forward "ORG-CENTER-END" nil t
)
2211 (org-replace-match-keep-properties "\\end{center}" t t
))
2213 (run-hooks 'org-export-latex-after-blockquotes-hook
)
2215 ;; Convert horizontal rules
2216 (goto-char (point-min))
2217 (while (re-search-forward "^----+.$" nil t
)
2219 (replace-match (org-export-latex-protect-string "\\hrule") t t
)))
2221 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
2222 (goto-char (point-min))
2224 "\\\\\\([a-zA-Z]+\\)"
2225 "\\(?:<[^<>\n]*>\\)*"
2226 "\\(?:\\[[^][\n]*?\\]\\)*"
2227 "\\(?:<[^<>\n]*>\\)*"
2228 "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}")))
2229 (while (re-search-forward re nil t
)
2231 ;; check for comment line
2232 (save-excursion (goto-char (match-beginning 0))
2233 (org-in-indented-comment-line))
2234 ;; Check if this is a defined entity, so that is may need conversion
2235 (org-entity-get (match-string 1))
2237 (add-text-properties (match-beginning 0) (match-end 0)
2238 '(org-protected t
)))))
2240 ;; Special case for \nbsp
2241 (goto-char (point-min))
2242 (while (re-search-forward "\\\\nbsp\\({}\\|\\>\\)" nil t
)
2244 (replace-match (org-export-latex-protect-string "~"))))
2246 ;; Protect LaTeX entities
2247 (goto-char (point-min))
2248 (while (re-search-forward org-latex-entities-regexp nil t
)
2250 (add-text-properties (match-beginning 0) (match-end 0)
2251 '(org-protected t
))))
2253 ;; Replace radio links
2254 (goto-char (point-min))
2255 (while (re-search-forward
2256 (concat "<<<?" org-export-latex-all-targets-re
2257 ">>>?\\((INVISIBLE)\\)?") nil t
)
2258 (org-if-unprotected-at (+ (match-beginning 0) 2)
2261 (org-export-latex-protect-string
2262 (format "\\label{%s}" (save-match-data (org-solidify-link-text
2263 (match-string 1)))))
2264 (if (match-string 2) "" (match-string 1)))
2267 ;; Delete @<...> constructs
2268 ;; Thanks to Daniel Clemente for this regexp
2269 (goto-char (point-min))
2270 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t
)
2272 (replace-match "")))
2274 ;; When converting to LaTeX, replace footnotes
2275 ;; FIXME: don't protect footnotes from conversion
2276 (when (plist-get org-export-latex-options-plist
:footnotes
)
2277 (goto-char (point-min))
2278 (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t
)
2280 (when (and (save-match-data
2281 (save-excursion (beginning-of-line)
2282 (looking-at "[^:|#]")))
2283 (not (org-in-verbatim-emphasis)))
2284 (let ((foot-beg (match-beginning 0))
2285 (foot-end (match-end 0))
2286 (foot-prefix (match-string 0))
2287 footnote footnote-rpl
)
2289 (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix
))
2291 (replace-match (org-export-latex-protect-string
2292 (concat "$^{" (match-string 1) "}$")))
2294 (let ((end (save-excursion
2295 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t
)
2296 (match-beginning 0) (point-max)))))
2297 (setq footnote
(concat (org-trim (buffer-substring (point) end
))
2298 " ")) ; prevent last } being part of a link
2299 (delete-region (point) end
))
2300 (goto-char foot-beg
)
2301 (delete-region foot-beg foot-end
)
2302 (unless (null footnote
)
2303 (setq footnote-rpl
(format "\\footnote{%s}" footnote
))
2304 (add-text-properties 0 10 '(org-protected t
) footnote-rpl
)
2305 (add-text-properties (1- (length footnote-rpl
))
2306 (length footnote-rpl
)
2307 '(org-protected t
) footnote-rpl
)
2308 (if (org-on-heading-p)
2310 (concat (org-export-latex-protect-string "\\protect")
2312 (insert footnote-rpl
)))
2315 ;; Remove footnote section tag for LaTeX
2316 (goto-char (point-min))
2317 (while (re-search-forward
2318 (concat "^" footnote-section-tag-regexp
) nil t
)
2320 (replace-match "")))))
2322 (defun org-export-latex-fix-inputenc ()
2323 "Set the coding system in inputenc to what the buffer is."
2324 (let* ((cs buffer-file-coding-system
)
2325 (opt (or (ignore-errors (latexenc-coding-system-to-inputenc cs
))
2328 ;; Translate if that is requested
2329 (setq opt
(or (cdr (assoc opt org-export-latex-inputenc-alist
)) opt
))
2330 ;; find the \usepackage statement and replace the option
2331 (goto-char (point-min))
2332 (while (re-search-forward "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
2334 (goto-char (match-beginning 1))
2335 (delete-region (match-beginning 1) (match-end 1))
2337 (and buffer-file-name
2342 (defun org-export-latex-lists ()
2343 "Convert plain text lists in current buffer into LaTeX lists."
2345 (goto-char (point-min))
2346 (while (org-search-forward-unenclosed org-item-beginning-re nil t
)
2348 (setq res
(org-list-to-latex (org-list-parse-list t
)
2349 org-export-latex-list-parameters
))
2350 (while (string-match "^\\(\\\\item[ \t]+\\)\\[@\\(?:start:\\)?\\([0-9]+\\)\\]"
2352 (setq res
(replace-match
2353 (concat (format "\\setcounter{enumi}{%d}"
2354 (1- (string-to-number
2355 (match-string 2 res
))))
2357 (match-string 1 res
))
2359 (insert res
"\n"))))
2361 (defconst org-latex-entities
2390 "\\begin{description}"
2391 "\\begin{enumerate}"
2395 "\\begin{flushleft}"
2396 "\\begin{flushright}"
2401 "\\begin{quotation}"
2406 "\\begin{thebibliography}"
2408 "\\begin{titlepage}"
2535 "A list of LaTeX commands to be protected when performing conversion.")
2537 (defconst org-latex-entities-regexp
2539 (dolist (x org-latex-entities
)
2540 (if (string-match "[a-zA-Z]$" x
)
2543 (concat "\\(" (regexp-opt (nreverse names
)) "\\>\\)"
2544 "\\|\\(" (regexp-opt (nreverse rest
)) "\\)")))
2546 (provide 'org-export-latex
)
2547 (provide 'org-latex
)
2549 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
2551 ;;; org-latex.el ends here