1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2016 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; See Org manual for details.
29 (eval-when-compile (require 'cl
))
33 (defvar org-latex-default-packages-alist
)
34 (defvar org-latex-packages-alist
)
35 (defvar orgtbl-exp-regexp
)
41 (org-export-define-backend 'latex
42 '((bold . org-latex-bold
)
43 (center-block . org-latex-center-block
)
44 (clock . org-latex-clock
)
45 (code . org-latex-code
)
46 (comment .
(lambda (&rest args
) ""))
47 (comment-block .
(lambda (&rest args
) ""))
48 (drawer . org-latex-drawer
)
49 (dynamic-block . org-latex-dynamic-block
)
50 (entity . org-latex-entity
)
51 (example-block . org-latex-example-block
)
52 (export-block . org-latex-export-block
)
53 (export-snippet . org-latex-export-snippet
)
54 (fixed-width . org-latex-fixed-width
)
55 (footnote-definition . org-latex-footnote-definition
)
56 (footnote-reference . org-latex-footnote-reference
)
57 (headline . org-latex-headline
)
58 (horizontal-rule . org-latex-horizontal-rule
)
59 (inline-src-block . org-latex-inline-src-block
)
60 (inlinetask . org-latex-inlinetask
)
61 (italic . org-latex-italic
)
62 (item . org-latex-item
)
63 (keyword . org-latex-keyword
)
64 (latex-environment . org-latex-latex-environment
)
65 (latex-fragment . org-latex-latex-fragment
)
66 (line-break . org-latex-line-break
)
67 (link . org-latex-link
)
68 (paragraph . org-latex-paragraph
)
69 (plain-list . org-latex-plain-list
)
70 (plain-text . org-latex-plain-text
)
71 (planning . org-latex-planning
)
72 (property-drawer .
(lambda (&rest args
) ""))
73 (quote-block . org-latex-quote-block
)
74 (quote-section . org-latex-quote-section
)
75 (radio-target . org-latex-radio-target
)
76 (section . org-latex-section
)
77 (special-block . org-latex-special-block
)
78 (src-block . org-latex-src-block
)
79 (statistics-cookie . org-latex-statistics-cookie
)
80 (strike-through . org-latex-strike-through
)
81 (subscript . org-latex-subscript
)
82 (superscript . org-latex-superscript
)
83 (table . org-latex-table
)
84 (table-cell . org-latex-table-cell
)
85 (table-row . org-latex-table-row
)
86 (target . org-latex-target
)
87 (template . org-latex-template
)
88 (timestamp . org-latex-timestamp
)
89 (underline . org-latex-underline
)
90 (verbatim . org-latex-verbatim
)
91 (verse-block . org-latex-verse-block
))
92 :export-block
'("LATEX" "TEX")
94 '(?l
"Export to LaTeX"
95 ((?L
"As LaTeX buffer" org-latex-export-as-latex
)
96 (?l
"As LaTeX file" org-latex-export-to-latex
)
97 (?p
"As PDF file" org-latex-export-to-pdf
)
98 (?o
"As PDF file and open"
100 (if a
(org-latex-export-to-pdf t s v b
)
101 (org-open-file (org-latex-export-to-pdf nil s v b
)))))))
102 :options-alist
'((:latex-class
"LATEX_CLASS" nil org-latex-default-class t
)
103 (:latex-class-options
"LATEX_CLASS_OPTIONS" nil nil t
)
104 (:latex-header
"LATEX_HEADER" nil nil newline
)
105 (:latex-header-extra
"LATEX_HEADER_EXTRA" nil nil newline
)
106 (:latex-hyperref-p nil
"texht" org-latex-with-hyperref t
)
107 ;; Redefine regular options.
108 (:date
"DATE" nil
"\\today" t
)))
112 ;;; Internal Variables
114 (defconst org-latex-babel-language-alist
115 '(("af" .
"afrikaans")
117 ("bt-br" .
"brazilian")
123 ("de-at" .
"naustrian")
124 ("de-de" .
"ngerman")
127 ("en-au" .
"australian")
128 ("en-ca" .
"canadian")
129 ("en-gb" .
"british")
131 ("en-nz" .
"newzealand")
132 ("en-us" .
"american")
138 ("fr-ca" .
"canadien")
142 ("id" .
"indonesian")
152 ("pt" .
"portuguese")
156 ("sb" .
"uppersorbian")
164 ("uk" .
"ukrainian"))
165 "Alist between language code and corresponding Babel option.")
167 (defconst org-latex-table-matrix-macros
'(("bordermatrix" .
"\\cr")
168 ("qbordermatrix" .
"\\cr")
169 ("kbordermatrix" .
"\\\\"))
170 "Alist between matrix macros and their row ending.")
174 ;;; User Configurable Variables
176 (defgroup org-export-latex nil
177 "Options for exporting Org mode files to LaTeX."
178 :tag
"Org Export LaTeX"
184 (defcustom org-latex-default-class
"article"
185 "The default LaTeX class."
186 :group
'org-export-latex
187 :type
'(string :tag
"LaTeX class"))
189 (defcustom org-latex-classes
191 "\\documentclass[11pt]{article}"
192 ("\\section{%s}" .
"\\section*{%s}")
193 ("\\subsection{%s}" .
"\\subsection*{%s}")
194 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
195 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
196 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
198 "\\documentclass[11pt]{report}"
199 ("\\part{%s}" .
"\\part*{%s}")
200 ("\\chapter{%s}" .
"\\chapter*{%s}")
201 ("\\section{%s}" .
"\\section*{%s}")
202 ("\\subsection{%s}" .
"\\subsection*{%s}")
203 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
205 "\\documentclass[11pt]{book}"
206 ("\\part{%s}" .
"\\part*{%s}")
207 ("\\chapter{%s}" .
"\\chapter*{%s}")
208 ("\\section{%s}" .
"\\section*{%s}")
209 ("\\subsection{%s}" .
"\\subsection*{%s}")
210 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
211 "Alist of LaTeX classes and associated header and structure.
212 If #+LATEX_CLASS is set in the buffer, use its value and the
213 associated information. Here is the structure of each cell:
217 (numbered-section . unnumbered-section)
223 The HEADER-STRING is the header that will be inserted into the
224 LaTeX file. It should contain the \\documentclass macro, and
225 anything else that is needed for this setup. To this header, the
226 following commands will be added:
228 - Calls to \\usepackage for all packages mentioned in the
229 variables `org-latex-default-packages-alist' and
230 `org-latex-packages-alist'. Thus, your header definitions
231 should avoid to also request these packages.
233 - Lines specified via \"#+LATEX_HEADER:\" and
234 \"#+LATEX_HEADER_EXTRA:\" keywords.
236 If you need more control about the sequence in which the header
237 is built up, or if you want to exclude one of these building
238 blocks for a particular class, you can use the following
239 macro-like placeholders.
241 [DEFAULT-PACKAGES] \\usepackage statements for default packages
242 [NO-DEFAULT-PACKAGES] do not include any of the default packages
243 [PACKAGES] \\usepackage statements for packages
244 [NO-PACKAGES] do not include the packages
245 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
246 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
250 \\documentclass{article}
251 [NO-DEFAULT-PACKAGES]
253 \\providecommand{\\alert}[1]{\\textbf{#1}}
256 will omit the default packages, and will include the
257 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
258 to \\providecommand, and then place \\usepackage commands based
259 on the content of `org-latex-packages-alist'.
261 If your header, `org-latex-default-packages-alist' or
262 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
263 AUTO will automatically be replaced with a coding system derived
264 from `buffer-file-coding-system'. See also the variable
265 `org-latex-inputenc-alist' for a way to influence this mechanism.
267 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\",
268 AUTO will be replaced with the language related to the language
269 code specified by `org-export-default-language', which see. Note
270 that constructions such as \"\\usepackage[french,AUTO,english]{babel}\"
273 The sectioning structure
274 ------------------------
276 The sectioning structure of the class is given by the elements
277 following the header string. For each sectioning level, a number
278 of strings is specified. A %s formatter is mandatory in each
279 section string and will be replaced by the title of the section.
281 Instead of a cons cell (numbered . unnumbered), you can also
282 provide a list of 2 or 4 elements,
284 (numbered-open numbered-close)
288 (numbered-open numbered-close unnumbered-open unnumbered-close)
290 providing opening and closing strings for a LaTeX environment
291 that should represent the document section. The opening clause
292 should have a %s to represent the section title.
294 Instead of a list of sectioning commands, you can also specify
295 a function name. That function will be called with two
296 parameters, the (reduced) level of the headline, and a predicate
297 non-nil when the headline should be numbered. It must return
298 a format string in which the section title will be added."
299 :group
'org-export-latex
301 (list (string :tag
"LaTeX class")
302 (string :tag
"LaTeX header")
303 (repeat :tag
"Levels" :inline t
306 (string :tag
" numbered")
307 (string :tag
"unnumbered"))
308 (list :tag
"Environment"
309 (string :tag
"Opening (numbered)")
310 (string :tag
"Closing (numbered)")
311 (string :tag
"Opening (unnumbered)")
312 (string :tag
"Closing (unnumbered)"))
313 (function :tag
"Hook computing sectioning"))))))
315 (defcustom org-latex-inputenc-alist nil
316 "Alist of inputenc coding system names, and what should really be used.
317 For example, adding an entry
319 (\"utf8\" . \"utf8x\")
321 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
322 are written as utf8 files."
323 :group
'org-export-latex
326 (string :tag
"Derived from buffer")
327 (string :tag
"Use this instead"))))
329 (defcustom org-latex-title-command
"\\maketitle"
330 "The command used to insert the title just after \\begin{document}.
331 If this string contains the formatting specification \"%s\" then
332 it will be used as a formatting string, passing the title as an
334 :group
'org-export-latex
337 (defcustom org-latex-toc-command
"\\tableofcontents\n\n"
338 "LaTeX command to set the table of contents, list of figures, etc.
339 This command only applies to the table of contents generated with
340 the toc:nil option, not to those generated with #+TOC keyword."
341 :group
'org-export-latex
344 (defcustom org-latex-with-hyperref t
345 "Toggle insertion of \\hypersetup{...} in the preamble."
346 :group
'org-export-latex
351 (defcustom org-latex-format-headline-function
352 'org-latex-format-headline-default-function
353 "Function for formatting the headline's text.
355 This function will be called with 5 arguments:
356 TODO the todo keyword (string or nil).
357 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
358 PRIORITY the priority of the headline (integer or nil)
359 TEXT the main headline text (string).
360 TAGS the tags as a list of strings (list of strings or nil).
362 The function result will be used in the section format string.
364 Use `org-latex-format-headline-default-function' by default,
365 which format headlines like for Org version prior to 8.0."
366 :group
'org-export-latex
368 :package-version
'(Org .
"8.0")
374 (defcustom org-latex-footnote-separator
"\\textsuperscript{,}\\,"
375 "Text used to separate footnotes."
376 :group
'org-export-latex
382 (defcustom org-latex-active-timestamp-format
"\\textit{%s}"
383 "A printf format string to be applied to active timestamps."
384 :group
'org-export-latex
387 (defcustom org-latex-inactive-timestamp-format
"\\textit{%s}"
388 "A printf format string to be applied to inactive timestamps."
389 :group
'org-export-latex
392 (defcustom org-latex-diary-timestamp-format
"\\textit{%s}"
393 "A printf format string to be applied to diary timestamps."
394 :group
'org-export-latex
400 (defcustom org-latex-image-default-option
""
401 "Default option for images."
402 :group
'org-export-latex
404 :package-version
'(Org .
"8.0")
407 (defcustom org-latex-image-default-width
".9\\linewidth"
408 "Default width for images.
409 This value will not be used if a height is provided."
410 :group
'org-export-latex
412 :package-version
'(Org .
"8.0")
415 (defcustom org-latex-image-default-height
""
416 "Default height for images.
417 This value will not be used if a width is provided, or if the
418 image is wrapped within a \"figure\" or \"wrapfigure\"
420 :group
'org-export-latex
422 :package-version
'(Org .
"8.0")
425 (defcustom org-latex-default-figure-position
"htb"
426 "Default position for latex figures."
427 :group
'org-export-latex
430 (defcustom org-latex-inline-image-rules
431 '(("file" .
"\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
432 "Rules characterizing image files that can be inlined into LaTeX.
434 A rule consists in an association whose key is the type of link
435 to consider, and value is a regexp that will be matched against
438 Note that, by default, the image extension *actually* allowed
439 depend on the way the LaTeX file is processed. When used with
440 pdflatex, pdf, jpg and png images are OK. When processing
441 through dvi to Postscript, only ps and eps are allowed. The
442 default we use here encompasses both."
443 :group
'org-export-latex
445 :package-version
'(Org .
"8.0")
446 :type
'(alist :key-type
(string :tag
"Type")
447 :value-type
(regexp :tag
"Path")))
449 (defcustom org-latex-link-with-unknown-path-format
"\\texttt{%s}"
450 "Format string for links with unknown path type."
451 :group
'org-export-latex
457 (defcustom org-latex-default-table-environment
"tabular"
458 "Default environment used to build tables."
459 :group
'org-export-latex
461 :package-version
'(Org .
"8.0")
464 (defcustom org-latex-default-table-mode
'table
465 "Default mode for tables.
467 Value can be a symbol among:
469 `table' Regular LaTeX table.
471 `math' In this mode, every cell is considered as being in math
472 mode and the complete table will be wrapped within a math
473 environment. It is particularly useful to write matrices.
475 `inline-math' This mode is almost the same as `math', but the
476 math environment will be inlined.
478 `verbatim' The table is exported as it appears in the Org
479 buffer, within a verbatim environment.
481 This value can be overridden locally with, i.e. \":mode math\" in
484 When modifying this variable, it may be useful to change
485 `org-latex-default-table-environment' accordingly."
486 :group
'org-export-latex
488 :package-version
'(Org .
"8.0")
489 :type
'(choice (const :tag
"Table" table
)
490 (const :tag
"Matrix" math
)
491 (const :tag
"Inline matrix" inline-math
)
492 (const :tag
"Verbatim" verbatim
)))
494 (defcustom org-latex-tables-centered t
495 "When non-nil, tables are exported in a center environment."
496 :group
'org-export-latex
499 (defcustom org-latex-tables-booktabs nil
500 "When non-nil, display tables in a formal \"booktabs\" style.
501 This option assumes that the \"booktabs\" package is properly
502 loaded in the header of the document. This value can be ignored
503 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
505 :group
'org-export-latex
507 :package-version
'(Org .
"8.0")
510 (defcustom org-latex-table-caption-above t
511 "When non-nil, place caption string at the beginning of the table.
512 Otherwise, place it near the end."
513 :group
'org-export-latex
516 (defcustom org-latex-table-scientific-notation
"%s\\,(%s)"
517 "Format string to display numbers in scientific notation.
518 The format should have \"%s\" twice, for mantissa and exponent
519 \(i.e., \"%s\\\\times10^{%s}\").
521 When nil, no transformation is made."
522 :group
'org-export-latex
524 :package-version
'(Org .
"8.0")
526 (string :tag
"Format string")
527 (const :tag
"No formatting" nil
)))
532 (defcustom org-latex-text-markup-alist
'((bold .
"\\textbf{%s}")
534 (italic .
"\\emph{%s}")
535 (strike-through .
"\\sout{%s}")
536 (underline .
"\\uline{%s}")
537 (verbatim . protectedtexttt
))
538 "Alist of LaTeX expressions to convert text markup.
540 The key must be a symbol among `bold', `code', `italic',
541 `strike-through', `underline' and `verbatim'. The value is
542 a formatting string to wrap fontified text with.
544 Value can also be set to the following symbols: `verb' and
545 `protectedtexttt'. For the former, Org will use \"\\verb\" to
546 create a format string and select a delimiter character that
547 isn't in the string. For the latter, Org will use \"\\texttt\"
548 to typeset and try to protect special characters.
550 If no association can be found for a given markup, text will be
552 :group
'org-export-latex
554 :options
'(bold code italic strike-through underline verbatim
))
559 (defcustom org-latex-format-drawer-function
560 (lambda (name contents
) contents
)
561 "Function called to format a drawer in LaTeX code.
563 The function must accept two parameters:
564 NAME the drawer name, like \"LOGBOOK\"
565 CONTENTS the contents of the drawer.
567 The function should return the string to be exported.
569 The default function simply returns the value of CONTENTS."
570 :group
'org-export-latex
572 :package-version
'(Org .
"8.3")
578 (defcustom org-latex-format-inlinetask-function
'ignore
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 behavior:
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:
629 (require \\='ox-latex)
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
642 (require \\='ox-latex)
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
1002 (replace-regexp-in-string "\n" " " text
)
1004 ;; Handle the `protectedtexttt' special case: Protect some
1005 ;; special chars and use "\texttt{%s}" format string.
1006 ((eq 'protectedtexttt fmt
)
1008 (trans '(("\\" .
"\\textbackslash{}")
1009 ("~" .
"\\textasciitilde{}")
1010 ("^" .
"\\textasciicircum{}")))
1013 (while (string-match "[\\{}$%&_#~^]" text
)
1014 (setq char
(match-string 0 text
))
1015 (if (> (match-beginning 0) 0)
1016 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
1017 (setq text
(substring text
(1+ (match-beginning 0))))
1018 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
1019 rtn
(concat rtn char
)))
1020 (setq text
(concat rtn text
)
1022 (while (string-match "--" text
)
1023 (setq text
(replace-match "-{}-" t t text
)))
1025 ;; Else use format string.
1026 (t (format fmt text
)))))
1028 (defun org-latex--delayed-footnotes-definitions (element info
)
1029 "Return footnotes definitions in ELEMENT as a string.
1031 INFO is a plist used as a communication channel.
1033 Footnotes definitions are returned within \"\\footnotetxt{}\"
1036 This function is used within constructs that don't support
1037 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1038 \"\\footnotemark\" is used within the construct and the function
1039 just outside of it."
1043 "\\footnotetext[%s]{%s}"
1044 (org-export-get-footnote-number ref info
)
1047 (org-export-get-footnote-definition ref info
) info
))))
1048 ;; Find every footnote reference in ELEMENT.
1050 search-refs
; For byte-compiler.
1054 ;; Return a list of all footnote references never seen
1056 (org-element-map data
'footnote-reference
1058 (when (org-export-footnote-first-reference-p ref info
)
1060 (when (eq (org-element-property :type ref
) 'standard
)
1061 (funcall search-refs
1062 (org-export-get-footnote-definition ref info
)))))
1064 (reverse all-refs
)))))
1065 (funcall search-refs element
))
1072 (defun org-latex-template (contents info
)
1073 "Return complete document string after LaTeX conversion.
1074 CONTENTS is the transcoded contents string. INFO is a plist
1075 holding export options."
1076 (let ((title (org-export-data (plist-get info
:title
) info
)))
1079 (and (plist-get info
:time-stamp-file
)
1080 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1081 ;; Document class and packages.
1082 (let* ((class (plist-get info
:latex-class
))
1083 (class-options (plist-get info
:latex-class-options
))
1084 (header (nth 1 (assoc class org-latex-classes
)))
1085 (document-class-string
1086 (and (stringp header
)
1087 (if (not class-options
) header
1088 (replace-regexp-in-string
1089 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1090 class-options header t nil
1)))))
1091 (if (not document-class-string
)
1092 (user-error "Unknown LaTeX class `%s'" class
)
1093 (org-latex-guess-babel-language
1094 (org-latex-guess-inputenc
1095 (org-element-normalize-string
1096 (org-splice-latex-header
1097 document-class-string
1098 org-latex-default-packages-alist
1099 org-latex-packages-alist nil
1100 (concat (org-element-normalize-string
1101 (plist-get info
:latex-header
))
1102 (plist-get info
:latex-header-extra
)))))
1104 ;; Possibly limit depth for headline numbering.
1105 (let ((sec-num (plist-get info
:section-numbers
)))
1106 (when (integerp sec-num
)
1107 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
1109 (let ((author (and (plist-get info
:with-author
)
1110 (let ((auth (plist-get info
:author
)))
1111 (and auth
(org-export-data auth info
)))))
1112 (email (and (plist-get info
:with-email
)
1113 (org-export-data (plist-get info
:email
) info
))))
1114 (cond ((and author email
(not (string= "" email
)))
1115 (format "\\author{%s\\thanks{%s}}\n" author email
))
1116 ((or author email
) (format "\\author{%s}\n" (or author email
)))))
1118 (let ((date (and (plist-get info
:with-date
) (org-export-get-date info
))))
1119 (format "\\date{%s}\n" (org-export-data date info
)))
1121 (format "\\title{%s}\n" title
)
1122 ;; Hyperref options.
1123 (when (plist-get info
:latex-hyperref-p
)
1124 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1125 (or (plist-get info
:keywords
) "")
1126 (or (plist-get info
:description
) "")
1127 (if (not (plist-get info
:with-creator
)) ""
1128 (plist-get info
:creator
))))
1130 "\\begin{document}\n\n"
1132 (org-element-normalize-string
1133 (cond ((string= "" title
) nil
)
1134 ((not (stringp org-latex-title-command
)) nil
)
1135 ((string-match "\\(?:[^%]\\|^\\)%s"
1136 org-latex-title-command
)
1137 (format org-latex-title-command title
))
1138 (t org-latex-title-command
)))
1139 ;; Table of contents.
1140 (let ((depth (plist-get info
:with-toc
)))
1142 (concat (when (wholenump depth
)
1143 (format "\\setcounter{tocdepth}{%d}\n" depth
))
1144 org-latex-toc-command
)))
1148 (let ((creator-info (plist-get info
:with-creator
)))
1150 ((not creator-info
) "")
1151 ((eq creator-info
'comment
)
1152 (format "%% %s\n" (plist-get info
:creator
)))
1153 (t (concat (plist-get info
:creator
) "\n"))))
1155 "\\end{document}")))
1159 ;;; Transcode Functions
1163 (defun org-latex-bold (bold contents info
)
1164 "Transcode BOLD from Org to LaTeX.
1165 CONTENTS is the text with bold markup. INFO is a plist holding
1166 contextual information."
1167 (org-latex--text-markup contents
'bold
))
1172 (defun org-latex-center-block (center-block contents info
)
1173 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1174 CONTENTS holds the contents of the center block. INFO is a plist
1175 holding contextual information."
1176 (org-latex--wrap-label
1178 (format "\\begin{center}\n%s\\end{center}" contents
)))
1183 (defun org-latex-clock (clock contents info
)
1184 "Transcode a CLOCK element from Org to LaTeX.
1185 CONTENTS is nil. INFO is a plist holding contextual
1189 (format "\\textbf{%s} " org-clock-string
)
1190 (format org-latex-inactive-timestamp-format
1191 (concat (org-translate-time
1192 (org-element-property :raw-value
1193 (org-element-property :value clock
)))
1194 (let ((time (org-element-property :duration clock
)))
1195 (and time
(format " (%s)" time
)))))
1201 (defun org-latex-code (code contents info
)
1202 "Transcode a CODE object from Org to LaTeX.
1203 CONTENTS is nil. INFO is a plist used as a communication
1205 (org-latex--text-markup (org-element-property :value code
) 'code
))
1210 (defun org-latex-drawer (drawer contents info
)
1211 "Transcode a DRAWER element from Org to LaTeX.
1212 CONTENTS holds the contents of the block. INFO is a plist
1213 holding contextual information."
1214 (let* ((name (org-element-property :drawer-name drawer
))
1215 (output (funcall org-latex-format-drawer-function
1217 (org-latex--wrap-label drawer output
)))
1222 (defun org-latex-dynamic-block (dynamic-block contents info
)
1223 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1224 CONTENTS holds the contents of the block. INFO is a plist
1225 holding contextual information. See `org-export-data'."
1226 (org-latex--wrap-label dynamic-block contents
))
1231 (defun org-latex-entity (entity contents info
)
1232 "Transcode an ENTITY object from Org to LaTeX.
1233 CONTENTS are the definition itself. INFO is a plist holding
1234 contextual information."
1235 (let ((ent (org-element-property :latex entity
)))
1236 (if (org-element-property :latex-math-p entity
) (format "$%s$" ent
) ent
)))
1241 (defun org-latex-example-block (example-block contents info
)
1242 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1243 CONTENTS is nil. INFO is a plist holding contextual
1245 (when (org-string-nw-p (org-element-property :value example-block
))
1246 (org-latex--wrap-label
1248 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1249 (org-export-format-code-default example-block info
)))))
1254 (defun org-latex-export-block (export-block contents info
)
1255 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1256 CONTENTS is nil. INFO is a plist holding contextual information."
1257 (when (member (org-element-property :type export-block
) '("LATEX" "TEX"))
1258 (org-remove-indentation (org-element-property :value export-block
))))
1263 (defun org-latex-export-snippet (export-snippet contents info
)
1264 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1265 CONTENTS is nil. INFO is a plist holding contextual information."
1266 (when (eq (org-export-snippet-backend export-snippet
) 'latex
)
1267 (org-element-property :value export-snippet
)))
1272 (defun org-latex-fixed-width (fixed-width contents info
)
1273 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1274 CONTENTS is nil. INFO is a plist holding contextual information."
1275 (org-latex--wrap-label
1277 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1278 (org-remove-indentation
1279 (org-element-property :value fixed-width
)))))
1282 ;;;; Footnote Reference
1284 (defun org-latex-footnote-reference (footnote-reference contents info
)
1285 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1286 CONTENTS is nil. INFO is a plist holding contextual information."
1288 ;; Insert separator between two footnotes in a row.
1289 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1290 (when (eq (org-element-type prev
) 'footnote-reference
)
1291 org-latex-footnote-separator
))
1293 ;; Use \footnotemark if the footnote has already been defined.
1294 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1295 (format "\\footnotemark[%s]{}"
1296 (org-export-get-footnote-number footnote-reference info
)))
1297 ;; Use \footnotemark if reference is within another footnote
1298 ;; reference, footnote definition or table cell.
1299 ((loop for parent in
(org-export-get-genealogy footnote-reference
)
1300 thereis
(memq (org-element-type parent
)
1301 '(footnote-reference footnote-definition table-cell
)))
1303 ;; Otherwise, define it with \footnote command.
1305 (let ((def (org-export-get-footnote-definition footnote-reference info
)))
1307 (format "\\footnote{%s}" (org-trim (org-export-data def info
)))
1308 ;; Retrieve all footnote references within the footnote and
1309 ;; add their definition after it, since LaTeX doesn't support
1311 (org-latex--delayed-footnotes-definitions def info
)))))))
1316 (defun org-latex-headline (headline contents info
)
1317 "Transcode a HEADLINE element from Org to LaTeX.
1318 CONTENTS holds the contents of the headline. INFO is a plist
1319 holding contextual information."
1320 (unless (org-element-property :footnote-section-p headline
)
1321 (let* ((class (plist-get info
:latex-class
))
1322 (level (org-export-get-relative-level headline info
))
1323 (numberedp (org-export-numbered-headline-p headline info
))
1324 (class-sectioning (assoc class org-latex-classes
))
1325 ;; Section formatting will set two placeholders: one for
1326 ;; the title and the other for the contents.
1328 (let ((sec (if (functionp (nth 2 class-sectioning
))
1329 (funcall (nth 2 class-sectioning
) level numberedp
)
1330 (nth (1+ level
) class-sectioning
))))
1332 ;; No section available for that LEVEL.
1334 ;; Section format directly returned by a function. Add
1335 ;; placeholder for contents.
1336 ((stringp sec
) (concat sec
"\n%s"))
1337 ;; (numbered-section . unnumbered-section)
1338 ((not (consp (cdr sec
)))
1339 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
1340 ;; (numbered-open numbered-close)
1342 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
1343 ;; (num-in num-out no-num-in no-num-out)
1345 (if numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))
1346 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
1347 ;; Create a temporary export back-end that hard-codes
1348 ;; "\underline" within "\section" and alike.
1350 (org-export-create-backend
1353 '((underline .
(lambda (o c i
) (format "\\underline{%s}" c
))))))
1355 (org-export-data-with-backend
1356 (org-element-property :title headline
) section-back-end info
))
1358 (and (plist-get info
:with-todo-keywords
)
1359 (let ((todo (org-element-property :todo-keyword headline
)))
1360 (and todo
(org-export-data todo info
)))))
1361 (todo-type (and todo
(org-element-property :todo-type headline
)))
1362 (tags (and (plist-get info
:with-tags
)
1363 (org-export-get-tags headline info
)))
1364 (priority (and (plist-get info
:with-priority
)
1365 (org-element-property :priority headline
)))
1366 ;; Create the headline text along with a no-tag version.
1367 ;; The latter is required to remove tags from toc.
1368 (full-text (funcall org-latex-format-headline-function
1369 todo todo-type priority text tags
))
1370 ;; Associate \label to the headline for internal links.
1372 (format "\\label{sec-%s}\n"
1373 (mapconcat 'number-to-string
1374 (org-export-get-headline-number headline info
)
1377 (make-string (org-element-property :pre-blank headline
) 10)))
1378 (if (or (not section-fmt
) (org-export-low-level-p headline info
))
1379 ;; This is a deep sub-tree: export it as a list item. Also
1380 ;; export as items headlines for which no section format has
1382 (let ((low-level-body
1384 ;; If headline is the first sibling, start a list.
1385 (when (org-export-first-sibling-p headline info
)
1386 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
1389 (and full-text
(org-string-match-p "\\`[ \t]*\\[" full-text
)
1395 ;; If headline is not the last sibling simply return
1396 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1398 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1399 (replace-regexp-in-string
1401 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
1403 ;; This is a standard headline. Export it as a section. Add
1404 ;; an alternative heading when possible, and when this is not
1405 ;; identical to the usual heading.
1407 (funcall org-latex-format-headline-function
1408 todo todo-type priority
1409 (org-export-data-with-backend
1410 (org-export-get-alt-title headline info
)
1411 section-back-end info
)
1412 (and (eq (plist-get info
:with-tags
) t
) tags
))))
1413 (if (and numberedp opt-title
1414 (not (equal opt-title full-text
))
1415 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt
))
1416 (format (replace-match "\\1[%s]" nil nil section-fmt
1)
1417 ;; Replace square brackets with parenthesis
1418 ;; since square brackets are not supported in
1419 ;; optional arguments.
1420 (replace-regexp-in-string
1421 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title
))
1423 (concat headline-label pre-blanks contents
))
1424 ;; Impossible to add an alternative heading. Fallback to
1425 ;; regular sectioning format string.
1426 (format section-fmt full-text
1427 (concat headline-label pre-blanks contents
))))))))
1429 (defun org-latex-format-headline-default-function
1430 (todo todo-type priority text tags
)
1431 "Default format function for a headline.
1432 See `org-latex-format-headline-function' for details."
1434 (and todo
(format "{\\bfseries\\sffamily %s} " todo
))
1435 (and priority
(format "\\framebox{\\#%c} " priority
))
1438 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags
":")))))
1441 ;;;; Horizontal Rule
1443 (defun org-latex-horizontal-rule (horizontal-rule contents info
)
1444 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1445 CONTENTS is nil. INFO is a plist holding contextual information."
1446 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule
))
1447 (prev (org-export-get-previous-element horizontal-rule info
)))
1449 ;; Make sure the rule doesn't start at the end of the current
1450 ;; line by separating it with a blank line from previous element.
1452 (let ((prev-blank (org-element-property :post-blank prev
)))
1453 (or (not prev-blank
) (zerop prev-blank
))))
1455 (org-latex--wrap-label
1457 (format "\\rule{%s}{%s}"
1458 (or (plist-get attr
:width
) "\\linewidth")
1459 (or (plist-get attr
:thickness
) "0.5pt"))))))
1462 ;;;; Inline Src Block
1464 (defun org-latex-inline-src-block (inline-src-block contents info
)
1465 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1466 CONTENTS holds the contents of the item. INFO is a plist holding
1467 contextual information."
1468 (let* ((code (org-element-property :value inline-src-block
))
1469 (separator (org-latex--find-verb-separator code
)))
1471 ;; Do not use a special package: transcode it verbatim.
1472 ((not org-latex-listings
)
1473 (concat "\\verb" separator code separator
))
1474 ;; Use minted package.
1475 ((eq org-latex-listings
'minted
)
1476 (let* ((org-lang (org-element-property :language inline-src-block
))
1477 (mint-lang (or (cadr (assq (intern org-lang
)
1478 org-latex-minted-langs
))
1479 (downcase org-lang
)))
1480 (options (org-latex--make-option-string
1481 org-latex-minted-options
)))
1482 (concat (format "\\mint%s{%s}"
1483 (if (string= options
"") "" (format "[%s]" options
))
1485 separator code separator
)))
1486 ;; Use listings package.
1488 ;; Maybe translate language's name.
1489 (let* ((org-lang (org-element-property :language inline-src-block
))
1490 (lst-lang (or (cadr (assq (intern org-lang
)
1491 org-latex-listings-langs
))
1493 (options (org-latex--make-option-string
1494 (append org-latex-listings-options
1495 `(("language" ,lst-lang
))))))
1496 (concat (format "\\lstinline[%s]" options
)
1497 separator code separator
))))))
1502 (defun org-latex-inlinetask (inlinetask contents info
)
1503 "Transcode an INLINETASK element from Org to LaTeX.
1504 CONTENTS holds the contents of the block. INFO is a plist
1505 holding contextual information."
1506 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1507 (todo (and (plist-get info
:with-todo-keywords
)
1508 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1509 (and todo
(org-export-data todo info
)))))
1510 (todo-type (org-element-property :todo-type inlinetask
))
1511 (tags (and (plist-get info
:with-tags
)
1512 (org-export-get-tags inlinetask info
)))
1513 (priority (and (plist-get info
:with-priority
)
1514 (org-element-property :priority inlinetask
))))
1515 ;; If `org-latex-format-inlinetask-function' is provided, call it
1516 ;; with appropriate arguments.
1517 (if (not (eq org-latex-format-inlinetask-function
'ignore
))
1518 (funcall org-latex-format-inlinetask-function
1519 todo todo-type priority title tags contents
)
1520 ;; Otherwise, use a default template.
1521 (org-latex--wrap-label
1525 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1526 (when priority
(format "\\framebox{\\#%c} " priority
))
1528 (when tags
(format "\\hfill{}\\textsc{:%s:}"
1529 (mapconcat #'identity tags
":"))))))
1530 (concat "\\begin{center}\n"
1532 "\\begin{minipage}[c]{.6\\textwidth}\n"
1534 (and (org-string-nw-p contents
)
1535 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents
))
1538 "\\end{center}"))))))
1543 (defun org-latex-italic (italic contents info
)
1544 "Transcode ITALIC from Org to LaTeX.
1545 CONTENTS is the text with italic markup. INFO is a plist holding
1546 contextual information."
1547 (org-latex--text-markup contents
'italic
))
1552 (defun org-latex-item (item contents info
)
1553 "Transcode an ITEM element from Org to LaTeX.
1554 CONTENTS holds the contents of the item. INFO is a plist holding
1555 contextual information."
1557 (let ((count (org-element-property :counter item
))
1559 ;; Determine level of current item to determine the
1560 ;; correct LaTeX counter to use (enumi, enumii...).
1561 (let ((parent item
) (level 0))
1562 (while (memq (org-element-type
1563 (setq parent
(org-export-get-parent parent
)))
1565 (when (and (eq (org-element-type parent
) 'plain-list
)
1566 (eq (org-element-property :type parent
)
1572 (format "\\setcounter{enum%s}{%s}\n"
1573 (nth (1- level
) '("i" "ii" "iii" "iv"))
1575 (checkbox (case (org-element-property :checkbox item
)
1576 (on "$\\boxtimes$ ")
1578 (trans "$\\boxminus$ ")))
1579 (tag (let ((tag (org-element-property :tag item
)))
1580 ;; Check-boxes must belong to the tag.
1581 (and tag
(format "[{%s}] "
1583 (org-export-data tag info
)))))))
1588 (checkbox (concat " " checkbox
))
1589 ;; Without a tag or a check-box, if CONTENTS starts with
1590 ;; an opening square bracket, add "\relax" to "\item",
1591 ;; unless the brackets comes from an initial export
1592 ;; snippet (i.e. it is inserted willingly by the user).
1594 (org-string-match-p "\\`[ \t]*\\[" contents
)
1595 (not (let ((e (car (org-element-contents item
))))
1596 (and (eq (org-element-type e
) 'paragraph
)
1597 (let ((o (car (org-element-contents e
))))
1598 (and (eq (org-element-type o
) 'export-snippet
)
1599 (eq (org-export-snippet-backend o
)
1603 (and contents
(org-trim contents
))
1604 ;; If there are footnotes references in tag, be sure to
1605 ;; add their definition at the end of the item. This
1606 ;; workaround is necessary since "\footnote{}" command is
1607 ;; not supported in tags.
1609 (org-latex--delayed-footnotes-definitions
1610 (org-element-property :tag item
) info
)))))
1615 (defun org-latex-keyword (keyword contents info
)
1616 "Transcode a KEYWORD element from Org to LaTeX.
1617 CONTENTS is nil. INFO is a plist holding contextual information."
1618 (let ((key (org-element-property :key keyword
))
1619 (value (org-element-property :value keyword
)))
1621 ((string= key
"LATEX") value
)
1622 ((string= key
"INDEX") (format "\\index{%s}" value
))
1623 ((string= key
"TOC")
1624 (let ((value (downcase value
)))
1626 ((string-match "\\<headlines\\>" value
)
1627 (let ((depth (or (and (string-match "[0-9]+" value
)
1628 (string-to-number (match-string 0 value
)))
1629 (plist-get info
:with-toc
))))
1631 (when (wholenump depth
)
1632 (format "\\setcounter{tocdepth}{%s}\n" depth
))
1633 "\\tableofcontents")))
1634 ((string= "tables" value
) "\\listoftables")
1635 ((string= "listings" value
)
1637 ((eq org-latex-listings
'minted
) "\\listoflistings")
1638 (org-latex-listings "\\lstlistoflistings")
1639 ;; At the moment, src blocks with a caption are wrapped
1640 ;; into a figure environment.
1641 (t "\\listoffigures")))))))))
1644 ;;;; Latex Environment
1646 (defun org-latex-latex-environment (latex-environment contents info
)
1647 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1648 CONTENTS is nil. INFO is a plist holding contextual information."
1649 (when (plist-get info
:with-latex
)
1650 (let ((label (org-element-property :name latex-environment
))
1651 (value (org-remove-indentation
1652 (org-element-property :value latex-environment
))))
1653 (if (not (org-string-nw-p label
)) value
1654 ;; Environment is labeled: label must be within the environment
1655 ;; (otherwise, a reference pointing to that element will count
1656 ;; the section instead).
1659 (goto-char (point-min))
1662 (format "\\label{%s}\n" (org-export-solidify-link-text label
)))
1663 (buffer-string))))))
1668 (defun org-latex-latex-fragment (latex-fragment contents info
)
1669 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1670 CONTENTS is nil. INFO is a plist holding contextual information."
1671 (when (plist-get info
:with-latex
)
1672 (org-element-property :value latex-fragment
)))
1677 (defun org-latex-line-break (line-break contents info
)
1678 "Transcode a LINE-BREAK object from Org to LaTeX.
1679 CONTENTS is nil. INFO is a plist holding contextual information."
1685 (defun org-latex--inline-image (link info
)
1686 "Return LaTeX code for an inline image.
1687 LINK is the link pointing to the inline image. INFO is a plist
1688 used as a communication channel."
1689 (let* ((parent (org-export-get-parent-element link
))
1690 (path (let ((raw-path (org-element-property :path link
)))
1691 (if (not (file-name-absolute-p raw-path
)) raw-path
1692 (expand-file-name raw-path
))))
1693 (filetype (file-name-extension path
))
1694 (caption (org-latex--caption/label-string parent info
))
1695 ;; Retrieve latex attributes from the element around.
1696 (attr (org-export-read-attribute :attr_latex parent
))
1697 (float (let ((float (plist-get attr
:float
)))
1698 (cond ((and (not float
) (plist-member attr
:float
)) nil
)
1699 ((string= float
"wrap") 'wrap
)
1700 ((string= float
"multicolumn") 'multicolumn
)
1702 (org-element-property :caption parent
)
1703 (org-string-nw-p (plist-get attr
:caption
)))
1706 (let ((place (plist-get attr
:placement
)))
1707 (cond (place (format "%s" place
))
1708 ((eq float
'wrap
) "{l}{0.5\\textwidth}")
1710 (format "[%s]" org-latex-default-figure-position
))
1712 (comment-include (if (plist-get attr
:comment-include
) "%" ""))
1713 ;; It is possible to specify width and height in the
1714 ;; ATTR_LATEX line, and also via default variables.
1715 (width (cond ((plist-get attr
:width
))
1716 ((plist-get attr
:height
) "")
1717 ((eq float
'wrap
) "0.48\\textwidth")
1718 (t org-latex-image-default-width
)))
1719 (height (cond ((plist-get attr
:height
))
1720 ((or (plist-get attr
:width
)
1721 (memq float
'(figure wrap
))) "")
1722 (t org-latex-image-default-height
)))
1723 (options (let ((opt (or (plist-get attr
:options
)
1724 org-latex-image-default-option
)))
1725 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt
)) opt
1726 (match-string 1 opt
))))
1728 (if (member filetype
'("tikz" "pgf"))
1730 ;; - use \input to read in image file.
1731 ;; - if options are present, wrap in a tikzpicture environment.
1732 ;; - if width or height are present, use \resizebox to change
1735 (setq image-code
(format "\\input{%s}" path
))
1736 (when (org-string-nw-p options
)
1738 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1741 (when (or (org-string-nw-p width
) (org-string-nw-p height
))
1742 (setq image-code
(format "\\resizebox{%s}{%s}{%s}"
1743 (if (org-string-nw-p width
) width
"!")
1744 (if (org-string-nw-p height
) height
"!")
1746 ;; For other images:
1747 ;; - add width and height to options.
1748 ;; - include the image with \includegraphics.
1749 (when (org-string-nw-p width
)
1750 (setq options
(concat options
",width=" width
)))
1751 (when (org-string-nw-p height
)
1752 (setq options
(concat options
",height=" height
)))
1754 (format "\\includegraphics%s{%s}"
1755 (cond ((not (org-string-nw-p options
)) "")
1756 ((= (aref options
0) ?
,)
1757 (format "[%s]"(substring options
1)))
1758 (t (format "[%s]" options
)))
1760 (when (equal filetype
"svg")
1761 (setq image-code
(replace-regexp-in-string "^\\\\includegraphics"
1765 (setq image-code
(replace-regexp-in-string "\\.svg}"
1769 ;; Return proper string, depending on FLOAT.
1771 (wrap (format "\\begin{wrapfigure}%s
1774 %s\\end{wrapfigure}" placement comment-include image-code caption
))
1775 (multicolumn (format "\\begin{figure*}%s
1778 %s\\end{figure*}" placement comment-include image-code caption
))
1779 (figure (format "\\begin{figure}%s
1782 %s\\end{figure}" placement comment-include image-code caption
))
1783 (otherwise image-code
))))
1785 (defun org-latex-link (link desc info
)
1786 "Transcode a LINK object from Org to LaTeX.
1788 DESC is the description part of the link, or the empty string.
1789 INFO is a plist holding contextual information. See
1791 (let* ((type (org-element-property :type link
))
1792 (raw-path (replace-regexp-in-string
1793 "%" "\\%" (org-element-property :path link
) nil t
))
1794 ;; Ensure DESC really exists, or set it to nil.
1795 (desc (and (not (string= desc
"")) desc
))
1796 (imagep (org-export-inline-image-p
1797 link org-latex-inline-image-rules
))
1799 ((member type
'("http" "https" "ftp" "mailto"))
1800 (concat type
":" raw-path
))
1801 ((and (string= type
"file") (file-name-absolute-p raw-path
))
1802 (concat "file:" raw-path
))
1807 (imagep (org-latex--inline-image link info
))
1808 ;; Radio link: Transcode target's contents and use them as link's
1810 ((string= type
"radio")
1811 (let ((destination (org-export-resolve-radio-link link info
)))
1812 (if (not destination
) desc
1813 (format "\\hyperref[%s]{%s}"
1814 (org-export-solidify-link-text
1815 (org-element-property :value destination
))
1817 ;; Links pointing to a headline: Find destination and build
1818 ;; appropriate referencing command.
1819 ((member type
'("custom-id" "fuzzy" "id"))
1820 (let ((destination (if (string= type
"fuzzy")
1821 (org-export-resolve-fuzzy-link link info
)
1822 (org-export-resolve-id-link link info
))))
1823 (case (org-element-type destination
)
1824 ;; Id link points to an external file.
1826 (if desc
(format "\\href{%s}{%s}" destination desc
)
1827 (format "\\url{%s}" destination
)))
1828 ;; Fuzzy link points nowhere.
1830 (format org-latex-link-with-unknown-path-format
1833 (org-element-property :raw-link link
) info
))))
1834 ;; LINK points to a headline. If headlines are numbered
1835 ;; and the link has no description, display headline's
1836 ;; number. Otherwise, display description or headline's
1843 (org-export-get-headline-number destination info
)
1846 (org-export-numbered-headline-p destination info
))
1847 (format "\\ref{%s}" label
)
1848 (format "\\hyperref[%s]{%s}" label
1851 (org-element-property :title destination
) info
))))))
1852 ;; Fuzzy link points to a target. Do as above.
1854 (let ((path (org-export-solidify-link-text path
)))
1855 (if (not desc
) (format "\\ref{%s}" path
)
1856 (format "\\hyperref[%s]{%s}" path desc
)))))))
1857 ;; Coderef: replace link with the reference name or the
1858 ;; equivalent line number.
1859 ((string= type
"coderef")
1860 (format (org-export-get-coderef-format path desc
)
1861 (org-export-resolve-coderef path info
)))
1862 ;; Link type is handled by a special function.
1863 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
1864 (funcall protocol
(org-link-unescape path
) desc
'latex
))
1865 ;; External link with a description part.
1866 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
1867 ;; External link without a description part.
1868 (path (format "\\url{%s}" path
))
1869 ;; No path, only description. Try to do something useful.
1870 (t (format org-latex-link-with-unknown-path-format desc
)))))
1875 (defun org-latex-paragraph (paragraph contents info
)
1876 "Transcode a PARAGRAPH element from Org to LaTeX.
1877 CONTENTS is the contents of the paragraph, as a string. INFO is
1878 the plist used as a communication channel."
1884 (defun org-latex-plain-list (plain-list contents info
)
1885 "Transcode a PLAIN-LIST element from Org to LaTeX.
1886 CONTENTS is the contents of the list. INFO is a plist holding
1887 contextual information."
1888 (let* ((type (org-element-property :type plain-list
))
1889 (attr (org-export-read-attribute :attr_latex plain-list
))
1890 (latex-type (let ((env (plist-get attr
:environment
)))
1891 (cond (env (format "%s" env
))
1892 ((eq type
'ordered
) "enumerate")
1893 ((eq type
'descriptive
) "description")
1895 (org-latex--wrap-label
1897 (format "\\begin{%s}%s\n%s\\end{%s}"
1899 (or (plist-get attr
:options
) "")
1906 (defun org-latex-plain-text (text info
)
1907 "Transcode a TEXT string from Org to LaTeX.
1908 TEXT is the string to transcode. INFO is a plist holding
1909 contextual information."
1910 (let ((specialp (plist-get info
:with-special-strings
))
1912 ;; Protect %, #, &, $, _, { and }.
1913 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output
)
1916 (format "\\%s" (match-string 2 output
)) nil t output
2)))
1919 (replace-regexp-in-string
1920 "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil
2))
1921 ;; Protect \. If special strings are used, be careful not to
1922 ;; protect "\" in "\-" constructs.
1923 (let ((symbols (if specialp
"-%$#&{}^_\\" "%$#&{}^_\\")))
1925 (replace-regexp-in-string
1926 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols
)
1927 "$\\backslash$" output nil t
1)))
1930 (replace-regexp-in-string
1931 "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t
2))
1932 ;; Activate smart quotes. Be sure to provide original TEXT string
1933 ;; since OUTPUT may have been modified.
1934 (when (plist-get info
:with-smart-quotes
)
1935 (setq output
(org-export-activate-smart-quotes output
:latex info text
)))
1936 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1937 (let ((case-fold-search nil
)
1939 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start
)
1940 (setq output
(replace-match
1941 (format "\\%s{}" (match-string 1 output
)) nil t output
)
1942 start
(match-end 0))))
1943 ;; Convert special strings.
1946 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t
)))
1947 ;; Handle break preservation if required.
1948 (when (plist-get info
:preserve-breaks
)
1949 (setq output
(replace-regexp-in-string
1950 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output
)))
1957 (defun org-latex-planning (planning contents info
)
1958 "Transcode a PLANNING element from Org to LaTeX.
1959 CONTENTS is nil. INFO is a plist holding contextual
1967 (let ((closed (org-element-property :closed planning
)))
1970 (format "\\textbf{%s} " org-closed-string
)
1971 (format org-latex-inactive-timestamp-format
1973 (org-element-property :raw-value closed
))))))
1974 (let ((deadline (org-element-property :deadline planning
)))
1977 (format "\\textbf{%s} " org-deadline-string
)
1978 (format org-latex-active-timestamp-format
1980 (org-element-property :raw-value deadline
))))))
1981 (let ((scheduled (org-element-property :scheduled planning
)))
1984 (format "\\textbf{%s} " org-scheduled-string
)
1985 (format org-latex-active-timestamp-format
1987 (org-element-property :raw-value scheduled
))))))))
1994 (defun org-latex-quote-block (quote-block contents info
)
1995 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1996 CONTENTS holds the contents of the block. INFO is a plist
1997 holding contextual information."
1998 (org-latex--wrap-label
2000 (format "\\begin{quote}\n%s\\end{quote}" contents
)))
2005 (defun org-latex-quote-section (quote-section contents info
)
2006 "Transcode a QUOTE-SECTION element from Org to LaTeX.
2007 CONTENTS is nil. INFO is a plist holding contextual information."
2008 (let ((value (org-remove-indentation
2009 (org-element-property :value quote-section
))))
2010 (when value
(format "\\begin{verbatim}\n%s\\end{verbatim}" value
))))
2015 (defun org-latex-radio-target (radio-target text info
)
2016 "Transcode a RADIO-TARGET object from Org to LaTeX.
2017 TEXT is the text of the target. INFO is a plist holding
2018 contextual information."
2019 (format "\\label{%s}%s"
2020 (org-export-solidify-link-text
2021 (org-element-property :value radio-target
))
2027 (defun org-latex-section (section contents info
)
2028 "Transcode a SECTION element from Org to LaTeX.
2029 CONTENTS holds the contents of the section. INFO is a plist
2030 holding contextual information."
2036 (defun org-latex-special-block (special-block contents info
)
2037 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2038 CONTENTS holds the contents of the block. INFO is a plist
2039 holding contextual information."
2040 (let ((type (downcase (org-element-property :type special-block
)))
2041 (opt (org-export-read-attribute :attr_latex special-block
:options
)))
2042 (concat (format "\\begin{%s}%s\n" type
(or opt
""))
2043 ;; Insert any label or caption within the block
2044 ;; (otherwise, a reference pointing to that element will
2045 ;; count the section instead).
2046 (org-latex--caption/label-string special-block info
)
2048 (format "\\end{%s}" type
))))
2053 (defun org-latex-src-block (src-block contents info
)
2054 "Transcode a SRC-BLOCK element from Org to LaTeX.
2055 CONTENTS holds the contents of the item. INFO is a plist holding
2056 contextual information."
2057 (when (org-string-nw-p (org-element-property :value src-block
))
2058 (let* ((lang (org-element-property :language src-block
))
2059 (caption (org-element-property :caption src-block
))
2060 (label (org-element-property :name src-block
))
2061 (custom-env (and lang
2062 (cadr (assq (intern lang
)
2063 org-latex-custom-lang-environments
))))
2064 (num-start (case (org-element-property :number-lines src-block
)
2065 (continued (org-export-get-loc src-block info
))
2067 (retain-labels (org-element-property :retain-labels src-block
))
2068 (attributes (org-export-read-attribute :attr_latex src-block
))
2069 (float (plist-get attributes
:float
)))
2071 ;; Case 1. No source fontification.
2072 ((not org-latex-listings
)
2073 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2075 (cond ((and (not float
) (plist-member attributes
:float
)) "%s")
2076 ((string= "multicolumn" float
)
2077 (format "\\begin{figure*}[%s]\n%%s%s\n\\end{figure*}"
2078 org-latex-default-figure-position
2081 (format "\\begin{figure}[H]\n%%s%s\n\\end{figure}"
2086 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2087 (org-export-format-code-default src-block info
))))))
2088 ;; Case 2. Custom environment.
2089 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2091 (org-export-format-code-default src-block info
)
2093 ;; Case 3. Use minted package.
2094 ((eq org-latex-listings
'minted
)
2095 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2097 (cond ((and (not float
) (plist-member attributes
:float
)) "%s")
2098 ((string= "multicolumn" float
)
2099 (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
2102 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2107 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2109 (org-latex--make-option-string
2110 (if (or (not num-start
)
2111 (assoc "linenos" org-latex-minted-options
))
2112 org-latex-minted-options
2115 ("firstnumber" ,(number-to-string (1+ num-start
))))
2116 org-latex-minted-options
)))
2118 (or (cadr (assq (intern lang
) org-latex-minted-langs
))
2121 (let* ((code-info (org-export-unravel-code src-block
))
2125 (org-split-string (car code-info
)
2127 (org-export-format-code
2129 (lambda (loc num ref
)
2133 ;; Ensure references are flushed to the right,
2134 ;; separated with 6 spaces from the widest line
2136 (concat (make-string (+ (- max-width
(length loc
)) 6)
2138 (format "(%s)" ref
)))))
2139 nil
(and retain-labels
(cdr code-info
)))))))
2141 (format float-env body
)))
2142 ;; Case 4. Use listings package.
2145 (or (cadr (assq (intern lang
) org-latex-listings-langs
)) lang
))
2148 (let ((main (org-export-get-caption src-block
))
2149 (secondary (org-export-get-caption src-block t
)))
2151 (format "{%s}" (org-export-data main info
))
2153 (org-export-data secondary info
)
2154 (org-export-data main info
)))))))
2159 (org-latex--make-option-string
2161 org-latex-listings-options
2163 ((and (not float
) (plist-member attributes
:float
)) nil
)
2164 ((string= "multicolumn" float
) '(("float" "*")))
2165 ((and float
(not (assoc "float" org-latex-listings-options
)))
2166 `(("float" ,org-latex-default-figure-position
))))
2167 `(("language" ,lst-lang
))
2168 (if label
`(("label" ,label
)) '(("label" " ")))
2169 (if caption-str
`(("caption" ,caption-str
)) '(("caption" " ")))
2170 (cond ((assoc "numbers" org-latex-listings-options
) nil
)
2171 ((not num-start
) '(("numbers" "none")))
2172 ((zerop num-start
) '(("numbers" "left")))
2173 (t `(("numbers" "left")
2175 ,(number-to-string (1+ num-start
)))))))))
2178 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2179 (let* ((code-info (org-export-unravel-code src-block
))
2183 (org-split-string (car code-info
) "\n")))))
2184 (org-export-format-code
2186 (lambda (loc num ref
)
2190 ;; Ensure references are flushed to the right,
2191 ;; separated with 6 spaces from the widest line of
2193 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2194 (format "(%s)" ref
)))))
2195 nil
(and retain-labels
(cdr code-info
))))))))))))
2198 ;;;; Statistics Cookie
2200 (defun org-latex-statistics-cookie (statistics-cookie contents info
)
2201 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2202 CONTENTS is nil. INFO is a plist holding contextual information."
2203 (replace-regexp-in-string
2204 "%" "\\%" (org-element-property :value statistics-cookie
) nil t
))
2209 (defun org-latex-strike-through (strike-through contents info
)
2210 "Transcode STRIKE-THROUGH from Org to LaTeX.
2211 CONTENTS is the text with strike-through markup. INFO is a plist
2212 holding contextual information."
2213 (org-latex--text-markup contents
'strike-through
))
2218 (defun org-latex--script-size (object info
)
2219 "Transcode a subscript or superscript object.
2220 OBJECT is an Org object. INFO is a plist used as a communication
2223 ;; Non-nil if object is already in a sub/superscript.
2224 (let ((parent object
))
2226 (while (setq parent
(org-export-get-parent parent
))
2227 (let ((type (org-element-type parent
)))
2228 (cond ((memq type
'(subscript superscript
))
2230 ((memq type org-element-all-elements
)
2231 (throw 'exit nil
))))))))
2232 (type (org-element-type object
))
2234 (org-element-map (org-element-contents object
)
2235 (cons 'plain-text org-element-all-objects
)
2237 (case (org-element-type obj
)
2238 ((entity latex-fragment
)
2239 (let ((data (org-trim (org-export-data obj info
))))
2241 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2245 (match-string 1 data
)
2246 (let ((blank (org-element-property :post-blank obj
)))
2247 (and blank
(> blank
0) "\\ "))))))
2250 (format "%s\\text{%s}" output
(org-export-data obj info
))))
2254 (org-export-data obj info
)
2255 (let ((blank (org-element-property :post-blank obj
)))
2256 (and blank
(> blank
0) "\\ ")))))))
2257 info nil org-element-recursive-objects
)
2258 ;; Result. Do not wrap into math mode if already in a subscript
2259 ;; or superscript. Do not wrap into curly brackets if OUTPUT is
2260 ;; a single character. Also merge consecutive subscript and
2261 ;; superscript into the same math snippet.
2262 (concat (and (not in-script-p
)
2263 (let ((prev (org-export-get-previous-element object info
)))
2265 (not (eq (org-element-type prev
)
2266 (if (eq type
'subscript
) 'superscript
2268 (let ((blank (org-element-property :post-blank prev
)))
2269 (and blank
(> blank
0)))))
2271 (if (eq (org-element-type object
) 'subscript
) "_" "^")
2272 (and (> (length output
) 1) "{")
2274 (and (> (length output
) 1) "}")
2275 (and (not in-script-p
)
2276 (or (let ((blank (org-element-property :post-blank object
)))
2277 (and blank
(> blank
0)))
2278 (not (eq (org-element-type
2279 (org-export-get-next-element object info
))
2280 (if (eq type
'subscript
) 'superscript
2284 (defun org-latex-subscript (subscript contents info
)
2285 "Transcode a SUBSCRIPT object from Org to LaTeX.
2286 CONTENTS is the contents of the object. INFO is a plist holding
2287 contextual information."
2288 (org-latex--script-size subscript info
))
2293 (defun org-latex-superscript (superscript contents info
)
2294 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2295 CONTENTS is the contents of the object. INFO is a plist holding
2296 contextual information."
2297 (org-latex--script-size superscript info
))
2302 ;; `org-latex-table' is the entry point for table transcoding. It
2303 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2304 ;; delegates the job to either `org-latex--table.el-table',
2305 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2306 ;; depending of the type of the table and the mode requested.
2308 ;; `org-latex--align-string' is a subroutine used to build alignment
2309 ;; string for Org tables.
2311 (defun org-latex-table (table contents info
)
2312 "Transcode a TABLE element from Org to LaTeX.
2313 CONTENTS is the contents of the table. INFO is a plist holding
2314 contextual information."
2315 (if (eq (org-element-property :type table
) 'table.el
)
2316 ;; "table.el" table. Convert it using appropriate tools.
2317 (org-latex--table.el-table table info
)
2318 (let ((type (or (org-export-read-attribute :attr_latex table
:mode
)
2319 org-latex-default-table-mode
)))
2321 ;; Case 1: Verbatim table.
2322 ((string= type
"verbatim")
2323 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2324 ;; Re-create table, without affiliated keywords.
2325 (org-trim (org-element-interpret-data
2326 `(table nil
,@(org-element-contents table
))))))
2328 ((or (string= type
"math") (string= type
"inline-math"))
2329 (org-latex--math-table table info
))
2330 ;; Case 3: Standard table.
2331 (t (concat (org-latex--org-table table contents info
)
2332 ;; When there are footnote references within the
2333 ;; table, insert their definition just after it.
2334 (org-latex--delayed-footnotes-definitions table info
)))))))
2336 (defun org-latex--align-string (table info
)
2337 "Return an appropriate LaTeX alignment string.
2338 TABLE is the considered table. INFO is a plist used as
2339 a communication channel."
2340 (or (org-export-read-attribute :attr_latex table
:align
)
2342 ;; Extract column groups and alignment from first (non-rule)
2345 (org-element-map table
'table-row
2347 (and (eq (org-element-property :type row
) 'standard
) row
))
2351 (let ((borders (org-export-table-cell-borders cell info
)))
2352 ;; Check left border for the first cell only.
2353 (when (and (memq 'left borders
) (not align
))
2355 (push (case (org-export-table-cell-alignment cell info
)
2360 (when (memq 'right borders
) (push "|" align
))))
2362 (apply 'concat
(nreverse align
)))))
2364 (defun org-latex--org-table (table contents info
)
2365 "Return appropriate LaTeX code for an Org table.
2367 TABLE is the table type element to transcode. CONTENTS is its
2368 contents, as a string. INFO is a plist used as a communication
2371 This function assumes TABLE has `org' as its `:type' property and
2372 `table' as its `:mode' attribute."
2373 (let* ((caption (org-latex--caption/label-string table info
))
2374 (attr (org-export-read-attribute :attr_latex table
))
2375 ;; Determine alignment string.
2376 (alignment (org-latex--align-string table info
))
2377 ;; Determine environment for the table: longtable, tabular...
2378 (table-env (or (plist-get attr
:environment
)
2379 org-latex-default-table-environment
))
2380 ;; If table is a float, determine environment: table, table*
2381 ;; or sidewaystable.
2382 (float-env (unless (member table-env
'("longtable" "longtabu"))
2383 (let ((float (plist-get attr
:float
)))
2385 ((and (not float
) (plist-member attr
:float
)) nil
)
2386 ((string= float
"sidewaystable") "sidewaystable")
2387 ((string= float
"multicolumn") "table*")
2389 (org-element-property :caption table
)
2390 (org-string-nw-p (plist-get attr
:caption
)))
2392 ;; Extract others display options.
2393 (fontsize (let ((font (plist-get attr
:font
)))
2394 (and font
(concat font
"\n"))))
2395 (width (plist-get attr
:width
))
2396 (spreadp (plist-get attr
:spread
))
2397 (placement (or (plist-get attr
:placement
)
2398 (format "[%s]" org-latex-default-figure-position
)))
2399 (centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2400 org-latex-tables-centered
)))
2401 ;; Prepare the final format string for the table.
2404 ((equal "longtable" table-env
)
2405 (concat (and fontsize
(concat "{" fontsize
))
2406 (format "\\begin{longtable}{%s}\n" alignment
)
2407 (and org-latex-table-caption-above
2408 (org-string-nw-p caption
)
2409 (concat caption
"\\\\\n"))
2411 (and (not org-latex-table-caption-above
)
2412 (org-string-nw-p caption
)
2413 (concat caption
"\\\\\n"))
2414 "\\end{longtable}\n"
2415 (and fontsize
"}")))
2417 ((equal "longtabu" table-env
)
2418 (concat (and fontsize
(concat "{" fontsize
))
2419 (format "\\begin{longtabu}%s{%s}\n"
2422 (if spreadp
"spread" "to") width
) "")
2424 (and org-latex-table-caption-above
2425 (org-string-nw-p caption
)
2426 (concat caption
"\\\\\n"))
2428 (and (not org-latex-table-caption-above
)
2429 (org-string-nw-p caption
)
2430 (concat caption
"\\\\\n"))
2432 (and fontsize
"}")))
2436 (concat (format "\\begin{%s}%s\n" float-env placement
)
2437 (if org-latex-table-caption-above caption
"")
2438 (when centerp
"\\centering\n")
2440 (centerp (concat "\\begin{center}\n" fontsize
))
2441 (fontsize (concat "{" fontsize
)))
2442 (cond ((equal "tabu" table-env
)
2443 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2445 (if spreadp
" spread %s " " to %s ")
2449 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2451 (if width
(format "{%s}" width
) "")
2457 (concat (if org-latex-table-caption-above
"" caption
)
2458 (format "\n\\end{%s}" float-env
)))
2459 (centerp "\n\\end{center}")
2460 (fontsize "}")))))))
2462 (defun org-latex--table.el-table
(table info
)
2463 "Return appropriate LaTeX code for a table.el table.
2465 TABLE is the table type element to transcode. INFO is a plist
2466 used as a communication channel.
2468 This function assumes TABLE has `table.el' as its `:type'
2471 ;; Ensure "*org-export-table*" buffer is empty.
2472 (with-current-buffer (get-buffer-create "*org-export-table*")
2474 (let ((output (with-temp-buffer
2475 (insert (org-element-property :value table
))
2477 (re-search-forward "^[ \t]*|[^|]" nil t
)
2478 (table-generate-source 'latex
"*org-export-table*")
2479 (with-current-buffer "*org-export-table*"
2480 (org-trim (buffer-string))))))
2481 (kill-buffer (get-buffer "*org-export-table*"))
2482 ;; Remove left out comments.
2483 (while (string-match "^%.*\n" output
)
2484 (setq output
(replace-match "" t t output
)))
2485 (let ((attr (org-export-read-attribute :attr_latex table
)))
2486 (when (plist-get attr
:rmlines
)
2487 ;; When the "rmlines" attribute is provided, remove all hlines
2488 ;; but the the one separating heading from the table body.
2489 (let ((n 0) (pos 0))
2490 (while (and (< (length output
) pos
)
2491 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
2493 (unless (= n
2) (setq output
(replace-match "" nil nil output
))))))
2494 (let ((centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2495 org-latex-tables-centered
)))
2496 (if (not centerp
) output
2497 (format "\\begin{center}\n%s\n\\end{center}" output
))))))
2499 (defun org-latex--math-table (table info
)
2500 "Return appropriate LaTeX code for a matrix.
2502 TABLE is the table type element to transcode. INFO is a plist
2503 used as a communication channel.
2505 This function assumes TABLE has `org' as its `:type' property and
2506 `inline-math' or `math' as its `:mode' attribute.."
2507 (let* ((caption (org-latex--caption/label-string table info
))
2508 (attr (org-export-read-attribute :attr_latex table
))
2509 (inlinep (equal (plist-get attr
:mode
) "inline-math"))
2510 (env (or (plist-get attr
:environment
)
2511 org-latex-default-table-environment
))
2515 ;; Ignore horizontal rules.
2516 (when (eq (org-element-property :type row
) 'standard
)
2517 ;; Return each cell unmodified.
2521 (substring (org-element-interpret-data cell
) 0 -
1))
2522 (org-element-map row
'table-cell
'identity info
) "&")
2523 (or (cdr (assoc env org-latex-table-matrix-macros
)) "\\\\")
2525 (org-element-map table
'table-row
'identity info
) ""))
2526 ;; Variables related to math clusters (contiguous math tables
2527 ;; of the same type).
2528 (mode (org-export-read-attribute :attr_latex table
:mode
))
2529 (prev (org-export-get-previous-element table info
))
2530 (next (org-export-get-next-element table info
))
2533 ;; Non-nil when TABLE has the same mode as current table.
2534 (string= (or (org-export-read-attribute :attr_latex table
:mode
)
2535 org-latex-default-table-mode
)
2538 ;; Opening string. If TABLE is in the middle of a table cluster,
2539 ;; do not insert any.
2541 (eq (org-element-type prev
) 'table
)
2542 (memq (org-element-property :post-blank prev
) '(0 nil
))
2543 (funcall same-mode-p prev
))
2546 ((org-string-nw-p caption
) (concat "\\begin{equation}\n" caption
))
2549 (or (plist-get attr
:math-prefix
) "")
2550 ;; Environment. Also treat special cases.
2551 (cond ((equal env
"array")
2552 (let ((align (org-latex--align-string table info
)))
2553 (format "\\begin{array}{%s}\n%s\\end{array}" align contents
)))
2554 ((assoc env org-latex-table-matrix-macros
)
2555 (format "\\%s%s{\n%s}"
2557 (or (plist-get attr
:math-arguments
) "")
2559 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env
)))
2561 (or (plist-get attr
:math-suffix
) "")
2562 ;; Closing string. If TABLE is in the middle of a table cluster,
2563 ;; do not insert any. If it closes such a cluster, be sure to
2564 ;; close the cluster with a string matching the opening string.
2566 (eq (org-element-type next
) 'table
)
2567 (memq (org-element-property :post-blank table
) '(0 nil
))
2568 (funcall same-mode-p next
))
2571 ;; Find cluster beginning to know which environment to use.
2572 ((let ((cluster-beg table
) prev
)
2573 (while (and (setq prev
(org-export-get-previous-element
2575 (memq (org-element-property :post-blank prev
)
2577 (funcall same-mode-p prev
))
2578 (setq cluster-beg prev
))
2579 (and (or (org-element-property :caption cluster-beg
)
2580 (org-element-property :name cluster-beg
))
2581 "\n\\end{equation}")))
2587 (defun org-latex-table-cell (table-cell contents info
)
2588 "Transcode a TABLE-CELL element from Org to LaTeX.
2589 CONTENTS is the cell contents. INFO is a plist used as
2590 a communication channel."
2591 (concat (if (and contents
2592 org-latex-table-scientific-notation
2593 (string-match orgtbl-exp-regexp contents
))
2594 ;; Use appropriate format string for scientific
2596 (format org-latex-table-scientific-notation
2597 (match-string 1 contents
)
2598 (match-string 2 contents
))
2600 (when (org-export-get-next-element table-cell info
) " & ")))
2605 (defun org-latex-table-row (table-row contents info
)
2606 "Transcode a TABLE-ROW element from Org to LaTeX.
2607 CONTENTS is the contents of the row. INFO is a plist used as
2608 a communication channel."
2609 ;; Rules are ignored since table separators are deduced from
2610 ;; borders of the current row.
2611 (when (eq (org-element-property :type table-row
) 'standard
)
2612 (let* ((attr (org-export-read-attribute :attr_latex
2613 (org-export-get-parent table-row
)))
2614 (longtablep (member (or (plist-get attr
:environment
)
2615 org-latex-default-table-environment
)
2616 '("longtable" "longtabu")))
2617 (booktabsp (if (plist-member attr
:booktabs
)
2618 (plist-get attr
:booktabs
)
2619 org-latex-tables-booktabs
))
2620 ;; TABLE-ROW's borders are extracted from its first cell.
2621 (borders (org-export-table-cell-borders
2622 (car (org-element-contents table-row
)) info
)))
2624 ;; When BOOKTABS are activated enforce top-rule even when no
2625 ;; hline was specifically marked.
2626 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2627 ((and (memq 'top borders
) (memq 'above borders
)) "\\hline\n"))
2630 ;; Special case for long tables. Define header and footers.
2631 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
2634 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2637 (if booktabsp
"\\midrule" "\\hline")
2638 (if booktabsp
"\\midrule" "\\hline")
2639 ;; Number of columns.
2640 (cdr (org-export-table-dimensions
2641 (org-export-get-parent-table table-row
) info
))))
2642 ;; When BOOKTABS are activated enforce bottom rule even when
2643 ;; no hline was specifically marked.
2644 ((and booktabsp
(memq 'bottom borders
)) "\\bottomrule")
2645 ((and (memq 'bottom borders
) (memq 'below borders
)) "\\hline")
2646 ((memq 'below borders
) (if booktabsp
"\\midrule" "\\hline")))))))
2651 (defun org-latex-target (target contents info
)
2652 "Transcode a TARGET object from Org to LaTeX.
2653 CONTENTS is nil. INFO is a plist holding contextual
2655 (format "\\label{%s}"
2656 (org-export-solidify-link-text (org-element-property :value target
))))
2661 (defun org-latex-timestamp (timestamp contents info
)
2662 "Transcode a TIMESTAMP object from Org to LaTeX.
2663 CONTENTS is nil. INFO is a plist holding contextual
2665 (let ((value (org-latex-plain-text
2666 (org-timestamp-translate timestamp
) info
)))
2667 (case (org-element-property :type timestamp
)
2668 ((active active-range
) (format org-latex-active-timestamp-format value
))
2669 ((inactive inactive-range
)
2670 (format org-latex-inactive-timestamp-format value
))
2671 (otherwise (format org-latex-diary-timestamp-format value
)))))
2676 (defun org-latex-underline (underline contents info
)
2677 "Transcode UNDERLINE from Org to LaTeX.
2678 CONTENTS is the text with underline markup. INFO is a plist
2679 holding contextual information."
2680 (org-latex--text-markup contents
'underline
))
2685 (defun org-latex-verbatim (verbatim contents info
)
2686 "Transcode a VERBATIM object from Org to LaTeX.
2687 CONTENTS is nil. INFO is a plist used as a communication
2689 (org-latex--text-markup (org-element-property :value verbatim
) 'verbatim
))
2694 (defun org-latex-verse-block (verse-block contents info
)
2695 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2696 CONTENTS is verse block contents. INFO is a plist holding
2697 contextual information."
2698 (org-latex--wrap-label
2700 ;; In a verse environment, add a line break to each newline
2701 ;; character and change each white space at beginning of a line
2702 ;; into a space of 1 em. Also change each blank line with
2703 ;; a vertical space of 1 em.
2705 (setq contents
(replace-regexp-in-string
2706 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2707 (replace-regexp-in-string
2708 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents
)))
2709 (while (string-match "^[ \t]+" contents
)
2710 (let ((new-str (format "\\hspace*{%dem}"
2711 (length (match-string 0 contents
)))))
2712 (setq contents
(replace-match new-str nil t contents
))))
2713 (format "\\begin{verse}\n%s\\end{verse}" contents
))))
2717 ;;; End-user functions
2720 (defun org-latex-export-as-latex
2721 (&optional async subtreep visible-only body-only ext-plist
)
2722 "Export current buffer as a LaTeX buffer.
2724 If narrowing is active in the current buffer, only export its
2727 If a region is active, export that region.
2729 A non-nil optional argument ASYNC means the process should happen
2730 asynchronously. The resulting buffer should be accessible
2731 through the `org-export-stack' interface.
2733 When optional argument SUBTREEP is non-nil, export the sub-tree
2734 at point, extracting information from the headline properties
2737 When optional argument VISIBLE-ONLY is non-nil, don't export
2738 contents of hidden elements.
2740 When optional argument BODY-ONLY is non-nil, only write code
2741 between \"\\begin{document}\" and \"\\end{document}\".
2743 EXT-PLIST, when provided, is a property list with external
2744 parameters overriding Org default settings, but still inferior to
2745 file-local settings.
2747 Export is done in a buffer named \"*Org LATEX Export*\", which
2748 will be displayed when `org-export-show-temporary-export-buffer'
2751 (org-export-to-buffer 'latex
"*Org LATEX Export*"
2752 async subtreep visible-only body-only ext-plist
(lambda () (LaTeX-mode))))
2755 (defun org-latex-convert-region-to-latex ()
2756 "Assume the current region has org-mode syntax, and convert it to LaTeX.
2757 This can be used in any buffer. For example, you can write an
2758 itemized list in org-mode syntax in an LaTeX buffer and use this
2759 command to convert it."
2761 (org-export-replace-region-by 'latex
))
2764 (defun org-latex-export-to-latex
2765 (&optional async subtreep visible-only body-only ext-plist
)
2766 "Export current buffer to a LaTeX file.
2768 If narrowing is active in the current buffer, only export its
2771 If a region is active, export that region.
2773 A non-nil optional argument ASYNC means the process should happen
2774 asynchronously. The resulting file should be accessible through
2775 the `org-export-stack' interface.
2777 When optional argument SUBTREEP is non-nil, export the sub-tree
2778 at point, extracting information from the headline properties
2781 When optional argument VISIBLE-ONLY is non-nil, don't export
2782 contents of hidden elements.
2784 When optional argument BODY-ONLY is non-nil, only write code
2785 between \"\\begin{document}\" and \"\\end{document}\".
2787 EXT-PLIST, when provided, is a property list with external
2788 parameters overriding Org default settings, but still inferior to
2789 file-local settings."
2791 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
2792 (org-export-to-file 'latex outfile
2793 async subtreep visible-only body-only ext-plist
)))
2796 (defun org-latex-export-to-pdf
2797 (&optional async subtreep visible-only body-only ext-plist
)
2798 "Export current buffer to LaTeX then process through to PDF.
2800 If narrowing is active in the current buffer, only export its
2803 If a region is active, export that region.
2805 A non-nil optional argument ASYNC means the process should happen
2806 asynchronously. The resulting file should be accessible through
2807 the `org-export-stack' interface.
2809 When optional argument SUBTREEP is non-nil, export the sub-tree
2810 at point, extracting information from the headline properties
2813 When optional argument VISIBLE-ONLY is non-nil, don't export
2814 contents of hidden elements.
2816 When optional argument BODY-ONLY is non-nil, only write code
2817 between \"\\begin{document}\" and \"\\end{document}\".
2819 EXT-PLIST, when provided, is a property list with external
2820 parameters overriding Org default settings, but still inferior to
2821 file-local settings.
2823 Return PDF file's name."
2825 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
2826 (org-export-to-file 'latex outfile
2827 async subtreep visible-only body-only ext-plist
2828 (lambda (file) (org-latex-compile file
)))))
2830 (defun org-latex-compile (texfile &optional snippet
)
2831 "Compile a TeX file.
2833 TEXFILE is the name of the file being compiled. Processing is
2834 done through the command specified in `org-latex-pdf-process'.
2836 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
2837 file used to preview a LaTeX snippet. In this case, do not
2838 create a log buffer and do not bother removing log files.
2840 Return PDF file name or an error if it couldn't be produced."
2841 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile
)))
2842 (full-name (file-truename texfile
))
2843 (out-dir (file-name-directory texfile
))
2844 ;; Properly set working directory for compilation.
2845 (default-directory (if (file-name-absolute-p texfile
)
2846 (file-name-directory full-name
)
2849 (unless snippet
(message "Processing LaTeX file %s..." texfile
))
2850 (save-window-excursion
2852 ;; A function is provided: Apply it.
2853 ((functionp org-latex-pdf-process
)
2854 (funcall org-latex-pdf-process
(shell-quote-argument texfile
)))
2855 ;; A list is provided: Replace %b, %f and %o with appropriate
2856 ;; values in each command before applying it. Output is
2857 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2858 ((consp org-latex-pdf-process
)
2859 (let ((outbuf (and (not snippet
)
2860 (get-buffer-create "*Org PDF LaTeX Output*"))))
2864 (replace-regexp-in-string
2865 "%b" (shell-quote-argument base-name
)
2866 (replace-regexp-in-string
2867 "%f" (shell-quote-argument full-name
)
2868 (replace-regexp-in-string
2869 "%o" (shell-quote-argument out-dir
) command t t
) t t
) t t
)
2871 org-latex-pdf-process
)
2872 ;; Collect standard errors from output buffer.
2873 (setq errors
(and (not snippet
) (org-latex--collect-errors outbuf
)))))
2874 (t (error "No valid command to process to PDF")))
2875 (let ((pdffile (concat out-dir base-name
".pdf")))
2876 ;; Check for process failure. Provide collected errors if
2878 (if (not (file-exists-p pdffile
))
2879 (error "PDF file %s wasn't produced%s" pdffile
2880 (if errors
(concat ": " errors
) ""))
2881 ;; Else remove log files, when specified, and signal end of
2882 ;; process to user, along with any error encountered.
2883 (when (and (not snippet
) org-latex-remove-logfiles
)
2884 (dolist (file (directory-files
2886 (concat (regexp-quote base-name
)
2887 "\\(?:\\.[0-9]+\\)?"
2889 (regexp-opt org-latex-logfiles-extensions
))))
2890 (delete-file file
)))
2891 (message (concat "Process completed"
2892 (if (not errors
) "."
2893 (concat " with errors: " errors
)))))
2894 ;; Return output file name.
2897 (defun org-latex--collect-errors (buffer)
2898 "Collect some kind of errors from \"pdflatex\" command output.
2900 BUFFER is the buffer containing output.
2902 Return collected error types as a string, or nil if there was
2904 (with-current-buffer buffer
2906 (goto-char (point-max))
2907 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t
)
2908 (let ((case-fold-search t
)
2910 (dolist (latex-error org-latex-known-errors
)
2911 (when (save-excursion (re-search-forward (car latex-error
) nil t
))
2912 (setq errors
(concat errors
" " (cdr latex-error
)))))
2913 (and (org-string-nw-p errors
) (org-trim errors
)))))))
2916 (defun org-latex-publish-to-latex (plist filename pub-dir
)
2917 "Publish an Org file to LaTeX.
2919 FILENAME is the filename of the Org file to be published. PLIST
2920 is the property list for the given project. PUB-DIR is the
2921 publishing directory.
2923 Return output file name."
2924 (org-publish-org-to 'latex filename
".tex" plist pub-dir
))
2927 (defun org-latex-publish-to-pdf (plist filename pub-dir
)
2928 "Publish an Org file to PDF (via LaTeX).
2930 FILENAME is the filename of the Org file to be published. PLIST
2931 is the property list for the given project. PUB-DIR is the
2932 publishing directory.
2934 Return output file name."
2935 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
2936 ;; in working directory and then moved to publishing directory.
2937 (org-publish-attachment
2941 'latex filename
".tex" plist
(file-name-directory filename
)))
2948 ;; generated-autoload-file: "org-loaddefs.el"
2951 ;;; ox-latex.el ends here