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