1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2014 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 (comment .
(lambda (&rest args
) ""))
47 (comment-block .
(lambda (&rest args
) ""))
48 (drawer . org-latex-drawer
)
49 (dynamic-block . org-latex-dynamic-block
)
50 (entity . org-latex-entity
)
51 (example-block . org-latex-example-block
)
52 (export-snippet . org-latex-export-snippet
)
53 (fixed-width . org-latex-fixed-width
)
54 (footnote-definition . org-latex-footnote-definition
)
55 (footnote-reference . org-latex-footnote-reference
)
56 (headline . org-latex-headline
)
57 (horizontal-rule . org-latex-horizontal-rule
)
58 (inline-src-block . org-latex-inline-src-block
)
59 (inlinetask . org-latex-inlinetask
)
60 (italic . org-latex-italic
)
61 (item . org-latex-item
)
62 (keyword . org-latex-keyword
)
63 (latex-environment . org-latex-latex-environment
)
64 (latex-fragment . org-latex-latex-fragment
)
65 (line-break . org-latex-line-break
)
66 (link . org-latex-link
)
67 (node-property . org-latex-node-property
)
68 (paragraph . org-latex-paragraph
)
69 (plain-list . org-latex-plain-list
)
70 (plain-text . org-latex-plain-text
)
71 (planning . org-latex-planning
)
72 (property-drawer . org-latex-property-drawer
)
73 (quote-block . org-latex-quote-block
)
74 (radio-target . org-latex-radio-target
)
75 (section . org-latex-section
)
76 (special-block . org-latex-special-block
)
77 (src-block . org-latex-src-block
)
78 (statistics-cookie . org-latex-statistics-cookie
)
79 (strike-through . org-latex-strike-through
)
80 (subscript . org-latex-subscript
)
81 (superscript . org-latex-superscript
)
82 (table . org-latex-table
)
83 (table-cell . org-latex-table-cell
)
84 (table-row . org-latex-table-row
)
85 (target . org-latex-target
)
86 (template . org-latex-template
)
87 (timestamp . org-latex-timestamp
)
88 (underline . org-latex-underline
)
89 (verbatim . org-latex-verbatim
)
90 (verse-block . org-latex-verse-block
)
92 (latex-math-block . org-latex-math-block
))
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
))
106 '((:latex-class
"LATEX_CLASS" nil org-latex-default-class t
)
107 (:latex-class-options
"LATEX_CLASS_OPTIONS" nil nil t
)
108 (:latex-header
"LATEX_HEADER" nil nil newline
)
109 (:latex-header-extra
"LATEX_HEADER_EXTRA" nil nil newline
)
111 (:latex-active-timestamp-format nil nil org-latex-active-timestamp-format
)
112 (:latex-classes nil nil org-latex-classes
)
113 (:latex-custom-id-labels nil nil org-latex-custom-id-as-label
)
114 (:latex-default-figure-position nil nil org-latex-default-figure-position
)
115 (:latex-default-table-environment nil nil org-latex-default-table-environment
)
116 (:latex-default-table-mode nil nil org-latex-default-table-mode
)
117 (:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format
)
118 (:latex-footnote-separator nil nil org-latex-footnote-separator
)
119 (:latex-format-drawer-function nil nil org-latex-format-drawer-function
)
120 (:latex-format-headline-function nil nil org-latex-format-headline-function
)
121 (:latex-format-inlinetask-function nil nil org-latex-format-inlinetask-function
)
122 (:latex-hyperref-template nil nil org-latex-hyperref-template t
)
123 (:latex-image-default-height nil nil org-latex-image-default-height
)
124 (:latex-image-default-option nil nil org-latex-image-default-option
)
125 (:latex-image-default-width nil nil org-latex-image-default-width
)
126 (:latex-inactive-timestamp-format nil nil org-latex-inactive-timestamp-format
)
127 (:latex-inline-image-rules nil nil org-latex-inline-image-rules
)
128 (:latex-link-with-unknown-path-format nil nil org-latex-link-with-unknown-path-format
)
129 (:latex-listings nil nil org-latex-listings
)
130 (:latex-listings-langs nil nil org-latex-listings-langs
)
131 (:latex-listings-options nil nil org-latex-listings-options
)
132 (:latex-minted-langs nil nil org-latex-minted-langs
)
133 (:latex-minted-options nil nil org-latex-minted-options
)
134 (:latex-table-caption-above nil nil org-latex-table-caption-above
)
135 (:latex-table-scientific-notation nil nil org-latex-table-scientific-notation
)
136 (:latex-tables-booktabs nil nil org-latex-tables-booktabs
)
137 (:latex-tables-centered nil nil org-latex-tables-centered
)
138 (:latex-text-markup-alist nil nil org-latex-text-markup-alist
)
139 (:latex-title-command nil nil org-latex-title-command
)
140 (:latex-toc-command nil nil org-latex-toc-command
)
141 ;; Redefine regular options.
142 (:date
"DATE" nil
"\\today" t
)))
146 ;;; Internal Variables
148 (defconst org-latex-babel-language-alist
149 '(("af" .
"afrikaans")
151 ("bt-br" .
"brazilian")
157 ("de-at" .
"naustrian")
158 ("de-de" .
"ngerman")
161 ("en-au" .
"australian")
162 ("en-ca" .
"canadian")
163 ("en-gb" .
"british")
165 ("en-nz" .
"newzealand")
166 ("en-us" .
"american")
172 ("fr-ca" .
"canadien")
176 ("id" .
"indonesian")
186 ("pt" .
"portuguese")
190 ("sb" .
"uppersorbian")
198 ("uk" .
"ukrainian"))
199 "Alist between language code and corresponding Babel option.")
201 (defconst org-latex-table-matrix-macros
'(("bordermatrix" .
"\\cr")
202 ("qbordermatrix" .
"\\cr")
203 ("kbordermatrix" .
"\\\\"))
204 "Alist between matrix macros and their row ending.")
206 (defconst org-latex-pseudo-objects
'(latex-math-block)
207 "List of pseudo-object types introduced in the back-end.")
211 ;;; User Configurable Variables
213 (defgroup org-export-latex nil
214 "Options for exporting Org mode files to LaTeX."
215 :tag
"Org Export LaTeX"
221 (defcustom org-latex-default-class
"article"
222 "The default LaTeX class."
223 :group
'org-export-latex
224 :type
'(string :tag
"LaTeX class"))
226 (defcustom org-latex-classes
228 "\\documentclass[11pt]{article}"
229 ("\\section{%s}" .
"\\section*{%s}")
230 ("\\subsection{%s}" .
"\\subsection*{%s}")
231 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
232 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
233 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
235 "\\documentclass[11pt]{report}"
236 ("\\part{%s}" .
"\\part*{%s}")
237 ("\\chapter{%s}" .
"\\chapter*{%s}")
238 ("\\section{%s}" .
"\\section*{%s}")
239 ("\\subsection{%s}" .
"\\subsection*{%s}")
240 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
242 "\\documentclass[11pt]{book}"
243 ("\\part{%s}" .
"\\part*{%s}")
244 ("\\chapter{%s}" .
"\\chapter*{%s}")
245 ("\\section{%s}" .
"\\section*{%s}")
246 ("\\subsection{%s}" .
"\\subsection*{%s}")
247 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
248 "Alist of LaTeX classes and associated header and structure.
249 If #+LATEX_CLASS is set in the buffer, use its value and the
250 associated information. Here is the structure of each cell:
254 \(numbered-section . unnumbered-section)
260 The HEADER-STRING is the header that will be inserted into the
261 LaTeX file. It should contain the \\documentclass macro, and
262 anything else that is needed for this setup. To this header, the
263 following commands will be added:
265 - Calls to \\usepackage for all packages mentioned in the
266 variables `org-latex-default-packages-alist' and
267 `org-latex-packages-alist'. Thus, your header definitions
268 should avoid to also request these packages.
270 - Lines specified via \"#+LATEX_HEADER:\" and
271 \"#+LATEX_HEADER_EXTRA:\" keywords.
273 If you need more control about the sequence in which the header
274 is built up, or if you want to exclude one of these building
275 blocks for a particular class, you can use the following
276 macro-like placeholders.
278 [DEFAULT-PACKAGES] \\usepackage statements for default packages
279 [NO-DEFAULT-PACKAGES] do not include any of the default packages
280 [PACKAGES] \\usepackage statements for packages
281 [NO-PACKAGES] do not include the packages
282 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
283 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
287 \\documentclass{article}
288 [NO-DEFAULT-PACKAGES]
290 \\providecommand{\\alert}[1]{\\textbf{#1}}
293 will omit the default packages, and will include the
294 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
295 to \\providecommand, and then place \\usepackage commands based
296 on the content of `org-latex-packages-alist'.
298 If your header, `org-latex-default-packages-alist' or
299 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
300 AUTO will automatically be replaced with a coding system derived
301 from `buffer-file-coding-system'. See also the variable
302 `org-latex-inputenc-alist' for a way to influence this mechanism.
304 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\",
305 AUTO will be replaced with the language related to the language
306 code specified by `org-export-default-language', which see. Note
307 that constructions such as \"\\usepackage[french,AUTO,english]{babel}\"
310 The sectioning structure
311 ------------------------
313 The sectioning structure of the class is given by the elements
314 following the header string. For each sectioning level, a number
315 of strings is specified. A %s formatter is mandatory in each
316 section string and will be replaced by the title of the section.
318 Instead of a cons cell (numbered . unnumbered), you can also
319 provide a list of 2 or 4 elements,
321 \(numbered-open numbered-close)
325 \(numbered-open numbered-close unnumbered-open unnumbered-close)
327 providing opening and closing strings for a LaTeX environment
328 that should represent the document section. The opening clause
329 should have a %s to represent the section title.
331 Instead of a list of sectioning commands, you can also specify
332 a function name. That function will be called with two
333 parameters, the (reduced) level of the headline, and a predicate
334 non-nil when the headline should be numbered. It must return
335 a format string in which the section title will be added."
336 :group
'org-export-latex
338 (list (string :tag
"LaTeX class")
339 (string :tag
"LaTeX header")
340 (repeat :tag
"Levels" :inline t
343 (string :tag
" numbered")
344 (string :tag
"unnumbered"))
345 (list :tag
"Environment"
346 (string :tag
"Opening (numbered)")
347 (string :tag
"Closing (numbered)")
348 (string :tag
"Opening (unnumbered)")
349 (string :tag
"Closing (unnumbered)"))
350 (function :tag
"Hook computing sectioning"))))))
352 (defcustom org-latex-inputenc-alist nil
353 "Alist of inputenc coding system names, and what should really be used.
354 For example, adding an entry
356 (\"utf8\" . \"utf8x\")
358 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
359 are written as utf8 files."
360 :group
'org-export-latex
363 (string :tag
"Derived from buffer")
364 (string :tag
"Use this instead"))))
366 (defcustom org-latex-title-command
"\\maketitle"
367 "The command used to insert the title just after \\begin{document}.
368 If this string contains the formatting specification \"%s\" then
369 it will be used as a formatting string, passing the title as an
371 :group
'org-export-latex
374 (defcustom org-latex-toc-command
"\\tableofcontents\n\n"
375 "LaTeX command to set the table of contents, list of figures, etc.
376 This command only applies to the table of contents generated with
377 the toc:nil option, not to those generated with #+TOC keyword."
378 :group
'org-export-latex
381 (defcustom org-latex-hyperref-template
382 "\\hypersetup{\n pdfkeywords={%k},\n pdfsubject={%d},\n pdfcreator={%c}}\n"
383 "Template for hyperref package options.
385 Value is a format string, which can contain the following placeholders:
388 %d for DESCRIPTION line
391 Set it to the empty string to ignore the command completely."
392 :group
'org-export-latex
394 :package-version
'(Org .
"8.3")
399 (defcustom org-latex-format-headline-function
400 'org-latex-format-headline-default-function
401 "Function for formatting the headline's text.
403 This function will be called with six arguments:
404 TODO the todo keyword (string or nil)
405 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
406 PRIORITY the priority of the headline (integer or nil)
407 TEXT the main headline text (string)
408 TAGS the tags (list of strings or nil)
409 INFO the export options (plist)
411 The function result will be used in the section format string."
412 :group
'org-export-latex
414 :package-version
'(Org .
"8.0")
417 (defcustom org-latex-custom-id-as-label nil
418 "Toggle use of CUSTOM_ID properties for generating section labels.
420 When this variable is non-nil, Org will use the value of a
421 headline's CUSTOM_ID property as the key for the \\label command
422 for the LaTeX section corresponding to the headline.
424 By default, Org generates its own internal section labels for all
425 headlines during LaTeX export. This process ensures that the
426 \\label keys are unique and valid, but it means the keys are not
427 available in advance of the export process.
429 Setting this variable gives you control over how Org generates
430 labels for sections during LaTeX export, so that you may know
431 their keys in advance. One reason to do this is that it allows
432 you to refer to headlines using a single label both in Org's link
433 syntax and in embedded LaTeX code.
435 For example, when this variable is non-nil, a headline like this:
441 This is section [[#sec:foo]].
443 And this is still section \\ref{sec:foo}.
446 will be exported to LaTeX as:
448 \\subsection{Some section}
450 This is section \\ref{sec:foo}.
451 And this is still section \\ref{sec:foo}.
453 Note, however, that setting this variable introduces a limitation
454 on the possible values for CUSTOM_ID. When this variable is
455 non-nil and a headline defines a CUSTOM_ID value, Org simply
456 passes this value to \\label unchanged. You are responsible for
457 ensuring that the value is a valid LaTeX \\label key, and that no
458 other \\label commands with the same key appear elsewhere in your
459 document. (Keys may contain letters, numbers, and the following
460 punctuation: '_' '.' '-' ':'.) There are no such limitations on
461 CUSTOM_ID when this variable is nil.
463 For headlines that do not define the CUSTOM_ID property, Org will
464 continue to use its default labeling scheme to generate labels
465 and resolve links into section references."
466 :group
'org-export-latex
469 :package-version
'(Org .
"8.3"))
473 (defcustom org-latex-footnote-separator
"\\textsuperscript{,}\\,"
474 "Text used to separate footnotes."
475 :group
'org-export-latex
481 (defcustom org-latex-active-timestamp-format
"\\textit{%s}"
482 "A printf format string to be applied to active timestamps."
483 :group
'org-export-latex
486 (defcustom org-latex-inactive-timestamp-format
"\\textit{%s}"
487 "A printf format string to be applied to inactive timestamps."
488 :group
'org-export-latex
491 (defcustom org-latex-diary-timestamp-format
"\\textit{%s}"
492 "A printf format string to be applied to diary timestamps."
493 :group
'org-export-latex
499 (defcustom org-latex-image-default-option
""
500 "Default option for images."
501 :group
'org-export-latex
503 :package-version
'(Org .
"8.0")
506 (defcustom org-latex-image-default-width
".9\\linewidth"
507 "Default width for images.
508 This value will not be used if a height is provided."
509 :group
'org-export-latex
511 :package-version
'(Org .
"8.0")
514 (defcustom org-latex-image-default-height
""
515 "Default height for images.
516 This value will not be used if a width is provided, or if the
517 image is wrapped within a \"figure\" or \"wrapfigure\"
519 :group
'org-export-latex
521 :package-version
'(Org .
"8.0")
524 (defcustom org-latex-default-figure-position
"htb"
525 "Default position for latex figures."
526 :group
'org-export-latex
529 (defcustom org-latex-inline-image-rules
530 '(("file" .
"\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
531 "Rules characterizing image files that can be inlined into LaTeX.
533 A rule consists in an association whose key is the type of link
534 to consider, and value is a regexp that will be matched against
537 Note that, by default, the image extension *actually* allowed
538 depend on the way the LaTeX file is processed. When used with
539 pdflatex, pdf, jpg and png images are OK. When processing
540 through dvi to Postscript, only ps and eps are allowed. The
541 default we use here encompasses both."
542 :group
'org-export-latex
544 :package-version
'(Org .
"8.0")
545 :type
'(alist :key-type
(string :tag
"Type")
546 :value-type
(regexp :tag
"Path")))
548 (defcustom org-latex-link-with-unknown-path-format
"\\texttt{%s}"
549 "Format string for links with unknown path type."
550 :group
'org-export-latex
556 (defcustom org-latex-default-table-environment
"tabular"
557 "Default environment used to build tables."
558 :group
'org-export-latex
560 :package-version
'(Org .
"8.0")
563 (defcustom org-latex-default-table-mode
'table
564 "Default mode for tables.
566 Value can be a symbol among:
568 `table' Regular LaTeX table.
570 `math' In this mode, every cell is considered as being in math
571 mode and the complete table will be wrapped within a math
572 environment. It is particularly useful to write matrices.
574 `inline-math' This mode is almost the same as `math', but the
575 math environment will be inlined.
577 `verbatim' The table is exported as it appears in the Org
578 buffer, within a verbatim environment.
580 This value can be overridden locally with, i.e. \":mode math\" in
583 When modifying this variable, it may be useful to change
584 `org-latex-default-table-environment' accordingly."
585 :group
'org-export-latex
587 :package-version
'(Org .
"8.0")
588 :type
'(choice (const :tag
"Table" table
)
589 (const :tag
"Matrix" math
)
590 (const :tag
"Inline matrix" inline-math
)
591 (const :tag
"Verbatim" verbatim
))
592 :safe
(lambda (s) (memq s
'(table math inline-math verbatim
))))
594 (defcustom org-latex-tables-centered t
595 "When non-nil, tables are exported in a center environment."
596 :group
'org-export-latex
600 (defcustom org-latex-tables-booktabs nil
601 "When non-nil, display tables in a formal \"booktabs\" style.
602 This option assumes that the \"booktabs\" package is properly
603 loaded in the header of the document. This value can be ignored
604 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
606 :group
'org-export-latex
608 :package-version
'(Org .
"8.0")
612 (defcustom org-latex-table-caption-above t
613 "When non-nil, place caption string at the beginning of the table.
614 Otherwise, place it near the end."
615 :group
'org-export-latex
619 (defcustom org-latex-table-scientific-notation
"%s\\,(%s)"
620 "Format string to display numbers in scientific notation.
621 The format should have \"%s\" twice, for mantissa and exponent
622 \(i.e., \"%s\\\\times10^{%s}\").
624 When nil, no transformation is made."
625 :group
'org-export-latex
627 :package-version
'(Org .
"8.0")
629 (string :tag
"Format string")
630 (const :tag
"No formatting" nil
)))
634 (defcustom org-latex-text-markup-alist
'((bold .
"\\textbf{%s}")
636 (italic .
"\\emph{%s}")
637 (strike-through .
"\\sout{%s}")
638 (underline .
"\\uline{%s}")
639 (verbatim . protectedtexttt
))
640 "Alist of LaTeX expressions to convert text markup.
642 The key must be a symbol among `bold', `code', `italic',
643 `strike-through', `underline' and `verbatim'. The value is
644 a formatting string to wrap fontified text with.
646 Value can also be set to the following symbols: `verb' and
647 `protectedtexttt'. For the former, Org will use \"\\verb\" to
648 create a format string and select a delimiter character that
649 isn't in the string. For the latter, Org will use \"\\texttt\"
650 to typeset and try to protect special characters.
652 If no association can be found for a given markup, text will be
654 :group
'org-export-latex
656 :options
'(bold code italic strike-through underline verbatim
))
661 (defcustom org-latex-format-drawer-function
662 (lambda (name contents
) contents
)
663 "Function called to format a drawer in LaTeX code.
665 The function must accept two parameters:
666 NAME the drawer name, like \"LOGBOOK\"
667 CONTENTS the contents of the drawer.
669 The function should return the string to be exported.
671 The default function simply returns the value of CONTENTS."
672 :group
'org-export-latex
674 :package-version
'(Org .
"8.3")
680 (defcustom org-latex-format-inlinetask-function
681 'org-latex-format-inlinetask-default-function
682 "Function called to format an inlinetask in LaTeX code.
684 The function must accept seven parameters:
685 TODO the todo keyword (string or nil)
686 TODO-TYPE the todo type (symbol: `todo', `done', nil)
687 PRIORITY the inlinetask priority (integer or nil)
688 NAME the inlinetask name (string)
689 TAGS the inlinetask tags (list of strings or nil)
690 CONTENTS the contents of the inlinetask (string or nil)
691 INFO the export options (plist)
693 The function should return the string to be exported."
694 :group
'org-export-latex
697 :package-version
'(Org .
"8.3"))
702 (defcustom org-latex-listings nil
703 "Non-nil means export source code using the listings package.
705 This package will fontify source code, possibly even with color.
706 If you want to use this, you also need to make LaTeX use the
707 listings package, and if you want to have color, the color
708 package. Just add these to `org-latex-packages-alist', for
709 example using customize, or with something like:
712 \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
713 \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
717 \(setq org-latex-listings 'minted)
719 causes source code to be exported using the minted package as
720 opposed to listings. If you want to use minted, you need to add
721 the minted package to `org-latex-packages-alist', for example
722 using customize, or with
725 \(add-to-list 'org-latex-packages-alist '(\"\" \"minted\"))
727 In addition, it is necessary to install pygments
728 \(http://pygments.org), and to configure the variable
729 `org-latex-pdf-process' so that the -shell-escape option is
732 The minted choice has possible repercussions on the preview of
733 latex fragments (see `org-preview-latex-fragment'). If you run
734 into previewing problems, please consult
736 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
737 :group
'org-export-latex
739 (const :tag
"Use listings" t
)
740 (const :tag
"Use minted" minted
)
741 (const :tag
"Export verbatim" nil
))
742 :safe
(lambda (s) (memq s
'(t nil minted
))))
744 (defcustom org-latex-listings-langs
745 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
748 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
749 (html "HTML") (xml "XML")
750 (tex "TeX") (latex "[LaTeX]TeX")
751 (shell-script "bash")
753 (ocaml "Caml") (caml "Caml")
754 (sql "SQL") (sqlite "sql")
756 "Alist mapping languages to their listing language counterpart.
757 The key is a symbol, the major mode symbol without the \"-mode\".
758 The value is the string that should be inserted as the language
759 parameter for the listings package. If the mode name and the
760 listings name are the same, the language does not need an entry
761 in this list - but it does not hurt if it is present."
762 :group
'org-export-latex
764 :package-version
'(Org .
"8.3")
767 (symbol :tag
"Major mode ")
768 (string :tag
"Listings language"))))
770 (defcustom org-latex-listings-options nil
771 "Association list of options for the latex listings package.
773 These options are supplied as a comma-separated list to the
774 \\lstset command. Each element of the association list should be
775 a list containing two strings: the name of the option, and the
778 \(setq org-latex-listings-options
779 '((\"basicstyle\" \"\\\\small\")
780 \(\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
782 will typeset the code in a small size font with underlined, bold
785 Note that the same options will be applied to blocks of all
786 languages. If you need block-specific options, you may use the
789 #+ATTR_LATEX: :options key1=value1,key2=value2
793 :group
'org-export-latex
796 (string :tag
"Listings option name ")
797 (string :tag
"Listings option value"))))
799 (defcustom org-latex-minted-langs
800 '((emacs-lisp "common-lisp")
803 (shell-script "bash")
805 "Alist mapping languages to their minted language counterpart.
806 The key is a symbol, the major mode symbol without the \"-mode\".
807 The value is the string that should be inserted as the language
808 parameter for the minted package. If the mode name and the
809 listings name are the same, the language does not need an entry
810 in this list - but it does not hurt if it is present.
812 Note that minted uses all lower case for language identifiers,
813 and that the full list of language identifiers can be obtained
816 pygmentize -L lexers"
817 :group
'org-export-latex
820 (symbol :tag
"Major mode ")
821 (string :tag
"Minted language"))))
823 (defcustom org-latex-minted-options nil
824 "Association list of options for the latex minted package.
826 These options are supplied within square brackets in
827 \\begin{minted} environments. Each element of the alist should
828 be a list containing two strings: the name of the option, and the
831 \(setq org-latex-minted-options
832 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
834 will result in src blocks being exported with
836 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
838 as the start of the minted environment. Note that the same
839 options will be applied to blocks of all languages. If you need
840 block-specific options, you may use the following syntax:
842 #+ATTR_LATEX: :options key1=value1,key2=value2
846 :group
'org-export-latex
849 (string :tag
"Minted option name ")
850 (string :tag
"Minted option value"))))
852 (defvar org-latex-custom-lang-environments nil
853 "Alist mapping languages to language-specific LaTeX environments.
855 It is used during export of src blocks by the listings and minted
856 latex packages. For example,
858 \(setq org-latex-custom-lang-environments
859 '\(\(python \"pythoncode\"\)\)\)
861 would have the effect that if org encounters begin_src python
862 during latex export it will output
871 (defcustom org-latex-pdf-process
872 '("pdflatex -interaction nonstopmode -output-directory %o %f"
873 "pdflatex -interaction nonstopmode -output-directory %o %f"
874 "pdflatex -interaction nonstopmode -output-directory %o %f")
875 "Commands to process a LaTeX file to a PDF file.
876 This is a list of strings, each of them will be given to the
877 shell as a command. %f in the command will be replaced by the
878 full file name, %b by the file base name (i.e. without directory
879 and extension parts) and %o by the base directory of the file.
881 The reason why this is a list is that it usually takes several
882 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
883 does not have a clever mechanism to detect which of these
884 commands have to be run to get to a stable result, and it also
885 does not do any error checking.
887 By default, Org uses 3 runs of `pdflatex' to do the processing.
888 If you have texi2dvi on your system and if that does not cause
889 the infamous egrep/locale bug:
891 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
893 then `texi2dvi' is the superior choice as it automates the LaTeX
894 build process by calling the \"correct\" combinations of
895 auxiliary programs. Org does offer `texi2dvi' as one of the
896 customize options. Alternatively, `rubber' and `latexmk' also
897 provide similar functionality. The latter supports `biber' out
900 Alternatively, this may be a Lisp function that does the
901 processing, so you could use this to apply the machinery of
902 AUCTeX or the Emacs LaTeX mode. This function should accept the
903 file name as its single argument."
904 :group
'org-export-pdf
906 (repeat :tag
"Shell command sequence"
907 (string :tag
"Shell command"))
908 (const :tag
"2 runs of pdflatex"
909 ("pdflatex -interaction nonstopmode -output-directory %o %f"
910 "pdflatex -interaction nonstopmode -output-directory %o %f"))
911 (const :tag
"3 runs of pdflatex"
912 ("pdflatex -interaction nonstopmode -output-directory %o %f"
913 "pdflatex -interaction nonstopmode -output-directory %o %f"
914 "pdflatex -interaction nonstopmode -output-directory %o %f"))
915 (const :tag
"pdflatex,bibtex,pdflatex,pdflatex"
916 ("pdflatex -interaction nonstopmode -output-directory %o %f"
918 "pdflatex -interaction nonstopmode -output-directory %o %f"
919 "pdflatex -interaction nonstopmode -output-directory %o %f"))
920 (const :tag
"2 runs of xelatex"
921 ("xelatex -interaction nonstopmode -output-directory %o %f"
922 "xelatex -interaction nonstopmode -output-directory %o %f"))
923 (const :tag
"3 runs of xelatex"
924 ("xelatex -interaction nonstopmode -output-directory %o %f"
925 "xelatex -interaction nonstopmode -output-directory %o %f"
926 "xelatex -interaction nonstopmode -output-directory %o %f"))
927 (const :tag
"xelatex,bibtex,xelatex,xelatex"
928 ("xelatex -interaction nonstopmode -output-directory %o %f"
930 "xelatex -interaction nonstopmode -output-directory %o %f"
931 "xelatex -interaction nonstopmode -output-directory %o %f"))
932 (const :tag
"texi2dvi"
933 ("texi2dvi -p -b -V %f"))
935 ("rubber -d --into %o %f"))
936 (const :tag
"latexmk"
937 ("latexmk -g -pdf %f"))
940 (defcustom org-latex-logfiles-extensions
941 '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
942 "run.xml" "snm" "toc" "vrb" "xdv")
943 "The list of file extensions to consider as LaTeX logfiles.
944 The logfiles will be remove if `org-latex-remove-logfiles' is
946 :group
'org-export-latex
947 :type
'(repeat (string :tag
"Extension")))
949 (defcustom org-latex-remove-logfiles t
950 "Non-nil means remove the logfiles produced by PDF production.
951 By default, logfiles are files with these extensions: .aux, .idx,
952 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
953 logfiles to remove, set `org-latex-logfiles-extensions'."
954 :group
'org-export-latex
957 (defcustom org-latex-known-warnings
958 '(("Reference.*?undefined" .
"[undefined reference]")
959 ("Runaway argument" .
"[runaway argument]")
960 ("Underfull \\hbox" .
"[underfull hbox]")
961 ("Overfull \\hbox" .
"[overfull hbox]")
962 ("Citation.*?undefined" .
"[undefined citation]")
963 ("Undefined control sequence" .
"[undefined control sequence]"))
964 "Alist of regular expressions and associated messages for the user.
965 The regular expressions are used to find possible warnings in the
966 log of a latex-run. These warnings will be reported after
967 calling `org-latex-compile'."
968 :group
'org-export-latex
970 :package-version
'(Org .
"8.3")
973 (string :tag
"Regexp")
974 (string :tag
"Message"))))
978 ;;; Internal Functions
980 (defun org-latex--caption/label-string
(element info
)
981 "Return caption and label LaTeX string for ELEMENT.
983 INFO is a plist holding contextual information. If there's no
984 caption nor label, return the empty string.
986 For non-floats, see `org-latex--wrap-label'."
987 (let* ((label (org-element-property :name element
))
988 (label-str (if (not (org-string-nw-p label
)) ""
989 (format "\\label{%s}"
990 (org-export-solidify-link-text label
))))
991 (main (org-export-get-caption element
))
992 (short (org-export-get-caption element t
))
993 (caption-from-attr-latex (org-export-read-attribute :attr_latex element
:caption
)))
995 ((org-string-nw-p caption-from-attr-latex
)
996 (concat caption-from-attr-latex
"\n"))
997 ((and (not main
) (equal label-str
"")) "")
998 ((not main
) (concat label-str
"\n"))
999 ;; Option caption format with short name.
1000 (short (format "\\caption[%s]{%s%s}\n"
1001 (org-export-data short info
)
1003 (org-export-data main info
)))
1004 ;; Standard caption format.
1005 (t (format "\\caption{%s%s}\n" label-str
(org-export-data main info
))))))
1007 (defun org-latex-guess-inputenc (header)
1008 "Set the coding system in inputenc to what the buffer is.
1010 HEADER is the LaTeX header string. This function only applies
1011 when specified inputenc option is \"AUTO\".
1013 Return the new header, as a string."
1014 (let* ((cs (or (ignore-errors
1015 (latexenc-coding-system-to-inputenc
1016 (or org-export-coding-system buffer-file-coding-system
)))
1019 ;; First translate if that is requested.
1020 (setq cs
(or (cdr (assoc cs org-latex-inputenc-alist
)) cs
))
1021 ;; Then find the \usepackage statement and replace the option.
1022 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1023 cs header t nil
1))))
1025 (defun org-latex-guess-babel-language (header info
)
1026 "Set Babel's language according to LANGUAGE keyword.
1028 HEADER is the LaTeX header string. INFO is the plist used as
1029 a communication channel.
1031 Insertion of guessed language only happens when Babel package has
1032 explicitly been loaded. Then it is added to the rest of
1035 The argument to Babel may be \"AUTO\" which is then replaced with
1036 the language of the document or `org-export-default-language'
1037 unless language in question is already loaded.
1039 Return the new header."
1040 (let ((language-code (plist-get info
:language
)))
1041 ;; If no language is set or Babel package is not loaded, return
1043 (if (or (not (stringp language-code
))
1044 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header
)))
1046 (let ((options (save-match-data
1047 (org-split-string (match-string 1 header
) ",[ \t]*")))
1048 (language (cdr (assoc language-code
1049 org-latex-babel-language-alist
))))
1050 ;; If LANGUAGE is already loaded, return header without AUTO.
1051 ;; Otherwise, replace AUTO with language or append language if
1052 ;; AUTO is not present.
1054 (mapconcat (lambda (option) (if (equal "AUTO" option
) language option
))
1055 (cond ((member language options
) (delete "AUTO" options
))
1056 ((member "AUTO" options
) options
)
1057 (t (append options
(list language
))))
1061 (defun org-latex--find-verb-separator (s)
1062 "Return a character not used in string S.
1063 This is used to choose a separator for constructs like \\verb."
1064 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1065 (loop for c across ll
1066 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
1067 return
(char-to-string c
))))
1069 (defun org-latex--make-option-string (options)
1070 "Return a comma separated string of keywords and values.
1071 OPTIONS is an alist where the key is the options keyword as
1072 a string, and the value a list containing the keyword value, or
1074 (mapconcat (lambda (pair)
1075 (concat (first pair
)
1076 (when (> (length (second pair
)) 0)
1077 (concat "=" (second pair
)))))
1081 (defun org-latex--wrap-label (element output
)
1082 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1083 This function shouldn't be used for floats. See
1084 `org-latex--caption/label-string'."
1085 (let ((label (org-element-property :name element
)))
1086 (if (not (and (org-string-nw-p output
) (org-string-nw-p label
))) output
1087 (concat (format "\\label{%s}\n" (org-export-solidify-link-text label
))
1090 (defun org-latex--text-markup (text markup info
)
1091 "Format TEXT depending on MARKUP text markup.
1092 INFO is a plist used as a communication channel. See
1093 `org-latex-text-markup-alist' for details."
1094 (let ((fmt (cdr (assq markup
(plist-get info
:latex-text-markup-alist
)))))
1096 ;; No format string: Return raw text.
1098 ;; Handle the `verb' special case: Find an appropriate separator
1099 ;; and use "\\verb" command.
1101 (let ((separator (org-latex--find-verb-separator text
)))
1102 (concat "\\verb" separator
1103 (replace-regexp-in-string "\n" " " text
)
1105 ;; Handle the `protectedtexttt' special case: Protect some
1106 ;; special chars and use "\texttt{%s}" format string.
1107 ((eq 'protectedtexttt fmt
)
1109 (trans '(("\\" .
"\\textbackslash{}")
1110 ("~" .
"\\textasciitilde{}")
1111 ("^" .
"\\textasciicircum{}")))
1114 (while (string-match "[\\{}$%&_#~^]" text
)
1115 (setq char
(match-string 0 text
))
1116 (if (> (match-beginning 0) 0)
1117 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
1118 (setq text
(substring text
(1+ (match-beginning 0))))
1119 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
1120 rtn
(concat rtn char
)))
1121 (setq text
(concat rtn text
)
1123 (while (string-match "--" text
)
1124 (setq text
(replace-match "-{}-" t t text
)))
1126 ;; Else use format string.
1127 (t (format fmt text
)))))
1129 (defun org-latex--delayed-footnotes-definitions (element info
)
1130 "Return footnotes definitions in ELEMENT as a string.
1132 INFO is a plist used as a communication channel.
1134 Footnotes definitions are returned within \"\\footnotetxt{}\"
1137 This function is used within constructs that don't support
1138 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1139 \"\\footnotemark\" is used within the construct and the function
1140 just outside of it."
1144 "\\footnotetext[%s]{%s}"
1145 (org-export-get-footnote-number ref info
)
1148 (org-export-get-footnote-definition ref info
) info
))))
1149 ;; Find every footnote reference in ELEMENT.
1151 search-refs
; For byte-compiler.
1155 ;; Return a list of all footnote references never seen
1157 (org-element-map data
'footnote-reference
1159 (when (org-export-footnote-first-reference-p ref info
)
1161 (when (eq (org-element-property :type ref
) 'standard
)
1162 (funcall search-refs
1163 (org-export-get-footnote-definition ref info
)))))
1165 (reverse all-refs
)))))
1166 (funcall search-refs element
))
1169 (defun org-latex--translate (s info
)
1170 "Translate string S according to specified language.
1171 INFO is a plist used as a communication channel."
1172 (org-export-translate s
:latex info
))
1178 (defun org-latex-template (contents info
)
1179 "Return complete document string after LaTeX conversion.
1180 CONTENTS is the transcoded contents string. INFO is a plist
1181 holding export options."
1182 (let ((title (org-export-data (plist-get info
:title
) info
)))
1185 (and (plist-get info
:time-stamp-file
)
1186 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1187 ;; Document class and packages.
1188 (let* ((class (plist-get info
:latex-class
))
1189 (class-options (plist-get info
:latex-class-options
))
1190 (header (nth 1 (assoc class
(plist-get info
:latex-classes
))))
1191 (document-class-string
1192 (and (stringp header
)
1193 (if (not class-options
) header
1194 (replace-regexp-in-string
1195 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1196 class-options header t nil
1)))))
1197 (if (not document-class-string
)
1198 (user-error "Unknown LaTeX class `%s'" class
)
1199 (org-latex-guess-babel-language
1200 (org-latex-guess-inputenc
1201 (org-element-normalize-string
1202 (org-splice-latex-header
1203 document-class-string
1204 org-latex-default-packages-alist
1205 org-latex-packages-alist nil
1206 (concat (org-element-normalize-string
1207 (plist-get info
:latex-header
))
1208 (plist-get info
:latex-header-extra
)))))
1210 ;; Possibly limit depth for headline numbering.
1211 (let ((sec-num (plist-get info
:section-numbers
)))
1212 (when (integerp sec-num
)
1213 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
1215 (let ((author (and (plist-get info
:with-author
)
1216 (let ((auth (plist-get info
:author
)))
1217 (and auth
(org-export-data auth info
)))))
1218 (email (and (plist-get info
:with-email
)
1219 (org-export-data (plist-get info
:email
) info
))))
1220 (cond ((and author email
(not (string= "" email
)))
1221 (format "\\author{%s\\thanks{%s}}\n" author email
))
1222 ((or author email
) (format "\\author{%s}\n" (or author email
)))))
1224 (let ((date (and (plist-get info
:with-date
) (org-export-get-date info
))))
1225 (format "\\date{%s}\n" (org-export-data date info
)))
1227 (format "\\title{%s}\n" title
)
1228 ;; Hyperref options.
1229 (format-spec (plist-get info
:latex-hyperref-template
)
1231 ?k
(or (plist-get info
:keywords
) "")
1232 ?d
(or (plist-get info
:description
)"")
1233 ?c
(if (plist-get info
:with-creator
)
1234 (plist-get info
:creator
)
1237 "\\begin{document}\n\n"
1239 (let ((command (plist-get info
:latex-title-command
)))
1240 (org-element-normalize-string
1241 (cond ((string= "" title
) nil
)
1242 ((not (stringp command
)) nil
)
1243 ((string-match "\\(?:[^%]\\|^\\)%s" command
)
1244 (format command title
))
1246 ;; Table of contents.
1247 (let ((depth (plist-get info
:with-toc
)))
1249 (concat (when (wholenump depth
)
1250 (format "\\setcounter{tocdepth}{%d}\n" depth
))
1251 (plist-get info
:latex-toc-command
))))
1255 (let ((creator-info (plist-get info
:with-creator
)))
1257 ((not creator-info
) "")
1258 ((eq creator-info
'comment
)
1259 (format "%% %s\n" (plist-get info
:creator
)))
1260 (t (concat (plist-get info
:creator
) "\n"))))
1262 "\\end{document}")))
1266 ;;; Transcode Functions
1270 (defun org-latex-bold (bold contents info
)
1271 "Transcode BOLD from Org to LaTeX.
1272 CONTENTS is the text with bold markup. INFO is a plist holding
1273 contextual information."
1274 (org-latex--text-markup contents
'bold info
))
1279 (defun org-latex-center-block (center-block contents info
)
1280 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1281 CONTENTS holds the contents of the center block. INFO is a plist
1282 holding contextual information."
1283 (org-latex--wrap-label
1285 (format "\\begin{center}\n%s\\end{center}" contents
)))
1290 (defun org-latex-clock (clock contents info
)
1291 "Transcode a CLOCK element from Org to LaTeX.
1292 CONTENTS is nil. INFO is a plist holding contextual
1296 (format "\\textbf{%s} " org-clock-string
)
1297 (format (plist-get info
:latex-inactive-timestamp-format
)
1298 (concat (org-translate-time
1299 (org-element-property :raw-value
1300 (org-element-property :value clock
)))
1301 (let ((time (org-element-property :duration clock
)))
1302 (and time
(format " (%s)" time
)))))
1308 (defun org-latex-code (code contents info
)
1309 "Transcode a CODE object from Org to LaTeX.
1310 CONTENTS is nil. INFO is a plist used as a communication
1312 (org-latex--text-markup (org-element-property :value code
) 'code info
))
1317 (defun org-latex-drawer (drawer contents info
)
1318 "Transcode a DRAWER element from Org to LaTeX.
1319 CONTENTS holds the contents of the block. INFO is a plist
1320 holding contextual information."
1321 (let* ((name (org-element-property :drawer-name drawer
))
1322 (output (funcall (plist-get info
:latex-format-drawer-function
)
1324 (org-latex--wrap-label drawer output
)))
1329 (defun org-latex-dynamic-block (dynamic-block contents info
)
1330 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1331 CONTENTS holds the contents of the block. INFO is a plist
1332 holding contextual information. See `org-export-data'."
1333 (org-latex--wrap-label dynamic-block contents
))
1338 (defun org-latex-entity (entity contents info
)
1339 "Transcode an ENTITY object from Org to LaTeX.
1340 CONTENTS are the definition itself. INFO is a plist holding
1341 contextual information."
1342 (org-element-property :latex entity
))
1347 (defun org-latex-example-block (example-block contents info
)
1348 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1349 CONTENTS is nil. INFO is a plist holding contextual
1351 (when (org-string-nw-p (org-element-property :value example-block
))
1352 (org-latex--wrap-label
1354 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1355 (org-export-format-code-default example-block info
)))))
1360 (defun org-latex-export-snippet (export-snippet contents info
)
1361 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1362 CONTENTS is nil. INFO is a plist holding contextual information."
1363 (when (eq (org-export-snippet-backend export-snippet
) 'latex
)
1364 (org-element-property :value export-snippet
)))
1369 (defun org-latex-fixed-width (fixed-width contents info
)
1370 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1371 CONTENTS is nil. INFO is a plist holding contextual information."
1372 (org-latex--wrap-label
1374 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1375 (org-remove-indentation
1376 (org-element-property :value fixed-width
)))))
1379 ;;;; Footnote Reference
1381 (defun org-latex-footnote-reference (footnote-reference contents info
)
1382 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1383 CONTENTS is nil. INFO is a plist holding contextual information."
1385 ;; Insert separator between two footnotes in a row.
1386 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1387 (when (eq (org-element-type prev
) 'footnote-reference
)
1388 (plist-get info
:latex-footnote-separator
)))
1390 ;; Use \footnotemark if the footnote has already been defined.
1391 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1392 (format "\\footnotemark[%s]{}"
1393 (org-export-get-footnote-number footnote-reference info
)))
1394 ;; Use \footnotemark if reference is within another footnote
1395 ;; reference, footnote definition or table cell.
1396 ((loop for parent in
(org-export-get-genealogy footnote-reference
)
1397 thereis
(memq (org-element-type parent
)
1398 '(footnote-reference footnote-definition table-cell
)))
1400 ;; Otherwise, define it with \footnote command.
1402 (let ((def (org-export-get-footnote-definition footnote-reference info
)))
1404 (format "\\footnote{%s}" (org-trim (org-export-data def info
)))
1405 ;; Retrieve all footnote references within the footnote and
1406 ;; add their definition after it, since LaTeX doesn't support
1408 (org-latex--delayed-footnotes-definitions def info
)))))))
1413 (defun org-latex-headline (headline contents info
)
1414 "Transcode a HEADLINE element from Org to LaTeX.
1415 CONTENTS holds the contents of the headline. INFO is a plist
1416 holding contextual information."
1417 (unless (org-element-property :footnote-section-p headline
)
1418 (let* ((class (plist-get info
:latex-class
))
1419 (level (org-export-get-relative-level headline info
))
1420 (numberedp (org-export-numbered-headline-p headline info
))
1421 (class-sectioning (assoc class
(plist-get info
:latex-classes
)))
1422 ;; Section formatting will set two placeholders: one for
1423 ;; the title and the other for the contents.
1425 (let ((sec (if (functionp (nth 2 class-sectioning
))
1426 (funcall (nth 2 class-sectioning
) level numberedp
)
1427 (nth (1+ level
) class-sectioning
))))
1429 ;; No section available for that LEVEL.
1431 ;; Section format directly returned by a function. Add
1432 ;; placeholder for contents.
1433 ((stringp sec
) (concat sec
"\n%s"))
1434 ;; (numbered-section . unnumbered-section)
1435 ((not (consp (cdr sec
)))
1436 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
1437 ;; (numbered-open numbered-close)
1439 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
1440 ;; (num-in num-out no-num-in no-num-out)
1442 (if numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))
1443 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
1444 ;; Create a temporary export back-end that hard-codes
1445 ;; "\underline" within "\section" and alike.
1447 (org-export-create-backend
1450 '((underline .
(lambda (o c i
) (format "\\underline{%s}" c
))))))
1452 (org-export-data-with-backend
1453 (org-element-property :title headline
) section-back-end info
))
1455 (and (plist-get info
:with-todo-keywords
)
1456 (let ((todo (org-element-property :todo-keyword headline
)))
1457 (and todo
(org-export-data todo info
)))))
1458 (todo-type (and todo
(org-element-property :todo-type headline
)))
1459 (tags (and (plist-get info
:with-tags
)
1460 (org-export-get-tags headline info
)))
1461 (priority (and (plist-get info
:with-priority
)
1462 (org-element-property :priority headline
)))
1463 ;; Create the headline text along with a no-tag version.
1464 ;; The latter is required to remove tags from toc.
1465 (full-text (funcall (plist-get info
:latex-format-headline-function
)
1466 todo todo-type priority text tags info
))
1467 ;; Associate \label to the headline for internal links.
1470 (and (plist-get info
:latex-custom-id-labels
)
1471 (org-element-property :CUSTOM_ID headline
))))
1472 (if custom-label
(format "\\label{%s}\n" custom-label
)
1473 (format "\\label{sec-%s}\n"
1476 (org-export-get-headline-number headline info
)
1479 (make-string (org-element-property :pre-blank headline
) 10)))
1480 (if (or (not section-fmt
) (org-export-low-level-p headline info
))
1481 ;; This is a deep sub-tree: export it as a list item. Also
1482 ;; export as items headlines for which no section format has
1484 (let ((low-level-body
1486 ;; If headline is the first sibling, start a list.
1487 (when (org-export-first-sibling-p headline info
)
1488 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
1491 (and full-text
(org-string-match-p "\\`[ \t]*\\[" full-text
)
1497 ;; If headline is not the last sibling simply return
1498 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1500 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1501 (replace-regexp-in-string
1503 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
1505 ;; This is a standard headline. Export it as a section. Add
1506 ;; an alternative heading when possible, and when this is not
1507 ;; identical to the usual heading.
1509 (funcall (plist-get info
:latex-format-headline-function
)
1510 todo todo-type priority
1511 (org-export-data-with-backend
1512 (org-export-get-alt-title headline info
)
1513 section-back-end info
)
1514 (and (eq (plist-get info
:with-tags
) t
) tags
)
1516 (if (and numberedp opt-title
1517 (not (equal opt-title full-text
))
1518 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt
))
1519 (format (replace-match "\\1[%s]" nil nil section-fmt
1)
1520 ;; Replace square brackets with parenthesis
1521 ;; since square brackets are not supported in
1522 ;; optional arguments.
1523 (replace-regexp-in-string
1524 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title
))
1526 (concat headline-label pre-blanks contents
))
1527 ;; Impossible to add an alternative heading. Fallback to
1528 ;; regular sectioning format string.
1529 (format section-fmt full-text
1530 (concat headline-label pre-blanks contents
))))))))
1532 (defun org-latex-format-headline-default-function
1533 (todo todo-type priority text tags info
)
1534 "Default format function for a headline.
1535 See `org-latex-format-headline-function' for details."
1537 (and todo
(format "{\\bfseries\\sffamily %s} " todo
))
1538 (and priority
(format "\\framebox{\\#%c} " priority
))
1541 (format "\\hfill{}\\textsc{%s}"
1542 (mapconcat (lambda (tag) (org-latex-plain-text tag info
))
1546 ;;;; Horizontal Rule
1548 (defun org-latex-horizontal-rule (horizontal-rule contents info
)
1549 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1550 CONTENTS is nil. INFO is a plist holding contextual information."
1551 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule
))
1552 (prev (org-export-get-previous-element horizontal-rule info
)))
1554 ;; Make sure the rule doesn't start at the end of the current
1555 ;; line by separating it with a blank line from previous element.
1557 (let ((prev-blank (org-element-property :post-blank prev
)))
1558 (or (not prev-blank
) (zerop prev-blank
))))
1560 (org-latex--wrap-label
1562 (format "\\rule{%s}{%s}"
1563 (or (plist-get attr
:width
) "\\linewidth")
1564 (or (plist-get attr
:thickness
) "0.5pt"))))))
1567 ;;;; Inline Src Block
1569 (defun org-latex-inline-src-block (inline-src-block contents info
)
1570 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1571 CONTENTS holds the contents of the item. INFO is a plist holding
1572 contextual information."
1573 (let* ((code (org-element-property :value inline-src-block
))
1574 (separator (org-latex--find-verb-separator code
)))
1575 (case (plist-get info
:latex-listings
)
1576 ;; Do not use a special package: transcode it verbatim.
1577 ((nil) (concat "\\verb" separator code separator
))
1578 ;; Use minted package.
1580 (let* ((org-lang (org-element-property :language inline-src-block
))
1581 (mint-lang (or (cadr (assq (intern org-lang
)
1582 (plist-get info
:latex-minted-langs
)))
1583 (downcase org-lang
)))
1584 (options (org-latex--make-option-string
1585 (plist-get info
:latex-minted-options
))))
1586 (concat (format "\\mint%s{%s}"
1587 (if (string= options
"") "" (format "[%s]" options
))
1589 separator code separator
)))
1590 ;; Use listings package.
1592 ;; Maybe translate language's name.
1593 (let* ((org-lang (org-element-property :language inline-src-block
))
1594 (lst-lang (or (cadr (assq (intern org-lang
)
1595 (plist-get info
:latex-listings-langs
)))
1597 (options (org-latex--make-option-string
1598 (append (plist-get info
:latex-listings-options
)
1599 `(("language" ,lst-lang
))))))
1600 (concat (format "\\lstinline[%s]" options
)
1601 separator code separator
))))))
1606 (defun org-latex-inlinetask (inlinetask contents info
)
1607 "Transcode an INLINETASK element from Org to LaTeX.
1608 CONTENTS holds the contents of the block. INFO is a plist
1609 holding contextual information."
1610 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1611 (todo (and (plist-get info
:with-todo-keywords
)
1612 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1613 (and todo
(org-export-data todo info
)))))
1614 (todo-type (org-element-property :todo-type inlinetask
))
1615 (tags (and (plist-get info
:with-tags
)
1616 (org-export-get-tags inlinetask info
)))
1617 (priority (and (plist-get info
:with-priority
)
1618 (org-element-property :priority inlinetask
)))
1620 (let ((label (org-element-property :CUSTOM_ID inlinetask
)))
1621 (and label
(format "\\label{%s}\n" label
)))
1623 (funcall (plist-get info
:latex-format-inlinetask-function
)
1624 todo todo-type priority title tags contents info
)))
1626 (defun org-latex-format-inlinetask-default-function
1627 (todo todo-type priority title tags contents info
)
1628 "Default format function for a inlinetasks.
1629 See `org-latex-format-inlinetask-function' for details."
1631 (concat (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1632 (when priority
(format "\\framebox{\\#%c} " priority
))
1635 (format "\\hfill{}\\textsc{:%s:}"
1637 (lambda (tag) (org-latex-plain-text tag info
))
1639 (concat "\\begin{center}\n"
1641 "\\begin{minipage}[c]{.6\\textwidth}\n"
1643 (and (org-string-nw-p contents
)
1644 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents
))
1652 (defun org-latex-italic (italic contents info
)
1653 "Transcode ITALIC from Org to LaTeX.
1654 CONTENTS is the text with italic markup. INFO is a plist holding
1655 contextual information."
1656 (org-latex--text-markup contents
'italic info
))
1661 (defun org-latex-item (item contents info
)
1662 "Transcode an ITEM element from Org to LaTeX.
1663 CONTENTS holds the contents of the item. INFO is a plist holding
1664 contextual information."
1666 (let ((count (org-element-property :counter item
))
1668 ;; Determine level of current item to determine the
1669 ;; correct LaTeX counter to use (enumi, enumii...).
1670 (let ((parent item
) (level 0))
1671 (while (memq (org-element-type
1672 (setq parent
(org-export-get-parent parent
)))
1674 (when (and (eq (org-element-type parent
) 'plain-list
)
1675 (eq (org-element-property :type parent
)
1681 (format "\\setcounter{enum%s}{%s}\n"
1682 (nth (1- level
) '("i" "ii" "iii" "iv"))
1684 (checkbox (case (org-element-property :checkbox item
)
1685 (on "$\\boxtimes$ ")
1687 (trans "$\\boxminus$ ")))
1688 (tag (let ((tag (org-element-property :tag item
)))
1689 ;; Check-boxes must belong to the tag.
1690 (and tag
(format "[{%s}] "
1692 (org-export-data tag info
)))))))
1697 (checkbox (concat " " checkbox
))
1698 ;; Without a tag or a check-box, if CONTENTS starts with
1699 ;; an opening square bracket, add "\relax" to "\item",
1700 ;; unless the brackets comes from an initial export
1701 ;; snippet (i.e. it is inserted willingly by the user).
1703 (org-string-match-p "\\`[ \t]*\\[" contents
)
1704 (not (let ((e (car (org-element-contents item
))))
1705 (and (eq (org-element-type e
) 'paragraph
)
1706 (let ((o (car (org-element-contents e
))))
1707 (and (eq (org-element-type o
) 'export-snippet
)
1708 (eq (org-export-snippet-backend o
)
1712 (and contents
(org-trim contents
))
1713 ;; If there are footnotes references in tag, be sure to
1714 ;; add their definition at the end of the item. This
1715 ;; workaround is necessary since "\footnote{}" command is
1716 ;; not supported in tags.
1718 (org-latex--delayed-footnotes-definitions
1719 (org-element-property :tag item
) info
)))))
1724 (defun org-latex-keyword (keyword contents info
)
1725 "Transcode a KEYWORD element from Org to LaTeX.
1726 CONTENTS is nil. INFO is a plist holding contextual information."
1727 (let ((key (org-element-property :key keyword
))
1728 (value (org-element-property :value keyword
)))
1730 ((string= key
"LATEX") value
)
1731 ((string= key
"INDEX") (format "\\index{%s}" value
))
1732 ((string= key
"TOC")
1733 (let ((value (downcase value
)))
1735 ((string-match "\\<headlines\\>" value
)
1736 (let ((depth (or (and (string-match "[0-9]+" value
)
1737 (string-to-number (match-string 0 value
)))
1738 (plist-get info
:with-toc
))))
1740 (when (wholenump depth
)
1741 (format "\\setcounter{tocdepth}{%s}\n" depth
))
1742 "\\tableofcontents")))
1743 ((string= "tables" value
) "\\listoftables")
1744 ((string= "listings" value
)
1745 (case (plist-get info
:latex-listings
)
1746 ((nil) "\\listoffigures")
1747 (minted "\\listoflistings")
1748 (otherwise "\\lstlistoflistings")))))))))
1751 ;;;; Latex Environment
1753 (defun org-latex-latex-environment (latex-environment contents info
)
1754 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1755 CONTENTS is nil. INFO is a plist holding contextual information."
1756 (when (plist-get info
:with-latex
)
1757 (let ((label (org-element-property :name latex-environment
))
1758 (value (org-remove-indentation
1759 (org-element-property :value latex-environment
))))
1760 (if (not (org-string-nw-p label
)) value
1761 ;; Environment is labeled: label must be within the environment
1762 ;; (otherwise, a reference pointing to that element will count
1763 ;; the section instead).
1766 (goto-char (point-min))
1769 (format "\\label{%s}\n" (org-export-solidify-link-text label
)))
1770 (buffer-string))))))
1775 (defun org-latex-latex-fragment (latex-fragment contents info
)
1776 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1777 CONTENTS is nil. INFO is a plist holding contextual information."
1778 (let ((value (org-element-property :value latex-fragment
)))
1779 ;; Trim math markers since the fragment is enclosed within
1780 ;; a latex-math-block object anyway.
1781 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value
)
1782 (match-string 2 value
))
1783 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value
)
1784 (match-string 1 value
))
1790 (defun org-latex-line-break (line-break contents info
)
1791 "Transcode a LINE-BREAK object from Org to LaTeX.
1792 CONTENTS is nil. INFO is a plist holding contextual information."
1798 (defun org-latex--inline-image (link info
)
1799 "Return LaTeX code for an inline image.
1800 LINK is the link pointing to the inline image. INFO is a plist
1801 used as a communication channel."
1802 (let* ((parent (org-export-get-parent-element link
))
1803 (path (let ((raw-path (org-element-property :path link
)))
1804 (if (not (file-name-absolute-p raw-path
)) raw-path
1805 (expand-file-name raw-path
))))
1806 (filetype (file-name-extension path
))
1807 (caption (org-latex--caption/label-string parent info
))
1808 ;; Retrieve latex attributes from the element around.
1809 (attr (org-export-read-attribute :attr_latex parent
))
1810 (float (let ((float (plist-get attr
:float
)))
1811 (cond ((and (not float
) (plist-member attr
:float
)) nil
)
1812 ((string= float
"wrap") 'wrap
)
1813 ((string= float
"sideways") 'sideways
)
1814 ((string= float
"multicolumn") 'multicolumn
)
1816 (org-element-property :caption parent
)
1817 (org-string-nw-p (plist-get attr
:caption
)))
1820 (let ((place (plist-get attr
:placement
)))
1822 (place (format "%s" place
))
1823 ((eq float
'wrap
) "{l}{0.5\\textwidth}")
1825 (format "[%s]" (plist-get info
:latex-default-figure-position
)))
1827 (comment-include (if (plist-get attr
:comment-include
) "%" ""))
1828 ;; It is possible to specify width and height in the
1829 ;; ATTR_LATEX line, and also via default variables.
1830 (width (cond ((plist-get attr
:width
))
1831 ((plist-get attr
:height
) "")
1832 ((eq float
'wrap
) "0.48\\textwidth")
1833 (t (plist-get info
:latex-image-default-width
))))
1834 (height (cond ((plist-get attr
:height
))
1835 ((or (plist-get attr
:width
)
1836 (memq float
'(figure wrap
))) "")
1837 (t (plist-get info
:latex-image-default-height
))))
1838 (options (let ((opt (or (plist-get attr
:options
)
1839 (plist-get info
:latex-image-default-option
))))
1840 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt
)) opt
1841 (match-string 1 opt
))))
1843 (if (member filetype
'("tikz" "pgf"))
1845 ;; - use \input to read in image file.
1846 ;; - if options are present, wrap in a tikzpicture environment.
1847 ;; - if width or height are present, use \resizebox to change
1850 (setq image-code
(format "\\input{%s}" path
))
1851 (when (org-string-nw-p options
)
1853 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1856 (when (or (org-string-nw-p width
) (org-string-nw-p height
))
1857 (setq image-code
(format "\\resizebox{%s}{%s}{%s}"
1858 (if (org-string-nw-p width
) width
"!")
1859 (if (org-string-nw-p height
) height
"!")
1861 ;; For other images:
1862 ;; - add width and height to options.
1863 ;; - include the image with \includegraphics.
1864 (when (org-string-nw-p width
)
1865 (setq options
(concat options
",width=" width
)))
1866 (when (org-string-nw-p height
)
1867 (setq options
(concat options
",height=" height
)))
1868 (let ((search-option (org-element-property :search-option link
)))
1869 (when (and search-option
1870 (equal filetype
"pdf")
1871 (org-string-match-p "\\`[0-9]+\\'" search-option
)
1872 (not (org-string-match-p "page=" options
)))
1873 (setq options
(concat options
",page=" search-option
))))
1875 (format "\\includegraphics%s{%s}"
1876 (cond ((not (org-string-nw-p options
)) "")
1877 ((= (aref options
0) ?
,)
1878 (format "[%s]"(substring options
1)))
1879 (t (format "[%s]" options
)))
1881 (when (equal filetype
"svg")
1882 (setq image-code
(replace-regexp-in-string "^\\\\includegraphics"
1886 (setq image-code
(replace-regexp-in-string "\\.svg}"
1890 ;; Return proper string, depending on FLOAT.
1892 (wrap (format "\\begin{wrapfigure}%s
1895 %s\\end{wrapfigure}" placement comment-include image-code caption
))
1896 (sideways (format "\\begin{sidewaysfigure}
1899 %s\\end{sidewaysfigure}" comment-include image-code caption
))
1900 (multicolumn (format "\\begin{figure*}%s
1903 %s\\end{figure*}" placement comment-include image-code caption
))
1904 (figure (format "\\begin{figure}%s
1907 %s\\end{figure}" placement comment-include image-code caption
))
1908 (otherwise image-code
))))
1910 (defun org-latex-link (link desc info
)
1911 "Transcode a LINK object from Org to LaTeX.
1913 DESC is the description part of the link, or the empty string.
1914 INFO is a plist holding contextual information. See
1916 (let* ((type (org-element-property :type link
))
1917 (raw-path (replace-regexp-in-string
1918 "%" "\\%" (org-element-property :path link
) nil t
))
1919 ;; Ensure DESC really exists, or set it to nil.
1920 (desc (and (not (string= desc
"")) desc
))
1921 (imagep (org-export-inline-image-p
1922 link
(plist-get info
:latex-inline-image-rules
)))
1924 ((member type
'("http" "https" "ftp" "mailto"))
1925 (concat type
":" raw-path
))
1926 ((and (string= type
"file") (file-name-absolute-p raw-path
))
1927 (concat "file:" raw-path
))
1932 (imagep (org-latex--inline-image link info
))
1933 ;; Radio link: Transcode target's contents and use them as link's
1935 ((string= type
"radio")
1936 (let ((destination (org-export-resolve-radio-link link info
)))
1938 (format "\\hyperref[%s]{%s}"
1939 (org-export-solidify-link-text
1940 (org-element-property :value destination
))
1942 ;; Links pointing to a headline: Find destination and build
1943 ;; appropriate referencing command.
1944 ((member type
'("custom-id" "fuzzy" "id"))
1945 (let ((destination (if (string= type
"fuzzy")
1946 (org-export-resolve-fuzzy-link link info
)
1947 (org-export-resolve-id-link link info
))))
1948 (case (org-element-type destination
)
1949 ;; Id link points to an external file.
1951 (if desc
(format "\\href{%s}{%s}" destination desc
)
1952 (format "\\url{%s}" destination
)))
1953 ;; Fuzzy link points nowhere.
1955 (format (plist-get info
:latex-link-with-unknown-path-format
)
1958 (org-element-property :raw-link link
) info
))))
1959 ;; LINK points to a headline. If headlines are numbered
1960 ;; and the link has no description, display headline's
1961 ;; number. Otherwise, display description or headline's
1964 (let* ((custom-label
1965 (and (plist-get info
:latex-custom-id-labels
)
1966 (org-element-property :CUSTOM_ID destination
)))
1973 (org-export-get-headline-number destination info
)
1975 (if (and (plist-get info
:section-numbers
) (not desc
))
1976 (format "\\ref{%s}" label
)
1977 (format "\\hyperref[%s]{%s}" label
1980 (org-element-property :title destination
) info
))))))
1981 ;; Fuzzy link points to a target. Do as above.
1983 (let ((path (org-export-solidify-link-text path
)))
1984 (if (not desc
) (format "\\ref{%s}" path
)
1985 (format "\\hyperref[%s]{%s}" path desc
)))))))
1986 ;; Coderef: replace link with the reference name or the
1987 ;; equivalent line number.
1988 ((string= type
"coderef")
1989 (format (org-export-get-coderef-format path desc
)
1990 (org-export-resolve-coderef path info
)))
1991 ;; Link type is handled by a special function.
1992 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
1993 (funcall protocol
(org-link-unescape path
) desc
'latex
))
1994 ;; External link with a description part.
1995 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
1996 ;; External link without a description part.
1997 (path (format "\\url{%s}" path
))
1998 ;; No path, only description. Try to do something useful.
1999 (t (format (plist-get info
:latex-link-with-unknown-path-format
) desc
)))))
2004 (defun org-latex-node-property (node-property contents info
)
2005 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2006 CONTENTS is nil. INFO is a plist holding contextual
2009 (org-element-property :key node-property
)
2010 (let ((value (org-element-property :value node-property
)))
2011 (if value
(concat " " value
) ""))))
2016 (defun org-latex-paragraph (paragraph contents info
)
2017 "Transcode a PARAGRAPH element from Org to LaTeX.
2018 CONTENTS is the contents of the paragraph, as a string. INFO is
2019 the plist used as a communication channel."
2025 (defun org-latex-plain-list (plain-list contents info
)
2026 "Transcode a PLAIN-LIST element from Org to LaTeX.
2027 CONTENTS is the contents of the list. INFO is a plist holding
2028 contextual information."
2029 (let* ((type (org-element-property :type plain-list
))
2030 (attr (org-export-read-attribute :attr_latex plain-list
))
2031 (latex-type (let ((env (plist-get attr
:environment
)))
2032 (cond (env (format "%s" env
))
2033 ((eq type
'ordered
) "enumerate")
2034 ((eq type
'descriptive
) "description")
2036 (org-latex--wrap-label
2038 (format "\\begin{%s}%s\n%s\\end{%s}"
2040 (or (plist-get attr
:options
) "")
2047 (defun org-latex-plain-text (text info
)
2048 "Transcode a TEXT string from Org to LaTeX.
2049 TEXT is the string to transcode. INFO is a plist holding
2050 contextual information."
2051 (let* ((specialp (plist-get info
:with-special-strings
))
2053 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2054 (let ((case-fold-search nil
))
2055 (replace-regexp-in-string
2056 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2057 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2058 ;; However, if special strings are used, be careful not
2059 ;; to protect "\" in "\-" constructs.
2060 (replace-regexp-in-string
2061 (concat "[%$#&{}_~^]\\|\\\\" (and specialp
"\\(?:[^-]\\|$\\)"))
2064 (?
\\ "$\\\\backslash$")
2065 (?~
"\\\\textasciitilde{}")
2069 ;; Activate smart quotes. Be sure to provide original TEXT string
2070 ;; since OUTPUT may have been modified.
2071 (when (plist-get info
:with-smart-quotes
)
2072 (setq output
(org-export-activate-smart-quotes output
:latex info text
)))
2073 ;; Convert special strings.
2075 (setq output
(replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output
)))
2076 ;; Handle break preservation if required.
2077 (when (plist-get info
:preserve-breaks
)
2078 (setq output
(replace-regexp-in-string
2079 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t
)))
2086 (defun org-latex-planning (planning contents info
)
2087 "Transcode a PLANNING element from Org to LaTeX.
2088 CONTENTS is nil. INFO is a plist holding contextual
2096 (let ((closed (org-element-property :closed planning
)))
2099 (format "\\textbf{%s} " org-closed-string
)
2100 (format (plist-get info
:latex-inactive-timestamp-format
)
2102 (org-element-property :raw-value closed
))))))
2103 (let ((deadline (org-element-property :deadline planning
)))
2106 (format "\\textbf{%s} " org-deadline-string
)
2107 (format (plist-get info
:latex-active-timestamp-format
)
2109 (org-element-property :raw-value deadline
))))))
2110 (let ((scheduled (org-element-property :scheduled planning
)))
2113 (format "\\textbf{%s} " org-scheduled-string
)
2114 (format (plist-get info
:latex-active-timestamp-format
)
2116 (org-element-property :raw-value scheduled
))))))))
2121 ;;;; Property Drawer
2123 (defun org-latex-property-drawer (property-drawer contents info
)
2124 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2125 CONTENTS holds the contents of the drawer. INFO is a plist
2126 holding contextual information."
2127 (and (org-string-nw-p contents
)
2128 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents
)))
2131 ;;;; Pseudo Object: LaTeX Math Block
2133 (defun org-latex--wrap-latex-math-block (data info
)
2134 "Merge contiguous math objects in a pseudo-object container.
2135 DATA is a parse tree or a secondary string. INFO is a plist
2136 containing export options. Modify DATA by side-effect and return it."
2137 (let ((valid-object-p
2139 ;; Non-nil when OBJ can be added to the latex math block.
2141 (case (org-element-type obj
)
2142 (entity (org-element-property :latex-math-p obj
))
2144 (let ((value (org-element-property :value obj
)))
2145 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value
)
2146 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value
))))
2147 ((subscript superscript
) t
))))))
2148 (org-element-map data
'(entity latex-fragment subscript superscript
)
2150 ;; Skip objects already wrapped.
2151 (when (and (not (eq (org-element-type
2152 (org-element-property :parent object
))
2154 (funcall valid-object-p object
))
2155 (let ((math-block (list 'latex-math-block nil
))
2156 (next-elements (org-export-get-next-element object info t
))
2158 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2159 (org-element-insert-before math-block object
)
2160 (org-element-extract-element object
)
2161 (org-element-adopt-elements math-block object
)
2162 (when (zerop (or (org-element-property :post-blank object
) 0))
2163 ;; MATH-BLOCK swallows consecutive math objects.
2165 (dolist (next next-elements
)
2166 (if (not (funcall valid-object-p next
)) (throw 'exit nil
)
2167 (org-element-extract-element next
)
2168 (org-element-adopt-elements math-block next
)
2169 ;; Eschew the case: \beta$x$ -> \(\betax\).
2170 (unless (memq (org-element-type next
)
2171 '(subscript superscript
))
2172 (org-element-put-property last
:post-blank
1))
2174 (when (> (or (org-element-property :post-blank next
) 0) 0)
2175 (throw 'exit nil
))))))
2176 (org-element-put-property
2177 math-block
:post-blank
(org-element-property :post-blank last
)))))
2178 info nil
'(subscript superscript latex-math-block
) t
)
2179 ;; Return updated DATA.
2182 (defun org-latex-math-block-tree-filter (tree backend info
)
2183 (org-latex--wrap-latex-math-block tree info
))
2185 (defun org-latex-math-block-options-filter (info backend
)
2186 (dolist (prop '(:author
:date
:title
) info
)
2187 (plist-put info prop
2188 (org-latex--wrap-latex-math-block (plist-get info prop
) info
))))
2190 (defun org-latex-math-block (math-block contents info
)
2191 "Transcode a MATH-BLOCK object from Org to LaTeX.
2192 CONTENTS is a string. INFO is a plist used as a communication
2194 (when (org-string-nw-p contents
)
2195 (format "\\(%s\\)" (org-trim contents
))))
2199 (defun org-latex-quote-block (quote-block contents info
)
2200 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2201 CONTENTS holds the contents of the block. INFO is a plist
2202 holding contextual information."
2203 (org-latex--wrap-label
2205 (format "\\begin{quote}\n%s\\end{quote}" contents
)))
2210 (defun org-latex-radio-target (radio-target text info
)
2211 "Transcode a RADIO-TARGET object from Org to LaTeX.
2212 TEXT is the text of the target. INFO is a plist holding
2213 contextual information."
2214 (format "\\label{%s}%s"
2215 (org-export-solidify-link-text
2216 (org-element-property :value radio-target
))
2222 (defun org-latex-section (section contents info
)
2223 "Transcode a SECTION element from Org to LaTeX.
2224 CONTENTS holds the contents of the section. INFO is a plist
2225 holding contextual information."
2231 (defun org-latex-special-block (special-block contents info
)
2232 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2233 CONTENTS holds the contents of the block. INFO is a plist
2234 holding contextual information."
2235 (if (org-export-raw-special-block-p special-block info
)
2236 (org-remove-indentation (org-element-property :raw-value special-block
))
2237 (let ((type (downcase (org-element-property :type special-block
)))
2238 (opt (org-export-read-attribute :attr_latex special-block
:options
)))
2239 (concat (format "\\begin{%s}%s\n" type
(or opt
""))
2240 ;; Insert any label or caption within the block
2241 ;; (otherwise, a reference pointing to that element will
2242 ;; count the section instead).
2243 (org-latex--caption/label-string special-block info
)
2245 (format "\\end{%s}" type
)))))
2250 (defun org-latex-src-block (src-block contents info
)
2251 "Transcode a SRC-BLOCK element from Org to LaTeX.
2252 CONTENTS holds the contents of the item. INFO is a plist holding
2253 contextual information."
2254 (when (org-string-nw-p (org-element-property :value src-block
))
2255 (let* ((lang (org-element-property :language src-block
))
2256 (caption (org-element-property :caption src-block
))
2257 (label (org-element-property :name src-block
))
2258 (custom-env (and lang
2259 (cadr (assq (intern lang
)
2260 org-latex-custom-lang-environments
))))
2261 (num-start (case (org-element-property :number-lines src-block
)
2262 (continued (org-export-get-loc src-block info
))
2264 (retain-labels (org-element-property :retain-labels src-block
))
2265 (attributes (org-export-read-attribute :attr_latex src-block
))
2266 (float (plist-get attributes
:float
))
2267 (listings (plist-get info
:latex-listings
)))
2269 ;; Case 1. No source fontification.
2271 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2273 (cond ((and (not float
) (plist-member attributes
:float
)) "%s")
2274 ((string= "multicolumn" float
)
2275 (format "\\begin{figure*}[%s]\n%%s%s\n\\end{figure*}"
2276 (plist-get info
:latex-default-figure-position
)
2279 (format "\\begin{figure}[H]\n%%s%s\n\\end{figure}"
2284 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2285 (org-export-format-code-default src-block info
))))))
2286 ;; Case 2. Custom environment.
2287 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2289 (org-export-format-code-default src-block info
)
2291 ;; Case 3. Use minted package.
2292 ((eq listings
'minted
)
2293 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2295 (cond ((and (not float
) (plist-member attributes
:float
)) "%s")
2296 ((string= "multicolumn" float
)
2297 (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
2300 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2303 (options (plist-get info
:latex-minted-options
))
2306 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2309 (org-latex--make-option-string
2310 (if (or (not num-start
) (assoc "linenos" options
))
2314 ("firstnumber" ,(number-to-string (1+ num-start
))))
2316 (let ((local-options (plist-get attributes
:options
)))
2317 (and local-options
(concat "," local-options
))))
2319 (or (cadr (assq (intern lang
)
2320 (plist-get info
:latex-minted-langs
)))
2323 (let* ((code-info (org-export-unravel-code src-block
))
2327 (org-split-string (car code-info
)
2329 (org-export-format-code
2331 (lambda (loc num ref
)
2335 ;; Ensure references are flushed to the right,
2336 ;; separated with 6 spaces from the widest line
2338 (concat (make-string (+ (- max-width
(length loc
)) 6)
2340 (format "(%s)" ref
)))))
2341 nil
(and retain-labels
(cdr code-info
)))))))
2343 (format float-env body
)))
2344 ;; Case 4. Use listings package.
2347 (or (cadr (assq (intern lang
)
2348 (plist-get info
:latex-listings-langs
)))
2352 (let ((main (org-export-get-caption src-block
))
2353 (secondary (org-export-get-caption src-block t
)))
2355 (format "{%s}" (org-export-data main info
))
2357 (org-export-data secondary info
)
2358 (org-export-data main info
))))))
2359 (lst-opt (plist-get info
:latex-listings-options
)))
2365 (org-latex--make-option-string
2369 ((and (not float
) (plist-member attributes
:float
)) nil
)
2370 ((string= "multicolumn" float
) '(("float" "*")))
2371 ((and float
(not (assoc "float" lst-opt
)))
2372 `(("float" ,(plist-get info
:latex-default-figure-position
)))))
2373 `(("language" ,lst-lang
))
2374 (if label
`(("label" ,label
)) '(("label" " ")))
2375 (if caption-str
`(("caption" ,caption-str
)) '(("caption" " ")))
2376 (cond ((assoc "numbers" lst-opt
) nil
)
2377 ((not num-start
) '(("numbers" "none")))
2378 ((zerop num-start
) '(("numbers" "left")))
2379 (t `(("numbers" "left")
2381 ,(number-to-string (1+ num-start
))))))))
2382 (let ((local-options (plist-get attributes
:options
)))
2383 (and local-options
(concat "," local-options
)))))
2386 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2387 (let* ((code-info (org-export-unravel-code src-block
))
2391 (org-split-string (car code-info
) "\n")))))
2392 (org-export-format-code
2394 (lambda (loc num ref
)
2398 ;; Ensure references are flushed to the right,
2399 ;; separated with 6 spaces from the widest line of
2401 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2402 (format "(%s)" ref
)))))
2403 nil
(and retain-labels
(cdr code-info
))))))))))))
2406 ;;;; Statistics Cookie
2408 (defun org-latex-statistics-cookie (statistics-cookie contents info
)
2409 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2410 CONTENTS is nil. INFO is a plist holding contextual information."
2411 (replace-regexp-in-string
2412 "%" "\\%" (org-element-property :value statistics-cookie
) nil t
))
2417 (defun org-latex-strike-through (strike-through contents info
)
2418 "Transcode STRIKE-THROUGH from Org to LaTeX.
2419 CONTENTS is the text with strike-through markup. INFO is a plist
2420 holding contextual information."
2421 (org-latex--text-markup contents
'strike-through info
))
2426 (defun org-latex--script-size (object info
)
2427 "Transcode a subscript or superscript object.
2428 OBJECT is an Org object. INFO is a plist used as a communication
2430 (let ((type (org-element-type object
))
2432 (org-element-map (org-element-contents object
)
2433 (cons 'plain-text org-element-all-objects
)
2435 (case (org-element-type obj
)
2436 ((entity latex-fragment
)
2437 (let ((data (org-trim (org-export-data obj info
))))
2439 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2443 (match-string 1 data
)
2444 (let ((blank (org-element-property :post-blank obj
)))
2445 (and blank
(> blank
0) "\\ "))))))
2448 (format "%s\\text{%s}" output
(org-export-data obj info
))))
2452 (org-export-data obj info
)
2453 (let ((blank (org-element-property :post-blank obj
)))
2454 (and blank
(> blank
0) "\\ ")))))))
2455 info nil org-element-recursive-objects
)
2456 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2458 (concat (if (eq (org-element-type object
) 'subscript
) "_" "^")
2459 (and (> (length output
) 1) "{")
2461 (and (> (length output
) 1) "}"))))
2463 (defun org-latex-subscript (subscript contents info
)
2464 "Transcode a SUBSCRIPT object from Org to LaTeX.
2465 CONTENTS is the contents of the object. INFO is a plist holding
2466 contextual information."
2467 (org-latex--script-size subscript info
))
2472 (defun org-latex-superscript (superscript contents info
)
2473 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2474 CONTENTS is the contents of the object. INFO is a plist holding
2475 contextual information."
2476 (org-latex--script-size superscript info
))
2481 ;; `org-latex-table' is the entry point for table transcoding. It
2482 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2483 ;; delegates the job to either `org-latex--table.el-table',
2484 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2485 ;; depending of the type of the table and the mode requested.
2487 ;; `org-latex--align-string' is a subroutine used to build alignment
2488 ;; string for Org tables.
2490 (defun org-latex-table (table contents info
)
2491 "Transcode a TABLE element from Org to LaTeX.
2492 CONTENTS is the contents of the table. INFO is a plist holding
2493 contextual information."
2494 (if (eq (org-element-property :type table
) 'table.el
)
2495 ;; "table.el" table. Convert it using appropriate tools.
2496 (org-latex--table.el-table table info
)
2497 (let ((type (or (org-export-read-attribute :attr_latex table
:mode
)
2498 (plist-get info
:latex-default-table-mode
))))
2500 ;; Case 1: Verbatim table.
2501 ((string= type
"verbatim")
2502 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2503 ;; Re-create table, without affiliated keywords.
2504 (org-trim (org-element-interpret-data
2505 `(table nil
,@(org-element-contents table
))
2506 org-latex-pseudo-objects
))))
2508 ((or (string= type
"math") (string= type
"inline-math"))
2509 (org-latex--math-table table info
))
2510 ;; Case 3: Standard table.
2511 (t (concat (org-latex--org-table table contents info
)
2512 ;; When there are footnote references within the
2513 ;; table, insert their definition just after it.
2514 (org-latex--delayed-footnotes-definitions table info
)))))))
2516 (defun org-latex--align-string (table info
)
2517 "Return an appropriate LaTeX alignment string.
2518 TABLE is the considered table. INFO is a plist used as
2519 a communication channel."
2520 (or (org-export-read-attribute :attr_latex table
:align
)
2522 ;; Extract column groups and alignment from first (non-rule)
2525 (org-element-map table
'table-row
2527 (and (eq (org-element-property :type row
) 'standard
) row
))
2531 (let ((borders (org-export-table-cell-borders cell info
)))
2532 ;; Check left border for the first cell only.
2533 (when (and (memq 'left borders
) (not align
))
2535 (push (case (org-export-table-cell-alignment cell info
)
2540 (when (memq 'right borders
) (push "|" align
))))
2542 (apply 'concat
(nreverse align
)))))
2544 (defun org-latex--org-table (table contents info
)
2545 "Return appropriate LaTeX code for an Org table.
2547 TABLE is the table type element to transcode. CONTENTS is its
2548 contents, as a string. INFO is a plist used as a communication
2551 This function assumes TABLE has `org' as its `:type' property and
2552 `table' as its `:mode' attribute."
2553 (let* ((caption (org-latex--caption/label-string table info
))
2554 (attr (org-export-read-attribute :attr_latex table
))
2555 ;; Determine alignment string.
2556 (alignment (org-latex--align-string table info
))
2557 ;; Determine environment for the table: longtable, tabular...
2558 (table-env (or (plist-get attr
:environment
)
2559 (plist-get info
:latex-default-table-environment
)))
2560 ;; If table is a float, determine environment: table, table*
2561 ;; or sidewaystable.
2562 (float-env (unless (member table-env
'("longtable" "longtabu"))
2563 (let ((float (plist-get attr
:float
)))
2565 ((and (not float
) (plist-member attr
:float
)) nil
)
2566 ((or (string= float
"sidewaystable")
2567 (string= float
"sideways")) "sidewaystable")
2568 ((string= float
"multicolumn") "table*")
2570 (org-element-property :caption table
)
2571 (org-string-nw-p (plist-get attr
:caption
)))
2573 ;; Extract others display options.
2574 (fontsize (let ((font (plist-get attr
:font
)))
2575 (and font
(concat font
"\n"))))
2576 (width (plist-get attr
:width
))
2577 (spreadp (plist-get attr
:spread
))
2579 (or (plist-get attr
:placement
)
2580 (format "[%s]" (plist-get info
:latex-default-figure-position
))))
2581 (centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2582 (plist-get info
:latex-tables-centered
)))
2583 (caption-above-p (plist-get info
:latex-table-caption-above
)))
2584 ;; Prepare the final format string for the table.
2587 ((equal "longtable" table-env
)
2588 (concat (and fontsize
(concat "{" fontsize
))
2589 (format "\\begin{longtable}{%s}\n" alignment
)
2590 (and caption-above-p
2591 (org-string-nw-p caption
)
2592 (concat caption
"\\\\\n"))
2594 (and (not caption-above-p
)
2595 (org-string-nw-p caption
)
2596 (concat caption
"\\\\\n"))
2597 "\\end{longtable}\n"
2598 (and fontsize
"}")))
2600 ((equal "longtabu" table-env
)
2601 (concat (and fontsize
(concat "{" fontsize
))
2602 (format "\\begin{longtabu}%s{%s}\n"
2605 (if spreadp
"spread" "to") width
) "")
2607 (and caption-above-p
2608 (org-string-nw-p caption
)
2609 (concat caption
"\\\\\n"))
2611 (and (not caption-above-p
)
2612 (org-string-nw-p caption
)
2613 (concat caption
"\\\\\n"))
2615 (and fontsize
"}")))
2619 (concat (format "\\begin{%s}%s\n" float-env placement
)
2620 (if caption-above-p caption
"")
2621 (when centerp
"\\centering\n")
2623 (centerp (concat "\\begin{center}\n" fontsize
))
2624 (fontsize (concat "{" fontsize
)))
2625 (cond ((equal "tabu" table-env
)
2626 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2628 (if spreadp
" spread %s " " to %s ")
2632 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2634 (if width
(format "{%s}" width
) "")
2640 (concat (if caption-above-p
"" caption
)
2641 (format "\n\\end{%s}" float-env
)))
2642 (centerp "\n\\end{center}")
2643 (fontsize "}")))))))
2645 (defun org-latex--table.el-table
(table info
)
2646 "Return appropriate LaTeX code for a table.el table.
2648 TABLE is the table type element to transcode. INFO is a plist
2649 used as a communication channel.
2651 This function assumes TABLE has `table.el' as its `:type'
2654 ;; Ensure "*org-export-table*" buffer is empty.
2655 (with-current-buffer (get-buffer-create "*org-export-table*")
2657 (let ((output (with-temp-buffer
2658 (insert (org-element-property :value table
))
2660 (re-search-forward "^[ \t]*|[^|]" nil t
)
2661 (table-generate-source 'latex
"*org-export-table*")
2662 (with-current-buffer "*org-export-table*"
2663 (org-trim (buffer-string))))))
2664 (kill-buffer (get-buffer "*org-export-table*"))
2665 ;; Remove left out comments.
2666 (while (string-match "^%.*\n" output
)
2667 (setq output
(replace-match "" t t output
)))
2668 (let ((attr (org-export-read-attribute :attr_latex table
)))
2669 (when (plist-get attr
:rmlines
)
2670 ;; When the "rmlines" attribute is provided, remove all hlines
2671 ;; but the the one separating heading from the table body.
2672 (let ((n 0) (pos 0))
2673 (while (and (< (length output
) pos
)
2674 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
2676 (unless (= n
2) (setq output
(replace-match "" nil nil output
))))))
2677 (let ((centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2678 (plist-get info
:latex-tables-centered
))))
2679 (if (not centerp
) output
2680 (format "\\begin{center}\n%s\n\\end{center}" output
))))))
2682 (defun org-latex--math-table (table info
)
2683 "Return appropriate LaTeX code for a matrix.
2685 TABLE is the table type element to transcode. INFO is a plist
2686 used as a communication channel.
2688 This function assumes TABLE has `org' as its `:type' property and
2689 `inline-math' or `math' as its `:mode' attribute.."
2690 (let* ((caption (org-latex--caption/label-string table info
))
2691 (attr (org-export-read-attribute :attr_latex table
))
2692 (inlinep (equal (plist-get attr
:mode
) "inline-math"))
2693 (env (or (plist-get attr
:environment
)
2694 (plist-get info
:latex-default-table-environment
)))
2698 ;; Ignore horizontal rules.
2699 (when (eq (org-element-property :type row
) 'standard
)
2700 ;; Return each cell unmodified.
2705 (org-element-interpret-data cell org-latex-pseudo-objects
)
2707 (org-element-map row
'table-cell
'identity info
) "&")
2708 (or (cdr (assoc env org-latex-table-matrix-macros
)) "\\\\")
2710 (org-element-map table
'table-row
'identity info
) ""))
2711 ;; Variables related to math clusters (contiguous math tables
2712 ;; of the same type).
2713 (mode (org-export-read-attribute :attr_latex table
:mode
))
2714 (prev (org-export-get-previous-element table info
))
2715 (next (org-export-get-next-element table info
))
2718 ;; Non-nil when TABLE has the same mode as current table.
2719 (string= (or (org-export-read-attribute :attr_latex table
:mode
)
2720 (plist-get info
:latex-default-table-mode
))
2723 ;; Opening string. If TABLE is in the middle of a table cluster,
2724 ;; do not insert any.
2726 (eq (org-element-type prev
) 'table
)
2727 (memq (org-element-property :post-blank prev
) '(0 nil
))
2728 (funcall same-mode-p prev
))
2731 ((org-string-nw-p caption
) (concat "\\begin{equation}\n" caption
))
2734 (or (plist-get attr
:math-prefix
) "")
2735 ;; Environment. Also treat special cases.
2736 (cond ((equal env
"array")
2737 (let ((align (org-latex--align-string table info
)))
2738 (format "\\begin{array}{%s}\n%s\\end{array}" align contents
)))
2739 ((assoc env org-latex-table-matrix-macros
)
2740 (format "\\%s%s{\n%s}"
2742 (or (plist-get attr
:math-arguments
) "")
2744 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env
)))
2746 (or (plist-get attr
:math-suffix
) "")
2747 ;; Closing string. If TABLE is in the middle of a table cluster,
2748 ;; do not insert any. If it closes such a cluster, be sure to
2749 ;; close the cluster with a string matching the opening string.
2751 (eq (org-element-type next
) 'table
)
2752 (memq (org-element-property :post-blank table
) '(0 nil
))
2753 (funcall same-mode-p next
))
2756 ;; Find cluster beginning to know which environment to use.
2757 ((let ((cluster-beg table
) prev
)
2758 (while (and (setq prev
(org-export-get-previous-element
2760 (memq (org-element-property :post-blank prev
)
2762 (funcall same-mode-p prev
))
2763 (setq cluster-beg prev
))
2764 (and (or (org-element-property :caption cluster-beg
)
2765 (org-element-property :name cluster-beg
))
2766 "\n\\end{equation}")))
2772 (defun org-latex-table-cell (table-cell contents info
)
2773 "Transcode a TABLE-CELL element from Org to LaTeX.
2774 CONTENTS is the cell contents. INFO is a plist used as
2775 a communication channel."
2777 (let ((scientific-format (plist-get info
:latex-table-scientific-notation
)))
2780 (string-match orgtbl-exp-regexp contents
))
2781 ;; Use appropriate format string for scientific
2783 (format scientific-format
2784 (match-string 1 contents
)
2785 (match-string 2 contents
))
2787 (when (org-export-get-next-element table-cell info
) " & ")))
2792 (defun org-latex-table-row (table-row contents info
)
2793 "Transcode a TABLE-ROW element from Org to LaTeX.
2794 CONTENTS is the contents of the row. INFO is a plist used as
2795 a communication channel."
2796 ;; Rules are ignored since table separators are deduced from
2797 ;; borders of the current row.
2798 (when (eq (org-element-property :type table-row
) 'standard
)
2799 (let* ((attr (org-export-read-attribute :attr_latex
2800 (org-export-get-parent table-row
)))
2802 (member (or (plist-get attr
:environment
)
2803 (plist-get info
:latex-default-table-environment
))
2804 '("longtable" "longtabu")))
2805 (booktabsp (if (plist-member attr
:booktabs
)
2806 (plist-get attr
:booktabs
)
2807 (plist-get info
:latex-tables-booktabs
)))
2808 ;; TABLE-ROW's borders are extracted from its first cell.
2809 (borders (org-export-table-cell-borders
2810 (car (org-element-contents table-row
)) info
)))
2812 ;; When BOOKTABS are activated enforce top-rule even when no
2813 ;; hline was specifically marked.
2814 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2815 ((and (memq 'top borders
) (memq 'above borders
)) "\\hline\n"))
2818 ;; Special case for long tables. Define header and footers.
2819 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
2822 \\multicolumn{%d}{l}{%s} \\\\
2827 %s\\multicolumn{%d}{r}{%s} \\\\
2830 (if booktabsp
"\\midrule" "\\hline")
2831 (cdr (org-export-table-dimensions
2832 (org-export-get-parent-table table-row
) info
))
2833 (org-latex--translate "Continued from previous page" info
)
2834 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2835 ((and (memq 'top borders
)
2836 (memq 'above borders
)) "\\hline\n")
2839 (if booktabsp
"\\midrule" "\\hline")
2840 (if booktabsp
"\\midrule" "\\hline")
2841 ;; Number of columns.
2842 (cdr (org-export-table-dimensions
2843 (org-export-get-parent-table table-row
) info
))
2844 (org-latex--translate "Continued on next page" info
)))
2845 ;; When BOOKTABS are activated enforce bottom rule even when
2846 ;; no hline was specifically marked.
2847 ((and booktabsp
(memq 'bottom borders
)) "\\bottomrule")
2848 ((and (memq 'bottom borders
) (memq 'below borders
)) "\\hline")
2849 ((memq 'below borders
) (if booktabsp
"\\midrule" "\\hline")))))))
2854 (defun org-latex-target (target contents info
)
2855 "Transcode a TARGET object from Org to LaTeX.
2856 CONTENTS is nil. INFO is a plist holding contextual
2858 (format "\\label{%s}"
2859 (org-export-solidify-link-text (org-element-property :value target
))))
2864 (defun org-latex-timestamp (timestamp contents info
)
2865 "Transcode a TIMESTAMP object from Org to LaTeX.
2866 CONTENTS is nil. INFO is a plist holding contextual
2868 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp
) info
)))
2871 (case (org-element-property :type timestamp
)
2872 ((active active-range
) :latex-active-timestamp-format
)
2873 ((inactive inactive-range
) :latex-inactive-timestamp-format
)
2874 (otherwise :latex-diary-timestamp-format
)))
2880 (defun org-latex-underline (underline contents info
)
2881 "Transcode UNDERLINE from Org to LaTeX.
2882 CONTENTS is the text with underline markup. INFO is a plist
2883 holding contextual information."
2884 (org-latex--text-markup contents
'underline info
))
2889 (defun org-latex-verbatim (verbatim contents info
)
2890 "Transcode a VERBATIM object from Org to LaTeX.
2891 CONTENTS is nil. INFO is a plist used as a communication
2893 (org-latex--text-markup
2894 (org-element-property :value verbatim
) 'verbatim info
))
2899 (defun org-latex-verse-block (verse-block contents info
)
2900 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2901 CONTENTS is verse block contents. INFO is a plist holding
2902 contextual information."
2903 (org-latex--wrap-label
2905 ;; In a verse environment, add a line break to each newline
2906 ;; character and change each white space at beginning of a line
2907 ;; into a space of 1 em. Also change each blank line with
2908 ;; a vertical space of 1 em.
2909 (format "\\begin{verse}\n%s\\end{verse}"
2910 (replace-regexp-in-string
2911 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m
)))
2912 (replace-regexp-in-string
2913 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
2914 (replace-regexp-in-string
2915 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
2916 contents nil t
) nil t
) nil t
))))
2920 ;;; End-user functions
2923 (defun org-latex-export-as-latex
2924 (&optional async subtreep visible-only body-only ext-plist
)
2925 "Export current buffer as a LaTeX buffer.
2927 If narrowing is active in the current buffer, only export its
2930 If a region is active, export that region.
2932 A non-nil optional argument ASYNC means the process should happen
2933 asynchronously. The resulting buffer should be accessible
2934 through the `org-export-stack' interface.
2936 When optional argument SUBTREEP is non-nil, export the sub-tree
2937 at point, extracting information from the headline properties
2940 When optional argument VISIBLE-ONLY is non-nil, don't export
2941 contents of hidden elements.
2943 When optional argument BODY-ONLY is non-nil, only write code
2944 between \"\\begin{document}\" and \"\\end{document}\".
2946 EXT-PLIST, when provided, is a property list with external
2947 parameters overriding Org default settings, but still inferior to
2948 file-local settings.
2950 Export is done in a buffer named \"*Org LATEX Export*\", which
2951 will be displayed when `org-export-show-temporary-export-buffer'
2954 (org-export-to-buffer 'latex
"*Org LATEX Export*"
2955 async subtreep visible-only body-only ext-plist
(lambda () (LaTeX-mode))))
2958 (defun org-latex-convert-region-to-latex ()
2959 "Assume the current region has org-mode syntax, and convert it to LaTeX.
2960 This can be used in any buffer. For example, you can write an
2961 itemized list in org-mode syntax in an LaTeX buffer and use this
2962 command to convert it."
2964 (org-export-replace-region-by 'latex
))
2967 (defun org-latex-export-to-latex
2968 (&optional async subtreep visible-only body-only ext-plist
)
2969 "Export current buffer to a LaTeX file.
2971 If narrowing is active in the current buffer, only export its
2974 If a region is active, export that region.
2976 A non-nil optional argument ASYNC means the process should happen
2977 asynchronously. The resulting file should be accessible through
2978 the `org-export-stack' interface.
2980 When optional argument SUBTREEP is non-nil, export the sub-tree
2981 at point, extracting information from the headline properties
2984 When optional argument VISIBLE-ONLY is non-nil, don't export
2985 contents of hidden elements.
2987 When optional argument BODY-ONLY is non-nil, only write code
2988 between \"\\begin{document}\" and \"\\end{document}\".
2990 EXT-PLIST, when provided, is a property list with external
2991 parameters overriding Org default settings, but still inferior to
2992 file-local settings."
2994 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
2995 (org-export-to-file 'latex outfile
2996 async subtreep visible-only body-only ext-plist
)))
2999 (defun org-latex-export-to-pdf
3000 (&optional async subtreep visible-only body-only ext-plist
)
3001 "Export current buffer to LaTeX then process through to PDF.
3003 If narrowing is active in the current buffer, only export its
3006 If a region is active, export that region.
3008 A non-nil optional argument ASYNC means the process should happen
3009 asynchronously. The resulting file should be accessible through
3010 the `org-export-stack' interface.
3012 When optional argument SUBTREEP is non-nil, export the sub-tree
3013 at point, extracting information from the headline properties
3016 When optional argument VISIBLE-ONLY is non-nil, don't export
3017 contents of hidden elements.
3019 When optional argument BODY-ONLY is non-nil, only write code
3020 between \"\\begin{document}\" and \"\\end{document}\".
3022 EXT-PLIST, when provided, is a property list with external
3023 parameters overriding Org default settings, but still inferior to
3024 file-local settings.
3026 Return PDF file's name."
3028 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
3029 (org-export-to-file 'latex outfile
3030 async subtreep visible-only body-only ext-plist
3031 (lambda (file) (org-latex-compile file
)))))
3033 (defun org-latex-compile (texfile &optional snippet
)
3034 "Compile a TeX file.
3036 TEXFILE is the name of the file being compiled. Processing is
3037 done through the command specified in `org-latex-pdf-process'.
3039 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3040 file used to preview a LaTeX snippet. In this case, do not
3041 create a log buffer and do not bother removing log files.
3043 Return PDF file name or an error if it couldn't be produced."
3044 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile
)))
3045 (full-name (file-truename texfile
))
3046 (out-dir (file-name-directory texfile
))
3047 ;; Properly set working directory for compilation.
3048 (default-directory (if (file-name-absolute-p texfile
)
3049 (file-name-directory full-name
)
3051 (time (current-time))
3053 (unless snippet
(message (format "Processing LaTeX file %s..." texfile
)))
3054 (save-window-excursion
3056 ;; A function is provided: Apply it.
3057 ((functionp org-latex-pdf-process
)
3058 (funcall org-latex-pdf-process
(shell-quote-argument texfile
)))
3059 ;; A list is provided: Replace %b, %f and %o with appropriate
3060 ;; values in each command before applying it. Output is
3061 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3062 ((consp org-latex-pdf-process
)
3063 (let ((outbuf (and (not snippet
)
3064 (get-buffer-create "*Org PDF LaTeX Output*"))))
3065 (dolist (command org-latex-pdf-process
)
3067 (replace-regexp-in-string
3068 "%b" (shell-quote-argument base-name
)
3069 (replace-regexp-in-string
3070 "%f" (shell-quote-argument full-name
)
3071 (replace-regexp-in-string
3072 "%o" (shell-quote-argument out-dir
) command t t
) t t
) t t
)
3074 ;; Collect standard errors from output buffer.
3075 (setq warnings
(and (not snippet
)
3076 (org-latex--collect-warnings outbuf
)))))
3077 (t (error "No valid command to process to PDF")))
3078 (let ((pdffile (concat out-dir base-name
".pdf")))
3079 ;; Check for process failure. Provide collected errors if
3081 (if (or (not (file-exists-p pdffile
))
3082 (time-less-p (nth 5 (file-attributes pdffile
)) time
))
3083 (error (format "PDF file %s wasn't produced" pdffile
))
3084 ;; Else remove log files, when specified, and signal end of
3085 ;; process to user, along with any error encountered.
3086 (when (and (not snippet
) org-latex-remove-logfiles
)
3087 (dolist (file (directory-files
3089 (concat (regexp-quote base-name
)
3090 "\\(?:\\.[0-9]+\\)?"
3092 (regexp-opt org-latex-logfiles-extensions
))))
3093 (delete-file file
)))
3094 (message (concat "PDF file produced"
3096 ((eq warnings
'error
) " with errors.")
3097 (warnings (concat " with warnings: " warnings
))
3099 ;; Return output file name.
3102 (defun org-latex--collect-warnings (buffer)
3103 "Collect some warnings from \"pdflatex\" command output.
3104 BUFFER is the buffer containing output. Return collected
3105 warnings types as a string, `error' if a LaTeX error was
3106 encountered or nil if there was none."
3107 (with-current-buffer buffer
3109 (goto-char (point-max))
3110 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t
)
3111 (if (re-search-forward "^!" nil t
) 'error
3112 (let ((case-fold-search t
)
3114 (dolist (warning org-latex-known-warnings
)
3115 (when (save-excursion (re-search-forward (car warning
) nil t
))
3116 (setq warnings
(concat warnings
" " (cdr warning
)))))
3117 (org-string-nw-p (org-trim warnings
))))))))
3120 (defun org-latex-publish-to-latex (plist filename pub-dir
)
3121 "Publish an Org file to LaTeX.
3123 FILENAME is the filename of the Org file to be published. PLIST
3124 is the property list for the given project. PUB-DIR is the
3125 publishing directory.
3127 Return output file name."
3128 (org-publish-org-to 'latex filename
".tex" plist pub-dir
))
3131 (defun org-latex-publish-to-pdf (plist filename pub-dir
)
3132 "Publish an Org file to PDF (via LaTeX).
3134 FILENAME is the filename of the Org file to be published. PLIST
3135 is the property list for the given project. PUB-DIR is the
3136 publishing directory.
3138 Return output file name."
3139 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3140 ;; in working directory and then moved to publishing directory.
3141 (org-publish-attachment
3145 'latex filename
".tex" plist
(file-name-directory filename
)))
3152 ;; generated-autoload-file: "org-loaddefs.el"
3155 ;;; ox-latex.el ends here