ox-latex: Remove useless call to `org-latex--wrap-latex-math-block'
[org-mode/org-tableheadings.git] / lisp / ox-latex.el
blobd0bc3b4cb4a21947a3345a6427b30df9b24c4841
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 (let ((environment (or (org-export-read-attribute
1492 :attr_latex example-block :environment)
1493 "verbatim")))
1494 (org-latex--wrap-label
1495 example-block
1496 (format "\\begin{%s}\n%s\\end{%s}"
1497 environment
1498 (org-export-format-code-default example-block info)
1499 environment)
1500 info))))
1503 ;;;; Export Block
1505 (defun org-latex-export-block (export-block contents info)
1506 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1507 CONTENTS is nil. INFO is a plist holding contextual information."
1508 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1509 (org-remove-indentation (org-element-property :value export-block))))
1512 ;;;; Export Snippet
1514 (defun org-latex-export-snippet (export-snippet contents info)
1515 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1516 CONTENTS is nil. INFO is a plist holding contextual information."
1517 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1518 (org-element-property :value export-snippet)))
1521 ;;;; Fixed Width
1523 (defun org-latex-fixed-width (fixed-width contents info)
1524 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1525 CONTENTS is nil. INFO is a plist holding contextual information."
1526 (org-latex--wrap-label
1527 fixed-width
1528 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1529 (org-remove-indentation
1530 (org-element-property :value fixed-width)))
1531 info))
1534 ;;;; Footnote Reference
1536 (defun org-latex-footnote-reference (footnote-reference contents info)
1537 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1538 CONTENTS is nil. INFO is a plist holding contextual information."
1539 (concat
1540 ;; Insert separator between two footnotes in a row.
1541 (let ((prev (org-export-get-previous-element footnote-reference info)))
1542 (when (eq (org-element-type prev) 'footnote-reference)
1543 (plist-get info :latex-footnote-separator)))
1544 (cond
1545 ;; Use \footnotemark if the footnote has already been defined.
1546 ((not (org-export-footnote-first-reference-p footnote-reference info))
1547 (format "\\footnotemark[%s]{}"
1548 (org-export-get-footnote-number footnote-reference info)))
1549 ;; Use \footnotemark if reference is within another footnote
1550 ;; reference, footnote definition or table cell.
1551 ((org-element-lineage footnote-reference
1552 '(footnote-reference footnote-definition table-cell))
1553 "\\footnotemark")
1554 ;; Otherwise, define it with \footnote command.
1556 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1557 (concat
1558 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1559 ;; Retrieve all footnote references within the footnote and
1560 ;; add their definition after it, since LaTeX doesn't support
1561 ;; them inside.
1562 (org-latex--delayed-footnotes-definitions def info)))))))
1565 ;;;; Headline
1567 (defun org-latex-headline (headline contents info)
1568 "Transcode a HEADLINE element from Org to LaTeX.
1569 CONTENTS holds the contents of the headline. INFO is a plist
1570 holding contextual information."
1571 (unless (org-element-property :footnote-section-p headline)
1572 (let* ((class (plist-get info :latex-class))
1573 (level (org-export-get-relative-level headline info))
1574 (numberedp (org-export-numbered-headline-p headline info))
1575 (class-sectioning (assoc class (plist-get info :latex-classes)))
1576 ;; Section formatting will set two placeholders: one for
1577 ;; the title and the other for the contents.
1578 (section-fmt
1579 (let ((sec (if (functionp (nth 2 class-sectioning))
1580 (funcall (nth 2 class-sectioning) level numberedp)
1581 (nth (1+ level) class-sectioning))))
1582 (cond
1583 ;; No section available for that LEVEL.
1584 ((not sec) nil)
1585 ;; Section format directly returned by a function. Add
1586 ;; placeholder for contents.
1587 ((stringp sec) (concat sec "\n%s"))
1588 ;; (numbered-section . unnumbered-section)
1589 ((not (consp (cdr sec)))
1590 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1591 ;; (numbered-open numbered-close)
1592 ((= (length sec) 2)
1593 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1594 ;; (num-in num-out no-num-in no-num-out)
1595 ((= (length sec) 4)
1596 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1597 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1598 ;; Create a temporary export back-end that hard-codes
1599 ;; "\underline" within "\section" and alike.
1600 (section-back-end
1601 (org-export-create-backend
1602 :parent 'latex
1603 :transcoders
1604 '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
1605 (text
1606 (org-export-data-with-backend
1607 (org-element-property :title headline) section-back-end info))
1608 (todo
1609 (and (plist-get info :with-todo-keywords)
1610 (let ((todo (org-element-property :todo-keyword headline)))
1611 (and todo (org-export-data todo info)))))
1612 (todo-type (and todo (org-element-property :todo-type headline)))
1613 (tags (and (plist-get info :with-tags)
1614 (org-export-get-tags headline info)))
1615 (priority (and (plist-get info :with-priority)
1616 (org-element-property :priority headline)))
1617 ;; Create the headline text along with a no-tag version.
1618 ;; The latter is required to remove tags from toc.
1619 (full-text (funcall (plist-get info :latex-format-headline-function)
1620 todo todo-type priority text tags info))
1621 ;; Associate \label to the headline for internal links.
1622 (headline-label (org-latex--label headline info t t))
1623 (pre-blanks
1624 (make-string (org-element-property :pre-blank headline) ?\n)))
1625 (if (or (not section-fmt) (org-export-low-level-p headline info))
1626 ;; This is a deep sub-tree: export it as a list item. Also
1627 ;; export as items headlines for which no section format has
1628 ;; been found.
1629 (let ((low-level-body
1630 (concat
1631 ;; If headline is the first sibling, start a list.
1632 (when (org-export-first-sibling-p headline info)
1633 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1634 ;; Itemize headline
1635 "\\item"
1636 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
1637 "\\relax")
1638 " " full-text "\n"
1639 headline-label
1640 pre-blanks
1641 contents)))
1642 ;; If headline is not the last sibling simply return
1643 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1644 ;; any blank line.
1645 (if (not (org-export-last-sibling-p headline info)) low-level-body
1646 (replace-regexp-in-string
1647 "[ \t\n]*\\'"
1648 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1649 low-level-body)))
1650 ;; This is a standard headline. Export it as a section. Add
1651 ;; an alternative heading when possible, and when this is not
1652 ;; identical to the usual heading.
1653 (let ((opt-title
1654 (funcall (plist-get info :latex-format-headline-function)
1655 todo todo-type priority
1656 (org-export-data-with-backend
1657 (org-export-get-alt-title headline info)
1658 section-back-end info)
1659 (and (eq (plist-get info :with-tags) t) tags)
1660 info))
1661 ;; Maybe end local TOC (see `org-latex-keyword').
1662 (contents
1663 (concat
1664 contents
1665 (let ((case-fold-search t)
1666 (section
1667 (let ((first (car (org-element-contents headline))))
1668 (and (eq (org-element-type first) 'section) first))))
1669 (org-element-map section 'keyword
1670 (lambda (k)
1671 (and (equal (org-element-property :key k) "TOC")
1672 (let ((v (org-element-property :value k)))
1673 (and (org-string-match-p "\\<headlines\\>" v)
1674 (org-string-match-p "\\<local\\>" v)
1675 (format "\\stopcontents[level-%d]" level)))))
1676 info t)))))
1677 (if (and opt-title
1678 (not (equal opt-title full-text))
1679 (string-match "\\`\\\\\\(.+?\\){" section-fmt))
1680 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1681 ;; Replace square brackets with parenthesis
1682 ;; since square brackets are not supported in
1683 ;; optional arguments.
1684 (replace-regexp-in-string
1685 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1686 full-text
1687 (concat headline-label pre-blanks contents))
1688 ;; Impossible to add an alternative heading. Fallback to
1689 ;; regular sectioning format string.
1690 (format section-fmt full-text
1691 (concat headline-label pre-blanks contents))))))))
1693 (defun org-latex-format-headline-default-function
1694 (todo todo-type priority text tags info)
1695 "Default format function for a headline.
1696 See `org-latex-format-headline-function' for details."
1697 (concat
1698 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1699 (and priority (format "\\framebox{\\#%c} " priority))
1700 text
1701 (and tags
1702 (format "\\hfill{}\\textsc{%s}"
1703 (mapconcat (lambda (tag) (org-latex-plain-text tag info))
1704 tags ":")))))
1707 ;;;; Horizontal Rule
1709 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1710 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1711 CONTENTS is nil. INFO is a plist holding contextual information."
1712 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1713 (prev (org-export-get-previous-element horizontal-rule info)))
1714 (concat
1715 ;; Make sure the rule doesn't start at the end of the current
1716 ;; line by separating it with a blank line from previous element.
1717 (when (and prev
1718 (let ((prev-blank (org-element-property :post-blank prev)))
1719 (or (not prev-blank) (zerop prev-blank))))
1720 "\n")
1721 (org-latex--wrap-label
1722 horizontal-rule
1723 (format "\\rule{%s}{%s}"
1724 (or (plist-get attr :width) "\\linewidth")
1725 (or (plist-get attr :thickness) "0.5pt"))
1726 info))))
1729 ;;;; Inline Src Block
1731 (defun org-latex-inline-src-block (inline-src-block contents info)
1732 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1733 CONTENTS holds the contents of the item. INFO is a plist holding
1734 contextual information."
1735 (let* ((code (org-element-property :value inline-src-block))
1736 (separator (org-latex--find-verb-separator code)))
1737 (case (plist-get info :latex-listings)
1738 ;; Do not use a special package: transcode it verbatim.
1739 ((nil) (format "\\texttt{%s}" (org-latex--protect-text code)))
1740 ;; Use minted package.
1741 (minted
1742 (let* ((org-lang (org-element-property :language inline-src-block))
1743 (mint-lang (or (cadr (assq (intern org-lang)
1744 (plist-get info :latex-minted-langs)))
1745 (downcase org-lang)))
1746 (options (org-latex--make-option-string
1747 (plist-get info :latex-minted-options))))
1748 (concat (format "\\mint%s{%s}"
1749 (if (string= options "") "" (format "[%s]" options))
1750 mint-lang)
1751 separator code separator)))
1752 ;; Use listings package.
1753 (otherwise
1754 ;; Maybe translate language's name.
1755 (let* ((org-lang (org-element-property :language inline-src-block))
1756 (lst-lang (or (cadr (assq (intern org-lang)
1757 (plist-get info :latex-listings-langs)))
1758 org-lang))
1759 (options (org-latex--make-option-string
1760 (append (plist-get info :latex-listings-options)
1761 `(("language" ,lst-lang))))))
1762 (concat (format "\\lstinline[%s]" options)
1763 separator code separator))))))
1766 ;;;; Inlinetask
1768 (defun org-latex-inlinetask (inlinetask contents info)
1769 "Transcode an INLINETASK element from Org to LaTeX.
1770 CONTENTS holds the contents of the block. INFO is a plist
1771 holding contextual information."
1772 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1773 (todo (and (plist-get info :with-todo-keywords)
1774 (let ((todo (org-element-property :todo-keyword inlinetask)))
1775 (and todo (org-export-data todo info)))))
1776 (todo-type (org-element-property :todo-type inlinetask))
1777 (tags (and (plist-get info :with-tags)
1778 (org-export-get-tags inlinetask info)))
1779 (priority (and (plist-get info :with-priority)
1780 (org-element-property :priority inlinetask)))
1781 (contents (concat (org-latex--label inlinetask info) contents)))
1782 (funcall (plist-get info :latex-format-inlinetask-function)
1783 todo todo-type priority title tags contents info)))
1785 (defun org-latex-format-inlinetask-default-function
1786 (todo todo-type priority title tags contents info)
1787 "Default format function for a inlinetasks.
1788 See `org-latex-format-inlinetask-function' for details."
1789 (let ((full-title
1790 (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1791 (when priority (format "\\framebox{\\#%c} " priority))
1792 title
1793 (when tags
1794 (format "\\hfill{}\\textsc{:%s:}"
1795 (mapconcat
1796 (lambda (tag) (org-latex-plain-text tag info))
1797 tags ":"))))))
1798 (concat "\\begin{center}\n"
1799 "\\fbox{\n"
1800 "\\begin{minipage}[c]{.6\\textwidth}\n"
1801 full-title "\n\n"
1802 (and (org-string-nw-p contents)
1803 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
1804 "\\end{minipage}\n"
1805 "}\n"
1806 "\\end{center}")))
1809 ;;;; Italic
1811 (defun org-latex-italic (italic contents info)
1812 "Transcode ITALIC from Org to LaTeX.
1813 CONTENTS is the text with italic markup. INFO is a plist holding
1814 contextual information."
1815 (org-latex--text-markup contents 'italic info))
1818 ;;;; Item
1820 (defun org-latex-item (item contents info)
1821 "Transcode an ITEM element from Org to LaTeX.
1822 CONTENTS holds the contents of the item. INFO is a plist holding
1823 contextual information."
1824 (let* ((counter
1825 (let ((count (org-element-property :counter item))
1826 (level
1827 ;; Determine level of current item to determine the
1828 ;; correct LaTeX counter to use (enumi, enumii...).
1829 (let ((parent item) (level 0))
1830 (while (memq (org-element-type
1831 (setq parent (org-export-get-parent parent)))
1832 '(plain-list item))
1833 (when (and (eq (org-element-type parent) 'plain-list)
1834 (eq (org-element-property :type parent)
1835 'ordered))
1836 (incf level)))
1837 level)))
1838 (and count
1839 (< level 5)
1840 (format "\\setcounter{enum%s}{%s}\n"
1841 (nth (1- level) '("i" "ii" "iii" "iv"))
1842 (1- count)))))
1843 (checkbox (case (org-element-property :checkbox item)
1844 (on "$\\boxtimes$ ")
1845 (off "$\\square$ ")
1846 (trans "$\\boxminus$ ")))
1847 (tag (let ((tag (org-element-property :tag item)))
1848 ;; Check-boxes must belong to the tag.
1849 (and tag (format "[{%s}] "
1850 (concat checkbox
1851 (org-export-data tag info)))))))
1852 (concat counter
1853 "\\item"
1854 (cond
1855 (tag)
1856 (checkbox (concat " " checkbox))
1857 ;; Without a tag or a check-box, if CONTENTS starts with
1858 ;; an opening square bracket, add "\relax" to "\item",
1859 ;; unless the brackets comes from an initial export
1860 ;; snippet (i.e. it is inserted willingly by the user).
1861 ((and contents
1862 (org-string-match-p "\\`[ \t]*\\[" contents)
1863 (not (let ((e (car (org-element-contents item))))
1864 (and (eq (org-element-type e) 'paragraph)
1865 (let ((o (car (org-element-contents e))))
1866 (and (eq (org-element-type o) 'export-snippet)
1867 (eq (org-export-snippet-backend o)
1868 'latex)))))))
1869 "\\relax ")
1870 (t " "))
1871 (and contents (org-trim contents))
1872 ;; If there are footnotes references in tag, be sure to
1873 ;; add their definition at the end of the item. This
1874 ;; workaround is necessary since "\footnote{}" command is
1875 ;; not supported in tags.
1876 (and tag
1877 (org-latex--delayed-footnotes-definitions
1878 (org-element-property :tag item) info)))))
1881 ;;;; Keyword
1883 (defun org-latex-keyword (keyword contents info)
1884 "Transcode a KEYWORD element from Org to LaTeX.
1885 CONTENTS is nil. INFO is a plist holding contextual information."
1886 (let ((key (org-element-property :key keyword))
1887 (value (org-element-property :value keyword)))
1888 (cond
1889 ((string= key "LATEX") value)
1890 ((string= key "INDEX") (format "\\index{%s}" value))
1891 ((string= key "TOC")
1892 (let ((case-fold-search t))
1893 (cond
1894 ((org-string-match-p "\\<headlines\\>" value)
1895 (let* ((localp (org-string-match-p "\\<local\\>" value))
1896 (parent (org-element-lineage keyword '(headline)))
1897 (level (if (not (and localp parent)) 0
1898 (org-export-get-relative-level parent info)))
1899 (depth
1900 (and (string-match "\\<[0-9]+\\>" value)
1901 (format
1902 "\\setcounter{tocdepth}{%d}"
1903 (+ (string-to-number (match-string 0 value)) level)))))
1904 (if (and localp parent)
1905 ;; Start local TOC, assuming package "titletoc" is
1906 ;; required.
1907 (format "\\startcontents[level-%d]
1908 \\printcontents[level-%d]{}{0}{%s}"
1909 level level (or depth ""))
1910 (concat depth (and depth "\n") "\\tableofcontents"))))
1911 ((org-string-match-p "\\<tables\\>" value) "\\listoftables")
1912 ((org-string-match-p "\\<listings\\>" value)
1913 (case (plist-get info :latex-listings)
1914 ((nil) "\\listoffigures")
1915 (minted "\\listoflistings")
1916 (otherwise "\\lstlistoflistings")))))))))
1919 ;;;; Latex Environment
1921 (defun org-latex-latex-environment (latex-environment contents info)
1922 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1923 CONTENTS is nil. INFO is a plist holding contextual information."
1924 (when (plist-get info :with-latex)
1925 (let ((value (org-remove-indentation
1926 (org-element-property :value latex-environment))))
1927 (if (not (org-element-property :name latex-environment)) value
1928 ;; Environment is labeled: label must be within the environment
1929 ;; (otherwise, a reference pointing to that element will count
1930 ;; the section instead).
1931 (with-temp-buffer
1932 (insert value)
1933 (goto-char (point-min))
1934 (forward-line)
1935 (insert (org-latex--label latex-environment info nil t))
1936 (buffer-string))))))
1939 ;;;; Latex Fragment
1941 (defun org-latex-latex-fragment (latex-fragment contents info)
1942 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1943 CONTENTS is nil. INFO is a plist holding contextual information."
1944 (let ((value (org-element-property :value latex-fragment)))
1945 ;; Trim math markers since the fragment is enclosed within
1946 ;; a latex-math-block object anyway.
1947 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
1948 (match-string 2 value))
1949 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
1950 (match-string 1 value))
1951 (t value))))
1954 ;;;; Line Break
1956 (defun org-latex-line-break (line-break contents info)
1957 "Transcode a LINE-BREAK object from Org to LaTeX.
1958 CONTENTS is nil. INFO is a plist holding contextual information."
1959 "\\\\\n")
1962 ;;;; Link
1964 (defun org-latex--inline-image (link info)
1965 "Return LaTeX code for an inline image.
1966 LINK is the link pointing to the inline image. INFO is a plist
1967 used as a communication channel."
1968 (let* ((parent (org-export-get-parent-element link))
1969 (path (let ((raw-path (org-element-property :path link)))
1970 (if (not (file-name-absolute-p raw-path)) raw-path
1971 (expand-file-name raw-path))))
1972 (filetype (file-name-extension path))
1973 (caption (org-latex--caption/label-string parent info))
1974 (caption-above-p (org-latex--caption-above-p link info))
1975 ;; Retrieve latex attributes from the element around.
1976 (attr (org-export-read-attribute :attr_latex parent))
1977 (float (let ((float (plist-get attr :float)))
1978 (cond ((and (not float) (plist-member attr :float)) nil)
1979 ((string= float "wrap") 'wrap)
1980 ((string= float "sideways") 'sideways)
1981 ((string= float "multicolumn") 'multicolumn)
1982 ((or float
1983 (org-element-property :caption parent)
1984 (org-string-nw-p (plist-get attr :caption)))
1985 'figure))))
1986 (placement
1987 (let ((place (plist-get attr :placement)))
1988 (cond
1989 (place (format "%s" place))
1990 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1991 ((eq float 'figure)
1992 (format "[%s]" (plist-get info :latex-default-figure-position)))
1993 (t ""))))
1994 (comment-include (if (plist-get attr :comment-include) "%" ""))
1995 ;; It is possible to specify width and height in the
1996 ;; ATTR_LATEX line, and also via default variables.
1997 (width (cond ((plist-get attr :width))
1998 ((plist-get attr :height) "")
1999 ((eq float 'wrap) "0.48\\textwidth")
2000 (t (plist-get info :latex-image-default-width))))
2001 (height (cond ((plist-get attr :height))
2002 ((or (plist-get attr :width)
2003 (memq float '(figure wrap))) "")
2004 (t (plist-get info :latex-image-default-height))))
2005 (options (let ((opt (or (plist-get attr :options)
2006 (plist-get info :latex-image-default-option))))
2007 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
2008 (match-string 1 opt))))
2009 image-code)
2010 (if (member filetype '("tikz" "pgf"))
2011 ;; For tikz images:
2012 ;; - use \input to read in image file.
2013 ;; - if options are present, wrap in a tikzpicture environment.
2014 ;; - if width or height are present, use \resizebox to change
2015 ;; the image size.
2016 (progn
2017 (setq image-code (format "\\input{%s}" path))
2018 (when (org-string-nw-p options)
2019 (setq image-code
2020 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
2021 options
2022 image-code)))
2023 (when (or (org-string-nw-p width) (org-string-nw-p height))
2024 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
2025 (if (org-string-nw-p width) width "!")
2026 (if (org-string-nw-p height) height "!")
2027 image-code))))
2028 ;; For other images:
2029 ;; - add width and height to options.
2030 ;; - include the image with \includegraphics.
2031 (when (org-string-nw-p width)
2032 (setq options (concat options ",width=" width)))
2033 (when (org-string-nw-p height)
2034 (setq options (concat options ",height=" height)))
2035 (let ((search-option (org-element-property :search-option link)))
2036 (when (and search-option
2037 (equal filetype "pdf")
2038 (org-string-match-p "\\`[0-9]+\\'" search-option)
2039 (not (org-string-match-p "page=" options)))
2040 (setq options (concat options ",page=" search-option))))
2041 (setq image-code
2042 (format "\\includegraphics%s{%s}"
2043 (cond ((not (org-string-nw-p options)) "")
2044 ((= (aref options 0) ?,)
2045 (format "[%s]"(substring options 1)))
2046 (t (format "[%s]" options)))
2047 path))
2048 (when (equal filetype "svg")
2049 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
2050 "\\includesvg"
2051 image-code
2052 nil t))
2053 (setq image-code (replace-regexp-in-string "\\.svg}"
2055 image-code
2056 nil t))))
2057 ;; Return proper string, depending on FLOAT.
2058 (case float
2059 (wrap (format "\\begin{wrapfigure}%s
2060 %s\\centering
2061 %s%s
2062 %s\\end{wrapfigure}"
2063 placement
2064 (if caption-above-p caption "")
2065 comment-include image-code
2066 (if caption-above-p "" caption)))
2067 (sideways (format "\\begin{sidewaysfigure}
2068 %s\\centering
2069 %s%s
2070 %s\\end{sidewaysfigure}"
2071 (if caption-above-p caption "")
2072 comment-include image-code
2073 (if caption-above-p "" caption)))
2074 (multicolumn (format "\\begin{figure*}%s
2075 %s\\centering
2076 %s%s
2077 %s\\end{figure*}"
2078 placement
2079 (if caption-above-p caption "")
2080 comment-include image-code
2081 (if caption-above-p "" caption)))
2082 (figure (format "\\begin{figure}%s
2083 %s\\centering
2084 %s%s
2085 %s\\end{figure}"
2086 placement
2087 (if caption-above-p caption "")
2088 comment-include image-code
2089 (if caption-above-p "" caption)))
2090 (otherwise image-code))))
2092 (defun org-latex-link (link desc info)
2093 "Transcode a LINK object from Org to LaTeX.
2095 DESC is the description part of the link, or the empty string.
2096 INFO is a plist holding contextual information. See
2097 `org-export-data'."
2098 (let* ((type (org-element-property :type link))
2099 (raw-path (replace-regexp-in-string
2100 "%" "\\%" (org-element-property :path link) nil t))
2101 ;; Ensure DESC really exists, or set it to nil.
2102 (desc (and (not (string= desc "")) desc))
2103 (imagep (org-export-inline-image-p
2104 link (plist-get info :latex-inline-image-rules)))
2105 (path (cond
2106 ((member type '("http" "https" "ftp" "mailto" "doi"))
2107 (concat type ":" raw-path))
2108 ((string= type "file") (org-export-file-uri raw-path))
2109 (t raw-path))))
2110 (cond
2111 ;; Link type is handled by a special function.
2112 ((org-export-custom-protocol-maybe link desc 'latex))
2113 ;; Image file.
2114 (imagep (org-latex--inline-image link info))
2115 ;; Radio link: Transcode target's contents and use them as link's
2116 ;; description.
2117 ((string= type "radio")
2118 (let ((destination (org-export-resolve-radio-link link info)))
2119 (if (not destination) desc
2120 (format "\\hyperref[%s]{%s}"
2121 (org-export-get-reference destination info)
2122 desc))))
2123 ;; Links pointing to a headline: Find destination and build
2124 ;; appropriate referencing command.
2125 ((member type '("custom-id" "fuzzy" "id"))
2126 (let ((destination (if (string= type "fuzzy")
2127 (org-export-resolve-fuzzy-link link info)
2128 (org-export-resolve-id-link link info))))
2129 (case (org-element-type destination)
2130 ;; Id link points to an external file.
2131 (plain-text
2132 (if desc (format "\\href{%s}{%s}" destination desc)
2133 (format "\\url{%s}" destination)))
2134 ;; Fuzzy link points nowhere.
2135 ((nil)
2136 (format (plist-get info :latex-link-with-unknown-path-format)
2137 (or desc
2138 (org-export-data
2139 (org-element-property :raw-link link) info))))
2140 ;; LINK points to a headline. If headlines are numbered
2141 ;; and the link has no description, display headline's
2142 ;; number. Otherwise, display description or headline's
2143 ;; title.
2144 (headline
2145 (let ((label (org-latex--label destination info t)))
2146 (if (and (not desc)
2147 (org-export-numbered-headline-p destination info))
2148 (format "\\ref{%s}" label)
2149 (format "\\hyperref[%s]{%s}" label
2150 (or desc
2151 (org-export-data
2152 (org-element-property :title destination) info))))))
2153 ;; Fuzzy link points to a target. Do as above.
2154 (otherwise
2155 (let ((ref (org-latex--label destination info t)))
2156 (if (not desc) (format "\\ref{%s}" ref)
2157 (format "\\hyperref[%s]{%s}" ref desc)))))))
2158 ;; Coderef: replace link with the reference name or the
2159 ;; equivalent line number.
2160 ((string= type "coderef")
2161 (format (org-export-get-coderef-format path desc)
2162 (org-export-resolve-coderef path info)))
2163 ;; External link with a description part.
2164 ((and path desc) (format "\\href{%s}{%s}" path desc))
2165 ;; External link without a description part.
2166 (path (format "\\url{%s}" path))
2167 ;; No path, only description. Try to do something useful.
2168 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
2171 ;;;; Node Property
2173 (defun org-latex-node-property (node-property contents info)
2174 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2175 CONTENTS is nil. INFO is a plist holding contextual
2176 information."
2177 (format "%s:%s"
2178 (org-element-property :key node-property)
2179 (let ((value (org-element-property :value node-property)))
2180 (if value (concat " " value) ""))))
2183 ;;;; Paragraph
2185 (defun org-latex-paragraph (paragraph contents info)
2186 "Transcode a PARAGRAPH element from Org to LaTeX.
2187 CONTENTS is the contents of the paragraph, as a string. INFO is
2188 the plist used as a communication channel."
2189 contents)
2192 ;;;; Plain List
2194 (defun org-latex-plain-list (plain-list contents info)
2195 "Transcode a PLAIN-LIST element from Org to LaTeX.
2196 CONTENTS is the contents of the list. INFO is a plist holding
2197 contextual information."
2198 (let* ((type (org-element-property :type plain-list))
2199 (attr (org-export-read-attribute :attr_latex plain-list))
2200 (latex-type (let ((env (plist-get attr :environment)))
2201 (cond (env (format "%s" env))
2202 ((eq type 'ordered) "enumerate")
2203 ((eq type 'descriptive) "description")
2204 (t "itemize")))))
2205 (org-latex--wrap-label
2206 plain-list
2207 (format "\\begin{%s}%s\n%s\\end{%s}"
2208 latex-type
2209 (or (plist-get attr :options) "")
2210 contents
2211 latex-type)
2212 info)))
2215 ;;;; Plain Text
2217 (defun org-latex-plain-text (text info)
2218 "Transcode a TEXT string from Org to LaTeX.
2219 TEXT is the string to transcode. INFO is a plist holding
2220 contextual information."
2221 (let* ((specialp (plist-get info :with-special-strings))
2222 (output
2223 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2224 (let ((case-fold-search nil))
2225 (replace-regexp-in-string
2226 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2227 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2228 ;; However, if special strings are used, be careful not
2229 ;; to protect "\" in "\-" constructs.
2230 (replace-regexp-in-string
2231 (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\(?:[^-]\\|$\\)"))
2232 (lambda (m)
2233 (case (aref m 0)
2234 (?\\ "$\\\\backslash$")
2235 (?~ "\\\\textasciitilde{}")
2236 (?^ "\\\\^{}")
2237 (t "\\\\\\&")))
2238 text)))))
2239 ;; Activate smart quotes. Be sure to provide original TEXT string
2240 ;; since OUTPUT may have been modified.
2241 (when (plist-get info :with-smart-quotes)
2242 (setq output (org-export-activate-smart-quotes output :latex info text)))
2243 ;; Convert special strings.
2244 (when specialp
2245 (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
2246 ;; Handle break preservation if required.
2247 (when (plist-get info :preserve-breaks)
2248 (setq output (replace-regexp-in-string
2249 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
2250 ;; Return value.
2251 output))
2254 ;;;; Planning
2256 (defun org-latex-planning (planning contents info)
2257 "Transcode a PLANNING element from Org to LaTeX.
2258 CONTENTS is nil. INFO is a plist holding contextual
2259 information."
2260 (concat
2261 "\\noindent"
2262 (mapconcat
2263 'identity
2264 (delq nil
2265 (list
2266 (let ((closed (org-element-property :closed planning)))
2267 (when closed
2268 (concat
2269 (format "\\textbf{%s} " org-closed-string)
2270 (format (plist-get info :latex-inactive-timestamp-format)
2271 (org-timestamp-translate closed)))))
2272 (let ((deadline (org-element-property :deadline planning)))
2273 (when deadline
2274 (concat
2275 (format "\\textbf{%s} " org-deadline-string)
2276 (format (plist-get info :latex-active-timestamp-format)
2277 (org-timestamp-translate deadline)))))
2278 (let ((scheduled (org-element-property :scheduled planning)))
2279 (when scheduled
2280 (concat
2281 (format "\\textbf{%s} " org-scheduled-string)
2282 (format (plist-get info :latex-active-timestamp-format)
2283 (org-timestamp-translate scheduled)))))))
2284 " ")
2285 "\\\\"))
2288 ;;;; Property Drawer
2290 (defun org-latex-property-drawer (property-drawer contents info)
2291 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2292 CONTENTS holds the contents of the drawer. INFO is a plist
2293 holding contextual information."
2294 (and (org-string-nw-p contents)
2295 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2298 ;;;; Pseudo Element: LaTeX Matrices
2300 ;; `latex-matrices' elements have the following properties:
2301 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2302 ;; `math').
2304 (defun org-latex--wrap-latex-matrices (data info)
2305 "Merge contiguous tables with the same mode within a pseudo-element.
2306 DATA is a parse tree or a secondary string. INFO is a plist
2307 containing export options. Modify DATA by side-effect and return
2308 it."
2309 (org-element-map data 'table
2310 (lambda (table)
2311 (when (eq (org-element-property :type table) 'org)
2312 (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
2313 (plist-get info :latex-default-table-mode))))
2314 (when (and (member mode '("inline-math" "math"))
2315 ;; Do not wrap twice the same table.
2316 (not (eq (org-element-type
2317 (org-element-property :parent table))
2318 'latex-matrices)))
2319 (let* ((caption (and (not (string= mode "inline-math"))
2320 (org-element-property :caption table)))
2321 (matrices
2322 (list 'latex-matrices
2323 (list :caption caption
2324 :markup
2325 (cond ((string= mode "inline-math") 'inline)
2326 (caption 'equation)
2327 (t 'math)))))
2328 (previous table)
2329 (next (org-export-get-next-element table info)))
2330 (org-element-insert-before matrices table)
2331 ;; Swallow all contiguous tables sharing the same mode.
2332 (while (and
2333 (zerop (or (org-element-property :post-blank previous) 0))
2334 (setq next (org-export-get-next-element previous info))
2335 (eq (org-element-type next) 'table)
2336 (eq (org-element-property :type next) 'org)
2337 (string= (or (org-export-read-attribute
2338 :attr_latex next :mode)
2339 (plist-get info :latex-default-table-mode))
2340 mode))
2341 (org-element-extract-element previous)
2342 (org-element-adopt-elements matrices previous)
2343 (setq previous next))
2344 (org-element-put-property
2345 matrices :post-blank (org-element-property :post-blank previous))
2346 (org-element-extract-element previous)
2347 (org-element-adopt-elements matrices previous))))))
2348 info)
2349 data)
2351 (defun org-latex-matrices (matrices contents info)
2352 "Transcode a MATRICES element from Org to LaTeX.
2353 CONTENTS is a string. INFO is a plist used as a communication
2354 channel."
2355 (format (case (org-element-property :markup matrices)
2356 (inline "\\(%s\\)")
2357 (equation "\\begin{equation}\n%s\\end{equation}")
2358 (t "\\[\n%s\\]"))
2359 contents))
2361 (defun org-latex-matrices-tree-filter (tree backend info)
2362 (org-latex--wrap-latex-matrices tree info))
2364 ;;;; Pseudo Object: LaTeX Math Block
2366 ;; `latex-math-block' objects have the following property:
2367 ;; `:post-blank'.
2369 (defun org-latex--wrap-latex-math-block (data info)
2370 "Merge contiguous math objects in a pseudo-object container.
2371 DATA is a parse tree or a secondary string. INFO is a plist
2372 containing export options. Modify DATA by side-effect and return it."
2373 (let ((valid-object-p
2374 (function
2375 ;; Non-nil when OBJ can be added to the latex math block.
2376 (lambda (obj)
2377 (case (org-element-type obj)
2378 (entity (org-element-property :latex-math-p obj))
2379 (latex-fragment
2380 (let ((value (org-element-property :value obj)))
2381 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2382 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2383 ((subscript superscript) t))))))
2384 (org-element-map data '(entity latex-fragment subscript superscript)
2385 (lambda (object)
2386 ;; Skip objects already wrapped.
2387 (when (and (not (eq (org-element-type
2388 (org-element-property :parent object))
2389 'latex-math-block))
2390 (funcall valid-object-p object))
2391 (let ((math-block (list 'latex-math-block nil))
2392 (next-elements (org-export-get-next-element object info t))
2393 (last object))
2394 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2395 (org-element-insert-before math-block object)
2396 (org-element-extract-element object)
2397 (org-element-adopt-elements math-block object)
2398 (when (zerop (or (org-element-property :post-blank object) 0))
2399 ;; MATH-BLOCK swallows consecutive math objects.
2400 (catch 'exit
2401 (dolist (next next-elements)
2402 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2403 (org-element-extract-element next)
2404 (org-element-adopt-elements math-block next)
2405 ;; Eschew the case: \beta$x$ -> \(\betax\).
2406 (unless (memq (org-element-type next)
2407 '(subscript superscript))
2408 (org-element-put-property last :post-blank 1))
2409 (setq last next)
2410 (when (> (or (org-element-property :post-blank next) 0) 0)
2411 (throw 'exit nil))))))
2412 (org-element-put-property
2413 math-block :post-blank (org-element-property :post-blank last)))))
2414 info nil '(subscript superscript latex-math-block) t)
2415 ;; Return updated DATA.
2416 data))
2418 (defun org-latex-math-block-tree-filter (tree backend info)
2419 (org-latex--wrap-latex-math-block tree info))
2421 (defun org-latex-math-block-options-filter (info backend)
2422 (dolist (prop '(:author :date :title) info)
2423 (plist-put info prop
2424 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2426 (defun org-latex-math-block (math-block contents info)
2427 "Transcode a MATH-BLOCK object from Org to LaTeX.
2428 CONTENTS is a string. INFO is a plist used as a communication
2429 channel."
2430 (when (org-string-nw-p contents)
2431 (format "\\(%s\\)" (org-trim contents))))
2433 ;;;; Quote Block
2435 (defun org-latex-quote-block (quote-block contents info)
2436 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2437 CONTENTS holds the contents of the block. INFO is a plist
2438 holding contextual information."
2439 (org-latex--wrap-label
2440 quote-block (format "\\begin{quote}\n%s\\end{quote}" contents) info))
2443 ;;;; Radio Target
2445 (defun org-latex-radio-target (radio-target text info)
2446 "Transcode a RADIO-TARGET object from Org to LaTeX.
2447 TEXT is the text of the target. INFO is a plist holding
2448 contextual information."
2449 (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
2452 ;;;; Section
2454 (defun org-latex-section (section contents info)
2455 "Transcode a SECTION element from Org to LaTeX.
2456 CONTENTS holds the contents of the section. INFO is a plist
2457 holding contextual information."
2458 contents)
2461 ;;;; Special Block
2463 (defun org-latex-special-block (special-block contents info)
2464 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2465 CONTENTS holds the contents of the block. INFO is a plist
2466 holding contextual information."
2467 (let ((type (org-element-property :type special-block))
2468 (opt (org-export-read-attribute :attr_latex special-block :options))
2469 (caption (org-latex--caption/label-string special-block info))
2470 (caption-above-p (org-latex--caption-above-p special-block info)))
2471 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2472 (and caption-above-p caption)
2473 contents
2474 (and (not caption-above-p) caption)
2475 (format "\\end{%s}" type))))
2478 ;;;; Src Block
2480 (defun org-latex-src-block (src-block contents info)
2481 "Transcode a SRC-BLOCK element from Org to LaTeX.
2482 CONTENTS holds the contents of the item. INFO is a plist holding
2483 contextual information."
2484 (when (org-string-nw-p (org-element-property :value src-block))
2485 (let* ((lang (org-element-property :language src-block))
2486 (caption (org-element-property :caption src-block))
2487 (caption-above-p (org-latex--caption-above-p src-block info))
2488 (label (org-element-property :name src-block))
2489 (custom-env (and lang
2490 (cadr (assq (intern lang)
2491 org-latex-custom-lang-environments))))
2492 (num-start (case (org-element-property :number-lines src-block)
2493 (continued (org-export-get-loc src-block info))
2494 (new 0)))
2495 (retain-labels (org-element-property :retain-labels src-block))
2496 (attributes (org-export-read-attribute :attr_latex src-block))
2497 (float (plist-get attributes :float))
2498 (listings (plist-get info :latex-listings)))
2499 (cond
2500 ;; Case 1. No source fontification.
2501 ((not listings)
2502 (let* ((caption-str (org-latex--caption/label-string src-block info))
2503 (float-env
2504 (cond ((and (not float) (plist-member attributes :float)) "%s")
2505 ((string= "multicolumn" float)
2506 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2507 (plist-get info :latex-default-figure-position)
2508 (if caption-above-p caption-str "")
2509 (if caption-above-p "" caption-str)))
2510 ((or caption float)
2511 (format "\\begin{figure}[H]\n%%s\n%s\\end{figure}"
2512 caption-str))
2513 (t "%s"))))
2514 (format
2515 float-env
2516 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2517 (org-export-format-code-default src-block info))))))
2518 ;; Case 2. Custom environment.
2519 (custom-env
2520 (let ((caption-str (org-latex--caption/label-string src-block info)))
2521 (format "\\begin{%s}\n%s\\end{%s}\n"
2522 custom-env
2523 (concat (and caption-above-p caption-str)
2524 (org-export-format-code-default src-block info)
2525 (and (not caption-above-p) caption-str))
2526 custom-env)))
2527 ;; Case 3. Use minted package.
2528 ((eq listings 'minted)
2529 (let* ((caption-str (org-latex--caption/label-string src-block info))
2530 (float-env
2531 (cond
2532 ((and (not float) (plist-member attributes :float) caption)
2533 (let ((caption
2534 (replace-regexp-in-string
2535 "\\\\caption" "\\captionof{listing}" caption-str
2536 t t)))
2537 (concat (and caption-above-p caption)
2538 "%%s"
2539 (and (not caption-above-p) (concat "\n" caption)))))
2540 ((and (not float) (plist-member attributes :float)) "%s")
2541 ((string= "multicolumn" float)
2542 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2543 (if caption-above-p caption-str "")
2544 (if caption-above-p "" caption-str)))
2545 ((or caption float)
2546 (format "\\begin{listing}[H]\n%s%%s\n%s\\end{listing}"
2547 (if caption-above-p caption-str "")
2548 (if caption-above-p "" caption-str)))
2549 (t "%s")))
2550 (options (plist-get info :latex-minted-options))
2551 (body
2552 (format
2553 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2554 ;; Options.
2555 (concat
2556 (org-latex--make-option-string
2557 (if (or (not num-start) (assoc "linenos" options))
2558 options
2559 (append
2560 `(("linenos")
2561 ("firstnumber" ,(number-to-string (1+ num-start))))
2562 options)))
2563 (let ((local-options (plist-get attributes :options)))
2564 (and local-options (concat "," local-options))))
2565 ;; Language.
2566 (or (cadr (assq (intern lang)
2567 (plist-get info :latex-minted-langs)))
2568 (downcase lang))
2569 ;; Source code.
2570 (let* ((code-info (org-export-unravel-code src-block))
2571 (max-width
2572 (apply 'max
2573 (mapcar 'length
2574 (org-split-string (car code-info)
2575 "\n")))))
2576 (org-export-format-code
2577 (car code-info)
2578 (lambda (loc num ref)
2579 (concat
2581 (when ref
2582 ;; Ensure references are flushed to the right,
2583 ;; separated with 6 spaces from the widest line
2584 ;; of code.
2585 (concat (make-string (+ (- max-width (length loc)) 6)
2586 ?\s)
2587 (format "(%s)" ref)))))
2588 nil (and retain-labels (cdr code-info)))))))
2589 ;; Return value.
2590 (format float-env body)))
2591 ;; Case 4. Use listings package.
2593 (let ((lst-lang
2594 (or (cadr (assq (intern lang)
2595 (plist-get info :latex-listings-langs)))
2596 lang))
2597 (caption-str
2598 (when caption
2599 (let ((main (org-export-get-caption src-block))
2600 (secondary (org-export-get-caption src-block t)))
2601 (if (not secondary)
2602 (format "{%s}" (org-export-data main info))
2603 (format "{[%s]%s}"
2604 (org-export-data secondary info)
2605 (org-export-data main info))))))
2606 (lst-opt (plist-get info :latex-listings-options)))
2607 (concat
2608 ;; Options.
2609 (format
2610 "\\lstset{%s}\n"
2611 (concat
2612 (org-latex--make-option-string
2613 (append
2614 lst-opt
2615 (cond
2616 ((and (not float) (plist-member attributes :float)) nil)
2617 ((string= "multicolumn" float) '(("float" "*")))
2618 ((and float (not (assoc "float" lst-opt)))
2619 `(("float" ,(plist-get info :latex-default-figure-position)))))
2620 `(("language" ,lst-lang))
2621 (if label `(("label" ,label)) '(("label" " ")))
2622 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2623 `(("captionpos" ,(if caption-above-p "t" "b")))
2624 (cond ((assoc "numbers" lst-opt) nil)
2625 ((not num-start) '(("numbers" "none")))
2626 ((zerop num-start) '(("numbers" "left")))
2627 (t `(("firstnumber" ,(number-to-string (1+ num-start)))
2628 ("numbers" "left"))))))
2629 (let ((local-options (plist-get attributes :options)))
2630 (and local-options (concat "," local-options)))))
2631 ;; Source code.
2632 (format
2633 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2634 (let* ((code-info (org-export-unravel-code src-block))
2635 (max-width
2636 (apply 'max
2637 (mapcar 'length
2638 (org-split-string (car code-info) "\n")))))
2639 (org-export-format-code
2640 (car code-info)
2641 (lambda (loc num ref)
2642 (concat
2644 (when ref
2645 ;; Ensure references are flushed to the right,
2646 ;; separated with 6 spaces from the widest line of
2647 ;; code
2648 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2649 (format "(%s)" ref)))))
2650 nil (and retain-labels (cdr code-info))))))))))))
2653 ;;;; Statistics Cookie
2655 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2656 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2657 CONTENTS is nil. INFO is a plist holding contextual information."
2658 (replace-regexp-in-string
2659 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2662 ;;;; Strike-Through
2664 (defun org-latex-strike-through (strike-through contents info)
2665 "Transcode STRIKE-THROUGH from Org to LaTeX.
2666 CONTENTS is the text with strike-through markup. INFO is a plist
2667 holding contextual information."
2668 (org-latex--text-markup contents 'strike-through info))
2671 ;;;; Subscript
2673 (defun org-latex--script-size (object info)
2674 "Transcode a subscript or superscript object.
2675 OBJECT is an Org object. INFO is a plist used as a communication
2676 channel."
2677 (let ((type (org-element-type object))
2678 (output ""))
2679 (org-element-map (org-element-contents object)
2680 (cons 'plain-text org-element-all-objects)
2681 (lambda (obj)
2682 (case (org-element-type obj)
2683 ((entity latex-fragment)
2684 (let ((data (org-trim (org-export-data obj info))))
2685 (string-match
2686 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2687 data)
2688 (setq output
2689 (concat output
2690 (match-string 1 data)
2691 (let ((blank (org-element-property :post-blank obj)))
2692 (and blank (> blank 0) "\\ "))))))
2693 (plain-text
2694 (setq output
2695 (format "%s\\text{%s}" output (org-export-data obj info))))
2696 (otherwise
2697 (setq output
2698 (concat output
2699 (org-export-data obj info)
2700 (let ((blank (org-element-property :post-blank obj)))
2701 (and blank (> blank 0) "\\ ")))))))
2702 info nil org-element-recursive-objects)
2703 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2704 ;; character.
2705 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2706 (and (> (length output) 1) "{")
2707 output
2708 (and (> (length output) 1) "}"))))
2710 (defun org-latex-subscript (subscript contents info)
2711 "Transcode a SUBSCRIPT object from Org to LaTeX.
2712 CONTENTS is the contents of the object. INFO is a plist holding
2713 contextual information."
2714 (org-latex--script-size subscript info))
2717 ;;;; Superscript
2719 (defun org-latex-superscript (superscript contents info)
2720 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2721 CONTENTS is the contents of the object. INFO is a plist holding
2722 contextual information."
2723 (org-latex--script-size superscript info))
2726 ;;;; Table
2728 ;; `org-latex-table' is the entry point for table transcoding. It
2729 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2730 ;; delegates the job to either `org-latex--table.el-table',
2731 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2732 ;; depending of the type of the table and the mode requested.
2734 ;; `org-latex--align-string' is a subroutine used to build alignment
2735 ;; string for Org tables.
2737 (defun org-latex-table (table contents info)
2738 "Transcode a TABLE element from Org to LaTeX.
2739 CONTENTS is the contents of the table. INFO is a plist holding
2740 contextual information."
2741 (if (eq (org-element-property :type table) 'table.el)
2742 ;; "table.el" table. Convert it using appropriate tools.
2743 (org-latex--table.el-table table info)
2744 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2745 (plist-get info :latex-default-table-mode))))
2746 (cond
2747 ;; Case 1: Verbatim table.
2748 ((string= type "verbatim")
2749 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2750 ;; Re-create table, without affiliated keywords.
2751 (org-trim (org-element-interpret-data
2752 `(table nil ,@(org-element-contents table))))))
2753 ;; Case 2: Matrix.
2754 ((or (string= type "math") (string= type "inline-math"))
2755 (org-latex--math-table table info))
2756 ;; Case 3: Standard table.
2757 (t (concat (org-latex--org-table table contents info)
2758 ;; When there are footnote references within the
2759 ;; table, insert their definition just after it.
2760 (org-latex--delayed-footnotes-definitions table info)))))))
2762 (defun org-latex--align-string (table info)
2763 "Return an appropriate LaTeX alignment string.
2764 TABLE is the considered table. INFO is a plist used as
2765 a communication channel."
2766 (or (org-export-read-attribute :attr_latex table :align)
2767 (let (align)
2768 ;; Extract column groups and alignment from first (non-rule)
2769 ;; row.
2770 (org-element-map
2771 (org-element-map table 'table-row
2772 (lambda (row)
2773 (and (eq (org-element-property :type row) 'standard) row))
2774 info 'first-match)
2775 'table-cell
2776 (lambda (cell)
2777 (let ((borders (org-export-table-cell-borders cell info)))
2778 ;; Check left border for the first cell only.
2779 (when (and (memq 'left borders) (not align))
2780 (push "|" align))
2781 (push (case (org-export-table-cell-alignment cell info)
2782 (left "l")
2783 (right "r")
2784 (center "c"))
2785 align)
2786 (when (memq 'right borders) (push "|" align))))
2787 info)
2788 (apply 'concat (nreverse align)))))
2790 (defun org-latex--org-table (table contents info)
2791 "Return appropriate LaTeX code for an Org table.
2793 TABLE is the table type element to transcode. CONTENTS is its
2794 contents, as a string. INFO is a plist used as a communication
2795 channel.
2797 This function assumes TABLE has `org' as its `:type' property and
2798 `table' as its `:mode' attribute."
2799 (let* ((caption (org-latex--caption/label-string table info))
2800 (attr (org-export-read-attribute :attr_latex table))
2801 ;; Determine alignment string.
2802 (alignment (org-latex--align-string table info))
2803 ;; Determine environment for the table: longtable, tabular...
2804 (table-env (or (plist-get attr :environment)
2805 (plist-get info :latex-default-table-environment)))
2806 ;; If table is a float, determine environment: table, table*
2807 ;; or sidewaystable.
2808 (float-env (unless (member table-env '("longtable" "longtabu"))
2809 (let ((float (plist-get attr :float)))
2810 (cond
2811 ((and (not float) (plist-member attr :float)) nil)
2812 ((or (string= float "sidewaystable")
2813 (string= float "sideways")) "sidewaystable")
2814 ((string= float "multicolumn") "table*")
2815 ((or float
2816 (org-element-property :caption table)
2817 (org-string-nw-p (plist-get attr :caption)))
2818 "table")))))
2819 ;; Extract others display options.
2820 (fontsize (let ((font (plist-get attr :font)))
2821 (and font (concat font "\n"))))
2822 ;; "tabular" environment doesn't allow to define a width.
2823 (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
2824 (spreadp (plist-get attr :spread))
2825 (placement
2826 (or (plist-get attr :placement)
2827 (format "[%s]" (plist-get info :latex-default-figure-position))))
2828 (centerp (if (plist-member attr :center) (plist-get attr :center)
2829 (plist-get info :latex-tables-centered)))
2830 (caption-above-p (org-latex--caption-above-p table info)))
2831 ;; Prepare the final format string for the table.
2832 (cond
2833 ;; Longtable.
2834 ((equal "longtable" table-env)
2835 (concat (and fontsize (concat "{" fontsize))
2836 (format "\\begin{longtable}{%s}\n" alignment)
2837 (and caption-above-p
2838 (org-string-nw-p caption)
2839 (concat caption "\\\\\n"))
2840 contents
2841 (and (not caption-above-p)
2842 (org-string-nw-p caption)
2843 (concat caption "\\\\\n"))
2844 "\\end{longtable}\n"
2845 (and fontsize "}")))
2846 ;; Longtabu
2847 ((equal "longtabu" table-env)
2848 (concat (and fontsize (concat "{" fontsize))
2849 (format "\\begin{longtabu}%s{%s}\n"
2850 (if width
2851 (format " %s %s "
2852 (if spreadp "spread" "to") width) "")
2853 alignment)
2854 (and caption-above-p
2855 (org-string-nw-p caption)
2856 (concat caption "\\\\\n"))
2857 contents
2858 (and (not caption-above-p)
2859 (org-string-nw-p caption)
2860 (concat caption "\\\\\n"))
2861 "\\end{longtabu}\n"
2862 (and fontsize "}")))
2863 ;; Others.
2864 (t (concat (cond
2865 (float-env
2866 (concat (format "\\begin{%s}%s\n" float-env placement)
2867 (if caption-above-p caption "")
2868 (when centerp "\\centering\n")
2869 fontsize))
2870 (centerp (concat "\\begin{center}\n" fontsize))
2871 (fontsize (concat "{" fontsize)))
2872 (cond ((equal "tabu" table-env)
2873 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2874 (if width (format
2875 (if spreadp " spread %s " " to %s ")
2876 width) "")
2877 alignment
2878 contents))
2879 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2880 table-env
2881 (if width (format "{%s}" width) "")
2882 alignment
2883 contents
2884 table-env)))
2885 (cond
2886 (float-env
2887 (concat (if caption-above-p "" caption)
2888 (format "\n\\end{%s}" float-env)))
2889 (centerp "\n\\end{center}")
2890 (fontsize "}")))))))
2892 (defun org-latex--table.el-table (table info)
2893 "Return appropriate LaTeX code for a table.el table.
2895 TABLE is the table type element to transcode. INFO is a plist
2896 used as a communication channel.
2898 This function assumes TABLE has `table.el' as its `:type'
2899 property."
2900 (require 'table)
2901 ;; Ensure "*org-export-table*" buffer is empty.
2902 (with-current-buffer (get-buffer-create "*org-export-table*")
2903 (erase-buffer))
2904 (let ((output (with-temp-buffer
2905 (insert (org-element-property :value table))
2906 (goto-char 1)
2907 (re-search-forward "^[ \t]*|[^|]" nil t)
2908 (table-generate-source 'latex "*org-export-table*")
2909 (with-current-buffer "*org-export-table*"
2910 (org-trim (buffer-string))))))
2911 (kill-buffer (get-buffer "*org-export-table*"))
2912 ;; Remove left out comments.
2913 (while (string-match "^%.*\n" output)
2914 (setq output (replace-match "" t t output)))
2915 (let ((attr (org-export-read-attribute :attr_latex table)))
2916 (when (plist-get attr :rmlines)
2917 ;; When the "rmlines" attribute is provided, remove all hlines
2918 ;; but the the one separating heading from the table body.
2919 (let ((n 0) (pos 0))
2920 (while (and (< (length output) pos)
2921 (setq pos (string-match "^\\\\hline\n?" output pos)))
2922 (incf n)
2923 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2924 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2925 (plist-get info :latex-tables-centered))))
2926 (if (not centerp) output
2927 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2929 (defun org-latex--math-table (table info)
2930 "Return appropriate LaTeX code for a matrix.
2932 TABLE is the table type element to transcode. INFO is a plist
2933 used as a communication channel.
2935 This function assumes TABLE has `org' as its `:type' property and
2936 `inline-math' or `math' as its `:mode' attribute."
2937 (let* ((attr (org-export-read-attribute :attr_latex table))
2938 (env (or (plist-get attr :environment)
2939 (plist-get info :latex-default-table-environment)))
2940 (contents
2941 (mapconcat
2942 (lambda (row)
2943 ;; Ignore horizontal rules.
2944 (when (eq (org-element-property :type row) 'standard)
2945 ;; Return each cell unmodified.
2946 (concat
2947 (mapconcat
2948 (lambda (cell)
2949 (substring (org-element-interpret-data cell) 0 -1))
2950 (org-element-map row 'table-cell #'identity info) "&")
2951 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2952 "\n")))
2953 (org-element-map table 'table-row #'identity info) "")))
2954 (concat
2955 ;; Prefix.
2956 (plist-get attr :math-prefix)
2957 ;; Environment. Also treat special cases.
2958 (cond ((member env '("array" "tabular"))
2959 (let ((align (make-string
2960 (cdr (org-export-table-dimensions table info)) ?c)))
2961 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
2962 ((assoc env org-latex-table-matrix-macros)
2963 (format "\\%s%s{\n%s}"
2965 (or (plist-get attr :math-arguments) "")
2966 contents))
2967 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2968 ;; Suffix.
2969 (plist-get attr :math-suffix))))
2972 ;;;; Table Cell
2974 (defun org-latex-table-cell (table-cell contents info)
2975 "Transcode a TABLE-CELL element from Org to LaTeX.
2976 CONTENTS is the cell contents. INFO is a plist used as
2977 a communication channel."
2978 (concat
2979 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
2980 (if (and contents
2981 scientific-format
2982 (string-match orgtbl-exp-regexp contents))
2983 ;; Use appropriate format string for scientific
2984 ;; notation.
2985 (format scientific-format
2986 (match-string 1 contents)
2987 (match-string 2 contents))
2988 contents))
2989 (when (org-export-get-next-element table-cell info) " & ")))
2992 ;;;; Table Row
2994 (defun org-latex-table-row (table-row contents info)
2995 "Transcode a TABLE-ROW element from Org to LaTeX.
2996 CONTENTS is the contents of the row. INFO is a plist used as
2997 a communication channel."
2998 (let* ((attr (org-export-read-attribute :attr_latex
2999 (org-export-get-parent table-row)))
3000 (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
3001 (plist-get info :latex-tables-booktabs)))
3002 (longtablep
3003 (member (or (plist-get attr :environment)
3004 (plist-get info :latex-default-table-environment))
3005 '("longtable" "longtabu"))))
3006 (if (eq (org-element-property :type table-row) 'rule)
3007 (cond
3008 ((not booktabsp) "\\hline")
3009 ((not (org-export-get-previous-element table-row info)) "\\toprule")
3010 ((not (org-export-get-next-element table-row info)) "\\bottomrule")
3011 ((and longtablep
3012 (org-export-table-row-ends-header-p
3013 (org-export-get-previous-element table-row info) info))
3015 (t "\\midrule"))
3016 (concat
3017 ;; When BOOKTABS are activated enforce top-rule even when no
3018 ;; hline was specifically marked.
3019 (and booktabsp (not (org-export-get-previous-element table-row info))
3020 "\\toprule\n")
3021 contents "\\\\\n"
3022 (cond
3023 ;; Special case for long tables. Define header and footers.
3024 ((and longtablep (org-export-table-row-ends-header-p table-row info))
3025 (let ((columns (cdr (org-export-table-dimensions
3026 (org-export-get-parent-table table-row) info))))
3027 (format "%s
3028 \\endfirsthead
3029 \\multicolumn{%d}{l}{%s} \\\\
3031 %s \\\\\n
3033 \\endhead
3034 %s\\multicolumn{%d}{r}{%s} \\\\
3035 \\endfoot
3036 \\endlastfoot"
3037 (if booktabsp "\\midrule" "\\hline")
3038 columns
3039 (org-latex--translate "Continued from previous page" info)
3040 (cond
3041 ((not (org-export-table-row-starts-header-p table-row info))
3043 (booktabsp "\\toprule\n")
3044 (t "\\hline\n"))
3045 contents
3046 (if booktabsp "\\midrule" "\\hline")
3047 (if booktabsp "\\midrule" "\\hline")
3048 columns
3049 (org-latex--translate "Continued on next page" info))))
3050 ;; When BOOKTABS are activated enforce bottom rule even when
3051 ;; no hline was specifically marked.
3052 ((and booktabsp (not (org-export-get-next-element table-row info)))
3053 "\\bottomrule"))))))
3056 ;;;; Target
3058 (defun org-latex-target (target contents info)
3059 "Transcode a TARGET object from Org to LaTeX.
3060 CONTENTS is nil. INFO is a plist holding contextual
3061 information."
3062 (format "\\label{%s}" (org-latex--label target info)))
3065 ;;;; Timestamp
3067 (defun org-latex-timestamp (timestamp contents info)
3068 "Transcode a TIMESTAMP object from Org to LaTeX.
3069 CONTENTS is nil. INFO is a plist holding contextual
3070 information."
3071 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
3072 (format
3073 (plist-get info
3074 (case (org-element-property :type timestamp)
3075 ((active active-range) :latex-active-timestamp-format)
3076 ((inactive inactive-range) :latex-inactive-timestamp-format)
3077 (otherwise :latex-diary-timestamp-format)))
3078 value)))
3081 ;;;; Underline
3083 (defun org-latex-underline (underline contents info)
3084 "Transcode UNDERLINE from Org to LaTeX.
3085 CONTENTS is the text with underline markup. INFO is a plist
3086 holding contextual information."
3087 (org-latex--text-markup contents 'underline info))
3090 ;;;; Verbatim
3092 (defun org-latex-verbatim (verbatim contents info)
3093 "Transcode a VERBATIM object from Org to LaTeX.
3094 CONTENTS is nil. INFO is a plist used as a communication
3095 channel."
3096 (org-latex--text-markup
3097 (org-element-property :value verbatim) 'verbatim info))
3100 ;;;; Verse Block
3102 (defun org-latex-verse-block (verse-block contents info)
3103 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3104 CONTENTS is verse block contents. INFO is a plist holding
3105 contextual information."
3106 (org-latex--wrap-label
3107 verse-block
3108 ;; In a verse environment, add a line break to each newline
3109 ;; character and change each white space at beginning of a line
3110 ;; into a space of 1 em. Also change each blank line with
3111 ;; a vertical space of 1 em.
3112 (format "\\begin{verse}\n%s\\end{verse}"
3113 (replace-regexp-in-string
3114 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
3115 (replace-regexp-in-string
3116 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3117 (replace-regexp-in-string
3118 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3119 contents nil t) nil t) nil t))
3120 info))
3124 ;;; End-user functions
3126 ;;;###autoload
3127 (defun org-latex-export-as-latex
3128 (&optional async subtreep visible-only body-only ext-plist)
3129 "Export current buffer as a LaTeX buffer.
3131 If narrowing is active in the current buffer, only export its
3132 narrowed part.
3134 If a region is active, export that region.
3136 A non-nil optional argument ASYNC means the process should happen
3137 asynchronously. The resulting buffer should be accessible
3138 through the `org-export-stack' interface.
3140 When optional argument SUBTREEP is non-nil, export the sub-tree
3141 at point, extracting information from the headline properties
3142 first.
3144 When optional argument VISIBLE-ONLY is non-nil, don't export
3145 contents of hidden elements.
3147 When optional argument BODY-ONLY is non-nil, only write code
3148 between \"\\begin{document}\" and \"\\end{document}\".
3150 EXT-PLIST, when provided, is a property list with external
3151 parameters overriding Org default settings, but still inferior to
3152 file-local settings.
3154 Export is done in a buffer named \"*Org LATEX Export*\", which
3155 will be displayed when `org-export-show-temporary-export-buffer'
3156 is non-nil."
3157 (interactive)
3158 (org-export-to-buffer 'latex "*Org LATEX Export*"
3159 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
3161 ;;;###autoload
3162 (defun org-latex-convert-region-to-latex ()
3163 "Assume the current region has org-mode syntax, and convert it to LaTeX.
3164 This can be used in any buffer. For example, you can write an
3165 itemized list in org-mode syntax in an LaTeX buffer and use this
3166 command to convert it."
3167 (interactive)
3168 (org-export-replace-region-by 'latex))
3170 ;;;###autoload
3171 (defun org-latex-export-to-latex
3172 (&optional async subtreep visible-only body-only ext-plist)
3173 "Export current buffer to a LaTeX file.
3175 If narrowing is active in the current buffer, only export its
3176 narrowed part.
3178 If a region is active, export that region.
3180 A non-nil optional argument ASYNC means the process should happen
3181 asynchronously. The resulting file should be accessible through
3182 the `org-export-stack' interface.
3184 When optional argument SUBTREEP is non-nil, export the sub-tree
3185 at point, extracting information from the headline properties
3186 first.
3188 When optional argument VISIBLE-ONLY is non-nil, don't export
3189 contents of hidden elements.
3191 When optional argument BODY-ONLY is non-nil, only write code
3192 between \"\\begin{document}\" and \"\\end{document}\".
3194 EXT-PLIST, when provided, is a property list with external
3195 parameters overriding Org default settings, but still inferior to
3196 file-local settings."
3197 (interactive)
3198 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3199 (org-export-to-file 'latex outfile
3200 async subtreep visible-only body-only ext-plist)))
3202 ;;;###autoload
3203 (defun org-latex-export-to-pdf
3204 (&optional async subtreep visible-only body-only ext-plist)
3205 "Export current buffer to LaTeX then process through to PDF.
3207 If narrowing is active in the current buffer, only export its
3208 narrowed part.
3210 If a region is active, export that region.
3212 A non-nil optional argument ASYNC means the process should happen
3213 asynchronously. The resulting file should be accessible through
3214 the `org-export-stack' interface.
3216 When optional argument SUBTREEP is non-nil, export the sub-tree
3217 at point, extracting information from the headline properties
3218 first.
3220 When optional argument VISIBLE-ONLY is non-nil, don't export
3221 contents of hidden elements.
3223 When optional argument BODY-ONLY is non-nil, only write code
3224 between \"\\begin{document}\" and \"\\end{document}\".
3226 EXT-PLIST, when provided, is a property list with external
3227 parameters overriding Org default settings, but still inferior to
3228 file-local settings.
3230 Return PDF file's name."
3231 (interactive)
3232 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3233 (org-export-to-file 'latex outfile
3234 async subtreep visible-only body-only ext-plist
3235 (lambda (file) (org-latex-compile file)))))
3237 (defun org-latex-compile (texfile &optional snippet)
3238 "Compile a TeX file.
3240 TEXFILE is the name of the file being compiled. Processing is
3241 done through the command specified in `org-latex-pdf-process'.
3243 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3244 file used to preview a LaTeX snippet. In this case, do not
3245 create a log buffer and do not bother removing log files.
3247 Return PDF file name or an error if it couldn't be produced."
3248 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3249 (full-name (file-truename texfile))
3250 (out-dir (file-name-directory texfile))
3251 ;; Properly set working directory for compilation.
3252 (default-directory (if (file-name-absolute-p texfile)
3253 (file-name-directory full-name)
3254 default-directory))
3255 (time (current-time))
3256 warnings)
3257 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
3258 (save-window-excursion
3259 (cond
3260 ;; A function is provided: Apply it.
3261 ((functionp org-latex-pdf-process)
3262 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3263 ;; A list is provided: Replace %b, %f and %o with appropriate
3264 ;; values in each command before applying it. Output is
3265 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3266 ((consp org-latex-pdf-process)
3267 (let ((outbuf (and (not snippet)
3268 (get-buffer-create "*Org PDF LaTeX Output*"))))
3269 (dolist (command org-latex-pdf-process)
3270 (shell-command
3271 (replace-regexp-in-string
3272 "%b" (shell-quote-argument base-name)
3273 (replace-regexp-in-string
3274 "%f" (shell-quote-argument full-name)
3275 (replace-regexp-in-string
3276 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
3277 outbuf))
3278 ;; Collect standard errors from output buffer.
3279 (setq warnings (and (not snippet)
3280 (org-latex--collect-warnings outbuf)))))
3281 (t (error "No valid command to process to PDF")))
3282 (let ((pdffile (concat out-dir base-name ".pdf")))
3283 ;; Check for process failure. Provide collected errors if
3284 ;; possible.
3285 (if (or (not (file-exists-p pdffile))
3286 (time-less-p (nth 5 (file-attributes pdffile)) time))
3287 (error (format "PDF file %s wasn't produced" pdffile))
3288 ;; Else remove log files, when specified, and signal end of
3289 ;; process to user, along with any error encountered.
3290 (unless snippet
3291 (when org-latex-remove-logfiles
3292 (dolist (file (directory-files
3293 out-dir t
3294 (concat (regexp-quote base-name)
3295 "\\(?:\\.[0-9]+\\)?"
3296 "\\."
3297 (regexp-opt org-latex-logfiles-extensions))))
3298 (delete-file file)))
3299 (message (concat "PDF file produced"
3300 (cond
3301 ((eq warnings 'error) " with errors.")
3302 (warnings (concat " with warnings: " warnings))
3303 (t "."))))))
3304 ;; Return output file name.
3305 pdffile))))
3307 (defun org-latex--collect-warnings (buffer)
3308 "Collect some warnings from \"pdflatex\" command output.
3309 BUFFER is the buffer containing output. Return collected
3310 warnings types as a string, `error' if a LaTeX error was
3311 encountered or nil if there was none."
3312 (with-current-buffer buffer
3313 (save-excursion
3314 (goto-char (point-max))
3315 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3316 (if (re-search-forward "^!" nil t) 'error
3317 (let ((case-fold-search t)
3318 (warnings ""))
3319 (dolist (warning org-latex-known-warnings)
3320 (when (save-excursion (re-search-forward (car warning) nil t))
3321 (setq warnings (concat warnings " " (cdr warning)))))
3322 (org-string-nw-p (org-trim warnings))))))))
3324 ;;;###autoload
3325 (defun org-latex-publish-to-latex (plist filename pub-dir)
3326 "Publish an Org file to LaTeX.
3328 FILENAME is the filename of the Org file to be published. PLIST
3329 is the property list for the given project. PUB-DIR is the
3330 publishing directory.
3332 Return output file name."
3333 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3335 ;;;###autoload
3336 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3337 "Publish an Org file to PDF (via LaTeX).
3339 FILENAME is the filename of the Org file to be published. PLIST
3340 is the property list for the given project. PUB-DIR is the
3341 publishing directory.
3343 Return output file name."
3344 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3345 ;; in working directory and then moved to publishing directory.
3346 (org-publish-attachment
3347 plist
3348 (org-latex-compile
3349 (org-publish-org-to
3350 'latex filename ".tex" plist (file-name-directory filename)))
3351 pub-dir))
3354 (provide 'ox-latex)
3356 ;; Local variables:
3357 ;; generated-autoload-file: "org-loaddefs.el"
3358 ;; End:
3360 ;;; ox-latex.el ends here