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