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