Merge branch 'maint'
[org-mode.git] / lisp / ox-latex.el
blobf888085b340459796abe09ac5343649ae4475e45
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 (R "r"))
929 "Alist mapping languages to their listing language counterpart.
930 The key is a symbol, the major mode symbol without the \"-mode\".
931 The value is the string that should be inserted as the language
932 parameter for the listings package. If the mode name and the
933 listings name are the same, the language does not need an entry
934 in this list - but it does not hurt if it is present."
935 :group 'org-export-latex
936 :version "24.4"
937 :package-version '(Org . "8.3")
938 :type '(repeat
939 (list
940 (symbol :tag "Major mode ")
941 (string :tag "Listings language"))))
943 (defcustom org-latex-listings-options nil
944 "Association list of options for the latex listings package.
946 These options are supplied as a comma-separated list to the
947 \\lstset command. Each element of the association list should be
948 a list containing two strings: the name of the option, and the
949 value. For example,
951 (setq org-latex-listings-options
952 \\='((\"basicstyle\" \"\\\\small\")
953 (\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
955 will typeset the code in a small size font with underlined, bold
956 black keywords.
958 Note that the same options will be applied to blocks of all
959 languages. If you need block-specific options, you may use the
960 following syntax:
962 #+ATTR_LATEX: :options key1=value1,key2=value2
963 #+BEGIN_SRC <LANG>
965 #+END_SRC"
966 :group 'org-export-latex
967 :type '(repeat
968 (list
969 (string :tag "Listings option name ")
970 (string :tag "Listings option value"))))
972 (defcustom org-latex-minted-langs
973 '((emacs-lisp "common-lisp")
974 (cc "c++")
975 (cperl "perl")
976 (shell-script "bash")
977 (caml "ocaml"))
978 "Alist mapping languages to their minted language counterpart.
979 The key is a symbol, the major mode symbol without the \"-mode\".
980 The value is the string that should be inserted as the language
981 parameter for the minted package. If the mode name and the
982 listings name are the same, the language does not need an entry
983 in this list - but it does not hurt if it is present.
985 Note that minted uses all lower case for language identifiers,
986 and that the full list of language identifiers can be obtained
987 with:
989 pygmentize -L lexers"
990 :group 'org-export-latex
991 :type '(repeat
992 (list
993 (symbol :tag "Major mode ")
994 (string :tag "Minted language"))))
996 (defcustom org-latex-minted-options nil
997 "Association list of options for the latex minted package.
999 These options are supplied within square brackets in
1000 \\begin{minted} environments. Each element of the alist should
1001 be a list containing two strings: the name of the option, and the
1002 value. For example,
1004 (setq org-latex-minted-options
1005 '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
1007 will result in src blocks being exported with
1009 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
1011 as the start of the minted environment. Note that the same
1012 options will be applied to blocks of all languages. If you need
1013 block-specific options, you may use the following syntax:
1015 #+ATTR_LATEX: :options key1=value1,key2=value2
1016 #+BEGIN_SRC <LANG>
1018 #+END_SRC"
1019 :group 'org-export-latex
1020 :type '(repeat
1021 (list
1022 (string :tag "Minted option name ")
1023 (string :tag "Minted option value"))))
1025 (defvar org-latex-custom-lang-environments nil
1026 "Alist mapping languages to language-specific LaTeX environments.
1028 It is used during export of src blocks by the listings and minted
1029 latex packages. For example,
1031 (setq org-latex-custom-lang-environments
1032 '((python \"pythoncode\")))
1034 would have the effect that if org encounters begin_src python
1035 during latex export it will output
1037 \\begin{pythoncode}
1038 <src block body>
1039 \\end{pythoncode}")
1042 ;;;; Compilation
1044 (defcustom org-latex-compiler-file-string "%% Indented LaTeX compiler: %s\n"
1045 "LaTeX compiler format-string.
1046 See also `org-latex-compiler'."
1047 :group 'org-export-latex
1048 :type '(choice
1049 (const :tag "Comment" "%% Indented LaTeX compiler: %s\n")
1050 (const :tag "latex-mode file variable" "%% -*- latex-run-command: %s -*-\n")
1051 (const :tag "AUCTeX file variable" "%% -*- LaTeX-command: %s -*-\n")
1052 (string :tag "custom format" "%% %s"))
1053 :version "25.1"
1054 :package-version '(Org . "9.0"))
1056 (defcustom org-latex-compiler "pdflatex"
1057 "LaTeX compiler to use.
1059 Must be an element in `org-latex-compilers' or the empty quote.
1060 Can also be set in buffers via #+LATEX_COMPILER. See also
1061 `org-latex-compiler-file-string'."
1062 :group 'org-export-latex
1063 :type '(choice
1064 (const :tag "pdfLaTeX" "pdflatex")
1065 (const :tag "XeLaTeX" "xelatex")
1066 (const :tag "LuaLaTeX" "lualatex")
1067 (const :tag "Unset" ""))
1068 :version "25.1"
1069 :package-version '(Org . "9.0"))
1071 (defconst org-latex-compilers '("pdflatex" "xelatex" "lualatex")
1072 "Known LaTeX compilers.
1073 See also `org-latex-compiler'.")
1075 (defcustom org-latex-bib-compiler "bibtex"
1076 "Command to process a LaTeX file's bibliography.
1078 The shorthand %bib in `org-latex-pdf-process' is replaced with
1079 this value.
1081 A better approach is to use a compiler suit such as `latexmk'."
1082 :group 'org-export-latex
1083 :type '(choice (const :tag "BibTeX" "bibtex")
1084 (const :tag "Biber" "biber")
1085 (string :tag "Other process"))
1086 :version "25.1"
1087 :package-version '(Org . "9.0"))
1089 (defcustom org-latex-pdf-process
1090 '("%latex -interaction nonstopmode -output-directory %o %f"
1091 "%latex -interaction nonstopmode -output-directory %o %f"
1092 "%latex -interaction nonstopmode -output-directory %o %f")
1093 "Commands to process a LaTeX file to a PDF file.
1094 This is a list of strings, each of them will be given to the
1095 shell as a command. %f in the command will be replaced by the
1096 full file name, %b by the file base name (i.e. without directory
1097 and extension parts), %o by the base directory of the file,
1098 %latex is the LaTeX compiler (see `org-latex-compiler'), and %bib
1099 is the BibTeX-like compiler (see `org-latex-bib-compiler').
1101 The reason why this is a list is that it usually takes several
1102 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
1103 does not have a clever mechanism to detect which of these
1104 commands have to be run to get to a stable result, and it also
1105 does not do any error checking.
1107 Consider a smart LaTeX compiler such as `texi2dvi' or `latexmk',
1108 which calls the \"correct\" combinations of auxiliary programs.
1110 Alternatively, this may be a Lisp function that does the
1111 processing, so you could use this to apply the machinery of
1112 AUCTeX or the Emacs LaTeX mode. This function should accept the
1113 file name as its single argument."
1114 :group 'org-export-pdf
1115 :type '(choice
1116 (repeat :tag "Shell command sequence"
1117 (string :tag "Shell command"))
1118 (const :tag "2 runs of latex"
1119 ("%latex -interaction nonstopmode -output-directory %o %f"
1120 "%latex -interaction nonstopmode -output-directory %o %f"))
1121 (const :tag "3 runs of latex"
1122 ("%latex -interaction nonstopmode -output-directory %o %f"
1123 "%latex -interaction nonstopmode -output-directory %o %f"
1124 "%latex -interaction nonstopmode -output-directory %o %f"))
1125 (const :tag "latex,bibtex,latex,latex"
1126 ("%latex -interaction nonstopmode -output-directory %o %f"
1127 "%bib %b"
1128 "%latex -interaction nonstopmode -output-directory %o %f"
1129 "%latex -interaction nonstopmode -output-directory %o %f"))
1130 (const :tag "texi2dvi"
1131 ("LATEX=\"%latex\" texi2dvi -p -b -V %f"))
1132 (const :tag "latexmk"
1133 ("latexmk -g -pdflatex=\"%latex\" %f"))
1134 (function)))
1136 (defcustom org-latex-logfiles-extensions
1137 '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
1138 "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
1139 "The list of file extensions to consider as LaTeX logfiles.
1140 The logfiles will be removed if `org-latex-remove-logfiles' is
1141 non-nil."
1142 :group 'org-export-latex
1143 :version "25.1"
1144 :package-version '(Org . "8.3")
1145 :type '(repeat (string :tag "Extension")))
1147 (defcustom org-latex-remove-logfiles t
1148 "Non-nil means remove the logfiles produced by PDF production.
1149 By default, logfiles are files with these extensions: .aux, .idx,
1150 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
1151 logfiles to remove, set `org-latex-logfiles-extensions'."
1152 :group 'org-export-latex
1153 :type 'boolean)
1155 (defcustom org-latex-known-warnings
1156 '(("Reference.*?undefined" . "[undefined reference]")
1157 ("Runaway argument" . "[runaway argument]")
1158 ("Underfull \\hbox" . "[underfull hbox]")
1159 ("Overfull \\hbox" . "[overfull hbox]")
1160 ("Citation.*?undefined" . "[undefined citation]")
1161 ("Undefined control sequence" . "[undefined control sequence]"))
1162 "Alist of regular expressions and associated messages for the user.
1163 The regular expressions are used to find possible warnings in the
1164 log of a latex-run. These warnings will be reported after
1165 calling `org-latex-compile'."
1166 :group 'org-export-latex
1167 :version "25.1"
1168 :package-version '(Org . "8.3")
1169 :type '(repeat
1170 (cons
1171 (string :tag "Regexp")
1172 (string :tag "Message"))))
1176 ;;; Internal Functions
1178 (defun org-latex--caption-above-p (element info)
1179 "Non nil when caption is expected to be located above ELEMENT.
1180 INFO is a plist holding contextual information."
1181 (let ((above (plist-get info :latex-caption-above)))
1182 (if (symbolp above) above
1183 (let ((type (org-element-type element)))
1184 (memq (if (eq type 'link) 'image type) above)))))
1186 (defun org-latex--label (datum info &optional force full)
1187 "Return an appropriate label for DATUM.
1188 DATUM is an element or a `target' type object. INFO is the
1189 current export state, as a plist.
1191 Return nil if element DATUM has no NAME or VALUE affiliated
1192 keyword or no CUSTOM_ID property, unless FORCE is non-nil. In
1193 this case always return a unique label.
1195 Eventually, if FULL is non-nil, wrap label within \"\\label{}\"."
1196 (let* ((type (org-element-type datum))
1197 (user-label
1198 (org-element-property
1199 (case type
1200 ((headline inlinetask) :CUSTOM_ID)
1201 (target :value)
1202 (otherwise :name))
1203 datum))
1204 (label
1205 (and (or user-label force)
1206 (if (and user-label (plist-get info :latex-prefer-user-labels))
1207 user-label
1208 (concat (case type
1209 (headline "sec:")
1210 (table "tab:")
1211 (latex-environment
1212 (and (org-string-match-p
1213 org-latex-math-environments-re
1214 (org-element-property :value datum))
1215 "eq:"))
1216 (paragraph
1217 (and (org-element-property :caption datum)
1218 "fig:")))
1219 (org-export-get-reference datum info))))))
1220 (cond ((not full) label)
1221 (label (format "\\label{%s}%s"
1222 label
1223 (if (eq type 'target) "" "\n")))
1224 (t ""))))
1226 (defun org-latex--caption/label-string (element info)
1227 "Return caption and label LaTeX string for ELEMENT.
1229 INFO is a plist holding contextual information. If there's no
1230 caption nor label, return the empty string.
1232 For non-floats, see `org-latex--wrap-label'."
1233 (let* ((label (org-latex--label element info nil t))
1234 (main (org-export-get-caption element))
1235 (attr (org-export-read-attribute :attr_latex element))
1236 (type (org-element-type element))
1237 (nonfloat (or (and (plist-member attr :float)
1238 (not (plist-get attr :float))
1239 main)
1240 (and (eq type 'src-block)
1241 (not (plist-get attr :float))
1242 (memq (plist-get info :latex-listings)
1243 '(nil minted)))))
1244 (short (org-export-get-caption element t))
1245 (caption-from-attr-latex (plist-get attr :caption)))
1246 (cond
1247 ((org-string-nw-p caption-from-attr-latex)
1248 (concat caption-from-attr-latex "\n"))
1249 ((and (not main) (equal label "")) "")
1250 ((not main) (concat label "\n"))
1251 ;; Option caption format with short name.
1253 (format (if nonfloat "\\captionof{%s}%s{%s%s}\n"
1254 "\\caption%s%s{%s%s}\n")
1255 (if nonfloat
1256 (case type
1257 (paragraph "figure")
1258 (src-block (if (plist-get info :latex-listings)
1259 "listing"
1260 "figure"))
1261 (t (symbol-name type)))
1263 (if short (format "[%s]" (org-export-data short info)) "")
1264 label
1265 (org-export-data main info))))))
1267 (defun org-latex-guess-inputenc (header)
1268 "Set the coding system in inputenc to what the buffer is.
1270 HEADER is the LaTeX header string. This function only applies
1271 when specified inputenc option is \"AUTO\".
1273 Return the new header, as a string."
1274 (let* ((cs (or (ignore-errors
1275 (latexenc-coding-system-to-inputenc
1276 (or org-export-coding-system buffer-file-coding-system)))
1277 "utf8")))
1278 (if (not cs) header
1279 ;; First translate if that is requested.
1280 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
1281 ;; Then find the \usepackage statement and replace the option.
1282 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1283 cs header t nil 1))))
1285 (defun org-latex-guess-babel-language (header info)
1286 "Set Babel's language according to LANGUAGE keyword.
1288 HEADER is the LaTeX header string. INFO is the plist used as
1289 a communication channel.
1291 Insertion of guessed language only happens when Babel package has
1292 explicitly been loaded. Then it is added to the rest of
1293 package's options.
1295 The argument to Babel may be \"AUTO\" which is then replaced with
1296 the language of the document or `org-export-default-language'
1297 unless language in question is already loaded.
1299 Return the new header."
1300 (let ((language-code (plist-get info :language)))
1301 ;; If no language is set or Babel package is not loaded, return
1302 ;; HEADER as-is.
1303 (if (or (not (stringp language-code))
1304 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
1305 header
1306 (let ((options (save-match-data
1307 (org-split-string (match-string 1 header) ",[ \t]*")))
1308 (language (cdr (assoc language-code
1309 org-latex-babel-language-alist))))
1310 ;; If LANGUAGE is already loaded, return header without AUTO.
1311 ;; Otherwise, replace AUTO with language or append language if
1312 ;; AUTO is not present.
1313 (replace-match
1314 (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
1315 (cond ((member language options) (delete "AUTO" options))
1316 ((member "AUTO" options) options)
1317 (t (append options (list language))))
1318 ", ")
1319 t nil header 1)))))
1321 (defun org-latex-guess-polyglossia-language (header info)
1322 "Set the Polyglossia language according to the LANGUAGE keyword.
1324 HEADER is the LaTeX header string. INFO is the plist used as
1325 a communication channel.
1327 Insertion of guessed language only happens when the Polyglossia
1328 package has been explicitly loaded.
1330 The argument to Polyglossia may be \"AUTO\" which is then
1331 replaced with the language of the document or
1332 `org-export-default-language'. Note, the language is really set
1333 using \setdefaultlanguage and not as an option to the package.
1335 Return the new header."
1336 (let ((language (plist-get info :language)))
1337 ;; If no language is set or Polyglossia is not loaded, return
1338 ;; HEADER as-is.
1339 (if (or (not (stringp language))
1340 (not (string-match
1341 "\\\\usepackage\\(?:\\[\\([^]]+?\\)\\]\\){polyglossia}\n"
1342 header)))
1343 header
1344 (let* ((options (org-string-nw-p (match-string 1 header)))
1345 (languages (and options
1346 ;; Reverse as the last loaded language is
1347 ;; the main language.
1348 (nreverse
1349 (delete-dups
1350 (save-match-data
1351 (org-split-string
1352 (replace-regexp-in-string
1353 "AUTO" language options t)
1354 ",[ \t]*"))))))
1355 (main-language-set
1356 (string-match-p "\\\\setmainlanguage{.*?}" header)))
1357 (replace-match
1358 (concat "\\usepackage{polyglossia}\n"
1359 (mapconcat
1360 (lambda (l)
1361 (let ((l (or (assoc l org-latex-polyglossia-language-alist)
1362 l)))
1363 (format (if main-language-set "\\setotherlanguage%s{%s}\n"
1364 (setq main-language-set t)
1365 "\\setmainlanguage%s{%s}\n")
1366 (if (and (consp l) (= (length l) 3))
1367 (format "[variant=%s]" (nth 2 l))
1369 (nth 1 l))))
1370 languages
1371 ""))
1372 t t header 0)))))
1374 (defun org-latex--remove-packages (pkg-alist info)
1375 "Remove packages based on the current LaTeX compiler.
1377 If the fourth argument of an element is set in pkg-alist, and it
1378 is not a member of the LaTeX compiler of the document, the packages
1379 is removed. See also `org-latex-compiler'.
1381 Return modified pkg-alist."
1382 (let ((compiler (or (plist-get info :latex-compiler) "")))
1383 (if (member-ignore-case compiler org-latex-compilers)
1384 (delq nil
1385 (mapcar
1386 (lambda (pkg)
1387 (unless (and
1388 (listp pkg)
1389 (let ((third (nth 3 pkg)))
1390 (and third
1391 (not (member-ignore-case
1392 compiler
1393 (if (listp third) third (list third)))))))
1394 pkg))
1395 pkg-alist))
1396 pkg-alist)))
1398 (defun org-latex--find-verb-separator (s)
1399 "Return a character not used in string S.
1400 This is used to choose a separator for constructs like \\verb."
1401 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1402 (loop for c across ll
1403 when (not (string-match (regexp-quote (char-to-string c)) s))
1404 return (char-to-string c))))
1406 (defun org-latex--make-option-string (options)
1407 "Return a comma separated string of keywords and values.
1408 OPTIONS is an alist where the key is the options keyword as
1409 a string, and the value a list containing the keyword value, or
1410 nil."
1411 (mapconcat (lambda (pair)
1412 (concat (first pair)
1413 (when (> (length (second pair)) 0)
1414 (concat "=" (second pair)))))
1415 options
1416 ","))
1418 (defun org-latex--wrap-label (element output info)
1419 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1420 INFO is the current export state, as a plist. This function
1421 should not be used for floats. See
1422 `org-latex--caption/label-string'."
1423 (if (not (and (org-string-nw-p output) (org-element-property :name element)))
1424 output
1425 (concat (format "\\phantomsection\n\\label{%s}\n"
1426 (org-latex--label element info))
1427 output)))
1429 (defun org-latex--protect-text (text)
1430 "Protect special characters in string TEXT and return it."
1431 (replace-regexp-in-string
1432 "--\\|[\\{}$%&_#~^]"
1433 (lambda (m)
1434 (cond ((equal m "--") "-{}-")
1435 ((equal m "\\") "\\textbackslash{}")
1436 ((equal m "~") "\\textasciitilde{}")
1437 ((equal m "^") "\\textasciicircum{}")
1438 (t (concat "\\" m))))
1439 text nil t))
1441 (defun org-latex--text-markup (text markup info)
1442 "Format TEXT depending on MARKUP text markup.
1443 INFO is a plist used as a communication channel. See
1444 `org-latex-text-markup-alist' for details."
1445 (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
1446 (case fmt
1447 ;; No format string: Return raw text.
1448 ((nil) text)
1449 ;; Handle the `verb' special case: Find an appropriate separator
1450 ;; and use "\\verb" command.
1451 (verb
1452 (let ((separator (org-latex--find-verb-separator text)))
1453 (concat "\\verb" separator
1454 (replace-regexp-in-string "\n" " " text)
1455 separator)))
1456 ;; Handle the `protectedtexttt' special case: Protect some
1457 ;; special chars and use "\texttt{%s}" format string.
1458 (protectedtexttt
1459 (format "\\texttt{%s}" (org-latex--protect-text text)))
1460 ;; Else use format string.
1461 (t (format fmt text)))))
1463 (defun org-latex--delayed-footnotes-definitions (element info)
1464 "Return footnotes definitions in ELEMENT as a string.
1466 INFO is a plist used as a communication channel.
1468 Footnotes definitions are returned within \"\\footnotetxt{}\"
1469 commands.
1471 This function is used within constructs that don't support
1472 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1473 \"\\footnotemark\" is used within the construct and the function
1474 just outside of it."
1475 (mapconcat
1476 (lambda (ref)
1477 (format
1478 "\\footnotetext[%s]{%s}"
1479 (org-export-get-footnote-number ref info)
1480 (org-trim
1481 (org-export-data
1482 (org-export-get-footnote-definition ref info) info))))
1483 ;; Find every footnote reference in ELEMENT.
1484 (let* (all-refs
1485 search-refs ; For byte-compiler.
1486 (search-refs
1487 (function
1488 (lambda (data)
1489 ;; Return a list of all footnote references never seen
1490 ;; before in DATA.
1491 (org-element-map data 'footnote-reference
1492 (lambda (ref)
1493 (when (org-export-footnote-first-reference-p ref info)
1494 (push ref all-refs)
1495 (when (eq (org-element-property :type ref) 'standard)
1496 (funcall search-refs
1497 (org-export-get-footnote-definition ref info)))))
1498 info)
1499 (reverse all-refs)))))
1500 (funcall search-refs element))
1501 ""))
1503 (defun org-latex--translate (s info)
1504 "Translate string S according to specified language.
1505 INFO is a plist used as a communication channel."
1506 (org-export-translate s :latex info))
1508 (defun org-latex--format-spec (info)
1509 "Create a format-spec for document meta-data.
1510 INFO is a plist used as a communication channel."
1511 (let ((language (let ((lang (plist-get info :language)))
1512 (or (cdr (assoc lang org-latex-babel-language-alist))
1513 lang))))
1514 `((?a . ,(org-export-data (plist-get info :author) info))
1515 (?t . ,(org-export-data (plist-get info :title) info))
1516 (?k . ,(org-export-data (org-latex--wrap-latex-math-block
1517 (plist-get info :keywords) info)
1518 info))
1519 (?d . ,(org-export-data (org-latex--wrap-latex-math-block
1520 (plist-get info :description) info)
1521 info))
1522 (?c . ,(plist-get info :creator))
1523 (?l . ,language)
1524 (?L . ,(capitalize language))
1525 (?D . ,(org-export-get-date info)))))
1527 (defun org-latex--make-preamble (info)
1528 "Return a formatted LaTeX preamble.
1529 INFO is a plist used as a communication channel."
1530 (let* ((class (plist-get info :latex-class))
1531 (class-options (plist-get info :latex-class-options))
1532 (header (nth 1 (assoc class (plist-get info :latex-classes))))
1533 (document-class-string
1534 (and (stringp header)
1535 (if (not class-options) header
1536 (replace-regexp-in-string
1537 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1538 class-options header t nil 1)))))
1539 (if (not document-class-string)
1540 (user-error "Unknown LaTeX class `%s'" class)
1541 (org-latex-guess-polyglossia-language
1542 (org-latex-guess-babel-language
1543 (org-latex-guess-inputenc
1544 (org-element-normalize-string
1545 (org-splice-latex-header
1546 document-class-string
1547 (org-latex--remove-packages
1548 org-latex-default-packages-alist info)
1549 (org-latex--remove-packages
1550 org-latex-packages-alist info)
1552 (mapconcat 'org-element-normalize-string
1553 (list (plist-get info :latex-header)
1554 (plist-get info :latex-header-extra)) ""))))
1555 info)
1556 info))))
1558 (defun org-latex--insert-compiler (info)
1559 "Insert LaTeX_compiler info into the document.
1560 INFO is a plist used as a communication channel."
1561 (let ((compiler (plist-get info :latex-compiler)))
1562 (and (org-string-nw-p org-latex-compiler-file-string)
1563 (string-match-p (regexp-opt org-latex-compilers) (or compiler ""))
1564 (format org-latex-compiler-file-string compiler))))
1567 ;;; Template
1569 (defun org-latex-template (contents info)
1570 "Return complete document string after LaTeX conversion.
1571 CONTENTS is the transcoded contents string. INFO is a plist
1572 holding export options."
1573 (let ((title (org-export-data (plist-get info :title) info))
1574 (spec (org-latex--format-spec info)))
1575 (concat
1576 ;; Time-stamp.
1577 (and (plist-get info :time-stamp-file)
1578 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1579 ;; LaTeX compiler.
1580 (org-latex--insert-compiler info)
1581 ;; Document class and packages.
1582 (org-latex--make-preamble info)
1583 ;; Possibly limit depth for headline numbering.
1584 (let ((sec-num (plist-get info :section-numbers)))
1585 (when (integerp sec-num)
1586 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1587 ;; Author.
1588 (let ((author (and (plist-get info :with-author)
1589 (let ((auth (plist-get info :author)))
1590 (and auth (org-export-data auth info)))))
1591 (email (and (plist-get info :with-email)
1592 (org-export-data (plist-get info :email) info))))
1593 (cond ((and author email (not (string= "" email)))
1594 (format "\\author{%s\\thanks{%s}}\n" author email))
1595 ((or author email) (format "\\author{%s}\n" (or author email)))))
1596 ;; Date.
1597 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1598 (format "\\date{%s}\n" (org-export-data date info)))
1599 ;; Title and subtitle.
1600 (let* ((subtitle (plist-get info :subtitle))
1601 (formatted-subtitle
1602 (when subtitle
1603 (format (plist-get info :latex-subtitle-format)
1604 (org-export-data subtitle info))))
1605 (separate (plist-get info :latex-subtitle-separate)))
1606 (concat
1607 (format "\\title{%s%s}\n" title
1608 (if separate "" (or formatted-subtitle "")))
1609 (when (and separate subtitle)
1610 (concat formatted-subtitle "\n"))))
1611 ;; Hyperref options.
1612 (let ((template (plist-get info :latex-hyperref-template)))
1613 (and (stringp template)
1614 (format-spec template spec)))
1615 ;; Document start.
1616 "\\begin{document}\n\n"
1617 ;; Title command.
1618 (let* ((title-command (plist-get info :latex-title-command))
1619 (command (and (stringp title-command)
1620 (format-spec title-command spec))))
1621 (org-element-normalize-string
1622 (cond ((not (plist-get info :with-title)) nil)
1623 ((string= "" title) nil)
1624 ((not (stringp command)) nil)
1625 ((string-match "\\(?:[^%]\\|^\\)%s" command)
1626 (format command title))
1627 (t command))))
1628 ;; Table of contents.
1629 (let ((depth (plist-get info :with-toc)))
1630 (when depth
1631 (concat (when (wholenump depth)
1632 (format "\\setcounter{tocdepth}{%d}\n" depth))
1633 (plist-get info :latex-toc-command))))
1634 ;; Document's body.
1635 contents
1636 ;; Creator.
1637 (and (plist-get info :with-creator)
1638 (concat (plist-get info :creator) "\n"))
1639 ;; Document end.
1640 "\\end{document}")))
1644 ;;; Transcode Functions
1646 ;;;; Bold
1648 (defun org-latex-bold (bold contents info)
1649 "Transcode BOLD from Org to LaTeX.
1650 CONTENTS is the text with bold markup. INFO is a plist holding
1651 contextual information."
1652 (org-latex--text-markup contents 'bold info))
1655 ;;;; Center Block
1657 (defun org-latex-center-block (center-block contents info)
1658 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1659 CONTENTS holds the contents of the center block. INFO is a plist
1660 holding contextual information."
1661 (org-latex--wrap-label
1662 center-block (format "\\begin{center}\n%s\\end{center}" contents) info))
1665 ;;;; Clock
1667 (defun org-latex-clock (clock contents info)
1668 "Transcode a CLOCK element from Org to LaTeX.
1669 CONTENTS is nil. INFO is a plist holding contextual
1670 information."
1671 (concat
1672 "\\noindent"
1673 (format "\\textbf{%s} " org-clock-string)
1674 (format (plist-get info :latex-inactive-timestamp-format)
1675 (concat (org-timestamp-translate (org-element-property :value clock))
1676 (let ((time (org-element-property :duration clock)))
1677 (and time (format " (%s)" time)))))
1678 "\\\\"))
1681 ;;;; Code
1683 (defun org-latex-code (code contents info)
1684 "Transcode a CODE object from Org to LaTeX.
1685 CONTENTS is nil. INFO is a plist used as a communication
1686 channel."
1687 (org-latex--text-markup (org-element-property :value code) 'code info))
1690 ;;;; Drawer
1692 (defun org-latex-drawer (drawer contents info)
1693 "Transcode a DRAWER element from Org to LaTeX.
1694 CONTENTS holds the contents of the block. INFO is a plist
1695 holding contextual information."
1696 (let* ((name (org-element-property :drawer-name drawer))
1697 (output (funcall (plist-get info :latex-format-drawer-function)
1698 name contents)))
1699 (org-latex--wrap-label drawer output info)))
1702 ;;;; Dynamic Block
1704 (defun org-latex-dynamic-block (dynamic-block contents info)
1705 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1706 CONTENTS holds the contents of the block. INFO is a plist
1707 holding contextual information. See `org-export-data'."
1708 (org-latex--wrap-label dynamic-block contents info))
1711 ;;;; Entity
1713 (defun org-latex-entity (entity contents info)
1714 "Transcode an ENTITY object from Org to LaTeX.
1715 CONTENTS are the definition itself. INFO is a plist holding
1716 contextual information."
1717 (org-element-property :latex entity))
1720 ;;;; Example Block
1722 (defun org-latex-example-block (example-block contents info)
1723 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1724 CONTENTS is nil. INFO is a plist holding contextual
1725 information."
1726 (when (org-string-nw-p (org-element-property :value example-block))
1727 (let ((environment (or (org-export-read-attribute
1728 :attr_latex example-block :environment)
1729 "verbatim")))
1730 (org-latex--wrap-label
1731 example-block
1732 (format "\\begin{%s}\n%s\\end{%s}"
1733 environment
1734 (org-export-format-code-default example-block info)
1735 environment)
1736 info))))
1739 ;;;; Export Block
1741 (defun org-latex-export-block (export-block contents info)
1742 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1743 CONTENTS is nil. INFO is a plist holding contextual information."
1744 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1745 (org-remove-indentation (org-element-property :value export-block))))
1748 ;;;; Export Snippet
1750 (defun org-latex-export-snippet (export-snippet contents info)
1751 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1752 CONTENTS is nil. INFO is a plist holding contextual information."
1753 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1754 (org-element-property :value export-snippet)))
1757 ;;;; Fixed Width
1759 (defun org-latex-fixed-width (fixed-width contents info)
1760 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1761 CONTENTS is nil. INFO is a plist holding contextual information."
1762 (org-latex--wrap-label
1763 fixed-width
1764 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1765 (org-remove-indentation
1766 (org-element-property :value fixed-width)))
1767 info))
1770 ;;;; Footnote Reference
1772 (defun org-latex-footnote-reference (footnote-reference contents info)
1773 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1774 CONTENTS is nil. INFO is a plist holding contextual information."
1775 (concat
1776 ;; Insert separator between two footnotes in a row.
1777 (let ((prev (org-export-get-previous-element footnote-reference info)))
1778 (when (eq (org-element-type prev) 'footnote-reference)
1779 (plist-get info :latex-footnote-separator)))
1780 (cond
1781 ;; Use \footnotemark if the footnote has already been defined.
1782 ((not (org-export-footnote-first-reference-p footnote-reference info))
1783 (format "\\footnotemark[%s]{}"
1784 (org-export-get-footnote-number footnote-reference info)))
1785 ;; Use \footnotemark if reference is within another footnote
1786 ;; reference, footnote definition or table cell.
1787 ((org-element-lineage footnote-reference
1788 '(footnote-reference footnote-definition table-cell))
1789 "\\footnotemark")
1790 ;; Otherwise, define it with \footnote command.
1792 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1793 (concat
1794 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1795 ;; Retrieve all footnote references within the footnote and
1796 ;; add their definition after it, since LaTeX doesn't support
1797 ;; them inside.
1798 (org-latex--delayed-footnotes-definitions def info)))))))
1801 ;;;; Headline
1803 (defun org-latex-headline (headline contents info)
1804 "Transcode a HEADLINE element from Org to LaTeX.
1805 CONTENTS holds the contents of the headline. INFO is a plist
1806 holding contextual information."
1807 (unless (org-element-property :footnote-section-p headline)
1808 (let* ((class (plist-get info :latex-class))
1809 (level (org-export-get-relative-level headline info))
1810 (numberedp (org-export-numbered-headline-p headline info))
1811 (class-sectioning (assoc class (plist-get info :latex-classes)))
1812 ;; Section formatting will set two placeholders: one for
1813 ;; the title and the other for the contents.
1814 (section-fmt
1815 (let ((sec (if (functionp (nth 2 class-sectioning))
1816 (funcall (nth 2 class-sectioning) level numberedp)
1817 (nth (1+ level) class-sectioning))))
1818 (cond
1819 ;; No section available for that LEVEL.
1820 ((not sec) nil)
1821 ;; Section format directly returned by a function. Add
1822 ;; placeholder for contents.
1823 ((stringp sec) (concat sec "\n%s"))
1824 ;; (numbered-section . unnumbered-section)
1825 ((not (consp (cdr sec)))
1826 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1827 ;; (numbered-open numbered-close)
1828 ((= (length sec) 2)
1829 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1830 ;; (num-in num-out no-num-in no-num-out)
1831 ((= (length sec) 4)
1832 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1833 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1834 ;; Create a temporary export back-end that hard-codes
1835 ;; "\underline" within "\section" and alike.
1836 (section-back-end
1837 (org-export-create-backend
1838 :parent 'latex
1839 :transcoders
1840 '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
1841 (text
1842 (org-export-data-with-backend
1843 (org-element-property :title headline) section-back-end info))
1844 (todo
1845 (and (plist-get info :with-todo-keywords)
1846 (let ((todo (org-element-property :todo-keyword headline)))
1847 (and todo (org-export-data todo info)))))
1848 (todo-type (and todo (org-element-property :todo-type headline)))
1849 (tags (and (plist-get info :with-tags)
1850 (org-export-get-tags headline info)))
1851 (priority (and (plist-get info :with-priority)
1852 (org-element-property :priority headline)))
1853 ;; Create the headline text along with a no-tag version.
1854 ;; The latter is required to remove tags from toc.
1855 (full-text (funcall (plist-get info :latex-format-headline-function)
1856 todo todo-type priority text tags info))
1857 ;; Associate \label to the headline for internal links.
1858 (headline-label (org-latex--label headline info t t))
1859 (pre-blanks
1860 (make-string (org-element-property :pre-blank headline) ?\n)))
1861 (if (or (not section-fmt) (org-export-low-level-p headline info))
1862 ;; This is a deep sub-tree: export it as a list item. Also
1863 ;; export as items headlines for which no section format has
1864 ;; been found.
1865 (let ((low-level-body
1866 (concat
1867 ;; If headline is the first sibling, start a list.
1868 (when (org-export-first-sibling-p headline info)
1869 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1870 ;; Itemize headline
1871 "\\item"
1872 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
1873 "\\relax")
1874 " " full-text "\n"
1875 headline-label
1876 pre-blanks
1877 contents)))
1878 ;; If headline is not the last sibling simply return
1879 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1880 ;; any blank line.
1881 (if (not (org-export-last-sibling-p headline info)) low-level-body
1882 (replace-regexp-in-string
1883 "[ \t\n]*\\'"
1884 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1885 low-level-body)))
1886 ;; This is a standard headline. Export it as a section. Add
1887 ;; an alternative heading when possible, and when this is not
1888 ;; identical to the usual heading.
1889 (let ((opt-title
1890 (funcall (plist-get info :latex-format-headline-function)
1891 todo todo-type priority
1892 (org-export-data-with-backend
1893 (org-export-get-alt-title headline info)
1894 section-back-end info)
1895 (and (eq (plist-get info :with-tags) t) tags)
1896 info))
1897 ;; Maybe end local TOC (see `org-latex-keyword').
1898 (contents
1899 (concat
1900 contents
1901 (let ((case-fold-search t)
1902 (section
1903 (let ((first (car (org-element-contents headline))))
1904 (and (eq (org-element-type first) 'section) first))))
1905 (org-element-map section 'keyword
1906 (lambda (k)
1907 (and (equal (org-element-property :key k) "TOC")
1908 (let ((v (org-element-property :value k)))
1909 (and (org-string-match-p "\\<headlines\\>" v)
1910 (org-string-match-p "\\<local\\>" v)
1911 (format "\\stopcontents[level-%d]" level)))))
1912 info t)))))
1913 (if (and opt-title
1914 (not (equal opt-title full-text))
1915 (string-match "\\`\\\\\\(.+?\\){" section-fmt))
1916 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1917 ;; Replace square brackets with parenthesis
1918 ;; since square brackets are not supported in
1919 ;; optional arguments.
1920 (replace-regexp-in-string
1921 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1922 full-text
1923 (concat headline-label pre-blanks contents))
1924 ;; Impossible to add an alternative heading. Fallback to
1925 ;; regular sectioning format string.
1926 (format section-fmt full-text
1927 (concat headline-label pre-blanks contents))))))))
1929 (defun org-latex-format-headline-default-function
1930 (todo todo-type priority text tags info)
1931 "Default format function for a headline.
1932 See `org-latex-format-headline-function' for details."
1933 (concat
1934 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1935 (and priority (format "\\framebox{\\#%c} " priority))
1936 text
1937 (and tags
1938 (format "\\hfill{}\\textsc{%s}"
1939 (mapconcat (lambda (tag) (org-latex-plain-text tag info))
1940 tags ":")))))
1943 ;;;; Horizontal Rule
1945 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1946 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1947 CONTENTS is nil. INFO is a plist holding contextual information."
1948 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1949 (prev (org-export-get-previous-element horizontal-rule info)))
1950 (concat
1951 ;; Make sure the rule doesn't start at the end of the current
1952 ;; line by separating it with a blank line from previous element.
1953 (when (and prev
1954 (let ((prev-blank (org-element-property :post-blank prev)))
1955 (or (not prev-blank) (zerop prev-blank))))
1956 "\n")
1957 (org-latex--wrap-label
1958 horizontal-rule
1959 (format "\\rule{%s}{%s}"
1960 (or (plist-get attr :width) "\\linewidth")
1961 (or (plist-get attr :thickness) "0.5pt"))
1962 info))))
1965 ;;;; Inline Src Block
1967 (defun org-latex-inline-src-block (inline-src-block contents info)
1968 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1969 CONTENTS holds the contents of the item. INFO is a plist holding
1970 contextual information."
1971 (let* ((code (org-element-property :value inline-src-block))
1972 (separator (org-latex--find-verb-separator code)))
1973 (case (plist-get info :latex-listings)
1974 ;; Do not use a special package: transcode it verbatim.
1975 ((nil) (format "\\texttt{%s}" (org-latex--protect-text code)))
1976 ;; Use minted package.
1977 (minted
1978 (let* ((org-lang (org-element-property :language inline-src-block))
1979 (mint-lang (or (cadr (assq (intern org-lang)
1980 (plist-get info :latex-minted-langs)))
1981 (downcase org-lang)))
1982 (options (org-latex--make-option-string
1983 (plist-get info :latex-minted-options))))
1984 (concat (format "\\mint%s{%s}"
1985 (if (string= options "") "" (format "[%s]" options))
1986 mint-lang)
1987 separator code separator)))
1988 ;; Use listings package.
1989 (otherwise
1990 ;; Maybe translate language's name.
1991 (let* ((org-lang (org-element-property :language inline-src-block))
1992 (lst-lang (or (cadr (assq (intern org-lang)
1993 (plist-get info :latex-listings-langs)))
1994 org-lang))
1995 (options (org-latex--make-option-string
1996 (append (plist-get info :latex-listings-options)
1997 `(("language" ,lst-lang))))))
1998 (concat (format "\\lstinline[%s]" options)
1999 separator code separator))))))
2002 ;;;; Inlinetask
2004 (defun org-latex-inlinetask (inlinetask contents info)
2005 "Transcode an INLINETASK element from Org to LaTeX.
2006 CONTENTS holds the contents of the block. INFO is a plist
2007 holding contextual information."
2008 (let ((title (org-export-data (org-element-property :title inlinetask) info))
2009 (todo (and (plist-get info :with-todo-keywords)
2010 (let ((todo (org-element-property :todo-keyword inlinetask)))
2011 (and todo (org-export-data todo info)))))
2012 (todo-type (org-element-property :todo-type inlinetask))
2013 (tags (and (plist-get info :with-tags)
2014 (org-export-get-tags inlinetask info)))
2015 (priority (and (plist-get info :with-priority)
2016 (org-element-property :priority inlinetask)))
2017 (contents (concat (org-latex--label inlinetask info) contents)))
2018 (funcall (plist-get info :latex-format-inlinetask-function)
2019 todo todo-type priority title tags contents info)))
2021 (defun org-latex-format-inlinetask-default-function
2022 (todo todo-type priority title tags contents info)
2023 "Default format function for a inlinetasks.
2024 See `org-latex-format-inlinetask-function' for details."
2025 (let ((full-title
2026 (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
2027 (when priority (format "\\framebox{\\#%c} " priority))
2028 title
2029 (when tags
2030 (format "\\hfill{}\\textsc{:%s:}"
2031 (mapconcat
2032 (lambda (tag) (org-latex-plain-text tag info))
2033 tags ":"))))))
2034 (concat "\\begin{center}\n"
2035 "\\fbox{\n"
2036 "\\begin{minipage}[c]{.6\\textwidth}\n"
2037 full-title "\n\n"
2038 (and (org-string-nw-p contents)
2039 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
2040 "\\end{minipage}\n"
2041 "}\n"
2042 "\\end{center}")))
2045 ;;;; Italic
2047 (defun org-latex-italic (italic contents info)
2048 "Transcode ITALIC from Org to LaTeX.
2049 CONTENTS is the text with italic markup. INFO is a plist holding
2050 contextual information."
2051 (org-latex--text-markup contents 'italic info))
2054 ;;;; Item
2056 (defun org-latex-item (item contents info)
2057 "Transcode an ITEM element from Org to LaTeX.
2058 CONTENTS holds the contents of the item. INFO is a plist holding
2059 contextual information."
2060 (let* ((counter
2061 (let ((count (org-element-property :counter item))
2062 (level
2063 ;; Determine level of current item to determine the
2064 ;; correct LaTeX counter to use (enumi, enumii...).
2065 (let ((parent item) (level 0))
2066 (while (memq (org-element-type
2067 (setq parent (org-export-get-parent parent)))
2068 '(plain-list item))
2069 (when (and (eq (org-element-type parent) 'plain-list)
2070 (eq (org-element-property :type parent)
2071 'ordered))
2072 (incf level)))
2073 level)))
2074 (and count
2075 (< level 5)
2076 (format "\\setcounter{enum%s}{%s}\n"
2077 (nth (1- level) '("i" "ii" "iii" "iv"))
2078 (1- count)))))
2079 (checkbox (case (org-element-property :checkbox item)
2080 (on "$\\boxtimes$ ")
2081 (off "$\\square$ ")
2082 (trans "$\\boxminus$ ")))
2083 (tag (let ((tag (org-element-property :tag item)))
2084 ;; Check-boxes must belong to the tag.
2085 (and tag (format "[{%s}] "
2086 (concat checkbox
2087 (org-export-data tag info)))))))
2088 (concat counter
2089 "\\item"
2090 (cond
2091 (tag)
2092 (checkbox (concat " " checkbox))
2093 ;; Without a tag or a check-box, if CONTENTS starts with
2094 ;; an opening square bracket, add "\relax" to "\item",
2095 ;; unless the brackets comes from an initial export
2096 ;; snippet (i.e. it is inserted willingly by the user).
2097 ((and contents
2098 (org-string-match-p "\\`[ \t]*\\[" contents)
2099 (not (let ((e (car (org-element-contents item))))
2100 (and (eq (org-element-type e) 'paragraph)
2101 (let ((o (car (org-element-contents e))))
2102 (and (eq (org-element-type o) 'export-snippet)
2103 (eq (org-export-snippet-backend o)
2104 'latex)))))))
2105 "\\relax ")
2106 (t " "))
2107 (and contents (org-trim contents))
2108 ;; If there are footnotes references in tag, be sure to
2109 ;; add their definition at the end of the item. This
2110 ;; workaround is necessary since "\footnote{}" command is
2111 ;; not supported in tags.
2112 (and tag
2113 (org-latex--delayed-footnotes-definitions
2114 (org-element-property :tag item) info)))))
2117 ;;;; Keyword
2119 (defun org-latex-keyword (keyword contents info)
2120 "Transcode a KEYWORD element from Org to LaTeX.
2121 CONTENTS is nil. INFO is a plist holding contextual information."
2122 (let ((key (org-element-property :key keyword))
2123 (value (org-element-property :value keyword)))
2124 (cond
2125 ((string= key "LATEX") value)
2126 ((string= key "INDEX") (format "\\index{%s}" value))
2127 ((string= key "TOC")
2128 (let ((case-fold-search t))
2129 (cond
2130 ((org-string-match-p "\\<headlines\\>" value)
2131 (let* ((localp (org-string-match-p "\\<local\\>" value))
2132 (parent (org-element-lineage keyword '(headline)))
2133 (level (if (not (and localp parent)) 0
2134 (org-export-get-relative-level parent info)))
2135 (depth
2136 (and (string-match "\\<[0-9]+\\>" value)
2137 (format
2138 "\\setcounter{tocdepth}{%d}"
2139 (+ (string-to-number (match-string 0 value)) level)))))
2140 (if (and localp parent)
2141 ;; Start local TOC, assuming package "titletoc" is
2142 ;; required.
2143 (format "\\startcontents[level-%d]
2144 \\printcontents[level-%d]{}{0}{%s}"
2145 level level (or depth ""))
2146 (concat depth (and depth "\n") "\\tableofcontents"))))
2147 ((org-string-match-p "\\<tables\\>" value) "\\listoftables")
2148 ((org-string-match-p "\\<listings\\>" value)
2149 (case (plist-get info :latex-listings)
2150 ((nil) "\\listoffigures")
2151 (minted "\\listoflistings")
2152 (otherwise "\\lstlistoflistings")))))))))
2155 ;;;; Latex Environment
2157 (defun org-latex-latex-environment (latex-environment contents info)
2158 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
2159 CONTENTS is nil. INFO is a plist holding contextual information."
2160 (when (plist-get info :with-latex)
2161 (let ((value (org-remove-indentation
2162 (org-element-property :value latex-environment))))
2163 (if (not (org-element-property :name latex-environment)) value
2164 ;; Environment is labeled: label must be within the environment
2165 ;; (otherwise, a reference pointing to that element will count
2166 ;; the section instead).
2167 (with-temp-buffer
2168 (insert value)
2169 (goto-char (point-min))
2170 (forward-line)
2171 (insert (org-latex--label latex-environment info nil t))
2172 (buffer-string))))))
2175 ;;;; Latex Fragment
2177 (defun org-latex-latex-fragment (latex-fragment contents info)
2178 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
2179 CONTENTS is nil. INFO is a plist holding contextual information."
2180 (let ((value (org-element-property :value latex-fragment)))
2181 ;; Trim math markers since the fragment is enclosed within
2182 ;; a latex-math-block object anyway.
2183 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
2184 (match-string 2 value))
2185 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
2186 (match-string 1 value))
2187 (t value))))
2190 ;;;; Line Break
2192 (defun org-latex-line-break (line-break contents info)
2193 "Transcode a LINE-BREAK object from Org to LaTeX.
2194 CONTENTS is nil. INFO is a plist holding contextual information."
2195 "\\\\\n")
2198 ;;;; Link
2200 (defun org-latex--inline-image (link info)
2201 "Return LaTeX code for an inline image.
2202 LINK is the link pointing to the inline image. INFO is a plist
2203 used as a communication channel."
2204 (let* ((parent (org-export-get-parent-element link))
2205 (path (let ((raw-path (org-element-property :path link)))
2206 (if (not (file-name-absolute-p raw-path)) raw-path
2207 (expand-file-name raw-path))))
2208 (filetype (file-name-extension path))
2209 (caption (org-latex--caption/label-string parent info))
2210 (caption-above-p (org-latex--caption-above-p link info))
2211 ;; Retrieve latex attributes from the element around.
2212 (attr (org-export-read-attribute :attr_latex parent))
2213 (float (let ((float (plist-get attr :float)))
2214 (cond ((string= float "wrap") 'wrap)
2215 ((string= float "sideways") 'sideways)
2216 ((string= float "multicolumn") 'multicolumn)
2217 ((or float
2218 (org-element-property :caption parent)
2219 (org-string-nw-p (plist-get attr :caption)))
2220 (if (and (plist-member attr :float) (not float))
2221 'nonfloat
2222 'figure))
2223 ((and (not float) (plist-member attr :float)) nil))))
2224 (placement
2225 (let ((place (plist-get attr :placement)))
2226 (cond
2227 (place (format "%s" place))
2228 ((eq float 'wrap) "{l}{0.5\\textwidth}")
2229 ((eq float 'figure)
2230 (format "[%s]" (plist-get info :latex-default-figure-position)))
2231 (t ""))))
2232 (comment-include (if (plist-get attr :comment-include) "%" ""))
2233 ;; It is possible to specify width and height in the
2234 ;; ATTR_LATEX line, and also via default variables.
2235 (width (cond ((plist-get attr :width))
2236 ((plist-get attr :height) "")
2237 ((eq float 'wrap) "0.48\\textwidth")
2238 (t (plist-get info :latex-image-default-width))))
2239 (height (cond ((plist-get attr :height))
2240 ((or (plist-get attr :width)
2241 (memq float '(figure wrap))) "")
2242 (t (plist-get info :latex-image-default-height))))
2243 (options (let ((opt (or (plist-get attr :options)
2244 (plist-get info :latex-image-default-option))))
2245 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
2246 (match-string 1 opt))))
2247 image-code)
2248 (if (member filetype '("tikz" "pgf"))
2249 ;; For tikz images:
2250 ;; - use \input to read in image file.
2251 ;; - if options are present, wrap in a tikzpicture environment.
2252 ;; - if width or height are present, use \resizebox to change
2253 ;; the image size.
2254 (progn
2255 (setq image-code (format "\\input{%s}" path))
2256 (when (org-string-nw-p options)
2257 (setq image-code
2258 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
2259 options
2260 image-code)))
2261 (when (or (org-string-nw-p width) (org-string-nw-p height))
2262 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
2263 (if (org-string-nw-p width) width "!")
2264 (if (org-string-nw-p height) height "!")
2265 image-code))))
2266 ;; For other images:
2267 ;; - add width and height to options.
2268 ;; - include the image with \includegraphics.
2269 (when (org-string-nw-p width)
2270 (setq options (concat options ",width=" width)))
2271 (when (org-string-nw-p height)
2272 (setq options (concat options ",height=" height)))
2273 (let ((search-option (org-element-property :search-option link)))
2274 (when (and search-option
2275 (equal filetype "pdf")
2276 (org-string-match-p "\\`[0-9]+\\'" search-option)
2277 (not (org-string-match-p "page=" options)))
2278 (setq options (concat options ",page=" search-option))))
2279 (setq image-code
2280 (format "\\includegraphics%s{%s}"
2281 (cond ((not (org-string-nw-p options)) "")
2282 ((= (aref options 0) ?,)
2283 (format "[%s]"(substring options 1)))
2284 (t (format "[%s]" options)))
2285 path))
2286 (when (equal filetype "svg")
2287 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
2288 "\\includesvg"
2289 image-code
2290 nil t))
2291 (setq image-code (replace-regexp-in-string "\\.svg}"
2293 image-code
2294 nil t))))
2295 ;; Return proper string, depending on FLOAT.
2296 (case float
2297 (wrap (format "\\begin{wrapfigure}%s
2298 %s\\centering
2299 %s%s
2300 %s\\end{wrapfigure}"
2301 placement
2302 (if caption-above-p caption "")
2303 comment-include image-code
2304 (if caption-above-p "" caption)))
2305 (sideways (format "\\begin{sidewaysfigure}
2306 %s\\centering
2307 %s%s
2308 %s\\end{sidewaysfigure}"
2309 (if caption-above-p caption "")
2310 comment-include image-code
2311 (if caption-above-p "" caption)))
2312 (multicolumn (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 (figure (format "\\begin{figure}%s
2321 %s\\centering
2322 %s%s
2323 %s\\end{figure}"
2324 placement
2325 (if caption-above-p caption "")
2326 comment-include image-code
2327 (if caption-above-p "" caption)))
2328 (nonfloat
2329 (format "\\begin{center}
2330 %s%s
2331 %s\\end{center}"
2332 (if caption-above-p caption "")
2333 image-code
2334 (if caption-above-p "" caption)))
2335 (otherwise image-code))))
2337 (defun org-latex-link (link desc info)
2338 "Transcode a LINK object from Org to LaTeX.
2340 DESC is the description part of the link, or the empty string.
2341 INFO is a plist holding contextual information. See
2342 `org-export-data'."
2343 (let* ((type (org-element-property :type link))
2344 (raw-path (replace-regexp-in-string
2345 "%" "\\%" (org-element-property :path link) nil t))
2346 ;; Ensure DESC really exists, or set it to nil.
2347 (desc (and (not (string= desc "")) desc))
2348 (imagep (org-export-inline-image-p
2349 link (plist-get info :latex-inline-image-rules)))
2350 (path (cond
2351 ((member type '("http" "https" "ftp" "mailto" "doi"))
2352 (concat type ":" raw-path))
2353 ((string= type "file") (org-export-file-uri raw-path))
2354 (t raw-path))))
2355 (cond
2356 ;; Link type is handled by a special function.
2357 ((org-export-custom-protocol-maybe link desc 'latex))
2358 ;; Image file.
2359 (imagep (org-latex--inline-image link info))
2360 ;; Radio link: Transcode target's contents and use them as link's
2361 ;; description.
2362 ((string= type "radio")
2363 (let ((destination (org-export-resolve-radio-link link info)))
2364 (if (not destination) desc
2365 (format "\\hyperref[%s]{%s}"
2366 (org-export-get-reference destination info)
2367 desc))))
2368 ;; Links pointing to a headline: Find destination and build
2369 ;; appropriate referencing command.
2370 ((member type '("custom-id" "fuzzy" "id"))
2371 (let ((destination (if (string= type "fuzzy")
2372 (org-export-resolve-fuzzy-link link info)
2373 (org-export-resolve-id-link link info))))
2374 (case (org-element-type destination)
2375 ;; Id link points to an external file.
2376 (plain-text
2377 (if desc (format "\\href{%s}{%s}" destination desc)
2378 (format "\\url{%s}" destination)))
2379 ;; Fuzzy link points nowhere.
2380 ((nil)
2381 (format (plist-get info :latex-link-with-unknown-path-format)
2382 (or desc
2383 (org-export-data
2384 (org-element-property :raw-link link) info))))
2385 ;; LINK points to a headline. If headlines are numbered
2386 ;; and the link has no description, display headline's
2387 ;; number. Otherwise, display description or headline's
2388 ;; title.
2389 (headline
2390 (let ((label (org-latex--label destination info t)))
2391 (if (and (not desc)
2392 (org-export-numbered-headline-p destination info))
2393 (format "\\ref{%s}" label)
2394 (format "\\hyperref[%s]{%s}" label
2395 (or desc
2396 (org-export-data
2397 (org-element-property :title destination) info))))))
2398 ;; Fuzzy link points to a target. Do as above.
2399 (otherwise
2400 (let ((ref (org-latex--label destination info t)))
2401 (if (not desc) (format "\\ref{%s}" ref)
2402 (format "\\hyperref[%s]{%s}" ref desc)))))))
2403 ;; Coderef: replace link with the reference name or the
2404 ;; equivalent line number.
2405 ((string= type "coderef")
2406 (format (org-export-get-coderef-format path desc)
2407 (org-export-resolve-coderef path info)))
2408 ;; External link with a description part.
2409 ((and path desc) (format "\\href{%s}{%s}" path desc))
2410 ;; External link without a description part.
2411 (path (format "\\url{%s}" path))
2412 ;; No path, only description. Try to do something useful.
2413 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
2416 ;;;; Node Property
2418 (defun org-latex-node-property (node-property contents info)
2419 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2420 CONTENTS is nil. INFO is a plist holding contextual
2421 information."
2422 (format "%s:%s"
2423 (org-element-property :key node-property)
2424 (let ((value (org-element-property :value node-property)))
2425 (if value (concat " " value) ""))))
2428 ;;;; Paragraph
2430 (defun org-latex-paragraph (paragraph contents info)
2431 "Transcode a PARAGRAPH element from Org to LaTeX.
2432 CONTENTS is the contents of the paragraph, as a string. INFO is
2433 the plist used as a communication channel."
2434 contents)
2437 ;;;; Plain List
2439 (defun org-latex-plain-list (plain-list contents info)
2440 "Transcode a PLAIN-LIST element from Org to LaTeX.
2441 CONTENTS is the contents of the list. INFO is a plist holding
2442 contextual information."
2443 (let* ((type (org-element-property :type plain-list))
2444 (attr (org-export-read-attribute :attr_latex plain-list))
2445 (latex-type (let ((env (plist-get attr :environment)))
2446 (cond (env (format "%s" env))
2447 ((eq type 'ordered) "enumerate")
2448 ((eq type 'descriptive) "description")
2449 (t "itemize")))))
2450 (org-latex--wrap-label
2451 plain-list
2452 (format "\\begin{%s}%s\n%s\\end{%s}"
2453 latex-type
2454 (or (plist-get attr :options) "")
2455 contents
2456 latex-type)
2457 info)))
2460 ;;;; Plain Text
2462 (defun org-latex-plain-text (text info)
2463 "Transcode a TEXT string from Org to LaTeX.
2464 TEXT is the string to transcode. INFO is a plist holding
2465 contextual information."
2466 (let* ((specialp (plist-get info :with-special-strings))
2467 (output
2468 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2469 (let ((case-fold-search nil))
2470 (replace-regexp-in-string
2471 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2472 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2473 ;; However, if special strings are used, be careful not
2474 ;; to protect "\" in "\-" constructs.
2475 (replace-regexp-in-string
2476 (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\([^-]\\|$\\)"))
2477 (lambda (m)
2478 (case (string-to-char m)
2479 (?\\ "$\\\\backslash$\\1")
2480 (?~ "\\\\textasciitilde{}")
2481 (?^ "\\\\^{}")
2482 (t "\\\\\\&")))
2483 text)))))
2484 ;; Activate smart quotes. Be sure to provide original TEXT string
2485 ;; since OUTPUT may have been modified.
2486 (when (plist-get info :with-smart-quotes)
2487 (setq output (org-export-activate-smart-quotes output :latex info text)))
2488 ;; Convert special strings.
2489 (when specialp
2490 (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
2491 ;; Handle break preservation if required.
2492 (when (plist-get info :preserve-breaks)
2493 (setq output (replace-regexp-in-string
2494 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
2495 ;; Return value.
2496 output))
2499 ;;;; Planning
2501 (defun org-latex-planning (planning contents info)
2502 "Transcode a PLANNING element from Org to LaTeX.
2503 CONTENTS is nil. INFO is a plist holding contextual
2504 information."
2505 (concat
2506 "\\noindent"
2507 (mapconcat
2508 'identity
2509 (delq nil
2510 (list
2511 (let ((closed (org-element-property :closed planning)))
2512 (when closed
2513 (concat
2514 (format "\\textbf{%s} " org-closed-string)
2515 (format (plist-get info :latex-inactive-timestamp-format)
2516 (org-timestamp-translate closed)))))
2517 (let ((deadline (org-element-property :deadline planning)))
2518 (when deadline
2519 (concat
2520 (format "\\textbf{%s} " org-deadline-string)
2521 (format (plist-get info :latex-active-timestamp-format)
2522 (org-timestamp-translate deadline)))))
2523 (let ((scheduled (org-element-property :scheduled planning)))
2524 (when scheduled
2525 (concat
2526 (format "\\textbf{%s} " org-scheduled-string)
2527 (format (plist-get info :latex-active-timestamp-format)
2528 (org-timestamp-translate scheduled)))))))
2529 " ")
2530 "\\\\"))
2533 ;;;; Property Drawer
2535 (defun org-latex-property-drawer (property-drawer contents info)
2536 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2537 CONTENTS holds the contents of the drawer. INFO is a plist
2538 holding contextual information."
2539 (and (org-string-nw-p contents)
2540 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2543 ;;;; Pseudo Element: LaTeX Matrices
2545 ;; `latex-matrices' elements have the following properties:
2546 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2547 ;; `math').
2549 (defun org-latex--wrap-latex-matrices (data info)
2550 "Merge contiguous tables with the same mode within a pseudo-element.
2551 DATA is a parse tree or a secondary string. INFO is a plist
2552 containing export options. Modify DATA by side-effect and return
2553 it."
2554 (org-element-map data 'table
2555 (lambda (table)
2556 (when (eq (org-element-property :type table) 'org)
2557 (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
2558 (plist-get info :latex-default-table-mode))))
2559 (when (and (member mode '("inline-math" "math"))
2560 ;; Do not wrap twice the same table.
2561 (not (eq (org-element-type
2562 (org-element-property :parent table))
2563 'latex-matrices)))
2564 (let* ((caption (and (not (string= mode "inline-math"))
2565 (org-element-property :caption table)))
2566 (matrices
2567 (list 'latex-matrices
2568 (list :caption caption
2569 :markup
2570 (cond ((string= mode "inline-math") 'inline)
2571 (caption 'equation)
2572 (t 'math)))))
2573 (previous table)
2574 (next (org-export-get-next-element table info)))
2575 (org-element-insert-before matrices table)
2576 ;; Swallow all contiguous tables sharing the same mode.
2577 (while (and
2578 (zerop (or (org-element-property :post-blank previous) 0))
2579 (setq next (org-export-get-next-element previous info))
2580 (eq (org-element-type next) 'table)
2581 (eq (org-element-property :type next) 'org)
2582 (string= (or (org-export-read-attribute
2583 :attr_latex next :mode)
2584 (plist-get info :latex-default-table-mode))
2585 mode))
2586 (org-element-extract-element previous)
2587 (org-element-adopt-elements matrices previous)
2588 (setq previous next))
2589 (org-element-put-property
2590 matrices :post-blank (org-element-property :post-blank previous))
2591 (org-element-extract-element previous)
2592 (org-element-adopt-elements matrices previous))))))
2593 info)
2594 data)
2596 (defun org-latex-matrices (matrices contents info)
2597 "Transcode a MATRICES element from Org to LaTeX.
2598 CONTENTS is a string. INFO is a plist used as a communication
2599 channel."
2600 (format (case (org-element-property :markup matrices)
2601 (inline "\\(%s\\)")
2602 (equation "\\begin{equation}\n%s\\end{equation}")
2603 (t "\\[\n%s\\]"))
2604 contents))
2606 (defun org-latex-matrices-tree-filter (tree backend info)
2607 (org-latex--wrap-latex-matrices tree info))
2609 ;;;; Pseudo Object: LaTeX Math Block
2611 ;; `latex-math-block' objects have the following property:
2612 ;; `:post-blank'.
2614 (defun org-latex--wrap-latex-math-block (data info)
2615 "Merge contiguous math objects in a pseudo-object container.
2616 DATA is a parse tree or a secondary string. INFO is a plist
2617 containing export options. Modify DATA by side-effect and return it."
2618 (let ((valid-object-p
2619 (function
2620 ;; Non-nil when OBJ can be added to the latex math block.
2621 (lambda (obj)
2622 (case (org-element-type obj)
2623 (entity (org-element-property :latex-math-p obj))
2624 (latex-fragment
2625 (let ((value (org-element-property :value obj)))
2626 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2627 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2628 ((subscript superscript) t))))))
2629 (org-element-map data '(entity latex-fragment subscript superscript)
2630 (lambda (object)
2631 ;; Skip objects already wrapped.
2632 (when (and (not (eq (org-element-type
2633 (org-element-property :parent object))
2634 'latex-math-block))
2635 (funcall valid-object-p object))
2636 (let ((math-block (list 'latex-math-block nil))
2637 (next-elements (org-export-get-next-element object info t))
2638 (last object))
2639 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2640 (org-element-insert-before math-block object)
2641 (org-element-extract-element object)
2642 (org-element-adopt-elements math-block object)
2643 (when (zerop (or (org-element-property :post-blank object) 0))
2644 ;; MATH-BLOCK swallows consecutive math objects.
2645 (catch 'exit
2646 (dolist (next next-elements)
2647 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2648 (org-element-extract-element next)
2649 (org-element-adopt-elements math-block next)
2650 ;; Eschew the case: \beta$x$ -> \(\betax\).
2651 (unless (memq (org-element-type next)
2652 '(subscript superscript))
2653 (org-element-put-property last :post-blank 1))
2654 (setq last next)
2655 (when (> (or (org-element-property :post-blank next) 0) 0)
2656 (throw 'exit nil))))))
2657 (org-element-put-property
2658 math-block :post-blank (org-element-property :post-blank last)))))
2659 info nil '(subscript superscript latex-math-block) t)
2660 ;; Return updated DATA.
2661 data))
2663 (defun org-latex-math-block-tree-filter (tree backend info)
2664 (org-latex--wrap-latex-math-block tree info))
2666 (defun org-latex-math-block-options-filter (info backend)
2667 (dolist (prop '(:author :date :title) info)
2668 (plist-put info prop
2669 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2671 (defun org-latex-math-block (math-block contents info)
2672 "Transcode a MATH-BLOCK object from Org to LaTeX.
2673 CONTENTS is a string. INFO is a plist used as a communication
2674 channel."
2675 (when (org-string-nw-p contents)
2676 (format "\\(%s\\)" (org-trim contents))))
2678 ;;;; Quote Block
2680 (defun org-latex-quote-block (quote-block contents info)
2681 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2682 CONTENTS holds the contents of the block. INFO is a plist
2683 holding contextual information."
2684 (org-latex--wrap-label
2685 quote-block (format "\\begin{quote}\n%s\\end{quote}" contents) info))
2688 ;;;; Radio Target
2690 (defun org-latex-radio-target (radio-target text info)
2691 "Transcode a RADIO-TARGET object from Org to LaTeX.
2692 TEXT is the text of the target. INFO is a plist holding
2693 contextual information."
2694 (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
2697 ;;;; Section
2699 (defun org-latex-section (section contents info)
2700 "Transcode a SECTION element from Org to LaTeX.
2701 CONTENTS holds the contents of the section. INFO is a plist
2702 holding contextual information."
2703 contents)
2706 ;;;; Special Block
2708 (defun org-latex-special-block (special-block contents info)
2709 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2710 CONTENTS holds the contents of the block. INFO is a plist
2711 holding contextual information."
2712 (let ((type (org-element-property :type special-block))
2713 (opt (org-export-read-attribute :attr_latex special-block :options))
2714 (caption (org-latex--caption/label-string special-block info))
2715 (caption-above-p (org-latex--caption-above-p special-block info)))
2716 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2717 (and caption-above-p caption)
2718 contents
2719 (and (not caption-above-p) caption)
2720 (format "\\end{%s}" type))))
2723 ;;;; Src Block
2725 (defun org-latex-src-block (src-block contents info)
2726 "Transcode a SRC-BLOCK element from Org to LaTeX.
2727 CONTENTS holds the contents of the item. INFO is a plist holding
2728 contextual information."
2729 (when (org-string-nw-p (org-element-property :value src-block))
2730 (let* ((lang (org-element-property :language src-block))
2731 (caption (org-element-property :caption src-block))
2732 (caption-above-p (org-latex--caption-above-p src-block info))
2733 (label (org-element-property :name src-block))
2734 (custom-env (and lang
2735 (cadr (assq (intern lang)
2736 org-latex-custom-lang-environments))))
2737 (num-start (case (org-element-property :number-lines src-block)
2738 (continued (org-export-get-loc src-block info))
2739 (new 0)))
2740 (retain-labels (org-element-property :retain-labels src-block))
2741 (attributes (org-export-read-attribute :attr_latex src-block))
2742 (float (plist-get attributes :float))
2743 (listings (plist-get info :latex-listings)))
2744 (cond
2745 ;; Case 1. No source fontification.
2746 ((not listings)
2747 (let* ((caption-str (org-latex--caption/label-string src-block info))
2748 (float-env
2749 (cond ((string= "multicolumn" float)
2750 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2751 (plist-get info :latex-default-figure-position)
2752 (if caption-above-p caption-str "")
2753 (if caption-above-p "" caption-str)))
2754 (caption (concat
2755 (if caption-above-p caption-str "")
2756 "%s"
2757 (if caption-above-p "" (concat "\n" caption-str))))
2758 (t "%s"))))
2759 (format
2760 float-env
2761 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2762 (org-export-format-code-default src-block info))))))
2763 ;; Case 2. Custom environment.
2764 (custom-env
2765 (let ((caption-str (org-latex--caption/label-string src-block info)))
2766 (format "\\begin{%s}\n%s\\end{%s}\n"
2767 custom-env
2768 (concat (and caption-above-p caption-str)
2769 (org-export-format-code-default src-block info)
2770 (and (not caption-above-p) caption-str))
2771 custom-env)))
2772 ;; Case 3. Use minted package.
2773 ((eq listings 'minted)
2774 (let* ((caption-str (org-latex--caption/label-string src-block info))
2775 (float-env
2776 (cond
2777 ((string= "multicolumn" float)
2778 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2779 (if caption-above-p caption-str "")
2780 (if caption-above-p "" caption-str)))
2781 (caption
2782 (concat (if caption-above-p caption-str "")
2783 "%s"
2784 (if caption-above-p "" (concat "\n" caption-str))))
2785 (t "%s")))
2786 (options (plist-get info :latex-minted-options))
2787 (body
2788 (format
2789 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2790 ;; Options.
2791 (concat
2792 (org-latex--make-option-string
2793 (if (or (not num-start) (assoc "linenos" options))
2794 options
2795 (append
2796 `(("linenos")
2797 ("firstnumber" ,(number-to-string (1+ num-start))))
2798 options)))
2799 (let ((local-options (plist-get attributes :options)))
2800 (and local-options (concat "," local-options))))
2801 ;; Language.
2802 (or (cadr (assq (intern lang)
2803 (plist-get info :latex-minted-langs)))
2804 (downcase lang))
2805 ;; Source code.
2806 (let* ((code-info (org-export-unravel-code src-block))
2807 (max-width
2808 (apply 'max
2809 (mapcar 'length
2810 (org-split-string (car code-info)
2811 "\n")))))
2812 (org-export-format-code
2813 (car code-info)
2814 (lambda (loc num ref)
2815 (concat
2817 (when ref
2818 ;; Ensure references are flushed to the right,
2819 ;; separated with 6 spaces from the widest line
2820 ;; of code.
2821 (concat (make-string (+ (- max-width (length loc)) 6)
2822 ?\s)
2823 (format "(%s)" ref)))))
2824 nil (and retain-labels (cdr code-info)))))))
2825 ;; Return value.
2826 (format float-env body)))
2827 ;; Case 4. Use listings package.
2829 (let ((lst-lang
2830 (or (cadr (assq (intern lang)
2831 (plist-get info :latex-listings-langs)))
2832 lang))
2833 (caption-str
2834 (when caption
2835 (let ((main (org-export-get-caption src-block))
2836 (secondary (org-export-get-caption src-block t)))
2837 (if (not secondary)
2838 (format "{%s}" (org-export-data main info))
2839 (format "{[%s]%s}"
2840 (org-export-data secondary info)
2841 (org-export-data main info))))))
2842 (lst-opt (plist-get info :latex-listings-options)))
2843 (concat
2844 ;; Options.
2845 (format
2846 "\\lstset{%s}\n"
2847 (concat
2848 (org-latex--make-option-string
2849 (append
2850 lst-opt
2851 (cond
2852 ((and (not float) (plist-member attributes :float)) nil)
2853 ((string= "multicolumn" float) '(("float" "*")))
2854 ((and float (not (assoc "float" lst-opt)))
2855 `(("float" ,(plist-get info :latex-default-figure-position)))))
2856 `(("language" ,lst-lang))
2857 (if label `(("label" ,label)) '(("label" " ")))
2858 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2859 `(("captionpos" ,(if caption-above-p "t" "b")))
2860 (cond ((assoc "numbers" lst-opt) nil)
2861 ((not num-start) '(("numbers" "none")))
2862 ((zerop num-start) '(("numbers" "left")))
2863 (t `(("firstnumber" ,(number-to-string (1+ num-start)))
2864 ("numbers" "left"))))))
2865 (let ((local-options (plist-get attributes :options)))
2866 (and local-options (concat "," local-options)))))
2867 ;; Source code.
2868 (format
2869 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2870 (let* ((code-info (org-export-unravel-code src-block))
2871 (max-width
2872 (apply 'max
2873 (mapcar 'length
2874 (org-split-string (car code-info) "\n")))))
2875 (org-export-format-code
2876 (car code-info)
2877 (lambda (loc num ref)
2878 (concat
2880 (when ref
2881 ;; Ensure references are flushed to the right,
2882 ;; separated with 6 spaces from the widest line of
2883 ;; code
2884 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2885 (format "(%s)" ref)))))
2886 nil (and retain-labels (cdr code-info))))))))))))
2889 ;;;; Statistics Cookie
2891 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2892 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2893 CONTENTS is nil. INFO is a plist holding contextual information."
2894 (replace-regexp-in-string
2895 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2898 ;;;; Strike-Through
2900 (defun org-latex-strike-through (strike-through contents info)
2901 "Transcode STRIKE-THROUGH from Org to LaTeX.
2902 CONTENTS is the text with strike-through markup. INFO is a plist
2903 holding contextual information."
2904 (org-latex--text-markup contents 'strike-through info))
2907 ;;;; Subscript
2909 (defun org-latex--script-size (object info)
2910 "Transcode a subscript or superscript object.
2911 OBJECT is an Org object. INFO is a plist used as a communication
2912 channel."
2913 (let ((type (org-element-type object))
2914 (output ""))
2915 (org-element-map (org-element-contents object)
2916 (cons 'plain-text org-element-all-objects)
2917 (lambda (obj)
2918 (case (org-element-type obj)
2919 ((entity latex-fragment)
2920 (let ((data (org-trim (org-export-data obj info))))
2921 (string-match
2922 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2923 data)
2924 (setq output
2925 (concat output
2926 (match-string 1 data)
2927 (let ((blank (org-element-property :post-blank obj)))
2928 (and blank (> blank 0) "\\ "))))))
2929 (plain-text
2930 (setq output
2931 (format "%s\\text{%s}" output (org-export-data obj info))))
2932 (otherwise
2933 (setq output
2934 (concat output
2935 (org-export-data obj info)
2936 (let ((blank (org-element-property :post-blank obj)))
2937 (and blank (> blank 0) "\\ ")))))))
2938 info nil org-element-recursive-objects)
2939 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2940 ;; character.
2941 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2942 (and (> (length output) 1) "{")
2943 output
2944 (and (> (length output) 1) "}"))))
2946 (defun org-latex-subscript (subscript contents info)
2947 "Transcode a SUBSCRIPT object from Org to LaTeX.
2948 CONTENTS is the contents of the object. INFO is a plist holding
2949 contextual information."
2950 (org-latex--script-size subscript info))
2953 ;;;; Superscript
2955 (defun org-latex-superscript (superscript contents info)
2956 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2957 CONTENTS is the contents of the object. INFO is a plist holding
2958 contextual information."
2959 (org-latex--script-size superscript info))
2962 ;;;; Table
2964 ;; `org-latex-table' is the entry point for table transcoding. It
2965 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2966 ;; delegates the job to either `org-latex--table.el-table',
2967 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2968 ;; depending of the type of the table and the mode requested.
2970 ;; `org-latex--align-string' is a subroutine used to build alignment
2971 ;; string for Org tables.
2973 (defun org-latex-table (table contents info)
2974 "Transcode a TABLE element from Org to LaTeX.
2975 CONTENTS is the contents of the table. INFO is a plist holding
2976 contextual information."
2977 (if (eq (org-element-property :type table) 'table.el)
2978 ;; "table.el" table. Convert it using appropriate tools.
2979 (org-latex--table.el-table table info)
2980 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2981 (plist-get info :latex-default-table-mode))))
2982 (cond
2983 ;; Case 1: Verbatim table.
2984 ((string= type "verbatim")
2985 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2986 ;; Re-create table, without affiliated keywords.
2987 (org-trim (org-element-interpret-data
2988 `(table nil ,@(org-element-contents table))))))
2989 ;; Case 2: Matrix.
2990 ((or (string= type "math") (string= type "inline-math"))
2991 (org-latex--math-table table info))
2992 ;; Case 3: Standard table.
2993 (t (concat (org-latex--org-table table contents info)
2994 ;; When there are footnote references within the
2995 ;; table, insert their definition just after it.
2996 (org-latex--delayed-footnotes-definitions table info)))))))
2998 (defun org-latex--align-string (table info)
2999 "Return an appropriate LaTeX alignment string.
3000 TABLE is the considered table. INFO is a plist used as
3001 a communication channel."
3002 (or (org-export-read-attribute :attr_latex table :align)
3003 (let (align)
3004 ;; Extract column groups and alignment from first (non-rule)
3005 ;; row.
3006 (org-element-map
3007 (org-element-map table 'table-row
3008 (lambda (row)
3009 (and (eq (org-element-property :type row) 'standard) row))
3010 info 'first-match)
3011 'table-cell
3012 (lambda (cell)
3013 (let ((borders (org-export-table-cell-borders cell info)))
3014 ;; Check left border for the first cell only.
3015 (when (and (memq 'left borders) (not align))
3016 (push "|" align))
3017 (push (case (org-export-table-cell-alignment cell info)
3018 (left "l")
3019 (right "r")
3020 (center "c"))
3021 align)
3022 (when (memq 'right borders) (push "|" align))))
3023 info)
3024 (apply 'concat (nreverse align)))))
3026 (defun org-latex--org-table (table contents info)
3027 "Return appropriate LaTeX code for an Org table.
3029 TABLE is the table type element to transcode. CONTENTS is its
3030 contents, as a string. INFO is a plist used as a communication
3031 channel.
3033 This function assumes TABLE has `org' as its `:type' property and
3034 `table' as its `:mode' attribute."
3035 (let* ((caption (org-latex--caption/label-string table info))
3036 (attr (org-export-read-attribute :attr_latex table))
3037 ;; Determine alignment string.
3038 (alignment (org-latex--align-string table info))
3039 ;; Determine environment for the table: longtable, tabular...
3040 (table-env (or (plist-get attr :environment)
3041 (plist-get info :latex-default-table-environment)))
3042 ;; If table is a float, determine environment: table, table*
3043 ;; or sidewaystable.
3044 (float-env (unless (member table-env '("longtable" "longtabu"))
3045 (let ((float (plist-get attr :float)))
3046 (cond
3047 ((and (not float) (plist-member attr :float)) nil)
3048 ((or (string= float "sidewaystable")
3049 (string= float "sideways")) "sidewaystable")
3050 ((string= float "multicolumn") "table*")
3051 ((or float
3052 (org-element-property :caption table)
3053 (org-string-nw-p (plist-get attr :caption)))
3054 "table")))))
3055 ;; Extract others display options.
3056 (fontsize (let ((font (plist-get attr :font)))
3057 (and font (concat font "\n"))))
3058 ;; "tabular" environment doesn't allow to define a width.
3059 (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
3060 (spreadp (plist-get attr :spread))
3061 (placement
3062 (or (plist-get attr :placement)
3063 (format "[%s]" (plist-get info :latex-default-figure-position))))
3064 (centerp (if (plist-member attr :center) (plist-get attr :center)
3065 (plist-get info :latex-tables-centered)))
3066 (caption-above-p (org-latex--caption-above-p table info)))
3067 ;; Prepare the final format string for the table.
3068 (cond
3069 ;; Longtable.
3070 ((equal "longtable" table-env)
3071 (concat (and fontsize (concat "{" fontsize))
3072 (format "\\begin{longtable}{%s}\n" alignment)
3073 (and caption-above-p
3074 (org-string-nw-p caption)
3075 (concat caption "\\\\\n"))
3076 contents
3077 (and (not caption-above-p)
3078 (org-string-nw-p caption)
3079 (concat caption "\\\\\n"))
3080 "\\end{longtable}\n"
3081 (and fontsize "}")))
3082 ;; Longtabu
3083 ((equal "longtabu" table-env)
3084 (concat (and fontsize (concat "{" fontsize))
3085 (format "\\begin{longtabu}%s{%s}\n"
3086 (if width
3087 (format " %s %s "
3088 (if spreadp "spread" "to") width) "")
3089 alignment)
3090 (and caption-above-p
3091 (org-string-nw-p caption)
3092 (concat caption "\\\\\n"))
3093 contents
3094 (and (not caption-above-p)
3095 (org-string-nw-p caption)
3096 (concat caption "\\\\\n"))
3097 "\\end{longtabu}\n"
3098 (and fontsize "}")))
3099 ;; Others.
3100 (t (concat (cond
3101 (float-env
3102 (concat (format "\\begin{%s}%s\n" float-env placement)
3103 (if caption-above-p caption "")
3104 (when centerp "\\centering\n")
3105 fontsize))
3106 ((and (not float-env) caption)
3107 (concat
3108 (and centerp "\\begin{center}\n" )
3109 (if caption-above-p caption "")
3110 (cond ((and fontsize centerp) fontsize)
3111 (fontsize (concat "{" fontsize)))))
3112 (centerp (concat "\\begin{center}\n" fontsize))
3113 (fontsize (concat "{" fontsize)))
3114 (cond ((equal "tabu" table-env)
3115 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
3116 (if width (format
3117 (if spreadp " spread %s " " to %s ")
3118 width) "")
3119 alignment
3120 contents))
3121 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
3122 table-env
3123 (if width (format "{%s}" width) "")
3124 alignment
3125 contents
3126 table-env)))
3127 (cond
3128 (float-env
3129 (concat (if caption-above-p "" (concat "\n" caption))
3130 (format "\n\\end{%s}" float-env)))
3131 ((and (not float-env) caption)
3132 (concat
3133 (if caption-above-p "" (concat "\n" caption))
3134 (and centerp "\n\\end{center}")
3135 (and fontsize (not centerp) "}")))
3136 (centerp "\n\\end{center}")
3137 (fontsize "}")))))))
3139 (defun org-latex--table.el-table (table info)
3140 "Return appropriate LaTeX code for a table.el table.
3142 TABLE is the table type element to transcode. INFO is a plist
3143 used as a communication channel.
3145 This function assumes TABLE has `table.el' as its `:type'
3146 property."
3147 (require 'table)
3148 ;; Ensure "*org-export-table*" buffer is empty.
3149 (with-current-buffer (get-buffer-create "*org-export-table*")
3150 (erase-buffer))
3151 (let ((output (with-temp-buffer
3152 (insert (org-element-property :value table))
3153 (goto-char 1)
3154 (re-search-forward "^[ \t]*|[^|]" nil t)
3155 (table-generate-source 'latex "*org-export-table*")
3156 (with-current-buffer "*org-export-table*"
3157 (org-trim (buffer-string))))))
3158 (kill-buffer (get-buffer "*org-export-table*"))
3159 ;; Remove left out comments.
3160 (while (string-match "^%.*\n" output)
3161 (setq output (replace-match "" t t output)))
3162 (let ((attr (org-export-read-attribute :attr_latex table)))
3163 (when (plist-get attr :rmlines)
3164 ;; When the "rmlines" attribute is provided, remove all hlines
3165 ;; but the the one separating heading from the table body.
3166 (let ((n 0) (pos 0))
3167 (while (and (< (length output) pos)
3168 (setq pos (string-match "^\\\\hline\n?" output pos)))
3169 (incf n)
3170 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
3171 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
3172 (plist-get info :latex-tables-centered))))
3173 (if (not centerp) output
3174 (format "\\begin{center}\n%s\n\\end{center}" output))))))
3176 (defun org-latex--math-table (table info)
3177 "Return appropriate LaTeX code for a matrix.
3179 TABLE is the table type element to transcode. INFO is a plist
3180 used as a communication channel.
3182 This function assumes TABLE has `org' as its `:type' property and
3183 `inline-math' or `math' as its `:mode' attribute."
3184 (let* ((attr (org-export-read-attribute :attr_latex table))
3185 (env (or (plist-get attr :environment)
3186 (plist-get info :latex-default-table-environment)))
3187 (contents
3188 (mapconcat
3189 (lambda (row)
3190 ;; Ignore horizontal rules.
3191 (when (eq (org-element-property :type row) 'standard)
3192 ;; Return each cell unmodified.
3193 (concat
3194 (mapconcat
3195 (lambda (cell)
3196 (substring (org-element-interpret-data cell) 0 -1))
3197 (org-element-map row 'table-cell #'identity info) "&")
3198 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
3199 "\n")))
3200 (org-element-map table 'table-row #'identity info) "")))
3201 (concat
3202 ;; Prefix.
3203 (plist-get attr :math-prefix)
3204 ;; Environment. Also treat special cases.
3205 (cond ((member env '("array" "tabular"))
3206 (let ((align (make-string
3207 (cdr (org-export-table-dimensions table info)) ?c)))
3208 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
3209 ((assoc env org-latex-table-matrix-macros)
3210 (format "\\%s%s{\n%s}"
3212 (or (plist-get attr :math-arguments) "")
3213 contents))
3214 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
3215 ;; Suffix.
3216 (plist-get attr :math-suffix))))
3219 ;;;; Table Cell
3221 (defun org-latex-table-cell (table-cell contents info)
3222 "Transcode a TABLE-CELL element from Org to LaTeX.
3223 CONTENTS is the cell contents. INFO is a plist used as
3224 a communication channel."
3225 (concat
3226 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
3227 (if (and contents
3228 scientific-format
3229 (string-match orgtbl-exp-regexp contents))
3230 ;; Use appropriate format string for scientific
3231 ;; notation.
3232 (format scientific-format
3233 (match-string 1 contents)
3234 (match-string 2 contents))
3235 contents))
3236 (when (org-export-get-next-element table-cell info) " & ")))
3239 ;;;; Table Row
3241 (defun org-latex-table-row (table-row contents info)
3242 "Transcode a TABLE-ROW element from Org to LaTeX.
3243 CONTENTS is the contents of the row. INFO is a plist used as
3244 a communication channel."
3245 (let* ((attr (org-export-read-attribute :attr_latex
3246 (org-export-get-parent table-row)))
3247 (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
3248 (plist-get info :latex-tables-booktabs)))
3249 (longtablep
3250 (member (or (plist-get attr :environment)
3251 (plist-get info :latex-default-table-environment))
3252 '("longtable" "longtabu"))))
3253 (if (eq (org-element-property :type table-row) 'rule)
3254 (cond
3255 ((not booktabsp) "\\hline")
3256 ((not (org-export-get-previous-element table-row info)) "\\toprule")
3257 ((not (org-export-get-next-element table-row info)) "\\bottomrule")
3258 ((and longtablep
3259 (org-export-table-row-ends-header-p
3260 (org-export-get-previous-element table-row info) info))
3262 (t "\\midrule"))
3263 (concat
3264 ;; When BOOKTABS are activated enforce top-rule even when no
3265 ;; hline was specifically marked.
3266 (and booktabsp (not (org-export-get-previous-element table-row info))
3267 "\\toprule\n")
3268 contents "\\\\\n"
3269 (cond
3270 ;; Special case for long tables. Define header and footers.
3271 ((and longtablep (org-export-table-row-ends-header-p table-row info))
3272 (let ((columns (cdr (org-export-table-dimensions
3273 (org-export-get-parent-table table-row) info))))
3274 (format "%s
3275 \\endfirsthead
3276 \\multicolumn{%d}{l}{%s} \\\\
3278 %s \\\\\n
3280 \\endhead
3281 %s\\multicolumn{%d}{r}{%s} \\\\
3282 \\endfoot
3283 \\endlastfoot"
3284 (if booktabsp "\\midrule" "\\hline")
3285 columns
3286 (org-latex--translate "Continued from previous page" info)
3287 (cond
3288 ((not (org-export-table-row-starts-header-p table-row info))
3290 (booktabsp "\\toprule\n")
3291 (t "\\hline\n"))
3292 contents
3293 (if booktabsp "\\midrule" "\\hline")
3294 (if booktabsp "\\midrule" "\\hline")
3295 columns
3296 (org-latex--translate "Continued on next page" info))))
3297 ;; When BOOKTABS are activated enforce bottom rule even when
3298 ;; no hline was specifically marked.
3299 ((and booktabsp (not (org-export-get-next-element table-row info)))
3300 "\\bottomrule"))))))
3303 ;;;; Target
3305 (defun org-latex-target (target contents info)
3306 "Transcode a TARGET object from Org to LaTeX.
3307 CONTENTS is nil. INFO is a plist holding contextual
3308 information."
3309 (format "\\label{%s}" (org-latex--label target info)))
3312 ;;;; Timestamp
3314 (defun org-latex-timestamp (timestamp contents info)
3315 "Transcode a TIMESTAMP object from Org to LaTeX.
3316 CONTENTS is nil. INFO is a plist holding contextual
3317 information."
3318 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
3319 (format
3320 (plist-get info
3321 (case (org-element-property :type timestamp)
3322 ((active active-range) :latex-active-timestamp-format)
3323 ((inactive inactive-range) :latex-inactive-timestamp-format)
3324 (otherwise :latex-diary-timestamp-format)))
3325 value)))
3328 ;;;; Underline
3330 (defun org-latex-underline (underline contents info)
3331 "Transcode UNDERLINE from Org to LaTeX.
3332 CONTENTS is the text with underline markup. INFO is a plist
3333 holding contextual information."
3334 (org-latex--text-markup contents 'underline info))
3337 ;;;; Verbatim
3339 (defun org-latex-verbatim (verbatim contents info)
3340 "Transcode a VERBATIM object from Org to LaTeX.
3341 CONTENTS is nil. INFO is a plist used as a communication
3342 channel."
3343 (org-latex--text-markup
3344 (org-element-property :value verbatim) 'verbatim info))
3347 ;;;; Verse Block
3349 (defun org-latex-verse-block (verse-block contents info)
3350 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3351 CONTENTS is verse block contents. INFO is a plist holding
3352 contextual information."
3353 (org-latex--wrap-label
3354 verse-block
3355 ;; In a verse environment, add a line break to each newline
3356 ;; character and change each white space at beginning of a line
3357 ;; into a space of 1 em. Also change each blank line with
3358 ;; a vertical space of 1 em.
3359 (format "\\begin{verse}\n%s\\end{verse}"
3360 (replace-regexp-in-string
3361 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
3362 (replace-regexp-in-string
3363 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3364 (replace-regexp-in-string
3365 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3366 contents nil t) nil t) nil t))
3367 info))
3371 ;;; End-user functions
3373 ;;;###autoload
3374 (defun org-latex-export-as-latex
3375 (&optional async subtreep visible-only body-only ext-plist)
3376 "Export current buffer as a LaTeX buffer.
3378 If narrowing is active in the current buffer, only export its
3379 narrowed part.
3381 If a region is active, export that region.
3383 A non-nil optional argument ASYNC means the process should happen
3384 asynchronously. The resulting buffer should be accessible
3385 through the `org-export-stack' interface.
3387 When optional argument SUBTREEP is non-nil, export the sub-tree
3388 at point, extracting information from the headline properties
3389 first.
3391 When optional argument VISIBLE-ONLY is non-nil, don't export
3392 contents of hidden elements.
3394 When optional argument BODY-ONLY is non-nil, only write code
3395 between \"\\begin{document}\" and \"\\end{document}\".
3397 EXT-PLIST, when provided, is a property list with external
3398 parameters overriding Org default settings, but still inferior to
3399 file-local settings.
3401 Export is done in a buffer named \"*Org LATEX Export*\", which
3402 will be displayed when `org-export-show-temporary-export-buffer'
3403 is non-nil."
3404 (interactive)
3405 (org-export-to-buffer 'latex "*Org LATEX Export*"
3406 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
3408 ;;;###autoload
3409 (defun org-latex-convert-region-to-latex ()
3410 "Assume the current region has org-mode syntax, and convert it to LaTeX.
3411 This can be used in any buffer. For example, you can write an
3412 itemized list in org-mode syntax in an LaTeX buffer and use this
3413 command to convert it."
3414 (interactive)
3415 (org-export-replace-region-by 'latex))
3417 ;;;###autoload
3418 (defun org-latex-export-to-latex
3419 (&optional async subtreep visible-only body-only ext-plist)
3420 "Export current buffer to a LaTeX file.
3422 If narrowing is active in the current buffer, only export its
3423 narrowed part.
3425 If a region is active, export that region.
3427 A non-nil optional argument ASYNC means the process should happen
3428 asynchronously. The resulting file should be accessible through
3429 the `org-export-stack' interface.
3431 When optional argument SUBTREEP is non-nil, export the sub-tree
3432 at point, extracting information from the headline properties
3433 first.
3435 When optional argument VISIBLE-ONLY is non-nil, don't export
3436 contents of hidden elements.
3438 When optional argument BODY-ONLY is non-nil, only write code
3439 between \"\\begin{document}\" and \"\\end{document}\".
3441 EXT-PLIST, when provided, is a property list with external
3442 parameters overriding Org default settings, but still inferior to
3443 file-local settings."
3444 (interactive)
3445 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3446 (org-export-to-file 'latex outfile
3447 async subtreep visible-only body-only ext-plist)))
3449 ;;;###autoload
3450 (defun org-latex-export-to-pdf
3451 (&optional async subtreep visible-only body-only ext-plist)
3452 "Export current buffer to LaTeX then process through to PDF.
3454 If narrowing is active in the current buffer, only export its
3455 narrowed part.
3457 If a region is active, export that region.
3459 A non-nil optional argument ASYNC means the process should happen
3460 asynchronously. The resulting file should be accessible through
3461 the `org-export-stack' interface.
3463 When optional argument SUBTREEP is non-nil, export the sub-tree
3464 at point, extracting information from the headline properties
3465 first.
3467 When optional argument VISIBLE-ONLY is non-nil, don't export
3468 contents of hidden elements.
3470 When optional argument BODY-ONLY is non-nil, only write code
3471 between \"\\begin{document}\" and \"\\end{document}\".
3473 EXT-PLIST, when provided, is a property list with external
3474 parameters overriding Org default settings, but still inferior to
3475 file-local settings.
3477 Return PDF file's name."
3478 (interactive)
3479 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3480 (org-export-to-file 'latex outfile
3481 async subtreep visible-only body-only ext-plist
3482 (lambda (file) (org-latex-compile file)))))
3484 (defun org-latex-compile (texfile &optional snippet)
3485 "Compile a TeX file.
3487 TEXFILE is the name of the file being compiled. Processing is
3488 done through the command specified in `org-latex-pdf-process'.
3490 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3491 file used to preview a LaTeX snippet. In this case, do not
3492 create a log buffer and do not bother removing log files.
3494 Return PDF file name or an error if it couldn't be produced."
3495 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3496 (full-name (file-truename texfile))
3497 (compiler (or (with-temp-buffer
3498 (save-excursion (insert-file-contents full-name))
3499 (when (and (search-forward-regexp
3500 (regexp-opt org-latex-compilers) (line-end-position 2) t)
3501 (progn (beginning-of-line)
3502 (looking-at-p "%")))
3503 (match-string 0)))
3504 "pdflatex"))
3505 (out-dir (file-name-directory texfile))
3506 ;; Properly set working directory for compilation.
3507 (default-directory (if (file-name-absolute-p texfile)
3508 (file-name-directory full-name)
3509 default-directory))
3510 (time (current-time))
3511 warnings)
3512 (unless snippet (message "Processing LaTeX file %s..." texfile))
3513 (save-window-excursion
3514 (cond
3515 ;; A function is provided: Apply it.
3516 ((functionp org-latex-pdf-process)
3517 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3518 ;; A list is provided: Replace %b, %f and %o with appropriate
3519 ;; values in each command before applying it. Note that while
3520 ;; "%latex" and "%bibtex" is used in `org-latex-pdf-process',
3521 ;; they are replaced with "%L" and "%B" to adhere to
3522 ;; format-spec. Output is redirected to "*Org PDF LaTeX
3523 ;; Output*" buffer.
3524 ((consp org-latex-pdf-process)
3525 (let ((outbuf (and (not snippet)
3526 (get-buffer-create "*Org PDF LaTeX Output*")))
3527 (spec (list (cons ?B (shell-quote-argument org-latex-bib-compiler))
3528 (cons ?L (shell-quote-argument compiler))
3529 (cons ?b (shell-quote-argument base-name))
3530 (cons ?f (shell-quote-argument full-name))
3531 (cons ?o (shell-quote-argument out-dir)))))
3532 (mapc (lambda (command)
3533 (shell-command (format-spec command spec) outbuf))
3534 (mapcar (lambda (command)
3535 (replace-regexp-in-string "%\\(latex\\|bibtex\\)\\>"
3536 (lambda (str) (upcase (substring str 0 2)))
3537 command))
3538 org-latex-pdf-process))
3539 ;; Collect standard errors from output buffer.
3540 (setq warnings (and (not snippet)
3541 (org-latex--collect-warnings outbuf)))))
3542 (t (error "No valid command to process to PDF")))
3543 (let ((pdffile (concat out-dir base-name ".pdf")))
3544 ;; Check for process failure. Provide collected errors if
3545 ;; possible.
3546 (if (or (not (file-exists-p pdffile))
3547 (time-less-p (nth 5 (file-attributes pdffile)) time))
3548 (error (format "PDF file %s wasn't produced" pdffile))
3549 ;; Else remove log files, when specified, and signal end of
3550 ;; process to user, along with any error encountered.
3551 (unless snippet
3552 (when org-latex-remove-logfiles
3553 (dolist (file (directory-files
3554 out-dir t
3555 (concat (regexp-quote base-name)
3556 "\\(?:\\.[0-9]+\\)?"
3557 "\\."
3558 (regexp-opt org-latex-logfiles-extensions))))
3559 (delete-file file)))
3560 (message (concat "PDF file produced"
3561 (cond
3562 ((eq warnings 'error) " with errors.")
3563 (warnings (concat " with warnings: " warnings))
3564 (t "."))))))
3565 ;; Return output file name.
3566 pdffile))))
3568 (defun org-latex--collect-warnings (buffer)
3569 "Collect some warnings from \"pdflatex\" command output.
3570 BUFFER is the buffer containing output. Return collected
3571 warnings types as a string, `error' if a LaTeX error was
3572 encountered or nil if there was none."
3573 (with-current-buffer buffer
3574 (save-excursion
3575 (goto-char (point-max))
3576 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3577 (if (re-search-forward "^!" nil t) 'error
3578 (let ((case-fold-search t)
3579 (warnings ""))
3580 (dolist (warning org-latex-known-warnings)
3581 (when (save-excursion (re-search-forward (car warning) nil t))
3582 (setq warnings (concat warnings " " (cdr warning)))))
3583 (org-string-nw-p (org-trim warnings))))))))
3585 ;;;###autoload
3586 (defun org-latex-publish-to-latex (plist filename pub-dir)
3587 "Publish an Org file to LaTeX.
3589 FILENAME is the filename of the Org file to be published. PLIST
3590 is the property list for the given project. PUB-DIR is the
3591 publishing directory.
3593 Return output file name."
3594 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3596 ;;;###autoload
3597 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3598 "Publish an Org file to PDF (via LaTeX).
3600 FILENAME is the filename of the Org file to be published. PLIST
3601 is the property list for the given project. PUB-DIR is the
3602 publishing directory.
3604 Return output file name."
3605 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3606 ;; in working directory and then moved to publishing directory.
3607 (org-publish-attachment
3608 plist
3609 (org-latex-compile
3610 (org-publish-org-to
3611 'latex filename ".tex" plist (file-name-directory filename)))
3612 pub-dir))
3615 (provide 'ox-latex)
3617 ;; Local variables:
3618 ;; generated-autoload-file: "org-loaddefs.el"
3619 ;; End:
3621 ;;; ox-latex.el ends here