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