ox-latex.el 130 KB
Newer Older
Rasmus's avatar
Rasmus committed
1
;;; ox-latex.el --- LaTeX Back-End for Org Export Engine -*- lexical-binding: t; -*-
Bastien Guerry's avatar
Bastien Guerry committed
2

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 2011-2020 Free Software Foundation, Inc.
Bastien Guerry's avatar
Bastien Guerry committed
4 5 6 7

;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
;; Keywords: outlines, hypermedia, calendar, wp

8 9
;; This file is part of GNU Emacs.

Bastien Guerry's avatar
Bastien Guerry committed
10 11 12 13 14 15 16 17 18 19 20
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
21
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
Bastien Guerry's avatar
Bastien Guerry committed
22 23 24 25 26 27 28

;;; Commentary:
;;
;; See Org manual for details.

;;; Code:

Rasmus's avatar
Rasmus committed
29
(require 'cl-lib)
Bastien Guerry's avatar
Bastien Guerry committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
(require 'ox)
(require 'ox-publish)

(defvar org-latex-default-packages-alist)
(defvar org-latex-packages-alist)
(defvar orgtbl-exp-regexp)



;;; Define Back-End

(org-export-define-backend 'latex
  '((bold . org-latex-bold)
    (center-block . org-latex-center-block)
    (clock . org-latex-clock)
    (code . org-latex-code)
    (drawer . org-latex-drawer)
    (dynamic-block . org-latex-dynamic-block)
    (entity . org-latex-entity)
    (example-block . org-latex-example-block)
    (export-block . org-latex-export-block)
    (export-snippet . org-latex-export-snippet)
    (fixed-width . org-latex-fixed-width)
    (footnote-definition . org-latex-footnote-definition)
    (footnote-reference . org-latex-footnote-reference)
    (headline . org-latex-headline)
    (horizontal-rule . org-latex-horizontal-rule)
    (inline-src-block . org-latex-inline-src-block)
    (inlinetask . org-latex-inlinetask)
    (italic . org-latex-italic)
    (item . org-latex-item)
    (keyword . org-latex-keyword)
    (latex-environment . org-latex-latex-environment)
    (latex-fragment . org-latex-latex-fragment)
    (line-break . org-latex-line-break)
    (link . org-latex-link)
Rasmus's avatar
Rasmus committed
66
    (node-property . org-latex-node-property)
Bastien Guerry's avatar
Bastien Guerry committed
67 68 69 70
    (paragraph . org-latex-paragraph)
    (plain-list . org-latex-plain-list)
    (plain-text . org-latex-plain-text)
    (planning . org-latex-planning)
Rasmus's avatar
Rasmus committed
71
    (property-drawer . org-latex-property-drawer)
Bastien Guerry's avatar
Bastien Guerry committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    (quote-block . org-latex-quote-block)
    (radio-target . org-latex-radio-target)
    (section . org-latex-section)
    (special-block . org-latex-special-block)
    (src-block . org-latex-src-block)
    (statistics-cookie . org-latex-statistics-cookie)
    (strike-through . org-latex-strike-through)
    (subscript . org-latex-subscript)
    (superscript . org-latex-superscript)
    (table . org-latex-table)
    (table-cell . org-latex-table-cell)
    (table-row . org-latex-table-row)
    (target . org-latex-target)
    (template . org-latex-template)
    (timestamp . org-latex-timestamp)
    (underline . org-latex-underline)
    (verbatim . org-latex-verbatim)
Rasmus's avatar
Rasmus committed
89 90 91 92
    (verse-block . org-latex-verse-block)
    ;; Pseudo objects and elements.
    (latex-math-block . org-latex-math-block)
    (latex-matrices . org-latex-matrices))
Bastien Guerry's avatar
Bastien Guerry committed
93 94 95 96 97 98 99 100 101
  :menu-entry
  '(?l "Export to LaTeX"
       ((?L "As LaTeX buffer" org-latex-export-as-latex)
	(?l "As LaTeX file" org-latex-export-to-latex)
	(?p "As PDF file" org-latex-export-to-pdf)
	(?o "As PDF file and open"
	    (lambda (a s v b)
	      (if a (org-latex-export-to-pdf t s v b)
		(org-open-file (org-latex-export-to-pdf nil s v b)))))))
Rasmus's avatar
Rasmus committed
102 103 104
  :filters-alist '((:filter-options . org-latex-math-block-options-filter)
		   (:filter-paragraph . org-latex-clean-invalid-line-breaks)
		   (:filter-parse-tree org-latex-math-block-tree-filter
Rasmus's avatar
Rasmus committed
105 106
				       org-latex-matrices-tree-filter
				       org-latex-image-link-filter)
Rasmus's avatar
Rasmus committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
		   (:filter-verse-block . org-latex-clean-invalid-line-breaks))
  :options-alist
  '((:latex-class "LATEX_CLASS" nil org-latex-default-class t)
    (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
    (:latex-header "LATEX_HEADER" nil nil newline)
    (:latex-header-extra "LATEX_HEADER_EXTRA" nil nil newline)
    (:description "DESCRIPTION" nil nil parse)
    (:keywords "KEYWORDS" nil nil parse)
    (:subtitle "SUBTITLE" nil nil parse)
    ;; Other variables.
    (:latex-active-timestamp-format nil nil org-latex-active-timestamp-format)
    (:latex-caption-above nil nil org-latex-caption-above)
    (:latex-classes nil nil org-latex-classes)
    (:latex-default-figure-position nil nil org-latex-default-figure-position)
    (:latex-default-table-environment nil nil org-latex-default-table-environment)
    (:latex-default-table-mode nil nil org-latex-default-table-mode)
    (:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format)
    (:latex-footnote-defined-format nil nil org-latex-footnote-defined-format)
    (:latex-footnote-separator nil nil org-latex-footnote-separator)
    (:latex-format-drawer-function nil nil org-latex-format-drawer-function)
    (:latex-format-headline-function nil nil org-latex-format-headline-function)
    (:latex-format-inlinetask-function nil nil org-latex-format-inlinetask-function)
    (:latex-hyperref-template nil nil org-latex-hyperref-template t)
Bastien's avatar
Bastien committed
130
    (:latex-image-default-scale nil nil org-latex-image-default-scale)
Rasmus's avatar
Rasmus committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    (:latex-image-default-height nil nil org-latex-image-default-height)
    (:latex-image-default-option nil nil org-latex-image-default-option)
    (:latex-image-default-width nil nil org-latex-image-default-width)
    (:latex-images-centered nil nil org-latex-images-centered)
    (:latex-inactive-timestamp-format nil nil org-latex-inactive-timestamp-format)
    (:latex-inline-image-rules nil nil org-latex-inline-image-rules)
    (:latex-link-with-unknown-path-format nil nil org-latex-link-with-unknown-path-format)
    (:latex-listings nil nil org-latex-listings)
    (:latex-listings-langs nil nil org-latex-listings-langs)
    (:latex-listings-options nil nil org-latex-listings-options)
    (:latex-minted-langs nil nil org-latex-minted-langs)
    (:latex-minted-options nil nil org-latex-minted-options)
    (:latex-prefer-user-labels nil nil org-latex-prefer-user-labels)
    (:latex-subtitle-format nil nil org-latex-subtitle-format)
    (:latex-subtitle-separate nil nil org-latex-subtitle-separate)
    (:latex-table-scientific-notation nil nil org-latex-table-scientific-notation)
    (:latex-tables-booktabs nil nil org-latex-tables-booktabs)
    (:latex-tables-centered nil nil org-latex-tables-centered)
    (:latex-text-markup-alist nil nil org-latex-text-markup-alist)
    (:latex-title-command nil nil org-latex-title-command)
    (:latex-toc-command nil nil org-latex-toc-command)
    (:latex-compiler "LATEX_COMPILER" nil org-latex-compiler)
    ;; Redefine regular options.
    (:date "DATE" nil "\\today" parse)))
Bastien Guerry's avatar
Bastien Guerry committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181



;;; Internal Variables

(defconst org-latex-babel-language-alist
  '(("af" . "afrikaans")
    ("bg" . "bulgarian")
    ("ca" . "catalan")
    ("cs" . "czech")
    ("cy" . "welsh")
    ("da" . "danish")
    ("de" . "germanb")
    ("de-at" . "naustrian")
    ("de-de" . "ngerman")
    ("el" . "greek")
    ("en" . "english")
    ("en-au" . "australian")
    ("en-ca" . "canadian")
    ("en-gb" . "british")
    ("en-ie" . "irish")
    ("en-nz" . "newzealand")
    ("en-us" . "american")
    ("es" . "spanish")
    ("et" . "estonian")
    ("eu" . "basque")
    ("fi" . "finnish")
Bastien's avatar
Bastien committed
182
    ("fr" . "french")
Bastien Guerry's avatar
Bastien Guerry committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    ("fr-ca" . "canadien")
    ("gl" . "galician")
    ("hr" . "croatian")
    ("hu" . "hungarian")
    ("id" . "indonesian")
    ("is" . "icelandic")
    ("it" . "italian")
    ("la" . "latin")
    ("ms" . "malay")
    ("nl" . "dutch")
    ("nb" . "norsk")
    ("nn" . "nynorsk")
    ("no" . "norsk")
    ("pl" . "polish")
    ("pt" . "portuguese")
Bastien's avatar
Bastien committed
198
    ("pt-br" . "brazilian")
Bastien Guerry's avatar
Bastien Guerry committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212
    ("ro" . "romanian")
    ("ru" . "russian")
    ("sa" . "sanskrit")
    ("sb" . "uppersorbian")
    ("sk" . "slovak")
    ("sl" . "slovene")
    ("sq" . "albanian")
    ("sr" . "serbian")
    ("sv" . "swedish")
    ("ta" . "tamil")
    ("tr" . "turkish")
    ("uk" . "ukrainian"))
  "Alist between language code and corresponding Babel option.")

