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