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 (node-property . org-latex-node-property
)
67 (paragraph . org-latex-paragraph
)
68 (plain-list . org-latex-plain-list
)
69 (plain-text . org-latex-plain-text
)
70 (planning . org-latex-planning
)
71 (property-drawer . org-latex-property-drawer
)
72 (quote-block . org-latex-quote-block
)
73 (quote-section . org-latex-quote-section
)
74 (radio-target . org-latex-radio-target
)
75 (section . org-latex-section
)
76 (special-block . org-latex-special-block
)
77 (src-block . org-latex-src-block
)
78 (statistics-cookie . org-latex-statistics-cookie
)
79 (strike-through . org-latex-strike-through
)
80 (subscript . org-latex-subscript
)
81 (superscript . org-latex-superscript
)
82 (table . org-latex-table
)
83 (table-cell . org-latex-table-cell
)
84 (table-row . org-latex-table-row
)
85 (target . org-latex-target
)
86 (template . org-latex-template
)
87 (timestamp . org-latex-timestamp
)
88 (underline . org-latex-underline
)
89 (verbatim . org-latex-verbatim
)
90 (verse-block . org-latex-verse-block
))
91 :export-block
'("LATEX" "TEX")
93 '(?l
"Export to LaTeX"
94 ((?L
"As LaTeX buffer" org-latex-export-as-latex
)
95 (?l
"As LaTeX file" org-latex-export-to-latex
)
96 (?p
"As PDF file" org-latex-export-to-pdf
)
97 (?o
"As PDF file and open"
99 (if a
(org-latex-export-to-pdf t s v b
)
100 (org-open-file (org-latex-export-to-pdf nil s v b
)))))))
101 :options-alist
'((:latex-class
"LATEX_CLASS" nil org-latex-default-class t
)
102 (:latex-class-options
"LATEX_CLASS_OPTIONS" nil nil t
)
103 (:latex-header
"LATEX_HEADER" nil nil newline
)
104 (:latex-header-extra
"LATEX_HEADER_EXTRA" nil nil newline
)
105 (:latex-hyperref-p nil
"texht" org-latex-with-hyperref t
)
106 ;; Redefine regular options.
107 (:date
"DATE" nil
"\\today" t
)))
111 ;;; Internal Variables
113 (defconst org-latex-babel-language-alist
114 '(("af" .
"afrikaans")
116 ("bt-br" .
"brazilian")
122 ("de-at" .
"naustrian")
123 ("de-de" .
"ngerman")
126 ("en-au" .
"australian")
127 ("en-ca" .
"canadian")
128 ("en-gb" .
"british")
130 ("en-nz" .
"newzealand")
131 ("en-us" .
"american")
137 ("fr-ca" .
"canadien")
141 ("id" .
"indonesian")
151 ("pt" .
"portuguese")
155 ("sb" .
"uppersorbian")
163 ("uk" .
"ukrainian"))
164 "Alist between language code and corresponding Babel option.")
166 (defconst org-latex-table-matrix-macros
'(("bordermatrix" .
"\\cr")
167 ("qbordermatrix" .
"\\cr")
168 ("kbordermatrix" .
"\\\\"))
169 "Alist between matrix macros and their row ending.")
173 ;;; User Configurable Variables
175 (defgroup org-export-latex nil
176 "Options for exporting Org mode files to LaTeX."
177 :tag
"Org Export LaTeX"
183 (defcustom org-latex-default-class
"article"
184 "The default LaTeX class."
185 :group
'org-export-latex
186 :type
'(string :tag
"LaTeX class"))
188 (defcustom org-latex-classes
190 "\\documentclass[11pt]{article}"
191 ("\\section{%s}" .
"\\section*{%s}")
192 ("\\subsection{%s}" .
"\\subsection*{%s}")
193 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
194 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
195 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
197 "\\documentclass[11pt]{report}"
198 ("\\part{%s}" .
"\\part*{%s}")
199 ("\\chapter{%s}" .
"\\chapter*{%s}")
200 ("\\section{%s}" .
"\\section*{%s}")
201 ("\\subsection{%s}" .
"\\subsection*{%s}")
202 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
204 "\\documentclass[11pt]{book}"
205 ("\\part{%s}" .
"\\part*{%s}")
206 ("\\chapter{%s}" .
"\\chapter*{%s}")
207 ("\\section{%s}" .
"\\section*{%s}")
208 ("\\subsection{%s}" .
"\\subsection*{%s}")
209 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
210 "Alist of LaTeX classes and associated header and structure.
211 If #+LATEX_CLASS is set in the buffer, use its value and the
212 associated information. Here is the structure of each cell:
216 \(numbered-section . unnumbered-section)
222 The HEADER-STRING is the header that will be inserted into the
223 LaTeX file. It should contain the \\documentclass macro, and
224 anything else that is needed for this setup. To this header, the
225 following commands will be added:
227 - Calls to \\usepackage for all packages mentioned in the
228 variables `org-latex-default-packages-alist' and
229 `org-latex-packages-alist'. Thus, your header definitions
230 should avoid to also request these packages.
232 - Lines specified via \"#+LATEX_HEADER:\" and
233 \"#+LATEX_HEADER_EXTRA:\" keywords.
235 If you need more control about the sequence in which the header
236 is built up, or if you want to exclude one of these building
237 blocks for a particular class, you can use the following
238 macro-like placeholders.
240 [DEFAULT-PACKAGES] \\usepackage statements for default packages
241 [NO-DEFAULT-PACKAGES] do not include any of the default packages
242 [PACKAGES] \\usepackage statements for packages
243 [NO-PACKAGES] do not include the packages
244 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
245 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
249 \\documentclass{article}
250 [NO-DEFAULT-PACKAGES]
252 \\providecommand{\\alert}[1]{\\textbf{#1}}
255 will omit the default packages, and will include the
256 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
257 to \\providecommand, and then place \\usepackage commands based
258 on the content of `org-latex-packages-alist'.
260 If your header, `org-latex-default-packages-alist' or
261 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
262 AUTO will automatically be replaced with a coding system derived
263 from `buffer-file-coding-system'. See also the variable
264 `org-latex-inputenc-alist' for a way to influence this mechanism.
266 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\",
267 AUTO will be replaced with the language related to the language
268 code specified by `org-export-default-language', which see. Note
269 that constructions such as \"\\usepackage[french,AUTO,english]{babel}\"
272 The sectioning structure
273 ------------------------
275 The sectioning structure of the class is given by the elements
276 following the header string. For each sectioning level, a number
277 of strings is specified. A %s formatter is mandatory in each
278 section string and will be replaced by the title of the section.
280 Instead of a cons cell (numbered . unnumbered), you can also
281 provide a list of 2 or 4 elements,
283 \(numbered-open numbered-close)
287 \(numbered-open numbered-close unnumbered-open unnumbered-close)
289 providing opening and closing strings for a LaTeX environment
290 that should represent the document section. The opening clause
291 should have a %s to represent the section title.
293 Instead of a list of sectioning commands, you can also specify
294 a function name. That function will be called with two
295 parameters, the (reduced) level of the headline, and a predicate
296 non-nil when the headline should be numbered. It must return
297 a format string in which the section title will be added."
298 :group
'org-export-latex
300 (list (string :tag
"LaTeX class")
301 (string :tag
"LaTeX header")
302 (repeat :tag
"Levels" :inline t
305 (string :tag
" numbered")
306 (string :tag
"unnumbered"))
307 (list :tag
"Environment"
308 (string :tag
"Opening (numbered)")
309 (string :tag
"Closing (numbered)")
310 (string :tag
"Opening (unnumbered)")
311 (string :tag
"Closing (unnumbered)"))
312 (function :tag
"Hook computing sectioning"))))))
314 (defcustom org-latex-inputenc-alist nil
315 "Alist of inputenc coding system names, and what should really be used.
316 For example, adding an entry
318 (\"utf8\" . \"utf8x\")
320 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
321 are written as utf8 files."
322 :group
'org-export-latex
325 (string :tag
"Derived from buffer")
326 (string :tag
"Use this instead"))))
328 (defcustom org-latex-title-command
"\\maketitle"
329 "The command used to insert the title just after \\begin{document}.
330 If this string contains the formatting specification \"%s\" then
331 it will be used as a formatting string, passing the title as an
333 :group
'org-export-latex
336 (defcustom org-latex-toc-command
"\\tableofcontents\n\n"
337 "LaTeX command to set the table of contents, list of figures, etc.
338 This command only applies to the table of contents generated with
339 the toc:nil option, not to those generated with #+TOC keyword."
340 :group
'org-export-latex
343 (defcustom org-latex-with-hyperref t
344 "Toggle insertion of \\hypersetup{...} in the preamble."
345 :group
'org-export-latex
351 (defcustom org-latex-format-headline-function
352 'org-latex-format-headline-default-function
353 "Function for formatting the headline's text.
355 This function will be called with 5 arguments:
356 TODO the todo keyword (string or nil).
357 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
358 PRIORITY the priority of the headline (integer or nil)
359 TEXT the main headline text (string).
360 TAGS the tags as a list of strings (list of strings or nil).
362 The function result will be used in the section format string.
364 Use `org-latex-format-headline-default-function' by default,
365 which format headlines like for Org version prior to 8.0."
366 :group
'org-export-latex
368 :package-version
'(Org .
"8.0")
374 (defcustom org-latex-footnote-separator
"\\textsuperscript{,}\\,"
375 "Text used to separate footnotes."
376 :group
'org-export-latex
382 (defcustom org-latex-active-timestamp-format
"\\textit{%s}"
383 "A printf format string to be applied to active timestamps."
384 :group
'org-export-latex
387 (defcustom org-latex-inactive-timestamp-format
"\\textit{%s}"
388 "A printf format string to be applied to inactive timestamps."
389 :group
'org-export-latex
392 (defcustom org-latex-diary-timestamp-format
"\\textit{%s}"
393 "A printf format string to be applied to diary timestamps."
394 :group
'org-export-latex
400 (defcustom org-latex-image-default-option
""
401 "Default option for images."
402 :group
'org-export-latex
404 :package-version
'(Org .
"8.0")
407 (defcustom org-latex-image-default-width
".9\\linewidth"
408 "Default width for images.
409 This value will not be used if a height is provided."
410 :group
'org-export-latex
412 :package-version
'(Org .
"8.0")
415 (defcustom org-latex-image-default-height
""
416 "Default height for images.
417 This value will not be used if a width is provided, or if the
418 image is wrapped within a \"figure\" or \"wrapfigure\"
420 :group
'org-export-latex
422 :package-version
'(Org .
"8.0")
425 (defcustom org-latex-default-figure-position
"htb"
426 "Default position for latex figures."
427 :group
'org-export-latex
430 (defcustom org-latex-inline-image-rules
431 '(("file" .
"\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
432 "Rules characterizing image files that can be inlined into LaTeX.
434 A rule consists in an association whose key is the type of link
435 to consider, and value is a regexp that will be matched against
438 Note that, by default, the image extension *actually* allowed
439 depend on the way the LaTeX file is processed. When used with
440 pdflatex, pdf, jpg and png images are OK. When processing
441 through dvi to Postscript, only ps and eps are allowed. The
442 default we use here encompasses both."
443 :group
'org-export-latex
445 :package-version
'(Org .
"8.0")
446 :type
'(alist :key-type
(string :tag
"Type")
447 :value-type
(regexp :tag
"Path")))
449 (defcustom org-latex-link-with-unknown-path-format
"\\texttt{%s}"
450 "Format string for links with unknown path type."
451 :group
'org-export-latex
457 (defcustom org-latex-default-table-environment
"tabular"
458 "Default environment used to build tables."
459 :group
'org-export-latex
461 :package-version
'(Org .
"8.0")
464 (defcustom org-latex-default-table-mode
'table
465 "Default mode for tables.
467 Value can be a symbol among:
469 `table' Regular LaTeX table.
471 `math' In this mode, every cell is considered as being in math
472 mode and the complete table will be wrapped within a math
473 environment. It is particularly useful to write matrices.
475 `inline-math' This mode is almost the same as `math', but the
476 math environment will be inlined.
478 `verbatim' The table is exported as it appears in the Org
479 buffer, within a verbatim environment.
481 This value can be overridden locally with, i.e. \":mode math\" in
484 When modifying this variable, it may be useful to change
485 `org-latex-default-table-environment' accordingly."
486 :group
'org-export-latex
488 :package-version
'(Org .
"8.0")
489 :type
'(choice (const :tag
"Table" table
)
490 (const :tag
"Matrix" math
)
491 (const :tag
"Inline matrix" inline-math
)
492 (const :tag
"Verbatim" verbatim
))
493 :safe
(lambda (s) (memq s
'(table math inline-math verbatim
))))
495 (defcustom org-latex-tables-centered t
496 "When non-nil, tables are exported in a center environment."
497 :group
'org-export-latex
501 (defcustom org-latex-tables-booktabs nil
502 "When non-nil, display tables in a formal \"booktabs\" style.
503 This option assumes that the \"booktabs\" package is properly
504 loaded in the header of the document. This value can be ignored
505 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
507 :group
'org-export-latex
509 :package-version
'(Org .
"8.0")
513 (defcustom org-latex-table-caption-above t
514 "When non-nil, place caption string at the beginning of the table.
515 Otherwise, place it near the end."
516 :group
'org-export-latex
520 (defcustom org-latex-table-scientific-notation
"%s\\,(%s)"
521 "Format string to display numbers in scientific notation.
522 The format should have \"%s\" twice, for mantissa and exponent
523 \(i.e., \"%s\\\\times10^{%s}\").
525 When nil, no transformation is made."
526 :group
'org-export-latex
528 :package-version
'(Org .
"8.0")
530 (string :tag
"Format string")
531 (const :tag
"No formatting")))
535 (defcustom org-latex-text-markup-alist
'((bold .
"\\textbf{%s}")
537 (italic .
"\\emph{%s}")
538 (strike-through .
"\\sout{%s}")
539 (underline .
"\\uline{%s}")
540 (verbatim . protectedtexttt
))
541 "Alist of LaTeX expressions to convert text markup.
543 The key must be a symbol among `bold', `code', `italic',
544 `strike-through', `underline' and `verbatim'. The value is
545 a formatting string to wrap fontified text with.
547 Value can also be set to the following symbols: `verb' and
548 `protectedtexttt'. For the former, Org will use \"\\verb\" to
549 create a format string and select a delimiter character that
550 isn't in the string. For the latter, Org will use \"\\texttt\"
551 to typeset and try to protect special characters.
553 If no association can be found for a given markup, text will be
555 :group
'org-export-latex
557 :options
'(bold code italic strike-through underline verbatim
))
562 (defcustom org-latex-format-drawer-function nil
563 "Function called to format a drawer in LaTeX code.
565 The function must accept two parameters:
566 NAME the drawer name, like \"LOGBOOK\"
567 CONTENTS the contents of the drawer.
569 The function should return the string to be exported.
571 For example, the variable could be set to the following function
572 in order to mimic default behaviour:
574 \(defun org-latex-format-drawer-default \(name contents\)
575 \"Format a drawer element for LaTeX export.\"
577 :group
'org-export-latex
583 (defcustom org-latex-format-inlinetask-function nil
584 "Function called to format an inlinetask in LaTeX code.
586 The function must accept six parameters:
587 TODO the todo keyword, as a string
588 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
589 PRIORITY the inlinetask priority, as a string
590 NAME the inlinetask name, as a string.
591 TAGS the inlinetask tags, as a list of strings.
592 CONTENTS the contents of the inlinetask, as a string.
594 The function should return the string to be exported.
596 For example, the variable could be set to the following function
597 in order to mimic default behaviour:
599 \(defun org-latex-format-inlinetask \(todo type priority name tags contents\)
600 \"Format an inline task element for LaTeX export.\"
604 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
605 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
608 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
609 \(mapconcat 'identity tags \":\")))))
610 \(format (concat \"\\\\begin{center}\\n\"
612 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
614 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
616 \"\\\\end{minipage}}\"
618 full-title contents))"
619 :group
'org-export-latex
625 (defcustom org-latex-listings nil
626 "Non-nil means export source code using the listings package.
628 This package will fontify source code, possibly even with color.
629 If you want to use this, you also need to make LaTeX use the
630 listings package, and if you want to have color, the color
631 package. Just add these to `org-latex-packages-alist', for
632 example using customize, or with something like:
635 \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
636 \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
640 \(setq org-latex-listings 'minted)
642 causes source code to be exported using the minted package as
643 opposed to listings. If you want to use minted, you need to add
644 the minted package to `org-latex-packages-alist', for example
645 using customize, or with
648 \(add-to-list 'org-latex-packages-alist '(\"\" \"minted\"))
650 In addition, it is necessary to install pygments
651 \(http://pygments.org), and to configure the variable
652 `org-latex-pdf-process' so that the -shell-escape option is
655 The minted choice has possible repercussions on the preview of
656 latex fragments (see `org-preview-latex-fragment'). If you run
657 into previewing problems, please consult
659 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
660 :group
'org-export-latex
662 (const :tag
"Use listings" t
)
663 (const :tag
"Use minted" minted
)
664 (const :tag
"Export verbatim" nil
))
665 :safe
(lambda (s) (memq s
'(t nil minted
))))
667 (defcustom org-latex-listings-langs
668 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
671 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
672 (html "HTML") (xml "XML")
673 (tex "TeX") (latex "[LaTeX]TeX")
674 (shell-script "bash")
676 (ocaml "Caml") (caml "Caml")
677 (sql "SQL") (sqlite "sql"))
678 "Alist mapping languages to their listing language counterpart.
679 The key is a symbol, the major mode symbol without the \"-mode\".
680 The value is the string that should be inserted as the language
681 parameter for the listings package. If the mode name and the
682 listings name are the same, the language does not need an entry
683 in this list - but it does not hurt if it is present."
684 :group
'org-export-latex
687 (symbol :tag
"Major mode ")
688 (string :tag
"Listings language"))))
690 (defcustom org-latex-listings-options nil
691 "Association list of options for the latex listings package.
693 These options are supplied as a comma-separated list to the
694 \\lstset command. Each element of the association list should be
695 a list containing two strings: the name of the option, and the
698 (setq org-latex-listings-options
699 '((\"basicstyle\" \"\\\\small\")
700 (\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
702 will typeset the code in a small size font with underlined, bold
705 Note that the same options will be applied to blocks of all
707 :group
'org-export-latex
710 (string :tag
"Listings option name ")
711 (string :tag
"Listings option value"))))
713 (defcustom org-latex-minted-langs
714 '((emacs-lisp "common-lisp")
717 (shell-script "bash")
719 "Alist mapping languages to their minted language counterpart.
720 The key is a symbol, the major mode symbol without the \"-mode\".
721 The value is the string that should be inserted as the language
722 parameter for the minted package. If the mode name and the
723 listings name are the same, the language does not need an entry
724 in this list - but it does not hurt if it is present.
726 Note that minted uses all lower case for language identifiers,
727 and that the full list of language identifiers can be obtained
730 pygmentize -L lexers"
731 :group
'org-export-latex
734 (symbol :tag
"Major mode ")
735 (string :tag
"Minted language"))))
737 (defcustom org-latex-minted-options nil
738 "Association list of options for the latex minted package.
740 These options are supplied within square brackets in
741 \\begin{minted} environments. Each element of the alist should
742 be a list containing two strings: the name of the option, and the
745 \(setq org-latex-minted-options
746 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
748 will result in src blocks being exported with
750 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
752 as the start of the minted environment. Note that the same
753 options will be applied to blocks of all languages."
754 :group
'org-export-latex
757 (string :tag
"Minted option name ")
758 (string :tag
"Minted option value"))))
760 (defvar org-latex-custom-lang-environments nil
761 "Alist mapping languages to language-specific LaTeX environments.
763 It is used during export of src blocks by the listings and minted
764 latex packages. For example,
766 \(setq org-latex-custom-lang-environments
767 '\(\(python \"pythoncode\"\)\)\)
769 would have the effect that if org encounters begin_src python
770 during latex export it will output
779 (defcustom org-latex-pdf-process
780 '("pdflatex -interaction nonstopmode -output-directory %o %f"
781 "pdflatex -interaction nonstopmode -output-directory %o %f"
782 "pdflatex -interaction nonstopmode -output-directory %o %f")
783 "Commands to process a LaTeX file to a PDF file.
784 This is a list of strings, each of them will be given to the
785 shell as a command. %f in the command will be replaced by the
786 full file name, %b by the file base name (i.e. without directory
787 and extension parts) and %o by the base directory of the file.
789 The reason why this is a list is that it usually takes several
790 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
791 does not have a clever mechanism to detect which of these
792 commands have to be run to get to a stable result, and it also
793 does not do any error checking.
795 By default, Org uses 3 runs of `pdflatex' to do the processing.
796 If you have texi2dvi on your system and if that does not cause
797 the infamous egrep/locale bug:
799 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
801 then `texi2dvi' is the superior choice as it automates the LaTeX
802 build process by calling the \"correct\" combinations of
803 auxiliary programs. Org does offer `texi2dvi' as one of the
804 customize options. Alternatively, `rubber' and `latexmk' also
805 provide similar functionality. The latter supports `biber' out
808 Alternatively, this may be a Lisp function that does the
809 processing, so you could use this to apply the machinery of
810 AUCTeX or the Emacs LaTeX mode. This function should accept the
811 file name as its single argument."
812 :group
'org-export-pdf
814 (repeat :tag
"Shell command sequence"
815 (string :tag
"Shell command"))
816 (const :tag
"2 runs of pdflatex"
817 ("pdflatex -interaction nonstopmode -output-directory %o %f"
818 "pdflatex -interaction nonstopmode -output-directory %o %f"))
819 (const :tag
"3 runs of pdflatex"
820 ("pdflatex -interaction nonstopmode -output-directory %o %f"
821 "pdflatex -interaction nonstopmode -output-directory %o %f"
822 "pdflatex -interaction nonstopmode -output-directory %o %f"))
823 (const :tag
"pdflatex,bibtex,pdflatex,pdflatex"
824 ("pdflatex -interaction nonstopmode -output-directory %o %f"
826 "pdflatex -interaction nonstopmode -output-directory %o %f"
827 "pdflatex -interaction nonstopmode -output-directory %o %f"))
828 (const :tag
"2 runs of xelatex"
829 ("xelatex -interaction nonstopmode -output-directory %o %f"
830 "xelatex -interaction nonstopmode -output-directory %o %f"))
831 (const :tag
"3 runs of xelatex"
832 ("xelatex -interaction nonstopmode -output-directory %o %f"
833 "xelatex -interaction nonstopmode -output-directory %o %f"
834 "xelatex -interaction nonstopmode -output-directory %o %f"))
835 (const :tag
"xelatex,bibtex,xelatex,xelatex"
836 ("xelatex -interaction nonstopmode -output-directory %o %f"
838 "xelatex -interaction nonstopmode -output-directory %o %f"
839 "xelatex -interaction nonstopmode -output-directory %o %f"))
840 (const :tag
"texi2dvi"
841 ("texi2dvi -p -b -V %f"))
843 ("rubber -d --into %o %f"))
844 (const :tag
"latexmk"
845 ("latexmk -g -pdf %f"))
848 (defcustom org-latex-logfiles-extensions
849 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
850 "The list of file extensions to consider as LaTeX logfiles.
851 The logfiles will be remove if `org-latex-remove-logfiles' is
853 :group
'org-export-latex
854 :type
'(repeat (string :tag
"Extension")))
856 (defcustom org-latex-remove-logfiles t
857 "Non-nil means remove the logfiles produced by PDF production.
858 By default, logfiles are files with these extensions: .aux, .idx,
859 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
860 logfiles to remove, set `org-latex-logfiles-extensions'."
861 :group
'org-export-latex
864 (defcustom org-latex-known-errors
865 '(("Reference.*?undefined" .
"[undefined reference]")
866 ("Citation.*?undefined" .
"[undefined citation]")
867 ("Undefined control sequence" .
"[undefined control sequence]")
868 ("^! LaTeX.*?Error" .
"[LaTeX error]")
869 ("^! Package.*?Error" .
"[package error]")
870 ("Runaway argument" .
"Runaway argument"))
871 "Alist of regular expressions and associated messages for the user.
872 The regular expressions are used to find possible errors in the
874 :group
'org-export-latex
876 :package-version
'(Org .
"8.0")
879 (string :tag
"Regexp")
880 (string :tag
"Message"))))
884 ;;; Internal Functions
886 (defun org-latex--caption/label-string
(element info
)
887 "Return caption and label LaTeX string for ELEMENT.
889 INFO is a plist holding contextual information. If there's no
890 caption nor label, return the empty string.
892 For non-floats, see `org-latex--wrap-label'."
893 (let* ((label (org-element-property :name element
))
894 (label-str (if (not (org-string-nw-p label
)) ""
895 (format "\\label{%s}"
896 (org-export-solidify-link-text label
))))
897 (main (org-export-get-caption element
))
898 (short (org-export-get-caption element t
))
899 (caption-from-attr-latex (org-export-read-attribute :attr_latex element
:caption
)))
901 ((org-string-nw-p caption-from-attr-latex
)
902 (concat caption-from-attr-latex
"\n"))
903 ((and (not main
) (equal label-str
"")) "")
904 ((not main
) (concat label-str
"\n"))
905 ;; Option caption format with short name.
906 (short (format "\\caption[%s]{%s%s}\n"
907 (org-export-data short info
)
909 (org-export-data main info
)))
910 ;; Standard caption format.
911 (t (format "\\caption{%s%s}\n" label-str
(org-export-data main info
))))))
913 (defun org-latex-guess-inputenc (header)
914 "Set the coding system in inputenc to what the buffer is.
916 HEADER is the LaTeX header string. This function only applies
917 when specified inputenc option is \"AUTO\".
919 Return the new header, as a string."
920 (let* ((cs (or (ignore-errors
921 (latexenc-coding-system-to-inputenc
922 (or org-export-coding-system buffer-file-coding-system
)))
925 ;; First translate if that is requested.
926 (setq cs
(or (cdr (assoc cs org-latex-inputenc-alist
)) cs
))
927 ;; Then find the \usepackage statement and replace the option.
928 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
929 cs header t nil
1))))
931 (defun org-latex-guess-babel-language (header info
)
932 "Set Babel's language according to LANGUAGE keyword.
934 HEADER is the LaTeX header string. INFO is the plist used as
935 a communication channel.
937 Insertion of guessed language only happens when Babel package has
938 explicitly been loaded. Then it is added to the rest of
941 The argument to Babel may be \"AUTO\" which is then replaced with
942 the language of the document or `org-export-default-language'
943 unless language in question is already loaded.
945 Return the new header."
946 (let ((language-code (plist-get info
:language
)))
947 ;; If no language is set or Babel package is not loaded, return
949 (if (or (not (stringp language-code
))
950 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header
)))
952 (let ((options (save-match-data
953 (org-split-string (match-string 1 header
) ",[ \t]*")))
954 (language (cdr (assoc language-code
955 org-latex-babel-language-alist
))))
956 ;; If LANGUAGE is already loaded, return header without AUTO.
957 ;; Otherwise, replace AUTO with language or append language if
958 ;; AUTO is not present.
960 (mapconcat (lambda (option) (if (equal "AUTO" option
) language option
))
961 (cond ((member language options
) (delete "AUTO" options
))
962 ((member "AUTO" options
) options
)
963 (t (append options
(list language
))))
967 (defun org-latex--find-verb-separator (s)
968 "Return a character not used in string S.
969 This is used to choose a separator for constructs like \\verb."
970 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
971 (loop for c across ll
972 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
973 return
(char-to-string c
))))
975 (defun org-latex--make-option-string (options)
976 "Return a comma separated string of keywords and values.
977 OPTIONS is an alist where the key is the options keyword as
978 a string, and the value a list containing the keyword value, or
980 (mapconcat (lambda (pair)
982 (when (> (length (second pair
)) 0)
983 (concat "=" (second pair
)))))
987 (defun org-latex--wrap-label (element output
)
988 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
989 This function shouldn't be used for floats. See
990 `org-latex--caption/label-string'."
991 (let ((label (org-element-property :name element
)))
992 (if (not (and (org-string-nw-p output
) (org-string-nw-p label
))) output
993 (concat (format "\\label{%s}\n" (org-export-solidify-link-text label
))
996 (defun org-latex--text-markup (text markup
)
997 "Format TEXT depending on MARKUP text markup.
998 See `org-latex-text-markup-alist' for details."
999 (let ((fmt (cdr (assq markup org-latex-text-markup-alist
))))
1001 ;; No format string: Return raw text.
1003 ;; Handle the `verb' special case: Find and appropriate separator
1004 ;; and use "\\verb" command.
1006 (let ((separator (org-latex--find-verb-separator text
)))
1007 (concat "\\verb" separator text separator
)))
1008 ;; Handle the `protectedtexttt' special case: Protect some
1009 ;; special chars and use "\texttt{%s}" format string.
1010 ((eq 'protectedtexttt fmt
)
1012 (trans '(("\\" .
"\\textbackslash{}")
1013 ("~" .
"\\textasciitilde{}")
1014 ("^" .
"\\textasciicircum{}")))
1017 (while (string-match "[\\{}$%&_#~^]" text
)
1018 (setq char
(match-string 0 text
))
1019 (if (> (match-beginning 0) 0)
1020 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
1021 (setq text
(substring text
(1+ (match-beginning 0))))
1022 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
1023 rtn
(concat rtn char
)))
1024 (setq text
(concat rtn text
)
1026 (while (string-match "--" text
)
1027 (setq text
(replace-match "-{}-" t t text
)))
1029 ;; Else use format string.
1030 (t (format fmt text
)))))
1032 (defun org-latex--delayed-footnotes-definitions (element info
)
1033 "Return footnotes definitions in ELEMENT as a string.
1035 INFO is a plist used as a communication channel.
1037 Footnotes definitions are returned within \"\\footnotetxt{}\"
1040 This function is used within constructs that don't support
1041 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1042 \"\\footnotemark\" is used within the construct and the function
1043 just outside of it."
1047 "\\footnotetext[%s]{%s}"
1048 (org-export-get-footnote-number ref info
)
1051 (org-export-get-footnote-definition ref info
) info
))))
1052 ;; Find every footnote reference in ELEMENT.
1054 search-refs
; For byte-compiler.
1058 ;; Return a list of all footnote references never seen
1060 (org-element-map data
'footnote-reference
1062 (when (org-export-footnote-first-reference-p ref info
)
1064 (when (eq (org-element-property :type ref
) 'standard
)
1065 (funcall search-refs
1066 (org-export-get-footnote-definition ref info
)))))
1068 (reverse all-refs
)))))
1069 (funcall search-refs element
))
1072 (defun org-latex--translate (s info
)
1073 "Translate string S according to specified language.
1074 INFO is a plist used as a communication channel."
1075 (org-export-translate s
:latex info
))
1081 (defun org-latex-template (contents info
)
1082 "Return complete document string after LaTeX conversion.
1083 CONTENTS is the transcoded contents string. INFO is a plist
1084 holding export options."
1085 (let ((title (org-export-data (plist-get info
:title
) info
)))
1088 (and (plist-get info
:time-stamp-file
)
1089 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1090 ;; Document class and packages.
1091 (let* ((class (plist-get info
:latex-class
))
1092 (class-options (plist-get info
:latex-class-options
))
1093 (header (nth 1 (assoc class org-latex-classes
)))
1094 (document-class-string
1095 (and (stringp header
)
1096 (if (not class-options
) header
1097 (replace-regexp-in-string
1098 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1099 class-options header t nil
1)))))
1100 (if (not document-class-string
)
1101 (user-error "Unknown LaTeX class `%s'" class
)
1102 (org-latex-guess-babel-language
1103 (org-latex-guess-inputenc
1104 (org-element-normalize-string
1105 (org-splice-latex-header
1106 document-class-string
1107 org-latex-default-packages-alist
1108 org-latex-packages-alist nil
1109 (concat (org-element-normalize-string
1110 (plist-get info
:latex-header
))
1111 (plist-get info
:latex-header-extra
)))))
1113 ;; Possibly limit depth for headline numbering.
1114 (let ((sec-num (plist-get info
:section-numbers
)))
1115 (when (integerp sec-num
)
1116 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
1118 (let ((author (and (plist-get info
:with-author
)
1119 (let ((auth (plist-get info
:author
)))
1120 (and auth
(org-export-data auth info
)))))
1121 (email (and (plist-get info
:with-email
)
1122 (org-export-data (plist-get info
:email
) info
))))
1123 (cond ((and author email
(not (string= "" email
)))
1124 (format "\\author{%s\\thanks{%s}}\n" author email
))
1125 ((or author email
) (format "\\author{%s}\n" (or author email
)))))
1127 (let ((date (and (plist-get info
:with-date
) (org-export-get-date info
))))
1128 (format "\\date{%s}\n" (org-export-data date info
)))
1130 (format "\\title{%s}\n" title
)
1131 ;; Hyperref options.
1132 (when (plist-get info
:latex-hyperref-p
)
1133 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1134 (or (plist-get info
:keywords
) "")
1135 (or (plist-get info
:description
) "")
1136 (if (not (plist-get info
:with-creator
)) ""
1137 (plist-get info
:creator
))))
1139 "\\begin{document}\n\n"
1141 (org-element-normalize-string
1142 (cond ((string= "" title
) nil
)
1143 ((not (stringp org-latex-title-command
)) nil
)
1144 ((string-match "\\(?:[^%]\\|^\\)%s"
1145 org-latex-title-command
)
1146 (format org-latex-title-command title
))
1147 (t org-latex-title-command
)))
1148 ;; Table of contents.
1149 (let ((depth (plist-get info
:with-toc
)))
1151 (concat (when (wholenump depth
)
1152 (format "\\setcounter{tocdepth}{%d}\n" depth
))
1153 org-latex-toc-command
)))
1157 (let ((creator-info (plist-get info
:with-creator
)))
1159 ((not creator-info
) "")
1160 ((eq creator-info
'comment
)
1161 (format "%% %s\n" (plist-get info
:creator
)))
1162 (t (concat (plist-get info
:creator
) "\n"))))
1164 "\\end{document}")))
1168 ;;; Transcode Functions
1172 (defun org-latex-bold (bold contents info
)
1173 "Transcode BOLD from Org to LaTeX.
1174 CONTENTS is the text with bold markup. INFO is a plist holding
1175 contextual information."
1176 (org-latex--text-markup contents
'bold
))
1181 (defun org-latex-center-block (center-block contents info
)
1182 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1183 CONTENTS holds the contents of the center block. INFO is a plist
1184 holding contextual information."
1185 (org-latex--wrap-label
1187 (format "\\begin{center}\n%s\\end{center}" contents
)))
1192 (defun org-latex-clock (clock contents info
)
1193 "Transcode a CLOCK element from Org to LaTeX.
1194 CONTENTS is nil. INFO is a plist holding contextual
1198 (format "\\textbf{%s} " org-clock-string
)
1199 (format org-latex-inactive-timestamp-format
1200 (concat (org-translate-time
1201 (org-element-property :raw-value
1202 (org-element-property :value clock
)))
1203 (let ((time (org-element-property :duration clock
)))
1204 (and time
(format " (%s)" time
)))))
1210 (defun org-latex-code (code contents info
)
1211 "Transcode a CODE object from Org to LaTeX.
1212 CONTENTS is nil. INFO is a plist used as a communication
1214 (org-latex--text-markup (org-element-property :value code
) 'code
))
1219 (defun org-latex-drawer (drawer contents info
)
1220 "Transcode a DRAWER element from Org to LaTeX.
1221 CONTENTS holds the contents of the block. INFO is a plist
1222 holding contextual information."
1223 (let* ((name (org-element-property :drawer-name drawer
))
1224 (output (if (functionp org-latex-format-drawer-function
)
1225 (funcall org-latex-format-drawer-function
1227 ;; If there's no user defined function: simply
1228 ;; display contents of the drawer.
1230 (org-latex--wrap-label drawer output
)))
1235 (defun org-latex-dynamic-block (dynamic-block contents info
)
1236 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1237 CONTENTS holds the contents of the block. INFO is a plist
1238 holding contextual information. See `org-export-data'."
1239 (org-latex--wrap-label dynamic-block contents
))
1244 (defun org-latex-entity (entity contents info
)
1245 "Transcode an ENTITY object from Org to LaTeX.
1246 CONTENTS are the definition itself. INFO is a plist holding
1247 contextual information."
1248 (let ((ent (org-element-property :latex entity
)))
1249 (if (org-element-property :latex-math-p entity
) (format "$%s$" ent
) ent
)))
1254 (defun org-latex-example-block (example-block contents info
)
1255 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1256 CONTENTS is nil. INFO is a plist holding contextual
1258 (when (org-string-nw-p (org-element-property :value example-block
))
1259 (org-latex--wrap-label
1261 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1262 (org-export-format-code-default example-block info
)))))
1267 (defun org-latex-export-block (export-block contents info
)
1268 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1269 CONTENTS is nil. INFO is a plist holding contextual information."
1270 (when (member (org-element-property :type export-block
) '("LATEX" "TEX"))
1271 (org-remove-indentation (org-element-property :value export-block
))))
1276 (defun org-latex-export-snippet (export-snippet contents info
)
1277 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1278 CONTENTS is nil. INFO is a plist holding contextual information."
1279 (when (eq (org-export-snippet-backend export-snippet
) 'latex
)
1280 (org-element-property :value export-snippet
)))
1285 (defun org-latex-fixed-width (fixed-width contents info
)
1286 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1287 CONTENTS is nil. INFO is a plist holding contextual information."
1288 (org-latex--wrap-label
1290 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1291 (org-remove-indentation
1292 (org-element-property :value fixed-width
)))))
1295 ;;;; Footnote Reference
1297 (defun org-latex-footnote-reference (footnote-reference contents info
)
1298 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1299 CONTENTS is nil. INFO is a plist holding contextual information."
1301 ;; Insert separator between two footnotes in a row.
1302 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1303 (when (eq (org-element-type prev
) 'footnote-reference
)
1304 org-latex-footnote-separator
))
1306 ;; Use \footnotemark if the footnote has already been defined.
1307 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1308 (format "\\footnotemark[%s]{}"
1309 (org-export-get-footnote-number footnote-reference info
)))
1310 ;; Use \footnotemark if reference is within another footnote
1311 ;; reference, footnote definition or table cell.
1312 ((loop for parent in
(org-export-get-genealogy footnote-reference
)
1313 thereis
(memq (org-element-type parent
)
1314 '(footnote-reference footnote-definition table-cell
)))
1316 ;; Otherwise, define it with \footnote command.
1318 (let ((def (org-export-get-footnote-definition footnote-reference info
)))
1320 (format "\\footnote{%s}" (org-trim (org-export-data def info
)))
1321 ;; Retrieve all footnote references within the footnote and
1322 ;; add their definition after it, since LaTeX doesn't support
1324 (org-latex--delayed-footnotes-definitions def info
)))))))
1329 (defun org-latex-headline (headline contents info
)
1330 "Transcode a HEADLINE element from Org to LaTeX.
1331 CONTENTS holds the contents of the headline. INFO is a plist
1332 holding contextual information."
1333 (unless (org-element-property :footnote-section-p headline
)
1334 (let* ((class (plist-get info
:latex-class
))
1335 (level (org-export-get-relative-level headline info
))
1336 (numberedp (org-export-numbered-headline-p headline info
))
1337 (class-sectionning (assoc class org-latex-classes
))
1338 ;; Section formatting will set two placeholders: one for
1339 ;; the title and the other for the contents.
1341 (let ((sec (if (functionp (nth 2 class-sectionning
))
1342 (funcall (nth 2 class-sectionning
) level numberedp
)
1343 (nth (1+ level
) class-sectionning
))))
1345 ;; No section available for that LEVEL.
1347 ;; Section format directly returned by a function. Add
1348 ;; placeholder for contents.
1349 ((stringp sec
) (concat sec
"\n%s"))
1350 ;; (numbered-section . unnumbered-section)
1351 ((not (consp (cdr sec
)))
1352 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
1353 ;; (numbered-open numbered-close)
1355 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
1356 ;; (num-in num-out no-num-in no-num-out)
1358 (if numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))
1359 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
1360 (text (org-export-data (org-element-property :title headline
) info
))
1362 (and (plist-get info
:with-todo-keywords
)
1363 (let ((todo (org-element-property :todo-keyword headline
)))
1364 (and todo
(org-export-data todo info
)))))
1365 (todo-type (and todo
(org-element-property :todo-type headline
)))
1366 (tags (and (plist-get info
:with-tags
)
1367 (org-export-get-tags headline info
)))
1368 (priority (and (plist-get info
:with-priority
)
1369 (org-element-property :priority headline
)))
1370 ;; Create the headline text along with a no-tag version.
1371 ;; The latter is required to remove tags from toc.
1372 (full-text (funcall org-latex-format-headline-function
1373 todo todo-type priority text tags
))
1374 ;; Associate \label to the headline for internal links.
1376 (format "\\label{sec-%s}\n"
1377 (mapconcat 'number-to-string
1378 (org-export-get-headline-number headline info
)
1381 (make-string (org-element-property :pre-blank headline
) 10)))
1382 (if (or (not section-fmt
) (org-export-low-level-p headline info
))
1383 ;; This is a deep sub-tree: export it as a list item. Also
1384 ;; export as items headlines for which no section format has
1386 (let ((low-level-body
1388 ;; If headline is the first sibling, start a list.
1389 (when (org-export-first-sibling-p headline info
)
1390 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
1392 "\\item " full-text
"\n" headline-label pre-blanks contents
)))
1393 ;; If headline is not the last sibling simply return
1394 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1396 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1397 (replace-regexp-in-string
1399 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
1401 ;; This is a standard headline. Export it as a section. Add
1402 ;; an alternative heading when possible, and when this is not
1403 ;; identical to the usual heading.
1405 (funcall org-latex-format-headline-function
1406 todo todo-type priority
1408 (org-export-get-alt-title headline info
) info
)
1409 (and (eq (plist-get info
:with-tags
) t
) tags
))))
1410 (if (and numberedp opt-title
1411 (not (equal opt-title full-text
))
1412 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt
))
1413 (format (replace-match "\\1[%s]" nil nil section-fmt
1)
1414 ;; Replace square brackets with parenthesis
1415 ;; since square brackets are not supported in
1416 ;; optional arguments.
1417 (replace-regexp-in-string
1418 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title
))
1420 (concat headline-label pre-blanks contents
))
1421 ;; Impossible to add an alternative heading. Fallback to
1422 ;; regular sectioning format string.
1423 (format section-fmt full-text
1424 (concat headline-label pre-blanks contents
))))))))
1426 (defun org-latex-format-headline-default-function
1427 (todo todo-type priority text tags
)
1428 "Default format function for a headline.
1429 See `org-latex-format-headline-function' for details."
1431 (and todo
(format "{\\bfseries\\sffamily %s} " todo
))
1432 (and priority
(format "\\framebox{\\#%c} " priority
))
1435 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags
":")))))
1438 ;;;; Horizontal Rule
1440 (defun org-latex-horizontal-rule (horizontal-rule contents info
)
1441 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1442 CONTENTS is nil. INFO is a plist holding contextual information."
1443 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule
))
1444 (prev (org-export-get-previous-element horizontal-rule info
)))
1446 ;; Make sure the rule doesn't start at the end of the current
1447 ;; line by separating it with a blank line from previous element.
1449 (let ((prev-blank (org-element-property :post-blank prev
)))
1450 (or (not prev-blank
) (zerop prev-blank
))))
1452 (org-latex--wrap-label
1454 (format "\\rule{%s}{%s}"
1455 (or (plist-get attr
:width
) "\\linewidth")
1456 (or (plist-get attr
:thickness
) "0.5pt"))))))
1459 ;;;; Inline Src Block
1461 (defun org-latex-inline-src-block (inline-src-block contents info
)
1462 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1463 CONTENTS holds the contents of the item. INFO is a plist holding
1464 contextual information."
1465 (let* ((code (org-element-property :value inline-src-block
))
1466 (separator (org-latex--find-verb-separator code
)))
1468 ;; Do not use a special package: transcode it verbatim.
1469 ((not org-latex-listings
)
1470 (concat "\\verb" separator code separator
))
1471 ;; Use minted package.
1472 ((eq org-latex-listings
'minted
)
1473 (let* ((org-lang (org-element-property :language inline-src-block
))
1474 (mint-lang (or (cadr (assq (intern org-lang
)
1475 org-latex-minted-langs
))
1477 (options (org-latex--make-option-string
1478 org-latex-minted-options
)))
1479 (concat (format "\\mint%s{%s}"
1480 (if (string= options
"") "" (format "[%s]" options
))
1482 separator code separator
)))
1483 ;; Use listings package.
1485 ;; Maybe translate language's name.
1486 (let* ((org-lang (org-element-property :language inline-src-block
))
1487 (lst-lang (or (cadr (assq (intern org-lang
)
1488 org-latex-listings-langs
))
1490 (options (org-latex--make-option-string
1491 (append org-latex-listings-options
1492 `(("language" ,lst-lang
))))))
1493 (concat (format "\\lstinline[%s]" options
)
1494 separator code separator
))))))
1499 (defun org-latex-inlinetask (inlinetask contents info
)
1500 "Transcode an INLINETASK element from Org to LaTeX.
1501 CONTENTS holds the contents of the block. INFO is a plist
1502 holding contextual information."
1503 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1504 (todo (and (plist-get info
:with-todo-keywords
)
1505 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1506 (and todo
(org-export-data todo info
)))))
1507 (todo-type (org-element-property :todo-type inlinetask
))
1508 (tags (and (plist-get info
:with-tags
)
1509 (org-export-get-tags inlinetask info
)))
1510 (priority (and (plist-get info
:with-priority
)
1511 (org-element-property :priority inlinetask
))))
1512 ;; If `org-latex-format-inlinetask-function' is provided, call it
1513 ;; with appropriate arguments.
1514 (if (functionp org-latex-format-inlinetask-function
)
1515 (funcall org-latex-format-inlinetask-function
1516 todo todo-type priority title tags contents
)
1517 ;; Otherwise, use a default template.
1518 (org-latex--wrap-label
1522 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1523 (when priority
(format "\\framebox{\\#%c} " priority
))
1525 (when tags
(format "\\hfill{}\\textsc{:%s:}"
1526 (mapconcat 'identity tags
":"))))))
1527 (format (concat "\\begin{center}\n"
1529 "\\begin{minipage}[c]{.6\\textwidth}\n"
1531 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1536 full-title contents
))))))
1541 (defun org-latex-italic (italic contents info
)
1542 "Transcode ITALIC from Org to LaTeX.
1543 CONTENTS is the text with italic markup. INFO is a plist holding
1544 contextual information."
1545 (org-latex--text-markup contents
'italic
))
1550 (defun org-latex-item (item contents info
)
1551 "Transcode an ITEM element from Org to LaTeX.
1552 CONTENTS holds the contents of the item. INFO is a plist holding
1553 contextual information."
1555 (let ((count (org-element-property :counter item
))
1557 ;; Determine level of current item to determine the
1558 ;; correct LaTeX counter to use (enumi, enumii...).
1559 (let ((parent item
) (level 0))
1560 (while (memq (org-element-type
1561 (setq parent
(org-export-get-parent parent
)))
1563 (when (and (eq (org-element-type parent
) 'plain-list
)
1564 (eq (org-element-property :type parent
)
1570 (format "\\setcounter{enum%s}{%s}\n"
1571 (nth (1- level
) '("i" "ii" "iii" "iv"))
1573 (checkbox (case (org-element-property :checkbox item
)
1574 (on "$\\boxtimes$ ")
1576 (trans "$\\boxminus$ ")))
1577 (tag (let ((tag (org-element-property :tag item
)))
1578 ;; Check-boxes must belong to the tag.
1579 (and tag
(format "[{%s}] "
1581 (org-export-data tag info
)))))))
1582 (concat counter
"\\item" (or tag
(concat " " checkbox
))
1583 (and contents
(org-trim contents
))
1584 ;; If there are footnotes references in tag, be sure to
1585 ;; add their definition at the end of the item. This
1586 ;; workaround is necessary since "\footnote{}" command is
1587 ;; not supported in tags.
1589 (org-latex--delayed-footnotes-definitions
1590 (org-element-property :tag item
) info
)))))
1595 (defun org-latex-keyword (keyword contents info
)
1596 "Transcode a KEYWORD element from Org to LaTeX.
1597 CONTENTS is nil. INFO is a plist holding contextual information."
1598 (let ((key (org-element-property :key keyword
))
1599 (value (org-element-property :value keyword
)))
1601 ((string= key
"LATEX") value
)
1602 ((string= key
"INDEX") (format "\\index{%s}" value
))
1603 ((string= key
"TOC")
1604 (let ((value (downcase value
)))
1606 ((string-match "\\<headlines\\>" value
)
1607 (let ((depth (or (and (string-match "[0-9]+" value
)
1608 (string-to-number (match-string 0 value
)))
1609 (plist-get info
:with-toc
))))
1611 (when (wholenump depth
)
1612 (format "\\setcounter{tocdepth}{%s}\n" depth
))
1613 "\\tableofcontents")))
1614 ((string= "tables" value
) "\\listoftables")
1615 ((string= "listings" value
)
1617 ((eq org-latex-listings
'minted
) "\\listoflistings")
1618 (org-latex-listings "\\lstlistoflistings")
1619 ;; At the moment, src blocks with a caption are wrapped
1620 ;; into a figure environment.
1621 (t "\\listoffigures")))))))))
1624 ;;;; Latex Environment
1626 (defun org-latex-latex-environment (latex-environment contents info
)
1627 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1628 CONTENTS is nil. INFO is a plist holding contextual information."
1629 (when (plist-get info
:with-latex
)
1630 (let ((label (org-element-property :name latex-environment
))
1631 (value (org-remove-indentation
1632 (org-element-property :value latex-environment
))))
1633 (if (not (org-string-nw-p label
)) value
1634 ;; Environment is labelled: label must be within the environment
1635 ;; (otherwise, a reference pointing to that element will count
1636 ;; the section instead).
1639 (goto-char (point-min))
1642 (format "\\label{%s}\n" (org-export-solidify-link-text label
)))
1643 (buffer-string))))))
1648 (defun org-latex-latex-fragment (latex-fragment contents info
)
1649 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1650 CONTENTS is nil. INFO is a plist holding contextual information."
1651 (when (plist-get info
:with-latex
)
1652 (org-element-property :value latex-fragment
)))
1657 (defun org-latex-line-break (line-break contents info
)
1658 "Transcode a LINE-BREAK object from Org to LaTeX.
1659 CONTENTS is nil. INFO is a plist holding contextual information."
1665 (defun org-latex--inline-image (link info
)
1666 "Return LaTeX code for an inline image.
1667 LINK is the link pointing to the inline image. INFO is a plist
1668 used as a communication channel."
1669 (let* ((parent (org-export-get-parent-element link
))
1670 (path (let ((raw-path (org-element-property :path link
)))
1671 (if (not (file-name-absolute-p raw-path
)) raw-path
1672 (expand-file-name raw-path
))))
1673 (filetype (file-name-extension path
))
1674 (caption (org-latex--caption/label-string parent info
))
1675 ;; Retrieve latex attributes from the element around.
1676 (attr (org-export-read-attribute :attr_latex parent
))
1677 (float (let ((float (plist-get attr
:float
)))
1678 (cond ((and (not float
) (plist-member attr
:float
)) nil
)
1679 ((string= float
"wrap") 'wrap
)
1680 ((string= float
"multicolumn") 'multicolumn
)
1682 (org-element-property :caption parent
)
1683 (org-string-nw-p (plist-get attr
:caption
)))
1686 (let ((place (plist-get attr
:placement
)))
1687 (cond (place (format "%s" place
))
1688 ((eq float
'wrap
) "{l}{0.5\\textwidth}")
1690 (format "[%s]" org-latex-default-figure-position
))
1692 (comment-include (if (plist-get attr
:comment-include
) "%" ""))
1693 ;; It is possible to specify width and height in the
1694 ;; ATTR_LATEX line, and also via default variables.
1695 (width (cond ((plist-get attr
:width
))
1696 ((plist-get attr
:height
) "")
1697 ((eq float
'wrap
) "0.48\\textwidth")
1698 (t org-latex-image-default-width
)))
1699 (height (cond ((plist-get attr
:height
))
1700 ((or (plist-get attr
:width
)
1701 (memq float
'(figure wrap
))) "")
1702 (t org-latex-image-default-height
)))
1703 (options (let ((opt (or (plist-get attr
:options
)
1704 org-latex-image-default-option
)))
1705 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt
)) opt
1706 (match-string 1 opt
))))
1708 (if (member filetype
'("tikz" "pgf"))
1710 ;; - use \input to read in image file.
1711 ;; - if options are present, wrap in a tikzpicture environment.
1712 ;; - if width or height are present, use \resizebox to change
1715 (setq image-code
(format "\\input{%s}" path
))
1716 (when (org-string-nw-p options
)
1718 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1721 (when (or (org-string-nw-p width
) (org-string-nw-p height
))
1722 (setq image-code
(format "\\resizebox{%s}{%s}{%s}"
1723 (if (org-string-nw-p width
) width
"!")
1724 (if (org-string-nw-p height
) height
"!")
1726 ;; For other images:
1727 ;; - add width and height to options.
1728 ;; - include the image with \includegraphics.
1729 (when (org-string-nw-p width
)
1730 (setq options
(concat options
",width=" width
)))
1731 (when (org-string-nw-p height
)
1732 (setq options
(concat options
",height=" height
)))
1734 (format "\\includegraphics%s{%s}"
1735 (cond ((not (org-string-nw-p options
)) "")
1736 ((= (aref options
0) ?
,)
1737 (format "[%s]"(substring options
1)))
1738 (t (format "[%s]" options
)))
1740 (when (equal filetype
"svg")
1741 (setq image-code
(replace-regexp-in-string "^\\\\includegraphics"
1745 (setq image-code
(replace-regexp-in-string "\\.svg}"
1749 ;; Return proper string, depending on FLOAT.
1751 (wrap (format "\\begin{wrapfigure}%s
1754 %s\\end{wrapfigure}" placement comment-include image-code caption
))
1755 (multicolumn (format "\\begin{figure*}%s
1758 %s\\end{figure*}" placement comment-include image-code caption
))
1759 (figure (format "\\begin{figure}%s
1762 %s\\end{figure}" placement comment-include image-code caption
))
1763 (otherwise image-code
))))
1765 (defun org-latex-link (link desc info
)
1766 "Transcode a LINK object from Org to LaTeX.
1768 DESC is the description part of the link, or the empty string.
1769 INFO is a plist holding contextual information. See
1771 (let* ((type (org-element-property :type link
))
1772 (raw-path (org-element-property :path link
))
1773 ;; Ensure DESC really exists, or set it to nil.
1774 (desc (and (not (string= desc
"")) desc
))
1775 (imagep (org-export-inline-image-p
1776 link org-latex-inline-image-rules
))
1778 ((member type
'("http" "https" "ftp" "mailto"))
1779 (concat type
":" raw-path
))
1780 ((string= type
"file")
1781 (if (not (file-name-absolute-p raw-path
)) raw-path
1782 (concat "file://" (expand-file-name raw-path
))))
1787 (imagep (org-latex--inline-image link info
))
1788 ;; Radio link: Transcode target's contents and use them as link's
1790 ((string= type
"radio")
1791 (let ((destination (org-export-resolve-radio-link link info
)))
1793 (format "\\hyperref[%s]{%s}"
1794 (org-export-solidify-link-text path
)
1795 (org-export-data (org-element-contents destination
) info
)))))
1796 ;; Links pointing to a headline: Find destination and build
1797 ;; appropriate referencing command.
1798 ((member type
'("custom-id" "fuzzy" "id"))
1799 (let ((destination (if (string= type
"fuzzy")
1800 (org-export-resolve-fuzzy-link link info
)
1801 (org-export-resolve-id-link link info
))))
1802 (case (org-element-type destination
)
1803 ;; Id link points to an external file.
1805 (if desc
(format "\\href{%s}{%s}" destination desc
)
1806 (format "\\url{%s}" destination
)))
1807 ;; Fuzzy link points nowhere.
1809 (format org-latex-link-with-unknown-path-format
1812 (org-element-property :raw-link link
) info
))))
1813 ;; LINK points to a headline. If headlines are numbered
1814 ;; and the link has no description, display headline's
1815 ;; number. Otherwise, display description or headline's
1822 (org-export-get-headline-number destination info
)
1824 (if (and (plist-get info
:section-numbers
) (not desc
))
1825 (format "\\ref{%s}" label
)
1826 (format "\\hyperref[%s]{%s}" label
1829 (org-element-property :title destination
) info
))))))
1830 ;; Fuzzy link points to a target. Do as above.
1832 (let ((path (org-export-solidify-link-text path
)))
1833 (if (not desc
) (format "\\ref{%s}" path
)
1834 (format "\\hyperref[%s]{%s}" path desc
)))))))
1835 ;; Coderef: replace link with the reference name or the
1836 ;; equivalent line number.
1837 ((string= type
"coderef")
1838 (format (org-export-get-coderef-format path desc
)
1839 (org-export-resolve-coderef path info
)))
1840 ;; Link type is handled by a special function.
1841 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
1842 (funcall protocol
(org-link-unescape path
) desc
'latex
))
1843 ;; External link with a description part.
1844 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
1845 ;; External link without a description part.
1846 (path (format "\\url{%s}" path
))
1847 ;; No path, only description. Try to do something useful.
1848 (t (format org-latex-link-with-unknown-path-format desc
)))))
1853 (defun org-latex-node-property (node-property contents info
)
1854 "Transcode a NODE-PROPERTY element from Org to LaTeX.
1855 CONTENTS is nil. INFO is a plist holding contextual
1858 (org-element-property :key node-property
)
1859 (let ((value (org-element-property :value node-property
)))
1860 (if value
(concat " " value
) ""))))
1865 (defun org-latex-paragraph (paragraph contents info
)
1866 "Transcode a PARAGRAPH element from Org to LaTeX.
1867 CONTENTS is the contents of the paragraph, as a string. INFO is
1868 the plist used as a communication channel."
1874 (defun org-latex-plain-list (plain-list contents info
)
1875 "Transcode a PLAIN-LIST element from Org to LaTeX.
1876 CONTENTS is the contents of the list. INFO is a plist holding
1877 contextual information."
1878 (let* ((type (org-element-property :type plain-list
))
1879 (attr (org-export-read-attribute :attr_latex plain-list
))
1880 (latex-type (let ((env (plist-get attr
:environment
)))
1881 (cond (env (format "%s" env
))
1882 ((eq type
'ordered
) "enumerate")
1883 ((eq type
'unordered
) "itemize")
1884 ((eq type
'descriptive
) "description")))))
1885 (org-latex--wrap-label
1887 (format "\\begin{%s}%s\n%s\\end{%s}"
1889 ;; Put optional arguments, if any inside square brackets
1891 (let ((options (format "%s" (or (plist-get attr
:options
) ""))))
1892 (cond ((equal options
"") "")
1893 ((string-match "\\`\\[.*\\]\\'" options
) options
)
1894 (t (format "[%s]" options
))))
1901 (defun org-latex-plain-text (text info
)
1902 "Transcode a TEXT string from Org to LaTeX.
1903 TEXT is the string to transcode. INFO is a plist holding
1904 contextual information."
1905 (let ((specialp (plist-get info
:with-special-strings
))
1907 ;; Protect %, #, &, $, _, { and }.
1908 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output
)
1911 (format "\\%s" (match-string 2 output
)) nil t output
2)))
1914 (replace-regexp-in-string
1915 "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil
2))
1916 ;; Protect \. If special strings are used, be careful not to
1917 ;; protect "\" in "\-" constructs.
1918 (let ((symbols (if specialp
"-%$#&{}^_\\" "%$#&{}^_\\")))
1920 (replace-regexp-in-string
1921 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols
)
1922 "$\\backslash$" output nil t
1)))
1925 (replace-regexp-in-string
1926 "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t
2))
1927 ;; Activate smart quotes. Be sure to provide original TEXT string
1928 ;; since OUTPUT may have been modified.
1929 (when (plist-get info
:with-smart-quotes
)
1930 (setq output
(org-export-activate-smart-quotes output
:latex info text
)))
1931 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1932 (let ((case-fold-search nil
)
1934 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start
)
1935 (setq output
(replace-match
1936 (format "\\%s{}" (match-string 1 output
)) nil t output
)
1937 start
(match-end 0))))
1938 ;; Convert special strings.
1941 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t
)))
1942 ;; Handle break preservation if required.
1943 (when (plist-get info
:preserve-breaks
)
1944 (setq output
(replace-regexp-in-string
1945 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output
)))
1952 (defun org-latex-planning (planning contents info
)
1953 "Transcode a PLANNING element from Org to LaTeX.
1954 CONTENTS is nil. INFO is a plist holding contextual
1962 (let ((closed (org-element-property :closed planning
)))
1965 (format "\\textbf{%s} " org-closed-string
)
1966 (format org-latex-inactive-timestamp-format
1968 (org-element-property :raw-value closed
))))))
1969 (let ((deadline (org-element-property :deadline planning
)))
1972 (format "\\textbf{%s} " org-deadline-string
)
1973 (format org-latex-active-timestamp-format
1975 (org-element-property :raw-value deadline
))))))
1976 (let ((scheduled (org-element-property :scheduled planning
)))
1979 (format "\\textbf{%s} " org-scheduled-string
)
1980 (format org-latex-active-timestamp-format
1982 (org-element-property :raw-value scheduled
))))))))
1987 ;;;; Property Drawer
1989 (defun org-latex-property-drawer (property-drawer contents info
)
1990 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1991 CONTENTS holds the contents of the drawer. INFO is a plist
1992 holding contextual information."
1993 (and (org-string-nw-p contents
)
1994 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents
)))
1999 (defun org-latex-quote-block (quote-block contents info
)
2000 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2001 CONTENTS holds the contents of the block. INFO is a plist
2002 holding contextual information."
2003 (org-latex--wrap-label
2005 (format "\\begin{quote}\n%s\\end{quote}" contents
)))
2010 (defun org-latex-quote-section (quote-section contents info
)
2011 "Transcode a QUOTE-SECTION element from Org to LaTeX.
2012 CONTENTS is nil. INFO is a plist holding contextual information."
2013 (let ((value (org-remove-indentation
2014 (org-element-property :value quote-section
))))
2015 (when value
(format "\\begin{verbatim}\n%s\\end{verbatim}" value
))))
2020 (defun org-latex-radio-target (radio-target text info
)
2021 "Transcode a RADIO-TARGET object from Org to LaTeX.
2022 TEXT is the text of the target. INFO is a plist holding
2023 contextual information."
2024 (format "\\label{%s}%s"
2025 (org-export-solidify-link-text
2026 (org-element-property :value radio-target
))
2032 (defun org-latex-section (section contents info
)
2033 "Transcode a SECTION element from Org to LaTeX.
2034 CONTENTS holds the contents of the section. INFO is a plist
2035 holding contextual information."
2041 (defun org-latex-special-block (special-block contents info
)
2042 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2043 CONTENTS holds the contents of the block. INFO is a plist
2044 holding contextual information."
2045 (let ((type (downcase (org-element-property :type special-block
)))
2046 (opt (org-export-read-attribute :attr_latex special-block
:options
)))
2047 (concat (format "\\begin{%s}%s\n" type
(or opt
""))
2048 ;; Insert any label or caption within the block
2049 ;; (otherwise, a reference pointing to that element will
2050 ;; count the section instead).
2051 (org-latex--caption/label-string special-block info
)
2053 (format "\\end{%s}" type
))))
2058 (defun org-latex-src-block (src-block contents info
)
2059 "Transcode a SRC-BLOCK element from Org to LaTeX.
2060 CONTENTS holds the contents of the item. INFO is a plist holding
2061 contextual information."
2062 (when (org-string-nw-p (org-element-property :value src-block
))
2063 (let* ((lang (org-element-property :language src-block
))
2064 (caption (org-element-property :caption src-block
))
2065 (label (org-element-property :name src-block
))
2066 (custom-env (and lang
2067 (cadr (assq (intern lang
)
2068 org-latex-custom-lang-environments
))))
2069 (num-start (case (org-element-property :number-lines src-block
)
2070 (continued (org-export-get-loc src-block info
))
2072 (retain-labels (org-element-property :retain-labels src-block
))
2073 (attributes (org-export-read-attribute :attr_latex src-block
))
2074 (float (plist-get attributes
:float
)))
2076 ;; Case 1. No source fontification.
2077 ((not org-latex-listings
)
2078 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2080 (cond ((and (not float
) (plist-member attributes
:float
)) "%s")
2081 ((string= "multicolumn" float
)
2082 (format "\\begin{figure*}[%s]\n%%s%s\n\\end{figure*}"
2083 org-latex-default-figure-position
2086 (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
)
2100 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2102 (cond ((and (not float
) (plist-member attributes
:float
)) "%s")
2103 ((string= "multicolumn" float
)
2104 (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
2107 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2112 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2114 (org-latex--make-option-string
2115 (if (or (not num-start
)
2116 (assoc "linenos" org-latex-minted-options
))
2117 org-latex-minted-options
2120 ("firstnumber" ,(number-to-string (1+ num-start
))))
2121 org-latex-minted-options
)))
2123 (or (cadr (assq (intern lang
) org-latex-minted-langs
)) lang
)
2125 (let* ((code-info (org-export-unravel-code src-block
))
2129 (org-split-string (car code-info
)
2131 (org-export-format-code
2133 (lambda (loc num ref
)
2137 ;; Ensure references are flushed to the right,
2138 ;; separated with 6 spaces from the widest line
2140 (concat (make-string (+ (- max-width
(length loc
)) 6)
2142 (format "(%s)" ref
)))))
2143 nil
(and retain-labels
(cdr code-info
)))))))
2145 (format float-env body
)))
2146 ;; Case 4. Use listings package.
2149 (or (cadr (assq (intern lang
) org-latex-listings-langs
)) lang
))
2152 (let ((main (org-export-get-caption src-block
))
2153 (secondary (org-export-get-caption src-block t
)))
2155 (format "{%s}" (org-export-data main info
))
2157 (org-export-data secondary info
)
2158 (org-export-data main info
)))))))
2163 (org-latex--make-option-string
2165 org-latex-listings-options
2167 ((and (not float
) (plist-member attributes
:float
)) nil
)
2168 ((string= "multicolumn" float
) '(("float" "*")))
2169 ((and float
(not (assoc "float" org-latex-listings-options
)))
2170 `(("float" ,org-latex-default-figure-position
))))
2171 `(("language" ,lst-lang
))
2172 (when label
`(("label" ,label
)))
2173 (when caption-str
`(("caption" ,caption-str
)))
2174 (cond ((assoc "numbers" org-latex-listings-options
) nil
)
2175 ((not num-start
) '(("numbers" "none")))
2176 ((zerop num-start
) '(("numbers" "left")))
2177 (t `(("numbers" "left")
2179 ,(number-to-string (1+ num-start
)))))))))
2182 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2183 (let* ((code-info (org-export-unravel-code src-block
))
2187 (org-split-string (car code-info
) "\n")))))
2188 (org-export-format-code
2190 (lambda (loc num ref
)
2194 ;; Ensure references are flushed to the right,
2195 ;; separated with 6 spaces from the widest line of
2197 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2198 (format "(%s)" ref
)))))
2199 nil
(and retain-labels
(cdr code-info
))))))))))))
2202 ;;;; Statistics Cookie
2204 (defun org-latex-statistics-cookie (statistics-cookie contents info
)
2205 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2206 CONTENTS is nil. INFO is a plist holding contextual information."
2207 (replace-regexp-in-string
2208 "%" "\\%" (org-element-property :value statistics-cookie
) nil t
))
2213 (defun org-latex-strike-through (strike-through contents info
)
2214 "Transcode STRIKE-THROUGH from Org to LaTeX.
2215 CONTENTS is the text with strike-through markup. INFO is a plist
2216 holding contextual information."
2217 (org-latex--text-markup contents
'strike-through
))
2222 (defun org-latex--script-size (object info
)
2223 "Transcode a subscript or superscript object.
2224 OBJECT is an Org object. INFO is a plist used as a communication
2227 ;; Non-nil if object is already in a sub/superscript.
2228 (let ((parent object
))
2230 (while (setq parent
(org-export-get-parent parent
))
2231 (let ((type (org-element-type parent
)))
2232 (cond ((memq type
'(subscript superscript
))
2234 ((memq type org-element-all-elements
)
2235 (throw 'exit nil
))))))))
2236 (type (org-element-type object
))
2238 (org-element-map (org-element-contents object
)
2239 (cons 'plain-text org-element-all-objects
)
2241 (case (org-element-type obj
)
2242 ((entity latex-fragment
)
2243 (let ((data (org-trim (org-export-data obj info
))))
2245 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2249 (match-string 1 data
)
2250 (let ((blank (org-element-property :post-blank obj
)))
2251 (and blank
(> blank
0) "\\ "))))))
2254 (format "%s\\text{%s}" output
(org-export-data obj info
))))
2258 (org-export-data obj info
)
2259 (let ((blank (org-element-property :post-blank obj
)))
2260 (and blank
(> blank
0) "\\ ")))))))
2261 info nil org-element-recursive-objects
)
2262 ;; Result. Do not wrap into math mode if already in a subscript
2263 ;; or superscript. Do not wrap into curly brackets if OUTPUT is
2264 ;; a single character. Also merge consecutive subscript and
2265 ;; superscript into the same math snippet.
2266 (concat (and (not in-script-p
)
2267 (let ((prev (org-export-get-previous-element object info
)))
2269 (not (eq (org-element-type prev
)
2270 (if (eq type
'subscript
) 'superscript
2272 (let ((blank (org-element-property :post-blank prev
)))
2273 (and blank
(> blank
0)))))
2275 (if (eq (org-element-type object
) 'subscript
) "_" "^")
2276 (and (> (length output
) 1) "{")
2278 (and (> (length output
) 1) "}")
2279 (and (not in-script-p
)
2280 (or (let ((blank (org-element-property :post-blank object
)))
2281 (and blank
(> blank
0)))
2282 (not (eq (org-element-type
2283 (org-export-get-next-element object info
))
2284 (if (eq type
'subscript
) 'superscript
2288 (defun org-latex-subscript (subscript contents info
)
2289 "Transcode a SUBSCRIPT object from Org to LaTeX.
2290 CONTENTS is the contents of the object. INFO is a plist holding
2291 contextual information."
2292 (org-latex--script-size subscript info
))
2297 (defun org-latex-superscript (superscript contents info
)
2298 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2299 CONTENTS is the contents of the object. INFO is a plist holding
2300 contextual information."
2301 (org-latex--script-size superscript info
))
2306 ;; `org-latex-table' is the entry point for table transcoding. It
2307 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2308 ;; delegates the job to either `org-latex--table.el-table',
2309 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2310 ;; depending of the type of the table and the mode requested.
2312 ;; `org-latex--align-string' is a subroutine used to build alignment
2313 ;; string for Org tables.
2315 (defun org-latex-table (table contents info
)
2316 "Transcode a TABLE element from Org to LaTeX.
2317 CONTENTS is the contents of the table. INFO is a plist holding
2318 contextual information."
2319 (if (eq (org-element-property :type table
) 'table.el
)
2320 ;; "table.el" table. Convert it using appropriate tools.
2321 (org-latex--table.el-table table info
)
2322 (let ((type (or (org-export-read-attribute :attr_latex table
:mode
)
2323 org-latex-default-table-mode
)))
2325 ;; Case 1: Verbatim table.
2326 ((string= type
"verbatim")
2327 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2328 ;; Re-create table, without affiliated keywords.
2329 (org-trim (org-element-interpret-data
2330 `(table nil
,@(org-element-contents table
))))))
2332 ((or (string= type
"math") (string= type
"inline-math"))
2333 (org-latex--math-table table info
))
2334 ;; Case 3: Standard table.
2335 (t (concat (org-latex--org-table table contents info
)
2336 ;; When there are footnote references within the
2337 ;; table, insert their definition just after it.
2338 (org-latex--delayed-footnotes-definitions table info
)))))))
2340 (defun org-latex--align-string (table info
)
2341 "Return an appropriate LaTeX alignment string.
2342 TABLE is the considered table. INFO is a plist used as
2343 a communication channel."
2344 (or (org-export-read-attribute :attr_latex table
:align
)
2346 ;; Extract column groups and alignment from first (non-rule)
2349 (org-element-map table
'table-row
2351 (and (eq (org-element-property :type row
) 'standard
) row
))
2355 (let ((borders (org-export-table-cell-borders cell info
)))
2356 ;; Check left border for the first cell only.
2357 (when (and (memq 'left borders
) (not align
))
2359 (push (case (org-export-table-cell-alignment cell info
)
2364 (when (memq 'right borders
) (push "|" align
))))
2366 (apply 'concat
(nreverse align
)))))
2368 (defun org-latex--org-table (table contents info
)
2369 "Return appropriate LaTeX code for an Org table.
2371 TABLE is the table type element to transcode. CONTENTS is its
2372 contents, as a string. INFO is a plist used as a communication
2375 This function assumes TABLE has `org' as its `:type' property and
2376 `table' as its `:mode' attribute."
2377 (let* ((caption (org-latex--caption/label-string table info
))
2378 (attr (org-export-read-attribute :attr_latex table
))
2379 ;; Determine alignment string.
2380 (alignment (org-latex--align-string table info
))
2381 ;; Determine environment for the table: longtable, tabular...
2382 (table-env (or (plist-get attr
:environment
)
2383 org-latex-default-table-environment
))
2384 ;; If table is a float, determine environment: table, table*
2385 ;; or sidewaystable.
2386 (float-env (unless (member table-env
'("longtable" "longtabu"))
2387 (let ((float (plist-get attr
:float
)))
2389 ((and (not float
) (plist-member attr
:float
)) nil
)
2390 ((string= float
"sidewaystable") "sidewaystable")
2391 ((string= float
"multicolumn") "table*")
2393 (org-element-property :caption table
)
2394 (org-string-nw-p (plist-get attr
:caption
)))
2396 ;; Extract others display options.
2397 (fontsize (let ((font (plist-get attr
:font
)))
2398 (and font
(concat font
"\n"))))
2399 (width (plist-get attr
:width
))
2400 (spreadp (plist-get attr
:spread
))
2401 (placement (or (plist-get attr
:placement
)
2402 (format "[%s]" org-latex-default-figure-position
)))
2403 (centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2404 org-latex-tables-centered
)))
2405 ;; Prepare the final format string for the table.
2408 ((equal "longtable" table-env
)
2409 (concat (and fontsize
(concat "{" fontsize
))
2410 (format "\\begin{longtable}{%s}\n" alignment
)
2411 (and org-latex-table-caption-above
2412 (org-string-nw-p caption
)
2413 (concat caption
"\\\\\n"))
2415 (and (not org-latex-table-caption-above
)
2416 (org-string-nw-p caption
)
2417 (concat caption
"\\\\\n"))
2418 "\\end{longtable}\n"
2419 (and fontsize
"}")))
2421 ((equal "longtabu" table-env
)
2422 (concat (and fontsize
(concat "{" fontsize
))
2423 (format "\\begin{longtabu}%s{%s}\n"
2426 (if spreadp
"spread" "to") width
) "")
2428 (and org-latex-table-caption-above
2429 (org-string-nw-p caption
)
2430 (concat caption
"\\\\\n"))
2432 (and (not org-latex-table-caption-above
)
2433 (org-string-nw-p caption
)
2434 (concat caption
"\\\\\n"))
2436 (and fontsize
"}")))
2440 (concat (format "\\begin{%s}%s\n" float-env placement
)
2441 (if org-latex-table-caption-above caption
"")
2442 (when centerp
"\\centering\n")
2444 (centerp (concat "\\begin{center}\n" fontsize
))
2445 (fontsize (concat "{" fontsize
)))
2446 (cond ((equal "tabu" table-env
)
2447 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2449 (if spreadp
" spread %s " " to %s ")
2453 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2455 (if width
(format "{%s}" width
) "")
2461 (concat (if org-latex-table-caption-above
"" caption
)
2462 (format "\n\\end{%s}" float-env
)))
2463 (centerp "\n\\end{center}")
2464 (fontsize "}")))))))
2466 (defun org-latex--table.el-table
(table info
)
2467 "Return appropriate LaTeX code for a table.el table.
2469 TABLE is the table type element to transcode. INFO is a plist
2470 used as a communication channel.
2472 This function assumes TABLE has `table.el' as its `:type'
2475 ;; Ensure "*org-export-table*" buffer is empty.
2476 (with-current-buffer (get-buffer-create "*org-export-table*")
2478 (let ((output (with-temp-buffer
2479 (insert (org-element-property :value table
))
2481 (re-search-forward "^[ \t]*|[^|]" nil t
)
2482 (table-generate-source 'latex
"*org-export-table*")
2483 (with-current-buffer "*org-export-table*"
2484 (org-trim (buffer-string))))))
2485 (kill-buffer (get-buffer "*org-export-table*"))
2486 ;; Remove left out comments.
2487 (while (string-match "^%.*\n" output
)
2488 (setq output
(replace-match "" t t output
)))
2489 (let ((attr (org-export-read-attribute :attr_latex table
)))
2490 (when (plist-get attr
:rmlines
)
2491 ;; When the "rmlines" attribute is provided, remove all hlines
2492 ;; but the the one separating heading from the table body.
2493 (let ((n 0) (pos 0))
2494 (while (and (< (length output
) pos
)
2495 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
2497 (unless (= n
2) (setq output
(replace-match "" nil nil output
))))))
2498 (let ((centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2499 org-latex-tables-centered
)))
2500 (if (not centerp
) output
2501 (format "\\begin{center}\n%s\n\\end{center}" output
))))))
2503 (defun org-latex--math-table (table info
)
2504 "Return appropriate LaTeX code for a matrix.
2506 TABLE is the table type element to transcode. INFO is a plist
2507 used as a communication channel.
2509 This function assumes TABLE has `org' as its `:type' property and
2510 `inline-math' or `math' as its `:mode' attribute.."
2511 (let* ((caption (org-latex--caption/label-string table info
))
2512 (attr (org-export-read-attribute :attr_latex table
))
2513 (inlinep (equal (plist-get attr
:mode
) "inline-math"))
2514 (env (or (plist-get attr
:environment
)
2515 org-latex-default-table-environment
))
2519 ;; Ignore horizontal rules.
2520 (when (eq (org-element-property :type row
) 'standard
)
2521 ;; Return each cell unmodified.
2525 (substring (org-element-interpret-data cell
) 0 -
1))
2526 (org-element-map row
'table-cell
'identity info
) "&")
2527 (or (cdr (assoc env org-latex-table-matrix-macros
)) "\\\\")
2529 (org-element-map table
'table-row
'identity info
) ""))
2530 ;; Variables related to math clusters (contiguous math tables
2531 ;; of the same type).
2532 (mode (org-export-read-attribute :attr_latex table
:mode
))
2533 (prev (org-export-get-previous-element table info
))
2534 (next (org-export-get-next-element table info
))
2537 ;; Non-nil when TABLE has the same mode as current table.
2538 (string= (or (org-export-read-attribute :attr_latex table
:mode
)
2539 org-latex-default-table-mode
)
2542 ;; Opening string. If TABLE is in the middle of a table cluster,
2543 ;; do not insert any.
2545 (eq (org-element-type prev
) 'table
)
2546 (memq (org-element-property :post-blank prev
) '(0 nil
))
2547 (funcall same-mode-p prev
))
2550 ((org-string-nw-p caption
) (concat "\\begin{equation}\n" caption
))
2553 (or (plist-get attr
:math-prefix
) "")
2554 ;; Environment. Also treat special cases.
2555 (cond ((equal env
"array")
2556 (let ((align (org-latex--align-string table info
)))
2557 (format "\\begin{array}{%s}\n%s\\end{array}" align contents
)))
2558 ((assoc env org-latex-table-matrix-macros
)
2559 (format "\\%s%s{\n%s}"
2561 (or (plist-get attr
:math-arguments
) "")
2563 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env
)))
2565 (or (plist-get attr
:math-suffix
) "")
2566 ;; Closing string. If TABLE is in the middle of a table cluster,
2567 ;; do not insert any. If it closes such a cluster, be sure to
2568 ;; close the cluster with a string matching the opening string.
2570 (eq (org-element-type next
) 'table
)
2571 (memq (org-element-property :post-blank table
) '(0 nil
))
2572 (funcall same-mode-p next
))
2575 ;; Find cluster beginning to know which environment to use.
2576 ((let ((cluster-beg table
) prev
)
2577 (while (and (setq prev
(org-export-get-previous-element
2579 (memq (org-element-property :post-blank prev
)
2581 (funcall same-mode-p prev
))
2582 (setq cluster-beg prev
))
2583 (and (or (org-element-property :caption cluster-beg
)
2584 (org-element-property :name cluster-beg
))
2585 "\n\\end{equation}")))
2591 (defun org-latex-table-cell (table-cell contents info
)
2592 "Transcode a TABLE-CELL element from Org to LaTeX.
2593 CONTENTS is the cell contents. INFO is a plist used as
2594 a communication channel."
2595 (concat (if (and contents
2596 org-latex-table-scientific-notation
2597 (string-match orgtbl-exp-regexp contents
))
2598 ;; Use appropriate format string for scientific
2600 (format org-latex-table-scientific-notation
2601 (match-string 1 contents
)
2602 (match-string 2 contents
))
2604 (when (org-export-get-next-element table-cell info
) " & ")))
2609 (defun org-latex-table-row (table-row contents info
)
2610 "Transcode a TABLE-ROW element from Org to LaTeX.
2611 CONTENTS is the contents of the row. INFO is a plist used as
2612 a communication channel."
2613 ;; Rules are ignored since table separators are deduced from
2614 ;; borders of the current row.
2615 (when (eq (org-element-property :type table-row
) 'standard
)
2616 (let* ((attr (org-export-read-attribute :attr_latex
2617 (org-export-get-parent table-row
)))
2618 (longtablep (member (or (plist-get attr
:environment
)
2619 org-latex-default-table-environment
)
2620 '("longtable" "longtabu")))
2621 (booktabsp (if (plist-member attr
:booktabs
)
2622 (plist-get attr
:booktabs
)
2623 org-latex-tables-booktabs
))
2624 ;; TABLE-ROW's borders are extracted from its first cell.
2625 (borders (org-export-table-cell-borders
2626 (car (org-element-contents table-row
)) info
)))
2628 ;; When BOOKTABS are activated enforce top-rule even when no
2629 ;; hline was specifically marked.
2630 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2631 ((and (memq 'top borders
) (memq 'above borders
)) "\\hline\n"))
2634 ;; Special case for long tables. Define header and footers.
2635 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
2638 \\multicolumn{%d}{l}{%s} \\\\
2643 %s\\multicolumn{%d}{r}{%s} \\\\
2646 (if booktabsp
"\\midrule" "\\hline")
2647 (cdr (org-export-table-dimensions
2648 (org-export-get-parent-table table-row
) info
))
2649 (org-latex--translate "Continued from previous page" info
)
2650 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2651 ((and (memq 'top borders
)
2652 (memq 'above borders
)) "\\hline\n")
2655 (if booktabsp
"\\midrule" "\\hline")
2656 (if booktabsp
"\\midrule" "\\hline")
2657 ;; Number of columns.
2658 (cdr (org-export-table-dimensions
2659 (org-export-get-parent-table table-row
) info
))
2660 (org-latex--translate "Continued on next page" info
)))
2661 ;; When BOOKTABS are activated enforce bottom rule even when
2662 ;; no hline was specifically marked.
2663 ((and booktabsp
(memq 'bottom borders
)) "\\bottomrule")
2664 ((and (memq 'bottom borders
) (memq 'below borders
)) "\\hline")
2665 ((memq 'below borders
) (if booktabsp
"\\midrule" "\\hline")))))))
2670 (defun org-latex-target (target contents info
)
2671 "Transcode a TARGET object from Org to LaTeX.
2672 CONTENTS is nil. INFO is a plist holding contextual
2674 (format "\\label{%s}"
2675 (org-export-solidify-link-text (org-element-property :value target
))))
2680 (defun org-latex-timestamp (timestamp contents info
)
2681 "Transcode a TIMESTAMP object from Org to LaTeX.
2682 CONTENTS is nil. INFO is a plist holding contextual
2684 (let ((value (org-latex-plain-text
2685 (org-timestamp-translate timestamp
) info
)))
2686 (case (org-element-property :type timestamp
)
2687 ((active active-range
) (format org-latex-active-timestamp-format value
))
2688 ((inactive inactive-range
)
2689 (format org-latex-inactive-timestamp-format value
))
2690 (otherwise (format org-latex-diary-timestamp-format value
)))))
2695 (defun org-latex-underline (underline contents info
)
2696 "Transcode UNDERLINE from Org to LaTeX.
2697 CONTENTS is the text with underline markup. INFO is a plist
2698 holding contextual information."
2699 (org-latex--text-markup contents
'underline
))
2704 (defun org-latex-verbatim (verbatim contents info
)
2705 "Transcode a VERBATIM object from Org to LaTeX.
2706 CONTENTS is nil. INFO is a plist used as a communication
2708 (org-latex--text-markup (org-element-property :value verbatim
) 'verbatim
))
2713 (defun org-latex-verse-block (verse-block contents info
)
2714 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2715 CONTENTS is verse block contents. INFO is a plist holding
2716 contextual information."
2717 (org-latex--wrap-label
2719 ;; In a verse environment, add a line break to each newline
2720 ;; character and change each white space at beginning of a line
2721 ;; into a space of 1 em. Also change each blank line with
2722 ;; a vertical space of 1 em.
2724 (setq contents
(replace-regexp-in-string
2725 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2726 (replace-regexp-in-string
2727 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents
)))
2728 (while (string-match "^[ \t]+" contents
)
2729 (let ((new-str (format "\\hspace*{%dem}"
2730 (length (match-string 0 contents
)))))
2731 (setq contents
(replace-match new-str nil t contents
))))
2732 (format "\\begin{verse}\n%s\\end{verse}" contents
))))
2736 ;;; End-user functions
2739 (defun org-latex-export-as-latex
2740 (&optional async subtreep visible-only body-only ext-plist
)
2741 "Export current buffer as a LaTeX buffer.
2743 If narrowing is active in the current buffer, only export its
2746 If a region is active, export that region.
2748 A non-nil optional argument ASYNC means the process should happen
2749 asynchronously. The resulting buffer should be accessible
2750 through the `org-export-stack' interface.
2752 When optional argument SUBTREEP is non-nil, export the sub-tree
2753 at point, extracting information from the headline properties
2756 When optional argument VISIBLE-ONLY is non-nil, don't export
2757 contents of hidden elements.
2759 When optional argument BODY-ONLY is non-nil, only write code
2760 between \"\\begin{document}\" and \"\\end{document}\".
2762 EXT-PLIST, when provided, is a property list with external
2763 parameters overriding Org default settings, but still inferior to
2764 file-local settings.
2766 Export is done in a buffer named \"*Org LATEX Export*\", which
2767 will be displayed when `org-export-show-temporary-export-buffer'
2770 (org-export-to-buffer 'latex
"*Org LATEX Export*"
2771 async subtreep visible-only body-only ext-plist
(lambda () (LaTeX-mode))))
2774 (defun org-latex-convert-region-to-latex ()
2775 "Assume the current region has org-mode syntax, and convert it to LaTeX.
2776 This can be used in any buffer. For example, you can write an
2777 itemized list in org-mode syntax in an LaTeX buffer and use this
2778 command to convert it."
2780 (org-export-replace-region-by 'latex
))
2783 (defun org-latex-export-to-latex
2784 (&optional async subtreep visible-only body-only ext-plist
)
2785 "Export current buffer to a LaTeX file.
2787 If narrowing is active in the current buffer, only export its
2790 If a region is active, export that region.
2792 A non-nil optional argument ASYNC means the process should happen
2793 asynchronously. The resulting file should be accessible through
2794 the `org-export-stack' interface.
2796 When optional argument SUBTREEP is non-nil, export the sub-tree
2797 at point, extracting information from the headline properties
2800 When optional argument VISIBLE-ONLY is non-nil, don't export
2801 contents of hidden elements.
2803 When optional argument BODY-ONLY is non-nil, only write code
2804 between \"\\begin{document}\" and \"\\end{document}\".
2806 EXT-PLIST, when provided, is a property list with external
2807 parameters overriding Org default settings, but still inferior to
2808 file-local settings."
2810 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
2811 (org-export-to-file 'latex outfile
2812 async subtreep visible-only body-only ext-plist
)))
2815 (defun org-latex-export-to-pdf
2816 (&optional async subtreep visible-only body-only ext-plist
)
2817 "Export current buffer to LaTeX then process through to PDF.
2819 If narrowing is active in the current buffer, only export its
2822 If a region is active, export that region.
2824 A non-nil optional argument ASYNC means the process should happen
2825 asynchronously. The resulting file should be accessible through
2826 the `org-export-stack' interface.
2828 When optional argument SUBTREEP is non-nil, export the sub-tree
2829 at point, extracting information from the headline properties
2832 When optional argument VISIBLE-ONLY is non-nil, don't export
2833 contents of hidden elements.
2835 When optional argument BODY-ONLY is non-nil, only write code
2836 between \"\\begin{document}\" and \"\\end{document}\".
2838 EXT-PLIST, when provided, is a property list with external
2839 parameters overriding Org default settings, but still inferior to
2840 file-local settings.
2842 Return PDF file's name."
2844 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
2845 (org-export-to-file 'latex outfile
2846 async subtreep visible-only body-only ext-plist
2847 (lambda (file) (org-latex-compile file
)))))
2849 (defun org-latex-compile (texfile &optional snippet
)
2850 "Compile a TeX file.
2852 TEXFILE is the name of the file being compiled. Processing is
2853 done through the command specified in `org-latex-pdf-process'.
2855 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
2856 file used to preview a LaTeX snippet. In this case, do not
2857 create a log buffer and do not bother removing log files.
2859 Return PDF file name or an error if it couldn't be produced."
2860 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile
)))
2861 (full-name (file-truename texfile
))
2862 (out-dir (file-name-directory texfile
))
2863 ;; Properly set working directory for compilation.
2864 (default-directory (if (file-name-absolute-p texfile
)
2865 (file-name-directory full-name
)
2868 (unless snippet
(message (format "Processing LaTeX file %s..." texfile
)))
2869 (save-window-excursion
2871 ;; A function is provided: Apply it.
2872 ((functionp org-latex-pdf-process
)
2873 (funcall org-latex-pdf-process
(shell-quote-argument texfile
)))
2874 ;; A list is provided: Replace %b, %f and %o with appropriate
2875 ;; values in each command before applying it. Output is
2876 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2877 ((consp org-latex-pdf-process
)
2878 (let ((outbuf (and (not snippet
)
2879 (get-buffer-create "*Org PDF LaTeX Output*"))))
2883 (replace-regexp-in-string
2884 "%b" (shell-quote-argument base-name
)
2885 (replace-regexp-in-string
2886 "%f" (shell-quote-argument full-name
)
2887 (replace-regexp-in-string
2888 "%o" (shell-quote-argument out-dir
) command t t
) t t
) t t
)
2890 org-latex-pdf-process
)
2891 ;; Collect standard errors from output buffer.
2892 (setq errors
(and (not snippet
) (org-latex--collect-errors outbuf
)))))
2893 (t (error "No valid command to process to PDF")))
2894 (let ((pdffile (concat out-dir base-name
".pdf")))
2895 ;; Check for process failure. Provide collected errors if
2897 (if (not (file-exists-p pdffile
))
2898 (error (concat (format "PDF file %s wasn't produced" pdffile
)
2899 (when errors
(concat ": " errors
))))
2900 ;; Else remove log files, when specified, and signal end of
2901 ;; process to user, along with any error encountered.
2902 (when (and (not snippet
) org-latex-remove-logfiles
)
2903 (dolist (file (directory-files
2905 (concat (regexp-quote base-name
)
2906 "\\(?:\\.[0-9]+\\)?"
2908 (regexp-opt org-latex-logfiles-extensions
))))
2909 (delete-file file
)))
2910 (message (concat "Process completed"
2911 (if (not errors
) "."
2912 (concat " with errors: " errors
)))))
2913 ;; Return output file name.
2916 (defun org-latex--collect-errors (buffer)
2917 "Collect some kind of errors from \"pdflatex\" command output.
2919 BUFFER is the buffer containing output.
2921 Return collected error types as a string, or nil if there was
2923 (with-current-buffer buffer
2925 (goto-char (point-max))
2926 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t
)
2927 (let ((case-fold-search t
)
2929 (dolist (latex-error org-latex-known-errors
)
2930 (when (save-excursion (re-search-forward (car latex-error
) nil t
))
2931 (setq errors
(concat errors
" " (cdr latex-error
)))))
2932 (and (org-string-nw-p errors
) (org-trim errors
)))))))
2935 (defun org-latex-publish-to-latex (plist filename pub-dir
)
2936 "Publish an Org file to LaTeX.
2938 FILENAME is the filename of the Org file to be published. PLIST
2939 is the property list for the given project. PUB-DIR is the
2940 publishing directory.
2942 Return output file name."
2943 (org-publish-org-to 'latex filename
".tex" plist pub-dir
))
2946 (defun org-latex-publish-to-pdf (plist filename pub-dir
)
2947 "Publish an Org file to PDF (via LaTeX).
2949 FILENAME is the filename of the Org file to be published. PLIST
2950 is the property list for the given project. PUB-DIR is the
2951 publishing directory.
2953 Return output file name."
2954 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
2955 ;; in working directory and then moved to publishing directory.
2956 (org-publish-attachment
2958 (org-latex-compile (org-publish-org-to 'latex filename
".tex" plist
))
2965 ;; generated-autoload-file: "org-loaddefs.el"
2968 ;;; ox-latex.el ends here