Fix logging in indirect buffers
[org-mode.git] / lisp / ox-latex.el
blob192739ac7ac623eed1594c01e9c8e2aa5f067041
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 . verb)
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 :type 'alist
727 :options '(bold code italic strike-through underline verbatim))
730 ;;;; Drawers
732 (defcustom org-latex-format-drawer-function
733 (lambda (name contents) contents)
734 "Function called to format a drawer in LaTeX code.
736 The function must accept two parameters:
737 NAME the drawer name, like \"LOGBOOK\"
738 CONTENTS the contents of the drawer.
740 The function should return the string to be exported.
742 The default function simply returns the value of CONTENTS."
743 :group 'org-export-latex
744 :version "24.4"
745 :package-version '(Org . "8.3")
746 :type 'function)
749 ;;;; Inlinetasks
751 (defcustom org-latex-format-inlinetask-function
752 'org-latex-format-inlinetask-default-function
753 "Function called to format an inlinetask in LaTeX code.
755 The function must accept seven parameters:
756 TODO the todo keyword (string or nil)
757 TODO-TYPE the todo type (symbol: `todo', `done', nil)
758 PRIORITY the inlinetask priority (integer or nil)
759 NAME the inlinetask name (string)
760 TAGS the inlinetask tags (list of strings or nil)
761 CONTENTS the contents of the inlinetask (string or nil)
762 INFO the export options (plist)
764 The function should return the string to be exported."
765 :group 'org-export-latex
766 :type 'function
767 :version "25.1"
768 :package-version '(Org . "8.3"))
771 ;; Src blocks
773 (defcustom org-latex-listings nil
774 "Non-nil means export source code using the listings package.
776 This package will fontify source code, possibly even with color.
777 If you want to use this, you also need to make LaTeX use the
778 listings package, and if you want to have color, the color
779 package. Just add these to `org-latex-packages-alist', for
780 example using customize, or with something like:
782 \(require 'ox-latex)
783 \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
784 \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
786 Alternatively,
788 \(setq org-latex-listings 'minted)
790 causes source code to be exported using the minted package as
791 opposed to listings. If you want to use minted, you need to add
792 the minted package to `org-latex-packages-alist', for example
793 using customize, or with
795 \(require 'ox-latex)
796 \(add-to-list 'org-latex-packages-alist '(\"\" \"minted\"))
798 In addition, it is necessary to install pygments
799 \(http://pygments.org), and to configure the variable
800 `org-latex-pdf-process' so that the -shell-escape option is
801 passed to pdflatex.
803 The minted choice has possible repercussions on the preview of
804 latex fragments (see `org-preview-latex-fragment'). If you run
805 into previewing problems, please consult
807 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
808 :group 'org-export-latex
809 :type '(choice
810 (const :tag "Use listings" t)
811 (const :tag "Use minted" minted)
812 (const :tag "Export verbatim" nil))
813 :safe (lambda (s) (memq s '(t nil minted))))
815 (defcustom org-latex-listings-langs
816 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
817 (c "C") (cc "C++")
818 (fortran "fortran")
819 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
820 (html "HTML") (xml "XML")
821 (tex "TeX") (latex "[LaTeX]TeX")
822 (shell-script "bash")
823 (gnuplot "Gnuplot")
824 (ocaml "Caml") (caml "Caml")
825 (sql "SQL") (sqlite "sql")
826 (makefile "make"))
827 "Alist mapping languages to their listing language counterpart.
828 The key is a symbol, the major mode symbol without the \"-mode\".
829 The value is the string that should be inserted as the language
830 parameter for the listings package. If the mode name and the
831 listings name are the same, the language does not need an entry
832 in this list - but it does not hurt if it is present."
833 :group 'org-export-latex
834 :version "24.4"
835 :package-version '(Org . "8.3")
836 :type '(repeat
837 (list
838 (symbol :tag "Major mode ")
839 (string :tag "Listings language"))))
841 (defcustom org-latex-listings-options nil
842 "Association list of options for the latex listings package.
844 These options are supplied as a comma-separated list to the
845 \\lstset command. Each element of the association list should be
846 a list containing two strings: the name of the option, and the
847 value. For example,
849 \(setq org-latex-listings-options
850 '((\"basicstyle\" \"\\\\small\")
851 \(\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
853 will typeset the code in a small size font with underlined, bold
854 black keywords.
856 Note that the same options will be applied to blocks of all
857 languages. If you need block-specific options, you may use the
858 following syntax:
860 #+ATTR_LATEX: :options key1=value1,key2=value2
861 #+BEGIN_SRC <LANG>
863 #+END_SRC"
864 :group 'org-export-latex
865 :type '(repeat
866 (list
867 (string :tag "Listings option name ")
868 (string :tag "Listings option value"))))
870 (defcustom org-latex-minted-langs
871 '((emacs-lisp "common-lisp")
872 (cc "c++")
873 (cperl "perl")
874 (shell-script "bash")
875 (caml "ocaml"))
876 "Alist mapping languages to their minted language counterpart.
877 The key is a symbol, the major mode symbol without the \"-mode\".
878 The value is the string that should be inserted as the language
879 parameter for the minted package. If the mode name and the
880 listings name are the same, the language does not need an entry
881 in this list - but it does not hurt if it is present.
883 Note that minted uses all lower case for language identifiers,
884 and that the full list of language identifiers can be obtained
885 with:
887 pygmentize -L lexers"
888 :group 'org-export-latex
889 :type '(repeat
890 (list
891 (symbol :tag "Major mode ")
892 (string :tag "Minted language"))))
894 (defcustom org-latex-minted-options nil
895 "Association list of options for the latex minted package.
897 These options are supplied within square brackets in
898 \\begin{minted} environments. Each element of the alist should
899 be a list containing two strings: the name of the option, and the
900 value. For example,
902 \(setq org-latex-minted-options
903 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
905 will result in src blocks being exported with
907 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
909 as the start of the minted environment. Note that the same
910 options will be applied to blocks of all languages. If you need
911 block-specific options, you may use the following syntax:
913 #+ATTR_LATEX: :options key1=value1,key2=value2
914 #+BEGIN_SRC <LANG>
916 #+END_SRC"
917 :group 'org-export-latex
918 :type '(repeat
919 (list
920 (string :tag "Minted option name ")
921 (string :tag "Minted option value"))))
923 (defvar org-latex-custom-lang-environments nil
924 "Alist mapping languages to language-specific LaTeX environments.
926 It is used during export of src blocks by the listings and minted
927 latex packages. For example,
929 \(setq org-latex-custom-lang-environments
930 '\(\(python \"pythoncode\"\)\)\)
932 would have the effect that if org encounters begin_src python
933 during latex export it will output
935 \\begin{pythoncode}
936 <src block body>
937 \\end{pythoncode}")
940 ;;;; Compilation
942 (defcustom org-latex-pdf-process
943 '("pdflatex -interaction nonstopmode -output-directory %o %f"
944 "pdflatex -interaction nonstopmode -output-directory %o %f"
945 "pdflatex -interaction nonstopmode -output-directory %o %f")
946 "Commands to process a LaTeX file to a PDF file.
947 This is a list of strings, each of them will be given to the
948 shell as a command. %f in the command will be replaced by the
949 full file name, %b by the file base name (i.e. without directory
950 and extension parts) and %o by the base directory of the file.
952 The reason why this is a list is that it usually takes several
953 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
954 does not have a clever mechanism to detect which of these
955 commands have to be run to get to a stable result, and it also
956 does not do any error checking.
958 By default, Org uses 3 runs of `pdflatex' to do the processing.
959 If you have texi2dvi on your system and if that does not cause
960 the infamous egrep/locale bug:
962 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
964 then `texi2dvi' is the superior choice as it automates the LaTeX
965 build process by calling the \"correct\" combinations of
966 auxiliary programs. Org does offer `texi2dvi' as one of the
967 customize options. Alternatively, `rubber' and `latexmk' also
968 provide similar functionality. The latter supports `biber' out
969 of the box.
971 Alternatively, this may be a Lisp function that does the
972 processing, so you could use this to apply the machinery of
973 AUCTeX or the Emacs LaTeX mode. This function should accept the
974 file name as its single argument."
975 :group 'org-export-pdf
976 :type '(choice
977 (repeat :tag "Shell command sequence"
978 (string :tag "Shell command"))
979 (const :tag "2 runs of pdflatex"
980 ("pdflatex -interaction nonstopmode -output-directory %o %f"
981 "pdflatex -interaction nonstopmode -output-directory %o %f"))
982 (const :tag "3 runs of pdflatex"
983 ("pdflatex -interaction nonstopmode -output-directory %o %f"
984 "pdflatex -interaction nonstopmode -output-directory %o %f"
985 "pdflatex -interaction nonstopmode -output-directory %o %f"))
986 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
987 ("pdflatex -interaction nonstopmode -output-directory %o %f"
988 "bibtex %b"
989 "pdflatex -interaction nonstopmode -output-directory %o %f"
990 "pdflatex -interaction nonstopmode -output-directory %o %f"))
991 (const :tag "2 runs of xelatex"
992 ("xelatex -interaction nonstopmode -output-directory %o %f"
993 "xelatex -interaction nonstopmode -output-directory %o %f"))
994 (const :tag "3 runs of xelatex"
995 ("xelatex -interaction nonstopmode -output-directory %o %f"
996 "xelatex -interaction nonstopmode -output-directory %o %f"
997 "xelatex -interaction nonstopmode -output-directory %o %f"))
998 (const :tag "xelatex,bibtex,xelatex,xelatex"
999 ("xelatex -interaction nonstopmode -output-directory %o %f"
1000 "bibtex %b"
1001 "xelatex -interaction nonstopmode -output-directory %o %f"
1002 "xelatex -interaction nonstopmode -output-directory %o %f"))
1003 (const :tag "texi2dvi"
1004 ("texi2dvi -p -b -V %f"))
1005 (const :tag "rubber"
1006 ("rubber -d --into %o %f"))
1007 (const :tag "latexmk"
1008 ("latexmk -g -pdf %f"))
1009 (function)))
1011 (defcustom org-latex-logfiles-extensions
1012 '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
1013 "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
1014 "The list of file extensions to consider as LaTeX logfiles.
1015 The logfiles will be removed if `org-latex-remove-logfiles' is
1016 non-nil."
1017 :group 'org-export-latex
1018 :version "25.1"
1019 :package-version '(Org . "8.3")
1020 :type '(repeat (string :tag "Extension")))
1022 (defcustom org-latex-remove-logfiles t
1023 "Non-nil means remove the logfiles produced by PDF production.
1024 By default, logfiles are files with these extensions: .aux, .idx,
1025 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
1026 logfiles to remove, set `org-latex-logfiles-extensions'."
1027 :group 'org-export-latex
1028 :type 'boolean)
1030 (defcustom org-latex-known-warnings
1031 '(("Reference.*?undefined" . "[undefined reference]")
1032 ("Runaway argument" . "[runaway argument]")
1033 ("Underfull \\hbox" . "[underfull hbox]")
1034 ("Overfull \\hbox" . "[overfull hbox]")
1035 ("Citation.*?undefined" . "[undefined citation]")
1036 ("Undefined control sequence" . "[undefined control sequence]"))
1037 "Alist of regular expressions and associated messages for the user.
1038 The regular expressions are used to find possible warnings in the
1039 log of a latex-run. These warnings will be reported after
1040 calling `org-latex-compile'."
1041 :group 'org-export-latex
1042 :version "25.1"
1043 :package-version '(Org . "8.3")
1044 :type '(repeat
1045 (cons
1046 (string :tag "Regexp")
1047 (string :tag "Message"))))
1051 ;;; Internal Functions
1053 (defun org-latex--caption-above-p (element info)
1054 "Non nil when caption is expected to be located above ELEMENT.
1055 INFO is a plist holding contextual information."
1056 (let ((above (plist-get info :latex-caption-above)))
1057 (if (symbolp above) above
1058 (let ((type (org-element-type element)))
1059 (memq (if (eq type 'link) 'image type) above)))))
1061 (defun org-latex--label (datum info &optional force full)
1062 "Return an appropriate label for DATUM.
1063 DATUM is an element or a `target' type object. INFO is the
1064 current export state, as a plist.
1066 Return nil if element DATUM has no NAME or VALUE affiliated
1067 keyword or no CUSTOM_ID property, unless FORCE is non-nil. In
1068 this case always return a unique label.
1070 Eventually, if FULL is non-nil, wrap label within \"\\label{}\"."
1071 (let* ((type (org-element-type datum))
1072 (user-label
1073 (org-element-property
1074 (case type
1075 ((headline inlinetask) :CUSTOM_ID)
1076 (target :value)
1077 (otherwise :name))
1078 datum))
1079 (label
1080 (and (or user-label force)
1081 (if (and user-label (plist-get info :latex-prefer-user-labels))
1082 user-label
1083 (concat (case type
1084 (headline "sec:")
1085 (table "tab:")
1086 (latex-environment
1087 (and (org-string-match-p
1088 org-latex-math-environments-re
1089 (org-element-property :value datum))
1090 "eq:"))
1091 (paragraph
1092 (and (org-element-property :caption datum)
1093 "fig:")))
1094 (org-export-get-reference datum info))))))
1095 (cond ((not full) label)
1096 (label (format "\\label{%s}%s"
1097 label
1098 (if (eq type 'target) "" "\n")))
1099 (t ""))))
1101 (defun org-latex--caption/label-string (element info)
1102 "Return caption and label LaTeX string for ELEMENT.
1104 INFO is a plist holding contextual information. If there's no
1105 caption nor label, return the empty string.
1107 For non-floats, see `org-latex--wrap-label'."
1108 (let* ((label (org-latex--label element info nil t))
1109 (main (org-export-get-caption element))
1110 (short (org-export-get-caption element t))
1111 (caption-from-attr-latex
1112 (org-export-read-attribute :attr_latex element :caption)))
1113 (cond
1114 ((org-string-nw-p caption-from-attr-latex)
1115 (concat caption-from-attr-latex "\n"))
1116 ((and (not main) (equal label "")) "")
1117 ((not main) (concat label "\n"))
1118 ;; Option caption format with short name.
1119 (short (format "\\caption[%s]{%s%s}\n"
1120 (org-export-data short info)
1121 label
1122 (org-export-data main info)))
1123 ;; Standard caption format.
1124 (t (format "\\caption{%s%s}\n" label (org-export-data main info))))))
1126 (defun org-latex-guess-inputenc (header)
1127 "Set the coding system in inputenc to what the buffer is.
1129 HEADER is the LaTeX header string. This function only applies
1130 when specified inputenc option is \"AUTO\".
1132 Return the new header, as a string."
1133 (let* ((cs (or (ignore-errors
1134 (latexenc-coding-system-to-inputenc
1135 (or org-export-coding-system buffer-file-coding-system)))
1136 "utf8")))
1137 (if (not cs) header
1138 ;; First translate if that is requested.
1139 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
1140 ;; Then find the \usepackage statement and replace the option.
1141 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1142 cs header t nil 1))))
1144 (defun org-latex-guess-babel-language (header info)
1145 "Set Babel's language according to LANGUAGE keyword.
1147 HEADER is the LaTeX header string. INFO is the plist used as
1148 a communication channel.
1150 Insertion of guessed language only happens when Babel package has
1151 explicitly been loaded. Then it is added to the rest of
1152 package's options.
1154 The argument to Babel may be \"AUTO\" which is then replaced with
1155 the language of the document or `org-export-default-language'
1156 unless language in question is already loaded.
1158 Return the new header."
1159 (let ((language-code (plist-get info :language)))
1160 ;; If no language is set or Babel package is not loaded, return
1161 ;; HEADER as-is.
1162 (if (or (not (stringp language-code))
1163 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
1164 header
1165 (let ((options (save-match-data
1166 (org-split-string (match-string 1 header) ",[ \t]*")))
1167 (language (cdr (assoc language-code
1168 org-latex-babel-language-alist))))
1169 ;; If LANGUAGE is already loaded, return header without AUTO.
1170 ;; Otherwise, replace AUTO with language or append language if
1171 ;; AUTO is not present.
1172 (replace-match
1173 (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
1174 (cond ((member language options) (delete "AUTO" options))
1175 ((member "AUTO" options) options)
1176 (t (append options (list language))))
1177 ", ")
1178 t nil header 1)))))
1180 (defun org-latex--find-verb-separator (s)
1181 "Return a character not used in string S.
1182 This is used to choose a separator for constructs like \\verb."
1183 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1184 (loop for c across ll
1185 when (not (string-match (regexp-quote (char-to-string c)) s))
1186 return (char-to-string c))))
1188 (defun org-latex--make-option-string (options)
1189 "Return a comma separated string of keywords and values.
1190 OPTIONS is an alist where the key is the options keyword as
1191 a string, and the value a list containing the keyword value, or
1192 nil."
1193 (mapconcat (lambda (pair)
1194 (concat (first pair)
1195 (when (> (length (second pair)) 0)
1196 (concat "=" (second pair)))))
1197 options
1198 ","))
1200 (defun org-latex--wrap-label (element output info)
1201 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1202 INFO is the current export state, as a plist. This function
1203 should not be used for floats. See
1204 `org-latex--caption/label-string'."
1205 (if (not (and (org-string-nw-p output) (org-element-property :name element)))
1206 output
1207 (concat (format "\\phantomsection\n\\label{%s}\n"
1208 (org-latex--label element info))
1209 output)))
1211 (defun org-latex--text-markup (text markup info)
1212 "Format TEXT depending on MARKUP text markup.
1213 INFO is a plist used as a communication channel. See
1214 `org-latex-text-markup-alist' for details."
1215 (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
1216 (cond
1217 ;; No format string: Return raw text.
1218 ((not fmt) text)
1219 ;; Handle the `verb' special case: Find an appropriate separator
1220 ;; and use "\\verb" command.
1221 ((eq 'verb fmt)
1222 (let ((separator (org-latex--find-verb-separator text)))
1223 (concat "\\verb" separator
1224 (replace-regexp-in-string "\n" " " text)
1225 separator)))
1226 ;; Handle the `protectedtexttt' special case: Protect some
1227 ;; special chars and use "\texttt{%s}" format string.
1228 ((eq 'protectedtexttt fmt)
1229 (let ((start 0)
1230 (trans '(("\\" . "\\textbackslash{}")
1231 ("~" . "\\textasciitilde{}")
1232 ("^" . "\\textasciicircum{}")))
1233 (rtn "")
1234 char)
1235 (while (string-match "[\\{}$%&_#~^]" text)
1236 (setq char (match-string 0 text))
1237 (if (> (match-beginning 0) 0)
1238 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
1239 (setq text (substring text (1+ (match-beginning 0))))
1240 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1241 rtn (concat rtn char)))
1242 (setq text (concat rtn text)
1243 fmt "\\texttt{%s}")
1244 (while (string-match "--" text)
1245 (setq text (replace-match "-{}-" t t text)))
1246 (format fmt text)))
1247 ;; Else use format string.
1248 (t (format fmt text)))))
1250 (defun org-latex--delayed-footnotes-definitions (element info)
1251 "Return footnotes definitions in ELEMENT as a string.
1253 INFO is a plist used as a communication channel.
1255 Footnotes definitions are returned within \"\\footnotetxt{}\"
1256 commands.
1258 This function is used within constructs that don't support
1259 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1260 \"\\footnotemark\" is used within the construct and the function
1261 just outside of it."
1262 (mapconcat
1263 (lambda (ref)
1264 (format
1265 "\\footnotetext[%s]{%s}"
1266 (org-export-get-footnote-number ref info)
1267 (org-trim
1268 (org-export-data
1269 (org-export-get-footnote-definition ref info) info))))
1270 ;; Find every footnote reference in ELEMENT.
1271 (let* (all-refs
1272 search-refs ; For byte-compiler.
1273 (search-refs
1274 (function
1275 (lambda (data)
1276 ;; Return a list of all footnote references never seen
1277 ;; before in DATA.
1278 (org-element-map data 'footnote-reference
1279 (lambda (ref)
1280 (when (org-export-footnote-first-reference-p ref info)
1281 (push ref all-refs)
1282 (when (eq (org-element-property :type ref) 'standard)
1283 (funcall search-refs
1284 (org-export-get-footnote-definition ref info)))))
1285 info)
1286 (reverse all-refs)))))
1287 (funcall search-refs element))
1288 ""))
1290 (defun org-latex--translate (s info)
1291 "Translate string S according to specified language.
1292 INFO is a plist used as a communication channel."
1293 (org-export-translate s :latex info))
1295 (defun org-latex--format-spec (info)
1296 "Create a format-spec for document meta-data.
1297 INFO is a plist used as a communication channel."
1298 (let ((language (let ((lang (plist-get info :language)))
1299 (or (cdr (assoc lang org-latex-babel-language-alist))
1300 lang))))
1301 `((?a . ,(org-export-data (plist-get info :author) info))
1302 (?t . ,(org-export-data (plist-get info :title) info))
1303 (?k . ,(org-export-data (org-latex--wrap-latex-math-block
1304 (plist-get info :keywords) info)
1305 info))
1306 (?d . ,(org-export-data (org-latex--wrap-latex-math-block
1307 (plist-get info :description) info)
1308 info))
1309 (?c . ,(plist-get info :creator))
1310 (?l . ,language)
1311 (?L . ,(capitalize language))
1312 (?D . ,(org-export-get-date info)))))
1315 ;;; Template
1317 (defun org-latex-template (contents info)
1318 "Return complete document string after LaTeX conversion.
1319 CONTENTS is the transcoded contents string. INFO is a plist
1320 holding export options."
1321 (let ((title (org-export-data (plist-get info :title) info))
1322 (spec (org-latex--format-spec info)))
1323 (concat
1324 ;; Time-stamp.
1325 (and (plist-get info :time-stamp-file)
1326 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1327 ;; Document class and packages.
1328 (let* ((class (plist-get info :latex-class))
1329 (class-options (plist-get info :latex-class-options))
1330 (header (nth 1 (assoc class (plist-get info :latex-classes))))
1331 (document-class-string
1332 (and (stringp header)
1333 (if (not class-options) header
1334 (replace-regexp-in-string
1335 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1336 class-options header t nil 1)))))
1337 (if (not document-class-string)
1338 (user-error "Unknown LaTeX class `%s'" class)
1339 (org-latex-guess-babel-language
1340 (org-latex-guess-inputenc
1341 (org-element-normalize-string
1342 (org-splice-latex-header
1343 document-class-string
1344 org-latex-default-packages-alist
1345 org-latex-packages-alist nil
1346 (concat (org-element-normalize-string
1347 (plist-get info :latex-header))
1348 (plist-get info :latex-header-extra)))))
1349 info)))
1350 ;; Possibly limit depth for headline numbering.
1351 (let ((sec-num (plist-get info :section-numbers)))
1352 (when (integerp sec-num)
1353 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1354 ;; Author.
1355 (let ((author (and (plist-get info :with-author)
1356 (let ((auth (plist-get info :author)))
1357 (and auth (org-export-data auth info)))))
1358 (email (and (plist-get info :with-email)
1359 (org-export-data (plist-get info :email) info))))
1360 (cond ((and author email (not (string= "" email)))
1361 (format "\\author{%s\\thanks{%s}}\n" author email))
1362 ((or author email) (format "\\author{%s}\n" (or author email)))))
1363 ;; Date.
1364 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1365 (format "\\date{%s}\n" (org-export-data date info)))
1366 ;; Title and subtitle.
1367 (let* ((subtitle (plist-get info :subtitle))
1368 (formatted-subtitle
1369 (when subtitle
1370 (format (plist-get info :latex-subtitle-format)
1371 (org-export-data subtitle info))))
1372 (separate (plist-get info :latex-subtitle-separate)))
1373 (concat
1374 (format "\\title{%s%s}\n" title
1375 (if separate "" (or formatted-subtitle "")))
1376 (when (and separate subtitle)
1377 (concat formatted-subtitle "\n"))))
1378 ;; Hyperref options.
1379 (let ((template (plist-get info :latex-hyperref-template)))
1380 (and (stringp template)
1381 (format-spec template spec)))
1382 ;; Document start.
1383 "\\begin{document}\n\n"
1384 ;; Title command.
1385 (let* ((title-command (plist-get info :latex-title-command))
1386 (command (and (stringp title-command)
1387 (format-spec title-command spec))))
1388 (org-element-normalize-string
1389 (cond ((not (plist-get info :with-title)) nil)
1390 ((string= "" title) nil)
1391 ((not (stringp command)) nil)
1392 ((string-match "\\(?:[^%]\\|^\\)%s" command)
1393 (format command title))
1394 (t command))))
1395 ;; Table of contents.
1396 (let ((depth (plist-get info :with-toc)))
1397 (when depth
1398 (concat (when (wholenump depth)
1399 (format "\\setcounter{tocdepth}{%d}\n" depth))
1400 (plist-get info :latex-toc-command))))
1401 ;; Document's body.
1402 contents
1403 ;; Creator.
1404 (and (plist-get info :with-creator)
1405 (concat (plist-get info :creator) "\n"))
1406 ;; Document end.
1407 "\\end{document}")))
1411 ;;; Transcode Functions
1413 ;;;; Bold
1415 (defun org-latex-bold (bold contents info)
1416 "Transcode BOLD from Org to LaTeX.
1417 CONTENTS is the text with bold markup. INFO is a plist holding
1418 contextual information."
1419 (org-latex--text-markup contents 'bold info))
1422 ;;;; Center Block
1424 (defun org-latex-center-block (center-block contents info)
1425 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1426 CONTENTS holds the contents of the center block. INFO is a plist
1427 holding contextual information."
1428 (org-latex--wrap-label
1429 center-block (format "\\begin{center}\n%s\\end{center}" contents) info))
1432 ;;;; Clock
1434 (defun org-latex-clock (clock contents info)
1435 "Transcode a CLOCK element from Org to LaTeX.
1436 CONTENTS is nil. INFO is a plist holding contextual
1437 information."
1438 (concat
1439 "\\noindent"
1440 (format "\\textbf{%s} " org-clock-string)
1441 (format (plist-get info :latex-inactive-timestamp-format)
1442 (concat (org-timestamp-translate (org-element-property :value clock))
1443 (let ((time (org-element-property :duration clock)))
1444 (and time (format " (%s)" time)))))
1445 "\\\\"))
1448 ;;;; Code
1450 (defun org-latex-code (code contents info)
1451 "Transcode a CODE object from Org to LaTeX.
1452 CONTENTS is nil. INFO is a plist used as a communication
1453 channel."
1454 (org-latex--text-markup (org-element-property :value code) 'code info))
1457 ;;;; Drawer
1459 (defun org-latex-drawer (drawer contents info)
1460 "Transcode a DRAWER element from Org to LaTeX.
1461 CONTENTS holds the contents of the block. INFO is a plist
1462 holding contextual information."
1463 (let* ((name (org-element-property :drawer-name drawer))
1464 (output (funcall (plist-get info :latex-format-drawer-function)
1465 name contents)))
1466 (org-latex--wrap-label drawer output info)))
1469 ;;;; Dynamic Block
1471 (defun org-latex-dynamic-block (dynamic-block contents info)
1472 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1473 CONTENTS holds the contents of the block. INFO is a plist
1474 holding contextual information. See `org-export-data'."
1475 (org-latex--wrap-label dynamic-block contents info))
1478 ;;;; Entity
1480 (defun org-latex-entity (entity contents info)
1481 "Transcode an ENTITY object from Org to LaTeX.
1482 CONTENTS are the definition itself. INFO is a plist holding
1483 contextual information."
1484 (org-element-property :latex entity))
1487 ;;;; Example Block
1489 (defun org-latex-example-block (example-block contents info)
1490 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1491 CONTENTS is nil. INFO is a plist holding contextual
1492 information."
1493 (when (org-string-nw-p (org-element-property :value example-block))
1494 (org-latex--wrap-label
1495 example-block
1496 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1497 (org-export-format-code-default example-block info))
1498 info)))
1501 ;;;; Export Block
1503 (defun org-latex-export-block (export-block contents info)
1504 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1505 CONTENTS is nil. INFO is a plist holding contextual information."
1506 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1507 (org-remove-indentation (org-element-property :value export-block))))
1510 ;;;; Export Snippet
1512 (defun org-latex-export-snippet (export-snippet contents info)
1513 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1514 CONTENTS is nil. INFO is a plist holding contextual information."
1515 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1516 (org-element-property :value export-snippet)))
1519 ;;;; Fixed Width
1521 (defun org-latex-fixed-width (fixed-width contents info)
1522 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1523 CONTENTS is nil. INFO is a plist holding contextual information."
1524 (org-latex--wrap-label
1525 fixed-width
1526 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1527 (org-remove-indentation
1528 (org-element-property :value fixed-width)))
1529 info))
1532 ;;;; Footnote Reference
1534 (defun org-latex-footnote-reference (footnote-reference contents info)
1535 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1536 CONTENTS is nil. INFO is a plist holding contextual information."
1537 (concat
1538 ;; Insert separator between two footnotes in a row.
1539 (let ((prev (org-export-get-previous-element footnote-reference info)))
1540 (when (eq (org-element-type prev) 'footnote-reference)
1541 (plist-get info :latex-footnote-separator)))
1542 (cond
1543 ;; Use \footnotemark if the footnote has already been defined.
1544 ((not (org-export-footnote-first-reference-p footnote-reference info))
1545 (format "\\footnotemark[%s]{}"
1546 (org-export-get-footnote-number footnote-reference info)))
1547 ;; Use \footnotemark if reference is within another footnote
1548 ;; reference, footnote definition or table cell.
1549 ((org-element-lineage footnote-reference
1550 '(footnote-reference footnote-definition table-cell))
1551 "\\footnotemark")
1552 ;; Otherwise, define it with \footnote command.
1554 (let ((def (org-latex--wrap-latex-math-block
1555 (org-export-get-footnote-definition footnote-reference info) info)))
1556 (concat
1557 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1558 ;; Retrieve all footnote references within the footnote and
1559 ;; add their definition after it, since LaTeX doesn't support
1560 ;; them inside.
1561 (org-latex--delayed-footnotes-definitions def info)))))))
1564 ;;;; Headline
1566 (defun org-latex-headline (headline contents info)
1567 "Transcode a HEADLINE element from Org to LaTeX.
1568 CONTENTS holds the contents of the headline. INFO is a plist
1569 holding contextual information."
1570 (unless (org-element-property :footnote-section-p headline)
1571 (let* ((class (plist-get info :latex-class))
1572 (level (org-export-get-relative-level headline info))
1573 (numberedp (org-export-numbered-headline-p headline info))
1574 (class-sectioning (assoc class (plist-get info :latex-classes)))
1575 ;; Section formatting will set two placeholders: one for
1576 ;; the title and the other for the contents.
1577 (section-fmt
1578 (let ((sec (if (functionp (nth 2 class-sectioning))
1579 (funcall (nth 2 class-sectioning) level numberedp)
1580 (nth (1+ level) class-sectioning))))
1581 (cond
1582 ;; No section available for that LEVEL.
1583 ((not sec) nil)
1584 ;; Section format directly returned by a function. Add
1585 ;; placeholder for contents.
1586 ((stringp sec) (concat sec "\n%s"))
1587 ;; (numbered-section . unnumbered-section)
1588 ((not (consp (cdr sec)))
1589 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1590 ;; (numbered-open numbered-close)
1591 ((= (length sec) 2)
1592 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1593 ;; (num-in num-out no-num-in no-num-out)
1594 ((= (length sec) 4)
1595 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1596 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1597 ;; Create a temporary export back-end that hard-codes
1598 ;; "\underline" within "\section" and alike.
1599 (section-back-end
1600 (org-export-create-backend
1601 :parent 'latex
1602 :transcoders
1603 '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
1604 (text
1605 (org-export-data-with-backend
1606 (org-element-property :title headline) section-back-end info))
1607 (todo
1608 (and (plist-get info :with-todo-keywords)
1609 (let ((todo (org-element-property :todo-keyword headline)))
1610 (and todo (org-export-data todo info)))))
1611 (todo-type (and todo (org-element-property :todo-type headline)))
1612 (tags (and (plist-get info :with-tags)
1613 (org-export-get-tags headline info)))
1614 (priority (and (plist-get info :with-priority)
1615 (org-element-property :priority headline)))
1616 ;; Create the headline text along with a no-tag version.
1617 ;; The latter is required to remove tags from toc.
1618 (full-text (funcall (plist-get info :latex-format-headline-function)
1619 todo todo-type priority text tags info))
1620 ;; Associate \label to the headline for internal links.
1621 (headline-label (org-latex--label headline info t t))
1622 (pre-blanks
1623 (make-string (org-element-property :pre-blank headline) ?\n)))
1624 (if (or (not section-fmt) (org-export-low-level-p headline info))
1625 ;; This is a deep sub-tree: export it as a list item. Also
1626 ;; export as items headlines for which no section format has
1627 ;; been found.
1628 (let ((low-level-body
1629 (concat
1630 ;; If headline is the first sibling, start a list.
1631 (when (org-export-first-sibling-p headline info)
1632 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1633 ;; Itemize headline
1634 "\\item"
1635 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
1636 "\\relax")
1637 " " full-text "\n"
1638 headline-label
1639 pre-blanks
1640 contents)))
1641 ;; If headline is not the last sibling simply return
1642 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1643 ;; any blank line.
1644 (if (not (org-export-last-sibling-p headline info)) low-level-body
1645 (replace-regexp-in-string
1646 "[ \t\n]*\\'"
1647 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1648 low-level-body)))
1649 ;; This is a standard headline. Export it as a section. Add
1650 ;; an alternative heading when possible, and when this is not
1651 ;; identical to the usual heading.
1652 (let ((opt-title
1653 (funcall (plist-get info :latex-format-headline-function)
1654 todo todo-type priority
1655 (org-export-data-with-backend
1656 (org-export-get-alt-title headline info)
1657 section-back-end info)
1658 (and (eq (plist-get info :with-tags) t) tags)
1659 info))
1660 ;; Maybe end local TOC (see `org-latex-keyword').
1661 (contents
1662 (concat
1663 contents
1664 (let ((case-fold-search t)
1665 (section
1666 (let ((first (car (org-element-contents headline))))
1667 (and (eq (org-element-type first) 'section) first))))
1668 (org-element-map section 'keyword
1669 (lambda (k)
1670 (and (equal (org-element-property :key k) "TOC")
1671 (let ((v (org-element-property :value k)))
1672 (and (org-string-match-p "\\<headlines\\>" v)
1673 (org-string-match-p "\\<local\\>" v)
1674 (format "\\stopcontents[level-%d]" level)))))
1675 info t)))))
1676 (if (and opt-title
1677 (not (equal opt-title full-text))
1678 (string-match "\\`\\\\\\(.+?\\){" section-fmt))
1679 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1680 ;; Replace square brackets with parenthesis
1681 ;; since square brackets are not supported in
1682 ;; optional arguments.
1683 (replace-regexp-in-string
1684 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1685 full-text
1686 (concat headline-label pre-blanks contents))
1687 ;; Impossible to add an alternative heading. Fallback to
1688 ;; regular sectioning format string.
1689 (format section-fmt full-text
1690 (concat headline-label pre-blanks contents))))))))
1692 (defun org-latex-format-headline-default-function
1693 (todo todo-type priority text tags info)
1694 "Default format function for a headline.
1695 See `org-latex-format-headline-function' for details."
1696 (concat
1697 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1698 (and priority (format "\\framebox{\\#%c} " priority))
1699 text
1700 (and tags
1701 (format "\\hfill{}\\textsc{%s}"
1702 (mapconcat (lambda (tag) (org-latex-plain-text tag info))
1703 tags ":")))))
1706 ;;;; Horizontal Rule
1708 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1709 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1710 CONTENTS is nil. INFO is a plist holding contextual information."
1711 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1712 (prev (org-export-get-previous-element horizontal-rule info)))
1713 (concat
1714 ;; Make sure the rule doesn't start at the end of the current
1715 ;; line by separating it with a blank line from previous element.
1716 (when (and prev
1717 (let ((prev-blank (org-element-property :post-blank prev)))
1718 (or (not prev-blank) (zerop prev-blank))))
1719 "\n")
1720 (org-latex--wrap-label
1721 horizontal-rule
1722 (format "\\rule{%s}{%s}"
1723 (or (plist-get attr :width) "\\linewidth")
1724 (or (plist-get attr :thickness) "0.5pt"))
1725 info))))
1728 ;;;; Inline Src Block
1730 (defun org-latex-inline-src-block (inline-src-block contents info)
1731 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1732 CONTENTS holds the contents of the item. INFO is a plist holding
1733 contextual information."
1734 (let* ((code (org-element-property :value inline-src-block))
1735 (separator (org-latex--find-verb-separator code)))
1736 (case (plist-get info :latex-listings)
1737 ;; Do not use a special package: transcode it verbatim.
1738 ((nil) (concat "\\verb" separator code separator))
1739 ;; Use minted package.
1740 (minted
1741 (let* ((org-lang (org-element-property :language inline-src-block))
1742 (mint-lang (or (cadr (assq (intern org-lang)
1743 (plist-get info :latex-minted-langs)))
1744 (downcase org-lang)))
1745 (options (org-latex--make-option-string
1746 (plist-get info :latex-minted-options))))
1747 (concat (format "\\mint%s{%s}"
1748 (if (string= options "") "" (format "[%s]" options))
1749 mint-lang)
1750 separator code separator)))
1751 ;; Use listings package.
1752 (otherwise
1753 ;; Maybe translate language's name.
1754 (let* ((org-lang (org-element-property :language inline-src-block))
1755 (lst-lang (or (cadr (assq (intern org-lang)
1756 (plist-get info :latex-listings-langs)))
1757 org-lang))
1758 (options (org-latex--make-option-string
1759 (append (plist-get info :latex-listings-options)
1760 `(("language" ,lst-lang))))))
1761 (concat (format "\\lstinline[%s]" options)
1762 separator code separator))))))
1765 ;;;; Inlinetask
1767 (defun org-latex-inlinetask (inlinetask contents info)
1768 "Transcode an INLINETASK element from Org to LaTeX.
1769 CONTENTS holds the contents of the block. INFO is a plist
1770 holding contextual information."
1771 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1772 (todo (and (plist-get info :with-todo-keywords)
1773 (let ((todo (org-element-property :todo-keyword inlinetask)))
1774 (and todo (org-export-data todo info)))))
1775 (todo-type (org-element-property :todo-type inlinetask))
1776 (tags (and (plist-get info :with-tags)
1777 (org-export-get-tags inlinetask info)))
1778 (priority (and (plist-get info :with-priority)
1779 (org-element-property :priority inlinetask)))
1780 (contents (concat (org-latex--label inlinetask info) contents)))
1781 (funcall (plist-get info :latex-format-inlinetask-function)
1782 todo todo-type priority title tags contents info)))
1784 (defun org-latex-format-inlinetask-default-function
1785 (todo todo-type priority title tags contents info)
1786 "Default format function for a inlinetasks.
1787 See `org-latex-format-inlinetask-function' for details."
1788 (let ((full-title
1789 (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1790 (when priority (format "\\framebox{\\#%c} " priority))
1791 title
1792 (when tags
1793 (format "\\hfill{}\\textsc{:%s:}"
1794 (mapconcat
1795 (lambda (tag) (org-latex-plain-text tag info))
1796 tags ":"))))))
1797 (concat "\\begin{center}\n"
1798 "\\fbox{\n"
1799 "\\begin{minipage}[c]{.6\\textwidth}\n"
1800 full-title "\n\n"
1801 (and (org-string-nw-p contents)
1802 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
1803 "\\end{minipage}\n"
1804 "}\n"
1805 "\\end{center}")))
1808 ;;;; Italic
1810 (defun org-latex-italic (italic contents info)
1811 "Transcode ITALIC from Org to LaTeX.
1812 CONTENTS is the text with italic markup. INFO is a plist holding
1813 contextual information."
1814 (org-latex--text-markup contents 'italic info))
1817 ;;;; Item
1819 (defun org-latex-item (item contents info)
1820 "Transcode an ITEM element from Org to LaTeX.
1821 CONTENTS holds the contents of the item. INFO is a plist holding
1822 contextual information."
1823 (let* ((counter
1824 (let ((count (org-element-property :counter item))
1825 (level
1826 ;; Determine level of current item to determine the
1827 ;; correct LaTeX counter to use (enumi, enumii...).
1828 (let ((parent item) (level 0))
1829 (while (memq (org-element-type
1830 (setq parent (org-export-get-parent parent)))
1831 '(plain-list item))
1832 (when (and (eq (org-element-type parent) 'plain-list)
1833 (eq (org-element-property :type parent)
1834 'ordered))
1835 (incf level)))
1836 level)))
1837 (and count
1838 (< level 5)
1839 (format "\\setcounter{enum%s}{%s}\n"
1840 (nth (1- level) '("i" "ii" "iii" "iv"))
1841 (1- count)))))
1842 (checkbox (case (org-element-property :checkbox item)
1843 (on "$\\boxtimes$ ")
1844 (off "$\\square$ ")
1845 (trans "$\\boxminus$ ")))
1846 (tag (let ((tag (org-element-property :tag item)))
1847 ;; Check-boxes must belong to the tag.
1848 (and tag (format "[{%s}] "
1849 (concat checkbox
1850 (org-export-data tag info)))))))
1851 (concat counter
1852 "\\item"
1853 (cond
1854 (tag)
1855 (checkbox (concat " " checkbox))
1856 ;; Without a tag or a check-box, if CONTENTS starts with
1857 ;; an opening square bracket, add "\relax" to "\item",
1858 ;; unless the brackets comes from an initial export
1859 ;; snippet (i.e. it is inserted willingly by the user).
1860 ((and contents
1861 (org-string-match-p "\\`[ \t]*\\[" contents)
1862 (not (let ((e (car (org-element-contents item))))
1863 (and (eq (org-element-type e) 'paragraph)
1864 (let ((o (car (org-element-contents e))))
1865 (and (eq (org-element-type o) 'export-snippet)
1866 (eq (org-export-snippet-backend o)
1867 'latex)))))))
1868 "\\relax ")
1869 (t " "))
1870 (and contents (org-trim contents))
1871 ;; If there are footnotes references in tag, be sure to
1872 ;; add their definition at the end of the item. This
1873 ;; workaround is necessary since "\footnote{}" command is
1874 ;; not supported in tags.
1875 (and tag
1876 (org-latex--delayed-footnotes-definitions
1877 (org-element-property :tag item) info)))))
1880 ;;;; Keyword
1882 (defun org-latex-keyword (keyword contents info)
1883 "Transcode a KEYWORD element from Org to LaTeX.
1884 CONTENTS is nil. INFO is a plist holding contextual information."
1885 (let ((key (org-element-property :key keyword))
1886 (value (org-element-property :value keyword)))
1887 (cond
1888 ((string= key "LATEX") value)
1889 ((string= key "INDEX") (format "\\index{%s}" value))
1890 ((string= key "TOC")
1891 (let ((case-fold-search t))
1892 (cond
1893 ((org-string-match-p "\\<headlines\\>" value)
1894 (let* ((localp (org-string-match-p "\\<local\\>" value))
1895 (parent (org-element-lineage keyword '(headline)))
1896 (level (if (not (and localp parent)) 0
1897 (org-export-get-relative-level parent info)))
1898 (depth
1899 (and (string-match "\\<[0-9]+\\>" value)
1900 (format
1901 "\\setcounter{tocdepth}{%d}"
1902 (+ (string-to-number (match-string 0 value)) level)))))
1903 (if (and localp parent)
1904 ;; Start local TOC, assuming package "titletoc" is
1905 ;; required.
1906 (format "\\startcontents[level-%d]
1907 \\printcontents[level-%d]{}{0}{%s}"
1908 level level (or depth ""))
1909 (concat depth (and depth "\n") "\\tableofcontents"))))
1910 ((org-string-match-p "\\<tables\\>" value) "\\listoftables")
1911 ((org-string-match-p "\\<listings\\>" value)
1912 (case (plist-get info :latex-listings)
1913 ((nil) "\\listoffigures")
1914 (minted "\\listoflistings")
1915 (otherwise "\\lstlistoflistings")))))))))
1918 ;;;; Latex Environment
1920 (defun org-latex-latex-environment (latex-environment contents info)
1921 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1922 CONTENTS is nil. INFO is a plist holding contextual information."
1923 (when (plist-get info :with-latex)
1924 (let ((value (org-remove-indentation
1925 (org-element-property :value latex-environment))))
1926 (if (not (org-element-property :name latex-environment)) value
1927 ;; Environment is labeled: label must be within the environment
1928 ;; (otherwise, a reference pointing to that element will count
1929 ;; the section instead).
1930 (with-temp-buffer
1931 (insert value)
1932 (goto-char (point-min))
1933 (forward-line)
1934 (insert (org-latex--label latex-environment info nil t))
1935 (buffer-string))))))
1938 ;;;; Latex Fragment
1940 (defun org-latex-latex-fragment (latex-fragment contents info)
1941 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1942 CONTENTS is nil. INFO is a plist holding contextual information."
1943 (let ((value (org-element-property :value latex-fragment)))
1944 ;; Trim math markers since the fragment is enclosed within
1945 ;; a latex-math-block object anyway.
1946 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
1947 (match-string 2 value))
1948 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
1949 (match-string 1 value))
1950 (t value))))
1953 ;;;; Line Break
1955 (defun org-latex-line-break (line-break contents info)
1956 "Transcode a LINE-BREAK object from Org to LaTeX.
1957 CONTENTS is nil. INFO is a plist holding contextual information."
1958 "\\\\\n")
1961 ;;;; Link
1963 (defun org-latex--inline-image (link info)
1964 "Return LaTeX code for an inline image.
1965 LINK is the link pointing to the inline image. INFO is a plist
1966 used as a communication channel."
1967 (let* ((parent (org-export-get-parent-element link))
1968 (path (let ((raw-path (org-element-property :path link)))
1969 (if (not (file-name-absolute-p raw-path)) raw-path
1970 (expand-file-name raw-path))))
1971 (filetype (file-name-extension path))
1972 (caption (org-latex--caption/label-string parent info))
1973 (caption-above-p (org-latex--caption-above-p link info))
1974 ;; Retrieve latex attributes from the element around.
1975 (attr (org-export-read-attribute :attr_latex parent))
1976 (float (let ((float (plist-get attr :float)))
1977 (cond ((and (not float) (plist-member attr :float)) nil)
1978 ((string= float "wrap") 'wrap)
1979 ((string= float "sideways") 'sideways)
1980 ((string= float "multicolumn") 'multicolumn)
1981 ((or float
1982 (org-element-property :caption parent)
1983 (org-string-nw-p (plist-get attr :caption)))
1984 'figure))))
1985 (placement
1986 (let ((place (plist-get attr :placement)))
1987 (cond
1988 (place (format "%s" place))
1989 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1990 ((eq float 'figure)
1991 (format "[%s]" (plist-get info :latex-default-figure-position)))
1992 (t ""))))
1993 (comment-include (if (plist-get attr :comment-include) "%" ""))
1994 ;; It is possible to specify width and height in the
1995 ;; ATTR_LATEX line, and also via default variables.
1996 (width (cond ((plist-get attr :width))
1997 ((plist-get attr :height) "")
1998 ((eq float 'wrap) "0.48\\textwidth")
1999 (t (plist-get info :latex-image-default-width))))
2000 (height (cond ((plist-get attr :height))
2001 ((or (plist-get attr :width)
2002 (memq float '(figure wrap))) "")
2003 (t (plist-get info :latex-image-default-height))))
2004 (options (let ((opt (or (plist-get attr :options)
2005 (plist-get info :latex-image-default-option))))
2006 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
2007 (match-string 1 opt))))
2008 image-code)
2009 (if (member filetype '("tikz" "pgf"))
2010 ;; For tikz images:
2011 ;; - use \input to read in image file.
2012 ;; - if options are present, wrap in a tikzpicture environment.
2013 ;; - if width or height are present, use \resizebox to change
2014 ;; the image size.
2015 (progn
2016 (setq image-code (format "\\input{%s}" path))
2017 (when (org-string-nw-p options)
2018 (setq image-code
2019 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
2020 options
2021 image-code)))
2022 (when (or (org-string-nw-p width) (org-string-nw-p height))
2023 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
2024 (if (org-string-nw-p width) width "!")
2025 (if (org-string-nw-p height) height "!")
2026 image-code))))
2027 ;; For other images:
2028 ;; - add width and height to options.
2029 ;; - include the image with \includegraphics.
2030 (when (org-string-nw-p width)
2031 (setq options (concat options ",width=" width)))
2032 (when (org-string-nw-p height)
2033 (setq options (concat options ",height=" height)))
2034 (let ((search-option (org-element-property :search-option link)))
2035 (when (and search-option
2036 (equal filetype "pdf")
2037 (org-string-match-p "\\`[0-9]+\\'" search-option)
2038 (not (org-string-match-p "page=" options)))
2039 (setq options (concat options ",page=" search-option))))
2040 (setq image-code
2041 (format "\\includegraphics%s{%s}"
2042 (cond ((not (org-string-nw-p options)) "")
2043 ((= (aref options 0) ?,)
2044 (format "[%s]"(substring options 1)))
2045 (t (format "[%s]" options)))
2046 path))
2047 (when (equal filetype "svg")
2048 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
2049 "\\includesvg"
2050 image-code
2051 nil t))
2052 (setq image-code (replace-regexp-in-string "\\.svg}"
2054 image-code
2055 nil t))))
2056 ;; Return proper string, depending on FLOAT.
2057 (case float
2058 (wrap (format "\\begin{wrapfigure}%s
2059 %s\\centering
2060 %s%s
2061 %s\\end{wrapfigure}"
2062 placement
2063 (if caption-above-p caption "")
2064 comment-include image-code
2065 (if caption-above-p "" caption)))
2066 (sideways (format "\\begin{sidewaysfigure}
2067 %s\\centering
2068 %s%s
2069 %s\\end{sidewaysfigure}"
2070 (if caption-above-p caption "")
2071 comment-include image-code
2072 (if caption-above-p "" caption)))
2073 (multicolumn (format "\\begin{figure*}%s
2074 %s\\centering
2075 %s%s
2076 %s\\end{figure*}"
2077 placement
2078 (if caption-above-p caption "")
2079 comment-include image-code
2080 (if caption-above-p "" caption)))
2081 (figure (format "\\begin{figure}%s
2082 %s\\centering
2083 %s%s
2084 %s\\end{figure}"
2085 placement
2086 (if caption-above-p caption "")
2087 comment-include image-code
2088 (if caption-above-p "" caption)))
2089 (otherwise image-code))))
2091 (defun org-latex-link (link desc info)
2092 "Transcode a LINK object from Org to LaTeX.
2094 DESC is the description part of the link, or the empty string.
2095 INFO is a plist holding contextual information. See
2096 `org-export-data'."
2097 (let* ((type (org-element-property :type link))
2098 (raw-path (replace-regexp-in-string
2099 "%" "\\%" (org-element-property :path link) nil t))
2100 ;; Ensure DESC really exists, or set it to nil.
2101 (desc (and (not (string= desc "")) desc))
2102 (imagep (org-export-inline-image-p
2103 link (plist-get info :latex-inline-image-rules)))
2104 (path (cond
2105 ((member type '("http" "https" "ftp" "mailto" "doi"))
2106 (concat type ":" raw-path))
2107 ((string= type "file") (org-export-file-uri raw-path))
2108 (t raw-path))))
2109 (cond
2110 ;; Link type is handled by a special function.
2111 ((org-export-custom-protocol-maybe link desc 'latex))
2112 ;; Image file.
2113 (imagep (org-latex--inline-image link info))
2114 ;; Radio link: Transcode target's contents and use them as link's
2115 ;; description.
2116 ((string= type "radio")
2117 (let ((destination (org-export-resolve-radio-link link info)))
2118 (if (not destination) desc
2119 (format "\\hyperref[%s]{%s}"
2120 (org-export-get-reference destination info)
2121 desc))))
2122 ;; Links pointing to a headline: Find destination and build
2123 ;; appropriate referencing command.
2124 ((member type '("custom-id" "fuzzy" "id"))
2125 (let ((destination (if (string= type "fuzzy")
2126 (org-export-resolve-fuzzy-link link info)
2127 (org-export-resolve-id-link link info))))
2128 (case (org-element-type destination)
2129 ;; Id link points to an external file.
2130 (plain-text
2131 (if desc (format "\\href{%s}{%s}" destination desc)
2132 (format "\\url{%s}" destination)))
2133 ;; Fuzzy link points nowhere.
2134 ((nil)
2135 (format (plist-get info :latex-link-with-unknown-path-format)
2136 (or desc
2137 (org-export-data
2138 (org-element-property :raw-link link) info))))
2139 ;; LINK points to a headline. If headlines are numbered
2140 ;; and the link has no description, display headline's
2141 ;; number. Otherwise, display description or headline's
2142 ;; title.
2143 (headline
2144 (let ((label (org-latex--label destination info t)))
2145 (if (and (not desc)
2146 (org-export-numbered-headline-p destination info))
2147 (format "\\ref{%s}" label)
2148 (format "\\hyperref[%s]{%s}" label
2149 (or desc
2150 (org-export-data
2151 (org-element-property :title destination) info))))))
2152 ;; Fuzzy link points to a target. Do as above.
2153 (otherwise
2154 (let ((ref (org-latex--label destination info t)))
2155 (if (not desc) (format "\\ref{%s}" ref)
2156 (format "\\hyperref[%s]{%s}" ref desc)))))))
2157 ;; Coderef: replace link with the reference name or the
2158 ;; equivalent line number.
2159 ((string= type "coderef")
2160 (format (org-export-get-coderef-format path desc)
2161 (org-export-resolve-coderef path info)))
2162 ;; External link with a description part.
2163 ((and path desc) (format "\\href{%s}{%s}" path desc))
2164 ;; External link without a description part.
2165 (path (format "\\url{%s}" path))
2166 ;; No path, only description. Try to do something useful.
2167 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
2170 ;;;; Node Property
2172 (defun org-latex-node-property (node-property contents info)
2173 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2174 CONTENTS is nil. INFO is a plist holding contextual
2175 information."
2176 (format "%s:%s"
2177 (org-element-property :key node-property)
2178 (let ((value (org-element-property :value node-property)))
2179 (if value (concat " " value) ""))))
2182 ;;;; Paragraph
2184 (defun org-latex-paragraph (paragraph contents info)
2185 "Transcode a PARAGRAPH element from Org to LaTeX.
2186 CONTENTS is the contents of the paragraph, as a string. INFO is
2187 the plist used as a communication channel."
2188 contents)
2191 ;;;; Plain List
2193 (defun org-latex-plain-list (plain-list contents info)
2194 "Transcode a PLAIN-LIST element from Org to LaTeX.
2195 CONTENTS is the contents of the list. INFO is a plist holding
2196 contextual information."
2197 (let* ((type (org-element-property :type plain-list))
2198 (attr (org-export-read-attribute :attr_latex plain-list))
2199 (latex-type (let ((env (plist-get attr :environment)))
2200 (cond (env (format "%s" env))
2201 ((eq type 'ordered) "enumerate")
2202 ((eq type 'descriptive) "description")
2203 (t "itemize")))))
2204 (org-latex--wrap-label
2205 plain-list
2206 (format "\\begin{%s}%s\n%s\\end{%s}"
2207 latex-type
2208 (or (plist-get attr :options) "")
2209 contents
2210 latex-type)
2211 info)))
2214 ;;;; Plain Text
2216 (defun org-latex-plain-text (text info)
2217 "Transcode a TEXT string from Org to LaTeX.
2218 TEXT is the string to transcode. INFO is a plist holding
2219 contextual information."
2220 (let* ((specialp (plist-get info :with-special-strings))
2221 (output
2222 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2223 (let ((case-fold-search nil))
2224 (replace-regexp-in-string
2225 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2226 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2227 ;; However, if special strings are used, be careful not
2228 ;; to protect "\" in "\-" constructs.
2229 (replace-regexp-in-string
2230 (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\(?:[^-]\\|$\\)"))
2231 (lambda (m)
2232 (case (aref m 0)
2233 (?\\ "$\\\\backslash$")
2234 (?~ "\\\\textasciitilde{}")
2235 (?^ "\\\\^{}")
2236 (t "\\\\\\&")))
2237 text)))))
2238 ;; Activate smart quotes. Be sure to provide original TEXT string
2239 ;; since OUTPUT may have been modified.
2240 (when (plist-get info :with-smart-quotes)
2241 (setq output (org-export-activate-smart-quotes output :latex info text)))
2242 ;; Convert special strings.
2243 (when specialp
2244 (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
2245 ;; Handle break preservation if required.
2246 (when (plist-get info :preserve-breaks)
2247 (setq output (replace-regexp-in-string
2248 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
2249 ;; Return value.
2250 output))
2253 ;;;; Planning
2255 (defun org-latex-planning (planning contents info)
2256 "Transcode a PLANNING element from Org to LaTeX.
2257 CONTENTS is nil. INFO is a plist holding contextual
2258 information."
2259 (concat
2260 "\\noindent"
2261 (mapconcat
2262 'identity
2263 (delq nil
2264 (list
2265 (let ((closed (org-element-property :closed planning)))
2266 (when closed
2267 (concat
2268 (format "\\textbf{%s} " org-closed-string)
2269 (format (plist-get info :latex-inactive-timestamp-format)
2270 (org-timestamp-translate closed)))))
2271 (let ((deadline (org-element-property :deadline planning)))
2272 (when deadline
2273 (concat
2274 (format "\\textbf{%s} " org-deadline-string)
2275 (format (plist-get info :latex-active-timestamp-format)
2276 (org-timestamp-translate deadline)))))
2277 (let ((scheduled (org-element-property :scheduled planning)))
2278 (when scheduled
2279 (concat
2280 (format "\\textbf{%s} " org-scheduled-string)
2281 (format (plist-get info :latex-active-timestamp-format)
2282 (org-timestamp-translate scheduled)))))))
2283 " ")
2284 "\\\\"))
2287 ;;;; Property Drawer
2289 (defun org-latex-property-drawer (property-drawer contents info)
2290 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2291 CONTENTS holds the contents of the drawer. INFO is a plist
2292 holding contextual information."
2293 (and (org-string-nw-p contents)
2294 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2297 ;;;; Pseudo Element: LaTeX Matrices
2299 ;; `latex-matrices' elements have the following properties:
2300 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2301 ;; `math').
2303 (defun org-latex--wrap-latex-matrices (data info)
2304 "Merge contiguous tables with the same mode within a pseudo-element.
2305 DATA is a parse tree or a secondary string. INFO is a plist
2306 containing export options. Modify DATA by side-effect and return
2307 it."
2308 (org-element-map data 'table
2309 (lambda (table)
2310 (when (eq (org-element-property :type table) 'org)
2311 (let ((mode (or (org-export-read-attribute :attr_latex table :mode)
2312 (plist-get info :latex-default-table-mode))))
2313 (when (and (member mode '("inline-math" "math"))
2314 ;; Do not wrap twice the same table.
2315 (not (eq (org-element-type
2316 (org-element-property :parent table))
2317 'latex-matrices)))
2318 (let* ((caption (and (not (string= mode "inline-math"))
2319 (org-element-property :caption table)))
2320 (matrices
2321 (list 'latex-matrices
2322 (list :caption caption
2323 :markup
2324 (cond ((string= mode "inline-math") 'inline)
2325 (caption 'equation)
2326 (t 'math)))))
2327 (previous table)
2328 (next (org-export-get-next-element table info)))
2329 (org-element-insert-before matrices table)
2330 ;; Swallow all contiguous tables sharing the same mode.
2331 (while (and
2332 (zerop (or (org-element-property :post-blank previous) 0))
2333 (setq next (org-export-get-next-element previous info))
2334 (eq (org-element-type next) 'table)
2335 (eq (org-element-property :type next) 'org)
2336 (string= (or (org-export-read-attribute
2337 :attr_latex next :mode)
2338 (plist-get info :latex-default-table-mode))
2339 mode))
2340 (org-element-extract-element previous)
2341 (org-element-adopt-elements matrices previous)
2342 (setq previous next))
2343 (org-element-put-property
2344 matrices :post-blank (org-element-property :post-blank previous))
2345 (org-element-extract-element previous)
2346 (org-element-adopt-elements matrices previous))))))
2347 info)
2348 data)
2350 (defun org-latex-matrices (matrices contents info)
2351 "Transcode a MATRICES element from Org to LaTeX.
2352 CONTENTS is a string. INFO is a plist used as a communication
2353 channel."
2354 (format (case (org-element-property :markup matrices)
2355 (inline "\\(%s\\)")
2356 (equation "\\begin{equation}\n%s\\end{equation}")
2357 (t "\\[\n%s\\]"))
2358 contents))
2360 (defun org-latex-matrices-tree-filter (tree backend info)
2361 (org-latex--wrap-latex-matrices tree info))
2363 ;;;; Pseudo Object: LaTeX Math Block
2365 ;; `latex-math-block' objects have the following property:
2366 ;; `:post-blank'.
2368 (defun org-latex--wrap-latex-math-block (data info)
2369 "Merge contiguous math objects in a pseudo-object container.
2370 DATA is a parse tree or a secondary string. INFO is a plist
2371 containing export options. Modify DATA by side-effect and return it."
2372 (let ((valid-object-p
2373 (function
2374 ;; Non-nil when OBJ can be added to the latex math block.
2375 (lambda (obj)
2376 (case (org-element-type obj)
2377 (entity (org-element-property :latex-math-p obj))
2378 (latex-fragment
2379 (let ((value (org-element-property :value obj)))
2380 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2381 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2382 ((subscript superscript) t))))))
2383 (org-element-map data '(entity latex-fragment subscript superscript)
2384 (lambda (object)
2385 ;; Skip objects already wrapped.
2386 (when (and (not (eq (org-element-type
2387 (org-element-property :parent object))
2388 'latex-math-block))
2389 (funcall valid-object-p object))
2390 (let ((math-block (list 'latex-math-block nil))
2391 (next-elements (org-export-get-next-element object info t))
2392 (last object))
2393 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2394 (org-element-insert-before math-block object)
2395 (org-element-extract-element object)
2396 (org-element-adopt-elements math-block object)
2397 (when (zerop (or (org-element-property :post-blank object) 0))
2398 ;; MATH-BLOCK swallows consecutive math objects.
2399 (catch 'exit
2400 (dolist (next next-elements)
2401 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2402 (org-element-extract-element next)
2403 (org-element-adopt-elements math-block next)
2404 ;; Eschew the case: \beta$x$ -> \(\betax\).
2405 (unless (memq (org-element-type next)
2406 '(subscript superscript))
2407 (org-element-put-property last :post-blank 1))
2408 (setq last next)
2409 (when (> (or (org-element-property :post-blank next) 0) 0)
2410 (throw 'exit nil))))))
2411 (org-element-put-property
2412 math-block :post-blank (org-element-property :post-blank last)))))
2413 info nil '(subscript superscript latex-math-block) t)
2414 ;; Return updated DATA.
2415 data))
2417 (defun org-latex-math-block-tree-filter (tree backend info)
2418 (org-latex--wrap-latex-math-block tree info))
2420 (defun org-latex-math-block-options-filter (info backend)
2421 (dolist (prop '(:author :date :title) info)
2422 (plist-put info prop
2423 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2425 (defun org-latex-math-block (math-block contents info)
2426 "Transcode a MATH-BLOCK object from Org to LaTeX.
2427 CONTENTS is a string. INFO is a plist used as a communication
2428 channel."
2429 (when (org-string-nw-p contents)
2430 (format "\\(%s\\)" (org-trim contents))))
2432 ;;;; Quote Block
2434 (defun org-latex-quote-block (quote-block contents info)
2435 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2436 CONTENTS holds the contents of the block. INFO is a plist
2437 holding contextual information."
2438 (org-latex--wrap-label
2439 quote-block (format "\\begin{quote}\n%s\\end{quote}" contents) info))
2442 ;;;; Radio Target
2444 (defun org-latex-radio-target (radio-target text info)
2445 "Transcode a RADIO-TARGET object from Org to LaTeX.
2446 TEXT is the text of the target. INFO is a plist holding
2447 contextual information."
2448 (format "\\label{%s}%s" (org-export-get-reference radio-target info) text))
2451 ;;;; Section
2453 (defun org-latex-section (section contents info)
2454 "Transcode a SECTION element from Org to LaTeX.
2455 CONTENTS holds the contents of the section. INFO is a plist
2456 holding contextual information."
2457 contents)
2460 ;;;; Special Block
2462 (defun org-latex-special-block (special-block contents info)
2463 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2464 CONTENTS holds the contents of the block. INFO is a plist
2465 holding contextual information."
2466 (let ((type (org-element-property :type special-block))
2467 (opt (org-export-read-attribute :attr_latex special-block :options))
2468 (caption (org-latex--caption/label-string special-block info))
2469 (caption-above-p (org-latex--caption-above-p special-block info)))
2470 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2471 (and caption-above-p caption)
2472 contents
2473 (and (not caption-above-p) caption)
2474 (format "\\end{%s}" type))))
2477 ;;;; Src Block
2479 (defun org-latex-src-block (src-block contents info)
2480 "Transcode a SRC-BLOCK element from Org to LaTeX.
2481 CONTENTS holds the contents of the item. INFO is a plist holding
2482 contextual information."
2483 (when (org-string-nw-p (org-element-property :value src-block))
2484 (let* ((lang (org-element-property :language src-block))
2485 (caption (org-element-property :caption src-block))
2486 (caption-above-p (org-latex--caption-above-p src-block info))
2487 (label (org-element-property :name src-block))
2488 (custom-env (and lang
2489 (cadr (assq (intern lang)
2490 org-latex-custom-lang-environments))))
2491 (num-start (case (org-element-property :number-lines src-block)
2492 (continued (org-export-get-loc src-block info))
2493 (new 0)))
2494 (retain-labels (org-element-property :retain-labels src-block))
2495 (attributes (org-export-read-attribute :attr_latex src-block))
2496 (float (plist-get attributes :float))
2497 (listings (plist-get info :latex-listings)))
2498 (cond
2499 ;; Case 1. No source fontification.
2500 ((not listings)
2501 (let* ((caption-str (org-latex--caption/label-string src-block info))
2502 (float-env
2503 (cond ((and (not float) (plist-member attributes :float)) "%s")
2504 ((string= "multicolumn" float)
2505 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2506 (plist-get info :latex-default-figure-position)
2507 (if caption-above-p caption-str "")
2508 (if caption-above-p "" caption-str)))
2509 ((or caption float)
2510 (format "\\begin{figure}[H]\n%%s\n%s\\end{figure}"
2511 caption-str))
2512 (t "%s"))))
2513 (format
2514 float-env
2515 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2516 (org-export-format-code-default src-block info))))))
2517 ;; Case 2. Custom environment.
2518 (custom-env
2519 (let ((caption-str (org-latex--caption/label-string src-block info)))
2520 (format "\\begin{%s}\n%s\\end{%s}\n"
2521 custom-env
2522 (concat (and caption-above-p caption-str)
2523 (org-export-format-code-default src-block info)
2524 (and (not caption-above-p) caption-str))
2525 custom-env)))
2526 ;; Case 3. Use minted package.
2527 ((eq listings 'minted)
2528 (let* ((caption-str (org-latex--caption/label-string src-block info))
2529 (float-env
2530 (cond
2531 ((and (not float) (plist-member attributes :float) caption)
2532 (let ((caption
2533 (replace-regexp-in-string
2534 "\\\\caption" "\\captionof{listing}" caption-str
2535 t t)))
2536 (concat (and caption-above-p caption)
2537 "%%s"
2538 (and (not caption-above-p) (concat "\n" caption)))))
2539 ((and (not float) (plist-member attributes :float)) "%s")
2540 ((string= "multicolumn" float)
2541 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2542 (if caption-above-p caption-str "")
2543 (if caption-above-p "" caption-str)))
2544 ((or caption float)
2545 (format "\\begin{listing}[H]\n%s%%s\n%s\\end{listing}"
2546 (if caption-above-p caption-str "")
2547 (if caption-above-p "" caption-str)))
2548 (t "%s")))
2549 (options (plist-get info :latex-minted-options))
2550 (body
2551 (format
2552 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2553 ;; Options.
2554 (concat
2555 (org-latex--make-option-string
2556 (if (or (not num-start) (assoc "linenos" options))
2557 options
2558 (append
2559 `(("linenos")
2560 ("firstnumber" ,(number-to-string (1+ num-start))))
2561 options)))
2562 (let ((local-options (plist-get attributes :options)))
2563 (and local-options (concat "," local-options))))
2564 ;; Language.
2565 (or (cadr (assq (intern lang)
2566 (plist-get info :latex-minted-langs)))
2567 (downcase lang))
2568 ;; Source code.
2569 (let* ((code-info (org-export-unravel-code src-block))
2570 (max-width
2571 (apply 'max
2572 (mapcar 'length
2573 (org-split-string (car code-info)
2574 "\n")))))
2575 (org-export-format-code
2576 (car code-info)
2577 (lambda (loc num ref)
2578 (concat
2580 (when ref
2581 ;; Ensure references are flushed to the right,
2582 ;; separated with 6 spaces from the widest line
2583 ;; of code.
2584 (concat (make-string (+ (- max-width (length loc)) 6)
2585 ?\s)
2586 (format "(%s)" ref)))))
2587 nil (and retain-labels (cdr code-info)))))))
2588 ;; Return value.
2589 (format float-env body)))
2590 ;; Case 4. Use listings package.
2592 (let ((lst-lang
2593 (or (cadr (assq (intern lang)
2594 (plist-get info :latex-listings-langs)))
2595 lang))
2596 (caption-str
2597 (when caption
2598 (let ((main (org-export-get-caption src-block))
2599 (secondary (org-export-get-caption src-block t)))
2600 (if (not secondary)
2601 (format "{%s}" (org-export-data main info))
2602 (format "{[%s]%s}"
2603 (org-export-data secondary info)
2604 (org-export-data main info))))))
2605 (lst-opt (plist-get info :latex-listings-options)))
2606 (concat
2607 ;; Options.
2608 (format
2609 "\\lstset{%s}\n"
2610 (concat
2611 (org-latex--make-option-string
2612 (append
2613 lst-opt
2614 (cond
2615 ((and (not float) (plist-member attributes :float)) nil)
2616 ((string= "multicolumn" float) '(("float" "*")))
2617 ((and float (not (assoc "float" lst-opt)))
2618 `(("float" ,(plist-get info :latex-default-figure-position)))))
2619 `(("language" ,lst-lang))
2620 (if label `(("label" ,label)) '(("label" " ")))
2621 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2622 `(("captionpos" ,(if caption-above-p "t" "b")))
2623 (cond ((assoc "numbers" lst-opt) nil)
2624 ((not num-start) '(("numbers" "none")))
2625 ((zerop num-start) '(("numbers" "left")))
2626 (t `(("firstnumber" ,(number-to-string (1+ num-start)))
2627 ("numbers" "left"))))))
2628 (let ((local-options (plist-get attributes :options)))
2629 (and local-options (concat "," local-options)))))
2630 ;; Source code.
2631 (format
2632 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2633 (let* ((code-info (org-export-unravel-code src-block))
2634 (max-width
2635 (apply 'max
2636 (mapcar 'length
2637 (org-split-string (car code-info) "\n")))))
2638 (org-export-format-code
2639 (car code-info)
2640 (lambda (loc num ref)
2641 (concat
2643 (when ref
2644 ;; Ensure references are flushed to the right,
2645 ;; separated with 6 spaces from the widest line of
2646 ;; code
2647 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2648 (format "(%s)" ref)))))
2649 nil (and retain-labels (cdr code-info))))))))))))
2652 ;;;; Statistics Cookie
2654 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2655 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2656 CONTENTS is nil. INFO is a plist holding contextual information."
2657 (replace-regexp-in-string
2658 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2661 ;;;; Strike-Through
2663 (defun org-latex-strike-through (strike-through contents info)
2664 "Transcode STRIKE-THROUGH from Org to LaTeX.
2665 CONTENTS is the text with strike-through markup. INFO is a plist
2666 holding contextual information."
2667 (org-latex--text-markup contents 'strike-through info))
2670 ;;;; Subscript
2672 (defun org-latex--script-size (object info)
2673 "Transcode a subscript or superscript object.
2674 OBJECT is an Org object. INFO is a plist used as a communication
2675 channel."
2676 (let ((type (org-element-type object))
2677 (output ""))
2678 (org-element-map (org-element-contents object)
2679 (cons 'plain-text org-element-all-objects)
2680 (lambda (obj)
2681 (case (org-element-type obj)
2682 ((entity latex-fragment)
2683 (let ((data (org-trim (org-export-data obj info))))
2684 (string-match
2685 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2686 data)
2687 (setq output
2688 (concat output
2689 (match-string 1 data)
2690 (let ((blank (org-element-property :post-blank obj)))
2691 (and blank (> blank 0) "\\ "))))))
2692 (plain-text
2693 (setq output
2694 (format "%s\\text{%s}" output (org-export-data obj info))))
2695 (otherwise
2696 (setq output
2697 (concat output
2698 (org-export-data obj info)
2699 (let ((blank (org-element-property :post-blank obj)))
2700 (and blank (> blank 0) "\\ ")))))))
2701 info nil org-element-recursive-objects)
2702 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2703 ;; character.
2704 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2705 (and (> (length output) 1) "{")
2706 output
2707 (and (> (length output) 1) "}"))))
2709 (defun org-latex-subscript (subscript contents info)
2710 "Transcode a SUBSCRIPT object from Org to LaTeX.
2711 CONTENTS is the contents of the object. INFO is a plist holding
2712 contextual information."
2713 (org-latex--script-size subscript info))
2716 ;;;; Superscript
2718 (defun org-latex-superscript (superscript contents info)
2719 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2720 CONTENTS is the contents of the object. INFO is a plist holding
2721 contextual information."
2722 (org-latex--script-size superscript info))
2725 ;;;; Table
2727 ;; `org-latex-table' is the entry point for table transcoding. It
2728 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2729 ;; delegates the job to either `org-latex--table.el-table',
2730 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2731 ;; depending of the type of the table and the mode requested.
2733 ;; `org-latex--align-string' is a subroutine used to build alignment
2734 ;; string for Org tables.
2736 (defun org-latex-table (table contents info)
2737 "Transcode a TABLE element from Org to LaTeX.
2738 CONTENTS is the contents of the table. INFO is a plist holding
2739 contextual information."
2740 (if (eq (org-element-property :type table) 'table.el)
2741 ;; "table.el" table. Convert it using appropriate tools.
2742 (org-latex--table.el-table table info)
2743 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2744 (plist-get info :latex-default-table-mode))))
2745 (cond
2746 ;; Case 1: Verbatim table.
2747 ((string= type "verbatim")
2748 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2749 ;; Re-create table, without affiliated keywords.
2750 (org-trim (org-element-interpret-data
2751 `(table nil ,@(org-element-contents table))))))
2752 ;; Case 2: Matrix.
2753 ((or (string= type "math") (string= type "inline-math"))
2754 (org-latex--math-table table info))
2755 ;; Case 3: Standard table.
2756 (t (concat (org-latex--org-table table contents info)
2757 ;; When there are footnote references within the
2758 ;; table, insert their definition just after it.
2759 (org-latex--delayed-footnotes-definitions table info)))))))
2761 (defun org-latex--align-string (table info)
2762 "Return an appropriate LaTeX alignment string.
2763 TABLE is the considered table. INFO is a plist used as
2764 a communication channel."
2765 (or (org-export-read-attribute :attr_latex table :align)
2766 (let (align)
2767 ;; Extract column groups and alignment from first (non-rule)
2768 ;; row.
2769 (org-element-map
2770 (org-element-map table 'table-row
2771 (lambda (row)
2772 (and (eq (org-element-property :type row) 'standard) row))
2773 info 'first-match)
2774 'table-cell
2775 (lambda (cell)
2776 (let ((borders (org-export-table-cell-borders cell info)))
2777 ;; Check left border for the first cell only.
2778 (when (and (memq 'left borders) (not align))
2779 (push "|" align))
2780 (push (case (org-export-table-cell-alignment cell info)
2781 (left "l")
2782 (right "r")
2783 (center "c"))
2784 align)
2785 (when (memq 'right borders) (push "|" align))))
2786 info)
2787 (apply 'concat (nreverse align)))))
2789 (defun org-latex--org-table (table contents info)
2790 "Return appropriate LaTeX code for an Org table.
2792 TABLE is the table type element to transcode. CONTENTS is its
2793 contents, as a string. INFO is a plist used as a communication
2794 channel.
2796 This function assumes TABLE has `org' as its `:type' property and
2797 `table' as its `:mode' attribute."
2798 (let* ((caption (org-latex--caption/label-string table info))
2799 (attr (org-export-read-attribute :attr_latex table))
2800 ;; Determine alignment string.
2801 (alignment (org-latex--align-string table info))
2802 ;; Determine environment for the table: longtable, tabular...
2803 (table-env (or (plist-get attr :environment)
2804 (plist-get info :latex-default-table-environment)))
2805 ;; If table is a float, determine environment: table, table*
2806 ;; or sidewaystable.
2807 (float-env (unless (member table-env '("longtable" "longtabu"))
2808 (let ((float (plist-get attr :float)))
2809 (cond
2810 ((and (not float) (plist-member attr :float)) nil)
2811 ((or (string= float "sidewaystable")
2812 (string= float "sideways")) "sidewaystable")
2813 ((string= float "multicolumn") "table*")
2814 ((or float
2815 (org-element-property :caption table)
2816 (org-string-nw-p (plist-get attr :caption)))
2817 "table")))))
2818 ;; Extract others display options.
2819 (fontsize (let ((font (plist-get attr :font)))
2820 (and font (concat font "\n"))))
2821 ;; "tabular" environment doesn't allow to define a width.
2822 (width (and (not (equal table-env "tabular")) (plist-get attr :width)))
2823 (spreadp (plist-get attr :spread))
2824 (placement
2825 (or (plist-get attr :placement)
2826 (format "[%s]" (plist-get info :latex-default-figure-position))))
2827 (centerp (if (plist-member attr :center) (plist-get attr :center)
2828 (plist-get info :latex-tables-centered)))
2829 (caption-above-p (org-latex--caption-above-p table info)))
2830 ;; Prepare the final format string for the table.
2831 (cond
2832 ;; Longtable.
2833 ((equal "longtable" table-env)
2834 (concat (and fontsize (concat "{" fontsize))
2835 (format "\\begin{longtable}{%s}\n" alignment)
2836 (and caption-above-p
2837 (org-string-nw-p caption)
2838 (concat caption "\\\\\n"))
2839 contents
2840 (and (not caption-above-p)
2841 (org-string-nw-p caption)
2842 (concat caption "\\\\\n"))
2843 "\\end{longtable}\n"
2844 (and fontsize "}")))
2845 ;; Longtabu
2846 ((equal "longtabu" table-env)
2847 (concat (and fontsize (concat "{" fontsize))
2848 (format "\\begin{longtabu}%s{%s}\n"
2849 (if width
2850 (format " %s %s "
2851 (if spreadp "spread" "to") width) "")
2852 alignment)
2853 (and caption-above-p
2854 (org-string-nw-p caption)
2855 (concat caption "\\\\\n"))
2856 contents
2857 (and (not caption-above-p)
2858 (org-string-nw-p caption)
2859 (concat caption "\\\\\n"))
2860 "\\end{longtabu}\n"
2861 (and fontsize "}")))
2862 ;; Others.
2863 (t (concat (cond
2864 (float-env
2865 (concat (format "\\begin{%s}%s\n" float-env placement)
2866 (if caption-above-p caption "")
2867 (when centerp "\\centering\n")
2868 fontsize))
2869 (centerp (concat "\\begin{center}\n" fontsize))
2870 (fontsize (concat "{" fontsize)))
2871 (cond ((equal "tabu" table-env)
2872 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2873 (if width (format
2874 (if spreadp " spread %s " " to %s ")
2875 width) "")
2876 alignment
2877 contents))
2878 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2879 table-env
2880 (if width (format "{%s}" width) "")
2881 alignment
2882 contents
2883 table-env)))
2884 (cond
2885 (float-env
2886 (concat (if caption-above-p "" caption)
2887 (format "\n\\end{%s}" float-env)))
2888 (centerp "\n\\end{center}")
2889 (fontsize "}")))))))
2891 (defun org-latex--table.el-table (table info)
2892 "Return appropriate LaTeX code for a table.el table.
2894 TABLE is the table type element to transcode. INFO is a plist
2895 used as a communication channel.
2897 This function assumes TABLE has `table.el' as its `:type'
2898 property."
2899 (require 'table)
2900 ;; Ensure "*org-export-table*" buffer is empty.
2901 (with-current-buffer (get-buffer-create "*org-export-table*")
2902 (erase-buffer))
2903 (let ((output (with-temp-buffer
2904 (insert (org-element-property :value table))
2905 (goto-char 1)
2906 (re-search-forward "^[ \t]*|[^|]" nil t)
2907 (table-generate-source 'latex "*org-export-table*")
2908 (with-current-buffer "*org-export-table*"
2909 (org-trim (buffer-string))))))
2910 (kill-buffer (get-buffer "*org-export-table*"))
2911 ;; Remove left out comments.
2912 (while (string-match "^%.*\n" output)
2913 (setq output (replace-match "" t t output)))
2914 (let ((attr (org-export-read-attribute :attr_latex table)))
2915 (when (plist-get attr :rmlines)
2916 ;; When the "rmlines" attribute is provided, remove all hlines
2917 ;; but the the one separating heading from the table body.
2918 (let ((n 0) (pos 0))
2919 (while (and (< (length output) pos)
2920 (setq pos (string-match "^\\\\hline\n?" output pos)))
2921 (incf n)
2922 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2923 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2924 (plist-get info :latex-tables-centered))))
2925 (if (not centerp) output
2926 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2928 (defun org-latex--math-table (table info)
2929 "Return appropriate LaTeX code for a matrix.
2931 TABLE is the table type element to transcode. INFO is a plist
2932 used as a communication channel.
2934 This function assumes TABLE has `org' as its `:type' property and
2935 `inline-math' or `math' as its `:mode' attribute."
2936 (let* ((attr (org-export-read-attribute :attr_latex table))
2937 (env (or (plist-get attr :environment)
2938 (plist-get info :latex-default-table-environment)))
2939 (contents
2940 (mapconcat
2941 (lambda (row)
2942 ;; Ignore horizontal rules.
2943 (when (eq (org-element-property :type row) 'standard)
2944 ;; Return each cell unmodified.
2945 (concat
2946 (mapconcat
2947 (lambda (cell)
2948 (substring (org-element-interpret-data cell) 0 -1))
2949 (org-element-map row 'table-cell #'identity info) "&")
2950 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2951 "\n")))
2952 (org-element-map table 'table-row #'identity info) "")))
2953 (concat
2954 ;; Prefix.
2955 (plist-get attr :math-prefix)
2956 ;; Environment. Also treat special cases.
2957 (cond ((member env '("array" "tabular"))
2958 (let ((align (make-string
2959 (cdr (org-export-table-dimensions table info)) ?c)))
2960 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env)))
2961 ((assoc env org-latex-table-matrix-macros)
2962 (format "\\%s%s{\n%s}"
2964 (or (plist-get attr :math-arguments) "")
2965 contents))
2966 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2967 ;; Suffix.
2968 (plist-get attr :math-suffix))))
2971 ;;;; Table Cell
2973 (defun org-latex-table-cell (table-cell contents info)
2974 "Transcode a TABLE-CELL element from Org to LaTeX.
2975 CONTENTS is the cell contents. INFO is a plist used as
2976 a communication channel."
2977 (concat
2978 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
2979 (if (and contents
2980 scientific-format
2981 (string-match orgtbl-exp-regexp contents))
2982 ;; Use appropriate format string for scientific
2983 ;; notation.
2984 (format scientific-format
2985 (match-string 1 contents)
2986 (match-string 2 contents))
2987 contents))
2988 (when (org-export-get-next-element table-cell info) " & ")))
2991 ;;;; Table Row
2993 (defun org-latex-table-row (table-row contents info)
2994 "Transcode a TABLE-ROW element from Org to LaTeX.
2995 CONTENTS is the contents of the row. INFO is a plist used as
2996 a communication channel."
2997 (let* ((attr (org-export-read-attribute :attr_latex
2998 (org-export-get-parent table-row)))
2999 (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
3000 (plist-get info :latex-tables-booktabs)))
3001 (longtablep
3002 (member (or (plist-get attr :environment)
3003 (plist-get info :latex-default-table-environment))
3004 '("longtable" "longtabu"))))
3005 (if (eq (org-element-property :type table-row) 'rule)
3006 (cond
3007 ((not booktabsp) "\\hline")
3008 ((not (org-export-get-previous-element table-row info)) "\\toprule")
3009 ((not (org-export-get-next-element table-row info)) "\\bottomrule")
3010 ((and longtablep
3011 (org-export-table-row-ends-header-p
3012 (org-export-get-previous-element table-row info) info))
3014 (t "\\midrule"))
3015 (concat
3016 ;; When BOOKTABS are activated enforce top-rule even when no
3017 ;; hline was specifically marked.
3018 (and booktabsp (not (org-export-get-previous-element table-row info))
3019 "\\toprule\n")
3020 contents "\\\\\n"
3021 (cond
3022 ;; Special case for long tables. Define header and footers.
3023 ((and longtablep (org-export-table-row-ends-header-p table-row info))
3024 (let ((columns (cdr (org-export-table-dimensions
3025 (org-export-get-parent-table table-row) info))))
3026 (format "%s
3027 \\endfirsthead
3028 \\multicolumn{%d}{l}{%s} \\\\
3030 %s \\\\\n
3032 \\endhead
3033 %s\\multicolumn{%d}{r}{%s} \\\\
3034 \\endfoot
3035 \\endlastfoot"
3036 (if booktabsp "\\midrule" "\\hline")
3037 columns
3038 (org-latex--translate "Continued from previous page" info)
3039 (cond
3040 ((not (org-export-table-row-starts-header-p table-row info))
3042 (booktabsp "\\toprule\n")
3043 (t "\\hline\n"))
3044 contents
3045 (if booktabsp "\\midrule" "\\hline")
3046 (if booktabsp "\\midrule" "\\hline")
3047 columns
3048 (org-latex--translate "Continued on next page" info))))
3049 ;; When BOOKTABS are activated enforce bottom rule even when
3050 ;; no hline was specifically marked.
3051 ((and booktabsp (not (org-export-get-next-element table-row info)))
3052 "\\bottomrule"))))))
3055 ;;;; Target
3057 (defun org-latex-target (target contents info)
3058 "Transcode a TARGET object from Org to LaTeX.
3059 CONTENTS is nil. INFO is a plist holding contextual
3060 information."
3061 (format "\\label{%s}" (org-latex--label target info)))
3064 ;;;; Timestamp
3066 (defun org-latex-timestamp (timestamp contents info)
3067 "Transcode a TIMESTAMP object from Org to LaTeX.
3068 CONTENTS is nil. INFO is a plist holding contextual
3069 information."
3070 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
3071 (format
3072 (plist-get info
3073 (case (org-element-property :type timestamp)
3074 ((active active-range) :latex-active-timestamp-format)
3075 ((inactive inactive-range) :latex-inactive-timestamp-format)
3076 (otherwise :latex-diary-timestamp-format)))
3077 value)))
3080 ;;;; Underline
3082 (defun org-latex-underline (underline contents info)
3083 "Transcode UNDERLINE from Org to LaTeX.
3084 CONTENTS is the text with underline markup. INFO is a plist
3085 holding contextual information."
3086 (org-latex--text-markup contents 'underline info))
3089 ;;;; Verbatim
3091 (defun org-latex-verbatim (verbatim contents info)
3092 "Transcode a VERBATIM object from Org to LaTeX.
3093 CONTENTS is nil. INFO is a plist used as a communication
3094 channel."
3095 (org-latex--text-markup
3096 (org-element-property :value verbatim) 'verbatim info))
3099 ;;;; Verse Block
3101 (defun org-latex-verse-block (verse-block contents info)
3102 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3103 CONTENTS is verse block contents. INFO is a plist holding
3104 contextual information."
3105 (org-latex--wrap-label
3106 verse-block
3107 ;; In a verse environment, add a line break to each newline
3108 ;; character and change each white space at beginning of a line
3109 ;; into a space of 1 em. Also change each blank line with
3110 ;; a vertical space of 1 em.
3111 (format "\\begin{verse}\n%s\\end{verse}"
3112 (replace-regexp-in-string
3113 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
3114 (replace-regexp-in-string
3115 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3116 (replace-regexp-in-string
3117 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3118 contents nil t) nil t) nil t))
3119 info))
3123 ;;; End-user functions
3125 ;;;###autoload
3126 (defun org-latex-export-as-latex
3127 (&optional async subtreep visible-only body-only ext-plist)
3128 "Export current buffer as a LaTeX buffer.
3130 If narrowing is active in the current buffer, only export its
3131 narrowed part.
3133 If a region is active, export that region.
3135 A non-nil optional argument ASYNC means the process should happen
3136 asynchronously. The resulting buffer should be accessible
3137 through the `org-export-stack' interface.
3139 When optional argument SUBTREEP is non-nil, export the sub-tree
3140 at point, extracting information from the headline properties
3141 first.
3143 When optional argument VISIBLE-ONLY is non-nil, don't export
3144 contents of hidden elements.
3146 When optional argument BODY-ONLY is non-nil, only write code
3147 between \"\\begin{document}\" and \"\\end{document}\".
3149 EXT-PLIST, when provided, is a property list with external
3150 parameters overriding Org default settings, but still inferior to
3151 file-local settings.
3153 Export is done in a buffer named \"*Org LATEX Export*\", which
3154 will be displayed when `org-export-show-temporary-export-buffer'
3155 is non-nil."
3156 (interactive)
3157 (org-export-to-buffer 'latex "*Org LATEX Export*"
3158 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
3160 ;;;###autoload
3161 (defun org-latex-convert-region-to-latex ()
3162 "Assume the current region has org-mode syntax, and convert it to LaTeX.
3163 This can be used in any buffer. For example, you can write an
3164 itemized list in org-mode syntax in an LaTeX buffer and use this
3165 command to convert it."
3166 (interactive)
3167 (org-export-replace-region-by 'latex))
3169 ;;;###autoload
3170 (defun org-latex-export-to-latex
3171 (&optional async subtreep visible-only body-only ext-plist)
3172 "Export current buffer to a LaTeX file.
3174 If narrowing is active in the current buffer, only export its
3175 narrowed part.
3177 If a region is active, export that region.
3179 A non-nil optional argument ASYNC means the process should happen
3180 asynchronously. The resulting file should be accessible through
3181 the `org-export-stack' interface.
3183 When optional argument SUBTREEP is non-nil, export the sub-tree
3184 at point, extracting information from the headline properties
3185 first.
3187 When optional argument VISIBLE-ONLY is non-nil, don't export
3188 contents of hidden elements.
3190 When optional argument BODY-ONLY is non-nil, only write code
3191 between \"\\begin{document}\" and \"\\end{document}\".
3193 EXT-PLIST, when provided, is a property list with external
3194 parameters overriding Org default settings, but still inferior to
3195 file-local settings."
3196 (interactive)
3197 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3198 (org-export-to-file 'latex outfile
3199 async subtreep visible-only body-only ext-plist)))
3201 ;;;###autoload
3202 (defun org-latex-export-to-pdf
3203 (&optional async subtreep visible-only body-only ext-plist)
3204 "Export current buffer to LaTeX then process through to PDF.
3206 If narrowing is active in the current buffer, only export its
3207 narrowed part.
3209 If a region is active, export that region.
3211 A non-nil optional argument ASYNC means the process should happen
3212 asynchronously. The resulting file should be accessible through
3213 the `org-export-stack' interface.
3215 When optional argument SUBTREEP is non-nil, export the sub-tree
3216 at point, extracting information from the headline properties
3217 first.
3219 When optional argument VISIBLE-ONLY is non-nil, don't export
3220 contents of hidden elements.
3222 When optional argument BODY-ONLY is non-nil, only write code
3223 between \"\\begin{document}\" and \"\\end{document}\".
3225 EXT-PLIST, when provided, is a property list with external
3226 parameters overriding Org default settings, but still inferior to
3227 file-local settings.
3229 Return PDF file's name."
3230 (interactive)
3231 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3232 (org-export-to-file 'latex outfile
3233 async subtreep visible-only body-only ext-plist
3234 (lambda (file) (org-latex-compile file)))))
3236 (defun org-latex-compile (texfile &optional snippet)
3237 "Compile a TeX file.
3239 TEXFILE is the name of the file being compiled. Processing is
3240 done through the command specified in `org-latex-pdf-process'.
3242 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3243 file used to preview a LaTeX snippet. In this case, do not
3244 create a log buffer and do not bother removing log files.
3246 Return PDF file name or an error if it couldn't be produced."
3247 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3248 (full-name (file-truename texfile))
3249 (out-dir (file-name-directory texfile))
3250 ;; Properly set working directory for compilation.
3251 (default-directory (if (file-name-absolute-p texfile)
3252 (file-name-directory full-name)
3253 default-directory))
3254 (time (current-time))
3255 warnings)
3256 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
3257 (save-window-excursion
3258 (cond
3259 ;; A function is provided: Apply it.
3260 ((functionp org-latex-pdf-process)
3261 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3262 ;; A list is provided: Replace %b, %f and %o with appropriate
3263 ;; values in each command before applying it. Output is
3264 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3265 ((consp org-latex-pdf-process)
3266 (let ((outbuf (and (not snippet)
3267 (get-buffer-create "*Org PDF LaTeX Output*"))))
3268 (dolist (command org-latex-pdf-process)
3269 (shell-command
3270 (replace-regexp-in-string
3271 "%b" (shell-quote-argument base-name)
3272 (replace-regexp-in-string
3273 "%f" (shell-quote-argument full-name)
3274 (replace-regexp-in-string
3275 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
3276 outbuf))
3277 ;; Collect standard errors from output buffer.
3278 (setq warnings (and (not snippet)
3279 (org-latex--collect-warnings outbuf)))))
3280 (t (error "No valid command to process to PDF")))
3281 (let ((pdffile (concat out-dir base-name ".pdf")))
3282 ;; Check for process failure. Provide collected errors if
3283 ;; possible.
3284 (if (or (not (file-exists-p pdffile))
3285 (time-less-p (nth 5 (file-attributes pdffile)) time))
3286 (error (format "PDF file %s wasn't produced" pdffile))
3287 ;; Else remove log files, when specified, and signal end of
3288 ;; process to user, along with any error encountered.
3289 (unless snippet
3290 (when org-latex-remove-logfiles
3291 (dolist (file (directory-files
3292 out-dir t
3293 (concat (regexp-quote base-name)
3294 "\\(?:\\.[0-9]+\\)?"
3295 "\\."
3296 (regexp-opt org-latex-logfiles-extensions))))
3297 (delete-file file)))
3298 (message (concat "PDF file produced"
3299 (cond
3300 ((eq warnings 'error) " with errors.")
3301 (warnings (concat " with warnings: " warnings))
3302 (t "."))))))
3303 ;; Return output file name.
3304 pdffile))))
3306 (defun org-latex--collect-warnings (buffer)
3307 "Collect some warnings from \"pdflatex\" command output.
3308 BUFFER is the buffer containing output. Return collected
3309 warnings types as a string, `error' if a LaTeX error was
3310 encountered or nil if there was none."
3311 (with-current-buffer buffer
3312 (save-excursion
3313 (goto-char (point-max))
3314 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3315 (if (re-search-forward "^!" nil t) 'error
3316 (let ((case-fold-search t)
3317 (warnings ""))
3318 (dolist (warning org-latex-known-warnings)
3319 (when (save-excursion (re-search-forward (car warning) nil t))
3320 (setq warnings (concat warnings " " (cdr warning)))))
3321 (org-string-nw-p (org-trim warnings))))))))
3323 ;;;###autoload
3324 (defun org-latex-publish-to-latex (plist filename pub-dir)
3325 "Publish an Org file to LaTeX.
3327 FILENAME is the filename of the Org file to be published. PLIST
3328 is the property list for the given project. PUB-DIR is the
3329 publishing directory.
3331 Return output file name."
3332 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3334 ;;;###autoload
3335 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3336 "Publish an Org file to PDF (via LaTeX).
3338 FILENAME is the filename of the Org file to be published. PLIST
3339 is the property list for the given project. PUB-DIR is the
3340 publishing directory.
3342 Return output file name."
3343 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3344 ;; in working directory and then moved to publishing directory.
3345 (org-publish-attachment
3346 plist
3347 (org-latex-compile
3348 (org-publish-org-to
3349 'latex filename ".tex" plist (file-name-directory filename)))
3350 pub-dir))
3353 (provide 'ox-latex)
3355 ;; Local variables:
3356 ;; generated-autoload-file: "org-loaddefs.el"
3357 ;; End:
3359 ;;; ox-latex.el ends here