1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2015 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; See Org manual for details.
29 (eval-when-compile (require 'cl
))
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
))
93 :export-block
'("LATEX" "TEX")
95 '(?l
"Export to LaTeX"
96 ((?L
"As LaTeX buffer" org-latex-export-as-latex
)
97 (?l
"As LaTeX file" org-latex-export-to-latex
)
98 (?p
"As PDF file" org-latex-export-to-pdf
)
99 (?o
"As PDF file and open"
101 (if a
(org-latex-export-to-pdf t s v b
)
102 (org-open-file (org-latex-export-to-pdf nil s v b
)))))))
103 :filters-alist
'((:filter-options . org-latex-math-block-options-filter
)
104 (:filter-parse-tree org-latex-math-block-tree-filter
105 org-latex-matrices-tree-filter
))
107 '((:latex-class
"LATEX_CLASS" nil org-latex-default-class t
)
108 (:latex-class-options
"LATEX_CLASS_OPTIONS" nil nil t
)
109 (:latex-header
"LATEX_HEADER" nil nil newline
)
110 (:latex-header-extra
"LATEX_HEADER_EXTRA" nil nil newline
)
111 (:description
"DESCRIPTION" nil nil parse
)
112 (:keywords
"KEYWORDS" nil nil parse
)
113 (:subtitle
"SUBTITLE" nil nil parse
)
115 (:latex-active-timestamp-format nil nil org-latex-active-timestamp-format
)
116 (:latex-caption-above nil nil org-latex-caption-above
)
117 (:latex-classes nil nil org-latex-classes
)
118 (:latex-default-figure-position nil nil org-latex-default-figure-position
)
119 (:latex-default-table-environment nil nil org-latex-default-table-environment
)
120 (:latex-default-table-mode nil nil org-latex-default-table-mode
)
121 (:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format
)
122 (:latex-footnote-separator nil nil org-latex-footnote-separator
)
123 (:latex-format-drawer-function nil nil org-latex-format-drawer-function
)
124 (:latex-format-headline-function nil nil org-latex-format-headline-function
)
125 (:latex-format-inlinetask-function nil nil org-latex-format-inlinetask-function
)
126 (:latex-hyperref-template nil nil org-latex-hyperref-template t
)
127 (:latex-image-default-height nil nil org-latex-image-default-height
)
128 (:latex-image-default-option nil nil org-latex-image-default-option
)
129 (:latex-image-default-width nil nil org-latex-image-default-width
)
130 (:latex-inactive-timestamp-format nil nil org-latex-inactive-timestamp-format
)
131 (:latex-inline-image-rules nil nil org-latex-inline-image-rules
)
132 (:latex-link-with-unknown-path-format nil nil org-latex-link-with-unknown-path-format
)
133 (:latex-listings nil nil org-latex-listings
)
134 (:latex-listings-langs nil nil org-latex-listings-langs
)
135 (:latex-listings-options nil nil org-latex-listings-options
)
136 (:latex-minted-langs nil nil org-latex-minted-langs
)
137 (:latex-minted-options nil nil org-latex-minted-options
)
138 (:latex-prefer-user-labels nil nil org-latex-prefer-user-labels
)
139 (:latex-subtitle-format nil nil org-latex-subtitle-format
)
140 (:latex-subtitle-separate nil nil org-latex-subtitle-separate
)
141 (:latex-table-scientific-notation nil nil org-latex-table-scientific-notation
)
142 (:latex-tables-booktabs nil nil org-latex-tables-booktabs
)
143 (:latex-tables-centered nil nil org-latex-tables-centered
)
144 (:latex-text-markup-alist nil nil org-latex-text-markup-alist
)
145 (:latex-title-command nil nil org-latex-title-command
)
146 (:latex-toc-command nil nil org-latex-toc-command
)
147 ;; Redefine regular options.
148 (:date
"DATE" nil
"\\today" parse
)))
152 ;;; Internal Variables
154 (defconst org-latex-babel-language-alist
155 '(("af" .
"afrikaans")
157 ("bt-br" .
"brazilian")
163 ("de-at" .
"naustrian")
164 ("de-de" .
"ngerman")
167 ("en-au" .
"australian")
168 ("en-ca" .
"canadian")
169 ("en-gb" .
"british")
171 ("en-nz" .
"newzealand")
172 ("en-us" .
"american")
178 ("fr-ca" .
"canadien")
182 ("id" .
"indonesian")
192 ("pt" .
"portuguese")
196 ("sb" .
"uppersorbian")
204 ("uk" .
"ukrainian"))
205 "Alist between language code and corresponding Babel option.")
207 (defconst org-latex-polyglossia-language-alist
215 ("bt-br" "brazilian")
221 ("de" "german" "german")
222 ("de-at" "german" "austrian")
223 ("de-de" "german" "german")
226 ("en" "english" "usmax")
227 ("en-au" "english" "australian")
228 ("en-gb" "english" "uk")
229 ("en-nz" "english" "newzealand")
230 ("en-us" "english" "usmax")
252 ("la" "latin" "modern")
253 ("la-modern" "latin" "modern")
254 ("la-classic" "latin" "classic")
255 ("la-medieval" "latin" "medieval")
268 ("pms" "piedmontese")
291 "Alist between language code and corresponding Polyglossia option")
295 (defconst org-latex-table-matrix-macros
'(("bordermatrix" .
"\\cr")
296 ("qbordermatrix" .
"\\cr")
297 ("kbordermatrix" .
"\\\\"))
298 "Alist between matrix macros and their row ending.")
300 (defconst org-latex-math-environments-re
302 "\\`[ \t]*\\\\begin{%s\\*?}"
304 '("equation" "eqnarray" "math" "displaymath"
305 "align" "gather" "multline" "flalign" "alignat"
306 "xalignat" "xxalignat"
309 "dmath" "dseries" "dgroup" "darray"
312 "Regexp of LaTeX math environments.")
315 ;;; User Configurable Variables
317 (defgroup org-export-latex nil
318 "Options for exporting Org mode files to LaTeX."
319 :tag
"Org Export LaTeX"
324 (defcustom org-latex-caption-above
'(table)
325 "When non-nil, place caption string at the beginning of elements.
326 Otherwise, place it near the end. When value is a list of
327 symbols, put caption above selected elements only. Allowed
328 symbols are: `image', `table', `src-block' and `special-block'."
329 :group
'org-export-latex
331 :package-version
'(Org .
"8.3")
333 (const :tag
"For all elements" t
)
334 (const :tag
"For no element" nil
)
335 (set :tag
"For the following elements only" :greedy t
336 (const :tag
"Images" image
)
337 (const :tag
"Tables" table
)
338 (const :tag
"Source code" src-block
)
339 (const :tag
"Special blocks" special-block
))))
341 (defcustom org-latex-prefer-user-labels nil
342 "Use user-provided labels instead of internal ones when non-nil.
344 When this variable is non-nil, Org will use the value of
345 CUSTOM_ID property, NAME keyword or Org target as the key for the
346 \\label commands generated.
348 By default, Org generates its own internal labels during LaTeX
349 export. This process ensures that the \\label keys are unique
350 and valid, but it means the keys are not available in advance of
353 Setting this variable gives you control over how Org generates
354 labels during LaTeX export, so that you may know their keys in
355 advance. One reason to do this is that it allows you to refer to
356 various elements using a single label both in Org's link syntax
357 and in embedded LaTeX code.
359 For example, when this variable is non-nil, a headline like this:
365 This is section [[#sec:foo]].
367 And this is still section \\ref{sec:foo}.
370 will be exported to LaTeX as:
372 \\subsection{Some section}
374 This is section \\ref{sec:foo}.
375 And this is still section \\ref{sec:foo}.
377 Note, however, that setting this variable introduces a limitation
378 on the possible values for CUSTOM_ID and NAME. When this
379 variable is non-nil, Org passes their value to \\label unchanged.
380 You are responsible for ensuring that the value is a valid LaTeX
381 \\label key, and that no other \\label commands with the same key
382 appear elsewhere in your document. (Keys may contain letters,
383 numbers, and the following punctuation: '_' '.' '-' ':'.) There
384 are no such limitations on CUSTOM_ID and NAME when this variable
387 For headlines that do not define the CUSTOM_ID property or
388 elements without a NAME, Org will continue to use its default
389 labeling scheme to generate labels and resolve links into proper
391 :group
'org-export-latex
394 :package-version
'(Org .
"8.3"))
398 (defcustom org-latex-default-class
"article"
399 "The default LaTeX class."
400 :group
'org-export-latex
401 :type
'(string :tag
"LaTeX class"))
403 (defcustom org-latex-classes
405 "\\documentclass[11pt]{article}"
406 ("\\section{%s}" .
"\\section*{%s}")
407 ("\\subsection{%s}" .
"\\subsection*{%s}")
408 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
409 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
410 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
412 "\\documentclass[11pt]{report}"
413 ("\\part{%s}" .
"\\part*{%s}")
414 ("\\chapter{%s}" .
"\\chapter*{%s}")
415 ("\\section{%s}" .
"\\section*{%s}")
416 ("\\subsection{%s}" .
"\\subsection*{%s}")
417 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
419 "\\documentclass[11pt]{book}"
420 ("\\part{%s}" .
"\\part*{%s}")
421 ("\\chapter{%s}" .
"\\chapter*{%s}")
422 ("\\section{%s}" .
"\\section*{%s}")
423 ("\\subsection{%s}" .
"\\subsection*{%s}")
424 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
425 "Alist of LaTeX classes and associated header and structure.
426 If #+LATEX_CLASS is set in the buffer, use its value and the
427 associated information. Here is the structure of each cell:
431 \(numbered-section . unnumbered-section)
437 The HEADER-STRING is the header that will be inserted into the
438 LaTeX file. It should contain the \\documentclass macro, and
439 anything else that is needed for this setup. To this header, the
440 following commands will be added:
442 - Calls to \\usepackage for all packages mentioned in the
443 variables `org-latex-default-packages-alist' and
444 `org-latex-packages-alist'. Thus, your header definitions
445 should avoid to also request these packages.
447 - Lines specified via \"#+LATEX_HEADER:\" and
448 \"#+LATEX_HEADER_EXTRA:\" keywords.
450 If you need more control about the sequence in which the header
451 is built up, or if you want to exclude one of these building
452 blocks for a particular class, you can use the following
453 macro-like placeholders.
455 [DEFAULT-PACKAGES] \\usepackage statements for default packages
456 [NO-DEFAULT-PACKAGES] do not include any of the default packages
457 [PACKAGES] \\usepackage statements for packages
458 [NO-PACKAGES] do not include the packages
459 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
460 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
464 \\documentclass{article}
465 [NO-DEFAULT-PACKAGES]
467 \\providecommand{\\alert}[1]{\\textbf{#1}}
470 will omit the default packages, and will include the
471 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
472 to \\providecommand, and then place \\usepackage commands based
473 on the content of `org-latex-packages-alist'.
475 If your header, `org-latex-default-packages-alist' or
476 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
477 AUTO will automatically be replaced with a coding system derived
478 from `buffer-file-coding-system'. See also the variable
479 `org-latex-inputenc-alist' for a way to influence this mechanism.
481 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\",
482 AUTO will be replaced with the language related to the language
483 code specified by `org-export-default-language', which see. Note
484 that constructions such as \"\\usepackage[french,AUTO,english]{babel}\"
487 The sectioning structure
488 ------------------------
490 The sectioning structure of the class is given by the elements
491 following the header string. For each sectioning level, a number
492 of strings is specified. A %s formatter is mandatory in each
493 section string and will be replaced by the title of the section.
495 Instead of a cons cell (numbered . unnumbered), you can also
496 provide a list of 2 or 4 elements,
498 \(numbered-open numbered-close)
502 \(numbered-open numbered-close unnumbered-open unnumbered-close)
504 providing opening and closing strings for a LaTeX environment
505 that should represent the document section. The opening clause
506 should have a %s to represent the section title.
508 Instead of a list of sectioning commands, you can also specify
509 a function name. That function will be called with two
510 parameters, the (reduced) level of the headline, and a predicate
511 non-nil when the headline should be numbered. It must return
512 a format string in which the section title will be added."
513 :group
'org-export-latex
515 (list (string :tag
"LaTeX class")
516 (string :tag
"LaTeX header")
517 (repeat :tag
"Levels" :inline t
520 (string :tag
" numbered")
521 (string :tag
"unnumbered"))
522 (list :tag
"Environment"
523 (string :tag
"Opening (numbered)")
524 (string :tag
"Closing (numbered)")
525 (string :tag
"Opening (unnumbered)")
526 (string :tag
"Closing (unnumbered)"))
527 (function :tag
"Hook computing sectioning"))))))
529 (defcustom org-latex-inputenc-alist nil
530 "Alist of inputenc coding system names, and what should really be used.
531 For example, adding an entry
533 (\"utf8\" . \"utf8x\")
535 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
536 are written as utf8 files."
537 :group
'org-export-latex
540 (string :tag
"Derived from buffer")
541 (string :tag
"Use this instead"))))
543 (defcustom org-latex-title-command
"\\maketitle"
544 "The command used to insert the title just after \\begin{document}.
546 This format string may contain these elements:
548 %a for AUTHOR keyword
550 %s for SUBTITLE keyword
552 %d for DESCRIPTION line
554 %l for Language keyword
555 %L for capitalized language keyword
558 If you need to use a \"%\" character, you need to escape it
561 Setting :latex-title-command in publishing projects will take
562 precedence over this variable."
563 :group
'org-export-latex
564 :type
'(string :tag
"Format string"))
566 (defcustom org-latex-subtitle-format
"\\\\\\medskip\n\\large %s"
567 "Format string used for transcoded subtitle.
568 The format string should have at most one \"%s\"-expression,
569 which is replaced with the subtitle."
570 :group
'org-export-latex
572 :package-version
'(Org .
"8.3")
573 :type
'(string :tag
"Format string"))
575 (defcustom org-latex-subtitle-separate nil
576 "Non-nil means the subtitle is not typeset as part of title."
577 :group
'org-export-latex
579 :package-version
'(Org .
"8.3")
582 (defcustom org-latex-toc-command
"\\tableofcontents\n\n"
583 "LaTeX command to set the table of contents, list of figures, etc.
584 This command only applies to the table of contents generated with
585 the toc:nil option, not to those generated with #+TOC keyword."
586 :group
'org-export-latex
589 (defcustom org-latex-hyperref-template
590 "\\hypersetup{\n pdfauthor={%a},\n pdftitle={%t},\n pdfkeywords={%k},
591 pdfsubject={%d},\n pdfcreator={%c}, \n pdflang={%L}}\n"
592 "Template for hyperref package options.
594 This format string may contain these elements:
596 %a for AUTHOR keyword
598 %s for SUBTITLE keyword
600 %d for DESCRIPTION line
602 %l for Language keyword
603 %L for capitalized language keyword
606 If you need to use a \"%\" character, you need to escape it
609 Setting :latex-hyperref-template in publishing projects will take
610 precedence over this variable."
611 :group
'org-export-latex
613 :package-version
'(Org .
"8.3")
614 :type
'(string :tag
"Format string"))
618 (defcustom org-latex-format-headline-function
619 'org-latex-format-headline-default-function
620 "Function for formatting the headline's text.
622 This function will be called with six arguments:
623 TODO the todo keyword (string or nil)
624 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
625 PRIORITY the priority of the headline (integer or nil)
626 TEXT the main headline text (string)
627 TAGS the tags (list of strings or nil)
628 INFO the export options (plist)
630 The function result will be used in the section format string."
631 :group
'org-export-latex
633 :package-version
'(Org .
"8.0")
639 (defcustom org-latex-footnote-separator
"\\textsuperscript{,}\\,"
640 "Text used to separate footnotes."
641 :group
'org-export-latex
647 (defcustom org-latex-active-timestamp-format
"\\textit{%s}"
648 "A printf format string to be applied to active timestamps."
649 :group
'org-export-latex
652 (defcustom org-latex-inactive-timestamp-format
"\\textit{%s}"
653 "A printf format string to be applied to inactive timestamps."
654 :group
'org-export-latex
657 (defcustom org-latex-diary-timestamp-format
"\\textit{%s}"
658 "A printf format string to be applied to diary timestamps."
659 :group
'org-export-latex
665 (defcustom org-latex-image-default-option
""
666 "Default option for images."
667 :group
'org-export-latex
669 :package-version
'(Org .
"8.0")
672 (defcustom org-latex-image-default-width
".9\\linewidth"
673 "Default width for images.
674 This value will not be used if a height is provided."
675 :group
'org-export-latex
677 :package-version
'(Org .
"8.0")
680 (defcustom org-latex-image-default-height
""
681 "Default height for images.
682 This value will not be used if a width is provided, or if the
683 image is wrapped within a \"figure\" or \"wrapfigure\"
685 :group
'org-export-latex
687 :package-version
'(Org .
"8.0")
690 (defcustom org-latex-default-figure-position
"htb"
691 "Default position for latex figures."
692 :group
'org-export-latex
695 (defcustom org-latex-inline-image-rules
696 '(("file" .
"\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
697 "Rules characterizing image files that can be inlined into LaTeX.
699 A rule consists in an association whose key is the type of link
700 to consider, and value is a regexp that will be matched against
703 Note that, by default, the image extension *actually* allowed
704 depend on the way the LaTeX file is processed. When used with
705 pdflatex, pdf, jpg and png images are OK. When processing
706 through dvi to Postscript, only ps and eps are allowed. The
707 default we use here encompasses both."
708 :group
'org-export-latex
710 :package-version
'(Org .
"8.0")
711 :type
'(alist :key-type
(string :tag
"Type")
712 :value-type
(regexp :tag
"Path")))
714 (defcustom org-latex-link-with-unknown-path-format
"\\texttt{%s}"
715 "Format string for links with unknown path type."
716 :group
'org-export-latex
722 (defcustom org-latex-default-table-environment
"tabular"
723 "Default environment used to build tables."
724 :group
'org-export-latex
726 :package-version
'(Org .
"8.0")
729 (defcustom org-latex-default-table-mode
'table
730 "Default mode for tables.
732 Value can be a symbol among:
734 `table' Regular LaTeX table.
736 `math' In this mode, every cell is considered as being in math
737 mode and the complete table will be wrapped within a math
738 environment. It is particularly useful to write matrices.
740 `inline-math' This mode is almost the same as `math', but the
741 math environment will be inlined.
743 `verbatim' The table is exported as it appears in the Org
744 buffer, within a verbatim environment.
746 This value can be overridden locally with, i.e. \":mode math\" in
749 When modifying this variable, it may be useful to change
750 `org-latex-default-table-environment' accordingly."
751 :group
'org-export-latex
753 :package-version
'(Org .
"8.0")
754 :type
'(choice (const :tag
"Table" table
)
755 (const :tag
"Matrix" math
)
756 (const :tag
"Inline matrix" inline-math
)
757 (const :tag
"Verbatim" verbatim
))
758 :safe
(lambda (s) (memq s
'(table math inline-math verbatim
))))
760 (defcustom org-latex-tables-centered t
761 "When non-nil, tables are exported in a center environment."
762 :group
'org-export-latex
766 (defcustom org-latex-tables-booktabs nil
767 "When non-nil, display tables in a formal \"booktabs\" style.
768 This option assumes that the \"booktabs\" package is properly
769 loaded in the header of the document. This value can be ignored
770 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
772 :group
'org-export-latex
774 :package-version
'(Org .
"8.0")
778 (defcustom org-latex-table-scientific-notation
"%s\\,(%s)"
779 "Format string to display numbers in scientific notation.
780 The format should have \"%s\" twice, for mantissa and exponent
781 \(i.e., \"%s\\\\times10^{%s}\").
783 When nil, no transformation is made."
784 :group
'org-export-latex
786 :package-version
'(Org .
"8.0")
788 (string :tag
"Format string")
789 (const :tag
"No formatting" nil
)))
793 (defcustom org-latex-text-markup-alist
'((bold .
"\\textbf{%s}")
794 (code . protectedtexttt
)
795 (italic .
"\\emph{%s}")
796 (strike-through .
"\\sout{%s}")
797 (underline .
"\\uline{%s}")
798 (verbatim . protectedtexttt
))
799 "Alist of LaTeX expressions to convert text markup.
801 The key must be a symbol among `bold', `code', `italic',
802 `strike-through', `underline' and `verbatim'. The value is
803 a formatting string to wrap fontified text with.
805 Value can also be set to the following symbols: `verb' and
806 `protectedtexttt'. For the former, Org will use \"\\verb\" to
807 create a format string and select a delimiter character that
808 isn't in the string. For the latter, Org will use \"\\texttt\"
809 to typeset and try to protect special characters.
811 If no association can be found for a given markup, text will be
813 :group
'org-export-latex
815 :package-version
'(Org .
"8.3")
817 :options
'(bold code italic strike-through underline verbatim
))
822 (defcustom org-latex-format-drawer-function
823 (lambda (name contents
) contents
)
824 "Function called to format a drawer in LaTeX code.
826 The function must accept two parameters:
827 NAME the drawer name, like \"LOGBOOK\"
828 CONTENTS the contents of the drawer.
830 The function should return the string to be exported.
832 The default function simply returns the value of CONTENTS."
833 :group
'org-export-latex
835 :package-version
'(Org .
"8.3")
841 (defcustom org-latex-format-inlinetask-function
842 'org-latex-format-inlinetask-default-function
843 "Function called to format an inlinetask in LaTeX code.
845 The function must accept seven parameters:
846 TODO the todo keyword (string or nil)
847 TODO-TYPE the todo type (symbol: `todo', `done', nil)
848 PRIORITY the inlinetask priority (integer or nil)
849 NAME the inlinetask name (string)
850 TAGS the inlinetask tags (list of strings or nil)
851 CONTENTS the contents of the inlinetask (string or nil)
852 INFO the export options (plist)
854 The function should return the string to be exported."
855 :group
'org-export-latex
858 :package-version
'(Org .
"8.3"))
863 (defcustom org-latex-listings nil
864 "Non-nil means export source code using the listings package.
866 This package will fontify source code, possibly even with color.
867 If you want to use this, you also need to make LaTeX use the
868 listings package, and if you want to have color, the color
869 package. Just add these to `org-latex-packages-alist', for
870 example using customize, or with something like:
873 \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
874 \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
878 \(setq org-latex-listings 'minted)
880 causes source code to be exported using the minted package as
881 opposed to listings. If you want to use minted, you need to add
882 the minted package to `org-latex-packages-alist', for example
883 using customize, or with
886 \(add-to-list 'org-latex-packages-alist '(\"newfloat\" \"minted\"))
888 In addition, it is necessary to install pygments
889 \(http://pygments.org), and to configure the variable
890 `org-latex-pdf-process' so that the -shell-escape option is
893 The minted choice has possible repercussions on the preview of
894 latex fragments (see `org-preview-latex-fragment'). If you run
895 into previewing problems, please consult
897 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
898 :group
'org-export-latex
900 (const :tag
"Use listings" t
)
901 (const :tag
"Use minted" minted
)
902 (const :tag
"Export verbatim" nil
))
903 :safe
(lambda (s) (memq s
'(t nil minted
))))
905 (defcustom org-latex-listings-langs
906 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
909 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
910 (html "HTML") (xml "XML")
911 (tex "TeX") (latex "[LaTeX]TeX")
912 (shell-script "bash")
914 (ocaml "Caml") (caml "Caml")
915 (sql "SQL") (sqlite "sql")
917 "Alist mapping languages to their listing language counterpart.
918 The key is a symbol, the major mode symbol without the \"-mode\".
919 The value is the string that should be inserted as the language
920 parameter for the listings package. If the mode name and the
921 listings name are the same, the language does not need an entry
922 in this list - but it does not hurt if it is present."
923 :group
'org-export-latex
925 :package-version
'(Org .
"8.3")
928 (symbol :tag
"Major mode ")
929 (string :tag
"Listings language"))))
931 (defcustom org-latex-listings-options nil
932 "Association list of options for the latex listings package.
934 These options are supplied as a comma-separated list to the
935 \\lstset command. Each element of the association list should be
936 a list containing two strings: the name of the option, and the
939 \(setq org-latex-listings-options
940 '((\"basicstyle\" \"\\\\small\")
941 \(\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
943 will typeset the code in a small size font with underlined, bold
946 Note that the same options will be applied to blocks of all
947 languages. If you need block-specific options, you may use the
950 #+ATTR_LATEX: :options key1=value1,key2=value2
954 :group
'org-export-latex
957 (string :tag
"Listings option name ")
958 (string :tag
"Listings option value"))))
960 (defcustom org-latex-minted-langs
961 '((emacs-lisp "common-lisp")
964 (shell-script "bash")
966 "Alist mapping languages to their minted language counterpart.
967 The key is a symbol, the major mode symbol without the \"-mode\".
968 The value is the string that should be inserted as the language
969 parameter for the minted package. If the mode name and the
970 listings name are the same, the language does not need an entry
971 in this list - but it does not hurt if it is present.
973 Note that minted uses all lower case for language identifiers,
974 and that the full list of language identifiers can be obtained
977 pygmentize -L lexers"
978 :group
'org-export-latex
981 (symbol :tag
"Major mode ")
982 (string :tag
"Minted language"))))
984 (defcustom org-latex-minted-options nil
985 "Association list of options for the latex minted package.
987 These options are supplied within square brackets in
988 \\begin{minted} environments. Each element of the alist should
989 be a list containing two strings: the name of the option, and the
992 \(setq org-latex-minted-options
993 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
995 will result in src blocks being exported with
997 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
999 as the start of the minted environment. Note that the same
1000 options will be applied to blocks of all languages. If you need
1001 block-specific options, you may use the following syntax:
1003 #+ATTR_LATEX: :options key1=value1,key2=value2
1007 :group
'org-export-latex
1010 (string :tag
"Minted option name ")
1011 (string :tag
"Minted option value"))))
1013 (defvar org-latex-custom-lang-environments nil
1014 "Alist mapping languages to language-specific LaTeX environments.
1016 It is used during export of src blocks by the listings and minted
1017 latex packages. For example,
1019 \(setq org-latex-custom-lang-environments
1020 '\(\(python \"pythoncode\"\)\)\)
1022 would have the effect that if org encounters begin_src python
1023 during latex export it will output
1032 (defcustom org-latex-pdf-process
1033 '("pdflatex -interaction nonstopmode -output-directory %o %f"
1034 "pdflatex -interaction nonstopmode -output-directory %o %f"
1035 "pdflatex -interaction nonstopmode -output-directory %o %f")
1036 "Commands to process a LaTeX file to a PDF file.
1037 This is a list of strings, each of them will be given to the
1038 shell as a command. %f in the command will be replaced by the
1039 full file name, %b by the file base name (i.e. without directory
1040 and extension parts) and %o by the base directory of the file.
1042 The reason why this is a list is that it usually takes several
1043 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
1044 does not have a clever mechanism to detect which of these
1045 commands have to be run to get to a stable result, and it also
1046 does not do any error checking.
1048 By default, Org uses 3 runs of `pdflatex' to do the processing.
1049 If you have texi2dvi on your system and if that does not cause
1050 the infamous egrep/locale bug:
1052 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
1054 then `texi2dvi' is the superior choice as it automates the LaTeX
1055 build process by calling the \"correct\" combinations of
1056 auxiliary programs. Org does offer `texi2dvi' as one of the
1057 customize options. Alternatively, `rubber' and `latexmk' also
1058 provide similar functionality. The latter supports `biber' out
1061 Alternatively, this may be a Lisp function that does the
1062 processing, so you could use this to apply the machinery of
1063 AUCTeX or the Emacs LaTeX mode. This function should accept the
1064 file name as its single argument."
1065 :group
'org-export-pdf
1067 (repeat :tag
"Shell command sequence"
1068 (string :tag
"Shell command"))
1069 (const :tag
"2 runs of pdflatex"
1070 ("pdflatex -interaction nonstopmode -output-directory %o %f"
1071 "pdflatex -interaction nonstopmode -output-directory %o %f"))
1072 (const :tag
"3 runs of pdflatex"
1073 ("pdflatex -interaction nonstopmode -output-directory %o %f"
1074 "pdflatex -interaction nonstopmode -output-directory %o %f"
1075 "pdflatex -interaction nonstopmode -output-directory %o %f"))
1076 (const :tag
"pdflatex,bibtex,pdflatex,pdflatex"
1077 ("pdflatex -interaction nonstopmode -output-directory %o %f"
1079 "pdflatex -interaction nonstopmode -output-directory %o %f"
1080 "pdflatex -interaction nonstopmode -output-directory %o %f"))
1081 (const :tag
"2 runs of xelatex"
1082 ("xelatex -interaction nonstopmode -output-directory %o %f"
1083 "xelatex -interaction nonstopmode -output-directory %o %f"))
1084 (const :tag
"3 runs of xelatex"
1085 ("xelatex -interaction nonstopmode -output-directory %o %f"
1086 "xelatex -interaction nonstopmode -output-directory %o %f"
1087 "xelatex -interaction nonstopmode -output-directory %o %f"))
1088 (const :tag
"xelatex,bibtex,xelatex,xelatex"
1089 ("xelatex -interaction nonstopmode -output-directory %o %f"
1091 "xelatex -interaction nonstopmode -output-directory %o %f"
1092 "xelatex -interaction nonstopmode -output-directory %o %f"))
1093 (const :tag
"texi2dvi"
1094 ("texi2dvi -p -b -V %f"))
1095 (const :tag
"rubber"
1096 ("rubber -d --into %o %f"))
1097 (const :tag
"latexmk"
1098 ("latexmk -g -pdf %f"))
1101 (defcustom org-latex-logfiles-extensions
1102 '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
1103 "ptc" "run.xml" "snm" "toc" "vrb" "xdv")
1104 "The list of file extensions to consider as LaTeX logfiles.
1105 The logfiles will be removed if `org-latex-remove-logfiles' is
1107 :group
'org-export-latex
1109 :package-version
'(Org .
"8.3")
1110 :type
'(repeat (string :tag
"Extension")))
1112 (defcustom org-latex-remove-logfiles t
1113 "Non-nil means remove the logfiles produced by PDF production.
1114 By default, logfiles are files with these extensions: .aux, .idx,
1115 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
1116 logfiles to remove, set `org-latex-logfiles-extensions'."
1117 :group
'org-export-latex
1120 (defcustom org-latex-known-warnings
1121 '(("Reference.*?undefined" .
"[undefined reference]")
1122 ("Runaway argument" .
"[runaway argument]")
1123 ("Underfull \\hbox" .
"[underfull hbox]")
1124 ("Overfull \\hbox" .
"[overfull hbox]")
1125 ("Citation.*?undefined" .
"[undefined citation]")
1126 ("Undefined control sequence" .
"[undefined control sequence]"))
1127 "Alist of regular expressions and associated messages for the user.
1128 The regular expressions are used to find possible warnings in the
1129 log of a latex-run. These warnings will be reported after
1130 calling `org-latex-compile'."
1131 :group
'org-export-latex
1133 :package-version
'(Org .
"8.3")
1136 (string :tag
"Regexp")
1137 (string :tag
"Message"))))
1141 ;;; Internal Functions
1143 (defun org-latex--caption-above-p (element info
)
1144 "Non nil when caption is expected to be located above ELEMENT.
1145 INFO is a plist holding contextual information."
1146 (let ((above (plist-get info
:latex-caption-above
)))
1147 (if (symbolp above
) above
1148 (let ((type (org-element-type element
)))
1149 (memq (if (eq type
'link
) 'image type
) above
)))))
1151 (defun org-latex--label (datum info
&optional force full
)
1152 "Return an appropriate label for DATUM.
1153 DATUM is an element or a `target' type object. INFO is the
1154 current export state, as a plist.
1156 Return nil if element DATUM has no NAME or VALUE affiliated
1157 keyword or no CUSTOM_ID property, unless FORCE is non-nil. In
1158 this case always return a unique label.
1160 Eventually, if FULL is non-nil, wrap label within \"\\label{}\"."
1161 (let* ((type (org-element-type datum
))
1163 (org-element-property
1165 ((headline inlinetask
) :CUSTOM_ID
)
1170 (and (or user-label force
)
1171 (if (and user-label
(plist-get info
:latex-prefer-user-labels
))
1177 (and (org-string-match-p
1178 org-latex-math-environments-re
1179 (org-element-property :value datum
))
1182 (and (org-element-property :caption datum
)
1184 (org-export-get-reference datum info
))))))
1185 (cond ((not full
) label
)
1186 (label (format "\\label{%s}%s"
1188 (if (eq type
'target
) "" "\n")))
1191 (defun org-latex--caption/label-string
(element info
)
1192 "Return caption and label LaTeX string for ELEMENT.
1194 INFO is a plist holding contextual information. If there's no
1195 caption nor label, return the empty string.
1197 For non-floats, see `org-latex--wrap-label'."
1198 (let* ((label (org-latex--label element info nil t
))
1199 (main (org-export-get-caption element
))
1200 (attr (org-export-read-attribute :attr_latex element
))
1201 (type (org-element-type element
))
1202 (nonfloat (or (and (plist-member attr
:float
)
1203 (not (plist-get attr
:float
))
1205 (and (eq type
'src-block
)
1206 (not (plist-get attr
:float
))
1207 (memq (plist-get info
:latex-listings
)
1209 (short (org-export-get-caption element t
))
1210 (caption-from-attr-latex (plist-get attr
:caption
)))
1212 ((org-string-nw-p caption-from-attr-latex
)
1213 (concat caption-from-attr-latex
"\n"))
1214 ((and (not main
) (equal label
"")) "")
1215 ((not main
) (concat label
"\n"))
1216 ;; Option caption format with short name.
1218 (format (if nonfloat
"\\captionof{%s}%s{%s%s}\n"
1219 "\\caption%s%s{%s%s}\n")
1222 (paragraph "figure")
1223 (src-block (if (plist-get info
:latex-listings
)
1226 (t (symbol-name type
)))
1228 (if short
(format "[%s]" (org-export-data short info
)) "")
1230 (org-export-data main info
))))))
1232 (defun org-latex-guess-inputenc (header)
1233 "Set the coding system in inputenc to what the buffer is.
1235 HEADER is the LaTeX header string. This function only applies
1236 when specified inputenc option is \"AUTO\".
1238 Return the new header, as a string."
1239 (let* ((cs (or (ignore-errors
1240 (latexenc-coding-system-to-inputenc
1241 (or org-export-coding-system buffer-file-coding-system
)))
1244 ;; First translate if that is requested.
1245 (setq cs
(or (cdr (assoc cs org-latex-inputenc-alist
)) cs
))
1246 ;; Then find the \usepackage statement and replace the option.
1247 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1248 cs header t nil
1))))
1250 (defun org-latex-guess-babel-language (header info
)
1251 "Set Babel's language according to LANGUAGE keyword.
1253 HEADER is the LaTeX header string. INFO is the plist used as
1254 a communication channel.
1256 Insertion of guessed language only happens when Babel package has
1257 explicitly been loaded. Then it is added to the rest of
1260 The argument to Babel may be \"AUTO\" which is then replaced with
1261 the language of the document or `org-export-default-language'
1262 unless language in question is already loaded.
1264 Return the new header."
1265 (let ((language-code (plist-get info
:language
)))
1266 ;; If no language is set or Babel package is not loaded, return
1268 (if (or (not (stringp language-code
))
1269 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header
)))
1271 (let ((options (save-match-data
1272 (org-split-string (match-string 1 header
) ",[ \t]*")))
1273 (language (cdr (assoc language-code
1274 org-latex-babel-language-alist
))))
1275 ;; If LANGUAGE is already loaded, return header without AUTO.
1276 ;; Otherwise, replace AUTO with language or append language if
1277 ;; AUTO is not present.
1279 (mapconcat (lambda (option) (if (equal "AUTO" option
) language option
))
1280 (cond ((member language options
) (delete "AUTO" options
))
1281 ((member "AUTO" options
) options
)
1282 (t (append options
(list language
))))
1286 (defun org-latex-guess-polyglossia-language (header info
)
1287 "Set the Polyglossia language according to the LANGUAGE keyword.
1289 HEADER is the LaTeX header string. INFO is the plist used as
1290 a communication channel.
1292 Insertion of guessed language only happens when the Polyglossia
1293 package has been explicitly loaded.
1295 The argument to Polyglossia may be \"AUTO\" which is then
1296 replaced with the language of the document or
1297 `org-export-default-language'. Note, the language is really set
1298 using \setdefaultlanguage and not as an option to the package.
1300 Return the new header."
1301 (let ((language (plist-get info
:language
))
1303 ;; If no language is set or Polyglossia is not loaded, return
1305 (if (or (not (org-string-nw-p language
))
1307 "\\\\usepackage\\(?:\\[\\([^]]+?\\)\\]\\){polyglossia}\n" header
)))
1309 (let* ((options (org-string-nw-p (match-string 1 header
)))
1310 (languages (when options
1312 ;; Reverse as the last loaded language is
1313 ;; the main language.
1317 (replace-regexp-in-string
1318 "AUTO" language options t
)
1320 (main-language-set-p
1321 (string-match-p "\\\\setmainlanguage{.*?}" header
)))
1323 (concat "\\usepackage{polyglossia}\n"
1324 (loop for l in languages concat
1325 (let ((lang (or (assoc l org-latex-polyglossia-language-alist
)
1327 (format (if main-language-set-p
1328 "\\setotherlanguage%s{%s}\n"
1329 (progn (setq main-language-set-p t
)
1330 "\\setmainlanguage%s{%s}\n"))
1331 (if (and (listp lang
) (eq 3 (length lang
)))
1332 (format "[variant=%s]" (nth 2 lang
))
1337 (defun org-latex--find-verb-separator (s)
1338 "Return a character not used in string S.
1339 This is used to choose a separator for constructs like \\verb."
1340 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1341 (loop for c across ll
1342 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
1343 return
(char-to-string c
))))
1345 (defun org-latex--make-option-string (options)
1346 "Return a comma separated string of keywords and values.
1347 OPTIONS is an alist where the key is the options keyword as
1348 a string, and the value a list containing the keyword value, or
1350 (mapconcat (lambda (pair)
1351 (concat (first pair
)
1352 (when (> (length (second pair
)) 0)
1353 (concat "=" (second pair
)))))
1357 (defun org-latex--wrap-label (element output info
)
1358 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1359 INFO is the current export state, as a plist. This function
1360 should not be used for floats. See
1361 `org-latex--caption/label-string'."
1362 (if (not (and (org-string-nw-p output
) (org-element-property :name element
)))
1364 (concat (format "\\phantomsection\n\\label{%s}\n"
1365 (org-latex--label element info
))
1368 (defun org-latex--protect-text (text)
1369 "Protect special characters in string TEXT and return it."
1370 (replace-regexp-in-string
1371 "--\\|[\\{}$%&_#~^]"
1373 (cond ((equal m
"--") "-{}-")
1374 ((equal m
"\\") "\\textbackslash{}")
1375 ((equal m
"~") "\\textasciitilde{}")
1376 ((equal m
"^") "\\textasciicircum{}")
1377 (t (concat "\\" m
))))
1380 (defun org-latex--text-markup (text markup info
)
1381 "Format TEXT depending on MARKUP text markup.
1382 INFO is a plist used as a communication channel. See
1383 `org-latex-text-markup-alist' for details."
1384 (let ((fmt (cdr (assq markup
(plist-get info
:latex-text-markup-alist
)))))
1386 ;; No format string: Return raw text.
1388 ;; Handle the `verb' special case: Find an appropriate separator
1389 ;; and use "\\verb" command.
1391 (let ((separator (org-latex--find-verb-separator text
)))
1392 (concat "\\verb" separator
1393 (replace-regexp-in-string "\n" " " text
)
1395 ;; Handle the `protectedtexttt' special case: Protect some
1396 ;; special chars and use "\texttt{%s}" format string.
1398 (format "\\texttt{%s}" (org-latex--protect-text text
)))
1399 ;; Else use format string.
1400 (t (format fmt text
)))))
1402 (defun org-latex--delayed-footnotes-definitions (element info
)
1403 "Return footnotes definitions in ELEMENT as a string.
1405 INFO is a plist used as a communication channel.
1407 Footnotes definitions are returned within \"\\footnotetxt{}\"
1410 This function is used within constructs that don't support
1411 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1412 \"\\footnotemark\" is used within the construct and the function
1413 just outside of it."
1417 "\\footnotetext[%s]{%s}"
1418 (org-export-get-footnote-number ref info
)
1421 (org-export-get-footnote-definition ref info
) info
))))
1422 ;; Find every footnote reference in ELEMENT.
1424 search-refs
; For byte-compiler.
1428 ;; Return a list of all footnote references never seen
1430 (org-element-map data
'footnote-reference
1432 (when (org-export-footnote-first-reference-p ref info
)
1434 (when (eq (org-element-property :type ref
) 'standard
)
1435 (funcall search-refs
1436 (org-export-get-footnote-definition ref info
)))))
1438 (reverse all-refs
)))))
1439 (funcall search-refs element
))
1442 (defun org-latex--translate (s info
)
1443 "Translate string S according to specified language.
1444 INFO is a plist used as a communication channel."
1445 (org-export-translate s
:latex info
))
1447 (defun org-latex--format-spec (info)
1448 "Create a format-spec for document meta-data.
1449 INFO is a plist used as a communication channel."
1450 (let ((language (let ((lang (plist-get info
:language
)))
1451 (or (cdr (assoc lang org-latex-babel-language-alist
))
1453 `((?a .
,(org-export-data (plist-get info
:author
) info
))
1454 (?t .
,(org-export-data (plist-get info
:title
) info
))
1455 (?k .
,(org-export-data (org-latex--wrap-latex-math-block
1456 (plist-get info
:keywords
) info
)
1458 (?d .
,(org-export-data (org-latex--wrap-latex-math-block
1459 (plist-get info
:description
) info
)
1461 (?c .
,(plist-get info
:creator
))
1463 (?L .
,(capitalize language
))
1464 (?D .
,(org-export-get-date info
)))))
1469 (defun org-latex-template (contents info
)
1470 "Return complete document string after LaTeX conversion.
1471 CONTENTS is the transcoded contents string. INFO is a plist
1472 holding export options."
1473 (let ((title (org-export-data (plist-get info
:title
) info
))
1474 (spec (org-latex--format-spec info
)))
1477 (and (plist-get info
:time-stamp-file
)
1478 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1479 ;; Document class and packages.
1480 (let* ((class (plist-get info
:latex-class
))
1481 (class-options (plist-get info
:latex-class-options
))
1482 (header (nth 1 (assoc class
(plist-get info
:latex-classes
))))
1483 (document-class-string
1484 (and (stringp header
)
1485 (if (not class-options
) header
1486 (replace-regexp-in-string
1487 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1488 class-options header t nil
1)))))
1489 (if (not document-class-string
)
1490 (user-error "Unknown LaTeX class `%s'" class
)
1491 (org-latex-guess-polyglossia-language
1492 (org-latex-guess-babel-language
1493 (org-latex-guess-inputenc
1494 (org-element-normalize-string
1495 (org-splice-latex-header
1496 document-class-string
1497 org-latex-default-packages-alist
1498 org-latex-packages-alist nil
1499 (concat (org-element-normalize-string
1500 (plist-get info
:latex-header
))
1501 (plist-get info
:latex-header-extra
)))))
1504 ;; Possibly limit depth for headline numbering.
1505 (let ((sec-num (plist-get info
:section-numbers
)))
1506 (when (integerp sec-num
)
1507 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
1509 (let ((author (and (plist-get info
:with-author
)
1510 (let ((auth (plist-get info
:author
)))
1511 (and auth
(org-export-data auth info
)))))
1512 (email (and (plist-get info
:with-email
)
1513 (org-export-data (plist-get info
:email
) info
))))
1514 (cond ((and author email
(not (string= "" email
)))
1515 (format "\\author{%s\\thanks{%s}}\n" author email
))
1516 ((or author email
) (format "\\author{%s}\n" (or author email
)))))
1518 (let ((date (and (plist-get info
:with-date
) (org-export-get-date info
))))
1519 (format "\\date{%s}\n" (org-export-data date info
)))
1520 ;; Title and subtitle.
1521 (let* ((subtitle (plist-get info
:subtitle
))
1524 (format (plist-get info
:latex-subtitle-format
)
1525 (org-export-data subtitle info
))))
1526 (separate (plist-get info
:latex-subtitle-separate
)))
1528 (format "\\title{%s%s}\n" title
1529 (if separate
"" (or formatted-subtitle
"")))
1530 (when (and separate subtitle
)
1531 (concat formatted-subtitle
"\n"))))
1532 ;; Hyperref options.
1533 (let ((template (plist-get info
:latex-hyperref-template
)))
1534 (and (stringp template
)
1535 (format-spec template spec
)))
1537 "\\begin{document}\n\n"
1539 (let* ((title-command (plist-get info
:latex-title-command
))
1540 (command (and (stringp title-command
)
1541 (format-spec title-command spec
))))
1542 (org-element-normalize-string
1543 (cond ((not (plist-get info
:with-title
)) nil
)
1544 ((string= "" title
) nil
)
1545 ((not (stringp command
)) nil
)
1546 ((string-match "\\(?:[^%]\\|^\\)%s" command
)
1547 (format command title
))
1549 ;; Table of contents.
1550 (let ((depth (plist-get info
:with-toc
)))
1552 (concat (when (wholenump depth
)
1553 (format "\\setcounter{tocdepth}{%d}\n" depth
))
1554 (plist-get info
:latex-toc-command
))))
1558 (and (plist-get info
:with-creator
)
1559 (concat (plist-get info
:creator
) "\n"))
1561 "\\end{document}")))
1565 ;;; Transcode Functions
1569 (defun org-latex-bold (bold contents info
)
1570 "Transcode BOLD from Org to LaTeX.
1571 CONTENTS is the text with bold markup. INFO is a plist holding
1572 contextual information."
1573 (org-latex--text-markup contents
'bold info
))
1578 (defun org-latex-center-block (center-block contents info
)
1579 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1580 CONTENTS holds the contents of the center block. INFO is a plist
1581 holding contextual information."
1582 (org-latex--wrap-label
1583 center-block
(format "\\begin{center}\n%s\\end{center}" contents
) info
))
1588 (defun org-latex-clock (clock contents info
)
1589 "Transcode a CLOCK element from Org to LaTeX.
1590 CONTENTS is nil. INFO is a plist holding contextual
1594 (format "\\textbf{%s} " org-clock-string
)
1595 (format (plist-get info
:latex-inactive-timestamp-format
)
1596 (concat (org-timestamp-translate (org-element-property :value clock
))
1597 (let ((time (org-element-property :duration clock
)))
1598 (and time
(format " (%s)" time
)))))
1604 (defun org-latex-code (code contents info
)
1605 "Transcode a CODE object from Org to LaTeX.
1606 CONTENTS is nil. INFO is a plist used as a communication
1608 (org-latex--text-markup (org-element-property :value code
) 'code info
))
1613 (defun org-latex-drawer (drawer contents info
)
1614 "Transcode a DRAWER element from Org to LaTeX.
1615 CONTENTS holds the contents of the block. INFO is a plist
1616 holding contextual information."
1617 (let* ((name (org-element-property :drawer-name drawer
))
1618 (output (funcall (plist-get info
:latex-format-drawer-function
)
1620 (org-latex--wrap-label drawer output info
)))
1625 (defun org-latex-dynamic-block (dynamic-block contents info
)
1626 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1627 CONTENTS holds the contents of the block. INFO is a plist
1628 holding contextual information. See `org-export-data'."
1629 (org-latex--wrap-label dynamic-block contents info
))
1634 (defun org-latex-entity (entity contents info
)
1635 "Transcode an ENTITY object from Org to LaTeX.
1636 CONTENTS are the definition itself. INFO is a plist holding
1637 contextual information."
1638 (org-element-property :latex entity
))
1643 (defun org-latex-example-block (example-block contents info
)
1644 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1645 CONTENTS is nil. INFO is a plist holding contextual
1647 (when (org-string-nw-p (org-element-property :value example-block
))
1648 (let ((environment (or (org-export-read-attribute
1649 :attr_latex example-block
:environment
)
1651 (org-latex--wrap-label
1653 (format "\\begin{%s}\n%s\\end{%s}"
1655 (org-export-format-code-default example-block info
)
1662 (defun org-latex-export-block (export-block contents info
)
1663 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1664 CONTENTS is nil. INFO is a plist holding contextual information."
1665 (when (member (org-element-property :type export-block
) '("LATEX" "TEX"))
1666 (org-remove-indentation (org-element-property :value export-block
))))
1671 (defun org-latex-export-snippet (export-snippet contents info
)
1672 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1673 CONTENTS is nil. INFO is a plist holding contextual information."
1674 (when (eq (org-export-snippet-backend export-snippet
) 'latex
)
1675 (org-element-property :value export-snippet
)))
1680 (defun org-latex-fixed-width (fixed-width contents info
)
1681 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1682 CONTENTS is nil. INFO is a plist holding contextual information."
1683 (org-latex--wrap-label
1685 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1686 (org-remove-indentation
1687 (org-element-property :value fixed-width
)))
1691 ;;;; Footnote Reference
1693 (defun org-latex-footnote-reference (footnote-reference contents info
)
1694 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1695 CONTENTS is nil. INFO is a plist holding contextual information."
1697 ;; Insert separator between two footnotes in a row.
1698 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1699 (when (eq (org-element-type prev
) 'footnote-reference
)
1700 (plist-get info
:latex-footnote-separator
)))
1702 ;; Use \footnotemark if the footnote has already been defined.
1703 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1704 (format "\\footnotemark[%s]{}"
1705 (org-export-get-footnote-number footnote-reference info
)))
1706 ;; Use \footnotemark if reference is within another footnote
1707 ;; reference, footnote definition or table cell.
1708 ((org-element-lineage footnote-reference
1709 '(footnote-reference footnote-definition table-cell
))
1711 ;; Otherwise, define it with \footnote command.
1713 (let ((def (org-export-get-footnote-definition footnote-reference info
)))
1715 (format "\\footnote{%s}" (org-trim (org-export-data def info
)))
1716 ;; Retrieve all footnote references within the footnote and
1717 ;; add their definition after it, since LaTeX doesn't support
1719 (org-latex--delayed-footnotes-definitions def info
)))))))
1724 (defun org-latex-headline (headline contents info
)
1725 "Transcode a HEADLINE element from Org to LaTeX.
1726 CONTENTS holds the contents of the headline. INFO is a plist
1727 holding contextual information."
1728 (unless (org-element-property :footnote-section-p headline
)
1729 (let* ((class (plist-get info
:latex-class
))
1730 (level (org-export-get-relative-level headline info
))
1731 (numberedp (org-export-numbered-headline-p headline info
))
1732 (class-sectioning (assoc class
(plist-get info
:latex-classes
)))
1733 ;; Section formatting will set two placeholders: one for
1734 ;; the title and the other for the contents.
1736 (let ((sec (if (functionp (nth 2 class-sectioning
))
1737 (funcall (nth 2 class-sectioning
) level numberedp
)
1738 (nth (1+ level
) class-sectioning
))))
1740 ;; No section available for that LEVEL.
1742 ;; Section format directly returned by a function. Add
1743 ;; placeholder for contents.
1744 ((stringp sec
) (concat sec
"\n%s"))
1745 ;; (numbered-section . unnumbered-section)
1746 ((not (consp (cdr sec
)))
1747 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
1748 ;; (numbered-open numbered-close)
1750 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
1751 ;; (num-in num-out no-num-in no-num-out)
1753 (if numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))
1754 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
1755 ;; Create a temporary export back-end that hard-codes
1756 ;; "\underline" within "\section" and alike.
1758 (org-export-create-backend
1761 '((underline .
(lambda (o c i
) (format "\\underline{%s}" c
))))))
1763 (org-export-data-with-backend
1764 (org-element-property :title headline
) section-back-end info
))
1766 (and (plist-get info
:with-todo-keywords
)
1767 (let ((todo (org-element-property :todo-keyword headline
)))
1768 (and todo
(org-export-data todo info
)))))
1769 (todo-type (and todo
(org-element-property :todo-type headline
)))
1770 (tags (and (plist-get info
:with-tags
)
1771 (org-export-get-tags headline info
)))
1772 (priority (and (plist-get info
:with-priority
)
1773 (org-element-property :priority headline
)))
1774 ;; Create the headline text along with a no-tag version.
1775 ;; The latter is required to remove tags from toc.
1776 (full-text (funcall (plist-get info
:latex-format-headline-function
)
1777 todo todo-type priority text tags info
))
1778 ;; Associate \label to the headline for internal links.
1779 (headline-label (org-latex--label headline info t t
))
1781 (make-string (org-element-property :pre-blank headline
) ?
\n)))
1782 (if (or (not section-fmt
) (org-export-low-level-p headline info
))
1783 ;; This is a deep sub-tree: export it as a list item. Also
1784 ;; export as items headlines for which no section format has
1786 (let ((low-level-body
1788 ;; If headline is the first sibling, start a list.
1789 (when (org-export-first-sibling-p headline info
)
1790 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
1793 (and full-text
(org-string-match-p "\\`[ \t]*\\[" full-text
)
1799 ;; If headline is not the last sibling simply return
1800 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1802 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1803 (replace-regexp-in-string
1805 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
1807 ;; This is a standard headline. Export it as a section. Add
1808 ;; an alternative heading when possible, and when this is not
1809 ;; identical to the usual heading.
1811 (funcall (plist-get info
:latex-format-headline-function
)
1812 todo todo-type priority
1813 (org-export-data-with-backend
1814 (org-export-get-alt-title headline info
)
1815 section-back-end info
)
1816 (and (eq (plist-get info
:with-tags
) t
) tags
)
1818 ;; Maybe end local TOC (see `org-latex-keyword').
1822 (let ((case-fold-search t
)
1824 (let ((first (car (org-element-contents headline
))))
1825 (and (eq (org-element-type first
) 'section
) first
))))
1826 (org-element-map section
'keyword
1828 (and (equal (org-element-property :key k
) "TOC")
1829 (let ((v (org-element-property :value k
)))
1830 (and (org-string-match-p "\\<headlines\\>" v
)
1831 (org-string-match-p "\\<local\\>" v
)
1832 (format "\\stopcontents[level-%d]" level
)))))
1835 (not (equal opt-title full-text
))
1836 (string-match "\\`\\\\\\(.+?\\){" section-fmt
))
1837 (format (replace-match "\\1[%s]" nil nil section-fmt
1)
1838 ;; Replace square brackets with parenthesis
1839 ;; since square brackets are not supported in
1840 ;; optional arguments.
1841 (replace-regexp-in-string
1842 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title
))
1844 (concat headline-label pre-blanks contents
))
1845 ;; Impossible to add an alternative heading. Fallback to
1846 ;; regular sectioning format string.
1847 (format section-fmt full-text
1848 (concat headline-label pre-blanks contents
))))))))
1850 (defun org-latex-format-headline-default-function
1851 (todo todo-type priority text tags info
)
1852 "Default format function for a headline.
1853 See `org-latex-format-headline-function' for details."
1855 (and todo
(format "{\\bfseries\\sffamily %s} " todo
))
1856 (and priority
(format "\\framebox{\\#%c} " priority
))
1859 (format "\\hfill{}\\textsc{%s}"
1860 (mapconcat (lambda (tag) (org-latex-plain-text tag info
))
1864 ;;;; Horizontal Rule
1866 (defun org-latex-horizontal-rule (horizontal-rule contents info
)
1867 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1868 CONTENTS is nil. INFO is a plist holding contextual information."
1869 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule
))
1870 (prev (org-export-get-previous-element horizontal-rule info
)))
1872 ;; Make sure the rule doesn't start at the end of the current
1873 ;; line by separating it with a blank line from previous element.
1875 (let ((prev-blank (org-element-property :post-blank prev
)))
1876 (or (not prev-blank
) (zerop prev-blank
))))
1878 (org-latex--wrap-label
1880 (format "\\rule{%s}{%s}"
1881 (or (plist-get attr
:width
) "\\linewidth")
1882 (or (plist-get attr
:thickness
) "0.5pt"))
1886 ;;;; Inline Src Block
1888 (defun org-latex-inline-src-block (inline-src-block contents info
)
1889 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1890 CONTENTS holds the contents of the item. INFO is a plist holding
1891 contextual information."
1892 (let* ((code (org-element-property :value inline-src-block
))
1893 (separator (org-latex--find-verb-separator code
)))
1894 (case (plist-get info
:latex-listings
)
1895 ;; Do not use a special package: transcode it verbatim.
1896 ((nil) (format "\\texttt{%s}" (org-latex--protect-text code
)))
1897 ;; Use minted package.
1899 (let* ((org-lang (org-element-property :language inline-src-block
))
1900 (mint-lang (or (cadr (assq (intern org-lang
)
1901 (plist-get info
:latex-minted-langs
)))
1902 (downcase org-lang
)))
1903 (options (org-latex--make-option-string
1904 (plist-get info
:latex-minted-options
))))
1905 (concat (format "\\mint%s{%s}"
1906 (if (string= options
"") "" (format "[%s]" options
))
1908 separator code separator
)))
1909 ;; Use listings package.
1911 ;; Maybe translate language's name.
1912 (let* ((org-lang (org-element-property :language inline-src-block
))
1913 (lst-lang (or (cadr (assq (intern org-lang
)
1914 (plist-get info
:latex-listings-langs
)))
1916 (options (org-latex--make-option-string
1917 (append (plist-get info
:latex-listings-options
)
1918 `(("language" ,lst-lang
))))))
1919 (concat (format "\\lstinline[%s]" options
)
1920 separator code separator
))))))
1925 (defun org-latex-inlinetask (inlinetask contents info
)
1926 "Transcode an INLINETASK element from Org to LaTeX.
1927 CONTENTS holds the contents of the block. INFO is a plist
1928 holding contextual information."
1929 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1930 (todo (and (plist-get info
:with-todo-keywords
)
1931 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1932 (and todo
(org-export-data todo info
)))))
1933 (todo-type (org-element-property :todo-type inlinetask
))
1934 (tags (and (plist-get info
:with-tags
)
1935 (org-export-get-tags inlinetask info
)))
1936 (priority (and (plist-get info
:with-priority
)
1937 (org-element-property :priority inlinetask
)))
1938 (contents (concat (org-latex--label inlinetask info
) contents
)))
1939 (funcall (plist-get info
:latex-format-inlinetask-function
)
1940 todo todo-type priority title tags contents info
)))
1942 (defun org-latex-format-inlinetask-default-function
1943 (todo todo-type priority title tags contents info
)
1944 "Default format function for a inlinetasks.
1945 See `org-latex-format-inlinetask-function' for details."
1947 (concat (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1948 (when priority
(format "\\framebox{\\#%c} " priority
))
1951 (format "\\hfill{}\\textsc{:%s:}"
1953 (lambda (tag) (org-latex-plain-text tag info
))
1955 (concat "\\begin{center}\n"
1957 "\\begin{minipage}[c]{.6\\textwidth}\n"
1959 (and (org-string-nw-p contents
)
1960 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents
))
1968 (defun org-latex-italic (italic contents info
)
1969 "Transcode ITALIC from Org to LaTeX.
1970 CONTENTS is the text with italic markup. INFO is a plist holding
1971 contextual information."
1972 (org-latex--text-markup contents
'italic info
))
1977 (defun org-latex-item (item contents info
)
1978 "Transcode an ITEM element from Org to LaTeX.
1979 CONTENTS holds the contents of the item. INFO is a plist holding
1980 contextual information."
1982 (let ((count (org-element-property :counter item
))
1984 ;; Determine level of current item to determine the
1985 ;; correct LaTeX counter to use (enumi, enumii...).
1986 (let ((parent item
) (level 0))
1987 (while (memq (org-element-type
1988 (setq parent
(org-export-get-parent parent
)))
1990 (when (and (eq (org-element-type parent
) 'plain-list
)
1991 (eq (org-element-property :type parent
)
1997 (format "\\setcounter{enum%s}{%s}\n"
1998 (nth (1- level
) '("i" "ii" "iii" "iv"))
2000 (checkbox (case (org-element-property :checkbox item
)
2001 (on "$\\boxtimes$ ")
2003 (trans "$\\boxminus$ ")))
2004 (tag (let ((tag (org-element-property :tag item
)))
2005 ;; Check-boxes must belong to the tag.
2006 (and tag
(format "[{%s}] "
2008 (org-export-data tag info
)))))))
2013 (checkbox (concat " " checkbox
))
2014 ;; Without a tag or a check-box, if CONTENTS starts with
2015 ;; an opening square bracket, add "\relax" to "\item",
2016 ;; unless the brackets comes from an initial export
2017 ;; snippet (i.e. it is inserted willingly by the user).
2019 (org-string-match-p "\\`[ \t]*\\[" contents
)
2020 (not (let ((e (car (org-element-contents item
))))
2021 (and (eq (org-element-type e
) 'paragraph
)
2022 (let ((o (car (org-element-contents e
))))
2023 (and (eq (org-element-type o
) 'export-snippet
)
2024 (eq (org-export-snippet-backend o
)
2028 (and contents
(org-trim contents
))
2029 ;; If there are footnotes references in tag, be sure to
2030 ;; add their definition at the end of the item. This
2031 ;; workaround is necessary since "\footnote{}" command is
2032 ;; not supported in tags.
2034 (org-latex--delayed-footnotes-definitions
2035 (org-element-property :tag item
) info
)))))
2040 (defun org-latex-keyword (keyword contents info
)
2041 "Transcode a KEYWORD element from Org to LaTeX.
2042 CONTENTS is nil. INFO is a plist holding contextual information."
2043 (let ((key (org-element-property :key keyword
))
2044 (value (org-element-property :value keyword
)))
2046 ((string= key
"LATEX") value
)
2047 ((string= key
"INDEX") (format "\\index{%s}" value
))
2048 ((string= key
"TOC")
2049 (let ((case-fold-search t
))
2051 ((org-string-match-p "\\<headlines\\>" value
)
2052 (let* ((localp (org-string-match-p "\\<local\\>" value
))
2053 (parent (org-element-lineage keyword
'(headline)))
2054 (level (if (not (and localp parent
)) 0
2055 (org-export-get-relative-level parent info
)))
2057 (and (string-match "\\<[0-9]+\\>" value
)
2059 "\\setcounter{tocdepth}{%d}"
2060 (+ (string-to-number (match-string 0 value
)) level
)))))
2061 (if (and localp parent
)
2062 ;; Start local TOC, assuming package "titletoc" is
2064 (format "\\startcontents[level-%d]
2065 \\printcontents[level-%d]{}{0}{%s}"
2066 level level
(or depth
""))
2067 (concat depth
(and depth
"\n") "\\tableofcontents"))))
2068 ((org-string-match-p "\\<tables\\>" value
) "\\listoftables")
2069 ((org-string-match-p "\\<listings\\>" value
)
2070 (case (plist-get info
:latex-listings
)
2071 ((nil) "\\listoffigures")
2072 (minted "\\listoflistings")
2073 (otherwise "\\lstlistoflistings")))))))))
2076 ;;;; Latex Environment
2078 (defun org-latex-latex-environment (latex-environment contents info
)
2079 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
2080 CONTENTS is nil. INFO is a plist holding contextual information."
2081 (when (plist-get info
:with-latex
)
2082 (let ((value (org-remove-indentation
2083 (org-element-property :value latex-environment
))))
2084 (if (not (org-element-property :name latex-environment
)) value
2085 ;; Environment is labeled: label must be within the environment
2086 ;; (otherwise, a reference pointing to that element will count
2087 ;; the section instead).
2090 (goto-char (point-min))
2092 (insert (org-latex--label latex-environment info nil t
))
2093 (buffer-string))))))
2098 (defun org-latex-latex-fragment (latex-fragment contents info
)
2099 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
2100 CONTENTS is nil. INFO is a plist holding contextual information."
2101 (let ((value (org-element-property :value latex-fragment
)))
2102 ;; Trim math markers since the fragment is enclosed within
2103 ;; a latex-math-block object anyway.
2104 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value
)
2105 (match-string 2 value
))
2106 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value
)
2107 (match-string 1 value
))
2113 (defun org-latex-line-break (line-break contents info
)
2114 "Transcode a LINE-BREAK object from Org to LaTeX.
2115 CONTENTS is nil. INFO is a plist holding contextual information."
2121 (defun org-latex--inline-image (link info
)
2122 "Return LaTeX code for an inline image.
2123 LINK is the link pointing to the inline image. INFO is a plist
2124 used as a communication channel."
2125 (let* ((parent (org-export-get-parent-element link
))
2126 (path (let ((raw-path (org-element-property :path link
)))
2127 (if (not (file-name-absolute-p raw-path
)) raw-path
2128 (expand-file-name raw-path
))))
2129 (filetype (file-name-extension path
))
2130 (caption (org-latex--caption/label-string parent info
))
2131 (caption-above-p (org-latex--caption-above-p link info
))
2132 ;; Retrieve latex attributes from the element around.
2133 (attr (org-export-read-attribute :attr_latex parent
))
2134 (float (let ((float (plist-get attr
:float
)))
2135 (cond ((string= float
"wrap") 'wrap
)
2136 ((string= float
"sideways") 'sideways
)
2137 ((string= float
"multicolumn") 'multicolumn
)
2139 (org-element-property :caption parent
)
2140 (org-string-nw-p (plist-get attr
:caption
)))
2141 (if (and (plist-member attr
:float
) (not float
))
2144 ((and (not float
) (plist-member attr
:float
)) nil
))))
2146 (let ((place (plist-get attr
:placement
)))
2148 (place (format "%s" place
))
2149 ((eq float
'wrap
) "{l}{0.5\\textwidth}")
2151 (format "[%s]" (plist-get info
:latex-default-figure-position
)))
2153 (comment-include (if (plist-get attr
:comment-include
) "%" ""))
2154 ;; It is possible to specify width and height in the
2155 ;; ATTR_LATEX line, and also via default variables.
2156 (width (cond ((plist-get attr
:width
))
2157 ((plist-get attr
:height
) "")
2158 ((eq float
'wrap
) "0.48\\textwidth")
2159 (t (plist-get info
:latex-image-default-width
))))
2160 (height (cond ((plist-get attr
:height
))
2161 ((or (plist-get attr
:width
)
2162 (memq float
'(figure wrap
))) "")
2163 (t (plist-get info
:latex-image-default-height
))))
2164 (options (let ((opt (or (plist-get attr
:options
)
2165 (plist-get info
:latex-image-default-option
))))
2166 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt
)) opt
2167 (match-string 1 opt
))))
2169 (if (member filetype
'("tikz" "pgf"))
2171 ;; - use \input to read in image file.
2172 ;; - if options are present, wrap in a tikzpicture environment.
2173 ;; - if width or height are present, use \resizebox to change
2176 (setq image-code
(format "\\input{%s}" path
))
2177 (when (org-string-nw-p options
)
2179 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
2182 (when (or (org-string-nw-p width
) (org-string-nw-p height
))
2183 (setq image-code
(format "\\resizebox{%s}{%s}{%s}"
2184 (if (org-string-nw-p width
) width
"!")
2185 (if (org-string-nw-p height
) height
"!")
2187 ;; For other images:
2188 ;; - add width and height to options.
2189 ;; - include the image with \includegraphics.
2190 (when (org-string-nw-p width
)
2191 (setq options
(concat options
",width=" width
)))
2192 (when (org-string-nw-p height
)
2193 (setq options
(concat options
",height=" height
)))
2194 (let ((search-option (org-element-property :search-option link
)))
2195 (when (and search-option
2196 (equal filetype
"pdf")
2197 (org-string-match-p "\\`[0-9]+\\'" search-option
)
2198 (not (org-string-match-p "page=" options
)))
2199 (setq options
(concat options
",page=" search-option
))))
2201 (format "\\includegraphics%s{%s}"
2202 (cond ((not (org-string-nw-p options
)) "")
2203 ((= (aref options
0) ?
,)
2204 (format "[%s]"(substring options
1)))
2205 (t (format "[%s]" options
)))
2207 (when (equal filetype
"svg")
2208 (setq image-code
(replace-regexp-in-string "^\\\\includegraphics"
2212 (setq image-code
(replace-regexp-in-string "\\.svg}"
2216 ;; Return proper string, depending on FLOAT.
2218 (wrap (format "\\begin{wrapfigure}%s
2221 %s\\end{wrapfigure}"
2223 (if caption-above-p caption
"")
2224 comment-include image-code
2225 (if caption-above-p
"" caption
)))
2226 (sideways (format "\\begin{sidewaysfigure}
2229 %s\\end{sidewaysfigure}"
2230 (if caption-above-p caption
"")
2231 comment-include image-code
2232 (if caption-above-p
"" caption
)))
2233 (multicolumn (format "\\begin{figure*}%s
2238 (if caption-above-p caption
"")
2239 comment-include image-code
2240 (if caption-above-p
"" caption
)))
2241 (figure (format "\\begin{figure}%s
2246 (if caption-above-p caption
"")
2247 comment-include image-code
2248 (if caption-above-p
"" caption
)))
2250 (format "\\begin{center}
2253 (if caption-above-p caption
"")
2255 (if caption-above-p
"" caption
)))
2256 (otherwise image-code
))))
2258 (defun org-latex-link (link desc info
)
2259 "Transcode a LINK object from Org to LaTeX.
2261 DESC is the description part of the link, or the empty string.
2262 INFO is a plist holding contextual information. See
2264 (let* ((type (org-element-property :type link
))
2265 (raw-path (replace-regexp-in-string
2266 "%" "\\%" (org-element-property :path link
) nil t
))
2267 ;; Ensure DESC really exists, or set it to nil.
2268 (desc (and (not (string= desc
"")) desc
))
2269 (imagep (org-export-inline-image-p
2270 link
(plist-get info
:latex-inline-image-rules
)))
2272 ((member type
'("http" "https" "ftp" "mailto" "doi"))
2273 (concat type
":" raw-path
))
2274 ((string= type
"file") (org-export-file-uri raw-path
))
2277 ;; Link type is handled by a special function.
2278 ((org-export-custom-protocol-maybe link desc
'latex
))
2280 (imagep (org-latex--inline-image link info
))
2281 ;; Radio link: Transcode target's contents and use them as link's
2283 ((string= type
"radio")
2284 (let ((destination (org-export-resolve-radio-link link info
)))
2285 (if (not destination
) desc
2286 (format "\\hyperref[%s]{%s}"
2287 (org-export-get-reference destination info
)
2289 ;; Links pointing to a headline: Find destination and build
2290 ;; appropriate referencing command.
2291 ((member type
'("custom-id" "fuzzy" "id"))
2292 (let ((destination (if (string= type
"fuzzy")
2293 (org-export-resolve-fuzzy-link link info
)
2294 (org-export-resolve-id-link link info
))))
2295 (case (org-element-type destination
)
2296 ;; Id link points to an external file.
2298 (if desc
(format "\\href{%s}{%s}" destination desc
)
2299 (format "\\url{%s}" destination
)))
2300 ;; Fuzzy link points nowhere.
2302 (format (plist-get info
:latex-link-with-unknown-path-format
)
2305 (org-element-property :raw-link link
) info
))))
2306 ;; LINK points to a headline. If headlines are numbered
2307 ;; and the link has no description, display headline's
2308 ;; number. Otherwise, display description or headline's
2311 (let ((label (org-latex--label destination info t
)))
2313 (org-export-numbered-headline-p destination info
))
2314 (format "\\ref{%s}" label
)
2315 (format "\\hyperref[%s]{%s}" label
2318 (org-element-property :title destination
) info
))))))
2319 ;; Fuzzy link points to a target. Do as above.
2321 (let ((ref (org-latex--label destination info t
)))
2322 (if (not desc
) (format "\\ref{%s}" ref
)
2323 (format "\\hyperref[%s]{%s}" ref desc
)))))))
2324 ;; Coderef: replace link with the reference name or the
2325 ;; equivalent line number.
2326 ((string= type
"coderef")
2327 (format (org-export-get-coderef-format path desc
)
2328 (org-export-resolve-coderef path info
)))
2329 ;; External link with a description part.
2330 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
2331 ;; External link without a description part.
2332 (path (format "\\url{%s}" path
))
2333 ;; No path, only description. Try to do something useful.
2334 (t (format (plist-get info
:latex-link-with-unknown-path-format
) desc
)))))
2339 (defun org-latex-node-property (node-property contents info
)
2340 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2341 CONTENTS is nil. INFO is a plist holding contextual
2344 (org-element-property :key node-property
)
2345 (let ((value (org-element-property :value node-property
)))
2346 (if value
(concat " " value
) ""))))
2351 (defun org-latex-paragraph (paragraph contents info
)
2352 "Transcode a PARAGRAPH element from Org to LaTeX.
2353 CONTENTS is the contents of the paragraph, as a string. INFO is
2354 the plist used as a communication channel."
2360 (defun org-latex-plain-list (plain-list contents info
)
2361 "Transcode a PLAIN-LIST element from Org to LaTeX.
2362 CONTENTS is the contents of the list. INFO is a plist holding
2363 contextual information."
2364 (let* ((type (org-element-property :type plain-list
))
2365 (attr (org-export-read-attribute :attr_latex plain-list
))
2366 (latex-type (let ((env (plist-get attr
:environment
)))
2367 (cond (env (format "%s" env
))
2368 ((eq type
'ordered
) "enumerate")
2369 ((eq type
'descriptive
) "description")
2371 (org-latex--wrap-label
2373 (format "\\begin{%s}%s\n%s\\end{%s}"
2375 (or (plist-get attr
:options
) "")
2383 (defun org-latex-plain-text (text info
)
2384 "Transcode a TEXT string from Org to LaTeX.
2385 TEXT is the string to transcode. INFO is a plist holding
2386 contextual information."
2387 (let* ((specialp (plist-get info
:with-special-strings
))
2389 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2390 (let ((case-fold-search nil
))
2391 (replace-regexp-in-string
2392 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2393 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2394 ;; However, if special strings are used, be careful not
2395 ;; to protect "\" in "\-" constructs.
2396 (replace-regexp-in-string
2397 (concat "[%$#&{}_~^]\\|\\\\" (and specialp
"\\([^-]\\|$\\)"))
2399 (case (string-to-char m
)
2400 (?
\\ "$\\\\backslash$\\1")
2401 (?~
"\\\\textasciitilde{}")
2405 ;; Activate smart quotes. Be sure to provide original TEXT string
2406 ;; since OUTPUT may have been modified.
2407 (when (plist-get info
:with-smart-quotes
)
2408 (setq output
(org-export-activate-smart-quotes output
:latex info text
)))
2409 ;; Convert special strings.
2411 (setq output
(replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output
)))
2412 ;; Handle break preservation if required.
2413 (when (plist-get info
:preserve-breaks
)
2414 (setq output
(replace-regexp-in-string
2415 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t
)))
2422 (defun org-latex-planning (planning contents info
)
2423 "Transcode a PLANNING element from Org to LaTeX.
2424 CONTENTS is nil. INFO is a plist holding contextual
2432 (let ((closed (org-element-property :closed planning
)))
2435 (format "\\textbf{%s} " org-closed-string
)
2436 (format (plist-get info
:latex-inactive-timestamp-format
)
2437 (org-timestamp-translate closed
)))))
2438 (let ((deadline (org-element-property :deadline planning
)))
2441 (format "\\textbf{%s} " org-deadline-string
)
2442 (format (plist-get info
:latex-active-timestamp-format
)
2443 (org-timestamp-translate deadline
)))))
2444 (let ((scheduled (org-element-property :scheduled planning
)))
2447 (format "\\textbf{%s} " org-scheduled-string
)
2448 (format (plist-get info
:latex-active-timestamp-format
)
2449 (org-timestamp-translate scheduled
)))))))
2454 ;;;; Property Drawer
2456 (defun org-latex-property-drawer (property-drawer contents info
)
2457 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2458 CONTENTS holds the contents of the drawer. INFO is a plist
2459 holding contextual information."
2460 (and (org-string-nw-p contents
)
2461 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents
)))
2464 ;;;; Pseudo Element: LaTeX Matrices
2466 ;; `latex-matrices' elements have the following properties:
2467 ;; `:caption', `:post-blank' and `:markup' (`inline', `equation' or
2470 (defun org-latex--wrap-latex-matrices (data info
)
2471 "Merge contiguous tables with the same mode within a pseudo-element.
2472 DATA is a parse tree or a secondary string. INFO is a plist
2473 containing export options. Modify DATA by side-effect and return
2475 (org-element-map data
'table
2477 (when (eq (org-element-property :type table
) 'org
)
2478 (let ((mode (or (org-export-read-attribute :attr_latex table
:mode
)
2479 (plist-get info
:latex-default-table-mode
))))
2480 (when (and (member mode
'("inline-math" "math"))
2481 ;; Do not wrap twice the same table.
2482 (not (eq (org-element-type
2483 (org-element-property :parent table
))
2485 (let* ((caption (and (not (string= mode
"inline-math"))
2486 (org-element-property :caption table
)))
2488 (list 'latex-matrices
2489 (list :caption caption
2491 (cond ((string= mode
"inline-math") 'inline
)
2495 (next (org-export-get-next-element table info
)))
2496 (org-element-insert-before matrices table
)
2497 ;; Swallow all contiguous tables sharing the same mode.
2499 (zerop (or (org-element-property :post-blank previous
) 0))
2500 (setq next
(org-export-get-next-element previous info
))
2501 (eq (org-element-type next
) 'table
)
2502 (eq (org-element-property :type next
) 'org
)
2503 (string= (or (org-export-read-attribute
2504 :attr_latex next
:mode
)
2505 (plist-get info
:latex-default-table-mode
))
2507 (org-element-extract-element previous
)
2508 (org-element-adopt-elements matrices previous
)
2509 (setq previous next
))
2510 (org-element-put-property
2511 matrices
:post-blank
(org-element-property :post-blank previous
))
2512 (org-element-extract-element previous
)
2513 (org-element-adopt-elements matrices previous
))))))
2517 (defun org-latex-matrices (matrices contents info
)
2518 "Transcode a MATRICES element from Org to LaTeX.
2519 CONTENTS is a string. INFO is a plist used as a communication
2521 (format (case (org-element-property :markup matrices
)
2523 (equation "\\begin{equation}\n%s\\end{equation}")
2527 (defun org-latex-matrices-tree-filter (tree backend info
)
2528 (org-latex--wrap-latex-matrices tree info
))
2530 ;;;; Pseudo Object: LaTeX Math Block
2532 ;; `latex-math-block' objects have the following property:
2535 (defun org-latex--wrap-latex-math-block (data info
)
2536 "Merge contiguous math objects in a pseudo-object container.
2537 DATA is a parse tree or a secondary string. INFO is a plist
2538 containing export options. Modify DATA by side-effect and return it."
2539 (let ((valid-object-p
2541 ;; Non-nil when OBJ can be added to the latex math block.
2543 (case (org-element-type obj
)
2544 (entity (org-element-property :latex-math-p obj
))
2546 (let ((value (org-element-property :value obj
)))
2547 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value
)
2548 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value
))))
2549 ((subscript superscript
) t
))))))
2550 (org-element-map data
'(entity latex-fragment subscript superscript
)
2552 ;; Skip objects already wrapped.
2553 (when (and (not (eq (org-element-type
2554 (org-element-property :parent object
))
2556 (funcall valid-object-p object
))
2557 (let ((math-block (list 'latex-math-block nil
))
2558 (next-elements (org-export-get-next-element object info t
))
2560 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2561 (org-element-insert-before math-block object
)
2562 (org-element-extract-element object
)
2563 (org-element-adopt-elements math-block object
)
2564 (when (zerop (or (org-element-property :post-blank object
) 0))
2565 ;; MATH-BLOCK swallows consecutive math objects.
2567 (dolist (next next-elements
)
2568 (if (not (funcall valid-object-p next
)) (throw 'exit nil
)
2569 (org-element-extract-element next
)
2570 (org-element-adopt-elements math-block next
)
2571 ;; Eschew the case: \beta$x$ -> \(\betax\).
2572 (unless (memq (org-element-type next
)
2573 '(subscript superscript
))
2574 (org-element-put-property last
:post-blank
1))
2576 (when (> (or (org-element-property :post-blank next
) 0) 0)
2577 (throw 'exit nil
))))))
2578 (org-element-put-property
2579 math-block
:post-blank
(org-element-property :post-blank last
)))))
2580 info nil
'(subscript superscript latex-math-block
) t
)
2581 ;; Return updated DATA.
2584 (defun org-latex-math-block-tree-filter (tree backend info
)
2585 (org-latex--wrap-latex-math-block tree info
))
2587 (defun org-latex-math-block-options-filter (info backend
)
2588 (dolist (prop '(:author
:date
:title
) info
)
2589 (plist-put info prop
2590 (org-latex--wrap-latex-math-block (plist-get info prop
) info
))))
2592 (defun org-latex-math-block (math-block contents info
)
2593 "Transcode a MATH-BLOCK object from Org to LaTeX.
2594 CONTENTS is a string. INFO is a plist used as a communication
2596 (when (org-string-nw-p contents
)
2597 (format "\\(%s\\)" (org-trim contents
))))
2601 (defun org-latex-quote-block (quote-block contents info
)
2602 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2603 CONTENTS holds the contents of the block. INFO is a plist
2604 holding contextual information."
2605 (org-latex--wrap-label
2606 quote-block
(format "\\begin{quote}\n%s\\end{quote}" contents
) info
))
2611 (defun org-latex-radio-target (radio-target text info
)
2612 "Transcode a RADIO-TARGET object from Org to LaTeX.
2613 TEXT is the text of the target. INFO is a plist holding
2614 contextual information."
2615 (format "\\label{%s}%s" (org-export-get-reference radio-target info
) text
))
2620 (defun org-latex-section (section contents info
)
2621 "Transcode a SECTION element from Org to LaTeX.
2622 CONTENTS holds the contents of the section. INFO is a plist
2623 holding contextual information."
2629 (defun org-latex-special-block (special-block contents info
)
2630 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2631 CONTENTS holds the contents of the block. INFO is a plist
2632 holding contextual information."
2633 (let ((type (org-element-property :type special-block
))
2634 (opt (org-export-read-attribute :attr_latex special-block
:options
))
2635 (caption (org-latex--caption/label-string special-block info
))
2636 (caption-above-p (org-latex--caption-above-p special-block info
)))
2637 (concat (format "\\begin{%s}%s\n" type
(or opt
""))
2638 (and caption-above-p caption
)
2640 (and (not caption-above-p
) caption
)
2641 (format "\\end{%s}" type
))))
2646 (defun org-latex-src-block (src-block contents info
)
2647 "Transcode a SRC-BLOCK element from Org to LaTeX.
2648 CONTENTS holds the contents of the item. INFO is a plist holding
2649 contextual information."
2650 (when (org-string-nw-p (org-element-property :value src-block
))
2651 (let* ((lang (org-element-property :language src-block
))
2652 (caption (org-element-property :caption src-block
))
2653 (caption-above-p (org-latex--caption-above-p src-block info
))
2654 (label (org-element-property :name src-block
))
2655 (custom-env (and lang
2656 (cadr (assq (intern lang
)
2657 org-latex-custom-lang-environments
))))
2658 (num-start (case (org-element-property :number-lines src-block
)
2659 (continued (org-export-get-loc src-block info
))
2661 (retain-labels (org-element-property :retain-labels src-block
))
2662 (attributes (org-export-read-attribute :attr_latex src-block
))
2663 (float (plist-get attributes
:float
))
2664 (listings (plist-get info
:latex-listings
)))
2666 ;; Case 1. No source fontification.
2668 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2670 (cond ((string= "multicolumn" float
)
2671 (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
2672 (plist-get info
:latex-default-figure-position
)
2673 (if caption-above-p caption-str
"")
2674 (if caption-above-p
"" caption-str
)))
2676 (if caption-above-p caption-str
"")
2678 (if caption-above-p
"" (concat "\n" caption-str
))))
2682 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2683 (org-export-format-code-default src-block info
))))))
2684 ;; Case 2. Custom environment.
2686 (let ((caption-str (org-latex--caption/label-string src-block info
)))
2687 (format "\\begin{%s}\n%s\\end{%s}\n"
2689 (concat (and caption-above-p caption-str
)
2690 (org-export-format-code-default src-block info
)
2691 (and (not caption-above-p
) caption-str
))
2693 ;; Case 3. Use minted package.
2694 ((eq listings
'minted
)
2695 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2698 ((string= "multicolumn" float
)
2699 (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
2700 (if caption-above-p caption-str
"")
2701 (if caption-above-p
"" caption-str
)))
2703 (concat (if caption-above-p caption-str
"")
2705 (if caption-above-p
"" (concat "\n" caption-str
))))
2707 (options (plist-get info
:latex-minted-options
))
2710 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2713 (org-latex--make-option-string
2714 (if (or (not num-start
) (assoc "linenos" options
))
2718 ("firstnumber" ,(number-to-string (1+ num-start
))))
2720 (let ((local-options (plist-get attributes
:options
)))
2721 (and local-options
(concat "," local-options
))))
2723 (or (cadr (assq (intern lang
)
2724 (plist-get info
:latex-minted-langs
)))
2727 (let* ((code-info (org-export-unravel-code src-block
))
2731 (org-split-string (car code-info
)
2733 (org-export-format-code
2735 (lambda (loc num ref
)
2739 ;; Ensure references are flushed to the right,
2740 ;; separated with 6 spaces from the widest line
2742 (concat (make-string (+ (- max-width
(length loc
)) 6)
2744 (format "(%s)" ref
)))))
2745 nil
(and retain-labels
(cdr code-info
)))))))
2747 (format float-env body
)))
2748 ;; Case 4. Use listings package.
2751 (or (cadr (assq (intern lang
)
2752 (plist-get info
:latex-listings-langs
)))
2756 (let ((main (org-export-get-caption src-block
))
2757 (secondary (org-export-get-caption src-block t
)))
2759 (format "{%s}" (org-export-data main info
))
2761 (org-export-data secondary info
)
2762 (org-export-data main info
))))))
2763 (lst-opt (plist-get info
:latex-listings-options
)))
2769 (org-latex--make-option-string
2773 ((and (not float
) (plist-member attributes
:float
)) nil
)
2774 ((string= "multicolumn" float
) '(("float" "*")))
2775 ((and float
(not (assoc "float" lst-opt
)))
2776 `(("float" ,(plist-get info
:latex-default-figure-position
)))))
2777 `(("language" ,lst-lang
))
2778 (if label
`(("label" ,label
)) '(("label" " ")))
2779 (if caption-str
`(("caption" ,caption-str
)) '(("caption" " ")))
2780 `(("captionpos" ,(if caption-above-p
"t" "b")))
2781 (cond ((assoc "numbers" lst-opt
) nil
)
2782 ((not num-start
) '(("numbers" "none")))
2783 ((zerop num-start
) '(("numbers" "left")))
2784 (t `(("firstnumber" ,(number-to-string (1+ num-start
)))
2785 ("numbers" "left"))))))
2786 (let ((local-options (plist-get attributes
:options
)))
2787 (and local-options
(concat "," local-options
)))))
2790 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2791 (let* ((code-info (org-export-unravel-code src-block
))
2795 (org-split-string (car code-info
) "\n")))))
2796 (org-export-format-code
2798 (lambda (loc num ref
)
2802 ;; Ensure references are flushed to the right,
2803 ;; separated with 6 spaces from the widest line of
2805 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2806 (format "(%s)" ref
)))))
2807 nil
(and retain-labels
(cdr code-info
))))))))))))
2810 ;;;; Statistics Cookie
2812 (defun org-latex-statistics-cookie (statistics-cookie contents info
)
2813 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2814 CONTENTS is nil. INFO is a plist holding contextual information."
2815 (replace-regexp-in-string
2816 "%" "\\%" (org-element-property :value statistics-cookie
) nil t
))
2821 (defun org-latex-strike-through (strike-through contents info
)
2822 "Transcode STRIKE-THROUGH from Org to LaTeX.
2823 CONTENTS is the text with strike-through markup. INFO is a plist
2824 holding contextual information."
2825 (org-latex--text-markup contents
'strike-through info
))
2830 (defun org-latex--script-size (object info
)
2831 "Transcode a subscript or superscript object.
2832 OBJECT is an Org object. INFO is a plist used as a communication
2834 (let ((type (org-element-type object
))
2836 (org-element-map (org-element-contents object
)
2837 (cons 'plain-text org-element-all-objects
)
2839 (case (org-element-type obj
)
2840 ((entity latex-fragment
)
2841 (let ((data (org-trim (org-export-data obj info
))))
2843 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2847 (match-string 1 data
)
2848 (let ((blank (org-element-property :post-blank obj
)))
2849 (and blank
(> blank
0) "\\ "))))))
2852 (format "%s\\text{%s}" output
(org-export-data obj info
))))
2856 (org-export-data obj info
)
2857 (let ((blank (org-element-property :post-blank obj
)))
2858 (and blank
(> blank
0) "\\ ")))))))
2859 info nil org-element-recursive-objects
)
2860 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2862 (concat (if (eq (org-element-type object
) 'subscript
) "_" "^")
2863 (and (> (length output
) 1) "{")
2865 (and (> (length output
) 1) "}"))))
2867 (defun org-latex-subscript (subscript contents info
)
2868 "Transcode a SUBSCRIPT object from Org to LaTeX.
2869 CONTENTS is the contents of the object. INFO is a plist holding
2870 contextual information."
2871 (org-latex--script-size subscript info
))
2876 (defun org-latex-superscript (superscript contents info
)
2877 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2878 CONTENTS is the contents of the object. INFO is a plist holding
2879 contextual information."
2880 (org-latex--script-size superscript info
))
2885 ;; `org-latex-table' is the entry point for table transcoding. It
2886 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2887 ;; delegates the job to either `org-latex--table.el-table',
2888 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2889 ;; depending of the type of the table and the mode requested.
2891 ;; `org-latex--align-string' is a subroutine used to build alignment
2892 ;; string for Org tables.
2894 (defun org-latex-table (table contents info
)
2895 "Transcode a TABLE element from Org to LaTeX.
2896 CONTENTS is the contents of the table. INFO is a plist holding
2897 contextual information."
2898 (if (eq (org-element-property :type table
) 'table.el
)
2899 ;; "table.el" table. Convert it using appropriate tools.
2900 (org-latex--table.el-table table info
)
2901 (let ((type (or (org-export-read-attribute :attr_latex table
:mode
)
2902 (plist-get info
:latex-default-table-mode
))))
2904 ;; Case 1: Verbatim table.
2905 ((string= type
"verbatim")
2906 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2907 ;; Re-create table, without affiliated keywords.
2908 (org-trim (org-element-interpret-data
2909 `(table nil
,@(org-element-contents table
))))))
2911 ((or (string= type
"math") (string= type
"inline-math"))
2912 (org-latex--math-table table info
))
2913 ;; Case 3: Standard table.
2914 (t (concat (org-latex--org-table table contents info
)
2915 ;; When there are footnote references within the
2916 ;; table, insert their definition just after it.
2917 (org-latex--delayed-footnotes-definitions table info
)))))))
2919 (defun org-latex--align-string (table info
)
2920 "Return an appropriate LaTeX alignment string.
2921 TABLE is the considered table. INFO is a plist used as
2922 a communication channel."
2923 (or (org-export-read-attribute :attr_latex table
:align
)
2925 ;; Extract column groups and alignment from first (non-rule)
2928 (org-element-map table
'table-row
2930 (and (eq (org-element-property :type row
) 'standard
) row
))
2934 (let ((borders (org-export-table-cell-borders cell info
)))
2935 ;; Check left border for the first cell only.
2936 (when (and (memq 'left borders
) (not align
))
2938 (push (case (org-export-table-cell-alignment cell info
)
2943 (when (memq 'right borders
) (push "|" align
))))
2945 (apply 'concat
(nreverse align
)))))
2947 (defun org-latex--org-table (table contents info
)
2948 "Return appropriate LaTeX code for an Org table.
2950 TABLE is the table type element to transcode. CONTENTS is its
2951 contents, as a string. INFO is a plist used as a communication
2954 This function assumes TABLE has `org' as its `:type' property and
2955 `table' as its `:mode' attribute."
2956 (let* ((caption (org-latex--caption/label-string table info
))
2957 (attr (org-export-read-attribute :attr_latex table
))
2958 ;; Determine alignment string.
2959 (alignment (org-latex--align-string table info
))
2960 ;; Determine environment for the table: longtable, tabular...
2961 (table-env (or (plist-get attr
:environment
)
2962 (plist-get info
:latex-default-table-environment
)))
2963 ;; If table is a float, determine environment: table, table*
2964 ;; or sidewaystable.
2965 (float-env (unless (member table-env
'("longtable" "longtabu"))
2966 (let ((float (plist-get attr
:float
)))
2968 ((and (not float
) (plist-member attr
:float
)) nil
)
2969 ((or (string= float
"sidewaystable")
2970 (string= float
"sideways")) "sidewaystable")
2971 ((string= float
"multicolumn") "table*")
2973 (org-element-property :caption table
)
2974 (org-string-nw-p (plist-get attr
:caption
)))
2976 ;; Extract others display options.
2977 (fontsize (let ((font (plist-get attr
:font
)))
2978 (and font
(concat font
"\n"))))
2979 ;; "tabular" environment doesn't allow to define a width.
2980 (width (and (not (equal table-env
"tabular")) (plist-get attr
:width
)))
2981 (spreadp (plist-get attr
:spread
))
2983 (or (plist-get attr
:placement
)
2984 (format "[%s]" (plist-get info
:latex-default-figure-position
))))
2985 (centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2986 (plist-get info
:latex-tables-centered
)))
2987 (caption-above-p (org-latex--caption-above-p table info
)))
2988 ;; Prepare the final format string for the table.
2991 ((equal "longtable" table-env
)
2992 (concat (and fontsize
(concat "{" fontsize
))
2993 (format "\\begin{longtable}{%s}\n" alignment
)
2994 (and caption-above-p
2995 (org-string-nw-p caption
)
2996 (concat caption
"\\\\\n"))
2998 (and (not caption-above-p
)
2999 (org-string-nw-p caption
)
3000 (concat caption
"\\\\\n"))
3001 "\\end{longtable}\n"
3002 (and fontsize
"}")))
3004 ((equal "longtabu" table-env
)
3005 (concat (and fontsize
(concat "{" fontsize
))
3006 (format "\\begin{longtabu}%s{%s}\n"
3009 (if spreadp
"spread" "to") width
) "")
3011 (and caption-above-p
3012 (org-string-nw-p caption
)
3013 (concat caption
"\\\\\n"))
3015 (and (not caption-above-p
)
3016 (org-string-nw-p caption
)
3017 (concat caption
"\\\\\n"))
3019 (and fontsize
"}")))
3023 (concat (format "\\begin{%s}%s\n" float-env placement
)
3024 (if caption-above-p caption
"")
3025 (when centerp
"\\centering\n")
3027 ((and (not float-env
) caption
)
3029 (and centerp
"\\begin{center}\n" )
3030 (if caption-above-p caption
"")
3031 (cond ((and fontsize centerp
) fontsize
)
3032 (fontsize (concat "{" fontsize
)))))
3033 (centerp (concat "\\begin{center}\n" fontsize
))
3034 (fontsize (concat "{" fontsize
)))
3035 (cond ((equal "tabu" table-env
)
3036 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
3038 (if spreadp
" spread %s " " to %s ")
3042 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
3044 (if width
(format "{%s}" width
) "")
3050 (concat (if caption-above-p
"" (concat "\n" caption
))
3051 (format "\n\\end{%s}" float-env
)))
3052 ((and (not float-env
) caption
)
3054 (if caption-above-p
"" (concat "\n" caption
))
3055 (and centerp
"\n\\end{center}")
3056 (and fontsize
(not centerp
) "}")))
3057 (centerp "\n\\end{center}")
3058 (fontsize "}")))))))
3060 (defun org-latex--table.el-table
(table info
)
3061 "Return appropriate LaTeX code for a table.el table.
3063 TABLE is the table type element to transcode. INFO is a plist
3064 used as a communication channel.
3066 This function assumes TABLE has `table.el' as its `:type'
3069 ;; Ensure "*org-export-table*" buffer is empty.
3070 (with-current-buffer (get-buffer-create "*org-export-table*")
3072 (let ((output (with-temp-buffer
3073 (insert (org-element-property :value table
))
3075 (re-search-forward "^[ \t]*|[^|]" nil t
)
3076 (table-generate-source 'latex
"*org-export-table*")
3077 (with-current-buffer "*org-export-table*"
3078 (org-trim (buffer-string))))))
3079 (kill-buffer (get-buffer "*org-export-table*"))
3080 ;; Remove left out comments.
3081 (while (string-match "^%.*\n" output
)
3082 (setq output
(replace-match "" t t output
)))
3083 (let ((attr (org-export-read-attribute :attr_latex table
)))
3084 (when (plist-get attr
:rmlines
)
3085 ;; When the "rmlines" attribute is provided, remove all hlines
3086 ;; but the the one separating heading from the table body.
3087 (let ((n 0) (pos 0))
3088 (while (and (< (length output
) pos
)
3089 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
3091 (unless (= n
2) (setq output
(replace-match "" nil nil output
))))))
3092 (let ((centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
3093 (plist-get info
:latex-tables-centered
))))
3094 (if (not centerp
) output
3095 (format "\\begin{center}\n%s\n\\end{center}" output
))))))
3097 (defun org-latex--math-table (table info
)
3098 "Return appropriate LaTeX code for a matrix.
3100 TABLE is the table type element to transcode. INFO is a plist
3101 used as a communication channel.
3103 This function assumes TABLE has `org' as its `:type' property and
3104 `inline-math' or `math' as its `:mode' attribute."
3105 (let* ((attr (org-export-read-attribute :attr_latex table
))
3106 (env (or (plist-get attr
:environment
)
3107 (plist-get info
:latex-default-table-environment
)))
3111 ;; Ignore horizontal rules.
3112 (when (eq (org-element-property :type row
) 'standard
)
3113 ;; Return each cell unmodified.
3117 (substring (org-element-interpret-data cell
) 0 -
1))
3118 (org-element-map row
'table-cell
#'identity info
) "&")
3119 (or (cdr (assoc env org-latex-table-matrix-macros
)) "\\\\")
3121 (org-element-map table
'table-row
#'identity info
) "")))
3124 (plist-get attr
:math-prefix
)
3125 ;; Environment. Also treat special cases.
3126 (cond ((member env
'("array" "tabular"))
3127 (let ((align (make-string
3128 (cdr (org-export-table-dimensions table info
)) ?c
)))
3129 (format "\\begin{%s}{%s}\n%s\\end{%s}" env align contents env
)))
3130 ((assoc env org-latex-table-matrix-macros
)
3131 (format "\\%s%s{\n%s}"
3133 (or (plist-get attr
:math-arguments
) "")
3135 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env
)))
3137 (plist-get attr
:math-suffix
))))
3142 (defun org-latex-table-cell (table-cell contents info
)
3143 "Transcode a TABLE-CELL element from Org to LaTeX.
3144 CONTENTS is the cell contents. INFO is a plist used as
3145 a communication channel."
3147 (let ((scientific-format (plist-get info
:latex-table-scientific-notation
)))
3150 (string-match orgtbl-exp-regexp contents
))
3151 ;; Use appropriate format string for scientific
3153 (format scientific-format
3154 (match-string 1 contents
)
3155 (match-string 2 contents
))
3157 (when (org-export-get-next-element table-cell info
) " & ")))
3162 (defun org-latex-table-row (table-row contents info
)
3163 "Transcode a TABLE-ROW element from Org to LaTeX.
3164 CONTENTS is the contents of the row. INFO is a plist used as
3165 a communication channel."
3166 (let* ((attr (org-export-read-attribute :attr_latex
3167 (org-export-get-parent table-row
)))
3168 (booktabsp (if (plist-member attr
:booktabs
) (plist-get attr
:booktabs
)
3169 (plist-get info
:latex-tables-booktabs
)))
3171 (member (or (plist-get attr
:environment
)
3172 (plist-get info
:latex-default-table-environment
))
3173 '("longtable" "longtabu"))))
3174 (if (eq (org-element-property :type table-row
) 'rule
)
3176 ((not booktabsp
) "\\hline")
3177 ((not (org-export-get-previous-element table-row info
)) "\\toprule")
3178 ((not (org-export-get-next-element table-row info
)) "\\bottomrule")
3180 (org-export-table-row-ends-header-p
3181 (org-export-get-previous-element table-row info
) info
))
3185 ;; When BOOKTABS are activated enforce top-rule even when no
3186 ;; hline was specifically marked.
3187 (and booktabsp
(not (org-export-get-previous-element table-row info
))
3191 ;; Special case for long tables. Define header and footers.
3192 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
3193 (let ((columns (cdr (org-export-table-dimensions
3194 (org-export-get-parent-table table-row
) info
))))
3197 \\multicolumn{%d}{l}{%s} \\\\
3202 %s\\multicolumn{%d}{r}{%s} \\\\
3205 (if booktabsp
"\\midrule" "\\hline")
3207 (org-latex--translate "Continued from previous page" info
)
3209 ((not (org-export-table-row-starts-header-p table-row info
))
3211 (booktabsp "\\toprule\n")
3214 (if booktabsp
"\\midrule" "\\hline")
3215 (if booktabsp
"\\midrule" "\\hline")
3217 (org-latex--translate "Continued on next page" info
))))
3218 ;; When BOOKTABS are activated enforce bottom rule even when
3219 ;; no hline was specifically marked.
3220 ((and booktabsp
(not (org-export-get-next-element table-row info
)))
3221 "\\bottomrule"))))))
3226 (defun org-latex-target (target contents info
)
3227 "Transcode a TARGET object from Org to LaTeX.
3228 CONTENTS is nil. INFO is a plist holding contextual
3230 (format "\\label{%s}" (org-latex--label target info
)))
3235 (defun org-latex-timestamp (timestamp contents info
)
3236 "Transcode a TIMESTAMP object from Org to LaTeX.
3237 CONTENTS is nil. INFO is a plist holding contextual
3239 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp
) info
)))
3242 (case (org-element-property :type timestamp
)
3243 ((active active-range
) :latex-active-timestamp-format
)
3244 ((inactive inactive-range
) :latex-inactive-timestamp-format
)
3245 (otherwise :latex-diary-timestamp-format
)))
3251 (defun org-latex-underline (underline contents info
)
3252 "Transcode UNDERLINE from Org to LaTeX.
3253 CONTENTS is the text with underline markup. INFO is a plist
3254 holding contextual information."
3255 (org-latex--text-markup contents
'underline info
))
3260 (defun org-latex-verbatim (verbatim contents info
)
3261 "Transcode a VERBATIM object from Org to LaTeX.
3262 CONTENTS is nil. INFO is a plist used as a communication
3264 (org-latex--text-markup
3265 (org-element-property :value verbatim
) 'verbatim info
))
3270 (defun org-latex-verse-block (verse-block contents info
)
3271 "Transcode a VERSE-BLOCK element from Org to LaTeX.
3272 CONTENTS is verse block contents. INFO is a plist holding
3273 contextual information."
3274 (org-latex--wrap-label
3276 ;; In a verse environment, add a line break to each newline
3277 ;; character and change each white space at beginning of a line
3278 ;; into a space of 1 em. Also change each blank line with
3279 ;; a vertical space of 1 em.
3280 (format "\\begin{verse}\n%s\\end{verse}"
3281 (replace-regexp-in-string
3282 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m
)))
3283 (replace-regexp-in-string
3284 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
3285 (replace-regexp-in-string
3286 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
3287 contents nil t
) nil t
) nil t
))
3292 ;;; End-user functions
3295 (defun org-latex-export-as-latex
3296 (&optional async subtreep visible-only body-only ext-plist
)
3297 "Export current buffer as a LaTeX buffer.
3299 If narrowing is active in the current buffer, only export its
3302 If a region is active, export that region.
3304 A non-nil optional argument ASYNC means the process should happen
3305 asynchronously. The resulting buffer should be accessible
3306 through the `org-export-stack' interface.
3308 When optional argument SUBTREEP is non-nil, export the sub-tree
3309 at point, extracting information from the headline properties
3312 When optional argument VISIBLE-ONLY is non-nil, don't export
3313 contents of hidden elements.
3315 When optional argument BODY-ONLY is non-nil, only write code
3316 between \"\\begin{document}\" and \"\\end{document}\".
3318 EXT-PLIST, when provided, is a property list with external
3319 parameters overriding Org default settings, but still inferior to
3320 file-local settings.
3322 Export is done in a buffer named \"*Org LATEX Export*\", which
3323 will be displayed when `org-export-show-temporary-export-buffer'
3326 (org-export-to-buffer 'latex
"*Org LATEX Export*"
3327 async subtreep visible-only body-only ext-plist
(lambda () (LaTeX-mode))))
3330 (defun org-latex-convert-region-to-latex ()
3331 "Assume the current region has org-mode syntax, and convert it to LaTeX.
3332 This can be used in any buffer. For example, you can write an
3333 itemized list in org-mode syntax in an LaTeX buffer and use this
3334 command to convert it."
3336 (org-export-replace-region-by 'latex
))
3339 (defun org-latex-export-to-latex
3340 (&optional async subtreep visible-only body-only ext-plist
)
3341 "Export current buffer to a LaTeX file.
3343 If narrowing is active in the current buffer, only export its
3346 If a region is active, export that region.
3348 A non-nil optional argument ASYNC means the process should happen
3349 asynchronously. The resulting file should be accessible through
3350 the `org-export-stack' interface.
3352 When optional argument SUBTREEP is non-nil, export the sub-tree
3353 at point, extracting information from the headline properties
3356 When optional argument VISIBLE-ONLY is non-nil, don't export
3357 contents of hidden elements.
3359 When optional argument BODY-ONLY is non-nil, only write code
3360 between \"\\begin{document}\" and \"\\end{document}\".
3362 EXT-PLIST, when provided, is a property list with external
3363 parameters overriding Org default settings, but still inferior to
3364 file-local settings."
3366 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
3367 (org-export-to-file 'latex outfile
3368 async subtreep visible-only body-only ext-plist
)))
3371 (defun org-latex-export-to-pdf
3372 (&optional async subtreep visible-only body-only ext-plist
)
3373 "Export current buffer to LaTeX then process through to PDF.
3375 If narrowing is active in the current buffer, only export its
3378 If a region is active, export that region.
3380 A non-nil optional argument ASYNC means the process should happen
3381 asynchronously. The resulting file should be accessible through
3382 the `org-export-stack' interface.
3384 When optional argument SUBTREEP is non-nil, export the sub-tree
3385 at point, extracting information from the headline properties
3388 When optional argument VISIBLE-ONLY is non-nil, don't export
3389 contents of hidden elements.
3391 When optional argument BODY-ONLY is non-nil, only write code
3392 between \"\\begin{document}\" and \"\\end{document}\".
3394 EXT-PLIST, when provided, is a property list with external
3395 parameters overriding Org default settings, but still inferior to
3396 file-local settings.
3398 Return PDF file's name."
3400 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
3401 (org-export-to-file 'latex outfile
3402 async subtreep visible-only body-only ext-plist
3403 (lambda (file) (org-latex-compile file
)))))
3405 (defun org-latex-compile (texfile &optional snippet
)
3406 "Compile a TeX file.
3408 TEXFILE is the name of the file being compiled. Processing is
3409 done through the command specified in `org-latex-pdf-process'.
3411 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3412 file used to preview a LaTeX snippet. In this case, do not
3413 create a log buffer and do not bother removing log files.
3415 Return PDF file name or an error if it couldn't be produced."
3416 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile
)))
3417 (full-name (file-truename texfile
))
3418 (out-dir (file-name-directory texfile
))
3419 ;; Properly set working directory for compilation.
3420 (default-directory (if (file-name-absolute-p texfile
)
3421 (file-name-directory full-name
)
3423 (time (current-time))
3425 (unless snippet
(message (format "Processing LaTeX file %s..." texfile
)))
3426 (save-window-excursion
3428 ;; A function is provided: Apply it.
3429 ((functionp org-latex-pdf-process
)
3430 (funcall org-latex-pdf-process
(shell-quote-argument texfile
)))
3431 ;; A list is provided: Replace %b, %f and %o with appropriate
3432 ;; values in each command before applying it. Output is
3433 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3434 ((consp org-latex-pdf-process
)
3435 (let ((outbuf (and (not snippet
)
3436 (get-buffer-create "*Org PDF LaTeX Output*"))))
3437 (dolist (command org-latex-pdf-process
)
3439 (replace-regexp-in-string
3440 "%b" (shell-quote-argument base-name
)
3441 (replace-regexp-in-string
3442 "%f" (shell-quote-argument full-name
)
3443 (replace-regexp-in-string
3444 "%o" (shell-quote-argument out-dir
) command t t
) t t
) t t
)
3446 ;; Collect standard errors from output buffer.
3447 (setq warnings
(and (not snippet
)
3448 (org-latex--collect-warnings outbuf
)))))
3449 (t (error "No valid command to process to PDF")))
3450 (let ((pdffile (concat out-dir base-name
".pdf")))
3451 ;; Check for process failure. Provide collected errors if
3453 (if (or (not (file-exists-p pdffile
))
3454 (time-less-p (nth 5 (file-attributes pdffile
)) time
))
3455 (error (format "PDF file %s wasn't produced" pdffile
))
3456 ;; Else remove log files, when specified, and signal end of
3457 ;; process to user, along with any error encountered.
3459 (when org-latex-remove-logfiles
3460 (dolist (file (directory-files
3462 (concat (regexp-quote base-name
)
3463 "\\(?:\\.[0-9]+\\)?"
3465 (regexp-opt org-latex-logfiles-extensions
))))
3466 (delete-file file
)))
3467 (message (concat "PDF file produced"
3469 ((eq warnings
'error
) " with errors.")
3470 (warnings (concat " with warnings: " warnings
))
3472 ;; Return output file name.
3475 (defun org-latex--collect-warnings (buffer)
3476 "Collect some warnings from \"pdflatex\" command output.
3477 BUFFER is the buffer containing output. Return collected
3478 warnings types as a string, `error' if a LaTeX error was
3479 encountered or nil if there was none."
3480 (with-current-buffer buffer
3482 (goto-char (point-max))
3483 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t
)
3484 (if (re-search-forward "^!" nil t
) 'error
3485 (let ((case-fold-search t
)
3487 (dolist (warning org-latex-known-warnings
)
3488 (when (save-excursion (re-search-forward (car warning
) nil t
))
3489 (setq warnings
(concat warnings
" " (cdr warning
)))))
3490 (org-string-nw-p (org-trim warnings
))))))))
3493 (defun org-latex-publish-to-latex (plist filename pub-dir
)
3494 "Publish an Org file to LaTeX.
3496 FILENAME is the filename of the Org file to be published. PLIST
3497 is the property list for the given project. PUB-DIR is the
3498 publishing directory.
3500 Return output file name."
3501 (org-publish-org-to 'latex filename
".tex" plist pub-dir
))
3504 (defun org-latex-publish-to-pdf (plist filename pub-dir
)
3505 "Publish an Org file to PDF (via LaTeX).
3507 FILENAME is the filename of the Org file to be published. PLIST
3508 is the property list for the given project. PUB-DIR is the
3509 publishing directory.
3511 Return output file name."
3512 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3513 ;; in working directory and then moved to publishing directory.
3514 (org-publish-attachment
3518 'latex filename
".tex" plist
(file-name-directory filename
)))
3525 ;; generated-autoload-file: "org-loaddefs.el"
3528 ;;; ox-latex.el ends here