1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2011-2019 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 <https://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-scale nil nil org-latex-image-default-scale
)
131 (:latex-image-default-height nil nil org-latex-image-default-height
)
132 (:latex-image-default-option nil nil org-latex-image-default-option
)
133 (:latex-image-default-width nil nil org-latex-image-default-width
)
134 (:latex-images-centered nil nil org-latex-images-centered
)
135 (:latex-inactive-timestamp-format nil nil org-latex-inactive-timestamp-format
)
136 (:latex-inline-image-rules nil nil org-latex-inline-image-rules
)
137 (:latex-link-with-unknown-path-format nil nil org-latex-link-with-unknown-path-format
)
138 (:latex-listings nil nil org-latex-listings
)
139 (:latex-listings-langs nil nil org-latex-listings-langs
)
140 (:latex-listings-options nil nil org-latex-listings-options
)
141 (:latex-minted-langs nil nil org-latex-minted-langs
)
142 (:latex-minted-options nil nil org-latex-minted-options
)
143 (:latex-prefer-user-labels nil nil org-latex-prefer-user-labels
)
144 (:latex-subtitle-format nil nil org-latex-subtitle-format
)
145 (:latex-subtitle-separate nil nil org-latex-subtitle-separate
)
146 (:latex-table-scientific-notation nil nil org-latex-table-scientific-notation
)
147 (:latex-tables-booktabs nil nil org-latex-tables-booktabs
)
148 (:latex-tables-centered nil nil org-latex-tables-centered
)
149 (:latex-text-markup-alist nil nil org-latex-text-markup-alist
)
150 (:latex-title-command nil nil org-latex-title-command
)
151 (:latex-toc-command nil nil org-latex-toc-command
)
152 (:latex-compiler
"LATEX_COMPILER" nil org-latex-compiler
)
153 ;; Redefine regular options.
154 (:date
"DATE" nil
"\\today" parse
)))
158 ;;; Internal Variables
160 (defconst org-latex-babel-language-alist
161 '(("af" .
"afrikaans")
163 ("bt-br" .
"brazilian")
169 ("de-at" .
"naustrian")
170 ("de-de" .
"ngerman")
173 ("en-au" .
"australian")
174 ("en-ca" .
"canadian")
175 ("en-gb" .
"british")
177 ("en-nz" .
"newzealand")
178 ("en-us" .
"american")
184 ("fr-ca" .
"canadien")
188 ("id" .
"indonesian")
198 ("pt" .
"portuguese")
202 ("sb" .
"uppersorbian")
210 ("uk" .
"ukrainian"))
211 "Alist between language code and corresponding Babel option.")
213 (defconst org-latex-polyglossia-language-alist
221 ("bt-br" "brazilian")
227 ("de" "german" "german")
228 ("de-at" "german" "austrian")
229 ("de-de" "german" "german")
232 ("en" "english" "usmax")
233 ("en-au" "english" "australian")
234 ("en-gb" "english" "uk")
235 ("en-nz" "english" "newzealand")
236 ("en-us" "english" "usmax")
258 ("la" "latin" "modern")
259 ("la-modern" "latin" "modern")
260 ("la-classic" "latin" "classic")
261 ("la-medieval" "latin" "medieval")
274 ("pms" "piedmontese")
297 "Alist between language code and corresponding Polyglossia option")
301 (defconst org-latex-table-matrix-macros
'(("bordermatrix" .
"\\cr")
302 ("qbordermatrix" .
"\\cr")
303 ("kbordermatrix" .
"\\\\"))
304 "Alist between matrix macros and their row ending.")
306 (defconst org-latex-math-environments-re
308 "\\`[ \t]*\\\\begin{%s\\*?}"
310 '("equation" "eqnarray" "math" "displaymath"
311 "align" "gather" "multline" "flalign" "alignat"
312 "xalignat" "xxalignat"
315 "dmath" "dseries" "dgroup" "darray"
318 "Regexp of LaTeX math environments.")
321 ;;; User Configurable Variables
323 (defgroup org-export-latex nil
324 "Options for exporting Org mode files to LaTeX."
325 :tag
"Org Export LaTeX"
330 (defcustom org-latex-caption-above
'(table)
331 "When non-nil, place caption string at the beginning of elements.
332 Otherwise, place it near the end. When value is a list of
333 symbols, put caption above selected elements only. Allowed
334 symbols are: `image', `table', `src-block' and `special-block'."
335 :group
'org-export-latex
337 :package-version
'(Org .
"8.3")
339 (const :tag
"For all elements" t
)
340 (const :tag
"For no element" nil
)
341 (set :tag
"For the following elements only" :greedy t
342 (const :tag
"Images" image
)
343 (const :tag
"Tables" table
)
344 (const :tag
"Source code" src-block
)
345 (const :tag
"Special blocks" special-block
))))
347 (defcustom org-latex-prefer-user-labels nil
348 "Use user-provided labels instead of internal ones when non-nil.
350 When this variable is non-nil, Org will use the value of
351 CUSTOM_ID property, NAME keyword or Org target as the key for the
352 \\label commands generated.
354 By default, Org generates its own internal labels during LaTeX
355 export. This process ensures that the \\label keys are unique
356 and valid, but it means the keys are not available in advance of
359 Setting this variable gives you control over how Org generates
360 labels during LaTeX export, so that you may know their keys in
361 advance. One reason to do this is that it allows you to refer to
362 various elements using a single label both in Org's link syntax
363 and in embedded LaTeX code.
365 For example, when this variable is non-nil, a headline like this:
371 This is section [[#sec:foo]].
373 And this is still section \\ref{sec:foo}.
376 will be exported to LaTeX as:
378 \\subsection{Some section}
380 This is section \\ref{sec:foo}.
381 And this is still section \\ref{sec:foo}.
383 Note, however, that setting this variable introduces a limitation
384 on the possible values for CUSTOM_ID and NAME. When this
385 variable is non-nil, Org passes their value to \\label unchanged.
386 You are responsible for ensuring that the value is a valid LaTeX
387 \\label key, and that no other \\label commands with the same key
388 appear elsewhere in your document. (Keys may contain letters,
389 numbers, and the following punctuation: '_' '.' '-' ':'.) There
390 are no such limitations on CUSTOM_ID and NAME when this variable
393 For headlines that do not define the CUSTOM_ID property or
394 elements without a NAME, Org will continue to use its default
395 labeling scheme to generate labels and resolve links into proper
397 :group
'org-export-latex
400 :package-version
'(Org .
"8.3"))
404 (defcustom org-latex-default-class
"article"
405 "The default LaTeX class."
406 :group
'org-export-latex
407 :type
'(string :tag
"LaTeX class"))
409 (defcustom org-latex-classes
411 "\\documentclass[11pt]{article}"
412 ("\\section{%s}" .
"\\section*{%s}")
413 ("\\subsection{%s}" .
"\\subsection*{%s}")
414 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
415 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
416 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
418 "\\documentclass[11pt]{report}"
419 ("\\part{%s}" .
"\\part*{%s}")
420 ("\\chapter{%s}" .
"\\chapter*{%s}")
421 ("\\section{%s}" .
"\\section*{%s}")
422 ("\\subsection{%s}" .
"\\subsection*{%s}")
423 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
425 "\\documentclass[11pt]{book}"
426 ("\\part{%s}" .
"\\part*{%s}")
427 ("\\chapter{%s}" .
"\\chapter*{%s}")
428 ("\\section{%s}" .
"\\section*{%s}")
429 ("\\subsection{%s}" .
"\\subsection*{%s}")
430 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
431 "Alist of LaTeX classes and associated header and structure.
432 If #+LATEX_CLASS is set in the buffer, use its value and the
433 associated information. Here is the structure of each cell:
437 (numbered-section . unnumbered-section)
443 The HEADER-STRING is the header that will be inserted into the
444 LaTeX file. It should contain the \\documentclass macro, and
445 anything else that is needed for this setup. To this header, the
446 following commands will be added:
448 - Calls to \\usepackage for all packages mentioned in the
449 variables `org-latex-default-packages-alist' and
450 `org-latex-packages-alist'. Thus, your header definitions
451 should avoid to also request these packages.
453 - Lines specified via \"#+LATEX_HEADER:\" and
454 \"#+LATEX_HEADER_EXTRA:\" keywords.
456 If you need more control about the sequence in which the header
457 is built up, or if you want to exclude one of these building
458 blocks for a particular class, you can use the following
459 macro-like placeholders.
461 [DEFAULT-PACKAGES] \\usepackage statements for default packages
462 [NO-DEFAULT-PACKAGES] do not include any of the default packages
463 [PACKAGES] \\usepackage statements for packages
464 [NO-PACKAGES] do not include the packages
465 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
466 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
470 \\documentclass{article}
471 [NO-DEFAULT-PACKAGES]
473 \\providecommand{\\alert}[1]{\\textbf{#1}}
476 will omit the default packages, and will include the
477 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
478 to \\providecommand, and then place \\usepackage commands based
479 on the content of `org-latex-packages-alist'.
481 If your header, `org-latex-default-packages-alist' or
482 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
483 AUTO will automatically be replaced with a coding system derived
484 from `buffer-file-coding-system'. See also the variable
485 `org-latex-inputenc-alist' for a way to influence this mechanism.
487 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\"
488 or \"\\usepackage[AUTO]{polyglossia}\", AUTO will be replaced
489 with the language related to the language code specified by
490 `org-export-default-language'. Note that constructions such as
491 \"\\usepackage[french,AUTO,english]{babel}\" are permitted. For
492 Polyglossia the language will be set via the macros
493 \"\\setmainlanguage\" and \"\\setotherlanguage\". See also
494 `org-latex-guess-babel-language' and
495 `org-latex-guess-polyglossia-language'.
497 The sectioning structure
498 ------------------------
500 The sectioning structure of the class is given by the elements
501 following the header string. For each sectioning level, a number
502 of strings is specified. A %s formatter is mandatory in each
503 section string and will be replaced by the title of the section.
505 Instead of a cons cell (numbered . unnumbered), you can also
506 provide a list of 2 or 4 elements,
508 (numbered-open numbered-close)
512 (numbered-open numbered-close unnumbered-open unnumbered-close)
514 providing opening and closing strings for a LaTeX environment
515 that should represent the document section. The opening clause
516 should have a %s to represent the section title.
518 Instead of a list of sectioning commands, you can also specify
519 a function name. That function will be called with two
520 parameters, the (reduced) level of the headline, and a predicate
521 non-nil when the headline should be numbered. It must return
522 a format string in which the section title will be added."
523 :group
'org-export-latex
525 (list (string :tag
"LaTeX class")
526 (string :tag
"LaTeX header")
527 (repeat :tag
"Levels" :inline t
530 (string :tag
" numbered")
531 (string :tag
"unnumbered"))
532 (list :tag
"Environment"
533 (string :tag
"Opening (numbered)")
534 (string :tag
"Closing (numbered)")
535 (string :tag
"Opening (unnumbered)")
536 (string :tag
"Closing (unnumbered)"))
537 (function :tag
"Hook computing sectioning"))))))
539 (defcustom org-latex-inputenc-alist nil
540 "Alist of inputenc coding system names, and what should really be used.
541 For example, adding an entry
543 (\"utf8\" . \"utf8x\")
545 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
546 are written as utf8 files."
547 :group
'org-export-latex
550 (string :tag
"Derived from buffer")
551 (string :tag
"Use this instead"))))
553 (defcustom org-latex-title-command
"\\maketitle"
554 "The command used to insert the title just after \\begin{document}.
556 This format string may contain these elements:
558 %a for AUTHOR keyword
560 %s for SUBTITLE keyword
562 %d for DESCRIPTION line
564 %l for Language keyword
565 %L for capitalized language keyword
568 If you need to use a \"%\" character, you need to escape it
571 Setting :latex-title-command in publishing projects will take
572 precedence over this variable."
573 :group
'org-export-latex
574 :type
'(string :tag
"Format string"))
576 (defcustom org-latex-subtitle-format
"\\\\\\medskip\n\\large %s"
577 "Format string used for transcoded subtitle.
578 The format string should have at most one \"%s\"-expression,
579 which is replaced with the subtitle."
580 :group
'org-export-latex
582 :package-version
'(Org .
"8.3")
583 :type
'(string :tag
"Format string"))
585 (defcustom org-latex-subtitle-separate nil
586 "Non-nil means the subtitle is not typeset as part of title."
587 :group
'org-export-latex
589 :package-version
'(Org .
"8.3")
592 (defcustom org-latex-toc-command
"\\tableofcontents\n\n"
593 "LaTeX command to set the table of contents, list of figures, etc.
594 This command only applies to the table of contents generated with
595 the toc:nil option, not to those generated with #+TOC keyword."
596 :group
'org-export-latex
599 (defcustom org-latex-hyperref-template
600 "\\hypersetup{\n pdfauthor={%a},\n pdftitle={%t},\n pdfkeywords={%k},
601 pdfsubject={%d},\n pdfcreator={%c}, \n pdflang={%L}}\n"
602 "Template for hyperref package options.
604 This format string may contain these elements:
606 %a for AUTHOR keyword
608 %s for SUBTITLE keyword
610 %d for DESCRIPTION line
612 %l for Language keyword
613 %L for capitalized language keyword
616 If you need to use a \"%\" character, you need to escape it
619 As a special case, a nil value prevents template from being
622 Setting :latex-hyperref-template in publishing projects will take
623 precedence over this variable."
624 :group
'org-export-latex
626 :package-version
'(Org .
"8.3")
627 :type
'(choice (const :tag
"No template" nil
)
628 (string :tag
"Format string")))
632 (defcustom org-latex-format-headline-function
633 'org-latex-format-headline-default-function
634 "Function for formatting the headline's text.
636 This function will be called with six arguments:
637 TODO the todo keyword (string or nil)
638 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
639 PRIORITY the priority of the headline (integer or nil)
640 TEXT the main headline text (string)
641 TAGS the tags (list of strings or nil)
642 INFO the export options (plist)
644 The function result will be used in the section format string."
645 :group
'org-export-latex
647 :package-version
'(Org .
"8.0")
653 (defcustom org-latex-footnote-separator
"\\textsuperscript{,}\\,"
654 "Text used to separate footnotes."
655 :group
'org-export-latex
658 (defcustom org-latex-footnote-defined-format
"\\textsuperscript{\\ref{%s}}"
659 "Format string used to format reference to footnote already defined.
660 %s will be replaced by the label of the referred footnote."
661 :group
'org-export-latex
663 (const :tag
"Use plain superscript (default)" "\\textsuperscript{\\ref{%s}}")
664 (const :tag
"Use Memoir/KOMA-Script footref" "\\footref{%s}")
665 (string :tag
"Other format string"))
667 :package-version
'(Org .
"9.0"))
671 (defcustom org-latex-active-timestamp-format
"\\textit{%s}"
672 "A printf format string to be applied to active timestamps."
673 :group
'org-export-latex
676 (defcustom org-latex-inactive-timestamp-format
"\\textit{%s}"
677 "A printf format string to be applied to inactive timestamps."
678 :group
'org-export-latex
681 (defcustom org-latex-diary-timestamp-format
"\\textit{%s}"
682 "A printf format string to be applied to diary timestamps."
683 :group
'org-export-latex
689 (defcustom org-latex-images-centered t
690 "When non-nil, images are centered."
691 :group
'org-export-latex
693 :package-version
'(Org .
"9.0")
697 (defcustom org-latex-image-default-option
""
698 "Default option for images."
699 :group
'org-export-latex
701 :package-version
'(Org .
"8.0")
704 (defcustom org-latex-image-default-width
".9\\linewidth"
705 "Default width for images.
706 This value will not be used if a height is provided."
707 :group
'org-export-latex
709 :package-version
'(Org .
"8.0")
712 (defcustom org-latex-image-default-scale
""
713 "Default scale for images.
714 This value will not be used if a width or a scale is provided,
715 or if the image is wrapped within a \"wrapfigure\" environment.
716 Scale overrides width and height."
717 :group
'org-export-latex
718 :package-version
'(Org .
"9.3")
722 (defcustom org-latex-image-default-height
""
723 "Default height for images.
724 This value will not be used if a width is provided, or if the
725 image is wrapped within a \"figure\" or \"wrapfigure\"
727 :group
'org-export-latex
729 :package-version
'(Org .
"8.0")
732 (defcustom org-latex-default-figure-position
"htbp"
733 "Default position for LaTeX figures."
734 :group
'org-export-latex
737 :package-version
'(Org .
"9.0")
740 (defcustom org-latex-inline-image-rules
741 `(("file" .
,(regexp-opt
742 '("pdf" "jpeg" "jpg" "png" "ps" "eps" "tikz" "pgf" "svg"))))
743 "Rules characterizing image files that can be inlined into LaTeX.
745 A rule consists in an association whose key is the type of link
746 to consider, and value is a regexp that will be matched against
749 Note that, by default, the image extension *actually* allowed
750 depend on the way the LaTeX file is processed. When used with
751 pdflatex, pdf, jpg and png images are OK. When processing
752 through dvi to Postscript, only ps and eps are allowed. The
753 default we use here encompasses both."
754 :group
'org-export-latex
756 :package-version
'(Org .
"8.0")
757 :type
'(alist :key-type
(string :tag
"Type")
758 :value-type
(regexp :tag
"Path")))
760 (defcustom org-latex-link-with-unknown-path-format
"\\texttt{%s}"
761 "Format string for links with unknown path type."
762 :group
'org-export-latex
768 (defcustom org-latex-default-table-environment
"tabular"
769 "Default environment used to build tables."
770 :group
'org-export-latex
772 :package-version
'(Org .
"8.0")
775 (defcustom org-latex-default-table-mode
'table
776 "Default mode for tables.
778 Value can be a symbol among:
780 `table' Regular LaTeX table.
782 `math' In this mode, every cell is considered as being in math
783 mode and the complete table will be wrapped within a math
784 environment. It is particularly useful to write matrices.
786 `inline-math' This mode is almost the same as `math', but the
787 math environment will be inlined.
789 `verbatim' The table is exported as it appears in the Org
790 buffer, within a verbatim environment.
792 This value can be overridden locally with, i.e. \":mode math\" in
795 When modifying this variable, it may be useful to change
796 `org-latex-default-table-environment' accordingly."
797 :group
'org-export-latex
799 :package-version
'(Org .
"8.0")
800 :type
'(choice (const :tag
"Table" table
)
801 (const :tag
"Matrix" math
)
802 (const :tag
"Inline matrix" inline-math
)
803 (const :tag
"Verbatim" verbatim
))
804 :safe
(lambda (s) (memq s
'(table math inline-math verbatim
))))
806 (defcustom org-latex-tables-centered t
807 "When non-nil, tables are exported in a center environment."
808 :group
'org-export-latex
812 (defcustom org-latex-tables-booktabs nil
813 "When non-nil, display tables in a formal \"booktabs\" style.
814 This option assumes that the \"booktabs\" package is properly
815 loaded in the header of the document. This value can be ignored
816 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
818 :group
'org-export-latex
820 :package-version
'(Org .
"8.0")
824 (defcustom org-latex-table-scientific-notation nil
825 "Format string to display numbers in scientific notation.
827 The format should have \"%s\" twice, for mantissa and exponent
828 \(i.e., \"%s\\\\times10^{%s}\").
830 When nil, no transformation is made."
831 :group
'org-export-latex
833 :package-version
'(Org .
"8.0")
835 (string :tag
"Format string")
836 (const :tag
"No formatting" nil
)))
840 (defcustom org-latex-text-markup-alist
'((bold .
"\\textbf{%s}")
841 (code . protectedtexttt
)
842 (italic .
"\\emph{%s}")
843 (strike-through .
"\\sout{%s}")
844 (underline .
"\\uline{%s}")
845 (verbatim . protectedtexttt
))
846 "Alist of LaTeX expressions to convert text markup.
848 The key must be a symbol among `bold', `code', `italic',
849 `strike-through', `underline' and `verbatim'. The value is
850 a formatting string to wrap fontified text with.
852 Value can also be set to the following symbols: `verb' and
853 `protectedtexttt'. For the former, Org will use \"\\verb\" to
854 create a format string and select a delimiter character that
855 isn't in the string. For the latter, Org will use \"\\texttt\"
856 to typeset and try to protect special characters.
858 If no association can be found for a given markup, text will be
860 :group
'org-export-latex
862 :package-version
'(Org .
"8.3")
864 :options
'(bold code italic strike-through underline verbatim
))
869 (defcustom org-latex-format-drawer-function
(lambda (_ contents
) contents
)
870 "Function called to format a drawer in LaTeX code.
872 The function must accept two parameters:
873 NAME the drawer name, like \"LOGBOOK\"
874 CONTENTS the contents of the drawer.
876 The function should return the string to be exported.
878 The default function simply returns the value of CONTENTS."
879 :group
'org-export-latex
881 :package-version
'(Org .
"8.3")
887 (defcustom org-latex-format-inlinetask-function
888 'org-latex-format-inlinetask-default-function
889 "Function called to format an inlinetask in LaTeX code.
891 The function must accept seven parameters:
892 TODO the todo keyword (string or nil)
893 TODO-TYPE the todo type (symbol: `todo', `done', nil)
894 PRIORITY the inlinetask priority (integer or nil)
895 NAME the inlinetask name (string)
896 TAGS the inlinetask tags (list of strings or nil)
897 CONTENTS the contents of the inlinetask (string or nil)
898 INFO the export options (plist)
900 The function should return the string to be exported."
901 :group
'org-export-latex
904 :package-version
'(Org .
"8.3"))
909 (defcustom org-latex-listings nil
910 "Non-nil means export source code using the listings package.
912 This package will fontify source code, possibly even with color.
913 If you want to use this, you also need to make LaTeX use the
914 listings package, and if you want to have color, the color
915 package. Just add these to `org-latex-packages-alist', for
916 example using customize, or with something like:
918 (require \\='ox-latex)
919 (add-to-list \\='org-latex-packages-alist \\='(\"\" \"listings\"))
920 (add-to-list \\='org-latex-packages-alist \\='(\"\" \"color\"))
924 (setq org-latex-listings \\='minted)
926 causes source code to be exported using the minted package as
927 opposed to listings. If you want to use minted, you need to add
928 the minted package to `org-latex-packages-alist', for example
929 using customize, or with
931 (require \\='ox-latex)
932 (add-to-list \\='org-latex-packages-alist \\='(\"newfloat\" \"minted\"))
934 In addition, it is necessary to install pygments
935 \(URL `http://pygments.org>'), and to configure the variable
936 `org-latex-pdf-process' so that the -shell-escape option is
939 The minted choice has possible repercussions on the preview of
940 latex fragments (see `org-preview-latex-fragment'). If you run
941 into previewing problems, please consult
942 URL `https://orgmode.org/worg/org-tutorials/org-latex-preview.html'."
943 :group
'org-export-latex
945 (const :tag
"Use listings" t
)
946 (const :tag
"Use minted" minted
)
947 (const :tag
"Export verbatim" nil
))
948 :safe
(lambda (s) (memq s
'(t nil minted
))))
950 (defcustom org-latex-listings-langs
951 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
954 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
955 (html "HTML") (xml "XML")
956 (tex "TeX") (latex "[LaTeX]TeX")
957 (shell-script "bash")
959 (ocaml "Caml") (caml "Caml")
960 (sql "SQL") (sqlite "sql")
963 "Alist mapping languages to their listing language counterpart.
964 The key is a symbol, the major mode symbol without the \"-mode\".
965 The value is the string that should be inserted as the language
966 parameter for the listings package. If the mode name and the
967 listings name are the same, the language does not need an entry
968 in this list - but it does not hurt if it is present."
969 :group
'org-export-latex
971 :package-version
'(Org .
"8.3")
974 (symbol :tag
"Major mode ")
975 (string :tag
"Listings language"))))
977 (defcustom org-latex-listings-options nil
978 "Association list of options for the latex listings package.
980 These options are supplied as a comma-separated list to the
981 \\lstset command. Each element of the association list should be
982 a list containing two strings: the name of the option, and the
985 (setq org-latex-listings-options
986 \\='((\"basicstyle\" \"\\\\small\")
987 (\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
989 will typeset the code in a small size font with underlined, bold
992 Note that the same options will be applied to blocks of all
993 languages. If you need block-specific options, you may use the
996 #+ATTR_LATEX: :options key1=value1,key2=value2
1000 :group
'org-export-latex
1003 (string :tag
"Listings option name ")
1004 (string :tag
"Listings option value"))))
1006 (defcustom org-latex-minted-langs
1007 '((emacs-lisp "common-lisp")
1010 (shell-script "bash")
1012 "Alist mapping languages to their minted language counterpart.
1013 The key is a symbol, the major mode symbol without the \"-mode\".
1014 The value is the string that should be inserted as the language
1015 parameter for the minted package. If the mode name and the
1016 listings name are the same, the language does not need an entry
1017 in this list - but it does not hurt if it is present.
1019 Note that minted uses all lower case for language identifiers,
1020 and that the full list of language identifiers can be obtained
1023 pygmentize -L lexers"
1024 :group
'org-export-latex
1027 (symbol :tag
"Major mode ")
1028 (string :tag
"Minted language"))))
1030 (defcustom org-latex-minted-options nil
1031 "Association list of options for the latex minted package.
1033 These options are supplied within square brackets in
1034 \\begin{minted} environments. Each element of the alist should
1035 be a list containing two strings: the name of the option, and the
1038 (setq org-latex-minted-options
1039 \\='((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
1041 will result in source blocks being exported with
1043 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
1045 as the start of the minted environment. Note that the same
1046 options will be applied to blocks of all languages. If you need
1047 block-specific options, you may use the following syntax:
1049 #+ATTR_LATEX: :options key1=value1,key2=value2
1053 :group
'org-export-latex
1056 (string :tag
"Minted option name ")
1057 (string :tag
"Minted option value"))))
1059 (defcustom org-latex-custom-lang-environments nil
1060 "Alist mapping languages to language-specific LaTeX environments.
1062 It is used during export of source blocks by the listings and
1063 minted LaTeX packages. The environment may be a simple string,
1064 composed of only letters and numbers. In this case, the string
1065 is directly the name of the LaTeX environment to use. The
1066 environment may also be a format string. In this case the format
1067 string will be directly exported. This format string may contain
1070 %s for the formatted source
1072 %f for the float attribute
1073 %l for an appropriate label
1074 %o for the LaTeX attributes
1078 (setq org-latex-custom-lang-environments
1079 \\='((python \"pythoncode\")
1080 (ocaml \"\\\\begin{listing}
1081 \\\\begin{minted}[%o]{ocaml}
1086 would have the effect that if Org encounters a Python source block
1087 during LaTeX export it will produce
1093 and if Org encounters an Ocaml source block during LaTeX export it
1097 \\begin{minted}[<attr_latex options>]{ocaml}
1100 \\caption{<caption>}
1103 :group
'org-export-latex
1106 (symbol :tag
"Language name ")
1107 (string :tag
"Environment name or format string")))
1109 :package-version
'(Org .
"9.0"))
1114 (defcustom org-latex-compiler-file-string
"%% Intended LaTeX compiler: %s\n"
1115 "LaTeX compiler format-string.
1116 See also `org-latex-compiler'."
1117 :group
'org-export-latex
1119 (const :tag
"Comment" "%% Intended LaTeX compiler: %s\n")
1120 (const :tag
"latex-mode file variable" "%% -*- latex-run-command: %s -*-\n")
1121 (const :tag
"AUCTeX file variable" "%% -*- LaTeX-command: %s -*-\n")
1122 (string :tag
"custom format" "%% %s"))
1124 :package-version
'(Org .
"9.0"))
1126 (defcustom org-latex-compiler
"pdflatex"
1127 "LaTeX compiler to use.
1129 Must be an element in `org-latex-compilers' or the empty quote.
1130 Can also be set in buffers via #+LATEX_COMPILER. See also
1131 `org-latex-compiler-file-string'."
1132 :group
'org-export-latex
1134 (const :tag
"pdfLaTeX" "pdflatex")
1135 (const :tag
"XeLaTeX" "xelatex")
1136 (const :tag
"LuaLaTeX" "lualatex")
1137 (const :tag
"Unset" ""))
1139 :package-version
'(Org .
"9.0"))
1141 (defconst org-latex-compilers
'("pdflatex" "xelatex" "lualatex")
1142 "Known LaTeX compilers.
1143 See also `org-latex-compiler'.")
1145 (defcustom org-latex-bib-compiler
"bibtex"
1146 "Command to process a LaTeX file's bibliography.
1148 The shorthand %bib in `org-latex-pdf-process' is replaced with
1151 A better approach is to use a compiler suit such as `latexmk'."
1152 :group
'org-export-latex
1153 :type
'(choice (const :tag
"BibTeX" "bibtex")
1154 (const :tag
"Biber" "biber")
1155 (string :tag
"Other process"))
1157 :package-version
'(Org .
"9.0"))
1159 (defcustom org-latex-pdf-process
1160 '("%latex -interaction nonstopmode -output-directory %o %f"
1161 "%latex -interaction nonstopmode -output-directory %o %f"
1162 "%latex -interaction nonstopmode -output-directory %o %f")
1163 "Commands to process a LaTeX file to a PDF file.
1165 This is a list of strings, each of them will be given to the
1166 shell as a command. %f in the command will be replaced by the
1167 relative file name, %F by the absolute file name, %b by the file
1168 base name (i.e. without directory and extension parts), %o by the
1169 base directory of the file, %O by the absolute file name of the
1170 output file, %latex is the LaTeX compiler (see
1171 `org-latex-compiler'), and %bib is the BibTeX-like compiler (see
1172 `org-latex-bib-compiler').
1174 The reason why this is a list is that it usually takes several
1175 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
1176 does not have a clever mechanism to detect which of these
1177 commands have to be run to get to a stable result, and it also
1178 does not do any error checking.
1180 Consider a smart LaTeX compiler such as `texi2dvi' or `latexmk',
1181 which calls the \"correct\" combinations of auxiliary programs.
1183 Alternatively, this may be a Lisp function that does the
1184 processing, so you could use this to apply the machinery of
1185 AUCTeX or the Emacs LaTeX mode. This function should accept the
1186 file name as its single argument."
1187 :group
'org-export-pdf
1189 (repeat :tag
"Shell command sequence"
1190 (string :tag
"Shell command"))
1191 (const :tag
"2 runs of latex"
1192 ("%latex -interaction nonstopmode -output-directory %o %f"
1193 "%latex -interaction nonstopmode -output-directory %o %f"))
1194 (const :tag
"3 runs of latex"
1195 ("%latex -interaction nonstopmode -output-directory %o %f"
1196 "%latex -interaction nonstopmode -output-directory %o %f"
1197 "%latex -interaction nonstopmode -output-directory %o %f"))
1198 (const :tag
"latex,bibtex,latex,latex"
1199 ("%latex -interaction nonstopmode -output-directory %o %f"
1201 "%latex -interaction nonstopmode -output-directory %o %f"
1202 "%latex -interaction nonstopmode -output-directory %o %f"))
1203 (const :tag
"texi2dvi"
1204 ("cd %o; LATEX=\"%latex\" texi2dvi -p -b -V %b.tex"))
1205 (const :tag
"latexmk"
1206 ("latexmk -g -pdf -pdflatex=\"%latex\" -outdir=%o %f"))
1209 (defcustom org-latex-logfiles-extensions
1210 '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
1211 "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
1212 "The list of file extensions to consider as LaTeX logfiles.
1213 The logfiles will be removed if `org-latex-remove-logfiles' is
1215 :group
'org-export-latex
1217 :package-version
'(Org .
"8.3")
1218 :type
'(repeat (string :tag
"Extension")))
1220 (defcustom org-latex-remove-logfiles t
1221 "Non-nil means remove the logfiles produced by PDF production.
1222 By default, logfiles are files with these extensions: .aux, .idx,
1223 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
1224 logfiles to remove, set `org-latex-logfiles-extensions'."
1225 :group
'org-export-latex
1228 (defcustom org-latex-known-warnings
1229 '(("Reference.*?undefined" .
"[undefined reference]")
1230 ("Runaway argument" .
"[runaway argument]")
1231 ("Underfull \\hbox" .
"[underfull hbox]")
1232 ("Overfull \\hbox" .
"[overfull hbox]")
1233 ("Citation.*?undefined" .
"[undefined citation]")
1234 ("Undefined control sequence" .
"[undefined control sequence]"))
1235 "Alist of regular expressions and associated messages for the user.
1236 The regular expressions are used to find possible warnings in the
1237 log of a LaTeX-run. These warnings will be reported after
1238 calling `org-latex-compile'."
1239 :group
'org-export-latex
1241 :package-version
'(Org .
"8.3")
1244 (string :tag
"Regexp")
1245 (string :tag
"Message"))))
1249 ;;; Internal Functions
1251 (defun org-latex--caption-above-p (element info
)
1252 "Non nil when caption is expected to be located above ELEMENT.
1253 INFO is a plist holding contextual information."
1254 (let ((above (plist-get info
:latex-caption-above
)))
1255 (if (symbolp above
) above
1256 (let ((type (org-element-type element
)))
1257 (memq (if (eq type
'link
) 'image type
) above
)))))
1259 (defun org-latex--label (datum info
&optional force full
)
1260 "Return an appropriate label for DATUM.
1261 DATUM is an element or a `target' type object. INFO is the
1262 current export state, as a plist.
1264 Return nil if element DATUM has no NAME or VALUE affiliated
1265 keyword or no CUSTOM_ID property, unless FORCE is non-nil. In
1266 this case always return a unique label.
1268 Eventually, if FULL is non-nil, wrap label within \"\\label{}\"."
1269 (let* ((type (org-element-type datum
))
1271 (org-element-property
1273 ((headline inlinetask
) :CUSTOM_ID
)
1278 (and (or user-label force
)
1279 (if (and user-label
(plist-get info
:latex-prefer-user-labels
))
1285 (and (string-match-p
1286 org-latex-math-environments-re
1287 (org-element-property :value datum
))
1289 (`latex-matrices
"eq:")
1291 (and (org-element-property :caption datum
)
1294 (org-export-get-reference datum info
))))))
1295 (cond ((not full
) label
)
1296 (label (format "\\label{%s}%s"
1298 (if (eq type
'target
) "" "\n")))
1301 (defun org-latex--caption/label-string
(element info
)
1302 "Return caption and label LaTeX string for ELEMENT.
1304 INFO is a plist holding contextual information. If there's no
1305 caption nor label, return the empty string.
1307 For non-floats, see `org-latex--wrap-label'."
1308 (let* ((label (org-latex--label element info nil t
))
1309 (main (org-export-get-caption element
))
1310 (attr (org-export-read-attribute :attr_latex element
))
1311 (type (org-element-type element
))
1312 (nonfloat (or (and (plist-member attr
:float
)
1313 (not (plist-get attr
:float
))
1315 (and (eq type
'src-block
)
1316 (not (plist-get attr
:float
))
1317 (null (plist-get info
:latex-listings
)))))
1318 (short (org-export-get-caption element t
))
1319 (caption-from-attr-latex (plist-get attr
:caption
)))
1321 ((org-string-nw-p caption-from-attr-latex
)
1322 (concat caption-from-attr-latex
"\n"))
1323 ((and (not main
) (equal label
"")) "")
1325 ;; Option caption format with short name.
1327 (format (if nonfloat
"\\captionof{%s}%s{%s%s}\n"
1328 "\\caption%s%s{%s%s}\n")
1329 (let ((type* (if (eq type
'latex-environment
)
1330 (org-latex--environment-type element
)
1334 (paragraph "figure")
1336 (special-block "figure")
1337 (src-block (if (plist-get info
:latex-listings
)
1340 (t (symbol-name type
*)))
1342 (if short
(format "[%s]" (org-export-data short info
)) "")
1344 (org-export-data main info
))))))
1346 (defun org-latex-guess-inputenc (header)
1347 "Set the coding system in inputenc to what the buffer is.
1349 HEADER is the LaTeX header string. This function only applies
1350 when specified inputenc option is \"AUTO\".
1352 Return the new header, as a string."
1353 (let* ((cs (or (ignore-errors
1354 (latexenc-coding-system-to-inputenc
1355 (or org-export-coding-system buffer-file-coding-system
)))
1358 ;; First translate if that is requested.
1359 (setq cs
(or (cdr (assoc cs org-latex-inputenc-alist
)) cs
))
1360 ;; Then find the \usepackage statement and replace the option.
1361 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1362 cs header t nil
1))))
1364 (defun org-latex-guess-babel-language (header info
)
1365 "Set Babel's language according to LANGUAGE keyword.
1367 HEADER is the LaTeX header string. INFO is the plist used as
1368 a communication channel.
1370 Insertion of guessed language only happens when Babel package has
1371 explicitly been loaded. Then it is added to the rest of
1374 The argument to Babel may be \"AUTO\" which is then replaced with
1375 the language of the document or `org-export-default-language'
1376 unless language in question is already loaded.
1378 Return the new header."
1379 (let ((language-code (plist-get info
:language
)))
1380 ;; If no language is set or Babel package is not loaded, return
1382 (if (or (not (stringp language-code
))
1383 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header
)))
1385 (let ((options (save-match-data
1386 (org-split-string (match-string 1 header
) ",[ \t]*")))
1387 (language (cdr (assoc-string language-code
1388 org-latex-babel-language-alist t
))))
1389 ;; If LANGUAGE is already loaded, return header without AUTO.
1390 ;; Otherwise, replace AUTO with language or append language if
1391 ;; AUTO is not present.
1393 (mapconcat (lambda (option) (if (equal "AUTO" option
) language option
))
1394 (cond ((member language options
) (delete "AUTO" options
))
1395 ((member "AUTO" options
) options
)
1396 (t (append options
(list language
))))
1400 (defun org-latex-guess-polyglossia-language (header info
)
1401 "Set the Polyglossia language according to the LANGUAGE keyword.
1403 HEADER is the LaTeX header string. INFO is the plist used as
1404 a communication channel.
1406 Insertion of guessed language only happens when the Polyglossia
1407 package has been explicitly loaded.
1409 The argument to Polyglossia may be \"AUTO\" which is then
1410 replaced with the language of the document or
1411 `org-export-default-language'. Note, the language is really set
1412 using \setdefaultlanguage and not as an option to the package.
1414 Return the new header."
1415 (let ((language (plist-get info
:language
)))
1416 ;; If no language is set or Polyglossia is not loaded, return
1418 (if (or (not (stringp language
))
1420 "\\\\usepackage\\(?:\\[\\([^]]+?\\)\\]\\){polyglossia}\n"
1423 (let* ((options (org-string-nw-p (match-string 1 header
)))
1424 (languages (and options
1425 ;; Reverse as the last loaded language is
1426 ;; the main language.
1431 (replace-regexp-in-string
1432 "AUTO" language options t
)
1435 (string-match-p "\\\\setmainlanguage{.*?}" header
)))
1437 (concat "\\usepackage{polyglossia}\n"
1440 (let ((l (or (assoc l org-latex-polyglossia-language-alist
)
1442 (format (if main-language-set
"\\setotherlanguage%s{%s}\n"
1443 (setq main-language-set t
)
1444 "\\setmainlanguage%s{%s}\n")
1445 (if (and (consp l
) (= (length l
) 3))
1446 (format "[variant=%s]" (nth 2 l
))
1453 (defun org-latex--remove-packages (pkg-alist info
)
1454 "Remove packages based on the current LaTeX compiler.
1456 PKG-ALIST is a list of packages, as in `org-latex-packages-alist'
1457 and `org-latex-default-packages-alist'. If the fourth argument
1458 of a package is neither nil nor a member of the LaTeX compiler
1459 associated to the document, the package is removed.
1461 Return new list of packages."
1462 (let ((compiler (or (plist-get info
:latex-compiler
) "")))
1463 (if (not (member-ignore-case compiler org-latex-compilers
)) pkg-alist
1468 (`(,_
,_
,_
,compilers
) (member-ignore-case compiler compilers
))
1472 (defun org-latex--find-verb-separator (s)
1473 "Return a character not used in string S.
1474 This is used to choose a separator for constructs like \\verb."
1475 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1476 (cl-loop for c across ll
1477 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
1478 return
(char-to-string c
))))
1480 (defun org-latex--make-option-string (options)
1481 "Return a comma separated string of keywords and values.
1482 OPTIONS is an alist where the key is the options keyword as
1483 a string, and the value a list containing the keyword value, or
1485 (mapconcat (lambda (pair)
1486 (pcase-let ((`(,keyword
,value
) pair
))
1488 (and (> (length value
) 0)
1489 (concat "=" value
)))))
1493 (defun org-latex--wrap-label (element output info
)
1494 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1495 INFO is the current export state, as a plist. This function
1496 should not be used for floats. See
1497 `org-latex--caption/label-string'."
1498 (if (not (and (org-string-nw-p output
) (org-element-property :name element
)))
1500 (concat (format "\\phantomsection\n\\label{%s}\n"
1501 (org-latex--label element info
))
1504 (defun org-latex--protect-text (text)
1505 "Protect special characters in string TEXT and return it."
1506 (replace-regexp-in-string "[\\{}$%&_#~^]" "\\\\\\&" text
))
1508 (defun org-latex--text-markup (text markup info
)
1509 "Format TEXT depending on MARKUP text markup.
1510 INFO is a plist used as a communication channel. See
1511 `org-latex-text-markup-alist' for details."
1512 (let ((fmt (cdr (assq markup
(plist-get info
:latex-text-markup-alist
)))))
1514 ;; No format string: Return raw text.
1516 ;; Handle the `verb' special case: Find an appropriate separator
1517 ;; and use "\\verb" command.
1519 (let ((separator (org-latex--find-verb-separator text
)))
1522 (replace-regexp-in-string "\n" " " text
)
1524 ;; Handle the `protectedtexttt' special case: Protect some
1525 ;; special chars and use "\texttt{%s}" format string.
1527 (format "\\texttt{%s}"
1528 (replace-regexp-in-string
1529 "--\\|[\\{}$%&_#~^]"
1531 (cond ((equal m
"--") "-{}-")
1532 ((equal m
"\\") "\\textbackslash{}")
1533 ((equal m
"~") "\\textasciitilde{}")
1534 ((equal m
"^") "\\textasciicircum{}")
1535 (t (org-latex--protect-text m
))))
1537 ;; Else use format string.
1538 (t (format fmt text
)))))
1540 (defun org-latex--delayed-footnotes-definitions (element info
)
1541 "Return footnotes definitions in ELEMENT as a string.
1543 INFO is a plist used as a communication channel.
1545 Footnotes definitions are returned within \"\\footnotetext{}\"
1548 This function is used within constructs that don't support
1549 \"\\footnote{}\" command (e.g., an item tag). In that case,
1550 \"\\footnotemark\" is used within the construct and the function
1551 just outside of it."
1554 (let ((def (org-export-get-footnote-definition ref info
)))
1555 (format "\\footnotetext[%d]{%s%s}"
1556 (org-export-get-footnote-number ref info
)
1557 (org-trim (org-latex--label def info t t
))
1558 (org-trim (org-export-data def info
)))))
1559 ;; Find every footnote reference in ELEMENT.
1560 (letrec ((all-refs nil
)
1563 ;; Return a list of all footnote references never seen
1565 (org-element-map data
'footnote-reference
1567 (when (org-export-footnote-first-reference-p ref info
)
1569 (when (eq (org-element-property :type ref
) 'standard
)
1570 (funcall search-refs
1571 (org-export-get-footnote-definition ref info
)))))
1573 (reverse all-refs
))))
1574 (funcall search-refs element
))
1577 (defun org-latex--translate (s info
)
1578 "Translate string S according to specified language.
1579 INFO is a plist used as a communication channel."
1580 (org-export-translate s
:latex info
))
1582 (defun org-latex--format-spec (info)
1583 "Create a format-spec for document meta-data.
1584 INFO is a plist used as a communication channel."
1585 (let ((language (let ((lang (plist-get info
:language
)))
1586 (or (cdr (assoc-string lang org-latex-babel-language-alist t
))
1587 (nth 1 (assoc-string lang org-latex-polyglossia-language-alist t
))
1589 `((?a .
,(org-export-data (plist-get info
:author
) info
))
1590 (?t .
,(org-export-data (plist-get info
:title
) info
))
1591 (?k .
,(org-export-data (org-latex--wrap-latex-math-block
1592 (plist-get info
:keywords
) info
)
1594 (?d .
,(org-export-data (org-latex--wrap-latex-math-block
1595 (plist-get info
:description
) info
)
1597 (?c .
,(plist-get info
:creator
))
1599 (?L .
,(capitalize language
))
1600 (?D .
,(org-export-get-date info
)))))
1602 (defun org-latex--insert-compiler (info)
1603 "Insert LaTeX_compiler info into the document.
1604 INFO is a plist used as a communication channel."
1605 (let ((compiler (plist-get info
:latex-compiler
)))
1606 (and (org-string-nw-p org-latex-compiler-file-string
)
1607 (member (or compiler
"") org-latex-compilers
)
1608 (format org-latex-compiler-file-string compiler
))))
1613 (defun org-latex-matrices-tree-filter (tree _backend info
)
1614 (org-latex--wrap-latex-matrices tree info
))
1616 (defun org-latex-math-block-tree-filter (tree _backend info
)
1617 (org-latex--wrap-latex-math-block tree info
))
1619 (defun org-latex-math-block-options-filter (info _backend
)
1620 (dolist (prop '(:author
:date
:title
) info
)
1621 (plist-put info prop
1622 (org-latex--wrap-latex-math-block (plist-get info prop
) info
))))
1624 (defun org-latex-clean-invalid-line-breaks (data _backend _info
)
1625 (replace-regexp-in-string
1626 "\\(\\\\end{[A-Za-z0-9*]+}\\|^\\)[ \t]*\\\\\\\\[ \t]*$" "\\1"
1633 (defun org-latex-make-preamble (info &optional template snippet?
)
1634 "Return a formatted LaTeX preamble.
1635 INFO is a plist used as a communication channel. Optional
1636 argument TEMPLATE, when non-nil, is the header template string,
1637 as expected by `org-splice-latex-header'. When SNIPPET? is
1638 non-nil, only includes packages relevant to image generation, as
1639 specified in `org-latex-default-packages-alist' or
1640 `org-latex-packages-alist'."
1641 (let* ((class (plist-get info
:latex-class
))
1644 (let* ((class-options (plist-get info
:latex-class-options
))
1645 (header (nth 1 (assoc class
(plist-get info
:latex-classes
)))))
1646 (and (stringp header
)
1647 (if (not class-options
) header
1648 (replace-regexp-in-string
1649 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1650 class-options header t nil
1))))
1651 (user-error "Unknown LaTeX class `%s'" class
))))
1652 (org-latex-guess-polyglossia-language
1653 (org-latex-guess-babel-language
1654 (org-latex-guess-inputenc
1655 (org-element-normalize-string
1656 (org-splice-latex-header
1658 (org-latex--remove-packages org-latex-default-packages-alist info
)
1659 (org-latex--remove-packages org-latex-packages-alist info
)
1661 (mapconcat #'org-element-normalize-string
1662 (list (plist-get info
:latex-header
)
1664 (plist-get info
:latex-header-extra
)))
1669 (defun org-latex-template (contents info
)
1670 "Return complete document string after LaTeX conversion.
1671 CONTENTS is the transcoded contents string. INFO is a plist
1672 holding export options."
1673 (let ((title (org-export-data (plist-get info
:title
) info
))
1674 (spec (org-latex--format-spec info
)))
1677 (and (plist-get info
:time-stamp-file
)
1678 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1680 (org-latex--insert-compiler info
)
1681 ;; Document class and packages.
1682 (org-latex-make-preamble info
)
1683 ;; Possibly limit depth for headline numbering.
1684 (let ((sec-num (plist-get info
:section-numbers
)))
1685 (when (integerp sec-num
)
1686 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
1688 (let ((author (and (plist-get info
:with-author
)
1689 (let ((auth (plist-get info
:author
)))
1690 (and auth
(org-export-data auth info
)))))
1691 (email (and (plist-get info
:with-email
)
1692 (org-export-data (plist-get info
:email
) info
))))
1693 (cond ((and author email
(not (string= "" email
)))
1694 (format "\\author{%s\\thanks{%s}}\n" author email
))
1695 ((or author email
) (format "\\author{%s}\n" (or author email
)))))
1697 (let ((date (and (plist-get info
:with-date
) (org-export-get-date info
))))
1698 (format "\\date{%s}\n" (org-export-data date info
)))
1699 ;; Title and subtitle.
1700 (let* ((subtitle (plist-get info
:subtitle
))
1703 (format (plist-get info
:latex-subtitle-format
)
1704 (org-export-data subtitle info
))))
1705 (separate (plist-get info
:latex-subtitle-separate
)))
1707 (format "\\title{%s%s}\n" title
1708 (if separate
"" (or formatted-subtitle
"")))
1709 (when (and separate subtitle
)
1710 (concat formatted-subtitle
"\n"))))
1711 ;; Hyperref options.
1712 (let ((template (plist-get info
:latex-hyperref-template
)))
1713 (and (stringp template
)
1714 (format-spec template spec
)))
1716 "\\begin{document}\n\n"
1718 (let* ((title-command (plist-get info
:latex-title-command
))
1719 (command (and (stringp title-command
)
1720 (format-spec title-command spec
))))
1721 (org-element-normalize-string
1722 (cond ((not (plist-get info
:with-title
)) nil
)
1723 ((string= "" title
) nil
)
1724 ((not (stringp command
)) nil
)
1725 ((string-match "\\(?:[^%]\\|^\\)%s" command
)
1726 (format command title
))
1728 ;; Table of contents.
1729 (let ((depth (plist-get info
:with-toc
)))
1731 (concat (when (integerp depth
)
1732 (format "\\setcounter{tocdepth}{%d}\n" depth
))
1733 (plist-get info
:latex-toc-command
))))
1737 (and (plist-get info
:with-creator
)
1738 (concat (plist-get info
:creator
) "\n"))
1740 "\\end{document}")))
1744 ;;; Transcode Functions
1748 (defun org-latex-bold (_bold contents info
)
1749 "Transcode BOLD from Org to LaTeX.
1750 CONTENTS is the text with bold markup. INFO is a plist holding
1751 contextual information."
1752 (org-latex--text-markup contents
'bold info
))
1757 (defun org-latex-center-block (center-block contents info
)
1758 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1759 CONTENTS holds the contents of the center block. INFO is a plist
1760 holding contextual information."
1761 (org-latex--wrap-label
1762 center-block
(format "\\begin{center}\n%s\\end{center}" contents
) info
))
1767 (defun org-latex-clock (clock _contents info
)
1768 "Transcode a CLOCK element from Org to LaTeX.
1769 CONTENTS is nil. INFO is a plist holding contextual
1773 (format "\\textbf{%s} " org-clock-string
)
1774 (format (plist-get info
:latex-inactive-timestamp-format
)
1775 (concat (org-timestamp-translate (org-element-property :value clock
))
1776 (let ((time (org-element-property :duration clock
)))
1777 (and time
(format " (%s)" time
)))))
1783 (defun org-latex-code (code _contents info
)
1784 "Transcode a CODE object from Org to LaTeX.
1785 CONTENTS is nil. INFO is a plist used as a communication
1787 (org-latex--text-markup (org-element-property :value code
) 'code info
))
1792 (defun org-latex-drawer (drawer contents info
)
1793 "Transcode a DRAWER element from Org to LaTeX.
1794 CONTENTS holds the contents of the block. INFO is a plist
1795 holding contextual information."
1796 (let* ((name (org-element-property :drawer-name drawer
))
1797 (output (funcall (plist-get info
:latex-format-drawer-function
)
1799 (org-latex--wrap-label drawer output info
)))
1804 (defun org-latex-dynamic-block (dynamic-block contents info
)
1805 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1806 CONTENTS holds the contents of the block. INFO is a plist
1807 holding contextual information. See `org-export-data'."
1808 (org-latex--wrap-label dynamic-block contents info
))
1813 (defun org-latex-entity (entity _contents _info
)
1814 "Transcode an ENTITY object from Org to LaTeX.
1815 CONTENTS are the definition itself. INFO is a plist holding
1816 contextual information."
1817 (org-element-property :latex entity
))
1822 (defun org-latex-example-block (example-block _contents info
)
1823 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1824 CONTENTS is nil. INFO is a plist holding contextual
1826 (when (org-string-nw-p (org-element-property :value example-block
))
1827 (let ((environment (or (org-export-read-attribute
1828 :attr_latex example-block
:environment
)
1830 (org-latex--wrap-label
1832 (format "\\begin{%s}\n%s\\end{%s}"
1834 (org-export-format-code-default example-block info
)
1841 (defun org-latex-export-block (export-block _contents _info
)
1842 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1843 CONTENTS is nil. INFO is a plist holding contextual information."
1844 (when (member (org-element-property :type export-block
) '("LATEX" "TEX"))
1845 (org-remove-indentation (org-element-property :value export-block
))))
1850 (defun org-latex-export-snippet (export-snippet _contents _info
)
1851 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1852 CONTENTS is nil. INFO is a plist holding contextual information."
1853 (when (eq (org-export-snippet-backend export-snippet
) 'latex
)
1854 (org-element-property :value export-snippet
)))
1859 (defun org-latex-fixed-width (fixed-width _contents info
)
1860 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1861 CONTENTS is nil. INFO is a plist holding contextual information."
1862 (org-latex--wrap-label
1864 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
1865 (org-remove-indentation
1866 (org-element-property :value fixed-width
)))
1870 ;;;; Footnote Reference
1872 (defun org-latex-footnote-reference (footnote-reference _contents info
)
1873 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1874 CONTENTS is nil. INFO is a plist holding contextual information."
1875 (let ((label (org-element-property :label footnote-reference
)))
1877 ;; Insert separator between two footnotes in a row.
1878 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1879 (when (eq (org-element-type prev
) 'footnote-reference
)
1880 (plist-get info
:latex-footnote-separator
)))
1882 ;; Use `:latex-footnote-defined-format' if the footnote has
1883 ;; already been defined.
1884 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1885 (format (plist-get info
:latex-footnote-defined-format
)
1887 (org-export-get-footnote-definition footnote-reference info
)
1889 ;; Use \footnotemark if reference is within another footnote
1890 ;; reference, footnote definition, table cell or item's tag.
1891 ((or (org-element-lineage footnote-reference
1892 '(footnote-reference footnote-definition
1894 (eq 'item
(org-element-type
1895 (org-export-get-parent-element footnote-reference
))))
1897 ;; Otherwise, define it with \footnote command.
1899 (let ((def (org-export-get-footnote-definition footnote-reference info
)))
1901 (format "\\footnote{%s%s}" (org-trim (org-export-data def info
))
1902 ;; Only insert a \label if there exist another
1903 ;; reference to def.
1904 (cond ((not label
) "")
1905 ((org-element-map (plist-get info
:parse-tree
) 'footnote-reference
1907 (and (not (eq f footnote-reference
))
1908 (equal (org-element-property :label f
) label
)
1909 (org-trim (org-latex--label def info t t
))))
1912 ;; Retrieve all footnote references within the footnote and
1913 ;; add their definition after it, since LaTeX doesn't support
1915 (org-latex--delayed-footnotes-definitions def info
))))))))
1920 (defun org-latex-headline (headline contents info
)
1921 "Transcode a HEADLINE element from Org to LaTeX.
1922 CONTENTS holds the contents of the headline. INFO is a plist
1923 holding contextual information."
1924 (unless (org-element-property :footnote-section-p headline
)
1925 (let* ((class (plist-get info
:latex-class
))
1926 (level (org-export-get-relative-level headline info
))
1927 (numberedp (org-export-numbered-headline-p headline info
))
1928 (class-sectioning (assoc class
(plist-get info
:latex-classes
)))
1929 ;; Section formatting will set two placeholders: one for
1930 ;; the title and the other for the contents.
1932 (let ((sec (if (functionp (nth 2 class-sectioning
))
1933 (funcall (nth 2 class-sectioning
) level numberedp
)
1934 (nth (1+ level
) class-sectioning
))))
1936 ;; No section available for that LEVEL.
1938 ;; Section format directly returned by a function. Add
1939 ;; placeholder for contents.
1940 ((stringp sec
) (concat sec
"\n%s"))
1941 ;; (numbered-section . unnumbered-section)
1942 ((not (consp (cdr sec
)))
1943 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
1944 ;; (numbered-open numbered-close)
1946 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
1947 ;; (num-in num-out no-num-in no-num-out)
1949 (if numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))
1950 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
1951 ;; Create a temporary export back-end that hard-codes
1952 ;; "\underline" within "\section" and alike.
1954 (org-export-create-backend
1957 '((underline .
(lambda (o c i
) (format "\\underline{%s}" c
))))))
1959 (org-export-data-with-backend
1960 (org-element-property :title headline
) section-back-end info
))
1962 (and (plist-get info
:with-todo-keywords
)
1963 (let ((todo (org-element-property :todo-keyword headline
)))
1964 (and todo
(org-export-data todo info
)))))
1965 (todo-type (and todo
(org-element-property :todo-type headline
)))
1966 (tags (and (plist-get info
:with-tags
)
1967 (org-export-get-tags headline info
)))
1968 (priority (and (plist-get info
:with-priority
)
1969 (org-element-property :priority headline
)))
1970 ;; Create the headline text along with a no-tag version.
1971 ;; The latter is required to remove tags from toc.
1972 (full-text (funcall (plist-get info
:latex-format-headline-function
)
1973 todo todo-type priority text tags info
))
1974 ;; Associate \label to the headline for internal links.
1975 (headline-label (org-latex--label headline info t t
))
1977 (make-string (org-element-property :pre-blank headline
) ?
\n)))
1978 (if (or (not section-fmt
) (org-export-low-level-p headline info
))
1979 ;; This is a deep sub-tree: export it as a list item. Also
1980 ;; export as items headlines for which no section format has
1982 (let ((low-level-body
1984 ;; If headline is the first sibling, start a list.
1985 (when (org-export-first-sibling-p headline info
)
1986 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
1990 (string-match-p "\\`[ \t]*\\[" full-text
)
1996 ;; If headline is not the last sibling simply return
1997 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1999 (if (not (org-export-last-sibling-p headline info
)) low-level-body
2000 (replace-regexp-in-string
2002 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
2004 ;; This is a standard headline. Export it as a section. Add
2005 ;; an alternative heading when possible, and when this is not
2006 ;; identical to the usual heading.
2008 (funcall (plist-get info
:latex-format-headline-function
)
2009 todo todo-type priority
2010 (org-export-data-with-backend
2011 (org-export-get-alt-title headline info
)
2012 section-back-end info
)
2013 (and (eq (plist-get info
:with-tags
) t
) tags
)
2015 ;; Maybe end local TOC (see `org-latex-keyword').
2019 (let ((case-fold-search t
)
2021 (let ((first (car (org-element-contents headline
))))
2022 (and (eq (org-element-type first
) 'section
) first
))))
2023 (org-element-map section
'keyword
2025 (and (equal (org-element-property :key k
) "TOC")
2026 (let ((v (org-element-property :value k
)))
2027 (and (string-match-p "\\<headlines\\>" v
)
2028 (string-match-p "\\<local\\>" v
)
2029 (format "\\stopcontents[level-%d]" level
)))))
2032 (not (equal opt-title full-text
))
2033 (string-match "\\`\\\\\\(.+?\\){" section-fmt
))
2034 (format (replace-match "\\1[%s]" nil nil section-fmt
1)
2035 ;; Replace square brackets with parenthesis
2036 ;; since square brackets are not supported in
2037 ;; optional arguments.
2038 (replace-regexp-in-string
2039 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title
))
2041 (concat headline-label pre-blanks contents
))
2042 ;; Impossible to add an alternative heading. Fallback to
2043 ;; regular sectioning format string.
2044 (format section-fmt full-text
2045 (concat headline-label pre-blanks contents
))))))))
2047 (defun org-latex-format-headline-default-function
2048 (todo _todo-type priority text tags _info
)
2049 "Default format function for a headline.
2050 See `org-latex-format-headline-function' for details."
2052 (and todo
(format "{\\bfseries\\sffamily %s} " todo
))
2053 (and priority
(format "\\framebox{\\#%c} " priority
))
2056 (format "\\hfill{}\\textsc{%s}"
2057 (mapconcat #'org-latex--protect-text tags
":")))))
2060 ;;;; Horizontal Rule
2062 (defun org-latex-horizontal-rule (horizontal-rule _contents info
)
2063 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
2064 CONTENTS is nil. INFO is a plist holding contextual information."
2065 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule
))
2066 (prev (org-export-get-previous-element horizontal-rule info
)))
2068 ;; Make sure the rule doesn't start at the end of the current
2069 ;; line by separating it with a blank line from previous element.
2071 (let ((prev-blank (org-element-property :post-blank prev
)))
2072 (or (not prev-blank
) (zerop prev-blank
))))
2074 (org-latex--wrap-label
2076 (format "\\noindent\\rule{%s}{%s}"
2077 (or (plist-get attr
:width
) "\\textwidth")
2078 (or (plist-get attr
:thickness
) "0.5pt"))
2082 ;;;; Inline Src Block
2084 (defun org-latex-inline-src-block (inline-src-block _contents info
)
2085 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
2086 CONTENTS holds the contents of the item. INFO is a plist holding
2087 contextual information."
2088 (let* ((code (org-element-property :value inline-src-block
))
2089 (separator (org-latex--find-verb-separator code
)))
2090 (cl-case (plist-get info
:latex-listings
)
2091 ;; Do not use a special package: transcode it verbatim.
2092 ((nil) (format "\\texttt{%s}" (org-latex--text-markup code
'code info
)))
2093 ;; Use minted package.
2095 (let* ((org-lang (org-element-property :language inline-src-block
))
2096 (mint-lang (or (cadr (assq (intern org-lang
)
2097 (plist-get info
:latex-minted-langs
)))
2098 (downcase org-lang
)))
2099 (options (org-latex--make-option-string
2100 (plist-get info
:latex-minted-options
))))
2101 (format "\\mintinline%s{%s}{%s}"
2102 (if (string= options
"") "" (format "[%s]" options
))
2105 ;; Use listings package.
2107 ;; Maybe translate language's name.
2108 (let* ((org-lang (org-element-property :language inline-src-block
))
2109 (lst-lang (or (cadr (assq (intern org-lang
)
2110 (plist-get info
:latex-listings-langs
)))
2112 (options (org-latex--make-option-string
2113 (append (plist-get info
:latex-listings-options
)
2114 `(("language" ,lst-lang
))))))
2115 (concat (format "\\lstinline[%s]" options
)
2116 separator code separator
))))))
2121 (defun org-latex-inlinetask (inlinetask contents info
)
2122 "Transcode an INLINETASK element from Org to LaTeX.
2123 CONTENTS holds the contents of the block. INFO is a plist
2124 holding contextual information."
2125 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
2126 (todo (and (plist-get info
:with-todo-keywords
)
2127 (let ((todo (org-element-property :todo-keyword inlinetask
)))
2128 (and todo
(org-export-data todo info
)))))
2129 (todo-type (org-element-property :todo-type inlinetask
))
2130 (tags (and (plist-get info
:with-tags
)
2131 (org-export-get-tags inlinetask info
)))
2132 (priority (and (plist-get info
:with-priority
)
2133 (org-element-property :priority inlinetask
)))
2134 (contents (concat (org-latex--label inlinetask info
) contents
)))
2135 (funcall (plist-get info
:latex-format-inlinetask-function
)
2136 todo todo-type priority title tags contents info
)))
2138 (defun org-latex-format-inlinetask-default-function
2139 (todo _todo-type priority title tags contents _info
)
2140 "Default format function for inlinetasks.
2141 See `org-latex-format-inlinetask-function' for details."
2143 (concat (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
2144 (when priority
(format "\\framebox{\\#%c} " priority
))
2147 (format "\\hfill{}\\textsc{%s}"
2148 (org-make-tag-string
2149 (mapcar #'org-latex--protect-text tags
)))))))
2150 (concat "\\begin{center}\n"
2152 "\\begin{minipage}[c]{.6\\textwidth}\n"
2154 (and (org-string-nw-p contents
)
2155 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents
))
2163 (defun org-latex-italic (_italic contents info
)
2164 "Transcode ITALIC from Org to LaTeX.
2165 CONTENTS is the text with italic markup. INFO is a plist holding
2166 contextual information."
2167 (org-latex--text-markup contents
'italic info
))
2172 (defun org-latex-item (item contents info
)
2173 "Transcode an ITEM element from Org to LaTeX.
2174 CONTENTS holds the contents of the item. INFO is a plist holding
2175 contextual information."
2177 (let ((count (org-element-property :counter item
))
2179 ;; Determine level of current item to determine the
2180 ;; correct LaTeX counter to use (enumi, enumii...).
2181 (let ((parent item
) (level 0))
2182 (while (memq (org-element-type
2183 (setq parent
(org-export-get-parent parent
)))
2185 (when (and (eq (org-element-type parent
) 'plain-list
)
2186 (eq (org-element-property :type parent
)
2192 (format "\\setcounter{enum%s}{%s}\n"
2193 (nth (1- level
) '("i" "ii" "iii" "iv"))
2195 (checkbox (cl-case (org-element-property :checkbox item
)
2198 (trans "$\\boxminus$")))
2199 (tag (let ((tag (org-element-property :tag item
)))
2200 (and tag
(org-export-data tag info
))))
2201 ;; If there are footnotes references in tag, be sure to add
2202 ;; their definition at the end of the item. This workaround
2203 ;; is necessary since "\footnote{}" command is not supported
2206 (or (and tag
(org-latex--delayed-footnotes-definitions
2207 (org-element-property :tag item
) info
))
2213 (format "[{%s %s}] %s" checkbox tag tag-footnotes
))
2215 (format "[{%s}] %s" (or checkbox tag
) tag-footnotes
))
2216 ;; Without a tag or a check-box, if CONTENTS starts with
2217 ;; an opening square bracket, add "\relax" to "\item",
2218 ;; unless the brackets comes from an initial export
2219 ;; snippet (i.e. it is inserted willingly by the user).
2221 (string-match-p "\\`[ \t]*\\[" contents
)
2222 (not (let ((e (car (org-element-contents item
))))
2223 (and (eq (org-element-type e
) 'paragraph
)
2224 (let ((o (car (org-element-contents e
))))
2225 (and (eq (org-element-type o
) 'export-snippet
)
2226 (eq (org-export-snippet-backend o
)
2230 (and contents
(org-trim contents
)))))
2235 (defun org-latex-keyword (keyword _contents info
)
2236 "Transcode a KEYWORD element from Org to LaTeX.
2237 CONTENTS is nil. INFO is a plist holding contextual information."
2238 (let ((key (org-element-property :key keyword
))
2239 (value (org-element-property :value keyword
)))
2241 ((string= key
"LATEX") value
)
2242 ((string= key
"INDEX") (format "\\index{%s}" value
))
2243 ((string= key
"TOC")
2244 (let ((case-fold-search t
))
2246 ((string-match-p "\\<headlines\\>" value
)
2247 (let* ((localp (string-match-p "\\<local\\>" value
))
2248 (parent (org-element-lineage keyword
'(headline)))
2249 (level (if (not (and localp parent
)) 0
2250 (org-export-get-relative-level parent info
)))
2252 (and (string-match "\\<[0-9]+\\>" value
)
2254 "\\setcounter{tocdepth}{%d}"
2255 (+ (string-to-number (match-string 0 value
)) level
)))))
2256 (if (and localp parent
)
2257 ;; Start local TOC, assuming package "titletoc" is
2259 (format "\\startcontents[level-%d]
2260 \\printcontents[level-%d]{}{0}{%s}"
2261 level level
(or depth
""))
2262 (concat depth
(and depth
"\n") "\\tableofcontents"))))
2263 ((string-match-p "\\<tables\\>" value
) "\\listoftables")
2264 ((string-match-p "\\<listings\\>" value
)
2265 (cl-case (plist-get info
:latex-listings
)
2266 ((nil) "\\listoffigures")
2267 (minted "\\listoflistings")
2268 (otherwise "\\lstlistoflistings")))))))))
2271 ;;;; Latex Environment
2273 (defun org-latex--environment-type (latex-environment)
2274 "Return the TYPE of LATEX-ENVIRONMENT.
2276 The TYPE is determined from the actual latex environment, and
2277 could be a member of `org-latex-caption-above' or `math'."
2278 (let* ((latex-begin-re "\\\\begin{\\([A-Za-z0-9*]+\\)}")
2279 (value (org-remove-indentation
2280 (org-element-property :value latex-environment
)))
2281 (env (or (and (string-match latex-begin-re value
)
2282 (match-string 1 value
))
2285 ((string-match-p org-latex-math-environments-re value
) 'math
)
2288 (regexp-opt '("table" "longtable" "tabular" "tabu" "longtabu")))
2291 ((string-match-p "figure" env
) 'image
)
2294 (regexp-opt '("lstlisting" "listing" "verbatim" "minted")))
2297 (t 'special-block
))))
2299 (defun org-latex-latex-environment (latex-environment _contents info
)
2300 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
2301 CONTENTS is nil. INFO is a plist holding contextual information."
2302 (when (plist-get info
:with-latex
)
2303 (let* ((value (org-remove-indentation
2304 (org-element-property :value latex-environment
)))
2305 (type (org-latex--environment-type latex-environment
))
2306 (caption (if (eq type
'math
)
2307 (org-latex--label latex-environment info nil t
)
2308 (org-latex--caption/label-string latex-environment info
)))
2310 (memq type
(append (plist-get info
:latex-caption-above
) '(math)))))
2311 (if (not (or (org-element-property :name latex-environment
)
2312 (org-element-property :caption latex-environment
)))
2314 ;; Environment is labeled: label must be within the environment
2315 ;; (otherwise, a reference pointing to that element will count
2316 ;; the section instead). Also insert caption if `latex-environment'
2317 ;; is not a math environment.
2322 (goto-char (point-min))
2324 (goto-char (point-max))
2327 (buffer-string))))))
2331 (defun org-latex-latex-fragment (latex-fragment _contents _info
)
2332 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
2333 CONTENTS is nil. INFO is a plist holding contextual information."
2334 (let ((value (org-element-property :value latex-fragment
)))
2335 ;; Trim math markers since the fragment is enclosed within
2336 ;; a latex-math-block object anyway.
2337 (cond ((string-match-p "\\`\\$[^$]" value
) (substring value
1 -
1))
2338 ((string-prefix-p "\\(" value
) (substring value
2 -
2))
2344 (defun org-latex-line-break (_line-break _contents _info
)
2345 "Transcode a LINE-BREAK object from Org to LaTeX.
2346 CONTENTS is nil. INFO is a plist holding contextual information."
2352 (defun org-latex-image-link-filter (data _backend info
)
2353 (org-export-insert-image-links data info org-latex-inline-image-rules
))
2355 (defun org-latex--inline-image (link info
)
2356 "Return LaTeX code for an inline image.
2357 LINK is the link pointing to the inline image. INFO is a plist
2358 used as a communication channel."
2359 (let* ((parent (org-export-get-parent-element link
))
2360 (path (let ((raw-path (org-element-property :path link
)))
2361 (if (not (file-name-absolute-p raw-path
)) raw-path
2362 (expand-file-name raw-path
))))
2363 (filetype (file-name-extension path
))
2364 (caption (org-latex--caption/label-string parent info
))
2365 (caption-above-p (org-latex--caption-above-p link info
))
2366 ;; Retrieve latex attributes from the element around.
2367 (attr (org-export-read-attribute :attr_latex parent
))
2368 (float (let ((float (plist-get attr
:float
)))
2369 (cond ((string= float
"wrap") 'wrap
)
2370 ((string= float
"sideways") 'sideways
)
2371 ((string= float
"multicolumn") 'multicolumn
)
2372 ((and (plist-member attr
:float
) (not float
)) 'nonfloat
)
2374 (org-element-property :caption parent
)
2375 (org-string-nw-p (plist-get attr
:caption
)))
2379 (let ((place (plist-get attr
:placement
)))
2381 (place (format "%s" place
))
2382 ((eq float
'wrap
) "{l}{0.5\\textwidth}")
2384 (format "[%s]" (plist-get info
:latex-default-figure-position
)))
2387 (if (plist-member attr
:center
) (plist-get attr
:center
)
2388 (plist-get info
:latex-images-centered
)))
2389 (comment-include (if (plist-get attr
:comment-include
) "%" ""))
2390 ;; It is possible to specify scale or width and height in
2391 ;; the ATTR_LATEX line, and also via default variables.
2392 (scale (cond ((eq float
'wrap
) "")
2393 ((plist-get attr
:scale
))
2394 (t (plist-get info
:latex-image-default-scale
))))
2395 (width (cond ((org-string-nw-p scale
) "")
2396 ((plist-get attr
:width
))
2397 ((plist-get attr
:height
) "")
2398 ((eq float
'wrap
) "0.48\\textwidth")
2399 (t (plist-get info
:latex-image-default-width
))))
2400 (height (cond ((org-string-nw-p scale
) "")
2401 ((plist-get attr
:height
))
2402 ((or (plist-get attr
:width
)
2403 (memq float
'(figure wrap
))) "")
2404 (t (plist-get info
:latex-image-default-height
))))
2405 (options (let ((opt (or (plist-get attr
:options
)
2406 (plist-get info
:latex-image-default-option
))))
2407 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt
)) opt
2408 (match-string 1 opt
))))
2410 (if (member filetype
'("tikz" "pgf"))
2412 ;; - use \input to read in image file.
2413 ;; - if options are present, wrap in a tikzpicture environment.
2414 ;; - if width or height are present, use \resizebox to change
2417 (setq image-code
(format "\\input{%s}" path
))
2418 (when (org-string-nw-p options
)
2420 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
2424 (cond ((org-string-nw-p scale
)
2425 (format "\\scalebox{%s}{%s}" scale image-code
))
2426 ((or (org-string-nw-p width
) (org-string-nw-p height
))
2427 (format "\\resizebox{%s}{%s}{%s}"
2428 (if (org-string-nw-p width
) width
"!")
2429 (if (org-string-nw-p height
) height
"!")
2431 ;; For other images:
2432 ;; - add scale, or width and height to options.
2433 ;; - include the image with \includegraphics.
2434 (if (org-string-nw-p scale
)
2435 (setq options
(concat options
",scale=" scale
))
2436 (when (org-string-nw-p width
) (setq options
(concat options
",width=" width
)))
2437 (when (org-string-nw-p height
) (setq options
(concat options
",height=" height
))))
2438 (let ((search-option (org-element-property :search-option link
)))
2439 (when (and search-option
2440 (equal filetype
"pdf")
2441 (string-match-p "\\`[0-9]+\\'" search-option
)
2442 (not (string-match-p "page=" options
)))
2443 (setq options
(concat options
",page=" search-option
))))
2445 (format "\\includegraphics%s{%s}"
2446 (cond ((not (org-string-nw-p options
)) "")
2447 ((string-prefix-p "," options
)
2448 (format "[%s]" (substring options
1)))
2449 (t (format "[%s]" options
)))
2451 (when (equal filetype
"svg")
2452 (setq image-code
(replace-regexp-in-string "^\\\\includegraphics"
2456 (setq image-code
(replace-regexp-in-string "\\.svg}"
2460 ;; Return proper string, depending on FLOAT.
2462 (`wrap
(format "\\begin{wrapfigure}%s
2465 %s\\end{wrapfigure}"
2467 (if caption-above-p caption
"")
2468 (if center
"\\centering" "")
2469 comment-include image-code
2470 (if caption-above-p
"" caption
)))
2471 (`sideways
(format "\\begin{sidewaysfigure}
2474 %s\\end{sidewaysfigure}"
2475 (if caption-above-p caption
"")
2476 (if center
"\\centering" "")
2477 comment-include image-code
2478 (if caption-above-p
"" caption
)))
2479 (`multicolumn
(format "\\begin{figure*}%s
2484 (if caption-above-p caption
"")
2485 (if center
"\\centering" "")
2486 comment-include image-code
2487 (if caption-above-p
"" caption
)))
2488 (`figure
(format "\\begin{figure}%s
2493 (if caption-above-p caption
"")
2494 (if center
"\\centering" "")
2495 comment-include image-code
2496 (if caption-above-p
"" caption
)))
2498 (format "\\begin{center}
2501 (if caption-above-p caption
"")
2503 (if caption-above-p
"" caption
)))
2505 (concat (if caption-above-p caption
"")
2507 (if caption-above-p caption
""))))))
2509 (defun org-latex-link (link desc info
)
2510 "Transcode a LINK object from Org to LaTeX.
2512 DESC is the description part of the link, or the empty string.
2513 INFO is a plist holding contextual information. See
2515 (let* ((type (org-element-property :type link
))
2516 (raw-path (org-element-property :path link
))
2517 ;; Ensure DESC really exists, or set it to nil.
2518 (desc (and (not (string= desc
"")) desc
))
2519 (imagep (org-export-inline-image-p
2520 link
(plist-get info
:latex-inline-image-rules
)))
2521 (path (org-latex--protect-text
2522 (cond ((member type
'("http" "https" "ftp" "mailto" "doi"))
2523 (concat type
":" raw-path
))
2524 ((string= type
"file")
2525 (org-export-file-uri raw-path
))
2529 ;; Link type is handled by a special function.
2530 ((org-export-custom-protocol-maybe link desc
'latex
))
2532 (imagep (org-latex--inline-image link info
))
2533 ;; Radio link: Transcode target's contents and use them as link's
2535 ((string= type
"radio")
2536 (let ((destination (org-export-resolve-radio-link link info
)))
2537 (if (not destination
) desc
2538 (format "\\hyperref[%s]{%s}"
2539 (org-export-get-reference destination info
)
2541 ;; Links pointing to a headline: Find destination and build
2542 ;; appropriate referencing command.
2543 ((member type
'("custom-id" "fuzzy" "id"))
2545 (if (string= type
"fuzzy")
2546 (org-export-resolve-fuzzy-link link info
'latex-matrices
)
2547 (org-export-resolve-id-link link info
))))
2548 (cl-case (org-element-type destination
)
2549 ;; Id link points to an external file.
2551 (if desc
(format "\\href{%s}{%s}" destination desc
)
2552 (format "\\url{%s}" destination
)))
2553 ;; Fuzzy link points nowhere.
2555 (format (plist-get info
:latex-link-with-unknown-path-format
)
2558 (org-element-property :raw-link link
) info
))))
2559 ;; LINK points to a headline. If headlines are numbered
2560 ;; and the link has no description, display headline's
2561 ;; number. Otherwise, display description or headline's
2564 (let ((label (org-latex--label destination info t
)))
2566 (org-export-numbered-headline-p destination info
))
2567 (format "\\ref{%s}" label
)
2568 (format "\\hyperref[%s]{%s}" label
2571 (org-element-property :title destination
) info
))))))
2572 ;; Fuzzy link points to a target. Do as above.
2574 (let ((ref (org-latex--label destination info t
)))
2575 (if (not desc
) (format "\\ref{%s}" ref
)
2576 (format "\\hyperref[%s]{%s}" ref desc
)))))))
2577 ;; Coderef: replace link with the reference name or the
2578 ;; equivalent line number.
2579 ((string= type
"coderef")
2580 (format (org-export-get-coderef-format path desc
)
2581 (org-export-resolve-coderef path info
)))
2582 ;; External link with a description part.
2583 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
2584 ;; External link without a description part.
2585 (path (format "\\url{%s}" path
))
2586 ;; No path, only description. Try to do something useful.
2587 (t (format (plist-get info
:latex-link-with-unknown-path-format
) desc
)))))
2592 (defun org-latex-node-property (node-property _contents _info
)
2593 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2594 CONTENTS is nil. INFO is a plist holding contextual
2597 (org-element-property :key node-property
)
2598 (let ((value (org-element-property :value node-property
)))
2599 (if value
(concat " " value
) ""))))
2604 (defun org-latex-paragraph (_paragraph contents _info
)
2605 "Transcode a PARAGRAPH element from Org to LaTeX.
2606 CONTENTS is the contents of the paragraph, as a string. INFO is
2607 the plist used as a communication channel."
2613 (defun org-latex-plain-list (plain-list contents info
)
2614 "Transcode a PLAIN-LIST element from Org to LaTeX.
2615 CONTENTS is the contents of the list. INFO is a plist holding
2616 contextual information."
2617 (let* ((type (org-element-property :type plain-list
))
2618 (attr (org-export-read-attribute :attr_latex plain-list
))
2619 (latex-type (let ((env (plist-get attr
:environment
)))
2620 (cond (env (format "%s" env
))
2621 ((eq type
'ordered
) "enumerate")
2622 ((eq type
'descriptive
) "description")
2624 (org-latex--wrap-label
2626 (format "\\begin{%s}%s\n%s\\end{%s}"
2628 (or (plist-get attr
:options
) "")
2636 (defun org-latex-plain-text (text info
)
2637 "Transcode a TEXT string from Org to LaTeX.
2638 TEXT is the string to transcode. INFO is a plist holding
2639 contextual information."
2640 (let* ((specialp (plist-get info
:with-special-strings
))
2642 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2643 (let ((case-fold-search nil
))
2644 (replace-regexp-in-string
2645 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2646 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2647 ;; However, if special strings are used, be careful not
2648 ;; to protect "\" in "\-" constructs.
2649 (replace-regexp-in-string
2650 (concat "[%$#&{}_~^]\\|\\\\" (and specialp
"\\([^-]\\|$\\)"))
2652 (cl-case (string-to-char m
)
2653 (?
\\ "$\\\\backslash$\\1")
2654 (?~
"\\\\textasciitilde{}")
2658 ;; Activate smart quotes. Be sure to provide original TEXT string
2659 ;; since OUTPUT may have been modified.
2660 (when (plist-get info
:with-smart-quotes
)
2661 (setq output
(org-export-activate-smart-quotes output
:latex info text
)))
2662 ;; Convert special strings.
2664 (setq output
(replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output
)))
2665 ;; Handle break preservation if required.
2666 (when (plist-get info
:preserve-breaks
)
2667 (setq output
(replace-regexp-in-string
2668 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t
)))
2675 (defun org-latex-planning (planning _contents info
)
2676 "Transcode a PLANNING element from Org to LaTeX.
2677 CONTENTS is nil. INFO is a plist holding contextual
2685 (let ((closed (org-element-property :closed planning
)))
2688 (format "\\textbf{%s} " org-closed-string
)
2689 (format (plist-get info
:latex-inactive-timestamp-format
)
2690 (org-timestamp-translate closed
)))))
2691 (let ((deadline (org-element-property :deadline planning
)))
2694 (format "\\textbf{%s} " org-deadline-string
)
2695 (format (plist-get info
:latex-active-timestamp-format
)
2696 (org-timestamp-translate deadline
)))))
2697 (let ((scheduled (org-element-property :scheduled planning
)))
2700 (format "\\textbf{%s} " org-scheduled-string
)
2701 (format (plist-get info
:latex-active-timestamp-format
)
2702 (org-timestamp-translate scheduled
)))))))
2707 ;;;; Property Drawer
2709 (defun org-latex-property-drawer (_property-drawer contents _info
)
2710 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2711 CONTENTS holds the contents of the drawer. INFO is a plist
2712 holding contextual information."
2713 (and (org-string-nw-p contents
)
2714 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents
)))
2717 ;;;; Pseudo Element: LaTeX Matrices
2719 ;; `latex-matrices' elements have the following properties:
2720 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2723 (defun org-latex--wrap-latex-matrices (data info
)
2724 "Merge contiguous tables with the same mode within a pseudo-element.
2725 DATA is a parse tree or a secondary string. INFO is a plist
2726 containing export options. Modify DATA by side-effect and return
2728 (org-element-map data
'table
2730 (when (eq (org-element-property :type table
) 'org
)
2731 (let ((mode (or (org-export-read-attribute :attr_latex table
:mode
)
2732 (plist-get info
:latex-default-table-mode
))))
2733 (when (and (member mode
'("inline-math" "math"))
2734 ;; Do not wrap twice the same table.
2735 (not (eq (org-element-type
2736 (org-element-property :parent table
))
2738 (let* ((caption (and (not (string= mode
"inline-math"))
2739 (org-element-property :caption table
)))
2740 (name (and (not (string= mode
"inline-math"))
2741 (org-element-property :name table
)))
2743 (list 'latex-matrices
2744 ;; Inherit name from the first table.
2746 ;; FIXME: what syntax for captions?
2750 (cond ((string= mode
"inline-math") 'inline
)
2751 ((or caption name
) 'equation
)
2754 (next (org-export-get-next-element table info
)))
2755 (org-element-insert-before matrices table
)
2756 ;; Swallow all contiguous tables sharing the same mode.
2758 (zerop (or (org-element-property :post-blank previous
) 0))
2759 (setq next
(org-export-get-next-element previous info
))
2760 (eq (org-element-type next
) 'table
)
2761 (eq (org-element-property :type next
) 'org
)
2762 (string= (or (org-export-read-attribute
2763 :attr_latex next
:mode
)
2764 (plist-get info
:latex-default-table-mode
))
2766 (org-element-put-property table
:name nil
)
2767 (org-element-put-property table
:caption nil
)
2768 (org-element-extract-element previous
)
2769 (org-element-adopt-elements matrices previous
)
2770 (setq previous next
))
2771 ;; Inherit `:post-blank' from the value of the last
2772 ;; swallowed table. Set the latter's `:post-blank'
2773 ;; value to 0 so as to not duplicate empty lines.
2774 (org-element-put-property
2775 matrices
:post-blank
(org-element-property :post-blank previous
))
2776 (org-element-put-property previous
:post-blank
0)
2777 (org-element-put-property table
:name nil
)
2778 (org-element-put-property table
:caption nil
)
2779 (org-element-extract-element previous
)
2780 (org-element-adopt-elements matrices previous
))))))
2784 (defun org-latex-matrices (matrices contents info
)
2785 "Transcode a MATRICES element from Org to LaTeX.
2786 CONTENTS is a string. INFO is a plist used as a communication
2788 (pcase (org-element-property :markup matrices
)
2789 (`inline
(format "\\(%s\\)" contents
))
2791 (let ((caption (org-latex--caption/label-string matrices info
))
2792 (caption-above?
(org-latex--caption-above-p matrices info
)))
2793 (concat "\\begin{equation}\n"
2794 (and caption-above? caption
)
2796 (and (not caption-above?
) caption
)
2797 "\\end{equation}")))
2799 (format "\\[\n%s\\]" contents
))))
2802 ;;;; Pseudo Object: LaTeX Math Block
2804 ;; `latex-math-block' objects have the following property:
2807 (defun org-latex--wrap-latex-math-block (data info
)
2808 "Merge contiguous math objects in a pseudo-object container.
2809 DATA is a parse tree or a secondary string. INFO is a plist
2810 containing export options. Modify DATA by side-effect and return it."
2811 (let ((valid-object-p
2812 ;; Non-nil when OBJECT can be added to a latex math block.
2814 (pcase (org-element-type object
)
2815 (`entity
(org-element-property :latex-math-p object
))
2817 (let ((value (org-element-property :value object
)))
2818 (or (string-prefix-p "\\(" value
)
2819 (string-match-p "\\`\\$[^$]" value
))))))))
2820 (org-element-map data
'(entity latex-fragment
)
2822 ;; Skip objects already wrapped.
2823 (when (and (not (eq (org-element-type
2824 (org-element-property :parent object
))
2826 (funcall valid-object-p object
))
2827 (let ((math-block (list 'latex-math-block nil
))
2828 (next-elements (org-export-get-next-element object info t
))
2830 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2831 (org-element-insert-before math-block object
)
2832 (org-element-extract-element object
)
2833 (org-element-adopt-elements math-block object
)
2834 (when (zerop (or (org-element-property :post-blank object
) 0))
2835 ;; MATH-BLOCK swallows consecutive math objects.
2837 (dolist (next next-elements
)
2838 (unless (funcall valid-object-p next
) (throw 'exit nil
))
2839 (org-element-extract-element next
)
2840 (org-element-adopt-elements math-block next
)
2841 ;; Eschew the case: \beta$x$ -> \(\betax\).
2842 (org-element-put-property last
:post-blank
1)
2844 (when (> (or (org-element-property :post-blank next
) 0) 0)
2845 (throw 'exit nil
)))))
2846 (org-element-put-property
2847 math-block
:post-blank
(org-element-property :post-blank last
)))))
2848 info nil
'(latex-math-block) t
)
2849 ;; Return updated DATA.
2852 (defun org-latex-math-block (_math-block contents _info
)
2853 "Transcode a MATH-BLOCK object from Org to LaTeX.
2854 CONTENTS is a string. INFO is a plist used as a communication
2856 (when (org-string-nw-p contents
)
2857 (format "\\(%s\\)" (org-trim contents
))))
2861 (defun org-latex-quote-block (quote-block contents info
)
2862 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2863 CONTENTS holds the contents of the block. INFO is a plist
2864 holding contextual information."
2865 (org-latex--wrap-label
2866 quote-block
(format "\\begin{quote}\n%s\\end{quote}" contents
) info
))
2871 (defun org-latex-radio-target (radio-target text info
)
2872 "Transcode a RADIO-TARGET object from Org to LaTeX.
2873 TEXT is the text of the target. INFO is a plist holding
2874 contextual information."
2875 (format "\\label{%s}%s" (org-export-get-reference radio-target info
) text
))
2880 (defun org-latex-section (_section contents _info
)
2881 "Transcode a SECTION element from Org to LaTeX.
2882 CONTENTS holds the contents of the section. INFO is a plist
2883 holding contextual information."
2889 (defun org-latex-special-block (special-block contents info
)
2890 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2891 CONTENTS holds the contents of the block. INFO is a plist
2892 holding contextual information."
2893 (let ((type (org-element-property :type special-block
))
2894 (opt (org-export-read-attribute :attr_latex special-block
:options
))
2895 (caption (org-latex--caption/label-string special-block info
))
2896 (caption-above-p (org-latex--caption-above-p special-block info
)))
2897 (concat (format "\\begin{%s}%s\n" type
(or opt
""))
2898 (and caption-above-p caption
)
2900 (and (not caption-above-p
) caption
)
2901 (format "\\end{%s}" type
))))
2906 (defun org-latex-src-block (src-block _contents info
)
2907 "Transcode a SRC-BLOCK element from Org to LaTeX.
2908 CONTENTS holds the contents of the item. INFO is a plist holding
2909 contextual information."
2910 (when (org-string-nw-p (org-element-property :value src-block
))
2911 (let* ((lang (org-element-property :language src-block
))
2912 (caption (org-element-property :caption src-block
))
2913 (caption-above-p (org-latex--caption-above-p src-block info
))
2914 (label (org-element-property :name src-block
))
2915 (custom-env (and lang
2916 (cadr (assq (intern lang
)
2917 org-latex-custom-lang-environments
))))
2918 (num-start (org-export-get-loc src-block info
))
2919 (retain-labels (org-element-property :retain-labels src-block
))
2920 (attributes (org-export-read-attribute :attr_latex src-block
))
2921 (float (plist-get attributes
:float
))
2922 (listings (plist-get info
:latex-listings
)))
2924 ;; Case 1. No source fontification.
2925 ((or (not lang
) (not listings
))
2926 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2928 (cond ((string= "multicolumn" float
)
2929 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2930 (plist-get info
:latex-default-figure-position
)
2931 (if caption-above-p caption-str
"")
2932 (if caption-above-p
"" caption-str
)))
2934 (if caption-above-p caption-str
"")
2936 (if caption-above-p
"" (concat "\n" caption-str
))))
2940 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2941 (org-export-format-code-default src-block info
))))))
2942 ;; Case 2. Custom environment.
2944 (let ((caption-str (org-latex--caption/label-string src-block info
))
2945 (formatted-src (org-export-format-code-default src-block info
)))
2946 (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env
)
2947 (format "\\begin{%s}\n%s\\end{%s}\n"
2949 (concat (and caption-above-p caption-str
)
2951 (and (not caption-above-p
) caption-str
))
2953 (format-spec custom-env
2954 `((?s .
,formatted-src
)
2957 (?l .
,(org-latex--label src-block info
))
2958 (?o .
,(or (plist-get attributes
:options
) "")))))))
2959 ;; Case 3. Use minted package.
2960 ((eq listings
'minted
)
2961 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2964 ((string= "multicolumn" float
)
2965 (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
2966 (plist-get info
:latex-default-figure-position
)
2967 (if caption-above-p caption-str
"")
2968 (if caption-above-p
"" caption-str
)))
2970 (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
2971 (plist-get info
:latex-default-figure-position
)
2972 (if caption-above-p caption-str
"")
2973 (if caption-above-p
"" caption-str
)))
2974 ((string= "t" float
)
2975 (concat (format "\\begin{listing}[%s]\n"
2976 (plist-get info
:latex-default-figure-position
))
2977 "%s\n\\end{listing}"))
2979 (options (plist-get info
:latex-minted-options
))
2982 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2985 (org-latex--make-option-string
2986 (if (or (not num-start
) (assoc "linenos" options
))
2990 ("firstnumber" ,(number-to-string (1+ num-start
))))
2992 (let ((local-options (plist-get attributes
:options
)))
2993 (and local-options
(concat "," local-options
))))
2995 (or (cadr (assq (intern lang
)
2996 (plist-get info
:latex-minted-langs
)))
2999 (let* ((code-info (org-export-unravel-code src-block
))
3003 (org-split-string (car code-info
)
3005 (org-export-format-code
3007 (lambda (loc _num ref
)
3011 ;; Ensure references are flushed to the right,
3012 ;; separated with 6 spaces from the widest line
3014 (concat (make-string (+ (- max-width
(length loc
)) 6)
3016 (format "(%s)" ref
)))))
3017 nil
(and retain-labels
(cdr code-info
)))))))
3019 (format float-env body
)))
3020 ;; Case 4. Use listings package.
3023 (or (cadr (assq (intern lang
)
3024 (plist-get info
:latex-listings-langs
)))
3028 (let ((main (org-export-get-caption src-block
))
3029 (secondary (org-export-get-caption src-block t
)))
3031 (format "{%s}" (org-export-data main info
))
3033 (org-export-data secondary info
)
3034 (org-export-data main info
))))))
3035 (lst-opt (plist-get info
:latex-listings-options
)))
3041 (org-latex--make-option-string
3045 ((and (not float
) (plist-member attributes
:float
)) nil
)
3046 ((string= "multicolumn" float
) '(("float" "*")))
3047 ((and float
(not (assoc "float" lst-opt
)))
3048 `(("float" ,(plist-get info
:latex-default-figure-position
)))))
3049 `(("language" ,lst-lang
))
3051 `(("label" ,(org-latex--label src-block info
)))
3053 (if caption-str
`(("caption" ,caption-str
)) '(("caption" " ")))
3054 `(("captionpos" ,(if caption-above-p
"t" "b")))
3055 (cond ((assoc "numbers" lst-opt
) nil
)
3056 ((not num-start
) '(("numbers" "none")))
3057 (t `(("firstnumber" ,(number-to-string (1+ num-start
)))
3058 ("numbers" "left"))))))
3059 (let ((local-options (plist-get attributes
:options
)))
3060 (and local-options
(concat "," local-options
)))))
3063 "\\begin{lstlisting}\n%s\\end{lstlisting}"
3064 (let* ((code-info (org-export-unravel-code src-block
))
3068 (org-split-string (car code-info
) "\n")))))
3069 (org-export-format-code
3071 (lambda (loc _num ref
)
3075 ;; Ensure references are flushed to the right,
3076 ;; separated with 6 spaces from the widest line of
3078 (concat (make-string (+ (- max-width
(length loc
)) 6) ?\s
)
3079 (format "(%s)" ref
)))))
3080 nil
(and retain-labels
(cdr code-info
))))))))))))
3083 ;;;; Statistics Cookie
3085 (defun org-latex-statistics-cookie (statistics-cookie _contents _info
)
3086 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
3087 CONTENTS is nil. INFO is a plist holding contextual information."
3088 (replace-regexp-in-string
3089 "%" "\\%" (org-element-property :value statistics-cookie
) nil t
))
3094 (defun org-latex-strike-through (_strike-through contents info
)
3095 "Transcode STRIKE-THROUGH from Org to LaTeX.
3096 CONTENTS is the text with strike-through markup. INFO is a plist
3097 holding contextual information."
3098 (org-latex--text-markup contents
'strike-through info
))
3103 (defun org-latex-subscript (_subscript contents _info
)
3104 "Transcode a SUBSCRIPT object from Org to LaTeX.
3105 CONTENTS is the contents of the object."
3106 (format "\\textsubscript{%s}" contents
))
3111 (defun org-latex-superscript (_superscript contents _info
)
3112 "Transcode a SUPERSCRIPT object from Org to LaTeX.
3113 CONTENTS is the contents of the object."
3114 (format "\\textsuperscript{%s}" contents
))
3119 ;; `org-latex-table' is the entry point for table transcoding. It
3120 ;; takes care of tables with a "verbatim" mode. Otherwise, it
3121 ;; delegates the job to either `org-latex--table.el-table',
3122 ;; `org-latex--org-table' or `org-latex--math-table' functions,
3123 ;; depending of the type of the table and the mode requested.
3125 ;; `org-latex--align-string' is a subroutine used to build alignment
3126 ;; string for Org tables.
3128 (defun org-latex-table (table contents info
)
3129 "Transcode a TABLE element from Org to LaTeX.
3130 CONTENTS is the contents of the table. INFO is a plist holding
3131 contextual information."
3132 (if (eq (org-element-property :type table
) 'table.el
)
3133 ;; "table.el" table. Convert it using appropriate tools.
3134 (org-latex--table.el-table table info
)
3135 (let ((type (or (org-export-read-attribute :attr_latex table
:mode
)
3136 (plist-get info
:latex-default-table-mode
))))
3138 ;; Case 1: Verbatim table.
3139 ((string= type
"verbatim")
3140 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
3141 ;; Re-create table, without affiliated keywords.
3142 (org-trim (org-element-interpret-data
3143 `(table nil
,@(org-element-contents table
))))))
3145 ((or (string= type
"math") (string= type
"inline-math"))
3146 (org-latex--math-table table info
))
3147 ;; Case 3: Standard table.
3148 (t (concat (org-latex--org-table table contents info
)
3149 ;; When there are footnote references within the
3150 ;; table, insert their definition just after it.
3151 (org-latex--delayed-footnotes-definitions table info
)))))))
3153 (defun org-latex--align-string (table info
&optional math?
)
3154 "Return an appropriate LaTeX alignment string.
3155 TABLE is the considered table. INFO is a plist used as
3156 a communication channel. When optional argument MATH? is
3157 non-nil, TABLE is meant to be a matrix, where all cells are
3159 (or (org-export-read-attribute :attr_latex table
:align
)
3161 ;; Extract column groups and alignment from first (non-rule)
3164 (org-element-map table
'table-row
3166 (and (eq (org-element-property :type row
) 'standard
) row
))
3170 (let ((borders (org-export-table-cell-borders cell info
)))
3171 ;; Check left border for the first cell only.
3172 (when (and (memq 'left borders
) (not align
))
3174 (push (if math?
"c" ;center cells in matrices
3175 (cl-case (org-export-table-cell-alignment cell info
)
3180 (when (memq 'right borders
) (push "|" align
))))
3182 (apply 'concat
(nreverse align
)))))
3184 (defun org-latex--decorate-table (table attributes caption above? info
)
3185 "Decorate TABLE string with caption and float environment.
3187 ATTRIBUTES is the plist containing is LaTeX attributes. CAPTION
3188 is its caption, as a string or nil. It is located above the
3189 table if ABOVE? is non-nil. INFO is the plist containing current
3192 Return new environment, as a string."
3193 (let* ((float-environment
3194 (let ((float (plist-get attributes
:float
)))
3195 (cond ((and (not float
) (plist-member attributes
:float
)) nil
)
3196 ((member float
'("sidewaystable" "sideways")) "sidewaystable")
3197 ((equal float
"multicolumn") "table*")
3198 ((or float
(org-string-nw-p caption
)) "table")
3201 (or (plist-get attributes
:placement
)
3202 (format "[%s]" (plist-get info
:latex-default-figure-position
))))
3203 (center?
(if (plist-member attributes
:center
)
3204 (plist-get attributes
:center
)
3205 (plist-get info
:latex-tables-centered
)))
3206 (fontsize (let ((font (plist-get attributes
:font
)))
3207 (and font
(concat font
"\n")))))
3210 (concat (format "\\begin{%s}%s\n" float-environment placement
)
3211 (if above? caption
"")
3212 (when center?
"\\centering\n")
3215 (concat (and center?
"\\begin{center}\n" )
3216 (if above? caption
"")
3217 (cond ((and fontsize center?
) fontsize
)
3218 (fontsize (concat "{" fontsize
))
3220 (center?
(concat "\\begin{center}\n" fontsize
))
3221 (fontsize (concat "{" fontsize
)))
3225 (concat (if above?
"" (concat "\n" caption
))
3226 (format "\n\\end{%s}" float-environment
)))
3228 (concat (if above?
"" (concat "\n" caption
))
3229 (and center?
"\n\\end{center}")
3230 (and fontsize
(not center?
) "}")))
3231 (center?
"\n\\end{center}")
3234 (defun org-latex--org-table (table contents info
)
3235 "Return appropriate LaTeX code for an Org table.
3237 TABLE is the table type element to transcode. CONTENTS is its
3238 contents, as a string. INFO is a plist used as a communication
3241 This function assumes TABLE has `org' as its `:type' property and
3242 `table' as its `:mode' attribute."
3243 (let* ((attr (org-export-read-attribute :attr_latex table
))
3244 (alignment (org-latex--align-string table info
))
3245 (table-env (or (plist-get attr
:environment
)
3246 (plist-get info
:latex-default-table-environment
)))
3248 (let ((w (plist-get attr
:width
)))
3250 ((member table-env
'("tabular" "longtable")) "")
3251 ((member table-env
'("tabu" "longtabu"))
3252 (format (if (plist-get attr
:spread
) " spread %s "
3255 (t (format "{%s}" w
)))))
3256 (caption (org-latex--caption/label-string table info
))
3257 (above?
(org-latex--caption-above-p table info
)))
3259 ((member table-env
'("longtable" "longtabu"))
3260 (let ((fontsize (let ((font (plist-get attr
:font
)))
3261 (and font
(concat font
"\n")))))
3262 (concat (and fontsize
(concat "{" fontsize
))
3263 (format "\\begin{%s}%s{%s}\n" table-env width alignment
)
3265 (org-string-nw-p caption
)
3266 (concat caption
"\\\\\n"))
3269 (org-string-nw-p caption
)
3270 (concat caption
"\\\\\n"))
3271 (format "\\end{%s}" table-env
)
3272 (and fontsize
"}"))))
3274 (let ((output (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
3280 (org-latex--decorate-table output attr caption above? info
))))))
3282 (defun org-latex--table.el-table
(table info
)
3283 "Return appropriate LaTeX code for a table.el table.
3285 TABLE is the table type element to transcode. INFO is a plist
3286 used as a communication channel.
3288 This function assumes TABLE has `table.el' as its `:type'
3291 ;; Ensure "*org-export-table*" buffer is empty.
3292 (with-current-buffer (get-buffer-create "*org-export-table*")
3295 (replace-regexp-in-string
3296 "^%.*\n" "" ;remove comments
3298 (save-excursion (insert (org-element-property :value table
)))
3299 (re-search-forward "^[ \t]*|[^|]" nil t
)
3300 (table-generate-source 'latex
"*org-export-table*")
3301 (with-current-buffer "*org-export-table*"
3302 (org-trim (buffer-string))))
3304 (kill-buffer (get-buffer "*org-export-table*"))
3305 (let ((attr (org-export-read-attribute :attr_latex table
))
3306 (caption (org-latex--caption/label-string table info
))
3307 (above?
(org-latex--caption-above-p table info
)))
3308 (when (plist-get attr
:rmlines
)
3309 ;; When the "rmlines" attribute is provided, remove all hlines
3310 ;; but the the one separating heading from the table body.
3311 (let ((n 0) (pos 0))
3312 (while (and (< (length output
) pos
)
3313 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
3315 (unless (= n
2) (setq output
(replace-match "" nil nil output
))))))
3316 (org-latex--decorate-table output attr caption above? info
))))
3318 (defun org-latex--math-table (table info
)
3319 "Return appropriate LaTeX code for a matrix.
3321 TABLE is the table type element to transcode. INFO is a plist
3322 used as a communication channel.
3324 This function assumes TABLE has `org' as its `:type' property and
3325 `inline-math' or `math' as its `:mode' attribute."
3326 (let* ((attr (org-export-read-attribute :attr_latex table
))
3327 (env (or (plist-get attr
:environment
)
3328 (plist-get info
:latex-default-table-environment
)))
3332 (if (eq (org-element-property :type row
) 'rule
) "\\hline"
3333 ;; Return each cell unmodified.
3337 (substring (org-element-interpret-data cell
) 0 -
1))
3338 (org-element-map row
'table-cell
#'identity info
) "&")
3339 (or (cdr (assoc env org-latex-table-matrix-macros
)) "\\\\")
3341 (org-element-map table
'table-row
#'identity info
) "")))
3344 (plist-get attr
:math-prefix
)
3345 ;; Environment. Also treat special cases.
3346 (cond ((member env
'("array" "tabular"))
3347 (format "\\begin{%s}{%s}\n%s\\end{%s}"
3348 env
(org-latex--align-string table info t
) contents env
))
3349 ((assoc env org-latex-table-matrix-macros
)
3350 (format "\\%s%s{\n%s}"
3352 (or (plist-get attr
:math-arguments
) "")
3354 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env
)))
3356 (plist-get attr
:math-suffix
))))
3361 (defun org-latex-table-cell (table-cell contents info
)
3362 "Transcode a TABLE-CELL element from Org to LaTeX.
3363 CONTENTS is the cell contents. INFO is a plist used as
3364 a communication channel."
3366 (let ((scientific-format (plist-get info
:latex-table-scientific-notation
)))
3369 (string-match orgtbl-exp-regexp contents
))
3370 ;; Use appropriate format string for scientific
3372 (format scientific-format
3373 (match-string 1 contents
)
3374 (match-string 2 contents
))
3376 (when (org-export-get-next-element table-cell info
) " & ")))
3381 (defun org-latex-table-row (table-row contents info
)
3382 "Transcode a TABLE-ROW element from Org to LaTeX.
3383 CONTENTS is the contents of the row. INFO is a plist used as
3384 a communication channel."
3385 (let* ((attr (org-export-read-attribute :attr_latex
3386 (org-export-get-parent table-row
)))
3387 (booktabsp (if (plist-member attr
:booktabs
) (plist-get attr
:booktabs
)
3388 (plist-get info
:latex-tables-booktabs
)))
3390 (member (or (plist-get attr
:environment
)
3391 (plist-get info
:latex-default-table-environment
))
3392 '("longtable" "longtabu"))))
3393 (if (eq (org-element-property :type table-row
) 'rule
)
3395 ((not booktabsp
) "\\hline")
3396 ((not (org-export-get-previous-element table-row info
)) "\\toprule")
3397 ((not (org-export-get-next-element table-row info
)) "\\bottomrule")
3399 (org-export-table-row-ends-header-p
3400 (org-export-get-previous-element table-row info
) info
))
3404 ;; When BOOKTABS are activated enforce top-rule even when no
3405 ;; hline was specifically marked.
3406 (and booktabsp
(not (org-export-get-previous-element table-row info
))
3410 ;; Special case for long tables. Define header and footers.
3411 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
3412 (let ((columns (cdr (org-export-table-dimensions
3413 (org-export-get-parent-table table-row
) info
))))
3416 \\multicolumn{%d}{l}{%s} \\\\
3421 %s\\multicolumn{%d}{r}{%s} \\\\
3424 (if booktabsp
"\\midrule" "\\hline")
3426 (org-latex--translate "Continued from previous page" info
)
3428 ((not (org-export-table-row-starts-header-p table-row info
))
3430 (booktabsp "\\toprule\n")
3433 (if booktabsp
"\\midrule" "\\hline")
3434 (if booktabsp
"\\midrule" "\\hline")
3436 (org-latex--translate "Continued on next page" info
))))
3437 ;; When BOOKTABS are activated enforce bottom rule even when
3438 ;; no hline was specifically marked.
3439 ((and booktabsp
(not (org-export-get-next-element table-row info
)))
3440 "\\bottomrule"))))))
3445 (defun org-latex-target (target _contents info
)
3446 "Transcode a TARGET object from Org to LaTeX.
3447 CONTENTS is nil. INFO is a plist holding contextual
3449 (format "\\label{%s}" (org-latex--label target info
)))
3454 (defun org-latex-timestamp (timestamp _contents info
)
3455 "Transcode a TIMESTAMP object from Org to LaTeX.
3456 CONTENTS is nil. INFO is a plist holding contextual
3458 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp
) info
)))
3461 (cl-case (org-element-property :type timestamp
)
3462 ((active active-range
) :latex-active-timestamp-format
)
3463 ((inactive inactive-range
) :latex-inactive-timestamp-format
)
3464 (otherwise :latex-diary-timestamp-format
)))
3470 (defun org-latex-underline (_underline contents info
)
3471 "Transcode UNDERLINE from Org to LaTeX.
3472 CONTENTS is the text with underline markup. INFO is a plist
3473 holding contextual information."
3474 (org-latex--text-markup contents
'underline info
))
3479 (defun org-latex-verbatim (verbatim _contents info
)
3480 "Transcode a VERBATIM object from Org to LaTeX.
3481 CONTENTS is nil. INFO is a plist used as a communication
3483 (org-latex--text-markup
3484 (org-element-property :value verbatim
) 'verbatim info
))
3489 (defun org-latex-verse-block (verse-block contents info
)
3490 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3491 CONTENTS is verse block contents. INFO is a plist holding
3492 contextual information."
3493 (org-latex--wrap-label
3495 ;; In a verse environment, add a line break to each newline
3496 ;; character and change each white space at beginning of a line
3497 ;; into a space of 1 em. Also change each blank line with
3498 ;; a vertical space of 1 em.
3499 (format "\\begin{verse}\n%s\\end{verse}"
3500 (replace-regexp-in-string
3501 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m
)))
3502 (replace-regexp-in-string
3503 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3504 (replace-regexp-in-string
3505 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3506 contents nil t
) nil t
) nil t
))
3511 ;;; End-user functions
3514 (defun org-latex-export-as-latex
3515 (&optional async subtreep visible-only body-only ext-plist
)
3516 "Export current buffer as a LaTeX buffer.
3518 If narrowing is active in the current buffer, only export its
3521 If a region is active, export that region.
3523 A non-nil optional argument ASYNC means the process should happen
3524 asynchronously. The resulting buffer should be accessible
3525 through the `org-export-stack' interface.
3527 When optional argument SUBTREEP is non-nil, export the sub-tree
3528 at point, extracting information from the headline properties
3531 When optional argument VISIBLE-ONLY is non-nil, don't export
3532 contents of hidden elements.
3534 When optional argument BODY-ONLY is non-nil, only write code
3535 between \"\\begin{document}\" and \"\\end{document}\".
3537 EXT-PLIST, when provided, is a property list with external
3538 parameters overriding Org default settings, but still inferior to
3539 file-local settings.
3541 Export is done in a buffer named \"*Org LATEX Export*\", which
3542 will be displayed when `org-export-show-temporary-export-buffer'
3545 (org-export-to-buffer 'latex
"*Org LATEX Export*"
3546 async subtreep visible-only body-only ext-plist
(lambda () (LaTeX-mode))))
3549 (defun org-latex-convert-region-to-latex ()
3550 "Assume the current region has Org syntax, and convert it to LaTeX.
3551 This can be used in any buffer. For example, you can write an
3552 itemized list in Org syntax in an LaTeX buffer and use this
3553 command to convert it."
3555 (org-export-replace-region-by 'latex
))
3558 (defun org-latex-export-to-latex
3559 (&optional async subtreep visible-only body-only ext-plist
)
3560 "Export current buffer to a LaTeX file.
3562 If narrowing is active in the current buffer, only export its
3565 If a region is active, export that region.
3567 A non-nil optional argument ASYNC means the process should happen
3568 asynchronously. The resulting file should be accessible through
3569 the `org-export-stack' interface.
3571 When optional argument SUBTREEP is non-nil, export the sub-tree
3572 at point, extracting information from the headline properties
3575 When optional argument VISIBLE-ONLY is non-nil, don't export
3576 contents of hidden elements.
3578 When optional argument BODY-ONLY is non-nil, only write code
3579 between \"\\begin{document}\" and \"\\end{document}\".
3581 EXT-PLIST, when provided, is a property list with external
3582 parameters overriding Org default settings, but still inferior to
3583 file-local settings."
3585 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
3586 (org-export-to-file 'latex outfile
3587 async subtreep visible-only body-only ext-plist
)))
3590 (defun org-latex-export-to-pdf
3591 (&optional async subtreep visible-only body-only ext-plist
)
3592 "Export current buffer to LaTeX then process through to PDF.
3594 If narrowing is active in the current buffer, only export its
3597 If a region is active, export that region.
3599 A non-nil optional argument ASYNC means the process should happen
3600 asynchronously. The resulting file should be accessible through
3601 the `org-export-stack' interface.
3603 When optional argument SUBTREEP is non-nil, export the sub-tree
3604 at point, extracting information from the headline properties
3607 When optional argument VISIBLE-ONLY is non-nil, don't export
3608 contents of hidden elements.
3610 When optional argument BODY-ONLY is non-nil, only write code
3611 between \"\\begin{document}\" and \"\\end{document}\".
3613 EXT-PLIST, when provided, is a property list with external
3614 parameters overriding Org default settings, but still inferior to
3615 file-local settings.
3617 Return PDF file's name."
3619 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
3620 (org-export-to-file 'latex outfile
3621 async subtreep visible-only body-only ext-plist
3622 (lambda (file) (org-latex-compile file
)))))
3624 (defun org-latex-compile (texfile &optional snippet
)
3625 "Compile a TeX file.
3627 TEXFILE is the name of the file being compiled. Processing is
3628 done through the command specified in `org-latex-pdf-process',
3629 which see. Output is redirected to \"*Org PDF LaTeX Output*\"
3632 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3633 file used to preview a LaTeX snippet. In this case, do not
3634 create a log buffer and do not remove log files.
3636 Return PDF file name or raise an error if it couldn't be
3638 (unless snippet
(message "Processing LaTeX file %s..." texfile
))
3640 (or (with-temp-buffer
3641 (save-excursion (insert-file-contents texfile
))
3642 (and (search-forward-regexp (regexp-opt org-latex-compilers
)
3643 (line-end-position 2)
3645 (progn (beginning-of-line) (looking-at-p "%"))
3648 (process (if (functionp org-latex-pdf-process
) org-latex-pdf-process
3649 ;; Replace "%latex" and "%bibtex" with,
3650 ;; respectively, "%L" and "%B" so as to adhere to
3651 ;; `format-spec' specifications.
3652 (mapcar (lambda (command)
3653 (replace-regexp-in-string
3654 "%\\(?:bib\\|la\\)tex\\>"
3655 (lambda (m) (upcase (substring m
0 2)))
3657 org-latex-pdf-process
)))
3658 (spec `((?B .
,(shell-quote-argument org-latex-bib-compiler
))
3659 (?L .
,(shell-quote-argument compiler
))))
3660 (log-buf-name "*Org PDF LaTeX Output*")
3661 (log-buf (and (not snippet
) (get-buffer-create log-buf-name
)))
3662 (outfile (org-compile-file texfile process
"pdf"
3663 (format "See %S for details" log-buf-name
)
3666 (when org-latex-remove-logfiles
3669 (file-name-directory outfile
)
3671 (concat (regexp-quote (file-name-base outfile
))
3672 "\\(?:\\.[0-9]+\\)?\\."
3673 (regexp-opt org-latex-logfiles-extensions
))
3675 (let ((warnings (org-latex--collect-warnings log-buf
)))
3676 (message (concat "PDF file produced"
3678 ((eq warnings
'error
) " with errors.")
3679 (warnings (concat " with warnings: " warnings
))
3681 ;; Return output file name.
3684 (defun org-latex--collect-warnings (buffer)
3685 "Collect some warnings from \"pdflatex\" command output.
3686 BUFFER is the buffer containing output. Return collected
3687 warnings types as a string, `error' if a LaTeX error was
3688 encountered or nil if there was none."
3689 (with-current-buffer buffer
3691 (goto-char (point-max))
3692 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t
)
3693 (if (re-search-forward "^!" nil t
) 'error
3694 (let ((case-fold-search t
)
3696 (dolist (warning org-latex-known-warnings
)
3697 (when (save-excursion (re-search-forward (car warning
) nil t
))
3698 (setq warnings
(concat warnings
" " (cdr warning
)))))
3699 (org-string-nw-p (org-trim warnings
))))))))
3702 (defun org-latex-publish-to-latex (plist filename pub-dir
)
3703 "Publish an Org file to LaTeX.
3705 FILENAME is the filename of the Org file to be published. PLIST
3706 is the property list for the given project. PUB-DIR is the
3707 publishing directory.
3709 Return output file name."
3710 (org-publish-org-to 'latex filename
".tex" plist pub-dir
))
3713 (defun org-latex-publish-to-pdf (plist filename pub-dir
)
3714 "Publish an Org file to PDF (via LaTeX).
3716 FILENAME is the filename of the Org file to be published. PLIST
3717 is the property list for the given project. PUB-DIR is the
3718 publishing directory.
3720 Return output file name."
3721 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3722 ;; in working directory and then moved to publishing directory.
3723 (org-publish-attachment
3725 ;; Default directory could be anywhere when this function is
3726 ;; called. We ensure it is set to source file directory during
3727 ;; compilation so as to not break links to external documents.
3728 (let ((default-directory (file-name-directory filename
)))
3731 'latex filename
".tex" plist
(file-name-directory filename
))))
3738 ;; generated-autoload-file: "org-loaddefs.el"
3741 ;;; ox-latex.el ends here