org-compat.el: Silent the byte-compiler about a warning related to XEmacs support.
[org-mode.git] / contrib / lisp / org-e-latex.el
blob26b659c366a764b45452662700dfee8365616380
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 ;;;; Bold
946 (defun org-e-latex-bold (bold contents info)
947 "Transcode BOLD from Org to LaTeX.
948 CONTENTS is the text with bold markup. INFO is a plist holding
949 contextual information."
950 (org-e-latex--text-markup contents 'bold))
953 ;;;; Center Block
955 (defun org-e-latex-center-block (center-block contents info)
956 "Transcode a CENTER-BLOCK element from Org to LaTeX.
957 CONTENTS holds the contents of the center block. INFO is a plist
958 holding contextual information."
959 (org-e-latex--wrap-label
960 center-block
961 (format "\\begin{center}\n%s\\end{center}" contents)))
964 ;;;; Code
966 (defun org-e-latex-code (code contents info)
967 "Transcode a CODE object from Org to LaTeX.
968 CONTENTS is nil. INFO is a plist used as a communication
969 channel."
970 (org-e-latex--text-markup (org-element-property :value code) 'code))
973 ;;;; Comment
975 ;; Comments are ignored.
978 ;;;; Comment Block
980 ;; Comment Blocks are ignored.
983 ;;;; Drawer
985 (defun org-e-latex-drawer (drawer contents info)
986 "Transcode a DRAWER element from Org to LaTeX.
987 CONTENTS holds the contents of the block. INFO is a plist
988 holding contextual information."
989 (let* ((name (org-element-property :drawer-name drawer))
990 (output (if (functionp org-e-latex-format-drawer-function)
991 (funcall org-e-latex-format-drawer-function
992 name contents)
993 ;; If there's no user defined function: simply
994 ;; display contents of the drawer.
995 contents)))
996 (org-e-latex--wrap-label drawer output)))
999 ;;;; Dynamic Block
1001 (defun org-e-latex-dynamic-block (dynamic-block contents info)
1002 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1003 CONTENTS holds the contents of the block. INFO is a plist
1004 holding contextual information. See `org-export-data'."
1005 (org-e-latex--wrap-label dynamic-block contents))
1008 ;;;; Entity
1010 (defun org-e-latex-entity (entity contents info)
1011 "Transcode an ENTITY object from Org to LaTeX.
1012 CONTENTS are the definition itself. INFO is a plist holding
1013 contextual information."
1014 (let ((ent (org-element-property :latex entity)))
1015 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
1018 ;;;; Example Block
1020 (defun org-e-latex-example-block (example-block contents info)
1021 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1022 CONTENTS is nil. INFO is a plist holding contextual
1023 information."
1024 (org-e-latex--wrap-label
1025 example-block
1026 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1027 (org-export-format-code-default example-block info))))
1030 ;;;; Export Snippet
1032 (defun org-e-latex-export-snippet (export-snippet contents info)
1033 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1034 CONTENTS is nil. INFO is a plist holding contextual information."
1035 (when (eq (org-export-snippet-backend export-snippet) 'e-latex)
1036 (org-element-property :value export-snippet)))
1039 ;;;; Export Block
1041 (defun org-e-latex-export-block (export-block contents info)
1042 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1043 CONTENTS is nil. INFO is a plist holding contextual information."
1044 (when (string= (org-element-property :type export-block) "latex")
1045 (org-remove-indentation (org-element-property :value export-block))))
1048 ;;;; Fixed Width
1050 (defun org-e-latex-fixed-width (fixed-width contents info)
1051 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1052 CONTENTS is nil. INFO is a plist holding contextual information."
1053 (let* ((value (org-element-normalize-string
1054 (replace-regexp-in-string
1055 "^[ \t]*: ?" ""
1056 (org-element-property :value fixed-width)))))
1057 (org-e-latex--wrap-label
1058 fixed-width (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1061 ;;;; Footnote Definition
1063 ;; Footnote Definitions are ignored.
1066 ;;;; Footnote Reference
1068 (defun org-e-latex-footnote-reference (footnote-reference contents info)
1069 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1070 CONTENTS is nil. INFO is a plist holding contextual information."
1071 (concat
1072 ;; Insert separator between two footnotes in a row.
1073 (let ((prev (org-export-get-previous-element footnote-reference info)))
1074 (when (eq (org-element-type prev) 'footnote-reference)
1075 org-e-latex-footnote-separator))
1076 (cond
1077 ;; Use \footnotemark if the footnote has already been defined.
1078 ((not (org-export-footnote-first-reference-p footnote-reference info))
1079 (format "\\footnotemark[%s]{}"
1080 (org-export-get-footnote-number footnote-reference info)))
1081 ;; Use also \footnotemark if reference is within another footnote
1082 ;; reference or footnote definition.
1083 ((loop for parent in (org-export-get-genealogy footnote-reference info)
1084 thereis (memq (org-element-type parent)
1085 '(footnote-reference footnote-definition)))
1086 (let ((num (org-export-get-footnote-number footnote-reference info)))
1087 (format "\\footnotemark[%s]{}\\setcounter{footnote}{%s}" num num)))
1088 ;; Otherwise, define it with \footnote command.
1090 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1091 (unless (eq (org-element-type def) 'org-data)
1092 (setq def (cons 'org-data (cons nil def))))
1093 (concat
1094 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1095 ;; Retrieve all footnote references within the footnote and
1096 ;; add their definition after it, since LaTeX doesn't support
1097 ;; them inside.
1098 (let* (all-refs
1099 search-refs ; for byte-compiler
1100 (search-refs
1101 (function
1102 (lambda (data)
1103 ;; Return a list of all footnote references in DATA.
1104 (org-element-map
1105 data 'footnote-reference
1106 (lambda (ref)
1107 (when (org-export-footnote-first-reference-p ref info)
1108 (push ref all-refs)
1109 (when (eq (org-element-property :type ref) 'standard)
1110 (funcall
1111 search-refs
1112 (org-export-get-footnote-definition ref info)))))
1113 info) (reverse all-refs)))))
1114 (mapconcat
1115 (lambda (ref)
1116 (format
1117 "\\footnotetext[%s]{%s}"
1118 (org-export-get-footnote-number ref info)
1119 (org-trim
1120 (funcall
1121 (if (eq (org-element-property :type ref) 'inline)
1122 'org-export-secondary-string
1123 'org-export-data)
1124 (org-export-get-footnote-definition ref info) info))))
1125 (funcall search-refs def) ""))))))))
1128 ;;;; Headline
1130 (defun org-e-latex-headline (headline contents info)
1131 "Transcode an HEADLINE element from Org to LaTeX.
1132 CONTENTS holds the contents of the headline. INFO is a plist
1133 holding contextual information."
1134 (let* ((class (plist-get info :latex-class))
1135 (level (org-export-get-relative-level headline info))
1136 (numberedp (org-export-numbered-headline-p headline info))
1137 (class-sectionning (assoc class org-e-latex-classes))
1138 ;; Section formatting will set two placeholders: one for the
1139 ;; title and the other for the contents.
1140 (section-fmt
1141 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
1142 (fboundp (nth 2 class-sectionning)))
1143 (funcall (nth 2 class-sectionning) level numberedp)
1144 (nth (1+ level) class-sectionning))))
1145 (cond
1146 ;; No section available for that LEVEL.
1147 ((not sec) nil)
1148 ;; Section format directly returned by a function.
1149 ((stringp sec) sec)
1150 ;; (numbered-section . unnumbered-section)
1151 ((not (consp (cdr sec)))
1152 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1153 ;; (numbered-open numbered-close)
1154 ((= (length sec) 2)
1155 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1156 ;; (num-in num-out no-num-in no-num-out)
1157 ((= (length sec) 4)
1158 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1159 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1160 (text (org-export-secondary-string
1161 (org-element-property :title headline) info))
1162 (todo
1163 (and (plist-get info :with-todo-keywords)
1164 (let ((todo (org-element-property :todo-keyword headline)))
1165 (and todo (org-export-secondary-string todo info)))))
1166 (todo-type (and todo (org-element-property :todo-type headline)))
1167 (tags (and (plist-get info :with-tags)
1168 (org-element-property :tags headline)))
1169 (priority (and (plist-get info :with-priority)
1170 (org-element-property :priority headline)))
1171 ;; Create the headline text.
1172 (full-text (if (functionp org-e-latex-format-headline-function)
1173 ;; User-defined formatting function.
1174 (funcall org-e-latex-format-headline-function
1175 todo todo-type priority text tags)
1176 ;; Default formatting.
1177 (concat
1178 (when todo
1179 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1180 (when priority (format "\\framebox{\\#%c} " priority))
1181 text
1182 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1183 ;; Associate some \label to the headline for internal links.
1184 (headline-label
1185 (format "\\label{sec-%s}\n"
1186 (mapconcat 'number-to-string
1187 (org-export-get-headline-number headline info)
1188 "-")))
1189 (pre-blanks
1190 (make-string (org-element-property :pre-blank headline) 10)))
1191 (cond
1192 ;; Case 1: This is a footnote section: ignore it.
1193 ((org-element-property :footnote-section-p headline) nil)
1194 ;; Case 2. This is a deep sub-tree: export it as a list item.
1195 ;; Also export as items headlines for which no section
1196 ;; format has been found.
1197 ((or (not section-fmt) (org-export-low-level-p headline info))
1198 ;; Build the real contents of the sub-tree.
1199 (let ((low-level-body
1200 (concat
1201 ;; If the headline is the first sibling, start a list.
1202 (when (org-export-first-sibling-p headline info)
1203 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1204 ;; Itemize headline
1205 "\\item " full-text "\n" headline-label pre-blanks contents)))
1206 ;; If headline is not the last sibling simply return
1207 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1208 ;; blank line.
1209 (if (not (org-export-last-sibling-p headline info)) low-level-body
1210 (replace-regexp-in-string
1211 "[ \t\n]*\\'"
1212 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1213 low-level-body))))
1214 ;; Case 3. Standard headline. Export it as a section.
1215 (t (format section-fmt full-text
1216 (concat headline-label pre-blanks contents))))))
1219 ;;;; Horizontal Rule
1221 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1222 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1223 CONTENTS is nil. INFO is a plist holding contextual information."
1224 (let ((attr (mapconcat #'identity
1225 (org-element-property :attr_latex horizontal-rule)
1226 " ")))
1227 (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
1230 ;;;; Inline Babel Call
1232 ;; Inline Babel Calls are ignored.
1235 ;;;; Inline Src Block
1237 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1238 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1239 CONTENTS holds the contents of the item. INFO is a plist holding
1240 contextual information."
1241 (let* ((code (org-element-property :value inline-src-block))
1242 (separator (org-e-latex--find-verb-separator code)))
1243 (cond
1244 ;; Do not use a special package: transcode it verbatim.
1245 ((not org-e-latex-listings)
1246 (concat "\\verb" separator code separator))
1247 ;; Use minted package.
1248 ((eq org-e-latex-listings 'minted)
1249 (let* ((org-lang (org-element-property :language inline-src-block))
1250 (mint-lang (or (cadr (assq (intern org-lang)
1251 org-e-latex-minted-langs))
1252 org-lang))
1253 (options (org-e-latex--make-option-string
1254 org-e-latex-minted-options)))
1255 (concat (format "\\mint%s{%s}"
1256 (if (string= options "") "" (format "[%s]" options))
1257 mint-lang)
1258 separator code separator)))
1259 ;; Use listings package.
1261 ;; Maybe translate language's name.
1262 (let* ((org-lang (org-element-property :language inline-src-block))
1263 (lst-lang (or (cadr (assq (intern org-lang)
1264 org-e-latex-listings-langs))
1265 org-lang))
1266 (options (org-e-latex--make-option-string
1267 (append org-e-latex-listings-options
1268 `(("language" ,lst-lang))))))
1269 (concat (format "\\lstinline[%s]" options)
1270 separator code separator))))))
1273 ;;;; Inlinetask
1275 (defun org-e-latex-inlinetask (inlinetask contents info)
1276 "Transcode an INLINETASK element from Org to LaTeX.
1277 CONTENTS holds the contents of the block. INFO is a plist
1278 holding contextual information."
1279 (let ((title (org-export-secondary-string
1280 (org-element-property :title inlinetask) info))
1281 (todo (and (plist-get info :with-todo-keywords)
1282 (let ((todo (org-element-property
1283 :todo-keyword inlinetask)))
1284 (and todo
1285 (org-export-secondary-string todo info)))))
1286 (todo-type (org-element-property :todo-type inlinetask))
1287 (tags (and (plist-get info :with-tags)
1288 (org-element-property :tags inlinetask)))
1289 (priority (and (plist-get info :with-priority)
1290 (org-element-property :priority inlinetask))))
1291 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1292 ;; with appropriate arguments.
1293 (if (functionp org-e-latex-format-inlinetask-function)
1294 (funcall org-e-latex-format-inlinetask-function
1295 todo todo-type priority title tags contents)
1296 ;; Otherwise, use a default template.
1297 (org-e-latex--wrap-label
1298 inlinetask
1299 (let ((full-title
1300 (concat
1301 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1302 (when priority (format "\\framebox{\\#%c} " priority))
1303 title
1304 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1305 (format (concat "\\begin{center}\n"
1306 "\\fbox{\n"
1307 "\\begin{minipage}[c]{.6\\textwidth}\n"
1308 "%s\n\n"
1309 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1310 "%s"
1311 "\\end{minipage}\n"
1312 "}\n"
1313 "\\end{center}")
1314 full-title contents))))))
1317 ;;;; Italic
1319 (defun org-e-latex-italic (italic contents info)
1320 "Transcode ITALIC from Org to LaTeX.
1321 CONTENTS is the text with italic markup. INFO is a plist holding
1322 contextual information."
1323 (org-e-latex--text-markup contents 'italic))
1326 ;;;; Item
1328 (defun org-e-latex-item (item contents info)
1329 "Transcode an ITEM element from Org to LaTeX.
1330 CONTENTS holds the contents of the item. INFO is a plist holding
1331 contextual information."
1332 ;; Grab `:level' from plain-list properties, which is always the
1333 ;; first element above current item.
1334 (let* ((level (org-element-property :level (org-export-get-parent item info)))
1335 (counter (let ((count (org-element-property :counter item)))
1336 (and count
1337 (< level 4)
1338 (format "\\setcounter{enum%s}{%s}\n"
1339 (nth level '("i" "ii" "iii" "iv"))
1340 (1- count)))))
1341 (checkbox (let ((checkbox (org-element-property :checkbox item)))
1342 (cond ((eq checkbox 'on) "$\\boxtimes$ ")
1343 ((eq checkbox 'off) "$\\Box$ ")
1344 ((eq checkbox 'trans) "$\\boxminus$ "))))
1345 (tag (let ((tag (org-element-property :tag item)))
1346 (and tag
1347 (format "[%s]" (org-export-secondary-string tag info))))))
1348 (concat counter "\\item" tag " " checkbox contents)))
1351 ;;;; Keyword
1353 (defun org-e-latex-keyword (keyword contents info)
1354 "Transcode a KEYWORD element from Org to LaTeX.
1355 CONTENTS is nil. INFO is a plist holding contextual information."
1356 (let ((key (org-element-property :key keyword))
1357 (value (org-element-property :value keyword)))
1358 (cond
1359 ((string= key "LATEX") value)
1360 ((string= key "INDEX") (format "\\index{%s}" value))
1361 ;; Invisible targets.
1362 ((string= key "TARGET") nil)
1363 ((string= key "TOC")
1364 (let ((value (downcase value)))
1365 (cond
1366 ((string-match "\\<headlines\\>" value)
1367 (let ((depth (or (and (string-match "[0-9]+" value)
1368 (string-to-number (match-string 0 value)))
1369 (plist-get info :with-toc))))
1370 (concat
1371 (when (wholenump depth)
1372 (format "\\setcounter{tocdepth}{%s}\n" depth))
1373 "\\tableofcontents")))
1374 ((string= "tables" value) "\\listoftables")
1375 ((string= "figures" value) "\\listoffigures")
1376 ((string= "listings" value)
1377 (cond
1378 ((eq org-e-latex-listings 'minted) "\\listoflistings")
1379 (org-e-latex-listings "\\lstlistoflistings")
1380 ;; At the moment, src blocks with a caption are wrapped
1381 ;; into a figure environment.
1382 (t "\\listoffigures")))))))))
1385 ;;;; Latex Environment
1387 (defun org-e-latex-latex-environment (latex-environment contents info)
1388 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1389 CONTENTS is nil. INFO is a plist holding contextual information."
1390 (let ((label (org-element-property :name latex-environment))
1391 (value (org-remove-indentation
1392 (org-element-property :value latex-environment))))
1393 (if (not (org-string-nw-p label)) value
1394 ;; Environment is labelled: label must be within the environment
1395 ;; (otherwise, a reference pointing to that element will count
1396 ;; the section instead).
1397 (with-temp-buffer
1398 (insert value)
1399 (goto-char (point-min))
1400 (forward-line)
1401 (insert (format "\\label{%s}\n" label))
1402 (buffer-string)))))
1405 ;;;; Latex Fragment
1407 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1408 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1409 CONTENTS is nil. INFO is a plist holding contextual information."
1410 (org-element-property :value latex-fragment))
1413 ;;;; Line Break
1415 (defun org-e-latex-line-break (line-break contents info)
1416 "Transcode a LINE-BREAK object from Org to LaTeX.
1417 CONTENTS is nil. INFO is a plist holding contextual information."
1418 "\\\\")
1421 ;;;; Link
1423 (defun org-e-latex-link--inline-image (link info)
1424 "Return LaTeX code for an inline image.
1425 LINK is the link pointing to the inline image. INFO is a plist
1426 used as a communication channel."
1427 (let* ((parent (org-export-get-parent-paragraph link info))
1428 (path (let ((raw-path (org-element-property :path link)))
1429 (if (not (file-name-absolute-p raw-path)) raw-path
1430 (expand-file-name raw-path))))
1431 (caption (org-e-latex--caption/label-string
1432 (org-element-property :caption parent)
1433 (org-element-property :name parent)
1434 info))
1435 ;; Retrieve latex attributes from the element around.
1436 (attr (let ((raw-attr
1437 (mapconcat #'identity
1438 (org-element-property :attr_latex parent)
1439 " ")))
1440 (unless (string= raw-attr "") raw-attr)))
1441 (disposition
1442 (cond
1443 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1444 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1445 ((or (and attr (string-match "\\<float\\>" attr))
1446 (not (string= caption "")))
1447 'float)))
1448 (placement
1449 (cond
1450 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1451 (org-match-string-no-properties 1 attr))
1452 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1453 ((eq disposition 'float)
1454 (concat "[" org-e-latex-default-figure-position "]"))
1455 (t ""))))
1456 ;; Now clear ATTR from any special keyword and set a default
1457 ;; value if nothing is left.
1458 (setq attr
1459 (if (not attr) ""
1460 (org-trim
1461 (replace-regexp-in-string
1462 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
1463 (setq attr (cond ((not (string= attr "")) attr)
1464 ((eq disposition 'float) "width=0.7\\textwidth")
1465 ((eq disposition 'wrap) "width=0.48\\textwidth")
1466 (t (or org-e-latex-image-default-option ""))))
1467 ;; Return proper string, depending on DISPOSITION.
1468 (case disposition
1469 (wrap (format "\\begin{wrapfigure}%s
1470 \\centering
1471 \\includegraphics[%s]{%s}
1472 %s\\end{wrapfigure}" placement attr path caption))
1473 (mulicolumn (format "\\begin{figure*}%s
1474 \\centering
1475 \\includegraphics[%s]{%s}
1476 %s\\end{figure*}" placement attr path caption))
1477 (float (format "\\begin{figure}%s
1478 \\centering
1479 \\includegraphics[%s]{%s}
1480 %s\\end{figure}" placement attr path caption))
1481 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1483 (defun org-e-latex-link (link desc info)
1484 "Transcode a LINK object from Org to LaTeX.
1486 DESC is the description part of the link, or the empty string.
1487 INFO is a plist holding contextual information. See
1488 `org-export-data'."
1489 (let* ((type (org-element-property :type link))
1490 (raw-path (org-element-property :path link))
1491 ;; Ensure DESC really exists, or set it to nil.
1492 (desc (and (not (string= desc "")) desc))
1493 (imagep (org-export-inline-image-p
1494 link org-e-latex-inline-image-rules))
1495 (path (cond
1496 ((member type '("http" "https" "ftp" "mailto"))
1497 (concat type ":" raw-path))
1498 ((string= type "file")
1499 (when (string-match "\\(.+\\)::.+" raw-path)
1500 (setq raw-path (match-string 1 raw-path)))
1501 (if (file-name-absolute-p raw-path)
1502 (concat "file://" (expand-file-name raw-path))
1503 ;; TODO: Not implemented yet. Concat also:
1504 ;; (org-export-directory :LaTeX info)
1505 (concat "file://" raw-path)))
1506 (t raw-path)))
1507 protocol)
1508 (cond
1509 ;; Image file.
1510 (imagep (org-e-latex-link--inline-image link info))
1511 ;; Radioed target: Target's name is obtained from original raw
1512 ;; link. Path is parsed and transcoded in order to have a proper
1513 ;; display of the contents.
1514 ((string= type "radio")
1515 (format "\\hyperref[%s]{%s}"
1516 (org-export-solidify-link-text path)
1517 (org-export-secondary-string
1518 (org-element-parse-secondary-string
1519 path (cdr (assq 'radio-target org-element-object-restrictions)))
1520 info)))
1521 ;; Links pointing to an headline: Find destination and build
1522 ;; appropriate referencing command.
1523 ((member type '("custom-id" "fuzzy" "id"))
1524 (let ((destination (if (string= type "fuzzy")
1525 (org-export-resolve-fuzzy-link link info)
1526 (org-export-resolve-id-link link info))))
1527 (case (org-element-type destination)
1528 ;; Fuzzy link points nowhere.
1529 ('nil
1530 (format "\\texttt{%s}"
1531 (or desc
1532 (org-export-secondary-string
1533 (org-element-property :raw-link link) info))))
1534 ;; Fuzzy link points to an invisible target.
1535 (keyword nil)
1536 ;; LINK points to an headline. If headlines are numbered
1537 ;; and the link has no description, display headline's
1538 ;; number. Otherwise, display description or headline's
1539 ;; title.
1540 (headline
1541 (let ((label
1542 (format "sec-%s"
1543 (mapconcat
1544 'number-to-string
1545 (org-export-get-headline-number destination info)
1546 "-"))))
1547 (if (and (plist-get info :section-numbers) (not desc))
1548 (format "\\ref{%s}" label)
1549 (format "\\hyperref[%s]{%s}" label
1550 (or desc
1551 (org-export-secondary-string
1552 (org-element-property :title destination) info))))))
1553 ;; Fuzzy link points to a target. Do as above.
1554 (otherwise
1555 (let ((path (org-export-solidify-link-text path)))
1556 (if (not desc) (format "\\ref{%s}" path)
1557 (format "\\hyperref[%s]{%s}" path desc)))))))
1558 ;; Coderef: replace link with the reference name or the
1559 ;; equivalent line number.
1560 ((string= type "coderef")
1561 (format (org-export-get-coderef-format path desc)
1562 (org-export-resolve-coderef path info)))
1563 ;; Link type is handled by a special function.
1564 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1565 (funcall protocol (org-link-unescape path) desc 'latex))
1566 ;; External link with a description part.
1567 ((and path desc) (format "\\href{%s}{%s}" path desc))
1568 ;; External link without a description part.
1569 (path (format "\\url{%s}" path))
1570 ;; No path, only description. Try to do something useful.
1571 (t (format "\\texttt{%s}" desc)))))
1574 ;;;; Babel Call
1576 ;; Babel Calls are ignored.
1579 ;;;; Macro
1581 (defun org-e-latex-macro (macro contents info)
1582 "Transcode a MACRO element from Org to LaTeX.
1583 CONTENTS is nil. INFO is a plist holding contextual information."
1584 ;; Use available tools.
1585 (org-export-expand-macro macro info))
1588 ;;;; Paragraph
1590 (defun org-e-latex-paragraph (paragraph contents info)
1591 "Transcode a PARAGRAPH element from Org to LaTeX.
1592 CONTENTS is the contents of the paragraph, as a string. INFO is
1593 the plist used as a communication channel."
1594 contents)
1597 ;;;; Plain List
1599 (defun org-e-latex-plain-list (plain-list contents info)
1600 "Transcode a PLAIN-LIST element from Org to LaTeX.
1601 CONTENTS is the contents of the list. INFO is a plist holding
1602 contextual information."
1603 (let* ((type (org-element-property :type plain-list))
1604 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1605 "inparadesc" "asparadesc"))
1606 (paralist-regexp (concat
1607 "\\("
1608 (mapconcat 'identity paralist-types "\\|")
1609 "\\)"))
1610 (attr (mapconcat #'identity
1611 (org-element-property :attr_latex plain-list)
1612 " "))
1613 (latex-type (cond
1614 ((and attr
1615 (string-match
1616 (format "\\<%s\\>" paralist-regexp) attr))
1617 (match-string 1 attr))
1618 ((eq type 'ordered) "enumerate")
1619 ((eq type 'unordered) "itemize")
1620 ((eq type 'descriptive) "description"))))
1621 (org-e-latex--wrap-label
1622 plain-list
1623 (format "\\begin{%s}%s\n%s\\end{%s}"
1624 latex-type
1625 ;; Once special environment, if any, has been removed, the
1626 ;; rest of the attributes will be optional arguments.
1627 ;; They will be put inside square brackets if necessary.
1628 (let ((opt (replace-regexp-in-string
1629 (format " *%s *" paralist-regexp) "" attr)))
1630 (cond ((string= opt "") "")
1631 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1632 (t (format "[%s]" opt))))
1633 contents
1634 latex-type))))
1637 ;;;; Plain Text
1639 (defun org-e-latex-plain-text (text info)
1640 "Transcode a TEXT string from Org to LaTeX.
1641 TEXT is the string to transcode. INFO is a plist holding
1642 contextual information."
1643 ;; Protect %, #, &, $, ~, ^, _, { and }.
1644 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1645 (setq text
1646 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1647 ;; Protect \
1648 (setq text (replace-regexp-in-string
1649 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1650 "$\\backslash$" text nil t 1))
1651 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1652 (let ((case-fold-search nil)
1653 (start 0))
1654 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1655 (setq text (replace-match
1656 (format "\\%s{}" (match-string 1 text)) nil t text)
1657 start (match-end 0))))
1658 ;; Handle quotation marks
1659 (setq text (org-e-latex--quotation-marks text info))
1660 ;; Convert special strings.
1661 (when (plist-get info :with-special-strings)
1662 (while (string-match (regexp-quote "...") text)
1663 (setq text (replace-match "\\ldots{}" nil t text))))
1664 ;; Handle break preservation if required.
1665 (when (plist-get info :preserve-breaks)
1666 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1667 text)))
1668 ;; Return value.
1669 text)
1672 ;;;; Property Drawer
1674 (defun org-e-latex-property-drawer (property-drawer contents info)
1675 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1676 CONTENTS is nil. INFO is a plist holding contextual
1677 information."
1678 ;; The property drawer isn't exported but we want separating blank
1679 ;; lines nonetheless.
1683 ;;;; Quote Block
1685 (defun org-e-latex-quote-block (quote-block contents info)
1686 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1687 CONTENTS holds the contents of the block. INFO is a plist
1688 holding contextual information."
1689 (org-e-latex--wrap-label
1690 quote-block
1691 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1694 ;;;; Quote Section
1696 (defun org-e-latex-quote-section (quote-section contents info)
1697 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1698 CONTENTS is nil. INFO is a plist holding contextual information."
1699 (let ((value (org-remove-indentation
1700 (org-element-property :value quote-section))))
1701 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1704 ;;;; Section
1706 (defun org-e-latex-section (section contents info)
1707 "Transcode a SECTION element from Org to LaTeX.
1708 CONTENTS holds the contents of the section. INFO is a plist
1709 holding contextual information."
1710 contents)
1713 ;;;; Radio Target
1715 (defun org-e-latex-radio-target (radio-target text info)
1716 "Transcode a RADIO-TARGET object from Org to LaTeX.
1717 TEXT is the text of the target. INFO is a plist holding
1718 contextual information."
1719 (format "\\label{%s}%s"
1720 (org-export-solidify-link-text
1721 (org-element-property :value radio-target))
1722 text))
1725 ;;;; Special Block
1727 (defun org-e-latex-special-block (special-block contents info)
1728 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1729 CONTENTS holds the contents of the block. INFO is a plist
1730 holding contextual information."
1731 (let ((type (downcase (org-element-property :type special-block))))
1732 (org-e-latex--wrap-label
1733 special-block
1734 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
1737 ;;;; Src Block
1739 (defun org-e-latex-src-block (src-block contents info)
1740 "Transcode a SRC-BLOCK element from Org to LaTeX.
1741 CONTENTS holds the contents of the item. INFO is a plist holding
1742 contextual information."
1743 (let* ((lang (org-element-property :language src-block))
1744 (caption (org-element-property :caption src-block))
1745 (label (org-element-property :name src-block))
1746 (custom-env (and lang
1747 (cadr (assq (intern lang)
1748 org-e-latex-custom-lang-environments))))
1749 (num-start (case (org-element-property :number-lines src-block)
1750 (continued (org-export-get-loc src-block info))
1751 (new 0)))
1752 (retain-labels (org-element-property :retain-labels src-block)))
1753 (cond
1754 ;; Case 1. No source fontification.
1755 ((not org-e-latex-listings)
1756 (let ((caption-str (org-e-latex--caption/label-string caption label info))
1757 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
1758 (format
1759 (or float-env "%s")
1760 (concat caption-str
1761 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1762 (org-export-format-code-default src-block info))))))
1763 ;; Case 2. Custom environment.
1764 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
1765 custom-env
1766 (org-export-format-code-default src-block info)
1767 custom-env))
1768 ;; Case 3. Use minted package.
1769 ((eq org-e-latex-listings 'minted)
1770 (let ((float-env (when (or label caption)
1771 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1772 (org-e-latex--caption/label-string
1773 caption label info))))
1774 (body
1775 (format
1776 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1777 ;; Options.
1778 (org-e-latex--make-option-string
1779 (if (not num-start) org-e-latex-minted-options
1780 (append `(("linenos")
1781 ("firstnumber" ,(number-to-string (1+ num-start))))
1782 org-e-latex-minted-options)))
1783 ;; Language.
1784 (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
1785 ;; Source code.
1786 (let* ((code-info (org-export-unravel-code src-block))
1787 (max-width
1788 (apply 'max
1789 (mapcar 'length
1790 (org-split-string (car code-info) "\n")))))
1791 (org-export-format-code
1792 (car code-info)
1793 (lambda (loc num ref)
1794 (concat
1796 (when ref
1797 ;; Ensure references are flushed to the right,
1798 ;; separated with 6 spaces from the widest line
1799 ;; of code.
1800 (concat (make-string (+ (- max-width (length loc)) 6) ? )
1801 (format "(%s)" ref)))))
1802 nil (and retain-labels (cdr code-info)))))))
1803 ;; Return value.
1804 (if float-env (format float-env body) body)))
1805 ;; Case 4. Use listings package.
1807 (let ((lst-lang
1808 (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
1809 (caption-str
1810 (when caption
1811 (let ((main (org-export-secondary-string (car caption) info)))
1812 (if (not (cdr caption)) (format "{%s}" main)
1813 (format
1814 "{[%s]%s}"
1815 (org-export-secondary-string (cdr caption) info)
1816 main))))))
1817 (concat
1818 ;; Options.
1819 (format "\\lstset{%s}\n"
1820 (org-e-latex--make-option-string
1821 (append org-e-latex-listings-options
1822 `(("language" ,lst-lang))
1823 (when label `(("label" ,label)))
1824 (when caption-str `(("caption" ,caption-str)))
1825 (cond ((not num-start) '(("numbers" "none")))
1826 ((zerop num-start) '(("numbers" "left")))
1827 (t `(("numbers" "left")
1828 ("firstnumber"
1829 ,(number-to-string (1+ num-start)))))))))
1830 ;; Source code.
1831 (format
1832 "\\begin{lstlisting}\n%s\\end{lstlisting}"
1833 (let* ((code-info (org-export-unravel-code src-block))
1834 (max-width
1835 (apply 'max
1836 (mapcar 'length
1837 (org-split-string (car code-info) "\n")))))
1838 (org-export-format-code
1839 (car code-info)
1840 (lambda (loc num ref)
1841 (concat
1843 (when ref
1844 ;; Ensure references are flushed to the right,
1845 ;; separated with 6 spaces from the widest line of
1846 ;; code
1847 (concat (make-string (+ (- max-width (length loc)) 6) ? )
1848 (format "(%s)" ref)))))
1849 nil (and retain-labels (cdr code-info)))))))))))
1852 ;;;; Statistics Cookie
1854 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
1855 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1856 CONTENTS is nil. INFO is a plist holding contextual information."
1857 (org-element-property :value statistics-cookie))
1860 ;;;; Strike-Through
1862 (defun org-e-latex-strike-through (strike-through contents info)
1863 "Transcode STRIKE-THROUGH from Org to LaTeX.
1864 CONTENTS is the text with strike-through markup. INFO is a plist
1865 holding contextual information."
1866 (org-e-latex--text-markup contents 'strike-through))
1869 ;;;; Subscript
1871 (defun org-e-latex-subscript (subscript contents info)
1872 "Transcode a SUBSCRIPT object from Org to LaTeX.
1873 CONTENTS is the contents of the object. INFO is a plist holding
1874 contextual information."
1875 (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
1878 ;;;; Superscript
1880 (defun org-e-latex-superscript (superscript contents info)
1881 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1882 CONTENTS is the contents of the object. INFO is a plist holding
1883 contextual information."
1884 (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
1887 ;;;; Table
1889 (defun org-e-latex-table--format-string (table info)
1890 "Return an appropriate format string for TABLE.
1892 TABLE-INFO is the plist containing format info about the table,
1893 as returned by `org-export-table-format-info'. INFO is a plist
1894 used as a communication channel.
1896 The format string leaves one placeholder for the body of the
1897 table."
1898 (let* ((label (org-element-property :name table))
1899 (caption (org-e-latex--caption/label-string
1900 (org-element-property :caption table) label info))
1901 (attr (mapconcat 'identity
1902 (org-element-property :attr_latex table)
1903 " "))
1904 ;; Determine alignment string.
1905 (alignment (org-e-latex-table--align-string table info))
1906 ;; Determine environment for the table: longtable, tabular...
1907 (table-env (cond
1908 ((not attr) org-e-latex-default-table-environment)
1909 ((string-match "\\<longtable\\>" attr) "longtable")
1910 ((string-match "\\<tabular.?\\>" attr)
1911 (org-match-string-no-properties 0 attr))
1912 (t org-e-latex-default-table-environment)))
1913 ;; If table is a float, determine environment: table, table*
1914 ;; or sidewaystable.
1915 (float-env (cond
1916 ((string= "longtable" table-env) nil)
1917 ((and attr (string-match "\\<sidewaystable\\>" attr))
1918 "sidewaystables")
1919 ((and attr
1920 (or (string-match (regexp-quote "table*") attr)
1921 (string-match "\\<multicolumn\\>" attr)))
1922 "table*")
1923 ((or (not (string= caption "")) label) "table")))
1924 ;; Extract others display options.
1925 (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
1926 (org-match-string-no-properties 1 attr)))
1927 (placement
1928 (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1929 (org-match-string-no-properties 1 attr)
1930 (format "[%s]" org-e-latex-default-figure-position))))
1931 ;; Prepare the final format string for the table.
1932 (cond
1933 ;; Longtable.
1934 ((string= "longtable" table-env)
1935 (format
1936 "\\begin{longtable}{%s}\n%s%%s%s\\end{longtable}"
1937 alignment
1938 (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
1939 (concat (org-trim caption) "\\\\\n"))
1940 (if (or org-e-latex-table-caption-above (string= "" caption)) ""
1941 (concat (org-trim caption) "\\\\\n"))))
1942 ;; Others.
1943 (t (concat (when float-env
1944 (concat
1945 (format "\\begin{%s}%s\n" float-env placement)
1946 (if org-e-latex-table-caption-above caption "")))
1947 (when org-e-latex-tables-centered "\\begin{center}\n")
1948 (format "\\begin{%s}%s{%s}\n%%s\\end{%s}"
1949 table-env
1950 (if width (format "{%s}" width) "") alignment table-env)
1951 (when org-e-latex-tables-centered "\n\\end{center}")
1952 (when float-env
1953 (concat (if org-e-latex-table-caption-above "" caption)
1954 (format "\n\\end{%s}" float-env))))))))
1956 (defun org-e-latex-table--align-string (table info)
1957 "Return an appropriate LaTeX alignment string.
1958 TABLE is the considered table. INFO is a plist used as
1959 a communication channel."
1960 (let ((attr (mapconcat 'identity
1961 (org-element-property :attr_latex table)
1962 " ")))
1963 (if (and attr (string-match "\\<align=\\(\\S-+\\)" attr))
1964 (match-string 1 attr)
1965 (let (alignment)
1966 ;; Extract column groups and alignment from first (non-rule)
1967 ;; row.
1968 (org-element-map
1969 (org-element-map table 'table-row 'identity info 'first-match)
1970 'table-cell
1971 (lambda (cell)
1972 (let ((borders (org-export-table-cell-borders cell info)))
1973 ;; Check left border for the first cell only.
1974 (when (and (memq 'left borders) (not alignment))
1975 (push "|" alignment))
1976 (push (case (org-export-table-cell-alignment cell info)
1977 (left "l")
1978 (right "r")
1979 (center "c"))
1980 alignment)
1981 (when (memq 'right borders) (push "|" alignment))))
1982 info)
1983 (apply 'concat (reverse alignment))))))
1985 (defun org-e-latex-table (table contents info)
1986 "Transcode a TABLE element from Org to LaTeX.
1987 CONTENTS is nil. INFO is a plist holding contextual information."
1988 (cond
1989 ;; Case 1: verbatim table.
1990 ((or org-e-latex-tables-verbatim
1991 (let ((attr (mapconcat 'identity
1992 (org-element-property :attr_latex table)
1993 " ")))
1994 (and attr (string-match "\\<verbatim\\>" attr))))
1995 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
1996 ;; Re-create table, without affiliated keywords.
1997 (org-trim
1998 (org-element-interpret-data
1999 `(org-data nil (table nil ,@(org-element-contents table)))))))
2000 ;; Case 2: table.el table. Convert it using appropriate tools.
2001 ((eq (org-element-property :type table) 'table.el)
2002 (require 'table)
2003 ;; Ensure "*org-export-table*" buffer is empty.
2004 (with-current-buffer (get-buffer-create "*org-export-table*")
2005 (erase-buffer))
2006 (let ((output (with-temp-buffer
2007 (insert (org-element-property :value table))
2008 (goto-char 1)
2009 (re-search-forward "^[ \t]*|[^|]" nil t)
2010 (table-generate-source 'latex "*org-export-table*")
2011 (with-current-buffer "*org-export-table*"
2012 (org-trim (buffer-string))))))
2013 (kill-buffer (get-buffer "*org-export-table*"))
2014 ;; Remove left out comments.
2015 (while (string-match "^%.*\n" output)
2016 (setq output (replace-match "" t t output)))
2017 ;; When the "rmlines" attribute is provided, remove all hlines
2018 ;; but the the one separating heading from the table body.
2019 (let ((attr (mapconcat 'identity
2020 (org-element-property :attr_latex table)
2021 " ")))
2022 (when (and attr (string-match "\\<rmlines\\>" attr))
2023 (let ((n 0) (pos 0))
2024 (while (and (< (length output) pos)
2025 (setq pos (string-match "^\\\\hline\n?" output pos)))
2026 (incf n)
2027 (unless (= n 2)
2028 (setq output (replace-match "" nil nil output)))))))
2029 (if (not org-e-latex-tables-centered) output
2030 (format "\\begin{center}\n%s\n\\end{center}" output))))
2031 ;; Case 3: Standard table.
2032 (t (format (org-e-latex-table--format-string table info) contents))))
2035 ;;;; Table Cell
2037 (defun org-e-latex-table-cell (table-cell contents info)
2038 "Transcode a TABLE-CELL element from Org to LaTeX.
2039 CONTENTS is the cell contents. INFO is a plist used as
2040 a communication channel."
2041 (concat (if (and contents
2042 org-e-latex-table-scientific-notation
2043 (string-match orgtbl-exp-regexp contents))
2044 ;; Use appropriate format string for scientific
2045 ;; notation.
2046 (format org-e-latex-table-scientific-notation
2047 (match-string 1 contents)
2048 (match-string 2 contents))
2049 contents)
2050 (when (org-export-get-next-element table-cell info) " & ")))
2053 ;;;; Table Row
2055 (defun org-e-latex-table-row (table-row contents info)
2056 "Transcode a TABLE-ROW element from Org to LaTeX.
2057 CONTENTS is the contents of the row. INFO is a plist used as
2058 a communication channel."
2059 ;; Rules are ignored since table separators are deduced from
2060 ;; borders of the current row.
2061 (when (eq (org-element-property :type table-row) 'standard)
2062 (let* ((attr (mapconcat 'identity
2063 (org-element-property
2064 :attr_latex (org-export-get-parent table-row info))
2065 " "))
2066 (longtablep (and attr (string-match "\\<longtable\\>" attr)))
2067 (booktabsp
2068 (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
2069 org-e-latex-tables-booktabs))
2070 ;; TABLE-ROW's borders are extracted from its first cell.
2071 (borders
2072 (org-export-table-cell-borders
2073 (car (org-element-contents table-row)) info)))
2074 (concat
2075 ;; When BOOKTABS are activated enforce top-rule even when no
2076 ;; hline was specifically marked.
2077 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2078 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2079 contents "\\\\\n"
2080 (cond
2081 ;; Special case for long tables. Define header and footers.
2082 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2083 (format "%s
2084 \\endhead
2085 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2086 \\endfoot
2087 \\endlastfoot"
2088 (if booktabsp "\\midrule" "\\hline")
2089 (if booktabsp "\\midrule" "\\hline")
2090 ;; Number of columns.
2091 (cdr (org-export-table-dimensions
2092 (org-export-get-parent-table table-row info) info))))
2093 ;; When BOOKTABS are activated enforce bottom rule even when
2094 ;; no hline was specifically marked.
2095 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2096 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2097 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2100 ;;;; Target
2102 (defun org-e-latex-target (target contents info)
2103 "Transcode a TARGET object from Org to LaTeX.
2104 CONTENTS is nil. INFO is a plist holding contextual
2105 information."
2106 (format "\\label{%s}"
2107 (org-export-solidify-link-text (org-element-property :value target))))
2110 ;;;; Time-stamp
2112 (defun org-e-latex-time-stamp (time-stamp contents info)
2113 "Transcode a TIME-STAMP object from Org to LaTeX.
2114 CONTENTS is nil. INFO is a plist holding contextual
2115 information."
2116 (let ((value (org-element-property :value time-stamp))
2117 (type (org-element-property :type time-stamp))
2118 (appt-type (org-element-property :appt-type time-stamp)))
2119 (concat (cond ((eq appt-type 'scheduled)
2120 (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
2121 ((eq appt-type 'deadline)
2122 (format "\\textbf{\\textsc{%s}} " org-deadline-string))
2123 ((eq appt-type 'closed)
2124 (format "\\textbf{\\textsc{%s}} " org-closed-string)))
2125 (cond ((memq type '(active active-range))
2126 (format org-e-latex-active-timestamp-format value))
2127 ((memq type '(inactive inactive-range))
2128 (format org-e-latex-inactive-timestamp-format value))
2130 (format org-e-latex-diary-timestamp-format value))))))
2133 ;;;; Underline
2135 (defun org-e-latex-underline (underline contents info)
2136 "Transcode UNDERLINE from Org to LaTeX.
2137 CONTENTS is the text with underline markup. INFO is a plist
2138 holding contextual information."
2139 (org-e-latex--text-markup contents 'underline))
2142 ;;;; Verbatim
2144 (defun org-e-latex-verbatim (verbatim contents info)
2145 "Transcode a VERBATIM object from Org to LaTeX.
2146 CONTENTS is nil. INFO is a plist used as a communication
2147 channel."
2148 (org-e-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2151 ;;;; Verse Block
2153 (defun org-e-latex-verse-block (verse-block contents info)
2154 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2155 CONTENTS is verse block contents. INFO is a plist holding
2156 contextual information."
2157 (org-e-latex--wrap-label
2158 verse-block
2159 ;; In a verse environment, add a line break to each newline
2160 ;; character and change each white space at beginning of a line
2161 ;; into a space of 1 em. Also change each blank line with
2162 ;; a vertical space of 1 em.
2163 (progn
2164 (setq contents (replace-regexp-in-string
2165 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2166 (replace-regexp-in-string
2167 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2168 (while (string-match "^[ \t]+" contents)
2169 (let ((new-str (format "\\hspace*{%dem}"
2170 (length (match-string 0 contents)))))
2171 (setq contents (replace-match new-str nil t contents))))
2172 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2176 ;;; Interactive functions
2178 (defun org-e-latex-export-to-latex
2179 (&optional subtreep visible-only body-only ext-plist pub-dir)
2180 "Export current buffer to a LaTeX file.
2182 If narrowing is active in the current buffer, only export its
2183 narrowed part.
2185 If a region is active, export that region.
2187 When optional argument SUBTREEP is non-nil, export the sub-tree
2188 at point, extracting information from the headline properties
2189 first.
2191 When optional argument VISIBLE-ONLY is non-nil, don't export
2192 contents of hidden elements.
2194 When optional argument BODY-ONLY is non-nil, only write code
2195 between \"\\begin{document}\" and \"\\end{document}\".
2197 EXT-PLIST, when provided, is a property list with external
2198 parameters overriding Org default settings, but still inferior to
2199 file-local settings.
2201 When optional argument PUB-DIR is set, use it as the publishing
2202 directory.
2204 Return output file's name."
2205 (interactive)
2206 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
2207 (org-export-to-file
2208 'e-latex outfile subtreep visible-only body-only ext-plist)))
2210 (defun org-e-latex-export-to-pdf
2211 (&optional subtreep visible-only body-only ext-plist pub-dir)
2212 "Export current buffer to LaTeX then process through to PDF.
2214 If narrowing is active in the current buffer, only export its
2215 narrowed part.
2217 If a region is active, export that region.
2219 When optional argument SUBTREEP is non-nil, export the sub-tree
2220 at point, extracting information from the headline properties
2221 first.
2223 When optional argument VISIBLE-ONLY is non-nil, don't export
2224 contents of hidden elements.
2226 When optional argument BODY-ONLY is non-nil, only write code
2227 between \"\\begin{document}\" and \"\\end{document}\".
2229 EXT-PLIST, when provided, is a property list with external
2230 parameters overriding Org default settings, but still inferior to
2231 file-local settings.
2233 When optional argument PUB-DIR is set, use it as the publishing
2234 directory.
2236 Return PDF file's name."
2237 (interactive)
2238 (org-e-latex-compile
2239 (org-e-latex-export-to-latex
2240 subtreep visible-only body-only ext-plist pub-dir)))
2242 (defun org-e-latex-compile (texfile)
2243 "Compile a TeX file.
2245 TEXFILE is the name of the file being compiled. Processing is
2246 done through the command specified in `org-e-latex-pdf-process'.
2248 Return PDF file name or an error if it couldn't be produced."
2249 (let* ((wconfig (current-window-configuration))
2250 (texfile (file-truename texfile))
2251 (base (file-name-sans-extension texfile))
2252 errors)
2253 (message (format "Processing LaTeX file %s ..." texfile))
2254 (unwind-protect
2255 (progn
2256 (cond
2257 ;; A function is provided: Apply it.
2258 ((functionp org-e-latex-pdf-process)
2259 (funcall org-e-latex-pdf-process (shell-quote-argument texfile)))
2260 ;; A list is provided: Replace %b, %f and %o with appropriate
2261 ;; values in each command before applying it. Output is
2262 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2263 ((consp org-e-latex-pdf-process)
2264 (let* ((out-dir (or (file-name-directory texfile) "./"))
2265 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2266 (mapc
2267 (lambda (command)
2268 (shell-command
2269 (replace-regexp-in-string
2270 "%b" (shell-quote-argument base)
2271 (replace-regexp-in-string
2272 "%f" (shell-quote-argument texfile)
2273 (replace-regexp-in-string
2274 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2275 outbuf))
2276 org-e-latex-pdf-process)
2277 ;; Collect standard errors from output buffer.
2278 (setq errors (org-e-latex-collect-errors outbuf))))
2279 (t (error "No valid command to process to PDF")))
2280 (let ((pdffile (concat base ".pdf")))
2281 ;; Check for process failure. Provide collected errors if
2282 ;; possible.
2283 (if (not (file-exists-p pdffile))
2284 (error (concat (format "PDF file %s wasn't produced" pdffile)
2285 (when errors (concat ": " errors))))
2286 ;; Else remove log files, when specified, and signal end of
2287 ;; process to user, along with any error encountered.
2288 (when org-e-latex-remove-logfiles
2289 (dolist (ext org-e-latex-logfiles-extensions)
2290 (let ((file (concat base "." ext)))
2291 (when (file-exists-p file) (delete-file file)))))
2292 (message (concat "Process completed"
2293 (if (not errors) "."
2294 (concat " with errors: " errors)))))
2295 ;; Return output file name.
2296 pdffile))
2297 (set-window-configuration wconfig))))
2299 (defun org-e-latex-collect-errors (buffer)
2300 "Collect some kind of errors from \"pdflatex\" command output.
2302 BUFFER is the buffer containing output.
2304 Return collected error types as a string, or nil if there was
2305 none."
2306 (with-current-buffer buffer
2307 (save-excursion
2308 (goto-char (point-max))
2309 ;; Find final "pdflatex" run.
2310 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
2311 (let ((case-fold-search t)
2312 (errors ""))
2313 (when (save-excursion
2314 (re-search-forward "Reference.*?undefined" nil t))
2315 (setq errors (concat errors " [undefined reference]")))
2316 (when (save-excursion
2317 (re-search-forward "Citation.*?undefined" nil t))
2318 (setq errors (concat errors " [undefined citation]")))
2319 (when (save-excursion
2320 (re-search-forward "Undefined control sequence" nil t))
2321 (setq errors (concat errors " [undefined control sequence]")))
2322 (when (save-excursion
2323 (re-search-forward "^! LaTeX.*?Error" nil t))
2324 (setq errors (concat errors " [LaTeX error]")))
2325 (when (save-excursion
2326 (re-search-forward "^! Package.*?Error" nil t))
2327 (setq errors (concat errors " [package error]")))
2328 (and (org-string-nw-p errors) (org-trim errors)))))))
2331 (provide 'org-e-latex)
2332 ;;; org-e-latex.el ends here