1 ;;; org-latex.el --- LaTeX exporter for org-mode
3 ;; Copyright (C) 2007, 2008, 2009 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 \\usepackage[AUTO]{inputenc}
96 \\usepackage[T1]{fontenc}
97 \\usepackage{graphicx}
98 \\usepackage{longtable}
100 \\usepackage{wrapfig}
102 \\usepackage{latexsym}
103 \\usepackage{amssymb}
104 \\usepackage{hyperref}"
105 ("\\section{%s}" .
"\\section*{%s}")
106 ("\\subsection{%s}" .
"\\subsection*{%s}")
107 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
108 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
109 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
111 "\\documentclass[11pt]{report}
112 \\usepackage[AUTO]{inputenc}
113 \\usepackage[T1]{fontenc}
114 \\usepackage{graphicx}
115 \\usepackage{longtable}
117 \\usepackage{wrapfig}
119 \\usepackage{latexsym}
120 \\usepackage{amssymb}
121 \\usepackage{hyperref}"
122 ("\\part{%s}" .
"\\part*{%s}")
123 ("\\chapter{%s}" .
"\\chapter*{%s}")
124 ("\\section{%s}" .
"\\section*{%s}")
125 ("\\subsection{%s}" .
"\\subsection*{%s}")
126 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
128 "\\documentclass[11pt]{book}
129 \\usepackage[AUTO]{inputenc}
130 \\usepackage[T1]{fontenc}
131 \\usepackage{graphicx}
132 \\usepackage{longtable}
134 \\usepackage{wrapfig}
136 \\usepackage{latexsym}
137 \\usepackage{amssymb}
138 \\usepackage{hyperref}"
139 ("\\part{%s}" .
"\\part*{%s}")
140 ("\\chapter{%s}" .
"\\chapter*{%s}")
141 ("\\section{%s}" .
"\\section*{%s}")
142 ("\\subsection{%s}" .
"\\subsection*{%s}")
143 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
145 "\\documentclass{beamer}
146 \\usepackage[AUTO]{inputenc}
147 \\usepackage[T1]{fontenc}
148 \\usepackage{graphicx}
149 \\usepackage{longtable}
151 \\usepackage{wrapfig}
153 \\usepackage{latexsym}
154 \\usepackage{amssymb}
155 \\usepackage{hyperref}"
156 org-beamer-sectioning
158 "Alist of LaTeX classes and associated header and structure.
159 If #+LaTeX_CLASS is set in the buffer, use its value and the
160 associated information. Here is the structure of each cell:
164 (numbered-section . unnumbered-section\)
170 The HEADER-STRING is the header that will be inserted into the LaTeX file.
171 It should contain the \\documentclass macro, package call via \\usepackage
172 and anything else you would always like to have in the header. Note that
173 the header will be augmented with additional usepackage statements
174 according to the variable `org-export-latex-packages-alist', and also with
175 lines specified via \"#+LaTeX_HEADER:\".
176 If the header definition contains \"\\usepackage[AUTO]{inputenc}\", AUTO
177 will automatically be replaced with a coding system derived from
178 `buffer-file-coding-system'. See also the variable
179 `org-export-latex-inputenc-alist' for a way to influence this mechanism.
182 The sectioning structure
183 ------------------------
185 The sectioning structure of the class is given by the elements following
186 the header string. For ech sectioning level, a number of strings is
187 specified. A %s formatter is mandatory in each section string and will
188 be replaced by the title of the section.
190 Instead of a cons cell (numbered . unnumbered), you can also provide a list
193 (numbered-open numbered-close)
197 (numbered-open numbered-close unnumbered-open unnumbered-close)
199 providing opening and closing strings for an environment that should
200 represent the document section. The opening clause should have a %s
201 to represent the section title.
203 Instead of a list of sectioning commands, you can also specify a
204 function name. That function will be called with two parameters,
205 the (reduced) level of the headline, and the headline text. The functions
206 returns a cons cell with the (possibly modified) headline text, and the
207 sectioning list in the cdr."
208 :group
'org-export-latex
210 (list (string :tag
"LaTeX class")
211 (string :tag
"LaTeX header")
212 (repeat :tag
"Levels" :inline t
215 (string :tag
" numbered")
216 (string :tag
"unnumbered"))
217 (list :tag
"Environment"
218 (string :tag
"Opening (numbered)")
219 (string :tag
"Closing (numbered)")
220 (string :tag
"Opening (unnumbered)")
221 (string :tag
"Closing (unnumbered)"))
222 (function :tag
"Hook computing sectioning"))))))
224 (defcustom org-export-latex-inputenc-alist nil
225 "Alist of inputenc coding system names, and what should really be used.
226 For example, adding an entry
228 (\"utf8\" . \"utf8x\")
230 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
231 are written as utf8 files."
232 :group
'org-export-latex
235 (string :tag
"Derived from buffer")
236 (string :tag
"Use this instead"))))
239 (defcustom org-export-latex-emphasis-alist
240 '(("*" "\\textbf{%s}" nil
)
241 ("/" "\\emph{%s}" nil
)
242 ("_" "\\underline{%s}" nil
)
246 "Alist of LaTeX expressions to convert emphasis fontifiers.
247 Each element of the list is a list of three elements.
248 The first element is the character used as a marker for fontification.
249 The second element is a formatting string to wrap fontified text with.
250 If it is \"\\verb\", Org will automatically select a delimiter
251 character that is not in the string.
252 The third element decides whether to protect converted text from other
254 :group
'org-export-latex
257 (defcustom org-export-latex-title-command
"\\maketitle"
258 "The command used to insert the title just after \\begin{document}.
259 If this string contains the formatting specification \"%s\" then
260 it will be used as a formatting string, passing the title as an
262 :group
'org-export-latex
265 (defcustom org-export-latex-import-inbuffer-stuff nil
266 "Non-nil means define TeX macros for Org's inbuffer definitions.
267 For example \orgTITLE for #+TITLE."
268 :group
'org-export-latex
271 (defcustom org-export-latex-date-format
273 "Format string for \\date{...}."
274 :group
'org-export-latex
277 (defcustom org-export-latex-todo-keyword-markup
"\\textbf{%s}"
278 "Markup for TODO keywords, as a printf format.
279 This can be a single format for all keywords, a cons cell with separate
280 formats for not-done and done states, or an association list with setup
281 for individual keywords. If a keyword shows up for which there is no
282 markup defined, the first one in the association list will be used."
283 :group
'org-export-latex
285 (string :tag
"Default")
286 (cons :tag
"Distinguish undone and done"
287 (string :tag
"Not-DONE states")
288 (string :tag
"DONE states"))
289 (repeat :tag
"Per keyword markup"
291 (string :tag
"Keyword")
292 (string :tag
"Markup")))))
294 (defcustom org-export-latex-timestamp-markup
"\\textit{%s}"
295 "A printf format string to be applied to time stamps."
296 :group
'org-export-latex
299 (defcustom org-export-latex-timestamp-keyword-markup
"\\texttt{%s}"
300 "A printf format string to be applied to time stamps."
301 :group
'org-export-latex
304 (defcustom org-export-latex-hyperref-format
"\\href{%s}{%s}"
305 "A printf format string to be applied to hyperref links.
306 The format must contain two %s instances. The first will be filled with
307 the link, the second with the link description."
308 :group
'org-export-latex
311 (defcustom org-export-latex-tables-verbatim nil
312 "When non-nil, tables are exported verbatim."
313 :group
'org-export-latex
316 (defcustom org-export-latex-tables-centered t
317 "When non-nil, tables are exported in a center environment."
318 :group
'org-export-latex
321 (defcustom org-export-latex-tables-column-borders nil
322 "When non-nil, grouping columns can cause outer vertical lines in tables.
323 When nil, grouping causes only separation lines between groups."
324 :group
'org-export-latex
327 (defcustom org-export-latex-low-levels
'itemize
328 "How to convert sections below the current level of sectioning.
329 This is specified by the `org-export-headline-levels' option or the
330 value of \"H:\" in Org's #+OPTION line.
332 This can be either nil (skip the sections), `description', `itemize',
333 or `enumerate' (convert the sections as the corresponding list type), or
334 a string to be used instead of \\section{%s}. In this latter case,
335 the %s stands here for the inserted headline and is mandatory.
337 It may also be a list of three string to define a user-defined environment
338 that should be used. The first string should be the like
339 \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
340 to two occurrences of %s for the title and a label, respectively. The third
341 string should be like \"\\end{itemize\"."
342 :group
'org-export-latex
343 :type
'(choice (const :tag
"Ignore" nil
)
344 (const :tag
"Convert as descriptive list" description
)
345 (const :tag
"Convert as itemized list" itemize
)
346 (const :tag
"Convert as enumerated list" enumerate
)
347 (list :tag
"User-defined environment"
348 :value
("\\begin{itemize}" "\\end{itemize}" "\\item %s")
349 (string :tag
"Start")
351 (string :tag
"item"))
352 (string :tag
"Use a section string" :value
"\\subparagraph{%s}")))
354 (defcustom org-export-latex-list-parameters
355 '(:cbon
"$\\boxtimes$" :cboff
"$\\Box$")
356 "Parameters for the LaTeX list exporter.
357 These parameters will be passed on to `org-list-to-latex', which in turn
358 will pass them (combined with the LaTeX default list parameters) to
359 `org-list-to-generic'."
360 :group
'org-export-latex
363 (defcustom org-export-latex-verbatim-wrap
364 '("\\begin{verbatim}\n" .
"\\end{verbatim}\n")
365 "Environment to be wrapped around a fixed-width section in LaTeX export.
366 This is a cons with two strings, to be added before and after the
369 Defaults to \\begin{verbatim} and \\end{verbatim}."
370 :group
'org-export-translation
371 :group
'org-export-latex
372 :type
'(cons (string :tag
"Open")
373 (string :tag
"Close")))
375 (defcustom org-export-latex-listings nil
376 "Non-nil means export source code using the listings package.
377 This package will fontify source code, possibly even with color.
378 If you want to use this, you also need to make LaTeX use the
379 listings package, and if you want to have color, the color
380 package. Just add these to `org-export-latex-packages-alist',
381 for example using customize, or with something like
384 (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
385 (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))"
386 :group
'org-export-latex
389 (defcustom org-export-latex-listings-langs
390 '((emacs-lisp "Lisp") (lisp "Lisp")
393 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
394 (html "HTML") (xml "XML")
395 (tex "TeX") (latex "TeX")
396 (shell-script "bash")
398 (ocaml "Caml") (caml "Caml")
400 "Alist mapping languages to their listing language counterpart.
401 The key is a symbol, the major mode symbol without the \"-mode\".
402 The value is the string that should be inserted as the language parameter
403 for the listings package. If the mode name and the listings name are
404 the same, the language does not need an entry in this list - but it does not
405 hurt if it is present."
406 :group
'org-export-latex
409 (symbol :tag
"Major mode ")
410 (string :tag
"Listings language"))))
412 (defcustom org-export-latex-remove-from-headlines
413 '(:todo nil
:priority nil
:tags nil
)
414 "A plist of keywords to remove from headlines. OBSOLETE.
415 Non-nil means remove this keyword type from the headline.
417 Don't remove the keys, just change their values.
419 Obsolete, this variable is no longer used. Use the separate
420 variables `org-export-with-todo-keywords', `org-export-with-priority',
421 and `org-export-with-tags' instead."
423 :group
'org-export-latex
)
425 (defcustom org-export-latex-image-default-option
"width=10em"
426 "Default option for images."
427 :group
'org-export-latex
430 (defcustom org-export-latex-inline-image-extensions
431 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
432 "Extensions of image files that can be inlined into LaTeX.
433 Note that the image extension *actually* allowed depend on the way the
434 LaTeX file is processed. When used with pdflatex, pdf, jpg and png images
435 are OK. When processing through dvi to Postscript, only ps and eps are
436 allowed. The default we use here encompasses both."
437 :group
'org-export-latex
438 :type
'(repeat (string :tag
"Extension")))
440 (defcustom org-export-latex-coding-system nil
441 "Coding system for the exported LaTex file."
442 :group
'org-export-latex
443 :type
'coding-system
)
445 (defgroup org-export-pdf nil
446 "Options for exporting Org-mode files to PDF, via LaTeX."
447 :tag
"Org Export PDF"
448 :group
'org-export-latex
451 (defcustom org-latex-to-pdf-process
452 '("pdflatex -interaction nonstopmode %s"
453 "pdflatex -interaction nonstopmode %s")
454 "Commands to process a LaTeX file to a PDF file.
455 This is a list of strings, each of them will be given to the shell
456 as a command. %s in the command will be replaced by the full file name, %b
457 by the file base name (i.e. without extension).
458 The reason why this is a list is that it usually takes several runs of
459 pdflatex, maybe mixed with a call to bibtex. Org does not have a clever
460 mechanism to detect which of these commands have to be run to get to a stable
461 result, and it also does not do any error checking.
463 Alternatively, this may be a Lisp function that does the processing, so you
464 could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
465 This function should accept the file name as its single argument."
466 :group
'org-export-pdf
467 :type
'(choice (repeat :tag
"Shell command sequence"
468 (string :tag
"Shell command"))
471 (defcustom org-export-pdf-logfiles
472 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
473 "The list of file extensions to consider as LaTeX logfiles."
474 :group
'org-export-pdf
475 :type
'(repeat (string :tag
"Extension")))
477 (defcustom org-export-pdf-remove-logfiles t
478 "Non-nil means remove the logfiles produced by PDF production.
479 These are the .aux, .log, .out, and .toc files."
480 :group
'org-export-pdf
485 (defvar org-export-latex-after-initial-vars-hook nil
486 "Hook run before LaTeX export.
487 The exact moment is after the initial variables like org-export-latex-class
488 have been determined from the environment.")
490 (defvar org-export-latex-after-blockquotes-hook nil
491 "Hook run during LaTeX export, after blockquote, verse, center are done.")
493 (defvar org-export-latex-final-hook nil
494 "Hook run in the finalized LaTeX buffer.")
496 (defvar org-export-latex-after-save-hook nil
497 "Hook run in the finalized LaTeX buffer, after it has been saved.")
499 ;;; Autoload functions:
502 (defun org-export-as-latex-batch ()
503 "Call `org-export-as-latex', may be used in batch processing.
507 --load=$HOME/lib/emacs/org.el
508 --eval \"(setq org-export-headline-levels 2)\"
509 --visit=MyFile --funcall org-export-as-latex-batch"
510 (org-export-as-latex org-export-headline-levels
'hidden
))
513 (defun org-export-as-latex-to-buffer (arg)
514 "Call `org-export-as-latex` with output to a temporary buffer.
515 No file is created. The prefix ARG is passed through to `org-export-as-latex'."
517 (org-export-as-latex arg nil nil
"*Org LaTeX Export*")
518 (when org-export-show-temporary-export-buffer
519 (switch-to-buffer-other-window "*Org LaTeX Export*")))
522 (defun org-replace-region-by-latex (beg end
)
523 "Replace the region from BEG to END with its LaTeX export.
524 It assumes the region has `org-mode' syntax, and then convert it to
525 LaTeX. This can be used in any buffer. For example, you could
526 write an itemized list in `org-mode' syntax in an LaTeX buffer and
527 then use this command to convert it."
530 (save-window-excursion
532 (setq latex
(org-export-region-as-latex
534 (setq reg
(buffer-substring beg end
)
535 buf
(get-buffer-create "*Org tmp*"))
536 (with-current-buffer buf
540 (setq latex
(org-export-region-as-latex
541 (point-min) (point-max) t
'string
)))
543 (delete-region beg end
)
547 (defun org-export-region-as-latex (beg end
&optional body-only buffer
)
548 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
549 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
550 contents, and only produce the region of converted text, useful for
551 cut-and-paste operations.
552 If BUFFER is a buffer or a string, use/create that buffer as a target
553 of the converted LaTeX. If BUFFER is the symbol `string', return the
554 produced LaTeX as a string and leave no buffer behind. For example,
555 a Lisp program could call this function in the following way:
557 (setq latex (org-export-region-as-latex beg end t 'string))
559 When called interactively, the output buffer is selected, and shown
560 in a window. A non-interactive call will only return the buffer."
562 (when (interactive-p)
563 (setq buffer
"*Org LaTeX Export*"))
564 (let ((transient-mark-mode t
) (zmacs-regions t
)
566 (setq ext-plist
(plist-put ext-plist
:ignore-subtree-p t
))
568 (set-mark (point)) ;; to activate the region
570 (setq rtn
(org-export-as-latex
573 (if (fboundp 'deactivate-mark
) (deactivate-mark))
574 (if (and (interactive-p) (bufferp rtn
))
575 (switch-to-buffer-other-window rtn
)
579 (defun org-export-as-latex (arg &optional hidden ext-plist
580 to-buffer body-only pub-dir
)
581 "Export current buffer to a LaTeX file.
582 If there is an active region, export only the region. The prefix
583 ARG specifies how many levels of the outline should become
584 headlines. The default is 3. Lower levels will be exported
585 depending on `org-export-latex-low-levels'. The default is to
586 convert them as description lists.
587 HIDDEN is obsolete and does nothing.
588 EXT-PLIST is a property list with
589 external parameters overriding org-mode's default settings, but
590 still inferior to file-local settings. When TO-BUFFER is
591 non-nil, create a buffer with that name and export to that
592 buffer. If TO-BUFFER is the symbol `string', don't leave any
593 buffer behind but just return the resulting LaTeX as a string.
594 When BODY-ONLY is set, don't produce the file header and footer,
595 simply return the content of \begin{document}...\end{document},
596 without even the \begin{document} and \end{document} commands.
597 when PUB-DIR is set, use this as the publishing directory."
599 (run-hooks 'org-export-first-hook
)
601 ;; Make sure we have a file name when we need it.
602 (when (and (not (or to-buffer body-only
))
603 (not buffer-file-name
))
604 (if (buffer-base-buffer)
605 (org-set-local 'buffer-file-name
606 (with-current-buffer (buffer-base-buffer)
608 (error "Need a file name to be able to export")))
610 (message "Exporting to LaTeX...")
612 (let ((inhibit-read-only t
))
613 (remove-text-properties (point-min) (point-max)
614 '(:org-license-to-kill nil
))))
615 (org-update-radio-target-regexp)
616 (org-export-latex-set-initial-vars ext-plist arg
)
617 (setq org-export-opt-plist org-export-latex-options-plist
)
618 (org-install-letbind)
619 (run-hooks 'org-export-latex-after-initial-vars-hook
)
620 (let* ((wcf (current-window-configuration))
621 (opt-plist org-export-latex-options-plist
)
622 (region-p (org-region-active-p))
623 (rbeg (and region-p
(region-beginning)))
624 (rend (and region-p
(region-end)))
626 (if (plist-get opt-plist
:ignore-subtree-p
)
631 (and (org-at-heading-p)
632 (>= (org-end-of-subtree t t
) rend
))))))
633 (opt-plist (setq org-export-opt-plist
635 (org-export-add-subtree-options opt-plist rbeg
)
637 ;; Make sure the variable contains the updated values.
638 (org-export-latex-options-plist (setq org-export-opt-plist opt-plist
))
639 ;; The following two are dynamically scoped into other
641 (org-current-export-dir
642 (or pub-dir
(org-export-directory :html opt-plist
)))
643 (org-current-export-file buffer-file-name
)
644 (title (or (and subtree-p
(org-export-get-title-from-subtree))
645 (plist-get opt-plist
:title
)
647 (plist-get opt-plist
:skip-before-1st-heading
))
648 (org-export-grab-title-from-buffer))
649 (and buffer-file-name
650 (file-name-sans-extension
651 (file-name-nondirectory buffer-file-name
)))
656 (file-name-as-directory
658 (org-export-directory :LaTeX ext-plist
)))
659 (file-name-sans-extension
661 (org-entry-get rbeg
"EXPORT_FILE_NAME" t
))
662 (file-name-nondirectory ;sans-extension
664 (error "Don't know which export file to use.")))))
668 (if (equal (file-truename filename
)
669 (file-truename (or buffer-file-name
"dummy.org")))
670 (concat filename
".tex")
672 (buffer (if to-buffer
674 ((eq to-buffer
'string
) (get-buffer-create
675 "*Org LaTeX Export*"))
676 (t (get-buffer-create to-buffer
)))
677 (find-file-noselect filename
)))
678 (odd org-odd-levels-only
)
679 (header (org-export-latex-make-header title opt-plist
))
680 (skip (cond (subtree-p nil
)
682 (t (plist-get opt-plist
:skip-before-1st-heading
))))
683 (text (plist-get opt-plist
:text
))
684 (org-export-preprocess-hook
686 `(lambda () (org-set-local 'org-complex-heading-regexp
687 ,org-export-latex-complex-heading-re
))
688 org-export-preprocess-hook
))
689 (first-lines (if skip
"" (org-export-latex-first-lines
696 (if region-p rend
))))
697 (coding-system (and (boundp 'buffer-file-coding-system
)
698 buffer-file-coding-system
))
699 (coding-system-for-write (or org-export-latex-coding-system
701 (save-buffer-coding-system (or org-export-latex-coding-system
703 (region (buffer-substring
704 (if region-p
(region-beginning) (point-min))
705 (if region-p
(region-end) (point-max))))
707 (and text
(string-match "\\S-" text
)
708 (org-export-preprocess-string
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
)
720 :skip-before-1st-heading skip
723 :LaTeX-fragments nil
)))
725 (org-export-preprocess-string
730 :tags
(plist-get opt-plist
:tags
)
731 :priority
(plist-get opt-plist
:priority
)
732 :footnotes
(plist-get opt-plist
:footnotes
)
733 :drawers
(plist-get opt-plist
:drawers
)
734 :timestamps
(plist-get opt-plist
:timestamps
)
735 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
736 :add-text
(if (eq to-buffer
'string
) nil text
)
737 :skip-before-1st-heading skip
738 :select-tags
(plist-get opt-plist
:select-tags
)
739 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
740 :LaTeX-fragments nil
)))
744 (org-install-letbind)
746 (and (fboundp 'set-buffer-file-coding-system
)
747 (set-buffer-file-coding-system coding-system-for-write
))
749 ;; insert the header and initial document commands
750 (unless (or (eq to-buffer
'string
) body-only
)
753 ;; insert text found in #+TEXT
754 (when (and text
(not (eq to-buffer
'string
)))
755 (insert (org-export-latex-content
756 text
'(lists tables fixed-width keywords
))
759 ;; insert lines before the first headline
761 (insert first-lines
))
763 ;; export the content of headlines
764 (org-export-latex-global
766 (insert string-for-export
)
767 (goto-char (point-min))
768 (when (re-search-forward "^\\(\\*+\\) " nil t
)
769 (let* ((asters (length (match-string 1)))
770 (level (if odd
(- asters
2) (- asters
1))))
771 (setq org-export-latex-add-level
772 (if odd
(1- (/ (1+ asters
) 2)) (1- asters
)))
773 (org-export-latex-parse-global level odd
)))))
776 (unless body-only
(insert "\n\\end{document}"))
778 ;; Attach description terms to the \item macro
779 (goto-char (point-min))
780 (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t
)
781 (delete-region (match-beginning 1) (match-end 1)))
783 ;; Relocate the table of contents
784 (goto-char (point-min))
785 (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t
)
786 (goto-char (point-min))
787 (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t
)
789 (goto-char (point-min))
790 (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t
)
791 (replace-match "\\tableofcontents" t t
)))
793 ;; Cleanup forced line ends in items where they are not needed
794 (goto-char (point-min))
795 (while (re-search-forward
796 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin"
798 (delete-region (match-beginning 1) (match-end 1)))
799 (goto-char (point-min))
800 (while (re-search-forward
801 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*"
803 (if (looking-at "[\n \t]+")
804 (replace-match "\n")))
806 (run-hooks 'org-export-latex-final-hook
)
807 (or to-buffer
(save-buffer))
808 (org-export-latex-fix-inputenc)
809 (run-hooks 'org-export-latex-after-save-hook
)
810 (goto-char (point-min))
811 (or (org-export-push-to-kill-ring "LaTeX")
812 (message "Exporting to LaTeX...done"))
814 (if (eq to-buffer
'string
)
815 (prog1 (buffer-substring (point-min) (point-max))
816 (kill-buffer (current-buffer)))
818 (set-window-configuration wcf
))))
821 (defun org-export-as-pdf (arg &optional hidden ext-plist
822 to-buffer body-only pub-dir
)
823 "Export as LaTeX, then process through to PDF."
825 (message "Exporting to PDF...")
826 (let* ((wconfig (current-window-configuration))
827 (lbuf (org-export-as-latex arg hidden ext-plist
828 to-buffer body-only pub-dir
))
829 (file (buffer-file-name lbuf
))
830 (base (file-name-sans-extension (buffer-file-name lbuf
)))
831 (pdffile (concat base
".pdf"))
832 (cmds org-latex-to-pdf-process
)
833 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
834 (bibtex-p (with-current-buffer lbuf
836 (goto-char (point-min))
837 (re-search-forward "\\\\bibliography{" nil t
))))
839 (with-current-buffer outbuf
(erase-buffer))
840 (message "Processing LaTeX file...")
841 (if (and cmds
(symbolp cmds
))
844 (setq cmd
(pop cmds
))
845 (while (string-match "%b" cmd
)
846 (setq cmd
(replace-match
848 (shell-quote-argument base
))
850 (while (string-match "%s" cmd
)
851 (setq cmd
(replace-match
853 (shell-quote-argument file
))
855 (shell-command cmd outbuf outbuf
)))
856 (message "Processing LaTeX file...done")
857 (if (not (file-exists-p pdffile
))
858 (error "PDF file was not produced")
859 (set-window-configuration wconfig
)
860 (when org-export-pdf-remove-logfiles
861 (dolist (ext org-export-pdf-logfiles
)
862 (setq file
(concat base
"." ext
))
863 (and (file-exists-p file
) (delete-file file
))))
864 (message "Exporting to PDF...done")
868 (defun org-export-as-pdf-and-open (arg)
869 "Export as LaTeX, then process through to PDF, and open."
871 (let ((pdffile (org-export-as-pdf arg
)))
874 (org-open-file pdffile
)
875 (when org-export-kill-product-buffer-when-displayed
876 (kill-buffer (find-buffer-visiting
877 (concat (file-name-sans-extension (buffer-file-name))
879 (error "PDF file was not produced"))))
881 ;;; Parsing functions:
883 (defun org-export-latex-parse-global (level odd
)
884 "Parse the current buffer recursively, starting at LEVEL.
885 If ODD is non-nil, assume the buffer only contains odd sections.
886 Return a list reflecting the document structure."
888 (goto-char (point-min))
889 (let* ((cnt 0) output
890 (depth org-export-latex-sectioning-depth
))
891 (while (org-re-search-forward-unprotected
892 (concat "^\\(\\(?:\\*\\)\\{"
893 (number-to-string (+ (if odd
2 1) level
))
895 ;; make sure that there is no upper heading
899 (org-re-search-forward-unprotected
900 (concat "^\\(\\(?:\\*\\)\\{"
901 (number-to-string level
)
902 "\\}\\) \\(.*\\)$") nil t
)))) t
)
904 (let* ((pos (match-beginning 0))
905 (heading (match-string 2))
906 (nlevel (if odd
(/ (+ 3 level
) 2) (1+ level
))))
911 (if (org-re-search-forward-unprotected
912 (concat "^\\(\\(?:\\*\\)\\{"
913 (number-to-string (+ (if odd
2 1) level
))
914 "\\}\\) \\(.*\\)$") nil t
)
917 (goto-char (point-min))
925 `(heading .
,heading
)
926 `(content .
,(org-export-latex-parse-content))
927 `(subcontent .
,(org-export-latex-parse-subcontent
932 (defun org-export-latex-parse-content ()
933 "Extract the content of a section."
935 (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t
)
936 (progn (beginning-of-line) (point))
938 (buffer-substring beg end
)))
940 (defun org-export-latex-parse-subcontent (level odd
)
941 "Extract the subcontent of a section at LEVEL.
942 If ODD Is non-nil, assume subcontent only contains odd sections."
943 (if (not (org-re-search-forward-unprotected
944 (concat "^\\(\\(?:\\*\\)\\{"
945 (number-to-string (+ (if odd
4 2) level
))
948 nil
; subcontent is nil
949 (org-export-latex-parse-global (+ (if odd
2 1) level
) odd
)))
951 ;;; Rendering functions:
952 (defun org-export-latex-global (content)
953 "Export CONTENT to LaTeX.
954 CONTENT is an element of the list produced by
955 `org-export-latex-parse-global'."
956 (if (eq (car content
) 'subcontent
)
957 (mapc 'org-export-latex-sub
(cdr content
))
958 (org-export-latex-sub (car content
))))
960 (defun org-export-latex-sub (subcontent)
961 "Export the list SUBCONTENT to LaTeX.
962 SUBCONTENT is an alist containing information about the headline
964 (let ((num (plist-get org-export-latex-options-plist
:section-numbers
)))
965 (mapc (lambda(x) (org-export-latex-subcontent x num
)) subcontent
)))
967 (defun org-export-latex-subcontent (subcontent num
)
968 "Export each cell of SUBCONTENT to LaTeX.
969 If NUM, export sections as numerical sections."
970 (let* ((heading (cdr (assoc 'heading subcontent
)))
971 (level (- (cdr (assoc 'level subcontent
))
972 org-export-latex-add-level
))
973 (occur (number-to-string (cdr (assoc 'occur subcontent
))))
974 (content (cdr (assoc 'content subcontent
)))
975 (subcontent (cadr (assoc 'subcontent subcontent
)))
976 (label (org-get-text-property-any 0 'target heading
))
977 (label-list (cons label
(cdr (assoc label
978 org-export-target-aliases
))))
979 (sectioning org-export-latex-sectioning
)
980 (depth org-export-latex-sectioning-depth
)
981 main-heading sub-heading
)
982 (when (symbolp (car sectioning
))
983 (setq sectioning
(funcall (car sectioning
) level heading
))
985 (setq heading
(car sectioning
)
986 sectioning
(cdr sectioning
)
987 ;; target property migh have changed...
988 label
(org-get-text-property-any 0 'target heading
)
989 label-list
(cons label
(cdr (assoc label
990 org-export-target-aliases
)))))
991 (if sectioning
(setq sectioning
(make-list 10 sectioning
)))
992 (setq depth
(if sectioning
10000 0)))
993 (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading
)
994 (setq main-heading
(substring heading
0 (match-beginning 0))
995 sub-heading
(substring heading
(match-end 0))))
996 (setq heading
(org-export-latex-fontify-headline heading
)
997 sub-heading
(and sub-heading
998 (org-export-latex-fontify-headline sub-heading
))
999 main-heading
(and main-heading
1000 (org-export-latex-fontify-headline main-heading
)))
1002 ;; Normal conversion
1004 (let* ((sec (nth (1- level
) sectioning
))
1006 (if (consp (cdr sec
))
1007 (setq start
(nth (if num
0 2) sec
)
1008 end
(nth (if num
1 3) sec
))
1009 (setq start
(if num
(car sec
) (cdr sec
))))
1010 (insert (format start
(if main-heading main-heading heading
)
1011 (or sub-heading
"")))
1014 (insert (mapconcat (lambda (l) (format "\\label{%s}" l
))
1015 label-list
"\n") "\n"))
1016 (insert (org-export-latex-content content
))
1017 (cond ((stringp subcontent
) (insert subcontent
))
1019 (while (org-looking-back "\n\n") (backward-delete-char 1))
1020 (org-export-latex-sub subcontent
)))
1021 (when (and end
(string-match "[^ \t]" end
))
1022 (let ((hook (org-get-text-property-any 0 'org-insert-hook end
)))
1023 (and (functionp hook
) (funcall hook
)))
1024 (insert end
"\n"))))
1025 ;; At a level under the hl option: we can drop this subsection
1027 (cond ((eq org-export-latex-low-levels
'description
)
1028 (if (string-match "% ends low level$"
1029 (buffer-substring (point-at-bol 0) (point)))
1030 (delete-region (point-at-bol 0) (point))
1031 (insert "\\begin{description}\n"))
1032 (insert (format "\n\\item[%s]%s~\n"
1034 (if label
(format "\\label{%s}" label
) "")))
1035 (insert (org-export-latex-content content
))
1036 (cond ((stringp subcontent
) (insert subcontent
))
1037 ((listp subcontent
) (org-export-latex-sub subcontent
)))
1038 (insert "\\end{description} % ends low level\n"))
1039 ((memq org-export-latex-low-levels
'(itemize enumerate
))
1040 (if (string-match "% ends low level$"
1041 (buffer-substring (point-at-bol 0) (point)))
1042 (delete-region (point-at-bol 0) (point))
1043 (insert (format "\\begin{%s}\n"
1044 (symbol-name org-export-latex-low-levels
))))
1045 (insert (format "\n\\item %s\\\\\n%s%%"
1047 (if label
(format "\\label{%s}" label
) "")))
1048 (insert (org-export-latex-content content
))
1049 (cond ((stringp subcontent
) (insert subcontent
))
1050 ((listp subcontent
) (org-export-latex-sub subcontent
)))
1051 (insert (format "\\end{%s} %% ends low level\n"
1052 (symbol-name org-export-latex-low-levels
))))
1054 ((listp org-export-latex-low-levels
)
1055 (if (string-match "% ends low level$"
1056 (buffer-substring (point-at-bol 0) (point)))
1057 (delete-region (point-at-bol 0) (point))
1058 (insert (car org-export-latex-low-levels
) "\n"))
1059 (insert (format (nth 2 org-export-latex-low-levels
)
1061 (if label
(format "\\label{%s}" label
) "")))
1062 (insert (org-export-latex-content content
))
1063 (cond ((stringp subcontent
) (insert subcontent
))
1064 ((listp subcontent
) (org-export-latex-sub subcontent
)))
1065 (insert (nth 1 org-export-latex-low-levels
)
1066 " %% ends low level\n"))
1068 ((stringp org-export-latex-low-levels
)
1069 (insert (format org-export-latex-low-levels heading
) "\n")
1070 (when label
(insert (format "\\label{%s}\n" label
)))
1071 (insert (org-export-latex-content content
))
1072 (cond ((stringp subcontent
) (insert subcontent
))
1073 ((listp subcontent
) (org-export-latex-sub subcontent
)))))))))
1075 ;;; Exporting internals:
1076 (defun org-export-latex-set-initial-vars (ext-plist level
)
1077 "Store org local variables required for LaTeX export.
1078 EXT-PLIST is an optional additional plist.
1079 LEVEL indicates the default depth for export."
1080 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
1081 org-export-latex-done-keywords org-done-keywords
1082 org-export-latex-not-done-keywords org-not-done-keywords
1083 org-export-latex-complex-heading-re org-complex-heading-regexp
1084 org-export-latex-display-custom-times org-display-custom-times
1085 org-export-latex-all-targets-re
1086 (org-make-target-link-regexp (org-all-targets))
1087 org-export-latex-options-plist
1088 (org-combine-plists (org-default-export-plist) ext-plist
1089 (org-infile-export-plist))
1090 org-export-latex-class
1091 (or (and (org-region-active-p)
1093 (goto-char (region-beginning))
1094 (and (looking-at org-complex-heading-regexp
)
1095 (org-entry-get nil
"LaTeX_CLASS" 'selective
))))
1099 (goto-char (point-min))
1100 (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t
)
1102 (plist-get org-export-latex-options-plist
:latex-class
)
1103 org-export-latex-default-class
)
1104 org-export-latex-class-options
1105 (or (and (org-region-active-p)
1107 (goto-char (region-beginning))
1108 (and (looking-at org-complex-heading-regexp
)
1109 (org-entry-get nil
"LaTeX_CLASS_OPTIONS" 'selective
))))
1113 (goto-char (point-min))
1114 (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t
)
1116 (plist-get org-export-latex-options-plist
:latex-class-options
))
1117 org-export-latex-class
1118 (or (car (assoc org-export-latex-class org-export-latex-classes
))
1119 (error "No definition for class `%s' in `org-export-latex-classes'"
1120 org-export-latex-class
))
1121 org-export-latex-header
1122 (cadr (assoc org-export-latex-class org-export-latex-classes
))
1123 org-export-latex-sectioning
1124 (cddr (assoc org-export-latex-class org-export-latex-classes
))
1125 org-export-latex-sectioning-depth
1128 (plist-get org-export-latex-options-plist
:headline-levels
))
1129 (sec-depth (length org-export-latex-sectioning
)))
1130 (if (> hl-levels sec-depth
) sec-depth hl-levels
))))
1131 (when (and org-export-latex-class-options
1132 (string-match "\\S-" org-export-latex-class-options
)
1133 (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?"
1134 org-export-latex-header
))
1135 (setq org-export-latex-header
1136 (concat (substring org-export-latex-header
0 (match-end 1))
1137 org-export-latex-class-options
1138 (substring org-export-latex-header
(match-end 0))))))
1140 (defvar org-export-latex-format-toc-function
1141 'org-export-latex-format-toc-default
1142 "The function formatting returning the string to createthe table of contents.
1143 The function mus take one parameter, the depth of the table of contents.")
1145 (defun org-export-latex-make-header (title opt-plist
)
1146 "Make the LaTeX header and return it as a string.
1147 TITLE is the current title from the buffer or region.
1148 OPT-PLIST is the options plist for current buffer."
1149 (let ((toc (plist-get opt-plist
:table-of-contents
))
1150 (author (org-export-apply-macros-in-string
1151 (plist-get opt-plist
:author
))))
1153 (if (plist-get opt-plist
:time-stamp-file
)
1154 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1155 ;; insert LaTeX custom header
1156 (org-export-apply-macros-in-string org-export-latex-header
)
1158 ;; insert information on LaTeX packages
1159 (when org-export-latex-packages-alist
1161 (mapconcat (lambda(p)
1162 (if (equal "" (car p
))
1163 (format "\\usepackage{%s}" (cadr p
))
1164 (format "\\usepackage[%s]{%s}"
1166 org-export-latex-packages-alist
"\n")
1168 ;; insert additional commands in the header
1169 (org-export-apply-macros-in-string
1170 (plist-get opt-plist
:latex-header-extra
))
1171 (org-export-apply-macros-in-string org-export-latex-append-header
)
1172 ;; define align if not yet defined
1173 "\\providecommand{\\alert}[1]{\\textbf{#1}}"
1177 ;; convert the title
1178 (org-export-latex-content
1179 title
'(lists tables fixed-width keywords
)))
1180 ;; insert author info
1181 (if (plist-get opt-plist
:author-info
)
1182 (format "\\author{%s}\n"
1183 (org-export-latex-fontify-headline (or author user-full-name
)))
1184 (format "%%\\author{%s}\n"
1185 (org-export-latex-fontify-headline (or author user-full-name
))))
1187 (format "\\date{%s}\n"
1189 (or (plist-get opt-plist
:date
)
1190 org-export-latex-date-format
)))
1191 ;; beginning of the document
1192 "\n\\begin{document}\n\n"
1193 ;; insert the title command
1194 (when (string-match "\\S-" title
)
1195 (if (string-match "%s" org-export-latex-title-command
)
1196 (format org-export-latex-title-command title
)
1197 org-export-latex-title-command
))
1199 ;; table of contents
1200 (when (and org-export-with-toc
1201 (plist-get opt-plist
:section-numbers
))
1202 (funcall org-export-latex-format-toc-function
1203 (cond ((numberp toc
)
1204 (min toc
(plist-get opt-plist
:headline-levels
)))
1205 (toc (plist-get opt-plist
:headline-levels
))))))))
1207 (defun org-export-latex-format-toc-default (depth)
1209 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1212 (defun org-export-latex-first-lines (opt-plist &optional beg end
)
1213 "Export the first lines before first headline.
1214 If BEG is non-nil, it is the beginning of the region.
1215 If END is non-nil, it is the end of the region."
1217 (goto-char (or beg
(point-min)))
1219 (end (if (re-search-forward "^\\*+ " end t
)
1220 (goto-char (match-beginning 0))
1221 (goto-char (or end
(point-max))))))
1223 (org-export-latex-content
1224 (org-export-preprocess-string
1225 (buffer-substring pt end
)
1230 :skip-before-1st-heading nil
1231 :LaTeX-fragments nil
1232 :timestamps
(plist-get opt-plist
:timestamps
)
1233 :footnotes
(plist-get opt-plist
:footnotes
)))
1235 (let ((inhibit-read-only t
))
1236 (add-text-properties pt
(max pt
(1- end
))
1237 '(:org-license-to-kill t
))))))))
1239 (defvar org-export-latex-header-defs nil
1240 "The header definitions that might be used in the LaTeX body.")
1241 (defvar org-export-latex-header-defs-re nil
1242 "The header definitions that might be used in the LaTeX body.")
1244 (defun org-export-latex-content (content &optional exclude-list
)
1245 "Convert CONTENT string to LaTeX.
1246 Don't perform conversions that are in EXCLUDE-LIST. Recognized
1247 conversion types are: quotation-marks, emphasis, sub-superscript,
1248 links, keywords, lists, tables, fixed-width"
1251 (unless (memq 'timestamps exclude-list
)
1252 (org-export-latex-time-stamps))
1253 (unless (memq 'quotation-marks exclude-list
)
1254 (org-export-latex-quotation-marks))
1255 (unless (memq 'emphasis exclude-list
)
1256 (when (plist-get org-export-latex-options-plist
:emphasize
)
1257 (org-export-latex-fontify)))
1258 (unless (memq 'sub-superscript exclude-list
)
1259 (org-export-latex-special-chars
1260 (plist-get org-export-latex-options-plist
:sub-superscript
)))
1261 (unless (memq 'links exclude-list
)
1262 (org-export-latex-links))
1263 (unless (memq 'keywords exclude-list
)
1264 (org-export-latex-keywords))
1265 (unless (memq 'lists exclude-list
)
1266 (org-export-latex-lists))
1267 (unless (memq 'tables exclude-list
)
1268 (org-export-latex-tables
1269 (plist-get org-export-latex-options-plist
:tables
)))
1270 (unless (memq 'fixed-width exclude-list
)
1271 (org-export-latex-fixed-width
1272 (plist-get org-export-latex-options-plist
:fixed-width
)))
1274 (buffer-substring (point-min) (point-max))))
1276 (defun org-export-latex-protect-string (s)
1277 "Add the org-protected property to string S."
1278 (add-text-properties 0 (length s
) '(org-protected t
) s
) s
)
1280 (defun org-export-latex-protect-char-in-string (char-list string
)
1281 "Add org-protected text-property to char from CHAR-LIST in STRING."
1285 (goto-char (point-min))
1286 (while (re-search-forward (regexp-opt char-list
) nil t
)
1287 (add-text-properties (match-beginning 0)
1288 (match-end 0) '(org-protected t
)))
1291 (defun org-export-latex-keywords-maybe (&optional remove-list
)
1292 "Maybe remove keywords depending on rules in REMOVE-LIST."
1293 (goto-char (point-min))
1294 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1
"\\|"))
1295 (case-fold-search nil
)
1296 (todo-markup org-export-latex-todo-keyword-markup
)
1298 ;; convert TODO keywords
1299 (when (re-search-forward (concat "^\\(" re-todo
"\\)") nil t
)
1300 (if (plist-get remove-list
:todo
)
1303 ((stringp todo-markup
) todo-markup
)
1304 ((and (consp todo-markup
) (stringp (car todo-markup
)))
1305 (if (member (match-string 1) org-export-latex-done-keywords
)
1306 (cdr todo-markup
) (car todo-markup
)))
1307 (t (cdr (or (assoc (match-string 1) todo-markup
)
1308 (car todo-markup
))))))
1309 (replace-match (format fmt
(match-string 1)) t t
)))
1310 ;; convert priority string
1311 (when (re-search-forward "\\[\\\\#.\\]" nil t
)
1312 (if (plist-get remove-list
:priority
)
1314 (replace-match (format "\\textbf{%s}" (match-string 0)) t t
)))
1316 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t
)
1317 (if (or (not org-export-with-tags
)
1318 (plist-get remove-list
:tags
))
1321 (org-export-latex-protect-string
1322 (format "\\textbf{%s}"
1324 (replace-regexp-in-string
1325 "_" "\\\\_" (match-string 0)))))
1328 (defun org-export-latex-fontify-headline (string)
1329 "Fontify special words in STRING."
1331 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
1332 ;; the beginning of the buffer - inserting "\n" is safe here though.
1333 (insert "\n" string
)
1334 (goto-char (point-min))
1335 (let ((re (concat "\\\\[a-zA-Z]+"
1336 "\\(?:<[^<>\n]*>\\)*"
1337 "\\(?:\\[[^][\n]*?\\]\\)*"
1338 "\\(?:<[^<>\n]*>\\)*"
1340 (org-create-multibrace-regexp "{" "}" 3)
1342 (while (re-search-forward re nil t
)
1343 (unless (save-excursion (goto-char (match-beginning 0))
1344 (equal (char-after (point-at-bol)) ?
#))
1345 (add-text-properties (match-beginning 0) (match-end 0)
1346 '(org-protected t
)))))
1347 (when (plist-get org-export-latex-options-plist
:emphasize
)
1348 (org-export-latex-fontify))
1349 (org-export-latex-keywords-maybe)
1350 (org-export-latex-special-chars
1351 (plist-get org-export-latex-options-plist
:sub-superscript
))
1352 (org-export-latex-links)
1353 (org-trim (buffer-string))))
1355 (defun org-export-latex-time-stamps ()
1356 "Format time stamps."
1357 (goto-char (point-min))
1358 (let ((org-display-custom-times org-export-latex-display-custom-times
))
1359 (while (re-search-forward org-ts-regexp-both nil t
)
1360 (org-if-unprotected-at (1- (point))
1362 (org-export-latex-protect-string
1363 (format org-export-latex-timestamp-markup
1364 (substring (org-translate-time (match-string 0)) 1 -
1)))
1367 (defun org-export-latex-quotation-marks ()
1368 "Export quotation marks depending on language conventions."
1369 (let* ((lang (plist-get org-export-latex-options-plist
:language
))
1370 (quote-rpl (if (equal lang
"fr")
1371 '(("\\(\\s-\\)\"" "«~")
1372 ("\\(\\S-\\)\"" "~»")
1373 ("\\(\\s-\\)'" "`"))
1374 '(("\\(\\s-\\|[[(]\\)\"" "``")
1375 ("\\(\\S-\\)\"" "''")
1376 ("\\(\\s-\\|(\\)'" "`")))))
1377 (mapc (lambda(l) (goto-char (point-min))
1378 (while (re-search-forward (car l
) nil t
)
1379 (let ((rpl (concat (match-string 1)
1380 (org-export-latex-protect-string
1381 (copy-sequence (cadr l
))))))
1382 (org-if-unprotected-1
1383 (replace-match rpl t t
))))) quote-rpl
)))
1385 (defun org-export-latex-special-chars (sub-superscript)
1386 "Export special characters to LaTeX.
1387 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
1388 See the `org-export-latex.el' code for a complete conversion table."
1389 (goto-char (point-min))
1391 (goto-char (point-min))
1392 (while (re-search-forward c nil t
)
1393 ;; Put the point where to check for org-protected
1394 (unless (get-text-property (match-beginning 2) 'org-protected
)
1395 (cond ((member (match-string 2) '("\\$" "$"))
1396 (if (equal (match-string 2) "\\$")
1398 (replace-match "\\$" t t
)))
1399 ((member (match-string 2) '("&" "%" "#"))
1400 (if (equal (match-string 1) "\\")
1401 (replace-match (match-string 2) t t
)
1402 (replace-match (concat (match-string 1) "\\"
1403 (match-string 2)) t t
)
1405 ((equal (match-string 2) "...")
1407 (concat (match-string 1)
1408 (org-export-latex-protect-string "\\ldots{}")) t t
))
1409 ((equal (match-string 2) "~")
1410 (cond ((equal (match-string 1) "\\") nil
)
1411 ((eq 'org-link
(get-text-property 0 'face
(match-string 2)))
1412 (replace-match (concat (match-string 1) "\\~") t t
))
1414 (org-export-latex-protect-string
1415 (concat (match-string 1) "\\~{}")) t t
))))
1416 ((member (match-string 2) '("{" "}"))
1417 (unless (save-match-data (org-inside-latex-math-p))
1418 (if (equal (match-string 1) "\\")
1419 (replace-match (match-string 2) t t
)
1420 (replace-match (concat (match-string 1) "\\"
1421 (match-string 2)) t t
)))))
1422 (unless (save-match-data (org-inside-latex-math-p))
1423 (cond ((equal (match-string 2) "\\")
1424 (replace-match (or (save-match-data
1425 (org-export-latex-treat-backslash-char
1427 (or (match-string 3) "")))
1429 ((member (match-string 2) '("_" "^"))
1430 (replace-match (or (save-match-data
1431 (org-export-latex-treat-sub-super-char
1435 (match-string 3))) "") t t
)
1436 (backward-char 1)))))))
1437 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
1438 "\\(\\(\\\\?\\$\\)\\)"
1439 "\\([a-za-z0-9()]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
1440 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
1441 "\\(.\\|^\\)\\(&\\)"
1442 "\\(.\\|^\\)\\(#\\)"
1443 "\\(.\\|^\\)\\(%\\)"
1444 "\\(.\\|^\\)\\({\\)"
1445 "\\(.\\|^\\)\\(}\\)"
1446 "\\(.\\|^\\)\\(~\\)"
1447 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
1448 ;; (?\< . "\\textless{}")
1449 ;; (?\> . "\\textgreater{}")
1452 (defun org-inside-latex-math-p ()
1453 (get-text-property (point) 'org-latex-math
))
1455 (defun org-export-latex-treat-sub-super-char
1456 (subsup char string-before string-after
)
1457 "Convert the \"_\" and \"^\" characters to LaTeX.
1458 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
1459 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
1460 (cond ((equal string-before
"\\")
1461 (concat string-before char string-after
))
1462 ((and (string-match "\\S-+" string-after
))
1463 ;; this is part of a math formula
1464 (cond ((eq 'org-link
(get-text-property 0 'face char
))
1465 (concat string-before
"\\" char string-after
))
1466 ((save-match-data (org-inside-latex-math-p))
1468 (cond ((eq 1 (length string-after
))
1469 (concat string-before char string-after
))
1470 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after
)
1471 (format "%s%s{%s}" string-before char
1472 (match-string 1 string-after
))))))
1473 ((and (> (length string-after
) 1)
1475 (and (equal subsup
'{}) (eq (string-to-char string-after
) ?\
{)))
1476 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after
))
1477 (org-export-latex-protect-string
1478 (format "%s$%s{%s}$" string-before char
1479 (if (and (> (match-end 1) (1+ (match-beginning 1)))
1480 (not (equal (substring string-after
0 2) "{\\")))
1481 (concat "\\mathrm{" (match-string 1 string-after
) "}")
1482 (match-string 1 string-after
)))))
1483 ((eq subsup t
) (concat string-before
"$" char string-after
"$"))
1484 (t (org-export-latex-protect-string
1485 (concat string-before
"\\" char
"{}" string-after
)))))
1486 (t (org-export-latex-protect-string
1487 (concat string-before
"\\" char
"{}" string-after
)))))
1489 (defun org-export-latex-treat-backslash-char (string-before string-after
)
1490 "Convert the \"$\" special character to LaTeX.
1491 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
1492 (cond ((member (list string-after
) org-html-entities
)
1493 ;; backslash is part of a special entity (like "\alpha")
1494 (concat string-before
"$\\"
1495 (or (cdar (member (list string-after
) org-html-entities
))
1497 ((and (not (string-match "^[ \n\t]" string-after
))
1498 (not (string-match "[ \t]\\'\\|^" string-before
)))
1499 ;; backslash is inside a word
1500 (concat string-before
1501 (org-export-latex-protect-string
1502 (concat "\\textbackslash{}" string-after
))))
1503 ((not (or (equal string-after
"")
1504 (string-match "^[ \t\n]" string-after
)))
1505 ;; backslash might escape a character (like \#) or a user TeX
1506 ;; macro (like \setcounter)
1507 (concat string-before
1508 (org-export-latex-protect-string (concat "\\" string-after
))))
1509 ((and (string-match "^[ \t\n]" string-after
)
1510 (string-match "[ \t\n]\\'" string-before
))
1511 ;; backslash is alone, convert it to $\backslash$
1512 (org-export-latex-protect-string
1513 (concat string-before
"\\textbackslash{}" string-after
)))
1514 (t (org-export-latex-protect-string
1515 (concat string-before
"\\textbackslash{}" string-after
)))))
1517 (defun org-export-latex-keywords ()
1518 "Convert special keywords to LaTeX."
1519 (goto-char (point-min))
1520 (while (re-search-forward org-export-latex-special-keyword-regexp nil t
)
1521 (replace-match (format org-export-latex-timestamp-keyword-markup
1522 (match-string 0)) t t
)
1524 (beginning-of-line 1)
1525 (unless (looking-at ".*\n[ \t]*\n")
1529 (defun org-export-latex-fixed-width (opt)
1530 "When OPT is non-nil convert fixed-width sections to LaTeX."
1531 (goto-char (point-min))
1532 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t
)
1534 (progn (goto-char (match-beginning 0))
1535 (insert "\\begin{verbatim}\n")
1536 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1537 (replace-match (concat (match-string 1)
1538 (match-string 2)) t t
)
1540 (insert "\\end{verbatim}\n\n"))
1541 (progn (goto-char (match-beginning 0))
1542 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1543 (replace-match (concat "%" (match-string 1)
1544 (match-string 2)) t t
)
1548 (defvar org-table-last-alignment
) ; defined in org-table.el
1549 (defvar org-table-last-column-widths
) ; defined in org-table.el
1550 (declare-function orgtbl-to-latex
"org-table" (table params
) t
)
1551 (defun org-export-latex-tables (insert)
1552 "Convert tables to LaTeX and INSERT it."
1553 ;; First, get the table.el tables
1554 (goto-char (point-min))
1555 (while (re-search-forward "^[ \t]*\\(\\+-[-+]*\\+\\)[ \t]*\n[ \t]*|" nil t
)
1558 (org-export-latex-convert-table.el-table
)))
1560 ;; And now the Org-mode tables
1561 (goto-char (point-min))
1562 (while (re-search-forward "^\\([ \t]*\\)|" nil t
)
1563 (org-if-unprotected-at (1- (point))
1565 (let* ((beg (org-table-begin))
1566 (end (org-table-end))
1567 (raw-table (buffer-substring beg end
))
1568 (org-table-last-alignment (copy-sequence org-table-last-alignment
))
1569 (org-table-last-column-widths (copy-sequence
1570 org-table-last-column-widths
))
1571 fnum fields line lines olines gr colgropen line-fmt align
1572 caption label attr floatp longtblp
)
1573 (if org-export-latex-tables-verbatim
1574 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1575 "\\end{verbatim}\n")))
1576 (apply 'delete-region
(list beg end
))
1577 (insert (org-export-latex-protect-string tbl
)))
1579 (setq caption
(org-find-text-property-in-string
1580 'org-caption raw-table
)
1581 attr
(org-find-text-property-in-string
1582 'org-attributes raw-table
)
1583 label
(org-find-text-property-in-string
1584 'org-label raw-table
)
1585 longtblp
(and attr
(stringp attr
)
1586 (string-match "\\<longtable\\>" attr
))
1587 align
(and attr
(stringp attr
)
1588 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr
)
1589 (match-string 1 attr
))
1590 floatp
(or caption label
))
1591 (setq caption
(and caption
(org-export-latex-content caption
)))
1592 (setq lines
(org-split-string raw-table
"\n"))
1593 (apply 'delete-region
(list beg end
))
1594 (when org-export-table-remove-special-lines
1595 (setq lines
(org-table-clean-before-export lines
'maybe-quoted
)))
1596 (when org-table-clean-did-remove-column
1597 (pop org-table-last-alignment
)
1598 (pop org-table-last-column-widths
))
1599 ;; make a formatting string to reflect alignment
1601 (while (and (not line-fmt
) (setq line
(pop olines
)))
1602 (unless (string-match "^[ \t]*|-" line
)
1603 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
1604 (setq fnum
(make-vector (length fields
) 0))
1608 (setq gr
(pop org-table-colgroup-info
))
1610 (cond ((eq gr
:start
)
1611 (prog1 (if colgropen
"|" "|")
1612 (setq colgropen t
)))
1614 (prog1 (if colgropen
"|" "|")
1615 (setq colgropen nil
)))
1617 (if (memq gr
'(:end
:startend
))
1618 (progn (setq colgropen nil
) "|")
1621 ;; fix double || in line-fmt
1622 (setq line-fmt
(replace-regexp-in-string "||" "|" line-fmt
))
1623 ;; maybe remove the first and last "|"
1624 (when (and (not org-export-latex-tables-column-borders
)
1625 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt
))
1626 (setq line-fmt
(match-string 2 line-fmt
)))
1629 (setq align
(apply 'format
1631 (mapcar (lambda (x) (if x
"r" "l"))
1632 org-table-last-alignment
)))))
1633 ;; prepare the table to send to orgtbl-to-latex
1637 (or (and (string-match "[ \t]*|-+" elem
) 'hline
)
1638 (org-split-string (org-trim elem
) "|")))
1641 (insert (org-export-latex-protect-string
1644 (concat "\\begin{longtable}{" align
"}\n")
1645 (if floatp
"\\begin{table}[htb]\n"))
1649 (if label
(concat "\\\label{" label
"}") "")
1651 (if (and longtblp caption
) "\\\\\n" "\n")
1652 (if (and org-export-latex-tables-centered
(not longtblp
))
1653 "\\begin{center}\n")
1654 (if (not longtblp
) (concat "\\begin{tabular}{" align
"}\n"))
1657 `(:tstart nil
:tend nil
1658 :hlend
,(if longtblp
1662 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
1664 \\endlastfoot" (length org-table-last-alignment
))
1666 (if (not longtblp
) (concat "\n\\end{tabular}"))
1667 (if longtblp
"\n" (if org-export-latex-tables-centered
1668 "\n\\end{center}\n" "\n"))
1671 (if floatp
"\\end{table}"))))
1674 (defun org-export-latex-convert-table.el-table
()
1675 "Replace table.el table at point with LaTeX code."
1676 (let (tbl caption label line floatp attr align rmlines
)
1677 (setq line
(buffer-substring (point-at-bol) (point-at-eol))
1678 label
(org-get-text-property-any 0 'org-label line
)
1679 caption
(org-get-text-property-any 0 'org-caption line
)
1680 attr
(org-get-text-property-any 0 'org-attributes line
)
1681 align
(and attr
(stringp attr
)
1682 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr
)
1683 (match-string 1 attr
))
1684 rmlines
(and attr
(stringp attr
)
1685 (string-match "\\<rmlines\\>" attr
))
1686 floatp
(or label caption
))
1687 (and (get-buffer "*org-export-table*")
1688 (kill-buffer (get-buffer "*org-export-table*")))
1689 (table-generate-source 'latex
"*org-export-table*" "caption")
1690 (setq tbl
(with-current-buffer "*org-export-table*"
1692 (while (string-match "^%.*\n" tbl
)
1693 (setq tbl
(replace-match "" t t tbl
)))
1697 (setq lines
(mapcar (lambda (x)
1698 (if (string-match "^\\\\hline$" x
)
1703 (org-split-string tbl
"\n")))
1704 (setq tbl
(mapconcat 'identity
(delq nil lines
) "\n"))))
1705 (when (and align
(string-match "\\\\begin{tabular}{.*}" tbl
))
1706 (setq tbl
(replace-match (concat "\\begin{tabular}{" align
"}")
1708 (and (get-buffer "*org-export-table*")
1709 (kill-buffer (get-buffer "*org-export-table*")))
1710 (beginning-of-line 0)
1711 (while (looking-at "[ \t]*\\(|\\|\\+-\\)")
1712 (delete-region (point) (1+ (point-at-eol))))
1713 (when org-export-latex-tables-centered
1714 (setq tbl
(concat "\\begin{center}\n" tbl
"\\end{center}")))
1716 (setq tbl
(concat "\\begin{table}\n"
1717 (format "\\caption{%s%s}\n"
1718 (if label
(format "\\label{%s}" label
) "")
1721 "\n\\end{table}\n")))
1722 (insert (org-export-latex-protect-string tbl
))))
1724 (defun org-export-latex-fontify ()
1725 "Convert fontification to LaTeX."
1726 (goto-char (point-min))
1727 (while (re-search-forward org-emph-re nil t
)
1728 ;; The match goes one char after the *string*, except at the end of a line
1729 (let ((emph (assoc (match-string 3)
1730 org-export-latex-emphasis-alist
))
1731 (beg (match-beginning 0))
1735 (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
1737 (unless (or (and (get-text-property (- (point) 2) 'org-protected
)
1738 (not (get-text-property
1739 (- (point) 2) 'org-verbatim-emph
)))
1741 (goto-char (match-beginning 1))
1743 (and (org-at-table-p)
1745 "[|\n]" (buffer-substring beg end
)))))
1746 (and (equal (match-string 3) "+")
1748 (string-match "\\`-+\\'" (match-string 4)))))
1749 (setq s
(match-string 4))
1750 (setq rpl
(concat (match-string 1)
1751 (org-export-latex-emph-format (cadr emph
)
1755 (setq rpl
(org-export-latex-protect-string rpl
))
1757 (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl
)
1759 (add-text-properties (match-beginning 1) (match-end 1)
1760 '(org-protected t
) rpl
)
1761 (add-text-properties (match-beginning 3) (match-end 3)
1762 '(org-protected t
) rpl
)))))
1763 (replace-match rpl t t
)))
1766 (defvar org-export-latex-use-verb nil
)
1767 (defun org-export-latex-emph-format (format string
)
1768 "Format an emphasis string and handle the \\verb special case."
1769 (when (equal format
"\\verb")
1771 (if org-export-latex-use-verb
1772 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1774 (loop for i from
0 to
(1- (length ll
)) do
1775 (if (not (string-match (regexp-quote (substring ll i
(1+ i
)))
1778 (setq format
(concat "\\verb" (substring ll i
(1+ i
))
1779 "%s" (substring ll i
(1+ i
))))
1780 (throw 'exit nil
))))))
1782 (trans '(("\\" .
"\\textbackslash{}")
1783 ("~" .
"\\textasciitilde{}")
1784 ("^" .
"\\textasciicircum{}")))
1786 (while (string-match "[\\{}$%&_#~^]" string
)
1787 (setq char
(match-string 0 string
))
1788 (if (> (match-beginning 0) 0)
1789 (setq rtn
(concat rtn
(substring string
1790 0 (match-beginning 0)))))
1791 (setq string
(substring string
(1+ (match-beginning 0))))
1792 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
1793 rtn
(concat rtn char
)))
1794 (setq string
(concat rtn string
) format
"\\texttt{%s}")))))
1795 (format format string
))
1797 (defun org-export-latex-links ()
1798 ;; Make sure to use the LaTeX hyperref and graphicx package
1799 ;; or send some warnings.
1800 "Convert links to LaTeX."
1801 (goto-char (point-min))
1802 (while (re-search-forward org-bracket-link-analytic-regexp
++ nil t
)
1803 (org-if-unprotected-1
1804 (goto-char (match-beginning 0))
1805 (let* ((re-radio org-export-latex-all-targets-re
)
1806 (remove (list (match-beginning 0) (match-end 0)))
1807 (raw-path (org-extract-attributes (match-string 3)))
1808 (full-raw-path (concat (match-string 1) raw-path
))
1809 (desc (match-string 5))
1810 (type (or (match-string 2)
1811 (if (or (file-name-absolute-p raw-path
)
1812 (string-match "^\\.\\.?/" raw-path
))
1814 (coderefp (equal type
"coderef"))
1815 (caption (org-find-text-property-in-string 'org-caption raw-path
))
1816 (attr (or (org-find-text-property-in-string 'org-attributes raw-path
)
1817 (plist-get org-export-latex-options-plist
:latex-image-options
)))
1818 (label (org-find-text-property-in-string 'org-label raw-path
))
1820 ;; define the path of the link
1822 ((member type
'("coderef"))
1824 ((member type
'("http" "https" "ftp"))
1825 (concat type
":" raw-path
))
1826 ((and re-radio
(string-match re-radio raw-path
))
1828 ((equal type
"mailto")
1829 (concat type
":" raw-path
))
1830 ((equal type
"file")
1831 (if (and (org-file-image-p
1834 org-export-latex-inline-image-extensions
)
1835 (or (get-text-property 0 'org-no-description
1837 (equal desc full-raw-path
)))
1839 (progn (when (string-match "\\(.+\\)::.+" raw-path
)
1840 (setq raw-path
(match-string 1 raw-path
)))
1841 (if (file-exists-p raw-path
)
1842 (concat type
"://" (expand-file-name raw-path
))
1843 (concat type
"://" (org-export-directory
1844 :LaTeX org-export-latex-options-plist
)
1846 ;; process with link inserting
1847 (apply 'delete-region remove
)
1848 (setq caption
(and caption
(org-export-latex-content caption
)))
1850 (plist-get org-export-latex-options-plist
:inline-images
))
1851 ;; OK, we need to inline an image
1853 (org-export-latex-format-image raw-path caption label attr
)))
1856 (org-export-get-coderef-format path desc
)
1857 (cdr (assoc path org-export-code-refs
)))))
1858 (radiop (insert (format "\\hyperref[%s]{%s}"
1859 (org-solidify-link-text raw-path
) desc
)))
1861 (insert (format "\\hyperref[%s]{%s}"
1862 (org-remove-initial-hash
1863 (org-solidify-link-text raw-path
))
1866 (when (org-at-table-p)
1867 ;; There is a strange problem when we have a link in a table,
1868 ;; ampersands then cause a problem. I think this must be
1869 ;; a LaTeX issue, but we here implement a work-around anyway.
1870 (setq path
(org-export-latex-protect-amp path
)
1871 desc
(org-export-latex-protect-amp desc
)))
1872 (insert (format org-export-latex-hyperref-format path desc
)))
1873 (t (insert "\\texttt{" desc
"}")))))))
1876 (defun org-export-latex-format-image (path caption label attr
)
1877 "Format the image element, depending on user settings."
1878 (let (ind floatp wrapp placement figenv
)
1879 (setq floatp
(or caption label
))
1880 (setq ind
(org-get-text-property-any 0 'original-indentation path
))
1881 (when (and attr
(stringp attr
))
1882 (if (string-match "[ \t]*\\<wrap\\>" attr
)
1883 (setq wrapp t floatp nil attr
(replace-match "" t t attr
)))
1884 (if (string-match "[ \t]*\\<float\\>" attr
)
1885 (setq wrapp nil floatp t attr
(replace-match "" t t attr
))))
1889 (wrapp "{l}{0.5\\textwidth}")
1893 (when (and attr
(stringp attr
)
1894 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr
))
1895 (setq placement
(match-string 1 attr
)
1896 attr
(replace-match "" t t attr
)))
1897 (setq attr
(and attr
(org-trim attr
)))
1898 (when (or (not attr
) (= (length attr
) 0))
1899 (setq attr
(cond (floatp "width=0.7\\textwidth")
1900 (wrapp "width=0.48\\textwidth")
1904 (wrapp "\\begin{wrapfigure}%placement
1906 \\includegraphics[%attr]{%path}
1907 \\caption{%labelcmd%caption}
1909 (floatp "\\begin{figure}%placement
1911 \\includegraphics[%attr]{%path}
1912 \\caption{%labelcmd%caption}
1914 (t "\\includegraphics[%attr]{%path}")))
1917 (setq figenv
(mapconcat 'identity
(split-string figenv
"\n")
1918 (save-excursion (beginning-of-line 1)
1919 (looking-at "[ \t]*")
1920 (concat "\n" (match-string 0)))))
1922 (if (and (not label
) (not caption
)
1923 (string-match "^\\\\caption{.*\n" figenv
))
1924 (setq figenv
(replace-match "" t t figenv
)))
1929 (if (file-name-absolute-p path
)
1930 (expand-file-name path
)
1933 (cons "labelcmd" (if label
(format "\\label{%s}"
1935 (cons "caption" (or caption
""))
1936 (cons "placement" (or placement
""))))
1937 nil
'original-indentation ind
)))
1939 (defun org-export-latex-protect-amp (s)
1940 (while (string-match "\\([^\\\\]\\)\\(&\\)" s
)
1941 (setq s
(replace-match (concat (match-string 1 s
) "\\" (match-string 2 s
))
1945 (defun org-remove-initial-hash (s)
1946 (if (string-match "\\`#" s
)
1949 (defvar org-latex-entities
) ; defined below
1950 (defvar org-latex-entities-regexp
) ; defined below
1951 (defvar org-latex-entities-exceptions
) ; defined below
1953 (defun org-export-latex-preprocess (parameters)
1954 "Clean stuff in the LaTeX export."
1955 ;; Preserve line breaks
1956 (goto-char (point-min))
1957 (while (re-search-forward "\\\\\\\\" nil t
)
1958 (add-text-properties (match-beginning 0) (match-end 0)
1959 '(org-protected t
)))
1961 ;; Preserve latex environments
1962 (goto-char (point-min))
1963 (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t
)
1964 (let* ((start (progn (beginning-of-line) (point)))
1965 (end (and (re-search-forward
1966 (concat "^[ \t]*\\\\end{"
1967 (regexp-quote (match-string 1))
1971 (add-text-properties start end
'(org-protected t
))
1972 (goto-char (point-at-eol)))))
1974 ;; Preserve math snippets
1976 (let* ((matchers (plist-get org-format-latex-options
:matchers
))
1977 (re-list org-latex-regexps
)
1978 beg end re e m n block off
)
1979 ;; Check the different regular expressions
1980 (while (setq e
(pop re-list
))
1981 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
1982 block
(if (nth 3 e
) "\n\n" ""))
1983 (setq off
(if (member m
'("$" "$1")) 1 0))
1984 (when (and (member m matchers
) (not (equal m
"begin")))
1985 (goto-char (point-min))
1986 (while (re-search-forward re nil t
)
1987 (setq beg
(+ (match-beginning 0) off
) end
(- (match-end 0) 0))
1988 (add-text-properties beg end
'(org-protected t org-latex-math t
))))))
1990 ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{}
1991 (goto-char (point-min))
1992 (let ((case-fold-search nil
))
1993 (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t
)
1994 (unless (eq (char-before (match-beginning 1)) ?
\\)
1995 (org-if-unprotected-1
1996 (replace-match (org-export-latex-protect-string
1997 (concat "\\" (match-string 1)
2000 ;; Convert blockquotes
2001 (goto-char (point-min))
2002 (while (search-forward "ORG-BLOCKQUOTE-START" nil t
)
2003 (org-replace-match-keep-properties "\\begin{quote}" t t
))
2004 (goto-char (point-min))
2005 (while (search-forward "ORG-BLOCKQUOTE-END" nil t
)
2006 (org-replace-match-keep-properties "\\end{quote}" t t
))
2009 (goto-char (point-min))
2010 (while (search-forward "ORG-VERSE-START" nil t
)
2011 (org-replace-match-keep-properties "\\begin{verse}" t t
)
2012 (beginning-of-line 2)
2013 (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
2014 (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
2015 (goto-char (match-end 1))
2016 (org-replace-match-keep-properties
2017 (org-export-latex-protect-string
2018 (concat "\\hspace*{1cm}" (match-string 2))) t t
)
2019 (beginning-of-line 1))
2020 (if (looking-at "[ \t]*$")
2021 (insert (org-export-latex-protect-string "\\vspace*{1em}"))
2022 (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
2025 (beginning-of-line 2))
2026 (and (looking-at "[ \t]*ORG-VERSE-END.*")
2027 (org-replace-match-keep-properties "\\end{verse}" t t
)))
2030 (goto-char (point-min))
2031 (while (search-forward "ORG-CENTER-START" nil t
)
2032 (org-replace-match-keep-properties "\\begin{center}" t t
))
2033 (goto-char (point-min))
2034 (while (search-forward "ORG-CENTER-END" nil t
)
2035 (org-replace-match-keep-properties "\\end{center}" t t
))
2037 (run-hooks 'org-export-latex-after-blockquotes-hook
)
2039 ;; Convert horizontal rules
2040 (goto-char (point-min))
2041 (while (re-search-forward "^----+.$" nil t
)
2043 (replace-match (org-export-latex-protect-string "\\hrule") t t
)))
2045 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
2046 (goto-char (point-min))
2049 "\\(?:<[^<>\n]*>\\)*"
2050 "\\(?:\\[[^][\n]*?\\]\\)*"
2051 "\\(?:<[^<>\n]*>\\)*"
2052 "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}")))
2053 (while (re-search-forward re nil t
)
2054 (unless (save-excursion (goto-char (match-beginning 0))
2055 (equal (char-after (point-at-bol)) ?
#))
2056 (add-text-properties (match-beginning 0) (match-end 0)
2057 '(org-protected t
)))))
2059 ;; Protect LaTeX entities
2060 (goto-char (point-min))
2062 (while (re-search-forward org-latex-entities-regexp nil t
)
2063 (if (setq a
(assoc (match-string 0) org-latex-entities-exceptions
))
2064 (replace-match (org-add-props (nth 1 a
) nil
'org-protected t
)
2066 (add-text-properties (match-beginning 0) (match-end 0)
2067 '(org-protected t
)))))
2069 ;; Replace radio links
2070 (goto-char (point-min))
2071 (while (re-search-forward
2072 (concat "<<<?" org-export-latex-all-targets-re
2073 ">>>?\\((INVISIBLE)\\)?") nil t
)
2074 (org-if-unprotected-at (+ (match-beginning 0) 2)
2077 (org-export-latex-protect-string
2078 (format "\\label{%s}" (save-match-data (org-solidify-link-text
2079 (match-string 1)))))
2080 (if (match-string 2) "" (match-string 1)))
2083 ;; Delete @<...> constructs
2084 ;; Thanks to Daniel Clemente for this regexp
2085 (goto-char (point-min))
2086 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t
)
2088 (replace-match "")))
2090 ;; When converting to LaTeX, replace footnotes
2091 ;; FIXME: don't protect footnotes from conversion
2092 (when (plist-get org-export-latex-options-plist
:footnotes
)
2093 (goto-char (point-min))
2094 (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t
)
2096 (when (and (save-match-data
2097 (save-excursion (beginning-of-line)
2098 (looking-at "[^:|#]")))
2099 (not (org-in-verbatim-emphasis)))
2100 (let ((foot-beg (match-beginning 0))
2101 (foot-end (match-end 0))
2102 (foot-prefix (match-string 0))
2103 footnote footnote-rpl
)
2105 (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix
))
2107 (replace-match (org-export-latex-protect-string
2108 (concat "$^{" (match-string 1) "}$")))
2110 (let ((end (save-excursion
2111 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t
)
2112 (match-beginning 0) (point-max)))))
2113 (setq footnote
(concat (org-trim (buffer-substring (point) end
))
2114 " ")) ; prevent last } being part of a link
2115 (delete-region (point) end
))
2116 (goto-char foot-beg
)
2117 (delete-region foot-beg foot-end
)
2118 (unless (null footnote
)
2119 (setq footnote-rpl
(format "\\footnote{%s}" footnote
))
2120 (add-text-properties 0 10 '(org-protected t
) footnote-rpl
)
2121 (add-text-properties (1- (length footnote-rpl
))
2122 (length footnote-rpl
)
2123 '(org-protected t
) footnote-rpl
)
2124 (if (org-on-heading-p)
2126 (concat (org-export-latex-protect-string "\\protect")
2128 (insert footnote-rpl
)))
2131 ;; Remove footnote section tag for LaTeX
2132 (goto-char (point-min))
2133 (while (re-search-forward
2134 (concat "^" footnote-section-tag-regexp
) nil t
)
2136 (replace-match "")))))
2138 (defun org-export-latex-fix-inputenc ()
2139 "Set the codingsystem in inputenc to what the buffer is."
2140 (let* ((cs buffer-file-coding-system
)
2141 (opt (or (ignore-errors (latexenc-coding-system-to-inputenc cs
))
2144 ;; Translate if that is requested
2145 (setq opt
(or (cdr (assoc opt org-export-latex-inputenc-alist
)) opt
))
2146 ;; find the \usepackage statement and replace the option
2147 (goto-char (point-min))
2148 (while (re-search-forward "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
2150 (goto-char (match-beginning 1))
2151 (delete-region (match-beginning 1) (match-end 1))
2153 (and buffer-file-name
2158 (defun org-export-latex-lists ()
2159 "Convert plain text lists in current buffer into LaTeX lists."
2160 (goto-char (point-min))
2161 (while (re-search-forward org-list-beginning-re nil t
)
2164 (insert (org-list-to-latex (org-list-parse-list t
)
2165 org-export-latex-list-parameters
))
2168 (defconst org-latex-entities
2197 "\\begin{description}"
2198 "\\begin{enumerate}"
2202 "\\begin{flushleft}"
2203 "\\begin{flushright}"
2208 "\\begin{quotation}"
2213 "\\begin{thebibliography}"
2215 "\\begin{titlepage}"
2343 "A list of LaTeX commands to be protected when performing conversion.")
2345 (defvar org-latex-entities-exceptions nil
)
2347 (defconst org-latex-entities-regexp
2349 (dolist (x org-latex-entities
)
2351 (add-to-list 'org-latex-entities-exceptions x
)
2353 (if (string-match "[a-zA-Z]$" x
)
2356 (concat "\\(" (regexp-opt (nreverse names
)) "\\>\\)"
2357 "\\|\\(" (regexp-opt (nreverse rest
)) "\\)")))
2359 (provide 'org-export-latex
)
2360 (provide 'org-latex
)
2362 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
2364 ;;; org-latex.el ends here