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