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