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