ox-latex: Do not use \verb command anymore
[org-mode/org-tableheadings.git] / lisp / ox-latex.el
blob46128fc549f2df957c10b212f721bd7d89ff2f90
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 (case (plist-get info :latex-listings)
1733 ;; Do not use a special package: transcode it verbatim.
1734 ((nil) (format "\\texttt{%s}" (org-latex--protect-text code)))
1735 ;; Use minted package.
1736 (minted
1737 (let* ((org-lang (org-element-property :language inline-src-block))
1738 (mint-lang (or (cadr (assq (intern org-lang)
1739 (plist-get info :latex-minted-langs)))
1740 (downcase org-lang)))
1741 (options (org-latex--make-option-string
1742 (plist-get info :latex-minted-options))))
1743 (concat (format "\\mint%s{%s}"
1744 (if (string= options "") "" (format "[%s]" options))
1745 mint-lang)
1746 separator code separator)))
1747 ;; Use listings package.
1748 (otherwise
1749 ;; Maybe translate language's name.
1750 (let* ((org-lang (org-element-property :language inline-src-block))
1751 (lst-lang (or (cadr (assq (intern org-lang)
1752 (plist-get info :latex-listings-langs)))
1753 org-lang))
1754 (options (org-latex--make-option-string
1755 (append (plist-get info :latex-listings-options)
1756 `(("language" ,lst-lang))))))
1757 (concat (format "\\lstinline[%s]" options)
1758 separator code separator))))))
1761 ;;;; Inlinetask
1763 (defun org-latex-inlinetask (inlinetask contents info)
1764 "Transcode an INLINETASK element from Org to LaTeX.
1765 CONTENTS holds the contents of the block. INFO is a plist
1766 holding contextual information."
1767 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1768 (todo (and (plist-get info :with-todo-keywords)
1769 (let ((todo (org-element-property :todo-keyword inlinetask)))
1770 (and todo (org-export-data todo info)))))
1771 (todo-type (org-element-property :todo-type inlinetask))
1772 (tags (and (plist-get info :with-tags)
1773 (org-export-get-tags inlinetask info)))
1774 (priority (and (plist-get info :with-priority)
1775 (org-element-property :priority inlinetask)))
1776 (contents (concat (org-latex--label inlinetask info) contents)))
1777 (funcall (plist-get info :latex-format-inlinetask-function)
1778 todo todo-type priority title tags contents info)))
1780 (defun org-latex-format-inlinetask-default-function
1781 (todo todo-type priority title tags contents info)
1782 "Default format function for a inlinetasks.
1783 See `org-latex-format-inlinetask-function' for details."
1784 (let ((full-title
1785 (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1786 (when priority (format "\\framebox{\\#%c} " priority))
1787 title
1788 (when tags
1789 (format "\\hfill{}\\textsc{:%s:}"
1790 (mapconcat
1791 (lambda (tag) (org-latex-plain-text tag info))
1792 tags ":"))))))
1793 (concat "\\begin{center}\n"
1794 "\\fbox{\n"
1795 "\\begin{minipage}[c]{.6\\textwidth}\n"
1796 full-title "\n\n"
1797 (and (org-string-nw-p contents)
1798 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
1799 "\\end{minipage}\n"
1800 "}\n"
1801 "\\end{center}")))
1804 ;;;; Italic
1806 (defun org-latex-italic (italic contents info)
1807 "Transcode ITALIC from Org to LaTeX.
1808 CONTENTS is the text with italic markup. INFO is a plist holding
1809 contextual information."
1810 (org-latex--text-markup contents 'italic info))
1813 ;;;; Item
1815 (defun org-latex-item (item contents info)
1816 "Transcode an ITEM element from Org to LaTeX.
1817 CONTENTS holds the contents of the item. INFO is a plist holding
1818 contextual information."
1819 (let* ((counter
1820 (let ((count (org-element-property :counter item))
1821 (level
1822 ;; Determine level of current item to determine the
1823 ;; correct LaTeX counter to use (enumi, enumii...).
1824 (let ((parent item) (level 0))
1825 (while (memq (org-element-type
1826 (setq parent (org-export-get-parent parent)))
1827 '(plain-list item))
1828 (when (and (eq (org-element-type parent) 'plain-list)
1829 (eq (org-element-property :type parent)
1830 'ordered))
1831 (incf level)))
1832 level)))
1833 (and count
1834 (< level 5)
1835 (format "\\setcounter{enum%s}{%s}\n"
1836 (nth (1- level) '("i" "ii" "iii" "iv"))
1837 (1- count)))))
1838 (checkbox (case (org-element-property :checkbox item)
1839 (on "$\\boxtimes$ ")
1840 (off "$\\square$ ")
1841 (trans "$\\boxminus$ ")))
1842 (tag (let ((tag (org-element-property :tag item)))
1843 ;; Check-boxes must belong to the tag.
1844 (and tag (format "[{%s}] "
1845 (concat checkbox
1846 (org-export-data tag info)))))))
1847 (concat counter
1848 "\\item"
1849 (cond
1850 (tag)
1851 (checkbox (concat " " checkbox))
1852 ;; Without a tag or a check-box, if CONTENTS starts with
1853 ;; an opening square bracket, add "\relax" to "\item",
1854 ;; unless the brackets comes from an initial export
1855 ;; snippet (i.e. it is inserted willingly by the user).
1856 ((and contents
1857 (org-string-match-p "\\`[ \t]*\\[" contents)
1858 (not (let ((e (car (org-element-contents item))))
1859 (and (eq (org-element-type e) 'paragraph)
1860 (let ((o (car (org-element-contents e))))
1861 (and (eq (org-element-type o) 'export-snippet)
1862 (eq (org-export-snippet-backend o)
1863 'latex)))))))
1864 "\\relax ")
1865 (t " "))
1866 (and contents (org-trim contents))
1867 ;; If there are footnotes references in tag, be sure to
1868 ;; add their definition at the end of the item. This
1869 ;; workaround is necessary since "\footnote{}" command is
1870 ;; not supported in tags.
1871 (and tag
1872 (org-latex--delayed-footnotes-definitions
1873 (org-element-property :tag item) info)))))
1876 ;;;; Keyword
1878 (defun org-latex-keyword (keyword contents info)
1879 "Transcode a KEYWORD element from Org to LaTeX.
1880 CONTENTS is nil. INFO is a plist holding contextual information."
1881 (let ((key (org-element-property :key keyword))
1882 (value (org-element-property :value keyword)))
1883 (cond
1884 ((string= key "LATEX") value)
1885 ((string= key "INDEX") (format "\\index{%s}" value))
1886 ((string= key "TOC")
1887 (let ((case-fold-search t))
1888 (cond
1889 ((org-string-match-p "\\<headlines\\>" value)
1890 (let* ((localp (org-string-match-p "\\<local\\>" value))
1891 (parent (org-element-lineage keyword '(headline)))
1892 (level (if (not (and localp parent)) 0
1893 (org-export-get-relative-level parent info)))
1894 (depth
1895 (and (string-match "\\<[0-9]+\\>" value)
1896 (format
1897 "\\setcounter{tocdepth}{%d}"
1898 (+ (string-to-number (match-string 0 value)) level)))))
1899 (if (and localp parent)
1900 ;; Start local TOC, assuming package "titletoc" is
1901 ;; required.
1902 (format "\\startcontents[level-%d]
1903 \\printcontents[level-%d]{}{0}{%s}"
1904 level level (or depth ""))
1905 (concat depth (and depth "\n") "\\tableofcontents"))))
1906 ((org-string-match-p "\\<tables\\>" value) "\\listoftables")
1907 ((org-string-match-p "\\<listings\\>" value)
1908 (case (plist-get info :latex-listings)
1909 ((nil) "\\listoffigures")
1910 (minted "\\listoflistings")
1911 (otherwise "\\lstlistoflistings")))))))))
1914 ;;;; Latex Environment
1916 (defun org-latex-latex-environment (latex-environment contents info)
1917 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1918 CONTENTS is nil. INFO is a plist holding contextual information."
1919 (when (plist-get info :with-latex)
1920 (let ((value (org-remove-indentation
1921 (org-element-property :value latex-environment))))
1922 (if (not (org-element-property :name latex-environment)) value
1923 ;; Environment is labeled: label must be within the environment
1924 ;; (otherwise, a reference pointing to that element will count
1925 ;; the section instead).
1926 (with-temp-buffer
1927 (insert value)
1928 (goto-char (point-min))
1929 (forward-line)
1930 (insert (org-latex--label latex-environment info nil t))
1931 (buffer-string))))))
1934 ;;;; Latex Fragment
1936 (defun org-latex-latex-fragment (latex-fragment contents info)
1937 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1938 CONTENTS is nil. INFO is a plist holding contextual information."
1939 (let ((value (org-element-property :value latex-fragment)))
1940 ;; Trim math markers since the fragment is enclosed within
1941 ;; a latex-math-block object anyway.
1942 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
1943 (match-string 2 value))
1944 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
1945 (match-string 1 value))
1946 (t value))))
1949 ;;;; Line Break
1951 (defun org-latex-line-break (line-break contents info)
1952 "Transcode a LINE-BREAK object from Org to LaTeX.
1953 CONTENTS is nil. INFO is a plist holding contextual information."
1954 "\\\\\n")
1957 ;;;; Link
1959 (defun org-latex--inline-image (link info)
1960 "Return LaTeX code for an inline image.
1961 LINK is the link pointing to the inline image. INFO is a plist
1962 used as a communication channel."
1963 (let* ((parent (org-export-get-parent-element link))
1964 (path (let ((raw-path (org-element-property :path link)))
1965 (if (not (file-name-absolute-p raw-path)) raw-path
1966 (expand-file-name raw-path))))
1967 (filetype (file-name-extension path))
1968 (caption (org-latex--caption/label-string parent info))
1969 (caption-above-p (org-latex--caption-above-p link info))
1970 ;; Retrieve latex attributes from the element around.
1971 (attr (org-export-read-attribute :attr_latex parent))
1972 (float (let ((float (plist-get attr :float)))
1973 (cond ((and (not float) (plist-member attr :float)) nil)
1974 ((string= float "wrap") 'wrap)
1975 ((string= float "sideways") 'sideways)
1976 ((string= float "multicolumn") 'multicolumn)
1977 ((or float
1978 (org-element-property :caption parent)
1979 (org-string-nw-p (plist-get attr :caption)))
1980 'figure))))
1981 (placement
1982 (let ((place (plist-get attr :placement)))
1983 (cond
1984 (place (format "%s" place))
1985 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1986 ((eq float 'figure)
1987 (format "[%s]" (plist-get info :latex-default-figure-position)))
1988 (t ""))))
1989 (comment-include (if (plist-get attr :comment-include) "%" ""))
1990 ;; It is possible to specify width and height in the
1991 ;; ATTR_LATEX line, and also via default variables.
1992 (width (cond ((plist-get attr :width))
1993 ((plist-get attr :height) "")
1994 ((eq float 'wrap) "0.48\\textwidth")
1995 (t (plist-get info :latex-image-default-width))))
1996 (height (cond ((plist-get attr :height))
1997 ((or (plist-get attr :width)
1998 (memq float '(figure wrap))) "")
1999 (t (plist-get info :latex-image-default-height))))
2000 (options (let ((opt (or (plist-get attr :options)
2001 (plist-get info :latex-image-default-option))))
2002 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
2003 (match-string 1 opt))))
2004 image-code)
2005 (if (member filetype '("tikz" "pgf"))
2006 ;; For tikz images:
2007 ;; - use \input to read in image file.
2008 ;; - if options are present, wrap in a tikzpicture environment.
2009 ;; - if width or height are present, use \resizebox to change
2010 ;; the image size.
2011 (progn
2012 (setq image-code (format "\\input{%s}" path))
2013 (when (org-string-nw-p options)
2014 (setq image-code
2015 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
2016 options
2017 image-code)))
2018 (when (or (org-string-nw-p width) (org-string-nw-p height))
2019 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
2020 (if (org-string-nw-p width) width "!")
2021 (if (org-string-nw-p height) height "!")
2022 image-code))))
2023 ;; For other images:
2024 ;; - add width and height to options.
2025 ;; - include the image with \includegraphics.
2026 (when (org-string-nw-p width)
2027 (setq options (concat options ",width=" width)))
2028 (when (org-string-nw-p height)
2029 (setq options (concat options ",height=" height)))
2030 (let ((search-option (org-element-property :search-option link)))
2031 (when (and search-option
2032 (equal filetype "pdf")
2033 (org-string-match-p "\\`[0-9]+\\'" search-option)
2034 (not (org-string-match-p "page=" options)))
2035 (setq options (concat options ",page=" search-option))))
2036 (setq image-code
2037 (format "\\includegraphics%s{%s}"
2038 (cond ((not (org-string-nw-p options)) "")
2039 ((= (aref options 0) ?,)
2040 (format "[%s]"(substring options 1)))
2041 (t (format "[%s]" options)))
2042 path))
2043 (when (equal filetype "svg")
2044 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
2045 "\\includesvg"
2046 image-code
2047 nil t))
2048 (setq image-code (replace-regexp-in-string "\\.svg}"
2050 image-code
2051 nil t))))
2052 ;; Return proper string, depending on FLOAT.
2053 (case float
2054 (wrap (format "\\begin{wrapfigure}%s
2055 %s\\centering
2056 %s%s
2057 %s\\end{wrapfigure}"
2058 placement
2059 (if caption-above-p caption "")
2060 comment-include image-code
2061 (if caption-above-p "" caption)))
2062 (sideways (format "\\begin{sidewaysfigure}
2063 %s\\centering
2064 %s%s
2065 %s\\end{sidewaysfigure}"
2066 (if caption-above-p caption "")
2067 comment-include image-code
2068 (if caption-above-p "" caption)))
2069 (multicolumn (format "\\begin{figure*}%s
2070 %s\\centering
2071 %s%s
2072 %s\\end{figure*}"
2073 placement
2074 (if caption-above-p caption "")
2075 comment-include image-code
2076 (if caption-above-p "" caption)))
2077 (figure (format "\\begin{figure}%s
2078 %s\\centering
2079 %s%s
2080 %s\\end{figure}"
2081 placement
2082 (if caption-above-p caption "")
2083 comment-include image-code
2084 (if caption-above-p "" caption)))
2085 (otherwise image-code))))
2087 (defun org-latex-link (link desc info)
2088 "Transcode a LINK object from Org to LaTeX.
2090 DESC is the description part of the link, or the empty string.
2091 INFO is a plist holding contextual information. See
2092 `org-export-data'."
2093 (let* ((type (org-element-property :type link))
2094 (raw-path (replace-regexp-in-string
2095 "%" "\\%" (org-element-property :path link) nil t))
2096 ;; Ensure DESC really exists, or set it to nil.
2097 (desc (and (not (string= desc "")) desc))
2098 (imagep (org-export-inline-image-p
2099 link (plist-get info :latex-inline-image-rules)))
2100 (path (cond
2101 ((member type '("http" "https" "ftp" "mailto" "doi"))
2102 (concat type ":" raw-path))
2103 ((string= type "file") (org-export-file-uri raw-path))
2104 (t raw-path))))
2105 (cond
2106 ;; Link type is handled by a special function.
2107 ((org-export-custom-protocol-maybe link desc 'latex))
2108 ;; Image file.
2109 (imagep (org-latex--inline-image link info))
2110 ;; Radio link: Transcode target's contents and use them as link's
2111 ;; description.
2112 ((string= type "radio")
2113 (let ((destination (org-export-resolve-radio-link link info)))
2114 (if (not destination) desc
2115 (format "\\hyperref[%s]{%s}"
2116 (org-export-get-reference destination info)
2117 desc))))
2118 ;; Links pointing to a headline: Find destination and build
2119 ;; appropriate referencing command.
2120 ((member type '("custom-id" "fuzzy" "id"))
2121 (let ((destination (if (string= type "fuzzy")
2122 (org-export-resolve-fuzzy-link link info)
2123 (org-export-resolve-id-link link info))))
2124 (case (org-element-type destination)
2125 ;; Id link points to an external file.
2126 (plain-text
2127 (if desc (format "\\href{%s}{%s}" destination desc)
2128 (format "\\url{%s}" destination)))
2129 ;; Fuzzy link points nowhere.
2130 ((nil)
2131 (format (plist-get info :latex-link-with-unknown-path-format)
2132 (or desc
2133 (org-export-data
2134 (org-element-property :raw-link link) info))))
2135 ;; LINK points to a headline. If headlines are numbered
2136 ;; and the link has no description, display headline's
2137 ;; number. Otherwise, display description or headline's
2138 ;; title.
2139 (headline
2140 (let ((label (org-latex--label destination info t)))
2141 (if (and (not desc)
2142 (org-export-numbered-headline-p destination info))
2143 (format "\\ref{%s}" label)
2144 (format "\\hyperref[%s]{%s}" label
2145 (or desc
2146 (org-export-data
2147 (org-element-property :title destination) info))))))
2148 ;; Fuzzy link points to a target. Do as above.
2149 (otherwise
2150 (let ((ref (org-latex--label destination info t)))
2151 (if (not desc) (format "\\ref{%s}" ref)
2152 (format "\\hyperref[%s]{%s}" ref desc)))))))
2153 ;; Coderef: replace link with the reference name or the
2154 ;; equivalent line number.
2155 ((string= type "coderef")
2156 (format (org-export-get-coderef-format path desc)
2157 (org-export-resolve-coderef path info)))
2158 ;; External link with a description part.
2159 ((and path desc) (format "\\href{%s}{%s}" path desc))
2160 ;; External link without a description part.
2161 (path (format "\\url{%s}" path))
2162 ;; No path, only description. Try to do something useful.
2163 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
2166 ;;;; Node Property
2168 (defun org-latex-node-property (node-property contents info)
2169 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2170 CONTENTS is nil. INFO is a plist holding contextual
2171 information."
2172 (format "%s:%s"
2173 (org-element-property :key node-property)
2174 (let ((value (org-element-property :value node-property)))
2175 (if value (concat " " value) ""))))
2178 ;;;; Paragraph
2180 (defun org-latex-paragraph (paragraph contents info)
2181 "Transcode a PARAGRAPH element from Org to LaTeX.
2182 CONTENTS is the contents of the paragraph, as a string. INFO is
2183 the plist used as a communication channel."
2184 contents)
2187 ;;;; Plain List
2189 (defun org-latex-plain-list (plain-list contents info)
2190 "Transcode a PLAIN-LIST element from Org to LaTeX.
2191 CONTENTS is the contents of the list. INFO is a plist holding
2192 contextual information."
2193 (let* ((type (org-element-property :type plain-list))
2194 (attr (org-export-read-attribute :attr_latex plain-list))
2195 (latex-type (let ((env (plist-get attr :environment)))
2196 (cond (env (format "%s" env))
2197 ((eq type 'ordered) "enumerate")
2198 ((eq type 'descriptive) "description")
2199 (t "itemize")))))
2200 (org-latex--wrap-label
2201 plain-list
2202 (format "\\begin{%s}%s\n%s\\end{%s}"
2203 latex-type
2204 (or (plist-get attr :options) "")
2205 contents
2206 latex-type)
2207 info)))
2210 ;;;; Plain Text
2212 (defun org-latex-plain-text (text info)
2213 "Transcode a TEXT string from Org to LaTeX.
2214 TEXT is the string to transcode. INFO is a plist holding
2215 contextual information."
2216 (let* ((specialp (plist-get info :with-special-strings))
2217 (output
2218 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2219 (let ((case-fold-search nil))
2220 (replace-regexp-in-string
2221 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2222 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2223 ;; However, if special strings are used, be careful not
2224 ;; to protect "\" in "\-" constructs.
2225 (replace-regexp-in-string
2226 (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\(?:[^-]\\|$\\)"))
2227 (lambda (m)
2228 (case (aref m 0)
2229 (?\\ "$\\\\backslash$")
2230 (?~ "\\\\textasciitilde{}")
2231 (?^ "\\\\^{}")
2232 (t "\\\\\\&")))
2233 text)))))
2234 ;; Activate smart quotes. Be sure to provide original TEXT string
2235 ;; since OUTPUT may have been modified.
2236 (when (plist-get info :with-smart-quotes)
2237 (setq output (org-export-activate-smart-quotes output :latex info text)))
2238 ;; Convert special strings.
2239 (when specialp
2240 (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
2241 ;; Handle break preservation if required.
2242 (when (plist-get info :preserve-breaks)
2243 (setq output (replace-regexp-in-string
2244 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
2245 ;; Return value.
2246 output))
2249 ;;;; Planning
2251 (defun org-latex-planning (planning contents info)
2252 "Transcode a PLANNING element from Org to LaTeX.
2253 CONTENTS is nil. INFO is a plist holding contextual
2254 information."
2255 (concat
2256 "\\noindent"
2257 (mapconcat
2258 'identity
2259 (delq nil
2260 (list
2261 (let ((closed (org-element-property :closed planning)))
2262 (when closed
2263 (concat
2264 (format "\\textbf{%s} " org-closed-string)
2265 (format (plist-get info :latex-inactive-timestamp-format)
2266 (org-timestamp-translate closed)))))
2267 (let ((deadline (org-element-property :deadline planning)))
2268 (when deadline
2269 (concat
2270 (format "\\textbf{%s} " org-deadline-string)
2271 (format (plist-get info :latex-active-timestamp-format)
2272 (org-timestamp-translate deadline)))))
2273 (let ((scheduled (org-element-property :scheduled planning)))
2274 (when scheduled
2275 (concat
2276 (format "\\textbf{%s} " org-scheduled-string)
2277 (format (plist-get info :latex-active-timestamp-format)
2278 (org-timestamp-translate scheduled)))))))
2279 " ")
2280 "\\\\"))
2283 ;;;; Property Drawer
2285 (defun org-latex-property-drawer (property-drawer contents info)
2286 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2287 CONTENTS holds the contents of the drawer. INFO is a plist
2288 holding contextual information."
2289 (and (org-string-nw-p contents)
2290 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2293 ;;;; Pseudo Element: LaTeX Matrices
2295 ;; `latex-matrices' elements have the following properties:
2296 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2297 ;; `math').
2299 (defun org-latex--wrap-latex-matrices (data info)
2300 "Merge contiguous tables with the same mode within a pseudo-element.
2301 DATA is a parse tree or a secondary string. INFO is a plist
2302 containing export options. Modify DATA by side-effect and return
2303 it."
2304 (org-element-map data 'table
2305 (lambda (table)
2306 (when (eq (org-element-property :type table) 'org)
2307 (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
2308 (plist-get info :latex-default-table-mode))))
2309 (when (and (member mode '("inline-math" "math"))
2310 ;; Do not wrap twice the same table.
2311 (not (eq (org-element-type
2312 (org-element-property :parent table))
2313 'latex-matrices)))
2314 (let* ((caption (and (not (string= mode "inline-math"))
2315 (org-element-property :caption table)))
2316 (matrices
2317 (list 'latex-matrices
2318 (list :caption caption
2319 :markup
2320 (cond ((string= mode "inline-math") 'inline)
2321 (caption 'equation)
2322 (t 'math)))))
2323 (previous table)
2324 (next (org-export-get-next-element table info)))
2325 (org-element-insert-before matrices table)
2326 ;; Swallow all contiguous tables sharing the same mode.
2327 (while (and
2328 (zerop (or (org-element-property :post-blank previous) 0))
2329 (setq next (org-export-get-next-element previous info))
2330 (eq (org-element-type next) 'table)
2331 (eq (org-element-property :type next) 'org)
2332 (string= (or (org-export-read-attribute
2333 :attr_latex next :mode)
2334 (plist-get info :latex-default-table-mode))
2335 mode))
2336 (org-element-extract-element previous)
2337 (org-element-adopt-elements matrices previous)
2338 (setq previous next))
2339 (org-element-put-property
2340 matrices :post-blank (org-element-property :post-blank previous))
2341 (org-element-extract-element previous)
2342 (org-element-adopt-elements matrices previous))))))
2343 info)
2344 data)
2346 (defun org-latex-matrices (matrices contents info)
2347 "Transcode a MATRICES element from Org to LaTeX.
2348 CONTENTS is a string. INFO is a plist used as a communication
2349 channel."
2350 (format (case (org-element-property :markup matrices)
2351 (inline "\\(%s\\)")
2352 (equation "\\begin{equation}\n%s\\end{equation}")
2353 (t "\\[\n%s\\]"))
2354 contents))
2356 (defun org-latex-matrices-tree-filter (tree backend info)
2357 (org-latex--wrap-latex-matrices tree info))
2359 ;;;; Pseudo Object: LaTeX Math Block
2361 ;; `latex-math-block' objects have the following property:
2362 ;; `:post-blank'.
2364 (defun org-latex--wrap-latex-math-block (data info)
2365 "Merge contiguous math objects in a pseudo-object container.
2366 DATA is a parse tree or a secondary string. INFO is a plist
2367 containing export options. Modify DATA by side-effect and return it."
2368 (let ((valid-object-p
2369 (function
2370 ;; Non-nil when OBJ can be added to the latex math block.
2371 (lambda (obj)
2372 (case (org-element-type obj)
2373 (entity (org-element-property :latex-math-p obj))
2374 (latex-fragment
2375 (let ((value (org-element-property :value obj)))
2376 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2377 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2378 ((subscript superscript) t))))))
2379 (org-element-map data '(entity latex-fragment subscript superscript)
2380 (lambda (object)
2381 ;; Skip objects already wrapped.
2382 (when (and (not (eq (org-element-type
2383 (org-element-property :parent object))
2384 'latex-math-block))
2385 (funcall valid-object-p object))
2386 (let ((math-block (list 'latex-math-block nil))
2387 (next-elements (org-export-get-next-element object info t))
2388 (last object))
2389 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2390 (org-element-insert-before math-block object)
2391 (org-element-extract-element object)
2392 (org-element-adopt-elements math-block object)
2393 (when (zerop (or (org-element-property :post-blank object) 0))
2394 ;; MATH-BLOCK swallows consecutive math objects.
2395 (catch 'exit
2396 (dolist (next next-elements)
2397 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2398 (org-element-extract-element next)
2399 (org-element-adopt-elements math-block next)
2400 ;; Eschew the case: \beta$x$ -> \(\betax\).
2401 (unless (memq (org-element-type next)
2402 '(subscript superscript))
2403 (org-element-put-property last :post-blank 1))
2404 (setq last next)
2405 (when (> (or (org-element-property :post-blank next) 0) 0)
2406 (throw 'exit nil))))))
2407 (org-element-put-property
2408 math-block :post-blank (org-element-property :post-blank last)))))
2409 info nil '(subscript superscript latex-math-block) t)
2410 ;; Return updated DATA.
2411 data))
2413 (defun org-latex-math-block-tree-filter (tree backend info)
2414 (org-latex--wrap-latex-math-block tree info))
2416 (defun org-latex-math-block-options-filter (info backend)
2417 (dolist (prop '(:author :date :title) info)
2418 (plist-put info prop
2419 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2421 (defun org-latex-math-block (math-block contents info)
2422 "Transcode a MATH-BLOCK object from Org to LaTeX.
2423 CONTENTS is a string. INFO is a plist used as a communication
2424 channel."
2425 (when (org-string-nw-p contents)
2426 (format "\\(%s\\)" (org-trim contents))))
2428 ;;;; Quote Block
2430 (defun org-latex-quote-block (quote-block contents info)
2431 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2432 CONTENTS holds the contents of the block. INFO is a plist
2433 holding contextual information."
2434 (org-latex--wrap-label
2435 quote-block (format "\\begin{quote}\n%s\\end{quote}" contents) info))
2438 ;;;; Radio Target
2440 (defun org-latex-radio-target (radio-target text info)
2441 "Transcode a RADIO-TARGET object from Org to LaTeX.
2442 TEXT is the text of the target. INFO is a plist holding
2443 contextual information."
2444 (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
2447 ;;;; Section
2449 (defun org-latex-section (section contents info)
2450 "Transcode a SECTION element from Org to LaTeX.
2451 CONTENTS holds the contents of the section. INFO is a plist
2452 holding contextual information."
2453 contents)
2456 ;;;; Special Block
2458 (defun org-latex-special-block (special-block contents info)
2459 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2460 CONTENTS holds the contents of the block. INFO is a plist
2461 holding contextual information."
2462 (let ((type (org-element-property :type special-block))
2463 (opt (org-export-read-attribute :attr_latex special-block :options))
2464 (caption (org-latex--caption/label-string special-block info))
2465 (caption-above-p (org-latex--caption-above-p special-block info)))
2466 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2467 (and caption-above-p caption)
2468 contents
2469 (and (not caption-above-p) caption)
2470 (format "\\end{%s}" type))))
2473 ;;;; Src Block
2475 (defun org-latex-src-block (src-block contents info)
2476 "Transcode a SRC-BLOCK element from Org to LaTeX.
2477 CONTENTS holds the contents of the item. INFO is a plist holding
2478 contextual information."
2479 (when (org-string-nw-p (org-element-property :value src-block))
2480 (let* ((lang (org-element-property :language src-block))
2481 (caption (org-element-property :caption src-block))
2482 (caption-above-p (org-latex--caption-above-p src-block info))
2483 (label (org-element-property :name src-block))
2484 (custom-env (and lang
2485 (cadr (assq (intern lang)
2486 org-latex-custom-lang-environments))))
2487 (num-start (case (org-element-property :number-lines src-block)
2488 (continued (org-export-get-loc src-block info))
2489 (new 0)))
2490 (retain-labels (org-element-property :retain-labels src-block))
2491 (attributes (org-export-read-attribute :attr_latex src-block))
2492 (float (plist-get attributes :float))
2493 (listings (plist-get info :latex-listings)))
2494 (cond
2495 ;; Case 1. No source fontification.
2496 ((not listings)
2497 (let* ((caption-str (org-latex--caption/label-string src-block info))
2498 (float-env
2499 (cond ((and (not float) (plist-member attributes :float)) "%s")
2500 ((string= "multicolumn" float)
2501 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2502 (plist-get info :latex-default-figure-position)
2503 (if caption-above-p caption-str "")
2504 (if caption-above-p "" caption-str)))
2505 ((or caption float)
2506 (format "\\begin{figure}[H]\n%%s\n%s\\end{figure}"
2507 caption-str))
2508 (t "%s"))))
2509 (format
2510 float-env
2511 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2512 (org-export-format-code-default src-block info))))))
2513 ;; Case 2. Custom environment.
2514 (custom-env
2515 (let ((caption-str (org-latex--caption/label-string src-block info)))
2516 (format "\\begin{%s}\n%s\\end{%s}\n"
2517 custom-env
2518 (concat (and caption-above-p caption-str)
2519 (org-export-format-code-default src-block info)
2520 (and (not caption-above-p) caption-str))
2521 custom-env)))
2522 ;; Case 3. Use minted package.
2523 ((eq listings 'minted)
2524 (let* ((caption-str (org-latex--caption/label-string src-block info))
2525 (float-env
2526 (cond
2527 ((and (not float) (plist-member attributes :float) caption)
2528 (let ((caption
2529 (replace-regexp-in-string
2530 "\\\\caption" "\\captionof{listing}" caption-str
2531 t t)))
2532 (concat (and caption-above-p caption)
2533 "%%s"
2534 (and (not caption-above-p) (concat "\n" caption)))))
2535 ((and (not float) (plist-member attributes :float)) "%s")
2536 ((string= "multicolumn" float)
2537 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2538 (if caption-above-p caption-str "")
2539 (if caption-above-p "" caption-str)))
2540 ((or caption float)
2541 (format "\\begin{listing}[H]\n%s%%s\n%s\\end{listing}"
2542 (if caption-above-p caption-str "")
2543 (if caption-above-p "" caption-str)))
2544 (t "%s")))
2545 (options (plist-get info :latex-minted-options))
2546 (body
2547 (format
2548 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2549 ;; Options.
2550 (concat
2551 (org-latex--make-option-string
2552 (if (or (not num-start) (assoc "linenos" options))
2553 options
2554 (append
2555 `(("linenos")
2556 ("firstnumber" ,(number-to-string (1+ num-start))))
2557 options)))
2558 (let ((local-options (plist-get attributes :options)))
2559 (and local-options (concat "," local-options))))
2560 ;; Language.
2561 (or (cadr (assq (intern lang)
2562 (plist-get info :latex-minted-langs)))
2563 (downcase lang))
2564 ;; Source code.
2565 (let* ((code-info (org-export-unravel-code src-block))
2566 (max-width
2567 (apply 'max
2568 (mapcar 'length
2569 (org-split-string (car code-info)
2570 "\n")))))
2571 (org-export-format-code
2572 (car code-info)
2573 (lambda (loc num ref)
2574 (concat
2576 (when ref
2577 ;; Ensure references are flushed to the right,
2578 ;; separated with 6 spaces from the widest line
2579 ;; of code.
2580 (concat (make-string (+ (- max-width (length loc)) 6)
2581 ?\s)
2582 (format "(%s)" ref)))))
2583 nil (and retain-labels (cdr code-info)))))))
2584 ;; Return value.
2585 (format float-env body)))
2586 ;; Case 4. Use listings package.
2588 (let ((lst-lang
2589 (or (cadr (assq (intern lang)
2590 (plist-get info :latex-listings-langs)))
2591 lang))
2592 (caption-str
2593 (when caption
2594 (let ((main (org-export-get-caption src-block))
2595 (secondary (org-export-get-caption src-block t)))
2596 (if (not secondary)
2597 (format "{%s}" (org-export-data main info))
2598 (format "{[%s]%s}"
2599 (org-export-data secondary info)
2600 (org-export-data main info))))))
2601 (lst-opt (plist-get info :latex-listings-options)))
2602 (concat
2603 ;; Options.
2604 (format
2605 "\\lstset{%s}\n"
2606 (concat
2607 (org-latex--make-option-string
2608 (append
2609 lst-opt
2610 (cond
2611 ((and (not float) (plist-member attributes :float)) nil)
2612 ((string= "multicolumn" float) '(("float" "*")))
2613 ((and float (not (assoc "float" lst-opt)))
2614 `(("float" ,(plist-get info :latex-default-figure-position)))))
2615 `(("language" ,lst-lang))
2616 (if label `(("label" ,label)) '(("label" " ")))
2617 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2618 `(("captionpos" ,(if caption-above-p "t" "b")))
2619 (cond ((assoc "numbers" lst-opt) nil)
2620 ((not num-start) '(("numbers" "none")))
2621 ((zerop num-start) '(("numbers" "left")))
2622 (t `(("firstnumber" ,(number-to-string (1+ num-start)))
2623 ("numbers" "left"))))))
2624 (let ((local-options (plist-get attributes :options)))
2625 (and local-options (concat "," local-options)))))
2626 ;; Source code.
2627 (format
2628 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2629 (let* ((code-info (org-export-unravel-code src-block))
2630 (max-width
2631 (apply 'max
2632 (mapcar 'length
2633 (org-split-string (car code-info) "\n")))))
2634 (org-export-format-code
2635 (car code-info)
2636 (lambda (loc num ref)
2637 (concat
2639 (when ref
2640 ;; Ensure references are flushed to the right,
2641 ;; separated with 6 spaces from the widest line of
2642 ;; code
2643 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2644 (format "(%s)" ref)))))
2645 nil (and retain-labels (cdr code-info))))))))))))
2648 ;;;; Statistics Cookie
2650 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2651 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2652 CONTENTS is nil. INFO is a plist holding contextual information."
2653 (replace-regexp-in-string
2654 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2657 ;;;; Strike-Through
2659 (defun org-latex-strike-through (strike-through contents info)
2660 "Transcode STRIKE-THROUGH from Org to LaTeX.
2661 CONTENTS is the text with strike-through markup. INFO is a plist
2662 holding contextual information."
2663 (org-latex--text-markup contents 'strike-through info))
2666 ;;;; Subscript
2668 (defun org-latex--script-size (object info)
2669 "Transcode a subscript or superscript object.
2670 OBJECT is an Org object. INFO is a plist used as a communication
2671 channel."
2672 (let ((type (org-element-type object))
2673 (output ""))
2674 (org-element-map (org-element-contents object)
2675 (cons 'plain-text org-element-all-objects)
2676 (lambda (obj)
2677 (case (org-element-type obj)
2678 ((entity latex-fragment)
2679 (let ((data (org-trim (org-export-data obj info))))
2680 (string-match
2681 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2682 data)
2683 (setq output
2684 (concat output
2685 (match-string 1 data)
2686 (let ((blank (org-element-property :post-blank obj)))
2687 (and blank (> blank 0) "\\ "))))))
2688 (plain-text
2689 (setq output
2690 (format "%s\\text{%s}" output (org-export-data obj info))))
2691 (otherwise
2692 (setq output
2693 (concat output
2694 (org-export-data obj info)
2695 (let ((blank (org-element-property :post-blank obj)))
2696 (and blank (> blank 0) "\\ ")))))))
2697 info nil org-element-recursive-objects)
2698 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2699 ;; character.
2700 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2701 (and (> (length output) 1) "{")
2702 output
2703 (and (> (length output) 1) "}"))))
2705 (defun org-latex-subscript (subscript contents info)
2706 "Transcode a SUBSCRIPT object from Org to LaTeX.
2707 CONTENTS is the contents of the object. INFO is a plist holding
2708 contextual information."
2709 (org-latex--script-size subscript info))
2712 ;;;; Superscript
2714 (defun org-latex-superscript (superscript contents info)
2715 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2716 CONTENTS is the contents of the object. INFO is a plist holding
2717 contextual information."
2718 (org-latex--script-size superscript info))
2721 ;;;; Table
2723 ;; `org-latex-table' is the entry point for table transcoding. It
2724 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2725 ;; delegates the job to either `org-latex--table.el-table',
2726 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2727 ;; depending of the type of the table and the mode requested.
2729 ;; `org-latex--align-string' is a subroutine used to build alignment
2730 ;; string for Org tables.
2732 (defun org-latex-table (table contents info)
2733 "Transcode a TABLE element from Org to LaTeX.
2734 CONTENTS is the contents of the table. INFO is a plist holding
2735 contextual information."
2736 (if (eq (org-element-property :type table) 'table.el)
2737 ;; "table.el" table. Convert it using appropriate tools.
2738 (org-latex--table.el-table table info)
2739 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2740 (plist-get info :latex-default-table-mode))))
2741 (cond
2742 ;; Case 1: Verbatim table.
2743 ((string= type "verbatim")
2744 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2745 ;; Re-create table, without affiliated keywords.
2746 (org-trim (org-element-interpret-data
2747 `(table nil ,@(org-element-contents table))))))
2748 ;; Case 2: Matrix.
2749 ((or (string= type "math") (string= type "inline-math"))
2750 (org-latex--math-table table info))
2751 ;; Case 3: Standard table.
2752 (t (concat (org-latex--org-table table contents info)
2753 ;; When there are footnote references within the
2754 ;; table, insert their definition just after it.
2755 (org-latex--delayed-footnotes-definitions table info)))))))
2757 (defun org-latex--align-string (table info)
2758 "Return an appropriate LaTeX alignment string.
2759 TABLE is the considered table. INFO is a plist used as
2760 a communication channel."
2761 (or (org-export-read-attribute :attr_latex table :align)
2762 (let (align)
2763 ;; Extract column groups and alignment from first (non-rule)
2764 ;; row.
2765 (org-element-map
2766 (org-element-map table 'table-row
2767 (lambda (row)
2768 (and (eq (org-element-property :type row) 'standard) row))
2769 info 'first-match)
2770 'table-cell
2771 (lambda (cell)
2772 (let ((borders (org-export-table-cell-borders cell info)))
2773 ;; Check left border for the first cell only.
2774 (when (and (memq 'left borders) (not align))
2775 (push "|" align))
2776 (push (case (org-export-table-cell-alignment cell info)
2777 (left "l")
2778 (right "r")
2779 (center "c"))
2780 align)
2781 (when (memq 'right borders) (push "|" align))))
2782 info)
2783 (apply 'concat (nreverse align)))))
2785 (defun org-latex--org-table (table contents info)
2786 "Return appropriate LaTeX code for an Org table.
2788 TABLE is the table type element to transcode. CONTENTS is its
2789 contents, as a string. INFO is a plist used as a communication
2790 channel.
2792 This function assumes TABLE has `org' as its `:type' property and
2793 `table' as its `:mode' attribute."
2794 (let* ((caption (org-latex--caption/label-string table info))
2795 (attr (org-export-read-attribute :attr_latex table))
2796 ;; Determine alignment string.
2797 (alignment (org-latex--align-string table info))
2798 ;; Determine environment for the table: longtable, tabular...
2799 (table-env (or (plist-get attr :environment)
2800 (plist-get info :latex-default-table-environment)))
2801 ;; If table is a float, determine environment: table, table*
2802 ;; or sidewaystable.
2803 (float-env (unless (member table-env '("longtable" "longtabu"))
2804 (let ((float (plist-get attr :float)))
2805 (cond
2806 ((and (not float) (plist-member attr :float)) nil)
2807 ((or (string= float "sidewaystable")
2808 (string= float "sideways")) "sidewaystable")
2809 ((string= float "multicolumn") "table*")
2810 ((or float
2811 (org-element-property :caption table)
2812 (org-string-nw-p (plist-get attr :caption)))
2813 "table")))))
2814 ;; Extract others display options.
2815 (fontsize (let ((font (plist-get attr :font)))
2816 (and font (concat font "\n"))))
2817 ;; "tabular" environment doesn't allow to define a width.
2818 (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
2819 (spreadp (plist-get attr :spread))
2820 (placement
2821 (or (plist-get attr :placement)
2822 (format "[%s]" (plist-get info :latex-default-figure-position))))
2823 (centerp (if (plist-member attr :center) (plist-get attr :center)
2824 (plist-get info :latex-tables-centered)))
2825 (caption-above-p (org-latex--caption-above-p table info)))
2826 ;; Prepare the final format string for the table.
2827 (cond
2828 ;; Longtable.
2829 ((equal "longtable" table-env)
2830 (concat (and fontsize (concat "{" fontsize))
2831 (format "\\begin{longtable}{%s}\n" alignment)
2832 (and caption-above-p
2833 (org-string-nw-p caption)
2834 (concat caption "\\\\\n"))
2835 contents
2836 (and (not caption-above-p)
2837 (org-string-nw-p caption)
2838 (concat caption "\\\\\n"))
2839 "\\end{longtable}\n"
2840 (and fontsize "}")))
2841 ;; Longtabu
2842 ((equal "longtabu" table-env)
2843 (concat (and fontsize (concat "{" fontsize))
2844 (format "\\begin{longtabu}%s{%s}\n"
2845 (if width
2846 (format " %s %s "
2847 (if spreadp "spread" "to") width) "")
2848 alignment)
2849 (and caption-above-p
2850 (org-string-nw-p caption)
2851 (concat caption "\\\\\n"))
2852 contents
2853 (and (not caption-above-p)
2854 (org-string-nw-p caption)
2855 (concat caption "\\\\\n"))
2856 "\\end{longtabu}\n"
2857 (and fontsize "}")))
2858 ;; Others.
2859 (t (concat (cond
2860 (float-env
2861 (concat (format "\\begin{%s}%s\n" float-env placement)
2862 (if caption-above-p caption "")
2863 (when centerp "\\centering\n")
2864 fontsize))
2865 (centerp (concat "\\begin{center}\n" fontsize))
2866 (fontsize (concat "{" fontsize)))
2867 (cond ((equal "tabu" table-env)
2868 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2869 (if width (format
2870 (if spreadp " spread %s " " to %s ")
2871 width) "")
2872 alignment
2873 contents))
2874 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2875 table-env
2876 (if width (format "{%s}" width) "")
2877 alignment
2878 contents
2879 table-env)))
2880 (cond
2881 (float-env
2882 (concat (if caption-above-p "" caption)
2883 (format "\n\\end{%s}" float-env)))
2884 (centerp "\n\\end{center}")
2885 (fontsize "}")))))))
2887 (defun org-latex--table.el-table (table info)
2888 "Return appropriate LaTeX code for a table.el table.
2890 TABLE is the table type element to transcode. INFO is a plist
2891 used as a communication channel.
2893 This function assumes TABLE has `table.el' as its `:type'
2894 property."
2895 (require 'table)
2896 ;; Ensure "*org-export-table*" buffer is empty.
2897 (with-current-buffer (get-buffer-create "*org-export-table*")
2898 (erase-buffer))
2899 (let ((output (with-temp-buffer
2900 (insert (org-element-property :value table))
2901 (goto-char 1)
2902 (re-search-forward "^[ \t]*|[^|]" nil t)
2903 (table-generate-source 'latex "*org-export-table*")
2904 (with-current-buffer "*org-export-table*"
2905 (org-trim (buffer-string))))))
2906 (kill-buffer (get-buffer "*org-export-table*"))
2907 ;; Remove left out comments.
2908 (while (string-match "^%.*\n" output)
2909 (setq output (replace-match "" t t output)))
2910 (let ((attr (org-export-read-attribute :attr_latex table)))
2911 (when (plist-get attr :rmlines)
2912 ;; When the "rmlines" attribute is provided, remove all hlines
2913 ;; but the the one separating heading from the table body.
2914 (let ((n 0) (pos 0))
2915 (while (and (< (length output) pos)
2916 (setq pos (string-match "^\\\\hline\n?" output pos)))
2917 (incf n)
2918 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2919 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2920 (plist-get info :latex-tables-centered))))
2921 (if (not centerp) output
2922 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2924 (defun org-latex--math-table (table info)
2925 "Return appropriate LaTeX code for a matrix.
2927 TABLE is the table type element to transcode. INFO is a plist
2928 used as a communication channel.
2930 This function assumes TABLE has `org' as its `:type' property and
2931 `inline-math' or `math' as its `:mode' attribute."
2932 (let* ((attr (org-export-read-attribute :attr_latex table))
2933 (env (or (plist-get attr :environment)
2934 (plist-get info :latex-default-table-environment)))
2935 (contents
2936 (mapconcat
2937 (lambda (row)
2938 ;; Ignore horizontal rules.
2939 (when (eq (org-element-property :type row) 'standard)
2940 ;; Return each cell unmodified.
2941 (concat
2942 (mapconcat
2943 (lambda (cell)
2944 (substring (org-element-interpret-data cell) 0 -1))
2945 (org-element-map row 'table-cell #'identity info) "&")
2946 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2947 "\n")))
2948 (org-element-map table 'table-row #'identity info) "")))
2949 (concat
2950 ;; Prefix.
2951 (plist-get attr :math-prefix)
2952 ;; Environment. Also treat special cases.
2953 (cond ((member env '("array" "tabular"))
2954 (let ((align (make-string
2955 (cdr (org-export-table-dimensions table info)) ?c)))
2956 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
2957 ((assoc env org-latex-table-matrix-macros)
2958 (format "\\%s%s{\n%s}"
2960 (or (plist-get attr :math-arguments) "")
2961 contents))
2962 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2963 ;; Suffix.
2964 (plist-get attr :math-suffix))))
2967 ;;;; Table Cell
2969 (defun org-latex-table-cell (table-cell contents info)
2970 "Transcode a TABLE-CELL element from Org to LaTeX.
2971 CONTENTS is the cell contents. INFO is a plist used as
2972 a communication channel."
2973 (concat
2974 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
2975 (if (and contents
2976 scientific-format
2977 (string-match orgtbl-exp-regexp contents))
2978 ;; Use appropriate format string for scientific
2979 ;; notation.
2980 (format scientific-format
2981 (match-string 1 contents)
2982 (match-string 2 contents))
2983 contents))
2984 (when (org-export-get-next-element table-cell info) " & ")))
2987 ;;;; Table Row
2989 (defun org-latex-table-row (table-row contents info)
2990 "Transcode a TABLE-ROW element from Org to LaTeX.
2991 CONTENTS is the contents of the row. INFO is a plist used as
2992 a communication channel."
2993 (let* ((attr (org-export-read-attribute :attr_latex
2994 (org-export-get-parent table-row)))
2995 (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
2996 (plist-get info :latex-tables-booktabs)))
2997 (longtablep
2998 (member (or (plist-get attr :environment)
2999 (plist-get info :latex-default-table-environment))
3000 '("longtable" "longtabu"))))
3001 (if (eq (org-element-property :type table-row) 'rule)
3002 (cond
3003 ((not booktabsp) "\\hline")
3004 ((not (org-export-get-previous-element table-row info)) "\\toprule")
3005 ((not (org-export-get-next-element table-row info)) "\\bottomrule")
3006 ((and longtablep
3007 (org-export-table-row-ends-header-p
3008 (org-export-get-previous-element table-row info) info))
3010 (t "\\midrule"))
3011 (concat
3012 ;; When BOOKTABS are activated enforce top-rule even when no
3013 ;; hline was specifically marked.
3014 (and booktabsp (not (org-export-get-previous-element table-row info))
3015 "\\toprule\n")
3016 contents "\\\\\n"
3017 (cond
3018 ;; Special case for long tables. Define header and footers.
3019 ((and longtablep (org-export-table-row-ends-header-p table-row info))
3020 (let ((columns (cdr (org-export-table-dimensions
3021 (org-export-get-parent-table table-row) info))))
3022 (format "%s
3023 \\endfirsthead
3024 \\multicolumn{%d}{l}{%s} \\\\
3026 %s \\\\\n
3028 \\endhead
3029 %s\\multicolumn{%d}{r}{%s} \\\\
3030 \\endfoot
3031 \\endlastfoot"
3032 (if booktabsp "\\midrule" "\\hline")
3033 columns
3034 (org-latex--translate "Continued from previous page" info)
3035 (cond
3036 ((not (org-export-table-row-starts-header-p table-row info))
3038 (booktabsp "\\toprule\n")
3039 (t "\\hline\n"))
3040 contents
3041 (if booktabsp "\\midrule" "\\hline")
3042 (if booktabsp "\\midrule" "\\hline")
3043 columns
3044 (org-latex--translate "Continued on next page" info))))
3045 ;; When BOOKTABS are activated enforce bottom rule even when
3046 ;; no hline was specifically marked.
3047 ((and booktabsp (not (org-export-get-next-element table-row info)))
3048 "\\bottomrule"))))))
3051 ;;;; Target
3053 (defun org-latex-target (target contents info)
3054 "Transcode a TARGET object from Org to LaTeX.
3055 CONTENTS is nil. INFO is a plist holding contextual
3056 information."
3057 (format "\\label{%s}" (org-latex--label target info)))
3060 ;;;; Timestamp
3062 (defun org-latex-timestamp (timestamp contents info)
3063 "Transcode a TIMESTAMP object from Org to LaTeX.
3064 CONTENTS is nil. INFO is a plist holding contextual
3065 information."
3066 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
3067 (format
3068 (plist-get info
3069 (case (org-element-property :type timestamp)
3070 ((active active-range) :latex-active-timestamp-format)
3071 ((inactive inactive-range) :latex-inactive-timestamp-format)
3072 (otherwise :latex-diary-timestamp-format)))
3073 value)))
3076 ;;;; Underline
3078 (defun org-latex-underline (underline contents info)
3079 "Transcode UNDERLINE from Org to LaTeX.
3080 CONTENTS is the text with underline markup. INFO is a plist
3081 holding contextual information."
3082 (org-latex--text-markup contents 'underline info))
3085 ;;;; Verbatim
3087 (defun org-latex-verbatim (verbatim contents info)
3088 "Transcode a VERBATIM object from Org to LaTeX.
3089 CONTENTS is nil. INFO is a plist used as a communication
3090 channel."
3091 (org-latex--text-markup
3092 (org-element-property :value verbatim) 'verbatim info))
3095 ;;;; Verse Block
3097 (defun org-latex-verse-block (verse-block contents info)
3098 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3099 CONTENTS is verse block contents. INFO is a plist holding
3100 contextual information."
3101 (org-latex--wrap-label
3102 verse-block
3103 ;; In a verse environment, add a line break to each newline
3104 ;; character and change each white space at beginning of a line
3105 ;; into a space of 1 em. Also change each blank line with
3106 ;; a vertical space of 1 em.
3107 (format "\\begin{verse}\n%s\\end{verse}"
3108 (replace-regexp-in-string
3109 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
3110 (replace-regexp-in-string
3111 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3112 (replace-regexp-in-string
3113 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3114 contents nil t) nil t) nil t))
3115 info))
3119 ;;; End-user functions
3121 ;;;###autoload
3122 (defun org-latex-export-as-latex
3123 (&optional async subtreep visible-only body-only ext-plist)
3124 "Export current buffer as a LaTeX buffer.
3126 If narrowing is active in the current buffer, only export its
3127 narrowed part.
3129 If a region is active, export that region.
3131 A non-nil optional argument ASYNC means the process should happen
3132 asynchronously. The resulting buffer should be accessible
3133 through the `org-export-stack' interface.
3135 When optional argument SUBTREEP is non-nil, export the sub-tree
3136 at point, extracting information from the headline properties
3137 first.
3139 When optional argument VISIBLE-ONLY is non-nil, don't export
3140 contents of hidden elements.
3142 When optional argument BODY-ONLY is non-nil, only write code
3143 between \"\\begin{document}\" and \"\\end{document}\".
3145 EXT-PLIST, when provided, is a property list with external
3146 parameters overriding Org default settings, but still inferior to
3147 file-local settings.
3149 Export is done in a buffer named \"*Org LATEX Export*\", which
3150 will be displayed when `org-export-show-temporary-export-buffer'
3151 is non-nil."
3152 (interactive)
3153 (org-export-to-buffer 'latex "*Org LATEX Export*"
3154 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
3156 ;;;###autoload
3157 (defun org-latex-convert-region-to-latex ()
3158 "Assume the current region has org-mode syntax, and convert it to LaTeX.
3159 This can be used in any buffer. For example, you can write an
3160 itemized list in org-mode syntax in an LaTeX buffer and use this
3161 command to convert it."
3162 (interactive)
3163 (org-export-replace-region-by 'latex))
3165 ;;;###autoload
3166 (defun org-latex-export-to-latex
3167 (&optional async subtreep visible-only body-only ext-plist)
3168 "Export current buffer to a LaTeX file.
3170 If narrowing is active in the current buffer, only export its
3171 narrowed part.
3173 If a region is active, export that region.
3175 A non-nil optional argument ASYNC means the process should happen
3176 asynchronously. The resulting file should be accessible through
3177 the `org-export-stack' interface.
3179 When optional argument SUBTREEP is non-nil, export the sub-tree
3180 at point, extracting information from the headline properties
3181 first.
3183 When optional argument VISIBLE-ONLY is non-nil, don't export
3184 contents of hidden elements.
3186 When optional argument BODY-ONLY is non-nil, only write code
3187 between \"\\begin{document}\" and \"\\end{document}\".
3189 EXT-PLIST, when provided, is a property list with external
3190 parameters overriding Org default settings, but still inferior to
3191 file-local settings."
3192 (interactive)
3193 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3194 (org-export-to-file 'latex outfile
3195 async subtreep visible-only body-only ext-plist)))
3197 ;;;###autoload
3198 (defun org-latex-export-to-pdf
3199 (&optional async subtreep visible-only body-only ext-plist)
3200 "Export current buffer to LaTeX then process through to PDF.
3202 If narrowing is active in the current buffer, only export its
3203 narrowed part.
3205 If a region is active, export that region.
3207 A non-nil optional argument ASYNC means the process should happen
3208 asynchronously. The resulting file should be accessible through
3209 the `org-export-stack' interface.
3211 When optional argument SUBTREEP is non-nil, export the sub-tree
3212 at point, extracting information from the headline properties
3213 first.
3215 When optional argument VISIBLE-ONLY is non-nil, don't export
3216 contents of hidden elements.
3218 When optional argument BODY-ONLY is non-nil, only write code
3219 between \"\\begin{document}\" and \"\\end{document}\".
3221 EXT-PLIST, when provided, is a property list with external
3222 parameters overriding Org default settings, but still inferior to
3223 file-local settings.
3225 Return PDF file's name."
3226 (interactive)
3227 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3228 (org-export-to-file 'latex outfile
3229 async subtreep visible-only body-only ext-plist
3230 (lambda (file) (org-latex-compile file)))))
3232 (defun org-latex-compile (texfile &optional snippet)
3233 "Compile a TeX file.
3235 TEXFILE is the name of the file being compiled. Processing is
3236 done through the command specified in `org-latex-pdf-process'.
3238 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3239 file used to preview a LaTeX snippet. In this case, do not
3240 create a log buffer and do not bother removing log files.
3242 Return PDF file name or an error if it couldn't be produced."
3243 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3244 (full-name (file-truename texfile))
3245 (out-dir (file-name-directory texfile))
3246 ;; Properly set working directory for compilation.
3247 (default-directory (if (file-name-absolute-p texfile)
3248 (file-name-directory full-name)
3249 default-directory))
3250 (time (current-time))
3251 warnings)
3252 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
3253 (save-window-excursion
3254 (cond
3255 ;; A function is provided: Apply it.
3256 ((functionp org-latex-pdf-process)
3257 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3258 ;; A list is provided: Replace %b, %f and %o with appropriate
3259 ;; values in each command before applying it. Output is
3260 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3261 ((consp org-latex-pdf-process)
3262 (let ((outbuf (and (not snippet)
3263 (get-buffer-create "*Org PDF LaTeX Output*"))))
3264 (dolist (command org-latex-pdf-process)
3265 (shell-command
3266 (replace-regexp-in-string
3267 "%b" (shell-quote-argument base-name)
3268 (replace-regexp-in-string
3269 "%f" (shell-quote-argument full-name)
3270 (replace-regexp-in-string
3271 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
3272 outbuf))
3273 ;; Collect standard errors from output buffer.
3274 (setq warnings (and (not snippet)
3275 (org-latex--collect-warnings outbuf)))))
3276 (t (error "No valid command to process to PDF")))
3277 (let ((pdffile (concat out-dir base-name ".pdf")))
3278 ;; Check for process failure. Provide collected errors if
3279 ;; possible.
3280 (if (or (not (file-exists-p pdffile))
3281 (time-less-p (nth 5 (file-attributes pdffile)) time))
3282 (error (format "PDF file %s wasn't produced" pdffile))
3283 ;; Else remove log files, when specified, and signal end of
3284 ;; process to user, along with any error encountered.
3285 (unless snippet
3286 (when org-latex-remove-logfiles
3287 (dolist (file (directory-files
3288 out-dir t
3289 (concat (regexp-quote base-name)
3290 "\\(?:\\.[0-9]+\\)?"
3291 "\\."
3292 (regexp-opt org-latex-logfiles-extensions))))
3293 (delete-file file)))
3294 (message (concat "PDF file produced"
3295 (cond
3296 ((eq warnings 'error) " with errors.")
3297 (warnings (concat " with warnings: " warnings))
3298 (t "."))))))
3299 ;; Return output file name.
3300 pdffile))))
3302 (defun org-latex--collect-warnings (buffer)
3303 "Collect some warnings from \"pdflatex\" command output.
3304 BUFFER is the buffer containing output. Return collected
3305 warnings types as a string, `error' if a LaTeX error was
3306 encountered or nil if there was none."
3307 (with-current-buffer buffer
3308 (save-excursion
3309 (goto-char (point-max))
3310 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3311 (if (re-search-forward "^!" nil t) 'error
3312 (let ((case-fold-search t)
3313 (warnings ""))
3314 (dolist (warning org-latex-known-warnings)
3315 (when (save-excursion (re-search-forward (car warning) nil t))
3316 (setq warnings (concat warnings " " (cdr warning)))))
3317 (org-string-nw-p (org-trim warnings))))))))
3319 ;;;###autoload
3320 (defun org-latex-publish-to-latex (plist filename pub-dir)
3321 "Publish an Org file to LaTeX.
3323 FILENAME is the filename of the Org file to be published. PLIST
3324 is the property list for the given project. PUB-DIR is the
3325 publishing directory.
3327 Return output file name."
3328 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3330 ;;;###autoload
3331 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3332 "Publish an Org file to PDF (via LaTeX).
3334 FILENAME is the filename of the Org file to be published. PLIST
3335 is the property list for the given project. PUB-DIR is the
3336 publishing directory.
3338 Return output file name."
3339 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3340 ;; in working directory and then moved to publishing directory.
3341 (org-publish-attachment
3342 plist
3343 (org-latex-compile
3344 (org-publish-org-to
3345 'latex filename ".tex" plist (file-name-directory filename)))
3346 pub-dir))
3349 (provide 'ox-latex)
3351 ;; Local variables:
3352 ;; generated-autoload-file: "org-loaddefs.el"
3353 ;; End:
3355 ;;; ox-latex.el ends here