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