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