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