org-export: Fix error when exporting a buffer with a code block
[org-mode.git] / EXPERIMENTAL / org-e-latex.el
blob88849a61a4f55f91d737097cc8ffcc1bfb9e1a57
1 ;;; org-e-latex.el --- LaTeX Back-End For Org Export Engine
3 ;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;; This library implements a LaTeX back-end for Org generic exporter.
25 ;; To test it, run
27 ;; M-: (org-export-to-buffer 'e-latex "*Test e-LaTeX*") RET
29 ;; in an org-mode buffer then switch to the buffer to see the LaTeX
30 ;; export. See contrib/lisp/org-export.el for more details on how
31 ;; this exporter works.
33 ;; It introduces three new buffer keywords: "LATEX_CLASS",
34 ;; "LATEX_CLASS_OPTIONS" and "LATEX_HEADER".
36 ;;; Code:
38 (eval-when-compile (require 'cl))
40 (defvar org-export-latex-default-packages-alist)
41 (defvar org-export-latex-packages-alist)
43 (declare-function org-element-get-property "org-element" (property element))
44 (declare-function org-element-normalize-string "org-element" (s))
45 (declare-function org-element-parse-secondary-string
46 "org-element" (string restriction &optional buffer))
47 (defvar org-element-string-restrictions)
49 (declare-function org-export-clean-table "org-export" (table specialp))
50 (declare-function org-export-data "org-export" (data backend info))
51 (declare-function org-export-directory "org-export" (type plist))
52 (declare-function org-export-expand-macro "org-export" (macro info))
53 (declare-function org-export-first-sibling-p "org-export" (headline info))
54 (declare-function org-export-footnote-first-reference-p "org-export" (footnote-reference info))
55 (declare-function org-export-get-coderef-format "org-export" (path desc))
56 (declare-function org-export-get-footnote-definition "org-export" (footnote-reference info))
57 (declare-function org-export-get-footnote-number "org-export" (footnote info))
58 (declare-function org-export-get-previous-element "org-export" (blob info))
59 (declare-function org-export-get-relative-level "org-export" (headline info))
60 (declare-function org-export-handle-code
61 "org-export" (element info &optional num-fmt ref-fmt delayed))
62 (declare-function org-export-included-file "org-export" (keyword backend info))
63 (declare-function org-export-inline-image-p "org-export" (link &optional extensions))
64 (declare-function org-export-last-sibling-p "org-export" (headline info))
65 (declare-function org-export-low-level-p "org-export" (headline info))
66 (declare-function org-export-output-file-name
67 "org-export" (extension &optional subtreep pub-dir))
68 (declare-function org-export-resolve-coderef "org-export" (ref info))
69 (declare-function org-export-resolve-fuzzy-link "org-export" (link info))
70 (declare-function org-export-secondary-string "org-export" (secondary backend info))
71 (declare-function org-export-solidify-link-text "org-export" (s))
72 (declare-function org-export-table-format-info "org-export" (table))
73 (declare-function
74 org-export-to-buffer "org-export"
75 (backend buffer &optional subtreep visible-only body-only ext-plist))
76 (declare-function
77 org-export-to-file "org-export"
78 (backend file &optional subtreep visible-only body-only ext-plist))
82 ;;; Internal Variables
84 (defconst org-e-latex-option-alist
85 '((:date "DATE" nil org-e-latex-date-format t)
86 (:latex-class "LATEX_CLASS" nil org-e-latex-default-class t)
87 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
88 (:latex-header-extra "LATEX_HEADER" nil nil newline))
89 "Alist between LaTeX export properties and ways to set them.
90 See `org-export-option-alist' for more information on the
91 structure of the value.")
95 ;;; User Configurable Variables
97 (defgroup org-export-e-latex nil
98 "Options for exporting Org mode files to LaTeX."
99 :tag "Org Export LaTeX"
100 :group 'org-export)
103 ;;;; Preamble
105 (defcustom org-e-latex-default-class "article"
106 "The default LaTeX class."
107 :group 'org-export-e-latex
108 :type '(string :tag "LaTeX class"))
110 (defcustom org-e-latex-classes
111 '(("article"
112 "\\documentclass[11pt]{article}"
113 ("\\section{%s}" . "\\section*{%s}")
114 ("\\subsection{%s}" . "\\subsection*{%s}")
115 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
116 ("\\paragraph{%s}" . "\\paragraph*{%s}")
117 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
118 ("report"
119 "\\documentclass[11pt]{report}"
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 ("\\part{%s}" . "\\part*{%s}")
128 ("\\chapter{%s}" . "\\chapter*{%s}")
129 ("\\section{%s}" . "\\section*{%s}")
130 ("\\subsection{%s}" . "\\subsection*{%s}")
131 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
132 "Alist of LaTeX classes and associated header and structure.
133 If #+LaTeX_CLASS is set in the buffer, use its value and the
134 associated information. Here is the structure of each cell:
136 \(class-name
137 header-string
138 \(numbered-section . unnumbered-section\)
139 ...\)
141 The header string
142 -----------------
144 The HEADER-STRING is the header that will be inserted into the LaTeX file.
145 It should contain the \\documentclass macro, and anything else that is needed
146 for this setup. To this header, the following commands will be added:
148 - Calls to \\usepackage for all packages mentioned in the variables
149 `org-e-latex-default-packages-alist' and
150 `org-e-latex-packages-alist'. Thus, your header definitions should
151 avoid to also request these packages.
153 - Lines specified via \"#+LaTeX_HEADER:\"
155 If you need more control about the sequence in which the header is built
156 up, or if you want to exclude one of these building blocks for a particular
157 class, you can use the following macro-like placeholders.
159 [DEFAULT-PACKAGES] \\usepackage statements for default packages
160 [NO-DEFAULT-PACKAGES] do not include any of the default packages
161 [PACKAGES] \\usepackage statements for packages
162 [NO-PACKAGES] do not include the packages
163 [EXTRA] the stuff from #+LaTeX_HEADER
164 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
165 [BEAMER-HEADER-EXTRA] the beamer extra headers
167 So a header like
169 \\documentclass{article}
170 [NO-DEFAULT-PACKAGES]
171 [EXTRA]
172 \\providecommand{\\alert}[1]{\\textbf{#1}}
173 [PACKAGES]
175 will omit the default packages, and will include the #+LaTeX_HEADER lines,
176 then have a call to \\providecommand, and then place \\usepackage commands
177 based on the content of `org-e-latex-packages-alist'.
179 If your header or `org-e-latex-default-packages-alist' inserts
180 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be replaced with
181 a coding system derived from `buffer-file-coding-system'. See also the
182 variable `org-e-latex-inputenc-alist' for a way to influence this
183 mechanism.
185 The sectioning structure
186 ------------------------
188 The sectioning structure of the class is given by the elements following
189 the header string. For each sectioning level, a number of strings is
190 specified. A %s formatter is mandatory in each section string and will
191 be replaced by the title of the section.
193 Instead of a cons cell \(numbered . unnumbered\), you can also
194 provide a list of 2 or 4 elements,
196 \(numbered-open numbered-close\)
200 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
202 providing opening and closing strings for a LaTeX environment that should
203 represent the document section. The opening clause should have a %s
204 to represent the section title.
206 Instead of a list of sectioning commands, you can also specify a
207 function name. That function will be called with two parameters,
208 the (reduced) level of the headline, and a predicate non-nil when
209 the headline should be numbered. It must return a format string in
210 which the section title will be added."
211 :group 'org-export-e-latex
212 :type '(repeat
213 (list (string :tag "LaTeX class")
214 (string :tag "LaTeX header")
215 (repeat :tag "Levels" :inline t
216 (choice
217 (cons :tag "Heading"
218 (string :tag " numbered")
219 (string :tag "unnumbered"))
220 (list :tag "Environment"
221 (string :tag "Opening (numbered)")
222 (string :tag "Closing (numbered)")
223 (string :tag "Opening (unnumbered)")
224 (string :tag "Closing (unnumbered)"))
225 (function :tag "Hook computing sectioning"))))))
227 (defcustom org-e-latex-inputenc-alist nil
228 "Alist of inputenc coding system names, and what should really be used.
229 For example, adding an entry
231 (\"utf8\" . \"utf8x\")
233 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
234 are written as utf8 files."
235 :group 'org-export-e-latex
236 :type '(repeat
237 (cons
238 (string :tag "Derived from buffer")
239 (string :tag "Use this instead"))))
241 (defcustom org-e-latex-date-format
242 "\\today"
243 "Format string for \\date{...}."
244 :group 'org-export-e-latex
245 :type 'boolean)
247 (defcustom org-e-latex-title-command "\\maketitle"
248 "The command used to insert the title just after \\begin{document}.
249 If this string contains the formatting specification \"%s\" then
250 it will be used as a formatting string, passing the title as an
251 argument."
252 :group 'org-export-e-latex
253 :type 'string)
256 ;;;; Headline
258 (defcustom org-e-latex-format-headline-function nil
259 "Function to format headline text.
261 This function will be called with 5 arguments:
262 TODO the todo keyword \(string or nil\).
263 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
264 PRIORITY the priority of the headline \(integer or nil\)
265 TEXT the main headline text \(string\).
266 TAGS the tags string, separated with colons \(string or nil\).
268 The function result will be used in the section format string.
270 As an example, one could set the variable to the following, in
271 order to reproduce the default set-up:
273 \(defun org-e-latex-format-headline \(todo todo-type priority text tags\)
274 \"Default format function for an headline.\"
275 \(concat \(when todo
276 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
277 \(when priority
278 \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
279 text
280 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
281 :group 'org-export-e-latex
282 :type 'function)
285 ;;;; Emphasis
287 (defcustom org-e-latex-emphasis-alist
288 '(("*" . "\\textbf{%s}")
289 ("/" . "\\emph{%s}")
290 ("_" . "\\underline{%s}")
291 ("+" . "\\st{%s}")
292 ("=" . protectedtexttt)
293 ("~" . verb))
294 "Alist of LaTeX expressions to convert emphasis fontifiers.
296 The key is the character used as a marker for fontification. The
297 value is a formatting string to wrap fontified text with.
299 Value can also be set to the following symbols: `verb' and
300 `protectedtexttt'. For the former, Org will use \"\\verb\" to
301 create a format string and select a delimiter character that
302 isn't in the string. For the latter, Org will use \"\\texttt\"
303 to typeset and try to protect special characters."
304 :group 'org-export-e-latex
305 :type 'alist)
308 ;;;; Footnotes
310 (defcustom org-e-latex-footnote-separator "\\textsuperscript{,}\\,"
311 "Text used to separate footnotes."
312 :group 'org-export-e-latex
313 :type 'string)
316 ;;;; Time-stamps
318 (defcustom org-e-latex-active-timestamp-format "\\textit{%s}"
319 "A printf format string to be applied to active time-stamps."
320 :group 'org-export-e-latex
321 :type 'string)
323 (defcustom org-e-latex-inactive-timestamp-format "\\textit{%s}"
324 "A printf format string to be applied to inactive time-stamps."
325 :group 'org-export-e-latex
326 :type 'string)
328 (defcustom org-e-latex-diary-timestamp-format "\\textit{%s}"
329 "A printf format string to be applied to diary time-stamps."
330 :group 'org-export-e-latex
331 :type 'string)
334 ;;;; Links
336 (defcustom org-e-latex-image-default-option "width=.9\\linewidth"
337 "Default option for images."
338 :group 'org-export-e-latex
339 :type 'string)
341 (defcustom org-e-latex-default-figure-position "htb"
342 "Default position for latex figures."
343 :group 'org-export-e-latex
344 :type 'string)
346 (defcustom org-e-latex-inline-image-extensions
347 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
348 "Extensions of image files that can be inlined into LaTeX.
350 Note that the image extension *actually* allowed depend on the
351 way the LaTeX file is processed. When used with pdflatex, pdf,
352 jpg and png images are OK. When processing through dvi to
353 Postscript, only ps and eps are allowed. The default we use here
354 encompasses both."
355 :group 'org-export-e-latex
356 :type '(repeat (string :tag "Extension")))
359 ;;;; Tables
361 (defcustom org-e-latex-default-table-environment "tabular"
362 "Default environment used to build tables."
363 :group 'org-export-e-latex
364 :type 'string)
366 (defcustom org-e-latex-tables-centered t
367 "When non-nil, tables are exported in a center environment."
368 :group 'org-export-e-latex
369 :type 'boolean)
371 (defcustom org-e-latex-tables-verbatim nil
372 "When non-nil, tables are exported verbatim."
373 :group 'org-export-e-latex
374 :type 'boolean)
376 (defcustom org-e-latex-table-caption-above t
377 "When non-nil, place caption string at the beginning of the table.
378 Otherwise, place it near the end."
379 :group 'org-export-e-latex
380 :type 'boolean)
383 ;;;; Drawers
385 (defcustom org-e-latex-format-drawer-function nil
386 "Function called to format a drawer in LaTeX code.
388 The function must accept two parameters:
389 NAME the drawer name, like \"LOGBOOK\"
390 CONTENTS the contents of the drawer.
392 The function should return the string to be exported.
394 For example, the variable could be set to the following function
395 in order to mimic default behaviour:
397 \(defun org-e-latex-format-drawer-default \(name contents\)
398 \"Format a drawer element for LaTeX export.\"
399 contents\)"
400 :group 'org-export-e-latex
401 :type 'function)
404 ;;;; Inlinetasks
406 (defcustom org-e-latex-format-inlinetask-function nil
407 "Function called to format an inlinetask in LaTeX code.
409 The function must accept six parameters:
410 TODO the todo keyword, as a string
411 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
412 PRIORITY the inlinetask priority, as a string
413 NAME the inlinetask name, as a string.
414 TAGS the inlinetask tags, as a string.
415 CONTENTS the contents of the inlinetask, as a string.
417 The function should return the string to be exported.
419 For example, the variable could be set to the following function
420 in order to mimic default behaviour:
422 \(defun org-e-latex-format-inlinetask \(todo type priority name tags contents\)
423 \"Format an inline task element for LaTeX export.\"
424 \(let \(\(full-title
425 \(concat
426 \(when todo
427 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
428 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
429 title
430 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
431 \(format \(concat \"\\\\begin{center}\\n\"
432 \"\\\\fbox{\\n\"
433 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
434 \"%s\\n\\n\"
435 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
436 \"%s\"
437 \"\\\\end{minipage}}\"
438 \"\\\\end{center}\"\)
439 full-title contents\)\)"
440 :group 'org-export-e-latex
441 :type 'function)
444 ;; Src blocks
446 (defcustom org-e-latex-listings nil
447 "Non-nil means export source code using the listings package.
448 This package will fontify source code, possibly even with color.
449 If you want to use this, you also need to make LaTeX use the
450 listings package, and if you want to have color, the color
451 package. Just add these to `org-e-latex-packages-alist',
452 for example using customize, or with something like
454 (require 'org-e-latex)
455 (add-to-list 'org-e-latex-packages-alist '(\"\" \"listings\"))
456 (add-to-list 'org-e-latex-packages-alist '(\"\" \"color\"))
458 Alternatively,
460 (setq org-e-latex-listings 'minted)
462 causes source code to be exported using the minted package as
463 opposed to listings. If you want to use minted, you need to add
464 the minted package to `org-e-latex-packages-alist', for
465 example using customize, or with
467 (require 'org-e-latex)
468 (add-to-list 'org-e-latex-packages-alist '(\"\" \"minted\"))
470 In addition, it is necessary to install
471 pygments (http://pygments.org), and to configure the variable
472 `org-e-latex-to-pdf-process' so that the -shell-escape option is
473 passed to pdflatex."
474 :group 'org-export-e-latex
475 :type '(choice
476 (const :tag "Use listings" t)
477 (const :tag "Use minted" 'minted)
478 (const :tag "Export verbatim" nil)))
480 (defcustom org-e-latex-listings-langs
481 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
482 (c "C") (cc "C++")
483 (fortran "fortran")
484 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
485 (html "HTML") (xml "XML")
486 (tex "TeX") (latex "TeX")
487 (shell-script "bash")
488 (gnuplot "Gnuplot")
489 (ocaml "Caml") (caml "Caml")
490 (sql "SQL") (sqlite "sql"))
491 "Alist mapping languages to their listing language counterpart.
492 The key is a symbol, the major mode symbol without the \"-mode\".
493 The value is the string that should be inserted as the language parameter
494 for the listings package. If the mode name and the listings name are
495 the same, the language does not need an entry in this list - but it does not
496 hurt if it is present."
497 :group 'org-export-e-latex
498 :type '(repeat
499 (list
500 (symbol :tag "Major mode ")
501 (string :tag "Listings language"))))
503 (defcustom org-e-latex-listings-options nil
504 "Association list of options for the latex listings package.
506 These options are supplied as a comma-separated list to the
507 \\lstset command. Each element of the association list should be
508 a list containing two strings: the name of the option, and the
509 value. For example,
511 (setq org-e-latex-listings-options
512 '((\"basicstyle\" \"\\small\")
513 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
515 will typeset the code in a small size font with underlined, bold
516 black keywords.
518 Note that the same options will be applied to blocks of all
519 languages."
520 :group 'org-export-e-latex
521 :type '(repeat
522 (list
523 (string :tag "Listings option name ")
524 (string :tag "Listings option value"))))
526 (defcustom org-e-latex-minted-langs
527 '((emacs-lisp "common-lisp")
528 (cc "c++")
529 (cperl "perl")
530 (shell-script "bash")
531 (caml "ocaml"))
532 "Alist mapping languages to their minted language counterpart.
533 The key is a symbol, the major mode symbol without the \"-mode\".
534 The value is the string that should be inserted as the language parameter
535 for the minted package. If the mode name and the listings name are
536 the same, the language does not need an entry in this list - but it does not
537 hurt if it is present.
539 Note that minted uses all lower case for language identifiers,
540 and that the full list of language identifiers can be obtained
541 with:
542 pygmentize -L lexers"
543 :group 'org-export-e-latex
544 :type '(repeat
545 (list
546 (symbol :tag "Major mode ")
547 (string :tag "Minted language"))))
549 (defcustom org-e-latex-minted-options nil
550 "Association list of options for the latex minted package.
552 These options are supplied within square brackets in
553 \\begin{minted} environments. Each element of the alist should
554 be a list containing two strings: the name of the option, and the
555 value. For example,
557 (setq org-e-latex-minted-options
558 '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
560 will result in src blocks being exported with
562 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
564 as the start of the minted environment. Note that the same
565 options will be applied to blocks of all languages."
566 :group 'org-export-e-latex
567 :type '(repeat
568 (list
569 (string :tag "Minted option name ")
570 (string :tag "Minted option value"))))
572 (defvar org-e-latex-custom-lang-environments nil
573 "Alist mapping languages to language-specific LaTeX environments.
575 It is used during export of src blocks by the listings and minted
576 latex packages. For example,
578 \(setq org-e-latex-custom-lang-environments
579 '\(\(python \"pythoncode\"\)\)\)
581 would have the effect that if org encounters begin_src python
582 during latex export it will output
584 \\begin{pythoncode}
585 <src block body>
586 \\end{pythoncode}")
589 ;;;; Plain text
591 (defcustom org-e-latex-quotes
592 '(("fr" ("\\(\\s-\\|[[(]\\)\"" . "«~") ("\\(\\S-\\)\"" . "~»") ("\\(\\s-\\|(\\)'" . "'"))
593 ("en" ("\\(\\s-\\|[[(]\\)\"" . "``") ("\\(\\S-\\)\"" . "''") ("\\(\\s-\\|(\\)'" . "`")))
594 "Alist for quotes to use when converting english double-quotes.
596 The CAR of each item in this alist is the language code.
597 The CDR of each item in this alist is a list of three CONS:
598 - the first CONS defines the opening quote;
599 - the second CONS defines the closing quote;
600 - the last CONS defines single quotes.
602 For each item in a CONS, the first string is a regexp
603 for allowed characters before/after the quote, the second
604 string defines the replacement string for this quote."
605 :group 'org-export-e-latex
606 :type '(list
607 (cons :tag "Opening quote"
608 (string :tag "Regexp for char before")
609 (string :tag "Replacement quote "))
610 (cons :tag "Closing quote"
611 (string :tag "Regexp for char after ")
612 (string :tag "Replacement quote "))
613 (cons :tag "Single quote"
614 (string :tag "Regexp for char before")
615 (string :tag "Replacement quote "))))
618 ;;;; Compilation
620 (defcustom org-e-latex-pdf-process
621 '("pdflatex -interaction nonstopmode -output-directory %o %f"
622 "pdflatex -interaction nonstopmode -output-directory %o %f"
623 "pdflatex -interaction nonstopmode -output-directory %o %f")
624 "Commands to process a LaTeX file to a PDF file.
625 This is a list of strings, each of them will be given to the shell
626 as a command. %f in the command will be replaced by the full file name, %b
627 by the file base name (i.e. without extension) and %o by the base directory
628 of the file.
630 The reason why this is a list is that it usually takes several runs of
631 `pdflatex', maybe mixed with a call to `bibtex'. Org does not have a clever
632 mechanism to detect which of these commands have to be run to get to a stable
633 result, and it also does not do any error checking.
635 By default, Org uses 3 runs of `pdflatex' to do the processing. If you
636 have texi2dvi on your system and if that does not cause the infamous
637 egrep/locale bug:
639 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
641 then `texi2dvi' is the superior choice. Org does offer it as one
642 of the customize options.
644 Alternatively, this may be a Lisp function that does the processing, so you
645 could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
646 This function should accept the file name as its single argument."
647 :group 'org-export-pdf
648 :type '(choice
649 (repeat :tag "Shell command sequence"
650 (string :tag "Shell command"))
651 (const :tag "2 runs of pdflatex"
652 ("pdflatex -interaction nonstopmode -output-directory %o %f"
653 "pdflatex -interaction nonstopmode -output-directory %o %f"))
654 (const :tag "3 runs of pdflatex"
655 ("pdflatex -interaction nonstopmode -output-directory %o %f"
656 "pdflatex -interaction nonstopmode -output-directory %o %f"
657 "pdflatex -interaction nonstopmode -output-directory %o %f"))
658 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
659 ("pdflatex -interaction nonstopmode -output-directory %o %f"
660 "bibtex %b"
661 "pdflatex -interaction nonstopmode -output-directory %o %f"
662 "pdflatex -interaction nonstopmode -output-directory %o %f"))
663 (const :tag "texi2dvi"
664 ("texi2dvi -p -b -c -V %f"))
665 (const :tag "rubber"
666 ("rubber -d --into %o %f"))
667 (function)))
669 (defcustom org-e-latex-logfiles-extensions
670 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
671 "The list of file extensions to consider as LaTeX logfiles."
672 :group 'org-export-e-latex
673 :type '(repeat (string :tag "Extension")))
675 (defcustom org-e-latex-remove-logfiles t
676 "Non-nil means remove the logfiles produced by PDF production.
677 These are the .aux, .log, .out, and .toc files."
678 :group 'org-export-e-latex
679 :type 'boolean)
683 ;;; Internal Functions
685 (defun org-e-latex--caption/label-string (caption label info)
686 "Return caption and label LaTeX string for floats.
688 CAPTION is a cons cell of secondary strings, the car being the
689 standard caption and the cdr its short form. LABEL is a string
690 representing the label. INFO is a plist holding contextual
691 information.
693 If there's no caption nor label, return the empty string.
695 For non-floats, see `org-e-latex--wrap-label'."
696 (let ((label-str (if label (format "\\label{%s}" label) "")))
697 (cond
698 ((and (not caption) (not label)) "")
699 ((not caption) (format "\\label{%s}\n" label))
700 ;; Option caption format with short name.
701 ((cdr caption)
702 (format "\\caption[%s]{%s%s}\n"
703 (org-export-secondary-string (cdr caption) 'e-latex info)
704 label-str
705 (org-export-secondary-string (car caption) 'e-latex info)))
706 ;; Standard caption format.
707 (t (format "\\caption{%s%s}\n"
708 label-str
709 (org-export-secondary-string (car caption) 'e-latex info))))))
711 (defun org-e-latex--guess-inputenc (header)
712 "Set the coding system in inputenc to what the buffer is.
714 HEADER is the LaTeX header string.
716 Return the new header."
717 (let* ((cs (or (ignore-errors
718 (latexenc-coding-system-to-inputenc
719 buffer-file-coding-system))
720 "utf8")))
721 (if (not cs)
722 header
723 ;; First translate if that is requested.
724 (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
725 ;; Then find the \usepackage statement and replace the option.
726 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
727 cs header t nil 1))))
729 (defun org-e-latex--find-verb-separator (s)
730 "Return a character not used in string S.
731 This is used to choose a separator for constructs like \\verb."
732 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
733 (loop for c across ll
734 when (not (string-match (regexp-quote (char-to-string c)) s))
735 return (char-to-string c))))
737 (defun org-e-latex--make-option-string (options)
738 "Return a comma separated string of keywords and values.
739 OPTIONS is an alist where the key is the options keyword as
740 a string, and the value a list containing the keyword value, or
741 nil."
742 (mapconcat (lambda (pair)
743 (concat (first pair)
744 (when (> (length (second pair)) 0)
745 (concat "=" (second pair)))))
746 options
747 ","))
749 (defun org-e-latex--quotation-marks (text info)
750 "Export quotation marks depending on language conventions.
751 TEXT is a string containing quotation marks to be replaced. INFO
752 is a plist used as a communication channel."
753 (mapc (lambda(l)
754 (let ((start 0))
755 (while (setq start (string-match (car l) text start))
756 (let ((new-quote (concat (match-string 1 text) (cdr l))))
757 (setq text (replace-match new-quote t t text))))))
758 (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
759 ;; Falls back on English.
760 (assoc "en" org-e-latex-quotes))))
761 text)
763 (defun org-e-latex--wrap-label (element output)
764 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
765 This function shouldn't be used for floats. See
766 `org-e-latex--caption/label-string'."
767 (let ((label (org-element-get-property :name element)))
768 (if (or (not output) (not label) (string= output "") (string= label ""))
769 output
770 (concat (format "\\label{%s}\n" label) output))))
774 ;;; Template
776 (defun org-e-latex-template (contents info)
777 "Return complete document string after LaTeX conversion.
778 CONTENTS is the transcoded contents string. INFO is a plist
779 holding export options."
780 (let ((title (org-export-secondary-string
781 (plist-get info :title) 'e-latex info)))
782 (concat
783 ;; 1. Time-stamp.
784 (and (plist-get info :time-stamp-file)
785 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
786 ;; 2. Document class and packages.
787 (let ((class (plist-get info :latex-class))
788 (class-options (plist-get info :latex-class-options)))
789 (org-element-normalize-string
790 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
791 (document-class-string
792 (and (stringp header)
793 (if class-options
794 (replace-regexp-in-string
795 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
796 class-options header t nil 1)
797 header))))
798 (org-e-latex--guess-inputenc
799 (org-splice-latex-header
800 document-class-string
801 org-export-latex-default-packages-alist ; defined in org.el
802 org-export-latex-packages-alist nil ; defined in org.el
803 (plist-get info :latex-header-extra))))))
804 ;; 3. Define alert if not yet defined.
805 "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
806 ;; 4. Possibly limit depth for headline numbering.
807 (let ((sec-num (plist-get info :section-numbers)))
808 (when (integerp sec-num)
809 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
810 ;; 5. Author.
811 (let ((author (and (plist-get info :with-author)
812 (let ((auth (plist-get info :author)))
813 (and auth (org-export-secondary-string
814 auth 'e-latex info)))))
815 (email (and (plist-get info :with-email)
816 (org-export-secondary-string
817 (plist-get info :email) 'e-latex info))))
818 (cond ((and author email (not (string= "" email)))
819 (format "\\author{%s\\thanks{%s}}\n" author email))
820 (author (format "\\author{%s}\n" author))
821 (t "\\author{}\n")))
822 ;; 6. Date.
823 (let ((date (plist-get info :date)))
824 (and date (format "\\date{%s}\n" date)))
825 ;; 7. Title
826 (format "\\title{%s}\n" title)
827 ;; 8. Hyperref options.
828 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
829 (or (plist-get info :keywords) "")
830 (or (plist-get info :description) "")
831 (let ((creator-info (plist-get info :with-creator)))
832 (cond
833 ((not creator-info) "")
834 ((eq creator-info 'comment) "")
835 (t (plist-get info :creator)))))
836 ;; 9. Document start.
837 "\\begin{document}\n\n"
838 ;; 10. Title command.
839 (org-element-normalize-string
840 (cond ((string= "" title) nil)
841 ((not (stringp org-e-latex-title-command)) nil)
842 ((string-match "\\(?:[^%]\\|^\\)%s"
843 org-e-latex-title-command)
844 (format org-e-latex-title-command title))
845 (t org-e-latex-title-command)))
846 ;; 11. Table of contents.
847 (let ((depth (plist-get info :with-toc)))
848 (when depth
849 (concat (when (wholenump depth)
850 (format "\\setcounter{tocdepth}{%d}\n" depth))
851 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
852 ;; 12. Document's body.
853 contents
854 ;; 13. Creator.
855 (let ((creator-info (plist-get info :with-creator)))
856 (cond
857 ((not creator-info))
858 ((eq creator-info 'comment)
859 (format "%% %s\n" (plist-get info :creator)))
860 (t (concat (plist-get info :creator) "\n"))))
861 ;; 14. Document end.
862 "\\end{document}")))
866 ;;; Transcode Functions
868 ;;;; Block
870 (defun org-e-latex-center-block (center-block contents info)
871 "Transcode a CENTER-BLOCK element from Org to LaTeX.
872 CONTENTS holds the contents of the block. INFO is a plist
873 holding contextual information."
874 (org-e-latex--wrap-label
875 center-block
876 (format "\\begin{center}\n%s\\end{center}" contents)))
879 ;;;; Comment
881 ;; Comments are ignored.
884 ;;;; Comment Block
886 ;; Comment Blocks are ignored.
889 ;;;; Drawer
891 (defun org-e-latex-drawer (drawer contents info)
892 "Transcode a DRAWER element from Org to LaTeX.
893 CONTENTS holds the contents of the block. INFO is a plist
894 holding contextual information."
895 (let* ((name (org-element-get-property :drawer-name drawer))
896 (output (if (functionp org-e-latex-format-drawer-function)
897 (funcall org-e-latex-format-drawer-function
898 name contents)
899 ;; If there's no user defined function: simply
900 ;; display contents of the drawer.
901 contents)))
902 (org-e-latex--wrap-label drawer output)))
905 ;;;; Dynamic Block
907 (defun org-e-latex-dynamic-block (dynamic-block contents info)
908 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
909 CONTENTS holds the contents of the block. INFO is a plist
910 holding contextual information. See
911 `org-export-data'."
912 (org-e-latex--wrap-label dynamic-block contents))
915 ;;;; Emphasis
917 (defun org-e-latex-emphasis (emphasis contents info)
918 "Transcode EMPHASIS from Org to LaTeX.
919 CONTENTS is the contents of the emphasized text. INFO is a plist
920 holding contextual information.."
921 (format (cdr (assoc (org-element-get-property :marker emphasis)
922 org-e-latex-emphasis-alist))
923 contents))
926 ;;;; Entity
928 (defun org-e-latex-entity (entity contents info)
929 "Transcode an ENTITY object from Org to LaTeX.
930 CONTENTS are the definition itself. INFO is a plist holding
931 contextual information."
932 (let ((ent (org-element-get-property :latex entity)))
933 (if (org-element-get-property :latex-math-p entity)
934 (format "$%s$" ent)
935 ent)))
938 ;;;; Example Block
940 (defun org-e-latex-example-block (example-block contents info)
941 "Transcode a EXAMPLE-BLOCK element from Org to LaTeX.
942 CONTENTS is nil. INFO is a plist holding contextual information."
943 (let* ((options (or (org-element-get-property :options example-block) ""))
944 (value (org-export-handle-code example-block info)))
945 (org-e-latex--wrap-label
946 example-block (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
949 ;;;; Export Snippet
951 (defun org-e-latex-export-snippet (export-snippet contents info)
952 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
953 CONTENTS is nil. INFO is a plist holding contextual information."
954 (org-element-get-property :value export-snippet))
957 ;;;; Export Block
959 (defun org-e-latex-export-block (export-block contents info)
960 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
961 CONTENTS is nil. INFO is a plist holding contextual information."
962 (when (string= (org-element-get-property :type export-block) "latex")
963 (org-remove-indentation (org-element-get-property :value export-block))))
966 ;;;; Fixed Width
968 (defun org-e-latex-fixed-width (fixed-width contents info)
969 "Transcode a FIXED-WIDTH element from Org to LaTeX.
970 CONTENTS is nil. INFO is a plist holding contextual information."
971 (let* ((value (org-element-normalize-string
972 (replace-regexp-in-string
973 "^[ \t]*: ?" ""
974 (org-element-get-property :value fixed-width)))))
975 (org-e-latex--wrap-label
976 fixed-width (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
979 ;;;; Footnote Definition
981 ;; Footnote Definitions are ignored.
984 ;;;; Footnote Reference
986 (defun org-e-latex-footnote-reference (footnote-reference contents info)
987 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
988 CONTENTS is nil. INFO is a plist holding contextual information."
989 (concat
990 ;; Insert separator between two footnotes in a row.
991 (let ((prev (org-export-get-previous-element footnote-reference info)))
992 (when (and (listp prev) (eq (car prev) 'footnote-reference))
993 org-e-latex-footnote-separator))
994 ;; Use \footnotemark if the footnote has already been defined.
995 ;; Otherwise, define it with \footnote command.
996 (cond
997 ((not (org-export-footnote-first-reference-p footnote-reference info))
998 (format "\\footnotemark[%s]"
999 (org-export-get-footnote-number footnote-reference info)))
1000 ;; Inline definitions are secondary strings.
1001 ((eq (org-element-get-property :type footnote-reference) 'inline)
1002 (format "\\footnote{%s}"
1003 (org-trim
1004 (org-export-secondary-string
1005 (org-export-get-footnote-definition footnote-reference info)
1006 'e-latex info))))
1007 ;; Non-inline footnotes definitions are full Org data.
1009 (format "\\footnote{%s}"
1010 (org-trim
1011 (org-export-data
1012 (org-export-get-footnote-definition footnote-reference info)
1013 'e-latex info)))))))
1016 ;;;; Headline
1018 (defun org-e-latex-headline (headline contents info)
1019 "Transcode an HEADLINE element from Org to LaTeX.
1020 CONTENTS holds the contents of the headline. INFO is a plist
1021 holding contextual information."
1022 (let* ((class (plist-get info :latex-class))
1023 (numberedp (plist-get info :section-numbers))
1024 ;; Get level relative to current parsed data.
1025 (level (org-export-get-relative-level headline info))
1026 (class-sectionning (assoc class org-e-latex-classes))
1027 ;; Section formatting will set two placeholders: one for the
1028 ;; title and the other for the contents.
1029 (section-fmt
1030 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
1031 (fboundp (nth 2 class-sectionning)))
1032 (funcall (nth 2 class-sectionning) level numberedp)
1033 (nth (1+ level) class-sectionning))))
1034 (cond
1035 ;; No section available for that LEVEL.
1036 ((not sec) nil)
1037 ;; Section format directly returned by a function.
1038 ((stringp sec) sec)
1039 ;; (numbered-section . unnumbered-section)
1040 ((not (consp (cdr sec)))
1041 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1042 ;; (numbered-open numbered-close)
1043 ((= (length sec) 2)
1044 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1045 ;; (num-in num-out no-num-in no-num-out)
1046 ((= (length sec) 4)
1047 (if numberedp
1048 (concat (car sec) "\n%s" (nth 1 sec))
1049 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1050 (text (org-export-secondary-string
1051 (org-element-get-property :title headline) 'e-latex info))
1052 (todo (and (plist-get info :with-todo-keywords)
1053 (let ((todo (org-element-get-property
1054 :todo-keyword headline)))
1055 (and todo
1056 (org-export-secondary-string todo 'e-latex info)))))
1057 (todo-type (and todo (org-element-get-property :todo-type headline)))
1058 (tags (and (plist-get info :with-tags)
1059 (org-element-get-property :tags headline)))
1060 (priority (and (plist-get info :with-priority)
1061 (org-element-get-property :priority headline)))
1062 ;; Create the headline text.
1063 (full-text (if (functionp org-e-latex-format-headline-function)
1064 ;; User-defined formatting function.
1065 (funcall org-e-latex-format-headline-function
1066 todo todo-type priority text tags)
1067 ;; Default formatting.
1068 (concat
1069 (when todo
1070 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1071 (when priority (format "\\framebox{\\#%c} " priority))
1072 text
1073 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1074 ;; Associate some \label to the headline for internal links.
1075 (headline-label
1076 (format "\\label{sec-%s}\n"
1077 (mapconcat 'number-to-string
1078 (org-export-get-headline-number headline info)
1079 "-")))
1080 (pre-blanks (make-string
1081 (org-element-get-property :pre-blank headline) 10)))
1082 (cond
1083 ;; Case 1: This is a footnote section: ignore it.
1084 ((org-element-get-property :footnote-section-p headline) nil)
1085 ;; Case 2. This is a deep sub-tree: export it as a list item.
1086 ;; Also export as items headlines for which no section
1087 ;; format has been found.
1088 ((or (not section-fmt) (org-export-low-level-p headline info))
1089 ;; Build the real contents of the sub-tree.
1090 (let ((low-level-body
1091 (concat
1092 ;; If the headline is the first sibling, start a list.
1093 (when (org-export-first-sibling-p headline info)
1094 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1095 ;; Itemize headline
1096 "\\item " full-text "\n" headline-label pre-blanks contents)))
1097 ;; If headline in the last sibling, close the list, before any
1098 ;; blank line. Otherwise, simply return LOW-LEVEL-BODY.
1099 (if (org-export-last-sibling-p headline info)
1100 (replace-regexp-in-string
1101 "[ \t\n]*\\'"
1102 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1103 low-level-body)
1104 low-level-body)))
1105 ;; Case 3. Standard headline. Export it as a section.
1106 (t (format section-fmt full-text
1107 (concat headline-label pre-blanks contents))))))
1110 ;;;; Horizontal Rule
1112 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1113 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1114 CONTENTS is nil. INFO is a plist holding contextual information."
1115 (let ((attr (mapconcat #'identity
1116 (org-element-get-property :attr_latex horizontal-rule)
1117 " ")))
1118 (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
1121 ;;;; Inline Babel Call
1123 ;; Inline Babel Calls are ignored.
1126 ;;;; Inline Src Block
1128 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1129 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1130 CONTENTS holds the contents of the item. INFO is a plist holding
1131 contextual information."
1132 (let* ((code (org-element-get-property :value inline-src-block))
1133 (separator (org-e-latex--find-verb-separator code)))
1134 (cond
1135 ;; Do not use a special package: transcode it verbatim.
1136 ((not org-e-latex-listings)
1137 (concat "\\verb" separator code separator))
1138 ;; Use minted package.
1139 ((eq org-e-latex-listings 'minted)
1140 (let* ((org-lang (org-element-get-property :language inline-src-block))
1141 (mint-lang (or (cadr (assq (intern org-lang)
1142 org-e-latex-minted-langs))
1143 org-lang))
1144 (options (org-e-latex--make-option-string
1145 org-e-latex-minted-options)))
1146 (concat (format "\\mint%s{%s}"
1147 (if (string= options "") "" (format "[%s]" options))
1148 mint-lang)
1149 separator code separator)))
1150 ;; Use listings package.
1152 ;; Maybe translate language's name.
1153 (let* ((org-lang (org-element-get-property :language inline-src-block))
1154 (lst-lang (or (cadr (assq (intern org-lang)
1155 org-e-latex-listings-langs))
1156 org-lang))
1157 (options (org-e-latex--make-option-string
1158 (append org-e-latex-listings-options
1159 `(("language" ,lst-lang))))))
1160 (concat (format "\\lstinline[%s]" options)
1161 separator code separator))))))
1164 ;;;; Inlinetask
1166 (defun org-e-latex-inlinetask (inlinetask contents info)
1167 "Transcode an INLINETASK element from Org to LaTeX.
1168 CONTENTS holds the contents of the block. INFO is a plist
1169 holding contextual information."
1170 (let ((title (org-export-secondary-string
1171 (org-element-get-property :title inlinetask) 'e-latex info))
1172 (todo (and (plist-get info :with-todo-keywords)
1173 (let ((todo (org-element-get-property
1174 :todo-keyword inlinetask)))
1175 (and todo
1176 (org-export-secondary-string todo 'e-latex info)))))
1177 (todo-type (org-element-get-property :todo-type inlinetask))
1178 (tags (and (plist-get info :with-tags)
1179 (org-element-get-property :tags inlinetask)))
1180 (priority (and (plist-get info :with-priority)
1181 (org-element-get-property :priority inlinetask))))
1182 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1183 ;; with appropriate arguments.
1184 (if (functionp org-e-latex-format-inlinetask-function)
1185 (funcall org-e-latex-format-inlinetask-function
1186 todo todo-type priority title tags contents)
1187 ;; Otherwise, use a default template.
1188 (org-e-latex--wrap-label
1189 inlinetask
1190 (let ((full-title
1191 (concat
1192 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1193 (when priority (format "\\framebox{\\#%c} " priority))
1194 title
1195 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1196 (format (concat "\\begin{center}\n"
1197 "\\fbox{\n"
1198 "\\begin{minipage}[c]{.6\\textwidth}\n"
1199 "%s\n\n"
1200 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1201 "%s"
1202 "\\end{minipage}\n"
1203 "}\n"
1204 "\\end{center}")
1205 full-title contents))))))
1208 ;;;; Item
1210 (defun org-e-latex-item (item contents info)
1211 "Transcode an ITEM element from Org to LaTeX.
1212 CONTENTS holds the contents of the item. INFO is a plist holding
1213 contextual information."
1214 ;; Grab `:level' from plain-list properties, which is always the
1215 ;; first element above current item.
1216 (let* ((level (org-element-get-property
1217 :level (car (plist-get info :genealogy))))
1218 (counter (let ((count (org-element-get-property :counter item)))
1219 (and count
1220 (< level 4)
1221 (format "\\setcounter{enum%s}{%s}\n"
1222 (nth level '("i" "ii" "iii" "iv"))
1223 (1- count)))))
1224 (checkbox (let ((checkbox (org-element-get-property :checkbox item)))
1225 (cond ((eq checkbox 'on) "$\\boxtimes$ ")
1226 ((eq checkbox 'off) "$\\Box$ ")
1227 ((eq checkbox 'trans) "$\\boxminus$ "))))
1228 (tag (let ((tag (org-element-get-property :tag item)))
1229 (and tag
1230 (format "[%s]" (org-export-secondary-string
1231 tag 'e-latex info))))))
1232 (concat counter "\\item" tag " " checkbox contents)))
1235 ;;;; Keyword
1237 (defun org-e-latex-keyword (keyword contents info)
1238 "Transcode a KEYWORD element from Org to LaTeX.
1239 CONTENTS is nil. INFO is a plist holding contextual information."
1240 (let ((key (downcase (org-element-get-property :key keyword)))
1241 (value (org-element-get-property :value keyword)))
1242 (cond
1243 ((string= key "latex") value)
1244 ((string= key "index") (format "\\index{%s}" value))
1245 ((string= key "target")
1246 (format "\\label{%s}" (org-export-solidify-link-text value)))
1247 ((string= key "toc")
1248 (let ((value (downcase value)))
1249 (cond
1250 ((string-match "\\<headlines\\>" value)
1251 (let ((depth (or (and (string-match "[0-9]+" value)
1252 (string-to-number (match-string 0 value)))
1253 (plist-get info :with-toc))))
1254 (concat
1255 (when (wholenump depth)
1256 (format "\\setcounter{tocdepth}{%s}\n" depth))
1257 "\\tableofcontents")))
1258 ((string= "tables" value) "\\listoftables")
1259 ((string= "figures" value) "\\listoffigures")
1260 ((string= "listings" value) "\\listoflistings"))))
1261 ((string= key "include")
1262 (org-export-included-file keyword 'e-latex info)))))
1265 ;;;; Latex Environment
1267 (defun org-e-latex-latex-environment (latex-environment contents info)
1268 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1269 CONTENTS is nil. INFO is a plist holding contextual information."
1270 (org-e-latex--wrap-label
1271 latex-environment
1272 (org-remove-indentation (org-element-get-property :value latex-environment))))
1275 ;;;; Latex Fragment
1277 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1278 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1279 CONTENTS is nil. INFO is a plist holding contextual information."
1280 (org-element-get-property :value latex-fragment))
1283 ;;;; Line Break
1285 (defun org-e-latex-line-break (line-break contents info)
1286 "Transcode a LINE-BREAK object from Org to LaTeX.
1287 CONTENTS is nil. INFO is a plist holding contextual information."
1288 "\\\\")
1291 ;;;; Link
1293 (defun org-e-latex-link--inline-image (path info)
1294 "Return LaTeX code for an image at PATH.
1295 INFO is a plist containing export options."
1296 (let* ((parent-props (nth 1 (car (plist-get info :genealogy))))
1297 (caption (org-e-latex--caption/label-string
1298 (plist-get parent-props :caption)
1299 (plist-get parent-props :name)
1300 info))
1301 ;; Retrieve latex attributes from the element around.
1302 (attr (let ((raw-attr
1303 (mapconcat #'identity
1304 (plist-get parent-props :attr_latex) " ")))
1305 (unless (string= raw-attr "") raw-attr)))
1306 (disposition
1307 (cond
1308 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1309 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1310 ((or (and attr (string-match "\\<float\\>" attr))
1311 (not (string= caption "")))
1312 'float)))
1313 (placement
1314 (cond
1315 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1316 (org-match-string-no-properties 1 attr))
1317 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1318 ((eq disposition 'float)
1319 (concat "[" org-e-latex-default-figure-position "]"))
1320 (t ""))))
1321 ;; Now clear ATTR from any special keyword and set a default
1322 ;; value if nothing is left.
1323 (setq attr
1324 (if (not attr) ""
1325 (org-trim
1326 (replace-regexp-in-string
1327 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
1328 (setq attr (cond ((not (string= attr "")) attr)
1329 ((eq disposition 'float) "width=0.7\\textwidth")
1330 ((eq disposition 'wrap) "width=0.48\\textwidth")
1331 (t (or org-e-latex-image-default-option ""))))
1332 ;; Return proper string, depending on DISPOSITION.
1333 (case disposition
1334 ('wrap (format "\\begin{wrapfigure}%s
1335 \\centering
1336 \\includegraphics[%s]{%s}
1337 %s\\end{wrapfigure}" placement attr path caption))
1338 ('mulicolumn (format "\\begin{figure*}%s
1339 \\centering
1340 \\includegraphics[%s]{%s}
1341 %s\\end{figure*}" placement attr path caption))
1342 ('float (format "\\begin{figure}%s
1343 \\centering
1344 \\includegraphics[%s]{%s}
1345 %s\\end{figure}" placement attr path caption))
1346 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1348 (defun org-e-latex-link (link desc info)
1349 "Transcode a LINK object from Org to LaTeX.
1351 DESC is the description part of the link, or the empty string.
1352 INFO is a plist holding contextual information. See
1353 `org-export-data'."
1354 (let* ((type (org-element-get-property :type link))
1355 (raw-path (org-element-get-property :path link))
1356 ;; Ensure DESC really exists, or set it to nil.
1357 (desc (and (not (string= desc "")) desc))
1358 (imagep (org-export-inline-image-p
1359 link org-e-latex-inline-image-extensions))
1360 (path (cond
1361 ((member type '("http" "https" "ftp" "mailto"))
1362 (concat type ":" raw-path))
1363 (imagep (if (not (file-name-absolute-p raw-path)) raw-path
1364 (expand-file-name raw-path)))
1365 ((string= type "file")
1366 (when (string-match "\\(.+\\)::.+" raw-path)
1367 (setq raw-path (match-string 1 raw-path)))
1368 (if (file-name-absolute-p raw-path)
1369 (concat "file://" (expand-file-name raw-path))
1370 ;; TODO: Not implemented yet. Concat also:
1371 ;; (org-export-directory :LaTeX info)
1372 (concat "file://" raw-path)))
1373 (t raw-path)))
1374 protocol)
1375 (cond
1376 ;; Image file.
1377 (imagep (org-e-latex-link--inline-image path info))
1378 ;; Target or radioed target: replace link with the normalized
1379 ;; custom-id/target name.
1380 ((member type '("target" "radio"))
1381 (format "\\hyperref[%s]{%s}"
1382 (org-export-solidify-link-text path)
1383 (or desc (org-export-secondary-string path 'e-latex info))))
1384 ;; Links pointing to an headline: Find destination and build
1385 ;; appropriate referencing commanding.
1386 ((member type '("custom-id" "fuzzy" "id"))
1387 (let ((destination (if (string= type "fuzzy")
1388 (org-export-resolve-fuzzy-link link info)
1389 (org-export-resolve-id-link link info))))
1390 ;; Fuzzy link points to a target. Do as above.
1391 (case (car destination)
1392 (target
1393 (format "\\hyperref[%s]{%s}"
1394 (org-export-solidify-link-text
1395 (org-element-get-property :raw-value destination))
1396 (or desc
1397 (org-export-secondary-string
1398 (org-element-get-property :raw-link link)
1399 'e-latex info))))
1400 ;; Fuzzy link points to an headline. If headlines are
1401 ;; numbered and the link has no description, display
1402 ;; headline's number. Otherwise, display description or
1403 ;; headline's title.
1404 (headline
1405 (let ((label
1406 (format "sec-%s"
1407 (mapconcat
1408 'number-to-string
1409 (org-export-get-headline-number destination info)
1410 "-"))))
1411 (if (and (plist-get info :section-numbers) (not desc))
1412 (format "\\ref{%s}" label)
1413 (format "\\hyperref[%s]{%s}" label
1414 (or desc
1415 (org-export-secondary-string
1416 (org-element-get-property :title destination)
1417 'e-latex info))))))
1418 ;; Fuzzy link points nowhere.
1419 (otherwise
1420 (format "\\texttt{%s}"
1421 (or desc
1422 (org-export-secondary-string
1423 (org-element-get-property :raw-link link)
1424 'e-latex info)))))))
1425 ;; Coderef: replace link with the reference name or the
1426 ;; equivalent line number.
1427 ((string= type "coderef")
1428 (format (org-export-get-coderef-format path (or desc ""))
1429 (org-export-resolve-coderef path info)))
1430 ;; Link type is handled by a special function.
1431 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1432 (funcall protocol (org-link-unescape path) desc 'latex))
1433 ;; External link with a description part.
1434 ((and path desc) (format "\\href{%s}{%s}" path desc))
1435 ;; External link without a description part.
1436 (path (format "\\url{%s}" path))
1437 ;; No path, only description. Try to do something useful.
1438 (t (format "\\texttt{%s}" desc)))))
1441 ;;;; Babel Call
1443 ;; Babel Calls are ignored.
1446 ;;;; Macro
1448 (defun org-e-latex-macro (macro contents info)
1449 "Transcode a MACRO element from Org to LaTeX.
1450 CONTENTS is nil. INFO is a plist holding contextual information."
1451 ;; Use available tools.
1452 (org-export-expand-macro macro info))
1455 ;;;; Paragraph
1457 (defun org-e-latex-paragraph (paragraph contents info)
1458 "Transcode a PARAGRAPH element from Org to LaTeX.
1459 CONTENTS is the contents of the paragraph, as a string. INFO is
1460 the plist used as a communication channel."
1461 contents)
1464 ;;;; Plain List
1466 (defun org-e-latex-plain-list (plain-list contents info)
1467 "Transcode a PLAIN-LIST element from Org to LaTeX.
1468 CONTENTS is the contents of the list. INFO is a plist holding
1469 contextual information."
1470 (let* ((type (org-element-get-property :type plain-list))
1471 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1472 "inparadesc" "asparadesc"))
1473 (paralist-regexp (concat
1474 "\\("
1475 (mapconcat 'identity paralist-types "\\|")
1476 "\\)"))
1477 (attr (mapconcat #'identity
1478 (org-element-get-property :attr_latex plain-list)
1479 " "))
1480 (latex-type (cond
1481 ((and attr
1482 (string-match
1483 (format "\\<%s\\>" paralist-regexp) attr))
1484 (match-string 1 attr))
1485 ((eq type 'ordered) "enumerate")
1486 ((eq type 'unordered) "itemize")
1487 ((eq type 'descriptive) "description"))))
1488 (org-e-latex--wrap-label
1489 plain-list
1490 (format "\\begin{%s}%s\n%s\\end{%s}"
1491 latex-type
1492 ;; Once special environment, if any, has been removed, the
1493 ;; rest of the attributes will be optional arguments.
1494 ;; They will be put inside square brackets if necessary.
1495 (let ((opt (replace-regexp-in-string
1496 (format " *%s *" paralist-regexp) "" attr)))
1497 (cond ((string= opt "") "")
1498 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1499 (t (format "[%s]" opt))))
1500 contents
1501 latex-type))))
1504 ;;;; Plain Text
1506 (defun org-e-latex-plain-text (text info)
1507 "Transcode a TEXT string from Org to LaTeX.
1508 TEXT is the string to transcode. INFO is a plist holding
1509 contextual information."
1510 ;; Protect %, #, &, $, ~, ^, _, { and }.
1511 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1512 (setq text
1513 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1514 ;; Protect \
1515 (setq text (replace-regexp-in-string
1516 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1517 "$\\backslash$" text nil t 1))
1518 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1519 (let ((case-fold-search nil)
1520 (start 0))
1521 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1522 (setq text (replace-match
1523 (format "\\%s{}" (match-string 1 text)) nil t text)
1524 start (match-end 0))))
1525 ;; Handle quotation marks
1526 (setq text (org-e-latex--quotation-marks text info))
1527 ;; Convert special strings.
1528 (when (plist-get info :with-special-strings)
1529 (while (string-match (regexp-quote "...") text)
1530 (setq text (replace-match "\\ldots{}" nil t text))))
1531 ;; Handle break preservation if required.
1532 (when (plist-get info :preserve-breaks)
1533 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1534 text)))
1535 ;; Return value.
1536 text)
1539 ;;;; Property Drawer
1541 (defun org-e-latex-property-drawer (property-drawer contents info)
1542 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1543 CONTENTS is nil. INFO is a plist holding contextual
1544 information."
1545 ;; The property drawer isn't exported but we want separating blank
1546 ;; lines nonetheless.
1550 ;;;; Quote Block
1552 (defun org-e-latex-quote-block (quote-block contents info)
1553 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1554 CONTENTS holds the contents of the block. INFO is a plist
1555 holding contextual information."
1556 (org-e-latex--wrap-label
1557 quote-block
1558 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1561 ;;;; Quote Section
1563 (defun org-e-latex-quote-section (quote-section contents info)
1564 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1565 CONTENTS is nil. INFO is a plist holding contextual information."
1566 (let ((value (org-remove-indentation
1567 (org-element-get-property :value quote-section))))
1568 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1571 ;;;; Section
1573 (defun org-e-latex-section (section contents info)
1574 "Transcode a SECTION element from Org to LaTeX.
1575 CONTENTS holds the contents of the section. INFO is a plist
1576 holding contextual information."
1577 contents)
1580 ;;;; Radio Target
1582 (defun org-e-latex-radio-target (radio-target text info)
1583 "Transcode a RADIO-TARGET object from Org to LaTeX.
1584 TEXT is the text of the target. INFO is a plist holding
1585 contextual information."
1586 (format "\\label{%s}%s"
1587 (org-export-solidify-link-text
1588 (org-element-get-property :raw-value radio-target))
1589 text))
1592 ;;;; Special Block
1594 (defun org-e-latex-special-block (special-block contents info)
1595 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1596 CONTENTS holds the contents of the block. INFO is a plist
1597 holding contextual information."
1598 (let ((type (downcase (org-element-get-property :type special-block))))
1599 (org-e-latex--wrap-label
1600 special-block
1601 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
1604 ;;;; Src Block
1606 (defun org-e-latex-src-block (src-block contents info)
1607 "Transcode a SRC-BLOCK element from Org to LaTeX.
1608 CONTENTS holds the contents of the item. INFO is a plist holding
1609 contextual information."
1610 (let* ((lang (org-element-get-property :language src-block))
1611 (code (org-export-handle-code src-block info))
1612 (caption (org-element-get-property :caption src-block))
1613 (label (org-element-get-property :name src-block))
1614 (custom-env (and lang
1615 (cadr (assq (intern lang)
1616 org-e-latex-custom-lang-environments)))))
1617 (cond
1618 ;; No source fontification.
1619 ((not org-e-latex-listings)
1620 (let ((caption-str (org-e-latex--caption/label-string
1621 caption label info))
1622 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
1623 (format (or float-env "%s")
1624 (concat
1625 caption-str
1626 (format "\\begin{verbatim}\n%s\\end{verbatim}" code)))))
1627 ;; Custom environment.
1628 (custom-env
1629 (format "\\begin{%s}\n%s\\end{%s}\n" custom-env code custom-env))
1630 ;; Use minted package.
1631 ((eq org-e-latex-listings 'minted)
1632 (let* ((mint-lang (or (cadr (assq (intern lang) org-e-latex-minted-langs))
1633 lang))
1634 (float-env (when (or label caption)
1635 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1636 (org-e-latex--caption/label-string
1637 caption label info))))
1638 (body (format "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1639 (org-e-latex--make-option-string
1640 org-e-latex-minted-options)
1641 mint-lang code)))
1642 (if float-env (format float-env body) body)))
1643 ;; Use listings package.
1645 (let ((lst-lang
1646 (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
1647 (caption-str
1648 (when caption
1649 (let ((main (org-export-secondary-string
1650 (car caption) 'e-latex info)))
1651 (if (not (cdr caption)) (format "{%s}" main)
1652 (format
1653 "{[%s]%s}"
1654 (org-export-secondary-string (cdr caption) 'e-latex info)
1655 main))))))
1656 (concat (format "\\lstset{%s}\n"
1657 (org-e-latex--make-option-string
1658 (append org-e-latex-listings-options
1659 `(("language" ,lst-lang))
1660 (when label `(("label" ,label)))
1661 (when caption-str
1662 `(("caption" ,caption-str))))))
1663 (format "\\begin{lstlisting}\n%s\\end{lstlisting}" code)))))))
1666 ;;;; Statistics Cookie
1668 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
1669 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1670 CONTENTS is nil. INFO is a plist holding contextual information."
1671 (org-element-get-property :value statistics-cookie))
1674 ;;;; Subscript
1676 (defun org-e-latex-subscript (subscript contents info)
1677 "Transcode a SUBSCRIPT object from Org to LaTeX.
1678 CONTENTS is the contents of the object. INFO is a plist holding
1679 contextual information."
1680 (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
1683 ;;;; Superscript
1685 (defun org-e-latex-superscript (superscript contents info)
1686 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1687 CONTENTS is the contents of the object. INFO is a plist holding
1688 contextual information."
1689 (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
1692 ;;;; Table
1694 (defun org-e-latex-table--format-string (table table-info info)
1695 "Return an appropriate format string for TABLE.
1697 TABLE-INFO is the plist containing format info about the table,
1698 as returned by `org-export-table-format-info'. INFO is a plist
1699 used as a communication channel.
1701 The format string one placeholder for the body of the table."
1702 (let* ((label (org-element-get-property :name table))
1703 (caption (org-e-latex--caption/label-string
1704 (org-element-get-property :caption table) label info))
1705 (attr (mapconcat #'identity
1706 (org-element-get-property :attr_latex table)
1707 " "))
1708 ;; Determine alignment string.
1709 (alignment (org-e-latex-table--align-string attr table-info))
1710 ;; Determine environment for the table: longtable, tabular...
1711 (table-env (cond
1712 ((not attr) org-e-latex-default-table-environment)
1713 ((string-match "\\<longtable\\>" attr) "longtable")
1714 ((string-match "\\(tabular.\\)" attr)
1715 (org-match-string-no-properties 1 attr))
1716 (t org-e-latex-default-table-environment)))
1717 ;; If table is a float, determine environment: table or table*.
1718 (float-env (cond
1719 ((string= "longtable" table-env) nil)
1720 ((and attr
1721 (or (string-match (regexp-quote "table*") attr)
1722 (string-match "\\<multicolumn\\>" attr)))
1723 "table*")
1724 ((or (not (string= caption "")) label) "table")))
1725 ;; Extract others display options.
1726 (width (and attr
1727 (string-match "\\<width=\\(\\S-+\\)" attr)
1728 (org-match-string-no-properties 1 attr)))
1729 (placement (if (and attr
1730 (string-match "\\<placement=\\(\\S-+\\)" attr))
1731 (org-match-string-no-properties 1 attr)
1732 (concat "["
1733 org-e-latex-default-figure-position
1734 "]"))))
1735 ;; Prepare the final format string for the table.
1736 (cond
1737 ;; Longtable.
1738 ((string= "longtable" table-env)
1739 (format "\\begin{longtable}{%s}\n%s\n%%s\n%s\\end{longtable}"
1740 alignment
1741 (if (or (not org-e-latex-table-caption-above)
1742 (string= "" caption))
1744 (concat (org-trim caption) "\\\\"))
1745 (if (or org-e-latex-table-caption-above
1746 (string= "" caption))
1748 (concat (org-trim caption) "\\\\\n"))))
1749 ;; Others.
1750 (t (concat (when float-env
1751 (concat
1752 (format "\\begin{%s}%s\n" float-env placement)
1753 (if org-e-latex-table-caption-above caption "")))
1754 (when org-e-latex-tables-centered "\\begin{center}\n")
1755 (format "\\begin{%s}%s{%s}\n%%s\n\\end{%s}"
1756 table-env
1757 (if width (format "{%s}" width) "")
1758 alignment
1759 table-env)
1760 (when org-e-latex-tables-centered "\n\\end{center}")
1761 (when float-env
1762 (concat (if org-e-latex-table-caption-above "" caption)
1763 (format "\n\\end{%s}" float-env))))))))
1765 (defun org-e-latex-table--align-string (attr table-info)
1766 "Return an appropriate LaTeX alignment string.
1767 ATTR is a string containing table's LaTeX specific attributes.
1768 TABLE-INFO is the plist containing format info about the table,
1769 as returned by `org-export-table-format-info'."
1770 (or (and attr
1771 (string-match "\\<align=\\(\\S-+\\)" attr)
1772 (match-string 1 attr))
1773 (let* ((align (copy-sequence (plist-get table-info :alignment)))
1774 (colgroups (copy-sequence (plist-get table-info :column-groups)))
1775 (cols (length align))
1776 (separators (make-vector (1+ cols) "")))
1777 ;; Ignore the first column if it's special.
1778 (when (plist-get table-info :special-column-p)
1779 (aset align 0 "") (aset colgroups 0 nil))
1780 (let ((col 0))
1781 (mapc (lambda (el)
1782 (let ((gr (aref colgroups col)))
1783 (when (memq gr '(start start-end))
1784 (aset separators col "|"))
1785 (when (memq gr '(end start-end))
1786 (aset separators (1+ col) "|")))
1787 (incf col))
1788 align))
1789 ;; Build the LaTeX specific alignment string.
1790 (loop for al across align
1791 for sep across separators
1792 concat (concat sep al) into output
1793 finally return (concat output (aref separators cols))))))
1795 (defun org-e-latex-table (table contents info)
1796 "Transcode a TABLE element from Org to LaTeX.
1797 CONTENTS is nil. INFO is a plist holding contextual information."
1798 (let ((attr (mapconcat #'identity
1799 (org-element-get-property :attr_latex table)
1800 " "))
1801 (raw-table (org-element-get-property :raw-table table)))
1802 (cond
1803 ;; Case 1: verbatim table.
1804 ((or org-e-latex-tables-verbatim
1805 (and attr (string-match "\\<verbatim\\>" attr)))
1806 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
1807 (org-export-clean-table
1808 raw-table
1809 (plist-get (org-export-table-format-info raw-table)
1810 :special-column-p))))
1811 ;; Case 2: table.el table. Convert it using appropriate tools.
1812 ((eq (org-element-get-property :type table) 'table.el)
1813 (require 'table)
1814 ;; Ensure "*org-export-table*" buffer is empty.
1815 (and (get-buffer "*org-export-table*")
1816 (kill-buffer (get-buffer "*org-export-table*")))
1817 (let ((output (with-temp-buffer
1818 (insert raw-table)
1819 (goto-char 1)
1820 (re-search-forward "^[ \t]*|[^|]" nil t)
1821 (table-generate-source 'latex "*org-export-table*")
1822 (with-current-buffer "*org-export-table*"
1823 (org-trim (buffer-string))))))
1824 (kill-buffer (get-buffer "*org-export-table*"))
1825 ;; Remove left out comments.
1826 (while (string-match "^%.*\n" output)
1827 (setq output (replace-match "" t t output)))
1828 ;; When the "rmlines" attribute is provided, remove all hlines
1829 ;; but the the one separating heading from the table body.
1830 (when (and attr (string-match "\\<rmlines\\>" attr))
1831 (let ((n 0) (pos 0))
1832 (while (and (< (length output) pos)
1833 (setq pos (string-match "^\\\\hline\n?" output pos)))
1834 (incf n)
1835 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
1836 (if org-e-latex-tables-centered
1837 (format "\\begin{center}\n%s\n\\end{center}" output)
1838 output)))
1839 ;; Case 3: Standard table.
1841 (let* ((table-info (org-export-table-format-info raw-table))
1842 (clean-table (org-export-clean-table
1843 raw-table (plist-get table-info :special-column-p)))
1844 (columns-number (length (plist-get table-info :alignment))))
1845 ;; Convert ROWS to send them to `orgtbl-to-latex'. In
1846 ;; particular, send each cell to
1847 ;; `org-element-parse-secondary-string' to expand any Org
1848 ;; object within. Eventually, flesh the format string out with
1849 ;; the table.
1850 (format (org-e-latex-table--format-string table table-info info)
1851 (orgtbl-to-latex
1852 (mapcar
1853 (lambda (row)
1854 (if (string-match org-table-hline-regexp row)
1855 'hline
1856 (mapcar
1857 (lambda (cell)
1858 (org-export-secondary-string
1859 (org-element-parse-secondary-string
1860 cell
1861 (cdr (assq 'table org-element-string-restrictions)))
1862 'e-latex info))
1863 (org-split-string row "[ \t]*|[ \t]*"))))
1864 (org-split-string clean-table "\n"))
1865 `(:tstart nil :tend nil
1866 ;; Longtable environment requires specific
1867 ;; header line end.
1868 :hlend ,(and attr
1869 (string-match "\\<longtable\\>" attr)
1870 (format "\\\\
1871 \\hline
1872 \\endhead
1873 \\hline\\multicolumn{%d}{r}{Continued on next page}\\\\
1874 \\endfoot
1875 \\endlastfoot"
1876 columns-number))))))))))
1879 ;;;; Target
1881 (defun org-e-latex-target (target text info)
1882 "Transcode a TARGET object from Org to LaTeX.
1883 TEXT is the text of the target. INFO is a plist holding
1884 contextual information."
1885 (format "\\label{%s}%s"
1886 (org-export-solidify-link-text
1887 (org-element-get-property :raw-value target))
1888 text))
1891 ;;;; Time-stamp
1893 (defun org-e-latex-time-stamp (time-stamp contents info)
1894 "Transcode a TIME-STAMP object from Org to LaTeX.
1895 CONTENTS is nil. INFO is a plist holding contextual
1896 information."
1897 (let ((value (org-element-get-property :value time-stamp))
1898 (type (org-element-get-property :type time-stamp))
1899 (appt-type (org-element-get-property :appt-type time-stamp)))
1900 (concat (cond ((eq appt-type 'scheduled)
1901 (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
1902 ((eq appt-type 'deadline)
1903 (format "\\textbf{\\textsc{%s}} " org-deadline-string))
1904 ((eq appt-type 'closed)
1905 (format "\\textbf{\\textsc{%s}} " org-closed-string)))
1906 (cond ((memq type '(active active-range))
1907 (format org-e-latex-active-timestamp-format value))
1908 ((memq type '(inactive inactive-range))
1909 (format org-e-latex-inactive-timestamp-format value))
1911 (format org-e-latex-diary-timestamp-format value))))))
1914 ;;;; Verbatim
1916 (defun org-e-latex-verbatim (verbatim contents info)
1917 "Transcode a VERBATIM object from Org to LaTeX.
1918 CONTENTS is nil. INFO is a plist used as a communication
1919 channel."
1920 (let ((fmt (cdr (assoc (org-element-get-property :marker verbatim)
1921 org-e-latex-emphasis-alist)))
1922 (value (org-element-get-property :value verbatim)))
1923 (cond
1924 ;; Handle the `verb' special case.
1925 ((eq 'verb fmt)
1926 (let ((separator (org-e-latex--find-verb-separator value)))
1927 (concat "\\verb" separator value separator)))
1928 ;; Handle the `protectedtexttt' special case.
1929 ((eq 'protectedtexttt fmt)
1930 (let ((start 0)
1931 (trans '(("\\" . "\\textbackslash{}")
1932 ("~" . "\\textasciitilde{}")
1933 ("^" . "\\textasciicircum{}")))
1934 (rtn "")
1935 char)
1936 (while (string-match "[\\{}$%&_#~^]" value)
1937 (setq char (match-string 0 value))
1938 (if (> (match-beginning 0) 0)
1939 (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
1940 (setq value (substring value (1+ (match-beginning 0))))
1941 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1942 rtn (concat rtn char)))
1943 (setq value (concat rtn value)
1944 fmt "\\texttt{%s}")
1945 (while (string-match "--" value)
1946 (setq value (replace-match "-{}-" t t value)))
1947 (format fmt value)))
1948 ;; Else use format string.
1949 (t (format fmt value)))))
1952 ;;;; Verse Block
1954 (defun org-e-latex-verse-block (verse-block contents info)
1955 "Transcode a VERSE-BLOCK element from Org to LaTeX.
1956 CONTENTS is nil. INFO is a plist holding contextual information."
1957 (org-e-latex--wrap-label
1958 verse-block
1959 ;; In a verse environment, add a line break to each newline
1960 ;; character and change each white space at beginning of a line
1961 ;; into a space of 1 em. Also change each blank line with
1962 ;; a vertical space of 1 em.
1963 (progn
1964 (setq contents (replace-regexp-in-string
1965 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1966 (replace-regexp-in-string
1967 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1968 (org-remove-indentation
1969 (org-export-secondary-string
1970 (org-element-get-property :value verse-block)
1971 'e-latex info)))))
1972 (while (string-match "^[ \t]+" contents)
1973 (let ((new-str (format "\\hspace*{%dem}"
1974 (length (match-string 0 contents)))))
1975 (setq contents (replace-match new-str nil t contents))))
1976 (format "\\begin{verse}\n%s\\end{verse}" contents))))
1980 ;;; Interactive functions
1982 (defun org-e-latex-export-to-latex
1983 (&optional subtreep visible-only body-only ext-plist pub-dir)
1984 "Export current buffer to a LaTeX file.
1986 If narrowing is active in the current buffer, only export its
1987 narrowed part.
1989 If a region is active, export that region.
1991 When optional argument SUBTREEP is non-nil, export the sub-tree
1992 at point, extracting information from the headline properties
1993 first.
1995 When optional argument VISIBLE-ONLY is non-nil, don't export
1996 contents of hidden elements.
1998 When optional argument BODY-ONLY is non-nil, only write code
1999 between \"\\begin{document}\" and \"\\end{document}\".
2001 EXT-PLIST, when provided, is a property list with external
2002 parameters overriding Org default settings, but still inferior to
2003 file-local settings.
2005 When optional argument PUB-DIR is set, use it as the publishing
2006 directory.
2008 Return output file's name."
2009 (interactive)
2010 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
2011 (org-export-to-file
2012 'e-latex outfile subtreep visible-only body-only ext-plist)))
2014 (defun org-e-latex-export-to-pdf
2015 (&optional subtreep visible-only body-only ext-plist pub-dir)
2016 "Export current buffer to LaTeX then process through to PDF.
2018 If narrowing is active in the current buffer, only export its
2019 narrowed part.
2021 If a region is active, export that region.
2023 When optional argument SUBTREEP is non-nil, export the sub-tree
2024 at point, extracting information from the headline properties
2025 first.
2027 When optional argument VISIBLE-ONLY is non-nil, don't export
2028 contents of hidden elements.
2030 When optional argument BODY-ONLY is non-nil, only write code
2031 between \"\\begin{document}\" and \"\\end{document}\".
2033 EXT-PLIST, when provided, is a property list with external
2034 parameters overriding Org default settings, but still inferior to
2035 file-local settings.
2037 When optional argument PUB-DIR is set, use it as the publishing
2038 directory.
2040 Return PDF file's name."
2041 (interactive)
2042 (org-e-latex-compile
2043 (org-e-latex-export-to-latex
2044 subtreep visible-only body-only ext-plist pub-dir)))
2046 (defun org-e-latex-compile (texfile)
2047 "Compile a TeX file.
2049 TEXFILE is the name of the file being compiled. Processing is
2050 done through the command specified in `org-e-latex-pdf-process'.
2052 Return PDF file name or an error if it couldn't be produced."
2053 (let* ((wconfig (current-window-configuration))
2054 (texfile (file-truename texfile))
2055 (base (file-name-sans-extension texfile))
2056 errors)
2057 (message (format "Processing LaTeX file %s ..." texfile))
2058 (unwind-protect
2059 (progn
2060 (cond
2061 ;; A function is provided: Apply it.
2062 ((functionp org-latex-to-pdf-process)
2063 (funcall org-latex-to-pdf-process (shell-quote-argument texfile)))
2064 ;; A list is provided: Replace %b, %f and %o with appropriate
2065 ;; values in each command before applying it. Output is
2066 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2067 ((consp org-e-latex-pdf-process)
2068 (let* ((out-dir (or (file-name-directory texfile) "./"))
2069 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2070 (mapc
2071 (lambda (command)
2072 (shell-command
2073 (replace-regexp-in-string
2074 "%b" (shell-quote-argument base)
2075 (replace-regexp-in-string
2076 "%f" (shell-quote-argument texfile)
2077 (replace-regexp-in-string
2078 "%o" (shell-quote-argument out-dir) command)))
2079 outbuf))
2080 org-e-latex-pdf-process)
2081 ;; Collect standard errors from output buffer.
2082 (setq errors (org-e-latex-collect-errors outbuf))))
2083 (t (error "No valid command to process to PDF")))
2084 (let ((pdffile (concat base ".pdf")))
2085 ;; Check for process failure. Provide collected errors if
2086 ;; possible.
2087 (if (not (file-exists-p pdffile))
2088 (error (concat (format "PDF file %s wasn't produced" pdffile)
2089 (when errors (concat ": " errors))))
2090 ;; Else remove log files, when specified, and signal end of
2091 ;; process to user, along with any error encountered.
2092 (when org-e-latex-remove-logfiles
2093 (dolist (ext org-e-latex-logfiles-extensions)
2094 (let ((file (concat base "." ext)))
2095 (when (file-exists-p file) (delete-file file)))))
2096 (message (concat "Process completed"
2097 (if (not errors) "."
2098 (concat " with errors: " errors)))))
2099 ;; Return output file name.
2100 pdffile))
2101 (set-window-configuration wconfig))))
2103 (defun org-e-latex-collect-errors (buffer)
2104 "Collect some kind of errors from \"pdflatex\" command output.
2106 BUFFER is the buffer containing output.
2108 Return collected error types as a string, or nil if there was
2109 none."
2110 (with-current-buffer buffer
2111 (save-excursion
2112 (goto-char (point-max))
2113 ;; Find final "pdflatex" run.
2114 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
2115 (let ((case-fold-search t)
2116 (errors ""))
2117 (when (save-excursion
2118 (re-search-forward "Reference.*?undefined" nil t))
2119 (setq errors (concat errors " [undefined reference]")))
2120 (when (save-excursion
2121 (re-search-forward "Citation.*?undefined" nil t))
2122 (setq errors (concat errors " [undefined citation]")))
2123 (when (save-excursion
2124 (re-search-forward "Undefined control sequence" nil t))
2125 (setq errors (concat errors " [undefined control sequence]")))
2126 (when (save-excursion
2127 (re-search-forward "^! LaTeX.*?Error" nil t))
2128 (setq errors (concat errors " [LaTeX error]")))
2129 (when (save-excursion
2130 (re-search-forward "^! Package.*?Error" nil t))
2131 (setq errors (concat errors " [package error]")))
2132 (and (org-string-nw-p errors) (org-trim errors)))))))
2135 (provide 'org-e-latex)
2136 ;;; org-e-latex.el ends here