Merge branch 'maint'
[org-mode.git] / lisp / ox-latex.el
blobbbf7f41fb8e4d99864d544c54368c38f6c2a6ca7
1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine -*- lexical-binding: t; -*-
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 (:latex-compiler "LATEX_COMPILER" nil org-latex-compiler)
148 ;; Redefine regular options.
149 (:date "DATE" nil "\\today" parse)))
153 ;;; Internal Variables
155 (defconst org-latex-babel-language-alist
156 '(("af" . "afrikaans")
157 ("bg" . "bulgarian")
158 ("bt-br" . "brazilian")
159 ("ca" . "catalan")
160 ("cs" . "czech")
161 ("cy" . "welsh")
162 ("da" . "danish")
163 ("de" . "germanb")
164 ("de-at" . "naustrian")
165 ("de-de" . "ngerman")
166 ("el" . "greek")
167 ("en" . "english")
168 ("en-au" . "australian")
169 ("en-ca" . "canadian")
170 ("en-gb" . "british")
171 ("en-ie" . "irish")
172 ("en-nz" . "newzealand")
173 ("en-us" . "american")
174 ("es" . "spanish")
175 ("et" . "estonian")
176 ("eu" . "basque")
177 ("fi" . "finnish")
178 ("fr" . "frenchb")
179 ("fr-ca" . "canadien")
180 ("gl" . "galician")
181 ("hr" . "croatian")
182 ("hu" . "hungarian")
183 ("id" . "indonesian")
184 ("is" . "icelandic")
185 ("it" . "italian")
186 ("la" . "latin")
187 ("ms" . "malay")
188 ("nl" . "dutch")
189 ("nb" . "norsk")
190 ("nn" . "nynorsk")
191 ("no" . "norsk")
192 ("pl" . "polish")
193 ("pt" . "portuguese")
194 ("ro" . "romanian")
195 ("ru" . "russian")
196 ("sa" . "sanskrit")
197 ("sb" . "uppersorbian")
198 ("sk" . "slovak")
199 ("sl" . "slovene")
200 ("sq" . "albanian")
201 ("sr" . "serbian")
202 ("sv" . "swedish")
203 ("ta" . "tamil")
204 ("tr" . "turkish")
205 ("uk" . "ukrainian"))
206 "Alist between language code and corresponding Babel option.")
208 (defconst org-latex-polyglossia-language-alist
209 '(("am" "amharic")
210 ("ast" "asturian")
211 ("ar" "arabic")
212 ("bo" "tibetan")
213 ("bn" "bengali")
214 ("bg" "bulgarian")
215 ("br" "breton")
216 ("bt-br" "brazilian")
217 ("ca" "catalan")
218 ("cop" "coptic")
219 ("cs" "czech")
220 ("cy" "welsh")
221 ("da" "danish")
222 ("de" "german" "german")
223 ("de-at" "german" "austrian")
224 ("de-de" "german" "german")
225 ("dv" "divehi")
226 ("el" "greek")
227 ("en" "english" "usmax")
228 ("en-au" "english" "australian")
229 ("en-gb" "english" "uk")
230 ("en-nz" "english" "newzealand")
231 ("en-us" "english" "usmax")
232 ("eo" "esperanto")
233 ("es" "spanish")
234 ("et" "estonian")
235 ("eu" "basque")
236 ("fa" "farsi")
237 ("fi" "finnish")
238 ("fr" "french")
239 ("fu" "friulan")
240 ("ga" "irish")
241 ("gd" "scottish")
242 ("gl" "galician")
243 ("he" "hebrew")
244 ("hi" "hindi")
245 ("hr" "croatian")
246 ("hu" "magyar")
247 ("hy" "armenian")
248 ("id" "bahasai")
249 ("ia" "interlingua")
250 ("is" "icelandic")
251 ("it" "italian")
252 ("kn" "kannada")
253 ("la" "latin" "modern")
254 ("la-modern" "latin" "modern")
255 ("la-classic" "latin" "classic")
256 ("la-medieval" "latin" "medieval")
257 ("lo" "lao")
258 ("lt" "lithuanian")
259 ("lv" "latvian")
260 ("mr" "maranthi")
261 ("ml" "malayalam")
262 ("nl" "dutch")
263 ("nb" "norsk")
264 ("nn" "nynorsk")
265 ("nko" "nko")
266 ("no" "norsk")
267 ("oc" "occitan")
268 ("pl" "polish")
269 ("pms" "piedmontese")
270 ("pt" "portuges")
271 ("rm" "romansh")
272 ("ro" "romanian")
273 ("ru" "russian")
274 ("sa" "sanskrit")
275 ("hsb" "usorbian")
276 ("dsb" "lsorbian")
277 ("sk" "slovak")
278 ("sl" "slovenian")
279 ("se" "samin")
280 ("sq" "albanian")
281 ("sr" "serbian")
282 ("sv" "swedish")
283 ("syr" "syriac")
284 ("ta" "tamil")
285 ("te" "telugu")
286 ("th" "thai")
287 ("tk" "turkmen")
288 ("tr" "turkish")
289 ("uk" "ukrainian")
290 ("ur" "urdu")
291 ("vi" "vietnamese"))
292 "Alist between language code and corresponding Polyglossia option")
296 (defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
297 ("qbordermatrix" . "\\cr")
298 ("kbordermatrix" . "\\\\"))
299 "Alist between matrix macros and their row ending.")
301 (defconst org-latex-math-environments-re
302 (format
303 "\\`[ \t]*\\\\begin{%s\\*?}"
304 (regexp-opt
305 '("equation" "eqnarray" "math" "displaymath"
306 "align" "gather" "multline" "flalign" "alignat"
307 "xalignat" "xxalignat"
308 "subequations"
309 ;; breqn
310 "dmath" "dseries" "dgroup" "darray"
311 ;; empheq
312 "empheq")))
313 "Regexp of LaTeX math environments.")
316 ;;; User Configurable Variables
318 (defgroup org-export-latex nil
319 "Options for exporting Org mode files to LaTeX."
320 :tag "Org Export LaTeX"
321 :group 'org-export)
323 ;;;; Generic
325 (defcustom org-latex-caption-above '(table)
326 "When non-nil, place caption string at the beginning of elements.
327 Otherwise, place it near the end. When value is a list of
328 symbols, put caption above selected elements only. Allowed
329 symbols are: `image', `table', `src-block' and `special-block'."
330 :group 'org-export-latex
331 :version "25.1"
332 :package-version '(Org . "8.3")
333 :type '(choice
334 (const :tag "For all elements" t)
335 (const :tag "For no element" nil)
336 (set :tag "For the following elements only" :greedy t
337 (const :tag "Images" image)
338 (const :tag "Tables" table)
339 (const :tag "Source code" src-block)
340 (const :tag "Special blocks" special-block))))
342 (defcustom org-latex-prefer-user-labels nil
343 "Use user-provided labels instead of internal ones when non-nil.
345 When this variable is non-nil, Org will use the value of
346 CUSTOM_ID property, NAME keyword or Org target as the key for the
347 \\label commands generated.
349 By default, Org generates its own internal labels during LaTeX
350 export. This process ensures that the \\label keys are unique
351 and valid, but it means the keys are not available in advance of
352 the export process.
354 Setting this variable gives you control over how Org generates
355 labels during LaTeX export, so that you may know their keys in
356 advance. One reason to do this is that it allows you to refer to
357 various elements using a single label both in Org's link syntax
358 and in embedded LaTeX code.
360 For example, when this variable is non-nil, a headline like this:
362 ** Some section
363 :PROPERTIES:
364 :CUSTOM_ID: sec:foo
365 :END:
366 This is section [[#sec:foo]].
367 #+BEGIN_LATEX
368 And this is still section \\ref{sec:foo}.
369 #+END_LATEX
371 will be exported to LaTeX as:
373 \\subsection{Some section}
374 \\label{sec:foo}
375 This is section \\ref{sec:foo}.
376 And this is still section \\ref{sec:foo}.
378 Note, however, that setting this variable introduces a limitation
379 on the possible values for CUSTOM_ID and NAME. When this
380 variable is non-nil, Org passes their value to \\label unchanged.
381 You are responsible for ensuring that the value is a valid LaTeX
382 \\label key, and that no other \\label commands with the same key
383 appear elsewhere in your document. (Keys may contain letters,
384 numbers, and the following punctuation: '_' '.' '-' ':'.) There
385 are no such limitations on CUSTOM_ID and NAME when this variable
386 is nil.
388 For headlines that do not define the CUSTOM_ID property or
389 elements without a NAME, Org will continue to use its default
390 labeling scheme to generate labels and resolve links into proper
391 references."
392 :group 'org-export-latex
393 :type 'boolean
394 :version "25.1"
395 :package-version '(Org . "8.3"))
397 ;;;; Preamble
399 (defcustom org-latex-default-class "article"
400 "The default LaTeX class."
401 :group 'org-export-latex
402 :type '(string :tag "LaTeX class"))
404 (defcustom org-latex-classes
405 '(("article"
406 "\\documentclass[11pt]{article}"
407 ("\\section{%s}" . "\\section*{%s}")
408 ("\\subsection{%s}" . "\\subsection*{%s}")
409 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
410 ("\\paragraph{%s}" . "\\paragraph*{%s}")
411 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
412 ("report"
413 "\\documentclass[11pt]{report}"
414 ("\\part{%s}" . "\\part*{%s}")
415 ("\\chapter{%s}" . "\\chapter*{%s}")
416 ("\\section{%s}" . "\\section*{%s}")
417 ("\\subsection{%s}" . "\\subsection*{%s}")
418 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
419 ("book"
420 "\\documentclass[11pt]{book}"
421 ("\\part{%s}" . "\\part*{%s}")
422 ("\\chapter{%s}" . "\\chapter*{%s}")
423 ("\\section{%s}" . "\\section*{%s}")
424 ("\\subsection{%s}" . "\\subsection*{%s}")
425 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
426 "Alist of LaTeX classes and associated header and structure.
427 If #+LATEX_CLASS is set in the buffer, use its value and the
428 associated information. Here is the structure of each cell:
430 (class-name
431 header-string
432 (numbered-section . unnumbered-section)
433 ...)
435 The header string
436 -----------------
438 The HEADER-STRING is the header that will be inserted into the
439 LaTeX file. It should contain the \\documentclass macro, and
440 anything else that is needed for this setup. To this header, the
441 following commands will be added:
443 - Calls to \\usepackage for all packages mentioned in the
444 variables `org-latex-default-packages-alist' and
445 `org-latex-packages-alist'. Thus, your header definitions
446 should avoid to also request these packages.
448 - Lines specified via \"#+LATEX_HEADER:\" and
449 \"#+LATEX_HEADER_EXTRA:\" keywords.
451 If you need more control about the sequence in which the header
452 is built up, or if you want to exclude one of these building
453 blocks for a particular class, you can use the following
454 macro-like placeholders.
456 [DEFAULT-PACKAGES] \\usepackage statements for default packages
457 [NO-DEFAULT-PACKAGES] do not include any of the default packages
458 [PACKAGES] \\usepackage statements for packages
459 [NO-PACKAGES] do not include the packages
460 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
461 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
463 So a header like
465 \\documentclass{article}
466 [NO-DEFAULT-PACKAGES]
467 [EXTRA]
468 \\providecommand{\\alert}[1]{\\textbf{#1}}
469 [PACKAGES]
471 will omit the default packages, and will include the
472 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
473 to \\providecommand, and then place \\usepackage commands based
474 on the content of `org-latex-packages-alist'.
476 If your header, `org-latex-default-packages-alist' or
477 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
478 AUTO will automatically be replaced with a coding system derived
479 from `buffer-file-coding-system'. See also the variable
480 `org-latex-inputenc-alist' for a way to influence this mechanism.
482 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\"
483 or \"\\usepackage[AUTO]{polyglossia}\", AUTO will be replaced
484 with the language related to the language code specified by
485 `org-export-default-language'. Note that constructions such as
486 \"\\usepackage[french,AUTO,english]{babel}\" are permitted. For
487 Polyglossia the language will be set via the macros
488 \"\\setmainlanguage\" and \"\\setotherlanguage\". See also
489 `org-latex-guess-babel-language' and
490 `org-latex-guess-polyglossia-language'.
492 The sectioning structure
493 ------------------------
495 The sectioning structure of the class is given by the elements
496 following the header string. For each sectioning level, a number
497 of strings is specified. A %s formatter is mandatory in each
498 section string and will be replaced by the title of the section.
500 Instead of a cons cell (numbered . unnumbered), you can also
501 provide a list of 2 or 4 elements,
503 (numbered-open numbered-close)
507 (numbered-open numbered-close unnumbered-open unnumbered-close)
509 providing opening and closing strings for a LaTeX environment
510 that should represent the document section. The opening clause
511 should have a %s to represent the section title.
513 Instead of a list of sectioning commands, you can also specify
514 a function name. That function will be called with two
515 parameters, the (reduced) level of the headline, and a predicate
516 non-nil when the headline should be numbered. It must return
517 a format string in which the section title will be added."
518 :group 'org-export-latex
519 :type '(repeat
520 (list (string :tag "LaTeX class")
521 (string :tag "LaTeX header")
522 (repeat :tag "Levels" :inline t
523 (choice
524 (cons :tag "Heading"
525 (string :tag " numbered")
526 (string :tag "unnumbered"))
527 (list :tag "Environment"
528 (string :tag "Opening (numbered)")
529 (string :tag "Closing (numbered)")
530 (string :tag "Opening (unnumbered)")
531 (string :tag "Closing (unnumbered)"))
532 (function :tag "Hook computing sectioning"))))))
534 (defcustom org-latex-inputenc-alist nil
535 "Alist of inputenc coding system names, and what should really be used.
536 For example, adding an entry
538 (\"utf8\" . \"utf8x\")
540 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
541 are written as utf8 files."
542 :group 'org-export-latex
543 :type '(repeat
544 (cons
545 (string :tag "Derived from buffer")
546 (string :tag "Use this instead"))))
548 (defcustom org-latex-title-command "\\maketitle"
549 "The command used to insert the title just after \\begin{document}.
551 This format string may contain these elements:
553 %a for AUTHOR keyword
554 %t for TITLE keyword
555 %s for SUBTITLE keyword
556 %k for KEYWORDS line
557 %d for DESCRIPTION line
558 %c for CREATOR line
559 %l for Language keyword
560 %L for capitalized language keyword
561 %D for DATE keyword
563 If you need to use a \"%\" character, you need to escape it
564 like that: \"%%\".
566 Setting :latex-title-command in publishing projects will take
567 precedence over this variable."
568 :group 'org-export-latex
569 :type '(string :tag "Format string"))
571 (defcustom org-latex-subtitle-format "\\\\\\medskip\n\\large %s"
572 "Format string used for transcoded subtitle.
573 The format string should have at most one \"%s\"-expression,
574 which is replaced with the subtitle."
575 :group 'org-export-latex
576 :version "25.1"
577 :package-version '(Org . "8.3")
578 :type '(string :tag "Format string"))
580 (defcustom org-latex-subtitle-separate nil
581 "Non-nil means the subtitle is not typeset as part of title."
582 :group 'org-export-latex
583 :version "25.1"
584 :package-version '(Org . "8.3")
585 :type 'boolean)
587 (defcustom org-latex-toc-command "\\tableofcontents\n\n"
588 "LaTeX command to set the table of contents, list of figures, etc.
589 This command only applies to the table of contents generated with
590 the toc:nil option, not to those generated with #+TOC keyword."
591 :group 'org-export-latex
592 :type 'string)
594 (defcustom org-latex-hyperref-template
595 "\\hypersetup{\n pdfauthor={%a},\n pdftitle={%t},\n pdfkeywords={%k},
596 pdfsubject={%d},\n pdfcreator={%c}, \n pdflang={%L}}\n"
597 "Template for hyperref package options.
599 This format string may contain these elements:
601 %a for AUTHOR keyword
602 %t for TITLE keyword
603 %s for SUBTITLE keyword
604 %k for KEYWORDS line
605 %d for DESCRIPTION line
606 %c for CREATOR line
607 %l for Language keyword
608 %L for capitalized language keyword
609 %D for DATE keyword
611 If you need to use a \"%\" character, you need to escape it
612 like that: \"%%\".
614 As a special case, a nil value prevents template from being
615 inserted.
617 Setting :latex-hyperref-template in publishing projects will take
618 precedence over this variable."
619 :group 'org-export-latex
620 :version "25.1"
621 :package-version '(Org . "8.3")
622 :type '(choice (const :tag "No template" nil)
623 (string :tag "Format string")))
624 (define-obsolete-variable-alias
625 'org-latex-with-hyperref 'org-latex-hyperref-template "25.1")
627 ;;;; Headline
629 (defcustom org-latex-format-headline-function
630 'org-latex-format-headline-default-function
631 "Function for formatting the headline's text.
633 This function will be called with six arguments:
634 TODO the todo keyword (string or nil)
635 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
636 PRIORITY the priority of the headline (integer or nil)
637 TEXT the main headline text (string)
638 TAGS the tags (list of strings or nil)
639 INFO the export options (plist)
641 The function result will be used in the section format string."
642 :group 'org-export-latex
643 :version "24.4"
644 :package-version '(Org . "8.0")
645 :type 'function)
648 ;;;; Footnotes
650 (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
651 "Text used to separate footnotes."
652 :group 'org-export-latex
653 :type 'string)
656 ;;;; Timestamps
658 (defcustom org-latex-active-timestamp-format "\\textit{%s}"
659 "A printf format string to be applied to active timestamps."
660 :group 'org-export-latex
661 :type 'string)
663 (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
664 "A printf format string to be applied to inactive timestamps."
665 :group 'org-export-latex
666 :type 'string)
668 (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
669 "A printf format string to be applied to diary timestamps."
670 :group 'org-export-latex
671 :type 'string)
674 ;;;; Links
676 (defcustom org-latex-image-default-option ""
677 "Default option for images."
678 :group 'org-export-latex
679 :version "24.4"
680 :package-version '(Org . "8.0")
681 :type 'string)
683 (defcustom org-latex-image-default-width ".9\\linewidth"
684 "Default width for images.
685 This value will not be used if a height is provided."
686 :group 'org-export-latex
687 :version "24.4"
688 :package-version '(Org . "8.0")
689 :type 'string)
691 (defcustom org-latex-image-default-height ""
692 "Default height for images.
693 This value will not be used if a width is provided, or if the
694 image is wrapped within a \"figure\" or \"wrapfigure\"
695 environment."
696 :group 'org-export-latex
697 :version "24.4"
698 :package-version '(Org . "8.0")
699 :type 'string)
701 (defcustom org-latex-default-figure-position "htb"
702 "Default position for latex figures."
703 :group 'org-export-latex
704 :type 'string)
706 (defcustom org-latex-inline-image-rules
707 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
708 "Rules characterizing image files that can be inlined into LaTeX.
710 A rule consists in an association whose key is the type of link
711 to consider, and value is a regexp that will be matched against
712 link's path.
714 Note that, by default, the image extension *actually* allowed
715 depend on the way the LaTeX file is processed. When used with
716 pdflatex, pdf, jpg and png images are OK. When processing
717 through dvi to Postscript, only ps and eps are allowed. The
718 default we use here encompasses both."
719 :group 'org-export-latex
720 :version "24.4"
721 :package-version '(Org . "8.0")
722 :type '(alist :key-type (string :tag "Type")
723 :value-type (regexp :tag "Path")))
725 (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
726 "Format string for links with unknown path type."
727 :group 'org-export-latex
728 :type 'string)
731 ;;;; Tables
733 (defcustom org-latex-default-table-environment "tabular"
734 "Default environment used to build tables."
735 :group 'org-export-latex
736 :version "24.4"
737 :package-version '(Org . "8.0")
738 :type 'string)
740 (defcustom org-latex-default-table-mode 'table
741 "Default mode for tables.
743 Value can be a symbol among:
745 `table' Regular LaTeX table.
747 `math' In this mode, every cell is considered as being in math
748 mode and the complete table will be wrapped within a math
749 environment. It is particularly useful to write matrices.
751 `inline-math' This mode is almost the same as `math', but the
752 math environment will be inlined.
754 `verbatim' The table is exported as it appears in the Org
755 buffer, within a verbatim environment.
757 This value can be overridden locally with, i.e. \":mode math\" in
758 LaTeX attributes.
760 When modifying this variable, it may be useful to change
761 `org-latex-default-table-environment' accordingly."
762 :group 'org-export-latex
763 :version "24.4"
764 :package-version '(Org . "8.0")
765 :type '(choice (const :tag "Table" table)
766 (const :tag "Matrix" math)
767 (const :tag "Inline matrix" inline-math)
768 (const :tag "Verbatim" verbatim))
769 :safe (lambda (s) (memq s '(table math inline-math verbatim))))
771 (defcustom org-latex-tables-centered t
772 "When non-nil, tables are exported in a center environment."
773 :group 'org-export-latex
774 :type 'boolean
775 :safe #'booleanp)
777 (defcustom org-latex-tables-booktabs nil
778 "When non-nil, display tables in a formal \"booktabs\" style.
779 This option assumes that the \"booktabs\" package is properly
780 loaded in the header of the document. This value can be ignored
781 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
782 attributes."
783 :group 'org-export-latex
784 :version "24.4"
785 :package-version '(Org . "8.0")
786 :type 'boolean
787 :safe #'booleanp)
789 (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
790 "Format string to display numbers in scientific notation.
791 The format should have \"%s\" twice, for mantissa and exponent
792 \(i.e., \"%s\\\\times10^{%s}\").
794 When nil, no transformation is made."
795 :group 'org-export-latex
796 :version "24.4"
797 :package-version '(Org . "8.0")
798 :type '(choice
799 (string :tag "Format string")
800 (const :tag "No formatting" nil)))
802 ;;;; Text markup
804 (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
805 (code . protectedtexttt)
806 (italic . "\\emph{%s}")
807 (strike-through . "\\sout{%s}")
808 (underline . "\\uline{%s}")
809 (verbatim . protectedtexttt))
810 "Alist of LaTeX expressions to convert text markup.
812 The key must be a symbol among `bold', `code', `italic',
813 `strike-through', `underline' and `verbatim'. The value is
814 a formatting string to wrap fontified text with.
816 Value can also be set to the following symbols: `verb' and
817 `protectedtexttt'. For the former, Org will use \"\\verb\" to
818 create a format string and select a delimiter character that
819 isn't in the string. For the latter, Org will use \"\\texttt\"
820 to typeset and try to protect special characters.
822 If no association can be found for a given markup, text will be
823 returned as-is."
824 :group 'org-export-latex
825 :version "25.1"
826 :package-version '(Org . "8.3")
827 :type 'alist
828 :options '(bold code italic strike-through underline verbatim))
831 ;;;; Drawers
833 (defcustom org-latex-format-drawer-function (lambda (_ 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 (R "r"))
928 "Alist mapping languages to their listing language counterpart.
929 The key is a symbol, the major mode symbol without the \"-mode\".
930 The value is the string that should be inserted as the language
931 parameter for the listings package. If the mode name and the
932 listings name are the same, the language does not need an entry
933 in this list - but it does not hurt if it is present."
934 :group 'org-export-latex
935 :version "24.4"
936 :package-version '(Org . "8.3")
937 :type '(repeat
938 (list
939 (symbol :tag "Major mode ")
940 (string :tag "Listings language"))))
942 (defcustom org-latex-listings-options nil
943 "Association list of options for the latex listings package.
945 These options are supplied as a comma-separated list to the
946 \\lstset command. Each element of the association list should be
947 a list containing two strings: the name of the option, and the
948 value. For example,
950 (setq org-latex-listings-options
951 \\='((\"basicstyle\" \"\\\\small\")
952 (\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
954 will typeset the code in a small size font with underlined, bold
955 black keywords.
957 Note that the same options will be applied to blocks of all
958 languages. If you need block-specific options, you may use the
959 following syntax:
961 #+ATTR_LATEX: :options key1=value1,key2=value2
962 #+BEGIN_SRC <LANG>
964 #+END_SRC"
965 :group 'org-export-latex
966 :type '(repeat
967 (list
968 (string :tag "Listings option name ")
969 (string :tag "Listings option value"))))
971 (defcustom org-latex-minted-langs
972 '((emacs-lisp "common-lisp")
973 (cc "c++")
974 (cperl "perl")
975 (shell-script "bash")
976 (caml "ocaml"))
977 "Alist mapping languages to their minted language counterpart.
978 The key is a symbol, the major mode symbol without the \"-mode\".
979 The value is the string that should be inserted as the language
980 parameter for the minted package. If the mode name and the
981 listings name are the same, the language does not need an entry
982 in this list - but it does not hurt if it is present.
984 Note that minted uses all lower case for language identifiers,
985 and that the full list of language identifiers can be obtained
986 with:
988 pygmentize -L lexers"
989 :group 'org-export-latex
990 :type '(repeat
991 (list
992 (symbol :tag "Major mode ")
993 (string :tag "Minted language"))))
995 (defcustom org-latex-minted-options nil
996 "Association list of options for the latex minted package.
998 These options are supplied within square brackets in
999 \\begin{minted} environments. Each element of the alist should
1000 be a list containing two strings: the name of the option, and the
1001 value. For example,
1003 (setq org-latex-minted-options
1004 '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
1006 will result in src blocks being exported with
1008 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
1010 as the start of the minted environment. Note that the same
1011 options will be applied to blocks of all languages. If you need
1012 block-specific options, you may use the following syntax:
1014 #+ATTR_LATEX: :options key1=value1,key2=value2
1015 #+BEGIN_SRC <LANG>
1017 #+END_SRC"
1018 :group 'org-export-latex
1019 :type '(repeat
1020 (list
1021 (string :tag "Minted option name ")
1022 (string :tag "Minted option value"))))
1024 (defvar org-latex-custom-lang-environments nil
1025 "Alist mapping languages to language-specific LaTeX environments.
1027 It is used during export of src blocks by the listings and minted
1028 latex packages. For example,
1030 (setq org-latex-custom-lang-environments
1031 '((python \"pythoncode\")))
1033 would have the effect that if org encounters begin_src python
1034 during latex export it will output
1036 \\begin{pythoncode}
1037 <src block body>
1038 \\end{pythoncode}")
1041 ;;;; Compilation
1043 (defcustom org-latex-compiler-file-string "%% Indented LaTeX compiler: %s\n"
1044 "LaTeX compiler format-string.
1045 See also `org-latex-compiler'."
1046 :group 'org-export-latex
1047 :type '(choice
1048 (const :tag "Comment" "%% Indented LaTeX compiler: %s\n")
1049 (const :tag "latex-mode file variable" "%% -*- latex-run-command: %s -*-\n")
1050 (const :tag "AUCTeX file variable" "%% -*- LaTeX-command: %s -*-\n")
1051 (string :tag "custom format" "%% %s"))
1052 :version "25.1"
1053 :package-version '(Org . "9.0"))
1055 (defcustom org-latex-compiler "pdflatex"
1056 "LaTeX compiler to use.
1058 Must be an element in `org-latex-compilers' or the empty quote.
1059 Can also be set in buffers via #+LATEX_COMPILER. See also
1060 `org-latex-compiler-file-string'."
1061 :group 'org-export-latex
1062 :type '(choice
1063 (const :tag "pdfLaTeX" "pdflatex")
1064 (const :tag "XeLaTeX" "xelatex")
1065 (const :tag "LuaLaTeX" "lualatex")
1066 (const :tag "Unset" ""))
1067 :version "25.1"
1068 :package-version '(Org . "9.0"))
1070 (defconst org-latex-compilers '("pdflatex" "xelatex" "lualatex")
1071 "Known LaTeX compilers.
1072 See also `org-latex-compiler'.")
1074 (defcustom org-latex-bib-compiler "bibtex"
1075 "Command to process a LaTeX file's bibliography.
1077 The shorthand %bib in `org-latex-pdf-process' is replaced with
1078 this value.
1080 A better approach is to use a compiler suit such as `latexmk'."
1081 :group 'org-export-latex
1082 :type '(choice (const :tag "BibTeX" "bibtex")
1083 (const :tag "Biber" "biber")
1084 (string :tag "Other process"))
1085 :version "25.1"
1086 :package-version '(Org . "9.0"))
1088 (defcustom org-latex-pdf-process
1089 '("%latex -interaction nonstopmode -output-directory %o %f"
1090 "%latex -interaction nonstopmode -output-directory %o %f"
1091 "%latex -interaction nonstopmode -output-directory %o %f")
1092 "Commands to process a LaTeX file to a PDF file.
1093 This is a list of strings, each of them will be given to the
1094 shell as a command. %f in the command will be replaced by the
1095 full file name, %b by the file base name (i.e. without directory
1096 and extension parts), %o by the base directory of the file,
1097 %latex is the LaTeX compiler (see `org-latex-compiler'), and %bib
1098 is the BibTeX-like compiler (see `org-latex-bib-compiler').
1100 The reason why this is a list is that it usually takes several
1101 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
1102 does not have a clever mechanism to detect which of these
1103 commands have to be run to get to a stable result, and it also
1104 does not do any error checking.
1106 Consider a smart LaTeX compiler such as `texi2dvi' or `latexmk',
1107 which calls the \"correct\" combinations of auxiliary programs.
1109 Alternatively, this may be a Lisp function that does the
1110 processing, so you could use this to apply the machinery of
1111 AUCTeX or the Emacs LaTeX mode. This function should accept the
1112 file name as its single argument."
1113 :group 'org-export-pdf
1114 :type '(choice
1115 (repeat :tag "Shell command sequence"
1116 (string :tag "Shell command"))
1117 (const :tag "2 runs of latex"
1118 ("%latex -interaction nonstopmode -output-directory %o %f"
1119 "%latex -interaction nonstopmode -output-directory %o %f"))
1120 (const :tag "3 runs of latex"
1121 ("%latex -interaction nonstopmode -output-directory %o %f"
1122 "%latex -interaction nonstopmode -output-directory %o %f"
1123 "%latex -interaction nonstopmode -output-directory %o %f"))
1124 (const :tag "latex,bibtex,latex,latex"
1125 ("%latex -interaction nonstopmode -output-directory %o %f"
1126 "%bib %b"
1127 "%latex -interaction nonstopmode -output-directory %o %f"
1128 "%latex -interaction nonstopmode -output-directory %o %f"))
1129 (const :tag "texi2dvi"
1130 ("LATEX=\"%latex\" texi2dvi -p -b -V %f"))
1131 (const :tag "latexmk"
1132 ("latexmk -g -pdflatex=\"%latex\" %f"))
1133 (function)))
1135 (defcustom org-latex-logfiles-extensions
1136 '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
1137 "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
1138 "The list of file extensions to consider as LaTeX logfiles.
1139 The logfiles will be removed if `org-latex-remove-logfiles' is
1140 non-nil."
1141 :group 'org-export-latex
1142 :version "25.1"
1143 :package-version '(Org . "8.3")
1144 :type '(repeat (string :tag "Extension")))
1146 (defcustom org-latex-remove-logfiles t
1147 "Non-nil means remove the logfiles produced by PDF production.
1148 By default, logfiles are files with these extensions: .aux, .idx,
1149 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
1150 logfiles to remove, set `org-latex-logfiles-extensions'."
1151 :group 'org-export-latex
1152 :type 'boolean)
1154 (defcustom org-latex-known-warnings
1155 '(("Reference.*?undefined" . "[undefined reference]")
1156 ("Runaway argument" . "[runaway argument]")
1157 ("Underfull \\hbox" . "[underfull hbox]")
1158 ("Overfull \\hbox" . "[overfull hbox]")
1159 ("Citation.*?undefined" . "[undefined citation]")
1160 ("Undefined control sequence" . "[undefined control sequence]"))
1161 "Alist of regular expressions and associated messages for the user.
1162 The regular expressions are used to find possible warnings in the
1163 log of a latex-run. These warnings will be reported after
1164 calling `org-latex-compile'."
1165 :group 'org-export-latex
1166 :version "25.1"
1167 :package-version '(Org . "8.3")
1168 :type '(repeat
1169 (cons
1170 (string :tag "Regexp")
1171 (string :tag "Message"))))
1175 ;;; Internal Functions
1177 (defun org-latex--caption-above-p (element info)
1178 "Non nil when caption is expected to be located above ELEMENT.
1179 INFO is a plist holding contextual information."
1180 (let ((above (plist-get info :latex-caption-above)))
1181 (if (symbolp above) above
1182 (let ((type (org-element-type element)))
1183 (memq (if (eq type 'link) 'image type) above)))))
1185 (defun org-latex--label (datum info &optional force full)
1186 "Return an appropriate label for DATUM.
1187 DATUM is an element or a `target' type object. INFO is the
1188 current export state, as a plist.
1190 Return nil if element DATUM has no NAME or VALUE affiliated
1191 keyword or no CUSTOM_ID property, unless FORCE is non-nil. In
1192 this case always return a unique label.
1194 Eventually, if FULL is non-nil, wrap label within \"\\label{}\"."
1195 (let* ((type (org-element-type datum))
1196 (user-label
1197 (org-element-property
1198 (case type
1199 ((headline inlinetask) :CUSTOM_ID)
1200 (target :value)
1201 (otherwise :name))
1202 datum))
1203 (label
1204 (and (or user-label force)
1205 (if (and user-label (plist-get info :latex-prefer-user-labels))
1206 user-label
1207 (concat (case type
1208 (headline "sec:")
1209 (table "tab:")
1210 (latex-environment
1211 (and (org-string-match-p
1212 org-latex-math-environments-re
1213 (org-element-property :value datum))
1214 "eq:"))
1215 (paragraph
1216 (and (org-element-property :caption datum)
1217 "fig:")))
1218 (org-export-get-reference datum info))))))
1219 (cond ((not full) label)
1220 (label (format "\\label{%s}%s"
1221 label
1222 (if (eq type 'target) "" "\n")))
1223 (t ""))))
1225 (defun org-latex--caption/label-string (element info)
1226 "Return caption and label LaTeX string for ELEMENT.
1228 INFO is a plist holding contextual information. If there's no
1229 caption nor label, return the empty string.
1231 For non-floats, see `org-latex--wrap-label'."
1232 (let* ((label (org-latex--label element info nil t))
1233 (main (org-export-get-caption element))
1234 (attr (org-export-read-attribute :attr_latex element))
1235 (type (org-element-type element))
1236 (nonfloat (or (and (plist-member attr :float)
1237 (not (plist-get attr :float))
1238 main)
1239 (and (eq type 'src-block)
1240 (not (plist-get attr :float))
1241 (memq (plist-get info :latex-listings)
1242 '(nil minted)))))
1243 (short (org-export-get-caption element t))
1244 (caption-from-attr-latex (plist-get attr :caption)))
1245 (cond
1246 ((org-string-nw-p caption-from-attr-latex)
1247 (concat caption-from-attr-latex "\n"))
1248 ((and (not main) (equal label "")) "")
1249 ((not main) (concat label "\n"))
1250 ;; Option caption format with short name.
1252 (format (if nonfloat "\\captionof{%s}%s{%s%s}\n"
1253 "\\caption%s%s{%s%s}\n")
1254 (if nonfloat
1255 (case type
1256 (paragraph "figure")
1257 (src-block (if (plist-get info :latex-listings)
1258 "listing"
1259 "figure"))
1260 (t (symbol-name type)))
1262 (if short (format "[%s]" (org-export-data short info)) "")
1263 label
1264 (org-export-data main info))))))
1266 (defun org-latex-guess-inputenc (header)
1267 "Set the coding system in inputenc to what the buffer is.
1269 HEADER is the LaTeX header string. This function only applies
1270 when specified inputenc option is \"AUTO\".
1272 Return the new header, as a string."
1273 (let* ((cs (or (ignore-errors
1274 (latexenc-coding-system-to-inputenc
1275 (or org-export-coding-system buffer-file-coding-system)))
1276 "utf8")))
1277 (if (not cs) header
1278 ;; First translate if that is requested.
1279 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
1280 ;; Then find the \usepackage statement and replace the option.
1281 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1282 cs header t nil 1))))
1284 (defun org-latex-guess-babel-language (header info)
1285 "Set Babel's language according to LANGUAGE keyword.
1287 HEADER is the LaTeX header string. INFO is the plist used as
1288 a communication channel.
1290 Insertion of guessed language only happens when Babel package has
1291 explicitly been loaded. Then it is added to the rest of
1292 package's options.
1294 The argument to Babel may be \"AUTO\" which is then replaced with
1295 the language of the document or `org-export-default-language'
1296 unless language in question is already loaded.
1298 Return the new header."
1299 (let ((language-code (plist-get info :language)))
1300 ;; If no language is set or Babel package is not loaded, return
1301 ;; HEADER as-is.
1302 (if (or (not (stringp language-code))
1303 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
1304 header
1305 (let ((options (save-match-data
1306 (org-split-string (match-string 1 header) ",[ \t]*")))
1307 (language (cdr (assoc language-code
1308 org-latex-babel-language-alist))))
1309 ;; If LANGUAGE is already loaded, return header without AUTO.
1310 ;; Otherwise, replace AUTO with language or append language if
1311 ;; AUTO is not present.
1312 (replace-match
1313 (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
1314 (cond ((member language options) (delete "AUTO" options))
1315 ((member "AUTO" options) options)
1316 (t (append options (list language))))
1317 ", ")
1318 t nil header 1)))))
1320 (defun org-latex-guess-polyglossia-language (header info)
1321 "Set the Polyglossia language according to the LANGUAGE keyword.
1323 HEADER is the LaTeX header string. INFO is the plist used as
1324 a communication channel.
1326 Insertion of guessed language only happens when the Polyglossia
1327 package has been explicitly loaded.
1329 The argument to Polyglossia may be \"AUTO\" which is then
1330 replaced with the language of the document or
1331 `org-export-default-language'. Note, the language is really set
1332 using \setdefaultlanguage and not as an option to the package.
1334 Return the new header."
1335 (let ((language (plist-get info :language)))
1336 ;; If no language is set or Polyglossia is not loaded, return
1337 ;; HEADER as-is.
1338 (if (or (not (stringp language))
1339 (not (string-match
1340 "\\\\usepackage\\(?:\\[\\([^]]+?\\)\\]\\){polyglossia}\n"
1341 header)))
1342 header
1343 (let* ((options (org-string-nw-p (match-string 1 header)))
1344 (languages (and options
1345 ;; Reverse as the last loaded language is
1346 ;; the main language.
1347 (nreverse
1348 (delete-dups
1349 (save-match-data
1350 (org-split-string
1351 (replace-regexp-in-string
1352 "AUTO" language options t)
1353 ",[ \t]*"))))))
1354 (main-language-set
1355 (string-match-p "\\\\setmainlanguage{.*?}" header)))
1356 (replace-match
1357 (concat "\\usepackage{polyglossia}\n"
1358 (mapconcat
1359 (lambda (l)
1360 (let ((l (or (assoc l org-latex-polyglossia-language-alist)
1361 l)))
1362 (format (if main-language-set "\\setotherlanguage%s{%s}\n"
1363 (setq main-language-set t)
1364 "\\setmainlanguage%s{%s}\n")
1365 (if (and (consp l) (= (length l) 3))
1366 (format "[variant=%s]" (nth 2 l))
1368 (nth 1 l))))
1369 languages
1370 ""))
1371 t t header 0)))))
1373 (defun org-latex--remove-packages (pkg-alist info)
1374 "Remove packages based on the current LaTeX compiler.
1376 If the fourth argument of an element is set in pkg-alist, and it
1377 is not a member of the LaTeX compiler of the document, the packages
1378 is removed. See also `org-latex-compiler'.
1380 Return modified pkg-alist."
1381 (let ((compiler (or (plist-get info :latex-compiler) "")))
1382 (if (member-ignore-case compiler org-latex-compilers)
1383 (delq nil
1384 (mapcar
1385 (lambda (pkg)
1386 (unless (and
1387 (listp pkg)
1388 (let ((third (nth 3 pkg)))
1389 (and third
1390 (not (member-ignore-case
1391 compiler
1392 (if (listp third) third (list third)))))))
1393 pkg))
1394 pkg-alist))
1395 pkg-alist)))
1397 (defun org-latex--find-verb-separator (s)
1398 "Return a character not used in string S.
1399 This is used to choose a separator for constructs like \\verb."
1400 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1401 (loop for c across ll
1402 when (not (string-match (regexp-quote (char-to-string c)) s))
1403 return (char-to-string c))))
1405 (defun org-latex--make-option-string (options)
1406 "Return a comma separated string of keywords and values.
1407 OPTIONS is an alist where the key is the options keyword as
1408 a string, and the value a list containing the keyword value, or
1409 nil."
1410 (mapconcat (lambda (pair)
1411 (concat (first pair)
1412 (when (> (length (second pair)) 0)
1413 (concat "=" (second pair)))))
1414 options
1415 ","))
1417 (defun org-latex--wrap-label (element output info)
1418 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1419 INFO is the current export state, as a plist. This function
1420 should not be used for floats. See
1421 `org-latex--caption/label-string'."
1422 (if (not (and (org-string-nw-p output) (org-element-property :name element)))
1423 output
1424 (concat (format "\\phantomsection\n\\label{%s}\n"
1425 (org-latex--label element info))
1426 output)))
1428 (defun org-latex--protect-text (text)
1429 "Protect special characters in string TEXT and return it."
1430 (replace-regexp-in-string
1431 "--\\|[\\{}$%&_#~^]"
1432 (lambda (m)
1433 (cond ((equal m "--") "-{}-")
1434 ((equal m "\\") "\\textbackslash{}")
1435 ((equal m "~") "\\textasciitilde{}")
1436 ((equal m "^") "\\textasciicircum{}")
1437 (t (concat "\\" m))))
1438 text nil t))
1440 (defun org-latex--text-markup (text markup info)
1441 "Format TEXT depending on MARKUP text markup.
1442 INFO is a plist used as a communication channel. See
1443 `org-latex-text-markup-alist' for details."
1444 (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
1445 (case fmt
1446 ;; No format string: Return raw text.
1447 ((nil) text)
1448 ;; Handle the `verb' special case: Find an appropriate separator
1449 ;; and use "\\verb" command.
1450 (verb
1451 (let ((separator (org-latex--find-verb-separator text)))
1452 (concat "\\verb" separator
1453 (replace-regexp-in-string "\n" " " text)
1454 separator)))
1455 ;; Handle the `protectedtexttt' special case: Protect some
1456 ;; special chars and use "\texttt{%s}" format string.
1457 (protectedtexttt
1458 (format "\\texttt{%s}" (org-latex--protect-text text)))
1459 ;; Else use format string.
1460 (t (format fmt text)))))
1462 (defun org-latex--delayed-footnotes-definitions (element info)
1463 "Return footnotes definitions in ELEMENT as a string.
1465 INFO is a plist used as a communication channel.
1467 Footnotes definitions are returned within \"\\footnotetxt{}\"
1468 commands.
1470 This function is used within constructs that don't support
1471 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1472 \"\\footnotemark\" is used within the construct and the function
1473 just outside of it."
1474 (mapconcat
1475 (lambda (ref)
1476 (format
1477 "\\footnotetext[%s]{%s}"
1478 (org-export-get-footnote-number ref info)
1479 (org-trim
1480 (org-export-data
1481 (org-export-get-footnote-definition ref info) info))))
1482 ;; Find every footnote reference in ELEMENT.
1483 (letrec ((all-refs)
1484 (search-refs
1485 (lambda (data)
1486 ;; Return a list of all footnote references never seen
1487 ;; before in DATA.
1488 (org-element-map data 'footnote-reference
1489 (lambda (ref)
1490 (when (org-export-footnote-first-reference-p ref info)
1491 (push ref all-refs)
1492 (when (eq (org-element-property :type ref) 'standard)
1493 (funcall search-refs
1494 (org-export-get-footnote-definition ref info)))))
1495 info)
1496 (reverse all-refs))))
1497 (funcall search-refs element))
1498 ""))
1500 (defun org-latex--translate (s info)
1501 "Translate string S according to specified language.
1502 INFO is a plist used as a communication channel."
1503 (org-export-translate s :latex info))
1505 (defun org-latex--format-spec (info)
1506 "Create a format-spec for document meta-data.
1507 INFO is a plist used as a communication channel."
1508 (let ((language (let ((lang (plist-get info :language)))
1509 (or (cdr (assoc lang org-latex-babel-language-alist))
1510 lang))))
1511 `((?a . ,(org-export-data (plist-get info :author) info))
1512 (?t . ,(org-export-data (plist-get info :title) info))
1513 (?k . ,(org-export-data (org-latex--wrap-latex-math-block
1514 (plist-get info :keywords) info)
1515 info))
1516 (?d . ,(org-export-data (org-latex--wrap-latex-math-block
1517 (plist-get info :description) info)
1518 info))
1519 (?c . ,(plist-get info :creator))
1520 (?l . ,language)
1521 (?L . ,(capitalize language))
1522 (?D . ,(org-export-get-date info)))))
1524 (defun org-latex--make-preamble (info)
1525 "Return a formatted LaTeX preamble.
1526 INFO is a plist used as a communication channel."
1527 (let* ((class (plist-get info :latex-class))
1528 (class-options (plist-get info :latex-class-options))
1529 (header (nth 1 (assoc class (plist-get info :latex-classes))))
1530 (document-class-string
1531 (and (stringp header)
1532 (if (not class-options) header
1533 (replace-regexp-in-string
1534 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1535 class-options header t nil 1)))))
1536 (if (not document-class-string)
1537 (user-error "Unknown LaTeX class `%s'" class)
1538 (org-latex-guess-polyglossia-language
1539 (org-latex-guess-babel-language
1540 (org-latex-guess-inputenc
1541 (org-element-normalize-string
1542 (org-splice-latex-header
1543 document-class-string
1544 (org-latex--remove-packages
1545 org-latex-default-packages-alist info)
1546 (org-latex--remove-packages
1547 org-latex-packages-alist info)
1549 (mapconcat 'org-element-normalize-string
1550 (list (plist-get info :latex-header)
1551 (plist-get info :latex-header-extra)) ""))))
1552 info)
1553 info))))
1555 (defun org-latex--insert-compiler (info)
1556 "Insert LaTeX_compiler info into the document.
1557 INFO is a plist used as a communication channel."
1558 (let ((compiler (plist-get info :latex-compiler)))
1559 (and (org-string-nw-p org-latex-compiler-file-string)
1560 (string-match-p (regexp-opt org-latex-compilers) (or compiler ""))
1561 (format org-latex-compiler-file-string compiler))))
1564 ;;; Template
1566 (defun org-latex-template (contents info)
1567 "Return complete document string after LaTeX conversion.
1568 CONTENTS is the transcoded contents string. INFO is a plist
1569 holding export options."
1570 (let ((title (org-export-data (plist-get info :title) info))
1571 (spec (org-latex--format-spec info)))
1572 (concat
1573 ;; Time-stamp.
1574 (and (plist-get info :time-stamp-file)
1575 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1576 ;; LaTeX compiler.
1577 (org-latex--insert-compiler info)
1578 ;; Document class and packages.
1579 (org-latex--make-preamble info)
1580 ;; Possibly limit depth for headline numbering.
1581 (let ((sec-num (plist-get info :section-numbers)))
1582 (when (integerp sec-num)
1583 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1584 ;; Author.
1585 (let ((author (and (plist-get info :with-author)
1586 (let ((auth (plist-get info :author)))
1587 (and auth (org-export-data auth info)))))
1588 (email (and (plist-get info :with-email)
1589 (org-export-data (plist-get info :email) info))))
1590 (cond ((and author email (not (string= "" email)))
1591 (format "\\author{%s\\thanks{%s}}\n" author email))
1592 ((or author email) (format "\\author{%s}\n" (or author email)))))
1593 ;; Date.
1594 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1595 (format "\\date{%s}\n" (org-export-data date info)))
1596 ;; Title and subtitle.
1597 (let* ((subtitle (plist-get info :subtitle))
1598 (formatted-subtitle
1599 (when subtitle
1600 (format (plist-get info :latex-subtitle-format)
1601 (org-export-data subtitle info))))
1602 (separate (plist-get info :latex-subtitle-separate)))
1603 (concat
1604 (format "\\title{%s%s}\n" title
1605 (if separate "" (or formatted-subtitle "")))
1606 (when (and separate subtitle)
1607 (concat formatted-subtitle "\n"))))
1608 ;; Hyperref options.
1609 (let ((template (plist-get info :latex-hyperref-template)))
1610 (and (stringp template)
1611 (format-spec template spec)))
1612 ;; Document start.
1613 "\\begin{document}\n\n"
1614 ;; Title command.
1615 (let* ((title-command (plist-get info :latex-title-command))
1616 (command (and (stringp title-command)
1617 (format-spec title-command spec))))
1618 (org-element-normalize-string
1619 (cond ((not (plist-get info :with-title)) nil)
1620 ((string= "" title) nil)
1621 ((not (stringp command)) nil)
1622 ((string-match "\\(?:[^%]\\|^\\)%s" command)
1623 (format command title))
1624 (t command))))
1625 ;; Table of contents.
1626 (let ((depth (plist-get info :with-toc)))
1627 (when depth
1628 (concat (when (wholenump depth)
1629 (format "\\setcounter{tocdepth}{%d}\n" depth))
1630 (plist-get info :latex-toc-command))))
1631 ;; Document's body.
1632 contents
1633 ;; Creator.
1634 (and (plist-get info :with-creator)
1635 (concat (plist-get info :creator) "\n"))
1636 ;; Document end.
1637 "\\end{document}")))
1641 ;;; Transcode Functions
1643 ;;;; Bold
1645 (defun org-latex-bold (_bold contents info)
1646 "Transcode BOLD from Org to LaTeX.
1647 CONTENTS is the text with bold markup. INFO is a plist holding
1648 contextual information."
1649 (org-latex--text-markup contents 'bold info))
1652 ;;;; Center Block
1654 (defun org-latex-center-block (center-block contents info)
1655 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1656 CONTENTS holds the contents of the center block. INFO is a plist
1657 holding contextual information."
1658 (org-latex--wrap-label
1659 center-block (format "\\begin{center}\n%s\\end{center}" contents) info))
1662 ;;;; Clock
1664 (defun org-latex-clock (clock _contents info)
1665 "Transcode a CLOCK element from Org to LaTeX.
1666 CONTENTS is nil. INFO is a plist holding contextual
1667 information."
1668 (concat
1669 "\\noindent"
1670 (format "\\textbf{%s} " org-clock-string)
1671 (format (plist-get info :latex-inactive-timestamp-format)
1672 (concat (org-timestamp-translate (org-element-property :value clock))
1673 (let ((time (org-element-property :duration clock)))
1674 (and time (format " (%s)" time)))))
1675 "\\\\"))
1678 ;;;; Code
1680 (defun org-latex-code (code _contents info)
1681 "Transcode a CODE object from Org to LaTeX.
1682 CONTENTS is nil. INFO is a plist used as a communication
1683 channel."
1684 (org-latex--text-markup (org-element-property :value code) 'code info))
1687 ;;;; Drawer
1689 (defun org-latex-drawer (drawer contents info)
1690 "Transcode a DRAWER element from Org to LaTeX.
1691 CONTENTS holds the contents of the block. INFO is a plist
1692 holding contextual information."
1693 (let* ((name (org-element-property :drawer-name drawer))
1694 (output (funcall (plist-get info :latex-format-drawer-function)
1695 name contents)))
1696 (org-latex--wrap-label drawer output info)))
1699 ;;;; Dynamic Block
1701 (defun org-latex-dynamic-block (dynamic-block contents info)
1702 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1703 CONTENTS holds the contents of the block. INFO is a plist
1704 holding contextual information. See `org-export-data'."
1705 (org-latex--wrap-label dynamic-block contents info))
1708 ;;;; Entity
1710 (defun org-latex-entity (entity _contents _info)
1711 "Transcode an ENTITY object from Org to LaTeX.
1712 CONTENTS are the definition itself. INFO is a plist holding
1713 contextual information."
1714 (org-element-property :latex entity))
1717 ;;;; Example Block
1719 (defun org-latex-example-block (example-block _contents info)
1720 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1721 CONTENTS is nil. INFO is a plist holding contextual
1722 information."
1723 (when (org-string-nw-p (org-element-property :value example-block))
1724 (let ((environment (or (org-export-read-attribute
1725 :attr_latex example-block :environment)
1726 "verbatim")))
1727 (org-latex--wrap-label
1728 example-block
1729 (format "\\begin{%s}\n%s\\end{%s}"
1730 environment
1731 (org-export-format-code-default example-block info)
1732 environment)
1733 info))))
1736 ;;;; Export Block
1738 (defun org-latex-export-block (export-block _contents _info)
1739 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1740 CONTENTS is nil. INFO is a plist holding contextual information."
1741 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1742 (org-remove-indentation (org-element-property :value export-block))))
1745 ;;;; Export Snippet
1747 (defun org-latex-export-snippet (export-snippet _contents _info)
1748 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1749 CONTENTS is nil. INFO is a plist holding contextual information."
1750 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1751 (org-element-property :value export-snippet)))
1754 ;;;; Fixed Width
1756 (defun org-latex-fixed-width (fixed-width _contents info)
1757 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1758 CONTENTS is nil. INFO is a plist holding contextual information."
1759 (org-latex--wrap-label
1760 fixed-width
1761 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1762 (org-remove-indentation
1763 (org-element-property :value fixed-width)))
1764 info))
1767 ;;;; Footnote Reference
1769 (defun org-latex-footnote-reference (footnote-reference _contents info)
1770 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1771 CONTENTS is nil. INFO is a plist holding contextual information."
1772 (concat
1773 ;; Insert separator between two footnotes in a row.
1774 (let ((prev (org-export-get-previous-element footnote-reference info)))
1775 (when (eq (org-element-type prev) 'footnote-reference)
1776 (plist-get info :latex-footnote-separator)))
1777 (cond
1778 ;; Use \footnotemark if the footnote has already been defined.
1779 ((not (org-export-footnote-first-reference-p footnote-reference info))
1780 (format "\\footnotemark[%s]{}"
1781 (org-export-get-footnote-number footnote-reference info)))
1782 ;; Use \footnotemark if reference is within another footnote
1783 ;; reference, footnote definition or table cell.
1784 ((org-element-lineage footnote-reference
1785 '(footnote-reference footnote-definition table-cell))
1786 "\\footnotemark")
1787 ;; Otherwise, define it with \footnote command.
1789 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1790 (concat
1791 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1792 ;; Retrieve all footnote references within the footnote and
1793 ;; add their definition after it, since LaTeX doesn't support
1794 ;; them inside.
1795 (org-latex--delayed-footnotes-definitions def info)))))))
1798 ;;;; Headline
1800 (defun org-latex-headline (headline contents info)
1801 "Transcode a HEADLINE element from Org to LaTeX.
1802 CONTENTS holds the contents of the headline. INFO is a plist
1803 holding contextual information."
1804 (unless (org-element-property :footnote-section-p headline)
1805 (let* ((class (plist-get info :latex-class))
1806 (level (org-export-get-relative-level headline info))
1807 (numberedp (org-export-numbered-headline-p headline info))
1808 (class-sectioning (assoc class (plist-get info :latex-classes)))
1809 ;; Section formatting will set two placeholders: one for
1810 ;; the title and the other for the contents.
1811 (section-fmt
1812 (let ((sec (if (functionp (nth 2 class-sectioning))
1813 (funcall (nth 2 class-sectioning) level numberedp)
1814 (nth (1+ level) class-sectioning))))
1815 (cond
1816 ;; No section available for that LEVEL.
1817 ((not sec) nil)
1818 ;; Section format directly returned by a function. Add
1819 ;; placeholder for contents.
1820 ((stringp sec) (concat sec "\n%s"))
1821 ;; (numbered-section . unnumbered-section)
1822 ((not (consp (cdr sec)))
1823 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1824 ;; (numbered-open numbered-close)
1825 ((= (length sec) 2)
1826 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1827 ;; (num-in num-out no-num-in no-num-out)
1828 ((= (length sec) 4)
1829 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1830 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1831 ;; Create a temporary export back-end that hard-codes
1832 ;; "\underline" within "\section" and alike.
1833 (section-back-end
1834 (org-export-create-backend
1835 :parent 'latex
1836 :transcoders
1837 '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
1838 (text
1839 (org-export-data-with-backend
1840 (org-element-property :title headline) section-back-end info))
1841 (todo
1842 (and (plist-get info :with-todo-keywords)
1843 (let ((todo (org-element-property :todo-keyword headline)))
1844 (and todo (org-export-data todo info)))))
1845 (todo-type (and todo (org-element-property :todo-type headline)))
1846 (tags (and (plist-get info :with-tags)
1847 (org-export-get-tags headline info)))
1848 (priority (and (plist-get info :with-priority)
1849 (org-element-property :priority headline)))
1850 ;; Create the headline text along with a no-tag version.
1851 ;; The latter is required to remove tags from toc.
1852 (full-text (funcall (plist-get info :latex-format-headline-function)
1853 todo todo-type priority text tags info))
1854 ;; Associate \label to the headline for internal links.
1855 (headline-label (org-latex--label headline info t t))
1856 (pre-blanks
1857 (make-string (org-element-property :pre-blank headline) ?\n)))
1858 (if (or (not section-fmt) (org-export-low-level-p headline info))
1859 ;; This is a deep sub-tree: export it as a list item. Also
1860 ;; export as items headlines for which no section format has
1861 ;; been found.
1862 (let ((low-level-body
1863 (concat
1864 ;; If headline is the first sibling, start a list.
1865 (when (org-export-first-sibling-p headline info)
1866 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1867 ;; Itemize headline
1868 "\\item"
1869 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
1870 "\\relax")
1871 " " full-text "\n"
1872 headline-label
1873 pre-blanks
1874 contents)))
1875 ;; If headline is not the last sibling simply return
1876 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1877 ;; any blank line.
1878 (if (not (org-export-last-sibling-p headline info)) low-level-body
1879 (replace-regexp-in-string
1880 "[ \t\n]*\\'"
1881 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1882 low-level-body)))
1883 ;; This is a standard headline. Export it as a section. Add
1884 ;; an alternative heading when possible, and when this is not
1885 ;; identical to the usual heading.
1886 (let ((opt-title
1887 (funcall (plist-get info :latex-format-headline-function)
1888 todo todo-type priority
1889 (org-export-data-with-backend
1890 (org-export-get-alt-title headline info)
1891 section-back-end info)
1892 (and (eq (plist-get info :with-tags) t) tags)
1893 info))
1894 ;; Maybe end local TOC (see `org-latex-keyword').
1895 (contents
1896 (concat
1897 contents
1898 (let ((case-fold-search t)
1899 (section
1900 (let ((first (car (org-element-contents headline))))
1901 (and (eq (org-element-type first) 'section) first))))
1902 (org-element-map section 'keyword
1903 (lambda (k)
1904 (and (equal (org-element-property :key k) "TOC")
1905 (let ((v (org-element-property :value k)))
1906 (and (org-string-match-p "\\<headlines\\>" v)
1907 (org-string-match-p "\\<local\\>" v)
1908 (format "\\stopcontents[level-%d]" level)))))
1909 info t)))))
1910 (if (and opt-title
1911 (not (equal opt-title full-text))
1912 (string-match "\\`\\\\\\(.+?\\){" section-fmt))
1913 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1914 ;; Replace square brackets with parenthesis
1915 ;; since square brackets are not supported in
1916 ;; optional arguments.
1917 (replace-regexp-in-string
1918 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1919 full-text
1920 (concat headline-label pre-blanks contents))
1921 ;; Impossible to add an alternative heading. Fallback to
1922 ;; regular sectioning format string.
1923 (format section-fmt full-text
1924 (concat headline-label pre-blanks contents))))))))
1926 (defun org-latex-format-headline-default-function
1927 (todo _todo-type priority text tags info)
1928 "Default format function for a headline.
1929 See `org-latex-format-headline-function' for details."
1930 (concat
1931 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1932 (and priority (format "\\framebox{\\#%c} " priority))
1933 text
1934 (and tags
1935 (format "\\hfill{}\\textsc{%s}"
1936 (mapconcat (lambda (tag) (org-latex-plain-text tag info))
1937 tags ":")))))
1940 ;;;; Horizontal Rule
1942 (defun org-latex-horizontal-rule (horizontal-rule _contents info)
1943 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1944 CONTENTS is nil. INFO is a plist holding contextual information."
1945 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1946 (prev (org-export-get-previous-element horizontal-rule info)))
1947 (concat
1948 ;; Make sure the rule doesn't start at the end of the current
1949 ;; line by separating it with a blank line from previous element.
1950 (when (and prev
1951 (let ((prev-blank (org-element-property :post-blank prev)))
1952 (or (not prev-blank) (zerop prev-blank))))
1953 "\n")
1954 (org-latex--wrap-label
1955 horizontal-rule
1956 (format "\\rule{%s}{%s}"
1957 (or (plist-get attr :width) "\\linewidth")
1958 (or (plist-get attr :thickness) "0.5pt"))
1959 info))))
1962 ;;;; Inline Src Block
1964 (defun org-latex-inline-src-block (inline-src-block _contents info)
1965 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1966 CONTENTS holds the contents of the item. INFO is a plist holding
1967 contextual information."
1968 (let* ((code (org-element-property :value inline-src-block))
1969 (separator (org-latex--find-verb-separator code)))
1970 (case (plist-get info :latex-listings)
1971 ;; Do not use a special package: transcode it verbatim.
1972 ((nil) (format "\\texttt{%s}" (org-latex--protect-text code)))
1973 ;; Use minted package.
1974 (minted
1975 (let* ((org-lang (org-element-property :language inline-src-block))
1976 (mint-lang (or (cadr (assq (intern org-lang)
1977 (plist-get info :latex-minted-langs)))
1978 (downcase org-lang)))
1979 (options (org-latex--make-option-string
1980 (plist-get info :latex-minted-options))))
1981 (concat (format "\\mint%s{%s}"
1982 (if (string= options "") "" (format "[%s]" options))
1983 mint-lang)
1984 separator code separator)))
1985 ;; Use listings package.
1986 (otherwise
1987 ;; Maybe translate language's name.
1988 (let* ((org-lang (org-element-property :language inline-src-block))
1989 (lst-lang (or (cadr (assq (intern org-lang)
1990 (plist-get info :latex-listings-langs)))
1991 org-lang))
1992 (options (org-latex--make-option-string
1993 (append (plist-get info :latex-listings-options)
1994 `(("language" ,lst-lang))))))
1995 (concat (format "\\lstinline[%s]" options)
1996 separator code separator))))))
1999 ;;;; Inlinetask
2001 (defun org-latex-inlinetask (inlinetask contents info)
2002 "Transcode an INLINETASK element from Org to LaTeX.
2003 CONTENTS holds the contents of the block. INFO is a plist
2004 holding contextual information."
2005 (let ((title (org-export-data (org-element-property :title inlinetask) info))
2006 (todo (and (plist-get info :with-todo-keywords)
2007 (let ((todo (org-element-property :todo-keyword inlinetask)))
2008 (and todo (org-export-data todo info)))))
2009 (todo-type (org-element-property :todo-type inlinetask))
2010 (tags (and (plist-get info :with-tags)
2011 (org-export-get-tags inlinetask info)))
2012 (priority (and (plist-get info :with-priority)
2013 (org-element-property :priority inlinetask)))
2014 (contents (concat (org-latex--label inlinetask info) contents)))
2015 (funcall (plist-get info :latex-format-inlinetask-function)
2016 todo todo-type priority title tags contents info)))
2018 (defun org-latex-format-inlinetask-default-function
2019 (todo _todo-type priority title tags contents info)
2020 "Default format function for a inlinetasks.
2021 See `org-latex-format-inlinetask-function' for details."
2022 (let ((full-title
2023 (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
2024 (when priority (format "\\framebox{\\#%c} " priority))
2025 title
2026 (when tags
2027 (format "\\hfill{}\\textsc{:%s:}"
2028 (mapconcat
2029 (lambda (tag) (org-latex-plain-text tag info))
2030 tags ":"))))))
2031 (concat "\\begin{center}\n"
2032 "\\fbox{\n"
2033 "\\begin{minipage}[c]{.6\\textwidth}\n"
2034 full-title "\n\n"
2035 (and (org-string-nw-p contents)
2036 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
2037 "\\end{minipage}\n"
2038 "}\n"
2039 "\\end{center}")))
2042 ;;;; Italic
2044 (defun org-latex-italic (_italic contents info)
2045 "Transcode ITALIC from Org to LaTeX.
2046 CONTENTS is the text with italic markup. INFO is a plist holding
2047 contextual information."
2048 (org-latex--text-markup contents 'italic info))
2051 ;;;; Item
2053 (defun org-latex-item (item contents info)
2054 "Transcode an ITEM element from Org to LaTeX.
2055 CONTENTS holds the contents of the item. INFO is a plist holding
2056 contextual information."
2057 (let* ((counter
2058 (let ((count (org-element-property :counter item))
2059 (level
2060 ;; Determine level of current item to determine the
2061 ;; correct LaTeX counter to use (enumi, enumii...).
2062 (let ((parent item) (level 0))
2063 (while (memq (org-element-type
2064 (setq parent (org-export-get-parent parent)))
2065 '(plain-list item))
2066 (when (and (eq (org-element-type parent) 'plain-list)
2067 (eq (org-element-property :type parent)
2068 'ordered))
2069 (incf level)))
2070 level)))
2071 (and count
2072 (< level 5)
2073 (format "\\setcounter{enum%s}{%s}\n"
2074 (nth (1- level) '("i" "ii" "iii" "iv"))
2075 (1- count)))))
2076 (checkbox (case (org-element-property :checkbox item)
2077 (on "$\\boxtimes$ ")
2078 (off "$\\square$ ")
2079 (trans "$\\boxminus$ ")))
2080 (tag (let ((tag (org-element-property :tag item)))
2081 ;; Check-boxes must belong to the tag.
2082 (and tag (format "[{%s}] "
2083 (concat checkbox
2084 (org-export-data tag info)))))))
2085 (concat counter
2086 "\\item"
2087 (cond
2088 (tag)
2089 (checkbox (concat " " checkbox))
2090 ;; Without a tag or a check-box, if CONTENTS starts with
2091 ;; an opening square bracket, add "\relax" to "\item",
2092 ;; unless the brackets comes from an initial export
2093 ;; snippet (i.e. it is inserted willingly by the user).
2094 ((and contents
2095 (org-string-match-p "\\`[ \t]*\\[" contents)
2096 (not (let ((e (car (org-element-contents item))))
2097 (and (eq (org-element-type e) 'paragraph)
2098 (let ((o (car (org-element-contents e))))
2099 (and (eq (org-element-type o) 'export-snippet)
2100 (eq (org-export-snippet-backend o)
2101 'latex)))))))
2102 "\\relax ")
2103 (t " "))
2104 (and contents (org-trim contents))
2105 ;; If there are footnotes references in tag, be sure to
2106 ;; add their definition at the end of the item. This
2107 ;; workaround is necessary since "\footnote{}" command is
2108 ;; not supported in tags.
2109 (and tag
2110 (org-latex--delayed-footnotes-definitions
2111 (org-element-property :tag item) info)))))
2114 ;;;; Keyword
2116 (defun org-latex-keyword (keyword _contents info)
2117 "Transcode a KEYWORD element from Org to LaTeX.
2118 CONTENTS is nil. INFO is a plist holding contextual information."
2119 (let ((key (org-element-property :key keyword))
2120 (value (org-element-property :value keyword)))
2121 (cond
2122 ((string= key "LATEX") value)
2123 ((string= key "INDEX") (format "\\index{%s}" value))
2124 ((string= key "TOC")
2125 (let ((case-fold-search t))
2126 (cond
2127 ((org-string-match-p "\\<headlines\\>" value)
2128 (let* ((localp (org-string-match-p "\\<local\\>" value))
2129 (parent (org-element-lineage keyword '(headline)))
2130 (level (if (not (and localp parent)) 0
2131 (org-export-get-relative-level parent info)))
2132 (depth
2133 (and (string-match "\\<[0-9]+\\>" value)
2134 (format
2135 "\\setcounter{tocdepth}{%d}"
2136 (+ (string-to-number (match-string 0 value)) level)))))
2137 (if (and localp parent)
2138 ;; Start local TOC, assuming package "titletoc" is
2139 ;; required.
2140 (format "\\startcontents[level-%d]
2141 \\printcontents[level-%d]{}{0}{%s}"
2142 level level (or depth ""))
2143 (concat depth (and depth "\n") "\\tableofcontents"))))
2144 ((org-string-match-p "\\<tables\\>" value) "\\listoftables")
2145 ((org-string-match-p "\\<listings\\>" value)
2146 (case (plist-get info :latex-listings)
2147 ((nil) "\\listoffigures")
2148 (minted "\\listoflistings")
2149 (otherwise "\\lstlistoflistings")))))))))
2152 ;;;; Latex Environment
2154 (defun org-latex-latex-environment (latex-environment _contents info)
2155 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
2156 CONTENTS is nil. INFO is a plist holding contextual information."
2157 (when (plist-get info :with-latex)
2158 (let ((value (org-remove-indentation
2159 (org-element-property :value latex-environment))))
2160 (if (not (org-element-property :name latex-environment)) value
2161 ;; Environment is labeled: label must be within the environment
2162 ;; (otherwise, a reference pointing to that element will count
2163 ;; the section instead).
2164 (with-temp-buffer
2165 (insert value)
2166 (goto-char (point-min))
2167 (forward-line)
2168 (insert (org-latex--label latex-environment info nil t))
2169 (buffer-string))))))
2172 ;;;; Latex Fragment
2174 (defun org-latex-latex-fragment (latex-fragment _contents _info)
2175 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
2176 CONTENTS is nil. INFO is a plist holding contextual information."
2177 (let ((value (org-element-property :value latex-fragment)))
2178 ;; Trim math markers since the fragment is enclosed within
2179 ;; a latex-math-block object anyway.
2180 (cond ((string-match-p "\\`\\$[^$]" value) (substring value 1 -1))
2181 ((string-prefix-p "\\(" value) (substring value 2 -2))
2182 (t value))))
2185 ;;;; Line Break
2187 (defun org-latex-line-break (_line-break _contents _info)
2188 "Transcode a LINE-BREAK object from Org to LaTeX.
2189 CONTENTS is nil. INFO is a plist holding contextual information."
2190 "\\\\\n")
2193 ;;;; Link
2195 (defun org-latex--inline-image (link info)
2196 "Return LaTeX code for an inline image.
2197 LINK is the link pointing to the inline image. INFO is a plist
2198 used as a communication channel."
2199 (let* ((parent (org-export-get-parent-element link))
2200 (path (let ((raw-path (org-element-property :path link)))
2201 (if (not (file-name-absolute-p raw-path)) raw-path
2202 (expand-file-name raw-path))))
2203 (filetype (file-name-extension path))
2204 (caption (org-latex--caption/label-string parent info))
2205 (caption-above-p (org-latex--caption-above-p link info))
2206 ;; Retrieve latex attributes from the element around.
2207 (attr (org-export-read-attribute :attr_latex parent))
2208 (float (let ((float (plist-get attr :float)))
2209 (cond ((string= float "wrap") 'wrap)
2210 ((string= float "sideways") 'sideways)
2211 ((string= float "multicolumn") 'multicolumn)
2212 ((or float
2213 (org-element-property :caption parent)
2214 (org-string-nw-p (plist-get attr :caption)))
2215 (if (and (plist-member attr :float) (not float))
2216 'nonfloat
2217 'figure))
2218 ((and (not float) (plist-member attr :float)) nil))))
2219 (placement
2220 (let ((place (plist-get attr :placement)))
2221 (cond
2222 (place (format "%s" place))
2223 ((eq float 'wrap) "{l}{0.5\\textwidth}")
2224 ((eq float 'figure)
2225 (format "[%s]" (plist-get info :latex-default-figure-position)))
2226 (t ""))))
2227 (comment-include (if (plist-get attr :comment-include) "%" ""))
2228 ;; It is possible to specify width and height in the
2229 ;; ATTR_LATEX line, and also via default variables.
2230 (width (cond ((plist-get attr :width))
2231 ((plist-get attr :height) "")
2232 ((eq float 'wrap) "0.48\\textwidth")
2233 (t (plist-get info :latex-image-default-width))))
2234 (height (cond ((plist-get attr :height))
2235 ((or (plist-get attr :width)
2236 (memq float '(figure wrap))) "")
2237 (t (plist-get info :latex-image-default-height))))
2238 (options (let ((opt (or (plist-get attr :options)
2239 (plist-get info :latex-image-default-option))))
2240 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
2241 (match-string 1 opt))))
2242 image-code)
2243 (if (member filetype '("tikz" "pgf"))
2244 ;; For tikz images:
2245 ;; - use \input to read in image file.
2246 ;; - if options are present, wrap in a tikzpicture environment.
2247 ;; - if width or height are present, use \resizebox to change
2248 ;; the image size.
2249 (progn
2250 (setq image-code (format "\\input{%s}" path))
2251 (when (org-string-nw-p options)
2252 (setq image-code
2253 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
2254 options
2255 image-code)))
2256 (when (or (org-string-nw-p width) (org-string-nw-p height))
2257 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
2258 (if (org-string-nw-p width) width "!")
2259 (if (org-string-nw-p height) height "!")
2260 image-code))))
2261 ;; For other images:
2262 ;; - add width and height to options.
2263 ;; - include the image with \includegraphics.
2264 (when (org-string-nw-p width)
2265 (setq options (concat options ",width=" width)))
2266 (when (org-string-nw-p height)
2267 (setq options (concat options ",height=" height)))
2268 (let ((search-option (org-element-property :search-option link)))
2269 (when (and search-option
2270 (equal filetype "pdf")
2271 (org-string-match-p "\\`[0-9]+\\'" search-option)
2272 (not (org-string-match-p "page=" options)))
2273 (setq options (concat options ",page=" search-option))))
2274 (setq image-code
2275 (format "\\includegraphics%s{%s}"
2276 (cond ((not (org-string-nw-p options)) "")
2277 ((= (aref options 0) ?,)
2278 (format "[%s]"(substring options 1)))
2279 (t (format "[%s]" options)))
2280 path))
2281 (when (equal filetype "svg")
2282 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
2283 "\\includesvg"
2284 image-code
2285 nil t))
2286 (setq image-code (replace-regexp-in-string "\\.svg}"
2288 image-code
2289 nil t))))
2290 ;; Return proper string, depending on FLOAT.
2291 (case float
2292 (wrap (format "\\begin{wrapfigure}%s
2293 %s\\centering
2294 %s%s
2295 %s\\end{wrapfigure}"
2296 placement
2297 (if caption-above-p caption "")
2298 comment-include image-code
2299 (if caption-above-p "" caption)))
2300 (sideways (format "\\begin{sidewaysfigure}
2301 %s\\centering
2302 %s%s
2303 %s\\end{sidewaysfigure}"
2304 (if caption-above-p caption "")
2305 comment-include image-code
2306 (if caption-above-p "" caption)))
2307 (multicolumn (format "\\begin{figure*}%s
2308 %s\\centering
2309 %s%s
2310 %s\\end{figure*}"
2311 placement
2312 (if caption-above-p caption "")
2313 comment-include image-code
2314 (if caption-above-p "" caption)))
2315 (figure (format "\\begin{figure}%s
2316 %s\\centering
2317 %s%s
2318 %s\\end{figure}"
2319 placement
2320 (if caption-above-p caption "")
2321 comment-include image-code
2322 (if caption-above-p "" caption)))
2323 (nonfloat
2324 (format "\\begin{center}
2325 %s%s
2326 %s\\end{center}"
2327 (if caption-above-p caption "")
2328 image-code
2329 (if caption-above-p "" caption)))
2330 (otherwise image-code))))
2332 (defun org-latex-link (link desc info)
2333 "Transcode a LINK object from Org to LaTeX.
2335 DESC is the description part of the link, or the empty string.
2336 INFO is a plist holding contextual information. See
2337 `org-export-data'."
2338 (let* ((type (org-element-property :type link))
2339 (raw-path (replace-regexp-in-string
2340 "%" "\\%" (org-element-property :path link) nil t))
2341 ;; Ensure DESC really exists, or set it to nil.
2342 (desc (and (not (string= desc "")) desc))
2343 (imagep (org-export-inline-image-p
2344 link (plist-get info :latex-inline-image-rules)))
2345 (path (cond
2346 ((member type '("http" "https" "ftp" "mailto" "doi"))
2347 (concat type ":" raw-path))
2348 ((string= type "file") (org-export-file-uri raw-path))
2349 (t raw-path))))
2350 (cond
2351 ;; Link type is handled by a special function.
2352 ((org-export-custom-protocol-maybe link desc 'latex))
2353 ;; Image file.
2354 (imagep (org-latex--inline-image link info))
2355 ;; Radio link: Transcode target's contents and use them as link's
2356 ;; description.
2357 ((string= type "radio")
2358 (let ((destination (org-export-resolve-radio-link link info)))
2359 (if (not destination) desc
2360 (format "\\hyperref[%s]{%s}"
2361 (org-export-get-reference destination info)
2362 desc))))
2363 ;; Links pointing to a headline: Find destination and build
2364 ;; appropriate referencing command.
2365 ((member type '("custom-id" "fuzzy" "id"))
2366 (let ((destination (if (string= type "fuzzy")
2367 (org-export-resolve-fuzzy-link link info)
2368 (org-export-resolve-id-link link info))))
2369 (case (org-element-type destination)
2370 ;; Id link points to an external file.
2371 (plain-text
2372 (if desc (format "\\href{%s}{%s}" destination desc)
2373 (format "\\url{%s}" destination)))
2374 ;; Fuzzy link points nowhere.
2375 ((nil)
2376 (format (plist-get info :latex-link-with-unknown-path-format)
2377 (or desc
2378 (org-export-data
2379 (org-element-property :raw-link link) info))))
2380 ;; LINK points to a headline. If headlines are numbered
2381 ;; and the link has no description, display headline's
2382 ;; number. Otherwise, display description or headline's
2383 ;; title.
2384 (headline
2385 (let ((label (org-latex--label destination info t)))
2386 (if (and (not desc)
2387 (org-export-numbered-headline-p destination info))
2388 (format "\\ref{%s}" label)
2389 (format "\\hyperref[%s]{%s}" label
2390 (or desc
2391 (org-export-data
2392 (org-element-property :title destination) info))))))
2393 ;; Fuzzy link points to a target. Do as above.
2394 (otherwise
2395 (let ((ref (org-latex--label destination info t)))
2396 (if (not desc) (format "\\ref{%s}" ref)
2397 (format "\\hyperref[%s]{%s}" ref desc)))))))
2398 ;; Coderef: replace link with the reference name or the
2399 ;; equivalent line number.
2400 ((string= type "coderef")
2401 (format (org-export-get-coderef-format path desc)
2402 (org-export-resolve-coderef path info)))
2403 ;; External link with a description part.
2404 ((and path desc) (format "\\href{%s}{%s}" path desc))
2405 ;; External link without a description part.
2406 (path (format "\\url{%s}" path))
2407 ;; No path, only description. Try to do something useful.
2408 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
2411 ;;;; Node Property
2413 (defun org-latex-node-property (node-property _contents _info)
2414 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2415 CONTENTS is nil. INFO is a plist holding contextual
2416 information."
2417 (format "%s:%s"
2418 (org-element-property :key node-property)
2419 (let ((value (org-element-property :value node-property)))
2420 (if value (concat " " value) ""))))
2423 ;;;; Paragraph
2425 (defun org-latex-paragraph (_paragraph contents _info)
2426 "Transcode a PARAGRAPH element from Org to LaTeX.
2427 CONTENTS is the contents of the paragraph, as a string. INFO is
2428 the plist used as a communication channel."
2429 contents)
2432 ;;;; Plain List
2434 (defun org-latex-plain-list (plain-list contents info)
2435 "Transcode a PLAIN-LIST element from Org to LaTeX.
2436 CONTENTS is the contents of the list. INFO is a plist holding
2437 contextual information."
2438 (let* ((type (org-element-property :type plain-list))
2439 (attr (org-export-read-attribute :attr_latex plain-list))
2440 (latex-type (let ((env (plist-get attr :environment)))
2441 (cond (env (format "%s" env))
2442 ((eq type 'ordered) "enumerate")
2443 ((eq type 'descriptive) "description")
2444 (t "itemize")))))
2445 (org-latex--wrap-label
2446 plain-list
2447 (format "\\begin{%s}%s\n%s\\end{%s}"
2448 latex-type
2449 (or (plist-get attr :options) "")
2450 contents
2451 latex-type)
2452 info)))
2455 ;;;; Plain Text
2457 (defun org-latex-plain-text (text info)
2458 "Transcode a TEXT string from Org to LaTeX.
2459 TEXT is the string to transcode. INFO is a plist holding
2460 contextual information."
2461 (let* ((specialp (plist-get info :with-special-strings))
2462 (output
2463 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2464 (let ((case-fold-search nil))
2465 (replace-regexp-in-string
2466 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2467 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2468 ;; However, if special strings are used, be careful not
2469 ;; to protect "\" in "\-" constructs.
2470 (replace-regexp-in-string
2471 (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\([^-]\\|$\\)"))
2472 (lambda (m)
2473 (case (string-to-char m)
2474 (?\\ "$\\\\backslash$\\1")
2475 (?~ "\\\\textasciitilde{}")
2476 (?^ "\\\\^{}")
2477 (t "\\\\\\&")))
2478 text)))))
2479 ;; Activate smart quotes. Be sure to provide original TEXT string
2480 ;; since OUTPUT may have been modified.
2481 (when (plist-get info :with-smart-quotes)
2482 (setq output (org-export-activate-smart-quotes output :latex info text)))
2483 ;; Convert special strings.
2484 (when specialp
2485 (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
2486 ;; Handle break preservation if required.
2487 (when (plist-get info :preserve-breaks)
2488 (setq output (replace-regexp-in-string
2489 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
2490 ;; Return value.
2491 output))
2494 ;;;; Planning
2496 (defun org-latex-planning (planning _contents info)
2497 "Transcode a PLANNING element from Org to LaTeX.
2498 CONTENTS is nil. INFO is a plist holding contextual
2499 information."
2500 (concat
2501 "\\noindent"
2502 (mapconcat
2503 'identity
2504 (delq nil
2505 (list
2506 (let ((closed (org-element-property :closed planning)))
2507 (when closed
2508 (concat
2509 (format "\\textbf{%s} " org-closed-string)
2510 (format (plist-get info :latex-inactive-timestamp-format)
2511 (org-timestamp-translate closed)))))
2512 (let ((deadline (org-element-property :deadline planning)))
2513 (when deadline
2514 (concat
2515 (format "\\textbf{%s} " org-deadline-string)
2516 (format (plist-get info :latex-active-timestamp-format)
2517 (org-timestamp-translate deadline)))))
2518 (let ((scheduled (org-element-property :scheduled planning)))
2519 (when scheduled
2520 (concat
2521 (format "\\textbf{%s} " org-scheduled-string)
2522 (format (plist-get info :latex-active-timestamp-format)
2523 (org-timestamp-translate scheduled)))))))
2524 " ")
2525 "\\\\"))
2528 ;;;; Property Drawer
2530 (defun org-latex-property-drawer (_property-drawer contents _info)
2531 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2532 CONTENTS holds the contents of the drawer. INFO is a plist
2533 holding contextual information."
2534 (and (org-string-nw-p contents)
2535 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2538 ;;;; Pseudo Element: LaTeX Matrices
2540 ;; `latex-matrices' elements have the following properties:
2541 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2542 ;; `math').
2544 (defun org-latex--wrap-latex-matrices (data info)
2545 "Merge contiguous tables with the same mode within a pseudo-element.
2546 DATA is a parse tree or a secondary string. INFO is a plist
2547 containing export options. Modify DATA by side-effect and return
2548 it."
2549 (org-element-map data 'table
2550 (lambda (table)
2551 (when (eq (org-element-property :type table) 'org)
2552 (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
2553 (plist-get info :latex-default-table-mode))))
2554 (when (and (member mode '("inline-math" "math"))
2555 ;; Do not wrap twice the same table.
2556 (not (eq (org-element-type
2557 (org-element-property :parent table))
2558 'latex-matrices)))
2559 (let* ((caption (and (not (string= mode "inline-math"))
2560 (org-element-property :caption table)))
2561 (matrices
2562 (list 'latex-matrices
2563 (list :caption caption
2564 :markup
2565 (cond ((string= mode "inline-math") 'inline)
2566 (caption 'equation)
2567 (t 'math)))))
2568 (previous table)
2569 (next (org-export-get-next-element table info)))
2570 (org-element-insert-before matrices table)
2571 ;; Swallow all contiguous tables sharing the same mode.
2572 (while (and
2573 (zerop (or (org-element-property :post-blank previous) 0))
2574 (setq next (org-export-get-next-element previous info))
2575 (eq (org-element-type next) 'table)
2576 (eq (org-element-property :type next) 'org)
2577 (string= (or (org-export-read-attribute
2578 :attr_latex next :mode)
2579 (plist-get info :latex-default-table-mode))
2580 mode))
2581 (org-element-extract-element previous)
2582 (org-element-adopt-elements matrices previous)
2583 (setq previous next))
2584 (org-element-put-property
2585 matrices :post-blank (org-element-property :post-blank previous))
2586 (org-element-extract-element previous)
2587 (org-element-adopt-elements matrices previous))))))
2588 info)
2589 data)
2591 (defun org-latex-matrices (matrices contents _info)
2592 "Transcode a MATRICES element from Org to LaTeX.
2593 CONTENTS is a string. INFO is a plist used as a communication
2594 channel."
2595 (format (case (org-element-property :markup matrices)
2596 (inline "\\(%s\\)")
2597 (equation "\\begin{equation}\n%s\\end{equation}")
2598 (t "\\[\n%s\\]"))
2599 contents))
2601 (defun org-latex-matrices-tree-filter (tree _backend info)
2602 (org-latex--wrap-latex-matrices tree info))
2604 ;;;; Pseudo Object: LaTeX Math Block
2606 ;; `latex-math-block' objects have the following property:
2607 ;; `:post-blank'.
2609 (defun org-latex--wrap-latex-math-block (data info)
2610 "Merge contiguous math objects in a pseudo-object container.
2611 DATA is a parse tree or a secondary string. INFO is a plist
2612 containing export options. Modify DATA by side-effect and return it."
2613 (let ((valid-object-p
2614 ;; Non-nil when OBJ can be added to the latex math block.
2615 (lambda (obj)
2616 (pcase (org-element-type obj)
2617 (`entity (org-element-property :latex-math-p obj))
2618 (`latex-fragment
2619 (let ((value (org-element-property :value obj)))
2620 (or (string-prefix-p "\\(" value)
2621 (string-match-p "\\`\\$[^$]" value))))
2622 ((or `subscript `superscript) t)))))
2623 (org-element-map data '(entity latex-fragment subscript superscript)
2624 (lambda (object)
2625 ;; Skip objects already wrapped.
2626 (when (and (not (eq (org-element-type
2627 (org-element-property :parent object))
2628 'latex-math-block))
2629 (funcall valid-object-p object))
2630 (let ((math-block (list 'latex-math-block nil))
2631 (next-elements (org-export-get-next-element object info t))
2632 (last object))
2633 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2634 (org-element-insert-before math-block object)
2635 (org-element-extract-element object)
2636 (org-element-adopt-elements math-block object)
2637 (when (zerop (or (org-element-property :post-blank object) 0))
2638 ;; MATH-BLOCK swallows consecutive math objects.
2639 (catch 'exit
2640 (dolist (next next-elements)
2641 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2642 (org-element-extract-element next)
2643 (org-element-adopt-elements math-block next)
2644 ;; Eschew the case: \beta$x$ -> \(\betax\).
2645 (unless (memq (org-element-type next)
2646 '(subscript superscript))
2647 (org-element-put-property last :post-blank 1))
2648 (setq last next)
2649 (when (> (or (org-element-property :post-blank next) 0) 0)
2650 (throw 'exit nil))))))
2651 (org-element-put-property
2652 math-block :post-blank (org-element-property :post-blank last)))))
2653 info nil '(subscript superscript latex-math-block) t)
2654 ;; Return updated DATA.
2655 data))
2657 (defun org-latex-math-block-tree-filter (tree _backend info)
2658 (org-latex--wrap-latex-math-block tree info))
2660 (defun org-latex-math-block-options-filter (info _backend)
2661 (dolist (prop '(:author :date :title) info)
2662 (plist-put info prop
2663 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2665 (defun org-latex-math-block (_math-block contents _info)
2666 "Transcode a MATH-BLOCK object from Org to LaTeX.
2667 CONTENTS is a string. INFO is a plist used as a communication
2668 channel."
2669 (when (org-string-nw-p contents)
2670 (format "\\(%s\\)" (org-trim contents))))
2672 ;;;; Quote Block
2674 (defun org-latex-quote-block (quote-block contents info)
2675 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2676 CONTENTS holds the contents of the block. INFO is a plist
2677 holding contextual information."
2678 (org-latex--wrap-label
2679 quote-block (format "\\begin{quote}\n%s\\end{quote}" contents) info))
2682 ;;;; Radio Target
2684 (defun org-latex-radio-target (radio-target text info)
2685 "Transcode a RADIO-TARGET object from Org to LaTeX.
2686 TEXT is the text of the target. INFO is a plist holding
2687 contextual information."
2688 (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
2691 ;;;; Section
2693 (defun org-latex-section (_section contents _info)
2694 "Transcode a SECTION element from Org to LaTeX.
2695 CONTENTS holds the contents of the section. INFO is a plist
2696 holding contextual information."
2697 contents)
2700 ;;;; Special Block
2702 (defun org-latex-special-block (special-block contents info)
2703 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2704 CONTENTS holds the contents of the block. INFO is a plist
2705 holding contextual information."
2706 (let ((type (org-element-property :type special-block))
2707 (opt (org-export-read-attribute :attr_latex special-block :options))
2708 (caption (org-latex--caption/label-string special-block info))
2709 (caption-above-p (org-latex--caption-above-p special-block info)))
2710 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2711 (and caption-above-p caption)
2712 contents
2713 (and (not caption-above-p) caption)
2714 (format "\\end{%s}" type))))
2717 ;;;; Src Block
2719 (defun org-latex-src-block (src-block _contents info)
2720 "Transcode a SRC-BLOCK element from Org to LaTeX.
2721 CONTENTS holds the contents of the item. INFO is a plist holding
2722 contextual information."
2723 (when (org-string-nw-p (org-element-property :value src-block))
2724 (let* ((lang (org-element-property :language src-block))
2725 (caption (org-element-property :caption src-block))
2726 (caption-above-p (org-latex--caption-above-p src-block info))
2727 (label (org-element-property :name src-block))
2728 (custom-env (and lang
2729 (cadr (assq (intern lang)
2730 org-latex-custom-lang-environments))))
2731 (num-start (case (org-element-property :number-lines src-block)
2732 (continued (org-export-get-loc src-block info))
2733 (new 0)))
2734 (retain-labels (org-element-property :retain-labels src-block))
2735 (attributes (org-export-read-attribute :attr_latex src-block))
2736 (float (plist-get attributes :float))
2737 (listings (plist-get info :latex-listings)))
2738 (cond
2739 ;; Case 1. No source fontification.
2740 ((not listings)
2741 (let* ((caption-str (org-latex--caption/label-string src-block info))
2742 (float-env
2743 (cond ((string= "multicolumn" float)
2744 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2745 (plist-get info :latex-default-figure-position)
2746 (if caption-above-p caption-str "")
2747 (if caption-above-p "" caption-str)))
2748 (caption (concat
2749 (if caption-above-p caption-str "")
2750 "%s"
2751 (if caption-above-p "" (concat "\n" caption-str))))
2752 (t "%s"))))
2753 (format
2754 float-env
2755 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2756 (org-export-format-code-default src-block info))))))
2757 ;; Case 2. Custom environment.
2758 (custom-env
2759 (let ((caption-str (org-latex--caption/label-string src-block info)))
2760 (format "\\begin{%s}\n%s\\end{%s}\n"
2761 custom-env
2762 (concat (and caption-above-p caption-str)
2763 (org-export-format-code-default src-block info)
2764 (and (not caption-above-p) caption-str))
2765 custom-env)))
2766 ;; Case 3. Use minted package.
2767 ((eq listings 'minted)
2768 (let* ((caption-str (org-latex--caption/label-string src-block info))
2769 (float-env
2770 (cond
2771 ((string= "multicolumn" float)
2772 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2773 (if caption-above-p caption-str "")
2774 (if caption-above-p "" caption-str)))
2775 (caption
2776 (concat (if caption-above-p caption-str "")
2777 "%s"
2778 (if caption-above-p "" (concat "\n" caption-str))))
2779 (t "%s")))
2780 (options (plist-get info :latex-minted-options))
2781 (body
2782 (format
2783 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2784 ;; Options.
2785 (concat
2786 (org-latex--make-option-string
2787 (if (or (not num-start) (assoc "linenos" options))
2788 options
2789 (append
2790 `(("linenos")
2791 ("firstnumber" ,(number-to-string (1+ num-start))))
2792 options)))
2793 (let ((local-options (plist-get attributes :options)))
2794 (and local-options (concat "," local-options))))
2795 ;; Language.
2796 (or (cadr (assq (intern lang)
2797 (plist-get info :latex-minted-langs)))
2798 (downcase lang))
2799 ;; Source code.
2800 (let* ((code-info (org-export-unravel-code src-block))
2801 (max-width
2802 (apply 'max
2803 (mapcar 'length
2804 (org-split-string (car code-info)
2805 "\n")))))
2806 (org-export-format-code
2807 (car code-info)
2808 (lambda (loc _num ref)
2809 (concat
2811 (when ref
2812 ;; Ensure references are flushed to the right,
2813 ;; separated with 6 spaces from the widest line
2814 ;; of code.
2815 (concat (make-string (+ (- max-width (length loc)) 6)
2816 ?\s)
2817 (format "(%s)" ref)))))
2818 nil (and retain-labels (cdr code-info)))))))
2819 ;; Return value.
2820 (format float-env body)))
2821 ;; Case 4. Use listings package.
2823 (let ((lst-lang
2824 (or (cadr (assq (intern lang)
2825 (plist-get info :latex-listings-langs)))
2826 lang))
2827 (caption-str
2828 (when caption
2829 (let ((main (org-export-get-caption src-block))
2830 (secondary (org-export-get-caption src-block t)))
2831 (if (not secondary)
2832 (format "{%s}" (org-export-data main info))
2833 (format "{[%s]%s}"
2834 (org-export-data secondary info)
2835 (org-export-data main info))))))
2836 (lst-opt (plist-get info :latex-listings-options)))
2837 (concat
2838 ;; Options.
2839 (format
2840 "\\lstset{%s}\n"
2841 (concat
2842 (org-latex--make-option-string
2843 (append
2844 lst-opt
2845 (cond
2846 ((and (not float) (plist-member attributes :float)) nil)
2847 ((string= "multicolumn" float) '(("float" "*")))
2848 ((and float (not (assoc "float" lst-opt)))
2849 `(("float" ,(plist-get info :latex-default-figure-position)))))
2850 `(("language" ,lst-lang))
2851 (if label `(("label" ,label)) '(("label" " ")))
2852 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2853 `(("captionpos" ,(if caption-above-p "t" "b")))
2854 (cond ((assoc "numbers" lst-opt) nil)
2855 ((not num-start) '(("numbers" "none")))
2856 ((zerop num-start) '(("numbers" "left")))
2857 (t `(("firstnumber" ,(number-to-string (1+ num-start)))
2858 ("numbers" "left"))))))
2859 (let ((local-options (plist-get attributes :options)))
2860 (and local-options (concat "," local-options)))))
2861 ;; Source code.
2862 (format
2863 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2864 (let* ((code-info (org-export-unravel-code src-block))
2865 (max-width
2866 (apply 'max
2867 (mapcar 'length
2868 (org-split-string (car code-info) "\n")))))
2869 (org-export-format-code
2870 (car code-info)
2871 (lambda (loc _num ref)
2872 (concat
2874 (when ref
2875 ;; Ensure references are flushed to the right,
2876 ;; separated with 6 spaces from the widest line of
2877 ;; code
2878 (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
2879 (format "(%s)" ref)))))
2880 nil (and retain-labels (cdr code-info))))))))))))
2883 ;;;; Statistics Cookie
2885 (defun org-latex-statistics-cookie (statistics-cookie _contents _info)
2886 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2887 CONTENTS is nil. INFO is a plist holding contextual information."
2888 (replace-regexp-in-string
2889 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2892 ;;;; Strike-Through
2894 (defun org-latex-strike-through (_strike-through contents info)
2895 "Transcode STRIKE-THROUGH from Org to LaTeX.
2896 CONTENTS is the text with strike-through markup. INFO is a plist
2897 holding contextual information."
2898 (org-latex--text-markup contents 'strike-through info))
2901 ;;;; Subscript
2903 (defun org-latex--script-size (object info)
2904 "Transcode a subscript or superscript object.
2905 OBJECT is an Org object. INFO is a plist used as a communication
2906 channel."
2907 (let ((output ""))
2908 (org-element-map (org-element-contents object)
2909 (cons 'plain-text org-element-all-objects)
2910 (lambda (obj)
2911 (case (org-element-type obj)
2912 ((entity latex-fragment)
2913 (let ((data (org-trim (org-export-data obj info))))
2914 (string-match
2915 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2916 data)
2917 (setq output
2918 (concat output
2919 (match-string 1 data)
2920 (let ((blank (org-element-property :post-blank obj)))
2921 (and blank (> blank 0) "\\ "))))))
2922 (plain-text
2923 (setq output
2924 (format "%s\\text{%s}" output (org-export-data obj info))))
2925 (otherwise
2926 (setq output
2927 (concat output
2928 (org-export-data obj info)
2929 (let ((blank (org-element-property :post-blank obj)))
2930 (and blank (> blank 0) "\\ ")))))))
2931 info nil org-element-recursive-objects)
2932 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2933 ;; character.
2934 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2935 (and (> (length output) 1) "{")
2936 output
2937 (and (> (length output) 1) "}"))))
2939 (defun org-latex-subscript (subscript _contents info)
2940 "Transcode a SUBSCRIPT object from Org to LaTeX.
2941 CONTENTS is the contents of the object. INFO is a plist holding
2942 contextual information."
2943 (org-latex--script-size subscript info))
2946 ;;;; Superscript
2948 (defun org-latex-superscript (superscript _contents info)
2949 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2950 CONTENTS is the contents of the object. INFO is a plist holding
2951 contextual information."
2952 (org-latex--script-size superscript info))
2955 ;;;; Table
2957 ;; `org-latex-table' is the entry point for table transcoding. It
2958 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2959 ;; delegates the job to either `org-latex--table.el-table',
2960 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2961 ;; depending of the type of the table and the mode requested.
2963 ;; `org-latex--align-string' is a subroutine used to build alignment
2964 ;; string for Org tables.
2966 (defun org-latex-table (table contents info)
2967 "Transcode a TABLE element from Org to LaTeX.
2968 CONTENTS is the contents of the table. INFO is a plist holding
2969 contextual information."
2970 (if (eq (org-element-property :type table) 'table.el)
2971 ;; "table.el" table. Convert it using appropriate tools.
2972 (org-latex--table.el-table table info)
2973 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2974 (plist-get info :latex-default-table-mode))))
2975 (cond
2976 ;; Case 1: Verbatim table.
2977 ((string= type "verbatim")
2978 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2979 ;; Re-create table, without affiliated keywords.
2980 (org-trim (org-element-interpret-data
2981 `(table nil ,@(org-element-contents table))))))
2982 ;; Case 2: Matrix.
2983 ((or (string= type "math") (string= type "inline-math"))
2984 (org-latex--math-table table info))
2985 ;; Case 3: Standard table.
2986 (t (concat (org-latex--org-table table contents info)
2987 ;; When there are footnote references within the
2988 ;; table, insert their definition just after it.
2989 (org-latex--delayed-footnotes-definitions table info)))))))
2991 (defun org-latex--align-string (table info)
2992 "Return an appropriate LaTeX alignment string.
2993 TABLE is the considered table. INFO is a plist used as
2994 a communication channel."
2995 (or (org-export-read-attribute :attr_latex table :align)
2996 (let (align)
2997 ;; Extract column groups and alignment from first (non-rule)
2998 ;; row.
2999 (org-element-map
3000 (org-element-map table 'table-row
3001 (lambda (row)
3002 (and (eq (org-element-property :type row) 'standard) row))
3003 info 'first-match)
3004 'table-cell
3005 (lambda (cell)
3006 (let ((borders (org-export-table-cell-borders cell info)))
3007 ;; Check left border for the first cell only.
3008 (when (and (memq 'left borders) (not align))
3009 (push "|" align))
3010 (push (case (org-export-table-cell-alignment cell info)
3011 (left "l")
3012 (right "r")
3013 (center "c"))
3014 align)
3015 (when (memq 'right borders) (push "|" align))))
3016 info)
3017 (apply 'concat (nreverse align)))))
3019 (defun org-latex--org-table (table contents info)
3020 "Return appropriate LaTeX code for an Org table.
3022 TABLE is the table type element to transcode. CONTENTS is its
3023 contents, as a string. INFO is a plist used as a communication
3024 channel.
3026 This function assumes TABLE has `org' as its `:type' property and
3027 `table' as its `:mode' attribute."
3028 (let* ((caption (org-latex--caption/label-string table info))
3029 (attr (org-export-read-attribute :attr_latex table))
3030 ;; Determine alignment string.
3031 (alignment (org-latex--align-string table info))
3032 ;; Determine environment for the table: longtable, tabular...
3033 (table-env (or (plist-get attr :environment)
3034 (plist-get info :latex-default-table-environment)))
3035 ;; If table is a float, determine environment: table, table*
3036 ;; or sidewaystable.
3037 (float-env (unless (member table-env '("longtable" "longtabu"))
3038 (let ((float (plist-get attr :float)))
3039 (cond
3040 ((and (not float) (plist-member attr :float)) nil)
3041 ((or (string= float "sidewaystable")
3042 (string= float "sideways")) "sidewaystable")
3043 ((string= float "multicolumn") "table*")
3044 ((or float
3045 (org-element-property :caption table)
3046 (org-string-nw-p (plist-get attr :caption)))
3047 "table")))))
3048 ;; Extract others display options.
3049 (fontsize (let ((font (plist-get attr :font)))
3050 (and font (concat font "\n"))))
3051 ;; "tabular" environment doesn't allow to define a width.
3052 (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
3053 (spreadp (plist-get attr :spread))
3054 (placement
3055 (or (plist-get attr :placement)
3056 (format "[%s]" (plist-get info :latex-default-figure-position))))
3057 (centerp (if (plist-member attr :center) (plist-get attr :center)
3058 (plist-get info :latex-tables-centered)))
3059 (caption-above-p (org-latex--caption-above-p table info)))
3060 ;; Prepare the final format string for the table.
3061 (cond
3062 ;; Longtable.
3063 ((equal "longtable" table-env)
3064 (concat (and fontsize (concat "{" fontsize))
3065 (format "\\begin{longtable}{%s}\n" alignment)
3066 (and caption-above-p
3067 (org-string-nw-p caption)
3068 (concat caption "\\\\\n"))
3069 contents
3070 (and (not caption-above-p)
3071 (org-string-nw-p caption)
3072 (concat caption "\\\\\n"))
3073 "\\end{longtable}\n"
3074 (and fontsize "}")))
3075 ;; Longtabu
3076 ((equal "longtabu" table-env)
3077 (concat (and fontsize (concat "{" fontsize))
3078 (format "\\begin{longtabu}%s{%s}\n"
3079 (if width
3080 (format " %s %s "
3081 (if spreadp "spread" "to") width) "")
3082 alignment)
3083 (and caption-above-p
3084 (org-string-nw-p caption)
3085 (concat caption "\\\\\n"))
3086 contents
3087 (and (not caption-above-p)
3088 (org-string-nw-p caption)
3089 (concat caption "\\\\\n"))
3090 "\\end{longtabu}\n"
3091 (and fontsize "}")))
3092 ;; Others.
3093 (t (concat (cond
3094 (float-env
3095 (concat (format "\\begin{%s}%s\n" float-env placement)
3096 (if caption-above-p caption "")
3097 (when centerp "\\centering\n")
3098 fontsize))
3099 ((and (not float-env) caption)
3100 (concat
3101 (and centerp "\\begin{center}\n" )
3102 (if caption-above-p caption "")
3103 (cond ((and fontsize centerp) fontsize)
3104 (fontsize (concat "{" fontsize)))))
3105 (centerp (concat "\\begin{center}\n" fontsize))
3106 (fontsize (concat "{" fontsize)))
3107 (cond ((equal "tabu" table-env)
3108 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
3109 (if width (format
3110 (if spreadp " spread %s " " to %s ")
3111 width) "")
3112 alignment
3113 contents))
3114 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
3115 table-env
3116 (if width (format "{%s}" width) "")
3117 alignment
3118 contents
3119 table-env)))
3120 (cond
3121 (float-env
3122 (concat (if caption-above-p "" (concat "\n" caption))
3123 (format "\n\\end{%s}" float-env)))
3124 ((and (not float-env) caption)
3125 (concat
3126 (if caption-above-p "" (concat "\n" caption))
3127 (and centerp "\n\\end{center}")
3128 (and fontsize (not centerp) "}")))
3129 (centerp "\n\\end{center}")
3130 (fontsize "}")))))))
3132 (defun org-latex--table.el-table (table info)
3133 "Return appropriate LaTeX code for a table.el table.
3135 TABLE is the table type element to transcode. INFO is a plist
3136 used as a communication channel.
3138 This function assumes TABLE has `table.el' as its `:type'
3139 property."
3140 (require 'table)
3141 ;; Ensure "*org-export-table*" buffer is empty.
3142 (with-current-buffer (get-buffer-create "*org-export-table*")
3143 (erase-buffer))
3144 (let ((output (with-temp-buffer
3145 (insert (org-element-property :value table))
3146 (goto-char 1)
3147 (re-search-forward "^[ \t]*|[^|]" nil t)
3148 (table-generate-source 'latex "*org-export-table*")
3149 (with-current-buffer "*org-export-table*"
3150 (org-trim (buffer-string))))))
3151 (kill-buffer (get-buffer "*org-export-table*"))
3152 ;; Remove left out comments.
3153 (while (string-match "^%.*\n" output)
3154 (setq output (replace-match "" t t output)))
3155 (let ((attr (org-export-read-attribute :attr_latex table)))
3156 (when (plist-get attr :rmlines)
3157 ;; When the "rmlines" attribute is provided, remove all hlines
3158 ;; but the the one separating heading from the table body.
3159 (let ((n 0) (pos 0))
3160 (while (and (< (length output) pos)
3161 (setq pos (string-match "^\\\\hline\n?" output pos)))
3162 (incf n)
3163 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
3164 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
3165 (plist-get info :latex-tables-centered))))
3166 (if (not centerp) output
3167 (format "\\begin{center}\n%s\n\\end{center}" output))))))
3169 (defun org-latex--math-table (table info)
3170 "Return appropriate LaTeX code for a matrix.
3172 TABLE is the table type element to transcode. INFO is a plist
3173 used as a communication channel.
3175 This function assumes TABLE has `org' as its `:type' property and
3176 `inline-math' or `math' as its `:mode' attribute."
3177 (let* ((attr (org-export-read-attribute :attr_latex table))
3178 (env (or (plist-get attr :environment)
3179 (plist-get info :latex-default-table-environment)))
3180 (contents
3181 (mapconcat
3182 (lambda (row)
3183 ;; Ignore horizontal rules.
3184 (when (eq (org-element-property :type row) 'standard)
3185 ;; Return each cell unmodified.
3186 (concat
3187 (mapconcat
3188 (lambda (cell)
3189 (substring (org-element-interpret-data cell) 0 -1))
3190 (org-element-map row 'table-cell #'identity info) "&")
3191 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
3192 "\n")))
3193 (org-element-map table 'table-row #'identity info) "")))
3194 (concat
3195 ;; Prefix.
3196 (plist-get attr :math-prefix)
3197 ;; Environment. Also treat special cases.
3198 (cond ((member env '("array" "tabular"))
3199 (let ((align (make-string
3200 (cdr (org-export-table-dimensions table info)) ?c)))
3201 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
3202 ((assoc env org-latex-table-matrix-macros)
3203 (format "\\%s%s{\n%s}"
3205 (or (plist-get attr :math-arguments) "")
3206 contents))
3207 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
3208 ;; Suffix.
3209 (plist-get attr :math-suffix))))
3212 ;;;; Table Cell
3214 (defun org-latex-table-cell (table-cell contents info)
3215 "Transcode a TABLE-CELL element from Org to LaTeX.
3216 CONTENTS is the cell contents. INFO is a plist used as
3217 a communication channel."
3218 (concat
3219 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
3220 (if (and contents
3221 scientific-format
3222 (string-match orgtbl-exp-regexp contents))
3223 ;; Use appropriate format string for scientific
3224 ;; notation.
3225 (format scientific-format
3226 (match-string 1 contents)
3227 (match-string 2 contents))
3228 contents))
3229 (when (org-export-get-next-element table-cell info) " & ")))
3232 ;;;; Table Row
3234 (defun org-latex-table-row (table-row contents info)
3235 "Transcode a TABLE-ROW element from Org to LaTeX.
3236 CONTENTS is the contents of the row. INFO is a plist used as
3237 a communication channel."
3238 (let* ((attr (org-export-read-attribute :attr_latex
3239 (org-export-get-parent table-row)))
3240 (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
3241 (plist-get info :latex-tables-booktabs)))
3242 (longtablep
3243 (member (or (plist-get attr :environment)
3244 (plist-get info :latex-default-table-environment))
3245 '("longtable" "longtabu"))))
3246 (if (eq (org-element-property :type table-row) 'rule)
3247 (cond
3248 ((not booktabsp) "\\hline")
3249 ((not (org-export-get-previous-element table-row info)) "\\toprule")
3250 ((not (org-export-get-next-element table-row info)) "\\bottomrule")
3251 ((and longtablep
3252 (org-export-table-row-ends-header-p
3253 (org-export-get-previous-element table-row info) info))
3255 (t "\\midrule"))
3256 (concat
3257 ;; When BOOKTABS are activated enforce top-rule even when no
3258 ;; hline was specifically marked.
3259 (and booktabsp (not (org-export-get-previous-element table-row info))
3260 "\\toprule\n")
3261 contents "\\\\\n"
3262 (cond
3263 ;; Special case for long tables. Define header and footers.
3264 ((and longtablep (org-export-table-row-ends-header-p table-row info))
3265 (let ((columns (cdr (org-export-table-dimensions
3266 (org-export-get-parent-table table-row) info))))
3267 (format "%s
3268 \\endfirsthead
3269 \\multicolumn{%d}{l}{%s} \\\\
3271 %s \\\\\n
3273 \\endhead
3274 %s\\multicolumn{%d}{r}{%s} \\\\
3275 \\endfoot
3276 \\endlastfoot"
3277 (if booktabsp "\\midrule" "\\hline")
3278 columns
3279 (org-latex--translate "Continued from previous page" info)
3280 (cond
3281 ((not (org-export-table-row-starts-header-p table-row info))
3283 (booktabsp "\\toprule\n")
3284 (t "\\hline\n"))
3285 contents
3286 (if booktabsp "\\midrule" "\\hline")
3287 (if booktabsp "\\midrule" "\\hline")
3288 columns
3289 (org-latex--translate "Continued on next page" info))))
3290 ;; When BOOKTABS are activated enforce bottom rule even when
3291 ;; no hline was specifically marked.
3292 ((and booktabsp (not (org-export-get-next-element table-row info)))
3293 "\\bottomrule"))))))
3296 ;;;; Target
3298 (defun org-latex-target (target _contents info)
3299 "Transcode a TARGET object from Org to LaTeX.
3300 CONTENTS is nil. INFO is a plist holding contextual
3301 information."
3302 (format "\\label{%s}" (org-latex--label target info)))
3305 ;;;; Timestamp
3307 (defun org-latex-timestamp (timestamp _contents info)
3308 "Transcode a TIMESTAMP object from Org to LaTeX.
3309 CONTENTS is nil. INFO is a plist holding contextual
3310 information."
3311 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
3312 (format
3313 (plist-get info
3314 (case (org-element-property :type timestamp)
3315 ((active active-range) :latex-active-timestamp-format)
3316 ((inactive inactive-range) :latex-inactive-timestamp-format)
3317 (otherwise :latex-diary-timestamp-format)))
3318 value)))
3321 ;;;; Underline
3323 (defun org-latex-underline (_underline contents info)
3324 "Transcode UNDERLINE from Org to LaTeX.
3325 CONTENTS is the text with underline markup. INFO is a plist
3326 holding contextual information."
3327 (org-latex--text-markup contents 'underline info))
3330 ;;;; Verbatim
3332 (defun org-latex-verbatim (verbatim _contents info)
3333 "Transcode a VERBATIM object from Org to LaTeX.
3334 CONTENTS is nil. INFO is a plist used as a communication
3335 channel."
3336 (org-latex--text-markup
3337 (org-element-property :value verbatim) 'verbatim info))
3340 ;;;; Verse Block
3342 (defun org-latex-verse-block (verse-block contents info)
3343 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3344 CONTENTS is verse block contents. INFO is a plist holding
3345 contextual information."
3346 (org-latex--wrap-label
3347 verse-block
3348 ;; In a verse environment, add a line break to each newline
3349 ;; character and change each white space at beginning of a line
3350 ;; into a space of 1 em. Also change each blank line with
3351 ;; a vertical space of 1 em.
3352 (format "\\begin{verse}\n%s\\end{verse}"
3353 (replace-regexp-in-string
3354 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
3355 (replace-regexp-in-string
3356 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3357 (replace-regexp-in-string
3358 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3359 contents nil t) nil t) nil t))
3360 info))
3364 ;;; End-user functions
3366 ;;;###autoload
3367 (defun org-latex-export-as-latex
3368 (&optional async subtreep visible-only body-only ext-plist)
3369 "Export current buffer as a LaTeX buffer.
3371 If narrowing is active in the current buffer, only export its
3372 narrowed part.
3374 If a region is active, export that region.
3376 A non-nil optional argument ASYNC means the process should happen
3377 asynchronously. The resulting buffer should be accessible
3378 through the `org-export-stack' interface.
3380 When optional argument SUBTREEP is non-nil, export the sub-tree
3381 at point, extracting information from the headline properties
3382 first.
3384 When optional argument VISIBLE-ONLY is non-nil, don't export
3385 contents of hidden elements.
3387 When optional argument BODY-ONLY is non-nil, only write code
3388 between \"\\begin{document}\" and \"\\end{document}\".
3390 EXT-PLIST, when provided, is a property list with external
3391 parameters overriding Org default settings, but still inferior to
3392 file-local settings.
3394 Export is done in a buffer named \"*Org LATEX Export*\", which
3395 will be displayed when `org-export-show-temporary-export-buffer'
3396 is non-nil."
3397 (interactive)
3398 (org-export-to-buffer 'latex "*Org LATEX Export*"
3399 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
3401 ;;;###autoload
3402 (defun org-latex-convert-region-to-latex ()
3403 "Assume the current region has org-mode syntax, and convert it to LaTeX.
3404 This can be used in any buffer. For example, you can write an
3405 itemized list in org-mode syntax in an LaTeX buffer and use this
3406 command to convert it."
3407 (interactive)
3408 (org-export-replace-region-by 'latex))
3410 ;;;###autoload
3411 (defun org-latex-export-to-latex
3412 (&optional async subtreep visible-only body-only ext-plist)
3413 "Export current buffer to a LaTeX file.
3415 If narrowing is active in the current buffer, only export its
3416 narrowed part.
3418 If a region is active, export that region.
3420 A non-nil optional argument ASYNC means the process should happen
3421 asynchronously. The resulting file should be accessible through
3422 the `org-export-stack' interface.
3424 When optional argument SUBTREEP is non-nil, export the sub-tree
3425 at point, extracting information from the headline properties
3426 first.
3428 When optional argument VISIBLE-ONLY is non-nil, don't export
3429 contents of hidden elements.
3431 When optional argument BODY-ONLY is non-nil, only write code
3432 between \"\\begin{document}\" and \"\\end{document}\".
3434 EXT-PLIST, when provided, is a property list with external
3435 parameters overriding Org default settings, but still inferior to
3436 file-local settings."
3437 (interactive)
3438 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3439 (org-export-to-file 'latex outfile
3440 async subtreep visible-only body-only ext-plist)))
3442 ;;;###autoload
3443 (defun org-latex-export-to-pdf
3444 (&optional async subtreep visible-only body-only ext-plist)
3445 "Export current buffer to LaTeX then process through to PDF.
3447 If narrowing is active in the current buffer, only export its
3448 narrowed part.
3450 If a region is active, export that region.
3452 A non-nil optional argument ASYNC means the process should happen
3453 asynchronously. The resulting file should be accessible through
3454 the `org-export-stack' interface.
3456 When optional argument SUBTREEP is non-nil, export the sub-tree
3457 at point, extracting information from the headline properties
3458 first.
3460 When optional argument VISIBLE-ONLY is non-nil, don't export
3461 contents of hidden elements.
3463 When optional argument BODY-ONLY is non-nil, only write code
3464 between \"\\begin{document}\" and \"\\end{document}\".
3466 EXT-PLIST, when provided, is a property list with external
3467 parameters overriding Org default settings, but still inferior to
3468 file-local settings.
3470 Return PDF file's name."
3471 (interactive)
3472 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3473 (org-export-to-file 'latex outfile
3474 async subtreep visible-only body-only ext-plist
3475 (lambda (file) (org-latex-compile file)))))
3477 (defun org-latex-compile (texfile &optional snippet)
3478 "Compile a TeX file.
3480 TEXFILE is the name of the file being compiled. Processing is
3481 done through the command specified in `org-latex-pdf-process'.
3483 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3484 file used to preview a LaTeX snippet. In this case, do not
3485 create a log buffer and do not bother removing log files.
3487 Return PDF file name or an error if it couldn't be produced."
3488 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3489 (full-name (file-truename texfile))
3490 (compiler (or (with-temp-buffer
3491 (save-excursion (insert-file-contents full-name))
3492 (when (and (search-forward-regexp
3493 (regexp-opt org-latex-compilers) (line-end-position 2) t)
3494 (progn (beginning-of-line)
3495 (looking-at-p "%")))
3496 (match-string 0)))
3497 "pdflatex"))
3498 (out-dir (file-name-directory texfile))
3499 ;; Properly set working directory for compilation.
3500 (default-directory (if (file-name-absolute-p texfile)
3501 (file-name-directory full-name)
3502 default-directory))
3503 (time (current-time))
3504 warnings)
3505 (unless snippet (message "Processing LaTeX file %s..." texfile))
3506 (save-window-excursion
3507 (cond
3508 ;; A function is provided: Apply it.
3509 ((functionp org-latex-pdf-process)
3510 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3511 ;; A list is provided: Replace %b, %f and %o with appropriate
3512 ;; values in each command before applying it. Note that while
3513 ;; "%latex" and "%bibtex" is used in `org-latex-pdf-process',
3514 ;; they are replaced with "%L" and "%B" to adhere to
3515 ;; format-spec. Output is redirected to "*Org PDF LaTeX
3516 ;; Output*" buffer.
3517 ((consp org-latex-pdf-process)
3518 (let ((outbuf (and (not snippet)
3519 (get-buffer-create "*Org PDF LaTeX Output*")))
3520 (spec (list (cons ?B (shell-quote-argument org-latex-bib-compiler))
3521 (cons ?L (shell-quote-argument compiler))
3522 (cons ?b (shell-quote-argument base-name))
3523 (cons ?f (shell-quote-argument full-name))
3524 (cons ?o (shell-quote-argument out-dir)))))
3525 (dolist (command org-latex-pdf-process)
3526 (let ((c (replace-regexp-in-string
3527 "%\\(latex\\|bibtex\\)\\>"
3528 (lambda (str) (upcase (substring str 0 2)))
3529 command)))
3530 (shell-command (format-spec c spec) outbuf)))
3531 ;; Collect standard errors from output buffer.
3532 (setq warnings (and (not snippet)
3533 (org-latex--collect-warnings outbuf)))))
3534 (t (error "No valid command to process to PDF")))
3535 (let ((pdffile (concat out-dir base-name ".pdf")))
3536 ;; Check for process failure. Provide collected errors if
3537 ;; possible.
3538 (if (or (not (file-exists-p pdffile))
3539 (time-less-p (nth 5 (file-attributes pdffile)) time))
3540 (error (format "PDF file %s wasn't produced" pdffile))
3541 ;; Else remove log files, when specified, and signal end of
3542 ;; process to user, along with any error encountered.
3543 (unless snippet
3544 (when org-latex-remove-logfiles
3545 (dolist (file (directory-files
3546 out-dir t
3547 (concat (regexp-quote base-name)
3548 "\\(?:\\.[0-9]+\\)?"
3549 "\\."
3550 (regexp-opt org-latex-logfiles-extensions))))
3551 (delete-file file)))
3552 (message (concat "PDF file produced"
3553 (cond
3554 ((eq warnings 'error) " with errors.")
3555 (warnings (concat " with warnings: " warnings))
3556 (t "."))))))
3557 ;; Return output file name.
3558 pdffile))))
3560 (defun org-latex--collect-warnings (buffer)
3561 "Collect some warnings from \"pdflatex\" command output.
3562 BUFFER is the buffer containing output. Return collected
3563 warnings types as a string, `error' if a LaTeX error was
3564 encountered or nil if there was none."
3565 (with-current-buffer buffer
3566 (save-excursion
3567 (goto-char (point-max))
3568 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3569 (if (re-search-forward "^!" nil t) 'error
3570 (let ((case-fold-search t)
3571 (warnings ""))
3572 (dolist (warning org-latex-known-warnings)
3573 (when (save-excursion (re-search-forward (car warning) nil t))
3574 (setq warnings (concat warnings " " (cdr warning)))))
3575 (org-string-nw-p (org-trim warnings))))))))
3577 ;;;###autoload
3578 (defun org-latex-publish-to-latex (plist filename pub-dir)
3579 "Publish an Org file to LaTeX.
3581 FILENAME is the filename of the Org file to be published. PLIST
3582 is the property list for the given project. PUB-DIR is the
3583 publishing directory.
3585 Return output file name."
3586 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3588 ;;;###autoload
3589 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3590 "Publish an Org file to PDF (via LaTeX).
3592 FILENAME is the filename of the Org file to be published. PLIST
3593 is the property list for the given project. PUB-DIR is the
3594 publishing directory.
3596 Return output file name."
3597 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3598 ;; in working directory and then moved to publishing directory.
3599 (org-publish-attachment
3600 plist
3601 (org-latex-compile
3602 (org-publish-org-to
3603 'latex filename ".tex" plist (file-name-directory filename)))
3604 pub-dir))
3607 (provide 'ox-latex)
3609 ;; Local variables:
3610 ;; generated-autoload-file: "org-loaddefs.el"
3611 ;; End:
3613 ;;; ox-latex.el ends here