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