1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;; See Org manual for details.
27 (eval-when-compile (require 'cl
))
31 (defvar org-latex-default-packages-alist
)
32 (defvar org-latex-packages-alist
)
33 (defvar orgtbl-exp-regexp
)
39 (org-export-define-backend 'latex
40 '((bold . org-latex-bold
)
41 (center-block . org-latex-center-block
)
42 (clock . org-latex-clock
)
43 (code . org-latex-code
)
44 (comment .
(lambda (&rest args
) ""))
45 (comment-block .
(lambda (&rest args
) ""))
46 (drawer . org-latex-drawer
)
47 (dynamic-block . org-latex-dynamic-block
)
48 (entity . org-latex-entity
)
49 (example-block . org-latex-example-block
)
50 (export-block . org-latex-export-block
)
51 (export-snippet . org-latex-export-snippet
)
52 (fixed-width . org-latex-fixed-width
)
53 (footnote-definition . org-latex-footnote-definition
)
54 (footnote-reference . org-latex-footnote-reference
)
55 (headline . org-latex-headline
)
56 (horizontal-rule . org-latex-horizontal-rule
)
57 (inline-src-block . org-latex-inline-src-block
)
58 (inlinetask . org-latex-inlinetask
)
59 (italic . org-latex-italic
)
60 (item . org-latex-item
)
61 (keyword . org-latex-keyword
)
62 (latex-environment . org-latex-latex-environment
)
63 (latex-fragment . org-latex-latex-fragment
)
64 (line-break . org-latex-line-break
)
65 (link . org-latex-link
)
66 (paragraph . org-latex-paragraph
)
67 (plain-list . org-latex-plain-list
)
68 (plain-text . org-latex-plain-text
)
69 (planning . org-latex-planning
)
70 (property-drawer .
(lambda (&rest args
) ""))
71 (quote-block . org-latex-quote-block
)
72 (quote-section . org-latex-quote-section
)
73 (radio-target . org-latex-radio-target
)
74 (section . org-latex-section
)
75 (special-block . org-latex-special-block
)
76 (src-block . org-latex-src-block
)
77 (statistics-cookie . org-latex-statistics-cookie
)
78 (strike-through . org-latex-strike-through
)
79 (subscript . org-latex-subscript
)
80 (superscript . org-latex-superscript
)
81 (table . org-latex-table
)
82 (table-cell . org-latex-table-cell
)
83 (table-row . org-latex-table-row
)
84 (target . org-latex-target
)
85 (template . org-latex-template
)
86 (timestamp . org-latex-timestamp
)
87 (underline . org-latex-underline
)
88 (verbatim . org-latex-verbatim
)
89 (verse-block . org-latex-verse-block
))
90 :export-block
'("LATEX" "TEX")
92 '(?l
"Export to LaTeX"
93 ((?L
"As LaTeX buffer" org-latex-export-as-latex
)
94 (?l
"As LaTeX file" org-latex-export-to-latex
)
95 (?p
"As PDF file" org-latex-export-to-pdf
)
96 (?o
"As PDF file and open"
98 (if a
(org-latex-export-to-pdf t s v b
)
99 (org-open-file (org-latex-export-to-pdf nil s v b
)))))))
100 :options-alist
'((:latex-class
"LATEX_CLASS" nil org-latex-default-class t
)
101 (:latex-class-options
"LATEX_CLASS_OPTIONS" nil nil t
)
102 (:latex-header
"LATEX_HEADER" nil nil newline
)
103 (:latex-header-extra
"LATEX_HEADER_EXTRA" nil nil newline
)
104 (:latex-hyperref-p nil
"texht" org-latex-with-hyperref t
)
105 ;; Redefine regular options.
106 (:date
"DATE" nil
"\\today" t
)))
110 ;;; Internal Variables
112 (defconst org-latex-babel-language-alist
113 '(("af" .
"afrikaans")
115 ("bt-br" .
"brazilian")
121 ("de-at" .
"naustrian")
122 ("de-de" .
"ngerman")
125 ("en-au" .
"australian")
126 ("en-ca" .
"canadian")
127 ("en-gb" .
"british")
129 ("en-nz" .
"newzealand")
130 ("en-us" .
"american")
136 ("fr-ca" .
"canadien")
140 ("id" .
"indonesian")
150 ("pt" .
"portuguese")
154 ("sb" .
"uppersorbian")
162 ("uk" .
"ukrainian"))
163 "Alist between language code and corresponding Babel option.")
165 (defconst org-latex-table-matrix-macros
'(("bordermatrix" .
"\\cr")
166 ("qbordermatrix" .
"\\cr")
167 ("kbordermatrix" .
"\\\\"))
168 "Alist between matrix macros and their row ending.")
172 ;;; User Configurable Variables
174 (defgroup org-export-latex nil
175 "Options for exporting Org mode files to LaTeX."
176 :tag
"Org Export LaTeX"
182 (defcustom org-latex-default-class
"article"
183 "The default LaTeX class."
184 :group
'org-export-latex
185 :type
'(string :tag
"LaTeX class"))
187 (defcustom org-latex-classes
189 "\\documentclass[11pt]{article}"
190 ("\\section{%s}" .
"\\section*{%s}")
191 ("\\subsection{%s}" .
"\\subsection*{%s}")
192 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
193 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
194 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
196 "\\documentclass[11pt]{report}"
197 ("\\part{%s}" .
"\\part*{%s}")
198 ("\\chapter{%s}" .
"\\chapter*{%s}")
199 ("\\section{%s}" .
"\\section*{%s}")
200 ("\\subsection{%s}" .
"\\subsection*{%s}")
201 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
203 "\\documentclass[11pt]{book}"
204 ("\\part{%s}" .
"\\part*{%s}")
205 ("\\chapter{%s}" .
"\\chapter*{%s}")
206 ("\\section{%s}" .
"\\section*{%s}")
207 ("\\subsection{%s}" .
"\\subsection*{%s}")
208 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
209 "Alist of LaTeX classes and associated header and structure.
210 If #+LATEX_CLASS is set in the buffer, use its value and the
211 associated information. Here is the structure of each cell:
215 \(numbered-section . unnumbered-section)
221 The HEADER-STRING is the header that will be inserted into the
222 LaTeX file. It should contain the \\documentclass macro, and
223 anything else that is needed for this setup. To this header, the
224 following commands will be added:
226 - Calls to \\usepackage for all packages mentioned in the
227 variables `org-latex-default-packages-alist' and
228 `org-latex-packages-alist'. Thus, your header definitions
229 should avoid to also request these packages.
231 - Lines specified via \"#+LATEX_HEADER:\" and
232 \"#+LATEX_HEADER_EXTRA:\" keywords.
234 If you need more control about the sequence in which the header
235 is built up, or if you want to exclude one of these building
236 blocks for a particular class, you can use the following
237 macro-like placeholders.
239 [DEFAULT-PACKAGES] \\usepackage statements for default packages
240 [NO-DEFAULT-PACKAGES] do not include any of the default packages
241 [PACKAGES] \\usepackage statements for packages
242 [NO-PACKAGES] do not include the packages
243 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
244 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
248 \\documentclass{article}
249 [NO-DEFAULT-PACKAGES]
251 \\providecommand{\\alert}[1]{\\textbf{#1}}
254 will omit the default packages, and will include the
255 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
256 to \\providecommand, and then place \\usepackage commands based
257 on the content of `org-latex-packages-alist'.
259 If your header, `org-latex-default-packages-alist' or
260 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
261 AUTO will automatically be replaced with a coding system derived
262 from `buffer-file-coding-system'. See also the variable
263 `org-latex-inputenc-alist' for a way to influence this mechanism.
265 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\",
266 AUTO will be replaced with the language related to the language
267 code specified by `org-export-default-language', which see. Note
268 that constructions such as \"\\usepackage[french,AUTO,english]{babel}\"
271 The sectioning structure
272 ------------------------
274 The sectioning structure of the class is given by the elements
275 following the header string. For each sectioning level, a number
276 of strings is specified. A %s formatter is mandatory in each
277 section string and will be replaced by the title of the section.
279 Instead of a cons cell (numbered . unnumbered), you can also
280 provide a list of 2 or 4 elements,
282 \(numbered-open numbered-close)
286 \(numbered-open numbered-close unnumbered-open unnumbered-close)
288 providing opening and closing strings for a LaTeX environment
289 that should represent the document section. The opening clause
290 should have a %s to represent the section title.
292 Instead of a list of sectioning commands, you can also specify
293 a function name. That function will be called with two
294 parameters, the (reduced) level of the headline, and a predicate
295 non-nil when the headline should be numbered. It must return
296 a format string in which the section title will be added."
297 :group
'org-export-latex
299 (list (string :tag
"LaTeX class")
300 (string :tag
"LaTeX header")
301 (repeat :tag
"Levels" :inline t
304 (string :tag
" numbered")
305 (string :tag
"unnumbered"))
306 (list :tag
"Environment"
307 (string :tag
"Opening (numbered)")
308 (string :tag
"Closing (numbered)")
309 (string :tag
"Opening (unnumbered)")
310 (string :tag
"Closing (unnumbered)"))
311 (function :tag
"Hook computing sectioning"))))))
313 (defcustom org-latex-inputenc-alist nil
314 "Alist of inputenc coding system names, and what should really be used.
315 For example, adding an entry
317 (\"utf8\" . \"utf8x\")
319 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
320 are written as utf8 files."
321 :group
'org-export-latex
324 (string :tag
"Derived from buffer")
325 (string :tag
"Use this instead"))))
327 (defcustom org-latex-title-command
"\\maketitle"
328 "The command used to insert the title just after \\begin{document}.
329 If this string contains the formatting specification \"%s\" then
330 it will be used as a formatting string, passing the title as an
332 :group
'org-export-latex
335 (defcustom org-latex-toc-command
"\\tableofcontents\n\n"
336 "LaTeX command to set the table of contents, list of figures, etc.
337 This command only applies to the table of contents generated with
338 the toc:nil option, not to those generated with #+TOC keyword."
339 :group
'org-export-latex
342 (defcustom org-latex-with-hyperref t
343 "Toggle insertion of \\hypersetup{...} in the preamble."
344 :group
'org-export-latex
349 (defcustom org-latex-format-headline-function
350 'org-latex-format-headline-default-function
351 "Function for formatting the headline's text.
353 This function will be called with 5 arguments:
354 TODO the todo keyword (string or nil).
355 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
356 PRIORITY the priority of the headline (integer or nil)
357 TEXT the main headline text (string).
358 TAGS the tags as a list of strings (list of strings or nil).
360 The function result will be used in the section format string.
362 Use `org-latex-format-headline-default-function' by default,
363 which format headlines like for Org version prior to 8.0."
364 :group
'org-export-latex
366 :package-version
'(Org .
"8.0")
372 (defcustom org-latex-footnote-separator
"\\textsuperscript{,}\\,"
373 "Text used to separate footnotes."
374 :group
'org-export-latex
380 (defcustom org-latex-active-timestamp-format
"\\textit{%s}"
381 "A printf format string to be applied to active timestamps."
382 :group
'org-export-latex
385 (defcustom org-latex-inactive-timestamp-format
"\\textit{%s}"
386 "A printf format string to be applied to inactive timestamps."
387 :group
'org-export-latex
390 (defcustom org-latex-diary-timestamp-format
"\\textit{%s}"
391 "A printf format string to be applied to diary timestamps."
392 :group
'org-export-latex
398 (defcustom org-latex-image-default-option
""
399 "Default option for images."
400 :group
'org-export-latex
402 :package-version
'(Org .
"8.0")
405 (defcustom org-latex-image-default-width
".9\\linewidth"
406 "Default width for images.
407 This value will not be used if a height is provided."
408 :group
'org-export-latex
410 :package-version
'(Org .
"8.0")
413 (defcustom org-latex-image-default-height
""
414 "Default height for images.
415 This value will not be used if a width is provided, or if the
416 image is wrapped within a \"figure\" or \"wrapfigure\"
418 :group
'org-export-latex
420 :package-version
'(Org .
"8.0")
423 (defcustom org-latex-default-figure-position
"htb"
424 "Default position for latex figures."
425 :group
'org-export-latex
428 (defcustom org-latex-inline-image-rules
429 '(("file" .
"\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
430 "Rules characterizing image files that can be inlined into LaTeX.
432 A rule consists in an association whose key is the type of link
433 to consider, and value is a regexp that will be matched against
436 Note that, by default, the image extension *actually* allowed
437 depend on the way the LaTeX file is processed. When used with
438 pdflatex, pdf, jpg and png images are OK. When processing
439 through dvi to Postscript, only ps and eps are allowed. The
440 default we use here encompasses both."
441 :group
'org-export-latex
443 :package-version
'(Org .
"8.0")
444 :type
'(alist :key-type
(string :tag
"Type")
445 :value-type
(regexp :tag
"Path")))
447 (defcustom org-latex-link-with-unknown-path-format
"\\texttt{%s}"
448 "Format string for links with unknown path type."
449 :group
'org-export-latex
455 (defcustom org-latex-default-table-environment
"tabular"
456 "Default environment used to build tables."
457 :group
'org-export-latex
459 :package-version
'(Org .
"8.0")
462 (defcustom org-latex-default-table-mode
'table
463 "Default mode for tables.
465 Value can be a symbol among:
467 `table' Regular LaTeX table.
469 `math' In this mode, every cell is considered as being in math
470 mode and the complete table will be wrapped within a math
471 environment. It is particularly useful to write matrices.
473 `inline-math' This mode is almost the same as `math', but the
474 math environment will be inlined.
476 `verbatim' The table is exported as it appears in the Org
477 buffer, within a verbatim environment.
479 This value can be overridden locally with, i.e. \":mode math\" in
482 When modifying this variable, it may be useful to change
483 `org-latex-default-table-environment' accordingly."
484 :group
'org-export-latex
486 :package-version
'(Org .
"8.0")
487 :type
'(choice (const :tag
"Table" table
)
488 (const :tag
"Matrix" math
)
489 (const :tag
"Inline matrix" inline-math
)
490 (const :tag
"Verbatim" verbatim
)))
492 (defcustom org-latex-tables-centered t
493 "When non-nil, tables are exported in a center environment."
494 :group
'org-export-latex
497 (defcustom org-latex-tables-booktabs nil
498 "When non-nil, display tables in a formal \"booktabs\" style.
499 This option assumes that the \"booktabs\" package is properly
500 loaded in the header of the document. This value can be ignored
501 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
503 :group
'org-export-latex
505 :package-version
'(Org .
"8.0")
508 (defcustom org-latex-table-caption-above t
509 "When non-nil, place caption string at the beginning of the table.
510 Otherwise, place it near the end."
511 :group
'org-export-latex
514 (defcustom org-latex-table-scientific-notation
"%s\\,(%s)"
515 "Format string to display numbers in scientific notation.
516 The format should have \"%s\" twice, for mantissa and exponent
517 \(i.e., \"%s\\\\times10^{%s}\").
519 When nil, no transformation is made."
520 :group
'org-export-latex
522 :package-version
'(Org .
"8.0")
524 (string :tag
"Format string")
525 (const :tag
"No formatting")))
530 (defcustom org-latex-text-markup-alist
'((bold .
"\\textbf{%s}")
532 (italic .
"\\emph{%s}")
533 (strike-through .
"\\sout{%s}")
534 (underline .
"\\uline{%s}")
535 (verbatim . protectedtexttt
))
536 "Alist of LaTeX expressions to convert text markup.
538 The key must be a symbol among `bold', `code', `italic',
539 `strike-through', `underline' and `verbatim'. The value is
540 a formatting string to wrap fontified text with.
542 Value can also be set to the following symbols: `verb' and
543 `protectedtexttt'. For the former, Org will use \"\\verb\" to
544 create a format string and select a delimiter character that
545 isn't in the string. For the latter, Org will use \"\\texttt\"
546 to typeset and try to protect special characters.
548 If no association can be found for a given markup, text will be
550 :group
'org-export-latex
552 :options
'(bold code italic strike-through underline verbatim
))
557 (defcustom org-latex-format-drawer-function nil
558 "Function called to format a drawer in LaTeX code.
560 The function must accept two parameters:
561 NAME the drawer name, like \"LOGBOOK\"
562 CONTENTS the contents of the drawer.
564 The function should return the string to be exported.
566 For example, the variable could be set to the following function
567 in order to mimic default behaviour:
569 \(defun org-latex-format-drawer-default \(name contents\)
570 \"Format a drawer element for LaTeX export.\"
572 :group
'org-export-latex
578 (defcustom org-latex-format-inlinetask-function nil
579 "Function called to format an inlinetask in LaTeX code.
581 The function must accept six parameters:
582 TODO the todo keyword, as a string
583 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
584 PRIORITY the inlinetask priority, as a string
585 NAME the inlinetask name, as a string.
586 TAGS the inlinetask tags, as a list of strings.
587 CONTENTS the contents of the inlinetask, as a string.
589 The function should return the string to be exported.
591 For example, the variable could be set to the following function
592 in order to mimic default behaviour:
594 \(defun org-latex-format-inlinetask \(todo type priority name tags contents\)
595 \"Format an inline task element for LaTeX export.\"
599 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
600 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
603 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
604 \(mapconcat 'identity tags \":\")))))
605 \(format (concat \"\\\\begin{center}\\n\"
607 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
609 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
611 \"\\\\end{minipage}}\"
613 full-title contents))"
614 :group
'org-export-latex
620 (defcustom org-latex-listings nil
621 "Non-nil means export source code using the listings package.
623 This package will fontify source code, possibly even with color.
624 If you want to use this, you also need to make LaTeX use the
625 listings package, and if you want to have color, the color
626 package. Just add these to `org-latex-packages-alist', for
627 example using customize, or with something like:
630 \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
631 \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
635 \(setq org-latex-listings 'minted)
637 causes source code to be exported using the minted package as
638 opposed to listings. If you want to use minted, you need to add
639 the minted package to `org-latex-packages-alist', for example
640 using customize, or with
643 \(add-to-list 'org-latex-packages-alist '(\"\" \"minted\"))
645 In addition, it is necessary to install pygments
646 \(http://pygments.org), and to configure the variable
647 `org-latex-pdf-process' so that the -shell-escape option is
650 The minted choice has possible repercussions on the preview of
651 latex fragments (see `org-preview-latex-fragment'). If you run
652 into previewing problems, please consult
654 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
655 :group
'org-export-latex
657 (const :tag
"Use listings" t
)
658 (const :tag
"Use minted" 'minted
)
659 (const :tag
"Export verbatim" nil
)))
661 (defcustom org-latex-listings-langs
662 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
665 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
666 (html "HTML") (xml "XML")
667 (tex "TeX") (latex "[LaTeX]TeX")
668 (shell-script "bash")
670 (ocaml "Caml") (caml "Caml")
671 (sql "SQL") (sqlite "sql"))
672 "Alist mapping languages to their listing language counterpart.
673 The key is a symbol, the major mode symbol without the \"-mode\".
674 The value is the string that should be inserted as the language
675 parameter for the listings package. If the mode name and the
676 listings name are the same, the language does not need an entry
677 in this list - but it does not hurt if it is present."
678 :group
'org-export-latex
681 (symbol :tag
"Major mode ")
682 (string :tag
"Listings language"))))
684 (defcustom org-latex-listings-options nil
685 "Association list of options for the latex listings package.
687 These options are supplied as a comma-separated list to the
688 \\lstset command. Each element of the association list should be
689 a list containing two strings: the name of the option, and the
692 (setq org-latex-listings-options
693 '((\"basicstyle\" \"\\small\")
694 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
696 will typeset the code in a small size font with underlined, bold
699 Note that the same options will be applied to blocks of all
701 :group
'org-export-latex
704 (string :tag
"Listings option name ")
705 (string :tag
"Listings option value"))))
707 (defcustom org-latex-minted-langs
708 '((emacs-lisp "common-lisp")
711 (shell-script "bash")
713 "Alist mapping languages to their minted language counterpart.
714 The key is a symbol, the major mode symbol without the \"-mode\".
715 The value is the string that should be inserted as the language
716 parameter for the minted package. If the mode name and the
717 listings name are the same, the language does not need an entry
718 in this list - but it does not hurt if it is present.
720 Note that minted uses all lower case for language identifiers,
721 and that the full list of language identifiers can be obtained
724 pygmentize -L lexers"
725 :group
'org-export-latex
728 (symbol :tag
"Major mode ")
729 (string :tag
"Minted language"))))
731 (defcustom org-latex-minted-options nil
732 "Association list of options for the latex minted package.
734 These options are supplied within square brackets in
735 \\begin{minted} environments. Each element of the alist should
736 be a list containing two strings: the name of the option, and the
739 \(setq org-latex-minted-options
740 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
742 will result in src blocks being exported with
744 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
746 as the start of the minted environment. Note that the same
747 options will be applied to blocks of all languages."
748 :group
'org-export-latex
751 (string :tag
"Minted option name ")
752 (string :tag
"Minted option value"))))
754 (defvar org-latex-custom-lang-environments nil
755 "Alist mapping languages to language-specific LaTeX environments.
757 It is used during export of src blocks by the listings and minted
758 latex packages. For example,
760 \(setq org-latex-custom-lang-environments
761 '\(\(python \"pythoncode\"\)\)\)
763 would have the effect that if org encounters begin_src python
764 during latex export it will output
773 (defcustom org-latex-pdf-process
774 '("pdflatex -interaction nonstopmode -output-directory %o %f"
775 "pdflatex -interaction nonstopmode -output-directory %o %f"
776 "pdflatex -interaction nonstopmode -output-directory %o %f")
777 "Commands to process a LaTeX file to a PDF file.
778 This is a list of strings, each of them will be given to the
779 shell as a command. %f in the command will be replaced by the
780 full file name, %b by the file base name (i.e. without directory
781 and extension parts) and %o by the base directory of the file.
783 The reason why this is a list is that it usually takes several
784 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
785 does not have a clever mechanism to detect which of these
786 commands have to be run to get to a stable result, and it also
787 does not do any error checking.
789 By default, Org uses 3 runs of `pdflatex' to do the processing.
790 If you have texi2dvi on your system and if that does not cause
791 the infamous egrep/locale bug:
793 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
795 then `texi2dvi' is the superior choice as it automates the LaTeX
796 build process by calling the \"correct\" combinations of
797 auxiliary programs. Org does offer `texi2dvi' as one of the
798 customize options. Alternatively, `rubber' and `latexmk' also
799 provide similar functionality. The latter supports `biber' out
802 Alternatively, this may be a Lisp function that does the
803 processing, so you could use this to apply the machinery of
804 AUCTeX or the Emacs LaTeX mode. This function should accept the
805 file name as its single argument."
806 :group
'org-export-pdf
808 (repeat :tag
"Shell command sequence"
809 (string :tag
"Shell command"))
810 (const :tag
"2 runs of pdflatex"
811 ("pdflatex -interaction nonstopmode -output-directory %o %f"
812 "pdflatex -interaction nonstopmode -output-directory %o %f"))
813 (const :tag
"3 runs of pdflatex"
814 ("pdflatex -interaction nonstopmode -output-directory %o %f"
815 "pdflatex -interaction nonstopmode -output-directory %o %f"
816 "pdflatex -interaction nonstopmode -output-directory %o %f"))
817 (const :tag
"pdflatex,bibtex,pdflatex,pdflatex"
818 ("pdflatex -interaction nonstopmode -output-directory %o %f"
820 "pdflatex -interaction nonstopmode -output-directory %o %f"
821 "pdflatex -interaction nonstopmode -output-directory %o %f"))
822 (const :tag
"2 runs of xelatex"
823 ("xelatex -interaction nonstopmode -output-directory %o %f"
824 "xelatex -interaction nonstopmode -output-directory %o %f"))
825 (const :tag
"3 runs of xelatex"
826 ("xelatex -interaction nonstopmode -output-directory %o %f"
827 "xelatex -interaction nonstopmode -output-directory %o %f"
828 "xelatex -interaction nonstopmode -output-directory %o %f"))
829 (const :tag
"xelatex,bibtex,xelatex,xelatex"
830 ("xelatex -interaction nonstopmode -output-directory %o %f"
832 "xelatex -interaction nonstopmode -output-directory %o %f"
833 "xelatex -interaction nonstopmode -output-directory %o %f"))
834 (const :tag
"texi2dvi"
835 ("texi2dvi -p -b -V %f"))
837 ("rubber -d --into %o %f"))
838 (const :tag
"latexmk"
839 ("latexmk -g -pdf %f"))
842 (defcustom org-latex-logfiles-extensions
843 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
844 "The list of file extensions to consider as LaTeX logfiles.
845 The logfiles will be remove if `org-latex-remove-logfiles' is
847 :group
'org-export-latex
848 :type
'(repeat (string :tag
"Extension")))
850 (defcustom org-latex-remove-logfiles t
851 "Non-nil means remove the logfiles produced by PDF production.
852 By default, logfiles are files with these extensions: .aux, .idx,
853 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
854 logfiles to remove, set `org-latex-logfiles-extensions'."
855 :group
'org-export-latex
858 (defcustom org-latex-known-errors
859 '(("Reference.*?undefined" .
"[undefined reference]")
860 ("Citation.*?undefined" .
"[undefined citation]")
861 ("Undefined control sequence" .
"[undefined control sequence]")
862 ("^! LaTeX.*?Error" .
"[LaTeX error]")
863 ("^! Package.*?Error" .
"[package error]")
864 ("Runaway argument" .
"Runaway argument"))
865 "Alist of regular expressions and associated messages for the user.
866 The regular expressions are used to find possible errors in the
868 :group
'org-export-latex
870 :package-version
'(Org .
"8.0")
873 (string :tag
"Regexp")
874 (string :tag
"Message"))))
878 ;;; Internal Functions
880 (defun org-latex--caption/label-string
(element info
)
881 "Return caption and label LaTeX string for ELEMENT.
883 INFO is a plist holding contextual information. If there's no
884 caption nor label, return the empty string.
886 For non-floats, see `org-latex--wrap-label'."
887 (let* ((label (org-element-property :name element
))
888 (label-str (if (not (org-string-nw-p label
)) ""
889 (format "\\label{%s}"
890 (org-export-solidify-link-text label
))))
891 (main (org-export-get-caption element
))
892 (short (org-export-get-caption element t
))
893 (caption-from-attr-latex (org-export-read-attribute :attr_latex element
:caption
)))
895 ((org-string-nw-p caption-from-attr-latex
)
896 (concat caption-from-attr-latex
"\n"))
897 ((and (not main
) (equal label-str
"")) "")
898 ((not main
) (concat label-str
"\n"))
899 ;; Option caption format with short name.
900 (short (format "\\caption[%s]{%s%s}\n"
901 (org-export-data short info
)
903 (org-export-data main info
)))
904 ;; Standard caption format.
905 (t (format "\\caption{%s%s}\n" label-str
(org-export-data main info
))))))
907 (defun org-latex-guess-inputenc (header)
908 "Set the coding system in inputenc to what the buffer is.
910 HEADER is the LaTeX header string. This function only applies
911 when specified inputenc option is \"AUTO\".
913 Return the new header, as a string."
914 (let* ((cs (or (ignore-errors
915 (latexenc-coding-system-to-inputenc
916 (or org-export-coding-system buffer-file-coding-system
)))
919 ;; First translate if that is requested.
920 (setq cs
(or (cdr (assoc cs org-latex-inputenc-alist
)) cs
))
921 ;; Then find the \usepackage statement and replace the option.
922 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
923 cs header t nil
1))))
925 (defun org-latex-guess-babel-language (header info
)
926 "Set Babel's language according to LANGUAGE keyword.
928 HEADER is the LaTeX header string. INFO is the plist used as
929 a communication channel.
931 Insertion of guessed language only happens when Babel package has
932 explicitly been loaded. Then it is added to the rest of
935 The argument to Babel may be \"AUTO\" which is then replaced with
936 the language of the document or `org-export-default-language'
937 unless language in question is already loaded.
939 Return the new header."
940 (let ((language-code (plist-get info
:language
)))
941 ;; If no language is set or Babel package is not loaded, return
943 (if (or (not (stringp language-code
))
944 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header
)))
946 (let ((options (save-match-data
947 (org-split-string (match-string 1 header
) ",[ \t]*")))
948 (language (cdr (assoc language-code
949 org-latex-babel-language-alist
))))
950 ;; If LANGUAGE is already loaded, return header without AUTO.
951 ;; Otherwise, replace AUTO with language or append language if
952 ;; AUTO is not present.
954 (mapconcat (lambda (option) (if (equal "AUTO" option
) language option
))
955 (cond ((member language options
) (delete "AUTO" options
))
956 ((member "AUTO" options
) options
)
957 (t (append options
(list language
))))
961 (defun org-latex--find-verb-separator (s)
962 "Return a character not used in string S.
963 This is used to choose a separator for constructs like \\verb."
964 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
965 (loop for c across ll
966 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
967 return
(char-to-string c
))))
969 (defun org-latex--make-option-string (options)
970 "Return a comma separated string of keywords and values.
971 OPTIONS is an alist where the key is the options keyword as
972 a string, and the value a list containing the keyword value, or
974 (mapconcat (lambda (pair)
976 (when (> (length (second pair
)) 0)
977 (concat "=" (second pair
)))))
981 (defun org-latex--wrap-label (element output
)
982 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
983 This function shouldn't be used for floats. See
984 `org-latex--caption/label-string'."
985 (let ((label (org-element-property :name element
)))
986 (if (not (and (org-string-nw-p output
) (org-string-nw-p label
))) output
987 (concat (format "\\label{%s}\n" (org-export-solidify-link-text label
))
990 (defun org-latex--text-markup (text markup
)
991 "Format TEXT depending on MARKUP text markup.
992 See `org-latex-text-markup-alist' for details."
993 (let ((fmt (cdr (assq markup org-latex-text-markup-alist
))))
995 ;; No format string: Return raw text.
997 ;; Handle the `verb' special case: Find and appropriate separator
998 ;; and use "\\verb" command.
1000 (let ((separator (org-latex--find-verb-separator text
)))
1001 (concat "\\verb" separator text separator
)))
1002 ;; Handle the `protectedtexttt' special case: Protect some
1003 ;; special chars and use "\texttt{%s}" format string.
1004 ((eq 'protectedtexttt fmt
)
1006 (trans '(("\\" .
"\\textbackslash{}")
1007 ("~" .
"\\textasciitilde{}")
1008 ("^" .
"\\textasciicircum{}")))
1011 (while (string-match "[\\{}$%&_#~^]" text
)
1012 (setq char
(match-string 0 text
))
1013 (if (> (match-beginning 0) 0)
1014 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
1015 (setq text
(substring text
(1+ (match-beginning 0))))
1016 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
1017 rtn
(concat rtn char
)))
1018 (setq text
(concat rtn text
)
1020 (while (string-match "--" text
)
1021 (setq text
(replace-match "-{}-" t t text
)))
1023 ;; Else use format string.
1024 (t (format fmt text
)))))
1026 (defun org-latex--delayed-footnotes-definitions (element info
)
1027 "Return footnotes definitions in ELEMENT as a string.
1029 INFO is a plist used as a communication channel.
1031 Footnotes definitions are returned within \"\\footnotetxt{}\"
1034 This function is used within constructs that don't support
1035 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1036 \"\\footnotemark\" is used within the construct and the function
1037 just outside of it."
1041 "\\footnotetext[%s]{%s}"
1042 (org-export-get-footnote-number ref info
)
1045 (org-export-get-footnote-definition ref info
) info
))))
1046 ;; Find every footnote reference in ELEMENT.
1048 search-refs
; For byte-compiler.
1052 ;; Return a list of all footnote references never seen
1054 (org-element-map data
'footnote-reference
1056 (when (org-export-footnote-first-reference-p ref info
)
1058 (when (eq (org-element-property :type ref
) 'standard
)
1059 (funcall search-refs
1060 (org-export-get-footnote-definition ref info
)))))
1062 (reverse all-refs
)))))
1063 (funcall search-refs element
))
1070 (defun org-latex-template (contents info
)
1071 "Return complete document string after LaTeX conversion.
1072 CONTENTS is the transcoded contents string. INFO is a plist
1073 holding export options."
1074 (let ((title (org-export-data (plist-get info
:title
) info
)))
1077 (and (plist-get info
:time-stamp-file
)
1078 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1079 ;; Document class and packages.
1080 (let* ((class (plist-get info
:latex-class
))
1081 (class-options (plist-get info
:latex-class-options
))
1082 (header (nth 1 (assoc class org-latex-classes
)))
1083 (document-class-string
1084 (and (stringp header
)
1085 (if (not class-options
) header
1086 (replace-regexp-in-string
1087 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1088 class-options header t nil
1)))))
1089 (if (not document-class-string
)
1090 (user-error "Unknown LaTeX class `%s'" class
)
1091 (org-latex-guess-babel-language
1092 (org-latex-guess-inputenc
1093 (org-element-normalize-string
1094 (org-splice-latex-header
1095 document-class-string
1096 org-latex-default-packages-alist
1097 org-latex-packages-alist nil
1098 (concat (org-element-normalize-string
1099 (plist-get info
:latex-header
))
1100 (plist-get info
:latex-header-extra
)))))
1102 ;; Possibly limit depth for headline numbering.
1103 (let ((sec-num (plist-get info
:section-numbers
)))
1104 (when (integerp sec-num
)
1105 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
1107 (let ((author (and (plist-get info
:with-author
)
1108 (let ((auth (plist-get info
:author
)))
1109 (and auth
(org-export-data auth info
)))))
1110 (email (and (plist-get info
:with-email
)
1111 (org-export-data (plist-get info
:email
) info
))))
1112 (cond ((and author email
(not (string= "" email
)))
1113 (format "\\author{%s\\thanks{%s}}\n" author email
))
1114 ((or author email
) (format "\\author{%s}\n" (or author email
)))))
1116 (let ((date (and (plist-get info
:with-date
) (org-export-get-date info
))))
1117 (format "\\date{%s}\n" (org-export-data date info
)))
1119 (format "\\title{%s}\n" title
)
1120 ;; Hyperref options.
1121 (when (plist-get info
:latex-hyperref-p
)
1122 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1123 (or (plist-get info
:keywords
) "")
1124 (or (plist-get info
:description
) "")
1125 (if (not (plist-get info
:with-creator
)) ""
1126 (plist-get info
:creator
))))
1128 "\\begin{document}\n\n"
1130 (org-element-normalize-string
1131 (cond ((string= "" title
) nil
)
1132 ((not (stringp org-latex-title-command
)) nil
)
1133 ((string-match "\\(?:[^%]\\|^\\)%s"
1134 org-latex-title-command
)
1135 (format org-latex-title-command title
))
1136 (t org-latex-title-command
)))
1137 ;; Table of contents.
1138 (let ((depth (plist-get info
:with-toc
)))
1140 (concat (when (wholenump depth
)
1141 (format "\\setcounter{tocdepth}{%d}\n" depth
))
1142 org-latex-toc-command
)))
1146 (let ((creator-info (plist-get info
:with-creator
)))
1148 ((not creator-info
) "")
1149 ((eq creator-info
'comment
)
1150 (format "%% %s\n" (plist-get info
:creator
)))
1151 (t (concat (plist-get info
:creator
) "\n"))))
1153 "\\end{document}")))
1157 ;;; Transcode Functions
1161 (defun org-latex-bold (bold contents info
)
1162 "Transcode BOLD from Org to LaTeX.
1163 CONTENTS is the text with bold markup. INFO is a plist holding
1164 contextual information."
1165 (org-latex--text-markup contents
'bold
))
1170 (defun org-latex-center-block (center-block contents info
)
1171 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1172 CONTENTS holds the contents of the center block. INFO is a plist
1173 holding contextual information."
1174 (org-latex--wrap-label
1176 (format "\\begin{center}\n%s\\end{center}" contents
)))
1181 (defun org-latex-clock (clock contents info
)
1182 "Transcode a CLOCK element from Org to LaTeX.
1183 CONTENTS is nil. INFO is a plist holding contextual
1187 (format "\\textbf{%s} " org-clock-string
)
1188 (format org-latex-inactive-timestamp-format
1189 (concat (org-translate-time
1190 (org-element-property :raw-value
1191 (org-element-property :value clock
)))
1192 (let ((time (org-element-property :duration clock
)))
1193 (and time
(format " (%s)" time
)))))
1199 (defun org-latex-code (code contents info
)
1200 "Transcode a CODE object from Org to LaTeX.
1201 CONTENTS is nil. INFO is a plist used as a communication
1203 (org-latex--text-markup (org-element-property :value code
) 'code
))
1208 (defun org-latex-drawer (drawer contents info
)
1209 "Transcode a DRAWER element from Org to LaTeX.
1210 CONTENTS holds the contents of the block. INFO is a plist
1211 holding contextual information."
1212 (let* ((name (org-element-property :drawer-name drawer
))
1213 (output (if (functionp org-latex-format-drawer-function
)
1214 (funcall org-latex-format-drawer-function
1216 ;; If there's no user defined function: simply
1217 ;; display contents of the drawer.
1219 (org-latex--wrap-label drawer output
)))
1224 (defun org-latex-dynamic-block (dynamic-block contents info
)
1225 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1226 CONTENTS holds the contents of the block. INFO is a plist
1227 holding contextual information. See `org-export-data'."
1228 (org-latex--wrap-label dynamic-block contents
))
1233 (defun org-latex-entity (entity contents info
)
1234 "Transcode an ENTITY object from Org to LaTeX.
1235 CONTENTS are the definition itself. INFO is a plist holding
1236 contextual information."
1237 (let ((ent (org-element-property :latex entity
)))
1238 (if (org-element-property :latex-math-p entity
) (format "$%s$" ent
) ent
)))
1243 (defun org-latex-example-block (example-block contents info
)
1244 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1245 CONTENTS is nil. INFO is a plist holding contextual
1247 (when (org-string-nw-p (org-element-property :value example-block
))
1248 (org-latex--wrap-label
1250 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1251 (org-export-format-code-default example-block info
)))))
1256 (defun org-latex-export-block (export-block contents info
)
1257 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1258 CONTENTS is nil. INFO is a plist holding contextual information."
1259 (when (member (org-element-property :type export-block
) '("LATEX" "TEX"))
1260 (org-remove-indentation (org-element-property :value export-block
))))
1265 (defun org-latex-export-snippet (export-snippet contents info
)
1266 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1267 CONTENTS is nil. INFO is a plist holding contextual information."
1268 (when (eq (org-export-snippet-backend export-snippet
) 'latex
)
1269 (org-element-property :value export-snippet
)))
1274 (defun org-latex-fixed-width (fixed-width contents info
)
1275 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1276 CONTENTS is nil. INFO is a plist holding contextual information."
1277 (org-latex--wrap-label
1279 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1280 (org-remove-indentation
1281 (org-element-property :value fixed-width
)))))
1284 ;;;; Footnote Reference
1286 (defun org-latex-footnote-reference (footnote-reference contents info
)
1287 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1288 CONTENTS is nil. INFO is a plist holding contextual information."
1290 ;; Insert separator between two footnotes in a row.
1291 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1292 (when (eq (org-element-type prev
) 'footnote-reference
)
1293 org-latex-footnote-separator
))
1295 ;; Use \footnotemark if the footnote has already been defined.
1296 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1297 (format "\\footnotemark[%s]{}"
1298 (org-export-get-footnote-number footnote-reference info
)))
1299 ;; Use \footnotemark if reference is within another footnote
1300 ;; reference, footnote definition or table cell.
1301 ((loop for parent in
(org-export-get-genealogy footnote-reference
)
1302 thereis
(memq (org-element-type parent
)
1303 '(footnote-reference footnote-definition table-cell
)))
1305 ;; Otherwise, define it with \footnote command.
1307 (let ((def (org-export-get-footnote-definition footnote-reference info
)))
1309 (format "\\footnote{%s}" (org-trim (org-export-data def info
)))
1310 ;; Retrieve all footnote references within the footnote and
1311 ;; add their definition after it, since LaTeX doesn't support
1313 (org-latex--delayed-footnotes-definitions def info
)))))))
1318 (defun org-latex-headline (headline contents info
)
1319 "Transcode a HEADLINE element from Org to LaTeX.
1320 CONTENTS holds the contents of the headline. INFO is a plist
1321 holding contextual information."
1322 (unless (org-element-property :footnote-section-p headline
)
1323 (let* ((class (plist-get info
:latex-class
))
1324 (level (org-export-get-relative-level headline info
))
1325 (numberedp (org-export-numbered-headline-p headline info
))
1326 (class-sectionning (assoc class org-latex-classes
))
1327 ;; Section formatting will set two placeholders: one for
1328 ;; the title and the other for the contents.
1330 (let ((sec (if (functionp (nth 2 class-sectionning
))
1331 (funcall (nth 2 class-sectionning
) level numberedp
)
1332 (nth (1+ level
) class-sectionning
))))
1334 ;; No section available for that LEVEL.
1336 ;; Section format directly returned by a function. Add
1337 ;; placeholder for contents.
1338 ((stringp sec
) (concat sec
"\n%s"))
1339 ;; (numbered-section . unnumbered-section)
1340 ((not (consp (cdr sec
)))
1341 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
1342 ;; (numbered-open numbered-close)
1344 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
1345 ;; (num-in num-out no-num-in no-num-out)
1347 (if numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))
1348 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
1349 (text (org-export-data (org-element-property :title headline
) info
))
1351 (and (plist-get info
:with-todo-keywords
)
1352 (let ((todo (org-element-property :todo-keyword headline
)))
1353 (and todo
(org-export-data todo info
)))))
1354 (todo-type (and todo
(org-element-property :todo-type headline
)))
1355 (tags (and (plist-get info
:with-tags
)
1356 (org-export-get-tags headline info
)))
1357 (priority (and (plist-get info
:with-priority
)
1358 (org-element-property :priority headline
)))
1359 ;; Create the headline text along with a no-tag version.
1360 ;; The latter is required to remove tags from toc.
1361 (full-text (funcall org-latex-format-headline-function
1362 todo todo-type priority text tags
))
1363 ;; Associate \label to the headline for internal links.
1365 (format "\\label{sec-%s}\n"
1366 (mapconcat 'number-to-string
1367 (org-export-get-headline-number headline info
)
1370 (make-string (org-element-property :pre-blank headline
) 10)))
1371 (if (or (not section-fmt
) (org-export-low-level-p headline info
))
1372 ;; This is a deep sub-tree: export it as a list item. Also
1373 ;; export as items headlines for which no section format has
1375 (let ((low-level-body
1377 ;; If headline is the first sibling, start a list.
1378 (when (org-export-first-sibling-p headline info
)
1379 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
1381 "\\item " full-text
"\n" headline-label pre-blanks contents
)))
1382 ;; If headline is not the last sibling simply return
1383 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1385 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1386 (replace-regexp-in-string
1388 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
1390 ;; This is a standard headline. Export it as a section. Add
1391 ;; an alternative heading when possible, and when this is not
1392 ;; identical to the usual heading.
1394 (funcall org-latex-format-headline-function
1395 todo todo-type priority
1397 (org-export-get-alt-title headline info
) info
)
1398 (and (eq (plist-get info
:with-tags
) t
) tags
))))
1399 (if (and numberedp opt-title
1400 (not (equal opt-title full-text
))
1401 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt
))
1402 (format (replace-match "\\1[%s]" nil nil section-fmt
1)
1403 ;; Replace square brackets with parenthesis
1404 ;; since square brackets are not supported in
1405 ;; optional arguments.
1406 (replace-regexp-in-string
1407 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title
))
1409 (concat headline-label pre-blanks contents
))
1410 ;; Impossible to add an alternative heading. Fallback to
1411 ;; regular sectioning format string.
1412 (format section-fmt full-text
1413 (concat headline-label pre-blanks contents
))))))))
1415 (defun org-latex-format-headline-default-function
1416 (todo todo-type priority text tags
)
1417 "Default format function for a headline.
1418 See `org-latex-format-headline-function' for details."
1420 (and todo
(format "{\\bfseries\\sffamily %s} " todo
))
1421 (and priority
(format "\\framebox{\\#%c} " priority
))
1424 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags
":")))))
1427 ;;;; Horizontal Rule
1429 (defun org-latex-horizontal-rule (horizontal-rule contents info
)
1430 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1431 CONTENTS is nil. INFO is a plist holding contextual information."
1432 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule
))
1433 (prev (org-export-get-previous-element horizontal-rule info
)))
1435 ;; Make sure the rule doesn't start at the end of the current
1436 ;; line by separating it with a blank line from previous element.
1438 (let ((prev-blank (org-element-property :post-blank prev
)))
1439 (or (not prev-blank
) (zerop prev-blank
))))
1441 (org-latex--wrap-label
1443 (format "\\rule{%s}{%s}"
1444 (or (plist-get attr
:width
) "\\linewidth")
1445 (or (plist-get attr
:thickness
) "0.5pt"))))))
1448 ;;;; Inline Src Block
1450 (defun org-latex-inline-src-block (inline-src-block contents info
)
1451 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1452 CONTENTS holds the contents of the item. INFO is a plist holding
1453 contextual information."
1454 (let* ((code (org-element-property :value inline-src-block
))
1455 (separator (org-latex--find-verb-separator code
)))
1457 ;; Do not use a special package: transcode it verbatim.
1458 ((not org-latex-listings
)
1459 (concat "\\verb" separator code separator
))
1460 ;; Use minted package.
1461 ((eq org-latex-listings
'minted
)
1462 (let* ((org-lang (org-element-property :language inline-src-block
))
1463 (mint-lang (or (cadr (assq (intern org-lang
)
1464 org-latex-minted-langs
))
1466 (options (org-latex--make-option-string
1467 org-latex-minted-options
)))
1468 (concat (format "\\mint%s{%s}"
1469 (if (string= options
"") "" (format "[%s]" options
))
1471 separator code separator
)))
1472 ;; Use listings package.
1474 ;; Maybe translate language's name.
1475 (let* ((org-lang (org-element-property :language inline-src-block
))
1476 (lst-lang (or (cadr (assq (intern org-lang
)
1477 org-latex-listings-langs
))
1479 (options (org-latex--make-option-string
1480 (append org-latex-listings-options
1481 `(("language" ,lst-lang
))))))
1482 (concat (format "\\lstinline[%s]" options
)
1483 separator code separator
))))))
1488 (defun org-latex-inlinetask (inlinetask contents info
)
1489 "Transcode an INLINETASK element from Org to LaTeX.
1490 CONTENTS holds the contents of the block. INFO is a plist
1491 holding contextual information."
1492 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1493 (todo (and (plist-get info
:with-todo-keywords
)
1494 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1495 (and todo
(org-export-data todo info
)))))
1496 (todo-type (org-element-property :todo-type inlinetask
))
1497 (tags (and (plist-get info
:with-tags
)
1498 (org-export-get-tags inlinetask info
)))
1499 (priority (and (plist-get info
:with-priority
)
1500 (org-element-property :priority inlinetask
))))
1501 ;; If `org-latex-format-inlinetask-function' is provided, call it
1502 ;; with appropriate arguments.
1503 (if (functionp org-latex-format-inlinetask-function
)
1504 (funcall org-latex-format-inlinetask-function
1505 todo todo-type priority title tags contents
)
1506 ;; Otherwise, use a default template.
1507 (org-latex--wrap-label
1511 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1512 (when priority
(format "\\framebox{\\#%c} " priority
))
1514 (when tags
(format "\\hfill{}\\textsc{:%s:}"
1515 (mapconcat 'identity tags
":"))))))
1516 (format (concat "\\begin{center}\n"
1518 "\\begin{minipage}[c]{.6\\textwidth}\n"
1520 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1525 full-title contents
))))))
1530 (defun org-latex-italic (italic contents info
)
1531 "Transcode ITALIC from Org to LaTeX.
1532 CONTENTS is the text with italic markup. INFO is a plist holding
1533 contextual information."
1534 (org-latex--text-markup contents
'italic
))
1539 (defun org-latex-item (item contents info
)
1540 "Transcode an ITEM element from Org to LaTeX.
1541 CONTENTS holds the contents of the item. INFO is a plist holding
1542 contextual information."
1544 (let ((count (org-element-property :counter item
))
1546 ;; Determine level of current item to determine the
1547 ;; correct LaTeX counter to use (enumi, enumii...).
1548 (let ((parent item
) (level 0))
1549 (while (memq (org-element-type
1550 (setq parent
(org-export-get-parent parent
)))
1552 (when (and (eq (org-element-type parent
) 'plain-list
)
1553 (eq (org-element-property :type parent
)
1559 (format "\\setcounter{enum%s}{%s}\n"
1560 (nth (1- level
) '("i" "ii" "iii" "iv"))
1562 (checkbox (case (org-element-property :checkbox item
)
1563 (on "$\\boxtimes$ ")
1565 (trans "$\\boxminus$ ")))
1566 (tag (let ((tag (org-element-property :tag item
)))
1567 ;; Check-boxes must belong to the tag.
1568 (and tag
(format "[{%s}] "
1570 (org-export-data tag info
)))))))
1571 (concat counter
"\\item" (or tag
(concat " " checkbox
))
1572 (and contents
(org-trim contents
))
1573 ;; If there are footnotes references in tag, be sure to
1574 ;; add their definition at the end of the item. This
1575 ;; workaround is necessary since "\footnote{}" command is
1576 ;; not supported in tags.
1578 (org-latex--delayed-footnotes-definitions
1579 (org-element-property :tag item
) info
)))))
1584 (defun org-latex-keyword (keyword contents info
)
1585 "Transcode a KEYWORD element from Org to LaTeX.
1586 CONTENTS is nil. INFO is a plist holding contextual information."
1587 (let ((key (org-element-property :key keyword
))
1588 (value (org-element-property :value keyword
)))
1590 ((string= key
"LATEX") value
)
1591 ((string= key
"INDEX") (format "\\index{%s}" value
))
1592 ((string= key
"TOC")
1593 (let ((value (downcase value
)))
1595 ((string-match "\\<headlines\\>" value
)
1596 (let ((depth (or (and (string-match "[0-9]+" value
)
1597 (string-to-number (match-string 0 value
)))
1598 (plist-get info
:with-toc
))))
1600 (when (wholenump depth
)
1601 (format "\\setcounter{tocdepth}{%s}\n" depth
))
1602 "\\tableofcontents")))
1603 ((string= "tables" value
) "\\listoftables")
1604 ((string= "listings" value
)
1606 ((eq org-latex-listings
'minted
) "\\listoflistings")
1607 (org-latex-listings "\\lstlistoflistings")
1608 ;; At the moment, src blocks with a caption are wrapped
1609 ;; into a figure environment.
1610 (t "\\listoffigures")))))))))
1613 ;;;; Latex Environment
1615 (defun org-latex-latex-environment (latex-environment contents info
)
1616 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1617 CONTENTS is nil. INFO is a plist holding contextual information."
1618 (when (plist-get info
:with-latex
)
1619 (let ((label (org-element-property :name latex-environment
))
1620 (value (org-remove-indentation
1621 (org-element-property :value latex-environment
))))
1622 (if (not (org-string-nw-p label
)) value
1623 ;; Environment is labelled: label must be within the environment
1624 ;; (otherwise, a reference pointing to that element will count
1625 ;; the section instead).
1628 (goto-char (point-min))
1631 (format "\\label{%s}\n" (org-export-solidify-link-text label
)))
1632 (buffer-string))))))
1637 (defun org-latex-latex-fragment (latex-fragment contents info
)
1638 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1639 CONTENTS is nil. INFO is a plist holding contextual information."
1640 (when (plist-get info
:with-latex
)
1641 (org-element-property :value latex-fragment
)))
1646 (defun org-latex-line-break (line-break contents info
)
1647 "Transcode a LINE-BREAK object from Org to LaTeX.
1648 CONTENTS is nil. INFO is a plist holding contextual information."
1654 (defun org-latex--inline-image (link info
)
1655 "Return LaTeX code for an inline image.
1656 LINK is the link pointing to the inline image. INFO is a plist
1657 used as a communication channel."
1658 (let* ((parent (org-export-get-parent-element link
))
1659 (path (let ((raw-path (org-element-property :path link
)))
1660 (if (not (file-name-absolute-p raw-path
)) raw-path
1661 (expand-file-name raw-path
))))
1662 (filetype (file-name-extension path
))
1663 (caption (org-latex--caption/label-string parent info
))
1664 ;; Retrieve latex attributes from the element around.
1665 (attr (org-export-read-attribute :attr_latex parent
))
1666 (float (let ((float (plist-get attr
:float
)))
1667 (cond ((and (not float
) (plist-member attr
:float
)) nil
)
1668 ((string= float
"wrap") 'wrap
)
1669 ((string= float
"multicolumn") 'multicolumn
)
1671 (org-element-property :caption parent
)
1672 (org-string-nw-p (plist-get attr
:caption
)))
1675 (let ((place (plist-get attr
:placement
)))
1676 (cond (place (format "%s" place
))
1677 ((eq float
'wrap
) "{l}{0.5\\textwidth}")
1679 (format "[%s]" org-latex-default-figure-position
))
1681 (comment-include (if (plist-get attr
:comment-include
) "%" ""))
1682 ;; It is possible to specify width and height in the
1683 ;; ATTR_LATEX line, and also via default variables.
1684 (width (cond ((plist-get attr
:width
))
1685 ((plist-get attr
:height
) "")
1686 ((eq float
'wrap
) "0.48\\textwidth")
1687 (t org-latex-image-default-width
)))
1688 (height (cond ((plist-get attr
:height
))
1689 ((or (plist-get attr
:width
)
1690 (memq float
'(figure wrap
))) "")
1691 (t org-latex-image-default-height
)))
1692 (options (let ((opt (or (plist-get attr
:options
)
1693 org-latex-image-default-option
)))
1694 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt
)) opt
1695 (match-string 1 opt
))))
1697 (if (member filetype
'("tikz" "pgf"))
1699 ;; - use \input to read in image file.
1700 ;; - if options are present, wrap in a tikzpicture environment.
1701 ;; - if width or height are present, use \resizebox to change
1704 (setq image-code
(format "\\input{%s}" path
))
1705 (when (org-string-nw-p options
)
1707 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1710 (when (or (org-string-nw-p width
) (org-string-nw-p height
))
1711 (setq image-code
(format "\\resizebox{%s}{%s}{%s}"
1712 (if (org-string-nw-p width
) width
"!")
1713 (if (org-string-nw-p height
) height
"!")
1715 ;; For other images:
1716 ;; - add width and height to options.
1717 ;; - include the image with \includegraphics.
1718 (when (org-string-nw-p width
)
1719 (setq options
(concat options
",width=" width
)))
1720 (when (org-string-nw-p height
)
1721 (setq options
(concat options
",height=" height
)))
1723 (format "\\includegraphics%s{%s}"
1724 (cond ((not (org-string-nw-p options
)) "")
1725 ((= (aref options
0) ?
,)
1726 (format "[%s]"(substring options
1)))
1727 (t (format "[%s]" options
)))
1729 (when (equal filetype
"svg")
1730 (setq image-code
(replace-regexp-in-string "^\\\\includegraphics"
1734 (setq image-code
(replace-regexp-in-string "\\.svg}"
1738 ;; Return proper string, depending on FLOAT.
1740 (wrap (format "\\begin{wrapfigure}%s
1743 %s\\end{wrapfigure}" placement comment-include image-code caption
))
1744 (multicolumn (format "\\begin{figure*}%s
1747 %s\\end{figure*}" placement comment-include image-code caption
))
1748 (figure (format "\\begin{figure}%s
1751 %s\\end{figure}" placement comment-include image-code caption
))
1752 (otherwise image-code
))))
1754 (defun org-latex-link (link desc info
)
1755 "Transcode a LINK object from Org to LaTeX.
1757 DESC is the description part of the link, or the empty string.
1758 INFO is a plist holding contextual information. See
1760 (let* ((type (org-element-property :type link
))
1761 (raw-path (org-element-property :path link
))
1762 ;; Ensure DESC really exists, or set it to nil.
1763 (desc (and (not (string= desc
"")) desc
))
1764 (imagep (org-export-inline-image-p
1765 link org-latex-inline-image-rules
))
1767 ((member type
'("http" "https" "ftp" "mailto"))
1768 (concat type
":" raw-path
))
1769 ((string= type
"file")
1770 (if (not (file-name-absolute-p raw-path
)) raw-path
1771 (concat "file://" (expand-file-name raw-path
))))
1776 (imagep (org-latex--inline-image link info
))
1777 ;; Radio link: Transcode target's contents and use them as link's
1779 ((string= type
"radio")
1780 (let ((destination (org-export-resolve-radio-link link info
)))
1782 (format "\\hyperref[%s]{%s}"
1783 (org-export-solidify-link-text path
)
1784 (org-export-data (org-element-contents destination
) info
)))))
1785 ;; Links pointing to a headline: Find destination and build
1786 ;; appropriate referencing command.
1787 ((member type
'("custom-id" "fuzzy" "id"))
1788 (let ((destination (if (string= type
"fuzzy")
1789 (org-export-resolve-fuzzy-link link info
)
1790 (org-export-resolve-id-link link info
))))
1791 (case (org-element-type destination
)
1792 ;; Id link points to an external file.
1794 (if desc
(format "\\href{%s}{%s}" destination desc
)
1795 (format "\\url{%s}" destination
)))
1796 ;; Fuzzy link points nowhere.
1798 (format org-latex-link-with-unknown-path-format
1801 (org-element-property :raw-link link
) info
))))
1802 ;; LINK points to a headline. If headlines are numbered
1803 ;; and the link has no description, display headline's
1804 ;; number. Otherwise, display description or headline's
1811 (org-export-get-headline-number destination info
)
1813 (if (and (plist-get info
:section-numbers
) (not desc
))
1814 (format "\\ref{%s}" label
)
1815 (format "\\hyperref[%s]{%s}" label
1818 (org-element-property :title destination
) info
))))))
1819 ;; Fuzzy link points to a target. Do as above.
1821 (let ((path (org-export-solidify-link-text path
)))
1822 (if (not desc
) (format "\\ref{%s}" path
)
1823 (format "\\hyperref[%s]{%s}" path desc
)))))))
1824 ;; Coderef: replace link with the reference name or the
1825 ;; equivalent line number.
1826 ((string= type
"coderef")
1827 (format (org-export-get-coderef-format path desc
)
1828 (org-export-resolve-coderef path info
)))
1829 ;; Link type is handled by a special function.
1830 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
1831 (funcall protocol
(org-link-unescape path
) desc
'latex
))
1832 ;; External link with a description part.
1833 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
1834 ;; External link without a description part.
1835 (path (format "\\url{%s}" path
))
1836 ;; No path, only description. Try to do something useful.
1837 (t (format org-latex-link-with-unknown-path-format desc
)))))
1842 (defun org-latex-paragraph (paragraph contents info
)
1843 "Transcode a PARAGRAPH element from Org to LaTeX.
1844 CONTENTS is the contents of the paragraph, as a string. INFO is
1845 the plist used as a communication channel."
1851 (defun org-latex-plain-list (plain-list contents info
)
1852 "Transcode a PLAIN-LIST element from Org to LaTeX.
1853 CONTENTS is the contents of the list. INFO is a plist holding
1854 contextual information."
1855 (let* ((type (org-element-property :type plain-list
))
1856 (attr (org-export-read-attribute :attr_latex plain-list
))
1857 (latex-type (let ((env (plist-get attr
:environment
)))
1858 (cond (env (format "%s" env
))
1859 ((eq type
'ordered
) "enumerate")
1860 ((eq type
'unordered
) "itemize")
1861 ((eq type
'descriptive
) "description")))))
1862 (org-latex--wrap-label
1864 (format "\\begin{%s}%s\n%s\\end{%s}"
1866 ;; Put optional arguments, if any inside square brackets
1868 (let ((options (format "%s" (or (plist-get attr
:options
) ""))))
1869 (cond ((equal options
"") "")
1870 ((string-match "\\`\\[.*\\]\\'" options
) options
)
1871 (t (format "[%s]" options
))))
1878 (defun org-latex-plain-text (text info
)
1879 "Transcode a TEXT string from Org to LaTeX.
1880 TEXT is the string to transcode. INFO is a plist holding
1881 contextual information."
1882 (let ((specialp (plist-get info
:with-special-strings
))
1884 ;; Protect %, #, &, $, _, { and }.
1885 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output
)
1888 (format "\\%s" (match-string 2 output
)) nil t output
2)))
1891 (replace-regexp-in-string
1892 "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil
2))
1893 ;; Protect \. If special strings are used, be careful not to
1894 ;; protect "\" in "\-" constructs.
1895 (let ((symbols (if specialp
"-%$#&{}^_\\" "%$#&{}^_\\")))
1897 (replace-regexp-in-string
1898 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols
)
1899 "$\\backslash$" output nil t
1)))
1902 (replace-regexp-in-string
1903 "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t
2))
1904 ;; Activate smart quotes. Be sure to provide original TEXT string
1905 ;; since OUTPUT may have been modified.
1906 (when (plist-get info
:with-smart-quotes
)
1907 (setq output
(org-export-activate-smart-quotes output
:latex info text
)))
1908 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1909 (let ((case-fold-search nil
)
1911 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start
)
1912 (setq output
(replace-match
1913 (format "\\%s{}" (match-string 1 output
)) nil t output
)
1914 start
(match-end 0))))
1915 ;; Convert special strings.
1918 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t
)))
1919 ;; Handle break preservation if required.
1920 (when (plist-get info
:preserve-breaks
)
1921 (setq output
(replace-regexp-in-string
1922 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output
)))
1929 (defun org-latex-planning (planning contents info
)
1930 "Transcode a PLANNING element from Org to LaTeX.
1931 CONTENTS is nil. INFO is a plist holding contextual
1939 (let ((closed (org-element-property :closed planning
)))
1942 (format "\\textbf{%s} " org-closed-string
)
1943 (format org-latex-inactive-timestamp-format
1945 (org-element-property :raw-value closed
))))))
1946 (let ((deadline (org-element-property :deadline planning
)))
1949 (format "\\textbf{%s} " org-deadline-string
)
1950 (format org-latex-active-timestamp-format
1952 (org-element-property :raw-value deadline
))))))
1953 (let ((scheduled (org-element-property :scheduled planning
)))
1956 (format "\\textbf{%s} " org-scheduled-string
)
1957 (format org-latex-active-timestamp-format
1959 (org-element-property :raw-value scheduled
))))))))
1966 (defun org-latex-quote-block (quote-block contents info
)
1967 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1968 CONTENTS holds the contents of the block. INFO is a plist
1969 holding contextual information."
1970 (org-latex--wrap-label
1972 (format "\\begin{quote}\n%s\\end{quote}" contents
)))
1977 (defun org-latex-quote-section (quote-section contents info
)
1978 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1979 CONTENTS is nil. INFO is a plist holding contextual information."
1980 (let ((value (org-remove-indentation
1981 (org-element-property :value quote-section
))))
1982 (when value
(format "\\begin{verbatim}\n%s\\end{verbatim}" value
))))
1987 (defun org-latex-radio-target (radio-target text info
)
1988 "Transcode a RADIO-TARGET object from Org to LaTeX.
1989 TEXT is the text of the target. INFO is a plist holding
1990 contextual information."
1991 (format "\\label{%s}%s"
1992 (org-export-solidify-link-text
1993 (org-element-property :value radio-target
))
1999 (defun org-latex-section (section contents info
)
2000 "Transcode a SECTION element from Org to LaTeX.
2001 CONTENTS holds the contents of the section. INFO is a plist
2002 holding contextual information."
2008 (defun org-latex-special-block (special-block contents info
)
2009 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2010 CONTENTS holds the contents of the block. INFO is a plist
2011 holding contextual information."
2012 (let ((type (downcase (org-element-property :type special-block
)))
2013 (opt (org-export-read-attribute :attr_latex special-block
:options
)))
2014 (concat (format "\\begin{%s}%s\n" type
(or opt
""))
2015 ;; Insert any label or caption within the block
2016 ;; (otherwise, a reference pointing to that element will
2017 ;; count the section instead).
2018 (org-latex--caption/label-string special-block info
)
2020 (format "\\end{%s}" type
))))
2025 (defun org-latex-src-block (src-block contents info
)
2026 "Transcode a SRC-BLOCK element from Org to LaTeX.
2027 CONTENTS holds the contents of the item. INFO is a plist holding
2028 contextual information."
2029 (when (org-string-nw-p (org-element-property :value src-block
))
2030 (let* ((lang (org-element-property :language src-block
))
2031 (caption (org-element-property :caption src-block
))
2032 (label (org-element-property :name src-block
))
2033 (custom-env (and lang
2034 (cadr (assq (intern lang
)
2035 org-latex-custom-lang-environments
))))
2036 (num-start (case (org-element-property :number-lines src-block
)
2037 (continued (org-export-get-loc src-block info
))
2039 (retain-labels (org-element-property :retain-labels src-block
))
2040 (attributes (org-export-read-attribute :attr_latex src-block
))
2041 (float (plist-get attributes
:float
)))
2043 ;; Case 1. No source fontification.
2044 ((not org-latex-listings
)
2045 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2047 (cond ((and (not float
) (plist-member attributes
:float
)) "%s")
2048 ((string= "multicolumn" float
)
2049 (format "\\begin{figure*}[%s]\n%%s%s\n\\end{figure*}"
2050 org-latex-default-figure-position
2053 (format "\\begin{figure}[H]\n%%s%s\n\\end{figure}"
2058 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2059 (org-export-format-code-default src-block info
))))))
2060 ;; Case 2. Custom environment.
2061 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2063 (org-export-format-code-default src-block info
)
2065 ;; Case 3. Use minted package.
2066 ((eq org-latex-listings
'minted
)
2067 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2069 (cond ((and (not float
) (plist-member attributes
:float
)) "%s")
2070 ((string= "multicolumn" float
)
2071 (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
2074 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2079 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2081 (org-latex--make-option-string
2082 (if (or (not num-start
)
2083 (assoc "linenos" org-latex-minted-options
))
2084 org-latex-minted-options
2087 ("firstnumber" ,(number-to-string (1+ num-start
))))
2088 org-latex-minted-options
)))
2090 (or (cadr (assq (intern lang
) org-latex-minted-langs
)) lang
)
2092 (let* ((code-info (org-export-unravel-code src-block
))
2096 (org-split-string (car code-info
)
2098 (org-export-format-code
2100 (lambda (loc num ref
)
2104 ;; Ensure references are flushed to the right,
2105 ;; separated with 6 spaces from the widest line
2107 (concat (make-string (+ (- max-width
(length loc
)) 6)
2109 (format "(%s)" ref
)))))
2110 nil
(and retain-labels
(cdr code-info
)))))))
2112 (format float-env body
)))
2113 ;; Case 4. Use listings package.
2116 (or (cadr (assq (intern lang
) org-latex-listings-langs
)) lang
))
2119 (let ((main (org-export-get-caption src-block
))
2120 (secondary (org-export-get-caption src-block t
)))
2122 (format "{%s}" (org-export-data main info
))
2124 (org-export-data secondary info
)
2125 (org-export-data main info
)))))))
2130 (org-latex--make-option-string
2132 org-latex-listings-options
2134 ((and (not float
) (plist-member attributes
:float
)) nil
)
2135 ((string= "multicolumn" float
) '(("float" "*")))
2136 ((and float
(not (assoc "float" org-latex-listings-options
)))
2137 `(("float" ,org-latex-default-figure-position
))))
2138 `(("language" ,lst-lang
))
2139 (when label
`(("label" ,label
)))
2140 (when caption-str
`(("caption" ,caption-str
)))
2141 (cond ((assoc "numbers" org-latex-listings-options
) nil
)
2142 ((not num-start
) '(("numbers" "none")))
2143 ((zerop num-start
) '(("numbers" "left")))
2144 (t `(("numbers" "left")
2146 ,(number-to-string (1+ num-start
)))))))))
2149 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2150 (let* ((code-info (org-export-unravel-code src-block
))
2154 (org-split-string (car code-info
) "\n")))))
2155 (org-export-format-code
2157 (lambda (loc num ref
)
2161 ;; Ensure references are flushed to the right,
2162 ;; separated with 6 spaces from the widest line of
2164 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2165 (format "(%s)" ref
)))))
2166 nil
(and retain-labels
(cdr code-info
))))))))))))
2169 ;;;; Statistics Cookie
2171 (defun org-latex-statistics-cookie (statistics-cookie contents info
)
2172 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2173 CONTENTS is nil. INFO is a plist holding contextual information."
2174 (replace-regexp-in-string
2175 "%" "\\%" (org-element-property :value statistics-cookie
) nil t
))
2180 (defun org-latex-strike-through (strike-through contents info
)
2181 "Transcode STRIKE-THROUGH from Org to LaTeX.
2182 CONTENTS is the text with strike-through markup. INFO is a plist
2183 holding contextual information."
2184 (org-latex--text-markup contents
'strike-through
))
2189 (defun org-latex--script-size (object info
)
2190 "Transcode a subscript or superscript object.
2191 OBJECT is an Org object. INFO is a plist used as a communication
2194 ;; Non-nil if object is already in a sub/superscript.
2195 (let ((parent object
))
2197 (while (setq parent
(org-export-get-parent parent
))
2198 (let ((type (org-element-type parent
)))
2199 (cond ((memq type
'(subscript superscript
))
2201 ((memq type org-element-all-elements
)
2202 (throw 'exit nil
))))))))
2203 (type (org-element-type object
))
2205 (org-element-map (org-element-contents object
)
2206 (cons 'plain-text org-element-all-objects
)
2208 (case (org-element-type obj
)
2209 ((entity latex-fragment
)
2210 (let ((data (org-trim (org-export-data obj info
))))
2212 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2216 (match-string 1 data
)
2217 (let ((blank (org-element-property :post-blank obj
)))
2218 (and blank
(> blank
0) "\\ "))))))
2221 (format "%s\\text{%s}" output
(org-export-data obj info
))))
2225 (org-export-data obj info
)
2226 (let ((blank (org-element-property :post-blank obj
)))
2227 (and blank
(> blank
0) "\\ ")))))))
2228 info nil org-element-recursive-objects
)
2229 ;; Result. Do not wrap into math mode if already in a subscript
2230 ;; or superscript. Do not wrap into curly brackets if OUTPUT is
2231 ;; a single character. Also merge consecutive subscript and
2232 ;; superscript into the same math snippet.
2233 (concat (and (not in-script-p
)
2234 (let ((prev (org-export-get-previous-element object info
)))
2236 (not (eq (org-element-type prev
)
2237 (if (eq type
'subscript
) 'superscript
2239 (let ((blank (org-element-property :post-blank prev
)))
2240 (and blank
(> blank
0)))))
2242 (if (eq (org-element-type object
) 'subscript
) "_" "^")
2243 (and (> (length output
) 1) "{")
2245 (and (> (length output
) 1) "}")
2246 (and (not in-script-p
)
2247 (or (let ((blank (org-element-property :post-blank object
)))
2248 (and blank
(> blank
0)))
2249 (not (eq (org-element-type
2250 (org-export-get-next-element object info
))
2251 (if (eq type
'subscript
) 'superscript
2255 (defun org-latex-subscript (subscript contents info
)
2256 "Transcode a SUBSCRIPT object from Org to LaTeX.
2257 CONTENTS is the contents of the object. INFO is a plist holding
2258 contextual information."
2259 (org-latex--script-size subscript info
))
2264 (defun org-latex-superscript (superscript contents info
)
2265 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2266 CONTENTS is the contents of the object. INFO is a plist holding
2267 contextual information."
2268 (org-latex--script-size superscript info
))
2273 ;; `org-latex-table' is the entry point for table transcoding. It
2274 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2275 ;; delegates the job to either `org-latex--table.el-table',
2276 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2277 ;; depending of the type of the table and the mode requested.
2279 ;; `org-latex--align-string' is a subroutine used to build alignment
2280 ;; string for Org tables.
2282 (defun org-latex-table (table contents info
)
2283 "Transcode a TABLE element from Org to LaTeX.
2284 CONTENTS is the contents of the table. INFO is a plist holding
2285 contextual information."
2286 (if (eq (org-element-property :type table
) 'table.el
)
2287 ;; "table.el" table. Convert it using appropriate tools.
2288 (org-latex--table.el-table table info
)
2289 (let ((type (or (org-export-read-attribute :attr_latex table
:mode
)
2290 org-latex-default-table-mode
)))
2292 ;; Case 1: Verbatim table.
2293 ((string= type
"verbatim")
2294 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2295 ;; Re-create table, without affiliated keywords.
2296 (org-trim (org-element-interpret-data
2297 `(table nil
,@(org-element-contents table
))))))
2299 ((or (string= type
"math") (string= type
"inline-math"))
2300 (org-latex--math-table table info
))
2301 ;; Case 3: Standard table.
2302 (t (concat (org-latex--org-table table contents info
)
2303 ;; When there are footnote references within the
2304 ;; table, insert their definition just after it.
2305 (org-latex--delayed-footnotes-definitions table info
)))))))
2307 (defun org-latex--align-string (table info
)
2308 "Return an appropriate LaTeX alignment string.
2309 TABLE is the considered table. INFO is a plist used as
2310 a communication channel."
2311 (or (org-export-read-attribute :attr_latex table
:align
)
2313 ;; Extract column groups and alignment from first (non-rule)
2316 (org-element-map table
'table-row
2318 (and (eq (org-element-property :type row
) 'standard
) row
))
2322 (let ((borders (org-export-table-cell-borders cell info
)))
2323 ;; Check left border for the first cell only.
2324 (when (and (memq 'left borders
) (not align
))
2326 (push (case (org-export-table-cell-alignment cell info
)
2331 (when (memq 'right borders
) (push "|" align
))))
2333 (apply 'concat
(nreverse align
)))))
2335 (defun org-latex--org-table (table contents info
)
2336 "Return appropriate LaTeX code for an Org table.
2338 TABLE is the table type element to transcode. CONTENTS is its
2339 contents, as a string. INFO is a plist used as a communication
2342 This function assumes TABLE has `org' as its `:type' property and
2343 `table' as its `:mode' attribute."
2344 (let* ((caption (org-latex--caption/label-string table info
))
2345 (attr (org-export-read-attribute :attr_latex table
))
2346 ;; Determine alignment string.
2347 (alignment (org-latex--align-string table info
))
2348 ;; Determine environment for the table: longtable, tabular...
2349 (table-env (or (plist-get attr
:environment
)
2350 org-latex-default-table-environment
))
2351 ;; If table is a float, determine environment: table, table*
2352 ;; or sidewaystable.
2353 (float-env (unless (member table-env
'("longtable" "longtabu"))
2354 (let ((float (plist-get attr
:float
)))
2356 ((and (not float
) (plist-member attr
:float
)) nil
)
2357 ((string= float
"sidewaystable") "sidewaystable")
2358 ((string= float
"multicolumn") "table*")
2360 (org-element-property :caption table
)
2361 (org-string-nw-p (plist-get attr
:caption
)))
2363 ;; Extract others display options.
2364 (fontsize (let ((font (plist-get attr
:font
)))
2365 (and font
(concat font
"\n"))))
2366 (width (plist-get attr
:width
))
2367 (spreadp (plist-get attr
:spread
))
2368 (placement (or (plist-get attr
:placement
)
2369 (format "[%s]" org-latex-default-figure-position
)))
2370 (centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2371 org-latex-tables-centered
)))
2372 ;; Prepare the final format string for the table.
2375 ((equal "longtable" table-env
)
2376 (concat (and fontsize
(concat "{" fontsize
))
2377 (format "\\begin{longtable}{%s}\n" alignment
)
2378 (and org-latex-table-caption-above
2379 (org-string-nw-p caption
)
2380 (concat caption
"\\\\\n"))
2382 (and (not org-latex-table-caption-above
)
2383 (org-string-nw-p caption
)
2384 (concat caption
"\\\\\n"))
2385 "\\end{longtable}\n"
2386 (and fontsize
"}")))
2388 ((equal "longtabu" table-env
)
2389 (concat (and fontsize
(concat "{" fontsize
))
2390 (format "\\begin{longtabu}%s{%s}\n"
2393 (if spreadp
"spread" "to") width
) "")
2395 (and org-latex-table-caption-above
2396 (org-string-nw-p caption
)
2397 (concat caption
"\\\\\n"))
2399 (and (not org-latex-table-caption-above
)
2400 (org-string-nw-p caption
)
2401 (concat caption
"\\\\\n"))
2403 (and fontsize
"}")))
2407 (concat (format "\\begin{%s}%s\n" float-env placement
)
2408 (if org-latex-table-caption-above caption
"")
2409 (when centerp
"\\centering\n")
2411 (centerp (concat "\\begin{center}\n" fontsize
))
2412 (fontsize (concat "{" fontsize
)))
2413 (cond ((equal "tabu" table-env
)
2414 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2416 (if spreadp
" spread %s " " to %s ")
2420 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2422 (if width
(format "{%s}" width
) "")
2428 (concat (if org-latex-table-caption-above
"" caption
)
2429 (format "\n\\end{%s}" float-env
)))
2430 (centerp "\n\\end{center}")
2431 (fontsize "}")))))))
2433 (defun org-latex--table.el-table
(table info
)
2434 "Return appropriate LaTeX code for a table.el table.
2436 TABLE is the table type element to transcode. INFO is a plist
2437 used as a communication channel.
2439 This function assumes TABLE has `table.el' as its `:type'
2442 ;; Ensure "*org-export-table*" buffer is empty.
2443 (with-current-buffer (get-buffer-create "*org-export-table*")
2445 (let ((output (with-temp-buffer
2446 (insert (org-element-property :value table
))
2448 (re-search-forward "^[ \t]*|[^|]" nil t
)
2449 (table-generate-source 'latex
"*org-export-table*")
2450 (with-current-buffer "*org-export-table*"
2451 (org-trim (buffer-string))))))
2452 (kill-buffer (get-buffer "*org-export-table*"))
2453 ;; Remove left out comments.
2454 (while (string-match "^%.*\n" output
)
2455 (setq output
(replace-match "" t t output
)))
2456 (let ((attr (org-export-read-attribute :attr_latex table
)))
2457 (when (plist-get attr
:rmlines
)
2458 ;; When the "rmlines" attribute is provided, remove all hlines
2459 ;; but the the one separating heading from the table body.
2460 (let ((n 0) (pos 0))
2461 (while (and (< (length output
) pos
)
2462 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
2464 (unless (= n
2) (setq output
(replace-match "" nil nil output
))))))
2465 (let ((centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2466 org-latex-tables-centered
)))
2467 (if (not centerp
) output
2468 (format "\\begin{center}\n%s\n\\end{center}" output
))))))
2470 (defun org-latex--math-table (table info
)
2471 "Return appropriate LaTeX code for a matrix.
2473 TABLE is the table type element to transcode. INFO is a plist
2474 used as a communication channel.
2476 This function assumes TABLE has `org' as its `:type' property and
2477 `inline-math' or `math' as its `:mode' attribute.."
2478 (let* ((caption (org-latex--caption/label-string table info
))
2479 (attr (org-export-read-attribute :attr_latex table
))
2480 (inlinep (equal (plist-get attr
:mode
) "inline-math"))
2481 (env (or (plist-get attr
:environment
)
2482 org-latex-default-table-environment
))
2486 ;; Ignore horizontal rules.
2487 (when (eq (org-element-property :type row
) 'standard
)
2488 ;; Return each cell unmodified.
2492 (substring (org-element-interpret-data cell
) 0 -
1))
2493 (org-element-map row
'table-cell
'identity info
) "&")
2494 (or (cdr (assoc env org-latex-table-matrix-macros
)) "\\\\")
2496 (org-element-map table
'table-row
'identity info
) ""))
2497 ;; Variables related to math clusters (contiguous math tables
2498 ;; of the same type).
2499 (mode (org-export-read-attribute :attr_latex table
:mode
))
2500 (prev (org-export-get-previous-element table info
))
2501 (next (org-export-get-next-element table info
))
2504 ;; Non-nil when TABLE has the same mode as current table.
2505 (string= (or (org-export-read-attribute :attr_latex table
:mode
)
2506 org-latex-default-table-mode
)
2509 ;; Opening string. If TABLE is in the middle of a table cluster,
2510 ;; do not insert any.
2512 (eq (org-element-type prev
) 'table
)
2513 (memq (org-element-property :post-blank prev
) '(0 nil
))
2514 (funcall same-mode-p prev
))
2517 ((org-string-nw-p caption
) (concat "\\begin{equation}\n" caption
))
2520 (or (plist-get attr
:math-prefix
) "")
2521 ;; Environment. Also treat special cases.
2522 (cond ((equal env
"array")
2523 (let ((align (org-latex--align-string table info
)))
2524 (format "\\begin{array}{%s}\n%s\\end{array}" align contents
)))
2525 ((assoc env org-latex-table-matrix-macros
)
2526 (format "\\%s%s{\n%s}"
2528 (or (plist-get attr
:math-arguments
) "")
2530 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env
)))
2532 (or (plist-get attr
:math-suffix
) "")
2533 ;; Closing string. If TABLE is in the middle of a table cluster,
2534 ;; do not insert any. If it closes such a cluster, be sure to
2535 ;; close the cluster with a string matching the opening string.
2537 (eq (org-element-type next
) 'table
)
2538 (memq (org-element-property :post-blank table
) '(0 nil
))
2539 (funcall same-mode-p next
))
2542 ;; Find cluster beginning to know which environment to use.
2543 ((let ((cluster-beg table
) prev
)
2544 (while (and (setq prev
(org-export-get-previous-element
2546 (memq (org-element-property :post-blank prev
)
2548 (funcall same-mode-p prev
))
2549 (setq cluster-beg prev
))
2550 (and (or (org-element-property :caption cluster-beg
)
2551 (org-element-property :name cluster-beg
))
2552 "\n\\end{equation}")))
2558 (defun org-latex-table-cell (table-cell contents info
)
2559 "Transcode a TABLE-CELL element from Org to LaTeX.
2560 CONTENTS is the cell contents. INFO is a plist used as
2561 a communication channel."
2562 (concat (if (and contents
2563 org-latex-table-scientific-notation
2564 (string-match orgtbl-exp-regexp contents
))
2565 ;; Use appropriate format string for scientific
2567 (format org-latex-table-scientific-notation
2568 (match-string 1 contents
)
2569 (match-string 2 contents
))
2571 (when (org-export-get-next-element table-cell info
) " & ")))
2576 (defun org-latex-table-row (table-row contents info
)
2577 "Transcode a TABLE-ROW element from Org to LaTeX.
2578 CONTENTS is the contents of the row. INFO is a plist used as
2579 a communication channel."
2580 ;; Rules are ignored since table separators are deduced from
2581 ;; borders of the current row.
2582 (when (eq (org-element-property :type table-row
) 'standard
)
2583 (let* ((attr (org-export-read-attribute :attr_latex
2584 (org-export-get-parent table-row
)))
2585 (longtablep (member (or (plist-get attr
:environment
)
2586 org-latex-default-table-environment
)
2587 '("longtable" "longtabu")))
2588 (booktabsp (if (plist-member attr
:booktabs
)
2589 (plist-get attr
:booktabs
)
2590 org-latex-tables-booktabs
))
2591 ;; TABLE-ROW's borders are extracted from its first cell.
2592 (borders (org-export-table-cell-borders
2593 (car (org-element-contents table-row
)) info
)))
2595 ;; When BOOKTABS are activated enforce top-rule even when no
2596 ;; hline was specifically marked.
2597 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2598 ((and (memq 'top borders
) (memq 'above borders
)) "\\hline\n"))
2601 ;; Special case for long tables. Define header and footers.
2602 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
2605 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2608 (if booktabsp
"\\midrule" "\\hline")
2609 (if booktabsp
"\\midrule" "\\hline")
2610 ;; Number of columns.
2611 (cdr (org-export-table-dimensions
2612 (org-export-get-parent-table table-row
) info
))))
2613 ;; When BOOKTABS are activated enforce bottom rule even when
2614 ;; no hline was specifically marked.
2615 ((and booktabsp
(memq 'bottom borders
)) "\\bottomrule")
2616 ((and (memq 'bottom borders
) (memq 'below borders
)) "\\hline")
2617 ((memq 'below borders
) (if booktabsp
"\\midrule" "\\hline")))))))
2622 (defun org-latex-target (target contents info
)
2623 "Transcode a TARGET object from Org to LaTeX.
2624 CONTENTS is nil. INFO is a plist holding contextual
2626 (format "\\label{%s}"
2627 (org-export-solidify-link-text (org-element-property :value target
))))
2632 (defun org-latex-timestamp (timestamp contents info
)
2633 "Transcode a TIMESTAMP object from Org to LaTeX.
2634 CONTENTS is nil. INFO is a plist holding contextual
2636 (let ((value (org-latex-plain-text
2637 (org-timestamp-translate timestamp
) info
)))
2638 (case (org-element-property :type timestamp
)
2639 ((active active-range
) (format org-latex-active-timestamp-format value
))
2640 ((inactive inactive-range
)
2641 (format org-latex-inactive-timestamp-format value
))
2642 (otherwise (format org-latex-diary-timestamp-format value
)))))
2647 (defun org-latex-underline (underline contents info
)
2648 "Transcode UNDERLINE from Org to LaTeX.
2649 CONTENTS is the text with underline markup. INFO is a plist
2650 holding contextual information."
2651 (org-latex--text-markup contents
'underline
))
2656 (defun org-latex-verbatim (verbatim contents info
)
2657 "Transcode a VERBATIM object from Org to LaTeX.
2658 CONTENTS is nil. INFO is a plist used as a communication
2660 (org-latex--text-markup (org-element-property :value verbatim
) 'verbatim
))
2665 (defun org-latex-verse-block (verse-block contents info
)
2666 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2667 CONTENTS is verse block contents. INFO is a plist holding
2668 contextual information."
2669 (org-latex--wrap-label
2671 ;; In a verse environment, add a line break to each newline
2672 ;; character and change each white space at beginning of a line
2673 ;; into a space of 1 em. Also change each blank line with
2674 ;; a vertical space of 1 em.
2676 (setq contents
(replace-regexp-in-string
2677 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2678 (replace-regexp-in-string
2679 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents
)))
2680 (while (string-match "^[ \t]+" contents
)
2681 (let ((new-str (format "\\hspace*{%dem}"
2682 (length (match-string 0 contents
)))))
2683 (setq contents
(replace-match new-str nil t contents
))))
2684 (format "\\begin{verse}\n%s\\end{verse}" contents
))))
2688 ;;; End-user functions
2691 (defun org-latex-export-as-latex
2692 (&optional async subtreep visible-only body-only ext-plist
)
2693 "Export current buffer as a LaTeX buffer.
2695 If narrowing is active in the current buffer, only export its
2698 If a region is active, export that region.
2700 A non-nil optional argument ASYNC means the process should happen
2701 asynchronously. The resulting buffer should be accessible
2702 through the `org-export-stack' interface.
2704 When optional argument SUBTREEP is non-nil, export the sub-tree
2705 at point, extracting information from the headline properties
2708 When optional argument VISIBLE-ONLY is non-nil, don't export
2709 contents of hidden elements.
2711 When optional argument BODY-ONLY is non-nil, only write code
2712 between \"\\begin{document}\" and \"\\end{document}\".
2714 EXT-PLIST, when provided, is a property list with external
2715 parameters overriding Org default settings, but still inferior to
2716 file-local settings.
2718 Export is done in a buffer named \"*Org LATEX Export*\", which
2719 will be displayed when `org-export-show-temporary-export-buffer'
2722 (org-export-to-buffer 'latex
"*Org LATEX Export*"
2723 async subtreep visible-only body-only ext-plist
(lambda () (LaTeX-mode))))
2726 (defun org-latex-convert-region-to-latex ()
2727 "Assume the current region has org-mode syntax, and convert it to LaTeX.
2728 This can be used in any buffer. For example, you can write an
2729 itemized list in org-mode syntax in an LaTeX buffer and use this
2730 command to convert it."
2732 (org-export-replace-region-by 'latex
))
2735 (defun org-latex-export-to-latex
2736 (&optional async subtreep visible-only body-only ext-plist
)
2737 "Export current buffer to a LaTeX file.
2739 If narrowing is active in the current buffer, only export its
2742 If a region is active, export that region.
2744 A non-nil optional argument ASYNC means the process should happen
2745 asynchronously. The resulting file should be accessible through
2746 the `org-export-stack' interface.
2748 When optional argument SUBTREEP is non-nil, export the sub-tree
2749 at point, extracting information from the headline properties
2752 When optional argument VISIBLE-ONLY is non-nil, don't export
2753 contents of hidden elements.
2755 When optional argument BODY-ONLY is non-nil, only write code
2756 between \"\\begin{document}\" and \"\\end{document}\".
2758 EXT-PLIST, when provided, is a property list with external
2759 parameters overriding Org default settings, but still inferior to
2760 file-local settings."
2762 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
2763 (org-export-to-file 'latex outfile
2764 async subtreep visible-only body-only ext-plist
)))
2767 (defun org-latex-export-to-pdf
2768 (&optional async subtreep visible-only body-only ext-plist
)
2769 "Export current buffer to LaTeX then process through to PDF.
2771 If narrowing is active in the current buffer, only export its
2774 If a region is active, export that region.
2776 A non-nil optional argument ASYNC means the process should happen
2777 asynchronously. The resulting file should be accessible through
2778 the `org-export-stack' interface.
2780 When optional argument SUBTREEP is non-nil, export the sub-tree
2781 at point, extracting information from the headline properties
2784 When optional argument VISIBLE-ONLY is non-nil, don't export
2785 contents of hidden elements.
2787 When optional argument BODY-ONLY is non-nil, only write code
2788 between \"\\begin{document}\" and \"\\end{document}\".
2790 EXT-PLIST, when provided, is a property list with external
2791 parameters overriding Org default settings, but still inferior to
2792 file-local settings.
2794 Return PDF file's name."
2796 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
2797 (org-export-to-file 'latex outfile
2798 async subtreep visible-only body-only ext-plist
2799 (lambda (file) (org-latex-compile file
)))))
2801 (defun org-latex-compile (texfile &optional snippet
)
2802 "Compile a TeX file.
2804 TEXFILE is the name of the file being compiled. Processing is
2805 done through the command specified in `org-latex-pdf-process'.
2807 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
2808 file used to preview a LaTeX snippet. In this case, do not
2809 create a log buffer and do not bother removing log files.
2811 Return PDF file name or an error if it couldn't be produced."
2812 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile
)))
2813 (full-name (file-truename texfile
))
2814 (out-dir (file-name-directory texfile
))
2815 ;; Properly set working directory for compilation.
2816 (default-directory (if (file-name-absolute-p texfile
)
2817 (file-name-directory full-name
)
2820 (unless snippet
(message (format "Processing LaTeX file %s..." texfile
)))
2821 (save-window-excursion
2823 ;; A function is provided: Apply it.
2824 ((functionp org-latex-pdf-process
)
2825 (funcall org-latex-pdf-process
(shell-quote-argument texfile
)))
2826 ;; A list is provided: Replace %b, %f and %o with appropriate
2827 ;; values in each command before applying it. Output is
2828 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2829 ((consp org-latex-pdf-process
)
2830 (let ((outbuf (and (not snippet
)
2831 (get-buffer-create "*Org PDF LaTeX Output*"))))
2835 (replace-regexp-in-string
2836 "%b" (shell-quote-argument base-name
)
2837 (replace-regexp-in-string
2838 "%f" (shell-quote-argument full-name
)
2839 (replace-regexp-in-string
2840 "%o" (shell-quote-argument out-dir
) command t t
) t t
) t t
)
2842 org-latex-pdf-process
)
2843 ;; Collect standard errors from output buffer.
2844 (setq errors
(and (not snippet
) (org-latex--collect-errors outbuf
)))))
2845 (t (error "No valid command to process to PDF")))
2846 (let ((pdffile (concat out-dir base-name
".pdf")))
2847 ;; Check for process failure. Provide collected errors if
2849 (if (not (file-exists-p pdffile
))
2850 (error (concat (format "PDF file %s wasn't produced" pdffile
)
2851 (when errors
(concat ": " errors
))))
2852 ;; Else remove log files, when specified, and signal end of
2853 ;; process to user, along with any error encountered.
2854 (when (and (not snippet
) org-latex-remove-logfiles
)
2855 (dolist (ext org-latex-logfiles-extensions
)
2856 (let ((file (concat out-dir base-name
"." ext
)))
2857 (when (file-exists-p file
) (delete-file file
)))))
2858 (message (concat "Process completed"
2859 (if (not errors
) "."
2860 (concat " with errors: " errors
)))))
2861 ;; Return output file name.
2864 (defun org-latex--collect-errors (buffer)
2865 "Collect some kind of errors from \"pdflatex\" command output.
2867 BUFFER is the buffer containing output.
2869 Return collected error types as a string, or nil if there was
2871 (with-current-buffer buffer
2873 (goto-char (point-max))
2874 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t
)
2875 (let ((case-fold-search t
)
2877 (dolist (latex-error org-latex-known-errors
)
2878 (when (save-excursion (re-search-forward (car latex-error
) nil t
))
2879 (setq errors
(concat errors
" " (cdr latex-error
)))))
2880 (and (org-string-nw-p errors
) (org-trim errors
)))))))
2883 (defun org-latex-publish-to-latex (plist filename pub-dir
)
2884 "Publish an Org file to LaTeX.
2886 FILENAME is the filename of the Org file to be published. PLIST
2887 is the property list for the given project. PUB-DIR is the
2888 publishing directory.
2890 Return output file name."
2891 (org-publish-org-to 'latex filename
".tex" plist pub-dir
))
2894 (defun org-latex-publish-to-pdf (plist filename pub-dir
)
2895 "Publish an Org file to PDF (via LaTeX).
2897 FILENAME is the filename of the Org file to be published. PLIST
2898 is the property list for the given project. PUB-DIR is the
2899 publishing directory.
2901 Return output file name."
2902 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
2903 ;; in working directory and then moved to publishing directory.
2904 (org-publish-attachment
2906 (org-latex-compile (org-publish-org-to 'latex filename
".tex" plist
))
2913 ;; generated-autoload-file: "org-loaddefs.el"
2916 ;;; ox-latex.el ends here