1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;; See Org manual for details.
27 (eval-when-compile (require 'cl
))
31 (defvar org-latex-default-packages-alist
)
32 (defvar org-latex-packages-alist
)
33 (defvar orgtbl-exp-regexp
)
39 (org-export-define-backend 'latex
40 '((bold . org-latex-bold
)
41 (center-block . org-latex-center-block
)
42 (clock . org-latex-clock
)
43 (code . org-latex-code
)
44 (comment .
(lambda (&rest args
) ""))
45 (comment-block .
(lambda (&rest args
) ""))
46 (drawer . org-latex-drawer
)
47 (dynamic-block . org-latex-dynamic-block
)
48 (entity . org-latex-entity
)
49 (example-block . org-latex-example-block
)
50 (export-block . org-latex-export-block
)
51 (export-snippet . org-latex-export-snippet
)
52 (fixed-width . org-latex-fixed-width
)
53 (footnote-definition . org-latex-footnote-definition
)
54 (footnote-reference . org-latex-footnote-reference
)
55 (headline . org-latex-headline
)
56 (horizontal-rule . org-latex-horizontal-rule
)
57 (inline-src-block . org-latex-inline-src-block
)
58 (inlinetask . org-latex-inlinetask
)
59 (italic . org-latex-italic
)
60 (item . org-latex-item
)
61 (keyword . org-latex-keyword
)
62 (latex-environment . org-latex-latex-environment
)
63 (latex-fragment . org-latex-latex-fragment
)
64 (line-break . org-latex-line-break
)
65 (link . org-latex-link
)
66 (paragraph . org-latex-paragraph
)
67 (plain-list . org-latex-plain-list
)
68 (plain-text . org-latex-plain-text
)
69 (planning . org-latex-planning
)
70 (property-drawer .
(lambda (&rest args
) ""))
71 (quote-block . org-latex-quote-block
)
72 (quote-section . org-latex-quote-section
)
73 (radio-target . org-latex-radio-target
)
74 (section . org-latex-section
)
75 (special-block . org-latex-special-block
)
76 (src-block . org-latex-src-block
)
77 (statistics-cookie . org-latex-statistics-cookie
)
78 (strike-through . org-latex-strike-through
)
79 (subscript . org-latex-subscript
)
80 (superscript . org-latex-superscript
)
81 (table . org-latex-table
)
82 (table-cell . org-latex-table-cell
)
83 (table-row . org-latex-table-row
)
84 (target . org-latex-target
)
85 (template . org-latex-template
)
86 (timestamp . org-latex-timestamp
)
87 (underline . org-latex-underline
)
88 (verbatim . org-latex-verbatim
)
89 (verse-block . org-latex-verse-block
))
90 :export-block
'("LATEX" "TEX")
92 '(?l
"Export to LaTeX"
93 ((?L
"As LaTeX buffer" org-latex-export-as-latex
)
94 (?l
"As LaTeX file" org-latex-export-to-latex
)
95 (?p
"As PDF file" org-latex-export-to-pdf
)
96 (?o
"As PDF file and open"
98 (if a
(org-latex-export-to-pdf t s v b
)
99 (org-open-file (org-latex-export-to-pdf nil s v b
)))))))
100 :options-alist
'((:latex-class
"LATEX_CLASS" nil org-latex-default-class t
)
101 (:latex-class-options
"LATEX_CLASS_OPTIONS" nil nil t
)
102 (:latex-header
"LATEX_HEADER" nil nil newline
)
103 (:latex-header-extra
"LATEX_HEADER_EXTRA" nil nil newline
)
104 (:latex-hyperref-p nil
"texht" org-latex-with-hyperref t
)
105 ;; Redefine regular options.
106 (:date
"DATE" nil
"\\today" t
)))
110 ;;; Internal Variables
112 (defconst org-latex-babel-language-alist
113 '(("af" .
"afrikaans")
115 ("bt-br" .
"brazilian")
121 ("de-at" .
"naustrian")
122 ("de-de" .
"ngerman")
125 ("en-au" .
"australian")
126 ("en-ca" .
"canadian")
127 ("en-gb" .
"british")
129 ("en-nz" .
"newzealand")
130 ("en-us" .
"american")
136 ("fr-ca" .
"canadien")
140 ("id" .
"indonesian")
146 ("no-no" .
"nynorsk")
148 ("pt" .
"portuguese")
152 ("sb" .
"uppersorbian")
160 ("uk" .
"ukrainian"))
161 "Alist between language code and corresponding Babel option.")
163 (defconst org-latex-table-matrix-macros
'(("bordermatrix" .
"\\cr")
164 ("qbordermatrix" .
"\\cr")
165 ("kbordermatrix" .
"\\\\"))
166 "Alist between matrix macros and their row ending.")
170 ;;; User Configurable Variables
172 (defgroup org-export-latex nil
173 "Options for exporting Org mode files to LaTeX."
174 :tag
"Org Export LaTeX"
180 (defcustom org-latex-default-class
"article"
181 "The default LaTeX class."
182 :group
'org-export-latex
183 :type
'(string :tag
"LaTeX class"))
185 (defcustom org-latex-classes
187 "\\documentclass[11pt]{article}"
188 ("\\section{%s}" .
"\\section*{%s}")
189 ("\\subsection{%s}" .
"\\subsection*{%s}")
190 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
191 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
192 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
194 "\\documentclass[11pt]{report}"
195 ("\\part{%s}" .
"\\part*{%s}")
196 ("\\chapter{%s}" .
"\\chapter*{%s}")
197 ("\\section{%s}" .
"\\section*{%s}")
198 ("\\subsection{%s}" .
"\\subsection*{%s}")
199 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
201 "\\documentclass[11pt]{book}"
202 ("\\part{%s}" .
"\\part*{%s}")
203 ("\\chapter{%s}" .
"\\chapter*{%s}")
204 ("\\section{%s}" .
"\\section*{%s}")
205 ("\\subsection{%s}" .
"\\subsection*{%s}")
206 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
207 "Alist of LaTeX classes and associated header and structure.
208 If #+LATEX_CLASS is set in the buffer, use its value and the
209 associated information. Here is the structure of each cell:
213 \(numbered-section . unnumbered-section)
219 The HEADER-STRING is the header that will be inserted into the
220 LaTeX file. It should contain the \\documentclass macro, and
221 anything else that is needed for this setup. To this header, the
222 following commands will be added:
224 - Calls to \\usepackage for all packages mentioned in the
225 variables `org-latex-default-packages-alist' and
226 `org-latex-packages-alist'. Thus, your header definitions
227 should avoid to also request these packages.
229 - Lines specified via \"#+LATEX_HEADER:\" and
230 \"#+LATEX_HEADER_EXTRA:\" keywords.
232 If you need more control about the sequence in which the header
233 is built up, or if you want to exclude one of these building
234 blocks for a particular class, you can use the following
235 macro-like placeholders.
237 [DEFAULT-PACKAGES] \\usepackage statements for default packages
238 [NO-DEFAULT-PACKAGES] do not include any of the default packages
239 [PACKAGES] \\usepackage statements for packages
240 [NO-PACKAGES] do not include the packages
241 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
242 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
246 \\documentclass{article}
247 [NO-DEFAULT-PACKAGES]
249 \\providecommand{\\alert}[1]{\\textbf{#1}}
252 will omit the default packages, and will include the
253 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
254 to \\providecommand, and then place \\usepackage commands based
255 on the content of `org-latex-packages-alist'.
257 If your header, `org-latex-default-packages-alist' or
258 `org-latex-packages-alist' inserts
259 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
260 replaced with a coding system derived from
261 `buffer-file-coding-system'. See also the variable
262 `org-latex-inputenc-alist' for a way to influence this mechanism.
264 The sectioning structure
265 ------------------------
267 The sectioning structure of the class is given by the elements
268 following the header string. For each sectioning level, a number
269 of strings is specified. A %s formatter is mandatory in each
270 section string and will be replaced by the title of the section.
272 Instead of a cons cell (numbered . unnumbered), you can also
273 provide a list of 2 or 4 elements,
275 \(numbered-open numbered-close)
279 \(numbered-open numbered-close unnumbered-open unnumbered-close)
281 providing opening and closing strings for a LaTeX environment
282 that should represent the document section. The opening clause
283 should have a %s to represent the section title.
285 Instead of a list of sectioning commands, you can also specify
286 a function name. That function will be called with two
287 parameters, the (reduced) level of the headline, and a predicate
288 non-nil when the headline should be numbered. It must return
289 a format string in which the section title will be added."
290 :group
'org-export-latex
292 (list (string :tag
"LaTeX class")
293 (string :tag
"LaTeX header")
294 (repeat :tag
"Levels" :inline t
297 (string :tag
" numbered")
298 (string :tag
"unnumbered"))
299 (list :tag
"Environment"
300 (string :tag
"Opening (numbered)")
301 (string :tag
"Closing (numbered)")
302 (string :tag
"Opening (unnumbered)")
303 (string :tag
"Closing (unnumbered)"))
304 (function :tag
"Hook computing sectioning"))))))
306 (defcustom org-latex-inputenc-alist nil
307 "Alist of inputenc coding system names, and what should really be used.
308 For example, adding an entry
310 (\"utf8\" . \"utf8x\")
312 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
313 are written as utf8 files."
314 :group
'org-export-latex
317 (string :tag
"Derived from buffer")
318 (string :tag
"Use this instead"))))
320 (defcustom org-latex-title-command
"\\maketitle"
321 "The command used to insert the title just after \\begin{document}.
322 If this string contains the formatting specification \"%s\" then
323 it will be used as a formatting string, passing the title as an
325 :group
'org-export-latex
328 (defcustom org-latex-toc-command
"\\tableofcontents\n\n"
329 "LaTeX command to set the table of contents, list of figures, etc.
330 This command only applies to the table of contents generated with
331 the toc:nil option, not to those generated with #+TOC keyword."
332 :group
'org-export-latex
335 (defcustom org-latex-with-hyperref t
336 "Toggle insertion of \\hypersetup{...} in the preamble."
337 :group
'org-export-latex
343 (defcustom org-latex-format-headline-function
344 'org-latex-format-headline-default-function
345 "Function for formatting the headline's text.
347 This function will be called with 5 arguments:
348 TODO the todo keyword (string or nil).
349 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
350 PRIORITY the priority of the headline (integer or nil)
351 TEXT the main headline text (string).
352 TAGS the tags as a list of strings (list of strings or nil).
354 The function result will be used in the section format string.
356 Use `org-latex-format-headline-default-function' by default,
357 which format headlines like for Org version prior to 8.0."
358 :group
'org-export-latex
360 :package-version
'(Org .
"8.0")
366 (defcustom org-latex-footnote-separator
"\\textsuperscript{,}\\,"
367 "Text used to separate footnotes."
368 :group
'org-export-latex
374 (defcustom org-latex-active-timestamp-format
"\\textit{%s}"
375 "A printf format string to be applied to active timestamps."
376 :group
'org-export-latex
379 (defcustom org-latex-inactive-timestamp-format
"\\textit{%s}"
380 "A printf format string to be applied to inactive timestamps."
381 :group
'org-export-latex
384 (defcustom org-latex-diary-timestamp-format
"\\textit{%s}"
385 "A printf format string to be applied to diary timestamps."
386 :group
'org-export-latex
392 (defcustom org-latex-image-default-option
""
393 "Default option for images."
394 :group
'org-export-latex
396 :package-version
'(Org .
"8.0")
399 (defcustom org-latex-image-default-width
".9\\linewidth"
400 "Default width for images.
401 This value will not be used if a height is provided."
402 :group
'org-export-latex
404 :package-version
'(Org .
"8.0")
407 (defcustom org-latex-image-default-height
""
408 "Default height for images.
409 This value will not be used if a width is provided, or if the
410 image is wrapped within a \"figure\" or \"wrapfigure\"
412 :group
'org-export-latex
414 :package-version
'(Org .
"8.0")
417 (defcustom org-latex-default-figure-position
"htb"
418 "Default position for latex figures."
419 :group
'org-export-latex
422 (defcustom org-latex-inline-image-rules
423 '(("file" .
"\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\)\\'"))
424 "Rules characterizing image files that can be inlined into LaTeX.
426 A rule consists in an association whose key is the type of link
427 to consider, and value is a regexp that will be matched against
430 Note that, by default, the image extension *actually* allowed
431 depend on the way the LaTeX file is processed. When used with
432 pdflatex, pdf, jpg and png images are OK. When processing
433 through dvi to Postscript, only ps and eps are allowed. The
434 default we use here encompasses both."
435 :group
'org-export-latex
437 :package-version
'(Org .
"8.0")
438 :type
'(alist :key-type
(string :tag
"Type")
439 :value-type
(regexp :tag
"Path")))
441 (defcustom org-latex-link-with-unknown-path-format
"\\texttt{%s}"
442 "Format string for links with unknown path type."
443 :group
'org-export-latex
449 (defcustom org-latex-default-table-environment
"tabular"
450 "Default environment used to build tables."
451 :group
'org-export-latex
453 :package-version
'(Org .
"8.0")
456 (defcustom org-latex-default-table-mode
'table
457 "Default mode for tables.
459 Value can be a symbol among:
461 `table' Regular LaTeX table.
463 `math' In this mode, every cell is considered as being in math
464 mode and the complete table will be wrapped within a math
465 environment. It is particularly useful to write matrices.
467 `inline-math' This mode is almost the same as `math', but the
468 math environment will be inlined.
470 `verbatim' The table is exported as it appears in the Org
471 buffer, within a verbatim environment.
473 This value can be overridden locally with, i.e. \":mode math\" in
476 When modifying this variable, it may be useful to change
477 `org-latex-default-table-environment' accordingly."
478 :group
'org-export-latex
480 :package-version
'(Org .
"8.0")
481 :type
'(choice (const :tag
"Table" table
)
482 (const :tag
"Matrix" math
)
483 (const :tag
"Inline matrix" inline-math
)
484 (const :tag
"Verbatim" verbatim
)))
486 (defcustom org-latex-tables-centered t
487 "When non-nil, tables are exported in a center environment."
488 :group
'org-export-latex
491 (defcustom org-latex-tables-booktabs nil
492 "When non-nil, display tables in a formal \"booktabs\" style.
493 This option assumes that the \"booktabs\" package is properly
494 loaded in the header of the document. This value can be ignored
495 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
497 :group
'org-export-latex
499 :package-version
'(Org .
"8.0")
502 (defcustom org-latex-table-caption-above t
503 "When non-nil, place caption string at the beginning of the table.
504 Otherwise, place it near the end."
505 :group
'org-export-latex
508 (defcustom org-latex-table-scientific-notation
"%s\\,(%s)"
509 "Format string to display numbers in scientific notation.
510 The format should have \"%s\" twice, for mantissa and exponent
511 \(i.e., \"%s\\\\times10^{%s}\").
513 When nil, no transformation is made."
514 :group
'org-export-latex
516 :package-version
'(Org .
"8.0")
518 (string :tag
"Format string")
519 (const :tag
"No formatting")))
524 (defcustom org-latex-text-markup-alist
'((bold .
"\\textbf{%s}")
526 (italic .
"\\emph{%s}")
527 (strike-through .
"\\st{%s}")
528 (underline .
"\\underline{%s}")
529 (verbatim . protectedtexttt
))
530 "Alist of LaTeX expressions to convert text markup.
532 The key must be a symbol among `bold', `code', `italic',
533 `strike-through', `underline' and `verbatim'. The value is
534 a formatting string to wrap fontified text with.
536 Value can also be set to the following symbols: `verb' and
537 `protectedtexttt'. For the former, Org will use \"\\verb\" to
538 create a format string and select a delimiter character that
539 isn't in the string. For the latter, Org will use \"\\texttt\"
540 to typeset and try to protect special characters.
542 If no association can be found for a given markup, text will be
544 :group
'org-export-latex
546 :options
'(bold code italic strike-through underline verbatim
))
551 (defcustom org-latex-format-drawer-function nil
552 "Function called to format a drawer in LaTeX code.
554 The function must accept two parameters:
555 NAME the drawer name, like \"LOGBOOK\"
556 CONTENTS the contents of the drawer.
558 The function should return the string to be exported.
560 For example, the variable could be set to the following function
561 in order to mimic default behaviour:
563 \(defun org-latex-format-drawer-default \(name contents\)
564 \"Format a drawer element for LaTeX export.\"
566 :group
'org-export-latex
572 (defcustom org-latex-format-inlinetask-function nil
573 "Function called to format an inlinetask in LaTeX code.
575 The function must accept six parameters:
576 TODO the todo keyword, as a string
577 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
578 PRIORITY the inlinetask priority, as a string
579 NAME the inlinetask name, as a string.
580 TAGS the inlinetask tags, as a list of strings.
581 CONTENTS the contents of the inlinetask, as a string.
583 The function should return the string to be exported.
585 For example, the variable could be set to the following function
586 in order to mimic default behaviour:
588 \(defun org-latex-format-inlinetask \(todo type priority name tags contents\)
589 \"Format an inline task element for LaTeX export.\"
593 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
594 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
597 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
598 \(mapconcat 'identity tags \":\")))))
599 \(format (concat \"\\\\begin{center}\\n\"
601 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
603 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
605 \"\\\\end{minipage}}\"
607 full-title contents))"
608 :group
'org-export-latex
614 (defcustom org-latex-listings nil
615 "Non-nil means export source code using the listings package.
616 This package will fontify source code, possibly even with color.
617 If you want to use this, you also need to make LaTeX use the
618 listings package, and if you want to have color, the color
619 package. Just add these to `org-latex-packages-alist', for
620 example using customize, or with something like:
623 \(add-to-list 'org-latex-packages-alist '\(\"\" \"listings\"))
624 \(add-to-list 'org-latex-packages-alist '\(\"\" \"color\"))
628 \(setq org-latex-listings 'minted)
630 causes source code to be exported using the minted package as
631 opposed to listings. If you want to use minted, you need to add
632 the minted package to `org-latex-packages-alist', for example
633 using customize, or with
636 \(add-to-list 'org-latex-packages-alist '\(\"\" \"minted\"))
638 In addition, it is necessary to install pygments
639 \(http://pygments.org), and to configure the variable
640 `org-latex-pdf-process' so that the -shell-escape option is
642 :group
'org-export-latex
644 (const :tag
"Use listings" t
)
645 (const :tag
"Use minted" 'minted
)
646 (const :tag
"Export verbatim" nil
)))
648 (defcustom org-latex-listings-langs
649 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
652 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
653 (html "HTML") (xml "XML")
654 (tex "TeX") (latex "TeX")
655 (shell-script "bash")
657 (ocaml "Caml") (caml "Caml")
658 (sql "SQL") (sqlite "sql"))
659 "Alist mapping languages to their listing language counterpart.
660 The key is a symbol, the major mode symbol without the \"-mode\".
661 The value is the string that should be inserted as the language
662 parameter for the listings package. If the mode name and the
663 listings name are the same, the language does not need an entry
664 in this list - but it does not hurt if it is present."
665 :group
'org-export-latex
668 (symbol :tag
"Major mode ")
669 (string :tag
"Listings language"))))
671 (defcustom org-latex-listings-options nil
672 "Association list of options for the latex listings package.
674 These options are supplied as a comma-separated list to the
675 \\lstset command. Each element of the association list should be
676 a list containing two strings: the name of the option, and the
679 (setq org-latex-listings-options
680 '((\"basicstyle\" \"\\small\")
681 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
683 will typeset the code in a small size font with underlined, bold
686 Note that the same options will be applied to blocks of all
688 :group
'org-export-latex
691 (string :tag
"Listings option name ")
692 (string :tag
"Listings option value"))))
694 (defcustom org-latex-minted-langs
695 '((emacs-lisp "common-lisp")
698 (shell-script "bash")
700 "Alist mapping languages to their minted language counterpart.
701 The key is a symbol, the major mode symbol without the \"-mode\".
702 The value is the string that should be inserted as the language
703 parameter for the minted package. If the mode name and the
704 listings name are the same, the language does not need an entry
705 in this list - but it does not hurt if it is present.
707 Note that minted uses all lower case for language identifiers,
708 and that the full list of language identifiers can be obtained
711 pygmentize -L lexers"
712 :group
'org-export-latex
715 (symbol :tag
"Major mode ")
716 (string :tag
"Minted language"))))
718 (defcustom org-latex-minted-options nil
719 "Association list of options for the latex minted package.
721 These options are supplied within square brackets in
722 \\begin{minted} environments. Each element of the alist should
723 be a list containing two strings: the name of the option, and the
726 \(setq org-latex-minted-options
727 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
729 will result in src blocks being exported with
731 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
733 as the start of the minted environment. Note that the same
734 options will be applied to blocks of all languages."
735 :group
'org-export-latex
738 (string :tag
"Minted option name ")
739 (string :tag
"Minted option value"))))
741 (defcustom org-latex-long-listings nil
742 "When non-nil no listing will be wrapped within a float.
744 Removing floats may break some functionalities. For example, it
745 will be impossible to use cross-references to listings when using
746 `minted' set-up when this variable is non-nil.
748 This value can be locally ignored with \":long-listing t\" and
749 \":long-listing nil\" LaTeX attributes."
750 :group
'org-export-latex
752 :package-version
'(Org .
"8.0")
755 (defvar org-latex-custom-lang-environments nil
756 "Alist mapping languages to language-specific LaTeX environments.
758 It is used during export of src blocks by the listings and minted
759 latex packages. For example,
761 \(setq org-latex-custom-lang-environments
762 '\(\(python \"pythoncode\"\)\)\)
764 would have the effect that if org encounters begin_src python
765 during latex export it will output
774 (defcustom org-latex-pdf-process
775 '("pdflatex -interaction nonstopmode -output-directory %o %f"
776 "pdflatex -interaction nonstopmode -output-directory %o %f"
777 "pdflatex -interaction nonstopmode -output-directory %o %f")
778 "Commands to process a LaTeX file to a PDF file.
779 This is a list of strings, each of them will be given to the
780 shell as a command. %f in the command will be replaced by the
781 full file name, %b by the file base name (i.e. without directory
782 and extension parts) and %o by the base directory of the file.
784 The reason why this is a list is that it usually takes several
785 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
786 does not have a clever mechanism to detect which of these
787 commands have to be run to get to a stable result, and it also
788 does not do any error checking.
790 By default, Org uses 3 runs of `pdflatex' to do the processing.
791 If you have texi2dvi on your system and if that does not cause
792 the infamous egrep/locale bug:
794 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
796 then `texi2dvi' is the superior choice. Org does offer it as one
797 of the customize options.
799 Alternatively, this may be a Lisp function that does the
800 processing, so you could use this to apply the machinery of
801 AUCTeX or the Emacs LaTeX mode. This function should accept the
802 file name as its single argument."
803 :group
'org-export-pdf
805 (repeat :tag
"Shell command sequence"
806 (string :tag
"Shell command"))
807 (const :tag
"2 runs of pdflatex"
808 ("pdflatex -interaction nonstopmode -output-directory %o %f"
809 "pdflatex -interaction nonstopmode -output-directory %o %f"))
810 (const :tag
"3 runs of pdflatex"
811 ("pdflatex -interaction nonstopmode -output-directory %o %f"
812 "pdflatex -interaction nonstopmode -output-directory %o %f"
813 "pdflatex -interaction nonstopmode -output-directory %o %f"))
814 (const :tag
"pdflatex,bibtex,pdflatex,pdflatex"
815 ("pdflatex -interaction nonstopmode -output-directory %o %f"
817 "pdflatex -interaction nonstopmode -output-directory %o %f"
818 "pdflatex -interaction nonstopmode -output-directory %o %f"))
819 (const :tag
"2 runs of xelatex"
820 ("xelatex -interaction nonstopmode -output-directory %o %f"
821 "xelatex -interaction nonstopmode -output-directory %o %f"))
822 (const :tag
"3 runs of xelatex"
823 ("xelatex -interaction nonstopmode -output-directory %o %f"
824 "xelatex -interaction nonstopmode -output-directory %o %f"
825 "xelatex -interaction nonstopmode -output-directory %o %f"))
826 (const :tag
"xelatex,bibtex,xelatex,xelatex"
827 ("xelatex -interaction nonstopmode -output-directory %o %f"
829 "xelatex -interaction nonstopmode -output-directory %o %f"
830 "xelatex -interaction nonstopmode -output-directory %o %f"))
831 (const :tag
"texi2dvi"
832 ("texi2dvi -p -b -c -V %f"))
834 ("rubber -d --into %o %f"))
837 (defcustom org-latex-logfiles-extensions
838 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
839 "The list of file extensions to consider as LaTeX logfiles.
840 The logfiles will be remove if `org-latex-remove-logfiles' is
842 :group
'org-export-latex
843 :type
'(repeat (string :tag
"Extension")))
845 (defcustom org-latex-remove-logfiles t
846 "Non-nil means remove the logfiles produced by PDF production.
847 By default, logfiles are files with these extensions: .aux, .idx,
848 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
849 logfiles to remove, set `org-latex-logfiles-extensions'."
850 :group
'org-export-latex
853 (defcustom org-latex-known-errors
854 '(("Reference.*?undefined" .
"[undefined reference]")
855 ("Citation.*?undefined" .
"[undefined citation]")
856 ("Undefined control sequence" .
"[undefined control sequence]")
857 ("^! LaTeX.*?Error" .
"[LaTeX error]")
858 ("^! Package.*?Error" .
"[package error]")
859 ("Runaway argument" .
"Runaway argument"))
860 "Alist of regular expressions and associated messages for the user.
861 The regular expressions are used to find possible errors in the
863 :group
'org-export-latex
865 :package-version
'(Org .
"8.0")
868 (string :tag
"Regexp")
869 (string :tag
"Message"))))
873 ;;; Internal Functions
875 (defun org-latex--caption/label-string
(element info
)
876 "Return caption and label LaTeX string for ELEMENT.
878 INFO is a plist holding contextual information. If there's no
879 caption nor label, return the empty string.
881 For non-floats, see `org-latex--wrap-label'."
882 (let* ((label (org-element-property :name element
))
883 (label-str (if (not (org-string-nw-p label
)) ""
884 (format "\\label{%s}"
885 (org-export-solidify-link-text label
))))
886 (main (org-export-get-caption element
))
887 (short (org-export-get-caption element t
)))
889 ((and (not main
) (equal label-str
"")) "")
890 ((not main
) (concat label-str
"\n"))
891 ;; Option caption format with short name.
892 (short (format "\\caption[%s]{%s%s}\n"
893 (org-export-data short info
)
895 (org-export-data main info
)))
896 ;; Standard caption format.
897 (t (format "\\caption{%s%s}\n" label-str
(org-export-data main info
))))))
899 (defun org-latex-guess-inputenc (header)
900 "Set the coding system in inputenc to what the buffer is.
902 HEADER is the LaTeX header string. This function only applies
903 when specified inputenc option is \"AUTO\".
905 Return the new header, as a string."
906 (let* ((cs (or (ignore-errors
907 (latexenc-coding-system-to-inputenc
908 (or org-export-coding-system buffer-file-coding-system
)))
911 ;; First translate if that is requested.
912 (setq cs
(or (cdr (assoc cs org-latex-inputenc-alist
)) cs
))
913 ;; Then find the \usepackage statement and replace the option.
914 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
915 cs header t nil
1))))
917 (defun org-latex-guess-babel-language (header info
)
918 "Set Babel's language according to LANGUAGE keyword.
920 HEADER is the LaTeX header string. INFO is the plist used as
921 a communication channel.
923 Insertion of guessed language only happens when Babel package has
924 explicitly been loaded. Then it is added to the rest of
927 Return the new header."
928 (let ((language-code (plist-get info
:language
)))
929 ;; If no language is set or Babel package is not loaded, return
931 (if (or (not (stringp language-code
))
932 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header
)))
934 (let ((options (save-match-data
935 (org-split-string (match-string 1 header
) ",")))
936 (language (cdr (assoc language-code
937 org-latex-babel-language-alist
))))
938 ;; If LANGUAGE is already loaded, return header. Otherwise,
939 ;; append LANGUAGE to other options.
940 (if (member language options
) header
941 (replace-match (mapconcat 'identity
942 (append options
(list language
))
944 nil nil header
1))))))
946 (defun org-latex--find-verb-separator (s)
947 "Return a character not used in string S.
948 This is used to choose a separator for constructs like \\verb."
949 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
950 (loop for c across ll
951 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
952 return
(char-to-string c
))))
954 (defun org-latex--make-option-string (options)
955 "Return a comma separated string of keywords and values.
956 OPTIONS is an alist where the key is the options keyword as
957 a string, and the value a list containing the keyword value, or
959 (mapconcat (lambda (pair)
961 (when (> (length (second pair
)) 0)
962 (concat "=" (second pair
)))))
966 (defun org-latex--wrap-label (element output
)
967 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
968 This function shouldn't be used for floats. See
969 `org-latex--caption/label-string'."
970 (let ((label (org-element-property :name element
)))
971 (if (not (and (org-string-nw-p output
) (org-string-nw-p label
))) output
972 (concat (format "\\label{%s}\n" (org-export-solidify-link-text label
))
975 (defun org-latex--text-markup (text markup
)
976 "Format TEXT depending on MARKUP text markup.
977 See `org-latex-text-markup-alist' for details."
978 (let ((fmt (cdr (assq markup org-latex-text-markup-alist
))))
980 ;; No format string: Return raw text.
982 ;; Handle the `verb' special case: Find and appropriate separator
983 ;; and use "\\verb" command.
985 (let ((separator (org-latex--find-verb-separator text
)))
986 (concat "\\verb" separator text separator
)))
987 ;; Handle the `protectedtexttt' special case: Protect some
988 ;; special chars and use "\texttt{%s}" format string.
989 ((eq 'protectedtexttt fmt
)
991 (trans '(("\\" .
"\\textbackslash{}")
992 ("~" .
"\\textasciitilde{}")
993 ("^" .
"\\textasciicircum{}")))
996 (while (string-match "[\\{}$%&_#~^]" text
)
997 (setq char
(match-string 0 text
))
998 (if (> (match-beginning 0) 0)
999 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
1000 (setq text
(substring text
(1+ (match-beginning 0))))
1001 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
1002 rtn
(concat rtn char
)))
1003 (setq text
(concat rtn text
)
1005 (while (string-match "--" text
)
1006 (setq text
(replace-match "-{}-" t t text
)))
1008 ;; Else use format string.
1009 (t (format fmt text
)))))
1011 (defun org-latex--delayed-footnotes-definitions (element info
)
1012 "Return footnotes definitions in ELEMENT as a string.
1014 INFO is a plist used as a communication channel.
1016 Footnotes definitions are returned within \"\\footnotetxt{}\"
1019 This function is used within constructs that don't support
1020 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1021 \"\\footnotemark\" is used within the construct and the function
1022 just outside of it."
1026 "\\footnotetext[%s]{%s}"
1027 (org-export-get-footnote-number ref info
)
1030 (org-export-get-footnote-definition ref info
) info
))))
1031 ;; Find every footnote reference in ELEMENT.
1033 search-refs
; For byte-compiler.
1037 ;; Return a list of all footnote references never seen
1039 (org-element-map data
'footnote-reference
1041 (when (org-export-footnote-first-reference-p ref info
)
1043 (when (eq (org-element-property :type ref
) 'standard
)
1044 (funcall search-refs
1045 (org-export-get-footnote-definition ref info
)))))
1047 (reverse all-refs
)))))
1048 (funcall search-refs element
))
1055 (defun org-latex-template (contents info
)
1056 "Return complete document string after LaTeX conversion.
1057 CONTENTS is the transcoded contents string. INFO is a plist
1058 holding export options."
1059 (let ((title (org-export-data (plist-get info
:title
) info
)))
1062 (and (plist-get info
:time-stamp-file
)
1063 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1064 ;; Document class and packages.
1065 (let ((class (plist-get info
:latex-class
))
1066 (class-options (plist-get info
:latex-class-options
)))
1067 (org-element-normalize-string
1068 (let* ((header (nth 1 (assoc class org-latex-classes
)))
1069 (document-class-string
1070 (and (stringp header
)
1071 (if (not class-options
) header
1072 (replace-regexp-in-string
1073 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1074 class-options header t nil
1)))))
1075 (if (not document-class-string
)
1076 (user-error "Unknown LaTeX class `%s'" class
)
1077 (org-latex-guess-babel-language
1078 (org-latex-guess-inputenc
1079 (org-splice-latex-header
1080 document-class-string
1081 org-latex-default-packages-alist
1082 org-latex-packages-alist nil
1083 (concat (plist-get info
:latex-header
)
1084 (plist-get info
:latex-header-extra
))))
1086 ;; Possibly limit depth for headline numbering.
1087 (let ((sec-num (plist-get info
:section-numbers
)))
1088 (when (integerp sec-num
)
1089 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
1091 (let ((author (and (plist-get info
:with-author
)
1092 (let ((auth (plist-get info
:author
)))
1093 (and auth
(org-export-data auth info
)))))
1094 (email (and (plist-get info
:with-email
)
1095 (org-export-data (plist-get info
:email
) info
))))
1096 (cond ((and author email
(not (string= "" email
)))
1097 (format "\\author{%s\\thanks{%s}}\n" author email
))
1098 ((or author email
) (format "\\author{%s}\n" (or author email
)))))
1100 (let ((date (and (plist-get info
:with-date
) (org-export-get-date info
))))
1101 (format "\\date{%s}\n" (org-export-data date info
)))
1103 (format "\\title{%s}\n" title
)
1104 ;; Hyperref options.
1105 (when (plist-get info
:latex-hyperref-p
)
1106 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1107 (or (plist-get info
:keywords
) "")
1108 (or (plist-get info
:description
) "")
1109 (if (not (plist-get info
:with-creator
)) ""
1110 (plist-get info
:creator
))))
1112 "\\begin{document}\n\n"
1114 (org-element-normalize-string
1115 (cond ((string= "" title
) nil
)
1116 ((not (stringp org-latex-title-command
)) nil
)
1117 ((string-match "\\(?:[^%]\\|^\\)%s"
1118 org-latex-title-command
)
1119 (format org-latex-title-command title
))
1120 (t org-latex-title-command
)))
1121 ;; Table of contents.
1122 (let ((depth (plist-get info
:with-toc
)))
1124 (concat (when (wholenump depth
)
1125 (format "\\setcounter{tocdepth}{%d}\n" depth
))
1126 org-latex-toc-command
)))
1130 (let ((creator-info (plist-get info
:with-creator
)))
1132 ((not creator-info
) "")
1133 ((eq creator-info
'comment
)
1134 (format "%% %s\n" (plist-get info
:creator
)))
1135 (t (concat (plist-get info
:creator
) "\n"))))
1137 "\\end{document}")))
1141 ;;; Transcode Functions
1145 (defun org-latex-bold (bold contents info
)
1146 "Transcode BOLD from Org to LaTeX.
1147 CONTENTS is the text with bold markup. INFO is a plist holding
1148 contextual information."
1149 (org-latex--text-markup contents
'bold
))
1154 (defun org-latex-center-block (center-block contents info
)
1155 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1156 CONTENTS holds the contents of the center block. INFO is a plist
1157 holding contextual information."
1158 (org-latex--wrap-label
1160 (format "\\begin{center}\n%s\\end{center}" contents
)))
1165 (defun org-latex-clock (clock contents info
)
1166 "Transcode a CLOCK element from Org to LaTeX.
1167 CONTENTS is nil. INFO is a plist holding contextual
1171 (format "\\textbf{%s} " org-clock-string
)
1172 (format org-latex-inactive-timestamp-format
1173 (concat (org-translate-time
1174 (org-element-property :raw-value
1175 (org-element-property :value clock
)))
1176 (let ((time (org-element-property :duration clock
)))
1177 (and time
(format " (%s)" time
)))))
1183 (defun org-latex-code (code contents info
)
1184 "Transcode a CODE object from Org to LaTeX.
1185 CONTENTS is nil. INFO is a plist used as a communication
1187 (org-latex--text-markup (org-element-property :value code
) 'code
))
1192 (defun org-latex-drawer (drawer contents info
)
1193 "Transcode a DRAWER element from Org to LaTeX.
1194 CONTENTS holds the contents of the block. INFO is a plist
1195 holding contextual information."
1196 (let* ((name (org-element-property :drawer-name drawer
))
1197 (output (if (functionp org-latex-format-drawer-function
)
1198 (funcall org-latex-format-drawer-function
1200 ;; If there's no user defined function: simply
1201 ;; display contents of the drawer.
1203 (org-latex--wrap-label drawer output
)))
1208 (defun org-latex-dynamic-block (dynamic-block contents info
)
1209 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1210 CONTENTS holds the contents of the block. INFO is a plist
1211 holding contextual information. See `org-export-data'."
1212 (org-latex--wrap-label dynamic-block contents
))
1217 (defun org-latex-entity (entity contents info
)
1218 "Transcode an ENTITY object from Org to LaTeX.
1219 CONTENTS are the definition itself. INFO is a plist holding
1220 contextual information."
1221 (let ((ent (org-element-property :latex entity
)))
1222 (if (org-element-property :latex-math-p entity
) (format "$%s$" ent
) ent
)))
1227 (defun org-latex-example-block (example-block contents info
)
1228 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1229 CONTENTS is nil. INFO is a plist holding contextual
1231 (when (org-string-nw-p (org-element-property :value example-block
))
1232 (org-latex--wrap-label
1234 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1235 (org-export-format-code-default example-block info
)))))
1240 (defun org-latex-export-block (export-block contents info
)
1241 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1242 CONTENTS is nil. INFO is a plist holding contextual information."
1243 (when (member (org-element-property :type export-block
) '("LATEX" "TEX"))
1244 (org-remove-indentation (org-element-property :value export-block
))))
1249 (defun org-latex-export-snippet (export-snippet contents info
)
1250 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1251 CONTENTS is nil. INFO is a plist holding contextual information."
1252 (when (eq (org-export-snippet-backend export-snippet
) 'latex
)
1253 (org-element-property :value export-snippet
)))
1258 (defun org-latex-fixed-width (fixed-width contents info
)
1259 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1260 CONTENTS is nil. INFO is a plist holding contextual information."
1261 (org-latex--wrap-label
1263 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1264 (org-remove-indentation
1265 (org-element-property :value fixed-width
)))))
1268 ;;;; Footnote Reference
1270 ;; Footnote reference export is handled by
1271 ;; `org-latex-footnote-reference'.
1273 ;; Internally, `org-latex--get-footnote-counter' is used to restore
1274 ;; the value of the LaTeX "footnote" counter after a jump due to
1275 ;; a reference to an already defined footnote. It is only needed in
1276 ;; item tags since the optional argument to \footnotemark is not
1279 (defun org-latex--get-footnote-counter (footnote-reference info
)
1280 "Return \"footnote\" counter before FOOTNOTE-REFERENCE is encountered.
1281 INFO is a plist used as a communication channel."
1282 ;; Find original counter value by counting number of footnote
1283 ;; references appearing for the first time before the current
1284 ;; footnote reference.
1285 (let* ((label (org-element-property :label footnote-reference
))
1287 search-ref
; For byte-compiler.
1291 ;; Search footnote references through DATA, filling
1292 ;; SEEN-REFS along the way.
1293 (org-element-map data
'footnote-reference
1295 (let ((fn-lbl (org-element-property :label fn
)))
1297 ;; Anonymous footnote match: return number.
1298 ((eq fn footnote-reference
) (length seen-refs
))
1299 ;; Anonymous footnote: it's always a new one.
1300 ;; Also, be sure to return nil from the `cond' so
1301 ;; `first-match' doesn't get us out of the loop.
1302 ((not fn-lbl
) (push 'inline seen-refs
) nil
)
1303 ;; Label not seen so far: add it so SEEN-REFS.
1305 ;; Also search for subsequent references in
1306 ;; footnote definition so numbering follows
1307 ;; reading logic. Note that we don't care about
1308 ;; inline definitions, since `org-element-map'
1309 ;; already traverses them at the right time.
1310 ((not (member fn-lbl seen-refs
))
1311 (push fn-lbl seen-refs
)
1313 (org-export-get-footnote-definition fn info
))))))
1314 ;; Don't enter footnote definitions since it will
1315 ;; happen when their first reference is found.
1316 info
'first-match
'footnote-definition
)))))
1317 (funcall search-ref
(plist-get info
:parse-tree
))))
1319 (defun org-latex-footnote-reference (footnote-reference contents info
)
1320 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1321 CONTENTS is nil. INFO is a plist holding contextual information."
1323 ;; Insert separator between two footnotes in a row.
1324 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1325 (when (eq (org-element-type prev
) 'footnote-reference
)
1326 org-latex-footnote-separator
))
1328 ;; Use \footnotemark if reference is within an item's tag.
1329 ((eq (org-element-type (org-export-get-parent-element footnote-reference
))
1331 (if (org-export-footnote-first-reference-p footnote-reference info
)
1333 ;; Since we can't specify footnote number as an optional
1334 ;; argument within an item tag, some extra work has to be done
1335 ;; when the footnote has already been referenced. In that
1336 ;; case, set footnote counter to the desired number, use the
1337 ;; footnotemark, then set counter back to its original value.
1339 "\\setcounter{footnote}{%s}\\footnotemark\\setcounter{footnote}{%s}"
1340 (1- (org-export-get-footnote-number footnote-reference info
))
1341 (org-latex--get-footnote-counter footnote-reference info
))))
1342 ;; Use \footnotemark if the footnote has already been defined.
1343 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1344 (format "\\footnotemark[%s]{}"
1345 (org-export-get-footnote-number footnote-reference info
)))
1346 ;; Use \footnotemark if reference is within another footnote
1347 ;; reference, footnote definition or table cell.
1348 ((loop for parent in
(org-export-get-genealogy footnote-reference
)
1349 thereis
(memq (org-element-type parent
)
1350 '(footnote-reference footnote-definition table-cell
)))
1352 ;; Otherwise, define it with \footnote command.
1354 (let ((def (org-export-get-footnote-definition footnote-reference info
)))
1356 (format "\\footnote{%s}" (org-trim (org-export-data def info
)))
1357 ;; Retrieve all footnote references within the footnote and
1358 ;; add their definition after it, since LaTeX doesn't support
1360 (org-latex--delayed-footnotes-definitions def info
)))))))
1365 (defun org-latex-headline (headline contents info
)
1366 "Transcode a HEADLINE element from Org to LaTeX.
1367 CONTENTS holds the contents of the headline. INFO is a plist
1368 holding contextual information."
1369 (unless (org-element-property :footnote-section-p headline
)
1370 (let* ((class (plist-get info
:latex-class
))
1371 (level (org-export-get-relative-level headline info
))
1372 (numberedp (org-export-numbered-headline-p headline info
))
1373 (class-sectionning (assoc class org-latex-classes
))
1374 ;; Section formatting will set two placeholders: one for
1375 ;; the title and the other for the contents.
1377 (let ((sec (if (functionp (nth 2 class-sectionning
))
1378 (funcall (nth 2 class-sectionning
) level numberedp
)
1379 (nth (1+ level
) class-sectionning
))))
1381 ;; No section available for that LEVEL.
1383 ;; Section format directly returned by a function. Add
1384 ;; placeholder for contents.
1385 ((stringp sec
) (concat sec
"\n%s"))
1386 ;; (numbered-section . unnumbered-section)
1387 ((not (consp (cdr sec
)))
1388 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
1389 ;; (numbered-open numbered-close)
1391 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
1392 ;; (num-in num-out no-num-in no-num-out)
1394 (if numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))
1395 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
1396 (text (org-export-data (org-element-property :title headline
) info
))
1398 (and (plist-get info
:with-todo-keywords
)
1399 (let ((todo (org-element-property :todo-keyword headline
)))
1400 (and todo
(org-export-data todo info
)))))
1401 (todo-type (and todo
(org-element-property :todo-type headline
)))
1402 (tags (and (plist-get info
:with-tags
)
1403 (org-export-get-tags headline info
)))
1404 (priority (and (plist-get info
:with-priority
)
1405 (org-element-property :priority headline
)))
1406 ;; Create the headline text along with a no-tag version.
1407 ;; The latter is required to remove tags from toc.
1408 (full-text (funcall org-latex-format-headline-function
1409 todo todo-type priority text tags
))
1410 ;; Associate \label to the headline for internal links.
1412 (format "\\label{sec-%s}\n"
1413 (mapconcat 'number-to-string
1414 (org-export-get-headline-number headline info
)
1417 (make-string (org-element-property :pre-blank headline
) 10)))
1418 (if (or (not section-fmt
) (org-export-low-level-p headline info
))
1419 ;; This is a deep sub-tree: export it as a list item. Also
1420 ;; export as items headlines for which no section format has
1422 (let ((low-level-body
1424 ;; If headline is the first sibling, start a list.
1425 (when (org-export-first-sibling-p headline info
)
1426 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
1428 "\\item " full-text
"\n" headline-label pre-blanks contents
)))
1429 ;; If headline is not the last sibling simply return
1430 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1432 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1433 (replace-regexp-in-string
1435 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
1437 ;; This is a standard headline. Export it as a section. Add
1438 ;; an alternative heading when possible, and when this is not
1439 ;; identical to the usual heading.
1441 (funcall org-latex-format-headline-function
1442 todo todo-type priority
1444 (org-export-get-alt-title headline info
) info
)
1445 (and (eq (plist-get info
:with-tags
) t
) tags
))))
1446 (if (and numberedp opt-title
1447 (not (equal opt-title full-text
))
1448 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt
))
1449 (format (replace-match "\\1[%s]" nil nil section-fmt
1)
1450 ;; Replace square brackets with parenthesis
1451 ;; since square brackets are not supported in
1452 ;; optional arguments.
1453 (replace-regexp-in-string
1454 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title
))
1456 (concat headline-label pre-blanks contents
))
1457 ;; Impossible to add an alternative heading. Fallback to
1458 ;; regular sectioning format string.
1459 (format section-fmt full-text
1460 (concat headline-label pre-blanks contents
))))))))
1462 (defun org-latex-format-headline-default-function
1463 (todo todo-type priority text tags
)
1464 "Default format function for a headline.
1465 See `org-latex-format-headline-function' for details."
1467 (and todo
(format "{\\bfseries\\sffamily %s} " todo
))
1468 (and priority
(format "\\framebox{\\#%c} " priority
))
1471 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags
":")))))
1474 ;;;; Horizontal Rule
1476 (defun org-latex-horizontal-rule (horizontal-rule contents info
)
1477 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1478 CONTENTS is nil. INFO is a plist holding contextual information."
1479 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule
))
1480 (prev (org-export-get-previous-element horizontal-rule info
)))
1482 ;; Make sure the rule doesn't start at the end of the current
1483 ;; line by separating it with a blank line from previous element.
1485 (let ((prev-blank (org-element-property :post-blank prev
)))
1486 (or (not prev-blank
) (zerop prev-blank
))))
1488 (org-latex--wrap-label
1490 (format "\\rule{%s}{%s}"
1491 (or (plist-get attr
:width
) "\\linewidth")
1492 (or (plist-get attr
:thickness
) "0.5pt"))))))
1495 ;;;; Inline Src Block
1497 (defun org-latex-inline-src-block (inline-src-block contents info
)
1498 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1499 CONTENTS holds the contents of the item. INFO is a plist holding
1500 contextual information."
1501 (let* ((code (org-element-property :value inline-src-block
))
1502 (separator (org-latex--find-verb-separator code
)))
1504 ;; Do not use a special package: transcode it verbatim.
1505 ((not org-latex-listings
)
1506 (concat "\\verb" separator code separator
))
1507 ;; Use minted package.
1508 ((eq org-latex-listings
'minted
)
1509 (let* ((org-lang (org-element-property :language inline-src-block
))
1510 (mint-lang (or (cadr (assq (intern org-lang
)
1511 org-latex-minted-langs
))
1513 (options (org-latex--make-option-string
1514 org-latex-minted-options
)))
1515 (concat (format "\\mint%s{%s}"
1516 (if (string= options
"") "" (format "[%s]" options
))
1518 separator code separator
)))
1519 ;; Use listings package.
1521 ;; Maybe translate language's name.
1522 (let* ((org-lang (org-element-property :language inline-src-block
))
1523 (lst-lang (or (cadr (assq (intern org-lang
)
1524 org-latex-listings-langs
))
1526 (options (org-latex--make-option-string
1527 (append org-latex-listings-options
1528 `(("language" ,lst-lang
))))))
1529 (concat (format "\\lstinline[%s]" options
)
1530 separator code separator
))))))
1535 (defun org-latex-inlinetask (inlinetask contents info
)
1536 "Transcode an INLINETASK element from Org to LaTeX.
1537 CONTENTS holds the contents of the block. INFO is a plist
1538 holding contextual information."
1539 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1540 (todo (and (plist-get info
:with-todo-keywords
)
1541 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1542 (and todo
(org-export-data todo info
)))))
1543 (todo-type (org-element-property :todo-type inlinetask
))
1544 (tags (and (plist-get info
:with-tags
)
1545 (org-export-get-tags inlinetask info
)))
1546 (priority (and (plist-get info
:with-priority
)
1547 (org-element-property :priority inlinetask
))))
1548 ;; If `org-latex-format-inlinetask-function' is provided, call it
1549 ;; with appropriate arguments.
1550 (if (functionp org-latex-format-inlinetask-function
)
1551 (funcall org-latex-format-inlinetask-function
1552 todo todo-type priority title tags contents
)
1553 ;; Otherwise, use a default template.
1554 (org-latex--wrap-label
1558 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1559 (when priority
(format "\\framebox{\\#%c} " priority
))
1561 (when tags
(format "\\hfill{}\\textsc{:%s:}"
1562 (mapconcat 'identity tags
":"))))))
1563 (format (concat "\\begin{center}\n"
1565 "\\begin{minipage}[c]{.6\\textwidth}\n"
1567 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1572 full-title contents
))))))
1577 (defun org-latex-italic (italic contents info
)
1578 "Transcode ITALIC from Org to LaTeX.
1579 CONTENTS is the text with italic markup. INFO is a plist holding
1580 contextual information."
1581 (org-latex--text-markup contents
'italic
))
1586 (defun org-latex-item (item contents info
)
1587 "Transcode an ITEM element from Org to LaTeX.
1588 CONTENTS holds the contents of the item. INFO is a plist holding
1589 contextual information."
1591 (let ((count (org-element-property :counter item
))
1593 ;; Determine level of current item to determine the
1594 ;; correct LaTeX counter to use (enumi, enumii...).
1595 (let ((parent item
) (level 0))
1596 (while (memq (org-element-type
1597 (setq parent
(org-export-get-parent parent
)))
1599 (when (and (eq (org-element-type parent
) 'plain-list
)
1600 (eq (org-element-property :type parent
)
1606 (format "\\setcounter{enum%s}{%s}\n"
1607 (nth (1- level
) '("i" "ii" "iii" "iv"))
1609 (checkbox (case (org-element-property :checkbox item
)
1610 (on "$\\boxtimes$ ")
1612 (trans "$\\boxminus$ ")))
1613 (tag (let ((tag (org-element-property :tag item
)))
1614 ;; Check-boxes must belong to the tag.
1615 (and tag
(format "[%s] "
1617 (org-export-data tag info
)))))))
1618 (concat counter
"\\item" (or tag
(concat " " checkbox
))
1619 (and contents
(org-trim contents
))
1620 ;; If there are footnotes references in tag, be sure to
1621 ;; add their definition at the end of the item. This
1622 ;; workaround is necessary since "\footnote{}" command is
1623 ;; not supported in tags.
1625 (org-latex--delayed-footnotes-definitions
1626 (org-element-property :tag item
) info
)))))
1631 (defun org-latex-keyword (keyword contents info
)
1632 "Transcode a KEYWORD element from Org to LaTeX.
1633 CONTENTS is nil. INFO is a plist holding contextual information."
1634 (let ((key (org-element-property :key keyword
))
1635 (value (org-element-property :value keyword
)))
1637 ((string= key
"LATEX") value
)
1638 ((string= key
"INDEX") (format "\\index{%s}" value
))
1639 ((string= key
"TOC")
1640 (let ((value (downcase value
)))
1642 ((string-match "\\<headlines\\>" value
)
1643 (let ((depth (or (and (string-match "[0-9]+" value
)
1644 (string-to-number (match-string 0 value
)))
1645 (plist-get info
:with-toc
))))
1647 (when (wholenump depth
)
1648 (format "\\setcounter{tocdepth}{%s}\n" depth
))
1649 "\\tableofcontents")))
1650 ((string= "tables" value
) "\\listoftables")
1651 ((string= "listings" value
)
1653 ((eq org-latex-listings
'minted
) "\\listoflistings")
1654 (org-latex-listings "\\lstlistoflistings")
1655 ;; At the moment, src blocks with a caption are wrapped
1656 ;; into a figure environment.
1657 (t "\\listoffigures")))))))))
1660 ;;;; Latex Environment
1662 (defun org-latex-latex-environment (latex-environment contents info
)
1663 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1664 CONTENTS is nil. INFO is a plist holding contextual information."
1665 (when (plist-get info
:with-latex
)
1666 (let ((label (org-element-property :name latex-environment
))
1667 (value (org-remove-indentation
1668 (org-element-property :value latex-environment
))))
1669 (if (not (org-string-nw-p label
)) value
1670 ;; Environment is labelled: label must be within the environment
1671 ;; (otherwise, a reference pointing to that element will count
1672 ;; the section instead).
1675 (goto-char (point-min))
1678 (format "\\label{%s}\n" (org-export-solidify-link-text label
)))
1679 (buffer-string))))))
1684 (defun org-latex-latex-fragment (latex-fragment contents info
)
1685 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1686 CONTENTS is nil. INFO is a plist holding contextual information."
1687 (when (plist-get info
:with-latex
)
1688 (org-element-property :value latex-fragment
)))
1693 (defun org-latex-line-break (line-break contents info
)
1694 "Transcode a LINE-BREAK object from Org to LaTeX.
1695 CONTENTS is nil. INFO is a plist holding contextual information."
1701 (defun org-latex--inline-image (link info
)
1702 "Return LaTeX code for an inline image.
1703 LINK is the link pointing to the inline image. INFO is a plist
1704 used as a communication channel."
1705 (let* ((parent (org-export-get-parent-element link
))
1706 (path (let ((raw-path (org-element-property :path link
)))
1707 (if (not (file-name-absolute-p raw-path
)) raw-path
1708 (expand-file-name raw-path
))))
1709 (filetype (file-name-extension path
))
1710 (caption (org-latex--caption/label-string parent info
))
1711 ;; Retrieve latex attributes from the element around.
1712 (attr (org-export-read-attribute :attr_latex parent
))
1713 (float (let ((float (plist-get attr
:float
)))
1714 (cond ((string= float
"wrap") 'wrap
)
1715 ((string= float
"multicolumn") 'multicolumn
)
1716 ((or (string= float
"figure")
1717 (org-element-property :caption parent
))
1720 (let ((place (plist-get attr
:placement
)))
1721 (cond (place (format "%s" place
))
1722 ((eq float
'wrap
) "{l}{0.5\\textwidth}")
1724 (format "[%s]" org-latex-default-figure-position
))
1726 (comment-include (if (plist-get attr
:comment-include
) "%" ""))
1727 ;; It is possible to specify width and height in the
1728 ;; ATTR_LATEX line, and also via default variables.
1729 (width (cond ((plist-get attr
:width
))
1730 ((plist-get attr
:height
) "")
1731 ((eq float
'wrap
) "0.48\\textwidth")
1732 (t org-latex-image-default-width
)))
1733 (height (cond ((plist-get attr
:height
))
1734 ((or (plist-get attr
:width
)
1735 (memq float
'(figure wrap
))) "")
1736 (t org-latex-image-default-height
)))
1737 (options (let ((opt (or (plist-get attr
:options
)
1738 org-latex-image-default-option
)))
1739 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt
)) opt
1740 (match-string 1 opt
))))
1742 (if (equal filetype
"tikz")
1744 ;; - use \input to read in image file.
1745 ;; - if options are present, wrap in a tikzpicture environment.
1746 ;; - if width or height are present, use \resizebox to change
1749 (setq image-code
(format "\\input{%s}" path
))
1750 (when (org-string-nw-p options
)
1752 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1755 (when (or (org-string-nw-p width
) (org-string-nw-p height
))
1756 (setq image-code
(format "\\resizebox{%s}{%s}{%s}"
1757 (if (org-string-nw-p width
) width
"!")
1758 (if (org-string-nw-p height
) height
"!")
1760 ;; For other images:
1761 ;; - add width and height to options.
1762 ;; - include the image with \includegraphics.
1763 (when (org-string-nw-p width
)
1764 (setq options
(concat options
",width=" width
)))
1765 (when (org-string-nw-p height
)
1766 (setq options
(concat options
",height=" height
)))
1768 (format "\\includegraphics%s{%s}"
1769 (cond ((not (org-string-nw-p options
)) "")
1770 ((= (aref options
0) ?
,)
1771 (format "[%s]"(substring options
1)))
1772 (t (format "[%s]" options
)))
1774 ;; Return proper string, depending on FLOAT.
1776 (wrap (format "\\begin{wrapfigure}%s
1779 %s\\end{wrapfigure}" placement comment-include image-code caption
))
1780 (multicolumn (format "\\begin{figure*}%s
1783 %s\\end{figure*}" placement comment-include image-code caption
))
1784 (figure (format "\\begin{figure}%s
1787 %s\\end{figure}" placement comment-include image-code caption
))
1788 (otherwise image-code
))))
1790 (defun org-latex-link (link desc info
)
1791 "Transcode a LINK object from Org to LaTeX.
1793 DESC is the description part of the link, or the empty string.
1794 INFO is a plist holding contextual information. See
1796 (let* ((type (org-element-property :type link
))
1797 (raw-path (org-element-property :path link
))
1798 ;; Ensure DESC really exists, or set it to nil.
1799 (desc (and (not (string= desc
"")) desc
))
1800 (imagep (org-export-inline-image-p
1801 link org-latex-inline-image-rules
))
1803 ((member type
'("http" "https" "ftp" "mailto"))
1804 (concat type
":" raw-path
))
1805 ((string= type
"file")
1806 (if (not (file-name-absolute-p raw-path
)) raw-path
1807 (concat "file://" (expand-file-name raw-path
))))
1812 (imagep (org-latex--inline-image link info
))
1813 ;; Radio link: Transcode target's contents and use them as link's
1815 ((string= type
"radio")
1816 (let ((destination (org-export-resolve-radio-link link info
)))
1818 (format "\\hyperref[%s]{%s}"
1819 (org-export-solidify-link-text path
)
1820 (org-export-data (org-element-contents destination
) info
)))))
1821 ;; Links pointing to a headline: Find destination and build
1822 ;; appropriate referencing command.
1823 ((member type
'("custom-id" "fuzzy" "id"))
1824 (let ((destination (if (string= type
"fuzzy")
1825 (org-export-resolve-fuzzy-link link info
)
1826 (org-export-resolve-id-link link info
))))
1827 (case (org-element-type destination
)
1828 ;; Id link points to an external file.
1830 (if desc
(format "\\href{%s}{%s}" destination desc
)
1831 (format "\\url{%s}" destination
)))
1832 ;; Fuzzy link points nowhere.
1834 (format org-latex-link-with-unknown-path-format
1837 (org-element-property :raw-link link
) info
))))
1838 ;; LINK points to a headline. If headlines are numbered
1839 ;; and the link has no description, display headline's
1840 ;; number. Otherwise, display description or headline's
1847 (org-export-get-headline-number destination info
)
1849 (if (and (plist-get info
:section-numbers
) (not desc
))
1850 (format "\\ref{%s}" label
)
1851 (format "\\hyperref[%s]{%s}" label
1854 (org-element-property :title destination
) info
))))))
1855 ;; Fuzzy link points to a target. Do as above.
1857 (let ((path (org-export-solidify-link-text path
)))
1858 (if (not desc
) (format "\\ref{%s}" path
)
1859 (format "\\hyperref[%s]{%s}" path desc
)))))))
1860 ;; Coderef: replace link with the reference name or the
1861 ;; equivalent line number.
1862 ((string= type
"coderef")
1863 (format (org-export-get-coderef-format path desc
)
1864 (org-export-resolve-coderef path info
)))
1865 ;; Link type is handled by a special function.
1866 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
1867 (funcall protocol
(org-link-unescape path
) desc
'latex
))
1868 ;; External link with a description part.
1869 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
1870 ;; External link without a description part.
1871 (path (format "\\url{%s}" path
))
1872 ;; No path, only description. Try to do something useful.
1873 (t (format org-latex-link-with-unknown-path-format desc
)))))
1878 (defun org-latex-paragraph (paragraph contents info
)
1879 "Transcode a PARAGRAPH element from Org to LaTeX.
1880 CONTENTS is the contents of the paragraph, as a string. INFO is
1881 the plist used as a communication channel."
1887 (defun org-latex-plain-list (plain-list contents info
)
1888 "Transcode a PLAIN-LIST element from Org to LaTeX.
1889 CONTENTS is the contents of the list. INFO is a plist holding
1890 contextual information."
1891 (let* ((type (org-element-property :type plain-list
))
1892 (attr (org-export-read-attribute :attr_latex plain-list
))
1893 (latex-type (let ((env (plist-get attr
:environment
)))
1894 (cond (env (format "%s" env
))
1895 ((eq type
'ordered
) "enumerate")
1896 ((eq type
'unordered
) "itemize")
1897 ((eq type
'descriptive
) "description")))))
1898 (org-latex--wrap-label
1900 (format "\\begin{%s}%s\n%s\\end{%s}"
1902 ;; Put optional arguments, if any inside square brackets
1904 (let ((options (format "%s" (or (plist-get attr
:options
) ""))))
1905 (cond ((equal options
"") "")
1906 ((string-match "\\`\\[.*\\]\\'" options
) options
)
1907 (t (format "[%s]" options
))))
1914 (defun org-latex-plain-text (text info
)
1915 "Transcode a TEXT string from Org to LaTeX.
1916 TEXT is the string to transcode. INFO is a plist holding
1917 contextual information."
1918 (let ((specialp (plist-get info
:with-special-strings
))
1920 ;; Protect %, #, &, $, _, { and }.
1921 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output
)
1924 (format "\\%s" (match-string 2 output
)) nil t output
2)))
1927 (replace-regexp-in-string
1928 "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil
2))
1929 ;; Protect \. If special strings are used, be careful not to
1930 ;; protect "\" in "\-" constructs.
1931 (let ((symbols (if specialp
"-%$#&{}^_\\" "%$#&{}^_\\")))
1933 (replace-regexp-in-string
1934 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols
)
1935 "$\\backslash$" output nil t
1)))
1938 (replace-regexp-in-string
1939 "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t
2))
1940 ;; Activate smart quotes. Be sure to provide original TEXT string
1941 ;; since OUTPUT may have been modified.
1942 (when (plist-get info
:with-smart-quotes
)
1943 (setq output
(org-export-activate-smart-quotes output
:latex info text
)))
1944 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1945 (let ((case-fold-search nil
)
1947 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start
)
1948 (setq output
(replace-match
1949 (format "\\%s{}" (match-string 1 output
)) nil t output
)
1950 start
(match-end 0))))
1951 ;; Convert special strings.
1954 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t
)))
1955 ;; Handle break preservation if required.
1956 (when (plist-get info
:preserve-breaks
)
1957 (setq output
(replace-regexp-in-string
1958 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output
)))
1965 (defun org-latex-planning (planning contents info
)
1966 "Transcode a PLANNING element from Org to LaTeX.
1967 CONTENTS is nil. INFO is a plist holding contextual
1975 (let ((closed (org-element-property :closed planning
)))
1978 (format "\\textbf{%s} " org-closed-string
)
1979 (format org-latex-inactive-timestamp-format
1981 (org-element-property :raw-value closed
))))))
1982 (let ((deadline (org-element-property :deadline planning
)))
1985 (format "\\textbf{%s} " org-deadline-string
)
1986 (format org-latex-active-timestamp-format
1988 (org-element-property :raw-value deadline
))))))
1989 (let ((scheduled (org-element-property :scheduled planning
)))
1992 (format "\\textbf{%s} " org-scheduled-string
)
1993 (format org-latex-active-timestamp-format
1995 (org-element-property :raw-value scheduled
))))))))
2002 (defun org-latex-quote-block (quote-block contents info
)
2003 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2004 CONTENTS holds the contents of the block. INFO is a plist
2005 holding contextual information."
2006 (org-latex--wrap-label
2008 (format "\\begin{quote}\n%s\\end{quote}" contents
)))
2013 (defun org-latex-quote-section (quote-section contents info
)
2014 "Transcode a QUOTE-SECTION element from Org to LaTeX.
2015 CONTENTS is nil. INFO is a plist holding contextual information."
2016 (let ((value (org-remove-indentation
2017 (org-element-property :value quote-section
))))
2018 (when value
(format "\\begin{verbatim}\n%s\\end{verbatim}" value
))))
2023 (defun org-latex-radio-target (radio-target text info
)
2024 "Transcode a RADIO-TARGET object from Org to LaTeX.
2025 TEXT is the text of the target. INFO is a plist holding
2026 contextual information."
2027 (format "\\label{%s}%s"
2028 (org-export-solidify-link-text
2029 (org-element-property :value radio-target
))
2035 (defun org-latex-section (section contents info
)
2036 "Transcode a SECTION element from Org to LaTeX.
2037 CONTENTS holds the contents of the section. INFO is a plist
2038 holding contextual information."
2044 (defun org-latex-special-block (special-block contents info
)
2045 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2046 CONTENTS holds the contents of the block. INFO is a plist
2047 holding contextual information."
2048 (let ((type (downcase (org-element-property :type special-block
)))
2049 (opt (org-export-read-attribute :attr_latex special-block
:options
)))
2050 (concat (format "\\begin{%s}%s\n" type
(or opt
""))
2051 ;; Insert any label or caption within the block
2052 ;; (otherwise, a reference pointing to that element will
2053 ;; count the section instead).
2054 (org-latex--caption/label-string special-block info
)
2056 (format "\\end{%s}" type
))))
2061 (defun org-latex-src-block (src-block contents info
)
2062 "Transcode a SRC-BLOCK element from Org to LaTeX.
2063 CONTENTS holds the contents of the item. INFO is a plist holding
2064 contextual information."
2065 (when (org-string-nw-p (org-element-property :value src-block
))
2066 (let* ((lang (org-element-property :language src-block
))
2067 (caption (org-element-property :caption src-block
))
2068 (label (org-element-property :name src-block
))
2069 (custom-env (and lang
2070 (cadr (assq (intern lang
)
2071 org-latex-custom-lang-environments
))))
2072 (num-start (case (org-element-property :number-lines src-block
)
2073 (continued (org-export-get-loc src-block info
))
2075 (retain-labels (org-element-property :retain-labels src-block
))
2077 (let ((attr (org-export-read-attribute :attr_latex src-block
)))
2078 (if (plist-member attr
:long-listing
)
2079 (plist-get attr
:long-listing
)
2080 org-latex-long-listings
))))
2082 ;; Case 1. No source fontification.
2083 ((not org-latex-listings
)
2084 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2085 (float-env (and (not long-listing
)
2087 (format "\\begin{figure}[H]\n%s%%s\n\\end{figure}"
2091 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2092 (org-export-format-code-default src-block info
))))))
2093 ;; Case 2. Custom environment.
2094 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2096 (org-export-format-code-default src-block info
)
2098 ;; Case 3. Use minted package.
2099 ((eq org-latex-listings
'minted
)
2101 (and (not long-listing
)
2103 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2104 (org-latex--caption/label-string src-block info
))))
2107 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2109 (org-latex--make-option-string
2110 (if (or (not num-start
)
2111 (assoc "linenos" org-latex-minted-options
))
2112 org-latex-minted-options
2113 (append `(("linenos")
2114 ("firstnumber" ,(number-to-string (1+ num-start
))))
2115 org-latex-minted-options
)))
2117 (or (cadr (assq (intern lang
) org-latex-minted-langs
)) lang
)
2119 (let* ((code-info (org-export-unravel-code src-block
))
2123 (org-split-string (car code-info
)
2125 (org-export-format-code
2127 (lambda (loc num ref
)
2131 ;; Ensure references are flushed to the right,
2132 ;; separated with 6 spaces from the widest line
2134 (concat (make-string (+ (- max-width
(length loc
)) 6)
2136 (format "(%s)" ref
)))))
2137 nil
(and retain-labels
(cdr code-info
)))))))
2139 (if float-env
(format float-env body
) body
)))
2140 ;; Case 4. Use listings package.
2143 (or (cadr (assq (intern lang
) org-latex-listings-langs
)) lang
))
2146 (let ((main (org-export-get-caption src-block
))
2147 (secondary (org-export-get-caption src-block t
)))
2149 (format "{%s}" (org-export-data main info
))
2151 (org-export-data secondary info
)
2152 (org-export-data main info
)))))))
2155 (format "\\lstset{%s}\n"
2156 (org-latex--make-option-string
2158 org-latex-listings-options
2159 `(("language" ,lst-lang
))
2160 (when label
`(("label" ,label
)))
2161 (when caption-str
`(("caption" ,caption-str
)))
2162 (cond ((assoc "numbers" org-latex-listings-options
) nil
)
2163 ((not num-start
) '(("numbers" "none")))
2164 ((zerop num-start
) '(("numbers" "left")))
2165 (t `(("numbers" "left")
2167 ,(number-to-string (1+ num-start
)))))))))
2170 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2171 (let* ((code-info (org-export-unravel-code src-block
))
2175 (org-split-string (car code-info
) "\n")))))
2176 (org-export-format-code
2178 (lambda (loc num ref
)
2182 ;; Ensure references are flushed to the right,
2183 ;; separated with 6 spaces from the widest line of
2185 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2186 (format "(%s)" ref
)))))
2187 nil
(and retain-labels
(cdr code-info
))))))))))))
2190 ;;;; Statistics Cookie
2192 (defun org-latex-statistics-cookie (statistics-cookie contents info
)
2193 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2194 CONTENTS is nil. INFO is a plist holding contextual information."
2195 (replace-regexp-in-string
2196 "%" "\\%" (org-element-property :value statistics-cookie
) nil t
))
2201 (defun org-latex-strike-through (strike-through contents info
)
2202 "Transcode STRIKE-THROUGH from Org to LaTeX.
2203 CONTENTS is the text with strike-through markup. INFO is a plist
2204 holding contextual information."
2205 (org-latex--text-markup contents
'strike-through
))
2210 (defun org-latex--script-size (object info
)
2211 "Transcode a subscript or superscript object.
2212 OBJECT is an Org object. INFO is a plist used as a communication
2215 ;; Non-nil if object is already in a sub/superscript.
2216 (let ((parent object
))
2218 (while (setq parent
(org-export-get-parent parent
))
2219 (let ((type (org-element-type parent
)))
2220 (cond ((memq type
'(subscript superscript
))
2222 ((memq type org-element-all-elements
)
2223 (throw 'exit nil
))))))))
2224 (type (org-element-type object
))
2226 (org-element-map (org-element-contents object
)
2227 (cons 'plain-text org-element-all-objects
)
2229 (case (org-element-type obj
)
2230 ((entity latex-fragment
)
2231 (let ((data (org-trim (org-export-data obj info
))))
2233 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2237 (match-string 1 data
)
2238 (let ((blank (org-element-property :post-blank obj
)))
2239 (and blank
(> blank
0) "\\ "))))))
2242 (format "%s\\text{%s}" output
(org-export-data obj info
))))
2246 (org-export-data obj info
)
2247 (let ((blank (org-element-property :post-blank obj
)))
2248 (and blank
(> blank
0) "\\ ")))))))
2249 info nil org-element-recursive-objects
)
2250 ;; Result. Do not wrap into math mode if already in a subscript
2251 ;; or superscript. Do not wrap into curly brackets if OUTPUT is
2252 ;; a single character. Also merge consecutive subscript and
2253 ;; superscript into the same math snippet.
2254 (concat (and (not in-script-p
)
2255 (let ((prev (org-export-get-previous-element object info
)))
2257 (not (eq (org-element-type prev
)
2258 (if (eq type
'subscript
) 'superscript
2260 (let ((blank (org-element-property :post-blank prev
)))
2261 (and blank
(> blank
0)))))
2263 (if (eq (org-element-type object
) 'subscript
) "_" "^")
2264 (and (> (length output
) 1) "{")
2266 (and (> (length output
) 1) "}")
2267 (and (not in-script-p
)
2268 (or (let ((blank (org-element-property :post-blank object
)))
2269 (and blank
(> blank
0)))
2270 (not (eq (org-element-type
2271 (org-export-get-next-element object info
))
2272 (if (eq type
'subscript
) 'superscript
2276 (defun org-latex-subscript (subscript contents info
)
2277 "Transcode a SUBSCRIPT object from Org to LaTeX.
2278 CONTENTS is the contents of the object. INFO is a plist holding
2279 contextual information."
2280 (org-latex--script-size subscript info
))
2285 (defun org-latex-superscript (superscript contents info
)
2286 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2287 CONTENTS is the contents of the object. INFO is a plist holding
2288 contextual information."
2289 (org-latex--script-size superscript info
))
2294 ;; `org-latex-table' is the entry point for table transcoding. It
2295 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2296 ;; delegates the job to either `org-latex--table.el-table',
2297 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2298 ;; depending of the type of the table and the mode requested.
2300 ;; `org-latex--align-string' is a subroutine used to build alignment
2301 ;; string for Org tables.
2303 (defun org-latex-table (table contents info
)
2304 "Transcode a TABLE element from Org to LaTeX.
2305 CONTENTS is the contents of the table. INFO is a plist holding
2306 contextual information."
2307 (if (eq (org-element-property :type table
) 'table.el
)
2308 ;; "table.el" table. Convert it using appropriate tools.
2309 (org-latex--table.el-table table info
)
2310 (let ((type (or (org-export-read-attribute :attr_latex table
:mode
)
2311 org-latex-default-table-mode
)))
2313 ;; Case 1: Verbatim table.
2314 ((string= type
"verbatim")
2315 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2316 ;; Re-create table, without affiliated keywords.
2317 (org-trim (org-element-interpret-data
2318 `(table nil
,@(org-element-contents table
))))))
2320 ((or (string= type
"math") (string= type
"inline-math"))
2321 (org-latex--math-table table info
))
2322 ;; Case 3: Standard table.
2323 (t (concat (org-latex--org-table table contents info
)
2324 ;; When there are footnote references within the
2325 ;; table, insert their definition just after it.
2326 (org-latex--delayed-footnotes-definitions table info
)))))))
2328 (defun org-latex--align-string (table info
)
2329 "Return an appropriate LaTeX alignment string.
2330 TABLE is the considered table. INFO is a plist used as
2331 a communication channel."
2332 (or (org-export-read-attribute :attr_latex table
:align
)
2334 ;; Extract column groups and alignment from first (non-rule)
2337 (org-element-map table
'table-row
2339 (and (eq (org-element-property :type row
) 'standard
) row
))
2343 (let ((borders (org-export-table-cell-borders cell info
)))
2344 ;; Check left border for the first cell only.
2345 (when (and (memq 'left borders
) (not align
))
2347 (push (case (org-export-table-cell-alignment cell info
)
2352 (when (memq 'right borders
) (push "|" align
))))
2354 (apply 'concat
(nreverse align
)))))
2356 (defun org-latex--org-table (table contents info
)
2357 "Return appropriate LaTeX code for an Org table.
2359 TABLE is the table type element to transcode. CONTENTS is its
2360 contents, as a string. INFO is a plist used as a communication
2363 This function assumes TABLE has `org' as its `:type' property and
2364 `table' as its `:mode' attribute."
2365 (let* ((caption (org-latex--caption/label-string table info
))
2366 (attr (org-export-read-attribute :attr_latex table
))
2367 ;; Determine alignment string.
2368 (alignment (org-latex--align-string table info
))
2369 ;; Determine environment for the table: longtable, tabular...
2370 (table-env (or (plist-get attr
:environment
)
2371 org-latex-default-table-environment
))
2372 ;; If table is a float, determine environment: table, table*
2373 ;; or sidewaystable.
2374 (float-env (unless (member table-env
'("longtable" "longtabu"))
2375 (let ((float (plist-get attr
:float
)))
2377 ((string= float
"sidewaystable") "sidewaystable")
2378 ((string= float
"multicolumn") "table*")
2379 ((or (string= float
"table")
2380 (org-element-property :caption table
))
2382 ;; Extract others display options.
2383 (fontsize (let ((font (plist-get attr
:font
)))
2384 (and font
(concat font
"\n"))))
2385 (width (plist-get attr
:width
))
2386 (spreadp (plist-get attr
:spread
))
2387 (placement (or (plist-get attr
:placement
)
2388 (format "[%s]" org-latex-default-figure-position
)))
2389 (centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2390 org-latex-tables-centered
)))
2391 ;; Prepare the final format string for the table.
2394 ((equal "longtable" table-env
)
2395 (concat (and fontsize
(concat "{" fontsize
))
2396 (format "\\begin{longtable}{%s}\n" alignment
)
2397 (and org-latex-table-caption-above
2398 (org-string-nw-p caption
)
2399 (concat caption
"\\\\\n"))
2401 (and (not org-latex-table-caption-above
)
2402 (org-string-nw-p caption
)
2403 (concat caption
"\\\\\n"))
2404 "\\end{longtable}\n"
2405 (and fontsize
"}")))
2407 ((equal "longtabu" table-env
)
2408 (concat (and fontsize
(concat "{" fontsize
))
2409 (format "\\begin{longtabu}%s{%s}\n"
2412 (if spreadp
"spread" "to") width
) "")
2414 (and org-latex-table-caption-above
2415 (org-string-nw-p caption
)
2416 (concat caption
"\\\\\n"))
2418 (and (not org-latex-table-caption-above
)
2419 (org-string-nw-p caption
)
2420 (concat caption
"\\\\\n"))
2422 (and fontsize
"}")))
2426 (concat (format "\\begin{%s}%s\n" float-env placement
)
2427 (if org-latex-table-caption-above caption
"")
2428 (when centerp
"\\centering\n")
2430 (centerp (concat "\\begin{center}\n" fontsize
))
2431 (fontsize (concat "{" fontsize
)))
2432 (cond ((equal "tabu" table-env
)
2433 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2435 (if spreadp
" spread %s " " to %s ")
2439 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2441 (if width
(format "{%s}" width
) "")
2447 (concat (if org-latex-table-caption-above
"" caption
)
2448 (format "\n\\end{%s}" float-env
)))
2449 (centerp "\n\\end{center}")
2450 (fontsize "}")))))))
2452 (defun org-latex--table.el-table
(table info
)
2453 "Return appropriate LaTeX code for a table.el table.
2455 TABLE is the table type element to transcode. INFO is a plist
2456 used as a communication channel.
2458 This function assumes TABLE has `table.el' as its `:type'
2461 ;; Ensure "*org-export-table*" buffer is empty.
2462 (with-current-buffer (get-buffer-create "*org-export-table*")
2464 (let ((output (with-temp-buffer
2465 (insert (org-element-property :value table
))
2467 (re-search-forward "^[ \t]*|[^|]" nil t
)
2468 (table-generate-source 'latex
"*org-export-table*")
2469 (with-current-buffer "*org-export-table*"
2470 (org-trim (buffer-string))))))
2471 (kill-buffer (get-buffer "*org-export-table*"))
2472 ;; Remove left out comments.
2473 (while (string-match "^%.*\n" output
)
2474 (setq output
(replace-match "" t t output
)))
2475 (let ((attr (org-export-read-attribute :attr_latex table
)))
2476 (when (plist-get attr
:rmlines
)
2477 ;; When the "rmlines" attribute is provided, remove all hlines
2478 ;; but the the one separating heading from the table body.
2479 (let ((n 0) (pos 0))
2480 (while (and (< (length output
) pos
)
2481 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
2483 (unless (= n
2) (setq output
(replace-match "" nil nil output
))))))
2484 (let ((centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2485 org-latex-tables-centered
)))
2486 (if (not centerp
) output
2487 (format "\\begin{center}\n%s\n\\end{center}" output
))))))
2489 (defun org-latex--math-table (table info
)
2490 "Return appropriate LaTeX code for a matrix.
2492 TABLE is the table type element to transcode. INFO is a plist
2493 used as a communication channel.
2495 This function assumes TABLE has `org' as its `:type' property and
2496 `inline-math' or `math' as its `:mode' attribute.."
2497 (let* ((caption (org-latex--caption/label-string table info
))
2498 (attr (org-export-read-attribute :attr_latex table
))
2499 (inlinep (equal (plist-get attr
:mode
) "inline-math"))
2500 (env (or (plist-get attr
:environment
)
2501 org-latex-default-table-environment
))
2505 ;; Ignore horizontal rules.
2506 (when (eq (org-element-property :type row
) 'standard
)
2507 ;; Return each cell unmodified.
2511 (substring (org-element-interpret-data cell
) 0 -
1))
2512 (org-element-map row
'table-cell
'identity info
) "&")
2513 (or (cdr (assoc env org-latex-table-matrix-macros
)) "\\\\")
2515 (org-element-map table
'table-row
'identity info
) ""))
2516 ;; Variables related to math clusters (contiguous math tables
2517 ;; of the same type).
2518 (mode (org-export-read-attribute :attr_latex table
:mode
))
2519 (prev (org-export-get-previous-element table info
))
2520 (next (org-export-get-next-element table info
))
2523 ;; Non-nil when TABLE has the same mode as current table.
2524 (string= (or (org-export-read-attribute :attr_latex table
:mode
)
2525 org-latex-default-table-mode
)
2528 ;; Opening string. If TABLE is in the middle of a table cluster,
2529 ;; do not insert any.
2531 (eq (org-element-type prev
) 'table
)
2532 (memq (org-element-property :post-blank prev
) '(0 nil
))
2533 (funcall same-mode-p prev
))
2536 ((org-string-nw-p caption
) (concat "\\begin{equation}\n" caption
))
2539 (or (plist-get attr
:math-prefix
) "")
2540 ;; Environment. Also treat special cases.
2541 (cond ((equal env
"array")
2542 (let ((align (org-latex--align-string table info
)))
2543 (format "\\begin{array}{%s}\n%s\\end{array}" align contents
)))
2544 ((assoc env org-latex-table-matrix-macros
)
2545 (format "\\%s%s{\n%s}"
2547 (or (plist-get attr
:math-arguments
) "")
2549 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env
)))
2551 (or (plist-get attr
:math-suffix
) "")
2552 ;; Closing string. If TABLE is in the middle of a table cluster,
2553 ;; do not insert any. If it closes such a cluster, be sure to
2554 ;; close the cluster with a string matching the opening string.
2556 (eq (org-element-type next
) 'table
)
2557 (memq (org-element-property :post-blank table
) '(0 nil
))
2558 (funcall same-mode-p next
))
2561 ;; Find cluster beginning to know which environment to use.
2562 ((let ((cluster-beg table
) prev
)
2563 (while (and (setq prev
(org-export-get-previous-element
2565 (memq (org-element-property :post-blank prev
)
2567 (funcall same-mode-p prev
))
2568 (setq cluster-beg prev
))
2569 (and (or (org-element-property :caption cluster-beg
)
2570 (org-element-property :name cluster-beg
))
2571 "\n\\end{equation}")))
2577 (defun org-latex-table-cell (table-cell contents info
)
2578 "Transcode a TABLE-CELL element from Org to LaTeX.
2579 CONTENTS is the cell contents. INFO is a plist used as
2580 a communication channel."
2581 (concat (if (and contents
2582 org-latex-table-scientific-notation
2583 (string-match orgtbl-exp-regexp contents
))
2584 ;; Use appropriate format string for scientific
2586 (format org-latex-table-scientific-notation
2587 (match-string 1 contents
)
2588 (match-string 2 contents
))
2590 (when (org-export-get-next-element table-cell info
) " & ")))
2595 (defun org-latex-table-row (table-row contents info
)
2596 "Transcode a TABLE-ROW element from Org to LaTeX.
2597 CONTENTS is the contents of the row. INFO is a plist used as
2598 a communication channel."
2599 ;; Rules are ignored since table separators are deduced from
2600 ;; borders of the current row.
2601 (when (eq (org-element-property :type table-row
) 'standard
)
2602 (let* ((attr (org-export-read-attribute :attr_latex
2603 (org-export-get-parent table-row
)))
2604 (longtablep (member (or (plist-get attr
:environment
)
2605 org-latex-default-table-environment
)
2606 '("longtable" "longtabu")))
2607 (booktabsp (if (plist-member attr
:booktabs
)
2608 (plist-get attr
:booktabs
)
2609 org-latex-tables-booktabs
))
2610 ;; TABLE-ROW's borders are extracted from its first cell.
2611 (borders (org-export-table-cell-borders
2612 (car (org-element-contents table-row
)) info
)))
2614 ;; When BOOKTABS are activated enforce top-rule even when no
2615 ;; hline was specifically marked.
2616 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2617 ((and (memq 'top borders
) (memq 'above borders
)) "\\hline\n"))
2620 ;; Special case for long tables. Define header and footers.
2621 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
2624 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2627 (if booktabsp
"\\midrule" "\\hline")
2628 (if booktabsp
"\\midrule" "\\hline")
2629 ;; Number of columns.
2630 (cdr (org-export-table-dimensions
2631 (org-export-get-parent-table table-row
) info
))))
2632 ;; When BOOKTABS are activated enforce bottom rule even when
2633 ;; no hline was specifically marked.
2634 ((and booktabsp
(memq 'bottom borders
)) "\\bottomrule")
2635 ((and (memq 'bottom borders
) (memq 'below borders
)) "\\hline")
2636 ((memq 'below borders
) (if booktabsp
"\\midrule" "\\hline")))))))
2641 (defun org-latex-target (target contents info
)
2642 "Transcode a TARGET object from Org to LaTeX.
2643 CONTENTS is nil. INFO is a plist holding contextual
2645 (format "\\label{%s}"
2646 (org-export-solidify-link-text (org-element-property :value target
))))
2651 (defun org-latex-timestamp (timestamp contents info
)
2652 "Transcode a TIMESTAMP object from Org to LaTeX.
2653 CONTENTS is nil. INFO is a plist holding contextual
2655 (let ((value (org-latex-plain-text
2656 (org-timestamp-translate timestamp
) info
)))
2657 (case (org-element-property :type timestamp
)
2658 ((active active-range
) (format org-latex-active-timestamp-format value
))
2659 ((inactive inactive-range
)
2660 (format org-latex-inactive-timestamp-format value
))
2661 (otherwise (format org-latex-diary-timestamp-format value
)))))
2666 (defun org-latex-underline (underline contents info
)
2667 "Transcode UNDERLINE from Org to LaTeX.
2668 CONTENTS is the text with underline markup. INFO is a plist
2669 holding contextual information."
2670 (org-latex--text-markup contents
'underline
))
2675 (defun org-latex-verbatim (verbatim contents info
)
2676 "Transcode a VERBATIM object from Org to LaTeX.
2677 CONTENTS is nil. INFO is a plist used as a communication
2679 (org-latex--text-markup (org-element-property :value verbatim
) 'verbatim
))
2684 (defun org-latex-verse-block (verse-block contents info
)
2685 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2686 CONTENTS is verse block contents. INFO is a plist holding
2687 contextual information."
2688 (org-latex--wrap-label
2690 ;; In a verse environment, add a line break to each newline
2691 ;; character and change each white space at beginning of a line
2692 ;; into a space of 1 em. Also change each blank line with
2693 ;; a vertical space of 1 em.
2695 (setq contents
(replace-regexp-in-string
2696 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2697 (replace-regexp-in-string
2698 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents
)))
2699 (while (string-match "^[ \t]+" contents
)
2700 (let ((new-str (format "\\hspace*{%dem}"
2701 (length (match-string 0 contents
)))))
2702 (setq contents
(replace-match new-str nil t contents
))))
2703 (format "\\begin{verse}\n%s\\end{verse}" contents
))))
2707 ;;; End-user functions
2710 (defun org-latex-export-as-latex
2711 (&optional async subtreep visible-only body-only ext-plist
)
2712 "Export current buffer as a LaTeX buffer.
2714 If narrowing is active in the current buffer, only export its
2717 If a region is active, export that region.
2719 A non-nil optional argument ASYNC means the process should happen
2720 asynchronously. The resulting buffer should be accessible
2721 through the `org-export-stack' interface.
2723 When optional argument SUBTREEP is non-nil, export the sub-tree
2724 at point, extracting information from the headline properties
2727 When optional argument VISIBLE-ONLY is non-nil, don't export
2728 contents of hidden elements.
2730 When optional argument BODY-ONLY is non-nil, only write code
2731 between \"\\begin{document}\" and \"\\end{document}\".
2733 EXT-PLIST, when provided, is a property list with external
2734 parameters overriding Org default settings, but still inferior to
2735 file-local settings.
2737 Export is done in a buffer named \"*Org LATEX Export*\", which
2738 will be displayed when `org-export-show-temporary-export-buffer'
2742 (org-export-async-start
2744 (with-current-buffer (get-buffer-create "*Org LATEX Export*")
2747 (goto-char (point-min))
2749 (org-export-add-to-stack (current-buffer) 'latex
)))
2750 `(org-export-as 'latex
,subtreep
,visible-only
,body-only
2753 (org-export-to-buffer 'latex
"*Org LATEX Export*"
2754 subtreep visible-only body-only ext-plist
)))
2755 (with-current-buffer outbuf
(LaTeX-mode))
2756 (when org-export-show-temporary-export-buffer
2757 (switch-to-buffer-other-window outbuf
)))))
2760 (defun org-latex-convert-region-to-latex ()
2761 "Assume the current region has org-mode syntax, and convert it to LaTeX.
2762 This can be used in any buffer. For example, you can write an
2763 itemized list in org-mode syntax in an LaTeX buffer and use this
2764 command to convert it."
2766 (org-export-replace-region-by 'latex
))
2769 (defun org-latex-export-to-latex
2770 (&optional async subtreep visible-only body-only ext-plist
)
2771 "Export current buffer to a LaTeX file.
2773 If narrowing is active in the current buffer, only export its
2776 If a region is active, export that region.
2778 A non-nil optional argument ASYNC means the process should happen
2779 asynchronously. The resulting file should be accessible through
2780 the `org-export-stack' interface.
2782 When optional argument SUBTREEP is non-nil, export the sub-tree
2783 at point, extracting information from the headline properties
2786 When optional argument VISIBLE-ONLY is non-nil, don't export
2787 contents of hidden elements.
2789 When optional argument BODY-ONLY is non-nil, only write code
2790 between \"\\begin{document}\" and \"\\end{document}\".
2792 EXT-PLIST, when provided, is a property list with external
2793 parameters overriding Org default settings, but still inferior to
2794 file-local settings.
2796 Return output file's name."
2798 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
2800 (org-export-async-start
2801 (lambda (f) (org-export-add-to-stack f
'latex
))
2804 'latex
,outfile
,subtreep
,visible-only
,body-only
',ext-plist
)))
2806 'latex outfile subtreep visible-only body-only ext-plist
))))
2809 (defun org-latex-export-to-pdf
2810 (&optional async subtreep visible-only body-only ext-plist
)
2811 "Export current buffer to LaTeX then process through to PDF.
2813 If narrowing is active in the current buffer, only export its
2816 If a region is active, export that region.
2818 A non-nil optional argument ASYNC means the process should happen
2819 asynchronously. The resulting file should be accessible through
2820 the `org-export-stack' interface.
2822 When optional argument SUBTREEP is non-nil, export the sub-tree
2823 at point, extracting information from the headline properties
2826 When optional argument VISIBLE-ONLY is non-nil, don't export
2827 contents of hidden elements.
2829 When optional argument BODY-ONLY is non-nil, only write code
2830 between \"\\begin{document}\" and \"\\end{document}\".
2832 EXT-PLIST, when provided, is a property list with external
2833 parameters overriding Org default settings, but still inferior to
2834 file-local settings.
2836 Return PDF file's name."
2839 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
2840 (org-export-async-start
2841 (lambda (f) (org-export-add-to-stack f
'latex
))
2845 'latex
,outfile
,subtreep
,visible-only
,body-only
2848 (org-latex-export-to-latex
2849 nil subtreep visible-only body-only ext-plist
))))
2851 (defun org-latex-compile (texfile &optional snippet
)
2852 "Compile a TeX file.
2854 TEXFILE is the name of the file being compiled. Processing is
2855 done through the command specified in `org-latex-pdf-process'.
2857 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
2858 file used to preview a LaTeX snippet. In this case, do not
2859 create a log buffer and do not bother removing log files.
2861 Return PDF file name or an error if it couldn't be produced."
2862 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile
)))
2863 (full-name (file-truename texfile
))
2864 (out-dir (file-name-directory texfile
))
2865 ;; Make sure `default-directory' is set to TEXFILE directory,
2866 ;; not to whatever value the current buffer may have.
2867 (default-directory (file-name-directory full-name
))
2869 (unless snippet
(message (format "Processing LaTeX file %s..." texfile
)))
2870 (save-window-excursion
2872 ;; A function is provided: Apply it.
2873 ((functionp org-latex-pdf-process
)
2874 (funcall org-latex-pdf-process
(shell-quote-argument texfile
)))
2875 ;; A list is provided: Replace %b, %f and %o with appropriate
2876 ;; values in each command before applying it. Output is
2877 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2878 ((consp org-latex-pdf-process
)
2879 (let ((outbuf (and (not snippet
)
2880 (get-buffer-create "*Org PDF LaTeX Output*"))))
2884 (replace-regexp-in-string
2885 "%b" (shell-quote-argument base-name
)
2886 (replace-regexp-in-string
2887 "%f" (shell-quote-argument full-name
)
2888 (replace-regexp-in-string
2889 "%o" (shell-quote-argument out-dir
) command t t
) t t
) t t
)
2891 org-latex-pdf-process
)
2892 ;; Collect standard errors from output buffer.
2893 (setq errors
(and (not snippet
) (org-latex--collect-errors outbuf
)))))
2894 (t (error "No valid command to process to PDF")))
2895 (let ((pdffile (concat out-dir base-name
".pdf")))
2896 ;; Check for process failure. Provide collected errors if
2898 (if (not (file-exists-p pdffile
))
2899 (error (concat (format "PDF file %s wasn't produced" pdffile
)
2900 (when errors
(concat ": " errors
))))
2901 ;; Else remove log files, when specified, and signal end of
2902 ;; process to user, along with any error encountered.
2903 (when (and (not snippet
) org-latex-remove-logfiles
)
2904 (dolist (ext org-latex-logfiles-extensions
)
2905 (let ((file (concat out-dir base-name
"." ext
)))
2906 (when (file-exists-p file
) (delete-file file
)))))
2907 (message (concat "Process completed"
2908 (if (not errors
) "."
2909 (concat " with errors: " errors
)))))
2910 ;; Return output file name.
2913 (defun org-latex--collect-errors (buffer)
2914 "Collect some kind of errors from \"pdflatex\" command output.
2916 BUFFER is the buffer containing output.
2918 Return collected error types as a string, or nil if there was
2920 (with-current-buffer buffer
2922 (goto-char (point-max))
2923 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t
)
2924 (let ((case-fold-search t
)
2926 (dolist (latex-error org-latex-known-errors
)
2927 (when (save-excursion (re-search-forward (car latex-error
) nil t
))
2928 (setq errors
(concat errors
" " (cdr latex-error
)))))
2929 (and (org-string-nw-p errors
) (org-trim errors
)))))))
2932 (defun org-latex-publish-to-latex (plist filename pub-dir
)
2933 "Publish an Org file to LaTeX.
2935 FILENAME is the filename of the Org file to be published. PLIST
2936 is the property list for the given project. PUB-DIR is the
2937 publishing directory.
2939 Return output file name."
2940 (org-publish-org-to 'latex filename
".tex" plist pub-dir
))
2943 (defun org-latex-publish-to-pdf (plist filename pub-dir
)
2944 "Publish an Org file to PDF (via LaTeX).
2946 FILENAME is the filename of the Org file to be published. PLIST
2947 is the property list for the given project. PUB-DIR is the
2948 publishing directory.
2950 Return output file name."
2951 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
2952 ;; in working directory and then moved to publishing directory.
2953 (org-publish-attachment
2955 (org-latex-compile (org-publish-org-to 'latex filename
".tex" plist
))
2962 ;; generated-autoload-file: "org-loaddefs.el"
2965 ;;; ox-latex.el ends here