org-mime.el: Fix org-mime-htmlize
[org-mode.git] / lisp / ox-latex.el
blobff42843da01f02553cb8d3062c6c4dc32516c1af
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 ;; Redefine regular options.
148 (:date "DATE" nil "\\today" parse)))
152 ;;; Internal Variables
154 (defconst org-latex-babel-language-alist
155 '(("af" . "afrikaans")
156 ("bg" . "bulgarian")
157 ("bt-br" . "brazilian")
158 ("ca" . "catalan")
159 ("cs" . "czech")
160 ("cy" . "welsh")
161 ("da" . "danish")
162 ("de" . "germanb")
163 ("de-at" . "naustrian")
164 ("de-de" . "ngerman")
165 ("el" . "greek")
166 ("en" . "english")
167 ("en-au" . "australian")
168 ("en-ca" . "canadian")
169 ("en-gb" . "british")
170 ("en-ie" . "irish")
171 ("en-nz" . "newzealand")
172 ("en-us" . "american")
173 ("es" . "spanish")
174 ("et" . "estonian")
175 ("eu" . "basque")
176 ("fi" . "finnish")
177 ("fr" . "frenchb")
178 ("fr-ca" . "canadien")
179 ("gl" . "galician")
180 ("hr" . "croatian")
181 ("hu" . "hungarian")
182 ("id" . "indonesian")
183 ("is" . "icelandic")
184 ("it" . "italian")
185 ("la" . "latin")
186 ("ms" . "malay")
187 ("nl" . "dutch")
188 ("nb" . "norsk")
189 ("nn" . "nynorsk")
190 ("no" . "norsk")
191 ("pl" . "polish")
192 ("pt" . "portuguese")
193 ("ro" . "romanian")
194 ("ru" . "russian")
195 ("sa" . "sanskrit")
196 ("sb" . "uppersorbian")
197 ("sk" . "slovak")
198 ("sl" . "slovene")
199 ("sq" . "albanian")
200 ("sr" . "serbian")
201 ("sv" . "swedish")
202 ("ta" . "tamil")
203 ("tr" . "turkish")
204 ("uk" . "ukrainian"))
205 "Alist between language code and corresponding Babel option.")
207 (defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
208 ("qbordermatrix" . "\\cr")
209 ("kbordermatrix" . "\\\\"))
210 "Alist between matrix macros and their row ending.")
212 (defconst org-latex-math-environments-re
213 (format
214 "\\`[ \t]*\\\\begin{%s\\*?}"
215 (regexp-opt
216 '("equation" "eqnarray" "math" "displaymath"
217 "align" "gather" "multline" "flalign" "alignat"
218 "xalignat" "xxalignat"
219 "subequations"
220 ;; breqn
221 "dmath" "dseries" "dgroup" "darray"
222 ;; empheq
223 "empheq")))
224 "Regexp of LaTeX math environments.")
227 ;;; User Configurable Variables
229 (defgroup org-export-latex nil
230 "Options for exporting Org mode files to LaTeX."
231 :tag "Org Export LaTeX"
232 :group 'org-export)
234 ;;;; Generic
236 (defcustom org-latex-caption-above '(table)
237 "When non-nil, place caption string at the beginning of elements.
238 Otherwise, place it near the end. When value is a list of
239 symbols, put caption above selected elements only. Allowed
240 symbols are: `image', `table', `src-block' and `special-block'."
241 :group 'org-export-latex
242 :version "25.1"
243 :package-version '(Org . "8.3")
244 :type '(choice
245 (const :tag "For all elements" t)
246 (const :tag "For no element" nil)
247 (set :tag "For the following elements only" :greedy t
248 (const :tag "Images" image)
249 (const :tag "Tables" table)
250 (const :tag "Source code" src-block)
251 (const :tag "Special blocks" special-block))))
253 (defcustom org-latex-prefer-user-labels nil
254 "Use user-provided labels instead of internal ones when non-nil.
256 When this variable is non-nil, Org will use the value of
257 CUSTOM_ID property, NAME keyword or Org target as the key for the
258 \\label commands generated.
260 By default, Org generates its own internal labels during LaTeX
261 export. This process ensures that the \\label keys are unique
262 and valid, but it means the keys are not available in advance of
263 the export process.
265 Setting this variable gives you control over how Org generates
266 labels during LaTeX export, so that you may know their keys in
267 advance. One reason to do this is that it allows you to refer to
268 various elements using a single label both in Org's link syntax
269 and in embedded LaTeX code.
271 For example, when this variable is non-nil, a headline like this:
273 ** Some section
274 :PROPERTIES:
275 :CUSTOM_ID: sec:foo
276 :END:
277 This is section [[#sec:foo]].
278 #+BEGIN_LATEX
279 And this is still section \\ref{sec:foo}.
280 #+END_LATEX
282 will be exported to LaTeX as:
284 \\subsection{Some section}
285 \\label{sec:foo}
286 This is section \\ref{sec:foo}.
287 And this is still section \\ref{sec:foo}.
289 Note, however, that setting this variable introduces a limitation
290 on the possible values for CUSTOM_ID and NAME. When this
291 variable is non-nil, Org passes their value to \\label unchanged.
292 You are responsible for ensuring that the value is a valid LaTeX
293 \\label key, and that no other \\label commands with the same key
294 appear elsewhere in your document. (Keys may contain letters,
295 numbers, and the following punctuation: '_' '.' '-' ':'.) There
296 are no such limitations on CUSTOM_ID and NAME when this variable
297 is nil.
299 For headlines that do not define the CUSTOM_ID property or
300 elements without a NAME, Org will continue to use its default
301 labeling scheme to generate labels and resolve links into proper
302 references."
303 :group 'org-export-latex
304 :type 'boolean
305 :version "25.1"
306 :package-version '(Org . "8.3"))
308 ;;;; Preamble
310 (defcustom org-latex-default-class "article"
311 "The default LaTeX class."
312 :group 'org-export-latex
313 :type '(string :tag "LaTeX class"))
315 (defcustom org-latex-classes
316 '(("article"
317 "\\documentclass[11pt]{article}"
318 ("\\section{%s}" . "\\section*{%s}")
319 ("\\subsection{%s}" . "\\subsection*{%s}")
320 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
321 ("\\paragraph{%s}" . "\\paragraph*{%s}")
322 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
323 ("report"
324 "\\documentclass[11pt]{report}"
325 ("\\part{%s}" . "\\part*{%s}")
326 ("\\chapter{%s}" . "\\chapter*{%s}")
327 ("\\section{%s}" . "\\section*{%s}")
328 ("\\subsection{%s}" . "\\subsection*{%s}")
329 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
330 ("book"
331 "\\documentclass[11pt]{book}"
332 ("\\part{%s}" . "\\part*{%s}")
333 ("\\chapter{%s}" . "\\chapter*{%s}")
334 ("\\section{%s}" . "\\section*{%s}")
335 ("\\subsection{%s}" . "\\subsection*{%s}")
336 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
337 "Alist of LaTeX classes and associated header and structure.
338 If #+LATEX_CLASS is set in the buffer, use its value and the
339 associated information. Here is the structure of each cell:
341 \(class-name
342 header-string
343 \(numbered-section . unnumbered-section)
344 ...)
346 The header string
347 -----------------
349 The HEADER-STRING is the header that will be inserted into the
350 LaTeX file. It should contain the \\documentclass macro, and
351 anything else that is needed for this setup. To this header, the
352 following commands will be added:
354 - Calls to \\usepackage for all packages mentioned in the
355 variables `org-latex-default-packages-alist' and
356 `org-latex-packages-alist'. Thus, your header definitions
357 should avoid to also request these packages.
359 - Lines specified via \"#+LATEX_HEADER:\" and
360 \"#+LATEX_HEADER_EXTRA:\" keywords.
362 If you need more control about the sequence in which the header
363 is built up, or if you want to exclude one of these building
364 blocks for a particular class, you can use the following
365 macro-like placeholders.
367 [DEFAULT-PACKAGES] \\usepackage statements for default packages
368 [NO-DEFAULT-PACKAGES] do not include any of the default packages
369 [PACKAGES] \\usepackage statements for packages
370 [NO-PACKAGES] do not include the packages
371 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
372 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
374 So a header like
376 \\documentclass{article}
377 [NO-DEFAULT-PACKAGES]
378 [EXTRA]
379 \\providecommand{\\alert}[1]{\\textbf{#1}}
380 [PACKAGES]
382 will omit the default packages, and will include the
383 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
384 to \\providecommand, and then place \\usepackage commands based
385 on the content of `org-latex-packages-alist'.
387 If your header, `org-latex-default-packages-alist' or
388 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
389 AUTO will automatically be replaced with a coding system derived
390 from `buffer-file-coding-system'. See also the variable
391 `org-latex-inputenc-alist' for a way to influence this mechanism.
393 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\",
394 AUTO will be replaced with the language related to the language
395 code specified by `org-export-default-language', which see. Note
396 that constructions such as \"\\usepackage[french,AUTO,english]{babel}\"
397 are permitted.
399 The sectioning structure
400 ------------------------
402 The sectioning structure of the class is given by the elements
403 following the header string. For each sectioning level, a number
404 of strings is specified. A %s formatter is mandatory in each
405 section string and will be replaced by the title of the section.
407 Instead of a cons cell (numbered . unnumbered), you can also
408 provide a list of 2 or 4 elements,
410 \(numbered-open numbered-close)
414 \(numbered-open numbered-close unnumbered-open unnumbered-close)
416 providing opening and closing strings for a LaTeX environment
417 that should represent the document section. The opening clause
418 should have a %s to represent the section title.
420 Instead of a list of sectioning commands, you can also specify
421 a function name. That function will be called with two
422 parameters, the (reduced) level of the headline, and a predicate
423 non-nil when the headline should be numbered. It must return
424 a format string in which the section title will be added."
425 :group 'org-export-latex
426 :type '(repeat
427 (list (string :tag "LaTeX class")
428 (string :tag "LaTeX header")
429 (repeat :tag "Levels" :inline t
430 (choice
431 (cons :tag "Heading"
432 (string :tag " numbered")
433 (string :tag "unnumbered"))
434 (list :tag "Environment"
435 (string :tag "Opening (numbered)")
436 (string :tag "Closing (numbered)")
437 (string :tag "Opening (unnumbered)")
438 (string :tag "Closing (unnumbered)"))
439 (function :tag "Hook computing sectioning"))))))
441 (defcustom org-latex-inputenc-alist nil
442 "Alist of inputenc coding system names, and what should really be used.
443 For example, adding an entry
445 (\"utf8\" . \"utf8x\")
447 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
448 are written as utf8 files."
449 :group 'org-export-latex
450 :type '(repeat
451 (cons
452 (string :tag "Derived from buffer")
453 (string :tag "Use this instead"))))
455 (defcustom org-latex-title-command "\\maketitle"
456 "The command used to insert the title just after \\begin{document}.
458 This format string may contain these elements:
460 %a for AUTHOR keyword
461 %t for TITLE keyword
462 %s for SUBTITLE keyword
463 %k for KEYWORDS line
464 %d for DESCRIPTION line
465 %c for CREATOR line
466 %l for Language keyword
467 %L for capitalized language keyword
468 %D for DATE keyword
470 If you need to use a \"%\" character, you need to escape it
471 like that: \"%%\".
473 Setting :latex-title-command in publishing projects will take
474 precedence over this variable."
475 :group 'org-export-latex
476 :type '(string :tag "Format string"))
478 (defcustom org-latex-subtitle-format "\\\\\\medskip\n\\large %s"
479 "Format string used for transcoded subtitle.
480 The format string should have at most one \"%s\"-expression,
481 which is replaced with the subtitle."
482 :group 'org-export-latex
483 :version "25.1"
484 :package-version '(Org . "8.3")
485 :type '(string :tag "Format string"))
487 (defcustom org-latex-subtitle-separate nil
488 "Non-nil means the subtitle is not typeset as part of title."
489 :group 'org-export-latex
490 :version "25.1"
491 :package-version '(Org . "8.3")
492 :type 'boolean)
494 (defcustom org-latex-toc-command "\\tableofcontents\n\n"
495 "LaTeX command to set the table of contents, list of figures, etc.
496 This command only applies to the table of contents generated with
497 the toc:nil option, not to those generated with #+TOC keyword."
498 :group 'org-export-latex
499 :type 'string)
501 (defcustom org-latex-hyperref-template
502 "\\hypersetup{\n pdfauthor={%a},\n pdftitle={%t},\n pdfkeywords={%k},
503 pdfsubject={%d},\n pdfcreator={%c}, \n pdflang={%L}}\n"
504 "Template for hyperref package options.
506 This format string may contain these elements:
508 %a for AUTHOR keyword
509 %t for TITLE keyword
510 %s for SUBTITLE keyword
511 %k for KEYWORDS line
512 %d for DESCRIPTION line
513 %c for CREATOR line
514 %l for Language keyword
515 %L for capitalized language keyword
516 %D for DATE keyword
518 If you need to use a \"%\" character, you need to escape it
519 like that: \"%%\".
521 Setting :latex-hyperref-template in publishing projects will take
522 precedence over this variable."
523 :group 'org-export-latex
524 :version "25.1"
525 :package-version '(Org . "8.3")
526 :type '(string :tag "Format string"))
528 ;;;; Headline
530 (defcustom org-latex-format-headline-function
531 'org-latex-format-headline-default-function
532 "Function for formatting the headline's text.
534 This function will be called with six arguments:
535 TODO the todo keyword (string or nil)
536 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
537 PRIORITY the priority of the headline (integer or nil)
538 TEXT the main headline text (string)
539 TAGS the tags (list of strings or nil)
540 INFO the export options (plist)
542 The function result will be used in the section format string."
543 :group 'org-export-latex
544 :version "24.4"
545 :package-version '(Org . "8.0")
546 :type 'function)
549 ;;;; Footnotes
551 (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
552 "Text used to separate footnotes."
553 :group 'org-export-latex
554 :type 'string)
557 ;;;; Timestamps
559 (defcustom org-latex-active-timestamp-format "\\textit{%s}"
560 "A printf format string to be applied to active timestamps."
561 :group 'org-export-latex
562 :type 'string)
564 (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
565 "A printf format string to be applied to inactive timestamps."
566 :group 'org-export-latex
567 :type 'string)
569 (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
570 "A printf format string to be applied to diary timestamps."
571 :group 'org-export-latex
572 :type 'string)
575 ;;;; Links
577 (defcustom org-latex-image-default-option ""
578 "Default option for images."
579 :group 'org-export-latex
580 :version "24.4"
581 :package-version '(Org . "8.0")
582 :type 'string)
584 (defcustom org-latex-image-default-width ".9\\linewidth"
585 "Default width for images.
586 This value will not be used if a height is provided."
587 :group 'org-export-latex
588 :version "24.4"
589 :package-version '(Org . "8.0")
590 :type 'string)
592 (defcustom org-latex-image-default-height ""
593 "Default height for images.
594 This value will not be used if a width is provided, or if the
595 image is wrapped within a \"figure\" or \"wrapfigure\"
596 environment."
597 :group 'org-export-latex
598 :version "24.4"
599 :package-version '(Org . "8.0")
600 :type 'string)
602 (defcustom org-latex-default-figure-position "htb"
603 "Default position for latex figures."
604 :group 'org-export-latex
605 :type 'string)
607 (defcustom org-latex-inline-image-rules
608 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
609 "Rules characterizing image files that can be inlined into LaTeX.
611 A rule consists in an association whose key is the type of link
612 to consider, and value is a regexp that will be matched against
613 link's path.
615 Note that, by default, the image extension *actually* allowed
616 depend on the way the LaTeX file is processed. When used with
617 pdflatex, pdf, jpg and png images are OK. When processing
618 through dvi to Postscript, only ps and eps are allowed. The
619 default we use here encompasses both."
620 :group 'org-export-latex
621 :version "24.4"
622 :package-version '(Org . "8.0")
623 :type '(alist :key-type (string :tag "Type")
624 :value-type (regexp :tag "Path")))
626 (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
627 "Format string for links with unknown path type."
628 :group 'org-export-latex
629 :type 'string)
632 ;;;; Tables
634 (defcustom org-latex-default-table-environment "tabular"
635 "Default environment used to build tables."
636 :group 'org-export-latex
637 :version "24.4"
638 :package-version '(Org . "8.0")
639 :type 'string)
641 (defcustom org-latex-default-table-mode 'table
642 "Default mode for tables.
644 Value can be a symbol among:
646 `table' Regular LaTeX table.
648 `math' In this mode, every cell is considered as being in math
649 mode and the complete table will be wrapped within a math
650 environment. It is particularly useful to write matrices.
652 `inline-math' This mode is almost the same as `math', but the
653 math environment will be inlined.
655 `verbatim' The table is exported as it appears in the Org
656 buffer, within a verbatim environment.
658 This value can be overridden locally with, i.e. \":mode math\" in
659 LaTeX attributes.
661 When modifying this variable, it may be useful to change
662 `org-latex-default-table-environment' accordingly."
663 :group 'org-export-latex
664 :version "24.4"
665 :package-version '(Org . "8.0")
666 :type '(choice (const :tag "Table" table)
667 (const :tag "Matrix" math)
668 (const :tag "Inline matrix" inline-math)
669 (const :tag "Verbatim" verbatim))
670 :safe (lambda (s) (memq s '(table math inline-math verbatim))))
672 (defcustom org-latex-tables-centered t
673 "When non-nil, tables are exported in a center environment."
674 :group 'org-export-latex
675 :type 'boolean
676 :safe #'booleanp)
678 (defcustom org-latex-tables-booktabs nil
679 "When non-nil, display tables in a formal \"booktabs\" style.
680 This option assumes that the \"booktabs\" package is properly
681 loaded in the header of the document. This value can be ignored
682 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
683 attributes."
684 :group 'org-export-latex
685 :version "24.4"
686 :package-version '(Org . "8.0")
687 :type 'boolean
688 :safe #'booleanp)
690 (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
691 "Format string to display numbers in scientific notation.
692 The format should have \"%s\" twice, for mantissa and exponent
693 \(i.e., \"%s\\\\times10^{%s}\").
695 When nil, no transformation is made."
696 :group 'org-export-latex
697 :version "24.4"
698 :package-version '(Org . "8.0")
699 :type '(choice
700 (string :tag "Format string")
701 (const :tag "No formatting" nil)))
703 ;;;; Text markup
705 (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
706 (code . protectedtexttt)
707 (italic . "\\emph{%s}")
708 (strike-through . "\\sout{%s}")
709 (underline . "\\uline{%s}")
710 (verbatim . protectedtexttt))
711 "Alist of LaTeX expressions to convert text markup.
713 The key must be a symbol among `bold', `code', `italic',
714 `strike-through', `underline' and `verbatim'. The value is
715 a formatting string to wrap fontified text with.
717 Value can also be set to the following symbols: `verb' and
718 `protectedtexttt'. For the former, Org will use \"\\verb\" to
719 create a format string and select a delimiter character that
720 isn't in the string. For the latter, Org will use \"\\texttt\"
721 to typeset and try to protect special characters.
723 If no association can be found for a given markup, text will be
724 returned as-is."
725 :group 'org-export-latex
726 :version "25.1"
727 :package-version '(Org . "8.3")
728 :type 'alist
729 :options '(bold code italic strike-through underline verbatim))
732 ;;;; Drawers
734 (defcustom org-latex-format-drawer-function
735 (lambda (name contents) contents)
736 "Function called to format a drawer in LaTeX code.
738 The function must accept two parameters:
739 NAME the drawer name, like \"LOGBOOK\"
740 CONTENTS the contents of the drawer.
742 The function should return the string to be exported.
744 The default function simply returns the value of CONTENTS."
745 :group 'org-export-latex
746 :version "24.4"
747 :package-version '(Org . "8.3")
748 :type 'function)
751 ;;;; Inlinetasks
753 (defcustom org-latex-format-inlinetask-function
754 'org-latex-format-inlinetask-default-function
755 "Function called to format an inlinetask in LaTeX code.
757 The function must accept seven parameters:
758 TODO the todo keyword (string or nil)
759 TODO-TYPE the todo type (symbol: `todo', `done', nil)
760 PRIORITY the inlinetask priority (integer or nil)
761 NAME the inlinetask name (string)
762 TAGS the inlinetask tags (list of strings or nil)
763 CONTENTS the contents of the inlinetask (string or nil)
764 INFO the export options (plist)
766 The function should return the string to be exported."
767 :group 'org-export-latex
768 :type 'function
769 :version "25.1"
770 :package-version '(Org . "8.3"))
773 ;; Src blocks
775 (defcustom org-latex-listings nil
776 "Non-nil means export source code using the listings package.
778 This package will fontify source code, possibly even with color.
779 If you want to use this, you also need to make LaTeX use the
780 listings package, and if you want to have color, the color
781 package. Just add these to `org-latex-packages-alist', for
782 example using customize, or with something like:
784 \(require 'ox-latex)
785 \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
786 \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
788 Alternatively,
790 \(setq org-latex-listings 'minted)
792 causes source code to be exported using the minted package as
793 opposed to listings. If you want to use minted, you need to add
794 the minted package to `org-latex-packages-alist', for example
795 using customize, or with
797 \(require 'ox-latex)
798 \(add-to-list 'org-latex-packages-alist '(\"newfloat\" \"minted\"))
800 In addition, it is necessary to install pygments
801 \(http://pygments.org), and to configure the variable
802 `org-latex-pdf-process' so that the -shell-escape option is
803 passed to pdflatex.
805 The minted choice has possible repercussions on the preview of
806 latex fragments (see `org-preview-latex-fragment'). If you run
807 into previewing problems, please consult
809 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
810 :group 'org-export-latex
811 :type '(choice
812 (const :tag "Use listings" t)
813 (const :tag "Use minted" minted)
814 (const :tag "Export verbatim" nil))
815 :safe (lambda (s) (memq s '(t nil minted))))
817 (defcustom org-latex-listings-langs
818 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
819 (c "C") (cc "C++")
820 (fortran "fortran")
821 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
822 (html "HTML") (xml "XML")
823 (tex "TeX") (latex "[LaTeX]TeX")
824 (shell-script "bash")
825 (gnuplot "Gnuplot")
826 (ocaml "Caml") (caml "Caml")
827 (sql "SQL") (sqlite "sql")
828 (makefile "make"))
829 "Alist mapping languages to their listing language counterpart.
830 The key is a symbol, the major mode symbol without the \"-mode\".
831 The value is the string that should be inserted as the language
832 parameter for the listings package. If the mode name and the
833 listings name are the same, the language does not need an entry
834 in this list - but it does not hurt if it is present."
835 :group 'org-export-latex
836 :version "24.4"
837 :package-version '(Org . "8.3")
838 :type '(repeat
839 (list
840 (symbol :tag "Major mode ")
841 (string :tag "Listings language"))))
843 (defcustom org-latex-listings-options nil
844 "Association list of options for the latex listings package.
846 These options are supplied as a comma-separated list to the
847 \\lstset command. Each element of the association list should be
848 a list containing two strings: the name of the option, and the
849 value. For example,
851 \(setq org-latex-listings-options
852 '((\"basicstyle\" \"\\\\small\")
853 \(\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
855 will typeset the code in a small size font with underlined, bold
856 black keywords.
858 Note that the same options will be applied to blocks of all
859 languages. If you need block-specific options, you may use the
860 following syntax:
862 #+ATTR_LATEX: :options key1=value1,key2=value2
863 #+BEGIN_SRC <LANG>
865 #+END_SRC"
866 :group 'org-export-latex
867 :type '(repeat
868 (list
869 (string :tag "Listings option name ")
870 (string :tag "Listings option value"))))
872 (defcustom org-latex-minted-langs
873 '((emacs-lisp "common-lisp")
874 (cc "c++")
875 (cperl "perl")
876 (shell-script "bash")
877 (caml "ocaml"))
878 "Alist mapping languages to their minted language counterpart.
879 The key is a symbol, the major mode symbol without the \"-mode\".
880 The value is the string that should be inserted as the language
881 parameter for the minted package. If the mode name and the
882 listings name are the same, the language does not need an entry
883 in this list - but it does not hurt if it is present.
885 Note that minted uses all lower case for language identifiers,
886 and that the full list of language identifiers can be obtained
887 with:
889 pygmentize -L lexers"
890 :group 'org-export-latex
891 :type '(repeat
892 (list
893 (symbol :tag "Major mode ")
894 (string :tag "Minted language"))))
896 (defcustom org-latex-minted-options nil
897 "Association list of options for the latex minted package.
899 These options are supplied within square brackets in
900 \\begin{minted} environments. Each element of the alist should
901 be a list containing two strings: the name of the option, and the
902 value. For example,
904 \(setq org-latex-minted-options
905 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
907 will result in src blocks being exported with
909 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
911 as the start of the minted environment. Note that the same
912 options will be applied to blocks of all languages. If you need
913 block-specific options, you may use the following syntax:
915 #+ATTR_LATEX: :options key1=value1,key2=value2
916 #+BEGIN_SRC <LANG>
918 #+END_SRC"
919 :group 'org-export-latex
920 :type '(repeat
921 (list
922 (string :tag "Minted option name ")
923 (string :tag "Minted option value"))))
925 (defvar org-latex-custom-lang-environments nil
926 "Alist mapping languages to language-specific LaTeX environments.
928 It is used during export of src blocks by the listings and minted
929 latex packages. For example,
931 \(setq org-latex-custom-lang-environments
932 '\(\(python \"pythoncode\"\)\)\)
934 would have the effect that if org encounters begin_src python
935 during latex export it will output
937 \\begin{pythoncode}
938 <src block body>
939 \\end{pythoncode}")
942 ;;;; Compilation
944 (defcustom org-latex-pdf-process
945 '("pdflatex -interaction nonstopmode -output-directory %o %f"
946 "pdflatex -interaction nonstopmode -output-directory %o %f"
947 "pdflatex -interaction nonstopmode -output-directory %o %f")
948 "Commands to process a LaTeX file to a PDF file.
949 This is a list of strings, each of them will be given to the
950 shell as a command. %f in the command will be replaced by the
951 full file name, %b by the file base name (i.e. without directory
952 and extension parts) and %o by the base directory of the file.
954 The reason why this is a list is that it usually takes several
955 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
956 does not have a clever mechanism to detect which of these
957 commands have to be run to get to a stable result, and it also
958 does not do any error checking.
960 By default, Org uses 3 runs of `pdflatex' to do the processing.
961 If you have texi2dvi on your system and if that does not cause
962 the infamous egrep/locale bug:
964 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
966 then `texi2dvi' is the superior choice as it automates the LaTeX
967 build process by calling the \"correct\" combinations of
968 auxiliary programs. Org does offer `texi2dvi' as one of the
969 customize options. Alternatively, `rubber' and `latexmk' also
970 provide similar functionality. The latter supports `biber' out
971 of the box.
973 Alternatively, this may be a Lisp function that does the
974 processing, so you could use this to apply the machinery of
975 AUCTeX or the Emacs LaTeX mode. This function should accept the
976 file name as its single argument."
977 :group 'org-export-pdf
978 :type '(choice
979 (repeat :tag "Shell command sequence"
980 (string :tag "Shell command"))
981 (const :tag "2 runs of pdflatex"
982 ("pdflatex -interaction nonstopmode -output-directory %o %f"
983 "pdflatex -interaction nonstopmode -output-directory %o %f"))
984 (const :tag "3 runs of pdflatex"
985 ("pdflatex -interaction nonstopmode -output-directory %o %f"
986 "pdflatex -interaction nonstopmode -output-directory %o %f"
987 "pdflatex -interaction nonstopmode -output-directory %o %f"))
988 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
989 ("pdflatex -interaction nonstopmode -output-directory %o %f"
990 "bibtex %b"
991 "pdflatex -interaction nonstopmode -output-directory %o %f"
992 "pdflatex -interaction nonstopmode -output-directory %o %f"))
993 (const :tag "2 runs of xelatex"
994 ("xelatex -interaction nonstopmode -output-directory %o %f"
995 "xelatex -interaction nonstopmode -output-directory %o %f"))
996 (const :tag "3 runs of xelatex"
997 ("xelatex -interaction nonstopmode -output-directory %o %f"
998 "xelatex -interaction nonstopmode -output-directory %o %f"
999 "xelatex -interaction nonstopmode -output-directory %o %f"))
1000 (const :tag "xelatex,bibtex,xelatex,xelatex"
1001 ("xelatex -interaction nonstopmode -output-directory %o %f"
1002 "bibtex %b"
1003 "xelatex -interaction nonstopmode -output-directory %o %f"
1004 "xelatex -interaction nonstopmode -output-directory %o %f"))
1005 (const :tag "texi2dvi"
1006 ("texi2dvi -p -b -V %f"))
1007 (const :tag "rubber"
1008 ("rubber -d --into %o %f"))
1009 (const :tag "latexmk"
1010 ("latexmk -g -pdf %f"))
1011 (function)))
1013 (defcustom org-latex-logfiles-extensions
1014 '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
1015 "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
1016 "The list of file extensions to consider as LaTeX logfiles.
1017 The logfiles will be removed if `org-latex-remove-logfiles' is
1018 non-nil."
1019 :group 'org-export-latex
1020 :version "25.1"
1021 :package-version '(Org . "8.3")
1022 :type '(repeat (string :tag "Extension")))
1024 (defcustom org-latex-remove-logfiles t
1025 "Non-nil means remove the logfiles produced by PDF production.
1026 By default, logfiles are files with these extensions: .aux, .idx,
1027 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
1028 logfiles to remove, set `org-latex-logfiles-extensions'."
1029 :group 'org-export-latex
1030 :type 'boolean)
1032 (defcustom org-latex-known-warnings
1033 '(("Reference.*?undefined" . "[undefined reference]")
1034 ("Runaway argument" . "[runaway argument]")
1035 ("Underfull \\hbox" . "[underfull hbox]")
1036 ("Overfull \\hbox" . "[overfull hbox]")
1037 ("Citation.*?undefined" . "[undefined citation]")
1038 ("Undefined control sequence" . "[undefined control sequence]"))
1039 "Alist of regular expressions and associated messages for the user.
1040 The regular expressions are used to find possible warnings in the
1041 log of a latex-run. These warnings will be reported after
1042 calling `org-latex-compile'."
1043 :group 'org-export-latex
1044 :version "25.1"
1045 :package-version '(Org . "8.3")
1046 :type '(repeat
1047 (cons
1048 (string :tag "Regexp")
1049 (string :tag "Message"))))
1053 ;;; Internal Functions
1055 (defun org-latex--caption-above-p (element info)
1056 "Non nil when caption is expected to be located above ELEMENT.
1057 INFO is a plist holding contextual information."
1058 (let ((above (plist-get info :latex-caption-above)))
1059 (if (symbolp above) above
1060 (let ((type (org-element-type element)))
1061 (memq (if (eq type 'link) 'image type) above)))))
1063 (defun org-latex--label (datum info &optional force full)
1064 "Return an appropriate label for DATUM.
1065 DATUM is an element or a `target' type object. INFO is the
1066 current export state, as a plist.
1068 Return nil if element DATUM has no NAME or VALUE affiliated
1069 keyword or no CUSTOM_ID property, unless FORCE is non-nil. In
1070 this case always return a unique label.
1072 Eventually, if FULL is non-nil, wrap label within \"\\label{}\"."
1073 (let* ((type (org-element-type datum))
1074 (user-label
1075 (org-element-property
1076 (case type
1077 ((headline inlinetask) :CUSTOM_ID)
1078 (target :value)
1079 (otherwise :name))
1080 datum))
1081 (label
1082 (and (or user-label force)
1083 (if (and user-label (plist-get info :latex-prefer-user-labels))
1084 user-label
1085 (concat (case type
1086 (headline "sec:")
1087 (table "tab:")
1088 (latex-environment
1089 (and (org-string-match-p
1090 org-latex-math-environments-re
1091 (org-element-property :value datum))
1092 "eq:"))
1093 (paragraph
1094 (and (org-element-property :caption datum)
1095 "fig:")))
1096 (org-export-get-reference datum info))))))
1097 (cond ((not full) label)
1098 (label (format "\\label{%s}%s"
1099 label
1100 (if (eq type 'target) "" "\n")))
1101 (t ""))))
1103 (defun org-latex--caption/label-string (element info)
1104 "Return caption and label LaTeX string for ELEMENT.
1106 INFO is a plist holding contextual information. If there's no
1107 caption nor label, return the empty string.
1109 For non-floats, see `org-latex--wrap-label'."
1110 (let* ((label (org-latex--label element info nil t))
1111 (main (org-export-get-caption element))
1112 (attr (org-export-read-attribute :attr_latex element))
1113 (type (org-element-type element))
1114 (nonfloat (or (and (plist-member attr :float)
1115 (not (plist-get attr :float))
1116 main)
1117 (and (eq type 'src-block)
1118 (not (plist-get attr :float))
1119 (memq (plist-get info :latex-listings)
1120 '(nil minted)))))
1121 (short (org-export-get-caption element t))
1122 (caption-from-attr-latex (plist-get attr :caption)))
1123 (cond
1124 ((org-string-nw-p caption-from-attr-latex)
1125 (concat caption-from-attr-latex "\n"))
1126 ((and (not main) (equal label "")) "")
1127 ((not main) (concat label "\n"))
1128 ;; Option caption format with short name.
1130 (format (if nonfloat "\\captionof{%s}%s{%s%s}\n"
1131 "\\caption%s%s{%s%s}\n")
1132 (if nonfloat
1133 (case type
1134 (paragraph "figure")
1135 (src-block (if (plist-get info :latex-listings)
1136 "listing"
1137 "figure"))
1138 (t (symbol-name type)))
1140 (if short (format "[%s]" (org-export-data short info)) "")
1141 label
1142 (org-export-data main info))))))
1144 (defun org-latex-guess-inputenc (header)
1145 "Set the coding system in inputenc to what the buffer is.
1147 HEADER is the LaTeX header string. This function only applies
1148 when specified inputenc option is \"AUTO\".
1150 Return the new header, as a string."
1151 (let* ((cs (or (ignore-errors
1152 (latexenc-coding-system-to-inputenc
1153 (or org-export-coding-system buffer-file-coding-system)))
1154 "utf8")))
1155 (if (not cs) header
1156 ;; First translate if that is requested.
1157 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
1158 ;; Then find the \usepackage statement and replace the option.
1159 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1160 cs header t nil 1))))
1162 (defun org-latex-guess-babel-language (header info)
1163 "Set Babel's language according to LANGUAGE keyword.
1165 HEADER is the LaTeX header string. INFO is the plist used as
1166 a communication channel.
1168 Insertion of guessed language only happens when Babel package has
1169 explicitly been loaded. Then it is added to the rest of
1170 package's options.
1172 The argument to Babel may be \"AUTO\" which is then replaced with
1173 the language of the document or `org-export-default-language'
1174 unless language in question is already loaded.
1176 Return the new header."
1177 (let ((language-code (plist-get info :language)))
1178 ;; If no language is set or Babel package is not loaded, return
1179 ;; HEADER as-is.
1180 (if (or (not (stringp language-code))
1181 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
1182 header
1183 (let ((options (save-match-data
1184 (org-split-string (match-string 1 header) ",[ \t]*")))
1185 (language (cdr (assoc language-code
1186 org-latex-babel-language-alist))))
1187 ;; If LANGUAGE is already loaded, return header without AUTO.
1188 ;; Otherwise, replace AUTO with language or append language if
1189 ;; AUTO is not present.
1190 (replace-match
1191 (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
1192 (cond ((member language options) (delete "AUTO" options))
1193 ((member "AUTO" options) options)
1194 (t (append options (list language))))
1195 ", ")
1196 t nil header 1)))))
1198 (defun org-latex--find-verb-separator (s)
1199 "Return a character not used in string S.
1200 This is used to choose a separator for constructs like \\verb."
1201 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1202 (loop for c across ll
1203 when (not (string-match (regexp-quote (char-to-string c)) s))
1204 return (char-to-string c))))
1206 (defun org-latex--make-option-string (options)
1207 "Return a comma separated string of keywords and values.
1208 OPTIONS is an alist where the key is the options keyword as
1209 a string, and the value a list containing the keyword value, or
1210 nil."
1211 (mapconcat (lambda (pair)
1212 (concat (first pair)
1213 (when (> (length (second pair)) 0)
1214 (concat "=" (second pair)))))
1215 options
1216 ","))
1218 (defun org-latex--wrap-label (element output info)
1219 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1220 INFO is the current export state, as a plist. This function
1221 should not be used for floats. See
1222 `org-latex--caption/label-string'."
1223 (if (not (and (org-string-nw-p output) (org-element-property :name element)))
1224 output
1225 (concat (format "\\phantomsection\n\\label{%s}\n"
1226 (org-latex--label element info))
1227 output)))
1229 (defun org-latex--protect-text (text)
1230 "Protect special characters in string TEXT and return it."
1231 (replace-regexp-in-string
1232 "--\\|[\\{}$%&_#~^]"
1233 (lambda (m)
1234 (cond ((equal m "--") "-{}-")
1235 ((equal m "\\") "\\textbackslash{}")
1236 ((equal m "~") "\\textasciitilde{}")
1237 ((equal m "^") "\\textasciicircum{}")
1238 (t (concat "\\" m))))
1239 text nil t))
1241 (defun org-latex--text-markup (text markup info)
1242 "Format TEXT depending on MARKUP text markup.
1243 INFO is a plist used as a communication channel. See
1244 `org-latex-text-markup-alist' for details."
1245 (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
1246 (case fmt
1247 ;; No format string: Return raw text.
1248 ((nil) text)
1249 ;; Handle the `verb' special case: Find an appropriate separator
1250 ;; and use "\\verb" command.
1251 (verb
1252 (let ((separator (org-latex--find-verb-separator text)))
1253 (concat "\\verb" separator
1254 (replace-regexp-in-string "\n" " " text)
1255 separator)))
1256 ;; Handle the `protectedtexttt' special case: Protect some
1257 ;; special chars and use "\texttt{%s}" format string.
1258 (protectedtexttt
1259 (format "\\texttt{%s}" (org-latex--protect-text text)))
1260 ;; Else use format string.
1261 (t (format fmt text)))))
1263 (defun org-latex--delayed-footnotes-definitions (element info)
1264 "Return footnotes definitions in ELEMENT as a string.
1266 INFO is a plist used as a communication channel.
1268 Footnotes definitions are returned within \"\\footnotetxt{}\"
1269 commands.
1271 This function is used within constructs that don't support
1272 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1273 \"\\footnotemark\" is used within the construct and the function
1274 just outside of it."
1275 (mapconcat
1276 (lambda (ref)
1277 (format
1278 "\\footnotetext[%s]{%s}"
1279 (org-export-get-footnote-number ref info)
1280 (org-trim
1281 (org-export-data
1282 (org-export-get-footnote-definition ref info) info))))
1283 ;; Find every footnote reference in ELEMENT.
1284 (let* (all-refs
1285 search-refs ; For byte-compiler.
1286 (search-refs
1287 (function
1288 (lambda (data)
1289 ;; Return a list of all footnote references never seen
1290 ;; before in DATA.
1291 (org-element-map data 'footnote-reference
1292 (lambda (ref)
1293 (when (org-export-footnote-first-reference-p ref info)
1294 (push ref all-refs)
1295 (when (eq (org-element-property :type ref) 'standard)
1296 (funcall search-refs
1297 (org-export-get-footnote-definition ref info)))))
1298 info)
1299 (reverse all-refs)))))
1300 (funcall search-refs element))
1301 ""))
1303 (defun org-latex--translate (s info)
1304 "Translate string S according to specified language.
1305 INFO is a plist used as a communication channel."
1306 (org-export-translate s :latex info))
1308 (defun org-latex--format-spec (info)
1309 "Create a format-spec for document meta-data.
1310 INFO is a plist used as a communication channel."
1311 (let ((language (let ((lang (plist-get info :language)))
1312 (or (cdr (assoc lang org-latex-babel-language-alist))
1313 lang))))
1314 `((?a . ,(org-export-data (plist-get info :author) info))
1315 (?t . ,(org-export-data (plist-get info :title) info))
1316 (?k . ,(org-export-data (org-latex--wrap-latex-math-block
1317 (plist-get info :keywords) info)
1318 info))
1319 (?d . ,(org-export-data (org-latex--wrap-latex-math-block
1320 (plist-get info :description) info)
1321 info))
1322 (?c . ,(plist-get info :creator))
1323 (?l . ,language)
1324 (?L . ,(capitalize language))
1325 (?D . ,(org-export-get-date info)))))
1328 ;;; Template
1330 (defun org-latex-template (contents info)
1331 "Return complete document string after LaTeX conversion.
1332 CONTENTS is the transcoded contents string. INFO is a plist
1333 holding export options."
1334 (let ((title (org-export-data (plist-get info :title) info))
1335 (spec (org-latex--format-spec info)))
1336 (concat
1337 ;; Time-stamp.
1338 (and (plist-get info :time-stamp-file)
1339 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1340 ;; Document class and packages.
1341 (let* ((class (plist-get info :latex-class))
1342 (class-options (plist-get info :latex-class-options))
1343 (header (nth 1 (assoc class (plist-get info :latex-classes))))
1344 (document-class-string
1345 (and (stringp header)
1346 (if (not class-options) header
1347 (replace-regexp-in-string
1348 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1349 class-options header t nil 1)))))
1350 (if (not document-class-string)
1351 (user-error "Unknown LaTeX class `%s'" class)
1352 (org-latex-guess-babel-language
1353 (org-latex-guess-inputenc
1354 (org-element-normalize-string
1355 (org-splice-latex-header
1356 document-class-string
1357 org-latex-default-packages-alist
1358 org-latex-packages-alist nil
1359 (concat (org-element-normalize-string
1360 (plist-get info :latex-header))
1361 (plist-get info :latex-header-extra)))))
1362 info)))
1363 ;; Possibly limit depth for headline numbering.
1364 (let ((sec-num (plist-get info :section-numbers)))
1365 (when (integerp sec-num)
1366 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1367 ;; Author.
1368 (let ((author (and (plist-get info :with-author)
1369 (let ((auth (plist-get info :author)))
1370 (and auth (org-export-data auth info)))))
1371 (email (and (plist-get info :with-email)
1372 (org-export-data (plist-get info :email) info))))
1373 (cond ((and author email (not (string= "" email)))
1374 (format "\\author{%s\\thanks{%s}}\n" author email))
1375 ((or author email) (format "\\author{%s}\n" (or author email)))))
1376 ;; Date.
1377 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1378 (format "\\date{%s}\n" (org-export-data date info)))
1379 ;; Title and subtitle.
1380 (let* ((subtitle (plist-get info :subtitle))
1381 (formatted-subtitle
1382 (when subtitle
1383 (format (plist-get info :latex-subtitle-format)
1384 (org-export-data subtitle info))))
1385 (separate (plist-get info :latex-subtitle-separate)))
1386 (concat
1387 (format "\\title{%s%s}\n" title
1388 (if separate "" (or formatted-subtitle "")))
1389 (when (and separate subtitle)
1390 (concat formatted-subtitle "\n"))))
1391 ;; Hyperref options.
1392 (let ((template (plist-get info :latex-hyperref-template)))
1393 (and (stringp template)
1394 (format-spec template spec)))
1395 ;; Document start.
1396 "\\begin{document}\n\n"
1397 ;; Title command.
1398 (let* ((title-command (plist-get info :latex-title-command))
1399 (command (and (stringp title-command)
1400 (format-spec title-command spec))))
1401 (org-element-normalize-string
1402 (cond ((not (plist-get info :with-title)) nil)
1403 ((string= "" title) nil)
1404 ((not (stringp command)) nil)
1405 ((string-match "\\(?:[^%]\\|^\\)%s" command)
1406 (format command title))
1407 (t command))))
1408 ;; Table of contents.
1409 (let ((depth (plist-get info :with-toc)))
1410 (when depth
1411 (concat (when (wholenump depth)
1412 (format "\\setcounter{tocdepth}{%d}\n" depth))
1413 (plist-get info :latex-toc-command))))
1414 ;; Document's body.
1415 contents
1416 ;; Creator.
1417 (and (plist-get info :with-creator)
1418 (concat (plist-get info :creator) "\n"))
1419 ;; Document end.
1420 "\\end{document}")))
1424 ;;; Transcode Functions
1426 ;;;; Bold
1428 (defun org-latex-bold (bold contents info)
1429 "Transcode BOLD from Org to LaTeX.
1430 CONTENTS is the text with bold markup. INFO is a plist holding
1431 contextual information."
1432 (org-latex--text-markup contents 'bold info))
1435 ;;;; Center Block
1437 (defun org-latex-center-block (center-block contents info)
1438 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1439 CONTENTS holds the contents of the center block. INFO is a plist
1440 holding contextual information."
1441 (org-latex--wrap-label
1442 center-block (format "\\begin{center}\n%s\\end{center}" contents) info))
1445 ;;;; Clock
1447 (defun org-latex-clock (clock contents info)
1448 "Transcode a CLOCK element from Org to LaTeX.
1449 CONTENTS is nil. INFO is a plist holding contextual
1450 information."
1451 (concat
1452 "\\noindent"
1453 (format "\\textbf{%s} " org-clock-string)
1454 (format (plist-get info :latex-inactive-timestamp-format)
1455 (concat (org-timestamp-translate (org-element-property :value clock))
1456 (let ((time (org-element-property :duration clock)))
1457 (and time (format " (%s)" time)))))
1458 "\\\\"))
1461 ;;;; Code
1463 (defun org-latex-code (code contents info)
1464 "Transcode a CODE object from Org to LaTeX.
1465 CONTENTS is nil. INFO is a plist used as a communication
1466 channel."
1467 (org-latex--text-markup (org-element-property :value code) 'code info))
1470 ;;;; Drawer
1472 (defun org-latex-drawer (drawer contents info)
1473 "Transcode a DRAWER element from Org to LaTeX.
1474 CONTENTS holds the contents of the block. INFO is a plist
1475 holding contextual information."
1476 (let* ((name (org-element-property :drawer-name drawer))
1477 (output (funcall (plist-get info :latex-format-drawer-function)
1478 name contents)))
1479 (org-latex--wrap-label drawer output info)))
1482 ;;;; Dynamic Block
1484 (defun org-latex-dynamic-block (dynamic-block contents info)
1485 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1486 CONTENTS holds the contents of the block. INFO is a plist
1487 holding contextual information. See `org-export-data'."
1488 (org-latex--wrap-label dynamic-block contents info))
1491 ;;;; Entity
1493 (defun org-latex-entity (entity contents info)
1494 "Transcode an ENTITY object from Org to LaTeX.
1495 CONTENTS are the definition itself. INFO is a plist holding
1496 contextual information."
1497 (org-element-property :latex entity))
1500 ;;;; Example Block
1502 (defun org-latex-example-block (example-block contents info)
1503 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1504 CONTENTS is nil. INFO is a plist holding contextual
1505 information."
1506 (when (org-string-nw-p (org-element-property :value example-block))
1507 (let ((environment (or (org-export-read-attribute
1508 :attr_latex example-block :environment)
1509 "verbatim")))
1510 (org-latex--wrap-label
1511 example-block
1512 (format "\\begin{%s}\n%s\\end{%s}"
1513 environment
1514 (org-export-format-code-default example-block info)
1515 environment)
1516 info))))
1519 ;;;; Export Block
1521 (defun org-latex-export-block (export-block contents info)
1522 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1523 CONTENTS is nil. INFO is a plist holding contextual information."
1524 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1525 (org-remove-indentation (org-element-property :value export-block))))
1528 ;;;; Export Snippet
1530 (defun org-latex-export-snippet (export-snippet contents info)
1531 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1532 CONTENTS is nil. INFO is a plist holding contextual information."
1533 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1534 (org-element-property :value export-snippet)))
1537 ;;;; Fixed Width
1539 (defun org-latex-fixed-width (fixed-width contents info)
1540 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1541 CONTENTS is nil. INFO is a plist holding contextual information."
1542 (org-latex--wrap-label
1543 fixed-width
1544 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1545 (org-remove-indentation
1546 (org-element-property :value fixed-width)))
1547 info))
1550 ;;;; Footnote Reference
1552 (defun org-latex-footnote-reference (footnote-reference contents info)
1553 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1554 CONTENTS is nil. INFO is a plist holding contextual information."
1555 (concat
1556 ;; Insert separator between two footnotes in a row.
1557 (let ((prev (org-export-get-previous-element footnote-reference info)))
1558 (when (eq (org-element-type prev) 'footnote-reference)
1559 (plist-get info :latex-footnote-separator)))
1560 (cond
1561 ;; Use \footnotemark if the footnote has already been defined.
1562 ((not (org-export-footnote-first-reference-p footnote-reference info))
1563 (format "\\footnotemark[%s]{}"
1564 (org-export-get-footnote-number footnote-reference info)))
1565 ;; Use \footnotemark if reference is within another footnote
1566 ;; reference, footnote definition or table cell.
1567 ((org-element-lineage footnote-reference
1568 '(footnote-reference footnote-definition table-cell))
1569 "\\footnotemark")
1570 ;; Otherwise, define it with \footnote command.
1572 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1573 (concat
1574 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1575 ;; Retrieve all footnote references within the footnote and
1576 ;; add their definition after it, since LaTeX doesn't support
1577 ;; them inside.
1578 (org-latex--delayed-footnotes-definitions def info)))))))
1581 ;;;; Headline
1583 (defun org-latex-headline (headline contents info)
1584 "Transcode a HEADLINE element from Org to LaTeX.
1585 CONTENTS holds the contents of the headline. INFO is a plist
1586 holding contextual information."
1587 (unless (org-element-property :footnote-section-p headline)
1588 (let* ((class (plist-get info :latex-class))
1589 (level (org-export-get-relative-level headline info))
1590 (numberedp (org-export-numbered-headline-p headline info))
1591 (class-sectioning (assoc class (plist-get info :latex-classes)))
1592 ;; Section formatting will set two placeholders: one for
1593 ;; the title and the other for the contents.
1594 (section-fmt
1595 (let ((sec (if (functionp (nth 2 class-sectioning))
1596 (funcall (nth 2 class-sectioning) level numberedp)
1597 (nth (1+ level) class-sectioning))))
1598 (cond
1599 ;; No section available for that LEVEL.
1600 ((not sec) nil)
1601 ;; Section format directly returned by a function. Add
1602 ;; placeholder for contents.
1603 ((stringp sec) (concat sec "\n%s"))
1604 ;; (numbered-section . unnumbered-section)
1605 ((not (consp (cdr sec)))
1606 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1607 ;; (numbered-open numbered-close)
1608 ((= (length sec) 2)
1609 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1610 ;; (num-in num-out no-num-in no-num-out)
1611 ((= (length sec) 4)
1612 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1613 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1614 ;; Create a temporary export back-end that hard-codes
1615 ;; "\underline" within "\section" and alike.
1616 (section-back-end
1617 (org-export-create-backend
1618 :parent 'latex
1619 :transcoders
1620 '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
1621 (text
1622 (org-export-data-with-backend
1623 (org-element-property :title headline) section-back-end info))
1624 (todo
1625 (and (plist-get info :with-todo-keywords)
1626 (let ((todo (org-element-property :todo-keyword headline)))
1627 (and todo (org-export-data todo info)))))
1628 (todo-type (and todo (org-element-property :todo-type headline)))
1629 (tags (and (plist-get info :with-tags)
1630 (org-export-get-tags headline info)))
1631 (priority (and (plist-get info :with-priority)
1632 (org-element-property :priority headline)))
1633 ;; Create the headline text along with a no-tag version.
1634 ;; The latter is required to remove tags from toc.
1635 (full-text (funcall (plist-get info :latex-format-headline-function)
1636 todo todo-type priority text tags info))
1637 ;; Associate \label to the headline for internal links.
1638 (headline-label (org-latex--label headline info t t))
1639 (pre-blanks
1640 (make-string (org-element-property :pre-blank headline) ?\n)))
1641 (if (or (not section-fmt) (org-export-low-level-p headline info))
1642 ;; This is a deep sub-tree: export it as a list item. Also
1643 ;; export as items headlines for which no section format has
1644 ;; been found.
1645 (let ((low-level-body
1646 (concat
1647 ;; If headline is the first sibling, start a list.
1648 (when (org-export-first-sibling-p headline info)
1649 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1650 ;; Itemize headline
1651 "\\item"
1652 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
1653 "\\relax")
1654 " " full-text "\n"
1655 headline-label
1656 pre-blanks
1657 contents)))
1658 ;; If headline is not the last sibling simply return
1659 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1660 ;; any blank line.
1661 (if (not (org-export-last-sibling-p headline info)) low-level-body
1662 (replace-regexp-in-string
1663 "[ \t\n]*\\'"
1664 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1665 low-level-body)))
1666 ;; This is a standard headline. Export it as a section. Add
1667 ;; an alternative heading when possible, and when this is not
1668 ;; identical to the usual heading.
1669 (let ((opt-title
1670 (funcall (plist-get info :latex-format-headline-function)
1671 todo todo-type priority
1672 (org-export-data-with-backend
1673 (org-export-get-alt-title headline info)
1674 section-back-end info)
1675 (and (eq (plist-get info :with-tags) t) tags)
1676 info))
1677 ;; Maybe end local TOC (see `org-latex-keyword').
1678 (contents
1679 (concat
1680 contents
1681 (let ((case-fold-search t)
1682 (section
1683 (let ((first (car (org-element-contents headline))))
1684 (and (eq (org-element-type first) 'section) first))))
1685 (org-element-map section 'keyword
1686 (lambda (k)
1687 (and (equal (org-element-property :key k) "TOC")
1688 (let ((v (org-element-property :value k)))
1689 (and (org-string-match-p "\\<headlines\\>" v)
1690 (org-string-match-p "\\<local\\>" v)
1691 (format "\\stopcontents[level-%d]" level)))))
1692 info t)))))
1693 (if (and opt-title
1694 (not (equal opt-title full-text))
1695 (string-match "\\`\\\\\\(.+?\\){" section-fmt))
1696 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1697 ;; Replace square brackets with parenthesis
1698 ;; since square brackets are not supported in
1699 ;; optional arguments.
1700 (replace-regexp-in-string
1701 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1702 full-text
1703 (concat headline-label pre-blanks contents))
1704 ;; Impossible to add an alternative heading. Fallback to
1705 ;; regular sectioning format string.
1706 (format section-fmt full-text
1707 (concat headline-label pre-blanks contents))))))))
1709 (defun org-latex-format-headline-default-function
1710 (todo todo-type priority text tags info)
1711 "Default format function for a headline.
1712 See `org-latex-format-headline-function' for details."
1713 (concat
1714 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1715 (and priority (format "\\framebox{\\#%c} " priority))
1716 text
1717 (and tags
1718 (format "\\hfill{}\\textsc{%s}"
1719 (mapconcat (lambda (tag) (org-latex-plain-text tag info))
1720 tags ":")))))
1723 ;;;; Horizontal Rule
1725 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1726 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1727 CONTENTS is nil. INFO is a plist holding contextual information."
1728 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1729 (prev (org-export-get-previous-element horizontal-rule info)))
1730 (concat
1731 ;; Make sure the rule doesn't start at the end of the current
1732 ;; line by separating it with a blank line from previous element.
1733 (when (and prev
1734 (let ((prev-blank (org-element-property :post-blank prev)))
1735 (or (not prev-blank) (zerop prev-blank))))
1736 "\n")
1737 (org-latex--wrap-label
1738 horizontal-rule
1739 (format "\\rule{%s}{%s}"
1740 (or (plist-get attr :width) "\\linewidth")
1741 (or (plist-get attr :thickness) "0.5pt"))
1742 info))))
1745 ;;;; Inline Src Block
1747 (defun org-latex-inline-src-block (inline-src-block contents info)
1748 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1749 CONTENTS holds the contents of the item. INFO is a plist holding
1750 contextual information."
1751 (let* ((code (org-element-property :value inline-src-block))
1752 (separator (org-latex--find-verb-separator code)))
1753 (case (plist-get info :latex-listings)
1754 ;; Do not use a special package: transcode it verbatim.
1755 ((nil) (format "\\texttt{%s}" (org-latex--protect-text code)))
1756 ;; Use minted package.
1757 (minted
1758 (let* ((org-lang (org-element-property :language inline-src-block))
1759 (mint-lang (or (cadr (assq (intern org-lang)
1760 (plist-get info :latex-minted-langs)))
1761 (downcase org-lang)))
1762 (options (org-latex--make-option-string
1763 (plist-get info :latex-minted-options))))
1764 (concat (format "\\mint%s{%s}"
1765 (if (string= options "") "" (format "[%s]" options))
1766 mint-lang)
1767 separator code separator)))
1768 ;; Use listings package.
1769 (otherwise
1770 ;; Maybe translate language's name.
1771 (let* ((org-lang (org-element-property :language inline-src-block))
1772 (lst-lang (or (cadr (assq (intern org-lang)
1773 (plist-get info :latex-listings-langs)))
1774 org-lang))
1775 (options (org-latex--make-option-string
1776 (append (plist-get info :latex-listings-options)
1777 `(("language" ,lst-lang))))))
1778 (concat (format "\\lstinline[%s]" options)
1779 separator code separator))))))
1782 ;;;; Inlinetask
1784 (defun org-latex-inlinetask (inlinetask contents info)
1785 "Transcode an INLINETASK element from Org to LaTeX.
1786 CONTENTS holds the contents of the block. INFO is a plist
1787 holding contextual information."
1788 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1789 (todo (and (plist-get info :with-todo-keywords)
1790 (let ((todo (org-element-property :todo-keyword inlinetask)))
1791 (and todo (org-export-data todo info)))))
1792 (todo-type (org-element-property :todo-type inlinetask))
1793 (tags (and (plist-get info :with-tags)
1794 (org-export-get-tags inlinetask info)))
1795 (priority (and (plist-get info :with-priority)
1796 (org-element-property :priority inlinetask)))
1797 (contents (concat (org-latex--label inlinetask info) contents)))
1798 (funcall (plist-get info :latex-format-inlinetask-function)
1799 todo todo-type priority title tags contents info)))
1801 (defun org-latex-format-inlinetask-default-function
1802 (todo todo-type priority title tags contents info)
1803 "Default format function for a inlinetasks.
1804 See `org-latex-format-inlinetask-function' for details."
1805 (let ((full-title
1806 (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1807 (when priority (format "\\framebox{\\#%c} " priority))
1808 title
1809 (when tags
1810 (format "\\hfill{}\\textsc{:%s:}"
1811 (mapconcat
1812 (lambda (tag) (org-latex-plain-text tag info))
1813 tags ":"))))))
1814 (concat "\\begin{center}\n"
1815 "\\fbox{\n"
1816 "\\begin{minipage}[c]{.6\\textwidth}\n"
1817 full-title "\n\n"
1818 (and (org-string-nw-p contents)
1819 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
1820 "\\end{minipage}\n"
1821 "}\n"
1822 "\\end{center}")))
1825 ;;;; Italic
1827 (defun org-latex-italic (italic contents info)
1828 "Transcode ITALIC from Org to LaTeX.
1829 CONTENTS is the text with italic markup. INFO is a plist holding
1830 contextual information."
1831 (org-latex--text-markup contents 'italic info))
1834 ;;;; Item
1836 (defun org-latex-item (item contents info)
1837 "Transcode an ITEM element from Org to LaTeX.
1838 CONTENTS holds the contents of the item. INFO is a plist holding
1839 contextual information."
1840 (let* ((counter
1841 (let ((count (org-element-property :counter item))
1842 (level
1843 ;; Determine level of current item to determine the
1844 ;; correct LaTeX counter to use (enumi, enumii...).
1845 (let ((parent item) (level 0))
1846 (while (memq (org-element-type
1847 (setq parent (org-export-get-parent parent)))
1848 '(plain-list item))
1849 (when (and (eq (org-element-type parent) 'plain-list)
1850 (eq (org-element-property :type parent)
1851 'ordered))
1852 (incf level)))
1853 level)))
1854 (and count
1855 (< level 5)
1856 (format "\\setcounter{enum%s}{%s}\n"
1857 (nth (1- level) '("i" "ii" "iii" "iv"))
1858 (1- count)))))
1859 (checkbox (case (org-element-property :checkbox item)
1860 (on "$\\boxtimes$ ")
1861 (off "$\\square$ ")
1862 (trans "$\\boxminus$ ")))
1863 (tag (let ((tag (org-element-property :tag item)))
1864 ;; Check-boxes must belong to the tag.
1865 (and tag (format "[{%s}] "
1866 (concat checkbox
1867 (org-export-data tag info)))))))
1868 (concat counter
1869 "\\item"
1870 (cond
1871 (tag)
1872 (checkbox (concat " " checkbox))
1873 ;; Without a tag or a check-box, if CONTENTS starts with
1874 ;; an opening square bracket, add "\relax" to "\item",
1875 ;; unless the brackets comes from an initial export
1876 ;; snippet (i.e. it is inserted willingly by the user).
1877 ((and contents
1878 (org-string-match-p "\\`[ \t]*\\[" contents)
1879 (not (let ((e (car (org-element-contents item))))
1880 (and (eq (org-element-type e) 'paragraph)
1881 (let ((o (car (org-element-contents e))))
1882 (and (eq (org-element-type o) 'export-snippet)
1883 (eq (org-export-snippet-backend o)
1884 'latex)))))))
1885 "\\relax ")
1886 (t " "))
1887 (and contents (org-trim contents))
1888 ;; If there are footnotes references in tag, be sure to
1889 ;; add their definition at the end of the item. This
1890 ;; workaround is necessary since "\footnote{}" command is
1891 ;; not supported in tags.
1892 (and tag
1893 (org-latex--delayed-footnotes-definitions
1894 (org-element-property :tag item) info)))))
1897 ;;;; Keyword
1899 (defun org-latex-keyword (keyword contents info)
1900 "Transcode a KEYWORD element from Org to LaTeX.
1901 CONTENTS is nil. INFO is a plist holding contextual information."
1902 (let ((key (org-element-property :key keyword))
1903 (value (org-element-property :value keyword)))
1904 (cond
1905 ((string= key "LATEX") value)
1906 ((string= key "INDEX") (format "\\index{%s}" value))
1907 ((string= key "TOC")
1908 (let ((case-fold-search t))
1909 (cond
1910 ((org-string-match-p "\\<headlines\\>" value)
1911 (let* ((localp (org-string-match-p "\\<local\\>" value))
1912 (parent (org-element-lineage keyword '(headline)))
1913 (level (if (not (and localp parent)) 0
1914 (org-export-get-relative-level parent info)))
1915 (depth
1916 (and (string-match "\\<[0-9]+\\>" value)
1917 (format
1918 "\\setcounter{tocdepth}{%d}"
1919 (+ (string-to-number (match-string 0 value)) level)))))
1920 (if (and localp parent)
1921 ;; Start local TOC, assuming package "titletoc" is
1922 ;; required.
1923 (format "\\startcontents[level-%d]
1924 \\printcontents[level-%d]{}{0}{%s}"
1925 level level (or depth ""))
1926 (concat depth (and depth "\n") "\\tableofcontents"))))
1927 ((org-string-match-p "\\<tables\\>" value) "\\listoftables")
1928 ((org-string-match-p "\\<listings\\>" value)
1929 (case (plist-get info :latex-listings)
1930 ((nil) "\\listoffigures")
1931 (minted "\\listoflistings")
1932 (otherwise "\\lstlistoflistings")))))))))
1935 ;;;; Latex Environment
1937 (defun org-latex-latex-environment (latex-environment contents info)
1938 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1939 CONTENTS is nil. INFO is a plist holding contextual information."
1940 (when (plist-get info :with-latex)
1941 (let ((value (org-remove-indentation
1942 (org-element-property :value latex-environment))))
1943 (if (not (org-element-property :name latex-environment)) value
1944 ;; Environment is labeled: label must be within the environment
1945 ;; (otherwise, a reference pointing to that element will count
1946 ;; the section instead).
1947 (with-temp-buffer
1948 (insert value)
1949 (goto-char (point-min))
1950 (forward-line)
1951 (insert (org-latex--label latex-environment info nil t))
1952 (buffer-string))))))
1955 ;;;; Latex Fragment
1957 (defun org-latex-latex-fragment (latex-fragment contents info)
1958 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1959 CONTENTS is nil. INFO is a plist holding contextual information."
1960 (let ((value (org-element-property :value latex-fragment)))
1961 ;; Trim math markers since the fragment is enclosed within
1962 ;; a latex-math-block object anyway.
1963 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
1964 (match-string 2 value))
1965 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
1966 (match-string 1 value))
1967 (t value))))
1970 ;;;; Line Break
1972 (defun org-latex-line-break (line-break contents info)
1973 "Transcode a LINE-BREAK object from Org to LaTeX.
1974 CONTENTS is nil. INFO is a plist holding contextual information."
1975 "\\\\\n")
1978 ;;;; Link
1980 (defun org-latex--inline-image (link info)
1981 "Return LaTeX code for an inline image.
1982 LINK is the link pointing to the inline image. INFO is a plist
1983 used as a communication channel."
1984 (let* ((parent (org-export-get-parent-element link))
1985 (path (let ((raw-path (org-element-property :path link)))
1986 (if (not (file-name-absolute-p raw-path)) raw-path
1987 (expand-file-name raw-path))))
1988 (filetype (file-name-extension path))
1989 (caption (org-latex--caption/label-string parent info))
1990 (caption-above-p (org-latex--caption-above-p link info))
1991 ;; Retrieve latex attributes from the element around.
1992 (attr (org-export-read-attribute :attr_latex parent))
1993 (float (let ((float (plist-get attr :float)))
1994 (cond ((string= float "wrap") 'wrap)
1995 ((string= float "sideways") 'sideways)
1996 ((string= float "multicolumn") 'multicolumn)
1997 ((or float
1998 (org-element-property :caption parent)
1999 (org-string-nw-p (plist-get attr :caption)))
2000 (if (and (plist-member attr :float) (not float))
2001 'nonfloat
2002 'figure))
2003 ((and (not float) (plist-member attr :float)) nil))))
2004 (placement
2005 (let ((place (plist-get attr :placement)))
2006 (cond
2007 (place (format "%s" place))
2008 ((eq float 'wrap) "{l}{0.5\\textwidth}")
2009 ((eq float 'figure)
2010 (format "[%s]" (plist-get info :latex-default-figure-position)))
2011 (t ""))))
2012 (comment-include (if (plist-get attr :comment-include) "%" ""))
2013 ;; It is possible to specify width and height in the
2014 ;; ATTR_LATEX line, and also via default variables.
2015 (width (cond ((plist-get attr :width))
2016 ((plist-get attr :height) "")
2017 ((eq float 'wrap) "0.48\\textwidth")
2018 (t (plist-get info :latex-image-default-width))))
2019 (height (cond ((plist-get attr :height))
2020 ((or (plist-get attr :width)
2021 (memq float '(figure wrap))) "")
2022 (t (plist-get info :latex-image-default-height))))
2023 (options (let ((opt (or (plist-get attr :options)
2024 (plist-get info :latex-image-default-option))))
2025 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
2026 (match-string 1 opt))))
2027 image-code)
2028 (if (member filetype '("tikz" "pgf"))
2029 ;; For tikz images:
2030 ;; - use \input to read in image file.
2031 ;; - if options are present, wrap in a tikzpicture environment.
2032 ;; - if width or height are present, use \resizebox to change
2033 ;; the image size.
2034 (progn
2035 (setq image-code (format "\\input{%s}" path))
2036 (when (org-string-nw-p options)
2037 (setq image-code
2038 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
2039 options
2040 image-code)))
2041 (when (or (org-string-nw-p width) (org-string-nw-p height))
2042 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
2043 (if (org-string-nw-p width) width "!")
2044 (if (org-string-nw-p height) height "!")
2045 image-code))))
2046 ;; For other images:
2047 ;; - add width and height to options.
2048 ;; - include the image with \includegraphics.
2049 (when (org-string-nw-p width)
2050 (setq options (concat options ",width=" width)))
2051 (when (org-string-nw-p height)
2052 (setq options (concat options ",height=" height)))
2053 (let ((search-option (org-element-property :search-option link)))
2054 (when (and search-option
2055 (equal filetype "pdf")
2056 (org-string-match-p "\\`[0-9]+\\'" search-option)
2057 (not (org-string-match-p "page=" options)))
2058 (setq options (concat options ",page=" search-option))))
2059 (setq image-code
2060 (format "\\includegraphics%s{%s}"
2061 (cond ((not (org-string-nw-p options)) "")
2062 ((= (aref options 0) ?,)
2063 (format "[%s]"(substring options 1)))
2064 (t (format "[%s]" options)))
2065 path))
2066 (when (equal filetype "svg")
2067 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
2068 "\\includesvg"
2069 image-code
2070 nil t))
2071 (setq image-code (replace-regexp-in-string "\\.svg}"
2073 image-code
2074 nil t))))
2075 ;; Return proper string, depending on FLOAT.
2076 (case float
2077 (wrap (format "\\begin{wrapfigure}%s
2078 %s\\centering
2079 %s%s
2080 %s\\end{wrapfigure}"
2081 placement
2082 (if caption-above-p caption "")
2083 comment-include image-code
2084 (if caption-above-p "" caption)))
2085 (sideways (format "\\begin{sidewaysfigure}
2086 %s\\centering
2087 %s%s
2088 %s\\end{sidewaysfigure}"
2089 (if caption-above-p caption "")
2090 comment-include image-code
2091 (if caption-above-p "" caption)))
2092 (multicolumn (format "\\begin{figure*}%s
2093 %s\\centering
2094 %s%s
2095 %s\\end{figure*}"
2096 placement
2097 (if caption-above-p caption "")
2098 comment-include image-code
2099 (if caption-above-p "" caption)))
2100 (figure (format "\\begin{figure}%s
2101 %s\\centering
2102 %s%s
2103 %s\\end{figure}"
2104 placement
2105 (if caption-above-p caption "")
2106 comment-include image-code
2107 (if caption-above-p "" caption)))
2108 (nonfloat
2109 (format "\\begin{center}
2110 %s%s
2111 %s\\end{center}"
2112 (if caption-above-p caption "")
2113 image-code
2114 (if caption-above-p "" caption)))
2115 (otherwise image-code))))
2117 (defun org-latex-link (link desc info)
2118 "Transcode a LINK object from Org to LaTeX.
2120 DESC is the description part of the link, or the empty string.
2121 INFO is a plist holding contextual information. See
2122 `org-export-data'."
2123 (let* ((type (org-element-property :type link))
2124 (raw-path (replace-regexp-in-string
2125 "%" "\\%" (org-element-property :path link) nil t))
2126 ;; Ensure DESC really exists, or set it to nil.
2127 (desc (and (not (string= desc "")) desc))
2128 (imagep (org-export-inline-image-p
2129 link (plist-get info :latex-inline-image-rules)))
2130 (path (cond
2131 ((member type '("http" "https" "ftp" "mailto" "doi"))
2132 (concat type ":" raw-path))
2133 ((string= type "file") (org-export-file-uri raw-path))
2134 (t raw-path))))
2135 (cond
2136 ;; Link type is handled by a special function.
2137 ((org-export-custom-protocol-maybe link desc 'latex))
2138 ;; Image file.
2139 (imagep (org-latex--inline-image link info))
2140 ;; Radio link: Transcode target's contents and use them as link's
2141 ;; description.
2142 ((string= type "radio")
2143 (let ((destination (org-export-resolve-radio-link link info)))
2144 (if (not destination) desc
2145 (format "\\hyperref[%s]{%s}"
2146 (org-export-get-reference destination info)
2147 desc))))
2148 ;; Links pointing to a headline: Find destination and build
2149 ;; appropriate referencing command.
2150 ((member type '("custom-id" "fuzzy" "id"))
2151 (let ((destination (if (string= type "fuzzy")
2152 (org-export-resolve-fuzzy-link link info)
2153 (org-export-resolve-id-link link info))))
2154 (case (org-element-type destination)
2155 ;; Id link points to an external file.
2156 (plain-text
2157 (if desc (format "\\href{%s}{%s}" destination desc)
2158 (format "\\url{%s}" destination)))
2159 ;; Fuzzy link points nowhere.
2160 ((nil)
2161 (format (plist-get info :latex-link-with-unknown-path-format)
2162 (or desc
2163 (org-export-data
2164 (org-element-property :raw-link link) info))))
2165 ;; LINK points to a headline. If headlines are numbered
2166 ;; and the link has no description, display headline's
2167 ;; number. Otherwise, display description or headline's
2168 ;; title.
2169 (headline
2170 (let ((label (org-latex--label destination info t)))
2171 (if (and (not desc)
2172 (org-export-numbered-headline-p destination info))
2173 (format "\\ref{%s}" label)
2174 (format "\\hyperref[%s]{%s}" label
2175 (or desc
2176 (org-export-data
2177 (org-element-property :title destination) info))))))
2178 ;; Fuzzy link points to a target. Do as above.
2179 (otherwise
2180 (let ((ref (org-latex--label destination info t)))
2181 (if (not desc) (format "\\ref{%s}" ref)
2182 (format "\\hyperref[%s]{%s}" ref desc)))))))
2183 ;; Coderef: replace link with the reference name or the
2184 ;; equivalent line number.
2185 ((string= type "coderef")
2186 (format (org-export-get-coderef-format path desc)
2187 (org-export-resolve-coderef path info)))
2188 ;; External link with a description part.
2189 ((and path desc) (format "\\href{%s}{%s}" path desc))
2190 ;; External link without a description part.
2191 (path (format "\\url{%s}" path))
2192 ;; No path, only description. Try to do something useful.
2193 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
2196 ;;;; Node Property
2198 (defun org-latex-node-property (node-property contents info)
2199 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2200 CONTENTS is nil. INFO is a plist holding contextual
2201 information."
2202 (format "%s:%s"
2203 (org-element-property :key node-property)
2204 (let ((value (org-element-property :value node-property)))
2205 (if value (concat " " value) ""))))
2208 ;;;; Paragraph
2210 (defun org-latex-paragraph (paragraph contents info)
2211 "Transcode a PARAGRAPH element from Org to LaTeX.
2212 CONTENTS is the contents of the paragraph, as a string. INFO is
2213 the plist used as a communication channel."
2214 contents)
2217 ;;;; Plain List
2219 (defun org-latex-plain-list (plain-list contents info)
2220 "Transcode a PLAIN-LIST element from Org to LaTeX.
2221 CONTENTS is the contents of the list. INFO is a plist holding
2222 contextual information."
2223 (let* ((type (org-element-property :type plain-list))
2224 (attr (org-export-read-attribute :attr_latex plain-list))
2225 (latex-type (let ((env (plist-get attr :environment)))
2226 (cond (env (format "%s" env))
2227 ((eq type 'ordered) "enumerate")
2228 ((eq type 'descriptive) "description")
2229 (t "itemize")))))
2230 (org-latex--wrap-label
2231 plain-list
2232 (format "\\begin{%s}%s\n%s\\end{%s}"
2233 latex-type
2234 (or (plist-get attr :options) "")
2235 contents
2236 latex-type)
2237 info)))
2240 ;;;; Plain Text
2242 (defun org-latex-plain-text (text info)
2243 "Transcode a TEXT string from Org to LaTeX.
2244 TEXT is the string to transcode. INFO is a plist holding
2245 contextual information."
2246 (let* ((specialp (plist-get info :with-special-strings))
2247 (output
2248 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2249 (let ((case-fold-search nil))
2250 (replace-regexp-in-string
2251 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2252 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2253 ;; However, if special strings are used, be careful not
2254 ;; to protect "\" in "\-" constructs.
2255 (replace-regexp-in-string
2256 (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\([^-]\\|$\\)"))
2257 (lambda (m)
2258 (case (string-to-char m)
2259 (?\\ "$\\\\backslash$\\1")
2260 (?~ "\\\\textasciitilde{}")
2261 (?^ "\\\\^{}")
2262 (t "\\\\\\&")))
2263 text)))))
2264 ;; Activate smart quotes. Be sure to provide original TEXT string
2265 ;; since OUTPUT may have been modified.
2266 (when (plist-get info :with-smart-quotes)
2267 (setq output (org-export-activate-smart-quotes output :latex info text)))
2268 ;; Convert special strings.
2269 (when specialp
2270 (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
2271 ;; Handle break preservation if required.
2272 (when (plist-get info :preserve-breaks)
2273 (setq output (replace-regexp-in-string
2274 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
2275 ;; Return value.
2276 output))
2279 ;;;; Planning
2281 (defun org-latex-planning (planning contents info)
2282 "Transcode a PLANNING element from Org to LaTeX.
2283 CONTENTS is nil. INFO is a plist holding contextual
2284 information."
2285 (concat
2286 "\\noindent"
2287 (mapconcat
2288 'identity
2289 (delq nil
2290 (list
2291 (let ((closed (org-element-property :closed planning)))
2292 (when closed
2293 (concat
2294 (format "\\textbf{%s} " org-closed-string)
2295 (format (plist-get info :latex-inactive-timestamp-format)
2296 (org-timestamp-translate closed)))))
2297 (let ((deadline (org-element-property :deadline planning)))
2298 (when deadline
2299 (concat
2300 (format "\\textbf{%s} " org-deadline-string)
2301 (format (plist-get info :latex-active-timestamp-format)
2302 (org-timestamp-translate deadline)))))
2303 (let ((scheduled (org-element-property :scheduled planning)))
2304 (when scheduled
2305 (concat
2306 (format "\\textbf{%s} " org-scheduled-string)
2307 (format (plist-get info :latex-active-timestamp-format)
2308 (org-timestamp-translate scheduled)))))))
2309 " ")
2310 "\\\\"))
2313 ;;;; Property Drawer
2315 (defun org-latex-property-drawer (property-drawer contents info)
2316 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2317 CONTENTS holds the contents of the drawer. INFO is a plist
2318 holding contextual information."
2319 (and (org-string-nw-p contents)
2320 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2323 ;;;; Pseudo Element: LaTeX Matrices
2325 ;; `latex-matrices' elements have the following properties:
2326 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2327 ;; `math').
2329 (defun org-latex--wrap-latex-matrices (data info)
2330 "Merge contiguous tables with the same mode within a pseudo-element.
2331 DATA is a parse tree or a secondary string. INFO is a plist
2332 containing export options. Modify DATA by side-effect and return
2333 it."
2334 (org-element-map data 'table
2335 (lambda (table)
2336 (when (eq (org-element-property :type table) 'org)
2337 (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
2338 (plist-get info :latex-default-table-mode))))
2339 (when (and (member mode '("inline-math" "math"))
2340 ;; Do not wrap twice the same table.
2341 (not (eq (org-element-type
2342 (org-element-property :parent table))
2343 'latex-matrices)))
2344 (let* ((caption (and (not (string= mode "inline-math"))
2345 (org-element-property :caption table)))
2346 (matrices
2347 (list 'latex-matrices
2348 (list :caption caption
2349 :markup
2350 (cond ((string= mode "inline-math") 'inline)
2351 (caption 'equation)
2352 (t 'math)))))
2353 (previous table)
2354 (next (org-export-get-next-element table info)))
2355 (org-element-insert-before matrices table)
2356 ;; Swallow all contiguous tables sharing the same mode.
2357 (while (and
2358 (zerop (or (org-element-property :post-blank previous) 0))
2359 (setq next (org-export-get-next-element previous info))
2360 (eq (org-element-type next) 'table)
2361 (eq (org-element-property :type next) 'org)
2362 (string= (or (org-export-read-attribute
2363 :attr_latex next :mode)
2364 (plist-get info :latex-default-table-mode))
2365 mode))
2366 (org-element-extract-element previous)
2367 (org-element-adopt-elements matrices previous)
2368 (setq previous next))
2369 (org-element-put-property
2370 matrices :post-blank (org-element-property :post-blank previous))
2371 (org-element-extract-element previous)
2372 (org-element-adopt-elements matrices previous))))))
2373 info)
2374 data)
2376 (defun org-latex-matrices (matrices contents info)
2377 "Transcode a MATRICES element from Org to LaTeX.
2378 CONTENTS is a string. INFO is a plist used as a communication
2379 channel."
2380 (format (case (org-element-property :markup matrices)
2381 (inline "\\(%s\\)")
2382 (equation "\\begin{equation}\n%s\\end{equation}")
2383 (t "\\[\n%s\\]"))
2384 contents))
2386 (defun org-latex-matrices-tree-filter (tree backend info)
2387 (org-latex--wrap-latex-matrices tree info))
2389 ;;;; Pseudo Object: LaTeX Math Block
2391 ;; `latex-math-block' objects have the following property:
2392 ;; `:post-blank'.
2394 (defun org-latex--wrap-latex-math-block (data info)
2395 "Merge contiguous math objects in a pseudo-object container.
2396 DATA is a parse tree or a secondary string. INFO is a plist
2397 containing export options. Modify DATA by side-effect and return it."
2398 (let ((valid-object-p
2399 (function
2400 ;; Non-nil when OBJ can be added to the latex math block.
2401 (lambda (obj)
2402 (case (org-element-type obj)
2403 (entity (org-element-property :latex-math-p obj))
2404 (latex-fragment
2405 (let ((value (org-element-property :value obj)))
2406 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2407 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2408 ((subscript superscript) t))))))
2409 (org-element-map data '(entity latex-fragment subscript superscript)
2410 (lambda (object)
2411 ;; Skip objects already wrapped.
2412 (when (and (not (eq (org-element-type
2413 (org-element-property :parent object))
2414 'latex-math-block))
2415 (funcall valid-object-p object))
2416 (let ((math-block (list 'latex-math-block nil))
2417 (next-elements (org-export-get-next-element object info t))
2418 (last object))
2419 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2420 (org-element-insert-before math-block object)
2421 (org-element-extract-element object)
2422 (org-element-adopt-elements math-block object)
2423 (when (zerop (or (org-element-property :post-blank object) 0))
2424 ;; MATH-BLOCK swallows consecutive math objects.
2425 (catch 'exit
2426 (dolist (next next-elements)
2427 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2428 (org-element-extract-element next)
2429 (org-element-adopt-elements math-block next)
2430 ;; Eschew the case: \beta$x$ -> \(\betax\).
2431 (unless (memq (org-element-type next)
2432 '(subscript superscript))
2433 (org-element-put-property last :post-blank 1))
2434 (setq last next)
2435 (when (> (or (org-element-property :post-blank next) 0) 0)
2436 (throw 'exit nil))))))
2437 (org-element-put-property
2438 math-block :post-blank (org-element-property :post-blank last)))))
2439 info nil '(subscript superscript latex-math-block) t)
2440 ;; Return updated DATA.
2441 data))
2443 (defun org-latex-math-block-tree-filter (tree backend info)
2444 (org-latex--wrap-latex-math-block tree info))
2446 (defun org-latex-math-block-options-filter (info backend)
2447 (dolist (prop '(:author :date :title) info)
2448 (plist-put info prop
2449 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2451 (defun org-latex-math-block (math-block contents info)
2452 "Transcode a MATH-BLOCK object from Org to LaTeX.
2453 CONTENTS is a string. INFO is a plist used as a communication
2454 channel."
2455 (when (org-string-nw-p contents)
2456 (format "\\(%s\\)" (org-trim contents))))
2458 ;;;; Quote Block
2460 (defun org-latex-quote-block (quote-block contents info)
2461 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2462 CONTENTS holds the contents of the block. INFO is a plist
2463 holding contextual information."
2464 (org-latex--wrap-label
2465 quote-block (format "\\begin{quote}\n%s\\end{quote}" contents) info))
2468 ;;;; Radio Target
2470 (defun org-latex-radio-target (radio-target text info)
2471 "Transcode a RADIO-TARGET object from Org to LaTeX.
2472 TEXT is the text of the target. INFO is a plist holding
2473 contextual information."
2474 (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
2477 ;;;; Section
2479 (defun org-latex-section (section contents info)
2480 "Transcode a SECTION element from Org to LaTeX.
2481 CONTENTS holds the contents of the section. INFO is a plist
2482 holding contextual information."
2483 contents)
2486 ;;;; Special Block
2488 (defun org-latex-special-block (special-block contents info)
2489 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2490 CONTENTS holds the contents of the block. INFO is a plist
2491 holding contextual information."
2492 (let ((type (org-element-property :type special-block))
2493 (opt (org-export-read-attribute :attr_latex special-block :options))
2494 (caption (org-latex--caption/label-string special-block info))
2495 (caption-above-p (org-latex--caption-above-p special-block info)))
2496 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2497 (and caption-above-p caption)
2498 contents
2499 (and (not caption-above-p) caption)
2500 (format "\\end{%s}" type))))
2503 ;;;; Src Block
2505 (defun org-latex-src-block (src-block contents info)
2506 "Transcode a SRC-BLOCK element from Org to LaTeX.
2507 CONTENTS holds the contents of the item. INFO is a plist holding
2508 contextual information."
2509 (when (org-string-nw-p (org-element-property :value src-block))
2510 (let* ((lang (org-element-property :language src-block))
2511 (caption (org-element-property :caption src-block))
2512 (caption-above-p (org-latex--caption-above-p src-block info))
2513 (label (org-element-property :name src-block))
2514 (custom-env (and lang
2515 (cadr (assq (intern lang)
2516 org-latex-custom-lang-environments))))
2517 (num-start (case (org-element-property :number-lines src-block)
2518 (continued (org-export-get-loc src-block info))
2519 (new 0)))
2520 (retain-labels (org-element-property :retain-labels src-block))
2521 (attributes (org-export-read-attribute :attr_latex src-block))
2522 (float (plist-get attributes :float))
2523 (listings (plist-get info :latex-listings)))
2524 (cond
2525 ;; Case 1. No source fontification.
2526 ((not listings)
2527 (let* ((caption-str (org-latex--caption/label-string src-block info))
2528 (float-env
2529 (cond ((string= "multicolumn" float)
2530 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2531 (plist-get info :latex-default-figure-position)
2532 (if caption-above-p caption-str "")
2533 (if caption-above-p "" caption-str)))
2534 (caption (concat
2535 (if caption-above-p caption-str "")
2536 "%s"
2537 (if caption-above-p "" (concat "\n" caption-str))))
2538 (t "%s"))))
2539 (format
2540 float-env
2541 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2542 (org-export-format-code-default src-block info))))))
2543 ;; Case 2. Custom environment.
2544 (custom-env
2545 (let ((caption-str (org-latex--caption/label-string src-block info)))
2546 (format "\\begin{%s}\n%s\\end{%s}\n"
2547 custom-env
2548 (concat (and caption-above-p caption-str)
2549 (org-export-format-code-default src-block info)
2550 (and (not caption-above-p) caption-str))
2551 custom-env)))
2552 ;; Case 3. Use minted package.
2553 ((eq listings 'minted)
2554 (let* ((caption-str (org-latex--caption/label-string src-block info))
2555 (float-env
2556 (cond
2557 ((string= "multicolumn" float)
2558 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2559 (if caption-above-p caption-str "")
2560 (if caption-above-p "" caption-str)))
2561 (caption
2562 (concat (if caption-above-p caption-str "")
2563 "%s"
2564 (if caption-above-p "" (concat "\n" caption-str))))
2565 (t "%s")))
2566 (options (plist-get info :latex-minted-options))
2567 (body
2568 (format
2569 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2570 ;; Options.
2571 (concat
2572 (org-latex--make-option-string
2573 (if (or (not num-start) (assoc "linenos" options))
2574 options
2575 (append
2576 `(("linenos")
2577 ("firstnumber" ,(number-to-string (1+ num-start))))
2578 options)))
2579 (let ((local-options (plist-get attributes :options)))
2580 (and local-options (concat "," local-options))))
2581 ;; Language.
2582 (or (cadr (assq (intern lang)
2583 (plist-get info :latex-minted-langs)))
2584 (downcase lang))
2585 ;; Source code.
2586 (let* ((code-info (org-export-unravel-code src-block))
2587 (max-width
2588 (apply 'max
2589 (mapcar 'length
2590 (org-split-string (car code-info)
2591 "\n")))))
2592 (org-export-format-code
2593 (car code-info)
2594 (lambda (loc num ref)
2595 (concat
2597 (when ref
2598 ;; Ensure references are flushed to the right,
2599 ;; separated with 6 spaces from the widest line
2600 ;; of code.
2601 (concat (make-string (+ (- max-width (length loc)) 6)
2602 ?\s)
2603 (format "(%s)" ref)))))
2604 nil (and retain-labels (cdr code-info)))))))
2605 ;; Return value.
2606 (format float-env body)))
2607 ;; Case 4. Use listings package.
2609 (let ((lst-lang
2610 (or (cadr (assq (intern lang)
2611 (plist-get info :latex-listings-langs)))
2612 lang))
2613 (caption-str
2614 (when caption
2615 (let ((main (org-export-get-caption src-block))
2616 (secondary (org-export-get-caption src-block t)))
2617 (if (not secondary)
2618 (format "{%s}" (org-export-data main info))
2619 (format "{[%s]%s}"
2620 (org-export-data secondary info)
2621 (org-export-data main info))))))
2622 (lst-opt (plist-get info :latex-listings-options)))
2623 (concat
2624 ;; Options.
2625 (format
2626 "\\lstset{%s}\n"
2627 (concat
2628 (org-latex--make-option-string
2629 (append
2630 lst-opt
2631 (cond
2632 ((and (not float) (plist-member attributes :float)) nil)
2633 ((string= "multicolumn" float) '(("float" "*")))
2634 ((and float (not (assoc "float" lst-opt)))
2635 `(("float" ,(plist-get info :latex-default-figure-position)))))
2636 `(("language" ,lst-lang))
2637 (if label `(("label" ,label)) '(("label" " ")))
2638 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2639 `(("captionpos" ,(if caption-above-p "t" "b")))
2640 (cond ((assoc "numbers" lst-opt) nil)
2641 ((not num-start) '(("numbers" "none")))
2642 ((zerop num-start) '(("numbers" "left")))
2643 (t `(("firstnumber" ,(number-to-string (1+ num-start)))
2644 ("numbers" "left"))))))
2645 (let ((local-options (plist-get attributes :options)))
2646 (and local-options (concat "," local-options)))))
2647 ;; Source code.
2648 (format
2649 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2650 (let* ((code-info (org-export-unravel-code src-block))
2651 (max-width
2652 (apply 'max
2653 (mapcar 'length
2654 (org-split-string (car code-info) "\n")))))
2655 (org-export-format-code
2656 (car code-info)
2657 (lambda (loc num ref)
2658 (concat
2660 (when ref
2661 ;; Ensure references are flushed to the right,
2662 ;; separated with 6 spaces from the widest line of
2663 ;; code
2664 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2665 (format "(%s)" ref)))))
2666 nil (and retain-labels (cdr code-info))))))))))))
2669 ;;;; Statistics Cookie
2671 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2672 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2673 CONTENTS is nil. INFO is a plist holding contextual information."
2674 (replace-regexp-in-string
2675 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2678 ;;;; Strike-Through
2680 (defun org-latex-strike-through (strike-through contents info)
2681 "Transcode STRIKE-THROUGH from Org to LaTeX.
2682 CONTENTS is the text with strike-through markup. INFO is a plist
2683 holding contextual information."
2684 (org-latex--text-markup contents 'strike-through info))
2687 ;;;; Subscript
2689 (defun org-latex--script-size (object info)
2690 "Transcode a subscript or superscript object.
2691 OBJECT is an Org object. INFO is a plist used as a communication
2692 channel."
2693 (let ((type (org-element-type object))
2694 (output ""))
2695 (org-element-map (org-element-contents object)
2696 (cons 'plain-text org-element-all-objects)
2697 (lambda (obj)
2698 (case (org-element-type obj)
2699 ((entity latex-fragment)
2700 (let ((data (org-trim (org-export-data obj info))))
2701 (string-match
2702 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2703 data)
2704 (setq output
2705 (concat output
2706 (match-string 1 data)
2707 (let ((blank (org-element-property :post-blank obj)))
2708 (and blank (> blank 0) "\\ "))))))
2709 (plain-text
2710 (setq output
2711 (format "%s\\text{%s}" output (org-export-data obj info))))
2712 (otherwise
2713 (setq output
2714 (concat output
2715 (org-export-data obj info)
2716 (let ((blank (org-element-property :post-blank obj)))
2717 (and blank (> blank 0) "\\ ")))))))
2718 info nil org-element-recursive-objects)
2719 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2720 ;; character.
2721 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2722 (and (> (length output) 1) "{")
2723 output
2724 (and (> (length output) 1) "}"))))
2726 (defun org-latex-subscript (subscript contents info)
2727 "Transcode a SUBSCRIPT object from Org to LaTeX.
2728 CONTENTS is the contents of the object. INFO is a plist holding
2729 contextual information."
2730 (org-latex--script-size subscript info))
2733 ;;;; Superscript
2735 (defun org-latex-superscript (superscript contents info)
2736 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2737 CONTENTS is the contents of the object. INFO is a plist holding
2738 contextual information."
2739 (org-latex--script-size superscript info))
2742 ;;;; Table
2744 ;; `org-latex-table' is the entry point for table transcoding. It
2745 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2746 ;; delegates the job to either `org-latex--table.el-table',
2747 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2748 ;; depending of the type of the table and the mode requested.
2750 ;; `org-latex--align-string' is a subroutine used to build alignment
2751 ;; string for Org tables.
2753 (defun org-latex-table (table contents info)
2754 "Transcode a TABLE element from Org to LaTeX.
2755 CONTENTS is the contents of the table. INFO is a plist holding
2756 contextual information."
2757 (if (eq (org-element-property :type table) 'table.el)
2758 ;; "table.el" table. Convert it using appropriate tools.
2759 (org-latex--table.el-table table info)
2760 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2761 (plist-get info :latex-default-table-mode))))
2762 (cond
2763 ;; Case 1: Verbatim table.
2764 ((string= type "verbatim")
2765 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2766 ;; Re-create table, without affiliated keywords.
2767 (org-trim (org-element-interpret-data
2768 `(table nil ,@(org-element-contents table))))))
2769 ;; Case 2: Matrix.
2770 ((or (string= type "math") (string= type "inline-math"))
2771 (org-latex--math-table table info))
2772 ;; Case 3: Standard table.
2773 (t (concat (org-latex--org-table table contents info)
2774 ;; When there are footnote references within the
2775 ;; table, insert their definition just after it.
2776 (org-latex--delayed-footnotes-definitions table info)))))))
2778 (defun org-latex--align-string (table info)
2779 "Return an appropriate LaTeX alignment string.
2780 TABLE is the considered table. INFO is a plist used as
2781 a communication channel."
2782 (or (org-export-read-attribute :attr_latex table :align)
2783 (let (align)
2784 ;; Extract column groups and alignment from first (non-rule)
2785 ;; row.
2786 (org-element-map
2787 (org-element-map table 'table-row
2788 (lambda (row)
2789 (and (eq (org-element-property :type row) 'standard) row))
2790 info 'first-match)
2791 'table-cell
2792 (lambda (cell)
2793 (let ((borders (org-export-table-cell-borders cell info)))
2794 ;; Check left border for the first cell only.
2795 (when (and (memq 'left borders) (not align))
2796 (push "|" align))
2797 (push (case (org-export-table-cell-alignment cell info)
2798 (left "l")
2799 (right "r")
2800 (center "c"))
2801 align)
2802 (when (memq 'right borders) (push "|" align))))
2803 info)
2804 (apply 'concat (nreverse align)))))
2806 (defun org-latex--org-table (table contents info)
2807 "Return appropriate LaTeX code for an Org table.
2809 TABLE is the table type element to transcode. CONTENTS is its
2810 contents, as a string. INFO is a plist used as a communication
2811 channel.
2813 This function assumes TABLE has `org' as its `:type' property and
2814 `table' as its `:mode' attribute."
2815 (let* ((caption (org-latex--caption/label-string table info))
2816 (attr (org-export-read-attribute :attr_latex table))
2817 ;; Determine alignment string.
2818 (alignment (org-latex--align-string table info))
2819 ;; Determine environment for the table: longtable, tabular...
2820 (table-env (or (plist-get attr :environment)
2821 (plist-get info :latex-default-table-environment)))
2822 ;; If table is a float, determine environment: table, table*
2823 ;; or sidewaystable.
2824 (float-env (unless (member table-env '("longtable" "longtabu"))
2825 (let ((float (plist-get attr :float)))
2826 (cond
2827 ((and (not float) (plist-member attr :float)) nil)
2828 ((or (string= float "sidewaystable")
2829 (string= float "sideways")) "sidewaystable")
2830 ((string= float "multicolumn") "table*")
2831 ((or float
2832 (org-element-property :caption table)
2833 (org-string-nw-p (plist-get attr :caption)))
2834 "table")))))
2835 ;; Extract others display options.
2836 (fontsize (let ((font (plist-get attr :font)))
2837 (and font (concat font "\n"))))
2838 ;; "tabular" environment doesn't allow to define a width.
2839 (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
2840 (spreadp (plist-get attr :spread))
2841 (placement
2842 (or (plist-get attr :placement)
2843 (format "[%s]" (plist-get info :latex-default-figure-position))))
2844 (centerp (if (plist-member attr :center) (plist-get attr :center)
2845 (plist-get info :latex-tables-centered)))
2846 (caption-above-p (org-latex--caption-above-p table info)))
2847 ;; Prepare the final format string for the table.
2848 (cond
2849 ;; Longtable.
2850 ((equal "longtable" table-env)
2851 (concat (and fontsize (concat "{" fontsize))
2852 (format "\\begin{longtable}{%s}\n" alignment)
2853 (and caption-above-p
2854 (org-string-nw-p caption)
2855 (concat caption "\\\\\n"))
2856 contents
2857 (and (not caption-above-p)
2858 (org-string-nw-p caption)
2859 (concat caption "\\\\\n"))
2860 "\\end{longtable}\n"
2861 (and fontsize "}")))
2862 ;; Longtabu
2863 ((equal "longtabu" table-env)
2864 (concat (and fontsize (concat "{" fontsize))
2865 (format "\\begin{longtabu}%s{%s}\n"
2866 (if width
2867 (format " %s %s "
2868 (if spreadp "spread" "to") width) "")
2869 alignment)
2870 (and caption-above-p
2871 (org-string-nw-p caption)
2872 (concat caption "\\\\\n"))
2873 contents
2874 (and (not caption-above-p)
2875 (org-string-nw-p caption)
2876 (concat caption "\\\\\n"))
2877 "\\end{longtabu}\n"
2878 (and fontsize "}")))
2879 ;; Others.
2880 (t (concat (cond
2881 (float-env
2882 (concat (format "\\begin{%s}%s\n" float-env placement)
2883 (if caption-above-p caption "")
2884 (when centerp "\\centering\n")
2885 fontsize))
2886 ((and (not float-env) caption)
2887 (concat
2888 (and centerp "\\begin{center}\n" )
2889 (if caption-above-p caption "")
2890 (cond ((and fontsize centerp) fontsize)
2891 (fontsize (concat "{" fontsize)))))
2892 (centerp (concat "\\begin{center}\n" fontsize))
2893 (fontsize (concat "{" fontsize)))
2894 (cond ((equal "tabu" table-env)
2895 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2896 (if width (format
2897 (if spreadp " spread %s " " to %s ")
2898 width) "")
2899 alignment
2900 contents))
2901 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2902 table-env
2903 (if width (format "{%s}" width) "")
2904 alignment
2905 contents
2906 table-env)))
2907 (cond
2908 (float-env
2909 (concat (if caption-above-p "" (concat "\n" caption))
2910 (format "\n\\end{%s}" float-env)))
2911 ((and (not float-env) caption)
2912 (concat
2913 (if caption-above-p "" (concat "\n" caption))
2914 (and centerp "\n\\end{center}")
2915 (and fontsize (not centerp) "}")))
2916 (centerp "\n\\end{center}")
2917 (fontsize "}")))))))
2919 (defun org-latex--table.el-table (table info)
2920 "Return appropriate LaTeX code for a table.el table.
2922 TABLE is the table type element to transcode. INFO is a plist
2923 used as a communication channel.
2925 This function assumes TABLE has `table.el' as its `:type'
2926 property."
2927 (require 'table)
2928 ;; Ensure "*org-export-table*" buffer is empty.
2929 (with-current-buffer (get-buffer-create "*org-export-table*")
2930 (erase-buffer))
2931 (let ((output (with-temp-buffer
2932 (insert (org-element-property :value table))
2933 (goto-char 1)
2934 (re-search-forward "^[ \t]*|[^|]" nil t)
2935 (table-generate-source 'latex "*org-export-table*")
2936 (with-current-buffer "*org-export-table*"
2937 (org-trim (buffer-string))))))
2938 (kill-buffer (get-buffer "*org-export-table*"))
2939 ;; Remove left out comments.
2940 (while (string-match "^%.*\n" output)
2941 (setq output (replace-match "" t t output)))
2942 (let ((attr (org-export-read-attribute :attr_latex table)))
2943 (when (plist-get attr :rmlines)
2944 ;; When the "rmlines" attribute is provided, remove all hlines
2945 ;; but the the one separating heading from the table body.
2946 (let ((n 0) (pos 0))
2947 (while (and (< (length output) pos)
2948 (setq pos (string-match "^\\\\hline\n?" output pos)))
2949 (incf n)
2950 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2951 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2952 (plist-get info :latex-tables-centered))))
2953 (if (not centerp) output
2954 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2956 (defun org-latex--math-table (table info)
2957 "Return appropriate LaTeX code for a matrix.
2959 TABLE is the table type element to transcode. INFO is a plist
2960 used as a communication channel.
2962 This function assumes TABLE has `org' as its `:type' property and
2963 `inline-math' or `math' as its `:mode' attribute."
2964 (let* ((attr (org-export-read-attribute :attr_latex table))
2965 (env (or (plist-get attr :environment)
2966 (plist-get info :latex-default-table-environment)))
2967 (contents
2968 (mapconcat
2969 (lambda (row)
2970 ;; Ignore horizontal rules.
2971 (when (eq (org-element-property :type row) 'standard)
2972 ;; Return each cell unmodified.
2973 (concat
2974 (mapconcat
2975 (lambda (cell)
2976 (substring (org-element-interpret-data cell) 0 -1))
2977 (org-element-map row 'table-cell #'identity info) "&")
2978 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2979 "\n")))
2980 (org-element-map table 'table-row #'identity info) "")))
2981 (concat
2982 ;; Prefix.
2983 (plist-get attr :math-prefix)
2984 ;; Environment. Also treat special cases.
2985 (cond ((member env '("array" "tabular"))
2986 (let ((align (make-string
2987 (cdr (org-export-table-dimensions table info)) ?c)))
2988 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
2989 ((assoc env org-latex-table-matrix-macros)
2990 (format "\\%s%s{\n%s}"
2992 (or (plist-get attr :math-arguments) "")
2993 contents))
2994 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2995 ;; Suffix.
2996 (plist-get attr :math-suffix))))
2999 ;;;; Table Cell
3001 (defun org-latex-table-cell (table-cell contents info)
3002 "Transcode a TABLE-CELL element from Org to LaTeX.
3003 CONTENTS is the cell contents. INFO is a plist used as
3004 a communication channel."
3005 (concat
3006 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
3007 (if (and contents
3008 scientific-format
3009 (string-match orgtbl-exp-regexp contents))
3010 ;; Use appropriate format string for scientific
3011 ;; notation.
3012 (format scientific-format
3013 (match-string 1 contents)
3014 (match-string 2 contents))
3015 contents))
3016 (when (org-export-get-next-element table-cell info) " & ")))
3019 ;;;; Table Row
3021 (defun org-latex-table-row (table-row contents info)
3022 "Transcode a TABLE-ROW element from Org to LaTeX.
3023 CONTENTS is the contents of the row. INFO is a plist used as
3024 a communication channel."
3025 (let* ((attr (org-export-read-attribute :attr_latex
3026 (org-export-get-parent table-row)))
3027 (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
3028 (plist-get info :latex-tables-booktabs)))
3029 (longtablep
3030 (member (or (plist-get attr :environment)
3031 (plist-get info :latex-default-table-environment))
3032 '("longtable" "longtabu"))))
3033 (if (eq (org-element-property :type table-row) 'rule)
3034 (cond
3035 ((not booktabsp) "\\hline")
3036 ((not (org-export-get-previous-element table-row info)) "\\toprule")
3037 ((not (org-export-get-next-element table-row info)) "\\bottomrule")
3038 ((and longtablep
3039 (org-export-table-row-ends-header-p
3040 (org-export-get-previous-element table-row info) info))
3042 (t "\\midrule"))
3043 (concat
3044 ;; When BOOKTABS are activated enforce top-rule even when no
3045 ;; hline was specifically marked.
3046 (and booktabsp (not (org-export-get-previous-element table-row info))
3047 "\\toprule\n")
3048 contents "\\\\\n"
3049 (cond
3050 ;; Special case for long tables. Define header and footers.
3051 ((and longtablep (org-export-table-row-ends-header-p table-row info))
3052 (let ((columns (cdr (org-export-table-dimensions
3053 (org-export-get-parent-table table-row) info))))
3054 (format "%s
3055 \\endfirsthead
3056 \\multicolumn{%d}{l}{%s} \\\\
3058 %s \\\\\n
3060 \\endhead
3061 %s\\multicolumn{%d}{r}{%s} \\\\
3062 \\endfoot
3063 \\endlastfoot"
3064 (if booktabsp "\\midrule" "\\hline")
3065 columns
3066 (org-latex--translate "Continued from previous page" info)
3067 (cond
3068 ((not (org-export-table-row-starts-header-p table-row info))
3070 (booktabsp "\\toprule\n")
3071 (t "\\hline\n"))
3072 contents
3073 (if booktabsp "\\midrule" "\\hline")
3074 (if booktabsp "\\midrule" "\\hline")
3075 columns
3076 (org-latex--translate "Continued on next page" info))))
3077 ;; When BOOKTABS are activated enforce bottom rule even when
3078 ;; no hline was specifically marked.
3079 ((and booktabsp (not (org-export-get-next-element table-row info)))
3080 "\\bottomrule"))))))
3083 ;;;; Target
3085 (defun org-latex-target (target contents info)
3086 "Transcode a TARGET object from Org to LaTeX.
3087 CONTENTS is nil. INFO is a plist holding contextual
3088 information."
3089 (format "\\label{%s}" (org-latex--label target info)))
3092 ;;;; Timestamp
3094 (defun org-latex-timestamp (timestamp contents info)
3095 "Transcode a TIMESTAMP object from Org to LaTeX.
3096 CONTENTS is nil. INFO is a plist holding contextual
3097 information."
3098 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
3099 (format
3100 (plist-get info
3101 (case (org-element-property :type timestamp)
3102 ((active active-range) :latex-active-timestamp-format)
3103 ((inactive inactive-range) :latex-inactive-timestamp-format)
3104 (otherwise :latex-diary-timestamp-format)))
3105 value)))
3108 ;;;; Underline
3110 (defun org-latex-underline (underline contents info)
3111 "Transcode UNDERLINE from Org to LaTeX.
3112 CONTENTS is the text with underline markup. INFO is a plist
3113 holding contextual information."
3114 (org-latex--text-markup contents 'underline info))
3117 ;;;; Verbatim
3119 (defun org-latex-verbatim (verbatim contents info)
3120 "Transcode a VERBATIM object from Org to LaTeX.
3121 CONTENTS is nil. INFO is a plist used as a communication
3122 channel."
3123 (org-latex--text-markup
3124 (org-element-property :value verbatim) 'verbatim info))
3127 ;;;; Verse Block
3129 (defun org-latex-verse-block (verse-block contents info)
3130 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3131 CONTENTS is verse block contents. INFO is a plist holding
3132 contextual information."
3133 (org-latex--wrap-label
3134 verse-block
3135 ;; In a verse environment, add a line break to each newline
3136 ;; character and change each white space at beginning of a line
3137 ;; into a space of 1 em. Also change each blank line with
3138 ;; a vertical space of 1 em.
3139 (format "\\begin{verse}\n%s\\end{verse}"
3140 (replace-regexp-in-string
3141 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
3142 (replace-regexp-in-string
3143 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3144 (replace-regexp-in-string
3145 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3146 contents nil t) nil t) nil t))
3147 info))
3151 ;;; End-user functions
3153 ;;;###autoload
3154 (defun org-latex-export-as-latex
3155 (&optional async subtreep visible-only body-only ext-plist)
3156 "Export current buffer as a LaTeX buffer.
3158 If narrowing is active in the current buffer, only export its
3159 narrowed part.
3161 If a region is active, export that region.
3163 A non-nil optional argument ASYNC means the process should happen
3164 asynchronously. The resulting buffer should be accessible
3165 through the `org-export-stack' interface.
3167 When optional argument SUBTREEP is non-nil, export the sub-tree
3168 at point, extracting information from the headline properties
3169 first.
3171 When optional argument VISIBLE-ONLY is non-nil, don't export
3172 contents of hidden elements.
3174 When optional argument BODY-ONLY is non-nil, only write code
3175 between \"\\begin{document}\" and \"\\end{document}\".
3177 EXT-PLIST, when provided, is a property list with external
3178 parameters overriding Org default settings, but still inferior to
3179 file-local settings.
3181 Export is done in a buffer named \"*Org LATEX Export*\", which
3182 will be displayed when `org-export-show-temporary-export-buffer'
3183 is non-nil."
3184 (interactive)
3185 (org-export-to-buffer 'latex "*Org LATEX Export*"
3186 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
3188 ;;;###autoload
3189 (defun org-latex-convert-region-to-latex ()
3190 "Assume the current region has org-mode syntax, and convert it to LaTeX.
3191 This can be used in any buffer. For example, you can write an
3192 itemized list in org-mode syntax in an LaTeX buffer and use this
3193 command to convert it."
3194 (interactive)
3195 (org-export-replace-region-by 'latex))
3197 ;;;###autoload
3198 (defun org-latex-export-to-latex
3199 (&optional async subtreep visible-only body-only ext-plist)
3200 "Export current buffer to a LaTeX file.
3202 If narrowing is active in the current buffer, only export its
3203 narrowed part.
3205 If a region is active, export that region.
3207 A non-nil optional argument ASYNC means the process should happen
3208 asynchronously. The resulting file should be accessible through
3209 the `org-export-stack' interface.
3211 When optional argument SUBTREEP is non-nil, export the sub-tree
3212 at point, extracting information from the headline properties
3213 first.
3215 When optional argument VISIBLE-ONLY is non-nil, don't export
3216 contents of hidden elements.
3218 When optional argument BODY-ONLY is non-nil, only write code
3219 between \"\\begin{document}\" and \"\\end{document}\".
3221 EXT-PLIST, when provided, is a property list with external
3222 parameters overriding Org default settings, but still inferior to
3223 file-local settings."
3224 (interactive)
3225 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3226 (org-export-to-file 'latex outfile
3227 async subtreep visible-only body-only ext-plist)))
3229 ;;;###autoload
3230 (defun org-latex-export-to-pdf
3231 (&optional async subtreep visible-only body-only ext-plist)
3232 "Export current buffer to LaTeX then process through to PDF.
3234 If narrowing is active in the current buffer, only export its
3235 narrowed part.
3237 If a region is active, export that region.
3239 A non-nil optional argument ASYNC means the process should happen
3240 asynchronously. The resulting file should be accessible through
3241 the `org-export-stack' interface.
3243 When optional argument SUBTREEP is non-nil, export the sub-tree
3244 at point, extracting information from the headline properties
3245 first.
3247 When optional argument VISIBLE-ONLY is non-nil, don't export
3248 contents of hidden elements.
3250 When optional argument BODY-ONLY is non-nil, only write code
3251 between \"\\begin{document}\" and \"\\end{document}\".
3253 EXT-PLIST, when provided, is a property list with external
3254 parameters overriding Org default settings, but still inferior to
3255 file-local settings.
3257 Return PDF file's name."
3258 (interactive)
3259 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3260 (org-export-to-file 'latex outfile
3261 async subtreep visible-only body-only ext-plist
3262 (lambda (file) (org-latex-compile file)))))
3264 (defun org-latex-compile (texfile &optional snippet)
3265 "Compile a TeX file.
3267 TEXFILE is the name of the file being compiled. Processing is
3268 done through the command specified in `org-latex-pdf-process'.
3270 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3271 file used to preview a LaTeX snippet. In this case, do not
3272 create a log buffer and do not bother removing log files.
3274 Return PDF file name or an error if it couldn't be produced."
3275 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3276 (full-name (file-truename texfile))
3277 (out-dir (file-name-directory texfile))
3278 ;; Properly set working directory for compilation.
3279 (default-directory (if (file-name-absolute-p texfile)
3280 (file-name-directory full-name)
3281 default-directory))
3282 (time (current-time))
3283 warnings)
3284 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
3285 (save-window-excursion
3286 (cond
3287 ;; A function is provided: Apply it.
3288 ((functionp org-latex-pdf-process)
3289 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3290 ;; A list is provided: Replace %b, %f and %o with appropriate
3291 ;; values in each command before applying it. Output is
3292 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3293 ((consp org-latex-pdf-process)
3294 (let ((outbuf (and (not snippet)
3295 (get-buffer-create "*Org PDF LaTeX Output*"))))
3296 (dolist (command org-latex-pdf-process)
3297 (shell-command
3298 (replace-regexp-in-string
3299 "%b" (shell-quote-argument base-name)
3300 (replace-regexp-in-string
3301 "%f" (shell-quote-argument full-name)
3302 (replace-regexp-in-string
3303 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
3304 outbuf))
3305 ;; Collect standard errors from output buffer.
3306 (setq warnings (and (not snippet)
3307 (org-latex--collect-warnings outbuf)))))
3308 (t (error "No valid command to process to PDF")))
3309 (let ((pdffile (concat out-dir base-name ".pdf")))
3310 ;; Check for process failure. Provide collected errors if
3311 ;; possible.
3312 (if (or (not (file-exists-p pdffile))
3313 (time-less-p (nth 5 (file-attributes pdffile)) time))
3314 (error (format "PDF file %s wasn't produced" pdffile))
3315 ;; Else remove log files, when specified, and signal end of
3316 ;; process to user, along with any error encountered.
3317 (unless snippet
3318 (when org-latex-remove-logfiles
3319 (dolist (file (directory-files
3320 out-dir t
3321 (concat (regexp-quote base-name)
3322 "\\(?:\\.[0-9]+\\)?"
3323 "\\."
3324 (regexp-opt org-latex-logfiles-extensions))))
3325 (delete-file file)))
3326 (message (concat "PDF file produced"
3327 (cond
3328 ((eq warnings 'error) " with errors.")
3329 (warnings (concat " with warnings: " warnings))
3330 (t "."))))))
3331 ;; Return output file name.
3332 pdffile))))
3334 (defun org-latex--collect-warnings (buffer)
3335 "Collect some warnings from \"pdflatex\" command output.
3336 BUFFER is the buffer containing output. Return collected
3337 warnings types as a string, `error' if a LaTeX error was
3338 encountered or nil if there was none."
3339 (with-current-buffer buffer
3340 (save-excursion
3341 (goto-char (point-max))
3342 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3343 (if (re-search-forward "^!" nil t) 'error
3344 (let ((case-fold-search t)
3345 (warnings ""))
3346 (dolist (warning org-latex-known-warnings)
3347 (when (save-excursion (re-search-forward (car warning) nil t))
3348 (setq warnings (concat warnings " " (cdr warning)))))
3349 (org-string-nw-p (org-trim warnings))))))))
3351 ;;;###autoload
3352 (defun org-latex-publish-to-latex (plist filename pub-dir)
3353 "Publish an Org file to LaTeX.
3355 FILENAME is the filename of the Org file to be published. PLIST
3356 is the property list for the given project. PUB-DIR is the
3357 publishing directory.
3359 Return output file name."
3360 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3362 ;;;###autoload
3363 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3364 "Publish an Org file to PDF (via LaTeX).
3366 FILENAME is the filename of the Org file to be published. PLIST
3367 is the property list for the given project. PUB-DIR is the
3368 publishing directory.
3370 Return output file name."
3371 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3372 ;; in working directory and then moved to publishing directory.
3373 (org-publish-attachment
3374 plist
3375 (org-latex-compile
3376 (org-publish-org-to
3377 'latex filename ".tex" plist (file-name-directory filename)))
3378 pub-dir))
3381 (provide 'ox-latex)
3383 ;; Local variables:
3384 ;; generated-autoload-file: "org-loaddefs.el"
3385 ;; End:
3387 ;;; ox-latex.el ends here