Rasmus's avatar
Rasmus committed
213 214 215
(defconst org-latex-polyglossia-language-alist
  '(("am" "amharic")
    ("ar" "arabic")
Bastien's avatar
Bastien committed
216
    ("ast" "asturian")
Rasmus's avatar
Rasmus committed
217
    ("bg" "bulgarian")
Bastien's avatar
Bastien committed
218 219
    ("bn" "bengali")
    ("bo" "tibetan")
Rasmus's avatar
Rasmus committed
220 221 222 223 224 225 226 227 228
    ("br" "breton")
    ("ca" "catalan")
    ("cop" "coptic")
    ("cs" "czech")
    ("cy" "welsh")
    ("da" "danish")
    ("de" "german" "german")
    ("de-at" "german" "austrian")
    ("de-de" "german" "german")
Bastien's avatar
Bastien committed
229
    ("dsb" "lsorbian")
Rasmus's avatar
Rasmus committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    ("dv" "divehi")
    ("el" "greek")
    ("en" "english" "usmax")
    ("en-au" "english" "australian")
    ("en-gb" "english" "uk")
    ("en-nz" "english" "newzealand")
    ("en-us" "english" "usmax")
    ("eo" "esperanto")
    ("es" "spanish")
    ("et" "estonian")
    ("eu" "basque")
    ("fa" "farsi")
    ("fi" "finnish")
    ("fr" "french")
    ("fu" "friulan")
    ("ga" "irish")
    ("gd" "scottish")
    ("gl" "galician")
    ("he" "hebrew")
    ("hi" "hindi")
    ("hr" "croatian")
Bastien's avatar
Bastien committed
251
    ("hsb" "usorbian")
Rasmus's avatar
Rasmus committed
252 253 254
    ("hu" "magyar")
    ("hy" "armenian")
    ("ia" "interlingua")
Bastien's avatar
Bastien committed
255
    ("id" "bahasai")
Rasmus's avatar
Rasmus committed
256 257 258 259 260 261
    ("is" "icelandic")
    ("it" "italian")
    ("kn" "kannada")
    ("la" "latin" "modern")
    ("la-classic" "latin" "classic")
    ("la-medieval" "latin" "medieval")
Bastien's avatar
Bastien committed
262
    ("la-modern" "latin" "modern")
Rasmus's avatar
Rasmus committed
263 264 265 266
    ("lo" "lao")
    ("lt" "lithuanian")
    ("lv" "latvian")
    ("ml" "malayalam")
Bastien's avatar
Bastien committed
267
    ("mr" "maranthi")
Rasmus's avatar
Rasmus committed
268 269
    ("nb" "norsk")
    ("nko" "nko")
Bastien's avatar
Bastien committed
270 271
    ("nl" "dutch")
    ("nn" "nynorsk")
Rasmus's avatar
Rasmus committed
272 273 274 275 276
    ("no" "norsk")
    ("oc" "occitan")
    ("pl" "polish")
    ("pms" "piedmontese")
    ("pt" "portuges")
Bastien's avatar
Bastien committed
277
    ("pt-br" "brazilian")
Rasmus's avatar
Rasmus committed
278 279 280 281
    ("rm" "romansh")
    ("ro" "romanian")
    ("ru" "russian")
    ("sa" "sanskrit")
Bastien's avatar
Bastien committed
282
    ("se" "samin")
Rasmus's avatar
Rasmus committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    ("sk" "slovak")
    ("sl" "slovenian")
    ("sq" "albanian")
    ("sr" "serbian")
    ("sv" "swedish")
    ("syr" "syriac")
    ("ta" "tamil")
    ("te" "telugu")
    ("th" "thai")
    ("tk" "turkmen")
    ("tr" "turkish")
    ("uk" "ukrainian")
    ("ur" "urdu")
    ("vi" "vietnamese"))
  "Alist between language code and corresponding Polyglossia option")

Bastien Guerry's avatar
Bastien Guerry committed
299
(defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
Rasmus's avatar
Rasmus committed
300 301
					  ("qbordermatrix" . "\\cr")
					  ("kbordermatrix" . "\\\\"))
Bastien Guerry's avatar
Bastien Guerry committed
302 303
  "Alist between matrix macros and their row ending.")

Rasmus's avatar
Rasmus committed
304 305 306 307 308 309 310 311 312 313 314 315 316
(defconst org-latex-math-environments-re
  (format
   "\\`[ \t]*\\\\begin{%s\\*?}"
   (regexp-opt
	   '("equation" "eqnarray" "math" "displaymath"
	     "align"  "gather" "multline" "flalign"  "alignat"
	     "xalignat" "xxalignat"
	     "subequations"
	     ;; breqn
	     "dmath" "dseries" "dgroup" "darray"
	     ;; empheq
	     "empheq")))
  "Regexp of LaTeX math environments.")
Bastien Guerry's avatar
Bastien Guerry committed
317 318 319 320 321 322 323 324 325


;;; User Configurable Variables

