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-block . org-latex-export-block
)
53 (export-snippet . org-latex-export-snippet
)
54 (fixed-width . org-latex-fixed-width
)
55 (footnote-definition . org-latex-footnote-definition
)
56 (footnote-reference . org-latex-footnote-reference
)
57 (headline . org-latex-headline
)
58 (horizontal-rule . org-latex-horizontal-rule
)
59 (inline-src-block . org-latex-inline-src-block
)
60 (inlinetask . org-latex-inlinetask
)
61 (italic . org-latex-italic
)
62 (item . org-latex-item
)
63 (keyword . org-latex-keyword
)
64 (latex-environment . org-latex-latex-environment
)
65 (latex-fragment . org-latex-latex-fragment
)
66 (line-break . org-latex-line-break
)
67 (link . org-latex-link
)
68 (node-property . org-latex-node-property
)
69 (paragraph . org-latex-paragraph
)
70 (plain-list . org-latex-plain-list
)
71 (plain-text . org-latex-plain-text
)
72 (planning . org-latex-planning
)
73 (property-drawer . org-latex-property-drawer
)
74 (quote-block . org-latex-quote-block
)
75 (radio-target . org-latex-radio-target
)
76 (section . org-latex-section
)
77 (special-block . org-latex-special-block
)
78 (src-block . org-latex-src-block
)
79 (statistics-cookie . org-latex-statistics-cookie
)
80 (strike-through . org-latex-strike-through
)
81 (subscript . org-latex-subscript
)
82 (superscript . org-latex-superscript
)
83 (table . org-latex-table
)
84 (table-cell . org-latex-table-cell
)
85 (table-row . org-latex-table-row
)
86 (target . org-latex-target
)
87 (template . org-latex-template
)
88 (timestamp . org-latex-timestamp
)
89 (underline . org-latex-underline
)
90 (verbatim . org-latex-verbatim
)
91 (verse-block . org-latex-verse-block
)
93 (latex-math-block . org-latex-math-block
))
94 :export-block
'("LATEX" "TEX")
96 '(?l
"Export to LaTeX"
97 ((?L
"As LaTeX buffer" org-latex-export-as-latex
)
98 (?l
"As LaTeX file" org-latex-export-to-latex
)
99 (?p
"As PDF file" org-latex-export-to-pdf
)
100 (?o
"As PDF file and open"
102 (if a
(org-latex-export-to-pdf t s v b
)
103 (org-open-file (org-latex-export-to-pdf nil s v b
)))))))
104 :filters-alist
'((:filter-options . org-latex-math-block-options-filter
)
105 (:filter-parse-tree . org-latex-math-block-tree-filter
))
107 '((:latex-class
"LATEX_CLASS" nil org-latex-default-class t
)
108 (:latex-class-options
"LATEX_CLASS_OPTIONS" nil nil t
)
109 (:latex-header
"LATEX_HEADER" nil nil newline
)
110 (:latex-header-extra
"LATEX_HEADER_EXTRA" nil nil newline
)
112 (:latex-active-timestamp-format nil nil org-latex-active-timestamp-format
)
113 (:latex-classes nil nil org-latex-classes
)
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-inputenc-alist nil nil org-latex-inputenc-alist
)
129 (:latex-known-warnings nil nil org-latex-known-warnings
)
130 (:latex-link-with-unknown-path-format nil nil org-latex-link-with-unknown-path-format
)
131 (:latex-listings nil nil org-latex-listings
)
132 (:latex-listings-langs nil nil org-latex-listings-langs
)
133 (:latex-listings-options nil nil org-latex-listings-options
)
134 (:latex-logfiles-extensions nil nil org-latex-logfiles-extensions
)
135 (:latex-minted-langs nil nil org-latex-minted-langs
)
136 (:latex-minted-options nil nil org-latex-minted-options
)
137 (:latex-pdf-process nil nil org-latex-pdf-process
)
138 (:latex-remove-logfiles nil nil org-latex-remove-logfiles
)
139 (:latex-table-caption-above nil nil org-latex-table-caption-above
)
140 (:latex-table-scientific-notation nil nil org-latex-table-scientific-notation
)
141 (:latex-tables-booktabs nil nil org-latex-tables-booktabs
)
142 (:latex-tables-centered nil nil org-latex-tables-centered
)
143 (:latex-text-markup-alist nil nil org-latex-text-markup-alist
)
144 (:latex-title-command nil nil org-latex-title-command
)
145 (:latex-toc-command nil nil org-latex-toc-command
)
146 ;; Redefine regular options.
147 (:date
"DATE" nil
"\\today" t
)))
151 ;;; Internal Variables
153 (defconst org-latex-babel-language-alist
154 '(("af" .
"afrikaans")
156 ("bt-br" .
"brazilian")
162 ("de-at" .
"naustrian")
163 ("de-de" .
"ngerman")
166 ("en-au" .
"australian")
167 ("en-ca" .
"canadian")
168 ("en-gb" .
"british")
170 ("en-nz" .
"newzealand")
171 ("en-us" .
"american")
177 ("fr-ca" .
"canadien")
181 ("id" .
"indonesian")
191 ("pt" .
"portuguese")
195 ("sb" .
"uppersorbian")
203 ("uk" .
"ukrainian"))
204 "Alist between language code and corresponding Babel option.")
206 (defconst org-latex-table-matrix-macros
'(("bordermatrix" .
"\\cr")
207 ("qbordermatrix" .
"\\cr")
208 ("kbordermatrix" .
"\\\\"))
209 "Alist between matrix macros and their row ending.")
211 (defconst org-latex-pseudo-objects
'(latex-math-block)
212 "List of pseudo-object types introduced in the back-end.")
216 ;;; User Configurable Variables
218 (defgroup org-export-latex nil
219 "Options for exporting Org mode files to LaTeX."
220 :tag
"Org Export LaTeX"
226 (defcustom org-latex-default-class
"article"
227 "The default LaTeX class."
228 :group
'org-export-latex
229 :type
'(string :tag
"LaTeX class"))
231 (defcustom org-latex-classes
233 "\\documentclass[11pt]{article}"
234 ("\\section{%s}" .
"\\section*{%s}")
235 ("\\subsection{%s}" .
"\\subsection*{%s}")
236 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
237 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
238 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
240 "\\documentclass[11pt]{report}"
241 ("\\part{%s}" .
"\\part*{%s}")
242 ("\\chapter{%s}" .
"\\chapter*{%s}")
243 ("\\section{%s}" .
"\\section*{%s}")
244 ("\\subsection{%s}" .
"\\subsection*{%s}")
245 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
247 "\\documentclass[11pt]{book}"
248 ("\\part{%s}" .
"\\part*{%s}")
249 ("\\chapter{%s}" .
"\\chapter*{%s}")
250 ("\\section{%s}" .
"\\section*{%s}")
251 ("\\subsection{%s}" .
"\\subsection*{%s}")
252 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
253 "Alist of LaTeX classes and associated header and structure.
254 If #+LATEX_CLASS is set in the buffer, use its value and the
255 associated information. Here is the structure of each cell:
259 \(numbered-section . unnumbered-section)
265 The HEADER-STRING is the header that will be inserted into the
266 LaTeX file. It should contain the \\documentclass macro, and
267 anything else that is needed for this setup. To this header, the
268 following commands will be added:
270 - Calls to \\usepackage for all packages mentioned in the
271 variables `org-latex-default-packages-alist' and
272 `org-latex-packages-alist'. Thus, your header definitions
273 should avoid to also request these packages.
275 - Lines specified via \"#+LATEX_HEADER:\" and
276 \"#+LATEX_HEADER_EXTRA:\" keywords.
278 If you need more control about the sequence in which the header
279 is built up, or if you want to exclude one of these building
280 blocks for a particular class, you can use the following
281 macro-like placeholders.
283 [DEFAULT-PACKAGES] \\usepackage statements for default packages
284 [NO-DEFAULT-PACKAGES] do not include any of the default packages
285 [PACKAGES] \\usepackage statements for packages
286 [NO-PACKAGES] do not include the packages
287 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
288 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
292 \\documentclass{article}
293 [NO-DEFAULT-PACKAGES]
295 \\providecommand{\\alert}[1]{\\textbf{#1}}
298 will omit the default packages, and will include the
299 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
300 to \\providecommand, and then place \\usepackage commands based
301 on the content of `org-latex-packages-alist'.
303 If your header, `org-latex-default-packages-alist' or
304 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
305 AUTO will automatically be replaced with a coding system derived
306 from `buffer-file-coding-system'. See also the variable
307 `org-latex-inputenc-alist' for a way to influence this mechanism.
309 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\",
310 AUTO will be replaced with the language related to the language
311 code specified by `org-export-default-language', which see. Note
312 that constructions such as \"\\usepackage[french,AUTO,english]{babel}\"
315 The sectioning structure
316 ------------------------
318 The sectioning structure of the class is given by the elements
319 following the header string. For each sectioning level, a number
320 of strings is specified. A %s formatter is mandatory in each
321 section string and will be replaced by the title of the section.
323 Instead of a cons cell (numbered . unnumbered), you can also
324 provide a list of 2 or 4 elements,
326 \(numbered-open numbered-close)
330 \(numbered-open numbered-close unnumbered-open unnumbered-close)
332 providing opening and closing strings for a LaTeX environment
333 that should represent the document section. The opening clause
334 should have a %s to represent the section title.
336 Instead of a list of sectioning commands, you can also specify
337 a function name. That function will be called with two
338 parameters, the (reduced) level of the headline, and a predicate
339 non-nil when the headline should be numbered. It must return
340 a format string in which the section title will be added."
341 :group
'org-export-latex
343 (list (string :tag
"LaTeX class")
344 (string :tag
"LaTeX header")
345 (repeat :tag
"Levels" :inline t
348 (string :tag
" numbered")
349 (string :tag
"unnumbered"))
350 (list :tag
"Environment"
351 (string :tag
"Opening (numbered)")
352 (string :tag
"Closing (numbered)")
353 (string :tag
"Opening (unnumbered)")
354 (string :tag
"Closing (unnumbered)"))
355 (function :tag
"Hook computing sectioning"))))))
357 (defcustom org-latex-inputenc-alist nil
358 "Alist of inputenc coding system names, and what should really be used.
359 For example, adding an entry
361 (\"utf8\" . \"utf8x\")
363 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
364 are written as utf8 files."
365 :group
'org-export-latex
368 (string :tag
"Derived from buffer")
369 (string :tag
"Use this instead"))))
371 (defcustom org-latex-title-command
"\\maketitle"
372 "The command used to insert the title just after \\begin{document}.
373 If this string contains the formatting specification \"%s\" then
374 it will be used as a formatting string, passing the title as an
376 :group
'org-export-latex
379 (defcustom org-latex-toc-command
"\\tableofcontents\n\n"
380 "LaTeX command to set the table of contents, list of figures, etc.
381 This command only applies to the table of contents generated with
382 the toc:nil option, not to those generated with #+TOC keyword."
383 :group
'org-export-latex
386 (defcustom org-latex-hyperref-template
387 "\\hypersetup{\n pdfkeywords={%k},\n pdfsubject={%d},\n pdfcreator={%c}}\n"
388 "Template for hyperref package options.
390 Value is a format string, which can contain the following placeholders:
393 %d for DESCRIPTION line
396 Set it to the empty string to ignore the command completely."
397 :group
'org-export-latex
399 :package-version
'(Org .
"8.3")
404 (defcustom org-latex-format-headline-function
405 'org-latex-format-headline-default-function
406 "Function for formatting the headline's text.
408 This function will be called with 5 arguments:
409 TODO the todo keyword (string or nil).
410 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
411 PRIORITY the priority of the headline (integer or nil)
412 TEXT the main headline text (string).
413 TAGS the tags as a list of strings (list of strings or nil).
415 The function result will be used in the section format string.
417 Use `org-latex-format-headline-default-function' by default,
418 which format headlines like for Org version prior to 8.0."
419 :group
'org-export-latex
421 :package-version
'(Org .
"8.0")
424 (defcustom org-latex-custom-id-as-label nil
425 "Toggle use of CUSTOM_ID properties for generating section labels.
427 When this variable is non-nil, Org will use the value of a
428 headline's CUSTOM_ID property as the key for the \\label command
429 for the LaTeX section corresponding to the headline.
431 By default, Org generates its own internal section labels for all
432 headlines during LaTeX export. This process ensures that the
433 \\label keys are unique and valid, but it means the keys are not
434 available in advance of the export process.
436 Setting this variable gives you control over how Org generates
437 labels for sections during LaTeX export, so that you may know
438 their keys in advance. One reason to do this is that it allows
439 you to refer to headlines using a single label both in Org's link
440 syntax and in embedded LaTeX code.
442 For example, when this variable is non-nil, a headline like this:
448 This is section [[#sec:foo]].
450 And this is still section \\ref{sec:foo}.
453 will be exported to LaTeX as:
455 \\subsection{Some section}
457 This is section \\ref{sec:foo}.
458 And this is still section \\ref{sec:foo}.
460 Note, however, that setting this variable introduces a limitation
461 on the possible values for CUSTOM_ID. When this variable is
462 non-nil and a headline defines a CUSTOM_ID value, Org simply
463 passes this value to \\label unchanged. You are responsible for
464 ensuring that the value is a valid LaTeX \\label key, and that no
465 other \\label commands with the same key appear elsewhere in your
466 document. (Keys may contain letters, numbers, and the following
467 punctuation: '_' '.' '-' ':'.) There are no such limitations on
468 CUSTOM_ID when this variable is nil.
470 For headlines that do not define the CUSTOM_ID property, Org will
471 continue to use its default labeling scheme to generate labels
472 and resolve links into section references."
473 :group
'org-export-latex
476 :package-version
'(Org .
"8.3"))
480 (defcustom org-latex-footnote-separator
"\\textsuperscript{,}\\,"
481 "Text used to separate footnotes."
482 :group
'org-export-latex
488 (defcustom org-latex-active-timestamp-format
"\\textit{%s}"
489 "A printf format string to be applied to active timestamps."
490 :group
'org-export-latex
493 (defcustom org-latex-inactive-timestamp-format
"\\textit{%s}"
494 "A printf format string to be applied to inactive timestamps."
495 :group
'org-export-latex
498 (defcustom org-latex-diary-timestamp-format
"\\textit{%s}"
499 "A printf format string to be applied to diary timestamps."
500 :group
'org-export-latex
506 (defcustom org-latex-image-default-option
""
507 "Default option for images."
508 :group
'org-export-latex
510 :package-version
'(Org .
"8.0")
513 (defcustom org-latex-image-default-width
".9\\linewidth"
514 "Default width for images.
515 This value will not be used if a height is provided."
516 :group
'org-export-latex
518 :package-version
'(Org .
"8.0")
521 (defcustom org-latex-image-default-height
""
522 "Default height for images.
523 This value will not be used if a width is provided, or if the
524 image is wrapped within a \"figure\" or \"wrapfigure\"
526 :group
'org-export-latex
528 :package-version
'(Org .
"8.0")
531 (defcustom org-latex-default-figure-position
"htb"
532 "Default position for latex figures."
533 :group
'org-export-latex
536 (defcustom org-latex-inline-image-rules
537 '(("file" .
"\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
538 "Rules characterizing image files that can be inlined into LaTeX.
540 A rule consists in an association whose key is the type of link
541 to consider, and value is a regexp that will be matched against
544 Note that, by default, the image extension *actually* allowed
545 depend on the way the LaTeX file is processed. When used with
546 pdflatex, pdf, jpg and png images are OK. When processing
547 through dvi to Postscript, only ps and eps are allowed. The
548 default we use here encompasses both."
549 :group
'org-export-latex
551 :package-version
'(Org .
"8.0")
552 :type
'(alist :key-type
(string :tag
"Type")
553 :value-type
(regexp :tag
"Path")))
555 (defcustom org-latex-link-with-unknown-path-format
"\\texttt{%s}"
556 "Format string for links with unknown path type."
557 :group
'org-export-latex
563 (defcustom org-latex-default-table-environment
"tabular"
564 "Default environment used to build tables."
565 :group
'org-export-latex
567 :package-version
'(Org .
"8.0")
570 (defcustom org-latex-default-table-mode
'table
571 "Default mode for tables.
573 Value can be a symbol among:
575 `table' Regular LaTeX table.
577 `math' In this mode, every cell is considered as being in math
578 mode and the complete table will be wrapped within a math
579 environment. It is particularly useful to write matrices.
581 `inline-math' This mode is almost the same as `math', but the
582 math environment will be inlined.
584 `verbatim' The table is exported as it appears in the Org
585 buffer, within a verbatim environment.
587 This value can be overridden locally with, i.e. \":mode math\" in
590 When modifying this variable, it may be useful to change
591 `org-latex-default-table-environment' accordingly."
592 :group
'org-export-latex
594 :package-version
'(Org .
"8.0")
595 :type
'(choice (const :tag
"Table" table
)
596 (const :tag
"Matrix" math
)
597 (const :tag
"Inline matrix" inline-math
)
598 (const :tag
"Verbatim" verbatim
))
599 :safe
(lambda (s) (memq s
'(table math inline-math verbatim
))))
601 (defcustom org-latex-tables-centered t
602 "When non-nil, tables are exported in a center environment."
603 :group
'org-export-latex
607 (defcustom org-latex-tables-booktabs nil
608 "When non-nil, display tables in a formal \"booktabs\" style.
609 This option assumes that the \"booktabs\" package is properly
610 loaded in the header of the document. This value can be ignored
611 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
613 :group
'org-export-latex
615 :package-version
'(Org .
"8.0")
619 (defcustom org-latex-table-caption-above t
620 "When non-nil, place caption string at the beginning of the table.
621 Otherwise, place it near the end."
622 :group
'org-export-latex
626 (defcustom org-latex-table-scientific-notation
"%s\\,(%s)"
627 "Format string to display numbers in scientific notation.
628 The format should have \"%s\" twice, for mantissa and exponent
629 \(i.e., \"%s\\\\times10^{%s}\").
631 When nil, no transformation is made."
632 :group
'org-export-latex
634 :package-version
'(Org .
"8.0")
636 (string :tag
"Format string")
637 (const :tag
"No formatting")))
641 (defcustom org-latex-text-markup-alist
'((bold .
"\\textbf{%s}")
643 (italic .
"\\emph{%s}")
644 (strike-through .
"\\sout{%s}")
645 (underline .
"\\uline{%s}")
646 (verbatim . protectedtexttt
))
647 "Alist of LaTeX expressions to convert text markup.
649 The key must be a symbol among `bold', `code', `italic',
650 `strike-through', `underline' and `verbatim'. The value is
651 a formatting string to wrap fontified text with.
653 Value can also be set to the following symbols: `verb' and
654 `protectedtexttt'. For the former, Org will use \"\\verb\" to
655 create a format string and select a delimiter character that
656 isn't in the string. For the latter, Org will use \"\\texttt\"
657 to typeset and try to protect special characters.
659 If no association can be found for a given markup, text will be
661 :group
'org-export-latex
663 :options
'(bold code italic strike-through underline verbatim
))
668 (defcustom org-latex-format-drawer-function
669 (lambda (name contents
) contents
)
670 "Function called to format a drawer in LaTeX code.
672 The function must accept two parameters:
673 NAME the drawer name, like \"LOGBOOK\"
674 CONTENTS the contents of the drawer.
676 The function should return the string to be exported.
678 The default function simply returns the value of CONTENTS."
679 :group
'org-export-latex
681 :package-version
'(Org .
"8.3")
687 (defcustom org-latex-format-inlinetask-function
'ignore
688 "Function called to format an inlinetask in LaTeX code.
690 The function must accept six parameters:
691 TODO the todo keyword, as a string
692 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
693 PRIORITY the inlinetask priority, as a string
694 NAME the inlinetask name, as a string.
695 TAGS the inlinetask tags, as a list of strings.
696 CONTENTS the contents of the inlinetask, as a string.
698 The function should return the string to be exported.
700 For example, the variable could be set to the following function
701 in order to mimic default behaviour:
703 \(defun org-latex-format-inlinetask \(todo type priority name tags contents\)
704 \"Format an inline task element for LaTeX export.\"
708 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
709 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
712 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
713 \(mapconcat 'identity tags \":\")))))
714 \(format (concat \"\\\\begin{center}\\n\"
716 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
718 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
720 \"\\\\end{minipage}}\"
722 full-title contents))"
723 :group
'org-export-latex
729 (defcustom org-latex-listings nil
730 "Non-nil means export source code using the listings package.
732 This package will fontify source code, possibly even with color.
733 If you want to use this, you also need to make LaTeX use the
734 listings package, and if you want to have color, the color
735 package. Just add these to `org-latex-packages-alist', for
736 example using customize, or with something like:
739 \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
740 \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
744 \(setq org-latex-listings 'minted)
746 causes source code to be exported using the minted package as
747 opposed to listings. If you want to use minted, you need to add
748 the minted package to `org-latex-packages-alist', for example
749 using customize, or with
752 \(add-to-list 'org-latex-packages-alist '(\"\" \"minted\"))
754 In addition, it is necessary to install pygments
755 \(http://pygments.org), and to configure the variable
756 `org-latex-pdf-process' so that the -shell-escape option is
759 The minted choice has possible repercussions on the preview of
760 latex fragments (see `org-preview-latex-fragment'). If you run
761 into previewing problems, please consult
763 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
764 :group
'org-export-latex
766 (const :tag
"Use listings" t
)
767 (const :tag
"Use minted" minted
)
768 (const :tag
"Export verbatim" nil
))
769 :safe
(lambda (s) (memq s
'(t nil minted
))))
771 (defcustom org-latex-listings-langs
772 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
775 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
776 (html "HTML") (xml "XML")
777 (tex "TeX") (latex "[LaTeX]TeX")
778 (shell-script "bash")
780 (ocaml "Caml") (caml "Caml")
781 (sql "SQL") (sqlite "sql"))
782 "Alist mapping languages to their listing language counterpart.
783 The key is a symbol, the major mode symbol without the \"-mode\".
784 The value is the string that should be inserted as the language
785 parameter for the listings package. If the mode name and the
786 listings name are the same, the language does not need an entry
787 in this list - but it does not hurt if it is present."
788 :group
'org-export-latex
791 (symbol :tag
"Major mode ")
792 (string :tag
"Listings language"))))
794 (defcustom org-latex-listings-options nil
795 "Association list of options for the latex listings package.
797 These options are supplied as a comma-separated list to the
798 \\lstset command. Each element of the association list should be
799 a list containing two strings: the name of the option, and the
802 (setq org-latex-listings-options
803 '((\"basicstyle\" \"\\\\small\")
804 (\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
806 will typeset the code in a small size font with underlined, bold
809 Note that the same options will be applied to blocks of all
810 languages. If you need block-specific options, you may use the
813 #+ATTR_LATEX: :options key1=value1,key2=value2
817 :group
'org-export-latex
820 (string :tag
"Listings option name ")
821 (string :tag
"Listings option value"))))
823 (defcustom org-latex-minted-langs
824 '((emacs-lisp "common-lisp")
827 (shell-script "bash")
829 "Alist mapping languages to their minted language counterpart.
830 The key is a symbol, the major mode symbol without the \"-mode\".
831 The value is the string that should be inserted as the language
832 parameter for the minted package. If the mode name and the
833 listings name are the same, the language does not need an entry
834 in this list - but it does not hurt if it is present.
836 Note that minted uses all lower case for language identifiers,
837 and that the full list of language identifiers can be obtained
840 pygmentize -L lexers"
841 :group
'org-export-latex
844 (symbol :tag
"Major mode ")
845 (string :tag
"Minted language"))))
847 (defcustom org-latex-minted-options nil
848 "Association list of options for the latex minted package.
850 These options are supplied within square brackets in
851 \\begin{minted} environments. Each element of the alist should
852 be a list containing two strings: the name of the option, and the
855 \(setq org-latex-minted-options
856 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
858 will result in src blocks being exported with
860 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
862 as the start of the minted environment. Note that the same
863 options will be applied to blocks of all languages. If you need
864 block-specific options, you may use the following syntax:
866 #+ATTR_LATEX: :options key1=value1,key2=value2
870 :group
'org-export-latex
873 (string :tag
"Minted option name ")
874 (string :tag
"Minted option value"))))
876 (defvar org-latex-custom-lang-environments nil
877 "Alist mapping languages to language-specific LaTeX environments.
879 It is used during export of src blocks by the listings and minted
880 latex packages. For example,
882 \(setq org-latex-custom-lang-environments
883 '\(\(python \"pythoncode\"\)\)\)
885 would have the effect that if org encounters begin_src python
886 during latex export it will output
895 (defcustom org-latex-pdf-process
896 '("pdflatex -interaction nonstopmode -output-directory %o %f"
897 "pdflatex -interaction nonstopmode -output-directory %o %f"
898 "pdflatex -interaction nonstopmode -output-directory %o %f")
899 "Commands to process a LaTeX file to a PDF file.
900 This is a list of strings, each of them will be given to the
901 shell as a command. %f in the command will be replaced by the
902 full file name, %b by the file base name (i.e. without directory
903 and extension parts) and %o by the base directory of the file.
905 The reason why this is a list is that it usually takes several
906 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
907 does not have a clever mechanism to detect which of these
908 commands have to be run to get to a stable result, and it also
909 does not do any error checking.
911 By default, Org uses 3 runs of `pdflatex' to do the processing.
912 If you have texi2dvi on your system and if that does not cause
913 the infamous egrep/locale bug:
915 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
917 then `texi2dvi' is the superior choice as it automates the LaTeX
918 build process by calling the \"correct\" combinations of
919 auxiliary programs. Org does offer `texi2dvi' as one of the
920 customize options. Alternatively, `rubber' and `latexmk' also
921 provide similar functionality. The latter supports `biber' out
924 Alternatively, this may be a Lisp function that does the
925 processing, so you could use this to apply the machinery of
926 AUCTeX or the Emacs LaTeX mode. This function should accept the
927 file name as its single argument."
928 :group
'org-export-pdf
930 (repeat :tag
"Shell command sequence"
931 (string :tag
"Shell command"))
932 (const :tag
"2 runs of pdflatex"
933 ("pdflatex -interaction nonstopmode -output-directory %o %f"
934 "pdflatex -interaction nonstopmode -output-directory %o %f"))
935 (const :tag
"3 runs of pdflatex"
936 ("pdflatex -interaction nonstopmode -output-directory %o %f"
937 "pdflatex -interaction nonstopmode -output-directory %o %f"
938 "pdflatex -interaction nonstopmode -output-directory %o %f"))
939 (const :tag
"pdflatex,bibtex,pdflatex,pdflatex"
940 ("pdflatex -interaction nonstopmode -output-directory %o %f"
942 "pdflatex -interaction nonstopmode -output-directory %o %f"
943 "pdflatex -interaction nonstopmode -output-directory %o %f"))
944 (const :tag
"2 runs of xelatex"
945 ("xelatex -interaction nonstopmode -output-directory %o %f"
946 "xelatex -interaction nonstopmode -output-directory %o %f"))
947 (const :tag
"3 runs of xelatex"
948 ("xelatex -interaction nonstopmode -output-directory %o %f"
949 "xelatex -interaction nonstopmode -output-directory %o %f"
950 "xelatex -interaction nonstopmode -output-directory %o %f"))
951 (const :tag
"xelatex,bibtex,xelatex,xelatex"
952 ("xelatex -interaction nonstopmode -output-directory %o %f"
954 "xelatex -interaction nonstopmode -output-directory %o %f"
955 "xelatex -interaction nonstopmode -output-directory %o %f"))
956 (const :tag
"texi2dvi"
957 ("texi2dvi -p -b -V %f"))
959 ("rubber -d --into %o %f"))
960 (const :tag
"latexmk"
961 ("latexmk -g -pdf %f"))
964 (defcustom org-latex-logfiles-extensions
965 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
966 "The list of file extensions to consider as LaTeX logfiles.
967 The logfiles will be remove if `org-latex-remove-logfiles' is
969 :group
'org-export-latex
970 :type
'(repeat (string :tag
"Extension")))
972 (defcustom org-latex-remove-logfiles t
973 "Non-nil means remove the logfiles produced by PDF production.
974 By default, logfiles are files with these extensions: .aux, .idx,
975 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
976 logfiles to remove, set `org-latex-logfiles-extensions'."
977 :group
'org-export-latex
980 (defcustom org-latex-known-warnings
981 '(("Reference.*?undefined" .
"[undefined reference]")
982 ("Runaway argument" .
"[runaway argument]")
983 ("Underfull \\hbox" .
"[underfull hbox]")
984 ("Overfull \\hbox" .
"[overfull hbox]")
985 ("Citation.*?undefined" .
"[undefined citation]")
986 ("Undefined control sequence" .
"[undefined control sequence]"))
987 "Alist of regular expressions and associated messages for the user.
988 The regular expressions are used to find possible warnings in the
989 log of a latex-run. These warnings will be reported after
990 calling `org-latex-compile'."
991 :group
'org-export-latex
993 :package-version
'(Org .
"8.3")
996 (string :tag
"Regexp")
997 (string :tag
"Message"))))
1001 ;;; Internal Functions
1003 (defun org-latex--caption/label-string
(element info
)
1004 "Return caption and label LaTeX string for ELEMENT.
1006 INFO is a plist holding contextual information. If there's no
1007 caption nor label, return the empty string.
1009 For non-floats, see `org-latex--wrap-label'."
1010 (let* ((label (org-element-property :name element
))
1011 (label-str (if (not (org-string-nw-p label
)) ""
1012 (format "\\label{%s}"
1013 (org-export-solidify-link-text label
))))
1014 (main (org-export-get-caption element
))
1015 (short (org-export-get-caption element t
))
1016 (caption-from-attr-latex (org-export-read-attribute :attr_latex element
:caption
)))
1018 ((org-string-nw-p caption-from-attr-latex
)
1019 (concat caption-from-attr-latex
"\n"))
1020 ((and (not main
) (equal label-str
"")) "")
1021 ((not main
) (concat label-str
"\n"))
1022 ;; Option caption format with short name.
1023 (short (format "\\caption[%s]{%s%s}\n"
1024 (org-export-data short info
)
1026 (org-export-data main info
)))
1027 ;; Standard caption format.
1028 (t (format "\\caption{%s%s}\n" label-str
(org-export-data main info
))))))
1030 (defun org-latex-guess-inputenc (header)
1031 "Set the coding system in inputenc to what the buffer is.
1033 HEADER is the LaTeX header string. This function only applies
1034 when specified inputenc option is \"AUTO\".
1036 Return the new header, as a string."
1037 (let* ((cs (or (ignore-errors
1038 (latexenc-coding-system-to-inputenc
1039 (or org-export-coding-system buffer-file-coding-system
)))
1042 ;; First translate if that is requested.
1043 (setq cs
(or (cdr (assoc cs org-latex-inputenc-alist
)) cs
))
1044 ;; Then find the \usepackage statement and replace the option.
1045 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1046 cs header t nil
1))))
1048 (defun org-latex-guess-babel-language (header info
)
1049 "Set Babel's language according to LANGUAGE keyword.
1051 HEADER is the LaTeX header string. INFO is the plist used as
1052 a communication channel.
1054 Insertion of guessed language only happens when Babel package has
1055 explicitly been loaded. Then it is added to the rest of
1058 The argument to Babel may be \"AUTO\" which is then replaced with
1059 the language of the document or `org-export-default-language'
1060 unless language in question is already loaded.
1062 Return the new header."
1063 (let ((language-code (plist-get info
:language
)))
1064 ;; If no language is set or Babel package is not loaded, return
1066 (if (or (not (stringp language-code
))
1067 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header
)))
1069 (let ((options (save-match-data
1070 (org-split-string (match-string 1 header
) ",[ \t]*")))
1071 (language (cdr (assoc language-code
1072 org-latex-babel-language-alist
))))
1073 ;; If LANGUAGE is already loaded, return header without AUTO.
1074 ;; Otherwise, replace AUTO with language or append language if
1075 ;; AUTO is not present.
1077 (mapconcat (lambda (option) (if (equal "AUTO" option
) language option
))
1078 (cond ((member language options
) (delete "AUTO" options
))
1079 ((member "AUTO" options
) options
)
1080 (t (append options
(list language
))))
1084 (defun org-latex--find-verb-separator (s)
1085 "Return a character not used in string S.
1086 This is used to choose a separator for constructs like \\verb."
1087 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1088 (loop for c across ll
1089 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
1090 return
(char-to-string c
))))
1092 (defun org-latex--make-option-string (options)
1093 "Return a comma separated string of keywords and values.
1094 OPTIONS is an alist where the key is the options keyword as
1095 a string, and the value a list containing the keyword value, or
1097 (mapconcat (lambda (pair)
1098 (concat (first pair
)
1099 (when (> (length (second pair
)) 0)
1100 (concat "=" (second pair
)))))
1104 (defun org-latex--wrap-label (element output
)
1105 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1106 This function shouldn't be used for floats. See
1107 `org-latex--caption/label-string'."
1108 (let ((label (org-element-property :name element
)))
1109 (if (not (and (org-string-nw-p output
) (org-string-nw-p label
))) output
1110 (concat (format "\\label{%s}\n" (org-export-solidify-link-text label
))
1113 (defun org-latex--text-markup (text markup
)
1114 "Format TEXT depending on MARKUP text markup.
1115 See `org-latex-text-markup-alist' for details."
1116 (let ((fmt (cdr (assq markup org-latex-text-markup-alist
))))
1118 ;; No format string: Return raw text.
1120 ;; Handle the `verb' special case: Find an appropriate separator
1121 ;; and use "\\verb" command.
1123 (let ((separator (org-latex--find-verb-separator text
)))
1124 (concat "\\verb" separator
1125 (replace-regexp-in-string "\n" " " text
)
1127 ;; Handle the `protectedtexttt' special case: Protect some
1128 ;; special chars and use "\texttt{%s}" format string.
1129 ((eq 'protectedtexttt fmt
)
1131 (trans '(("\\" .
"\\textbackslash{}")
1132 ("~" .
"\\textasciitilde{}")
1133 ("^" .
"\\textasciicircum{}")))
1136 (while (string-match "[\\{}$%&_#~^]" text
)
1137 (setq char
(match-string 0 text
))
1138 (if (> (match-beginning 0) 0)
1139 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
1140 (setq text
(substring text
(1+ (match-beginning 0))))
1141 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
1142 rtn
(concat rtn char
)))
1143 (setq text
(concat rtn text
)
1145 (while (string-match "--" text
)
1146 (setq text
(replace-match "-{}-" t t text
)))
1148 ;; Else use format string.
1149 (t (format fmt text
)))))
1151 (defun org-latex--delayed-footnotes-definitions (element info
)
1152 "Return footnotes definitions in ELEMENT as a string.
1154 INFO is a plist used as a communication channel.
1156 Footnotes definitions are returned within \"\\footnotetxt{}\"
1159 This function is used within constructs that don't support
1160 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1161 \"\\footnotemark\" is used within the construct and the function
1162 just outside of it."
1166 "\\footnotetext[%s]{%s}"
1167 (org-export-get-footnote-number ref info
)
1170 (org-export-get-footnote-definition ref info
) info
))))
1171 ;; Find every footnote reference in ELEMENT.
1173 search-refs
; For byte-compiler.
1177 ;; Return a list of all footnote references never seen
1179 (org-element-map data
'footnote-reference
1181 (when (org-export-footnote-first-reference-p ref info
)
1183 (when (eq (org-element-property :type ref
) 'standard
)
1184 (funcall search-refs
1185 (org-export-get-footnote-definition ref info
)))))
1187 (reverse all-refs
)))))
1188 (funcall search-refs element
))
1191 (defun org-latex--translate (s info
)
1192 "Translate string S according to specified language.
1193 INFO is a plist used as a communication channel."
1194 (org-export-translate s
:latex info
))
1200 (defun org-latex-template (contents info
)
1201 "Return complete document string after LaTeX conversion.
1202 CONTENTS is the transcoded contents string. INFO is a plist
1203 holding export options."
1204 (let ((title (org-export-data (plist-get info
:title
) info
)))
1207 (and (plist-get info
:time-stamp-file
)
1208 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1209 ;; Document class and packages.
1210 (let* ((class (plist-get info
:latex-class
))
1211 (class-options (plist-get info
:latex-class-options
))
1212 (header (nth 1 (assoc class org-latex-classes
)))
1213 (document-class-string
1214 (and (stringp header
)
1215 (if (not class-options
) header
1216 (replace-regexp-in-string
1217 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1218 class-options header t nil
1)))))
1219 (if (not document-class-string
)
1220 (user-error "Unknown LaTeX class `%s'" class
)
1221 (org-latex-guess-babel-language
1222 (org-latex-guess-inputenc
1223 (org-element-normalize-string
1224 (org-splice-latex-header
1225 document-class-string
1226 org-latex-default-packages-alist
1227 org-latex-packages-alist nil
1228 (concat (org-element-normalize-string
1229 (plist-get info
:latex-header
))
1230 (plist-get info
:latex-header-extra
)))))
1232 ;; Possibly limit depth for headline numbering.
1233 (let ((sec-num (plist-get info
:section-numbers
)))
1234 (when (integerp sec-num
)
1235 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
1237 (let ((author (and (plist-get info
:with-author
)
1238 (let ((auth (plist-get info
:author
)))
1239 (and auth
(org-export-data auth info
)))))
1240 (email (and (plist-get info
:with-email
)
1241 (org-export-data (plist-get info
:email
) info
))))
1242 (cond ((and author email
(not (string= "" email
)))
1243 (format "\\author{%s\\thanks{%s}}\n" author email
))
1244 ((or author email
) (format "\\author{%s}\n" (or author email
)))))
1246 (let ((date (and (plist-get info
:with-date
) (org-export-get-date info
))))
1247 (format "\\date{%s}\n" (org-export-data date info
)))
1249 (format "\\title{%s}\n" title
)
1250 ;; Hyperref options.
1251 (format-spec (plist-get info
:latex-hyperref-template
)
1253 ?k
(or (plist-get info
:keywords
) "")
1254 ?d
(or (plist-get info
:description
)"")
1255 ?c
(if (plist-get info
:with-creator
)
1256 (plist-get info
:creator
)
1259 "\\begin{document}\n\n"
1261 (org-element-normalize-string
1262 (cond ((string= "" title
) nil
)
1263 ((not (stringp org-latex-title-command
)) nil
)
1264 ((string-match "\\(?:[^%]\\|^\\)%s"
1265 org-latex-title-command
)
1266 (format org-latex-title-command title
))
1267 (t org-latex-title-command
)))
1268 ;; Table of contents.
1269 (let ((depth (plist-get info
:with-toc
)))
1271 (concat (when (wholenump depth
)
1272 (format "\\setcounter{tocdepth}{%d}\n" depth
))
1273 org-latex-toc-command
)))
1277 (let ((creator-info (plist-get info
:with-creator
)))
1279 ((not creator-info
) "")
1280 ((eq creator-info
'comment
)
1281 (format "%% %s\n" (plist-get info
:creator
)))
1282 (t (concat (plist-get info
:creator
) "\n"))))
1284 "\\end{document}")))
1288 ;;; Transcode Functions
1292 (defun org-latex-bold (bold contents info
)
1293 "Transcode BOLD from Org to LaTeX.
1294 CONTENTS is the text with bold markup. INFO is a plist holding
1295 contextual information."
1296 (org-latex--text-markup contents
'bold
))
1301 (defun org-latex-center-block (center-block contents info
)
1302 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1303 CONTENTS holds the contents of the center block. INFO is a plist
1304 holding contextual information."
1305 (org-latex--wrap-label
1307 (format "\\begin{center}\n%s\\end{center}" contents
)))
1312 (defun org-latex-clock (clock contents info
)
1313 "Transcode a CLOCK element from Org to LaTeX.
1314 CONTENTS is nil. INFO is a plist holding contextual
1318 (format "\\textbf{%s} " org-clock-string
)
1319 (format org-latex-inactive-timestamp-format
1320 (concat (org-translate-time
1321 (org-element-property :raw-value
1322 (org-element-property :value clock
)))
1323 (let ((time (org-element-property :duration clock
)))
1324 (and time
(format " (%s)" time
)))))
1330 (defun org-latex-code (code contents info
)
1331 "Transcode a CODE object from Org to LaTeX.
1332 CONTENTS is nil. INFO is a plist used as a communication
1334 (org-latex--text-markup (org-element-property :value code
) 'code
))
1339 (defun org-latex-drawer (drawer contents info
)
1340 "Transcode a DRAWER element from Org to LaTeX.
1341 CONTENTS holds the contents of the block. INFO is a plist
1342 holding contextual information."
1343 (let* ((name (org-element-property :drawer-name drawer
))
1344 (output (funcall org-latex-format-drawer-function
1346 (org-latex--wrap-label drawer output
)))
1351 (defun org-latex-dynamic-block (dynamic-block contents info
)
1352 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1353 CONTENTS holds the contents of the block. INFO is a plist
1354 holding contextual information. See `org-export-data'."
1355 (org-latex--wrap-label dynamic-block contents
))
1360 (defun org-latex-entity (entity contents info
)
1361 "Transcode an ENTITY object from Org to LaTeX.
1362 CONTENTS are the definition itself. INFO is a plist holding
1363 contextual information."
1364 (org-element-property :latex entity
))
1369 (defun org-latex-example-block (example-block contents info
)
1370 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1371 CONTENTS is nil. INFO is a plist holding contextual
1373 (when (org-string-nw-p (org-element-property :value example-block
))
1374 (org-latex--wrap-label
1376 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1377 (org-export-format-code-default example-block info
)))))
1382 (defun org-latex-export-block (export-block contents info
)
1383 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1384 CONTENTS is nil. INFO is a plist holding contextual information."
1385 (when (member (org-element-property :type export-block
) '("LATEX" "TEX"))
1386 (org-remove-indentation (org-element-property :value export-block
))))
1391 (defun org-latex-export-snippet (export-snippet contents info
)
1392 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1393 CONTENTS is nil. INFO is a plist holding contextual information."
1394 (when (eq (org-export-snippet-backend export-snippet
) 'latex
)
1395 (org-element-property :value export-snippet
)))
1400 (defun org-latex-fixed-width (fixed-width contents info
)
1401 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1402 CONTENTS is nil. INFO is a plist holding contextual information."
1403 (org-latex--wrap-label
1405 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1406 (org-remove-indentation
1407 (org-element-property :value fixed-width
)))))
1410 ;;;; Footnote Reference
1412 (defun org-latex-footnote-reference (footnote-reference contents info
)
1413 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1414 CONTENTS is nil. INFO is a plist holding contextual information."
1416 ;; Insert separator between two footnotes in a row.
1417 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1418 (when (eq (org-element-type prev
) 'footnote-reference
)
1419 org-latex-footnote-separator
))
1421 ;; Use \footnotemark if the footnote has already been defined.
1422 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1423 (format "\\footnotemark[%s]{}"
1424 (org-export-get-footnote-number footnote-reference info
)))
1425 ;; Use \footnotemark if reference is within another footnote
1426 ;; reference, footnote definition or table cell.
1427 ((loop for parent in
(org-export-get-genealogy footnote-reference
)
1428 thereis
(memq (org-element-type parent
)
1429 '(footnote-reference footnote-definition table-cell
)))
1431 ;; Otherwise, define it with \footnote command.
1433 (let ((def (org-export-get-footnote-definition footnote-reference info
)))
1435 (format "\\footnote{%s}" (org-trim (org-export-data def info
)))
1436 ;; Retrieve all footnote references within the footnote and
1437 ;; add their definition after it, since LaTeX doesn't support
1439 (org-latex--delayed-footnotes-definitions def info
)))))))
1444 (defun org-latex-headline (headline contents info
)
1445 "Transcode a HEADLINE element from Org to LaTeX.
1446 CONTENTS holds the contents of the headline. INFO is a plist
1447 holding contextual information."
1448 (unless (org-element-property :footnote-section-p headline
)
1449 (let* ((class (plist-get info
:latex-class
))
1450 (level (org-export-get-relative-level headline info
))
1451 (numberedp (org-export-numbered-headline-p headline info
))
1452 (class-sectioning (assoc class org-latex-classes
))
1453 ;; Section formatting will set two placeholders: one for
1454 ;; the title and the other for the contents.
1456 (let ((sec (if (functionp (nth 2 class-sectioning
))
1457 (funcall (nth 2 class-sectioning
) level numberedp
)
1458 (nth (1+ level
) class-sectioning
))))
1460 ;; No section available for that LEVEL.
1462 ;; Section format directly returned by a function. Add
1463 ;; placeholder for contents.
1464 ((stringp sec
) (concat sec
"\n%s"))
1465 ;; (numbered-section . unnumbered-section)
1466 ((not (consp (cdr sec
)))
1467 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
1468 ;; (numbered-open numbered-close)
1470 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
1471 ;; (num-in num-out no-num-in no-num-out)
1473 (if numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))
1474 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
1475 ;; Create a temporary export back-end that hard-codes
1476 ;; "\underline" within "\section" and alike.
1478 (org-export-create-backend
1481 '((underline .
(lambda (o c i
) (format "\\underline{%s}" c
))))))
1483 (org-export-data-with-backend
1484 (org-element-property :title headline
) section-back-end info
))
1486 (and (plist-get info
:with-todo-keywords
)
1487 (let ((todo (org-element-property :todo-keyword headline
)))
1488 (and todo
(org-export-data todo info
)))))
1489 (todo-type (and todo
(org-element-property :todo-type headline
)))
1490 (tags (and (plist-get info
:with-tags
)
1491 (org-export-get-tags headline info
)))
1492 (priority (and (plist-get info
:with-priority
)
1493 (org-element-property :priority headline
)))
1494 ;; Create the headline text along with a no-tag version.
1495 ;; The latter is required to remove tags from toc.
1496 (full-text (funcall org-latex-format-headline-function
1497 todo todo-type priority text tags
))
1498 ;; Associate \label to the headline for internal links.
1501 (and (plist-get info
:latex-custom-id-labels
)
1502 (org-element-property :CUSTOM_ID headline
))))
1503 (if custom-label
(format "\\label{%s}\n" custom-label
)
1504 (format "\\label{sec-%s}\n"
1507 (org-export-get-headline-number headline info
)
1510 (make-string (org-element-property :pre-blank headline
) 10)))
1511 (if (or (not section-fmt
) (org-export-low-level-p headline info
))
1512 ;; This is a deep sub-tree: export it as a list item. Also
1513 ;; export as items headlines for which no section format has
1515 (let ((low-level-body
1517 ;; If headline is the first sibling, start a list.
1518 (when (org-export-first-sibling-p headline info
)
1519 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
1522 (and full-text
(org-string-match-p "\\`[ \t]*\\[" full-text
)
1528 ;; If headline is not the last sibling simply return
1529 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1531 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1532 (replace-regexp-in-string
1534 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
1536 ;; This is a standard headline. Export it as a section. Add
1537 ;; an alternative heading when possible, and when this is not
1538 ;; identical to the usual heading.
1540 (funcall org-latex-format-headline-function
1541 todo todo-type priority
1542 (org-export-data-with-backend
1543 (org-export-get-alt-title headline info
)
1544 section-back-end info
)
1545 (and (eq (plist-get info
:with-tags
) t
) tags
))))
1546 (if (and numberedp opt-title
1547 (not (equal opt-title full-text
))
1548 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt
))
1549 (format (replace-match "\\1[%s]" nil nil section-fmt
1)
1550 ;; Replace square brackets with parenthesis
1551 ;; since square brackets are not supported in
1552 ;; optional arguments.
1553 (replace-regexp-in-string
1554 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title
))
1556 (concat headline-label pre-blanks contents
))
1557 ;; Impossible to add an alternative heading. Fallback to
1558 ;; regular sectioning format string.
1559 (format section-fmt full-text
1560 (concat headline-label pre-blanks contents
))))))))
1562 (defun org-latex-format-headline-default-function
1563 (todo todo-type priority text tags
)
1564 "Default format function for a headline.
1565 See `org-latex-format-headline-function' for details."
1567 (and todo
(format "{\\bfseries\\sffamily %s} " todo
))
1568 (and priority
(format "\\framebox{\\#%c} " priority
))
1571 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags
":")))))
1574 ;;;; Horizontal Rule
1576 (defun org-latex-horizontal-rule (horizontal-rule contents info
)
1577 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1578 CONTENTS is nil. INFO is a plist holding contextual information."
1579 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule
))
1580 (prev (org-export-get-previous-element horizontal-rule info
)))
1582 ;; Make sure the rule doesn't start at the end of the current
1583 ;; line by separating it with a blank line from previous element.
1585 (let ((prev-blank (org-element-property :post-blank prev
)))
1586 (or (not prev-blank
) (zerop prev-blank
))))
1588 (org-latex--wrap-label
1590 (format "\\rule{%s}{%s}"
1591 (or (plist-get attr
:width
) "\\linewidth")
1592 (or (plist-get attr
:thickness
) "0.5pt"))))))
1595 ;;;; Inline Src Block
1597 (defun org-latex-inline-src-block (inline-src-block contents info
)
1598 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1599 CONTENTS holds the contents of the item. INFO is a plist holding
1600 contextual information."
1601 (let* ((code (org-element-property :value inline-src-block
))
1602 (separator (org-latex--find-verb-separator code
)))
1604 ;; Do not use a special package: transcode it verbatim.
1605 ((not org-latex-listings
)
1606 (concat "\\verb" separator code separator
))
1607 ;; Use minted package.
1608 ((eq org-latex-listings
'minted
)
1609 (let* ((org-lang (org-element-property :language inline-src-block
))
1610 (mint-lang (or (cadr (assq (intern org-lang
)
1611 org-latex-minted-langs
))
1612 (downcase org-lang
)))
1613 (options (org-latex--make-option-string
1614 org-latex-minted-options
)))
1615 (concat (format "\\mint%s{%s}"
1616 (if (string= options
"") "" (format "[%s]" options
))
1618 separator code separator
)))
1619 ;; Use listings package.
1621 ;; Maybe translate language's name.
1622 (let* ((org-lang (org-element-property :language inline-src-block
))
1623 (lst-lang (or (cadr (assq (intern org-lang
)
1624 org-latex-listings-langs
))
1626 (options (org-latex--make-option-string
1627 (append org-latex-listings-options
1628 `(("language" ,lst-lang
))))))
1629 (concat (format "\\lstinline[%s]" options
)
1630 separator code separator
))))))
1635 (defun org-latex-inlinetask (inlinetask contents info
)
1636 "Transcode an INLINETASK element from Org to LaTeX.
1637 CONTENTS holds the contents of the block. INFO is a plist
1638 holding contextual information."
1639 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1640 (todo (and (plist-get info
:with-todo-keywords
)
1641 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1642 (and todo
(org-export-data todo info
)))))
1643 (todo-type (org-element-property :todo-type inlinetask
))
1644 (tags (and (plist-get info
:with-tags
)
1645 (org-export-get-tags inlinetask info
)))
1646 (priority (and (plist-get info
:with-priority
)
1647 (org-element-property :priority inlinetask
))))
1648 ;; If `org-latex-format-inlinetask-function' is provided, call it
1649 ;; with appropriate arguments.
1650 (if (not (eq org-latex-format-inlinetask-function
'ignore
))
1651 (funcall org-latex-format-inlinetask-function
1652 todo todo-type priority title tags contents
)
1653 ;; Otherwise, use a default template.
1654 (org-latex--wrap-label
1658 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1659 (when priority
(format "\\framebox{\\#%c} " priority
))
1661 (when tags
(format "\\hfill{}\\textsc{:%s:}"
1662 (mapconcat #'identity tags
":"))))))
1663 (concat "\\begin{center}\n"
1665 "\\begin{minipage}[c]{.6\\textwidth}\n"
1667 (and (org-string-nw-p contents
)
1668 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents
))
1671 "\\end{center}"))))))
1676 (defun org-latex-italic (italic contents info
)
1677 "Transcode ITALIC from Org to LaTeX.
1678 CONTENTS is the text with italic markup. INFO is a plist holding
1679 contextual information."
1680 (org-latex--text-markup contents
'italic
))
1685 (defun org-latex-item (item contents info
)
1686 "Transcode an ITEM element from Org to LaTeX.
1687 CONTENTS holds the contents of the item. INFO is a plist holding
1688 contextual information."
1690 (let ((count (org-element-property :counter item
))
1692 ;; Determine level of current item to determine the
1693 ;; correct LaTeX counter to use (enumi, enumii...).
1694 (let ((parent item
) (level 0))
1695 (while (memq (org-element-type
1696 (setq parent
(org-export-get-parent parent
)))
1698 (when (and (eq (org-element-type parent
) 'plain-list
)
1699 (eq (org-element-property :type parent
)
1705 (format "\\setcounter{enum%s}{%s}\n"
1706 (nth (1- level
) '("i" "ii" "iii" "iv"))
1708 (checkbox (case (org-element-property :checkbox item
)
1709 (on "$\\boxtimes$ ")
1711 (trans "$\\boxminus$ ")))
1712 (tag (let ((tag (org-element-property :tag item
)))
1713 ;; Check-boxes must belong to the tag.
1714 (and tag
(format "[{%s}] "
1716 (org-export-data tag info
)))))))
1721 (checkbox (concat " " checkbox
))
1722 ;; Without a tag or a check-box, if CONTENTS starts with
1723 ;; an opening square bracket, add "\relax" to "\item",
1724 ;; unless the brackets comes from an initial export
1725 ;; snippet (i.e. it is inserted willingly by the user).
1727 (org-string-match-p "\\`[ \t]*\\[" contents
)
1728 (not (let ((e (car (org-element-contents item
))))
1729 (and (eq (org-element-type e
) 'paragraph
)
1730 (let ((o (car (org-element-contents e
))))
1731 (and (eq (org-element-type o
) 'export-snippet
)
1732 (eq (org-export-snippet-backend o
)
1736 (and contents
(org-trim contents
))
1737 ;; If there are footnotes references in tag, be sure to
1738 ;; add their definition at the end of the item. This
1739 ;; workaround is necessary since "\footnote{}" command is
1740 ;; not supported in tags.
1742 (org-latex--delayed-footnotes-definitions
1743 (org-element-property :tag item
) info
)))))
1748 (defun org-latex-keyword (keyword contents info
)
1749 "Transcode a KEYWORD element from Org to LaTeX.
1750 CONTENTS is nil. INFO is a plist holding contextual information."
1751 (let ((key (org-element-property :key keyword
))
1752 (value (org-element-property :value keyword
)))
1754 ((string= key
"LATEX") value
)
1755 ((string= key
"INDEX") (format "\\index{%s}" value
))
1756 ((string= key
"TOC")
1757 (let ((value (downcase value
)))
1759 ((string-match "\\<headlines\\>" value
)
1760 (let ((depth (or (and (string-match "[0-9]+" value
)
1761 (string-to-number (match-string 0 value
)))
1762 (plist-get info
:with-toc
))))
1764 (when (wholenump depth
)
1765 (format "\\setcounter{tocdepth}{%s}\n" depth
))
1766 "\\tableofcontents")))
1767 ((string= "tables" value
) "\\listoftables")
1768 ((string= "listings" value
)
1770 ((eq org-latex-listings
'minted
) "\\listoflistings")
1771 (org-latex-listings "\\lstlistoflistings")
1772 ;; At the moment, src blocks with a caption are wrapped
1773 ;; into a figure environment.
1774 (t "\\listoffigures")))))))))
1777 ;;;; Latex Environment
1779 (defun org-latex-latex-environment (latex-environment contents info
)
1780 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1781 CONTENTS is nil. INFO is a plist holding contextual information."
1782 (when (plist-get info
:with-latex
)
1783 (let ((label (org-element-property :name latex-environment
))
1784 (value (org-remove-indentation
1785 (org-element-property :value latex-environment
))))
1786 (if (not (org-string-nw-p label
)) value
1787 ;; Environment is labeled: label must be within the environment
1788 ;; (otherwise, a reference pointing to that element will count
1789 ;; the section instead).
1792 (goto-char (point-min))
1795 (format "\\label{%s}\n" (org-export-solidify-link-text label
)))
1796 (buffer-string))))))
1801 (defun org-latex-latex-fragment (latex-fragment contents info
)
1802 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1803 CONTENTS is nil. INFO is a plist holding contextual information."
1804 (let ((value (org-element-property :value latex-fragment
)))
1805 ;; Trim math markers since the fragment is enclosed within
1806 ;; a latex-math-block object anyway.
1807 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value
)
1808 (match-string 2 value
))
1809 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value
)
1810 (match-string 1 value
))
1816 (defun org-latex-line-break (line-break contents info
)
1817 "Transcode a LINE-BREAK object from Org to LaTeX.
1818 CONTENTS is nil. INFO is a plist holding contextual information."
1824 (defun org-latex--inline-image (link info
)
1825 "Return LaTeX code for an inline image.
1826 LINK is the link pointing to the inline image. INFO is a plist
1827 used as a communication channel."
1828 (let* ((parent (org-export-get-parent-element link
))
1829 (path (let ((raw-path (org-element-property :path link
)))
1830 (if (not (file-name-absolute-p raw-path
)) raw-path
1831 (expand-file-name raw-path
))))
1832 (filetype (file-name-extension path
))
1833 (caption (org-latex--caption/label-string parent info
))
1834 ;; Retrieve latex attributes from the element around.
1835 (attr (org-export-read-attribute :attr_latex parent
))
1836 (float (let ((float (plist-get attr
:float
)))
1837 (cond ((and (not float
) (plist-member attr
:float
)) nil
)
1838 ((string= float
"wrap") 'wrap
)
1839 ((string= float
"sideways") 'sideways
)
1840 ((string= float
"multicolumn") 'multicolumn
)
1842 (org-element-property :caption parent
)
1843 (org-string-nw-p (plist-get attr
:caption
)))
1846 (let ((place (plist-get attr
:placement
)))
1847 (cond (place (format "%s" place
))
1848 ((eq float
'wrap
) "{l}{0.5\\textwidth}")
1850 (format "[%s]" org-latex-default-figure-position
))
1852 (comment-include (if (plist-get attr
:comment-include
) "%" ""))
1853 ;; It is possible to specify width and height in the
1854 ;; ATTR_LATEX line, and also via default variables.
1855 (width (cond ((plist-get attr
:width
))
1856 ((plist-get attr
:height
) "")
1857 ((eq float
'wrap
) "0.48\\textwidth")
1858 (t org-latex-image-default-width
)))
1859 (height (cond ((plist-get attr
:height
))
1860 ((or (plist-get attr
:width
)
1861 (memq float
'(figure wrap
))) "")
1862 (t org-latex-image-default-height
)))
1863 (options (let ((opt (or (plist-get attr
:options
)
1864 org-latex-image-default-option
)))
1865 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt
)) opt
1866 (match-string 1 opt
))))
1868 (if (member filetype
'("tikz" "pgf"))
1870 ;; - use \input to read in image file.
1871 ;; - if options are present, wrap in a tikzpicture environment.
1872 ;; - if width or height are present, use \resizebox to change
1875 (setq image-code
(format "\\input{%s}" path
))
1876 (when (org-string-nw-p options
)
1878 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1881 (when (or (org-string-nw-p width
) (org-string-nw-p height
))
1882 (setq image-code
(format "\\resizebox{%s}{%s}{%s}"
1883 (if (org-string-nw-p width
) width
"!")
1884 (if (org-string-nw-p height
) height
"!")
1886 ;; For other images:
1887 ;; - add width and height to options.
1888 ;; - include the image with \includegraphics.
1889 (when (org-string-nw-p width
)
1890 (setq options
(concat options
",width=" width
)))
1891 (when (org-string-nw-p height
)
1892 (setq options
(concat options
",height=" height
)))
1894 (format "\\includegraphics%s{%s}"
1895 (cond ((not (org-string-nw-p options
)) "")
1896 ((= (aref options
0) ?
,)
1897 (format "[%s]"(substring options
1)))
1898 (t (format "[%s]" options
)))
1900 (when (equal filetype
"svg")
1901 (setq image-code
(replace-regexp-in-string "^\\\\includegraphics"
1905 (setq image-code
(replace-regexp-in-string "\\.svg}"
1909 ;; Return proper string, depending on FLOAT.
1911 (wrap (format "\\begin{wrapfigure}%s
1914 %s\\end{wrapfigure}" placement comment-include image-code caption
))
1915 (sideways (format "\\begin{sidewaysfigure}
1918 %s\\end{sidewaysfigure}" comment-include image-code caption
))
1919 (multicolumn (format "\\begin{figure*}%s
1922 %s\\end{figure*}" placement comment-include image-code caption
))
1923 (figure (format "\\begin{figure}%s
1926 %s\\end{figure}" placement comment-include image-code caption
))
1927 (otherwise image-code
))))
1929 (defun org-latex-link (link desc info
)
1930 "Transcode a LINK object from Org to LaTeX.
1932 DESC is the description part of the link, or the empty string.
1933 INFO is a plist holding contextual information. See
1935 (let* ((type (org-element-property :type link
))
1936 (raw-path (replace-regexp-in-string
1937 "%" "\\%" (org-element-property :path link
) nil t
))
1938 ;; Ensure DESC really exists, or set it to nil.
1939 (desc (and (not (string= desc
"")) desc
))
1940 (imagep (org-export-inline-image-p
1941 link org-latex-inline-image-rules
))
1943 ((member type
'("http" "https" "ftp" "mailto"))
1944 (concat type
":" raw-path
))
1945 ((and (string= type
"file") (file-name-absolute-p raw-path
))
1946 (concat "file:" raw-path
))
1951 (imagep (org-latex--inline-image link info
))
1952 ;; Radio link: Transcode target's contents and use them as link's
1954 ((string= type
"radio")
1955 (let ((destination (org-export-resolve-radio-link link info
)))
1957 (format "\\hyperref[%s]{%s}"
1958 (org-export-solidify-link-text
1959 (org-element-property :value destination
))
1961 ;; Links pointing to a headline: Find destination and build
1962 ;; appropriate referencing command.
1963 ((member type
'("custom-id" "fuzzy" "id"))
1964 (let ((destination (if (string= type
"fuzzy")
1965 (org-export-resolve-fuzzy-link link info
)
1966 (org-export-resolve-id-link link info
))))
1967 (case (org-element-type destination
)
1968 ;; Id link points to an external file.
1970 (if desc
(format "\\href{%s}{%s}" destination desc
)
1971 (format "\\url{%s}" destination
)))
1972 ;; Fuzzy link points nowhere.
1974 (format org-latex-link-with-unknown-path-format
1977 (org-element-property :raw-link link
) info
))))
1978 ;; LINK points to a headline. If headlines are numbered
1979 ;; and the link has no description, display headline's
1980 ;; number. Otherwise, display description or headline's
1983 (let* ((custom-label
1984 (and (plist-get info
:latex-custom-id-labels
)
1985 (org-element-property :CUSTOM_ID destination
)))
1992 (org-export-get-headline-number destination info
)
1994 (if (and (plist-get info
:section-numbers
) (not desc
))
1995 (format "\\ref{%s}" label
)
1996 (format "\\hyperref[%s]{%s}" label
1999 (org-element-property :title destination
) info
))))))
2000 ;; Fuzzy link points to a target. Do as above.
2002 (let ((path (org-export-solidify-link-text path
)))
2003 (if (not desc
) (format "\\ref{%s}" path
)
2004 (format "\\hyperref[%s]{%s}" path desc
)))))))
2005 ;; Coderef: replace link with the reference name or the
2006 ;; equivalent line number.
2007 ((string= type
"coderef")
2008 (format (org-export-get-coderef-format path desc
)
2009 (org-export-resolve-coderef path info
)))
2010 ;; Link type is handled by a special function.
2011 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
2012 (funcall protocol
(org-link-unescape path
) desc
'latex
))
2013 ;; External link with a description part.
2014 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
2015 ;; External link without a description part.
2016 (path (format "\\url{%s}" path
))
2017 ;; No path, only description. Try to do something useful.
2018 (t (format org-latex-link-with-unknown-path-format desc
)))))
2023 (defun org-latex-node-property (node-property contents info
)
2024 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2025 CONTENTS is nil. INFO is a plist holding contextual
2028 (org-element-property :key node-property
)
2029 (let ((value (org-element-property :value node-property
)))
2030 (if value
(concat " " value
) ""))))
2035 (defun org-latex-paragraph (paragraph contents info
)
2036 "Transcode a PARAGRAPH element from Org to LaTeX.
2037 CONTENTS is the contents of the paragraph, as a string. INFO is
2038 the plist used as a communication channel."
2044 (defun org-latex-plain-list (plain-list contents info
)
2045 "Transcode a PLAIN-LIST element from Org to LaTeX.
2046 CONTENTS is the contents of the list. INFO is a plist holding
2047 contextual information."
2048 (let* ((type (org-element-property :type plain-list
))
2049 (attr (org-export-read-attribute :attr_latex plain-list
))
2050 (latex-type (let ((env (plist-get attr
:environment
)))
2051 (cond (env (format "%s" env
))
2052 ((eq type
'ordered
) "enumerate")
2053 ((eq type
'descriptive
) "description")
2055 (org-latex--wrap-label
2057 (format "\\begin{%s}%s\n%s\\end{%s}"
2059 (or (plist-get attr
:options
) "")
2066 (defun org-latex-plain-text (text info
)
2067 "Transcode a TEXT string from Org to LaTeX.
2068 TEXT is the string to transcode. INFO is a plist holding
2069 contextual information."
2070 (let ((specialp (plist-get info
:with-special-strings
))
2072 ;; Protect %, #, &, $, _, { and }.
2073 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output
)
2076 (format "\\%s" (match-string 2 output
)) nil t output
2)))
2079 (replace-regexp-in-string
2080 "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil
2))
2081 ;; Protect \. If special strings are used, be careful not to
2082 ;; protect "\" in "\-" constructs.
2083 (let ((symbols (if specialp
"-%$#&{}^_\\" "%$#&{}^_\\")))
2085 (replace-regexp-in-string
2086 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols
)
2087 "$\\backslash$" output nil t
1)))
2090 (replace-regexp-in-string
2091 "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t
2))
2092 ;; Activate smart quotes. Be sure to provide original TEXT string
2093 ;; since OUTPUT may have been modified.
2094 (when (plist-get info
:with-smart-quotes
)
2095 (setq output
(org-export-activate-smart-quotes output
:latex info text
)))
2096 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
2097 (let ((case-fold-search nil
)
2099 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start
)
2100 (setq output
(replace-match
2101 (format "\\%s{}" (match-string 1 output
)) nil t output
)
2102 start
(match-end 0))))
2103 ;; Convert special strings.
2106 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t
)))
2107 ;; Handle break preservation if required.
2108 (when (plist-get info
:preserve-breaks
)
2109 (setq output
(replace-regexp-in-string
2110 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output
)))
2117 (defun org-latex-planning (planning contents info
)
2118 "Transcode a PLANNING element from Org to LaTeX.
2119 CONTENTS is nil. INFO is a plist holding contextual
2127 (let ((closed (org-element-property :closed planning
)))
2130 (format "\\textbf{%s} " org-closed-string
)
2131 (format org-latex-inactive-timestamp-format
2133 (org-element-property :raw-value closed
))))))
2134 (let ((deadline (org-element-property :deadline planning
)))
2137 (format "\\textbf{%s} " org-deadline-string
)
2138 (format org-latex-active-timestamp-format
2140 (org-element-property :raw-value deadline
))))))
2141 (let ((scheduled (org-element-property :scheduled planning
)))
2144 (format "\\textbf{%s} " org-scheduled-string
)
2145 (format org-latex-active-timestamp-format
2147 (org-element-property :raw-value scheduled
))))))))
2152 ;;;; Property Drawer
2154 (defun org-latex-property-drawer (property-drawer contents info
)
2155 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2156 CONTENTS holds the contents of the drawer. INFO is a plist
2157 holding contextual information."
2158 (and (org-string-nw-p contents
)
2159 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents
)))
2162 ;;;; Pseudo Object: LaTeX Math Block
2164 (defun org-latex--wrap-latex-math-block (data info
)
2165 "Merge contiguous math objects in a pseudo-object container.
2166 DATA is a parse tree or a secondary string. INFO is a plist
2167 containing export options. Modify DATA by side-effect and return it."
2168 (let ((valid-object-p
2170 ;; Non-nil when OBJ can be added to the latex math block.
2172 (case (org-element-type obj
)
2173 (entity (org-element-property :latex-math-p obj
))
2175 (let ((value (org-element-property :value obj
)))
2176 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value
)
2177 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value
))))
2178 ((subscript superscript
) t
))))))
2179 (org-element-map data
'(entity latex-fragment subscript superscript
)
2181 ;; Skip objects already wrapped.
2182 (when (and (not (eq (org-element-type
2183 (org-element-property :parent object
))
2185 (funcall valid-object-p object
))
2186 (let ((math-block (list 'latex-math-block nil
))
2187 (next-elements (org-export-get-next-element object info t
))
2189 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2190 (org-element-insert-before math-block object
)
2191 (org-element-extract-element object
)
2192 (org-element-adopt-elements math-block object
)
2193 (when (zerop (or (org-element-property :post-blank object
) 0))
2194 ;; MATH-BLOCK swallows consecutive math objects.
2196 (dolist (next next-elements
)
2197 (if (not (funcall valid-object-p next
)) (throw 'exit nil
)
2198 (org-element-extract-element next
)
2199 (org-element-adopt-elements math-block next
)
2200 ;; Eschew the case: \beta$x$ -> \(\betax\).
2201 (unless (memq (org-element-type next
)
2202 '(subscript superscript
))
2203 (org-element-put-property last
:post-blank
1))
2205 (when (> (or (org-element-property :post-blank next
) 0) 0)
2206 (throw 'exit nil
))))))
2207 (org-element-put-property
2208 math-block
:post-blank
(org-element-property :post-blank last
)))))
2209 info nil
'(subscript superscript latex-math-block
) t
)
2210 ;; Return updated DATA.
2213 (defun org-latex-math-block-tree-filter (tree backend info
)
2214 (org-latex--wrap-latex-math-block tree info
))
2216 (defun org-latex-math-block-options-filter (info backend
)
2217 (dolist (prop '(:author
:date
:title
) info
)
2218 (plist-put info prop
2219 (org-latex--wrap-latex-math-block (plist-get info prop
) info
))))
2221 (defun org-latex-math-block (math-block contents info
)
2222 "Transcode a MATH-BLOCK object from Org to LaTeX.
2223 CONTENTS is a string. INFO is a plist used as a communication
2225 (when (org-string-nw-p contents
)
2226 (format "\\(%s\\)" (org-trim contents
))))
2230 (defun org-latex-quote-block (quote-block contents info
)
2231 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2232 CONTENTS holds the contents of the block. INFO is a plist
2233 holding contextual information."
2234 (org-latex--wrap-label
2236 (format "\\begin{quote}\n%s\\end{quote}" contents
)))
2241 (defun org-latex-radio-target (radio-target text info
)
2242 "Transcode a RADIO-TARGET object from Org to LaTeX.
2243 TEXT is the text of the target. INFO is a plist holding
2244 contextual information."
2245 (format "\\label{%s}%s"
2246 (org-export-solidify-link-text
2247 (org-element-property :value radio-target
))
2253 (defun org-latex-section (section contents info
)
2254 "Transcode a SECTION element from Org to LaTeX.
2255 CONTENTS holds the contents of the section. INFO is a plist
2256 holding contextual information."
2262 (defun org-latex-special-block (special-block contents info
)
2263 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2264 CONTENTS holds the contents of the block. INFO is a plist
2265 holding contextual information."
2266 (let ((type (downcase (org-element-property :type special-block
)))
2267 (opt (org-export-read-attribute :attr_latex special-block
:options
)))
2268 (concat (format "\\begin{%s}%s\n" type
(or opt
""))
2269 ;; Insert any label or caption within the block
2270 ;; (otherwise, a reference pointing to that element will
2271 ;; count the section instead).
2272 (org-latex--caption/label-string special-block info
)
2274 (format "\\end{%s}" type
))))
2279 (defun org-latex-src-block (src-block contents info
)
2280 "Transcode a SRC-BLOCK element from Org to LaTeX.
2281 CONTENTS holds the contents of the item. INFO is a plist holding
2282 contextual information."
2283 (when (org-string-nw-p (org-element-property :value src-block
))
2284 (let* ((lang (org-element-property :language src-block
))
2285 (caption (org-element-property :caption src-block
))
2286 (label (org-element-property :name src-block
))
2287 (custom-env (and lang
2288 (cadr (assq (intern lang
)
2289 org-latex-custom-lang-environments
))))
2290 (num-start (case (org-element-property :number-lines src-block
)
2291 (continued (org-export-get-loc src-block info
))
2293 (retain-labels (org-element-property :retain-labels src-block
))
2294 (attributes (org-export-read-attribute :attr_latex src-block
))
2295 (float (plist-get attributes
:float
)))
2297 ;; Case 1. No source fontification.
2298 ((not org-latex-listings
)
2299 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2301 (cond ((and (not float
) (plist-member attributes
:float
)) "%s")
2302 ((string= "multicolumn" float
)
2303 (format "\\begin{figure*}[%s]\n%%s%s\n\\end{figure*}"
2304 org-latex-default-figure-position
2307 (format "\\begin{figure}[H]\n%%s%s\n\\end{figure}"
2312 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2313 (org-export-format-code-default src-block info
))))))
2314 ;; Case 2. Custom environment.
2315 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2317 (org-export-format-code-default src-block info
)
2319 ;; Case 3. Use minted package.
2320 ((eq org-latex-listings
'minted
)
2321 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2323 (cond ((and (not float
) (plist-member attributes
:float
)) "%s")
2324 ((string= "multicolumn" float
)
2325 (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
2328 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2333 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2336 (org-latex--make-option-string
2337 (if (or (not num-start
)
2338 (assoc "linenos" org-latex-minted-options
))
2339 org-latex-minted-options
2342 ("firstnumber" ,(number-to-string (1+ num-start
))))
2343 org-latex-minted-options
)))
2344 (let ((local-options (plist-get attributes
:options
)))
2345 (and local-options
(concat "," local-options
))))
2347 (or (cadr (assq (intern lang
) org-latex-minted-langs
))
2350 (let* ((code-info (org-export-unravel-code src-block
))
2354 (org-split-string (car code-info
)
2356 (org-export-format-code
2358 (lambda (loc num ref
)
2362 ;; Ensure references are flushed to the right,
2363 ;; separated with 6 spaces from the widest line
2365 (concat (make-string (+ (- max-width
(length loc
)) 6)
2367 (format "(%s)" ref
)))))
2368 nil
(and retain-labels
(cdr code-info
)))))))
2370 (format float-env body
)))
2371 ;; Case 4. Use listings package.
2374 (or (cadr (assq (intern lang
) org-latex-listings-langs
)) lang
))
2377 (let ((main (org-export-get-caption src-block
))
2378 (secondary (org-export-get-caption src-block t
)))
2380 (format "{%s}" (org-export-data main info
))
2382 (org-export-data secondary info
)
2383 (org-export-data main info
)))))))
2389 (org-latex--make-option-string
2391 org-latex-listings-options
2393 ((and (not float
) (plist-member attributes
:float
)) nil
)
2394 ((string= "multicolumn" float
) '(("float" "*")))
2395 ((and float
(not (assoc "float" org-latex-listings-options
)))
2396 `(("float" ,org-latex-default-figure-position
))))
2397 `(("language" ,lst-lang
))
2398 (if label
`(("label" ,label
)) '(("label" " ")))
2399 (if caption-str
`(("caption" ,caption-str
)) '(("caption" " ")))
2400 (cond ((assoc "numbers" org-latex-listings-options
) nil
)
2401 ((not num-start
) '(("numbers" "none")))
2402 ((zerop num-start
) '(("numbers" "left")))
2403 (t `(("numbers" "left")
2405 ,(number-to-string (1+ num-start
))))))))
2406 (let ((local-options (plist-get attributes
:options
)))
2407 (and local-options
(concat "," local-options
)))))
2410 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2411 (let* ((code-info (org-export-unravel-code src-block
))
2415 (org-split-string (car code-info
) "\n")))))
2416 (org-export-format-code
2418 (lambda (loc num ref
)
2422 ;; Ensure references are flushed to the right,
2423 ;; separated with 6 spaces from the widest line of
2425 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2426 (format "(%s)" ref
)))))
2427 nil
(and retain-labels
(cdr code-info
))))))))))))
2430 ;;;; Statistics Cookie
2432 (defun org-latex-statistics-cookie (statistics-cookie contents info
)
2433 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2434 CONTENTS is nil. INFO is a plist holding contextual information."
2435 (replace-regexp-in-string
2436 "%" "\\%" (org-element-property :value statistics-cookie
) nil t
))
2441 (defun org-latex-strike-through (strike-through contents info
)
2442 "Transcode STRIKE-THROUGH from Org to LaTeX.
2443 CONTENTS is the text with strike-through markup. INFO is a plist
2444 holding contextual information."
2445 (org-latex--text-markup contents
'strike-through
))
2450 (defun org-latex--script-size (object info
)
2451 "Transcode a subscript or superscript object.
2452 OBJECT is an Org object. INFO is a plist used as a communication
2454 (let ((type (org-element-type object
))
2456 (org-element-map (org-element-contents object
)
2457 (cons 'plain-text org-element-all-objects
)
2459 (case (org-element-type obj
)
2460 ((entity latex-fragment
)
2461 (let ((data (org-trim (org-export-data obj info
))))
2463 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2467 (match-string 1 data
)
2468 (let ((blank (org-element-property :post-blank obj
)))
2469 (and blank
(> blank
0) "\\ "))))))
2472 (format "%s\\text{%s}" output
(org-export-data obj info
))))
2476 (org-export-data obj info
)
2477 (let ((blank (org-element-property :post-blank obj
)))
2478 (and blank
(> blank
0) "\\ ")))))))
2479 info nil org-element-recursive-objects
)
2480 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2482 (concat (if (eq (org-element-type object
) 'subscript
) "_" "^")
2483 (and (> (length output
) 1) "{")
2485 (and (> (length output
) 1) "}"))))
2487 (defun org-latex-subscript (subscript contents info
)
2488 "Transcode a SUBSCRIPT object from Org to LaTeX.
2489 CONTENTS is the contents of the object. INFO is a plist holding
2490 contextual information."
2491 (org-latex--script-size subscript info
))
2496 (defun org-latex-superscript (superscript contents info
)
2497 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2498 CONTENTS is the contents of the object. INFO is a plist holding
2499 contextual information."
2500 (org-latex--script-size superscript info
))
2505 ;; `org-latex-table' is the entry point for table transcoding. It
2506 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2507 ;; delegates the job to either `org-latex--table.el-table',
2508 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2509 ;; depending of the type of the table and the mode requested.
2511 ;; `org-latex--align-string' is a subroutine used to build alignment
2512 ;; string for Org tables.
2514 (defun org-latex-table (table contents info
)
2515 "Transcode a TABLE element from Org to LaTeX.
2516 CONTENTS is the contents of the table. INFO is a plist holding
2517 contextual information."
2518 (if (eq (org-element-property :type table
) 'table.el
)
2519 ;; "table.el" table. Convert it using appropriate tools.
2520 (org-latex--table.el-table table info
)
2521 (let ((type (or (org-export-read-attribute :attr_latex table
:mode
)
2522 org-latex-default-table-mode
)))
2524 ;; Case 1: Verbatim table.
2525 ((string= type
"verbatim")
2526 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2527 ;; Re-create table, without affiliated keywords.
2528 (org-trim (org-element-interpret-data
2529 `(table nil
,@(org-element-contents table
))
2530 org-latex-pseudo-objects
))))
2532 ((or (string= type
"math") (string= type
"inline-math"))
2533 (org-latex--math-table table info
))
2534 ;; Case 3: Standard table.
2535 (t (concat (org-latex--org-table table contents info
)
2536 ;; When there are footnote references within the
2537 ;; table, insert their definition just after it.
2538 (org-latex--delayed-footnotes-definitions table info
)))))))
2540 (defun org-latex--align-string (table info
)
2541 "Return an appropriate LaTeX alignment string.
2542 TABLE is the considered table. INFO is a plist used as
2543 a communication channel."
2544 (or (org-export-read-attribute :attr_latex table
:align
)
2546 ;; Extract column groups and alignment from first (non-rule)
2549 (org-element-map table
'table-row
2551 (and (eq (org-element-property :type row
) 'standard
) row
))
2555 (let ((borders (org-export-table-cell-borders cell info
)))
2556 ;; Check left border for the first cell only.
2557 (when (and (memq 'left borders
) (not align
))
2559 (push (case (org-export-table-cell-alignment cell info
)
2564 (when (memq 'right borders
) (push "|" align
))))
2566 (apply 'concat
(nreverse align
)))))
2568 (defun org-latex--org-table (table contents info
)
2569 "Return appropriate LaTeX code for an Org table.
2571 TABLE is the table type element to transcode. CONTENTS is its
2572 contents, as a string. INFO is a plist used as a communication
2575 This function assumes TABLE has `org' as its `:type' property and
2576 `table' as its `:mode' attribute."
2577 (let* ((caption (org-latex--caption/label-string table info
))
2578 (attr (org-export-read-attribute :attr_latex table
))
2579 ;; Determine alignment string.
2580 (alignment (org-latex--align-string table info
))
2581 ;; Determine environment for the table: longtable, tabular...
2582 (table-env (or (plist-get attr
:environment
)
2583 org-latex-default-table-environment
))
2584 ;; If table is a float, determine environment: table, table*
2585 ;; or sidewaystable.
2586 (float-env (unless (member table-env
'("longtable" "longtabu"))
2587 (let ((float (plist-get attr
:float
)))
2589 ((and (not float
) (plist-member attr
:float
)) nil
)
2590 ((or (string= float
"sidewaystable")
2591 (string= float
"sideways")) "sidewaystable")
2592 ((string= float
"multicolumn") "table*")
2594 (org-element-property :caption table
)
2595 (org-string-nw-p (plist-get attr
:caption
)))
2597 ;; Extract others display options.
2598 (fontsize (let ((font (plist-get attr
:font
)))
2599 (and font
(concat font
"\n"))))
2600 (width (plist-get attr
:width
))
2601 (spreadp (plist-get attr
:spread
))
2602 (placement (or (plist-get attr
:placement
)
2603 (format "[%s]" org-latex-default-figure-position
)))
2604 (centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2605 org-latex-tables-centered
)))
2606 ;; Prepare the final format string for the table.
2609 ((equal "longtable" table-env
)
2610 (concat (and fontsize
(concat "{" fontsize
))
2611 (format "\\begin{longtable}{%s}\n" alignment
)
2612 (and org-latex-table-caption-above
2613 (org-string-nw-p caption
)
2614 (concat caption
"\\\\\n"))
2616 (and (not org-latex-table-caption-above
)
2617 (org-string-nw-p caption
)
2618 (concat caption
"\\\\\n"))
2619 "\\end{longtable}\n"
2620 (and fontsize
"}")))
2622 ((equal "longtabu" table-env
)
2623 (concat (and fontsize
(concat "{" fontsize
))
2624 (format "\\begin{longtabu}%s{%s}\n"
2627 (if spreadp
"spread" "to") width
) "")
2629 (and org-latex-table-caption-above
2630 (org-string-nw-p caption
)
2631 (concat caption
"\\\\\n"))
2633 (and (not org-latex-table-caption-above
)
2634 (org-string-nw-p caption
)
2635 (concat caption
"\\\\\n"))
2637 (and fontsize
"}")))
2641 (concat (format "\\begin{%s}%s\n" float-env placement
)
2642 (if org-latex-table-caption-above caption
"")
2643 (when centerp
"\\centering\n")
2645 (centerp (concat "\\begin{center}\n" fontsize
))
2646 (fontsize (concat "{" fontsize
)))
2647 (cond ((equal "tabu" table-env
)
2648 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2650 (if spreadp
" spread %s " " to %s ")
2654 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2656 (if width
(format "{%s}" width
) "")
2662 (concat (if org-latex-table-caption-above
"" caption
)
2663 (format "\n\\end{%s}" float-env
)))
2664 (centerp "\n\\end{center}")
2665 (fontsize "}")))))))
2667 (defun org-latex--table.el-table
(table info
)
2668 "Return appropriate LaTeX code for a table.el table.
2670 TABLE is the table type element to transcode. INFO is a plist
2671 used as a communication channel.
2673 This function assumes TABLE has `table.el' as its `:type'
2676 ;; Ensure "*org-export-table*" buffer is empty.
2677 (with-current-buffer (get-buffer-create "*org-export-table*")
2679 (let ((output (with-temp-buffer
2680 (insert (org-element-property :value table
))
2682 (re-search-forward "^[ \t]*|[^|]" nil t
)
2683 (table-generate-source 'latex
"*org-export-table*")
2684 (with-current-buffer "*org-export-table*"
2685 (org-trim (buffer-string))))))
2686 (kill-buffer (get-buffer "*org-export-table*"))
2687 ;; Remove left out comments.
2688 (while (string-match "^%.*\n" output
)
2689 (setq output
(replace-match "" t t output
)))
2690 (let ((attr (org-export-read-attribute :attr_latex table
)))
2691 (when (plist-get attr
:rmlines
)
2692 ;; When the "rmlines" attribute is provided, remove all hlines
2693 ;; but the the one separating heading from the table body.
2694 (let ((n 0) (pos 0))
2695 (while (and (< (length output
) pos
)
2696 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
2698 (unless (= n
2) (setq output
(replace-match "" nil nil output
))))))
2699 (let ((centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2700 org-latex-tables-centered
)))
2701 (if (not centerp
) output
2702 (format "\\begin{center}\n%s\n\\end{center}" output
))))))
2704 (defun org-latex--math-table (table info
)
2705 "Return appropriate LaTeX code for a matrix.
2707 TABLE is the table type element to transcode. INFO is a plist
2708 used as a communication channel.
2710 This function assumes TABLE has `org' as its `:type' property and
2711 `inline-math' or `math' as its `:mode' attribute.."
2712 (let* ((caption (org-latex--caption/label-string table info
))
2713 (attr (org-export-read-attribute :attr_latex table
))
2714 (inlinep (equal (plist-get attr
:mode
) "inline-math"))
2715 (env (or (plist-get attr
:environment
)
2716 org-latex-default-table-environment
))
2720 ;; Ignore horizontal rules.
2721 (when (eq (org-element-property :type row
) 'standard
)
2722 ;; Return each cell unmodified.
2727 (org-element-interpret-data cell org-latex-pseudo-objects
)
2729 (org-element-map row
'table-cell
'identity info
) "&")
2730 (or (cdr (assoc env org-latex-table-matrix-macros
)) "\\\\")
2732 (org-element-map table
'table-row
'identity info
) ""))
2733 ;; Variables related to math clusters (contiguous math tables
2734 ;; of the same type).
2735 (mode (org-export-read-attribute :attr_latex table
:mode
))
2736 (prev (org-export-get-previous-element table info
))
2737 (next (org-export-get-next-element table info
))
2740 ;; Non-nil when TABLE has the same mode as current table.
2741 (string= (or (org-export-read-attribute :attr_latex table
:mode
)
2742 org-latex-default-table-mode
)
2745 ;; Opening string. If TABLE is in the middle of a table cluster,
2746 ;; do not insert any.
2748 (eq (org-element-type prev
) 'table
)
2749 (memq (org-element-property :post-blank prev
) '(0 nil
))
2750 (funcall same-mode-p prev
))
2753 ((org-string-nw-p caption
) (concat "\\begin{equation}\n" caption
))
2756 (or (plist-get attr
:math-prefix
) "")
2757 ;; Environment. Also treat special cases.
2758 (cond ((equal env
"array")
2759 (let ((align (org-latex--align-string table info
)))
2760 (format "\\begin{array}{%s}\n%s\\end{array}" align contents
)))
2761 ((assoc env org-latex-table-matrix-macros
)
2762 (format "\\%s%s{\n%s}"
2764 (or (plist-get attr
:math-arguments
) "")
2766 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env
)))
2768 (or (plist-get attr
:math-suffix
) "")
2769 ;; Closing string. If TABLE is in the middle of a table cluster,
2770 ;; do not insert any. If it closes such a cluster, be sure to
2771 ;; close the cluster with a string matching the opening string.
2773 (eq (org-element-type next
) 'table
)
2774 (memq (org-element-property :post-blank table
) '(0 nil
))
2775 (funcall same-mode-p next
))
2778 ;; Find cluster beginning to know which environment to use.
2779 ((let ((cluster-beg table
) prev
)
2780 (while (and (setq prev
(org-export-get-previous-element
2782 (memq (org-element-property :post-blank prev
)
2784 (funcall same-mode-p prev
))
2785 (setq cluster-beg prev
))
2786 (and (or (org-element-property :caption cluster-beg
)
2787 (org-element-property :name cluster-beg
))
2788 "\n\\end{equation}")))
2794 (defun org-latex-table-cell (table-cell contents info
)
2795 "Transcode a TABLE-CELL element from Org to LaTeX.
2796 CONTENTS is the cell contents. INFO is a plist used as
2797 a communication channel."
2798 (concat (if (and contents
2799 org-latex-table-scientific-notation
2800 (string-match orgtbl-exp-regexp contents
))
2801 ;; Use appropriate format string for scientific
2803 (format org-latex-table-scientific-notation
2804 (match-string 1 contents
)
2805 (match-string 2 contents
))
2807 (when (org-export-get-next-element table-cell info
) " & ")))
2812 (defun org-latex-table-row (table-row contents info
)
2813 "Transcode a TABLE-ROW element from Org to LaTeX.
2814 CONTENTS is the contents of the row. INFO is a plist used as
2815 a communication channel."
2816 ;; Rules are ignored since table separators are deduced from
2817 ;; borders of the current row.
2818 (when (eq (org-element-property :type table-row
) 'standard
)
2819 (let* ((attr (org-export-read-attribute :attr_latex
2820 (org-export-get-parent table-row
)))
2821 (longtablep (member (or (plist-get attr
:environment
)
2822 org-latex-default-table-environment
)
2823 '("longtable" "longtabu")))
2824 (booktabsp (if (plist-member attr
:booktabs
)
2825 (plist-get attr
:booktabs
)
2826 org-latex-tables-booktabs
))
2827 ;; TABLE-ROW's borders are extracted from its first cell.
2828 (borders (org-export-table-cell-borders
2829 (car (org-element-contents table-row
)) info
)))
2831 ;; When BOOKTABS are activated enforce top-rule even when no
2832 ;; hline was specifically marked.
2833 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2834 ((and (memq 'top borders
) (memq 'above borders
)) "\\hline\n"))
2837 ;; Special case for long tables. Define header and footers.
2838 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
2841 \\multicolumn{%d}{l}{%s} \\\\
2846 %s\\multicolumn{%d}{r}{%s} \\\\
2849 (if booktabsp
"\\midrule" "\\hline")
2850 (cdr (org-export-table-dimensions
2851 (org-export-get-parent-table table-row
) info
))
2852 (org-latex--translate "Continued from previous page" info
)
2853 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2854 ((and (memq 'top borders
)
2855 (memq 'above borders
)) "\\hline\n")
2858 (if booktabsp
"\\midrule" "\\hline")
2859 (if booktabsp
"\\midrule" "\\hline")
2860 ;; Number of columns.
2861 (cdr (org-export-table-dimensions
2862 (org-export-get-parent-table table-row
) info
))
2863 (org-latex--translate "Continued on next page" info
)))
2864 ;; When BOOKTABS are activated enforce bottom rule even when
2865 ;; no hline was specifically marked.
2866 ((and booktabsp
(memq 'bottom borders
)) "\\bottomrule")
2867 ((and (memq 'bottom borders
) (memq 'below borders
)) "\\hline")
2868 ((memq 'below borders
) (if booktabsp
"\\midrule" "\\hline")))))))
2873 (defun org-latex-target (target contents info
)
2874 "Transcode a TARGET object from Org to LaTeX.
2875 CONTENTS is nil. INFO is a plist holding contextual
2877 (format "\\label{%s}"
2878 (org-export-solidify-link-text (org-element-property :value target
))))
2883 (defun org-latex-timestamp (timestamp contents info
)
2884 "Transcode a TIMESTAMP object from Org to LaTeX.
2885 CONTENTS is nil. INFO is a plist holding contextual
2887 (let ((value (org-latex-plain-text
2888 (org-timestamp-translate timestamp
) info
)))
2889 (case (org-element-property :type timestamp
)
2890 ((active active-range
) (format org-latex-active-timestamp-format value
))
2891 ((inactive inactive-range
)
2892 (format org-latex-inactive-timestamp-format value
))
2893 (otherwise (format org-latex-diary-timestamp-format value
)))))
2898 (defun org-latex-underline (underline contents info
)
2899 "Transcode UNDERLINE from Org to LaTeX.
2900 CONTENTS is the text with underline markup. INFO is a plist
2901 holding contextual information."
2902 (org-latex--text-markup contents
'underline
))
2907 (defun org-latex-verbatim (verbatim contents info
)
2908 "Transcode a VERBATIM object from Org to LaTeX.
2909 CONTENTS is nil. INFO is a plist used as a communication
2911 (org-latex--text-markup (org-element-property :value verbatim
) 'verbatim
))
2916 (defun org-latex-verse-block (verse-block contents info
)
2917 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2918 CONTENTS is verse block contents. INFO is a plist holding
2919 contextual information."
2920 (org-latex--wrap-label
2922 ;; In a verse environment, add a line break to each newline
2923 ;; character and change each white space at beginning of a line
2924 ;; into a space of 1 em. Also change each blank line with
2925 ;; a vertical space of 1 em.
2927 (setq contents
(replace-regexp-in-string
2928 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2929 (replace-regexp-in-string
2930 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents
)))
2931 (while (string-match "^[ \t]+" contents
)
2932 (let ((new-str (format "\\hspace*{%dem}"
2933 (length (match-string 0 contents
)))))
2934 (setq contents
(replace-match new-str nil t contents
))))
2935 (format "\\begin{verse}\n%s\\end{verse}" contents
))))
2939 ;;; End-user functions
2942 (defun org-latex-export-as-latex
2943 (&optional async subtreep visible-only body-only ext-plist
)
2944 "Export current buffer as a LaTeX buffer.
2946 If narrowing is active in the current buffer, only export its
2949 If a region is active, export that region.
2951 A non-nil optional argument ASYNC means the process should happen
2952 asynchronously. The resulting buffer should be accessible
2953 through the `org-export-stack' interface.
2955 When optional argument SUBTREEP is non-nil, export the sub-tree
2956 at point, extracting information from the headline properties
2959 When optional argument VISIBLE-ONLY is non-nil, don't export
2960 contents of hidden elements.
2962 When optional argument BODY-ONLY is non-nil, only write code
2963 between \"\\begin{document}\" and \"\\end{document}\".
2965 EXT-PLIST, when provided, is a property list with external
2966 parameters overriding Org default settings, but still inferior to
2967 file-local settings.
2969 Export is done in a buffer named \"*Org LATEX Export*\", which
2970 will be displayed when `org-export-show-temporary-export-buffer'
2973 (org-export-to-buffer 'latex
"*Org LATEX Export*"
2974 async subtreep visible-only body-only ext-plist
(lambda () (LaTeX-mode))))
2977 (defun org-latex-convert-region-to-latex ()
2978 "Assume the current region has org-mode syntax, and convert it to LaTeX.
2979 This can be used in any buffer. For example, you can write an
2980 itemized list in org-mode syntax in an LaTeX buffer and use this
2981 command to convert it."
2983 (org-export-replace-region-by 'latex
))
2986 (defun org-latex-export-to-latex
2987 (&optional async subtreep visible-only body-only ext-plist
)
2988 "Export current buffer to a LaTeX file.
2990 If narrowing is active in the current buffer, only export its
2993 If a region is active, export that region.
2995 A non-nil optional argument ASYNC means the process should happen
2996 asynchronously. The resulting file should be accessible through
2997 the `org-export-stack' interface.
2999 When optional argument SUBTREEP is non-nil, export the sub-tree
3000 at point, extracting information from the headline properties
3003 When optional argument VISIBLE-ONLY is non-nil, don't export
3004 contents of hidden elements.
3006 When optional argument BODY-ONLY is non-nil, only write code
3007 between \"\\begin{document}\" and \"\\end{document}\".
3009 EXT-PLIST, when provided, is a property list with external
3010 parameters overriding Org default settings, but still inferior to
3011 file-local settings."
3013 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
3014 (org-export-to-file 'latex outfile
3015 async subtreep visible-only body-only ext-plist
)))
3018 (defun org-latex-export-to-pdf
3019 (&optional async subtreep visible-only body-only ext-plist
)
3020 "Export current buffer to LaTeX then process through to PDF.
3022 If narrowing is active in the current buffer, only export its
3025 If a region is active, export that region.
3027 A non-nil optional argument ASYNC means the process should happen
3028 asynchronously. The resulting file should be accessible through
3029 the `org-export-stack' interface.
3031 When optional argument SUBTREEP is non-nil, export the sub-tree
3032 at point, extracting information from the headline properties
3035 When optional argument VISIBLE-ONLY is non-nil, don't export
3036 contents of hidden elements.
3038 When optional argument BODY-ONLY is non-nil, only write code
3039 between \"\\begin{document}\" and \"\\end{document}\".
3041 EXT-PLIST, when provided, is a property list with external
3042 parameters overriding Org default settings, but still inferior to
3043 file-local settings.
3045 Return PDF file's name."
3047 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
3048 (org-export-to-file 'latex outfile
3049 async subtreep visible-only body-only ext-plist
3050 (lambda (file) (org-latex-compile file
)))))
3052 (defun org-latex-compile (texfile &optional snippet
)
3053 "Compile a TeX file.
3055 TEXFILE is the name of the file being compiled. Processing is
3056 done through the command specified in `org-latex-pdf-process'.
3058 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3059 file used to preview a LaTeX snippet. In this case, do not
3060 create a log buffer and do not bother removing log files.
3062 Return PDF file name or an error if it couldn't be produced."
3063 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile
)))
3064 (full-name (file-truename texfile
))
3065 (out-dir (file-name-directory texfile
))
3066 ;; Properly set working directory for compilation.
3067 (default-directory (if (file-name-absolute-p texfile
)
3068 (file-name-directory full-name
)
3070 (time (current-time))
3072 (unless snippet
(message (format "Processing LaTeX file %s..." texfile
)))
3073 (save-window-excursion
3075 ;; A function is provided: Apply it.
3076 ((functionp org-latex-pdf-process
)
3077 (funcall org-latex-pdf-process
(shell-quote-argument texfile
)))
3078 ;; A list is provided: Replace %b, %f and %o with appropriate
3079 ;; values in each command before applying it. Output is
3080 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3081 ((consp org-latex-pdf-process
)
3082 (let ((outbuf (and (not snippet
)
3083 (get-buffer-create "*Org PDF LaTeX Output*"))))
3087 (replace-regexp-in-string
3088 "%b" (shell-quote-argument base-name
)
3089 (replace-regexp-in-string
3090 "%f" (shell-quote-argument full-name
)
3091 (replace-regexp-in-string
3092 "%o" (shell-quote-argument out-dir
) command t t
) t t
) t t
)
3094 org-latex-pdf-process
)
3095 ;; Collect standard errors from output buffer.
3096 (setq warnings
(and (not snippet
)
3097 (org-latex--collect-warnings outbuf
)))))
3098 (t (error "No valid command to process to PDF")))
3099 (let ((pdffile (concat out-dir base-name
".pdf")))
3100 ;; Check for process failure. Provide collected errors if
3102 (if (or (not (file-exists-p pdffile
))
3103 (time-less-p (nth 5 (file-attributes pdffile
)) time
))
3104 (error (format "PDF file %s wasn't produced" pdffile
))
3105 ;; Else remove log files, when specified, and signal end of
3106 ;; process to user, along with any error encountered.
3107 (when (and (not snippet
) org-latex-remove-logfiles
)
3108 (dolist (file (directory-files
3110 (concat (regexp-quote base-name
)
3111 "\\(?:\\.[0-9]+\\)?"
3113 (regexp-opt org-latex-logfiles-extensions
))))
3114 (delete-file file
)))
3115 (message (concat "PDF file produced"
3117 ((eq warnings
'error
) " with errors.")
3118 (warnings (concat " with warnings: " warnings
))
3120 ;; Return output file name.
3123 (defun org-latex--collect-warnings (buffer)
3124 "Collect some warnings from \"pdflatex\" command output.
3125 BUFFER is the buffer containing output. Return collected
3126 warnings types as a string, `error' if a LaTeX error was
3127 encountered or nil if there was none."
3128 (with-current-buffer buffer
3130 (goto-char (point-max))
3131 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t
)
3132 (if (re-search-forward "^!" nil t
) 'error
3133 (let ((case-fold-search t
)
3135 (dolist (warning org-latex-known-warnings
)
3137 (when (save-excursion (re-search-forward (car warning
) nil t
))
3138 (setq warnings
(concat warnings
" " (cdr warning
))))))
3139 (and (org-string-nw-p warnings
) (org-trim warnings
))))))))
3142 (defun org-latex-publish-to-latex (plist filename pub-dir
)
3143 "Publish an Org file to LaTeX.
3145 FILENAME is the filename of the Org file to be published. PLIST
3146 is the property list for the given project. PUB-DIR is the
3147 publishing directory.
3149 Return output file name."
3150 (org-publish-org-to 'latex filename
".tex" plist pub-dir
))
3153 (defun org-latex-publish-to-pdf (plist filename pub-dir
)
3154 "Publish an Org file to PDF (via LaTeX).
3156 FILENAME is the filename of the Org file to be published. PLIST
3157 is the property list for the given project. PUB-DIR is the
3158 publishing directory.
3160 Return output file name."
3161 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3162 ;; in working directory and then moved to publishing directory.
3163 (org-publish-attachment
3167 'latex filename
".tex" plist
(file-name-directory filename
)))
3174 ;; generated-autoload-file: "org-loaddefs.el"
3177 ;;; ox-latex.el ends here