Add notes on Polyglossia support
[org-mode.git] / lisp / ox-latex.el
blob5dbfd7425916c768616099a7ddf982dbfc8cacf2
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 result)
1307 ;; If no language is set or Polyglossia is not loaded, return
1308 ;; HEADER as-is.
1309 (if (or (not (org-string-nw-p language))
1310 (not (string-match
1311 "\\\\usepackage\\(?:\\[\\([^]]+?\\)\\]\\){polyglossia}\n" header)))
1312 header
1313 (let* ((options (org-string-nw-p (match-string 1 header)))
1314 (languages (when options
1315 (save-match-data
1316 ;; Reverse as the last loaded language is
1317 ;; the main language.
1318 (reverse
1319 (delete-dups
1320 (org-split-string
1321 (replace-regexp-in-string
1322 "AUTO" language options t)
1323 ",[ \t]*"))))))
1324 (main-language-set-p
1325 (string-match-p "\\\\setmainlanguage{.*?}" header)))
1326 (replace-match
1327 (concat "\\usepackage{polyglossia}\n"
1328 (loop for l in languages concat
1329 (let ((lang (or (assoc l org-latex-polyglossia-language-alist)
1330 l)))
1331 (format (if main-language-set-p
1332 "\\setotherlanguage%s{%s}\n"
1333 (progn (setq main-language-set-p t)
1334 "\\setmainlanguage%s{%s}\n"))
1335 (if (and (listp lang) (eq 3 (length lang)))
1336 (format "[variant=%s]" (nth 2 lang))
1338 (nth 1 lang)))))
1339 t t header 0)))))
1341 (defun org-latex--find-verb-separator (s)
1342 "Return a character not used in string S.
1343 This is used to choose a separator for constructs like \\verb."
1344 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1345 (loop for c across ll
1346 when (not (string-match (regexp-quote (char-to-string c)) s))
1347 return (char-to-string c))))
1349 (defun org-latex--make-option-string (options)
1350 "Return a comma separated string of keywords and values.
1351 OPTIONS is an alist where the key is the options keyword as
1352 a string, and the value a list containing the keyword value, or
1353 nil."
1354 (mapconcat (lambda (pair)
1355 (concat (first pair)
1356 (when (> (length (second pair)) 0)
1357 (concat "=" (second pair)))))
1358 options
1359 ","))
1361 (defun org-latex--wrap-label (element output info)
1362 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1363 INFO is the current export state, as a plist. This function
1364 should not be used for floats. See
1365 `org-latex--caption/label-string'."
1366 (if (not (and (org-string-nw-p output) (org-element-property :name element)))
1367 output
1368 (concat (format "\\phantomsection\n\\label{%s}\n"
1369 (org-latex--label element info))
1370 output)))
1372 (defun org-latex--protect-text (text)
1373 "Protect special characters in string TEXT and return it."
1374 (replace-regexp-in-string
1375 "--\\|[\\{}$%&_#~^]"
1376 (lambda (m)
1377 (cond ((equal m "--") "-{}-")
1378 ((equal m "\\") "\\textbackslash{}")
1379 ((equal m "~") "\\textasciitilde{}")
1380 ((equal m "^") "\\textasciicircum{}")
1381 (t (concat "\\" m))))
1382 text nil t))
1384 (defun org-latex--text-markup (text markup info)
1385 "Format TEXT depending on MARKUP text markup.
1386 INFO is a plist used as a communication channel. See
1387 `org-latex-text-markup-alist' for details."
1388 (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
1389 (case fmt
1390 ;; No format string: Return raw text.
1391 ((nil) text)
1392 ;; Handle the `verb' special case: Find an appropriate separator
1393 ;; and use "\\verb" command.
1394 (verb
1395 (let ((separator (org-latex--find-verb-separator text)))
1396 (concat "\\verb" separator
1397 (replace-regexp-in-string "\n" " " text)
1398 separator)))
1399 ;; Handle the `protectedtexttt' special case: Protect some
1400 ;; special chars and use "\texttt{%s}" format string.
1401 (protectedtexttt
1402 (format "\\texttt{%s}" (org-latex--protect-text text)))
1403 ;; Else use format string.
1404 (t (format fmt text)))))
1406 (defun org-latex--delayed-footnotes-definitions (element info)
1407 "Return footnotes definitions in ELEMENT as a string.
1409 INFO is a plist used as a communication channel.
1411 Footnotes definitions are returned within \"\\footnotetxt{}\"
1412 commands.
1414 This function is used within constructs that don't support
1415 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1416 \"\\footnotemark\" is used within the construct and the function
1417 just outside of it."
1418 (mapconcat
1419 (lambda (ref)
1420 (format
1421 "\\footnotetext[%s]{%s}"
1422 (org-export-get-footnote-number ref info)
1423 (org-trim
1424 (org-export-data
1425 (org-export-get-footnote-definition ref info) info))))
1426 ;; Find every footnote reference in ELEMENT.
1427 (let* (all-refs
1428 search-refs ; For byte-compiler.
1429 (search-refs
1430 (function
1431 (lambda (data)
1432 ;; Return a list of all footnote references never seen
1433 ;; before in DATA.
1434 (org-element-map data 'footnote-reference
1435 (lambda (ref)
1436 (when (org-export-footnote-first-reference-p ref info)
1437 (push ref all-refs)
1438 (when (eq (org-element-property :type ref) 'standard)
1439 (funcall search-refs
1440 (org-export-get-footnote-definition ref info)))))
1441 info)
1442 (reverse all-refs)))))
1443 (funcall search-refs element))
1444 ""))
1446 (defun org-latex--translate (s info)
1447 "Translate string S according to specified language.
1448 INFO is a plist used as a communication channel."
1449 (org-export-translate s :latex info))
1451 (defun org-latex--format-spec (info)
1452 "Create a format-spec for document meta-data.
1453 INFO is a plist used as a communication channel."
1454 (let ((language (let ((lang (plist-get info :language)))
1455 (or (cdr (assoc lang org-latex-babel-language-alist))
1456 lang))))
1457 `((?a . ,(org-export-data (plist-get info :author) info))
1458 (?t . ,(org-export-data (plist-get info :title) info))
1459 (?k . ,(org-export-data (org-latex--wrap-latex-math-block
1460 (plist-get info :keywords) info)
1461 info))
1462 (?d . ,(org-export-data (org-latex--wrap-latex-math-block
1463 (plist-get info :description) info)
1464 info))
1465 (?c . ,(plist-get info :creator))
1466 (?l . ,language)
1467 (?L . ,(capitalize language))
1468 (?D . ,(org-export-get-date info)))))
1471 ;;; Template
1473 (defun org-latex-template (contents info)
1474 "Return complete document string after LaTeX conversion.
1475 CONTENTS is the transcoded contents string. INFO is a plist
1476 holding export options."
1477 (let ((title (org-export-data (plist-get info :title) info))
1478 (spec (org-latex--format-spec info)))
1479 (concat
1480 ;; Time-stamp.
1481 (and (plist-get info :time-stamp-file)
1482 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1483 ;; Document class and packages.
1484 (let* ((class (plist-get info :latex-class))
1485 (class-options (plist-get info :latex-class-options))
1486 (header (nth 1 (assoc class (plist-get info :latex-classes))))
1487 (document-class-string
1488 (and (stringp header)
1489 (if (not class-options) header
1490 (replace-regexp-in-string
1491 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1492 class-options header t nil 1)))))
1493 (if (not document-class-string)
1494 (user-error "Unknown LaTeX class `%s'" class)
1495 (org-latex-guess-polyglossia-language
1496 (org-latex-guess-babel-language
1497 (org-latex-guess-inputenc
1498 (org-element-normalize-string
1499 (org-splice-latex-header
1500 document-class-string
1501 org-latex-default-packages-alist
1502 org-latex-packages-alist nil
1503 (concat (org-element-normalize-string
1504 (plist-get info :latex-header))
1505 (plist-get info :latex-header-extra)))))
1506 info)
1507 info)))
1508 ;; Possibly limit depth for headline numbering.
1509 (let ((sec-num (plist-get info :section-numbers)))
1510 (when (integerp sec-num)
1511 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1512 ;; Author.
1513 (let ((author (and (plist-get info :with-author)
1514 (let ((auth (plist-get info :author)))
1515 (and auth (org-export-data auth info)))))
1516 (email (and (plist-get info :with-email)
1517 (org-export-data (plist-get info :email) info))))
1518 (cond ((and author email (not (string= "" email)))
1519 (format "\\author{%s\\thanks{%s}}\n" author email))
1520 ((or author email) (format "\\author{%s}\n" (or author email)))))
1521 ;; Date.
1522 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1523 (format "\\date{%s}\n" (org-export-data date info)))
1524 ;; Title and subtitle.
1525 (let* ((subtitle (plist-get info :subtitle))
1526 (formatted-subtitle
1527 (when subtitle
1528 (format (plist-get info :latex-subtitle-format)
1529 (org-export-data subtitle info))))
1530 (separate (plist-get info :latex-subtitle-separate)))
1531 (concat
1532 (format "\\title{%s%s}\n" title
1533 (if separate "" (or formatted-subtitle "")))
1534 (when (and separate subtitle)
1535 (concat formatted-subtitle "\n"))))
1536 ;; Hyperref options.
1537 (let ((template (plist-get info :latex-hyperref-template)))
1538 (and (stringp template)
1539 (format-spec template spec)))
1540 ;; Document start.
1541 "\\begin{document}\n\n"
1542 ;; Title command.
1543 (let* ((title-command (plist-get info :latex-title-command))
1544 (command (and (stringp title-command)
1545 (format-spec title-command spec))))
1546 (org-element-normalize-string
1547 (cond ((not (plist-get info :with-title)) nil)
1548 ((string= "" title) nil)
1549 ((not (stringp command)) nil)
1550 ((string-match "\\(?:[^%]\\|^\\)%s" command)
1551 (format command title))
1552 (t command))))
1553 ;; Table of contents.
1554 (let ((depth (plist-get info :with-toc)))
1555 (when depth
1556 (concat (when (wholenump depth)
1557 (format "\\setcounter{tocdepth}{%d}\n" depth))
1558 (plist-get info :latex-toc-command))))
1559 ;; Document's body.
1560 contents
1561 ;; Creator.
1562 (and (plist-get info :with-creator)
1563 (concat (plist-get info :creator) "\n"))
1564 ;; Document end.
1565 "\\end{document}")))
1569 ;;; Transcode Functions
1571 ;;;; Bold
1573 (defun org-latex-bold (bold contents info)
1574 "Transcode BOLD from Org to LaTeX.
1575 CONTENTS is the text with bold markup. INFO is a plist holding
1576 contextual information."
1577 (org-latex--text-markup contents 'bold info))
1580 ;;;; Center Block
1582 (defun org-latex-center-block (center-block contents info)
1583 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1584 CONTENTS holds the contents of the center block. INFO is a plist
1585 holding contextual information."
1586 (org-latex--wrap-label
1587 center-block (format "\\begin{center}\n%s\\end{center}" contents) info))
1590 ;;;; Clock
1592 (defun org-latex-clock (clock contents info)
1593 "Transcode a CLOCK element from Org to LaTeX.
1594 CONTENTS is nil. INFO is a plist holding contextual
1595 information."
1596 (concat
1597 "\\noindent"
1598 (format "\\textbf{%s} " org-clock-string)
1599 (format (plist-get info :latex-inactive-timestamp-format)
1600 (concat (org-timestamp-translate (org-element-property :value clock))
1601 (let ((time (org-element-property :duration clock)))
1602 (and time (format " (%s)" time)))))
1603 "\\\\"))
1606 ;;;; Code
1608 (defun org-latex-code (code contents info)
1609 "Transcode a CODE object from Org to LaTeX.
1610 CONTENTS is nil. INFO is a plist used as a communication
1611 channel."
1612 (org-latex--text-markup (org-element-property :value code) 'code info))
1615 ;;;; Drawer
1617 (defun org-latex-drawer (drawer contents info)
1618 "Transcode a DRAWER element from Org to LaTeX.
1619 CONTENTS holds the contents of the block. INFO is a plist
1620 holding contextual information."
1621 (let* ((name (org-element-property :drawer-name drawer))
1622 (output (funcall (plist-get info :latex-format-drawer-function)
1623 name contents)))
1624 (org-latex--wrap-label drawer output info)))
1627 ;;;; Dynamic Block
1629 (defun org-latex-dynamic-block (dynamic-block contents info)
1630 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1631 CONTENTS holds the contents of the block. INFO is a plist
1632 holding contextual information. See `org-export-data'."
1633 (org-latex--wrap-label dynamic-block contents info))
1636 ;;;; Entity
1638 (defun org-latex-entity (entity contents info)
1639 "Transcode an ENTITY object from Org to LaTeX.
1640 CONTENTS are the definition itself. INFO is a plist holding
1641 contextual information."
1642 (org-element-property :latex entity))
1645 ;;;; Example Block
1647 (defun org-latex-example-block (example-block contents info)
1648 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1649 CONTENTS is nil. INFO is a plist holding contextual
1650 information."
1651 (when (org-string-nw-p (org-element-property :value example-block))
1652 (let ((environment (or (org-export-read-attribute
1653 :attr_latex example-block :environment)
1654 "verbatim")))
1655 (org-latex--wrap-label
1656 example-block
1657 (format "\\begin{%s}\n%s\\end{%s}"
1658 environment
1659 (org-export-format-code-default example-block info)
1660 environment)
1661 info))))
1664 ;;;; Export Block
1666 (defun org-latex-export-block (export-block contents info)
1667 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1668 CONTENTS is nil. INFO is a plist holding contextual information."
1669 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1670 (org-remove-indentation (org-element-property :value export-block))))
1673 ;;;; Export Snippet
1675 (defun org-latex-export-snippet (export-snippet contents info)
1676 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1677 CONTENTS is nil. INFO is a plist holding contextual information."
1678 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1679 (org-element-property :value export-snippet)))
1682 ;;;; Fixed Width
1684 (defun org-latex-fixed-width (fixed-width contents info)
1685 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1686 CONTENTS is nil. INFO is a plist holding contextual information."
1687 (org-latex--wrap-label
1688 fixed-width
1689 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1690 (org-remove-indentation
1691 (org-element-property :value fixed-width)))
1692 info))
1695 ;;;; Footnote Reference
1697 (defun org-latex-footnote-reference (footnote-reference contents info)
1698 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1699 CONTENTS is nil. INFO is a plist holding contextual information."
1700 (concat
1701 ;; Insert separator between two footnotes in a row.
1702 (let ((prev (org-export-get-previous-element footnote-reference info)))
1703 (when (eq (org-element-type prev) 'footnote-reference)
1704 (plist-get info :latex-footnote-separator)))
1705 (cond
1706 ;; Use \footnotemark if the footnote has already been defined.
1707 ((not (org-export-footnote-first-reference-p footnote-reference info))
1708 (format "\\footnotemark[%s]{}"
1709 (org-export-get-footnote-number footnote-reference info)))
1710 ;; Use \footnotemark if reference is within another footnote
1711 ;; reference, footnote definition or table cell.
1712 ((org-element-lineage footnote-reference
1713 '(footnote-reference footnote-definition table-cell))
1714 "\\footnotemark")
1715 ;; Otherwise, define it with \footnote command.
1717 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1718 (concat
1719 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1720 ;; Retrieve all footnote references within the footnote and
1721 ;; add their definition after it, since LaTeX doesn't support
1722 ;; them inside.
1723 (org-latex--delayed-footnotes-definitions def info)))))))
1726 ;;;; Headline
1728 (defun org-latex-headline (headline contents info)
1729 "Transcode a HEADLINE element from Org to LaTeX.
1730 CONTENTS holds the contents of the headline. INFO is a plist
1731 holding contextual information."
1732 (unless (org-element-property :footnote-section-p headline)
1733 (let* ((class (plist-get info :latex-class))
1734 (level (org-export-get-relative-level headline info))
1735 (numberedp (org-export-numbered-headline-p headline info))
1736 (class-sectioning (assoc class (plist-get info :latex-classes)))
1737 ;; Section formatting will set two placeholders: one for
1738 ;; the title and the other for the contents.
1739 (section-fmt
1740 (let ((sec (if (functionp (nth 2 class-sectioning))
1741 (funcall (nth 2 class-sectioning) level numberedp)
1742 (nth (1+ level) class-sectioning))))
1743 (cond
1744 ;; No section available for that LEVEL.
1745 ((not sec) nil)
1746 ;; Section format directly returned by a function. Add
1747 ;; placeholder for contents.
1748 ((stringp sec) (concat sec "\n%s"))
1749 ;; (numbered-section . unnumbered-section)
1750 ((not (consp (cdr sec)))
1751 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1752 ;; (numbered-open numbered-close)
1753 ((= (length sec) 2)
1754 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1755 ;; (num-in num-out no-num-in no-num-out)
1756 ((= (length sec) 4)
1757 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1758 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1759 ;; Create a temporary export back-end that hard-codes
1760 ;; "\underline" within "\section" and alike.
1761 (section-back-end
1762 (org-export-create-backend
1763 :parent 'latex
1764 :transcoders
1765 '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
1766 (text
1767 (org-export-data-with-backend
1768 (org-element-property :title headline) section-back-end info))
1769 (todo
1770 (and (plist-get info :with-todo-keywords)
1771 (let ((todo (org-element-property :todo-keyword headline)))
1772 (and todo (org-export-data todo info)))))
1773 (todo-type (and todo (org-element-property :todo-type headline)))
1774 (tags (and (plist-get info :with-tags)
1775 (org-export-get-tags headline info)))
1776 (priority (and (plist-get info :with-priority)
1777 (org-element-property :priority headline)))
1778 ;; Create the headline text along with a no-tag version.
1779 ;; The latter is required to remove tags from toc.
1780 (full-text (funcall (plist-get info :latex-format-headline-function)
1781 todo todo-type priority text tags info))
1782 ;; Associate \label to the headline for internal links.
1783 (headline-label (org-latex--label headline info t t))
1784 (pre-blanks
1785 (make-string (org-element-property :pre-blank headline) ?\n)))
1786 (if (or (not section-fmt) (org-export-low-level-p headline info))
1787 ;; This is a deep sub-tree: export it as a list item. Also
1788 ;; export as items headlines for which no section format has
1789 ;; been found.
1790 (let ((low-level-body
1791 (concat
1792 ;; If headline is the first sibling, start a list.
1793 (when (org-export-first-sibling-p headline info)
1794 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1795 ;; Itemize headline
1796 "\\item"
1797 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
1798 "\\relax")
1799 " " full-text "\n"
1800 headline-label
1801 pre-blanks
1802 contents)))
1803 ;; If headline is not the last sibling simply return
1804 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1805 ;; any blank line.
1806 (if (not (org-export-last-sibling-p headline info)) low-level-body
1807 (replace-regexp-in-string
1808 "[ \t\n]*\\'"
1809 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1810 low-level-body)))
1811 ;; This is a standard headline. Export it as a section. Add
1812 ;; an alternative heading when possible, and when this is not
1813 ;; identical to the usual heading.
1814 (let ((opt-title
1815 (funcall (plist-get info :latex-format-headline-function)
1816 todo todo-type priority
1817 (org-export-data-with-backend
1818 (org-export-get-alt-title headline info)
1819 section-back-end info)
1820 (and (eq (plist-get info :with-tags) t) tags)
1821 info))
1822 ;; Maybe end local TOC (see `org-latex-keyword').
1823 (contents
1824 (concat
1825 contents
1826 (let ((case-fold-search t)
1827 (section
1828 (let ((first (car (org-element-contents headline))))
1829 (and (eq (org-element-type first) 'section) first))))
1830 (org-element-map section 'keyword
1831 (lambda (k)
1832 (and (equal (org-element-property :key k) "TOC")
1833 (let ((v (org-element-property :value k)))
1834 (and (org-string-match-p "\\<headlines\\>" v)
1835 (org-string-match-p "\\<local\\>" v)
1836 (format "\\stopcontents[level-%d]" level)))))
1837 info t)))))
1838 (if (and opt-title
1839 (not (equal opt-title full-text))
1840 (string-match "\\`\\\\\\(.+?\\){" section-fmt))
1841 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1842 ;; Replace square brackets with parenthesis
1843 ;; since square brackets are not supported in
1844 ;; optional arguments.
1845 (replace-regexp-in-string
1846 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1847 full-text
1848 (concat headline-label pre-blanks contents))
1849 ;; Impossible to add an alternative heading. Fallback to
1850 ;; regular sectioning format string.
1851 (format section-fmt full-text
1852 (concat headline-label pre-blanks contents))))))))
1854 (defun org-latex-format-headline-default-function
1855 (todo todo-type priority text tags info)
1856 "Default format function for a headline.
1857 See `org-latex-format-headline-function' for details."
1858 (concat
1859 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1860 (and priority (format "\\framebox{\\#%c} " priority))
1861 text
1862 (and tags
1863 (format "\\hfill{}\\textsc{%s}"
1864 (mapconcat (lambda (tag) (org-latex-plain-text tag info))
1865 tags ":")))))
1868 ;;;; Horizontal Rule
1870 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1871 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1872 CONTENTS is nil. INFO is a plist holding contextual information."
1873 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1874 (prev (org-export-get-previous-element horizontal-rule info)))
1875 (concat
1876 ;; Make sure the rule doesn't start at the end of the current
1877 ;; line by separating it with a blank line from previous element.
1878 (when (and prev
1879 (let ((prev-blank (org-element-property :post-blank prev)))
1880 (or (not prev-blank) (zerop prev-blank))))
1881 "\n")
1882 (org-latex--wrap-label
1883 horizontal-rule
1884 (format "\\rule{%s}{%s}"
1885 (or (plist-get attr :width) "\\linewidth")
1886 (or (plist-get attr :thickness) "0.5pt"))
1887 info))))
1890 ;;;; Inline Src Block
1892 (defun org-latex-inline-src-block (inline-src-block contents info)
1893 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1894 CONTENTS holds the contents of the item. INFO is a plist holding
1895 contextual information."
1896 (let* ((code (org-element-property :value inline-src-block))
1897 (separator (org-latex--find-verb-separator code)))
1898 (case (plist-get info :latex-listings)
1899 ;; Do not use a special package: transcode it verbatim.
1900 ((nil) (format "\\texttt{%s}" (org-latex--protect-text code)))
1901 ;; Use minted package.
1902 (minted
1903 (let* ((org-lang (org-element-property :language inline-src-block))
1904 (mint-lang (or (cadr (assq (intern org-lang)
1905 (plist-get info :latex-minted-langs)))
1906 (downcase org-lang)))
1907 (options (org-latex--make-option-string
1908 (plist-get info :latex-minted-options))))
1909 (concat (format "\\mint%s{%s}"
1910 (if (string= options "") "" (format "[%s]" options))
1911 mint-lang)
1912 separator code separator)))
1913 ;; Use listings package.
1914 (otherwise
1915 ;; Maybe translate language's name.
1916 (let* ((org-lang (org-element-property :language inline-src-block))
1917 (lst-lang (or (cadr (assq (intern org-lang)
1918 (plist-get info :latex-listings-langs)))
1919 org-lang))
1920 (options (org-latex--make-option-string
1921 (append (plist-get info :latex-listings-options)
1922 `(("language" ,lst-lang))))))
1923 (concat (format "\\lstinline[%s]" options)
1924 separator code separator))))))
1927 ;;;; Inlinetask
1929 (defun org-latex-inlinetask (inlinetask contents info)
1930 "Transcode an INLINETASK element from Org to LaTeX.
1931 CONTENTS holds the contents of the block. INFO is a plist
1932 holding contextual information."
1933 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1934 (todo (and (plist-get info :with-todo-keywords)
1935 (let ((todo (org-element-property :todo-keyword inlinetask)))
1936 (and todo (org-export-data todo info)))))
1937 (todo-type (org-element-property :todo-type inlinetask))
1938 (tags (and (plist-get info :with-tags)
1939 (org-export-get-tags inlinetask info)))
1940 (priority (and (plist-get info :with-priority)
1941 (org-element-property :priority inlinetask)))
1942 (contents (concat (org-latex--label inlinetask info) contents)))
1943 (funcall (plist-get info :latex-format-inlinetask-function)
1944 todo todo-type priority title tags contents info)))
1946 (defun org-latex-format-inlinetask-default-function
1947 (todo todo-type priority title tags contents info)
1948 "Default format function for a inlinetasks.
1949 See `org-latex-format-inlinetask-function' for details."
1950 (let ((full-title
1951 (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1952 (when priority (format "\\framebox{\\#%c} " priority))
1953 title
1954 (when tags
1955 (format "\\hfill{}\\textsc{:%s:}"
1956 (mapconcat
1957 (lambda (tag) (org-latex-plain-text tag info))
1958 tags ":"))))))
1959 (concat "\\begin{center}\n"
1960 "\\fbox{\n"
1961 "\\begin{minipage}[c]{.6\\textwidth}\n"
1962 full-title "\n\n"
1963 (and (org-string-nw-p contents)
1964 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
1965 "\\end{minipage}\n"
1966 "}\n"
1967 "\\end{center}")))
1970 ;;;; Italic
1972 (defun org-latex-italic (italic contents info)
1973 "Transcode ITALIC from Org to LaTeX.
1974 CONTENTS is the text with italic markup. INFO is a plist holding
1975 contextual information."
1976 (org-latex--text-markup contents 'italic info))
1979 ;;;; Item
1981 (defun org-latex-item (item contents info)
1982 "Transcode an ITEM element from Org to LaTeX.
1983 CONTENTS holds the contents of the item. INFO is a plist holding
1984 contextual information."
1985 (let* ((counter
1986 (let ((count (org-element-property :counter item))
1987 (level
1988 ;; Determine level of current item to determine the
1989 ;; correct LaTeX counter to use (enumi, enumii...).
1990 (let ((parent item) (level 0))
1991 (while (memq (org-element-type
1992 (setq parent (org-export-get-parent parent)))
1993 '(plain-list item))
1994 (when (and (eq (org-element-type parent) 'plain-list)
1995 (eq (org-element-property :type parent)
1996 'ordered))
1997 (incf level)))
1998 level)))
1999 (and count
2000 (< level 5)
2001 (format "\\setcounter{enum%s}{%s}\n"
2002 (nth (1- level) '("i" "ii" "iii" "iv"))
2003 (1- count)))))
2004 (checkbox (case (org-element-property :checkbox item)
2005 (on "$\\boxtimes$ ")
2006 (off "$\\square$ ")
2007 (trans "$\\boxminus$ ")))
2008 (tag (let ((tag (org-element-property :tag item)))
2009 ;; Check-boxes must belong to the tag.
2010 (and tag (format "[{%s}] "
2011 (concat checkbox
2012 (org-export-data tag info)))))))
2013 (concat counter
2014 "\\item"
2015 (cond
2016 (tag)
2017 (checkbox (concat " " checkbox))
2018 ;; Without a tag or a check-box, if CONTENTS starts with
2019 ;; an opening square bracket, add "\relax" to "\item",
2020 ;; unless the brackets comes from an initial export
2021 ;; snippet (i.e. it is inserted willingly by the user).
2022 ((and contents
2023 (org-string-match-p "\\`[ \t]*\\[" contents)
2024 (not (let ((e (car (org-element-contents item))))
2025 (and (eq (org-element-type e) 'paragraph)
2026 (let ((o (car (org-element-contents e))))
2027 (and (eq (org-element-type o) 'export-snippet)
2028 (eq (org-export-snippet-backend o)
2029 'latex)))))))
2030 "\\relax ")
2031 (t " "))
2032 (and contents (org-trim contents))
2033 ;; If there are footnotes references in tag, be sure to
2034 ;; add their definition at the end of the item. This
2035 ;; workaround is necessary since "\footnote{}" command is
2036 ;; not supported in tags.
2037 (and tag
2038 (org-latex--delayed-footnotes-definitions
2039 (org-element-property :tag item) info)))))
2042 ;;;; Keyword
2044 (defun org-latex-keyword (keyword contents info)
2045 "Transcode a KEYWORD element from Org to LaTeX.
2046 CONTENTS is nil. INFO is a plist holding contextual information."
2047 (let ((key (org-element-property :key keyword))
2048 (value (org-element-property :value keyword)))
2049 (cond
2050 ((string= key "LATEX") value)
2051 ((string= key "INDEX") (format "\\index{%s}" value))
2052 ((string= key "TOC")
2053 (let ((case-fold-search t))
2054 (cond
2055 ((org-string-match-p "\\<headlines\\>" value)
2056 (let* ((localp (org-string-match-p "\\<local\\>" value))
2057 (parent (org-element-lineage keyword '(headline)))
2058 (level (if (not (and localp parent)) 0
2059 (org-export-get-relative-level parent info)))
2060 (depth
2061 (and (string-match "\\<[0-9]+\\>" value)
2062 (format
2063 "\\setcounter{tocdepth}{%d}"
2064 (+ (string-to-number (match-string 0 value)) level)))))
2065 (if (and localp parent)
2066 ;; Start local TOC, assuming package "titletoc" is
2067 ;; required.
2068 (format "\\startcontents[level-%d]
2069 \\printcontents[level-%d]{}{0}{%s}"
2070 level level (or depth ""))
2071 (concat depth (and depth "\n") "\\tableofcontents"))))
2072 ((org-string-match-p "\\<tables\\>" value) "\\listoftables")
2073 ((org-string-match-p "\\<listings\\>" value)
2074 (case (plist-get info :latex-listings)
2075 ((nil) "\\listoffigures")
2076 (minted "\\listoflistings")
2077 (otherwise "\\lstlistoflistings")))))))))
2080 ;;;; Latex Environment
2082 (defun org-latex-latex-environment (latex-environment contents info)
2083 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
2084 CONTENTS is nil. INFO is a plist holding contextual information."
2085 (when (plist-get info :with-latex)
2086 (let ((value (org-remove-indentation
2087 (org-element-property :value latex-environment))))
2088 (if (not (org-element-property :name latex-environment)) value
2089 ;; Environment is labeled: label must be within the environment
2090 ;; (otherwise, a reference pointing to that element will count
2091 ;; the section instead).
2092 (with-temp-buffer
2093 (insert value)
2094 (goto-char (point-min))
2095 (forward-line)
2096 (insert (org-latex--label latex-environment info nil t))
2097 (buffer-string))))))
2100 ;;;; Latex Fragment
2102 (defun org-latex-latex-fragment (latex-fragment contents info)
2103 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
2104 CONTENTS is nil. INFO is a plist holding contextual information."
2105 (let ((value (org-element-property :value latex-fragment)))
2106 ;; Trim math markers since the fragment is enclosed within
2107 ;; a latex-math-block object anyway.
2108 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
2109 (match-string 2 value))
2110 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
2111 (match-string 1 value))
2112 (t value))))
2115 ;;;; Line Break
2117 (defun org-latex-line-break (line-break contents info)
2118 "Transcode a LINE-BREAK object from Org to LaTeX.
2119 CONTENTS is nil. INFO is a plist holding contextual information."
2120 "\\\\\n")
2123 ;;;; Link
2125 (defun org-latex--inline-image (link info)
2126 "Return LaTeX code for an inline image.
2127 LINK is the link pointing to the inline image. INFO is a plist
2128 used as a communication channel."
2129 (let* ((parent (org-export-get-parent-element link))
2130 (path (let ((raw-path (org-element-property :path link)))
2131 (if (not (file-name-absolute-p raw-path)) raw-path
2132 (expand-file-name raw-path))))
2133 (filetype (file-name-extension path))
2134 (caption (org-latex--caption/label-string parent info))
2135 (caption-above-p (org-latex--caption-above-p link info))
2136 ;; Retrieve latex attributes from the element around.
2137 (attr (org-export-read-attribute :attr_latex parent))
2138 (float (let ((float (plist-get attr :float)))
2139 (cond ((string= float "wrap") 'wrap)
2140 ((string= float "sideways") 'sideways)
2141 ((string= float "multicolumn") 'multicolumn)
2142 ((or float
2143 (org-element-property :caption parent)
2144 (org-string-nw-p (plist-get attr :caption)))
2145 (if (and (plist-member attr :float) (not float))
2146 'nonfloat
2147 'figure))
2148 ((and (not float) (plist-member attr :float)) nil))))
2149 (placement
2150 (let ((place (plist-get attr :placement)))
2151 (cond
2152 (place (format "%s" place))
2153 ((eq float 'wrap) "{l}{0.5\\textwidth}")
2154 ((eq float 'figure)
2155 (format "[%s]" (plist-get info :latex-default-figure-position)))
2156 (t ""))))
2157 (comment-include (if (plist-get attr :comment-include) "%" ""))
2158 ;; It is possible to specify width and height in the
2159 ;; ATTR_LATEX line, and also via default variables.
2160 (width (cond ((plist-get attr :width))
2161 ((plist-get attr :height) "")
2162 ((eq float 'wrap) "0.48\\textwidth")
2163 (t (plist-get info :latex-image-default-width))))
2164 (height (cond ((plist-get attr :height))
2165 ((or (plist-get attr :width)
2166 (memq float '(figure wrap))) "")
2167 (t (plist-get info :latex-image-default-height))))
2168 (options (let ((opt (or (plist-get attr :options)
2169 (plist-get info :latex-image-default-option))))
2170 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
2171 (match-string 1 opt))))
2172 image-code)
2173 (if (member filetype '("tikz" "pgf"))
2174 ;; For tikz images:
2175 ;; - use \input to read in image file.
2176 ;; - if options are present, wrap in a tikzpicture environment.
2177 ;; - if width or height are present, use \resizebox to change
2178 ;; the image size.
2179 (progn
2180 (setq image-code (format "\\input{%s}" path))
2181 (when (org-string-nw-p options)
2182 (setq image-code
2183 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
2184 options
2185 image-code)))
2186 (when (or (org-string-nw-p width) (org-string-nw-p height))
2187 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
2188 (if (org-string-nw-p width) width "!")
2189 (if (org-string-nw-p height) height "!")
2190 image-code))))
2191 ;; For other images:
2192 ;; - add width and height to options.
2193 ;; - include the image with \includegraphics.
2194 (when (org-string-nw-p width)
2195 (setq options (concat options ",width=" width)))
2196 (when (org-string-nw-p height)
2197 (setq options (concat options ",height=" height)))
2198 (let ((search-option (org-element-property :search-option link)))
2199 (when (and search-option
2200 (equal filetype "pdf")
2201 (org-string-match-p "\\`[0-9]+\\'" search-option)
2202 (not (org-string-match-p "page=" options)))
2203 (setq options (concat options ",page=" search-option))))
2204 (setq image-code
2205 (format "\\includegraphics%s{%s}"
2206 (cond ((not (org-string-nw-p options)) "")
2207 ((= (aref options 0) ?,)
2208 (format "[%s]"(substring options 1)))
2209 (t (format "[%s]" options)))
2210 path))
2211 (when (equal filetype "svg")
2212 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
2213 "\\includesvg"
2214 image-code
2215 nil t))
2216 (setq image-code (replace-regexp-in-string "\\.svg}"
2218 image-code
2219 nil t))))
2220 ;; Return proper string, depending on FLOAT.
2221 (case float
2222 (wrap (format "\\begin{wrapfigure}%s
2223 %s\\centering
2224 %s%s
2225 %s\\end{wrapfigure}"
2226 placement
2227 (if caption-above-p caption "")
2228 comment-include image-code
2229 (if caption-above-p "" caption)))
2230 (sideways (format "\\begin{sidewaysfigure}
2231 %s\\centering
2232 %s%s
2233 %s\\end{sidewaysfigure}"
2234 (if caption-above-p caption "")
2235 comment-include image-code
2236 (if caption-above-p "" caption)))
2237 (multicolumn (format "\\begin{figure*}%s
2238 %s\\centering
2239 %s%s
2240 %s\\end{figure*}"
2241 placement
2242 (if caption-above-p caption "")
2243 comment-include image-code
2244 (if caption-above-p "" caption)))
2245 (figure (format "\\begin{figure}%s
2246 %s\\centering
2247 %s%s
2248 %s\\end{figure}"
2249 placement
2250 (if caption-above-p caption "")
2251 comment-include image-code
2252 (if caption-above-p "" caption)))
2253 (nonfloat
2254 (format "\\begin{center}
2255 %s%s
2256 %s\\end{center}"
2257 (if caption-above-p caption "")
2258 image-code
2259 (if caption-above-p "" caption)))
2260 (otherwise image-code))))
2262 (defun org-latex-link (link desc info)
2263 "Transcode a LINK object from Org to LaTeX.
2265 DESC is the description part of the link, or the empty string.
2266 INFO is a plist holding contextual information. See
2267 `org-export-data'."
2268 (let* ((type (org-element-property :type link))
2269 (raw-path (replace-regexp-in-string
2270 "%" "\\%" (org-element-property :path link) nil t))
2271 ;; Ensure DESC really exists, or set it to nil.
2272 (desc (and (not (string= desc "")) desc))
2273 (imagep (org-export-inline-image-p
2274 link (plist-get info :latex-inline-image-rules)))
2275 (path (cond
2276 ((member type '("http" "https" "ftp" "mailto" "doi"))
2277 (concat type ":" raw-path))
2278 ((string= type "file") (org-export-file-uri raw-path))
2279 (t raw-path))))
2280 (cond
2281 ;; Link type is handled by a special function.
2282 ((org-export-custom-protocol-maybe link desc 'latex))
2283 ;; Image file.
2284 (imagep (org-latex--inline-image link info))
2285 ;; Radio link: Transcode target's contents and use them as link's
2286 ;; description.
2287 ((string= type "radio")
2288 (let ((destination (org-export-resolve-radio-link link info)))
2289 (if (not destination) desc
2290 (format "\\hyperref[%s]{%s}"
2291 (org-export-get-reference destination info)
2292 desc))))
2293 ;; Links pointing to a headline: Find destination and build
2294 ;; appropriate referencing command.
2295 ((member type '("custom-id" "fuzzy" "id"))
2296 (let ((destination (if (string= type "fuzzy")
2297 (org-export-resolve-fuzzy-link link info)
2298 (org-export-resolve-id-link link info))))
2299 (case (org-element-type destination)
2300 ;; Id link points to an external file.
2301 (plain-text
2302 (if desc (format "\\href{%s}{%s}" destination desc)
2303 (format "\\url{%s}" destination)))
2304 ;; Fuzzy link points nowhere.
2305 ((nil)
2306 (format (plist-get info :latex-link-with-unknown-path-format)
2307 (or desc
2308 (org-export-data
2309 (org-element-property :raw-link link) info))))
2310 ;; LINK points to a headline. If headlines are numbered
2311 ;; and the link has no description, display headline's
2312 ;; number. Otherwise, display description or headline's
2313 ;; title.
2314 (headline
2315 (let ((label (org-latex--label destination info t)))
2316 (if (and (not desc)
2317 (org-export-numbered-headline-p destination info))
2318 (format "\\ref{%s}" label)
2319 (format "\\hyperref[%s]{%s}" label
2320 (or desc
2321 (org-export-data
2322 (org-element-property :title destination) info))))))
2323 ;; Fuzzy link points to a target. Do as above.
2324 (otherwise
2325 (let ((ref (org-latex--label destination info t)))
2326 (if (not desc) (format "\\ref{%s}" ref)
2327 (format "\\hyperref[%s]{%s}" ref desc)))))))
2328 ;; Coderef: replace link with the reference name or the
2329 ;; equivalent line number.
2330 ((string= type "coderef")
2331 (format (org-export-get-coderef-format path desc)
2332 (org-export-resolve-coderef path info)))
2333 ;; External link with a description part.
2334 ((and path desc) (format "\\href{%s}{%s}" path desc))
2335 ;; External link without a description part.
2336 (path (format "\\url{%s}" path))
2337 ;; No path, only description. Try to do something useful.
2338 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
2341 ;;;; Node Property
2343 (defun org-latex-node-property (node-property contents info)
2344 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2345 CONTENTS is nil. INFO is a plist holding contextual
2346 information."
2347 (format "%s:%s"
2348 (org-element-property :key node-property)
2349 (let ((value (org-element-property :value node-property)))
2350 (if value (concat " " value) ""))))
2353 ;;;; Paragraph
2355 (defun org-latex-paragraph (paragraph contents info)
2356 "Transcode a PARAGRAPH element from Org to LaTeX.
2357 CONTENTS is the contents of the paragraph, as a string. INFO is
2358 the plist used as a communication channel."
2359 contents)
2362 ;;;; Plain List
2364 (defun org-latex-plain-list (plain-list contents info)
2365 "Transcode a PLAIN-LIST element from Org to LaTeX.
2366 CONTENTS is the contents of the list. INFO is a plist holding
2367 contextual information."
2368 (let* ((type (org-element-property :type plain-list))
2369 (attr (org-export-read-attribute :attr_latex plain-list))
2370 (latex-type (let ((env (plist-get attr :environment)))
2371 (cond (env (format "%s" env))
2372 ((eq type 'ordered) "enumerate")
2373 ((eq type 'descriptive) "description")
2374 (t "itemize")))))
2375 (org-latex--wrap-label
2376 plain-list
2377 (format "\\begin{%s}%s\n%s\\end{%s}"
2378 latex-type
2379 (or (plist-get attr :options) "")
2380 contents
2381 latex-type)
2382 info)))
2385 ;;;; Plain Text
2387 (defun org-latex-plain-text (text info)
2388 "Transcode a TEXT string from Org to LaTeX.
2389 TEXT is the string to transcode. INFO is a plist holding
2390 contextual information."
2391 (let* ((specialp (plist-get info :with-special-strings))
2392 (output
2393 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2394 (let ((case-fold-search nil))
2395 (replace-regexp-in-string
2396 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2397 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2398 ;; However, if special strings are used, be careful not
2399 ;; to protect "\" in "\-" constructs.
2400 (replace-regexp-in-string
2401 (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\([^-]\\|$\\)"))
2402 (lambda (m)
2403 (case (string-to-char m)
2404 (?\\ "$\\\\backslash$\\1")
2405 (?~ "\\\\textasciitilde{}")
2406 (?^ "\\\\^{}")
2407 (t "\\\\\\&")))
2408 text)))))
2409 ;; Activate smart quotes. Be sure to provide original TEXT string
2410 ;; since OUTPUT may have been modified.
2411 (when (plist-get info :with-smart-quotes)
2412 (setq output (org-export-activate-smart-quotes output :latex info text)))
2413 ;; Convert special strings.
2414 (when specialp
2415 (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
2416 ;; Handle break preservation if required.
2417 (when (plist-get info :preserve-breaks)
2418 (setq output (replace-regexp-in-string
2419 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
2420 ;; Return value.
2421 output))
2424 ;;;; Planning
2426 (defun org-latex-planning (planning contents info)
2427 "Transcode a PLANNING element from Org to LaTeX.
2428 CONTENTS is nil. INFO is a plist holding contextual
2429 information."
2430 (concat
2431 "\\noindent"
2432 (mapconcat
2433 'identity
2434 (delq nil
2435 (list
2436 (let ((closed (org-element-property :closed planning)))
2437 (when closed
2438 (concat
2439 (format "\\textbf{%s} " org-closed-string)
2440 (format (plist-get info :latex-inactive-timestamp-format)
2441 (org-timestamp-translate closed)))))
2442 (let ((deadline (org-element-property :deadline planning)))
2443 (when deadline
2444 (concat
2445 (format "\\textbf{%s} " org-deadline-string)
2446 (format (plist-get info :latex-active-timestamp-format)
2447 (org-timestamp-translate deadline)))))
2448 (let ((scheduled (org-element-property :scheduled planning)))
2449 (when scheduled
2450 (concat
2451 (format "\\textbf{%s} " org-scheduled-string)
2452 (format (plist-get info :latex-active-timestamp-format)
2453 (org-timestamp-translate scheduled)))))))
2454 " ")
2455 "\\\\"))
2458 ;;;; Property Drawer
2460 (defun org-latex-property-drawer (property-drawer contents info)
2461 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2462 CONTENTS holds the contents of the drawer. INFO is a plist
2463 holding contextual information."
2464 (and (org-string-nw-p contents)
2465 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2468 ;;;; Pseudo Element: LaTeX Matrices
2470 ;; `latex-matrices' elements have the following properties:
2471 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2472 ;; `math').
2474 (defun org-latex--wrap-latex-matrices (data info)
2475 "Merge contiguous tables with the same mode within a pseudo-element.
2476 DATA is a parse tree or a secondary string. INFO is a plist
2477 containing export options. Modify DATA by side-effect and return
2478 it."
2479 (org-element-map data 'table
2480 (lambda (table)
2481 (when (eq (org-element-property :type table) 'org)
2482 (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
2483 (plist-get info :latex-default-table-mode))))
2484 (when (and (member mode '("inline-math" "math"))
2485 ;; Do not wrap twice the same table.
2486 (not (eq (org-element-type
2487 (org-element-property :parent table))
2488 'latex-matrices)))
2489 (let* ((caption (and (not (string= mode "inline-math"))
2490 (org-element-property :caption table)))
2491 (matrices
2492 (list 'latex-matrices
2493 (list :caption caption
2494 :markup
2495 (cond ((string= mode "inline-math") 'inline)
2496 (caption 'equation)
2497 (t 'math)))))
2498 (previous table)
2499 (next (org-export-get-next-element table info)))
2500 (org-element-insert-before matrices table)
2501 ;; Swallow all contiguous tables sharing the same mode.
2502 (while (and
2503 (zerop (or (org-element-property :post-blank previous) 0))
2504 (setq next (org-export-get-next-element previous info))
2505 (eq (org-element-type next) 'table)
2506 (eq (org-element-property :type next) 'org)
2507 (string= (or (org-export-read-attribute
2508 :attr_latex next :mode)
2509 (plist-get info :latex-default-table-mode))
2510 mode))
2511 (org-element-extract-element previous)
2512 (org-element-adopt-elements matrices previous)
2513 (setq previous next))
2514 (org-element-put-property
2515 matrices :post-blank (org-element-property :post-blank previous))
2516 (org-element-extract-element previous)
2517 (org-element-adopt-elements matrices previous))))))
2518 info)
2519 data)
2521 (defun org-latex-matrices (matrices contents info)
2522 "Transcode a MATRICES element from Org to LaTeX.
2523 CONTENTS is a string. INFO is a plist used as a communication
2524 channel."
2525 (format (case (org-element-property :markup matrices)
2526 (inline "\\(%s\\)")
2527 (equation "\\begin{equation}\n%s\\end{equation}")
2528 (t "\\[\n%s\\]"))
2529 contents))
2531 (defun org-latex-matrices-tree-filter (tree backend info)
2532 (org-latex--wrap-latex-matrices tree info))
2534 ;;;; Pseudo Object: LaTeX Math Block
2536 ;; `latex-math-block' objects have the following property:
2537 ;; `:post-blank'.
2539 (defun org-latex--wrap-latex-math-block (data info)
2540 "Merge contiguous math objects in a pseudo-object container.
2541 DATA is a parse tree or a secondary string. INFO is a plist
2542 containing export options. Modify DATA by side-effect and return it."
2543 (let ((valid-object-p
2544 (function
2545 ;; Non-nil when OBJ can be added to the latex math block.
2546 (lambda (obj)
2547 (case (org-element-type obj)
2548 (entity (org-element-property :latex-math-p obj))
2549 (latex-fragment
2550 (let ((value (org-element-property :value obj)))
2551 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2552 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2553 ((subscript superscript) t))))))
2554 (org-element-map data '(entity latex-fragment subscript superscript)
2555 (lambda (object)
2556 ;; Skip objects already wrapped.
2557 (when (and (not (eq (org-element-type
2558 (org-element-property :parent object))
2559 'latex-math-block))
2560 (funcall valid-object-p object))
2561 (let ((math-block (list 'latex-math-block nil))
2562 (next-elements (org-export-get-next-element object info t))
2563 (last object))
2564 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2565 (org-element-insert-before math-block object)
2566 (org-element-extract-element object)
2567 (org-element-adopt-elements math-block object)
2568 (when (zerop (or (org-element-property :post-blank object) 0))
2569 ;; MATH-BLOCK swallows consecutive math objects.
2570 (catch 'exit
2571 (dolist (next next-elements)
2572 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2573 (org-element-extract-element next)
2574 (org-element-adopt-elements math-block next)
2575 ;; Eschew the case: \beta$x$ -> \(\betax\).
2576 (unless (memq (org-element-type next)
2577 '(subscript superscript))
2578 (org-element-put-property last :post-blank 1))
2579 (setq last next)
2580 (when (> (or (org-element-property :post-blank next) 0) 0)
2581 (throw 'exit nil))))))
2582 (org-element-put-property
2583 math-block :post-blank (org-element-property :post-blank last)))))
2584 info nil '(subscript superscript latex-math-block) t)
2585 ;; Return updated DATA.
2586 data))
2588 (defun org-latex-math-block-tree-filter (tree backend info)
2589 (org-latex--wrap-latex-math-block tree info))
2591 (defun org-latex-math-block-options-filter (info backend)
2592 (dolist (prop '(:author :date :title) info)
2593 (plist-put info prop
2594 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2596 (defun org-latex-math-block (math-block contents info)
2597 "Transcode a MATH-BLOCK object from Org to LaTeX.
2598 CONTENTS is a string. INFO is a plist used as a communication
2599 channel."
2600 (when (org-string-nw-p contents)
2601 (format "\\(%s\\)" (org-trim contents))))
2603 ;;;; Quote Block
2605 (defun org-latex-quote-block (quote-block contents info)
2606 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2607 CONTENTS holds the contents of the block. INFO is a plist
2608 holding contextual information."
2609 (org-latex--wrap-label
2610 quote-block (format "\\begin{quote}\n%s\\end{quote}" contents) info))
2613 ;;;; Radio Target
2615 (defun org-latex-radio-target (radio-target text info)
2616 "Transcode a RADIO-TARGET object from Org to LaTeX.
2617 TEXT is the text of the target. INFO is a plist holding
2618 contextual information."
2619 (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
2622 ;;;; Section
2624 (defun org-latex-section (section contents info)
2625 "Transcode a SECTION element from Org to LaTeX.
2626 CONTENTS holds the contents of the section. INFO is a plist
2627 holding contextual information."
2628 contents)
2631 ;;;; Special Block
2633 (defun org-latex-special-block (special-block contents info)
2634 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2635 CONTENTS holds the contents of the block. INFO is a plist
2636 holding contextual information."
2637 (let ((type (org-element-property :type special-block))
2638 (opt (org-export-read-attribute :attr_latex special-block :options))
2639 (caption (org-latex--caption/label-string special-block info))
2640 (caption-above-p (org-latex--caption-above-p special-block info)))
2641 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2642 (and caption-above-p caption)
2643 contents
2644 (and (not caption-above-p) caption)
2645 (format "\\end{%s}" type))))
2648 ;;;; Src Block
2650 (defun org-latex-src-block (src-block contents info)
2651 "Transcode a SRC-BLOCK element from Org to LaTeX.
2652 CONTENTS holds the contents of the item. INFO is a plist holding
2653 contextual information."
2654 (when (org-string-nw-p (org-element-property :value src-block))
2655 (let* ((lang (org-element-property :language src-block))
2656 (caption (org-element-property :caption src-block))
2657 (caption-above-p (org-latex--caption-above-p src-block info))
2658 (label (org-element-property :name src-block))
2659 (custom-env (and lang
2660 (cadr (assq (intern lang)
2661 org-latex-custom-lang-environments))))
2662 (num-start (case (org-element-property :number-lines src-block)
2663 (continued (org-export-get-loc src-block info))
2664 (new 0)))
2665 (retain-labels (org-element-property :retain-labels src-block))
2666 (attributes (org-export-read-attribute :attr_latex src-block))
2667 (float (plist-get attributes :float))
2668 (listings (plist-get info :latex-listings)))
2669 (cond
2670 ;; Case 1. No source fontification.
2671 ((not listings)
2672 (let* ((caption-str (org-latex--caption/label-string src-block info))
2673 (float-env
2674 (cond ((string= "multicolumn" float)
2675 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2676 (plist-get info :latex-default-figure-position)
2677 (if caption-above-p caption-str "")
2678 (if caption-above-p "" caption-str)))
2679 (caption (concat
2680 (if caption-above-p caption-str "")
2681 "%s"
2682 (if caption-above-p "" (concat "\n" caption-str))))
2683 (t "%s"))))
2684 (format
2685 float-env
2686 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2687 (org-export-format-code-default src-block info))))))
2688 ;; Case 2. Custom environment.
2689 (custom-env
2690 (let ((caption-str (org-latex--caption/label-string src-block info)))
2691 (format "\\begin{%s}\n%s\\end{%s}\n"
2692 custom-env
2693 (concat (and caption-above-p caption-str)
2694 (org-export-format-code-default src-block info)
2695 (and (not caption-above-p) caption-str))
2696 custom-env)))
2697 ;; Case 3. Use minted package.
2698 ((eq listings 'minted)
2699 (let* ((caption-str (org-latex--caption/label-string src-block info))
2700 (float-env
2701 (cond
2702 ((string= "multicolumn" float)
2703 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2704 (if caption-above-p caption-str "")
2705 (if caption-above-p "" caption-str)))
2706 (caption
2707 (concat (if caption-above-p caption-str "")
2708 "%s"
2709 (if caption-above-p "" (concat "\n" caption-str))))
2710 (t "%s")))
2711 (options (plist-get info :latex-minted-options))
2712 (body
2713 (format
2714 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2715 ;; Options.
2716 (concat
2717 (org-latex--make-option-string
2718 (if (or (not num-start) (assoc "linenos" options))
2719 options
2720 (append
2721 `(("linenos")
2722 ("firstnumber" ,(number-to-string (1+ num-start))))
2723 options)))
2724 (let ((local-options (plist-get attributes :options)))
2725 (and local-options (concat "," local-options))))
2726 ;; Language.
2727 (or (cadr (assq (intern lang)
2728 (plist-get info :latex-minted-langs)))
2729 (downcase lang))
2730 ;; Source code.
2731 (let* ((code-info (org-export-unravel-code src-block))
2732 (max-width
2733 (apply 'max
2734 (mapcar 'length
2735 (org-split-string (car code-info)
2736 "\n")))))
2737 (org-export-format-code
2738 (car code-info)
2739 (lambda (loc num ref)
2740 (concat
2742 (when ref
2743 ;; Ensure references are flushed to the right,
2744 ;; separated with 6 spaces from the widest line
2745 ;; of code.
2746 (concat (make-string (+ (- max-width (length loc)) 6)
2747 ?\s)
2748 (format "(%s)" ref)))))
2749 nil (and retain-labels (cdr code-info)))))))
2750 ;; Return value.
2751 (format float-env body)))
2752 ;; Case 4. Use listings package.
2754 (let ((lst-lang
2755 (or (cadr (assq (intern lang)
2756 (plist-get info :latex-listings-langs)))
2757 lang))
2758 (caption-str
2759 (when caption
2760 (let ((main (org-export-get-caption src-block))
2761 (secondary (org-export-get-caption src-block t)))
2762 (if (not secondary)
2763 (format "{%s}" (org-export-data main info))
2764 (format "{[%s]%s}"
2765 (org-export-data secondary info)
2766 (org-export-data main info))))))
2767 (lst-opt (plist-get info :latex-listings-options)))
2768 (concat
2769 ;; Options.
2770 (format
2771 "\\lstset{%s}\n"
2772 (concat
2773 (org-latex--make-option-string
2774 (append
2775 lst-opt
2776 (cond
2777 ((and (not float) (plist-member attributes :float)) nil)
2778 ((string= "multicolumn" float) '(("float" "*")))
2779 ((and float (not (assoc "float" lst-opt)))
2780 `(("float" ,(plist-get info :latex-default-figure-position)))))
2781 `(("language" ,lst-lang))
2782 (if label `(("label" ,label)) '(("label" " ")))
2783 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2784 `(("captionpos" ,(if caption-above-p "t" "b")))
2785 (cond ((assoc "numbers" lst-opt) nil)
2786 ((not num-start) '(("numbers" "none")))
2787 ((zerop num-start) '(("numbers" "left")))
2788 (t `(("firstnumber" ,(number-to-string (1+ num-start)))
2789 ("numbers" "left"))))))
2790 (let ((local-options (plist-get attributes :options)))
2791 (and local-options (concat "," local-options)))))
2792 ;; Source code.
2793 (format
2794 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2795 (let* ((code-info (org-export-unravel-code src-block))
2796 (max-width
2797 (apply 'max
2798 (mapcar 'length
2799 (org-split-string (car code-info) "\n")))))
2800 (org-export-format-code
2801 (car code-info)
2802 (lambda (loc num ref)
2803 (concat
2805 (when ref
2806 ;; Ensure references are flushed to the right,
2807 ;; separated with 6 spaces from the widest line of
2808 ;; code
2809 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2810 (format "(%s)" ref)))))
2811 nil (and retain-labels (cdr code-info))))))))))))
2814 ;;;; Statistics Cookie
2816 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2817 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2818 CONTENTS is nil. INFO is a plist holding contextual information."
2819 (replace-regexp-in-string
2820 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2823 ;;;; Strike-Through
2825 (defun org-latex-strike-through (strike-through contents info)
2826 "Transcode STRIKE-THROUGH from Org to LaTeX.
2827 CONTENTS is the text with strike-through markup. INFO is a plist
2828 holding contextual information."
2829 (org-latex--text-markup contents 'strike-through info))
2832 ;;;; Subscript
2834 (defun org-latex--script-size (object info)
2835 "Transcode a subscript or superscript object.
2836 OBJECT is an Org object. INFO is a plist used as a communication
2837 channel."
2838 (let ((type (org-element-type object))
2839 (output ""))
2840 (org-element-map (org-element-contents object)
2841 (cons 'plain-text org-element-all-objects)
2842 (lambda (obj)
2843 (case (org-element-type obj)
2844 ((entity latex-fragment)
2845 (let ((data (org-trim (org-export-data obj info))))
2846 (string-match
2847 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2848 data)
2849 (setq output
2850 (concat output
2851 (match-string 1 data)
2852 (let ((blank (org-element-property :post-blank obj)))
2853 (and blank (> blank 0) "\\ "))))))
2854 (plain-text
2855 (setq output
2856 (format "%s\\text{%s}" output (org-export-data obj info))))
2857 (otherwise
2858 (setq output
2859 (concat output
2860 (org-export-data obj info)
2861 (let ((blank (org-element-property :post-blank obj)))
2862 (and blank (> blank 0) "\\ ")))))))
2863 info nil org-element-recursive-objects)
2864 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2865 ;; character.
2866 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2867 (and (> (length output) 1) "{")
2868 output
2869 (and (> (length output) 1) "}"))))
2871 (defun org-latex-subscript (subscript contents info)
2872 "Transcode a SUBSCRIPT object from Org to LaTeX.
2873 CONTENTS is the contents of the object. INFO is a plist holding
2874 contextual information."
2875 (org-latex--script-size subscript info))
2878 ;;;; Superscript
2880 (defun org-latex-superscript (superscript contents info)
2881 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2882 CONTENTS is the contents of the object. INFO is a plist holding
2883 contextual information."
2884 (org-latex--script-size superscript info))
2887 ;;;; Table
2889 ;; `org-latex-table' is the entry point for table transcoding. It
2890 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2891 ;; delegates the job to either `org-latex--table.el-table',
2892 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2893 ;; depending of the type of the table and the mode requested.
2895 ;; `org-latex--align-string' is a subroutine used to build alignment
2896 ;; string for Org tables.
2898 (defun org-latex-table (table contents info)
2899 "Transcode a TABLE element from Org to LaTeX.
2900 CONTENTS is the contents of the table. INFO is a plist holding
2901 contextual information."
2902 (if (eq (org-element-property :type table) 'table.el)
2903 ;; "table.el" table. Convert it using appropriate tools.
2904 (org-latex--table.el-table table info)
2905 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2906 (plist-get info :latex-default-table-mode))))
2907 (cond
2908 ;; Case 1: Verbatim table.
2909 ((string= type "verbatim")
2910 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2911 ;; Re-create table, without affiliated keywords.
2912 (org-trim (org-element-interpret-data
2913 `(table nil ,@(org-element-contents table))))))
2914 ;; Case 2: Matrix.
2915 ((or (string= type "math") (string= type "inline-math"))
2916 (org-latex--math-table table info))
2917 ;; Case 3: Standard table.
2918 (t (concat (org-latex--org-table table contents info)
2919 ;; When there are footnote references within the
2920 ;; table, insert their definition just after it.
2921 (org-latex--delayed-footnotes-definitions table info)))))))
2923 (defun org-latex--align-string (table info)
2924 "Return an appropriate LaTeX alignment string.
2925 TABLE is the considered table. INFO is a plist used as
2926 a communication channel."
2927 (or (org-export-read-attribute :attr_latex table :align)
2928 (let (align)
2929 ;; Extract column groups and alignment from first (non-rule)
2930 ;; row.
2931 (org-element-map
2932 (org-element-map table 'table-row
2933 (lambda (row)
2934 (and (eq (org-element-property :type row) 'standard) row))
2935 info 'first-match)
2936 'table-cell
2937 (lambda (cell)
2938 (let ((borders (org-export-table-cell-borders cell info)))
2939 ;; Check left border for the first cell only.
2940 (when (and (memq 'left borders) (not align))
2941 (push "|" align))
2942 (push (case (org-export-table-cell-alignment cell info)
2943 (left "l")
2944 (right "r")
2945 (center "c"))
2946 align)
2947 (when (memq 'right borders) (push "|" align))))
2948 info)
2949 (apply 'concat (nreverse align)))))
2951 (defun org-latex--org-table (table contents info)
2952 "Return appropriate LaTeX code for an Org table.
2954 TABLE is the table type element to transcode. CONTENTS is its
2955 contents, as a string. INFO is a plist used as a communication
2956 channel.
2958 This function assumes TABLE has `org' as its `:type' property and
2959 `table' as its `:mode' attribute."
2960 (let* ((caption (org-latex--caption/label-string table info))
2961 (attr (org-export-read-attribute :attr_latex table))
2962 ;; Determine alignment string.
2963 (alignment (org-latex--align-string table info))
2964 ;; Determine environment for the table: longtable, tabular...
2965 (table-env (or (plist-get attr :environment)
2966 (plist-get info :latex-default-table-environment)))
2967 ;; If table is a float, determine environment: table, table*
2968 ;; or sidewaystable.
2969 (float-env (unless (member table-env '("longtable" "longtabu"))
2970 (let ((float (plist-get attr :float)))
2971 (cond
2972 ((and (not float) (plist-member attr :float)) nil)
2973 ((or (string= float "sidewaystable")
2974 (string= float "sideways")) "sidewaystable")
2975 ((string= float "multicolumn") "table*")
2976 ((or float
2977 (org-element-property :caption table)
2978 (org-string-nw-p (plist-get attr :caption)))
2979 "table")))))
2980 ;; Extract others display options.
2981 (fontsize (let ((font (plist-get attr :font)))
2982 (and font (concat font "\n"))))
2983 ;; "tabular" environment doesn't allow to define a width.
2984 (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
2985 (spreadp (plist-get attr :spread))
2986 (placement
2987 (or (plist-get attr :placement)
2988 (format "[%s]" (plist-get info :latex-default-figure-position))))
2989 (centerp (if (plist-member attr :center) (plist-get attr :center)
2990 (plist-get info :latex-tables-centered)))
2991 (caption-above-p (org-latex--caption-above-p table info)))
2992 ;; Prepare the final format string for the table.
2993 (cond
2994 ;; Longtable.
2995 ((equal "longtable" table-env)
2996 (concat (and fontsize (concat "{" fontsize))
2997 (format "\\begin{longtable}{%s}\n" alignment)
2998 (and caption-above-p
2999 (org-string-nw-p caption)
3000 (concat caption "\\\\\n"))
3001 contents
3002 (and (not caption-above-p)
3003 (org-string-nw-p caption)
3004 (concat caption "\\\\\n"))
3005 "\\end{longtable}\n"
3006 (and fontsize "}")))
3007 ;; Longtabu
3008 ((equal "longtabu" table-env)
3009 (concat (and fontsize (concat "{" fontsize))
3010 (format "\\begin{longtabu}%s{%s}\n"
3011 (if width
3012 (format " %s %s "
3013 (if spreadp "spread" "to") width) "")
3014 alignment)
3015 (and caption-above-p
3016 (org-string-nw-p caption)
3017 (concat caption "\\\\\n"))
3018 contents
3019 (and (not caption-above-p)
3020 (org-string-nw-p caption)
3021 (concat caption "\\\\\n"))
3022 "\\end{longtabu}\n"
3023 (and fontsize "}")))
3024 ;; Others.
3025 (t (concat (cond
3026 (float-env
3027 (concat (format "\\begin{%s}%s\n" float-env placement)
3028 (if caption-above-p caption "")
3029 (when centerp "\\centering\n")
3030 fontsize))
3031 ((and (not float-env) caption)
3032 (concat
3033 (and centerp "\\begin{center}\n" )
3034 (if caption-above-p caption "")
3035 (cond ((and fontsize centerp) fontsize)
3036 (fontsize (concat "{" fontsize)))))
3037 (centerp (concat "\\begin{center}\n" fontsize))
3038 (fontsize (concat "{" fontsize)))
3039 (cond ((equal "tabu" table-env)
3040 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
3041 (if width (format
3042 (if spreadp " spread %s " " to %s ")
3043 width) "")
3044 alignment
3045 contents))
3046 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
3047 table-env
3048 (if width (format "{%s}" width) "")
3049 alignment
3050 contents
3051 table-env)))
3052 (cond
3053 (float-env
3054 (concat (if caption-above-p "" (concat "\n" caption))
3055 (format "\n\\end{%s}" float-env)))
3056 ((and (not float-env) caption)
3057 (concat
3058 (if caption-above-p "" (concat "\n" caption))
3059 (and centerp "\n\\end{center}")
3060 (and fontsize (not centerp) "}")))
3061 (centerp "\n\\end{center}")
3062 (fontsize "}")))))))
3064 (defun org-latex--table.el-table (table info)
3065 "Return appropriate LaTeX code for a table.el table.
3067 TABLE is the table type element to transcode. INFO is a plist
3068 used as a communication channel.
3070 This function assumes TABLE has `table.el' as its `:type'
3071 property."
3072 (require 'table)
3073 ;; Ensure "*org-export-table*" buffer is empty.
3074 (with-current-buffer (get-buffer-create "*org-export-table*")
3075 (erase-buffer))
3076 (let ((output (with-temp-buffer
3077 (insert (org-element-property :value table))
3078 (goto-char 1)
3079 (re-search-forward "^[ \t]*|[^|]" nil t)
3080 (table-generate-source 'latex "*org-export-table*")
3081 (with-current-buffer "*org-export-table*"
3082 (org-trim (buffer-string))))))
3083 (kill-buffer (get-buffer "*org-export-table*"))
3084 ;; Remove left out comments.
3085 (while (string-match "^%.*\n" output)
3086 (setq output (replace-match "" t t output)))
3087 (let ((attr (org-export-read-attribute :attr_latex table)))
3088 (when (plist-get attr :rmlines)
3089 ;; When the "rmlines" attribute is provided, remove all hlines
3090 ;; but the the one separating heading from the table body.
3091 (let ((n 0) (pos 0))
3092 (while (and (< (length output) pos)
3093 (setq pos (string-match "^\\\\hline\n?" output pos)))
3094 (incf n)
3095 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
3096 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
3097 (plist-get info :latex-tables-centered))))
3098 (if (not centerp) output
3099 (format "\\begin{center}\n%s\n\\end{center}" output))))))
3101 (defun org-latex--math-table (table info)
3102 "Return appropriate LaTeX code for a matrix.
3104 TABLE is the table type element to transcode. INFO is a plist
3105 used as a communication channel.
3107 This function assumes TABLE has `org' as its `:type' property and
3108 `inline-math' or `math' as its `:mode' attribute."
3109 (let* ((attr (org-export-read-attribute :attr_latex table))
3110 (env (or (plist-get attr :environment)
3111 (plist-get info :latex-default-table-environment)))
3112 (contents
3113 (mapconcat
3114 (lambda (row)
3115 ;; Ignore horizontal rules.
3116 (when (eq (org-element-property :type row) 'standard)
3117 ;; Return each cell unmodified.
3118 (concat
3119 (mapconcat
3120 (lambda (cell)
3121 (substring (org-element-interpret-data cell) 0 -1))
3122 (org-element-map row 'table-cell #'identity info) "&")
3123 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
3124 "\n")))
3125 (org-element-map table 'table-row #'identity info) "")))
3126 (concat
3127 ;; Prefix.
3128 (plist-get attr :math-prefix)
3129 ;; Environment. Also treat special cases.
3130 (cond ((member env '("array" "tabular"))
3131 (let ((align (make-string
3132 (cdr (org-export-table-dimensions table info)) ?c)))
3133 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
3134 ((assoc env org-latex-table-matrix-macros)
3135 (format "\\%s%s{\n%s}"
3137 (or (plist-get attr :math-arguments) "")
3138 contents))
3139 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
3140 ;; Suffix.
3141 (plist-get attr :math-suffix))))
3144 ;;;; Table Cell
3146 (defun org-latex-table-cell (table-cell contents info)
3147 "Transcode a TABLE-CELL element from Org to LaTeX.
3148 CONTENTS is the cell contents. INFO is a plist used as
3149 a communication channel."
3150 (concat
3151 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
3152 (if (and contents
3153 scientific-format
3154 (string-match orgtbl-exp-regexp contents))
3155 ;; Use appropriate format string for scientific
3156 ;; notation.
3157 (format scientific-format
3158 (match-string 1 contents)
3159 (match-string 2 contents))
3160 contents))
3161 (when (org-export-get-next-element table-cell info) " & ")))
3164 ;;;; Table Row
3166 (defun org-latex-table-row (table-row contents info)
3167 "Transcode a TABLE-ROW element from Org to LaTeX.
3168 CONTENTS is the contents of the row. INFO is a plist used as
3169 a communication channel."
3170 (let* ((attr (org-export-read-attribute :attr_latex
3171 (org-export-get-parent table-row)))
3172 (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
3173 (plist-get info :latex-tables-booktabs)))
3174 (longtablep
3175 (member (or (plist-get attr :environment)
3176 (plist-get info :latex-default-table-environment))
3177 '("longtable" "longtabu"))))
3178 (if (eq (org-element-property :type table-row) 'rule)
3179 (cond
3180 ((not booktabsp) "\\hline")
3181 ((not (org-export-get-previous-element table-row info)) "\\toprule")
3182 ((not (org-export-get-next-element table-row info)) "\\bottomrule")
3183 ((and longtablep
3184 (org-export-table-row-ends-header-p
3185 (org-export-get-previous-element table-row info) info))
3187 (t "\\midrule"))
3188 (concat
3189 ;; When BOOKTABS are activated enforce top-rule even when no
3190 ;; hline was specifically marked.
3191 (and booktabsp (not (org-export-get-previous-element table-row info))
3192 "\\toprule\n")
3193 contents "\\\\\n"
3194 (cond
3195 ;; Special case for long tables. Define header and footers.
3196 ((and longtablep (org-export-table-row-ends-header-p table-row info))
3197 (let ((columns (cdr (org-export-table-dimensions
3198 (org-export-get-parent-table table-row) info))))
3199 (format "%s
3200 \\endfirsthead
3201 \\multicolumn{%d}{l}{%s} \\\\
3203 %s \\\\\n
3205 \\endhead
3206 %s\\multicolumn{%d}{r}{%s} \\\\
3207 \\endfoot
3208 \\endlastfoot"
3209 (if booktabsp "\\midrule" "\\hline")
3210 columns
3211 (org-latex--translate "Continued from previous page" info)
3212 (cond
3213 ((not (org-export-table-row-starts-header-p table-row info))
3215 (booktabsp "\\toprule\n")
3216 (t "\\hline\n"))
3217 contents
3218 (if booktabsp "\\midrule" "\\hline")
3219 (if booktabsp "\\midrule" "\\hline")
3220 columns
3221 (org-latex--translate "Continued on next page" info))))
3222 ;; When BOOKTABS are activated enforce bottom rule even when
3223 ;; no hline was specifically marked.
3224 ((and booktabsp (not (org-export-get-next-element table-row info)))
3225 "\\bottomrule"))))))
3228 ;;;; Target
3230 (defun org-latex-target (target contents info)
3231 "Transcode a TARGET object from Org to LaTeX.
3232 CONTENTS is nil. INFO is a plist holding contextual
3233 information."
3234 (format "\\label{%s}" (org-latex--label target info)))
3237 ;;;; Timestamp
3239 (defun org-latex-timestamp (timestamp contents info)
3240 "Transcode a TIMESTAMP object from Org to LaTeX.
3241 CONTENTS is nil. INFO is a plist holding contextual
3242 information."
3243 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
3244 (format
3245 (plist-get info
3246 (case (org-element-property :type timestamp)
3247 ((active active-range) :latex-active-timestamp-format)
3248 ((inactive inactive-range) :latex-inactive-timestamp-format)
3249 (otherwise :latex-diary-timestamp-format)))
3250 value)))
3253 ;;;; Underline
3255 (defun org-latex-underline (underline contents info)
3256 "Transcode UNDERLINE from Org to LaTeX.
3257 CONTENTS is the text with underline markup. INFO is a plist
3258 holding contextual information."
3259 (org-latex--text-markup contents 'underline info))
3262 ;;;; Verbatim
3264 (defun org-latex-verbatim (verbatim contents info)
3265 "Transcode a VERBATIM object from Org to LaTeX.
3266 CONTENTS is nil. INFO is a plist used as a communication
3267 channel."
3268 (org-latex--text-markup
3269 (org-element-property :value verbatim) 'verbatim info))
3272 ;;;; Verse Block
3274 (defun org-latex-verse-block (verse-block contents info)
3275 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3276 CONTENTS is verse block contents. INFO is a plist holding
3277 contextual information."
3278 (org-latex--wrap-label
3279 verse-block
3280 ;; In a verse environment, add a line break to each newline
3281 ;; character and change each white space at beginning of a line
3282 ;; into a space of 1 em. Also change each blank line with
3283 ;; a vertical space of 1 em.
3284 (format "\\begin{verse}\n%s\\end{verse}"
3285 (replace-regexp-in-string
3286 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
3287 (replace-regexp-in-string
3288 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3289 (replace-regexp-in-string
3290 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3291 contents nil t) nil t) nil t))
3292 info))
3296 ;;; End-user functions
3298 ;;;###autoload
3299 (defun org-latex-export-as-latex
3300 (&optional async subtreep visible-only body-only ext-plist)
3301 "Export current buffer as a LaTeX buffer.
3303 If narrowing is active in the current buffer, only export its
3304 narrowed part.
3306 If a region is active, export that region.
3308 A non-nil optional argument ASYNC means the process should happen
3309 asynchronously. The resulting buffer should be accessible
3310 through the `org-export-stack' interface.
3312 When optional argument SUBTREEP is non-nil, export the sub-tree
3313 at point, extracting information from the headline properties
3314 first.
3316 When optional argument VISIBLE-ONLY is non-nil, don't export
3317 contents of hidden elements.
3319 When optional argument BODY-ONLY is non-nil, only write code
3320 between \"\\begin{document}\" and \"\\end{document}\".
3322 EXT-PLIST, when provided, is a property list with external
3323 parameters overriding Org default settings, but still inferior to
3324 file-local settings.
3326 Export is done in a buffer named \"*Org LATEX Export*\", which
3327 will be displayed when `org-export-show-temporary-export-buffer'
3328 is non-nil."
3329 (interactive)
3330 (org-export-to-buffer 'latex "*Org LATEX Export*"
3331 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
3333 ;;;###autoload
3334 (defun org-latex-convert-region-to-latex ()
3335 "Assume the current region has org-mode syntax, and convert it to LaTeX.
3336 This can be used in any buffer. For example, you can write an
3337 itemized list in org-mode syntax in an LaTeX buffer and use this
3338 command to convert it."
3339 (interactive)
3340 (org-export-replace-region-by 'latex))
3342 ;;;###autoload
3343 (defun org-latex-export-to-latex
3344 (&optional async subtreep visible-only body-only ext-plist)
3345 "Export current buffer to a LaTeX file.
3347 If narrowing is active in the current buffer, only export its
3348 narrowed part.
3350 If a region is active, export that region.
3352 A non-nil optional argument ASYNC means the process should happen
3353 asynchronously. The resulting file should be accessible through
3354 the `org-export-stack' interface.
3356 When optional argument SUBTREEP is non-nil, export the sub-tree
3357 at point, extracting information from the headline properties
3358 first.
3360 When optional argument VISIBLE-ONLY is non-nil, don't export
3361 contents of hidden elements.
3363 When optional argument BODY-ONLY is non-nil, only write code
3364 between \"\\begin{document}\" and \"\\end{document}\".
3366 EXT-PLIST, when provided, is a property list with external
3367 parameters overriding Org default settings, but still inferior to
3368 file-local settings."
3369 (interactive)
3370 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3371 (org-export-to-file 'latex outfile
3372 async subtreep visible-only body-only ext-plist)))
3374 ;;;###autoload
3375 (defun org-latex-export-to-pdf
3376 (&optional async subtreep visible-only body-only ext-plist)
3377 "Export current buffer to LaTeX then process through to PDF.
3379 If narrowing is active in the current buffer, only export its
3380 narrowed part.
3382 If a region is active, export that region.
3384 A non-nil optional argument ASYNC means the process should happen
3385 asynchronously. The resulting file should be accessible through
3386 the `org-export-stack' interface.
3388 When optional argument SUBTREEP is non-nil, export the sub-tree
3389 at point, extracting information from the headline properties
3390 first.
3392 When optional argument VISIBLE-ONLY is non-nil, don't export
3393 contents of hidden elements.
3395 When optional argument BODY-ONLY is non-nil, only write code
3396 between \"\\begin{document}\" and \"\\end{document}\".
3398 EXT-PLIST, when provided, is a property list with external
3399 parameters overriding Org default settings, but still inferior to
3400 file-local settings.
3402 Return PDF file's name."
3403 (interactive)
3404 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3405 (org-export-to-file 'latex outfile
3406 async subtreep visible-only body-only ext-plist
3407 (lambda (file) (org-latex-compile file)))))
3409 (defun org-latex-compile (texfile &optional snippet)
3410 "Compile a TeX file.
3412 TEXFILE is the name of the file being compiled. Processing is
3413 done through the command specified in `org-latex-pdf-process'.
3415 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3416 file used to preview a LaTeX snippet. In this case, do not
3417 create a log buffer and do not bother removing log files.
3419 Return PDF file name or an error if it couldn't be produced."
3420 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3421 (full-name (file-truename texfile))
3422 (out-dir (file-name-directory texfile))
3423 ;; Properly set working directory for compilation.
3424 (default-directory (if (file-name-absolute-p texfile)
3425 (file-name-directory full-name)
3426 default-directory))
3427 (time (current-time))
3428 warnings)
3429 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
3430 (save-window-excursion
3431 (cond
3432 ;; A function is provided: Apply it.
3433 ((functionp org-latex-pdf-process)
3434 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3435 ;; A list is provided: Replace %b, %f and %o with appropriate
3436 ;; values in each command before applying it. Output is
3437 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3438 ((consp org-latex-pdf-process)
3439 (let ((outbuf (and (not snippet)
3440 (get-buffer-create "*Org PDF LaTeX Output*"))))
3441 (dolist (command org-latex-pdf-process)
3442 (shell-command
3443 (replace-regexp-in-string
3444 "%b" (shell-quote-argument base-name)
3445 (replace-regexp-in-string
3446 "%f" (shell-quote-argument full-name)
3447 (replace-regexp-in-string
3448 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
3449 outbuf))
3450 ;; Collect standard errors from output buffer.
3451 (setq warnings (and (not snippet)
3452 (org-latex--collect-warnings outbuf)))))
3453 (t (error "No valid command to process to PDF")))
3454 (let ((pdffile (concat out-dir base-name ".pdf")))
3455 ;; Check for process failure. Provide collected errors if
3456 ;; possible.
3457 (if (or (not (file-exists-p pdffile))
3458 (time-less-p (nth 5 (file-attributes pdffile)) time))
3459 (error (format "PDF file %s wasn't produced" pdffile))
3460 ;; Else remove log files, when specified, and signal end of
3461 ;; process to user, along with any error encountered.
3462 (unless snippet
3463 (when org-latex-remove-logfiles
3464 (dolist (file (directory-files
3465 out-dir t
3466 (concat (regexp-quote base-name)
3467 "\\(?:\\.[0-9]+\\)?"
3468 "\\."
3469 (regexp-opt org-latex-logfiles-extensions))))
3470 (delete-file file)))
3471 (message (concat "PDF file produced"
3472 (cond
3473 ((eq warnings 'error) " with errors.")
3474 (warnings (concat " with warnings: " warnings))
3475 (t "."))))))
3476 ;; Return output file name.
3477 pdffile))))
3479 (defun org-latex--collect-warnings (buffer)
3480 "Collect some warnings from \"pdflatex\" command output.
3481 BUFFER is the buffer containing output. Return collected
3482 warnings types as a string, `error' if a LaTeX error was
3483 encountered or nil if there was none."
3484 (with-current-buffer buffer
3485 (save-excursion
3486 (goto-char (point-max))
3487 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3488 (if (re-search-forward "^!" nil t) 'error
3489 (let ((case-fold-search t)
3490 (warnings ""))
3491 (dolist (warning org-latex-known-warnings)
3492 (when (save-excursion (re-search-forward (car warning) nil t))
3493 (setq warnings (concat warnings " " (cdr warning)))))
3494 (org-string-nw-p (org-trim warnings))))))))
3496 ;;;###autoload
3497 (defun org-latex-publish-to-latex (plist filename pub-dir)
3498 "Publish an Org file to LaTeX.
3500 FILENAME is the filename of the Org file to be published. PLIST
3501 is the property list for the given project. PUB-DIR is the
3502 publishing directory.
3504 Return output file name."
3505 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3507 ;;;###autoload
3508 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3509 "Publish an Org file to PDF (via LaTeX).
3511 FILENAME is the filename of the Org file to be published. PLIST
3512 is the property list for the given project. PUB-DIR is the
3513 publishing directory.
3515 Return output file name."
3516 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3517 ;; in working directory and then moved to publishing directory.
3518 (org-publish-attachment
3519 plist
3520 (org-latex-compile
3521 (org-publish-org-to
3522 'latex filename ".tex" plist (file-name-directory filename)))
3523 pub-dir))
3526 (provide 'ox-latex)
3528 ;; Local variables:
3529 ;; generated-autoload-file: "org-loaddefs.el"
3530 ;; End:
3532 ;;; ox-latex.el ends here