org.texi: Edit date and time macros
[org-mode.git] / lisp / ox-latex.el
blobddb9649600b8c5ffe7c39d80188907799eff1464
1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2015 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; See Org manual for details.
27 ;;; Code:
29 (eval-when-compile (require 'cl))
30 (require 'ox)
31 (require 'ox-publish)
33 (defvar org-latex-default-packages-alist)
34 (defvar org-latex-packages-alist)
35 (defvar orgtbl-exp-regexp)
39 ;;; Define Back-End
41 (org-export-define-backend 'latex
42 '((bold . org-latex-bold)
43 (center-block . org-latex-center-block)
44 (clock . org-latex-clock)
45 (code . org-latex-code)
46 (drawer . org-latex-drawer)
47 (dynamic-block . org-latex-dynamic-block)
48 (entity . org-latex-entity)
49 (example-block . org-latex-example-block)
50 (export-block . org-latex-export-block)
51 (export-snippet . org-latex-export-snippet)
52 (fixed-width . org-latex-fixed-width)
53 (footnote-definition . org-latex-footnote-definition)
54 (footnote-reference . org-latex-footnote-reference)
55 (headline . org-latex-headline)
56 (horizontal-rule . org-latex-horizontal-rule)
57 (inline-src-block . org-latex-inline-src-block)
58 (inlinetask . org-latex-inlinetask)
59 (italic . org-latex-italic)
60 (item . org-latex-item)
61 (keyword . org-latex-keyword)
62 (latex-environment . org-latex-latex-environment)
63 (latex-fragment . org-latex-latex-fragment)
64 (line-break . org-latex-line-break)
65 (link . org-latex-link)
66 (node-property . org-latex-node-property)
67 (paragraph . org-latex-paragraph)
68 (plain-list . org-latex-plain-list)
69 (plain-text . org-latex-plain-text)
70 (planning . org-latex-planning)
71 (property-drawer . org-latex-property-drawer)
72 (quote-block . org-latex-quote-block)
73 (radio-target . org-latex-radio-target)
74 (section . org-latex-section)
75 (special-block . org-latex-special-block)
76 (src-block . org-latex-src-block)
77 (statistics-cookie . org-latex-statistics-cookie)
78 (strike-through . org-latex-strike-through)
79 (subscript . org-latex-subscript)
80 (superscript . org-latex-superscript)
81 (table . org-latex-table)
82 (table-cell . org-latex-table-cell)
83 (table-row . org-latex-table-row)
84 (target . org-latex-target)
85 (template . org-latex-template)
86 (timestamp . org-latex-timestamp)
87 (underline . org-latex-underline)
88 (verbatim . org-latex-verbatim)
89 (verse-block . org-latex-verse-block)
90 ;; Pseudo objects and elements.
91 (latex-math-block . org-latex-math-block)
92 (latex-matrices . org-latex-matrices))
93 :export-block '("LATEX" "TEX")
94 :menu-entry
95 '(?l "Export to LaTeX"
96 ((?L "As LaTeX buffer" org-latex-export-as-latex)
97 (?l "As LaTeX file" org-latex-export-to-latex)
98 (?p "As PDF file" org-latex-export-to-pdf)
99 (?o "As PDF file and open"
100 (lambda (a s v b)
101 (if a (org-latex-export-to-pdf t s v b)
102 (org-open-file (org-latex-export-to-pdf nil s v b)))))))
103 :filters-alist '((:filter-options . org-latex-math-block-options-filter)
104 (:filter-parse-tree org-latex-math-block-tree-filter
105 org-latex-matrices-tree-filter))
106 :options-alist
107 '((:latex-class "LATEX_CLASS" nil org-latex-default-class t)
108 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
109 (:latex-header "LATEX_HEADER" nil nil newline)
110 (:latex-header-extra "LATEX_HEADER_EXTRA" nil nil newline)
111 (:description "DESCRIPTION" nil nil parse)
112 (:keywords "KEYWORDS" nil nil parse)
113 (:subtitle "SUBTITLE" nil nil parse)
114 ;; Other variables.
115 (:latex-active-timestamp-format nil nil org-latex-active-timestamp-format)
116 (:latex-caption-above nil nil org-latex-caption-above)
117 (:latex-classes nil nil org-latex-classes)
118 (:latex-default-figure-position nil nil org-latex-default-figure-position)
119 (:latex-default-table-environment nil nil org-latex-default-table-environment)
120 (:latex-default-table-mode nil nil org-latex-default-table-mode)
121 (:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format)
122 (:latex-footnote-separator nil nil org-latex-footnote-separator)
123 (:latex-format-drawer-function nil nil org-latex-format-drawer-function)
124 (:latex-format-headline-function nil nil org-latex-format-headline-function)
125 (:latex-format-inlinetask-function nil nil org-latex-format-inlinetask-function)
126 (:latex-hyperref-template nil nil org-latex-hyperref-template t)
127 (:latex-image-default-height nil nil org-latex-image-default-height)
128 (:latex-image-default-option nil nil org-latex-image-default-option)
129 (:latex-image-default-width nil nil org-latex-image-default-width)
130 (:latex-inactive-timestamp-format nil nil org-latex-inactive-timestamp-format)
131 (:latex-inline-image-rules nil nil org-latex-inline-image-rules)
132 (:latex-link-with-unknown-path-format nil nil org-latex-link-with-unknown-path-format)
133 (:latex-listings nil nil org-latex-listings)
134 (:latex-listings-langs nil nil org-latex-listings-langs)
135 (:latex-listings-options nil nil org-latex-listings-options)
136 (:latex-minted-langs nil nil org-latex-minted-langs)
137 (:latex-minted-options nil nil org-latex-minted-options)
138 (:latex-prefer-user-labels nil nil org-latex-prefer-user-labels)
139 (:latex-subtitle-format nil nil org-latex-subtitle-format)
140 (:latex-subtitle-separate nil nil org-latex-subtitle-separate)
141 (:latex-table-scientific-notation nil nil org-latex-table-scientific-notation)
142 (:latex-tables-booktabs nil nil org-latex-tables-booktabs)
143 (:latex-tables-centered nil nil org-latex-tables-centered)
144 (:latex-text-markup-alist nil nil org-latex-text-markup-alist)
145 (:latex-title-command nil nil org-latex-title-command)
146 (:latex-toc-command nil nil org-latex-toc-command)
147 ;; Redefine regular options.
148 (:date "DATE" nil "\\today" parse)))
152 ;;; Internal Variables
154 (defconst org-latex-babel-language-alist
155 '(("af" . "afrikaans")
156 ("bg" . "bulgarian")
157 ("bt-br" . "brazilian")
158 ("ca" . "catalan")
159 ("cs" . "czech")
160 ("cy" . "welsh")
161 ("da" . "danish")
162 ("de" . "germanb")
163 ("de-at" . "naustrian")
164 ("de-de" . "ngerman")
165 ("el" . "greek")
166 ("en" . "english")
167 ("en-au" . "australian")
168 ("en-ca" . "canadian")
169 ("en-gb" . "british")
170 ("en-ie" . "irish")
171 ("en-nz" . "newzealand")
172 ("en-us" . "american")
173 ("es" . "spanish")
174 ("et" . "estonian")
175 ("eu" . "basque")
176 ("fi" . "finnish")
177 ("fr" . "frenchb")
178 ("fr-ca" . "canadien")
179 ("gl" . "galician")
180 ("hr" . "croatian")
181 ("hu" . "hungarian")
182 ("id" . "indonesian")
183 ("is" . "icelandic")
184 ("it" . "italian")
185 ("la" . "latin")
186 ("ms" . "malay")
187 ("nl" . "dutch")
188 ("nb" . "norsk")
189 ("nn" . "nynorsk")
190 ("no" . "norsk")
191 ("pl" . "polish")
192 ("pt" . "portuguese")
193 ("ro" . "romanian")
194 ("ru" . "russian")
195 ("sa" . "sanskrit")
196 ("sb" . "uppersorbian")
197 ("sk" . "slovak")
198 ("sl" . "slovene")
199 ("sq" . "albanian")
200 ("sr" . "serbian")
201 ("sv" . "swedish")
202 ("ta" . "tamil")
203 ("tr" . "turkish")
204 ("uk" . "ukrainian"))
205 "Alist between language code and corresponding Babel option.")
207 (defconst org-latex-polyglossia-language-alist
208 '(("am" "amharic")
209 ("ast" "asturian")
210 ("ar" "arabic")
211 ("bo" "tibetan")
212 ("bn" "bengali")
213 ("bg" "bulgarian")
214 ("br" "breton")
215 ("bt-br" "brazilian")
216 ("ca" "catalan")
217 ("cop" "coptic")
218 ("cs" "czech")
219 ("cy" "welsh")
220 ("da" "danish")
221 ("de" "german" "german")
222 ("de-at" "german" "austrian")
223 ("de-de" "german" "german")
224 ("dv" "divehi")
225 ("el" "greek")
226 ("en" "english" "usmax")
227 ("en-au" "english" "australian")
228 ("en-gb" "english" "uk")
229 ("en-nz" "english" "newzealand")
230 ("en-us" "english" "usmax")
231 ("eo" "esperanto")
232 ("es" "spanish")
233 ("et" "estonian")
234 ("eu" "basque")
235 ("fa" "farsi")
236 ("fi" "finnish")
237 ("fr" "french")
238 ("fu" "friulan")
239 ("ga" "irish")
240 ("gd" "scottish")
241 ("gl" "galician")
242 ("he" "hebrew")
243 ("hi" "hindi")
244 ("hr" "croatian")
245 ("hu" "magyar")
246 ("hy" "armenian")
247 ("id" "bahasai")
248 ("ia" "interlingua")
249 ("is" "icelandic")
250 ("it" "italian")
251 ("kn" "kannada")
252 ("la" "latin" "modern")
253 ("la-modern" "latin" "modern")
254 ("la-classic" "latin" "classic")
255 ("la-medieval" "latin" "medieval")
256 ("lo" "lao")
257 ("lt" "lithuanian")
258 ("lv" "latvian")
259 ("mr" "maranthi")
260 ("ml" "malayalam")
261 ("nl" "dutch")
262 ("nb" "norsk")
263 ("nn" "nynorsk")
264 ("nko" "nko")
265 ("no" "norsk")
266 ("oc" "occitan")
267 ("pl" "polish")
268 ("pms" "piedmontese")
269 ("pt" "portuges")
270 ("rm" "romansh")
271 ("ro" "romanian")
272 ("ru" "russian")
273 ("sa" "sanskrit")
274 ("hsb" "usorbian")
275 ("dsb" "lsorbian")
276 ("sk" "slovak")
277 ("sl" "slovenian")
278 ("se" "samin")
279 ("sq" "albanian")
280 ("sr" "serbian")
281 ("sv" "swedish")
282 ("syr" "syriac")
283 ("ta" "tamil")
284 ("te" "telugu")
285 ("th" "thai")
286 ("tk" "turkmen")
287 ("tr" "turkish")
288 ("uk" "ukrainian")
289 ("ur" "urdu")
290 ("vi" "vietnamese"))
291 "Alist between language code and corresponding Polyglossia option")
295 (defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
296 ("qbordermatrix" . "\\cr")
297 ("kbordermatrix" . "\\\\"))
298 "Alist between matrix macros and their row ending.")
300 (defconst org-latex-math-environments-re
301 (format
302 "\\`[ \t]*\\\\begin{%s\\*?}"
303 (regexp-opt
304 '("equation" "eqnarray" "math" "displaymath"
305 "align" "gather" "multline" "flalign" "alignat"
306 "xalignat" "xxalignat"
307 "subequations"
308 ;; breqn
309 "dmath" "dseries" "dgroup" "darray"
310 ;; empheq
311 "empheq")))
312 "Regexp of LaTeX math environments.")
315 ;;; User Configurable Variables
317 (defgroup org-export-latex nil
318 "Options for exporting Org mode files to LaTeX."
319 :tag "Org Export LaTeX"
320 :group 'org-export)
322 ;;;; Generic
324 (defcustom org-latex-caption-above '(table)
325 "When non-nil, place caption string at the beginning of elements.
326 Otherwise, place it near the end. When value is a list of
327 symbols, put caption above selected elements only. Allowed
328 symbols are: `image', `table', `src-block' and `special-block'."
329 :group 'org-export-latex
330 :version "25.1"
331 :package-version '(Org . "8.3")
332 :type '(choice
333 (const :tag "For all elements" t)
334 (const :tag "For no element" nil)
335 (set :tag "For the following elements only" :greedy t
336 (const :tag "Images" image)
337 (const :tag "Tables" table)
338 (const :tag "Source code" src-block)
339 (const :tag "Special blocks" special-block))))
341 (defcustom org-latex-prefer-user-labels nil
342 "Use user-provided labels instead of internal ones when non-nil.
344 When this variable is non-nil, Org will use the value of
345 CUSTOM_ID property, NAME keyword or Org target as the key for the
346 \\label commands generated.
348 By default, Org generates its own internal labels during LaTeX
349 export. This process ensures that the \\label keys are unique
350 and valid, but it means the keys are not available in advance of
351 the export process.
353 Setting this variable gives you control over how Org generates
354 labels during LaTeX export, so that you may know their keys in
355 advance. One reason to do this is that it allows you to refer to
356 various elements using a single label both in Org's link syntax
357 and in embedded LaTeX code.
359 For example, when this variable is non-nil, a headline like this:
361 ** Some section
362 :PROPERTIES:
363 :CUSTOM_ID: sec:foo
364 :END:
365 This is section [[#sec:foo]].
366 #+BEGIN_LATEX
367 And this is still section \\ref{sec:foo}.
368 #+END_LATEX
370 will be exported to LaTeX as:
372 \\subsection{Some section}
373 \\label{sec:foo}
374 This is section \\ref{sec:foo}.
375 And this is still section \\ref{sec:foo}.
377 Note, however, that setting this variable introduces a limitation
378 on the possible values for CUSTOM_ID and NAME. When this
379 variable is non-nil, Org passes their value to \\label unchanged.
380 You are responsible for ensuring that the value is a valid LaTeX
381 \\label key, and that no other \\label commands with the same key
382 appear elsewhere in your document. (Keys may contain letters,
383 numbers, and the following punctuation: '_' '.' '-' ':'.) There
384 are no such limitations on CUSTOM_ID and NAME when this variable
385 is nil.
387 For headlines that do not define the CUSTOM_ID property or
388 elements without a NAME, Org will continue to use its default
389 labeling scheme to generate labels and resolve links into proper
390 references."
391 :group 'org-export-latex
392 :type 'boolean
393 :version "25.1"
394 :package-version '(Org . "8.3"))
396 ;;;; Preamble
398 (defcustom org-latex-default-class "article"
399 "The default LaTeX class."
400 :group 'org-export-latex
401 :type '(string :tag "LaTeX class"))
403 (defcustom org-latex-classes
404 '(("article"
405 "\\documentclass[11pt]{article}"
406 ("\\section{%s}" . "\\section*{%s}")
407 ("\\subsection{%s}" . "\\subsection*{%s}")
408 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
409 ("\\paragraph{%s}" . "\\paragraph*{%s}")
410 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
411 ("report"
412 "\\documentclass[11pt]{report}"
413 ("\\part{%s}" . "\\part*{%s}")
414 ("\\chapter{%s}" . "\\chapter*{%s}")
415 ("\\section{%s}" . "\\section*{%s}")
416 ("\\subsection{%s}" . "\\subsection*{%s}")
417 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
418 ("book"
419 "\\documentclass[11pt]{book}"
420 ("\\part{%s}" . "\\part*{%s}")
421 ("\\chapter{%s}" . "\\chapter*{%s}")
422 ("\\section{%s}" . "\\section*{%s}")
423 ("\\subsection{%s}" . "\\subsection*{%s}")
424 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
425 "Alist of LaTeX classes and associated header and structure.
426 If #+LATEX_CLASS is set in the buffer, use its value and the
427 associated information. Here is the structure of each cell:
429 \(class-name
430 header-string
431 \(numbered-section . unnumbered-section)
432 ...)
434 The header string
435 -----------------
437 The HEADER-STRING is the header that will be inserted into the
438 LaTeX file. It should contain the \\documentclass macro, and
439 anything else that is needed for this setup. To this header, the
440 following commands will be added:
442 - Calls to \\usepackage for all packages mentioned in the
443 variables `org-latex-default-packages-alist' and
444 `org-latex-packages-alist'. Thus, your header definitions
445 should avoid to also request these packages.
447 - Lines specified via \"#+LATEX_HEADER:\" and
448 \"#+LATEX_HEADER_EXTRA:\" keywords.
450 If you need more control about the sequence in which the header
451 is built up, or if you want to exclude one of these building
452 blocks for a particular class, you can use the following
453 macro-like placeholders.
455 [DEFAULT-PACKAGES] \\usepackage statements for default packages
456 [NO-DEFAULT-PACKAGES] do not include any of the default packages
457 [PACKAGES] \\usepackage statements for packages
458 [NO-PACKAGES] do not include the packages
459 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
460 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
462 So a header like
464 \\documentclass{article}
465 [NO-DEFAULT-PACKAGES]
466 [EXTRA]
467 \\providecommand{\\alert}[1]{\\textbf{#1}}
468 [PACKAGES]
470 will omit the default packages, and will include the
471 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
472 to \\providecommand, and then place \\usepackage commands based
473 on the content of `org-latex-packages-alist'.
475 If your header, `org-latex-default-packages-alist' or
476 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
477 AUTO will automatically be replaced with a coding system derived
478 from `buffer-file-coding-system'. See also the variable
479 `org-latex-inputenc-alist' for a way to influence this mechanism.
481 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\"
482 or \"\\usepackage[AUTO]{polyglossia}\", AUTO will be replaced
483 with the language related to the language code specified by
484 `org-export-default-language'. Note that constructions such as
485 \"\\usepackage[french,AUTO,english]{babel}\" are permitted. For
486 Polyglossia the language will be set via the macros
487 \"\\setmainlanguage\" and \"\\setotherlanguage\". See also
488 `org-latex-guess-babel-language' and
489 `org-latex-guess-polyglossia-language'.
491 The sectioning structure
492 ------------------------
494 The sectioning structure of the class is given by the elements
495 following the header string. For each sectioning level, a number
496 of strings is specified. A %s formatter is mandatory in each
497 section string and will be replaced by the title of the section.
499 Instead of a cons cell (numbered . unnumbered), you can also
500 provide a list of 2 or 4 elements,
502 \(numbered-open numbered-close)
506 \(numbered-open numbered-close unnumbered-open unnumbered-close)
508 providing opening and closing strings for a LaTeX environment
509 that should represent the document section. The opening clause
510 should have a %s to represent the section title.
512 Instead of a list of sectioning commands, you can also specify
513 a function name. That function will be called with two
514 parameters, the (reduced) level of the headline, and a predicate
515 non-nil when the headline should be numbered. It must return
516 a format string in which the section title will be added."
517 :group 'org-export-latex
518 :type '(repeat
519 (list (string :tag "LaTeX class")
520 (string :tag "LaTeX header")
521 (repeat :tag "Levels" :inline t
522 (choice
523 (cons :tag "Heading"
524 (string :tag " numbered")
525 (string :tag "unnumbered"))
526 (list :tag "Environment"
527 (string :tag "Opening (numbered)")
528 (string :tag "Closing (numbered)")
529 (string :tag "Opening (unnumbered)")
530 (string :tag "Closing (unnumbered)"))
531 (function :tag "Hook computing sectioning"))))))
533 (defcustom org-latex-inputenc-alist nil
534 "Alist of inputenc coding system names, and what should really be used.
535 For example, adding an entry
537 (\"utf8\" . \"utf8x\")
539 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
540 are written as utf8 files."
541 :group 'org-export-latex
542 :type '(repeat
543 (cons
544 (string :tag "Derived from buffer")
545 (string :tag "Use this instead"))))
547 (defcustom org-latex-title-command "\\maketitle"
548 "The command used to insert the title just after \\begin{document}.
550 This format string may contain these elements:
552 %a for AUTHOR keyword
553 %t for TITLE keyword
554 %s for SUBTITLE keyword
555 %k for KEYWORDS line
556 %d for DESCRIPTION line
557 %c for CREATOR line
558 %l for Language keyword
559 %L for capitalized language keyword
560 %D for DATE keyword
562 If you need to use a \"%\" character, you need to escape it
563 like that: \"%%\".
565 Setting :latex-title-command in publishing projects will take
566 precedence over this variable."
567 :group 'org-export-latex
568 :type '(string :tag "Format string"))
570 (defcustom org-latex-subtitle-format "\\\\\\medskip\n\\large %s"
571 "Format string used for transcoded subtitle.
572 The format string should have at most one \"%s\"-expression,
573 which is replaced with the subtitle."
574 :group 'org-export-latex
575 :version "25.1"
576 :package-version '(Org . "8.3")
577 :type '(string :tag "Format string"))
579 (defcustom org-latex-subtitle-separate nil
580 "Non-nil means the subtitle is not typeset as part of title."
581 :group 'org-export-latex
582 :version "25.1"
583 :package-version '(Org . "8.3")
584 :type 'boolean)
586 (defcustom org-latex-toc-command "\\tableofcontents\n\n"
587 "LaTeX command to set the table of contents, list of figures, etc.
588 This command only applies to the table of contents generated with
589 the toc:nil option, not to those generated with #+TOC keyword."
590 :group 'org-export-latex
591 :type 'string)
593 (defcustom org-latex-hyperref-template
594 "\\hypersetup{\n pdfauthor={%a},\n pdftitle={%t},\n pdfkeywords={%k},
595 pdfsubject={%d},\n pdfcreator={%c}, \n pdflang={%L}}\n"
596 "Template for hyperref package options.
598 This format string may contain these elements:
600 %a for AUTHOR keyword
601 %t for TITLE keyword
602 %s for SUBTITLE keyword
603 %k for KEYWORDS line
604 %d for DESCRIPTION line
605 %c for CREATOR line
606 %l for Language keyword
607 %L for capitalized language keyword
608 %D for DATE keyword
610 If you need to use a \"%\" character, you need to escape it
611 like that: \"%%\".
613 Setting :latex-hyperref-template in publishing projects will take
614 precedence over this variable."
615 :group 'org-export-latex
616 :version "25.1"
617 :package-version '(Org . "8.3")
618 :type '(string :tag "Format string"))
620 ;;;; Headline
622 (defcustom org-latex-format-headline-function
623 'org-latex-format-headline-default-function
624 "Function for formatting the headline's text.
626 This function will be called with six arguments:
627 TODO the todo keyword (string or nil)
628 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
629 PRIORITY the priority of the headline (integer or nil)
630 TEXT the main headline text (string)
631 TAGS the tags (list of strings or nil)
632 INFO the export options (plist)
634 The function result will be used in the section format string."
635 :group 'org-export-latex
636 :version "24.4"
637 :package-version '(Org . "8.0")
638 :type 'function)
641 ;;;; Footnotes
643 (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
644 "Text used to separate footnotes."
645 :group 'org-export-latex
646 :type 'string)
649 ;;;; Timestamps
651 (defcustom org-latex-active-timestamp-format "\\textit{%s}"
652 "A printf format string to be applied to active timestamps."
653 :group 'org-export-latex
654 :type 'string)
656 (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
657 "A printf format string to be applied to inactive timestamps."
658 :group 'org-export-latex
659 :type 'string)
661 (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
662 "A printf format string to be applied to diary timestamps."
663 :group 'org-export-latex
664 :type 'string)
667 ;;;; Links
669 (defcustom org-latex-image-default-option ""
670 "Default option for images."
671 :group 'org-export-latex
672 :version "24.4"
673 :package-version '(Org . "8.0")
674 :type 'string)
676 (defcustom org-latex-image-default-width ".9\\linewidth"
677 "Default width for images.
678 This value will not be used if a height is provided."
679 :group 'org-export-latex
680 :version "24.4"
681 :package-version '(Org . "8.0")
682 :type 'string)
684 (defcustom org-latex-image-default-height ""
685 "Default height for images.
686 This value will not be used if a width is provided, or if the
687 image is wrapped within a \"figure\" or \"wrapfigure\"
688 environment."
689 :group 'org-export-latex
690 :version "24.4"
691 :package-version '(Org . "8.0")
692 :type 'string)
694 (defcustom org-latex-default-figure-position "htb"
695 "Default position for latex figures."
696 :group 'org-export-latex
697 :type 'string)
699 (defcustom org-latex-inline-image-rules
700 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
701 "Rules characterizing image files that can be inlined into LaTeX.
703 A rule consists in an association whose key is the type of link
704 to consider, and value is a regexp that will be matched against
705 link's path.
707 Note that, by default, the image extension *actually* allowed
708 depend on the way the LaTeX file is processed. When used with
709 pdflatex, pdf, jpg and png images are OK. When processing
710 through dvi to Postscript, only ps and eps are allowed. The
711 default we use here encompasses both."
712 :group 'org-export-latex
713 :version "24.4"
714 :package-version '(Org . "8.0")
715 :type '(alist :key-type (string :tag "Type")
716 :value-type (regexp :tag "Path")))
718 (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
719 "Format string for links with unknown path type."
720 :group 'org-export-latex
721 :type 'string)
724 ;;;; Tables
726 (defcustom org-latex-default-table-environment "tabular"
727 "Default environment used to build tables."
728 :group 'org-export-latex
729 :version "24.4"
730 :package-version '(Org . "8.0")
731 :type 'string)
733 (defcustom org-latex-default-table-mode 'table
734 "Default mode for tables.
736 Value can be a symbol among:
738 `table' Regular LaTeX table.
740 `math' In this mode, every cell is considered as being in math
741 mode and the complete table will be wrapped within a math
742 environment. It is particularly useful to write matrices.
744 `inline-math' This mode is almost the same as `math', but the
745 math environment will be inlined.
747 `verbatim' The table is exported as it appears in the Org
748 buffer, within a verbatim environment.
750 This value can be overridden locally with, i.e. \":mode math\" in
751 LaTeX attributes.
753 When modifying this variable, it may be useful to change
754 `org-latex-default-table-environment' accordingly."
755 :group 'org-export-latex
756 :version "24.4"
757 :package-version '(Org . "8.0")
758 :type '(choice (const :tag "Table" table)
759 (const :tag "Matrix" math)
760 (const :tag "Inline matrix" inline-math)
761 (const :tag "Verbatim" verbatim))
762 :safe (lambda (s) (memq s '(table math inline-math verbatim))))
764 (defcustom org-latex-tables-centered t
765 "When non-nil, tables are exported in a center environment."
766 :group 'org-export-latex
767 :type 'boolean
768 :safe #'booleanp)
770 (defcustom org-latex-tables-booktabs nil
771 "When non-nil, display tables in a formal \"booktabs\" style.
772 This option assumes that the \"booktabs\" package is properly
773 loaded in the header of the document. This value can be ignored
774 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
775 attributes."
776 :group 'org-export-latex
777 :version "24.4"
778 :package-version '(Org . "8.0")
779 :type 'boolean
780 :safe #'booleanp)
782 (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
783 "Format string to display numbers in scientific notation.
784 The format should have \"%s\" twice, for mantissa and exponent
785 \(i.e., \"%s\\\\times10^{%s}\").
787 When nil, no transformation is made."
788 :group 'org-export-latex
789 :version "24.4"
790 :package-version '(Org . "8.0")
791 :type '(choice
792 (string :tag "Format string")
793 (const :tag "No formatting" nil)))
795 ;;;; Text markup
797 (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
798 (code . protectedtexttt)
799 (italic . "\\emph{%s}")
800 (strike-through . "\\sout{%s}")
801 (underline . "\\uline{%s}")
802 (verbatim . protectedtexttt))
803 "Alist of LaTeX expressions to convert text markup.
805 The key must be a symbol among `bold', `code', `italic',
806 `strike-through', `underline' and `verbatim'. The value is
807 a formatting string to wrap fontified text with.
809 Value can also be set to the following symbols: `verb' and
810 `protectedtexttt'. For the former, Org will use \"\\verb\" to
811 create a format string and select a delimiter character that
812 isn't in the string. For the latter, Org will use \"\\texttt\"
813 to typeset and try to protect special characters.
815 If no association can be found for a given markup, text will be
816 returned as-is."
817 :group 'org-export-latex
818 :version "25.1"
819 :package-version '(Org . "8.3")
820 :type 'alist
821 :options '(bold code italic strike-through underline verbatim))
824 ;;;; Drawers
826 (defcustom org-latex-format-drawer-function
827 (lambda (name contents) contents)
828 "Function called to format a drawer in LaTeX code.
830 The function must accept two parameters:
831 NAME the drawer name, like \"LOGBOOK\"
832 CONTENTS the contents of the drawer.
834 The function should return the string to be exported.
836 The default function simply returns the value of CONTENTS."
837 :group 'org-export-latex
838 :version "24.4"
839 :package-version '(Org . "8.3")
840 :type 'function)
843 ;;;; Inlinetasks
845 (defcustom org-latex-format-inlinetask-function
846 'org-latex-format-inlinetask-default-function
847 "Function called to format an inlinetask in LaTeX code.
849 The function must accept seven parameters:
850 TODO the todo keyword (string or nil)
851 TODO-TYPE the todo type (symbol: `todo', `done', nil)
852 PRIORITY the inlinetask priority (integer or nil)
853 NAME the inlinetask name (string)
854 TAGS the inlinetask tags (list of strings or nil)
855 CONTENTS the contents of the inlinetask (string or nil)
856 INFO the export options (plist)
858 The function should return the string to be exported."
859 :group 'org-export-latex
860 :type 'function
861 :version "25.1"
862 :package-version '(Org . "8.3"))
865 ;; Src blocks
867 (defcustom org-latex-listings nil
868 "Non-nil means export source code using the listings package.
870 This package will fontify source code, possibly even with color.
871 If you want to use this, you also need to make LaTeX use the
872 listings package, and if you want to have color, the color
873 package. Just add these to `org-latex-packages-alist', for
874 example using customize, or with something like:
876 \(require 'ox-latex)
877 \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
878 \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
880 Alternatively,
882 \(setq org-latex-listings 'minted)
884 causes source code to be exported using the minted package as
885 opposed to listings. If you want to use minted, you need to add
886 the minted package to `org-latex-packages-alist', for example
887 using customize, or with
889 \(require 'ox-latex)
890 \(add-to-list 'org-latex-packages-alist '(\"newfloat\" \"minted\"))
892 In addition, it is necessary to install pygments
893 \(http://pygments.org), and to configure the variable
894 `org-latex-pdf-process' so that the -shell-escape option is
895 passed to pdflatex.
897 The minted choice has possible repercussions on the preview of
898 latex fragments (see `org-preview-latex-fragment'). If you run
899 into previewing problems, please consult
901 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
902 :group 'org-export-latex
903 :type '(choice
904 (const :tag "Use listings" t)
905 (const :tag "Use minted" minted)
906 (const :tag "Export verbatim" nil))
907 :safe (lambda (s) (memq s '(t nil minted))))
909 (defcustom org-latex-listings-langs
910 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
911 (c "C") (cc "C++")
912 (fortran "fortran")
913 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
914 (html "HTML") (xml "XML")
915 (tex "TeX") (latex "[LaTeX]TeX")
916 (shell-script "bash")
917 (gnuplot "Gnuplot")
918 (ocaml "Caml") (caml "Caml")
919 (sql "SQL") (sqlite "sql")
920 (makefile "make"))
921 "Alist mapping languages to their listing language counterpart.
922 The key is a symbol, the major mode symbol without the \"-mode\".
923 The value is the string that should be inserted as the language
924 parameter for the listings package. If the mode name and the
925 listings name are the same, the language does not need an entry
926 in this list - but it does not hurt if it is present."
927 :group 'org-export-latex
928 :version "24.4"
929 :package-version '(Org . "8.3")
930 :type '(repeat
931 (list
932 (symbol :tag "Major mode ")
933 (string :tag "Listings language"))))
935 (defcustom org-latex-listings-options nil
936 "Association list of options for the latex listings package.
938 These options are supplied as a comma-separated list to the
939 \\lstset command. Each element of the association list should be
940 a list containing two strings: the name of the option, and the
941 value. For example,
943 \(setq org-latex-listings-options
944 '((\"basicstyle\" \"\\\\small\")
945 \(\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
947 will typeset the code in a small size font with underlined, bold
948 black keywords.
950 Note that the same options will be applied to blocks of all
951 languages. If you need block-specific options, you may use the
952 following syntax:
954 #+ATTR_LATEX: :options key1=value1,key2=value2
955 #+BEGIN_SRC <LANG>
957 #+END_SRC"
958 :group 'org-export-latex
959 :type '(repeat
960 (list
961 (string :tag "Listings option name ")
962 (string :tag "Listings option value"))))
964 (defcustom org-latex-minted-langs
965 '((emacs-lisp "common-lisp")
966 (cc "c++")
967 (cperl "perl")
968 (shell-script "bash")
969 (caml "ocaml"))
970 "Alist mapping languages to their minted language counterpart.
971 The key is a symbol, the major mode symbol without the \"-mode\".
972 The value is the string that should be inserted as the language
973 parameter for the minted package. If the mode name and the
974 listings name are the same, the language does not need an entry
975 in this list - but it does not hurt if it is present.
977 Note that minted uses all lower case for language identifiers,
978 and that the full list of language identifiers can be obtained
979 with:
981 pygmentize -L lexers"
982 :group 'org-export-latex
983 :type '(repeat
984 (list
985 (symbol :tag "Major mode ")
986 (string :tag "Minted language"))))
988 (defcustom org-latex-minted-options nil
989 "Association list of options for the latex minted package.
991 These options are supplied within square brackets in
992 \\begin{minted} environments. Each element of the alist should
993 be a list containing two strings: the name of the option, and the
994 value. For example,
996 \(setq org-latex-minted-options
997 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
999 will result in src blocks being exported with
1001 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
1003 as the start of the minted environment. Note that the same
1004 options will be applied to blocks of all languages. If you need
1005 block-specific options, you may use the following syntax:
1007 #+ATTR_LATEX: :options key1=value1,key2=value2
1008 #+BEGIN_SRC <LANG>
1010 #+END_SRC"
1011 :group 'org-export-latex
1012 :type '(repeat
1013 (list
1014 (string :tag "Minted option name ")
1015 (string :tag "Minted option value"))))
1017 (defvar org-latex-custom-lang-environments nil
1018 "Alist mapping languages to language-specific LaTeX environments.
1020 It is used during export of src blocks by the listings and minted
1021 latex packages. For example,
1023 \(setq org-latex-custom-lang-environments
1024 '\(\(python \"pythoncode\"\)\)\)
1026 would have the effect that if org encounters begin_src python
1027 during latex export it will output
1029 \\begin{pythoncode}
1030 <src block body>
1031 \\end{pythoncode}")
1034 ;;;; Compilation
1036 (defcustom org-latex-pdf-process
1037 '("pdflatex -interaction nonstopmode -output-directory %o %f"
1038 "pdflatex -interaction nonstopmode -output-directory %o %f"
1039 "pdflatex -interaction nonstopmode -output-directory %o %f")
1040 "Commands to process a LaTeX file to a PDF file.
1041 This is a list of strings, each of them will be given to the
1042 shell as a command. %f in the command will be replaced by the
1043 full file name, %b by the file base name (i.e. without directory
1044 and extension parts) and %o by the base directory of the file.
1046 The reason why this is a list is that it usually takes several
1047 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
1048 does not have a clever mechanism to detect which of these
1049 commands have to be run to get to a stable result, and it also
1050 does not do any error checking.
1052 By default, Org uses 3 runs of `pdflatex' to do the processing.
1053 If you have texi2dvi on your system and if that does not cause
1054 the infamous egrep/locale bug:
1056 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
1058 then `texi2dvi' is the superior choice as it automates the LaTeX
1059 build process by calling the \"correct\" combinations of
1060 auxiliary programs. Org does offer `texi2dvi' as one of the
1061 customize options. Alternatively, `rubber' and `latexmk' also
1062 provide similar functionality. The latter supports `biber' out
1063 of the box.
1065 Alternatively, this may be a Lisp function that does the
1066 processing, so you could use this to apply the machinery of
1067 AUCTeX or the Emacs LaTeX mode. This function should accept the
1068 file name as its single argument."
1069 :group 'org-export-pdf
1070 :type '(choice
1071 (repeat :tag "Shell command sequence"
1072 (string :tag "Shell command"))
1073 (const :tag "2 runs of pdflatex"
1074 ("pdflatex -interaction nonstopmode -output-directory %o %f"
1075 "pdflatex -interaction nonstopmode -output-directory %o %f"))
1076 (const :tag "3 runs of pdflatex"
1077 ("pdflatex -interaction nonstopmode -output-directory %o %f"
1078 "pdflatex -interaction nonstopmode -output-directory %o %f"
1079 "pdflatex -interaction nonstopmode -output-directory %o %f"))
1080 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
1081 ("pdflatex -interaction nonstopmode -output-directory %o %f"
1082 "bibtex %b"
1083 "pdflatex -interaction nonstopmode -output-directory %o %f"
1084 "pdflatex -interaction nonstopmode -output-directory %o %f"))
1085 (const :tag "2 runs of xelatex"
1086 ("xelatex -interaction nonstopmode -output-directory %o %f"
1087 "xelatex -interaction nonstopmode -output-directory %o %f"))
1088 (const :tag "3 runs of xelatex"
1089 ("xelatex -interaction nonstopmode -output-directory %o %f"
1090 "xelatex -interaction nonstopmode -output-directory %o %f"
1091 "xelatex -interaction nonstopmode -output-directory %o %f"))
1092 (const :tag "xelatex,bibtex,xelatex,xelatex"
1093 ("xelatex -interaction nonstopmode -output-directory %o %f"
1094 "bibtex %b"
1095 "xelatex -interaction nonstopmode -output-directory %o %f"
1096 "xelatex -interaction nonstopmode -output-directory %o %f"))
1097 (const :tag "texi2dvi"
1098 ("texi2dvi -p -b -V %f"))
1099 (const :tag "rubber"
1100 ("rubber -d --into %o %f"))
1101 (const :tag "latexmk"
1102 ("latexmk -g -pdf %f"))
1103 (function)))
1105 (defcustom org-latex-logfiles-extensions
1106 '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
1107 "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
1108 "The list of file extensions to consider as LaTeX logfiles.
1109 The logfiles will be removed if `org-latex-remove-logfiles' is
1110 non-nil."
1111 :group 'org-export-latex
1112 :version "25.1"
1113 :package-version '(Org . "8.3")
1114 :type '(repeat (string :tag "Extension")))
1116 (defcustom org-latex-remove-logfiles t
1117 "Non-nil means remove the logfiles produced by PDF production.
1118 By default, logfiles are files with these extensions: .aux, .idx,
1119 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
1120 logfiles to remove, set `org-latex-logfiles-extensions'."
1121 :group 'org-export-latex
1122 :type 'boolean)
1124 (defcustom org-latex-known-warnings
1125 '(("Reference.*?undefined" . "[undefined reference]")
1126 ("Runaway argument" . "[runaway argument]")
1127 ("Underfull \\hbox" . "[underfull hbox]")
1128 ("Overfull \\hbox" . "[overfull hbox]")
1129 ("Citation.*?undefined" . "[undefined citation]")
1130 ("Undefined control sequence" . "[undefined control sequence]"))
1131 "Alist of regular expressions and associated messages for the user.
1132 The regular expressions are used to find possible warnings in the
1133 log of a latex-run. These warnings will be reported after
1134 calling `org-latex-compile'."
1135 :group 'org-export-latex
1136 :version "25.1"
1137 :package-version '(Org . "8.3")
1138 :type '(repeat
1139 (cons
1140 (string :tag "Regexp")
1141 (string :tag "Message"))))
1145 ;;; Internal Functions
1147 (defun org-latex--caption-above-p (element info)
1148 "Non nil when caption is expected to be located above ELEMENT.
1149 INFO is a plist holding contextual information."
1150 (let ((above (plist-get info :latex-caption-above)))
1151 (if (symbolp above) above
1152 (let ((type (org-element-type element)))
1153 (memq (if (eq type 'link) 'image type) above)))))
1155 (defun org-latex--label (datum info &optional force full)
1156 "Return an appropriate label for DATUM.
1157 DATUM is an element or a `target' type object. INFO is the
1158 current export state, as a plist.
1160 Return nil if element DATUM has no NAME or VALUE affiliated
1161 keyword or no CUSTOM_ID property, unless FORCE is non-nil. In
1162 this case always return a unique label.
1164 Eventually, if FULL is non-nil, wrap label within \"\\label{}\"."
1165 (let* ((type (org-element-type datum))
1166 (user-label
1167 (org-element-property
1168 (case type
1169 ((headline inlinetask) :CUSTOM_ID)
1170 (target :value)
1171 (otherwise :name))
1172 datum))
1173 (label
1174 (and (or user-label force)
1175 (if (and user-label (plist-get info :latex-prefer-user-labels))
1176 user-label
1177 (concat (case type
1178 (headline "sec:")
1179 (table "tab:")
1180 (latex-environment
1181 (and (org-string-match-p
1182 org-latex-math-environments-re
1183 (org-element-property :value datum))
1184 "eq:"))
1185 (paragraph
1186 (and (org-element-property :caption datum)
1187 "fig:")))
1188 (org-export-get-reference datum info))))))
1189 (cond ((not full) label)
1190 (label (format "\\label{%s}%s"
1191 label
1192 (if (eq type 'target) "" "\n")))
1193 (t ""))))
1195 (defun org-latex--caption/label-string (element info)
1196 "Return caption and label LaTeX string for ELEMENT.
1198 INFO is a plist holding contextual information. If there's no
1199 caption nor label, return the empty string.
1201 For non-floats, see `org-latex--wrap-label'."
1202 (let* ((label (org-latex--label element info nil t))
1203 (main (org-export-get-caption element))
1204 (attr (org-export-read-attribute :attr_latex element))
1205 (type (org-element-type element))
1206 (nonfloat (or (and (plist-member attr :float)
1207 (not (plist-get attr :float))
1208 main)
1209 (and (eq type 'src-block)
1210 (not (plist-get attr :float))
1211 (memq (plist-get info :latex-listings)
1212 '(nil minted)))))
1213 (short (org-export-get-caption element t))
1214 (caption-from-attr-latex (plist-get attr :caption)))
1215 (cond
1216 ((org-string-nw-p caption-from-attr-latex)
1217 (concat caption-from-attr-latex "\n"))
1218 ((and (not main) (equal label "")) "")
1219 ((not main) (concat label "\n"))
1220 ;; Option caption format with short name.
1222 (format (if nonfloat "\\captionof{%s}%s{%s%s}\n"
1223 "\\caption%s%s{%s%s}\n")
1224 (if nonfloat
1225 (case type
1226 (paragraph "figure")
1227 (src-block (if (plist-get info :latex-listings)
1228 "listing"
1229 "figure"))
1230 (t (symbol-name type)))
1232 (if short (format "[%s]" (org-export-data short info)) "")
1233 label
1234 (org-export-data main info))))))
1236 (defun org-latex-guess-inputenc (header)
1237 "Set the coding system in inputenc to what the buffer is.
1239 HEADER is the LaTeX header string. This function only applies
1240 when specified inputenc option is \"AUTO\".
1242 Return the new header, as a string."
1243 (let* ((cs (or (ignore-errors
1244 (latexenc-coding-system-to-inputenc
1245 (or org-export-coding-system buffer-file-coding-system)))
1246 "utf8")))
1247 (if (not cs) header
1248 ;; First translate if that is requested.
1249 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
1250 ;; Then find the \usepackage statement and replace the option.
1251 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1252 cs header t nil 1))))
1254 (defun org-latex-guess-babel-language (header info)
1255 "Set Babel's language according to LANGUAGE keyword.
1257 HEADER is the LaTeX header string. INFO is the plist used as
1258 a communication channel.
1260 Insertion of guessed language only happens when Babel package has
1261 explicitly been loaded. Then it is added to the rest of
1262 package's options.
1264 The argument to Babel may be \"AUTO\" which is then replaced with
1265 the language of the document or `org-export-default-language'
1266 unless language in question is already loaded.
1268 Return the new header."
1269 (let ((language-code (plist-get info :language)))
1270 ;; If no language is set or Babel package is not loaded, return
1271 ;; HEADER as-is.
1272 (if (or (not (stringp language-code))
1273 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
1274 header
1275 (let ((options (save-match-data
1276 (org-split-string (match-string 1 header) ",[ \t]*")))
1277 (language (cdr (assoc language-code
1278 org-latex-babel-language-alist))))
1279 ;; If LANGUAGE is already loaded, return header without AUTO.
1280 ;; Otherwise, replace AUTO with language or append language if
1281 ;; AUTO is not present.
1282 (replace-match
1283 (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
1284 (cond ((member language options) (delete "AUTO" options))
1285 ((member "AUTO" options) options)
1286 (t (append options (list language))))
1287 ", ")
1288 t nil header 1)))))
1290 (defun org-latex-guess-polyglossia-language (header info)
1291 "Set the Polyglossia language according to the LANGUAGE keyword.
1293 HEADER is the LaTeX header string. INFO is the plist used as
1294 a communication channel.
1296 Insertion of guessed language only happens when the Polyglossia
1297 package has been explicitly loaded.
1299 The argument to Polyglossia may be \"AUTO\" which is then
1300 replaced with the language of the document or
1301 `org-export-default-language'. Note, the language is really set
1302 using \setdefaultlanguage and not as an option to the package.
1304 Return the new header."
1305 (let ((language (plist-get info :language)))
1306 ;; If no language is set or Polyglossia is not loaded, return
1307 ;; HEADER as-is.
1308 (if (or (not (stringp language))
1309 (not (string-match
1310 "\\\\usepackage\\(?:\\[\\([^]]+?\\)\\]\\){polyglossia}\n"
1311 header)))
1312 header
1313 (let* ((options (org-string-nw-p (match-string 1 header)))
1314 (languages (and options
1315 ;; Reverse as the last loaded language is
1316 ;; the main language.
1317 (nreverse
1318 (delete-dups
1319 (save-match-data
1320 (org-split-string
1321 (replace-regexp-in-string
1322 "AUTO" language options t)
1323 ",[ \t]*"))))))
1324 (main-language-set
1325 (string-match-p "\\\\setmainlanguage{.*?}" header)))
1326 (replace-match
1327 (concat "\\usepackage{polyglossia}\n"
1328 (mapconcat
1329 (lambda (l)
1330 (let ((l (or (assoc l org-latex-polyglossia-language-alist)
1331 l)))
1332 (format (if main-language-set "\\setotherlanguage%s{%s}\n"
1333 (setq main-language-set t)
1334 "\\setmainlanguage%s{%s}\n")
1335 (if (and (consp l) (= (length l) 3))
1336 (format "[variant=%s]" (nth 2 l))
1338 (nth 1 l))))
1339 languages
1340 ""))
1341 t t header 0)))))
1343 (defun org-latex--find-verb-separator (s)
1344 "Return a character not used in string S.
1345 This is used to choose a separator for constructs like \\verb."
1346 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1347 (loop for c across ll
1348 when (not (string-match (regexp-quote (char-to-string c)) s))
1349 return (char-to-string c))))
1351 (defun org-latex--make-option-string (options)
1352 "Return a comma separated string of keywords and values.
1353 OPTIONS is an alist where the key is the options keyword as
1354 a string, and the value a list containing the keyword value, or
1355 nil."
1356 (mapconcat (lambda (pair)
1357 (concat (first pair)
1358 (when (> (length (second pair)) 0)
1359 (concat "=" (second pair)))))
1360 options
1361 ","))
1363 (defun org-latex--wrap-label (element output info)
1364 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1365 INFO is the current export state, as a plist. This function
1366 should not be used for floats. See
1367 `org-latex--caption/label-string'."
1368 (if (not (and (org-string-nw-p output) (org-element-property :name element)))
1369 output
1370 (concat (format "\\phantomsection\n\\label{%s}\n"
1371 (org-latex--label element info))
1372 output)))
1374 (defun org-latex--protect-text (text)
1375 "Protect special characters in string TEXT and return it."
1376 (replace-regexp-in-string
1377 "--\\|[\\{}$%&_#~^]"
1378 (lambda (m)
1379 (cond ((equal m "--") "-{}-")
1380 ((equal m "\\") "\\textbackslash{}")
1381 ((equal m "~") "\\textasciitilde{}")
1382 ((equal m "^") "\\textasciicircum{}")
1383 (t (concat "\\" m))))
1384 text nil t))
1386 (defun org-latex--text-markup (text markup info)
1387 "Format TEXT depending on MARKUP text markup.
1388 INFO is a plist used as a communication channel. See
1389 `org-latex-text-markup-alist' for details."
1390 (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
1391 (case fmt
1392 ;; No format string: Return raw text.
1393 ((nil) text)
1394 ;; Handle the `verb' special case: Find an appropriate separator
1395 ;; and use "\\verb" command.
1396 (verb
1397 (let ((separator (org-latex--find-verb-separator text)))
1398 (concat "\\verb" separator
1399 (replace-regexp-in-string "\n" " " text)
1400 separator)))
1401 ;; Handle the `protectedtexttt' special case: Protect some
1402 ;; special chars and use "\texttt{%s}" format string.
1403 (protectedtexttt
1404 (format "\\texttt{%s}" (org-latex--protect-text text)))
1405 ;; Else use format string.
1406 (t (format fmt text)))))
1408 (defun org-latex--delayed-footnotes-definitions (element info)
1409 "Return footnotes definitions in ELEMENT as a string.
1411 INFO is a plist used as a communication channel.
1413 Footnotes definitions are returned within \"\\footnotetxt{}\"
1414 commands.
1416 This function is used within constructs that don't support
1417 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1418 \"\\footnotemark\" is used within the construct and the function
1419 just outside of it."
1420 (mapconcat
1421 (lambda (ref)
1422 (format
1423 "\\footnotetext[%s]{%s}"
1424 (org-export-get-footnote-number ref info)
1425 (org-trim
1426 (org-export-data
1427 (org-export-get-footnote-definition ref info) info))))
1428 ;; Find every footnote reference in ELEMENT.
1429 (let* (all-refs
1430 search-refs ; For byte-compiler.
1431 (search-refs
1432 (function
1433 (lambda (data)
1434 ;; Return a list of all footnote references never seen
1435 ;; before in DATA.
1436 (org-element-map data 'footnote-reference
1437 (lambda (ref)
1438 (when (org-export-footnote-first-reference-p ref info)
1439 (push ref all-refs)
1440 (when (eq (org-element-property :type ref) 'standard)
1441 (funcall search-refs
1442 (org-export-get-footnote-definition ref info)))))
1443 info)
1444 (reverse all-refs)))))
1445 (funcall search-refs element))
1446 ""))
1448 (defun org-latex--translate (s info)
1449 "Translate string S according to specified language.
1450 INFO is a plist used as a communication channel."
1451 (org-export-translate s :latex info))
1453 (defun org-latex--format-spec (info)
1454 "Create a format-spec for document meta-data.
1455 INFO is a plist used as a communication channel."
1456 (let ((language (let ((lang (plist-get info :language)))
1457 (or (cdr (assoc lang org-latex-babel-language-alist))
1458 lang))))
1459 `((?a . ,(org-export-data (plist-get info :author) info))
1460 (?t . ,(org-export-data (plist-get info :title) info))
1461 (?k . ,(org-export-data (org-latex--wrap-latex-math-block
1462 (plist-get info :keywords) info)
1463 info))
1464 (?d . ,(org-export-data (org-latex--wrap-latex-math-block
1465 (plist-get info :description) info)
1466 info))
1467 (?c . ,(plist-get info :creator))
1468 (?l . ,language)
1469 (?L . ,(capitalize language))
1470 (?D . ,(org-export-get-date info)))))
1472 (defun org-latex--make-header (info)
1473 "Return a formatted LaTeX header.
1474 INFO is a plist used as a communication channel."
1475 (let* ((class (plist-get info :latex-class))
1476 (class-options (plist-get info :latex-class-options))
1477 (header (nth 1 (assoc class (plist-get info :latex-classes))))
1478 (document-class-string
1479 (and (stringp header)
1480 (if (not class-options) header
1481 (replace-regexp-in-string
1482 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1483 class-options header t nil 1)))))
1484 (if (not document-class-string)
1485 (user-error "Unknown LaTeX class `%s'" class)
1486 (org-latex-guess-polyglossia-language
1487 (org-latex-guess-babel-language
1488 (org-latex-guess-inputenc
1489 (org-element-normalize-string
1490 (org-splice-latex-header
1491 document-class-string
1492 org-latex-default-packages-alist
1493 org-latex-packages-alist nil
1494 (concat (org-element-normalize-string
1495 (plist-get info :latex-header))
1496 (plist-get info :latex-header-extra)))))
1497 info)
1498 info))))
1501 ;;; Template
1503 (defun org-latex-template (contents info)
1504 "Return complete document string after LaTeX conversion.
1505 CONTENTS is the transcoded contents string. INFO is a plist
1506 holding export options."
1507 (let ((title (org-export-data (plist-get info :title) info))
1508 (spec (org-latex--format-spec info)))
1509 (concat
1510 ;; Time-stamp.
1511 (and (plist-get info :time-stamp-file)
1512 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1513 ;; Document class and packages.
1514 (org-latex--make-header info)
1515 ;; Possibly limit depth for headline numbering.
1516 (let ((sec-num (plist-get info :section-numbers)))
1517 (when (integerp sec-num)
1518 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1519 ;; Author.
1520 (let ((author (and (plist-get info :with-author)
1521 (let ((auth (plist-get info :author)))
1522 (and auth (org-export-data auth info)))))
1523 (email (and (plist-get info :with-email)
1524 (org-export-data (plist-get info :email) info))))
1525 (cond ((and author email (not (string= "" email)))
1526 (format "\\author{%s\\thanks{%s}}\n" author email))
1527 ((or author email) (format "\\author{%s}\n" (or author email)))))
1528 ;; Date.
1529 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1530 (format "\\date{%s}\n" (org-export-data date info)))
1531 ;; Title and subtitle.
1532 (let* ((subtitle (plist-get info :subtitle))
1533 (formatted-subtitle
1534 (when subtitle
1535 (format (plist-get info :latex-subtitle-format)
1536 (org-export-data subtitle info))))
1537 (separate (plist-get info :latex-subtitle-separate)))
1538 (concat
1539 (format "\\title{%s%s}\n" title
1540 (if separate "" (or formatted-subtitle "")))
1541 (when (and separate subtitle)
1542 (concat formatted-subtitle "\n"))))
1543 ;; Hyperref options.
1544 (let ((template (plist-get info :latex-hyperref-template)))
1545 (and (stringp template)
1546 (format-spec template spec)))
1547 ;; Document start.
1548 "\\begin{document}\n\n"
1549 ;; Title command.
1550 (let* ((title-command (plist-get info :latex-title-command))
1551 (command (and (stringp title-command)
1552 (format-spec title-command spec))))
1553 (org-element-normalize-string
1554 (cond ((not (plist-get info :with-title)) nil)
1555 ((string= "" title) nil)
1556 ((not (stringp command)) nil)
1557 ((string-match "\\(?:[^%]\\|^\\)%s" command)
1558 (format command title))
1559 (t command))))
1560 ;; Table of contents.
1561 (let ((depth (plist-get info :with-toc)))
1562 (when depth
1563 (concat (when (wholenump depth)
1564 (format "\\setcounter{tocdepth}{%d}\n" depth))
1565 (plist-get info :latex-toc-command))))
1566 ;; Document's body.
1567 contents
1568 ;; Creator.
1569 (and (plist-get info :with-creator)
1570 (concat (plist-get info :creator) "\n"))
1571 ;; Document end.
1572 "\\end{document}")))
1576 ;;; Transcode Functions
1578 ;;;; Bold
1580 (defun org-latex-bold (bold contents info)
1581 "Transcode BOLD from Org to LaTeX.
1582 CONTENTS is the text with bold markup. INFO is a plist holding
1583 contextual information."
1584 (org-latex--text-markup contents 'bold info))
1587 ;;;; Center Block
1589 (defun org-latex-center-block (center-block contents info)
1590 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1591 CONTENTS holds the contents of the center block. INFO is a plist
1592 holding contextual information."
1593 (org-latex--wrap-label
1594 center-block (format "\\begin{center}\n%s\\end{center}" contents) info))
1597 ;;;; Clock
1599 (defun org-latex-clock (clock contents info)
1600 "Transcode a CLOCK element from Org to LaTeX.
1601 CONTENTS is nil. INFO is a plist holding contextual
1602 information."
1603 (concat
1604 "\\noindent"
1605 (format "\\textbf{%s} " org-clock-string)
1606 (format (plist-get info :latex-inactive-timestamp-format)
1607 (concat (org-timestamp-translate (org-element-property :value clock))
1608 (let ((time (org-element-property :duration clock)))
1609 (and time (format " (%s)" time)))))
1610 "\\\\"))
1613 ;;;; Code
1615 (defun org-latex-code (code contents info)
1616 "Transcode a CODE object from Org to LaTeX.
1617 CONTENTS is nil. INFO is a plist used as a communication
1618 channel."
1619 (org-latex--text-markup (org-element-property :value code) 'code info))
1622 ;;;; Drawer
1624 (defun org-latex-drawer (drawer contents info)
1625 "Transcode a DRAWER element from Org to LaTeX.
1626 CONTENTS holds the contents of the block. INFO is a plist
1627 holding contextual information."
1628 (let* ((name (org-element-property :drawer-name drawer))
1629 (output (funcall (plist-get info :latex-format-drawer-function)
1630 name contents)))
1631 (org-latex--wrap-label drawer output info)))
1634 ;;;; Dynamic Block
1636 (defun org-latex-dynamic-block (dynamic-block contents info)
1637 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1638 CONTENTS holds the contents of the block. INFO is a plist
1639 holding contextual information. See `org-export-data'."
1640 (org-latex--wrap-label dynamic-block contents info))
1643 ;;;; Entity
1645 (defun org-latex-entity (entity contents info)
1646 "Transcode an ENTITY object from Org to LaTeX.
1647 CONTENTS are the definition itself. INFO is a plist holding
1648 contextual information."
1649 (org-element-property :latex entity))
1652 ;;;; Example Block
1654 (defun org-latex-example-block (example-block contents info)
1655 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1656 CONTENTS is nil. INFO is a plist holding contextual
1657 information."
1658 (when (org-string-nw-p (org-element-property :value example-block))
1659 (let ((environment (or (org-export-read-attribute
1660 :attr_latex example-block :environment)
1661 "verbatim")))
1662 (org-latex--wrap-label
1663 example-block
1664 (format "\\begin{%s}\n%s\\end{%s}"
1665 environment
1666 (org-export-format-code-default example-block info)
1667 environment)
1668 info))))
1671 ;;;; Export Block
1673 (defun org-latex-export-block (export-block contents info)
1674 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1675 CONTENTS is nil. INFO is a plist holding contextual information."
1676 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1677 (org-remove-indentation (org-element-property :value export-block))))
1680 ;;;; Export Snippet
1682 (defun org-latex-export-snippet (export-snippet contents info)
1683 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1684 CONTENTS is nil. INFO is a plist holding contextual information."
1685 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1686 (org-element-property :value export-snippet)))
1689 ;;;; Fixed Width
1691 (defun org-latex-fixed-width (fixed-width contents info)
1692 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1693 CONTENTS is nil. INFO is a plist holding contextual information."
1694 (org-latex--wrap-label
1695 fixed-width
1696 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1697 (org-remove-indentation
1698 (org-element-property :value fixed-width)))
1699 info))
1702 ;;;; Footnote Reference
1704 (defun org-latex-footnote-reference (footnote-reference contents info)
1705 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1706 CONTENTS is nil. INFO is a plist holding contextual information."
1707 (concat
1708 ;; Insert separator between two footnotes in a row.
1709 (let ((prev (org-export-get-previous-element footnote-reference info)))
1710 (when (eq (org-element-type prev) 'footnote-reference)
1711 (plist-get info :latex-footnote-separator)))
1712 (cond
1713 ;; Use \footnotemark if the footnote has already been defined.
1714 ((not (org-export-footnote-first-reference-p footnote-reference info))
1715 (format "\\footnotemark[%s]{}"
1716 (org-export-get-footnote-number footnote-reference info)))
1717 ;; Use \footnotemark if reference is within another footnote
1718 ;; reference, footnote definition or table cell.
1719 ((org-element-lineage footnote-reference
1720 '(footnote-reference footnote-definition table-cell))
1721 "\\footnotemark")
1722 ;; Otherwise, define it with \footnote command.
1724 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1725 (concat
1726 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1727 ;; Retrieve all footnote references within the footnote and
1728 ;; add their definition after it, since LaTeX doesn't support
1729 ;; them inside.
1730 (org-latex--delayed-footnotes-definitions def info)))))))
1733 ;;;; Headline
1735 (defun org-latex-headline (headline contents info)
1736 "Transcode a HEADLINE element from Org to LaTeX.
1737 CONTENTS holds the contents of the headline. INFO is a plist
1738 holding contextual information."
1739 (unless (org-element-property :footnote-section-p headline)
1740 (let* ((class (plist-get info :latex-class))
1741 (level (org-export-get-relative-level headline info))
1742 (numberedp (org-export-numbered-headline-p headline info))
1743 (class-sectioning (assoc class (plist-get info :latex-classes)))
1744 ;; Section formatting will set two placeholders: one for
1745 ;; the title and the other for the contents.
1746 (section-fmt
1747 (let ((sec (if (functionp (nth 2 class-sectioning))
1748 (funcall (nth 2 class-sectioning) level numberedp)
1749 (nth (1+ level) class-sectioning))))
1750 (cond
1751 ;; No section available for that LEVEL.
1752 ((not sec) nil)
1753 ;; Section format directly returned by a function. Add
1754 ;; placeholder for contents.
1755 ((stringp sec) (concat sec "\n%s"))
1756 ;; (numbered-section . unnumbered-section)
1757 ((not (consp (cdr sec)))
1758 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1759 ;; (numbered-open numbered-close)
1760 ((= (length sec) 2)
1761 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1762 ;; (num-in num-out no-num-in no-num-out)
1763 ((= (length sec) 4)
1764 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1765 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1766 ;; Create a temporary export back-end that hard-codes
1767 ;; "\underline" within "\section" and alike.
1768 (section-back-end
1769 (org-export-create-backend
1770 :parent 'latex
1771 :transcoders
1772 '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
1773 (text
1774 (org-export-data-with-backend
1775 (org-element-property :title headline) section-back-end info))
1776 (todo
1777 (and (plist-get info :with-todo-keywords)
1778 (let ((todo (org-element-property :todo-keyword headline)))
1779 (and todo (org-export-data todo info)))))
1780 (todo-type (and todo (org-element-property :todo-type headline)))
1781 (tags (and (plist-get info :with-tags)
1782 (org-export-get-tags headline info)))
1783 (priority (and (plist-get info :with-priority)
1784 (org-element-property :priority headline)))
1785 ;; Create the headline text along with a no-tag version.
1786 ;; The latter is required to remove tags from toc.
1787 (full-text (funcall (plist-get info :latex-format-headline-function)
1788 todo todo-type priority text tags info))
1789 ;; Associate \label to the headline for internal links.
1790 (headline-label (org-latex--label headline info t t))
1791 (pre-blanks
1792 (make-string (org-element-property :pre-blank headline) ?\n)))
1793 (if (or (not section-fmt) (org-export-low-level-p headline info))
1794 ;; This is a deep sub-tree: export it as a list item. Also
1795 ;; export as items headlines for which no section format has
1796 ;; been found.
1797 (let ((low-level-body
1798 (concat
1799 ;; If headline is the first sibling, start a list.
1800 (when (org-export-first-sibling-p headline info)
1801 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1802 ;; Itemize headline
1803 "\\item"
1804 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
1805 "\\relax")
1806 " " full-text "\n"
1807 headline-label
1808 pre-blanks
1809 contents)))
1810 ;; If headline is not the last sibling simply return
1811 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1812 ;; any blank line.
1813 (if (not (org-export-last-sibling-p headline info)) low-level-body
1814 (replace-regexp-in-string
1815 "[ \t\n]*\\'"
1816 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1817 low-level-body)))
1818 ;; This is a standard headline. Export it as a section. Add
1819 ;; an alternative heading when possible, and when this is not
1820 ;; identical to the usual heading.
1821 (let ((opt-title
1822 (funcall (plist-get info :latex-format-headline-function)
1823 todo todo-type priority
1824 (org-export-data-with-backend
1825 (org-export-get-alt-title headline info)
1826 section-back-end info)
1827 (and (eq (plist-get info :with-tags) t) tags)
1828 info))
1829 ;; Maybe end local TOC (see `org-latex-keyword').
1830 (contents
1831 (concat
1832 contents
1833 (let ((case-fold-search t)
1834 (section
1835 (let ((first (car (org-element-contents headline))))
1836 (and (eq (org-element-type first) 'section) first))))
1837 (org-element-map section 'keyword
1838 (lambda (k)
1839 (and (equal (org-element-property :key k) "TOC")
1840 (let ((v (org-element-property :value k)))
1841 (and (org-string-match-p "\\<headlines\\>" v)
1842 (org-string-match-p "\\<local\\>" v)
1843 (format "\\stopcontents[level-%d]" level)))))
1844 info t)))))
1845 (if (and opt-title
1846 (not (equal opt-title full-text))
1847 (string-match "\\`\\\\\\(.+?\\){" section-fmt))
1848 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1849 ;; Replace square brackets with parenthesis
1850 ;; since square brackets are not supported in
1851 ;; optional arguments.
1852 (replace-regexp-in-string
1853 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1854 full-text
1855 (concat headline-label pre-blanks contents))
1856 ;; Impossible to add an alternative heading. Fallback to
1857 ;; regular sectioning format string.
1858 (format section-fmt full-text
1859 (concat headline-label pre-blanks contents))))))))
1861 (defun org-latex-format-headline-default-function
1862 (todo todo-type priority text tags info)
1863 "Default format function for a headline.
1864 See `org-latex-format-headline-function' for details."
1865 (concat
1866 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1867 (and priority (format "\\framebox{\\#%c} " priority))
1868 text
1869 (and tags
1870 (format "\\hfill{}\\textsc{%s}"
1871 (mapconcat (lambda (tag) (org-latex-plain-text tag info))
1872 tags ":")))))
1875 ;;;; Horizontal Rule
1877 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1878 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1879 CONTENTS is nil. INFO is a plist holding contextual information."
1880 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1881 (prev (org-export-get-previous-element horizontal-rule info)))
1882 (concat
1883 ;; Make sure the rule doesn't start at the end of the current
1884 ;; line by separating it with a blank line from previous element.
1885 (when (and prev
1886 (let ((prev-blank (org-element-property :post-blank prev)))
1887 (or (not prev-blank) (zerop prev-blank))))
1888 "\n")
1889 (org-latex--wrap-label
1890 horizontal-rule
1891 (format "\\rule{%s}{%s}"
1892 (or (plist-get attr :width) "\\linewidth")
1893 (or (plist-get attr :thickness) "0.5pt"))
1894 info))))
1897 ;;;; Inline Src Block
1899 (defun org-latex-inline-src-block (inline-src-block contents info)
1900 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1901 CONTENTS holds the contents of the item. INFO is a plist holding
1902 contextual information."
1903 (let* ((code (org-element-property :value inline-src-block))
1904 (separator (org-latex--find-verb-separator code)))
1905 (case (plist-get info :latex-listings)
1906 ;; Do not use a special package: transcode it verbatim.
1907 ((nil) (format "\\texttt{%s}" (org-latex--protect-text code)))
1908 ;; Use minted package.
1909 (minted
1910 (let* ((org-lang (org-element-property :language inline-src-block))
1911 (mint-lang (or (cadr (assq (intern org-lang)
1912 (plist-get info :latex-minted-langs)))
1913 (downcase org-lang)))
1914 (options (org-latex--make-option-string
1915 (plist-get info :latex-minted-options))))
1916 (concat (format "\\mint%s{%s}"
1917 (if (string= options "") "" (format "[%s]" options))
1918 mint-lang)
1919 separator code separator)))
1920 ;; Use listings package.
1921 (otherwise
1922 ;; Maybe translate language's name.
1923 (let* ((org-lang (org-element-property :language inline-src-block))
1924 (lst-lang (or (cadr (assq (intern org-lang)
1925 (plist-get info :latex-listings-langs)))
1926 org-lang))
1927 (options (org-latex--make-option-string
1928 (append (plist-get info :latex-listings-options)
1929 `(("language" ,lst-lang))))))
1930 (concat (format "\\lstinline[%s]" options)
1931 separator code separator))))))
1934 ;;;; Inlinetask
1936 (defun org-latex-inlinetask (inlinetask contents info)
1937 "Transcode an INLINETASK element from Org to LaTeX.
1938 CONTENTS holds the contents of the block. INFO is a plist
1939 holding contextual information."
1940 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1941 (todo (and (plist-get info :with-todo-keywords)
1942 (let ((todo (org-element-property :todo-keyword inlinetask)))
1943 (and todo (org-export-data todo info)))))
1944 (todo-type (org-element-property :todo-type inlinetask))
1945 (tags (and (plist-get info :with-tags)
1946 (org-export-get-tags inlinetask info)))
1947 (priority (and (plist-get info :with-priority)
1948 (org-element-property :priority inlinetask)))
1949 (contents (concat (org-latex--label inlinetask info) contents)))
1950 (funcall (plist-get info :latex-format-inlinetask-function)
1951 todo todo-type priority title tags contents info)))
1953 (defun org-latex-format-inlinetask-default-function
1954 (todo todo-type priority title tags contents info)
1955 "Default format function for a inlinetasks.
1956 See `org-latex-format-inlinetask-function' for details."
1957 (let ((full-title
1958 (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1959 (when priority (format "\\framebox{\\#%c} " priority))
1960 title
1961 (when tags
1962 (format "\\hfill{}\\textsc{:%s:}"
1963 (mapconcat
1964 (lambda (tag) (org-latex-plain-text tag info))
1965 tags ":"))))))
1966 (concat "\\begin{center}\n"
1967 "\\fbox{\n"
1968 "\\begin{minipage}[c]{.6\\textwidth}\n"
1969 full-title "\n\n"
1970 (and (org-string-nw-p contents)
1971 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
1972 "\\end{minipage}\n"
1973 "}\n"
1974 "\\end{center}")))
1977 ;;;; Italic
1979 (defun org-latex-italic (italic contents info)
1980 "Transcode ITALIC from Org to LaTeX.
1981 CONTENTS is the text with italic markup. INFO is a plist holding
1982 contextual information."
1983 (org-latex--text-markup contents 'italic info))
1986 ;;;; Item
1988 (defun org-latex-item (item contents info)
1989 "Transcode an ITEM element from Org to LaTeX.
1990 CONTENTS holds the contents of the item. INFO is a plist holding
1991 contextual information."
1992 (let* ((counter
1993 (let ((count (org-element-property :counter item))
1994 (level
1995 ;; Determine level of current item to determine the
1996 ;; correct LaTeX counter to use (enumi, enumii...).
1997 (let ((parent item) (level 0))
1998 (while (memq (org-element-type
1999 (setq parent (org-export-get-parent parent)))
2000 '(plain-list item))
2001 (when (and (eq (org-element-type parent) 'plain-list)
2002 (eq (org-element-property :type parent)
2003 'ordered))
2004 (incf level)))
2005 level)))
2006 (and count
2007 (< level 5)
2008 (format "\\setcounter{enum%s}{%s}\n"
2009 (nth (1- level) '("i" "ii" "iii" "iv"))
2010 (1- count)))))
2011 (checkbox (case (org-element-property :checkbox item)
2012 (on "$\\boxtimes$ ")
2013 (off "$\\square$ ")
2014 (trans "$\\boxminus$ ")))
2015 (tag (let ((tag (org-element-property :tag item)))
2016 ;; Check-boxes must belong to the tag.
2017 (and tag (format "[{%s}] "
2018 (concat checkbox
2019 (org-export-data tag info)))))))
2020 (concat counter
2021 "\\item"
2022 (cond
2023 (tag)
2024 (checkbox (concat " " checkbox))
2025 ;; Without a tag or a check-box, if CONTENTS starts with
2026 ;; an opening square bracket, add "\relax" to "\item",
2027 ;; unless the brackets comes from an initial export
2028 ;; snippet (i.e. it is inserted willingly by the user).
2029 ((and contents
2030 (org-string-match-p "\\`[ \t]*\\[" contents)
2031 (not (let ((e (car (org-element-contents item))))
2032 (and (eq (org-element-type e) 'paragraph)
2033 (let ((o (car (org-element-contents e))))
2034 (and (eq (org-element-type o) 'export-snippet)
2035 (eq (org-export-snippet-backend o)
2036 'latex)))))))
2037 "\\relax ")
2038 (t " "))
2039 (and contents (org-trim contents))
2040 ;; If there are footnotes references in tag, be sure to
2041 ;; add their definition at the end of the item. This
2042 ;; workaround is necessary since "\footnote{}" command is
2043 ;; not supported in tags.
2044 (and tag
2045 (org-latex--delayed-footnotes-definitions
2046 (org-element-property :tag item) info)))))
2049 ;;;; Keyword
2051 (defun org-latex-keyword (keyword contents info)
2052 "Transcode a KEYWORD element from Org to LaTeX.
2053 CONTENTS is nil. INFO is a plist holding contextual information."
2054 (let ((key (org-element-property :key keyword))
2055 (value (org-element-property :value keyword)))
2056 (cond
2057 ((string= key "LATEX") value)
2058 ((string= key "INDEX") (format "\\index{%s}" value))
2059 ((string= key "TOC")
2060 (let ((case-fold-search t))
2061 (cond
2062 ((org-string-match-p "\\<headlines\\>" value)
2063 (let* ((localp (org-string-match-p "\\<local\\>" value))
2064 (parent (org-element-lineage keyword '(headline)))
2065 (level (if (not (and localp parent)) 0
2066 (org-export-get-relative-level parent info)))
2067 (depth
2068 (and (string-match "\\<[0-9]+\\>" value)
2069 (format
2070 "\\setcounter{tocdepth}{%d}"
2071 (+ (string-to-number (match-string 0 value)) level)))))
2072 (if (and localp parent)
2073 ;; Start local TOC, assuming package "titletoc" is
2074 ;; required.
2075 (format "\\startcontents[level-%d]
2076 \\printcontents[level-%d]{}{0}{%s}"
2077 level level (or depth ""))
2078 (concat depth (and depth "\n") "\\tableofcontents"))))
2079 ((org-string-match-p "\\<tables\\>" value) "\\listoftables")
2080 ((org-string-match-p "\\<listings\\>" value)
2081 (case (plist-get info :latex-listings)
2082 ((nil) "\\listoffigures")
2083 (minted "\\listoflistings")
2084 (otherwise "\\lstlistoflistings")))))))))
2087 ;;;; Latex Environment
2089 (defun org-latex-latex-environment (latex-environment contents info)
2090 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
2091 CONTENTS is nil. INFO is a plist holding contextual information."
2092 (when (plist-get info :with-latex)
2093 (let ((value (org-remove-indentation
2094 (org-element-property :value latex-environment))))
2095 (if (not (org-element-property :name latex-environment)) value
2096 ;; Environment is labeled: label must be within the environment
2097 ;; (otherwise, a reference pointing to that element will count
2098 ;; the section instead).
2099 (with-temp-buffer
2100 (insert value)
2101 (goto-char (point-min))
2102 (forward-line)
2103 (insert (org-latex--label latex-environment info nil t))
2104 (buffer-string))))))
2107 ;;;; Latex Fragment
2109 (defun org-latex-latex-fragment (latex-fragment contents info)
2110 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
2111 CONTENTS is nil. INFO is a plist holding contextual information."
2112 (let ((value (org-element-property :value latex-fragment)))
2113 ;; Trim math markers since the fragment is enclosed within
2114 ;; a latex-math-block object anyway.
2115 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
2116 (match-string 2 value))
2117 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
2118 (match-string 1 value))
2119 (t value))))
2122 ;;;; Line Break
2124 (defun org-latex-line-break (line-break contents info)
2125 "Transcode a LINE-BREAK object from Org to LaTeX.
2126 CONTENTS is nil. INFO is a plist holding contextual information."
2127 "\\\\\n")
2130 ;;;; Link
2132 (defun org-latex--inline-image (link info)
2133 "Return LaTeX code for an inline image.
2134 LINK is the link pointing to the inline image. INFO is a plist
2135 used as a communication channel."
2136 (let* ((parent (org-export-get-parent-element link))
2137 (path (let ((raw-path (org-element-property :path link)))
2138 (if (not (file-name-absolute-p raw-path)) raw-path
2139 (expand-file-name raw-path))))
2140 (filetype (file-name-extension path))
2141 (caption (org-latex--caption/label-string parent info))
2142 (caption-above-p (org-latex--caption-above-p link info))
2143 ;; Retrieve latex attributes from the element around.
2144 (attr (org-export-read-attribute :attr_latex parent))
2145 (float (let ((float (plist-get attr :float)))
2146 (cond ((string= float "wrap") 'wrap)
2147 ((string= float "sideways") 'sideways)
2148 ((string= float "multicolumn") 'multicolumn)
2149 ((or float
2150 (org-element-property :caption parent)
2151 (org-string-nw-p (plist-get attr :caption)))
2152 (if (and (plist-member attr :float) (not float))
2153 'nonfloat
2154 'figure))
2155 ((and (not float) (plist-member attr :float)) nil))))
2156 (placement
2157 (let ((place (plist-get attr :placement)))
2158 (cond
2159 (place (format "%s" place))
2160 ((eq float 'wrap) "{l}{0.5\\textwidth}")
2161 ((eq float 'figure)
2162 (format "[%s]" (plist-get info :latex-default-figure-position)))
2163 (t ""))))
2164 (comment-include (if (plist-get attr :comment-include) "%" ""))
2165 ;; It is possible to specify width and height in the
2166 ;; ATTR_LATEX line, and also via default variables.
2167 (width (cond ((plist-get attr :width))
2168 ((plist-get attr :height) "")
2169 ((eq float 'wrap) "0.48\\textwidth")
2170 (t (plist-get info :latex-image-default-width))))
2171 (height (cond ((plist-get attr :height))
2172 ((or (plist-get attr :width)
2173 (memq float '(figure wrap))) "")
2174 (t (plist-get info :latex-image-default-height))))
2175 (options (let ((opt (or (plist-get attr :options)
2176 (plist-get info :latex-image-default-option))))
2177 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
2178 (match-string 1 opt))))
2179 image-code)
2180 (if (member filetype '("tikz" "pgf"))
2181 ;; For tikz images:
2182 ;; - use \input to read in image file.
2183 ;; - if options are present, wrap in a tikzpicture environment.
2184 ;; - if width or height are present, use \resizebox to change
2185 ;; the image size.
2186 (progn
2187 (setq image-code (format "\\input{%s}" path))
2188 (when (org-string-nw-p options)
2189 (setq image-code
2190 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
2191 options
2192 image-code)))
2193 (when (or (org-string-nw-p width) (org-string-nw-p height))
2194 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
2195 (if (org-string-nw-p width) width "!")
2196 (if (org-string-nw-p height) height "!")
2197 image-code))))
2198 ;; For other images:
2199 ;; - add width and height to options.
2200 ;; - include the image with \includegraphics.
2201 (when (org-string-nw-p width)
2202 (setq options (concat options ",width=" width)))
2203 (when (org-string-nw-p height)
2204 (setq options (concat options ",height=" height)))
2205 (let ((search-option (org-element-property :search-option link)))
2206 (when (and search-option
2207 (equal filetype "pdf")
2208 (org-string-match-p "\\`[0-9]+\\'" search-option)
2209 (not (org-string-match-p "page=" options)))
2210 (setq options (concat options ",page=" search-option))))
2211 (setq image-code
2212 (format "\\includegraphics%s{%s}"
2213 (cond ((not (org-string-nw-p options)) "")
2214 ((= (aref options 0) ?,)
2215 (format "[%s]"(substring options 1)))
2216 (t (format "[%s]" options)))
2217 path))
2218 (when (equal filetype "svg")
2219 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
2220 "\\includesvg"
2221 image-code
2222 nil t))
2223 (setq image-code (replace-regexp-in-string "\\.svg}"
2225 image-code
2226 nil t))))
2227 ;; Return proper string, depending on FLOAT.
2228 (case float
2229 (wrap (format "\\begin{wrapfigure}%s
2230 %s\\centering
2231 %s%s
2232 %s\\end{wrapfigure}"
2233 placement
2234 (if caption-above-p caption "")
2235 comment-include image-code
2236 (if caption-above-p "" caption)))
2237 (sideways (format "\\begin{sidewaysfigure}
2238 %s\\centering
2239 %s%s
2240 %s\\end{sidewaysfigure}"
2241 (if caption-above-p caption "")
2242 comment-include image-code
2243 (if caption-above-p "" caption)))
2244 (multicolumn (format "\\begin{figure*}%s
2245 %s\\centering
2246 %s%s
2247 %s\\end{figure*}"
2248 placement
2249 (if caption-above-p caption "")
2250 comment-include image-code
2251 (if caption-above-p "" caption)))
2252 (figure (format "\\begin{figure}%s
2253 %s\\centering
2254 %s%s
2255 %s\\end{figure}"
2256 placement
2257 (if caption-above-p caption "")
2258 comment-include image-code
2259 (if caption-above-p "" caption)))
2260 (nonfloat
2261 (format "\\begin{center}
2262 %s%s
2263 %s\\end{center}"
2264 (if caption-above-p caption "")
2265 image-code
2266 (if caption-above-p "" caption)))
2267 (otherwise image-code))))
2269 (defun org-latex-link (link desc info)
2270 "Transcode a LINK object from Org to LaTeX.
2272 DESC is the description part of the link, or the empty string.
2273 INFO is a plist holding contextual information. See
2274 `org-export-data'."
2275 (let* ((type (org-element-property :type link))
2276 (raw-path (replace-regexp-in-string
2277 "%" "\\%" (org-element-property :path link) nil t))
2278 ;; Ensure DESC really exists, or set it to nil.
2279 (desc (and (not (string= desc "")) desc))
2280 (imagep (org-export-inline-image-p
2281 link (plist-get info :latex-inline-image-rules)))
2282 (path (cond
2283 ((member type '("http" "https" "ftp" "mailto" "doi"))
2284 (concat type ":" raw-path))
2285 ((string= type "file") (org-export-file-uri raw-path))
2286 (t raw-path))))
2287 (cond
2288 ;; Link type is handled by a special function.
2289 ((org-export-custom-protocol-maybe link desc 'latex))
2290 ;; Image file.
2291 (imagep (org-latex--inline-image link info))
2292 ;; Radio link: Transcode target's contents and use them as link's
2293 ;; description.
2294 ((string= type "radio")
2295 (let ((destination (org-export-resolve-radio-link link info)))
2296 (if (not destination) desc
2297 (format "\\hyperref[%s]{%s}"
2298 (org-export-get-reference destination info)
2299 desc))))
2300 ;; Links pointing to a headline: Find destination and build
2301 ;; appropriate referencing command.
2302 ((member type '("custom-id" "fuzzy" "id"))
2303 (let ((destination (if (string= type "fuzzy")
2304 (org-export-resolve-fuzzy-link link info)
2305 (org-export-resolve-id-link link info))))
2306 (case (org-element-type destination)
2307 ;; Id link points to an external file.
2308 (plain-text
2309 (if desc (format "\\href{%s}{%s}" destination desc)
2310 (format "\\url{%s}" destination)))
2311 ;; Fuzzy link points nowhere.
2312 ((nil)
2313 (format (plist-get info :latex-link-with-unknown-path-format)
2314 (or desc
2315 (org-export-data
2316 (org-element-property :raw-link link) info))))
2317 ;; LINK points to a headline. If headlines are numbered
2318 ;; and the link has no description, display headline's
2319 ;; number. Otherwise, display description or headline's
2320 ;; title.
2321 (headline
2322 (let ((label (org-latex--label destination info t)))
2323 (if (and (not desc)
2324 (org-export-numbered-headline-p destination info))
2325 (format "\\ref{%s}" label)
2326 (format "\\hyperref[%s]{%s}" label
2327 (or desc
2328 (org-export-data
2329 (org-element-property :title destination) info))))))
2330 ;; Fuzzy link points to a target. Do as above.
2331 (otherwise
2332 (let ((ref (org-latex--label destination info t)))
2333 (if (not desc) (format "\\ref{%s}" ref)
2334 (format "\\hyperref[%s]{%s}" ref desc)))))))
2335 ;; Coderef: replace link with the reference name or the
2336 ;; equivalent line number.
2337 ((string= type "coderef")
2338 (format (org-export-get-coderef-format path desc)
2339 (org-export-resolve-coderef path info)))
2340 ;; External link with a description part.
2341 ((and path desc) (format "\\href{%s}{%s}" path desc))
2342 ;; External link without a description part.
2343 (path (format "\\url{%s}" path))
2344 ;; No path, only description. Try to do something useful.
2345 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
2348 ;;;; Node Property
2350 (defun org-latex-node-property (node-property contents info)
2351 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2352 CONTENTS is nil. INFO is a plist holding contextual
2353 information."
2354 (format "%s:%s"
2355 (org-element-property :key node-property)
2356 (let ((value (org-element-property :value node-property)))
2357 (if value (concat " " value) ""))))
2360 ;;;; Paragraph
2362 (defun org-latex-paragraph (paragraph contents info)
2363 "Transcode a PARAGRAPH element from Org to LaTeX.
2364 CONTENTS is the contents of the paragraph, as a string. INFO is
2365 the plist used as a communication channel."
2366 contents)
2369 ;;;; Plain List
2371 (defun org-latex-plain-list (plain-list contents info)
2372 "Transcode a PLAIN-LIST element from Org to LaTeX.
2373 CONTENTS is the contents of the list. INFO is a plist holding
2374 contextual information."
2375 (let* ((type (org-element-property :type plain-list))
2376 (attr (org-export-read-attribute :attr_latex plain-list))
2377 (latex-type (let ((env (plist-get attr :environment)))
2378 (cond (env (format "%s" env))
2379 ((eq type 'ordered) "enumerate")
2380 ((eq type 'descriptive) "description")
2381 (t "itemize")))))
2382 (org-latex--wrap-label
2383 plain-list
2384 (format "\\begin{%s}%s\n%s\\end{%s}"
2385 latex-type
2386 (or (plist-get attr :options) "")
2387 contents
2388 latex-type)
2389 info)))
2392 ;;;; Plain Text
2394 (defun org-latex-plain-text (text info)
2395 "Transcode a TEXT string from Org to LaTeX.
2396 TEXT is the string to transcode. INFO is a plist holding
2397 contextual information."
2398 (let* ((specialp (plist-get info :with-special-strings))
2399 (output
2400 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2401 (let ((case-fold-search nil))
2402 (replace-regexp-in-string
2403 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2404 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2405 ;; However, if special strings are used, be careful not
2406 ;; to protect "\" in "\-" constructs.
2407 (replace-regexp-in-string
2408 (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\([^-]\\|$\\)"))
2409 (lambda (m)
2410 (case (string-to-char m)
2411 (?\\ "$\\\\backslash$\\1")
2412 (?~ "\\\\textasciitilde{}")
2413 (?^ "\\\\^{}")
2414 (t "\\\\\\&")))
2415 text)))))
2416 ;; Activate smart quotes. Be sure to provide original TEXT string
2417 ;; since OUTPUT may have been modified.
2418 (when (plist-get info :with-smart-quotes)
2419 (setq output (org-export-activate-smart-quotes output :latex info text)))
2420 ;; Convert special strings.
2421 (when specialp
2422 (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
2423 ;; Handle break preservation if required.
2424 (when (plist-get info :preserve-breaks)
2425 (setq output (replace-regexp-in-string
2426 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
2427 ;; Return value.
2428 output))
2431 ;;;; Planning
2433 (defun org-latex-planning (planning contents info)
2434 "Transcode a PLANNING element from Org to LaTeX.
2435 CONTENTS is nil. INFO is a plist holding contextual
2436 information."
2437 (concat
2438 "\\noindent"
2439 (mapconcat
2440 'identity
2441 (delq nil
2442 (list
2443 (let ((closed (org-element-property :closed planning)))
2444 (when closed
2445 (concat
2446 (format "\\textbf{%s} " org-closed-string)
2447 (format (plist-get info :latex-inactive-timestamp-format)
2448 (org-timestamp-translate closed)))))
2449 (let ((deadline (org-element-property :deadline planning)))
2450 (when deadline
2451 (concat
2452 (format "\\textbf{%s} " org-deadline-string)
2453 (format (plist-get info :latex-active-timestamp-format)
2454 (org-timestamp-translate deadline)))))
2455 (let ((scheduled (org-element-property :scheduled planning)))
2456 (when scheduled
2457 (concat
2458 (format "\\textbf{%s} " org-scheduled-string)
2459 (format (plist-get info :latex-active-timestamp-format)
2460 (org-timestamp-translate scheduled)))))))
2461 " ")
2462 "\\\\"))
2465 ;;;; Property Drawer
2467 (defun org-latex-property-drawer (property-drawer contents info)
2468 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2469 CONTENTS holds the contents of the drawer. INFO is a plist
2470 holding contextual information."
2471 (and (org-string-nw-p contents)
2472 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2475 ;;;; Pseudo Element: LaTeX Matrices
2477 ;; `latex-matrices' elements have the following properties:
2478 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2479 ;; `math').
2481 (defun org-latex--wrap-latex-matrices (data info)
2482 "Merge contiguous tables with the same mode within a pseudo-element.
2483 DATA is a parse tree or a secondary string. INFO is a plist
2484 containing export options. Modify DATA by side-effect and return
2485 it."
2486 (org-element-map data 'table
2487 (lambda (table)
2488 (when (eq (org-element-property :type table) 'org)
2489 (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
2490 (plist-get info :latex-default-table-mode))))
2491 (when (and (member mode '("inline-math" "math"))
2492 ;; Do not wrap twice the same table.
2493 (not (eq (org-element-type
2494 (org-element-property :parent table))
2495 'latex-matrices)))
2496 (let* ((caption (and (not (string= mode "inline-math"))
2497 (org-element-property :caption table)))
2498 (matrices
2499 (list 'latex-matrices
2500 (list :caption caption
2501 :markup
2502 (cond ((string= mode "inline-math") 'inline)
2503 (caption 'equation)
2504 (t 'math)))))
2505 (previous table)
2506 (next (org-export-get-next-element table info)))
2507 (org-element-insert-before matrices table)
2508 ;; Swallow all contiguous tables sharing the same mode.
2509 (while (and
2510 (zerop (or (org-element-property :post-blank previous) 0))
2511 (setq next (org-export-get-next-element previous info))
2512 (eq (org-element-type next) 'table)
2513 (eq (org-element-property :type next) 'org)
2514 (string= (or (org-export-read-attribute
2515 :attr_latex next :mode)
2516 (plist-get info :latex-default-table-mode))
2517 mode))
2518 (org-element-extract-element previous)
2519 (org-element-adopt-elements matrices previous)
2520 (setq previous next))
2521 (org-element-put-property
2522 matrices :post-blank (org-element-property :post-blank previous))
2523 (org-element-extract-element previous)
2524 (org-element-adopt-elements matrices previous))))))
2525 info)
2526 data)
2528 (defun org-latex-matrices (matrices contents info)
2529 "Transcode a MATRICES element from Org to LaTeX.
2530 CONTENTS is a string. INFO is a plist used as a communication
2531 channel."
2532 (format (case (org-element-property :markup matrices)
2533 (inline "\\(%s\\)")
2534 (equation "\\begin{equation}\n%s\\end{equation}")
2535 (t "\\[\n%s\\]"))
2536 contents))
2538 (defun org-latex-matrices-tree-filter (tree backend info)
2539 (org-latex--wrap-latex-matrices tree info))
2541 ;;;; Pseudo Object: LaTeX Math Block
2543 ;; `latex-math-block' objects have the following property:
2544 ;; `:post-blank'.
2546 (defun org-latex--wrap-latex-math-block (data info)
2547 "Merge contiguous math objects in a pseudo-object container.
2548 DATA is a parse tree or a secondary string. INFO is a plist
2549 containing export options. Modify DATA by side-effect and return it."
2550 (let ((valid-object-p
2551 (function
2552 ;; Non-nil when OBJ can be added to the latex math block.
2553 (lambda (obj)
2554 (case (org-element-type obj)
2555 (entity (org-element-property :latex-math-p obj))
2556 (latex-fragment
2557 (let ((value (org-element-property :value obj)))
2558 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2559 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2560 ((subscript superscript) t))))))
2561 (org-element-map data '(entity latex-fragment subscript superscript)
2562 (lambda (object)
2563 ;; Skip objects already wrapped.
2564 (when (and (not (eq (org-element-type
2565 (org-element-property :parent object))
2566 'latex-math-block))
2567 (funcall valid-object-p object))
2568 (let ((math-block (list 'latex-math-block nil))
2569 (next-elements (org-export-get-next-element object info t))
2570 (last object))
2571 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2572 (org-element-insert-before math-block object)
2573 (org-element-extract-element object)
2574 (org-element-adopt-elements math-block object)
2575 (when (zerop (or (org-element-property :post-blank object) 0))
2576 ;; MATH-BLOCK swallows consecutive math objects.
2577 (catch 'exit
2578 (dolist (next next-elements)
2579 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2580 (org-element-extract-element next)
2581 (org-element-adopt-elements math-block next)
2582 ;; Eschew the case: \beta$x$ -> \(\betax\).
2583 (unless (memq (org-element-type next)
2584 '(subscript superscript))
2585 (org-element-put-property last :post-blank 1))
2586 (setq last next)
2587 (when (> (or (org-element-property :post-blank next) 0) 0)
2588 (throw 'exit nil))))))
2589 (org-element-put-property
2590 math-block :post-blank (org-element-property :post-blank last)))))
2591 info nil '(subscript superscript latex-math-block) t)
2592 ;; Return updated DATA.
2593 data))
2595 (defun org-latex-math-block-tree-filter (tree backend info)
2596 (org-latex--wrap-latex-math-block tree info))
2598 (defun org-latex-math-block-options-filter (info backend)
2599 (dolist (prop '(:author :date :title) info)
2600 (plist-put info prop
2601 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2603 (defun org-latex-math-block (math-block contents info)
2604 "Transcode a MATH-BLOCK object from Org to LaTeX.
2605 CONTENTS is a string. INFO is a plist used as a communication
2606 channel."
2607 (when (org-string-nw-p contents)
2608 (format "\\(%s\\)" (org-trim contents))))
2610 ;;;; Quote Block
2612 (defun org-latex-quote-block (quote-block contents info)
2613 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2614 CONTENTS holds the contents of the block. INFO is a plist
2615 holding contextual information."
2616 (org-latex--wrap-label
2617 quote-block (format "\\begin{quote}\n%s\\end{quote}" contents) info))
2620 ;;;; Radio Target
2622 (defun org-latex-radio-target (radio-target text info)
2623 "Transcode a RADIO-TARGET object from Org to LaTeX.
2624 TEXT is the text of the target. INFO is a plist holding
2625 contextual information."
2626 (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
2629 ;;;; Section
2631 (defun org-latex-section (section contents info)
2632 "Transcode a SECTION element from Org to LaTeX.
2633 CONTENTS holds the contents of the section. INFO is a plist
2634 holding contextual information."
2635 contents)
2638 ;;;; Special Block
2640 (defun org-latex-special-block (special-block contents info)
2641 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2642 CONTENTS holds the contents of the block. INFO is a plist
2643 holding contextual information."
2644 (let ((type (org-element-property :type special-block))
2645 (opt (org-export-read-attribute :attr_latex special-block :options))
2646 (caption (org-latex--caption/label-string special-block info))
2647 (caption-above-p (org-latex--caption-above-p special-block info)))
2648 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2649 (and caption-above-p caption)
2650 contents
2651 (and (not caption-above-p) caption)
2652 (format "\\end{%s}" type))))
2655 ;;;; Src Block
2657 (defun org-latex-src-block (src-block contents info)
2658 "Transcode a SRC-BLOCK element from Org to LaTeX.
2659 CONTENTS holds the contents of the item. INFO is a plist holding
2660 contextual information."
2661 (when (org-string-nw-p (org-element-property :value src-block))
2662 (let* ((lang (org-element-property :language src-block))
2663 (caption (org-element-property :caption src-block))
2664 (caption-above-p (org-latex--caption-above-p src-block info))
2665 (label (org-element-property :name src-block))
2666 (custom-env (and lang
2667 (cadr (assq (intern lang)
2668 org-latex-custom-lang-environments))))
2669 (num-start (case (org-element-property :number-lines src-block)
2670 (continued (org-export-get-loc src-block info))
2671 (new 0)))
2672 (retain-labels (org-element-property :retain-labels src-block))
2673 (attributes (org-export-read-attribute :attr_latex src-block))
2674 (float (plist-get attributes :float))
2675 (listings (plist-get info :latex-listings)))
2676 (cond
2677 ;; Case 1. No source fontification.
2678 ((not listings)
2679 (let* ((caption-str (org-latex--caption/label-string src-block info))
2680 (float-env
2681 (cond ((string= "multicolumn" float)
2682 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2683 (plist-get info :latex-default-figure-position)
2684 (if caption-above-p caption-str "")
2685 (if caption-above-p "" caption-str)))
2686 (caption (concat
2687 (if caption-above-p caption-str "")
2688 "%s"
2689 (if caption-above-p "" (concat "\n" caption-str))))
2690 (t "%s"))))
2691 (format
2692 float-env
2693 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2694 (org-export-format-code-default src-block info))))))
2695 ;; Case 2. Custom environment.
2696 (custom-env
2697 (let ((caption-str (org-latex--caption/label-string src-block info)))
2698 (format "\\begin{%s}\n%s\\end{%s}\n"
2699 custom-env
2700 (concat (and caption-above-p caption-str)
2701 (org-export-format-code-default src-block info)
2702 (and (not caption-above-p) caption-str))
2703 custom-env)))
2704 ;; Case 3. Use minted package.
2705 ((eq listings 'minted)
2706 (let* ((caption-str (org-latex--caption/label-string src-block info))
2707 (float-env
2708 (cond
2709 ((string= "multicolumn" float)
2710 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2711 (if caption-above-p caption-str "")
2712 (if caption-above-p "" caption-str)))
2713 (caption
2714 (concat (if caption-above-p caption-str "")
2715 "%s"
2716 (if caption-above-p "" (concat "\n" caption-str))))
2717 (t "%s")))
2718 (options (plist-get info :latex-minted-options))
2719 (body
2720 (format
2721 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2722 ;; Options.
2723 (concat
2724 (org-latex--make-option-string
2725 (if (or (not num-start) (assoc "linenos" options))
2726 options
2727 (append
2728 `(("linenos")
2729 ("firstnumber" ,(number-to-string (1+ num-start))))
2730 options)))
2731 (let ((local-options (plist-get attributes :options)))
2732 (and local-options (concat "," local-options))))
2733 ;; Language.
2734 (or (cadr (assq (intern lang)
2735 (plist-get info :latex-minted-langs)))
2736 (downcase lang))
2737 ;; Source code.
2738 (let* ((code-info (org-export-unravel-code src-block))
2739 (max-width
2740 (apply 'max
2741 (mapcar 'length
2742 (org-split-string (car code-info)
2743 "\n")))))
2744 (org-export-format-code
2745 (car code-info)
2746 (lambda (loc num ref)
2747 (concat
2749 (when ref
2750 ;; Ensure references are flushed to the right,
2751 ;; separated with 6 spaces from the widest line
2752 ;; of code.
2753 (concat (make-string (+ (- max-width (length loc)) 6)
2754 ?\s)
2755 (format "(%s)" ref)))))
2756 nil (and retain-labels (cdr code-info)))))))
2757 ;; Return value.
2758 (format float-env body)))
2759 ;; Case 4. Use listings package.
2761 (let ((lst-lang
2762 (or (cadr (assq (intern lang)
2763 (plist-get info :latex-listings-langs)))
2764 lang))
2765 (caption-str
2766 (when caption
2767 (let ((main (org-export-get-caption src-block))
2768 (secondary (org-export-get-caption src-block t)))
2769 (if (not secondary)
2770 (format "{%s}" (org-export-data main info))
2771 (format "{[%s]%s}"
2772 (org-export-data secondary info)
2773 (org-export-data main info))))))
2774 (lst-opt (plist-get info :latex-listings-options)))
2775 (concat
2776 ;; Options.
2777 (format
2778 "\\lstset{%s}\n"
2779 (concat
2780 (org-latex--make-option-string
2781 (append
2782 lst-opt
2783 (cond
2784 ((and (not float) (plist-member attributes :float)) nil)
2785 ((string= "multicolumn" float) '(("float" "*")))
2786 ((and float (not (assoc "float" lst-opt)))
2787 `(("float" ,(plist-get info :latex-default-figure-position)))))
2788 `(("language" ,lst-lang))
2789 (if label `(("label" ,label)) '(("label" " ")))
2790 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2791 `(("captionpos" ,(if caption-above-p "t" "b")))
2792 (cond ((assoc "numbers" lst-opt) nil)
2793 ((not num-start) '(("numbers" "none")))
2794 ((zerop num-start) '(("numbers" "left")))
2795 (t `(("firstnumber" ,(number-to-string (1+ num-start)))
2796 ("numbers" "left"))))))
2797 (let ((local-options (plist-get attributes :options)))
2798 (and local-options (concat "," local-options)))))
2799 ;; Source code.
2800 (format
2801 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2802 (let* ((code-info (org-export-unravel-code src-block))
2803 (max-width
2804 (apply 'max
2805 (mapcar 'length
2806 (org-split-string (car code-info) "\n")))))
2807 (org-export-format-code
2808 (car code-info)
2809 (lambda (loc num ref)
2810 (concat
2812 (when ref
2813 ;; Ensure references are flushed to the right,
2814 ;; separated with 6 spaces from the widest line of
2815 ;; code
2816 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2817 (format "(%s)" ref)))))
2818 nil (and retain-labels (cdr code-info))))))))))))
2821 ;;;; Statistics Cookie
2823 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2824 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2825 CONTENTS is nil. INFO is a plist holding contextual information."
2826 (replace-regexp-in-string
2827 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2830 ;;;; Strike-Through
2832 (defun org-latex-strike-through (strike-through contents info)
2833 "Transcode STRIKE-THROUGH from Org to LaTeX.
2834 CONTENTS is the text with strike-through markup. INFO is a plist
2835 holding contextual information."
2836 (org-latex--text-markup contents 'strike-through info))
2839 ;;;; Subscript
2841 (defun org-latex--script-size (object info)
2842 "Transcode a subscript or superscript object.
2843 OBJECT is an Org object. INFO is a plist used as a communication
2844 channel."
2845 (let ((type (org-element-type object))
2846 (output ""))
2847 (org-element-map (org-element-contents object)
2848 (cons 'plain-text org-element-all-objects)
2849 (lambda (obj)
2850 (case (org-element-type obj)
2851 ((entity latex-fragment)
2852 (let ((data (org-trim (org-export-data obj info))))
2853 (string-match
2854 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2855 data)
2856 (setq output
2857 (concat output
2858 (match-string 1 data)
2859 (let ((blank (org-element-property :post-blank obj)))
2860 (and blank (> blank 0) "\\ "))))))
2861 (plain-text
2862 (setq output
2863 (format "%s\\text{%s}" output (org-export-data obj info))))
2864 (otherwise
2865 (setq output
2866 (concat output
2867 (org-export-data obj info)
2868 (let ((blank (org-element-property :post-blank obj)))
2869 (and blank (> blank 0) "\\ ")))))))
2870 info nil org-element-recursive-objects)
2871 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2872 ;; character.
2873 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2874 (and (> (length output) 1) "{")
2875 output
2876 (and (> (length output) 1) "}"))))
2878 (defun org-latex-subscript (subscript contents info)
2879 "Transcode a SUBSCRIPT object from Org to LaTeX.
2880 CONTENTS is the contents of the object. INFO is a plist holding
2881 contextual information."
2882 (org-latex--script-size subscript info))
2885 ;;;; Superscript
2887 (defun org-latex-superscript (superscript contents info)
2888 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2889 CONTENTS is the contents of the object. INFO is a plist holding
2890 contextual information."
2891 (org-latex--script-size superscript info))
2894 ;;;; Table
2896 ;; `org-latex-table' is the entry point for table transcoding. It
2897 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2898 ;; delegates the job to either `org-latex--table.el-table',
2899 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2900 ;; depending of the type of the table and the mode requested.
2902 ;; `org-latex--align-string' is a subroutine used to build alignment
2903 ;; string for Org tables.
2905 (defun org-latex-table (table contents info)
2906 "Transcode a TABLE element from Org to LaTeX.
2907 CONTENTS is the contents of the table. INFO is a plist holding
2908 contextual information."
2909 (if (eq (org-element-property :type table) 'table.el)
2910 ;; "table.el" table. Convert it using appropriate tools.
2911 (org-latex--table.el-table table info)
2912 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2913 (plist-get info :latex-default-table-mode))))
2914 (cond
2915 ;; Case 1: Verbatim table.
2916 ((string= type "verbatim")
2917 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2918 ;; Re-create table, without affiliated keywords.
2919 (org-trim (org-element-interpret-data
2920 `(table nil ,@(org-element-contents table))))))
2921 ;; Case 2: Matrix.
2922 ((or (string= type "math") (string= type "inline-math"))
2923 (org-latex--math-table table info))
2924 ;; Case 3: Standard table.
2925 (t (concat (org-latex--org-table table contents info)
2926 ;; When there are footnote references within the
2927 ;; table, insert their definition just after it.
2928 (org-latex--delayed-footnotes-definitions table info)))))))
2930 (defun org-latex--align-string (table info)
2931 "Return an appropriate LaTeX alignment string.
2932 TABLE is the considered table. INFO is a plist used as
2933 a communication channel."
2934 (or (org-export-read-attribute :attr_latex table :align)
2935 (let (align)
2936 ;; Extract column groups and alignment from first (non-rule)
2937 ;; row.
2938 (org-element-map
2939 (org-element-map table 'table-row
2940 (lambda (row)
2941 (and (eq (org-element-property :type row) 'standard) row))
2942 info 'first-match)
2943 'table-cell
2944 (lambda (cell)
2945 (let ((borders (org-export-table-cell-borders cell info)))
2946 ;; Check left border for the first cell only.
2947 (when (and (memq 'left borders) (not align))
2948 (push "|" align))
2949 (push (case (org-export-table-cell-alignment cell info)
2950 (left "l")
2951 (right "r")
2952 (center "c"))
2953 align)
2954 (when (memq 'right borders) (push "|" align))))
2955 info)
2956 (apply 'concat (nreverse align)))))
2958 (defun org-latex--org-table (table contents info)
2959 "Return appropriate LaTeX code for an Org table.
2961 TABLE is the table type element to transcode. CONTENTS is its
2962 contents, as a string. INFO is a plist used as a communication
2963 channel.
2965 This function assumes TABLE has `org' as its `:type' property and
2966 `table' as its `:mode' attribute."
2967 (let* ((caption (org-latex--caption/label-string table info))
2968 (attr (org-export-read-attribute :attr_latex table))
2969 ;; Determine alignment string.
2970 (alignment (org-latex--align-string table info))
2971 ;; Determine environment for the table: longtable, tabular...
2972 (table-env (or (plist-get attr :environment)
2973 (plist-get info :latex-default-table-environment)))
2974 ;; If table is a float, determine environment: table, table*
2975 ;; or sidewaystable.
2976 (float-env (unless (member table-env '("longtable" "longtabu"))
2977 (let ((float (plist-get attr :float)))
2978 (cond
2979 ((and (not float) (plist-member attr :float)) nil)
2980 ((or (string= float "sidewaystable")
2981 (string= float "sideways")) "sidewaystable")
2982 ((string= float "multicolumn") "table*")
2983 ((or float
2984 (org-element-property :caption table)
2985 (org-string-nw-p (plist-get attr :caption)))
2986 "table")))))
2987 ;; Extract others display options.
2988 (fontsize (let ((font (plist-get attr :font)))
2989 (and font (concat font "\n"))))
2990 ;; "tabular" environment doesn't allow to define a width.
2991 (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
2992 (spreadp (plist-get attr :spread))
2993 (placement
2994 (or (plist-get attr :placement)
2995 (format "[%s]" (plist-get info :latex-default-figure-position))))
2996 (centerp (if (plist-member attr :center) (plist-get attr :center)
2997 (plist-get info :latex-tables-centered)))
2998 (caption-above-p (org-latex--caption-above-p table info)))
2999 ;; Prepare the final format string for the table.
3000 (cond
3001 ;; Longtable.
3002 ((equal "longtable" table-env)
3003 (concat (and fontsize (concat "{" fontsize))
3004 (format "\\begin{longtable}{%s}\n" alignment)
3005 (and caption-above-p
3006 (org-string-nw-p caption)
3007 (concat caption "\\\\\n"))
3008 contents
3009 (and (not caption-above-p)
3010 (org-string-nw-p caption)
3011 (concat caption "\\\\\n"))
3012 "\\end{longtable}\n"
3013 (and fontsize "}")))
3014 ;; Longtabu
3015 ((equal "longtabu" table-env)
3016 (concat (and fontsize (concat "{" fontsize))
3017 (format "\\begin{longtabu}%s{%s}\n"
3018 (if width
3019 (format " %s %s "
3020 (if spreadp "spread" "to") width) "")
3021 alignment)
3022 (and caption-above-p
3023 (org-string-nw-p caption)
3024 (concat caption "\\\\\n"))
3025 contents
3026 (and (not caption-above-p)
3027 (org-string-nw-p caption)
3028 (concat caption "\\\\\n"))
3029 "\\end{longtabu}\n"
3030 (and fontsize "}")))
3031 ;; Others.
3032 (t (concat (cond
3033 (float-env
3034 (concat (format "\\begin{%s}%s\n" float-env placement)
3035 (if caption-above-p caption "")
3036 (when centerp "\\centering\n")
3037 fontsize))
3038 ((and (not float-env) caption)
3039 (concat
3040 (and centerp "\\begin{center}\n" )
3041 (if caption-above-p caption "")
3042 (cond ((and fontsize centerp) fontsize)
3043 (fontsize (concat "{" fontsize)))))
3044 (centerp (concat "\\begin{center}\n" fontsize))
3045 (fontsize (concat "{" fontsize)))
3046 (cond ((equal "tabu" table-env)
3047 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
3048 (if width (format
3049 (if spreadp " spread %s " " to %s ")
3050 width) "")
3051 alignment
3052 contents))
3053 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
3054 table-env
3055 (if width (format "{%s}" width) "")
3056 alignment
3057 contents
3058 table-env)))
3059 (cond
3060 (float-env
3061 (concat (if caption-above-p "" (concat "\n" caption))
3062 (format "\n\\end{%s}" float-env)))
3063 ((and (not float-env) caption)
3064 (concat
3065 (if caption-above-p "" (concat "\n" caption))
3066 (and centerp "\n\\end{center}")
3067 (and fontsize (not centerp) "}")))
3068 (centerp "\n\\end{center}")
3069 (fontsize "}")))))))
3071 (defun org-latex--table.el-table (table info)
3072 "Return appropriate LaTeX code for a table.el table.
3074 TABLE is the table type element to transcode. INFO is a plist
3075 used as a communication channel.
3077 This function assumes TABLE has `table.el' as its `:type'
3078 property."
3079 (require 'table)
3080 ;; Ensure "*org-export-table*" buffer is empty.
3081 (with-current-buffer (get-buffer-create "*org-export-table*")
3082 (erase-buffer))
3083 (let ((output (with-temp-buffer
3084 (insert (org-element-property :value table))
3085 (goto-char 1)
3086 (re-search-forward "^[ \t]*|[^|]" nil t)
3087 (table-generate-source 'latex "*org-export-table*")
3088 (with-current-buffer "*org-export-table*"
3089 (org-trim (buffer-string))))))
3090 (kill-buffer (get-buffer "*org-export-table*"))
3091 ;; Remove left out comments.
3092 (while (string-match "^%.*\n" output)
3093 (setq output (replace-match "" t t output)))
3094 (let ((attr (org-export-read-attribute :attr_latex table)))
3095 (when (plist-get attr :rmlines)
3096 ;; When the "rmlines" attribute is provided, remove all hlines
3097 ;; but the the one separating heading from the table body.
3098 (let ((n 0) (pos 0))
3099 (while (and (< (length output) pos)
3100 (setq pos (string-match "^\\\\hline\n?" output pos)))
3101 (incf n)
3102 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
3103 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
3104 (plist-get info :latex-tables-centered))))
3105 (if (not centerp) output
3106 (format "\\begin{center}\n%s\n\\end{center}" output))))))
3108 (defun org-latex--math-table (table info)
3109 "Return appropriate LaTeX code for a matrix.
3111 TABLE is the table type element to transcode. INFO is a plist
3112 used as a communication channel.
3114 This function assumes TABLE has `org' as its `:type' property and
3115 `inline-math' or `math' as its `:mode' attribute."
3116 (let* ((attr (org-export-read-attribute :attr_latex table))
3117 (env (or (plist-get attr :environment)
3118 (plist-get info :latex-default-table-environment)))
3119 (contents
3120 (mapconcat
3121 (lambda (row)
3122 ;; Ignore horizontal rules.
3123 (when (eq (org-element-property :type row) 'standard)
3124 ;; Return each cell unmodified.
3125 (concat
3126 (mapconcat
3127 (lambda (cell)
3128 (substring (org-element-interpret-data cell) 0 -1))
3129 (org-element-map row 'table-cell #'identity info) "&")
3130 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
3131 "\n")))
3132 (org-element-map table 'table-row #'identity info) "")))
3133 (concat
3134 ;; Prefix.
3135 (plist-get attr :math-prefix)
3136 ;; Environment. Also treat special cases.
3137 (cond ((member env '("array" "tabular"))
3138 (let ((align (make-string
3139 (cdr (org-export-table-dimensions table info)) ?c)))
3140 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
3141 ((assoc env org-latex-table-matrix-macros)
3142 (format "\\%s%s{\n%s}"
3144 (or (plist-get attr :math-arguments) "")
3145 contents))
3146 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
3147 ;; Suffix.
3148 (plist-get attr :math-suffix))))
3151 ;;;; Table Cell
3153 (defun org-latex-table-cell (table-cell contents info)
3154 "Transcode a TABLE-CELL element from Org to LaTeX.
3155 CONTENTS is the cell contents. INFO is a plist used as
3156 a communication channel."
3157 (concat
3158 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
3159 (if (and contents
3160 scientific-format
3161 (string-match orgtbl-exp-regexp contents))
3162 ;; Use appropriate format string for scientific
3163 ;; notation.
3164 (format scientific-format
3165 (match-string 1 contents)
3166 (match-string 2 contents))
3167 contents))
3168 (when (org-export-get-next-element table-cell info) " & ")))
3171 ;;;; Table Row
3173 (defun org-latex-table-row (table-row contents info)
3174 "Transcode a TABLE-ROW element from Org to LaTeX.
3175 CONTENTS is the contents of the row. INFO is a plist used as
3176 a communication channel."
3177 (let* ((attr (org-export-read-attribute :attr_latex
3178 (org-export-get-parent table-row)))
3179 (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
3180 (plist-get info :latex-tables-booktabs)))
3181 (longtablep
3182 (member (or (plist-get attr :environment)
3183 (plist-get info :latex-default-table-environment))
3184 '("longtable" "longtabu"))))
3185 (if (eq (org-element-property :type table-row) 'rule)
3186 (cond
3187 ((not booktabsp) "\\hline")
3188 ((not (org-export-get-previous-element table-row info)) "\\toprule")
3189 ((not (org-export-get-next-element table-row info)) "\\bottomrule")
3190 ((and longtablep
3191 (org-export-table-row-ends-header-p
3192 (org-export-get-previous-element table-row info) info))
3194 (t "\\midrule"))
3195 (concat
3196 ;; When BOOKTABS are activated enforce top-rule even when no
3197 ;; hline was specifically marked.
3198 (and booktabsp (not (org-export-get-previous-element table-row info))
3199 "\\toprule\n")
3200 contents "\\\\\n"
3201 (cond
3202 ;; Special case for long tables. Define header and footers.
3203 ((and longtablep (org-export-table-row-ends-header-p table-row info))
3204 (let ((columns (cdr (org-export-table-dimensions
3205 (org-export-get-parent-table table-row) info))))
3206 (format "%s
3207 \\endfirsthead
3208 \\multicolumn{%d}{l}{%s} \\\\
3210 %s \\\\\n
3212 \\endhead
3213 %s\\multicolumn{%d}{r}{%s} \\\\
3214 \\endfoot
3215 \\endlastfoot"
3216 (if booktabsp "\\midrule" "\\hline")
3217 columns
3218 (org-latex--translate "Continued from previous page" info)
3219 (cond
3220 ((not (org-export-table-row-starts-header-p table-row info))
3222 (booktabsp "\\toprule\n")
3223 (t "\\hline\n"))
3224 contents
3225 (if booktabsp "\\midrule" "\\hline")
3226 (if booktabsp "\\midrule" "\\hline")
3227 columns
3228 (org-latex--translate "Continued on next page" info))))
3229 ;; When BOOKTABS are activated enforce bottom rule even when
3230 ;; no hline was specifically marked.
3231 ((and booktabsp (not (org-export-get-next-element table-row info)))
3232 "\\bottomrule"))))))
3235 ;;;; Target
3237 (defun org-latex-target (target contents info)
3238 "Transcode a TARGET object from Org to LaTeX.
3239 CONTENTS is nil. INFO is a plist holding contextual
3240 information."
3241 (format "\\label{%s}" (org-latex--label target info)))
3244 ;;;; Timestamp
3246 (defun org-latex-timestamp (timestamp contents info)
3247 "Transcode a TIMESTAMP object from Org to LaTeX.
3248 CONTENTS is nil. INFO is a plist holding contextual
3249 information."
3250 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
3251 (format
3252 (plist-get info
3253 (case (org-element-property :type timestamp)
3254 ((active active-range) :latex-active-timestamp-format)
3255 ((inactive inactive-range) :latex-inactive-timestamp-format)
3256 (otherwise :latex-diary-timestamp-format)))
3257 value)))
3260 ;;;; Underline
3262 (defun org-latex-underline (underline contents info)
3263 "Transcode UNDERLINE from Org to LaTeX.
3264 CONTENTS is the text with underline markup. INFO is a plist
3265 holding contextual information."
3266 (org-latex--text-markup contents 'underline info))
3269 ;;;; Verbatim
3271 (defun org-latex-verbatim (verbatim contents info)
3272 "Transcode a VERBATIM object from Org to LaTeX.
3273 CONTENTS is nil. INFO is a plist used as a communication
3274 channel."
3275 (org-latex--text-markup
3276 (org-element-property :value verbatim) 'verbatim info))
3279 ;;;; Verse Block
3281 (defun org-latex-verse-block (verse-block contents info)
3282 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3283 CONTENTS is verse block contents. INFO is a plist holding
3284 contextual information."
3285 (org-latex--wrap-label
3286 verse-block
3287 ;; In a verse environment, add a line break to each newline
3288 ;; character and change each white space at beginning of a line
3289 ;; into a space of 1 em. Also change each blank line with
3290 ;; a vertical space of 1 em.
3291 (format "\\begin{verse}\n%s\\end{verse}"
3292 (replace-regexp-in-string
3293 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
3294 (replace-regexp-in-string
3295 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3296 (replace-regexp-in-string
3297 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3298 contents nil t) nil t) nil t))
3299 info))
3303 ;;; End-user functions
3305 ;;;###autoload
3306 (defun org-latex-export-as-latex
3307 (&optional async subtreep visible-only body-only ext-plist)
3308 "Export current buffer as a LaTeX buffer.
3310 If narrowing is active in the current buffer, only export its
3311 narrowed part.
3313 If a region is active, export that region.
3315 A non-nil optional argument ASYNC means the process should happen
3316 asynchronously. The resulting buffer should be accessible
3317 through the `org-export-stack' interface.
3319 When optional argument SUBTREEP is non-nil, export the sub-tree
3320 at point, extracting information from the headline properties
3321 first.
3323 When optional argument VISIBLE-ONLY is non-nil, don't export
3324 contents of hidden elements.
3326 When optional argument BODY-ONLY is non-nil, only write code
3327 between \"\\begin{document}\" and \"\\end{document}\".
3329 EXT-PLIST, when provided, is a property list with external
3330 parameters overriding Org default settings, but still inferior to
3331 file-local settings.
3333 Export is done in a buffer named \"*Org LATEX Export*\", which
3334 will be displayed when `org-export-show-temporary-export-buffer'
3335 is non-nil."
3336 (interactive)
3337 (org-export-to-buffer 'latex "*Org LATEX Export*"
3338 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
3340 ;;;###autoload
3341 (defun org-latex-convert-region-to-latex ()
3342 "Assume the current region has org-mode syntax, and convert it to LaTeX.
3343 This can be used in any buffer. For example, you can write an
3344 itemized list in org-mode syntax in an LaTeX buffer and use this
3345 command to convert it."
3346 (interactive)
3347 (org-export-replace-region-by 'latex))
3349 ;;;###autoload
3350 (defun org-latex-export-to-latex
3351 (&optional async subtreep visible-only body-only ext-plist)
3352 "Export current buffer to a LaTeX file.
3354 If narrowing is active in the current buffer, only export its
3355 narrowed part.
3357 If a region is active, export that region.
3359 A non-nil optional argument ASYNC means the process should happen
3360 asynchronously. The resulting file should be accessible through
3361 the `org-export-stack' interface.
3363 When optional argument SUBTREEP is non-nil, export the sub-tree
3364 at point, extracting information from the headline properties
3365 first.
3367 When optional argument VISIBLE-ONLY is non-nil, don't export
3368 contents of hidden elements.
3370 When optional argument BODY-ONLY is non-nil, only write code
3371 between \"\\begin{document}\" and \"\\end{document}\".
3373 EXT-PLIST, when provided, is a property list with external
3374 parameters overriding Org default settings, but still inferior to
3375 file-local settings."
3376 (interactive)
3377 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3378 (org-export-to-file 'latex outfile
3379 async subtreep visible-only body-only ext-plist)))
3381 ;;;###autoload
3382 (defun org-latex-export-to-pdf
3383 (&optional async subtreep visible-only body-only ext-plist)
3384 "Export current buffer to LaTeX then process through to PDF.
3386 If narrowing is active in the current buffer, only export its
3387 narrowed part.
3389 If a region is active, export that region.
3391 A non-nil optional argument ASYNC means the process should happen
3392 asynchronously. The resulting file should be accessible through
3393 the `org-export-stack' interface.
3395 When optional argument SUBTREEP is non-nil, export the sub-tree
3396 at point, extracting information from the headline properties
3397 first.
3399 When optional argument VISIBLE-ONLY is non-nil, don't export
3400 contents of hidden elements.
3402 When optional argument BODY-ONLY is non-nil, only write code
3403 between \"\\begin{document}\" and \"\\end{document}\".
3405 EXT-PLIST, when provided, is a property list with external
3406 parameters overriding Org default settings, but still inferior to
3407 file-local settings.
3409 Return PDF file's name."
3410 (interactive)
3411 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3412 (org-export-to-file 'latex outfile
3413 async subtreep visible-only body-only ext-plist
3414 (lambda (file) (org-latex-compile file)))))
3416 (defun org-latex-compile (texfile &optional snippet)
3417 "Compile a TeX file.
3419 TEXFILE is the name of the file being compiled. Processing is
3420 done through the command specified in `org-latex-pdf-process'.
3422 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3423 file used to preview a LaTeX snippet. In this case, do not
3424 create a log buffer and do not bother removing log files.
3426 Return PDF file name or an error if it couldn't be produced."
3427 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3428 (full-name (file-truename texfile))
3429 (out-dir (file-name-directory texfile))
3430 ;; Properly set working directory for compilation.
3431 (default-directory (if (file-name-absolute-p texfile)
3432 (file-name-directory full-name)
3433 default-directory))
3434 (time (current-time))
3435 warnings)
3436 (unless snippet (message "Processing LaTeX file %s..." texfile))
3437 (save-window-excursion
3438 (cond
3439 ;; A function is provided: Apply it.
3440 ((functionp org-latex-pdf-process)
3441 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3442 ;; A list is provided: Replace %b, %f and %o with appropriate
3443 ;; values in each command before applying it. Output is
3444 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3445 ((consp org-latex-pdf-process)
3446 (let ((outbuf (and (not snippet)
3447 (get-buffer-create "*Org PDF LaTeX Output*"))))
3448 (dolist (command org-latex-pdf-process)
3449 (shell-command
3450 (replace-regexp-in-string
3451 "%b" (shell-quote-argument base-name)
3452 (replace-regexp-in-string
3453 "%f" (shell-quote-argument full-name)
3454 (replace-regexp-in-string
3455 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
3456 outbuf))
3457 ;; Collect standard errors from output buffer.
3458 (setq warnings (and (not snippet)
3459 (org-latex--collect-warnings outbuf)))))
3460 (t (error "No valid command to process to PDF")))
3461 (let ((pdffile (concat out-dir base-name ".pdf")))
3462 ;; Check for process failure. Provide collected errors if
3463 ;; possible.
3464 (if (or (not (file-exists-p pdffile))
3465 (time-less-p (nth 5 (file-attributes pdffile)) time))
3466 (error (format "PDF file %s wasn't produced" pdffile))
3467 ;; Else remove log files, when specified, and signal end of
3468 ;; process to user, along with any error encountered.
3469 (unless snippet
3470 (when org-latex-remove-logfiles
3471 (dolist (file (directory-files
3472 out-dir t
3473 (concat (regexp-quote base-name)
3474 "\\(?:\\.[0-9]+\\)?"
3475 "\\."
3476 (regexp-opt org-latex-logfiles-extensions))))
3477 (delete-file file)))
3478 (message (concat "PDF file produced"
3479 (cond
3480 ((eq warnings 'error) " with errors.")
3481 (warnings (concat " with warnings: " warnings))
3482 (t "."))))))
3483 ;; Return output file name.
3484 pdffile))))
3486 (defun org-latex--collect-warnings (buffer)
3487 "Collect some warnings from \"pdflatex\" command output.
3488 BUFFER is the buffer containing output. Return collected
3489 warnings types as a string, `error' if a LaTeX error was
3490 encountered or nil if there was none."
3491 (with-current-buffer buffer
3492 (save-excursion
3493 (goto-char (point-max))
3494 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3495 (if (re-search-forward "^!" nil t) 'error
3496 (let ((case-fold-search t)
3497 (warnings ""))
3498 (dolist (warning org-latex-known-warnings)
3499 (when (save-excursion (re-search-forward (car warning) nil t))
3500 (setq warnings (concat warnings " " (cdr warning)))))
3501 (org-string-nw-p (org-trim warnings))))))))
3503 ;;;###autoload
3504 (defun org-latex-publish-to-latex (plist filename pub-dir)
3505 "Publish an Org file to LaTeX.
3507 FILENAME is the filename of the Org file to be published. PLIST
3508 is the property list for the given project. PUB-DIR is the
3509 publishing directory.
3511 Return output file name."
3512 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3514 ;;;###autoload
3515 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3516 "Publish an Org file to PDF (via LaTeX).
3518 FILENAME is the filename of the Org file to be published. PLIST
3519 is the property list for the given project. PUB-DIR is the
3520 publishing directory.
3522 Return output file name."
3523 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3524 ;; in working directory and then moved to publishing directory.
3525 (org-publish-attachment
3526 plist
3527 (org-latex-compile
3528 (org-publish-org-to
3529 'latex filename ".tex" plist (file-name-directory filename)))
3530 pub-dir))
3533 (provide 'ox-latex)
3535 ;; Local variables:
3536 ;; generated-autoload-file: "org-loaddefs.el"
3537 ;; End:
3539 ;;; ox-latex.el ends here