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