org-timer: Fix "No running timer" error
[org-mode/org-tableheadings.git] / lisp / ox-latex.el
blob27eb84c1588efafe6f693dd8f97e7aab916de8d3
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-translate-time
1322 (org-element-property :raw-value
1323 (org-element-property :value clock)))
1324 (let ((time (org-element-property :duration clock)))
1325 (and time (format " (%s)" time)))))
1326 "\\\\"))
1329 ;;;; Code
1331 (defun org-latex-code (code contents info)
1332 "Transcode a CODE object from Org to LaTeX.
1333 CONTENTS is nil. INFO is a plist used as a communication
1334 channel."
1335 (org-latex--text-markup (org-element-property :value code) 'code info))
1338 ;;;; Drawer
1340 (defun org-latex-drawer (drawer contents info)
1341 "Transcode a DRAWER element from Org to LaTeX.
1342 CONTENTS holds the contents of the block. INFO is a plist
1343 holding contextual information."
1344 (let* ((name (org-element-property :drawer-name drawer))
1345 (output (funcall (plist-get info :latex-format-drawer-function)
1346 name contents)))
1347 (org-latex--wrap-label drawer output)))
1350 ;;;; Dynamic Block
1352 (defun org-latex-dynamic-block (dynamic-block contents info)
1353 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1354 CONTENTS holds the contents of the block. INFO is a plist
1355 holding contextual information. See `org-export-data'."
1356 (org-latex--wrap-label dynamic-block contents))
1359 ;;;; Entity
1361 (defun org-latex-entity (entity contents info)
1362 "Transcode an ENTITY object from Org to LaTeX.
1363 CONTENTS are the definition itself. INFO is a plist holding
1364 contextual information."
1365 (org-element-property :latex entity))
1368 ;;;; Example Block
1370 (defun org-latex-example-block (example-block contents info)
1371 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1372 CONTENTS is nil. INFO is a plist holding contextual
1373 information."
1374 (when (org-string-nw-p (org-element-property :value example-block))
1375 (org-latex--wrap-label
1376 example-block
1377 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1378 (org-export-format-code-default example-block info)))))
1381 ;;;; Export Block
1383 (defun org-latex-export-block (export-block contents info)
1384 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1385 CONTENTS is nil. INFO is a plist holding contextual information."
1386 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1387 (org-remove-indentation (org-element-property :value export-block))))
1390 ;;;; Export Snippet
1392 (defun org-latex-export-snippet (export-snippet contents info)
1393 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1394 CONTENTS is nil. INFO is a plist holding contextual information."
1395 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1396 (org-element-property :value export-snippet)))
1399 ;;;; Fixed Width
1401 (defun org-latex-fixed-width (fixed-width contents info)
1402 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1403 CONTENTS is nil. INFO is a plist holding contextual information."
1404 (org-latex--wrap-label
1405 fixed-width
1406 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1407 (org-remove-indentation
1408 (org-element-property :value fixed-width)))))
1411 ;;;; Footnote Reference
1413 (defun org-latex-footnote-reference (footnote-reference contents info)
1414 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1415 CONTENTS is nil. INFO is a plist holding contextual information."
1416 (concat
1417 ;; Insert separator between two footnotes in a row.
1418 (let ((prev (org-export-get-previous-element footnote-reference info)))
1419 (when (eq (org-element-type prev) 'footnote-reference)
1420 (plist-get info :latex-footnote-separator)))
1421 (cond
1422 ;; Use \footnotemark if the footnote has already been defined.
1423 ((not (org-export-footnote-first-reference-p footnote-reference info))
1424 (format "\\footnotemark[%s]{}"
1425 (org-export-get-footnote-number footnote-reference info)))
1426 ;; Use \footnotemark if reference is within another footnote
1427 ;; reference, footnote definition or table cell.
1428 ((org-element-lineage footnote-reference
1429 '(footnote-reference footnote-definition table-cell))
1430 "\\footnotemark")
1431 ;; Otherwise, define it with \footnote command.
1433 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1434 (concat
1435 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1436 ;; Retrieve all footnote references within the footnote and
1437 ;; add their definition after it, since LaTeX doesn't support
1438 ;; them inside.
1439 (org-latex--delayed-footnotes-definitions def info)))))))
1442 ;;;; Headline
1444 (defun org-latex-headline (headline contents info)
1445 "Transcode a HEADLINE element from Org to LaTeX.
1446 CONTENTS holds the contents of the headline. INFO is a plist
1447 holding contextual information."
1448 (unless (org-element-property :footnote-section-p headline)
1449 (let* ((class (plist-get info :latex-class))
1450 (level (org-export-get-relative-level headline info))
1451 (numberedp (org-export-numbered-headline-p headline info))
1452 (class-sectioning (assoc class (plist-get info :latex-classes)))
1453 ;; Section formatting will set two placeholders: one for
1454 ;; the title and the other for the contents.
1455 (section-fmt
1456 (let ((sec (if (functionp (nth 2 class-sectioning))
1457 (funcall (nth 2 class-sectioning) level numberedp)
1458 (nth (1+ level) class-sectioning))))
1459 (cond
1460 ;; No section available for that LEVEL.
1461 ((not sec) nil)
1462 ;; Section format directly returned by a function. Add
1463 ;; placeholder for contents.
1464 ((stringp sec) (concat sec "\n%s"))
1465 ;; (numbered-section . unnumbered-section)
1466 ((not (consp (cdr sec)))
1467 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1468 ;; (numbered-open numbered-close)
1469 ((= (length sec) 2)
1470 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1471 ;; (num-in num-out no-num-in no-num-out)
1472 ((= (length sec) 4)
1473 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1474 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1475 ;; Create a temporary export back-end that hard-codes
1476 ;; "\underline" within "\section" and alike.
1477 (section-back-end
1478 (org-export-create-backend
1479 :parent 'latex
1480 :transcoders
1481 '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
1482 (text
1483 (org-export-data-with-backend
1484 (org-element-property :title headline) section-back-end info))
1485 (todo
1486 (and (plist-get info :with-todo-keywords)
1487 (let ((todo (org-element-property :todo-keyword headline)))
1488 (and todo (org-export-data todo info)))))
1489 (todo-type (and todo (org-element-property :todo-type headline)))
1490 (tags (and (plist-get info :with-tags)
1491 (org-export-get-tags headline info)))
1492 (priority (and (plist-get info :with-priority)
1493 (org-element-property :priority headline)))
1494 ;; Create the headline text along with a no-tag version.
1495 ;; The latter is required to remove tags from toc.
1496 (full-text (funcall (plist-get info :latex-format-headline-function)
1497 todo todo-type priority text tags info))
1498 ;; Associate \label to the headline for internal links.
1499 (headline-label
1500 (format "\\label{%s}\n"
1501 (or (and (plist-get info :latex-custom-id-labels)
1502 (org-element-property :CUSTOM_ID headline))
1503 (org-export-get-headline-id headline info))))
1504 (pre-blanks
1505 (make-string (org-element-property :pre-blank headline) 10)))
1506 (if (or (not section-fmt) (org-export-low-level-p headline info))
1507 ;; This is a deep sub-tree: export it as a list item. Also
1508 ;; export as items headlines for which no section format has
1509 ;; been found.
1510 (let ((low-level-body
1511 (concat
1512 ;; If headline is the first sibling, start a list.
1513 (when (org-export-first-sibling-p headline info)
1514 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1515 ;; Itemize headline
1516 "\\item"
1517 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
1518 "\\relax")
1519 " " full-text "\n"
1520 headline-label
1521 pre-blanks
1522 contents)))
1523 ;; If headline is not the last sibling simply return
1524 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1525 ;; any blank line.
1526 (if (not (org-export-last-sibling-p headline info)) low-level-body
1527 (replace-regexp-in-string
1528 "[ \t\n]*\\'"
1529 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1530 low-level-body)))
1531 ;; This is a standard headline. Export it as a section. Add
1532 ;; an alternative heading when possible, and when this is not
1533 ;; identical to the usual heading.
1534 (let ((opt-title
1535 (funcall (plist-get info :latex-format-headline-function)
1536 todo todo-type priority
1537 (org-export-data-with-backend
1538 (org-export-get-alt-title headline info)
1539 section-back-end info)
1540 (and (eq (plist-get info :with-tags) t) tags)
1541 info)))
1542 (if (and numberedp opt-title
1543 (not (equal opt-title full-text))
1544 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
1545 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1546 ;; Replace square brackets with parenthesis
1547 ;; since square brackets are not supported in
1548 ;; optional arguments.
1549 (replace-regexp-in-string
1550 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1551 full-text
1552 (concat headline-label pre-blanks contents))
1553 ;; Impossible to add an alternative heading. Fallback to
1554 ;; regular sectioning format string.
1555 (format section-fmt full-text
1556 (concat headline-label pre-blanks contents))))))))
1558 (defun org-latex-format-headline-default-function
1559 (todo todo-type priority text tags info)
1560 "Default format function for a headline.
1561 See `org-latex-format-headline-function' for details."
1562 (concat
1563 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1564 (and priority (format "\\framebox{\\#%c} " priority))
1565 text
1566 (and tags
1567 (format "\\hfill{}\\textsc{%s}"
1568 (mapconcat (lambda (tag) (org-latex-plain-text tag info))
1569 tags ":")))))
1572 ;;;; Horizontal Rule
1574 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1575 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1576 CONTENTS is nil. INFO is a plist holding contextual information."
1577 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1578 (prev (org-export-get-previous-element horizontal-rule info)))
1579 (concat
1580 ;; Make sure the rule doesn't start at the end of the current
1581 ;; line by separating it with a blank line from previous element.
1582 (when (and prev
1583 (let ((prev-blank (org-element-property :post-blank prev)))
1584 (or (not prev-blank) (zerop prev-blank))))
1585 "\n")
1586 (org-latex--wrap-label
1587 horizontal-rule
1588 (format "\\rule{%s}{%s}"
1589 (or (plist-get attr :width) "\\linewidth")
1590 (or (plist-get attr :thickness) "0.5pt"))))))
1593 ;;;; Inline Src Block
1595 (defun org-latex-inline-src-block (inline-src-block contents info)
1596 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1597 CONTENTS holds the contents of the item. INFO is a plist holding
1598 contextual information."
1599 (let* ((code (org-element-property :value inline-src-block))
1600 (separator (org-latex--find-verb-separator code)))
1601 (case (plist-get info :latex-listings)
1602 ;; Do not use a special package: transcode it verbatim.
1603 ((nil) (concat "\\verb" separator code separator))
1604 ;; Use minted package.
1605 (minted
1606 (let* ((org-lang (org-element-property :language inline-src-block))
1607 (mint-lang (or (cadr (assq (intern org-lang)
1608 (plist-get info :latex-minted-langs)))
1609 (downcase org-lang)))
1610 (options (org-latex--make-option-string
1611 (plist-get info :latex-minted-options))))
1612 (concat (format "\\mint%s{%s}"
1613 (if (string= options "") "" (format "[%s]" options))
1614 mint-lang)
1615 separator code separator)))
1616 ;; Use listings package.
1617 (otherwise
1618 ;; Maybe translate language's name.
1619 (let* ((org-lang (org-element-property :language inline-src-block))
1620 (lst-lang (or (cadr (assq (intern org-lang)
1621 (plist-get info :latex-listings-langs)))
1622 org-lang))
1623 (options (org-latex--make-option-string
1624 (append (plist-get info :latex-listings-options)
1625 `(("language" ,lst-lang))))))
1626 (concat (format "\\lstinline[%s]" options)
1627 separator code separator))))))
1630 ;;;; Inlinetask
1632 (defun org-latex-inlinetask (inlinetask contents info)
1633 "Transcode an INLINETASK element from Org to LaTeX.
1634 CONTENTS holds the contents of the block. INFO is a plist
1635 holding contextual information."
1636 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1637 (todo (and (plist-get info :with-todo-keywords)
1638 (let ((todo (org-element-property :todo-keyword inlinetask)))
1639 (and todo (org-export-data todo info)))))
1640 (todo-type (org-element-property :todo-type inlinetask))
1641 (tags (and (plist-get info :with-tags)
1642 (org-export-get-tags inlinetask info)))
1643 (priority (and (plist-get info :with-priority)
1644 (org-element-property :priority inlinetask)))
1645 (contents (concat
1646 (let ((label (org-element-property :CUSTOM_ID inlinetask)))
1647 (and label (format "\\label{%s}\n" label)))
1648 contents)))
1649 (funcall (plist-get info :latex-format-inlinetask-function)
1650 todo todo-type priority title tags contents info)))
1652 (defun org-latex-format-inlinetask-default-function
1653 (todo todo-type priority title tags contents info)
1654 "Default format function for a inlinetasks.
1655 See `org-latex-format-inlinetask-function' for details."
1656 (let ((full-title
1657 (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1658 (when priority (format "\\framebox{\\#%c} " priority))
1659 title
1660 (when tags
1661 (format "\\hfill{}\\textsc{:%s:}"
1662 (mapconcat
1663 (lambda (tag) (org-latex-plain-text tag info))
1664 tags ":"))))))
1665 (concat "\\begin{center}\n"
1666 "\\fbox{\n"
1667 "\\begin{minipage}[c]{.6\\textwidth}\n"
1668 full-title "\n\n"
1669 (and (org-string-nw-p contents)
1670 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
1671 "\\end{minipage}\n"
1672 "}\n"
1673 "\\end{center}")))
1676 ;;;; Italic
1678 (defun org-latex-italic (italic contents info)
1679 "Transcode ITALIC from Org to LaTeX.
1680 CONTENTS is the text with italic markup. INFO is a plist holding
1681 contextual information."
1682 (org-latex--text-markup contents 'italic info))
1685 ;;;; Item
1687 (defun org-latex-item (item contents info)
1688 "Transcode an ITEM element from Org to LaTeX.
1689 CONTENTS holds the contents of the item. INFO is a plist holding
1690 contextual information."
1691 (let* ((counter
1692 (let ((count (org-element-property :counter item))
1693 (level
1694 ;; Determine level of current item to determine the
1695 ;; correct LaTeX counter to use (enumi, enumii...).
1696 (let ((parent item) (level 0))
1697 (while (memq (org-element-type
1698 (setq parent (org-export-get-parent parent)))
1699 '(plain-list item))
1700 (when (and (eq (org-element-type parent) 'plain-list)
1701 (eq (org-element-property :type parent)
1702 'ordered))
1703 (incf level)))
1704 level)))
1705 (and count
1706 (< level 5)
1707 (format "\\setcounter{enum%s}{%s}\n"
1708 (nth (1- level) '("i" "ii" "iii" "iv"))
1709 (1- count)))))
1710 (checkbox (case (org-element-property :checkbox item)
1711 (on "$\\boxtimes$ ")
1712 (off "$\\square$ ")
1713 (trans "$\\boxminus$ ")))
1714 (tag (let ((tag (org-element-property :tag item)))
1715 ;; Check-boxes must belong to the tag.
1716 (and tag (format "[{%s}] "
1717 (concat checkbox
1718 (org-export-data tag info)))))))
1719 (concat counter
1720 "\\item"
1721 (cond
1722 (tag)
1723 (checkbox (concat " " checkbox))
1724 ;; Without a tag or a check-box, if CONTENTS starts with
1725 ;; an opening square bracket, add "\relax" to "\item",
1726 ;; unless the brackets comes from an initial export
1727 ;; snippet (i.e. it is inserted willingly by the user).
1728 ((and contents
1729 (org-string-match-p "\\`[ \t]*\\[" contents)
1730 (not (let ((e (car (org-element-contents item))))
1731 (and (eq (org-element-type e) 'paragraph)
1732 (let ((o (car (org-element-contents e))))
1733 (and (eq (org-element-type o) 'export-snippet)
1734 (eq (org-export-snippet-backend o)
1735 'latex)))))))
1736 "\\relax ")
1737 (t " "))
1738 (and contents (org-trim contents))
1739 ;; If there are footnotes references in tag, be sure to
1740 ;; add their definition at the end of the item. This
1741 ;; workaround is necessary since "\footnote{}" command is
1742 ;; not supported in tags.
1743 (and tag
1744 (org-latex--delayed-footnotes-definitions
1745 (org-element-property :tag item) info)))))
1748 ;;;; Keyword
1750 (defun org-latex-keyword (keyword contents info)
1751 "Transcode a KEYWORD element from Org to LaTeX.
1752 CONTENTS is nil. INFO is a plist holding contextual information."
1753 (let ((key (org-element-property :key keyword))
1754 (value (org-element-property :value keyword)))
1755 (cond
1756 ((string= key "LATEX") value)
1757 ((string= key "INDEX") (format "\\index{%s}" value))
1758 ((string= key "TOC")
1759 (let ((value (downcase value)))
1760 (cond
1761 ((string-match "\\<headlines\\>" value)
1762 (let ((depth (or (and (string-match "[0-9]+" value)
1763 (string-to-number (match-string 0 value)))
1764 (plist-get info :with-toc))))
1765 (concat
1766 (when (wholenump depth)
1767 (format "\\setcounter{tocdepth}{%s}\n" depth))
1768 "\\tableofcontents")))
1769 ((string= "tables" value) "\\listoftables")
1770 ((string= "listings" value)
1771 (case (plist-get info :latex-listings)
1772 ((nil) "\\listoffigures")
1773 (minted "\\listoflistings")
1774 (otherwise "\\lstlistoflistings")))))))))
1777 ;;;; Latex Environment
1779 (defun org-latex-latex-environment (latex-environment contents info)
1780 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1781 CONTENTS is nil. INFO is a plist holding contextual information."
1782 (when (plist-get info :with-latex)
1783 (let ((label (org-element-property :name latex-environment))
1784 (value (org-remove-indentation
1785 (org-element-property :value latex-environment))))
1786 (if (not (org-string-nw-p label)) value
1787 ;; Environment is labeled: label must be within the environment
1788 ;; (otherwise, a reference pointing to that element will count
1789 ;; the section instead).
1790 (with-temp-buffer
1791 (insert value)
1792 (goto-char (point-min))
1793 (forward-line)
1794 (insert
1795 (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1796 (buffer-string))))))
1799 ;;;; Latex Fragment
1801 (defun org-latex-latex-fragment (latex-fragment contents info)
1802 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1803 CONTENTS is nil. INFO is a plist holding contextual information."
1804 (let ((value (org-element-property :value latex-fragment)))
1805 ;; Trim math markers since the fragment is enclosed within
1806 ;; a latex-math-block object anyway.
1807 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
1808 (match-string 2 value))
1809 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
1810 (match-string 1 value))
1811 (t value))))
1814 ;;;; Line Break
1816 (defun org-latex-line-break (line-break contents info)
1817 "Transcode a LINE-BREAK object from Org to LaTeX.
1818 CONTENTS is nil. INFO is a plist holding contextual information."
1819 "\\\\\n")
1822 ;;;; Link
1824 (defun org-latex--inline-image (link info)
1825 "Return LaTeX code for an inline image.
1826 LINK is the link pointing to the inline image. INFO is a plist
1827 used as a communication channel."
1828 (let* ((parent (org-export-get-parent-element link))
1829 (path (let ((raw-path (org-element-property :path link)))
1830 (if (not (file-name-absolute-p raw-path)) raw-path
1831 (expand-file-name raw-path))))
1832 (filetype (file-name-extension path))
1833 (caption (org-latex--caption/label-string parent info))
1834 (caption-above-p (org-latex--caption-above-p link info))
1835 ;; Retrieve latex attributes from the element around.
1836 (attr (org-export-read-attribute :attr_latex parent))
1837 (float (let ((float (plist-get attr :float)))
1838 (cond ((and (not float) (plist-member attr :float)) nil)
1839 ((string= float "wrap") 'wrap)
1840 ((string= float "sideways") 'sideways)
1841 ((string= float "multicolumn") 'multicolumn)
1842 ((or float
1843 (org-element-property :caption parent)
1844 (org-string-nw-p (plist-get attr :caption)))
1845 'figure))))
1846 (placement
1847 (let ((place (plist-get attr :placement)))
1848 (cond
1849 (place (format "%s" place))
1850 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1851 ((eq float 'figure)
1852 (format "[%s]" (plist-get info :latex-default-figure-position)))
1853 (t ""))))
1854 (comment-include (if (plist-get attr :comment-include) "%" ""))
1855 ;; It is possible to specify width and height in the
1856 ;; ATTR_LATEX line, and also via default variables.
1857 (width (cond ((plist-get attr :width))
1858 ((plist-get attr :height) "")
1859 ((eq float 'wrap) "0.48\\textwidth")
1860 (t (plist-get info :latex-image-default-width))))
1861 (height (cond ((plist-get attr :height))
1862 ((or (plist-get attr :width)
1863 (memq float '(figure wrap))) "")
1864 (t (plist-get info :latex-image-default-height))))
1865 (options (let ((opt (or (plist-get attr :options)
1866 (plist-get info :latex-image-default-option))))
1867 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
1868 (match-string 1 opt))))
1869 image-code)
1870 (if (member filetype '("tikz" "pgf"))
1871 ;; For tikz images:
1872 ;; - use \input to read in image file.
1873 ;; - if options are present, wrap in a tikzpicture environment.
1874 ;; - if width or height are present, use \resizebox to change
1875 ;; the image size.
1876 (progn
1877 (setq image-code (format "\\input{%s}" path))
1878 (when (org-string-nw-p options)
1879 (setq image-code
1880 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1881 options
1882 image-code)))
1883 (when (or (org-string-nw-p width) (org-string-nw-p height))
1884 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
1885 (if (org-string-nw-p width) width "!")
1886 (if (org-string-nw-p height) height "!")
1887 image-code))))
1888 ;; For other images:
1889 ;; - add width and height to options.
1890 ;; - include the image with \includegraphics.
1891 (when (org-string-nw-p width)
1892 (setq options (concat options ",width=" width)))
1893 (when (org-string-nw-p height)
1894 (setq options (concat options ",height=" height)))
1895 (let ((search-option (org-element-property :search-option link)))
1896 (when (and search-option
1897 (equal filetype "pdf")
1898 (org-string-match-p "\\`[0-9]+\\'" search-option)
1899 (not (org-string-match-p "page=" options)))
1900 (setq options (concat options ",page=" search-option))))
1901 (setq image-code
1902 (format "\\includegraphics%s{%s}"
1903 (cond ((not (org-string-nw-p options)) "")
1904 ((= (aref options 0) ?,)
1905 (format "[%s]"(substring options 1)))
1906 (t (format "[%s]" options)))
1907 path))
1908 (when (equal filetype "svg")
1909 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
1910 "\\includesvg"
1911 image-code
1912 nil t))
1913 (setq image-code (replace-regexp-in-string "\\.svg}"
1915 image-code
1916 nil t))))
1917 ;; Return proper string, depending on FLOAT.
1918 (case float
1919 (wrap (format "\\begin{wrapfigure}%s
1920 %s\\centering
1921 %s%s
1922 %s\\end{wrapfigure}"
1923 placement
1924 (if caption-above-p caption "")
1925 comment-include image-code
1926 (if caption-above-p "" caption)))
1927 (sideways (format "\\begin{sidewaysfigure}
1928 %s\\centering
1929 %s%s
1930 %s\\end{sidewaysfigure}"
1931 (if caption-above-p caption "")
1932 comment-include image-code
1933 (if caption-above-p "" caption)))
1934 (multicolumn (format "\\begin{figure*}%s
1935 %s\\centering
1936 %s%s
1937 %s\\end{figure*}"
1938 placement
1939 (if caption-above-p caption "")
1940 comment-include image-code
1941 (if caption-above-p "" caption)))
1942 (figure (format "\\begin{figure}%s
1943 %s\\centering
1944 %s%s
1945 %s\\end{figure}"
1946 placement
1947 (if caption-above-p caption "")
1948 comment-include image-code
1949 (if caption-above-p "" caption)))
1950 (otherwise image-code))))
1952 (defun org-latex-link (link desc info)
1953 "Transcode a LINK object from Org to LaTeX.
1955 DESC is the description part of the link, or the empty string.
1956 INFO is a plist holding contextual information. See
1957 `org-export-data'."
1958 (let* ((type (org-element-property :type link))
1959 (raw-path (replace-regexp-in-string
1960 "%" "\\%" (org-element-property :path link) nil t))
1961 ;; Ensure DESC really exists, or set it to nil.
1962 (desc (and (not (string= desc "")) desc))
1963 (imagep (org-export-inline-image-p
1964 link (plist-get info :latex-inline-image-rules)))
1965 (path (cond
1966 ((member type '("http" "https" "ftp" "mailto"))
1967 (concat type ":" raw-path))
1968 ((and (string= type "file") (file-name-absolute-p raw-path))
1969 (concat "file:" raw-path))
1970 (t raw-path)))
1971 protocol)
1972 (cond
1973 ;; Image file.
1974 (imagep (org-latex--inline-image link info))
1975 ;; Radio link: Transcode target's contents and use them as link's
1976 ;; description.
1977 ((string= type "radio")
1978 (let ((destination (org-export-resolve-radio-link link info)))
1979 (if (not destination) desc
1980 (format "\\hyperref[%s]{%s}"
1981 (org-export-solidify-link-text
1982 (org-element-property :value destination))
1983 desc))))
1984 ;; Links pointing to a headline: Find destination and build
1985 ;; appropriate referencing command.
1986 ((member type '("custom-id" "fuzzy" "id"))
1987 (let ((destination (if (string= type "fuzzy")
1988 (org-export-resolve-fuzzy-link link info)
1989 (org-export-resolve-id-link link info))))
1990 (case (org-element-type destination)
1991 ;; Id link points to an external file.
1992 (plain-text
1993 (if desc (format "\\href{%s}{%s}" destination desc)
1994 (format "\\url{%s}" destination)))
1995 ;; Fuzzy link points nowhere.
1996 ('nil
1997 (format (plist-get info :latex-link-with-unknown-path-format)
1998 (or desc
1999 (org-export-data
2000 (org-element-property :raw-link link) info))))
2001 ;; LINK points to a headline. If headlines are numbered
2002 ;; and the link has no description, display headline's
2003 ;; number. Otherwise, display description or headline's
2004 ;; title.
2005 (headline
2006 (let* ((custom-label
2007 (and (plist-get info :latex-custom-id-labels)
2008 (org-element-property :CUSTOM_ID destination)))
2009 (label (or custom-label
2010 (org-export-get-headline-id destination info))))
2011 (if (and (not desc)
2012 (org-export-numbered-headline-p destination info))
2013 (format "\\ref{%s}" label)
2014 (format "\\hyperref[%s]{%s}" label
2015 (or desc
2016 (org-export-data
2017 (org-element-property :title destination) info))))))
2018 ;; Fuzzy link points to a target. Do as above.
2019 (otherwise
2020 (let ((path (org-export-solidify-link-text path)))
2021 (if (not desc) (format "\\ref{%s}" path)
2022 (format "\\hyperref[%s]{%s}" path desc)))))))
2023 ;; Coderef: replace link with the reference name or the
2024 ;; equivalent line number.
2025 ((string= type "coderef")
2026 (format (org-export-get-coderef-format path desc)
2027 (org-export-resolve-coderef path info)))
2028 ;; Link type is handled by a special function.
2029 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
2030 (funcall protocol (org-link-unescape path) desc 'latex))
2031 ;; External link with a description part.
2032 ((and path desc) (format "\\href{%s}{%s}" path desc))
2033 ;; External link without a description part.
2034 (path (format "\\url{%s}" path))
2035 ;; No path, only description. Try to do something useful.
2036 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
2039 ;;;; Node Property
2041 (defun org-latex-node-property (node-property contents info)
2042 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2043 CONTENTS is nil. INFO is a plist holding contextual
2044 information."
2045 (format "%s:%s"
2046 (org-element-property :key node-property)
2047 (let ((value (org-element-property :value node-property)))
2048 (if value (concat " " value) ""))))
2051 ;;;; Paragraph
2053 (defun org-latex-paragraph (paragraph contents info)
2054 "Transcode a PARAGRAPH element from Org to LaTeX.
2055 CONTENTS is the contents of the paragraph, as a string. INFO is
2056 the plist used as a communication channel."
2057 contents)
2060 ;;;; Plain List
2062 (defun org-latex-plain-list (plain-list contents info)
2063 "Transcode a PLAIN-LIST element from Org to LaTeX.
2064 CONTENTS is the contents of the list. INFO is a plist holding
2065 contextual information."
2066 (let* ((type (org-element-property :type plain-list))
2067 (attr (org-export-read-attribute :attr_latex plain-list))
2068 (latex-type (let ((env (plist-get attr :environment)))
2069 (cond (env (format "%s" env))
2070 ((eq type 'ordered) "enumerate")
2071 ((eq type 'descriptive) "description")
2072 (t "itemize")))))
2073 (org-latex--wrap-label
2074 plain-list
2075 (format "\\begin{%s}%s\n%s\\end{%s}"
2076 latex-type
2077 (or (plist-get attr :options) "")
2078 contents
2079 latex-type))))
2082 ;;;; Plain Text
2084 (defun org-latex-plain-text (text info)
2085 "Transcode a TEXT string from Org to LaTeX.
2086 TEXT is the string to transcode. INFO is a plist holding
2087 contextual information."
2088 (let* ((specialp (plist-get info :with-special-strings))
2089 (output
2090 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2091 (let ((case-fold-search nil))
2092 (replace-regexp-in-string
2093 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2094 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2095 ;; However, if special strings are used, be careful not
2096 ;; to protect "\" in "\-" constructs.
2097 (replace-regexp-in-string
2098 (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\(?:[^-]\\|$\\)"))
2099 (lambda (m)
2100 (case (aref m 0)
2101 (?\\ "$\\\\backslash$")
2102 (?~ "\\\\textasciitilde{}")
2103 (?^ "\\\\^{}")
2104 (t "\\\\\\&")))
2105 text)))))
2106 ;; Activate smart quotes. Be sure to provide original TEXT string
2107 ;; since OUTPUT may have been modified.
2108 (when (plist-get info :with-smart-quotes)
2109 (setq output (org-export-activate-smart-quotes output :latex info text)))
2110 ;; Convert special strings.
2111 (when specialp
2112 (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
2113 ;; Handle break preservation if required.
2114 (when (plist-get info :preserve-breaks)
2115 (setq output (replace-regexp-in-string
2116 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
2117 ;; Return value.
2118 output))
2121 ;;;; Planning
2123 (defun org-latex-planning (planning contents info)
2124 "Transcode a PLANNING element from Org to LaTeX.
2125 CONTENTS is nil. INFO is a plist holding contextual
2126 information."
2127 (concat
2128 "\\noindent"
2129 (mapconcat
2130 'identity
2131 (delq nil
2132 (list
2133 (let ((closed (org-element-property :closed planning)))
2134 (when closed
2135 (concat
2136 (format "\\textbf{%s} " org-closed-string)
2137 (format (plist-get info :latex-inactive-timestamp-format)
2138 (org-translate-time
2139 (org-element-property :raw-value closed))))))
2140 (let ((deadline (org-element-property :deadline planning)))
2141 (when deadline
2142 (concat
2143 (format "\\textbf{%s} " org-deadline-string)
2144 (format (plist-get info :latex-active-timestamp-format)
2145 (org-translate-time
2146 (org-element-property :raw-value deadline))))))
2147 (let ((scheduled (org-element-property :scheduled planning)))
2148 (when scheduled
2149 (concat
2150 (format "\\textbf{%s} " org-scheduled-string)
2151 (format (plist-get info :latex-active-timestamp-format)
2152 (org-translate-time
2153 (org-element-property :raw-value scheduled))))))))
2154 " ")
2155 "\\\\"))
2158 ;;;; Property Drawer
2160 (defun org-latex-property-drawer (property-drawer contents info)
2161 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2162 CONTENTS holds the contents of the drawer. INFO is a plist
2163 holding contextual information."
2164 (and (org-string-nw-p contents)
2165 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2168 ;;;; Pseudo Element: LaTeX Matrices
2170 ;; `latex-matrices' elements have the following properties:
2171 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2172 ;; `math').
2174 (defun org-latex--wrap-latex-matrices (data info)
2175 "Merge contiguous tables with the same mode within a pseudo-element.
2176 DATA is a parse tree or a secondary string. INFO is a plist
2177 containing export options. Modify DATA by side-effect and return
2178 it."
2179 (org-element-map data 'table
2180 (lambda (table)
2181 (when (eq (org-element-property :type table) 'org)
2182 (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
2183 (plist-get info :latex-default-table-mode))))
2184 (when (and (member mode '("inline-math" "math"))
2185 ;; Do not wrap twice the same table.
2186 (not (eq (org-element-type
2187 (org-element-property :parent table))
2188 'latex-matrices)))
2189 (let* ((caption (and (not (string= mode "inline-math"))
2190 (org-element-property :caption table)))
2191 (matrices
2192 (list 'latex-matrices
2193 (list :caption caption
2194 :markup
2195 (cond ((string= mode "inline-math") 'inline)
2196 (caption 'equation)
2197 (t 'math)))))
2198 (previous table)
2199 (next (org-export-get-next-element table info)))
2200 (org-element-insert-before matrices table)
2201 ;; Swallow all contiguous tables sharing the same mode.
2202 (while (and
2203 (zerop (or (org-element-property :post-blank previous) 0))
2204 (setq next (org-export-get-next-element previous info))
2205 (eq (org-element-type next) 'table)
2206 (eq (org-element-property :type next) 'org)
2207 (string= (or (org-export-read-attribute
2208 :attr_latex next :mode)
2209 (plist-get info :latex-default-table-mode))
2210 mode))
2211 (org-element-extract-element previous)
2212 (org-element-adopt-elements matrices previous)
2213 (setq previous next))
2214 (org-element-put-property
2215 matrices :post-blank (org-element-property :post-blank previous))
2216 (org-element-extract-element previous)
2217 (org-element-adopt-elements matrices previous))))))
2218 info)
2219 data)
2221 (defun org-latex-matrices (matrices contents info)
2222 "Transcode a MATRICES element from Org to LaTeX.
2223 CONTENTS is a string. INFO is a plist used as a communication
2224 channel."
2225 (format (case (org-element-property :markup matrices)
2226 (inline "\\(%s\\)")
2227 (equation "\\begin{equation}\n%s\\end{equation}")
2228 (t "\\[\n%s\\]"))
2229 contents))
2231 (defun org-latex-matrices-tree-filter (tree backend info)
2232 (org-latex--wrap-latex-matrices tree info))
2234 ;;;; Pseudo Object: LaTeX Math Block
2236 ;; `latex-math-block' objects have the following property:
2237 ;; `:post-blank'.
2239 (defun org-latex--wrap-latex-math-block (data info)
2240 "Merge contiguous math objects in a pseudo-object container.
2241 DATA is a parse tree or a secondary string. INFO is a plist
2242 containing export options. Modify DATA by side-effect and return it."
2243 (let ((valid-object-p
2244 (function
2245 ;; Non-nil when OBJ can be added to the latex math block.
2246 (lambda (obj)
2247 (case (org-element-type obj)
2248 (entity (org-element-property :latex-math-p obj))
2249 (latex-fragment
2250 (let ((value (org-element-property :value obj)))
2251 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2252 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2253 ((subscript superscript) t))))))
2254 (org-element-map data '(entity latex-fragment subscript superscript)
2255 (lambda (object)
2256 ;; Skip objects already wrapped.
2257 (when (and (not (eq (org-element-type
2258 (org-element-property :parent object))
2259 'latex-math-block))
2260 (funcall valid-object-p object))
2261 (let ((math-block (list 'latex-math-block nil))
2262 (next-elements (org-export-get-next-element object info t))
2263 (last object))
2264 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2265 (org-element-insert-before math-block object)
2266 (org-element-extract-element object)
2267 (org-element-adopt-elements math-block object)
2268 (when (zerop (or (org-element-property :post-blank object) 0))
2269 ;; MATH-BLOCK swallows consecutive math objects.
2270 (catch 'exit
2271 (dolist (next next-elements)
2272 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2273 (org-element-extract-element next)
2274 (org-element-adopt-elements math-block next)
2275 ;; Eschew the case: \beta$x$ -> \(\betax\).
2276 (unless (memq (org-element-type next)
2277 '(subscript superscript))
2278 (org-element-put-property last :post-blank 1))
2279 (setq last next)
2280 (when (> (or (org-element-property :post-blank next) 0) 0)
2281 (throw 'exit nil))))))
2282 (org-element-put-property
2283 math-block :post-blank (org-element-property :post-blank last)))))
2284 info nil '(subscript superscript latex-math-block) t)
2285 ;; Return updated DATA.
2286 data))
2288 (defun org-latex-math-block-tree-filter (tree backend info)
2289 (org-latex--wrap-latex-math-block tree info))
2291 (defun org-latex-math-block-options-filter (info backend)
2292 (dolist (prop '(:author :date :title) info)
2293 (plist-put info prop
2294 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2296 (defun org-latex-math-block (math-block contents info)
2297 "Transcode a MATH-BLOCK object from Org to LaTeX.
2298 CONTENTS is a string. INFO is a plist used as a communication
2299 channel."
2300 (when (org-string-nw-p contents)
2301 (format "\\(%s\\)" (org-trim contents))))
2303 ;;;; Quote Block
2305 (defun org-latex-quote-block (quote-block contents info)
2306 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2307 CONTENTS holds the contents of the block. INFO is a plist
2308 holding contextual information."
2309 (org-latex--wrap-label
2310 quote-block
2311 (format "\\begin{quote}\n%s\\end{quote}" contents)))
2314 ;;;; Radio Target
2316 (defun org-latex-radio-target (radio-target text info)
2317 "Transcode a RADIO-TARGET object from Org to LaTeX.
2318 TEXT is the text of the target. INFO is a plist holding
2319 contextual information."
2320 (format "\\label{%s}%s"
2321 (org-export-solidify-link-text
2322 (org-element-property :value radio-target))
2323 text))
2326 ;;;; Section
2328 (defun org-latex-section (section contents info)
2329 "Transcode a SECTION element from Org to LaTeX.
2330 CONTENTS holds the contents of the section. INFO is a plist
2331 holding contextual information."
2332 contents)
2335 ;;;; Special Block
2337 (defun org-latex-special-block (special-block contents info)
2338 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2339 CONTENTS holds the contents of the block. INFO is a plist
2340 holding contextual information."
2341 (let ((type (org-element-property :type special-block))
2342 (opt (org-export-read-attribute :attr_latex special-block :options))
2343 (caption (org-latex--caption/label-string special-block info))
2344 (caption-above-p (org-latex--caption-above-p special-block info)))
2345 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2346 (and caption-above-p caption)
2347 contents
2348 (and (not caption-above-p) caption)
2349 (format "\\end{%s}" type))))
2352 ;;;; Src Block
2354 (defun org-latex-src-block (src-block contents info)
2355 "Transcode a SRC-BLOCK element from Org to LaTeX.
2356 CONTENTS holds the contents of the item. INFO is a plist holding
2357 contextual information."
2358 (when (org-string-nw-p (org-element-property :value src-block))
2359 (let* ((lang (org-element-property :language src-block))
2360 (caption (org-element-property :caption src-block))
2361 (caption-above-p (org-latex--caption-above-p src-block info))
2362 (label (org-element-property :name src-block))
2363 (custom-env (and lang
2364 (cadr (assq (intern lang)
2365 org-latex-custom-lang-environments))))
2366 (num-start (case (org-element-property :number-lines src-block)
2367 (continued (org-export-get-loc src-block info))
2368 (new 0)))
2369 (retain-labels (org-element-property :retain-labels src-block))
2370 (attributes (org-export-read-attribute :attr_latex src-block))
2371 (float (plist-get attributes :float))
2372 (listings (plist-get info :latex-listings)))
2373 (cond
2374 ;; Case 1. No source fontification.
2375 ((not listings)
2376 (let* ((caption-str (org-latex--caption/label-string src-block info))
2377 (float-env
2378 (cond ((and (not float) (plist-member attributes :float)) "%s")
2379 ((string= "multicolumn" float)
2380 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2381 (plist-get info :latex-default-figure-position)
2382 (if caption-above-p caption-str "")
2383 (if caption-above-p "" caption-str)))
2384 ((or caption float)
2385 (format "\\begin{figure}[H]\n%%s\n%s\\end{figure}"
2386 caption-str))
2387 (t "%s"))))
2388 (format
2389 float-env
2390 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2391 (org-export-format-code-default src-block info))))))
2392 ;; Case 2. Custom environment.
2393 (custom-env
2394 (let ((caption-str (org-latex--caption/label-string src-block info)))
2395 (format "\\begin{%s}\n%s\\end{%s}\n"
2396 custom-env
2397 (concat (and caption-above-p caption-str)
2398 (org-export-format-code-default src-block info)
2399 (and (not caption-above-p) caption-str))
2400 custom-env)))
2401 ;; Case 3. Use minted package.
2402 ((eq listings 'minted)
2403 (let* ((caption-str (org-latex--caption/label-string src-block info))
2404 (float-env
2405 (cond
2406 ((and (not float) (plist-member attributes :float) caption)
2407 (let ((caption
2408 (replace-regexp-in-string
2409 "\\\\caption" "\\captionof{listing}" caption-str
2410 t t)))
2411 (concat (and caption-above-p caption)
2412 "%%s"
2413 (and (not caption-above-p) (concat "\n" caption)))))
2414 ((and (not float) (plist-member attributes :float)) "%s")
2415 ((string= "multicolumn" float)
2416 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2417 (if caption-above-p caption-str "")
2418 (if caption-above-p "" caption-str)))
2419 ((or caption float)
2420 (format "\\begin{listing}[H]\n%s%%s\n%s\\end{listing}"
2421 (if caption-above-p caption-str "")
2422 (if caption-above-p "" caption-str)))
2423 (t "%s")))
2424 (options (plist-get info :latex-minted-options))
2425 (body
2426 (format
2427 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2428 ;; Options.
2429 (concat
2430 (org-latex--make-option-string
2431 (if (or (not num-start) (assoc "linenos" options))
2432 options
2433 (append
2434 `(("linenos")
2435 ("firstnumber" ,(number-to-string (1+ num-start))))
2436 options)))
2437 (let ((local-options (plist-get attributes :options)))
2438 (and local-options (concat "," local-options))))
2439 ;; Language.
2440 (or (cadr (assq (intern lang)
2441 (plist-get info :latex-minted-langs)))
2442 (downcase lang))
2443 ;; Source code.
2444 (let* ((code-info (org-export-unravel-code src-block))
2445 (max-width
2446 (apply 'max
2447 (mapcar 'length
2448 (org-split-string (car code-info)
2449 "\n")))))
2450 (org-export-format-code
2451 (car code-info)
2452 (lambda (loc num ref)
2453 (concat
2455 (when ref
2456 ;; Ensure references are flushed to the right,
2457 ;; separated with 6 spaces from the widest line
2458 ;; of code.
2459 (concat (make-string (+ (- max-width (length loc)) 6)
2460 ?\s)
2461 (format "(%s)" ref)))))
2462 nil (and retain-labels (cdr code-info)))))))
2463 ;; Return value.
2464 (format float-env body)))
2465 ;; Case 4. Use listings package.
2467 (let ((lst-lang
2468 (or (cadr (assq (intern lang)
2469 (plist-get info :latex-listings-langs)))
2470 lang))
2471 (caption-str
2472 (when caption
2473 (let ((main (org-export-get-caption src-block))
2474 (secondary (org-export-get-caption src-block t)))
2475 (if (not secondary)
2476 (format "{%s}" (org-export-data main info))
2477 (format "{[%s]%s}"
2478 (org-export-data secondary info)
2479 (org-export-data main info))))))
2480 (lst-opt (plist-get info :latex-listings-options)))
2481 (concat
2482 ;; Options.
2483 (format
2484 "\\lstset{%s}\n"
2485 (concat
2486 (org-latex--make-option-string
2487 (append
2488 lst-opt
2489 (cond
2490 ((and (not float) (plist-member attributes :float)) nil)
2491 ((string= "multicolumn" float) '(("float" "*")))
2492 ((and float (not (assoc "float" lst-opt)))
2493 `(("float" ,(plist-get info :latex-default-figure-position)))))
2494 `(("language" ,lst-lang))
2495 (if label `(("label" ,label)) '(("label" " ")))
2496 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2497 `(("captionpos" ,(if caption-above-p "t" "b")))
2498 (cond ((assoc "numbers" lst-opt) nil)
2499 ((not num-start) '(("numbers" "none")))
2500 ((zerop num-start) '(("numbers" "left")))
2501 (t `(("firstnumber" ,(number-to-string (1+ num-start)))
2502 ("numbers" "left"))))))
2503 (let ((local-options (plist-get attributes :options)))
2504 (and local-options (concat "," local-options)))))
2505 ;; Source code.
2506 (format
2507 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2508 (let* ((code-info (org-export-unravel-code src-block))
2509 (max-width
2510 (apply 'max
2511 (mapcar 'length
2512 (org-split-string (car code-info) "\n")))))
2513 (org-export-format-code
2514 (car code-info)
2515 (lambda (loc num ref)
2516 (concat
2518 (when ref
2519 ;; Ensure references are flushed to the right,
2520 ;; separated with 6 spaces from the widest line of
2521 ;; code
2522 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2523 (format "(%s)" ref)))))
2524 nil (and retain-labels (cdr code-info))))))))))))
2527 ;;;; Statistics Cookie
2529 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2530 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2531 CONTENTS is nil. INFO is a plist holding contextual information."
2532 (replace-regexp-in-string
2533 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2536 ;;;; Strike-Through
2538 (defun org-latex-strike-through (strike-through contents info)
2539 "Transcode STRIKE-THROUGH from Org to LaTeX.
2540 CONTENTS is the text with strike-through markup. INFO is a plist
2541 holding contextual information."
2542 (org-latex--text-markup contents 'strike-through info))
2545 ;;;; Subscript
2547 (defun org-latex--script-size (object info)
2548 "Transcode a subscript or superscript object.
2549 OBJECT is an Org object. INFO is a plist used as a communication
2550 channel."
2551 (let ((type (org-element-type object))
2552 (output ""))
2553 (org-element-map (org-element-contents object)
2554 (cons 'plain-text org-element-all-objects)
2555 (lambda (obj)
2556 (case (org-element-type obj)
2557 ((entity latex-fragment)
2558 (let ((data (org-trim (org-export-data obj info))))
2559 (string-match
2560 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2561 data)
2562 (setq output
2563 (concat output
2564 (match-string 1 data)
2565 (let ((blank (org-element-property :post-blank obj)))
2566 (and blank (> blank 0) "\\ "))))))
2567 (plain-text
2568 (setq output
2569 (format "%s\\text{%s}" output (org-export-data obj info))))
2570 (otherwise
2571 (setq output
2572 (concat output
2573 (org-export-data obj info)
2574 (let ((blank (org-element-property :post-blank obj)))
2575 (and blank (> blank 0) "\\ ")))))))
2576 info nil org-element-recursive-objects)
2577 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2578 ;; character.
2579 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2580 (and (> (length output) 1) "{")
2581 output
2582 (and (> (length output) 1) "}"))))
2584 (defun org-latex-subscript (subscript contents info)
2585 "Transcode a SUBSCRIPT object from Org to LaTeX.
2586 CONTENTS is the contents of the object. INFO is a plist holding
2587 contextual information."
2588 (org-latex--script-size subscript info))
2591 ;;;; Superscript
2593 (defun org-latex-superscript (superscript contents info)
2594 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2595 CONTENTS is the contents of the object. INFO is a plist holding
2596 contextual information."
2597 (org-latex--script-size superscript info))
2600 ;;;; Table
2602 ;; `org-latex-table' is the entry point for table transcoding. It
2603 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2604 ;; delegates the job to either `org-latex--table.el-table',
2605 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2606 ;; depending of the type of the table and the mode requested.
2608 ;; `org-latex--align-string' is a subroutine used to build alignment
2609 ;; string for Org tables.
2611 (defun org-latex-table (table contents info)
2612 "Transcode a TABLE element from Org to LaTeX.
2613 CONTENTS is the contents of the table. INFO is a plist holding
2614 contextual information."
2615 (if (eq (org-element-property :type table) 'table.el)
2616 ;; "table.el" table. Convert it using appropriate tools.
2617 (org-latex--table.el-table table info)
2618 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2619 (plist-get info :latex-default-table-mode))))
2620 (cond
2621 ;; Case 1: Verbatim table.
2622 ((string= type "verbatim")
2623 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2624 ;; Re-create table, without affiliated keywords.
2625 (org-trim (org-element-interpret-data
2626 `(table nil ,@(org-element-contents table))
2627 org-latex-pseudo-objects))))
2628 ;; Case 2: Matrix.
2629 ((or (string= type "math") (string= type "inline-math"))
2630 (org-latex--math-table table info))
2631 ;; Case 3: Standard table.
2632 (t (concat (org-latex--org-table table contents info)
2633 ;; When there are footnote references within the
2634 ;; table, insert their definition just after it.
2635 (org-latex--delayed-footnotes-definitions table info)))))))
2637 (defun org-latex--align-string (table info)
2638 "Return an appropriate LaTeX alignment string.
2639 TABLE is the considered table. INFO is a plist used as
2640 a communication channel."
2641 (or (org-export-read-attribute :attr_latex table :align)
2642 (let (align)
2643 ;; Extract column groups and alignment from first (non-rule)
2644 ;; row.
2645 (org-element-map
2646 (org-element-map table 'table-row
2647 (lambda (row)
2648 (and (eq (org-element-property :type row) 'standard) row))
2649 info 'first-match)
2650 'table-cell
2651 (lambda (cell)
2652 (let ((borders (org-export-table-cell-borders cell info)))
2653 ;; Check left border for the first cell only.
2654 (when (and (memq 'left borders) (not align))
2655 (push "|" align))
2656 (push (case (org-export-table-cell-alignment cell info)
2657 (left "l")
2658 (right "r")
2659 (center "c"))
2660 align)
2661 (when (memq 'right borders) (push "|" align))))
2662 info)
2663 (apply 'concat (nreverse align)))))
2665 (defun org-latex--org-table (table contents info)
2666 "Return appropriate LaTeX code for an Org table.
2668 TABLE is the table type element to transcode. CONTENTS is its
2669 contents, as a string. INFO is a plist used as a communication
2670 channel.
2672 This function assumes TABLE has `org' as its `:type' property and
2673 `table' as its `:mode' attribute."
2674 (let* ((caption (org-latex--caption/label-string table info))
2675 (attr (org-export-read-attribute :attr_latex table))
2676 ;; Determine alignment string.
2677 (alignment (org-latex--align-string table info))
2678 ;; Determine environment for the table: longtable, tabular...
2679 (table-env (or (plist-get attr :environment)
2680 (plist-get info :latex-default-table-environment)))
2681 ;; If table is a float, determine environment: table, table*
2682 ;; or sidewaystable.
2683 (float-env (unless (member table-env '("longtable" "longtabu"))
2684 (let ((float (plist-get attr :float)))
2685 (cond
2686 ((and (not float) (plist-member attr :float)) nil)
2687 ((or (string= float "sidewaystable")
2688 (string= float "sideways")) "sidewaystable")
2689 ((string= float "multicolumn") "table*")
2690 ((or float
2691 (org-element-property :caption table)
2692 (org-string-nw-p (plist-get attr :caption)))
2693 "table")))))
2694 ;; Extract others display options.
2695 (fontsize (let ((font (plist-get attr :font)))
2696 (and font (concat font "\n"))))
2697 (width (plist-get attr :width))
2698 (spreadp (plist-get attr :spread))
2699 (placement
2700 (or (plist-get attr :placement)
2701 (format "[%s]" (plist-get info :latex-default-figure-position))))
2702 (centerp (if (plist-member attr :center) (plist-get attr :center)
2703 (plist-get info :latex-tables-centered)))
2704 (caption-above-p (org-latex--caption-above-p table info)))
2705 ;; Prepare the final format string for the table.
2706 (cond
2707 ;; Longtable.
2708 ((equal "longtable" table-env)
2709 (concat (and fontsize (concat "{" fontsize))
2710 (format "\\begin{longtable}{%s}\n" alignment)
2711 (and caption-above-p
2712 (org-string-nw-p caption)
2713 (concat caption "\\\\\n"))
2714 contents
2715 (and (not caption-above-p)
2716 (org-string-nw-p caption)
2717 (concat caption "\\\\\n"))
2718 "\\end{longtable}\n"
2719 (and fontsize "}")))
2720 ;; Longtabu
2721 ((equal "longtabu" table-env)
2722 (concat (and fontsize (concat "{" fontsize))
2723 (format "\\begin{longtabu}%s{%s}\n"
2724 (if width
2725 (format " %s %s "
2726 (if spreadp "spread" "to") width) "")
2727 alignment)
2728 (and caption-above-p
2729 (org-string-nw-p caption)
2730 (concat caption "\\\\\n"))
2731 contents
2732 (and (not caption-above-p)
2733 (org-string-nw-p caption)
2734 (concat caption "\\\\\n"))
2735 "\\end{longtabu}\n"
2736 (and fontsize "}")))
2737 ;; Others.
2738 (t (concat (cond
2739 (float-env
2740 (concat (format "\\begin{%s}%s\n" float-env placement)
2741 (if caption-above-p caption "")
2742 (when centerp "\\centering\n")
2743 fontsize))
2744 (centerp (concat "\\begin{center}\n" fontsize))
2745 (fontsize (concat "{" fontsize)))
2746 (cond ((equal "tabu" table-env)
2747 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2748 (if width (format
2749 (if spreadp " spread %s " " to %s ")
2750 width) "")
2751 alignment
2752 contents))
2753 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2754 table-env
2755 (if width (format "{%s}" width) "")
2756 alignment
2757 contents
2758 table-env)))
2759 (cond
2760 (float-env
2761 (concat (if caption-above-p "" caption)
2762 (format "\n\\end{%s}" float-env)))
2763 (centerp "\n\\end{center}")
2764 (fontsize "}")))))))
2766 (defun org-latex--table.el-table (table info)
2767 "Return appropriate LaTeX code for a table.el table.
2769 TABLE is the table type element to transcode. INFO is a plist
2770 used as a communication channel.
2772 This function assumes TABLE has `table.el' as its `:type'
2773 property."
2774 (require 'table)
2775 ;; Ensure "*org-export-table*" buffer is empty.
2776 (with-current-buffer (get-buffer-create "*org-export-table*")
2777 (erase-buffer))
2778 (let ((output (with-temp-buffer
2779 (insert (org-element-property :value table))
2780 (goto-char 1)
2781 (re-search-forward "^[ \t]*|[^|]" nil t)
2782 (table-generate-source 'latex "*org-export-table*")
2783 (with-current-buffer "*org-export-table*"
2784 (org-trim (buffer-string))))))
2785 (kill-buffer (get-buffer "*org-export-table*"))
2786 ;; Remove left out comments.
2787 (while (string-match "^%.*\n" output)
2788 (setq output (replace-match "" t t output)))
2789 (let ((attr (org-export-read-attribute :attr_latex table)))
2790 (when (plist-get attr :rmlines)
2791 ;; When the "rmlines" attribute is provided, remove all hlines
2792 ;; but the the one separating heading from the table body.
2793 (let ((n 0) (pos 0))
2794 (while (and (< (length output) pos)
2795 (setq pos (string-match "^\\\\hline\n?" output pos)))
2796 (incf n)
2797 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2798 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2799 (plist-get info :latex-tables-centered))))
2800 (if (not centerp) output
2801 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2803 (defun org-latex--math-table (table info)
2804 "Return appropriate LaTeX code for a matrix.
2806 TABLE is the table type element to transcode. INFO is a plist
2807 used as a communication channel.
2809 This function assumes TABLE has `org' as its `:type' property and
2810 `inline-math' or `math' as its `:mode' attribute."
2811 (let* ((attr (org-export-read-attribute :attr_latex table))
2812 (env (or (plist-get attr :environment)
2813 (plist-get info :latex-default-table-environment)))
2814 (contents
2815 (mapconcat
2816 (lambda (row)
2817 ;; Ignore horizontal rules.
2818 (when (eq (org-element-property :type row) 'standard)
2819 ;; Return each cell unmodified.
2820 (concat
2821 (mapconcat
2822 (lambda (cell)
2823 (substring
2824 (org-element-interpret-data cell org-latex-pseudo-objects)
2825 0 -1))
2826 (org-element-map row 'table-cell #'identity info) "&")
2827 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2828 "\n")))
2829 (org-element-map table 'table-row #'identity info) "")))
2830 (concat
2831 ;; Prefix.
2832 (plist-get attr :math-prefix)
2833 ;; Environment. Also treat special cases.
2834 (cond ((member env '("array" "tabular"))
2835 (let ((align (make-string
2836 (cdr (org-export-table-dimensions table info)) ?c)))
2837 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
2838 ((assoc env org-latex-table-matrix-macros)
2839 (format "\\%s%s{\n%s}"
2841 (or (plist-get attr :math-arguments) "")
2842 contents))
2843 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2844 ;; Suffix.
2845 (plist-get attr :math-suffix))))
2848 ;;;; Table Cell
2850 (defun org-latex-table-cell (table-cell contents info)
2851 "Transcode a TABLE-CELL element from Org to LaTeX.
2852 CONTENTS is the cell contents. INFO is a plist used as
2853 a communication channel."
2854 (concat
2855 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
2856 (if (and contents
2857 scientific-format
2858 (string-match orgtbl-exp-regexp contents))
2859 ;; Use appropriate format string for scientific
2860 ;; notation.
2861 (format scientific-format
2862 (match-string 1 contents)
2863 (match-string 2 contents))
2864 contents))
2865 (when (org-export-get-next-element table-cell info) " & ")))
2868 ;;;; Table Row
2870 (defun org-latex-table-row (table-row contents info)
2871 "Transcode a TABLE-ROW element from Org to LaTeX.
2872 CONTENTS is the contents of the row. INFO is a plist used as
2873 a communication channel."
2874 (let* ((attr (org-export-read-attribute :attr_latex
2875 (org-export-get-parent table-row)))
2876 (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
2877 (plist-get info :latex-tables-booktabs)))
2878 (longtablep
2879 (member (or (plist-get attr :environment)
2880 (plist-get info :latex-default-table-environment))
2881 '("longtable" "longtabu"))))
2882 (if (eq (org-element-property :type table-row) 'rule)
2883 (cond
2884 ((not booktabsp) "\\hline")
2885 ((not (org-export-get-previous-element table-row info)) "\\toprule")
2886 ((not (org-export-get-next-element table-row info)) "\\bottomrule")
2887 ((and longtablep
2888 (org-export-table-row-ends-header-p
2889 (org-export-get-previous-element table-row info) info))
2891 (t "\\midrule"))
2892 (concat
2893 ;; When BOOKTABS are activated enforce top-rule even when no
2894 ;; hline was specifically marked.
2895 (and booktabsp (not (org-export-get-previous-element table-row info))
2896 "\\toprule\n")
2897 contents "\\\\\n"
2898 (cond
2899 ;; Special case for long tables. Define header and footers.
2900 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2901 (let ((columns (cdr (org-export-table-dimensions
2902 (org-export-get-parent-table table-row) info))))
2903 (format "%s
2904 \\endfirsthead
2905 \\multicolumn{%d}{l}{%s} \\\\
2907 %s \\\\\n
2909 \\endhead
2910 %s\\multicolumn{%d}{r}{%s} \\\\
2911 \\endfoot
2912 \\endlastfoot"
2913 (if booktabsp "\\midrule" "\\hline")
2914 columns
2915 (org-latex--translate "Continued from previous page" info)
2916 (cond
2917 ((not (org-export-table-row-starts-header-p table-row info))
2919 (booktabsp "\\toprule\n")
2920 (t "\\hline\n"))
2921 contents
2922 (if booktabsp "\\midrule" "\\hline")
2923 (if booktabsp "\\midrule" "\\hline")
2924 columns
2925 (org-latex--translate "Continued on next page" info))))
2926 ;; When BOOKTABS are activated enforce bottom rule even when
2927 ;; no hline was specifically marked.
2928 ((and booktabsp (not (org-export-get-next-element table-row info)))
2929 "\\bottomrule"))))))
2932 ;;;; Target
2934 (defun org-latex-target (target contents info)
2935 "Transcode a TARGET object from Org to LaTeX.
2936 CONTENTS is nil. INFO is a plist holding contextual
2937 information."
2938 (format "\\label{%s}"
2939 (org-export-solidify-link-text (org-element-property :value target))))
2942 ;;;; Timestamp
2944 (defun org-latex-timestamp (timestamp contents info)
2945 "Transcode a TIMESTAMP object from Org to LaTeX.
2946 CONTENTS is nil. INFO is a plist holding contextual
2947 information."
2948 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
2949 (format
2950 (plist-get info
2951 (case (org-element-property :type timestamp)
2952 ((active active-range) :latex-active-timestamp-format)
2953 ((inactive inactive-range) :latex-inactive-timestamp-format)
2954 (otherwise :latex-diary-timestamp-format)))
2955 value)))
2958 ;;;; Underline
2960 (defun org-latex-underline (underline contents info)
2961 "Transcode UNDERLINE from Org to LaTeX.
2962 CONTENTS is the text with underline markup. INFO is a plist
2963 holding contextual information."
2964 (org-latex--text-markup contents 'underline info))
2967 ;;;; Verbatim
2969 (defun org-latex-verbatim (verbatim contents info)
2970 "Transcode a VERBATIM object from Org to LaTeX.
2971 CONTENTS is nil. INFO is a plist used as a communication
2972 channel."
2973 (org-latex--text-markup
2974 (org-element-property :value verbatim) 'verbatim info))
2977 ;;;; Verse Block
2979 (defun org-latex-verse-block (verse-block contents info)
2980 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2981 CONTENTS is verse block contents. INFO is a plist holding
2982 contextual information."
2983 (org-latex--wrap-label
2984 verse-block
2985 ;; In a verse environment, add a line break to each newline
2986 ;; character and change each white space at beginning of a line
2987 ;; into a space of 1 em. Also change each blank line with
2988 ;; a vertical space of 1 em.
2989 (format "\\begin{verse}\n%s\\end{verse}"
2990 (replace-regexp-in-string
2991 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
2992 (replace-regexp-in-string
2993 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
2994 (replace-regexp-in-string
2995 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
2996 contents nil t) nil t) nil t))))
3000 ;;; End-user functions
3002 ;;;###autoload
3003 (defun org-latex-export-as-latex
3004 (&optional async subtreep visible-only body-only ext-plist)
3005 "Export current buffer as a LaTeX buffer.
3007 If narrowing is active in the current buffer, only export its
3008 narrowed part.
3010 If a region is active, export that region.
3012 A non-nil optional argument ASYNC means the process should happen
3013 asynchronously. The resulting buffer should be accessible
3014 through the `org-export-stack' interface.
3016 When optional argument SUBTREEP is non-nil, export the sub-tree
3017 at point, extracting information from the headline properties
3018 first.
3020 When optional argument VISIBLE-ONLY is non-nil, don't export
3021 contents of hidden elements.
3023 When optional argument BODY-ONLY is non-nil, only write code
3024 between \"\\begin{document}\" and \"\\end{document}\".
3026 EXT-PLIST, when provided, is a property list with external
3027 parameters overriding Org default settings, but still inferior to
3028 file-local settings.
3030 Export is done in a buffer named \"*Org LATEX Export*\", which
3031 will be displayed when `org-export-show-temporary-export-buffer'
3032 is non-nil."
3033 (interactive)
3034 (org-export-to-buffer 'latex "*Org LATEX Export*"
3035 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
3037 ;;;###autoload
3038 (defun org-latex-convert-region-to-latex ()
3039 "Assume the current region has org-mode syntax, and convert it to LaTeX.
3040 This can be used in any buffer. For example, you can write an
3041 itemized list in org-mode syntax in an LaTeX buffer and use this
3042 command to convert it."
3043 (interactive)
3044 (org-export-replace-region-by 'latex))
3046 ;;;###autoload
3047 (defun org-latex-export-to-latex
3048 (&optional async subtreep visible-only body-only ext-plist)
3049 "Export current buffer to a LaTeX file.
3051 If narrowing is active in the current buffer, only export its
3052 narrowed part.
3054 If a region is active, export that region.
3056 A non-nil optional argument ASYNC means the process should happen
3057 asynchronously. The resulting file should be accessible through
3058 the `org-export-stack' interface.
3060 When optional argument SUBTREEP is non-nil, export the sub-tree
3061 at point, extracting information from the headline properties
3062 first.
3064 When optional argument VISIBLE-ONLY is non-nil, don't export
3065 contents of hidden elements.
3067 When optional argument BODY-ONLY is non-nil, only write code
3068 between \"\\begin{document}\" and \"\\end{document}\".
3070 EXT-PLIST, when provided, is a property list with external
3071 parameters overriding Org default settings, but still inferior to
3072 file-local settings."
3073 (interactive)
3074 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3075 (org-export-to-file 'latex outfile
3076 async subtreep visible-only body-only ext-plist)))
3078 ;;;###autoload
3079 (defun org-latex-export-to-pdf
3080 (&optional async subtreep visible-only body-only ext-plist)
3081 "Export current buffer to LaTeX then process through to PDF.
3083 If narrowing is active in the current buffer, only export its
3084 narrowed part.
3086 If a region is active, export that region.
3088 A non-nil optional argument ASYNC means the process should happen
3089 asynchronously. The resulting file should be accessible through
3090 the `org-export-stack' interface.
3092 When optional argument SUBTREEP is non-nil, export the sub-tree
3093 at point, extracting information from the headline properties
3094 first.
3096 When optional argument VISIBLE-ONLY is non-nil, don't export
3097 contents of hidden elements.
3099 When optional argument BODY-ONLY is non-nil, only write code
3100 between \"\\begin{document}\" and \"\\end{document}\".
3102 EXT-PLIST, when provided, is a property list with external
3103 parameters overriding Org default settings, but still inferior to
3104 file-local settings.
3106 Return PDF file's name."
3107 (interactive)
3108 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3109 (org-export-to-file 'latex outfile
3110 async subtreep visible-only body-only ext-plist
3111 (lambda (file) (org-latex-compile file)))))
3113 (defun org-latex-compile (texfile &optional snippet)
3114 "Compile a TeX file.
3116 TEXFILE is the name of the file being compiled. Processing is
3117 done through the command specified in `org-latex-pdf-process'.
3119 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3120 file used to preview a LaTeX snippet. In this case, do not
3121 create a log buffer and do not bother removing log files.
3123 Return PDF file name or an error if it couldn't be produced."
3124 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3125 (full-name (file-truename texfile))
3126 (out-dir (file-name-directory texfile))
3127 ;; Properly set working directory for compilation.
3128 (default-directory (if (file-name-absolute-p texfile)
3129 (file-name-directory full-name)
3130 default-directory))
3131 (time (current-time))
3132 warnings)
3133 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
3134 (save-window-excursion
3135 (cond
3136 ;; A function is provided: Apply it.
3137 ((functionp org-latex-pdf-process)
3138 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3139 ;; A list is provided: Replace %b, %f and %o with appropriate
3140 ;; values in each command before applying it. Output is
3141 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3142 ((consp org-latex-pdf-process)
3143 (let ((outbuf (and (not snippet)
3144 (get-buffer-create "*Org PDF LaTeX Output*"))))
3145 (dolist (command org-latex-pdf-process)
3146 (shell-command
3147 (replace-regexp-in-string
3148 "%b" (shell-quote-argument base-name)
3149 (replace-regexp-in-string
3150 "%f" (shell-quote-argument full-name)
3151 (replace-regexp-in-string
3152 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
3153 outbuf))
3154 ;; Collect standard errors from output buffer.
3155 (setq warnings (and (not snippet)
3156 (org-latex--collect-warnings outbuf)))))
3157 (t (error "No valid command to process to PDF")))
3158 (let ((pdffile (concat out-dir base-name ".pdf")))
3159 ;; Check for process failure. Provide collected errors if
3160 ;; possible.
3161 (if (or (not (file-exists-p pdffile))
3162 (time-less-p (nth 5 (file-attributes pdffile)) time))
3163 (error (format "PDF file %s wasn't produced" pdffile))
3164 ;; Else remove log files, when specified, and signal end of
3165 ;; process to user, along with any error encountered.
3166 (unless snippet
3167 (when org-latex-remove-logfiles
3168 (dolist (file (directory-files
3169 out-dir t
3170 (concat (regexp-quote base-name)
3171 "\\(?:\\.[0-9]+\\)?"
3172 "\\."
3173 (regexp-opt org-latex-logfiles-extensions))))
3174 (delete-file file)))
3175 (message (concat "PDF file produced"
3176 (cond
3177 ((eq warnings 'error) " with errors.")
3178 (warnings (concat " with warnings: " warnings))
3179 (t "."))))))
3180 ;; Return output file name.
3181 pdffile))))
3183 (defun org-latex--collect-warnings (buffer)
3184 "Collect some warnings from \"pdflatex\" command output.
3185 BUFFER is the buffer containing output. Return collected
3186 warnings types as a string, `error' if a LaTeX error was
3187 encountered or nil if there was none."
3188 (with-current-buffer buffer
3189 (save-excursion
3190 (goto-char (point-max))
3191 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3192 (if (re-search-forward "^!" nil t) 'error
3193 (let ((case-fold-search t)
3194 (warnings ""))
3195 (dolist (warning org-latex-known-warnings)
3196 (when (save-excursion (re-search-forward (car warning) nil t))
3197 (setq warnings (concat warnings " " (cdr warning)))))
3198 (org-string-nw-p (org-trim warnings))))))))
3200 ;;;###autoload
3201 (defun org-latex-publish-to-latex (plist filename pub-dir)
3202 "Publish an Org file to LaTeX.
3204 FILENAME is the filename of the Org file to be published. PLIST
3205 is the property list for the given project. PUB-DIR is the
3206 publishing directory.
3208 Return output file name."
3209 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3211 ;;;###autoload
3212 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3213 "Publish an Org file to PDF (via LaTeX).
3215 FILENAME is the filename of the Org file to be published. PLIST
3216 is the property list for the given project. PUB-DIR is the
3217 publishing directory.
3219 Return output file name."
3220 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3221 ;; in working directory and then moved to publishing directory.
3222 (org-publish-attachment
3223 plist
3224 (org-latex-compile
3225 (org-publish-org-to
3226 'latex filename ".tex" plist (file-name-directory filename)))
3227 pub-dir))
3230 (provide 'ox-latex)
3232 ;; Local variables:
3233 ;; generated-autoload-file: "org-loaddefs.el"
3234 ;; End:
3236 ;;; ox-latex.el ends here