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