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