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