Fix `org-hide-archived-subtrees'
[org-mode/org-tableheadings.git] / lisp / ox-latex.el
blobfb987525510b867816643a1c0475fd293888c9d4
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 protocol)
1970 (cond
1971 ;; Image file.
1972 (imagep (org-latex--inline-image link info))
1973 ;; Radio link: Transcode target's contents and use them as link's
1974 ;; description.
1975 ((string= type "radio")
1976 (let ((destination (org-export-resolve-radio-link link info)))
1977 (if (not destination) desc
1978 (format "\\hyperref[%s]{%s}"
1979 (org-export-solidify-link-text
1980 (org-element-property :value destination))
1981 desc))))
1982 ;; Links pointing to a headline: Find destination and build
1983 ;; appropriate referencing command.
1984 ((member type '("custom-id" "fuzzy" "id"))
1985 (let ((destination (if (string= type "fuzzy")
1986 (org-export-resolve-fuzzy-link link info)
1987 (org-export-resolve-id-link link info))))
1988 (case (org-element-type destination)
1989 ;; Id link points to an external file.
1990 (plain-text
1991 (if desc (format "\\href{%s}{%s}" destination desc)
1992 (format "\\url{%s}" destination)))
1993 ;; Fuzzy link points nowhere.
1994 ('nil
1995 (format (plist-get info :latex-link-with-unknown-path-format)
1996 (or desc
1997 (org-export-data
1998 (org-element-property :raw-link link) info))))
1999 ;; LINK points to a headline. If headlines are numbered
2000 ;; and the link has no description, display headline's
2001 ;; number. Otherwise, display description or headline's
2002 ;; title.
2003 (headline
2004 (let* ((custom-label
2005 (and (plist-get info :latex-custom-id-labels)
2006 (org-element-property :CUSTOM_ID destination)))
2007 (label (or custom-label
2008 (org-export-get-headline-id destination info))))
2009 (if (and (not desc)
2010 (org-export-numbered-headline-p destination info))
2011 (format "\\ref{%s}" label)
2012 (format "\\hyperref[%s]{%s}" label
2013 (or desc
2014 (org-export-data
2015 (org-element-property :title destination) info))))))
2016 ;; Fuzzy link points to a target. Do as above.
2017 (otherwise
2018 (let ((path (org-export-solidify-link-text path)))
2019 (if (not desc) (format "\\ref{%s}" path)
2020 (format "\\hyperref[%s]{%s}" path desc)))))))
2021 ;; Coderef: replace link with the reference name or the
2022 ;; equivalent line number.
2023 ((string= type "coderef")
2024 (format (org-export-get-coderef-format path desc)
2025 (org-export-resolve-coderef path info)))
2026 ;; Link type is handled by a special function.
2027 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
2028 (funcall protocol (org-link-unescape path) desc 'latex))
2029 ;; External link with a description part.
2030 ((and path desc) (format "\\href{%s}{%s}" path desc))
2031 ;; External link without a description part.
2032 (path (format "\\url{%s}" path))
2033 ;; No path, only description. Try to do something useful.
2034 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
2037 ;;;; Node Property
2039 (defun org-latex-node-property (node-property contents info)
2040 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2041 CONTENTS is nil. INFO is a plist holding contextual
2042 information."
2043 (format "%s:%s"
2044 (org-element-property :key node-property)
2045 (let ((value (org-element-property :value node-property)))
2046 (if value (concat " " value) ""))))
2049 ;;;; Paragraph
2051 (defun org-latex-paragraph (paragraph contents info)
2052 "Transcode a PARAGRAPH element from Org to LaTeX.
2053 CONTENTS is the contents of the paragraph, as a string. INFO is
2054 the plist used as a communication channel."
2055 contents)
2058 ;;;; Plain List
2060 (defun org-latex-plain-list (plain-list contents info)
2061 "Transcode a PLAIN-LIST element from Org to LaTeX.
2062 CONTENTS is the contents of the list. INFO is a plist holding
2063 contextual information."
2064 (let* ((type (org-element-property :type plain-list))
2065 (attr (org-export-read-attribute :attr_latex plain-list))
2066 (latex-type (let ((env (plist-get attr :environment)))
2067 (cond (env (format "%s" env))
2068 ((eq type 'ordered) "enumerate")
2069 ((eq type 'descriptive) "description")
2070 (t "itemize")))))
2071 (org-latex--wrap-label
2072 plain-list
2073 (format "\\begin{%s}%s\n%s\\end{%s}"
2074 latex-type
2075 (or (plist-get attr :options) "")
2076 contents
2077 latex-type))))
2080 ;;;; Plain Text
2082 (defun org-latex-plain-text (text info)
2083 "Transcode a TEXT string from Org to LaTeX.
2084 TEXT is the string to transcode. INFO is a plist holding
2085 contextual information."
2086 (let* ((specialp (plist-get info :with-special-strings))
2087 (output
2088 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2089 (let ((case-fold-search nil))
2090 (replace-regexp-in-string
2091 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2092 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2093 ;; However, if special strings are used, be careful not
2094 ;; to protect "\" in "\-" constructs.
2095 (replace-regexp-in-string
2096 (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\(?:[^-]\\|$\\)"))
2097 (lambda (m)
2098 (case (aref m 0)
2099 (?\\ "$\\\\backslash$")
2100 (?~ "\\\\textasciitilde{}")
2101 (?^ "\\\\^{}")
2102 (t "\\\\\\&")))
2103 text)))))
2104 ;; Activate smart quotes. Be sure to provide original TEXT string
2105 ;; since OUTPUT may have been modified.
2106 (when (plist-get info :with-smart-quotes)
2107 (setq output (org-export-activate-smart-quotes output :latex info text)))
2108 ;; Convert special strings.
2109 (when specialp
2110 (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
2111 ;; Handle break preservation if required.
2112 (when (plist-get info :preserve-breaks)
2113 (setq output (replace-regexp-in-string
2114 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
2115 ;; Return value.
2116 output))
2119 ;;;; Planning
2121 (defun org-latex-planning (planning contents info)
2122 "Transcode a PLANNING element from Org to LaTeX.
2123 CONTENTS is nil. INFO is a plist holding contextual
2124 information."
2125 (concat
2126 "\\noindent"
2127 (mapconcat
2128 'identity
2129 (delq nil
2130 (list
2131 (let ((closed (org-element-property :closed planning)))
2132 (when closed
2133 (concat
2134 (format "\\textbf{%s} " org-closed-string)
2135 (format (plist-get info :latex-inactive-timestamp-format)
2136 (org-timestamp-translate closed)))))
2137 (let ((deadline (org-element-property :deadline planning)))
2138 (when deadline
2139 (concat
2140 (format "\\textbf{%s} " org-deadline-string)
2141 (format (plist-get info :latex-active-timestamp-format)
2142 (org-timestamp-translate deadline)))))
2143 (let ((scheduled (org-element-property :scheduled planning)))
2144 (when scheduled
2145 (concat
2146 (format "\\textbf{%s} " org-scheduled-string)
2147 (format (plist-get info :latex-active-timestamp-format)
2148 (org-timestamp-translate scheduled)))))))
2149 " ")
2150 "\\\\"))
2153 ;;;; Property Drawer
2155 (defun org-latex-property-drawer (property-drawer contents info)
2156 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2157 CONTENTS holds the contents of the drawer. INFO is a plist
2158 holding contextual information."
2159 (and (org-string-nw-p contents)
2160 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2163 ;;;; Pseudo Element: LaTeX Matrices
2165 ;; `latex-matrices' elements have the following properties:
2166 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2167 ;; `math').
2169 (defun org-latex--wrap-latex-matrices (data info)
2170 "Merge contiguous tables with the same mode within a pseudo-element.
2171 DATA is a parse tree or a secondary string. INFO is a plist
2172 containing export options. Modify DATA by side-effect and return
2173 it."
2174 (org-element-map data 'table
2175 (lambda (table)
2176 (when (eq (org-element-property :type table) 'org)
2177 (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
2178 (plist-get info :latex-default-table-mode))))
2179 (when (and (member mode '("inline-math" "math"))
2180 ;; Do not wrap twice the same table.
2181 (not (eq (org-element-type
2182 (org-element-property :parent table))
2183 'latex-matrices)))
2184 (let* ((caption (and (not (string= mode "inline-math"))
2185 (org-element-property :caption table)))
2186 (matrices
2187 (list 'latex-matrices
2188 (list :caption caption
2189 :markup
2190 (cond ((string= mode "inline-math") 'inline)
2191 (caption 'equation)
2192 (t 'math)))))
2193 (previous table)
2194 (next (org-export-get-next-element table info)))
2195 (org-element-insert-before matrices table)
2196 ;; Swallow all contiguous tables sharing the same mode.
2197 (while (and
2198 (zerop (or (org-element-property :post-blank previous) 0))
2199 (setq next (org-export-get-next-element previous info))
2200 (eq (org-element-type next) 'table)
2201 (eq (org-element-property :type next) 'org)
2202 (string= (or (org-export-read-attribute
2203 :attr_latex next :mode)
2204 (plist-get info :latex-default-table-mode))
2205 mode))
2206 (org-element-extract-element previous)
2207 (org-element-adopt-elements matrices previous)
2208 (setq previous next))
2209 (org-element-put-property
2210 matrices :post-blank (org-element-property :post-blank previous))
2211 (org-element-extract-element previous)
2212 (org-element-adopt-elements matrices previous))))))
2213 info)
2214 data)
2216 (defun org-latex-matrices (matrices contents info)
2217 "Transcode a MATRICES element from Org to LaTeX.
2218 CONTENTS is a string. INFO is a plist used as a communication
2219 channel."
2220 (format (case (org-element-property :markup matrices)
2221 (inline "\\(%s\\)")
2222 (equation "\\begin{equation}\n%s\\end{equation}")
2223 (t "\\[\n%s\\]"))
2224 contents))
2226 (defun org-latex-matrices-tree-filter (tree backend info)
2227 (org-latex--wrap-latex-matrices tree info))
2229 ;;;; Pseudo Object: LaTeX Math Block
2231 ;; `latex-math-block' objects have the following property:
2232 ;; `:post-blank'.
2234 (defun org-latex--wrap-latex-math-block (data info)
2235 "Merge contiguous math objects in a pseudo-object container.
2236 DATA is a parse tree or a secondary string. INFO is a plist
2237 containing export options. Modify DATA by side-effect and return it."
2238 (let ((valid-object-p
2239 (function
2240 ;; Non-nil when OBJ can be added to the latex math block.
2241 (lambda (obj)
2242 (case (org-element-type obj)
2243 (entity (org-element-property :latex-math-p obj))
2244 (latex-fragment
2245 (let ((value (org-element-property :value obj)))
2246 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2247 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2248 ((subscript superscript) t))))))
2249 (org-element-map data '(entity latex-fragment subscript superscript)
2250 (lambda (object)
2251 ;; Skip objects already wrapped.
2252 (when (and (not (eq (org-element-type
2253 (org-element-property :parent object))
2254 'latex-math-block))
2255 (funcall valid-object-p object))
2256 (let ((math-block (list 'latex-math-block nil))
2257 (next-elements (org-export-get-next-element object info t))
2258 (last object))
2259 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2260 (org-element-insert-before math-block object)
2261 (org-element-extract-element object)
2262 (org-element-adopt-elements math-block object)
2263 (when (zerop (or (org-element-property :post-blank object) 0))
2264 ;; MATH-BLOCK swallows consecutive math objects.
2265 (catch 'exit
2266 (dolist (next next-elements)
2267 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2268 (org-element-extract-element next)
2269 (org-element-adopt-elements math-block next)
2270 ;; Eschew the case: \beta$x$ -> \(\betax\).
2271 (unless (memq (org-element-type next)
2272 '(subscript superscript))
2273 (org-element-put-property last :post-blank 1))
2274 (setq last next)
2275 (when (> (or (org-element-property :post-blank next) 0) 0)
2276 (throw 'exit nil))))))
2277 (org-element-put-property
2278 math-block :post-blank (org-element-property :post-blank last)))))
2279 info nil '(subscript superscript latex-math-block) t)
2280 ;; Return updated DATA.
2281 data))
2283 (defun org-latex-math-block-tree-filter (tree backend info)
2284 (org-latex--wrap-latex-math-block tree info))
2286 (defun org-latex-math-block-options-filter (info backend)
2287 (dolist (prop '(:author :date :title) info)
2288 (plist-put info prop
2289 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2291 (defun org-latex-math-block (math-block contents info)
2292 "Transcode a MATH-BLOCK object from Org to LaTeX.
2293 CONTENTS is a string. INFO is a plist used as a communication
2294 channel."
2295 (when (org-string-nw-p contents)
2296 (format "\\(%s\\)" (org-trim contents))))
2298 ;;;; Quote Block
2300 (defun org-latex-quote-block (quote-block contents info)
2301 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2302 CONTENTS holds the contents of the block. INFO is a plist
2303 holding contextual information."
2304 (org-latex--wrap-label
2305 quote-block
2306 (format "\\begin{quote}\n%s\\end{quote}" contents)))
2309 ;;;; Radio Target
2311 (defun org-latex-radio-target (radio-target text info)
2312 "Transcode a RADIO-TARGET object from Org to LaTeX.
2313 TEXT is the text of the target. INFO is a plist holding
2314 contextual information."
2315 (format "\\label{%s}%s"
2316 (org-export-solidify-link-text
2317 (org-element-property :value radio-target))
2318 text))
2321 ;;;; Section
2323 (defun org-latex-section (section contents info)
2324 "Transcode a SECTION element from Org to LaTeX.
2325 CONTENTS holds the contents of the section. INFO is a plist
2326 holding contextual information."
2327 contents)
2330 ;;;; Special Block
2332 (defun org-latex-special-block (special-block contents info)
2333 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2334 CONTENTS holds the contents of the block. INFO is a plist
2335 holding contextual information."
2336 (let ((type (org-element-property :type special-block))
2337 (opt (org-export-read-attribute :attr_latex special-block :options))
2338 (caption (org-latex--caption/label-string special-block info))
2339 (caption-above-p (org-latex--caption-above-p special-block info)))
2340 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2341 (and caption-above-p caption)
2342 contents
2343 (and (not caption-above-p) caption)
2344 (format "\\end{%s}" type))))
2347 ;;;; Src Block
2349 (defun org-latex-src-block (src-block contents info)
2350 "Transcode a SRC-BLOCK element from Org to LaTeX.
2351 CONTENTS holds the contents of the item. INFO is a plist holding
2352 contextual information."
2353 (when (org-string-nw-p (org-element-property :value src-block))
2354 (let* ((lang (org-element-property :language src-block))
2355 (caption (org-element-property :caption src-block))
2356 (caption-above-p (org-latex--caption-above-p src-block info))
2357 (label (org-element-property :name src-block))
2358 (custom-env (and lang
2359 (cadr (assq (intern lang)
2360 org-latex-custom-lang-environments))))
2361 (num-start (case (org-element-property :number-lines src-block)
2362 (continued (org-export-get-loc src-block info))
2363 (new 0)))
2364 (retain-labels (org-element-property :retain-labels src-block))
2365 (attributes (org-export-read-attribute :attr_latex src-block))
2366 (float (plist-get attributes :float))
2367 (listings (plist-get info :latex-listings)))
2368 (cond
2369 ;; Case 1. No source fontification.
2370 ((not listings)
2371 (let* ((caption-str (org-latex--caption/label-string src-block info))
2372 (float-env
2373 (cond ((and (not float) (plist-member attributes :float)) "%s")
2374 ((string= "multicolumn" float)
2375 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2376 (plist-get info :latex-default-figure-position)
2377 (if caption-above-p caption-str "")
2378 (if caption-above-p "" caption-str)))
2379 ((or caption float)
2380 (format "\\begin{figure}[H]\n%%s\n%s\\end{figure}"
2381 caption-str))
2382 (t "%s"))))
2383 (format
2384 float-env
2385 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2386 (org-export-format-code-default src-block info))))))
2387 ;; Case 2. Custom environment.
2388 (custom-env
2389 (let ((caption-str (org-latex--caption/label-string src-block info)))
2390 (format "\\begin{%s}\n%s\\end{%s}\n"
2391 custom-env
2392 (concat (and caption-above-p caption-str)
2393 (org-export-format-code-default src-block info)
2394 (and (not caption-above-p) caption-str))
2395 custom-env)))
2396 ;; Case 3. Use minted package.
2397 ((eq listings 'minted)
2398 (let* ((caption-str (org-latex--caption/label-string src-block info))
2399 (float-env
2400 (cond
2401 ((and (not float) (plist-member attributes :float) caption)
2402 (let ((caption
2403 (replace-regexp-in-string
2404 "\\\\caption" "\\captionof{listing}" caption-str
2405 t t)))
2406 (concat (and caption-above-p caption)
2407 "%%s"
2408 (and (not caption-above-p) (concat "\n" caption)))))
2409 ((and (not float) (plist-member attributes :float)) "%s")
2410 ((string= "multicolumn" float)
2411 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2412 (if caption-above-p caption-str "")
2413 (if caption-above-p "" caption-str)))
2414 ((or caption float)
2415 (format "\\begin{listing}[H]\n%s%%s\n%s\\end{listing}"
2416 (if caption-above-p caption-str "")
2417 (if caption-above-p "" caption-str)))
2418 (t "%s")))
2419 (options (plist-get info :latex-minted-options))
2420 (body
2421 (format
2422 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2423 ;; Options.
2424 (concat
2425 (org-latex--make-option-string
2426 (if (or (not num-start) (assoc "linenos" options))
2427 options
2428 (append
2429 `(("linenos")
2430 ("firstnumber" ,(number-to-string (1+ num-start))))
2431 options)))
2432 (let ((local-options (plist-get attributes :options)))
2433 (and local-options (concat "," local-options))))
2434 ;; Language.
2435 (or (cadr (assq (intern lang)
2436 (plist-get info :latex-minted-langs)))
2437 (downcase lang))
2438 ;; Source code.
2439 (let* ((code-info (org-export-unravel-code src-block))
2440 (max-width
2441 (apply 'max
2442 (mapcar 'length
2443 (org-split-string (car code-info)
2444 "\n")))))
2445 (org-export-format-code
2446 (car code-info)
2447 (lambda (loc num ref)
2448 (concat
2450 (when ref
2451 ;; Ensure references are flushed to the right,
2452 ;; separated with 6 spaces from the widest line
2453 ;; of code.
2454 (concat (make-string (+ (- max-width (length loc)) 6)
2455 ?\s)
2456 (format "(%s)" ref)))))
2457 nil (and retain-labels (cdr code-info)))))))
2458 ;; Return value.
2459 (format float-env body)))
2460 ;; Case 4. Use listings package.
2462 (let ((lst-lang
2463 (or (cadr (assq (intern lang)
2464 (plist-get info :latex-listings-langs)))
2465 lang))
2466 (caption-str
2467 (when caption
2468 (let ((main (org-export-get-caption src-block))
2469 (secondary (org-export-get-caption src-block t)))
2470 (if (not secondary)
2471 (format "{%s}" (org-export-data main info))
2472 (format "{[%s]%s}"
2473 (org-export-data secondary info)
2474 (org-export-data main info))))))
2475 (lst-opt (plist-get info :latex-listings-options)))
2476 (concat
2477 ;; Options.
2478 (format
2479 "\\lstset{%s}\n"
2480 (concat
2481 (org-latex--make-option-string
2482 (append
2483 lst-opt
2484 (cond
2485 ((and (not float) (plist-member attributes :float)) nil)
2486 ((string= "multicolumn" float) '(("float" "*")))
2487 ((and float (not (assoc "float" lst-opt)))
2488 `(("float" ,(plist-get info :latex-default-figure-position)))))
2489 `(("language" ,lst-lang))
2490 (if label `(("label" ,label)) '(("label" " ")))
2491 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2492 `(("captionpos" ,(if caption-above-p "t" "b")))
2493 (cond ((assoc "numbers" lst-opt) nil)
2494 ((not num-start) '(("numbers" "none")))
2495 ((zerop num-start) '(("numbers" "left")))
2496 (t `(("firstnumber" ,(number-to-string (1+ num-start)))
2497 ("numbers" "left"))))))
2498 (let ((local-options (plist-get attributes :options)))
2499 (and local-options (concat "," local-options)))))
2500 ;; Source code.
2501 (format
2502 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2503 (let* ((code-info (org-export-unravel-code src-block))
2504 (max-width
2505 (apply 'max
2506 (mapcar 'length
2507 (org-split-string (car code-info) "\n")))))
2508 (org-export-format-code
2509 (car code-info)
2510 (lambda (loc num ref)
2511 (concat
2513 (when ref
2514 ;; Ensure references are flushed to the right,
2515 ;; separated with 6 spaces from the widest line of
2516 ;; code
2517 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2518 (format "(%s)" ref)))))
2519 nil (and retain-labels (cdr code-info))))))))))))
2522 ;;;; Statistics Cookie
2524 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2525 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2526 CONTENTS is nil. INFO is a plist holding contextual information."
2527 (replace-regexp-in-string
2528 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2531 ;;;; Strike-Through
2533 (defun org-latex-strike-through (strike-through contents info)
2534 "Transcode STRIKE-THROUGH from Org to LaTeX.
2535 CONTENTS is the text with strike-through markup. INFO is a plist
2536 holding contextual information."
2537 (org-latex--text-markup contents 'strike-through info))
2540 ;;;; Subscript
2542 (defun org-latex--script-size (object info)
2543 "Transcode a subscript or superscript object.
2544 OBJECT is an Org object. INFO is a plist used as a communication
2545 channel."
2546 (let ((type (org-element-type object))
2547 (output ""))
2548 (org-element-map (org-element-contents object)
2549 (cons 'plain-text org-element-all-objects)
2550 (lambda (obj)
2551 (case (org-element-type obj)
2552 ((entity latex-fragment)
2553 (let ((data (org-trim (org-export-data obj info))))
2554 (string-match
2555 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2556 data)
2557 (setq output
2558 (concat output
2559 (match-string 1 data)
2560 (let ((blank (org-element-property :post-blank obj)))
2561 (and blank (> blank 0) "\\ "))))))
2562 (plain-text
2563 (setq output
2564 (format "%s\\text{%s}" output (org-export-data obj info))))
2565 (otherwise
2566 (setq output
2567 (concat output
2568 (org-export-data obj info)
2569 (let ((blank (org-element-property :post-blank obj)))
2570 (and blank (> blank 0) "\\ ")))))))
2571 info nil org-element-recursive-objects)
2572 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2573 ;; character.
2574 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2575 (and (> (length output) 1) "{")
2576 output
2577 (and (> (length output) 1) "}"))))
2579 (defun org-latex-subscript (subscript contents info)
2580 "Transcode a SUBSCRIPT object from Org to LaTeX.
2581 CONTENTS is the contents of the object. INFO is a plist holding
2582 contextual information."
2583 (org-latex--script-size subscript info))
2586 ;;;; Superscript
2588 (defun org-latex-superscript (superscript contents info)
2589 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2590 CONTENTS is the contents of the object. INFO is a plist holding
2591 contextual information."
2592 (org-latex--script-size superscript info))
2595 ;;;; Table
2597 ;; `org-latex-table' is the entry point for table transcoding. It
2598 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2599 ;; delegates the job to either `org-latex--table.el-table',
2600 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2601 ;; depending of the type of the table and the mode requested.
2603 ;; `org-latex--align-string' is a subroutine used to build alignment
2604 ;; string for Org tables.
2606 (defun org-latex-table (table contents info)
2607 "Transcode a TABLE element from Org to LaTeX.
2608 CONTENTS is the contents of the table. INFO is a plist holding
2609 contextual information."
2610 (if (eq (org-element-property :type table) 'table.el)
2611 ;; "table.el" table. Convert it using appropriate tools.
2612 (org-latex--table.el-table table info)
2613 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2614 (plist-get info :latex-default-table-mode))))
2615 (cond
2616 ;; Case 1: Verbatim table.
2617 ((string= type "verbatim")
2618 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2619 ;; Re-create table, without affiliated keywords.
2620 (org-trim (org-element-interpret-data
2621 `(table nil ,@(org-element-contents table))
2622 org-latex-pseudo-objects))))
2623 ;; Case 2: Matrix.
2624 ((or (string= type "math") (string= type "inline-math"))
2625 (org-latex--math-table table info))
2626 ;; Case 3: Standard table.
2627 (t (concat (org-latex--org-table table contents info)
2628 ;; When there are footnote references within the
2629 ;; table, insert their definition just after it.
2630 (org-latex--delayed-footnotes-definitions table info)))))))
2632 (defun org-latex--align-string (table info)
2633 "Return an appropriate LaTeX alignment string.
2634 TABLE is the considered table. INFO is a plist used as
2635 a communication channel."
2636 (or (org-export-read-attribute :attr_latex table :align)
2637 (let (align)
2638 ;; Extract column groups and alignment from first (non-rule)
2639 ;; row.
2640 (org-element-map
2641 (org-element-map table 'table-row
2642 (lambda (row)
2643 (and (eq (org-element-property :type row) 'standard) row))
2644 info 'first-match)
2645 'table-cell
2646 (lambda (cell)
2647 (let ((borders (org-export-table-cell-borders cell info)))
2648 ;; Check left border for the first cell only.
2649 (when (and (memq 'left borders) (not align))
2650 (push "|" align))
2651 (push (case (org-export-table-cell-alignment cell info)
2652 (left "l")
2653 (right "r")
2654 (center "c"))
2655 align)
2656 (when (memq 'right borders) (push "|" align))))
2657 info)
2658 (apply 'concat (nreverse align)))))
2660 (defun org-latex--org-table (table contents info)
2661 "Return appropriate LaTeX code for an Org table.
2663 TABLE is the table type element to transcode. CONTENTS is its
2664 contents, as a string. INFO is a plist used as a communication
2665 channel.
2667 This function assumes TABLE has `org' as its `:type' property and
2668 `table' as its `:mode' attribute."
2669 (let* ((caption (org-latex--caption/label-string table info))
2670 (attr (org-export-read-attribute :attr_latex table))
2671 ;; Determine alignment string.
2672 (alignment (org-latex--align-string table info))
2673 ;; Determine environment for the table: longtable, tabular...
2674 (table-env (or (plist-get attr :environment)
2675 (plist-get info :latex-default-table-environment)))
2676 ;; If table is a float, determine environment: table, table*
2677 ;; or sidewaystable.
2678 (float-env (unless (member table-env '("longtable" "longtabu"))
2679 (let ((float (plist-get attr :float)))
2680 (cond
2681 ((and (not float) (plist-member attr :float)) nil)
2682 ((or (string= float "sidewaystable")
2683 (string= float "sideways")) "sidewaystable")
2684 ((string= float "multicolumn") "table*")
2685 ((or float
2686 (org-element-property :caption table)
2687 (org-string-nw-p (plist-get attr :caption)))
2688 "table")))))
2689 ;; Extract others display options.
2690 (fontsize (let ((font (plist-get attr :font)))
2691 (and font (concat font "\n"))))
2692 (width (plist-get attr :width))
2693 (spreadp (plist-get attr :spread))
2694 (placement
2695 (or (plist-get attr :placement)
2696 (format "[%s]" (plist-get info :latex-default-figure-position))))
2697 (centerp (if (plist-member attr :center) (plist-get attr :center)
2698 (plist-get info :latex-tables-centered)))
2699 (caption-above-p (org-latex--caption-above-p table info)))
2700 ;; Prepare the final format string for the table.
2701 (cond
2702 ;; Longtable.
2703 ((equal "longtable" table-env)
2704 (concat (and fontsize (concat "{" fontsize))
2705 (format "\\begin{longtable}{%s}\n" alignment)
2706 (and caption-above-p
2707 (org-string-nw-p caption)
2708 (concat caption "\\\\\n"))
2709 contents
2710 (and (not caption-above-p)
2711 (org-string-nw-p caption)
2712 (concat caption "\\\\\n"))
2713 "\\end{longtable}\n"
2714 (and fontsize "}")))
2715 ;; Longtabu
2716 ((equal "longtabu" table-env)
2717 (concat (and fontsize (concat "{" fontsize))
2718 (format "\\begin{longtabu}%s{%s}\n"
2719 (if width
2720 (format " %s %s "
2721 (if spreadp "spread" "to") width) "")
2722 alignment)
2723 (and caption-above-p
2724 (org-string-nw-p caption)
2725 (concat caption "\\\\\n"))
2726 contents
2727 (and (not caption-above-p)
2728 (org-string-nw-p caption)
2729 (concat caption "\\\\\n"))
2730 "\\end{longtabu}\n"
2731 (and fontsize "}")))
2732 ;; Others.
2733 (t (concat (cond
2734 (float-env
2735 (concat (format "\\begin{%s}%s\n" float-env placement)
2736 (if caption-above-p caption "")
2737 (when centerp "\\centering\n")
2738 fontsize))
2739 (centerp (concat "\\begin{center}\n" fontsize))
2740 (fontsize (concat "{" fontsize)))
2741 (cond ((equal "tabu" table-env)
2742 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2743 (if width (format
2744 (if spreadp " spread %s " " to %s ")
2745 width) "")
2746 alignment
2747 contents))
2748 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2749 table-env
2750 (if width (format "{%s}" width) "")
2751 alignment
2752 contents
2753 table-env)))
2754 (cond
2755 (float-env
2756 (concat (if caption-above-p "" caption)
2757 (format "\n\\end{%s}" float-env)))
2758 (centerp "\n\\end{center}")
2759 (fontsize "}")))))))
2761 (defun org-latex--table.el-table (table info)
2762 "Return appropriate LaTeX code for a table.el table.
2764 TABLE is the table type element to transcode. INFO is a plist
2765 used as a communication channel.
2767 This function assumes TABLE has `table.el' as its `:type'
2768 property."
2769 (require 'table)
2770 ;; Ensure "*org-export-table*" buffer is empty.
2771 (with-current-buffer (get-buffer-create "*org-export-table*")
2772 (erase-buffer))
2773 (let ((output (with-temp-buffer
2774 (insert (org-element-property :value table))
2775 (goto-char 1)
2776 (re-search-forward "^[ \t]*|[^|]" nil t)
2777 (table-generate-source 'latex "*org-export-table*")
2778 (with-current-buffer "*org-export-table*"
2779 (org-trim (buffer-string))))))
2780 (kill-buffer (get-buffer "*org-export-table*"))
2781 ;; Remove left out comments.
2782 (while (string-match "^%.*\n" output)
2783 (setq output (replace-match "" t t output)))
2784 (let ((attr (org-export-read-attribute :attr_latex table)))
2785 (when (plist-get attr :rmlines)
2786 ;; When the "rmlines" attribute is provided, remove all hlines
2787 ;; but the the one separating heading from the table body.
2788 (let ((n 0) (pos 0))
2789 (while (and (< (length output) pos)
2790 (setq pos (string-match "^\\\\hline\n?" output pos)))
2791 (incf n)
2792 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2793 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2794 (plist-get info :latex-tables-centered))))
2795 (if (not centerp) output
2796 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2798 (defun org-latex--math-table (table info)
2799 "Return appropriate LaTeX code for a matrix.
2801 TABLE is the table type element to transcode. INFO is a plist
2802 used as a communication channel.
2804 This function assumes TABLE has `org' as its `:type' property and
2805 `inline-math' or `math' as its `:mode' attribute."
2806 (let* ((attr (org-export-read-attribute :attr_latex table))
2807 (env (or (plist-get attr :environment)
2808 (plist-get info :latex-default-table-environment)))
2809 (contents
2810 (mapconcat
2811 (lambda (row)
2812 ;; Ignore horizontal rules.
2813 (when (eq (org-element-property :type row) 'standard)
2814 ;; Return each cell unmodified.
2815 (concat
2816 (mapconcat
2817 (lambda (cell)
2818 (substring
2819 (org-element-interpret-data cell org-latex-pseudo-objects)
2820 0 -1))
2821 (org-element-map row 'table-cell #'identity info) "&")
2822 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2823 "\n")))
2824 (org-element-map table 'table-row #'identity info) "")))
2825 (concat
2826 ;; Prefix.
2827 (plist-get attr :math-prefix)
2828 ;; Environment. Also treat special cases.
2829 (cond ((member env '("array" "tabular"))
2830 (let ((align (make-string
2831 (cdr (org-export-table-dimensions table info)) ?c)))
2832 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
2833 ((assoc env org-latex-table-matrix-macros)
2834 (format "\\%s%s{\n%s}"
2836 (or (plist-get attr :math-arguments) "")
2837 contents))
2838 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2839 ;; Suffix.
2840 (plist-get attr :math-suffix))))
2843 ;;;; Table Cell
2845 (defun org-latex-table-cell (table-cell contents info)
2846 "Transcode a TABLE-CELL element from Org to LaTeX.
2847 CONTENTS is the cell contents. INFO is a plist used as
2848 a communication channel."
2849 (concat
2850 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
2851 (if (and contents
2852 scientific-format
2853 (string-match orgtbl-exp-regexp contents))
2854 ;; Use appropriate format string for scientific
2855 ;; notation.
2856 (format scientific-format
2857 (match-string 1 contents)
2858 (match-string 2 contents))
2859 contents))
2860 (when (org-export-get-next-element table-cell info) " & ")))
2863 ;;;; Table Row
2865 (defun org-latex-table-row (table-row contents info)
2866 "Transcode a TABLE-ROW element from Org to LaTeX.
2867 CONTENTS is the contents of the row. INFO is a plist used as
2868 a communication channel."
2869 (let* ((attr (org-export-read-attribute :attr_latex
2870 (org-export-get-parent table-row)))
2871 (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
2872 (plist-get info :latex-tables-booktabs)))
2873 (longtablep
2874 (member (or (plist-get attr :environment)
2875 (plist-get info :latex-default-table-environment))
2876 '("longtable" "longtabu"))))
2877 (if (eq (org-element-property :type table-row) 'rule)
2878 (cond
2879 ((not booktabsp) "\\hline")
2880 ((not (org-export-get-previous-element table-row info)) "\\toprule")
2881 ((not (org-export-get-next-element table-row info)) "\\bottomrule")
2882 ((and longtablep
2883 (org-export-table-row-ends-header-p
2884 (org-export-get-previous-element table-row info) info))
2886 (t "\\midrule"))
2887 (concat
2888 ;; When BOOKTABS are activated enforce top-rule even when no
2889 ;; hline was specifically marked.
2890 (and booktabsp (not (org-export-get-previous-element table-row info))
2891 "\\toprule\n")
2892 contents "\\\\\n"
2893 (cond
2894 ;; Special case for long tables. Define header and footers.
2895 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2896 (let ((columns (cdr (org-export-table-dimensions
2897 (org-export-get-parent-table table-row) info))))
2898 (format "%s
2899 \\endfirsthead
2900 \\multicolumn{%d}{l}{%s} \\\\
2902 %s \\\\\n
2904 \\endhead
2905 %s\\multicolumn{%d}{r}{%s} \\\\
2906 \\endfoot
2907 \\endlastfoot"
2908 (if booktabsp "\\midrule" "\\hline")
2909 columns
2910 (org-latex--translate "Continued from previous page" info)
2911 (cond
2912 ((not (org-export-table-row-starts-header-p table-row info))
2914 (booktabsp "\\toprule\n")
2915 (t "\\hline\n"))
2916 contents
2917 (if booktabsp "\\midrule" "\\hline")
2918 (if booktabsp "\\midrule" "\\hline")
2919 columns
2920 (org-latex--translate "Continued on next page" info))))
2921 ;; When BOOKTABS are activated enforce bottom rule even when
2922 ;; no hline was specifically marked.
2923 ((and booktabsp (not (org-export-get-next-element table-row info)))
2924 "\\bottomrule"))))))
2927 ;;;; Target
2929 (defun org-latex-target (target contents info)
2930 "Transcode a TARGET object from Org to LaTeX.
2931 CONTENTS is nil. INFO is a plist holding contextual
2932 information."
2933 (format "\\label{%s}"
2934 (org-export-solidify-link-text (org-element-property :value target))))
2937 ;;;; Timestamp
2939 (defun org-latex-timestamp (timestamp contents info)
2940 "Transcode a TIMESTAMP object from Org to LaTeX.
2941 CONTENTS is nil. INFO is a plist holding contextual
2942 information."
2943 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
2944 (format
2945 (plist-get info
2946 (case (org-element-property :type timestamp)
2947 ((active active-range) :latex-active-timestamp-format)
2948 ((inactive inactive-range) :latex-inactive-timestamp-format)
2949 (otherwise :latex-diary-timestamp-format)))
2950 value)))
2953 ;;;; Underline
2955 (defun org-latex-underline (underline contents info)
2956 "Transcode UNDERLINE from Org to LaTeX.
2957 CONTENTS is the text with underline markup. INFO is a plist
2958 holding contextual information."
2959 (org-latex--text-markup contents 'underline info))
2962 ;;;; Verbatim
2964 (defun org-latex-verbatim (verbatim contents info)
2965 "Transcode a VERBATIM object from Org to LaTeX.
2966 CONTENTS is nil. INFO is a plist used as a communication
2967 channel."
2968 (org-latex--text-markup
2969 (org-element-property :value verbatim) 'verbatim info))
2972 ;;;; Verse Block
2974 (defun org-latex-verse-block (verse-block contents info)
2975 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2976 CONTENTS is verse block contents. INFO is a plist holding
2977 contextual information."
2978 (org-latex--wrap-label
2979 verse-block
2980 ;; In a verse environment, add a line break to each newline
2981 ;; character and change each white space at beginning of a line
2982 ;; into a space of 1 em. Also change each blank line with
2983 ;; a vertical space of 1 em.
2984 (format "\\begin{verse}\n%s\\end{verse}"
2985 (replace-regexp-in-string
2986 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
2987 (replace-regexp-in-string
2988 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
2989 (replace-regexp-in-string
2990 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
2991 contents nil t) nil t) nil t))))
2995 ;;; End-user functions
2997 ;;;###autoload
2998 (defun org-latex-export-as-latex
2999 (&optional async subtreep visible-only body-only ext-plist)
3000 "Export current buffer as a LaTeX buffer.
3002 If narrowing is active in the current buffer, only export its
3003 narrowed part.
3005 If a region is active, export that region.
3007 A non-nil optional argument ASYNC means the process should happen
3008 asynchronously. The resulting buffer should be accessible
3009 through the `org-export-stack' interface.
3011 When optional argument SUBTREEP is non-nil, export the sub-tree
3012 at point, extracting information from the headline properties
3013 first.
3015 When optional argument VISIBLE-ONLY is non-nil, don't export
3016 contents of hidden elements.
3018 When optional argument BODY-ONLY is non-nil, only write code
3019 between \"\\begin{document}\" and \"\\end{document}\".
3021 EXT-PLIST, when provided, is a property list with external
3022 parameters overriding Org default settings, but still inferior to
3023 file-local settings.
3025 Export is done in a buffer named \"*Org LATEX Export*\", which
3026 will be displayed when `org-export-show-temporary-export-buffer'
3027 is non-nil."
3028 (interactive)
3029 (org-export-to-buffer 'latex "*Org LATEX Export*"
3030 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
3032 ;;;###autoload
3033 (defun org-latex-convert-region-to-latex ()
3034 "Assume the current region has org-mode syntax, and convert it to LaTeX.
3035 This can be used in any buffer. For example, you can write an
3036 itemized list in org-mode syntax in an LaTeX buffer and use this
3037 command to convert it."
3038 (interactive)
3039 (org-export-replace-region-by 'latex))
3041 ;;;###autoload
3042 (defun org-latex-export-to-latex
3043 (&optional async subtreep visible-only body-only ext-plist)
3044 "Export current buffer to a LaTeX file.
3046 If narrowing is active in the current buffer, only export its
3047 narrowed part.
3049 If a region is active, export that region.
3051 A non-nil optional argument ASYNC means the process should happen
3052 asynchronously. The resulting file should be accessible through
3053 the `org-export-stack' interface.
3055 When optional argument SUBTREEP is non-nil, export the sub-tree
3056 at point, extracting information from the headline properties
3057 first.
3059 When optional argument VISIBLE-ONLY is non-nil, don't export
3060 contents of hidden elements.
3062 When optional argument BODY-ONLY is non-nil, only write code
3063 between \"\\begin{document}\" and \"\\end{document}\".
3065 EXT-PLIST, when provided, is a property list with external
3066 parameters overriding Org default settings, but still inferior to
3067 file-local settings."
3068 (interactive)
3069 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3070 (org-export-to-file 'latex outfile
3071 async subtreep visible-only body-only ext-plist)))
3073 ;;;###autoload
3074 (defun org-latex-export-to-pdf
3075 (&optional async subtreep visible-only body-only ext-plist)
3076 "Export current buffer to LaTeX then process through to PDF.
3078 If narrowing is active in the current buffer, only export its
3079 narrowed part.
3081 If a region is active, export that region.
3083 A non-nil optional argument ASYNC means the process should happen
3084 asynchronously. The resulting file should be accessible through
3085 the `org-export-stack' interface.
3087 When optional argument SUBTREEP is non-nil, export the sub-tree
3088 at point, extracting information from the headline properties
3089 first.
3091 When optional argument VISIBLE-ONLY is non-nil, don't export
3092 contents of hidden elements.
3094 When optional argument BODY-ONLY is non-nil, only write code
3095 between \"\\begin{document}\" and \"\\end{document}\".
3097 EXT-PLIST, when provided, is a property list with external
3098 parameters overriding Org default settings, but still inferior to
3099 file-local settings.
3101 Return PDF file's name."
3102 (interactive)
3103 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3104 (org-export-to-file 'latex outfile
3105 async subtreep visible-only body-only ext-plist
3106 (lambda (file) (org-latex-compile file)))))
3108 (defun org-latex-compile (texfile &optional snippet)
3109 "Compile a TeX file.
3111 TEXFILE is the name of the file being compiled. Processing is
3112 done through the command specified in `org-latex-pdf-process'.
3114 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3115 file used to preview a LaTeX snippet. In this case, do not
3116 create a log buffer and do not bother removing log files.
3118 Return PDF file name or an error if it couldn't be produced."
3119 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3120 (full-name (file-truename texfile))
3121 (out-dir (file-name-directory texfile))
3122 ;; Properly set working directory for compilation.
3123 (default-directory (if (file-name-absolute-p texfile)
3124 (file-name-directory full-name)
3125 default-directory))
3126 (time (current-time))
3127 warnings)
3128 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
3129 (save-window-excursion
3130 (cond
3131 ;; A function is provided: Apply it.
3132 ((functionp org-latex-pdf-process)
3133 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3134 ;; A list is provided: Replace %b, %f and %o with appropriate
3135 ;; values in each command before applying it. Output is
3136 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3137 ((consp org-latex-pdf-process)
3138 (let ((outbuf (and (not snippet)
3139 (get-buffer-create "*Org PDF LaTeX Output*"))))
3140 (dolist (command org-latex-pdf-process)
3141 (shell-command
3142 (replace-regexp-in-string
3143 "%b" (shell-quote-argument base-name)
3144 (replace-regexp-in-string
3145 "%f" (shell-quote-argument full-name)
3146 (replace-regexp-in-string
3147 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
3148 outbuf))
3149 ;; Collect standard errors from output buffer.
3150 (setq warnings (and (not snippet)
3151 (org-latex--collect-warnings outbuf)))))
3152 (t (error "No valid command to process to PDF")))
3153 (let ((pdffile (concat out-dir base-name ".pdf")))
3154 ;; Check for process failure. Provide collected errors if
3155 ;; possible.
3156 (if (or (not (file-exists-p pdffile))
3157 (time-less-p (nth 5 (file-attributes pdffile)) time))
3158 (error (format "PDF file %s wasn't produced" pdffile))
3159 ;; Else remove log files, when specified, and signal end of
3160 ;; process to user, along with any error encountered.
3161 (unless snippet
3162 (when org-latex-remove-logfiles
3163 (dolist (file (directory-files
3164 out-dir t
3165 (concat (regexp-quote base-name)
3166 "\\(?:\\.[0-9]+\\)?"
3167 "\\."
3168 (regexp-opt org-latex-logfiles-extensions))))
3169 (delete-file file)))
3170 (message (concat "PDF file produced"
3171 (cond
3172 ((eq warnings 'error) " with errors.")
3173 (warnings (concat " with warnings: " warnings))
3174 (t "."))))))
3175 ;; Return output file name.
3176 pdffile))))
3178 (defun org-latex--collect-warnings (buffer)
3179 "Collect some warnings from \"pdflatex\" command output.
3180 BUFFER is the buffer containing output. Return collected
3181 warnings types as a string, `error' if a LaTeX error was
3182 encountered or nil if there was none."
3183 (with-current-buffer buffer
3184 (save-excursion
3185 (goto-char (point-max))
3186 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3187 (if (re-search-forward "^!" nil t) 'error
3188 (let ((case-fold-search t)
3189 (warnings ""))
3190 (dolist (warning org-latex-known-warnings)
3191 (when (save-excursion (re-search-forward (car warning) nil t))
3192 (setq warnings (concat warnings " " (cdr warning)))))
3193 (org-string-nw-p (org-trim warnings))))))))
3195 ;;;###autoload
3196 (defun org-latex-publish-to-latex (plist filename pub-dir)
3197 "Publish an Org file to LaTeX.
3199 FILENAME is the filename of the Org file to be published. PLIST
3200 is the property list for the given project. PUB-DIR is the
3201 publishing directory.
3203 Return output file name."
3204 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3206 ;;;###autoload
3207 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3208 "Publish an Org file to PDF (via LaTeX).
3210 FILENAME is the filename of the Org file to be published. PLIST
3211 is the property list for the given project. PUB-DIR is the
3212 publishing directory.
3214 Return output file name."
3215 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3216 ;; in working directory and then moved to publishing directory.
3217 (org-publish-attachment
3218 plist
3219 (org-latex-compile
3220 (org-publish-org-to
3221 'latex filename ".tex" plist (file-name-directory filename)))
3222 pub-dir))
3225 (provide 'ox-latex)
3227 ;; Local variables:
3228 ;; generated-autoload-file: "org-loaddefs.el"
3229 ;; End:
3231 ;;; ox-latex.el ends here