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