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