[ox-latex] add \phantomsection where appropriate.
[org-mode/org-tableheadings.git] / lisp / ox-latex.el
blob5dd79d3b44fa6b586002b5c749ac03939902f50b
1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2014 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; See Org manual for details.
27 ;;; Code:
29 (eval-when-compile (require 'cl))
30 (require 'ox)
31 (require 'ox-publish)
33 (defvar org-latex-default-packages-alist)
34 (defvar org-latex-packages-alist)
35 (defvar orgtbl-exp-regexp)
39 ;;; Define Back-End
41 (org-export-define-backend 'latex
42 '((bold . org-latex-bold)
43 (center-block . org-latex-center-block)
44 (clock . org-latex-clock)
45 (code . org-latex-code)
46 (comment . (lambda (&rest args) ""))
47 (comment-block . (lambda (&rest args) ""))
48 (drawer . org-latex-drawer)
49 (dynamic-block . org-latex-dynamic-block)
50 (entity . org-latex-entity)
51 (example-block . org-latex-example-block)
52 (export-block . org-latex-export-block)
53 (export-snippet . org-latex-export-snippet)
54 (fixed-width . org-latex-fixed-width)
55 (footnote-definition . org-latex-footnote-definition)
56 (footnote-reference . org-latex-footnote-reference)
57 (headline . org-latex-headline)
58 (horizontal-rule . org-latex-horizontal-rule)
59 (inline-src-block . org-latex-inline-src-block)
60 (inlinetask . org-latex-inlinetask)
61 (italic . org-latex-italic)
62 (item . org-latex-item)
63 (keyword . org-latex-keyword)
64 (latex-environment . org-latex-latex-environment)
65 (latex-fragment . org-latex-latex-fragment)
66 (line-break . org-latex-line-break)
67 (link . org-latex-link)
68 (node-property . org-latex-node-property)
69 (paragraph . org-latex-paragraph)
70 (plain-list . org-latex-plain-list)
71 (plain-text . org-latex-plain-text)
72 (planning . org-latex-planning)
73 (property-drawer . org-latex-property-drawer)
74 (quote-block . org-latex-quote-block)
75 (radio-target . org-latex-radio-target)
76 (section . org-latex-section)
77 (special-block . org-latex-special-block)
78 (src-block . org-latex-src-block)
79 (statistics-cookie . org-latex-statistics-cookie)
80 (strike-through . org-latex-strike-through)
81 (subscript . org-latex-subscript)
82 (superscript . org-latex-superscript)
83 (table . org-latex-table)
84 (table-cell . org-latex-table-cell)
85 (table-row . org-latex-table-row)
86 (target . org-latex-target)
87 (template . org-latex-template)
88 (timestamp . org-latex-timestamp)
89 (underline . org-latex-underline)
90 (verbatim . org-latex-verbatim)
91 (verse-block . org-latex-verse-block)
92 ;; Pseudo objects.
93 (latex-math-block . org-latex-math-block))
94 :export-block '("LATEX" "TEX")
95 :menu-entry
96 '(?l "Export to LaTeX"
97 ((?L "As LaTeX buffer" org-latex-export-as-latex)
98 (?l "As LaTeX file" org-latex-export-to-latex)
99 (?p "As PDF file" org-latex-export-to-pdf)
100 (?o "As PDF file and open"
101 (lambda (a s v b)
102 (if a (org-latex-export-to-pdf t s v b)
103 (org-open-file (org-latex-export-to-pdf nil s v b)))))))
104 :filters-alist '((:filter-options . org-latex-math-block-options-filter)
105 (:filter-parse-tree . org-latex-math-block-tree-filter))
106 :options-alist
107 '((:latex-class "LATEX_CLASS" nil org-latex-default-class t)
108 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
109 (:latex-header "LATEX_HEADER" nil nil newline)
110 (:latex-header-extra "LATEX_HEADER_EXTRA" nil nil newline)
111 ;; Other variables.
112 (:latex-active-timestamp-format nil nil org-latex-active-timestamp-format)
113 (:latex-classes nil nil org-latex-classes)
114 (:latex-custom-id-labels nil nil org-latex-custom-id-as-label)
115 (:latex-default-figure-position nil nil org-latex-default-figure-position)
116 (:latex-default-table-environment nil nil org-latex-default-table-environment)
117 (:latex-default-table-mode nil nil org-latex-default-table-mode)
118 (:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format)
119 (:latex-footnote-separator nil nil org-latex-footnote-separator)
120 (:latex-format-drawer-function nil nil org-latex-format-drawer-function)
121 (:latex-format-headline-function nil nil org-latex-format-headline-function)
122 (:latex-format-inlinetask-function nil nil org-latex-format-inlinetask-function)
123 (:latex-hyperref-template nil nil org-latex-hyperref-template t)
124 (:latex-image-default-height nil nil org-latex-image-default-height)
125 (:latex-image-default-option nil nil org-latex-image-default-option)
126 (:latex-image-default-width nil nil org-latex-image-default-width)
127 (:latex-inactive-timestamp-format nil nil org-latex-inactive-timestamp-format)
128 (:latex-inline-image-rules nil nil org-latex-inline-image-rules)
129 (:latex-link-with-unknown-path-format nil nil org-latex-link-with-unknown-path-format)
130 (:latex-listings nil nil org-latex-listings)
131 (:latex-listings-langs nil nil org-latex-listings-langs)
132 (:latex-listings-options nil nil org-latex-listings-options)
133 (:latex-minted-langs nil nil org-latex-minted-langs)
134 (:latex-minted-options nil nil org-latex-minted-options)
135 (:latex-table-caption-above nil nil org-latex-table-caption-above)
136 (:latex-table-scientific-notation nil nil org-latex-table-scientific-notation)
137 (:latex-tables-booktabs nil nil org-latex-tables-booktabs)
138 (:latex-tables-centered nil nil org-latex-tables-centered)
139 (:latex-text-markup-alist nil nil org-latex-text-markup-alist)
140 (:latex-title-command nil nil org-latex-title-command)
141 (:latex-toc-command nil nil org-latex-toc-command)
142 ;; Redefine regular options.
143 (:date "DATE" nil "\\today" t)))
147 ;;; Internal Variables
149 (defconst org-latex-babel-language-alist
150 '(("af" . "afrikaans")
151 ("bg" . "bulgarian")
152 ("bt-br" . "brazilian")
153 ("ca" . "catalan")
154 ("cs" . "czech")
155 ("cy" . "welsh")
156 ("da" . "danish")
157 ("de" . "germanb")
158 ("de-at" . "naustrian")
159 ("de-de" . "ngerman")
160 ("el" . "greek")
161 ("en" . "english")
162 ("en-au" . "australian")
163 ("en-ca" . "canadian")
164 ("en-gb" . "british")
165 ("en-ie" . "irish")
166 ("en-nz" . "newzealand")
167 ("en-us" . "american")
168 ("es" . "spanish")
169 ("et" . "estonian")
170 ("eu" . "basque")
171 ("fi" . "finnish")
172 ("fr" . "frenchb")
173 ("fr-ca" . "canadien")
174 ("gl" . "galician")
175 ("hr" . "croatian")
176 ("hu" . "hungarian")
177 ("id" . "indonesian")
178 ("is" . "icelandic")
179 ("it" . "italian")
180 ("la" . "latin")
181 ("ms" . "malay")
182 ("nl" . "dutch")
183 ("nb" . "norsk")
184 ("nn" . "nynorsk")
185 ("no" . "norsk")
186 ("pl" . "polish")
187 ("pt" . "portuguese")
188 ("ro" . "romanian")
189 ("ru" . "russian")
190 ("sa" . "sanskrit")
191 ("sb" . "uppersorbian")
192 ("sk" . "slovak")
193 ("sl" . "slovene")
194 ("sq" . "albanian")
195 ("sr" . "serbian")
196 ("sv" . "swedish")
197 ("ta" . "tamil")
198 ("tr" . "turkish")
199 ("uk" . "ukrainian"))
200 "Alist between language code and corresponding Babel option.")
202 (defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
203 ("qbordermatrix" . "\\cr")
204 ("kbordermatrix" . "\\\\"))
205 "Alist between matrix macros and their row ending.")
207 (defconst org-latex-pseudo-objects '(latex-math-block)
208 "List of pseudo-object types introduced in the back-end.")
212 ;;; User Configurable Variables
214 (defgroup org-export-latex nil
215 "Options for exporting Org mode files to LaTeX."
216 :tag "Org Export LaTeX"
217 :group 'org-export)
220 ;;;; Preamble
222 (defcustom org-latex-default-class "article"
223 "The default LaTeX class."
224 :group 'org-export-latex
225 :type '(string :tag "LaTeX class"))
227 (defcustom org-latex-classes
228 '(("article"
229 "\\documentclass[11pt]{article}"
230 ("\\section{%s}" . "\\section*{%s}")
231 ("\\subsection{%s}" . "\\subsection*{%s}")
232 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
233 ("\\paragraph{%s}" . "\\paragraph*{%s}")
234 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
235 ("report"
236 "\\documentclass[11pt]{report}"
237 ("\\part{%s}" . "\\part*{%s}")
238 ("\\chapter{%s}" . "\\chapter*{%s}")
239 ("\\section{%s}" . "\\section*{%s}")
240 ("\\subsection{%s}" . "\\subsection*{%s}")
241 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
242 ("book"
243 "\\documentclass[11pt]{book}"
244 ("\\part{%s}" . "\\part*{%s}")
245 ("\\chapter{%s}" . "\\chapter*{%s}")
246 ("\\section{%s}" . "\\section*{%s}")
247 ("\\subsection{%s}" . "\\subsection*{%s}")
248 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
249 "Alist of LaTeX classes and associated header and structure.
250 If #+LATEX_CLASS is set in the buffer, use its value and the
251 associated information. Here is the structure of each cell:
253 \(class-name
254 header-string
255 \(numbered-section . unnumbered-section)
256 ...)
258 The header string
259 -----------------
261 The HEADER-STRING is the header that will be inserted into the
262 LaTeX file. It should contain the \\documentclass macro, and
263 anything else that is needed for this setup. To this header, the
264 following commands will be added:
266 - Calls to \\usepackage for all packages mentioned in the
267 variables `org-latex-default-packages-alist' and
268 `org-latex-packages-alist'. Thus, your header definitions
269 should avoid to also request these packages.
271 - Lines specified via \"#+LATEX_HEADER:\" and
272 \"#+LATEX_HEADER_EXTRA:\" keywords.
274 If you need more control about the sequence in which the header
275 is built up, or if you want to exclude one of these building
276 blocks for a particular class, you can use the following
277 macro-like placeholders.
279 [DEFAULT-PACKAGES] \\usepackage statements for default packages
280 [NO-DEFAULT-PACKAGES] do not include any of the default packages
281 [PACKAGES] \\usepackage statements for packages
282 [NO-PACKAGES] do not include the packages
283 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
284 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
286 So a header like
288 \\documentclass{article}
289 [NO-DEFAULT-PACKAGES]
290 [EXTRA]
291 \\providecommand{\\alert}[1]{\\textbf{#1}}
292 [PACKAGES]
294 will omit the default packages, and will include the
295 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
296 to \\providecommand, and then place \\usepackage commands based
297 on the content of `org-latex-packages-alist'.
299 If your header, `org-latex-default-packages-alist' or
300 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
301 AUTO will automatically be replaced with a coding system derived
302 from `buffer-file-coding-system'. See also the variable
303 `org-latex-inputenc-alist' for a way to influence this mechanism.
305 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\",
306 AUTO will be replaced with the language related to the language
307 code specified by `org-export-default-language', which see. Note
308 that constructions such as \"\\usepackage[french,AUTO,english]{babel}\"
309 are permitted.
311 The sectioning structure
312 ------------------------
314 The sectioning structure of the class is given by the elements
315 following the header string. For each sectioning level, a number
316 of strings is specified. A %s formatter is mandatory in each
317 section string and will be replaced by the title of the section.
319 Instead of a cons cell (numbered . unnumbered), you can also
320 provide a list of 2 or 4 elements,
322 \(numbered-open numbered-close)
326 \(numbered-open numbered-close unnumbered-open unnumbered-close)
328 providing opening and closing strings for a LaTeX environment
329 that should represent the document section. The opening clause
330 should have a %s to represent the section title.
332 Instead of a list of sectioning commands, you can also specify
333 a function name. That function will be called with two
334 parameters, the (reduced) level of the headline, and a predicate
335 non-nil when the headline should be numbered. It must return
336 a format string in which the section title will be added."
337 :group 'org-export-latex
338 :type '(repeat
339 (list (string :tag "LaTeX class")
340 (string :tag "LaTeX header")
341 (repeat :tag "Levels" :inline t
342 (choice
343 (cons :tag "Heading"
344 (string :tag " numbered")
345 (string :tag "unnumbered"))
346 (list :tag "Environment"
347 (string :tag "Opening (numbered)")
348 (string :tag "Closing (numbered)")
349 (string :tag "Opening (unnumbered)")
350 (string :tag "Closing (unnumbered)"))
351 (function :tag "Hook computing sectioning"))))))
353 (defcustom org-latex-inputenc-alist nil
354 "Alist of inputenc coding system names, and what should really be used.
355 For example, adding an entry
357 (\"utf8\" . \"utf8x\")
359 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
360 are written as utf8 files."
361 :group 'org-export-latex
362 :type '(repeat
363 (cons
364 (string :tag "Derived from buffer")
365 (string :tag "Use this instead"))))
367 (defcustom org-latex-title-command "\\maketitle"
368 "The command used to insert the title just after \\begin{document}.
369 If this string contains the formatting specification \"%s\" then
370 it will be used as a formatting string, passing the title as an
371 argument."
372 :group 'org-export-latex
373 :type 'string)
375 (defcustom org-latex-toc-command "\\tableofcontents\n\n"
376 "LaTeX command to set the table of contents, list of figures, etc.
377 This command only applies to the table of contents generated with
378 the toc:nil option, not to those generated with #+TOC keyword."
379 :group 'org-export-latex
380 :type 'string)
382 (defcustom org-latex-hyperref-template
383 "\\hypersetup{\n pdfkeywords={%k},\n pdfsubject={%d},\n pdfcreator={%c}}\n"
384 "Template for hyperref package options.
386 Value is a format string, which can contain the following placeholders:
388 %k for KEYWORDS line
389 %d for DESCRIPTION line
390 %c for CREATOR line
392 Set it to the empty string to ignore the command completely."
393 :group 'org-export-latex
394 :version "24.5"
395 :package-version '(Org . "8.3")
396 :type 'string)
398 ;;;; Headline
400 (defcustom org-latex-format-headline-function
401 'org-latex-format-headline-default-function
402 "Function for formatting the headline's text.
404 This function will be called with six arguments:
405 TODO the todo keyword (string or nil)
406 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
407 PRIORITY the priority of the headline (integer or nil)
408 TEXT the main headline text (string)
409 TAGS the tags (list of strings or nil)
410 INFO the export options (plist)
412 The function result will be used in the section format string."
413 :group 'org-export-latex
414 :version "24.4"
415 :package-version '(Org . "8.0")
416 :type 'function)
418 (defcustom org-latex-custom-id-as-label nil
419 "Toggle use of CUSTOM_ID properties for generating section labels.
421 When this variable is non-nil, Org will use the value of a
422 headline's CUSTOM_ID property as the key for the \\label command
423 for the LaTeX section corresponding to the headline.
425 By default, Org generates its own internal section labels for all
426 headlines during LaTeX export. This process ensures that the
427 \\label keys are unique and valid, but it means the keys are not
428 available in advance of the export process.
430 Setting this variable gives you control over how Org generates
431 labels for sections during LaTeX export, so that you may know
432 their keys in advance. One reason to do this is that it allows
433 you to refer to headlines using a single label both in Org's link
434 syntax and in embedded LaTeX code.
436 For example, when this variable is non-nil, a headline like this:
438 ** Some section
439 :PROPERTIES:
440 :CUSTOM_ID: sec:foo
441 :END:
442 This is section [[#sec:foo]].
443 #+BEGIN_LATEX
444 And this is still section \\ref{sec:foo}.
445 #+END_LATEX
447 will be exported to LaTeX as:
449 \\subsection{Some section}
450 \\label{sec:foo}
451 This is section \\ref{sec:foo}.
452 And this is still section \\ref{sec:foo}.
454 Note, however, that setting this variable introduces a limitation
455 on the possible values for CUSTOM_ID. When this variable is
456 non-nil and a headline defines a CUSTOM_ID value, Org simply
457 passes this value to \\label unchanged. You are responsible for
458 ensuring that the value is a valid LaTeX \\label key, and that no
459 other \\label commands with the same key appear elsewhere in your
460 document. (Keys may contain letters, numbers, and the following
461 punctuation: '_' '.' '-' ':'.) There are no such limitations on
462 CUSTOM_ID when this variable is nil.
464 For headlines that do not define the CUSTOM_ID property, Org will
465 continue to use its default labeling scheme to generate labels
466 and resolve links into section references."
467 :group 'org-export-latex
468 :type 'boolean
469 :version "24.5"
470 :package-version '(Org . "8.3"))
472 ;;;; Footnotes
474 (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
475 "Text used to separate footnotes."
476 :group 'org-export-latex
477 :type 'string)
480 ;;;; Timestamps
482 (defcustom org-latex-active-timestamp-format "\\textit{%s}"
483 "A printf format string to be applied to active timestamps."
484 :group 'org-export-latex
485 :type 'string)
487 (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
488 "A printf format string to be applied to inactive timestamps."
489 :group 'org-export-latex
490 :type 'string)
492 (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
493 "A printf format string to be applied to diary timestamps."
494 :group 'org-export-latex
495 :type 'string)
498 ;;;; Links
500 (defcustom org-latex-image-default-option ""
501 "Default option for images."
502 :group 'org-export-latex
503 :version "24.4"
504 :package-version '(Org . "8.0")
505 :type 'string)
507 (defcustom org-latex-image-default-width ".9\\linewidth"
508 "Default width for images.
509 This value will not be used if a height is provided."
510 :group 'org-export-latex
511 :version "24.4"
512 :package-version '(Org . "8.0")
513 :type 'string)
515 (defcustom org-latex-image-default-height ""
516 "Default height for images.
517 This value will not be used if a width is provided, or if the
518 image is wrapped within a \"figure\" or \"wrapfigure\"
519 environment."
520 :group 'org-export-latex
521 :version "24.4"
522 :package-version '(Org . "8.0")
523 :type 'string)
525 (defcustom org-latex-default-figure-position "htb"
526 "Default position for latex figures."
527 :group 'org-export-latex
528 :type 'string)
530 (defcustom org-latex-inline-image-rules
531 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
532 "Rules characterizing image files that can be inlined into LaTeX.
534 A rule consists in an association whose key is the type of link
535 to consider, and value is a regexp that will be matched against
536 link's path.
538 Note that, by default, the image extension *actually* allowed
539 depend on the way the LaTeX file is processed. When used with
540 pdflatex, pdf, jpg and png images are OK. When processing
541 through dvi to Postscript, only ps and eps are allowed. The
542 default we use here encompasses both."
543 :group 'org-export-latex
544 :version "24.4"
545 :package-version '(Org . "8.0")
546 :type '(alist :key-type (string :tag "Type")
547 :value-type (regexp :tag "Path")))
549 (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
550 "Format string for links with unknown path type."
551 :group 'org-export-latex
552 :type 'string)
555 ;;;; Tables
557 (defcustom org-latex-default-table-environment "tabular"
558 "Default environment used to build tables."
559 :group 'org-export-latex
560 :version "24.4"
561 :package-version '(Org . "8.0")
562 :type 'string)
564 (defcustom org-latex-default-table-mode 'table
565 "Default mode for tables.
567 Value can be a symbol among:
569 `table' Regular LaTeX table.
571 `math' In this mode, every cell is considered as being in math
572 mode and the complete table will be wrapped within a math
573 environment. It is particularly useful to write matrices.
575 `inline-math' This mode is almost the same as `math', but the
576 math environment will be inlined.
578 `verbatim' The table is exported as it appears in the Org
579 buffer, within a verbatim environment.
581 This value can be overridden locally with, i.e. \":mode math\" in
582 LaTeX attributes.
584 When modifying this variable, it may be useful to change
585 `org-latex-default-table-environment' accordingly."
586 :group 'org-export-latex
587 :version "24.4"
588 :package-version '(Org . "8.0")
589 :type '(choice (const :tag "Table" table)
590 (const :tag "Matrix" math)
591 (const :tag "Inline matrix" inline-math)
592 (const :tag "Verbatim" verbatim))
593 :safe (lambda (s) (memq s '(table math inline-math verbatim))))
595 (defcustom org-latex-tables-centered t
596 "When non-nil, tables are exported in a center environment."
597 :group 'org-export-latex
598 :type 'boolean
599 :safe #'booleanp)
601 (defcustom org-latex-tables-booktabs nil
602 "When non-nil, display tables in a formal \"booktabs\" style.
603 This option assumes that the \"booktabs\" package is properly
604 loaded in the header of the document. This value can be ignored
605 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
606 attributes."
607 :group 'org-export-latex
608 :version "24.4"
609 :package-version '(Org . "8.0")
610 :type 'boolean
611 :safe #'booleanp)
613 (defcustom org-latex-table-caption-above t
614 "When non-nil, place caption string at the beginning of the table.
615 Otherwise, place it near the end."
616 :group 'org-export-latex
617 :type 'boolean
618 :safe #'booleanp)
620 (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
621 "Format string to display numbers in scientific notation.
622 The format should have \"%s\" twice, for mantissa and exponent
623 \(i.e., \"%s\\\\times10^{%s}\").
625 When nil, no transformation is made."
626 :group 'org-export-latex
627 :version "24.4"
628 :package-version '(Org . "8.0")
629 :type '(choice
630 (string :tag "Format string")
631 (const :tag "No formatting" nil)))
633 ;;;; Text markup
635 (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
636 (code . verb)
637 (italic . "\\emph{%s}")
638 (strike-through . "\\sout{%s}")
639 (underline . "\\uline{%s}")
640 (verbatim . protectedtexttt))
641 "Alist of LaTeX expressions to convert text markup.
643 The key must be a symbol among `bold', `code', `italic',
644 `strike-through', `underline' and `verbatim'. The value is
645 a formatting string to wrap fontified text with.
647 Value can also be set to the following symbols: `verb' and
648 `protectedtexttt'. For the former, Org will use \"\\verb\" to
649 create a format string and select a delimiter character that
650 isn't in the string. For the latter, Org will use \"\\texttt\"
651 to typeset and try to protect special characters.
653 If no association can be found for a given markup, text will be
654 returned as-is."
655 :group 'org-export-latex
656 :type 'alist
657 :options '(bold code italic strike-through underline verbatim))
660 ;;;; Drawers
662 (defcustom org-latex-format-drawer-function
663 (lambda (name contents) contents)
664 "Function called to format a drawer in LaTeX code.
666 The function must accept two parameters:
667 NAME the drawer name, like \"LOGBOOK\"
668 CONTENTS the contents of the drawer.
670 The function should return the string to be exported.
672 The default function simply returns the value of CONTENTS."
673 :group 'org-export-latex
674 :version "24.4"
675 :package-version '(Org . "8.3")
676 :type 'function)
679 ;;;; Inlinetasks
681 (defcustom org-latex-format-inlinetask-function
682 'org-latex-format-inlinetask-default-function
683 "Function called to format an inlinetask in LaTeX code.
685 The function must accept seven parameters:
686 TODO the todo keyword (string or nil)
687 TODO-TYPE the todo type (symbol: `todo', `done', nil)
688 PRIORITY the inlinetask priority (integer or nil)
689 NAME the inlinetask name (string)
690 TAGS the inlinetask tags (list of strings or nil)
691 CONTENTS the contents of the inlinetask (string or nil)
692 INFO the export options (plist)
694 The function should return the string to be exported."
695 :group 'org-export-latex
696 :type 'function
697 :version "24.5"
698 :package-version '(Org . "8.3"))
701 ;; Src blocks
703 (defcustom org-latex-listings nil
704 "Non-nil means export source code using the listings package.
706 This package will fontify source code, possibly even with color.
707 If you want to use this, you also need to make LaTeX use the
708 listings package, and if you want to have color, the color
709 package. Just add these to `org-latex-packages-alist', for
710 example using customize, or with something like:
712 \(require 'ox-latex)
713 \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
714 \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
716 Alternatively,
718 \(setq org-latex-listings 'minted)
720 causes source code to be exported using the minted package as
721 opposed to listings. If you want to use minted, you need to add
722 the minted package to `org-latex-packages-alist', for example
723 using customize, or with
725 \(require 'ox-latex)
726 \(add-to-list 'org-latex-packages-alist '(\"\" \"minted\"))
728 In addition, it is necessary to install pygments
729 \(http://pygments.org), and to configure the variable
730 `org-latex-pdf-process' so that the -shell-escape option is
731 passed to pdflatex.
733 The minted choice has possible repercussions on the preview of
734 latex fragments (see `org-preview-latex-fragment'). If you run
735 into previewing problems, please consult
737 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
738 :group 'org-export-latex
739 :type '(choice
740 (const :tag "Use listings" t)
741 (const :tag "Use minted" minted)
742 (const :tag "Export verbatim" nil))
743 :safe (lambda (s) (memq s '(t nil minted))))
745 (defcustom org-latex-listings-langs
746 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
747 (c "C") (cc "C++")
748 (fortran "fortran")
749 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
750 (html "HTML") (xml "XML")
751 (tex "TeX") (latex "[LaTeX]TeX")
752 (shell-script "bash")
753 (gnuplot "Gnuplot")
754 (ocaml "Caml") (caml "Caml")
755 (sql "SQL") (sqlite "sql")
756 (makefile "make"))
757 "Alist mapping languages to their listing language counterpart.
758 The key is a symbol, the major mode symbol without the \"-mode\".
759 The value is the string that should be inserted as the language
760 parameter for the listings package. If the mode name and the
761 listings name are the same, the language does not need an entry
762 in this list - but it does not hurt if it is present."
763 :group 'org-export-latex
764 :version "24.4"
765 :package-version '(Org . "8.3")
766 :type '(repeat
767 (list
768 (symbol :tag "Major mode ")
769 (string :tag "Listings language"))))
771 (defcustom org-latex-listings-options nil
772 "Association list of options for the latex listings package.
774 These options are supplied as a comma-separated list to the
775 \\lstset command. Each element of the association list should be
776 a list containing two strings: the name of the option, and the
777 value. For example,
779 \(setq org-latex-listings-options
780 '((\"basicstyle\" \"\\\\small\")
781 \(\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
783 will typeset the code in a small size font with underlined, bold
784 black keywords.
786 Note that the same options will be applied to blocks of all
787 languages. If you need block-specific options, you may use the
788 following syntax:
790 #+ATTR_LATEX: :options key1=value1,key2=value2
791 #+BEGIN_SRC <LANG>
793 #+END_SRC"
794 :group 'org-export-latex
795 :type '(repeat
796 (list
797 (string :tag "Listings option name ")
798 (string :tag "Listings option value"))))
800 (defcustom org-latex-minted-langs
801 '((emacs-lisp "common-lisp")
802 (cc "c++")
803 (cperl "perl")
804 (shell-script "bash")
805 (caml "ocaml"))
806 "Alist mapping languages to their minted language counterpart.
807 The key is a symbol, the major mode symbol without the \"-mode\".
808 The value is the string that should be inserted as the language
809 parameter for the minted package. If the mode name and the
810 listings name are the same, the language does not need an entry
811 in this list - but it does not hurt if it is present.
813 Note that minted uses all lower case for language identifiers,
814 and that the full list of language identifiers can be obtained
815 with:
817 pygmentize -L lexers"
818 :group 'org-export-latex
819 :type '(repeat
820 (list
821 (symbol :tag "Major mode ")
822 (string :tag "Minted language"))))
824 (defcustom org-latex-minted-options nil
825 "Association list of options for the latex minted package.
827 These options are supplied within square brackets in
828 \\begin{minted} environments. Each element of the alist should
829 be a list containing two strings: the name of the option, and the
830 value. For example,
832 \(setq org-latex-minted-options
833 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
835 will result in src blocks being exported with
837 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
839 as the start of the minted environment. Note that the same
840 options will be applied to blocks of all languages. If you need
841 block-specific options, you may use the following syntax:
843 #+ATTR_LATEX: :options key1=value1,key2=value2
844 #+BEGIN_SRC <LANG>
846 #+END_SRC"
847 :group 'org-export-latex
848 :type '(repeat
849 (list
850 (string :tag "Minted option name ")
851 (string :tag "Minted option value"))))
853 (defvar org-latex-custom-lang-environments nil
854 "Alist mapping languages to language-specific LaTeX environments.
856 It is used during export of src blocks by the listings and minted
857 latex packages. For example,
859 \(setq org-latex-custom-lang-environments
860 '\(\(python \"pythoncode\"\)\)\)
862 would have the effect that if org encounters begin_src python
863 during latex export it will output
865 \\begin{pythoncode}
866 <src block body>
867 \\end{pythoncode}")
870 ;;;; Compilation
872 (defcustom org-latex-pdf-process
873 '("pdflatex -interaction nonstopmode -output-directory %o %f"
874 "pdflatex -interaction nonstopmode -output-directory %o %f"
875 "pdflatex -interaction nonstopmode -output-directory %o %f")
876 "Commands to process a LaTeX file to a PDF file.
877 This is a list of strings, each of them will be given to the
878 shell as a command. %f in the command will be replaced by the
879 full file name, %b by the file base name (i.e. without directory
880 and extension parts) and %o by the base directory of the file.
882 The reason why this is a list is that it usually takes several
883 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
884 does not have a clever mechanism to detect which of these
885 commands have to be run to get to a stable result, and it also
886 does not do any error checking.
888 By default, Org uses 3 runs of `pdflatex' to do the processing.
889 If you have texi2dvi on your system and if that does not cause
890 the infamous egrep/locale bug:
892 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
894 then `texi2dvi' is the superior choice as it automates the LaTeX
895 build process by calling the \"correct\" combinations of
896 auxiliary programs. Org does offer `texi2dvi' as one of the
897 customize options. Alternatively, `rubber' and `latexmk' also
898 provide similar functionality. The latter supports `biber' out
899 of the box.
901 Alternatively, this may be a Lisp function that does the
902 processing, so you could use this to apply the machinery of
903 AUCTeX or the Emacs LaTeX mode. This function should accept the
904 file name as its single argument."
905 :group 'org-export-pdf
906 :type '(choice
907 (repeat :tag "Shell command sequence"
908 (string :tag "Shell command"))
909 (const :tag "2 runs of pdflatex"
910 ("pdflatex -interaction nonstopmode -output-directory %o %f"
911 "pdflatex -interaction nonstopmode -output-directory %o %f"))
912 (const :tag "3 runs of pdflatex"
913 ("pdflatex -interaction nonstopmode -output-directory %o %f"
914 "pdflatex -interaction nonstopmode -output-directory %o %f"
915 "pdflatex -interaction nonstopmode -output-directory %o %f"))
916 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
917 ("pdflatex -interaction nonstopmode -output-directory %o %f"
918 "bibtex %b"
919 "pdflatex -interaction nonstopmode -output-directory %o %f"
920 "pdflatex -interaction nonstopmode -output-directory %o %f"))
921 (const :tag "2 runs of xelatex"
922 ("xelatex -interaction nonstopmode -output-directory %o %f"
923 "xelatex -interaction nonstopmode -output-directory %o %f"))
924 (const :tag "3 runs of xelatex"
925 ("xelatex -interaction nonstopmode -output-directory %o %f"
926 "xelatex -interaction nonstopmode -output-directory %o %f"
927 "xelatex -interaction nonstopmode -output-directory %o %f"))
928 (const :tag "xelatex,bibtex,xelatex,xelatex"
929 ("xelatex -interaction nonstopmode -output-directory %o %f"
930 "bibtex %b"
931 "xelatex -interaction nonstopmode -output-directory %o %f"
932 "xelatex -interaction nonstopmode -output-directory %o %f"))
933 (const :tag "texi2dvi"
934 ("texi2dvi -p -b -V %f"))
935 (const :tag "rubber"
936 ("rubber -d --into %o %f"))
937 (const :tag "latexmk"
938 ("latexmk -g -pdf %f"))
939 (function)))
941 (defcustom org-latex-logfiles-extensions
942 '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
943 "run.xml" "snm" "toc" "vrb" "xdv")
944 "The list of file extensions to consider as LaTeX logfiles.
945 The logfiles will be remove if `org-latex-remove-logfiles' is
946 non-nil."
947 :group 'org-export-latex
948 :type '(repeat (string :tag "Extension")))
950 (defcustom org-latex-remove-logfiles t
951 "Non-nil means remove the logfiles produced by PDF production.
952 By default, logfiles are files with these extensions: .aux, .idx,
953 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
954 logfiles to remove, set `org-latex-logfiles-extensions'."
955 :group 'org-export-latex
956 :type 'boolean)
958 (defcustom org-latex-known-warnings
959 '(("Reference.*?undefined" . "[undefined reference]")
960 ("Runaway argument" . "[runaway argument]")
961 ("Underfull \\hbox" . "[underfull hbox]")
962 ("Overfull \\hbox" . "[overfull hbox]")
963 ("Citation.*?undefined" . "[undefined citation]")
964 ("Undefined control sequence" . "[undefined control sequence]"))
965 "Alist of regular expressions and associated messages for the user.
966 The regular expressions are used to find possible warnings in the
967 log of a latex-run. These warnings will be reported after
968 calling `org-latex-compile'."
969 :group 'org-export-latex
970 :version "24.5"
971 :package-version '(Org . "8.3")
972 :type '(repeat
973 (cons
974 (string :tag "Regexp")
975 (string :tag "Message"))))
979 ;;; Internal Functions
981 (defun org-latex--caption/label-string (element info)
982 "Return caption and label LaTeX string for ELEMENT.
984 INFO is a plist holding contextual information. If there's no
985 caption nor label, return the empty string.
987 For non-floats, see `org-latex--wrap-label'."
988 (let* ((label (org-element-property :name element))
989 (label-str (if (not (org-string-nw-p label)) ""
990 (format "\\label{%s}"
991 (org-export-solidify-link-text label))))
992 (main (org-export-get-caption element))
993 (short (org-export-get-caption element t))
994 (caption-from-attr-latex (org-export-read-attribute :attr_latex element :caption)))
995 (cond
996 ((org-string-nw-p caption-from-attr-latex)
997 (concat caption-from-attr-latex "\n"))
998 ((and (not main) (equal label-str "")) "")
999 ((not main) (concat label-str "\n"))
1000 ;; Option caption format with short name.
1001 (short (format "\\caption[%s]{%s%s}\n"
1002 (org-export-data short info)
1003 label-str
1004 (org-export-data main info)))
1005 ;; Standard caption format.
1006 (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
1008 (defun org-latex-guess-inputenc (header)
1009 "Set the coding system in inputenc to what the buffer is.
1011 HEADER is the LaTeX header string. This function only applies
1012 when specified inputenc option is \"AUTO\".
1014 Return the new header, as a string."
1015 (let* ((cs (or (ignore-errors
1016 (latexenc-coding-system-to-inputenc
1017 (or org-export-coding-system buffer-file-coding-system)))
1018 "utf8")))
1019 (if (not cs) header
1020 ;; First translate if that is requested.
1021 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
1022 ;; Then find the \usepackage statement and replace the option.
1023 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1024 cs header t nil 1))))
1026 (defun org-latex-guess-babel-language (header info)
1027 "Set Babel's language according to LANGUAGE keyword.
1029 HEADER is the LaTeX header string. INFO is the plist used as
1030 a communication channel.
1032 Insertion of guessed language only happens when Babel package has
1033 explicitly been loaded. Then it is added to the rest of
1034 package's options.
1036 The argument to Babel may be \"AUTO\" which is then replaced with
1037 the language of the document or `org-export-default-language'
1038 unless language in question is already loaded.
1040 Return the new header."
1041 (let ((language-code (plist-get info :language)))
1042 ;; If no language is set or Babel package is not loaded, return
1043 ;; HEADER as-is.
1044 (if (or (not (stringp language-code))
1045 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
1046 header
1047 (let ((options (save-match-data
1048 (org-split-string (match-string 1 header) ",[ \t]*")))
1049 (language (cdr (assoc language-code
1050 org-latex-babel-language-alist))))
1051 ;; If LANGUAGE is already loaded, return header without AUTO.
1052 ;; Otherwise, replace AUTO with language or append language if
1053 ;; AUTO is not present.
1054 (replace-match
1055 (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
1056 (cond ((member language options) (delete "AUTO" options))
1057 ((member "AUTO" options) options)
1058 (t (append options (list language))))
1059 ", ")
1060 t nil header 1)))))
1062 (defun org-latex--find-verb-separator (s)
1063 "Return a character not used in string S.
1064 This is used to choose a separator for constructs like \\verb."
1065 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1066 (loop for c across ll
1067 when (not (string-match (regexp-quote (char-to-string c)) s))
1068 return (char-to-string c))))
1070 (defun org-latex--make-option-string (options)
1071 "Return a comma separated string of keywords and values.
1072 OPTIONS is an alist where the key is the options keyword as
1073 a string, and the value a list containing the keyword value, or
1074 nil."
1075 (mapconcat (lambda (pair)
1076 (concat (first pair)
1077 (when (> (length (second pair)) 0)
1078 (concat "=" (second pair)))))
1079 options
1080 ","))
1082 (defun org-latex--wrap-label (element output)
1083 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1084 This function shouldn't be used for floats. See
1085 `org-latex--caption/label-string'."
1086 (let ((label (org-element-property :name element)))
1087 (if (not (and (org-string-nw-p output) (org-string-nw-p label))) output
1088 (concat (format "\\phantomsection\n\\label{%s}\n"
1089 (org-export-solidify-link-text label))
1090 output))))
1092 (defun org-latex--text-markup (text markup info)
1093 "Format TEXT depending on MARKUP text markup.
1094 INFO is a plist used as a communication channel. See
1095 `org-latex-text-markup-alist' for details."
1096 (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
1097 (cond
1098 ;; No format string: Return raw text.
1099 ((not fmt) text)
1100 ;; Handle the `verb' special case: Find an appropriate separator
1101 ;; and use "\\verb" command.
1102 ((eq 'verb fmt)
1103 (let ((separator (org-latex--find-verb-separator text)))
1104 (concat "\\verb" separator
1105 (replace-regexp-in-string "\n" " " text)
1106 separator)))
1107 ;; Handle the `protectedtexttt' special case: Protect some
1108 ;; special chars and use "\texttt{%s}" format string.
1109 ((eq 'protectedtexttt fmt)
1110 (let ((start 0)
1111 (trans '(("\\" . "\\textbackslash{}")
1112 ("~" . "\\textasciitilde{}")
1113 ("^" . "\\textasciicircum{}")))
1114 (rtn "")
1115 char)
1116 (while (string-match "[\\{}$%&_#~^]" text)
1117 (setq char (match-string 0 text))
1118 (if (> (match-beginning 0) 0)
1119 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
1120 (setq text (substring text (1+ (match-beginning 0))))
1121 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1122 rtn (concat rtn char)))
1123 (setq text (concat rtn text)
1124 fmt "\\texttt{%s}")
1125 (while (string-match "--" text)
1126 (setq text (replace-match "-{}-" t t text)))
1127 (format fmt text)))
1128 ;; Else use format string.
1129 (t (format fmt text)))))
1131 (defun org-latex--delayed-footnotes-definitions (element info)
1132 "Return footnotes definitions in ELEMENT as a string.
1134 INFO is a plist used as a communication channel.
1136 Footnotes definitions are returned within \"\\footnotetxt{}\"
1137 commands.
1139 This function is used within constructs that don't support
1140 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1141 \"\\footnotemark\" is used within the construct and the function
1142 just outside of it."
1143 (mapconcat
1144 (lambda (ref)
1145 (format
1146 "\\footnotetext[%s]{%s}"
1147 (org-export-get-footnote-number ref info)
1148 (org-trim
1149 (org-export-data
1150 (org-export-get-footnote-definition ref info) info))))
1151 ;; Find every footnote reference in ELEMENT.
1152 (let* (all-refs
1153 search-refs ; For byte-compiler.
1154 (search-refs
1155 (function
1156 (lambda (data)
1157 ;; Return a list of all footnote references never seen
1158 ;; before in DATA.
1159 (org-element-map data 'footnote-reference
1160 (lambda (ref)
1161 (when (org-export-footnote-first-reference-p ref info)
1162 (push ref all-refs)
1163 (when (eq (org-element-property :type ref) 'standard)
1164 (funcall search-refs
1165 (org-export-get-footnote-definition ref info)))))
1166 info)
1167 (reverse all-refs)))))
1168 (funcall search-refs element))
1169 ""))
1171 (defun org-latex--translate (s info)
1172 "Translate string S according to specified language.
1173 INFO is a plist used as a communication channel."
1174 (org-export-translate s :latex info))
1178 ;;; Template
1180 (defun org-latex-template (contents info)
1181 "Return complete document string after LaTeX conversion.
1182 CONTENTS is the transcoded contents string. INFO is a plist
1183 holding export options."
1184 (let ((title (org-export-data (plist-get info :title) info)))
1185 (concat
1186 ;; Time-stamp.
1187 (and (plist-get info :time-stamp-file)
1188 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1189 ;; Document class and packages.
1190 (let* ((class (plist-get info :latex-class))
1191 (class-options (plist-get info :latex-class-options))
1192 (header (nth 1 (assoc class (plist-get info :latex-classes))))
1193 (document-class-string
1194 (and (stringp header)
1195 (if (not class-options) header
1196 (replace-regexp-in-string
1197 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1198 class-options header t nil 1)))))
1199 (if (not document-class-string)
1200 (user-error "Unknown LaTeX class `%s'" class)
1201 (org-latex-guess-babel-language
1202 (org-latex-guess-inputenc
1203 (org-element-normalize-string
1204 (org-splice-latex-header
1205 document-class-string
1206 org-latex-default-packages-alist
1207 org-latex-packages-alist nil
1208 (concat (org-element-normalize-string
1209 (plist-get info :latex-header))
1210 (plist-get info :latex-header-extra)))))
1211 info)))
1212 ;; Possibly limit depth for headline numbering.
1213 (let ((sec-num (plist-get info :section-numbers)))
1214 (when (integerp sec-num)
1215 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1216 ;; Author.
1217 (let ((author (and (plist-get info :with-author)
1218 (let ((auth (plist-get info :author)))
1219 (and auth (org-export-data auth info)))))
1220 (email (and (plist-get info :with-email)
1221 (org-export-data (plist-get info :email) info))))
1222 (cond ((and author email (not (string= "" email)))
1223 (format "\\author{%s\\thanks{%s}}\n" author email))
1224 ((or author email) (format "\\author{%s}\n" (or author email)))))
1225 ;; Date.
1226 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1227 (format "\\date{%s}\n" (org-export-data date info)))
1228 ;; Title
1229 (format "\\title{%s}\n" title)
1230 ;; Hyperref options.
1231 (format-spec (plist-get info :latex-hyperref-template)
1232 (format-spec-make
1233 ?k (or (plist-get info :keywords) "")
1234 ?d (or (plist-get info :description)"")
1235 ?c (if (plist-get info :with-creator)
1236 (plist-get info :creator)
1237 "")))
1238 ;; Document start.
1239 "\\begin{document}\n\n"
1240 ;; Title command.
1241 (let ((command (plist-get info :latex-title-command)))
1242 (org-element-normalize-string
1243 (cond ((string= "" title) nil)
1244 ((not (stringp command)) nil)
1245 ((string-match "\\(?:[^%]\\|^\\)%s" command)
1246 (format command title))
1247 (t command))))
1248 ;; Table of contents.
1249 (let ((depth (plist-get info :with-toc)))
1250 (when depth
1251 (concat (when (wholenump depth)
1252 (format "\\setcounter{tocdepth}{%d}\n" depth))
1253 (plist-get info :latex-toc-command))))
1254 ;; Document's body.
1255 contents
1256 ;; Creator.
1257 (let ((creator-info (plist-get info :with-creator)))
1258 (cond
1259 ((not creator-info) "")
1260 ((eq creator-info 'comment)
1261 (format "%% %s\n" (plist-get info :creator)))
1262 (t (concat (plist-get info :creator) "\n"))))
1263 ;; Document end.
1264 "\\end{document}")))
1268 ;;; Transcode Functions
1270 ;;;; Bold
1272 (defun org-latex-bold (bold contents info)
1273 "Transcode BOLD from Org to LaTeX.
1274 CONTENTS is the text with bold markup. INFO is a plist holding
1275 contextual information."
1276 (org-latex--text-markup contents 'bold info))
1279 ;;;; Center Block
1281 (defun org-latex-center-block (center-block contents info)
1282 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1283 CONTENTS holds the contents of the center block. INFO is a plist
1284 holding contextual information."
1285 (org-latex--wrap-label
1286 center-block
1287 (format "\\begin{center}\n%s\\end{center}" contents)))
1290 ;;;; Clock
1292 (defun org-latex-clock (clock contents info)
1293 "Transcode a CLOCK element from Org to LaTeX.
1294 CONTENTS is nil. INFO is a plist holding contextual
1295 information."
1296 (concat
1297 "\\noindent"
1298 (format "\\textbf{%s} " org-clock-string)
1299 (format (plist-get info :latex-inactive-timestamp-format)
1300 (concat (org-translate-time
1301 (org-element-property :raw-value
1302 (org-element-property :value clock)))
1303 (let ((time (org-element-property :duration clock)))
1304 (and time (format " (%s)" time)))))
1305 "\\\\"))
1308 ;;;; Code
1310 (defun org-latex-code (code contents info)
1311 "Transcode a CODE object from Org to LaTeX.
1312 CONTENTS is nil. INFO is a plist used as a communication
1313 channel."
1314 (org-latex--text-markup (org-element-property :value code) 'code info))
1317 ;;;; Drawer
1319 (defun org-latex-drawer (drawer contents info)
1320 "Transcode a DRAWER element from Org to LaTeX.
1321 CONTENTS holds the contents of the block. INFO is a plist
1322 holding contextual information."
1323 (let* ((name (org-element-property :drawer-name drawer))
1324 (output (funcall (plist-get info :latex-format-drawer-function)
1325 name contents)))
1326 (org-latex--wrap-label drawer output)))
1329 ;;;; Dynamic Block
1331 (defun org-latex-dynamic-block (dynamic-block contents info)
1332 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1333 CONTENTS holds the contents of the block. INFO is a plist
1334 holding contextual information. See `org-export-data'."
1335 (org-latex--wrap-label dynamic-block contents))
1338 ;;;; Entity
1340 (defun org-latex-entity (entity contents info)
1341 "Transcode an ENTITY object from Org to LaTeX.
1342 CONTENTS are the definition itself. INFO is a plist holding
1343 contextual information."
1344 (org-element-property :latex entity))
1347 ;;;; Example Block
1349 (defun org-latex-example-block (example-block contents info)
1350 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1351 CONTENTS is nil. INFO is a plist holding contextual
1352 information."
1353 (when (org-string-nw-p (org-element-property :value example-block))
1354 (org-latex--wrap-label
1355 example-block
1356 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1357 (org-export-format-code-default example-block info)))))
1360 ;;;; Export Block
1362 (defun org-latex-export-block (export-block contents info)
1363 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1364 CONTENTS is nil. INFO is a plist holding contextual information."
1365 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1366 (org-remove-indentation (org-element-property :value export-block))))
1369 ;;;; Export Snippet
1371 (defun org-latex-export-snippet (export-snippet contents info)
1372 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1373 CONTENTS is nil. INFO is a plist holding contextual information."
1374 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1375 (org-element-property :value export-snippet)))
1378 ;;;; Fixed Width
1380 (defun org-latex-fixed-width (fixed-width contents info)
1381 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1382 CONTENTS is nil. INFO is a plist holding contextual information."
1383 (org-latex--wrap-label
1384 fixed-width
1385 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1386 (org-remove-indentation
1387 (org-element-property :value fixed-width)))))
1390 ;;;; Footnote Reference
1392 (defun org-latex-footnote-reference (footnote-reference contents info)
1393 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1394 CONTENTS is nil. INFO is a plist holding contextual information."
1395 (concat
1396 ;; Insert separator between two footnotes in a row.
1397 (let ((prev (org-export-get-previous-element footnote-reference info)))
1398 (when (eq (org-element-type prev) 'footnote-reference)
1399 (plist-get info :latex-footnote-separator)))
1400 (cond
1401 ;; Use \footnotemark if the footnote has already been defined.
1402 ((not (org-export-footnote-first-reference-p footnote-reference info))
1403 (format "\\footnotemark[%s]{}"
1404 (org-export-get-footnote-number footnote-reference info)))
1405 ;; Use \footnotemark if reference is within another footnote
1406 ;; reference, footnote definition or table cell.
1407 ((loop for parent in (org-export-get-genealogy footnote-reference)
1408 thereis (memq (org-element-type parent)
1409 '(footnote-reference footnote-definition table-cell)))
1410 "\\footnotemark")
1411 ;; Otherwise, define it with \footnote command.
1413 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1414 (concat
1415 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1416 ;; Retrieve all footnote references within the footnote and
1417 ;; add their definition after it, since LaTeX doesn't support
1418 ;; them inside.
1419 (org-latex--delayed-footnotes-definitions def info)))))))
1422 ;;;; Headline
1424 (defun org-latex-headline (headline contents info)
1425 "Transcode a HEADLINE element from Org to LaTeX.
1426 CONTENTS holds the contents of the headline. INFO is a plist
1427 holding contextual information."
1428 (unless (org-element-property :footnote-section-p headline)
1429 (let* ((class (plist-get info :latex-class))
1430 (level (org-export-get-relative-level headline info))
1431 (numberedp (org-export-numbered-headline-p headline info))
1432 (class-sectioning (assoc class (plist-get info :latex-classes)))
1433 ;; Section formatting will set two placeholders: one for
1434 ;; the title and the other for the contents.
1435 (section-fmt
1436 (let ((sec (if (functionp (nth 2 class-sectioning))
1437 (funcall (nth 2 class-sectioning) level numberedp)
1438 (nth (1+ level) class-sectioning))))
1439 (cond
1440 ;; No section available for that LEVEL.
1441 ((not sec) nil)
1442 ;; Section format directly returned by a function. Add
1443 ;; placeholder for contents.
1444 ((stringp sec) (concat sec "\n%s"))
1445 ;; (numbered-section . unnumbered-section)
1446 ((not (consp (cdr sec)))
1447 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1448 ;; (numbered-open numbered-close)
1449 ((= (length sec) 2)
1450 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1451 ;; (num-in num-out no-num-in no-num-out)
1452 ((= (length sec) 4)
1453 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1454 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1455 ;; Create a temporary export back-end that hard-codes
1456 ;; "\underline" within "\section" and alike.
1457 (section-back-end
1458 (org-export-create-backend
1459 :parent 'latex
1460 :transcoders
1461 '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
1462 (text
1463 (org-export-data-with-backend
1464 (org-element-property :title headline) section-back-end info))
1465 (todo
1466 (and (plist-get info :with-todo-keywords)
1467 (let ((todo (org-element-property :todo-keyword headline)))
1468 (and todo (org-export-data todo info)))))
1469 (todo-type (and todo (org-element-property :todo-type headline)))
1470 (tags (and (plist-get info :with-tags)
1471 (org-export-get-tags headline info)))
1472 (priority (and (plist-get info :with-priority)
1473 (org-element-property :priority headline)))
1474 ;; Create the headline text along with a no-tag version.
1475 ;; The latter is required to remove tags from toc.
1476 (full-text (funcall (plist-get info :latex-format-headline-function)
1477 todo todo-type priority text tags info))
1478 ;; Associate \label to the headline for internal links.
1479 (headline-label
1480 (let ((custom-label
1481 (and (plist-get info :latex-custom-id-labels)
1482 (org-element-property :CUSTOM_ID headline))))
1483 (if custom-label (format "\\label{%s}\n" custom-label)
1484 (format "\\label{sec-%s}\n"
1485 (mapconcat
1486 #'number-to-string
1487 (org-export-get-headline-number headline info)
1488 "-")))))
1489 (pre-blanks
1490 (make-string (org-element-property :pre-blank headline) 10)))
1491 (if (or (not section-fmt) (org-export-low-level-p headline info))
1492 ;; This is a deep sub-tree: export it as a list item. Also
1493 ;; export as items headlines for which no section format has
1494 ;; been found.
1495 (let ((low-level-body
1496 (concat
1497 ;; If headline is the first sibling, start a list.
1498 (when (org-export-first-sibling-p headline info)
1499 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1500 ;; Itemize headline
1501 "\\item"
1502 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
1503 "\\relax")
1504 " " full-text "\n"
1505 headline-label
1506 pre-blanks
1507 contents)))
1508 ;; If headline is not the last sibling simply return
1509 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1510 ;; any blank line.
1511 (if (not (org-export-last-sibling-p headline info)) low-level-body
1512 (replace-regexp-in-string
1513 "[ \t\n]*\\'"
1514 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1515 low-level-body)))
1516 ;; This is a standard headline. Export it as a section. Add
1517 ;; an alternative heading when possible, and when this is not
1518 ;; identical to the usual heading.
1519 (let ((opt-title
1520 (funcall (plist-get info :latex-format-headline-function)
1521 todo todo-type priority
1522 (org-export-data-with-backend
1523 (org-export-get-alt-title headline info)
1524 section-back-end info)
1525 (and (eq (plist-get info :with-tags) t) tags)
1526 info)))
1527 (if (and numberedp opt-title
1528 (not (equal opt-title full-text))
1529 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
1530 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1531 ;; Replace square brackets with parenthesis
1532 ;; since square brackets are not supported in
1533 ;; optional arguments.
1534 (replace-regexp-in-string
1535 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1536 full-text
1537 (concat headline-label pre-blanks contents))
1538 ;; Impossible to add an alternative heading. Fallback to
1539 ;; regular sectioning format string.
1540 (format section-fmt full-text
1541 (concat headline-label pre-blanks contents))))))))
1543 (defun org-latex-format-headline-default-function
1544 (todo todo-type priority text tags info)
1545 "Default format function for a headline.
1546 See `org-latex-format-headline-function' for details."
1547 (concat
1548 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1549 (and priority (format "\\framebox{\\#%c} " priority))
1550 text
1551 (and tags
1552 (format "\\hfill{}\\textsc{%s}"
1553 (mapconcat (lambda (tag) (org-latex-plain-text tag info))
1554 tags ":")))))
1557 ;;;; Horizontal Rule
1559 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1560 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1561 CONTENTS is nil. INFO is a plist holding contextual information."
1562 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1563 (prev (org-export-get-previous-element horizontal-rule info)))
1564 (concat
1565 ;; Make sure the rule doesn't start at the end of the current
1566 ;; line by separating it with a blank line from previous element.
1567 (when (and prev
1568 (let ((prev-blank (org-element-property :post-blank prev)))
1569 (or (not prev-blank) (zerop prev-blank))))
1570 "\n")
1571 (org-latex--wrap-label
1572 horizontal-rule
1573 (format "\\rule{%s}{%s}"
1574 (or (plist-get attr :width) "\\linewidth")
1575 (or (plist-get attr :thickness) "0.5pt"))))))
1578 ;;;; Inline Src Block
1580 (defun org-latex-inline-src-block (inline-src-block contents info)
1581 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1582 CONTENTS holds the contents of the item. INFO is a plist holding
1583 contextual information."
1584 (let* ((code (org-element-property :value inline-src-block))
1585 (separator (org-latex--find-verb-separator code)))
1586 (case (plist-get info :latex-listings)
1587 ;; Do not use a special package: transcode it verbatim.
1588 ((nil) (concat "\\verb" separator code separator))
1589 ;; Use minted package.
1590 (minted
1591 (let* ((org-lang (org-element-property :language inline-src-block))
1592 (mint-lang (or (cadr (assq (intern org-lang)
1593 (plist-get info :latex-minted-langs)))
1594 (downcase org-lang)))
1595 (options (org-latex--make-option-string
1596 (plist-get info :latex-minted-options))))
1597 (concat (format "\\mint%s{%s}"
1598 (if (string= options "") "" (format "[%s]" options))
1599 mint-lang)
1600 separator code separator)))
1601 ;; Use listings package.
1602 (otherwise
1603 ;; Maybe translate language's name.
1604 (let* ((org-lang (org-element-property :language inline-src-block))
1605 (lst-lang (or (cadr (assq (intern org-lang)
1606 (plist-get info :latex-listings-langs)))
1607 org-lang))
1608 (options (org-latex--make-option-string
1609 (append (plist-get info :latex-listings-options)
1610 `(("language" ,lst-lang))))))
1611 (concat (format "\\lstinline[%s]" options)
1612 separator code separator))))))
1615 ;;;; Inlinetask
1617 (defun org-latex-inlinetask (inlinetask contents info)
1618 "Transcode an INLINETASK element from Org to LaTeX.
1619 CONTENTS holds the contents of the block. INFO is a plist
1620 holding contextual information."
1621 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1622 (todo (and (plist-get info :with-todo-keywords)
1623 (let ((todo (org-element-property :todo-keyword inlinetask)))
1624 (and todo (org-export-data todo info)))))
1625 (todo-type (org-element-property :todo-type inlinetask))
1626 (tags (and (plist-get info :with-tags)
1627 (org-export-get-tags inlinetask info)))
1628 (priority (and (plist-get info :with-priority)
1629 (org-element-property :priority inlinetask)))
1630 (contents (concat
1631 (let ((label (org-element-property :CUSTOM_ID inlinetask)))
1632 (and label (format "\\label{%s}\n" label)))
1633 contents)))
1634 (funcall (plist-get info :latex-format-inlinetask-function)
1635 todo todo-type priority title tags contents info)))
1637 (defun org-latex-format-inlinetask-default-function
1638 (todo todo-type priority title tags contents info)
1639 "Default format function for a inlinetasks.
1640 See `org-latex-format-inlinetask-function' for details."
1641 (let ((full-title
1642 (concat (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1643 (when priority (format "\\framebox{\\#%c} " priority))
1644 title
1645 (when tags
1646 (format "\\hfill{}\\textsc{:%s:}"
1647 (mapconcat
1648 (lambda (tag) (org-latex-plain-text tag info))
1649 tags ":"))))))
1650 (concat "\\begin{center}\n"
1651 "\\fbox{\n"
1652 "\\begin{minipage}[c]{.6\\textwidth}\n"
1653 full-title "\n\n"
1654 (and (org-string-nw-p contents)
1655 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
1656 "\\end{minipage}\n"
1657 "}\n"
1658 "\\end{center}")))
1661 ;;;; Italic
1663 (defun org-latex-italic (italic contents info)
1664 "Transcode ITALIC from Org to LaTeX.
1665 CONTENTS is the text with italic markup. INFO is a plist holding
1666 contextual information."
1667 (org-latex--text-markup contents 'italic info))
1670 ;;;; Item
1672 (defun org-latex-item (item contents info)
1673 "Transcode an ITEM element from Org to LaTeX.
1674 CONTENTS holds the contents of the item. INFO is a plist holding
1675 contextual information."
1676 (let* ((counter
1677 (let ((count (org-element-property :counter item))
1678 (level
1679 ;; Determine level of current item to determine the
1680 ;; correct LaTeX counter to use (enumi, enumii...).
1681 (let ((parent item) (level 0))
1682 (while (memq (org-element-type
1683 (setq parent (org-export-get-parent parent)))
1684 '(plain-list item))
1685 (when (and (eq (org-element-type parent) 'plain-list)
1686 (eq (org-element-property :type parent)
1687 'ordered))
1688 (incf level)))
1689 level)))
1690 (and count
1691 (< level 5)
1692 (format "\\setcounter{enum%s}{%s}\n"
1693 (nth (1- level) '("i" "ii" "iii" "iv"))
1694 (1- count)))))
1695 (checkbox (case (org-element-property :checkbox item)
1696 (on "$\\boxtimes$ ")
1697 (off "$\\square$ ")
1698 (trans "$\\boxminus$ ")))
1699 (tag (let ((tag (org-element-property :tag item)))
1700 ;; Check-boxes must belong to the tag.
1701 (and tag (format "[{%s}] "
1702 (concat checkbox
1703 (org-export-data tag info)))))))
1704 (concat counter
1705 "\\item"
1706 (cond
1707 (tag)
1708 (checkbox (concat " " checkbox))
1709 ;; Without a tag or a check-box, if CONTENTS starts with
1710 ;; an opening square bracket, add "\relax" to "\item",
1711 ;; unless the brackets comes from an initial export
1712 ;; snippet (i.e. it is inserted willingly by the user).
1713 ((and contents
1714 (org-string-match-p "\\`[ \t]*\\[" contents)
1715 (not (let ((e (car (org-element-contents item))))
1716 (and (eq (org-element-type e) 'paragraph)
1717 (let ((o (car (org-element-contents e))))
1718 (and (eq (org-element-type o) 'export-snippet)
1719 (eq (org-export-snippet-backend o)
1720 'latex)))))))
1721 "\\relax ")
1722 (t " "))
1723 (and contents (org-trim contents))
1724 ;; If there are footnotes references in tag, be sure to
1725 ;; add their definition at the end of the item. This
1726 ;; workaround is necessary since "\footnote{}" command is
1727 ;; not supported in tags.
1728 (and tag
1729 (org-latex--delayed-footnotes-definitions
1730 (org-element-property :tag item) info)))))
1733 ;;;; Keyword
1735 (defun org-latex-keyword (keyword contents info)
1736 "Transcode a KEYWORD element from Org to LaTeX.
1737 CONTENTS is nil. INFO is a plist holding contextual information."
1738 (let ((key (org-element-property :key keyword))
1739 (value (org-element-property :value keyword)))
1740 (cond
1741 ((string= key "LATEX") value)
1742 ((string= key "INDEX") (format "\\index{%s}" value))
1743 ((string= key "TOC")
1744 (let ((value (downcase value)))
1745 (cond
1746 ((string-match "\\<headlines\\>" value)
1747 (let ((depth (or (and (string-match "[0-9]+" value)
1748 (string-to-number (match-string 0 value)))
1749 (plist-get info :with-toc))))
1750 (concat
1751 (when (wholenump depth)
1752 (format "\\setcounter{tocdepth}{%s}\n" depth))
1753 "\\tableofcontents")))
1754 ((string= "tables" value) "\\listoftables")
1755 ((string= "listings" value)
1756 (case (plist-get info :latex-listings)
1757 ((nil) "\\listoffigures")
1758 (minted "\\listoflistings")
1759 (otherwise "\\lstlistoflistings")))))))))
1762 ;;;; Latex Environment
1764 (defun org-latex-latex-environment (latex-environment contents info)
1765 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1766 CONTENTS is nil. INFO is a plist holding contextual information."
1767 (when (plist-get info :with-latex)
1768 (let ((label (org-element-property :name latex-environment))
1769 (value (org-remove-indentation
1770 (org-element-property :value latex-environment))))
1771 (if (not (org-string-nw-p label)) value
1772 ;; Environment is labeled: label must be within the environment
1773 ;; (otherwise, a reference pointing to that element will count
1774 ;; the section instead).
1775 (with-temp-buffer
1776 (insert value)
1777 (goto-char (point-min))
1778 (forward-line)
1779 (insert
1780 (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1781 (buffer-string))))))
1784 ;;;; Latex Fragment
1786 (defun org-latex-latex-fragment (latex-fragment contents info)
1787 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1788 CONTENTS is nil. INFO is a plist holding contextual information."
1789 (let ((value (org-element-property :value latex-fragment)))
1790 ;; Trim math markers since the fragment is enclosed within
1791 ;; a latex-math-block object anyway.
1792 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
1793 (match-string 2 value))
1794 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
1795 (match-string 1 value))
1796 (t value))))
1799 ;;;; Line Break
1801 (defun org-latex-line-break (line-break contents info)
1802 "Transcode a LINE-BREAK object from Org to LaTeX.
1803 CONTENTS is nil. INFO is a plist holding contextual information."
1804 "\\\\\n")
1807 ;;;; Link
1809 (defun org-latex--inline-image (link info)
1810 "Return LaTeX code for an inline image.
1811 LINK is the link pointing to the inline image. INFO is a plist
1812 used as a communication channel."
1813 (let* ((parent (org-export-get-parent-element link))
1814 (path (let ((raw-path (org-element-property :path link)))
1815 (if (not (file-name-absolute-p raw-path)) raw-path
1816 (expand-file-name raw-path))))
1817 (filetype (file-name-extension path))
1818 (caption (org-latex--caption/label-string parent info))
1819 ;; Retrieve latex attributes from the element around.
1820 (attr (org-export-read-attribute :attr_latex parent))
1821 (float (let ((float (plist-get attr :float)))
1822 (cond ((and (not float) (plist-member attr :float)) nil)
1823 ((string= float "wrap") 'wrap)
1824 ((string= float "sideways") 'sideways)
1825 ((string= float "multicolumn") 'multicolumn)
1826 ((or float
1827 (org-element-property :caption parent)
1828 (org-string-nw-p (plist-get attr :caption)))
1829 'figure))))
1830 (placement
1831 (let ((place (plist-get attr :placement)))
1832 (cond
1833 (place (format "%s" place))
1834 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1835 ((eq float 'figure)
1836 (format "[%s]" (plist-get info :latex-default-figure-position)))
1837 (t ""))))
1838 (comment-include (if (plist-get attr :comment-include) "%" ""))
1839 ;; It is possible to specify width and height in the
1840 ;; ATTR_LATEX line, and also via default variables.
1841 (width (cond ((plist-get attr :width))
1842 ((plist-get attr :height) "")
1843 ((eq float 'wrap) "0.48\\textwidth")
1844 (t (plist-get info :latex-image-default-width))))
1845 (height (cond ((plist-get attr :height))
1846 ((or (plist-get attr :width)
1847 (memq float '(figure wrap))) "")
1848 (t (plist-get info :latex-image-default-height))))
1849 (options (let ((opt (or (plist-get attr :options)
1850 (plist-get info :latex-image-default-option))))
1851 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
1852 (match-string 1 opt))))
1853 image-code)
1854 (if (member filetype '("tikz" "pgf"))
1855 ;; For tikz images:
1856 ;; - use \input to read in image file.
1857 ;; - if options are present, wrap in a tikzpicture environment.
1858 ;; - if width or height are present, use \resizebox to change
1859 ;; the image size.
1860 (progn
1861 (setq image-code (format "\\input{%s}" path))
1862 (when (org-string-nw-p options)
1863 (setq image-code
1864 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1865 options
1866 image-code)))
1867 (when (or (org-string-nw-p width) (org-string-nw-p height))
1868 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
1869 (if (org-string-nw-p width) width "!")
1870 (if (org-string-nw-p height) height "!")
1871 image-code))))
1872 ;; For other images:
1873 ;; - add width and height to options.
1874 ;; - include the image with \includegraphics.
1875 (when (org-string-nw-p width)
1876 (setq options (concat options ",width=" width)))
1877 (when (org-string-nw-p height)
1878 (setq options (concat options ",height=" height)))
1879 (let ((search-option (org-element-property :search-option link)))
1880 (when (and search-option
1881 (equal filetype "pdf")
1882 (org-string-match-p "\\`[0-9]+\\'" search-option)
1883 (not (org-string-match-p "page=" options)))
1884 (setq options (concat options ",page=" search-option))))
1885 (setq image-code
1886 (format "\\includegraphics%s{%s}"
1887 (cond ((not (org-string-nw-p options)) "")
1888 ((= (aref options 0) ?,)
1889 (format "[%s]"(substring options 1)))
1890 (t (format "[%s]" options)))
1891 path))
1892 (when (equal filetype "svg")
1893 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
1894 "\\includesvg"
1895 image-code
1896 nil t))
1897 (setq image-code (replace-regexp-in-string "\\.svg}"
1899 image-code
1900 nil t))))
1901 ;; Return proper string, depending on FLOAT.
1902 (case float
1903 (wrap (format "\\begin{wrapfigure}%s
1904 \\centering
1905 %s%s
1906 %s\\end{wrapfigure}" placement comment-include image-code caption))
1907 (sideways (format "\\begin{sidewaysfigure}
1908 \\centering
1909 %s%s
1910 %s\\end{sidewaysfigure}" comment-include image-code caption))
1911 (multicolumn (format "\\begin{figure*}%s
1912 \\centering
1913 %s%s
1914 %s\\end{figure*}" placement comment-include image-code caption))
1915 (figure (format "\\begin{figure}%s
1916 \\centering
1917 %s%s
1918 %s\\end{figure}" placement comment-include image-code caption))
1919 (otherwise image-code))))
1921 (defun org-latex-link (link desc info)
1922 "Transcode a LINK object from Org to LaTeX.
1924 DESC is the description part of the link, or the empty string.
1925 INFO is a plist holding contextual information. See
1926 `org-export-data'."
1927 (let* ((type (org-element-property :type link))
1928 (raw-path (replace-regexp-in-string
1929 "%" "\\%" (org-element-property :path link) nil t))
1930 ;; Ensure DESC really exists, or set it to nil.
1931 (desc (and (not (string= desc "")) desc))
1932 (imagep (org-export-inline-image-p
1933 link (plist-get info :latex-inline-image-rules)))
1934 (path (cond
1935 ((member type '("http" "https" "ftp" "mailto"))
1936 (concat type ":" raw-path))
1937 ((and (string= type "file") (file-name-absolute-p raw-path))
1938 (concat "file:" raw-path))
1939 (t raw-path)))
1940 protocol)
1941 (cond
1942 ;; Image file.
1943 (imagep (org-latex--inline-image link info))
1944 ;; Radio link: Transcode target's contents and use them as link's
1945 ;; description.
1946 ((string= type "radio")
1947 (let ((destination (org-export-resolve-radio-link link info)))
1948 (if (not destination) desc
1949 (format "\\hyperref[%s]{%s}"
1950 (org-export-solidify-link-text
1951 (org-element-property :value destination))
1952 desc))))
1953 ;; Links pointing to a headline: Find destination and build
1954 ;; appropriate referencing command.
1955 ((member type '("custom-id" "fuzzy" "id"))
1956 (let ((destination (if (string= type "fuzzy")
1957 (org-export-resolve-fuzzy-link link info)
1958 (org-export-resolve-id-link link info))))
1959 (case (org-element-type destination)
1960 ;; Id link points to an external file.
1961 (plain-text
1962 (if desc (format "\\href{%s}{%s}" destination desc)
1963 (format "\\url{%s}" destination)))
1964 ;; Fuzzy link points nowhere.
1965 ('nil
1966 (format (plist-get info :latex-link-with-unknown-path-format)
1967 (or desc
1968 (org-export-data
1969 (org-element-property :raw-link link) info))))
1970 ;; LINK points to a headline. If headlines are numbered
1971 ;; and the link has no description, display headline's
1972 ;; number. Otherwise, display description or headline's
1973 ;; title.
1974 (headline
1975 (let* ((custom-label
1976 (and (plist-get info :latex-custom-id-labels)
1977 (org-element-property :CUSTOM_ID destination)))
1978 (label
1980 custom-label
1981 (format "sec-%s"
1982 (mapconcat
1983 #'number-to-string
1984 (org-export-get-headline-number destination info)
1985 "-")))))
1986 (if (and (not desc)
1987 (org-export-numbered-headline-p destination info))
1988 (format "\\ref{%s}" label)
1989 (format "\\hyperref[%s]{%s}" label
1990 (or desc
1991 (org-export-data
1992 (org-element-property :title destination) info))))))
1993 ;; Fuzzy link points to a target. Do as above.
1994 (otherwise
1995 (let ((path (org-export-solidify-link-text path)))
1996 (if (not desc) (format "\\ref{%s}" path)
1997 (format "\\hyperref[%s]{%s}" path desc)))))))
1998 ;; Coderef: replace link with the reference name or the
1999 ;; equivalent line number.
2000 ((string= type "coderef")
2001 (format (org-export-get-coderef-format path desc)
2002 (org-export-resolve-coderef path info)))
2003 ;; Link type is handled by a special function.
2004 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
2005 (funcall protocol (org-link-unescape path) desc 'latex))
2006 ;; External link with a description part.
2007 ((and path desc) (format "\\href{%s}{%s}" path desc))
2008 ;; External link without a description part.
2009 (path (format "\\url{%s}" path))
2010 ;; No path, only description. Try to do something useful.
2011 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
2014 ;;;; Node Property
2016 (defun org-latex-node-property (node-property contents info)
2017 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2018 CONTENTS is nil. INFO is a plist holding contextual
2019 information."
2020 (format "%s:%s"
2021 (org-element-property :key node-property)
2022 (let ((value (org-element-property :value node-property)))
2023 (if value (concat " " value) ""))))
2026 ;;;; Paragraph
2028 (defun org-latex-paragraph (paragraph contents info)
2029 "Transcode a PARAGRAPH element from Org to LaTeX.
2030 CONTENTS is the contents of the paragraph, as a string. INFO is
2031 the plist used as a communication channel."
2032 contents)
2035 ;;;; Plain List
2037 (defun org-latex-plain-list (plain-list contents info)
2038 "Transcode a PLAIN-LIST element from Org to LaTeX.
2039 CONTENTS is the contents of the list. INFO is a plist holding
2040 contextual information."
2041 (let* ((type (org-element-property :type plain-list))
2042 (attr (org-export-read-attribute :attr_latex plain-list))
2043 (latex-type (let ((env (plist-get attr :environment)))
2044 (cond (env (format "%s" env))
2045 ((eq type 'ordered) "enumerate")
2046 ((eq type 'descriptive) "description")
2047 (t "itemize")))))
2048 (org-latex--wrap-label
2049 plain-list
2050 (format "\\begin{%s}%s\n%s\\end{%s}"
2051 latex-type
2052 (or (plist-get attr :options) "")
2053 contents
2054 latex-type))))
2057 ;;;; Plain Text
2059 (defun org-latex-plain-text (text info)
2060 "Transcode a TEXT string from Org to LaTeX.
2061 TEXT is the string to transcode. INFO is a plist holding
2062 contextual information."
2063 (let* ((specialp (plist-get info :with-special-strings))
2064 (output
2065 ;; Turn LaTeX into \LaTeX{} and TeX into \TeX{}.
2066 (let ((case-fold-search nil))
2067 (replace-regexp-in-string
2068 "\\<\\(?:La\\)?TeX\\>" "\\\\\\&{}"
2069 ;; Protect ^, ~, %, #, &, $, _, { and }. Also protect \.
2070 ;; However, if special strings are used, be careful not
2071 ;; to protect "\" in "\-" constructs.
2072 (replace-regexp-in-string
2073 (concat "[%$#&{}_~^]\\|\\\\" (and specialp "\\(?:[^-]\\|$\\)"))
2074 (lambda (m)
2075 (case (aref m 0)
2076 (?\\ "$\\\\backslash$")
2077 (?~ "\\\\textasciitilde{}")
2078 (?^ "\\\\^{}")
2079 (t "\\\\\\&")))
2080 text)))))
2081 ;; Activate smart quotes. Be sure to provide original TEXT string
2082 ;; since OUTPUT may have been modified.
2083 (when (plist-get info :with-smart-quotes)
2084 (setq output (org-export-activate-smart-quotes output :latex info text)))
2085 ;; Convert special strings.
2086 (when specialp
2087 (setq output (replace-regexp-in-string "\\.\\.\\." "\\\\ldots{}" output)))
2088 ;; Handle break preservation if required.
2089 (when (plist-get info :preserve-breaks)
2090 (setq output (replace-regexp-in-string
2091 "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
2092 ;; Return value.
2093 output))
2096 ;;;; Planning
2098 (defun org-latex-planning (planning contents info)
2099 "Transcode a PLANNING element from Org to LaTeX.
2100 CONTENTS is nil. INFO is a plist holding contextual
2101 information."
2102 (concat
2103 "\\noindent"
2104 (mapconcat
2105 'identity
2106 (delq nil
2107 (list
2108 (let ((closed (org-element-property :closed planning)))
2109 (when closed
2110 (concat
2111 (format "\\textbf{%s} " org-closed-string)
2112 (format (plist-get info :latex-inactive-timestamp-format)
2113 (org-translate-time
2114 (org-element-property :raw-value closed))))))
2115 (let ((deadline (org-element-property :deadline planning)))
2116 (when deadline
2117 (concat
2118 (format "\\textbf{%s} " org-deadline-string)
2119 (format (plist-get info :latex-active-timestamp-format)
2120 (org-translate-time
2121 (org-element-property :raw-value deadline))))))
2122 (let ((scheduled (org-element-property :scheduled planning)))
2123 (when scheduled
2124 (concat
2125 (format "\\textbf{%s} " org-scheduled-string)
2126 (format (plist-get info :latex-active-timestamp-format)
2127 (org-translate-time
2128 (org-element-property :raw-value scheduled))))))))
2129 " ")
2130 "\\\\"))
2133 ;;;; Property Drawer
2135 (defun org-latex-property-drawer (property-drawer contents info)
2136 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2137 CONTENTS holds the contents of the drawer. INFO is a plist
2138 holding contextual information."
2139 (and (org-string-nw-p contents)
2140 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2143 ;;;; Pseudo Object: LaTeX Math Block
2145 (defun org-latex--wrap-latex-math-block (data info)
2146 "Merge contiguous math objects in a pseudo-object container.
2147 DATA is a parse tree or a secondary string. INFO is a plist
2148 containing export options. Modify DATA by side-effect and return it."
2149 (let ((valid-object-p
2150 (function
2151 ;; Non-nil when OBJ can be added to the latex math block.
2152 (lambda (obj)
2153 (case (org-element-type obj)
2154 (entity (org-element-property :latex-math-p obj))
2155 (latex-fragment
2156 (let ((value (org-element-property :value obj)))
2157 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2158 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2159 ((subscript superscript) t))))))
2160 (org-element-map data '(entity latex-fragment subscript superscript)
2161 (lambda (object)
2162 ;; Skip objects already wrapped.
2163 (when (and (not (eq (org-element-type
2164 (org-element-property :parent object))
2165 'latex-math-block))
2166 (funcall valid-object-p object))
2167 (let ((math-block (list 'latex-math-block nil))
2168 (next-elements (org-export-get-next-element object info t))
2169 (last object))
2170 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2171 (org-element-insert-before math-block object)
2172 (org-element-extract-element object)
2173 (org-element-adopt-elements math-block object)
2174 (when (zerop (or (org-element-property :post-blank object) 0))
2175 ;; MATH-BLOCK swallows consecutive math objects.
2176 (catch 'exit
2177 (dolist (next next-elements)
2178 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2179 (org-element-extract-element next)
2180 (org-element-adopt-elements math-block next)
2181 ;; Eschew the case: \beta$x$ -> \(\betax\).
2182 (unless (memq (org-element-type next)
2183 '(subscript superscript))
2184 (org-element-put-property last :post-blank 1))
2185 (setq last next)
2186 (when (> (or (org-element-property :post-blank next) 0) 0)
2187 (throw 'exit nil))))))
2188 (org-element-put-property
2189 math-block :post-blank (org-element-property :post-blank last)))))
2190 info nil '(subscript superscript latex-math-block) t)
2191 ;; Return updated DATA.
2192 data))
2194 (defun org-latex-math-block-tree-filter (tree backend info)
2195 (org-latex--wrap-latex-math-block tree info))
2197 (defun org-latex-math-block-options-filter (info backend)
2198 (dolist (prop '(:author :date :title) info)
2199 (plist-put info prop
2200 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2202 (defun org-latex-math-block (math-block contents info)
2203 "Transcode a MATH-BLOCK object from Org to LaTeX.
2204 CONTENTS is a string. INFO is a plist used as a communication
2205 channel."
2206 (when (org-string-nw-p contents)
2207 (format "\\(%s\\)" (org-trim contents))))
2209 ;;;; Quote Block
2211 (defun org-latex-quote-block (quote-block contents info)
2212 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2213 CONTENTS holds the contents of the block. INFO is a plist
2214 holding contextual information."
2215 (org-latex--wrap-label
2216 quote-block
2217 (format "\\begin{quote}\n%s\\end{quote}" contents)))
2220 ;;;; Radio Target
2222 (defun org-latex-radio-target (radio-target text info)
2223 "Transcode a RADIO-TARGET object from Org to LaTeX.
2224 TEXT is the text of the target. INFO is a plist holding
2225 contextual information."
2226 (format "\\label{%s}%s"
2227 (org-export-solidify-link-text
2228 (org-element-property :value radio-target))
2229 text))
2232 ;;;; Section
2234 (defun org-latex-section (section contents info)
2235 "Transcode a SECTION element from Org to LaTeX.
2236 CONTENTS holds the contents of the section. INFO is a plist
2237 holding contextual information."
2238 contents)
2241 ;;;; Special Block
2243 (defun org-latex-special-block (special-block contents info)
2244 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2245 CONTENTS holds the contents of the block. INFO is a plist
2246 holding contextual information."
2247 (let ((type (org-element-property :type special-block))
2248 (opt (org-export-read-attribute :attr_latex special-block :options)))
2249 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2250 ;; Insert any label or caption within the block
2251 ;; (otherwise, a reference pointing to that element will
2252 ;; count the section instead).
2253 (org-latex--caption/label-string special-block info)
2254 contents
2255 (format "\\end{%s}" type))))
2258 ;;;; Src Block
2260 (defun org-latex-src-block (src-block contents info)
2261 "Transcode a SRC-BLOCK element from Org to LaTeX.
2262 CONTENTS holds the contents of the item. INFO is a plist holding
2263 contextual information."
2264 (when (org-string-nw-p (org-element-property :value src-block))
2265 (let* ((lang (org-element-property :language src-block))
2266 (caption (org-element-property :caption src-block))
2267 (label (org-element-property :name src-block))
2268 (custom-env (and lang
2269 (cadr (assq (intern lang)
2270 org-latex-custom-lang-environments))))
2271 (num-start (case (org-element-property :number-lines src-block)
2272 (continued (org-export-get-loc src-block info))
2273 (new 0)))
2274 (retain-labels (org-element-property :retain-labels src-block))
2275 (attributes (org-export-read-attribute :attr_latex src-block))
2276 (float (plist-get attributes :float))
2277 (listings (plist-get info :latex-listings)))
2278 (cond
2279 ;; Case 1. No source fontification.
2280 ((not listings)
2281 (let* ((caption-str (org-latex--caption/label-string src-block info))
2282 (float-env
2283 (cond ((and (not float) (plist-member attributes :float)) "%s")
2284 ((string= "multicolumn" float)
2285 (format "\\begin{figure*}[%s]\n%%s%s\n\\end{figure*}"
2286 (plist-get info :latex-default-figure-position)
2287 caption-str))
2288 ((or caption float)
2289 (format "\\begin{figure}[H]\n%%s%s\n\\end{figure}"
2290 caption-str))
2291 (t "%s"))))
2292 (format
2293 float-env
2294 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2295 (org-export-format-code-default src-block info))))))
2296 ;; Case 2. Custom environment.
2297 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2298 custom-env
2299 (org-export-format-code-default src-block info)
2300 custom-env))
2301 ;; Case 3. Use minted package.
2302 ((eq listings 'minted)
2303 (let* ((caption-str (org-latex--caption/label-string src-block info))
2304 (float-env
2305 (cond ((and (not float) (plist-member attributes :float) caption)
2306 (format "%%s\n%s" (replace-regexp-in-string
2307 "\\\\caption" "\\captionof{listing}"
2308 caption-str t t)))
2309 ((and (not float) (plist-member attributes :float)) "%s")
2310 ((string= "multicolumn" float)
2311 (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
2312 caption-str))
2313 ((or caption float)
2314 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2315 caption-str))
2316 (t "%s")))
2317 (options (plist-get info :latex-minted-options))
2318 (body
2319 (format
2320 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2321 ;; Options.
2322 (concat
2323 (org-latex--make-option-string
2324 (if (or (not num-start) (assoc "linenos" options))
2325 options
2326 (append
2327 `(("linenos")
2328 ("firstnumber" ,(number-to-string (1+ num-start))))
2329 options)))
2330 (let ((local-options (plist-get attributes :options)))
2331 (and local-options (concat "," local-options))))
2332 ;; Language.
2333 (or (cadr (assq (intern lang)
2334 (plist-get info :latex-minted-langs)))
2335 (downcase lang))
2336 ;; Source code.
2337 (let* ((code-info (org-export-unravel-code src-block))
2338 (max-width
2339 (apply 'max
2340 (mapcar 'length
2341 (org-split-string (car code-info)
2342 "\n")))))
2343 (org-export-format-code
2344 (car code-info)
2345 (lambda (loc num ref)
2346 (concat
2348 (when ref
2349 ;; Ensure references are flushed to the right,
2350 ;; separated with 6 spaces from the widest line
2351 ;; of code.
2352 (concat (make-string (+ (- max-width (length loc)) 6)
2353 ?\s)
2354 (format "(%s)" ref)))))
2355 nil (and retain-labels (cdr code-info)))))))
2356 ;; Return value.
2357 (format float-env body)))
2358 ;; Case 4. Use listings package.
2360 (let ((lst-lang
2361 (or (cadr (assq (intern lang)
2362 (plist-get info :latex-listings-langs)))
2363 lang))
2364 (caption-str
2365 (when caption
2366 (let ((main (org-export-get-caption src-block))
2367 (secondary (org-export-get-caption src-block t)))
2368 (if (not secondary)
2369 (format "{%s}" (org-export-data main info))
2370 (format "{[%s]%s}"
2371 (org-export-data secondary info)
2372 (org-export-data main info))))))
2373 (lst-opt (plist-get info :latex-listings-options)))
2374 (concat
2375 ;; Options.
2376 (format
2377 "\\lstset{%s}\n"
2378 (concat
2379 (org-latex--make-option-string
2380 (append
2381 lst-opt
2382 (cond
2383 ((and (not float) (plist-member attributes :float)) nil)
2384 ((string= "multicolumn" float) '(("float" "*")))
2385 ((and float (not (assoc "float" lst-opt)))
2386 `(("float" ,(plist-get info :latex-default-figure-position)))))
2387 `(("language" ,lst-lang))
2388 (if label `(("label" ,label)) '(("label" " ")))
2389 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2390 (cond ((assoc "numbers" lst-opt) nil)
2391 ((not num-start) '(("numbers" "none")))
2392 ((zerop num-start) '(("numbers" "left")))
2393 (t `(("numbers" "left")
2394 ("firstnumber"
2395 ,(number-to-string (1+ num-start))))))))
2396 (let ((local-options (plist-get attributes :options)))
2397 (and local-options (concat "," local-options)))))
2398 ;; Source code.
2399 (format
2400 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2401 (let* ((code-info (org-export-unravel-code src-block))
2402 (max-width
2403 (apply 'max
2404 (mapcar 'length
2405 (org-split-string (car code-info) "\n")))))
2406 (org-export-format-code
2407 (car code-info)
2408 (lambda (loc num ref)
2409 (concat
2411 (when ref
2412 ;; Ensure references are flushed to the right,
2413 ;; separated with 6 spaces from the widest line of
2414 ;; code
2415 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2416 (format "(%s)" ref)))))
2417 nil (and retain-labels (cdr code-info))))))))))))
2420 ;;;; Statistics Cookie
2422 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2423 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2424 CONTENTS is nil. INFO is a plist holding contextual information."
2425 (replace-regexp-in-string
2426 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2429 ;;;; Strike-Through
2431 (defun org-latex-strike-through (strike-through contents info)
2432 "Transcode STRIKE-THROUGH from Org to LaTeX.
2433 CONTENTS is the text with strike-through markup. INFO is a plist
2434 holding contextual information."
2435 (org-latex--text-markup contents 'strike-through info))
2438 ;;;; Subscript
2440 (defun org-latex--script-size (object info)
2441 "Transcode a subscript or superscript object.
2442 OBJECT is an Org object. INFO is a plist used as a communication
2443 channel."
2444 (let ((type (org-element-type object))
2445 (output ""))
2446 (org-element-map (org-element-contents object)
2447 (cons 'plain-text org-element-all-objects)
2448 (lambda (obj)
2449 (case (org-element-type obj)
2450 ((entity latex-fragment)
2451 (let ((data (org-trim (org-export-data obj info))))
2452 (string-match
2453 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2454 data)
2455 (setq output
2456 (concat output
2457 (match-string 1 data)
2458 (let ((blank (org-element-property :post-blank obj)))
2459 (and blank (> blank 0) "\\ "))))))
2460 (plain-text
2461 (setq output
2462 (format "%s\\text{%s}" output (org-export-data obj info))))
2463 (otherwise
2464 (setq output
2465 (concat output
2466 (org-export-data obj info)
2467 (let ((blank (org-element-property :post-blank obj)))
2468 (and blank (> blank 0) "\\ ")))))))
2469 info nil org-element-recursive-objects)
2470 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2471 ;; character.
2472 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2473 (and (> (length output) 1) "{")
2474 output
2475 (and (> (length output) 1) "}"))))
2477 (defun org-latex-subscript (subscript contents info)
2478 "Transcode a SUBSCRIPT object from Org to LaTeX.
2479 CONTENTS is the contents of the object. INFO is a plist holding
2480 contextual information."
2481 (org-latex--script-size subscript info))
2484 ;;;; Superscript
2486 (defun org-latex-superscript (superscript contents info)
2487 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2488 CONTENTS is the contents of the object. INFO is a plist holding
2489 contextual information."
2490 (org-latex--script-size superscript info))
2493 ;;;; Table
2495 ;; `org-latex-table' is the entry point for table transcoding. It
2496 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2497 ;; delegates the job to either `org-latex--table.el-table',
2498 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2499 ;; depending of the type of the table and the mode requested.
2501 ;; `org-latex--align-string' is a subroutine used to build alignment
2502 ;; string for Org tables.
2504 (defun org-latex-table (table contents info)
2505 "Transcode a TABLE element from Org to LaTeX.
2506 CONTENTS is the contents of the table. INFO is a plist holding
2507 contextual information."
2508 (if (eq (org-element-property :type table) 'table.el)
2509 ;; "table.el" table. Convert it using appropriate tools.
2510 (org-latex--table.el-table table info)
2511 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2512 (plist-get info :latex-default-table-mode))))
2513 (cond
2514 ;; Case 1: Verbatim table.
2515 ((string= type "verbatim")
2516 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2517 ;; Re-create table, without affiliated keywords.
2518 (org-trim (org-element-interpret-data
2519 `(table nil ,@(org-element-contents table))
2520 org-latex-pseudo-objects))))
2521 ;; Case 2: Matrix.
2522 ((or (string= type "math") (string= type "inline-math"))
2523 (org-latex--math-table table info))
2524 ;; Case 3: Standard table.
2525 (t (concat (org-latex--org-table table contents info)
2526 ;; When there are footnote references within the
2527 ;; table, insert their definition just after it.
2528 (org-latex--delayed-footnotes-definitions table info)))))))
2530 (defun org-latex--align-string (table info)
2531 "Return an appropriate LaTeX alignment string.
2532 TABLE is the considered table. INFO is a plist used as
2533 a communication channel."
2534 (or (org-export-read-attribute :attr_latex table :align)
2535 (let (align)
2536 ;; Extract column groups and alignment from first (non-rule)
2537 ;; row.
2538 (org-element-map
2539 (org-element-map table 'table-row
2540 (lambda (row)
2541 (and (eq (org-element-property :type row) 'standard) row))
2542 info 'first-match)
2543 'table-cell
2544 (lambda (cell)
2545 (let ((borders (org-export-table-cell-borders cell info)))
2546 ;; Check left border for the first cell only.
2547 (when (and (memq 'left borders) (not align))
2548 (push "|" align))
2549 (push (case (org-export-table-cell-alignment cell info)
2550 (left "l")
2551 (right "r")
2552 (center "c"))
2553 align)
2554 (when (memq 'right borders) (push "|" align))))
2555 info)
2556 (apply 'concat (nreverse align)))))
2558 (defun org-latex--org-table (table contents info)
2559 "Return appropriate LaTeX code for an Org table.
2561 TABLE is the table type element to transcode. CONTENTS is its
2562 contents, as a string. INFO is a plist used as a communication
2563 channel.
2565 This function assumes TABLE has `org' as its `:type' property and
2566 `table' as its `:mode' attribute."
2567 (let* ((caption (org-latex--caption/label-string table info))
2568 (attr (org-export-read-attribute :attr_latex table))
2569 ;; Determine alignment string.
2570 (alignment (org-latex--align-string table info))
2571 ;; Determine environment for the table: longtable, tabular...
2572 (table-env (or (plist-get attr :environment)
2573 (plist-get info :latex-default-table-environment)))
2574 ;; If table is a float, determine environment: table, table*
2575 ;; or sidewaystable.
2576 (float-env (unless (member table-env '("longtable" "longtabu"))
2577 (let ((float (plist-get attr :float)))
2578 (cond
2579 ((and (not float) (plist-member attr :float)) nil)
2580 ((or (string= float "sidewaystable")
2581 (string= float "sideways")) "sidewaystable")
2582 ((string= float "multicolumn") "table*")
2583 ((or float
2584 (org-element-property :caption table)
2585 (org-string-nw-p (plist-get attr :caption)))
2586 "table")))))
2587 ;; Extract others display options.
2588 (fontsize (let ((font (plist-get attr :font)))
2589 (and font (concat font "\n"))))
2590 (width (plist-get attr :width))
2591 (spreadp (plist-get attr :spread))
2592 (placement
2593 (or (plist-get attr :placement)
2594 (format "[%s]" (plist-get info :latex-default-figure-position))))
2595 (centerp (if (plist-member attr :center) (plist-get attr :center)
2596 (plist-get info :latex-tables-centered)))
2597 (caption-above-p (plist-get info :latex-table-caption-above)))
2598 ;; Prepare the final format string for the table.
2599 (cond
2600 ;; Longtable.
2601 ((equal "longtable" table-env)
2602 (concat (and fontsize (concat "{" fontsize))
2603 (format "\\begin{longtable}{%s}\n" alignment)
2604 (and caption-above-p
2605 (org-string-nw-p caption)
2606 (concat caption "\\\\\n"))
2607 contents
2608 (and (not caption-above-p)
2609 (org-string-nw-p caption)
2610 (concat caption "\\\\\n"))
2611 "\\end{longtable}\n"
2612 (and fontsize "}")))
2613 ;; Longtabu
2614 ((equal "longtabu" table-env)
2615 (concat (and fontsize (concat "{" fontsize))
2616 (format "\\begin{longtabu}%s{%s}\n"
2617 (if width
2618 (format " %s %s "
2619 (if spreadp "spread" "to") width) "")
2620 alignment)
2621 (and caption-above-p
2622 (org-string-nw-p caption)
2623 (concat caption "\\\\\n"))
2624 contents
2625 (and (not caption-above-p)
2626 (org-string-nw-p caption)
2627 (concat caption "\\\\\n"))
2628 "\\end{longtabu}\n"
2629 (and fontsize "}")))
2630 ;; Others.
2631 (t (concat (cond
2632 (float-env
2633 (concat (format "\\begin{%s}%s\n" float-env placement)
2634 (if caption-above-p caption "")
2635 (when centerp "\\centering\n")
2636 fontsize))
2637 (centerp (concat "\\begin{center}\n" fontsize))
2638 (fontsize (concat "{" fontsize)))
2639 (cond ((equal "tabu" table-env)
2640 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2641 (if width (format
2642 (if spreadp " spread %s " " to %s ")
2643 width) "")
2644 alignment
2645 contents))
2646 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2647 table-env
2648 (if width (format "{%s}" width) "")
2649 alignment
2650 contents
2651 table-env)))
2652 (cond
2653 (float-env
2654 (concat (if caption-above-p "" caption)
2655 (format "\n\\end{%s}" float-env)))
2656 (centerp "\n\\end{center}")
2657 (fontsize "}")))))))
2659 (defun org-latex--table.el-table (table info)
2660 "Return appropriate LaTeX code for a table.el table.
2662 TABLE is the table type element to transcode. INFO is a plist
2663 used as a communication channel.
2665 This function assumes TABLE has `table.el' as its `:type'
2666 property."
2667 (require 'table)
2668 ;; Ensure "*org-export-table*" buffer is empty.
2669 (with-current-buffer (get-buffer-create "*org-export-table*")
2670 (erase-buffer))
2671 (let ((output (with-temp-buffer
2672 (insert (org-element-property :value table))
2673 (goto-char 1)
2674 (re-search-forward "^[ \t]*|[^|]" nil t)
2675 (table-generate-source 'latex "*org-export-table*")
2676 (with-current-buffer "*org-export-table*"
2677 (org-trim (buffer-string))))))
2678 (kill-buffer (get-buffer "*org-export-table*"))
2679 ;; Remove left out comments.
2680 (while (string-match "^%.*\n" output)
2681 (setq output (replace-match "" t t output)))
2682 (let ((attr (org-export-read-attribute :attr_latex table)))
2683 (when (plist-get attr :rmlines)
2684 ;; When the "rmlines" attribute is provided, remove all hlines
2685 ;; but the the one separating heading from the table body.
2686 (let ((n 0) (pos 0))
2687 (while (and (< (length output) pos)
2688 (setq pos (string-match "^\\\\hline\n?" output pos)))
2689 (incf n)
2690 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2691 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2692 (plist-get info :latex-tables-centered))))
2693 (if (not centerp) output
2694 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2696 (defun org-latex--math-table (table info)
2697 "Return appropriate LaTeX code for a matrix.
2699 TABLE is the table type element to transcode. INFO is a plist
2700 used as a communication channel.
2702 This function assumes TABLE has `org' as its `:type' property and
2703 `inline-math' or `math' as its `:mode' attribute.."
2704 (let* ((caption (org-latex--caption/label-string table info))
2705 (attr (org-export-read-attribute :attr_latex table))
2706 (inlinep (equal (plist-get attr :mode) "inline-math"))
2707 (env (or (plist-get attr :environment)
2708 (plist-get info :latex-default-table-environment)))
2709 (contents
2710 (mapconcat
2711 (lambda (row)
2712 ;; Ignore horizontal rules.
2713 (when (eq (org-element-property :type row) 'standard)
2714 ;; Return each cell unmodified.
2715 (concat
2716 (mapconcat
2717 (lambda (cell)
2718 (substring
2719 (org-element-interpret-data cell org-latex-pseudo-objects)
2720 0 -1))
2721 (org-element-map row 'table-cell 'identity info) "&")
2722 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2723 "\n")))
2724 (org-element-map table 'table-row 'identity info) ""))
2725 ;; Variables related to math clusters (contiguous math tables
2726 ;; of the same type).
2727 (mode (org-export-read-attribute :attr_latex table :mode))
2728 (prev (org-export-get-previous-element table info))
2729 (next (org-export-get-next-element table info))
2730 (same-mode-p
2731 (lambda (table)
2732 ;; Non-nil when TABLE has the same mode as current table.
2733 (string= (or (org-export-read-attribute :attr_latex table :mode)
2734 (plist-get info :latex-default-table-mode))
2735 mode))))
2736 (concat
2737 ;; Opening string. If TABLE is in the middle of a table cluster,
2738 ;; do not insert any.
2739 (cond ((and prev
2740 (eq (org-element-type prev) 'table)
2741 (memq (org-element-property :post-blank prev) '(0 nil))
2742 (funcall same-mode-p prev))
2743 nil)
2744 (inlinep "\\(")
2745 ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
2746 (t "\\["))
2747 ;; Prefix.
2748 (or (plist-get attr :math-prefix) "")
2749 ;; Environment. Also treat special cases.
2750 (cond ((equal env "array")
2751 (let ((align (org-latex--align-string table info)))
2752 (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
2753 ((assoc env org-latex-table-matrix-macros)
2754 (format "\\%s%s{\n%s}"
2756 (or (plist-get attr :math-arguments) "")
2757 contents))
2758 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2759 ;; Suffix.
2760 (or (plist-get attr :math-suffix) "")
2761 ;; Closing string. If TABLE is in the middle of a table cluster,
2762 ;; do not insert any. If it closes such a cluster, be sure to
2763 ;; close the cluster with a string matching the opening string.
2764 (cond ((and next
2765 (eq (org-element-type next) 'table)
2766 (memq (org-element-property :post-blank table) '(0 nil))
2767 (funcall same-mode-p next))
2768 nil)
2769 (inlinep "\\)")
2770 ;; Find cluster beginning to know which environment to use.
2771 ((let ((cluster-beg table) prev)
2772 (while (and (setq prev (org-export-get-previous-element
2773 cluster-beg info))
2774 (memq (org-element-property :post-blank prev)
2775 '(0 nil))
2776 (funcall same-mode-p prev))
2777 (setq cluster-beg prev))
2778 (and (or (org-element-property :caption cluster-beg)
2779 (org-element-property :name cluster-beg))
2780 "\n\\end{equation}")))
2781 (t "\\]")))))
2784 ;;;; Table Cell
2786 (defun org-latex-table-cell (table-cell contents info)
2787 "Transcode a TABLE-CELL element from Org to LaTeX.
2788 CONTENTS is the cell contents. INFO is a plist used as
2789 a communication channel."
2790 (concat
2791 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
2792 (if (and contents
2793 scientific-format
2794 (string-match orgtbl-exp-regexp contents))
2795 ;; Use appropriate format string for scientific
2796 ;; notation.
2797 (format scientific-format
2798 (match-string 1 contents)
2799 (match-string 2 contents))
2800 contents))
2801 (when (org-export-get-next-element table-cell info) " & ")))
2804 ;;;; Table Row
2806 (defun org-latex-table-row (table-row contents info)
2807 "Transcode a TABLE-ROW element from Org to LaTeX.
2808 CONTENTS is the contents of the row. INFO is a plist used as
2809 a communication channel."
2810 (let* ((attr (org-export-read-attribute :attr_latex
2811 (org-export-get-parent table-row)))
2812 (booktabsp (if (plist-member attr :booktabs) (plist-get attr :booktabs)
2813 (plist-get info :latex-tables-booktabs)))
2814 (longtablep
2815 (member (or (plist-get attr :environment)
2816 (plist-get info :latex-default-table-environment))
2817 '("longtable" "longtabu"))))
2818 (if (eq (org-element-property :type table-row) 'rule)
2819 (cond
2820 ((not booktabsp) "\\hline")
2821 ((not (org-export-get-previous-element table-row info)) "\\toprule")
2822 ((not (org-export-get-next-element table-row info)) "\\bottomrule")
2823 ((and longtablep
2824 (org-export-table-row-ends-header-p
2825 (org-export-get-previous-element table-row info) info))
2827 (t "\\midrule"))
2828 (concat
2829 ;; When BOOKTABS are activated enforce top-rule even when no
2830 ;; hline was specifically marked.
2831 (and booktabsp (not (org-export-get-previous-element table-row info))
2832 "\\toprule\n")
2833 contents "\\\\\n"
2834 (cond
2835 ;; Special case for long tables. Define header and footers.
2836 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2837 (let ((columns (cdr (org-export-table-dimensions
2838 (org-export-get-parent-table table-row) info))))
2839 (format "%s
2840 \\endfirsthead
2841 \\multicolumn{%d}{l}{%s} \\\\
2843 %s \\\\\n
2845 \\endhead
2846 %s\\multicolumn{%d}{r}{%s} \\\\
2847 \\endfoot
2848 \\endlastfoot"
2849 (if booktabsp "\\midrule" "\\hline")
2850 columns
2851 (org-latex--translate "Continued from previous page" info)
2852 (cond
2853 ((not (org-export-table-row-starts-header-p table-row info))
2855 (booktabsp "\\toprule\n")
2856 (t "\\hline\n"))
2857 contents
2858 (if booktabsp "\\midrule" "\\hline")
2859 (if booktabsp "\\midrule" "\\hline")
2860 columns
2861 (org-latex--translate "Continued on next page" info))))
2862 ;; When BOOKTABS are activated enforce bottom rule even when
2863 ;; no hline was specifically marked.
2864 ((and booktabsp (not (org-export-get-next-element table-row info)))
2865 "\\bottomrule"))))))
2868 ;;;; Target
2870 (defun org-latex-target (target contents info)
2871 "Transcode a TARGET object from Org to LaTeX.
2872 CONTENTS is nil. INFO is a plist holding contextual
2873 information."
2874 (format "\\label{%s}"
2875 (org-export-solidify-link-text (org-element-property :value target))))
2878 ;;;; Timestamp
2880 (defun org-latex-timestamp (timestamp contents info)
2881 "Transcode a TIMESTAMP object from Org to LaTeX.
2882 CONTENTS is nil. INFO is a plist holding contextual
2883 information."
2884 (let ((value (org-latex-plain-text (org-timestamp-translate timestamp) info)))
2885 (format
2886 (plist-get info
2887 (case (org-element-property :type timestamp)
2888 ((active active-range) :latex-active-timestamp-format)
2889 ((inactive inactive-range) :latex-inactive-timestamp-format)
2890 (otherwise :latex-diary-timestamp-format)))
2891 value)))
2894 ;;;; Underline
2896 (defun org-latex-underline (underline contents info)
2897 "Transcode UNDERLINE from Org to LaTeX.
2898 CONTENTS is the text with underline markup. INFO is a plist
2899 holding contextual information."
2900 (org-latex--text-markup contents 'underline info))
2903 ;;;; Verbatim
2905 (defun org-latex-verbatim (verbatim contents info)
2906 "Transcode a VERBATIM object from Org to LaTeX.
2907 CONTENTS is nil. INFO is a plist used as a communication
2908 channel."
2909 (org-latex--text-markup
2910 (org-element-property :value verbatim) 'verbatim info))
2913 ;;;; Verse Block
2915 (defun org-latex-verse-block (verse-block contents info)
2916 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2917 CONTENTS is verse block contents. INFO is a plist holding
2918 contextual information."
2919 (org-latex--wrap-label
2920 verse-block
2921 ;; In a verse environment, add a line break to each newline
2922 ;; character and change each white space at beginning of a line
2923 ;; into a space of 1 em. Also change each blank line with
2924 ;; a vertical space of 1 em.
2925 (format "\\begin{verse}\n%s\\end{verse}"
2926 (replace-regexp-in-string
2927 "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
2928 (replace-regexp-in-string
2929 "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
2930 (replace-regexp-in-string
2931 "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
2932 contents nil t) nil t) nil t))))
2936 ;;; End-user functions
2938 ;;;###autoload
2939 (defun org-latex-export-as-latex
2940 (&optional async subtreep visible-only body-only ext-plist)
2941 "Export current buffer as a LaTeX buffer.
2943 If narrowing is active in the current buffer, only export its
2944 narrowed part.
2946 If a region is active, export that region.
2948 A non-nil optional argument ASYNC means the process should happen
2949 asynchronously. The resulting buffer should be accessible
2950 through the `org-export-stack' interface.
2952 When optional argument SUBTREEP is non-nil, export the sub-tree
2953 at point, extracting information from the headline properties
2954 first.
2956 When optional argument VISIBLE-ONLY is non-nil, don't export
2957 contents of hidden elements.
2959 When optional argument BODY-ONLY is non-nil, only write code
2960 between \"\\begin{document}\" and \"\\end{document}\".
2962 EXT-PLIST, when provided, is a property list with external
2963 parameters overriding Org default settings, but still inferior to
2964 file-local settings.
2966 Export is done in a buffer named \"*Org LATEX Export*\", which
2967 will be displayed when `org-export-show-temporary-export-buffer'
2968 is non-nil."
2969 (interactive)
2970 (org-export-to-buffer 'latex "*Org LATEX Export*"
2971 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
2973 ;;;###autoload
2974 (defun org-latex-convert-region-to-latex ()
2975 "Assume the current region has org-mode syntax, and convert it to LaTeX.
2976 This can be used in any buffer. For example, you can write an
2977 itemized list in org-mode syntax in an LaTeX buffer and use this
2978 command to convert it."
2979 (interactive)
2980 (org-export-replace-region-by 'latex))
2982 ;;;###autoload
2983 (defun org-latex-export-to-latex
2984 (&optional async subtreep visible-only body-only ext-plist)
2985 "Export current buffer to a LaTeX file.
2987 If narrowing is active in the current buffer, only export its
2988 narrowed part.
2990 If a region is active, export that region.
2992 A non-nil optional argument ASYNC means the process should happen
2993 asynchronously. The resulting file should be accessible through
2994 the `org-export-stack' interface.
2996 When optional argument SUBTREEP is non-nil, export the sub-tree
2997 at point, extracting information from the headline properties
2998 first.
3000 When optional argument VISIBLE-ONLY is non-nil, don't export
3001 contents of hidden elements.
3003 When optional argument BODY-ONLY is non-nil, only write code
3004 between \"\\begin{document}\" and \"\\end{document}\".
3006 EXT-PLIST, when provided, is a property list with external
3007 parameters overriding Org default settings, but still inferior to
3008 file-local settings."
3009 (interactive)
3010 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3011 (org-export-to-file 'latex outfile
3012 async subtreep visible-only body-only ext-plist)))
3014 ;;;###autoload
3015 (defun org-latex-export-to-pdf
3016 (&optional async subtreep visible-only body-only ext-plist)
3017 "Export current buffer to LaTeX then process through to PDF.
3019 If narrowing is active in the current buffer, only export its
3020 narrowed part.
3022 If a region is active, export that region.
3024 A non-nil optional argument ASYNC means the process should happen
3025 asynchronously. The resulting file should be accessible through
3026 the `org-export-stack' interface.
3028 When optional argument SUBTREEP is non-nil, export the sub-tree
3029 at point, extracting information from the headline properties
3030 first.
3032 When optional argument VISIBLE-ONLY is non-nil, don't export
3033 contents of hidden elements.
3035 When optional argument BODY-ONLY is non-nil, only write code
3036 between \"\\begin{document}\" and \"\\end{document}\".
3038 EXT-PLIST, when provided, is a property list with external
3039 parameters overriding Org default settings, but still inferior to
3040 file-local settings.
3042 Return PDF file's name."
3043 (interactive)
3044 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3045 (org-export-to-file 'latex outfile
3046 async subtreep visible-only body-only ext-plist
3047 (lambda (file) (org-latex-compile file)))))
3049 (defun org-latex-compile (texfile &optional snippet)
3050 "Compile a TeX file.
3052 TEXFILE is the name of the file being compiled. Processing is
3053 done through the command specified in `org-latex-pdf-process'.
3055 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3056 file used to preview a LaTeX snippet. In this case, do not
3057 create a log buffer and do not bother removing log files.
3059 Return PDF file name or an error if it couldn't be produced."
3060 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3061 (full-name (file-truename texfile))
3062 (out-dir (file-name-directory texfile))
3063 ;; Properly set working directory for compilation.
3064 (default-directory (if (file-name-absolute-p texfile)
3065 (file-name-directory full-name)
3066 default-directory))
3067 (time (current-time))
3068 warnings)
3069 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
3070 (save-window-excursion
3071 (cond
3072 ;; A function is provided: Apply it.
3073 ((functionp org-latex-pdf-process)
3074 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3075 ;; A list is provided: Replace %b, %f and %o with appropriate
3076 ;; values in each command before applying it. Output is
3077 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3078 ((consp org-latex-pdf-process)
3079 (let ((outbuf (and (not snippet)
3080 (get-buffer-create "*Org PDF LaTeX Output*"))))
3081 (dolist (command org-latex-pdf-process)
3082 (shell-command
3083 (replace-regexp-in-string
3084 "%b" (shell-quote-argument base-name)
3085 (replace-regexp-in-string
3086 "%f" (shell-quote-argument full-name)
3087 (replace-regexp-in-string
3088 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
3089 outbuf))
3090 ;; Collect standard errors from output buffer.
3091 (setq warnings (and (not snippet)
3092 (org-latex--collect-warnings outbuf)))))
3093 (t (error "No valid command to process to PDF")))
3094 (let ((pdffile (concat out-dir base-name ".pdf")))
3095 ;; Check for process failure. Provide collected errors if
3096 ;; possible.
3097 (if (or (not (file-exists-p pdffile))
3098 (time-less-p (nth 5 (file-attributes pdffile)) time))
3099 (error (format "PDF file %s wasn't produced" pdffile))
3100 ;; Else remove log files, when specified, and signal end of
3101 ;; process to user, along with any error encountered.
3102 (when (and (not snippet) org-latex-remove-logfiles)
3103 (dolist (file (directory-files
3104 out-dir t
3105 (concat (regexp-quote base-name)
3106 "\\(?:\\.[0-9]+\\)?"
3107 "\\."
3108 (regexp-opt org-latex-logfiles-extensions))))
3109 (delete-file file)))
3110 (message (concat "PDF file produced"
3111 (cond
3112 ((eq warnings 'error) " with errors.")
3113 (warnings (concat " with warnings: " warnings))
3114 (t ".")))))
3115 ;; Return output file name.
3116 pdffile))))
3118 (defun org-latex--collect-warnings (buffer)
3119 "Collect some warnings from \"pdflatex\" command output.
3120 BUFFER is the buffer containing output. Return collected
3121 warnings types as a string, `error' if a LaTeX error was
3122 encountered or nil if there was none."
3123 (with-current-buffer buffer
3124 (save-excursion
3125 (goto-char (point-max))
3126 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3127 (if (re-search-forward "^!" nil t) 'error
3128 (let ((case-fold-search t)
3129 (warnings ""))
3130 (dolist (warning org-latex-known-warnings)
3131 (when (save-excursion (re-search-forward (car warning) nil t))
3132 (setq warnings (concat warnings " " (cdr warning)))))
3133 (org-string-nw-p (org-trim warnings))))))))
3135 ;;;###autoload
3136 (defun org-latex-publish-to-latex (plist filename pub-dir)
3137 "Publish an Org file to LaTeX.
3139 FILENAME is the filename of the Org file to be published. PLIST
3140 is the property list for the given project. PUB-DIR is the
3141 publishing directory.
3143 Return output file name."
3144 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3146 ;;;###autoload
3147 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3148 "Publish an Org file to PDF (via LaTeX).
3150 FILENAME is the filename of the Org file to be published. PLIST
3151 is the property list for the given project. PUB-DIR is the
3152 publishing directory.
3154 Return output file name."
3155 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3156 ;; in working directory and then moved to publishing directory.
3157 (org-publish-attachment
3158 plist
3159 (org-latex-compile
3160 (org-publish-org-to
3161 'latex filename ".tex" plist (file-name-directory filename)))
3162 pub-dir))
3165 (provide 'ox-latex)
3167 ;; Local variables:
3168 ;; generated-autoload-file: "org-loaddefs.el"
3169 ;; End:
3171 ;;; ox-latex.el ends here