org-export: Export `clock' and `planning' elements
[org-mode/org-mode-NeilSmithlineMods.git] / contrib / lisp / org-e-latex.el
blob7bfb7c5187ecc0886c810156a8b673f1208f95a1
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-data "org-export" (data 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-format-code "org-export"
57 (code fun &optional num-lines ref-alist))
58 (declare-function org-export-format-code-default "org-export" (element info))
59 (declare-function org-export-get-coderef-format "org-export" (path desc))
60 (declare-function org-export-get-footnote-definition "org-export"
61 (footnote-reference info))
62 (declare-function org-export-get-footnote-number "org-export" (footnote info))
63 (declare-function org-export-get-previous-element "org-export" (blob info))
64 (declare-function org-export-get-relative-level "org-export" (headline info))
65 (declare-function org-export-unravel-code "org-export" (element))
66 (declare-function org-export-inline-image-p "org-export"
67 (link &optional extensions))
68 (declare-function org-export-last-sibling-p "org-export" (headline info))
69 (declare-function org-export-low-level-p "org-export" (headline info))
70 (declare-function org-export-output-file-name
71 "org-export" (extension &optional subtreep pub-dir))
72 (declare-function org-export-resolve-coderef "org-export" (ref info))
73 (declare-function org-export-resolve-fuzzy-link "org-export" (link info))
74 (declare-function org-export-secondary-string "org-export" (secondary info))
75 (declare-function org-export-solidify-link-text "org-export" (s))
76 (declare-function
77 org-export-to-buffer "org-export"
78 (backend buffer &optional subtreep visible-only body-only ext-plist))
79 (declare-function
80 org-export-to-file "org-export"
81 (backend file &optional subtreep visible-only body-only ext-plist))
85 ;;; Internal Variables
87 (defconst org-e-latex-option-alist
88 '((:date "DATE" nil org-e-latex-date-format t)
89 (:latex-class "LATEX_CLASS" nil org-e-latex-default-class t)
90 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
91 (:latex-header-extra "LATEX_HEADER" nil nil newline))
92 "Alist between LaTeX export properties and ways to set them.
93 See `org-export-option-alist' for more information on the
94 structure of the value.")
98 ;;; User Configurable Variables
100 (defgroup org-export-e-latex nil
101 "Options for exporting Org mode files to LaTeX."
102 :tag "Org Export LaTeX"
103 :group 'org-export)
106 ;;;; Preamble
108 (defcustom org-e-latex-default-class "article"
109 "The default LaTeX class."
110 :group 'org-export-e-latex
111 :type '(string :tag "LaTeX class"))
113 (defcustom org-e-latex-classes
114 '(("article"
115 "\\documentclass[11pt]{article}"
116 ("\\section{%s}" . "\\section*{%s}")
117 ("\\subsection{%s}" . "\\subsection*{%s}")
118 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
119 ("\\paragraph{%s}" . "\\paragraph*{%s}")
120 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
121 ("report"
122 "\\documentclass[11pt]{report}"
123 ("\\part{%s}" . "\\part*{%s}")
124 ("\\chapter{%s}" . "\\chapter*{%s}")
125 ("\\section{%s}" . "\\section*{%s}")
126 ("\\subsection{%s}" . "\\subsection*{%s}")
127 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
128 ("book"
129 "\\documentclass[11pt]{book}"
130 ("\\part{%s}" . "\\part*{%s}")
131 ("\\chapter{%s}" . "\\chapter*{%s}")
132 ("\\section{%s}" . "\\section*{%s}")
133 ("\\subsection{%s}" . "\\subsection*{%s}")
134 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
135 "Alist of LaTeX classes and associated header and structure.
136 If #+LaTeX_CLASS is set in the buffer, use its value and the
137 associated information. Here is the structure of each cell:
139 \(class-name
140 header-string
141 \(numbered-section . unnumbered-section\)
142 ...\)
144 The header string
145 -----------------
147 The HEADER-STRING is the header that will be inserted into the
148 LaTeX file. It should contain the \\documentclass macro, and
149 anything else that is needed for this setup. To this header, the
150 following commands will be added:
152 - Calls to \\usepackage for all packages mentioned in the
153 variables `org-export-latex-default-packages-alist' and
154 `org-export-latex-packages-alist'. Thus, your header
155 definitions should avoid to also request these packages.
157 - Lines specified via \"#+LaTeX_HEADER:\"
159 If you need more control about the sequence in which the header
160 is built up, or if you want to exclude one of these building
161 blocks for a particular class, you can use the following
162 macro-like placeholders.
164 [DEFAULT-PACKAGES] \\usepackage statements for default packages
165 [NO-DEFAULT-PACKAGES] do not include any of the default packages
166 [PACKAGES] \\usepackage statements for packages
167 [NO-PACKAGES] do not include the packages
168 [EXTRA] the stuff from #+LaTeX_HEADER
169 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
170 [BEAMER-HEADER-EXTRA] the beamer extra headers
172 So a header like
174 \\documentclass{article}
175 [NO-DEFAULT-PACKAGES]
176 [EXTRA]
177 \\providecommand{\\alert}[1]{\\textbf{#1}}
178 [PACKAGES]
180 will omit the default packages, and will include the
181 #+LaTeX_HEADER lines, then have a call to \\providecommand, and
182 then place \\usepackage commands based on the content of
183 `org-export-latex-packages-alist'.
185 If your header, `org-export-latex-default-packages-alist' or
186 `org-export-latex-packages-alist' inserts
187 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
188 replaced with a coding system derived from
189 `buffer-file-coding-system'. See also the variable
190 `org-e-latex-inputenc-alist' for a way to influence this
191 mechanism.
193 The sectioning structure
194 ------------------------
196 The sectioning structure of the class is given by the elements
197 following the header string. For each sectioning level, a number
198 of strings is specified. A %s formatter is mandatory in each
199 section string and will be replaced by the title of the section.
201 Instead of a cons cell \(numbered . unnumbered\), you can also
202 provide a list of 2 or 4 elements,
204 \(numbered-open numbered-close\)
208 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
210 providing opening and closing strings for a LaTeX environment
211 that should represent the document section. The opening clause
212 should have a %s to represent the section title.
214 Instead of a list of sectioning commands, you can also specify
215 a function name. That function will be called with two
216 parameters, the \(reduced) level of the headline, and a predicate
217 non-nil when the headline should be numbered. It must return
218 a format string in which the section title will be added."
219 :group 'org-export-e-latex
220 :type '(repeat
221 (list (string :tag "LaTeX class")
222 (string :tag "LaTeX header")
223 (repeat :tag "Levels" :inline t
224 (choice
225 (cons :tag "Heading"
226 (string :tag " numbered")
227 (string :tag "unnumbered"))
228 (list :tag "Environment"
229 (string :tag "Opening (numbered)")
230 (string :tag "Closing (numbered)")
231 (string :tag "Opening (unnumbered)")
232 (string :tag "Closing (unnumbered)"))
233 (function :tag "Hook computing sectioning"))))))
235 (defcustom org-e-latex-inputenc-alist nil
236 "Alist of inputenc coding system names, and what should really be used.
237 For example, adding an entry
239 (\"utf8\" . \"utf8x\")
241 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
242 are written as utf8 files."
243 :group 'org-export-e-latex
244 :type '(repeat
245 (cons
246 (string :tag "Derived from buffer")
247 (string :tag "Use this instead"))))
249 (defcustom org-e-latex-date-format
250 "\\today"
251 "Format string for \\date{...}."
252 :group 'org-export-e-latex
253 :type 'boolean)
255 (defcustom org-e-latex-title-command "\\maketitle"
256 "The command used to insert the title just after \\begin{document}.
257 If this string contains the formatting specification \"%s\" then
258 it will be used as a formatting string, passing the title as an
259 argument."
260 :group 'org-export-e-latex
261 :type 'string)
264 ;;;; Headline
266 (defcustom org-e-latex-format-headline-function nil
267 "Function to format headline text.
269 This function will be called with 5 arguments:
270 TODO the todo keyword \(string or nil\).
271 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
272 PRIORITY the priority of the headline \(integer or nil\)
273 TEXT the main headline text \(string\).
274 TAGS the tags string, separated with colons \(string or nil\).
276 The function result will be used in the section format string.
278 As an example, one could set the variable to the following, in
279 order to reproduce the default set-up:
281 \(defun org-e-latex-format-headline \(todo todo-type priority text tags\)
282 \"Default format function for an headline.\"
283 \(concat \(when todo
284 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
285 \(when priority
286 \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
287 text
288 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
289 :group 'org-export-e-latex
290 :type 'function)
293 ;;;; Footnotes
295 (defcustom org-e-latex-footnote-separator "\\textsuperscript{,}\\,"
296 "Text used to separate footnotes."
297 :group 'org-export-e-latex
298 :type 'string)
301 ;;;; Time-stamps
303 (defcustom org-e-latex-active-timestamp-format "\\textit{%s}"
304 "A printf format string to be applied to active time-stamps."
305 :group 'org-export-e-latex
306 :type 'string)
308 (defcustom org-e-latex-inactive-timestamp-format "\\textit{%s}"
309 "A printf format string to be applied to inactive time-stamps."
310 :group 'org-export-e-latex
311 :type 'string)
313 (defcustom org-e-latex-diary-timestamp-format "\\textit{%s}"
314 "A printf format string to be applied to diary time-stamps."
315 :group 'org-export-e-latex
316 :type 'string)
319 ;;;; Links
321 (defcustom org-e-latex-image-default-option "width=.9\\linewidth"
322 "Default option for images."
323 :group 'org-export-e-latex
324 :type 'string)
326 (defcustom org-e-latex-default-figure-position "htb"
327 "Default position for latex figures."
328 :group 'org-export-e-latex
329 :type 'string)
331 (defcustom org-e-latex-inline-image-rules
332 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\)\\'"))
333 "Rules characterizing image files that can be inlined into LaTeX.
335 A rule consists in an association whose key is the type of link
336 to consider, and value is a regexp that will be matched against
337 link's path.
339 Note that, by default, the image extension *actually* allowed
340 depend on the way the LaTeX file is processed. When used with
341 pdflatex, pdf, jpg and png images are OK. When processing
342 through dvi to Postscript, only ps and eps are allowed. The
343 default we use here encompasses both."
344 :group 'org-export-e-latex
345 :type '(alist :key-type (string :tag "Type")
346 :value-type (regexp :tag "Path")))
349 ;;;; Tables
351 (defcustom org-e-latex-default-table-environment "tabular"
352 "Default environment used to build tables."
353 :group 'org-export-e-latex
354 :type 'string)
356 (defcustom org-e-latex-tables-centered t
357 "When non-nil, tables are exported in a center environment."
358 :group 'org-export-e-latex
359 :type 'boolean)
361 (defcustom org-e-latex-tables-verbatim nil
362 "When non-nil, tables are exported verbatim."
363 :group 'org-export-e-latex
364 :type 'boolean)
366 (defcustom org-e-latex-tables-booktabs nil
367 "When non-nil, display tables in a formal \"booktabs\" style.
368 This option assumes that the \"booktabs\" package is properly
369 loaded in the header of the document. This value can be ignored
370 locally with \"booktabs=yes\" and \"booktabs=no\" LaTeX
371 attributes."
372 :group 'org-export-e-latex
373 :type 'boolean)
375 (defcustom org-e-latex-table-caption-above t
376 "When non-nil, place caption string at the beginning of the table.
377 Otherwise, place it near the end."
378 :group 'org-export-e-latex
379 :type 'boolean)
381 (defcustom org-e-latex-table-scientific-notation "%s\\,(%s)"
382 "Format string to display numbers in scientific notation.
383 The format should have \"%s\" twice, for mantissa and exponent
384 \(i.e. \"%s\\\\times10^{%s}\").
386 When nil, no transformation is made."
387 :group 'org-export-e-latex
388 :type '(choice
389 (string :tag "Format string")
390 (const :tag "No formatting")))
393 ;;;; Text markup
395 (defcustom org-e-latex-text-markup-alist '((bold . "\\textbf{%s}")
396 (code . verb)
397 (italic . "\\emph{%s}")
398 (strike-through . "\\st{%s}")
399 (underline . "\\underline{%s}")
400 (verbatim . protectedtexttt))
401 "Alist of LaTeX expressions to convert text markup.
403 The key must be a symbol among `bold', `code', `italic',
404 `strike-through', `underline' and `verbatim'. The value is
405 a formatting string to wrap fontified text with.
407 Value can also be set to the following symbols: `verb' and
408 `protectedtexttt'. For the former, Org will use \"\\verb\" to
409 create a format string and select a delimiter character that
410 isn't in the string. For the latter, Org will use \"\\texttt\"
411 to typeset and try to protect special characters.
413 If no association can be found for a given markup, text will be
414 returned as-is."
415 :group 'org-export-e-latex
416 :type 'alist
417 :options '(bold code italic strike-through underline verbatim))
420 ;;;; Drawers
422 (defcustom org-e-latex-format-drawer-function nil
423 "Function called to format a drawer in LaTeX code.
425 The function must accept two parameters:
426 NAME the drawer name, like \"LOGBOOK\"
427 CONTENTS the contents of the drawer.
429 The function should return the string to be exported.
431 For example, the variable could be set to the following function
432 in order to mimic default behaviour:
434 \(defun org-e-latex-format-drawer-default \(name contents\)
435 \"Format a drawer element for LaTeX export.\"
436 contents\)"
437 :group 'org-export-e-latex
438 :type 'function)
441 ;;;; Inlinetasks
443 (defcustom org-e-latex-format-inlinetask-function nil
444 "Function called to format an inlinetask in LaTeX code.
446 The function must accept six parameters:
447 TODO the todo keyword, as a string
448 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
449 PRIORITY the inlinetask priority, as a string
450 NAME the inlinetask name, as a string.
451 TAGS the inlinetask tags, as a string.
452 CONTENTS the contents of the inlinetask, as a string.
454 The function should return the string to be exported.
456 For example, the variable could be set to the following function
457 in order to mimic default behaviour:
459 \(defun org-e-latex-format-inlinetask \(todo type priority name tags contents\)
460 \"Format an inline task element for LaTeX export.\"
461 \(let \(\(full-title
462 \(concat
463 \(when todo
464 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
465 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
466 title
467 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
468 \(format \(concat \"\\\\begin{center}\\n\"
469 \"\\\\fbox{\\n\"
470 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
471 \"%s\\n\\n\"
472 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
473 \"%s\"
474 \"\\\\end{minipage}}\"
475 \"\\\\end{center}\"\)
476 full-title contents\)\)"
477 :group 'org-export-e-latex
478 :type 'function)
481 ;; Src blocks
483 (defcustom org-e-latex-listings nil
484 "Non-nil means export source code using the listings package.
485 This package will fontify source code, possibly even with color.
486 If you want to use this, you also need to make LaTeX use the
487 listings package, and if you want to have color, the color
488 package. Just add these to `org-export-latex-packages-alist',
489 for example using customize, or with something like:
491 \(require 'org-e-latex)
492 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"listings\"))
493 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"color\"))
495 Alternatively,
497 \(setq org-e-latex-listings 'minted)
499 causes source code to be exported using the minted package as
500 opposed to listings. If you want to use minted, you need to add
501 the minted package to `org-export-latex-packages-alist', for
502 example using customize, or with
504 \(require 'org-e-latex)
505 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"minted\"))
507 In addition, it is necessary to install pygments
508 \(http://pygments.org), and to configure the variable
509 `org-e-latex-pdf-process' so that the -shell-escape option is
510 passed to pdflatex."
511 :group 'org-export-e-latex
512 :type '(choice
513 (const :tag "Use listings" t)
514 (const :tag "Use minted" 'minted)
515 (const :tag "Export verbatim" nil)))
517 (defcustom org-e-latex-listings-langs
518 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
519 (c "C") (cc "C++")
520 (fortran "fortran")
521 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
522 (html "HTML") (xml "XML")
523 (tex "TeX") (latex "TeX")
524 (shell-script "bash")
525 (gnuplot "Gnuplot")
526 (ocaml "Caml") (caml "Caml")
527 (sql "SQL") (sqlite "sql"))
528 "Alist mapping languages to their listing language counterpart.
529 The key is a symbol, the major mode symbol without the \"-mode\".
530 The value is the string that should be inserted as the language
531 parameter for the listings package. If the mode name and the
532 listings name are the same, the language does not need an entry
533 in this list - but it does not hurt if it is present."
534 :group 'org-export-e-latex
535 :type '(repeat
536 (list
537 (symbol :tag "Major mode ")
538 (string :tag "Listings language"))))
540 (defcustom org-e-latex-listings-options nil
541 "Association list of options for the latex listings package.
543 These options are supplied as a comma-separated list to the
544 \\lstset command. Each element of the association list should be
545 a list containing two strings: the name of the option, and the
546 value. For example,
548 (setq org-e-latex-listings-options
549 '((\"basicstyle\" \"\\small\")
550 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
552 will typeset the code in a small size font with underlined, bold
553 black keywords.
555 Note that the same options will be applied to blocks of all
556 languages."
557 :group 'org-export-e-latex
558 :type '(repeat
559 (list
560 (string :tag "Listings option name ")
561 (string :tag "Listings option value"))))
563 (defcustom org-e-latex-minted-langs
564 '((emacs-lisp "common-lisp")
565 (cc "c++")
566 (cperl "perl")
567 (shell-script "bash")
568 (caml "ocaml"))
569 "Alist mapping languages to their minted language counterpart.
570 The key is a symbol, the major mode symbol without the \"-mode\".
571 The value is the string that should be inserted as the language
572 parameter for the minted package. If the mode name and the
573 listings name are the same, the language does not need an entry
574 in this list - but it does not hurt if it is present.
576 Note that minted uses all lower case for language identifiers,
577 and that the full list of language identifiers can be obtained
578 with:
580 pygmentize -L lexers"
581 :group 'org-export-e-latex
582 :type '(repeat
583 (list
584 (symbol :tag "Major mode ")
585 (string :tag "Minted language"))))
587 (defcustom org-e-latex-minted-options nil
588 "Association list of options for the latex minted package.
590 These options are supplied within square brackets in
591 \\begin{minted} environments. Each element of the alist should
592 be a list containing two strings: the name of the option, and the
593 value. For example,
595 \(setq org-e-latex-minted-options
596 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
598 will result in src blocks being exported with
600 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
602 as the start of the minted environment. Note that the same
603 options will be applied to blocks of all languages."
604 :group 'org-export-e-latex
605 :type '(repeat
606 (list
607 (string :tag "Minted option name ")
608 (string :tag "Minted option value"))))
610 (defvar org-e-latex-custom-lang-environments nil
611 "Alist mapping languages to language-specific LaTeX environments.
613 It is used during export of src blocks by the listings and minted
614 latex packages. For example,
616 \(setq org-e-latex-custom-lang-environments
617 '\(\(python \"pythoncode\"\)\)\)
619 would have the effect that if org encounters begin_src python
620 during latex export it will output
622 \\begin{pythoncode}
623 <src block body>
624 \\end{pythoncode}")
627 ;;;; Plain text
629 (defcustom org-e-latex-quotes
630 '(("fr"
631 ("\\(\\s-\\|[[(]\\|^\\)\"" . "«~")
632 ("\\(\\S-\\)\"" . "~»")
633 ("\\(\\s-\\|(\\|^\\)'" . "'"))
634 ("en"
635 ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
636 ("\\(\\S-\\)\"" . "''")
637 ("\\(\\s-\\|(\\|^\\)'" . "`")))
638 "Alist for quotes to use when converting english double-quotes.
640 The CAR of each item in this alist is the language code.
641 The CDR of each item in this alist is a list of three CONS:
642 - the first CONS defines the opening quote;
643 - the second CONS defines the closing quote;
644 - the last CONS defines single quotes.
646 For each item in a CONS, the first string is a regexp
647 for allowed characters before/after the quote, the second
648 string defines the replacement string for this quote."
649 :group 'org-export-e-latex
650 :type '(list
651 (cons :tag "Opening quote"
652 (string :tag "Regexp for char before")
653 (string :tag "Replacement quote "))
654 (cons :tag "Closing quote"
655 (string :tag "Regexp for char after ")
656 (string :tag "Replacement quote "))
657 (cons :tag "Single quote"
658 (string :tag "Regexp for char before")
659 (string :tag "Replacement quote "))))
662 ;;;; Compilation
664 (defcustom org-e-latex-pdf-process
665 '("pdflatex -interaction nonstopmode -output-directory %o %f"
666 "pdflatex -interaction nonstopmode -output-directory %o %f"
667 "pdflatex -interaction nonstopmode -output-directory %o %f")
668 "Commands to process a LaTeX file to a PDF file.
669 This is a list of strings, each of them will be given to the
670 shell as a command. %f in the command will be replaced by the
671 full file name, %b by the file base name \(i.e. without
672 extension) and %o by the base directory of the file.
674 The reason why this is a list is that it usually takes several
675 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
676 does not have a clever mechanism to detect which of these
677 commands have to be run to get to a stable result, and it also
678 does not do any error checking.
680 By default, Org uses 3 runs of `pdflatex' to do the processing.
681 If you have texi2dvi on your system and if that does not cause
682 the infamous egrep/locale bug:
684 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
686 then `texi2dvi' is the superior choice. Org does offer it as one
687 of the customize options.
689 Alternatively, this may be a Lisp function that does the
690 processing, so you could use this to apply the machinery of
691 AUCTeX or the Emacs LaTeX mode. This function should accept the
692 file name as its single argument."
693 :group 'org-export-pdf
694 :type '(choice
695 (repeat :tag "Shell command sequence"
696 (string :tag "Shell command"))
697 (const :tag "2 runs of pdflatex"
698 ("pdflatex -interaction nonstopmode -output-directory %o %f"
699 "pdflatex -interaction nonstopmode -output-directory %o %f"))
700 (const :tag "3 runs of pdflatex"
701 ("pdflatex -interaction nonstopmode -output-directory %o %f"
702 "pdflatex -interaction nonstopmode -output-directory %o %f"
703 "pdflatex -interaction nonstopmode -output-directory %o %f"))
704 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
705 ("pdflatex -interaction nonstopmode -output-directory %o %f"
706 "bibtex %b"
707 "pdflatex -interaction nonstopmode -output-directory %o %f"
708 "pdflatex -interaction nonstopmode -output-directory %o %f"))
709 (const :tag "texi2dvi"
710 ("texi2dvi -p -b -c -V %f"))
711 (const :tag "rubber"
712 ("rubber -d --into %o %f"))
713 (function)))
715 (defcustom org-e-latex-logfiles-extensions
716 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
717 "The list of file extensions to consider as LaTeX logfiles."
718 :group 'org-export-e-latex
719 :type '(repeat (string :tag "Extension")))
721 (defcustom org-e-latex-remove-logfiles t
722 "Non-nil means remove the logfiles produced by PDF production.
723 These are the .aux, .log, .out, and .toc files."
724 :group 'org-export-e-latex
725 :type 'boolean)
729 ;;; Internal Functions
731 (defun org-e-latex--caption/label-string (caption label info)
732 "Return caption and label LaTeX string for floats.
734 CAPTION is a cons cell of secondary strings, the car being the
735 standard caption and the cdr its short form. LABEL is a string
736 representing the label. INFO is a plist holding contextual
737 information.
739 If there's no caption nor label, return the empty string.
741 For non-floats, see `org-e-latex--wrap-label'."
742 (let ((label-str (if label (format "\\label{%s}" label) "")))
743 (cond
744 ((and (not caption) (not label)) "")
745 ((not caption) (format "\\label{%s}\n" label))
746 ;; Option caption format with short name.
747 ((cdr caption)
748 (format "\\caption[%s]{%s%s}\n"
749 (org-export-secondary-string (cdr caption) info)
750 label-str
751 (org-export-secondary-string (car caption) info)))
752 ;; Standard caption format.
753 (t (format "\\caption{%s%s}\n"
754 label-str
755 (org-export-secondary-string (car caption) info))))))
757 (defun org-e-latex--guess-inputenc (header)
758 "Set the coding system in inputenc to what the buffer is.
760 HEADER is the LaTeX header string.
762 Return the new header."
763 (let* ((cs (or (ignore-errors
764 (latexenc-coding-system-to-inputenc
765 buffer-file-coding-system))
766 "utf8")))
767 (if (not cs) header
768 ;; First translate if that is requested.
769 (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
770 ;; Then find the \usepackage statement and replace the option.
771 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
772 cs header t nil 1))))
774 (defun org-e-latex--find-verb-separator (s)
775 "Return a character not used in string S.
776 This is used to choose a separator for constructs like \\verb."
777 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
778 (loop for c across ll
779 when (not (string-match (regexp-quote (char-to-string c)) s))
780 return (char-to-string c))))
782 (defun org-e-latex--make-option-string (options)
783 "Return a comma separated string of keywords and values.
784 OPTIONS is an alist where the key is the options keyword as
785 a string, and the value a list containing the keyword value, or
786 nil."
787 (mapconcat (lambda (pair)
788 (concat (first pair)
789 (when (> (length (second pair)) 0)
790 (concat "=" (second pair)))))
791 options
792 ","))
794 (defun org-e-latex--quotation-marks (text info)
795 "Export quotation marks depending on language conventions.
796 TEXT is a string containing quotation marks to be replaced. INFO
797 is a plist used as a communication channel."
798 (mapc (lambda(l)
799 (let ((start 0))
800 (while (setq start (string-match (car l) text start))
801 (let ((new-quote (concat (match-string 1 text) (cdr l))))
802 (setq text (replace-match new-quote t t text))))))
803 (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
804 ;; Falls back on English.
805 (assoc "en" org-e-latex-quotes))))
806 text)
808 (defun org-e-latex--wrap-label (element output)
809 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
810 This function shouldn't be used for floats. See
811 `org-e-latex--caption/label-string'."
812 (let ((label (org-element-property :name element)))
813 (if (or (not output) (not label) (string= output "") (string= label ""))
814 output
815 (concat (format "\\label{%s}\n" label) output))))
817 (defun org-e-latex--text-markup (text markup)
818 "Format text depending on MARKUP text markup.
819 See `org-e-latex-text-markup-alist' for details."
820 (let ((fmt (cdr (assq markup org-e-latex-text-markup-alist))))
821 (cond
822 ;; No format string: Return raw text.
823 ((not fmt) text)
824 ;; Handle the `verb' special case: Find and appropriate separator
825 ;; and use "\\verb" command.
826 ((eq 'verb fmt)
827 (let ((separator (org-e-latex--find-verb-separator text)))
828 (concat "\\verb" separator text separator)))
829 ;; Handle the `protectedtexttt' special case: Protect some
830 ;; special chars and use "\texttt{%s}" format string.
831 ((eq 'protectedtexttt fmt)
832 (let ((start 0)
833 (trans '(("\\" . "\\textbackslash{}")
834 ("~" . "\\textasciitilde{}")
835 ("^" . "\\textasciicircum{}")))
836 (rtn "")
837 char)
838 (while (string-match "[\\{}$%&_#~^]" text)
839 (setq char (match-string 0 text))
840 (if (> (match-beginning 0) 0)
841 (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
842 (setq text (substring text (1+ (match-beginning 0))))
843 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
844 rtn (concat rtn char)))
845 (setq text (concat rtn text)
846 fmt "\\texttt{%s}")
847 (while (string-match "--" text)
848 (setq text (replace-match "-{}-" t t text)))
849 (format fmt text)))
850 ;; Else use format string.
851 (t (format fmt text)))))
855 ;;; Template
857 (defun org-e-latex-template (contents info)
858 "Return complete document string after LaTeX conversion.
859 CONTENTS is the transcoded contents string. INFO is a plist
860 holding export options."
861 (let ((title (org-export-secondary-string (plist-get info :title) info)))
862 (concat
863 ;; 1. Time-stamp.
864 (and (plist-get info :time-stamp-file)
865 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
866 ;; 2. Document class and packages.
867 (let ((class (plist-get info :latex-class))
868 (class-options (plist-get info :latex-class-options)))
869 (org-element-normalize-string
870 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
871 (document-class-string
872 (and (stringp header)
873 (if class-options
874 (replace-regexp-in-string
875 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
876 class-options header t nil 1)
877 header))))
878 (org-e-latex--guess-inputenc
879 (org-splice-latex-header
880 document-class-string
881 org-export-latex-default-packages-alist ; defined in org.el
882 org-export-latex-packages-alist nil ; defined in org.el
883 (plist-get info :latex-header-extra))))))
884 ;; 3. Define alert if not yet defined.
885 "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
886 ;; 4. Possibly limit depth for headline numbering.
887 (let ((sec-num (plist-get info :section-numbers)))
888 (when (integerp sec-num)
889 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
890 ;; 5. Author.
891 (let ((author (and (plist-get info :with-author)
892 (let ((auth (plist-get info :author)))
893 (and auth (org-export-secondary-string auth info)))))
894 (email (and (plist-get info :with-email)
895 (org-export-secondary-string
896 (plist-get info :email) info))))
897 (cond ((and author email (not (string= "" email)))
898 (format "\\author{%s\\thanks{%s}}\n" author email))
899 (author (format "\\author{%s}\n" author))
900 (t "\\author{}\n")))
901 ;; 6. Date.
902 (let ((date (plist-get info :date)))
903 (and date (format "\\date{%s}\n" date)))
904 ;; 7. Title
905 (format "\\title{%s}\n" title)
906 ;; 8. Hyperref options.
907 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
908 (or (plist-get info :keywords) "")
909 (or (plist-get info :description) "")
910 (if (not (plist-get info :with-creator)) ""
911 (plist-get info :creator)))
912 ;; 9. Document start.
913 "\\begin{document}\n\n"
914 ;; 10. Title command.
915 (org-element-normalize-string
916 (cond ((string= "" title) nil)
917 ((not (stringp org-e-latex-title-command)) nil)
918 ((string-match "\\(?:[^%]\\|^\\)%s"
919 org-e-latex-title-command)
920 (format org-e-latex-title-command title))
921 (t org-e-latex-title-command)))
922 ;; 11. Table of contents.
923 (let ((depth (plist-get info :with-toc)))
924 (when depth
925 (concat (when (wholenump depth)
926 (format "\\setcounter{tocdepth}{%d}\n" depth))
927 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
928 ;; 12. Document's body.
929 contents
930 ;; 13. Creator.
931 (let ((creator-info (plist-get info :with-creator)))
932 (cond
933 ((not creator-info) "")
934 ((eq creator-info 'comment)
935 (format "%% %s\n" (plist-get info :creator)))
936 (t (concat (plist-get info :creator) "\n"))))
937 ;; 14. Document end.
938 "\\end{document}")))
942 ;;; Transcode Functions
944 ;;;; Babel Call
946 ;; Babel Calls are ignored.
949 ;;;; Bold
951 (defun org-e-latex-bold (bold contents info)
952 "Transcode BOLD from Org to LaTeX.
953 CONTENTS is the text with bold markup. INFO is a plist holding
954 contextual information."
955 (org-e-latex--text-markup contents 'bold))
958 ;;;; Center Block
960 (defun org-e-latex-center-block (center-block contents info)
961 "Transcode a CENTER-BLOCK element from Org to LaTeX.
962 CONTENTS holds the contents of the center block. INFO is a plist
963 holding contextual information."
964 (org-e-latex--wrap-label
965 center-block
966 (format "\\begin{center}\n%s\\end{center}" contents)))
969 ;;;; Clock
971 (defun org-e-latex-clock (clock contents info)
972 "Transcode a CLOCK element from Org to LaTeX.
973 CONTENTS is nil. INFO is a plist holding contextual
974 information."
975 (concat
976 "\\noindent"
977 (format "\\textbf{%s} " org-clock-string)
978 (format org-e-latex-inactive-timestamp-format
979 (concat (org-translate-time (org-element-property :value clock))
980 (let ((time (org-element-property :time clock)))
981 (and time (format " (%s)" time)))))
982 "\\\\"))
985 ;;;; Code
987 (defun org-e-latex-code (code contents info)
988 "Transcode a CODE object from Org to LaTeX.
989 CONTENTS is nil. INFO is a plist used as a communication
990 channel."
991 (org-e-latex--text-markup (org-element-property :value code) 'code))
994 ;;;; Comment
996 ;; Comments are ignored.
999 ;;;; Comment Block
1001 ;; Comment Blocks are ignored.
1004 ;;;; Drawer
1006 (defun org-e-latex-drawer (drawer contents info)
1007 "Transcode a DRAWER element from Org to LaTeX.
1008 CONTENTS holds the contents of the block. INFO is a plist
1009 holding contextual information."
1010 (let* ((name (org-element-property :drawer-name drawer))
1011 (output (if (functionp org-e-latex-format-drawer-function)
1012 (funcall org-e-latex-format-drawer-function
1013 name contents)
1014 ;; If there's no user defined function: simply
1015 ;; display contents of the drawer.
1016 contents)))
1017 (org-e-latex--wrap-label drawer output)))
1020 ;;;; Dynamic Block
1022 (defun org-e-latex-dynamic-block (dynamic-block contents info)
1023 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1024 CONTENTS holds the contents of the block. INFO is a plist
1025 holding contextual information. See `org-export-data'."
1026 (org-e-latex--wrap-label dynamic-block contents))
1029 ;;;; Entity
1031 (defun org-e-latex-entity (entity contents info)
1032 "Transcode an ENTITY object from Org to LaTeX.
1033 CONTENTS are the definition itself. INFO is a plist holding
1034 contextual information."
1035 (let ((ent (org-element-property :latex entity)))
1036 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
1039 ;;;; Example Block
1041 (defun org-e-latex-example-block (example-block contents info)
1042 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1043 CONTENTS is nil. INFO is a plist holding contextual
1044 information."
1045 (org-e-latex--wrap-label
1046 example-block
1047 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1048 (org-export-format-code-default example-block info))))
1051 ;;;; Export Snippet
1053 (defun org-e-latex-export-snippet (export-snippet contents info)
1054 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1055 CONTENTS is nil. INFO is a plist holding contextual information."
1056 (when (eq (org-export-snippet-backend export-snippet) 'e-latex)
1057 (org-element-property :value export-snippet)))
1060 ;;;; Export Block
1062 (defun org-e-latex-export-block (export-block contents info)
1063 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1064 CONTENTS is nil. INFO is a plist holding contextual information."
1065 (when (string= (org-element-property :type export-block) "latex")
1066 (org-remove-indentation (org-element-property :value export-block))))
1069 ;;;; Fixed Width
1071 (defun org-e-latex-fixed-width (fixed-width contents info)
1072 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1073 CONTENTS is nil. INFO is a plist holding contextual information."
1074 (let* ((value (org-element-normalize-string
1075 (replace-regexp-in-string
1076 "^[ \t]*: ?" ""
1077 (org-element-property :value fixed-width)))))
1078 (org-e-latex--wrap-label
1079 fixed-width (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1082 ;;;; Footnote Definition
1084 ;; Footnote Definitions are ignored.
1087 ;;;; Footnote Reference
1089 (defun org-e-latex-footnote-reference (footnote-reference contents info)
1090 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1091 CONTENTS is nil. INFO is a plist holding contextual information."
1092 (concat
1093 ;; Insert separator between two footnotes in a row.
1094 (let ((prev (org-export-get-previous-element footnote-reference info)))
1095 (when (eq (org-element-type prev) 'footnote-reference)
1096 org-e-latex-footnote-separator))
1097 (cond
1098 ;; Use \footnotemark if the footnote has already been defined.
1099 ((not (org-export-footnote-first-reference-p footnote-reference info))
1100 (format "\\footnotemark[%s]{}"
1101 (org-export-get-footnote-number footnote-reference info)))
1102 ;; Use also \footnotemark if reference is within another footnote
1103 ;; reference or footnote definition.
1104 ((loop for parent in (org-export-get-genealogy footnote-reference info)
1105 thereis (memq (org-element-type parent)
1106 '(footnote-reference footnote-definition)))
1107 (let ((num (org-export-get-footnote-number footnote-reference info)))
1108 (format "\\footnotemark[%s]{}\\setcounter{footnote}{%s}" num num)))
1109 ;; Otherwise, define it with \footnote command.
1111 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1112 (unless (eq (org-element-type def) 'org-data)
1113 (setq def (cons 'org-data (cons nil def))))
1114 (concat
1115 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1116 ;; Retrieve all footnote references within the footnote and
1117 ;; add their definition after it, since LaTeX doesn't support
1118 ;; them inside.
1119 (let* (all-refs
1120 search-refs ; for byte-compiler
1121 (search-refs
1122 (function
1123 (lambda (data)
1124 ;; Return a list of all footnote references in DATA.
1125 (org-element-map
1126 data 'footnote-reference
1127 (lambda (ref)
1128 (when (org-export-footnote-first-reference-p ref info)
1129 (push ref all-refs)
1130 (when (eq (org-element-property :type ref) 'standard)
1131 (funcall
1132 search-refs
1133 (org-export-get-footnote-definition ref info)))))
1134 info) (reverse all-refs)))))
1135 (mapconcat
1136 (lambda (ref)
1137 (format
1138 "\\footnotetext[%s]{%s}"
1139 (org-export-get-footnote-number ref info)
1140 (org-trim
1141 (funcall
1142 (if (eq (org-element-property :type ref) 'inline)
1143 'org-export-secondary-string
1144 'org-export-data)
1145 (org-export-get-footnote-definition ref info) info))))
1146 (funcall search-refs def) ""))))))))
1149 ;;;; Headline
1151 (defun org-e-latex-headline (headline contents info)
1152 "Transcode an HEADLINE element from Org to LaTeX.
1153 CONTENTS holds the contents of the headline. INFO is a plist
1154 holding contextual information."
1155 (let* ((class (plist-get info :latex-class))
1156 (level (org-export-get-relative-level headline info))
1157 (numberedp (org-export-numbered-headline-p headline info))
1158 (class-sectionning (assoc class org-e-latex-classes))
1159 ;; Section formatting will set two placeholders: one for the
1160 ;; title and the other for the contents.
1161 (section-fmt
1162 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
1163 (fboundp (nth 2 class-sectionning)))
1164 (funcall (nth 2 class-sectionning) level numberedp)
1165 (nth (1+ level) class-sectionning))))
1166 (cond
1167 ;; No section available for that LEVEL.
1168 ((not sec) nil)
1169 ;; Section format directly returned by a function.
1170 ((stringp sec) sec)
1171 ;; (numbered-section . unnumbered-section)
1172 ((not (consp (cdr sec)))
1173 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1174 ;; (numbered-open numbered-close)
1175 ((= (length sec) 2)
1176 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1177 ;; (num-in num-out no-num-in no-num-out)
1178 ((= (length sec) 4)
1179 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1180 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1181 (text (org-export-secondary-string
1182 (org-element-property :title headline) info))
1183 (todo
1184 (and (plist-get info :with-todo-keywords)
1185 (let ((todo (org-element-property :todo-keyword headline)))
1186 (and todo (org-export-secondary-string todo info)))))
1187 (todo-type (and todo (org-element-property :todo-type headline)))
1188 (tags (and (plist-get info :with-tags)
1189 (org-element-property :tags headline)))
1190 (priority (and (plist-get info :with-priority)
1191 (org-element-property :priority headline)))
1192 ;; Create the headline text.
1193 (full-text (if (functionp org-e-latex-format-headline-function)
1194 ;; User-defined formatting function.
1195 (funcall org-e-latex-format-headline-function
1196 todo todo-type priority text tags)
1197 ;; Default formatting.
1198 (concat
1199 (when todo
1200 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1201 (when priority (format "\\framebox{\\#%c} " priority))
1202 text
1203 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1204 ;; Associate some \label to the headline for internal links.
1205 (headline-label
1206 (format "\\label{sec-%s}\n"
1207 (mapconcat 'number-to-string
1208 (org-export-get-headline-number headline info)
1209 "-")))
1210 (pre-blanks
1211 (make-string (org-element-property :pre-blank headline) 10)))
1212 (cond
1213 ;; Case 1: This is a footnote section: ignore it.
1214 ((org-element-property :footnote-section-p headline) nil)
1215 ;; Case 2. This is a deep sub-tree: export it as a list item.
1216 ;; Also export as items headlines for which no section
1217 ;; format has been found.
1218 ((or (not section-fmt) (org-export-low-level-p headline info))
1219 ;; Build the real contents of the sub-tree.
1220 (let ((low-level-body
1221 (concat
1222 ;; If the headline is the first sibling, start a list.
1223 (when (org-export-first-sibling-p headline info)
1224 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1225 ;; Itemize headline
1226 "\\item " full-text "\n" headline-label pre-blanks contents)))
1227 ;; If headline is not the last sibling simply return
1228 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1229 ;; blank line.
1230 (if (not (org-export-last-sibling-p headline info)) low-level-body
1231 (replace-regexp-in-string
1232 "[ \t\n]*\\'"
1233 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1234 low-level-body))))
1235 ;; Case 3. Standard headline. Export it as a section.
1236 (t (format section-fmt full-text
1237 (concat headline-label pre-blanks contents))))))
1240 ;;;; Horizontal Rule
1242 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1243 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1244 CONTENTS is nil. INFO is a plist holding contextual information."
1245 (let ((attr (mapconcat #'identity
1246 (org-element-property :attr_latex horizontal-rule)
1247 " ")))
1248 (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
1251 ;;;; Inline Babel Call
1253 ;; Inline Babel Calls are ignored.
1256 ;;;; Inline Src Block
1258 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1259 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1260 CONTENTS holds the contents of the item. INFO is a plist holding
1261 contextual information."
1262 (let* ((code (org-element-property :value inline-src-block))
1263 (separator (org-e-latex--find-verb-separator code)))
1264 (cond
1265 ;; Do not use a special package: transcode it verbatim.
1266 ((not org-e-latex-listings)
1267 (concat "\\verb" separator code separator))
1268 ;; Use minted package.
1269 ((eq org-e-latex-listings 'minted)
1270 (let* ((org-lang (org-element-property :language inline-src-block))
1271 (mint-lang (or (cadr (assq (intern org-lang)
1272 org-e-latex-minted-langs))
1273 org-lang))
1274 (options (org-e-latex--make-option-string
1275 org-e-latex-minted-options)))
1276 (concat (format "\\mint%s{%s}"
1277 (if (string= options "") "" (format "[%s]" options))
1278 mint-lang)
1279 separator code separator)))
1280 ;; Use listings package.
1282 ;; Maybe translate language's name.
1283 (let* ((org-lang (org-element-property :language inline-src-block))
1284 (lst-lang (or (cadr (assq (intern org-lang)
1285 org-e-latex-listings-langs))
1286 org-lang))
1287 (options (org-e-latex--make-option-string
1288 (append org-e-latex-listings-options
1289 `(("language" ,lst-lang))))))
1290 (concat (format "\\lstinline[%s]" options)
1291 separator code separator))))))
1294 ;;;; Inlinetask
1296 (defun org-e-latex-inlinetask (inlinetask contents info)
1297 "Transcode an INLINETASK element from Org to LaTeX.
1298 CONTENTS holds the contents of the block. INFO is a plist
1299 holding contextual information."
1300 (let ((title (org-export-secondary-string
1301 (org-element-property :title inlinetask) info))
1302 (todo (and (plist-get info :with-todo-keywords)
1303 (let ((todo (org-element-property
1304 :todo-keyword inlinetask)))
1305 (and todo
1306 (org-export-secondary-string todo info)))))
1307 (todo-type (org-element-property :todo-type inlinetask))
1308 (tags (and (plist-get info :with-tags)
1309 (org-element-property :tags inlinetask)))
1310 (priority (and (plist-get info :with-priority)
1311 (org-element-property :priority inlinetask))))
1312 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1313 ;; with appropriate arguments.
1314 (if (functionp org-e-latex-format-inlinetask-function)
1315 (funcall org-e-latex-format-inlinetask-function
1316 todo todo-type priority title tags contents)
1317 ;; Otherwise, use a default template.
1318 (org-e-latex--wrap-label
1319 inlinetask
1320 (let ((full-title
1321 (concat
1322 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1323 (when priority (format "\\framebox{\\#%c} " priority))
1324 title
1325 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1326 (format (concat "\\begin{center}\n"
1327 "\\fbox{\n"
1328 "\\begin{minipage}[c]{.6\\textwidth}\n"
1329 "%s\n\n"
1330 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1331 "%s"
1332 "\\end{minipage}\n"
1333 "}\n"
1334 "\\end{center}")
1335 full-title contents))))))
1338 ;;;; Italic
1340 (defun org-e-latex-italic (italic contents info)
1341 "Transcode ITALIC from Org to LaTeX.
1342 CONTENTS is the text with italic markup. INFO is a plist holding
1343 contextual information."
1344 (org-e-latex--text-markup contents 'italic))
1347 ;;;; Item
1349 (defun org-e-latex-item (item contents info)
1350 "Transcode an ITEM element from Org to LaTeX.
1351 CONTENTS holds the contents of the item. INFO is a plist holding
1352 contextual information."
1353 ;; Grab `:level' from plain-list properties, which is always the
1354 ;; first element above current item.
1355 (let* ((level (org-element-property :level (org-export-get-parent item info)))
1356 (counter (let ((count (org-element-property :counter item)))
1357 (and count
1358 (< level 4)
1359 (format "\\setcounter{enum%s}{%s}\n"
1360 (nth level '("i" "ii" "iii" "iv"))
1361 (1- count)))))
1362 (checkbox (let ((checkbox (org-element-property :checkbox item)))
1363 (cond ((eq checkbox 'on) "$\\boxtimes$ ")
1364 ((eq checkbox 'off) "$\\Box$ ")
1365 ((eq checkbox 'trans) "$\\boxminus$ "))))
1366 (tag (let ((tag (org-element-property :tag item)))
1367 (and tag
1368 (format "[%s]" (org-export-secondary-string tag info))))))
1369 (concat counter "\\item" tag " " checkbox contents)))
1372 ;;;; Keyword
1374 (defun org-e-latex-keyword (keyword contents info)
1375 "Transcode a KEYWORD element from Org to LaTeX.
1376 CONTENTS is nil. INFO is a plist holding contextual information."
1377 (let ((key (org-element-property :key keyword))
1378 (value (org-element-property :value keyword)))
1379 (cond
1380 ((string= key "LATEX") value)
1381 ((string= key "INDEX") (format "\\index{%s}" value))
1382 ;; Invisible targets.
1383 ((string= key "TARGET") nil)
1384 ((string= key "TOC")
1385 (let ((value (downcase value)))
1386 (cond
1387 ((string-match "\\<headlines\\>" value)
1388 (let ((depth (or (and (string-match "[0-9]+" value)
1389 (string-to-number (match-string 0 value)))
1390 (plist-get info :with-toc))))
1391 (concat
1392 (when (wholenump depth)
1393 (format "\\setcounter{tocdepth}{%s}\n" depth))
1394 "\\tableofcontents")))
1395 ((string= "tables" value) "\\listoftables")
1396 ((string= "figures" value) "\\listoffigures")
1397 ((string= "listings" value)
1398 (cond
1399 ((eq org-e-latex-listings 'minted) "\\listoflistings")
1400 (org-e-latex-listings "\\lstlistoflistings")
1401 ;; At the moment, src blocks with a caption are wrapped
1402 ;; into a figure environment.
1403 (t "\\listoffigures")))))))))
1406 ;;;; Latex Environment
1408 (defun org-e-latex-latex-environment (latex-environment contents info)
1409 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1410 CONTENTS is nil. INFO is a plist holding contextual information."
1411 (let ((label (org-element-property :name latex-environment))
1412 (value (org-remove-indentation
1413 (org-element-property :value latex-environment))))
1414 (if (not (org-string-nw-p label)) value
1415 ;; Environment is labelled: label must be within the environment
1416 ;; (otherwise, a reference pointing to that element will count
1417 ;; the section instead).
1418 (with-temp-buffer
1419 (insert value)
1420 (goto-char (point-min))
1421 (forward-line)
1422 (insert (format "\\label{%s}\n" label))
1423 (buffer-string)))))
1426 ;;;; Latex Fragment
1428 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1429 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1430 CONTENTS is nil. INFO is a plist holding contextual information."
1431 (org-element-property :value latex-fragment))
1434 ;;;; Line Break
1436 (defun org-e-latex-line-break (line-break contents info)
1437 "Transcode a LINE-BREAK object from Org to LaTeX.
1438 CONTENTS is nil. INFO is a plist holding contextual information."
1439 "\\\\")
1442 ;;;; Link
1444 (defun org-e-latex-link--inline-image (link info)
1445 "Return LaTeX code for an inline image.
1446 LINK is the link pointing to the inline image. INFO is a plist
1447 used as a communication channel."
1448 (let* ((parent (org-export-get-parent-paragraph link info))
1449 (path (let ((raw-path (org-element-property :path link)))
1450 (if (not (file-name-absolute-p raw-path)) raw-path
1451 (expand-file-name raw-path))))
1452 (caption (org-e-latex--caption/label-string
1453 (org-element-property :caption parent)
1454 (org-element-property :name parent)
1455 info))
1456 ;; Retrieve latex attributes from the element around.
1457 (attr (let ((raw-attr
1458 (mapconcat #'identity
1459 (org-element-property :attr_latex parent)
1460 " ")))
1461 (unless (string= raw-attr "") raw-attr)))
1462 (disposition
1463 (cond
1464 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1465 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1466 ((or (and attr (string-match "\\<float\\>" attr))
1467 (not (string= caption "")))
1468 'float)))
1469 (placement
1470 (cond
1471 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1472 (org-match-string-no-properties 1 attr))
1473 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1474 ((eq disposition 'float)
1475 (concat "[" org-e-latex-default-figure-position "]"))
1476 (t ""))))
1477 ;; Now clear ATTR from any special keyword and set a default
1478 ;; value if nothing is left.
1479 (setq attr
1480 (if (not attr) ""
1481 (org-trim
1482 (replace-regexp-in-string
1483 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
1484 (setq attr (cond ((not (string= attr "")) attr)
1485 ((eq disposition 'float) "width=0.7\\textwidth")
1486 ((eq disposition 'wrap) "width=0.48\\textwidth")
1487 (t (or org-e-latex-image-default-option ""))))
1488 ;; Return proper string, depending on DISPOSITION.
1489 (case disposition
1490 (wrap (format "\\begin{wrapfigure}%s
1491 \\centering
1492 \\includegraphics[%s]{%s}
1493 %s\\end{wrapfigure}" placement attr path caption))
1494 (mulicolumn (format "\\begin{figure*}%s
1495 \\centering
1496 \\includegraphics[%s]{%s}
1497 %s\\end{figure*}" placement attr path caption))
1498 (float (format "\\begin{figure}%s
1499 \\centering
1500 \\includegraphics[%s]{%s}
1501 %s\\end{figure}" placement attr path caption))
1502 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1504 (defun org-e-latex-link (link desc info)
1505 "Transcode a LINK object from Org to LaTeX.
1507 DESC is the description part of the link, or the empty string.
1508 INFO is a plist holding contextual information. See
1509 `org-export-data'."
1510 (let* ((type (org-element-property :type link))
1511 (raw-path (org-element-property :path link))
1512 ;; Ensure DESC really exists, or set it to nil.
1513 (desc (and (not (string= desc "")) desc))
1514 (imagep (org-export-inline-image-p
1515 link org-e-latex-inline-image-rules))
1516 (path (cond
1517 ((member type '("http" "https" "ftp" "mailto"))
1518 (concat type ":" raw-path))
1519 ((string= type "file")
1520 (when (string-match "\\(.+\\)::.+" raw-path)
1521 (setq raw-path (match-string 1 raw-path)))
1522 (if (file-name-absolute-p raw-path)
1523 (concat "file://" (expand-file-name raw-path))
1524 ;; TODO: Not implemented yet. Concat also:
1525 ;; (org-export-directory :LaTeX info)
1526 (concat "file://" raw-path)))
1527 (t raw-path)))
1528 protocol)
1529 (cond
1530 ;; Image file.
1531 (imagep (org-e-latex-link--inline-image link info))
1532 ;; Radioed target: Target's name is obtained from original raw
1533 ;; link. Path is parsed and transcoded in order to have a proper
1534 ;; display of the contents.
1535 ((string= type "radio")
1536 (format "\\hyperref[%s]{%s}"
1537 (org-export-solidify-link-text path)
1538 (org-export-secondary-string
1539 (org-element-parse-secondary-string
1540 path (cdr (assq 'radio-target org-element-object-restrictions)))
1541 info)))
1542 ;; Links pointing to an headline: Find destination and build
1543 ;; appropriate referencing command.
1544 ((member type '("custom-id" "fuzzy" "id"))
1545 (let ((destination (if (string= type "fuzzy")
1546 (org-export-resolve-fuzzy-link link info)
1547 (org-export-resolve-id-link link info))))
1548 (case (org-element-type destination)
1549 ;; Fuzzy link points nowhere.
1550 ('nil
1551 (format "\\texttt{%s}"
1552 (or desc
1553 (org-export-secondary-string
1554 (org-element-property :raw-link link) info))))
1555 ;; Fuzzy link points to an invisible target.
1556 (keyword nil)
1557 ;; LINK points to an headline. If headlines are numbered
1558 ;; and the link has no description, display headline's
1559 ;; number. Otherwise, display description or headline's
1560 ;; title.
1561 (headline
1562 (let ((label
1563 (format "sec-%s"
1564 (mapconcat
1565 'number-to-string
1566 (org-export-get-headline-number destination info)
1567 "-"))))
1568 (if (and (plist-get info :section-numbers) (not desc))
1569 (format "\\ref{%s}" label)
1570 (format "\\hyperref[%s]{%s}" label
1571 (or desc
1572 (org-export-secondary-string
1573 (org-element-property :title destination) info))))))
1574 ;; Fuzzy link points to a target. Do as above.
1575 (otherwise
1576 (let ((path (org-export-solidify-link-text path)))
1577 (if (not desc) (format "\\ref{%s}" path)
1578 (format "\\hyperref[%s]{%s}" path desc)))))))
1579 ;; Coderef: replace link with the reference name or the
1580 ;; equivalent line number.
1581 ((string= type "coderef")
1582 (format (org-export-get-coderef-format path desc)
1583 (org-export-resolve-coderef path info)))
1584 ;; Link type is handled by a special function.
1585 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1586 (funcall protocol (org-link-unescape path) desc 'latex))
1587 ;; External link with a description part.
1588 ((and path desc) (format "\\href{%s}{%s}" path desc))
1589 ;; External link without a description part.
1590 (path (format "\\url{%s}" path))
1591 ;; No path, only description. Try to do something useful.
1592 (t (format "\\texttt{%s}" desc)))))
1595 ;;;; Macro
1597 (defun org-e-latex-macro (macro contents info)
1598 "Transcode a MACRO element from Org to LaTeX.
1599 CONTENTS is nil. INFO is a plist holding contextual information."
1600 ;; Use available tools.
1601 (org-export-expand-macro macro info))
1604 ;;;; Paragraph
1606 (defun org-e-latex-paragraph (paragraph contents info)
1607 "Transcode a PARAGRAPH element from Org to LaTeX.
1608 CONTENTS is the contents of the paragraph, as a string. INFO is
1609 the plist used as a communication channel."
1610 contents)
1613 ;;;; Plain List
1615 (defun org-e-latex-plain-list (plain-list contents info)
1616 "Transcode a PLAIN-LIST element from Org to LaTeX.
1617 CONTENTS is the contents of the list. INFO is a plist holding
1618 contextual information."
1619 (let* ((type (org-element-property :type plain-list))
1620 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1621 "inparadesc" "asparadesc"))
1622 (paralist-regexp (concat
1623 "\\("
1624 (mapconcat 'identity paralist-types "\\|")
1625 "\\)"))
1626 (attr (mapconcat #'identity
1627 (org-element-property :attr_latex plain-list)
1628 " "))
1629 (latex-type (cond
1630 ((and attr
1631 (string-match
1632 (format "\\<%s\\>" paralist-regexp) attr))
1633 (match-string 1 attr))
1634 ((eq type 'ordered) "enumerate")
1635 ((eq type 'unordered) "itemize")
1636 ((eq type 'descriptive) "description"))))
1637 (org-e-latex--wrap-label
1638 plain-list
1639 (format "\\begin{%s}%s\n%s\\end{%s}"
1640 latex-type
1641 ;; Once special environment, if any, has been removed, the
1642 ;; rest of the attributes will be optional arguments.
1643 ;; They will be put inside square brackets if necessary.
1644 (let ((opt (replace-regexp-in-string
1645 (format " *%s *" paralist-regexp) "" attr)))
1646 (cond ((string= opt "") "")
1647 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1648 (t (format "[%s]" opt))))
1649 contents
1650 latex-type))))
1653 ;;;; Plain Text
1655 (defun org-e-latex-plain-text (text info)
1656 "Transcode a TEXT string from Org to LaTeX.
1657 TEXT is the string to transcode. INFO is a plist holding
1658 contextual information."
1659 ;; Protect %, #, &, $, ~, ^, _, { and }.
1660 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1661 (setq text
1662 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1663 ;; Protect \
1664 (setq text (replace-regexp-in-string
1665 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1666 "$\\backslash$" text nil t 1))
1667 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1668 (let ((case-fold-search nil)
1669 (start 0))
1670 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1671 (setq text (replace-match
1672 (format "\\%s{}" (match-string 1 text)) nil t text)
1673 start (match-end 0))))
1674 ;; Handle quotation marks
1675 (setq text (org-e-latex--quotation-marks text info))
1676 ;; Convert special strings.
1677 (when (plist-get info :with-special-strings)
1678 (while (string-match (regexp-quote "...") text)
1679 (setq text (replace-match "\\ldots{}" nil t text))))
1680 ;; Handle break preservation if required.
1681 (when (plist-get info :preserve-breaks)
1682 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1683 text)))
1684 ;; Return value.
1685 text)
1688 ;;;; Planning
1690 (defun org-e-latex-planning (planning contents info)
1691 "Transcode a PLANNING element from Org to LaTeX.
1692 CONTENTS is nil. INFO is a plist holding contextual
1693 information."
1694 (concat
1695 "\\noindent"
1696 (mapconcat
1697 'identity
1698 (delq nil
1699 (list
1700 (let ((closed (org-element-property :closed planning)))
1701 (when closed
1702 (concat
1703 (format "\\textbf{%s} " org-closed-string)
1704 (format org-e-latex-inactive-timestamp-format
1705 (org-translate-time closed)))))
1706 (let ((deadline (org-element-property :deadline planning)))
1707 (when deadline
1708 (concat
1709 (format "\\textbf{%s} " org-deadline-string)
1710 (format org-e-latex-active-timestamp-format
1711 (org-translate-time deadline)))))
1712 (let ((scheduled (org-element-property :scheduled planning)))
1713 (when scheduled
1714 (concat
1715 (format "\\textbf{%s} " org-scheduled-string)
1716 (format org-e-latex-active-timestamp-format
1717 (org-translate-time scheduled)))))))
1718 " ")
1719 "\\\\"))
1722 ;;;; Property Drawer
1724 (defun org-e-latex-property-drawer (property-drawer contents info)
1725 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1726 CONTENTS is nil. INFO is a plist holding contextual
1727 information."
1728 ;; The property drawer isn't exported but we want separating blank
1729 ;; lines nonetheless.
1733 ;;;; Quote Block
1735 (defun org-e-latex-quote-block (quote-block contents info)
1736 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1737 CONTENTS holds the contents of the block. INFO is a plist
1738 holding contextual information."
1739 (org-e-latex--wrap-label
1740 quote-block
1741 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1744 ;;;; Quote Section
1746 (defun org-e-latex-quote-section (quote-section contents info)
1747 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1748 CONTENTS is nil. INFO is a plist holding contextual information."
1749 (let ((value (org-remove-indentation
1750 (org-element-property :value quote-section))))
1751 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1754 ;;;; Section
1756 (defun org-e-latex-section (section contents info)
1757 "Transcode a SECTION element from Org to LaTeX.
1758 CONTENTS holds the contents of the section. INFO is a plist
1759 holding contextual information."
1760 contents)
1763 ;;;; Radio Target
1765 (defun org-e-latex-radio-target (radio-target text info)
1766 "Transcode a RADIO-TARGET object from Org to LaTeX.
1767 TEXT is the text of the target. INFO is a plist holding
1768 contextual information."
1769 (format "\\label{%s}%s"
1770 (org-export-solidify-link-text
1771 (org-element-property :value radio-target))
1772 text))
1775 ;;;; Special Block
1777 (defun org-e-latex-special-block (special-block contents info)
1778 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1779 CONTENTS holds the contents of the block. INFO is a plist
1780 holding contextual information."
1781 (let ((type (downcase (org-element-property :type special-block))))
1782 (org-e-latex--wrap-label
1783 special-block
1784 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
1787 ;;;; Src Block
1789 (defun org-e-latex-src-block (src-block contents info)
1790 "Transcode a SRC-BLOCK element from Org to LaTeX.
1791 CONTENTS holds the contents of the item. INFO is a plist holding
1792 contextual information."
1793 (let* ((lang (org-element-property :language src-block))
1794 (caption (org-element-property :caption src-block))
1795 (label (org-element-property :name src-block))
1796 (custom-env (and lang
1797 (cadr (assq (intern lang)
1798 org-e-latex-custom-lang-environments))))
1799 (num-start (case (org-element-property :number-lines src-block)
1800 (continued (org-export-get-loc src-block info))
1801 (new 0)))
1802 (retain-labels (org-element-property :retain-labels src-block)))
1803 (cond
1804 ;; Case 1. No source fontification.
1805 ((not org-e-latex-listings)
1806 (let ((caption-str (org-e-latex--caption/label-string caption label info))
1807 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
1808 (format
1809 (or float-env "%s")
1810 (concat caption-str
1811 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1812 (org-export-format-code-default src-block info))))))
1813 ;; Case 2. Custom environment.
1814 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
1815 custom-env
1816 (org-export-format-code-default src-block info)
1817 custom-env))
1818 ;; Case 3. Use minted package.
1819 ((eq org-e-latex-listings 'minted)
1820 (let ((float-env (when (or label caption)
1821 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1822 (org-e-latex--caption/label-string
1823 caption label info))))
1824 (body
1825 (format
1826 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1827 ;; Options.
1828 (org-e-latex--make-option-string
1829 (if (not num-start) org-e-latex-minted-options
1830 (append `(("linenos")
1831 ("firstnumber" ,(number-to-string (1+ num-start))))
1832 org-e-latex-minted-options)))
1833 ;; Language.
1834 (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
1835 ;; Source code.
1836 (let* ((code-info (org-export-unravel-code src-block))
1837 (max-width
1838 (apply 'max
1839 (mapcar 'length
1840 (org-split-string (car code-info) "\n")))))
1841 (org-export-format-code
1842 (car code-info)
1843 (lambda (loc num ref)
1844 (concat
1846 (when ref
1847 ;; Ensure references are flushed to the right,
1848 ;; separated with 6 spaces from the widest line
1849 ;; of code.
1850 (concat (make-string (+ (- max-width (length loc)) 6) ? )
1851 (format "(%s)" ref)))))
1852 nil (and retain-labels (cdr code-info)))))))
1853 ;; Return value.
1854 (if float-env (format float-env body) body)))
1855 ;; Case 4. Use listings package.
1857 (let ((lst-lang
1858 (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
1859 (caption-str
1860 (when caption
1861 (let ((main (org-export-secondary-string (car caption) info)))
1862 (if (not (cdr caption)) (format "{%s}" main)
1863 (format
1864 "{[%s]%s}"
1865 (org-export-secondary-string (cdr caption) info)
1866 main))))))
1867 (concat
1868 ;; Options.
1869 (format "\\lstset{%s}\n"
1870 (org-e-latex--make-option-string
1871 (append org-e-latex-listings-options
1872 `(("language" ,lst-lang))
1873 (when label `(("label" ,label)))
1874 (when caption-str `(("caption" ,caption-str)))
1875 (cond ((not num-start) '(("numbers" "none")))
1876 ((zerop num-start) '(("numbers" "left")))
1877 (t `(("numbers" "left")
1878 ("firstnumber"
1879 ,(number-to-string (1+ num-start)))))))))
1880 ;; Source code.
1881 (format
1882 "\\begin{lstlisting}\n%s\\end{lstlisting}"
1883 (let* ((code-info (org-export-unravel-code src-block))
1884 (max-width
1885 (apply 'max
1886 (mapcar 'length
1887 (org-split-string (car code-info) "\n")))))
1888 (org-export-format-code
1889 (car code-info)
1890 (lambda (loc num ref)
1891 (concat
1893 (when ref
1894 ;; Ensure references are flushed to the right,
1895 ;; separated with 6 spaces from the widest line of
1896 ;; code
1897 (concat (make-string (+ (- max-width (length loc)) 6) ? )
1898 (format "(%s)" ref)))))
1899 nil (and retain-labels (cdr code-info)))))))))))
1902 ;;;; Statistics Cookie
1904 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
1905 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1906 CONTENTS is nil. INFO is a plist holding contextual information."
1907 (org-element-property :value statistics-cookie))
1910 ;;;; Strike-Through
1912 (defun org-e-latex-strike-through (strike-through contents info)
1913 "Transcode STRIKE-THROUGH from Org to LaTeX.
1914 CONTENTS is the text with strike-through markup. INFO is a plist
1915 holding contextual information."
1916 (org-e-latex--text-markup contents 'strike-through))
1919 ;;;; Subscript
1921 (defun org-e-latex-subscript (subscript contents info)
1922 "Transcode a SUBSCRIPT object from Org to LaTeX.
1923 CONTENTS is the contents of the object. INFO is a plist holding
1924 contextual information."
1925 (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
1928 ;;;; Superscript
1930 (defun org-e-latex-superscript (superscript contents info)
1931 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1932 CONTENTS is the contents of the object. INFO is a plist holding
1933 contextual information."
1934 (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
1937 ;;;; Table
1939 (defun org-e-latex-table--format-string (table info)
1940 "Return an appropriate format string for TABLE.
1942 TABLE-INFO is the plist containing format info about the table,
1943 as returned by `org-export-table-format-info'. INFO is a plist
1944 used as a communication channel.
1946 The format string leaves one placeholder for the body of the
1947 table."
1948 (let* ((label (org-element-property :name table))
1949 (caption (org-e-latex--caption/label-string
1950 (org-element-property :caption table) label info))
1951 (attr (mapconcat 'identity
1952 (org-element-property :attr_latex table)
1953 " "))
1954 ;; Determine alignment string.
1955 (alignment (org-e-latex-table--align-string table info))
1956 ;; Determine environment for the table: longtable, tabular...
1957 (table-env (cond
1958 ((not attr) org-e-latex-default-table-environment)
1959 ((string-match "\\<longtable\\>" attr) "longtable")
1960 ((string-match "\\<tabular.?\\>" attr)
1961 (org-match-string-no-properties 0 attr))
1962 (t org-e-latex-default-table-environment)))
1963 ;; If table is a float, determine environment: table, table*
1964 ;; or sidewaystable.
1965 (float-env (cond
1966 ((string= "longtable" table-env) nil)
1967 ((and attr (string-match "\\<sidewaystable\\>" attr))
1968 "sidewaystables")
1969 ((and attr
1970 (or (string-match (regexp-quote "table*") attr)
1971 (string-match "\\<multicolumn\\>" attr)))
1972 "table*")
1973 ((or (not (string= caption "")) label) "table")))
1974 ;; Extract others display options.
1975 (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
1976 (org-match-string-no-properties 1 attr)))
1977 (placement
1978 (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1979 (org-match-string-no-properties 1 attr)
1980 (format "[%s]" org-e-latex-default-figure-position))))
1981 ;; Prepare the final format string for the table.
1982 (cond
1983 ;; Longtable.
1984 ((string= "longtable" table-env)
1985 (format
1986 "\\begin{longtable}{%s}\n%s%%s%s\\end{longtable}"
1987 alignment
1988 (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
1989 (concat (org-trim caption) "\\\\\n"))
1990 (if (or org-e-latex-table-caption-above (string= "" caption)) ""
1991 (concat (org-trim caption) "\\\\\n"))))
1992 ;; Others.
1993 (t (concat (when float-env
1994 (concat
1995 (format "\\begin{%s}%s\n" float-env placement)
1996 (if org-e-latex-table-caption-above caption "")))
1997 (when org-e-latex-tables-centered "\\begin{center}\n")
1998 (format "\\begin{%s}%s{%s}\n%%s\\end{%s}"
1999 table-env
2000 (if width (format "{%s}" width) "") alignment table-env)
2001 (when org-e-latex-tables-centered "\n\\end{center}")
2002 (when float-env
2003 (concat (if org-e-latex-table-caption-above "" caption)
2004 (format "\n\\end{%s}" float-env))))))))
2006 (defun org-e-latex-table--align-string (table info)
2007 "Return an appropriate LaTeX alignment string.
2008 TABLE is the considered table. INFO is a plist used as
2009 a communication channel."
2010 (let ((attr (mapconcat 'identity
2011 (org-element-property :attr_latex table)
2012 " ")))
2013 (if (and attr (string-match "\\<align=\\(\\S-+\\)" attr))
2014 (match-string 1 attr)
2015 (let (alignment)
2016 ;; Extract column groups and alignment from first (non-rule)
2017 ;; row.
2018 (org-element-map
2019 (org-element-map table 'table-row 'identity info 'first-match)
2020 'table-cell
2021 (lambda (cell)
2022 (let ((borders (org-export-table-cell-borders cell info)))
2023 ;; Check left border for the first cell only.
2024 (when (and (memq 'left borders) (not alignment))
2025 (push "|" alignment))
2026 (push (case (org-export-table-cell-alignment cell info)
2027 (left "l")
2028 (right "r")
2029 (center "c"))
2030 alignment)
2031 (when (memq 'right borders) (push "|" alignment))))
2032 info)
2033 (apply 'concat (reverse alignment))))))
2035 (defun org-e-latex-table (table contents info)
2036 "Transcode a TABLE element from Org to LaTeX.
2037 CONTENTS is nil. INFO is a plist holding contextual information."
2038 (cond
2039 ;; Case 1: verbatim table.
2040 ((or org-e-latex-tables-verbatim
2041 (let ((attr (mapconcat 'identity
2042 (org-element-property :attr_latex table)
2043 " ")))
2044 (and attr (string-match "\\<verbatim\\>" attr))))
2045 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2046 ;; Re-create table, without affiliated keywords.
2047 (org-trim
2048 (org-element-interpret-data
2049 `(org-data nil (table nil ,@(org-element-contents table)))))))
2050 ;; Case 2: table.el table. Convert it using appropriate tools.
2051 ((eq (org-element-property :type table) 'table.el)
2052 (require 'table)
2053 ;; Ensure "*org-export-table*" buffer is empty.
2054 (with-current-buffer (get-buffer-create "*org-export-table*")
2055 (erase-buffer))
2056 (let ((output (with-temp-buffer
2057 (insert (org-element-property :value table))
2058 (goto-char 1)
2059 (re-search-forward "^[ \t]*|[^|]" nil t)
2060 (table-generate-source 'latex "*org-export-table*")
2061 (with-current-buffer "*org-export-table*"
2062 (org-trim (buffer-string))))))
2063 (kill-buffer (get-buffer "*org-export-table*"))
2064 ;; Remove left out comments.
2065 (while (string-match "^%.*\n" output)
2066 (setq output (replace-match "" t t output)))
2067 ;; When the "rmlines" attribute is provided, remove all hlines
2068 ;; but the the one separating heading from the table body.
2069 (let ((attr (mapconcat 'identity
2070 (org-element-property :attr_latex table)
2071 " ")))
2072 (when (and attr (string-match "\\<rmlines\\>" attr))
2073 (let ((n 0) (pos 0))
2074 (while (and (< (length output) pos)
2075 (setq pos (string-match "^\\\\hline\n?" output pos)))
2076 (incf n)
2077 (unless (= n 2)
2078 (setq output (replace-match "" nil nil output)))))))
2079 (if (not org-e-latex-tables-centered) output
2080 (format "\\begin{center}\n%s\n\\end{center}" output))))
2081 ;; Case 3: Standard table.
2082 (t (format (org-e-latex-table--format-string table info) contents))))
2085 ;;;; Table Cell
2087 (defun org-e-latex-table-cell (table-cell contents info)
2088 "Transcode a TABLE-CELL element from Org to LaTeX.
2089 CONTENTS is the cell contents. INFO is a plist used as
2090 a communication channel."
2091 (concat (if (and contents
2092 org-e-latex-table-scientific-notation
2093 (string-match orgtbl-exp-regexp contents))
2094 ;; Use appropriate format string for scientific
2095 ;; notation.
2096 (format org-e-latex-table-scientific-notation
2097 (match-string 1 contents)
2098 (match-string 2 contents))
2099 contents)
2100 (when (org-export-get-next-element table-cell info) " & ")))
2103 ;;;; Table Row
2105 (defun org-e-latex-table-row (table-row contents info)
2106 "Transcode a TABLE-ROW element from Org to LaTeX.
2107 CONTENTS is the contents of the row. INFO is a plist used as
2108 a communication channel."
2109 ;; Rules are ignored since table separators are deduced from
2110 ;; borders of the current row.
2111 (when (eq (org-element-property :type table-row) 'standard)
2112 (let* ((attr (mapconcat 'identity
2113 (org-element-property
2114 :attr_latex (org-export-get-parent table-row info))
2115 " "))
2116 (longtablep (and attr (string-match "\\<longtable\\>" attr)))
2117 (booktabsp
2118 (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
2119 org-e-latex-tables-booktabs))
2120 ;; TABLE-ROW's borders are extracted from its first cell.
2121 (borders
2122 (org-export-table-cell-borders
2123 (car (org-element-contents table-row)) info)))
2124 (concat
2125 ;; When BOOKTABS are activated enforce top-rule even when no
2126 ;; hline was specifically marked.
2127 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2128 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2129 contents "\\\\\n"
2130 (cond
2131 ;; Special case for long tables. Define header and footers.
2132 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2133 (format "%s
2134 \\endhead
2135 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2136 \\endfoot
2137 \\endlastfoot"
2138 (if booktabsp "\\midrule" "\\hline")
2139 (if booktabsp "\\midrule" "\\hline")
2140 ;; Number of columns.
2141 (cdr (org-export-table-dimensions
2142 (org-export-get-parent-table table-row info) info))))
2143 ;; When BOOKTABS are activated enforce bottom rule even when
2144 ;; no hline was specifically marked.
2145 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2146 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2147 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2150 ;;;; Target
2152 (defun org-e-latex-target (target contents info)
2153 "Transcode a TARGET object from Org to LaTeX.
2154 CONTENTS is nil. INFO is a plist holding contextual
2155 information."
2156 (format "\\label{%s}"
2157 (org-export-solidify-link-text (org-element-property :value target))))
2160 ;;;; Time-stamp
2162 (defun org-e-latex-time-stamp (time-stamp contents info)
2163 "Transcode a TIME-STAMP object from Org to LaTeX.
2164 CONTENTS is nil. INFO is a plist holding contextual
2165 information."
2166 (let ((value (org-translate-time (org-element-property :value time-stamp)))
2167 (type (org-element-property :type time-stamp)))
2168 (cond ((memq type '(active active-range))
2169 (format org-e-latex-active-timestamp-format value))
2170 ((memq type '(inactive inactive-range))
2171 (format org-e-latex-inactive-timestamp-format value))
2172 (t (format org-e-latex-diary-timestamp-format value)))))
2175 ;;;; Underline
2177 (defun org-e-latex-underline (underline contents info)
2178 "Transcode UNDERLINE from Org to LaTeX.
2179 CONTENTS is the text with underline markup. INFO is a plist
2180 holding contextual information."
2181 (org-e-latex--text-markup contents 'underline))
2184 ;;;; Verbatim
2186 (defun org-e-latex-verbatim (verbatim contents info)
2187 "Transcode a VERBATIM object from Org to LaTeX.
2188 CONTENTS is nil. INFO is a plist used as a communication
2189 channel."
2190 (org-e-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2193 ;;;; Verse Block
2195 (defun org-e-latex-verse-block (verse-block contents info)
2196 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2197 CONTENTS is verse block contents. INFO is a plist holding
2198 contextual information."
2199 (org-e-latex--wrap-label
2200 verse-block
2201 ;; In a verse environment, add a line break to each newline
2202 ;; character and change each white space at beginning of a line
2203 ;; into a space of 1 em. Also change each blank line with
2204 ;; a vertical space of 1 em.
2205 (progn
2206 (setq contents (replace-regexp-in-string
2207 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2208 (replace-regexp-in-string
2209 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2210 (while (string-match "^[ \t]+" contents)
2211 (let ((new-str (format "\\hspace*{%dem}"
2212 (length (match-string 0 contents)))))
2213 (setq contents (replace-match new-str nil t contents))))
2214 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2218 ;;; Interactive functions
2220 (defun org-e-latex-export-to-latex
2221 (&optional subtreep visible-only body-only ext-plist pub-dir)
2222 "Export current buffer to a LaTeX file.
2224 If narrowing is active in the current buffer, only export its
2225 narrowed part.
2227 If a region is active, export that region.
2229 When optional argument SUBTREEP is non-nil, export the sub-tree
2230 at point, extracting information from the headline properties
2231 first.
2233 When optional argument VISIBLE-ONLY is non-nil, don't export
2234 contents of hidden elements.
2236 When optional argument BODY-ONLY is non-nil, only write code
2237 between \"\\begin{document}\" and \"\\end{document}\".
2239 EXT-PLIST, when provided, is a property list with external
2240 parameters overriding Org default settings, but still inferior to
2241 file-local settings.
2243 When optional argument PUB-DIR is set, use it as the publishing
2244 directory.
2246 Return output file's name."
2247 (interactive)
2248 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
2249 (org-export-to-file
2250 'e-latex outfile subtreep visible-only body-only ext-plist)))
2252 (defun org-e-latex-export-to-pdf
2253 (&optional subtreep visible-only body-only ext-plist pub-dir)
2254 "Export current buffer to LaTeX then process through to PDF.
2256 If narrowing is active in the current buffer, only export its
2257 narrowed part.
2259 If a region is active, export that region.
2261 When optional argument SUBTREEP is non-nil, export the sub-tree
2262 at point, extracting information from the headline properties
2263 first.
2265 When optional argument VISIBLE-ONLY is non-nil, don't export
2266 contents of hidden elements.
2268 When optional argument BODY-ONLY is non-nil, only write code
2269 between \"\\begin{document}\" and \"\\end{document}\".
2271 EXT-PLIST, when provided, is a property list with external
2272 parameters overriding Org default settings, but still inferior to
2273 file-local settings.
2275 When optional argument PUB-DIR is set, use it as the publishing
2276 directory.
2278 Return PDF file's name."
2279 (interactive)
2280 (org-e-latex-compile
2281 (org-e-latex-export-to-latex
2282 subtreep visible-only body-only ext-plist pub-dir)))
2284 (defun org-e-latex-compile (texfile)
2285 "Compile a TeX file.
2287 TEXFILE is the name of the file being compiled. Processing is
2288 done through the command specified in `org-e-latex-pdf-process'.
2290 Return PDF file name or an error if it couldn't be produced."
2291 (let* ((wconfig (current-window-configuration))
2292 (texfile (file-truename texfile))
2293 (base (file-name-sans-extension texfile))
2294 errors)
2295 (message (format "Processing LaTeX file %s ..." texfile))
2296 (unwind-protect
2297 (progn
2298 (cond
2299 ;; A function is provided: Apply it.
2300 ((functionp org-e-latex-pdf-process)
2301 (funcall org-e-latex-pdf-process (shell-quote-argument texfile)))
2302 ;; A list is provided: Replace %b, %f and %o with appropriate
2303 ;; values in each command before applying it. Output is
2304 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2305 ((consp org-e-latex-pdf-process)
2306 (let* ((out-dir (or (file-name-directory texfile) "./"))
2307 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2308 (mapc
2309 (lambda (command)
2310 (shell-command
2311 (replace-regexp-in-string
2312 "%b" (shell-quote-argument base)
2313 (replace-regexp-in-string
2314 "%f" (shell-quote-argument texfile)
2315 (replace-regexp-in-string
2316 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2317 outbuf))
2318 org-e-latex-pdf-process)
2319 ;; Collect standard errors from output buffer.
2320 (setq errors (org-e-latex-collect-errors outbuf))))
2321 (t (error "No valid command to process to PDF")))
2322 (let ((pdffile (concat base ".pdf")))
2323 ;; Check for process failure. Provide collected errors if
2324 ;; possible.
2325 (if (not (file-exists-p pdffile))
2326 (error (concat (format "PDF file %s wasn't produced" pdffile)
2327 (when errors (concat ": " errors))))
2328 ;; Else remove log files, when specified, and signal end of
2329 ;; process to user, along with any error encountered.
2330 (when org-e-latex-remove-logfiles
2331 (dolist (ext org-e-latex-logfiles-extensions)
2332 (let ((file (concat base "." ext)))
2333 (when (file-exists-p file) (delete-file file)))))
2334 (message (concat "Process completed"
2335 (if (not errors) "."
2336 (concat " with errors: " errors)))))
2337 ;; Return output file name.
2338 pdffile))
2339 (set-window-configuration wconfig))))
2341 (defun org-e-latex-collect-errors (buffer)
2342 "Collect some kind of errors from \"pdflatex\" command output.
2344 BUFFER is the buffer containing output.
2346 Return collected error types as a string, or nil if there was
2347 none."
2348 (with-current-buffer buffer
2349 (save-excursion
2350 (goto-char (point-max))
2351 ;; Find final "pdflatex" run.
2352 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
2353 (let ((case-fold-search t)
2354 (errors ""))
2355 (when (save-excursion
2356 (re-search-forward "Reference.*?undefined" nil t))
2357 (setq errors (concat errors " [undefined reference]")))
2358 (when (save-excursion
2359 (re-search-forward "Citation.*?undefined" nil t))
2360 (setq errors (concat errors " [undefined citation]")))
2361 (when (save-excursion
2362 (re-search-forward "Undefined control sequence" nil t))
2363 (setq errors (concat errors " [undefined control sequence]")))
2364 (when (save-excursion
2365 (re-search-forward "^! LaTeX.*?Error" nil t))
2366 (setq errors (concat errors " [LaTeX error]")))
2367 (when (save-excursion
2368 (re-search-forward "^! Package.*?Error" nil t))
2369 (setq errors (concat errors " [package error]")))
2370 (and (org-string-nw-p errors) (org-trim errors)))))))
2373 (provide 'org-e-latex)
2374 ;;; org-e-latex.el ends here