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