org-texi: Fix typo
[org-mode/org-tableheadings.git] / lisp / ox-latex.el
blobd64af87d7897f1e438dc229b655778b2852549eb
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 '(table)
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 :version "25.1"
230 :package-version '(Org . "8.3")
231 :type '(choice
232 (const :tag "For all elements" t)
233 (const :tag "For no element" nil)
234 (set :tag "For the following elements only" :greedy t
235 (const :tag "Images" image)
236 (const :tag "Tables" table)
237 (const :tag "Source code" src-block)
238 (const :tag "Special blocks" special-block))))
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 "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
957 "The list of file extensions to consider as LaTeX logfiles.
958 The logfiles will be removed if `org-latex-remove-logfiles' is
959 non-nil."
960 :group 'org-export-latex
961 :version "25.1"
962 :package-version '(Org . "8.3")
963 :type '(repeat (string :tag "Extension")))
965 (defcustom org-latex-remove-logfiles t
966 "Non-nil means remove the logfiles produced by PDF production.
967 By default, logfiles are files with these extensions: .aux, .idx,
968 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
969 logfiles to remove, set `org-latex-logfiles-extensions'."
970 :group 'org-export-latex
971 :type 'boolean)
973 (defcustom org-latex-known-warnings
974 '(("Reference.*?undefined" . "[undefined reference]")
975 ("Runaway argument" . "[runaway argument]")
976 ("Underfull \\hbox" . "[underfull hbox]")
977 ("Overfull \\hbox" . "[overfull hbox]")
978 ("Citation.*?undefined" . "[undefined citation]")
979 ("Undefined control sequence" . "[undefined control sequence]"))
980 "Alist of regular expressions and associated messages for the user.
981 The regular expressions are used to find possible warnings in the
982 log of a latex-run. These warnings will be reported after
983 calling `org-latex-compile'."
984 :group 'org-export-latex
985 :version "25.1"
986 :package-version '(Org . "8.3")
987 :type '(repeat
988 (cons
989 (string :tag "Regexp")
990 (string :tag "Message"))))
994 ;;; Internal Functions
996 (defun org-latex--caption-above-p (element info)
997 "Non nil when caption is expected to be located above ELEMENT.
998 INFO is a plist holding contextual information."
999 (let ((above (plist-get info :latex-caption-above)))
1000 (if (symbolp above) above
1001 (let ((type (org-element-type element)))
1002 (memq (if (eq type 'link) 'image type) above)))))
1004 (defun org-latex--caption/label-string (element info)
1005 "Return caption and label LaTeX string for ELEMENT.
1007 INFO is a plist holding contextual information. If there's no
1008 caption nor label, return the empty string.
1010 For non-floats, see `org-latex--wrap-label'."
1011 (let* ((label (org-element-property :name element))
1012 (label-str (if (not (org-string-nw-p label)) ""
1013 (format "\\label{%s}"
1014 (org-export-solidify-link-text label))))
1015 (main (org-export-get-caption element))
1016 (short (org-export-get-caption element t))
1017 (caption-from-attr-latex (org-export-read-attribute :attr_latex element :caption)))
1018 (cond
1019 ((org-string-nw-p caption-from-attr-latex)
1020 (concat caption-from-attr-latex "\n"))
1021 ((and (not main) (equal label-str "")) "")
1022 ((not main) (concat label-str "\n"))
1023 ;; Option caption format with short name.
1024 (short (format "\\caption[%s]{%s%s}\n"
1025 (org-export-data short info)
1026 label-str
1027 (org-export-data main info)))
1028 ;; Standard caption format.
1029 (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
1031 (defun org-latex-guess-inputenc (header)
1032 "Set the coding system in inputenc to what the buffer is.
1034 HEADER is the LaTeX header string. This function only applies
1035 when specified inputenc option is \"AUTO\".
1037 Return the new header, as a string."
1038 (let* ((cs (or (ignore-errors
1039 (latexenc-coding-system-to-inputenc
1040 (or org-export-coding-system buffer-file-coding-system)))
1041 "utf8")))
1042 (if (not cs) header
1043 ;; First translate if that is requested.
1044 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
1045 ;; Then find the \usepackage statement and replace the option.
1046 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1047 cs header t nil 1))))
1049 (defun org-latex-guess-babel-language (header info)
1050 "Set Babel's language according to LANGUAGE keyword.
1052 HEADER is the LaTeX header string. INFO is the plist used as
1053 a communication channel.
1055 Insertion of guessed language only happens when Babel package has
1056 explicitly been loaded. Then it is added to the rest of
1057 package's options.
1059 The argument to Babel may be \"AUTO\" which is then replaced with
1060 the language of the document or `org-export-default-language'
1061 unless language in question is already loaded.
1063 Return the new header."
1064 (let ((language-code (plist-get info :language)))
1065 ;; If no language is set or Babel package is not loaded, return
1066 ;; HEADER as-is.
1067 (if (or (not (stringp language-code))
1068 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
1069 header
1070 (let ((options (save-match-data
1071 (org-split-string (match-string 1 header) ",[ \t]*")))
1072 (language (cdr (assoc language-code
1073 org-latex-babel-language-alist))))
1074 ;; If LANGUAGE is already loaded, return header without AUTO.
1075 ;; Otherwise, replace AUTO with language or append language if
1076 ;; AUTO is not present.
1077 (replace-match
1078 (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
1079 (cond ((member language options) (delete "AUTO" options))
1080 ((member "AUTO" options) options)
1081 (t (append options (list language))))
1082 ", ")
1083 t nil header 1)))))
1085 (defun org-latex--find-verb-separator (s)
1086 "Return a character not used in string S.
1087 This is used to choose a separator for constructs like \\verb."
1088 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1089 (loop for c across ll
1090 when (not (string-match (regexp-quote (char-to-string c)) s))
1091 return (char-to-string c))))
1093 (defun org-latex--make-option-string (options)
1094 "Return a comma separated string of keywords and values.
1095 OPTIONS is an alist where the key is the options keyword as
1096 a string, and the value a list containing the keyword value, or
1097 nil."
1098 (mapconcat (lambda (pair)
1099 (concat (first pair)
1100 (when (> (length (second pair)) 0)
1101 (concat "=" (second pair)))))
1102 options
1103 ","))
1105 (defun org-latex--wrap-label (element output)
1106 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1107 This function shouldn't be used for floats. See
1108 `org-latex--caption/label-string'."
1109 (let ((label (org-element-property :name element)))
1110 (if (not (and (org-string-nw-p output) (org-string-nw-p label))) output
1111 (concat (format "\\phantomsection\n\\label{%s}\n"
1112 (org-export-solidify-link-text label))
1113 output))))
1115 (defun org-latex--text-markup (text markup info)
1116 "Format TEXT depending on MARKUP text markup.
1117 INFO is a plist used as a communication channel. See
1118 `org-latex-text-markup-alist' for details."
1119 (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
1120 (cond
1121 ;; No format string: Return raw text.
1122 ((not fmt) text)
1123 ;; Handle the `verb' special case: Find an appropriate separator
1124 ;; and use "\\verb" command.
1125 ((eq 'verb fmt)
1126 (let ((separator (org-latex--find-verb-separator text)))
1127 (concat "\\verb" separator
1128 (replace-regexp-in-string "\n" " " text)
1129 separator)))
1130 ;; Handle the `protectedtexttt' special case: Protect some
1131 ;; special chars and use "\texttt{%s}" format string.
1132 ((eq 'protectedtexttt fmt)
1133 (let ((start 0)
1134 (trans '(("\\" . "\\textbackslash{}")
1135 ("~" . "\\textasciitilde{}")
1136 ("^" . "\\textasciicircum{}")))
1137 (rtn "")
1138 char)
1139 (while (string-match "[\\{}$%&_#~^]" text)
1140 (setq char (match-string 0 text))
1141 (if (> (match-beginning 0) 0)
1142 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
1143 (setq text (substring text (1+ (match-beginning 0))))
1144 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1145 rtn (concat rtn char)))
1146 (setq text (concat rtn text)
1147 fmt "\\texttt{%s}")
1148 (while (string-match "--" text)
1149 (setq text (replace-match "-{}-" t t text)))
1150 (format fmt text)))
1151 ;; Else use format string.
1152 (t (format fmt text)))))
1154 (defun org-latex--delayed-footnotes-definitions (element info)
1155 "Return footnotes definitions in ELEMENT as a string.
1157 INFO is a plist used as a communication channel.
1159 Footnotes definitions are returned within \"\\footnotetxt{}\"
1160 commands.
1162 This function is used within constructs that don't support
1163 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1164 \"\\footnotemark\" is used within the construct and the function
1165 just outside of it."
1166 (mapconcat
1167 (lambda (ref)
1168 (format
1169 "\\footnotetext[%s]{%s}"
1170 (org-export-get-footnote-number ref info)
1171 (org-trim
1172 (org-export-data
1173 (org-export-get-footnote-definition ref info) info))))
1174 ;; Find every footnote reference in ELEMENT.
1175 (let* (all-refs
1176 search-refs ; For byte-compiler.
1177 (search-refs
1178 (function
1179 (lambda (data)
1180 ;; Return a list of all footnote references never seen
1181 ;; before in DATA.
1182 (org-element-map data 'footnote-reference
1183 (lambda (ref)
1184 (when (org-export-footnote-first-reference-p ref info)
1185 (push ref all-refs)
1186 (when (eq (org-element-property :type ref) 'standard)
1187 (funcall search-refs
1188 (org-export-get-footnote-definition ref info)))))
1189 info)
1190 (reverse all-refs)))))
1191 (funcall search-refs element))
1192 ""))
1194 (defun org-latex--translate (s info)
1195 "Translate string S according to specified language.
1196 INFO is a plist used as a communication channel."
1197 (org-export-translate s :latex info))
1201 ;;; Template
1203 (defun org-latex-template (contents info)
1204 "Return complete document string after LaTeX conversion.
1205 CONTENTS is the transcoded contents string. INFO is a plist
1206 holding export options."
1207 (let ((title (org-export-data (plist-get info :title) info)))
1208 (concat
1209 ;; Time-stamp.
1210 (and (plist-get info :time-stamp-file)
1211 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1212 ;; Document class and packages.
1213 (let* ((class (plist-get info :latex-class))
1214 (class-options (plist-get info :latex-class-options))
1215 (header (nth 1 (assoc class (plist-get info :latex-classes))))
1216 (document-class-string
1217 (and (stringp header)
1218 (if (not class-options) header
1219 (replace-regexp-in-string
1220 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1221 class-options header t nil 1)))))
1222 (if (not document-class-string)
1223 (user-error "Unknown LaTeX class `%s'" class)
1224 (org-latex-guess-babel-language
1225 (org-latex-guess-inputenc
1226 (org-element-normalize-string
1227 (org-splice-latex-header
1228 document-class-string
1229 org-latex-default-packages-alist
1230 org-latex-packages-alist nil
1231 (concat (org-element-normalize-string
1232 (plist-get info :latex-header))
1233 (plist-get info :latex-header-extra)))))
1234 info)))
1235 ;; Possibly limit depth for headline numbering.
1236 (let ((sec-num (plist-get info :section-numbers)))
1237 (when (integerp sec-num)
1238 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1239 ;; Author.
1240 (let ((author (and (plist-get info :with-author)
1241 (let ((auth (plist-get info :author)))
1242 (and auth (org-export-data auth info)))))
1243 (email (and (plist-get info :with-email)
1244 (org-export-data (plist-get info :email) info))))
1245 (cond ((and author email (not (string= "" email)))
1246 (format "\\author{%s\\thanks{%s}}\n" author email))
1247 ((or author email) (format "\\author{%s}\n" (or author email)))))
1248 ;; Date.
1249 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1250 (format "\\date{%s}\n" (org-export-data date info)))
1251 ;; Title
1252 (format "\\title{%s}\n" title)
1253 ;; Hyperref options.
1254 (format-spec (plist-get info :latex-hyperref-template)
1255 (format-spec-make
1256 ?k (or (plist-get info :keywords) "")
1257 ?d (or (plist-get info :description)"")
1258 ?c (if (plist-get info :with-creator)
1259 (plist-get info :creator)
1260 "")))
1261 ;; Document start.
1262 "\\begin{document}\n\n"
1263 ;; Title command.
1264 (let ((command (plist-get info :latex-title-command)))
1265 (org-element-normalize-string
1266 (cond ((string= "" title) nil)
1267 ((not (stringp command)) nil)
1268 ((string-match "\\(?:[^%]\\|^\\)%s" command)
1269 (format command title))
1270 (t command))))
1271 ;; Table of contents.
1272 (let ((depth (plist-get info :with-toc)))
1273 (when depth
1274 (concat (when (wholenump depth)
1275 (format "\\setcounter{tocdepth}{%d}\n" depth))
1276 (plist-get info :latex-toc-command))))
1277 ;; Document's body.
1278 contents
1279 ;; Creator.
1280 (let ((creator-info (plist-get info :with-creator)))
1281 (cond
1282 ((not creator-info) "")
1283 ((eq creator-info 'comment)
1284 (format "%% %s\n" (plist-get info :creator)))
1285 (t (concat (plist-get info :creator) "\n"))))
1286 ;; Document end.
1287 "\\end{document}")))
1291 ;;; Transcode Functions
1293 ;;;; Bold
1295 (defun org-latex-bold (bold contents info)
1296 "Transcode BOLD from Org to LaTeX.
1297 CONTENTS is the text with bold markup. INFO is a plist holding
1298 contextual information."
1299 (org-latex--text-markup contents 'bold info))
1302 ;;;; Center Block
1304 (defun org-latex-center-block (center-block contents info)
1305 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1306 CONTENTS holds the contents of the center block. INFO is a plist
1307 holding contextual information."
1308 (org-latex--wrap-label
1309 center-block
1310 (format "\\begin{center}\n%s\\end{center}" contents)))
1313 ;;;; Clock
1315 (defun org-latex-clock (clock contents info)
1316 "Transcode a CLOCK element from Org to LaTeX.
1317 CONTENTS is nil. INFO is a plist holding contextual
1318 information."
1319 (concat
1320 "\\noindent"
1321 (format "\\textbf{%s} " org-clock-string)
1322 (format (plist-get info :latex-inactive-timestamp-format)
1323 (concat (org-timestamp-translate (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 ((org-element-lineage footnote-reference
1429 '(footnote-reference footnote-definition table-cell))
1430 "\\footnotemark")
1431 ;; Otherwise, define it with \footnote command.
1433 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1434 (concat
1435 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1436 ;; Retrieve all footnote references within the footnote and
1437 ;; add their definition after it, since LaTeX doesn't support
1438 ;; them inside.
1439 (org-latex--delayed-footnotes-definitions def info)))))))
1442 ;;;; Headline
1444 (defun org-latex-headline (headline contents info)
1445 "Transcode a HEADLINE element from Org to LaTeX.
1446 CONTENTS holds the contents of the headline. INFO is a plist
1447 holding contextual information."
1448 (unless (org-element-property :footnote-section-p headline)
1449 (let* ((class (plist-get info :latex-class))
1450 (level (org-export-get-relative-level headline info))
1451 (numberedp (org-export-numbered-headline-p headline info))
1452 (class-sectioning (assoc class (plist-get info :latex-classes)))
1453 ;; Section formatting will set two placeholders: one for
1454 ;; the title and the other for the contents.
1455 (section-fmt
1456 (let ((sec (if (functionp (nth 2 class-sectioning))
1457 (funcall (nth 2 class-sectioning) level numberedp)
1458 (nth (1+ level) class-sectioning))))
1459 (cond
1460 ;; No section available for that LEVEL.
1461 ((not sec) nil)
1462 ;; Section format directly returned by a function. Add
1463 ;; placeholder for contents.
1464 ((stringp sec) (concat sec "\n%s"))
1465 ;; (numbered-section . unnumbered-section)
1466 ((not (consp (cdr sec)))
1467 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1468 ;; (numbered-open numbered-close)
1469 ((= (length sec) 2)
1470 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1471 ;; (num-in num-out no-num-in no-num-out)
1472 ((= (length sec) 4)
1473 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1474 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1475 ;; Create a temporary export back-end that hard-codes
1476 ;; "\underline" within "\section" and alike.
1477 (section-back-end
1478 (org-export-create-backend
1479 :parent 'latex
1480 :transcoders
1481 '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
1482 (text
1483 (org-export-data-with-backend
1484 (org-element-property :title headline) section-back-end info))
1485 (todo
1486 (and (plist-get info :with-todo-keywords)
1487 (let ((todo (org-element-property :todo-keyword headline)))
1488 (and todo (org-export-data todo info)))))
1489 (todo-type (and todo (org-element-property :todo-type headline)))
1490 (tags (and (plist-get info :with-tags)
1491 (org-export-get-tags headline info)))
1492 (priority (and (plist-get info :with-priority)
1493 (org-element-property :priority headline)))
1494 ;; Create the headline text along with a no-tag version.
1495 ;; The latter is required to remove tags from toc.
1496 (full-text (funcall (plist-get info :latex-format-headline-function)
1497 todo todo-type priority text tags info))
1498 ;; Associate \label to the headline for internal links.
1499 (headline-label
1500 (format "\\label{%s}\n"
1501 (or (and (plist-get info :latex-custom-id-labels)
1502 (org-element-property :CUSTOM_ID headline))
1503 (org-export-get-headline-id headline info))))
1504 (pre-blanks
1505 (make-string (org-element-property :pre-blank headline) 10)))
1506 (if (or (not section-fmt) (org-export-low-level-p headline info))
1507 ;; This is a deep sub-tree: export it as a list item. Also
1508 ;; export as items headlines for which no section format has
1509 ;; been found.
1510 (let ((low-level-body
1511 (concat
1512 ;; If headline is the first sibling, start a list.
1513 (when (org-export-first-sibling-p headline info)
1514 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1515 ;; Itemize headline
1516 "\\item"
1517 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
1518 "\\relax")
1519 " " full-text "\n"
1520 headline-label
1521 pre-blanks
1522 contents)))
1523 ;; If headline is not the last sibling simply return
1524 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1525 ;; any blank line.
1526 (if (not (org-export-last-sibling-p headline info)) low-level-body
1527 (replace-regexp-in-string
1528 "[ \t\n]*\\'"
1529 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1530 low-level-body)))
1531 ;; This is a standard headline. Export it as a section. Add
1532 ;; an alternative heading when possible, and when this is not
1533 ;; identical to the usual heading.
1534 (let ((opt-title
1535 (funcall (plist-get info :latex-format-headline-function)
1536 todo todo-type priority
1537 (org-export-data-with-backend
1538 (org-export-get-alt-title headline info)
1539 section-back-end info)
1540 (and (eq (plist-get info :with-tags) t) tags)
1541 info))
1542 ;; Maybe end local TOC (see `org-latex-keyword').
1543 (contents
1544 (concat
1545 contents
1546 (let ((case-fold-search t)
1547 (section
1548 (let ((first (car (org-element-contents headline))))
1549 (and (eq (org-element-type first) 'section) first))))
1550 (org-element-map section 'keyword
1551 (lambda (k)
1552 (and (equal (org-element-property :key k) "TOC")
1553 (let ((v (org-element-property :value k)))
1554 (and (org-string-match-p "\\<headlines\\>" v)
1555 (org-string-match-p "\\<local\\>" v)
1556 (format "\\stopcontents[level-%d]" level)))))
1557 info t)))))
1558 (if (and numberedp opt-title
1559 (not (equal opt-title full-text))
1560 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
1561 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1562 ;; Replace square brackets with parenthesis
1563 ;; since square brackets are not supported in
1564 ;; optional arguments.
1565 (replace-regexp-in-string
1566 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1567 full-text
1568 (concat headline-label pre-blanks contents))
1569 ;; Impossible to add an alternative heading. Fallback to
1570 ;; regular sectioning format string.
1571 (format section-fmt full-text
1572 (concat headline-label pre-blanks contents))))))))
1574 (defun org-latex-format-headline-default-function
1575 (todo todo-type priority text tags info)
1576 "Default format function for a headline.
1577 See `org-latex-format-headline-function' for details."
1578 (concat
1579 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1580 (and priority (format "\\framebox{\\#%c} " priority))
1581 text
1582 (and tags
1583 (format "\\hfill{}\\textsc{%s}"
1584 (mapconcat (lambda (tag) (org-latex-plain-text tag info))
1585 tags ":")))))
1588 ;;;; Horizontal Rule
1590 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1591 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1592 CONTENTS is nil. INFO is a plist holding contextual information."
1593 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1594 (prev (org-export-get-previous-element horizontal-rule info)))
1595 (concat
1596 ;; Make sure the rule doesn't start at the end of the current
1597 ;; line by separating it with a blank line from previous element.
1598 (when (and prev
1599 (let ((prev-blank (org-element-property :post-blank prev)))
1600 (or (not prev-blank) (zerop prev-blank))))
1601 "\n")
1602 (org-latex--wrap-label
1603 horizontal-rule
1604 (format "\\rule{%s}{%s}"
1605 (or (plist-get attr :width) "\\linewidth")
1606 (or (plist-get attr :thickness) "0.5pt"))))))
1609 ;;;; Inline Src Block
1611 (defun org-latex-inline-src-block (inline-src-block contents info)
1612 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1613 CONTENTS holds the contents of the item. INFO is a plist holding
1614 contextual information."
1615 (let* ((code (org-element-property :value inline-src-block))
1616 (separator (org-latex--find-verb-separator code)))
1617 (case (plist-get info :latex-listings)
1618 ;; Do not use a special package: transcode it verbatim.
1619 ((nil) (concat "\\verb" separator code separator))
1620 ;; Use minted package.
1621 (minted
1622 (let* ((org-lang (org-element-property :language inline-src-block))
1623 (mint-lang (or (cadr (assq (intern org-lang)
1624 (plist-get info :latex-minted-langs)))
1625 (downcase org-lang)))
1626 (options (org-latex--make-option-string
1627 (plist-get info :latex-minted-options))))
1628 (concat (format "\\mint%s{%s}"
1629 (if (string= options "") "" (format "[%s]" options))
1630 mint-lang)
1631 separator code separator)))
1632 ;; Use listings package.
1633 (otherwise
1634 ;; Maybe translate language's name.
1635 (let* ((org-lang (org-element-property :language inline-src-block))
1636 (lst-lang (or (cadr (assq (intern org-lang)
1637 (plist-get info :latex-listings-langs)))
1638 org-lang))
1639 (options (org-latex--make-option-string
1640 (append (plist-get info :latex-listings-options)
1641 `(("language" ,lst-lang))))))
1642 (concat (format "\\lstinline[%s]" options)
1643 separator code separator))))))
1646 ;;;; Inlinetask
1648 (defun org-latex-inlinetask (inlinetask contents info)
1649 "Transcode an INLINETASK element from Org to LaTeX.
1650 CONTENTS holds the contents of the block. INFO is a plist
1651 holding contextual information."
1652 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1653 (todo (and (plist-get info :with-todo-keywords)
1654 (let ((todo (org-element-property :todo-keyword inlinetask)))
1655 (and todo (org-export-data todo info)))))
1656 (todo-type (org-element-property :todo-type inlinetask))
1657 (tags (and (plist-get info :with-tags)
1658 (org-export-get-tags inlinetask info)))
1659 (priority (and (plist-get info :with-priority)
1660 (org-element-property :priority inlinetask)))
1661 (contents (concat
1662 (let ((label (org-element-property :CUSTOM_ID inlinetask)))
1663 (and label (format "\\label{%s}\n" label)))
1664 contents)))
1665 (funcall (plist-get info :latex-format-inlinetask-function)
1666 todo todo-type priority title tags contents info)))
1668 (defun org-latex-format-inlinetask-default-function
1669 (todo todo-type priority title tags contents info)
1670 "Default format function for a inlinetasks.
1671 See `org-latex-format-inlinetask-function' for details."
1672 (let ((full-title
1673 (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1674 (when priority (format "\\framebox{\\#%c} " priority))
1675 title
1676 (when tags
1677 (format "\\hfill{}\\textsc{:%s:}"
1678 (mapconcat
1679 (lambda (tag) (org-latex-plain-text tag info))
1680 tags ":"))))))
1681 (concat "\\begin{center}\n"
1682 "\\fbox{\n"
1683 "\\begin{minipage}[c]{.6\\textwidth}\n"
1684 full-title "\n\n"
1685 (and (org-string-nw-p contents)
1686 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
1687 "\\end{minipage}\n"
1688 "}\n"
1689 "\\end{center}")))
1692 ;;;; Italic
1694 (defun org-latex-italic (italic contents info)
1695 "Transcode ITALIC from Org to LaTeX.
1696 CONTENTS is the text with italic markup. INFO is a plist holding
1697 contextual information."
1698 (org-latex--text-markup contents 'italic info))
1701 ;;;; Item
1703 (defun org-latex-item (item contents info)
1704 "Transcode an ITEM element from Org to LaTeX.
1705 CONTENTS holds the contents of the item. INFO is a plist holding
1706 contextual information."
1707 (let* ((counter
1708 (let ((count (org-element-property :counter item))
1709 (level
1710 ;; Determine level of current item to determine the
1711 ;; correct LaTeX counter to use (enumi, enumii...).
1712 (let ((parent item) (level 0))
1713 (while (memq (org-element-type
1714 (setq parent (org-export-get-parent parent)))
1715 '(plain-list item))
1716 (when (and (eq (org-element-type parent) 'plain-list)
1717 (eq (org-element-property :type parent)
1718 'ordered))
1719 (incf level)))
1720 level)))
1721 (and count
1722 (< level 5)
1723 (format "\\setcounter{enum%s}{%s}\n"
1724 (nth (1- level) '("i" "ii" "iii" "iv"))
1725 (1- count)))))
1726 (checkbox (case (org-element-property :checkbox item)
1727 (on "$\\boxtimes$ ")
1728 (off "$\\square$ ")
1729 (trans "$\\boxminus$ ")))
1730 (tag (let ((tag (org-element-property :tag item)))
1731 ;; Check-boxes must belong to the tag.
1732 (and tag (format "[{%s}] "
1733 (concat checkbox
1734 (org-export-data tag info)))))))
1735 (concat counter
1736 "\\item"
1737 (cond
1738 (tag)
1739 (checkbox (concat " " checkbox))
1740 ;; Without a tag or a check-box, if CONTENTS starts with
1741 ;; an opening square bracket, add "\relax" to "\item",
1742 ;; unless the brackets comes from an initial export
1743 ;; snippet (i.e. it is inserted willingly by the user).
1744 ((and contents
1745 (org-string-match-p "\\`[ \t]*\\[" contents)
1746 (not (let ((e (car (org-element-contents item))))
1747 (and (eq (org-element-type e) 'paragraph)
1748 (let ((o (car (org-element-contents e))))
1749 (and (eq (org-element-type o) 'export-snippet)
1750 (eq (org-export-snippet-backend o)
1751 'latex)))))))
1752 "\\relax ")
1753 (t " "))
1754 (and contents (org-trim contents))
1755 ;; If there are footnotes references in tag, be sure to
1756 ;; add their definition at the end of the item. This
1757 ;; workaround is necessary since "\footnote{}" command is
1758 ;; not supported in tags.
1759 (and tag
1760 (org-latex--delayed-footnotes-definitions
1761 (org-element-property :tag item) info)))))
1764 ;;;; Keyword
1766 (defun org-latex-keyword (keyword contents info)
1767 "Transcode a KEYWORD element from Org to LaTeX.
1768 CONTENTS is nil. INFO is a plist holding contextual information."
1769 (let ((key (org-element-property :key keyword))
1770 (value (org-element-property :value keyword)))
1771 (cond
1772 ((string= key "LATEX") value)
1773 ((string= key "INDEX") (format "\\index{%s}" value))
1774 ((string= key "TOC")
1775 (let ((case-fold-search t))
1776 (cond
1777 ((org-string-match-p "\\<headlines\\>" value)
1778 (let* ((localp (org-string-match-p "\\<local\\>" value))
1779 (parent (org-element-lineage keyword '(headline)))
1780 (level (if (not (and localp parent)) 0
1781 (org-export-get-relative-level parent info)))
1782 (depth
1783 (and (string-match "\\<[0-9]+\\>" value)
1784 (format
1785 "\\setcounter{tocdepth}{%d}"
1786 (+ (string-to-number (match-string 0 value)) level)))))
1787 (if (and localp parent)
1788 ;; Start local TOC, assuming package "titletoc" is
1789 ;; required.
1790 (format "\\startcontents[level-%d]
1791 \\printcontents[level-%d]{}{0}{%s}"
1792 level level (or depth ""))
1793 (concat depth (and depth "\n") "\\tableofcontents"))))
1794 ((org-string-match-p "\\<tables\\>" value) "\\listoftables")
1795 ((org-string-match-p "\\<listings\\>" value)
1796 (case (plist-get info :latex-listings)
1797 ((nil) "\\listoffigures")
1798 (minted "\\listoflistings")
1799 (otherwise "\\lstlistoflistings")))))))))
1802 ;;;; Latex Environment
1804 (defun org-latex-latex-environment (latex-environment contents info)
1805 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1806 CONTENTS is nil. INFO is a plist holding contextual information."
1807 (when (plist-get info :with-latex)
1808 (let ((label (org-element-property :name latex-environment))
1809 (value (org-remove-indentation
1810 (org-element-property :value latex-environment))))
1811 (if (not (org-string-nw-p label)) value
1812 ;; Environment is labeled: label must be within the environment
1813 ;; (otherwise, a reference pointing to that element will count
1814 ;; the section instead).
1815 (with-temp-buffer
1816 (insert value)
1817 (goto-char (point-min))
1818 (forward-line)
1819 (insert
1820 (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1821 (buffer-string))))))
1824 ;;;; Latex Fragment
1826 (defun org-latex-latex-fragment (latex-fragment contents info)
1827 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1828 CONTENTS is nil. INFO is a plist holding contextual information."
1829 (let ((value (org-element-property :value latex-fragment)))
1830 ;; Trim math markers since the fragment is enclosed within
1831 ;; a latex-math-block object anyway.
1832 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
1833 (match-string 2 value))
1834 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
1835 (match-string 1 value))
1836 (t value))))
1839 ;;;; Line Break
1841 (defun org-latex-line-break (line-break contents info)
1842 "Transcode a LINE-BREAK object from Org to LaTeX.
1843 CONTENTS is nil. INFO is a plist holding contextual information."
1844 "\\\\\n")
1847 ;;;; Link
1849 (defun org-latex--inline-image (link info)
1850 "Return LaTeX code for an inline image.
1851 LINK is the link pointing to the inline image. INFO is a plist
1852 used as a communication channel."
1853 (let* ((parent (org-export-get-parent-element link))
1854 (path (let ((raw-path (org-element-property :path link)))
1855 (if (not (file-name-absolute-p raw-path)) raw-path
1856 (expand-file-name raw-path))))
1857 (filetype (file-name-extension path))
1858 (caption (org-latex--caption/label-string parent info))
1859 (caption-above-p (org-latex--caption-above-p link info))
1860 ;; Retrieve latex attributes from the element around.
1861 (attr (org-export-read-attribute :attr_latex parent))
1862 (float (let ((float (plist-get attr :float)))
1863 (cond ((and (not float) (plist-member attr :float)) nil)
1864 ((string= float "wrap") 'wrap)
1865 ((string= float "sideways") 'sideways)
1866 ((string= float "multicolumn") 'multicolumn)
1867 ((or float
1868 (org-element-property :caption parent)
1869 (org-string-nw-p (plist-get attr :caption)))
1870 'figure))))
1871 (placement
1872 (let ((place (plist-get attr :placement)))
1873 (cond
1874 (place (format "%s" place))
1875 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1876 ((eq float 'figure)
1877 (format "[%s]" (plist-get info :latex-default-figure-position)))
1878 (t ""))))
1879 (comment-include (if (plist-get attr :comment-include) "%" ""))
1880 ;; It is possible to specify width and height in the
1881 ;; ATTR_LATEX line, and also via default variables.
1882 (width (cond ((plist-get attr :width))
1883 ((plist-get attr :height) "")
1884 ((eq float 'wrap) "0.48\\textwidth")
1885 (t (plist-get info :latex-image-default-width))))
1886 (height (cond ((plist-get attr :height))
1887 ((or (plist-get attr :width)
1888 (memq float '(figure wrap))) "")
1889 (t (plist-get info :latex-image-default-height))))
1890 (options (let ((opt (or (plist-get attr :options)
1891 (plist-get info :latex-image-default-option))))
1892 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
1893 (match-string 1 opt))))
1894 image-code)
1895 (if (member filetype '("tikz" "pgf"))
1896 ;; For tikz images:
1897 ;; - use \input to read in image file.
1898 ;; - if options are present, wrap in a tikzpicture environment.
1899 ;; - if width or height are present, use \resizebox to change
1900 ;; the image size.
1901 (progn
1902 (setq image-code (format "\\input{%s}" path))
1903 (when (org-string-nw-p options)
1904 (setq image-code
1905 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1906 options
1907 image-code)))
1908 (when (or (org-string-nw-p width) (org-string-nw-p height))
1909 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
1910 (if (org-string-nw-p width) width "!")
1911 (if (org-string-nw-p height) height "!")
1912 image-code))))
1913 ;; For other images:
1914 ;; - add width and height to options.
1915 ;; - include the image with \includegraphics.
1916 (when (org-string-nw-p width)
1917 (setq options (concat options ",width=" width)))
1918 (when (org-string-nw-p height)
1919 (setq options (concat options ",height=" height)))
1920 (let ((search-option (org-element-property :search-option link)))
1921 (when (and search-option
1922 (equal filetype "pdf")
1923 (org-string-match-p "\\`[0-9]+\\'" search-option)
1924 (not (org-string-match-p "page=" options)))
1925 (setq options (concat options ",page=" search-option))))
1926 (setq image-code
1927 (format "\\includegraphics%s{%s}"
1928 (cond ((not (org-string-nw-p options)) "")
1929 ((= (aref options 0) ?,)
1930 (format "[%s]"(substring options 1)))
1931 (t (format "[%s]" options)))
1932 path))
1933 (when (equal filetype "svg")
1934 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
1935 "\\includesvg"
1936 image-code
1937 nil t))
1938 (setq image-code (replace-regexp-in-string "\\.svg}"
1940 image-code
1941 nil t))))
1942 ;; Return proper string, depending on FLOAT.
1943 (case float
1944 (wrap (format "\\begin{wrapfigure}%s
1945 %s\\centering
1946 %s%s
1947 %s\\end{wrapfigure}"
1948 placement
1949 (if caption-above-p caption "")
1950 comment-include image-code
1951 (if caption-above-p "" caption)))
1952 (sideways (format "\\begin{sidewaysfigure}
1953 %s\\centering
1954 %s%s
1955 %s\\end{sidewaysfigure}"
1956 (if caption-above-p caption "")
1957 comment-include image-code
1958 (if caption-above-p "" caption)))
1959 (multicolumn (format "\\begin{figure*}%s
1960 %s\\centering
1961 %s%s
1962 %s\\end{figure*}"
1963 placement
1964 (if caption-above-p caption "")
1965 comment-include image-code
1966 (if caption-above-p "" caption)))
1967 (figure (format "\\begin{figure}%s
1968 %s\\centering
1969 %s%s
1970 %s\\end{figure}"
1971 placement
1972 (if caption-above-p caption "")
1973 comment-include image-code
1974 (if caption-above-p "" caption)))
1975 (otherwise image-code))))
1977 (defun org-latex-link (link desc info)
1978 "Transcode a LINK object from Org to LaTeX.
1980 DESC is the description part of the link, or the empty string.
1981 INFO is a plist holding contextual information. See
1982 `org-export-data'."
1983 (let* ((type (org-element-property :type link))
1984 (raw-path (replace-regexp-in-string
1985 "%" "\\%" (org-element-property :path link) nil t))
1986 ;; Ensure DESC really exists, or set it to nil.
1987 (desc (and (not (string= desc "")) desc))
1988 (imagep (org-export-inline-image-p
1989 link (plist-get info :latex-inline-image-rules)))
1990 (path (cond
1991 ((member type '("http" "https" "ftp" "mailto"))
1992 (concat type ":" raw-path))
1993 ((and (string= type "file") (file-name-absolute-p raw-path))
1994 (concat "file:" raw-path))
1995 (t raw-path))))
1996 (cond
1997 ;; Link type is handled by a special function.
1998 ((org-export-custom-protocol-maybe link desc info))
1999 ;; Image file.
2000 (imagep (org-latex--inline-image link info))
2001 ;; Radio link: Transcode target's contents and use them as link's
2002 ;; description.
2003 ((string= type "radio")
2004 (let ((destination (org-export-resolve-radio-link link info)))
2005 (if (not destination) desc
2006 (format "\\hyperref[%s]{%s}"
2007 (org-export-solidify-link-text
2008 (org-element-property :value destination))
2009 desc))))
2010 ;; Links pointing to a headline: Find destination and build
2011 ;; appropriate referencing command.
2012 ((member type '("custom-id" "fuzzy" "id"))
2013 (let ((destination (if (string= type "fuzzy")
2014 (org-export-resolve-fuzzy-link link info)
2015 (org-export-resolve-id-link link info))))
2016 (case (org-element-type destination)
2017 ;; Id link points to an external file.
2018 (plain-text
2019 (if desc (format "\\href{%s}{%s}" destination desc)
2020 (format "\\url{%s}" destination)))
2021 ;; Fuzzy link points nowhere.
2022 ('nil
2023 (format (plist-get info :latex-link-with-unknown-path-format)
2024 (or desc
2025 (org-export-data
2026 (org-element-property :raw-link link) info))))
2027 ;; LINK points to a headline. If headlines are numbered
2028 ;; and the link has no description, display headline's
2029 ;; number. Otherwise, display description or headline's
2030 ;; title.
2031 (headline
2032 (let* ((custom-label
2033 (and (plist-get info :latex-custom-id-labels)
2034 (org-element-property :CUSTOM_ID destination)))
2035 (label (or custom-label
2036 (org-export-get-headline-id destination info))))
2037 (if (and (not desc)
2038 (org-export-numbered-headline-p destination info))
2039 (format "\\ref{%s}" label)
2040 (format "\\hyperref[%s]{%s}" label
2041 (or desc
2042 (org-export-data
2043 (org-element-property :title destination) info))))))
2044 ;; Fuzzy link points to a target. Do as above.
2045 (otherwise
2046 (let ((path (org-export-solidify-link-text path)))
2047 (if (not desc) (format "\\ref{%s}" path)
2048 (format "\\hyperref[%s]{%s}" path desc)))))))
2049 ;; Coderef: replace link with the reference name or the
2050 ;; equivalent line number.
2051 ((string= type "coderef")
2052 (format (org-export-get-coderef-format path desc)
2053 (org-export-resolve-coderef path info)))
2054 ;; External link with a description part.
2055 ((and path desc) (format "\\href{%s}{%s}" path desc))
2056 ;; External link without a description part.
2057 (path (format "\\url{%s}" path))
2058 ;; No path, only description. Try to do something useful.
2059 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
2062 ;;;; Node Property
2064 (defun org-latex-node-property (node-property contents info)
2065 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2066 CONTENTS is nil. INFO is a plist holding contextual
2067 information."
2068 (format "%s:%s"
2069 (org-element-property :key node-property)
2070 (let ((value (org-element-property :value node-property)))
2071 (if value (concat " " value) ""))))
2074 ;;;; Paragraph
2076 (defun org-latex-paragraph (paragraph contents info)
2077 "Transcode a PARAGRAPH element from Org to LaTeX.
2078 CONTENTS is the contents of the paragraph, as a string. INFO is
2079 the plist used as a communication channel."
2080 contents)
2083 ;;;; Plain List
2085 (defun org-latex-plain-list (plain-list contents info)
2086 "Transcode a PLAIN-LIST element from Org to LaTeX.
2087 CONTENTS is the contents of the list. INFO is a plist holding
2088 contextual information."
2089 (let* ((type (org-element-property :type plain-list))
2090 (attr (org-export-read-attribute :attr_latex plain-list))
2091 (latex-type (let ((env (plist-get attr :environment)))
2092 (cond (env (format "%s" env))
2093 ((eq type 'ordered) "enumerate")
2094 ((eq type 'descriptive) "description")
2095 (t "itemize")))))
2096 (org-latex--wrap-label
2097 plain-list
2098 (format "\\begin{%s}%s\n%s\\end{%s}"
2099 latex-type
2100 (or (plist-get attr :options) "")
2101 contents
2102 latex-type))))
2105 ;;;; Plain Text
2107 (defun org-latex-plain-text (text info)
2108 "Transcode a TEXT string from Org to LaTeX.
2109 TEXT is the string to transcode. INFO is a plist holding
2110 contextual information."
2111 (let* ((specialp (plist-get info :with-special-strings))
2112 (output
2113 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2114 (let ((case-fold-search nil))
2115 (replace-regexp-in-string
2116 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2117 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2118 ;; However, if special strings are used, be careful not
2119 ;; to protect "\" in "\-" constructs.
2120 (replace-regexp-in-string
2121 (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\(?:[^-]\\|$\\)"))
2122 (lambda (m)
2123 (case (aref m 0)
2124 (?\\ "$\\\\backslash$")
2125 (?~ "\\\\textasciitilde{}")
2126 (?^ "\\\\^{}")
2127 (t "\\\\\\&")))
2128 text)))))
2129 ;; Activate smart quotes. Be sure to provide original TEXT string
2130 ;; since OUTPUT may have been modified.
2131 (when (plist-get info :with-smart-quotes)
2132 (setq output (org-export-activate-smart-quotes output :latex info text)))
2133 ;; Convert special strings.
2134 (when specialp
2135 (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
2136 ;; Handle break preservation if required.
2137 (when (plist-get info :preserve-breaks)
2138 (setq output (replace-regexp-in-string
2139 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
2140 ;; Return value.
2141 output))
2144 ;;;; Planning
2146 (defun org-latex-planning (planning contents info)
2147 "Transcode a PLANNING element from Org to LaTeX.
2148 CONTENTS is nil. INFO is a plist holding contextual
2149 information."
2150 (concat
2151 "\\noindent"
2152 (mapconcat
2153 'identity
2154 (delq nil
2155 (list
2156 (let ((closed (org-element-property :closed planning)))
2157 (when closed
2158 (concat
2159 (format "\\textbf{%s} " org-closed-string)
2160 (format (plist-get info :latex-inactive-timestamp-format)
2161 (org-timestamp-translate closed)))))
2162 (let ((deadline (org-element-property :deadline planning)))
2163 (when deadline
2164 (concat
2165 (format "\\textbf{%s} " org-deadline-string)
2166 (format (plist-get info :latex-active-timestamp-format)
2167 (org-timestamp-translate deadline)))))
2168 (let ((scheduled (org-element-property :scheduled planning)))
2169 (when scheduled
2170 (concat
2171 (format "\\textbf{%s} " org-scheduled-string)
2172 (format (plist-get info :latex-active-timestamp-format)
2173 (org-timestamp-translate scheduled)))))))
2174 " ")
2175 "\\\\"))
2178 ;;;; Property Drawer
2180 (defun org-latex-property-drawer (property-drawer contents info)
2181 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2182 CONTENTS holds the contents of the drawer. INFO is a plist
2183 holding contextual information."
2184 (and (org-string-nw-p contents)
2185 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2188 ;;;; Pseudo Element: LaTeX Matrices
2190 ;; `latex-matrices' elements have the following properties:
2191 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2192 ;; `math').
2194 (defun org-latex--wrap-latex-matrices (data info)
2195 "Merge contiguous tables with the same mode within a pseudo-element.
2196 DATA is a parse tree or a secondary string. INFO is a plist
2197 containing export options. Modify DATA by side-effect and return
2198 it."
2199 (org-element-map data 'table
2200 (lambda (table)
2201 (when (eq (org-element-property :type table) 'org)
2202 (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
2203 (plist-get info :latex-default-table-mode))))
2204 (when (and (member mode '("inline-math" "math"))
2205 ;; Do not wrap twice the same table.
2206 (not (eq (org-element-type
2207 (org-element-property :parent table))
2208 'latex-matrices)))
2209 (let* ((caption (and (not (string= mode "inline-math"))
2210 (org-element-property :caption table)))
2211 (matrices
2212 (list 'latex-matrices
2213 (list :caption caption
2214 :markup
2215 (cond ((string= mode "inline-math") 'inline)
2216 (caption 'equation)
2217 (t 'math)))))
2218 (previous table)
2219 (next (org-export-get-next-element table info)))
2220 (org-element-insert-before matrices table)
2221 ;; Swallow all contiguous tables sharing the same mode.
2222 (while (and
2223 (zerop (or (org-element-property :post-blank previous) 0))
2224 (setq next (org-export-get-next-element previous info))
2225 (eq (org-element-type next) 'table)
2226 (eq (org-element-property :type next) 'org)
2227 (string= (or (org-export-read-attribute
2228 :attr_latex next :mode)
2229 (plist-get info :latex-default-table-mode))
2230 mode))
2231 (org-element-extract-element previous)
2232 (org-element-adopt-elements matrices previous)
2233 (setq previous next))
2234 (org-element-put-property
2235 matrices :post-blank (org-element-property :post-blank previous))
2236 (org-element-extract-element previous)
2237 (org-element-adopt-elements matrices previous))))))
2238 info)
2239 data)
2241 (defun org-latex-matrices (matrices contents info)
2242 "Transcode a MATRICES element from Org to LaTeX.
2243 CONTENTS is a string. INFO is a plist used as a communication
2244 channel."
2245 (format (case (org-element-property :markup matrices)
2246 (inline "\\(%s\\)")
2247 (equation "\\begin{equation}\n%s\\end{equation}")
2248 (t "\\[\n%s\\]"))
2249 contents))
2251 (defun org-latex-matrices-tree-filter (tree backend info)
2252 (org-latex--wrap-latex-matrices tree info))
2254 ;;;; Pseudo Object: LaTeX Math Block
2256 ;; `latex-math-block' objects have the following property:
2257 ;; `:post-blank'.
2259 (defun org-latex--wrap-latex-math-block (data info)
2260 "Merge contiguous math objects in a pseudo-object container.
2261 DATA is a parse tree or a secondary string. INFO is a plist
2262 containing export options. Modify DATA by side-effect and return it."
2263 (let ((valid-object-p
2264 (function
2265 ;; Non-nil when OBJ can be added to the latex math block.
2266 (lambda (obj)
2267 (case (org-element-type obj)
2268 (entity (org-element-property :latex-math-p obj))
2269 (latex-fragment
2270 (let ((value (org-element-property :value obj)))
2271 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2272 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2273 ((subscript superscript) t))))))
2274 (org-element-map data '(entity latex-fragment subscript superscript)
2275 (lambda (object)
2276 ;; Skip objects already wrapped.
2277 (when (and (not (eq (org-element-type
2278 (org-element-property :parent object))
2279 'latex-math-block))
2280 (funcall valid-object-p object))
2281 (let ((math-block (list 'latex-math-block nil))
2282 (next-elements (org-export-get-next-element object info t))
2283 (last object))
2284 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2285 (org-element-insert-before math-block object)
2286 (org-element-extract-element object)
2287 (org-element-adopt-elements math-block object)
2288 (when (zerop (or (org-element-property :post-blank object) 0))
2289 ;; MATH-BLOCK swallows consecutive math objects.
2290 (catch 'exit
2291 (dolist (next next-elements)
2292 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2293 (org-element-extract-element next)
2294 (org-element-adopt-elements math-block next)
2295 ;; Eschew the case: \beta$x$ -> \(\betax\).
2296 (unless (memq (org-element-type next)
2297 '(subscript superscript))
2298 (org-element-put-property last :post-blank 1))
2299 (setq last next)
2300 (when (> (or (org-element-property :post-blank next) 0) 0)
2301 (throw 'exit nil))))))
2302 (org-element-put-property
2303 math-block :post-blank (org-element-property :post-blank last)))))
2304 info nil '(subscript superscript latex-math-block) t)
2305 ;; Return updated DATA.
2306 data))
2308 (defun org-latex-math-block-tree-filter (tree backend info)
2309 (org-latex--wrap-latex-math-block tree info))
2311 (defun org-latex-math-block-options-filter (info backend)
2312 (dolist (prop '(:author :date :title) info)
2313 (plist-put info prop
2314 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2316 (defun org-latex-math-block (math-block contents info)
2317 "Transcode a MATH-BLOCK object from Org to LaTeX.
2318 CONTENTS is a string. INFO is a plist used as a communication
2319 channel."
2320 (when (org-string-nw-p contents)
2321 (format "\\(%s\\)" (org-trim contents))))
2323 ;;;; Quote Block
2325 (defun org-latex-quote-block (quote-block contents info)
2326 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2327 CONTENTS holds the contents of the block. INFO is a plist
2328 holding contextual information."
2329 (org-latex--wrap-label
2330 quote-block
2331 (format "\\begin{quote}\n%s\\end{quote}" contents)))
2334 ;;;; Radio Target
2336 (defun org-latex-radio-target (radio-target text info)
2337 "Transcode a RADIO-TARGET object from Org to LaTeX.
2338 TEXT is the text of the target. INFO is a plist holding
2339 contextual information."
2340 (format "\\label{%s}%s"
2341 (org-export-solidify-link-text
2342 (org-element-property :value radio-target))
2343 text))
2346 ;;;; Section
2348 (defun org-latex-section (section contents info)
2349 "Transcode a SECTION element from Org to LaTeX.
2350 CONTENTS holds the contents of the section. INFO is a plist
2351 holding contextual information."
2352 contents)
2355 ;;;; Special Block
2357 (defun org-latex-special-block (special-block contents info)
2358 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2359 CONTENTS holds the contents of the block. INFO is a plist
2360 holding contextual information."
2361 (let ((type (org-element-property :type special-block))
2362 (opt (org-export-read-attribute :attr_latex special-block :options))
2363 (caption (org-latex--caption/label-string special-block info))
2364 (caption-above-p (org-latex--caption-above-p special-block info)))
2365 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2366 (and caption-above-p caption)
2367 contents
2368 (and (not caption-above-p) caption)
2369 (format "\\end{%s}" type))))
2372 ;;;; Src Block
2374 (defun org-latex-src-block (src-block contents info)
2375 "Transcode a SRC-BLOCK element from Org to LaTeX.
2376 CONTENTS holds the contents of the item. INFO is a plist holding
2377 contextual information."
2378 (when (org-string-nw-p (org-element-property :value src-block))
2379 (let* ((lang (org-element-property :language src-block))
2380 (caption (org-element-property :caption src-block))
2381 (caption-above-p (org-latex--caption-above-p src-block info))
2382 (label (org-element-property :name src-block))
2383 (custom-env (and lang
2384 (cadr (assq (intern lang)
2385 org-latex-custom-lang-environments))))
2386 (num-start (case (org-element-property :number-lines src-block)
2387 (continued (org-export-get-loc src-block info))
2388 (new 0)))
2389 (retain-labels (org-element-property :retain-labels src-block))
2390 (attributes (org-export-read-attribute :attr_latex src-block))
2391 (float (plist-get attributes :float))
2392 (listings (plist-get info :latex-listings)))
2393 (cond
2394 ;; Case 1. No source fontification.
2395 ((not listings)
2396 (let* ((caption-str (org-latex--caption/label-string src-block info))
2397 (float-env
2398 (cond ((and (not float) (plist-member attributes :float)) "%s")
2399 ((string= "multicolumn" float)
2400 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2401 (plist-get info :latex-default-figure-position)
2402 (if caption-above-p caption-str "")
2403 (if caption-above-p "" caption-str)))
2404 ((or caption float)
2405 (format "\\begin{figure}[H]\n%%s\n%s\\end{figure}"
2406 caption-str))
2407 (t "%s"))))
2408 (format
2409 float-env
2410 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2411 (org-export-format-code-default src-block info))))))
2412 ;; Case 2. Custom environment.
2413 (custom-env
2414 (let ((caption-str (org-latex--caption/label-string src-block info)))
2415 (format "\\begin{%s}\n%s\\end{%s}\n"
2416 custom-env
2417 (concat (and caption-above-p caption-str)
2418 (org-export-format-code-default src-block info)
2419 (and (not caption-above-p) caption-str))
2420 custom-env)))
2421 ;; Case 3. Use minted package.
2422 ((eq listings 'minted)
2423 (let* ((caption-str (org-latex--caption/label-string src-block info))
2424 (float-env
2425 (cond
2426 ((and (not float) (plist-member attributes :float) caption)
2427 (let ((caption
2428 (replace-regexp-in-string
2429 "\\\\caption" "\\captionof{listing}" caption-str
2430 t t)))
2431 (concat (and caption-above-p caption)
2432 "%%s"
2433 (and (not caption-above-p) (concat "\n" caption)))))
2434 ((and (not float) (plist-member attributes :float)) "%s")
2435 ((string= "multicolumn" float)
2436 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2437 (if caption-above-p caption-str "")
2438 (if caption-above-p "" caption-str)))
2439 ((or caption float)
2440 (format "\\begin{listing}[H]\n%s%%s\n%s\\end{listing}"
2441 (if caption-above-p caption-str "")
2442 (if caption-above-p "" caption-str)))
2443 (t "%s")))
2444 (options (plist-get info :latex-minted-options))
2445 (body
2446 (format
2447 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2448 ;; Options.
2449 (concat
2450 (org-latex--make-option-string
2451 (if (or (not num-start) (assoc "linenos" options))
2452 options
2453 (append
2454 `(("linenos")
2455 ("firstnumber" ,(number-to-string (1+ num-start))))
2456 options)))
2457 (let ((local-options (plist-get attributes :options)))
2458 (and local-options (concat "," local-options))))
2459 ;; Language.
2460 (or (cadr (assq (intern lang)
2461 (plist-get info :latex-minted-langs)))
2462 (downcase lang))
2463 ;; Source code.
2464 (let* ((code-info (org-export-unravel-code src-block))
2465 (max-width
2466 (apply 'max
2467 (mapcar 'length
2468 (org-split-string (car code-info)
2469 "\n")))))
2470 (org-export-format-code
2471 (car code-info)
2472 (lambda (loc num ref)
2473 (concat
2475 (when ref
2476 ;; Ensure references are flushed to the right,
2477 ;; separated with 6 spaces from the widest line
2478 ;; of code.
2479 (concat (make-string (+ (- max-width (length loc)) 6)
2480 ?\s)
2481 (format "(%s)" ref)))))
2482 nil (and retain-labels (cdr code-info)))))))
2483 ;; Return value.
2484 (format float-env body)))
2485 ;; Case 4. Use listings package.
2487 (let ((lst-lang
2488 (or (cadr (assq (intern lang)
2489 (plist-get info :latex-listings-langs)))
2490 lang))
2491 (caption-str
2492 (when caption
2493 (let ((main (org-export-get-caption src-block))
2494 (secondary (org-export-get-caption src-block t)))
2495 (if (not secondary)
2496 (format "{%s}" (org-export-data main info))
2497 (format "{[%s]%s}"
2498 (org-export-data secondary info)
2499 (org-export-data main info))))))
2500 (lst-opt (plist-get info :latex-listings-options)))
2501 (concat
2502 ;; Options.
2503 (format
2504 "\\lstset{%s}\n"
2505 (concat
2506 (org-latex--make-option-string
2507 (append
2508 lst-opt
2509 (cond
2510 ((and (not float) (plist-member attributes :float)) nil)
2511 ((string= "multicolumn" float) '(("float" "*")))
2512 ((and float (not (assoc "float" lst-opt)))
2513 `(("float" ,(plist-get info :latex-default-figure-position)))))
2514 `(("language" ,lst-lang))
2515 (if label `(("label" ,label)) '(("label" " ")))
2516 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2517 `(("captionpos" ,(if caption-above-p "t" "b")))
2518 (cond ((assoc "numbers" lst-opt) nil)
2519 ((not num-start) '(("numbers" "none")))
2520 ((zerop num-start) '(("numbers" "left")))
2521 (t `(("firstnumber" ,(number-to-string (1+ num-start)))
2522 ("numbers" "left"))))))
2523 (let ((local-options (plist-get attributes :options)))
2524 (and local-options (concat "," local-options)))))
2525 ;; Source code.
2526 (format
2527 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2528 (let* ((code-info (org-export-unravel-code src-block))
2529 (max-width
2530 (apply 'max
2531 (mapcar 'length
2532 (org-split-string (car code-info) "\n")))))
2533 (org-export-format-code
2534 (car code-info)
2535 (lambda (loc num ref)
2536 (concat
2538 (when ref
2539 ;; Ensure references are flushed to the right,
2540 ;; separated with 6 spaces from the widest line of
2541 ;; code
2542 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2543 (format "(%s)" ref)))))
2544 nil (and retain-labels (cdr code-info))))))))))))
2547 ;;;; Statistics Cookie
2549 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2550 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2551 CONTENTS is nil. INFO is a plist holding contextual information."
2552 (replace-regexp-in-string
2553 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2556 ;;;; Strike-Through
2558 (defun org-latex-strike-through (strike-through contents info)
2559 "Transcode STRIKE-THROUGH from Org to LaTeX.
2560 CONTENTS is the text with strike-through markup. INFO is a plist
2561 holding contextual information."
2562 (org-latex--text-markup contents 'strike-through info))
2565 ;;;; Subscript
2567 (defun org-latex--script-size (object info)
2568 "Transcode a subscript or superscript object.
2569 OBJECT is an Org object. INFO is a plist used as a communication
2570 channel."
2571 (let ((type (org-element-type object))
2572 (output ""))
2573 (org-element-map (org-element-contents object)
2574 (cons 'plain-text org-element-all-objects)
2575 (lambda (obj)
2576 (case (org-element-type obj)
2577 ((entity latex-fragment)
2578 (let ((data (org-trim (org-export-data obj info))))
2579 (string-match
2580 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2581 data)
2582 (setq output
2583 (concat output
2584 (match-string 1 data)
2585 (let ((blank (org-element-property :post-blank obj)))
2586 (and blank (> blank 0) "\\ "))))))
2587 (plain-text
2588 (setq output
2589 (format "%s\\text{%s}" output (org-export-data obj info))))
2590 (otherwise
2591 (setq output
2592 (concat output
2593 (org-export-data obj info)
2594 (let ((blank (org-element-property :post-blank obj)))
2595 (and blank (> blank 0) "\\ ")))))))
2596 info nil org-element-recursive-objects)
2597 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2598 ;; character.
2599 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2600 (and (> (length output) 1) "{")
2601 output
2602 (and (> (length output) 1) "}"))))
2604 (defun org-latex-subscript (subscript contents info)
2605 "Transcode a SUBSCRIPT object from Org to LaTeX.
2606 CONTENTS is the contents of the object. INFO is a plist holding
2607 contextual information."
2608 (org-latex--script-size subscript info))
2611 ;;;; Superscript
2613 (defun org-latex-superscript (superscript contents info)
2614 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2615 CONTENTS is the contents of the object. INFO is a plist holding
2616 contextual information."
2617 (org-latex--script-size superscript info))
2620 ;;;; Table
2622 ;; `org-latex-table' is the entry point for table transcoding. It
2623 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2624 ;; delegates the job to either `org-latex--table.el-table',
2625 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2626 ;; depending of the type of the table and the mode requested.
2628 ;; `org-latex--align-string' is a subroutine used to build alignment
2629 ;; string for Org tables.
2631 (defun org-latex-table (table contents info)
2632 "Transcode a TABLE element from Org to LaTeX.
2633 CONTENTS is the contents of the table. INFO is a plist holding
2634 contextual information."
2635 (if (eq (org-element-property :type table) 'table.el)
2636 ;; "table.el" table. Convert it using appropriate tools.
2637 (org-latex--table.el-table table info)
2638 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2639 (plist-get info :latex-default-table-mode))))
2640 (cond
2641 ;; Case 1: Verbatim table.
2642 ((string= type "verbatim")
2643 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2644 ;; Re-create table, without affiliated keywords.
2645 (org-trim (org-element-interpret-data
2646 `(table nil ,@(org-element-contents table))
2647 org-latex-pseudo-objects))))
2648 ;; Case 2: Matrix.
2649 ((or (string= type "math") (string= type "inline-math"))
2650 (org-latex--math-table table info))
2651 ;; Case 3: Standard table.
2652 (t (concat (org-latex--org-table table contents info)
2653 ;; When there are footnote references within the
2654 ;; table, insert their definition just after it.
2655 (org-latex--delayed-footnotes-definitions table info)))))))
2657 (defun org-latex--align-string (table info)
2658 "Return an appropriate LaTeX alignment string.
2659 TABLE is the considered table. INFO is a plist used as
2660 a communication channel."
2661 (or (org-export-read-attribute :attr_latex table :align)
2662 (let (align)
2663 ;; Extract column groups and alignment from first (non-rule)
2664 ;; row.
2665 (org-element-map
2666 (org-element-map table 'table-row
2667 (lambda (row)
2668 (and (eq (org-element-property :type row) 'standard) row))
2669 info 'first-match)
2670 'table-cell
2671 (lambda (cell)
2672 (let ((borders (org-export-table-cell-borders cell info)))
2673 ;; Check left border for the first cell only.
2674 (when (and (memq 'left borders) (not align))
2675 (push "|" align))
2676 (push (case (org-export-table-cell-alignment cell info)
2677 (left "l")
2678 (right "r")
2679 (center "c"))
2680 align)
2681 (when (memq 'right borders) (push "|" align))))
2682 info)
2683 (apply 'concat (nreverse align)))))
2685 (defun org-latex--org-table (table contents info)
2686 "Return appropriate LaTeX code for an Org table.
2688 TABLE is the table type element to transcode. CONTENTS is its
2689 contents, as a string. INFO is a plist used as a communication
2690 channel.
2692 This function assumes TABLE has `org' as its `:type' property and
2693 `table' as its `:mode' attribute."
2694 (let* ((caption (org-latex--caption/label-string table info))
2695 (attr (org-export-read-attribute :attr_latex table))
2696 ;; Determine alignment string.
2697 (alignment (org-latex--align-string table info))
2698 ;; Determine environment for the table: longtable, tabular...
2699 (table-env (or (plist-get attr :environment)
2700 (plist-get info :latex-default-table-environment)))
2701 ;; If table is a float, determine environment: table, table*
2702 ;; or sidewaystable.
2703 (float-env (unless (member table-env '("longtable" "longtabu"))
2704 (let ((float (plist-get attr :float)))
2705 (cond
2706 ((and (not float) (plist-member attr :float)) nil)
2707 ((or (string= float "sidewaystable")
2708 (string= float "sideways")) "sidewaystable")
2709 ((string= float "multicolumn") "table*")
2710 ((or float
2711 (org-element-property :caption table)
2712 (org-string-nw-p (plist-get attr :caption)))
2713 "table")))))
2714 ;; Extract others display options.
2715 (fontsize (let ((font (plist-get attr :font)))
2716 (and font (concat font "\n"))))
2717 ;; "tabular" environment doesn't allow to define a width.
2718 (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
2719 (spreadp (plist-get attr :spread))
2720 (placement
2721 (or (plist-get attr :placement)
2722 (format "[%s]" (plist-get info :latex-default-figure-position))))
2723 (centerp (if (plist-member attr :center) (plist-get attr :center)
2724 (plist-get info :latex-tables-centered)))
2725 (caption-above-p (org-latex--caption-above-p table info)))
2726 ;; Prepare the final format string for the table.
2727 (cond
2728 ;; Longtable.
2729 ((equal "longtable" table-env)
2730 (concat (and fontsize (concat "{" fontsize))
2731 (format "\\begin{longtable}{%s}\n" alignment)
2732 (and caption-above-p
2733 (org-string-nw-p caption)
2734 (concat caption "\\\\\n"))
2735 contents
2736 (and (not caption-above-p)
2737 (org-string-nw-p caption)
2738 (concat caption "\\\\\n"))
2739 "\\end{longtable}\n"
2740 (and fontsize "}")))
2741 ;; Longtabu
2742 ((equal "longtabu" table-env)
2743 (concat (and fontsize (concat "{" fontsize))
2744 (format "\\begin{longtabu}%s{%s}\n"
2745 (if width
2746 (format " %s %s "
2747 (if spreadp "spread" "to") width) "")
2748 alignment)
2749 (and caption-above-p
2750 (org-string-nw-p caption)
2751 (concat caption "\\\\\n"))
2752 contents
2753 (and (not caption-above-p)
2754 (org-string-nw-p caption)
2755 (concat caption "\\\\\n"))
2756 "\\end{longtabu}\n"
2757 (and fontsize "}")))
2758 ;; Others.
2759 (t (concat (cond
2760 (float-env
2761 (concat (format "\\begin{%s}%s\n" float-env placement)
2762 (if caption-above-p caption "")
2763 (when centerp "\\centering\n")
2764 fontsize))
2765 (centerp (concat "\\begin{center}\n" fontsize))
2766 (fontsize (concat "{" fontsize)))
2767 (cond ((equal "tabu" table-env)
2768 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2769 (if width (format
2770 (if spreadp " spread %s " " to %s ")
2771 width) "")
2772 alignment
2773 contents))
2774 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2775 table-env
2776 (if width (format "{%s}" width) "")
2777 alignment
2778 contents
2779 table-env)))
2780 (cond
2781 (float-env
2782 (concat (if caption-above-p "" caption)
2783 (format "\n\\end{%s}" float-env)))
2784 (centerp "\n\\end{center}")
2785 (fontsize "}")))))))
2787 (defun org-latex--table.el-table (table info)
2788 "Return appropriate LaTeX code for a table.el table.
2790 TABLE is the table type element to transcode. INFO is a plist
2791 used as a communication channel.
2793 This function assumes TABLE has `table.el' as its `:type'
2794 property."
2795 (require 'table)
2796 ;; Ensure "*org-export-table*" buffer is empty.
2797 (with-current-buffer (get-buffer-create "*org-export-table*")
2798 (erase-buffer))
2799 (let ((output (with-temp-buffer
2800 (insert (org-element-property :value table))
2801 (goto-char 1)
2802 (re-search-forward "^[ \t]*|[^|]" nil t)
2803 (table-generate-source 'latex "*org-export-table*")
2804 (with-current-buffer "*org-export-table*"
2805 (org-trim (buffer-string))))))
2806 (kill-buffer (get-buffer "*org-export-table*"))
2807 ;; Remove left out comments.
2808 (while (string-match "^%.*\n" output)
2809 (setq output (replace-match "" t t output)))
2810 (let ((attr (org-export-read-attribute :attr_latex table)))
2811 (when (plist-get attr :rmlines)
2812 ;; When the "rmlines" attribute is provided, remove all hlines
2813 ;; but the the one separating heading from the table body.
2814 (let ((n 0) (pos 0))
2815 (while (and (< (length output) pos)
2816 (setq pos (string-match "^\\\\hline\n?" output pos)))
2817 (incf n)
2818 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2819 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2820 (plist-get info :latex-tables-centered))))
2821 (if (not centerp) output
2822 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2824 (defun org-latex--math-table (table info)
2825 "Return appropriate LaTeX code for a matrix.
2827 TABLE is the table type element to transcode. INFO is a plist
2828 used as a communication channel.
2830 This function assumes TABLE has `org' as its `:type' property and
2831 `inline-math' or `math' as its `:mode' attribute."
2832 (let* ((attr (org-export-read-attribute :attr_latex table))
2833 (env (or (plist-get attr :environment)
2834 (plist-get info :latex-default-table-environment)))
2835 (contents
2836 (mapconcat
2837 (lambda (row)
2838 ;; Ignore horizontal rules.
2839 (when (eq (org-element-property :type row) 'standard)
2840 ;; Return each cell unmodified.
2841 (concat
2842 (mapconcat
2843 (lambda (cell)
2844 (substring
2845 (org-element-interpret-data cell org-latex-pseudo-objects)
2846 0 -1))
2847 (org-element-map row 'table-cell #'identity info) "&")
2848 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2849 "\n")))
2850 (org-element-map table 'table-row #'identity info) "")))
2851 (concat
2852 ;; Prefix.
2853 (plist-get attr :math-prefix)
2854 ;; Environment. Also treat special cases.
2855 (cond ((member env '("array" "tabular"))
2856 (let ((align (make-string
2857 (cdr (org-export-table-dimensions table info)) ?c)))
2858 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
2859 ((assoc env org-latex-table-matrix-macros)
2860 (format "\\%s%s{\n%s}"
2862 (or (plist-get attr :math-arguments) "")
2863 contents))
2864 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2865 ;; Suffix.
2866 (plist-get attr :math-suffix))))
2869 ;;;; Table Cell
2871 (defun org-latex-table-cell (table-cell contents info)
2872 "Transcode a TABLE-CELL element from Org to LaTeX.
2873 CONTENTS is the cell contents. INFO is a plist used as
2874 a communication channel."
2875 (concat
2876 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
2877 (if (and contents
2878 scientific-format
2879 (string-match orgtbl-exp-regexp contents))
2880 ;; Use appropriate format string for scientific
2881 ;; notation.
2882 (format scientific-format
2883 (match-string 1 contents)
2884 (match-string 2 contents))
2885 contents))
2886 (when (org-export-get-next-element table-cell info) " & ")))
2889 ;;;; Table Row
2891 (defun org-latex-table-row (table-row contents info)
2892 "Transcode a TABLE-ROW element from Org to LaTeX.
2893 CONTENTS is the contents of the row. INFO is a plist used as
2894 a communication channel."
2895 (let* ((attr (org-export-read-attribute :attr_latex
2896 (org-export-get-parent table-row)))
2897 (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
2898 (plist-get info :latex-tables-booktabs)))
2899 (longtablep
2900 (member (or (plist-get attr :environment)
2901 (plist-get info :latex-default-table-environment))
2902 '("longtable" "longtabu"))))
2903 (if (eq (org-element-property :type table-row) 'rule)
2904 (cond
2905 ((not booktabsp) "\\hline")
2906 ((not (org-export-get-previous-element table-row info)) "\\toprule")
2907 ((not (org-export-get-next-element table-row info)) "\\bottomrule")
2908 ((and longtablep
2909 (org-export-table-row-ends-header-p
2910 (org-export-get-previous-element table-row info) info))
2912 (t "\\midrule"))
2913 (concat
2914 ;; When BOOKTABS are activated enforce top-rule even when no
2915 ;; hline was specifically marked.
2916 (and booktabsp (not (org-export-get-previous-element table-row info))
2917 "\\toprule\n")
2918 contents "\\\\\n"
2919 (cond
2920 ;; Special case for long tables. Define header and footers.
2921 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2922 (let ((columns (cdr (org-export-table-dimensions
2923 (org-export-get-parent-table table-row) info))))
2924 (format "%s
2925 \\endfirsthead
2926 \\multicolumn{%d}{l}{%s} \\\\
2928 %s \\\\\n
2930 \\endhead
2931 %s\\multicolumn{%d}{r}{%s} \\\\
2932 \\endfoot
2933 \\endlastfoot"
2934 (if booktabsp "\\midrule" "\\hline")
2935 columns
2936 (org-latex--translate "Continued from previous page" info)
2937 (cond
2938 ((not (org-export-table-row-starts-header-p table-row info))
2940 (booktabsp "\\toprule\n")
2941 (t "\\hline\n"))
2942 contents
2943 (if booktabsp "\\midrule" "\\hline")
2944 (if booktabsp "\\midrule" "\\hline")
2945 columns
2946 (org-latex--translate "Continued on next page" info))))
2947 ;; When BOOKTABS are activated enforce bottom rule even when
2948 ;; no hline was specifically marked.
2949 ((and booktabsp (not (org-export-get-next-element table-row info)))
2950 "\\bottomrule"))))))
2953 ;;;; Target
2955 (defun org-latex-target (target contents info)
2956 "Transcode a TARGET object from Org to LaTeX.
2957 CONTENTS is nil. INFO is a plist holding contextual
2958 information."
2959 (format "\\label{%s}"
2960 (org-export-solidify-link-text (org-element-property :value target))))
2963 ;;;; Timestamp
2965 (defun org-latex-timestamp (timestamp contents info)
2966 "Transcode a TIMESTAMP object from Org to LaTeX.
2967 CONTENTS is nil. INFO is a plist holding contextual
2968 information."
2969 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
2970 (format
2971 (plist-get info
2972 (case (org-element-property :type timestamp)
2973 ((active active-range) :latex-active-timestamp-format)
2974 ((inactive inactive-range) :latex-inactive-timestamp-format)
2975 (otherwise :latex-diary-timestamp-format)))
2976 value)))
2979 ;;;; Underline
2981 (defun org-latex-underline (underline contents info)
2982 "Transcode UNDERLINE from Org to LaTeX.
2983 CONTENTS is the text with underline markup. INFO is a plist
2984 holding contextual information."
2985 (org-latex--text-markup contents 'underline info))
2988 ;;;; Verbatim
2990 (defun org-latex-verbatim (verbatim contents info)
2991 "Transcode a VERBATIM object from Org to LaTeX.
2992 CONTENTS is nil. INFO is a plist used as a communication
2993 channel."
2994 (org-latex--text-markup
2995 (org-element-property :value verbatim) 'verbatim info))
2998 ;;;; Verse Block
3000 (defun org-latex-verse-block (verse-block contents info)
3001 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3002 CONTENTS is verse block contents. INFO is a plist holding
3003 contextual information."
3004 (org-latex--wrap-label
3005 verse-block
3006 ;; In a verse environment, add a line break to each newline
3007 ;; character and change each white space at beginning of a line
3008 ;; into a space of 1 em. Also change each blank line with
3009 ;; a vertical space of 1 em.
3010 (format "\\begin{verse}\n%s\\end{verse}"
3011 (replace-regexp-in-string
3012 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
3013 (replace-regexp-in-string
3014 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3015 (replace-regexp-in-string
3016 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3017 contents nil t) nil t) nil t))))
3021 ;;; End-user functions
3023 ;;;###autoload
3024 (defun org-latex-export-as-latex
3025 (&optional async subtreep visible-only body-only ext-plist)
3026 "Export current buffer as a LaTeX buffer.
3028 If narrowing is active in the current buffer, only export its
3029 narrowed part.
3031 If a region is active, export that region.
3033 A non-nil optional argument ASYNC means the process should happen
3034 asynchronously. The resulting buffer should be accessible
3035 through the `org-export-stack' interface.
3037 When optional argument SUBTREEP is non-nil, export the sub-tree
3038 at point, extracting information from the headline properties
3039 first.
3041 When optional argument VISIBLE-ONLY is non-nil, don't export
3042 contents of hidden elements.
3044 When optional argument BODY-ONLY is non-nil, only write code
3045 between \"\\begin{document}\" and \"\\end{document}\".
3047 EXT-PLIST, when provided, is a property list with external
3048 parameters overriding Org default settings, but still inferior to
3049 file-local settings.
3051 Export is done in a buffer named \"*Org LATEX Export*\", which
3052 will be displayed when `org-export-show-temporary-export-buffer'
3053 is non-nil."
3054 (interactive)
3055 (org-export-to-buffer 'latex "*Org LATEX Export*"
3056 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
3058 ;;;###autoload
3059 (defun org-latex-convert-region-to-latex ()
3060 "Assume the current region has org-mode syntax, and convert it to LaTeX.
3061 This can be used in any buffer. For example, you can write an
3062 itemized list in org-mode syntax in an LaTeX buffer and use this
3063 command to convert it."
3064 (interactive)
3065 (org-export-replace-region-by 'latex))
3067 ;;;###autoload
3068 (defun org-latex-export-to-latex
3069 (&optional async subtreep visible-only body-only ext-plist)
3070 "Export current buffer to a LaTeX file.
3072 If narrowing is active in the current buffer, only export its
3073 narrowed part.
3075 If a region is active, export that region.
3077 A non-nil optional argument ASYNC means the process should happen
3078 asynchronously. The resulting file should be accessible through
3079 the `org-export-stack' interface.
3081 When optional argument SUBTREEP is non-nil, export the sub-tree
3082 at point, extracting information from the headline properties
3083 first.
3085 When optional argument VISIBLE-ONLY is non-nil, don't export
3086 contents of hidden elements.
3088 When optional argument BODY-ONLY is non-nil, only write code
3089 between \"\\begin{document}\" and \"\\end{document}\".
3091 EXT-PLIST, when provided, is a property list with external
3092 parameters overriding Org default settings, but still inferior to
3093 file-local settings."
3094 (interactive)
3095 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3096 (org-export-to-file 'latex outfile
3097 async subtreep visible-only body-only ext-plist)))
3099 ;;;###autoload
3100 (defun org-latex-export-to-pdf
3101 (&optional async subtreep visible-only body-only ext-plist)
3102 "Export current buffer to LaTeX then process through to PDF.
3104 If narrowing is active in the current buffer, only export its
3105 narrowed part.
3107 If a region is active, export that region.
3109 A non-nil optional argument ASYNC means the process should happen
3110 asynchronously. The resulting file should be accessible through
3111 the `org-export-stack' interface.
3113 When optional argument SUBTREEP is non-nil, export the sub-tree
3114 at point, extracting information from the headline properties
3115 first.
3117 When optional argument VISIBLE-ONLY is non-nil, don't export
3118 contents of hidden elements.
3120 When optional argument BODY-ONLY is non-nil, only write code
3121 between \"\\begin{document}\" and \"\\end{document}\".
3123 EXT-PLIST, when provided, is a property list with external
3124 parameters overriding Org default settings, but still inferior to
3125 file-local settings.
3127 Return PDF file's name."
3128 (interactive)
3129 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3130 (org-export-to-file 'latex outfile
3131 async subtreep visible-only body-only ext-plist
3132 (lambda (file) (org-latex-compile file)))))
3134 (defun org-latex-compile (texfile &optional snippet)
3135 "Compile a TeX file.
3137 TEXFILE is the name of the file being compiled. Processing is
3138 done through the command specified in `org-latex-pdf-process'.
3140 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3141 file used to preview a LaTeX snippet. In this case, do not
3142 create a log buffer and do not bother removing log files.
3144 Return PDF file name or an error if it couldn't be produced."
3145 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3146 (full-name (file-truename texfile))
3147 (out-dir (file-name-directory texfile))
3148 ;; Properly set working directory for compilation.
3149 (default-directory (if (file-name-absolute-p texfile)
3150 (file-name-directory full-name)
3151 default-directory))
3152 (time (current-time))
3153 warnings)
3154 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
3155 (save-window-excursion
3156 (cond
3157 ;; A function is provided: Apply it.
3158 ((functionp org-latex-pdf-process)
3159 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3160 ;; A list is provided: Replace %b, %f and %o with appropriate
3161 ;; values in each command before applying it. Output is
3162 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3163 ((consp org-latex-pdf-process)
3164 (let ((outbuf (and (not snippet)
3165 (get-buffer-create "*Org PDF LaTeX Output*"))))
3166 (dolist (command org-latex-pdf-process)
3167 (shell-command
3168 (replace-regexp-in-string
3169 "%b" (shell-quote-argument base-name)
3170 (replace-regexp-in-string
3171 "%f" (shell-quote-argument full-name)
3172 (replace-regexp-in-string
3173 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
3174 outbuf))
3175 ;; Collect standard errors from output buffer.
3176 (setq warnings (and (not snippet)
3177 (org-latex--collect-warnings outbuf)))))
3178 (t (error "No valid command to process to PDF")))
3179 (let ((pdffile (concat out-dir base-name ".pdf")))
3180 ;; Check for process failure. Provide collected errors if
3181 ;; possible.
3182 (if (or (not (file-exists-p pdffile))
3183 (time-less-p (nth 5 (file-attributes pdffile)) time))
3184 (error (format "PDF file %s wasn't produced" pdffile))
3185 ;; Else remove log files, when specified, and signal end of
3186 ;; process to user, along with any error encountered.
3187 (unless snippet
3188 (when org-latex-remove-logfiles
3189 (dolist (file (directory-files
3190 out-dir t
3191 (concat (regexp-quote base-name)
3192 "\\(?:\\.[0-9]+\\)?"
3193 "\\."
3194 (regexp-opt org-latex-logfiles-extensions))))
3195 (delete-file file)))
3196 (message (concat "PDF file produced"
3197 (cond
3198 ((eq warnings 'error) " with errors.")
3199 (warnings (concat " with warnings: " warnings))
3200 (t "."))))))
3201 ;; Return output file name.
3202 pdffile))))
3204 (defun org-latex--collect-warnings (buffer)
3205 "Collect some warnings from \"pdflatex\" command output.
3206 BUFFER is the buffer containing output. Return collected
3207 warnings types as a string, `error' if a LaTeX error was
3208 encountered or nil if there was none."
3209 (with-current-buffer buffer
3210 (save-excursion
3211 (goto-char (point-max))
3212 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3213 (if (re-search-forward "^!" nil t) 'error
3214 (let ((case-fold-search t)
3215 (warnings ""))
3216 (dolist (warning org-latex-known-warnings)
3217 (when (save-excursion (re-search-forward (car warning) nil t))
3218 (setq warnings (concat warnings " " (cdr warning)))))
3219 (org-string-nw-p (org-trim warnings))))))))
3221 ;;;###autoload
3222 (defun org-latex-publish-to-latex (plist filename pub-dir)
3223 "Publish an Org file to LaTeX.
3225 FILENAME is the filename of the Org file to be published. PLIST
3226 is the property list for the given project. PUB-DIR is the
3227 publishing directory.
3229 Return output file name."
3230 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3232 ;;;###autoload
3233 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3234 "Publish an Org file to PDF (via LaTeX).
3236 FILENAME is the filename of the Org file to be published. PLIST
3237 is the property list for the given project. PUB-DIR is the
3238 publishing directory.
3240 Return output file name."
3241 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3242 ;; in working directory and then moved to publishing directory.
3243 (org-publish-attachment
3244 plist
3245 (org-latex-compile
3246 (org-publish-org-to
3247 'latex filename ".tex" plist (file-name-directory filename)))
3248 pub-dir))
3251 (provide 'ox-latex)
3253 ;; Local variables:
3254 ;; generated-autoload-file: "org-loaddefs.el"
3255 ;; End:
3257 ;;; ox-latex.el ends here