ox-latex, ox-koma-letter: Use new `parse' syntax
[org-mode/org-tableheadings.git] / lisp / ox-latex.el
blobd9599a11bd18a837df73220926e39c3c8980313d
1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2015 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; See Org manual for details.
27 ;;; Code:
29 (eval-when-compile (require 'cl))
30 (require 'ox)
31 (require 'ox-publish)
33 (defvar org-latex-default-packages-alist)
34 (defvar org-latex-packages-alist)
35 (defvar orgtbl-exp-regexp)
39 ;;; Define Back-End
41 (org-export-define-backend 'latex
42 '((bold . org-latex-bold)
43 (center-block . org-latex-center-block)
44 (clock . org-latex-clock)
45 (code . org-latex-code)
46 (drawer . org-latex-drawer)
47 (dynamic-block . org-latex-dynamic-block)
48 (entity . org-latex-entity)
49 (example-block . org-latex-example-block)
50 (export-block . org-latex-export-block)
51 (export-snippet . org-latex-export-snippet)
52 (fixed-width . org-latex-fixed-width)
53 (footnote-definition . org-latex-footnote-definition)
54 (footnote-reference . org-latex-footnote-reference)
55 (headline . org-latex-headline)
56 (horizontal-rule . org-latex-horizontal-rule)
57 (inline-src-block . org-latex-inline-src-block)
58 (inlinetask . org-latex-inlinetask)
59 (italic . org-latex-italic)
60 (item . org-latex-item)
61 (keyword . org-latex-keyword)
62 (latex-environment . org-latex-latex-environment)
63 (latex-fragment . org-latex-latex-fragment)
64 (line-break . org-latex-line-break)
65 (link . org-latex-link)
66 (node-property . org-latex-node-property)
67 (paragraph . org-latex-paragraph)
68 (plain-list . org-latex-plain-list)
69 (plain-text . org-latex-plain-text)
70 (planning . org-latex-planning)
71 (property-drawer . org-latex-property-drawer)
72 (quote-block . org-latex-quote-block)
73 (radio-target . org-latex-radio-target)
74 (section . org-latex-section)
75 (special-block . org-latex-special-block)
76 (src-block . org-latex-src-block)
77 (statistics-cookie . org-latex-statistics-cookie)
78 (strike-through . org-latex-strike-through)
79 (subscript . org-latex-subscript)
80 (superscript . org-latex-superscript)
81 (table . org-latex-table)
82 (table-cell . org-latex-table-cell)
83 (table-row . org-latex-table-row)
84 (target . org-latex-target)
85 (template . org-latex-template)
86 (timestamp . org-latex-timestamp)
87 (underline . org-latex-underline)
88 (verbatim . org-latex-verbatim)
89 (verse-block . org-latex-verse-block)
90 ;; Pseudo objects and elements.
91 (latex-math-block . org-latex-math-block)
92 (latex-matrices . org-latex-matrices))
93 :export-block '("LATEX" "TEX")
94 :menu-entry
95 '(?l "Export to LaTeX"
96 ((?L "As LaTeX buffer" org-latex-export-as-latex)
97 (?l "As LaTeX file" org-latex-export-to-latex)
98 (?p "As PDF file" org-latex-export-to-pdf)
99 (?o "As PDF file and open"
100 (lambda (a s v b)
101 (if a (org-latex-export-to-pdf t s v b)
102 (org-open-file (org-latex-export-to-pdf nil s v b)))))))
103 :filters-alist '((:filter-options . org-latex-math-block-options-filter)
104 (:filter-parse-tree org-latex-math-block-tree-filter
105 org-latex-matrices-tree-filter))
106 :options-alist
107 '((:latex-class "LATEX_CLASS" nil org-latex-default-class t)
108 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
109 (:latex-header "LATEX_HEADER" nil nil newline)
110 (:latex-header-extra "LATEX_HEADER_EXTRA" nil nil newline)
111 (:description "DESCRIPTION" nil nil parse)
112 (:keywords "KEYWORDS" nil nil parse)
113 ;; 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 ((language (let ((lang (plist-get info :language)))
1224 (or (cdr (assoc lang org-latex-babel-language-alist))
1225 lang))))
1226 `((?a . ,(org-export-data (plist-get info :author) info))
1227 (?t . ,(org-export-data (plist-get info :title) info))
1228 (?k . ,(org-export-data (org-latex--wrap-latex-math-block
1229 (plist-get info :keywords) info)
1230 info))
1231 (?d . ,(org-export-data (org-latex--wrap-latex-math-block
1232 (plist-get info :description) info)
1233 info))
1234 (?c . ,(plist-get info :creator))
1235 (?l . ,language)
1236 (?L . ,(capitalize language))
1237 (?D . ,(org-export-get-date info)))))
1240 ;;; Template
1242 (defun org-latex-template (contents info)
1243 "Return complete document string after LaTeX conversion.
1244 CONTENTS is the transcoded contents string. INFO is a plist
1245 holding export options."
1246 (let ((title (org-export-data (plist-get info :title) info))
1247 (spec (org-latex--format-spec info)))
1248 (concat
1249 ;; Time-stamp.
1250 (and (plist-get info :time-stamp-file)
1251 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1252 ;; Document class and packages.
1253 (let* ((class (plist-get info :latex-class))
1254 (class-options (plist-get info :latex-class-options))
1255 (header (nth 1 (assoc class (plist-get info :latex-classes))))
1256 (document-class-string
1257 (and (stringp header)
1258 (if (not class-options) header
1259 (replace-regexp-in-string
1260 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1261 class-options header t nil 1)))))
1262 (if (not document-class-string)
1263 (user-error "Unknown LaTeX class `%s'" class)
1264 (org-latex-guess-babel-language
1265 (org-latex-guess-inputenc
1266 (org-element-normalize-string
1267 (org-splice-latex-header
1268 document-class-string
1269 org-latex-default-packages-alist
1270 org-latex-packages-alist nil
1271 (concat (org-element-normalize-string
1272 (plist-get info :latex-header))
1273 (plist-get info :latex-header-extra)))))
1274 info)))
1275 ;; Possibly limit depth for headline numbering.
1276 (let ((sec-num (plist-get info :section-numbers)))
1277 (when (integerp sec-num)
1278 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1279 ;; Author.
1280 (let ((author (and (plist-get info :with-author)
1281 (let ((auth (plist-get info :author)))
1282 (and auth (org-export-data auth info)))))
1283 (email (and (plist-get info :with-email)
1284 (org-export-data (plist-get info :email) info))))
1285 (cond ((and author email (not (string= "" email)))
1286 (format "\\author{%s\\thanks{%s}}\n" author email))
1287 ((or author email) (format "\\author{%s}\n" (or author email)))))
1288 ;; Date.
1289 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1290 (format "\\date{%s}\n" (org-export-data date info)))
1291 ;; Title
1292 (format "\\title{%s}\n" title)
1293 ;; Hyperref options.
1294 (let ((template (plist-get info :latex-hyperref-template)))
1295 (and (stringp template)
1296 (format-spec template spec)))
1297 ;; Document start.
1298 "\\begin{document}\n\n"
1299 ;; Title command.
1300 (let* ((title-command (plist-get info :latex-title-command))
1301 (command (and (stringp title-command)
1302 (format-spec title-command spec))))
1303 (org-element-normalize-string
1304 (cond ((not (plist-get info :with-title)) nil)
1305 ((string= "" title) nil)
1306 ((not (stringp command)) nil)
1307 ((string-match "\\(?:[^%]\\|^\\)%s" command)
1308 (format command title))
1309 (t command))))
1310 ;; Table of contents.
1311 (let ((depth (plist-get info :with-toc)))
1312 (when depth
1313 (concat (when (wholenump depth)
1314 (format "\\setcounter{tocdepth}{%d}\n" depth))
1315 (plist-get info :latex-toc-command))))
1316 ;; Document's body.
1317 contents
1318 ;; Creator.
1319 (and (plist-get info :with-creator)
1320 (concat (plist-get info :creator) "\n"))
1321 ;; Document end.
1322 "\\end{document}")))
1326 ;;; Transcode Functions
1328 ;;;; Bold
1330 (defun org-latex-bold (bold contents info)
1331 "Transcode BOLD from Org to LaTeX.
1332 CONTENTS is the text with bold markup. INFO is a plist holding
1333 contextual information."
1334 (org-latex--text-markup contents 'bold info))
1337 ;;;; Center Block
1339 (defun org-latex-center-block (center-block contents info)
1340 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1341 CONTENTS holds the contents of the center block. INFO is a plist
1342 holding contextual information."
1343 (org-latex--wrap-label
1344 center-block
1345 (format "\\begin{center}\n%s\\end{center}" contents)))
1348 ;;;; Clock
1350 (defun org-latex-clock (clock contents info)
1351 "Transcode a CLOCK element from Org to LaTeX.
1352 CONTENTS is nil. INFO is a plist holding contextual
1353 information."
1354 (concat
1355 "\\noindent"
1356 (format "\\textbf{%s} " org-clock-string)
1357 (format (plist-get info :latex-inactive-timestamp-format)
1358 (concat (org-timestamp-translate (org-element-property :value clock))
1359 (let ((time (org-element-property :duration clock)))
1360 (and time (format " (%s)" time)))))
1361 "\\\\"))
1364 ;;;; Code
1366 (defun org-latex-code (code contents info)
1367 "Transcode a CODE object from Org to LaTeX.
1368 CONTENTS is nil. INFO is a plist used as a communication
1369 channel."
1370 (org-latex--text-markup (org-element-property :value code) 'code info))
1373 ;;;; Drawer
1375 (defun org-latex-drawer (drawer contents info)
1376 "Transcode a DRAWER element from Org to LaTeX.
1377 CONTENTS holds the contents of the block. INFO is a plist
1378 holding contextual information."
1379 (let* ((name (org-element-property :drawer-name drawer))
1380 (output (funcall (plist-get info :latex-format-drawer-function)
1381 name contents)))
1382 (org-latex--wrap-label drawer output)))
1385 ;;;; Dynamic Block
1387 (defun org-latex-dynamic-block (dynamic-block contents info)
1388 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1389 CONTENTS holds the contents of the block. INFO is a plist
1390 holding contextual information. See `org-export-data'."
1391 (org-latex--wrap-label dynamic-block contents))
1394 ;;;; Entity
1396 (defun org-latex-entity (entity contents info)
1397 "Transcode an ENTITY object from Org to LaTeX.
1398 CONTENTS are the definition itself. INFO is a plist holding
1399 contextual information."
1400 (org-element-property :latex entity))
1403 ;;;; Example Block
1405 (defun org-latex-example-block (example-block contents info)
1406 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1407 CONTENTS is nil. INFO is a plist holding contextual
1408 information."
1409 (when (org-string-nw-p (org-element-property :value example-block))
1410 (org-latex--wrap-label
1411 example-block
1412 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1413 (org-export-format-code-default example-block info)))))
1416 ;;;; Export Block
1418 (defun org-latex-export-block (export-block contents info)
1419 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1420 CONTENTS is nil. INFO is a plist holding contextual information."
1421 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1422 (org-remove-indentation (org-element-property :value export-block))))
1425 ;;;; Export Snippet
1427 (defun org-latex-export-snippet (export-snippet contents info)
1428 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1429 CONTENTS is nil. INFO is a plist holding contextual information."
1430 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1431 (org-element-property :value export-snippet)))
1434 ;;;; Fixed Width
1436 (defun org-latex-fixed-width (fixed-width contents info)
1437 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1438 CONTENTS is nil. INFO is a plist holding contextual information."
1439 (org-latex--wrap-label
1440 fixed-width
1441 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1442 (org-remove-indentation
1443 (org-element-property :value fixed-width)))))
1446 ;;;; Footnote Reference
1448 (defun org-latex-footnote-reference (footnote-reference contents info)
1449 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1450 CONTENTS is nil. INFO is a plist holding contextual information."
1451 (concat
1452 ;; Insert separator between two footnotes in a row.
1453 (let ((prev (org-export-get-previous-element footnote-reference info)))
1454 (when (eq (org-element-type prev) 'footnote-reference)
1455 (plist-get info :latex-footnote-separator)))
1456 (cond
1457 ;; Use \footnotemark if the footnote has already been defined.
1458 ((not (org-export-footnote-first-reference-p footnote-reference info))
1459 (format "\\footnotemark[%s]{}"
1460 (org-export-get-footnote-number footnote-reference info)))
1461 ;; Use \footnotemark if reference is within another footnote
1462 ;; reference, footnote definition or table cell.
1463 ((org-element-lineage footnote-reference
1464 '(footnote-reference footnote-definition table-cell))
1465 "\\footnotemark")
1466 ;; Otherwise, define it with \footnote command.
1468 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1469 (concat
1470 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1471 ;; Retrieve all footnote references within the footnote and
1472 ;; add their definition after it, since LaTeX doesn't support
1473 ;; them inside.
1474 (org-latex--delayed-footnotes-definitions def info)))))))
1477 ;;;; Headline
1479 (defun org-latex-headline (headline contents info)
1480 "Transcode a HEADLINE element from Org to LaTeX.
1481 CONTENTS holds the contents of the headline. INFO is a plist
1482 holding contextual information."
1483 (unless (org-element-property :footnote-section-p headline)
1484 (let* ((class (plist-get info :latex-class))
1485 (level (org-export-get-relative-level headline info))
1486 (numberedp (org-export-numbered-headline-p headline info))
1487 (class-sectioning (assoc class (plist-get info :latex-classes)))
1488 ;; Section formatting will set two placeholders: one for
1489 ;; the title and the other for the contents.
1490 (section-fmt
1491 (let ((sec (if (functionp (nth 2 class-sectioning))
1492 (funcall (nth 2 class-sectioning) level numberedp)
1493 (nth (1+ level) class-sectioning))))
1494 (cond
1495 ;; No section available for that LEVEL.
1496 ((not sec) nil)
1497 ;; Section format directly returned by a function. Add
1498 ;; placeholder for contents.
1499 ((stringp sec) (concat sec "\n%s"))
1500 ;; (numbered-section . unnumbered-section)
1501 ((not (consp (cdr sec)))
1502 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1503 ;; (numbered-open numbered-close)
1504 ((= (length sec) 2)
1505 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1506 ;; (num-in num-out no-num-in no-num-out)
1507 ((= (length sec) 4)
1508 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1509 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1510 ;; Create a temporary export back-end that hard-codes
1511 ;; "\underline" within "\section" and alike.
1512 (section-back-end
1513 (org-export-create-backend
1514 :parent 'latex
1515 :transcoders
1516 '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
1517 (text
1518 (org-export-data-with-backend
1519 (org-element-property :title headline) section-back-end info))
1520 (todo
1521 (and (plist-get info :with-todo-keywords)
1522 (let ((todo (org-element-property :todo-keyword headline)))
1523 (and todo (org-export-data todo info)))))
1524 (todo-type (and todo (org-element-property :todo-type headline)))
1525 (tags (and (plist-get info :with-tags)
1526 (org-export-get-tags headline info)))
1527 (priority (and (plist-get info :with-priority)
1528 (org-element-property :priority headline)))
1529 ;; Create the headline text along with a no-tag version.
1530 ;; The latter is required to remove tags from toc.
1531 (full-text (funcall (plist-get info :latex-format-headline-function)
1532 todo todo-type priority text tags info))
1533 ;; Associate \label to the headline for internal links.
1534 (headline-label
1535 (format "\\label{%s}\n"
1536 (or (and (plist-get info :latex-custom-id-labels)
1537 (org-element-property :CUSTOM_ID headline))
1538 (org-export-get-headline-id headline info))))
1539 (pre-blanks
1540 (make-string (org-element-property :pre-blank headline) 10)))
1541 (if (or (not section-fmt) (org-export-low-level-p headline info))
1542 ;; This is a deep sub-tree: export it as a list item. Also
1543 ;; export as items headlines for which no section format has
1544 ;; been found.
1545 (let ((low-level-body
1546 (concat
1547 ;; If headline is the first sibling, start a list.
1548 (when (org-export-first-sibling-p headline info)
1549 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1550 ;; Itemize headline
1551 "\\item"
1552 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
1553 "\\relax")
1554 " " full-text "\n"
1555 headline-label
1556 pre-blanks
1557 contents)))
1558 ;; If headline is not the last sibling simply return
1559 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1560 ;; any blank line.
1561 (if (not (org-export-last-sibling-p headline info)) low-level-body
1562 (replace-regexp-in-string
1563 "[ \t\n]*\\'"
1564 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1565 low-level-body)))
1566 ;; This is a standard headline. Export it as a section. Add
1567 ;; an alternative heading when possible, and when this is not
1568 ;; identical to the usual heading.
1569 (let ((opt-title
1570 (funcall (plist-get info :latex-format-headline-function)
1571 todo todo-type priority
1572 (org-export-data-with-backend
1573 (org-export-get-alt-title headline info)
1574 section-back-end info)
1575 (and (eq (plist-get info :with-tags) t) tags)
1576 info))
1577 ;; Maybe end local TOC (see `org-latex-keyword').
1578 (contents
1579 (concat
1580 contents
1581 (let ((case-fold-search t)
1582 (section
1583 (let ((first (car (org-element-contents headline))))
1584 (and (eq (org-element-type first) 'section) first))))
1585 (org-element-map section 'keyword
1586 (lambda (k)
1587 (and (equal (org-element-property :key k) "TOC")
1588 (let ((v (org-element-property :value k)))
1589 (and (org-string-match-p "\\<headlines\\>" v)
1590 (org-string-match-p "\\<local\\>" v)
1591 (format "\\stopcontents[level-%d]" level)))))
1592 info t)))))
1593 (if (and numberedp opt-title
1594 (not (equal opt-title full-text))
1595 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
1596 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1597 ;; Replace square brackets with parenthesis
1598 ;; since square brackets are not supported in
1599 ;; optional arguments.
1600 (replace-regexp-in-string
1601 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1602 full-text
1603 (concat headline-label pre-blanks contents))
1604 ;; Impossible to add an alternative heading. Fallback to
1605 ;; regular sectioning format string.
1606 (format section-fmt full-text
1607 (concat headline-label pre-blanks contents))))))))
1609 (defun org-latex-format-headline-default-function
1610 (todo todo-type priority text tags info)
1611 "Default format function for a headline.
1612 See `org-latex-format-headline-function' for details."
1613 (concat
1614 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1615 (and priority (format "\\framebox{\\#%c} " priority))
1616 text
1617 (and tags
1618 (format "\\hfill{}\\textsc{%s}"
1619 (mapconcat (lambda (tag) (org-latex-plain-text tag info))
1620 tags ":")))))
1623 ;;;; Horizontal Rule
1625 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1626 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1627 CONTENTS is nil. INFO is a plist holding contextual information."
1628 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1629 (prev (org-export-get-previous-element horizontal-rule info)))
1630 (concat
1631 ;; Make sure the rule doesn't start at the end of the current
1632 ;; line by separating it with a blank line from previous element.
1633 (when (and prev
1634 (let ((prev-blank (org-element-property :post-blank prev)))
1635 (or (not prev-blank) (zerop prev-blank))))
1636 "\n")
1637 (org-latex--wrap-label
1638 horizontal-rule
1639 (format "\\rule{%s}{%s}"
1640 (or (plist-get attr :width) "\\linewidth")
1641 (or (plist-get attr :thickness) "0.5pt"))))))
1644 ;;;; Inline Src Block
1646 (defun org-latex-inline-src-block (inline-src-block contents info)
1647 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1648 CONTENTS holds the contents of the item. INFO is a plist holding
1649 contextual information."
1650 (let* ((code (org-element-property :value inline-src-block))
1651 (separator (org-latex--find-verb-separator code)))
1652 (case (plist-get info :latex-listings)
1653 ;; Do not use a special package: transcode it verbatim.
1654 ((nil) (concat "\\verb" separator code separator))
1655 ;; Use minted package.
1656 (minted
1657 (let* ((org-lang (org-element-property :language inline-src-block))
1658 (mint-lang (or (cadr (assq (intern org-lang)
1659 (plist-get info :latex-minted-langs)))
1660 (downcase org-lang)))
1661 (options (org-latex--make-option-string
1662 (plist-get info :latex-minted-options))))
1663 (concat (format "\\mint%s{%s}"
1664 (if (string= options "") "" (format "[%s]" options))
1665 mint-lang)
1666 separator code separator)))
1667 ;; Use listings package.
1668 (otherwise
1669 ;; Maybe translate language's name.
1670 (let* ((org-lang (org-element-property :language inline-src-block))
1671 (lst-lang (or (cadr (assq (intern org-lang)
1672 (plist-get info :latex-listings-langs)))
1673 org-lang))
1674 (options (org-latex--make-option-string
1675 (append (plist-get info :latex-listings-options)
1676 `(("language" ,lst-lang))))))
1677 (concat (format "\\lstinline[%s]" options)
1678 separator code separator))))))
1681 ;;;; Inlinetask
1683 (defun org-latex-inlinetask (inlinetask contents info)
1684 "Transcode an INLINETASK element from Org to LaTeX.
1685 CONTENTS holds the contents of the block. INFO is a plist
1686 holding contextual information."
1687 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1688 (todo (and (plist-get info :with-todo-keywords)
1689 (let ((todo (org-element-property :todo-keyword inlinetask)))
1690 (and todo (org-export-data todo info)))))
1691 (todo-type (org-element-property :todo-type inlinetask))
1692 (tags (and (plist-get info :with-tags)
1693 (org-export-get-tags inlinetask info)))
1694 (priority (and (plist-get info :with-priority)
1695 (org-element-property :priority inlinetask)))
1696 (contents (concat
1697 (let ((label (org-element-property :CUSTOM_ID inlinetask)))
1698 (and label (format "\\label{%s}\n" label)))
1699 contents)))
1700 (funcall (plist-get info :latex-format-inlinetask-function)
1701 todo todo-type priority title tags contents info)))
1703 (defun org-latex-format-inlinetask-default-function
1704 (todo todo-type priority title tags contents info)
1705 "Default format function for a inlinetasks.
1706 See `org-latex-format-inlinetask-function' for details."
1707 (let ((full-title
1708 (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1709 (when priority (format "\\framebox{\\#%c} " priority))
1710 title
1711 (when tags
1712 (format "\\hfill{}\\textsc{:%s:}"
1713 (mapconcat
1714 (lambda (tag) (org-latex-plain-text tag info))
1715 tags ":"))))))
1716 (concat "\\begin{center}\n"
1717 "\\fbox{\n"
1718 "\\begin{minipage}[c]{.6\\textwidth}\n"
1719 full-title "\n\n"
1720 (and (org-string-nw-p contents)
1721 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
1722 "\\end{minipage}\n"
1723 "}\n"
1724 "\\end{center}")))
1727 ;;;; Italic
1729 (defun org-latex-italic (italic contents info)
1730 "Transcode ITALIC from Org to LaTeX.
1731 CONTENTS is the text with italic markup. INFO is a plist holding
1732 contextual information."
1733 (org-latex--text-markup contents 'italic info))
1736 ;;;; Item
1738 (defun org-latex-item (item contents info)
1739 "Transcode an ITEM element from Org to LaTeX.
1740 CONTENTS holds the contents of the item. INFO is a plist holding
1741 contextual information."
1742 (let* ((counter
1743 (let ((count (org-element-property :counter item))
1744 (level
1745 ;; Determine level of current item to determine the
1746 ;; correct LaTeX counter to use (enumi, enumii...).
1747 (let ((parent item) (level 0))
1748 (while (memq (org-element-type
1749 (setq parent (org-export-get-parent parent)))
1750 '(plain-list item))
1751 (when (and (eq (org-element-type parent) 'plain-list)
1752 (eq (org-element-property :type parent)
1753 'ordered))
1754 (incf level)))
1755 level)))
1756 (and count
1757 (< level 5)
1758 (format "\\setcounter{enum%s}{%s}\n"
1759 (nth (1- level) '("i" "ii" "iii" "iv"))
1760 (1- count)))))
1761 (checkbox (case (org-element-property :checkbox item)
1762 (on "$\\boxtimes$ ")
1763 (off "$\\square$ ")
1764 (trans "$\\boxminus$ ")))
1765 (tag (let ((tag (org-element-property :tag item)))
1766 ;; Check-boxes must belong to the tag.
1767 (and tag (format "[{%s}] "
1768 (concat checkbox
1769 (org-export-data tag info)))))))
1770 (concat counter
1771 "\\item"
1772 (cond
1773 (tag)
1774 (checkbox (concat " " checkbox))
1775 ;; Without a tag or a check-box, if CONTENTS starts with
1776 ;; an opening square bracket, add "\relax" to "\item",
1777 ;; unless the brackets comes from an initial export
1778 ;; snippet (i.e. it is inserted willingly by the user).
1779 ((and contents
1780 (org-string-match-p "\\`[ \t]*\\[" contents)
1781 (not (let ((e (car (org-element-contents item))))
1782 (and (eq (org-element-type e) 'paragraph)
1783 (let ((o (car (org-element-contents e))))
1784 (and (eq (org-element-type o) 'export-snippet)
1785 (eq (org-export-snippet-backend o)
1786 'latex)))))))
1787 "\\relax ")
1788 (t " "))
1789 (and contents (org-trim contents))
1790 ;; If there are footnotes references in tag, be sure to
1791 ;; add their definition at the end of the item. This
1792 ;; workaround is necessary since "\footnote{}" command is
1793 ;; not supported in tags.
1794 (and tag
1795 (org-latex--delayed-footnotes-definitions
1796 (org-element-property :tag item) info)))))
1799 ;;;; Keyword
1801 (defun org-latex-keyword (keyword contents info)
1802 "Transcode a KEYWORD element from Org to LaTeX.
1803 CONTENTS is nil. INFO is a plist holding contextual information."
1804 (let ((key (org-element-property :key keyword))
1805 (value (org-element-property :value keyword)))
1806 (cond
1807 ((string= key "LATEX") value)
1808 ((string= key "INDEX") (format "\\index{%s}" value))
1809 ((string= key "TOC")
1810 (let ((case-fold-search t))
1811 (cond
1812 ((org-string-match-p "\\<headlines\\>" value)
1813 (let* ((localp (org-string-match-p "\\<local\\>" value))
1814 (parent (org-element-lineage keyword '(headline)))
1815 (level (if (not (and localp parent)) 0
1816 (org-export-get-relative-level parent info)))
1817 (depth
1818 (and (string-match "\\<[0-9]+\\>" value)
1819 (format
1820 "\\setcounter{tocdepth}{%d}"
1821 (+ (string-to-number (match-string 0 value)) level)))))
1822 (if (and localp parent)
1823 ;; Start local TOC, assuming package "titletoc" is
1824 ;; required.
1825 (format "\\startcontents[level-%d]
1826 \\printcontents[level-%d]{}{0}{%s}"
1827 level level (or depth ""))
1828 (concat depth (and depth "\n") "\\tableofcontents"))))
1829 ((org-string-match-p "\\<tables\\>" value) "\\listoftables")
1830 ((org-string-match-p "\\<listings\\>" value)
1831 (case (plist-get info :latex-listings)
1832 ((nil) "\\listoffigures")
1833 (minted "\\listoflistings")
1834 (otherwise "\\lstlistoflistings")))))))))
1837 ;;;; Latex Environment
1839 (defun org-latex-latex-environment (latex-environment contents info)
1840 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1841 CONTENTS is nil. INFO is a plist holding contextual information."
1842 (when (plist-get info :with-latex)
1843 (let ((label (org-element-property :name latex-environment))
1844 (value (org-remove-indentation
1845 (org-element-property :value latex-environment))))
1846 (if (not (org-string-nw-p label)) value
1847 ;; Environment is labeled: label must be within the environment
1848 ;; (otherwise, a reference pointing to that element will count
1849 ;; the section instead).
1850 (with-temp-buffer
1851 (insert value)
1852 (goto-char (point-min))
1853 (forward-line)
1854 (insert
1855 (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1856 (buffer-string))))))
1859 ;;;; Latex Fragment
1861 (defun org-latex-latex-fragment (latex-fragment contents info)
1862 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1863 CONTENTS is nil. INFO is a plist holding contextual information."
1864 (let ((value (org-element-property :value latex-fragment)))
1865 ;; Trim math markers since the fragment is enclosed within
1866 ;; a latex-math-block object anyway.
1867 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
1868 (match-string 2 value))
1869 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
1870 (match-string 1 value))
1871 (t value))))
1874 ;;;; Line Break
1876 (defun org-latex-line-break (line-break contents info)
1877 "Transcode a LINE-BREAK object from Org to LaTeX.
1878 CONTENTS is nil. INFO is a plist holding contextual information."
1879 "\\\\\n")
1882 ;;;; Link
1884 (defun org-latex--inline-image (link info)
1885 "Return LaTeX code for an inline image.
1886 LINK is the link pointing to the inline image. INFO is a plist
1887 used as a communication channel."
1888 (let* ((parent (org-export-get-parent-element link))
1889 (path (let ((raw-path (org-element-property :path link)))
1890 (if (not (file-name-absolute-p raw-path)) raw-path
1891 (expand-file-name raw-path))))
1892 (filetype (file-name-extension path))
1893 (caption (org-latex--caption/label-string parent info))
1894 (caption-above-p (org-latex--caption-above-p link info))
1895 ;; Retrieve latex attributes from the element around.
1896 (attr (org-export-read-attribute :attr_latex parent))
1897 (float (let ((float (plist-get attr :float)))
1898 (cond ((and (not float) (plist-member attr :float)) nil)
1899 ((string= float "wrap") 'wrap)
1900 ((string= float "sideways") 'sideways)
1901 ((string= float "multicolumn") 'multicolumn)
1902 ((or float
1903 (org-element-property :caption parent)
1904 (org-string-nw-p (plist-get attr :caption)))
1905 'figure))))
1906 (placement
1907 (let ((place (plist-get attr :placement)))
1908 (cond
1909 (place (format "%s" place))
1910 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1911 ((eq float 'figure)
1912 (format "[%s]" (plist-get info :latex-default-figure-position)))
1913 (t ""))))
1914 (comment-include (if (plist-get attr :comment-include) "%" ""))
1915 ;; It is possible to specify width and height in the
1916 ;; ATTR_LATEX line, and also via default variables.
1917 (width (cond ((plist-get attr :width))
1918 ((plist-get attr :height) "")
1919 ((eq float 'wrap) "0.48\\textwidth")
1920 (t (plist-get info :latex-image-default-width))))
1921 (height (cond ((plist-get attr :height))
1922 ((or (plist-get attr :width)
1923 (memq float '(figure wrap))) "")
1924 (t (plist-get info :latex-image-default-height))))
1925 (options (let ((opt (or (plist-get attr :options)
1926 (plist-get info :latex-image-default-option))))
1927 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
1928 (match-string 1 opt))))
1929 image-code)
1930 (if (member filetype '("tikz" "pgf"))
1931 ;; For tikz images:
1932 ;; - use \input to read in image file.
1933 ;; - if options are present, wrap in a tikzpicture environment.
1934 ;; - if width or height are present, use \resizebox to change
1935 ;; the image size.
1936 (progn
1937 (setq image-code (format "\\input{%s}" path))
1938 (when (org-string-nw-p options)
1939 (setq image-code
1940 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1941 options
1942 image-code)))
1943 (when (or (org-string-nw-p width) (org-string-nw-p height))
1944 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
1945 (if (org-string-nw-p width) width "!")
1946 (if (org-string-nw-p height) height "!")
1947 image-code))))
1948 ;; For other images:
1949 ;; - add width and height to options.
1950 ;; - include the image with \includegraphics.
1951 (when (org-string-nw-p width)
1952 (setq options (concat options ",width=" width)))
1953 (when (org-string-nw-p height)
1954 (setq options (concat options ",height=" height)))
1955 (let ((search-option (org-element-property :search-option link)))
1956 (when (and search-option
1957 (equal filetype "pdf")
1958 (org-string-match-p "\\`[0-9]+\\'" search-option)
1959 (not (org-string-match-p "page=" options)))
1960 (setq options (concat options ",page=" search-option))))
1961 (setq image-code
1962 (format "\\includegraphics%s{%s}"
1963 (cond ((not (org-string-nw-p options)) "")
1964 ((= (aref options 0) ?,)
1965 (format "[%s]"(substring options 1)))
1966 (t (format "[%s]" options)))
1967 path))
1968 (when (equal filetype "svg")
1969 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
1970 "\\includesvg"
1971 image-code
1972 nil t))
1973 (setq image-code (replace-regexp-in-string "\\.svg}"
1975 image-code
1976 nil t))))
1977 ;; Return proper string, depending on FLOAT.
1978 (case float
1979 (wrap (format "\\begin{wrapfigure}%s
1980 %s\\centering
1981 %s%s
1982 %s\\end{wrapfigure}"
1983 placement
1984 (if caption-above-p caption "")
1985 comment-include image-code
1986 (if caption-above-p "" caption)))
1987 (sideways (format "\\begin{sidewaysfigure}
1988 %s\\centering
1989 %s%s
1990 %s\\end{sidewaysfigure}"
1991 (if caption-above-p caption "")
1992 comment-include image-code
1993 (if caption-above-p "" caption)))
1994 (multicolumn (format "\\begin{figure*}%s
1995 %s\\centering
1996 %s%s
1997 %s\\end{figure*}"
1998 placement
1999 (if caption-above-p caption "")
2000 comment-include image-code
2001 (if caption-above-p "" caption)))
2002 (figure (format "\\begin{figure}%s
2003 %s\\centering
2004 %s%s
2005 %s\\end{figure}"
2006 placement
2007 (if caption-above-p caption "")
2008 comment-include image-code
2009 (if caption-above-p "" caption)))
2010 (otherwise image-code))))
2012 (defun org-latex-link (link desc info)
2013 "Transcode a LINK object from Org to LaTeX.
2015 DESC is the description part of the link, or the empty string.
2016 INFO is a plist holding contextual information. See
2017 `org-export-data'."
2018 (let* ((type (org-element-property :type link))
2019 (raw-path (replace-regexp-in-string
2020 "%" "\\%" (org-element-property :path link) nil t))
2021 ;; Ensure DESC really exists, or set it to nil.
2022 (desc (and (not (string= desc "")) desc))
2023 (imagep (org-export-inline-image-p
2024 link (plist-get info :latex-inline-image-rules)))
2025 (path (cond
2026 ((member type '("http" "https" "ftp" "mailto" "doi"))
2027 (concat type ":" raw-path))
2028 ((and (string= type "file") (file-name-absolute-p raw-path))
2029 (concat "file:" raw-path))
2030 (t raw-path))))
2031 (cond
2032 ;; Link type is handled by a special function.
2033 ((org-export-custom-protocol-maybe link desc 'latex))
2034 ;; Image file.
2035 (imagep (org-latex--inline-image link info))
2036 ;; Radio link: Transcode target's contents and use them as link's
2037 ;; description.
2038 ((string= type "radio")
2039 (let ((destination (org-export-resolve-radio-link link info)))
2040 (if (not destination) desc
2041 (format "\\hyperref[%s]{%s}"
2042 (org-export-solidify-link-text
2043 (org-element-property :value destination))
2044 desc))))
2045 ;; Links pointing to a headline: Find destination and build
2046 ;; appropriate referencing command.
2047 ((member type '("custom-id" "fuzzy" "id"))
2048 (let ((destination (if (string= type "fuzzy")
2049 (org-export-resolve-fuzzy-link link info)
2050 (org-export-resolve-id-link link info))))
2051 (case (org-element-type destination)
2052 ;; Id link points to an external file.
2053 (plain-text
2054 (if desc (format "\\href{%s}{%s}" destination desc)
2055 (format "\\url{%s}" destination)))
2056 ;; Fuzzy link points nowhere.
2057 ('nil
2058 (format (plist-get info :latex-link-with-unknown-path-format)
2059 (or desc
2060 (org-export-data
2061 (org-element-property :raw-link link) info))))
2062 ;; LINK points to a headline. If headlines are numbered
2063 ;; and the link has no description, display headline's
2064 ;; number. Otherwise, display description or headline's
2065 ;; title.
2066 (headline
2067 (let* ((custom-label
2068 (and (plist-get info :latex-custom-id-labels)
2069 (org-element-property :CUSTOM_ID destination)))
2070 (label (or custom-label
2071 (org-export-get-headline-id destination info))))
2072 (if (and (not desc)
2073 (org-export-numbered-headline-p destination info))
2074 (format "\\ref{%s}" label)
2075 (format "\\hyperref[%s]{%s}" label
2076 (or desc
2077 (org-export-data
2078 (org-element-property :title destination) info))))))
2079 ;; Fuzzy link points to a target. Do as above.
2080 (otherwise
2081 (let ((path (org-export-solidify-link-text path)))
2082 (if (not desc) (format "\\ref{%s}" path)
2083 (format "\\hyperref[%s]{%s}" path desc)))))))
2084 ;; Coderef: replace link with the reference name or the
2085 ;; equivalent line number.
2086 ((string= type "coderef")
2087 (format (org-export-get-coderef-format path desc)
2088 (org-export-resolve-coderef path info)))
2089 ;; External link with a description part.
2090 ((and path desc) (format "\\href{%s}{%s}" path desc))
2091 ;; External link without a description part.
2092 (path (format "\\url{%s}" path))
2093 ;; No path, only description. Try to do something useful.
2094 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
2097 ;;;; Node Property
2099 (defun org-latex-node-property (node-property contents info)
2100 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2101 CONTENTS is nil. INFO is a plist holding contextual
2102 information."
2103 (format "%s:%s"
2104 (org-element-property :key node-property)
2105 (let ((value (org-element-property :value node-property)))
2106 (if value (concat " " value) ""))))
2109 ;;;; Paragraph
2111 (defun org-latex-paragraph (paragraph contents info)
2112 "Transcode a PARAGRAPH element from Org to LaTeX.
2113 CONTENTS is the contents of the paragraph, as a string. INFO is
2114 the plist used as a communication channel."
2115 contents)
2118 ;;;; Plain List
2120 (defun org-latex-plain-list (plain-list contents info)
2121 "Transcode a PLAIN-LIST element from Org to LaTeX.
2122 CONTENTS is the contents of the list. INFO is a plist holding
2123 contextual information."
2124 (let* ((type (org-element-property :type plain-list))
2125 (attr (org-export-read-attribute :attr_latex plain-list))
2126 (latex-type (let ((env (plist-get attr :environment)))
2127 (cond (env (format "%s" env))
2128 ((eq type 'ordered) "enumerate")
2129 ((eq type 'descriptive) "description")
2130 (t "itemize")))))
2131 (org-latex--wrap-label
2132 plain-list
2133 (format "\\begin{%s}%s\n%s\\end{%s}"
2134 latex-type
2135 (or (plist-get attr :options) "")
2136 contents
2137 latex-type))))
2140 ;;;; Plain Text
2142 (defun org-latex-plain-text (text info)
2143 "Transcode a TEXT string from Org to LaTeX.
2144 TEXT is the string to transcode. INFO is a plist holding
2145 contextual information."
2146 (let* ((specialp (plist-get info :with-special-strings))
2147 (output
2148 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2149 (let ((case-fold-search nil))
2150 (replace-regexp-in-string
2151 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2152 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2153 ;; However, if special strings are used, be careful not
2154 ;; to protect "\" in "\-" constructs.
2155 (replace-regexp-in-string
2156 (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\(?:[^-]\\|$\\)"))
2157 (lambda (m)
2158 (case (aref m 0)
2159 (?\\ "$\\\\backslash$")
2160 (?~ "\\\\textasciitilde{}")
2161 (?^ "\\\\^{}")
2162 (t "\\\\\\&")))
2163 text)))))
2164 ;; Activate smart quotes. Be sure to provide original TEXT string
2165 ;; since OUTPUT may have been modified.
2166 (when (plist-get info :with-smart-quotes)
2167 (setq output (org-export-activate-smart-quotes output :latex info text)))
2168 ;; Convert special strings.
2169 (when specialp
2170 (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
2171 ;; Handle break preservation if required.
2172 (when (plist-get info :preserve-breaks)
2173 (setq output (replace-regexp-in-string
2174 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
2175 ;; Return value.
2176 output))
2179 ;;;; Planning
2181 (defun org-latex-planning (planning contents info)
2182 "Transcode a PLANNING element from Org to LaTeX.
2183 CONTENTS is nil. INFO is a plist holding contextual
2184 information."
2185 (concat
2186 "\\noindent"
2187 (mapconcat
2188 'identity
2189 (delq nil
2190 (list
2191 (let ((closed (org-element-property :closed planning)))
2192 (when closed
2193 (concat
2194 (format "\\textbf{%s} " org-closed-string)
2195 (format (plist-get info :latex-inactive-timestamp-format)
2196 (org-timestamp-translate closed)))))
2197 (let ((deadline (org-element-property :deadline planning)))
2198 (when deadline
2199 (concat
2200 (format "\\textbf{%s} " org-deadline-string)
2201 (format (plist-get info :latex-active-timestamp-format)
2202 (org-timestamp-translate deadline)))))
2203 (let ((scheduled (org-element-property :scheduled planning)))
2204 (when scheduled
2205 (concat
2206 (format "\\textbf{%s} " org-scheduled-string)
2207 (format (plist-get info :latex-active-timestamp-format)
2208 (org-timestamp-translate scheduled)))))))
2209 " ")
2210 "\\\\"))
2213 ;;;; Property Drawer
2215 (defun org-latex-property-drawer (property-drawer contents info)
2216 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2217 CONTENTS holds the contents of the drawer. INFO is a plist
2218 holding contextual information."
2219 (and (org-string-nw-p contents)
2220 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2223 ;;;; Pseudo Element: LaTeX Matrices
2225 ;; `latex-matrices' elements have the following properties:
2226 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2227 ;; `math').
2229 (defun org-latex--wrap-latex-matrices (data info)
2230 "Merge contiguous tables with the same mode within a pseudo-element.
2231 DATA is a parse tree or a secondary string. INFO is a plist
2232 containing export options. Modify DATA by side-effect and return
2233 it."
2234 (org-element-map data 'table
2235 (lambda (table)
2236 (when (eq (org-element-property :type table) 'org)
2237 (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
2238 (plist-get info :latex-default-table-mode))))
2239 (when (and (member mode '("inline-math" "math"))
2240 ;; Do not wrap twice the same table.
2241 (not (eq (org-element-type
2242 (org-element-property :parent table))
2243 'latex-matrices)))
2244 (let* ((caption (and (not (string= mode "inline-math"))
2245 (org-element-property :caption table)))
2246 (matrices
2247 (list 'latex-matrices
2248 (list :caption caption
2249 :markup
2250 (cond ((string= mode "inline-math") 'inline)
2251 (caption 'equation)
2252 (t 'math)))))
2253 (previous table)
2254 (next (org-export-get-next-element table info)))
2255 (org-element-insert-before matrices table)
2256 ;; Swallow all contiguous tables sharing the same mode.
2257 (while (and
2258 (zerop (or (org-element-property :post-blank previous) 0))
2259 (setq next (org-export-get-next-element previous info))
2260 (eq (org-element-type next) 'table)
2261 (eq (org-element-property :type next) 'org)
2262 (string= (or (org-export-read-attribute
2263 :attr_latex next :mode)
2264 (plist-get info :latex-default-table-mode))
2265 mode))
2266 (org-element-extract-element previous)
2267 (org-element-adopt-elements matrices previous)
2268 (setq previous next))
2269 (org-element-put-property
2270 matrices :post-blank (org-element-property :post-blank previous))
2271 (org-element-extract-element previous)
2272 (org-element-adopt-elements matrices previous))))))
2273 info)
2274 data)
2276 (defun org-latex-matrices (matrices contents info)
2277 "Transcode a MATRICES element from Org to LaTeX.
2278 CONTENTS is a string. INFO is a plist used as a communication
2279 channel."
2280 (format (case (org-element-property :markup matrices)
2281 (inline "\\(%s\\)")
2282 (equation "\\begin{equation}\n%s\\end{equation}")
2283 (t "\\[\n%s\\]"))
2284 contents))
2286 (defun org-latex-matrices-tree-filter (tree backend info)
2287 (org-latex--wrap-latex-matrices tree info))
2289 ;;;; Pseudo Object: LaTeX Math Block
2291 ;; `latex-math-block' objects have the following property:
2292 ;; `:post-blank'.
2294 (defun org-latex--wrap-latex-math-block (data info)
2295 "Merge contiguous math objects in a pseudo-object container.
2296 DATA is a parse tree or a secondary string. INFO is a plist
2297 containing export options. Modify DATA by side-effect and return it."
2298 (let ((valid-object-p
2299 (function
2300 ;; Non-nil when OBJ can be added to the latex math block.
2301 (lambda (obj)
2302 (case (org-element-type obj)
2303 (entity (org-element-property :latex-math-p obj))
2304 (latex-fragment
2305 (let ((value (org-element-property :value obj)))
2306 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2307 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2308 ((subscript superscript) t))))))
2309 (org-element-map data '(entity latex-fragment subscript superscript)
2310 (lambda (object)
2311 ;; Skip objects already wrapped.
2312 (when (and (not (eq (org-element-type
2313 (org-element-property :parent object))
2314 'latex-math-block))
2315 (funcall valid-object-p object))
2316 (let ((math-block (list 'latex-math-block nil))
2317 (next-elements (org-export-get-next-element object info t))
2318 (last object))
2319 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2320 (org-element-insert-before math-block object)
2321 (org-element-extract-element object)
2322 (org-element-adopt-elements math-block object)
2323 (when (zerop (or (org-element-property :post-blank object) 0))
2324 ;; MATH-BLOCK swallows consecutive math objects.
2325 (catch 'exit
2326 (dolist (next next-elements)
2327 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2328 (org-element-extract-element next)
2329 (org-element-adopt-elements math-block next)
2330 ;; Eschew the case: \beta$x$ -> \(\betax\).
2331 (unless (memq (org-element-type next)
2332 '(subscript superscript))
2333 (org-element-put-property last :post-blank 1))
2334 (setq last next)
2335 (when (> (or (org-element-property :post-blank next) 0) 0)
2336 (throw 'exit nil))))))
2337 (org-element-put-property
2338 math-block :post-blank (org-element-property :post-blank last)))))
2339 info nil '(subscript superscript latex-math-block) t)
2340 ;; Return updated DATA.
2341 data))
2343 (defun org-latex-math-block-tree-filter (tree backend info)
2344 (org-latex--wrap-latex-math-block tree info))
2346 (defun org-latex-math-block-options-filter (info backend)
2347 (dolist (prop '(:author :date :title) info)
2348 (plist-put info prop
2349 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2351 (defun org-latex-math-block (math-block contents info)
2352 "Transcode a MATH-BLOCK object from Org to LaTeX.
2353 CONTENTS is a string. INFO is a plist used as a communication
2354 channel."
2355 (when (org-string-nw-p contents)
2356 (format "\\(%s\\)" (org-trim contents))))
2358 ;;;; Quote Block
2360 (defun org-latex-quote-block (quote-block contents info)
2361 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2362 CONTENTS holds the contents of the block. INFO is a plist
2363 holding contextual information."
2364 (org-latex--wrap-label
2365 quote-block
2366 (format "\\begin{quote}\n%s\\end{quote}" contents)))
2369 ;;;; Radio Target
2371 (defun org-latex-radio-target (radio-target text info)
2372 "Transcode a RADIO-TARGET object from Org to LaTeX.
2373 TEXT is the text of the target. INFO is a plist holding
2374 contextual information."
2375 (format "\\label{%s}%s"
2376 (org-export-solidify-link-text
2377 (org-element-property :value radio-target))
2378 text))
2381 ;;;; Section
2383 (defun org-latex-section (section contents info)
2384 "Transcode a SECTION element from Org to LaTeX.
2385 CONTENTS holds the contents of the section. INFO is a plist
2386 holding contextual information."
2387 contents)
2390 ;;;; Special Block
2392 (defun org-latex-special-block (special-block contents info)
2393 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2394 CONTENTS holds the contents of the block. INFO is a plist
2395 holding contextual information."
2396 (let ((type (org-element-property :type special-block))
2397 (opt (org-export-read-attribute :attr_latex special-block :options))
2398 (caption (org-latex--caption/label-string special-block info))
2399 (caption-above-p (org-latex--caption-above-p special-block info)))
2400 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2401 (and caption-above-p caption)
2402 contents
2403 (and (not caption-above-p) caption)
2404 (format "\\end{%s}" type))))
2407 ;;;; Src Block
2409 (defun org-latex-src-block (src-block contents info)
2410 "Transcode a SRC-BLOCK element from Org to LaTeX.
2411 CONTENTS holds the contents of the item. INFO is a plist holding
2412 contextual information."
2413 (when (org-string-nw-p (org-element-property :value src-block))
2414 (let* ((lang (org-element-property :language src-block))
2415 (caption (org-element-property :caption src-block))
2416 (caption-above-p (org-latex--caption-above-p src-block info))
2417 (label (org-element-property :name src-block))
2418 (custom-env (and lang
2419 (cadr (assq (intern lang)
2420 org-latex-custom-lang-environments))))
2421 (num-start (case (org-element-property :number-lines src-block)
2422 (continued (org-export-get-loc src-block info))
2423 (new 0)))
2424 (retain-labels (org-element-property :retain-labels src-block))
2425 (attributes (org-export-read-attribute :attr_latex src-block))
2426 (float (plist-get attributes :float))
2427 (listings (plist-get info :latex-listings)))
2428 (cond
2429 ;; Case 1. No source fontification.
2430 ((not listings)
2431 (let* ((caption-str (org-latex--caption/label-string src-block info))
2432 (float-env
2433 (cond ((and (not float) (plist-member attributes :float)) "%s")
2434 ((string= "multicolumn" float)
2435 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2436 (plist-get info :latex-default-figure-position)
2437 (if caption-above-p caption-str "")
2438 (if caption-above-p "" caption-str)))
2439 ((or caption float)
2440 (format "\\begin{figure}[H]\n%%s\n%s\\end{figure}"
2441 caption-str))
2442 (t "%s"))))
2443 (format
2444 float-env
2445 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2446 (org-export-format-code-default src-block info))))))
2447 ;; Case 2. Custom environment.
2448 (custom-env
2449 (let ((caption-str (org-latex--caption/label-string src-block info)))
2450 (format "\\begin{%s}\n%s\\end{%s}\n"
2451 custom-env
2452 (concat (and caption-above-p caption-str)
2453 (org-export-format-code-default src-block info)
2454 (and (not caption-above-p) caption-str))
2455 custom-env)))
2456 ;; Case 3. Use minted package.
2457 ((eq listings 'minted)
2458 (let* ((caption-str (org-latex--caption/label-string src-block info))
2459 (float-env
2460 (cond
2461 ((and (not float) (plist-member attributes :float) caption)
2462 (let ((caption
2463 (replace-regexp-in-string
2464 "\\\\caption" "\\captionof{listing}" caption-str
2465 t t)))
2466 (concat (and caption-above-p caption)
2467 "%%s"
2468 (and (not caption-above-p) (concat "\n" caption)))))
2469 ((and (not float) (plist-member attributes :float)) "%s")
2470 ((string= "multicolumn" float)
2471 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2472 (if caption-above-p caption-str "")
2473 (if caption-above-p "" caption-str)))
2474 ((or caption float)
2475 (format "\\begin{listing}[H]\n%s%%s\n%s\\end{listing}"
2476 (if caption-above-p caption-str "")
2477 (if caption-above-p "" caption-str)))
2478 (t "%s")))
2479 (options (plist-get info :latex-minted-options))
2480 (body
2481 (format
2482 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2483 ;; Options.
2484 (concat
2485 (org-latex--make-option-string
2486 (if (or (not num-start) (assoc "linenos" options))
2487 options
2488 (append
2489 `(("linenos")
2490 ("firstnumber" ,(number-to-string (1+ num-start))))
2491 options)))
2492 (let ((local-options (plist-get attributes :options)))
2493 (and local-options (concat "," local-options))))
2494 ;; Language.
2495 (or (cadr (assq (intern lang)
2496 (plist-get info :latex-minted-langs)))
2497 (downcase lang))
2498 ;; Source code.
2499 (let* ((code-info (org-export-unravel-code src-block))
2500 (max-width
2501 (apply 'max
2502 (mapcar 'length
2503 (org-split-string (car code-info)
2504 "\n")))))
2505 (org-export-format-code
2506 (car code-info)
2507 (lambda (loc num ref)
2508 (concat
2510 (when ref
2511 ;; Ensure references are flushed to the right,
2512 ;; separated with 6 spaces from the widest line
2513 ;; of code.
2514 (concat (make-string (+ (- max-width (length loc)) 6)
2515 ?\s)
2516 (format "(%s)" ref)))))
2517 nil (and retain-labels (cdr code-info)))))))
2518 ;; Return value.
2519 (format float-env body)))
2520 ;; Case 4. Use listings package.
2522 (let ((lst-lang
2523 (or (cadr (assq (intern lang)
2524 (plist-get info :latex-listings-langs)))
2525 lang))
2526 (caption-str
2527 (when caption
2528 (let ((main (org-export-get-caption src-block))
2529 (secondary (org-export-get-caption src-block t)))
2530 (if (not secondary)
2531 (format "{%s}" (org-export-data main info))
2532 (format "{[%s]%s}"
2533 (org-export-data secondary info)
2534 (org-export-data main info))))))
2535 (lst-opt (plist-get info :latex-listings-options)))
2536 (concat
2537 ;; Options.
2538 (format
2539 "\\lstset{%s}\n"
2540 (concat
2541 (org-latex--make-option-string
2542 (append
2543 lst-opt
2544 (cond
2545 ((and (not float) (plist-member attributes :float)) nil)
2546 ((string= "multicolumn" float) '(("float" "*")))
2547 ((and float (not (assoc "float" lst-opt)))
2548 `(("float" ,(plist-get info :latex-default-figure-position)))))
2549 `(("language" ,lst-lang))
2550 (if label `(("label" ,label)) '(("label" " ")))
2551 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2552 `(("captionpos" ,(if caption-above-p "t" "b")))
2553 (cond ((assoc "numbers" lst-opt) nil)
2554 ((not num-start) '(("numbers" "none")))
2555 ((zerop num-start) '(("numbers" "left")))
2556 (t `(("firstnumber" ,(number-to-string (1+ num-start)))
2557 ("numbers" "left"))))))
2558 (let ((local-options (plist-get attributes :options)))
2559 (and local-options (concat "," local-options)))))
2560 ;; Source code.
2561 (format
2562 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2563 (let* ((code-info (org-export-unravel-code src-block))
2564 (max-width
2565 (apply 'max
2566 (mapcar 'length
2567 (org-split-string (car code-info) "\n")))))
2568 (org-export-format-code
2569 (car code-info)
2570 (lambda (loc num ref)
2571 (concat
2573 (when ref
2574 ;; Ensure references are flushed to the right,
2575 ;; separated with 6 spaces from the widest line of
2576 ;; code
2577 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2578 (format "(%s)" ref)))))
2579 nil (and retain-labels (cdr code-info))))))))))))
2582 ;;;; Statistics Cookie
2584 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2585 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2586 CONTENTS is nil. INFO is a plist holding contextual information."
2587 (replace-regexp-in-string
2588 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2591 ;;;; Strike-Through
2593 (defun org-latex-strike-through (strike-through contents info)
2594 "Transcode STRIKE-THROUGH from Org to LaTeX.
2595 CONTENTS is the text with strike-through markup. INFO is a plist
2596 holding contextual information."
2597 (org-latex--text-markup contents 'strike-through info))
2600 ;;;; Subscript
2602 (defun org-latex--script-size (object info)
2603 "Transcode a subscript or superscript object.
2604 OBJECT is an Org object. INFO is a plist used as a communication
2605 channel."
2606 (let ((type (org-element-type object))
2607 (output ""))
2608 (org-element-map (org-element-contents object)
2609 (cons 'plain-text org-element-all-objects)
2610 (lambda (obj)
2611 (case (org-element-type obj)
2612 ((entity latex-fragment)
2613 (let ((data (org-trim (org-export-data obj info))))
2614 (string-match
2615 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2616 data)
2617 (setq output
2618 (concat output
2619 (match-string 1 data)
2620 (let ((blank (org-element-property :post-blank obj)))
2621 (and blank (> blank 0) "\\ "))))))
2622 (plain-text
2623 (setq output
2624 (format "%s\\text{%s}" output (org-export-data obj info))))
2625 (otherwise
2626 (setq output
2627 (concat output
2628 (org-export-data obj info)
2629 (let ((blank (org-element-property :post-blank obj)))
2630 (and blank (> blank 0) "\\ ")))))))
2631 info nil org-element-recursive-objects)
2632 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2633 ;; character.
2634 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2635 (and (> (length output) 1) "{")
2636 output
2637 (and (> (length output) 1) "}"))))
2639 (defun org-latex-subscript (subscript contents info)
2640 "Transcode a SUBSCRIPT object from Org to LaTeX.
2641 CONTENTS is the contents of the object. INFO is a plist holding
2642 contextual information."
2643 (org-latex--script-size subscript info))
2646 ;;;; Superscript
2648 (defun org-latex-superscript (superscript contents info)
2649 "Transcode a SUPERSCRIPT 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 superscript info))
2655 ;;;; Table
2657 ;; `org-latex-table' is the entry point for table transcoding. It
2658 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2659 ;; delegates the job to either `org-latex--table.el-table',
2660 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2661 ;; depending of the type of the table and the mode requested.
2663 ;; `org-latex--align-string' is a subroutine used to build alignment
2664 ;; string for Org tables.
2666 (defun org-latex-table (table contents info)
2667 "Transcode a TABLE element from Org to LaTeX.
2668 CONTENTS is the contents of the table. INFO is a plist holding
2669 contextual information."
2670 (if (eq (org-element-property :type table) 'table.el)
2671 ;; "table.el" table. Convert it using appropriate tools.
2672 (org-latex--table.el-table table info)
2673 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2674 (plist-get info :latex-default-table-mode))))
2675 (cond
2676 ;; Case 1: Verbatim table.
2677 ((string= type "verbatim")
2678 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2679 ;; Re-create table, without affiliated keywords.
2680 (org-trim (org-element-interpret-data
2681 `(table nil ,@(org-element-contents table))))))
2682 ;; Case 2: Matrix.
2683 ((or (string= type "math") (string= type "inline-math"))
2684 (org-latex--math-table table info))
2685 ;; Case 3: Standard table.
2686 (t (concat (org-latex--org-table table contents info)
2687 ;; When there are footnote references within the
2688 ;; table, insert their definition just after it.
2689 (org-latex--delayed-footnotes-definitions table info)))))))
2691 (defun org-latex--align-string (table info)
2692 "Return an appropriate LaTeX alignment string.
2693 TABLE is the considered table. INFO is a plist used as
2694 a communication channel."
2695 (or (org-export-read-attribute :attr_latex table :align)
2696 (let (align)
2697 ;; Extract column groups and alignment from first (non-rule)
2698 ;; row.
2699 (org-element-map
2700 (org-element-map table 'table-row
2701 (lambda (row)
2702 (and (eq (org-element-property :type row) 'standard) row))
2703 info 'first-match)
2704 'table-cell
2705 (lambda (cell)
2706 (let ((borders (org-export-table-cell-borders cell info)))
2707 ;; Check left border for the first cell only.
2708 (when (and (memq 'left borders) (not align))
2709 (push "|" align))
2710 (push (case (org-export-table-cell-alignment cell info)
2711 (left "l")
2712 (right "r")
2713 (center "c"))
2714 align)
2715 (when (memq 'right borders) (push "|" align))))
2716 info)
2717 (apply 'concat (nreverse align)))))
2719 (defun org-latex--org-table (table contents info)
2720 "Return appropriate LaTeX code for an Org table.
2722 TABLE is the table type element to transcode. CONTENTS is its
2723 contents, as a string. INFO is a plist used as a communication
2724 channel.
2726 This function assumes TABLE has `org' as its `:type' property and
2727 `table' as its `:mode' attribute."
2728 (let* ((caption (org-latex--caption/label-string table info))
2729 (attr (org-export-read-attribute :attr_latex table))
2730 ;; Determine alignment string.
2731 (alignment (org-latex--align-string table info))
2732 ;; Determine environment for the table: longtable, tabular...
2733 (table-env (or (plist-get attr :environment)
2734 (plist-get info :latex-default-table-environment)))
2735 ;; If table is a float, determine environment: table, table*
2736 ;; or sidewaystable.
2737 (float-env (unless (member table-env '("longtable" "longtabu"))
2738 (let ((float (plist-get attr :float)))
2739 (cond
2740 ((and (not float) (plist-member attr :float)) nil)
2741 ((or (string= float "sidewaystable")
2742 (string= float "sideways")) "sidewaystable")
2743 ((string= float "multicolumn") "table*")
2744 ((or float
2745 (org-element-property :caption table)
2746 (org-string-nw-p (plist-get attr :caption)))
2747 "table")))))
2748 ;; Extract others display options.
2749 (fontsize (let ((font (plist-get attr :font)))
2750 (and font (concat font "\n"))))
2751 ;; "tabular" environment doesn't allow to define a width.
2752 (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
2753 (spreadp (plist-get attr :spread))
2754 (placement
2755 (or (plist-get attr :placement)
2756 (format "[%s]" (plist-get info :latex-default-figure-position))))
2757 (centerp (if (plist-member attr :center) (plist-get attr :center)
2758 (plist-get info :latex-tables-centered)))
2759 (caption-above-p (org-latex--caption-above-p table info)))
2760 ;; Prepare the final format string for the table.
2761 (cond
2762 ;; Longtable.
2763 ((equal "longtable" table-env)
2764 (concat (and fontsize (concat "{" fontsize))
2765 (format "\\begin{longtable}{%s}\n" alignment)
2766 (and caption-above-p
2767 (org-string-nw-p caption)
2768 (concat caption "\\\\\n"))
2769 contents
2770 (and (not caption-above-p)
2771 (org-string-nw-p caption)
2772 (concat caption "\\\\\n"))
2773 "\\end{longtable}\n"
2774 (and fontsize "}")))
2775 ;; Longtabu
2776 ((equal "longtabu" table-env)
2777 (concat (and fontsize (concat "{" fontsize))
2778 (format "\\begin{longtabu}%s{%s}\n"
2779 (if width
2780 (format " %s %s "
2781 (if spreadp "spread" "to") width) "")
2782 alignment)
2783 (and caption-above-p
2784 (org-string-nw-p caption)
2785 (concat caption "\\\\\n"))
2786 contents
2787 (and (not caption-above-p)
2788 (org-string-nw-p caption)
2789 (concat caption "\\\\\n"))
2790 "\\end{longtabu}\n"
2791 (and fontsize "}")))
2792 ;; Others.
2793 (t (concat (cond
2794 (float-env
2795 (concat (format "\\begin{%s}%s\n" float-env placement)
2796 (if caption-above-p caption "")
2797 (when centerp "\\centering\n")
2798 fontsize))
2799 (centerp (concat "\\begin{center}\n" fontsize))
2800 (fontsize (concat "{" fontsize)))
2801 (cond ((equal "tabu" table-env)
2802 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2803 (if width (format
2804 (if spreadp " spread %s " " to %s ")
2805 width) "")
2806 alignment
2807 contents))
2808 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2809 table-env
2810 (if width (format "{%s}" width) "")
2811 alignment
2812 contents
2813 table-env)))
2814 (cond
2815 (float-env
2816 (concat (if caption-above-p "" caption)
2817 (format "\n\\end{%s}" float-env)))
2818 (centerp "\n\\end{center}")
2819 (fontsize "}")))))))
2821 (defun org-latex--table.el-table (table info)
2822 "Return appropriate LaTeX code for a table.el table.
2824 TABLE is the table type element to transcode. INFO is a plist
2825 used as a communication channel.
2827 This function assumes TABLE has `table.el' as its `:type'
2828 property."
2829 (require 'table)
2830 ;; Ensure "*org-export-table*" buffer is empty.
2831 (with-current-buffer (get-buffer-create "*org-export-table*")
2832 (erase-buffer))
2833 (let ((output (with-temp-buffer
2834 (insert (org-element-property :value table))
2835 (goto-char 1)
2836 (re-search-forward "^[ \t]*|[^|]" nil t)
2837 (table-generate-source 'latex "*org-export-table*")
2838 (with-current-buffer "*org-export-table*"
2839 (org-trim (buffer-string))))))
2840 (kill-buffer (get-buffer "*org-export-table*"))
2841 ;; Remove left out comments.
2842 (while (string-match "^%.*\n" output)
2843 (setq output (replace-match "" t t output)))
2844 (let ((attr (org-export-read-attribute :attr_latex table)))
2845 (when (plist-get attr :rmlines)
2846 ;; When the "rmlines" attribute is provided, remove all hlines
2847 ;; but the the one separating heading from the table body.
2848 (let ((n 0) (pos 0))
2849 (while (and (< (length output) pos)
2850 (setq pos (string-match "^\\\\hline\n?" output pos)))
2851 (incf n)
2852 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2853 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2854 (plist-get info :latex-tables-centered))))
2855 (if (not centerp) output
2856 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2858 (defun org-latex--math-table (table info)
2859 "Return appropriate LaTeX code for a matrix.
2861 TABLE is the table type element to transcode. INFO is a plist
2862 used as a communication channel.
2864 This function assumes TABLE has `org' as its `:type' property and
2865 `inline-math' or `math' as its `:mode' attribute."
2866 (let* ((attr (org-export-read-attribute :attr_latex table))
2867 (env (or (plist-get attr :environment)
2868 (plist-get info :latex-default-table-environment)))
2869 (contents
2870 (mapconcat
2871 (lambda (row)
2872 ;; Ignore horizontal rules.
2873 (when (eq (org-element-property :type row) 'standard)
2874 ;; Return each cell unmodified.
2875 (concat
2876 (mapconcat
2877 (lambda (cell)
2878 (substring (org-element-interpret-data cell) 0 -1))
2879 (org-element-map row 'table-cell #'identity info) "&")
2880 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2881 "\n")))
2882 (org-element-map table 'table-row #'identity info) "")))
2883 (concat
2884 ;; Prefix.
2885 (plist-get attr :math-prefix)
2886 ;; Environment. Also treat special cases.
2887 (cond ((member env '("array" "tabular"))
2888 (let ((align (make-string
2889 (cdr (org-export-table-dimensions table info)) ?c)))
2890 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
2891 ((assoc env org-latex-table-matrix-macros)
2892 (format "\\%s%s{\n%s}"
2894 (or (plist-get attr :math-arguments) "")
2895 contents))
2896 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2897 ;; Suffix.
2898 (plist-get attr :math-suffix))))
2901 ;;;; Table Cell
2903 (defun org-latex-table-cell (table-cell contents info)
2904 "Transcode a TABLE-CELL element from Org to LaTeX.
2905 CONTENTS is the cell contents. INFO is a plist used as
2906 a communication channel."
2907 (concat
2908 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
2909 (if (and contents
2910 scientific-format
2911 (string-match orgtbl-exp-regexp contents))
2912 ;; Use appropriate format string for scientific
2913 ;; notation.
2914 (format scientific-format
2915 (match-string 1 contents)
2916 (match-string 2 contents))
2917 contents))
2918 (when (org-export-get-next-element table-cell info) " & ")))
2921 ;;;; Table Row
2923 (defun org-latex-table-row (table-row contents info)
2924 "Transcode a TABLE-ROW element from Org to LaTeX.
2925 CONTENTS is the contents of the row. INFO is a plist used as
2926 a communication channel."
2927 (let* ((attr (org-export-read-attribute :attr_latex
2928 (org-export-get-parent table-row)))
2929 (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
2930 (plist-get info :latex-tables-booktabs)))
2931 (longtablep
2932 (member (or (plist-get attr :environment)
2933 (plist-get info :latex-default-table-environment))
2934 '("longtable" "longtabu"))))
2935 (if (eq (org-element-property :type table-row) 'rule)
2936 (cond
2937 ((not booktabsp) "\\hline")
2938 ((not (org-export-get-previous-element table-row info)) "\\toprule")
2939 ((not (org-export-get-next-element table-row info)) "\\bottomrule")
2940 ((and longtablep
2941 (org-export-table-row-ends-header-p
2942 (org-export-get-previous-element table-row info) info))
2944 (t "\\midrule"))
2945 (concat
2946 ;; When BOOKTABS are activated enforce top-rule even when no
2947 ;; hline was specifically marked.
2948 (and booktabsp (not (org-export-get-previous-element table-row info))
2949 "\\toprule\n")
2950 contents "\\\\\n"
2951 (cond
2952 ;; Special case for long tables. Define header and footers.
2953 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2954 (let ((columns (cdr (org-export-table-dimensions
2955 (org-export-get-parent-table table-row) info))))
2956 (format "%s
2957 \\endfirsthead
2958 \\multicolumn{%d}{l}{%s} \\\\
2960 %s \\\\\n
2962 \\endhead
2963 %s\\multicolumn{%d}{r}{%s} \\\\
2964 \\endfoot
2965 \\endlastfoot"
2966 (if booktabsp "\\midrule" "\\hline")
2967 columns
2968 (org-latex--translate "Continued from previous page" info)
2969 (cond
2970 ((not (org-export-table-row-starts-header-p table-row info))
2972 (booktabsp "\\toprule\n")
2973 (t "\\hline\n"))
2974 contents
2975 (if booktabsp "\\midrule" "\\hline")
2976 (if booktabsp "\\midrule" "\\hline")
2977 columns
2978 (org-latex--translate "Continued on next page" info))))
2979 ;; When BOOKTABS are activated enforce bottom rule even when
2980 ;; no hline was specifically marked.
2981 ((and booktabsp (not (org-export-get-next-element table-row info)))
2982 "\\bottomrule"))))))
2985 ;;;; Target
2987 (defun org-latex-target (target contents info)
2988 "Transcode a TARGET object from Org to LaTeX.
2989 CONTENTS is nil. INFO is a plist holding contextual
2990 information."
2991 (format "\\label{%s}"
2992 (org-export-solidify-link-text (org-element-property :value target))))
2995 ;;;; Timestamp
2997 (defun org-latex-timestamp (timestamp contents info)
2998 "Transcode a TIMESTAMP object from Org to LaTeX.
2999 CONTENTS is nil. INFO is a plist holding contextual
3000 information."
3001 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
3002 (format
3003 (plist-get info
3004 (case (org-element-property :type timestamp)
3005 ((active active-range) :latex-active-timestamp-format)
3006 ((inactive inactive-range) :latex-inactive-timestamp-format)
3007 (otherwise :latex-diary-timestamp-format)))
3008 value)))
3011 ;;;; Underline
3013 (defun org-latex-underline (underline contents info)
3014 "Transcode UNDERLINE from Org to LaTeX.
3015 CONTENTS is the text with underline markup. INFO is a plist
3016 holding contextual information."
3017 (org-latex--text-markup contents 'underline info))
3020 ;;;; Verbatim
3022 (defun org-latex-verbatim (verbatim contents info)
3023 "Transcode a VERBATIM object from Org to LaTeX.
3024 CONTENTS is nil. INFO is a plist used as a communication
3025 channel."
3026 (org-latex--text-markup
3027 (org-element-property :value verbatim) 'verbatim info))
3030 ;;;; Verse Block
3032 (defun org-latex-verse-block (verse-block contents info)
3033 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3034 CONTENTS is verse block contents. INFO is a plist holding
3035 contextual information."
3036 (org-latex--wrap-label
3037 verse-block
3038 ;; In a verse environment, add a line break to each newline
3039 ;; character and change each white space at beginning of a line
3040 ;; into a space of 1 em. Also change each blank line with
3041 ;; a vertical space of 1 em.
3042 (format "\\begin{verse}\n%s\\end{verse}"
3043 (replace-regexp-in-string
3044 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
3045 (replace-regexp-in-string
3046 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3047 (replace-regexp-in-string
3048 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3049 contents nil t) nil t) nil t))))
3053 ;;; End-user functions
3055 ;;;###autoload
3056 (defun org-latex-export-as-latex
3057 (&optional async subtreep visible-only body-only ext-plist)
3058 "Export current buffer as a LaTeX buffer.
3060 If narrowing is active in the current buffer, only export its
3061 narrowed part.
3063 If a region is active, export that region.
3065 A non-nil optional argument ASYNC means the process should happen
3066 asynchronously. The resulting buffer should be accessible
3067 through the `org-export-stack' interface.
3069 When optional argument SUBTREEP is non-nil, export the sub-tree
3070 at point, extracting information from the headline properties
3071 first.
3073 When optional argument VISIBLE-ONLY is non-nil, don't export
3074 contents of hidden elements.
3076 When optional argument BODY-ONLY is non-nil, only write code
3077 between \"\\begin{document}\" and \"\\end{document}\".
3079 EXT-PLIST, when provided, is a property list with external
3080 parameters overriding Org default settings, but still inferior to
3081 file-local settings.
3083 Export is done in a buffer named \"*Org LATEX Export*\", which
3084 will be displayed when `org-export-show-temporary-export-buffer'
3085 is non-nil."
3086 (interactive)
3087 (org-export-to-buffer 'latex "*Org LATEX Export*"
3088 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
3090 ;;;###autoload
3091 (defun org-latex-convert-region-to-latex ()
3092 "Assume the current region has org-mode syntax, and convert it to LaTeX.
3093 This can be used in any buffer. For example, you can write an
3094 itemized list in org-mode syntax in an LaTeX buffer and use this
3095 command to convert it."
3096 (interactive)
3097 (org-export-replace-region-by 'latex))
3099 ;;;###autoload
3100 (defun org-latex-export-to-latex
3101 (&optional async subtreep visible-only body-only ext-plist)
3102 "Export current buffer to a LaTeX file.
3104 If narrowing is active in the current buffer, only export its
3105 narrowed part.
3107 If a region is active, export that region.
3109 A non-nil optional argument ASYNC means the process should happen
3110 asynchronously. The resulting file should be accessible through
3111 the `org-export-stack' interface.
3113 When optional argument SUBTREEP is non-nil, export the sub-tree
3114 at point, extracting information from the headline properties
3115 first.
3117 When optional argument VISIBLE-ONLY is non-nil, don't export
3118 contents of hidden elements.
3120 When optional argument BODY-ONLY is non-nil, only write code
3121 between \"\\begin{document}\" and \"\\end{document}\".
3123 EXT-PLIST, when provided, is a property list with external
3124 parameters overriding Org default settings, but still inferior to
3125 file-local settings."
3126 (interactive)
3127 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3128 (org-export-to-file 'latex outfile
3129 async subtreep visible-only body-only ext-plist)))
3131 ;;;###autoload
3132 (defun org-latex-export-to-pdf
3133 (&optional async subtreep visible-only body-only ext-plist)
3134 "Export current buffer to LaTeX then process through to PDF.
3136 If narrowing is active in the current buffer, only export its
3137 narrowed part.
3139 If a region is active, export that region.
3141 A non-nil optional argument ASYNC means the process should happen
3142 asynchronously. The resulting file should be accessible through
3143 the `org-export-stack' interface.
3145 When optional argument SUBTREEP is non-nil, export the sub-tree
3146 at point, extracting information from the headline properties
3147 first.
3149 When optional argument VISIBLE-ONLY is non-nil, don't export
3150 contents of hidden elements.
3152 When optional argument BODY-ONLY is non-nil, only write code
3153 between \"\\begin{document}\" and \"\\end{document}\".
3155 EXT-PLIST, when provided, is a property list with external
3156 parameters overriding Org default settings, but still inferior to
3157 file-local settings.
3159 Return PDF file's name."
3160 (interactive)
3161 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3162 (org-export-to-file 'latex outfile
3163 async subtreep visible-only body-only ext-plist
3164 (lambda (file) (org-latex-compile file)))))
3166 (defun org-latex-compile (texfile &optional snippet)
3167 "Compile a TeX file.
3169 TEXFILE is the name of the file being compiled. Processing is
3170 done through the command specified in `org-latex-pdf-process'.
3172 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3173 file used to preview a LaTeX snippet. In this case, do not
3174 create a log buffer and do not bother removing log files.
3176 Return PDF file name or an error if it couldn't be produced."
3177 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3178 (full-name (file-truename texfile))
3179 (out-dir (file-name-directory texfile))
3180 ;; Properly set working directory for compilation.
3181 (default-directory (if (file-name-absolute-p texfile)
3182 (file-name-directory full-name)
3183 default-directory))
3184 (time (current-time))
3185 warnings)
3186 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
3187 (save-window-excursion
3188 (cond
3189 ;; A function is provided: Apply it.
3190 ((functionp org-latex-pdf-process)
3191 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3192 ;; A list is provided: Replace %b, %f and %o with appropriate
3193 ;; values in each command before applying it. Output is
3194 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3195 ((consp org-latex-pdf-process)
3196 (let ((outbuf (and (not snippet)
3197 (get-buffer-create "*Org PDF LaTeX Output*"))))
3198 (dolist (command org-latex-pdf-process)
3199 (shell-command
3200 (replace-regexp-in-string
3201 "%b" (shell-quote-argument base-name)
3202 (replace-regexp-in-string
3203 "%f" (shell-quote-argument full-name)
3204 (replace-regexp-in-string
3205 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
3206 outbuf))
3207 ;; Collect standard errors from output buffer.
3208 (setq warnings (and (not snippet)
3209 (org-latex--collect-warnings outbuf)))))
3210 (t (error "No valid command to process to PDF")))
3211 (let ((pdffile (concat out-dir base-name ".pdf")))
3212 ;; Check for process failure. Provide collected errors if
3213 ;; possible.
3214 (if (or (not (file-exists-p pdffile))
3215 (time-less-p (nth 5 (file-attributes pdffile)) time))
3216 (error (format "PDF file %s wasn't produced" pdffile))
3217 ;; Else remove log files, when specified, and signal end of
3218 ;; process to user, along with any error encountered.
3219 (unless snippet
3220 (when org-latex-remove-logfiles
3221 (dolist (file (directory-files
3222 out-dir t
3223 (concat (regexp-quote base-name)
3224 "\\(?:\\.[0-9]+\\)?"
3225 "\\."
3226 (regexp-opt org-latex-logfiles-extensions))))
3227 (delete-file file)))
3228 (message (concat "PDF file produced"
3229 (cond
3230 ((eq warnings 'error) " with errors.")
3231 (warnings (concat " with warnings: " warnings))
3232 (t "."))))))
3233 ;; Return output file name.
3234 pdffile))))
3236 (defun org-latex--collect-warnings (buffer)
3237 "Collect some warnings from \"pdflatex\" command output.
3238 BUFFER is the buffer containing output. Return collected
3239 warnings types as a string, `error' if a LaTeX error was
3240 encountered or nil if there was none."
3241 (with-current-buffer buffer
3242 (save-excursion
3243 (goto-char (point-max))
3244 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3245 (if (re-search-forward "^!" nil t) 'error
3246 (let ((case-fold-search t)
3247 (warnings ""))
3248 (dolist (warning org-latex-known-warnings)
3249 (when (save-excursion (re-search-forward (car warning) nil t))
3250 (setq warnings (concat warnings " " (cdr warning)))))
3251 (org-string-nw-p (org-trim warnings))))))))
3253 ;;;###autoload
3254 (defun org-latex-publish-to-latex (plist filename pub-dir)
3255 "Publish an Org file to LaTeX.
3257 FILENAME is the filename of the Org file to be published. PLIST
3258 is the property list for the given project. PUB-DIR is the
3259 publishing directory.
3261 Return output file name."
3262 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3264 ;;;###autoload
3265 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3266 "Publish an Org file to PDF (via LaTeX).
3268 FILENAME is the filename of the Org file to be published. PLIST
3269 is the property list for the given project. PUB-DIR is the
3270 publishing directory.
3272 Return output file name."
3273 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3274 ;; in working directory and then moved to publishing directory.
3275 (org-publish-attachment
3276 plist
3277 (org-latex-compile
3278 (org-publish-org-to
3279 'latex filename ".tex" plist (file-name-directory filename)))
3280 pub-dir))
3283 (provide 'ox-latex)
3285 ;; Local variables:
3286 ;; generated-autoload-file: "org-loaddefs.el"
3287 ;; End:
3289 ;;; ox-latex.el ends here