Document max number of stars in headings in docstring of org-inlinetask-minlevel
[org-mode.git] / EXPERIMENTAL / org-e-latex.el
blob5de0595a94eacaca73c4b415843ef77539f4ffac
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. Author.
807 (let ((author (and (plist-get info :with-author)
808 (let ((auth (plist-get info :author)))
809 (and auth (org-export-secondary-string
810 auth 'e-latex info)))))
811 (email (and (plist-get info :with-email)
812 (org-export-secondary-string
813 (plist-get info :email) 'e-latex info))))
814 (cond ((and author email (not (string= "" email)))
815 (format "\\author{%s\\thanks{%s}}\n" author email))
816 (author (format "\\author{%s}\n" author))
817 (t "\\author{}\n")))
818 ;; 5. Date.
819 (let ((date (plist-get info :date)))
820 (and date (format "\\date{%s}\n" date)))
821 ;; 6. Title
822 (format "\\title{%s}\n" title)
823 ;; 7. Hyperref options.
824 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
825 (or (plist-get info :keywords) "")
826 (or (plist-get info :description) "")
827 (let ((creator-info (plist-get info :with-creator)))
828 (cond
829 ((not creator-info) "")
830 ((eq creator-info 'comment) "")
831 (t (plist-get info :creator)))))
832 ;; 7. Document start.
833 "\\begin{document}\n\n"
834 ;; 8. Title command.
835 (org-element-normalize-string
836 (cond ((string= "" title) nil)
837 ((not (stringp org-e-latex-title-command)) nil)
838 ((string-match "\\(?:[^%]\\|^\\)%s"
839 org-e-latex-title-command)
840 (format org-e-latex-title-command title))
841 (t org-e-latex-title-command)))
842 ;; 9. Table of contents.
843 (let ((depth (plist-get info :with-toc)))
844 (when depth
845 (concat (when (wholenump depth)
846 (format "\\setcounter{tocdepth}{%d}\n" depth))
847 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
848 ;; 10. Document's body.
849 contents
850 ;; 11. Creator.
851 (let ((creator-info (plist-get info :with-creator)))
852 (cond
853 ((not creator-info))
854 ((eq creator-info 'comment)
855 (format "%% %s\n" (plist-get info :creator)))
856 (t (concat (plist-get info :creator) "\n"))))
857 ;; 12. Document end.
858 "\\end{document}")))
862 ;;; Transcode Functions
864 ;;;; Block
866 (defun org-e-latex-center-block (center-block contents info)
867 "Transcode a CENTER-BLOCK element from Org to LaTeX.
868 CONTENTS holds the contents of the block. INFO is a plist
869 holding contextual information."
870 (org-e-latex--wrap-label
871 center-block
872 (format "\\begin{center}\n%s\\end{center}" contents)))
875 ;;;; Comment
877 ;; Comments are ignored.
880 ;;;; Comment Block
882 ;; Comment Blocks are ignored.
885 ;;;; Drawer
887 (defun org-e-latex-drawer (drawer contents info)
888 "Transcode a DRAWER element from Org to LaTeX.
889 CONTENTS holds the contents of the block. INFO is a plist
890 holding contextual information."
891 (let* ((name (org-element-get-property :drawer-name drawer))
892 (output (if (functionp org-e-latex-format-drawer-function)
893 (funcall org-e-latex-format-drawer-function
894 name contents)
895 ;; If there's no user defined function: simply
896 ;; display contents of the drawer.
897 contents)))
898 (org-e-latex--wrap-label drawer output)))
901 ;;;; Dynamic Block
903 (defun org-e-latex-dynamic-block (dynamic-block contents info)
904 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
905 CONTENTS holds the contents of the block. INFO is a plist
906 holding contextual information. See
907 `org-export-data'."
908 (org-e-latex--wrap-label dynamic-block contents))
911 ;;;; Emphasis
913 (defun org-e-latex-emphasis (emphasis contents info)
914 "Transcode EMPHASIS from Org to LaTeX.
915 CONTENTS is the contents of the emphasized text. INFO is a plist
916 holding contextual information.."
917 (format (cdr (assoc (org-element-get-property :marker emphasis)
918 org-e-latex-emphasis-alist))
919 contents))
922 ;;;; Entity
924 (defun org-e-latex-entity (entity contents info)
925 "Transcode an ENTITY object from Org to LaTeX.
926 CONTENTS are the definition itself. INFO is a plist holding
927 contextual information."
928 (let ((ent (org-element-get-property :latex entity)))
929 (if (org-element-get-property :latex-math-p entity)
930 (format "$%s$" ent)
931 ent)))
934 ;;;; Example Block
936 (defun org-e-latex-example-block (example-block contents info)
937 "Transcode a EXAMPLE-BLOCK element from Org to LaTeX.
938 CONTENTS is nil. INFO is a plist holding contextual information."
939 (let* ((options (or (org-element-get-property :options example-block) ""))
940 (value (org-export-handle-code example-block info)))
941 (org-e-latex--wrap-label
942 example-block (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
945 ;;;; Export Snippet
947 (defun org-e-latex-export-snippet (export-snippet contents info)
948 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
949 CONTENTS is nil. INFO is a plist holding contextual information."
950 (org-element-get-property :value export-snippet))
953 ;;;; Export Block
955 (defun org-e-latex-export-block (export-block contents info)
956 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
957 CONTENTS is nil. INFO is a plist holding contextual information."
958 (when (string= (org-element-get-property :type export-block) "latex")
959 (org-remove-indentation (org-element-get-property :value export-block))))
962 ;;;; Fixed Width
964 (defun org-e-latex-fixed-width (fixed-width contents info)
965 "Transcode a FIXED-WIDTH element from Org to LaTeX.
966 CONTENTS is nil. INFO is a plist holding contextual information."
967 (let* ((value (org-element-normalize-string
968 (replace-regexp-in-string
969 "^[ \t]*: ?" ""
970 (org-element-get-property :value fixed-width)))))
971 (org-e-latex--wrap-label
972 fixed-width (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
975 ;;;; Footnote Definition
977 ;; Footnote Definitions are ignored.
980 ;;;; Footnote Reference
982 (defun org-e-latex-footnote-reference (footnote-reference contents info)
983 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
984 CONTENTS is nil. INFO is a plist holding contextual information."
985 (concat
986 ;; Insert separator between two footnotes in a row.
987 (let ((prev (org-export-get-previous-element footnote-reference info)))
988 (when (and (listp prev) (eq (car prev) 'footnote-reference))
989 org-e-latex-footnote-separator))
990 ;; Use \footnotemark if the footnote has already been defined.
991 ;; Otherwise, define it with \footnote command.
992 (cond
993 ((not (org-export-footnote-first-reference-p footnote-reference info))
994 (format "\\footnotemark[%s]"
995 (org-export-get-footnote-number footnote-reference info)))
996 ;; Inline definitions are secondary strings.
997 ((eq (org-element-get-property :type footnote-reference) 'inline)
998 (format "\\footnote{%s}"
999 (org-trim
1000 (org-export-secondary-string
1001 (org-export-get-footnote-definition footnote-reference info)
1002 'e-latex info))))
1003 ;; Non-inline footnotes definitions are full Org data.
1005 (format "\\footnote{%s}"
1006 (org-trim
1007 (org-export-data
1008 (org-export-get-footnote-definition footnote-reference info)
1009 'e-latex info)))))))
1012 ;;;; Headline
1014 (defun org-e-latex-headline (headline contents info)
1015 "Transcode an HEADLINE element from Org to LaTeX.
1016 CONTENTS holds the contents of the headline. INFO is a plist
1017 holding contextual information."
1018 (let* ((class (plist-get info :latex-class))
1019 (numberedp (plist-get info :section-numbers))
1020 ;; Get level relative to current parsed data.
1021 (level (org-export-get-relative-level headline info))
1022 (class-sectionning (assoc class org-e-latex-classes))
1023 ;; Section formatting will set two placeholders: one for the
1024 ;; title and the other for the contents.
1025 (section-fmt
1026 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
1027 (fboundp (nth 2 class-sectionning)))
1028 (funcall (nth 2 class-sectionning) level numberedp)
1029 (nth (1+ level) class-sectionning))))
1030 (cond
1031 ;; No section available for that LEVEL.
1032 ((not sec) nil)
1033 ;; Section format directly returned by a function.
1034 ((stringp sec) sec)
1035 ;; (numbered-section . unnumbered-section)
1036 ((not (consp (cdr sec)))
1037 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1038 ;; (numbered-open numbered-close)
1039 ((= (length sec) 2)
1040 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1041 ;; (num-in num-out no-num-in no-num-out)
1042 ((= (length sec) 4)
1043 (if numberedp
1044 (concat (car sec) "\n%s" (nth 1 sec))
1045 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1046 (text (org-export-secondary-string
1047 (org-element-get-property :title headline) 'e-latex info))
1048 (todo (and (plist-get info :with-todo-keywords)
1049 (let ((todo (org-element-get-property
1050 :todo-keyword headline)))
1051 (and todo
1052 (org-export-secondary-string todo 'e-latex info)))))
1053 (todo-type (and todo (org-element-get-property :todo-type headline)))
1054 (tags (and (plist-get info :with-tags)
1055 (org-element-get-property :tags headline)))
1056 (priority (and (plist-get info :with-priority)
1057 (org-element-get-property :priority headline)))
1058 ;; Create the headline text.
1059 (full-text (if (functionp org-e-latex-format-headline-function)
1060 ;; User-defined formatting function.
1061 (funcall org-e-latex-format-headline-function
1062 todo todo-type priority text tags)
1063 ;; Default formatting.
1064 (concat
1065 (when todo
1066 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1067 (when priority (format "\\framebox{\\#%c} " priority))
1068 text
1069 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1070 ;; Associate some \label to the headline for internal links.
1071 (headline-labels (mapconcat
1072 (lambda (p)
1073 (let ((val (org-element-get-property p headline)))
1074 (when val (format "\\label{%s}\n"
1075 (if (eq p :begin)
1076 (format "headline-%s" val)
1077 val)))))
1078 '(:custom-id :id :begin) ""))
1079 (pre-blanks (make-string (org-element-get-property :pre-blank headline)
1080 10)))
1081 (cond
1082 ;; Case 1: This is a footnote section: ignore it.
1083 ((org-element-get-property :footnote-section-p headline) nil)
1084 ;; Case 2. This is a deep sub-tree: export it as a list item.
1085 ;; Also export as items headlines for which no section
1086 ;; format has been found.
1087 ((or (not section-fmt) (org-export-low-level-p headline info))
1088 ;; Build the real contents of the sub-tree.
1089 (let ((low-level-body
1090 (concat
1091 ;; If the headline is the first sibling, start a list.
1092 (when (org-export-first-sibling-p headline info)
1093 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1094 ;; Itemize headline
1095 "\\item " full-text "\n" headline-labels pre-blanks contents)))
1096 ;; If headline in the last sibling, close the list, before any
1097 ;; blank line. Otherwise, simply return LOW-LEVEL-BODY.
1098 (if (org-export-last-sibling-p headline info)
1099 (replace-regexp-in-string
1100 "[ \t\n]*\\'"
1101 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1102 low-level-body)
1103 low-level-body)))
1104 ;; Case 3. Standard headline. Export it as a section.
1105 (t (format section-fmt full-text
1106 (concat headline-labels pre-blanks contents))))))
1109 ;;;; Horizontal Rule
1111 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1112 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1113 CONTENTS is nil. INFO is a plist holding contextual information."
1114 (let ((attr (mapconcat #'identity
1115 (org-element-get-property :attr_latex horizontal-rule)
1116 " ")))
1117 (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
1120 ;;;; Inline Babel Call
1122 ;; Inline Babel Calls are ignored.
1125 ;;;; Inline Src Block
1127 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1128 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1129 CONTENTS holds the contents of the item. INFO is a plist holding
1130 contextual information."
1131 (let* ((code (org-element-get-property :value inline-src-block))
1132 (separator (org-e-latex--find-verb-separator code)))
1133 (cond
1134 ;; Do not use a special package: transcode it verbatim.
1135 ((not org-e-latex-listings)
1136 (concat "\\verb" separator code separator))
1137 ;; Use minted package.
1138 ((eq org-e-latex-listings 'minted)
1139 (let* ((org-lang (org-element-get-property :language inline-src-block))
1140 (mint-lang (or (cadr (assq (intern org-lang)
1141 org-e-latex-minted-langs))
1142 org-lang))
1143 (options (org-e-latex--make-option-string
1144 org-e-latex-minted-options)))
1145 (concat (format "\\mint%s{%s}"
1146 (if (string= options "") "" (format "[%s]" options))
1147 mint-lang)
1148 separator code separator)))
1149 ;; Use listings package.
1151 ;; Maybe translate language's name.
1152 (let* ((org-lang (org-element-get-property :language inline-src-block))
1153 (lst-lang (or (cadr (assq (intern org-lang)
1154 org-e-latex-listings-langs))
1155 org-lang))
1156 (options (org-e-latex--make-option-string
1157 (append org-e-latex-listings-options
1158 `(("language" ,lst-lang))))))
1159 (concat (format "\\lstinline[%s]" options)
1160 separator code separator))))))
1163 ;;;; Inlinetask
1165 (defun org-e-latex-inlinetask (inlinetask contents info)
1166 "Transcode an INLINETASK element from Org to LaTeX.
1167 CONTENTS holds the contents of the block. INFO is a plist
1168 holding contextual information."
1169 (let ((title (org-export-secondary-string
1170 (org-element-get-property :title inlinetask) 'e-latex info))
1171 (todo (and (plist-get info :with-todo-keywords)
1172 (let ((todo (org-element-get-property
1173 :todo-keyword inlinetask)))
1174 (and todo
1175 (org-export-secondary-string todo 'e-latex info)))))
1176 (todo-type (org-element-get-property :todo-type inlinetask))
1177 (tags (and (plist-get info :with-tags)
1178 (org-element-get-property :tags inlinetask)))
1179 (priority (and (plist-get info :with-priority)
1180 (org-element-get-property :priority inlinetask))))
1181 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1182 ;; with appropriate arguments.
1183 (if (functionp org-e-latex-format-inlinetask-function)
1184 (funcall org-e-latex-format-inlinetask-function
1185 todo todo-type priority title tags contents)
1186 ;; Otherwise, use a default template.
1187 (org-e-latex--wrap-label
1188 inlinetask
1189 (let ((full-title
1190 (concat
1191 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1192 (when priority (format "\\framebox{\\#%c} " priority))
1193 title
1194 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1195 (format (concat "\\begin{center}\n"
1196 "\\fbox{\n"
1197 "\\begin{minipage}[c]{.6\\textwidth}\n"
1198 "%s\n\n"
1199 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1200 "%s"
1201 "\\end{minipage}\n"
1202 "}\n"
1203 "\\end{center}")
1204 full-title contents))))))
1207 ;;;; Item
1209 (defun org-e-latex-item (item contents info)
1210 "Transcode an ITEM element from Org to LaTeX.
1211 CONTENTS holds the contents of the item. INFO is a plist holding
1212 contextual information."
1213 ;; Grab `:level' from plain-list properties, which is always the
1214 ;; first element above current item.
1215 (let* ((level (org-element-get-property
1216 :level (car (plist-get info :genealogy))))
1217 (counter (let ((count (org-element-get-property :counter item)))
1218 (and count
1219 (< level 4)
1220 (format "\\setcounter{enum%s}{%s}\n"
1221 (nth level '("i" "ii" "iii" "iv"))
1222 (1- count)))))
1223 (checkbox (let ((checkbox (org-element-get-property :checkbox item)))
1224 (cond ((eq checkbox 'on) "$\\boxtimes$ ")
1225 ((eq checkbox 'off) "$\\Box$ ")
1226 ((eq checkbox 'trans) "$\\boxminus$ "))))
1227 (tag (let ((tag (org-element-get-property :tag item)))
1228 (and tag
1229 (format "[%s]" (org-export-secondary-string
1230 tag 'e-latex info))))))
1231 (concat counter "\\item" tag " " checkbox contents)))
1234 ;;;; Keyword
1236 (defun org-e-latex-keyword (keyword contents info)
1237 "Transcode a KEYWORD element from Org to LaTeX.
1238 CONTENTS is nil. INFO is a plist holding contextual information."
1239 (let ((key (downcase (org-element-get-property :key keyword)))
1240 (value (org-element-get-property :value keyword)))
1241 (cond
1242 ((string= key "latex") value)
1243 ((string= key "index") (format "\\index{%s}" value))
1244 ((string= key "target")
1245 (format "\\label{%s}" (org-export-solidify-link-text value)))
1246 ((string= key "toc")
1247 (let ((value (downcase value)))
1248 (cond
1249 ((string-match "\\<headlines\\>" value)
1250 (let ((depth (or (and (string-match "[0-9]+" value)
1251 (string-to-number (match-string 0 value)))
1252 (plist-get info :with-toc))))
1253 (concat
1254 (when (wholenump depth)
1255 (format "\\setcounter{tocdepth}{%s}\n" depth))
1256 "\\tableofcontents")))
1257 ((string= "tables" value) "\\listoftables")
1258 ((string= "figures" value) "\\listoffigures")
1259 ((string= "listings" value) "\\listoflistings"))))
1260 ((string= key "include")
1261 (org-export-included-file keyword 'e-latex info)))))
1264 ;;;; Latex Environment
1266 (defun org-e-latex-latex-environment (latex-environment contents info)
1267 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1268 CONTENTS is nil. INFO is a plist holding contextual information."
1269 (org-e-latex--wrap-label
1270 latex-environment
1271 (org-remove-indentation (org-element-get-property :value latex-environment))))
1274 ;;;; Latex Fragment
1276 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1277 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1278 CONTENTS is nil. INFO is a plist holding contextual information."
1279 (org-element-get-property :value latex-fragment))
1282 ;;;; Line Break
1284 (defun org-e-latex-line-break (line-break contents info)
1285 "Transcode a LINE-BREAK object from Org to LaTeX.
1286 CONTENTS is nil. INFO is a plist holding contextual information."
1287 "\\\\")
1290 ;;;; Link
1292 (defun org-e-latex-link--inline-image (path info)
1293 "Return LaTeX code for an image at PATH.
1294 INFO is a plist containing export options."
1295 (let* ((parent-props (nth 1 (car (plist-get info :genealogy))))
1296 (caption (org-e-latex--caption/label-string
1297 (plist-get parent-props :caption)
1298 (plist-get parent-props :name)
1299 info))
1300 ;; Retrieve latex attributes from the element around.
1301 (attr (let ((raw-attr
1302 (mapconcat #'identity
1303 (plist-get parent-props :attr_latex) " ")))
1304 (unless (string= raw-attr "") raw-attr)))
1305 (disposition
1306 (cond
1307 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1308 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1309 ((or (and attr (string-match "\\<float\\>" attr))
1310 (not (string= caption "")))
1311 'float)))
1312 (placement
1313 (cond
1314 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1315 (org-match-string-no-properties 1 attr))
1316 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1317 ((eq disposition 'float)
1318 (concat "[" org-e-latex-default-figure-position "]"))
1319 (t ""))))
1320 ;; Now clear ATTR from any special keyword and set a default
1321 ;; value if nothing is left.
1322 (if (not attr)
1323 (setq attr "")
1324 (while (string-match "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)"
1325 attr)
1326 (replace-match "" nil nil attr))
1327 (setq attr (org-trim 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 ;; Id: for now, assume it's an internal link. TODO: do something
1379 ;; to check if it isn't in the current file.
1380 ((string= type "id")
1381 (format "\\hyperref[%s]{%s}" path (or desc path)))
1382 ;; Custom-id, target or radioed target: replace link with the
1383 ;; normalized custom-id/target name.
1384 ((member type '("custom-id" "target" "radio"))
1385 (format "\\hyperref[%s]{%s}"
1386 (org-export-solidify-link-text path)
1387 (or desc (org-export-secondary-string path 'e-latex info))))
1388 ;; Fuzzy: With the help of `org-export-resolve-fuzzy-link', find
1389 ;; the destination of the link.
1390 ((string= type "fuzzy")
1391 (let ((destination (org-export-resolve-fuzzy-link link info)))
1392 (case (car destination)
1393 (target
1394 (format "\\hyperref[%s]{%s}"
1395 (org-export-solidify-link-text
1396 (org-element-get-property :raw-value destination))
1397 (or desc
1398 (org-export-secondary-string
1399 (org-element-get-property :raw-link link)
1400 'e-latex info))))
1401 (headline
1402 (format "\\hyperref[headline-%d]{%s}"
1403 (org-element-get-property :begin destination)
1404 (or desc
1405 (org-export-secondary-string
1406 (org-element-get-property :raw-link link)
1407 'e-latex info))))
1408 (otherwise
1409 (format "\\texttt{%s}"
1410 (or desc
1411 (org-export-secondary-string
1412 (org-element-get-property :raw-link link)
1413 'e-latex info)))))))
1414 ;; Coderef: replace link with the reference name or the
1415 ;; equivalent line number.
1416 ((string= type "coderef")
1417 (format (org-export-get-coderef-format path (or desc ""))
1418 (org-export-resolve-coderef path info)))
1419 ;; Link type is handled by a special function.
1420 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1421 (funcall protocol (org-link-unescape path) desc 'latex))
1422 ;; External link with a description part.
1423 ((and path desc) (format "\\href{%s}{%s}" path desc))
1424 ;; External link without a description part.
1425 (path (format "\\url{%s}" path))
1426 ;; No path, only description. Try to do something useful.
1427 (t (format "\\texttt{%s}" desc)))))
1430 ;;;; Babel Call
1432 ;; Babel Calls are ignored.
1435 ;;;; Macro
1437 (defun org-e-latex-macro (macro contents info)
1438 "Transcode a MACRO element from Org to LaTeX.
1439 CONTENTS is nil. INFO is a plist holding contextual information."
1440 ;; Use available tools.
1441 (org-export-expand-macro macro info))
1444 ;;;; Paragraph
1446 (defun org-e-latex-paragraph (paragraph contents info)
1447 "Transcode a PARAGRAPH element from Org to LaTeX.
1448 CONTENTS is the contents of the paragraph, as a string. INFO is
1449 the plist used as a communication channel."
1450 contents)
1453 ;;;; Plain List
1455 (defun org-e-latex-plain-list (plain-list contents info)
1456 "Transcode a PLAIN-LIST element from Org to LaTeX.
1457 CONTENTS is the contents of the list. INFO is a plist holding
1458 contextual information."
1459 (let* ((type (org-element-get-property :type plain-list))
1460 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1461 "inparadesc" "asparadesc"))
1462 (paralist-regexp (concat
1463 "\\("
1464 (mapconcat 'identity paralist-types "\\|")
1465 "\\)"))
1466 (attr (mapconcat #'identity
1467 (org-element-get-property :attr_latex plain-list)
1468 " "))
1469 (latex-type (cond
1470 ((and attr
1471 (string-match
1472 (format "\\<%s\\>" paralist-regexp) attr))
1473 (match-string 1 attr))
1474 ((eq type 'ordered) "enumerate")
1475 ((eq type 'unordered) "itemize")
1476 ((eq type 'descriptive) "description"))))
1477 (org-e-latex--wrap-label
1478 plain-list
1479 (format "\\begin{%s}%s\n%s\\end{%s}"
1480 latex-type
1481 ;; Once special environment, if any, has been removed, the
1482 ;; rest of the attributes will be optional arguments.
1483 ;; They will be put inside square brackets if necessary.
1484 (let ((opt (replace-regexp-in-string
1485 (format " *%s *" paralist-regexp) "" attr)))
1486 (cond ((string= opt "") "")
1487 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1488 (t (format "[%s]" opt))))
1489 contents
1490 latex-type))))
1493 ;;;; Plain Text
1495 (defun org-e-latex-plain-text (text info)
1496 "Transcode a TEXT string from Org to LaTeX.
1497 TEXT is the string to transcode. INFO is a plist holding
1498 contextual information."
1499 ;; Protect %, #, &, $, ~, ^, _, { and }.
1500 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1501 (setq text
1502 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1503 ;; Protect \
1504 (setq text (replace-regexp-in-string
1505 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1506 "$\\backslash$" text nil t 1))
1507 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1508 (let ((case-fold-search nil)
1509 (start 0))
1510 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1511 (setq text (replace-match
1512 (format "\\%s{}" (match-string 1 text)) nil t text)
1513 start (match-end 0))))
1514 ;; Handle quotation marks
1515 (setq text (org-e-latex--quotation-marks text info))
1516 ;; Convert special strings.
1517 (when (plist-get info :with-special-strings)
1518 (while (string-match (regexp-quote "...") text)
1519 (setq text (replace-match "\\ldots{}" nil t text))))
1520 ;; Handle break preservation if required.
1521 (when (plist-get info :preserve-breaks)
1522 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1523 text)))
1524 ;; Return value.
1525 text)
1528 ;;;; Property Drawer
1530 (defun org-e-latex-property-drawer (property-drawer contents info)
1531 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1532 CONTENTS is nil. INFO is a plist holding contextual
1533 information."
1534 ;; The property drawer isn't exported but we want separating blank
1535 ;; lines nonetheless.
1539 ;;;; Quote Block
1541 (defun org-e-latex-quote-block (quote-block contents info)
1542 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1543 CONTENTS holds the contents of the block. INFO is a plist
1544 holding contextual information."
1545 (org-e-latex--wrap-label
1546 quote-block
1547 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1550 ;;;; Quote Section
1552 (defun org-e-latex-quote-section (quote-section contents info)
1553 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1554 CONTENTS is nil. INFO is a plist holding contextual information."
1555 (let ((value (org-remove-indentation
1556 (org-element-get-property :value quote-section))))
1557 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1560 ;;;; Section
1562 (defun org-e-latex-section (section contents info)
1563 "Transcode a SECTION element from Org to LaTeX.
1564 CONTENTS holds the contents of the section. INFO is a plist
1565 holding contextual information."
1566 contents)
1569 ;;;; Radio Target
1571 (defun org-e-latex-radio-target (radio-target text info)
1572 "Transcode a RADIO-TARGET object from Org to LaTeX.
1573 TEXT is the text of the target. INFO is a plist holding
1574 contextual information."
1575 (format "\\label{%s}%s"
1576 (org-export-solidify-link-text
1577 (org-element-get-property :raw-value radio-target))
1578 text))
1581 ;;;; Special Block
1583 (defun org-e-latex-special-block (special-block contents info)
1584 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1585 CONTENTS holds the contents of the block. INFO is a plist
1586 holding contextual information."
1587 (let ((type (downcase (org-element-get-property :type special-block))))
1588 (org-e-latex--wrap-label
1589 special-block
1590 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
1593 ;;;; Src Block
1595 (defun org-e-latex-src-block (src-block contents info)
1596 "Transcode a SRC-BLOCK element from Org to LaTeX.
1597 CONTENTS holds the contents of the item. INFO is a plist holding
1598 contextual information."
1599 (let* ((lang (org-element-get-property :language src-block))
1600 (code (org-export-handle-code src-block info))
1601 (caption (org-element-get-property :caption src-block))
1602 (label (org-element-get-property :name src-block))
1603 (custom-env (and lang
1604 (cadr (assq (intern lang)
1605 org-e-latex-custom-lang-environments)))))
1606 (cond
1607 ;; No source fontification.
1608 ((not org-e-latex-listings)
1609 (let ((caption-str (org-e-latex--caption/label-string
1610 caption label info))
1611 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
1612 (format (or float-env "%s")
1613 (concat
1614 caption-str
1615 (format "\\begin{verbatim}\n%s\\end{verbatim}" code)))))
1616 ;; Custom environment.
1617 (custom-env
1618 (format "\\begin{%s}\n%s\\end{%s}\n" custom-env code custom-env))
1619 ;; Use minted package.
1620 ((eq org-e-latex-listings 'minted)
1621 (let* ((mint-lang (or (cadr (assq (intern lang) org-e-latex-minted-langs))
1622 lang))
1623 (float-env (when (or label caption)
1624 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1625 (org-e-latex--caption/label-string
1626 caption label info))))
1627 (body (format "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1628 (org-e-latex--make-option-string
1629 org-e-latex-minted-options)
1630 mint-lang code)))
1631 (if float-env (format float-env body) body)))
1632 ;; Use listings package.
1634 (let ((lst-lang
1635 (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
1636 (caption-str
1637 (when caption
1638 (let ((main (org-export-secondary-string
1639 (car caption) 'e-latex info)))
1640 (if (not (cdr caption)) (format "{%s}" main)
1641 (format
1642 "{[%s]%s}"
1643 (org-export-secondary-string (cdr caption) 'e-latex info)
1644 main))))))
1645 (concat (format "\\lstset{%s}\n"
1646 (org-e-latex--make-option-string
1647 (append org-e-latex-listings-options
1648 `(("language" ,lst-lang))
1649 (when label `(("label" ,label)))
1650 (when caption-str
1651 `(("caption" ,caption-str))))))
1652 (format "\\begin{lstlisting}\n%s\\end{lstlisting}" code)))))))
1655 ;;;; Statistics Cookie
1657 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
1658 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1659 CONTENTS is nil. INFO is a plist holding contextual information."
1660 (org-element-get-property :value statistics-cookie))
1663 ;;;; Subscript
1665 (defun org-e-latex-subscript (subscript contents info)
1666 "Transcode a SUBSCRIPT object from Org to LaTeX.
1667 CONTENTS is the contents of the object. INFO is a plist holding
1668 contextual information."
1669 (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
1672 ;;;; Superscript
1674 (defun org-e-latex-superscript (superscript contents info)
1675 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1676 CONTENTS is the contents of the object. INFO is a plist holding
1677 contextual information."
1678 (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
1681 ;;;; Table
1683 (defun org-e-latex-table--format-string (table table-info info)
1684 "Return an appropriate format string for TABLE.
1686 TABLE-INFO is the plist containing format info about the table,
1687 as returned by `org-export-table-format-info'. INFO is a plist
1688 used as a communication channel.
1690 The format string one placeholder for the body of the table."
1691 (let* ((label (org-element-get-property :name table))
1692 (caption (org-e-latex--caption/label-string
1693 (org-element-get-property :caption table) label info))
1694 (attr (mapconcat #'identity
1695 (org-element-get-property :attr_latex table)
1696 " "))
1697 ;; Determine alignment string.
1698 (alignment (org-e-latex-table--align-string attr table-info))
1699 ;; Determine environment for the table: longtable, tabular...
1700 (table-env (cond
1701 ((not attr) org-e-latex-default-table-environment)
1702 ((string-match "\\<longtable\\>" attr) "longtable")
1703 ((string-match "\\(tabular.\\)" attr)
1704 (org-match-string-no-properties 1 attr))
1705 (t org-e-latex-default-table-environment)))
1706 ;; If table is a float, determine environment: table or table*.
1707 (float-env (cond
1708 ((string= "longtable" table-env) nil)
1709 ((and attr
1710 (or (string-match (regexp-quote "table*") attr)
1711 (string-match "\\<multicolumn\\>" attr)))
1712 "table*")
1713 ((or (not (string= caption "")) label) "table")))
1714 ;; Extract others display options.
1715 (width (and attr
1716 (string-match "\\<width=\\(\\S-+\\)" attr)
1717 (org-match-string-no-properties 1 attr)))
1718 (placement (if (and attr
1719 (string-match "\\<placement=\\(\\S-+\\)" attr))
1720 (org-match-string-no-properties 1 attr)
1721 (concat "["
1722 org-e-latex-default-figure-position
1723 "]"))))
1724 ;; Prepare the final format string for the table.
1725 (cond
1726 ;; Longtable.
1727 ((string= "longtable" table-env)
1728 (format "\\begin{longtable}{%s}\n%s\n%%s\n%s\\end{longtable}"
1729 alignment
1730 (if (or (not org-e-latex-table-caption-above)
1731 (string= "" caption))
1733 (concat (org-trim caption) "\\\\"))
1734 (if (or org-e-latex-table-caption-above
1735 (string= "" caption))
1737 (concat (org-trim caption) "\\\\\n"))))
1738 ;; Others.
1739 (t (concat (when float-env
1740 (concat
1741 (format "\\begin{%s}%s\n" float-env placement)
1742 (if org-e-latex-table-caption-above caption "")))
1743 (when org-e-latex-tables-centered "\\begin{center}\n")
1744 (format "\\begin{%s}%s{%s}\n%%s\n\\end{%s}"
1745 table-env
1746 (if width (format "{%s}" width) "")
1747 alignment
1748 table-env)
1749 (when org-e-latex-tables-centered "\n\\end{center}")
1750 (when float-env
1751 (concat (if org-e-latex-table-caption-above "" caption)
1752 (format "\n\\end{%s}" float-env))))))))
1754 (defun org-e-latex-table--align-string (attr table-info)
1755 "Return an appropriate LaTeX alignment string.
1756 ATTR is a string containing table's LaTeX specific attributes.
1757 TABLE-INFO is the plist containing format info about the table,
1758 as returned by `org-export-table-format-info'."
1759 (or (and attr
1760 (string-match "\\<align=\\(\\S-+\\)" attr)
1761 (match-string 1 attr))
1762 (let* ((align (copy-sequence (plist-get table-info :alignment)))
1763 (colgroups (copy-sequence (plist-get table-info :column-groups)))
1764 (cols (length align))
1765 (separators (make-vector (1+ cols) "")))
1766 ;; Ignore the first column if it's special.
1767 (when (plist-get table-info :special-column-p)
1768 (aset align 0 "") (aset colgroups 0 nil))
1769 (let ((col 0))
1770 (mapc (lambda (el)
1771 (let ((gr (aref colgroups col)))
1772 (when (memq gr '(start start-end))
1773 (aset separators col "|"))
1774 (when (memq gr '(end start-end))
1775 (aset separators (1+ col) "|")))
1776 (incf col))
1777 align))
1778 ;; Build the LaTeX specific alignment string.
1779 (loop for al across align
1780 for sep across separators
1781 concat (concat sep al) into output
1782 finally return (concat output (aref separators cols))))))
1784 (defun org-e-latex-table (table contents info)
1785 "Transcode a TABLE element from Org to LaTeX.
1786 CONTENTS is nil. INFO is a plist holding contextual information."
1787 (let ((attr (mapconcat #'identity
1788 (org-element-get-property :attr_latex table)
1789 " "))
1790 (raw-table (org-element-get-property :raw-table table)))
1791 (cond
1792 ;; Case 1: verbatim table.
1793 ((or org-e-latex-tables-verbatim
1794 (and attr (string-match "\\<verbatim\\>" attr)))
1795 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
1796 (org-export-clean-table
1797 raw-table
1798 (plist-get (org-export-table-format-info raw-table)
1799 :special-column-p))))
1800 ;; Case 2: table.el table. Convert it using appropriate tools.
1801 ((eq (org-element-get-property :type table) 'table.el)
1802 (require 'table)
1803 ;; Ensure "*org-export-table*" buffer is empty.
1804 (and (get-buffer "*org-export-table*")
1805 (kill-buffer (get-buffer "*org-export-table*")))
1806 (let ((output (with-temp-buffer
1807 (insert raw-table)
1808 (goto-char 1)
1809 (re-search-forward "^[ \t]*|[^|]" nil t)
1810 (table-generate-source 'latex "*org-export-table*")
1811 (with-current-buffer "*org-export-table*"
1812 (org-trim (buffer-string))))))
1813 (kill-buffer (get-buffer "*org-export-table*"))
1814 ;; Remove left out comments.
1815 (while (string-match "^%.*\n" output)
1816 (setq output (replace-match "" t t output)))
1817 ;; When the "rmlines" attribute is provided, remove all hlines
1818 ;; but the the one separating heading from the table body.
1819 (when (and attr (string-match "\\<rmlines\\>" attr))
1820 (let ((n 0) (pos 0))
1821 (while (and (< (length output) pos)
1822 (setq pos (string-match "^\\\\hline\n?" output pos)))
1823 (incf n)
1824 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
1825 (if org-e-latex-tables-centered
1826 (format "\\begin{center}\n%s\n\\end{center}" output)
1827 output)))
1828 ;; Case 3: Standard table.
1830 (let* ((table-info (org-export-table-format-info raw-table))
1831 (clean-table (org-export-clean-table
1832 raw-table (plist-get table-info :special-column-p)))
1833 (columns-number (length (plist-get table-info :alignment))))
1834 ;; Convert ROWS to send them to `orgtbl-to-latex'. In
1835 ;; particular, send each cell to
1836 ;; `org-element-parse-secondary-string' to expand any Org
1837 ;; object within. Eventually, flesh the format string out with
1838 ;; the table.
1839 (format (org-e-latex-table--format-string table table-info info)
1840 (orgtbl-to-latex
1841 (mapcar
1842 (lambda (row)
1843 (if (string-match org-table-hline-regexp row)
1844 'hline
1845 (mapcar
1846 (lambda (cell)
1847 (org-export-secondary-string
1848 (org-element-parse-secondary-string
1849 cell
1850 (cdr (assq 'table org-element-string-restrictions)))
1851 'e-latex info))
1852 (org-split-string row "[ \t]*|[ \t]*"))))
1853 (org-split-string clean-table "\n"))
1854 `(:tstart nil :tend nil
1855 ;; Longtable environment requires specific
1856 ;; header line end.
1857 :hlend ,(and attr
1858 (string-match "\\<longtable\\>" attr)
1859 (format "\\\\
1860 \\hline
1861 \\endhead
1862 \\hline\\multicolumn{%d}{r}{Continued on next page}\\\\
1863 \\endfoot
1864 \\endlastfoot"
1865 columns-number))))))))))
1868 ;;;; Target
1870 (defun org-e-latex-target (target text info)
1871 "Transcode a TARGET object from Org to LaTeX.
1872 TEXT is the text of the target. INFO is a plist holding
1873 contextual information."
1874 (format "\\label{%s}%s"
1875 (org-export-solidify-link-text
1876 (org-element-get-property :raw-value target))
1877 text))
1880 ;;;; Time-stamp
1882 (defun org-e-latex-time-stamp (time-stamp contents info)
1883 "Transcode a TIME-STAMP object from Org to LaTeX.
1884 CONTENTS is nil. INFO is a plist holding contextual
1885 information."
1886 (let ((value (org-element-get-property :value time-stamp))
1887 (type (org-element-get-property :type time-stamp))
1888 (appt-type (org-element-get-property :appt-type time-stamp)))
1889 (concat (cond ((eq appt-type 'scheduled)
1890 (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
1891 ((eq appt-type 'deadline)
1892 (format "\\textbf{\\textsc{%s}} " org-deadline-string))
1893 ((eq appt-type 'closed)
1894 (format "\\textbf{\\textsc{%s}} " org-closed-string)))
1895 (cond ((memq type '(active active-range))
1896 (format org-e-latex-active-timestamp-format value))
1897 ((memq type '(inactive inactive-range))
1898 (format org-e-latex-inactive-timestamp-format value))
1900 (format org-e-latex-diary-timestamp-format value))))))
1903 ;;;; Verbatim
1905 (defun org-e-latex-verbatim (verbatim contents info)
1906 "Transcode a VERBATIM object from Org to LaTeX.
1907 CONTENTS is nil. INFO is a plist used as a communication
1908 channel."
1909 (let ((fmt (cdr (assoc (org-element-get-property :marker verbatim)
1910 org-e-latex-emphasis-alist)))
1911 (value (org-element-get-property :value verbatim)))
1912 (cond
1913 ;; Handle the `verb' special case.
1914 ((eq 'verb fmt)
1915 (let ((separator (org-e-latex--find-verb-separator value)))
1916 (concat "\\verb" separator value separator)))
1917 ;; Handle the `protectedtexttt' special case.
1918 ((eq 'protectedtexttt fmt)
1919 (let ((start 0)
1920 (trans '(("\\" . "\\textbackslash{}")
1921 ("~" . "\\textasciitilde{}")
1922 ("^" . "\\textasciicircum{}")))
1923 (rtn "")
1924 char)
1925 (while (string-match "[\\{}$%&_#~^]" value)
1926 (setq char (match-string 0 value))
1927 (if (> (match-beginning 0) 0)
1928 (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
1929 (setq value (substring value (1+ (match-beginning 0))))
1930 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1931 rtn (concat rtn char)))
1932 (setq value (concat rtn value)
1933 fmt "\\texttt{%s}")
1934 (while (string-match "--" value)
1935 (setq value (replace-match "-{}-" t t value)))
1936 (format fmt value)))
1937 ;; Else use format string.
1938 (t (format fmt value)))))
1941 ;;;; Verse Block
1943 (defun org-e-latex-verse-block (verse-block contents info)
1944 "Transcode a VERSE-BLOCK element from Org to LaTeX.
1945 CONTENTS is nil. INFO is a plist holding contextual information."
1946 (org-e-latex--wrap-label
1947 verse-block
1948 ;; In a verse environment, add a line break to each newline
1949 ;; character and change each white space at beginning of a line
1950 ;; into a space of 1 em. Also change each blank line with
1951 ;; a vertical space of 1 em.
1952 (progn
1953 (setq contents (replace-regexp-in-string
1954 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1955 (replace-regexp-in-string
1956 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1957 (org-remove-indentation
1958 (org-export-secondary-string
1959 (org-element-get-property :value verse-block)
1960 'e-latex info)))))
1961 (while (string-match "^[ \t]+" contents)
1962 (let ((new-str (format "\\hspace*{%dem}"
1963 (length (match-string 0 contents)))))
1964 (setq contents (replace-match new-str nil t contents))))
1965 (format "\\begin{verse}\n%s\\end{verse}" contents))))
1969 ;;; Interactive functions
1971 (defun org-e-latex-export-to-latex
1972 (&optional subtreep visible-only body-only ext-plist pub-dir)
1973 "Export current buffer to a LaTeX file.
1975 If narrowing is active in the current buffer, only export its
1976 narrowed part.
1978 If a region is active, export that region.
1980 When optional argument SUBTREEP is non-nil, export the sub-tree
1981 at point, extracting information from the headline properties
1982 first.
1984 When optional argument VISIBLE-ONLY is non-nil, don't export
1985 contents of hidden elements.
1987 When optional argument BODY-ONLY is non-nil, only write code
1988 between \"\\begin{document}\" and \"\\end{document}\".
1990 EXT-PLIST, when provided, is a property list with external
1991 parameters overriding Org default settings, but still inferior to
1992 file-local settings.
1994 When optional argument PUB-DIR is set, use it as the publishing
1995 directory.
1997 Return output file's name."
1998 (interactive)
1999 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
2000 (org-export-to-file
2001 'e-latex outfile subtreep visible-only body-only ext-plist)))
2003 (defun org-e-latex-export-to-pdf
2004 (&optional subtreep visible-only body-only ext-plist pub-dir)
2005 "Export current buffer to LaTeX then process through to PDF.
2007 If narrowing is active in the current buffer, only export its
2008 narrowed part.
2010 If a region is active, export that region.
2012 When optional argument SUBTREEP is non-nil, export the sub-tree
2013 at point, extracting information from the headline properties
2014 first.
2016 When optional argument VISIBLE-ONLY is non-nil, don't export
2017 contents of hidden elements.
2019 When optional argument BODY-ONLY is non-nil, only write code
2020 between \"\\begin{document}\" and \"\\end{document}\".
2022 EXT-PLIST, when provided, is a property list with external
2023 parameters overriding Org default settings, but still inferior to
2024 file-local settings.
2026 When optional argument PUB-DIR is set, use it as the publishing
2027 directory.
2029 Return PDF file's name."
2030 (interactive)
2031 (org-e-latex-compile
2032 (org-e-latex-export-to-latex
2033 subtreep visible-only body-only ext-plist pub-dir)))
2035 (defun org-e-latex-compile (texfile)
2036 "Compile a TeX file.
2038 TEXFILE is the name of the file being compiled. Processing is
2039 done through the command specified in `org-e-latex-pdf-process'.
2041 Return PDF file name or an error if it couldn't be produced."
2042 (let* ((wconfig (current-window-configuration))
2043 (texfile (file-truename texfile))
2044 (base (file-name-sans-extension texfile))
2045 errors)
2046 (message (format "Processing LaTeX file %s ..." texfile))
2047 (unwind-protect
2048 (progn
2049 (cond
2050 ;; A function is provided: Apply it.
2051 ((functionp org-latex-to-pdf-process)
2052 (funcall org-latex-to-pdf-process (shell-quote-argument texfile)))
2053 ;; A list is provided: Replace %b, %f and %o with appropriate
2054 ;; values in each command before applying it. Output is
2055 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2056 ((consp org-e-latex-pdf-process)
2057 (let* ((out-dir (or (file-name-directory texfile) "./"))
2058 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2059 (mapc
2060 (lambda (command)
2061 (shell-command
2062 (replace-regexp-in-string
2063 "%b" (shell-quote-argument base)
2064 (replace-regexp-in-string
2065 "%f" (shell-quote-argument texfile)
2066 (replace-regexp-in-string
2067 "%o" (shell-quote-argument out-dir) command)))
2068 outbuf))
2069 org-e-latex-pdf-process)
2070 ;; Collect standard errors from output buffer.
2071 (setq errors (org-e-latex-collect-errors outbuf))))
2072 (t (error "No valid command to process to PDF")))
2073 (let ((pdffile (concat base ".pdf")))
2074 ;; Check for process failure. Provide collected errors if
2075 ;; possible.
2076 (if (not (file-exists-p pdffile))
2077 (error (concat (format "PDF file %s wasn't produced" pdffile)
2078 (when errors (concat ": " errors))))
2079 ;; Else remove log files, when specified, and signal end of
2080 ;; process to user, along with any error encountered.
2081 (when org-e-latex-remove-logfiles
2082 (dolist (ext org-e-latex-logfiles-extensions)
2083 (let ((file (concat base "." ext)))
2084 (when (file-exists-p file) (delete-file file)))))
2085 (message (concat "Process completed"
2086 (if (not errors) "."
2087 (concat " with errors: " errors)))))
2088 ;; Return output file name.
2089 pdffile))
2090 (set-window-configuration wconfig))))
2092 (defun org-e-latex-collect-errors (buffer)
2093 "Collect some kind of errors from \"pdflatex\" command output.
2095 BUFFER is the buffer containing output.
2097 Return collected error types as a string, or nil if there was
2098 none."
2099 (with-current-buffer buffer
2100 (save-excursion
2101 (goto-char (point-max))
2102 ;; Find final "pdflatex" run.
2103 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
2104 (let ((case-fold-search t)
2105 (errors ""))
2106 (when (save-excursion
2107 (re-search-forward "Reference.*?undefined" nil t))
2108 (setq errors (concat errors " [undefined reference]")))
2109 (when (save-excursion
2110 (re-search-forward "Citation.*?undefined" nil t))
2111 (setq errors (concat errors " [undefined citation]")))
2112 (when (save-excursion
2113 (re-search-forward "Undefined control sequence" nil t))
2114 (setq errors (concat errors " [undefined control sequence]")))
2115 (when (save-excursion
2116 (re-search-forward "^! LaTeX.*?Error" nil t))
2117 (setq errors (concat errors " [LaTeX error]")))
2118 (when (save-excursion
2119 (re-search-forward "^! Package.*?Error" nil t))
2120 (setq errors (concat errors " [package error]")))
2121 (and (org-string-nw-p errors) (org-trim errors)))))))
2124 (provide 'org-e-latex)
2125 ;;; org-e-latex.el ends here