ox-latex: Tiny refactoring
[org-mode/org-tableheadings.git] / lisp / ox-latex.el
blobace7745cdb820d56acc20a17936a602c53e4f1f6
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 CUSTOM_ID property, NAME keyword or Org target as the key for the
245 \\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 (datum info &optional force full)
1049 "Return an appropriate label for DATUM.
1050 DATUM is an element or a `target' type object. INFO is the
1051 current export state, as a plist.
1053 Return nil if element DATUM has no NAME or VALUE affiliated
1054 keyword or no CUSTOM_ID property, unless FORCE is non-nil. In
1055 this case always return a unique label.
1057 Eventually, if FULL is non-nil, wrap label within \"\\label{}\"."
1058 (let* ((type (org-element-type datum))
1059 (user-label
1060 (org-element-property
1061 (case type
1062 ((headline inlinetask) :CUSTOM_ID)
1063 (target :value)
1064 (otherwise :name))
1065 datum))
1066 (label
1067 (and (or user-label force)
1068 (if (and user-label (plist-get info :latex-prefer-user-labels))
1069 user-label
1070 (org-export-get-reference datum info)))))
1071 (cond ((not full) label)
1072 (label (format "\\label{%s}%s"
1073 label
1074 (if (eq type 'target) "" "\n")))
1075 (t ""))))
1077 (defun org-latex--caption/label-string (element info)
1078 "Return caption and label LaTeX string for ELEMENT.
1080 INFO is a plist holding contextual information. If there's no
1081 caption nor label, return the empty string.
1083 For non-floats, see `org-latex--wrap-label'."
1084 (let* ((label (org-latex--label element info nil t))
1085 (main (org-export-get-caption element))
1086 (short (org-export-get-caption element t))
1087 (caption-from-attr-latex
1088 (org-export-read-attribute :attr_latex element :caption)))
1089 (cond
1090 ((org-string-nw-p caption-from-attr-latex)
1091 (concat caption-from-attr-latex "\n"))
1092 ((and (not main) (equal label "")) "")
1093 ((not main) (concat label "\n"))
1094 ;; Option caption format with short name.
1095 (short (format "\\caption[%s]{%s%s}\n"
1096 (org-export-data short info)
1097 label
1098 (org-export-data main info)))
1099 ;; Standard caption format.
1100 (t (format "\\caption{%s%s}\n" label (org-export-data main info))))))
1102 (defun org-latex-guess-inputenc (header)
1103 "Set the coding system in inputenc to what the buffer is.
1105 HEADER is the LaTeX header string. This function only applies
1106 when specified inputenc option is \"AUTO\".
1108 Return the new header, as a string."
1109 (let* ((cs (or (ignore-errors
1110 (latexenc-coding-system-to-inputenc
1111 (or org-export-coding-system buffer-file-coding-system)))
1112 "utf8")))
1113 (if (not cs) header
1114 ;; First translate if that is requested.
1115 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
1116 ;; Then find the \usepackage statement and replace the option.
1117 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1118 cs header t nil 1))))
1120 (defun org-latex-guess-babel-language (header info)
1121 "Set Babel's language according to LANGUAGE keyword.
1123 HEADER is the LaTeX header string. INFO is the plist used as
1124 a communication channel.
1126 Insertion of guessed language only happens when Babel package has
1127 explicitly been loaded. Then it is added to the rest of
1128 package's options.
1130 The argument to Babel may be \"AUTO\" which is then replaced with
1131 the language of the document or `org-export-default-language'
1132 unless language in question is already loaded.
1134 Return the new header."
1135 (let ((language-code (plist-get info :language)))
1136 ;; If no language is set or Babel package is not loaded, return
1137 ;; HEADER as-is.
1138 (if (or (not (stringp language-code))
1139 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
1140 header
1141 (let ((options (save-match-data
1142 (org-split-string (match-string 1 header) ",[ \t]*")))
1143 (language (cdr (assoc language-code
1144 org-latex-babel-language-alist))))
1145 ;; If LANGUAGE is already loaded, return header without AUTO.
1146 ;; Otherwise, replace AUTO with language or append language if
1147 ;; AUTO is not present.
1148 (replace-match
1149 (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
1150 (cond ((member language options) (delete "AUTO" options))
1151 ((member "AUTO" options) options)
1152 (t (append options (list language))))
1153 ", ")
1154 t nil header 1)))))
1156 (defun org-latex--find-verb-separator (s)
1157 "Return a character not used in string S.
1158 This is used to choose a separator for constructs like \\verb."
1159 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1160 (loop for c across ll
1161 when (not (string-match (regexp-quote (char-to-string c)) s))
1162 return (char-to-string c))))
1164 (defun org-latex--make-option-string (options)
1165 "Return a comma separated string of keywords and values.
1166 OPTIONS is an alist where the key is the options keyword as
1167 a string, and the value a list containing the keyword value, or
1168 nil."
1169 (mapconcat (lambda (pair)
1170 (concat (first pair)
1171 (when (> (length (second pair)) 0)
1172 (concat "=" (second pair)))))
1173 options
1174 ","))
1176 (defun org-latex--wrap-label (element output info)
1177 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1178 INFO is the current export state, as a plist. This function
1179 should not be used for floats. See
1180 `org-latex--caption/label-string'."
1181 (if (not (and (org-string-nw-p output) (org-element-property :name element)))
1182 output
1183 (concat (format "\\phantomsection\n\\label{%s}\n"
1184 (org-latex--label element info))
1185 output)))
1187 (defun org-latex--text-markup (text markup info)
1188 "Format TEXT depending on MARKUP text markup.
1189 INFO is a plist used as a communication channel. See
1190 `org-latex-text-markup-alist' for details."
1191 (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
1192 (cond
1193 ;; No format string: Return raw text.
1194 ((not fmt) text)
1195 ;; Handle the `verb' special case: Find an appropriate separator
1196 ;; and use "\\verb" command.
1197 ((eq 'verb fmt)
1198 (let ((separator (org-latex--find-verb-separator text)))
1199 (concat "\\verb" separator
1200 (replace-regexp-in-string "\n" " " text)
1201 separator)))
1202 ;; Handle the `protectedtexttt' special case: Protect some
1203 ;; special chars and use "\texttt{%s}" format string.
1204 ((eq 'protectedtexttt fmt)
1205 (let ((start 0)
1206 (trans '(("\\" . "\\textbackslash{}")
1207 ("~" . "\\textasciitilde{}")
1208 ("^" . "\\textasciicircum{}")))
1209 (rtn "")
1210 char)
1211 (while (string-match "[\\{}$%&_#~^]" text)
1212 (setq char (match-string 0 text))
1213 (if (> (match-beginning 0) 0)
1214 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
1215 (setq text (substring text (1+ (match-beginning 0))))
1216 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1217 rtn (concat rtn char)))
1218 (setq text (concat rtn text)
1219 fmt "\\texttt{%s}")
1220 (while (string-match "--" text)
1221 (setq text (replace-match "-{}-" t t text)))
1222 (format fmt text)))
1223 ;; Else use format string.
1224 (t (format fmt text)))))
1226 (defun org-latex--delayed-footnotes-definitions (element info)
1227 "Return footnotes definitions in ELEMENT as a string.
1229 INFO is a plist used as a communication channel.
1231 Footnotes definitions are returned within \"\\footnotetxt{}\"
1232 commands.
1234 This function is used within constructs that don't support
1235 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1236 \"\\footnotemark\" is used within the construct and the function
1237 just outside of it."
1238 (mapconcat
1239 (lambda (ref)
1240 (format
1241 "\\footnotetext[%s]{%s}"
1242 (org-export-get-footnote-number ref info)
1243 (org-trim
1244 (org-export-data
1245 (org-export-get-footnote-definition ref info) info))))
1246 ;; Find every footnote reference in ELEMENT.
1247 (let* (all-refs
1248 search-refs ; For byte-compiler.
1249 (search-refs
1250 (function
1251 (lambda (data)
1252 ;; Return a list of all footnote references never seen
1253 ;; before in DATA.
1254 (org-element-map data 'footnote-reference
1255 (lambda (ref)
1256 (when (org-export-footnote-first-reference-p ref info)
1257 (push ref all-refs)
1258 (when (eq (org-element-property :type ref) 'standard)
1259 (funcall search-refs
1260 (org-export-get-footnote-definition ref info)))))
1261 info)
1262 (reverse all-refs)))))
1263 (funcall search-refs element))
1264 ""))
1266 (defun org-latex--translate (s info)
1267 "Translate string S according to specified language.
1268 INFO is a plist used as a communication channel."
1269 (org-export-translate s :latex info))
1271 (defun org-latex--format-spec (info)
1272 "Create a format-spec for document meta-data.
1273 INFO is a plist used as a communication channel."
1274 (let ((language (let ((lang (plist-get info :language)))
1275 (or (cdr (assoc lang org-latex-babel-language-alist))
1276 lang))))
1277 `((?a . ,(org-export-data (plist-get info :author) info))
1278 (?t . ,(org-export-data (plist-get info :title) info))
1279 (?k . ,(org-export-data (org-latex--wrap-latex-math-block
1280 (plist-get info :keywords) info)
1281 info))
1282 (?d . ,(org-export-data (org-latex--wrap-latex-math-block
1283 (plist-get info :description) info)
1284 info))
1285 (?c . ,(plist-get info :creator))
1286 (?l . ,language)
1287 (?L . ,(capitalize language))
1288 (?D . ,(org-export-get-date info)))))
1291 ;;; Template
1293 (defun org-latex-template (contents info)
1294 "Return complete document string after LaTeX conversion.
1295 CONTENTS is the transcoded contents string. INFO is a plist
1296 holding export options."
1297 (let ((title (org-export-data (plist-get info :title) info))
1298 (spec (org-latex--format-spec info)))
1299 (concat
1300 ;; Time-stamp.
1301 (and (plist-get info :time-stamp-file)
1302 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1303 ;; Document class and packages.
1304 (let* ((class (plist-get info :latex-class))
1305 (class-options (plist-get info :latex-class-options))
1306 (header (nth 1 (assoc class (plist-get info :latex-classes))))
1307 (document-class-string
1308 (and (stringp header)
1309 (if (not class-options) header
1310 (replace-regexp-in-string
1311 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1312 class-options header t nil 1)))))
1313 (if (not document-class-string)
1314 (user-error "Unknown LaTeX class `%s'" class)
1315 (org-latex-guess-babel-language
1316 (org-latex-guess-inputenc
1317 (org-element-normalize-string
1318 (org-splice-latex-header
1319 document-class-string
1320 org-latex-default-packages-alist
1321 org-latex-packages-alist nil
1322 (concat (org-element-normalize-string
1323 (plist-get info :latex-header))
1324 (plist-get info :latex-header-extra)))))
1325 info)))
1326 ;; Possibly limit depth for headline numbering.
1327 (let ((sec-num (plist-get info :section-numbers)))
1328 (when (integerp sec-num)
1329 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1330 ;; Author.
1331 (let ((author (and (plist-get info :with-author)
1332 (let ((auth (plist-get info :author)))
1333 (and auth (org-export-data auth info)))))
1334 (email (and (plist-get info :with-email)
1335 (org-export-data (plist-get info :email) info))))
1336 (cond ((and author email (not (string= "" email)))
1337 (format "\\author{%s\\thanks{%s}}\n" author email))
1338 ((or author email) (format "\\author{%s}\n" (or author email)))))
1339 ;; Date.
1340 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1341 (format "\\date{%s}\n" (org-export-data date info)))
1342 ;; Title and subtitle.
1343 (let* ((subtitle (plist-get info :subtitle))
1344 (formatted-subtitle
1345 (when subtitle
1346 (format (plist-get info :latex-subtitle-format)
1347 (org-export-data subtitle info))))
1348 (separate (plist-get info :latex-subtitle-separate)))
1349 (concat
1350 (format "\\title{%s%s}\n" title
1351 (if separate "" (or formatted-subtitle "")))
1352 (when (and separate subtitle)
1353 (concat formatted-subtitle "\n"))))
1354 ;; Hyperref options.
1355 (let ((template (plist-get info :latex-hyperref-template)))
1356 (and (stringp template)
1357 (format-spec template spec)))
1358 ;; Document start.
1359 "\\begin{document}\n\n"
1360 ;; Title command.
1361 (let* ((title-command (plist-get info :latex-title-command))
1362 (command (and (stringp title-command)
1363 (format-spec title-command spec))))
1364 (org-element-normalize-string
1365 (cond ((not (plist-get info :with-title)) nil)
1366 ((string= "" title) nil)
1367 ((not (stringp command)) nil)
1368 ((string-match "\\(?:[^%]\\|^\\)%s" command)
1369 (format command title))
1370 (t command))))
1371 ;; Table of contents.
1372 (let ((depth (plist-get info :with-toc)))
1373 (when depth
1374 (concat (when (wholenump depth)
1375 (format "\\setcounter{tocdepth}{%d}\n" depth))
1376 (plist-get info :latex-toc-command))))
1377 ;; Document's body.
1378 contents
1379 ;; Creator.
1380 (and (plist-get info :with-creator)
1381 (concat (plist-get info :creator) "\n"))
1382 ;; Document end.
1383 "\\end{document}")))
1387 ;;; Transcode Functions
1389 ;;;; Bold
1391 (defun org-latex-bold (bold contents info)
1392 "Transcode BOLD from Org to LaTeX.
1393 CONTENTS is the text with bold markup. INFO is a plist holding
1394 contextual information."
1395 (org-latex--text-markup contents 'bold info))
1398 ;;;; Center Block
1400 (defun org-latex-center-block (center-block contents info)
1401 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1402 CONTENTS holds the contents of the center block. INFO is a plist
1403 holding contextual information."
1404 (org-latex--wrap-label
1405 center-block (format "\\begin{center}\n%s\\end{center}" contents) info))
1408 ;;;; Clock
1410 (defun org-latex-clock (clock contents info)
1411 "Transcode a CLOCK element from Org to LaTeX.
1412 CONTENTS is nil. INFO is a plist holding contextual
1413 information."
1414 (concat
1415 "\\noindent"
1416 (format "\\textbf{%s} " org-clock-string)
1417 (format (plist-get info :latex-inactive-timestamp-format)
1418 (concat (org-timestamp-translate (org-element-property :value clock))
1419 (let ((time (org-element-property :duration clock)))
1420 (and time (format " (%s)" time)))))
1421 "\\\\"))
1424 ;;;; Code
1426 (defun org-latex-code (code contents info)
1427 "Transcode a CODE object from Org to LaTeX.
1428 CONTENTS is nil. INFO is a plist used as a communication
1429 channel."
1430 (org-latex--text-markup (org-element-property :value code) 'code info))
1433 ;;;; Drawer
1435 (defun org-latex-drawer (drawer contents info)
1436 "Transcode a DRAWER element from Org to LaTeX.
1437 CONTENTS holds the contents of the block. INFO is a plist
1438 holding contextual information."
1439 (let* ((name (org-element-property :drawer-name drawer))
1440 (output (funcall (plist-get info :latex-format-drawer-function)
1441 name contents)))
1442 (org-latex--wrap-label drawer output info)))
1445 ;;;; Dynamic Block
1447 (defun org-latex-dynamic-block (dynamic-block contents info)
1448 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1449 CONTENTS holds the contents of the block. INFO is a plist
1450 holding contextual information. See `org-export-data'."
1451 (org-latex--wrap-label dynamic-block contents info))
1454 ;;;; Entity
1456 (defun org-latex-entity (entity contents info)
1457 "Transcode an ENTITY object from Org to LaTeX.
1458 CONTENTS are the definition itself. INFO is a plist holding
1459 contextual information."
1460 (org-element-property :latex entity))
1463 ;;;; Example Block
1465 (defun org-latex-example-block (example-block contents info)
1466 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1467 CONTENTS is nil. INFO is a plist holding contextual
1468 information."
1469 (when (org-string-nw-p (org-element-property :value example-block))
1470 (org-latex--wrap-label
1471 example-block
1472 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1473 (org-export-format-code-default example-block info))
1474 info)))
1477 ;;;; Export Block
1479 (defun org-latex-export-block (export-block contents info)
1480 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1481 CONTENTS is nil. INFO is a plist holding contextual information."
1482 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1483 (org-remove-indentation (org-element-property :value export-block))))
1486 ;;;; Export Snippet
1488 (defun org-latex-export-snippet (export-snippet contents info)
1489 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1490 CONTENTS is nil. INFO is a plist holding contextual information."
1491 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1492 (org-element-property :value export-snippet)))
1495 ;;;; Fixed Width
1497 (defun org-latex-fixed-width (fixed-width contents info)
1498 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1499 CONTENTS is nil. INFO is a plist holding contextual information."
1500 (org-latex--wrap-label
1501 fixed-width
1502 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1503 (org-remove-indentation
1504 (org-element-property :value fixed-width)))
1505 info))
1508 ;;;; Footnote Reference
1510 (defun org-latex-footnote-reference (footnote-reference contents info)
1511 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1512 CONTENTS is nil. INFO is a plist holding contextual information."
1513 (concat
1514 ;; Insert separator between two footnotes in a row.
1515 (let ((prev (org-export-get-previous-element footnote-reference info)))
1516 (when (eq (org-element-type prev) 'footnote-reference)
1517 (plist-get info :latex-footnote-separator)))
1518 (cond
1519 ;; Use \footnotemark if the footnote has already been defined.
1520 ((not (org-export-footnote-first-reference-p footnote-reference info))
1521 (format "\\footnotemark[%s]{}"
1522 (org-export-get-footnote-number footnote-reference info)))
1523 ;; Use \footnotemark if reference is within another footnote
1524 ;; reference, footnote definition or table cell.
1525 ((org-element-lineage footnote-reference
1526 '(footnote-reference footnote-definition table-cell))
1527 "\\footnotemark")
1528 ;; Otherwise, define it with \footnote command.
1530 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1531 (concat
1532 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1533 ;; Retrieve all footnote references within the footnote and
1534 ;; add their definition after it, since LaTeX doesn't support
1535 ;; them inside.
1536 (org-latex--delayed-footnotes-definitions def info)))))))
1539 ;;;; Headline
1541 (defun org-latex-headline (headline contents info)
1542 "Transcode a HEADLINE element from Org to LaTeX.
1543 CONTENTS holds the contents of the headline. INFO is a plist
1544 holding contextual information."
1545 (unless (org-element-property :footnote-section-p headline)
1546 (let* ((class (plist-get info :latex-class))
1547 (level (org-export-get-relative-level headline info))
1548 (numberedp (org-export-numbered-headline-p headline info))
1549 (class-sectioning (assoc class (plist-get info :latex-classes)))
1550 ;; Section formatting will set two placeholders: one for
1551 ;; the title and the other for the contents.
1552 (section-fmt
1553 (let ((sec (if (functionp (nth 2 class-sectioning))
1554 (funcall (nth 2 class-sectioning) level numberedp)
1555 (nth (1+ level) class-sectioning))))
1556 (cond
1557 ;; No section available for that LEVEL.
1558 ((not sec) nil)
1559 ;; Section format directly returned by a function. Add
1560 ;; placeholder for contents.
1561 ((stringp sec) (concat sec "\n%s"))
1562 ;; (numbered-section . unnumbered-section)
1563 ((not (consp (cdr sec)))
1564 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1565 ;; (numbered-open numbered-close)
1566 ((= (length sec) 2)
1567 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1568 ;; (num-in num-out no-num-in no-num-out)
1569 ((= (length sec) 4)
1570 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1571 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1572 ;; Create a temporary export back-end that hard-codes
1573 ;; "\underline" within "\section" and alike.
1574 (section-back-end
1575 (org-export-create-backend
1576 :parent 'latex
1577 :transcoders
1578 '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
1579 (text
1580 (org-export-data-with-backend
1581 (org-element-property :title headline) section-back-end info))
1582 (todo
1583 (and (plist-get info :with-todo-keywords)
1584 (let ((todo (org-element-property :todo-keyword headline)))
1585 (and todo (org-export-data todo info)))))
1586 (todo-type (and todo (org-element-property :todo-type headline)))
1587 (tags (and (plist-get info :with-tags)
1588 (org-export-get-tags headline info)))
1589 (priority (and (plist-get info :with-priority)
1590 (org-element-property :priority headline)))
1591 ;; Create the headline text along with a no-tag version.
1592 ;; The latter is required to remove tags from toc.
1593 (full-text (funcall (plist-get info :latex-format-headline-function)
1594 todo todo-type priority text tags info))
1595 ;; Associate \label to the headline for internal links.
1596 (headline-label (org-latex--label headline info t t))
1597 (pre-blanks
1598 (make-string (org-element-property :pre-blank headline) ?\n)))
1599 (if (or (not section-fmt) (org-export-low-level-p headline info))
1600 ;; This is a deep sub-tree: export it as a list item. Also
1601 ;; export as items headlines for which no section format has
1602 ;; been found.
1603 (let ((low-level-body
1604 (concat
1605 ;; If headline is the first sibling, start a list.
1606 (when (org-export-first-sibling-p headline info)
1607 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1608 ;; Itemize headline
1609 "\\item"
1610 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
1611 "\\relax")
1612 " " full-text "\n"
1613 headline-label
1614 pre-blanks
1615 contents)))
1616 ;; If headline is not the last sibling simply return
1617 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1618 ;; any blank line.
1619 (if (not (org-export-last-sibling-p headline info)) low-level-body
1620 (replace-regexp-in-string
1621 "[ \t\n]*\\'"
1622 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1623 low-level-body)))
1624 ;; This is a standard headline. Export it as a section. Add
1625 ;; an alternative heading when possible, and when this is not
1626 ;; identical to the usual heading.
1627 (let ((opt-title
1628 (funcall (plist-get info :latex-format-headline-function)
1629 todo todo-type priority
1630 (org-export-data-with-backend
1631 (org-export-get-alt-title headline info)
1632 section-back-end info)
1633 (and (eq (plist-get info :with-tags) t) tags)
1634 info))
1635 ;; Maybe end local TOC (see `org-latex-keyword').
1636 (contents
1637 (concat
1638 contents
1639 (let ((case-fold-search t)
1640 (section
1641 (let ((first (car (org-element-contents headline))))
1642 (and (eq (org-element-type first) 'section) first))))
1643 (org-element-map section 'keyword
1644 (lambda (k)
1645 (and (equal (org-element-property :key k) "TOC")
1646 (let ((v (org-element-property :value k)))
1647 (and (org-string-match-p "\\<headlines\\>" v)
1648 (org-string-match-p "\\<local\\>" v)
1649 (format "\\stopcontents[level-%d]" level)))))
1650 info t)))))
1651 (if (and opt-title
1652 (not (equal opt-title full-text))
1653 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
1654 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1655 ;; Replace square brackets with parenthesis
1656 ;; since square brackets are not supported in
1657 ;; optional arguments.
1658 (replace-regexp-in-string
1659 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1660 full-text
1661 (concat headline-label pre-blanks contents))
1662 ;; Impossible to add an alternative heading. Fallback to
1663 ;; regular sectioning format string.
1664 (format section-fmt full-text
1665 (concat headline-label pre-blanks contents))))))))
1667 (defun org-latex-format-headline-default-function
1668 (todo todo-type priority text tags info)
1669 "Default format function for a headline.
1670 See `org-latex-format-headline-function' for details."
1671 (concat
1672 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1673 (and priority (format "\\framebox{\\#%c} " priority))
1674 text
1675 (and tags
1676 (format "\\hfill{}\\textsc{%s}"
1677 (mapconcat (lambda (tag) (org-latex-plain-text tag info))
1678 tags ":")))))
1681 ;;;; Horizontal Rule
1683 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1684 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1685 CONTENTS is nil. INFO is a plist holding contextual information."
1686 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1687 (prev (org-export-get-previous-element horizontal-rule info)))
1688 (concat
1689 ;; Make sure the rule doesn't start at the end of the current
1690 ;; line by separating it with a blank line from previous element.
1691 (when (and prev
1692 (let ((prev-blank (org-element-property :post-blank prev)))
1693 (or (not prev-blank) (zerop prev-blank))))
1694 "\n")
1695 (org-latex--wrap-label
1696 horizontal-rule
1697 (format "\\rule{%s}{%s}"
1698 (or (plist-get attr :width) "\\linewidth")
1699 (or (plist-get attr :thickness) "0.5pt"))
1700 info))))
1703 ;;;; Inline Src Block
1705 (defun org-latex-inline-src-block (inline-src-block contents info)
1706 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1707 CONTENTS holds the contents of the item. INFO is a plist holding
1708 contextual information."
1709 (let* ((code (org-element-property :value inline-src-block))
1710 (separator (org-latex--find-verb-separator code)))
1711 (case (plist-get info :latex-listings)
1712 ;; Do not use a special package: transcode it verbatim.
1713 ((nil) (concat "\\verb" separator code separator))
1714 ;; Use minted package.
1715 (minted
1716 (let* ((org-lang (org-element-property :language inline-src-block))
1717 (mint-lang (or (cadr (assq (intern org-lang)
1718 (plist-get info :latex-minted-langs)))
1719 (downcase org-lang)))
1720 (options (org-latex--make-option-string
1721 (plist-get info :latex-minted-options))))
1722 (concat (format "\\mint%s{%s}"
1723 (if (string= options "") "" (format "[%s]" options))
1724 mint-lang)
1725 separator code separator)))
1726 ;; Use listings package.
1727 (otherwise
1728 ;; Maybe translate language's name.
1729 (let* ((org-lang (org-element-property :language inline-src-block))
1730 (lst-lang (or (cadr (assq (intern org-lang)
1731 (plist-get info :latex-listings-langs)))
1732 org-lang))
1733 (options (org-latex--make-option-string
1734 (append (plist-get info :latex-listings-options)
1735 `(("language" ,lst-lang))))))
1736 (concat (format "\\lstinline[%s]" options)
1737 separator code separator))))))
1740 ;;;; Inlinetask
1742 (defun org-latex-inlinetask (inlinetask contents info)
1743 "Transcode an INLINETASK element from Org to LaTeX.
1744 CONTENTS holds the contents of the block. INFO is a plist
1745 holding contextual information."
1746 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1747 (todo (and (plist-get info :with-todo-keywords)
1748 (let ((todo (org-element-property :todo-keyword inlinetask)))
1749 (and todo (org-export-data todo info)))))
1750 (todo-type (org-element-property :todo-type inlinetask))
1751 (tags (and (plist-get info :with-tags)
1752 (org-export-get-tags inlinetask info)))
1753 (priority (and (plist-get info :with-priority)
1754 (org-element-property :priority inlinetask)))
1755 (contents (concat (org-latex--label inlinetask info) contents)))
1756 (funcall (plist-get info :latex-format-inlinetask-function)
1757 todo todo-type priority title tags contents info)))
1759 (defun org-latex-format-inlinetask-default-function
1760 (todo todo-type priority title tags contents info)
1761 "Default format function for a inlinetasks.
1762 See `org-latex-format-inlinetask-function' for details."
1763 (let ((full-title
1764 (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1765 (when priority (format "\\framebox{\\#%c} " priority))
1766 title
1767 (when tags
1768 (format "\\hfill{}\\textsc{:%s:}"
1769 (mapconcat
1770 (lambda (tag) (org-latex-plain-text tag info))
1771 tags ":"))))))
1772 (concat "\\begin{center}\n"
1773 "\\fbox{\n"
1774 "\\begin{minipage}[c]{.6\\textwidth}\n"
1775 full-title "\n\n"
1776 (and (org-string-nw-p contents)
1777 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
1778 "\\end{minipage}\n"
1779 "}\n"
1780 "\\end{center}")))
1783 ;;;; Italic
1785 (defun org-latex-italic (italic contents info)
1786 "Transcode ITALIC from Org to LaTeX.
1787 CONTENTS is the text with italic markup. INFO is a plist holding
1788 contextual information."
1789 (org-latex--text-markup contents 'italic info))
1792 ;;;; Item
1794 (defun org-latex-item (item contents info)
1795 "Transcode an ITEM element from Org to LaTeX.
1796 CONTENTS holds the contents of the item. INFO is a plist holding
1797 contextual information."
1798 (let* ((counter
1799 (let ((count (org-element-property :counter item))
1800 (level
1801 ;; Determine level of current item to determine the
1802 ;; correct LaTeX counter to use (enumi, enumii...).
1803 (let ((parent item) (level 0))
1804 (while (memq (org-element-type
1805 (setq parent (org-export-get-parent parent)))
1806 '(plain-list item))
1807 (when (and (eq (org-element-type parent) 'plain-list)
1808 (eq (org-element-property :type parent)
1809 'ordered))
1810 (incf level)))
1811 level)))
1812 (and count
1813 (< level 5)
1814 (format "\\setcounter{enum%s}{%s}\n"
1815 (nth (1- level) '("i" "ii" "iii" "iv"))
1816 (1- count)))))
1817 (checkbox (case (org-element-property :checkbox item)
1818 (on "$\\boxtimes$ ")
1819 (off "$\\square$ ")
1820 (trans "$\\boxminus$ ")))
1821 (tag (let ((tag (org-element-property :tag item)))
1822 ;; Check-boxes must belong to the tag.
1823 (and tag (format "[{%s}] "
1824 (concat checkbox
1825 (org-export-data tag info)))))))
1826 (concat counter
1827 "\\item"
1828 (cond
1829 (tag)
1830 (checkbox (concat " " checkbox))
1831 ;; Without a tag or a check-box, if CONTENTS starts with
1832 ;; an opening square bracket, add "\relax" to "\item",
1833 ;; unless the brackets comes from an initial export
1834 ;; snippet (i.e. it is inserted willingly by the user).
1835 ((and contents
1836 (org-string-match-p "\\`[ \t]*\\[" contents)
1837 (not (let ((e (car (org-element-contents item))))
1838 (and (eq (org-element-type e) 'paragraph)
1839 (let ((o (car (org-element-contents e))))
1840 (and (eq (org-element-type o) 'export-snippet)
1841 (eq (org-export-snippet-backend o)
1842 'latex)))))))
1843 "\\relax ")
1844 (t " "))
1845 (and contents (org-trim contents))
1846 ;; If there are footnotes references in tag, be sure to
1847 ;; add their definition at the end of the item. This
1848 ;; workaround is necessary since "\footnote{}" command is
1849 ;; not supported in tags.
1850 (and tag
1851 (org-latex--delayed-footnotes-definitions
1852 (org-element-property :tag item) info)))))
1855 ;;;; Keyword
1857 (defun org-latex-keyword (keyword contents info)
1858 "Transcode a KEYWORD element from Org to LaTeX.
1859 CONTENTS is nil. INFO is a plist holding contextual information."
1860 (let ((key (org-element-property :key keyword))
1861 (value (org-element-property :value keyword)))
1862 (cond
1863 ((string= key "LATEX") value)
1864 ((string= key "INDEX") (format "\\index{%s}" value))
1865 ((string= key "TOC")
1866 (let ((case-fold-search t))
1867 (cond
1868 ((org-string-match-p "\\<headlines\\>" value)
1869 (let* ((localp (org-string-match-p "\\<local\\>" value))
1870 (parent (org-element-lineage keyword '(headline)))
1871 (level (if (not (and localp parent)) 0
1872 (org-export-get-relative-level parent info)))
1873 (depth
1874 (and (string-match "\\<[0-9]+\\>" value)
1875 (format
1876 "\\setcounter{tocdepth}{%d}"
1877 (+ (string-to-number (match-string 0 value)) level)))))
1878 (if (and localp parent)
1879 ;; Start local TOC, assuming package "titletoc" is
1880 ;; required.
1881 (format "\\startcontents[level-%d]
1882 \\printcontents[level-%d]{}{0}{%s}"
1883 level level (or depth ""))
1884 (concat depth (and depth "\n") "\\tableofcontents"))))
1885 ((org-string-match-p "\\<tables\\>" value) "\\listoftables")
1886 ((org-string-match-p "\\<listings\\>" value)
1887 (case (plist-get info :latex-listings)
1888 ((nil) "\\listoffigures")
1889 (minted "\\listoflistings")
1890 (otherwise "\\lstlistoflistings")))))))))
1893 ;;;; Latex Environment
1895 (defun org-latex-latex-environment (latex-environment contents info)
1896 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1897 CONTENTS is nil. INFO is a plist holding contextual information."
1898 (when (plist-get info :with-latex)
1899 (let ((value (org-remove-indentation
1900 (org-element-property :value latex-environment))))
1901 (if (not (org-element-property :name latex-environment)) value
1902 ;; Environment is labeled: label must be within the environment
1903 ;; (otherwise, a reference pointing to that element will count
1904 ;; the section instead).
1905 (with-temp-buffer
1906 (insert value)
1907 (goto-char (point-min))
1908 (forward-line)
1909 (insert (org-latex--label latex-environment info nil t))
1910 (buffer-string))))))
1913 ;;;; Latex Fragment
1915 (defun org-latex-latex-fragment (latex-fragment contents info)
1916 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1917 CONTENTS is nil. INFO is a plist holding contextual information."
1918 (let ((value (org-element-property :value latex-fragment)))
1919 ;; Trim math markers since the fragment is enclosed within
1920 ;; a latex-math-block object anyway.
1921 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
1922 (match-string 2 value))
1923 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
1924 (match-string 1 value))
1925 (t value))))
1928 ;;;; Line Break
1930 (defun org-latex-line-break (line-break contents info)
1931 "Transcode a LINE-BREAK object from Org to LaTeX.
1932 CONTENTS is nil. INFO is a plist holding contextual information."
1933 "\\\\\n")
1936 ;;;; Link
1938 (defun org-latex--inline-image (link info)
1939 "Return LaTeX code for an inline image.
1940 LINK is the link pointing to the inline image. INFO is a plist
1941 used as a communication channel."
1942 (let* ((parent (org-export-get-parent-element link))
1943 (path (let ((raw-path (org-element-property :path link)))
1944 (if (not (file-name-absolute-p raw-path)) raw-path
1945 (expand-file-name raw-path))))
1946 (filetype (file-name-extension path))
1947 (caption (org-latex--caption/label-string parent info))
1948 (caption-above-p (org-latex--caption-above-p link info))
1949 ;; Retrieve latex attributes from the element around.
1950 (attr (org-export-read-attribute :attr_latex parent))
1951 (float (let ((float (plist-get attr :float)))
1952 (cond ((and (not float) (plist-member attr :float)) nil)
1953 ((string= float "wrap") 'wrap)
1954 ((string= float "sideways") 'sideways)
1955 ((string= float "multicolumn") 'multicolumn)
1956 ((or float
1957 (org-element-property :caption parent)
1958 (org-string-nw-p (plist-get attr :caption)))
1959 'figure))))
1960 (placement
1961 (let ((place (plist-get attr :placement)))
1962 (cond
1963 (place (format "%s" place))
1964 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1965 ((eq float 'figure)
1966 (format "[%s]" (plist-get info :latex-default-figure-position)))
1967 (t ""))))
1968 (comment-include (if (plist-get attr :comment-include) "%" ""))
1969 ;; It is possible to specify width and height in the
1970 ;; ATTR_LATEX line, and also via default variables.
1971 (width (cond ((plist-get attr :width))
1972 ((plist-get attr :height) "")
1973 ((eq float 'wrap) "0.48\\textwidth")
1974 (t (plist-get info :latex-image-default-width))))
1975 (height (cond ((plist-get attr :height))
1976 ((or (plist-get attr :width)
1977 (memq float '(figure wrap))) "")
1978 (t (plist-get info :latex-image-default-height))))
1979 (options (let ((opt (or (plist-get attr :options)
1980 (plist-get info :latex-image-default-option))))
1981 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
1982 (match-string 1 opt))))
1983 image-code)
1984 (if (member filetype '("tikz" "pgf"))
1985 ;; For tikz images:
1986 ;; - use \input to read in image file.
1987 ;; - if options are present, wrap in a tikzpicture environment.
1988 ;; - if width or height are present, use \resizebox to change
1989 ;; the image size.
1990 (progn
1991 (setq image-code (format "\\input{%s}" path))
1992 (when (org-string-nw-p options)
1993 (setq image-code
1994 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1995 options
1996 image-code)))
1997 (when (or (org-string-nw-p width) (org-string-nw-p height))
1998 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
1999 (if (org-string-nw-p width) width "!")
2000 (if (org-string-nw-p height) height "!")
2001 image-code))))
2002 ;; For other images:
2003 ;; - add width and height to options.
2004 ;; - include the image with \includegraphics.
2005 (when (org-string-nw-p width)
2006 (setq options (concat options ",width=" width)))
2007 (when (org-string-nw-p height)
2008 (setq options (concat options ",height=" height)))
2009 (let ((search-option (org-element-property :search-option link)))
2010 (when (and search-option
2011 (equal filetype "pdf")
2012 (org-string-match-p "\\`[0-9]+\\'" search-option)
2013 (not (org-string-match-p "page=" options)))
2014 (setq options (concat options ",page=" search-option))))
2015 (setq image-code
2016 (format "\\includegraphics%s{%s}"
2017 (cond ((not (org-string-nw-p options)) "")
2018 ((= (aref options 0) ?,)
2019 (format "[%s]"(substring options 1)))
2020 (t (format "[%s]" options)))
2021 path))
2022 (when (equal filetype "svg")
2023 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
2024 "\\includesvg"
2025 image-code
2026 nil t))
2027 (setq image-code (replace-regexp-in-string "\\.svg}"
2029 image-code
2030 nil t))))
2031 ;; Return proper string, depending on FLOAT.
2032 (case float
2033 (wrap (format "\\begin{wrapfigure}%s
2034 %s\\centering
2035 %s%s
2036 %s\\end{wrapfigure}"
2037 placement
2038 (if caption-above-p caption "")
2039 comment-include image-code
2040 (if caption-above-p "" caption)))
2041 (sideways (format "\\begin{sidewaysfigure}
2042 %s\\centering
2043 %s%s
2044 %s\\end{sidewaysfigure}"
2045 (if caption-above-p caption "")
2046 comment-include image-code
2047 (if caption-above-p "" caption)))
2048 (multicolumn (format "\\begin{figure*}%s
2049 %s\\centering
2050 %s%s
2051 %s\\end{figure*}"
2052 placement
2053 (if caption-above-p caption "")
2054 comment-include image-code
2055 (if caption-above-p "" caption)))
2056 (figure (format "\\begin{figure}%s
2057 %s\\centering
2058 %s%s
2059 %s\\end{figure}"
2060 placement
2061 (if caption-above-p caption "")
2062 comment-include image-code
2063 (if caption-above-p "" caption)))
2064 (otherwise image-code))))
2066 (defun org-latex-link (link desc info)
2067 "Transcode a LINK object from Org to LaTeX.
2069 DESC is the description part of the link, or the empty string.
2070 INFO is a plist holding contextual information. See
2071 `org-export-data'."
2072 (let* ((type (org-element-property :type link))
2073 (raw-path (replace-regexp-in-string
2074 "%" "\\%" (org-element-property :path link) nil t))
2075 ;; Ensure DESC really exists, or set it to nil.
2076 (desc (and (not (string= desc "")) desc))
2077 (imagep (org-export-inline-image-p
2078 link (plist-get info :latex-inline-image-rules)))
2079 (path (cond
2080 ((member type '("http" "https" "ftp" "mailto" "doi"))
2081 (concat type ":" raw-path))
2082 ((and (string= type "file") (file-name-absolute-p raw-path))
2083 (concat "file:" raw-path))
2084 (t raw-path))))
2085 (cond
2086 ;; Link type is handled by a special function.
2087 ((org-export-custom-protocol-maybe link desc 'latex))
2088 ;; Image file.
2089 (imagep (org-latex--inline-image link info))
2090 ;; Radio link: Transcode target's contents and use them as link's
2091 ;; description.
2092 ((string= type "radio")
2093 (let ((destination (org-export-resolve-radio-link link info)))
2094 (if (not destination) desc
2095 (format "\\hyperref[%s]{%s}"
2096 (org-export-get-reference destination info)
2097 desc))))
2098 ;; Links pointing to a headline: Find destination and build
2099 ;; appropriate referencing command.
2100 ((member type '("custom-id" "fuzzy" "id"))
2101 (let ((destination (if (string= type "fuzzy")
2102 (org-export-resolve-fuzzy-link link info)
2103 (org-export-resolve-id-link link info))))
2104 (case (org-element-type destination)
2105 ;; Id link points to an external file.
2106 (plain-text
2107 (if desc (format "\\href{%s}{%s}" destination desc)
2108 (format "\\url{%s}" destination)))
2109 ;; Fuzzy link points nowhere.
2110 ((nil)
2111 (format (plist-get info :latex-link-with-unknown-path-format)
2112 (or desc
2113 (org-export-data
2114 (org-element-property :raw-link link) info))))
2115 ;; LINK points to a headline. If headlines are numbered
2116 ;; and the link has no description, display headline's
2117 ;; number. Otherwise, display description or headline's
2118 ;; title.
2119 (headline
2120 (let ((label (org-latex--label destination info t)))
2121 (if (and (not desc)
2122 (org-export-numbered-headline-p destination info))
2123 (format "\\ref{%s}" label)
2124 (format "\\hyperref[%s]{%s}" label
2125 (or desc
2126 (org-export-data
2127 (org-element-property :title destination) info))))))
2128 ;; Fuzzy link points to a target. Do as above.
2129 (otherwise
2130 (let ((ref (org-latex--label destination info t)))
2131 (if (not desc) (format "\\ref{%s}" ref)
2132 (format "\\hyperref[%s]{%s}" ref desc)))))))
2133 ;; Coderef: replace link with the reference name or the
2134 ;; equivalent line number.
2135 ((string= type "coderef")
2136 (format (org-export-get-coderef-format path desc)
2137 (org-export-resolve-coderef path info)))
2138 ;; External link with a description part.
2139 ((and path desc) (format "\\href{%s}{%s}" path desc))
2140 ;; External link without a description part.
2141 (path (format "\\url{%s}" path))
2142 ;; No path, only description. Try to do something useful.
2143 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
2146 ;;;; Node Property
2148 (defun org-latex-node-property (node-property contents info)
2149 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2150 CONTENTS is nil. INFO is a plist holding contextual
2151 information."
2152 (format "%s:%s"
2153 (org-element-property :key node-property)
2154 (let ((value (org-element-property :value node-property)))
2155 (if value (concat " " value) ""))))
2158 ;;;; Paragraph
2160 (defun org-latex-paragraph (paragraph contents info)
2161 "Transcode a PARAGRAPH element from Org to LaTeX.
2162 CONTENTS is the contents of the paragraph, as a string. INFO is
2163 the plist used as a communication channel."
2164 contents)
2167 ;;;; Plain List
2169 (defun org-latex-plain-list (plain-list contents info)
2170 "Transcode a PLAIN-LIST element from Org to LaTeX.
2171 CONTENTS is the contents of the list. INFO is a plist holding
2172 contextual information."
2173 (let* ((type (org-element-property :type plain-list))
2174 (attr (org-export-read-attribute :attr_latex plain-list))
2175 (latex-type (let ((env (plist-get attr :environment)))
2176 (cond (env (format "%s" env))
2177 ((eq type 'ordered) "enumerate")
2178 ((eq type 'descriptive) "description")
2179 (t "itemize")))))
2180 (org-latex--wrap-label
2181 plain-list
2182 (format "\\begin{%s}%s\n%s\\end{%s}"
2183 latex-type
2184 (or (plist-get attr :options) "")
2185 contents
2186 latex-type)
2187 info)))
2190 ;;;; Plain Text
2192 (defun org-latex-plain-text (text info)
2193 "Transcode a TEXT string from Org to LaTeX.
2194 TEXT is the string to transcode. INFO is a plist holding
2195 contextual information."
2196 (let* ((specialp (plist-get info :with-special-strings))
2197 (output
2198 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2199 (let ((case-fold-search nil))
2200 (replace-regexp-in-string
2201 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2202 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2203 ;; However, if special strings are used, be careful not
2204 ;; to protect "\" in "\-" constructs.
2205 (replace-regexp-in-string
2206 (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\(?:[^-]\\|$\\)"))
2207 (lambda (m)
2208 (case (aref m 0)
2209 (?\\ "$\\\\backslash$")
2210 (?~ "\\\\textasciitilde{}")
2211 (?^ "\\\\^{}")
2212 (t "\\\\\\&")))
2213 text)))))
2214 ;; Activate smart quotes. Be sure to provide original TEXT string
2215 ;; since OUTPUT may have been modified.
2216 (when (plist-get info :with-smart-quotes)
2217 (setq output (org-export-activate-smart-quotes output :latex info text)))
2218 ;; Convert special strings.
2219 (when specialp
2220 (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
2221 ;; Handle break preservation if required.
2222 (when (plist-get info :preserve-breaks)
2223 (setq output (replace-regexp-in-string
2224 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
2225 ;; Return value.
2226 output))
2229 ;;;; Planning
2231 (defun org-latex-planning (planning contents info)
2232 "Transcode a PLANNING element from Org to LaTeX.
2233 CONTENTS is nil. INFO is a plist holding contextual
2234 information."
2235 (concat
2236 "\\noindent"
2237 (mapconcat
2238 'identity
2239 (delq nil
2240 (list
2241 (let ((closed (org-element-property :closed planning)))
2242 (when closed
2243 (concat
2244 (format "\\textbf{%s} " org-closed-string)
2245 (format (plist-get info :latex-inactive-timestamp-format)
2246 (org-timestamp-translate closed)))))
2247 (let ((deadline (org-element-property :deadline planning)))
2248 (when deadline
2249 (concat
2250 (format "\\textbf{%s} " org-deadline-string)
2251 (format (plist-get info :latex-active-timestamp-format)
2252 (org-timestamp-translate deadline)))))
2253 (let ((scheduled (org-element-property :scheduled planning)))
2254 (when scheduled
2255 (concat
2256 (format "\\textbf{%s} " org-scheduled-string)
2257 (format (plist-get info :latex-active-timestamp-format)
2258 (org-timestamp-translate scheduled)))))))
2259 " ")
2260 "\\\\"))
2263 ;;;; Property Drawer
2265 (defun org-latex-property-drawer (property-drawer contents info)
2266 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2267 CONTENTS holds the contents of the drawer. INFO is a plist
2268 holding contextual information."
2269 (and (org-string-nw-p contents)
2270 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2273 ;;;; Pseudo Element: LaTeX Matrices
2275 ;; `latex-matrices' elements have the following properties:
2276 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2277 ;; `math').
2279 (defun org-latex--wrap-latex-matrices (data info)
2280 "Merge contiguous tables with the same mode within a pseudo-element.
2281 DATA is a parse tree or a secondary string. INFO is a plist
2282 containing export options. Modify DATA by side-effect and return
2283 it."
2284 (org-element-map data 'table
2285 (lambda (table)
2286 (when (eq (org-element-property :type table) 'org)
2287 (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
2288 (plist-get info :latex-default-table-mode))))
2289 (when (and (member mode '("inline-math" "math"))
2290 ;; Do not wrap twice the same table.
2291 (not (eq (org-element-type
2292 (org-element-property :parent table))
2293 'latex-matrices)))
2294 (let* ((caption (and (not (string= mode "inline-math"))
2295 (org-element-property :caption table)))
2296 (matrices
2297 (list 'latex-matrices
2298 (list :caption caption
2299 :markup
2300 (cond ((string= mode "inline-math") 'inline)
2301 (caption 'equation)
2302 (t 'math)))))
2303 (previous table)
2304 (next (org-export-get-next-element table info)))
2305 (org-element-insert-before matrices table)
2306 ;; Swallow all contiguous tables sharing the same mode.
2307 (while (and
2308 (zerop (or (org-element-property :post-blank previous) 0))
2309 (setq next (org-export-get-next-element previous info))
2310 (eq (org-element-type next) 'table)
2311 (eq (org-element-property :type next) 'org)
2312 (string= (or (org-export-read-attribute
2313 :attr_latex next :mode)
2314 (plist-get info :latex-default-table-mode))
2315 mode))
2316 (org-element-extract-element previous)
2317 (org-element-adopt-elements matrices previous)
2318 (setq previous next))
2319 (org-element-put-property
2320 matrices :post-blank (org-element-property :post-blank previous))
2321 (org-element-extract-element previous)
2322 (org-element-adopt-elements matrices previous))))))
2323 info)
2324 data)
2326 (defun org-latex-matrices (matrices contents info)
2327 "Transcode a MATRICES element from Org to LaTeX.
2328 CONTENTS is a string. INFO is a plist used as a communication
2329 channel."
2330 (format (case (org-element-property :markup matrices)
2331 (inline "\\(%s\\)")
2332 (equation "\\begin{equation}\n%s\\end{equation}")
2333 (t "\\[\n%s\\]"))
2334 contents))
2336 (defun org-latex-matrices-tree-filter (tree backend info)
2337 (org-latex--wrap-latex-matrices tree info))
2339 ;;;; Pseudo Object: LaTeX Math Block
2341 ;; `latex-math-block' objects have the following property:
2342 ;; `:post-blank'.
2344 (defun org-latex--wrap-latex-math-block (data info)
2345 "Merge contiguous math objects in a pseudo-object container.
2346 DATA is a parse tree or a secondary string. INFO is a plist
2347 containing export options. Modify DATA by side-effect and return it."
2348 (let ((valid-object-p
2349 (function
2350 ;; Non-nil when OBJ can be added to the latex math block.
2351 (lambda (obj)
2352 (case (org-element-type obj)
2353 (entity (org-element-property :latex-math-p obj))
2354 (latex-fragment
2355 (let ((value (org-element-property :value obj)))
2356 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2357 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2358 ((subscript superscript) t))))))
2359 (org-element-map data '(entity latex-fragment subscript superscript)
2360 (lambda (object)
2361 ;; Skip objects already wrapped.
2362 (when (and (not (eq (org-element-type
2363 (org-element-property :parent object))
2364 'latex-math-block))
2365 (funcall valid-object-p object))
2366 (let ((math-block (list 'latex-math-block nil))
2367 (next-elements (org-export-get-next-element object info t))
2368 (last object))
2369 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2370 (org-element-insert-before math-block object)
2371 (org-element-extract-element object)
2372 (org-element-adopt-elements math-block object)
2373 (when (zerop (or (org-element-property :post-blank object) 0))
2374 ;; MATH-BLOCK swallows consecutive math objects.
2375 (catch 'exit
2376 (dolist (next next-elements)
2377 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2378 (org-element-extract-element next)
2379 (org-element-adopt-elements math-block next)
2380 ;; Eschew the case: \beta$x$ -> \(\betax\).
2381 (unless (memq (org-element-type next)
2382 '(subscript superscript))
2383 (org-element-put-property last :post-blank 1))
2384 (setq last next)
2385 (when (> (or (org-element-property :post-blank next) 0) 0)
2386 (throw 'exit nil))))))
2387 (org-element-put-property
2388 math-block :post-blank (org-element-property :post-blank last)))))
2389 info nil '(subscript superscript latex-math-block) t)
2390 ;; Return updated DATA.
2391 data))
2393 (defun org-latex-math-block-tree-filter (tree backend info)
2394 (org-latex--wrap-latex-math-block tree info))
2396 (defun org-latex-math-block-options-filter (info backend)
2397 (dolist (prop '(:author :date :title) info)
2398 (plist-put info prop
2399 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2401 (defun org-latex-math-block (math-block contents info)
2402 "Transcode a MATH-BLOCK object from Org to LaTeX.
2403 CONTENTS is a string. INFO is a plist used as a communication
2404 channel."
2405 (when (org-string-nw-p contents)
2406 (format "\\(%s\\)" (org-trim contents))))
2408 ;;;; Quote Block
2410 (defun org-latex-quote-block (quote-block contents info)
2411 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2412 CONTENTS holds the contents of the block. INFO is a plist
2413 holding contextual information."
2414 (org-latex--wrap-label
2415 quote-block (format "\\begin{quote}\n%s\\end{quote}" contents) info))
2418 ;;;; Radio Target
2420 (defun org-latex-radio-target (radio-target text info)
2421 "Transcode a RADIO-TARGET object from Org to LaTeX.
2422 TEXT is the text of the target. INFO is a plist holding
2423 contextual information."
2424 (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
2427 ;;;; Section
2429 (defun org-latex-section (section contents info)
2430 "Transcode a SECTION element from Org to LaTeX.
2431 CONTENTS holds the contents of the section. INFO is a plist
2432 holding contextual information."
2433 contents)
2436 ;;;; Special Block
2438 (defun org-latex-special-block (special-block contents info)
2439 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2440 CONTENTS holds the contents of the block. INFO is a plist
2441 holding contextual information."
2442 (let ((type (org-element-property :type special-block))
2443 (opt (org-export-read-attribute :attr_latex special-block :options))
2444 (caption (org-latex--caption/label-string special-block info))
2445 (caption-above-p (org-latex--caption-above-p special-block info)))
2446 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2447 (and caption-above-p caption)
2448 contents
2449 (and (not caption-above-p) caption)
2450 (format "\\end{%s}" type))))
2453 ;;;; Src Block
2455 (defun org-latex-src-block (src-block contents info)
2456 "Transcode a SRC-BLOCK element from Org to LaTeX.
2457 CONTENTS holds the contents of the item. INFO is a plist holding
2458 contextual information."
2459 (when (org-string-nw-p (org-element-property :value src-block))
2460 (let* ((lang (org-element-property :language src-block))
2461 (caption (org-element-property :caption src-block))
2462 (caption-above-p (org-latex--caption-above-p src-block info))
2463 (label (org-element-property :name src-block))
2464 (custom-env (and lang
2465 (cadr (assq (intern lang)
2466 org-latex-custom-lang-environments))))
2467 (num-start (case (org-element-property :number-lines src-block)
2468 (continued (org-export-get-loc src-block info))
2469 (new 0)))
2470 (retain-labels (org-element-property :retain-labels src-block))
2471 (attributes (org-export-read-attribute :attr_latex src-block))
2472 (float (plist-get attributes :float))
2473 (listings (plist-get info :latex-listings)))
2474 (cond
2475 ;; Case 1. No source fontification.
2476 ((not listings)
2477 (let* ((caption-str (org-latex--caption/label-string src-block info))
2478 (float-env
2479 (cond ((and (not float) (plist-member attributes :float)) "%s")
2480 ((string= "multicolumn" float)
2481 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2482 (plist-get info :latex-default-figure-position)
2483 (if caption-above-p caption-str "")
2484 (if caption-above-p "" caption-str)))
2485 ((or caption float)
2486 (format "\\begin{figure}[H]\n%%s\n%s\\end{figure}"
2487 caption-str))
2488 (t "%s"))))
2489 (format
2490 float-env
2491 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2492 (org-export-format-code-default src-block info))))))
2493 ;; Case 2. Custom environment.
2494 (custom-env
2495 (let ((caption-str (org-latex--caption/label-string src-block info)))
2496 (format "\\begin{%s}\n%s\\end{%s}\n"
2497 custom-env
2498 (concat (and caption-above-p caption-str)
2499 (org-export-format-code-default src-block info)
2500 (and (not caption-above-p) caption-str))
2501 custom-env)))
2502 ;; Case 3. Use minted package.
2503 ((eq listings 'minted)
2504 (let* ((caption-str (org-latex--caption/label-string src-block info))
2505 (float-env
2506 (cond
2507 ((and (not float) (plist-member attributes :float) caption)
2508 (let ((caption
2509 (replace-regexp-in-string
2510 "\\\\caption" "\\captionof{listing}" caption-str
2511 t t)))
2512 (concat (and caption-above-p caption)
2513 "%%s"
2514 (and (not caption-above-p) (concat "\n" caption)))))
2515 ((and (not float) (plist-member attributes :float)) "%s")
2516 ((string= "multicolumn" float)
2517 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2518 (if caption-above-p caption-str "")
2519 (if caption-above-p "" caption-str)))
2520 ((or caption float)
2521 (format "\\begin{listing}[H]\n%s%%s\n%s\\end{listing}"
2522 (if caption-above-p caption-str "")
2523 (if caption-above-p "" caption-str)))
2524 (t "%s")))
2525 (options (plist-get info :latex-minted-options))
2526 (body
2527 (format
2528 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2529 ;; Options.
2530 (concat
2531 (org-latex--make-option-string
2532 (if (or (not num-start) (assoc "linenos" options))
2533 options
2534 (append
2535 `(("linenos")
2536 ("firstnumber" ,(number-to-string (1+ num-start))))
2537 options)))
2538 (let ((local-options (plist-get attributes :options)))
2539 (and local-options (concat "," local-options))))
2540 ;; Language.
2541 (or (cadr (assq (intern lang)
2542 (plist-get info :latex-minted-langs)))
2543 (downcase lang))
2544 ;; Source code.
2545 (let* ((code-info (org-export-unravel-code src-block))
2546 (max-width
2547 (apply 'max
2548 (mapcar 'length
2549 (org-split-string (car code-info)
2550 "\n")))))
2551 (org-export-format-code
2552 (car code-info)
2553 (lambda (loc num ref)
2554 (concat
2556 (when ref
2557 ;; Ensure references are flushed to the right,
2558 ;; separated with 6 spaces from the widest line
2559 ;; of code.
2560 (concat (make-string (+ (- max-width (length loc)) 6)
2561 ?\s)
2562 (format "(%s)" ref)))))
2563 nil (and retain-labels (cdr code-info)))))))
2564 ;; Return value.
2565 (format float-env body)))
2566 ;; Case 4. Use listings package.
2568 (let ((lst-lang
2569 (or (cadr (assq (intern lang)
2570 (plist-get info :latex-listings-langs)))
2571 lang))
2572 (caption-str
2573 (when caption
2574 (let ((main (org-export-get-caption src-block))
2575 (secondary (org-export-get-caption src-block t)))
2576 (if (not secondary)
2577 (format "{%s}" (org-export-data main info))
2578 (format "{[%s]%s}"
2579 (org-export-data secondary info)
2580 (org-export-data main info))))))
2581 (lst-opt (plist-get info :latex-listings-options)))
2582 (concat
2583 ;; Options.
2584 (format
2585 "\\lstset{%s}\n"
2586 (concat
2587 (org-latex--make-option-string
2588 (append
2589 lst-opt
2590 (cond
2591 ((and (not float) (plist-member attributes :float)) nil)
2592 ((string= "multicolumn" float) '(("float" "*")))
2593 ((and float (not (assoc "float" lst-opt)))
2594 `(("float" ,(plist-get info :latex-default-figure-position)))))
2595 `(("language" ,lst-lang))
2596 (if label `(("label" ,label)) '(("label" " ")))
2597 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2598 `(("captionpos" ,(if caption-above-p "t" "b")))
2599 (cond ((assoc "numbers" lst-opt) nil)
2600 ((not num-start) '(("numbers" "none")))
2601 ((zerop num-start) '(("numbers" "left")))
2602 (t `(("firstnumber" ,(number-to-string (1+ num-start)))
2603 ("numbers" "left"))))))
2604 (let ((local-options (plist-get attributes :options)))
2605 (and local-options (concat "," local-options)))))
2606 ;; Source code.
2607 (format
2608 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2609 (let* ((code-info (org-export-unravel-code src-block))
2610 (max-width
2611 (apply 'max
2612 (mapcar 'length
2613 (org-split-string (car code-info) "\n")))))
2614 (org-export-format-code
2615 (car code-info)
2616 (lambda (loc num ref)
2617 (concat
2619 (when ref
2620 ;; Ensure references are flushed to the right,
2621 ;; separated with 6 spaces from the widest line of
2622 ;; code
2623 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2624 (format "(%s)" ref)))))
2625 nil (and retain-labels (cdr code-info))))))))))))
2628 ;;;; Statistics Cookie
2630 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2631 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2632 CONTENTS is nil. INFO is a plist holding contextual information."
2633 (replace-regexp-in-string
2634 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2637 ;;;; Strike-Through
2639 (defun org-latex-strike-through (strike-through contents info)
2640 "Transcode STRIKE-THROUGH from Org to LaTeX.
2641 CONTENTS is the text with strike-through markup. INFO is a plist
2642 holding contextual information."
2643 (org-latex--text-markup contents 'strike-through info))
2646 ;;;; Subscript
2648 (defun org-latex--script-size (object info)
2649 "Transcode a subscript or superscript object.
2650 OBJECT is an Org object. INFO is a plist used as a communication
2651 channel."
2652 (let ((type (org-element-type object))
2653 (output ""))
2654 (org-element-map (org-element-contents object)
2655 (cons 'plain-text org-element-all-objects)
2656 (lambda (obj)
2657 (case (org-element-type obj)
2658 ((entity latex-fragment)
2659 (let ((data (org-trim (org-export-data obj info))))
2660 (string-match
2661 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2662 data)
2663 (setq output
2664 (concat output
2665 (match-string 1 data)
2666 (let ((blank (org-element-property :post-blank obj)))
2667 (and blank (> blank 0) "\\ "))))))
2668 (plain-text
2669 (setq output
2670 (format "%s\\text{%s}" output (org-export-data obj info))))
2671 (otherwise
2672 (setq output
2673 (concat output
2674 (org-export-data obj info)
2675 (let ((blank (org-element-property :post-blank obj)))
2676 (and blank (> blank 0) "\\ ")))))))
2677 info nil org-element-recursive-objects)
2678 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2679 ;; character.
2680 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2681 (and (> (length output) 1) "{")
2682 output
2683 (and (> (length output) 1) "}"))))
2685 (defun org-latex-subscript (subscript contents info)
2686 "Transcode a SUBSCRIPT object from Org to LaTeX.
2687 CONTENTS is the contents of the object. INFO is a plist holding
2688 contextual information."
2689 (org-latex--script-size subscript info))
2692 ;;;; Superscript
2694 (defun org-latex-superscript (superscript contents info)
2695 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2696 CONTENTS is the contents of the object. INFO is a plist holding
2697 contextual information."
2698 (org-latex--script-size superscript info))
2701 ;;;; Table
2703 ;; `org-latex-table' is the entry point for table transcoding. It
2704 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2705 ;; delegates the job to either `org-latex--table.el-table',
2706 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2707 ;; depending of the type of the table and the mode requested.
2709 ;; `org-latex--align-string' is a subroutine used to build alignment
2710 ;; string for Org tables.
2712 (defun org-latex-table (table contents info)
2713 "Transcode a TABLE element from Org to LaTeX.
2714 CONTENTS is the contents of the table. INFO is a plist holding
2715 contextual information."
2716 (if (eq (org-element-property :type table) 'table.el)
2717 ;; "table.el" table. Convert it using appropriate tools.
2718 (org-latex--table.el-table table info)
2719 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2720 (plist-get info :latex-default-table-mode))))
2721 (cond
2722 ;; Case 1: Verbatim table.
2723 ((string= type "verbatim")
2724 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2725 ;; Re-create table, without affiliated keywords.
2726 (org-trim (org-element-interpret-data
2727 `(table nil ,@(org-element-contents table))))))
2728 ;; Case 2: Matrix.
2729 ((or (string= type "math") (string= type "inline-math"))
2730 (org-latex--math-table table info))
2731 ;; Case 3: Standard table.
2732 (t (concat (org-latex--org-table table contents info)
2733 ;; When there are footnote references within the
2734 ;; table, insert their definition just after it.
2735 (org-latex--delayed-footnotes-definitions table info)))))))
2737 (defun org-latex--align-string (table info)
2738 "Return an appropriate LaTeX alignment string.
2739 TABLE is the considered table. INFO is a plist used as
2740 a communication channel."
2741 (or (org-export-read-attribute :attr_latex table :align)
2742 (let (align)
2743 ;; Extract column groups and alignment from first (non-rule)
2744 ;; row.
2745 (org-element-map
2746 (org-element-map table 'table-row
2747 (lambda (row)
2748 (and (eq (org-element-property :type row) 'standard) row))
2749 info 'first-match)
2750 'table-cell
2751 (lambda (cell)
2752 (let ((borders (org-export-table-cell-borders cell info)))
2753 ;; Check left border for the first cell only.
2754 (when (and (memq 'left borders) (not align))
2755 (push "|" align))
2756 (push (case (org-export-table-cell-alignment cell info)
2757 (left "l")
2758 (right "r")
2759 (center "c"))
2760 align)
2761 (when (memq 'right borders) (push "|" align))))
2762 info)
2763 (apply 'concat (nreverse align)))))
2765 (defun org-latex--org-table (table contents info)
2766 "Return appropriate LaTeX code for an Org table.
2768 TABLE is the table type element to transcode. CONTENTS is its
2769 contents, as a string. INFO is a plist used as a communication
2770 channel.
2772 This function assumes TABLE has `org' as its `:type' property and
2773 `table' as its `:mode' attribute."
2774 (let* ((caption (org-latex--caption/label-string table info))
2775 (attr (org-export-read-attribute :attr_latex table))
2776 ;; Determine alignment string.
2777 (alignment (org-latex--align-string table info))
2778 ;; Determine environment for the table: longtable, tabular...
2779 (table-env (or (plist-get attr :environment)
2780 (plist-get info :latex-default-table-environment)))
2781 ;; If table is a float, determine environment: table, table*
2782 ;; or sidewaystable.
2783 (float-env (unless (member table-env '("longtable" "longtabu"))
2784 (let ((float (plist-get attr :float)))
2785 (cond
2786 ((and (not float) (plist-member attr :float)) nil)
2787 ((or (string= float "sidewaystable")
2788 (string= float "sideways")) "sidewaystable")
2789 ((string= float "multicolumn") "table*")
2790 ((or float
2791 (org-element-property :caption table)
2792 (org-string-nw-p (plist-get attr :caption)))
2793 "table")))))
2794 ;; Extract others display options.
2795 (fontsize (let ((font (plist-get attr :font)))
2796 (and font (concat font "\n"))))
2797 ;; "tabular" environment doesn't allow to define a width.
2798 (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
2799 (spreadp (plist-get attr :spread))
2800 (placement
2801 (or (plist-get attr :placement)
2802 (format "[%s]" (plist-get info :latex-default-figure-position))))
2803 (centerp (if (plist-member attr :center) (plist-get attr :center)
2804 (plist-get info :latex-tables-centered)))
2805 (caption-above-p (org-latex--caption-above-p table info)))
2806 ;; Prepare the final format string for the table.
2807 (cond
2808 ;; Longtable.
2809 ((equal "longtable" table-env)
2810 (concat (and fontsize (concat "{" fontsize))
2811 (format "\\begin{longtable}{%s}\n" alignment)
2812 (and caption-above-p
2813 (org-string-nw-p caption)
2814 (concat caption "\\\\\n"))
2815 contents
2816 (and (not caption-above-p)
2817 (org-string-nw-p caption)
2818 (concat caption "\\\\\n"))
2819 "\\end{longtable}\n"
2820 (and fontsize "}")))
2821 ;; Longtabu
2822 ((equal "longtabu" table-env)
2823 (concat (and fontsize (concat "{" fontsize))
2824 (format "\\begin{longtabu}%s{%s}\n"
2825 (if width
2826 (format " %s %s "
2827 (if spreadp "spread" "to") width) "")
2828 alignment)
2829 (and caption-above-p
2830 (org-string-nw-p caption)
2831 (concat caption "\\\\\n"))
2832 contents
2833 (and (not caption-above-p)
2834 (org-string-nw-p caption)
2835 (concat caption "\\\\\n"))
2836 "\\end{longtabu}\n"
2837 (and fontsize "}")))
2838 ;; Others.
2839 (t (concat (cond
2840 (float-env
2841 (concat (format "\\begin{%s}%s\n" float-env placement)
2842 (if caption-above-p caption "")
2843 (when centerp "\\centering\n")
2844 fontsize))
2845 (centerp (concat "\\begin{center}\n" fontsize))
2846 (fontsize (concat "{" fontsize)))
2847 (cond ((equal "tabu" table-env)
2848 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2849 (if width (format
2850 (if spreadp " spread %s " " to %s ")
2851 width) "")
2852 alignment
2853 contents))
2854 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2855 table-env
2856 (if width (format "{%s}" width) "")
2857 alignment
2858 contents
2859 table-env)))
2860 (cond
2861 (float-env
2862 (concat (if caption-above-p "" caption)
2863 (format "\n\\end{%s}" float-env)))
2864 (centerp "\n\\end{center}")
2865 (fontsize "}")))))))
2867 (defun org-latex--table.el-table (table info)
2868 "Return appropriate LaTeX code for a table.el table.
2870 TABLE is the table type element to transcode. INFO is a plist
2871 used as a communication channel.
2873 This function assumes TABLE has `table.el' as its `:type'
2874 property."
2875 (require 'table)
2876 ;; Ensure "*org-export-table*" buffer is empty.
2877 (with-current-buffer (get-buffer-create "*org-export-table*")
2878 (erase-buffer))
2879 (let ((output (with-temp-buffer
2880 (insert (org-element-property :value table))
2881 (goto-char 1)
2882 (re-search-forward "^[ \t]*|[^|]" nil t)
2883 (table-generate-source 'latex "*org-export-table*")
2884 (with-current-buffer "*org-export-table*"
2885 (org-trim (buffer-string))))))
2886 (kill-buffer (get-buffer "*org-export-table*"))
2887 ;; Remove left out comments.
2888 (while (string-match "^%.*\n" output)
2889 (setq output (replace-match "" t t output)))
2890 (let ((attr (org-export-read-attribute :attr_latex table)))
2891 (when (plist-get attr :rmlines)
2892 ;; When the "rmlines" attribute is provided, remove all hlines
2893 ;; but the the one separating heading from the table body.
2894 (let ((n 0) (pos 0))
2895 (while (and (< (length output) pos)
2896 (setq pos (string-match "^\\\\hline\n?" output pos)))
2897 (incf n)
2898 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2899 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2900 (plist-get info :latex-tables-centered))))
2901 (if (not centerp) output
2902 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2904 (defun org-latex--math-table (table info)
2905 "Return appropriate LaTeX code for a matrix.
2907 TABLE is the table type element to transcode. INFO is a plist
2908 used as a communication channel.
2910 This function assumes TABLE has `org' as its `:type' property and
2911 `inline-math' or `math' as its `:mode' attribute."
2912 (let* ((attr (org-export-read-attribute :attr_latex table))
2913 (env (or (plist-get attr :environment)
2914 (plist-get info :latex-default-table-environment)))
2915 (contents
2916 (mapconcat
2917 (lambda (row)
2918 ;; Ignore horizontal rules.
2919 (when (eq (org-element-property :type row) 'standard)
2920 ;; Return each cell unmodified.
2921 (concat
2922 (mapconcat
2923 (lambda (cell)
2924 (substring (org-element-interpret-data cell) 0 -1))
2925 (org-element-map row 'table-cell #'identity info) "&")
2926 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2927 "\n")))
2928 (org-element-map table 'table-row #'identity info) "")))
2929 (concat
2930 ;; Prefix.
2931 (plist-get attr :math-prefix)
2932 ;; Environment. Also treat special cases.
2933 (cond ((member env '("array" "tabular"))
2934 (let ((align (make-string
2935 (cdr (org-export-table-dimensions table info)) ?c)))
2936 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
2937 ((assoc env org-latex-table-matrix-macros)
2938 (format "\\%s%s{\n%s}"
2940 (or (plist-get attr :math-arguments) "")
2941 contents))
2942 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2943 ;; Suffix.
2944 (plist-get attr :math-suffix))))
2947 ;;;; Table Cell
2949 (defun org-latex-table-cell (table-cell contents info)
2950 "Transcode a TABLE-CELL element from Org to LaTeX.
2951 CONTENTS is the cell contents. INFO is a plist used as
2952 a communication channel."
2953 (concat
2954 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
2955 (if (and contents
2956 scientific-format
2957 (string-match orgtbl-exp-regexp contents))
2958 ;; Use appropriate format string for scientific
2959 ;; notation.
2960 (format scientific-format
2961 (match-string 1 contents)
2962 (match-string 2 contents))
2963 contents))
2964 (when (org-export-get-next-element table-cell info) " & ")))
2967 ;;;; Table Row
2969 (defun org-latex-table-row (table-row contents info)
2970 "Transcode a TABLE-ROW element from Org to LaTeX.
2971 CONTENTS is the contents of the row. INFO is a plist used as
2972 a communication channel."
2973 (let* ((attr (org-export-read-attribute :attr_latex
2974 (org-export-get-parent table-row)))
2975 (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
2976 (plist-get info :latex-tables-booktabs)))
2977 (longtablep
2978 (member (or (plist-get attr :environment)
2979 (plist-get info :latex-default-table-environment))
2980 '("longtable" "longtabu"))))
2981 (if (eq (org-element-property :type table-row) 'rule)
2982 (cond
2983 ((not booktabsp) "\\hline")
2984 ((not (org-export-get-previous-element table-row info)) "\\toprule")
2985 ((not (org-export-get-next-element table-row info)) "\\bottomrule")
2986 ((and longtablep
2987 (org-export-table-row-ends-header-p
2988 (org-export-get-previous-element table-row info) info))
2990 (t "\\midrule"))
2991 (concat
2992 ;; When BOOKTABS are activated enforce top-rule even when no
2993 ;; hline was specifically marked.
2994 (and booktabsp (not (org-export-get-previous-element table-row info))
2995 "\\toprule\n")
2996 contents "\\\\\n"
2997 (cond
2998 ;; Special case for long tables. Define header and footers.
2999 ((and longtablep (org-export-table-row-ends-header-p table-row info))
3000 (let ((columns (cdr (org-export-table-dimensions
3001 (org-export-get-parent-table table-row) info))))
3002 (format "%s
3003 \\endfirsthead
3004 \\multicolumn{%d}{l}{%s} \\\\
3006 %s \\\\\n
3008 \\endhead
3009 %s\\multicolumn{%d}{r}{%s} \\\\
3010 \\endfoot
3011 \\endlastfoot"
3012 (if booktabsp "\\midrule" "\\hline")
3013 columns
3014 (org-latex--translate "Continued from previous page" info)
3015 (cond
3016 ((not (org-export-table-row-starts-header-p table-row info))
3018 (booktabsp "\\toprule\n")
3019 (t "\\hline\n"))
3020 contents
3021 (if booktabsp "\\midrule" "\\hline")
3022 (if booktabsp "\\midrule" "\\hline")
3023 columns
3024 (org-latex--translate "Continued on next page" info))))
3025 ;; When BOOKTABS are activated enforce bottom rule even when
3026 ;; no hline was specifically marked.
3027 ((and booktabsp (not (org-export-get-next-element table-row info)))
3028 "\\bottomrule"))))))
3031 ;;;; Target
3033 (defun org-latex-target (target contents info)
3034 "Transcode a TARGET object from Org to LaTeX.
3035 CONTENTS is nil. INFO is a plist holding contextual
3036 information."
3037 (format "\\label{%s}" (org-latex--label target info)))
3040 ;;;; Timestamp
3042 (defun org-latex-timestamp (timestamp contents info)
3043 "Transcode a TIMESTAMP object from Org to LaTeX.
3044 CONTENTS is nil. INFO is a plist holding contextual
3045 information."
3046 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
3047 (format
3048 (plist-get info
3049 (case (org-element-property :type timestamp)
3050 ((active active-range) :latex-active-timestamp-format)
3051 ((inactive inactive-range) :latex-inactive-timestamp-format)
3052 (otherwise :latex-diary-timestamp-format)))
3053 value)))
3056 ;;;; Underline
3058 (defun org-latex-underline (underline contents info)
3059 "Transcode UNDERLINE from Org to LaTeX.
3060 CONTENTS is the text with underline markup. INFO is a plist
3061 holding contextual information."
3062 (org-latex--text-markup contents 'underline info))
3065 ;;;; Verbatim
3067 (defun org-latex-verbatim (verbatim contents info)
3068 "Transcode a VERBATIM object from Org to LaTeX.
3069 CONTENTS is nil. INFO is a plist used as a communication
3070 channel."
3071 (org-latex--text-markup
3072 (org-element-property :value verbatim) 'verbatim info))
3075 ;;;; Verse Block
3077 (defun org-latex-verse-block (verse-block contents info)
3078 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3079 CONTENTS is verse block contents. INFO is a plist holding
3080 contextual information."
3081 (org-latex--wrap-label
3082 verse-block
3083 ;; In a verse environment, add a line break to each newline
3084 ;; character and change each white space at beginning of a line
3085 ;; into a space of 1 em. Also change each blank line with
3086 ;; a vertical space of 1 em.
3087 (format "\\begin{verse}\n%s\\end{verse}"
3088 (replace-regexp-in-string
3089 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
3090 (replace-regexp-in-string
3091 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3092 (replace-regexp-in-string
3093 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3094 contents nil t) nil t) nil t))
3095 info))
3099 ;;; End-user functions
3101 ;;;###autoload
3102 (defun org-latex-export-as-latex
3103 (&optional async subtreep visible-only body-only ext-plist)
3104 "Export current buffer as a LaTeX buffer.
3106 If narrowing is active in the current buffer, only export its
3107 narrowed part.
3109 If a region is active, export that region.
3111 A non-nil optional argument ASYNC means the process should happen
3112 asynchronously. The resulting buffer should be accessible
3113 through the `org-export-stack' interface.
3115 When optional argument SUBTREEP is non-nil, export the sub-tree
3116 at point, extracting information from the headline properties
3117 first.
3119 When optional argument VISIBLE-ONLY is non-nil, don't export
3120 contents of hidden elements.
3122 When optional argument BODY-ONLY is non-nil, only write code
3123 between \"\\begin{document}\" and \"\\end{document}\".
3125 EXT-PLIST, when provided, is a property list with external
3126 parameters overriding Org default settings, but still inferior to
3127 file-local settings.
3129 Export is done in a buffer named \"*Org LATEX Export*\", which
3130 will be displayed when `org-export-show-temporary-export-buffer'
3131 is non-nil."
3132 (interactive)
3133 (org-export-to-buffer 'latex "*Org LATEX Export*"
3134 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
3136 ;;;###autoload
3137 (defun org-latex-convert-region-to-latex ()
3138 "Assume the current region has org-mode syntax, and convert it to LaTeX.
3139 This can be used in any buffer. For example, you can write an
3140 itemized list in org-mode syntax in an LaTeX buffer and use this
3141 command to convert it."
3142 (interactive)
3143 (org-export-replace-region-by 'latex))
3145 ;;;###autoload
3146 (defun org-latex-export-to-latex
3147 (&optional async subtreep visible-only body-only ext-plist)
3148 "Export current buffer to a LaTeX file.
3150 If narrowing is active in the current buffer, only export its
3151 narrowed part.
3153 If a region is active, export that region.
3155 A non-nil optional argument ASYNC means the process should happen
3156 asynchronously. The resulting file should be accessible through
3157 the `org-export-stack' interface.
3159 When optional argument SUBTREEP is non-nil, export the sub-tree
3160 at point, extracting information from the headline properties
3161 first.
3163 When optional argument VISIBLE-ONLY is non-nil, don't export
3164 contents of hidden elements.
3166 When optional argument BODY-ONLY is non-nil, only write code
3167 between \"\\begin{document}\" and \"\\end{document}\".
3169 EXT-PLIST, when provided, is a property list with external
3170 parameters overriding Org default settings, but still inferior to
3171 file-local settings."
3172 (interactive)
3173 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3174 (org-export-to-file 'latex outfile
3175 async subtreep visible-only body-only ext-plist)))
3177 ;;;###autoload
3178 (defun org-latex-export-to-pdf
3179 (&optional async subtreep visible-only body-only ext-plist)
3180 "Export current buffer to LaTeX then process through to PDF.
3182 If narrowing is active in the current buffer, only export its
3183 narrowed part.
3185 If a region is active, export that region.
3187 A non-nil optional argument ASYNC means the process should happen
3188 asynchronously. The resulting file should be accessible through
3189 the `org-export-stack' interface.
3191 When optional argument SUBTREEP is non-nil, export the sub-tree
3192 at point, extracting information from the headline properties
3193 first.
3195 When optional argument VISIBLE-ONLY is non-nil, don't export
3196 contents of hidden elements.
3198 When optional argument BODY-ONLY is non-nil, only write code
3199 between \"\\begin{document}\" and \"\\end{document}\".
3201 EXT-PLIST, when provided, is a property list with external
3202 parameters overriding Org default settings, but still inferior to
3203 file-local settings.
3205 Return PDF file's name."
3206 (interactive)
3207 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3208 (org-export-to-file 'latex outfile
3209 async subtreep visible-only body-only ext-plist
3210 (lambda (file) (org-latex-compile file)))))
3212 (defun org-latex-compile (texfile &optional snippet)
3213 "Compile a TeX file.
3215 TEXFILE is the name of the file being compiled. Processing is
3216 done through the command specified in `org-latex-pdf-process'.
3218 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3219 file used to preview a LaTeX snippet. In this case, do not
3220 create a log buffer and do not bother removing log files.
3222 Return PDF file name or an error if it couldn't be produced."
3223 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3224 (full-name (file-truename texfile))
3225 (out-dir (file-name-directory texfile))
3226 ;; Properly set working directory for compilation.
3227 (default-directory (if (file-name-absolute-p texfile)
3228 (file-name-directory full-name)
3229 default-directory))
3230 (time (current-time))
3231 warnings)
3232 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
3233 (save-window-excursion
3234 (cond
3235 ;; A function is provided: Apply it.
3236 ((functionp org-latex-pdf-process)
3237 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3238 ;; A list is provided: Replace %b, %f and %o with appropriate
3239 ;; values in each command before applying it. Output is
3240 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3241 ((consp org-latex-pdf-process)
3242 (let ((outbuf (and (not snippet)
3243 (get-buffer-create "*Org PDF LaTeX Output*"))))
3244 (dolist (command org-latex-pdf-process)
3245 (shell-command
3246 (replace-regexp-in-string
3247 "%b" (shell-quote-argument base-name)
3248 (replace-regexp-in-string
3249 "%f" (shell-quote-argument full-name)
3250 (replace-regexp-in-string
3251 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
3252 outbuf))
3253 ;; Collect standard errors from output buffer.
3254 (setq warnings (and (not snippet)
3255 (org-latex--collect-warnings outbuf)))))
3256 (t (error "No valid command to process to PDF")))
3257 (let ((pdffile (concat out-dir base-name ".pdf")))
3258 ;; Check for process failure. Provide collected errors if
3259 ;; possible.
3260 (if (or (not (file-exists-p pdffile))
3261 (time-less-p (nth 5 (file-attributes pdffile)) time))
3262 (error (format "PDF file %s wasn't produced" pdffile))
3263 ;; Else remove log files, when specified, and signal end of
3264 ;; process to user, along with any error encountered.
3265 (unless snippet
3266 (when org-latex-remove-logfiles
3267 (dolist (file (directory-files
3268 out-dir t
3269 (concat (regexp-quote base-name)
3270 "\\(?:\\.[0-9]+\\)?"
3271 "\\."
3272 (regexp-opt org-latex-logfiles-extensions))))
3273 (delete-file file)))
3274 (message (concat "PDF file produced"
3275 (cond
3276 ((eq warnings 'error) " with errors.")
3277 (warnings (concat " with warnings: " warnings))
3278 (t "."))))))
3279 ;; Return output file name.
3280 pdffile))))
3282 (defun org-latex--collect-warnings (buffer)
3283 "Collect some warnings from \"pdflatex\" command output.
3284 BUFFER is the buffer containing output. Return collected
3285 warnings types as a string, `error' if a LaTeX error was
3286 encountered or nil if there was none."
3287 (with-current-buffer buffer
3288 (save-excursion
3289 (goto-char (point-max))
3290 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3291 (if (re-search-forward "^!" nil t) 'error
3292 (let ((case-fold-search t)
3293 (warnings ""))
3294 (dolist (warning org-latex-known-warnings)
3295 (when (save-excursion (re-search-forward (car warning) nil t))
3296 (setq warnings (concat warnings " " (cdr warning)))))
3297 (org-string-nw-p (org-trim warnings))))))))
3299 ;;;###autoload
3300 (defun org-latex-publish-to-latex (plist filename pub-dir)
3301 "Publish an Org file to LaTeX.
3303 FILENAME is the filename of the Org file to be published. PLIST
3304 is the property list for the given project. PUB-DIR is the
3305 publishing directory.
3307 Return output file name."
3308 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3310 ;;;###autoload
3311 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3312 "Publish an Org file to PDF (via LaTeX).
3314 FILENAME is the filename of the Org file to be published. PLIST
3315 is the property list for the given project. PUB-DIR is the
3316 publishing directory.
3318 Return output file name."
3319 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3320 ;; in working directory and then moved to publishing directory.
3321 (org-publish-attachment
3322 plist
3323 (org-latex-compile
3324 (org-publish-org-to
3325 'latex filename ".tex" plist (file-name-directory filename)))
3326 pub-dir))
3329 (provide 'ox-latex)
3331 ;; Local variables:
3332 ;; generated-autoload-file: "org-loaddefs.el"
3333 ;; End:
3335 ;;; ox-latex.el ends here