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