1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;; See Org manual for details.
27 (eval-when-compile (require 'cl
))
31 (defvar org-latex-default-packages-alist
)
32 (defvar org-latex-packages-alist
)
33 (defvar orgtbl-exp-regexp
)
39 (org-export-define-backend 'latex
40 '((bold . org-latex-bold
)
41 (center-block . org-latex-center-block
)
42 (clock . org-latex-clock
)
43 (code . org-latex-code
)
44 (comment .
(lambda (&rest args
) ""))
45 (comment-block .
(lambda (&rest args
) ""))
46 (drawer . org-latex-drawer
)
47 (dynamic-block . org-latex-dynamic-block
)
48 (entity . org-latex-entity
)
49 (example-block . org-latex-example-block
)
50 (export-block . org-latex-export-block
)
51 (export-snippet . org-latex-export-snippet
)
52 (fixed-width . org-latex-fixed-width
)
53 (footnote-definition . org-latex-footnote-definition
)
54 (footnote-reference . org-latex-footnote-reference
)
55 (headline . org-latex-headline
)
56 (horizontal-rule . org-latex-horizontal-rule
)
57 (inline-src-block . org-latex-inline-src-block
)
58 (inlinetask . org-latex-inlinetask
)
59 (italic . org-latex-italic
)
60 (item . org-latex-item
)
61 (keyword . org-latex-keyword
)
62 (latex-environment . org-latex-latex-environment
)
63 (latex-fragment . org-latex-latex-fragment
)
64 (line-break . org-latex-line-break
)
65 (link . org-latex-link
)
66 (paragraph . org-latex-paragraph
)
67 (plain-list . org-latex-plain-list
)
68 (plain-text . org-latex-plain-text
)
69 (planning . org-latex-planning
)
70 (property-drawer .
(lambda (&rest args
) ""))
71 (quote-block . org-latex-quote-block
)
72 (quote-section . org-latex-quote-section
)
73 (radio-target . org-latex-radio-target
)
74 (section . org-latex-section
)
75 (special-block . org-latex-special-block
)
76 (src-block . org-latex-src-block
)
77 (statistics-cookie . org-latex-statistics-cookie
)
78 (strike-through . org-latex-strike-through
)
79 (subscript . org-latex-subscript
)
80 (superscript . org-latex-superscript
)
81 (table . org-latex-table
)
82 (table-cell . org-latex-table-cell
)
83 (table-row . org-latex-table-row
)
84 (target . org-latex-target
)
85 (template . org-latex-template
)
86 (timestamp . org-latex-timestamp
)
87 (underline . org-latex-underline
)
88 (verbatim . org-latex-verbatim
)
89 (verse-block . org-latex-verse-block
))
90 :export-block
'("LATEX" "TEX")
92 '(?l
"Export to LaTeX"
93 ((?L
"As LaTeX buffer" org-latex-export-as-latex
)
94 (?l
"As LaTeX file" org-latex-export-to-latex
)
95 (?p
"As PDF file" org-latex-export-to-pdf
)
96 (?o
"As PDF file and open"
98 (if a
(org-latex-export-to-pdf t s v b
)
99 (org-open-file (org-latex-export-to-pdf nil s v b
)))))))
100 :options-alist
'((:latex-class
"LATEX_CLASS" nil org-latex-default-class t
)
101 (:latex-class-options
"LATEX_CLASS_OPTIONS" nil nil t
)
102 (:latex-header
"LATEX_HEADER" nil nil newline
)
103 (:latex-header-extra
"LATEX_HEADER_EXTRA" nil nil newline
)
104 (:latex-hyperref-p nil
"texht" org-latex-with-hyperref t
)
105 ;; Redefine regular options.
106 (:date
"DATE" nil
"\\today" t
)))
110 ;;; Internal Variables
112 (defconst org-latex-babel-language-alist
113 '(("af" .
"afrikaans")
115 ("bt-br" .
"brazilian")
121 ("de-at" .
"naustrian")
122 ("de-de" .
"ngerman")
125 ("en-au" .
"australian")
126 ("en-ca" .
"canadian")
127 ("en-gb" .
"british")
129 ("en-nz" .
"newzealand")
130 ("en-us" .
"american")
136 ("fr-ca" .
"canadien")
140 ("id" .
"indonesian")
146 ("no-no" .
"nynorsk")
148 ("pt" .
"portuguese")
152 ("sb" .
"uppersorbian")
160 ("uk" .
"ukrainian"))
161 "Alist between language code and corresponding Babel option.")
163 (defconst org-latex-table-matrix-macros
'(("bordermatrix" .
"\\cr")
164 ("qbordermatrix" .
"\\cr")
165 ("kbordermatrix" .
"\\\\"))
166 "Alist between matrix macros and their row ending.")
170 ;;; User Configurable Variables
172 (defgroup org-export-latex nil
173 "Options for exporting Org mode files to LaTeX."
174 :tag
"Org Export LaTeX"
180 (defcustom org-latex-default-class
"article"
181 "The default LaTeX class."
182 :group
'org-export-latex
183 :type
'(string :tag
"LaTeX class"))
185 (defcustom org-latex-classes
187 "\\documentclass[11pt]{article}"
188 ("\\section{%s}" .
"\\section*{%s}")
189 ("\\subsection{%s}" .
"\\subsection*{%s}")
190 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
191 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
192 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
194 "\\documentclass[11pt]{report}"
195 ("\\part{%s}" .
"\\part*{%s}")
196 ("\\chapter{%s}" .
"\\chapter*{%s}")
197 ("\\section{%s}" .
"\\section*{%s}")
198 ("\\subsection{%s}" .
"\\subsection*{%s}")
199 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
201 "\\documentclass[11pt]{book}"
202 ("\\part{%s}" .
"\\part*{%s}")
203 ("\\chapter{%s}" .
"\\chapter*{%s}")
204 ("\\section{%s}" .
"\\section*{%s}")
205 ("\\subsection{%s}" .
"\\subsection*{%s}")
206 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
207 "Alist of LaTeX classes and associated header and structure.
208 If #+LATEX_CLASS is set in the buffer, use its value and the
209 associated information. Here is the structure of each cell:
213 \(numbered-section . unnumbered-section)
219 The HEADER-STRING is the header that will be inserted into the
220 LaTeX file. It should contain the \\documentclass macro, and
221 anything else that is needed for this setup. To this header, the
222 following commands will be added:
224 - Calls to \\usepackage for all packages mentioned in the
225 variables `org-latex-default-packages-alist' and
226 `org-latex-packages-alist'. Thus, your header definitions
227 should avoid to also request these packages.
229 - Lines specified via \"#+LATEX_HEADER:\" and
230 \"#+LATEX_HEADER_EXTRA:\" keywords.
232 If you need more control about the sequence in which the header
233 is built up, or if you want to exclude one of these building
234 blocks for a particular class, you can use the following
235 macro-like placeholders.
237 [DEFAULT-PACKAGES] \\usepackage statements for default packages
238 [NO-DEFAULT-PACKAGES] do not include any of the default packages
239 [PACKAGES] \\usepackage statements for packages
240 [NO-PACKAGES] do not include the packages
241 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
242 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
246 \\documentclass{article}
247 [NO-DEFAULT-PACKAGES]
249 \\providecommand{\\alert}[1]{\\textbf{#1}}
252 will omit the default packages, and will include the
253 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
254 to \\providecommand, and then place \\usepackage commands based
255 on the content of `org-latex-packages-alist'.
257 If your header, `org-latex-default-packages-alist' or
258 `org-latex-packages-alist' inserts
259 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
260 replaced with a coding system derived from
261 `buffer-file-coding-system'. See also the variable
262 `org-latex-inputenc-alist' for a way to influence this mechanism.
264 The sectioning structure
265 ------------------------
267 The sectioning structure of the class is given by the elements
268 following the header string. For each sectioning level, a number
269 of strings is specified. A %s formatter is mandatory in each
270 section string and will be replaced by the title of the section.
272 Instead of a cons cell (numbered . unnumbered), you can also
273 provide a list of 2 or 4 elements,
275 \(numbered-open numbered-close)
279 \(numbered-open numbered-close unnumbered-open unnumbered-close)
281 providing opening and closing strings for a LaTeX environment
282 that should represent the document section. The opening clause
283 should have a %s to represent the section title.
285 Instead of a list of sectioning commands, you can also specify
286 a function name. That function will be called with two
287 parameters, the (reduced) level of the headline, and a predicate
288 non-nil when the headline should be numbered. It must return
289 a format string in which the section title will be added."
290 :group
'org-export-latex
292 (list (string :tag
"LaTeX class")
293 (string :tag
"LaTeX header")
294 (repeat :tag
"Levels" :inline t
297 (string :tag
" numbered")
298 (string :tag
"unnumbered"))
299 (list :tag
"Environment"
300 (string :tag
"Opening (numbered)")
301 (string :tag
"Closing (numbered)")
302 (string :tag
"Opening (unnumbered)")
303 (string :tag
"Closing (unnumbered)"))
304 (function :tag
"Hook computing sectioning"))))))
306 (defcustom org-latex-inputenc-alist nil
307 "Alist of inputenc coding system names, and what should really be used.
308 For example, adding an entry
310 (\"utf8\" . \"utf8x\")
312 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
313 are written as utf8 files."
314 :group
'org-export-latex
317 (string :tag
"Derived from buffer")
318 (string :tag
"Use this instead"))))
320 (defcustom org-latex-title-command
"\\maketitle"
321 "The command used to insert the title just after \\begin{document}.
322 If this string contains the formatting specification \"%s\" then
323 it will be used as a formatting string, passing the title as an
325 :group
'org-export-latex
328 (defcustom org-latex-toc-command
"\\tableofcontents\n\n"
329 "LaTeX command to set the table of contents, list of figures, etc.
330 This command only applies to the table of contents generated with
331 the toc:nil option, not to those generated with #+TOC keyword."
332 :group
'org-export-latex
335 (defcustom org-latex-with-hyperref t
336 "Toggle insertion of \\hypersetup{...} in the preamble."
337 :group
'org-export-latex
343 (defcustom org-latex-format-headline-function
344 'org-latex-format-headline-default-function
345 "Function for formatting the headline's text.
347 This function will be called with 5 arguments:
348 TODO the todo keyword (string or nil).
349 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
350 PRIORITY the priority of the headline (integer or nil)
351 TEXT the main headline text (string).
352 TAGS the tags as a list of strings (list of strings or nil).
354 The function result will be used in the section format string.
356 Use `org-latex-format-headline-default-function' by default,
357 which format headlines like for Org version prior to 8.0."
358 :group
'org-export-latex
360 :package-version
'(Org .
"8.0")
366 (defcustom org-latex-footnote-separator
"\\textsuperscript{,}\\,"
367 "Text used to separate footnotes."
368 :group
'org-export-latex
374 (defcustom org-latex-active-timestamp-format
"\\textit{%s}"
375 "A printf format string to be applied to active timestamps."
376 :group
'org-export-latex
379 (defcustom org-latex-inactive-timestamp-format
"\\textit{%s}"
380 "A printf format string to be applied to inactive timestamps."
381 :group
'org-export-latex
384 (defcustom org-latex-diary-timestamp-format
"\\textit{%s}"
385 "A printf format string to be applied to diary timestamps."
386 :group
'org-export-latex
392 (defcustom org-latex-image-default-option
""
393 "Default option for images."
394 :group
'org-export-latex
396 :package-version
'(Org .
"8.0")
399 (defcustom org-latex-image-default-width
".9\\linewidth"
400 "Default width for images.
401 This value will not be used if a height is provided."
402 :group
'org-export-latex
404 :package-version
'(Org .
"8.0")
407 (defcustom org-latex-image-default-height
""
408 "Default height for images.
409 This value will not be used if a width is provided, or if the
410 image is wrapped within a \"figure\" or \"wrapfigure\"
412 :group
'org-export-latex
414 :package-version
'(Org .
"8.0")
417 (defcustom org-latex-default-figure-position
"htb"
418 "Default position for latex figures."
419 :group
'org-export-latex
422 (defcustom org-latex-inline-image-rules
423 '(("file" .
"\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\)\\'"))
424 "Rules characterizing image files that can be inlined into LaTeX.
426 A rule consists in an association whose key is the type of link
427 to consider, and value is a regexp that will be matched against
430 Note that, by default, the image extension *actually* allowed
431 depend on the way the LaTeX file is processed. When used with
432 pdflatex, pdf, jpg and png images are OK. When processing
433 through dvi to Postscript, only ps and eps are allowed. The
434 default we use here encompasses both."
435 :group
'org-export-latex
437 :package-version
'(Org .
"8.0")
438 :type
'(alist :key-type
(string :tag
"Type")
439 :value-type
(regexp :tag
"Path")))
441 (defcustom org-latex-link-with-unknown-path-format
"\\texttt{%s}"
442 "Format string for links with unknown path type."
443 :group
'org-export-latex
449 (defcustom org-latex-default-table-environment
"tabular"
450 "Default environment used to build tables."
451 :group
'org-export-latex
453 :package-version
'(Org .
"8.0")
456 (defcustom org-latex-default-table-mode
'table
457 "Default mode for tables.
459 Value can be a symbol among:
461 `table' Regular LaTeX table.
463 `math' In this mode, every cell is considered as being in math
464 mode and the complete table will be wrapped within a math
465 environment. It is particularly useful to write matrices.
467 `inline-math' This mode is almost the same as `math', but the
468 math environment will be inlined.
470 `verbatim' The table is exported as it appears in the Org
471 buffer, within a verbatim environment.
473 This value can be overridden locally with, i.e. \":mode math\" in
476 When modifying this variable, it may be useful to change
477 `org-latex-default-table-environment' accordingly."
478 :group
'org-export-latex
480 :package-version
'(Org .
"8.0")
481 :type
'(choice (const :tag
"Table" table
)
482 (const :tag
"Matrix" math
)
483 (const :tag
"Inline matrix" inline-math
)
484 (const :tag
"Verbatim" verbatim
)))
486 (defcustom org-latex-tables-centered t
487 "When non-nil, tables are exported in a center environment."
488 :group
'org-export-latex
491 (defcustom org-latex-tables-booktabs nil
492 "When non-nil, display tables in a formal \"booktabs\" style.
493 This option assumes that the \"booktabs\" package is properly
494 loaded in the header of the document. This value can be ignored
495 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
497 :group
'org-export-latex
499 :package-version
'(Org .
"8.0")
502 (defcustom org-latex-table-caption-above t
503 "When non-nil, place caption string at the beginning of the table.
504 Otherwise, place it near the end."
505 :group
'org-export-latex
508 (defcustom org-latex-table-scientific-notation
"%s\\,(%s)"
509 "Format string to display numbers in scientific notation.
510 The format should have \"%s\" twice, for mantissa and exponent
511 \(i.e., \"%s\\\\times10^{%s}\").
513 When nil, no transformation is made."
514 :group
'org-export-latex
516 :package-version
'(Org .
"8.0")
518 (string :tag
"Format string")
519 (const :tag
"No formatting")))
524 (defcustom org-latex-text-markup-alist
'((bold .
"\\textbf{%s}")
526 (italic .
"\\emph{%s}")
527 (strike-through .
"\\sout{%s}")
528 (underline .
"\\uline{%s}")
529 (verbatim . protectedtexttt
))
530 "Alist of LaTeX expressions to convert text markup.
532 The key must be a symbol among `bold', `code', `italic',
533 `strike-through', `underline' and `verbatim'. The value is
534 a formatting string to wrap fontified text with.
536 Value can also be set to the following symbols: `verb' and
537 `protectedtexttt'. For the former, Org will use \"\\verb\" to
538 create a format string and select a delimiter character that
539 isn't in the string. For the latter, Org will use \"\\texttt\"
540 to typeset and try to protect special characters.
542 If no association can be found for a given markup, text will be
544 :group
'org-export-latex
546 :options
'(bold code italic strike-through underline verbatim
))
551 (defcustom org-latex-format-drawer-function nil
552 "Function called to format a drawer in LaTeX code.
554 The function must accept two parameters:
555 NAME the drawer name, like \"LOGBOOK\"
556 CONTENTS the contents of the drawer.
558 The function should return the string to be exported.
560 For example, the variable could be set to the following function
561 in order to mimic default behaviour:
563 \(defun org-latex-format-drawer-default \(name contents\)
564 \"Format a drawer element for LaTeX export.\"
566 :group
'org-export-latex
572 (defcustom org-latex-format-inlinetask-function nil
573 "Function called to format an inlinetask in LaTeX code.
575 The function must accept six parameters:
576 TODO the todo keyword, as a string
577 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
578 PRIORITY the inlinetask priority, as a string
579 NAME the inlinetask name, as a string.
580 TAGS the inlinetask tags, as a list of strings.
581 CONTENTS the contents of the inlinetask, as a string.
583 The function should return the string to be exported.
585 For example, the variable could be set to the following function
586 in order to mimic default behaviour:
588 \(defun org-latex-format-inlinetask \(todo type priority name tags contents\)
589 \"Format an inline task element for LaTeX export.\"
593 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
594 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
597 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
598 \(mapconcat 'identity tags \":\")))))
599 \(format (concat \"\\\\begin{center}\\n\"
601 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
603 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
605 \"\\\\end{minipage}}\"
607 full-title contents))"
608 :group
'org-export-latex
614 (defcustom org-latex-listings nil
615 "Non-nil means export source code using the listings package.
617 This package will fontify source code, possibly even with color.
618 If you want to use this, you also need to make LaTeX use the
619 listings package, and if you want to have color, the color
620 package. Just add these to `org-latex-packages-alist', for
621 example using customize, or with something like:
624 \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
625 \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
629 \(setq org-latex-listings 'minted)
631 causes source code to be exported using the minted package as
632 opposed to listings. If you want to use minted, you need to add
633 the minted package to `org-latex-packages-alist', for example
634 using customize, or with
637 \(add-to-list 'org-latex-packages-alist '(\"\" \"minted\"))
639 In addition, it is necessary to install pygments
640 \(http://pygments.org), and to configure the variable
641 `org-latex-pdf-process' so that the -shell-escape option is
644 The minted choice has possible repercussions on the preview of
645 latex fragments (see `org-preview-latex-fragment'). If you run
646 into previewing problems, please consult
648 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
649 :group
'org-export-latex
651 (const :tag
"Use listings" t
)
652 (const :tag
"Use minted" 'minted
)
653 (const :tag
"Export verbatim" nil
)))
655 (defcustom org-latex-listings-langs
656 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
659 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
660 (html "HTML") (xml "XML")
661 (tex "TeX") (latex "TeX")
662 (shell-script "bash")
664 (ocaml "Caml") (caml "Caml")
665 (sql "SQL") (sqlite "sql"))
666 "Alist mapping languages to their listing language counterpart.
667 The key is a symbol, the major mode symbol without the \"-mode\".
668 The value is the string that should be inserted as the language
669 parameter for the listings package. If the mode name and the
670 listings name are the same, the language does not need an entry
671 in this list - but it does not hurt if it is present."
672 :group
'org-export-latex
675 (symbol :tag
"Major mode ")
676 (string :tag
"Listings language"))))
678 (defcustom org-latex-listings-options nil
679 "Association list of options for the latex listings package.
681 These options are supplied as a comma-separated list to the
682 \\lstset command. Each element of the association list should be
683 a list containing two strings: the name of the option, and the
686 (setq org-latex-listings-options
687 '((\"basicstyle\" \"\\small\")
688 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
690 will typeset the code in a small size font with underlined, bold
693 Note that the same options will be applied to blocks of all
695 :group
'org-export-latex
698 (string :tag
"Listings option name ")
699 (string :tag
"Listings option value"))))
701 (defcustom org-latex-minted-langs
702 '((emacs-lisp "common-lisp")
705 (shell-script "bash")
707 "Alist mapping languages to their minted language counterpart.
708 The key is a symbol, the major mode symbol without the \"-mode\".
709 The value is the string that should be inserted as the language
710 parameter for the minted package. If the mode name and the
711 listings name are the same, the language does not need an entry
712 in this list - but it does not hurt if it is present.
714 Note that minted uses all lower case for language identifiers,
715 and that the full list of language identifiers can be obtained
718 pygmentize -L lexers"
719 :group
'org-export-latex
722 (symbol :tag
"Major mode ")
723 (string :tag
"Minted language"))))
725 (defcustom org-latex-minted-options nil
726 "Association list of options for the latex minted package.
728 These options are supplied within square brackets in
729 \\begin{minted} environments. Each element of the alist should
730 be a list containing two strings: the name of the option, and the
733 \(setq org-latex-minted-options
734 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
736 will result in src blocks being exported with
738 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
740 as the start of the minted environment. Note that the same
741 options will be applied to blocks of all languages."
742 :group
'org-export-latex
745 (string :tag
"Minted option name ")
746 (string :tag
"Minted option value"))))
748 (defcustom org-latex-long-listings nil
749 "When non-nil no listing will be wrapped within a float.
751 Removing floats may break some functionalities. For example, it
752 will be impossible to use cross-references to listings when using
753 `minted' set-up when this variable is non-nil.
755 This value can be locally ignored with \":long-listing t\" and
756 \":long-listing nil\" LaTeX attributes."
757 :group
'org-export-latex
759 :package-version
'(Org .
"8.0")
762 (defvar org-latex-custom-lang-environments nil
763 "Alist mapping languages to language-specific LaTeX environments.
765 It is used during export of src blocks by the listings and minted
766 latex packages. For example,
768 \(setq org-latex-custom-lang-environments
769 '\(\(python \"pythoncode\"\)\)\)
771 would have the effect that if org encounters begin_src python
772 during latex export it will output
781 (defcustom org-latex-pdf-process
782 '("pdflatex -interaction nonstopmode -output-directory %o %f"
783 "pdflatex -interaction nonstopmode -output-directory %o %f"
784 "pdflatex -interaction nonstopmode -output-directory %o %f")
785 "Commands to process a LaTeX file to a PDF file.
786 This is a list of strings, each of them will be given to the
787 shell as a command. %f in the command will be replaced by the
788 full file name, %b by the file base name (i.e. without directory
789 and extension parts) and %o by the base directory of the file.
791 The reason why this is a list is that it usually takes several
792 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
793 does not have a clever mechanism to detect which of these
794 commands have to be run to get to a stable result, and it also
795 does not do any error checking.
797 By default, Org uses 3 runs of `pdflatex' to do the processing.
798 If you have texi2dvi on your system and if that does not cause
799 the infamous egrep/locale bug:
801 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
803 then `texi2dvi' is the superior choice. Org does offer it as one
804 of the customize options.
806 Alternatively, this may be a Lisp function that does the
807 processing, so you could use this to apply the machinery of
808 AUCTeX or the Emacs LaTeX mode. This function should accept the
809 file name as its single argument."
810 :group
'org-export-pdf
812 (repeat :tag
"Shell command sequence"
813 (string :tag
"Shell command"))
814 (const :tag
"2 runs of pdflatex"
815 ("pdflatex -interaction nonstopmode -output-directory %o %f"
816 "pdflatex -interaction nonstopmode -output-directory %o %f"))
817 (const :tag
"3 runs of pdflatex"
818 ("pdflatex -interaction nonstopmode -output-directory %o %f"
819 "pdflatex -interaction nonstopmode -output-directory %o %f"
820 "pdflatex -interaction nonstopmode -output-directory %o %f"))
821 (const :tag
"pdflatex,bibtex,pdflatex,pdflatex"
822 ("pdflatex -interaction nonstopmode -output-directory %o %f"
824 "pdflatex -interaction nonstopmode -output-directory %o %f"
825 "pdflatex -interaction nonstopmode -output-directory %o %f"))
826 (const :tag
"2 runs of xelatex"
827 ("xelatex -interaction nonstopmode -output-directory %o %f"
828 "xelatex -interaction nonstopmode -output-directory %o %f"))
829 (const :tag
"3 runs of xelatex"
830 ("xelatex -interaction nonstopmode -output-directory %o %f"
831 "xelatex -interaction nonstopmode -output-directory %o %f"
832 "xelatex -interaction nonstopmode -output-directory %o %f"))
833 (const :tag
"xelatex,bibtex,xelatex,xelatex"
834 ("xelatex -interaction nonstopmode -output-directory %o %f"
836 "xelatex -interaction nonstopmode -output-directory %o %f"
837 "xelatex -interaction nonstopmode -output-directory %o %f"))
838 (const :tag
"texi2dvi"
839 ("texi2dvi -p -b -V %f"))
841 ("rubber -d --into %o %f"))
844 (defcustom org-latex-logfiles-extensions
845 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
846 "The list of file extensions to consider as LaTeX logfiles.
847 The logfiles will be remove if `org-latex-remove-logfiles' is
849 :group
'org-export-latex
850 :type
'(repeat (string :tag
"Extension")))
852 (defcustom org-latex-remove-logfiles t
853 "Non-nil means remove the logfiles produced by PDF production.
854 By default, logfiles are files with these extensions: .aux, .idx,
855 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
856 logfiles to remove, set `org-latex-logfiles-extensions'."
857 :group
'org-export-latex
860 (defcustom org-latex-known-errors
861 '(("Reference.*?undefined" .
"[undefined reference]")
862 ("Citation.*?undefined" .
"[undefined citation]")
863 ("Undefined control sequence" .
"[undefined control sequence]")
864 ("^! LaTeX.*?Error" .
"[LaTeX error]")
865 ("^! Package.*?Error" .
"[package error]")
866 ("Runaway argument" .
"Runaway argument"))
867 "Alist of regular expressions and associated messages for the user.
868 The regular expressions are used to find possible errors in the
870 :group
'org-export-latex
872 :package-version
'(Org .
"8.0")
875 (string :tag
"Regexp")
876 (string :tag
"Message"))))
880 ;;; Internal Functions
882 (defun org-latex--caption/label-string
(element info
)
883 "Return caption and label LaTeX string for ELEMENT.
885 INFO is a plist holding contextual information. If there's no
886 caption nor label, return the empty string.
888 For non-floats, see `org-latex--wrap-label'."
889 (let* ((label (org-element-property :name element
))
890 (label-str (if (not (org-string-nw-p label
)) ""
891 (format "\\label{%s}"
892 (org-export-solidify-link-text label
))))
893 (main (org-export-get-caption element
))
894 (short (org-export-get-caption element t
)))
896 ((and (not main
) (equal label-str
"")) "")
897 ((not main
) (concat label-str
"\n"))
898 ;; Option caption format with short name.
899 (short (format "\\caption[%s]{%s%s}\n"
900 (org-export-data short info
)
902 (org-export-data main info
)))
903 ;; Standard caption format.
904 (t (format "\\caption{%s%s}\n" label-str
(org-export-data main info
))))))
906 (defun org-latex-guess-inputenc (header)
907 "Set the coding system in inputenc to what the buffer is.
909 HEADER is the LaTeX header string. This function only applies
910 when specified inputenc option is \"AUTO\".
912 Return the new header, as a string."
913 (let* ((cs (or (ignore-errors
914 (latexenc-coding-system-to-inputenc
915 (or org-export-coding-system buffer-file-coding-system
)))
918 ;; First translate if that is requested.
919 (setq cs
(or (cdr (assoc cs org-latex-inputenc-alist
)) cs
))
920 ;; Then find the \usepackage statement and replace the option.
921 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
922 cs header t nil
1))))
924 (defun org-latex-guess-babel-language (header info
)
925 "Set Babel's language according to LANGUAGE keyword.
927 HEADER is the LaTeX header string. INFO is the plist used as
928 a communication channel.
930 Insertion of guessed language only happens when Babel package has
931 explicitly been loaded. Then it is added to the rest of
934 Return the new header."
935 (let ((language-code (plist-get info
:language
)))
936 ;; If no language is set or Babel package is not loaded, return
938 (if (or (not (stringp language-code
))
939 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header
)))
941 (let ((options (save-match-data
942 (org-split-string (match-string 1 header
) ",")))
943 (language (cdr (assoc language-code
944 org-latex-babel-language-alist
))))
945 ;; If LANGUAGE is already loaded, return header. Otherwise,
946 ;; append LANGUAGE to other options.
947 (if (member language options
) header
948 (replace-match (mapconcat 'identity
949 (append options
(list language
))
951 nil nil header
1))))))
953 (defun org-latex--find-verb-separator (s)
954 "Return a character not used in string S.
955 This is used to choose a separator for constructs like \\verb."
956 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
957 (loop for c across ll
958 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
959 return
(char-to-string c
))))
961 (defun org-latex--make-option-string (options)
962 "Return a comma separated string of keywords and values.
963 OPTIONS is an alist where the key is the options keyword as
964 a string, and the value a list containing the keyword value, or
966 (mapconcat (lambda (pair)
968 (when (> (length (second pair
)) 0)
969 (concat "=" (second pair
)))))
973 (defun org-latex--wrap-label (element output
)
974 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
975 This function shouldn't be used for floats. See
976 `org-latex--caption/label-string'."
977 (let ((label (org-element-property :name element
)))
978 (if (not (and (org-string-nw-p output
) (org-string-nw-p label
))) output
979 (concat (format "\\label{%s}\n" (org-export-solidify-link-text label
))
982 (defun org-latex--text-markup (text markup
)
983 "Format TEXT depending on MARKUP text markup.
984 See `org-latex-text-markup-alist' for details."
985 (let ((fmt (cdr (assq markup org-latex-text-markup-alist
))))
987 ;; No format string: Return raw text.
989 ;; Handle the `verb' special case: Find and appropriate separator
990 ;; and use "\\verb" command.
992 (let ((separator (org-latex--find-verb-separator text
)))
993 (concat "\\verb" separator text separator
)))
994 ;; Handle the `protectedtexttt' special case: Protect some
995 ;; special chars and use "\texttt{%s}" format string.
996 ((eq 'protectedtexttt fmt
)
998 (trans '(("\\" .
"\\textbackslash{}")
999 ("~" .
"\\textasciitilde{}")
1000 ("^" .
"\\textasciicircum{}")))
1003 (while (string-match "[\\{}$%&_#~^]" text
)
1004 (setq char
(match-string 0 text
))
1005 (if (> (match-beginning 0) 0)
1006 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
1007 (setq text
(substring text
(1+ (match-beginning 0))))
1008 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
1009 rtn
(concat rtn char
)))
1010 (setq text
(concat rtn text
)
1012 (while (string-match "--" text
)
1013 (setq text
(replace-match "-{}-" t t text
)))
1015 ;; Else use format string.
1016 (t (format fmt text
)))))
1018 (defun org-latex--delayed-footnotes-definitions (element info
)
1019 "Return footnotes definitions in ELEMENT as a string.
1021 INFO is a plist used as a communication channel.
1023 Footnotes definitions are returned within \"\\footnotetxt{}\"
1026 This function is used within constructs that don't support
1027 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1028 \"\\footnotemark\" is used within the construct and the function
1029 just outside of it."
1033 "\\footnotetext[%s]{%s}"
1034 (org-export-get-footnote-number ref info
)
1037 (org-export-get-footnote-definition ref info
) info
))))
1038 ;; Find every footnote reference in ELEMENT.
1040 search-refs
; For byte-compiler.
1044 ;; Return a list of all footnote references never seen
1046 (org-element-map data
'footnote-reference
1048 (when (org-export-footnote-first-reference-p ref info
)
1050 (when (eq (org-element-property :type ref
) 'standard
)
1051 (funcall search-refs
1052 (org-export-get-footnote-definition ref info
)))))
1054 (reverse all-refs
)))))
1055 (funcall search-refs element
))
1062 (defun org-latex-template (contents info
)
1063 "Return complete document string after LaTeX conversion.
1064 CONTENTS is the transcoded contents string. INFO is a plist
1065 holding export options."
1066 (let ((title (org-export-data (plist-get info
:title
) info
)))
1069 (and (plist-get info
:time-stamp-file
)
1070 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1071 ;; Document class and packages.
1072 (let* ((class (plist-get info
:latex-class
))
1073 (class-options (plist-get info
:latex-class-options
))
1074 (header (nth 1 (assoc class org-latex-classes
)))
1075 (document-class-string
1076 (and (stringp header
)
1077 (if (not class-options
) header
1078 (replace-regexp-in-string
1079 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1080 class-options header t nil
1)))))
1081 (if (not document-class-string
)
1082 (user-error "Unknown LaTeX class `%s'" class
)
1083 (org-latex-guess-babel-language
1084 (org-latex-guess-inputenc
1085 (org-element-normalize-string
1086 (org-splice-latex-header
1087 document-class-string
1088 org-latex-default-packages-alist
1089 org-latex-packages-alist nil
1090 (concat (org-element-normalize-string
1091 (plist-get info
:latex-header
))
1092 (plist-get info
:latex-header-extra
)))))
1094 ;; Possibly limit depth for headline numbering.
1095 (let ((sec-num (plist-get info
:section-numbers
)))
1096 (when (integerp sec-num
)
1097 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
1099 (let ((author (and (plist-get info
:with-author
)
1100 (let ((auth (plist-get info
:author
)))
1101 (and auth
(org-export-data auth info
)))))
1102 (email (and (plist-get info
:with-email
)
1103 (org-export-data (plist-get info
:email
) info
))))
1104 (cond ((and author email
(not (string= "" email
)))
1105 (format "\\author{%s\\thanks{%s}}\n" author email
))
1106 ((or author email
) (format "\\author{%s}\n" (or author email
)))))
1108 (let ((date (and (plist-get info
:with-date
) (org-export-get-date info
))))
1109 (format "\\date{%s}\n" (org-export-data date info
)))
1111 (format "\\title{%s}\n" title
)
1112 ;; Hyperref options.
1113 (when (plist-get info
:latex-hyperref-p
)
1114 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1115 (or (plist-get info
:keywords
) "")
1116 (or (plist-get info
:description
) "")
1117 (if (not (plist-get info
:with-creator
)) ""
1118 (plist-get info
:creator
))))
1120 "\\begin{document}\n\n"
1122 (org-element-normalize-string
1123 (cond ((string= "" title
) nil
)
1124 ((not (stringp org-latex-title-command
)) nil
)
1125 ((string-match "\\(?:[^%]\\|^\\)%s"
1126 org-latex-title-command
)
1127 (format org-latex-title-command title
))
1128 (t org-latex-title-command
)))
1129 ;; Table of contents.
1130 (let ((depth (plist-get info
:with-toc
)))
1132 (concat (when (wholenump depth
)
1133 (format "\\setcounter{tocdepth}{%d}\n" depth
))
1134 org-latex-toc-command
)))
1138 (let ((creator-info (plist-get info
:with-creator
)))
1140 ((not creator-info
) "")
1141 ((eq creator-info
'comment
)
1142 (format "%% %s\n" (plist-get info
:creator
)))
1143 (t (concat (plist-get info
:creator
) "\n"))))
1145 "\\end{document}")))
1149 ;;; Transcode Functions
1153 (defun org-latex-bold (bold contents info
)
1154 "Transcode BOLD from Org to LaTeX.
1155 CONTENTS is the text with bold markup. INFO is a plist holding
1156 contextual information."
1157 (org-latex--text-markup contents
'bold
))
1162 (defun org-latex-center-block (center-block contents info
)
1163 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1164 CONTENTS holds the contents of the center block. INFO is a plist
1165 holding contextual information."
1166 (org-latex--wrap-label
1168 (format "\\begin{center}\n%s\\end{center}" contents
)))
1173 (defun org-latex-clock (clock contents info
)
1174 "Transcode a CLOCK element from Org to LaTeX.
1175 CONTENTS is nil. INFO is a plist holding contextual
1179 (format "\\textbf{%s} " org-clock-string
)
1180 (format org-latex-inactive-timestamp-format
1181 (concat (org-translate-time
1182 (org-element-property :raw-value
1183 (org-element-property :value clock
)))
1184 (let ((time (org-element-property :duration clock
)))
1185 (and time
(format " (%s)" time
)))))
1191 (defun org-latex-code (code contents info
)
1192 "Transcode a CODE object from Org to LaTeX.
1193 CONTENTS is nil. INFO is a plist used as a communication
1195 (org-latex--text-markup (org-element-property :value code
) 'code
))
1200 (defun org-latex-drawer (drawer contents info
)
1201 "Transcode a DRAWER element from Org to LaTeX.
1202 CONTENTS holds the contents of the block. INFO is a plist
1203 holding contextual information."
1204 (let* ((name (org-element-property :drawer-name drawer
))
1205 (output (if (functionp org-latex-format-drawer-function
)
1206 (funcall org-latex-format-drawer-function
1208 ;; If there's no user defined function: simply
1209 ;; display contents of the drawer.
1211 (org-latex--wrap-label drawer output
)))
1216 (defun org-latex-dynamic-block (dynamic-block contents info
)
1217 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1218 CONTENTS holds the contents of the block. INFO is a plist
1219 holding contextual information. See `org-export-data'."
1220 (org-latex--wrap-label dynamic-block contents
))
1225 (defun org-latex-entity (entity contents info
)
1226 "Transcode an ENTITY object from Org to LaTeX.
1227 CONTENTS are the definition itself. INFO is a plist holding
1228 contextual information."
1229 (let ((ent (org-element-property :latex entity
)))
1230 (if (org-element-property :latex-math-p entity
) (format "$%s$" ent
) ent
)))
1235 (defun org-latex-example-block (example-block contents info
)
1236 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1237 CONTENTS is nil. INFO is a plist holding contextual
1239 (when (org-string-nw-p (org-element-property :value example-block
))
1240 (org-latex--wrap-label
1242 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1243 (org-export-format-code-default example-block info
)))))
1248 (defun org-latex-export-block (export-block contents info
)
1249 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1250 CONTENTS is nil. INFO is a plist holding contextual information."
1251 (when (member (org-element-property :type export-block
) '("LATEX" "TEX"))
1252 (org-remove-indentation (org-element-property :value export-block
))))
1257 (defun org-latex-export-snippet (export-snippet contents info
)
1258 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1259 CONTENTS is nil. INFO is a plist holding contextual information."
1260 (when (eq (org-export-snippet-backend export-snippet
) 'latex
)
1261 (org-element-property :value export-snippet
)))
1266 (defun org-latex-fixed-width (fixed-width contents info
)
1267 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1268 CONTENTS is nil. INFO is a plist holding contextual information."
1269 (org-latex--wrap-label
1271 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1272 (org-remove-indentation
1273 (org-element-property :value fixed-width
)))))
1276 ;;;; Footnote Reference
1278 (defun org-latex-footnote-reference (footnote-reference contents info
)
1279 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1280 CONTENTS is nil. INFO is a plist holding contextual information."
1282 ;; Insert separator between two footnotes in a row.
1283 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1284 (when (eq (org-element-type prev
) 'footnote-reference
)
1285 org-latex-footnote-separator
))
1287 ;; Use \footnotemark if the footnote has already been defined.
1288 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1289 (format "\\footnotemark[%s]{}"
1290 (org-export-get-footnote-number footnote-reference info
)))
1291 ;; Use \footnotemark if reference is within another footnote
1292 ;; reference, footnote definition or table cell.
1293 ((loop for parent in
(org-export-get-genealogy footnote-reference
)
1294 thereis
(memq (org-element-type parent
)
1295 '(footnote-reference footnote-definition table-cell
)))
1297 ;; Otherwise, define it with \footnote command.
1299 (let ((def (org-export-get-footnote-definition footnote-reference info
)))
1301 (format "\\footnote{%s}" (org-trim (org-export-data def info
)))
1302 ;; Retrieve all footnote references within the footnote and
1303 ;; add their definition after it, since LaTeX doesn't support
1305 (org-latex--delayed-footnotes-definitions def info
)))))))
1310 (defun org-latex-headline (headline contents info
)
1311 "Transcode a HEADLINE element from Org to LaTeX.
1312 CONTENTS holds the contents of the headline. INFO is a plist
1313 holding contextual information."
1314 (unless (org-element-property :footnote-section-p headline
)
1315 (let* ((class (plist-get info
:latex-class
))
1316 (level (org-export-get-relative-level headline info
))
1317 (numberedp (org-export-numbered-headline-p headline info
))
1318 (class-sectionning (assoc class org-latex-classes
))
1319 ;; Section formatting will set two placeholders: one for
1320 ;; the title and the other for the contents.
1322 (let ((sec (if (functionp (nth 2 class-sectionning
))
1323 (funcall (nth 2 class-sectionning
) level numberedp
)
1324 (nth (1+ level
) class-sectionning
))))
1326 ;; No section available for that LEVEL.
1328 ;; Section format directly returned by a function. Add
1329 ;; placeholder for contents.
1330 ((stringp sec
) (concat sec
"\n%s"))
1331 ;; (numbered-section . unnumbered-section)
1332 ((not (consp (cdr sec
)))
1333 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
1334 ;; (numbered-open numbered-close)
1336 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
1337 ;; (num-in num-out no-num-in no-num-out)
1339 (if numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))
1340 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
1341 (text (org-export-data (org-element-property :title headline
) info
))
1343 (and (plist-get info
:with-todo-keywords
)
1344 (let ((todo (org-element-property :todo-keyword headline
)))
1345 (and todo
(org-export-data todo info
)))))
1346 (todo-type (and todo
(org-element-property :todo-type headline
)))
1347 (tags (and (plist-get info
:with-tags
)
1348 (org-export-get-tags headline info
)))
1349 (priority (and (plist-get info
:with-priority
)
1350 (org-element-property :priority headline
)))
1351 ;; Create the headline text along with a no-tag version.
1352 ;; The latter is required to remove tags from toc.
1353 (full-text (funcall org-latex-format-headline-function
1354 todo todo-type priority text tags
))
1355 ;; Associate \label to the headline for internal links.
1357 (format "\\label{sec-%s}\n"
1358 (mapconcat 'number-to-string
1359 (org-export-get-headline-number headline info
)
1362 (make-string (org-element-property :pre-blank headline
) 10)))
1363 (if (or (not section-fmt
) (org-export-low-level-p headline info
))
1364 ;; This is a deep sub-tree: export it as a list item. Also
1365 ;; export as items headlines for which no section format has
1367 (let ((low-level-body
1369 ;; If headline is the first sibling, start a list.
1370 (when (org-export-first-sibling-p headline info
)
1371 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
1373 "\\item " full-text
"\n" headline-label pre-blanks contents
)))
1374 ;; If headline is not the last sibling simply return
1375 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1377 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1378 (replace-regexp-in-string
1380 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
1382 ;; This is a standard headline. Export it as a section. Add
1383 ;; an alternative heading when possible, and when this is not
1384 ;; identical to the usual heading.
1386 (funcall org-latex-format-headline-function
1387 todo todo-type priority
1389 (org-export-get-alt-title headline info
) info
)
1390 (and (eq (plist-get info
:with-tags
) t
) tags
))))
1391 (if (and numberedp opt-title
1392 (not (equal opt-title full-text
))
1393 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt
))
1394 (format (replace-match "\\1[%s]" nil nil section-fmt
1)
1395 ;; Replace square brackets with parenthesis
1396 ;; since square brackets are not supported in
1397 ;; optional arguments.
1398 (replace-regexp-in-string
1399 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title
))
1401 (concat headline-label pre-blanks contents
))
1402 ;; Impossible to add an alternative heading. Fallback to
1403 ;; regular sectioning format string.
1404 (format section-fmt full-text
1405 (concat headline-label pre-blanks contents
))))))))
1407 (defun org-latex-format-headline-default-function
1408 (todo todo-type priority text tags
)
1409 "Default format function for a headline.
1410 See `org-latex-format-headline-function' for details."
1412 (and todo
(format "{\\bfseries\\sffamily %s} " todo
))
1413 (and priority
(format "\\framebox{\\#%c} " priority
))
1416 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags
":")))))
1419 ;;;; Horizontal Rule
1421 (defun org-latex-horizontal-rule (horizontal-rule contents info
)
1422 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1423 CONTENTS is nil. INFO is a plist holding contextual information."
1424 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule
))
1425 (prev (org-export-get-previous-element horizontal-rule info
)))
1427 ;; Make sure the rule doesn't start at the end of the current
1428 ;; line by separating it with a blank line from previous element.
1430 (let ((prev-blank (org-element-property :post-blank prev
)))
1431 (or (not prev-blank
) (zerop prev-blank
))))
1433 (org-latex--wrap-label
1435 (format "\\rule{%s}{%s}"
1436 (or (plist-get attr
:width
) "\\linewidth")
1437 (or (plist-get attr
:thickness
) "0.5pt"))))))
1440 ;;;; Inline Src Block
1442 (defun org-latex-inline-src-block (inline-src-block contents info
)
1443 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1444 CONTENTS holds the contents of the item. INFO is a plist holding
1445 contextual information."
1446 (let* ((code (org-element-property :value inline-src-block
))
1447 (separator (org-latex--find-verb-separator code
)))
1449 ;; Do not use a special package: transcode it verbatim.
1450 ((not org-latex-listings
)
1451 (concat "\\verb" separator code separator
))
1452 ;; Use minted package.
1453 ((eq org-latex-listings
'minted
)
1454 (let* ((org-lang (org-element-property :language inline-src-block
))
1455 (mint-lang (or (cadr (assq (intern org-lang
)
1456 org-latex-minted-langs
))
1458 (options (org-latex--make-option-string
1459 org-latex-minted-options
)))
1460 (concat (format "\\mint%s{%s}"
1461 (if (string= options
"") "" (format "[%s]" options
))
1463 separator code separator
)))
1464 ;; Use listings package.
1466 ;; Maybe translate language's name.
1467 (let* ((org-lang (org-element-property :language inline-src-block
))
1468 (lst-lang (or (cadr (assq (intern org-lang
)
1469 org-latex-listings-langs
))
1471 (options (org-latex--make-option-string
1472 (append org-latex-listings-options
1473 `(("language" ,lst-lang
))))))
1474 (concat (format "\\lstinline[%s]" options
)
1475 separator code separator
))))))
1480 (defun org-latex-inlinetask (inlinetask contents info
)
1481 "Transcode an INLINETASK element from Org to LaTeX.
1482 CONTENTS holds the contents of the block. INFO is a plist
1483 holding contextual information."
1484 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1485 (todo (and (plist-get info
:with-todo-keywords
)
1486 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1487 (and todo
(org-export-data todo info
)))))
1488 (todo-type (org-element-property :todo-type inlinetask
))
1489 (tags (and (plist-get info
:with-tags
)
1490 (org-export-get-tags inlinetask info
)))
1491 (priority (and (plist-get info
:with-priority
)
1492 (org-element-property :priority inlinetask
))))
1493 ;; If `org-latex-format-inlinetask-function' is provided, call it
1494 ;; with appropriate arguments.
1495 (if (functionp org-latex-format-inlinetask-function
)
1496 (funcall org-latex-format-inlinetask-function
1497 todo todo-type priority title tags contents
)
1498 ;; Otherwise, use a default template.
1499 (org-latex--wrap-label
1503 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1504 (when priority
(format "\\framebox{\\#%c} " priority
))
1506 (when tags
(format "\\hfill{}\\textsc{:%s:}"
1507 (mapconcat 'identity tags
":"))))))
1508 (format (concat "\\begin{center}\n"
1510 "\\begin{minipage}[c]{.6\\textwidth}\n"
1512 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1517 full-title contents
))))))
1522 (defun org-latex-italic (italic contents info
)
1523 "Transcode ITALIC from Org to LaTeX.
1524 CONTENTS is the text with italic markup. INFO is a plist holding
1525 contextual information."
1526 (org-latex--text-markup contents
'italic
))
1531 (defun org-latex-item (item contents info
)
1532 "Transcode an ITEM element from Org to LaTeX.
1533 CONTENTS holds the contents of the item. INFO is a plist holding
1534 contextual information."
1536 (let ((count (org-element-property :counter item
))
1538 ;; Determine level of current item to determine the
1539 ;; correct LaTeX counter to use (enumi, enumii...).
1540 (let ((parent item
) (level 0))
1541 (while (memq (org-element-type
1542 (setq parent
(org-export-get-parent parent
)))
1544 (when (and (eq (org-element-type parent
) 'plain-list
)
1545 (eq (org-element-property :type parent
)
1551 (format "\\setcounter{enum%s}{%s}\n"
1552 (nth (1- level
) '("i" "ii" "iii" "iv"))
1554 (checkbox (case (org-element-property :checkbox item
)
1555 (on "$\\boxtimes$ ")
1557 (trans "$\\boxminus$ ")))
1558 (tag (let ((tag (org-element-property :tag item
)))
1559 ;; Check-boxes must belong to the tag.
1560 (and tag
(format "[{%s}] "
1562 (org-export-data tag info
)))))))
1563 (concat counter
"\\item" (or tag
(concat " " checkbox
))
1564 (and contents
(org-trim contents
))
1565 ;; If there are footnotes references in tag, be sure to
1566 ;; add their definition at the end of the item. This
1567 ;; workaround is necessary since "\footnote{}" command is
1568 ;; not supported in tags.
1570 (org-latex--delayed-footnotes-definitions
1571 (org-element-property :tag item
) info
)))))
1576 (defun org-latex-keyword (keyword contents info
)
1577 "Transcode a KEYWORD element from Org to LaTeX.
1578 CONTENTS is nil. INFO is a plist holding contextual information."
1579 (let ((key (org-element-property :key keyword
))
1580 (value (org-element-property :value keyword
)))
1582 ((string= key
"LATEX") value
)
1583 ((string= key
"INDEX") (format "\\index{%s}" value
))
1584 ((string= key
"TOC")
1585 (let ((value (downcase value
)))
1587 ((string-match "\\<headlines\\>" value
)
1588 (let ((depth (or (and (string-match "[0-9]+" value
)
1589 (string-to-number (match-string 0 value
)))
1590 (plist-get info
:with-toc
))))
1592 (when (wholenump depth
)
1593 (format "\\setcounter{tocdepth}{%s}\n" depth
))
1594 "\\tableofcontents")))
1595 ((string= "tables" value
) "\\listoftables")
1596 ((string= "listings" value
)
1598 ((eq org-latex-listings
'minted
) "\\listoflistings")
1599 (org-latex-listings "\\lstlistoflistings")
1600 ;; At the moment, src blocks with a caption are wrapped
1601 ;; into a figure environment.
1602 (t "\\listoffigures")))))))))
1605 ;;;; Latex Environment
1607 (defun org-latex-latex-environment (latex-environment contents info
)
1608 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1609 CONTENTS is nil. INFO is a plist holding contextual information."
1610 (when (plist-get info
:with-latex
)
1611 (let ((label (org-element-property :name latex-environment
))
1612 (value (org-remove-indentation
1613 (org-element-property :value latex-environment
))))
1614 (if (not (org-string-nw-p label
)) value
1615 ;; Environment is labelled: label must be within the environment
1616 ;; (otherwise, a reference pointing to that element will count
1617 ;; the section instead).
1620 (goto-char (point-min))
1623 (format "\\label{%s}\n" (org-export-solidify-link-text label
)))
1624 (buffer-string))))))
1629 (defun org-latex-latex-fragment (latex-fragment contents info
)
1630 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1631 CONTENTS is nil. INFO is a plist holding contextual information."
1632 (when (plist-get info
:with-latex
)
1633 (org-element-property :value latex-fragment
)))
1638 (defun org-latex-line-break (line-break contents info
)
1639 "Transcode a LINE-BREAK object from Org to LaTeX.
1640 CONTENTS is nil. INFO is a plist holding contextual information."
1646 (defun org-latex--inline-image (link info
)
1647 "Return LaTeX code for an inline image.
1648 LINK is the link pointing to the inline image. INFO is a plist
1649 used as a communication channel."
1650 (let* ((parent (org-export-get-parent-element link
))
1651 (path (let ((raw-path (org-element-property :path link
)))
1652 (if (not (file-name-absolute-p raw-path
)) raw-path
1653 (expand-file-name raw-path
))))
1654 (filetype (file-name-extension path
))
1655 (caption (org-latex--caption/label-string parent info
))
1656 ;; Retrieve latex attributes from the element around.
1657 (attr (org-export-read-attribute :attr_latex parent
))
1658 (float (let ((float (plist-get attr
:float
)))
1659 (cond ((string= float
"wrap") 'wrap
)
1660 ((string= float
"multicolumn") 'multicolumn
)
1661 ((or (string= float
"figure")
1662 (org-element-property :caption parent
))
1665 (let ((place (plist-get attr
:placement
)))
1666 (cond (place (format "%s" place
))
1667 ((eq float
'wrap
) "{l}{0.5\\textwidth}")
1669 (format "[%s]" org-latex-default-figure-position
))
1671 (comment-include (if (plist-get attr
:comment-include
) "%" ""))
1672 ;; It is possible to specify width and height in the
1673 ;; ATTR_LATEX line, and also via default variables.
1674 (width (cond ((plist-get attr
:width
))
1675 ((plist-get attr
:height
) "")
1676 ((eq float
'wrap
) "0.48\\textwidth")
1677 (t org-latex-image-default-width
)))
1678 (height (cond ((plist-get attr
:height
))
1679 ((or (plist-get attr
:width
)
1680 (memq float
'(figure wrap
))) "")
1681 (t org-latex-image-default-height
)))
1682 (options (let ((opt (or (plist-get attr
:options
)
1683 org-latex-image-default-option
)))
1684 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt
)) opt
1685 (match-string 1 opt
))))
1687 (if (equal filetype
"tikz")
1689 ;; - use \input to read in image file.
1690 ;; - if options are present, wrap in a tikzpicture environment.
1691 ;; - if width or height are present, use \resizebox to change
1694 (setq image-code
(format "\\input{%s}" path
))
1695 (when (org-string-nw-p options
)
1697 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1700 (when (or (org-string-nw-p width
) (org-string-nw-p height
))
1701 (setq image-code
(format "\\resizebox{%s}{%s}{%s}"
1702 (if (org-string-nw-p width
) width
"!")
1703 (if (org-string-nw-p height
) height
"!")
1705 ;; For other images:
1706 ;; - add width and height to options.
1707 ;; - include the image with \includegraphics.
1708 (when (org-string-nw-p width
)
1709 (setq options
(concat options
",width=" width
)))
1710 (when (org-string-nw-p height
)
1711 (setq options
(concat options
",height=" height
)))
1713 (format "\\includegraphics%s{%s}"
1714 (cond ((not (org-string-nw-p options
)) "")
1715 ((= (aref options
0) ?
,)
1716 (format "[%s]"(substring options
1)))
1717 (t (format "[%s]" options
)))
1719 ;; Return proper string, depending on FLOAT.
1721 (wrap (format "\\begin{wrapfigure}%s
1724 %s\\end{wrapfigure}" placement comment-include image-code caption
))
1725 (multicolumn (format "\\begin{figure*}%s
1728 %s\\end{figure*}" placement comment-include image-code caption
))
1729 (figure (format "\\begin{figure}%s
1732 %s\\end{figure}" placement comment-include image-code caption
))
1733 (otherwise image-code
))))
1735 (defun org-latex-link (link desc info
)
1736 "Transcode a LINK object from Org to LaTeX.
1738 DESC is the description part of the link, or the empty string.
1739 INFO is a plist holding contextual information. See
1741 (let* ((type (org-element-property :type link
))
1742 (raw-path (org-element-property :path link
))
1743 ;; Ensure DESC really exists, or set it to nil.
1744 (desc (and (not (string= desc
"")) desc
))
1745 (imagep (org-export-inline-image-p
1746 link org-latex-inline-image-rules
))
1748 ((member type
'("http" "https" "ftp" "mailto"))
1749 (concat type
":" raw-path
))
1750 ((string= type
"file")
1751 (if (not (file-name-absolute-p raw-path
)) raw-path
1752 (concat "file://" (expand-file-name raw-path
))))
1757 (imagep (org-latex--inline-image link info
))
1758 ;; Radio link: Transcode target's contents and use them as link's
1760 ((string= type
"radio")
1761 (let ((destination (org-export-resolve-radio-link link info
)))
1763 (format "\\hyperref[%s]{%s}"
1764 (org-export-solidify-link-text path
)
1765 (org-export-data (org-element-contents destination
) info
)))))
1766 ;; Links pointing to a headline: Find destination and build
1767 ;; appropriate referencing command.
1768 ((member type
'("custom-id" "fuzzy" "id"))
1769 (let ((destination (if (string= type
"fuzzy")
1770 (org-export-resolve-fuzzy-link link info
)
1771 (org-export-resolve-id-link link info
))))
1772 (case (org-element-type destination
)
1773 ;; Id link points to an external file.
1775 (if desc
(format "\\href{%s}{%s}" destination desc
)
1776 (format "\\url{%s}" destination
)))
1777 ;; Fuzzy link points nowhere.
1779 (format org-latex-link-with-unknown-path-format
1782 (org-element-property :raw-link link
) info
))))
1783 ;; LINK points to a headline. If headlines are numbered
1784 ;; and the link has no description, display headline's
1785 ;; number. Otherwise, display description or headline's
1792 (org-export-get-headline-number destination info
)
1794 (if (and (plist-get info
:section-numbers
) (not desc
))
1795 (format "\\ref{%s}" label
)
1796 (format "\\hyperref[%s]{%s}" label
1799 (org-element-property :title destination
) info
))))))
1800 ;; Fuzzy link points to a target. Do as above.
1802 (let ((path (org-export-solidify-link-text path
)))
1803 (if (not desc
) (format "\\ref{%s}" path
)
1804 (format "\\hyperref[%s]{%s}" path desc
)))))))
1805 ;; Coderef: replace link with the reference name or the
1806 ;; equivalent line number.
1807 ((string= type
"coderef")
1808 (format (org-export-get-coderef-format path desc
)
1809 (org-export-resolve-coderef path info
)))
1810 ;; Link type is handled by a special function.
1811 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
1812 (funcall protocol
(org-link-unescape path
) desc
'latex
))
1813 ;; External link with a description part.
1814 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
1815 ;; External link without a description part.
1816 (path (format "\\url{%s}" path
))
1817 ;; No path, only description. Try to do something useful.
1818 (t (format org-latex-link-with-unknown-path-format desc
)))))
1823 (defun org-latex-paragraph (paragraph contents info
)
1824 "Transcode a PARAGRAPH element from Org to LaTeX.
1825 CONTENTS is the contents of the paragraph, as a string. INFO is
1826 the plist used as a communication channel."
1832 (defun org-latex-plain-list (plain-list contents info
)
1833 "Transcode a PLAIN-LIST element from Org to LaTeX.
1834 CONTENTS is the contents of the list. INFO is a plist holding
1835 contextual information."
1836 (let* ((type (org-element-property :type plain-list
))
1837 (attr (org-export-read-attribute :attr_latex plain-list
))
1838 (latex-type (let ((env (plist-get attr
:environment
)))
1839 (cond (env (format "%s" env
))
1840 ((eq type
'ordered
) "enumerate")
1841 ((eq type
'unordered
) "itemize")
1842 ((eq type
'descriptive
) "description")))))
1843 (org-latex--wrap-label
1845 (format "\\begin{%s}%s\n%s\\end{%s}"
1847 ;; Put optional arguments, if any inside square brackets
1849 (let ((options (format "%s" (or (plist-get attr
:options
) ""))))
1850 (cond ((equal options
"") "")
1851 ((string-match "\\`\\[.*\\]\\'" options
) options
)
1852 (t (format "[%s]" options
))))
1859 (defun org-latex-plain-text (text info
)
1860 "Transcode a TEXT string from Org to LaTeX.
1861 TEXT is the string to transcode. INFO is a plist holding
1862 contextual information."
1863 (let ((specialp (plist-get info
:with-special-strings
))
1865 ;; Protect %, #, &, $, _, { and }.
1866 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output
)
1869 (format "\\%s" (match-string 2 output
)) nil t output
2)))
1872 (replace-regexp-in-string
1873 "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil
2))
1874 ;; Protect \. If special strings are used, be careful not to
1875 ;; protect "\" in "\-" constructs.
1876 (let ((symbols (if specialp
"-%$#&{}^_\\" "%$#&{}^_\\")))
1878 (replace-regexp-in-string
1879 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols
)
1880 "$\\backslash$" output nil t
1)))
1883 (replace-regexp-in-string
1884 "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t
2))
1885 ;; Activate smart quotes. Be sure to provide original TEXT string
1886 ;; since OUTPUT may have been modified.
1887 (when (plist-get info
:with-smart-quotes
)
1888 (setq output
(org-export-activate-smart-quotes output
:latex info text
)))
1889 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1890 (let ((case-fold-search nil
)
1892 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start
)
1893 (setq output
(replace-match
1894 (format "\\%s{}" (match-string 1 output
)) nil t output
)
1895 start
(match-end 0))))
1896 ;; Convert special strings.
1899 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t
)))
1900 ;; Handle break preservation if required.
1901 (when (plist-get info
:preserve-breaks
)
1902 (setq output
(replace-regexp-in-string
1903 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output
)))
1910 (defun org-latex-planning (planning contents info
)
1911 "Transcode a PLANNING element from Org to LaTeX.
1912 CONTENTS is nil. INFO is a plist holding contextual
1920 (let ((closed (org-element-property :closed planning
)))
1923 (format "\\textbf{%s} " org-closed-string
)
1924 (format org-latex-inactive-timestamp-format
1926 (org-element-property :raw-value closed
))))))
1927 (let ((deadline (org-element-property :deadline planning
)))
1930 (format "\\textbf{%s} " org-deadline-string
)
1931 (format org-latex-active-timestamp-format
1933 (org-element-property :raw-value deadline
))))))
1934 (let ((scheduled (org-element-property :scheduled planning
)))
1937 (format "\\textbf{%s} " org-scheduled-string
)
1938 (format org-latex-active-timestamp-format
1940 (org-element-property :raw-value scheduled
))))))))
1947 (defun org-latex-quote-block (quote-block contents info
)
1948 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1949 CONTENTS holds the contents of the block. INFO is a plist
1950 holding contextual information."
1951 (org-latex--wrap-label
1953 (format "\\begin{quote}\n%s\\end{quote}" contents
)))
1958 (defun org-latex-quote-section (quote-section contents info
)
1959 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1960 CONTENTS is nil. INFO is a plist holding contextual information."
1961 (let ((value (org-remove-indentation
1962 (org-element-property :value quote-section
))))
1963 (when value
(format "\\begin{verbatim}\n%s\\end{verbatim}" value
))))
1968 (defun org-latex-radio-target (radio-target text info
)
1969 "Transcode a RADIO-TARGET object from Org to LaTeX.
1970 TEXT is the text of the target. INFO is a plist holding
1971 contextual information."
1972 (format "\\label{%s}%s"
1973 (org-export-solidify-link-text
1974 (org-element-property :value radio-target
))
1980 (defun org-latex-section (section contents info
)
1981 "Transcode a SECTION element from Org to LaTeX.
1982 CONTENTS holds the contents of the section. INFO is a plist
1983 holding contextual information."
1989 (defun org-latex-special-block (special-block contents info
)
1990 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1991 CONTENTS holds the contents of the block. INFO is a plist
1992 holding contextual information."
1993 (let ((type (downcase (org-element-property :type special-block
)))
1994 (opt (org-export-read-attribute :attr_latex special-block
:options
)))
1995 (concat (format "\\begin{%s}%s\n" type
(or opt
""))
1996 ;; Insert any label or caption within the block
1997 ;; (otherwise, a reference pointing to that element will
1998 ;; count the section instead).
1999 (org-latex--caption/label-string special-block info
)
2001 (format "\\end{%s}" type
))))
2006 (defun org-latex-src-block (src-block contents info
)
2007 "Transcode a SRC-BLOCK element from Org to LaTeX.
2008 CONTENTS holds the contents of the item. INFO is a plist holding
2009 contextual information."
2010 (when (org-string-nw-p (org-element-property :value src-block
))
2011 (let* ((lang (org-element-property :language src-block
))
2012 (caption (org-element-property :caption src-block
))
2013 (label (org-element-property :name src-block
))
2014 (custom-env (and lang
2015 (cadr (assq (intern lang
)
2016 org-latex-custom-lang-environments
))))
2017 (num-start (case (org-element-property :number-lines src-block
)
2018 (continued (org-export-get-loc src-block info
))
2020 (retain-labels (org-element-property :retain-labels src-block
))
2022 (let ((attr (org-export-read-attribute :attr_latex src-block
)))
2023 (if (plist-member attr
:long-listing
)
2024 (plist-get attr
:long-listing
)
2025 org-latex-long-listings
))))
2027 ;; Case 1. No source fontification.
2028 ((not org-latex-listings
)
2029 (let* ((caption-str (org-latex--caption/label-string src-block info
))
2030 (float-env (and (not long-listing
)
2032 (format "\\begin{figure}[H]\n%s%%s\n\\end{figure}"
2036 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2037 (org-export-format-code-default src-block info
))))))
2038 ;; Case 2. Custom environment.
2039 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2041 (org-export-format-code-default src-block info
)
2043 ;; Case 3. Use minted package.
2044 ((eq org-latex-listings
'minted
)
2046 (and (not long-listing
)
2048 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2049 (org-latex--caption/label-string src-block info
))))
2052 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2054 (org-latex--make-option-string
2055 (if (or (not num-start
)
2056 (assoc "linenos" org-latex-minted-options
))
2057 org-latex-minted-options
2058 (append `(("linenos")
2059 ("firstnumber" ,(number-to-string (1+ num-start
))))
2060 org-latex-minted-options
)))
2062 (or (cadr (assq (intern lang
) org-latex-minted-langs
)) lang
)
2064 (let* ((code-info (org-export-unravel-code src-block
))
2068 (org-split-string (car code-info
)
2070 (org-export-format-code
2072 (lambda (loc num ref
)
2076 ;; Ensure references are flushed to the right,
2077 ;; separated with 6 spaces from the widest line
2079 (concat (make-string (+ (- max-width
(length loc
)) 6)
2081 (format "(%s)" ref
)))))
2082 nil
(and retain-labels
(cdr code-info
)))))))
2084 (if float-env
(format float-env body
) body
)))
2085 ;; Case 4. Use listings package.
2088 (or (cadr (assq (intern lang
) org-latex-listings-langs
)) lang
))
2091 (let ((main (org-export-get-caption src-block
))
2092 (secondary (org-export-get-caption src-block t
)))
2094 (format "{%s}" (org-export-data main info
))
2096 (org-export-data secondary info
)
2097 (org-export-data main info
)))))))
2100 (format "\\lstset{%s}\n"
2101 (org-latex--make-option-string
2103 org-latex-listings-options
2104 `(("language" ,lst-lang
))
2105 (when label
`(("label" ,label
)))
2106 (when caption-str
`(("caption" ,caption-str
)))
2107 (cond ((assoc "numbers" org-latex-listings-options
) nil
)
2108 ((not num-start
) '(("numbers" "none")))
2109 ((zerop num-start
) '(("numbers" "left")))
2110 (t `(("numbers" "left")
2112 ,(number-to-string (1+ num-start
)))))))))
2115 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2116 (let* ((code-info (org-export-unravel-code src-block
))
2120 (org-split-string (car code-info
) "\n")))))
2121 (org-export-format-code
2123 (lambda (loc num ref
)
2127 ;; Ensure references are flushed to the right,
2128 ;; separated with 6 spaces from the widest line of
2130 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2131 (format "(%s)" ref
)))))
2132 nil
(and retain-labels
(cdr code-info
))))))))))))
2135 ;;;; Statistics Cookie
2137 (defun org-latex-statistics-cookie (statistics-cookie contents info
)
2138 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2139 CONTENTS is nil. INFO is a plist holding contextual information."
2140 (replace-regexp-in-string
2141 "%" "\\%" (org-element-property :value statistics-cookie
) nil t
))
2146 (defun org-latex-strike-through (strike-through contents info
)
2147 "Transcode STRIKE-THROUGH from Org to LaTeX.
2148 CONTENTS is the text with strike-through markup. INFO is a plist
2149 holding contextual information."
2150 (org-latex--text-markup contents
'strike-through
))
2155 (defun org-latex--script-size (object info
)
2156 "Transcode a subscript or superscript object.
2157 OBJECT is an Org object. INFO is a plist used as a communication
2160 ;; Non-nil if object is already in a sub/superscript.
2161 (let ((parent object
))
2163 (while (setq parent
(org-export-get-parent parent
))
2164 (let ((type (org-element-type parent
)))
2165 (cond ((memq type
'(subscript superscript
))
2167 ((memq type org-element-all-elements
)
2168 (throw 'exit nil
))))))))
2169 (type (org-element-type object
))
2171 (org-element-map (org-element-contents object
)
2172 (cons 'plain-text org-element-all-objects
)
2174 (case (org-element-type obj
)
2175 ((entity latex-fragment
)
2176 (let ((data (org-trim (org-export-data obj info
))))
2178 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2182 (match-string 1 data
)
2183 (let ((blank (org-element-property :post-blank obj
)))
2184 (and blank
(> blank
0) "\\ "))))))
2187 (format "%s\\text{%s}" output
(org-export-data obj info
))))
2191 (org-export-data obj info
)
2192 (let ((blank (org-element-property :post-blank obj
)))
2193 (and blank
(> blank
0) "\\ ")))))))
2194 info nil org-element-recursive-objects
)
2195 ;; Result. Do not wrap into math mode if already in a subscript
2196 ;; or superscript. Do not wrap into curly brackets if OUTPUT is
2197 ;; a single character. Also merge consecutive subscript and
2198 ;; superscript into the same math snippet.
2199 (concat (and (not in-script-p
)
2200 (let ((prev (org-export-get-previous-element object info
)))
2202 (not (eq (org-element-type prev
)
2203 (if (eq type
'subscript
) 'superscript
2205 (let ((blank (org-element-property :post-blank prev
)))
2206 (and blank
(> blank
0)))))
2208 (if (eq (org-element-type object
) 'subscript
) "_" "^")
2209 (and (> (length output
) 1) "{")
2211 (and (> (length output
) 1) "}")
2212 (and (not in-script-p
)
2213 (or (let ((blank (org-element-property :post-blank object
)))
2214 (and blank
(> blank
0)))
2215 (not (eq (org-element-type
2216 (org-export-get-next-element object info
))
2217 (if (eq type
'subscript
) 'superscript
2221 (defun org-latex-subscript (subscript contents info
)
2222 "Transcode a SUBSCRIPT object from Org to LaTeX.
2223 CONTENTS is the contents of the object. INFO is a plist holding
2224 contextual information."
2225 (org-latex--script-size subscript info
))
2230 (defun org-latex-superscript (superscript contents info
)
2231 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2232 CONTENTS is the contents of the object. INFO is a plist holding
2233 contextual information."
2234 (org-latex--script-size superscript info
))
2239 ;; `org-latex-table' is the entry point for table transcoding. It
2240 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2241 ;; delegates the job to either `org-latex--table.el-table',
2242 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2243 ;; depending of the type of the table and the mode requested.
2245 ;; `org-latex--align-string' is a subroutine used to build alignment
2246 ;; string for Org tables.
2248 (defun org-latex-table (table contents info
)
2249 "Transcode a TABLE element from Org to LaTeX.
2250 CONTENTS is the contents of the table. INFO is a plist holding
2251 contextual information."
2252 (if (eq (org-element-property :type table
) 'table.el
)
2253 ;; "table.el" table. Convert it using appropriate tools.
2254 (org-latex--table.el-table table info
)
2255 (let ((type (or (org-export-read-attribute :attr_latex table
:mode
)
2256 org-latex-default-table-mode
)))
2258 ;; Case 1: Verbatim table.
2259 ((string= type
"verbatim")
2260 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2261 ;; Re-create table, without affiliated keywords.
2262 (org-trim (org-element-interpret-data
2263 `(table nil
,@(org-element-contents table
))))))
2265 ((or (string= type
"math") (string= type
"inline-math"))
2266 (org-latex--math-table table info
))
2267 ;; Case 3: Standard table.
2268 (t (concat (org-latex--org-table table contents info
)
2269 ;; When there are footnote references within the
2270 ;; table, insert their definition just after it.
2271 (org-latex--delayed-footnotes-definitions table info
)))))))
2273 (defun org-latex--align-string (table info
)
2274 "Return an appropriate LaTeX alignment string.
2275 TABLE is the considered table. INFO is a plist used as
2276 a communication channel."
2277 (or (org-export-read-attribute :attr_latex table
:align
)
2279 ;; Extract column groups and alignment from first (non-rule)
2282 (org-element-map table
'table-row
2284 (and (eq (org-element-property :type row
) 'standard
) row
))
2288 (let ((borders (org-export-table-cell-borders cell info
)))
2289 ;; Check left border for the first cell only.
2290 (when (and (memq 'left borders
) (not align
))
2292 (push (case (org-export-table-cell-alignment cell info
)
2297 (when (memq 'right borders
) (push "|" align
))))
2299 (apply 'concat
(nreverse align
)))))
2301 (defun org-latex--org-table (table contents info
)
2302 "Return appropriate LaTeX code for an Org table.
2304 TABLE is the table type element to transcode. CONTENTS is its
2305 contents, as a string. INFO is a plist used as a communication
2308 This function assumes TABLE has `org' as its `:type' property and
2309 `table' as its `:mode' attribute."
2310 (let* ((caption (org-latex--caption/label-string table info
))
2311 (attr (org-export-read-attribute :attr_latex table
))
2312 ;; Determine alignment string.
2313 (alignment (org-latex--align-string table info
))
2314 ;; Determine environment for the table: longtable, tabular...
2315 (table-env (or (plist-get attr
:environment
)
2316 org-latex-default-table-environment
))
2317 ;; If table is a float, determine environment: table, table*
2318 ;; or sidewaystable.
2319 (float-env (unless (member table-env
'("longtable" "longtabu"))
2320 (let ((float (plist-get attr
:float
)))
2322 ((string= float
"sidewaystable") "sidewaystable")
2323 ((string= float
"multicolumn") "table*")
2324 ((or (string= float
"table")
2325 (org-element-property :caption table
))
2327 ;; Extract others display options.
2328 (fontsize (let ((font (plist-get attr
:font
)))
2329 (and font
(concat font
"\n"))))
2330 (width (plist-get attr
:width
))
2331 (spreadp (plist-get attr
:spread
))
2332 (placement (or (plist-get attr
:placement
)
2333 (format "[%s]" org-latex-default-figure-position
)))
2334 (centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2335 org-latex-tables-centered
)))
2336 ;; Prepare the final format string for the table.
2339 ((equal "longtable" table-env
)
2340 (concat (and fontsize
(concat "{" fontsize
))
2341 (format "\\begin{longtable}{%s}\n" alignment
)
2342 (and org-latex-table-caption-above
2343 (org-string-nw-p caption
)
2344 (concat caption
"\\\\\n"))
2346 (and (not org-latex-table-caption-above
)
2347 (org-string-nw-p caption
)
2348 (concat caption
"\\\\\n"))
2349 "\\end{longtable}\n"
2350 (and fontsize
"}")))
2352 ((equal "longtabu" table-env
)
2353 (concat (and fontsize
(concat "{" fontsize
))
2354 (format "\\begin{longtabu}%s{%s}\n"
2357 (if spreadp
"spread" "to") width
) "")
2359 (and org-latex-table-caption-above
2360 (org-string-nw-p caption
)
2361 (concat caption
"\\\\\n"))
2363 (and (not org-latex-table-caption-above
)
2364 (org-string-nw-p caption
)
2365 (concat caption
"\\\\\n"))
2367 (and fontsize
"}")))
2371 (concat (format "\\begin{%s}%s\n" float-env placement
)
2372 (if org-latex-table-caption-above caption
"")
2373 (when centerp
"\\centering\n")
2375 (centerp (concat "\\begin{center}\n" fontsize
))
2376 (fontsize (concat "{" fontsize
)))
2377 (cond ((equal "tabu" table-env
)
2378 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2380 (if spreadp
" spread %s " " to %s ")
2384 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2386 (if width
(format "{%s}" width
) "")
2392 (concat (if org-latex-table-caption-above
"" caption
)
2393 (format "\n\\end{%s}" float-env
)))
2394 (centerp "\n\\end{center}")
2395 (fontsize "}")))))))
2397 (defun org-latex--table.el-table
(table info
)
2398 "Return appropriate LaTeX code for a table.el table.
2400 TABLE is the table type element to transcode. INFO is a plist
2401 used as a communication channel.
2403 This function assumes TABLE has `table.el' as its `:type'
2406 ;; Ensure "*org-export-table*" buffer is empty.
2407 (with-current-buffer (get-buffer-create "*org-export-table*")
2409 (let ((output (with-temp-buffer
2410 (insert (org-element-property :value table
))
2412 (re-search-forward "^[ \t]*|[^|]" nil t
)
2413 (table-generate-source 'latex
"*org-export-table*")
2414 (with-current-buffer "*org-export-table*"
2415 (org-trim (buffer-string))))))
2416 (kill-buffer (get-buffer "*org-export-table*"))
2417 ;; Remove left out comments.
2418 (while (string-match "^%.*\n" output
)
2419 (setq output
(replace-match "" t t output
)))
2420 (let ((attr (org-export-read-attribute :attr_latex table
)))
2421 (when (plist-get attr
:rmlines
)
2422 ;; When the "rmlines" attribute is provided, remove all hlines
2423 ;; but the the one separating heading from the table body.
2424 (let ((n 0) (pos 0))
2425 (while (and (< (length output
) pos
)
2426 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
2428 (unless (= n
2) (setq output
(replace-match "" nil nil output
))))))
2429 (let ((centerp (if (plist-member attr
:center
) (plist-get attr
:center
)
2430 org-latex-tables-centered
)))
2431 (if (not centerp
) output
2432 (format "\\begin{center}\n%s\n\\end{center}" output
))))))
2434 (defun org-latex--math-table (table info
)
2435 "Return appropriate LaTeX code for a matrix.
2437 TABLE is the table type element to transcode. INFO is a plist
2438 used as a communication channel.
2440 This function assumes TABLE has `org' as its `:type' property and
2441 `inline-math' or `math' as its `:mode' attribute.."
2442 (let* ((caption (org-latex--caption/label-string table info
))
2443 (attr (org-export-read-attribute :attr_latex table
))
2444 (inlinep (equal (plist-get attr
:mode
) "inline-math"))
2445 (env (or (plist-get attr
:environment
)
2446 org-latex-default-table-environment
))
2450 ;; Ignore horizontal rules.
2451 (when (eq (org-element-property :type row
) 'standard
)
2452 ;; Return each cell unmodified.
2456 (substring (org-element-interpret-data cell
) 0 -
1))
2457 (org-element-map row
'table-cell
'identity info
) "&")
2458 (or (cdr (assoc env org-latex-table-matrix-macros
)) "\\\\")
2460 (org-element-map table
'table-row
'identity info
) ""))
2461 ;; Variables related to math clusters (contiguous math tables
2462 ;; of the same type).
2463 (mode (org-export-read-attribute :attr_latex table
:mode
))
2464 (prev (org-export-get-previous-element table info
))
2465 (next (org-export-get-next-element table info
))
2468 ;; Non-nil when TABLE has the same mode as current table.
2469 (string= (or (org-export-read-attribute :attr_latex table
:mode
)
2470 org-latex-default-table-mode
)
2473 ;; Opening string. If TABLE is in the middle of a table cluster,
2474 ;; do not insert any.
2476 (eq (org-element-type prev
) 'table
)
2477 (memq (org-element-property :post-blank prev
) '(0 nil
))
2478 (funcall same-mode-p prev
))
2481 ((org-string-nw-p caption
) (concat "\\begin{equation}\n" caption
))
2484 (or (plist-get attr
:math-prefix
) "")
2485 ;; Environment. Also treat special cases.
2486 (cond ((equal env
"array")
2487 (let ((align (org-latex--align-string table info
)))
2488 (format "\\begin{array}{%s}\n%s\\end{array}" align contents
)))
2489 ((assoc env org-latex-table-matrix-macros
)
2490 (format "\\%s%s{\n%s}"
2492 (or (plist-get attr
:math-arguments
) "")
2494 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env
)))
2496 (or (plist-get attr
:math-suffix
) "")
2497 ;; Closing string. If TABLE is in the middle of a table cluster,
2498 ;; do not insert any. If it closes such a cluster, be sure to
2499 ;; close the cluster with a string matching the opening string.
2501 (eq (org-element-type next
) 'table
)
2502 (memq (org-element-property :post-blank table
) '(0 nil
))
2503 (funcall same-mode-p next
))
2506 ;; Find cluster beginning to know which environment to use.
2507 ((let ((cluster-beg table
) prev
)
2508 (while (and (setq prev
(org-export-get-previous-element
2510 (memq (org-element-property :post-blank prev
)
2512 (funcall same-mode-p prev
))
2513 (setq cluster-beg prev
))
2514 (and (or (org-element-property :caption cluster-beg
)
2515 (org-element-property :name cluster-beg
))
2516 "\n\\end{equation}")))
2522 (defun org-latex-table-cell (table-cell contents info
)
2523 "Transcode a TABLE-CELL element from Org to LaTeX.
2524 CONTENTS is the cell contents. INFO is a plist used as
2525 a communication channel."
2526 (concat (if (and contents
2527 org-latex-table-scientific-notation
2528 (string-match orgtbl-exp-regexp contents
))
2529 ;; Use appropriate format string for scientific
2531 (format org-latex-table-scientific-notation
2532 (match-string 1 contents
)
2533 (match-string 2 contents
))
2535 (when (org-export-get-next-element table-cell info
) " & ")))
2540 (defun org-latex-table-row (table-row contents info
)
2541 "Transcode a TABLE-ROW element from Org to LaTeX.
2542 CONTENTS is the contents of the row. INFO is a plist used as
2543 a communication channel."
2544 ;; Rules are ignored since table separators are deduced from
2545 ;; borders of the current row.
2546 (when (eq (org-element-property :type table-row
) 'standard
)
2547 (let* ((attr (org-export-read-attribute :attr_latex
2548 (org-export-get-parent table-row
)))
2549 (longtablep (member (or (plist-get attr
:environment
)
2550 org-latex-default-table-environment
)
2551 '("longtable" "longtabu")))
2552 (booktabsp (if (plist-member attr
:booktabs
)
2553 (plist-get attr
:booktabs
)
2554 org-latex-tables-booktabs
))
2555 ;; TABLE-ROW's borders are extracted from its first cell.
2556 (borders (org-export-table-cell-borders
2557 (car (org-element-contents table-row
)) info
)))
2559 ;; When BOOKTABS are activated enforce top-rule even when no
2560 ;; hline was specifically marked.
2561 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2562 ((and (memq 'top borders
) (memq 'above borders
)) "\\hline\n"))
2565 ;; Special case for long tables. Define header and footers.
2566 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
2569 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2572 (if booktabsp
"\\midrule" "\\hline")
2573 (if booktabsp
"\\midrule" "\\hline")
2574 ;; Number of columns.
2575 (cdr (org-export-table-dimensions
2576 (org-export-get-parent-table table-row
) info
))))
2577 ;; When BOOKTABS are activated enforce bottom rule even when
2578 ;; no hline was specifically marked.
2579 ((and booktabsp
(memq 'bottom borders
)) "\\bottomrule")
2580 ((and (memq 'bottom borders
) (memq 'below borders
)) "\\hline")
2581 ((memq 'below borders
) (if booktabsp
"\\midrule" "\\hline")))))))
2586 (defun org-latex-target (target contents info
)
2587 "Transcode a TARGET object from Org to LaTeX.
2588 CONTENTS is nil. INFO is a plist holding contextual
2590 (format "\\label{%s}"
2591 (org-export-solidify-link-text (org-element-property :value target
))))
2596 (defun org-latex-timestamp (timestamp contents info
)
2597 "Transcode a TIMESTAMP object from Org to LaTeX.
2598 CONTENTS is nil. INFO is a plist holding contextual
2600 (let ((value (org-latex-plain-text
2601 (org-timestamp-translate timestamp
) info
)))
2602 (case (org-element-property :type timestamp
)
2603 ((active active-range
) (format org-latex-active-timestamp-format value
))
2604 ((inactive inactive-range
)
2605 (format org-latex-inactive-timestamp-format value
))
2606 (otherwise (format org-latex-diary-timestamp-format value
)))))
2611 (defun org-latex-underline (underline contents info
)
2612 "Transcode UNDERLINE from Org to LaTeX.
2613 CONTENTS is the text with underline markup. INFO is a plist
2614 holding contextual information."
2615 (org-latex--text-markup contents
'underline
))
2620 (defun org-latex-verbatim (verbatim contents info
)
2621 "Transcode a VERBATIM object from Org to LaTeX.
2622 CONTENTS is nil. INFO is a plist used as a communication
2624 (org-latex--text-markup (org-element-property :value verbatim
) 'verbatim
))
2629 (defun org-latex-verse-block (verse-block contents info
)
2630 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2631 CONTENTS is verse block contents. INFO is a plist holding
2632 contextual information."
2633 (org-latex--wrap-label
2635 ;; In a verse environment, add a line break to each newline
2636 ;; character and change each white space at beginning of a line
2637 ;; into a space of 1 em. Also change each blank line with
2638 ;; a vertical space of 1 em.
2640 (setq contents
(replace-regexp-in-string
2641 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2642 (replace-regexp-in-string
2643 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents
)))
2644 (while (string-match "^[ \t]+" contents
)
2645 (let ((new-str (format "\\hspace*{%dem}"
2646 (length (match-string 0 contents
)))))
2647 (setq contents
(replace-match new-str nil t contents
))))
2648 (format "\\begin{verse}\n%s\\end{verse}" contents
))))
2652 ;;; End-user functions
2655 (defun org-latex-export-as-latex
2656 (&optional async subtreep visible-only body-only ext-plist
)
2657 "Export current buffer as a LaTeX buffer.
2659 If narrowing is active in the current buffer, only export its
2662 If a region is active, export that region.
2664 A non-nil optional argument ASYNC means the process should happen
2665 asynchronously. The resulting buffer should be accessible
2666 through the `org-export-stack' interface.
2668 When optional argument SUBTREEP is non-nil, export the sub-tree
2669 at point, extracting information from the headline properties
2672 When optional argument VISIBLE-ONLY is non-nil, don't export
2673 contents of hidden elements.
2675 When optional argument BODY-ONLY is non-nil, only write code
2676 between \"\\begin{document}\" and \"\\end{document}\".
2678 EXT-PLIST, when provided, is a property list with external
2679 parameters overriding Org default settings, but still inferior to
2680 file-local settings.
2682 Export is done in a buffer named \"*Org LATEX Export*\", which
2683 will be displayed when `org-export-show-temporary-export-buffer'
2687 (org-export-async-start
2689 (with-current-buffer (get-buffer-create "*Org LATEX Export*")
2692 (goto-char (point-min))
2694 (org-export-add-to-stack (current-buffer) 'latex
)))
2695 `(org-export-as 'latex
,subtreep
,visible-only
,body-only
2698 (org-export-to-buffer 'latex
"*Org LATEX Export*"
2699 subtreep visible-only body-only ext-plist
)))
2700 (with-current-buffer outbuf
(LaTeX-mode))
2701 (when org-export-show-temporary-export-buffer
2702 (switch-to-buffer-other-window outbuf
)))))
2705 (defun org-latex-convert-region-to-latex ()
2706 "Assume the current region has org-mode syntax, and convert it to LaTeX.
2707 This can be used in any buffer. For example, you can write an
2708 itemized list in org-mode syntax in an LaTeX buffer and use this
2709 command to convert it."
2711 (org-export-replace-region-by 'latex
))
2714 (defun org-latex-export-to-latex
2715 (&optional async subtreep visible-only body-only ext-plist
)
2716 "Export current buffer to a LaTeX file.
2718 If narrowing is active in the current buffer, only export its
2721 If a region is active, export that region.
2723 A non-nil optional argument ASYNC means the process should happen
2724 asynchronously. The resulting file should be accessible through
2725 the `org-export-stack' interface.
2727 When optional argument SUBTREEP is non-nil, export the sub-tree
2728 at point, extracting information from the headline properties
2731 When optional argument VISIBLE-ONLY is non-nil, don't export
2732 contents of hidden elements.
2734 When optional argument BODY-ONLY is non-nil, only write code
2735 between \"\\begin{document}\" and \"\\end{document}\".
2737 EXT-PLIST, when provided, is a property list with external
2738 parameters overriding Org default settings, but still inferior to
2739 file-local settings.
2741 Return output file's name."
2743 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
2745 (org-export-async-start
2746 (lambda (f) (org-export-add-to-stack f
'latex
))
2749 'latex
,outfile
,subtreep
,visible-only
,body-only
',ext-plist
)))
2751 'latex outfile subtreep visible-only body-only ext-plist
))))
2754 (defun org-latex-export-to-pdf
2755 (&optional async subtreep visible-only body-only ext-plist
)
2756 "Export current buffer to LaTeX then process through to PDF.
2758 If narrowing is active in the current buffer, only export its
2761 If a region is active, export that region.
2763 A non-nil optional argument ASYNC means the process should happen
2764 asynchronously. The resulting file should be accessible through
2765 the `org-export-stack' interface.
2767 When optional argument SUBTREEP is non-nil, export the sub-tree
2768 at point, extracting information from the headline properties
2771 When optional argument VISIBLE-ONLY is non-nil, don't export
2772 contents of hidden elements.
2774 When optional argument BODY-ONLY is non-nil, only write code
2775 between \"\\begin{document}\" and \"\\end{document}\".
2777 EXT-PLIST, when provided, is a property list with external
2778 parameters overriding Org default settings, but still inferior to
2779 file-local settings.
2781 Return PDF file's name."
2784 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
2785 (org-export-async-start
2786 (lambda (f) (org-export-add-to-stack f
'latex
))
2790 'latex
,outfile
,subtreep
,visible-only
,body-only
2793 (org-latex-export-to-latex
2794 nil subtreep visible-only body-only ext-plist
))))
2796 (defun org-latex-compile (texfile &optional snippet
)
2797 "Compile a TeX file.
2799 TEXFILE is the name of the file being compiled. Processing is
2800 done through the command specified in `org-latex-pdf-process'.
2802 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
2803 file used to preview a LaTeX snippet. In this case, do not
2804 create a log buffer and do not bother removing log files.
2806 Return PDF file name or an error if it couldn't be produced."
2807 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile
)))
2808 (full-name (file-truename texfile
))
2809 (out-dir (file-name-directory texfile
))
2810 ;; Properly set working directory for compilation.
2811 (default-directory (if (file-name-absolute-p texfile
)
2812 (file-name-directory full-name
)
2815 (unless snippet
(message (format "Processing LaTeX file %s..." texfile
)))
2816 (save-window-excursion
2818 ;; A function is provided: Apply it.
2819 ((functionp org-latex-pdf-process
)
2820 (funcall org-latex-pdf-process
(shell-quote-argument texfile
)))
2821 ;; A list is provided: Replace %b, %f and %o with appropriate
2822 ;; values in each command before applying it. Output is
2823 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2824 ((consp org-latex-pdf-process
)
2825 (let ((outbuf (and (not snippet
)
2826 (get-buffer-create "*Org PDF LaTeX Output*"))))
2830 (replace-regexp-in-string
2831 "%b" (shell-quote-argument base-name
)
2832 (replace-regexp-in-string
2833 "%f" (shell-quote-argument full-name
)
2834 (replace-regexp-in-string
2835 "%o" (shell-quote-argument out-dir
) command t t
) t t
) t t
)
2837 org-latex-pdf-process
)
2838 ;; Collect standard errors from output buffer.
2839 (setq errors
(and (not snippet
) (org-latex--collect-errors outbuf
)))))
2840 (t (error "No valid command to process to PDF")))
2841 (let ((pdffile (concat out-dir base-name
".pdf")))
2842 ;; Check for process failure. Provide collected errors if
2844 (if (not (file-exists-p pdffile
))
2845 (error (concat (format "PDF file %s wasn't produced" pdffile
)
2846 (when errors
(concat ": " errors
))))
2847 ;; Else remove log files, when specified, and signal end of
2848 ;; process to user, along with any error encountered.
2849 (when (and (not snippet
) org-latex-remove-logfiles
)
2850 (dolist (ext org-latex-logfiles-extensions
)
2851 (let ((file (concat out-dir base-name
"." ext
)))
2852 (when (file-exists-p file
) (delete-file file
)))))
2853 (message (concat "Process completed"
2854 (if (not errors
) "."
2855 (concat " with errors: " errors
)))))
2856 ;; Return output file name.
2859 (defun org-latex--collect-errors (buffer)
2860 "Collect some kind of errors from \"pdflatex\" command output.
2862 BUFFER is the buffer containing output.
2864 Return collected error types as a string, or nil if there was
2866 (with-current-buffer buffer
2868 (goto-char (point-max))
2869 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t
)
2870 (let ((case-fold-search t
)
2872 (dolist (latex-error org-latex-known-errors
)
2873 (when (save-excursion (re-search-forward (car latex-error
) nil t
))
2874 (setq errors
(concat errors
" " (cdr latex-error
)))))
2875 (and (org-string-nw-p errors
) (org-trim errors
)))))))
2878 (defun org-latex-publish-to-latex (plist filename pub-dir
)
2879 "Publish an Org file to LaTeX.
2881 FILENAME is the filename of the Org file to be published. PLIST
2882 is the property list for the given project. PUB-DIR is the
2883 publishing directory.
2885 Return output file name."
2886 (org-publish-org-to 'latex filename
".tex" plist pub-dir
))
2889 (defun org-latex-publish-to-pdf (plist filename pub-dir
)
2890 "Publish an Org file to PDF (via LaTeX).
2892 FILENAME is the filename of the Org file to be published. PLIST
2893 is the property list for the given project. PUB-DIR is the
2894 publishing directory.
2896 Return output file name."
2897 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
2898 ;; in working directory and then moved to publishing directory.
2899 (org-publish-attachment
2901 (org-latex-compile (org-publish-org-to 'latex filename
".tex" plist
))
2908 ;; generated-autoload-file: "org-loaddefs.el"
2911 ;;; ox-latex.el ends here