Fix f4d7646
[org-mode.git] / lisp / ox-latex.el
blobddbbd3368a887b7aa1c987c83d29177f2caa2e38
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 As a special case, a nil value prevents template from being
614 inserted.
616 Setting :latex-hyperref-template in publishing projects will take
617 precedence over this variable."
618 :group 'org-export-latex
619 :version "25.1"
620 :package-version '(Org . "8.3")
621 :type '(choice (const :tag "No template" nil)
622 (string :tag "Format string")))
623 (define-obsolete-variable-alias
624 'org-latex-with-hyperref 'org-latex-hyperref-template "25.1")
626 ;;;; Headline
628 (defcustom org-latex-format-headline-function
629 'org-latex-format-headline-default-function
630 "Function for formatting the headline's text.
632 This function will be called with six arguments:
633 TODO the todo keyword (string or nil)
634 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
635 PRIORITY the priority of the headline (integer or nil)
636 TEXT the main headline text (string)
637 TAGS the tags (list of strings or nil)
638 INFO the export options (plist)
640 The function result will be used in the section format string."
641 :group 'org-export-latex
642 :version "24.4"
643 :package-version '(Org . "8.0")
644 :type 'function)
647 ;;;; Footnotes
649 (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
650 "Text used to separate footnotes."
651 :group 'org-export-latex
652 :type 'string)
655 ;;;; Timestamps
657 (defcustom org-latex-active-timestamp-format "\\textit{%s}"
658 "A printf format string to be applied to active timestamps."
659 :group 'org-export-latex
660 :type 'string)
662 (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
663 "A printf format string to be applied to inactive timestamps."
664 :group 'org-export-latex
665 :type 'string)
667 (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
668 "A printf format string to be applied to diary timestamps."
669 :group 'org-export-latex
670 :type 'string)
673 ;;;; Links
675 (defcustom org-latex-image-default-option ""
676 "Default option for images."
677 :group 'org-export-latex
678 :version "24.4"
679 :package-version '(Org . "8.0")
680 :type 'string)
682 (defcustom org-latex-image-default-width ".9\\linewidth"
683 "Default width for images.
684 This value will not be used if a height is provided."
685 :group 'org-export-latex
686 :version "24.4"
687 :package-version '(Org . "8.0")
688 :type 'string)
690 (defcustom org-latex-image-default-height ""
691 "Default height for images.
692 This value will not be used if a width is provided, or if the
693 image is wrapped within a \"figure\" or \"wrapfigure\"
694 environment."
695 :group 'org-export-latex
696 :version "24.4"
697 :package-version '(Org . "8.0")
698 :type 'string)
700 (defcustom org-latex-default-figure-position "htb"
701 "Default position for latex figures."
702 :group 'org-export-latex
703 :type 'string)
705 (defcustom org-latex-inline-image-rules
706 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
707 "Rules characterizing image files that can be inlined into LaTeX.
709 A rule consists in an association whose key is the type of link
710 to consider, and value is a regexp that will be matched against
711 link's path.
713 Note that, by default, the image extension *actually* allowed
714 depend on the way the LaTeX file is processed. When used with
715 pdflatex, pdf, jpg and png images are OK. When processing
716 through dvi to Postscript, only ps and eps are allowed. The
717 default we use here encompasses both."
718 :group 'org-export-latex
719 :version "24.4"
720 :package-version '(Org . "8.0")
721 :type '(alist :key-type (string :tag "Type")
722 :value-type (regexp :tag "Path")))
724 (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
725 "Format string for links with unknown path type."
726 :group 'org-export-latex
727 :type 'string)
730 ;;;; Tables
732 (defcustom org-latex-default-table-environment "tabular"
733 "Default environment used to build tables."
734 :group 'org-export-latex
735 :version "24.4"
736 :package-version '(Org . "8.0")
737 :type 'string)
739 (defcustom org-latex-default-table-mode 'table
740 "Default mode for tables.
742 Value can be a symbol among:
744 `table' Regular LaTeX table.
746 `math' In this mode, every cell is considered as being in math
747 mode and the complete table will be wrapped within a math
748 environment. It is particularly useful to write matrices.
750 `inline-math' This mode is almost the same as `math', but the
751 math environment will be inlined.
753 `verbatim' The table is exported as it appears in the Org
754 buffer, within a verbatim environment.
756 This value can be overridden locally with, i.e. \":mode math\" in
757 LaTeX attributes.
759 When modifying this variable, it may be useful to change
760 `org-latex-default-table-environment' accordingly."
761 :group 'org-export-latex
762 :version "24.4"
763 :package-version '(Org . "8.0")
764 :type '(choice (const :tag "Table" table)
765 (const :tag "Matrix" math)
766 (const :tag "Inline matrix" inline-math)
767 (const :tag "Verbatim" verbatim))
768 :safe (lambda (s) (memq s '(table math inline-math verbatim))))
770 (defcustom org-latex-tables-centered t
771 "When non-nil, tables are exported in a center environment."
772 :group 'org-export-latex
773 :type 'boolean
774 :safe #'booleanp)
776 (defcustom org-latex-tables-booktabs nil
777 "When non-nil, display tables in a formal \"booktabs\" style.
778 This option assumes that the \"booktabs\" package is properly
779 loaded in the header of the document. This value can be ignored
780 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
781 attributes."
782 :group 'org-export-latex
783 :version "24.4"
784 :package-version '(Org . "8.0")
785 :type 'boolean
786 :safe #'booleanp)
788 (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
789 "Format string to display numbers in scientific notation.
790 The format should have \"%s\" twice, for mantissa and exponent
791 \(i.e., \"%s\\\\times10^{%s}\").
793 When nil, no transformation is made."
794 :group 'org-export-latex
795 :version "24.4"
796 :package-version '(Org . "8.0")
797 :type '(choice
798 (string :tag "Format string")
799 (const :tag "No formatting" nil)))
801 ;;;; Text markup
803 (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
804 (code . protectedtexttt)
805 (italic . "\\emph{%s}")
806 (strike-through . "\\sout{%s}")
807 (underline . "\\uline{%s}")
808 (verbatim . protectedtexttt))
809 "Alist of LaTeX expressions to convert text markup.
811 The key must be a symbol among `bold', `code', `italic',
812 `strike-through', `underline' and `verbatim'. The value is
813 a formatting string to wrap fontified text with.
815 Value can also be set to the following symbols: `verb' and
816 `protectedtexttt'. For the former, Org will use \"\\verb\" to
817 create a format string and select a delimiter character that
818 isn't in the string. For the latter, Org will use \"\\texttt\"
819 to typeset and try to protect special characters.
821 If no association can be found for a given markup, text will be
822 returned as-is."
823 :group 'org-export-latex
824 :version "25.1"
825 :package-version '(Org . "8.3")
826 :type 'alist
827 :options '(bold code italic strike-through underline verbatim))
830 ;;;; Drawers
832 (defcustom org-latex-format-drawer-function
833 (lambda (name contents) contents)
834 "Function called to format a drawer in LaTeX code.
836 The function must accept two parameters:
837 NAME the drawer name, like \"LOGBOOK\"
838 CONTENTS the contents of the drawer.
840 The function should return the string to be exported.
842 The default function simply returns the value of CONTENTS."
843 :group 'org-export-latex
844 :version "24.4"
845 :package-version '(Org . "8.3")
846 :type 'function)
849 ;;;; Inlinetasks
851 (defcustom org-latex-format-inlinetask-function
852 'org-latex-format-inlinetask-default-function
853 "Function called to format an inlinetask in LaTeX code.
855 The function must accept seven parameters:
856 TODO the todo keyword (string or nil)
857 TODO-TYPE the todo type (symbol: `todo', `done', nil)
858 PRIORITY the inlinetask priority (integer or nil)
859 NAME the inlinetask name (string)
860 TAGS the inlinetask tags (list of strings or nil)
861 CONTENTS the contents of the inlinetask (string or nil)
862 INFO the export options (plist)
864 The function should return the string to be exported."
865 :group 'org-export-latex
866 :type 'function
867 :version "25.1"
868 :package-version '(Org . "8.3"))
871 ;; Src blocks
873 (defcustom org-latex-listings nil
874 "Non-nil means export source code using the listings package.
876 This package will fontify source code, possibly even with color.
877 If you want to use this, you also need to make LaTeX use the
878 listings package, and if you want to have color, the color
879 package. Just add these to `org-latex-packages-alist', for
880 example using customize, or with something like:
882 \(require 'ox-latex)
883 \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
884 \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
886 Alternatively,
888 \(setq org-latex-listings 'minted)
890 causes source code to be exported using the minted package as
891 opposed to listings. If you want to use minted, you need to add
892 the minted package to `org-latex-packages-alist', for example
893 using customize, or with
895 \(require 'ox-latex)
896 \(add-to-list 'org-latex-packages-alist '(\"newfloat\" \"minted\"))
898 In addition, it is necessary to install pygments
899 \(http://pygments.org), and to configure the variable
900 `org-latex-pdf-process' so that the -shell-escape option is
901 passed to pdflatex.
903 The minted choice has possible repercussions on the preview of
904 latex fragments (see `org-preview-latex-fragment'). If you run
905 into previewing problems, please consult
907 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
908 :group 'org-export-latex
909 :type '(choice
910 (const :tag "Use listings" t)
911 (const :tag "Use minted" minted)
912 (const :tag "Export verbatim" nil))
913 :safe (lambda (s) (memq s '(t nil minted))))
915 (defcustom org-latex-listings-langs
916 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
917 (c "C") (cc "C++")
918 (fortran "fortran")
919 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
920 (html "HTML") (xml "XML")
921 (tex "TeX") (latex "[LaTeX]TeX")
922 (shell-script "bash")
923 (gnuplot "Gnuplot")
924 (ocaml "Caml") (caml "Caml")
925 (sql "SQL") (sqlite "sql")
926 (makefile "make"))
927 "Alist mapping languages to their listing language counterpart.
928 The key is a symbol, the major mode symbol without the \"-mode\".
929 The value is the string that should be inserted as the language
930 parameter for the listings package. If the mode name and the
931 listings name are the same, the language does not need an entry
932 in this list - but it does not hurt if it is present."
933 :group 'org-export-latex
934 :version "24.4"
935 :package-version '(Org . "8.3")
936 :type '(repeat
937 (list
938 (symbol :tag "Major mode ")
939 (string :tag "Listings language"))))
941 (defcustom org-latex-listings-options nil
942 "Association list of options for the latex listings package.
944 These options are supplied as a comma-separated list to the
945 \\lstset command. Each element of the association list should be
946 a list containing two strings: the name of the option, and the
947 value. For example,
949 \(setq org-latex-listings-options
950 '((\"basicstyle\" \"\\\\small\")
951 \(\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
953 will typeset the code in a small size font with underlined, bold
954 black keywords.
956 Note that the same options will be applied to blocks of all
957 languages. If you need block-specific options, you may use the
958 following syntax:
960 #+ATTR_LATEX: :options key1=value1,key2=value2
961 #+BEGIN_SRC <LANG>
963 #+END_SRC"
964 :group 'org-export-latex
965 :type '(repeat
966 (list
967 (string :tag "Listings option name ")
968 (string :tag "Listings option value"))))
970 (defcustom org-latex-minted-langs
971 '((emacs-lisp "common-lisp")
972 (cc "c++")
973 (cperl "perl")
974 (shell-script "bash")
975 (caml "ocaml"))
976 "Alist mapping languages to their minted language counterpart.
977 The key is a symbol, the major mode symbol without the \"-mode\".
978 The value is the string that should be inserted as the language
979 parameter for the minted package. If the mode name and the
980 listings name are the same, the language does not need an entry
981 in this list - but it does not hurt if it is present.
983 Note that minted uses all lower case for language identifiers,
984 and that the full list of language identifiers can be obtained
985 with:
987 pygmentize -L lexers"
988 :group 'org-export-latex
989 :type '(repeat
990 (list
991 (symbol :tag "Major mode ")
992 (string :tag "Minted language"))))
994 (defcustom org-latex-minted-options nil
995 "Association list of options for the latex minted package.
997 These options are supplied within square brackets in
998 \\begin{minted} environments. Each element of the alist should
999 be a list containing two strings: the name of the option, and the
1000 value. For example,
1002 \(setq org-latex-minted-options
1003 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
1005 will result in src blocks being exported with
1007 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
1009 as the start of the minted environment. Note that the same
1010 options will be applied to blocks of all languages. If you need
1011 block-specific options, you may use the following syntax:
1013 #+ATTR_LATEX: :options key1=value1,key2=value2
1014 #+BEGIN_SRC <LANG>
1016 #+END_SRC"
1017 :group 'org-export-latex
1018 :type '(repeat
1019 (list
1020 (string :tag "Minted option name ")
1021 (string :tag "Minted option value"))))
1023 (defvar org-latex-custom-lang-environments nil
1024 "Alist mapping languages to language-specific LaTeX environments.
1026 It is used during export of src blocks by the listings and minted
1027 latex packages. For example,
1029 \(setq org-latex-custom-lang-environments
1030 '\(\(python \"pythoncode\"\)\)\)
1032 would have the effect that if org encounters begin_src python
1033 during latex export it will output
1035 \\begin{pythoncode}
1036 <src block body>
1037 \\end{pythoncode}")
1040 ;;;; Compilation
1042 (defcustom org-latex-pdf-process
1043 '("pdflatex -interaction nonstopmode -output-directory %o %f"
1044 "pdflatex -interaction nonstopmode -output-directory %o %f"
1045 "pdflatex -interaction nonstopmode -output-directory %o %f")
1046 "Commands to process a LaTeX file to a PDF file.
1047 This is a list of strings, each of them will be given to the
1048 shell as a command. %f in the command will be replaced by the
1049 full file name, %b by the file base name (i.e. without directory
1050 and extension parts) and %o by the base directory of the file.
1052 The reason why this is a list is that it usually takes several
1053 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
1054 does not have a clever mechanism to detect which of these
1055 commands have to be run to get to a stable result, and it also
1056 does not do any error checking.
1058 By default, Org uses 3 runs of `pdflatex' to do the processing.
1059 If you have texi2dvi on your system and if that does not cause
1060 the infamous egrep/locale bug:
1062 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
1064 then `texi2dvi' is the superior choice as it automates the LaTeX
1065 build process by calling the \"correct\" combinations of
1066 auxiliary programs. Org does offer `texi2dvi' as one of the
1067 customize options. Alternatively, `rubber' and `latexmk' also
1068 provide similar functionality. The latter supports `biber' out
1069 of the box.
1071 Alternatively, this may be a Lisp function that does the
1072 processing, so you could use this to apply the machinery of
1073 AUCTeX or the Emacs LaTeX mode. This function should accept the
1074 file name as its single argument."
1075 :group 'org-export-pdf
1076 :type '(choice
1077 (repeat :tag "Shell command sequence"
1078 (string :tag "Shell command"))
1079 (const :tag "2 runs of pdflatex"
1080 ("pdflatex -interaction nonstopmode -output-directory %o %f"
1081 "pdflatex -interaction nonstopmode -output-directory %o %f"))
1082 (const :tag "3 runs of pdflatex"
1083 ("pdflatex -interaction nonstopmode -output-directory %o %f"
1084 "pdflatex -interaction nonstopmode -output-directory %o %f"
1085 "pdflatex -interaction nonstopmode -output-directory %o %f"))
1086 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
1087 ("pdflatex -interaction nonstopmode -output-directory %o %f"
1088 "bibtex %b"
1089 "pdflatex -interaction nonstopmode -output-directory %o %f"
1090 "pdflatex -interaction nonstopmode -output-directory %o %f"))
1091 (const :tag "2 runs of xelatex"
1092 ("xelatex -interaction nonstopmode -output-directory %o %f"
1093 "xelatex -interaction nonstopmode -output-directory %o %f"))
1094 (const :tag "3 runs of xelatex"
1095 ("xelatex -interaction nonstopmode -output-directory %o %f"
1096 "xelatex -interaction nonstopmode -output-directory %o %f"
1097 "xelatex -interaction nonstopmode -output-directory %o %f"))
1098 (const :tag "xelatex,bibtex,xelatex,xelatex"
1099 ("xelatex -interaction nonstopmode -output-directory %o %f"
1100 "bibtex %b"
1101 "xelatex -interaction nonstopmode -output-directory %o %f"
1102 "xelatex -interaction nonstopmode -output-directory %o %f"))
1103 (const :tag "texi2dvi"
1104 ("texi2dvi -p -b -V %f"))
1105 (const :tag "rubber"
1106 ("rubber -d --into %o %f"))
1107 (const :tag "latexmk"
1108 ("latexmk -g -pdf %f"))
1109 (function)))
1111 (defcustom org-latex-logfiles-extensions
1112 '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
1113 "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
1114 "The list of file extensions to consider as LaTeX logfiles.
1115 The logfiles will be removed if `org-latex-remove-logfiles' is
1116 non-nil."
1117 :group 'org-export-latex
1118 :version "25.1"
1119 :package-version '(Org . "8.3")
1120 :type '(repeat (string :tag "Extension")))
1122 (defcustom org-latex-remove-logfiles t
1123 "Non-nil means remove the logfiles produced by PDF production.
1124 By default, logfiles are files with these extensions: .aux, .idx,
1125 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
1126 logfiles to remove, set `org-latex-logfiles-extensions'."
1127 :group 'org-export-latex
1128 :type 'boolean)
1130 (defcustom org-latex-known-warnings
1131 '(("Reference.*?undefined" . "[undefined reference]")
1132 ("Runaway argument" . "[runaway argument]")
1133 ("Underfull \\hbox" . "[underfull hbox]")
1134 ("Overfull \\hbox" . "[overfull hbox]")
1135 ("Citation.*?undefined" . "[undefined citation]")
1136 ("Undefined control sequence" . "[undefined control sequence]"))
1137 "Alist of regular expressions and associated messages for the user.
1138 The regular expressions are used to find possible warnings in the
1139 log of a latex-run. These warnings will be reported after
1140 calling `org-latex-compile'."
1141 :group 'org-export-latex
1142 :version "25.1"
1143 :package-version '(Org . "8.3")
1144 :type '(repeat
1145 (cons
1146 (string :tag "Regexp")
1147 (string :tag "Message"))))
1151 ;;; Internal Functions
1153 (defun org-latex--caption-above-p (element info)
1154 "Non nil when caption is expected to be located above ELEMENT.
1155 INFO is a plist holding contextual information."
1156 (let ((above (plist-get info :latex-caption-above)))
1157 (if (symbolp above) above
1158 (let ((type (org-element-type element)))
1159 (memq (if (eq type 'link) 'image type) above)))))
1161 (defun org-latex--label (datum info &optional force full)
1162 "Return an appropriate label for DATUM.
1163 DATUM is an element or a `target' type object. INFO is the
1164 current export state, as a plist.
1166 Return nil if element DATUM has no NAME or VALUE affiliated
1167 keyword or no CUSTOM_ID property, unless FORCE is non-nil. In
1168 this case always return a unique label.
1170 Eventually, if FULL is non-nil, wrap label within \"\\label{}\"."
1171 (let* ((type (org-element-type datum))
1172 (user-label
1173 (org-element-property
1174 (case type
1175 ((headline inlinetask) :CUSTOM_ID)
1176 (target :value)
1177 (otherwise :name))
1178 datum))
1179 (label
1180 (and (or user-label force)
1181 (if (and user-label (plist-get info :latex-prefer-user-labels))
1182 user-label
1183 (concat (case type
1184 (headline "sec:")
1185 (table "tab:")
1186 (latex-environment
1187 (and (org-string-match-p
1188 org-latex-math-environments-re
1189 (org-element-property :value datum))
1190 "eq:"))
1191 (paragraph
1192 (and (org-element-property :caption datum)
1193 "fig:")))
1194 (org-export-get-reference datum info))))))
1195 (cond ((not full) label)
1196 (label (format "\\label{%s}%s"
1197 label
1198 (if (eq type 'target) "" "\n")))
1199 (t ""))))
1201 (defun org-latex--caption/label-string (element info)
1202 "Return caption and label LaTeX string for ELEMENT.
1204 INFO is a plist holding contextual information. If there's no
1205 caption nor label, return the empty string.
1207 For non-floats, see `org-latex--wrap-label'."
1208 (let* ((label (org-latex--label element info nil t))
1209 (main (org-export-get-caption element))
1210 (attr (org-export-read-attribute :attr_latex element))
1211 (type (org-element-type element))
1212 (nonfloat (or (and (plist-member attr :float)
1213 (not (plist-get attr :float))
1214 main)
1215 (and (eq type 'src-block)
1216 (not (plist-get attr :float))
1217 (memq (plist-get info :latex-listings)
1218 '(nil minted)))))
1219 (short (org-export-get-caption element t))
1220 (caption-from-attr-latex (plist-get attr :caption)))
1221 (cond
1222 ((org-string-nw-p caption-from-attr-latex)
1223 (concat caption-from-attr-latex "\n"))
1224 ((and (not main) (equal label "")) "")
1225 ((not main) (concat label "\n"))
1226 ;; Option caption format with short name.
1228 (format (if nonfloat "\\captionof{%s}%s{%s%s}\n"
1229 "\\caption%s%s{%s%s}\n")
1230 (if nonfloat
1231 (case type
1232 (paragraph "figure")
1233 (src-block (if (plist-get info :latex-listings)
1234 "listing"
1235 "figure"))
1236 (t (symbol-name type)))
1238 (if short (format "[%s]" (org-export-data short info)) "")
1239 label
1240 (org-export-data main info))))))
1242 (defun org-latex-guess-inputenc (header)
1243 "Set the coding system in inputenc to what the buffer is.
1245 HEADER is the LaTeX header string. This function only applies
1246 when specified inputenc option is \"AUTO\".
1248 Return the new header, as a string."
1249 (let* ((cs (or (ignore-errors
1250 (latexenc-coding-system-to-inputenc
1251 (or org-export-coding-system buffer-file-coding-system)))
1252 "utf8")))
1253 (if (not cs) header
1254 ;; First translate if that is requested.
1255 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
1256 ;; Then find the \usepackage statement and replace the option.
1257 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1258 cs header t nil 1))))
1260 (defun org-latex-guess-babel-language (header info)
1261 "Set Babel's language according to LANGUAGE keyword.
1263 HEADER is the LaTeX header string. INFO is the plist used as
1264 a communication channel.
1266 Insertion of guessed language only happens when Babel package has
1267 explicitly been loaded. Then it is added to the rest of
1268 package's options.
1270 The argument to Babel may be \"AUTO\" which is then replaced with
1271 the language of the document or `org-export-default-language'
1272 unless language in question is already loaded.
1274 Return the new header."
1275 (let ((language-code (plist-get info :language)))
1276 ;; If no language is set or Babel package is not loaded, return
1277 ;; HEADER as-is.
1278 (if (or (not (stringp language-code))
1279 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
1280 header
1281 (let ((options (save-match-data
1282 (org-split-string (match-string 1 header) ",[ \t]*")))
1283 (language (cdr (assoc language-code
1284 org-latex-babel-language-alist))))
1285 ;; If LANGUAGE is already loaded, return header without AUTO.
1286 ;; Otherwise, replace AUTO with language or append language if
1287 ;; AUTO is not present.
1288 (replace-match
1289 (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
1290 (cond ((member language options) (delete "AUTO" options))
1291 ((member "AUTO" options) options)
1292 (t (append options (list language))))
1293 ", ")
1294 t nil header 1)))))
1296 (defun org-latex-guess-polyglossia-language (header info)
1297 "Set the Polyglossia language according to the LANGUAGE keyword.
1299 HEADER is the LaTeX header string. INFO is the plist used as
1300 a communication channel.
1302 Insertion of guessed language only happens when the Polyglossia
1303 package has been explicitly loaded.
1305 The argument to Polyglossia may be \"AUTO\" which is then
1306 replaced with the language of the document or
1307 `org-export-default-language'. Note, the language is really set
1308 using \setdefaultlanguage and not as an option to the package.
1310 Return the new header."
1311 (let ((language (plist-get info :language)))
1312 ;; If no language is set or Polyglossia is not loaded, return
1313 ;; HEADER as-is.
1314 (if (or (not (stringp language))
1315 (not (string-match
1316 "\\\\usepackage\\(?:\\[\\([^]]+?\\)\\]\\){polyglossia}\n"
1317 header)))
1318 header
1319 (let* ((options (org-string-nw-p (match-string 1 header)))
1320 (languages (and options
1321 ;; Reverse as the last loaded language is
1322 ;; the main language.
1323 (nreverse
1324 (delete-dups
1325 (save-match-data
1326 (org-split-string
1327 (replace-regexp-in-string
1328 "AUTO" language options t)
1329 ",[ \t]*"))))))
1330 (main-language-set
1331 (string-match-p "\\\\setmainlanguage{.*?}" header)))
1332 (replace-match
1333 (concat "\\usepackage{polyglossia}\n"
1334 (mapconcat
1335 (lambda (l)
1336 (let ((l (or (assoc l org-latex-polyglossia-language-alist)
1337 l)))
1338 (format (if main-language-set "\\setotherlanguage%s{%s}\n"
1339 (setq main-language-set t)
1340 "\\setmainlanguage%s{%s}\n")
1341 (if (and (consp l) (= (length l) 3))
1342 (format "[variant=%s]" (nth 2 l))
1344 (nth 1 l))))
1345 languages
1346 ""))
1347 t t header 0)))))
1349 (defun org-latex--find-verb-separator (s)
1350 "Return a character not used in string S.
1351 This is used to choose a separator for constructs like \\verb."
1352 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1353 (loop for c across ll
1354 when (not (string-match (regexp-quote (char-to-string c)) s))
1355 return (char-to-string c))))
1357 (defun org-latex--make-option-string (options)
1358 "Return a comma separated string of keywords and values.
1359 OPTIONS is an alist where the key is the options keyword as
1360 a string, and the value a list containing the keyword value, or
1361 nil."
1362 (mapconcat (lambda (pair)
1363 (concat (first pair)
1364 (when (> (length (second pair)) 0)
1365 (concat "=" (second pair)))))
1366 options
1367 ","))
1369 (defun org-latex--wrap-label (element output info)
1370 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1371 INFO is the current export state, as a plist. This function
1372 should not be used for floats. See
1373 `org-latex--caption/label-string'."
1374 (if (not (and (org-string-nw-p output) (org-element-property :name element)))
1375 output
1376 (concat (format "\\phantomsection\n\\label{%s}\n"
1377 (org-latex--label element info))
1378 output)))
1380 (defun org-latex--protect-text (text)
1381 "Protect special characters in string TEXT and return it."
1382 (replace-regexp-in-string
1383 "--\\|[\\{}$%&_#~^]"
1384 (lambda (m)
1385 (cond ((equal m "--") "-{}-")
1386 ((equal m "\\") "\\textbackslash{}")
1387 ((equal m "~") "\\textasciitilde{}")
1388 ((equal m "^") "\\textasciicircum{}")
1389 (t (concat "\\" m))))
1390 text nil t))
1392 (defun org-latex--text-markup (text markup info)
1393 "Format TEXT depending on MARKUP text markup.
1394 INFO is a plist used as a communication channel. See
1395 `org-latex-text-markup-alist' for details."
1396 (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
1397 (case fmt
1398 ;; No format string: Return raw text.
1399 ((nil) text)
1400 ;; Handle the `verb' special case: Find an appropriate separator
1401 ;; and use "\\verb" command.
1402 (verb
1403 (let ((separator (org-latex--find-verb-separator text)))
1404 (concat "\\verb" separator
1405 (replace-regexp-in-string "\n" " " text)
1406 separator)))
1407 ;; Handle the `protectedtexttt' special case: Protect some
1408 ;; special chars and use "\texttt{%s}" format string.
1409 (protectedtexttt
1410 (format "\\texttt{%s}" (org-latex--protect-text text)))
1411 ;; Else use format string.
1412 (t (format fmt text)))))
1414 (defun org-latex--delayed-footnotes-definitions (element info)
1415 "Return footnotes definitions in ELEMENT as a string.
1417 INFO is a plist used as a communication channel.
1419 Footnotes definitions are returned within \"\\footnotetxt{}\"
1420 commands.
1422 This function is used within constructs that don't support
1423 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1424 \"\\footnotemark\" is used within the construct and the function
1425 just outside of it."
1426 (mapconcat
1427 (lambda (ref)
1428 (format
1429 "\\footnotetext[%s]{%s}"
1430 (org-export-get-footnote-number ref info)
1431 (org-trim
1432 (org-export-data
1433 (org-export-get-footnote-definition ref info) info))))
1434 ;; Find every footnote reference in ELEMENT.
1435 (let* (all-refs
1436 search-refs ; For byte-compiler.
1437 (search-refs
1438 (function
1439 (lambda (data)
1440 ;; Return a list of all footnote references never seen
1441 ;; before in DATA.
1442 (org-element-map data 'footnote-reference
1443 (lambda (ref)
1444 (when (org-export-footnote-first-reference-p ref info)
1445 (push ref all-refs)
1446 (when (eq (org-element-property :type ref) 'standard)
1447 (funcall search-refs
1448 (org-export-get-footnote-definition ref info)))))
1449 info)
1450 (reverse all-refs)))))
1451 (funcall search-refs element))
1452 ""))
1454 (defun org-latex--translate (s info)
1455 "Translate string S according to specified language.
1456 INFO is a plist used as a communication channel."
1457 (org-export-translate s :latex info))
1459 (defun org-latex--format-spec (info)
1460 "Create a format-spec for document meta-data.
1461 INFO is a plist used as a communication channel."
1462 (let ((language (let ((lang (plist-get info :language)))
1463 (or (cdr (assoc lang org-latex-babel-language-alist))
1464 lang))))
1465 `((?a . ,(org-export-data (plist-get info :author) info))
1466 (?t . ,(org-export-data (plist-get info :title) info))
1467 (?k . ,(org-export-data (org-latex--wrap-latex-math-block
1468 (plist-get info :keywords) info)
1469 info))
1470 (?d . ,(org-export-data (org-latex--wrap-latex-math-block
1471 (plist-get info :description) info)
1472 info))
1473 (?c . ,(plist-get info :creator))
1474 (?l . ,language)
1475 (?L . ,(capitalize language))
1476 (?D . ,(org-export-get-date info)))))
1478 (defun org-latex--make-header (info)
1479 "Return a formatted LaTeX header.
1480 INFO is a plist used as a communication channel."
1481 (let* ((class (plist-get info :latex-class))
1482 (class-options (plist-get info :latex-class-options))
1483 (header (nth 1 (assoc class (plist-get info :latex-classes))))
1484 (document-class-string
1485 (and (stringp header)
1486 (if (not class-options) header
1487 (replace-regexp-in-string
1488 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1489 class-options header t nil 1)))))
1490 (if (not document-class-string)
1491 (user-error "Unknown LaTeX class `%s'" class)
1492 (org-latex-guess-polyglossia-language
1493 (org-latex-guess-babel-language
1494 (org-latex-guess-inputenc
1495 (org-element-normalize-string
1496 (org-splice-latex-header
1497 document-class-string
1498 org-latex-default-packages-alist
1499 org-latex-packages-alist nil
1500 (concat (org-element-normalize-string
1501 (plist-get info :latex-header))
1502 (plist-get info :latex-header-extra)))))
1503 info)
1504 info))))
1507 ;;; Template
1509 (defun org-latex-template (contents info)
1510 "Return complete document string after LaTeX conversion.
1511 CONTENTS is the transcoded contents string. INFO is a plist
1512 holding export options."
1513 (let ((title (org-export-data (plist-get info :title) info))
1514 (spec (org-latex--format-spec info)))
1515 (concat
1516 ;; Time-stamp.
1517 (and (plist-get info :time-stamp-file)
1518 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1519 ;; Document class and packages.
1520 (org-latex--make-header info)
1521 ;; Possibly limit depth for headline numbering.
1522 (let ((sec-num (plist-get info :section-numbers)))
1523 (when (integerp sec-num)
1524 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1525 ;; Author.
1526 (let ((author (and (plist-get info :with-author)
1527 (let ((auth (plist-get info :author)))
1528 (and auth (org-export-data auth info)))))
1529 (email (and (plist-get info :with-email)
1530 (org-export-data (plist-get info :email) info))))
1531 (cond ((and author email (not (string= "" email)))
1532 (format "\\author{%s\\thanks{%s}}\n" author email))
1533 ((or author email) (format "\\author{%s}\n" (or author email)))))
1534 ;; Date.
1535 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1536 (format "\\date{%s}\n" (org-export-data date info)))
1537 ;; Title and subtitle.
1538 (let* ((subtitle (plist-get info :subtitle))
1539 (formatted-subtitle
1540 (when subtitle
1541 (format (plist-get info :latex-subtitle-format)
1542 (org-export-data subtitle info))))
1543 (separate (plist-get info :latex-subtitle-separate)))
1544 (concat
1545 (format "\\title{%s%s}\n" title
1546 (if separate "" (or formatted-subtitle "")))
1547 (when (and separate subtitle)
1548 (concat formatted-subtitle "\n"))))
1549 ;; Hyperref options.
1550 (let ((template (plist-get info :latex-hyperref-template)))
1551 (and (stringp template)
1552 (format-spec template spec)))
1553 ;; Document start.
1554 "\\begin{document}\n\n"
1555 ;; Title command.
1556 (let* ((title-command (plist-get info :latex-title-command))
1557 (command (and (stringp title-command)
1558 (format-spec title-command spec))))
1559 (org-element-normalize-string
1560 (cond ((not (plist-get info :with-title)) nil)
1561 ((string= "" title) nil)
1562 ((not (stringp command)) nil)
1563 ((string-match "\\(?:[^%]\\|^\\)%s" command)
1564 (format command title))
1565 (t command))))
1566 ;; Table of contents.
1567 (let ((depth (plist-get info :with-toc)))
1568 (when depth
1569 (concat (when (wholenump depth)
1570 (format "\\setcounter{tocdepth}{%d}\n" depth))
1571 (plist-get info :latex-toc-command))))
1572 ;; Document's body.
1573 contents
1574 ;; Creator.
1575 (and (plist-get info :with-creator)
1576 (concat (plist-get info :creator) "\n"))
1577 ;; Document end.
1578 "\\end{document}")))
1582 ;;; Transcode Functions
1584 ;;;; Bold
1586 (defun org-latex-bold (bold contents info)
1587 "Transcode BOLD from Org to LaTeX.
1588 CONTENTS is the text with bold markup. INFO is a plist holding
1589 contextual information."
1590 (org-latex--text-markup contents 'bold info))
1593 ;;;; Center Block
1595 (defun org-latex-center-block (center-block contents info)
1596 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1597 CONTENTS holds the contents of the center block. INFO is a plist
1598 holding contextual information."
1599 (org-latex--wrap-label
1600 center-block (format "\\begin{center}\n%s\\end{center}" contents) info))
1603 ;;;; Clock
1605 (defun org-latex-clock (clock contents info)
1606 "Transcode a CLOCK element from Org to LaTeX.
1607 CONTENTS is nil. INFO is a plist holding contextual
1608 information."
1609 (concat
1610 "\\noindent"
1611 (format "\\textbf{%s} " org-clock-string)
1612 (format (plist-get info :latex-inactive-timestamp-format)
1613 (concat (org-timestamp-translate (org-element-property :value clock))
1614 (let ((time (org-element-property :duration clock)))
1615 (and time (format " (%s)" time)))))
1616 "\\\\"))
1619 ;;;; Code
1621 (defun org-latex-code (code contents info)
1622 "Transcode a CODE object from Org to LaTeX.
1623 CONTENTS is nil. INFO is a plist used as a communication
1624 channel."
1625 (org-latex--text-markup (org-element-property :value code) 'code info))
1628 ;;;; Drawer
1630 (defun org-latex-drawer (drawer contents info)
1631 "Transcode a DRAWER element from Org to LaTeX.
1632 CONTENTS holds the contents of the block. INFO is a plist
1633 holding contextual information."
1634 (let* ((name (org-element-property :drawer-name drawer))
1635 (output (funcall (plist-get info :latex-format-drawer-function)
1636 name contents)))
1637 (org-latex--wrap-label drawer output info)))
1640 ;;;; Dynamic Block
1642 (defun org-latex-dynamic-block (dynamic-block contents info)
1643 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1644 CONTENTS holds the contents of the block. INFO is a plist
1645 holding contextual information. See `org-export-data'."
1646 (org-latex--wrap-label dynamic-block contents info))
1649 ;;;; Entity
1651 (defun org-latex-entity (entity contents info)
1652 "Transcode an ENTITY object from Org to LaTeX.
1653 CONTENTS are the definition itself. INFO is a plist holding
1654 contextual information."
1655 (org-element-property :latex entity))
1658 ;;;; Example Block
1660 (defun org-latex-example-block (example-block contents info)
1661 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1662 CONTENTS is nil. INFO is a plist holding contextual
1663 information."
1664 (when (org-string-nw-p (org-element-property :value example-block))
1665 (let ((environment (or (org-export-read-attribute
1666 :attr_latex example-block :environment)
1667 "verbatim")))
1668 (org-latex--wrap-label
1669 example-block
1670 (format "\\begin{%s}\n%s\\end{%s}"
1671 environment
1672 (org-export-format-code-default example-block info)
1673 environment)
1674 info))))
1677 ;;;; Export Block
1679 (defun org-latex-export-block (export-block contents info)
1680 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1681 CONTENTS is nil. INFO is a plist holding contextual information."
1682 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1683 (org-remove-indentation (org-element-property :value export-block))))
1686 ;;;; Export Snippet
1688 (defun org-latex-export-snippet (export-snippet contents info)
1689 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1690 CONTENTS is nil. INFO is a plist holding contextual information."
1691 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1692 (org-element-property :value export-snippet)))
1695 ;;;; Fixed Width
1697 (defun org-latex-fixed-width (fixed-width contents info)
1698 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1699 CONTENTS is nil. INFO is a plist holding contextual information."
1700 (org-latex--wrap-label
1701 fixed-width
1702 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1703 (org-remove-indentation
1704 (org-element-property :value fixed-width)))
1705 info))
1708 ;;;; Footnote Reference
1710 (defun org-latex-footnote-reference (footnote-reference contents info)
1711 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1712 CONTENTS is nil. INFO is a plist holding contextual information."
1713 (concat
1714 ;; Insert separator between two footnotes in a row.
1715 (let ((prev (org-export-get-previous-element footnote-reference info)))
1716 (when (eq (org-element-type prev) 'footnote-reference)
1717 (plist-get info :latex-footnote-separator)))
1718 (cond
1719 ;; Use \footnotemark if the footnote has already been defined.
1720 ((not (org-export-footnote-first-reference-p footnote-reference info))
1721 (format "\\footnotemark[%s]{}"
1722 (org-export-get-footnote-number footnote-reference info)))
1723 ;; Use \footnotemark if reference is within another footnote
1724 ;; reference, footnote definition or table cell.
1725 ((org-element-lineage footnote-reference
1726 '(footnote-reference footnote-definition table-cell))
1727 "\\footnotemark")
1728 ;; Otherwise, define it with \footnote command.
1730 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1731 (concat
1732 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1733 ;; Retrieve all footnote references within the footnote and
1734 ;; add their definition after it, since LaTeX doesn't support
1735 ;; them inside.
1736 (org-latex--delayed-footnotes-definitions def info)))))))
1739 ;;;; Headline
1741 (defun org-latex-headline (headline contents info)
1742 "Transcode a HEADLINE element from Org to LaTeX.
1743 CONTENTS holds the contents of the headline. INFO is a plist
1744 holding contextual information."
1745 (unless (org-element-property :footnote-section-p headline)
1746 (let* ((class (plist-get info :latex-class))
1747 (level (org-export-get-relative-level headline info))
1748 (numberedp (org-export-numbered-headline-p headline info))
1749 (class-sectioning (assoc class (plist-get info :latex-classes)))
1750 ;; Section formatting will set two placeholders: one for
1751 ;; the title and the other for the contents.
1752 (section-fmt
1753 (let ((sec (if (functionp (nth 2 class-sectioning))
1754 (funcall (nth 2 class-sectioning) level numberedp)
1755 (nth (1+ level) class-sectioning))))
1756 (cond
1757 ;; No section available for that LEVEL.
1758 ((not sec) nil)
1759 ;; Section format directly returned by a function. Add
1760 ;; placeholder for contents.
1761 ((stringp sec) (concat sec "\n%s"))
1762 ;; (numbered-section . unnumbered-section)
1763 ((not (consp (cdr sec)))
1764 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1765 ;; (numbered-open numbered-close)
1766 ((= (length sec) 2)
1767 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1768 ;; (num-in num-out no-num-in no-num-out)
1769 ((= (length sec) 4)
1770 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1771 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1772 ;; Create a temporary export back-end that hard-codes
1773 ;; "\underline" within "\section" and alike.
1774 (section-back-end
1775 (org-export-create-backend
1776 :parent 'latex
1777 :transcoders
1778 '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
1779 (text
1780 (org-export-data-with-backend
1781 (org-element-property :title headline) section-back-end info))
1782 (todo
1783 (and (plist-get info :with-todo-keywords)
1784 (let ((todo (org-element-property :todo-keyword headline)))
1785 (and todo (org-export-data todo info)))))
1786 (todo-type (and todo (org-element-property :todo-type headline)))
1787 (tags (and (plist-get info :with-tags)
1788 (org-export-get-tags headline info)))
1789 (priority (and (plist-get info :with-priority)
1790 (org-element-property :priority headline)))
1791 ;; Create the headline text along with a no-tag version.
1792 ;; The latter is required to remove tags from toc.
1793 (full-text (funcall (plist-get info :latex-format-headline-function)
1794 todo todo-type priority text tags info))
1795 ;; Associate \label to the headline for internal links.
1796 (headline-label (org-latex--label headline info t t))
1797 (pre-blanks
1798 (make-string (org-element-property :pre-blank headline) ?\n)))
1799 (if (or (not section-fmt) (org-export-low-level-p headline info))
1800 ;; This is a deep sub-tree: export it as a list item. Also
1801 ;; export as items headlines for which no section format has
1802 ;; been found.
1803 (let ((low-level-body
1804 (concat
1805 ;; If headline is the first sibling, start a list.
1806 (when (org-export-first-sibling-p headline info)
1807 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1808 ;; Itemize headline
1809 "\\item"
1810 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
1811 "\\relax")
1812 " " full-text "\n"
1813 headline-label
1814 pre-blanks
1815 contents)))
1816 ;; If headline is not the last sibling simply return
1817 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1818 ;; any blank line.
1819 (if (not (org-export-last-sibling-p headline info)) low-level-body
1820 (replace-regexp-in-string
1821 "[ \t\n]*\\'"
1822 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1823 low-level-body)))
1824 ;; This is a standard headline. Export it as a section. Add
1825 ;; an alternative heading when possible, and when this is not
1826 ;; identical to the usual heading.
1827 (let ((opt-title
1828 (funcall (plist-get info :latex-format-headline-function)
1829 todo todo-type priority
1830 (org-export-data-with-backend
1831 (org-export-get-alt-title headline info)
1832 section-back-end info)
1833 (and (eq (plist-get info :with-tags) t) tags)
1834 info))
1835 ;; Maybe end local TOC (see `org-latex-keyword').
1836 (contents
1837 (concat
1838 contents
1839 (let ((case-fold-search t)
1840 (section
1841 (let ((first (car (org-element-contents headline))))
1842 (and (eq (org-element-type first) 'section) first))))
1843 (org-element-map section 'keyword
1844 (lambda (k)
1845 (and (equal (org-element-property :key k) "TOC")
1846 (let ((v (org-element-property :value k)))
1847 (and (org-string-match-p "\\<headlines\\>" v)
1848 (org-string-match-p "\\<local\\>" v)
1849 (format "\\stopcontents[level-%d]" level)))))
1850 info t)))))
1851 (if (and opt-title
1852 (not (equal opt-title full-text))
1853 (string-match "\\`\\\\\\(.+?\\){" section-fmt))
1854 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1855 ;; Replace square brackets with parenthesis
1856 ;; since square brackets are not supported in
1857 ;; optional arguments.
1858 (replace-regexp-in-string
1859 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1860 full-text
1861 (concat headline-label pre-blanks contents))
1862 ;; Impossible to add an alternative heading. Fallback to
1863 ;; regular sectioning format string.
1864 (format section-fmt full-text
1865 (concat headline-label pre-blanks contents))))))))
1867 (defun org-latex-format-headline-default-function
1868 (todo todo-type priority text tags info)
1869 "Default format function for a headline.
1870 See `org-latex-format-headline-function' for details."
1871 (concat
1872 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1873 (and priority (format "\\framebox{\\#%c} " priority))
1874 text
1875 (and tags
1876 (format "\\hfill{}\\textsc{%s}"
1877 (mapconcat (lambda (tag) (org-latex-plain-text tag info))
1878 tags ":")))))
1881 ;;;; Horizontal Rule
1883 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1884 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1885 CONTENTS is nil. INFO is a plist holding contextual information."
1886 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1887 (prev (org-export-get-previous-element horizontal-rule info)))
1888 (concat
1889 ;; Make sure the rule doesn't start at the end of the current
1890 ;; line by separating it with a blank line from previous element.
1891 (when (and prev
1892 (let ((prev-blank (org-element-property :post-blank prev)))
1893 (or (not prev-blank) (zerop prev-blank))))
1894 "\n")
1895 (org-latex--wrap-label
1896 horizontal-rule
1897 (format "\\rule{%s}{%s}"
1898 (or (plist-get attr :width) "\\linewidth")
1899 (or (plist-get attr :thickness) "0.5pt"))
1900 info))))
1903 ;;;; Inline Src Block
1905 (defun org-latex-inline-src-block (inline-src-block contents info)
1906 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1907 CONTENTS holds the contents of the item. INFO is a plist holding
1908 contextual information."
1909 (let* ((code (org-element-property :value inline-src-block))
1910 (separator (org-latex--find-verb-separator code)))
1911 (case (plist-get info :latex-listings)
1912 ;; Do not use a special package: transcode it verbatim.
1913 ((nil) (format "\\texttt{%s}" (org-latex--protect-text code)))
1914 ;; Use minted package.
1915 (minted
1916 (let* ((org-lang (org-element-property :language inline-src-block))
1917 (mint-lang (or (cadr (assq (intern org-lang)
1918 (plist-get info :latex-minted-langs)))
1919 (downcase org-lang)))
1920 (options (org-latex--make-option-string
1921 (plist-get info :latex-minted-options))))
1922 (concat (format "\\mint%s{%s}"
1923 (if (string= options "") "" (format "[%s]" options))
1924 mint-lang)
1925 separator code separator)))
1926 ;; Use listings package.
1927 (otherwise
1928 ;; Maybe translate language's name.
1929 (let* ((org-lang (org-element-property :language inline-src-block))
1930 (lst-lang (or (cadr (assq (intern org-lang)
1931 (plist-get info :latex-listings-langs)))
1932 org-lang))
1933 (options (org-latex--make-option-string
1934 (append (plist-get info :latex-listings-options)
1935 `(("language" ,lst-lang))))))
1936 (concat (format "\\lstinline[%s]" options)
1937 separator code separator))))))
1940 ;;;; Inlinetask
1942 (defun org-latex-inlinetask (inlinetask contents info)
1943 "Transcode an INLINETASK element from Org to LaTeX.
1944 CONTENTS holds the contents of the block. INFO is a plist
1945 holding contextual information."
1946 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1947 (todo (and (plist-get info :with-todo-keywords)
1948 (let ((todo (org-element-property :todo-keyword inlinetask)))
1949 (and todo (org-export-data todo info)))))
1950 (todo-type (org-element-property :todo-type inlinetask))
1951 (tags (and (plist-get info :with-tags)
1952 (org-export-get-tags inlinetask info)))
1953 (priority (and (plist-get info :with-priority)
1954 (org-element-property :priority inlinetask)))
1955 (contents (concat (org-latex--label inlinetask info) contents)))
1956 (funcall (plist-get info :latex-format-inlinetask-function)
1957 todo todo-type priority title tags contents info)))
1959 (defun org-latex-format-inlinetask-default-function
1960 (todo todo-type priority title tags contents info)
1961 "Default format function for a inlinetasks.
1962 See `org-latex-format-inlinetask-function' for details."
1963 (let ((full-title
1964 (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1965 (when priority (format "\\framebox{\\#%c} " priority))
1966 title
1967 (when tags
1968 (format "\\hfill{}\\textsc{:%s:}"
1969 (mapconcat
1970 (lambda (tag) (org-latex-plain-text tag info))
1971 tags ":"))))))
1972 (concat "\\begin{center}\n"
1973 "\\fbox{\n"
1974 "\\begin{minipage}[c]{.6\\textwidth}\n"
1975 full-title "\n\n"
1976 (and (org-string-nw-p contents)
1977 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
1978 "\\end{minipage}\n"
1979 "}\n"
1980 "\\end{center}")))
1983 ;;;; Italic
1985 (defun org-latex-italic (italic contents info)
1986 "Transcode ITALIC from Org to LaTeX.
1987 CONTENTS is the text with italic markup. INFO is a plist holding
1988 contextual information."
1989 (org-latex--text-markup contents 'italic info))
1992 ;;;; Item
1994 (defun org-latex-item (item contents info)
1995 "Transcode an ITEM element from Org to LaTeX.
1996 CONTENTS holds the contents of the item. INFO is a plist holding
1997 contextual information."
1998 (let* ((counter
1999 (let ((count (org-element-property :counter item))
2000 (level
2001 ;; Determine level of current item to determine the
2002 ;; correct LaTeX counter to use (enumi, enumii...).
2003 (let ((parent item) (level 0))
2004 (while (memq (org-element-type
2005 (setq parent (org-export-get-parent parent)))
2006 '(plain-list item))
2007 (when (and (eq (org-element-type parent) 'plain-list)
2008 (eq (org-element-property :type parent)
2009 'ordered))
2010 (incf level)))
2011 level)))
2012 (and count
2013 (< level 5)
2014 (format "\\setcounter{enum%s}{%s}\n"
2015 (nth (1- level) '("i" "ii" "iii" "iv"))
2016 (1- count)))))
2017 (checkbox (case (org-element-property :checkbox item)
2018 (on "$\\boxtimes$ ")
2019 (off "$\\square$ ")
2020 (trans "$\\boxminus$ ")))
2021 (tag (let ((tag (org-element-property :tag item)))
2022 ;; Check-boxes must belong to the tag.
2023 (and tag (format "[{%s}] "
2024 (concat checkbox
2025 (org-export-data tag info)))))))
2026 (concat counter
2027 "\\item"
2028 (cond
2029 (tag)
2030 (checkbox (concat " " checkbox))
2031 ;; Without a tag or a check-box, if CONTENTS starts with
2032 ;; an opening square bracket, add "\relax" to "\item",
2033 ;; unless the brackets comes from an initial export
2034 ;; snippet (i.e. it is inserted willingly by the user).
2035 ((and contents
2036 (org-string-match-p "\\`[ \t]*\\[" contents)
2037 (not (let ((e (car (org-element-contents item))))
2038 (and (eq (org-element-type e) 'paragraph)
2039 (let ((o (car (org-element-contents e))))
2040 (and (eq (org-element-type o) 'export-snippet)
2041 (eq (org-export-snippet-backend o)
2042 'latex)))))))
2043 "\\relax ")
2044 (t " "))
2045 (and contents (org-trim contents))
2046 ;; If there are footnotes references in tag, be sure to
2047 ;; add their definition at the end of the item. This
2048 ;; workaround is necessary since "\footnote{}" command is
2049 ;; not supported in tags.
2050 (and tag
2051 (org-latex--delayed-footnotes-definitions
2052 (org-element-property :tag item) info)))))
2055 ;;;; Keyword
2057 (defun org-latex-keyword (keyword contents info)
2058 "Transcode a KEYWORD element from Org to LaTeX.
2059 CONTENTS is nil. INFO is a plist holding contextual information."
2060 (let ((key (org-element-property :key keyword))
2061 (value (org-element-property :value keyword)))
2062 (cond
2063 ((string= key "LATEX") value)
2064 ((string= key "INDEX") (format "\\index{%s}" value))
2065 ((string= key "TOC")
2066 (let ((case-fold-search t))
2067 (cond
2068 ((org-string-match-p "\\<headlines\\>" value)
2069 (let* ((localp (org-string-match-p "\\<local\\>" value))
2070 (parent (org-element-lineage keyword '(headline)))
2071 (level (if (not (and localp parent)) 0
2072 (org-export-get-relative-level parent info)))
2073 (depth
2074 (and (string-match "\\<[0-9]+\\>" value)
2075 (format
2076 "\\setcounter{tocdepth}{%d}"
2077 (+ (string-to-number (match-string 0 value)) level)))))
2078 (if (and localp parent)
2079 ;; Start local TOC, assuming package "titletoc" is
2080 ;; required.
2081 (format "\\startcontents[level-%d]
2082 \\printcontents[level-%d]{}{0}{%s}"
2083 level level (or depth ""))
2084 (concat depth (and depth "\n") "\\tableofcontents"))))
2085 ((org-string-match-p "\\<tables\\>" value) "\\listoftables")
2086 ((org-string-match-p "\\<listings\\>" value)
2087 (case (plist-get info :latex-listings)
2088 ((nil) "\\listoffigures")
2089 (minted "\\listoflistings")
2090 (otherwise "\\lstlistoflistings")))))))))
2093 ;;;; Latex Environment
2095 (defun org-latex-latex-environment (latex-environment contents info)
2096 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
2097 CONTENTS is nil. INFO is a plist holding contextual information."
2098 (when (plist-get info :with-latex)
2099 (let ((value (org-remove-indentation
2100 (org-element-property :value latex-environment))))
2101 (if (not (org-element-property :name latex-environment)) value
2102 ;; Environment is labeled: label must be within the environment
2103 ;; (otherwise, a reference pointing to that element will count
2104 ;; the section instead).
2105 (with-temp-buffer
2106 (insert value)
2107 (goto-char (point-min))
2108 (forward-line)
2109 (insert (org-latex--label latex-environment info nil t))
2110 (buffer-string))))))
2113 ;;;; Latex Fragment
2115 (defun org-latex-latex-fragment (latex-fragment contents info)
2116 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
2117 CONTENTS is nil. INFO is a plist holding contextual information."
2118 (let ((value (org-element-property :value latex-fragment)))
2119 ;; Trim math markers since the fragment is enclosed within
2120 ;; a latex-math-block object anyway.
2121 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
2122 (match-string 2 value))
2123 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
2124 (match-string 1 value))
2125 (t value))))
2128 ;;;; Line Break
2130 (defun org-latex-line-break (line-break contents info)
2131 "Transcode a LINE-BREAK object from Org to LaTeX.
2132 CONTENTS is nil. INFO is a plist holding contextual information."
2133 "\\\\\n")
2136 ;;;; Link
2138 (defun org-latex--inline-image (link info)
2139 "Return LaTeX code for an inline image.
2140 LINK is the link pointing to the inline image. INFO is a plist
2141 used as a communication channel."
2142 (let* ((parent (org-export-get-parent-element link))
2143 (path (let ((raw-path (org-element-property :path link)))
2144 (if (not (file-name-absolute-p raw-path)) raw-path
2145 (expand-file-name raw-path))))
2146 (filetype (file-name-extension path))
2147 (caption (org-latex--caption/label-string parent info))
2148 (caption-above-p (org-latex--caption-above-p link info))
2149 ;; Retrieve latex attributes from the element around.
2150 (attr (org-export-read-attribute :attr_latex parent))
2151 (float (let ((float (plist-get attr :float)))
2152 (cond ((string= float "wrap") 'wrap)
2153 ((string= float "sideways") 'sideways)
2154 ((string= float "multicolumn") 'multicolumn)
2155 ((or float
2156 (org-element-property :caption parent)
2157 (org-string-nw-p (plist-get attr :caption)))
2158 (if (and (plist-member attr :float) (not float))
2159 'nonfloat
2160 'figure))
2161 ((and (not float) (plist-member attr :float)) nil))))
2162 (placement
2163 (let ((place (plist-get attr :placement)))
2164 (cond
2165 (place (format "%s" place))
2166 ((eq float 'wrap) "{l}{0.5\\textwidth}")
2167 ((eq float 'figure)
2168 (format "[%s]" (plist-get info :latex-default-figure-position)))
2169 (t ""))))
2170 (comment-include (if (plist-get attr :comment-include) "%" ""))
2171 ;; It is possible to specify width and height in the
2172 ;; ATTR_LATEX line, and also via default variables.
2173 (width (cond ((plist-get attr :width))
2174 ((plist-get attr :height) "")
2175 ((eq float 'wrap) "0.48\\textwidth")
2176 (t (plist-get info :latex-image-default-width))))
2177 (height (cond ((plist-get attr :height))
2178 ((or (plist-get attr :width)
2179 (memq float '(figure wrap))) "")
2180 (t (plist-get info :latex-image-default-height))))
2181 (options (let ((opt (or (plist-get attr :options)
2182 (plist-get info :latex-image-default-option))))
2183 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
2184 (match-string 1 opt))))
2185 image-code)
2186 (if (member filetype '("tikz" "pgf"))
2187 ;; For tikz images:
2188 ;; - use \input to read in image file.
2189 ;; - if options are present, wrap in a tikzpicture environment.
2190 ;; - if width or height are present, use \resizebox to change
2191 ;; the image size.
2192 (progn
2193 (setq image-code (format "\\input{%s}" path))
2194 (when (org-string-nw-p options)
2195 (setq image-code
2196 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
2197 options
2198 image-code)))
2199 (when (or (org-string-nw-p width) (org-string-nw-p height))
2200 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
2201 (if (org-string-nw-p width) width "!")
2202 (if (org-string-nw-p height) height "!")
2203 image-code))))
2204 ;; For other images:
2205 ;; - add width and height to options.
2206 ;; - include the image with \includegraphics.
2207 (when (org-string-nw-p width)
2208 (setq options (concat options ",width=" width)))
2209 (when (org-string-nw-p height)
2210 (setq options (concat options ",height=" height)))
2211 (let ((search-option (org-element-property :search-option link)))
2212 (when (and search-option
2213 (equal filetype "pdf")
2214 (org-string-match-p "\\`[0-9]+\\'" search-option)
2215 (not (org-string-match-p "page=" options)))
2216 (setq options (concat options ",page=" search-option))))
2217 (setq image-code
2218 (format "\\includegraphics%s{%s}"
2219 (cond ((not (org-string-nw-p options)) "")
2220 ((= (aref options 0) ?,)
2221 (format "[%s]"(substring options 1)))
2222 (t (format "[%s]" options)))
2223 path))
2224 (when (equal filetype "svg")
2225 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
2226 "\\includesvg"
2227 image-code
2228 nil t))
2229 (setq image-code (replace-regexp-in-string "\\.svg}"
2231 image-code
2232 nil t))))
2233 ;; Return proper string, depending on FLOAT.
2234 (case float
2235 (wrap (format "\\begin{wrapfigure}%s
2236 %s\\centering
2237 %s%s
2238 %s\\end{wrapfigure}"
2239 placement
2240 (if caption-above-p caption "")
2241 comment-include image-code
2242 (if caption-above-p "" caption)))
2243 (sideways (format "\\begin{sidewaysfigure}
2244 %s\\centering
2245 %s%s
2246 %s\\end{sidewaysfigure}"
2247 (if caption-above-p caption "")
2248 comment-include image-code
2249 (if caption-above-p "" caption)))
2250 (multicolumn (format "\\begin{figure*}%s
2251 %s\\centering
2252 %s%s
2253 %s\\end{figure*}"
2254 placement
2255 (if caption-above-p caption "")
2256 comment-include image-code
2257 (if caption-above-p "" caption)))
2258 (figure (format "\\begin{figure}%s
2259 %s\\centering
2260 %s%s
2261 %s\\end{figure}"
2262 placement
2263 (if caption-above-p caption "")
2264 comment-include image-code
2265 (if caption-above-p "" caption)))
2266 (nonfloat
2267 (format "\\begin{center}
2268 %s%s
2269 %s\\end{center}"
2270 (if caption-above-p caption "")
2271 image-code
2272 (if caption-above-p "" caption)))
2273 (otherwise image-code))))
2275 (defun org-latex-link (link desc info)
2276 "Transcode a LINK object from Org to LaTeX.
2278 DESC is the description part of the link, or the empty string.
2279 INFO is a plist holding contextual information. See
2280 `org-export-data'."
2281 (let* ((type (org-element-property :type link))
2282 (raw-path (replace-regexp-in-string
2283 "%" "\\%" (org-element-property :path link) nil t))
2284 ;; Ensure DESC really exists, or set it to nil.
2285 (desc (and (not (string= desc "")) desc))
2286 (imagep (org-export-inline-image-p
2287 link (plist-get info :latex-inline-image-rules)))
2288 (path (cond
2289 ((member type '("http" "https" "ftp" "mailto" "doi"))
2290 (concat type ":" raw-path))
2291 ((string= type "file") (org-export-file-uri raw-path))
2292 (t raw-path))))
2293 (cond
2294 ;; Link type is handled by a special function.
2295 ((org-export-custom-protocol-maybe link desc 'latex))
2296 ;; Image file.
2297 (imagep (org-latex--inline-image link info))
2298 ;; Radio link: Transcode target's contents and use them as link's
2299 ;; description.
2300 ((string= type "radio")
2301 (let ((destination (org-export-resolve-radio-link link info)))
2302 (if (not destination) desc
2303 (format "\\hyperref[%s]{%s}"
2304 (org-export-get-reference destination info)
2305 desc))))
2306 ;; Links pointing to a headline: Find destination and build
2307 ;; appropriate referencing command.
2308 ((member type '("custom-id" "fuzzy" "id"))
2309 (let ((destination (if (string= type "fuzzy")
2310 (org-export-resolve-fuzzy-link link info)
2311 (org-export-resolve-id-link link info))))
2312 (case (org-element-type destination)
2313 ;; Id link points to an external file.
2314 (plain-text
2315 (if desc (format "\\href{%s}{%s}" destination desc)
2316 (format "\\url{%s}" destination)))
2317 ;; Fuzzy link points nowhere.
2318 ((nil)
2319 (format (plist-get info :latex-link-with-unknown-path-format)
2320 (or desc
2321 (org-export-data
2322 (org-element-property :raw-link link) info))))
2323 ;; LINK points to a headline. If headlines are numbered
2324 ;; and the link has no description, display headline's
2325 ;; number. Otherwise, display description or headline's
2326 ;; title.
2327 (headline
2328 (let ((label (org-latex--label destination info t)))
2329 (if (and (not desc)
2330 (org-export-numbered-headline-p destination info))
2331 (format "\\ref{%s}" label)
2332 (format "\\hyperref[%s]{%s}" label
2333 (or desc
2334 (org-export-data
2335 (org-element-property :title destination) info))))))
2336 ;; Fuzzy link points to a target. Do as above.
2337 (otherwise
2338 (let ((ref (org-latex--label destination info t)))
2339 (if (not desc) (format "\\ref{%s}" ref)
2340 (format "\\hyperref[%s]{%s}" ref desc)))))))
2341 ;; Coderef: replace link with the reference name or the
2342 ;; equivalent line number.
2343 ((string= type "coderef")
2344 (format (org-export-get-coderef-format path desc)
2345 (org-export-resolve-coderef path info)))
2346 ;; External link with a description part.
2347 ((and path desc) (format "\\href{%s}{%s}" path desc))
2348 ;; External link without a description part.
2349 (path (format "\\url{%s}" path))
2350 ;; No path, only description. Try to do something useful.
2351 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
2354 ;;;; Node Property
2356 (defun org-latex-node-property (node-property contents info)
2357 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2358 CONTENTS is nil. INFO is a plist holding contextual
2359 information."
2360 (format "%s:%s"
2361 (org-element-property :key node-property)
2362 (let ((value (org-element-property :value node-property)))
2363 (if value (concat " " value) ""))))
2366 ;;;; Paragraph
2368 (defun org-latex-paragraph (paragraph contents info)
2369 "Transcode a PARAGRAPH element from Org to LaTeX.
2370 CONTENTS is the contents of the paragraph, as a string. INFO is
2371 the plist used as a communication channel."
2372 contents)
2375 ;;;; Plain List
2377 (defun org-latex-plain-list (plain-list contents info)
2378 "Transcode a PLAIN-LIST element from Org to LaTeX.
2379 CONTENTS is the contents of the list. INFO is a plist holding
2380 contextual information."
2381 (let* ((type (org-element-property :type plain-list))
2382 (attr (org-export-read-attribute :attr_latex plain-list))
2383 (latex-type (let ((env (plist-get attr :environment)))
2384 (cond (env (format "%s" env))
2385 ((eq type 'ordered) "enumerate")
2386 ((eq type 'descriptive) "description")
2387 (t "itemize")))))
2388 (org-latex--wrap-label
2389 plain-list
2390 (format "\\begin{%s}%s\n%s\\end{%s}"
2391 latex-type
2392 (or (plist-get attr :options) "")
2393 contents
2394 latex-type)
2395 info)))
2398 ;;;; Plain Text
2400 (defun org-latex-plain-text (text info)
2401 "Transcode a TEXT string from Org to LaTeX.
2402 TEXT is the string to transcode. INFO is a plist holding
2403 contextual information."
2404 (let* ((specialp (plist-get info :with-special-strings))
2405 (output
2406 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2407 (let ((case-fold-search nil))
2408 (replace-regexp-in-string
2409 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2410 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2411 ;; However, if special strings are used, be careful not
2412 ;; to protect "\" in "\-" constructs.
2413 (replace-regexp-in-string
2414 (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\([^-]\\|$\\)"))
2415 (lambda (m)
2416 (case (string-to-char m)
2417 (?\\ "$\\\\backslash$\\1")
2418 (?~ "\\\\textasciitilde{}")
2419 (?^ "\\\\^{}")
2420 (t "\\\\\\&")))
2421 text)))))
2422 ;; Activate smart quotes. Be sure to provide original TEXT string
2423 ;; since OUTPUT may have been modified.
2424 (when (plist-get info :with-smart-quotes)
2425 (setq output (org-export-activate-smart-quotes output :latex info text)))
2426 ;; Convert special strings.
2427 (when specialp
2428 (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
2429 ;; Handle break preservation if required.
2430 (when (plist-get info :preserve-breaks)
2431 (setq output (replace-regexp-in-string
2432 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
2433 ;; Return value.
2434 output))
2437 ;;;; Planning
2439 (defun org-latex-planning (planning contents info)
2440 "Transcode a PLANNING element from Org to LaTeX.
2441 CONTENTS is nil. INFO is a plist holding contextual
2442 information."
2443 (concat
2444 "\\noindent"
2445 (mapconcat
2446 'identity
2447 (delq nil
2448 (list
2449 (let ((closed (org-element-property :closed planning)))
2450 (when closed
2451 (concat
2452 (format "\\textbf{%s} " org-closed-string)
2453 (format (plist-get info :latex-inactive-timestamp-format)
2454 (org-timestamp-translate closed)))))
2455 (let ((deadline (org-element-property :deadline planning)))
2456 (when deadline
2457 (concat
2458 (format "\\textbf{%s} " org-deadline-string)
2459 (format (plist-get info :latex-active-timestamp-format)
2460 (org-timestamp-translate deadline)))))
2461 (let ((scheduled (org-element-property :scheduled planning)))
2462 (when scheduled
2463 (concat
2464 (format "\\textbf{%s} " org-scheduled-string)
2465 (format (plist-get info :latex-active-timestamp-format)
2466 (org-timestamp-translate scheduled)))))))
2467 " ")
2468 "\\\\"))
2471 ;;;; Property Drawer
2473 (defun org-latex-property-drawer (property-drawer contents info)
2474 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2475 CONTENTS holds the contents of the drawer. INFO is a plist
2476 holding contextual information."
2477 (and (org-string-nw-p contents)
2478 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2481 ;;;; Pseudo Element: LaTeX Matrices
2483 ;; `latex-matrices' elements have the following properties:
2484 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2485 ;; `math').
2487 (defun org-latex--wrap-latex-matrices (data info)
2488 "Merge contiguous tables with the same mode within a pseudo-element.
2489 DATA is a parse tree or a secondary string. INFO is a plist
2490 containing export options. Modify DATA by side-effect and return
2491 it."
2492 (org-element-map data 'table
2493 (lambda (table)
2494 (when (eq (org-element-property :type table) 'org)
2495 (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
2496 (plist-get info :latex-default-table-mode))))
2497 (when (and (member mode '("inline-math" "math"))
2498 ;; Do not wrap twice the same table.
2499 (not (eq (org-element-type
2500 (org-element-property :parent table))
2501 'latex-matrices)))
2502 (let* ((caption (and (not (string= mode "inline-math"))
2503 (org-element-property :caption table)))
2504 (matrices
2505 (list 'latex-matrices
2506 (list :caption caption
2507 :markup
2508 (cond ((string= mode "inline-math") 'inline)
2509 (caption 'equation)
2510 (t 'math)))))
2511 (previous table)
2512 (next (org-export-get-next-element table info)))
2513 (org-element-insert-before matrices table)
2514 ;; Swallow all contiguous tables sharing the same mode.
2515 (while (and
2516 (zerop (or (org-element-property :post-blank previous) 0))
2517 (setq next (org-export-get-next-element previous info))
2518 (eq (org-element-type next) 'table)
2519 (eq (org-element-property :type next) 'org)
2520 (string= (or (org-export-read-attribute
2521 :attr_latex next :mode)
2522 (plist-get info :latex-default-table-mode))
2523 mode))
2524 (org-element-extract-element previous)
2525 (org-element-adopt-elements matrices previous)
2526 (setq previous next))
2527 (org-element-put-property
2528 matrices :post-blank (org-element-property :post-blank previous))
2529 (org-element-extract-element previous)
2530 (org-element-adopt-elements matrices previous))))))
2531 info)
2532 data)
2534 (defun org-latex-matrices (matrices contents info)
2535 "Transcode a MATRICES element from Org to LaTeX.
2536 CONTENTS is a string. INFO is a plist used as a communication
2537 channel."
2538 (format (case (org-element-property :markup matrices)
2539 (inline "\\(%s\\)")
2540 (equation "\\begin{equation}\n%s\\end{equation}")
2541 (t "\\[\n%s\\]"))
2542 contents))
2544 (defun org-latex-matrices-tree-filter (tree backend info)
2545 (org-latex--wrap-latex-matrices tree info))
2547 ;;;; Pseudo Object: LaTeX Math Block
2549 ;; `latex-math-block' objects have the following property:
2550 ;; `:post-blank'.
2552 (defun org-latex--wrap-latex-math-block (data info)
2553 "Merge contiguous math objects in a pseudo-object container.
2554 DATA is a parse tree or a secondary string. INFO is a plist
2555 containing export options. Modify DATA by side-effect and return it."
2556 (let ((valid-object-p
2557 (function
2558 ;; Non-nil when OBJ can be added to the latex math block.
2559 (lambda (obj)
2560 (case (org-element-type obj)
2561 (entity (org-element-property :latex-math-p obj))
2562 (latex-fragment
2563 (let ((value (org-element-property :value obj)))
2564 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2565 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2566 ((subscript superscript) t))))))
2567 (org-element-map data '(entity latex-fragment subscript superscript)
2568 (lambda (object)
2569 ;; Skip objects already wrapped.
2570 (when (and (not (eq (org-element-type
2571 (org-element-property :parent object))
2572 'latex-math-block))
2573 (funcall valid-object-p object))
2574 (let ((math-block (list 'latex-math-block nil))
2575 (next-elements (org-export-get-next-element object info t))
2576 (last object))
2577 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2578 (org-element-insert-before math-block object)
2579 (org-element-extract-element object)
2580 (org-element-adopt-elements math-block object)
2581 (when (zerop (or (org-element-property :post-blank object) 0))
2582 ;; MATH-BLOCK swallows consecutive math objects.
2583 (catch 'exit
2584 (dolist (next next-elements)
2585 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2586 (org-element-extract-element next)
2587 (org-element-adopt-elements math-block next)
2588 ;; Eschew the case: \beta$x$ -> \(\betax\).
2589 (unless (memq (org-element-type next)
2590 '(subscript superscript))
2591 (org-element-put-property last :post-blank 1))
2592 (setq last next)
2593 (when (> (or (org-element-property :post-blank next) 0) 0)
2594 (throw 'exit nil))))))
2595 (org-element-put-property
2596 math-block :post-blank (org-element-property :post-blank last)))))
2597 info nil '(subscript superscript latex-math-block) t)
2598 ;; Return updated DATA.
2599 data))
2601 (defun org-latex-math-block-tree-filter (tree backend info)
2602 (org-latex--wrap-latex-math-block tree info))
2604 (defun org-latex-math-block-options-filter (info backend)
2605 (dolist (prop '(:author :date :title) info)
2606 (plist-put info prop
2607 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2609 (defun org-latex-math-block (math-block contents info)
2610 "Transcode a MATH-BLOCK object from Org to LaTeX.
2611 CONTENTS is a string. INFO is a plist used as a communication
2612 channel."
2613 (when (org-string-nw-p contents)
2614 (format "\\(%s\\)" (org-trim contents))))
2616 ;;;; Quote Block
2618 (defun org-latex-quote-block (quote-block contents info)
2619 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2620 CONTENTS holds the contents of the block. INFO is a plist
2621 holding contextual information."
2622 (org-latex--wrap-label
2623 quote-block (format "\\begin{quote}\n%s\\end{quote}" contents) info))
2626 ;;;; Radio Target
2628 (defun org-latex-radio-target (radio-target text info)
2629 "Transcode a RADIO-TARGET object from Org to LaTeX.
2630 TEXT is the text of the target. INFO is a plist holding
2631 contextual information."
2632 (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
2635 ;;;; Section
2637 (defun org-latex-section (section contents info)
2638 "Transcode a SECTION element from Org to LaTeX.
2639 CONTENTS holds the contents of the section. INFO is a plist
2640 holding contextual information."
2641 contents)
2644 ;;;; Special Block
2646 (defun org-latex-special-block (special-block contents info)
2647 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2648 CONTENTS holds the contents of the block. INFO is a plist
2649 holding contextual information."
2650 (let ((type (org-element-property :type special-block))
2651 (opt (org-export-read-attribute :attr_latex special-block :options))
2652 (caption (org-latex--caption/label-string special-block info))
2653 (caption-above-p (org-latex--caption-above-p special-block info)))
2654 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2655 (and caption-above-p caption)
2656 contents
2657 (and (not caption-above-p) caption)
2658 (format "\\end{%s}" type))))
2661 ;;;; Src Block
2663 (defun org-latex-src-block (src-block contents info)
2664 "Transcode a SRC-BLOCK element from Org to LaTeX.
2665 CONTENTS holds the contents of the item. INFO is a plist holding
2666 contextual information."
2667 (when (org-string-nw-p (org-element-property :value src-block))
2668 (let* ((lang (org-element-property :language src-block))
2669 (caption (org-element-property :caption src-block))
2670 (caption-above-p (org-latex--caption-above-p src-block info))
2671 (label (org-element-property :name src-block))
2672 (custom-env (and lang
2673 (cadr (assq (intern lang)
2674 org-latex-custom-lang-environments))))
2675 (num-start (case (org-element-property :number-lines src-block)
2676 (continued (org-export-get-loc src-block info))
2677 (new 0)))
2678 (retain-labels (org-element-property :retain-labels src-block))
2679 (attributes (org-export-read-attribute :attr_latex src-block))
2680 (float (plist-get attributes :float))
2681 (listings (plist-get info :latex-listings)))
2682 (cond
2683 ;; Case 1. No source fontification.
2684 ((not listings)
2685 (let* ((caption-str (org-latex--caption/label-string src-block info))
2686 (float-env
2687 (cond ((string= "multicolumn" float)
2688 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2689 (plist-get info :latex-default-figure-position)
2690 (if caption-above-p caption-str "")
2691 (if caption-above-p "" caption-str)))
2692 (caption (concat
2693 (if caption-above-p caption-str "")
2694 "%s"
2695 (if caption-above-p "" (concat "\n" caption-str))))
2696 (t "%s"))))
2697 (format
2698 float-env
2699 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2700 (org-export-format-code-default src-block info))))))
2701 ;; Case 2. Custom environment.
2702 (custom-env
2703 (let ((caption-str (org-latex--caption/label-string src-block info)))
2704 (format "\\begin{%s}\n%s\\end{%s}\n"
2705 custom-env
2706 (concat (and caption-above-p caption-str)
2707 (org-export-format-code-default src-block info)
2708 (and (not caption-above-p) caption-str))
2709 custom-env)))
2710 ;; Case 3. Use minted package.
2711 ((eq listings 'minted)
2712 (let* ((caption-str (org-latex--caption/label-string src-block info))
2713 (float-env
2714 (cond
2715 ((string= "multicolumn" float)
2716 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2717 (if caption-above-p caption-str "")
2718 (if caption-above-p "" caption-str)))
2719 (caption
2720 (concat (if caption-above-p caption-str "")
2721 "%s"
2722 (if caption-above-p "" (concat "\n" caption-str))))
2723 (t "%s")))
2724 (options (plist-get info :latex-minted-options))
2725 (body
2726 (format
2727 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2728 ;; Options.
2729 (concat
2730 (org-latex--make-option-string
2731 (if (or (not num-start) (assoc "linenos" options))
2732 options
2733 (append
2734 `(("linenos")
2735 ("firstnumber" ,(number-to-string (1+ num-start))))
2736 options)))
2737 (let ((local-options (plist-get attributes :options)))
2738 (and local-options (concat "," local-options))))
2739 ;; Language.
2740 (or (cadr (assq (intern lang)
2741 (plist-get info :latex-minted-langs)))
2742 (downcase lang))
2743 ;; Source code.
2744 (let* ((code-info (org-export-unravel-code src-block))
2745 (max-width
2746 (apply 'max
2747 (mapcar 'length
2748 (org-split-string (car code-info)
2749 "\n")))))
2750 (org-export-format-code
2751 (car code-info)
2752 (lambda (loc num ref)
2753 (concat
2755 (when ref
2756 ;; Ensure references are flushed to the right,
2757 ;; separated with 6 spaces from the widest line
2758 ;; of code.
2759 (concat (make-string (+ (- max-width (length loc)) 6)
2760 ?\s)
2761 (format "(%s)" ref)))))
2762 nil (and retain-labels (cdr code-info)))))))
2763 ;; Return value.
2764 (format float-env body)))
2765 ;; Case 4. Use listings package.
2767 (let ((lst-lang
2768 (or (cadr (assq (intern lang)
2769 (plist-get info :latex-listings-langs)))
2770 lang))
2771 (caption-str
2772 (when caption
2773 (let ((main (org-export-get-caption src-block))
2774 (secondary (org-export-get-caption src-block t)))
2775 (if (not secondary)
2776 (format "{%s}" (org-export-data main info))
2777 (format "{[%s]%s}"
2778 (org-export-data secondary info)
2779 (org-export-data main info))))))
2780 (lst-opt (plist-get info :latex-listings-options)))
2781 (concat
2782 ;; Options.
2783 (format
2784 "\\lstset{%s}\n"
2785 (concat
2786 (org-latex--make-option-string
2787 (append
2788 lst-opt
2789 (cond
2790 ((and (not float) (plist-member attributes :float)) nil)
2791 ((string= "multicolumn" float) '(("float" "*")))
2792 ((and float (not (assoc "float" lst-opt)))
2793 `(("float" ,(plist-get info :latex-default-figure-position)))))
2794 `(("language" ,lst-lang))
2795 (if label `(("label" ,label)) '(("label" " ")))
2796 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2797 `(("captionpos" ,(if caption-above-p "t" "b")))
2798 (cond ((assoc "numbers" lst-opt) nil)
2799 ((not num-start) '(("numbers" "none")))
2800 ((zerop num-start) '(("numbers" "left")))
2801 (t `(("firstnumber" ,(number-to-string (1+ num-start)))
2802 ("numbers" "left"))))))
2803 (let ((local-options (plist-get attributes :options)))
2804 (and local-options (concat "," local-options)))))
2805 ;; Source code.
2806 (format
2807 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2808 (let* ((code-info (org-export-unravel-code src-block))
2809 (max-width
2810 (apply 'max
2811 (mapcar 'length
2812 (org-split-string (car code-info) "\n")))))
2813 (org-export-format-code
2814 (car code-info)
2815 (lambda (loc num ref)
2816 (concat
2818 (when ref
2819 ;; Ensure references are flushed to the right,
2820 ;; separated with 6 spaces from the widest line of
2821 ;; code
2822 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2823 (format "(%s)" ref)))))
2824 nil (and retain-labels (cdr code-info))))))))))))
2827 ;;;; Statistics Cookie
2829 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2830 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2831 CONTENTS is nil. INFO is a plist holding contextual information."
2832 (replace-regexp-in-string
2833 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2836 ;;;; Strike-Through
2838 (defun org-latex-strike-through (strike-through contents info)
2839 "Transcode STRIKE-THROUGH from Org to LaTeX.
2840 CONTENTS is the text with strike-through markup. INFO is a plist
2841 holding contextual information."
2842 (org-latex--text-markup contents 'strike-through info))
2845 ;;;; Subscript
2847 (defun org-latex--script-size (object info)
2848 "Transcode a subscript or superscript object.
2849 OBJECT is an Org object. INFO is a plist used as a communication
2850 channel."
2851 (let ((type (org-element-type object))
2852 (output ""))
2853 (org-element-map (org-element-contents object)
2854 (cons 'plain-text org-element-all-objects)
2855 (lambda (obj)
2856 (case (org-element-type obj)
2857 ((entity latex-fragment)
2858 (let ((data (org-trim (org-export-data obj info))))
2859 (string-match
2860 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2861 data)
2862 (setq output
2863 (concat output
2864 (match-string 1 data)
2865 (let ((blank (org-element-property :post-blank obj)))
2866 (and blank (> blank 0) "\\ "))))))
2867 (plain-text
2868 (setq output
2869 (format "%s\\text{%s}" output (org-export-data obj info))))
2870 (otherwise
2871 (setq output
2872 (concat output
2873 (org-export-data obj info)
2874 (let ((blank (org-element-property :post-blank obj)))
2875 (and blank (> blank 0) "\\ ")))))))
2876 info nil org-element-recursive-objects)
2877 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2878 ;; character.
2879 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2880 (and (> (length output) 1) "{")
2881 output
2882 (and (> (length output) 1) "}"))))
2884 (defun org-latex-subscript (subscript contents info)
2885 "Transcode a SUBSCRIPT object from Org to LaTeX.
2886 CONTENTS is the contents of the object. INFO is a plist holding
2887 contextual information."
2888 (org-latex--script-size subscript info))
2891 ;;;; Superscript
2893 (defun org-latex-superscript (superscript contents info)
2894 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2895 CONTENTS is the contents of the object. INFO is a plist holding
2896 contextual information."
2897 (org-latex--script-size superscript info))
2900 ;;;; Table
2902 ;; `org-latex-table' is the entry point for table transcoding. It
2903 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2904 ;; delegates the job to either `org-latex--table.el-table',
2905 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2906 ;; depending of the type of the table and the mode requested.
2908 ;; `org-latex--align-string' is a subroutine used to build alignment
2909 ;; string for Org tables.
2911 (defun org-latex-table (table contents info)
2912 "Transcode a TABLE element from Org to LaTeX.
2913 CONTENTS is the contents of the table. INFO is a plist holding
2914 contextual information."
2915 (if (eq (org-element-property :type table) 'table.el)
2916 ;; "table.el" table. Convert it using appropriate tools.
2917 (org-latex--table.el-table table info)
2918 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2919 (plist-get info :latex-default-table-mode))))
2920 (cond
2921 ;; Case 1: Verbatim table.
2922 ((string= type "verbatim")
2923 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2924 ;; Re-create table, without affiliated keywords.
2925 (org-trim (org-element-interpret-data
2926 `(table nil ,@(org-element-contents table))))))
2927 ;; Case 2: Matrix.
2928 ((or (string= type "math") (string= type "inline-math"))
2929 (org-latex--math-table table info))
2930 ;; Case 3: Standard table.
2931 (t (concat (org-latex--org-table table contents info)
2932 ;; When there are footnote references within the
2933 ;; table, insert their definition just after it.
2934 (org-latex--delayed-footnotes-definitions table info)))))))
2936 (defun org-latex--align-string (table info)
2937 "Return an appropriate LaTeX alignment string.
2938 TABLE is the considered table. INFO is a plist used as
2939 a communication channel."
2940 (or (org-export-read-attribute :attr_latex table :align)
2941 (let (align)
2942 ;; Extract column groups and alignment from first (non-rule)
2943 ;; row.
2944 (org-element-map
2945 (org-element-map table 'table-row
2946 (lambda (row)
2947 (and (eq (org-element-property :type row) 'standard) row))
2948 info 'first-match)
2949 'table-cell
2950 (lambda (cell)
2951 (let ((borders (org-export-table-cell-borders cell info)))
2952 ;; Check left border for the first cell only.
2953 (when (and (memq 'left borders) (not align))
2954 (push "|" align))
2955 (push (case (org-export-table-cell-alignment cell info)
2956 (left "l")
2957 (right "r")
2958 (center "c"))
2959 align)
2960 (when (memq 'right borders) (push "|" align))))
2961 info)
2962 (apply 'concat (nreverse align)))))
2964 (defun org-latex--org-table (table contents info)
2965 "Return appropriate LaTeX code for an Org table.
2967 TABLE is the table type element to transcode. CONTENTS is its
2968 contents, as a string. INFO is a plist used as a communication
2969 channel.
2971 This function assumes TABLE has `org' as its `:type' property and
2972 `table' as its `:mode' attribute."
2973 (let* ((caption (org-latex--caption/label-string table info))
2974 (attr (org-export-read-attribute :attr_latex table))
2975 ;; Determine alignment string.
2976 (alignment (org-latex--align-string table info))
2977 ;; Determine environment for the table: longtable, tabular...
2978 (table-env (or (plist-get attr :environment)
2979 (plist-get info :latex-default-table-environment)))
2980 ;; If table is a float, determine environment: table, table*
2981 ;; or sidewaystable.
2982 (float-env (unless (member table-env '("longtable" "longtabu"))
2983 (let ((float (plist-get attr :float)))
2984 (cond
2985 ((and (not float) (plist-member attr :float)) nil)
2986 ((or (string= float "sidewaystable")
2987 (string= float "sideways")) "sidewaystable")
2988 ((string= float "multicolumn") "table*")
2989 ((or float
2990 (org-element-property :caption table)
2991 (org-string-nw-p (plist-get attr :caption)))
2992 "table")))))
2993 ;; Extract others display options.
2994 (fontsize (let ((font (plist-get attr :font)))
2995 (and font (concat font "\n"))))
2996 ;; "tabular" environment doesn't allow to define a width.
2997 (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
2998 (spreadp (plist-get attr :spread))
2999 (placement
3000 (or (plist-get attr :placement)
3001 (format "[%s]" (plist-get info :latex-default-figure-position))))
3002 (centerp (if (plist-member attr :center) (plist-get attr :center)
3003 (plist-get info :latex-tables-centered)))
3004 (caption-above-p (org-latex--caption-above-p table info)))
3005 ;; Prepare the final format string for the table.
3006 (cond
3007 ;; Longtable.
3008 ((equal "longtable" table-env)
3009 (concat (and fontsize (concat "{" fontsize))
3010 (format "\\begin{longtable}{%s}\n" alignment)
3011 (and caption-above-p
3012 (org-string-nw-p caption)
3013 (concat caption "\\\\\n"))
3014 contents
3015 (and (not caption-above-p)
3016 (org-string-nw-p caption)
3017 (concat caption "\\\\\n"))
3018 "\\end{longtable}\n"
3019 (and fontsize "}")))
3020 ;; Longtabu
3021 ((equal "longtabu" table-env)
3022 (concat (and fontsize (concat "{" fontsize))
3023 (format "\\begin{longtabu}%s{%s}\n"
3024 (if width
3025 (format " %s %s "
3026 (if spreadp "spread" "to") width) "")
3027 alignment)
3028 (and caption-above-p
3029 (org-string-nw-p caption)
3030 (concat caption "\\\\\n"))
3031 contents
3032 (and (not caption-above-p)
3033 (org-string-nw-p caption)
3034 (concat caption "\\\\\n"))
3035 "\\end{longtabu}\n"
3036 (and fontsize "}")))
3037 ;; Others.
3038 (t (concat (cond
3039 (float-env
3040 (concat (format "\\begin{%s}%s\n" float-env placement)
3041 (if caption-above-p caption "")
3042 (when centerp "\\centering\n")
3043 fontsize))
3044 ((and (not float-env) caption)
3045 (concat
3046 (and centerp "\\begin{center}\n" )
3047 (if caption-above-p caption "")
3048 (cond ((and fontsize centerp) fontsize)
3049 (fontsize (concat "{" fontsize)))))
3050 (centerp (concat "\\begin{center}\n" fontsize))
3051 (fontsize (concat "{" fontsize)))
3052 (cond ((equal "tabu" table-env)
3053 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
3054 (if width (format
3055 (if spreadp " spread %s " " to %s ")
3056 width) "")
3057 alignment
3058 contents))
3059 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
3060 table-env
3061 (if width (format "{%s}" width) "")
3062 alignment
3063 contents
3064 table-env)))
3065 (cond
3066 (float-env
3067 (concat (if caption-above-p "" (concat "\n" caption))
3068 (format "\n\\end{%s}" float-env)))
3069 ((and (not float-env) caption)
3070 (concat
3071 (if caption-above-p "" (concat "\n" caption))
3072 (and centerp "\n\\end{center}")
3073 (and fontsize (not centerp) "}")))
3074 (centerp "\n\\end{center}")
3075 (fontsize "}")))))))
3077 (defun org-latex--table.el-table (table info)
3078 "Return appropriate LaTeX code for a table.el table.
3080 TABLE is the table type element to transcode. INFO is a plist
3081 used as a communication channel.
3083 This function assumes TABLE has `table.el' as its `:type'
3084 property."
3085 (require 'table)
3086 ;; Ensure "*org-export-table*" buffer is empty.
3087 (with-current-buffer (get-buffer-create "*org-export-table*")
3088 (erase-buffer))
3089 (let ((output (with-temp-buffer
3090 (insert (org-element-property :value table))
3091 (goto-char 1)
3092 (re-search-forward "^[ \t]*|[^|]" nil t)
3093 (table-generate-source 'latex "*org-export-table*")
3094 (with-current-buffer "*org-export-table*"
3095 (org-trim (buffer-string))))))
3096 (kill-buffer (get-buffer "*org-export-table*"))
3097 ;; Remove left out comments.
3098 (while (string-match "^%.*\n" output)
3099 (setq output (replace-match "" t t output)))
3100 (let ((attr (org-export-read-attribute :attr_latex table)))
3101 (when (plist-get attr :rmlines)
3102 ;; When the "rmlines" attribute is provided, remove all hlines
3103 ;; but the the one separating heading from the table body.
3104 (let ((n 0) (pos 0))
3105 (while (and (< (length output) pos)
3106 (setq pos (string-match "^\\\\hline\n?" output pos)))
3107 (incf n)
3108 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
3109 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
3110 (plist-get info :latex-tables-centered))))
3111 (if (not centerp) output
3112 (format "\\begin{center}\n%s\n\\end{center}" output))))))
3114 (defun org-latex--math-table (table info)
3115 "Return appropriate LaTeX code for a matrix.
3117 TABLE is the table type element to transcode. INFO is a plist
3118 used as a communication channel.
3120 This function assumes TABLE has `org' as its `:type' property and
3121 `inline-math' or `math' as its `:mode' attribute."
3122 (let* ((attr (org-export-read-attribute :attr_latex table))
3123 (env (or (plist-get attr :environment)
3124 (plist-get info :latex-default-table-environment)))
3125 (contents
3126 (mapconcat
3127 (lambda (row)
3128 ;; Ignore horizontal rules.
3129 (when (eq (org-element-property :type row) 'standard)
3130 ;; Return each cell unmodified.
3131 (concat
3132 (mapconcat
3133 (lambda (cell)
3134 (substring (org-element-interpret-data cell) 0 -1))
3135 (org-element-map row 'table-cell #'identity info) "&")
3136 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
3137 "\n")))
3138 (org-element-map table 'table-row #'identity info) "")))
3139 (concat
3140 ;; Prefix.
3141 (plist-get attr :math-prefix)
3142 ;; Environment. Also treat special cases.
3143 (cond ((member env '("array" "tabular"))
3144 (let ((align (make-string
3145 (cdr (org-export-table-dimensions table info)) ?c)))
3146 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
3147 ((assoc env org-latex-table-matrix-macros)
3148 (format "\\%s%s{\n%s}"
3150 (or (plist-get attr :math-arguments) "")
3151 contents))
3152 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
3153 ;; Suffix.
3154 (plist-get attr :math-suffix))))
3157 ;;;; Table Cell
3159 (defun org-latex-table-cell (table-cell contents info)
3160 "Transcode a TABLE-CELL element from Org to LaTeX.
3161 CONTENTS is the cell contents. INFO is a plist used as
3162 a communication channel."
3163 (concat
3164 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
3165 (if (and contents
3166 scientific-format
3167 (string-match orgtbl-exp-regexp contents))
3168 ;; Use appropriate format string for scientific
3169 ;; notation.
3170 (format scientific-format
3171 (match-string 1 contents)
3172 (match-string 2 contents))
3173 contents))
3174 (when (org-export-get-next-element table-cell info) " & ")))
3177 ;;;; Table Row
3179 (defun org-latex-table-row (table-row contents info)
3180 "Transcode a TABLE-ROW element from Org to LaTeX.
3181 CONTENTS is the contents of the row. INFO is a plist used as
3182 a communication channel."
3183 (let* ((attr (org-export-read-attribute :attr_latex
3184 (org-export-get-parent table-row)))
3185 (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
3186 (plist-get info :latex-tables-booktabs)))
3187 (longtablep
3188 (member (or (plist-get attr :environment)
3189 (plist-get info :latex-default-table-environment))
3190 '("longtable" "longtabu"))))
3191 (if (eq (org-element-property :type table-row) 'rule)
3192 (cond
3193 ((not booktabsp) "\\hline")
3194 ((not (org-export-get-previous-element table-row info)) "\\toprule")
3195 ((not (org-export-get-next-element table-row info)) "\\bottomrule")
3196 ((and longtablep
3197 (org-export-table-row-ends-header-p
3198 (org-export-get-previous-element table-row info) info))
3200 (t "\\midrule"))
3201 (concat
3202 ;; When BOOKTABS are activated enforce top-rule even when no
3203 ;; hline was specifically marked.
3204 (and booktabsp (not (org-export-get-previous-element table-row info))
3205 "\\toprule\n")
3206 contents "\\\\\n"
3207 (cond
3208 ;; Special case for long tables. Define header and footers.
3209 ((and longtablep (org-export-table-row-ends-header-p table-row info))
3210 (let ((columns (cdr (org-export-table-dimensions
3211 (org-export-get-parent-table table-row) info))))
3212 (format "%s
3213 \\endfirsthead
3214 \\multicolumn{%d}{l}{%s} \\\\
3216 %s \\\\\n
3218 \\endhead
3219 %s\\multicolumn{%d}{r}{%s} \\\\
3220 \\endfoot
3221 \\endlastfoot"
3222 (if booktabsp "\\midrule" "\\hline")
3223 columns
3224 (org-latex--translate "Continued from previous page" info)
3225 (cond
3226 ((not (org-export-table-row-starts-header-p table-row info))
3228 (booktabsp "\\toprule\n")
3229 (t "\\hline\n"))
3230 contents
3231 (if booktabsp "\\midrule" "\\hline")
3232 (if booktabsp "\\midrule" "\\hline")
3233 columns
3234 (org-latex--translate "Continued on next page" info))))
3235 ;; When BOOKTABS are activated enforce bottom rule even when
3236 ;; no hline was specifically marked.
3237 ((and booktabsp (not (org-export-get-next-element table-row info)))
3238 "\\bottomrule"))))))
3241 ;;;; Target
3243 (defun org-latex-target (target contents info)
3244 "Transcode a TARGET object from Org to LaTeX.
3245 CONTENTS is nil. INFO is a plist holding contextual
3246 information."
3247 (format "\\label{%s}" (org-latex--label target info)))
3250 ;;;; Timestamp
3252 (defun org-latex-timestamp (timestamp contents info)
3253 "Transcode a TIMESTAMP object from Org to LaTeX.
3254 CONTENTS is nil. INFO is a plist holding contextual
3255 information."
3256 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
3257 (format
3258 (plist-get info
3259 (case (org-element-property :type timestamp)
3260 ((active active-range) :latex-active-timestamp-format)
3261 ((inactive inactive-range) :latex-inactive-timestamp-format)
3262 (otherwise :latex-diary-timestamp-format)))
3263 value)))
3266 ;;;; Underline
3268 (defun org-latex-underline (underline contents info)
3269 "Transcode UNDERLINE from Org to LaTeX.
3270 CONTENTS is the text with underline markup. INFO is a plist
3271 holding contextual information."
3272 (org-latex--text-markup contents 'underline info))
3275 ;;;; Verbatim
3277 (defun org-latex-verbatim (verbatim contents info)
3278 "Transcode a VERBATIM object from Org to LaTeX.
3279 CONTENTS is nil. INFO is a plist used as a communication
3280 channel."
3281 (org-latex--text-markup
3282 (org-element-property :value verbatim) 'verbatim info))
3285 ;;;; Verse Block
3287 (defun org-latex-verse-block (verse-block contents info)
3288 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3289 CONTENTS is verse block contents. INFO is a plist holding
3290 contextual information."
3291 (org-latex--wrap-label
3292 verse-block
3293 ;; In a verse environment, add a line break to each newline
3294 ;; character and change each white space at beginning of a line
3295 ;; into a space of 1 em. Also change each blank line with
3296 ;; a vertical space of 1 em.
3297 (format "\\begin{verse}\n%s\\end{verse}"
3298 (replace-regexp-in-string
3299 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
3300 (replace-regexp-in-string
3301 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3302 (replace-regexp-in-string
3303 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3304 contents nil t) nil t) nil t))
3305 info))
3309 ;;; End-user functions
3311 ;;;###autoload
3312 (defun org-latex-export-as-latex
3313 (&optional async subtreep visible-only body-only ext-plist)
3314 "Export current buffer as a LaTeX buffer.
3316 If narrowing is active in the current buffer, only export its
3317 narrowed part.
3319 If a region is active, export that region.
3321 A non-nil optional argument ASYNC means the process should happen
3322 asynchronously. The resulting buffer should be accessible
3323 through the `org-export-stack' interface.
3325 When optional argument SUBTREEP is non-nil, export the sub-tree
3326 at point, extracting information from the headline properties
3327 first.
3329 When optional argument VISIBLE-ONLY is non-nil, don't export
3330 contents of hidden elements.
3332 When optional argument BODY-ONLY is non-nil, only write code
3333 between \"\\begin{document}\" and \"\\end{document}\".
3335 EXT-PLIST, when provided, is a property list with external
3336 parameters overriding Org default settings, but still inferior to
3337 file-local settings.
3339 Export is done in a buffer named \"*Org LATEX Export*\", which
3340 will be displayed when `org-export-show-temporary-export-buffer'
3341 is non-nil."
3342 (interactive)
3343 (org-export-to-buffer 'latex "*Org LATEX Export*"
3344 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
3346 ;;;###autoload
3347 (defun org-latex-convert-region-to-latex ()
3348 "Assume the current region has org-mode syntax, and convert it to LaTeX.
3349 This can be used in any buffer. For example, you can write an
3350 itemized list in org-mode syntax in an LaTeX buffer and use this
3351 command to convert it."
3352 (interactive)
3353 (org-export-replace-region-by 'latex))
3355 ;;;###autoload
3356 (defun org-latex-export-to-latex
3357 (&optional async subtreep visible-only body-only ext-plist)
3358 "Export current buffer to a LaTeX file.
3360 If narrowing is active in the current buffer, only export its
3361 narrowed part.
3363 If a region is active, export that region.
3365 A non-nil optional argument ASYNC means the process should happen
3366 asynchronously. The resulting file should be accessible through
3367 the `org-export-stack' interface.
3369 When optional argument SUBTREEP is non-nil, export the sub-tree
3370 at point, extracting information from the headline properties
3371 first.
3373 When optional argument VISIBLE-ONLY is non-nil, don't export
3374 contents of hidden elements.
3376 When optional argument BODY-ONLY is non-nil, only write code
3377 between \"\\begin{document}\" and \"\\end{document}\".
3379 EXT-PLIST, when provided, is a property list with external
3380 parameters overriding Org default settings, but still inferior to
3381 file-local settings."
3382 (interactive)
3383 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3384 (org-export-to-file 'latex outfile
3385 async subtreep visible-only body-only ext-plist)))
3387 ;;;###autoload
3388 (defun org-latex-export-to-pdf
3389 (&optional async subtreep visible-only body-only ext-plist)
3390 "Export current buffer to LaTeX then process through to PDF.
3392 If narrowing is active in the current buffer, only export its
3393 narrowed part.
3395 If a region is active, export that region.
3397 A non-nil optional argument ASYNC means the process should happen
3398 asynchronously. The resulting file should be accessible through
3399 the `org-export-stack' interface.
3401 When optional argument SUBTREEP is non-nil, export the sub-tree
3402 at point, extracting information from the headline properties
3403 first.
3405 When optional argument VISIBLE-ONLY is non-nil, don't export
3406 contents of hidden elements.
3408 When optional argument BODY-ONLY is non-nil, only write code
3409 between \"\\begin{document}\" and \"\\end{document}\".
3411 EXT-PLIST, when provided, is a property list with external
3412 parameters overriding Org default settings, but still inferior to
3413 file-local settings.
3415 Return PDF file's name."
3416 (interactive)
3417 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3418 (org-export-to-file 'latex outfile
3419 async subtreep visible-only body-only ext-plist
3420 (lambda (file) (org-latex-compile file)))))
3422 (defun org-latex-compile (texfile &optional snippet)
3423 "Compile a TeX file.
3425 TEXFILE is the name of the file being compiled. Processing is
3426 done through the command specified in `org-latex-pdf-process'.
3428 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3429 file used to preview a LaTeX snippet. In this case, do not
3430 create a log buffer and do not bother removing log files.
3432 Return PDF file name or an error if it couldn't be produced."
3433 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3434 (full-name (file-truename texfile))
3435 (out-dir (file-name-directory texfile))
3436 ;; Properly set working directory for compilation.
3437 (default-directory (if (file-name-absolute-p texfile)
3438 (file-name-directory full-name)
3439 default-directory))
3440 (time (current-time))
3441 warnings)
3442 (unless snippet (message "Processing LaTeX file %s..." texfile))
3443 (save-window-excursion
3444 (cond
3445 ;; A function is provided: Apply it.
3446 ((functionp org-latex-pdf-process)
3447 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3448 ;; A list is provided: Replace %b, %f and %o with appropriate
3449 ;; values in each command before applying it. Output is
3450 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3451 ((consp org-latex-pdf-process)
3452 (let ((outbuf (and (not snippet)
3453 (get-buffer-create "*Org PDF LaTeX Output*"))))
3454 (dolist (command org-latex-pdf-process)
3455 (shell-command
3456 (replace-regexp-in-string
3457 "%b" (shell-quote-argument base-name)
3458 (replace-regexp-in-string
3459 "%f" (shell-quote-argument full-name)
3460 (replace-regexp-in-string
3461 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
3462 outbuf))
3463 ;; Collect standard errors from output buffer.
3464 (setq warnings (and (not snippet)
3465 (org-latex--collect-warnings outbuf)))))
3466 (t (error "No valid command to process to PDF")))
3467 (let ((pdffile (concat out-dir base-name ".pdf")))
3468 ;; Check for process failure. Provide collected errors if
3469 ;; possible.
3470 (if (or (not (file-exists-p pdffile))
3471 (time-less-p (nth 5 (file-attributes pdffile)) time))
3472 (error (format "PDF file %s wasn't produced" pdffile))
3473 ;; Else remove log files, when specified, and signal end of
3474 ;; process to user, along with any error encountered.
3475 (unless snippet
3476 (when org-latex-remove-logfiles
3477 (dolist (file (directory-files
3478 out-dir t
3479 (concat (regexp-quote base-name)
3480 "\\(?:\\.[0-9]+\\)?"
3481 "\\."
3482 (regexp-opt org-latex-logfiles-extensions))))
3483 (delete-file file)))
3484 (message (concat "PDF file produced"
3485 (cond
3486 ((eq warnings 'error) " with errors.")
3487 (warnings (concat " with warnings: " warnings))
3488 (t "."))))))
3489 ;; Return output file name.
3490 pdffile))))
3492 (defun org-latex--collect-warnings (buffer)
3493 "Collect some warnings from \"pdflatex\" command output.
3494 BUFFER is the buffer containing output. Return collected
3495 warnings types as a string, `error' if a LaTeX error was
3496 encountered or nil if there was none."
3497 (with-current-buffer buffer
3498 (save-excursion
3499 (goto-char (point-max))
3500 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3501 (if (re-search-forward "^!" nil t) 'error
3502 (let ((case-fold-search t)
3503 (warnings ""))
3504 (dolist (warning org-latex-known-warnings)
3505 (when (save-excursion (re-search-forward (car warning) nil t))
3506 (setq warnings (concat warnings " " (cdr warning)))))
3507 (org-string-nw-p (org-trim warnings))))))))
3509 ;;;###autoload
3510 (defun org-latex-publish-to-latex (plist filename pub-dir)
3511 "Publish an Org file to LaTeX.
3513 FILENAME is the filename of the Org file to be published. PLIST
3514 is the property list for the given project. PUB-DIR is the
3515 publishing directory.
3517 Return output file name."
3518 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3520 ;;;###autoload
3521 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3522 "Publish an Org file to PDF (via LaTeX).
3524 FILENAME is the filename of the Org file to be published. PLIST
3525 is the property list for the given project. PUB-DIR is the
3526 publishing directory.
3528 Return output file name."
3529 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3530 ;; in working directory and then moved to publishing directory.
3531 (org-publish-attachment
3532 plist
3533 (org-latex-compile
3534 (org-publish-org-to
3535 'latex filename ".tex" plist (file-name-directory filename)))
3536 pub-dir))
3539 (provide 'ox-latex)
3541 ;; Local variables:
3542 ;; generated-autoload-file: "org-loaddefs.el"
3543 ;; End:
3545 ;;; ox-latex.el ends here