Make hyperref macro customizable
[rgr-org-mode.git] / lisp / org-latex.el
blobef575549e8939e516c60ffedb389d38615519db8
1 ;;; org-latex.el --- LaTeX exporter for org-mode
2 ;;
3 ;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
4 ;;
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: org-latex.el
7 ;; Version: 6.34trans
8 ;; Author: Bastien Guerry <bzg AT altern DOT org>
9 ;; Maintainer: Carsten Dominik <carsten.dominik AT gmail DOT com>
10 ;; Keywords: org, wp, tex
11 ;; Description: Converts an org-mode buffer into LaTeX
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;;; Commentary:
30 ;; This library implements a LaTeX exporter for org-mode.
32 ;; It is part of Org and will be autoloaded
34 ;; The interactive functions are similar to those of the HTML exporter:
36 ;; M-x `org-export-as-latex'
37 ;; M-x `org-export-as-pdf'
38 ;; M-x `org-export-as-pdf-and-open'
39 ;; M-x `org-export-as-latex-batch'
40 ;; M-x `org-export-as-latex-to-buffer'
41 ;; M-x `org-export-region-as-latex'
42 ;; M-x `org-replace-region-by-latex'
44 ;;; Code:
46 (eval-when-compile
47 (require 'cl))
49 (require 'footnote)
50 (require 'org)
51 (require 'org-exp)
52 (require 'org-macs)
53 (require 'org-beamer)
55 ;;; Variables:
56 (defvar org-export-latex-class nil)
57 (defvar org-export-latex-class-options nil)
58 (defvar org-export-latex-header nil)
59 (defvar org-export-latex-append-header nil)
60 (defvar org-export-latex-options-plist nil)
61 (defvar org-export-latex-todo-keywords-1 nil)
62 (defvar org-export-latex-complex-heading-re nil)
63 (defvar org-export-latex-not-done-keywords nil)
64 (defvar org-export-latex-done-keywords nil)
65 (defvar org-export-latex-display-custom-times nil)
66 (defvar org-export-latex-all-targets-re nil)
67 (defvar org-export-latex-add-level 0)
68 (defvar org-export-latex-sectioning "")
69 (defvar org-export-latex-sectioning-depth 0)
70 (defvar org-export-latex-special-keyword-regexp
71 (concat "\\<\\(" org-scheduled-string "\\|"
72 org-deadline-string "\\|"
73 org-closed-string"\\)")
74 "Regexp matching special time planning keywords plus the time after it.")
76 (defvar latexp) ; dynamically scoped from org.el
77 (defvar re-quote) ; dynamically scoped from org.el
78 (defvar commentsp) ; dynamically scoped from org.el
80 ;;; User variables:
82 (defgroup org-export-latex nil
83 "Options for exporting Org-mode files to LaTeX."
84 :tag "Org Export LaTeX"
85 :group 'org-export)
87 (defcustom org-export-latex-default-class "article"
88 "The default LaTeX class."
89 :group 'org-export-latex
90 :type '(string :tag "LaTeX class"))
92 (defcustom org-export-latex-classes
93 '(("article"
94 "\\documentclass[11pt]{article}
95 \\usepackage[utf8]{inputenc}
96 \\usepackage[T1]{fontenc}
97 \\usepackage{graphicx}
98 \\usepackage{longtable}
99 \\usepackage{float}
100 \\usepackage{wrapfig}
101 \\usepackage{soul}
102 \\usepackage{amssymb}
103 \\usepackage{hyperref}"
104 ("\\section{%s}" . "\\section*{%s}")
105 ("\\subsection{%s}" . "\\subsection*{%s}")
106 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
107 ("\\paragraph{%s}" . "\\paragraph*{%s}")
108 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
109 ("report"
110 "\\documentclass[11pt]{report}
111 \\usepackage[utf8]{inputenc}
112 \\usepackage[T1]{fontenc}
113 \\usepackage{graphicx}
114 \\usepackage{longtable}
115 \\usepackage{float}
116 \\usepackage{wrapfig}
117 \\usepackage{soul}
118 \\usepackage{amssymb}
119 \\usepackage{hyperref}"
120 ("\\part{%s}" . "\\part*{%s}")
121 ("\\chapter{%s}" . "\\chapter*{%s}")
122 ("\\section{%s}" . "\\section*{%s}")
123 ("\\subsection{%s}" . "\\subsection*{%s}")
124 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
125 ("book"
126 "\\documentclass[11pt]{book}
127 \\usepackage[utf8]{inputenc}
128 \\usepackage[T1]{fontenc}
129 \\usepackage{graphicx}
130 \\usepackage{longtable}
131 \\usepackage{float}
132 \\usepackage{wrapfig}
133 \\usepackage{soul}
134 \\usepackage{amssymb}
135 \\usepackage{hyperref}"
136 ("\\part{%s}" . "\\part*{%s}")
137 ("\\chapter{%s}" . "\\chapter*{%s}")
138 ("\\section{%s}" . "\\section*{%s}")
139 ("\\subsection{%s}" . "\\subsection*{%s}")
140 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
141 ("beamer"
142 "\\documentclass{beamer}
143 \\usepackage[utf8]{inputenc}
144 \\usepackage[T1]{fontenc}
145 \\usepackage{graphicx}
146 \\usepackage{longtable}
147 \\usepackage{float}
148 \\usepackage{wrapfig}
149 \\usepackage{soul}
150 \\usepackage{amssymb}
151 \\usepackage{hyperref}"
152 org-beamer-sectioning
154 "Alist of LaTeX classes and associated header and structure.
155 If #+LaTeX_CLASS is set in the buffer, use its value and the
156 associated information. Here is the structure of each cell:
158 \(class-name
159 header-string
160 (numbered-section . unnumbered-section\)
161 ...\)
163 A %s formatter is mandatory in each section string and will be
164 replaced by the title of the section.
166 Instead of a cons cell (numbered . unnumbered), you can also provide a list
167 of 2-4 elements,
169 (numbered-open numbered-close)
173 (numbered-open numbered-close unnumbered-open unnumbered-close)
175 providing opening and closing strings for an environment that should
176 represent the document section. The opening clause should have a %s
177 to represent the section title.
179 Instead of a list of sectioning commands, you can also specify a
180 function name. that function will be called with two parameters,
181 the (reduced) level of the headline, and the headline text. The functions
182 returns a cons cell with the (possibly modified) headline text, and the
183 sectioning list in the cdr."
184 :group 'org-export-latex
185 :type '(repeat
186 (list (string :tag "LaTeX class")
187 (string :tag "LaTeX header")
188 (repeat :tag "Levels" :inline t
189 (choice
190 (cons :tag "Heading"
191 (string :tag " numbered")
192 (string :tag "unnumbered"))
193 (list :tag "Environment"
194 (string :tag "Opening (numbered)")
195 (string :tag "Closing (numbered)")
196 (string :tag "Opening (unnumbered)")
197 (string :tag "Closing (unnumbered)"))
198 (function :tag "Hook computing sectioning"))))))
200 (defcustom org-export-latex-inputenc-alist nil
201 "Alist of inputenc coding system names, and what should really be used.
202 For example, adding an entry
204 (\"utf8\" . \"utf8x\")
206 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
207 are written as utf8 files."
208 :group 'org-export-latex
209 :type '(repeat
210 (cons
211 (string :tag "Derived from buffer")
212 (string :tag "Use this instead"))))
215 (defcustom org-export-latex-emphasis-alist
216 '(("*" "\\textbf{%s}" nil)
217 ("/" "\\emph{%s}" nil)
218 ("_" "\\underline{%s}" nil)
219 ("+" "\\st{%s}" nil)
220 ("=" "\\verb" t)
221 ("~" "\\verb" t))
222 "Alist of LaTeX expressions to convert emphasis fontifiers.
223 Each element of the list is a list of three elements.
224 The first element is the character used as a marker for fontification.
225 The second element is a formatting string to wrap fontified text with.
226 If it is \"\\verb\", Org will automatically select a delimiter
227 character that is not in the string.
228 The third element decides whether to protect converted text from other
229 conversions."
230 :group 'org-export-latex
231 :type 'alist)
233 (defcustom org-export-latex-title-command "\\maketitle"
234 "The command used to insert the title just after \\begin{document}.
235 If this string contains the formatting specification \"%s\" then
236 it will be used as a formatting string, passing the title as an
237 argument."
238 :group 'org-export-latex
239 :type 'string)
241 (defcustom org-export-latex-import-inbuffer-stuff nil
242 "Non-nil means define TeX macros for Org's inbuffer definitions.
243 For example \orgTITLE for #+TITLE."
244 :group 'org-export-latex
245 :type 'boolean)
247 (defcustom org-export-latex-date-format
248 "%d %B %Y"
249 "Format string for \\date{...}."
250 :group 'org-export-latex
251 :type 'string)
253 (defcustom org-export-latex-todo-keyword-markup "\\textbf{%s}"
254 "Markup for TODO keywords, as a printf format.
255 This can be a single format for all keywords, a cons cell with separate
256 formats for not-done and done states, or an association list with setup
257 for individual keywords. If a keyword shows up for which there is no
258 markup defined, the first one in the association list will be used."
259 :group 'org-export-latex
260 :type '(choice
261 (string :tag "Default")
262 (cons :tag "Distinguish undone and done"
263 (string :tag "Not-DONE states")
264 (string :tag "DONE states"))
265 (repeat :tag "Per keyword markup"
266 (cons
267 (string :tag "Keyword")
268 (string :tag "Markup")))))
270 (defcustom org-export-latex-timestamp-markup "\\textit{%s}"
271 "A printf format string to be applied to time stamps."
272 :group 'org-export-latex
273 :type 'string)
275 (defcustom org-export-latex-timestamp-keyword-markup "\\texttt{%s}"
276 "A printf format string to be applied to time stamps."
277 :group 'org-export-latex
278 :type 'string)
280 (defcustom org-export-latex-hyperref-format "\\href{%s}{%s}"
281 "A printf format string to be applied to hyperref links.
282 The format must contain two %s instances. The first will be filled with
283 the link, the second with the link description."
284 :group 'org-export-latex
285 :type 'string)
287 (defcustom org-export-latex-tables-verbatim nil
288 "When non-nil, tables are exported verbatim."
289 :group 'org-export-latex
290 :type 'boolean)
292 (defcustom org-export-latex-tables-centered t
293 "When non-nil, tables are exported in a center environment."
294 :group 'org-export-latex
295 :type 'boolean)
297 (defcustom org-export-latex-tables-column-borders nil
298 "When non-nil, grouping columns can cause outer vertical lines in tables.
299 When nil, grouping causes only separation lines between groups."
300 :group 'org-export-latex
301 :type 'boolean)
303 (defcustom org-export-latex-low-levels 'itemize
304 "How to convert sections below the current level of sectioning.
305 This is specified by the `org-export-headline-levels' option or the
306 value of \"H:\" in Org's #+OPTION line.
308 This can be either nil (skip the sections), `description', `itemize',
309 or `enumerate' (convert the sections as the corresponding list type), or
310 a string to be used instead of \\section{%s}. In this latter case,
311 the %s stands here for the inserted headline and is mandatory.
313 It may also be a list of three string to define a user-defined environment
314 that should be used. The first string should be the like
315 \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
316 to two occurrences of %s for the title and a label, respectively. The third
317 string should be like \"\\end{itemize\"."
318 :group 'org-export-latex
319 :type '(choice (const :tag "Ignore" nil)
320 (const :tag "Convert as descriptive list" description)
321 (const :tag "Convert as itemized list" itemize)
322 (const :tag "Convert as enumerated list" enumerate)
323 (list :tag "User-defined environment"
324 :value ("\\begin{itemize}" "\\end{itemize}" "\\item %s")
325 (string :tag "Start")
326 (string :tag "End")
327 (string :tag "item"))
328 (string :tag "Use a section string" :value "\\subparagraph{%s}")))
330 (defcustom org-export-latex-list-parameters
331 '(:cbon "$\\boxtimes$" :cboff "$\\Box$")
332 "Parameters for the LaTeX list exporter.
333 These parameters will be passed on to `org-list-to-latex', which in turn
334 will pass them (combined with the LaTeX default list parameters) to
335 `org-list-to-generic'."
336 :group 'org-export-latex
337 :type 'plist)
339 (defcustom org-export-latex-verbatim-wrap
340 '("\\begin{verbatim}\n" . "\\end{verbatim}\n")
341 "Environment to be wrapped around a fixed-width section in LaTeX export.
342 This is a cons with two strings, to be added before and after the
343 fixed-with text.
345 Defaults to \\begin{verbatim} and \\end{verbatim}."
346 :group 'org-export-translation
347 :group 'org-export-latex
348 :type '(cons (string :tag "Open")
349 (string :tag "Close")))
351 (defcustom org-export-latex-listings nil
352 "Non-nil means, export source code using the listings package.
353 This package will fontify source code, possibly even with color.
354 If you want to use this, you also need to make LaTeX use the
355 listings package, and if you want to have color, the color
356 package. Just add these to `org-export-latex-packages-alist',
357 for example using customize, or with something like
359 (require 'org-latex)
360 (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
361 (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))"
362 :group 'org-export-latex
363 :type 'boolean)
365 (defcustom org-export-latex-listings-langs
366 '((emacs-lisp "Lisp") (lisp "Lisp")
367 (c "C") (cc "C++")
368 (fortran "fortran")
369 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
370 (html "HTML") (xml "XML")
371 (tex "TeX") (latex "TeX")
372 (shell-script "bash")
373 (gnuplot "Gnuplot")
374 (ocaml "Caml") (caml "Caml")
375 (sql "SQL"))
376 "Alist mapping languages to their listing language counterpart.
377 The key is a symbol, the major mode symbol without the \"-mode\".
378 The value is the string that should be inserted as the language parameter
379 for the listings package. If the mode name and the listings name are
380 the same, the language does not need an entry in this list - but it does not
381 hurt if it is present."
382 :group 'org-export-latex
383 :type '(repeat
384 (list
385 (symbol :tag "Major mode ")
386 (string :tag "Listings language"))))
388 (defcustom org-export-latex-remove-from-headlines
389 '(:todo nil :priority nil :tags nil)
390 "A plist of keywords to remove from headlines. OBSOLETE.
391 Non-nil means remove this keyword type from the headline.
393 Don't remove the keys, just change their values.
395 Obsolete, this variable is no longer used. Use the separate
396 variables `org-export-with-todo-keywords', `org-export-with-priority',
397 and `org-export-with-tags' instead."
398 :type 'plist
399 :group 'org-export-latex)
401 (defcustom org-export-latex-image-default-option "width=10em"
402 "Default option for images."
403 :group 'org-export-latex
404 :type 'string)
406 (defcustom org-export-latex-inline-image-extensions
407 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
408 "Extensions of image files that can be inlined into LaTeX.
409 Note that the image extension *actually* allowed depend on the way the
410 LaTeX file is processed. When used with pdflatex, pdf, jpg and png images
411 are OK. When processing through dvi to Postscript, only ps and eps are
412 allowed. The default we use here encompasses both."
413 :group 'org-export-latex
414 :type '(repeat (string :tag "Extension")))
416 (defcustom org-export-latex-coding-system nil
417 "Coding system for the exported LaTex file."
418 :group 'org-export-latex
419 :type 'coding-system)
421 (defgroup org-export-pdf nil
422 "Options for exporting Org-mode files to PDF, via LaTeX."
423 :tag "Org Export PDF"
424 :group 'org-export-latex
425 :group 'org-export)
427 (defcustom org-latex-to-pdf-process
428 '("pdflatex -interaction nonstopmode %s"
429 "pdflatex -interaction nonstopmode %s")
430 "Commands to process a LaTeX file to a PDF file.
431 This is a list of strings, each of them will be given to the shell
432 as a command. %s in the command will be replaced by the full file name, %b
433 by the file base name (i.e. without extension).
434 The reason why this is a list is that it usually takes several runs of
435 pdflatex, maybe mixed with a call to bibtex. Org does not have a clever
436 mechanism to detect which of these commands have to be run to get to a stable
437 result, and it also does not do any error checking.
439 Alternatively, this may be a Lisp function that does the processing, so you
440 could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
441 This function should accept the file name as its single argument."
442 :group 'org-export-pdf
443 :type '(choice (repeat :tag "Shell command sequence"
444 (string :tag "Shell command"))
445 (function)))
447 (defcustom org-export-pdf-logfiles
448 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
449 "The list of file extensions to consider as LaTeX logfiles."
450 :group 'org-export-pdf
451 :type '(repeat (string :tag "Extension")))
453 (defcustom org-export-pdf-remove-logfiles t
454 "Non-nil means, remove the logfiles produced by PDF production.
455 These are the .aux, .log, .out, and .toc files."
456 :group 'org-export-pdf
457 :type 'boolean)
459 ;;; Hooks
461 (defvar org-export-latex-after-initial-vars-hook nil
462 "Hook run before LaTeX export.
463 The exact moment is after the initial variables like org-export-latex-class
464 have been determined from the environment.")
466 (defvar org-export-latex-after-blockquotes-hook nil
467 "Hook run during LaTeX export, after blockquote, verse, center are done.")
469 (defvar org-export-latex-final-hook nil
470 "Hook run in the finalized LaTeX buffer.")
472 (defvar org-export-latex-after-save-hook nil
473 "Hook run in the finalized LaTeX buffer, after it has been saved.")
475 ;;; Autoload functions:
477 ;;;###autoload
478 (defun org-export-as-latex-batch ()
479 "Call `org-export-as-latex', may be used in batch processing.
480 For example:
482 emacs --batch
483 --load=$HOME/lib/emacs/org.el
484 --eval \"(setq org-export-headline-levels 2)\"
485 --visit=MyFile --funcall org-export-as-latex-batch"
486 (org-export-as-latex org-export-headline-levels 'hidden))
488 ;;;###autoload
489 (defun org-export-as-latex-to-buffer (arg)
490 "Call `org-export-as-latex` with output to a temporary buffer.
491 No file is created. The prefix ARG is passed through to `org-export-as-latex'."
492 (interactive "P")
493 (org-export-as-latex arg nil nil "*Org LaTeX Export*")
494 (when org-export-show-temporary-export-buffer
495 (switch-to-buffer-other-window "*Org LaTeX Export*")))
497 ;;;###autoload
498 (defun org-replace-region-by-latex (beg end)
499 "Replace the region from BEG to END with its LaTeX export.
500 It assumes the region has `org-mode' syntax, and then convert it to
501 LaTeX. This can be used in any buffer. For example, you could
502 write an itemized list in `org-mode' syntax in an LaTeX buffer and
503 then use this command to convert it."
504 (interactive "r")
505 (let (reg latex buf)
506 (save-window-excursion
507 (if (org-mode-p)
508 (setq latex (org-export-region-as-latex
509 beg end t 'string))
510 (setq reg (buffer-substring beg end)
511 buf (get-buffer-create "*Org tmp*"))
512 (with-current-buffer buf
513 (erase-buffer)
514 (insert reg)
515 (org-mode)
516 (setq latex (org-export-region-as-latex
517 (point-min) (point-max) t 'string)))
518 (kill-buffer buf)))
519 (delete-region beg end)
520 (insert latex)))
522 ;;;###autoload
523 (defun org-export-region-as-latex (beg end &optional body-only buffer)
524 "Convert region from BEG to END in `org-mode' buffer to LaTeX.
525 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
526 contents, and only produce the region of converted text, useful for
527 cut-and-paste operations.
528 If BUFFER is a buffer or a string, use/create that buffer as a target
529 of the converted LaTeX. If BUFFER is the symbol `string', return the
530 produced LaTeX as a string and leave no buffer behind. For example,
531 a Lisp program could call this function in the following way:
533 (setq latex (org-export-region-as-latex beg end t 'string))
535 When called interactively, the output buffer is selected, and shown
536 in a window. A non-interactive call will only return the buffer."
537 (interactive "r\nP")
538 (when (interactive-p)
539 (setq buffer "*Org LaTeX Export*"))
540 (let ((transient-mark-mode t) (zmacs-regions t)
541 ext-plist rtn)
542 (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
543 (goto-char end)
544 (set-mark (point)) ;; to activate the region
545 (goto-char beg)
546 (setq rtn (org-export-as-latex
547 nil nil ext-plist
548 buffer body-only))
549 (if (fboundp 'deactivate-mark) (deactivate-mark))
550 (if (and (interactive-p) (bufferp rtn))
551 (switch-to-buffer-other-window rtn)
552 rtn)))
554 ;;;###autoload
555 (defun org-export-as-latex (arg &optional hidden ext-plist
556 to-buffer body-only pub-dir)
557 "Export current buffer to a LaTeX file.
558 If there is an active region, export only the region. The prefix
559 ARG specifies how many levels of the outline should become
560 headlines. The default is 3. Lower levels will be exported
561 depending on `org-export-latex-low-levels'. The default is to
562 convert them as description lists.
563 HIDDEN is obsolete and does nothing.
564 EXT-PLIST is a property list with
565 external parameters overriding org-mode's default settings, but
566 still inferior to file-local settings. When TO-BUFFER is
567 non-nil, create a buffer with that name and export to that
568 buffer. If TO-BUFFER is the symbol `string', don't leave any
569 buffer behind but just return the resulting LaTeX as a string.
570 When BODY-ONLY is set, don't produce the file header and footer,
571 simply return the content of \begin{document}...\end{document},
572 without even the \begin{document} and \end{document} commands.
573 when PUB-DIR is set, use this as the publishing directory."
574 (interactive "P")
575 (run-hooks 'org-export-first-hook)
577 ;; Make sure we have a file name when we need it.
578 (when (and (not (or to-buffer body-only))
579 (not buffer-file-name))
580 (if (buffer-base-buffer)
581 (org-set-local 'buffer-file-name
582 (with-current-buffer (buffer-base-buffer)
583 buffer-file-name))
584 (error "Need a file name to be able to export")))
586 (message "Exporting to LaTeX...")
587 (org-unmodified
588 (let ((inhibit-read-only t))
589 (remove-text-properties (point-min) (point-max)
590 '(:org-license-to-kill nil))))
591 (org-update-radio-target-regexp)
592 (org-export-latex-set-initial-vars ext-plist arg)
593 (setq org-export-opt-plist org-export-latex-options-plist)
594 (org-install-letbind)
595 (run-hooks 'org-export-latex-after-initial-vars-hook)
596 (let* ((wcf (current-window-configuration))
597 (opt-plist org-export-latex-options-plist)
598 (region-p (org-region-active-p))
599 (rbeg (and region-p (region-beginning)))
600 (rend (and region-p (region-end)))
601 (subtree-p
602 (if (plist-get opt-plist :ignore-subtree-p)
604 (when region-p
605 (save-excursion
606 (goto-char rbeg)
607 (and (org-at-heading-p)
608 (>= (org-end-of-subtree t t) rend))))))
609 (opt-plist (setq org-export-opt-plist
610 (if subtree-p
611 (org-export-add-subtree-options opt-plist rbeg)
612 opt-plist)))
613 ;; Make sure the variable contains the updated values.
614 (org-export-latex-options-plist (setq org-export-opt-plist opt-plist))
615 (title (or (and subtree-p (org-export-get-title-from-subtree))
616 (plist-get opt-plist :title)
617 (and (not
618 (plist-get opt-plist :skip-before-1st-heading))
619 (org-export-grab-title-from-buffer))
620 (file-name-sans-extension
621 (file-name-nondirectory buffer-file-name))))
622 (filename (concat (file-name-as-directory
623 (or pub-dir
624 (org-export-directory :LaTeX ext-plist)))
625 (file-name-sans-extension
626 (or (and subtree-p
627 (org-entry-get rbeg "EXPORT_FILE_NAME" t))
628 (file-name-nondirectory ;sans-extension
629 buffer-file-name)))
630 ".tex"))
631 (filename (if (equal (file-truename filename)
632 (file-truename buffer-file-name))
633 (concat filename ".tex")
634 filename))
635 (buffer (if to-buffer
636 (cond
637 ((eq to-buffer 'string) (get-buffer-create
638 "*Org LaTeX Export*"))
639 (t (get-buffer-create to-buffer)))
640 (find-file-noselect filename)))
641 (odd org-odd-levels-only)
642 (header (org-export-latex-make-header title opt-plist))
643 (skip (cond (subtree-p nil)
644 (region-p nil)
645 (t (plist-get opt-plist :skip-before-1st-heading))))
646 (text (plist-get opt-plist :text))
647 (org-export-preprocess-hook
648 (cons
649 `(lambda () (org-set-local 'org-complex-heading-regexp
650 ,org-export-latex-complex-heading-re))
651 org-export-preprocess-hook))
652 (first-lines (if skip "" (org-export-latex-first-lines
653 opt-plist
654 (if subtree-p
655 (save-excursion
656 (goto-char rbeg)
657 (point-at-bol 2))
658 rbeg)
659 (if region-p rend))))
660 (coding-system (and (boundp 'buffer-file-coding-system)
661 buffer-file-coding-system))
662 (coding-system-for-write (or org-export-latex-coding-system
663 coding-system))
664 (save-buffer-coding-system (or org-export-latex-coding-system
665 coding-system))
666 (region (buffer-substring
667 (if region-p (region-beginning) (point-min))
668 (if region-p (region-end) (point-max))))
669 (text
670 (and text (string-match "\\S-" text)
671 (org-export-preprocess-string
672 text
673 :emph-multiline t
674 :for-LaTeX t
675 :comments nil
676 :tags (plist-get opt-plist :tags)
677 :priority (plist-get opt-plist :priority)
678 :footnotes (plist-get opt-plist :footnotes)
679 :drawers (plist-get opt-plist :drawers)
680 :timestamps (plist-get opt-plist :timestamps)
681 :todo-keywords (plist-get opt-plist :todo-keywords)
682 :add-text nil
683 :skip-before-1st-heading skip
684 :select-tags nil
685 :exclude-tags nil
686 :LaTeX-fragments nil)))
687 (string-for-export
688 (org-export-preprocess-string
689 region
690 :emph-multiline t
691 :for-LaTeX t
692 :comments nil
693 :tags (plist-get opt-plist :tags)
694 :priority (plist-get opt-plist :priority)
695 :footnotes (plist-get opt-plist :footnotes)
696 :drawers (plist-get opt-plist :drawers)
697 :timestamps (plist-get opt-plist :timestamps)
698 :todo-keywords (plist-get opt-plist :todo-keywords)
699 :add-text (if (eq to-buffer 'string) nil text)
700 :skip-before-1st-heading skip
701 :select-tags (plist-get opt-plist :select-tags)
702 :exclude-tags (plist-get opt-plist :exclude-tags)
703 :LaTeX-fragments nil)))
705 (set-buffer buffer)
706 (erase-buffer)
707 (org-install-letbind)
709 (and (fboundp 'set-buffer-file-coding-system)
710 (set-buffer-file-coding-system coding-system-for-write))
712 ;; insert the header and initial document commands
713 (unless (or (eq to-buffer 'string) body-only)
714 (insert header))
716 ;; insert text found in #+TEXT
717 (when (and text (not (eq to-buffer 'string)))
718 (insert (org-export-latex-content
719 text '(lists tables fixed-width keywords))
720 "\n\n"))
722 ;; insert lines before the first headline
723 (unless skip
724 (insert first-lines))
726 ;; export the content of headlines
727 (org-export-latex-global
728 (with-temp-buffer
729 (insert string-for-export)
730 (goto-char (point-min))
731 (when (re-search-forward "^\\(\\*+\\) " nil t)
732 (let* ((asters (length (match-string 1)))
733 (level (if odd (- asters 2) (- asters 1))))
734 (setq org-export-latex-add-level
735 (if odd (1- (/ (1+ asters) 2)) (1- asters)))
736 (org-export-latex-parse-global level odd)))))
738 ;; finalization
739 (unless body-only (insert "\n\\end{document}"))
741 ;; Attach description terms to the \item macro
742 (goto-char (point-min))
743 (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t)
744 (delete-region (match-beginning 1) (match-end 1)))
746 ;; Relocate the table of contents
747 (goto-char (point-min))
748 (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
749 (goto-char (point-min))
750 (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t)
751 (replace-match ""))
752 (goto-char (point-min))
753 (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
754 (replace-match "\\tableofcontents" t t)))
756 ;; Cleanup forced line ends in items where they are not needed
757 (goto-char (point-min))
758 (while (re-search-forward
759 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin"
760 nil t)
761 (delete-region (match-beginning 1) (match-end 1)))
762 (goto-char (point-min))
763 (while (re-search-forward
764 "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*"
765 nil t)
766 (if (looking-at "[\n \t]+")
767 (replace-match "\n")))
769 (run-hooks 'org-export-latex-final-hook)
770 (or to-buffer (save-buffer))
771 (org-export-latex-fix-inputenc)
772 (run-hooks 'org-export-latex-after-save-hook)
773 (goto-char (point-min))
774 (or (org-export-push-to-kill-ring "LaTeX")
775 (message "Exporting to LaTeX...done"))
776 (prog1
777 (if (eq to-buffer 'string)
778 (prog1 (buffer-substring (point-min) (point-max))
779 (kill-buffer (current-buffer)))
780 (current-buffer))
781 (set-window-configuration wcf))))
783 ;;;###autoload
784 (defun org-export-as-pdf (arg &optional hidden ext-plist
785 to-buffer body-only pub-dir)
786 "Export as LaTeX, then process through to PDF."
787 (interactive "P")
788 (message "Exporting to PDF...")
789 (let* ((wconfig (current-window-configuration))
790 (lbuf (org-export-as-latex arg hidden ext-plist
791 to-buffer body-only pub-dir))
792 (file (buffer-file-name lbuf))
793 (base (file-name-sans-extension (buffer-file-name lbuf)))
794 (pdffile (concat base ".pdf"))
795 (cmds org-latex-to-pdf-process)
796 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
797 (bibtex-p (with-current-buffer lbuf
798 (save-excursion
799 (goto-char (point-min))
800 (re-search-forward "\\\\bibliography{" nil t))))
801 cmd)
802 (with-current-buffer outbuf (erase-buffer))
803 (message "Processing LaTeX file...")
804 (if (and cmds (symbolp cmds))
805 (funcall cmds file)
806 (while cmds
807 (setq cmd (pop cmds))
808 (while (string-match "%b" cmd)
809 (setq cmd (replace-match
810 (save-match-data
811 (shell-quote-argument base))
812 t t cmd)))
813 (while (string-match "%s" cmd)
814 (setq cmd (replace-match
815 (save-match-data
816 (shell-quote-argument file))
817 t t cmd)))
818 (shell-command cmd outbuf outbuf)))
819 (message "Processing LaTeX file...done")
820 (if (not (file-exists-p pdffile))
821 (error "PDF file was not produced")
822 (set-window-configuration wconfig)
823 (when org-export-pdf-remove-logfiles
824 (dolist (ext org-export-pdf-logfiles)
825 (setq file (concat base "." ext))
826 (and (file-exists-p file) (delete-file file))))
827 (message "Exporting to PDF...done")
828 pdffile)))
830 ;;;###autoload
831 (defun org-export-as-pdf-and-open (arg)
832 "Export as LaTeX, then process through to PDF, and open."
833 (interactive "P")
834 (let ((pdffile (org-export-as-pdf arg)))
835 (if pdffile
836 (org-open-file pdffile)
837 (error "PDF file was not produced"))))
839 ;;; Parsing functions:
841 (defun org-export-latex-parse-global (level odd)
842 "Parse the current buffer recursively, starting at LEVEL.
843 If ODD is non-nil, assume the buffer only contains odd sections.
844 Return a list reflecting the document structure."
845 (save-excursion
846 (goto-char (point-min))
847 (let* ((cnt 0) output
848 (depth org-export-latex-sectioning-depth))
849 (while (org-re-search-forward-unprotected
850 (concat "^\\(\\(?:\\*\\)\\{"
851 (number-to-string (+ (if odd 2 1) level))
852 "\\}\\) \\(.*\\)$")
853 ;; make sure that there is no upper heading
854 (when (> level 0)
855 (save-excursion
856 (save-match-data
857 (org-re-search-forward-unprotected
858 (concat "^\\(\\(?:\\*\\)\\{"
859 (number-to-string level)
860 "\\}\\) \\(.*\\)$") nil t)))) t)
861 (setq cnt (1+ cnt))
862 (let* ((pos (match-beginning 0))
863 (heading (match-string 2))
864 (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
865 (save-excursion
866 (narrow-to-region
867 (point)
868 (save-match-data
869 (if (org-re-search-forward-unprotected
870 (concat "^\\(\\(?:\\*\\)\\{"
871 (number-to-string (+ (if odd 2 1) level))
872 "\\}\\) \\(.*\\)$") nil t)
873 (match-beginning 0)
874 (point-max))))
875 (goto-char (point-min))
876 (setq output
877 (append output
878 (list
879 (list
880 `(pos . ,pos)
881 `(level . ,nlevel)
882 `(occur . ,cnt)
883 `(heading . ,heading)
884 `(content . ,(org-export-latex-parse-content))
885 `(subcontent . ,(org-export-latex-parse-subcontent
886 level odd)))))))
887 (widen)))
888 (list output))))
890 (defun org-export-latex-parse-content ()
891 "Extract the content of a section."
892 (let ((beg (point))
893 (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t)
894 (progn (beginning-of-line) (point))
895 (point-max))))
896 (buffer-substring beg end)))
898 (defun org-export-latex-parse-subcontent (level odd)
899 "Extract the subcontent of a section at LEVEL.
900 If ODD Is non-nil, assume subcontent only contains odd sections."
901 (if (not (org-re-search-forward-unprotected
902 (concat "^\\(\\(?:\\*\\)\\{"
903 (number-to-string (+ (if odd 4 2) level))
904 "\\}\\) \\(.*\\)$")
905 nil t))
906 nil ; subcontent is nil
907 (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
909 ;;; Rendering functions:
910 (defun org-export-latex-global (content)
911 "Export CONTENT to LaTeX.
912 CONTENT is an element of the list produced by
913 `org-export-latex-parse-global'."
914 (if (eq (car content) 'subcontent)
915 (mapc 'org-export-latex-sub (cdr content))
916 (org-export-latex-sub (car content))))
918 (defun org-export-latex-sub (subcontent)
919 "Export the list SUBCONTENT to LaTeX.
920 SUBCONTENT is an alist containing information about the headline
921 and its content."
922 (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
923 (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
925 (defun org-export-latex-subcontent (subcontent num)
926 "Export each cell of SUBCONTENT to LaTeX.
927 If NUM, export sections as numerical sections."
928 (let* ((heading (cdr (assoc 'heading subcontent)))
929 (level (- (cdr (assoc 'level subcontent))
930 org-export-latex-add-level))
931 (occur (number-to-string (cdr (assoc 'occur subcontent))))
932 (content (cdr (assoc 'content subcontent)))
933 (subcontent (cadr (assoc 'subcontent subcontent)))
934 (label (org-get-text-property-any 0 'target heading))
935 (label-list (cons label (cdr (assoc label
936 org-export-target-aliases))))
937 (sectioning org-export-latex-sectioning)
938 (depth org-export-latex-sectioning-depth)
939 main-heading sub-heading)
940 (when (symbolp (car sectioning))
941 (setq sectioning (funcall (car sectioning) level heading))
942 (when sectioning
943 (setq heading (car sectioning)
944 sectioning (cdr sectioning)
945 ;; target property migh have changed...
946 label (org-get-text-property-any 0 'target heading)
947 label-list (cons label (cdr (assoc label
948 org-export-target-aliases)))))
949 (if sectioning (setq sectioning (make-list 10 sectioning)))
950 (setq depth (if sectioning 10000 0)))
951 (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading)
952 (setq main-heading (substring heading 0 (match-beginning 0))
953 sub-heading (substring heading (match-end 0))))
954 (setq heading (org-export-latex-fontify-headline heading)
955 sub-heading (and sub-heading
956 (org-export-latex-fontify-headline sub-heading))
957 main-heading (and main-heading
958 (org-export-latex-fontify-headline main-heading)))
959 (cond
960 ;; Normal conversion
961 ((<= level depth)
962 (let* ((sec (nth (1- level) sectioning))
963 start end)
964 (if (consp (cdr sec))
965 (setq start (nth (if num 0 2) sec)
966 end (nth (if num 1 3) sec))
967 (setq start (if num (car sec) (cdr sec))))
968 (insert (format start (if main-heading main-heading heading)
969 (or sub-heading "")))
970 (insert "\n")
971 (when label
972 (insert (mapconcat (lambda (l) (format "\\label{%s}" l))
973 label-list "\n") "\n"))
974 (insert (org-export-latex-content content))
975 (cond ((stringp subcontent) (insert subcontent))
976 ((listp subcontent)
977 (while (org-looking-back "\n\n") (backward-delete-char 1))
978 (org-export-latex-sub subcontent)))
979 (when (and end (string-match "[^ \t]" end))
980 (let ((hook (org-get-text-property-any 0 'org-insert-hook end)))
981 (and (functionp hook) (funcall hook)))
982 (insert end "\n"))))
983 ;; At a level under the hl option: we can drop this subsection
984 ((> level depth)
985 (cond ((eq org-export-latex-low-levels 'description)
986 (if (string-match "% ends low level$"
987 (buffer-substring (point-at-bol 0) (point)))
988 (delete-region (point-at-bol 0) (point))
989 (insert "\\begin{description}\n"))
990 (insert (format "\n\\item[%s]%s~\n"
991 heading
992 (if label (format "\\label{%s}" label) "")))
993 (insert (org-export-latex-content content))
994 (cond ((stringp subcontent) (insert subcontent))
995 ((listp subcontent) (org-export-latex-sub subcontent)))
996 (insert "\\end{description} % ends low level\n"))
997 ((memq org-export-latex-low-levels '(itemize enumerate))
998 (if (string-match "% ends low level$"
999 (buffer-substring (point-at-bol 0) (point)))
1000 (delete-region (point-at-bol 0) (point))
1001 (insert (format "\\begin{%s}\n"
1002 (symbol-name org-export-latex-low-levels))))
1003 (insert (format "\n\\item %s\\\\\n%s%%"
1004 heading
1005 (if label (format "\\label{%s}" label) "")))
1006 (insert (org-export-latex-content content))
1007 (cond ((stringp subcontent) (insert subcontent))
1008 ((listp subcontent) (org-export-latex-sub subcontent)))
1009 (insert (format "\\end{%s} %% ends low level\n"
1010 (symbol-name org-export-latex-low-levels))))
1012 ((listp org-export-latex-low-levels)
1013 (if (string-match "% ends low level$"
1014 (buffer-substring (point-at-bol 0) (point)))
1015 (delete-region (point-at-bol 0) (point))
1016 (insert (car org-export-latex-low-levels) "\n"))
1017 (insert (format (nth 2 org-export-latex-low-levels)
1018 heading
1019 (if label (format "\\label{%s}" label) "")))
1020 (insert (org-export-latex-content content))
1021 (cond ((stringp subcontent) (insert subcontent))
1022 ((listp subcontent) (org-export-latex-sub subcontent)))
1023 (insert (nth 1 org-export-latex-low-levels)
1024 " %% ends low level\n"))
1026 ((stringp org-export-latex-low-levels)
1027 (insert (format org-export-latex-low-levels heading) "\n")
1028 (when label (insert (format "\\label{%s}\n" label)))
1029 (insert (org-export-latex-content content))
1030 (cond ((stringp subcontent) (insert subcontent))
1031 ((listp subcontent) (org-export-latex-sub subcontent)))))))))
1033 ;;; Exporting internals:
1034 (defun org-export-latex-set-initial-vars (ext-plist level)
1035 "Store org local variables required for LaTeX export.
1036 EXT-PLIST is an optional additional plist.
1037 LEVEL indicates the default depth for export."
1038 (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
1039 org-export-latex-done-keywords org-done-keywords
1040 org-export-latex-not-done-keywords org-not-done-keywords
1041 org-export-latex-complex-heading-re org-complex-heading-regexp
1042 org-export-latex-display-custom-times org-display-custom-times
1043 org-export-latex-all-targets-re
1044 (org-make-target-link-regexp (org-all-targets))
1045 org-export-latex-options-plist
1046 (org-combine-plists (org-default-export-plist) ext-plist
1047 (org-infile-export-plist))
1048 org-export-latex-class
1049 (or (and (org-region-active-p)
1050 (save-excursion
1051 (goto-char (region-beginning))
1052 (and (looking-at org-complex-heading-regexp)
1053 (org-entry-get nil "LaTeX_CLASS" 'selective))))
1054 (save-excursion
1055 (save-restriction
1056 (widen)
1057 (goto-char (point-min))
1058 (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
1059 (match-string 1))))
1060 (plist-get org-export-latex-options-plist :latex-class)
1061 org-export-latex-default-class)
1062 org-export-latex-class-options
1063 (or (and (org-region-active-p)
1064 (save-excursion
1065 (goto-char (region-beginning))
1066 (and (looking-at org-complex-heading-regexp)
1067 (org-entry-get nil "LaTeX_CLASS_OPTIONS" 'selective))))
1068 (save-excursion
1069 (save-restriction
1070 (widen)
1071 (goto-char (point-min))
1072 (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t)
1073 (match-string 1))))
1074 (plist-get org-export-latex-options-plist :latex-class-options))
1075 org-export-latex-class
1076 (or (car (assoc org-export-latex-class org-export-latex-classes))
1077 (error "No definition for class `%s' in `org-export-latex-classes'"
1078 org-export-latex-class))
1079 org-export-latex-header
1080 (cadr (assoc org-export-latex-class org-export-latex-classes))
1081 org-export-latex-sectioning
1082 (cddr (assoc org-export-latex-class org-export-latex-classes))
1083 org-export-latex-sectioning-depth
1084 (or level
1085 (let ((hl-levels
1086 (plist-get org-export-latex-options-plist :headline-levels))
1087 (sec-depth (length org-export-latex-sectioning)))
1088 (if (> hl-levels sec-depth) sec-depth hl-levels))))
1089 (when (and org-export-latex-class-options
1090 (string-match "\\S-" org-export-latex-class-options)
1091 (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?"
1092 org-export-latex-header))
1093 (setq org-export-latex-header
1094 (concat (substring org-export-latex-header 0 (match-end 1))
1095 org-export-latex-class-options
1096 (substring org-export-latex-header (match-end 0))))))
1098 (defvar org-export-latex-format-toc-function
1099 'org-export-latex-format-toc-default
1100 "The function formatting returning the string to createthe table of contents.
1101 The function mus take one parameter, the depth of the table of contents.")
1103 (defun org-export-latex-make-header (title opt-plist)
1104 "Make the LaTeX header and return it as a string.
1105 TITLE is the current title from the buffer or region.
1106 OPT-PLIST is the options plist for current buffer."
1107 (let ((toc (plist-get opt-plist :table-of-contents))
1108 (author (plist-get opt-plist :author)))
1109 (concat
1110 (if (plist-get opt-plist :time-stamp-file)
1111 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1112 ;; insert LaTeX custom header
1113 (org-export-apply-macros-in-string org-export-latex-header)
1114 "\n"
1115 ;; insert information on LaTeX packages
1116 (when org-export-latex-packages-alist
1117 (concat
1118 (mapconcat (lambda(p)
1119 (if (equal "" (car p))
1120 (format "\\usepackage{%s}" (cadr p))
1121 (format "\\usepackage[%s]{%s}"
1122 (car p) (cadr p))))
1123 org-export-latex-packages-alist "\n")
1124 "\n"))
1125 ;; insert additional commands in the header
1126 (org-export-apply-macros-in-string
1127 (plist-get opt-plist :latex-header-extra))
1128 (org-export-apply-macros-in-string org-export-latex-append-header)
1129 ;; insert the title
1130 (format
1131 "\n\n\\title{%s}\n"
1132 ;; convert the title
1133 (org-export-latex-content
1134 title '(lists tables fixed-width keywords)))
1135 ;; insert author info
1136 (if (plist-get opt-plist :author-info)
1137 (format "\\author{%s}\n"
1138 (org-export-latex-fontify-headline (or author user-full-name)))
1139 (format "%%\\author{%s}\n"
1140 (or author user-full-name)))
1141 ;; insert the date
1142 (format "\\date{%s}\n"
1143 (format-time-string
1144 (or (plist-get opt-plist :date)
1145 org-export-latex-date-format)))
1146 ;; beginning of the document
1147 "\n\\begin{document}\n\n"
1148 ;; insert the title command
1149 (when (string-match "\\S-" title)
1150 (if (string-match "%s" org-export-latex-title-command)
1151 (format org-export-latex-title-command title)
1152 org-export-latex-title-command))
1153 "\n\n"
1154 ;; table of contents
1155 (when (and org-export-with-toc
1156 (plist-get opt-plist :section-numbers))
1157 (funcall org-export-latex-format-toc-function
1158 (cond ((numberp toc)
1159 (min toc (plist-get opt-plist :headline-levels)))
1160 (toc (plist-get opt-plist :headline-levels))))))))
1162 (defun org-export-latex-format-toc-default (depth)
1163 (when depth
1164 (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
1165 depth)))
1167 (defun org-export-latex-first-lines (opt-plist &optional beg end)
1168 "Export the first lines before first headline.
1169 If BEG is non-nil, it is the beginning of the region.
1170 If END is non-nil, it is the end of the region."
1171 (save-excursion
1172 (goto-char (or beg (point-min)))
1173 (let* ((pt (point))
1174 (end (if (re-search-forward "^\\*+ " end t)
1175 (goto-char (match-beginning 0))
1176 (goto-char (or end (point-max))))))
1177 (prog1
1178 (org-export-latex-content
1179 (org-export-preprocess-string
1180 (buffer-substring pt end)
1181 :for-LaTeX t
1182 :emph-multiline t
1183 :add-text nil
1184 :comments nil
1185 :skip-before-1st-heading nil
1186 :LaTeX-fragments nil
1187 :timestamps (plist-get opt-plist :timestamps)
1188 :footnotes (plist-get opt-plist :footnotes)))
1189 (org-unmodified
1190 (let ((inhibit-read-only t))
1191 (add-text-properties pt (max pt (1- end))
1192 '(:org-license-to-kill t))))))))
1194 (defvar org-export-latex-header-defs nil
1195 "The header definitions that might be used in the LaTeX body.")
1196 (defvar org-export-latex-header-defs-re nil
1197 "The header definitions that might be used in the LaTeX body.")
1199 (defun org-export-latex-content (content &optional exclude-list)
1200 "Convert CONTENT string to LaTeX.
1201 Don't perform conversions that are in EXCLUDE-LIST. Recognized
1202 conversion types are: quotation-marks, emphasis, sub-superscript,
1203 links, keywords, lists, tables, fixed-width"
1204 (with-temp-buffer
1205 (insert content)
1206 (unless (memq 'timestamps exclude-list)
1207 (org-export-latex-time-stamps))
1208 (unless (memq 'quotation-marks exclude-list)
1209 (org-export-latex-quotation-marks))
1210 (unless (memq 'emphasis exclude-list)
1211 (when (plist-get org-export-latex-options-plist :emphasize)
1212 (org-export-latex-fontify)))
1213 (unless (memq 'sub-superscript exclude-list)
1214 (org-export-latex-special-chars
1215 (plist-get org-export-latex-options-plist :sub-superscript)))
1216 (unless (memq 'links exclude-list)
1217 (org-export-latex-links))
1218 (unless (memq 'keywords exclude-list)
1219 (org-export-latex-keywords))
1220 (unless (memq 'lists exclude-list)
1221 (org-export-latex-lists))
1222 (unless (memq 'tables exclude-list)
1223 (org-export-latex-tables
1224 (plist-get org-export-latex-options-plist :tables)))
1225 (unless (memq 'fixed-width exclude-list)
1226 (org-export-latex-fixed-width
1227 (plist-get org-export-latex-options-plist :fixed-width)))
1228 ;; return string
1229 (buffer-substring (point-min) (point-max))))
1231 (defun org-export-latex-protect-string (s)
1232 "Add the org-protected property to string S."
1233 (add-text-properties 0 (length s) '(org-protected t) s) s)
1235 (defun org-export-latex-protect-char-in-string (char-list string)
1236 "Add org-protected text-property to char from CHAR-LIST in STRING."
1237 (with-temp-buffer
1238 (save-match-data
1239 (insert string)
1240 (goto-char (point-min))
1241 (while (re-search-forward (regexp-opt char-list) nil t)
1242 (add-text-properties (match-beginning 0)
1243 (match-end 0) '(org-protected t)))
1244 (buffer-string))))
1246 (defun org-export-latex-keywords-maybe (&optional remove-list)
1247 "Maybe remove keywords depending on rules in REMOVE-LIST."
1248 (goto-char (point-min))
1249 (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
1250 (case-fold-search nil)
1251 (todo-markup org-export-latex-todo-keyword-markup)
1252 fmt)
1253 ;; convert TODO keywords
1254 (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
1255 (if (plist-get remove-list :todo)
1256 (replace-match "")
1257 (setq fmt (cond
1258 ((stringp todo-markup) todo-markup)
1259 ((and (consp todo-markup) (stringp (car todo-markup)))
1260 (if (member (match-string 1) org-export-latex-done-keywords)
1261 (cdr todo-markup) (car todo-markup)))
1262 (t (cdr (or (assoc (match-string 1) todo-markup)
1263 (car todo-markup))))))
1264 (replace-match (format fmt (match-string 1)) t t)))
1265 ;; convert priority string
1266 (when (re-search-forward "\\[\\\\#.\\]" nil t)
1267 (if (plist-get remove-list :priority)
1268 (replace-match "")
1269 (replace-match (format "\\textbf{%s}" (match-string 0)) t t)))
1270 ;; convert tags
1271 (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t)
1272 (if (or (not org-export-with-tags)
1273 (plist-get remove-list :tags))
1274 (replace-match "")
1275 (replace-match
1276 (org-export-latex-protect-string
1277 (format "\\textbf{%s}"
1278 (save-match-data
1279 (replace-regexp-in-string
1280 "_" "\\\\_" (match-string 0)))))
1281 t t)))))
1283 (defun org-export-latex-fontify-headline (string)
1284 "Fontify special words in STRING."
1285 (with-temp-buffer
1286 ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
1287 ;; the beginning of the buffer - inserting "\n" is safe here though.
1288 (insert "\n" string)
1289 (goto-char (point-min))
1290 (let ((re (concat "\\\\[a-zA-Z]+\\(?:"
1291 "\\[.*\\]"
1292 "\\)?"
1293 (org-create-multibrace-regexp "{" "}" 3))))
1294 (while (re-search-forward re nil t)
1295 (unless (save-excursion (goto-char (match-beginning 0))
1296 (equal (char-after (point-at-bol)) ?#))
1297 (add-text-properties (match-beginning 0) (match-end 0)
1298 '(org-protected t)))))
1299 (when (plist-get org-export-latex-options-plist :emphasize)
1300 (org-export-latex-fontify))
1301 (org-export-latex-keywords-maybe)
1302 (org-export-latex-special-chars
1303 (plist-get org-export-latex-options-plist :sub-superscript))
1304 (org-export-latex-links)
1305 (org-trim (buffer-string))))
1307 (defun org-export-latex-time-stamps ()
1308 "Format time stamps."
1309 (goto-char (point-min))
1310 (let ((org-display-custom-times org-export-latex-display-custom-times))
1311 (while (re-search-forward org-ts-regexp-both nil t)
1312 (org-if-unprotected-at (1- (point))
1313 (replace-match
1314 (org-export-latex-protect-string
1315 (format org-export-latex-timestamp-markup
1316 (substring (org-translate-time (match-string 0)) 1 -1)))
1317 t t)))))
1319 (defun org-export-latex-quotation-marks ()
1320 "Export quotation marks depending on language conventions."
1321 (let* ((lang (plist-get org-export-latex-options-plist :language))
1322 (quote-rpl (if (equal lang "fr")
1323 '(("\\(\\s-\\)\"" "«~")
1324 ("\\(\\S-\\)\"" "~»")
1325 ("\\(\\s-\\)'" "`"))
1326 '(("\\(\\s-\\|[[(]\\)\"" "``")
1327 ("\\(\\S-\\)\"" "''")
1328 ("\\(\\s-\\|(\\)'" "`")))))
1329 (mapc (lambda(l) (goto-char (point-min))
1330 (while (re-search-forward (car l) nil t)
1331 (let ((rpl (concat (match-string 1)
1332 (org-export-latex-protect-string
1333 (copy-sequence (cadr l))))))
1334 (org-if-unprotected-1
1335 (replace-match rpl t t))))) quote-rpl)))
1337 (defun org-export-latex-special-chars (sub-superscript)
1338 "Export special characters to LaTeX.
1339 If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
1340 See the `org-export-latex.el' code for a complete conversion table."
1341 (goto-char (point-min))
1342 (mapc (lambda(c)
1343 (goto-char (point-min))
1344 (while (re-search-forward c nil t)
1345 ;; Put the point where to check for org-protected
1346 (unless (get-text-property (match-beginning 2) 'org-protected)
1347 (cond ((member (match-string 2) '("\\$" "$"))
1348 (if (equal (match-string 2) "\\$")
1350 (replace-match "\\$" t t)))
1351 ((member (match-string 2) '("&" "%" "#"))
1352 (if (equal (match-string 1) "\\")
1353 (replace-match (match-string 2) t t)
1354 (replace-match (concat (match-string 1) "\\"
1355 (match-string 2)) t t)))
1356 ((equal (match-string 2) "...")
1357 (replace-match
1358 (concat (match-string 1)
1359 (org-export-latex-protect-string "\\ldots{}")) t t))
1360 ((equal (match-string 2) "~")
1361 (cond ((equal (match-string 1) "\\") nil)
1362 ((eq 'org-link (get-text-property 0 'face (match-string 2)))
1363 (replace-match (concat (match-string 1) "\\~") t t))
1364 (t (replace-match
1365 (org-export-latex-protect-string
1366 (concat (match-string 1) "\\~{}")) t t))))
1367 ((member (match-string 2) '("{" "}"))
1368 (unless (save-match-data (org-inside-latex-math-p))
1369 (if (equal (match-string 1) "\\")
1370 (replace-match (match-string 2) t t)
1371 (replace-match (concat (match-string 1) "\\"
1372 (match-string 2)) t t)))))
1373 (unless (save-match-data (org-inside-latex-math-p))
1374 (cond ((equal (match-string 2) "\\")
1375 (replace-match (or (save-match-data
1376 (org-export-latex-treat-backslash-char
1377 (match-string 1)
1378 (or (match-string 3) "")))
1379 "") t t))
1380 ((member (match-string 2) '("_" "^"))
1381 (replace-match (or (save-match-data
1382 (org-export-latex-treat-sub-super-char
1383 sub-superscript
1384 (match-string 2)
1385 (match-string 1)
1386 (match-string 3))) "") t t)
1387 (backward-char 1)))))))
1388 '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
1389 "\\(\\(\\\\?\\$\\)\\)"
1390 "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
1391 "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
1392 "\\(.\\|^\\)\\(&\\)"
1393 "\\(.\\|^\\)\\(#\\)"
1394 "\\(.\\|^\\)\\(%\\)"
1395 "\\(.\\|^\\)\\({\\)"
1396 "\\(.\\|^\\)\\(}\\)"
1397 "\\(.\\|^\\)\\(~\\)"
1398 "\\(.\\|^\\)\\(\\.\\.\\.\\)"
1399 ;; (?\< . "\\textless{}")
1400 ;; (?\> . "\\textgreater{}")
1403 (defun org-inside-latex-math-p ()
1404 (get-text-property (point) 'org-latex-math))
1406 (defun org-export-latex-treat-sub-super-char
1407 (subsup char string-before string-after)
1408 "Convert the \"_\" and \"^\" characters to LaTeX.
1409 SUBSUP corresponds to the ^: option in the #+OPTIONS line.
1410 Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
1411 (cond ((equal string-before "\\")
1412 (concat string-before char string-after))
1413 ((and (string-match "\\S-+" string-after))
1414 ;; this is part of a math formula
1415 (cond ((eq 'org-link (get-text-property 0 'face char))
1416 (concat string-before "\\" char string-after))
1417 ((save-match-data (org-inside-latex-math-p))
1418 (if subsup
1419 (cond ((eq 1 (length string-after))
1420 (concat string-before char string-after))
1421 ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
1422 (format "%s%s{%s}" string-before char
1423 (match-string 1 string-after))))))
1424 ((and (> (length string-after) 1)
1425 (or (eq subsup t)
1426 (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
1427 (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
1428 (org-export-latex-protect-string
1429 (format "%s$%s{%s}$" string-before char
1430 (if (and (> (match-end 1) (1+ (match-beginning 1)))
1431 (not (equal (substring string-after 0 2) "{\\")))
1432 (concat "\\mathrm{" (match-string 1 string-after) "}")
1433 (match-string 1 string-after)))))
1434 ((eq subsup t) (concat string-before "$" char string-after "$"))
1435 (t (org-export-latex-protect-string
1436 (concat string-before "\\" char "{}" string-after)))))
1437 (t (org-export-latex-protect-string
1438 (concat string-before "\\" char "{}" string-after)))))
1440 (defun org-export-latex-treat-backslash-char (string-before string-after)
1441 "Convert the \"$\" special character to LaTeX.
1442 The conversion is made depending of STRING-BEFORE and STRING-AFTER."
1443 (cond ((member (list string-after) org-html-entities)
1444 ;; backslash is part of a special entity (like "\alpha")
1445 (concat string-before "$\\"
1446 (or (cdar (member (list string-after) org-html-entities))
1447 string-after) "$"))
1448 ((and (not (string-match "^[ \n\t]" string-after))
1449 (not (string-match "[ \t]\\'\\|^" string-before)))
1450 ;; backslash is inside a word
1451 (concat string-before
1452 (org-export-latex-protect-string
1453 (concat "\\textbackslash{}" string-after))))
1454 ((not (or (equal string-after "")
1455 (string-match "^[ \t\n]" string-after)))
1456 ;; backslash might escape a character (like \#) or a user TeX
1457 ;; macro (like \setcounter)
1458 (concat string-before
1459 (org-export-latex-protect-string (concat "\\" string-after))))
1460 ((and (string-match "^[ \t\n]" string-after)
1461 (string-match "[ \t\n]\\'" string-before))
1462 ;; backslash is alone, convert it to $\backslash$
1463 (org-export-latex-protect-string
1464 (concat string-before "\\textbackslash{}" string-after)))
1465 (t (org-export-latex-protect-string
1466 (concat string-before "\\textbackslash{}" string-after)))))
1468 (defun org-export-latex-keywords ()
1469 "Convert special keywords to LaTeX."
1470 (goto-char (point-min))
1471 (while (re-search-forward org-export-latex-special-keyword-regexp nil t)
1472 (replace-match (format org-export-latex-timestamp-keyword-markup
1473 (match-string 0)) t t)
1474 (save-excursion
1475 (beginning-of-line 1)
1476 (unless (looking-at ".*\\\\newline[ \t]*$")
1477 (end-of-line 1)
1478 (insert "\\newline")))))
1480 (defun org-export-latex-fixed-width (opt)
1481 "When OPT is non-nil convert fixed-width sections to LaTeX."
1482 (goto-char (point-min))
1483 (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t)
1484 (if opt
1485 (progn (goto-char (match-beginning 0))
1486 (insert "\\begin{verbatim}\n")
1487 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1488 (replace-match (concat (match-string 1)
1489 (match-string 2)) t t)
1490 (forward-line))
1491 (insert "\\end{verbatim}\n\n"))
1492 (progn (goto-char (match-beginning 0))
1493 (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
1494 (replace-match (concat "%" (match-string 1)
1495 (match-string 2)) t t)
1496 (forward-line))))))
1499 (defvar org-table-last-alignment) ; defined in org-table.el
1500 (defvar org-table-last-column-widths) ; defined in org-table.el
1501 (declare-function orgtbl-to-latex "org-table" (table params) t)
1502 (defun org-export-latex-tables (insert)
1503 "Convert tables to LaTeX and INSERT it."
1504 (goto-char (point-min))
1505 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
1506 (org-if-unprotected-at (1- (point))
1507 (org-table-align)
1508 (let* ((beg (org-table-begin))
1509 (end (org-table-end))
1510 (raw-table (buffer-substring beg end))
1511 (org-table-last-alignment (copy-sequence org-table-last-alignment))
1512 (org-table-last-column-widths (copy-sequence
1513 org-table-last-column-widths))
1514 fnum fields line lines olines gr colgropen line-fmt align
1515 caption label attr floatp longtblp)
1516 (if org-export-latex-tables-verbatim
1517 (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
1518 "\\end{verbatim}\n")))
1519 (apply 'delete-region (list beg end))
1520 (insert (org-export-latex-protect-string tbl)))
1521 (progn
1522 (setq caption (org-find-text-property-in-string
1523 'org-caption raw-table)
1524 attr (org-find-text-property-in-string
1525 'org-attributes raw-table)
1526 label (org-find-text-property-in-string
1527 'org-label raw-table)
1528 longtblp (and attr (stringp attr)
1529 (string-match "\\<longtable\\>" attr))
1530 align (and attr (stringp attr)
1531 (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
1532 (match-string 1 attr))
1533 floatp (or caption label))
1534 (setq lines (org-split-string raw-table "\n"))
1535 (apply 'delete-region (list beg end))
1536 (when org-export-table-remove-special-lines
1537 (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
1538 (when org-table-clean-did-remove-column
1539 (pop org-table-last-alignment)
1540 (pop org-table-last-column-widths))
1541 ;; make a formatting string to reflect alignment
1542 (setq olines lines)
1543 (while (and (not line-fmt) (setq line (pop olines)))
1544 (unless (string-match "^[ \t]*|-" line)
1545 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1546 (setq fnum (make-vector (length fields) 0))
1547 (setq line-fmt
1548 (mapconcat
1549 (lambda (x)
1550 (setq gr (pop org-table-colgroup-info))
1551 (format "%s%%s%s"
1552 (cond ((eq gr :start)
1553 (prog1 (if colgropen "|" "|")
1554 (setq colgropen t)))
1555 ((eq gr :startend)
1556 (prog1 (if colgropen "|" "|")
1557 (setq colgropen nil)))
1558 (t ""))
1559 (if (memq gr '(:end :startend))
1560 (progn (setq colgropen nil) "|")
1561 "")))
1562 fnum ""))))
1563 ;; fix double || in line-fmt
1564 (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
1565 ;; maybe remove the first and last "|"
1566 (when (and (not org-export-latex-tables-column-borders)
1567 (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
1568 (setq line-fmt (match-string 2 line-fmt)))
1569 ;; format alignment
1570 (unless align
1571 (setq align (apply 'format
1572 (cons line-fmt
1573 (mapcar (lambda (x) (if x "r" "l"))
1574 org-table-last-alignment)))))
1575 ;; prepare the table to send to orgtbl-to-latex
1576 (setq lines
1577 (mapcar
1578 (lambda(elem)
1579 (or (and (string-match "[ \t]*|-+" elem) 'hline)
1580 (org-split-string (org-trim elem) "|")))
1581 lines))
1582 (when insert
1583 (insert (org-export-latex-protect-string
1584 (concat
1585 (if longtblp
1586 (concat "\\begin{longtable}{" align "}\n")
1587 (if floatp "\\begin{table}[htb]\n"))
1588 (if floatp
1589 (format
1590 "\\caption{%s%s}"
1591 (if label (concat "\\\label{" label "}") "")
1592 (or caption "")))
1593 (if (and longtblp caption) "\\\\\n" "\n")
1594 (if (and org-export-latex-tables-centered (not longtblp))
1595 "\\begin{center}\n")
1596 (if (not longtblp) (concat "\\begin{tabular}{" align "}\n"))
1597 (orgtbl-to-latex
1598 lines
1599 `(:tstart nil :tend nil
1600 :hlend ,(if longtblp
1601 (format "\\\\
1602 \\hline
1603 \\endhead
1604 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
1605 \\endfoot
1606 \\endlastfoot" (length org-table-last-alignment))
1607 nil)))
1608 (if (not longtblp) (concat "\n\\end{tabular}"))
1609 (if longtblp "\n" (if org-export-latex-tables-centered
1610 "\n\\end{center}\n" "\n"))
1611 (if longtblp
1612 "\\end{longtable}"
1613 (if floatp "\\end{table}"))))
1614 "\n\n"))))))))
1616 (defun org-export-latex-fontify ()
1617 "Convert fontification to LaTeX."
1618 (goto-char (point-min))
1619 (while (re-search-forward org-emph-re nil t)
1620 ;; The match goes one char after the *string*, except at the end of a line
1621 (let ((emph (assoc (match-string 3)
1622 org-export-latex-emphasis-alist))
1623 (beg (match-beginning 0))
1624 (end (match-end 0))
1625 rpl s)
1626 (unless emph
1627 (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
1628 (match-string 3)))
1629 (unless (or (and (get-text-property (- (point) 2) 'org-protected)
1630 (not (get-text-property
1631 (- (point) 2) 'org-verbatim-emph)))
1632 (save-excursion
1633 (goto-char (match-beginning 1))
1634 (save-match-data
1635 (and (org-at-table-p)
1636 (string-match
1637 "[|\n]" (buffer-substring beg end))))))
1638 (setq s (match-string 4))
1639 (setq rpl (concat (match-string 1)
1640 (org-export-latex-emph-format (cadr emph)
1641 (match-string 4))
1642 (match-string 5)))
1643 (if (caddr emph)
1644 (setq rpl (org-export-latex-protect-string rpl))
1645 (save-match-data
1646 (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl)
1647 (progn
1648 (add-text-properties (match-beginning 1) (match-end 1)
1649 '(org-protected t) rpl)
1650 (add-text-properties (match-beginning 3) (match-end 3)
1651 '(org-protected t) rpl)))))
1652 (replace-match rpl t t)))
1653 (backward-char)))
1655 (defvar org-export-latex-use-verb nil)
1656 (defun org-export-latex-emph-format (format string)
1657 "Format an emphasis string and handle the \\verb special case."
1658 (when (equal format "\\verb")
1659 (save-match-data
1660 (if org-export-latex-use-verb
1661 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1662 (catch 'exit
1663 (loop for i from 0 to (1- (length ll)) do
1664 (if (not (string-match (regexp-quote (substring ll i (1+ i)))
1665 string))
1666 (progn
1667 (setq format (concat "\\verb" (substring ll i (1+ i))
1668 "%s" (substring ll i (1+ i))))
1669 (throw 'exit nil))))))
1670 (let ((start 0)
1671 (trans '(("\\" . "\\textbackslash{}")
1672 ("~" . "\\textasciitilde{}")
1673 ("^" . "\\textasciicircum{}")))
1674 (rtn "") char)
1675 (while (string-match "[\\{}$%&_#~^]" string)
1676 (setq char (match-string 0 string))
1677 (if (> (match-beginning 0) 0)
1678 (setq rtn (concat rtn (substring string
1679 0 (match-beginning 0)))))
1680 (setq string (substring string (1+ (match-beginning 0))))
1681 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1682 rtn (concat rtn char)))
1683 (setq string (concat rtn string) format "\\texttt{%s}")))))
1684 (format format string))
1686 (defun org-export-latex-links ()
1687 ;; Make sure to use the LaTeX hyperref and graphicx package
1688 ;; or send some warnings.
1689 "Convert links to LaTeX."
1690 (goto-char (point-min))
1691 (while (re-search-forward org-bracket-link-analytic-regexp++ nil t)
1692 (org-if-unprotected-1
1693 (goto-char (match-beginning 0))
1694 (let* ((re-radio org-export-latex-all-targets-re)
1695 (remove (list (match-beginning 0) (match-end 0)))
1696 (raw-path (org-extract-attributes (match-string 3)))
1697 (full-raw-path (concat (match-string 1) raw-path))
1698 (desc (match-string 5))
1699 (type (or (match-string 2)
1700 (if (or (file-name-absolute-p raw-path)
1701 (string-match "^\\.\\.?/" raw-path))
1702 "file")))
1703 (coderefp (equal type "coderef"))
1704 (caption (org-find-text-property-in-string 'org-caption raw-path))
1705 (attr (or (org-find-text-property-in-string 'org-attributes raw-path)
1706 (plist-get org-export-latex-options-plist :latex-image-options)))
1707 (label (org-find-text-property-in-string 'org-label raw-path))
1708 imgp radiop
1709 ;; define the path of the link
1710 (path (cond
1711 ((member type '("coderef"))
1712 raw-path)
1713 ((member type '("http" "https" "ftp"))
1714 (concat type ":" raw-path))
1715 ((and re-radio (string-match re-radio raw-path))
1716 (setq radiop t))
1717 ((equal type "mailto")
1718 (concat type ":" raw-path))
1719 ((equal type "file")
1720 (if (and (org-file-image-p
1721 (expand-file-name
1722 raw-path)
1723 org-export-latex-inline-image-extensions)
1724 (or (get-text-property 0 'org-no-description
1725 raw-path)
1726 (equal desc full-raw-path)))
1727 (setq imgp t)
1728 (progn (when (string-match "\\(.+\\)::.+" raw-path)
1729 (setq raw-path (match-string 1 raw-path)))
1730 (if (file-exists-p raw-path)
1731 (concat type "://" (expand-file-name raw-path))
1732 (concat type "://" (org-export-directory
1733 :LaTeX org-export-latex-options-plist)
1734 raw-path))))))))
1735 ;; process with link inserting
1736 (apply 'delete-region remove)
1737 (cond ((and imgp
1738 (plist-get org-export-latex-options-plist :inline-images))
1739 ;; OK, we need to inline an image
1740 (insert
1741 (org-export-latex-format-image raw-path caption label attr)))
1742 (coderefp
1743 (insert (format
1744 (org-export-get-coderef-format path desc)
1745 (cdr (assoc path org-export-code-refs)))))
1746 (radiop (insert (format "\\hyperref[%s]{%s}"
1747 (org-solidify-link-text raw-path) desc)))
1748 ((not type)
1749 (insert (format "\\hyperref[%s]{%s}"
1750 (org-remove-initial-hash
1751 (org-solidify-link-text raw-path))
1752 desc)))
1753 (path
1754 (when (org-at-table-p)
1755 ;; There is a strange problem when we have a link in a table,
1756 ;; ampersands then cause a problem. I think this must be
1757 ;; a LaTeX issue, but we here implement a work-around anyway.
1758 (setq path (org-export-latex-protect-amp path)
1759 desc (org-export-latex-protect-amp desc)))
1760 (insert (format org-export-latex-hyperref-format path desc)))
1761 (t (insert "\\texttt{" desc "}")))))))
1764 (defun org-export-latex-format-image (path caption label attr)
1765 "Format the image element, depending on user settings."
1766 (let (ind floatp wrapp placement figenv)
1767 (setq floatp (or caption label))
1768 (setq ind (org-get-text-property-any 0 'original-indentation path))
1769 (when (and attr (stringp attr))
1770 (if (string-match "[ \t]*\\<wrap\\>" attr)
1771 (setq wrapp t floatp nil attr (replace-match "" t t attr)))
1772 (if (string-match "[ \t]*\\<float\\>" attr)
1773 (setq wrapp nil floatp t attr (replace-match "" t t attr))))
1775 (setq placement
1776 (cond
1777 (wrapp "{l}{0.5\\textwidth}")
1778 (floatp "[htb]")
1779 (t "")))
1781 (when (and attr (stringp attr)
1782 (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr))
1783 (setq placement (match-string 1 attr)
1784 attr (replace-match "" t t attr)))
1785 (setq attr (and attr (org-trim attr)))
1786 (when (or (not attr) (= (length attr) 0))
1787 (setq attr (cond (floatp "width=0.7\\textwidth")
1788 (wrapp "width=0.48\\textwidth")
1789 (t attr))))
1790 (setq figenv
1791 (cond
1792 (wrapp "\\begin{wrapfigure}%placement
1793 \\centering
1794 \\includegraphics[%attr]{%path}
1795 \\caption{%labelcmd%caption}
1796 \\end{wrapfigure}")
1797 (floatp "\\begin{figure}%placement
1798 \\centering
1799 \\includegraphics[%attr]{%path}
1800 \\caption{%labelcmd%caption}
1801 \\end{figure}")
1802 (t "\\includegraphics[%attr]{%path}")))
1805 (setq figenv (mapconcat 'identity (split-string figenv "\n")
1806 (save-excursion (beginning-of-line 1)
1807 (looking-at "[ \t]*")
1808 (concat "\n" (match-string 0)))))
1810 (if (and (not label) (not caption)
1811 (string-match "^\\\\caption{.*\n" figenv))
1812 (setq figenv (replace-match "" t t figenv)))
1813 (org-add-props
1814 (org-fill-template
1815 figenv
1816 (list (cons "path"
1817 (if (file-name-absolute-p path)
1818 (expand-file-name path)
1819 path))
1820 (cons "attr" attr)
1821 (cons "labelcmd" (if label (format "\\label{%s}"
1822 label)""))
1823 (cons "caption" (or caption ""))
1824 (cons "placement" (or placement ""))))
1825 nil 'original-indentation ind)))
1827 (defun org-export-latex-protect-amp (s)
1828 (while (string-match "\\([^\\\\]\\)\\(&\\)" s)
1829 (setq s (replace-match (concat (match-string 1 s) "\\" (match-string 2 s))
1830 t t s)))
1833 (defun org-remove-initial-hash (s)
1834 (if (string-match "\\`#" s)
1835 (substring s 1)
1837 (defvar org-latex-entities) ; defined below
1838 (defvar org-latex-entities-regexp) ; defined below
1839 (defvar org-latex-entities-exceptions) ; defined below
1841 (defun org-export-latex-preprocess (parameters)
1842 "Clean stuff in the LaTeX export."
1843 ;; Preserve line breaks
1844 (goto-char (point-min))
1845 (while (re-search-forward "\\\\\\\\" nil t)
1846 (add-text-properties (match-beginning 0) (match-end 0)
1847 '(org-protected t)))
1849 ;; Preserve latex environments
1850 (goto-char (point-min))
1851 (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t)
1852 (let* ((start (progn (beginning-of-line) (point)))
1853 (end (and (re-search-forward
1854 (concat "^[ \t]*\\\\end{"
1855 (regexp-quote (match-string 1))
1856 "}") nil t)
1857 (point-at-eol))))
1858 (if end
1859 (add-text-properties start end '(org-protected t))
1860 (goto-char (point-at-eol)))))
1862 ;; Preserve math snippets
1864 (let* ((matchers (plist-get org-format-latex-options :matchers))
1865 (re-list org-latex-regexps)
1866 beg end re e m n block off)
1867 ;; Check the different regular expressions
1868 (while (setq e (pop re-list))
1869 (setq m (car e) re (nth 1 e) n (nth 2 e)
1870 block (if (nth 3 e) "\n\n" ""))
1871 (setq off (if (member m '("$" "$1")) 1 0))
1872 (when (and (member m matchers) (not (equal m "begin")))
1873 (goto-char (point-min))
1874 (while (re-search-forward re nil t)
1875 (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
1876 (add-text-properties beg end '(org-protected t org-latex-math t))))))
1878 ;; Convert LaTeX to \LaTeX{}
1879 (goto-char (point-min))
1880 (let ((case-fold-search nil))
1881 (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
1882 (org-if-unprotected
1883 (replace-match (org-export-latex-protect-string
1884 (concat (match-string 1) "\\LaTeX{}")) t t))))
1886 ;; Convert blockquotes
1887 (goto-char (point-min))
1888 (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
1889 (org-replace-match-keep-properties "\\begin{quote}" t t))
1890 (goto-char (point-min))
1891 (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
1892 (org-replace-match-keep-properties "\\end{quote}" t t))
1894 ;; Convert verse
1895 (goto-char (point-min))
1896 (while (search-forward "ORG-VERSE-START" nil t)
1897 (org-replace-match-keep-properties "\\begin{verse}" t t)
1898 (beginning-of-line 2)
1899 (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
1900 (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
1901 (goto-char (match-end 1))
1902 (org-replace-match-keep-properties
1903 (org-export-latex-protect-string
1904 (concat "\\hspace*{1cm}" (match-string 2))) t t)
1905 (beginning-of-line 1))
1906 (if (looking-at "[ \t]*$")
1907 (insert (org-export-latex-protect-string "\\vspace*{1em}"))
1908 (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
1909 (end-of-line 1)
1910 (insert "\\\\")))
1911 (beginning-of-line 2))
1912 (and (looking-at "[ \t]*ORG-VERSE-END.*")
1913 (org-replace-match-keep-properties "\\end{verse}" t t)))
1915 ;; Convert center
1916 (goto-char (point-min))
1917 (while (search-forward "ORG-CENTER-START" nil t)
1918 (org-replace-match-keep-properties "\\begin{center}" t t))
1919 (goto-char (point-min))
1920 (while (search-forward "ORG-CENTER-END" nil t)
1921 (org-replace-match-keep-properties "\\end{center}" t t))
1923 (run-hooks 'org-export-latex-after-blockquotes-hook)
1925 ;; Convert horizontal rules
1926 (goto-char (point-min))
1927 (while (re-search-forward "^----+.$" nil t)
1928 (org-if-unprotected
1929 (replace-match (org-export-latex-protect-string "\\hrule") t t)))
1931 ;; Protect LaTeX commands like \command[...]{...} or \command{...}
1932 (goto-char (point-min))
1933 (let ((re (concat
1934 "\\\\[a-zA-Z]+"
1935 "\\(?:\\[[^][\n]*?\\]\\)*"
1936 "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}")))
1937 (while (re-search-forward re nil t)
1938 (unless (save-excursion (goto-char (match-beginning 0))
1939 (equal (char-after (point-at-bol)) ?#))
1940 (add-text-properties (match-beginning 0) (match-end 0)
1941 '(org-protected t)))))
1943 ;; Protect LaTeX entities
1944 (goto-char (point-min))
1945 (let (a)
1946 (while (re-search-forward org-latex-entities-regexp nil t)
1947 (if (setq a (assoc (match-string 0) org-latex-entities-exceptions))
1948 (replace-match (org-add-props (nth 1 a) nil 'org-protected t)
1949 t t)
1950 (add-text-properties (match-beginning 0) (match-end 0)
1951 '(org-protected t)))))
1953 ;; Replace radio links
1954 (goto-char (point-min))
1955 (while (re-search-forward
1956 (concat "<<<?" org-export-latex-all-targets-re
1957 ">>>?\\((INVISIBLE)\\)?") nil t)
1958 (org-if-unprotected-at (+ (match-beginning 0) 2)
1959 (replace-match
1960 (concat
1961 (org-export-latex-protect-string
1962 (format "\\label{%s}" (save-match-data (org-solidify-link-text
1963 (match-string 1)))))
1964 (if (match-string 2) "" (match-string 1)))
1965 t t)))
1967 ;; Delete @<...> constructs
1968 ;; Thanks to Daniel Clemente for this regexp
1969 (goto-char (point-min))
1970 (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
1971 (org-if-unprotected
1972 (replace-match "")))
1974 ;; When converting to LaTeX, replace footnotes
1975 ;; FIXME: don't protect footnotes from conversion
1976 (when (plist-get org-export-latex-options-plist :footnotes)
1977 (goto-char (point-min))
1978 (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t)
1979 (org-if-unprotected
1980 (when (and (save-match-data
1981 (save-excursion (beginning-of-line)
1982 (looking-at "[^:|#]")))
1983 (not (org-in-verbatim-emphasis)))
1984 (let ((foot-beg (match-beginning 0))
1985 (foot-end (match-end 0))
1986 (foot-prefix (match-string 0))
1987 footnote footnote-rpl)
1988 (save-excursion
1989 (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix))
1990 nil t))
1991 (replace-match (org-export-latex-protect-string
1992 (concat "$^{" (match-string 1) "}$")))
1993 (replace-match "")
1994 (let ((end (save-excursion
1995 (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
1996 (match-beginning 0) (point-max)))))
1997 (setq footnote (concat (org-trim (buffer-substring (point) end))
1998 " ")) ; prevent last } being part of a link
1999 (delete-region (point) end))
2000 (goto-char foot-beg)
2001 (delete-region foot-beg foot-end)
2002 (unless (null footnote)
2003 (setq footnote-rpl (format "\\footnote{%s}" footnote))
2004 (add-text-properties 0 10 '(org-protected t) footnote-rpl)
2005 (add-text-properties (1- (length footnote-rpl))
2006 (length footnote-rpl)
2007 '(org-protected t) footnote-rpl)
2008 (insert footnote-rpl)))
2009 )))))
2011 ;; Remove footnote section tag for LaTeX
2012 (goto-char (point-min))
2013 (while (re-search-forward
2014 (concat "^" footnote-section-tag-regexp) nil t)
2015 (org-if-unprotected
2016 (replace-match "")))))
2018 (defun org-export-latex-fix-inputenc ()
2019 "Set the codingsystem in inputenc to what the buffer is."
2020 (let* ((cs buffer-file-coding-system)
2021 (opt (latexenc-coding-system-to-inputenc cs)))
2022 (when opt
2023 ;; Translate if that is requested
2024 (setq opt (or (cdr (assoc opt org-export-latex-inputenc-alist)) opt))
2025 ;; find the \usepackage statement and replace the option
2026 (goto-char (point-min))
2027 (while (re-search-forward "\\\\usepackage\\[\\(.*?\\)\\]{inputenc}"
2028 nil t)
2029 (goto-char (match-beginning 1))
2030 (delete-region (match-beginning 1) (match-end 1))
2031 (insert opt))
2032 (save-buffer))))
2034 ;;; List handling:
2036 (defun org-export-latex-lists ()
2037 "Convert plain text lists in current buffer into LaTeX lists."
2038 (goto-char (point-min))
2039 (while (re-search-forward org-list-beginning-re nil t)
2040 (org-if-unprotected
2041 (beginning-of-line)
2042 (insert (org-list-to-latex (org-list-parse-list t)
2043 org-export-latex-list-parameters))
2044 "\n")))
2046 (defconst org-latex-entities
2047 '("\\!"
2048 "\\'"
2049 "\\+"
2050 "\\,"
2051 "\\-"
2052 "\\:"
2053 "\\;"
2054 "\\<"
2055 "\\="
2056 "\\>"
2057 "\\Huge"
2058 "\\LARGE"
2059 "\\Large"
2060 "\\Styles"
2061 "\\\\"
2062 "\\`"
2063 "\\addcontentsline"
2064 "\\address"
2065 "\\addtocontents"
2066 "\\addtocounter"
2067 "\\addtolength"
2068 "\\addvspace"
2069 "\\alph"
2070 "\\appendix"
2071 "\\arabic"
2072 "\\author"
2073 "\\begin{array}"
2074 "\\begin{center}"
2075 "\\begin{description}"
2076 "\\begin{enumerate}"
2077 "\\begin{eqnarray}"
2078 "\\begin{equation}"
2079 "\\begin{figure}"
2080 "\\begin{flushleft}"
2081 "\\begin{flushright}"
2082 "\\begin{itemize}"
2083 "\\begin{list}"
2084 "\\begin{minipage}"
2085 "\\begin{picture}"
2086 "\\begin{quotation}"
2087 "\\begin{quote}"
2088 "\\begin{tabbing}"
2089 "\\begin{table}"
2090 "\\begin{tabular}"
2091 "\\begin{thebibliography}"
2092 "\\begin{theorem}"
2093 "\\begin{titlepage}"
2094 "\\begin{verbatim}"
2095 "\\begin{verse}"
2096 "\\bf"
2097 "\\bf"
2098 "\\bibitem"
2099 "\\bigskip"
2100 "\\cdots"
2101 "\\centering"
2102 "\\circle"
2103 "\\cite"
2104 "\\cleardoublepage"
2105 "\\clearpage"
2106 "\\cline"
2107 "\\closing"
2108 "\\dashbox"
2109 "\\date"
2110 "\\ddots"
2111 "\\dotfill"
2112 "\\em"
2113 "\\fbox"
2114 "\\flushbottom"
2115 "\\fnsymbol"
2116 "\\footnote"
2117 "\\footnotemark"
2118 "\\footnotesize"
2119 "\\footnotetext"
2120 "\\frac"
2121 "\\frame"
2122 "\\framebox"
2123 "\\hfill"
2124 "\\hline"
2125 "\\hrulespace"
2126 "\\hspace"
2127 "\\huge"
2128 "\\hyphenation"
2129 "\\include"
2130 "\\includeonly"
2131 "\\indent"
2132 "\\input"
2133 "\\it"
2134 "\\kill"
2135 "\\label"
2136 "\\large"
2137 "\\ldots"
2138 "\\line"
2139 "\\linebreak"
2140 "\\linethickness"
2141 "\\listoffigures"
2142 "\\listoftables"
2143 "\\location"
2144 "\\makebox"
2145 "\\maketitle"
2146 "\\mark"
2147 "\\mbox"
2148 "\\medskip"
2149 "\\multicolumn"
2150 "\\multiput"
2151 ("\\nbsp" "~")
2152 "\\newcommand"
2153 "\\newcounter"
2154 "\\newenvironment"
2155 "\\newfont"
2156 "\\newlength"
2157 "\\newline"
2158 "\\newpage"
2159 "\\newsavebox"
2160 "\\newtheorem"
2161 "\\nocite"
2162 "\\nofiles"
2163 "\\noindent"
2164 "\\nolinebreak"
2165 "\\nopagebreak"
2166 "\\normalsize"
2167 "\\onecolumn"
2168 "\\opening"
2169 "\\oval"
2170 "\\overbrace"
2171 "\\overline"
2172 "\\pagebreak"
2173 "\\pagenumbering"
2174 "\\pageref"
2175 "\\pagestyle"
2176 "\\par"
2177 "\\parbox"
2178 "\\put"
2179 "\\raggedbottom"
2180 "\\raggedleft"
2181 "\\raggedright"
2182 "\\raisebox"
2183 "\\ref"
2184 "\\rm"
2185 "\\roman"
2186 "\\rule"
2187 "\\savebox"
2188 "\\sc"
2189 "\\scriptsize"
2190 "\\setcounter"
2191 "\\setlength"
2192 "\\settowidth"
2193 "\\sf"
2194 "\\shortstack"
2195 "\\signature"
2196 "\\sl"
2197 "\\small"
2198 "\\smallskip"
2199 "\\sqrt"
2200 "\\tableofcontents"
2201 "\\telephone"
2202 "\\thanks"
2203 "\\thispagestyle"
2204 "\\tiny"
2205 "\\title"
2206 "\\tt"
2207 "\\twocolumn"
2208 "\\typein"
2209 "\\typeout"
2210 "\\underbrace"
2211 "\\underline"
2212 "\\usebox"
2213 "\\usecounter"
2214 "\\value"
2215 "\\vdots"
2216 "\\vector"
2217 "\\verb"
2218 "\\vfill"
2219 "\\vline"
2220 "\\vspace")
2221 "A list of LaTeX commands to be protected when performing conversion.")
2223 (defvar org-latex-entities-exceptions nil)
2225 (defconst org-latex-entities-regexp
2226 (let (names rest)
2227 (dolist (x org-latex-entities)
2228 (when (consp x)
2229 (add-to-list 'org-latex-entities-exceptions x)
2230 (setq x (car x)))
2231 (if (string-match "[a-zA-Z]$" x)
2232 (push x names)
2233 (push x rest)))
2234 (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
2235 "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
2237 (provide 'org-export-latex)
2238 (provide 'org-latex)
2240 ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
2242 ;;; org-latex.el ends here