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