* add missing dependencies on org-version.inc
[org-mode.git] / EXPERIMENTAL / org-e-latex.el
blob2c6688c5ca0919f5b156082fc4a8b601469c33f4
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-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)
48 (defvar org-element-object-restrictions)
50 (declare-function org-export-clean-table "org-export" (table specialp))
51 (declare-function org-export-data "org-export" (data backend info))
52 (declare-function org-export-directory "org-export" (type plist))
53 (declare-function org-export-expand-macro "org-export" (macro info))
54 (declare-function org-export-first-sibling-p "org-export" (headline info))
55 (declare-function org-export-footnote-first-reference-p "org-export"
56 (footnote-reference info))
57 (declare-function org-export-format-code "org-export"
58 (code fun &optional num-lines ref-alist))
59 (declare-function org-export-format-code-default "org-export" (element info))
60 (declare-function org-export-get-coderef-format "org-export" (path desc))
61 (declare-function org-export-get-footnote-definition "org-export"
62 (footnote-reference info))
63 (declare-function org-export-get-footnote-number "org-export" (footnote info))
64 (declare-function org-export-get-previous-element "org-export" (blob info))
65 (declare-function org-export-get-relative-level "org-export" (headline info))
66 (declare-function org-export-unravel-code "org-export" (element))
67 (declare-function org-export-included-file "org-export" (keyword backend info))
68 (declare-function org-export-inline-image-p "org-export"
69 (link &optional extensions))
70 (declare-function org-export-last-sibling-p "org-export" (headline info))
71 (declare-function org-export-low-level-p "org-export" (headline info))
72 (declare-function org-export-output-file-name
73 "org-export" (extension &optional subtreep pub-dir))
74 (declare-function org-export-resolve-coderef "org-export" (ref info))
75 (declare-function org-export-resolve-fuzzy-link "org-export" (link info))
76 (declare-function org-export-secondary-string "org-export"
77 (secondary backend info))
78 (declare-function org-export-solidify-link-text "org-export" (s))
79 (declare-function org-export-table-format-info "org-export" (table))
80 (declare-function
81 org-export-to-buffer "org-export"
82 (backend buffer &optional subtreep visible-only body-only ext-plist))
83 (declare-function
84 org-export-to-file "org-export"
85 (backend file &optional subtreep visible-only body-only ext-plist))
89 ;;; Internal Variables
91 (defconst org-e-latex-option-alist
92 '((:date "DATE" nil org-e-latex-date-format t)
93 (:latex-class "LATEX_CLASS" nil org-e-latex-default-class t)
94 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
95 (:latex-header-extra "LATEX_HEADER" nil nil newline))
96 "Alist between LaTeX export properties and ways to set them.
97 See `org-export-option-alist' for more information on the
98 structure of the value.")
102 ;;; User Configurable Variables
104 (defgroup org-export-e-latex nil
105 "Options for exporting Org mode files to LaTeX."
106 :tag "Org Export LaTeX"
107 :group 'org-export)
110 ;;;; Preamble
112 (defcustom org-e-latex-default-class "article"
113 "The default LaTeX class."
114 :group 'org-export-e-latex
115 :type '(string :tag "LaTeX class"))
117 (defcustom org-e-latex-classes
118 '(("article"
119 "\\documentclass[11pt]{article}"
120 ("\\section{%s}" . "\\section*{%s}")
121 ("\\subsection{%s}" . "\\subsection*{%s}")
122 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
123 ("\\paragraph{%s}" . "\\paragraph*{%s}")
124 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
125 ("report"
126 "\\documentclass[11pt]{report}"
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 ("book"
133 "\\documentclass[11pt]{book}"
134 ("\\part{%s}" . "\\part*{%s}")
135 ("\\chapter{%s}" . "\\chapter*{%s}")
136 ("\\section{%s}" . "\\section*{%s}")
137 ("\\subsection{%s}" . "\\subsection*{%s}")
138 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
139 "Alist of LaTeX classes and associated header and structure.
140 If #+LaTeX_CLASS is set in the buffer, use its value and the
141 associated information. Here is the structure of each cell:
143 \(class-name
144 header-string
145 \(numbered-section . unnumbered-section\)
146 ...\)
148 The header string
149 -----------------
151 The HEADER-STRING is the header that will be inserted into the
152 LaTeX file. It should contain the \\documentclass macro, and
153 anything else that is needed for this setup. To this header, the
154 following commands will be added:
156 - Calls to \\usepackage for all packages mentioned in the
157 variables `org-export-latex-default-packages-alist' and
158 `org-export-latex-packages-alist'. Thus, your header
159 definitions should avoid to also request these packages.
161 - Lines specified via \"#+LaTeX_HEADER:\"
163 If you need more control about the sequence in which the header
164 is built up, or if you want to exclude one of these building
165 blocks for a particular class, you can use the following
166 macro-like placeholders.
168 [DEFAULT-PACKAGES] \\usepackage statements for default packages
169 [NO-DEFAULT-PACKAGES] do not include any of the default packages
170 [PACKAGES] \\usepackage statements for packages
171 [NO-PACKAGES] do not include the packages
172 [EXTRA] the stuff from #+LaTeX_HEADER
173 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
174 [BEAMER-HEADER-EXTRA] the beamer extra headers
176 So a header like
178 \\documentclass{article}
179 [NO-DEFAULT-PACKAGES]
180 [EXTRA]
181 \\providecommand{\\alert}[1]{\\textbf{#1}}
182 [PACKAGES]
184 will omit the default packages, and will include the
185 #+LaTeX_HEADER lines, then have a call to \\providecommand, and
186 then place \\usepackage commands based on the content of
187 `org-export-latex-packages-alist'.
189 If your header, `org-export-latex-default-packages-alist' or
190 `org-export-latex-packages-alist' inserts
191 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
192 replaced with a coding system derived from
193 `buffer-file-coding-system'. See also the variable
194 `org-e-latex-inputenc-alist' for a way to influence this
195 mechanism.
197 The sectioning structure
198 ------------------------
200 The sectioning structure of the class is given by the elements
201 following the header string. For each sectioning level, a number
202 of strings is specified. A %s formatter is mandatory in each
203 section string and will be replaced by the title of the section.
205 Instead of a cons cell \(numbered . unnumbered\), you can also
206 provide a list of 2 or 4 elements,
208 \(numbered-open numbered-close\)
212 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
214 providing opening and closing strings for a LaTeX environment
215 that should represent the document section. The opening clause
216 should have a %s to represent the section title.
218 Instead of a list of sectioning commands, you can also specify
219 a function name. That function will be called with two
220 parameters, the \(reduced) level of the headline, and a predicate
221 non-nil when the headline should be numbered. It must return
222 a format string in which the section title will be added."
223 :group 'org-export-e-latex
224 :type '(repeat
225 (list (string :tag "LaTeX class")
226 (string :tag "LaTeX header")
227 (repeat :tag "Levels" :inline t
228 (choice
229 (cons :tag "Heading"
230 (string :tag " numbered")
231 (string :tag "unnumbered"))
232 (list :tag "Environment"
233 (string :tag "Opening (numbered)")
234 (string :tag "Closing (numbered)")
235 (string :tag "Opening (unnumbered)")
236 (string :tag "Closing (unnumbered)"))
237 (function :tag "Hook computing sectioning"))))))
239 (defcustom org-e-latex-inputenc-alist nil
240 "Alist of inputenc coding system names, and what should really be used.
241 For example, adding an entry
243 (\"utf8\" . \"utf8x\")
245 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
246 are written as utf8 files."
247 :group 'org-export-e-latex
248 :type '(repeat
249 (cons
250 (string :tag "Derived from buffer")
251 (string :tag "Use this instead"))))
253 (defcustom org-e-latex-date-format
254 "\\today"
255 "Format string for \\date{...}."
256 :group 'org-export-e-latex
257 :type 'boolean)
259 (defcustom org-e-latex-title-command "\\maketitle"
260 "The command used to insert the title just after \\begin{document}.
261 If this string contains the formatting specification \"%s\" then
262 it will be used as a formatting string, passing the title as an
263 argument."
264 :group 'org-export-e-latex
265 :type 'string)
268 ;;;; Headline
270 (defcustom org-e-latex-format-headline-function nil
271 "Function to format headline text.
273 This function will be called with 5 arguments:
274 TODO the todo keyword \(string or nil\).
275 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
276 PRIORITY the priority of the headline \(integer or nil\)
277 TEXT the main headline text \(string\).
278 TAGS the tags string, separated with colons \(string or nil\).
280 The function result will be used in the section format string.
282 As an example, one could set the variable to the following, in
283 order to reproduce the default set-up:
285 \(defun org-e-latex-format-headline \(todo todo-type priority text tags\)
286 \"Default format function for an headline.\"
287 \(concat \(when todo
288 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
289 \(when priority
290 \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
291 text
292 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
293 :group 'org-export-e-latex
294 :type 'function)
297 ;;;; Emphasis
299 (defcustom org-e-latex-emphasis-alist
300 '(("*" . "\\textbf{%s}")
301 ("/" . "\\emph{%s}")
302 ("_" . "\\underline{%s}")
303 ("+" . "\\st{%s}")
304 ("=" . protectedtexttt)
305 ("~" . verb))
306 "Alist of LaTeX expressions to convert emphasis fontifiers.
308 The key is the character used as a marker for fontification. The
309 value is a formatting string to wrap fontified text with.
311 Value can also be set to the following symbols: `verb' and
312 `protectedtexttt'. For the former, Org will use \"\\verb\" to
313 create a format string and select a delimiter character that
314 isn't in the string. For the latter, Org will use \"\\texttt\"
315 to typeset and try to protect special characters."
316 :group 'org-export-e-latex
317 :type 'alist)
320 ;;;; Footnotes
322 (defcustom org-e-latex-footnote-separator "\\textsuperscript{,}\\,"
323 "Text used to separate footnotes."
324 :group 'org-export-e-latex
325 :type 'string)
328 ;;;; Time-stamps
330 (defcustom org-e-latex-active-timestamp-format "\\textit{%s}"
331 "A printf format string to be applied to active time-stamps."
332 :group 'org-export-e-latex
333 :type 'string)
335 (defcustom org-e-latex-inactive-timestamp-format "\\textit{%s}"
336 "A printf format string to be applied to inactive time-stamps."
337 :group 'org-export-e-latex
338 :type 'string)
340 (defcustom org-e-latex-diary-timestamp-format "\\textit{%s}"
341 "A printf format string to be applied to diary time-stamps."
342 :group 'org-export-e-latex
343 :type 'string)
346 ;;;; Links
348 (defcustom org-e-latex-image-default-option "width=.9\\linewidth"
349 "Default option for images."
350 :group 'org-export-e-latex
351 :type 'string)
353 (defcustom org-e-latex-default-figure-position "htb"
354 "Default position for latex figures."
355 :group 'org-export-e-latex
356 :type 'string)
358 (defcustom org-e-latex-inline-image-rules
359 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\)\\'"))
360 "Rules characterizing image files that can be inlined into LaTeX.
362 A rule consists in an association whose key is the type of link
363 to consider, and value is a regexp that will be matched against
364 link's path.
366 Note that, by default, the image extension *actually* allowed
367 depend on the way the LaTeX file is processed. When used with
368 pdflatex, pdf, jpg and png images are OK. When processing
369 through dvi to Postscript, only ps and eps are allowed. The
370 default we use here encompasses both."
371 :group 'org-export-e-latex
372 :type '(alist :key-type (string :tag "Type")
373 :value-type (regexp :tag "Path")))
376 ;;;; Tables
378 (defcustom org-e-latex-default-table-environment "tabular"
379 "Default environment used to build tables."
380 :group 'org-export-e-latex
381 :type 'string)
383 (defcustom org-e-latex-tables-centered t
384 "When non-nil, tables are exported in a center environment."
385 :group 'org-export-e-latex
386 :type 'boolean)
388 (defcustom org-e-latex-tables-verbatim nil
389 "When non-nil, tables are exported verbatim."
390 :group 'org-export-e-latex
391 :type 'boolean)
393 (defcustom org-e-latex-tables-booktabs nil
394 "When non-nil, display tables in a formal \"booktabs\" style.
395 This option assumes that the \"booktabs\" package is properly
396 loaded in the header of the document. This value can be ignored
397 locally with \"booktabs=yes\" and \"booktabs=no\" LaTeX
398 attributes."
399 :group 'org-export-e-latex
400 :type 'boolean)
402 (defcustom org-e-latex-table-caption-above t
403 "When non-nil, place caption string at the beginning of the table.
404 Otherwise, place it near the end."
405 :group 'org-export-e-latex
406 :type 'boolean)
409 ;;;; Drawers
411 (defcustom org-e-latex-format-drawer-function nil
412 "Function called to format a drawer in LaTeX code.
414 The function must accept two parameters:
415 NAME the drawer name, like \"LOGBOOK\"
416 CONTENTS the contents of the drawer.
418 The function should return the string to be exported.
420 For example, the variable could be set to the following function
421 in order to mimic default behaviour:
423 \(defun org-e-latex-format-drawer-default \(name contents\)
424 \"Format a drawer element for LaTeX export.\"
425 contents\)"
426 :group 'org-export-e-latex
427 :type 'function)
430 ;;;; Inlinetasks
432 (defcustom org-e-latex-format-inlinetask-function nil
433 "Function called to format an inlinetask in LaTeX code.
435 The function must accept six parameters:
436 TODO the todo keyword, as a string
437 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
438 PRIORITY the inlinetask priority, as a string
439 NAME the inlinetask name, as a string.
440 TAGS the inlinetask tags, as a string.
441 CONTENTS the contents of the inlinetask, as a string.
443 The function should return the string to be exported.
445 For example, the variable could be set to the following function
446 in order to mimic default behaviour:
448 \(defun org-e-latex-format-inlinetask \(todo type priority name tags contents\)
449 \"Format an inline task element for LaTeX export.\"
450 \(let \(\(full-title
451 \(concat
452 \(when todo
453 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
454 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
455 title
456 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
457 \(format \(concat \"\\\\begin{center}\\n\"
458 \"\\\\fbox{\\n\"
459 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
460 \"%s\\n\\n\"
461 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
462 \"%s\"
463 \"\\\\end{minipage}}\"
464 \"\\\\end{center}\"\)
465 full-title contents\)\)"
466 :group 'org-export-e-latex
467 :type 'function)
470 ;; Src blocks
472 (defcustom org-e-latex-listings nil
473 "Non-nil means export source code using the listings package.
474 This package will fontify source code, possibly even with color.
475 If you want to use this, you also need to make LaTeX use the
476 listings package, and if you want to have color, the color
477 package. Just add these to `org-export-latex-packages-alist',
478 for example using customize, or with something like:
480 \(require 'org-e-latex)
481 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"listings\"))
482 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"color\"))
484 Alternatively,
486 \(setq org-e-latex-listings 'minted)
488 causes source code to be exported using the minted package as
489 opposed to listings. If you want to use minted, you need to add
490 the minted package to `org-export-latex-packages-alist', for
491 example using customize, or with
493 \(require 'org-e-latex)
494 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"minted\"))
496 In addition, it is necessary to install pygments
497 \(http://pygments.org), and to configure the variable
498 `org-e-latex-pdf-process' so that the -shell-escape option is
499 passed to pdflatex."
500 :group 'org-export-e-latex
501 :type '(choice
502 (const :tag "Use listings" t)
503 (const :tag "Use minted" 'minted)
504 (const :tag "Export verbatim" nil)))
506 (defcustom org-e-latex-listings-langs
507 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
508 (c "C") (cc "C++")
509 (fortran "fortran")
510 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
511 (html "HTML") (xml "XML")
512 (tex "TeX") (latex "TeX")
513 (shell-script "bash")
514 (gnuplot "Gnuplot")
515 (ocaml "Caml") (caml "Caml")
516 (sql "SQL") (sqlite "sql"))
517 "Alist mapping languages to their listing language counterpart.
518 The key is a symbol, the major mode symbol without the \"-mode\".
519 The value is the string that should be inserted as the language
520 parameter for the listings package. If the mode name and the
521 listings name are the same, the language does not need an entry
522 in this list - but it does not hurt if it is present."
523 :group 'org-export-e-latex
524 :type '(repeat
525 (list
526 (symbol :tag "Major mode ")
527 (string :tag "Listings language"))))
529 (defcustom org-e-latex-listings-options nil
530 "Association list of options for the latex listings package.
532 These options are supplied as a comma-separated list to the
533 \\lstset command. Each element of the association list should be
534 a list containing two strings: the name of the option, and the
535 value. For example,
537 (setq org-e-latex-listings-options
538 '((\"basicstyle\" \"\\small\")
539 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
541 will typeset the code in a small size font with underlined, bold
542 black keywords.
544 Note that the same options will be applied to blocks of all
545 languages."
546 :group 'org-export-e-latex
547 :type '(repeat
548 (list
549 (string :tag "Listings option name ")
550 (string :tag "Listings option value"))))
552 (defcustom org-e-latex-minted-langs
553 '((emacs-lisp "common-lisp")
554 (cc "c++")
555 (cperl "perl")
556 (shell-script "bash")
557 (caml "ocaml"))
558 "Alist mapping languages to their minted language counterpart.
559 The key is a symbol, the major mode symbol without the \"-mode\".
560 The value is the string that should be inserted as the language
561 parameter for the minted package. If the mode name and the
562 listings name are the same, the language does not need an entry
563 in this list - but it does not hurt if it is present.
565 Note that minted uses all lower case for language identifiers,
566 and that the full list of language identifiers can be obtained
567 with:
569 pygmentize -L lexers"
570 :group 'org-export-e-latex
571 :type '(repeat
572 (list
573 (symbol :tag "Major mode ")
574 (string :tag "Minted language"))))
576 (defcustom org-e-latex-minted-options nil
577 "Association list of options for the latex minted package.
579 These options are supplied within square brackets in
580 \\begin{minted} environments. Each element of the alist should
581 be a list containing two strings: the name of the option, and the
582 value. For example,
584 \(setq org-e-latex-minted-options
585 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
587 will result in src blocks being exported with
589 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
591 as the start of the minted environment. Note that the same
592 options will be applied to blocks of all languages."
593 :group 'org-export-e-latex
594 :type '(repeat
595 (list
596 (string :tag "Minted option name ")
597 (string :tag "Minted option value"))))
599 (defvar org-e-latex-custom-lang-environments nil
600 "Alist mapping languages to language-specific LaTeX environments.
602 It is used during export of src blocks by the listings and minted
603 latex packages. For example,
605 \(setq org-e-latex-custom-lang-environments
606 '\(\(python \"pythoncode\"\)\)\)
608 would have the effect that if org encounters begin_src python
609 during latex export it will output
611 \\begin{pythoncode}
612 <src block body>
613 \\end{pythoncode}")
616 ;;;; Plain text
618 (defcustom org-e-latex-quotes
619 '(("fr"
620 ("\\(\\s-\\|[[(]\\|^\\)\"" . "«~")
621 ("\\(\\S-\\)\"" . "~»")
622 ("\\(\\s-\\|(\\|^\\)'" . "'"))
623 ("en"
624 ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
625 ("\\(\\S-\\)\"" . "''")
626 ("\\(\\s-\\|(\\|^\\)'" . "`")))
627 "Alist for quotes to use when converting english double-quotes.
629 The CAR of each item in this alist is the language code.
630 The CDR of each item in this alist is a list of three CONS:
631 - the first CONS defines the opening quote;
632 - the second CONS defines the closing quote;
633 - the last CONS defines single quotes.
635 For each item in a CONS, the first string is a regexp
636 for allowed characters before/after the quote, the second
637 string defines the replacement string for this quote."
638 :group 'org-export-e-latex
639 :type '(list
640 (cons :tag "Opening quote"
641 (string :tag "Regexp for char before")
642 (string :tag "Replacement quote "))
643 (cons :tag "Closing quote"
644 (string :tag "Regexp for char after ")
645 (string :tag "Replacement quote "))
646 (cons :tag "Single quote"
647 (string :tag "Regexp for char before")
648 (string :tag "Replacement quote "))))
651 ;;;; Compilation
653 (defcustom org-e-latex-pdf-process
654 '("pdflatex -interaction nonstopmode -output-directory %o %f"
655 "pdflatex -interaction nonstopmode -output-directory %o %f"
656 "pdflatex -interaction nonstopmode -output-directory %o %f")
657 "Commands to process a LaTeX file to a PDF file.
658 This is a list of strings, each of them will be given to the
659 shell as a command. %f in the command will be replaced by the
660 full file name, %b by the file base name \(i.e. without
661 extension) and %o by the base directory of the file.
663 The reason why this is a list is that it usually takes several
664 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
665 does not have a clever mechanism to detect which of these
666 commands have to be run to get to a stable result, and it also
667 does not do any error checking.
669 By default, Org uses 3 runs of `pdflatex' to do the processing.
670 If you have texi2dvi on your system and if that does not cause
671 the infamous egrep/locale bug:
673 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
675 then `texi2dvi' is the superior choice. Org does offer it as one
676 of the customize options.
678 Alternatively, this may be a Lisp function that does the
679 processing, so you could use this to apply the machinery of
680 AUCTeX or the Emacs LaTeX mode. This function should accept the
681 file name as its single argument."
682 :group 'org-export-pdf
683 :type '(choice
684 (repeat :tag "Shell command sequence"
685 (string :tag "Shell command"))
686 (const :tag "2 runs of pdflatex"
687 ("pdflatex -interaction nonstopmode -output-directory %o %f"
688 "pdflatex -interaction nonstopmode -output-directory %o %f"))
689 (const :tag "3 runs of pdflatex"
690 ("pdflatex -interaction nonstopmode -output-directory %o %f"
691 "pdflatex -interaction nonstopmode -output-directory %o %f"
692 "pdflatex -interaction nonstopmode -output-directory %o %f"))
693 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
694 ("pdflatex -interaction nonstopmode -output-directory %o %f"
695 "bibtex %b"
696 "pdflatex -interaction nonstopmode -output-directory %o %f"
697 "pdflatex -interaction nonstopmode -output-directory %o %f"))
698 (const :tag "texi2dvi"
699 ("texi2dvi -p -b -c -V %f"))
700 (const :tag "rubber"
701 ("rubber -d --into %o %f"))
702 (function)))
704 (defcustom org-e-latex-logfiles-extensions
705 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
706 "The list of file extensions to consider as LaTeX logfiles."
707 :group 'org-export-e-latex
708 :type '(repeat (string :tag "Extension")))
710 (defcustom org-e-latex-remove-logfiles t
711 "Non-nil means remove the logfiles produced by PDF production.
712 These are the .aux, .log, .out, and .toc files."
713 :group 'org-export-e-latex
714 :type 'boolean)
718 ;;; Internal Functions
720 (defun org-e-latex--caption/label-string (caption label info)
721 "Return caption and label LaTeX string for floats.
723 CAPTION is a cons cell of secondary strings, the car being the
724 standard caption and the cdr its short form. LABEL is a string
725 representing the label. INFO is a plist holding contextual
726 information.
728 If there's no caption nor label, return the empty string.
730 For non-floats, see `org-e-latex--wrap-label'."
731 (let ((label-str (if label (format "\\label{%s}" label) "")))
732 (cond
733 ((and (not caption) (not label)) "")
734 ((not caption) (format "\\label{%s}\n" label))
735 ;; Option caption format with short name.
736 ((cdr caption)
737 (format "\\caption[%s]{%s%s}\n"
738 (org-export-secondary-string (cdr caption) 'e-latex info)
739 label-str
740 (org-export-secondary-string (car caption) 'e-latex info)))
741 ;; Standard caption format.
742 (t (format "\\caption{%s%s}\n"
743 label-str
744 (org-export-secondary-string (car caption) 'e-latex info))))))
746 (defun org-e-latex--guess-inputenc (header)
747 "Set the coding system in inputenc to what the buffer is.
749 HEADER is the LaTeX header string.
751 Return the new header."
752 (let* ((cs (or (ignore-errors
753 (latexenc-coding-system-to-inputenc
754 buffer-file-coding-system))
755 "utf8")))
756 (if (not cs) header
757 ;; First translate if that is requested.
758 (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
759 ;; Then find the \usepackage statement and replace the option.
760 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
761 cs header t nil 1))))
763 (defun org-e-latex--find-verb-separator (s)
764 "Return a character not used in string S.
765 This is used to choose a separator for constructs like \\verb."
766 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
767 (loop for c across ll
768 when (not (string-match (regexp-quote (char-to-string c)) s))
769 return (char-to-string c))))
771 (defun org-e-latex--make-option-string (options)
772 "Return a comma separated string of keywords and values.
773 OPTIONS is an alist where the key is the options keyword as
774 a string, and the value a list containing the keyword value, or
775 nil."
776 (mapconcat (lambda (pair)
777 (concat (first pair)
778 (when (> (length (second pair)) 0)
779 (concat "=" (second pair)))))
780 options
781 ","))
783 (defun org-e-latex--quotation-marks (text info)
784 "Export quotation marks depending on language conventions.
785 TEXT is a string containing quotation marks to be replaced. INFO
786 is a plist used as a communication channel."
787 (mapc (lambda(l)
788 (let ((start 0))
789 (while (setq start (string-match (car l) text start))
790 (let ((new-quote (concat (match-string 1 text) (cdr l))))
791 (setq text (replace-match new-quote t t text))))))
792 (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
793 ;; Falls back on English.
794 (assoc "en" org-e-latex-quotes))))
795 text)
797 (defun org-e-latex--wrap-label (element output)
798 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
799 This function shouldn't be used for floats. See
800 `org-e-latex--caption/label-string'."
801 (let ((label (org-element-property :name element)))
802 (if (or (not output) (not label) (string= output "") (string= label ""))
803 output
804 (concat (format "\\label{%s}\n" label) output))))
808 ;;; Template
810 (defun org-e-latex-template (contents info)
811 "Return complete document string after LaTeX conversion.
812 CONTENTS is the transcoded contents string. INFO is a plist
813 holding export options."
814 (let ((title (org-export-secondary-string
815 (plist-get info :title) 'e-latex info)))
816 (concat
817 ;; 1. Time-stamp.
818 (and (plist-get info :time-stamp-file)
819 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
820 ;; 2. Document class and packages.
821 (let ((class (plist-get info :latex-class))
822 (class-options (plist-get info :latex-class-options)))
823 (org-element-normalize-string
824 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
825 (document-class-string
826 (and (stringp header)
827 (if class-options
828 (replace-regexp-in-string
829 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
830 class-options header t nil 1)
831 header))))
832 (org-e-latex--guess-inputenc
833 (org-splice-latex-header
834 document-class-string
835 org-export-latex-default-packages-alist ; defined in org.el
836 org-export-latex-packages-alist nil ; defined in org.el
837 (plist-get info :latex-header-extra))))))
838 ;; 3. Define alert if not yet defined.
839 "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
840 ;; 4. Possibly limit depth for headline numbering.
841 (let ((sec-num (plist-get info :section-numbers)))
842 (when (integerp sec-num)
843 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
844 ;; 5. Author.
845 (let ((author (and (plist-get info :with-author)
846 (let ((auth (plist-get info :author)))
847 (and auth (org-export-secondary-string
848 auth 'e-latex info)))))
849 (email (and (plist-get info :with-email)
850 (org-export-secondary-string
851 (plist-get info :email) 'e-latex info))))
852 (cond ((and author email (not (string= "" email)))
853 (format "\\author{%s\\thanks{%s}}\n" author email))
854 (author (format "\\author{%s}\n" author))
855 (t "\\author{}\n")))
856 ;; 6. Date.
857 (let ((date (plist-get info :date)))
858 (and date (format "\\date{%s}\n" date)))
859 ;; 7. Title
860 (format "\\title{%s}\n" title)
861 ;; 8. Hyperref options.
862 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
863 (or (plist-get info :keywords) "")
864 (or (plist-get info :description) "")
865 (if (not (plist-get info :with-creator)) ""
866 (plist-get info :creator)))
867 ;; 9. Document start.
868 "\\begin{document}\n\n"
869 ;; 10. Title command.
870 (org-element-normalize-string
871 (cond ((string= "" title) nil)
872 ((not (stringp org-e-latex-title-command)) nil)
873 ((string-match "\\(?:[^%]\\|^\\)%s"
874 org-e-latex-title-command)
875 (format org-e-latex-title-command title))
876 (t org-e-latex-title-command)))
877 ;; 11. Table of contents.
878 (let ((depth (plist-get info :with-toc)))
879 (when depth
880 (concat (when (wholenump depth)
881 (format "\\setcounter{tocdepth}{%d}\n" depth))
882 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
883 ;; 12. Document's body.
884 contents
885 ;; 13. Creator.
886 (let ((creator-info (plist-get info :with-creator)))
887 (cond
888 ((not creator-info) "")
889 ((eq creator-info 'comment)
890 (format "%% %s\n" (plist-get info :creator)))
891 (t (concat (plist-get info :creator) "\n"))))
892 ;; 14. Document end.
893 "\\end{document}")))
897 ;;; Transcode Functions
899 ;;;; Block
901 (defun org-e-latex-center-block (center-block contents info)
902 "Transcode a CENTER-BLOCK element from Org to LaTeX.
903 CONTENTS holds the contents of the block. INFO is a plist
904 holding contextual information."
905 (org-e-latex--wrap-label
906 center-block
907 (format "\\begin{center}\n%s\\end{center}" contents)))
910 ;;;; Comment
912 ;; Comments are ignored.
915 ;;;; Comment Block
917 ;; Comment Blocks are ignored.
920 ;;;; Drawer
922 (defun org-e-latex-drawer (drawer contents info)
923 "Transcode a DRAWER element from Org to LaTeX.
924 CONTENTS holds the contents of the block. INFO is a plist
925 holding contextual information."
926 (let* ((name (org-element-property :drawer-name drawer))
927 (output (if (functionp org-e-latex-format-drawer-function)
928 (funcall org-e-latex-format-drawer-function
929 name contents)
930 ;; If there's no user defined function: simply
931 ;; display contents of the drawer.
932 contents)))
933 (org-e-latex--wrap-label drawer output)))
936 ;;;; Dynamic Block
938 (defun org-e-latex-dynamic-block (dynamic-block contents info)
939 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
940 CONTENTS holds the contents of the block. INFO is a plist
941 holding contextual information. See
942 `org-export-data'."
943 (org-e-latex--wrap-label dynamic-block contents))
946 ;;;; Emphasis
948 (defun org-e-latex-emphasis (emphasis contents info)
949 "Transcode EMPHASIS from Org to LaTeX.
950 CONTENTS is the contents of the emphasized text. INFO is a plist
951 holding contextual information.."
952 (format (cdr (assoc (org-element-property :marker emphasis)
953 org-e-latex-emphasis-alist))
954 contents))
957 ;;;; Entity
959 (defun org-e-latex-entity (entity contents info)
960 "Transcode an ENTITY object from Org to LaTeX.
961 CONTENTS are the definition itself. INFO is a plist holding
962 contextual information."
963 (let ((ent (org-element-property :latex entity)))
964 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
967 ;;;; Example Block
969 (defun org-e-latex-example-block (example-block contents info)
970 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
971 CONTENTS is nil. INFO is a plist holding contextual
972 information."
973 (org-e-latex--wrap-label
974 example-block
975 (format "\\begin{verbatim}\n%s\\end{verbatim}"
976 (org-export-format-code-default example-block info))))
979 ;;;; Export Snippet
981 (defun org-e-latex-export-snippet (export-snippet contents info)
982 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
983 CONTENTS is nil. INFO is a plist holding contextual information."
984 (when (eq (org-export-snippet-backend export-snippet) 'e-latex)
985 (org-element-property :value export-snippet)))
988 ;;;; Export Block
990 (defun org-e-latex-export-block (export-block contents info)
991 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
992 CONTENTS is nil. INFO is a plist holding contextual information."
993 (when (string= (org-element-property :type export-block) "latex")
994 (org-remove-indentation (org-element-property :value export-block))))
997 ;;;; Fixed Width
999 (defun org-e-latex-fixed-width (fixed-width contents info)
1000 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1001 CONTENTS is nil. INFO is a plist holding contextual information."
1002 (let* ((value (org-element-normalize-string
1003 (replace-regexp-in-string
1004 "^[ \t]*: ?" ""
1005 (org-element-property :value fixed-width)))))
1006 (org-e-latex--wrap-label
1007 fixed-width (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1010 ;;;; Footnote Definition
1012 ;; Footnote Definitions are ignored.
1015 ;;;; Footnote Reference
1017 (defun org-e-latex-footnote-reference (footnote-reference contents info)
1018 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1019 CONTENTS is nil. INFO is a plist holding contextual information."
1020 (concat
1021 ;; Insert separator between two footnotes in a row.
1022 (let ((prev (org-export-get-previous-element footnote-reference info)))
1023 (when (eq (org-element-type prev) 'footnote-reference)
1024 org-e-latex-footnote-separator))
1025 (cond
1026 ;; Use \footnotemark if the footnote has already been defined.
1027 ((not (org-export-footnote-first-reference-p footnote-reference info))
1028 (format "\\footnotemark[%s]{}"
1029 (org-export-get-footnote-number footnote-reference info)))
1030 ;; Use also \footnotemark if reference is within another footnote
1031 ;; reference or footnote definition.
1032 ((loop for parent in (org-export-get-genealogy footnote-reference info)
1033 thereis (memq (org-element-type parent)
1034 '(footnote-reference footnote-definition)))
1035 (let ((num (org-export-get-footnote-number footnote-reference info)))
1036 (format "\\footnotemark[%s]{}\\setcounter{footnote}{%s}" num num)))
1037 ;; Otherwise, define it with \footnote command.
1039 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1040 (unless (eq (org-element-type def) 'org-data)
1041 (setq def (cons 'org-data (cons nil def))))
1042 (concat
1043 (format "\\footnote{%s}" (org-trim (org-export-data def 'e-latex info)))
1044 ;; Retrieve all footnote references within the footnote and
1045 ;; add their definition after it, since LaTeX doesn't support
1046 ;; them inside.
1047 (let* (all-refs
1048 search-refs ; for byte-compiler
1049 (search-refs
1050 (function
1051 (lambda (data)
1052 ;; Return a list of all footnote references in DATA.
1053 (org-element-map
1054 data 'footnote-reference
1055 (lambda (ref)
1056 (when (org-export-footnote-first-reference-p ref info)
1057 (push ref all-refs)
1058 (when (eq (org-element-property :type ref) 'standard)
1059 (funcall
1060 search-refs
1061 (org-export-get-footnote-definition ref info)))))
1062 info) (reverse all-refs)))))
1063 (mapconcat
1064 (lambda (ref)
1065 (format
1066 "\\footnotetext[%s]{%s}"
1067 (org-export-get-footnote-number ref info)
1068 (org-trim
1069 (funcall
1070 (if (eq (org-element-property :type ref) 'inline)
1071 'org-export-secondary-string
1072 'org-export-data)
1073 (org-export-get-footnote-definition ref info) 'e-latex info))))
1074 (funcall search-refs def) ""))))))))
1077 ;;;; Headline
1079 (defun org-e-latex-headline (headline contents info)
1080 "Transcode an HEADLINE element from Org to LaTeX.
1081 CONTENTS holds the contents of the headline. INFO is a plist
1082 holding contextual information."
1083 (let* ((class (plist-get info :latex-class))
1084 (level (org-export-get-relative-level headline info))
1085 (numberedp (org-export-numbered-headline-p headline info))
1086 (class-sectionning (assoc class org-e-latex-classes))
1087 ;; Section formatting will set two placeholders: one for the
1088 ;; title and the other for the contents.
1089 (section-fmt
1090 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
1091 (fboundp (nth 2 class-sectionning)))
1092 (funcall (nth 2 class-sectionning) level numberedp)
1093 (nth (1+ level) class-sectionning))))
1094 (cond
1095 ;; No section available for that LEVEL.
1096 ((not sec) nil)
1097 ;; Section format directly returned by a function.
1098 ((stringp sec) sec)
1099 ;; (numbered-section . unnumbered-section)
1100 ((not (consp (cdr sec)))
1101 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1102 ;; (numbered-open numbered-close)
1103 ((= (length sec) 2)
1104 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1105 ;; (num-in num-out no-num-in no-num-out)
1106 ((= (length sec) 4)
1107 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1108 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1109 (text (org-export-secondary-string
1110 (org-element-property :title headline) 'e-latex info))
1111 (todo
1112 (and (plist-get info :with-todo-keywords)
1113 (let ((todo (org-element-property :todo-keyword headline)))
1114 (and todo (org-export-secondary-string todo 'e-latex info)))))
1115 (todo-type (and todo (org-element-property :todo-type headline)))
1116 (tags (and (plist-get info :with-tags)
1117 (org-element-property :tags headline)))
1118 (priority (and (plist-get info :with-priority)
1119 (org-element-property :priority headline)))
1120 ;; Create the headline text.
1121 (full-text (if (functionp org-e-latex-format-headline-function)
1122 ;; User-defined formatting function.
1123 (funcall org-e-latex-format-headline-function
1124 todo todo-type priority text tags)
1125 ;; Default formatting.
1126 (concat
1127 (when todo
1128 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1129 (when priority (format "\\framebox{\\#%c} " priority))
1130 text
1131 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1132 ;; Associate some \label to the headline for internal links.
1133 (headline-label
1134 (format "\\label{sec-%s}\n"
1135 (mapconcat 'number-to-string
1136 (org-export-get-headline-number headline info)
1137 "-")))
1138 (pre-blanks
1139 (make-string (org-element-property :pre-blank headline) 10)))
1140 (cond
1141 ;; Case 1: This is a footnote section: ignore it.
1142 ((org-element-property :footnote-section-p headline) nil)
1143 ;; Case 2. This is a deep sub-tree: export it as a list item.
1144 ;; Also export as items headlines for which no section
1145 ;; format has been found.
1146 ((or (not section-fmt) (org-export-low-level-p headline info))
1147 ;; Build the real contents of the sub-tree.
1148 (let ((low-level-body
1149 (concat
1150 ;; If the headline is the first sibling, start a list.
1151 (when (org-export-first-sibling-p headline info)
1152 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1153 ;; Itemize headline
1154 "\\item " full-text "\n" headline-label pre-blanks contents)))
1155 ;; If headline is not the last sibling simply return
1156 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1157 ;; blank line.
1158 (if (not (org-export-last-sibling-p headline info)) low-level-body
1159 (replace-regexp-in-string
1160 "[ \t\n]*\\'"
1161 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1162 low-level-body))))
1163 ;; Case 3. Standard headline. Export it as a section.
1164 (t (format section-fmt full-text
1165 (concat headline-label pre-blanks contents))))))
1168 ;;;; Horizontal Rule
1170 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1171 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1172 CONTENTS is nil. INFO is a plist holding contextual information."
1173 (let ((attr (mapconcat #'identity
1174 (org-element-property :attr_latex horizontal-rule)
1175 " ")))
1176 (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
1179 ;;;; Inline Babel Call
1181 ;; Inline Babel Calls are ignored.
1184 ;;;; Inline Src Block
1186 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1187 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1188 CONTENTS holds the contents of the item. INFO is a plist holding
1189 contextual information."
1190 (let* ((code (org-element-property :value inline-src-block))
1191 (separator (org-e-latex--find-verb-separator code)))
1192 (cond
1193 ;; Do not use a special package: transcode it verbatim.
1194 ((not org-e-latex-listings)
1195 (concat "\\verb" separator code separator))
1196 ;; Use minted package.
1197 ((eq org-e-latex-listings 'minted)
1198 (let* ((org-lang (org-element-property :language inline-src-block))
1199 (mint-lang (or (cadr (assq (intern org-lang)
1200 org-e-latex-minted-langs))
1201 org-lang))
1202 (options (org-e-latex--make-option-string
1203 org-e-latex-minted-options)))
1204 (concat (format "\\mint%s{%s}"
1205 (if (string= options "") "" (format "[%s]" options))
1206 mint-lang)
1207 separator code separator)))
1208 ;; Use listings package.
1210 ;; Maybe translate language's name.
1211 (let* ((org-lang (org-element-property :language inline-src-block))
1212 (lst-lang (or (cadr (assq (intern org-lang)
1213 org-e-latex-listings-langs))
1214 org-lang))
1215 (options (org-e-latex--make-option-string
1216 (append org-e-latex-listings-options
1217 `(("language" ,lst-lang))))))
1218 (concat (format "\\lstinline[%s]" options)
1219 separator code separator))))))
1222 ;;;; Inlinetask
1224 (defun org-e-latex-inlinetask (inlinetask contents info)
1225 "Transcode an INLINETASK element from Org to LaTeX.
1226 CONTENTS holds the contents of the block. INFO is a plist
1227 holding contextual information."
1228 (let ((title (org-export-secondary-string
1229 (org-element-property :title inlinetask) 'e-latex info))
1230 (todo (and (plist-get info :with-todo-keywords)
1231 (let ((todo (org-element-property
1232 :todo-keyword inlinetask)))
1233 (and todo
1234 (org-export-secondary-string todo 'e-latex info)))))
1235 (todo-type (org-element-property :todo-type inlinetask))
1236 (tags (and (plist-get info :with-tags)
1237 (org-element-property :tags inlinetask)))
1238 (priority (and (plist-get info :with-priority)
1239 (org-element-property :priority inlinetask))))
1240 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1241 ;; with appropriate arguments.
1242 (if (functionp org-e-latex-format-inlinetask-function)
1243 (funcall org-e-latex-format-inlinetask-function
1244 todo todo-type priority title tags contents)
1245 ;; Otherwise, use a default template.
1246 (org-e-latex--wrap-label
1247 inlinetask
1248 (let ((full-title
1249 (concat
1250 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1251 (when priority (format "\\framebox{\\#%c} " priority))
1252 title
1253 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1254 (format (concat "\\begin{center}\n"
1255 "\\fbox{\n"
1256 "\\begin{minipage}[c]{.6\\textwidth}\n"
1257 "%s\n\n"
1258 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1259 "%s"
1260 "\\end{minipage}\n"
1261 "}\n"
1262 "\\end{center}")
1263 full-title contents))))))
1266 ;;;; Item
1268 (defun org-e-latex-item (item contents info)
1269 "Transcode an ITEM element from Org to LaTeX.
1270 CONTENTS holds the contents of the item. INFO is a plist holding
1271 contextual information."
1272 ;; Grab `:level' from plain-list properties, which is always the
1273 ;; first element above current item.
1274 (let* ((level (org-element-property :level (org-export-get-parent item info)))
1275 (counter (let ((count (org-element-property :counter item)))
1276 (and count
1277 (< level 4)
1278 (format "\\setcounter{enum%s}{%s}\n"
1279 (nth level '("i" "ii" "iii" "iv"))
1280 (1- count)))))
1281 (checkbox (let ((checkbox (org-element-property :checkbox item)))
1282 (cond ((eq checkbox 'on) "$\\boxtimes$ ")
1283 ((eq checkbox 'off) "$\\Box$ ")
1284 ((eq checkbox 'trans) "$\\boxminus$ "))))
1285 (tag (let ((tag (org-element-property :tag item)))
1286 (and tag
1287 (format "[%s]" (org-export-secondary-string
1288 tag 'e-latex info))))))
1289 (concat counter "\\item" tag " " checkbox contents)))
1292 ;;;; Keyword
1294 (defun org-e-latex-keyword (keyword contents info)
1295 "Transcode a KEYWORD element from Org to LaTeX.
1296 CONTENTS is nil. INFO is a plist holding contextual information."
1297 (let ((key (org-element-property :key keyword))
1298 (value (org-element-property :value keyword)))
1299 (cond
1300 ((string= key "LATEX") value)
1301 ((string= key "INDEX") (format "\\index{%s}" value))
1302 ;; Invisible targets.
1303 ((string= key "TARGET") nil)
1304 ((string= key "TOC")
1305 (let ((value (downcase value)))
1306 (cond
1307 ((string-match "\\<headlines\\>" value)
1308 (let ((depth (or (and (string-match "[0-9]+" value)
1309 (string-to-number (match-string 0 value)))
1310 (plist-get info :with-toc))))
1311 (concat
1312 (when (wholenump depth)
1313 (format "\\setcounter{tocdepth}{%s}\n" depth))
1314 "\\tableofcontents")))
1315 ((string= "tables" value) "\\listoftables")
1316 ((string= "figures" value) "\\listoffigures")
1317 ((string= "listings" value)
1318 (cond
1319 ((eq org-e-latex-listings 'minted) "\\listoflistings")
1320 (org-e-latex-listings "\\lstlistoflistings")
1321 ;; At the moment, src blocks with a caption are wrapped
1322 ;; into a figure environment.
1323 (t "\\listoffigures")))))))))
1326 ;;;; Latex Environment
1328 (defun org-e-latex-latex-environment (latex-environment contents info)
1329 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1330 CONTENTS is nil. INFO is a plist holding contextual information."
1331 (let ((label (org-element-property :name latex-environment))
1332 (value (org-remove-indentation
1333 (org-element-property :value latex-environment))))
1334 (if (not (org-string-nw-p label)) value
1335 ;; Environment is labelled: label must be within the environment
1336 ;; (otherwise, a reference pointing to that element will count
1337 ;; the section instead).
1338 (with-temp-buffer
1339 (insert value)
1340 (goto-char (point-min))
1341 (forward-line)
1342 (insert (format "\\label{%s}\n" label))
1343 (buffer-string)))))
1346 ;;;; Latex Fragment
1348 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1349 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1350 CONTENTS is nil. INFO is a plist holding contextual information."
1351 (org-element-property :value latex-fragment))
1354 ;;;; Line Break
1356 (defun org-e-latex-line-break (line-break contents info)
1357 "Transcode a LINE-BREAK object from Org to LaTeX.
1358 CONTENTS is nil. INFO is a plist holding contextual information."
1359 "\\\\")
1362 ;;;; Link
1364 (defun org-e-latex-link--inline-image (link info)
1365 "Return LaTeX code for an inline image.
1366 LINK is the link pointing to the inline image. INFO is a plist
1367 used as a communication channel."
1368 (let* ((parent (org-export-get-parent-paragraph link info))
1369 (path (let ((raw-path (org-element-property :path link)))
1370 (if (not (file-name-absolute-p raw-path)) raw-path
1371 (expand-file-name raw-path))))
1372 (caption (org-e-latex--caption/label-string
1373 (org-element-property :caption parent)
1374 (org-element-property :name parent)
1375 info))
1376 ;; Retrieve latex attributes from the element around.
1377 (attr (let ((raw-attr
1378 (mapconcat #'identity
1379 (org-element-property :attr_latex parent)
1380 " ")))
1381 (unless (string= raw-attr "") raw-attr)))
1382 (disposition
1383 (cond
1384 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1385 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1386 ((or (and attr (string-match "\\<float\\>" attr))
1387 (not (string= caption "")))
1388 'float)))
1389 (placement
1390 (cond
1391 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1392 (org-match-string-no-properties 1 attr))
1393 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1394 ((eq disposition 'float)
1395 (concat "[" org-e-latex-default-figure-position "]"))
1396 (t ""))))
1397 ;; Now clear ATTR from any special keyword and set a default
1398 ;; value if nothing is left.
1399 (setq attr
1400 (if (not attr) ""
1401 (org-trim
1402 (replace-regexp-in-string
1403 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
1404 (setq attr (cond ((not (string= attr "")) attr)
1405 ((eq disposition 'float) "width=0.7\\textwidth")
1406 ((eq disposition 'wrap) "width=0.48\\textwidth")
1407 (t (or org-e-latex-image-default-option ""))))
1408 ;; Return proper string, depending on DISPOSITION.
1409 (case disposition
1410 (wrap (format "\\begin{wrapfigure}%s
1411 \\centering
1412 \\includegraphics[%s]{%s}
1413 %s\\end{wrapfigure}" placement attr path caption))
1414 (mulicolumn (format "\\begin{figure*}%s
1415 \\centering
1416 \\includegraphics[%s]{%s}
1417 %s\\end{figure*}" placement attr path caption))
1418 (float (format "\\begin{figure}%s
1419 \\centering
1420 \\includegraphics[%s]{%s}
1421 %s\\end{figure}" placement attr path caption))
1422 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1424 (defun org-e-latex-link (link desc info)
1425 "Transcode a LINK object from Org to LaTeX.
1427 DESC is the description part of the link, or the empty string.
1428 INFO is a plist holding contextual information. See
1429 `org-export-data'."
1430 (let* ((type (org-element-property :type link))
1431 (raw-path (org-element-property :path link))
1432 ;; Ensure DESC really exists, or set it to nil.
1433 (desc (and (not (string= desc "")) desc))
1434 (imagep (org-export-inline-image-p
1435 link org-e-latex-inline-image-rules))
1436 (path (cond
1437 ((member type '("http" "https" "ftp" "mailto"))
1438 (concat type ":" raw-path))
1439 ((string= type "file")
1440 (when (string-match "\\(.+\\)::.+" raw-path)
1441 (setq raw-path (match-string 1 raw-path)))
1442 (if (file-name-absolute-p raw-path)
1443 (concat "file://" (expand-file-name raw-path))
1444 ;; TODO: Not implemented yet. Concat also:
1445 ;; (org-export-directory :LaTeX info)
1446 (concat "file://" raw-path)))
1447 (t raw-path)))
1448 protocol)
1449 (cond
1450 ;; Image file.
1451 (imagep (org-e-latex-link--inline-image link info))
1452 ;; Radioed target: Target's name is obtained from original raw
1453 ;; link. Path is parsed and transcoded in order to have a proper
1454 ;; display of the contents.
1455 ((string= type "radio")
1456 (format "\\hyperref[%s]{%s}"
1457 (org-export-solidify-link-text path)
1458 (org-export-secondary-string
1459 (org-element-parse-secondary-string
1460 path (cdr (assq 'radio-target org-element-object-restrictions)))
1461 'e-latex info)))
1462 ;; Links pointing to an headline: Find destination and build
1463 ;; appropriate referencing command.
1464 ((member type '("custom-id" "fuzzy" "id"))
1465 (let ((destination (if (string= type "fuzzy")
1466 (org-export-resolve-fuzzy-link link info)
1467 (org-export-resolve-id-link link info))))
1468 (case (org-element-type destination)
1469 ;; Fuzzy link points nowhere.
1470 ('nil
1471 (format "\\texttt{%s}"
1472 (or desc
1473 (org-export-secondary-string
1474 (org-element-property :raw-link link)
1475 'e-latex info))))
1476 ;; Fuzzy link points to an invisible target.
1477 (keyword nil)
1478 ;; LINK points to an headline. If headlines are numbered
1479 ;; and the link has no description, display headline's
1480 ;; number. Otherwise, display description or headline's
1481 ;; title.
1482 (headline
1483 (let ((label
1484 (format "sec-%s"
1485 (mapconcat
1486 'number-to-string
1487 (org-export-get-headline-number destination info)
1488 "-"))))
1489 (if (and (plist-get info :section-numbers) (not desc))
1490 (format "\\ref{%s}" label)
1491 (format "\\hyperref[%s]{%s}" label
1492 (or desc
1493 (org-export-secondary-string
1494 (org-element-property :title destination)
1495 'e-latex info))))))
1496 ;; Fuzzy link points to a target. Do as above.
1497 (otherwise
1498 (let ((path (org-export-solidify-link-text path)))
1499 (if (not desc) (format "\\ref{%s}" path)
1500 (format "\\hyperref[%s]{%s}" path desc)))))))
1501 ;; Coderef: replace link with the reference name or the
1502 ;; equivalent line number.
1503 ((string= type "coderef")
1504 (format (org-export-get-coderef-format path (or desc ""))
1505 (org-export-resolve-coderef path info)))
1506 ;; Link type is handled by a special function.
1507 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1508 (funcall protocol (org-link-unescape path) desc 'latex))
1509 ;; External link with a description part.
1510 ((and path desc) (format "\\href{%s}{%s}" path desc))
1511 ;; External link without a description part.
1512 (path (format "\\url{%s}" path))
1513 ;; No path, only description. Try to do something useful.
1514 (t (format "\\texttt{%s}" desc)))))
1517 ;;;; Babel Call
1519 ;; Babel Calls are ignored.
1522 ;;;; Macro
1524 (defun org-e-latex-macro (macro contents info)
1525 "Transcode a MACRO element from Org to LaTeX.
1526 CONTENTS is nil. INFO is a plist holding contextual information."
1527 ;; Use available tools.
1528 (org-export-expand-macro macro info))
1531 ;;;; Paragraph
1533 (defun org-e-latex-paragraph (paragraph contents info)
1534 "Transcode a PARAGRAPH element from Org to LaTeX.
1535 CONTENTS is the contents of the paragraph, as a string. INFO is
1536 the plist used as a communication channel."
1537 contents)
1540 ;;;; Plain List
1542 (defun org-e-latex-plain-list (plain-list contents info)
1543 "Transcode a PLAIN-LIST element from Org to LaTeX.
1544 CONTENTS is the contents of the list. INFO is a plist holding
1545 contextual information."
1546 (let* ((type (org-element-property :type plain-list))
1547 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1548 "inparadesc" "asparadesc"))
1549 (paralist-regexp (concat
1550 "\\("
1551 (mapconcat 'identity paralist-types "\\|")
1552 "\\)"))
1553 (attr (mapconcat #'identity
1554 (org-element-property :attr_latex plain-list)
1555 " "))
1556 (latex-type (cond
1557 ((and attr
1558 (string-match
1559 (format "\\<%s\\>" paralist-regexp) attr))
1560 (match-string 1 attr))
1561 ((eq type 'ordered) "enumerate")
1562 ((eq type 'unordered) "itemize")
1563 ((eq type 'descriptive) "description"))))
1564 (org-e-latex--wrap-label
1565 plain-list
1566 (format "\\begin{%s}%s\n%s\\end{%s}"
1567 latex-type
1568 ;; Once special environment, if any, has been removed, the
1569 ;; rest of the attributes will be optional arguments.
1570 ;; They will be put inside square brackets if necessary.
1571 (let ((opt (replace-regexp-in-string
1572 (format " *%s *" paralist-regexp) "" attr)))
1573 (cond ((string= opt "") "")
1574 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1575 (t (format "[%s]" opt))))
1576 contents
1577 latex-type))))
1580 ;;;; Plain Text
1582 (defun org-e-latex-plain-text (text info)
1583 "Transcode a TEXT string from Org to LaTeX.
1584 TEXT is the string to transcode. INFO is a plist holding
1585 contextual information."
1586 ;; Protect %, #, &, $, ~, ^, _, { and }.
1587 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1588 (setq text
1589 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1590 ;; Protect \
1591 (setq text (replace-regexp-in-string
1592 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1593 "$\\backslash$" text nil t 1))
1594 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1595 (let ((case-fold-search nil)
1596 (start 0))
1597 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1598 (setq text (replace-match
1599 (format "\\%s{}" (match-string 1 text)) nil t text)
1600 start (match-end 0))))
1601 ;; Handle quotation marks
1602 (setq text (org-e-latex--quotation-marks text info))
1603 ;; Convert special strings.
1604 (when (plist-get info :with-special-strings)
1605 (while (string-match (regexp-quote "...") text)
1606 (setq text (replace-match "\\ldots{}" nil t text))))
1607 ;; Handle break preservation if required.
1608 (when (plist-get info :preserve-breaks)
1609 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1610 text)))
1611 ;; Return value.
1612 text)
1615 ;;;; Property Drawer
1617 (defun org-e-latex-property-drawer (property-drawer contents info)
1618 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1619 CONTENTS is nil. INFO is a plist holding contextual
1620 information."
1621 ;; The property drawer isn't exported but we want separating blank
1622 ;; lines nonetheless.
1626 ;;;; Quote Block
1628 (defun org-e-latex-quote-block (quote-block contents info)
1629 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1630 CONTENTS holds the contents of the block. INFO is a plist
1631 holding contextual information."
1632 (org-e-latex--wrap-label
1633 quote-block
1634 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1637 ;;;; Quote Section
1639 (defun org-e-latex-quote-section (quote-section contents info)
1640 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1641 CONTENTS is nil. INFO is a plist holding contextual information."
1642 (let ((value (org-remove-indentation
1643 (org-element-property :value quote-section))))
1644 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1647 ;;;; Section
1649 (defun org-e-latex-section (section contents info)
1650 "Transcode a SECTION element from Org to LaTeX.
1651 CONTENTS holds the contents of the section. INFO is a plist
1652 holding contextual information."
1653 contents)
1656 ;;;; Radio Target
1658 (defun org-e-latex-radio-target (radio-target text info)
1659 "Transcode a RADIO-TARGET object from Org to LaTeX.
1660 TEXT is the text of the target. INFO is a plist holding
1661 contextual information."
1662 (format "\\label{%s}%s"
1663 (org-export-solidify-link-text
1664 (org-element-property :value radio-target))
1665 text))
1668 ;;;; Special Block
1670 (defun org-e-latex-special-block (special-block contents info)
1671 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1672 CONTENTS holds the contents of the block. INFO is a plist
1673 holding contextual information."
1674 (let ((type (downcase (org-element-property :type special-block))))
1675 (org-e-latex--wrap-label
1676 special-block
1677 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
1680 ;;;; Src Block
1682 (defun org-e-latex-src-block (src-block contents info)
1683 "Transcode a SRC-BLOCK element from Org to LaTeX.
1684 CONTENTS holds the contents of the item. INFO is a plist holding
1685 contextual information."
1686 (let* ((lang (org-element-property :language src-block))
1687 (caption (org-element-property :caption src-block))
1688 (label (org-element-property :name src-block))
1689 (custom-env (and lang
1690 (cadr (assq (intern lang)
1691 org-e-latex-custom-lang-environments))))
1692 (num-start (case (org-element-property :number-lines src-block)
1693 (continued (org-export-get-loc src-block info))
1694 (new 0)))
1695 (retain-labels (org-element-property :retain-labels src-block)))
1696 (cond
1697 ;; Case 1. No source fontification.
1698 ((not org-e-latex-listings)
1699 (let ((caption-str (org-e-latex--caption/label-string caption label info))
1700 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
1701 (format
1702 (or float-env "%s")
1703 (concat caption-str
1704 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1705 (org-export-format-code-default src-block info))))))
1706 ;; Case 2. Custom environment.
1707 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
1708 custom-env
1709 (org-export-format-code-default src-block info)
1710 custom-env))
1711 ;; Case 3. Use minted package.
1712 ((eq org-e-latex-listings 'minted)
1713 (let ((float-env (when (or label caption)
1714 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1715 (org-e-latex--caption/label-string
1716 caption label info))))
1717 (body
1718 (format
1719 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1720 ;; Options.
1721 (org-e-latex--make-option-string
1722 (if (not num-start) org-e-latex-minted-options
1723 (append `(("linenos")
1724 ("firstnumber" ,(number-to-string (1+ num-start))))
1725 org-e-latex-minted-options)))
1726 ;; Language.
1727 (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
1728 ;; Source code.
1729 (let* ((code-info (org-export-unravel-code src-block))
1730 (max-width
1731 (apply 'max
1732 (mapcar 'length
1733 (org-split-string (car code-info) "\n")))))
1734 (org-export-format-code
1735 (car code-info)
1736 (lambda (loc num ref)
1737 (concat
1739 (when ref
1740 ;; Ensure references are flushed to the right,
1741 ;; separated with 6 spaces from the widest line
1742 ;; of code.
1743 (concat (make-string (+ (- max-width (length loc)) 6) ? )
1744 (format "(%s)" ref)))))
1745 nil (and retain-labels (cdr code-info)))))))
1746 ;; Return value.
1747 (if float-env (format float-env body) body)))
1748 ;; Case 4. Use listings package.
1750 (let ((lst-lang
1751 (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
1752 (caption-str
1753 (when caption
1754 (let ((main (org-export-secondary-string
1755 (car caption) 'e-latex info)))
1756 (if (not (cdr caption)) (format "{%s}" main)
1757 (format
1758 "{[%s]%s}"
1759 (org-export-secondary-string (cdr caption) 'e-latex info)
1760 main))))))
1761 (concat
1762 ;; Options.
1763 (format "\\lstset{%s}\n"
1764 (org-e-latex--make-option-string
1765 (append org-e-latex-listings-options
1766 `(("language" ,lst-lang))
1767 (when label `(("label" ,label)))
1768 (when caption-str `(("caption" ,caption-str)))
1769 (cond ((not num-start) '(("numbers" "none")))
1770 ((zerop num-start) '(("numbers" "left")))
1771 (t `(("numbers" "left")
1772 ("firstnumber"
1773 ,(number-to-string (1+ num-start)))))))))
1774 ;; Source code.
1775 (format
1776 "\\begin{lstlisting}\n%s\\end{lstlisting}"
1777 (let* ((code-info (org-export-unravel-code src-block))
1778 (max-width
1779 (apply 'max
1780 (mapcar 'length
1781 (org-split-string (car code-info) "\n")))))
1782 (org-export-format-code
1783 (car code-info)
1784 (lambda (loc num ref)
1785 (concat
1787 (when ref
1788 ;; Ensure references are flushed to the right,
1789 ;; separated with 6 spaces from the widest line of
1790 ;; code
1791 (concat (make-string (+ (- max-width (length loc)) 6) ? )
1792 (format "(%s)" ref)))))
1793 nil (and retain-labels (cdr code-info)))))))))))
1796 ;;;; Statistics Cookie
1798 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
1799 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1800 CONTENTS is nil. INFO is a plist holding contextual information."
1801 (org-element-property :value statistics-cookie))
1804 ;;;; Subscript
1806 (defun org-e-latex-subscript (subscript contents info)
1807 "Transcode a SUBSCRIPT object from Org to LaTeX.
1808 CONTENTS is the contents of the object. INFO is a plist holding
1809 contextual information."
1810 (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
1813 ;;;; Superscript
1815 (defun org-e-latex-superscript (superscript contents info)
1816 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1817 CONTENTS is the contents of the object. INFO is a plist holding
1818 contextual information."
1819 (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
1822 ;;;; Table
1824 (defun org-e-latex-table--format-string (table table-info info)
1825 "Return an appropriate format string for TABLE.
1827 TABLE-INFO is the plist containing format info about the table,
1828 as returned by `org-export-table-format-info'. INFO is a plist
1829 used as a communication channel.
1831 The format string leaves one placeholder for the body of the
1832 table."
1833 (let* ((label (org-element-property :name table))
1834 (caption (org-e-latex--caption/label-string
1835 (org-element-property :caption table) label info))
1836 (attr (mapconcat 'identity
1837 (org-element-property :attr_latex table)
1838 " "))
1839 ;; Determine alignment string.
1840 (alignment (org-e-latex-table--align-string attr table-info))
1841 ;; Determine environment for the table: longtable, tabular...
1842 (table-env (cond
1843 ((not attr) org-e-latex-default-table-environment)
1844 ((string-match "\\<longtable\\>" attr) "longtable")
1845 ((string-match "\\<tabular.?\\>" attr)
1846 (org-match-string-no-properties 0 attr))
1847 (t org-e-latex-default-table-environment)))
1848 ;; If table is a float, determine environment: table, table*
1849 ;; or sidewaystable.
1850 (float-env (cond
1851 ((string= "longtable" table-env) nil)
1852 ((and attr (string-match "\\<sidewaystable\\>" attr))
1853 "sidewaystables")
1854 ((and attr
1855 (or (string-match (regexp-quote "table*") attr)
1856 (string-match "\\<multicolumn\\>" attr)))
1857 "table*")
1858 ((or (not (string= caption "")) label) "table")))
1859 ;; Extract others display options.
1860 (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
1861 (org-match-string-no-properties 1 attr)))
1862 (placement
1863 (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1864 (org-match-string-no-properties 1 attr)
1865 (format "[%s]" org-e-latex-default-figure-position))))
1866 ;; Prepare the final format string for the table.
1867 (cond
1868 ;; Longtable.
1869 ((string= "longtable" table-env)
1870 (format
1871 "\\begin{longtable}{%s}\n%s\n%%s\n%s\\end{longtable}"
1872 alignment
1873 (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
1874 (concat (org-trim caption) "\\\\"))
1875 (if (or org-e-latex-table-caption-above (string= "" caption)) ""
1876 (concat (org-trim caption) "\\\\\n"))))
1877 ;; Others.
1878 (t (concat (when float-env
1879 (concat
1880 (format "\\begin{%s}%s\n" float-env placement)
1881 (if org-e-latex-table-caption-above caption "")))
1882 (when org-e-latex-tables-centered "\\begin{center}\n")
1883 (format "\\begin{%s}%s{%s}\n%%s\n\\end{%s}"
1884 table-env
1885 (if width (format "{%s}" width) "") alignment table-env)
1886 (when org-e-latex-tables-centered "\n\\end{center}")
1887 (when float-env
1888 (concat (if org-e-latex-table-caption-above "" caption)
1889 (format "\n\\end{%s}" float-env))))))))
1891 (defun org-e-latex-table--align-string (attr table-info)
1892 "Return an appropriate LaTeX alignment string.
1893 ATTR is a string containing table's LaTeX specific attributes.
1894 TABLE-INFO is the plist containing format info about the table,
1895 as returned by `org-export-table-format-info'."
1896 (or (and attr
1897 (string-match "\\<align=\\(\\S-+\\)" attr)
1898 (match-string 1 attr))
1899 (let* ((align (copy-sequence (plist-get table-info :alignment)))
1900 (colgroups (copy-sequence (plist-get table-info :column-groups)))
1901 (cols (length align))
1902 (separators (make-vector (1+ cols) "")))
1903 ;; Ignore the first column if it's special.
1904 (when (plist-get table-info :special-column-p)
1905 (aset align 0 "") (aset colgroups 0 nil))
1906 (let ((col 0))
1907 (mapc (lambda (el)
1908 (let ((gr (aref colgroups col)))
1909 (when (memq gr '(start start-end))
1910 (aset separators col "|"))
1911 (when (memq gr '(end start-end))
1912 (aset separators (1+ col) "|")))
1913 (incf col))
1914 align))
1915 ;; Build the LaTeX specific alignment string.
1916 (loop for al across align
1917 for sep across separators
1918 concat (concat sep al) into output
1919 finally return (concat output (aref separators cols))))))
1921 (defun org-e-latex-table (table contents info)
1922 "Transcode a TABLE element from Org to LaTeX.
1923 CONTENTS is nil. INFO is a plist holding contextual information."
1924 (let ((attr (mapconcat #'identity
1925 (org-element-property :attr_latex table)
1926 " "))
1927 (raw-table (org-element-property :raw-table table)))
1928 (cond
1929 ;; Case 1: verbatim table.
1930 ((or org-e-latex-tables-verbatim
1931 (and attr (string-match "\\<verbatim\\>" attr)))
1932 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
1933 (org-export-clean-table
1934 raw-table
1935 (plist-get (org-export-table-format-info raw-table)
1936 :special-column-p))))
1937 ;; Case 2: table.el table. Convert it using appropriate tools.
1938 ((eq (org-element-property :type table) 'table.el)
1939 (require 'table)
1940 ;; Ensure "*org-export-table*" buffer is empty.
1941 (with-current-buffer (get-buffer-create "*org-export-table*")
1942 (erase-buffer))
1943 (let ((output (with-temp-buffer
1944 (insert raw-table)
1945 (goto-char 1)
1946 (re-search-forward "^[ \t]*|[^|]" nil t)
1947 (table-generate-source 'latex "*org-export-table*")
1948 (with-current-buffer "*org-export-table*"
1949 (org-trim (buffer-string))))))
1950 (kill-buffer (get-buffer "*org-export-table*"))
1951 ;; Remove left out comments.
1952 (while (string-match "^%.*\n" output)
1953 (setq output (replace-match "" t t output)))
1954 ;; When the "rmlines" attribute is provided, remove all hlines
1955 ;; but the the one separating heading from the table body.
1956 (when (and attr (string-match "\\<rmlines\\>" attr))
1957 (let ((n 0) (pos 0))
1958 (while (and (< (length output) pos)
1959 (setq pos (string-match "^\\\\hline\n?" output pos)))
1960 (incf n)
1961 (unless (= n 2)
1962 (setq output (replace-match "" nil nil output))))))
1963 (if (not org-e-latex-tables-centered) output
1964 (format "\\begin{center}\n%s\n\\end{center}" output))))
1965 ;; Case 3: Standard table.
1967 (let* ((table-info (org-export-table-format-info raw-table))
1968 (columns-number (length (plist-get table-info :alignment)))
1969 (longtablep (and attr (string-match "\\<longtable\\>" attr)))
1970 (booktabsp
1971 (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
1972 org-e-latex-tables-booktabs))
1973 ;; CLEAN-TABLE is a table turned into a list, much like
1974 ;; `org-table-to-lisp', with special column and
1975 ;; formatting cookies removed, and cells already
1976 ;; transcoded.
1977 (clean-table
1978 (mapcar
1979 (lambda (row)
1980 (if (string-match org-table-hline-regexp row) 'hline
1981 (mapcar
1982 (lambda (cell)
1983 (org-export-secondary-string
1984 (org-element-parse-secondary-string
1985 cell
1986 (cdr (assq 'table org-element-string-restrictions)))
1987 'e-latex info))
1988 (org-split-string row "[ \t]*|[ \t]*"))))
1989 (org-split-string
1990 (org-export-clean-table
1991 raw-table (plist-get table-info :special-column-p))
1992 "\n"))))
1993 ;; If BOOKTABSP is non-nil, remove any rule at the beginning
1994 ;; and the end of the table, since booktabs' special rules
1995 ;; will be inserted instead.
1996 (when booktabsp
1997 (when (eq (car clean-table) 'hline)
1998 (setq clean-table (cdr clean-table)))
1999 (when (eq (car (last clean-table)) 'hline)
2000 (setq clean-table (butlast clean-table))))
2001 ;; Convert ROWS to send them to `orgtbl-to-latex'. In
2002 ;; particular, send each cell to
2003 ;; `org-element-parse-secondary-string' to expand any Org
2004 ;; object within. Eventually, flesh the format string out
2005 ;; with the table.
2006 (format
2007 (org-e-latex-table--format-string table table-info info)
2008 (orgtbl-to-latex
2009 clean-table
2010 ;; Parameters passed to `orgtbl-to-latex'.
2011 `(:tstart ,(and booktabsp "\\toprule")
2012 :tend ,(and booktabsp "\\bottomrule")
2013 :hline ,(if booktabsp "\\midrule" "\\hline")
2014 ;; Longtable environment requires specific header
2015 ;; lines end string.
2016 :hlend ,(and longtablep
2017 (format "\\\\
2019 \\endhead
2020 %s\\multicolumn{%d}{r}{Continued on next page}\\\\
2021 \\endfoot
2022 \\endlastfoot"
2023 (if booktabsp "\\midrule" "\\hline")
2024 (if booktabsp "\\midrule" "\\hline")
2025 columns-number))))))))))
2028 ;;;; Target
2030 (defun org-e-latex-target (target contents info)
2031 "Transcode a TARGET object from Org to LaTeX.
2032 CONTENTS is nil. INFO is a plist holding contextual
2033 information."
2034 (format "\\label{%s}"
2035 (org-export-solidify-link-text (org-element-property :value target))))
2038 ;;;; Time-stamp
2040 (defun org-e-latex-time-stamp (time-stamp contents info)
2041 "Transcode a TIME-STAMP object from Org to LaTeX.
2042 CONTENTS is nil. INFO is a plist holding contextual
2043 information."
2044 (let ((value (org-element-property :value time-stamp))
2045 (type (org-element-property :type time-stamp))
2046 (appt-type (org-element-property :appt-type time-stamp)))
2047 (concat (cond ((eq appt-type 'scheduled)
2048 (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
2049 ((eq appt-type 'deadline)
2050 (format "\\textbf{\\textsc{%s}} " org-deadline-string))
2051 ((eq appt-type 'closed)
2052 (format "\\textbf{\\textsc{%s}} " org-closed-string)))
2053 (cond ((memq type '(active active-range))
2054 (format org-e-latex-active-timestamp-format value))
2055 ((memq type '(inactive inactive-range))
2056 (format org-e-latex-inactive-timestamp-format value))
2058 (format org-e-latex-diary-timestamp-format value))))))
2061 ;;;; Verbatim
2063 (defun org-e-latex-verbatim (verbatim contents info)
2064 "Transcode a VERBATIM object from Org to LaTeX.
2065 CONTENTS is nil. INFO is a plist used as a communication
2066 channel."
2067 (let ((fmt (cdr (assoc (org-element-property :marker verbatim)
2068 org-e-latex-emphasis-alist)))
2069 (value (org-element-property :value verbatim)))
2070 (cond
2071 ;; Handle the `verb' special case.
2072 ((eq 'verb fmt)
2073 (let ((separator (org-e-latex--find-verb-separator value)))
2074 (concat "\\verb" separator value separator)))
2075 ;; Handle the `protectedtexttt' special case.
2076 ((eq 'protectedtexttt fmt)
2077 (let ((start 0)
2078 (trans '(("\\" . "\\textbackslash{}")
2079 ("~" . "\\textasciitilde{}")
2080 ("^" . "\\textasciicircum{}")))
2081 (rtn "")
2082 char)
2083 (while (string-match "[\\{}$%&_#~^]" value)
2084 (setq char (match-string 0 value))
2085 (if (> (match-beginning 0) 0)
2086 (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
2087 (setq value (substring value (1+ (match-beginning 0))))
2088 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
2089 rtn (concat rtn char)))
2090 (setq value (concat rtn value)
2091 fmt "\\texttt{%s}")
2092 (while (string-match "--" value)
2093 (setq value (replace-match "-{}-" t t value)))
2094 (format fmt value)))
2095 ;; Else use format string.
2096 (t (format fmt value)))))
2099 ;;;; Verse Block
2101 (defun org-e-latex-verse-block (verse-block contents info)
2102 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2103 CONTENTS is nil. INFO is a plist holding contextual information."
2104 (org-e-latex--wrap-label
2105 verse-block
2106 ;; In a verse environment, add a line break to each newline
2107 ;; character and change each white space at beginning of a line
2108 ;; into a space of 1 em. Also change each blank line with
2109 ;; a vertical space of 1 em.
2110 (progn
2111 (setq contents (replace-regexp-in-string
2112 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2113 (replace-regexp-in-string
2114 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
2115 (org-remove-indentation
2116 (org-export-secondary-string
2117 (org-element-property :value verse-block)
2118 'e-latex info)))))
2119 (while (string-match "^[ \t]+" contents)
2120 (let ((new-str (format "\\hspace*{%dem}"
2121 (length (match-string 0 contents)))))
2122 (setq contents (replace-match new-str nil t contents))))
2123 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2127 ;;; Interactive functions
2129 (defun org-e-latex-export-to-latex
2130 (&optional subtreep visible-only body-only ext-plist pub-dir)
2131 "Export current buffer to a LaTeX file.
2133 If narrowing is active in the current buffer, only export its
2134 narrowed part.
2136 If a region is active, export that region.
2138 When optional argument SUBTREEP is non-nil, export the sub-tree
2139 at point, extracting information from the headline properties
2140 first.
2142 When optional argument VISIBLE-ONLY is non-nil, don't export
2143 contents of hidden elements.
2145 When optional argument BODY-ONLY is non-nil, only write code
2146 between \"\\begin{document}\" and \"\\end{document}\".
2148 EXT-PLIST, when provided, is a property list with external
2149 parameters overriding Org default settings, but still inferior to
2150 file-local settings.
2152 When optional argument PUB-DIR is set, use it as the publishing
2153 directory.
2155 Return output file's name."
2156 (interactive)
2157 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
2158 (org-export-to-file
2159 'e-latex outfile subtreep visible-only body-only ext-plist)))
2161 (defun org-e-latex-export-to-pdf
2162 (&optional subtreep visible-only body-only ext-plist pub-dir)
2163 "Export current buffer to LaTeX then process through to PDF.
2165 If narrowing is active in the current buffer, only export its
2166 narrowed part.
2168 If a region is active, export that region.
2170 When optional argument SUBTREEP is non-nil, export the sub-tree
2171 at point, extracting information from the headline properties
2172 first.
2174 When optional argument VISIBLE-ONLY is non-nil, don't export
2175 contents of hidden elements.
2177 When optional argument BODY-ONLY is non-nil, only write code
2178 between \"\\begin{document}\" and \"\\end{document}\".
2180 EXT-PLIST, when provided, is a property list with external
2181 parameters overriding Org default settings, but still inferior to
2182 file-local settings.
2184 When optional argument PUB-DIR is set, use it as the publishing
2185 directory.
2187 Return PDF file's name."
2188 (interactive)
2189 (org-e-latex-compile
2190 (org-e-latex-export-to-latex
2191 subtreep visible-only body-only ext-plist pub-dir)))
2193 (defun org-e-latex-compile (texfile)
2194 "Compile a TeX file.
2196 TEXFILE is the name of the file being compiled. Processing is
2197 done through the command specified in `org-e-latex-pdf-process'.
2199 Return PDF file name or an error if it couldn't be produced."
2200 (let* ((wconfig (current-window-configuration))
2201 (texfile (file-truename texfile))
2202 (base (file-name-sans-extension texfile))
2203 errors)
2204 (message (format "Processing LaTeX file %s ..." texfile))
2205 (unwind-protect
2206 (progn
2207 (cond
2208 ;; A function is provided: Apply it.
2209 ((functionp org-e-latex-pdf-process)
2210 (funcall org-e-latex-pdf-process (shell-quote-argument texfile)))
2211 ;; A list is provided: Replace %b, %f and %o with appropriate
2212 ;; values in each command before applying it. Output is
2213 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2214 ((consp org-e-latex-pdf-process)
2215 (let* ((out-dir (or (file-name-directory texfile) "./"))
2216 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2217 (mapc
2218 (lambda (command)
2219 (shell-command
2220 (replace-regexp-in-string
2221 "%b" (shell-quote-argument base)
2222 (replace-regexp-in-string
2223 "%f" (shell-quote-argument texfile)
2224 (replace-regexp-in-string
2225 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2226 outbuf))
2227 org-e-latex-pdf-process)
2228 ;; Collect standard errors from output buffer.
2229 (setq errors (org-e-latex-collect-errors outbuf))))
2230 (t (error "No valid command to process to PDF")))
2231 (let ((pdffile (concat base ".pdf")))
2232 ;; Check for process failure. Provide collected errors if
2233 ;; possible.
2234 (if (not (file-exists-p pdffile))
2235 (error (concat (format "PDF file %s wasn't produced" pdffile)
2236 (when errors (concat ": " errors))))
2237 ;; Else remove log files, when specified, and signal end of
2238 ;; process to user, along with any error encountered.
2239 (when org-e-latex-remove-logfiles
2240 (dolist (ext org-e-latex-logfiles-extensions)
2241 (let ((file (concat base "." ext)))
2242 (when (file-exists-p file) (delete-file file)))))
2243 (message (concat "Process completed"
2244 (if (not errors) "."
2245 (concat " with errors: " errors)))))
2246 ;; Return output file name.
2247 pdffile))
2248 (set-window-configuration wconfig))))
2250 (defun org-e-latex-collect-errors (buffer)
2251 "Collect some kind of errors from \"pdflatex\" command output.
2253 BUFFER is the buffer containing output.
2255 Return collected error types as a string, or nil if there was
2256 none."
2257 (with-current-buffer buffer
2258 (save-excursion
2259 (goto-char (point-max))
2260 ;; Find final "pdflatex" run.
2261 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
2262 (let ((case-fold-search t)
2263 (errors ""))
2264 (when (save-excursion
2265 (re-search-forward "Reference.*?undefined" nil t))
2266 (setq errors (concat errors " [undefined reference]")))
2267 (when (save-excursion
2268 (re-search-forward "Citation.*?undefined" nil t))
2269 (setq errors (concat errors " [undefined citation]")))
2270 (when (save-excursion
2271 (re-search-forward "Undefined control sequence" nil t))
2272 (setq errors (concat errors " [undefined control sequence]")))
2273 (when (save-excursion
2274 (re-search-forward "^! LaTeX.*?Error" nil t))
2275 (setq errors (concat errors " [LaTeX error]")))
2276 (when (save-excursion
2277 (re-search-forward "^! Package.*?Error" nil t))
2278 (setq errors (concat errors " [package error]")))
2279 (and (org-string-nw-p errors) (org-trim errors)))))))
2282 (provide 'org-e-latex)
2283 ;;; org-e-latex.el ends here