ox-latex: fix lost export option
[org-mode/org-kjn.git] / lisp / ox-latex.el
blobc14d6dcab378c8da73082ae0e7e254af942e8bca
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 5 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 as a list of strings (list of strings or nil).
410 The function result will be used in the section format string.
412 Use `org-latex-format-headline-default-function' by default,
413 which format headlines like for Org version prior to 8.0."
414 :group 'org-export-latex
415 :version "24.4"
416 :package-version '(Org . "8.0")
417 :type 'function)
419 (defcustom org-latex-custom-id-as-label nil
420 "Toggle use of CUSTOM_ID properties for generating section labels.
422 When this variable is non-nil, Org will use the value of a
423 headline's CUSTOM_ID property as the key for the \\label command
424 for the LaTeX section corresponding to the headline.
426 By default, Org generates its own internal section labels for all
427 headlines during LaTeX export. This process ensures that the
428 \\label keys are unique and valid, but it means the keys are not
429 available in advance of the export process.
431 Setting this variable gives you control over how Org generates
432 labels for sections during LaTeX export, so that you may know
433 their keys in advance. One reason to do this is that it allows
434 you to refer to headlines using a single label both in Org's link
435 syntax and in embedded LaTeX code.
437 For example, when this variable is non-nil, a headline like this:
439 ** Some section
440 :PROPERTIES:
441 :CUSTOM_ID: sec:foo
442 :END:
443 This is section [[#sec:foo]].
444 #+BEGIN_LATEX
445 And this is still section \\ref{sec:foo}.
446 #+END_LATEX
448 will be exported to LaTeX as:
450 \\subsection{Some section}
451 \\label{sec:foo}
452 This is section \\ref{sec:foo}.
453 And this is still section \\ref{sec:foo}.
455 Note, however, that setting this variable introduces a limitation
456 on the possible values for CUSTOM_ID. When this variable is
457 non-nil and a headline defines a CUSTOM_ID value, Org simply
458 passes this value to \\label unchanged. You are responsible for
459 ensuring that the value is a valid LaTeX \\label key, and that no
460 other \\label commands with the same key appear elsewhere in your
461 document. (Keys may contain letters, numbers, and the following
462 punctuation: '_' '.' '-' ':'.) There are no such limitations on
463 CUSTOM_ID when this variable is nil.
465 For headlines that do not define the CUSTOM_ID property, Org will
466 continue to use its default labeling scheme to generate labels
467 and resolve links into section references."
468 :group 'org-export-latex
469 :type 'boolean
470 :version "24.5"
471 :package-version '(Org . "8.3"))
473 ;;;; Footnotes
475 (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
476 "Text used to separate footnotes."
477 :group 'org-export-latex
478 :type 'string)
481 ;;;; Timestamps
483 (defcustom org-latex-active-timestamp-format "\\textit{%s}"
484 "A printf format string to be applied to active timestamps."
485 :group 'org-export-latex
486 :type 'string)
488 (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
489 "A printf format string to be applied to inactive timestamps."
490 :group 'org-export-latex
491 :type 'string)
493 (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
494 "A printf format string to be applied to diary timestamps."
495 :group 'org-export-latex
496 :type 'string)
499 ;;;; Links
501 (defcustom org-latex-image-default-option ""
502 "Default option for images."
503 :group 'org-export-latex
504 :version "24.4"
505 :package-version '(Org . "8.0")
506 :type 'string)
508 (defcustom org-latex-image-default-width ".9\\linewidth"
509 "Default width for images.
510 This value will not be used if a height is provided."
511 :group 'org-export-latex
512 :version "24.4"
513 :package-version '(Org . "8.0")
514 :type 'string)
516 (defcustom org-latex-image-default-height ""
517 "Default height for images.
518 This value will not be used if a width is provided, or if the
519 image is wrapped within a \"figure\" or \"wrapfigure\"
520 environment."
521 :group 'org-export-latex
522 :version "24.4"
523 :package-version '(Org . "8.0")
524 :type 'string)
526 (defcustom org-latex-default-figure-position "htb"
527 "Default position for latex figures."
528 :group 'org-export-latex
529 :type 'string)
531 (defcustom org-latex-inline-image-rules
532 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
533 "Rules characterizing image files that can be inlined into LaTeX.
535 A rule consists in an association whose key is the type of link
536 to consider, and value is a regexp that will be matched against
537 link's path.
539 Note that, by default, the image extension *actually* allowed
540 depend on the way the LaTeX file is processed. When used with
541 pdflatex, pdf, jpg and png images are OK. When processing
542 through dvi to Postscript, only ps and eps are allowed. The
543 default we use here encompasses both."
544 :group 'org-export-latex
545 :version "24.4"
546 :package-version '(Org . "8.0")
547 :type '(alist :key-type (string :tag "Type")
548 :value-type (regexp :tag "Path")))
550 (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
551 "Format string for links with unknown path type."
552 :group 'org-export-latex
553 :type 'string)
556 ;;;; Tables
558 (defcustom org-latex-default-table-environment "tabular"
559 "Default environment used to build tables."
560 :group 'org-export-latex
561 :version "24.4"
562 :package-version '(Org . "8.0")
563 :type 'string)
565 (defcustom org-latex-default-table-mode 'table
566 "Default mode for tables.
568 Value can be a symbol among:
570 `table' Regular LaTeX table.
572 `math' In this mode, every cell is considered as being in math
573 mode and the complete table will be wrapped within a math
574 environment. It is particularly useful to write matrices.
576 `inline-math' This mode is almost the same as `math', but the
577 math environment will be inlined.
579 `verbatim' The table is exported as it appears in the Org
580 buffer, within a verbatim environment.
582 This value can be overridden locally with, i.e. \":mode math\" in
583 LaTeX attributes.
585 When modifying this variable, it may be useful to change
586 `org-latex-default-table-environment' accordingly."
587 :group 'org-export-latex
588 :version "24.4"
589 :package-version '(Org . "8.0")
590 :type '(choice (const :tag "Table" table)
591 (const :tag "Matrix" math)
592 (const :tag "Inline matrix" inline-math)
593 (const :tag "Verbatim" verbatim))
594 :safe (lambda (s) (memq s '(table math inline-math verbatim))))
596 (defcustom org-latex-tables-centered t
597 "When non-nil, tables are exported in a center environment."
598 :group 'org-export-latex
599 :type 'boolean
600 :safe #'booleanp)
602 (defcustom org-latex-tables-booktabs nil
603 "When non-nil, display tables in a formal \"booktabs\" style.
604 This option assumes that the \"booktabs\" package is properly
605 loaded in the header of the document. This value can be ignored
606 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
607 attributes."
608 :group 'org-export-latex
609 :version "24.4"
610 :package-version '(Org . "8.0")
611 :type 'boolean
612 :safe #'booleanp)
614 (defcustom org-latex-table-caption-above t
615 "When non-nil, place caption string at the beginning of the table.
616 Otherwise, place it near the end."
617 :group 'org-export-latex
618 :type 'boolean
619 :safe #'booleanp)
621 (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
622 "Format string to display numbers in scientific notation.
623 The format should have \"%s\" twice, for mantissa and exponent
624 \(i.e., \"%s\\\\times10^{%s}\").
626 When nil, no transformation is made."
627 :group 'org-export-latex
628 :version "24.4"
629 :package-version '(Org . "8.0")
630 :type '(choice
631 (string :tag "Format string")
632 (const :tag "No formatting" nil)))
634 ;;;; Text markup
636 (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
637 (code . verb)
638 (italic . "\\emph{%s}")
639 (strike-through . "\\sout{%s}")
640 (underline . "\\uline{%s}")
641 (verbatim . protectedtexttt))
642 "Alist of LaTeX expressions to convert text markup.
644 The key must be a symbol among `bold', `code', `italic',
645 `strike-through', `underline' and `verbatim'. The value is
646 a formatting string to wrap fontified text with.
648 Value can also be set to the following symbols: `verb' and
649 `protectedtexttt'. For the former, Org will use \"\\verb\" to
650 create a format string and select a delimiter character that
651 isn't in the string. For the latter, Org will use \"\\texttt\"
652 to typeset and try to protect special characters.
654 If no association can be found for a given markup, text will be
655 returned as-is."
656 :group 'org-export-latex
657 :type 'alist
658 :options '(bold code italic strike-through underline verbatim))
661 ;;;; Drawers
663 (defcustom org-latex-format-drawer-function
664 (lambda (name contents) contents)
665 "Function called to format a drawer in LaTeX code.
667 The function must accept two parameters:
668 NAME the drawer name, like \"LOGBOOK\"
669 CONTENTS the contents of the drawer.
671 The function should return the string to be exported.
673 The default function simply returns the value of CONTENTS."
674 :group 'org-export-latex
675 :version "24.4"
676 :package-version '(Org . "8.3")
677 :type 'function)
680 ;;;; Inlinetasks
682 (defcustom org-latex-format-inlinetask-function
683 'org-latex-format-inlinetask-default-function
684 "Function called to format an inlinetask in LaTeX code.
686 The function must accept six parameters:
687 TODO the todo keyword, as a string
688 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
689 PRIORITY the inlinetask priority, as a string
690 NAME the inlinetask name, as a string.
691 TAGS the inlinetask tags, as a list of strings.
692 CONTENTS the contents of the inlinetask, as a string.
694 The function should return the string to be exported.
696 Use `org-latex-format-headline-default-function' by default."
697 :group 'org-export-latex
698 :type 'function
699 :version "24.5"
700 :package-version '(Org . "8.3"))
703 ;; Src blocks
705 (defcustom org-latex-listings nil
706 "Non-nil means export source code using the listings package.
708 This package will fontify source code, possibly even with color.
709 If you want to use this, you also need to make LaTeX use the
710 listings package, and if you want to have color, the color
711 package. Just add these to `org-latex-packages-alist', for
712 example using customize, or with something like:
714 \(require 'ox-latex)
715 \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
716 \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
718 Alternatively,
720 \(setq org-latex-listings 'minted)
722 causes source code to be exported using the minted package as
723 opposed to listings. If you want to use minted, you need to add
724 the minted package to `org-latex-packages-alist', for example
725 using customize, or with
727 \(require 'ox-latex)
728 \(add-to-list 'org-latex-packages-alist '(\"\" \"minted\"))
730 In addition, it is necessary to install pygments
731 \(http://pygments.org), and to configure the variable
732 `org-latex-pdf-process' so that the -shell-escape option is
733 passed to pdflatex.
735 The minted choice has possible repercussions on the preview of
736 latex fragments (see `org-preview-latex-fragment'). If you run
737 into previewing problems, please consult
739 http://orgmode.org/worg/org-tutorials/org-latex-preview.html"
740 :group 'org-export-latex
741 :type '(choice
742 (const :tag "Use listings" t)
743 (const :tag "Use minted" minted)
744 (const :tag "Export verbatim" nil))
745 :safe (lambda (s) (memq s '(t nil minted))))
747 (defcustom org-latex-listings-langs
748 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
749 (c "C") (cc "C++")
750 (fortran "fortran")
751 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
752 (html "HTML") (xml "XML")
753 (tex "TeX") (latex "[LaTeX]TeX")
754 (shell-script "bash")
755 (gnuplot "Gnuplot")
756 (ocaml "Caml") (caml "Caml")
757 (sql "SQL") (sqlite "sql")
758 (makefile "make"))
759 "Alist mapping languages to their listing language counterpart.
760 The key is a symbol, the major mode symbol without the \"-mode\".
761 The value is the string that should be inserted as the language
762 parameter for the listings package. If the mode name and the
763 listings name are the same, the language does not need an entry
764 in this list - but it does not hurt if it is present."
765 :group 'org-export-latex
766 :version "24.4"
767 :package-version '(Org . "8.3")
768 :type '(repeat
769 (list
770 (symbol :tag "Major mode ")
771 (string :tag "Listings language"))))
773 (defcustom org-latex-listings-options nil
774 "Association list of options for the latex listings package.
776 These options are supplied as a comma-separated list to the
777 \\lstset command. Each element of the association list should be
778 a list containing two strings: the name of the option, and the
779 value. For example,
781 \(setq org-latex-listings-options
782 '((\"basicstyle\" \"\\\\small\")
783 \(\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
785 will typeset the code in a small size font with underlined, bold
786 black keywords.
788 Note that the same options will be applied to blocks of all
789 languages. If you need block-specific options, you may use the
790 following syntax:
792 #+ATTR_LATEX: :options key1=value1,key2=value2
793 #+BEGIN_SRC <LANG>
795 #+END_SRC"
796 :group 'org-export-latex
797 :type '(repeat
798 (list
799 (string :tag "Listings option name ")
800 (string :tag "Listings option value"))))
802 (defcustom org-latex-minted-langs
803 '((emacs-lisp "common-lisp")
804 (cc "c++")
805 (cperl "perl")
806 (shell-script "bash")
807 (caml "ocaml"))
808 "Alist mapping languages to their minted language counterpart.
809 The key is a symbol, the major mode symbol without the \"-mode\".
810 The value is the string that should be inserted as the language
811 parameter for the minted package. If the mode name and the
812 listings name are the same, the language does not need an entry
813 in this list - but it does not hurt if it is present.
815 Note that minted uses all lower case for language identifiers,
816 and that the full list of language identifiers can be obtained
817 with:
819 pygmentize -L lexers"
820 :group 'org-export-latex
821 :type '(repeat
822 (list
823 (symbol :tag "Major mode ")
824 (string :tag "Minted language"))))
826 (defcustom org-latex-minted-options nil
827 "Association list of options for the latex minted package.
829 These options are supplied within square brackets in
830 \\begin{minted} environments. Each element of the alist should
831 be a list containing two strings: the name of the option, and the
832 value. For example,
834 \(setq org-latex-minted-options
835 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
837 will result in src blocks being exported with
839 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
841 as the start of the minted environment. Note that the same
842 options will be applied to blocks of all languages. If you need
843 block-specific options, you may use the following syntax:
845 #+ATTR_LATEX: :options key1=value1,key2=value2
846 #+BEGIN_SRC <LANG>
848 #+END_SRC"
849 :group 'org-export-latex
850 :type '(repeat
851 (list
852 (string :tag "Minted option name ")
853 (string :tag "Minted option value"))))
855 (defvar org-latex-custom-lang-environments nil
856 "Alist mapping languages to language-specific LaTeX environments.
858 It is used during export of src blocks by the listings and minted
859 latex packages. For example,
861 \(setq org-latex-custom-lang-environments
862 '\(\(python \"pythoncode\"\)\)\)
864 would have the effect that if org encounters begin_src python
865 during latex export it will output
867 \\begin{pythoncode}
868 <src block body>
869 \\end{pythoncode}")
872 ;;;; Compilation
874 (defcustom org-latex-pdf-process
875 '("pdflatex -interaction nonstopmode -output-directory %o %f"
876 "pdflatex -interaction nonstopmode -output-directory %o %f"
877 "pdflatex -interaction nonstopmode -output-directory %o %f")
878 "Commands to process a LaTeX file to a PDF file.
879 This is a list of strings, each of them will be given to the
880 shell as a command. %f in the command will be replaced by the
881 full file name, %b by the file base name (i.e. without directory
882 and extension parts) and %o by the base directory of the file.
884 The reason why this is a list is that it usually takes several
885 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
886 does not have a clever mechanism to detect which of these
887 commands have to be run to get to a stable result, and it also
888 does not do any error checking.
890 By default, Org uses 3 runs of `pdflatex' to do the processing.
891 If you have texi2dvi on your system and if that does not cause
892 the infamous egrep/locale bug:
894 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
896 then `texi2dvi' is the superior choice as it automates the LaTeX
897 build process by calling the \"correct\" combinations of
898 auxiliary programs. Org does offer `texi2dvi' as one of the
899 customize options. Alternatively, `rubber' and `latexmk' also
900 provide similar functionality. The latter supports `biber' out
901 of the box.
903 Alternatively, this may be a Lisp function that does the
904 processing, so you could use this to apply the machinery of
905 AUCTeX or the Emacs LaTeX mode. This function should accept the
906 file name as its single argument."
907 :group 'org-export-pdf
908 :type '(choice
909 (repeat :tag "Shell command sequence"
910 (string :tag "Shell command"))
911 (const :tag "2 runs of pdflatex"
912 ("pdflatex -interaction nonstopmode -output-directory %o %f"
913 "pdflatex -interaction nonstopmode -output-directory %o %f"))
914 (const :tag "3 runs of pdflatex"
915 ("pdflatex -interaction nonstopmode -output-directory %o %f"
916 "pdflatex -interaction nonstopmode -output-directory %o %f"
917 "pdflatex -interaction nonstopmode -output-directory %o %f"))
918 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
919 ("pdflatex -interaction nonstopmode -output-directory %o %f"
920 "bibtex %b"
921 "pdflatex -interaction nonstopmode -output-directory %o %f"
922 "pdflatex -interaction nonstopmode -output-directory %o %f"))
923 (const :tag "2 runs of xelatex"
924 ("xelatex -interaction nonstopmode -output-directory %o %f"
925 "xelatex -interaction nonstopmode -output-directory %o %f"))
926 (const :tag "3 runs of xelatex"
927 ("xelatex -interaction nonstopmode -output-directory %o %f"
928 "xelatex -interaction nonstopmode -output-directory %o %f"
929 "xelatex -interaction nonstopmode -output-directory %o %f"))
930 (const :tag "xelatex,bibtex,xelatex,xelatex"
931 ("xelatex -interaction nonstopmode -output-directory %o %f"
932 "bibtex %b"
933 "xelatex -interaction nonstopmode -output-directory %o %f"
934 "xelatex -interaction nonstopmode -output-directory %o %f"))
935 (const :tag "texi2dvi"
936 ("texi2dvi -p -b -V %f"))
937 (const :tag "rubber"
938 ("rubber -d --into %o %f"))
939 (const :tag "latexmk"
940 ("latexmk -g -pdf %f"))
941 (function)))
943 (defcustom org-latex-logfiles-extensions
944 '("aux" "bcf" "blg" "fdb_latexmk" "fls" "figlist" "idx" "log" "nav" "out"
945 "run.xml" "snm" "toc" "vrb" "xdv")
946 "The list of file extensions to consider as LaTeX logfiles.
947 The logfiles will be remove if `org-latex-remove-logfiles' is
948 non-nil."
949 :group 'org-export-latex
950 :type '(repeat (string :tag "Extension")))
952 (defcustom org-latex-remove-logfiles t
953 "Non-nil means remove the logfiles produced by PDF production.
954 By default, logfiles are files with these extensions: .aux, .idx,
955 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
956 logfiles to remove, set `org-latex-logfiles-extensions'."
957 :group 'org-export-latex
958 :type 'boolean)
960 (defcustom org-latex-known-warnings
961 '(("Reference.*?undefined" . "[undefined reference]")
962 ("Runaway argument" . "[runaway argument]")
963 ("Underfull \\hbox" . "[underfull hbox]")
964 ("Overfull \\hbox" . "[overfull hbox]")
965 ("Citation.*?undefined" . "[undefined citation]")
966 ("Undefined control sequence" . "[undefined control sequence]"))
967 "Alist of regular expressions and associated messages for the user.
968 The regular expressions are used to find possible warnings in the
969 log of a latex-run. These warnings will be reported after
970 calling `org-latex-compile'."
971 :group 'org-export-latex
972 :version "24.5"
973 :package-version '(Org . "8.3")
974 :type '(repeat
975 (cons
976 (string :tag "Regexp")
977 (string :tag "Message"))))
981 ;;; Internal Functions
983 (defun org-latex--caption/label-string (element info)
984 "Return caption and label LaTeX string for ELEMENT.
986 INFO is a plist holding contextual information. If there's no
987 caption nor label, return the empty string.
989 For non-floats, see `org-latex--wrap-label'."
990 (let* ((label (org-element-property :name element))
991 (label-str (if (not (org-string-nw-p label)) ""
992 (format "\\label{%s}"
993 (org-export-solidify-link-text label))))
994 (main (org-export-get-caption element))
995 (short (org-export-get-caption element t))
996 (caption-from-attr-latex (org-export-read-attribute :attr_latex element :caption)))
997 (cond
998 ((org-string-nw-p caption-from-attr-latex)
999 (concat caption-from-attr-latex "\n"))
1000 ((and (not main) (equal label-str "")) "")
1001 ((not main) (concat label-str "\n"))
1002 ;; Option caption format with short name.
1003 (short (format "\\caption[%s]{%s%s}\n"
1004 (org-export-data short info)
1005 label-str
1006 (org-export-data main info)))
1007 ;; Standard caption format.
1008 (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
1010 (defun org-latex-guess-inputenc (header)
1011 "Set the coding system in inputenc to what the buffer is.
1013 HEADER is the LaTeX header string. This function only applies
1014 when specified inputenc option is \"AUTO\".
1016 Return the new header, as a string."
1017 (let* ((cs (or (ignore-errors
1018 (latexenc-coding-system-to-inputenc
1019 (or org-export-coding-system buffer-file-coding-system)))
1020 "utf8")))
1021 (if (not cs) header
1022 ;; First translate if that is requested.
1023 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
1024 ;; Then find the \usepackage statement and replace the option.
1025 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1026 cs header t nil 1))))
1028 (defun org-latex-guess-babel-language (header info)
1029 "Set Babel's language according to LANGUAGE keyword.
1031 HEADER is the LaTeX header string. INFO is the plist used as
1032 a communication channel.
1034 Insertion of guessed language only happens when Babel package has
1035 explicitly been loaded. Then it is added to the rest of
1036 package's options.
1038 The argument to Babel may be \"AUTO\" which is then replaced with
1039 the language of the document or `org-export-default-language'
1040 unless language in question is already loaded.
1042 Return the new header."
1043 (let ((language-code (plist-get info :language)))
1044 ;; If no language is set or Babel package is not loaded, return
1045 ;; HEADER as-is.
1046 (if (or (not (stringp language-code))
1047 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
1048 header
1049 (let ((options (save-match-data
1050 (org-split-string (match-string 1 header) ",[ \t]*")))
1051 (language (cdr (assoc language-code
1052 org-latex-babel-language-alist))))
1053 ;; If LANGUAGE is already loaded, return header without AUTO.
1054 ;; Otherwise, replace AUTO with language or append language if
1055 ;; AUTO is not present.
1056 (replace-match
1057 (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
1058 (cond ((member language options) (delete "AUTO" options))
1059 ((member "AUTO" options) options)
1060 (t (append options (list language))))
1061 ", ")
1062 t nil header 1)))))
1064 (defun org-latex--find-verb-separator (s)
1065 "Return a character not used in string S.
1066 This is used to choose a separator for constructs like \\verb."
1067 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1068 (loop for c across ll
1069 when (not (string-match (regexp-quote (char-to-string c)) s))
1070 return (char-to-string c))))
1072 (defun org-latex--make-option-string (options)
1073 "Return a comma separated string of keywords and values.
1074 OPTIONS is an alist where the key is the options keyword as
1075 a string, and the value a list containing the keyword value, or
1076 nil."
1077 (mapconcat (lambda (pair)
1078 (concat (first pair)
1079 (when (> (length (second pair)) 0)
1080 (concat "=" (second pair)))))
1081 options
1082 ","))
1084 (defun org-latex--wrap-label (element output)
1085 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1086 This function shouldn't be used for floats. See
1087 `org-latex--caption/label-string'."
1088 (let ((label (org-element-property :name element)))
1089 (if (not (and (org-string-nw-p output) (org-string-nw-p label))) output
1090 (concat (format "\\label{%s}\n" (org-export-solidify-link-text label))
1091 output))))
1093 (defun org-latex--text-markup (text markup info)
1094 "Format TEXT depending on MARKUP text markup.
1095 INFO is a plist used as a communication channel. See
1096 `org-latex-text-markup-alist' for details."
1097 (let ((fmt (cdr (assq markup (plist-get info :latex-text-markup-alist)))))
1098 (cond
1099 ;; No format string: Return raw text.
1100 ((not fmt) text)
1101 ;; Handle the `verb' special case: Find an appropriate separator
1102 ;; and use "\\verb" command.
1103 ((eq 'verb fmt)
1104 (let ((separator (org-latex--find-verb-separator text)))
1105 (concat "\\verb" separator
1106 (replace-regexp-in-string "\n" " " text)
1107 separator)))
1108 ;; Handle the `protectedtexttt' special case: Protect some
1109 ;; special chars and use "\texttt{%s}" format string.
1110 ((eq 'protectedtexttt fmt)
1111 (let ((start 0)
1112 (trans '(("\\" . "\\textbackslash{}")
1113 ("~" . "\\textasciitilde{}")
1114 ("^" . "\\textasciicircum{}")))
1115 (rtn "")
1116 char)
1117 (while (string-match "[\\{}$%&_#~^]" text)
1118 (setq char (match-string 0 text))
1119 (if (> (match-beginning 0) 0)
1120 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
1121 (setq text (substring text (1+ (match-beginning 0))))
1122 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1123 rtn (concat rtn char)))
1124 (setq text (concat rtn text)
1125 fmt "\\texttt{%s}")
1126 (while (string-match "--" text)
1127 (setq text (replace-match "-{}-" t t text)))
1128 (format fmt text)))
1129 ;; Else use format string.
1130 (t (format fmt text)))))
1132 (defun org-latex--delayed-footnotes-definitions (element info)
1133 "Return footnotes definitions in ELEMENT as a string.
1135 INFO is a plist used as a communication channel.
1137 Footnotes definitions are returned within \"\\footnotetxt{}\"
1138 commands.
1140 This function is used within constructs that don't support
1141 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1142 \"\\footnotemark\" is used within the construct and the function
1143 just outside of it."
1144 (mapconcat
1145 (lambda (ref)
1146 (format
1147 "\\footnotetext[%s]{%s}"
1148 (org-export-get-footnote-number ref info)
1149 (org-trim
1150 (org-export-data
1151 (org-export-get-footnote-definition ref info) info))))
1152 ;; Find every footnote reference in ELEMENT.
1153 (let* (all-refs
1154 search-refs ; For byte-compiler.
1155 (search-refs
1156 (function
1157 (lambda (data)
1158 ;; Return a list of all footnote references never seen
1159 ;; before in DATA.
1160 (org-element-map data 'footnote-reference
1161 (lambda (ref)
1162 (when (org-export-footnote-first-reference-p ref info)
1163 (push ref all-refs)
1164 (when (eq (org-element-property :type ref) 'standard)
1165 (funcall search-refs
1166 (org-export-get-footnote-definition ref info)))))
1167 info)
1168 (reverse all-refs)))))
1169 (funcall search-refs element))
1170 ""))
1172 (defun org-latex--translate (s info)
1173 "Translate string S according to specified language.
1174 INFO is a plist used as a communication channel."
1175 (org-export-translate s :latex info))
1179 ;;; Template
1181 (defun org-latex-template (contents info)
1182 "Return complete document string after LaTeX conversion.
1183 CONTENTS is the transcoded contents string. INFO is a plist
1184 holding export options."
1185 (let ((title (org-export-data (plist-get info :title) info)))
1186 (concat
1187 ;; Time-stamp.
1188 (and (plist-get info :time-stamp-file)
1189 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1190 ;; Document class and packages.
1191 (let* ((class (plist-get info :latex-class))
1192 (class-options (plist-get info :latex-class-options))
1193 (header (nth 1 (assoc class (plist-get info :latex-classes))))
1194 (document-class-string
1195 (and (stringp header)
1196 (if (not class-options) header
1197 (replace-regexp-in-string
1198 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1199 class-options header t nil 1)))))
1200 (if (not document-class-string)
1201 (user-error "Unknown LaTeX class `%s'" class)
1202 (org-latex-guess-babel-language
1203 (org-latex-guess-inputenc
1204 (org-element-normalize-string
1205 (org-splice-latex-header
1206 document-class-string
1207 org-latex-default-packages-alist
1208 org-latex-packages-alist nil
1209 (concat (org-element-normalize-string
1210 (plist-get info :latex-header))
1211 (plist-get info :latex-header-extra)))))
1212 info)))
1213 ;; Possibly limit depth for headline numbering.
1214 (let ((sec-num (plist-get info :section-numbers)))
1215 (when (integerp sec-num)
1216 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1217 ;; Author.
1218 (let ((author (and (plist-get info :with-author)
1219 (let ((auth (plist-get info :author)))
1220 (and auth (org-export-data auth info)))))
1221 (email (and (plist-get info :with-email)
1222 (org-export-data (plist-get info :email) info))))
1223 (cond ((and author email (not (string= "" email)))
1224 (format "\\author{%s\\thanks{%s}}\n" author email))
1225 ((or author email) (format "\\author{%s}\n" (or author email)))))
1226 ;; Date.
1227 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1228 (format "\\date{%s}\n" (org-export-data date info)))
1229 ;; Title
1230 (format "\\title{%s}\n" title)
1231 ;; Hyperref options.
1232 (format-spec (plist-get info :latex-hyperref-template)
1233 (format-spec-make
1234 ?k (or (plist-get info :keywords) "")
1235 ?d (or (plist-get info :description)"")
1236 ?c (if (plist-get info :with-creator)
1237 (plist-get info :creator)
1238 "")))
1239 ;; Document start.
1240 "\\begin{document}\n\n"
1241 ;; Title command.
1242 (let ((command (plist-get info :latex-title-command)))
1243 (org-element-normalize-string
1244 (cond ((string= "" title) nil)
1245 ((not (stringp command)) nil)
1246 ((string-match "\\(?:[^%]\\|^\\)%s" command)
1247 (format command title))
1248 (t command))))
1249 ;; Table of contents.
1250 (let ((depth (plist-get info :with-toc)))
1251 (when depth
1252 (concat (when (wholenump depth)
1253 (format "\\setcounter{tocdepth}{%d}\n" depth))
1254 (plist-get info :latex-toc-command))))
1255 ;; Document's body.
1256 contents
1257 ;; Creator.
1258 (let ((creator-info (plist-get info :with-creator)))
1259 (cond
1260 ((not creator-info) "")
1261 ((eq creator-info 'comment)
1262 (format "%% %s\n" (plist-get info :creator)))
1263 (t (concat (plist-get info :creator) "\n"))))
1264 ;; Document end.
1265 "\\end{document}")))
1269 ;;; Transcode Functions
1271 ;;;; Bold
1273 (defun org-latex-bold (bold contents info)
1274 "Transcode BOLD from Org to LaTeX.
1275 CONTENTS is the text with bold markup. INFO is a plist holding
1276 contextual information."
1277 (org-latex--text-markup contents 'bold info))
1280 ;;;; Center Block
1282 (defun org-latex-center-block (center-block contents info)
1283 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1284 CONTENTS holds the contents of the center block. INFO is a plist
1285 holding contextual information."
1286 (org-latex--wrap-label
1287 center-block
1288 (format "\\begin{center}\n%s\\end{center}" contents)))
1291 ;;;; Clock
1293 (defun org-latex-clock (clock contents info)
1294 "Transcode a CLOCK element from Org to LaTeX.
1295 CONTENTS is nil. INFO is a plist holding contextual
1296 information."
1297 (concat
1298 "\\noindent"
1299 (format "\\textbf{%s} " org-clock-string)
1300 (format (plist-get info :latex-inactive-timestamp-format)
1301 (concat (org-translate-time
1302 (org-element-property :raw-value
1303 (org-element-property :value clock)))
1304 (let ((time (org-element-property :duration clock)))
1305 (and time (format " (%s)" time)))))
1306 "\\\\"))
1309 ;;;; Code
1311 (defun org-latex-code (code contents info)
1312 "Transcode a CODE object from Org to LaTeX.
1313 CONTENTS is nil. INFO is a plist used as a communication
1314 channel."
1315 (org-latex--text-markup (org-element-property :value code) 'code info))
1318 ;;;; Drawer
1320 (defun org-latex-drawer (drawer contents info)
1321 "Transcode a DRAWER element from Org to LaTeX.
1322 CONTENTS holds the contents of the block. INFO is a plist
1323 holding contextual information."
1324 (let* ((name (org-element-property :drawer-name drawer))
1325 (output (funcall (plist-get info :latex-format-drawer-function)
1326 name contents)))
1327 (org-latex--wrap-label drawer output)))
1330 ;;;; Dynamic Block
1332 (defun org-latex-dynamic-block (dynamic-block contents info)
1333 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1334 CONTENTS holds the contents of the block. INFO is a plist
1335 holding contextual information. See `org-export-data'."
1336 (org-latex--wrap-label dynamic-block contents))
1339 ;;;; Entity
1341 (defun org-latex-entity (entity contents info)
1342 "Transcode an ENTITY object from Org to LaTeX.
1343 CONTENTS are the definition itself. INFO is a plist holding
1344 contextual information."
1345 (org-element-property :latex entity))
1348 ;;;; Example Block
1350 (defun org-latex-example-block (example-block contents info)
1351 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1352 CONTENTS is nil. INFO is a plist holding contextual
1353 information."
1354 (when (org-string-nw-p (org-element-property :value example-block))
1355 (org-latex--wrap-label
1356 example-block
1357 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1358 (org-export-format-code-default example-block info)))))
1361 ;;;; Export Snippet
1363 (defun org-latex-export-snippet (export-snippet contents info)
1364 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1365 CONTENTS is nil. INFO is a plist holding contextual information."
1366 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1367 (org-element-property :value export-snippet)))
1370 ;;;; Fixed Width
1372 (defun org-latex-fixed-width (fixed-width contents info)
1373 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1374 CONTENTS is nil. INFO is a plist holding contextual information."
1375 (org-latex--wrap-label
1376 fixed-width
1377 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1378 (org-remove-indentation
1379 (org-element-property :value fixed-width)))))
1382 ;;;; Footnote Reference
1384 (defun org-latex-footnote-reference (footnote-reference contents info)
1385 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1386 CONTENTS is nil. INFO is a plist holding contextual information."
1387 (concat
1388 ;; Insert separator between two footnotes in a row.
1389 (let ((prev (org-export-get-previous-element footnote-reference info)))
1390 (when (eq (org-element-type prev) 'footnote-reference)
1391 (plist-get info :latex-footnote-separator)))
1392 (cond
1393 ;; Use \footnotemark if the footnote has already been defined.
1394 ((not (org-export-footnote-first-reference-p footnote-reference info))
1395 (format "\\footnotemark[%s]{}"
1396 (org-export-get-footnote-number footnote-reference info)))
1397 ;; Use \footnotemark if reference is within another footnote
1398 ;; reference, footnote definition or table cell.
1399 ((loop for parent in (org-export-get-genealogy footnote-reference)
1400 thereis (memq (org-element-type parent)
1401 '(footnote-reference footnote-definition table-cell)))
1402 "\\footnotemark")
1403 ;; Otherwise, define it with \footnote command.
1405 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1406 (concat
1407 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1408 ;; Retrieve all footnote references within the footnote and
1409 ;; add their definition after it, since LaTeX doesn't support
1410 ;; them inside.
1411 (org-latex--delayed-footnotes-definitions def info)))))))
1414 ;;;; Headline
1416 (defun org-latex-headline (headline contents info)
1417 "Transcode a HEADLINE element from Org to LaTeX.
1418 CONTENTS holds the contents of the headline. INFO is a plist
1419 holding contextual information."
1420 (unless (org-element-property :footnote-section-p headline)
1421 (let* ((class (plist-get info :latex-class))
1422 (level (org-export-get-relative-level headline info))
1423 (numberedp (org-export-numbered-headline-p headline info))
1424 (class-sectioning (assoc class (plist-get info :latex-classes)))
1425 ;; Section formatting will set two placeholders: one for
1426 ;; the title and the other for the contents.
1427 (section-fmt
1428 (let ((sec (if (functionp (nth 2 class-sectioning))
1429 (funcall (nth 2 class-sectioning) level numberedp)
1430 (nth (1+ level) class-sectioning))))
1431 (cond
1432 ;; No section available for that LEVEL.
1433 ((not sec) nil)
1434 ;; Section format directly returned by a function. Add
1435 ;; placeholder for contents.
1436 ((stringp sec) (concat sec "\n%s"))
1437 ;; (numbered-section . unnumbered-section)
1438 ((not (consp (cdr sec)))
1439 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1440 ;; (numbered-open numbered-close)
1441 ((= (length sec) 2)
1442 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1443 ;; (num-in num-out no-num-in no-num-out)
1444 ((= (length sec) 4)
1445 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1446 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1447 ;; Create a temporary export back-end that hard-codes
1448 ;; "\underline" within "\section" and alike.
1449 (section-back-end
1450 (org-export-create-backend
1451 :parent 'latex
1452 :transcoders
1453 '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
1454 (text
1455 (org-export-data-with-backend
1456 (org-element-property :title headline) section-back-end info))
1457 (todo
1458 (and (plist-get info :with-todo-keywords)
1459 (let ((todo (org-element-property :todo-keyword headline)))
1460 (and todo (org-export-data todo info)))))
1461 (todo-type (and todo (org-element-property :todo-type headline)))
1462 (tags (and (plist-get info :with-tags)
1463 (org-export-get-tags headline info)))
1464 (priority (and (plist-get info :with-priority)
1465 (org-element-property :priority headline)))
1466 ;; Create the headline text along with a no-tag version.
1467 ;; The latter is required to remove tags from toc.
1468 (full-text (funcall (plist-get info :latex-format-headline-function)
1469 todo todo-type priority text tags))
1470 ;; Associate \label to the headline for internal links.
1471 (headline-label
1472 (let ((custom-label
1473 (and (plist-get info :latex-custom-id-labels)
1474 (org-element-property :CUSTOM_ID headline))))
1475 (if custom-label (format "\\label{%s}\n" custom-label)
1476 (format "\\label{sec-%s}\n"
1477 (mapconcat
1478 #'number-to-string
1479 (org-export-get-headline-number headline info)
1480 "-")))))
1481 (pre-blanks
1482 (make-string (org-element-property :pre-blank headline) 10)))
1483 (if (or (not section-fmt) (org-export-low-level-p headline info))
1484 ;; This is a deep sub-tree: export it as a list item. Also
1485 ;; export as items headlines for which no section format has
1486 ;; been found.
1487 (let ((low-level-body
1488 (concat
1489 ;; If headline is the first sibling, start a list.
1490 (when (org-export-first-sibling-p headline info)
1491 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1492 ;; Itemize headline
1493 "\\item"
1494 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
1495 "\\relax")
1496 " " full-text "\n"
1497 headline-label
1498 pre-blanks
1499 contents)))
1500 ;; If headline is not the last sibling simply return
1501 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1502 ;; any blank line.
1503 (if (not (org-export-last-sibling-p headline info)) low-level-body
1504 (replace-regexp-in-string
1505 "[ \t\n]*\\'"
1506 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1507 low-level-body)))
1508 ;; This is a standard headline. Export it as a section. Add
1509 ;; an alternative heading when possible, and when this is not
1510 ;; identical to the usual heading.
1511 (let ((opt-title
1512 (funcall (plist-get info :latex-format-headline-function)
1513 todo todo-type priority
1514 (org-export-data-with-backend
1515 (org-export-get-alt-title headline info)
1516 section-back-end info)
1517 (and (eq (plist-get info :with-tags) t) tags))))
1518 (if (and numberedp opt-title
1519 (not (equal opt-title full-text))
1520 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
1521 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1522 ;; Replace square brackets with parenthesis
1523 ;; since square brackets are not supported in
1524 ;; optional arguments.
1525 (replace-regexp-in-string
1526 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1527 full-text
1528 (concat headline-label pre-blanks contents))
1529 ;; Impossible to add an alternative heading. Fallback to
1530 ;; regular sectioning format string.
1531 (format section-fmt full-text
1532 (concat headline-label pre-blanks contents))))))))
1534 (defun org-latex-format-headline-default-function
1535 (todo todo-type priority text tags)
1536 "Default format function for a headline.
1537 See `org-latex-format-headline-function' for details."
1538 (concat
1539 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1540 (and priority (format "\\framebox{\\#%c} " priority))
1541 text
1542 (and tags
1543 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity 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)))
1626 (defun org-latex-format-inlinetask-default-function
1627 (todo todo-type priority title tags contents)
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 (format "\\hfill{}\\textsc{:%s:}"
1635 (mapconcat #'identity tags ":"))))))
1636 (concat "\\begin{center}\n"
1637 "\\fbox{\n"
1638 "\\begin{minipage}[c]{.6\\textwidth}\n"
1639 full-title "\n\n"
1640 (and (org-string-nw-p contents)
1641 (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
1642 "\\end{minipage}\n"
1643 "}\n"
1644 "\\end{center}")))
1647 ;;;; Italic
1649 (defun org-latex-italic (italic contents info)
1650 "Transcode ITALIC from Org to LaTeX.
1651 CONTENTS is the text with italic markup. INFO is a plist holding
1652 contextual information."
1653 (org-latex--text-markup contents 'italic info))
1656 ;;;; Item
1658 (defun org-latex-item (item contents info)
1659 "Transcode an ITEM element from Org to LaTeX.
1660 CONTENTS holds the contents of the item. INFO is a plist holding
1661 contextual information."
1662 (let* ((counter
1663 (let ((count (org-element-property :counter item))
1664 (level
1665 ;; Determine level of current item to determine the
1666 ;; correct LaTeX counter to use (enumi, enumii...).
1667 (let ((parent item) (level 0))
1668 (while (memq (org-element-type
1669 (setq parent (org-export-get-parent parent)))
1670 '(plain-list item))
1671 (when (and (eq (org-element-type parent) 'plain-list)
1672 (eq (org-element-property :type parent)
1673 'ordered))
1674 (incf level)))
1675 level)))
1676 (and count
1677 (< level 5)
1678 (format "\\setcounter{enum%s}{%s}\n"
1679 (nth (1- level) '("i" "ii" "iii" "iv"))
1680 (1- count)))))
1681 (checkbox (case (org-element-property :checkbox item)
1682 (on "$\\boxtimes$ ")
1683 (off "$\\square$ ")
1684 (trans "$\\boxminus$ ")))
1685 (tag (let ((tag (org-element-property :tag item)))
1686 ;; Check-boxes must belong to the tag.
1687 (and tag (format "[{%s}] "
1688 (concat checkbox
1689 (org-export-data tag info)))))))
1690 (concat counter
1691 "\\item"
1692 (cond
1693 (tag)
1694 (checkbox (concat " " checkbox))
1695 ;; Without a tag or a check-box, if CONTENTS starts with
1696 ;; an opening square bracket, add "\relax" to "\item",
1697 ;; unless the brackets comes from an initial export
1698 ;; snippet (i.e. it is inserted willingly by the user).
1699 ((and contents
1700 (org-string-match-p "\\`[ \t]*\\[" contents)
1701 (not (let ((e (car (org-element-contents item))))
1702 (and (eq (org-element-type e) 'paragraph)
1703 (let ((o (car (org-element-contents e))))
1704 (and (eq (org-element-type o) 'export-snippet)
1705 (eq (org-export-snippet-backend o)
1706 'latex)))))))
1707 "\\relax ")
1708 (t " "))
1709 (and contents (org-trim contents))
1710 ;; If there are footnotes references in tag, be sure to
1711 ;; add their definition at the end of the item. This
1712 ;; workaround is necessary since "\footnote{}" command is
1713 ;; not supported in tags.
1714 (and tag
1715 (org-latex--delayed-footnotes-definitions
1716 (org-element-property :tag item) info)))))
1719 ;;;; Keyword
1721 (defun org-latex-keyword (keyword contents info)
1722 "Transcode a KEYWORD element from Org to LaTeX.
1723 CONTENTS is nil. INFO is a plist holding contextual information."
1724 (let ((key (org-element-property :key keyword))
1725 (value (org-element-property :value keyword)))
1726 (cond
1727 ((string= key "LATEX") value)
1728 ((string= key "INDEX") (format "\\index{%s}" value))
1729 ((string= key "TOC")
1730 (let ((value (downcase value)))
1731 (cond
1732 ((string-match "\\<headlines\\>" value)
1733 (let ((depth (or (and (string-match "[0-9]+" value)
1734 (string-to-number (match-string 0 value)))
1735 (plist-get info :with-toc))))
1736 (concat
1737 (when (wholenump depth)
1738 (format "\\setcounter{tocdepth}{%s}\n" depth))
1739 "\\tableofcontents")))
1740 ((string= "tables" value) "\\listoftables")
1741 ((string= "listings" value)
1742 (case (plist-get info :latex-listings)
1743 ((nil) "\\listoffigures")
1744 (minted "\\listoflistings")
1745 (otherwise "\\lstlistoflistings")))))))))
1748 ;;;; Latex Environment
1750 (defun org-latex-latex-environment (latex-environment contents info)
1751 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1752 CONTENTS is nil. INFO is a plist holding contextual information."
1753 (when (plist-get info :with-latex)
1754 (let ((label (org-element-property :name latex-environment))
1755 (value (org-remove-indentation
1756 (org-element-property :value latex-environment))))
1757 (if (not (org-string-nw-p label)) value
1758 ;; Environment is labeled: label must be within the environment
1759 ;; (otherwise, a reference pointing to that element will count
1760 ;; the section instead).
1761 (with-temp-buffer
1762 (insert value)
1763 (goto-char (point-min))
1764 (forward-line)
1765 (insert
1766 (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1767 (buffer-string))))))
1770 ;;;; Latex Fragment
1772 (defun org-latex-latex-fragment (latex-fragment contents info)
1773 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1774 CONTENTS is nil. INFO is a plist holding contextual information."
1775 (let ((value (org-element-property :value latex-fragment)))
1776 ;; Trim math markers since the fragment is enclosed within
1777 ;; a latex-math-block object anyway.
1778 (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
1779 (match-string 2 value))
1780 ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
1781 (match-string 1 value))
1782 (t value))))
1785 ;;;; Line Break
1787 (defun org-latex-line-break (line-break contents info)
1788 "Transcode a LINE-BREAK object from Org to LaTeX.
1789 CONTENTS is nil. INFO is a plist holding contextual information."
1790 "\\\\\n")
1793 ;;;; Link
1795 (defun org-latex--inline-image (link info)
1796 "Return LaTeX code for an inline image.
1797 LINK is the link pointing to the inline image. INFO is a plist
1798 used as a communication channel."
1799 (let* ((parent (org-export-get-parent-element link))
1800 (path (let ((raw-path (org-element-property :path link)))
1801 (if (not (file-name-absolute-p raw-path)) raw-path
1802 (expand-file-name raw-path))))
1803 (filetype (file-name-extension path))
1804 (caption (org-latex--caption/label-string parent info))
1805 ;; Retrieve latex attributes from the element around.
1806 (attr (org-export-read-attribute :attr_latex parent))
1807 (float (let ((float (plist-get attr :float)))
1808 (cond ((and (not float) (plist-member attr :float)) nil)
1809 ((string= float "wrap") 'wrap)
1810 ((string= float "sideways") 'sideways)
1811 ((string= float "multicolumn") 'multicolumn)
1812 ((or float
1813 (org-element-property :caption parent)
1814 (org-string-nw-p (plist-get attr :caption)))
1815 'figure))))
1816 (placement
1817 (let ((place (plist-get attr :placement)))
1818 (cond
1819 (place (format "%s" place))
1820 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1821 ((eq float 'figure)
1822 (format "[%s]" (plist-get info :latex-default-figure-position)))
1823 (t ""))))
1824 (comment-include (if (plist-get attr :comment-include) "%" ""))
1825 ;; It is possible to specify width and height in the
1826 ;; ATTR_LATEX line, and also via default variables.
1827 (width (cond ((plist-get attr :width))
1828 ((plist-get attr :height) "")
1829 ((eq float 'wrap) "0.48\\textwidth")
1830 (t (plist-get info :latex-image-default-width))))
1831 (height (cond ((plist-get attr :height))
1832 ((or (plist-get attr :width)
1833 (memq float '(figure wrap))) "")
1834 (t (plist-get info :latex-image-default-height))))
1835 (options (let ((opt (or (plist-get attr :options)
1836 (plist-get info :latex-image-default-option))))
1837 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
1838 (match-string 1 opt))))
1839 image-code)
1840 (if (member filetype '("tikz" "pgf"))
1841 ;; For tikz images:
1842 ;; - use \input to read in image file.
1843 ;; - if options are present, wrap in a tikzpicture environment.
1844 ;; - if width or height are present, use \resizebox to change
1845 ;; the image size.
1846 (progn
1847 (setq image-code (format "\\input{%s}" path))
1848 (when (org-string-nw-p options)
1849 (setq image-code
1850 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1851 options
1852 image-code)))
1853 (when (or (org-string-nw-p width) (org-string-nw-p height))
1854 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
1855 (if (org-string-nw-p width) width "!")
1856 (if (org-string-nw-p height) height "!")
1857 image-code))))
1858 ;; For other images:
1859 ;; - add width and height to options.
1860 ;; - include the image with \includegraphics.
1861 (when (org-string-nw-p width)
1862 (setq options (concat options ",width=" width)))
1863 (when (org-string-nw-p height)
1864 (setq options (concat options ",height=" height)))
1865 (let ((search-option (org-element-property :search-option link)))
1866 (when (and search-option
1867 (equal filetype "pdf")
1868 (org-string-match-p "\\`[0-9]+\\'" search-option)
1869 (not (org-string-match-p "page=" options)))
1870 (setq options (concat options ",page=" search-option))))
1871 (setq image-code
1872 (format "\\includegraphics%s{%s}"
1873 (cond ((not (org-string-nw-p options)) "")
1874 ((= (aref options 0) ?,)
1875 (format "[%s]"(substring options 1)))
1876 (t (format "[%s]" options)))
1877 path))
1878 (when (equal filetype "svg")
1879 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
1880 "\\includesvg"
1881 image-code
1882 nil t))
1883 (setq image-code (replace-regexp-in-string "\\.svg}"
1885 image-code
1886 nil t))))
1887 ;; Return proper string, depending on FLOAT.
1888 (case float
1889 (wrap (format "\\begin{wrapfigure}%s
1890 \\centering
1891 %s%s
1892 %s\\end{wrapfigure}" placement comment-include image-code caption))
1893 (sideways (format "\\begin{sidewaysfigure}
1894 \\centering
1895 %s%s
1896 %s\\end{sidewaysfigure}" comment-include image-code caption))
1897 (multicolumn (format "\\begin{figure*}%s
1898 \\centering
1899 %s%s
1900 %s\\end{figure*}" placement comment-include image-code caption))
1901 (figure (format "\\begin{figure}%s
1902 \\centering
1903 %s%s
1904 %s\\end{figure}" placement comment-include image-code caption))
1905 (otherwise image-code))))
1907 (defun org-latex-link (link desc info)
1908 "Transcode a LINK object from Org to LaTeX.
1910 DESC is the description part of the link, or the empty string.
1911 INFO is a plist holding contextual information. See
1912 `org-export-data'."
1913 (let* ((type (org-element-property :type link))
1914 (raw-path (replace-regexp-in-string
1915 "%" "\\%" (org-element-property :path link) nil t))
1916 ;; Ensure DESC really exists, or set it to nil.
1917 (desc (and (not (string= desc "")) desc))
1918 (imagep (org-export-inline-image-p
1919 link (plist-get info :latex-inline-image-rules)))
1920 (path (cond
1921 ((member type '("http" "https" "ftp" "mailto"))
1922 (concat type ":" raw-path))
1923 ((and (string= type "file") (file-name-absolute-p raw-path))
1924 (concat "file:" raw-path))
1925 (t raw-path)))
1926 protocol)
1927 (cond
1928 ;; Image file.
1929 (imagep (org-latex--inline-image link info))
1930 ;; Radio link: Transcode target's contents and use them as link's
1931 ;; description.
1932 ((string= type "radio")
1933 (let ((destination (org-export-resolve-radio-link link info)))
1934 (when destination
1935 (format "\\hyperref[%s]{%s}"
1936 (org-export-solidify-link-text
1937 (org-element-property :value destination))
1938 desc))))
1939 ;; Links pointing to a headline: Find destination and build
1940 ;; appropriate referencing command.
1941 ((member type '("custom-id" "fuzzy" "id"))
1942 (let ((destination (if (string= type "fuzzy")
1943 (org-export-resolve-fuzzy-link link info)
1944 (org-export-resolve-id-link link info))))
1945 (case (org-element-type destination)
1946 ;; Id link points to an external file.
1947 (plain-text
1948 (if desc (format "\\href{%s}{%s}" destination desc)
1949 (format "\\url{%s}" destination)))
1950 ;; Fuzzy link points nowhere.
1951 ('nil
1952 (format (plist-get info :latex-link-with-unknown-path-format)
1953 (or desc
1954 (org-export-data
1955 (org-element-property :raw-link link) info))))
1956 ;; LINK points to a headline. If headlines are numbered
1957 ;; and the link has no description, display headline's
1958 ;; number. Otherwise, display description or headline's
1959 ;; title.
1960 (headline
1961 (let* ((custom-label
1962 (and (plist-get info :latex-custom-id-labels)
1963 (org-element-property :CUSTOM_ID destination)))
1964 (label
1966 custom-label
1967 (format "sec-%s"
1968 (mapconcat
1969 #'number-to-string
1970 (org-export-get-headline-number destination info)
1971 "-")))))
1972 (if (and (plist-get info :section-numbers) (not desc))
1973 (format "\\ref{%s}" label)
1974 (format "\\hyperref[%s]{%s}" label
1975 (or desc
1976 (org-export-data
1977 (org-element-property :title destination) info))))))
1978 ;; Fuzzy link points to a target. Do as above.
1979 (otherwise
1980 (let ((path (org-export-solidify-link-text path)))
1981 (if (not desc) (format "\\ref{%s}" path)
1982 (format "\\hyperref[%s]{%s}" path desc)))))))
1983 ;; Coderef: replace link with the reference name or the
1984 ;; equivalent line number.
1985 ((string= type "coderef")
1986 (format (org-export-get-coderef-format path desc)
1987 (org-export-resolve-coderef path info)))
1988 ;; Link type is handled by a special function.
1989 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1990 (funcall protocol (org-link-unescape path) desc 'latex))
1991 ;; External link with a description part.
1992 ((and path desc) (format "\\href{%s}{%s}" path desc))
1993 ;; External link without a description part.
1994 (path (format "\\url{%s}" path))
1995 ;; No path, only description. Try to do something useful.
1996 (t (format (plist-get info :latex-link-with-unknown-path-format) desc)))))
1999 ;;;; Node Property
2001 (defun org-latex-node-property (node-property contents info)
2002 "Transcode a NODE-PROPERTY element from Org to LaTeX.
2003 CONTENTS is nil. INFO is a plist holding contextual
2004 information."
2005 (format "%s:%s"
2006 (org-element-property :key node-property)
2007 (let ((value (org-element-property :value node-property)))
2008 (if value (concat " " value) ""))))
2011 ;;;; Paragraph
2013 (defun org-latex-paragraph (paragraph contents info)
2014 "Transcode a PARAGRAPH element from Org to LaTeX.
2015 CONTENTS is the contents of the paragraph, as a string. INFO is
2016 the plist used as a communication channel."
2017 contents)
2020 ;;;; Plain List
2022 (defun org-latex-plain-list (plain-list contents info)
2023 "Transcode a PLAIN-LIST element from Org to LaTeX.
2024 CONTENTS is the contents of the list. INFO is a plist holding
2025 contextual information."
2026 (let* ((type (org-element-property :type plain-list))
2027 (attr (org-export-read-attribute :attr_latex plain-list))
2028 (latex-type (let ((env (plist-get attr :environment)))
2029 (cond (env (format "%s" env))
2030 ((eq type 'ordered) "enumerate")
2031 ((eq type 'descriptive) "description")
2032 (t "itemize")))))
2033 (org-latex--wrap-label
2034 plain-list
2035 (format "\\begin{%s}%s\n%s\\end{%s}"
2036 latex-type
2037 (or (plist-get attr :options) "")
2038 contents
2039 latex-type))))
2042 ;;;; Plain Text
2044 (defun org-latex-plain-text (text info)
2045 "Transcode a TEXT string from Org to LaTeX.
2046 TEXT is the string to transcode. INFO is a plist holding
2047 contextual information."
2048 (let ((specialp (plist-get info :with-special-strings))
2049 (output text))
2050 ;; Protect %, #, &, $, _, { and }.
2051 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output)
2052 (setq output
2053 (replace-match
2054 (format "\\%s" (match-string 2 output)) nil t output 2)))
2055 ;; Protect ^.
2056 (setq output
2057 (replace-regexp-in-string
2058 "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil 2))
2059 ;; Protect \. If special strings are used, be careful not to
2060 ;; protect "\" in "\-" constructs.
2061 (let ((symbols (if specialp "-%$#&{}^_\\" "%$#&{}^_\\")))
2062 (setq output
2063 (replace-regexp-in-string
2064 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
2065 "$\\backslash$" output nil t 1)))
2066 ;; Protect ~.
2067 (setq output
2068 (replace-regexp-in-string
2069 "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t 2))
2070 ;; Activate smart quotes. Be sure to provide original TEXT string
2071 ;; since OUTPUT may have been modified.
2072 (when (plist-get info :with-smart-quotes)
2073 (setq output (org-export-activate-smart-quotes output :latex info text)))
2074 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
2075 (let ((case-fold-search nil)
2076 (start 0))
2077 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
2078 (setq output (replace-match
2079 (format "\\%s{}" (match-string 1 output)) nil t output)
2080 start (match-end 0))))
2081 ;; Convert special strings.
2082 (when specialp
2083 (setq output
2084 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
2085 ;; Handle break preservation if required.
2086 (when (plist-get info :preserve-breaks)
2087 (setq output (replace-regexp-in-string
2088 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
2089 ;; Return value.
2090 output))
2093 ;;;; Planning
2095 (defun org-latex-planning (planning contents info)
2096 "Transcode a PLANNING element from Org to LaTeX.
2097 CONTENTS is nil. INFO is a plist holding contextual
2098 information."
2099 (concat
2100 "\\noindent"
2101 (mapconcat
2102 'identity
2103 (delq nil
2104 (list
2105 (let ((closed (org-element-property :closed planning)))
2106 (when closed
2107 (concat
2108 (format "\\textbf{%s} " org-closed-string)
2109 (format (plist-get info :latex-inactive-timestamp-format)
2110 (org-translate-time
2111 (org-element-property :raw-value closed))))))
2112 (let ((deadline (org-element-property :deadline planning)))
2113 (when deadline
2114 (concat
2115 (format "\\textbf{%s} " org-deadline-string)
2116 (format (plist-get info :latex-active-timestamp-format)
2117 (org-translate-time
2118 (org-element-property :raw-value deadline))))))
2119 (let ((scheduled (org-element-property :scheduled planning)))
2120 (when scheduled
2121 (concat
2122 (format "\\textbf{%s} " org-scheduled-string)
2123 (format (plist-get info :latex-active-timestamp-format)
2124 (org-translate-time
2125 (org-element-property :raw-value scheduled))))))))
2126 " ")
2127 "\\\\"))
2130 ;;;; Property Drawer
2132 (defun org-latex-property-drawer (property-drawer contents info)
2133 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
2134 CONTENTS holds the contents of the drawer. INFO is a plist
2135 holding contextual information."
2136 (and (org-string-nw-p contents)
2137 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
2140 ;;;; Pseudo Object: LaTeX Math Block
2142 (defun org-latex--wrap-latex-math-block (data info)
2143 "Merge contiguous math objects in a pseudo-object container.
2144 DATA is a parse tree or a secondary string. INFO is a plist
2145 containing export options. Modify DATA by side-effect and return it."
2146 (let ((valid-object-p
2147 (function
2148 ;; Non-nil when OBJ can be added to the latex math block.
2149 (lambda (obj)
2150 (case (org-element-type obj)
2151 (entity (org-element-property :latex-math-p obj))
2152 (latex-fragment
2153 (let ((value (org-element-property :value obj)))
2154 (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
2155 (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
2156 ((subscript superscript) t))))))
2157 (org-element-map data '(entity latex-fragment subscript superscript)
2158 (lambda (object)
2159 ;; Skip objects already wrapped.
2160 (when (and (not (eq (org-element-type
2161 (org-element-property :parent object))
2162 'latex-math-block))
2163 (funcall valid-object-p object))
2164 (let ((math-block (list 'latex-math-block nil))
2165 (next-elements (org-export-get-next-element object info t))
2166 (last object))
2167 ;; Wrap MATH-BLOCK around OBJECT in DATA.
2168 (org-element-insert-before math-block object)
2169 (org-element-extract-element object)
2170 (org-element-adopt-elements math-block object)
2171 (when (zerop (or (org-element-property :post-blank object) 0))
2172 ;; MATH-BLOCK swallows consecutive math objects.
2173 (catch 'exit
2174 (dolist (next next-elements)
2175 (if (not (funcall valid-object-p next)) (throw 'exit nil)
2176 (org-element-extract-element next)
2177 (org-element-adopt-elements math-block next)
2178 ;; Eschew the case: \beta$x$ -> \(\betax\).
2179 (unless (memq (org-element-type next)
2180 '(subscript superscript))
2181 (org-element-put-property last :post-blank 1))
2182 (setq last next)
2183 (when (> (or (org-element-property :post-blank next) 0) 0)
2184 (throw 'exit nil))))))
2185 (org-element-put-property
2186 math-block :post-blank (org-element-property :post-blank last)))))
2187 info nil '(subscript superscript latex-math-block) t)
2188 ;; Return updated DATA.
2189 data))
2191 (defun org-latex-math-block-tree-filter (tree backend info)
2192 (org-latex--wrap-latex-math-block tree info))
2194 (defun org-latex-math-block-options-filter (info backend)
2195 (dolist (prop '(:author :date :title) info)
2196 (plist-put info prop
2197 (org-latex--wrap-latex-math-block (plist-get info prop) info))))
2199 (defun org-latex-math-block (math-block contents info)
2200 "Transcode a MATH-BLOCK object from Org to LaTeX.
2201 CONTENTS is a string. INFO is a plist used as a communication
2202 channel."
2203 (when (org-string-nw-p contents)
2204 (format "\\(%s\\)" (org-trim contents))))
2206 ;;;; Quote Block
2208 (defun org-latex-quote-block (quote-block contents info)
2209 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2210 CONTENTS holds the contents of the block. INFO is a plist
2211 holding contextual information."
2212 (org-latex--wrap-label
2213 quote-block
2214 (format "\\begin{quote}\n%s\\end{quote}" contents)))
2217 ;;;; Radio Target
2219 (defun org-latex-radio-target (radio-target text info)
2220 "Transcode a RADIO-TARGET object from Org to LaTeX.
2221 TEXT is the text of the target. INFO is a plist holding
2222 contextual information."
2223 (format "\\label{%s}%s"
2224 (org-export-solidify-link-text
2225 (org-element-property :value radio-target))
2226 text))
2229 ;;;; Section
2231 (defun org-latex-section (section contents info)
2232 "Transcode a SECTION element from Org to LaTeX.
2233 CONTENTS holds the contents of the section. INFO is a plist
2234 holding contextual information."
2235 contents)
2238 ;;;; Special Block
2240 (defun org-latex-special-block (special-block contents info)
2241 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2242 CONTENTS holds the contents of the block. INFO is a plist
2243 holding contextual information."
2244 (if (org-export-raw-special-block-p special-block info)
2245 (org-remove-indentation (org-element-property :raw-value special-block))
2246 (let ((type (downcase (org-element-property :type special-block)))
2247 (opt (org-export-read-attribute :attr_latex special-block :options)))
2248 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2249 ;; Insert any label or caption within the block
2250 ;; (otherwise, a reference pointing to that element will
2251 ;; count the section instead).
2252 (org-latex--caption/label-string special-block info)
2253 contents
2254 (format "\\end{%s}" type)))))
2257 ;;;; Src Block
2259 (defun org-latex-src-block (src-block contents info)
2260 "Transcode a SRC-BLOCK element from Org to LaTeX.
2261 CONTENTS holds the contents of the item. INFO is a plist holding
2262 contextual information."
2263 (when (org-string-nw-p (org-element-property :value src-block))
2264 (let* ((lang (org-element-property :language src-block))
2265 (caption (org-element-property :caption src-block))
2266 (label (org-element-property :name src-block))
2267 (custom-env (and lang
2268 (cadr (assq (intern lang)
2269 org-latex-custom-lang-environments))))
2270 (num-start (case (org-element-property :number-lines src-block)
2271 (continued (org-export-get-loc src-block info))
2272 (new 0)))
2273 (retain-labels (org-element-property :retain-labels src-block))
2274 (attributes (org-export-read-attribute :attr_latex src-block))
2275 (float (plist-get attributes :float))
2276 (listings (plist-get info :latex-listings)))
2277 (cond
2278 ;; Case 1. No source fontification.
2279 ((not listings)
2280 (let* ((caption-str (org-latex--caption/label-string src-block info))
2281 (float-env
2282 (cond ((and (not float) (plist-member attributes :float)) "%s")
2283 ((string= "multicolumn" float)
2284 (format "\\begin{figure*}[%s]\n%%s%s\n\\end{figure*}"
2285 (plist-get info :latex-default-figure-position)
2286 caption-str))
2287 ((or caption float)
2288 (format "\\begin{figure}[H]\n%%s%s\n\\end{figure}"
2289 caption-str))
2290 (t "%s"))))
2291 (format
2292 float-env
2293 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2294 (org-export-format-code-default src-block info))))))
2295 ;; Case 2. Custom environment.
2296 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2297 custom-env
2298 (org-export-format-code-default src-block info)
2299 custom-env))
2300 ;; Case 3. Use minted package.
2301 ((eq listings 'minted)
2302 (let* ((caption-str (org-latex--caption/label-string src-block info))
2303 (float-env
2304 (cond ((and (not float) (plist-member attributes :float)) "%s")
2305 ((string= "multicolumn" float)
2306 (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
2307 caption-str))
2308 ((or caption float)
2309 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2310 caption-str))
2311 (t "%s")))
2312 (options (plist-get info :latex-minted-options))
2313 (body
2314 (format
2315 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2316 ;; Options.
2317 (concat
2318 (org-latex--make-option-string
2319 (if (or (not num-start) (assoc "linenos" options))
2320 options
2321 (append
2322 `(("linenos")
2323 ("firstnumber" ,(number-to-string (1+ num-start))))
2324 options)))
2325 (let ((local-options (plist-get attributes :options)))
2326 (and local-options (concat "," local-options))))
2327 ;; Language.
2328 (or (cadr (assq (intern lang)
2329 (plist-get info :latex-minted-langs)))
2330 (downcase lang))
2331 ;; Source code.
2332 (let* ((code-info (org-export-unravel-code src-block))
2333 (max-width
2334 (apply 'max
2335 (mapcar 'length
2336 (org-split-string (car code-info)
2337 "\n")))))
2338 (org-export-format-code
2339 (car code-info)
2340 (lambda (loc num ref)
2341 (concat
2343 (when ref
2344 ;; Ensure references are flushed to the right,
2345 ;; separated with 6 spaces from the widest line
2346 ;; of code.
2347 (concat (make-string (+ (- max-width (length loc)) 6)
2348 ?\s)
2349 (format "(%s)" ref)))))
2350 nil (and retain-labels (cdr code-info)))))))
2351 ;; Return value.
2352 (format float-env body)))
2353 ;; Case 4. Use listings package.
2355 (let ((lst-lang
2356 (or (cadr (assq (intern lang)
2357 (plist-get info :latex-listings-langs)))
2358 lang))
2359 (caption-str
2360 (when caption
2361 (let ((main (org-export-get-caption src-block))
2362 (secondary (org-export-get-caption src-block t)))
2363 (if (not secondary)
2364 (format "{%s}" (org-export-data main info))
2365 (format "{[%s]%s}"
2366 (org-export-data secondary info)
2367 (org-export-data main info))))))
2368 (lst-opt (plist-get info :latex-listings-options)))
2369 (concat
2370 ;; Options.
2371 (format
2372 "\\lstset{%s}\n"
2373 (concat
2374 (org-latex--make-option-string
2375 (append
2376 lst-opt
2377 (cond
2378 ((and (not float) (plist-member attributes :float)) nil)
2379 ((string= "multicolumn" float) '(("float" "*")))
2380 ((and float (not (assoc "float" lst-opt)))
2381 `(("float" ,(plist-get info :latex-default-figure-position)))))
2382 `(("language" ,lst-lang))
2383 (if label `(("label" ,label)) '(("label" " ")))
2384 (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
2385 (cond ((assoc "numbers" lst-opt) nil)
2386 ((not num-start) '(("numbers" "none")))
2387 ((zerop num-start) '(("numbers" "left")))
2388 (t `(("numbers" "left")
2389 ("firstnumber"
2390 ,(number-to-string (1+ num-start))))))))
2391 (let ((local-options (plist-get attributes :options)))
2392 (and local-options (concat "," local-options)))))
2393 ;; Source code.
2394 (format
2395 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2396 (let* ((code-info (org-export-unravel-code src-block))
2397 (max-width
2398 (apply 'max
2399 (mapcar 'length
2400 (org-split-string (car code-info) "\n")))))
2401 (org-export-format-code
2402 (car code-info)
2403 (lambda (loc num ref)
2404 (concat
2406 (when ref
2407 ;; Ensure references are flushed to the right,
2408 ;; separated with 6 spaces from the widest line of
2409 ;; code
2410 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2411 (format "(%s)" ref)))))
2412 nil (and retain-labels (cdr code-info))))))))))))
2415 ;;;; Statistics Cookie
2417 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2418 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2419 CONTENTS is nil. INFO is a plist holding contextual information."
2420 (replace-regexp-in-string
2421 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2424 ;;;; Strike-Through
2426 (defun org-latex-strike-through (strike-through contents info)
2427 "Transcode STRIKE-THROUGH from Org to LaTeX.
2428 CONTENTS is the text with strike-through markup. INFO is a plist
2429 holding contextual information."
2430 (org-latex--text-markup contents 'strike-through info))
2433 ;;;; Subscript
2435 (defun org-latex--script-size (object info)
2436 "Transcode a subscript or superscript object.
2437 OBJECT is an Org object. INFO is a plist used as a communication
2438 channel."
2439 (let ((type (org-element-type object))
2440 (output ""))
2441 (org-element-map (org-element-contents object)
2442 (cons 'plain-text org-element-all-objects)
2443 (lambda (obj)
2444 (case (org-element-type obj)
2445 ((entity latex-fragment)
2446 (let ((data (org-trim (org-export-data obj info))))
2447 (string-match
2448 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2449 data)
2450 (setq output
2451 (concat output
2452 (match-string 1 data)
2453 (let ((blank (org-element-property :post-blank obj)))
2454 (and blank (> blank 0) "\\ "))))))
2455 (plain-text
2456 (setq output
2457 (format "%s\\text{%s}" output (org-export-data obj info))))
2458 (otherwise
2459 (setq output
2460 (concat output
2461 (org-export-data obj info)
2462 (let ((blank (org-element-property :post-blank obj)))
2463 (and blank (> blank 0) "\\ ")))))))
2464 info nil org-element-recursive-objects)
2465 ;; Result. Do not wrap into curly brackets if OUTPUT is a single
2466 ;; character.
2467 (concat (if (eq (org-element-type object) 'subscript) "_" "^")
2468 (and (> (length output) 1) "{")
2469 output
2470 (and (> (length output) 1) "}"))))
2472 (defun org-latex-subscript (subscript contents info)
2473 "Transcode a SUBSCRIPT 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 subscript info))
2479 ;;;; Superscript
2481 (defun org-latex-superscript (superscript contents info)
2482 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2483 CONTENTS is the contents of the object. INFO is a plist holding
2484 contextual information."
2485 (org-latex--script-size superscript info))
2488 ;;;; Table
2490 ;; `org-latex-table' is the entry point for table transcoding. It
2491 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2492 ;; delegates the job to either `org-latex--table.el-table',
2493 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2494 ;; depending of the type of the table and the mode requested.
2496 ;; `org-latex--align-string' is a subroutine used to build alignment
2497 ;; string for Org tables.
2499 (defun org-latex-table (table contents info)
2500 "Transcode a TABLE element from Org to LaTeX.
2501 CONTENTS is the contents of the table. INFO is a plist holding
2502 contextual information."
2503 (if (eq (org-element-property :type table) 'table.el)
2504 ;; "table.el" table. Convert it using appropriate tools.
2505 (org-latex--table.el-table table info)
2506 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2507 (plist-get info :latex-default-table-mode))))
2508 (cond
2509 ;; Case 1: Verbatim table.
2510 ((string= type "verbatim")
2511 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2512 ;; Re-create table, without affiliated keywords.
2513 (org-trim (org-element-interpret-data
2514 `(table nil ,@(org-element-contents table))
2515 org-latex-pseudo-objects))))
2516 ;; Case 2: Matrix.
2517 ((or (string= type "math") (string= type "inline-math"))
2518 (org-latex--math-table table info))
2519 ;; Case 3: Standard table.
2520 (t (concat (org-latex--org-table table contents info)
2521 ;; When there are footnote references within the
2522 ;; table, insert their definition just after it.
2523 (org-latex--delayed-footnotes-definitions table info)))))))
2525 (defun org-latex--align-string (table info)
2526 "Return an appropriate LaTeX alignment string.
2527 TABLE is the considered table. INFO is a plist used as
2528 a communication channel."
2529 (or (org-export-read-attribute :attr_latex table :align)
2530 (let (align)
2531 ;; Extract column groups and alignment from first (non-rule)
2532 ;; row.
2533 (org-element-map
2534 (org-element-map table 'table-row
2535 (lambda (row)
2536 (and (eq (org-element-property :type row) 'standard) row))
2537 info 'first-match)
2538 'table-cell
2539 (lambda (cell)
2540 (let ((borders (org-export-table-cell-borders cell info)))
2541 ;; Check left border for the first cell only.
2542 (when (and (memq 'left borders) (not align))
2543 (push "|" align))
2544 (push (case (org-export-table-cell-alignment cell info)
2545 (left "l")
2546 (right "r")
2547 (center "c"))
2548 align)
2549 (when (memq 'right borders) (push "|" align))))
2550 info)
2551 (apply 'concat (nreverse align)))))
2553 (defun org-latex--org-table (table contents info)
2554 "Return appropriate LaTeX code for an Org table.
2556 TABLE is the table type element to transcode. CONTENTS is its
2557 contents, as a string. INFO is a plist used as a communication
2558 channel.
2560 This function assumes TABLE has `org' as its `:type' property and
2561 `table' as its `:mode' attribute."
2562 (let* ((caption (org-latex--caption/label-string table info))
2563 (attr (org-export-read-attribute :attr_latex table))
2564 ;; Determine alignment string.
2565 (alignment (org-latex--align-string table info))
2566 ;; Determine environment for the table: longtable, tabular...
2567 (table-env (or (plist-get attr :environment)
2568 (plist-get info :latex-default-table-environment)))
2569 ;; If table is a float, determine environment: table, table*
2570 ;; or sidewaystable.
2571 (float-env (unless (member table-env '("longtable" "longtabu"))
2572 (let ((float (plist-get attr :float)))
2573 (cond
2574 ((and (not float) (plist-member attr :float)) nil)
2575 ((or (string= float "sidewaystable")
2576 (string= float "sideways")) "sidewaystable")
2577 ((string= float "multicolumn") "table*")
2578 ((or float
2579 (org-element-property :caption table)
2580 (org-string-nw-p (plist-get attr :caption)))
2581 "table")))))
2582 ;; Extract others display options.
2583 (fontsize (let ((font (plist-get attr :font)))
2584 (and font (concat font "\n"))))
2585 (width (plist-get attr :width))
2586 (spreadp (plist-get attr :spread))
2587 (placement
2588 (or (plist-get attr :placement)
2589 (format "[%s]" (plist-get info :latex-default-figure-position))))
2590 (centerp (if (plist-member attr :center) (plist-get attr :center)
2591 (plist-get info :latex-tables-centered)))
2592 (caption-above-p (plist-get info :latex-table-caption-above)))
2593 ;; Prepare the final format string for the table.
2594 (cond
2595 ;; Longtable.
2596 ((equal "longtable" table-env)
2597 (concat (and fontsize (concat "{" fontsize))
2598 (format "\\begin{longtable}{%s}\n" alignment)
2599 (and caption-above-p
2600 (org-string-nw-p caption)
2601 (concat caption "\\\\\n"))
2602 contents
2603 (and (not caption-above-p)
2604 (org-string-nw-p caption)
2605 (concat caption "\\\\\n"))
2606 "\\end{longtable}\n"
2607 (and fontsize "}")))
2608 ;; Longtabu
2609 ((equal "longtabu" table-env)
2610 (concat (and fontsize (concat "{" fontsize))
2611 (format "\\begin{longtabu}%s{%s}\n"
2612 (if width
2613 (format " %s %s "
2614 (if spreadp "spread" "to") width) "")
2615 alignment)
2616 (and caption-above-p
2617 (org-string-nw-p caption)
2618 (concat caption "\\\\\n"))
2619 contents
2620 (and (not caption-above-p)
2621 (org-string-nw-p caption)
2622 (concat caption "\\\\\n"))
2623 "\\end{longtabu}\n"
2624 (and fontsize "}")))
2625 ;; Others.
2626 (t (concat (cond
2627 (float-env
2628 (concat (format "\\begin{%s}%s\n" float-env placement)
2629 (if caption-above-p caption "")
2630 (when centerp "\\centering\n")
2631 fontsize))
2632 (centerp (concat "\\begin{center}\n" fontsize))
2633 (fontsize (concat "{" fontsize)))
2634 (cond ((equal "tabu" table-env)
2635 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2636 (if width (format
2637 (if spreadp " spread %s " " to %s ")
2638 width) "")
2639 alignment
2640 contents))
2641 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2642 table-env
2643 (if width (format "{%s}" width) "")
2644 alignment
2645 contents
2646 table-env)))
2647 (cond
2648 (float-env
2649 (concat (if caption-above-p "" caption)
2650 (format "\n\\end{%s}" float-env)))
2651 (centerp "\n\\end{center}")
2652 (fontsize "}")))))))
2654 (defun org-latex--table.el-table (table info)
2655 "Return appropriate LaTeX code for a table.el table.
2657 TABLE is the table type element to transcode. INFO is a plist
2658 used as a communication channel.
2660 This function assumes TABLE has `table.el' as its `:type'
2661 property."
2662 (require 'table)
2663 ;; Ensure "*org-export-table*" buffer is empty.
2664 (with-current-buffer (get-buffer-create "*org-export-table*")
2665 (erase-buffer))
2666 (let ((output (with-temp-buffer
2667 (insert (org-element-property :value table))
2668 (goto-char 1)
2669 (re-search-forward "^[ \t]*|[^|]" nil t)
2670 (table-generate-source 'latex "*org-export-table*")
2671 (with-current-buffer "*org-export-table*"
2672 (org-trim (buffer-string))))))
2673 (kill-buffer (get-buffer "*org-export-table*"))
2674 ;; Remove left out comments.
2675 (while (string-match "^%.*\n" output)
2676 (setq output (replace-match "" t t output)))
2677 (let ((attr (org-export-read-attribute :attr_latex table)))
2678 (when (plist-get attr :rmlines)
2679 ;; When the "rmlines" attribute is provided, remove all hlines
2680 ;; but the the one separating heading from the table body.
2681 (let ((n 0) (pos 0))
2682 (while (and (< (length output) pos)
2683 (setq pos (string-match "^\\\\hline\n?" output pos)))
2684 (incf n)
2685 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2686 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2687 (plist-get info :latex-tables-centered))))
2688 (if (not centerp) output
2689 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2691 (defun org-latex--math-table (table info)
2692 "Return appropriate LaTeX code for a matrix.
2694 TABLE is the table type element to transcode. INFO is a plist
2695 used as a communication channel.
2697 This function assumes TABLE has `org' as its `:type' property and
2698 `inline-math' or `math' as its `:mode' attribute.."
2699 (let* ((caption (org-latex--caption/label-string table info))
2700 (attr (org-export-read-attribute :attr_latex table))
2701 (inlinep (equal (plist-get attr :mode) "inline-math"))
2702 (env (or (plist-get attr :environment)
2703 (plist-get info :latex-default-table-environment)))
2704 (contents
2705 (mapconcat
2706 (lambda (row)
2707 ;; Ignore horizontal rules.
2708 (when (eq (org-element-property :type row) 'standard)
2709 ;; Return each cell unmodified.
2710 (concat
2711 (mapconcat
2712 (lambda (cell)
2713 (substring
2714 (org-element-interpret-data cell org-latex-pseudo-objects)
2715 0 -1))
2716 (org-element-map row 'table-cell 'identity info) "&")
2717 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2718 "\n")))
2719 (org-element-map table 'table-row 'identity info) ""))
2720 ;; Variables related to math clusters (contiguous math tables
2721 ;; of the same type).
2722 (mode (org-export-read-attribute :attr_latex table :mode))
2723 (prev (org-export-get-previous-element table info))
2724 (next (org-export-get-next-element table info))
2725 (same-mode-p
2726 (lambda (table)
2727 ;; Non-nil when TABLE has the same mode as current table.
2728 (string= (or (org-export-read-attribute :attr_latex table :mode)
2729 (plist-get info :latex-default-table-mode))
2730 mode))))
2731 (concat
2732 ;; Opening string. If TABLE is in the middle of a table cluster,
2733 ;; do not insert any.
2734 (cond ((and prev
2735 (eq (org-element-type prev) 'table)
2736 (memq (org-element-property :post-blank prev) '(0 nil))
2737 (funcall same-mode-p prev))
2738 nil)
2739 (inlinep "\\(")
2740 ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
2741 (t "\\["))
2742 ;; Prefix.
2743 (or (plist-get attr :math-prefix) "")
2744 ;; Environment. Also treat special cases.
2745 (cond ((equal env "array")
2746 (let ((align (org-latex--align-string table info)))
2747 (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
2748 ((assoc env org-latex-table-matrix-macros)
2749 (format "\\%s%s{\n%s}"
2751 (or (plist-get attr :math-arguments) "")
2752 contents))
2753 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2754 ;; Suffix.
2755 (or (plist-get attr :math-suffix) "")
2756 ;; Closing string. If TABLE is in the middle of a table cluster,
2757 ;; do not insert any. If it closes such a cluster, be sure to
2758 ;; close the cluster with a string matching the opening string.
2759 (cond ((and next
2760 (eq (org-element-type next) 'table)
2761 (memq (org-element-property :post-blank table) '(0 nil))
2762 (funcall same-mode-p next))
2763 nil)
2764 (inlinep "\\)")
2765 ;; Find cluster beginning to know which environment to use.
2766 ((let ((cluster-beg table) prev)
2767 (while (and (setq prev (org-export-get-previous-element
2768 cluster-beg info))
2769 (memq (org-element-property :post-blank prev)
2770 '(0 nil))
2771 (funcall same-mode-p prev))
2772 (setq cluster-beg prev))
2773 (and (or (org-element-property :caption cluster-beg)
2774 (org-element-property :name cluster-beg))
2775 "\n\\end{equation}")))
2776 (t "\\]")))))
2779 ;;;; Table Cell
2781 (defun org-latex-table-cell (table-cell contents info)
2782 "Transcode a TABLE-CELL element from Org to LaTeX.
2783 CONTENTS is the cell contents. INFO is a plist used as
2784 a communication channel."
2785 (concat
2786 (let ((scientific-format (plist-get info :latex-table-scientific-notation)))
2787 (if (and contents
2788 scientific-format
2789 (string-match orgtbl-exp-regexp contents))
2790 ;; Use appropriate format string for scientific
2791 ;; notation.
2792 (format scientific-format
2793 (match-string 1 contents)
2794 (match-string 2 contents))
2795 contents))
2796 (when (org-export-get-next-element table-cell info) " & ")))
2799 ;;;; Table Row
2801 (defun org-latex-table-row (table-row contents info)
2802 "Transcode a TABLE-ROW element from Org to LaTeX.
2803 CONTENTS is the contents of the row. INFO is a plist used as
2804 a communication channel."
2805 ;; Rules are ignored since table separators are deduced from
2806 ;; borders of the current row.
2807 (when (eq (org-element-property :type table-row) 'standard)
2808 (let* ((attr (org-export-read-attribute :attr_latex
2809 (org-export-get-parent table-row)))
2810 (longtablep
2811 (member (or (plist-get attr :environment)
2812 (plist-get info :latex-default-table-environment))
2813 '("longtable" "longtabu")))
2814 (booktabsp (if (plist-member attr :booktabs)
2815 (plist-get attr :booktabs)
2816 (plist-get info :latex-tables-booktabs)))
2817 ;; TABLE-ROW's borders are extracted from its first cell.
2818 (borders (org-export-table-cell-borders
2819 (car (org-element-contents table-row)) info)))
2820 (concat
2821 ;; When BOOKTABS are activated enforce top-rule even when no
2822 ;; hline was specifically marked.
2823 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2824 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2825 contents "\\\\\n"
2826 (cond
2827 ;; Special case for long tables. Define header and footers.
2828 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2829 (format "%s
2830 \\endfirsthead
2831 \\multicolumn{%d}{l}{%s} \\\\
2833 %s \\\\\n
2835 \\endhead
2836 %s\\multicolumn{%d}{r}{%s} \\\\
2837 \\endfoot
2838 \\endlastfoot"
2839 (if booktabsp "\\midrule" "\\hline")
2840 (cdr (org-export-table-dimensions
2841 (org-export-get-parent-table table-row) info))
2842 (org-latex--translate "Continued from previous page" info)
2843 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2844 ((and (memq 'top borders)
2845 (memq 'above borders)) "\\hline\n")
2846 (t ""))
2847 contents
2848 (if booktabsp "\\midrule" "\\hline")
2849 (if booktabsp "\\midrule" "\\hline")
2850 ;; Number of columns.
2851 (cdr (org-export-table-dimensions
2852 (org-export-get-parent-table table-row) info))
2853 (org-latex--translate "Continued on next page" info)))
2854 ;; When BOOKTABS are activated enforce bottom rule even when
2855 ;; no hline was specifically marked.
2856 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2857 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2858 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2861 ;;;; Target
2863 (defun org-latex-target (target contents info)
2864 "Transcode a TARGET object from Org to LaTeX.
2865 CONTENTS is nil. INFO is a plist holding contextual
2866 information."
2867 (format "\\label{%s}"
2868 (org-export-solidify-link-text (org-element-property :value target))))
2871 ;;;; Timestamp
2873 (defun org-latex-timestamp (timestamp contents info)
2874 "Transcode a TIMESTAMP object from Org to LaTeX.
2875 CONTENTS is nil. INFO is a plist holding contextual
2876 information."
2877 (let ((value (org-latex-plain-text
2878 (org-timestamp-translate timestamp) info)))
2879 (case (org-element-property :type timestamp)
2880 ((active active-range)
2881 (format (plist-get info :latex-active-timestamp-format) value))
2882 ((inactive inactive-range)
2883 (format (plist-get info :latex-inactive-timestamp-format) value))
2884 (otherwise
2885 (format (plist-get info :latex-diary-timestamp-format) value)))))
2888 ;;;; Underline
2890 (defun org-latex-underline (underline contents info)
2891 "Transcode UNDERLINE from Org to LaTeX.
2892 CONTENTS is the text with underline markup. INFO is a plist
2893 holding contextual information."
2894 (org-latex--text-markup contents 'underline info))
2897 ;;;; Verbatim
2899 (defun org-latex-verbatim (verbatim contents info)
2900 "Transcode a VERBATIM object from Org to LaTeX.
2901 CONTENTS is nil. INFO is a plist used as a communication
2902 channel."
2903 (org-latex--text-markup
2904 (org-element-property :value verbatim) 'verbatim info))
2907 ;;;; Verse Block
2909 (defun org-latex-verse-block (verse-block contents info)
2910 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2911 CONTENTS is verse block contents. INFO is a plist holding
2912 contextual information."
2913 (org-latex--wrap-label
2914 verse-block
2915 ;; In a verse environment, add a line break to each newline
2916 ;; character and change each white space at beginning of a line
2917 ;; into a space of 1 em. Also change each blank line with
2918 ;; a vertical space of 1 em.
2919 (progn
2920 (setq contents (replace-regexp-in-string
2921 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2922 (replace-regexp-in-string
2923 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2924 (while (string-match "^[ \t]+" contents)
2925 (let ((new-str (format "\\hspace*{%dem}"
2926 (length (match-string 0 contents)))))
2927 (setq contents (replace-match new-str nil t contents))))
2928 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2932 ;;; End-user functions
2934 ;;;###autoload
2935 (defun org-latex-export-as-latex
2936 (&optional async subtreep visible-only body-only ext-plist)
2937 "Export current buffer as a LaTeX buffer.
2939 If narrowing is active in the current buffer, only export its
2940 narrowed part.
2942 If a region is active, export that region.
2944 A non-nil optional argument ASYNC means the process should happen
2945 asynchronously. The resulting buffer should be accessible
2946 through the `org-export-stack' interface.
2948 When optional argument SUBTREEP is non-nil, export the sub-tree
2949 at point, extracting information from the headline properties
2950 first.
2952 When optional argument VISIBLE-ONLY is non-nil, don't export
2953 contents of hidden elements.
2955 When optional argument BODY-ONLY is non-nil, only write code
2956 between \"\\begin{document}\" and \"\\end{document}\".
2958 EXT-PLIST, when provided, is a property list with external
2959 parameters overriding Org default settings, but still inferior to
2960 file-local settings.
2962 Export is done in a buffer named \"*Org LATEX Export*\", which
2963 will be displayed when `org-export-show-temporary-export-buffer'
2964 is non-nil."
2965 (interactive)
2966 (org-export-to-buffer 'latex "*Org LATEX Export*"
2967 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
2969 ;;;###autoload
2970 (defun org-latex-convert-region-to-latex ()
2971 "Assume the current region has org-mode syntax, and convert it to LaTeX.
2972 This can be used in any buffer. For example, you can write an
2973 itemized list in org-mode syntax in an LaTeX buffer and use this
2974 command to convert it."
2975 (interactive)
2976 (org-export-replace-region-by 'latex))
2978 ;;;###autoload
2979 (defun org-latex-export-to-latex
2980 (&optional async subtreep visible-only body-only ext-plist)
2981 "Export current buffer to a LaTeX file.
2983 If narrowing is active in the current buffer, only export its
2984 narrowed part.
2986 If a region is active, export that region.
2988 A non-nil optional argument ASYNC means the process should happen
2989 asynchronously. The resulting file should be accessible through
2990 the `org-export-stack' interface.
2992 When optional argument SUBTREEP is non-nil, export the sub-tree
2993 at point, extracting information from the headline properties
2994 first.
2996 When optional argument VISIBLE-ONLY is non-nil, don't export
2997 contents of hidden elements.
2999 When optional argument BODY-ONLY is non-nil, only write code
3000 between \"\\begin{document}\" and \"\\end{document}\".
3002 EXT-PLIST, when provided, is a property list with external
3003 parameters overriding Org default settings, but still inferior to
3004 file-local settings."
3005 (interactive)
3006 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3007 (org-export-to-file 'latex outfile
3008 async subtreep visible-only body-only ext-plist)))
3010 ;;;###autoload
3011 (defun org-latex-export-to-pdf
3012 (&optional async subtreep visible-only body-only ext-plist)
3013 "Export current buffer to LaTeX then process through to PDF.
3015 If narrowing is active in the current buffer, only export its
3016 narrowed part.
3018 If a region is active, export that region.
3020 A non-nil optional argument ASYNC means the process should happen
3021 asynchronously. The resulting file should be accessible through
3022 the `org-export-stack' interface.
3024 When optional argument SUBTREEP is non-nil, export the sub-tree
3025 at point, extracting information from the headline properties
3026 first.
3028 When optional argument VISIBLE-ONLY is non-nil, don't export
3029 contents of hidden elements.
3031 When optional argument BODY-ONLY is non-nil, only write code
3032 between \"\\begin{document}\" and \"\\end{document}\".
3034 EXT-PLIST, when provided, is a property list with external
3035 parameters overriding Org default settings, but still inferior to
3036 file-local settings.
3038 Return PDF file's name."
3039 (interactive)
3040 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
3041 (org-export-to-file 'latex outfile
3042 async subtreep visible-only body-only ext-plist
3043 (lambda (file) (org-latex-compile file)))))
3045 (defun org-latex-compile (texfile &optional snippet)
3046 "Compile a TeX file.
3048 TEXFILE is the name of the file being compiled. Processing is
3049 done through the command specified in `org-latex-pdf-process'.
3051 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
3052 file used to preview a LaTeX snippet. In this case, do not
3053 create a log buffer and do not bother removing log files.
3055 Return PDF file name or an error if it couldn't be produced."
3056 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
3057 (full-name (file-truename texfile))
3058 (out-dir (file-name-directory texfile))
3059 ;; Properly set working directory for compilation.
3060 (default-directory (if (file-name-absolute-p texfile)
3061 (file-name-directory full-name)
3062 default-directory))
3063 (time (current-time))
3064 warnings)
3065 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
3066 (save-window-excursion
3067 (cond
3068 ;; A function is provided: Apply it.
3069 ((functionp org-latex-pdf-process)
3070 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
3071 ;; A list is provided: Replace %b, %f and %o with appropriate
3072 ;; values in each command before applying it. Output is
3073 ;; redirected to "*Org PDF LaTeX Output*" buffer.
3074 ((consp org-latex-pdf-process)
3075 (let ((outbuf (and (not snippet)
3076 (get-buffer-create "*Org PDF LaTeX Output*"))))
3077 (mapc
3078 (lambda (command)
3079 (shell-command
3080 (replace-regexp-in-string
3081 "%b" (shell-quote-argument base-name)
3082 (replace-regexp-in-string
3083 "%f" (shell-quote-argument full-name)
3084 (replace-regexp-in-string
3085 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
3086 outbuf))
3087 org-latex-pdf-process)
3088 ;; Collect standard errors from output buffer.
3089 (setq warnings (and (not snippet)
3090 (org-latex--collect-warnings outbuf)))))
3091 (t (error "No valid command to process to PDF")))
3092 (let ((pdffile (concat out-dir base-name ".pdf")))
3093 ;; Check for process failure. Provide collected errors if
3094 ;; possible.
3095 (if (or (not (file-exists-p pdffile))
3096 (time-less-p (nth 5 (file-attributes pdffile)) time))
3097 (error (format "PDF file %s wasn't produced" pdffile))
3098 ;; Else remove log files, when specified, and signal end of
3099 ;; process to user, along with any error encountered.
3100 (when (and (not snippet) org-latex-remove-logfiles)
3101 (dolist (file (directory-files
3102 out-dir t
3103 (concat (regexp-quote base-name)
3104 "\\(?:\\.[0-9]+\\)?"
3105 "\\."
3106 (regexp-opt org-latex-logfiles-extensions))))
3107 (delete-file file)))
3108 (message (concat "PDF file produced"
3109 (cond
3110 ((eq warnings 'error) " with errors.")
3111 (warnings (concat " with warnings: " warnings))
3112 (t ".")))))
3113 ;; Return output file name.
3114 pdffile))))
3116 (defun org-latex--collect-warnings (buffer)
3117 "Collect some warnings from \"pdflatex\" command output.
3118 BUFFER is the buffer containing output. Return collected
3119 warnings types as a string, `error' if a LaTeX error was
3120 encountered or nil if there was none."
3121 (with-current-buffer buffer
3122 (save-excursion
3123 (goto-char (point-max))
3124 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3125 (if (re-search-forward "^!" nil t) 'error
3126 (let ((case-fold-search t)
3127 (warnings ""))
3128 (dolist (warning org-latex-known-warnings)
3129 (save-excursion
3130 (when (save-excursion (re-search-forward (car warning) nil t))
3131 (setq warnings (concat warnings " " (cdr warning))))))
3132 (and (org-string-nw-p warnings) (org-trim warnings))))))))
3134 ;;;###autoload
3135 (defun org-latex-publish-to-latex (plist filename pub-dir)
3136 "Publish an Org file to LaTeX.
3138 FILENAME is the filename of the Org file to be published. PLIST
3139 is the property list for the given project. PUB-DIR is the
3140 publishing directory.
3142 Return output file name."
3143 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3145 ;;;###autoload
3146 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3147 "Publish an Org file to PDF (via LaTeX).
3149 FILENAME is the filename of the Org file to be published. PLIST
3150 is the property list for the given project. PUB-DIR is the
3151 publishing directory.
3153 Return output file name."
3154 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3155 ;; in working directory and then moved to publishing directory.
3156 (org-publish-attachment
3157 plist
3158 (org-latex-compile
3159 (org-publish-org-to
3160 'latex filename ".tex" plist (file-name-directory filename)))
3161 pub-dir))
3164 (provide 'ox-latex)
3166 ;; Local variables:
3167 ;; generated-autoload-file: "org-loaddefs.el"
3168 ;; End:
3170 ;;; ox-latex.el ends here