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