(defgroup org-export-latex nil
  "Options for exporting Org mode files to LaTeX."
  :tag "Org Export LaTeX"
  :group 'org-export)

Rasmus's avatar
Rasmus committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
;;;; Generic

(defcustom org-latex-caption-above '(table)
  "When non-nil, place caption string at the beginning of elements.
Otherwise, place it near the end.  When value is a list of
symbols, put caption above selected elements only.  Allowed
symbols are: `image', `table', `src-block' and `special-block'."
  :group 'org-export-latex
  :version "26.1"
  :package-version '(Org . "8.3")
  :type '(choice
	  (const :tag "For all elements" t)
	  (const :tag "For no element" nil)
	  (set :tag "For the following elements only" :greedy t
	       (const :tag "Images" image)
	       (const :tag "Tables" table)
	       (const :tag "Source code" src-block)
	       (const :tag "Special blocks" special-block))))

(defcustom org-latex-prefer-user-labels nil
   "Use user-provided labels instead of internal ones when non-nil.

When this variable is non-nil, Org will use the value of
CUSTOM_ID property, NAME keyword or Org target as the key for the
\\label commands generated.

By default, Org generates its own internal labels during LaTeX
export.  This process ensures that the \\label keys are unique
and valid, but it means the keys are not available in advance of
the export process.

Setting this variable gives you control over how Org generates
labels during LaTeX export, so that you may know their keys in
advance.  One reason to do this is that it allows you to refer to
various elements using a single label both in Org's link syntax
and in embedded LaTeX code.

For example, when this variable is non-nil, a headline like this:

  ** Some section
     :PROPERTIES:
     :CUSTOM_ID: sec:foo
     :END:
  This is section [[#sec:foo]].
  #+BEGIN_EXPORT latex
  And this is still section \\ref{sec:foo}.
  #+END_EXPORT

will be exported to LaTeX as:

  \\subsection{Some section}
  \\label{sec:foo}
  This is section \\ref{sec:foo}.
  And this is still section \\ref{sec:foo}.

Note, however, that setting this variable introduces a limitation
on the possible values for CUSTOM_ID and NAME.  When this
variable is non-nil, Org passes their value to \\label unchanged.
You are responsible for ensuring that the value is a valid LaTeX
\\label key, and that no other \\label commands with the same key
appear elsewhere in your document.  (Keys may contain letters,
387
numbers, and the following punctuation: `_' `.' `-' `:'.)  There
Rasmus's avatar
Rasmus committed
388 389 390 391 392 393 394 395 396 397 398
are no such limitations on CUSTOM_ID and NAME when this variable
is nil.

For headlines that do not define the CUSTOM_ID property or
elements without a NAME, Org will continue to use its default
labeling scheme to generate labels and resolve links into proper
references."
  :group 'org-export-latex
  :type 'boolean
  :version "26.1"
  :package-version '(Org . "8.3"))
Bastien Guerry's avatar
Bastien Guerry committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432

;;;; Preamble

(defcustom org-latex-default-class "article"
  "The default LaTeX class."
  :group 'org-export-latex
  :type '(string :tag "LaTeX class"))

(defcustom org-latex-classes
  '(("article"
     "\\documentclass[11pt]{article}"
     ("\\section{%s}" . "\\section*{%s}")
     ("\\subsection{%s}" . "\\subsection*{%s}")
     ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
     ("\\paragraph{%s}" . "\\paragraph*{%s}")
     ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
    ("report"
     "\\documentclass[11pt]{report}"
     ("\\part{%s}" . "\\part*{%s}")
     ("\\chapter{%s}" . "\\chapter*{%s}")
     ("\\section{%s}" . "\\section*{%s}")
     ("\\subsection{%s}" . "\\subsection*{%s}")
     ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
    ("book"
     "\\documentclass[11pt]{book}"
     ("\\part{%s}" . "\\part*{%s}")
     ("\\chapter{%s}" . "\\chapter*{%s}")
     ("\\section{%s}" . "\\section*{%s}")
     ("\\subsection{%s}" . "\\subsection*{%s}")
     ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
  "Alist of LaTeX classes and associated header and structure.
If #+LATEX_CLASS is set in the buffer, use its value and the
associated information.  Here is the structure of each cell:

433
  (class-name
Bastien Guerry's avatar
Bastien Guerry committed
434
    header-string
435
    (numbered-section . unnumbered-section)
Bastien Guerry's avatar
Bastien Guerry committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
    ...)

The header string
-----------------

The HEADER-STRING is the header that will be inserted into the
LaTeX file.  It should contain the \\documentclass macro, and
anything else that is needed for this setup.  To this header, the
following commands will be added:

- Calls to \\usepackage for all packages mentioned in the
  variables `org-latex-default-packages-alist' and
  `org-latex-packages-alist'.  Thus, your header definitions
  should avoid to also request these packages.

- Lines specified via \"#+LATEX_HEADER:\" and
  \"#+LATEX_HEADER_EXTRA:\" keywords.

If you need more control about the sequence in which the header
is built up, or if you want to exclude one of these building
blocks for a particular class, you can use the following
macro-like placeholders.

 [DEFAULT-PACKAGES]      \\usepackage statements for default packages
 [NO-DEFAULT-PACKAGES]   do not include any of the default packages
 [PACKAGES]              \\usepackage statements for packages
 [NO-PACKAGES]           do not include the packages
 [EXTRA]                 the stuff from #+LATEX_HEADER(_EXTRA)
 [NO-EXTRA]              do not include #+LATEX_HEADER(_EXTRA) stuff

So a header like

  \\documentclass{article}
  [NO-DEFAULT-PACKAGES]
  [EXTRA]
  \\providecommand{\\alert}[1]{\\textbf{#1}}
  [PACKAGES]

will omit the default packages, and will include the
#+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
to \\providecommand, and then place \\usepackage commands based
on the content of `org-latex-packages-alist'.

If your header, `org-latex-default-packages-alist' or
`org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
AUTO will automatically be replaced with a coding system derived
from `buffer-file-coding-system'.  See also the variable
`org-latex-inputenc-alist' for a way to influence this mechanism.

Rasmus's avatar
Rasmus committed
485 486 487 488 489 490 491 492 493
Likewise, if your header contains \"\\usepackage[AUTO]{babel}\"
or \"\\usepackage[AUTO]{polyglossia}\", AUTO will be replaced
with the language related to the language code specified by
`org-export-default-language'.  Note that constructions such as
\"\\usepackage[french,AUTO,english]{babel}\" are permitted.  For
Polyglossia the language will be set via the macros
\"\\setmainlanguage\" and \"\\setotherlanguage\".  See also
`org-latex-guess-babel-language' and
`org-latex-guess-polyglossia-language'.
Bastien Guerry's avatar
Bastien Guerry committed
494 495 496 497 498 499 500 501 502 503 504 505

The sectioning structure
------------------------

The sectioning structure of the class is given by the elements
following the header string.  For each sectioning level, a number
of strings is specified.  A %s formatter is mandatory in each
section string and will be replaced by the title of the section.

Instead of a cons cell (numbered . unnumbered), you can also
provide a list of 2 or 4 elements,

506
  (numbered-open numbered-close)
Bastien Guerry's avatar
Bastien Guerry committed
507 508 509

or

510
  (numbered-open numbered-close unnumbered-open unnumbered-close)
Bastien Guerry's avatar
Bastien Guerry committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552

providing opening and closing strings for a LaTeX environment
that should represent the document section.  The opening clause
should have a %s to represent the section title.

Instead of a list of sectioning commands, you can also specify
a function name.  That function will be called with two
parameters, the (reduced) level of the headline, and a predicate
non-nil when the headline should be numbered.  It must return
a format string in which the section title will be added."
  :group 'org-export-latex
  :type '(repeat
	  (list (string :tag "LaTeX class")
		(string :tag "LaTeX header")
		(repeat :tag "Levels" :inline t
			(choice
			 (cons :tag "Heading"
			       (string :tag "  numbered")
			       (string :tag "unnumbered"))
			 (list :tag "Environment"
			       (string :tag "Opening   (numbered)")
			       (string :tag "Closing   (numbered)")
			       (string :tag "Opening (unnumbered)")
			       (string :tag "Closing (unnumbered)"))
			 (function :tag "Hook computing sectioning"))))))

(defcustom org-latex-inputenc-alist nil
  "Alist of inputenc coding system names, and what should really be used.
For example, adding an entry

      (\"utf8\" . \"utf8x\")

will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
are written as utf8 files."
  :group 'org-export-latex
  :type '(repeat
	  (cons
	   (string :tag "Derived from buffer")
	   (string :tag "Use this instead"))))

(defcustom org-latex-title-command "\\maketitle"
  "The command used to insert the title just after \\begin{document}.
Rasmus's avatar
Rasmus committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

This format string may contain these elements:

  %a for AUTHOR keyword
  %t for TITLE keyword
  %s for SUBTITLE keyword
  %k for KEYWORDS line
  %d for DESCRIPTION line
  %c for CREATOR line
  %l for Language keyword
  %L for capitalized language keyword
  %D for DATE keyword

If you need to use a \"%\" character, you need to escape it
like that: \"%%\".

Setting :latex-title-command in publishing projects will take
precedence over this variable."
Bastien Guerry's avatar
Bastien Guerry committed
571
  :group 'org-export-latex
Rasmus's avatar
Rasmus committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
  :type '(string :tag "Format string"))

(defcustom org-latex-subtitle-format "\\\\\\medskip\n\\large %s"
  "Format string used for transcoded subtitle.
The format string should have at most one \"%s\"-expression,
which is replaced with the subtitle."
  :group 'org-export-latex
  :version "26.1"
  :package-version '(Org . "8.3")
  :type '(string :tag "Format string"))

(defcustom org-latex-subtitle-separate nil
  "Non-nil means the subtitle is not typeset as part of title."
  :group 'org-export-latex
  :version "26.1"
  :package-version '(Org . "8.3")
  :type 'boolean)
Bastien Guerry's avatar
Bastien Guerry committed
589 590 591 592 593 594 595 596

(defcustom org-latex-toc-command "\\tableofcontents\n\n"
  "LaTeX command to set the table of contents, list of figures, etc.
This command only applies to the table of contents generated with
the toc:nil option, not to those generated with #+TOC keyword."
  :group 'org-export-latex
  :type 'string)

Rasmus's avatar
Rasmus committed
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
(defcustom org-latex-hyperref-template
  "\\hypersetup{\n pdfauthor={%a},\n pdftitle={%t},\n pdfkeywords={%k},
 pdfsubject={%d},\n pdfcreator={%c}, \n pdflang={%L}}\n"
  "Template for hyperref package options.

This format string may contain these elements:

  %a for AUTHOR keyword
  %t for TITLE keyword
  %s for SUBTITLE keyword
  %k for KEYWORDS line
  %d for DESCRIPTION line
  %c for CREATOR line
  %l for Language keyword
  %L for capitalized language keyword
  %D for DATE keyword

If you need to use a \"%\" character, you need to escape it
like that: \"%%\".

As a special case, a nil value prevents template from being
inserted.

Setting :latex-hyperref-template in publishing projects will take
precedence over this variable."
Bastien Guerry's avatar
Bastien Guerry committed
622
  :group 'org-export-latex
Rasmus's avatar
Rasmus committed
623 624 625 626
  :version "26.1"
  :package-version '(Org . "8.3")
  :type '(choice (const :tag "No template" nil)
		 (string :tag "Format string")))
Bastien Guerry's avatar
Bastien Guerry committed
627 628 629 630 631 632 633

;;;; Headline

(defcustom org-latex-format-headline-function
  'org-latex-format-headline-default-function
  "Function for formatting the headline's text.

Rasmus's avatar
Rasmus committed
634 635
This function will be called with six arguments:
TODO      the todo keyword (string or nil)
Bastien Guerry's avatar
Bastien Guerry committed
636 637
TODO-TYPE the type of todo (symbol: `todo', `done', nil)
PRIORITY  the priority of the headline (integer or nil)
Rasmus's avatar
Rasmus committed
638 639 640
TEXT      the main headline text (string)
TAGS      the tags (list of strings or nil)
INFO      the export options (plist)
Bastien Guerry's avatar
Bastien Guerry committed
641

Rasmus's avatar
Rasmus committed
642
The function result will be used in the section format string."
Bastien Guerry's avatar
Bastien Guerry committed
643 644 645 646 647 648 649 650 651 652 653 654 655
  :group 'org-export-latex
  :version "24.4"
  :package-version '(Org . "8.0")
  :type 'function)


;;;; Footnotes

(defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
  "Text used to separate footnotes."
  :group 'org-export-latex
  :type 'string)

Rasmus's avatar
Rasmus committed
656 657 658 659 660 661 662 663 664 665
(defcustom org-latex-footnote-defined-format "\\textsuperscript{\\ref{%s}}"
  "Format string used to format reference to footnote already defined.
%s will be replaced by the label of the referred footnote."
  :group 'org-export-latex
  :type '(choice
	  (const :tag "Use plain superscript (default)" "\\textsuperscript{\\ref{%s}}")
	  (const :tag "Use Memoir/KOMA-Script footref" "\\footref{%s}")
	  (string :tag "Other format string"))
  :version "26.1"
  :package-version '(Org . "9.0"))
Bastien Guerry's avatar
Bastien Guerry committed
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686

;;;; Timestamps

(defcustom org-latex-active-timestamp-format "\\textit{%s}"
  "A printf format string to be applied to active timestamps."
  :group 'org-export-latex
  :type 'string)

(defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
  "A printf format string to be applied to inactive timestamps."
  :group 'org-export-latex
  :type 'string)

(defcustom org-latex-diary-timestamp-format "\\textit{%s}"
  "A printf format string to be applied to diary timestamps."
  :group 'org-export-latex
  :type 'string)


;;;; Links

Rasmus's avatar
Rasmus committed
687 688 689 690 691 692 693 694
(defcustom org-latex-images-centered t
  "When non-nil, images are centered."
  :group 'org-export-latex
  :version "26.1"
  :package-version '(Org . "9.0")
  :type 'boolean
  :safe #'booleanp)

Bastien Guerry's avatar
Bastien Guerry committed
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
(defcustom org-latex-image-default-option ""
  "Default option for images."
  :group 'org-export-latex
  :version "24.4"
  :package-version '(Org . "8.0")
  :type 'string)

(defcustom org-latex-image-default-width ".9\\linewidth"
  "Default width for images.
This value will not be used if a height is provided."
  :group 'org-export-latex
  :version "24.4"
  :package-version '(Org . "8.0")
  :type 'string)

Bastien's avatar
Bastien committed
710 711 712 713 714 715 716 717 718 719
(defcustom org-latex-image-default-scale ""
  "Default scale for images.
This value will not be used if a width or a scale is provided,
or if the image is wrapped within a \"wrapfigure\" environment.
Scale overrides width and height."
  :group 'org-export-latex
  :package-version '(Org . "9.3")
  :type 'string
  :safe #'stringp)

Bastien Guerry's avatar
Bastien Guerry committed
720 721 722 723 724 725 726 727 728 729
(defcustom org-latex-image-default-height ""
  "Default height for images.
This value will not be used if a width is provided, or if the
image is wrapped within a \"figure\" or \"wrapfigure\"
environment."
  :group 'org-export-latex
  :version "24.4"
  :package-version '(Org . "8.0")
  :type 'string)

Rasmus's avatar
Rasmus committed
730 731
(defcustom org-latex-default-figure-position "htbp"
  "Default position for LaTeX figures."
Bastien Guerry's avatar
Bastien Guerry committed
732
  :group 'org-export-latex
Rasmus's avatar
Rasmus committed
733 734 735 736
  :type 'string
  :version "26.1"
  :package-version '(Org . "9.0")
  :safe #'stringp)
Bastien Guerry's avatar
Bastien Guerry committed
737 738

(defcustom org-latex-inline-image-rules
Rasmus's avatar
Rasmus committed
739 740
  `(("file" . ,(regexp-opt
		'("pdf" "jpeg" "jpg" "png" "ps" "eps" "tikz" "pgf" "svg"))))
Bastien Guerry's avatar
Bastien Guerry committed
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
  "Rules characterizing image files that can be inlined into LaTeX.

A rule consists in an association whose key is the type of link
to consider, and value is a regexp that will be matched against
link's path.

Note that, by default, the image extension *actually* allowed
depend on the way the LaTeX file is processed.  When used with
pdflatex, pdf, jpg and png images are OK.  When processing
through dvi to Postscript, only ps and eps are allowed.  The
default we use here encompasses both."
  :group 'org-export-latex
  :version "24.4"
  :package-version '(Org . "8.0")
  :type '(alist :key-type (string :tag "Type")
		:value-type (regexp :tag "Path")))

(defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
  "Format string for links with unknown path type."
  :group 'org-export-latex
  :type 'string)


;;;; Tables

(defcustom org-latex-default-table-environment "tabular"
  "Default environment used to build tables."
  :group 'org-export-latex
  :version "24.4"
  :package-version '(Org . "8.0")
  :type 'string)

(defcustom org-latex-default-table-mode 'table
  "Default mode for tables.

Value can be a symbol among:

  `table' Regular LaTeX table.

  `math' In this mode, every cell is considered as being in math
     mode and the complete table will be wrapped within a math
     environment.  It is particularly useful to write matrices.

  `inline-math' This mode is almost the same as `math', but the
     math environment will be inlined.

  `verbatim' The table is exported as it appears in the Org
     buffer, within a verbatim environment.

This value can be overridden locally with, i.e. \":mode math\" in
LaTeX attributes.

When modifying this variable, it may be useful to change
`org-latex-default-table-environment' accordingly."
  :group 'org-export-latex
  :version "24.4"
  :package-version '(Org . "8.0")
  :type '(choice (const :tag "Table" table)
		 (const :tag "Matrix" math)
		 (const :tag "Inline matrix" inline-math)
Rasmus's avatar
Rasmus committed
801 802
		 (const :tag "Verbatim" verbatim))
  :safe (lambda (s) (memq s '(table math inline-math verbatim))))
Bastien Guerry's avatar
Bastien Guerry committed
803 804 805 806

(defcustom org-latex-tables-centered t
  "When non-nil, tables are exported in a center environment."
  :group 'org-export-latex
Rasmus's avatar
Rasmus committed
807 808
  :type 'boolean
  :safe #'booleanp)
Bastien Guerry's avatar
Bastien Guerry committed
809 810 811 812 813 814 815 816 817 818

(defcustom org-latex-tables-booktabs nil
  "When non-nil, display tables in a formal \"booktabs\" style.
This option assumes that the \"booktabs\" package is properly
loaded in the header of the document.  This value can be ignored
locally with \":booktabs t\" and \":booktabs nil\" LaTeX
attributes."
  :group 'org-export-latex
  :version "24.4"
  :package-version '(Org . "8.0")
Rasmus's avatar
Rasmus committed
819 820
  :type 'boolean
  :safe #'booleanp)
Bastien Guerry's avatar
Bastien Guerry committed
821

Bastien's avatar
Bastien committed
822
(defcustom org-latex-table-scientific-notation nil
Bastien Guerry's avatar
Bastien Guerry committed
823
  "Format string to display numbers in scientific notation.
Bastien's avatar
Bastien committed
824

Bastien Guerry's avatar
Bastien Guerry committed
825 826 827 828 829 830 831 832 833
The format should have \"%s\" twice, for mantissa and exponent
\(i.e., \"%s\\\\times10^{%s}\").

When nil, no transformation is made."
  :group 'org-export-latex
  :version "24.4"
  :package-version '(Org . "8.0")
  :type '(choice
	  (string :tag "Format string")
Bastien Guerry's avatar
Bastien Guerry committed
834
	  (const :tag "No formatting" nil)))
Bastien Guerry's avatar
Bastien Guerry committed
835 836 837 838

;;;; Text markup

(defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
Rasmus's avatar
Rasmus committed
839
					 (code . protectedtexttt)
Bastien Guerry's avatar
Bastien Guerry committed
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
					 (italic . "\\emph{%s}")
					 (strike-through . "\\sout{%s}")
					 (underline . "\\uline{%s}")
					 (verbatim . protectedtexttt))
  "Alist of LaTeX expressions to convert text markup.

The key must be a symbol among `bold', `code', `italic',
`strike-through', `underline' and `verbatim'.  The value is
a formatting string to wrap fontified text with.

Value can also be set to the following symbols: `verb' and
`protectedtexttt'.  For the former, Org will use \"\\verb\" to
create a format string and select a delimiter character that
isn't in the string.  For the latter, Org will use \"\\texttt\"
to typeset and try to protect special characters.

If no association can be found for a given markup, text will be
returned as-is."
  :group 'org-export-latex
Rasmus's avatar
Rasmus committed
859 860
  :version "26.1"
  :package-version '(Org . "8.3")
Bastien Guerry's avatar
Bastien Guerry committed
861 862 863 864 865 866
  :type 'alist
  :options '(bold code italic strike-through underline verbatim))


;;;; Drawers

Rasmus's avatar
Rasmus committed
867
(defcustom org-latex-format-drawer-function (lambda (_ contents) contents)
Bastien Guerry's avatar
Bastien Guerry committed
868 869 870 871 872 873 874 875
  "Function called to format a drawer in LaTeX code.

The function must accept two parameters:
  NAME      the drawer name, like \"LOGBOOK\"
  CONTENTS  the contents of the drawer.

The function should return the string to be exported.

Bastien Guerry's avatar
Bastien Guerry committed
876
The default function simply returns the value of CONTENTS."
Bastien Guerry's avatar
Bastien Guerry committed
877
  :group 'org-export-latex
Rasmus's avatar
Rasmus committed
878
  :version "26.1"
Bastien Guerry's avatar
Bastien Guerry committed
879
  :package-version '(Org . "8.3")
Bastien Guerry's avatar
Bastien Guerry committed
880 881 882 883 884
  :type 'function)


;;;; Inlinetasks

Rasmus's avatar
Rasmus committed
885 886
(defcustom org-latex-format-inlinetask-function
  'org-latex-format-inlinetask-default-function
Bastien Guerry's avatar
Bastien Guerry committed
887 888
  "Function called to format an inlinetask in LaTeX code.

Rasmus's avatar
Rasmus committed
889 890 891 892 893 894 895 896
The function must accept seven parameters:
  TODO      the todo keyword (string or nil)
  TODO-TYPE the todo type (symbol: `todo', `done', nil)
  PRIORITY  the inlinetask priority (integer or nil)
  NAME      the inlinetask name (string)
  TAGS      the inlinetask tags (list of strings or nil)
  CONTENTS  the contents of the inlinetask (string or nil)
  INFO      the export options (plist)
Bastien Guerry's avatar
Bastien Guerry committed
897

Rasmus's avatar
Rasmus committed
898
The function should return the string to be exported."
Bastien Guerry's avatar
Bastien Guerry committed
899
  :group 'org-export-latex
Rasmus's avatar
Rasmus committed
900 901 902
  :type 'function
  :version "26.1"
  :package-version '(Org . "8.3"))
Bastien Guerry's avatar
Bastien Guerry committed
903 904 905 906 907 908 909 910 911 912 913 914 915


;; Src blocks

(defcustom org-latex-listings nil
  "Non-nil means export source code using the listings package.

This package will fontify source code, possibly even with color.
If you want to use this, you also need to make LaTeX use the
listings package, and if you want to have color, the color
package.  Just add these to `org-latex-packages-alist', for
example using customize, or with something like:

916 917 918
  (require \\='ox-latex)
  (add-to-list \\='org-latex-packages-alist \\='(\"\" \"listings\"))
  (add-to-list \\='org-latex-packages-alist \\='(\"\" \"color\"))
Bastien Guerry's avatar
Bastien Guerry committed
919 920 921

Alternatively,

922
  (setq org-latex-listings \\='minted)
Bastien Guerry's avatar
Bastien Guerry committed
923 924 925 926 927 928

causes source code to be exported using the minted package as
opposed to listings.  If you want to use minted, you need to add
the minted package to `org-latex-packages-alist', for example
using customize, or with

929
  (require \\='ox-latex)
Rasmus's avatar
Rasmus committed
930
  (add-to-list \\='org-latex-packages-alist \\='(\"newfloat\" \"minted\"))
Bastien Guerry's avatar
Bastien Guerry committed
931 932

In addition, it is necessary to install pygments
Rasmus's avatar
Rasmus committed
933
\(URL `http://pygments.org>'), and to configure the variable
Bastien Guerry's avatar
Bastien Guerry committed
934 935 936 937 938 939
`org-latex-pdf-process' so that the -shell-escape option is
passed to pdflatex.

The minted choice has possible repercussions on the preview of
latex fragments (see `org-preview-latex-fragment').  If you run
into previewing problems, please consult
Rasmus's avatar
Rasmus committed
940
URL `https://orgmode.org/worg/org-tutorials/org-latex-preview.html'."
Bastien Guerry's avatar
Bastien Guerry committed
941 942 943 944
  :group 'org-export-latex
  :type '(choice
	  (const :tag "Use listings" t)
	  (const :tag "Use minted" minted)
Rasmus's avatar
Rasmus committed
945 946
	  (const :tag "Export verbatim" nil))
  :safe (lambda (s) (memq s '(t nil minted))))
Bastien Guerry's avatar
Bastien Guerry committed
947 948 949 950 951 952 953 954 955 956 957

(defcustom org-latex-listings-langs
  '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
    (c "C") (cc "C++")
    (fortran "fortran")
    (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
    (html "HTML") (xml "XML")
    (tex "TeX") (latex "[LaTeX]TeX")
    (shell-script "bash")
    (gnuplot "Gnuplot")
    (ocaml "Caml") (caml "Caml")
Rasmus's avatar
Rasmus committed
958 959 960
    (sql "SQL") (sqlite "sql")
    (makefile "make")
    (R "r"))
Bastien Guerry's avatar
Bastien Guerry committed
961 962 963 964 965 966 967
  "Alist mapping languages to their listing language counterpart.
The key is a symbol, the major mode symbol without the \"-mode\".
The value is the string that should be inserted as the language
parameter for the listings package.  If the mode name and the
listings name are the same, the language does not need an entry
in this list - but it does not hurt if it is present."
  :group 'org-export-latex
Rasmus's avatar
Rasmus committed
968
  :version "26.1"
Rasmus's avatar
Rasmus committed
969
  :package-version '(Org . "8.3")
Bastien Guerry's avatar
Bastien Guerry committed
970 971 972 973 974 975 976 977 978 979 980 981 982
  :type '(repeat
	  (list
	   (symbol :tag "Major mode       ")
	   (string :tag "Listings language"))))

(defcustom org-latex-listings-options nil
  "Association list of options for the latex listings package.

These options are supplied as a comma-separated list to the
\\lstset command.  Each element of the association list should be
a list containing two strings: the name of the option, and the
value.  For example,

983
  (setq org-latex-listings-options
984
    \\='((\"basicstyle\" \"\\\\small\")
985
      (\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
Bastien Guerry's avatar
Bastien Guerry committed
986 987 988 989 990

will typeset the code in a small size font with underlined, bold
black keywords.

Note that the same options will be applied to blocks of all
Rasmus's avatar
Rasmus committed
991 992 993 994 995 996 997
languages.  If you need block-specific options, you may use the
following syntax:

  #+ATTR_LATEX: :options key1=value1,key2=value2
  #+BEGIN_SRC <LANG>
  ...
  #+END_SRC"
Bastien Guerry's avatar
Bastien Guerry committed
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
  :group 'org-export-latex
  :type '(repeat
	  (list
	   (string :tag "Listings option name ")
	   (string :tag "Listings option value"))))

(defcustom org-latex-minted-langs
  '((emacs-lisp "common-lisp")
    (cc "c++")
    (cperl "perl")
    (shell-script "bash")
    (caml "ocaml"))
  "Alist mapping languages to their minted language counterpart.
The key is a symbol, the major mode symbol without the \"-mode\".
The value is the string that should be inserted as the language
parameter for the minted package.  If the mode name and the
listings name are the same, the language does not need an entry
in this list - but it does not hurt if it is present.

Note that minted uses all lower case for language identifiers,
and that the full list of language identifiers can be obtained
with:

  pygmentize -L lexers"
  :group 'org-export-latex
  :type '(repeat
	  (list
	   (symbol :tag "Major mode     ")
	   (string :tag "Minted language"))))

(defcustom org-latex-minted-options nil
  "Association list of options for the latex minted package.

These options are supplied within square brackets in
\\begin{minted} environments.  Each element of the alist should
be a list containing two strings: the name of the option, and the
value.  For example,

1036
  (setq org-latex-minted-options
1037
    \\='((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
Bastien Guerry's avatar
Bastien Guerry committed
1038

Bastien's avatar
Bastien committed
1039
will result in source blocks being exported with
Bastien Guerry's avatar
Bastien Guerry committed
1040 1041 1042 1043

\\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}

as the start of the minted environment. Note that the same
Rasmus's avatar
Rasmus committed
1044 1045 1046 1047 1048 1049 1050
options will be applied to blocks of all languages.  If you need
block-specific options, you may use the following syntax:

  #+ATTR_LATEX: :options key1=value1,key2=value2
  #+BEGIN_SRC <LANG>
  ...
  #+END_SRC"
Bastien Guerry's avatar
Bastien Guerry committed
1051 1052 1053 1054 1055 1056
  :group 'org-export-latex
  :type '(repeat
	  (list
	   (string :tag "Minted option name ")
	   (string :tag "Minted option value"))))

Rasmus's avatar
Rasmus committed
1057
(defcustom org-latex-custom-lang-environments nil
Bastien Guerry's avatar
Bastien Guerry committed
1058 1059
  "Alist mapping languages to language-specific LaTeX environments.

Bastien's avatar
Bastien committed
1060 1061 1062 1063 1064 1065 1066
It is used during export of source blocks by the listings and
minted LaTeX packages.  The environment may be a simple string,
composed of only letters and numbers.  In this case, the string
is directly the name of the LaTeX environment to use.  The
environment may also be a format string.  In this case the format
string will be directly exported.  This format string may contain
these elements:
Rasmus's avatar
Rasmus committed
1067 1068 1069 1070 1071 1072 1073 1074

  %s for the formatted source
  %c for the caption
  %f for the float attribute
  %l for an appropriate label
  %o for the LaTeX attributes

For example,
Bastien Guerry's avatar
Bastien Guerry committed
1075

1076
  (setq org-latex-custom-lang-environments
Rasmus's avatar
Rasmus committed
1077 1078 1079 1080 1081 1082
     \\='((python \"pythoncode\")
       (ocaml \"\\\\begin{listing}
\\\\begin{minted}[%o]{ocaml}
%s\\\\end{minted}
\\\\caption{%c}
\\\\label{%l}\")))
Bastien Guerry's avatar
Bastien Guerry committed
1083

Rasmus's avatar
Rasmus committed
1084 1085
would have the effect that if Org encounters a Python source block
during LaTeX export it will produce
Bastien Guerry's avatar
Bastien Guerry committed
1086 1087

  \\begin{pythoncode}
Bastien's avatar
Bastien committed
1088
  <source block body>
Rasmus's avatar
Rasmus committed
1089 1090 1091 1092 1093 1094 1095
  \\end{pythoncode}

and if Org encounters an Ocaml source block during LaTeX export it
will produce

  \\begin{listing}
  \\begin{minted}[<attr_latex options>]{ocaml}
Bastien's avatar
Bastien committed
1096
  <source block body>
Rasmus's avatar
Rasmus committed
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
  \\end{minted}
  \\caption{<caption>}
  \\label{<label>}
  \\end{listing}"
  :group 'org-export-latex
  :type '(repeat
	  (list
	   (symbol :tag "Language name                    ")
	   (string :tag "Environment name or format string")))
  :version "26.1"
  :package-version '(Org . "9.0"))
Bastien Guerry's avatar
Bastien Guerry committed
1108 1109 1110 1111


;;;; Compilation

Rasmus's avatar
Rasmus committed
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
(defcustom org-latex-compiler-file-string "%% Intended LaTeX compiler: %s\n"
  "LaTeX compiler format-string.
See also `org-latex-compiler'."
  :group 'org-export-latex
  :type '(choice
	  (const :tag "Comment" "%% Intended LaTeX compiler: %s\n")
	  (const :tag "latex-mode file variable" "%% -*- latex-run-command: %s -*-\n")
	  (const :tag "AUCTeX file variable" "%% -*- LaTeX-command: %s -*-\n")
	  (string :tag "custom format" "%% %s"))
  :version "26.1"
  :package-version '(Org . "9.0"))

(defcustom org-latex-compiler "pdflatex"
  "LaTeX compiler to use.

Must be an element in `org-latex-compilers' or the empty quote.
Can also be set in buffers via #+LATEX_COMPILER.  See also
`org-latex-compiler-file-string'."
  :group 'org-export-latex
  :type '(choice
	  (const :tag "pdfLaTeX" "pdflatex")
	  (const :tag "XeLaTeX"  "xelatex")
	  (const :tag "LuaLaTeX" "lualatex")
	  (const :tag "Unset" ""))
  :version "26.1"
  :package-version '(Org . "9.0"))

(defconst org-latex-compilers '("pdflatex" "xelatex" "lualatex")
  "Known LaTeX compilers.
See also `org-latex-compiler'.")

(defcustom org-latex-bib-compiler "bibtex"
  "Command to process a LaTeX file's bibliography.

The shorthand %bib in `org-latex-pdf-process' is replaced with
this value.

A better approach is to use a compiler suit such as `latexmk'."
  :group 'org-export-latex
  :type '(choice (const :tag "BibTeX" "bibtex")
		 (const :tag "Biber" "biber")
		 (string :tag "Other process"))
  :version "26.1"
  :package-version '(Org . "9.0"))

Bastien Guerry's avatar
Bastien Guerry committed
1157
(defcustom org-latex-pdf-process
Rasmus's avatar
Rasmus committed
1158 1159 1160
  '("%latex -interaction nonstopmode -output-directory %o %f"
    "%latex -interaction nonstopmode -output-directory %o %f"
    "%latex -interaction nonstopmode -output-directory %o %f")
Bastien Guerry's avatar
Bastien Guerry committed
1161
  "Commands to process a LaTeX file to a PDF file.
Rasmus's avatar
Rasmus committed
1162

Bastien Guerry's avatar
Bastien Guerry committed
1163 1164
This is a list of strings, each of them will be given to the
shell as a command.  %f in the command will be replaced by the
Rasmus's avatar
Rasmus committed
1165 1166 1167 1168 1169 1170
relative file name, %F by the absolute file name, %b by the file
base name (i.e. without directory and extension parts), %o by the
base directory of the file, %O by the absolute file name of the
output file, %latex is the LaTeX compiler (see
`org-latex-compiler'), and %bib is the BibTeX-like compiler (see
`org-latex-bib-compiler').
Bastien Guerry's avatar
Bastien Guerry committed
1171 1172 1173 1174 1175 1176 1177

The reason why this is a list is that it usually takes several
runs of `pdflatex', maybe mixed with a call to `bibtex'.  Org
does not have a clever mechanism to detect which of these
commands have to be run to get to a stable result, and it also
does not do any error checking.

Rasmus's avatar
Rasmus committed
1178 1179
Consider a smart LaTeX compiler such as `texi2dvi' or `latexmk',
which calls the \"correct\" combinations of auxiliary programs.
Bastien Guerry's avatar
Bastien Guerry committed
1180 1181 1182 1183 1184 1185 1186 1187 1188

Alternatively, this may be a Lisp function that does the
processing, so you could use this to apply the machinery of
AUCTeX or the Emacs LaTeX mode.  This function should accept the
file name as its single argument."
  :group 'org-export-pdf
  :type '(choice
	  (repeat :tag "Shell command sequence"
		  (string :tag "Shell command"))
Rasmus's avatar
Rasmus committed
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
	  (const :tag "2 runs of latex"
		 ("%latex -interaction nonstopmode -output-directory %o %f"
		  "%latex -interaction nonstopmode -output-directory %o %f"))
	  (const :tag "3 runs of latex"
		 ("%latex -interaction nonstopmode -output-directory %o %f"
		  "%latex -interaction nonstopmode -output-directory %o %f"
		  "%latex -interaction nonstopmode -output-directory %o %f"))
	  (const :tag "latex,bibtex,latex,latex"
		 ("%latex -interaction nonstopmode -output-directory %o %f"
		  "%bib %b"
		  "%latex -interaction nonstopmode -output-directory %o %f"
		  "%latex -interaction nonstopmode -output-directory %o %f"))
Bastien Guerry's avatar
Bastien Guerry committed
1201
	  (const :tag "texi2dvi"
Rasmus's avatar
Rasmus committed
1202
		 ("cd %o; LATEX=\"%latex\" texi2dvi -p -b -V %b.tex"))
Bastien Guerry's avatar
Bastien Guerry committed
1203
	  (const :tag "latexmk"
Rasmus's avatar
Rasmus committed
1204
		 ("latexmk -g -pdf -pdflatex=\"%latex\" -outdir=%o %f"))
Bastien Guerry's avatar
Bastien Guerry committed
1205 1206 1207
	  (function)))

(defcustom org-latex-logfiles-extensions
Rasmus's avatar
Rasmus committed
1208 1209
  '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
    "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
Bastien Guerry's avatar
Bastien Guerry committed
1210
  "The list of file extensions to consider as LaTeX logfiles.
Rasmus's avatar
Rasmus committed
1211
The logfiles will be removed if `org-latex-remove-logfiles' is
Bastien Guerry's avatar
Bastien Guerry committed
1212 1213
non-nil."
  :group 'org-export-latex
Rasmus's avatar
Rasmus committed
1214 1215
  :version "26.1"
  :package-version '(Org . "8.3")
Bastien Guerry's avatar
Bastien Guerry committed
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
  :type '(repeat (string :tag "Extension")))

(defcustom org-latex-remove-logfiles t
  "Non-nil means remove the logfiles produced by PDF production.
By default, logfiles are files with these extensions: .aux, .idx,
.log, .out, .toc, .nav, .snm and .vrb.  To define the set of
logfiles to remove, set `org-latex-logfiles-extensions'."
  :group 'org-export-latex
  :type 'boolean)

Rasmus's avatar
Rasmus committed
1226 1227 1228 1229 1230 1231 1232
(defcustom org-latex-known-warnings
  '(("Reference.*?undefined" . "[undefined reference]")
    ("Runaway argument" . "[runaway argument]")
    ("Underfull \\hbox" . "[underfull hbox]")
    ("Overfull \\hbox" . "[overfull hbox]")
    ("Citation.*?undefined" . "[undefined citation]")
    ("Undefined control sequence" . "[undefined control sequence]"))
Bastien Guerry's avatar
Bastien Guerry committed
1233
  "Alist of regular expressions and associated messages for the user.
Rasmus's avatar
Rasmus committed
1234
The regular expressions are used to find possible warnings in the
Bastien's avatar
Bastien committed
1235
log of a LaTeX-run.  These warnings will be reported after
Rasmus's avatar
Rasmus committed
1236
calling `org-latex-compile'."
Bastien Guerry's avatar
Bastien Guerry committed
1237
  :group 'org-export-latex
Rasmus's avatar
Rasmus committed
1238 1239
  :version "26.1"
  :package-version '(Org . "8.3")
Bastien Guerry's avatar
Bastien Guerry committed
1240 1241
  :type '(repeat
	  (cons
1242
	   (regexp :tag "Regexp")
Bastien Guerry's avatar
Bastien Guerry committed
1243 1244 1245 1246 1247 1248
	   (string :tag "Message"))))



;;; Internal Functions

Rasmus's avatar
Rasmus committed
1249
(defun org-latex--caption-above-p (element info)
1250
  "Non-nil when caption is expected to be located above ELEMENT.
Rasmus's avatar
Rasmus committed
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
INFO is a plist holding contextual information."
  (let ((above (plist-get info :latex-caption-above)))
    (if (symbolp above) above
      (let ((type (org-element-type element)))
	(memq (if (eq type 'link) 'image type) above)))))

(defun org-latex--label (datum info &optional force full)
  "Return an appropriate label for DATUM.
DATUM is an element or a `target' type object.  INFO is the
current export state, as a plist.

Return nil if element DATUM has no NAME or VALUE affiliated
keyword or no CUSTOM_ID property, unless FORCE is non-nil.  In
this case always return a unique label.

Eventually, if FULL is non-nil, wrap label within \"\\label{}\"."
  (let* ((type (org-element-type datum))
	 (user-label
	  (org-element-property
	   (cl-case type
	     ((headline inlinetask) :CUSTOM_ID)
	     (target :value)
	     (otherwise :name))
	   datum))
	 (label
	  (and (or user-label force)
	       (if (and user-label (plist-get info :latex-prefer-user-labels))
		   user-label
Bastien's avatar
Bastien committed
1279 1280 1281 1282
		 (concat (pcase type
			   (`headline "sec:")
			   (`table "tab:")
			   (`latex-environment
Rasmus's avatar
Rasmus committed
1283 1284 1285 1286
			    (and (string-match-p
				  org-latex-math-environments-re
				  (org-element-property :value datum))
				 "eq:"))
Bastien's avatar
Bastien committed
1287 1288
			   (`latex-matrices "eq:")
			   (`paragraph
Rasmus's avatar
Rasmus committed
1289
			    (and (org-element-property :caption datum)
Bastien's avatar
Bastien committed
1290 1291
				 "fig:"))
			   (_ nil))
Rasmus's avatar
Rasmus committed
1292 1293 1294 1295 1296 1297 1298
			 (org-export-get-reference datum info))))))
    (cond ((not full) label)
	  (label (format "\\label{%s}%s"
			 label
			 (if (eq type 'target) "" "\n")))
	  (t ""))))

Bastien Guerry's avatar
Bastien Guerry committed
1299 1300 1301 1302 1303 1304 1305
(defun org-latex--caption/label-string (element info)
  "Return caption and label LaTeX string for ELEMENT.

INFO is a plist holding contextual information.  If there's no
caption nor label, return the empty string.

For non-floats, see `org-latex--wrap-label'."
Rasmus's avatar
Rasmus committed
1306
  (let* ((label (org-latex--label element info nil t))
Bastien Guerry's avatar
Bastien Guerry committed
1307
	 (main (org-export-get-caption element))
Rasmus's avatar
Rasmus committed
1308 1309 1310 1311 1312 1313 1314 1315
	 (attr (org-export-read-attribute :attr_latex element))
	 (type (org-element-type element))
	 (nonfloat (or (and (plist-member attr :float)
			    (not (plist-get attr :float))
			    main)
		       (and (eq type 'src-block)
			    (not (plist-get attr :float))
			    (null (plist-get info :latex-listings)))))
Bastien Guerry's avatar
Bastien Guerry committed
1316
	 (short (org-export-get-caption element t))
Rasmus's avatar
Rasmus committed
1317
	 (caption-from-attr-latex (plist-get attr :caption)))
Bastien Guerry's avatar
Bastien Guerry committed
1318 1319 1320
    (cond
     ((org-string-nw-p caption-from-attr-latex)
      (concat caption-from-attr-latex "\n"))
Rasmus's avatar
Rasmus committed
1321 1322
     ((and (not main) (equal label "")) "")
     ((not main) label)
Bastien Guerry's avatar
Bastien Guerry committed
1323
     ;; Option caption format with short name.
Rasmus's avatar
Rasmus committed
1324 1325 1326
     (t
      (format (if nonfloat "\\captionof{%s}%s{%s%s}\n"
		"\\caption%s%s{%s%s}\n")
Rasmus's avatar
Rasmus committed
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
	      (let ((type* (if (eq type 'latex-environment)
			       (org-latex--environment-type element)
			     type)))
		(if nonfloat
		    (cl-case type*
		      (paragraph "figure")
		      (image "figure")
		      (special-block "figure")
		      (src-block (if (plist-get info :latex-listings)
				     "listing"
				   "figure"))
		      (t (symbol-name type*)))
		  ""))
Rasmus's avatar
Rasmus committed
1340
	      (if short (format "[%s]" (org-export-data short info)) "")
Bastien's avatar
Bastien committed
1341
	      (org-trim label)
Rasmus's avatar
Rasmus committed
1342
	      (org-export-data main info))))))
Bastien Guerry's avatar
Bastien Guerry committed
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384

(defun org-latex-guess-inputenc (header)
  "Set the coding system in inputenc to what the buffer is.

HEADER is the LaTeX header string.  This function only applies
when specified inputenc option is \"AUTO\".

Return the new header, as a string."
  (let* ((cs (or (ignore-errors
		   (latexenc-coding-system-to-inputenc
		    (or org-export-coding-system buffer-file-coding-system)))
		 "utf8")))
    (if (not cs) header
      ;; First translate if that is requested.
      (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
      ;; Then find the \usepackage statement and replace the option.
      (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
				cs header t nil 1))))

(defun org-latex-guess-babel-language (header info)
  "Set Babel's language according to LANGUAGE keyword.

HEADER is the LaTeX header string.  INFO is the plist used as
a communication channel.

Insertion of guessed language only happens when Babel package has
explicitly been loaded.  Then it is added to the rest of
package's options.

The argument to Babel may be \"AUTO\" which is then replaced with
the language of the document or `org-export-default-language'
unless language in question is already loaded.

Return the new header."
  (let ((language-code (plist-get info :language)))
    ;; If no language is set or Babel package is not loaded, return
    ;; HEADER as-is.
    (if (or (not (stringp language-code))
	    (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
	header
      (let ((options (save-match-data
		       (org-split-string (match-string 1 header) ",[ \t]*")))
Rasmus's avatar
Rasmus committed
1385 1386
	    (language (cdr (assoc-string language-code
					 org-latex-babel-language-alist t))))
Bastien Guerry's avatar
Bastien Guerry committed
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
	;; If LANGUAGE is already loaded, return header without AUTO.
	;; Otherwise, replace AUTO with language or append language if
	;; AUTO is not present.
	(replace-match
	 (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
		    (cond ((member language options) (delete "AUTO" options))
			  ((member "AUTO" options) options)
			  (t (append options (list language))))
		    ", ")
	 t nil header 1)))))

Rasmus's avatar
Rasmus committed
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
(defun org-latex-guess-polyglossia-language (header info)
  "Set the Polyglossia language according to the LANGUAGE keyword.

HEADER is the LaTeX header string.  INFO is the plist used as
a communication channel.

Insertion of guessed language only happens when the Polyglossia
package has been explicitly loaded.

The argument to Polyglossia may be \"AUTO\" which is then
replaced with the language of the document or
`org-export-default-language'.  Note, the language is really set
using \setdefaultlanguage and not as an option to the package.

Return the new header."
  (let ((language (plist-get info :language)))
    ;; If no language is set or Polyglossia is not loaded, return
    ;; HEADER as-is.
    (if (or (not (stringp language))
	    (not (string-match
		  "\\\\usepackage\\(?:\\[\\([^]]+?\\)\\]\\){polyglossia}\n"
		  header)))
	header
      (let* ((options (org-string-nw-p (match-string 1 header)))
	     (languages (and options
			     ;; Reverse as the last loaded language is
			     ;; the main language.
			     (nreverse
			      (delete-dups
			       (save-match-data
				 (org-split-string
				  (replace-regexp-in-string
				   "AUTO" language options t)
				  ",[ \t]*"))))))
	     (main-language-set
	      (string-match-p "\\\\setmainlanguage{.*?}" header)))
	(replace-match
	 (concat "\\usepackage{polyglossia}\n"
		 (mapconcat
		  (lambda (l)
		    (let ((l (or (assoc l org-latex-polyglossia-language-alist)
				 l)))
		      (format (if main-language-set "\\setotherlanguage%s{%s}\n"
				(setq main-language-set t)
				"\\setmainlanguage%s{%s}\n")
			      (if (and (consp l) (= (length l) 3))
				  (format "[variant=%s]" (nth 2 l))
				"")
			      (nth 1 l))))
		  languages
		  ""))
	 t t header 0)))))

(defun org-latex--remove-packages (pkg-alist info)
  "Remove packages based on the current LaTeX compiler.

Bastien's avatar
Bastien committed
1454 1455 1456 1457
PKG-ALIST is a list of packages, as in `org-latex-packages-alist'
and `org-latex-default-packages-alist'.  If the fourth argument
of a package is neither nil nor a member of the LaTeX compiler
associated to the document, the package is removed.
Rasmus's avatar
Rasmus committed
1458

Bastien's avatar
Bastien committed
1459
Return new list of packages."
Rasmus's avatar
Rasmus committed
1460
  (let ((compiler (or (plist-get info :latex-compiler) "")))
Bastien's avatar
Bastien committed
1461 1462 1463 1464 1465 1466 1467 1468
    (if (not (member-ignore-case compiler org-latex-compilers)) pkg-alist
      (cl-remove-if-not
       (lambda (package)
	 (pcase package
	   (`(,_ ,_ ,_ nil) t)
	   (`(,_ ,_ ,_ ,compilers) (member-ignore-case compiler compilers))
	   (_ t)))
       pkg-alist))))
Rasmus's avatar
Rasmus committed
1469

Bastien Guerry's avatar
Bastien Guerry committed
1470 1471 1472 1473
(defun org-latex--find-verb-separator (s)
  "Return a character not used in string S.
This is used to choose a separator for constructs like \\verb."
  (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
Rasmus's avatar
Rasmus committed
1474 1475 1476
    (cl-loop for c across ll
	     when (not (string-match (regexp-quote (char-to-string c)) s))
	     return (char-to-string c))))
Bastien Guerry's avatar
Bastien Guerry committed
1477 1478 1479 1480 1481 1482 1483

(defun org-latex--make-option-string (options)
  "Return a comma separated string of keywords and values.
OPTIONS is an alist where the key is the options keyword as
a string, and the value a list containing the keyword value, or
nil."
  (mapconcat (lambda (pair)
Rasmus's avatar
Rasmus committed
1484 1485 1486 1487
	       (pcase-let ((`(,keyword ,value) pair))
		 (concat keyword
			 (and (> (length value) 0)
			      (concat "=" value)))))
Bastien Guerry's avatar
Bastien Guerry committed
1488 1489 1490
	     options
	     ","))

Rasmus's avatar
Rasmus committed
1491
(defun org-latex--wrap-label (element output info)
Bastien Guerry's avatar
Bastien Guerry committed
1492
  "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
Rasmus's avatar
Rasmus committed
1493 1494
INFO is the current export state, as a plist.  This function
should not be used for floats.  See
Bastien Guerry's avatar
Bastien Guerry committed
1495
`org-latex--caption/label-string'."
Rasmus's avatar
Rasmus committed
1496 1497 1498 1499 1500
  (if (not (and (org-string-nw-p output) (org-element-property :name element)))
      output
    (concat (format "\\phantomsection\n\\label{%s}\n"
		    (org-latex--label element info))
	    output)))
Bastien Guerry's avatar
Bastien Guerry committed
1501

Rasmus's avatar
Rasmus committed
1502 1503 1504 1505 1506
(defun org-latex--protect-text (text)
  "Protect special characters in string TEXT and return it."
  (replace-regexp-in-string "[\\{}$%&_#~^]" "\\\\\\&" text))

(defun org-latex--text-markup (text markup info)
Bastien Guerry's avatar
Bastien Guerry committed
1507
  "Format TEXT depending on MARKUP text markup.
Rasmus's avatar
Rasmus committed
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
INFO is a plist used as a communication channel.  See
`org-latex-text-markup-alist' for details."
  (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
    (cl-case fmt
      ;; No format string: Return raw text.
      ((nil) text)
      ;; Handle the `verb' special case: Find an appropriate separator
      ;; and use "\\verb" command.
      (verb
       (let ((separator (org-latex--find-verb-separator text)))
	 (concat "\\verb"
		 separator
		 (replace-regexp-in-string "\n" " " text)
		 separator)))
      ;; Handle the `protectedtexttt' special case: Protect some
      ;; special chars and use "\texttt{%s}" format string.
      (protectedtexttt
       (format "\\texttt{%s}"
	       (replace-regexp-in-string
		"--\\|[\\{}$%&_#~^]"
		(lambda (m)
		  (cond ((equal m "--") "-{}-")
			((equal m "\\") "\\textbackslash{}")
			((equal m "~") "\\textasciitilde{}")
			((equal m "^") "\\textasciicircum{}")
			(t (org-latex--protect-text m))))
		text nil t)))
      ;; Else use format string.
      (t (format fmt text)))))
Bastien Guerry's avatar
Bastien Guerry committed
1537 1538 1539 1540 1541 1542

(defun org-latex--delayed-footnotes-definitions (element info)
  "Return footnotes definitions in ELEMENT as a string.

INFO is a plist used as a communication channel.

Rasmus's avatar
Rasmus committed
1543
Footnotes definitions are returned within \"\\footnotetext{}\"
Bastien Guerry's avatar
Bastien Guerry committed
1544 1545 1546
commands.

This function is used within constructs that don't support
Rasmus's avatar
Rasmus committed
1547
\"\\footnote{}\" command (e.g., an item tag).  In that case,
Bastien Guerry's avatar
Bastien Guerry committed
1548 1549 1550 1551
\"\\footnotemark\" is used within the construct and the function
just outside of it."
  (mapconcat
   (lambda (ref)
Rasmus's avatar
Rasmus committed
1552 1553 1554 1555 1556
     (let ((def (org-export-get-footnote-definition ref info)))
       (format "\\footnotetext[%d]{%s%s}"
	       (org-export-get-footnote-number ref info)
	       (org-trim (org-latex--label def info t t))
	       (org-trim (org-export-data def info)))))
Bastien Guerry's avatar
Bastien Guerry committed
1557
   ;; Find every footnote reference in ELEMENT.
Rasmus's avatar
Rasmus committed
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
   (letrec ((all-refs nil)
	    (search-refs
	     (lambda (data)
	       ;; Return a list of all footnote references never seen
	       ;; before in DATA.
	       (org-element-map data 'footnote-reference
		 (lambda (ref)
		   (when (org-export-footnote-first-reference-p ref info)
		     (push ref all-refs)
		     (when (eq (org-element-property :type ref) 'standard)
		       (funcall search-refs
				(org-export-get-footnote-definition ref info)))))
		 info)
	       (reverse all-refs))))
Bastien Guerry's avatar
Bastien Guerry committed
1572 1573 1574
     (funcall search-refs element))
   ""))

Rasmus's avatar
Rasmus committed
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
(defun org-latex--translate (s info)
  "Translate string S according to specified language.
INFO is a plist used as a communication channel."
  (org-export-translate s :latex info))

(defun org-latex--format-spec (info)
  "Create a format-spec for document meta-data.
INFO is a plist used as a communication channel."
  (let ((language (let ((lang (plist-get info :language)))
		    (or (cdr (assoc-string lang org-latex-babel-language-alist t))
			(nth 1 (assoc-string lang org-latex-polyglossia-language-alist t))
			lang))))
    `((?a . ,(org-export-data (plist-get info :author) info))
      (?t . ,(org-export-data (plist-get info :title) info))
      (?k . ,(org-export-data (org-latex--wrap-latex-math-block
			       (plist-get info :keywords) info)
			      info))
      (?d . ,(org-export-data (org-latex--wrap-latex-math-block
			       (plist-get info :description) info)
			      info))
      (?c . ,(plist-get info :creator))
      (?l . ,language)
      (?L . ,(capitalize language))
      (?D . ,(org-export-get-date info)))))

(defun org-latex--insert-compiler (info)
  "Insert LaTeX_compiler info into the document.
INFO is a plist used as a communication channel."
  (let ((compiler (plist-get info :latex-compiler)))
       (and (org-string-nw-p org-latex-compiler-file-string)
	    (member (or compiler "") org-latex-compilers)
	    (format org-latex-compiler-file-string compiler))))


;;; Filters

(defun org-latex-matrices-tree-filter (tree _backend info)
  (org-latex--wrap-latex-matrices tree info))

(defun org-latex-math-block-tree-filter (tree _backend info)
  (org-latex--wrap-latex-math-block tree info))