1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2011-2017 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; See Org manual for details.
33 (defvar org-latex-default-packages-alist
)
34 (defvar org-latex-packages-alist
)
35 (defvar orgtbl-exp-regexp
)
41 (org-export-define-backend 'latex
42 '((bold . org-latex-bold
)
43 (center-block . org-latex-center-block
)
44 (clock . org-latex-clock
)
45 (code . org-latex-code
)
46 (drawer . org-latex-drawer
)
47 (dynamic-block . org-latex-dynamic-block
)
48 (entity . org-latex-entity
)
49 (example-block . org-latex-example-block
)
50 (export-block . org-latex-export-block
)
51 (export-snippet . org-latex-export-snippet
)
52 (fixed-width . org-latex-fixed-width
)
53 (footnote-definition . org-latex-footnote-definition
)
54 (footnote-reference . org-latex-footnote-reference
)
55 (headline . org-latex-headline
)
56 (horizontal-rule . org-latex-horizontal-rule
)
57 (inline-src-block . org-latex-inline-src-block
)
58 (inlinetask . org-latex-inlinetask
)
59 (italic . org-latex-italic
)
60 (item . org-latex-item
)
61 (keyword . org-latex-keyword
)
62 (latex-environment . org-latex-latex-environment
)
63 (latex-fragment . org-latex-latex-fragment
)
64 (line-break . org-latex-line-break
)
65 (link . org-latex-link
)
66 (node-property . org-latex-node-property
)
67 (paragraph . org-latex-paragraph
)
68 (plain-list . org-latex-plain-list
)
69 (plain-text . org-latex-plain-text
)
70 (planning . org-latex-planning
)
71 (property-drawer . org-latex-property-drawer
)
72 (quote-block . org-latex-quote-block
)
73 (radio-target . org-latex-radio-target
)
74 (section . org-latex-section
)
75 (special-block . org-latex-special-block
)
76 (src-block . org-latex-src-block
)
77 (statistics-cookie . org-latex-statistics-cookie
)
78 (strike-through . org-latex-strike-through
)
79 (subscript . org-latex-subscript
)
80 (superscript . org-latex-superscript
)
81 (table . org-latex-table
)
82 (table-cell . org-latex-table-cell
)
83 (table-row . org-latex-table-row
)
84 (target . org-latex-target
)
85 (template . org-latex-template
)
86 (timestamp . org-latex-timestamp
)
87 (underline . org-latex-underline
)
88 (verbatim . org-latex-verbatim
)
89 (verse-block . org-latex-verse-block
)
90 ;; Pseudo objects and elements.
91 (latex-math-block . org-latex-math-block
)
92 (latex-matrices . org-latex-matrices
))
94 '(?l
"Export to LaTeX"
95 ((?L
"As LaTeX buffer" org-latex-export-as-latex
)
96 (?l
"As LaTeX file" org-latex-export-to-latex
)
97 (?p
"As PDF file" org-latex-export-to-pdf
)
98 (?o
"As PDF file and open"
100 (if a
(org-latex-export-to-pdf t s v b
)
101 (org-open-file (org-latex-export-to-pdf nil s v b
)))))))
102 :filters-alist
'((:filter-options . org-latex-math-block-options-filter
)
103 (:filter-paragraph . org-latex-clean-invalid-line-breaks
)
104 (:filter-parse-tree org-latex-math-block-tree-filter
105 org-latex-matrices-tree-filter
)
106 (:filter-verse-block . org-latex-clean-invalid-line-breaks
))
108 '((:latex-class
"LATEX_CLASS" nil org-latex-default-class t
)
109 (:latex-class-options
"LATEX_CLASS_OPTIONS" nil nil t
)
110 (:latex-header
"LATEX_HEADER" nil nil newline
)
111 (:latex-header-extra
"LATEX_HEADER_EXTRA" nil nil newline
)
112 (:description
"DESCRIPTION" nil nil parse
)
113 (:keywords
"KEYWORDS" nil nil parse
)
114 (:subtitle
"SUBTITLE" nil nil parse
)
116 (:latex-active-timestamp-format nil nil org-latex-active-timestamp-format
)
117 (:latex-caption-above nil nil org-latex-caption-above
)
118 (:latex-classes nil nil org-latex-classes
)
119 (:latex-default-figure-position nil nil org-latex-default-figure-position
)
120 (:latex-default-table-environment nil nil org-latex-default-table-environment
)
121 (:latex-default-table-mode nil nil org-latex-default-table-mode
)
122 (:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format
)
123 (:latex-footnote-defined-format nil nil org-latex-footnote-defined-format
)
124 (:latex-footnote-separator nil nil org-latex-footnote-separator
)
125 (:latex-format-drawer-function nil nil org-latex-format-drawer-function
)
126 (:latex-format-headline-function nil nil org-latex-format-headline-function
)
127 (:latex-format-inlinetask-function nil nil org-latex-format-inlinetask-function
)
128 (:latex-hyperref-template nil nil org-latex-hyperref-template t
)
129 (:latex-image-default-height nil nil org-latex-image-default-height
)
130 (:latex-image-default-option nil nil org-latex-image-default-option
)
131 (:latex-image-default-width nil nil org-latex-image-default-width
)
132 (:latex-images-centered nil nil org-latex-images-centered
)
133 (:latex-inactive-timestamp-format nil nil org-latex-inactive-timestamp-format
)
134 (:latex-inline-image-rules nil nil org-latex-inline-image-rules
)
135 (:latex-link-with-unknown-path-format nil nil org-latex-link-with-unknown-path-format
)
136 (:latex-listings nil nil org-latex-listings
)
137 (:latex-listings-langs nil nil org-latex-listings-langs
)
138 (:latex-listings-options nil nil org-latex-listings-options
)
139 (:latex-minted-langs nil nil org-latex-minted-langs
)
140 (:latex-minted-options nil nil org-latex-minted-options
)
141 (:latex-prefer-user-labels nil nil org-latex-prefer-user-labels
)
142 (:latex-subtitle-format nil nil org-latex-subtitle-format
)
143 (:latex-subtitle-separate nil nil org-latex-subtitle-separate
)
144 (:latex-table-scientific-notation nil nil org-latex-table-scientific-notation
)
145 (:latex-tables-booktabs nil nil org-latex-tables-booktabs
)
146 (:latex-tables-centered nil nil org-latex-tables-centered
)
147 (:latex-text-markup-alist nil nil org-latex-text-markup-alist
)
148 (:latex-title-command nil nil org-latex-title-command
)
149 (:latex-toc-command nil nil org-latex-toc-command
)
150 (:latex-compiler
"LATEX_COMPILER" nil org-latex-compiler
)
151 ;; Redefine regular options.
152 (:date
"DATE" nil
"\\today" parse
)))
156 ;;; Internal Variables
158 (defconst org-latex-babel-language-alist
159 '(("af" .
"afrikaans")
161 ("bt-br" .
"brazilian")
167 ("de-at" .
"naustrian")
168 ("de-de" .
"ngerman")
171 ("en-au" .
"australian")
172 ("en-ca" .
"canadian")
173 ("en-gb" .
"british")
175 ("en-nz" .
"newzealand")
176 ("en-us" .
"american")
182 ("fr-ca" .
"canadien")
186 ("id" .
"indonesian")
196 ("pt" .
"portuguese")
200 ("sb" .
"uppersorbian")
208 ("uk" .
"ukrainian"))
209 "Alist between language code and corresponding Babel option.")
211 (defconst org-latex-polyglossia-language-alist
219 ("bt-br" "brazilian")
225 ("de" "german" "german")
226 ("de-at" "german" "austrian")
227 ("de-de" "german" "german")
230 ("en" "english" "usmax")
231 ("en-au" "english" "australian")
232 ("en-gb" "english" "uk")
233 ("en-nz" "english" "newzealand")
234 ("en-us" "english" "usmax")
256 ("la" "latin" "modern")
257 ("la-modern" "latin" "modern")
258 ("la-classic" "latin" "classic")
259 ("la-medieval" "latin" "medieval")
272 ("pms" "piedmontese")
295 "Alist between language code and corresponding Polyglossia option")
299 (defconst org-latex-table-matrix-macros
'(("bordermatrix" .
"\\cr")
300 ("qbordermatrix" .
"\\cr")
301 ("kbordermatrix" .
"\\\\"))
302 "Alist between matrix macros and their row ending.")
304 (defconst org-latex-math-environments-re
306 "\\`[ \t]*\\\\begin{%s\\*?}"
308 '("equation" "eqnarray" "math" "displaymath"
309 "align" "gather" "multline" "flalign" "alignat"
310 "xalignat" "xxalignat"
313 "dmath" "dseries" "dgroup" "darray"
316 "Regexp of LaTeX math environments.")
319 ;;; User Configurable Variables
321 (defgroup org-export-latex nil
322 "Options for exporting Org mode files to LaTeX."
323 :tag
"Org Export LaTeX"
328 (defcustom org-latex-caption-above
'(table)
329 "When non-nil, place caption string at the beginning of elements.
330 Otherwise, place it near the end. When value is a list of
331 symbols, put caption above selected elements only. Allowed
332 symbols are: `image', `table', `src-block' and `special-block'."
333 :group
'org-export-latex
335 :package-version
'(Org .
"8.3")
337 (const :tag
"For all elements" t
)
338 (const :tag
"For no element" nil
)
339 (set :tag
"For the following elements only" :greedy t
340 (const :tag
"Images" image
)
341 (const :tag
"Tables" table
)
342 (const :tag
"Source code" src-block
)
343 (const :tag
"Special blocks" special-block
))))
345 (defcustom org-latex-prefer-user-labels nil
346 "Use user-provided labels instead of internal ones when non-nil.
348 When this variable is non-nil, Org will use the value of
349 CUSTOM_ID property, NAME keyword or Org target as the key for the
350 \\label commands generated.
352 By default, Org generates its own internal labels during LaTeX
353 export. This process ensures that the \\label keys are unique
354 and valid, but it means the keys are not available in advance of
357 Setting this variable gives you control over how Org generates
358 labels during LaTeX export, so that you may know their keys in
359 advance. One reason to do this is that it allows you to refer to
360 various elements using a single label both in Org's link syntax
361 and in embedded LaTeX code.
363 For example, when this variable is non-nil, a headline like this:
369 This is section [[#sec:foo]].
371 And this is still section \\ref{sec:foo}.
374 will be exported to LaTeX as:
376 \\subsection{Some section}
378 This is section \\ref{sec:foo}.
379 And this is still section \\ref{sec:foo}.
381 Note, however, that setting this variable introduces a limitation
382 on the possible values for CUSTOM_ID and NAME. When this
383 variable is non-nil, Org passes their value to \\label unchanged.
384 You are responsible for ensuring that the value is a valid LaTeX
385 \\label key, and that no other \\label commands with the same key
386 appear elsewhere in your document. (Keys may contain letters,
387 numbers, and the following punctuation: '_' '.' '-' ':'.) There
388 are no such limitations on CUSTOM_ID and NAME when this variable
391 For headlines that do not define the CUSTOM_ID property or
392 elements without a NAME, Org will continue to use its default
393 labeling scheme to generate labels and resolve links into proper
395 :group
'org-export-latex
398 :package-version
'(Org .
"8.3"))
402 (defcustom org-latex-default-class
"article"
403 "The default LaTeX class."
404 :group
'org-export-latex
405 :type
'(string :tag
"LaTeX class"))
407 (defcustom org-latex-classes
409 "\\documentclass[11pt]{article}"
410 ("\\section{%s}" .
"\\section*{%s}")
411 ("\\subsection{%s}" .
"\\subsection*{%s}")
412 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
413 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
414 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
416 "\\documentclass[11pt]{report}"
417 ("\\part{%s}" .
"\\part*{%s}")
418 ("\\chapter{%s}" .
"\\chapter*{%s}")
419 ("\\section{%s}" .
"\\section*{%s}")
420 ("\\subsection{%s}" .
"\\subsection*{%s}")
421 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
423 "\\documentclass[11pt]{book}"
424 ("\\part{%s}" .
"\\part*{%s}")
425 ("\\chapter{%s}" .
"\\chapter*{%s}")
426 ("\\section{%s}" .
"\\section*{%s}")
427 ("\\subsection{%s}" .
"\\subsection*{%s}")
428 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
429 "Alist of LaTeX classes and associated header and structure.
430 If #+LATEX_CLASS is set in the buffer, use its value and the
431 associated information. Here is the structure of each cell:
435 (numbered-section . unnumbered-section)
441 The HEADER-STRING is the header that will be inserted into the
442 LaTeX file. It should contain the \\documentclass macro, and
443 anything else that is needed for this setup. To this header, the
444 following commands will be added:
446 - Calls to \\usepackage for all packages mentioned in the
447 variables `org-latex-default-packages-alist' and
448 `org-latex-packages-alist'. Thus, your header definitions
449 should avoid to also request these packages.
451 - Lines specified via \"#+LATEX_HEADER:\" and
452 \"#+LATEX_HEADER_EXTRA:\" keywords.
454 If you need more control about the sequence in which the header
455 is built up, or if you want to exclude one of these building
456 blocks for a particular class, you can use the following
457 macro-like placeholders.
459 [DEFAULT-PACKAGES] \\usepackage statements for default packages
460 [NO-DEFAULT-PACKAGES] do not include any of the default packages
461 [PACKAGES] \\usepackage statements for packages
462 [NO-PACKAGES] do not include the packages
463 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
464 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
468 \\documentclass{article}
469 [NO-DEFAULT-PACKAGES]
471 \\providecommand{\\alert}[1]{\\textbf{#1}}
474 will omit the default packages, and will include the
475 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
476 to \\providecommand, and then place \\usepackage commands based
477 on the content of `org-latex-packages-alist'.
479 If your header, `org-latex-default-packages-alist' or
480 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
481 AUTO will automatically be replaced with a coding system derived
482 from `buffer-file-coding-system'. See also the variable
483 `org-latex-inputenc-alist' for a way to influence this mechanism.
485 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\"
486 or \"\\usepackage[AUTO]{polyglossia}\", AUTO will be replaced
487 with the language related to the language code specified by
488 `org-export-default-language'. Note that constructions such as
489 \"\\usepackage[french,AUTO,english]{babel}\" are permitted. For
490 Polyglossia the language will be set via the macros
491 \"\\setmainlanguage\" and \"\\setotherlanguage\". See also
492 `org-latex-guess-babel-language' and
493 `org-latex-guess-polyglossia-language'.
495 The sectioning structure
496 ------------------------
498 The sectioning structure of the class is given by the elements
499 following the header string. For each sectioning level, a number
500 of strings is specified. A %s formatter is mandatory in each
501 section string and will be replaced by the title of the section.
503 Instead of a cons cell (numbered . unnumbered), you can also
504 provide a list of 2 or 4 elements,
506 (numbered-open numbered-close)
510 (numbered-open numbered-close unnumbered-open unnumbered-close)
512 providing opening and closing strings for a LaTeX environment
513 that should represent the document section. The opening clause
514 should have a %s to represent the section title.
516 Instead of a list of sectioning commands, you can also specify
517 a function name. That function will be called with two
518 parameters, the (reduced) level of the headline, and a predicate
519 non-nil when the headline should be numbered. It must return
520 a format string in which the section title will be added."
521 :group
'org-export-latex
523 (list (string :tag
"LaTeX class")
524 (string :tag
"LaTeX header")
525 (repeat :tag
"Levels" :inline t
528 (string :tag
" numbered")
529 (string :tag
"unnumbered"))
530 (list :tag
"Environment"
531 (string :tag
"Opening (numbered)")
532 (string :tag
"Closing (numbered)")
533 (string :tag
"Opening (unnumbered)")
534 (string :tag
"Closing (unnumbered)"))
535 (function :tag
"Hook computing sectioning"))))))
537 (defcustom org-latex-inputenc-alist nil
538 "Alist of inputenc coding system names, and what should really be used.
539 For example, adding an entry
541 (\"utf8\" . \"utf8x\")
543 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
544 are written as utf8 files."
545 :group
'org-export-latex
548 (string :tag
"Derived from buffer")
549 (string :tag
"Use this instead"))))
551 (defcustom org-latex-title-command
"\\maketitle"
552 "The command used to insert the title just after \\begin{document}.
554 This format string may contain these elements:
556 %a for AUTHOR keyword
558 %s for SUBTITLE keyword
560 %d for DESCRIPTION line
562 %l for Language keyword
563 %L for capitalized language keyword
566 If you need to use a \"%\" character, you need to escape it
569 Setting :latex-title-command in publishing projects will take
570 precedence over this variable."
571 :group
'org-export-latex
572 :type
'(string :tag
"Format string"))
574 (defcustom org-latex-subtitle-format
"\\\\\\medskip\n\\large %s"
575 "Format string used for transcoded subtitle.
576 The format string should have at most one \"%s\"-expression,
577 which is replaced with the subtitle."
578 :group
'org-export-latex
580 :package-version
'(Org .
"8.3")
581 :type
'(string :tag
"Format string"))
583 (defcustom org-latex-subtitle-separate nil
584 "Non-nil means the subtitle is not typeset as part of title."
585 :group
'org-export-latex
587 :package-version
'(Org .
"8.3")
590 (defcustom org-latex-toc-command
"\\tableofcontents\n\n"
591 "LaTeX command to set the table of contents, list of figures, etc.
592 This command only applies to the table of contents generated with
593 the toc:nil option, not to those generated with #+TOC keyword."
594 :group
'org-export-latex
597 (defcustom org-latex-hyperref-template
598 "\\hypersetup{\n pdfauthor={%a},\n pdftitle={%t},\n pdfkeywords={%k},
599 pdfsubject={%d},\n pdfcreator={%c}, \n pdflang={%L}}\n"
600 "Template for hyperref package options.
602 This format string may contain these elements:
604 %a for AUTHOR keyword
606 %s for SUBTITLE keyword
608 %d for DESCRIPTION line
610 %l for Language keyword
611 %L for capitalized language keyword
614 If you need to use a \"%\" character, you need to escape it
617 As a special case, a nil value prevents template from being
620 Setting :latex-hyperref-template in publishing projects will take
621 precedence over this variable."
622 :group
'org-export-latex
624 :package-version
'(Org .
"8.3")
625 :type
'(choice (const :tag
"No template" nil
)
626 (string :tag
"Format string")))
630 (defcustom org-latex-format-headline-function
631 'org-latex-format-headline-default-function
632 "Function for formatting the headline's text.
634 This function will be called with six arguments:
635 TODO the todo keyword (string or nil)
636 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
637 PRIORITY the priority of the headline (integer or nil)
638 TEXT the main headline text (string)
639 TAGS the tags (list of strings or nil)
640 INFO the export options (plist)
642 The function result will be used in the section format string."
643 :group
'org-export-latex
645 :package-version
'(Org .
"8.0")
651 (defcustom org-latex-footnote-separator
"\\textsuperscript{,}\\,"
652 "Text used to separate footnotes."
653 :group
'org-export-latex
656 (defcustom org-latex-footnote-defined-format
"\\textsuperscript{\\ref{%s}}"
657 "Format string used to format reference to footnote already defined.
658 %s will be replaced by the label of the referred footnote."
659 :group
'org-export-latex
661 (const :tag
"Use plain superscript (default)" "\\textsuperscript{\\ref{%s}}")
662 (const :tag
"Use Memoir/KOMA-Script footref" "\\footref{%s}")
663 (string :tag
"Other format string"))
665 :package-version
'(Org .
"9.0"))
669 (defcustom org-latex-active-timestamp-format
"\\textit{%s}"
670 "A printf format string to be applied to active timestamps."
671 :group
'org-export-latex
674 (defcustom org-latex-inactive-timestamp-format
"\\textit{%s}"
675 "A printf format string to be applied to inactive timestamps."
676 :group
'org-export-latex
679 (defcustom org-latex-diary-timestamp-format
"\\textit{%s}"
680 "A printf format string to be applied to diary timestamps."
681 :group
'org-export-latex
687 (defcustom org-latex-images-centered t
688 "When non-nil, images are centered."
689 :group
'org-export-latex
691 :package-version
'(Org .
"9.0")
695 (defcustom org-latex-image-default-option
""
696 "Default option for images."
697 :group
'org-export-latex
699 :package-version
'(Org .
"8.0")
702 (defcustom org-latex-image-default-width
".9\\linewidth"
703 "Default width for images.
704 This value will not be used if a height is provided."
705 :group
'org-export-latex
707 :package-version
'(Org .
"8.0")
710 (defcustom org-latex-image-default-height
""
711 "Default height for images.
712 This value will not be used if a width is provided, or if the
713 image is wrapped within a \"figure\" or \"wrapfigure\"
715 :group
'org-export-latex
717 :package-version
'(Org .
"8.0")
720 (defcustom org-latex-default-figure-position
"htbp"
721 "Default position for LaTeX figures."
722 :group
'org-export-latex
725 :package-version
'(Org .
"9.0")
728 (defcustom org-latex-inline-image-rules
729 '(("file" .
"\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
730 "Rules characterizing image files that can be inlined into LaTeX.
732 A rule consists in an association whose key is the type of link
733 to consider, and value is a regexp that will be matched against
736 Note that, by default, the image extension *actually* allowed
737 depend on the way the LaTeX file is processed. When used with
738 pdflatex, pdf, jpg and png images are OK. When processing
739 through dvi to Postscript, only ps and eps are allowed. The
740 default we use here encompasses both."
741 :group
'org-export-latex
743 :package-version
'(Org .
"8.0")
744 :type
'(alist :key-type
(string :tag
"Type")
745 :value-type
(regexp :tag
"Path")))
747 (defcustom org-latex-link-with-unknown-path-format
"\\texttt{%s}"
748 "Format string for links with unknown path type."
749 :group
'org-export-latex
755 (defcustom org-latex-default-table-environment
"tabular"
756 "Default environment used to build tables."
757 :group
'org-export-latex
759 :package-version
'(Org .
"8.0")
762 (defcustom org-latex-default-table-mode
'table
763 "Default mode for tables.
765 Value can be a symbol among:
767 `table' Regular LaTeX table.
769 `math' In this mode, every cell is considered as being in math
770 mode and the complete table will be wrapped within a math
771 environment. It is particularly useful to write matrices.
773 `inline-math' This mode is almost the same as `math', but the
774 math environment will be inlined.
776 `verbatim' The table is exported as it appears in the Org
777 buffer, within a verbatim environment.
779 This value can be overridden locally with, i.e. \":mode math\" in
782 When modifying this variable, it may be useful to change
783 `org-latex-default-table-environment' accordingly."
784 :group
'org-export-latex
786 :package-version
'(Org .
"8.0")
787 :type
'(choice (const :tag
"Table" table
)
788 (const :tag
"Matrix" math
)
789 (const :tag
"Inline matrix" inline-math
)
790 (const :tag
"Verbatim" verbatim
))
791 :safe
(lambda (s) (memq s
'(table math inline-math verbatim
))))
793 (defcustom org-latex-tables-centered t
794 "When non-nil, tables are exported in a center environment."
795 :group
'org-export-latex
799 (defcustom org-latex-tables-booktabs nil
800 "When non-nil, display tables in a formal \"booktabs\" style.
801 This option assumes that the \"booktabs\" package is properly
802 loaded in the header of the document. This value can be ignored
803 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
805 :group
'org-export-latex
807 :package-version
'(Org .
"8.0")
811 (defcustom org-latex-table-scientific-notation
"%s\\,(%s)"
812 "Format string to display numbers in scientific notation.
813 The format should have \"%s\" twice, for mantissa and exponent
814 \(i.e., \"%s\\\\times10^{%s}\").
816 When nil, no transformation is made."
817 :group
'org-export-latex
819 :package-version
'(Org .
"8.0")
821 (string :tag
"Format string")
822 (const :tag
"No formatting" nil
)))
826 (defcustom org-latex-text-markup-alist
'((bold .
"\\textbf{%s}")
827 (code . protectedtexttt
)
828 (italic .
"\\emph{%s}")
829 (strike-through .
"\\sout{%s}")
830 (underline .
"\\uline{%s}")
831 (verbatim . protectedtexttt
))
832 "Alist of LaTeX expressions to convert text markup.
834 The key must be a symbol among `bold', `code', `italic',
835 `strike-through', `underline' and `verbatim'. The value is
836 a formatting string to wrap fontified text with.
838 Value can also be set to the following symbols: `verb' and
839 `protectedtexttt'. For the former, Org will use \"\\verb\" to
840 create a format string and select a delimiter character that
841 isn't in the string. For the latter, Org will use \"\\texttt\"
842 to typeset and try to protect special characters.
844 If no association can be found for a given markup, text will be
846 :group
'org-export-latex
848 :package-version
'(Org .
"8.3")
850 :options
'(bold code italic strike-through underline verbatim
))
855 (defcustom org-latex-format-drawer-function
(lambda (_ contents
) contents
)
856 "Function called to format a drawer in LaTeX code.
858 The function must accept two parameters:
859 NAME the drawer name, like \"LOGBOOK\"
860 CONTENTS the contents of the drawer.
862 The function should return the string to be exported.
864 The default function simply returns the value of CONTENTS."
865 :group
'org-export-latex
867 :package-version
'(Org .
"8.3")
873 (defcustom org-latex-format-inlinetask-function
874 'org-latex-format-inlinetask-default-function
875 "Function called to format an inlinetask in LaTeX code.
877 The function must accept seven parameters:
878 TODO the todo keyword (string or nil)
879 TODO-TYPE the todo type (symbol: `todo', `done', nil)
880 PRIORITY the inlinetask priority (integer or nil)
881 NAME the inlinetask name (string)
882 TAGS the inlinetask tags (list of strings or nil)
883 CONTENTS the contents of the inlinetask (string or nil)
884 INFO the export options (plist)
886 The function should return the string to be exported."
887 :group
'org-export-latex
890 :package-version
'(Org .
"8.3"))
895 (defcustom org-latex-listings nil
896 "Non-nil means export source code using the listings package.
898 This package will fontify source code, possibly even with color.
899 If you want to use this, you also need to make LaTeX use the
900 listings package, and if you want to have color, the color
901 package. Just add these to `org-latex-packages-alist', for
902 example using customize, or with something like:
904 (require \\='ox-latex)
905 (add-to-list \\='org-latex-packages-alist \\='(\"\" \"listings\"))
906 (add-to-list \\='org-latex-packages-alist \\='(\"\" \"color\"))
910 (setq org-latex-listings \\='minted)
912 causes source code to be exported using the minted package as
913 opposed to listings. If you want to use minted, you need to add
914 the minted package to `org-latex-packages-alist', for example
915 using customize, or with
917 (require \\='ox-latex)
918 (add-to-list \\='org-latex-packages-alist \\='(\"newfloat\" \"minted\"))
920 In addition, it is necessary to install pygments
921 \(http://pygments.org), and to configure the variable
922 `org-latex-pdf-process' so that the -shell-escape option is
925 The minted choice has possible repercussions on the preview of
926 latex fragments (see `org-preview-latex-fragment'). If you run
927 into previewing problems, please consult
929 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
930 :group
'org-export-latex
932 (const :tag
"Use listings" t
)
933 (const :tag
"Use minted" minted
)
934 (const :tag
"Export verbatim" nil
))
935 :safe
(lambda (s) (memq s
'(t nil minted
))))
937 (defcustom org-latex-listings-langs
938 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
941 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
942 (html "HTML") (xml "XML")
943 (tex "TeX") (latex "[LaTeX]TeX")
944 (shell-script "bash")
946 (ocaml "Caml") (caml "Caml")
947 (sql "SQL") (sqlite "sql")
950 "Alist mapping languages to their listing language counterpart.
951 The key is a symbol, the major mode symbol without the \"-mode\".
952 The value is the string that should be inserted as the language
953 parameter for the listings package. If the mode name and the
954 listings name are the same, the language does not need an entry
955 in this list - but it does not hurt if it is present."
956 :group
'org-export-latex
958 :package-version
'(Org .
"8.3")
961 (symbol :tag
"Major mode ")
962 (string :tag
"Listings language"))))
964 (defcustom org-latex-listings-options nil
965 "Association list of options for the latex listings package.
967 These options are supplied as a comma-separated list to the
968 \\lstset command. Each element of the association list should be
969 a list containing two strings: the name of the option, and the
972 (setq org-latex-listings-options
973 \\='((\"basicstyle\" \"\\\\small\")
974 (\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
976 will typeset the code in a small size font with underlined, bold
979 Note that the same options will be applied to blocks of all
980 languages. If you need block-specific options, you may use the
983 #+ATTR_LATEX: :options key1=value1,key2=value2
987 :group
'org-export-latex
990 (string :tag
"Listings option name ")
991 (string :tag
"Listings option value"))))
993 (defcustom org-latex-minted-langs
994 '((emacs-lisp "common-lisp")
997 (shell-script "bash")
999 "Alist mapping languages to their minted language counterpart.
1000 The key is a symbol, the major mode symbol without the \"-mode\".
1001 The value is the string that should be inserted as the language
1002 parameter for the minted package. If the mode name and the
1003 listings name are the same, the language does not need an entry
1004 in this list - but it does not hurt if it is present.
1006 Note that minted uses all lower case for language identifiers,
1007 and that the full list of language identifiers can be obtained
1010 pygmentize -L lexers"
1011 :group
'org-export-latex
1014 (symbol :tag
"Major mode ")
1015 (string :tag
"Minted language"))))
1017 (defcustom org-latex-minted-options nil
1018 "Association list of options for the latex minted package.
1020 These options are supplied within square brackets in
1021 \\begin{minted} environments. Each element of the alist should
1022 be a list containing two strings: the name of the option, and the
1025 (setq org-latex-minted-options
1026 \\='((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
1028 will result in src blocks being exported with
1030 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
1032 as the start of the minted environment. Note that the same
1033 options will be applied to blocks of all languages. If you need
1034 block-specific options, you may use the following syntax:
1036 #+ATTR_LATEX: :options key1=value1,key2=value2
1040 :group
'org-export-latex
1043 (string :tag
"Minted option name ")
1044 (string :tag
"Minted option value"))))
1046 (defcustom org-latex-custom-lang-environments nil
1047 "Alist mapping languages to language-specific LaTeX environments.
1049 It is used during export of src blocks by the listings and minted
1050 latex packages. The environment may be a simple string, composed of
1051 only letters and numbers. In this case, the string is directly the
1052 name of the latex environment to use. The environment may also be
1053 a format string. In this case the format string will be directly
1054 exported. This format string may contain these elements:
1056 %s for the formatted source
1058 %f for the float attribute
1059 %l for an appropriate label
1060 %o for the LaTeX attributes
1064 (setq org-latex-custom-lang-environments
1065 \\='((python \"pythoncode\")
1066 (ocaml \"\\\\begin{listing}
1067 \\\\begin{minted}[%o]{ocaml}
1072 would have the effect that if Org encounters a Python source block
1073 during LaTeX export it will produce
1079 and if Org encounters an Ocaml source block during LaTeX export it
1083 \\begin{minted}[<attr_latex options>]{ocaml}
1086 \\caption{<caption>}
1089 :group
'org-export-latex
1092 (symbol :tag
"Language name ")
1093 (string :tag
"Environment name or format string")))
1095 :package-version
'(Org .
"9.0"))
1100 (defcustom org-latex-compiler-file-string
"%% Intended LaTeX compiler: %s\n"
1101 "LaTeX compiler format-string.
1102 See also `org-latex-compiler'."
1103 :group
'org-export-latex
1105 (const :tag
"Comment" "%% Intended LaTeX compiler: %s\n")
1106 (const :tag
"latex-mode file variable" "%% -*- latex-run-command: %s -*-\n")
1107 (const :tag
"AUCTeX file variable" "%% -*- LaTeX-command: %s -*-\n")
1108 (string :tag
"custom format" "%% %s"))
1110 :package-version
'(Org .
"9.0"))
1112 (defcustom org-latex-compiler
"pdflatex"
1113 "LaTeX compiler to use.
1115 Must be an element in `org-latex-compilers' or the empty quote.
1116 Can also be set in buffers via #+LATEX_COMPILER. See also
1117 `org-latex-compiler-file-string'."
1118 :group
'org-export-latex
1120 (const :tag
"pdfLaTeX" "pdflatex")
1121 (const :tag
"XeLaTeX" "xelatex")
1122 (const :tag
"LuaLaTeX" "lualatex")
1123 (const :tag
"Unset" ""))
1125 :package-version
'(Org .
"9.0"))
1127 (defconst org-latex-compilers
'("pdflatex" "xelatex" "lualatex")
1128 "Known LaTeX compilers.
1129 See also `org-latex-compiler'.")
1131 (defcustom org-latex-bib-compiler
"bibtex"
1132 "Command to process a LaTeX file's bibliography.
1134 The shorthand %bib in `org-latex-pdf-process' is replaced with
1137 A better approach is to use a compiler suit such as `latexmk'."
1138 :group
'org-export-latex
1139 :type
'(choice (const :tag
"BibTeX" "bibtex")
1140 (const :tag
"Biber" "biber")
1141 (string :tag
"Other process"))
1143 :package-version
'(Org .
"9.0"))
1145 (defcustom org-latex-pdf-process
1146 '("%latex -interaction nonstopmode -output-directory %o %f"
1147 "%latex -interaction nonstopmode -output-directory %o %f"
1148 "%latex -interaction nonstopmode -output-directory %o %f")
1149 "Commands to process a LaTeX file to a PDF file.
1151 This is a list of strings, each of them will be given to the
1152 shell as a command. %f in the command will be replaced by the
1153 relative file name, %F by the absolute file name, %b by the file
1154 base name (i.e. without directory and extension parts), %o by the
1155 base directory of the file, %O by the absolute file name of the
1156 output file, %latex is the LaTeX compiler (see
1157 `org-latex-compiler'), and %bib is the BibTeX-like compiler (see
1158 `org-latex-bib-compiler').
1160 The reason why this is a list is that it usually takes several
1161 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
1162 does not have a clever mechanism to detect which of these
1163 commands have to be run to get to a stable result, and it also
1164 does not do any error checking.
1166 Consider a smart LaTeX compiler such as `texi2dvi' or `latexmk',
1167 which calls the \"correct\" combinations of auxiliary programs.
1169 Alternatively, this may be a Lisp function that does the
1170 processing, so you could use this to apply the machinery of
1171 AUCTeX or the Emacs LaTeX mode. This function should accept the
1172 file name as its single argument."
1173 :group
'org-export-pdf
1175 (repeat :tag
"Shell command sequence"
1176 (string :tag
"Shell command"))
1177 (const :tag
"2 runs of latex"
1178 ("%latex -interaction nonstopmode -output-directory %o %f"
1179 "%latex -interaction nonstopmode -output-directory %o %f"))
1180 (const :tag
"3 runs of latex"
1181 ("%latex -interaction nonstopmode -output-directory %o %f"
1182 "%latex -interaction nonstopmode -output-directory %o %f"
1183 "%latex -interaction nonstopmode -output-directory %o %f"))
1184 (const :tag
"latex,bibtex,latex,latex"
1185 ("%latex -interaction nonstopmode -output-directory %o %f"
1187 "%latex -interaction nonstopmode -output-directory %o %f"
1188 "%latex -interaction nonstopmode -output-directory %o %f"))
1189 (const :tag
"texi2dvi"
1190 ("cd %o; LATEX=\"%latex\" texi2dvi -p -b -V %b.tex"))
1191 (const :tag
"latexmk"
1192 ("latexmk -g -pdf -pdflatex=\"%latex\" -outdir=%o %f"))
1195 (defcustom org-latex-logfiles-extensions
1196 '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
1197 "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
1198 "The list of file extensions to consider as LaTeX logfiles.
1199 The logfiles will be removed if `org-latex-remove-logfiles' is
1201 :group
'org-export-latex
1203 :package-version
'(Org .
"8.3")
1204 :type
'(repeat (string :tag
"Extension")))
1206 (defcustom org-latex-remove-logfiles t
1207 "Non-nil means remove the logfiles produced by PDF production.
1208 By default, logfiles are files with these extensions: .aux, .idx,
1209 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
1210 logfiles to remove, set `org-latex-logfiles-extensions'."
1211 :group
'org-export-latex
1214 (defcustom org-latex-known-warnings
1215 '(("Reference.*?undefined" .
"[undefined reference]")
1216 ("Runaway argument" .
"[runaway argument]")
1217 ("Underfull \\hbox" .
"[underfull hbox]")
1218 ("Overfull \\hbox" .
"[overfull hbox]")
1219 ("Citation.*?undefined" .
"[undefined citation]")
1220 ("Undefined control sequence" .
"[undefined control sequence]"))
1221 "Alist of regular expressions and associated messages for the user.
1222 The regular expressions are used to find possible warnings in the
1223 log of a latex-run. These warnings will be reported after
1224 calling `org-latex-compile'."
1225 :group
'org-export-latex
1227 :package-version
'(Org .
"8.3")
1230 (string :tag
"Regexp")
1231 (string :tag
"Message"))))
1235 ;;; Internal Functions
1237 (defun org-latex--caption-above-p (element info
)
1238 "Non nil when caption is expected to be located above ELEMENT.
1239 INFO is a plist holding contextual information."
1240 (let ((above (plist-get info
:latex-caption-above
)))
1241 (if (symbolp above
) above
1242 (let ((type (org-element-type element
)))
1243 (memq (if (eq type
'link
) 'image type
) above
)))))
1245 (defun org-latex--label (datum info
&optional force full
)
1246 "Return an appropriate label for DATUM.
1247 DATUM is an element or a `target' type object. INFO is the
1248 current export state, as a plist.
1250 Return nil if element DATUM has no NAME or VALUE affiliated
1251 keyword or no CUSTOM_ID property, unless FORCE is non-nil. In
1252 this case always return a unique label.
1254 Eventually, if FULL is non-nil, wrap label within \"\\label{}\"."
1255 (let* ((type (org-element-type datum
))
1257 (org-element-property
1259 ((headline inlinetask
) :CUSTOM_ID
)
1264 (and (or user-label force
)
1265 (if (and user-label
(plist-get info
:latex-prefer-user-labels
))
1267 (concat (cl-case type
1271 (and (string-match-p
1272 org-latex-math-environments-re
1273 (org-element-property :value datum
))
1276 (and (org-element-property :caption datum
)
1278 (org-export-get-reference datum info
))))))
1279 (cond ((not full
) label
)
1280 (label (format "\\label{%s}%s"
1282 (if (eq type
'target
) "" "\n")))
1285 (defun org-latex--caption/label-string
(element info
)
1286 "Return caption and label LaTeX string for ELEMENT.
1288 INFO is a plist holding contextual information. If there's no
1289 caption nor label, return the empty string.
1291 For non-floats, see `org-latex--wrap-label'."
1292 (let* ((label (org-latex--label element info nil t
))
1293 (main (org-export-get-caption element
))
1294 (attr (org-export-read-attribute :attr_latex element
))
1295 (type (org-element-type element
))
1296 (nonfloat (or (and (plist-member attr
:float
)
1297 (not (plist-get attr
:float
))
1299 (and (eq type
'src-block
)
1300 (not (plist-get attr
:float
))
1301 (memq (plist-get info
:latex-listings
)
1303 (short (org-export-get-caption element t
))
1304 (caption-from-attr-latex (plist-get attr
:caption
)))
1306 ((org-string-nw-p caption-from-attr-latex
)
1307 (concat caption-from-attr-latex
"\n"))
1308 ((and (not main
) (equal label
"")) "")
1310 ;; Option caption format with short name.
1312 (format (if nonfloat
"\\captionof{%s}%s{%s%s}\n"
1313 "\\caption%s%s{%s%s}\n")
1316 (paragraph "figure")
1317 (src-block (if (plist-get info
:latex-listings
)
1320 (t (symbol-name type
)))
1322 (if short
(format "[%s]" (org-export-data short info
)) "")
1324 (org-export-data main info
))))))
1326 (defun org-latex-guess-inputenc (header)
1327 "Set the coding system in inputenc to what the buffer is.
1329 HEADER is the LaTeX header string. This function only applies
1330 when specified inputenc option is \"AUTO\".
1332 Return the new header, as a string."
1333 (let* ((cs (or (ignore-errors
1334 (latexenc-coding-system-to-inputenc
1335 (or org-export-coding-system buffer-file-coding-system
)))
1338 ;; First translate if that is requested.
1339 (setq cs
(or (cdr (assoc cs org-latex-inputenc-alist
)) cs
))
1340 ;; Then find the \usepackage statement and replace the option.
1341 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1342 cs header t nil
1))))
1344 (defun org-latex-guess-babel-language (header info
)
1345 "Set Babel's language according to LANGUAGE keyword.
1347 HEADER is the LaTeX header string. INFO is the plist used as
1348 a communication channel.
1350 Insertion of guessed language only happens when Babel package has
1351 explicitly been loaded. Then it is added to the rest of
1354 The argument to Babel may be \"AUTO\" which is then replaced with
1355 the language of the document or `org-export-default-language'
1356 unless language in question is already loaded.
1358 Return the new header."
1359 (let ((language-code (plist-get info
:language
)))
1360 ;; If no language is set or Babel package is not loaded, return
1362 (if (or (not (stringp language-code
))
1363 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header
)))
1365 (let ((options (save-match-data
1366 (org-split-string (match-string 1 header
) ",[ \t]*")))
1367 (language (cdr (assoc-string language-code
1368 org-latex-babel-language-alist t
))))
1369 ;; If LANGUAGE is already loaded, return header without AUTO.
1370 ;; Otherwise, replace AUTO with language or append language if
1371 ;; AUTO is not present.
1373 (mapconcat (lambda (option) (if (equal "AUTO" option
) language option
))
1374 (cond ((member language options
) (delete "AUTO" options
))
1375 ((member "AUTO" options
) options
)
1376 (t (append options
(list language
))))
1380 (defun org-latex-guess-polyglossia-language (header info
)
1381 "Set the Polyglossia language according to the LANGUAGE keyword.
1383 HEADER is the LaTeX header string. INFO is the plist used as
1384 a communication channel.
1386 Insertion of guessed language only happens when the Polyglossia
1387 package has been explicitly loaded.
1389 The argument to Polyglossia may be \"AUTO\" which is then
1390 replaced with the language of the document or
1391 `org-export-default-language'. Note, the language is really set
1392 using \setdefaultlanguage and not as an option to the package.
1394 Return the new header."
1395 (let ((language (plist-get info
:language
)))
1396 ;; If no language is set or Polyglossia is not loaded, return
1398 (if (or (not (stringp language
))
1400 "\\\\usepackage\\(?:\\[\\([^]]+?\\)\\]\\){polyglossia}\n"
1403 (let* ((options (org-string-nw-p (match-string 1 header
)))
1404 (languages (and options
1405 ;; Reverse as the last loaded language is
1406 ;; the main language.
1411 (replace-regexp-in-string
1412 "AUTO" language options t
)
1415 (string-match-p "\\\\setmainlanguage{.*?}" header
)))
1417 (concat "\\usepackage{polyglossia}\n"
1420 (let ((l (or (assoc l org-latex-polyglossia-language-alist
)
1422 (format (if main-language-set
"\\setotherlanguage%s{%s}\n"
1423 (setq main-language-set t
)
1424 "\\setmainlanguage%s{%s}\n")
1425 (if (and (consp l
) (= (length l
) 3))
1426 (format "[variant=%s]" (nth 2 l
))
1433 (defun org-latex--remove-packages (pkg-alist info
)
1434 "Remove packages based on the current LaTeX compiler.
1436 If the fourth argument of an element is set in pkg-alist, and it
1437 is not a member of the LaTeX compiler of the document, the packages
1438 is removed. See also `org-latex-compiler'.
1440 Return modified pkg-alist."
1441 (let ((compiler (or (plist-get info
:latex-compiler
) "")))
1442 (if (member-ignore-case compiler org-latex-compilers
)
1448 (let ((third (nth 3 pkg
)))
1450 (not (member-ignore-case
1452 (if (listp third
) third
(list third
)))))))
1457 (defun org-latex--find-verb-separator (s)
1458 "Return a character not used in string S.
1459 This is used to choose a separator for constructs like \\verb."
1460 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1461 (cl-loop for c across ll
1462 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
1463 return
(char-to-string c
))))
1465 (defun org-latex--make-option-string (options)
1466 "Return a comma separated string of keywords and values.
1467 OPTIONS is an alist where the key is the options keyword as
1468 a string, and the value a list containing the keyword value, or
1470 (mapconcat (lambda (pair)
1471 (pcase-let ((`(,keyword
,value
) pair
))
1473 (and (> (length value
) 0)
1474 (concat "=" value
)))))
1478 (defun org-latex--wrap-label (element output info
)
1479 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1480 INFO is the current export state, as a plist. This function
1481 should not be used for floats. See
1482 `org-latex--caption/label-string'."
1483 (if (not (and (org-string-nw-p output
) (org-element-property :name element
)))
1485 (concat (format "\\phantomsection\n\\label{%s}\n"
1486 (org-latex--label element info
))
1489 (defun org-latex--protect-text (text)
1490 "Protect special characters in string TEXT and return it."
1491 (replace-regexp-in-string "[\\{}$%&_#~^]" "\\\\\\&" text
))
1493 (defun org-latex--text-markup (text markup info
)
1494 "Format TEXT depending on MARKUP text markup.
1495 INFO is a plist used as a communication channel. See
1496 `org-latex-text-markup-alist' for details."
1497 (let ((fmt (cdr (assq markup
(plist-get info
:latex-text-markup-alist
)))))
1499 ;; No format string: Return raw text.
1501 ;; Handle the `verb' special case: Find an appropriate separator
1502 ;; and use "\\verb" command.
1504 (let ((separator (org-latex--find-verb-separator text
)))
1507 (replace-regexp-in-string "\n" " " text
)
1509 ;; Handle the `protectedtexttt' special case: Protect some
1510 ;; special chars and use "\texttt{%s}" format string.
1512 (format "\\texttt{%s}"
1513 (replace-regexp-in-string
1514 "--\\|[\\{}$%&_#~^]"
1516 (cond ((equal m
"--") "-{}-")
1517 ((equal m
"\\") "\\textbackslash{}")
1518 ((equal m
"~") "\\textasciitilde{}")
1519 ((equal m
"^") "\\textasciicircum{}")
1520 (t (org-latex--protect-text m
))))
1522 ;; Else use format string.
1523 (t (format fmt text
)))))
1525 (defun org-latex--delayed-footnotes-definitions (element info
)
1526 "Return footnotes definitions in ELEMENT as a string.
1528 INFO is a plist used as a communication channel.
1530 Footnotes definitions are returned within \"\\footnotetext{}\"
1533 This function is used within constructs that don't support
1534 \"\\footnote{}\" command (e.g., an item tag). In that case,
1535 \"\\footnotemark\" is used within the construct and the function
1536 just outside of it."
1539 (let ((def (org-export-get-footnote-definition ref info
)))
1540 (format "\\footnotetext[%d]{%s%s}"
1541 (org-export-get-footnote-number ref info
)
1542 (org-trim (org-latex--label def info t t
))
1543 (org-trim (org-export-data def info
)))))
1544 ;; Find every footnote reference in ELEMENT.
1545 (letrec ((all-refs nil
)
1548 ;; Return a list of all footnote references never seen
1550 (org-element-map data
'footnote-reference
1552 (when (org-export-footnote-first-reference-p ref info
)
1554 (when (eq (org-element-property :type ref
) 'standard
)
1555 (funcall search-refs
1556 (org-export-get-footnote-definition ref info
)))))
1558 (reverse all-refs
))))
1559 (funcall search-refs element
))
1562 (defun org-latex--translate (s info
)
1563 "Translate string S according to specified language.
1564 INFO is a plist used as a communication channel."
1565 (org-export-translate s
:latex info
))
1567 (defun org-latex--format-spec (info)
1568 "Create a format-spec for document meta-data.
1569 INFO is a plist used as a communication channel."
1570 (let ((language (let ((lang (plist-get info
:language
)))
1571 (or (cdr (assoc-string lang org-latex-babel-language-alist t
))
1572 (nth 1 (assoc-string lang org-latex-polyglossia-language-alist t
))
1574 `((?a .
,(org-export-data (plist-get info
:author
) info
))
1575 (?t .
,(org-export-data (plist-get info
:title
) info
))
1576 (?k .
,(org-export-data (org-latex--wrap-latex-math-block
1577 (plist-get info
:keywords
) info
)
1579 (?d .
,(org-export-data (org-latex--wrap-latex-math-block
1580 (plist-get info
:description
) info
)
1582 (?c .
,(plist-get info
:creator
))
1584 (?L .
,(capitalize language
))
1585 (?D .
,(org-export-get-date info
)))))
1587 (defun org-latex--insert-compiler (info)
1588 "Insert LaTeX_compiler info into the document.
1589 INFO is a plist used as a communication channel."
1590 (let ((compiler (plist-get info
:latex-compiler
)))
1591 (and (org-string-nw-p org-latex-compiler-file-string
)
1592 (member (or compiler
"") org-latex-compilers
)
1593 (format org-latex-compiler-file-string compiler
))))
1598 (defun org-latex-matrices-tree-filter (tree _backend info
)
1599 (org-latex--wrap-latex-matrices tree info
))
1601 (defun org-latex-math-block-tree-filter (tree _backend info
)
1602 (org-latex--wrap-latex-math-block tree info
))
1604 (defun org-latex-math-block-options-filter (info _backend
)
1605 (dolist (prop '(:author
:date
:title
) info
)
1606 (plist-put info prop
1607 (org-latex--wrap-latex-math-block (plist-get info prop
) info
))))
1609 (defun org-latex-clean-invalid-line-breaks (data _backend _info
)
1610 (replace-regexp-in-string
1611 "\\(\\end{[A-Za-z0-9*]+}\\|^\\)[ \t]*\\\\\\\\[ \t]*$" "\\1"
1618 (defun org-latex-make-preamble (info &optional template
)
1619 "Return a formatted LaTeX preamble.
1620 INFO is a plist used as a communication channel. Optional
1621 argument TEMPLATE, when non-nil, is the header template string,
1622 as expected by `org-splice-latex-header'."
1623 (let* ((class (plist-get info
:latex-class
))
1624 (class-options (plist-get info
:latex-class-options
))
1625 (header (nth 1 (assoc class
(plist-get info
:latex-classes
))))
1628 (and (stringp header
)
1629 (if (not class-options
) header
1630 (replace-regexp-in-string
1631 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1632 class-options header t nil
1)))
1633 (user-error "Unknown LaTeX class `%s'" class
))))
1634 (org-latex-guess-polyglossia-language
1635 (org-latex-guess-babel-language
1636 (org-latex-guess-inputenc
1637 (org-element-normalize-string
1638 (org-splice-latex-header
1640 (org-latex--remove-packages org-latex-default-packages-alist info
)
1641 (org-latex--remove-packages org-latex-packages-alist info
)
1643 (mapconcat #'org-element-normalize-string
1644 (list (plist-get info
:latex-header
)
1645 (plist-get info
:latex-header-extra
)) ""))))
1649 (defun org-latex-template (contents info
)
1650 "Return complete document string after LaTeX conversion.
1651 CONTENTS is the transcoded contents string. INFO is a plist
1652 holding export options."
1653 (let ((title (org-export-data (plist-get info
:title
) info
))
1654 (spec (org-latex--format-spec info
)))
1657 (and (plist-get info
:time-stamp-file
)
1658 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1660 (org-latex--insert-compiler info
)
1661 ;; Document class and packages.
1662 (org-latex-make-preamble info
)
1663 ;; Possibly limit depth for headline numbering.
1664 (let ((sec-num (plist-get info
:section-numbers
)))
1665 (when (integerp sec-num
)
1666 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
1668 (let ((author (and (plist-get info
:with-author
)
1669 (let ((auth (plist-get info
:author
)))
1670 (and auth
(org-export-data auth info
)))))
1671 (email (and (plist-get info
:with-email
)
1672 (org-export-data (plist-get info
:email
) info
))))
1673 (cond ((and author email
(not (string= "" email
)))
1674 (format "\\author{%s\\thanks{%s}}\n" author email
))
1675 ((or author email
) (format "\\author{%s}\n" (or author email
)))))
1677 (let ((date (and (plist-get info
:with-date
) (org-export-get-date info
))))
1678 (format "\\date{%s}\n" (org-export-data date info
)))
1679 ;; Title and subtitle.
1680 (let* ((subtitle (plist-get info
:subtitle
))
1683 (format (plist-get info
:latex-subtitle-format
)
1684 (org-export-data subtitle info
))))
1685 (separate (plist-get info
:latex-subtitle-separate
)))
1687 (format "\\title{%s%s}\n" title
1688 (if separate
"" (or formatted-subtitle
"")))
1689 (when (and separate subtitle
)
1690 (concat formatted-subtitle
"\n"))))
1691 ;; Hyperref options.
1692 (let ((template (plist-get info
:latex-hyperref-template
)))
1693 (and (stringp template
)
1694 (format-spec template spec
)))
1696 "\\begin{document}\n\n"
1698 (let* ((title-command (plist-get info
:latex-title-command
))
1699 (command (and (stringp title-command
)
1700 (format-spec title-command spec
))))
1701 (org-element-normalize-string
1702 (cond ((not (plist-get info
:with-title
)) nil
)
1703 ((string= "" title
) nil
)
1704 ((not (stringp command
)) nil
)
1705 ((string-match "\\(?:[^%]\\|^\\)%s" command
)
1706 (format command title
))
1708 ;; Table of contents.
1709 (let ((depth (plist-get info
:with-toc
)))
1711 (concat (when (wholenump depth
)
1712 (format "\\setcounter{tocdepth}{%d}\n" depth
))
1713 (plist-get info
:latex-toc-command
))))
1717 (and (plist-get info
:with-creator
)
1718 (concat (plist-get info
:creator
) "\n"))
1720 "\\end{document}")))
1724 ;;; Transcode Functions
1728 (defun org-latex-bold (_bold contents info
)
1729 "Transcode BOLD from Org to LaTeX.
1730 CONTENTS is the text with bold markup. INFO is a plist holding
1731 contextual information."
1732 (org-latex--text-markup contents
'bold info
))
1737 (defun org-latex-center-block (center-block contents info
)
1738 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1739 CONTENTS holds the contents of the center block. INFO is a plist
1740 holding contextual information."
1741 (org-latex--wrap-label
1742 center-block
(format "\\begin{center}\n%s\\end{center}" contents
) info
))
1747 (defun org-latex-clock (clock _contents info
)
1748 "Transcode a CLOCK element from Org to LaTeX.
1749 CONTENTS is nil. INFO is a plist holding contextual
1753 (format "\\textbf{%s} " org-clock-string
)
1754 (format (plist-get info
:latex-inactive-timestamp-format
)
1755 (concat (org-timestamp-translate (org-element-property :value clock
))
1756 (let ((time (org-element-property :duration clock
)))
1757 (and time
(format " (%s)" time
)))))
1763 (defun org-latex-code (code _contents info
)
1764 "Transcode a CODE object from Org to LaTeX.
1765 CONTENTS is nil. INFO is a plist used as a communication
1767 (org-latex--text-markup (org-element-property :value code
) 'code info
))
1772 (defun org-latex-drawer (drawer contents info
)
1773 "Transcode a DRAWER element from Org to LaTeX.
1774 CONTENTS holds the contents of the block. INFO is a plist
1775 holding contextual information."
1776 (let* ((name (org-element-property :drawer-name drawer
))
1777 (output (funcall (plist-get info
:latex-format-drawer-function
)
1779 (org-latex--wrap-label drawer output info
)))
1784 (defun org-latex-dynamic-block (dynamic-block contents info
)
1785 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1786 CONTENTS holds the contents of the block. INFO is a plist
1787 holding contextual information. See `org-export-data'."
1788 (org-latex--wrap-label dynamic-block contents info
))
1793 (defun org-latex-entity (entity _contents _info
)
1794 "Transcode an ENTITY object from Org to LaTeX.
1795 CONTENTS are the definition itself. INFO is a plist holding
1796 contextual information."
1797 (org-element-property :latex entity
))
1802 (defun org-latex-example-block (example-block _contents info
)
1803 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1804 CONTENTS is nil. INFO is a plist holding contextual
1806 (when (org-string-nw-p (org-element-property :value example-block
))
1807 (let ((environment (or (org-export-read-attribute
1808 :attr_latex example-block
:environment
)
1810 (org-latex--wrap-label
1812 (format "\\begin{%s}\n%s\\end{%s}"
1814 (org-export-format-code-default example-block info
)
1821 (defun org-latex-export-block (export-block _contents _info
)
1822 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1823 CONTENTS is nil. INFO is a plist holding contextual information."
1824 (when (member (org-element-property :type export-block
) '("LATEX" "TEX"))
1825 (org-remove-indentation (org-element-property :value export-block
))))
1830 (defun org-latex-export-snippet (export-snippet _contents _info
)
1831 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1832 CONTENTS is nil. INFO is a plist holding contextual information."
1833 (when (eq (org-export-snippet-backend export-snippet
) 'latex
)
1834 (org-element-property :value export-snippet
)))
1839 (defun org-latex-fixed-width (fixed-width _contents info
)
1840 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1841 CONTENTS is nil. INFO is a plist holding contextual information."
1842 (org-latex--wrap-label
1844 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1845 (org-remove-indentation
1846 (org-element-property :value fixed-width
)))
1850 ;;;; Footnote Reference
1852 (defun org-latex-footnote-reference (footnote-reference _contents info
)
1853 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1854 CONTENTS is nil. INFO is a plist holding contextual information."
1855 (let ((label (org-element-property :label footnote-reference
)))
1857 ;; Insert separator between two footnotes in a row.
1858 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1859 (when (eq (org-element-type prev
) 'footnote-reference
)
1860 (plist-get info
:latex-footnote-separator
)))
1862 ;; Use `:latex-footnote-defined-format' if the footnote has
1863 ;; already been defined.
1864 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1865 (format (plist-get info
:latex-footnote-defined-format
)
1867 (org-export-get-footnote-definition footnote-reference info
)
1869 ;; Use \footnotemark if reference is within another footnote
1870 ;; reference, footnote definition or table cell.
1871 ((org-element-lineage footnote-reference
1872 '(footnote-reference footnote-definition table-cell
))
1874 ;; Otherwise, define it with \footnote command.
1876 (let ((def (org-export-get-footnote-definition footnote-reference info
)))
1878 (format "\\footnote{%s%s}" (org-trim (org-export-data def info
))
1879 ;; Only insert a \label if there exist another
1880 ;; reference to def.
1881 (cond ((not label
) "")
1882 ((org-element-map (plist-get info
:parse-tree
) 'footnote-reference
1884 (and (not (eq f footnote-reference
))
1885 (equal (org-element-property :label f
) label
)
1886 (org-trim (org-latex--label def info t t
))))
1889 ;; Retrieve all footnote references within the footnote and
1890 ;; add their definition after it, since LaTeX doesn't support
1892 (org-latex--delayed-footnotes-definitions def info
))))))))
1897 (defun org-latex-headline (headline contents info
)
1898 "Transcode a HEADLINE element from Org to LaTeX.
1899 CONTENTS holds the contents of the headline. INFO is a plist
1900 holding contextual information."
1901 (unless (org-element-property :footnote-section-p headline
)
1902 (let* ((class (plist-get info
:latex-class
))
1903 (level (org-export-get-relative-level headline info
))
1904 (numberedp (org-export-numbered-headline-p headline info
))
1905 (class-sectioning (assoc class
(plist-get info
:latex-classes
)))
1906 ;; Section formatting will set two placeholders: one for
1907 ;; the title and the other for the contents.
1909 (let ((sec (if (functionp (nth 2 class-sectioning
))
1910 (funcall (nth 2 class-sectioning
) level numberedp
)
1911 (nth (1+ level
) class-sectioning
))))
1913 ;; No section available for that LEVEL.
1915 ;; Section format directly returned by a function. Add
1916 ;; placeholder for contents.
1917 ((stringp sec
) (concat sec
"\n%s"))
1918 ;; (numbered-section . unnumbered-section)
1919 ((not (consp (cdr sec
)))
1920 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
1921 ;; (numbered-open numbered-close)
1923 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
1924 ;; (num-in num-out no-num-in no-num-out)
1926 (if numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))
1927 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
1928 ;; Create a temporary export back-end that hard-codes
1929 ;; "\underline" within "\section" and alike.
1931 (org-export-create-backend
1934 '((underline .
(lambda (o c i
) (format "\\underline{%s}" c
))))))
1936 (org-export-data-with-backend
1937 (org-element-property :title headline
) section-back-end info
))
1939 (and (plist-get info
:with-todo-keywords
)
1940 (let ((todo (org-element-property :todo-keyword headline
)))
1941 (and todo
(org-export-data todo info
)))))
1942 (todo-type (and todo
(org-element-property :todo-type headline
)))
1943 (tags (and (plist-get info
:with-tags
)
1944 (org-export-get-tags headline info
)))
1945 (priority (and (plist-get info
:with-priority
)
1946 (org-element-property :priority headline
)))
1947 ;; Create the headline text along with a no-tag version.
1948 ;; The latter is required to remove tags from toc.
1949 (full-text (funcall (plist-get info
:latex-format-headline-function
)
1950 todo todo-type priority text tags info
))
1951 ;; Associate \label to the headline for internal links.
1952 (headline-label (org-latex--label headline info t t
))
1954 (make-string (org-element-property :pre-blank headline
) ?
\n)))
1955 (if (or (not section-fmt
) (org-export-low-level-p headline info
))
1956 ;; This is a deep sub-tree: export it as a list item. Also
1957 ;; export as items headlines for which no section format has
1959 (let ((low-level-body
1961 ;; If headline is the first sibling, start a list.
1962 (when (org-export-first-sibling-p headline info
)
1963 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
1967 (string-match-p "\\`[ \t]*\\[" full-text
)
1973 ;; If headline is not the last sibling simply return
1974 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1976 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1977 (replace-regexp-in-string
1979 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
1981 ;; This is a standard headline. Export it as a section. Add
1982 ;; an alternative heading when possible, and when this is not
1983 ;; identical to the usual heading.
1985 (funcall (plist-get info
:latex-format-headline-function
)
1986 todo todo-type priority
1987 (org-export-data-with-backend
1988 (org-export-get-alt-title headline info
)
1989 section-back-end info
)
1990 (and (eq (plist-get info
:with-tags
) t
) tags
)
1992 ;; Maybe end local TOC (see `org-latex-keyword').
1996 (let ((case-fold-search t
)
1998 (let ((first (car (org-element-contents headline
))))
1999 (and (eq (org-element-type first
) 'section
) first
))))
2000 (org-element-map section
'keyword
2002 (and (equal (org-element-property :key k
) "TOC")
2003 (let ((v (org-element-property :value k
)))
2004 (and (string-match-p "\\<headlines\\>" v
)
2005 (string-match-p "\\<local\\>" v
)
2006 (format "\\stopcontents[level-%d]" level
)))))
2009 (not (equal opt-title full-text
))
2010 (string-match "\\`\\\\\\(.+?\\){" section-fmt
))
2011 (format (replace-match "\\1[%s]" nil nil section-fmt
1)
2012 ;; Replace square brackets with parenthesis
2013 ;; since square brackets are not supported in
2014 ;; optional arguments.
2015 (replace-regexp-in-string
2016 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title
))
2018 (concat headline-label pre-blanks contents
))
2019 ;; Impossible to add an alternative heading. Fallback to
2020 ;; regular sectioning format string.
2021 (format section-fmt full-text
2022 (concat headline-label pre-blanks contents
))))))))
2024 (defun org-latex-format-headline-default-function
2025 (todo _todo-type priority text tags _info
)
2026 "Default format function for a headline.
2027 See `org-latex-format-headline-function' for details."
2029 (and todo
(format "{\\bfseries\\sffamily %s} " todo
))
2030 (and priority
(format "\\framebox{\\#%c} " priority
))
2033 (format "\\hfill{}\\textsc{%s}"
2034 (mapconcat #'org-latex--protect-text tags
":")))))
2037 ;;;; Horizontal Rule
2039 (defun org-latex-horizontal-rule (horizontal-rule _contents info
)
2040 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
2041 CONTENTS is nil. INFO is a plist holding contextual information."
2042 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule
))
2043 (prev (org-export-get-previous-element horizontal-rule info
)))
2045 ;; Make sure the rule doesn't start at the end of the current
2046 ;; line by separating it with a blank line from previous element.
2048 (let ((prev-blank (org-element-property :post-blank prev
)))
2049 (or (not prev-blank
) (zerop prev-blank
))))
2051 (org-latex--wrap-label
2053 (format "\\rule{%s}{%s}"
2054 (or (plist-get attr
:width
) "\\linewidth")
2055 (or (plist-get attr
:thickness
) "0.5pt"))
2059 ;;;; Inline Src Block
2061 (defun org-latex-inline-src-block (inline-src-block _contents info
)
2062 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
2063 CONTENTS holds the contents of the item. INFO is a plist holding
2064 contextual information."
2065 (let* ((code (org-element-property :value inline-src-block
))
2066 (separator (org-latex--find-verb-separator code
)))
2067 (cl-case (plist-get info
:latex-listings
)
2068 ;; Do not use a special package: transcode it verbatim.
2069 ((nil) (format "\\texttt{%s}" (org-latex--text-markup code
'code info
)))
2070 ;; Use minted package.
2072 (let* ((org-lang (org-element-property :language inline-src-block
))
2073 (mint-lang (or (cadr (assq (intern org-lang
)
2074 (plist-get info
:latex-minted-langs
)))
2075 (downcase org-lang
)))
2076 (options (org-latex--make-option-string
2077 (plist-get info
:latex-minted-options
))))
2078 (format "\\mintinline%s{%s}{%s}"
2079 (if (string= options
"") "" (format "[%s]" options
))
2082 ;; Use listings package.
2084 ;; Maybe translate language's name.
2085 (let* ((org-lang (org-element-property :language inline-src-block
))
2086 (lst-lang (or (cadr (assq (intern org-lang
)
2087 (plist-get info
:latex-listings-langs
)))
2089 (options (org-latex--make-option-string
2090 (append (plist-get info
:latex-listings-options
)
2091 `(("language" ,lst-lang
))))))
2092 (concat (format "\\lstinline[%s]" options
)
2093 separator code separator
))))))
2098 (defun org-latex-inlinetask (inlinetask contents info
)
2099 "Transcode an INLINETASK element from Org to LaTeX.
2100 CONTENTS holds the contents of the block. INFO is a plist
2101 holding contextual information."
2102 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
2103 (todo (and (plist-get info
:with-todo-keywords
)
2104 (let ((todo (org-element-property :todo-keyword inlinetask
)))
2105 (and todo
(org-export-data todo info
)))))
2106 (todo-type (org-element-property :todo-type inlinetask
))
2107 (tags (and (plist-get info
:with-tags
)
2108 (org-export-get-tags inlinetask info
)))
2109 (priority (and (plist-get info
:with-priority
)
2110 (org-element-property :priority inlinetask
)))
2111 (contents (concat (org-latex--label inlinetask info
) contents
)))
2112 (funcall (plist-get info
:latex-format-inlinetask-function
)
2113 todo todo-type priority title tags contents info
)))
2115 (defun org-latex-format-inlinetask-default-function
2116 (todo _todo-type priority title tags contents _info
)
2117 "Default format function for a inlinetasks.
2118 See `org-latex-format-inlinetask-function' for details."
2120 (concat (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
2121 (when priority
(format "\\framebox{\\#%c} " priority
))
2124 (format "\\hfill{}\\textsc{:%s:}"
2125 (mapconcat #'org-latex--protect-text tags
":"))))))
2126 (concat "\\begin{center}\n"
2128 "\\begin{minipage}[c]{.6\\textwidth}\n"
2130 (and (org-string-nw-p contents
)
2131 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents
))
2139 (defun org-latex-italic (_italic contents info
)
2140 "Transcode ITALIC from Org to LaTeX.
2141 CONTENTS is the text with italic markup. INFO is a plist holding
2142 contextual information."
2143 (org-latex--text-markup contents
'italic info
))
2148 (defun org-latex-item (item contents info
)
2149 "Transcode an ITEM element from Org to LaTeX.
2150 CONTENTS holds the contents of the item. INFO is a plist holding
2151 contextual information."
2153 (let ((count (org-element-property :counter item
))
2155 ;; Determine level of current item to determine the
2156 ;; correct LaTeX counter to use (enumi, enumii...).
2157 (let ((parent item
) (level 0))
2158 (while (memq (org-element-type
2159 (setq parent
(org-export-get-parent parent
)))
2161 (when (and (eq (org-element-type parent
) 'plain-list
)
2162 (eq (org-element-property :type parent
)
2168 (format "\\setcounter{enum%s}{%s}\n"
2169 (nth (1- level
) '("i" "ii" "iii" "iv"))
2171 (checkbox (cl-case (org-element-property :checkbox item
)
2172 (on "$\\boxtimes$ ")
2174 (trans "$\\boxminus$ ")))
2175 (tag (let ((tag (org-element-property :tag item
)))
2176 ;; Check-boxes must belong to the tag.
2177 (and tag
(format "[{%s}] "
2179 (org-export-data tag info
)))))))
2184 (checkbox (concat " " checkbox
))
2185 ;; Without a tag or a check-box, if CONTENTS starts with
2186 ;; an opening square bracket, add "\relax" to "\item",
2187 ;; unless the brackets comes from an initial export
2188 ;; snippet (i.e. it is inserted willingly by the user).
2190 (string-match-p "\\`[ \t]*\\[" contents
)
2191 (not (let ((e (car (org-element-contents item
))))
2192 (and (eq (org-element-type e
) 'paragraph
)
2193 (let ((o (car (org-element-contents e
))))
2194 (and (eq (org-element-type o
) 'export-snippet
)
2195 (eq (org-export-snippet-backend o
)
2199 (and contents
(org-trim contents
))
2200 ;; If there are footnotes references in tag, be sure to
2201 ;; add their definition at the end of the item. This
2202 ;; workaround is necessary since "\footnote{}" command is
2203 ;; not supported in tags.
2205 (org-latex--delayed-footnotes-definitions
2206 (org-element-property :tag item
) info
)))))
2211 (defun org-latex-keyword (keyword _contents info
)
2212 "Transcode a KEYWORD element from Org to LaTeX.
2213 CONTENTS is nil. INFO is a plist holding contextual information."
2214 (let ((key (org-element-property :key keyword
))
2215 (value (org-element-property :value keyword
)))
2217 ((string= key
"LATEX") value
)
2218 ((string= key
"INDEX") (format "\\index{%s}" value
))
2219 ((string= key
"TOC")
2220 (let ((case-fold-search t
))
2222 ((string-match-p "\\<headlines\\>" value
)
2223 (let* ((localp (string-match-p "\\<local\\>" value
))
2224 (parent (org-element-lineage keyword
'(headline)))
2225 (level (if (not (and localp parent
)) 0
2226 (org-export-get-relative-level parent info
)))
2228 (and (string-match "\\<[0-9]+\\>" value
)
2230 "\\setcounter{tocdepth}{%d}"
2231 (+ (string-to-number (match-string 0 value
)) level
)))))
2232 (if (and localp parent
)
2233 ;; Start local TOC, assuming package "titletoc" is
2235 (format "\\startcontents[level-%d]
2236 \\printcontents[level-%d]{}{0}{%s}"
2237 level level
(or depth
""))
2238 (concat depth
(and depth
"\n") "\\tableofcontents"))))
2239 ((string-match-p "\\<tables\\>" value
) "\\listoftables")
2240 ((string-match-p "\\<listings\\>" value
)
2241 (cl-case (plist-get info
:latex-listings
)
2242 ((nil) "\\listoffigures")
2243 (minted "\\listoflistings")
2244 (otherwise "\\lstlistoflistings")))))))))
2247 ;;;; Latex Environment
2249 (defun org-latex-latex-environment (latex-environment _contents info
)
2250 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
2251 CONTENTS is nil. INFO is a plist holding contextual information."
2252 (when (plist-get info
:with-latex
)
2253 (let ((value (org-remove-indentation
2254 (org-element-property :value latex-environment
))))
2255 (if (not (org-element-property :name latex-environment
)) value
2256 ;; Environment is labeled: label must be within the environment
2257 ;; (otherwise, a reference pointing to that element will count
2258 ;; the section instead).
2261 (goto-char (point-min))
2263 (insert (org-latex--label latex-environment info nil t
))
2264 (buffer-string))))))
2269 (defun org-latex-latex-fragment (latex-fragment _contents _info
)
2270 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
2271 CONTENTS is nil. INFO is a plist holding contextual information."
2272 (let ((value (org-element-property :value latex-fragment
)))
2273 ;; Trim math markers since the fragment is enclosed within
2274 ;; a latex-math-block object anyway.
2275 (cond ((string-match-p "\\`\\$[^$]" value
) (substring value
1 -
1))
2276 ((string-prefix-p "\\(" value
) (substring value
2 -
2))
2282 (defun org-latex-line-break (_line-break _contents _info
)
2283 "Transcode a LINE-BREAK object from Org to LaTeX.
2284 CONTENTS is nil. INFO is a plist holding contextual information."
2290 (defun org-latex--inline-image (link info
)
2291 "Return LaTeX code for an inline image.
2292 LINK is the link pointing to the inline image. INFO is a plist
2293 used as a communication channel."
2294 (let* ((parent (org-export-get-parent-element link
))
2295 (path (let ((raw-path (org-element-property :path link
)))
2296 (if (not (file-name-absolute-p raw-path
)) raw-path
2297 (expand-file-name raw-path
))))
2298 (filetype (file-name-extension path
))
2299 (caption (org-latex--caption/label-string parent info
))
2300 (caption-above-p (org-latex--caption-above-p link info
))
2301 ;; Retrieve latex attributes from the element around.
2302 (attr (org-export-read-attribute :attr_latex parent
))
2303 (float (let ((float (plist-get attr
:float
)))
2304 (cond ((string= float
"wrap") 'wrap
)
2305 ((string= float
"sideways") 'sideways
)
2306 ((string= float
"multicolumn") 'multicolumn
)
2307 ((and (plist-member attr
:float
) (not float
)) 'nonfloat
)
2309 (org-element-property :caption parent
)
2310 (org-string-nw-p (plist-get attr
:caption
)))
2314 (let ((place (plist-get attr
:placement
)))
2316 (place (format "%s" place
))
2317 ((eq float
'wrap
) "{l}{0.5\\textwidth}")
2319 (format "[%s]" (plist-get info
:latex-default-figure-position
)))
2322 (if (plist-member attr
:center
) (plist-get attr
:center
)
2323 (plist-get info
:latex-images-centered
)))
2324 (comment-include (if (plist-get attr
:comment-include
) "%" ""))
2325 ;; It is possible to specify width and height in the
2326 ;; ATTR_LATEX line, and also via default variables.
2327 (width (cond ((plist-get attr
:width
))
2328 ((plist-get attr
:height
) "")
2329 ((eq float
'wrap
) "0.48\\textwidth")
2330 (t (plist-get info
:latex-image-default-width
))))
2331 (height (cond ((plist-get attr
:height
))
2332 ((or (plist-get attr
:width
)
2333 (memq float
'(figure wrap
))) "")
2334 (t (plist-get info
:latex-image-default-height
))))
2335 (options (let ((opt (or (plist-get attr
:options
)
2336 (plist-get info
:latex-image-default-option
))))
2337 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt
)) opt
2338 (match-string 1 opt
))))
2340 (if (member filetype
'("tikz" "pgf"))
2342 ;; - use \input to read in image file.
2343 ;; - if options are present, wrap in a tikzpicture environment.
2344 ;; - if width or height are present, use \resizebox to change
2347 (setq image-code
(format "\\input{%s}" path
))
2348 (when (org-string-nw-p options
)
2350 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
2353 (when (or (org-string-nw-p width
) (org-string-nw-p height
))
2354 (setq image-code
(format "\\resizebox{%s}{%s}{%s}"
2355 (if (org-string-nw-p width
) width
"!")
2356 (if (org-string-nw-p height
) height
"!")
2358 ;; For other images:
2359 ;; - add width and height to options.
2360 ;; - include the image with \includegraphics.
2361 (when (org-string-nw-p width
)
2362 (setq options
(concat options
",width=" width
)))
2363 (when (org-string-nw-p height
)
2364 (setq options
(concat options
",height=" height
)))
2365 (let ((search-option (org-element-property :search-option link
)))
2366 (when (and search-option
2367 (equal filetype
"pdf")
2368 (string-match-p "\\`[0-9]+\\'" search-option
)
2369 (not (string-match-p "page=" options
)))
2370 (setq options
(concat options
",page=" search-option
))))
2372 (format "\\includegraphics%s{%s}"
2373 (cond ((not (org-string-nw-p options
)) "")
2374 ((string-prefix-p "," options
)
2375 (format "[%s]" (substring options
1)))
2376 (t (format "[%s]" options
)))
2378 (when (equal filetype
"svg")
2379 (setq image-code
(replace-regexp-in-string "^\\\\includegraphics"
2383 (setq image-code
(replace-regexp-in-string "\\.svg}"
2387 ;; Return proper string, depending on FLOAT.
2389 (`wrap
(format "\\begin{wrapfigure}%s
2392 %s\\end{wrapfigure}"
2394 (if caption-above-p caption
"")
2395 (if center
"\\centering" "")
2396 comment-include image-code
2397 (if caption-above-p
"" caption
)))
2398 (`sideways
(format "\\begin{sidewaysfigure}
2401 %s\\end{sidewaysfigure}"
2402 (if caption-above-p caption
"")
2403 (if center
"\\centering" "")
2404 comment-include image-code
2405 (if caption-above-p
"" caption
)))
2406 (`multicolumn
(format "\\begin{figure*}%s
2411 (if caption-above-p caption
"")
2412 (if center
"\\centering" "")
2413 comment-include image-code
2414 (if caption-above-p
"" caption
)))
2415 (`figure
(format "\\begin{figure}%s
2420 (if caption-above-p caption
"")
2421 (if center
"\\centering" "")
2422 comment-include image-code
2423 (if caption-above-p
"" caption
)))
2425 (format "\\begin{center}
2428 (if caption-above-p caption
"")
2430 (if caption-above-p
"" caption
)))
2432 (concat (if caption-above-p caption
"")
2434 (if caption-above-p caption
""))))))
2436 (defun org-latex-link (link desc info
)
2437 "Transcode a LINK object from Org to LaTeX.
2439 DESC is the description part of the link, or the empty string.
2440 INFO is a plist holding contextual information. See
2442 (let* ((type (org-element-property :type link
))
2443 (raw-path (org-element-property :path link
))
2444 ;; Ensure DESC really exists, or set it to nil.
2445 (desc (and (not (string= desc
"")) desc
))
2446 (imagep (org-export-inline-image-p
2447 link
(plist-get info
:latex-inline-image-rules
)))
2448 (path (org-latex--protect-text
2449 (cond ((member type
'("http" "https" "ftp" "mailto" "doi"))
2450 (concat type
":" raw-path
))
2451 ((string= type
"file") (org-export-file-uri raw-path
))
2454 ;; Link type is handled by a special function.
2455 ((org-export-custom-protocol-maybe link desc
'latex
))
2457 (imagep (org-latex--inline-image link info
))
2458 ;; Radio link: Transcode target's contents and use them as link's
2460 ((string= type
"radio")
2461 (let ((destination (org-export-resolve-radio-link link info
)))
2462 (if (not destination
) desc
2463 (format "\\hyperref[%s]{%s}"
2464 (org-export-get-reference destination info
)
2466 ;; Links pointing to a headline: Find destination and build
2467 ;; appropriate referencing command.
2468 ((member type
'("custom-id" "fuzzy" "id"))
2469 (let ((destination (if (string= type
"fuzzy")
2470 (org-export-resolve-fuzzy-link link info
)
2471 (org-export-resolve-id-link link info
))))
2472 (cl-case (org-element-type destination
)
2473 ;; Id link points to an external file.
2475 (if desc
(format "\\href{%s}{%s}" destination desc
)
2476 (format "\\url{%s}" destination
)))
2477 ;; Fuzzy link points nowhere.
2479 (format (plist-get info
:latex-link-with-unknown-path-format
)
2482 (org-element-property :raw-link link
) info
))))
2483 ;; LINK points to a headline. If headlines are numbered
2484 ;; and the link has no description, display headline's
2485 ;; number. Otherwise, display description or headline's
2488 (let ((label (org-latex--label destination info t
)))
2490 (org-export-numbered-headline-p destination info
))
2491 (format "\\ref{%s}" label
)
2492 (format "\\hyperref[%s]{%s}" label
2495 (org-element-property :title destination
) info
))))))
2496 ;; Fuzzy link points to a target. Do as above.
2498 (let ((ref (org-latex--label destination info t
)))
2499 (if (not desc
) (format "\\ref{%s}" ref
)
2500 (format "\\hyperref[%s]{%s}" ref desc
)))))))
2501 ;; Coderef: replace link with the reference name or the
2502 ;; equivalent line number.
2503 ((string= type
"coderef")
2504 (format (org-export-get-coderef-format path desc
)
2505 (org-export-resolve-coderef path info
)))
2506 ;; External link with a description part.
2507 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
2508 ;; External link without a description part.
2509 (path (format "\\url{%s}" path
))
2510 ;; No path, only description. Try to do something useful.
2511 (t (format (plist-get info
:latex-link-with-unknown-path-format
) desc
)))))
2516 (defun org-latex-node-property (node-property _contents _info
)
2517 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2518 CONTENTS is nil. INFO is a plist holding contextual
2521 (org-element-property :key node-property
)
2522 (let ((value (org-element-property :value node-property
)))
2523 (if value
(concat " " value
) ""))))
2528 (defun org-latex-paragraph (_paragraph contents _info
)
2529 "Transcode a PARAGRAPH element from Org to LaTeX.
2530 CONTENTS is the contents of the paragraph, as a string. INFO is
2531 the plist used as a communication channel."
2537 (defun org-latex-plain-list (plain-list contents info
)
2538 "Transcode a PLAIN-LIST element from Org to LaTeX.
2539 CONTENTS is the contents of the list. INFO is a plist holding
2540 contextual information."
2541 (let* ((type (org-element-property :type plain-list
))
2542 (attr (org-export-read-attribute :attr_latex plain-list
))
2543 (latex-type (let ((env (plist-get attr
:environment
)))
2544 (cond (env (format "%s" env
))
2545 ((eq type
'ordered
) "enumerate")
2546 ((eq type
'descriptive
) "description")
2548 (org-latex--wrap-label
2550 (format "\\begin{%s}%s\n%s\\end{%s}"
2552 (or (plist-get attr
:options
) "")
2560 (defun org-latex-plain-text (text info
)
2561 "Transcode a TEXT string from Org to LaTeX.
2562 TEXT is the string to transcode. INFO is a plist holding
2563 contextual information."
2564 (let* ((specialp (plist-get info
:with-special-strings
))
2566 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2567 (let ((case-fold-search nil
))
2568 (replace-regexp-in-string
2569 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2570 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2571 ;; However, if special strings are used, be careful not
2572 ;; to protect "\" in "\-" constructs.
2573 (replace-regexp-in-string
2574 (concat "[%$#&{}_~^]\\|\\\\" (and specialp
"\\([^-]\\|$\\)"))
2576 (cl-case (string-to-char m
)
2577 (?
\\ "$\\\\backslash$\\1")
2578 (?~
"\\\\textasciitilde{}")
2582 ;; Activate smart quotes. Be sure to provide original TEXT string
2583 ;; since OUTPUT may have been modified.
2584 (when (plist-get info
:with-smart-quotes
)
2585 (setq output
(org-export-activate-smart-quotes output
:latex info text
)))
2586 ;; Convert special strings.
2588 (setq output
(replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output
)))
2589 ;; Handle break preservation if required.
2590 (when (plist-get info
:preserve-breaks
)
2591 (setq output
(replace-regexp-in-string
2592 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t
)))
2599 (defun org-latex-planning (planning _contents info
)
2600 "Transcode a PLANNING element from Org to LaTeX.
2601 CONTENTS is nil. INFO is a plist holding contextual
2609 (let ((closed (org-element-property :closed planning
)))
2612 (format "\\textbf{%s} " org-closed-string
)
2613 (format (plist-get info
:latex-inactive-timestamp-format
)
2614 (org-timestamp-translate closed
)))))
2615 (let ((deadline (org-element-property :deadline planning
)))
2618 (format "\\textbf{%s} " org-deadline-string
)
2619 (format (plist-get info
:latex-active-timestamp-format
)
2620 (org-timestamp-translate deadline
)))))
2621 (let ((scheduled (org-element-property :scheduled planning
)))
2624 (format "\\textbf{%s} " org-scheduled-string
)
2625 (format (plist-get info
:latex-active-timestamp-format
)
2626 (org-timestamp-translate scheduled
)))))))
2631 ;;;; Property Drawer
2633 (defun org-latex-property-drawer (_property-drawer contents _info
)
2634 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2635 CONTENTS holds the contents of the drawer. INFO is a plist
2636 holding contextual information."
2637 (and (org-string-nw-p contents
)
2638 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents
)))
2641 ;;;; Pseudo Element: LaTeX Matrices
2643 ;; `latex-matrices' elements have the following properties:
2644 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2647 (defun org-latex--wrap-latex-matrices (data info
)
2648 "Merge contiguous tables with the same mode within a pseudo-element.
2649 DATA is a parse tree or a secondary string. INFO is a plist
2650 containing export options. Modify DATA by side-effect and return
2652 (org-element-map data
'table
2654 (when (eq (org-element-property :type table
) 'org
)
2655 (let ((mode (or (org-export-read-attribute :attr_latex table
:mode
)
2656 (plist-get info
:latex-default-table-mode
))))
2657 (when (and (member mode
'("inline-math" "math"))
2658 ;; Do not wrap twice the same table.
2659 (not (eq (org-element-type
2660 (org-element-property :parent table
))
2662 (let* ((caption (and (not (string= mode
"inline-math"))
2663 (org-element-property :caption table
)))
2665 (list 'latex-matrices
2666 (list :caption caption
2668 (cond ((string= mode
"inline-math") 'inline
)
2672 (next (org-export-get-next-element table info
)))
2673 (org-element-insert-before matrices table
)
2674 ;; Swallow all contiguous tables sharing the same mode.
2676 (zerop (or (org-element-property :post-blank previous
) 0))
2677 (setq next
(org-export-get-next-element previous info
))
2678 (eq (org-element-type next
) 'table
)
2679 (eq (org-element-property :type next
) 'org
)
2680 (string= (or (org-export-read-attribute
2681 :attr_latex next
:mode
)
2682 (plist-get info
:latex-default-table-mode
))
2684 (org-element-extract-element previous
)
2685 (org-element-adopt-elements matrices previous
)
2686 (setq previous next
))
2687 ;; Inherit `:post-blank' from the value of the last
2688 ;; swallowed table. Set the latter's `:post-blank'
2689 ;; value to 0 so as to not duplicate empty lines.
2690 (org-element-put-property
2691 matrices
:post-blank
(org-element-property :post-blank previous
))
2692 (org-element-put-property previous
:post-blank
0)
2693 (org-element-extract-element previous
)
2694 (org-element-adopt-elements matrices previous
))))))
2698 (defun org-latex-matrices (matrices contents _info
)
2699 "Transcode a MATRICES element from Org to LaTeX.
2700 CONTENTS is a string. INFO is a plist used as a communication
2702 (format (cl-case (org-element-property :markup matrices
)
2704 (equation "\\begin{equation}\n%s\\end{equation}")
2709 ;;;; Pseudo Object: LaTeX Math Block
2711 ;; `latex-math-block' objects have the following property:
2714 (defun org-latex--wrap-latex-math-block (data info
)
2715 "Merge contiguous math objects in a pseudo-object container.
2716 DATA is a parse tree or a secondary string. INFO is a plist
2717 containing export options. Modify DATA by side-effect and return it."
2718 (let ((valid-object-p
2719 ;; Non-nil when OBJ can be added to the latex math block B.
2721 (pcase (org-element-type obj
)
2722 (`entity
(org-element-property :latex-math-p obj
))
2724 (let ((value (org-element-property :value obj
)))
2725 (or (string-prefix-p "\\(" value
)
2726 (string-match-p "\\`\\$[^$]" value
))))
2727 ((and type
(or `subscript
`superscript
))
2728 (not (memq type
(mapcar #'org-element-type
2729 (org-element-contents b
)))))))))
2730 (org-element-map data
'(entity latex-fragment subscript superscript
)
2732 ;; Skip objects already wrapped.
2733 (when (and (not (eq (org-element-type
2734 (org-element-property :parent object
))
2736 (funcall valid-object-p object nil
))
2737 (let ((math-block (list 'latex-math-block nil
))
2738 (next-elements (org-export-get-next-element object info t
))
2740 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2741 (org-element-insert-before math-block object
)
2742 (org-element-extract-element object
)
2743 (org-element-adopt-elements math-block object
)
2744 (when (zerop (or (org-element-property :post-blank object
) 0))
2745 ;; MATH-BLOCK swallows consecutive math objects.
2747 (dolist (next next-elements
)
2748 (unless (funcall valid-object-p next math-block
)
2750 (org-element-extract-element next
)
2751 (org-element-adopt-elements math-block next
)
2752 ;; Eschew the case: \beta$x$ -> \(\betax\).
2753 (unless (memq (org-element-type next
)
2754 '(subscript superscript
))
2755 (org-element-put-property last
:post-blank
1))
2757 (when (> (or (org-element-property :post-blank next
) 0) 0)
2758 (throw 'exit nil
)))))
2759 (org-element-put-property
2760 math-block
:post-blank
(org-element-property :post-blank last
)))))
2761 info nil
'(subscript superscript latex-math-block
) t
)
2762 ;; Return updated DATA.
2765 (defun org-latex-math-block (_math-block contents _info
)
2766 "Transcode a MATH-BLOCK object from Org to LaTeX.
2767 CONTENTS is a string. INFO is a plist used as a communication
2769 (when (org-string-nw-p contents
)
2770 (format "\\(%s\\)" (org-trim contents
))))
2774 (defun org-latex-quote-block (quote-block contents info
)
2775 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2776 CONTENTS holds the contents of the block. INFO is a plist
2777 holding contextual information."
2778 (org-latex--wrap-label
2779 quote-block
(format "\\begin{quote}\n%s\\end{quote}" contents
) info
))
2784 (defun org-latex-radio-target (radio-target text info
)
2785 "Transcode a RADIO-TARGET object from Org to LaTeX.
2786 TEXT is the text of the target. INFO is a plist holding
2787 contextual information."
2788 (format "\\label{%s}%s" (org-export-get-reference radio-target info
) text
))
2793 (defun org-latex-section (_section contents _info
)
2794 "Transcode a SECTION element from Org to LaTeX.
2795 CONTENTS holds the contents of the section. INFO is a plist
2796 holding contextual information."
2802 (defun org-latex-special-block (special-block contents info
)
2803 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2804 CONTENTS holds the contents of the block. INFO is a plist
2805 holding contextual information."
2806 (let ((type (org-element-property :type special-block
))
2807 (opt (org-export-read-attribute :attr_latex special-block
:options
))
2808 (caption (org-latex--caption/label-string special-block info
))
2809 (caption-above-p (org-latex--caption-above-p special-block info
)))
2810 (concat (format "\\begin{%s}%s\n" type
(or opt
""))
2811 (and caption-above-p caption
)
2813 (and (not caption-above-p
) caption
)
2814 (format "\\end{%s}" type
))))
2819 (defun org-latex-src-block (src-block _contents info
)
2820 "Transcode a SRC-BLOCK element from Org to LaTeX.
2821 CONTENTS holds the contents of the item. INFO is a plist holding
2822 contextual information."
2823 (when (org-string-nw-p (org-element-property :value src-block
))
2824 (let* ((lang (org-element-property :language src-block
))
2825 (caption (org-element-property :caption src-block
))
2826 (caption-above-p (org-latex--caption-above-p src-block info
))
2827 (label (org-element-property :name src-block
))
2828 (custom-env (and lang
2829 (cadr (assq (intern lang
)
2830 org-latex-custom-lang-environments
))))
2831 (num-start (org-export-get-loc src-block info
))
2832 (retain-labels (org-element-property :retain-labels src-block
))
2833 (attributes (org-export-read-attribute :attr_latex src-block
))
2834 (float (plist-get attributes
:float
))
2835 (listings (plist-get info
:latex-listings
)))
2837 ;; Case 1. No source fontification.
2839 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2841 (cond ((string= "multicolumn" float
)
2842 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2843 (plist-get info
:latex-default-figure-position
)
2844 (if caption-above-p caption-str
"")
2845 (if caption-above-p
"" caption-str
)))
2847 (if caption-above-p caption-str
"")
2849 (if caption-above-p
"" (concat "\n" caption-str
))))
2853 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2854 (org-export-format-code-default src-block info
))))))
2855 ;; Case 2. Custom environment.
2857 (let ((caption-str (org-latex--caption/label-string src-block info
))
2858 (formatted-src (org-export-format-code-default src-block info
)))
2859 (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env
)
2860 (format "\\begin{%s}\n%s\\end{%s}\n"
2862 (concat (and caption-above-p caption-str
)
2864 (and (not caption-above-p
) caption-str
))
2866 (format-spec custom-env
2867 `((?s .
,formatted-src
)
2870 (?l .
,(org-latex--label src-block info
))
2871 (?o .
,(or (plist-get attributes
:options
) "")))))))
2872 ;; Case 3. Use minted package.
2873 ((eq listings
'minted
)
2874 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2877 ((string= "multicolumn" float
)
2878 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2879 (if caption-above-p caption-str
"")
2880 (if caption-above-p
"" caption-str
)))
2882 (concat (if caption-above-p caption-str
"")
2884 (if caption-above-p
"" (concat "\n" caption-str
))))
2886 (options (plist-get info
:latex-minted-options
))
2889 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2892 (org-latex--make-option-string
2893 (if (or (not num-start
) (assoc "linenos" options
))
2897 ("firstnumber" ,(number-to-string (1+ num-start
))))
2899 (let ((local-options (plist-get attributes
:options
)))
2900 (and local-options
(concat "," local-options
))))
2902 (or (cadr (assq (intern lang
)
2903 (plist-get info
:latex-minted-langs
)))
2906 (let* ((code-info (org-export-unravel-code src-block
))
2910 (org-split-string (car code-info
)
2912 (org-export-format-code
2914 (lambda (loc _num ref
)
2918 ;; Ensure references are flushed to the right,
2919 ;; separated with 6 spaces from the widest line
2921 (concat (make-string (+ (- max-width
(length loc
)) 6)
2923 (format "(%s)" ref
)))))
2924 nil
(and retain-labels
(cdr code-info
)))))))
2926 (format float-env body
)))
2927 ;; Case 4. Use listings package.
2930 (or (cadr (assq (intern lang
)
2931 (plist-get info
:latex-listings-langs
)))
2935 (let ((main (org-export-get-caption src-block
))
2936 (secondary (org-export-get-caption src-block t
)))
2938 (format "{%s}" (org-export-data main info
))
2940 (org-export-data secondary info
)
2941 (org-export-data main info
))))))
2942 (lst-opt (plist-get info
:latex-listings-options
)))
2948 (org-latex--make-option-string
2952 ((and (not float
) (plist-member attributes
:float
)) nil
)
2953 ((string= "multicolumn" float
) '(("float" "*")))
2954 ((and float
(not (assoc "float" lst-opt
)))
2955 `(("float" ,(plist-get info
:latex-default-figure-position
)))))
2956 `(("language" ,lst-lang
))
2958 `(("label" ,(org-latex--label src-block info
)))
2960 (if caption-str
`(("caption" ,caption-str
)) '(("caption" " ")))
2961 `(("captionpos" ,(if caption-above-p
"t" "b")))
2962 (cond ((assoc "numbers" lst-opt
) nil
)
2963 ((not num-start
) '(("numbers" "none")))
2964 (t `(("firstnumber" ,(number-to-string (1+ num-start
)))
2965 ("numbers" "left"))))))
2966 (let ((local-options (plist-get attributes
:options
)))
2967 (and local-options
(concat "," local-options
)))))
2970 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2971 (let* ((code-info (org-export-unravel-code src-block
))
2975 (org-split-string (car code-info
) "\n")))))
2976 (org-export-format-code
2978 (lambda (loc _num ref
)
2982 ;; Ensure references are flushed to the right,
2983 ;; separated with 6 spaces from the widest line of
2985 (concat (make-string (+ (- max-width
(length loc
)) 6) ?\s
)
2986 (format "(%s)" ref
)))))
2987 nil
(and retain-labels
(cdr code-info
))))))))))))
2990 ;;;; Statistics Cookie
2992 (defun org-latex-statistics-cookie (statistics-cookie _contents _info
)
2993 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2994 CONTENTS is nil. INFO is a plist holding contextual information."
2995 (replace-regexp-in-string
2996 "%" "\\%" (org-element-property :value statistics-cookie
) nil t
))
3001 (defun org-latex-strike-through (_strike-through contents info
)
3002 "Transcode STRIKE-THROUGH from Org to LaTeX.
3003 CONTENTS is the text with strike-through markup. INFO is a plist
3004 holding contextual information."
3005 (org-latex--text-markup contents
'strike-through info
))
3010 (defun org-latex--script-size (object info
)
3011 "Transcode a subscript or superscript object.
3012 OBJECT is an Org object. INFO is a plist used as a communication
3015 (org-element-map (org-element-contents object
)
3016 (cons 'plain-text org-element-all-objects
)
3018 (cl-case (org-element-type obj
)
3019 ((entity latex-fragment
)
3020 (let ((data (org-trim (org-export-data obj info
))))
3022 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
3026 (match-string 1 data
)
3027 (let ((blank (org-element-property :post-blank obj
)))
3028 (and blank
(> blank
0) "\\ "))))))
3031 (format "%s\\text{%s}" output
(org-export-data obj info
))))
3035 (org-export-data obj info
)
3036 (let ((blank (org-element-property :post-blank obj
)))
3037 (and blank
(> blank
0) "\\ ")))))))
3038 info nil org-element-recursive-objects
)
3039 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
3041 (concat (if (eq (org-element-type object
) 'subscript
) "_" "^")
3042 (and (> (length output
) 1) "{")
3044 (and (> (length output
) 1) "}"))))
3046 (defun org-latex-subscript (subscript _contents info
)
3047 "Transcode a SUBSCRIPT object from Org to LaTeX.
3048 CONTENTS is the contents of the object. INFO is a plist holding
3049 contextual information."
3050 (org-latex--script-size subscript info
))
3055 (defun org-latex-superscript (superscript _contents info
)
3056 "Transcode a SUPERSCRIPT object from Org to LaTeX.
3057 CONTENTS is the contents of the object. INFO is a plist holding
3058 contextual information."
3059 (org-latex--script-size superscript info
))
3064 ;; `org-latex-table' is the entry point for table transcoding. It
3065 ;; takes care of tables with a "verbatim" mode. Otherwise, it
3066 ;; delegates the job to either `org-latex--table.el-table',
3067 ;; `org-latex--org-table' or `org-latex--math-table' functions,
3068 ;; depending of the type of the table and the mode requested.
3070 ;; `org-latex--align-string' is a subroutine used to build alignment
3071 ;; string for Org tables.
3073 (defun org-latex-table (table contents info
)
3074 "Transcode a TABLE element from Org to LaTeX.
3075 CONTENTS is the contents of the table. INFO is a plist holding
3076 contextual information."
3077 (if (eq (org-element-property :type table
) 'table.el
)
3078 ;; "table.el" table. Convert it using appropriate tools.
3079 (org-latex--table.el-table table info
)
3080 (let ((type (or (org-export-read-attribute :attr_latex table
:mode
)
3081 (plist-get info
:latex-default-table-mode
))))
3083 ;; Case 1: Verbatim table.
3084 ((string= type
"verbatim")
3085 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
3086 ;; Re-create table, without affiliated keywords.
3087 (org-trim (org-element-interpret-data
3088 `(table nil
,@(org-element-contents table
))))))
3090 ((or (string= type
"math") (string= type
"inline-math"))
3091 (org-latex--math-table table info
))
3092 ;; Case 3: Standard table.
3093 (t (concat (org-latex--org-table table contents info
)
3094 ;; When there are footnote references within the
3095 ;; table, insert their definition just after it.
3096 (org-latex--delayed-footnotes-definitions table info
)))))))
3098 (defun org-latex--align-string (table info
)
3099 "Return an appropriate LaTeX alignment string.
3100 TABLE is the considered table. INFO is a plist used as
3101 a communication channel."
3102 (or (org-export-read-attribute :attr_latex table
:align
)
3104 ;; Extract column groups and alignment from first (non-rule)
3107 (org-element-map table
'table-row
3109 (and (eq (org-element-property :type row
) 'standard
) row
))
3113 (let ((borders (org-export-table-cell-borders cell info
)))
3114 ;; Check left border for the first cell only.
3115 (when (and (memq 'left borders
) (not align
))
3117 (push (cl-case (org-export-table-cell-alignment cell info
)
3122 (when (memq 'right borders
) (push "|" align
))))
3124 (apply 'concat
(nreverse align
)))))
3126 (defun org-latex--org-table (table contents info
)
3127 "Return appropriate LaTeX code for an Org table.
3129 TABLE is the table type element to transcode. CONTENTS is its
3130 contents, as a string. INFO is a plist used as a communication
3133 This function assumes TABLE has `org' as its `:type' property and
3134 `table' as its `:mode' attribute."
3135 (let* ((caption (org-latex--caption/label-string table info
))
3136 (attr (org-export-read-attribute :attr_latex table
))
3137 ;; Determine alignment string.
3138 (alignment (org-latex--align-string table info
))
3139 ;; Determine environment for the table: longtable, tabular...
3140 (table-env (or (plist-get attr
:environment
)
3141 (plist-get info
:latex-default-table-environment
)))
3142 ;; If table is a float, determine environment: table, table*
3143 ;; or sidewaystable.
3144 (float-env (unless (member table-env
'("longtable" "longtabu"))
3145 (let ((float (plist-get attr
:float
)))
3147 ((and (not float
) (plist-member attr
:float
)) nil
)
3148 ((or (string= float
"sidewaystable")
3149 (string= float
"sideways")) "sidewaystable")
3150 ((string= float
"multicolumn") "table*")
3152 (org-element-property :caption table
)
3153 (org-string-nw-p (plist-get attr
:caption
)))
3155 ;; Extract others display options.
3156 (fontsize (let ((font (plist-get attr
:font
)))
3157 (and font
(concat font
"\n"))))
3158 ;; "tabular" environment doesn't allow to define a width.
3159 (width (and (not (equal table-env
"tabular")) (plist-get attr
:width
)))
3160 (spreadp (plist-get attr
:spread
))
3162 (or (plist-get attr
:placement
)
3163 (format "[%s]" (plist-get info
:latex-default-figure-position
))))
3164 (centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
3165 (plist-get info
:latex-tables-centered
)))
3166 (caption-above-p (org-latex--caption-above-p table info
)))
3167 ;; Prepare the final format string for the table.
3170 ((equal "longtable" table-env
)
3171 (concat (and fontsize
(concat "{" fontsize
))
3172 (format "\\begin{longtable}{%s}\n" alignment
)
3173 (and caption-above-p
3174 (org-string-nw-p caption
)
3175 (concat caption
"\\\\\n"))
3177 (and (not caption-above-p
)
3178 (org-string-nw-p caption
)
3179 (concat caption
"\\\\\n"))
3180 "\\end{longtable}\n"
3181 (and fontsize
"}")))
3183 ((equal "longtabu" table-env
)
3184 (concat (and fontsize
(concat "{" fontsize
))
3185 (format "\\begin{longtabu}%s{%s}\n"
3188 (if spreadp
"spread" "to") width
) "")
3190 (and caption-above-p
3191 (org-string-nw-p caption
)
3192 (concat caption
"\\\\\n"))
3194 (and (not caption-above-p
)
3195 (org-string-nw-p caption
)
3196 (concat caption
"\\\\\n"))
3198 (and fontsize
"}")))
3202 (concat (format "\\begin{%s}%s\n" float-env placement
)
3203 (if caption-above-p caption
"")
3204 (when centerp
"\\centering\n")
3206 ((and (not float-env
) caption
)
3208 (and centerp
"\\begin{center}\n" )
3209 (if caption-above-p caption
"")
3210 (cond ((and fontsize centerp
) fontsize
)
3211 (fontsize (concat "{" fontsize
)))))
3212 (centerp (concat "\\begin{center}\n" fontsize
))
3213 (fontsize (concat "{" fontsize
)))
3214 (cond ((equal "tabu" table-env
)
3215 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
3217 (if spreadp
" spread %s " " to %s ")
3221 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
3223 (if width
(format "{%s}" width
) "")
3229 (concat (if caption-above-p
"" (concat "\n" caption
))
3230 (format "\n\\end{%s}" float-env
)))
3231 ((and (not float-env
) caption
)
3233 (if caption-above-p
"" (concat "\n" caption
))
3234 (and centerp
"\n\\end{center}")
3235 (and fontsize
(not centerp
) "}")))
3236 (centerp "\n\\end{center}")
3237 (fontsize "}")))))))
3239 (defun org-latex--table.el-table
(table info
)
3240 "Return appropriate LaTeX code for a table.el table.
3242 TABLE is the table type element to transcode. INFO is a plist
3243 used as a communication channel.
3245 This function assumes TABLE has `table.el' as its `:type'
3248 ;; Ensure "*org-export-table*" buffer is empty.
3249 (with-current-buffer (get-buffer-create "*org-export-table*")
3251 (let ((output (with-temp-buffer
3252 (insert (org-element-property :value table
))
3254 (re-search-forward "^[ \t]*|[^|]" nil t
)
3255 (table-generate-source 'latex
"*org-export-table*")
3256 (with-current-buffer "*org-export-table*"
3257 (org-trim (buffer-string))))))
3258 (kill-buffer (get-buffer "*org-export-table*"))
3259 ;; Remove left out comments.
3260 (while (string-match "^%.*\n" output
)
3261 (setq output
(replace-match "" t t output
)))
3262 (let ((attr (org-export-read-attribute :attr_latex table
)))
3263 (when (plist-get attr
:rmlines
)
3264 ;; When the "rmlines" attribute is provided, remove all hlines
3265 ;; but the the one separating heading from the table body.
3266 (let ((n 0) (pos 0))
3267 (while (and (< (length output
) pos
)
3268 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
3270 (unless (= n
2) (setq output
(replace-match "" nil nil output
))))))
3271 (let ((centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
3272 (plist-get info
:latex-tables-centered
))))
3273 (if (not centerp
) output
3274 (format "\\begin{center}\n%s\n\\end{center}" output
))))))
3276 (defun org-latex--math-table (table info
)
3277 "Return appropriate LaTeX code for a matrix.
3279 TABLE is the table type element to transcode. INFO is a plist
3280 used as a communication channel.
3282 This function assumes TABLE has `org' as its `:type' property and
3283 `inline-math' or `math' as its `:mode' attribute."
3284 (let* ((attr (org-export-read-attribute :attr_latex table
))
3285 (env (or (plist-get attr
:environment
)
3286 (plist-get info
:latex-default-table-environment
)))
3290 ;; Ignore horizontal rules.
3291 (when (eq (org-element-property :type row
) 'standard
)
3292 ;; Return each cell unmodified.
3296 (substring (org-element-interpret-data cell
) 0 -
1))
3297 (org-element-map row
'table-cell
#'identity info
) "&")
3298 (or (cdr (assoc env org-latex-table-matrix-macros
)) "\\\\")
3300 (org-element-map table
'table-row
#'identity info
) "")))
3303 (plist-get attr
:math-prefix
)
3304 ;; Environment. Also treat special cases.
3305 (cond ((member env
'("array" "tabular"))
3306 ;; Make sure cells are always centered while preserving
3307 ;; vertical separators.
3308 (let ((align (replace-regexp-in-string
3309 "[lr]" "c" (org-latex--align-string table info
))))
3310 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env
)))
3311 ((assoc env org-latex-table-matrix-macros
)
3312 (format "\\%s%s{\n%s}"
3314 (or (plist-get attr
:math-arguments
) "")
3316 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env
)))
3318 (plist-get attr
:math-suffix
))))
3323 (defun org-latex-table-cell (table-cell contents info
)
3324 "Transcode a TABLE-CELL element from Org to LaTeX.
3325 CONTENTS is the cell contents. INFO is a plist used as
3326 a communication channel."
3328 (let ((scientific-format (plist-get info
:latex-table-scientific-notation
)))
3331 (string-match orgtbl-exp-regexp contents
))
3332 ;; Use appropriate format string for scientific
3334 (format scientific-format
3335 (match-string 1 contents
)
3336 (match-string 2 contents
))
3338 (when (org-export-get-next-element table-cell info
) " & ")))
3343 (defun org-latex-table-row (table-row contents info
)
3344 "Transcode a TABLE-ROW element from Org to LaTeX.
3345 CONTENTS is the contents of the row. INFO is a plist used as
3346 a communication channel."
3347 (let* ((attr (org-export-read-attribute :attr_latex
3348 (org-export-get-parent table-row
)))
3349 (booktabsp (if (plist-member attr
:booktabs
) (plist-get attr
:booktabs
)
3350 (plist-get info
:latex-tables-booktabs
)))
3352 (member (or (plist-get attr
:environment
)
3353 (plist-get info
:latex-default-table-environment
))
3354 '("longtable" "longtabu"))))
3355 (if (eq (org-element-property :type table-row
) 'rule
)
3357 ((not booktabsp
) "\\hline")
3358 ((not (org-export-get-previous-element table-row info
)) "\\toprule")
3359 ((not (org-export-get-next-element table-row info
)) "\\bottomrule")
3361 (org-export-table-row-ends-header-p
3362 (org-export-get-previous-element table-row info
) info
))
3366 ;; When BOOKTABS are activated enforce top-rule even when no
3367 ;; hline was specifically marked.
3368 (and booktabsp
(not (org-export-get-previous-element table-row info
))
3372 ;; Special case for long tables. Define header and footers.
3373 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
3374 (let ((columns (cdr (org-export-table-dimensions
3375 (org-export-get-parent-table table-row
) info
))))
3378 \\multicolumn{%d}{l}{%s} \\\\
3383 %s\\multicolumn{%d}{r}{%s} \\\\
3386 (if booktabsp
"\\midrule" "\\hline")
3388 (org-latex--translate "Continued from previous page" info
)
3390 ((not (org-export-table-row-starts-header-p table-row info
))
3392 (booktabsp "\\toprule\n")
3395 (if booktabsp
"\\midrule" "\\hline")
3396 (if booktabsp
"\\midrule" "\\hline")
3398 (org-latex--translate "Continued on next page" info
))))
3399 ;; When BOOKTABS are activated enforce bottom rule even when
3400 ;; no hline was specifically marked.
3401 ((and booktabsp
(not (org-export-get-next-element table-row info
)))
3402 "\\bottomrule"))))))
3407 (defun org-latex-target (target _contents info
)
3408 "Transcode a TARGET object from Org to LaTeX.
3409 CONTENTS is nil. INFO is a plist holding contextual
3411 (format "\\label{%s}" (org-latex--label target info
)))
3416 (defun org-latex-timestamp (timestamp _contents info
)
3417 "Transcode a TIMESTAMP object from Org to LaTeX.
3418 CONTENTS is nil. INFO is a plist holding contextual
3420 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp
) info
)))
3423 (cl-case (org-element-property :type timestamp
)
3424 ((active active-range
) :latex-active-timestamp-format
)
3425 ((inactive inactive-range
) :latex-inactive-timestamp-format
)
3426 (otherwise :latex-diary-timestamp-format
)))
3432 (defun org-latex-underline (_underline contents info
)
3433 "Transcode UNDERLINE from Org to LaTeX.
3434 CONTENTS is the text with underline markup. INFO is a plist
3435 holding contextual information."
3436 (org-latex--text-markup contents
'underline info
))
3441 (defun org-latex-verbatim (verbatim _contents info
)
3442 "Transcode a VERBATIM object from Org to LaTeX.
3443 CONTENTS is nil. INFO is a plist used as a communication
3445 (org-latex--text-markup
3446 (org-element-property :value verbatim
) 'verbatim info
))
3451 (defun org-latex-verse-block (verse-block contents info
)
3452 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3453 CONTENTS is verse block contents. INFO is a plist holding
3454 contextual information."
3455 (org-latex--wrap-label
3457 ;; In a verse environment, add a line break to each newline
3458 ;; character and change each white space at beginning of a line
3459 ;; into a space of 1 em. Also change each blank line with
3460 ;; a vertical space of 1 em.
3461 (format "\\begin{verse}\n%s\\end{verse}"
3462 (replace-regexp-in-string
3463 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m
)))
3464 (replace-regexp-in-string
3465 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3466 (replace-regexp-in-string
3467 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3468 contents nil t
) nil t
) nil t
))
3473 ;;; End-user functions
3476 (defun org-latex-export-as-latex
3477 (&optional async subtreep visible-only body-only ext-plist
)
3478 "Export current buffer as a LaTeX buffer.
3480 If narrowing is active in the current buffer, only export its
3483 If a region is active, export that region.
3485 A non-nil optional argument ASYNC means the process should happen
3486 asynchronously. The resulting buffer should be accessible
3487 through the `org-export-stack' interface.
3489 When optional argument SUBTREEP is non-nil, export the sub-tree
3490 at point, extracting information from the headline properties
3493 When optional argument VISIBLE-ONLY is non-nil, don't export
3494 contents of hidden elements.
3496 When optional argument BODY-ONLY is non-nil, only write code
3497 between \"\\begin{document}\" and \"\\end{document}\".
3499 EXT-PLIST, when provided, is a property list with external
3500 parameters overriding Org default settings, but still inferior to
3501 file-local settings.
3503 Export is done in a buffer named \"*Org LATEX Export*\", which
3504 will be displayed when `org-export-show-temporary-export-buffer'
3507 (org-export-to-buffer 'latex
"*Org LATEX Export*"
3508 async subtreep visible-only body-only ext-plist
(lambda () (LaTeX-mode))))
3511 (defun org-latex-convert-region-to-latex ()
3512 "Assume the current region has Org syntax, and convert it to LaTeX.
3513 This can be used in any buffer. For example, you can write an
3514 itemized list in Org syntax in an LaTeX buffer and use this
3515 command to convert it."
3517 (org-export-replace-region-by 'latex
))
3520 (defun org-latex-export-to-latex
3521 (&optional async subtreep visible-only body-only ext-plist
)
3522 "Export current buffer to a LaTeX file.
3524 If narrowing is active in the current buffer, only export its
3527 If a region is active, export that region.
3529 A non-nil optional argument ASYNC means the process should happen
3530 asynchronously. The resulting file should be accessible through
3531 the `org-export-stack' interface.
3533 When optional argument SUBTREEP is non-nil, export the sub-tree
3534 at point, extracting information from the headline properties
3537 When optional argument VISIBLE-ONLY is non-nil, don't export
3538 contents of hidden elements.
3540 When optional argument BODY-ONLY is non-nil, only write code
3541 between \"\\begin{document}\" and \"\\end{document}\".
3543 EXT-PLIST, when provided, is a property list with external
3544 parameters overriding Org default settings, but still inferior to
3545 file-local settings."
3547 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
3548 (org-export-to-file 'latex outfile
3549 async subtreep visible-only body-only ext-plist
)))
3552 (defun org-latex-export-to-pdf
3553 (&optional async subtreep visible-only body-only ext-plist
)
3554 "Export current buffer to LaTeX then process through to PDF.
3556 If narrowing is active in the current buffer, only export its
3559 If a region is active, export that region.
3561 A non-nil optional argument ASYNC means the process should happen
3562 asynchronously. The resulting file should be accessible through
3563 the `org-export-stack' interface.
3565 When optional argument SUBTREEP is non-nil, export the sub-tree
3566 at point, extracting information from the headline properties
3569 When optional argument VISIBLE-ONLY is non-nil, don't export
3570 contents of hidden elements.
3572 When optional argument BODY-ONLY is non-nil, only write code
3573 between \"\\begin{document}\" and \"\\end{document}\".
3575 EXT-PLIST, when provided, is a property list with external
3576 parameters overriding Org default settings, but still inferior to
3577 file-local settings.
3579 Return PDF file's name."
3581 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
3582 (org-export-to-file 'latex outfile
3583 async subtreep visible-only body-only ext-plist
3584 (lambda (file) (org-latex-compile file
)))))
3586 (defun org-latex-compile (texfile &optional snippet
)
3587 "Compile a TeX file.
3589 TEXFILE is the name of the file being compiled. Processing is
3590 done through the command specified in `org-latex-pdf-process',
3591 which see. Output is redirected to \"*Org PDF LaTeX Output*\"
3594 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3595 file used to preview a LaTeX snippet. In this case, do not
3596 create a log buffer and do not remove log files.
3598 Return PDF file name or raise an error if it couldn't be
3600 (unless snippet
(message "Processing LaTeX file %s..." texfile
))
3602 (or (with-temp-buffer
3603 (save-excursion (insert-file-contents texfile
))
3604 (and (search-forward-regexp (regexp-opt org-latex-compilers
)
3605 (line-end-position 2)
3607 (progn (beginning-of-line) (looking-at-p "%"))
3610 (process (if (functionp org-latex-pdf-process
) org-latex-pdf-process
3611 ;; Replace "%latex" and "%bibtex" with,
3612 ;; respectively, "%L" and "%B" so as to adhere to
3613 ;; `format-spec' specifications.
3614 (mapcar (lambda (command)
3615 (replace-regexp-in-string
3616 "%\\(?:bib\\|la\\)tex\\>"
3617 (lambda (m) (upcase (substring m
0 2)))
3619 org-latex-pdf-process
)))
3620 (spec `((?B .
,(shell-quote-argument org-latex-bib-compiler
))
3621 (?L .
,(shell-quote-argument compiler
))))
3622 (log-buf-name "*Org PDF LaTeX Output*")
3623 (log-buf (and (not snippet
) (get-buffer-create log-buf-name
)))
3624 (outfile (org-compile-file texfile process
"pdf"
3625 (format "See %S for details" log-buf-name
)
3628 (when org-latex-remove-logfiles
3631 (file-name-directory outfile
)
3633 (concat (regexp-quote (file-name-base outfile
))
3634 "\\(?:\\.[0-9]+\\)?\\."
3635 (regexp-opt org-latex-logfiles-extensions
))
3637 (let ((warnings (org-latex--collect-warnings log-buf
)))
3638 (message (concat "PDF file produced"
3640 ((eq warnings
'error
) " with errors.")
3641 (warnings (concat " with warnings: " warnings
))
3643 ;; Return output file name.
3646 (defun org-latex--collect-warnings (buffer)
3647 "Collect some warnings from \"pdflatex\" command output.
3648 BUFFER is the buffer containing output. Return collected
3649 warnings types as a string, `error' if a LaTeX error was
3650 encountered or nil if there was none."
3651 (with-current-buffer buffer
3653 (goto-char (point-max))
3654 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t
)
3655 (if (re-search-forward "^!" nil t
) 'error
3656 (let ((case-fold-search t
)
3658 (dolist (warning org-latex-known-warnings
)
3659 (when (save-excursion (re-search-forward (car warning
) nil t
))
3660 (setq warnings
(concat warnings
" " (cdr warning
)))))
3661 (org-string-nw-p (org-trim warnings
))))))))
3664 (defun org-latex-publish-to-latex (plist filename pub-dir
)
3665 "Publish an Org file to LaTeX.
3667 FILENAME is the filename of the Org file to be published. PLIST
3668 is the property list for the given project. PUB-DIR is the
3669 publishing directory.
3671 Return output file name."
3672 (org-publish-org-to 'latex filename
".tex" plist pub-dir
))
3675 (defun org-latex-publish-to-pdf (plist filename pub-dir
)
3676 "Publish an Org file to PDF (via LaTeX).
3678 FILENAME is the filename of the Org file to be published. PLIST
3679 is the property list for the given project. PUB-DIR is the
3680 publishing directory.
3682 Return output file name."
3683 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3684 ;; in working directory and then moved to publishing directory.
3685 (org-publish-attachment
3689 'latex filename
".tex" plist
(file-name-directory filename
)))
3696 ;; generated-autoload-file: "org-loaddefs.el"
3699 ;;; ox-latex.el ends here