1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2011-2017 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/>.
25 ;; See Org manual for details.
33 (defvar org-latex-default-packages-alist
)
34 (defvar org-latex-packages-alist
)
35 (defvar orgtbl-exp-regexp
)
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
))
94 '(?l
"Export to LaTeX"
95 ((?L
"As LaTeX buffer" org-latex-export-as-latex
)
96 (?l
"As LaTeX file" org-latex-export-to-latex
)
97 (?p
"As PDF file" org-latex-export-to-pdf
)
98 (?o
"As PDF file and open"
100 (if a
(org-latex-export-to-pdf t s v b
)
101 (org-open-file (org-latex-export-to-pdf nil s v b
)))))))
102 :filters-alist
'((:filter-options . org-latex-math-block-options-filter
)
103 (:filter-paragraph . org-latex-clean-invalid-line-breaks
)
104 (:filter-parse-tree org-latex-math-block-tree-filter
105 org-latex-matrices-tree-filter
106 org-latex-image-link-filter
)
107 (:filter-verse-block . org-latex-clean-invalid-line-breaks
))
109 '((:latex-class
"LATEX_CLASS" nil org-latex-default-class t
)
110 (:latex-class-options
"LATEX_CLASS_OPTIONS" nil nil t
)
111 (:latex-header
"LATEX_HEADER" nil nil newline
)
112 (:latex-header-extra
"LATEX_HEADER_EXTRA" nil nil newline
)
113 (:description
"DESCRIPTION" nil nil parse
)
114 (:keywords
"KEYWORDS" nil nil parse
)
115 (:subtitle
"SUBTITLE" nil nil parse
)
117 (:latex-active-timestamp-format nil nil org-latex-active-timestamp-format
)
118 (:latex-caption-above nil nil org-latex-caption-above
)
119 (:latex-classes nil nil org-latex-classes
)
120 (:latex-default-figure-position nil nil org-latex-default-figure-position
)
121 (:latex-default-table-environment nil nil org-latex-default-table-environment
)
122 (:latex-default-table-mode nil nil org-latex-default-table-mode
)
123 (:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format
)
124 (:latex-footnote-defined-format nil nil org-latex-footnote-defined-format
)
125 (:latex-footnote-separator nil nil org-latex-footnote-separator
)
126 (:latex-format-drawer-function nil nil org-latex-format-drawer-function
)
127 (:latex-format-headline-function nil nil org-latex-format-headline-function
)
128 (:latex-format-inlinetask-function nil nil org-latex-format-inlinetask-function
)
129 (:latex-hyperref-template nil nil org-latex-hyperref-template t
)
130 (:latex-image-default-height nil nil org-latex-image-default-height
)
131 (:latex-image-default-option nil nil org-latex-image-default-option
)
132 (:latex-image-default-width nil nil org-latex-image-default-width
)
133 (:latex-images-centered nil nil org-latex-images-centered
)
134 (:latex-inactive-timestamp-format nil nil org-latex-inactive-timestamp-format
)
135 (:latex-inline-image-rules nil nil org-latex-inline-image-rules
)
136 (:latex-link-with-unknown-path-format nil nil org-latex-link-with-unknown-path-format
)
137 (:latex-listings nil nil org-latex-listings
)
138 (:latex-listings-langs nil nil org-latex-listings-langs
)
139 (:latex-listings-options nil nil org-latex-listings-options
)
140 (:latex-minted-langs nil nil org-latex-minted-langs
)
141 (:latex-minted-options nil nil org-latex-minted-options
)
142 (:latex-prefer-user-labels nil nil org-latex-prefer-user-labels
)
143 (:latex-subtitle-format nil nil org-latex-subtitle-format
)
144 (:latex-subtitle-separate nil nil org-latex-subtitle-separate
)
145 (:latex-table-scientific-notation nil nil org-latex-table-scientific-notation
)
146 (:latex-tables-booktabs nil nil org-latex-tables-booktabs
)
147 (:latex-tables-centered nil nil org-latex-tables-centered
)
148 (:latex-text-markup-alist nil nil org-latex-text-markup-alist
)
149 (:latex-title-command nil nil org-latex-title-command
)
150 (:latex-toc-command nil nil org-latex-toc-command
)
151 (:latex-compiler
"LATEX_COMPILER" nil org-latex-compiler
)
152 ;; Redefine regular options.
153 (:date
"DATE" nil
"\\today" parse
)))
157 ;;; Internal Variables
159 (defconst org-latex-babel-language-alist
160 '(("af" .
"afrikaans")
162 ("bt-br" .
"brazilian")
168 ("de-at" .
"naustrian")
169 ("de-de" .
"ngerman")
172 ("en-au" .
"australian")
173 ("en-ca" .
"canadian")
174 ("en-gb" .
"british")
176 ("en-nz" .
"newzealand")
177 ("en-us" .
"american")
183 ("fr-ca" .
"canadien")
187 ("id" .
"indonesian")
197 ("pt" .
"portuguese")
201 ("sb" .
"uppersorbian")
209 ("uk" .
"ukrainian"))
210 "Alist between language code and corresponding Babel option.")
212 (defconst org-latex-polyglossia-language-alist
220 ("bt-br" "brazilian")
226 ("de" "german" "german")
227 ("de-at" "german" "austrian")
228 ("de-de" "german" "german")
231 ("en" "english" "usmax")
232 ("en-au" "english" "australian")
233 ("en-gb" "english" "uk")
234 ("en-nz" "english" "newzealand")
235 ("en-us" "english" "usmax")
257 ("la" "latin" "modern")
258 ("la-modern" "latin" "modern")
259 ("la-classic" "latin" "classic")
260 ("la-medieval" "latin" "medieval")
273 ("pms" "piedmontese")
296 "Alist between language code and corresponding Polyglossia option")
300 (defconst org-latex-table-matrix-macros
'(("bordermatrix" .
"\\cr")
301 ("qbordermatrix" .
"\\cr")
302 ("kbordermatrix" .
"\\\\"))
303 "Alist between matrix macros and their row ending.")
305 (defconst org-latex-math-environments-re
307 "\\`[ \t]*\\\\begin{%s\\*?}"
309 '("equation" "eqnarray" "math" "displaymath"
310 "align" "gather" "multline" "flalign" "alignat"
311 "xalignat" "xxalignat"
314 "dmath" "dseries" "dgroup" "darray"
317 "Regexp of LaTeX math environments.")
320 ;;; User Configurable Variables
322 (defgroup org-export-latex nil
323 "Options for exporting Org mode files to LaTeX."
324 :tag
"Org Export LaTeX"
329 (defcustom org-latex-caption-above
'(table)
330 "When non-nil, place caption string at the beginning of elements.
331 Otherwise, place it near the end. When value is a list of
332 symbols, put caption above selected elements only. Allowed
333 symbols are: `image', `table', `src-block' and `special-block'."
334 :group
'org-export-latex
336 :package-version
'(Org .
"8.3")
338 (const :tag
"For all elements" t
)
339 (const :tag
"For no element" nil
)
340 (set :tag
"For the following elements only" :greedy t
341 (const :tag
"Images" image
)
342 (const :tag
"Tables" table
)
343 (const :tag
"Source code" src-block
)
344 (const :tag
"Special blocks" special-block
))))
346 (defcustom org-latex-prefer-user-labels nil
347 "Use user-provided labels instead of internal ones when non-nil.
349 When this variable is non-nil, Org will use the value of
350 CUSTOM_ID property, NAME keyword or Org target as the key for the
351 \\label commands generated.
353 By default, Org generates its own internal labels during LaTeX
354 export. This process ensures that the \\label keys are unique
355 and valid, but it means the keys are not available in advance of
358 Setting this variable gives you control over how Org generates
359 labels during LaTeX export, so that you may know their keys in
360 advance. One reason to do this is that it allows you to refer to
361 various elements using a single label both in Org's link syntax
362 and in embedded LaTeX code.
364 For example, when this variable is non-nil, a headline like this:
370 This is section [[#sec:foo]].
372 And this is still section \\ref{sec:foo}.
375 will be exported to LaTeX as:
377 \\subsection{Some section}
379 This is section \\ref{sec:foo}.
380 And this is still section \\ref{sec:foo}.
382 Note, however, that setting this variable introduces a limitation
383 on the possible values for CUSTOM_ID and NAME. When this
384 variable is non-nil, Org passes their value to \\label unchanged.
385 You are responsible for ensuring that the value is a valid LaTeX
386 \\label key, and that no other \\label commands with the same key
387 appear elsewhere in your document. (Keys may contain letters,
388 numbers, and the following punctuation: '_' '.' '-' ':'.) There
389 are no such limitations on CUSTOM_ID and NAME when this variable
392 For headlines that do not define the CUSTOM_ID property or
393 elements without a NAME, Org will continue to use its default
394 labeling scheme to generate labels and resolve links into proper
396 :group
'org-export-latex
399 :package-version
'(Org .
"8.3"))
403 (defcustom org-latex-default-class
"article"
404 "The default LaTeX class."
405 :group
'org-export-latex
406 :type
'(string :tag
"LaTeX class"))
408 (defcustom org-latex-classes
410 "\\documentclass[11pt]{article}"
411 ("\\section{%s}" .
"\\section*{%s}")
412 ("\\subsection{%s}" .
"\\subsection*{%s}")
413 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
414 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
415 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
417 "\\documentclass[11pt]{report}"
418 ("\\part{%s}" .
"\\part*{%s}")
419 ("\\chapter{%s}" .
"\\chapter*{%s}")
420 ("\\section{%s}" .
"\\section*{%s}")
421 ("\\subsection{%s}" .
"\\subsection*{%s}")
422 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
424 "\\documentclass[11pt]{book}"
425 ("\\part{%s}" .
"\\part*{%s}")
426 ("\\chapter{%s}" .
"\\chapter*{%s}")
427 ("\\section{%s}" .
"\\section*{%s}")
428 ("\\subsection{%s}" .
"\\subsection*{%s}")
429 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
430 "Alist of LaTeX classes and associated header and structure.
431 If #+LATEX_CLASS is set in the buffer, use its value and the
432 associated information. Here is the structure of each cell:
436 (numbered-section . unnumbered-section)
442 The HEADER-STRING is the header that will be inserted into the
443 LaTeX file. It should contain the \\documentclass macro, and
444 anything else that is needed for this setup. To this header, the
445 following commands will be added:
447 - Calls to \\usepackage for all packages mentioned in the
448 variables `org-latex-default-packages-alist' and
449 `org-latex-packages-alist'. Thus, your header definitions
450 should avoid to also request these packages.
452 - Lines specified via \"#+LATEX_HEADER:\" and
453 \"#+LATEX_HEADER_EXTRA:\" keywords.
455 If you need more control about the sequence in which the header
456 is built up, or if you want to exclude one of these building
457 blocks for a particular class, you can use the following
458 macro-like placeholders.
460 [DEFAULT-PACKAGES] \\usepackage statements for default packages
461 [NO-DEFAULT-PACKAGES] do not include any of the default packages
462 [PACKAGES] \\usepackage statements for packages
463 [NO-PACKAGES] do not include the packages
464 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
465 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
469 \\documentclass{article}
470 [NO-DEFAULT-PACKAGES]
472 \\providecommand{\\alert}[1]{\\textbf{#1}}
475 will omit the default packages, and will include the
476 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
477 to \\providecommand, and then place \\usepackage commands based
478 on the content of `org-latex-packages-alist'.
480 If your header, `org-latex-default-packages-alist' or
481 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
482 AUTO will automatically be replaced with a coding system derived
483 from `buffer-file-coding-system'. See also the variable
484 `org-latex-inputenc-alist' for a way to influence this mechanism.
486 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\"
487 or \"\\usepackage[AUTO]{polyglossia}\", AUTO will be replaced
488 with the language related to the language code specified by
489 `org-export-default-language'. Note that constructions such as
490 \"\\usepackage[french,AUTO,english]{babel}\" are permitted. For
491 Polyglossia the language will be set via the macros
492 \"\\setmainlanguage\" and \"\\setotherlanguage\". See also
493 `org-latex-guess-babel-language' and
494 `org-latex-guess-polyglossia-language'.
496 The sectioning structure
497 ------------------------
499 The sectioning structure of the class is given by the elements
500 following the header string. For each sectioning level, a number
501 of strings is specified. A %s formatter is mandatory in each
502 section string and will be replaced by the title of the section.
504 Instead of a cons cell (numbered . unnumbered), you can also
505 provide a list of 2 or 4 elements,
507 (numbered-open numbered-close)
511 (numbered-open numbered-close unnumbered-open unnumbered-close)
513 providing opening and closing strings for a LaTeX environment
514 that should represent the document section. The opening clause
515 should have a %s to represent the section title.
517 Instead of a list of sectioning commands, you can also specify
518 a function name. That function will be called with two
519 parameters, the (reduced) level of the headline, and a predicate
520 non-nil when the headline should be numbered. It must return
521 a format string in which the section title will be added."
522 :group
'org-export-latex
524 (list (string :tag
"LaTeX class")
525 (string :tag
"LaTeX header")
526 (repeat :tag
"Levels" :inline t
529 (string :tag
" numbered")
530 (string :tag
"unnumbered"))
531 (list :tag
"Environment"
532 (string :tag
"Opening (numbered)")
533 (string :tag
"Closing (numbered)")
534 (string :tag
"Opening (unnumbered)")
535 (string :tag
"Closing (unnumbered)"))
536 (function :tag
"Hook computing sectioning"))))))
538 (defcustom org-latex-inputenc-alist nil
539 "Alist of inputenc coding system names, and what should really be used.
540 For example, adding an entry
542 (\"utf8\" . \"utf8x\")
544 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
545 are written as utf8 files."
546 :group
'org-export-latex
549 (string :tag
"Derived from buffer")
550 (string :tag
"Use this instead"))))
552 (defcustom org-latex-title-command
"\\maketitle"
553 "The command used to insert the title just after \\begin{document}.
555 This format string may contain these elements:
557 %a for AUTHOR keyword
559 %s for SUBTITLE keyword
561 %d for DESCRIPTION line
563 %l for Language keyword
564 %L for capitalized language keyword
567 If you need to use a \"%\" character, you need to escape it
570 Setting :latex-title-command in publishing projects will take
571 precedence over this variable."
572 :group
'org-export-latex
573 :type
'(string :tag
"Format string"))
575 (defcustom org-latex-subtitle-format
"\\\\\\medskip\n\\large %s"
576 "Format string used for transcoded subtitle.
577 The format string should have at most one \"%s\"-expression,
578 which is replaced with the subtitle."
579 :group
'org-export-latex
581 :package-version
'(Org .
"8.3")
582 :type
'(string :tag
"Format string"))
584 (defcustom org-latex-subtitle-separate nil
585 "Non-nil means the subtitle is not typeset as part of title."
586 :group
'org-export-latex
588 :package-version
'(Org .
"8.3")
591 (defcustom org-latex-toc-command
"\\tableofcontents\n\n"
592 "LaTeX command to set the table of contents, list of figures, etc.
593 This command only applies to the table of contents generated with
594 the toc:nil option, not to those generated with #+TOC keyword."
595 :group
'org-export-latex
598 (defcustom org-latex-hyperref-template
599 "\\hypersetup{\n pdfauthor={%a},\n pdftitle={%t},\n pdfkeywords={%k},
600 pdfsubject={%d},\n pdfcreator={%c}, \n pdflang={%L}}\n"
601 "Template for hyperref package options.
603 This format string may contain these elements:
605 %a for AUTHOR keyword
607 %s for SUBTITLE keyword
609 %d for DESCRIPTION line
611 %l for Language keyword
612 %L for capitalized language keyword
615 If you need to use a \"%\" character, you need to escape it
618 As a special case, a nil value prevents template from being
621 Setting :latex-hyperref-template in publishing projects will take
622 precedence over this variable."
623 :group
'org-export-latex
625 :package-version
'(Org .
"8.3")
626 :type
'(choice (const :tag
"No template" nil
)
627 (string :tag
"Format string")))
631 (defcustom org-latex-format-headline-function
632 'org-latex-format-headline-default-function
633 "Function for formatting the headline's text.
635 This function will be called with six arguments:
636 TODO the todo keyword (string or nil)
637 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
638 PRIORITY the priority of the headline (integer or nil)
639 TEXT the main headline text (string)
640 TAGS the tags (list of strings or nil)
641 INFO the export options (plist)
643 The function result will be used in the section format string."
644 :group
'org-export-latex
646 :package-version
'(Org .
"8.0")
652 (defcustom org-latex-footnote-separator
"\\textsuperscript{,}\\,"
653 "Text used to separate footnotes."
654 :group
'org-export-latex
657 (defcustom org-latex-footnote-defined-format
"\\textsuperscript{\\ref{%s}}"
658 "Format string used to format reference to footnote already defined.
659 %s will be replaced by the label of the referred footnote."
660 :group
'org-export-latex
662 (const :tag
"Use plain superscript (default)" "\\textsuperscript{\\ref{%s}}")
663 (const :tag
"Use Memoir/KOMA-Script footref" "\\footref{%s}")
664 (string :tag
"Other format string"))
666 :package-version
'(Org .
"9.0"))
670 (defcustom org-latex-active-timestamp-format
"\\textit{%s}"
671 "A printf format string to be applied to active timestamps."
672 :group
'org-export-latex
675 (defcustom org-latex-inactive-timestamp-format
"\\textit{%s}"
676 "A printf format string to be applied to inactive timestamps."
677 :group
'org-export-latex
680 (defcustom org-latex-diary-timestamp-format
"\\textit{%s}"
681 "A printf format string to be applied to diary timestamps."
682 :group
'org-export-latex
688 (defcustom org-latex-images-centered t
689 "When non-nil, images are centered."
690 :group
'org-export-latex
692 :package-version
'(Org .
"9.0")
696 (defcustom org-latex-image-default-option
""
697 "Default option for images."
698 :group
'org-export-latex
700 :package-version
'(Org .
"8.0")
703 (defcustom org-latex-image-default-width
".9\\linewidth"
704 "Default width for images.
705 This value will not be used if a height is provided."
706 :group
'org-export-latex
708 :package-version
'(Org .
"8.0")
711 (defcustom org-latex-image-default-height
""
712 "Default height for images.
713 This value will not be used if a width is provided, or if the
714 image is wrapped within a \"figure\" or \"wrapfigure\"
716 :group
'org-export-latex
718 :package-version
'(Org .
"8.0")
721 (defcustom org-latex-default-figure-position
"htbp"
722 "Default position for LaTeX figures."
723 :group
'org-export-latex
726 :package-version
'(Org .
"9.0")
729 (defcustom org-latex-inline-image-rules
730 `(("file" .
,(regexp-opt
731 '("pdf" "jpeg" "jpg" "png" "ps" "eps" "tikz" "pgf" "svg"))))
732 "Rules characterizing image files that can be inlined into LaTeX.
734 A rule consists in an association whose key is the type of link
735 to consider, and value is a regexp that will be matched against
738 Note that, by default, the image extension *actually* allowed
739 depend on the way the LaTeX file is processed. When used with
740 pdflatex, pdf, jpg and png images are OK. When processing
741 through dvi to Postscript, only ps and eps are allowed. The
742 default we use here encompasses both."
743 :group
'org-export-latex
745 :package-version
'(Org .
"8.0")
746 :type
'(alist :key-type
(string :tag
"Type")
747 :value-type
(regexp :tag
"Path")))
749 (defcustom org-latex-link-with-unknown-path-format
"\\texttt{%s}"
750 "Format string for links with unknown path type."
751 :group
'org-export-latex
757 (defcustom org-latex-default-table-environment
"tabular"
758 "Default environment used to build tables."
759 :group
'org-export-latex
761 :package-version
'(Org .
"8.0")
764 (defcustom org-latex-default-table-mode
'table
765 "Default mode for tables.
767 Value can be a symbol among:
769 `table' Regular LaTeX table.
771 `math' In this mode, every cell is considered as being in math
772 mode and the complete table will be wrapped within a math
773 environment. It is particularly useful to write matrices.
775 `inline-math' This mode is almost the same as `math', but the
776 math environment will be inlined.
778 `verbatim' The table is exported as it appears in the Org
779 buffer, within a verbatim environment.
781 This value can be overridden locally with, i.e. \":mode math\" in
784 When modifying this variable, it may be useful to change
785 `org-latex-default-table-environment' accordingly."
786 :group
'org-export-latex
788 :package-version
'(Org .
"8.0")
789 :type
'(choice (const :tag
"Table" table
)
790 (const :tag
"Matrix" math
)
791 (const :tag
"Inline matrix" inline-math
)
792 (const :tag
"Verbatim" verbatim
))
793 :safe
(lambda (s) (memq s
'(table math inline-math verbatim
))))
795 (defcustom org-latex-tables-centered t
796 "When non-nil, tables are exported in a center environment."
797 :group
'org-export-latex
801 (defcustom org-latex-tables-booktabs nil
802 "When non-nil, display tables in a formal \"booktabs\" style.
803 This option assumes that the \"booktabs\" package is properly
804 loaded in the header of the document. This value can be ignored
805 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
807 :group
'org-export-latex
809 :package-version
'(Org .
"8.0")
813 (defcustom org-latex-table-scientific-notation
"%s\\,(%s)"
814 "Format string to display numbers in scientific notation.
815 The format should have \"%s\" twice, for mantissa and exponent
816 \(i.e., \"%s\\\\times10^{%s}\").
818 When nil, no transformation is made."
819 :group
'org-export-latex
821 :package-version
'(Org .
"8.0")
823 (string :tag
"Format string")
824 (const :tag
"No formatting" nil
)))
828 (defcustom org-latex-text-markup-alist
'((bold .
"\\textbf{%s}")
829 (code . protectedtexttt
)
830 (italic .
"\\emph{%s}")
831 (strike-through .
"\\sout{%s}")
832 (underline .
"\\uline{%s}")
833 (verbatim . protectedtexttt
))
834 "Alist of LaTeX expressions to convert text markup.
836 The key must be a symbol among `bold', `code', `italic',
837 `strike-through', `underline' and `verbatim'. The value is
838 a formatting string to wrap fontified text with.
840 Value can also be set to the following symbols: `verb' and
841 `protectedtexttt'. For the former, Org will use \"\\verb\" to
842 create a format string and select a delimiter character that
843 isn't in the string. For the latter, Org will use \"\\texttt\"
844 to typeset and try to protect special characters.
846 If no association can be found for a given markup, text will be
848 :group
'org-export-latex
850 :package-version
'(Org .
"8.3")
852 :options
'(bold code italic strike-through underline verbatim
))
857 (defcustom org-latex-format-drawer-function
(lambda (_ contents
) contents
)
858 "Function called to format a drawer in LaTeX code.
860 The function must accept two parameters:
861 NAME the drawer name, like \"LOGBOOK\"
862 CONTENTS the contents of the drawer.
864 The function should return the string to be exported.
866 The default function simply returns the value of CONTENTS."
867 :group
'org-export-latex
869 :package-version
'(Org .
"8.3")
875 (defcustom org-latex-format-inlinetask-function
876 'org-latex-format-inlinetask-default-function
877 "Function called to format an inlinetask in LaTeX code.
879 The function must accept seven parameters:
880 TODO the todo keyword (string or nil)
881 TODO-TYPE the todo type (symbol: `todo', `done', nil)
882 PRIORITY the inlinetask priority (integer or nil)
883 NAME the inlinetask name (string)
884 TAGS the inlinetask tags (list of strings or nil)
885 CONTENTS the contents of the inlinetask (string or nil)
886 INFO the export options (plist)
888 The function should return the string to be exported."
889 :group
'org-export-latex
892 :package-version
'(Org .
"8.3"))
897 (defcustom org-latex-listings nil
898 "Non-nil means export source code using the listings package.
900 This package will fontify source code, possibly even with color.
901 If you want to use this, you also need to make LaTeX use the
902 listings package, and if you want to have color, the color
903 package. Just add these to `org-latex-packages-alist', for
904 example using customize, or with something like:
906 (require \\='ox-latex)
907 (add-to-list \\='org-latex-packages-alist \\='(\"\" \"listings\"))
908 (add-to-list \\='org-latex-packages-alist \\='(\"\" \"color\"))
912 (setq org-latex-listings \\='minted)
914 causes source code to be exported using the minted package as
915 opposed to listings. If you want to use minted, you need to add
916 the minted package to `org-latex-packages-alist', for example
917 using customize, or with
919 (require \\='ox-latex)
920 (add-to-list \\='org-latex-packages-alist \\='(\"newfloat\" \"minted\"))
922 In addition, it is necessary to install pygments
923 \(http://pygments.org), and to configure the variable
924 `org-latex-pdf-process' so that the -shell-escape option is
927 The minted choice has possible repercussions on the preview of
928 latex fragments (see `org-preview-latex-fragment'). If you run
929 into previewing problems, please consult
931 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
932 :group
'org-export-latex
934 (const :tag
"Use listings" t
)
935 (const :tag
"Use minted" minted
)
936 (const :tag
"Export verbatim" nil
))
937 :safe
(lambda (s) (memq s
'(t nil minted
))))
939 (defcustom org-latex-listings-langs
940 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
943 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
944 (html "HTML") (xml "XML")
945 (tex "TeX") (latex "[LaTeX]TeX")
946 (shell-script "bash")
948 (ocaml "Caml") (caml "Caml")
949 (sql "SQL") (sqlite "sql")
952 "Alist mapping languages to their listing language counterpart.
953 The key is a symbol, the major mode symbol without the \"-mode\".
954 The value is the string that should be inserted as the language
955 parameter for the listings package. If the mode name and the
956 listings name are the same, the language does not need an entry
957 in this list - but it does not hurt if it is present."
958 :group
'org-export-latex
960 :package-version
'(Org .
"8.3")
963 (symbol :tag
"Major mode ")
964 (string :tag
"Listings language"))))
966 (defcustom org-latex-listings-options nil
967 "Association list of options for the latex listings package.
969 These options are supplied as a comma-separated list to the
970 \\lstset command. Each element of the association list should be
971 a list containing two strings: the name of the option, and the
974 (setq org-latex-listings-options
975 \\='((\"basicstyle\" \"\\\\small\")
976 (\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
978 will typeset the code in a small size font with underlined, bold
981 Note that the same options will be applied to blocks of all
982 languages. If you need block-specific options, you may use the
985 #+ATTR_LATEX: :options key1=value1,key2=value2
989 :group
'org-export-latex
992 (string :tag
"Listings option name ")
993 (string :tag
"Listings option value"))))
995 (defcustom org-latex-minted-langs
996 '((emacs-lisp "common-lisp")
999 (shell-script "bash")
1001 "Alist mapping languages to their minted language counterpart.
1002 The key is a symbol, the major mode symbol without the \"-mode\".
1003 The value is the string that should be inserted as the language
1004 parameter for the minted package. If the mode name and the
1005 listings name are the same, the language does not need an entry
1006 in this list - but it does not hurt if it is present.
1008 Note that minted uses all lower case for language identifiers,
1009 and that the full list of language identifiers can be obtained
1012 pygmentize -L lexers"
1013 :group
'org-export-latex
1016 (symbol :tag
"Major mode ")
1017 (string :tag
"Minted language"))))
1019 (defcustom org-latex-minted-options nil
1020 "Association list of options for the latex minted package.
1022 These options are supplied within square brackets in
1023 \\begin{minted} environments. Each element of the alist should
1024 be a list containing two strings: the name of the option, and the
1027 (setq org-latex-minted-options
1028 \\='((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
1030 will result in src blocks being exported with
1032 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
1034 as the start of the minted environment. Note that the same
1035 options will be applied to blocks of all languages. If you need
1036 block-specific options, you may use the following syntax:
1038 #+ATTR_LATEX: :options key1=value1,key2=value2
1042 :group
'org-export-latex
1045 (string :tag
"Minted option name ")
1046 (string :tag
"Minted option value"))))
1048 (defcustom org-latex-custom-lang-environments nil
1049 "Alist mapping languages to language-specific LaTeX environments.
1051 It is used during export of src blocks by the listings and minted
1052 latex packages. The environment may be a simple string, composed of
1053 only letters and numbers. In this case, the string is directly the
1054 name of the latex environment to use. The environment may also be
1055 a format string. In this case the format string will be directly
1056 exported. This format string may contain these elements:
1058 %s for the formatted source
1060 %f for the float attribute
1061 %l for an appropriate label
1062 %o for the LaTeX attributes
1066 (setq org-latex-custom-lang-environments
1067 \\='((python \"pythoncode\")
1068 (ocaml \"\\\\begin{listing}
1069 \\\\begin{minted}[%o]{ocaml}
1074 would have the effect that if Org encounters a Python source block
1075 during LaTeX export it will produce
1081 and if Org encounters an Ocaml source block during LaTeX export it
1085 \\begin{minted}[<attr_latex options>]{ocaml}
1088 \\caption{<caption>}
1091 :group
'org-export-latex
1094 (symbol :tag
"Language name ")
1095 (string :tag
"Environment name or format string")))
1097 :package-version
'(Org .
"9.0"))
1102 (defcustom org-latex-compiler-file-string
"%% Intended LaTeX compiler: %s\n"
1103 "LaTeX compiler format-string.
1104 See also `org-latex-compiler'."
1105 :group
'org-export-latex
1107 (const :tag
"Comment" "%% Intended LaTeX compiler: %s\n")
1108 (const :tag
"latex-mode file variable" "%% -*- latex-run-command: %s -*-\n")
1109 (const :tag
"AUCTeX file variable" "%% -*- LaTeX-command: %s -*-\n")
1110 (string :tag
"custom format" "%% %s"))
1112 :package-version
'(Org .
"9.0"))
1114 (defcustom org-latex-compiler
"pdflatex"
1115 "LaTeX compiler to use.
1117 Must be an element in `org-latex-compilers' or the empty quote.
1118 Can also be set in buffers via #+LATEX_COMPILER. See also
1119 `org-latex-compiler-file-string'."
1120 :group
'org-export-latex
1122 (const :tag
"pdfLaTeX" "pdflatex")
1123 (const :tag
"XeLaTeX" "xelatex")
1124 (const :tag
"LuaLaTeX" "lualatex")
1125 (const :tag
"Unset" ""))
1127 :package-version
'(Org .
"9.0"))
1129 (defconst org-latex-compilers
'("pdflatex" "xelatex" "lualatex")
1130 "Known LaTeX compilers.
1131 See also `org-latex-compiler'.")
1133 (defcustom org-latex-bib-compiler
"bibtex"
1134 "Command to process a LaTeX file's bibliography.
1136 The shorthand %bib in `org-latex-pdf-process' is replaced with
1139 A better approach is to use a compiler suit such as `latexmk'."
1140 :group
'org-export-latex
1141 :type
'(choice (const :tag
"BibTeX" "bibtex")
1142 (const :tag
"Biber" "biber")
1143 (string :tag
"Other process"))
1145 :package-version
'(Org .
"9.0"))
1147 (defcustom org-latex-pdf-process
1148 '("%latex -interaction nonstopmode -output-directory %o %f"
1149 "%latex -interaction nonstopmode -output-directory %o %f"
1150 "%latex -interaction nonstopmode -output-directory %o %f")
1151 "Commands to process a LaTeX file to a PDF file.
1153 This is a list of strings, each of them will be given to the
1154 shell as a command. %f in the command will be replaced by the
1155 relative file name, %F by the absolute file name, %b by the file
1156 base name (i.e. without directory and extension parts), %o by the
1157 base directory of the file, %O by the absolute file name of the
1158 output file, %latex is the LaTeX compiler (see
1159 `org-latex-compiler'), and %bib is the BibTeX-like compiler (see
1160 `org-latex-bib-compiler').
1162 The reason why this is a list is that it usually takes several
1163 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
1164 does not have a clever mechanism to detect which of these
1165 commands have to be run to get to a stable result, and it also
1166 does not do any error checking.
1168 Consider a smart LaTeX compiler such as `texi2dvi' or `latexmk',
1169 which calls the \"correct\" combinations of auxiliary programs.
1171 Alternatively, this may be a Lisp function that does the
1172 processing, so you could use this to apply the machinery of
1173 AUCTeX or the Emacs LaTeX mode. This function should accept the
1174 file name as its single argument."
1175 :group
'org-export-pdf
1177 (repeat :tag
"Shell command sequence"
1178 (string :tag
"Shell command"))
1179 (const :tag
"2 runs of latex"
1180 ("%latex -interaction nonstopmode -output-directory %o %f"
1181 "%latex -interaction nonstopmode -output-directory %o %f"))
1182 (const :tag
"3 runs of latex"
1183 ("%latex -interaction nonstopmode -output-directory %o %f"
1184 "%latex -interaction nonstopmode -output-directory %o %f"
1185 "%latex -interaction nonstopmode -output-directory %o %f"))
1186 (const :tag
"latex,bibtex,latex,latex"
1187 ("%latex -interaction nonstopmode -output-directory %o %f"
1189 "%latex -interaction nonstopmode -output-directory %o %f"
1190 "%latex -interaction nonstopmode -output-directory %o %f"))
1191 (const :tag
"texi2dvi"
1192 ("cd %o; LATEX=\"%latex\" texi2dvi -p -b -V %b.tex"))
1193 (const :tag
"latexmk"
1194 ("latexmk -g -pdf -pdflatex=\"%latex\" -outdir=%o %f"))
1197 (defcustom org-latex-logfiles-extensions
1198 '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
1199 "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
1200 "The list of file extensions to consider as LaTeX logfiles.
1201 The logfiles will be removed if `org-latex-remove-logfiles' is
1203 :group
'org-export-latex
1205 :package-version
'(Org .
"8.3")
1206 :type
'(repeat (string :tag
"Extension")))
1208 (defcustom org-latex-remove-logfiles t
1209 "Non-nil means remove the logfiles produced by PDF production.
1210 By default, logfiles are files with these extensions: .aux, .idx,
1211 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
1212 logfiles to remove, set `org-latex-logfiles-extensions'."
1213 :group
'org-export-latex
1216 (defcustom org-latex-known-warnings
1217 '(("Reference.*?undefined" .
"[undefined reference]")
1218 ("Runaway argument" .
"[runaway argument]")
1219 ("Underfull \\hbox" .
"[underfull hbox]")
1220 ("Overfull \\hbox" .
"[overfull hbox]")
1221 ("Citation.*?undefined" .
"[undefined citation]")
1222 ("Undefined control sequence" .
"[undefined control sequence]"))
1223 "Alist of regular expressions and associated messages for the user.
1224 The regular expressions are used to find possible warnings in the
1225 log of a latex-run. These warnings will be reported after
1226 calling `org-latex-compile'."
1227 :group
'org-export-latex
1229 :package-version
'(Org .
"8.3")
1232 (string :tag
"Regexp")
1233 (string :tag
"Message"))))
1237 ;;; Internal Functions
1239 (defun org-latex--caption-above-p (element info
)
1240 "Non nil when caption is expected to be located above ELEMENT.
1241 INFO is a plist holding contextual information."
1242 (let ((above (plist-get info
:latex-caption-above
)))
1243 (if (symbolp above
) above
1244 (let ((type (org-element-type element
)))
1245 (memq (if (eq type
'link
) 'image type
) above
)))))
1247 (defun org-latex--label (datum info
&optional force full
)
1248 "Return an appropriate label for DATUM.
1249 DATUM is an element or a `target' type object. INFO is the
1250 current export state, as a plist.
1252 Return nil if element DATUM has no NAME or VALUE affiliated
1253 keyword or no CUSTOM_ID property, unless FORCE is non-nil. In
1254 this case always return a unique label.
1256 Eventually, if FULL is non-nil, wrap label within \"\\label{}\"."
1257 (let* ((type (org-element-type datum
))
1259 (org-element-property
1261 ((headline inlinetask
) :CUSTOM_ID
)
1266 (and (or user-label force
)
1267 (if (and user-label
(plist-get info
:latex-prefer-user-labels
))
1269 (concat (cl-case type
1273 (and (string-match-p
1274 org-latex-math-environments-re
1275 (org-element-property :value datum
))
1278 (and (org-element-property :caption datum
)
1280 (org-export-get-reference datum info
))))))
1281 (cond ((not full
) label
)
1282 (label (format "\\label{%s}%s"
1284 (if (eq type
'target
) "" "\n")))
1287 (defun org-latex--caption/label-string
(element info
)
1288 "Return caption and label LaTeX string for ELEMENT.
1290 INFO is a plist holding contextual information. If there's no
1291 caption nor label, return the empty string.
1293 For non-floats, see `org-latex--wrap-label'."
1294 (let* ((label (org-latex--label element info nil t
))
1295 (main (org-export-get-caption element
))
1296 (attr (org-export-read-attribute :attr_latex element
))
1297 (type (org-element-type element
))
1298 (nonfloat (or (and (plist-member attr
:float
)
1299 (not (plist-get attr
:float
))
1301 (and (eq type
'src-block
)
1302 (not (plist-get attr
:float
))
1303 (null (plist-get info
:latex-listings
)))))
1304 (short (org-export-get-caption element t
))
1305 (caption-from-attr-latex (plist-get attr
:caption
)))
1307 ((org-string-nw-p caption-from-attr-latex
)
1308 (concat caption-from-attr-latex
"\n"))
1309 ((and (not main
) (equal label
"")) "")
1311 ;; Option caption format with short name.
1313 (format (if nonfloat
"\\captionof{%s}%s{%s%s}\n"
1314 "\\caption%s%s{%s%s}\n")
1317 (paragraph "figure")
1318 (src-block (if (plist-get info
:latex-listings
)
1321 (t (symbol-name type
)))
1323 (if short
(format "[%s]" (org-export-data short info
)) "")
1325 (org-export-data main info
))))))
1327 (defun org-latex-guess-inputenc (header)
1328 "Set the coding system in inputenc to what the buffer is.
1330 HEADER is the LaTeX header string. This function only applies
1331 when specified inputenc option is \"AUTO\".
1333 Return the new header, as a string."
1334 (let* ((cs (or (ignore-errors
1335 (latexenc-coding-system-to-inputenc
1336 (or org-export-coding-system buffer-file-coding-system
)))
1339 ;; First translate if that is requested.
1340 (setq cs
(or (cdr (assoc cs org-latex-inputenc-alist
)) cs
))
1341 ;; Then find the \usepackage statement and replace the option.
1342 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1343 cs header t nil
1))))
1345 (defun org-latex-guess-babel-language (header info
)
1346 "Set Babel's language according to LANGUAGE keyword.
1348 HEADER is the LaTeX header string. INFO is the plist used as
1349 a communication channel.
1351 Insertion of guessed language only happens when Babel package has
1352 explicitly been loaded. Then it is added to the rest of
1355 The argument to Babel may be \"AUTO\" which is then replaced with
1356 the language of the document or `org-export-default-language'
1357 unless language in question is already loaded.
1359 Return the new header."
1360 (let ((language-code (plist-get info
:language
)))
1361 ;; If no language is set or Babel package is not loaded, return
1363 (if (or (not (stringp language-code
))
1364 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header
)))
1366 (let ((options (save-match-data
1367 (org-split-string (match-string 1 header
) ",[ \t]*")))
1368 (language (cdr (assoc-string language-code
1369 org-latex-babel-language-alist t
))))
1370 ;; If LANGUAGE is already loaded, return header without AUTO.
1371 ;; Otherwise, replace AUTO with language or append language if
1372 ;; AUTO is not present.
1374 (mapconcat (lambda (option) (if (equal "AUTO" option
) language option
))
1375 (cond ((member language options
) (delete "AUTO" options
))
1376 ((member "AUTO" options
) options
)
1377 (t (append options
(list language
))))
1381 (defun org-latex-guess-polyglossia-language (header info
)
1382 "Set the Polyglossia language according to the LANGUAGE keyword.
1384 HEADER is the LaTeX header string. INFO is the plist used as
1385 a communication channel.
1387 Insertion of guessed language only happens when the Polyglossia
1388 package has been explicitly loaded.
1390 The argument to Polyglossia may be \"AUTO\" which is then
1391 replaced with the language of the document or
1392 `org-export-default-language'. Note, the language is really set
1393 using \setdefaultlanguage and not as an option to the package.
1395 Return the new header."
1396 (let ((language (plist-get info
:language
)))
1397 ;; If no language is set or Polyglossia is not loaded, return
1399 (if (or (not (stringp language
))
1401 "\\\\usepackage\\(?:\\[\\([^]]+?\\)\\]\\){polyglossia}\n"
1404 (let* ((options (org-string-nw-p (match-string 1 header
)))
1405 (languages (and options
1406 ;; Reverse as the last loaded language is
1407 ;; the main language.
1412 (replace-regexp-in-string
1413 "AUTO" language options t
)
1416 (string-match-p "\\\\setmainlanguage{.*?}" header
)))
1418 (concat "\\usepackage{polyglossia}\n"
1421 (let ((l (or (assoc l org-latex-polyglossia-language-alist
)
1423 (format (if main-language-set
"\\setotherlanguage%s{%s}\n"
1424 (setq main-language-set t
)
1425 "\\setmainlanguage%s{%s}\n")
1426 (if (and (consp l
) (= (length l
) 3))
1427 (format "[variant=%s]" (nth 2 l
))
1434 (defun org-latex--remove-packages (pkg-alist info
)
1435 "Remove packages based on the current LaTeX compiler.
1437 If the fourth argument of an element is set in pkg-alist, and it
1438 is not a member of the LaTeX compiler of the document, the packages
1439 is removed. See also `org-latex-compiler'.
1441 Return modified pkg-alist."
1442 (let ((compiler (or (plist-get info
:latex-compiler
) "")))
1443 (if (member-ignore-case compiler org-latex-compilers
)
1449 (let ((third (nth 3 pkg
)))
1451 (not (member-ignore-case
1453 (if (listp third
) third
(list third
)))))))
1458 (defun org-latex--find-verb-separator (s)
1459 "Return a character not used in string S.
1460 This is used to choose a separator for constructs like \\verb."
1461 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1462 (cl-loop for c across ll
1463 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
1464 return
(char-to-string c
))))
1466 (defun org-latex--make-option-string (options)
1467 "Return a comma separated string of keywords and values.
1468 OPTIONS is an alist where the key is the options keyword as
1469 a string, and the value a list containing the keyword value, or
1471 (mapconcat (lambda (pair)
1472 (pcase-let ((`(,keyword
,value
) pair
))
1474 (and (> (length value
) 0)
1475 (concat "=" value
)))))
1479 (defun org-latex--wrap-label (element output info
)
1480 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1481 INFO is the current export state, as a plist. This function
1482 should not be used for floats. See
1483 `org-latex--caption/label-string'."
1484 (if (not (and (org-string-nw-p output
) (org-element-property :name element
)))
1486 (concat (format "\\phantomsection\n\\label{%s}\n"
1487 (org-latex--label element info
))
1490 (defun org-latex--protect-text (text)
1491 "Protect special characters in string TEXT and return it."
1492 (replace-regexp-in-string "[\\{}$%&_#~^]" "\\\\\\&" text
))
1494 (defun org-latex--text-markup (text markup info
)
1495 "Format TEXT depending on MARKUP text markup.
1496 INFO is a plist used as a communication channel. See
1497 `org-latex-text-markup-alist' for details."
1498 (let ((fmt (cdr (assq markup
(plist-get info
:latex-text-markup-alist
)))))
1500 ;; No format string: Return raw text.
1502 ;; Handle the `verb' special case: Find an appropriate separator
1503 ;; and use "\\verb" command.
1505 (let ((separator (org-latex--find-verb-separator text
)))
1508 (replace-regexp-in-string "\n" " " text
)
1510 ;; Handle the `protectedtexttt' special case: Protect some
1511 ;; special chars and use "\texttt{%s}" format string.
1513 (format "\\texttt{%s}"
1514 (replace-regexp-in-string
1515 "--\\|[\\{}$%&_#~^]"
1517 (cond ((equal m
"--") "-{}-")
1518 ((equal m
"\\") "\\textbackslash{}")
1519 ((equal m
"~") "\\textasciitilde{}")
1520 ((equal m
"^") "\\textasciicircum{}")
1521 (t (org-latex--protect-text m
))))
1523 ;; Else use format string.
1524 (t (format fmt text
)))))
1526 (defun org-latex--delayed-footnotes-definitions (element info
)
1527 "Return footnotes definitions in ELEMENT as a string.
1529 INFO is a plist used as a communication channel.
1531 Footnotes definitions are returned within \"\\footnotetext{}\"
1534 This function is used within constructs that don't support
1535 \"\\footnote{}\" command (e.g., an item tag). In that case,
1536 \"\\footnotemark\" is used within the construct and the function
1537 just outside of it."
1540 (let ((def (org-export-get-footnote-definition ref info
)))
1541 (format "\\footnotetext[%d]{%s%s}"
1542 (org-export-get-footnote-number ref info
)
1543 (org-trim (org-latex--label def info t t
))
1544 (org-trim (org-export-data def info
)))))
1545 ;; Find every footnote reference in ELEMENT.
1546 (letrec ((all-refs nil
)
1549 ;; Return a list of all footnote references never seen
1551 (org-element-map data
'footnote-reference
1553 (when (org-export-footnote-first-reference-p ref info
)
1555 (when (eq (org-element-property :type ref
) 'standard
)
1556 (funcall search-refs
1557 (org-export-get-footnote-definition ref info
)))))
1559 (reverse all-refs
))))
1560 (funcall search-refs element
))
1563 (defun org-latex--translate (s info
)
1564 "Translate string S according to specified language.
1565 INFO is a plist used as a communication channel."
1566 (org-export-translate s
:latex info
))
1568 (defun org-latex--format-spec (info)
1569 "Create a format-spec for document meta-data.
1570 INFO is a plist used as a communication channel."
1571 (let ((language (let ((lang (plist-get info
:language
)))
1572 (or (cdr (assoc-string lang org-latex-babel-language-alist t
))
1573 (nth 1 (assoc-string lang org-latex-polyglossia-language-alist t
))
1575 `((?a .
,(org-export-data (plist-get info
:author
) info
))
1576 (?t .
,(org-export-data (plist-get info
:title
) info
))
1577 (?k .
,(org-export-data (org-latex--wrap-latex-math-block
1578 (plist-get info
:keywords
) info
)
1580 (?d .
,(org-export-data (org-latex--wrap-latex-math-block
1581 (plist-get info
:description
) info
)
1583 (?c .
,(plist-get info
:creator
))
1585 (?L .
,(capitalize language
))
1586 (?D .
,(org-export-get-date info
)))))
1588 (defun org-latex--insert-compiler (info)
1589 "Insert LaTeX_compiler info into the document.
1590 INFO is a plist used as a communication channel."
1591 (let ((compiler (plist-get info
:latex-compiler
)))
1592 (and (org-string-nw-p org-latex-compiler-file-string
)
1593 (member (or compiler
"") org-latex-compilers
)
1594 (format org-latex-compiler-file-string compiler
))))
1599 (defun org-latex-matrices-tree-filter (tree _backend info
)
1600 (org-latex--wrap-latex-matrices tree info
))
1602 (defun org-latex-math-block-tree-filter (tree _backend info
)
1603 (org-latex--wrap-latex-math-block tree info
))
1605 (defun org-latex-math-block-options-filter (info _backend
)
1606 (dolist (prop '(:author
:date
:title
) info
)
1607 (plist-put info prop
1608 (org-latex--wrap-latex-math-block (plist-get info prop
) info
))))
1610 (defun org-latex-clean-invalid-line-breaks (data _backend _info
)
1611 (replace-regexp-in-string
1612 "\\(\\end{[A-Za-z0-9*]+}\\|^\\)[ \t]*\\\\\\\\[ \t]*$" "\\1"
1619 (defun org-latex-make-preamble (info &optional template snippet?
)
1620 "Return a formatted LaTeX preamble.
1621 INFO is a plist used as a communication channel. Optional
1622 argument TEMPLATE, when non-nil, is the header template string,
1623 as expected by `org-splice-latex-header'. When SNIPPET? is
1624 non-nil, only includes packages relevant to image generation, as
1625 specified in `org-latex-default-packages-alist' or
1626 `org-latex-packages-alist'."
1627 (let* ((class (plist-get info
:latex-class
))
1628 (class-options (plist-get info
:latex-class-options
))
1629 (header (nth 1 (assoc class
(plist-get info
:latex-classes
))))
1632 (and (stringp header
)
1633 (if (not class-options
) header
1634 (replace-regexp-in-string
1635 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1636 class-options header t nil
1)))
1637 (user-error "Unknown LaTeX class `%s'" class
))))
1638 (org-latex-guess-polyglossia-language
1639 (org-latex-guess-babel-language
1640 (org-latex-guess-inputenc
1641 (org-element-normalize-string
1642 (org-splice-latex-header
1644 (org-latex--remove-packages org-latex-default-packages-alist info
)
1645 (org-latex--remove-packages org-latex-packages-alist info
)
1647 (mapconcat #'org-element-normalize-string
1648 (list (plist-get info
:latex-header
)
1649 (plist-get info
:latex-header-extra
)) ""))))
1653 (defun org-latex-template (contents info
)
1654 "Return complete document string after LaTeX conversion.
1655 CONTENTS is the transcoded contents string. INFO is a plist
1656 holding export options."
1657 (let ((title (org-export-data (plist-get info
:title
) info
))
1658 (spec (org-latex--format-spec info
)))
1661 (and (plist-get info
:time-stamp-file
)
1662 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1664 (org-latex--insert-compiler info
)
1665 ;; Document class and packages.
1666 (org-latex-make-preamble info
)
1667 ;; Possibly limit depth for headline numbering.
1668 (let ((sec-num (plist-get info
:section-numbers
)))
1669 (when (integerp sec-num
)
1670 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
1672 (let ((author (and (plist-get info
:with-author
)
1673 (let ((auth (plist-get info
:author
)))
1674 (and auth
(org-export-data auth info
)))))
1675 (email (and (plist-get info
:with-email
)
1676 (org-export-data (plist-get info
:email
) info
))))
1677 (cond ((and author email
(not (string= "" email
)))
1678 (format "\\author{%s\\thanks{%s}}\n" author email
))
1679 ((or author email
) (format "\\author{%s}\n" (or author email
)))))
1681 (let ((date (and (plist-get info
:with-date
) (org-export-get-date info
))))
1682 (format "\\date{%s}\n" (org-export-data date info
)))
1683 ;; Title and subtitle.
1684 (let* ((subtitle (plist-get info
:subtitle
))
1687 (format (plist-get info
:latex-subtitle-format
)
1688 (org-export-data subtitle info
))))
1689 (separate (plist-get info
:latex-subtitle-separate
)))
1691 (format "\\title{%s%s}\n" title
1692 (if separate
"" (or formatted-subtitle
"")))
1693 (when (and separate subtitle
)
1694 (concat formatted-subtitle
"\n"))))
1695 ;; Hyperref options.
1696 (let ((template (plist-get info
:latex-hyperref-template
)))
1697 (and (stringp template
)
1698 (format-spec template spec
)))
1700 "\\begin{document}\n\n"
1702 (let* ((title-command (plist-get info
:latex-title-command
))
1703 (command (and (stringp title-command
)
1704 (format-spec title-command spec
))))
1705 (org-element-normalize-string
1706 (cond ((not (plist-get info
:with-title
)) nil
)
1707 ((string= "" title
) nil
)
1708 ((not (stringp command
)) nil
)
1709 ((string-match "\\(?:[^%]\\|^\\)%s" command
)
1710 (format command title
))
1712 ;; Table of contents.
1713 (let ((depth (plist-get info
:with-toc
)))
1715 (concat (when (wholenump depth
)
1716 (format "\\setcounter{tocdepth}{%d}\n" depth
))
1717 (plist-get info
:latex-toc-command
))))
1721 (and (plist-get info
:with-creator
)
1722 (concat (plist-get info
:creator
) "\n"))
1724 "\\end{document}")))
1728 ;;; Transcode Functions
1732 (defun org-latex-bold (_bold contents info
)
1733 "Transcode BOLD from Org to LaTeX.
1734 CONTENTS is the text with bold markup. INFO is a plist holding
1735 contextual information."
1736 (org-latex--text-markup contents
'bold info
))
1741 (defun org-latex-center-block (center-block contents info
)
1742 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1743 CONTENTS holds the contents of the center block. INFO is a plist
1744 holding contextual information."
1745 (org-latex--wrap-label
1746 center-block
(format "\\begin{center}\n%s\\end{center}" contents
) info
))
1751 (defun org-latex-clock (clock _contents info
)
1752 "Transcode a CLOCK element from Org to LaTeX.
1753 CONTENTS is nil. INFO is a plist holding contextual
1757 (format "\\textbf{%s} " org-clock-string
)
1758 (format (plist-get info
:latex-inactive-timestamp-format
)
1759 (concat (org-timestamp-translate (org-element-property :value clock
))
1760 (let ((time (org-element-property :duration clock
)))
1761 (and time
(format " (%s)" time
)))))
1767 (defun org-latex-code (code _contents info
)
1768 "Transcode a CODE object from Org to LaTeX.
1769 CONTENTS is nil. INFO is a plist used as a communication
1771 (org-latex--text-markup (org-element-property :value code
) 'code info
))
1776 (defun org-latex-drawer (drawer contents info
)
1777 "Transcode a DRAWER element from Org to LaTeX.
1778 CONTENTS holds the contents of the block. INFO is a plist
1779 holding contextual information."
1780 (let* ((name (org-element-property :drawer-name drawer
))
1781 (output (funcall (plist-get info
:latex-format-drawer-function
)
1783 (org-latex--wrap-label drawer output info
)))
1788 (defun org-latex-dynamic-block (dynamic-block contents info
)
1789 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1790 CONTENTS holds the contents of the block. INFO is a plist
1791 holding contextual information. See `org-export-data'."
1792 (org-latex--wrap-label dynamic-block contents info
))
1797 (defun org-latex-entity (entity _contents _info
)
1798 "Transcode an ENTITY object from Org to LaTeX.
1799 CONTENTS are the definition itself. INFO is a plist holding
1800 contextual information."
1801 (org-element-property :latex entity
))
1806 (defun org-latex-example-block (example-block _contents info
)
1807 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1808 CONTENTS is nil. INFO is a plist holding contextual
1810 (when (org-string-nw-p (org-element-property :value example-block
))
1811 (let ((environment (or (org-export-read-attribute
1812 :attr_latex example-block
:environment
)
1814 (org-latex--wrap-label
1816 (format "\\begin{%s}\n%s\\end{%s}"
1818 (org-export-format-code-default example-block info
)
1825 (defun org-latex-export-block (export-block _contents _info
)
1826 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1827 CONTENTS is nil. INFO is a plist holding contextual information."
1828 (when (member (org-element-property :type export-block
) '("LATEX" "TEX"))
1829 (org-remove-indentation (org-element-property :value export-block
))))
1834 (defun org-latex-export-snippet (export-snippet _contents _info
)
1835 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1836 CONTENTS is nil. INFO is a plist holding contextual information."
1837 (when (eq (org-export-snippet-backend export-snippet
) 'latex
)
1838 (org-element-property :value export-snippet
)))
1843 (defun org-latex-fixed-width (fixed-width _contents info
)
1844 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1845 CONTENTS is nil. INFO is a plist holding contextual information."
1846 (org-latex--wrap-label
1848 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1849 (org-remove-indentation
1850 (org-element-property :value fixed-width
)))
1854 ;;;; Footnote Reference
1856 (defun org-latex-footnote-reference (footnote-reference _contents info
)
1857 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1858 CONTENTS is nil. INFO is a plist holding contextual information."
1859 (let ((label (org-element-property :label footnote-reference
)))
1861 ;; Insert separator between two footnotes in a row.
1862 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1863 (when (eq (org-element-type prev
) 'footnote-reference
)
1864 (plist-get info
:latex-footnote-separator
)))
1866 ;; Use `:latex-footnote-defined-format' if the footnote has
1867 ;; already been defined.
1868 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1869 (format (plist-get info
:latex-footnote-defined-format
)
1871 (org-export-get-footnote-definition footnote-reference info
)
1873 ;; Use \footnotemark if reference is within another footnote
1874 ;; reference, footnote definition or table cell.
1875 ((org-element-lineage footnote-reference
1876 '(footnote-reference footnote-definition table-cell
))
1878 ;; Otherwise, define it with \footnote command.
1880 (let ((def (org-export-get-footnote-definition footnote-reference info
)))
1882 (format "\\footnote{%s%s}" (org-trim (org-export-data def info
))
1883 ;; Only insert a \label if there exist another
1884 ;; reference to def.
1885 (cond ((not label
) "")
1886 ((org-element-map (plist-get info
:parse-tree
) 'footnote-reference
1888 (and (not (eq f footnote-reference
))
1889 (equal (org-element-property :label f
) label
)
1890 (org-trim (org-latex--label def info t t
))))
1893 ;; Retrieve all footnote references within the footnote and
1894 ;; add their definition after it, since LaTeX doesn't support
1896 (org-latex--delayed-footnotes-definitions def info
))))))))
1901 (defun org-latex-headline (headline contents info
)
1902 "Transcode a HEADLINE element from Org to LaTeX.
1903 CONTENTS holds the contents of the headline. INFO is a plist
1904 holding contextual information."
1905 (unless (org-element-property :footnote-section-p headline
)
1906 (let* ((class (plist-get info
:latex-class
))
1907 (level (org-export-get-relative-level headline info
))
1908 (numberedp (org-export-numbered-headline-p headline info
))
1909 (class-sectioning (assoc class
(plist-get info
:latex-classes
)))
1910 ;; Section formatting will set two placeholders: one for
1911 ;; the title and the other for the contents.
1913 (let ((sec (if (functionp (nth 2 class-sectioning
))
1914 (funcall (nth 2 class-sectioning
) level numberedp
)
1915 (nth (1+ level
) class-sectioning
))))
1917 ;; No section available for that LEVEL.
1919 ;; Section format directly returned by a function. Add
1920 ;; placeholder for contents.
1921 ((stringp sec
) (concat sec
"\n%s"))
1922 ;; (numbered-section . unnumbered-section)
1923 ((not (consp (cdr sec
)))
1924 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
1925 ;; (numbered-open numbered-close)
1927 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
1928 ;; (num-in num-out no-num-in no-num-out)
1930 (if numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))
1931 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
1932 ;; Create a temporary export back-end that hard-codes
1933 ;; "\underline" within "\section" and alike.
1935 (org-export-create-backend
1938 '((underline .
(lambda (o c i
) (format "\\underline{%s}" c
))))))
1940 (org-export-data-with-backend
1941 (org-element-property :title headline
) section-back-end info
))
1943 (and (plist-get info
:with-todo-keywords
)
1944 (let ((todo (org-element-property :todo-keyword headline
)))
1945 (and todo
(org-export-data todo info
)))))
1946 (todo-type (and todo
(org-element-property :todo-type headline
)))
1947 (tags (and (plist-get info
:with-tags
)
1948 (org-export-get-tags headline info
)))
1949 (priority (and (plist-get info
:with-priority
)
1950 (org-element-property :priority headline
)))
1951 ;; Create the headline text along with a no-tag version.
1952 ;; The latter is required to remove tags from toc.
1953 (full-text (funcall (plist-get info
:latex-format-headline-function
)
1954 todo todo-type priority text tags info
))
1955 ;; Associate \label to the headline for internal links.
1956 (headline-label (org-latex--label headline info t t
))
1958 (make-string (org-element-property :pre-blank headline
) ?
\n)))
1959 (if (or (not section-fmt
) (org-export-low-level-p headline info
))
1960 ;; This is a deep sub-tree: export it as a list item. Also
1961 ;; export as items headlines for which no section format has
1963 (let ((low-level-body
1965 ;; If headline is the first sibling, start a list.
1966 (when (org-export-first-sibling-p headline info
)
1967 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
1971 (string-match-p "\\`[ \t]*\\[" full-text
)
1977 ;; If headline is not the last sibling simply return
1978 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1980 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1981 (replace-regexp-in-string
1983 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
1985 ;; This is a standard headline. Export it as a section. Add
1986 ;; an alternative heading when possible, and when this is not
1987 ;; identical to the usual heading.
1989 (funcall (plist-get info
:latex-format-headline-function
)
1990 todo todo-type priority
1991 (org-export-data-with-backend
1992 (org-export-get-alt-title headline info
)
1993 section-back-end info
)
1994 (and (eq (plist-get info
:with-tags
) t
) tags
)
1996 ;; Maybe end local TOC (see `org-latex-keyword').
2000 (let ((case-fold-search t
)
2002 (let ((first (car (org-element-contents headline
))))
2003 (and (eq (org-element-type first
) 'section
) first
))))
2004 (org-element-map section
'keyword
2006 (and (equal (org-element-property :key k
) "TOC")
2007 (let ((v (org-element-property :value k
)))
2008 (and (string-match-p "\\<headlines\\>" v
)
2009 (string-match-p "\\<local\\>" v
)
2010 (format "\\stopcontents[level-%d]" level
)))))
2013 (not (equal opt-title full-text
))
2014 (string-match "\\`\\\\\\(.+?\\){" section-fmt
))
2015 (format (replace-match "\\1[%s]" nil nil section-fmt
1)
2016 ;; Replace square brackets with parenthesis
2017 ;; since square brackets are not supported in
2018 ;; optional arguments.
2019 (replace-regexp-in-string
2020 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title
))
2022 (concat headline-label pre-blanks contents
))
2023 ;; Impossible to add an alternative heading. Fallback to
2024 ;; regular sectioning format string.
2025 (format section-fmt full-text
2026 (concat headline-label pre-blanks contents
))))))))
2028 (defun org-latex-format-headline-default-function
2029 (todo _todo-type priority text tags _info
)
2030 "Default format function for a headline.
2031 See `org-latex-format-headline-function' for details."
2033 (and todo
(format "{\\bfseries\\sffamily %s} " todo
))
2034 (and priority
(format "\\framebox{\\#%c} " priority
))
2037 (format "\\hfill{}\\textsc{%s}"
2038 (mapconcat #'org-latex--protect-text tags
":")))))
2041 ;;;; Horizontal Rule
2043 (defun org-latex-horizontal-rule (horizontal-rule _contents info
)
2044 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
2045 CONTENTS is nil. INFO is a plist holding contextual information."
2046 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule
))
2047 (prev (org-export-get-previous-element horizontal-rule info
)))
2049 ;; Make sure the rule doesn't start at the end of the current
2050 ;; line by separating it with a blank line from previous element.
2052 (let ((prev-blank (org-element-property :post-blank prev
)))
2053 (or (not prev-blank
) (zerop prev-blank
))))
2055 (org-latex--wrap-label
2057 (format "\\rule{%s}{%s}"
2058 (or (plist-get attr
:width
) "\\linewidth")
2059 (or (plist-get attr
:thickness
) "0.5pt"))
2063 ;;;; Inline Src Block
2065 (defun org-latex-inline-src-block (inline-src-block _contents info
)
2066 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
2067 CONTENTS holds the contents of the item. INFO is a plist holding
2068 contextual information."
2069 (let* ((code (org-element-property :value inline-src-block
))
2070 (separator (org-latex--find-verb-separator code
)))
2071 (cl-case (plist-get info
:latex-listings
)
2072 ;; Do not use a special package: transcode it verbatim.
2073 ((nil) (format "\\texttt{%s}" (org-latex--text-markup code
'code info
)))
2074 ;; Use minted package.
2076 (let* ((org-lang (org-element-property :language inline-src-block
))
2077 (mint-lang (or (cadr (assq (intern org-lang
)
2078 (plist-get info
:latex-minted-langs
)))
2079 (downcase org-lang
)))
2080 (options (org-latex--make-option-string
2081 (plist-get info
:latex-minted-options
))))
2082 (format "\\mintinline%s{%s}{%s}"
2083 (if (string= options
"") "" (format "[%s]" options
))
2086 ;; Use listings package.
2088 ;; Maybe translate language's name.
2089 (let* ((org-lang (org-element-property :language inline-src-block
))
2090 (lst-lang (or (cadr (assq (intern org-lang
)
2091 (plist-get info
:latex-listings-langs
)))
2093 (options (org-latex--make-option-string
2094 (append (plist-get info
:latex-listings-options
)
2095 `(("language" ,lst-lang
))))))
2096 (concat (format "\\lstinline[%s]" options
)
2097 separator code separator
))))))
2102 (defun org-latex-inlinetask (inlinetask contents info
)
2103 "Transcode an INLINETASK element from Org to LaTeX.
2104 CONTENTS holds the contents of the block. INFO is a plist
2105 holding contextual information."
2106 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
2107 (todo (and (plist-get info
:with-todo-keywords
)
2108 (let ((todo (org-element-property :todo-keyword inlinetask
)))
2109 (and todo
(org-export-data todo info
)))))
2110 (todo-type (org-element-property :todo-type inlinetask
))
2111 (tags (and (plist-get info
:with-tags
)
2112 (org-export-get-tags inlinetask info
)))
2113 (priority (and (plist-get info
:with-priority
)
2114 (org-element-property :priority inlinetask
)))
2115 (contents (concat (org-latex--label inlinetask info
) contents
)))
2116 (funcall (plist-get info
:latex-format-inlinetask-function
)
2117 todo todo-type priority title tags contents info
)))
2119 (defun org-latex-format-inlinetask-default-function
2120 (todo _todo-type priority title tags contents _info
)
2121 "Default format function for a inlinetasks.
2122 See `org-latex-format-inlinetask-function' for details."
2124 (concat (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
2125 (when priority
(format "\\framebox{\\#%c} " priority
))
2128 (format "\\hfill{}\\textsc{:%s:}"
2129 (mapconcat #'org-latex--protect-text tags
":"))))))
2130 (concat "\\begin{center}\n"
2132 "\\begin{minipage}[c]{.6\\textwidth}\n"
2134 (and (org-string-nw-p contents
)
2135 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents
))
2143 (defun org-latex-italic (_italic contents info
)
2144 "Transcode ITALIC from Org to LaTeX.
2145 CONTENTS is the text with italic markup. INFO is a plist holding
2146 contextual information."
2147 (org-latex--text-markup contents
'italic info
))
2152 (defun org-latex-item (item contents info
)
2153 "Transcode an ITEM element from Org to LaTeX.
2154 CONTENTS holds the contents of the item. INFO is a plist holding
2155 contextual information."
2157 (let ((count (org-element-property :counter item
))
2159 ;; Determine level of current item to determine the
2160 ;; correct LaTeX counter to use (enumi, enumii...).
2161 (let ((parent item
) (level 0))
2162 (while (memq (org-element-type
2163 (setq parent
(org-export-get-parent parent
)))
2165 (when (and (eq (org-element-type parent
) 'plain-list
)
2166 (eq (org-element-property :type parent
)
2172 (format "\\setcounter{enum%s}{%s}\n"
2173 (nth (1- level
) '("i" "ii" "iii" "iv"))
2175 (checkbox (cl-case (org-element-property :checkbox item
)
2176 (on "$\\boxtimes$ ")
2178 (trans "$\\boxminus$ ")))
2179 (tag (let ((tag (org-element-property :tag item
)))
2180 ;; Check-boxes must belong to the tag.
2181 (and tag
(format "[{%s}] "
2183 (org-export-data tag info
)))))))
2188 (checkbox (concat " " checkbox
))
2189 ;; Without a tag or a check-box, if CONTENTS starts with
2190 ;; an opening square bracket, add "\relax" to "\item",
2191 ;; unless the brackets comes from an initial export
2192 ;; snippet (i.e. it is inserted willingly by the user).
2194 (string-match-p "\\`[ \t]*\\[" contents
)
2195 (not (let ((e (car (org-element-contents item
))))
2196 (and (eq (org-element-type e
) 'paragraph
)
2197 (let ((o (car (org-element-contents e
))))
2198 (and (eq (org-element-type o
) 'export-snippet
)
2199 (eq (org-export-snippet-backend o
)
2203 (and contents
(org-trim contents
))
2204 ;; If there are footnotes references in tag, be sure to
2205 ;; add their definition at the end of the item. This
2206 ;; workaround is necessary since "\footnote{}" command is
2207 ;; not supported in tags.
2209 (org-latex--delayed-footnotes-definitions
2210 (org-element-property :tag item
) info
)))))
2215 (defun org-latex-keyword (keyword _contents info
)
2216 "Transcode a KEYWORD element from Org to LaTeX.
2217 CONTENTS is nil. INFO is a plist holding contextual information."
2218 (let ((key (org-element-property :key keyword
))
2219 (value (org-element-property :value keyword
)))
2221 ((string= key
"LATEX") value
)
2222 ((string= key
"INDEX") (format "\\index{%s}" value
))
2223 ((string= key
"TOC")
2224 (let ((case-fold-search t
))
2226 ((string-match-p "\\<headlines\\>" value
)
2227 (let* ((localp (string-match-p "\\<local\\>" value
))
2228 (parent (org-element-lineage keyword
'(headline)))
2229 (level (if (not (and localp parent
)) 0
2230 (org-export-get-relative-level parent info
)))
2232 (and (string-match "\\<[0-9]+\\>" value
)
2234 "\\setcounter{tocdepth}{%d}"
2235 (+ (string-to-number (match-string 0 value
)) level
)))))
2236 (if (and localp parent
)
2237 ;; Start local TOC, assuming package "titletoc" is
2239 (format "\\startcontents[level-%d]
2240 \\printcontents[level-%d]{}{0}{%s}"
2241 level level
(or depth
""))
2242 (concat depth
(and depth
"\n") "\\tableofcontents"))))
2243 ((string-match-p "\\<tables\\>" value
) "\\listoftables")
2244 ((string-match-p "\\<listings\\>" value
)
2245 (cl-case (plist-get info
:latex-listings
)
2246 ((nil) "\\listoffigures")
2247 (minted "\\listoflistings")
2248 (otherwise "\\lstlistoflistings")))))))))
2251 ;;;; Latex Environment
2253 (defun org-latex-latex-environment (latex-environment _contents info
)
2254 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
2255 CONTENTS is nil. INFO is a plist holding contextual information."
2256 (when (plist-get info
:with-latex
)
2257 (let ((value (org-remove-indentation
2258 (org-element-property :value latex-environment
))))
2259 (if (not (org-element-property :name latex-environment
)) value
2260 ;; Environment is labeled: label must be within the environment
2261 ;; (otherwise, a reference pointing to that element will count
2262 ;; the section instead).
2265 (goto-char (point-min))
2267 (insert (org-latex--label latex-environment info nil t
))
2268 (buffer-string))))))
2273 (defun org-latex-latex-fragment (latex-fragment _contents _info
)
2274 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
2275 CONTENTS is nil. INFO is a plist holding contextual information."
2276 (let ((value (org-element-property :value latex-fragment
)))
2277 ;; Trim math markers since the fragment is enclosed within
2278 ;; a latex-math-block object anyway.
2279 (cond ((string-match-p "\\`\\$[^$]" value
) (substring value
1 -
1))
2280 ((string-prefix-p "\\(" value
) (substring value
2 -
2))
2286 (defun org-latex-line-break (_line-break _contents _info
)
2287 "Transcode a LINE-BREAK object from Org to LaTeX.
2288 CONTENTS is nil. INFO is a plist holding contextual information."
2294 (defun org-latex-image-link-filter (data _backend info
)
2295 (org-export-insert-image-links data info org-latex-inline-image-rules
))
2297 (defun org-latex--inline-image (link info
)
2298 "Return LaTeX code for an inline image.
2299 LINK is the link pointing to the inline image. INFO is a plist
2300 used as a communication channel."
2301 (let* ((parent (org-export-get-parent-element link
))
2302 (path (let ((raw-path (org-element-property :path link
)))
2303 (if (not (file-name-absolute-p raw-path
)) raw-path
2304 (expand-file-name raw-path
))))
2305 (filetype (file-name-extension path
))
2306 (caption (org-latex--caption/label-string parent info
))
2307 (caption-above-p (org-latex--caption-above-p link info
))
2308 ;; Retrieve latex attributes from the element around.
2309 (attr (org-export-read-attribute :attr_latex parent
))
2310 (float (let ((float (plist-get attr
:float
)))
2311 (cond ((string= float
"wrap") 'wrap
)
2312 ((string= float
"sideways") 'sideways
)
2313 ((string= float
"multicolumn") 'multicolumn
)
2314 ((and (plist-member attr
:float
) (not float
)) 'nonfloat
)
2316 (org-element-property :caption parent
)
2317 (org-string-nw-p (plist-get attr
:caption
)))
2321 (let ((place (plist-get attr
:placement
)))
2323 (place (format "%s" place
))
2324 ((eq float
'wrap
) "{l}{0.5\\textwidth}")
2326 (format "[%s]" (plist-get info
:latex-default-figure-position
)))
2329 (if (plist-member attr
:center
) (plist-get attr
:center
)
2330 (plist-get info
:latex-images-centered
)))
2331 (comment-include (if (plist-get attr
:comment-include
) "%" ""))
2332 ;; It is possible to specify width and height in the
2333 ;; ATTR_LATEX line, and also via default variables.
2334 (width (cond ((plist-get attr
:width
))
2335 ((plist-get attr
:height
) "")
2336 ((eq float
'wrap
) "0.48\\textwidth")
2337 (t (plist-get info
:latex-image-default-width
))))
2338 (height (cond ((plist-get attr
:height
))
2339 ((or (plist-get attr
:width
)
2340 (memq float
'(figure wrap
))) "")
2341 (t (plist-get info
:latex-image-default-height
))))
2342 (options (let ((opt (or (plist-get attr
:options
)
2343 (plist-get info
:latex-image-default-option
))))
2344 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt
)) opt
2345 (match-string 1 opt
))))
2347 (if (member filetype
'("tikz" "pgf"))
2349 ;; - use \input to read in image file.
2350 ;; - if options are present, wrap in a tikzpicture environment.
2351 ;; - if width or height are present, use \resizebox to change
2354 (setq image-code
(format "\\input{%s}" path
))
2355 (when (org-string-nw-p options
)
2357 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
2360 (when (or (org-string-nw-p width
) (org-string-nw-p height
))
2361 (setq image-code
(format "\\resizebox{%s}{%s}{%s}"
2362 (if (org-string-nw-p width
) width
"!")
2363 (if (org-string-nw-p height
) height
"!")
2365 ;; For other images:
2366 ;; - add width and height to options.
2367 ;; - include the image with \includegraphics.
2368 (when (org-string-nw-p width
)
2369 (setq options
(concat options
",width=" width
)))
2370 (when (org-string-nw-p height
)
2371 (setq options
(concat options
",height=" height
)))
2372 (let ((search-option (org-element-property :search-option link
)))
2373 (when (and search-option
2374 (equal filetype
"pdf")
2375 (string-match-p "\\`[0-9]+\\'" search-option
)
2376 (not (string-match-p "page=" options
)))
2377 (setq options
(concat options
",page=" search-option
))))
2379 (format "\\includegraphics%s{%s}"
2380 (cond ((not (org-string-nw-p options
)) "")
2381 ((string-prefix-p "," options
)
2382 (format "[%s]" (substring options
1)))
2383 (t (format "[%s]" options
)))
2385 (when (equal filetype
"svg")
2386 (setq image-code
(replace-regexp-in-string "^\\\\includegraphics"
2390 (setq image-code
(replace-regexp-in-string "\\.svg}"
2394 ;; Return proper string, depending on FLOAT.
2396 (`wrap
(format "\\begin{wrapfigure}%s
2399 %s\\end{wrapfigure}"
2401 (if caption-above-p caption
"")
2402 (if center
"\\centering" "")
2403 comment-include image-code
2404 (if caption-above-p
"" caption
)))
2405 (`sideways
(format "\\begin{sidewaysfigure}
2408 %s\\end{sidewaysfigure}"
2409 (if caption-above-p caption
"")
2410 (if center
"\\centering" "")
2411 comment-include image-code
2412 (if caption-above-p
"" caption
)))
2413 (`multicolumn
(format "\\begin{figure*}%s
2418 (if caption-above-p caption
"")
2419 (if center
"\\centering" "")
2420 comment-include image-code
2421 (if caption-above-p
"" caption
)))
2422 (`figure
(format "\\begin{figure}%s
2427 (if caption-above-p caption
"")
2428 (if center
"\\centering" "")
2429 comment-include image-code
2430 (if caption-above-p
"" caption
)))
2432 (format "\\begin{center}
2435 (if caption-above-p caption
"")
2437 (if caption-above-p
"" caption
)))
2439 (concat (if caption-above-p caption
"")
2441 (if caption-above-p caption
""))))))
2443 (defun org-latex-link (link desc info
)
2444 "Transcode a LINK object from Org to LaTeX.
2446 DESC is the description part of the link, or the empty string.
2447 INFO is a plist holding contextual information. See
2449 (let* ((type (org-element-property :type link
))
2450 (raw-path (org-element-property :path link
))
2451 ;; Ensure DESC really exists, or set it to nil.
2452 (desc (and (not (string= desc
"")) desc
))
2453 (imagep (org-export-inline-image-p
2454 link
(plist-get info
:latex-inline-image-rules
)))
2455 (path (org-latex--protect-text
2456 (cond ((member type
'("http" "https" "ftp" "mailto" "doi"))
2457 (concat type
":" raw-path
))
2458 ((string= type
"file") (org-export-file-uri raw-path
))
2461 ;; Link type is handled by a special function.
2462 ((org-export-custom-protocol-maybe link desc
'latex
))
2464 (imagep (org-latex--inline-image link info
))
2465 ;; Radio link: Transcode target's contents and use them as link's
2467 ((string= type
"radio")
2468 (let ((destination (org-export-resolve-radio-link link info
)))
2469 (if (not destination
) desc
2470 (format "\\hyperref[%s]{%s}"
2471 (org-export-get-reference destination info
)
2473 ;; Links pointing to a headline: Find destination and build
2474 ;; appropriate referencing command.
2475 ((member type
'("custom-id" "fuzzy" "id"))
2476 (let ((destination (if (string= type
"fuzzy")
2477 (org-export-resolve-fuzzy-link link info
)
2478 (org-export-resolve-id-link link info
))))
2479 (cl-case (org-element-type destination
)
2480 ;; Id link points to an external file.
2482 (if desc
(format "\\href{%s}{%s}" destination desc
)
2483 (format "\\url{%s}" destination
)))
2484 ;; Fuzzy link points nowhere.
2486 (format (plist-get info
:latex-link-with-unknown-path-format
)
2489 (org-element-property :raw-link link
) info
))))
2490 ;; LINK points to a headline. If headlines are numbered
2491 ;; and the link has no description, display headline's
2492 ;; number. Otherwise, display description or headline's
2495 (let ((label (org-latex--label destination info t
)))
2497 (org-export-numbered-headline-p destination info
))
2498 (format "\\ref{%s}" label
)
2499 (format "\\hyperref[%s]{%s}" label
2502 (org-element-property :title destination
) info
))))))
2503 ;; Fuzzy link points to a target. Do as above.
2505 (let ((ref (org-latex--label destination info t
)))
2506 (if (not desc
) (format "\\ref{%s}" ref
)
2507 (format "\\hyperref[%s]{%s}" ref desc
)))))))
2508 ;; Coderef: replace link with the reference name or the
2509 ;; equivalent line number.
2510 ((string= type
"coderef")
2511 (format (org-export-get-coderef-format path desc
)
2512 (org-export-resolve-coderef path info
)))
2513 ;; External link with a description part.
2514 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
2515 ;; External link without a description part.
2516 (path (format "\\url{%s}" path
))
2517 ;; No path, only description. Try to do something useful.
2518 (t (format (plist-get info
:latex-link-with-unknown-path-format
) desc
)))))
2523 (defun org-latex-node-property (node-property _contents _info
)
2524 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2525 CONTENTS is nil. INFO is a plist holding contextual
2528 (org-element-property :key node-property
)
2529 (let ((value (org-element-property :value node-property
)))
2530 (if value
(concat " " value
) ""))))
2535 (defun org-latex-paragraph (_paragraph contents _info
)
2536 "Transcode a PARAGRAPH element from Org to LaTeX.
2537 CONTENTS is the contents of the paragraph, as a string. INFO is
2538 the plist used as a communication channel."
2544 (defun org-latex-plain-list (plain-list contents info
)
2545 "Transcode a PLAIN-LIST element from Org to LaTeX.
2546 CONTENTS is the contents of the list. INFO is a plist holding
2547 contextual information."
2548 (let* ((type (org-element-property :type plain-list
))
2549 (attr (org-export-read-attribute :attr_latex plain-list
))
2550 (latex-type (let ((env (plist-get attr
:environment
)))
2551 (cond (env (format "%s" env
))
2552 ((eq type
'ordered
) "enumerate")
2553 ((eq type
'descriptive
) "description")
2555 (org-latex--wrap-label
2557 (format "\\begin{%s}%s\n%s\\end{%s}"
2559 (or (plist-get attr
:options
) "")
2567 (defun org-latex-plain-text (text info
)
2568 "Transcode a TEXT string from Org to LaTeX.
2569 TEXT is the string to transcode. INFO is a plist holding
2570 contextual information."
2571 (let* ((specialp (plist-get info
:with-special-strings
))
2573 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2574 (let ((case-fold-search nil
))
2575 (replace-regexp-in-string
2576 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2577 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2578 ;; However, if special strings are used, be careful not
2579 ;; to protect "\" in "\-" constructs.
2580 (replace-regexp-in-string
2581 (concat "[%$#&{}_~^]\\|\\\\" (and specialp
"\\([^-]\\|$\\)"))
2583 (cl-case (string-to-char m
)
2584 (?
\\ "$\\\\backslash$\\1")
2585 (?~
"\\\\textasciitilde{}")
2589 ;; Activate smart quotes. Be sure to provide original TEXT string
2590 ;; since OUTPUT may have been modified.
2591 (when (plist-get info
:with-smart-quotes
)
2592 (setq output
(org-export-activate-smart-quotes output
:latex info text
)))
2593 ;; Convert special strings.
2595 (setq output
(replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output
)))
2596 ;; Handle break preservation if required.
2597 (when (plist-get info
:preserve-breaks
)
2598 (setq output
(replace-regexp-in-string
2599 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t
)))
2606 (defun org-latex-planning (planning _contents info
)
2607 "Transcode a PLANNING element from Org to LaTeX.
2608 CONTENTS is nil. INFO is a plist holding contextual
2616 (let ((closed (org-element-property :closed planning
)))
2619 (format "\\textbf{%s} " org-closed-string
)
2620 (format (plist-get info
:latex-inactive-timestamp-format
)
2621 (org-timestamp-translate closed
)))))
2622 (let ((deadline (org-element-property :deadline planning
)))
2625 (format "\\textbf{%s} " org-deadline-string
)
2626 (format (plist-get info
:latex-active-timestamp-format
)
2627 (org-timestamp-translate deadline
)))))
2628 (let ((scheduled (org-element-property :scheduled planning
)))
2631 (format "\\textbf{%s} " org-scheduled-string
)
2632 (format (plist-get info
:latex-active-timestamp-format
)
2633 (org-timestamp-translate scheduled
)))))))
2638 ;;;; Property Drawer
2640 (defun org-latex-property-drawer (_property-drawer contents _info
)
2641 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2642 CONTENTS holds the contents of the drawer. INFO is a plist
2643 holding contextual information."
2644 (and (org-string-nw-p contents
)
2645 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents
)))
2648 ;;;; Pseudo Element: LaTeX Matrices
2650 ;; `latex-matrices' elements have the following properties:
2651 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2654 (defun org-latex--wrap-latex-matrices (data info
)
2655 "Merge contiguous tables with the same mode within a pseudo-element.
2656 DATA is a parse tree or a secondary string. INFO is a plist
2657 containing export options. Modify DATA by side-effect and return
2659 (org-element-map data
'table
2661 (when (eq (org-element-property :type table
) 'org
)
2662 (let ((mode (or (org-export-read-attribute :attr_latex table
:mode
)
2663 (plist-get info
:latex-default-table-mode
))))
2664 (when (and (member mode
'("inline-math" "math"))
2665 ;; Do not wrap twice the same table.
2666 (not (eq (org-element-type
2667 (org-element-property :parent table
))
2669 (let* ((caption (and (not (string= mode
"inline-math"))
2670 (org-element-property :caption table
)))
2672 (list 'latex-matrices
2673 (list :caption caption
2675 (cond ((string= mode
"inline-math") 'inline
)
2679 (next (org-export-get-next-element table info
)))
2680 (org-element-insert-before matrices table
)
2681 ;; Swallow all contiguous tables sharing the same mode.
2683 (zerop (or (org-element-property :post-blank previous
) 0))
2684 (setq next
(org-export-get-next-element previous info
))
2685 (eq (org-element-type next
) 'table
)
2686 (eq (org-element-property :type next
) 'org
)
2687 (string= (or (org-export-read-attribute
2688 :attr_latex next
:mode
)
2689 (plist-get info
:latex-default-table-mode
))
2691 (org-element-extract-element previous
)
2692 (org-element-adopt-elements matrices previous
)
2693 (setq previous next
))
2694 ;; Inherit `:post-blank' from the value of the last
2695 ;; swallowed table. Set the latter's `:post-blank'
2696 ;; value to 0 so as to not duplicate empty lines.
2697 (org-element-put-property
2698 matrices
:post-blank
(org-element-property :post-blank previous
))
2699 (org-element-put-property previous
:post-blank
0)
2700 (org-element-extract-element previous
)
2701 (org-element-adopt-elements matrices previous
))))))
2705 (defun org-latex-matrices (matrices contents _info
)
2706 "Transcode a MATRICES element from Org to LaTeX.
2707 CONTENTS is a string. INFO is a plist used as a communication
2709 (format (cl-case (org-element-property :markup matrices
)
2711 (equation "\\begin{equation}\n%s\\end{equation}")
2716 ;;;; Pseudo Object: LaTeX Math Block
2718 ;; `latex-math-block' objects have the following property:
2721 (defun org-latex--wrap-latex-math-block (data info
)
2722 "Merge contiguous math objects in a pseudo-object container.
2723 DATA is a parse tree or a secondary string. INFO is a plist
2724 containing export options. Modify DATA by side-effect and return it."
2725 (let ((valid-object-p
2726 ;; Non-nil when OBJ can be added to the latex math block B.
2728 (pcase (org-element-type obj
)
2729 (`entity
(org-element-property :latex-math-p obj
))
2731 (let ((value (org-element-property :value obj
)))
2732 (or (string-prefix-p "\\(" value
)
2733 (string-match-p "\\`\\$[^$]" value
))))
2734 ((and type
(or `subscript
`superscript
))
2735 (not (memq type
(mapcar #'org-element-type
2736 (org-element-contents b
)))))))))
2737 (org-element-map data
'(entity latex-fragment subscript superscript
)
2739 ;; Skip objects already wrapped.
2740 (when (and (not (eq (org-element-type
2741 (org-element-property :parent object
))
2743 (funcall valid-object-p object nil
))
2744 (let ((math-block (list 'latex-math-block nil
))
2745 (next-elements (org-export-get-next-element object info t
))
2747 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2748 (org-element-insert-before math-block object
)
2749 (org-element-extract-element object
)
2750 (org-element-adopt-elements math-block object
)
2751 (when (zerop (or (org-element-property :post-blank object
) 0))
2752 ;; MATH-BLOCK swallows consecutive math objects.
2754 (dolist (next next-elements
)
2755 (unless (funcall valid-object-p next math-block
)
2757 (org-element-extract-element next
)
2758 (org-element-adopt-elements math-block next
)
2759 ;; Eschew the case: \beta$x$ -> \(\betax\).
2760 (unless (memq (org-element-type next
)
2761 '(subscript superscript
))
2762 (org-element-put-property last
:post-blank
1))
2764 (when (> (or (org-element-property :post-blank next
) 0) 0)
2765 (throw 'exit nil
)))))
2766 (org-element-put-property
2767 math-block
:post-blank
(org-element-property :post-blank last
)))))
2768 info nil
'(subscript superscript latex-math-block
) t
)
2769 ;; Return updated DATA.
2772 (defun org-latex-math-block (_math-block contents _info
)
2773 "Transcode a MATH-BLOCK object from Org to LaTeX.
2774 CONTENTS is a string. INFO is a plist used as a communication
2776 (when (org-string-nw-p contents
)
2777 (format "\\(%s\\)" (org-trim contents
))))
2781 (defun org-latex-quote-block (quote-block contents info
)
2782 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2783 CONTENTS holds the contents of the block. INFO is a plist
2784 holding contextual information."
2785 (org-latex--wrap-label
2786 quote-block
(format "\\begin{quote}\n%s\\end{quote}" contents
) info
))
2791 (defun org-latex-radio-target (radio-target text info
)
2792 "Transcode a RADIO-TARGET object from Org to LaTeX.
2793 TEXT is the text of the target. INFO is a plist holding
2794 contextual information."
2795 (format "\\label{%s}%s" (org-export-get-reference radio-target info
) text
))
2800 (defun org-latex-section (_section contents _info
)
2801 "Transcode a SECTION element from Org to LaTeX.
2802 CONTENTS holds the contents of the section. INFO is a plist
2803 holding contextual information."
2809 (defun org-latex-special-block (special-block contents info
)
2810 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2811 CONTENTS holds the contents of the block. INFO is a plist
2812 holding contextual information."
2813 (let ((type (org-element-property :type special-block
))
2814 (opt (org-export-read-attribute :attr_latex special-block
:options
))
2815 (caption (org-latex--caption/label-string special-block info
))
2816 (caption-above-p (org-latex--caption-above-p special-block info
)))
2817 (concat (format "\\begin{%s}%s\n" type
(or opt
""))
2818 (and caption-above-p caption
)
2820 (and (not caption-above-p
) caption
)
2821 (format "\\end{%s}" type
))))
2826 (defun org-latex-src-block (src-block _contents info
)
2827 "Transcode a SRC-BLOCK element from Org to LaTeX.
2828 CONTENTS holds the contents of the item. INFO is a plist holding
2829 contextual information."
2830 (when (org-string-nw-p (org-element-property :value src-block
))
2831 (let* ((lang (org-element-property :language src-block
))
2832 (caption (org-element-property :caption src-block
))
2833 (caption-above-p (org-latex--caption-above-p src-block info
))
2834 (label (org-element-property :name src-block
))
2835 (custom-env (and lang
2836 (cadr (assq (intern lang
)
2837 org-latex-custom-lang-environments
))))
2838 (num-start (org-export-get-loc src-block info
))
2839 (retain-labels (org-element-property :retain-labels src-block
))
2840 (attributes (org-export-read-attribute :attr_latex src-block
))
2841 (float (plist-get attributes
:float
))
2842 (listings (plist-get info
:latex-listings
)))
2844 ;; Case 1. No source fontification.
2846 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2848 (cond ((string= "multicolumn" float
)
2849 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2850 (plist-get info
:latex-default-figure-position
)
2851 (if caption-above-p caption-str
"")
2852 (if caption-above-p
"" caption-str
)))
2854 (if caption-above-p caption-str
"")
2856 (if caption-above-p
"" (concat "\n" caption-str
))))
2860 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2861 (org-export-format-code-default src-block info
))))))
2862 ;; Case 2. Custom environment.
2864 (let ((caption-str (org-latex--caption/label-string src-block info
))
2865 (formatted-src (org-export-format-code-default src-block info
)))
2866 (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env
)
2867 (format "\\begin{%s}\n%s\\end{%s}\n"
2869 (concat (and caption-above-p caption-str
)
2871 (and (not caption-above-p
) caption-str
))
2873 (format-spec custom-env
2874 `((?s .
,formatted-src
)
2877 (?l .
,(org-latex--label src-block info
))
2878 (?o .
,(or (plist-get attributes
:options
) "")))))))
2879 ;; Case 3. Use minted package.
2880 ((eq listings
'minted
)
2881 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2884 ((string= "multicolumn" float
)
2885 (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
2886 (plist-get info
:latex-default-figure-position
)
2887 (if caption-above-p caption-str
"")
2888 (if caption-above-p
"" caption-str
)))
2890 (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
2891 (plist-get info
:latex-default-figure-position
)
2892 (if caption-above-p caption-str
"")
2893 (if caption-above-p
"" caption-str
)))
2894 ((string= "t" float
)
2895 (concat (format "\\begin{listing}[%s]\n"
2896 (plist-get info
:latex-default-figure-position
))
2897 "%s\n\\end{listing}"))
2899 (options (plist-get info
:latex-minted-options
))
2902 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2905 (org-latex--make-option-string
2906 (if (or (not num-start
) (assoc "linenos" options
))
2910 ("firstnumber" ,(number-to-string (1+ num-start
))))
2912 (let ((local-options (plist-get attributes
:options
)))
2913 (and local-options
(concat "," local-options
))))
2915 (or (cadr (assq (intern lang
)
2916 (plist-get info
:latex-minted-langs
)))
2919 (let* ((code-info (org-export-unravel-code src-block
))
2923 (org-split-string (car code-info
)
2925 (org-export-format-code
2927 (lambda (loc _num ref
)
2931 ;; Ensure references are flushed to the right,
2932 ;; separated with 6 spaces from the widest line
2934 (concat (make-string (+ (- max-width
(length loc
)) 6)
2936 (format "(%s)" ref
)))))
2937 nil
(and retain-labels
(cdr code-info
)))))))
2939 (format float-env body
)))
2940 ;; Case 4. Use listings package.
2943 (or (cadr (assq (intern lang
)
2944 (plist-get info
:latex-listings-langs
)))
2948 (let ((main (org-export-get-caption src-block
))
2949 (secondary (org-export-get-caption src-block t
)))
2951 (format "{%s}" (org-export-data main info
))
2953 (org-export-data secondary info
)
2954 (org-export-data main info
))))))
2955 (lst-opt (plist-get info
:latex-listings-options
)))
2961 (org-latex--make-option-string
2965 ((and (not float
) (plist-member attributes
:float
)) nil
)
2966 ((string= "multicolumn" float
) '(("float" "*")))
2967 ((and float
(not (assoc "float" lst-opt
)))
2968 `(("float" ,(plist-get info
:latex-default-figure-position
)))))
2969 `(("language" ,lst-lang
))
2971 `(("label" ,(org-latex--label src-block info
)))
2973 (if caption-str
`(("caption" ,caption-str
)) '(("caption" " ")))
2974 `(("captionpos" ,(if caption-above-p
"t" "b")))
2975 (cond ((assoc "numbers" lst-opt
) nil
)
2976 ((not num-start
) '(("numbers" "none")))
2977 (t `(("firstnumber" ,(number-to-string (1+ num-start
)))
2978 ("numbers" "left"))))))
2979 (let ((local-options (plist-get attributes
:options
)))
2980 (and local-options
(concat "," local-options
)))))
2983 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2984 (let* ((code-info (org-export-unravel-code src-block
))
2988 (org-split-string (car code-info
) "\n")))))
2989 (org-export-format-code
2991 (lambda (loc _num ref
)
2995 ;; Ensure references are flushed to the right,
2996 ;; separated with 6 spaces from the widest line of
2998 (concat (make-string (+ (- max-width
(length loc
)) 6) ?\s
)
2999 (format "(%s)" ref
)))))
3000 nil
(and retain-labels
(cdr code-info
))))))))))))
3003 ;;;; Statistics Cookie
3005 (defun org-latex-statistics-cookie (statistics-cookie _contents _info
)
3006 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
3007 CONTENTS is nil. INFO is a plist holding contextual information."
3008 (replace-regexp-in-string
3009 "%" "\\%" (org-element-property :value statistics-cookie
) nil t
))
3014 (defun org-latex-strike-through (_strike-through contents info
)
3015 "Transcode STRIKE-THROUGH from Org to LaTeX.
3016 CONTENTS is the text with strike-through markup. INFO is a plist
3017 holding contextual information."
3018 (org-latex--text-markup contents
'strike-through info
))
3023 (defun org-latex--script-size (object info
)
3024 "Transcode a subscript or superscript object.
3025 OBJECT is an Org object. INFO is a plist used as a communication
3028 (org-element-map (org-element-contents object
)
3029 (cons 'plain-text org-element-all-objects
)
3031 (cl-case (org-element-type obj
)
3032 ((entity latex-fragment
)
3033 (let ((data (org-trim (org-export-data obj info
))))
3035 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
3039 (match-string 1 data
)
3040 (let ((blank (org-element-property :post-blank obj
)))
3041 (and blank
(> blank
0) "\\ "))))))
3044 (format "%s\\text{%s}" output
(org-export-data obj info
))))
3048 (org-export-data obj info
)
3049 (let ((blank (org-element-property :post-blank obj
)))
3050 (and blank
(> blank
0) "\\ ")))))))
3051 info nil org-element-recursive-objects
)
3052 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
3054 (concat (if (eq (org-element-type object
) 'subscript
) "_" "^")
3055 (and (> (length output
) 1) "{")
3057 (and (> (length output
) 1) "}"))))
3059 (defun org-latex-subscript (subscript _contents info
)
3060 "Transcode a SUBSCRIPT object from Org to LaTeX.
3061 CONTENTS is the contents of the object. INFO is a plist holding
3062 contextual information."
3063 (org-latex--script-size subscript info
))
3068 (defun org-latex-superscript (superscript _contents info
)
3069 "Transcode a SUPERSCRIPT object from Org to LaTeX.
3070 CONTENTS is the contents of the object. INFO is a plist holding
3071 contextual information."
3072 (org-latex--script-size superscript info
))
3077 ;; `org-latex-table' is the entry point for table transcoding. It
3078 ;; takes care of tables with a "verbatim" mode. Otherwise, it
3079 ;; delegates the job to either `org-latex--table.el-table',
3080 ;; `org-latex--org-table' or `org-latex--math-table' functions,
3081 ;; depending of the type of the table and the mode requested.
3083 ;; `org-latex--align-string' is a subroutine used to build alignment
3084 ;; string for Org tables.
3086 (defun org-latex-table (table contents info
)
3087 "Transcode a TABLE element from Org to LaTeX.
3088 CONTENTS is the contents of the table. INFO is a plist holding
3089 contextual information."
3090 (if (eq (org-element-property :type table
) 'table.el
)
3091 ;; "table.el" table. Convert it using appropriate tools.
3092 (org-latex--table.el-table table info
)
3093 (let ((type (or (org-export-read-attribute :attr_latex table
:mode
)
3094 (plist-get info
:latex-default-table-mode
))))
3096 ;; Case 1: Verbatim table.
3097 ((string= type
"verbatim")
3098 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
3099 ;; Re-create table, without affiliated keywords.
3100 (org-trim (org-element-interpret-data
3101 `(table nil
,@(org-element-contents table
))))))
3103 ((or (string= type
"math") (string= type
"inline-math"))
3104 (org-latex--math-table table info
))
3105 ;; Case 3: Standard table.
3106 (t (concat (org-latex--org-table table contents info
)
3107 ;; When there are footnote references within the
3108 ;; table, insert their definition just after it.
3109 (org-latex--delayed-footnotes-definitions table info
)))))))
3111 (defun org-latex--align-string (table info
)
3112 "Return an appropriate LaTeX alignment string.
3113 TABLE is the considered table. INFO is a plist used as
3114 a communication channel."
3115 (or (org-export-read-attribute :attr_latex table
:align
)
3117 ;; Extract column groups and alignment from first (non-rule)
3120 (org-element-map table
'table-row
3122 (and (eq (org-element-property :type row
) 'standard
) row
))
3126 (let ((borders (org-export-table-cell-borders cell info
)))
3127 ;; Check left border for the first cell only.
3128 (when (and (memq 'left borders
) (not align
))
3130 (push (cl-case (org-export-table-cell-alignment cell info
)
3135 (when (memq 'right borders
) (push "|" align
))))
3137 (apply 'concat
(nreverse align
)))))
3139 (defun org-latex--org-table (table contents info
)
3140 "Return appropriate LaTeX code for an Org table.
3142 TABLE is the table type element to transcode. CONTENTS is its
3143 contents, as a string. INFO is a plist used as a communication
3146 This function assumes TABLE has `org' as its `:type' property and
3147 `table' as its `:mode' attribute."
3148 (let* ((caption (org-latex--caption/label-string table info
))
3149 (attr (org-export-read-attribute :attr_latex table
))
3150 ;; Determine alignment string.
3151 (alignment (org-latex--align-string table info
))
3152 ;; Determine environment for the table: longtable, tabular...
3153 (table-env (or (plist-get attr
:environment
)
3154 (plist-get info
:latex-default-table-environment
)))
3155 ;; If table is a float, determine environment: table, table*
3156 ;; or sidewaystable.
3157 (float-env (unless (member table-env
'("longtable" "longtabu"))
3158 (let ((float (plist-get attr
:float
)))
3160 ((and (not float
) (plist-member attr
:float
)) nil
)
3161 ((or (string= float
"sidewaystable")
3162 (string= float
"sideways")) "sidewaystable")
3163 ((string= float
"multicolumn") "table*")
3165 (org-element-property :caption table
)
3166 (org-string-nw-p (plist-get attr
:caption
)))
3168 ;; Extract others display options.
3169 (fontsize (let ((font (plist-get attr
:font
)))
3170 (and font
(concat font
"\n"))))
3171 ;; "tabular" environment doesn't allow to define a width.
3172 (width (and (not (equal table-env
"tabular")) (plist-get attr
:width
)))
3173 (spreadp (plist-get attr
:spread
))
3175 (or (plist-get attr
:placement
)
3176 (format "[%s]" (plist-get info
:latex-default-figure-position
))))
3177 (centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
3178 (plist-get info
:latex-tables-centered
)))
3179 (caption-above-p (org-latex--caption-above-p table info
)))
3180 ;; Prepare the final format string for the table.
3183 ((equal "longtable" table-env
)
3184 (concat (and fontsize
(concat "{" fontsize
))
3185 (format "\\begin{longtable}{%s}\n" alignment
)
3186 (and caption-above-p
3187 (org-string-nw-p caption
)
3188 (concat caption
"\\\\\n"))
3190 (and (not caption-above-p
)
3191 (org-string-nw-p caption
)
3192 (concat caption
"\\\\\n"))
3193 "\\end{longtable}\n"
3194 (and fontsize
"}")))
3196 ((equal "longtabu" table-env
)
3197 (concat (and fontsize
(concat "{" fontsize
))
3198 (format "\\begin{longtabu}%s{%s}\n"
3201 (if spreadp
"spread" "to") width
) "")
3203 (and caption-above-p
3204 (org-string-nw-p caption
)
3205 (concat caption
"\\\\\n"))
3207 (and (not caption-above-p
)
3208 (org-string-nw-p caption
)
3209 (concat caption
"\\\\\n"))
3211 (and fontsize
"}")))
3215 (concat (format "\\begin{%s}%s\n" float-env placement
)
3216 (if caption-above-p caption
"")
3217 (when centerp
"\\centering\n")
3219 ((and (not float-env
) caption
)
3221 (and centerp
"\\begin{center}\n" )
3222 (if caption-above-p caption
"")
3223 (cond ((and fontsize centerp
) fontsize
)
3224 (fontsize (concat "{" fontsize
)))))
3225 (centerp (concat "\\begin{center}\n" fontsize
))
3226 (fontsize (concat "{" fontsize
)))
3227 (cond ((equal "tabu" table-env
)
3228 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
3230 (if spreadp
" spread %s " " to %s ")
3234 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
3236 (if width
(format "{%s}" width
) "")
3242 (concat (if caption-above-p
"" (concat "\n" caption
))
3243 (format "\n\\end{%s}" float-env
)))
3244 ((and (not float-env
) caption
)
3246 (if caption-above-p
"" (concat "\n" caption
))
3247 (and centerp
"\n\\end{center}")
3248 (and fontsize
(not centerp
) "}")))
3249 (centerp "\n\\end{center}")
3250 (fontsize "}")))))))
3252 (defun org-latex--table.el-table
(table info
)
3253 "Return appropriate LaTeX code for a table.el table.
3255 TABLE is the table type element to transcode. INFO is a plist
3256 used as a communication channel.
3258 This function assumes TABLE has `table.el' as its `:type'
3261 ;; Ensure "*org-export-table*" buffer is empty.
3262 (with-current-buffer (get-buffer-create "*org-export-table*")
3264 (let ((output (with-temp-buffer
3265 (insert (org-element-property :value table
))
3267 (re-search-forward "^[ \t]*|[^|]" nil t
)
3268 (table-generate-source 'latex
"*org-export-table*")
3269 (with-current-buffer "*org-export-table*"
3270 (org-trim (buffer-string))))))
3271 (kill-buffer (get-buffer "*org-export-table*"))
3272 ;; Remove left out comments.
3273 (while (string-match "^%.*\n" output
)
3274 (setq output
(replace-match "" t t output
)))
3275 (let ((attr (org-export-read-attribute :attr_latex table
)))
3276 (when (plist-get attr
:rmlines
)
3277 ;; When the "rmlines" attribute is provided, remove all hlines
3278 ;; but the the one separating heading from the table body.
3279 (let ((n 0) (pos 0))
3280 (while (and (< (length output
) pos
)
3281 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
3283 (unless (= n
2) (setq output
(replace-match "" nil nil output
))))))
3284 (let ((centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
3285 (plist-get info
:latex-tables-centered
))))
3286 (if (not centerp
) output
3287 (format "\\begin{center}\n%s\n\\end{center}" output
))))))
3289 (defun org-latex--math-table (table info
)
3290 "Return appropriate LaTeX code for a matrix.
3292 TABLE is the table type element to transcode. INFO is a plist
3293 used as a communication channel.
3295 This function assumes TABLE has `org' as its `:type' property and
3296 `inline-math' or `math' as its `:mode' attribute."
3297 (let* ((attr (org-export-read-attribute :attr_latex table
))
3298 (env (or (plist-get attr
:environment
)
3299 (plist-get info
:latex-default-table-environment
)))
3303 (if (eq (org-element-property :type row
) 'rule
) "\\hline"
3304 ;; Return each cell unmodified.
3308 (substring (org-element-interpret-data cell
) 0 -
1))
3309 (org-element-map row
'table-cell
#'identity info
) "&")
3310 (or (cdr (assoc env org-latex-table-matrix-macros
)) "\\\\")
3312 (org-element-map table
'table-row
#'identity info
) "")))
3315 (plist-get attr
:math-prefix
)
3316 ;; Environment. Also treat special cases.
3317 (cond ((member env
'("array" "tabular"))
3318 ;; Make sure cells are always centered while preserving
3319 ;; vertical separators.
3320 (let ((align (replace-regexp-in-string
3321 "[lr]" "c" (org-latex--align-string table info
))))
3322 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env
)))
3323 ((assoc env org-latex-table-matrix-macros
)
3324 (format "\\%s%s{\n%s}"
3326 (or (plist-get attr
:math-arguments
) "")
3328 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env
)))
3330 (plist-get attr
:math-suffix
))))
3335 (defun org-latex-table-cell (table-cell contents info
)
3336 "Transcode a TABLE-CELL element from Org to LaTeX.
3337 CONTENTS is the cell contents. INFO is a plist used as
3338 a communication channel."
3340 (let ((scientific-format (plist-get info
:latex-table-scientific-notation
)))
3343 (string-match orgtbl-exp-regexp contents
))
3344 ;; Use appropriate format string for scientific
3346 (format scientific-format
3347 (match-string 1 contents
)
3348 (match-string 2 contents
))
3350 (when (org-export-get-next-element table-cell info
) " & ")))
3355 (defun org-latex-table-row (table-row contents info
)
3356 "Transcode a TABLE-ROW element from Org to LaTeX.
3357 CONTENTS is the contents of the row. INFO is a plist used as
3358 a communication channel."
3359 (let* ((attr (org-export-read-attribute :attr_latex
3360 (org-export-get-parent table-row
)))
3361 (booktabsp (if (plist-member attr
:booktabs
) (plist-get attr
:booktabs
)
3362 (plist-get info
:latex-tables-booktabs
)))
3364 (member (or (plist-get attr
:environment
)
3365 (plist-get info
:latex-default-table-environment
))
3366 '("longtable" "longtabu"))))
3367 (if (eq (org-element-property :type table-row
) 'rule
)
3369 ((not booktabsp
) "\\hline")
3370 ((not (org-export-get-previous-element table-row info
)) "\\toprule")
3371 ((not (org-export-get-next-element table-row info
)) "\\bottomrule")
3373 (org-export-table-row-ends-header-p
3374 (org-export-get-previous-element table-row info
) info
))
3378 ;; When BOOKTABS are activated enforce top-rule even when no
3379 ;; hline was specifically marked.
3380 (and booktabsp
(not (org-export-get-previous-element table-row info
))
3384 ;; Special case for long tables. Define header and footers.
3385 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
3386 (let ((columns (cdr (org-export-table-dimensions
3387 (org-export-get-parent-table table-row
) info
))))
3390 \\multicolumn{%d}{l}{%s} \\\\
3395 %s\\multicolumn{%d}{r}{%s} \\\\
3398 (if booktabsp
"\\midrule" "\\hline")
3400 (org-latex--translate "Continued from previous page" info
)
3402 ((not (org-export-table-row-starts-header-p table-row info
))
3404 (booktabsp "\\toprule\n")
3407 (if booktabsp
"\\midrule" "\\hline")
3408 (if booktabsp
"\\midrule" "\\hline")
3410 (org-latex--translate "Continued on next page" info
))))
3411 ;; When BOOKTABS are activated enforce bottom rule even when
3412 ;; no hline was specifically marked.
3413 ((and booktabsp
(not (org-export-get-next-element table-row info
)))
3414 "\\bottomrule"))))))
3419 (defun org-latex-target (target _contents info
)
3420 "Transcode a TARGET object from Org to LaTeX.
3421 CONTENTS is nil. INFO is a plist holding contextual
3423 (format "\\label{%s}" (org-latex--label target info
)))
3428 (defun org-latex-timestamp (timestamp _contents info
)
3429 "Transcode a TIMESTAMP object from Org to LaTeX.
3430 CONTENTS is nil. INFO is a plist holding contextual
3432 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp
) info
)))
3435 (cl-case (org-element-property :type timestamp
)
3436 ((active active-range
) :latex-active-timestamp-format
)
3437 ((inactive inactive-range
) :latex-inactive-timestamp-format
)
3438 (otherwise :latex-diary-timestamp-format
)))
3444 (defun org-latex-underline (_underline contents info
)
3445 "Transcode UNDERLINE from Org to LaTeX.
3446 CONTENTS is the text with underline markup. INFO is a plist
3447 holding contextual information."
3448 (org-latex--text-markup contents
'underline info
))
3453 (defun org-latex-verbatim (verbatim _contents info
)
3454 "Transcode a VERBATIM object from Org to LaTeX.
3455 CONTENTS is nil. INFO is a plist used as a communication
3457 (org-latex--text-markup
3458 (org-element-property :value verbatim
) 'verbatim info
))
3463 (defun org-latex-verse-block (verse-block contents info
)
3464 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3465 CONTENTS is verse block contents. INFO is a plist holding
3466 contextual information."
3467 (org-latex--wrap-label
3469 ;; In a verse environment, add a line break to each newline
3470 ;; character and change each white space at beginning of a line
3471 ;; into a space of 1 em. Also change each blank line with
3472 ;; a vertical space of 1 em.
3473 (format "\\begin{verse}\n%s\\end{verse}"
3474 (replace-regexp-in-string
3475 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m
)))
3476 (replace-regexp-in-string
3477 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3478 (replace-regexp-in-string
3479 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3480 contents nil t
) nil t
) nil t
))
3485 ;;; End-user functions
3488 (defun org-latex-export-as-latex
3489 (&optional async subtreep visible-only body-only ext-plist
)
3490 "Export current buffer as a LaTeX buffer.
3492 If narrowing is active in the current buffer, only export its
3495 If a region is active, export that region.
3497 A non-nil optional argument ASYNC means the process should happen
3498 asynchronously. The resulting buffer should be accessible
3499 through the `org-export-stack' interface.
3501 When optional argument SUBTREEP is non-nil, export the sub-tree
3502 at point, extracting information from the headline properties
3505 When optional argument VISIBLE-ONLY is non-nil, don't export
3506 contents of hidden elements.
3508 When optional argument BODY-ONLY is non-nil, only write code
3509 between \"\\begin{document}\" and \"\\end{document}\".
3511 EXT-PLIST, when provided, is a property list with external
3512 parameters overriding Org default settings, but still inferior to
3513 file-local settings.
3515 Export is done in a buffer named \"*Org LATEX Export*\", which
3516 will be displayed when `org-export-show-temporary-export-buffer'
3519 (org-export-to-buffer 'latex
"*Org LATEX Export*"
3520 async subtreep visible-only body-only ext-plist
(lambda () (LaTeX-mode))))
3523 (defun org-latex-convert-region-to-latex ()
3524 "Assume the current region has Org syntax, and convert it to LaTeX.
3525 This can be used in any buffer. For example, you can write an
3526 itemized list in Org syntax in an LaTeX buffer and use this
3527 command to convert it."
3529 (org-export-replace-region-by 'latex
))
3532 (defun org-latex-export-to-latex
3533 (&optional async subtreep visible-only body-only ext-plist
)
3534 "Export current buffer to a LaTeX file.
3536 If narrowing is active in the current buffer, only export its
3539 If a region is active, export that region.
3541 A non-nil optional argument ASYNC means the process should happen
3542 asynchronously. The resulting file should be accessible through
3543 the `org-export-stack' interface.
3545 When optional argument SUBTREEP is non-nil, export the sub-tree
3546 at point, extracting information from the headline properties
3549 When optional argument VISIBLE-ONLY is non-nil, don't export
3550 contents of hidden elements.
3552 When optional argument BODY-ONLY is non-nil, only write code
3553 between \"\\begin{document}\" and \"\\end{document}\".
3555 EXT-PLIST, when provided, is a property list with external
3556 parameters overriding Org default settings, but still inferior to
3557 file-local settings."
3559 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
3560 (org-export-to-file 'latex outfile
3561 async subtreep visible-only body-only ext-plist
)))
3564 (defun org-latex-export-to-pdf
3565 (&optional async subtreep visible-only body-only ext-plist
)
3566 "Export current buffer to LaTeX then process through to PDF.
3568 If narrowing is active in the current buffer, only export its
3571 If a region is active, export that region.
3573 A non-nil optional argument ASYNC means the process should happen
3574 asynchronously. The resulting file should be accessible through
3575 the `org-export-stack' interface.
3577 When optional argument SUBTREEP is non-nil, export the sub-tree
3578 at point, extracting information from the headline properties
3581 When optional argument VISIBLE-ONLY is non-nil, don't export
3582 contents of hidden elements.
3584 When optional argument BODY-ONLY is non-nil, only write code
3585 between \"\\begin{document}\" and \"\\end{document}\".
3587 EXT-PLIST, when provided, is a property list with external
3588 parameters overriding Org default settings, but still inferior to
3589 file-local settings.
3591 Return PDF file's name."
3593 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
3594 (org-export-to-file 'latex outfile
3595 async subtreep visible-only body-only ext-plist
3596 (lambda (file) (org-latex-compile file
)))))
3598 (defun org-latex-compile (texfile &optional snippet
)
3599 "Compile a TeX file.
3601 TEXFILE is the name of the file being compiled. Processing is
3602 done through the command specified in `org-latex-pdf-process',
3603 which see. Output is redirected to \"*Org PDF LaTeX Output*\"
3606 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3607 file used to preview a LaTeX snippet. In this case, do not
3608 create a log buffer and do not remove log files.
3610 Return PDF file name or raise an error if it couldn't be
3612 (unless snippet
(message "Processing LaTeX file %s..." texfile
))
3614 (or (with-temp-buffer
3615 (save-excursion (insert-file-contents texfile
))
3616 (and (search-forward-regexp (regexp-opt org-latex-compilers
)
3617 (line-end-position 2)
3619 (progn (beginning-of-line) (looking-at-p "%"))
3622 (process (if (functionp org-latex-pdf-process
) org-latex-pdf-process
3623 ;; Replace "%latex" and "%bibtex" with,
3624 ;; respectively, "%L" and "%B" so as to adhere to
3625 ;; `format-spec' specifications.
3626 (mapcar (lambda (command)
3627 (replace-regexp-in-string
3628 "%\\(?:bib\\|la\\)tex\\>"
3629 (lambda (m) (upcase (substring m
0 2)))
3631 org-latex-pdf-process
)))
3632 (spec `((?B .
,(shell-quote-argument org-latex-bib-compiler
))
3633 (?L .
,(shell-quote-argument compiler
))))
3634 (log-buf-name "*Org PDF LaTeX Output*")
3635 (log-buf (and (not snippet
) (get-buffer-create log-buf-name
)))
3636 (outfile (org-compile-file texfile process
"pdf"
3637 (format "See %S for details" log-buf-name
)
3640 (when org-latex-remove-logfiles
3643 (file-name-directory outfile
)
3645 (concat (regexp-quote (file-name-base outfile
))
3646 "\\(?:\\.[0-9]+\\)?\\."
3647 (regexp-opt org-latex-logfiles-extensions
))
3649 (let ((warnings (org-latex--collect-warnings log-buf
)))
3650 (message (concat "PDF file produced"
3652 ((eq warnings
'error
) " with errors.")
3653 (warnings (concat " with warnings: " warnings
))
3655 ;; Return output file name.
3658 (defun org-latex--collect-warnings (buffer)
3659 "Collect some warnings from \"pdflatex\" command output.
3660 BUFFER is the buffer containing output. Return collected
3661 warnings types as a string, `error' if a LaTeX error was
3662 encountered or nil if there was none."
3663 (with-current-buffer buffer
3665 (goto-char (point-max))
3666 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t
)
3667 (if (re-search-forward "^!" nil t
) 'error
3668 (let ((case-fold-search t
)
3670 (dolist (warning org-latex-known-warnings
)
3671 (when (save-excursion (re-search-forward (car warning
) nil t
))
3672 (setq warnings
(concat warnings
" " (cdr warning
)))))
3673 (org-string-nw-p (org-trim warnings
))))))))
3676 (defun org-latex-publish-to-latex (plist filename pub-dir
)
3677 "Publish an Org file to LaTeX.
3679 FILENAME is the filename of the Org file to be published. PLIST
3680 is the property list for the given project. PUB-DIR is the
3681 publishing directory.
3683 Return output file name."
3684 (org-publish-org-to 'latex filename
".tex" plist pub-dir
))
3687 (defun org-latex-publish-to-pdf (plist filename pub-dir
)
3688 "Publish an Org file to PDF (via LaTeX).
3690 FILENAME is the filename of the Org file to be published. PLIST
3691 is the property list for the given project. PUB-DIR is the
3692 publishing directory.
3694 Return output file name."
3695 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3696 ;; in working directory and then moved to publishing directory.
3697 (org-publish-attachment
3699 ;; Default directory could be anywhere when this function is
3700 ;; called. We ensure it is set to source file directory during
3701 ;; compilation so as to not break links to external documents.
3702 (let ((default-directory (file-name-directory filename
)))
3705 'latex filename
".tex" plist
(file-name-directory filename
))))
3712 ;; generated-autoload-file: "org-loaddefs.el"
3715 ;;; ox-latex.el ends here