ox: Remove comments and comment blocks prior to export
[org-mode.git] / lisp / ox-latex.el
blob9e86863a4b2c57897c5d380835af9325f0238cb1
1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2015 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; See Org manual for details.
27 ;;; Code:
29 (eval-when-compile (require 'cl))
30 (require 'ox)
31 (require 'ox-publish)
33 (defvar org-latex-default-packages-alist)
34 (defvar org-latex-packages-alist)
35 (defvar orgtbl-exp-regexp)
39 ;;; Define Back-End
41 (org-export-define-backend 'latex
42 '((bold . org-latex-bold)
43 (center-block . org-latex-center-block)
44 (clock . org-latex-clock)
45 (code . org-latex-code)
46 (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}.
387 This format string may contain these elements:
389 %a for AUTHOR keyword
390 %t for TITLE keyword
391 %k for KEYWORDS line
392 %d for DESCRIPTION line
393 %c for CREATOR line
394 %l for Language keyword
395 %L for capitalized language keyword
396 %D for DATE keyword
398 If you need to use a \"%\" character, you need to escape it
399 like that: \"%%\".
401 Setting :latex-title-command in publishing projects will take
402 precedence over this variable."
403 :group 'org-export-latex
404 :type '(string :tag "Format string"))
406 (defcustom org-latex-toc-command "\\tableofcontents\n\n"
407 "LaTeX command to set the table of contents, list of figures, etc.
408 This command only applies to the table of contents generated with
409 the toc:nil option, not to those generated with #+TOC keyword."
410 :group 'org-export-latex
411 :type 'string)
413 (defcustom org-latex-hyperref-template
414 "\\hypersetup{\n pdfauthor={%a},\n pdftitle={%t},\n pdfkeywords={%k},
415 pdfsubject={%d},\n pdfcreator={%c}, \n pdflang={%L}}\n"
416 "Template for hyperref package options.
418 This format string may contain these elements:
420 %a for AUTHOR keyword
421 %t for TITLE keyword
422 %k for KEYWORDS line
423 %d for DESCRIPTION line
424 %c for CREATOR line
425 %l for Language keyword
426 %L for capitalized language keyword
427 %D for DATE keyword
429 If you need to use a \"%\" character, you need to escape it
430 like that: \"%%\".
432 Setting :latex-hyperref-template in publishing projects will take
433 precedence over this variable."
434 :group 'org-export-latex
435 :version "25.1"
436 :package-version '(Org . "8.3")
437 :type '(string :tag "Format string"))
439 ;;;; Headline
441 (defcustom org-latex-format-headline-function
442 'org-latex-format-headline-default-function
443 "Function for formatting the headline's text.
445 This function will be called with six arguments:
446 TODO the todo keyword (string or nil)
447 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
448 PRIORITY the priority of the headline (integer or nil)
449 TEXT the main headline text (string)
450 TAGS the tags (list of strings or nil)
451 INFO the export options (plist)
453 The function result will be used in the section format string."
454 :group 'org-export-latex
455 :version "24.4"
456 :package-version '(Org . "8.0")
457 :type 'function)
459 (defcustom org-latex-custom-id-as-label nil
460 "Toggle use of CUSTOM_ID properties for generating section labels.
462 When this variable is non-nil, Org will use the value of a
463 headline's CUSTOM_ID property as the key for the \\label command
464 for the LaTeX section corresponding to the headline.
466 By default, Org generates its own internal section labels for all
467 headlines during LaTeX export. This process ensures that the
468 \\label keys are unique and valid, but it means the keys are not
469 available in advance of the export process.
471 Setting this variable gives you control over how Org generates
472 labels for sections during LaTeX export, so that you may know
473 their keys in advance. One reason to do this is that it allows
474 you to refer to headlines using a single label both in Org's link
475 syntax and in embedded LaTeX code.
477 For example, when this variable is non-nil, a headline like this:
479 ** Some section
480 :PROPERTIES:
481 :CUSTOM_ID: sec:foo
482 :END:
483 This is section [[#sec:foo]].
484 #+BEGIN_LATEX
485 And this is still section \\ref{sec:foo}.
486 #+END_LATEX
488 will be exported to LaTeX as:
490 \\subsection{Some section}
491 \\label{sec:foo}
492 This is section \\ref{sec:foo}.
493 And this is still section \\ref{sec:foo}.
495 Note, however, that setting this variable introduces a limitation
496 on the possible values for CUSTOM_ID. When this variable is
497 non-nil and a headline defines a CUSTOM_ID value, Org simply
498 passes this value to \\label unchanged. You are responsible for
499 ensuring that the value is a valid LaTeX \\label key, and that no
500 other \\label commands with the same key appear elsewhere in your
501 document. (Keys may contain letters, numbers, and the following
502 punctuation: '_' '.' '-' ':'.) There are no such limitations on
503 CUSTOM_ID when this variable is nil.
505 For headlines that do not define the CUSTOM_ID property, Org will
506 continue to use its default labeling scheme to generate labels
507 and resolve links into section references."
508 :group 'org-export-latex
509 :type 'boolean
510 :version "25.1"
511 :package-version '(Org . "8.3"))
513 ;;;; Footnotes
515 (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
516 "Text used to separate footnotes."
517 :group 'org-export-latex
518 :type 'string)
521 ;;;; Timestamps
523 (defcustom org-latex-active-timestamp-format "\\textit{%s}"
524 "A printf format string to be applied to active timestamps."
525 :group 'org-export-latex
526 :type 'string)
528 (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
529 "A printf format string to be applied to inactive timestamps."
530 :group 'org-export-latex
531 :type 'string)
533 (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
534 "A printf format string to be applied to diary timestamps."
535 :group 'org-export-latex
536 :type 'string)
539 ;;;; Links
541 (defcustom org-latex-image-default-option ""
542 "Default option for images."
543 :group 'org-export-latex
544 :version "24.4"
545 :package-version '(Org . "8.0")
546 :type 'string)
548 (defcustom org-latex-image-default-width ".9\\linewidth"
549 "Default width for images.
550 This value will not be used if a height is provided."
551 :group 'org-export-latex
552 :version "24.4"
553 :package-version '(Org . "8.0")
554 :type 'string)
556 (defcustom org-latex-image-default-height ""
557 "Default height for images.
558 This value will not be used if a width is provided, or if the
559 image is wrapped within a \"figure\" or \"wrapfigure\"
560 environment."
561 :group 'org-export-latex
562 :version "24.4"
563 :package-version '(Org . "8.0")
564 :type 'string)
566 (defcustom org-latex-default-figure-position "htb"
567 "Default position for latex figures."
568 :group 'org-export-latex
569 :type 'string)
571 (defcustom org-latex-inline-image-rules
572 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
573 "Rules characterizing image files that can be inlined into LaTeX.
575 A rule consists in an association whose key is the type of link
576 to consider, and value is a regexp that will be matched against
577 link's path.
579 Note that, by default, the image extension *actually* allowed
580 depend on the way the LaTeX file is processed. When used with
581 pdflatex, pdf, jpg and png images are OK. When processing
582 through dvi to Postscript, only ps and eps are allowed. The
583 default we use here encompasses both."
584 :group 'org-export-latex
585 :version "24.4"
586 :package-version '(Org . "8.0")
587 :type '(alist :key-type (string :tag "Type")
588 :value-type (regexp :tag "Path")))
590 (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
591 "Format string for links with unknown path type."
592 :group 'org-export-latex
593 :type 'string)
596 ;;;; Tables
598 (defcustom org-latex-default-table-environment "tabular"
599 "Default environment used to build tables."
600 :group 'org-export-latex
601 :version "24.4"
602 :package-version '(Org . "8.0")
603 :type 'string)
605 (defcustom org-latex-default-table-mode 'table
606 "Default mode for tables.
608 Value can be a symbol among:
610 `table' Regular LaTeX table.
612 `math' In this mode, every cell is considered as being in math
613 mode and the complete table will be wrapped within a math
614 environment. It is particularly useful to write matrices.
616 `inline-math' This mode is almost the same as `math', but the
617 math environment will be inlined.
619 `verbatim' The table is exported as it appears in the Org
620 buffer, within a verbatim environment.
622 This value can be overridden locally with, i.e. \":mode math\" in
623 LaTeX attributes.
625 When modifying this variable, it may be useful to change
626 `org-latex-default-table-environment' accordingly."
627 :group 'org-export-latex
628 :version "24.4"
629 :package-version '(Org . "8.0")
630 :type '(choice (const :tag "Table" table)
631 (const :tag "Matrix" math)
632 (const :tag "Inline matrix" inline-math)
633 (const :tag "Verbatim" verbatim))
634 :safe (lambda (s) (memq s '(table math inline-math verbatim))))
636 (defcustom org-latex-tables-centered t
637 "When non-nil, tables are exported in a center environment."
638 :group 'org-export-latex
639 :type 'boolean
640 :safe #'booleanp)
642 (defcustom org-latex-tables-booktabs nil
643 "When non-nil, display tables in a formal \"booktabs\" style.
644 This option assumes that the \"booktabs\" package is properly
645 loaded in the header of the document. This value can be ignored
646 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
647 attributes."
648 :group 'org-export-latex
649 :version "24.4"
650 :package-version '(Org . "8.0")
651 :type 'boolean
652 :safe #'booleanp)
654 (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
655 "Format string to display numbers in scientific notation.
656 The format should have \"%s\" twice, for mantissa and exponent
657 \(i.e., \"%s\\\\times10^{%s}\").
659 When nil, no transformation is made."
660 :group 'org-export-latex
661 :version "24.4"
662 :package-version '(Org . "8.0")
663 :type '(choice
664 (string :tag "Format string")
665 (const :tag "No formatting" nil)))
667 ;;;; Text markup
669 (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
670 (code . verb)
671 (italic . "\\emph{%s}")
672 (strike-through . "\\sout{%s}")
673 (underline . "\\uline{%s}")
674 (verbatim . protectedtexttt))
675 "Alist of LaTeX expressions to convert text markup.
677 The key must be a symbol among `bold', `code', `italic',
678 `strike-through', `underline' and `verbatim'. The value is
679 a formatting string to wrap fontified text with.
681 Value can also be set to the following symbols: `verb' and
682 `protectedtexttt'. For the former, Org will use \"\\verb\" to
683 create a format string and select a delimiter character that
684 isn't in the string. For the latter, Org will use \"\\texttt\"
685 to typeset and try to protect special characters.
687 If no association can be found for a given markup, text will be
688 returned as-is."
689 :group 'org-export-latex
690 :type 'alist
691 :options '(bold code italic strike-through underline verbatim))
694 ;;;; Drawers
696 (defcustom org-latex-format-drawer-function
697 (lambda (name contents) contents)
698 "Function called to format a drawer in LaTeX code.
700 The function must accept two parameters:
701 NAME the drawer name, like \"LOGBOOK\"
702 CONTENTS the contents of the drawer.
704 The function should return the string to be exported.
706 The default function simply returns the value of CONTENTS."
707 :group 'org-export-latex
708 :version "24.4"
709 :package-version '(Org . "8.3")
710 :type 'function)
713 ;;;; Inlinetasks
715 (defcustom org-latex-format-inlinetask-function
716 'org-latex-format-inlinetask-default-function
717 "Function called to format an inlinetask in LaTeX code.
719 The function must accept seven parameters:
720 TODO the todo keyword (string or nil)
721 TODO-TYPE the todo type (symbol: `todo', `done', nil)
722 PRIORITY the inlinetask priority (integer or nil)
723 NAME the inlinetask name (string)
724 TAGS the inlinetask tags (list of strings or nil)
725 CONTENTS the contents of the inlinetask (string or nil)
726 INFO the export options (plist)
728 The function should return the string to be exported."
729 :group 'org-export-latex
730 :type 'function
731 :version "25.1"
732 :package-version '(Org . "8.3"))
735 ;; Src blocks
737 (defcustom org-latex-listings nil
738 "Non-nil means export source code using the listings package.
740 This package will fontify source code, possibly even with color.
741 If you want to use this, you also need to make LaTeX use the
742 listings package, and if you want to have color, the color
743 package. Just add these to `org-latex-packages-alist', for
744 example using customize, or with something like:
746 \(require 'ox-latex)
747 \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
748 \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
750 Alternatively,
752 \(setq org-latex-listings 'minted)
754 causes source code to be exported using the minted package as
755 opposed to listings. If you want to use minted, you need to add
756 the minted package to `org-latex-packages-alist', for example
757 using customize, or with
759 \(require 'ox-latex)
760 \(add-to-list 'org-latex-packages-alist '(\"\" \"minted\"))
762 In addition, it is necessary to install pygments
763 \(http://pygments.org), and to configure the variable
764 `org-latex-pdf-process' so that the -shell-escape option is
765 passed to pdflatex.
767 The minted choice has possible repercussions on the preview of
768 latex fragments (see `org-preview-latex-fragment'). If you run
769 into previewing problems, please consult
771 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
772 :group 'org-export-latex
773 :type '(choice
774 (const :tag "Use listings" t)
775 (const :tag "Use minted" minted)
776 (const :tag "Export verbatim" nil))
777 :safe (lambda (s) (memq s '(t nil minted))))
779 (defcustom org-latex-listings-langs
780 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
781 (c "C") (cc "C++")
782 (fortran "fortran")
783 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
784 (html "HTML") (xml "XML")
785 (tex "TeX") (latex "[LaTeX]TeX")
786 (shell-script "bash")
787 (gnuplot "Gnuplot")
788 (ocaml "Caml") (caml "Caml")
789 (sql "SQL") (sqlite "sql")
790 (makefile "make"))
791 "Alist mapping languages to their listing language counterpart.
792 The key is a symbol, the major mode symbol without the \"-mode\".
793 The value is the string that should be inserted as the language
794 parameter for the listings package. If the mode name and the
795 listings name are the same, the language does not need an entry
796 in this list - but it does not hurt if it is present."
797 :group 'org-export-latex
798 :version "24.4"
799 :package-version '(Org . "8.3")
800 :type '(repeat
801 (list
802 (symbol :tag "Major mode ")
803 (string :tag "Listings language"))))
805 (defcustom org-latex-listings-options nil
806 "Association list of options for the latex listings package.
808 These options are supplied as a comma-separated list to the
809 \\lstset command. Each element of the association list should be
810 a list containing two strings: the name of the option, and the
811 value. For example,
813 \(setq org-latex-listings-options
814 '((\"basicstyle\" \"\\\\small\")
815 \(\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
817 will typeset the code in a small size font with underlined, bold
818 black keywords.
820 Note that the same options will be applied to blocks of all
821 languages. If you need block-specific options, you may use the
822 following syntax:
824 #+ATTR_LATEX: :options key1=value1,key2=value2
825 #+BEGIN_SRC <LANG>
827 #+END_SRC"
828 :group 'org-export-latex
829 :type '(repeat
830 (list
831 (string :tag "Listings option name ")
832 (string :tag "Listings option value"))))
834 (defcustom org-latex-minted-langs
835 '((emacs-lisp "common-lisp")
836 (cc "c++")
837 (cperl "perl")
838 (shell-script "bash")
839 (caml "ocaml"))
840 "Alist mapping languages to their minted language counterpart.
841 The key is a symbol, the major mode symbol without the \"-mode\".
842 The value is the string that should be inserted as the language
843 parameter for the minted package. If the mode name and the
844 listings name are the same, the language does not need an entry
845 in this list - but it does not hurt if it is present.
847 Note that minted uses all lower case for language identifiers,
848 and that the full list of language identifiers can be obtained
849 with:
851 pygmentize -L lexers"
852 :group 'org-export-latex
853 :type '(repeat
854 (list
855 (symbol :tag "Major mode ")
856 (string :tag "Minted language"))))
858 (defcustom org-latex-minted-options nil
859 "Association list of options for the latex minted package.
861 These options are supplied within square brackets in
862 \\begin{minted} environments. Each element of the alist should
863 be a list containing two strings: the name of the option, and the
864 value. For example,
866 \(setq org-latex-minted-options
867 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
869 will result in src blocks being exported with
871 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
873 as the start of the minted environment. Note that the same
874 options will be applied to blocks of all languages. If you need
875 block-specific options, you may use the following syntax:
877 #+ATTR_LATEX: :options key1=value1,key2=value2
878 #+BEGIN_SRC <LANG>
880 #+END_SRC"
881 :group 'org-export-latex
882 :type '(repeat
883 (list
884 (string :tag "Minted option name ")
885 (string :tag "Minted option value"))))
887 (defvar org-latex-custom-lang-environments nil
888 "Alist mapping languages to language-specific LaTeX environments.
890 It is used during export of src blocks by the listings and minted
891 latex packages. For example,
893 \(setq org-latex-custom-lang-environments
894 '\(\(python \"pythoncode\"\)\)\)
896 would have the effect that if org encounters begin_src python
897 during latex export it will output
899 \\begin{pythoncode}
900 <src block body>
901 \\end{pythoncode}")
904 ;;;; Compilation
906 (defcustom org-latex-pdf-process
907 '("pdflatex -interaction nonstopmode -output-directory %o %f"
908 "pdflatex -interaction nonstopmode -output-directory %o %f"
909 "pdflatex -interaction nonstopmode -output-directory %o %f")
910 "Commands to process a LaTeX file to a PDF file.
911 This is a list of strings, each of them will be given to the
912 shell as a command. %f in the command will be replaced by the
913 full file name, %b by the file base name (i.e. without directory
914 and extension parts) and %o by the base directory of the file.
916 The reason why this is a list is that it usually takes several
917 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
918 does not have a clever mechanism to detect which of these
919 commands have to be run to get to a stable result, and it also
920 does not do any error checking.
922 By default, Org uses 3 runs of `pdflatex' to do the processing.
923 If you have texi2dvi on your system and if that does not cause
924 the infamous egrep/locale bug:
926 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
928 then `texi2dvi' is the superior choice as it automates the LaTeX
929 build process by calling the \"correct\" combinations of
930 auxiliary programs. Org does offer `texi2dvi' as one of the
931 customize options. Alternatively, `rubber' and `latexmk' also
932 provide similar functionality. The latter supports `biber' out
933 of the box.
935 Alternatively, this may be a Lisp function that does the
936 processing, so you could use this to apply the machinery of
937 AUCTeX or the Emacs LaTeX mode. This function should accept the
938 file name as its single argument."
939 :group 'org-export-pdf
940 :type '(choice
941 (repeat :tag "Shell command sequence"
942 (string :tag "Shell command"))
943 (const :tag "2 runs of pdflatex"
944 ("pdflatex -interaction nonstopmode -output-directory %o %f"
945 "pdflatex -interaction nonstopmode -output-directory %o %f"))
946 (const :tag "3 runs of pdflatex"
947 ("pdflatex -interaction nonstopmode -output-directory %o %f"
948 "pdflatex -interaction nonstopmode -output-directory %o %f"
949 "pdflatex -interaction nonstopmode -output-directory %o %f"))
950 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
951 ("pdflatex -interaction nonstopmode -output-directory %o %f"
952 "bibtex %b"
953 "pdflatex -interaction nonstopmode -output-directory %o %f"
954 "pdflatex -interaction nonstopmode -output-directory %o %f"))
955 (const :tag "2 runs of xelatex"
956 ("xelatex -interaction nonstopmode -output-directory %o %f"
957 "xelatex -interaction nonstopmode -output-directory %o %f"))
958 (const :tag "3 runs of xelatex"
959 ("xelatex -interaction nonstopmode -output-directory %o %f"
960 "xelatex -interaction nonstopmode -output-directory %o %f"
961 "xelatex -interaction nonstopmode -output-directory %o %f"))
962 (const :tag "xelatex,bibtex,xelatex,xelatex"
963 ("xelatex -interaction nonstopmode -output-directory %o %f"
964 "bibtex %b"
965 "xelatex -interaction nonstopmode -output-directory %o %f"
966 "xelatex -interaction nonstopmode -output-directory %o %f"))
967 (const :tag "texi2dvi"
968 ("texi2dvi -p -b -V %f"))
969 (const :tag "rubber"
970 ("rubber -d --into %o %f"))
971 (const :tag "latexmk"
972 ("latexmk -g -pdf %f"))
973 (function)))
975 (defcustom org-latex-logfiles-extensions
976 '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
977 "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
978 "The list of file extensions to consider as LaTeX logfiles.
979 The logfiles will be removed if `org-latex-remove-logfiles' is
980 non-nil."
981 :group 'org-export-latex
982 :version "25.1"
983 :package-version '(Org . "8.3")
984 :type '(repeat (string :tag "Extension")))
986 (defcustom org-latex-remove-logfiles t
987 "Non-nil means remove the logfiles produced by PDF production.
988 By default, logfiles are files with these extensions: .aux, .idx,
989 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
990 logfiles to remove, set `org-latex-logfiles-extensions'."
991 :group 'org-export-latex
992 :type 'boolean)
994 (defcustom org-latex-known-warnings
995 '(("Reference.*?undefined" . "[undefined reference]")
996 ("Runaway argument" . "[runaway argument]")
997 ("Underfull \\hbox" . "[underfull hbox]")
998 ("Overfull \\hbox" . "[overfull hbox]")
999 ("Citation.*?undefined" . "[undefined citation]")
1000 ("Undefined control sequence" . "[undefined control sequence]"))
1001 "Alist of regular expressions and associated messages for the user.
1002 The regular expressions are used to find possible warnings in the
1003 log of a latex-run. These warnings will be reported after
1004 calling `org-latex-compile'."
1005 :group 'org-export-latex
1006 :version "25.1"
1007 :package-version '(Org . "8.3")
1008 :type '(repeat
1009 (cons
1010 (string :tag "Regexp")
1011 (string :tag "Message"))))
1015 ;;; Internal Functions
1017 (defun org-latex--caption-above-p (element info)
1018 "Non nil when caption is expected to be located above ELEMENT.
1019 INFO is a plist holding contextual information."
1020 (let ((above (plist-get info :latex-caption-above)))
1021 (if (symbolp above) above
1022 (let ((type (org-element-type element)))
1023 (memq (if (eq type 'link) 'image type) above)))))
1025 (defun org-latex--caption/label-string (element info)
1026 "Return caption and label LaTeX string for ELEMENT.
1028 INFO is a plist holding contextual information. If there's no
1029 caption nor label, return the empty string.
1031 For non-floats, see `org-latex--wrap-label'."
1032 (let* ((label (org-element-property :name element))
1033 (label-str (if (not (org-string-nw-p label)) ""
1034 (format "\\label{%s}"
1035 (org-export-solidify-link-text label))))
1036 (main (org-export-get-caption element))
1037 (short (org-export-get-caption element t))
1038 (caption-from-attr-latex (org-export-read-attribute :attr_latex element :caption)))
1039 (cond
1040 ((org-string-nw-p caption-from-attr-latex)
1041 (concat caption-from-attr-latex "\n"))
1042 ((and (not main) (equal label-str "")) "")
1043 ((not main) (concat label-str "\n"))
1044 ;; Option caption format with short name.
1045 (short (format "\\caption[%s]{%s%s}\n"
1046 (org-export-data short info)
1047 label-str
1048 (org-export-data main info)))
1049 ;; Standard caption format.
1050 (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
1052 (defun org-latex-guess-inputenc (header)
1053 "Set the coding system in inputenc to what the buffer is.
1055 HEADER is the LaTeX header string. This function only applies
1056 when specified inputenc option is \"AUTO\".
1058 Return the new header, as a string."
1059 (let* ((cs (or (ignore-errors
1060 (latexenc-coding-system-to-inputenc
1061 (or org-export-coding-system buffer-file-coding-system)))
1062 "utf8")))
1063 (if (not cs) header
1064 ;; First translate if that is requested.
1065 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
1066 ;; Then find the \usepackage statement and replace the option.
1067 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1068 cs header t nil 1))))
1070 (defun org-latex-guess-babel-language (header info)
1071 "Set Babel's language according to LANGUAGE keyword.
1073 HEADER is the LaTeX header string. INFO is the plist used as
1074 a communication channel.
1076 Insertion of guessed language only happens when Babel package has
1077 explicitly been loaded. Then it is added to the rest of
1078 package's options.
1080 The argument to Babel may be \"AUTO\" which is then replaced with
1081 the language of the document or `org-export-default-language'
1082 unless language in question is already loaded.
1084 Return the new header."
1085 (let ((language-code (plist-get info :language)))
1086 ;; If no language is set or Babel package is not loaded, return
1087 ;; HEADER as-is.
1088 (if (or (not (stringp language-code))
1089 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
1090 header
1091 (let ((options (save-match-data
1092 (org-split-string (match-string 1 header) ",[ \t]*")))
1093 (language (cdr (assoc language-code
1094 org-latex-babel-language-alist))))
1095 ;; If LANGUAGE is already loaded, return header without AUTO.
1096 ;; Otherwise, replace AUTO with language or append language if
1097 ;; AUTO is not present.
1098 (replace-match
1099 (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
1100 (cond ((member language options) (delete "AUTO" options))
1101 ((member "AUTO" options) options)
1102 (t (append options (list language))))
1103 ", ")
1104 t nil header 1)))))
1106 (defun org-latex--find-verb-separator (s)
1107 "Return a character not used in string S.
1108 This is used to choose a separator for constructs like \\verb."
1109 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1110 (loop for c across ll
1111 when (not (string-match (regexp-quote (char-to-string c)) s))
1112 return (char-to-string c))))
1114 (defun org-latex--make-option-string (options)
1115 "Return a comma separated string of keywords and values.
1116 OPTIONS is an alist where the key is the options keyword as
1117 a string, and the value a list containing the keyword value, or
1118 nil."
1119 (mapconcat (lambda (pair)
1120 (concat (first pair)
1121 (when (> (length (second pair)) 0)
1122 (concat "=" (second pair)))))
1123 options
1124 ","))
1126 (defun org-latex--wrap-label (element output)
1127 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1128 This function shouldn't be used for floats. See
1129 `org-latex--caption/label-string'."
1130 (let ((label (org-element-property :name element)))
1131 (if (not (and (org-string-nw-p output) (org-string-nw-p label))) output
1132 (concat (format "\\phantomsection\n\\label{%s}\n"
1133 (org-export-solidify-link-text label))
1134 output))))
1136 (defun org-latex--text-markup (text markup info)
1137 "Format TEXT depending on MARKUP text markup.
1138 INFO is a plist used as a communication channel. See
1139 `org-latex-text-markup-alist' for details."
1140 (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
1141 (cond
1142 ;; No format string: Return raw text.
1143 ((not fmt) text)
1144 ;; Handle the `verb' special case: Find an appropriate separator
1145 ;; and use "\\verb" command.
1146 ((eq 'verb fmt)
1147 (let ((separator (org-latex--find-verb-separator text)))
1148 (concat "\\verb" separator
1149 (replace-regexp-in-string "\n" " " text)
1150 separator)))
1151 ;; Handle the `protectedtexttt' special case: Protect some
1152 ;; special chars and use "\texttt{%s}" format string.
1153 ((eq 'protectedtexttt fmt)
1154 (let ((start 0)
1155 (trans '(("\\" . "\\textbackslash{}")
1156 ("~" . "\\textasciitilde{}")
1157 ("^" . "\\textasciicircum{}")))
1158 (rtn "")
1159 char)
1160 (while (string-match "[\\{}$%&_#~^]" text)
1161 (setq char (match-string 0 text))
1162 (if (> (match-beginning 0) 0)
1163 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
1164 (setq text (substring text (1+ (match-beginning 0))))
1165 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1166 rtn (concat rtn char)))
1167 (setq text (concat rtn text)
1168 fmt "\\texttt{%s}")
1169 (while (string-match "--" text)
1170 (setq text (replace-match "-{}-" t t text)))
1171 (format fmt text)))
1172 ;; Else use format string.
1173 (t (format fmt text)))))
1175 (defun org-latex--delayed-footnotes-definitions (element info)
1176 "Return footnotes definitions in ELEMENT as a string.
1178 INFO is a plist used as a communication channel.
1180 Footnotes definitions are returned within \"\\footnotetxt{}\"
1181 commands.
1183 This function is used within constructs that don't support
1184 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1185 \"\\footnotemark\" is used within the construct and the function
1186 just outside of it."
1187 (mapconcat
1188 (lambda (ref)
1189 (format
1190 "\\footnotetext[%s]{%s}"
1191 (org-export-get-footnote-number ref info)
1192 (org-trim
1193 (org-export-data
1194 (org-export-get-footnote-definition ref info) info))))
1195 ;; Find every footnote reference in ELEMENT.
1196 (let* (all-refs
1197 search-refs ; For byte-compiler.
1198 (search-refs
1199 (function
1200 (lambda (data)
1201 ;; Return a list of all footnote references never seen
1202 ;; before in DATA.
1203 (org-element-map data 'footnote-reference
1204 (lambda (ref)
1205 (when (org-export-footnote-first-reference-p ref info)
1206 (push ref all-refs)
1207 (when (eq (org-element-property :type ref) 'standard)
1208 (funcall search-refs
1209 (org-export-get-footnote-definition ref info)))))
1210 info)
1211 (reverse all-refs)))))
1212 (funcall search-refs element))
1213 ""))
1215 (defun org-latex--translate (s info)
1216 "Translate string S according to specified language.
1217 INFO is a plist used as a communication channel."
1218 (org-export-translate s :latex info))
1220 (defun org-latex--format-spec (info)
1221 "Create a format-spec for document meta-data.
1222 INFO is a plist used as a communication channel."
1223 (let ((objects '(bold code entity export-snippet inline-babel-call
1224 inline-src-block italic latex-fragment
1225 latex-math-block link macro strike-through
1226 subscript superscript timestamp underline
1227 verbatim))
1228 (language (let ((lang (plist-get info :language)))
1229 (or (cdr (assoc lang org-latex-babel-language-alist))
1230 lang))))
1231 `((?a . ,(org-export-data (plist-get info :author) info))
1232 (?t . ,(org-export-data (plist-get info :title) info))
1233 (?k . ,(org-export-data (org-latex--wrap-latex-math-block
1234 (org-element-parse-secondary-string
1235 (plist-get info :keywords) objects)
1236 info)
1237 info))
1238 (?d . ,(org-export-data (org-latex--wrap-latex-math-block
1239 (org-element-parse-secondary-string
1240 (plist-get info :description) objects)
1241 info)
1242 info))
1243 (?c . ,(plist-get info :creator))
1244 (?l . ,language)
1245 (?L . ,(capitalize language))
1246 (?D . ,(org-export-get-date info)))))
1249 ;;; Template
1251 (defun org-latex-template (contents info)
1252 "Return complete document string after LaTeX conversion.
1253 CONTENTS is the transcoded contents string. INFO is a plist
1254 holding export options."
1255 (let ((title (org-export-data (plist-get info :title) info))
1256 (spec (org-latex--format-spec info)))
1257 (concat
1258 ;; Time-stamp.
1259 (and (plist-get info :time-stamp-file)
1260 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1261 ;; Document class and packages.
1262 (let* ((class (plist-get info :latex-class))
1263 (class-options (plist-get info :latex-class-options))
1264 (header (nth 1 (assoc class (plist-get info :latex-classes))))
1265 (document-class-string
1266 (and (stringp header)
1267 (if (not class-options) header
1268 (replace-regexp-in-string
1269 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1270 class-options header t nil 1)))))
1271 (if (not document-class-string)
1272 (user-error "Unknown LaTeX class `%s'" class)
1273 (org-latex-guess-babel-language
1274 (org-latex-guess-inputenc
1275 (org-element-normalize-string
1276 (org-splice-latex-header
1277 document-class-string
1278 org-latex-default-packages-alist
1279 org-latex-packages-alist nil
1280 (concat (org-element-normalize-string
1281 (plist-get info :latex-header))
1282 (plist-get info :latex-header-extra)))))
1283 info)))
1284 ;; Possibly limit depth for headline numbering.
1285 (let ((sec-num (plist-get info :section-numbers)))
1286 (when (integerp sec-num)
1287 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1288 ;; Author.
1289 (let ((author (and (plist-get info :with-author)
1290 (let ((auth (plist-get info :author)))
1291 (and auth (org-export-data auth info)))))
1292 (email (and (plist-get info :with-email)
1293 (org-export-data (plist-get info :email) info))))
1294 (cond ((and author email (not (string= "" email)))
1295 (format "\\author{%s\\thanks{%s}}\n" author email))
1296 ((or author email) (format "\\author{%s}\n" (or author email)))))
1297 ;; Date.
1298 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1299 (format "\\date{%s}\n" (org-export-data date info)))
1300 ;; Title
1301 (format "\\title{%s}\n" title)
1302 ;; Hyperref options.
1303 (let ((template (plist-get info :latex-hyperref-template)))
1304 (and (stringp template)
1305 (format-spec template spec)))
1306 ;; Document start.
1307 "\\begin{document}\n\n"
1308 ;; Title command.
1309 (let* ((title-command (plist-get info :latex-title-command))
1310 (command (and (stringp title-command)
1311 (format-spec title-command spec))))
1312 (org-element-normalize-string
1313 (cond ((not (plist-get info :with-title)) nil)
1314 ((string= "" title) nil)
1315 ((not (stringp command)) nil)
1316 ((string-match "\\(?:[^%]\\|^\\)%s" command)
1317 (format command title))
1318 (t command))))
1319 ;; Table of contents.
1320 (let ((depth (plist-get info :with-toc)))
1321 (when depth
1322 (concat (when (wholenump depth)
1323 (format "\\setcounter{tocdepth}{%d}\n" depth))
1324 (plist-get info :latex-toc-command))))
1325 ;; Document's body.
1326 contents
1327 ;; Creator.
1328 (and (plist-get info :with-creator)
1329 (concat (plist-get info :creator) "\n"))
1330 ;; Document end.
1331 "\\end{document}")))
1335 ;;; Transcode Functions
1337 ;;;; Bold
1339 (defun org-latex-bold (bold contents info)
1340 "Transcode BOLD from Org to LaTeX.
1341 CONTENTS is the text with bold markup. INFO is a plist holding
1342 contextual information."
1343 (org-latex--text-markup contents 'bold info))
1346 ;;;; Center Block
1348 (defun org-latex-center-block (center-block contents info)
1349 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1350 CONTENTS holds the contents of the center block. INFO is a plist
1351 holding contextual information."
1352 (org-latex--wrap-label
1353 center-block
1354 (format "\\begin{center}\n%s\\end{center}" contents)))
1357 ;;;; Clock
1359 (defun org-latex-clock (clock contents info)
1360 "Transcode a CLOCK element from Org to LaTeX.
1361 CONTENTS is nil. INFO is a plist holding contextual
1362 information."
1363 (concat
1364 "\\noindent"
1365 (format "\\textbf{%s} " org-clock-string)
1366 (format (plist-get info :latex-inactive-timestamp-format)
1367 (concat (org-timestamp-translate (org-element-property :value clock))
1368 (let ((time (org-element-property :duration clock)))
1369 (and time (format " (%s)" time)))))
1370 "\\\\"))
1373 ;;;; Code
1375 (defun org-latex-code (code contents info)
1376 "Transcode a CODE object from Org to LaTeX.
1377 CONTENTS is nil. INFO is a plist used as a communication
1378 channel."
1379 (org-latex--text-markup (org-element-property :value code) 'code info))
1382 ;;;; Drawer
1384 (defun org-latex-drawer (drawer contents info)
1385 "Transcode a DRAWER element from Org to LaTeX.
1386 CONTENTS holds the contents of the block. INFO is a plist
1387 holding contextual information."
1388 (let* ((name (org-element-property :drawer-name drawer))
1389 (output (funcall (plist-get info :latex-format-drawer-function)
1390 name contents)))
1391 (org-latex--wrap-label drawer output)))
1394 ;;;; Dynamic Block
1396 (defun org-latex-dynamic-block (dynamic-block contents info)
1397 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1398 CONTENTS holds the contents of the block. INFO is a plist
1399 holding contextual information. See `org-export-data'."
1400 (org-latex--wrap-label dynamic-block contents))
1403 ;;;; Entity
1405 (defun org-latex-entity (entity contents info)
1406 "Transcode an ENTITY object from Org to LaTeX.
1407 CONTENTS are the definition itself. INFO is a plist holding
1408 contextual information."
1409 (org-element-property :latex entity))
1412 ;;;; Example Block
1414 (defun org-latex-example-block (example-block contents info)
1415 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1416 CONTENTS is nil. INFO is a plist holding contextual
1417 information."
1418 (when (org-string-nw-p (org-element-property :value example-block))
1419 (org-latex--wrap-label
1420 example-block
1421 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1422 (org-export-format-code-default example-block info)))))
1425 ;;;; Export Block
1427 (defun org-latex-export-block (export-block contents info)
1428 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1429 CONTENTS is nil. INFO is a plist holding contextual information."
1430 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1431 (org-remove-indentation (org-element-property :value export-block))))
1434 ;;;; Export Snippet
1436 (defun org-latex-export-snippet (export-snippet contents info)
1437 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1438 CONTENTS is nil. INFO is a plist holding contextual information."
1439 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1440 (org-element-property :value export-snippet)))
1443 ;;;; Fixed Width
1445 (defun org-latex-fixed-width (fixed-width contents info)
1446 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1447 CONTENTS is nil. INFO is a plist holding contextual information."
1448 (org-latex--wrap-label
1449 fixed-width
1450 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1451 (org-remove-indentation
1452 (org-element-property :value fixed-width)))))
1455 ;;;; Footnote Reference
1457 (defun org-latex-footnote-reference (footnote-reference contents info)
1458 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1459 CONTENTS is nil. INFO is a plist holding contextual information."
1460 (concat
1461 ;; Insert separator between two footnotes in a row.
1462 (let ((prev (org-export-get-previous-element footnote-reference info)))
1463 (when (eq (org-element-type prev) 'footnote-reference)
1464 (plist-get info :latex-footnote-separator)))
1465 (cond
1466 ;; Use \footnotemark if the footnote has already been defined.
1467 ((not (org-export-footnote-first-reference-p footnote-reference info))
1468 (format "\\footnotemark[%s]{}"
1469 (org-export-get-footnote-number footnote-reference info)))
1470 ;; Use \footnotemark if reference is within another footnote
1471 ;; reference, footnote definition or table cell.
1472 ((org-element-lineage footnote-reference
1473 '(footnote-reference footnote-definition table-cell))
1474 "\\footnotemark")
1475 ;; Otherwise, define it with \footnote command.
1477 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1478 (concat
1479 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1480 ;; Retrieve all footnote references within the footnote and
1481 ;; add their definition after it, since LaTeX doesn't support
1482 ;; them inside.
1483 (org-latex--delayed-footnotes-definitions def info)))))))
1486 ;;;; Headline
1488 (defun org-latex-headline (headline contents info)
1489 "Transcode a HEADLINE element from Org to LaTeX.
1490 CONTENTS holds the contents of the headline. INFO is a plist
1491 holding contextual information."
1492 (unless (org-element-property :footnote-section-p headline)
1493 (let* ((class (plist-get info :latex-class))
1494 (level (org-export-get-relative-level headline info))
1495 (numberedp (org-export-numbered-headline-p headline info))
1496 (class-sectioning (assoc class (plist-get info :latex-classes)))
1497 ;; Section formatting will set two placeholders: one for
1498 ;; the title and the other for the contents.
1499 (section-fmt
1500 (let ((sec (if (functionp (nth 2 class-sectioning))
1501 (funcall (nth 2 class-sectioning) level numberedp)
1502 (nth (1+ level) class-sectioning))))
1503 (cond
1504 ;; No section available for that LEVEL.
1505 ((not sec) nil)
1506 ;; Section format directly returned by a function. Add
1507 ;; placeholder for contents.
1508 ((stringp sec) (concat sec "\n%s"))
1509 ;; (numbered-section . unnumbered-section)
1510 ((not (consp (cdr sec)))
1511 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1512 ;; (numbered-open numbered-close)
1513 ((= (length sec) 2)
1514 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1515 ;; (num-in num-out no-num-in no-num-out)
1516 ((= (length sec) 4)
1517 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1518 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1519 ;; Create a temporary export back-end that hard-codes
1520 ;; "\underline" within "\section" and alike.
1521 (section-back-end
1522 (org-export-create-backend
1523 :parent 'latex
1524 :transcoders
1525 '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
1526 (text
1527 (org-export-data-with-backend
1528 (org-element-property :title headline) section-back-end info))
1529 (todo
1530 (and (plist-get info :with-todo-keywords)
1531 (let ((todo (org-element-property :todo-keyword headline)))
1532 (and todo (org-export-data todo info)))))
1533 (todo-type (and todo (org-element-property :todo-type headline)))
1534 (tags (and (plist-get info :with-tags)
1535 (org-export-get-tags headline info)))
1536 (priority (and (plist-get info :with-priority)
1537 (org-element-property :priority headline)))
1538 ;; Create the headline text along with a no-tag version.
1539 ;; The latter is required to remove tags from toc.
1540 (full-text (funcall (plist-get info :latex-format-headline-function)
1541 todo todo-type priority text tags info))
1542 ;; Associate \label to the headline for internal links.
1543 (headline-label
1544 (format "\\label{%s}\n"
1545 (or (and (plist-get info :latex-custom-id-labels)
1546 (org-element-property :CUSTOM_ID headline))
1547 (org-export-get-headline-id headline info))))
1548 (pre-blanks
1549 (make-string (org-element-property :pre-blank headline) 10)))
1550 (if (or (not section-fmt) (org-export-low-level-p headline info))
1551 ;; This is a deep sub-tree: export it as a list item. Also
1552 ;; export as items headlines for which no section format has
1553 ;; been found.
1554 (let ((low-level-body
1555 (concat
1556 ;; If headline is the first sibling, start a list.
1557 (when (org-export-first-sibling-p headline info)
1558 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1559 ;; Itemize headline
1560 "\\item"
1561 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
1562 "\\relax")
1563 " " full-text "\n"
1564 headline-label
1565 pre-blanks
1566 contents)))
1567 ;; If headline is not the last sibling simply return
1568 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1569 ;; any blank line.
1570 (if (not (org-export-last-sibling-p headline info)) low-level-body
1571 (replace-regexp-in-string
1572 "[ \t\n]*\\'"
1573 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1574 low-level-body)))
1575 ;; This is a standard headline. Export it as a section. Add
1576 ;; an alternative heading when possible, and when this is not
1577 ;; identical to the usual heading.
1578 (let ((opt-title
1579 (funcall (plist-get info :latex-format-headline-function)
1580 todo todo-type priority
1581 (org-export-data-with-backend
1582 (org-export-get-alt-title headline info)
1583 section-back-end info)
1584 (and (eq (plist-get info :with-tags) t) tags)
1585 info))
1586 ;; Maybe end local TOC (see `org-latex-keyword').
1587 (contents
1588 (concat
1589 contents
1590 (let ((case-fold-search t)
1591 (section
1592 (let ((first (car (org-element-contents headline))))
1593 (and (eq (org-element-type first) 'section) first))))
1594 (org-element-map section 'keyword
1595 (lambda (k)
1596 (and (equal (org-element-property :key k) "TOC")
1597 (let ((v (org-element-property :value k)))
1598 (and (org-string-match-p "\\<headlines\\>" v)
1599 (org-string-match-p "\\<local\\>" v)
1600 (format "\\stopcontents[level-%d]" level)))))
1601 info t)))))
1602 (if (and numberedp opt-title
1603 (not (equal opt-title full-text))
1604 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
1605 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1606 ;; Replace square brackets with parenthesis
1607 ;; since square brackets are not supported in
1608 ;; optional arguments.
1609 (replace-regexp-in-string
1610 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1611 full-text
1612 (concat headline-label pre-blanks contents))
1613 ;; Impossible to add an alternative heading. Fallback to
1614 ;; regular sectioning format string.
1615 (format section-fmt full-text
1616 (concat headline-label pre-blanks contents))))))))
1618 (defun org-latex-format-headline-default-function
1619 (todo todo-type priority text tags info)
1620 "Default format function for a headline.
1621 See `org-latex-format-headline-function' for details."
1622 (concat
1623 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1624 (and priority (format "\\framebox{\\#%c} " priority))
1625 text
1626 (and tags
1627 (format "\\hfill{}\\textsc{%s}"
1628 (mapconcat (lambda (tag) (org-latex-plain-text tag info))
1629 tags ":")))))
1632 ;;;; Horizontal Rule
1634 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1635 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1636 CONTENTS is nil. INFO is a plist holding contextual information."
1637 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1638 (prev (org-export-get-previous-element horizontal-rule info)))
1639 (concat
1640 ;; Make sure the rule doesn't start at the end of the current
1641 ;; line by separating it with a blank line from previous element.
1642 (when (and prev
1643 (let ((prev-blank (org-element-property :post-blank prev)))
1644 (or (not prev-blank) (zerop prev-blank))))
1645 "\n")
1646 (org-latex--wrap-label
1647 horizontal-rule
1648 (format "\\rule{%s}{%s}"
1649 (or (plist-get attr :width) "\\linewidth")
1650 (or (plist-get attr :thickness) "0.5pt"))))))
1653 ;;;; Inline Src Block
1655 (defun org-latex-inline-src-block (inline-src-block contents info)
1656 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1657 CONTENTS holds the contents of the item. INFO is a plist holding
1658 contextual information."
1659 (let* ((code (org-element-property :value inline-src-block))
1660 (separator (org-latex--find-verb-separator code)))
1661 (case (plist-get info :latex-listings)
1662 ;; Do not use a special package: transcode it verbatim.
1663 ((nil) (concat "\\verb" separator code separator))
1664 ;; Use minted package.
1665 (minted
1666 (let* ((org-lang (org-element-property :language inline-src-block))
1667 (mint-lang (or (cadr (assq (intern org-lang)
1668 (plist-get info :latex-minted-langs)))
1669 (downcase org-lang)))
1670 (options (org-latex--make-option-string
1671 (plist-get info :latex-minted-options))))
1672 (concat (format "\\mint%s{%s}"
1673 (if (string= options "") "" (format "[%s]" options))
1674 mint-lang)
1675 separator code separator)))
1676 ;; Use listings package.
1677 (otherwise
1678 ;; Maybe translate language's name.
1679 (let* ((org-lang (org-element-property :language inline-src-block))
1680 (lst-lang (or (cadr (assq (intern org-lang)
1681 (plist-get info :latex-listings-langs)))
1682 org-lang))
1683 (options (org-latex--make-option-string
1684 (append (plist-get info :latex-listings-options)
1685 `(("language" ,lst-lang))))))
1686 (concat (format "\\lstinline[%s]" options)
1687 separator code separator))))))
1690 ;;;; Inlinetask
1692 (defun org-latex-inlinetask (inlinetask contents info)
1693 "Transcode an INLINETASK element from Org to LaTeX.
1694 CONTENTS holds the contents of the block. INFO is a plist
1695 holding contextual information."
1696 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1697 (todo (and (plist-get info :with-todo-keywords)
1698 (let ((todo (org-element-property :todo-keyword inlinetask)))
1699 (and todo (org-export-data todo info)))))
1700 (todo-type (org-element-property :todo-type inlinetask))
1701 (tags (and (plist-get info :with-tags)
1702 (org-export-get-tags inlinetask info)))
1703 (priority (and (plist-get info :with-priority)
1704 (org-element-property :priority inlinetask)))
1705 (contents (concat
1706 (let ((label (org-element-property :CUSTOM_ID inlinetask)))
1707 (and label (format "\\label{%s}\n" label)))
1708 contents)))
1709 (funcall (plist-get info :latex-format-inlinetask-function)
1710 todo todo-type priority title tags contents info)))
1712 (defun org-latex-format-inlinetask-default-function
1713 (todo todo-type priority title tags contents info)
1714 "Default format function for a inlinetasks.
1715 See `org-latex-format-inlinetask-function' for details."
1716 (let ((full-title
1717 (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1718 (when priority (format "\\framebox{\\#%c} " priority))
1719 title
1720 (when tags
1721 (format "\\hfill{}\\textsc{:%s:}"
1722 (mapconcat
1723 (lambda (tag) (org-latex-plain-text tag info))
1724 tags ":"))))))
1725 (concat "\\begin{center}\n"
1726 "\\fbox{\n"
1727 "\\begin{minipage}[c]{.6\\textwidth}\n"
1728 full-title "\n\n"
1729 (and (org-string-nw-p contents)
1730 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
1731 "\\end{minipage}\n"
1732 "}\n"
1733 "\\end{center}")))
1736 ;;;; Italic
1738 (defun org-latex-italic (italic contents info)
1739 "Transcode ITALIC from Org to LaTeX.
1740 CONTENTS is the text with italic markup. INFO is a plist holding
1741 contextual information."
1742 (org-latex--text-markup contents 'italic info))
1745 ;;;; Item
1747 (defun org-latex-item (item contents info)
1748 "Transcode an ITEM element from Org to LaTeX.
1749 CONTENTS holds the contents of the item. INFO is a plist holding
1750 contextual information."
1751 (let* ((counter
1752 (let ((count (org-element-property :counter item))
1753 (level
1754 ;; Determine level of current item to determine the
1755 ;; correct LaTeX counter to use (enumi, enumii...).
1756 (let ((parent item) (level 0))
1757 (while (memq (org-element-type
1758 (setq parent (org-export-get-parent parent)))
1759 '(plain-list item))
1760 (when (and (eq (org-element-type parent) 'plain-list)
1761 (eq (org-element-property :type parent)
1762 'ordered))
1763 (incf level)))
1764 level)))
1765 (and count
1766 (< level 5)
1767 (format "\\setcounter{enum%s}{%s}\n"
1768 (nth (1- level) '("i" "ii" "iii" "iv"))
1769 (1- count)))))
1770 (checkbox (case (org-element-property :checkbox item)
1771 (on "$\\boxtimes$ ")
1772 (off "$\\square$ ")
1773 (trans "$\\boxminus$ ")))
1774 (tag (let ((tag (org-element-property :tag item)))
1775 ;; Check-boxes must belong to the tag.
1776 (and tag (format "[{%s}] "
1777 (concat checkbox
1778 (org-export-data tag info)))))))
1779 (concat counter
1780 "\\item"
1781 (cond
1782 (tag)
1783 (checkbox (concat " " checkbox))
1784 ;; Without a tag or a check-box, if CONTENTS starts with
1785 ;; an opening square bracket, add "\relax" to "\item",
1786 ;; unless the brackets comes from an initial export
1787 ;; snippet (i.e. it is inserted willingly by the user).
1788 ((and contents
1789 (org-string-match-p "\\`[ \t]*\\[" contents)
1790 (not (let ((e (car (org-element-contents item))))
1791 (and (eq (org-element-type e) 'paragraph)
1792 (let ((o (car (org-element-contents e))))
1793 (and (eq (org-element-type o) 'export-snippet)
1794 (eq (org-export-snippet-backend o)
1795 'latex)))))))
1796 "\\relax ")
1797 (t " "))
1798 (and contents (org-trim contents))
1799 ;; If there are footnotes references in tag, be sure to
1800 ;; add their definition at the end of the item. This
1801 ;; workaround is necessary since "\footnote{}" command is
1802 ;; not supported in tags.
1803 (and tag
1804 (org-latex--delayed-footnotes-definitions
1805 (org-element-property :tag item) info)))))
1808 ;;;; Keyword
1810 (defun org-latex-keyword (keyword contents info)
1811 "Transcode a KEYWORD element from Org to LaTeX.
1812 CONTENTS is nil. INFO is a plist holding contextual information."
1813 (let ((key (org-element-property :key keyword))
1814 (value (org-element-property :value keyword)))
1815 (cond
1816 ((string= key "LATEX") value)
1817 ((string= key "INDEX") (format "\\index{%s}" value))
1818 ((string= key "TOC")
1819 (let ((case-fold-search t))
1820 (cond
1821 ((org-string-match-p "\\<headlines\\>" value)
1822 (let* ((localp (org-string-match-p "\\<local\\>" value))
1823 (parent (org-element-lineage keyword '(headline)))
1824 (level (if (not (and localp parent)) 0
1825 (org-export-get-relative-level parent info)))
1826 (depth
1827 (and (string-match "\\<[0-9]+\\>" value)
1828 (format
1829 "\\setcounter{tocdepth}{%d}"
1830 (+ (string-to-number (match-string 0 value)) level)))))
1831 (if (and localp parent)
1832 ;; Start local TOC, assuming package "titletoc" is
1833 ;; required.
1834 (format "\\startcontents[level-%d]
1835 \\printcontents[level-%d]{}{0}{%s}"
1836 level level (or depth ""))
1837 (concat depth (and depth "\n") "\\tableofcontents"))))
1838 ((org-string-match-p "\\<tables\\>" value) "\\listoftables")
1839 ((org-string-match-p "\\<listings\\>" value)
1840 (case (plist-get info :latex-listings)
1841 ((nil) "\\listoffigures")
1842 (minted "\\listoflistings")
1843 (otherwise "\\lstlistoflistings")))))))))
1846 ;;;; Latex Environment
1848 (defun org-latex-latex-environment (latex-environment contents info)
1849 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1850 CONTENTS is nil. INFO is a plist holding contextual information."
1851 (when (plist-get info :with-latex)
1852 (let ((label (org-element-property :name latex-environment))
1853 (value (org-remove-indentation
1854 (org-element-property :value latex-environment))))
1855 (if (not (org-string-nw-p label)) value
1856 ;; Environment is labeled: label must be within the environment
1857 ;; (otherwise, a reference pointing to that element will count
1858 ;; the section instead).
1859 (with-temp-buffer
1860 (insert value)
1861 (goto-char (point-min))
1862 (forward-line)
1863 (insert
1864 (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1865 (buffer-string))))))
1868 ;;;; Latex Fragment
1870 (defun org-latex-latex-fragment (latex-fragment contents info)
1871 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1872 CONTENTS is nil. INFO is a plist holding contextual information."
1873 (let ((value (org-element-property :value latex-fragment)))
1874 ;; Trim math markers since the fragment is enclosed within
1875 ;; a latex-math-block object anyway.
1876 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
1877 (match-string 2 value))
1878 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
1879 (match-string 1 value))
1880 (t value))))
1883 ;;;; Line Break
1885 (defun org-latex-line-break (line-break contents info)
1886 "Transcode a LINE-BREAK object from Org to LaTeX.
1887 CONTENTS is nil. INFO is a plist holding contextual information."
1888 "\\\\\n")
1891 ;;;; Link
1893 (defun org-latex--inline-image (link info)
1894 "Return LaTeX code for an inline image.
1895 LINK is the link pointing to the inline image. INFO is a plist
1896 used as a communication channel."
1897 (let* ((parent (org-export-get-parent-element link))
1898 (path (let ((raw-path (org-element-property :path link)))
1899 (if (not (file-name-absolute-p raw-path)) raw-path
1900 (expand-file-name raw-path))))
1901 (filetype (file-name-extension path))
1902 (caption (org-latex--caption/label-string parent info))
1903 (caption-above-p (org-latex--caption-above-p link info))
1904 ;; Retrieve latex attributes from the element around.
1905 (attr (org-export-read-attribute :attr_latex parent))
1906 (float (let ((float (plist-get attr :float)))
1907 (cond ((and (not float) (plist-member attr :float)) nil)
1908 ((string= float "wrap") 'wrap)
1909 ((string= float "sideways") 'sideways)
1910 ((string= float "multicolumn") 'multicolumn)
1911 ((or float
1912 (org-element-property :caption parent)
1913 (org-string-nw-p (plist-get attr :caption)))
1914 'figure))))
1915 (placement
1916 (let ((place (plist-get attr :placement)))
1917 (cond
1918 (place (format "%s" place))
1919 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1920 ((eq float 'figure)
1921 (format "[%s]" (plist-get info :latex-default-figure-position)))
1922 (t ""))))
1923 (comment-include (if (plist-get attr :comment-include) "%" ""))
1924 ;; It is possible to specify width and height in the
1925 ;; ATTR_LATEX line, and also via default variables.
1926 (width (cond ((plist-get attr :width))
1927 ((plist-get attr :height) "")
1928 ((eq float 'wrap) "0.48\\textwidth")
1929 (t (plist-get info :latex-image-default-width))))
1930 (height (cond ((plist-get attr :height))
1931 ((or (plist-get attr :width)
1932 (memq float '(figure wrap))) "")
1933 (t (plist-get info :latex-image-default-height))))
1934 (options (let ((opt (or (plist-get attr :options)
1935 (plist-get info :latex-image-default-option))))
1936 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
1937 (match-string 1 opt))))
1938 image-code)
1939 (if (member filetype '("tikz" "pgf"))
1940 ;; For tikz images:
1941 ;; - use \input to read in image file.
1942 ;; - if options are present, wrap in a tikzpicture environment.
1943 ;; - if width or height are present, use \resizebox to change
1944 ;; the image size.
1945 (progn
1946 (setq image-code (format "\\input{%s}" path))
1947 (when (org-string-nw-p options)
1948 (setq image-code
1949 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1950 options
1951 image-code)))
1952 (when (or (org-string-nw-p width) (org-string-nw-p height))
1953 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
1954 (if (org-string-nw-p width) width "!")
1955 (if (org-string-nw-p height) height "!")
1956 image-code))))
1957 ;; For other images:
1958 ;; - add width and height to options.
1959 ;; - include the image with \includegraphics.
1960 (when (org-string-nw-p width)
1961 (setq options (concat options ",width=" width)))
1962 (when (org-string-nw-p height)
1963 (setq options (concat options ",height=" height)))
1964 (let ((search-option (org-element-property :search-option link)))
1965 (when (and search-option
1966 (equal filetype "pdf")
1967 (org-string-match-p "\\`[0-9]+\\'" search-option)
1968 (not (org-string-match-p "page=" options)))
1969 (setq options (concat options ",page=" search-option))))
1970 (setq image-code
1971 (format "\\includegraphics%s{%s}"
1972 (cond ((not (org-string-nw-p options)) "")
1973 ((= (aref options 0) ?,)
1974 (format "[%s]"(substring options 1)))
1975 (t (format "[%s]" options)))
1976 path))
1977 (when (equal filetype "svg")
1978 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
1979 "\\includesvg"
1980 image-code
1981 nil t))
1982 (setq image-code (replace-regexp-in-string "\\.svg}"
1984 image-code
1985 nil t))))
1986 ;; Return proper string, depending on FLOAT.
1987 (case float
1988 (wrap (format "\\begin{wrapfigure}%s
1989 %s\\centering
1990 %s%s
1991 %s\\end{wrapfigure}"
1992 placement
1993 (if caption-above-p caption "")
1994 comment-include image-code
1995 (if caption-above-p "" caption)))
1996 (sideways (format "\\begin{sidewaysfigure}
1997 %s\\centering
1998 %s%s
1999 %s\\end{sidewaysfigure}"
2000 (if caption-above-p caption "")
2001 comment-include image-code
2002 (if caption-above-p "" caption)))
2003 (multicolumn (format "\\begin{figure*}%s
2004 %s\\centering
2005 %s%s
2006 %s\\end{figure*}"
2007 placement
2008 (if caption-above-p caption "")
2009 comment-include image-code
2010 (if caption-above-p "" caption)))
2011 (figure (format "\\begin{figure}%s
2012 %s\\centering
2013 %s%s
2014 %s\\end{figure}"
2015 placement
2016 (if caption-above-p caption "")
2017 comment-include image-code
2018 (if caption-above-p "" caption)))
2019 (otherwise image-code))))
2021 (defun org-latex-link (link desc info)
2022 "Transcode a LINK object from Org to LaTeX.
2024 DESC is the description part of the link, or the empty string.
2025 INFO is a plist holding contextual information. See
2026 `org-export-data'."
2027 (let* ((type (org-element-property :type link))
2028 (raw-path (replace-regexp-in-string
2029 "%" "\\%" (org-element-property :path link) nil t))
2030 ;; Ensure DESC really exists, or set it to nil.
2031 (desc (and (not (string= desc "")) desc))
2032 (imagep (org-export-inline-image-p
2033 link (plist-get info :latex-inline-image-rules)))
2034 (path (cond
2035 ((member type '("http" "https" "ftp" "mailto"))
2036 (concat type ":" raw-path))
2037 ((and (string= type "file") (file-name-absolute-p raw-path))
2038 (concat "file:" raw-path))
2039 (t raw-path))))
2040 (cond
2041 ;; Link type is handled by a special function.
2042 ((org-export-custom-protocol-maybe link desc 'latex))
2043 ;; Image file.
2044 (imagep (org-latex--inline-image link info))
2045 ;; Radio link: Transcode target's contents and use them as link's
2046 ;; description.
2047 ((string= type "radio")
2048 (let ((destination (org-export-resolve-radio-link link info)))
2049 (if (not destination) desc
2050 (format "\\hyperref[%s]{%s}"
2051 (org-export-solidify-link-text
2052 (org-element-property :value destination))
2053 desc))))
2054 ;; Links pointing to a headline: Find destination and build
2055 ;; appropriate referencing command.
2056 ((member type '("custom-id" "fuzzy" "id"))
2057 (let ((destination (if (string= type "fuzzy")
2058 (org-export-resolve-fuzzy-link link info)
2059 (org-export-resolve-id-link link info))))
2060 (case (org-element-type destination)
2061 ;; Id link points to an external file.
2062 (plain-text
2063 (if desc (format "\\href{%s}{%s}" destination desc)
2064 (format "\\url{%s}" destination)))
2065 ;; Fuzzy link points nowhere.
2066 ('nil
2067 (format (plist-get info :latex-link-with-unknown-path-format)
2068 (or desc
2069 (org-export-data
2070 (org-element-property :raw-link link) info))))
2071 ;; LINK points to a headline. If headlines are numbered
2072 ;; and the link has no description, display headline's
2073 ;; number. Otherwise, display description or headline's
2074 ;; title.
2075 (headline
2076 (let* ((custom-label
2077 (and (plist-get info :latex-custom-id-labels)
2078 (org-element-property :CUSTOM_ID destination)))
2079 (label (or custom-label
2080 (org-export-get-headline-id destination info))))
2081 (if (and (not desc)
2082 (org-export-numbered-headline-p destination info))
2083 (format "\\ref{%s}" label)
2084 (format "\\hyperref[%s]{%s}" label
2085 (or desc
2086 (org-export-data
2087 (org-element-property :title destination) info))))))
2088 ;; Fuzzy link points to a target. Do as above.
2089 (otherwise
2090 (let ((path (org-export-solidify-link-text path)))
2091 (if (not desc) (format "\\ref{%s}" path)
2092 (format "\\hyperref[%s]{%s}" path desc)))))))
2093 ;; Coderef: replace link with the reference name or the
2094 ;; equivalent line number.
2095 ((string= type "coderef")
2096 (format (org-export-get-coderef-format path desc)
2097 (org-export-resolve-coderef path info)))
2098 ;; External link with a description part.
2099 ((and path desc) (format "\\href{%s}{%s}" path desc))
2100 ;; External link without a description part.
2101 (path (format "\\url{%s}" path))
2102 ;; No path, only description. Try to do something useful.
2103 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
2106 ;;;; Node Property
2108 (defun org-latex-node-property (node-property contents info)
2109 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2110 CONTENTS is nil. INFO is a plist holding contextual
2111 information."
2112 (format "%s:%s"
2113 (org-element-property :key node-property)
2114 (let ((value (org-element-property :value node-property)))
2115 (if value (concat " " value) ""))))
2118 ;;;; Paragraph
2120 (defun org-latex-paragraph (paragraph contents info)
2121 "Transcode a PARAGRAPH element from Org to LaTeX.
2122 CONTENTS is the contents of the paragraph, as a string. INFO is
2123 the plist used as a communication channel."
2124 contents)
2127 ;;;; Plain List
2129 (defun org-latex-plain-list (plain-list contents info)
2130 "Transcode a PLAIN-LIST element from Org to LaTeX.
2131 CONTENTS is the contents of the list. INFO is a plist holding
2132 contextual information."
2133 (let* ((type (org-element-property :type plain-list))
2134 (attr (org-export-read-attribute :attr_latex plain-list))
2135 (latex-type (let ((env (plist-get attr :environment)))
2136 (cond (env (format "%s" env))
2137 ((eq type 'ordered) "enumerate")
2138 ((eq type 'descriptive) "description")
2139 (t "itemize")))))
2140 (org-latex--wrap-label
2141 plain-list
2142 (format "\\begin{%s}%s\n%s\\end{%s}"
2143 latex-type
2144 (or (plist-get attr :options) "")
2145 contents
2146 latex-type))))
2149 ;;;; Plain Text
2151 (defun org-latex-plain-text (text info)
2152 "Transcode a TEXT string from Org to LaTeX.
2153 TEXT is the string to transcode. INFO is a plist holding
2154 contextual information."
2155 (let* ((specialp (plist-get info :with-special-strings))
2156 (output
2157 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2158 (let ((case-fold-search nil))
2159 (replace-regexp-in-string
2160 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2161 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2162 ;; However, if special strings are used, be careful not
2163 ;; to protect "\" in "\-" constructs.
2164 (replace-regexp-in-string
2165 (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\(?:[^-]\\|$\\)"))
2166 (lambda (m)
2167 (case (aref m 0)
2168 (?\\ "$\\\\backslash$")
2169 (?~ "\\\\textasciitilde{}")
2170 (?^ "\\\\^{}")
2171 (t "\\\\\\&")))
2172 text)))))
2173 ;; Activate smart quotes. Be sure to provide original TEXT string
2174 ;; since OUTPUT may have been modified.
2175 (when (plist-get info :with-smart-quotes)
2176 (setq output (org-export-activate-smart-quotes output :latex info text)))
2177 ;; Convert special strings.
2178 (when specialp
2179 (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
2180 ;; Handle break preservation if required.
2181 (when (plist-get info :preserve-breaks)
2182 (setq output (replace-regexp-in-string
2183 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
2184 ;; Return value.
2185 output))
2188 ;;;; Planning
2190 (defun org-latex-planning (planning contents info)
2191 "Transcode a PLANNING element from Org to LaTeX.
2192 CONTENTS is nil. INFO is a plist holding contextual
2193 information."
2194 (concat
2195 "\\noindent"
2196 (mapconcat
2197 'identity
2198 (delq nil
2199 (list
2200 (let ((closed (org-element-property :closed planning)))
2201 (when closed
2202 (concat
2203 (format "\\textbf{%s} " org-closed-string)
2204 (format (plist-get info :latex-inactive-timestamp-format)
2205 (org-timestamp-translate closed)))))
2206 (let ((deadline (org-element-property :deadline planning)))
2207 (when deadline
2208 (concat
2209 (format "\\textbf{%s} " org-deadline-string)
2210 (format (plist-get info :latex-active-timestamp-format)
2211 (org-timestamp-translate deadline)))))
2212 (let ((scheduled (org-element-property :scheduled planning)))
2213 (when scheduled
2214 (concat
2215 (format "\\textbf{%s} " org-scheduled-string)
2216 (format (plist-get info :latex-active-timestamp-format)
2217 (org-timestamp-translate scheduled)))))))
2218 " ")
2219 "\\\\"))
2222 ;;;; Property Drawer
2224 (defun org-latex-property-drawer (property-drawer contents info)
2225 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2226 CONTENTS holds the contents of the drawer. INFO is a plist
2227 holding contextual information."
2228 (and (org-string-nw-p contents)
2229 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2232 ;;;; Pseudo Element: LaTeX Matrices
2234 ;; `latex-matrices' elements have the following properties:
2235 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2236 ;; `math').
2238 (defun org-latex--wrap-latex-matrices (data info)
2239 "Merge contiguous tables with the same mode within a pseudo-element.
2240 DATA is a parse tree or a secondary string. INFO is a plist
2241 containing export options. Modify DATA by side-effect and return
2242 it."
2243 (org-element-map data 'table
2244 (lambda (table)
2245 (when (eq (org-element-property :type table) 'org)
2246 (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
2247 (plist-get info :latex-default-table-mode))))
2248 (when (and (member mode '("inline-math" "math"))
2249 ;; Do not wrap twice the same table.
2250 (not (eq (org-element-type
2251 (org-element-property :parent table))
2252 'latex-matrices)))
2253 (let* ((caption (and (not (string= mode "inline-math"))
2254 (org-element-property :caption table)))
2255 (matrices
2256 (list 'latex-matrices
2257 (list :caption caption
2258 :markup
2259 (cond ((string= mode "inline-math") 'inline)
2260 (caption 'equation)
2261 (t 'math)))))
2262 (previous table)
2263 (next (org-export-get-next-element table info)))
2264 (org-element-insert-before matrices table)
2265 ;; Swallow all contiguous tables sharing the same mode.
2266 (while (and
2267 (zerop (or (org-element-property :post-blank previous) 0))
2268 (setq next (org-export-get-next-element previous info))
2269 (eq (org-element-type next) 'table)
2270 (eq (org-element-property :type next) 'org)
2271 (string= (or (org-export-read-attribute
2272 :attr_latex next :mode)
2273 (plist-get info :latex-default-table-mode))
2274 mode))
2275 (org-element-extract-element previous)
2276 (org-element-adopt-elements matrices previous)
2277 (setq previous next))
2278 (org-element-put-property
2279 matrices :post-blank (org-element-property :post-blank previous))
2280 (org-element-extract-element previous)
2281 (org-element-adopt-elements matrices previous))))))
2282 info)
2283 data)
2285 (defun org-latex-matrices (matrices contents info)
2286 "Transcode a MATRICES element from Org to LaTeX.
2287 CONTENTS is a string. INFO is a plist used as a communication
2288 channel."
2289 (format (case (org-element-property :markup matrices)
2290 (inline "\\(%s\\)")
2291 (equation "\\begin{equation}\n%s\\end{equation}")
2292 (t "\\[\n%s\\]"))
2293 contents))
2295 (defun org-latex-matrices-tree-filter (tree backend info)
2296 (org-latex--wrap-latex-matrices tree info))
2298 ;;;; Pseudo Object: LaTeX Math Block
2300 ;; `latex-math-block' objects have the following property:
2301 ;; `:post-blank'.
2303 (defun org-latex--wrap-latex-math-block (data info)
2304 "Merge contiguous math objects in a pseudo-object container.
2305 DATA is a parse tree or a secondary string. INFO is a plist
2306 containing export options. Modify DATA by side-effect and return it."
2307 (let ((valid-object-p
2308 (function
2309 ;; Non-nil when OBJ can be added to the latex math block.
2310 (lambda (obj)
2311 (case (org-element-type obj)
2312 (entity (org-element-property :latex-math-p obj))
2313 (latex-fragment
2314 (let ((value (org-element-property :value obj)))
2315 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2316 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2317 ((subscript superscript) t))))))
2318 (org-element-map data '(entity latex-fragment subscript superscript)
2319 (lambda (object)
2320 ;; Skip objects already wrapped.
2321 (when (and (not (eq (org-element-type
2322 (org-element-property :parent object))
2323 'latex-math-block))
2324 (funcall valid-object-p object))
2325 (let ((math-block (list 'latex-math-block nil))
2326 (next-elements (org-export-get-next-element object info t))
2327 (last object))
2328 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2329 (org-element-insert-before math-block object)
2330 (org-element-extract-element object)
2331 (org-element-adopt-elements math-block object)
2332 (when (zerop (or (org-element-property :post-blank object) 0))
2333 ;; MATH-BLOCK swallows consecutive math objects.
2334 (catch 'exit
2335 (dolist (next next-elements)
2336 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2337 (org-element-extract-element next)
2338 (org-element-adopt-elements math-block next)
2339 ;; Eschew the case: \beta$x$ -> \(\betax\).
2340 (unless (memq (org-element-type next)
2341 '(subscript superscript))
2342 (org-element-put-property last :post-blank 1))
2343 (setq last next)
2344 (when (> (or (org-element-property :post-blank next) 0) 0)
2345 (throw 'exit nil))))))
2346 (org-element-put-property
2347 math-block :post-blank (org-element-property :post-blank last)))))
2348 info nil '(subscript superscript latex-math-block) t)
2349 ;; Return updated DATA.
2350 data))
2352 (defun org-latex-math-block-tree-filter (tree backend info)
2353 (org-latex--wrap-latex-math-block tree info))
2355 (defun org-latex-math-block-options-filter (info backend)
2356 (dolist (prop '(:author :date :title) info)
2357 (plist-put info prop
2358 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2360 (defun org-latex-math-block (math-block contents info)
2361 "Transcode a MATH-BLOCK object from Org to LaTeX.
2362 CONTENTS is a string. INFO is a plist used as a communication
2363 channel."
2364 (when (org-string-nw-p contents)
2365 (format "\\(%s\\)" (org-trim contents))))
2367 ;;;; Quote Block
2369 (defun org-latex-quote-block (quote-block contents info)
2370 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2371 CONTENTS holds the contents of the block. INFO is a plist
2372 holding contextual information."
2373 (org-latex--wrap-label
2374 quote-block
2375 (format "\\begin{quote}\n%s\\end{quote}" contents)))
2378 ;;;; Radio Target
2380 (defun org-latex-radio-target (radio-target text info)
2381 "Transcode a RADIO-TARGET object from Org to LaTeX.
2382 TEXT is the text of the target. INFO is a plist holding
2383 contextual information."
2384 (format "\\label{%s}%s"
2385 (org-export-solidify-link-text
2386 (org-element-property :value radio-target))
2387 text))
2390 ;;;; Section
2392 (defun org-latex-section (section contents info)
2393 "Transcode a SECTION element from Org to LaTeX.
2394 CONTENTS holds the contents of the section. INFO is a plist
2395 holding contextual information."
2396 contents)
2399 ;;;; Special Block
2401 (defun org-latex-special-block (special-block contents info)
2402 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2403 CONTENTS holds the contents of the block. INFO is a plist
2404 holding contextual information."
2405 (let ((type (org-element-property :type special-block))
2406 (opt (org-export-read-attribute :attr_latex special-block :options))
2407 (caption (org-latex--caption/label-string special-block info))
2408 (caption-above-p (org-latex--caption-above-p special-block info)))
2409 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2410 (and caption-above-p caption)
2411 contents
2412 (and (not caption-above-p) caption)
2413 (format "\\end{%s}" type))))
2416 ;;;; Src Block
2418 (defun org-latex-src-block (src-block contents info)
2419 "Transcode a SRC-BLOCK element from Org to LaTeX.
2420 CONTENTS holds the contents of the item. INFO is a plist holding
2421 contextual information."
2422 (when (org-string-nw-p (org-element-property :value src-block))
2423 (let* ((lang (org-element-property :language src-block))
2424 (caption (org-element-property :caption src-block))
2425 (caption-above-p (org-latex--caption-above-p src-block info))
2426 (label (org-element-property :name src-block))
2427 (custom-env (and lang
2428 (cadr (assq (intern lang)
2429 org-latex-custom-lang-environments))))
2430 (num-start (case (org-element-property :number-lines src-block)
2431 (continued (org-export-get-loc src-block info))
2432 (new 0)))
2433 (retain-labels (org-element-property :retain-labels src-block))
2434 (attributes (org-export-read-attribute :attr_latex src-block))
2435 (float (plist-get attributes :float))
2436 (listings (plist-get info :latex-listings)))
2437 (cond
2438 ;; Case 1. No source fontification.
2439 ((not listings)
2440 (let* ((caption-str (org-latex--caption/label-string src-block info))
2441 (float-env
2442 (cond ((and (not float) (plist-member attributes :float)) "%s")
2443 ((string= "multicolumn" float)
2444 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2445 (plist-get info :latex-default-figure-position)
2446 (if caption-above-p caption-str "")
2447 (if caption-above-p "" caption-str)))
2448 ((or caption float)
2449 (format "\\begin{figure}[H]\n%%s\n%s\\end{figure}"
2450 caption-str))
2451 (t "%s"))))
2452 (format
2453 float-env
2454 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2455 (org-export-format-code-default src-block info))))))
2456 ;; Case 2. Custom environment.
2457 (custom-env
2458 (let ((caption-str (org-latex--caption/label-string src-block info)))
2459 (format "\\begin{%s}\n%s\\end{%s}\n"
2460 custom-env
2461 (concat (and caption-above-p caption-str)
2462 (org-export-format-code-default src-block info)
2463 (and (not caption-above-p) caption-str))
2464 custom-env)))
2465 ;; Case 3. Use minted package.
2466 ((eq listings 'minted)
2467 (let* ((caption-str (org-latex--caption/label-string src-block info))
2468 (float-env
2469 (cond
2470 ((and (not float) (plist-member attributes :float) caption)
2471 (let ((caption
2472 (replace-regexp-in-string
2473 "\\\\caption" "\\captionof{listing}" caption-str
2474 t t)))
2475 (concat (and caption-above-p caption)
2476 "%%s"
2477 (and (not caption-above-p) (concat "\n" caption)))))
2478 ((and (not float) (plist-member attributes :float)) "%s")
2479 ((string= "multicolumn" float)
2480 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2481 (if caption-above-p caption-str "")
2482 (if caption-above-p "" caption-str)))
2483 ((or caption float)
2484 (format "\\begin{listing}[H]\n%s%%s\n%s\\end{listing}"
2485 (if caption-above-p caption-str "")
2486 (if caption-above-p "" caption-str)))
2487 (t "%s")))
2488 (options (plist-get info :latex-minted-options))
2489 (body
2490 (format
2491 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2492 ;; Options.
2493 (concat
2494 (org-latex--make-option-string
2495 (if (or (not num-start) (assoc "linenos" options))
2496 options
2497 (append
2498 `(("linenos")
2499 ("firstnumber" ,(number-to-string (1+ num-start))))
2500 options)))
2501 (let ((local-options (plist-get attributes :options)))
2502 (and local-options (concat "," local-options))))
2503 ;; Language.
2504 (or (cadr (assq (intern lang)
2505 (plist-get info :latex-minted-langs)))
2506 (downcase lang))
2507 ;; Source code.
2508 (let* ((code-info (org-export-unravel-code src-block))
2509 (max-width
2510 (apply 'max
2511 (mapcar 'length
2512 (org-split-string (car code-info)
2513 "\n")))))
2514 (org-export-format-code
2515 (car code-info)
2516 (lambda (loc num ref)
2517 (concat
2519 (when ref
2520 ;; Ensure references are flushed to the right,
2521 ;; separated with 6 spaces from the widest line
2522 ;; of code.
2523 (concat (make-string (+ (- max-width (length loc)) 6)
2524 ?\s)
2525 (format "(%s)" ref)))))
2526 nil (and retain-labels (cdr code-info)))))))
2527 ;; Return value.
2528 (format float-env body)))
2529 ;; Case 4. Use listings package.
2531 (let ((lst-lang
2532 (or (cadr (assq (intern lang)
2533 (plist-get info :latex-listings-langs)))
2534 lang))
2535 (caption-str
2536 (when caption
2537 (let ((main (org-export-get-caption src-block))
2538 (secondary (org-export-get-caption src-block t)))
2539 (if (not secondary)
2540 (format "{%s}" (org-export-data main info))
2541 (format "{[%s]%s}"
2542 (org-export-data secondary info)
2543 (org-export-data main info))))))
2544 (lst-opt (plist-get info :latex-listings-options)))
2545 (concat
2546 ;; Options.
2547 (format
2548 "\\lstset{%s}\n"
2549 (concat
2550 (org-latex--make-option-string
2551 (append
2552 lst-opt
2553 (cond
2554 ((and (not float) (plist-member attributes :float)) nil)
2555 ((string= "multicolumn" float) '(("float" "*")))
2556 ((and float (not (assoc "float" lst-opt)))
2557 `(("float" ,(plist-get info :latex-default-figure-position)))))
2558 `(("language" ,lst-lang))
2559 (if label `(("label" ,label)) '(("label" " ")))
2560 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2561 `(("captionpos" ,(if caption-above-p "t" "b")))
2562 (cond ((assoc "numbers" lst-opt) nil)
2563 ((not num-start) '(("numbers" "none")))
2564 ((zerop num-start) '(("numbers" "left")))
2565 (t `(("firstnumber" ,(number-to-string (1+ num-start)))
2566 ("numbers" "left"))))))
2567 (let ((local-options (plist-get attributes :options)))
2568 (and local-options (concat "," local-options)))))
2569 ;; Source code.
2570 (format
2571 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2572 (let* ((code-info (org-export-unravel-code src-block))
2573 (max-width
2574 (apply 'max
2575 (mapcar 'length
2576 (org-split-string (car code-info) "\n")))))
2577 (org-export-format-code
2578 (car code-info)
2579 (lambda (loc num ref)
2580 (concat
2582 (when ref
2583 ;; Ensure references are flushed to the right,
2584 ;; separated with 6 spaces from the widest line of
2585 ;; code
2586 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2587 (format "(%s)" ref)))))
2588 nil (and retain-labels (cdr code-info))))))))))))
2591 ;;;; Statistics Cookie
2593 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2594 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2595 CONTENTS is nil. INFO is a plist holding contextual information."
2596 (replace-regexp-in-string
2597 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2600 ;;;; Strike-Through
2602 (defun org-latex-strike-through (strike-through contents info)
2603 "Transcode STRIKE-THROUGH from Org to LaTeX.
2604 CONTENTS is the text with strike-through markup. INFO is a plist
2605 holding contextual information."
2606 (org-latex--text-markup contents 'strike-through info))
2609 ;;;; Subscript
2611 (defun org-latex--script-size (object info)
2612 "Transcode a subscript or superscript object.
2613 OBJECT is an Org object. INFO is a plist used as a communication
2614 channel."
2615 (let ((type (org-element-type object))
2616 (output ""))
2617 (org-element-map (org-element-contents object)
2618 (cons 'plain-text org-element-all-objects)
2619 (lambda (obj)
2620 (case (org-element-type obj)
2621 ((entity latex-fragment)
2622 (let ((data (org-trim (org-export-data obj info))))
2623 (string-match
2624 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2625 data)
2626 (setq output
2627 (concat output
2628 (match-string 1 data)
2629 (let ((blank (org-element-property :post-blank obj)))
2630 (and blank (> blank 0) "\\ "))))))
2631 (plain-text
2632 (setq output
2633 (format "%s\\text{%s}" output (org-export-data obj info))))
2634 (otherwise
2635 (setq output
2636 (concat output
2637 (org-export-data obj info)
2638 (let ((blank (org-element-property :post-blank obj)))
2639 (and blank (> blank 0) "\\ ")))))))
2640 info nil org-element-recursive-objects)
2641 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2642 ;; character.
2643 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2644 (and (> (length output) 1) "{")
2645 output
2646 (and (> (length output) 1) "}"))))
2648 (defun org-latex-subscript (subscript contents info)
2649 "Transcode a SUBSCRIPT object from Org to LaTeX.
2650 CONTENTS is the contents of the object. INFO is a plist holding
2651 contextual information."
2652 (org-latex--script-size subscript info))
2655 ;;;; Superscript
2657 (defun org-latex-superscript (superscript contents info)
2658 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2659 CONTENTS is the contents of the object. INFO is a plist holding
2660 contextual information."
2661 (org-latex--script-size superscript info))
2664 ;;;; Table
2666 ;; `org-latex-table' is the entry point for table transcoding. It
2667 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2668 ;; delegates the job to either `org-latex--table.el-table',
2669 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2670 ;; depending of the type of the table and the mode requested.
2672 ;; `org-latex--align-string' is a subroutine used to build alignment
2673 ;; string for Org tables.
2675 (defun org-latex-table (table contents info)
2676 "Transcode a TABLE element from Org to LaTeX.
2677 CONTENTS is the contents of the table. INFO is a plist holding
2678 contextual information."
2679 (if (eq (org-element-property :type table) 'table.el)
2680 ;; "table.el" table. Convert it using appropriate tools.
2681 (org-latex--table.el-table table info)
2682 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2683 (plist-get info :latex-default-table-mode))))
2684 (cond
2685 ;; Case 1: Verbatim table.
2686 ((string= type "verbatim")
2687 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2688 ;; Re-create table, without affiliated keywords.
2689 (org-trim (org-element-interpret-data
2690 `(table nil ,@(org-element-contents table))))))
2691 ;; Case 2: Matrix.
2692 ((or (string= type "math") (string= type "inline-math"))
2693 (org-latex--math-table table info))
2694 ;; Case 3: Standard table.
2695 (t (concat (org-latex--org-table table contents info)
2696 ;; When there are footnote references within the
2697 ;; table, insert their definition just after it.
2698 (org-latex--delayed-footnotes-definitions table info)))))))
2700 (defun org-latex--align-string (table info)
2701 "Return an appropriate LaTeX alignment string.
2702 TABLE is the considered table. INFO is a plist used as
2703 a communication channel."
2704 (or (org-export-read-attribute :attr_latex table :align)
2705 (let (align)
2706 ;; Extract column groups and alignment from first (non-rule)
2707 ;; row.
2708 (org-element-map
2709 (org-element-map table 'table-row
2710 (lambda (row)
2711 (and (eq (org-element-property :type row) 'standard) row))
2712 info 'first-match)
2713 'table-cell
2714 (lambda (cell)
2715 (let ((borders (org-export-table-cell-borders cell info)))
2716 ;; Check left border for the first cell only.
2717 (when (and (memq 'left borders) (not align))
2718 (push "|" align))
2719 (push (case (org-export-table-cell-alignment cell info)
2720 (left "l")
2721 (right "r")
2722 (center "c"))
2723 align)
2724 (when (memq 'right borders) (push "|" align))))
2725 info)
2726 (apply 'concat (nreverse align)))))
2728 (defun org-latex--org-table (table contents info)
2729 "Return appropriate LaTeX code for an Org table.
2731 TABLE is the table type element to transcode. CONTENTS is its
2732 contents, as a string. INFO is a plist used as a communication
2733 channel.
2735 This function assumes TABLE has `org' as its `:type' property and
2736 `table' as its `:mode' attribute."
2737 (let* ((caption (org-latex--caption/label-string table info))
2738 (attr (org-export-read-attribute :attr_latex table))
2739 ;; Determine alignment string.
2740 (alignment (org-latex--align-string table info))
2741 ;; Determine environment for the table: longtable, tabular...
2742 (table-env (or (plist-get attr :environment)
2743 (plist-get info :latex-default-table-environment)))
2744 ;; If table is a float, determine environment: table, table*
2745 ;; or sidewaystable.
2746 (float-env (unless (member table-env '("longtable" "longtabu"))
2747 (let ((float (plist-get attr :float)))
2748 (cond
2749 ((and (not float) (plist-member attr :float)) nil)
2750 ((or (string= float "sidewaystable")
2751 (string= float "sideways")) "sidewaystable")
2752 ((string= float "multicolumn") "table*")
2753 ((or float
2754 (org-element-property :caption table)
2755 (org-string-nw-p (plist-get attr :caption)))
2756 "table")))))
2757 ;; Extract others display options.
2758 (fontsize (let ((font (plist-get attr :font)))
2759 (and font (concat font "\n"))))
2760 ;; "tabular" environment doesn't allow to define a width.
2761 (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
2762 (spreadp (plist-get attr :spread))
2763 (placement
2764 (or (plist-get attr :placement)
2765 (format "[%s]" (plist-get info :latex-default-figure-position))))
2766 (centerp (if (plist-member attr :center) (plist-get attr :center)
2767 (plist-get info :latex-tables-centered)))
2768 (caption-above-p (org-latex--caption-above-p table info)))
2769 ;; Prepare the final format string for the table.
2770 (cond
2771 ;; Longtable.
2772 ((equal "longtable" table-env)
2773 (concat (and fontsize (concat "{" fontsize))
2774 (format "\\begin{longtable}{%s}\n" alignment)
2775 (and caption-above-p
2776 (org-string-nw-p caption)
2777 (concat caption "\\\\\n"))
2778 contents
2779 (and (not caption-above-p)
2780 (org-string-nw-p caption)
2781 (concat caption "\\\\\n"))
2782 "\\end{longtable}\n"
2783 (and fontsize "}")))
2784 ;; Longtabu
2785 ((equal "longtabu" table-env)
2786 (concat (and fontsize (concat "{" fontsize))
2787 (format "\\begin{longtabu}%s{%s}\n"
2788 (if width
2789 (format " %s %s "
2790 (if spreadp "spread" "to") width) "")
2791 alignment)
2792 (and caption-above-p
2793 (org-string-nw-p caption)
2794 (concat caption "\\\\\n"))
2795 contents
2796 (and (not caption-above-p)
2797 (org-string-nw-p caption)
2798 (concat caption "\\\\\n"))
2799 "\\end{longtabu}\n"
2800 (and fontsize "}")))
2801 ;; Others.
2802 (t (concat (cond
2803 (float-env
2804 (concat (format "\\begin{%s}%s\n" float-env placement)
2805 (if caption-above-p caption "")
2806 (when centerp "\\centering\n")
2807 fontsize))
2808 (centerp (concat "\\begin{center}\n" fontsize))
2809 (fontsize (concat "{" fontsize)))
2810 (cond ((equal "tabu" table-env)
2811 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2812 (if width (format
2813 (if spreadp " spread %s " " to %s ")
2814 width) "")
2815 alignment
2816 contents))
2817 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2818 table-env
2819 (if width (format "{%s}" width) "")
2820 alignment
2821 contents
2822 table-env)))
2823 (cond
2824 (float-env
2825 (concat (if caption-above-p "" caption)
2826 (format "\n\\end{%s}" float-env)))
2827 (centerp "\n\\end{center}")
2828 (fontsize "}")))))))
2830 (defun org-latex--table.el-table (table info)
2831 "Return appropriate LaTeX code for a table.el table.
2833 TABLE is the table type element to transcode. INFO is a plist
2834 used as a communication channel.
2836 This function assumes TABLE has `table.el' as its `:type'
2837 property."
2838 (require 'table)
2839 ;; Ensure "*org-export-table*" buffer is empty.
2840 (with-current-buffer (get-buffer-create "*org-export-table*")
2841 (erase-buffer))
2842 (let ((output (with-temp-buffer
2843 (insert (org-element-property :value table))
2844 (goto-char 1)
2845 (re-search-forward "^[ \t]*|[^|]" nil t)
2846 (table-generate-source 'latex "*org-export-table*")
2847 (with-current-buffer "*org-export-table*"
2848 (org-trim (buffer-string))))))
2849 (kill-buffer (get-buffer "*org-export-table*"))
2850 ;; Remove left out comments.
2851 (while (string-match "^%.*\n" output)
2852 (setq output (replace-match "" t t output)))
2853 (let ((attr (org-export-read-attribute :attr_latex table)))
2854 (when (plist-get attr :rmlines)
2855 ;; When the "rmlines" attribute is provided, remove all hlines
2856 ;; but the the one separating heading from the table body.
2857 (let ((n 0) (pos 0))
2858 (while (and (< (length output) pos)
2859 (setq pos (string-match "^\\\\hline\n?" output pos)))
2860 (incf n)
2861 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2862 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2863 (plist-get info :latex-tables-centered))))
2864 (if (not centerp) output
2865 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2867 (defun org-latex--math-table (table info)
2868 "Return appropriate LaTeX code for a matrix.
2870 TABLE is the table type element to transcode. INFO is a plist
2871 used as a communication channel.
2873 This function assumes TABLE has `org' as its `:type' property and
2874 `inline-math' or `math' as its `:mode' attribute."
2875 (let* ((attr (org-export-read-attribute :attr_latex table))
2876 (env (or (plist-get attr :environment)
2877 (plist-get info :latex-default-table-environment)))
2878 (contents
2879 (mapconcat
2880 (lambda (row)
2881 ;; Ignore horizontal rules.
2882 (when (eq (org-element-property :type row) 'standard)
2883 ;; Return each cell unmodified.
2884 (concat
2885 (mapconcat
2886 (lambda (cell)
2887 (substring (org-element-interpret-data cell) 0 -1))
2888 (org-element-map row 'table-cell #'identity info) "&")
2889 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2890 "\n")))
2891 (org-element-map table 'table-row #'identity info) "")))
2892 (concat
2893 ;; Prefix.
2894 (plist-get attr :math-prefix)
2895 ;; Environment. Also treat special cases.
2896 (cond ((member env '("array" "tabular"))
2897 (let ((align (make-string
2898 (cdr (org-export-table-dimensions table info)) ?c)))
2899 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
2900 ((assoc env org-latex-table-matrix-macros)
2901 (format "\\%s%s{\n%s}"
2903 (or (plist-get attr :math-arguments) "")
2904 contents))
2905 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2906 ;; Suffix.
2907 (plist-get attr :math-suffix))))
2910 ;;;; Table Cell
2912 (defun org-latex-table-cell (table-cell contents info)
2913 "Transcode a TABLE-CELL element from Org to LaTeX.
2914 CONTENTS is the cell contents. INFO is a plist used as
2915 a communication channel."
2916 (concat
2917 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
2918 (if (and contents
2919 scientific-format
2920 (string-match orgtbl-exp-regexp contents))
2921 ;; Use appropriate format string for scientific
2922 ;; notation.
2923 (format scientific-format
2924 (match-string 1 contents)
2925 (match-string 2 contents))
2926 contents))
2927 (when (org-export-get-next-element table-cell info) " & ")))
2930 ;;;; Table Row
2932 (defun org-latex-table-row (table-row contents info)
2933 "Transcode a TABLE-ROW element from Org to LaTeX.
2934 CONTENTS is the contents of the row. INFO is a plist used as
2935 a communication channel."
2936 (let* ((attr (org-export-read-attribute :attr_latex
2937 (org-export-get-parent table-row)))
2938 (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
2939 (plist-get info :latex-tables-booktabs)))
2940 (longtablep
2941 (member (or (plist-get attr :environment)
2942 (plist-get info :latex-default-table-environment))
2943 '("longtable" "longtabu"))))
2944 (if (eq (org-element-property :type table-row) 'rule)
2945 (cond
2946 ((not booktabsp) "\\hline")
2947 ((not (org-export-get-previous-element table-row info)) "\\toprule")
2948 ((not (org-export-get-next-element table-row info)) "\\bottomrule")
2949 ((and longtablep
2950 (org-export-table-row-ends-header-p
2951 (org-export-get-previous-element table-row info) info))
2953 (t "\\midrule"))
2954 (concat
2955 ;; When BOOKTABS are activated enforce top-rule even when no
2956 ;; hline was specifically marked.
2957 (and booktabsp (not (org-export-get-previous-element table-row info))
2958 "\\toprule\n")
2959 contents "\\\\\n"
2960 (cond
2961 ;; Special case for long tables. Define header and footers.
2962 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2963 (let ((columns (cdr (org-export-table-dimensions
2964 (org-export-get-parent-table table-row) info))))
2965 (format "%s
2966 \\endfirsthead
2967 \\multicolumn{%d}{l}{%s} \\\\
2969 %s \\\\\n
2971 \\endhead
2972 %s\\multicolumn{%d}{r}{%s} \\\\
2973 \\endfoot
2974 \\endlastfoot"
2975 (if booktabsp "\\midrule" "\\hline")
2976 columns
2977 (org-latex--translate "Continued from previous page" info)
2978 (cond
2979 ((not (org-export-table-row-starts-header-p table-row info))
2981 (booktabsp "\\toprule\n")
2982 (t "\\hline\n"))
2983 contents
2984 (if booktabsp "\\midrule" "\\hline")
2985 (if booktabsp "\\midrule" "\\hline")
2986 columns
2987 (org-latex--translate "Continued on next page" info))))
2988 ;; When BOOKTABS are activated enforce bottom rule even when
2989 ;; no hline was specifically marked.
2990 ((and booktabsp (not (org-export-get-next-element table-row info)))
2991 "\\bottomrule"))))))
2994 ;;;; Target
2996 (defun org-latex-target (target contents info)
2997 "Transcode a TARGET object from Org to LaTeX.
2998 CONTENTS is nil. INFO is a plist holding contextual
2999 information."
3000 (format "\\label{%s}"
3001 (org-export-solidify-link-text (org-element-property :value target))))
3004 ;;;; Timestamp
3006 (defun org-latex-timestamp (timestamp contents info)
3007 "Transcode a TIMESTAMP object from Org to LaTeX.
3008 CONTENTS is nil. INFO is a plist holding contextual
3009 information."
3010 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
3011 (format
3012 (plist-get info
3013 (case (org-element-property :type timestamp)
3014 ((active active-range) :latex-active-timestamp-format)
3015 ((inactive inactive-range) :latex-inactive-timestamp-format)
3016 (otherwise :latex-diary-timestamp-format)))
3017 value)))
3020 ;;;; Underline
3022 (defun org-latex-underline (underline contents info)
3023 "Transcode UNDERLINE from Org to LaTeX.
3024 CONTENTS is the text with underline markup. INFO is a plist
3025 holding contextual information."
3026 (org-latex--text-markup contents 'underline info))
3029 ;;;; Verbatim
3031 (defun org-latex-verbatim (verbatim contents info)
3032 "Transcode a VERBATIM object from Org to LaTeX.
3033 CONTENTS is nil. INFO is a plist used as a communication
3034 channel."
3035 (org-latex--text-markup
3036 (org-element-property :value verbatim) 'verbatim info))
3039 ;;;; Verse Block
3041 (defun org-latex-verse-block (verse-block contents info)
3042 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3043 CONTENTS is verse block contents. INFO is a plist holding
3044 contextual information."
3045 (org-latex--wrap-label
3046 verse-block
3047 ;; In a verse environment, add a line break to each newline
3048 ;; character and change each white space at beginning of a line
3049 ;; into a space of 1 em. Also change each blank line with
3050 ;; a vertical space of 1 em.
3051 (format "\\begin{verse}\n%s\\end{verse}"
3052 (replace-regexp-in-string
3053 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
3054 (replace-regexp-in-string
3055 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3056 (replace-regexp-in-string
3057 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3058 contents nil t) nil t) nil t))))
3062 ;;; End-user functions
3064 ;;;###autoload
3065 (defun org-latex-export-as-latex
3066 (&optional async subtreep visible-only body-only ext-plist)
3067 "Export current buffer as a LaTeX buffer.
3069 If narrowing is active in the current buffer, only export its
3070 narrowed part.
3072 If a region is active, export that region.
3074 A non-nil optional argument ASYNC means the process should happen
3075 asynchronously. The resulting buffer should be accessible
3076 through the `org-export-stack' interface.
3078 When optional argument SUBTREEP is non-nil, export the sub-tree
3079 at point, extracting information from the headline properties
3080 first.
3082 When optional argument VISIBLE-ONLY is non-nil, don't export
3083 contents of hidden elements.
3085 When optional argument BODY-ONLY is non-nil, only write code
3086 between \"\\begin{document}\" and \"\\end{document}\".
3088 EXT-PLIST, when provided, is a property list with external
3089 parameters overriding Org default settings, but still inferior to
3090 file-local settings.
3092 Export is done in a buffer named \"*Org LATEX Export*\", which
3093 will be displayed when `org-export-show-temporary-export-buffer'
3094 is non-nil."
3095 (interactive)
3096 (org-export-to-buffer 'latex "*Org LATEX Export*"
3097 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
3099 ;;;###autoload
3100 (defun org-latex-convert-region-to-latex ()
3101 "Assume the current region has org-mode syntax, and convert it to LaTeX.
3102 This can be used in any buffer. For example, you can write an
3103 itemized list in org-mode syntax in an LaTeX buffer and use this
3104 command to convert it."
3105 (interactive)
3106 (org-export-replace-region-by 'latex))
3108 ;;;###autoload
3109 (defun org-latex-export-to-latex
3110 (&optional async subtreep visible-only body-only ext-plist)
3111 "Export current buffer to a LaTeX file.
3113 If narrowing is active in the current buffer, only export its
3114 narrowed part.
3116 If a region is active, export that region.
3118 A non-nil optional argument ASYNC means the process should happen
3119 asynchronously. The resulting file should be accessible through
3120 the `org-export-stack' interface.
3122 When optional argument SUBTREEP is non-nil, export the sub-tree
3123 at point, extracting information from the headline properties
3124 first.
3126 When optional argument VISIBLE-ONLY is non-nil, don't export
3127 contents of hidden elements.
3129 When optional argument BODY-ONLY is non-nil, only write code
3130 between \"\\begin{document}\" and \"\\end{document}\".
3132 EXT-PLIST, when provided, is a property list with external
3133 parameters overriding Org default settings, but still inferior to
3134 file-local settings."
3135 (interactive)
3136 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3137 (org-export-to-file 'latex outfile
3138 async subtreep visible-only body-only ext-plist)))
3140 ;;;###autoload
3141 (defun org-latex-export-to-pdf
3142 (&optional async subtreep visible-only body-only ext-plist)
3143 "Export current buffer to LaTeX then process through to PDF.
3145 If narrowing is active in the current buffer, only export its
3146 narrowed part.
3148 If a region is active, export that region.
3150 A non-nil optional argument ASYNC means the process should happen
3151 asynchronously. The resulting file should be accessible through
3152 the `org-export-stack' interface.
3154 When optional argument SUBTREEP is non-nil, export the sub-tree
3155 at point, extracting information from the headline properties
3156 first.
3158 When optional argument VISIBLE-ONLY is non-nil, don't export
3159 contents of hidden elements.
3161 When optional argument BODY-ONLY is non-nil, only write code
3162 between \"\\begin{document}\" and \"\\end{document}\".
3164 EXT-PLIST, when provided, is a property list with external
3165 parameters overriding Org default settings, but still inferior to
3166 file-local settings.
3168 Return PDF file's name."
3169 (interactive)
3170 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3171 (org-export-to-file 'latex outfile
3172 async subtreep visible-only body-only ext-plist
3173 (lambda (file) (org-latex-compile file)))))
3175 (defun org-latex-compile (texfile &optional snippet)
3176 "Compile a TeX file.
3178 TEXFILE is the name of the file being compiled. Processing is
3179 done through the command specified in `org-latex-pdf-process'.
3181 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3182 file used to preview a LaTeX snippet. In this case, do not
3183 create a log buffer and do not bother removing log files.
3185 Return PDF file name or an error if it couldn't be produced."
3186 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3187 (full-name (file-truename texfile))
3188 (out-dir (file-name-directory texfile))
3189 ;; Properly set working directory for compilation.
3190 (default-directory (if (file-name-absolute-p texfile)
3191 (file-name-directory full-name)
3192 default-directory))
3193 (time (current-time))
3194 warnings)
3195 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
3196 (save-window-excursion
3197 (cond
3198 ;; A function is provided: Apply it.
3199 ((functionp org-latex-pdf-process)
3200 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3201 ;; A list is provided: Replace %b, %f and %o with appropriate
3202 ;; values in each command before applying it. Output is
3203 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3204 ((consp org-latex-pdf-process)
3205 (let ((outbuf (and (not snippet)
3206 (get-buffer-create "*Org PDF LaTeX Output*"))))
3207 (dolist (command org-latex-pdf-process)
3208 (shell-command
3209 (replace-regexp-in-string
3210 "%b" (shell-quote-argument base-name)
3211 (replace-regexp-in-string
3212 "%f" (shell-quote-argument full-name)
3213 (replace-regexp-in-string
3214 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
3215 outbuf))
3216 ;; Collect standard errors from output buffer.
3217 (setq warnings (and (not snippet)
3218 (org-latex--collect-warnings outbuf)))))
3219 (t (error "No valid command to process to PDF")))
3220 (let ((pdffile (concat out-dir base-name ".pdf")))
3221 ;; Check for process failure. Provide collected errors if
3222 ;; possible.
3223 (if (or (not (file-exists-p pdffile))
3224 (time-less-p (nth 5 (file-attributes pdffile)) time))
3225 (error (format "PDF file %s wasn't produced" pdffile))
3226 ;; Else remove log files, when specified, and signal end of
3227 ;; process to user, along with any error encountered.
3228 (unless snippet
3229 (when org-latex-remove-logfiles
3230 (dolist (file (directory-files
3231 out-dir t
3232 (concat (regexp-quote base-name)
3233 "\\(?:\\.[0-9]+\\)?"
3234 "\\."
3235 (regexp-opt org-latex-logfiles-extensions))))
3236 (delete-file file)))
3237 (message (concat "PDF file produced"
3238 (cond
3239 ((eq warnings 'error) " with errors.")
3240 (warnings (concat " with warnings: " warnings))
3241 (t "."))))))
3242 ;; Return output file name.
3243 pdffile))))
3245 (defun org-latex--collect-warnings (buffer)
3246 "Collect some warnings from \"pdflatex\" command output.
3247 BUFFER is the buffer containing output. Return collected
3248 warnings types as a string, `error' if a LaTeX error was
3249 encountered or nil if there was none."
3250 (with-current-buffer buffer
3251 (save-excursion
3252 (goto-char (point-max))
3253 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3254 (if (re-search-forward "^!" nil t) 'error
3255 (let ((case-fold-search t)
3256 (warnings ""))
3257 (dolist (warning org-latex-known-warnings)
3258 (when (save-excursion (re-search-forward (car warning) nil t))
3259 (setq warnings (concat warnings " " (cdr warning)))))
3260 (org-string-nw-p (org-trim warnings))))))))
3262 ;;;###autoload
3263 (defun org-latex-publish-to-latex (plist filename pub-dir)
3264 "Publish an Org file to LaTeX.
3266 FILENAME is the filename of the Org file to be published. PLIST
3267 is the property list for the given project. PUB-DIR is the
3268 publishing directory.
3270 Return output file name."
3271 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3273 ;;;###autoload
3274 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3275 "Publish an Org file to PDF (via LaTeX).
3277 FILENAME is the filename of the Org file to be published. PLIST
3278 is the property list for the given project. PUB-DIR is the
3279 publishing directory.
3281 Return output file name."
3282 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3283 ;; in working directory and then moved to publishing directory.
3284 (org-publish-attachment
3285 plist
3286 (org-latex-compile
3287 (org-publish-org-to
3288 'latex filename ".tex" plist (file-name-directory filename)))
3289 pub-dir))
3292 (provide 'ox-latex)
3294 ;; Local variables:
3295 ;; generated-autoload-file: "org-loaddefs.el"
3296 ;; End:
3298 ;;; ox-latex.el ends here