cycle tables for :results org and :results wrap
[org-mode.git] / lisp / ox-latex.el
blob8727adc2d54fcb7513295e49411e48745381c258
1 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;; This library implements a LaTeX back-end for Org generic exporter.
25 ;; Depending on the desired output format, three commands are provided
26 ;; for export: `org-latex-export-as-latex' (temporary buffer),
27 ;; `org-latex-export-to-latex' ("tex" file) and
28 ;; `org-latex-export-to-pdf' ("pdf" file). Also, two publishing
29 ;; functions are available: `org-latex-publish-to-latex' and
30 ;; `org-latex-publish-to-pdf'.
32 ;; The library introduces four new buffer keywords: "LATEX_CLASS",
33 ;; "LATEX_CLASS_OPTIONS", "LATEX_HEADER" and "LATEX_HEADER_EXTRA" (the
34 ;; latter will not be used to build LaTeX snippets). It also
35 ;; introduces a new OPTIONS item: "textht".
37 ;; Table export can be controlled with a number of attributes (through
38 ;; ATTR_LATEX keyword).
40 ;; - The main one is the `:mode' attribute, which can be set to
41 ;; `table', `math', `inline-math' and `verbatim'. In particular,
42 ;; when in `math' or `inline-math' mode, every cell is exported
43 ;; as-is, horizontal rules are ignored and the table will be wrapped
44 ;; in a math environment. Also, contiguous tables sharing the same
45 ;; math mode will be wrapped within the same environment. Default
46 ;; mode is stored in `org-latex-default-table-mode'.
48 ;; - The second most important attribute is `:environment'. It is the
49 ;; environment used for the table and defaults to
50 ;; `org-latex-default-table-environment' value. It can be set to
51 ;; anything, including "tabularx", "longtable", "array", "tabu",
52 ;; "bmatrix"...
54 ;; - `:float' attribute defines a float environment for the table.
55 ;; Possible values are `sidewaystable', `multicolumn' and `table'.
56 ;; If unspecified, a table with a caption will have a `table'
57 ;; environment. Moreover, `:placement' attribute can specify the
58 ;; positioning of the float
60 ;; - `:align', `:font' and `:width' attributes set, respectively, the
61 ;; alignment string of the table, its font size and its width. They
62 ;; only apply on regular tables.
64 ;; - `:spread' is a boolean attribute specific to the "tabu" and
65 ;; "longtabu" environments, and only takes effect when used in
66 ;; conjunction with the `:width' attribute. When `:spread' is
67 ;; non-nil, the table will be spread or shrunk by the value of
68 ;; `:width'.
70 ;; - `:booktabs', `:center' and `:rmlines' values are booleans. They
71 ;; toggle, respectively "booktabs" usage (assuming the package is
72 ;; properly loaded), table centering and removal of every horizontal
73 ;; rule but the first one (in a "table.el" table only).
75 ;; - `:math-prefix', `:math-suffix' and `:math-arguments' are string
76 ;; which will be inserted, respectively, before the table within the
77 ;; math environment, after the table within the math environment,
78 ;; and between the macro name and the contents of the table. The
79 ;; latter attribute is necessary to matrix macros that require more
80 ;; than one argument (i.e. "qbordermatrix").
82 ;; Plain lists accept two optional attributes: `:environment' and
83 ;; `:options'. The first one allows to use a non-standard environment
84 ;; (i.e. "inparaenum"). The second one allows to specify optional
85 ;; arguments for that environment (square brackets are not mandatory).
87 ;; Images accept `:float', `:placement', `:comment-include', `:width',
88 ;; and `:height', and `:options' as attributes. `:float' accepts
89 ;; a symbol among `wrap', `multicolumn', and `figure', which defines
90 ;; the float environment for the image (if unspecified, an image with
91 ;; a caption will be set in a "figure" environment).
92 ;; `:comment-include' is a boolean that toggles whether to comment out
93 ;; the code which actually includes the image. `:placement' is
94 ;; a string that will be used as argument for the environment chosen.
95 ;; `:width' and `:height' control the width and height of the image.
96 ;; `:options' is a string that will be used as the optional argument
97 ;; for "includegraphics" macro or, in the case of tikz images, used as
98 ;; the optional argument for a `tikzpicture' environment which will
99 ;; surround the "\input" picture code.
101 ;; Special blocks accept `:options' as attribute. Its value will be
102 ;; appended as-is to the opening string of the environment created.
104 ;; Source blocks accept `:long-listing' attribute, which prevents the
105 ;; block to be wrapped within a float when non-nil.
107 ;; This back-end also offers enhanced support for footnotes. Thus, it
108 ;; handles nested footnotes, footnotes in tables and footnotes in item
109 ;; descriptions.
111 ;; Smart quotes are activated by default.
113 ;;; Code:
115 (eval-when-compile (require 'cl))
116 (require 'ox)
117 (require 'ox-publish)
119 (defvar org-latex-default-packages-alist)
120 (defvar org-latex-packages-alist)
121 (defvar orgtbl-exp-regexp)
125 ;;; Define Back-End
127 (org-export-define-backend 'latex
128 '((bold . org-latex-bold)
129 (center-block . org-latex-center-block)
130 (clock . org-latex-clock)
131 (code . org-latex-code)
132 (comment . (lambda (&rest args) ""))
133 (comment-block . (lambda (&rest args) ""))
134 (drawer . org-latex-drawer)
135 (dynamic-block . org-latex-dynamic-block)
136 (entity . org-latex-entity)
137 (example-block . org-latex-example-block)
138 (export-block . org-latex-export-block)
139 (export-snippet . org-latex-export-snippet)
140 (fixed-width . org-latex-fixed-width)
141 (footnote-definition . org-latex-footnote-definition)
142 (footnote-reference . org-latex-footnote-reference)
143 (headline . org-latex-headline)
144 (horizontal-rule . org-latex-horizontal-rule)
145 (inline-src-block . org-latex-inline-src-block)
146 (inlinetask . org-latex-inlinetask)
147 (italic . org-latex-italic)
148 (item . org-latex-item)
149 (keyword . org-latex-keyword)
150 (latex-environment . org-latex-latex-environment)
151 (latex-fragment . org-latex-latex-fragment)
152 (line-break . org-latex-line-break)
153 (link . org-latex-link)
154 (paragraph . org-latex-paragraph)
155 (plain-list . org-latex-plain-list)
156 (plain-text . org-latex-plain-text)
157 (planning . org-latex-planning)
158 (property-drawer . (lambda (&rest args) ""))
159 (quote-block . org-latex-quote-block)
160 (quote-section . org-latex-quote-section)
161 (radio-target . org-latex-radio-target)
162 (section . org-latex-section)
163 (special-block . org-latex-special-block)
164 (src-block . org-latex-src-block)
165 (statistics-cookie . org-latex-statistics-cookie)
166 (strike-through . org-latex-strike-through)
167 (subscript . org-latex-subscript)
168 (superscript . org-latex-superscript)
169 (table . org-latex-table)
170 (table-cell . org-latex-table-cell)
171 (table-row . org-latex-table-row)
172 (target . org-latex-target)
173 (template . org-latex-template)
174 (timestamp . org-latex-timestamp)
175 (underline . org-latex-underline)
176 (verbatim . org-latex-verbatim)
177 (verse-block . org-latex-verse-block))
178 :export-block '("LATEX" "TEX")
179 :menu-entry
180 '(?l "Export to LaTeX"
181 ((?L "As LaTeX buffer" org-latex-export-as-latex)
182 (?l "As LaTeX file" org-latex-export-to-latex)
183 (?p "As PDF file" org-latex-export-to-pdf)
184 (?o "As PDF file and open"
185 (lambda (a s v b)
186 (if a (org-latex-export-to-pdf t s v b)
187 (org-open-file (org-latex-export-to-pdf nil s v b)))))))
188 :options-alist '((:date-format nil nil org-latex-date-timestamp-format)
189 (:latex-class "LATEX_CLASS" nil org-latex-default-class t)
190 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
191 (:latex-header "LATEX_HEADER" nil nil newline)
192 (:latex-header-extra "LATEX_HEADER_EXTRA" nil nil newline)
193 (:latex-hyperref-p nil "texht" org-latex-with-hyperref t)
194 ;; Redefine regular options.
195 (:date "DATE" nil "\\today" t)
196 (:with-smart-quotes nil "'" t)))
200 ;;; Internal Variables
202 (defconst org-latex-babel-language-alist
203 '(("af" . "afrikaans")
204 ("bg" . "bulgarian")
205 ("bt-br" . "brazilian")
206 ("ca" . "catalan")
207 ("cs" . "czech")
208 ("cy" . "welsh")
209 ("da" . "danish")
210 ("de" . "germanb")
211 ("de-at" . "naustrian")
212 ("de-de" . "ngerman")
213 ("el" . "greek")
214 ("en" . "english")
215 ("en-au" . "australian")
216 ("en-ca" . "canadian")
217 ("en-gb" . "british")
218 ("en-ie" . "irish")
219 ("en-nz" . "newzealand")
220 ("en-us" . "american")
221 ("es" . "spanish")
222 ("et" . "estonian")
223 ("eu" . "basque")
224 ("fi" . "finnish")
225 ("fr" . "frenchb")
226 ("fr-ca" . "canadien")
227 ("gl" . "galician")
228 ("hr" . "croatian")
229 ("hu" . "hungarian")
230 ("id" . "indonesian")
231 ("is" . "icelandic")
232 ("it" . "italian")
233 ("la" . "latin")
234 ("ms" . "malay")
235 ("nl" . "dutch")
236 ("no-no" . "nynorsk")
237 ("pl" . "polish")
238 ("pt" . "portuguese")
239 ("ro" . "romanian")
240 ("ru" . "russian")
241 ("sa" . "sanskrit")
242 ("sb" . "uppersorbian")
243 ("sk" . "slovak")
244 ("sl" . "slovene")
245 ("sq" . "albanian")
246 ("sr" . "serbian")
247 ("sv" . "swedish")
248 ("ta" . "tamil")
249 ("tr" . "turkish")
250 ("uk" . "ukrainian"))
251 "Alist between language code and corresponding Babel option.")
253 (defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
254 ("qbordermatrix" . "\\cr")
255 ("kbordermatrix" . "\\\\"))
256 "Alist between matrix macros and their row ending.")
260 ;;; User Configurable Variables
262 (defgroup org-export-latex nil
263 "Options for exporting Org mode files to LaTeX."
264 :tag "Org Export LaTeX"
265 :group 'org-export)
268 ;;;; Preamble
270 (defcustom org-latex-default-class "article"
271 "The default LaTeX class."
272 :group 'org-export-latex
273 :type '(string :tag "LaTeX class"))
275 (defcustom org-latex-classes
276 '(("article"
277 "\\documentclass[11pt]{article}"
278 ("\\section{%s}" . "\\section*{%s}")
279 ("\\subsection{%s}" . "\\subsection*{%s}")
280 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
281 ("\\paragraph{%s}" . "\\paragraph*{%s}")
282 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
283 ("report"
284 "\\documentclass[11pt]{report}"
285 ("\\part{%s}" . "\\part*{%s}")
286 ("\\chapter{%s}" . "\\chapter*{%s}")
287 ("\\section{%s}" . "\\section*{%s}")
288 ("\\subsection{%s}" . "\\subsection*{%s}")
289 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
290 ("book"
291 "\\documentclass[11pt]{book}"
292 ("\\part{%s}" . "\\part*{%s}")
293 ("\\chapter{%s}" . "\\chapter*{%s}")
294 ("\\section{%s}" . "\\section*{%s}")
295 ("\\subsection{%s}" . "\\subsection*{%s}")
296 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
297 "Alist of LaTeX classes and associated header and structure.
298 If #+LaTeX_CLASS is set in the buffer, use its value and the
299 associated information. Here is the structure of each cell:
301 \(class-name
302 header-string
303 \(numbered-section . unnumbered-section\)
304 ...\)
306 The header string
307 -----------------
309 The HEADER-STRING is the header that will be inserted into the
310 LaTeX file. It should contain the \\documentclass macro, and
311 anything else that is needed for this setup. To this header, the
312 following commands will be added:
314 - Calls to \\usepackage for all packages mentioned in the
315 variables `org-latex-default-packages-alist' and
316 `org-latex-packages-alist'. Thus, your header definitions
317 should avoid to also request these packages.
319 - Lines specified via \"#+LaTeX_HEADER:\"
321 If you need more control about the sequence in which the header
322 is built up, or if you want to exclude one of these building
323 blocks for a particular class, you can use the following
324 macro-like placeholders.
326 [DEFAULT-PACKAGES] \\usepackage statements for default packages
327 [NO-DEFAULT-PACKAGES] do not include any of the default packages
328 [PACKAGES] \\usepackage statements for packages
329 [NO-PACKAGES] do not include the packages
330 [EXTRA] the stuff from #+LaTeX_HEADER
331 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
333 So a header like
335 \\documentclass{article}
336 [NO-DEFAULT-PACKAGES]
337 [EXTRA]
338 \\providecommand{\\alert}[1]{\\textbf{#1}}
339 [PACKAGES]
341 will omit the default packages, and will include the
342 #+LaTeX_HEADER lines, then have a call to \\providecommand, and
343 then place \\usepackage commands based on the content of
344 `org-latex-packages-alist'.
346 If your header, `org-latex-default-packages-alist' or
347 `org-latex-packages-alist' inserts
348 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
349 replaced with a coding system derived from
350 `buffer-file-coding-system'. See also the variable
351 `org-latex-inputenc-alist' for a way to influence this mechanism.
353 The sectioning structure
354 ------------------------
356 The sectioning structure of the class is given by the elements
357 following the header string. For each sectioning level, a number
358 of strings is specified. A %s formatter is mandatory in each
359 section string and will be replaced by the title of the section.
361 Instead of a cons cell \(numbered . unnumbered\), you can also
362 provide a list of 2 or 4 elements,
364 \(numbered-open numbered-close\)
368 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
370 providing opening and closing strings for a LaTeX environment
371 that should represent the document section. The opening clause
372 should have a %s to represent the section title.
374 Instead of a list of sectioning commands, you can also specify
375 a function name. That function will be called with two
376 parameters, the \(reduced) level of the headline, and a predicate
377 non-nil when the headline should be numbered. It must return
378 a format string in which the section title will be added."
379 :group 'org-export-latex
380 :type '(repeat
381 (list (string :tag "LaTeX class")
382 (string :tag "LaTeX header")
383 (repeat :tag "Levels" :inline t
384 (choice
385 (cons :tag "Heading"
386 (string :tag " numbered")
387 (string :tag "unnumbered"))
388 (list :tag "Environment"
389 (string :tag "Opening (numbered)")
390 (string :tag "Closing (numbered)")
391 (string :tag "Opening (unnumbered)")
392 (string :tag "Closing (unnumbered)"))
393 (function :tag "Hook computing sectioning"))))))
395 (defcustom org-latex-inputenc-alist nil
396 "Alist of inputenc coding system names, and what should really be used.
397 For example, adding an entry
399 (\"utf8\" . \"utf8x\")
401 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
402 are written as utf8 files."
403 :group 'org-export-latex
404 :type '(repeat
405 (cons
406 (string :tag "Derived from buffer")
407 (string :tag "Use this instead"))))
409 (defcustom org-latex-date-timestamp-format nil
410 "Time-stamp format string to use for DATE keyword.
412 The format string, when specified, only applies if date consists
413 in a single time-stamp. Otherwise its value will be ignored.
415 See `format-time-string' for details on how to build this
416 string."
417 :group 'org-export-latex
418 :type '(choice
419 (string :tag "Time-stamp format string")
420 (const :tag "No format string" nil)))
422 (defcustom org-latex-title-command "\\maketitle"
423 "The command used to insert the title just after \\begin{document}.
424 If this string contains the formatting specification \"%s\" then
425 it will be used as a formatting string, passing the title as an
426 argument."
427 :group 'org-export-latex
428 :type 'string)
430 (defcustom org-latex-toc-command "\\tableofcontents\n\n"
431 "LaTeX command to set the table of contents, list of figures, etc.
432 This command only applies to the table of contents generated with
433 the toc:nil option, not to those generated with #+TOC keyword."
434 :group 'org-export-latex
435 :type 'string)
437 (defcustom org-latex-with-hyperref t
438 "Toggle insertion of \\hypersetup{...} in the preamble."
439 :group 'org-export-latex
440 :type 'boolean)
443 ;;;; Headline
445 (defcustom org-latex-format-headline-function
446 'org-latex-format-headline-default-function
447 "Function for formatting the headline's text.
449 This function will be called with 5 arguments:
450 TODO the todo keyword (string or nil).
451 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
452 PRIORITY the priority of the headline (integer or nil)
453 TEXT the main headline text (string).
454 TAGS the tags as a list of strings (list of strings or nil).
456 The function result will be used in the section format string.
458 Use `org-latex-format-headline-default-function' by default,
459 which format headlines like for Org version prior to 8.0."
460 :group 'org-export-latex
461 :version "24.4"
462 :package-version '(Org . "8.0")
463 :type 'function)
466 ;;;; Footnotes
468 (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
469 "Text used to separate footnotes."
470 :group 'org-export-latex
471 :type 'string)
474 ;;;; Timestamps
476 (defcustom org-latex-active-timestamp-format "\\textit{%s}"
477 "A printf format string to be applied to active timestamps."
478 :group 'org-export-latex
479 :type 'string)
481 (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
482 "A printf format string to be applied to inactive timestamps."
483 :group 'org-export-latex
484 :type 'string)
486 (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
487 "A printf format string to be applied to diary timestamps."
488 :group 'org-export-latex
489 :type 'string)
492 ;;;; Links
494 (defcustom org-latex-image-default-option ""
495 "Default option for images."
496 :group 'org-export-latex
497 :version "24.4"
498 :package-version '(Org . "8.0")
499 :type 'string)
501 (defcustom org-latex-image-default-width ".9\\linewidth"
502 "Default width for images.
503 This value will not be used if a height is provided."
504 :group 'org-export-latex
505 :version "24.4"
506 :package-version '(Org . "8.0")
507 :type 'string)
509 (defcustom org-latex-image-default-height ""
510 "Default height for images.
511 This value will not be used if a width is provided, or if the
512 image is wrapped within a \"figure\" or \"wrapfigure\"
513 environment."
514 :group 'org-export-latex
515 :version "24.4"
516 :package-version '(Org . "8.0")
517 :type 'string)
519 (defcustom org-latex-default-figure-position "htb"
520 "Default position for latex figures."
521 :group 'org-export-latex
522 :type 'string)
524 (defcustom org-latex-inline-image-rules
525 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\)\\'"))
526 "Rules characterizing image files that can be inlined into LaTeX.
528 A rule consists in an association whose key is the type of link
529 to consider, and value is a regexp that will be matched against
530 link's path.
532 Note that, by default, the image extension *actually* allowed
533 depend on the way the LaTeX file is processed. When used with
534 pdflatex, pdf, jpg and png images are OK. When processing
535 through dvi to Postscript, only ps and eps are allowed. The
536 default we use here encompasses both."
537 :group 'org-export-latex
538 :version "24.4"
539 :package-version '(Org . "8.0")
540 :type '(alist :key-type (string :tag "Type")
541 :value-type (regexp :tag "Path")))
543 (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
544 "Format string for links with unknown path type."
545 :group 'org-export-latex
546 :type 'string)
549 ;;;; Tables
551 (defcustom org-latex-default-table-environment "tabular"
552 "Default environment used to build tables."
553 :group 'org-export-latex
554 :version "24.4"
555 :package-version '(Org . "8.0")
556 :type 'string)
558 (defcustom org-latex-default-table-mode 'table
559 "Default mode for tables.
561 Value can be a symbol among:
563 `table' Regular LaTeX table.
565 `math' In this mode, every cell is considered as being in math
566 mode and the complete table will be wrapped within a math
567 environment. It is particularly useful to write matrices.
569 `inline-math' This mode is almost the same as `math', but the
570 math environment will be inlined.
572 `verbatim' The table is exported as it appears in the Org
573 buffer, within a verbatim environment.
575 This value can be overridden locally with, i.e. \":mode math\" in
576 LaTeX attributes.
578 When modifying this variable, it may be useful to change
579 `org-latex-default-table-environment' accordingly."
580 :group 'org-export-latex
581 :version "24.4"
582 :package-version '(Org . "8.0")
583 :type '(choice (const :tag "Table" table)
584 (const :tag "Matrix" math)
585 (const :tag "Inline matrix" inline-math)
586 (const :tag "Verbatim" verbatim)))
588 (defcustom org-latex-tables-centered t
589 "When non-nil, tables are exported in a center environment."
590 :group 'org-export-latex
591 :type 'boolean)
593 (defcustom org-latex-tables-booktabs nil
594 "When non-nil, display tables in a formal \"booktabs\" style.
595 This option assumes that the \"booktabs\" package is properly
596 loaded in the header of the document. This value can be ignored
597 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
598 attributes."
599 :group 'org-export-latex
600 :version "24.4"
601 :package-version '(Org . "8.0")
602 :type 'boolean)
604 (defcustom org-latex-table-caption-above t
605 "When non-nil, place caption string at the beginning of the table.
606 Otherwise, place it near the end."
607 :group 'org-export-latex
608 :type 'boolean)
610 (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
611 "Format string to display numbers in scientific notation.
612 The format should have \"%s\" twice, for mantissa and exponent
613 \(i.e., \"%s\\\\times10^{%s}\").
615 When nil, no transformation is made."
616 :group 'org-export-latex
617 :version "24.4"
618 :package-version '(Org . "8.0")
619 :type '(choice
620 (string :tag "Format string")
621 (const :tag "No formatting")))
624 ;;;; Text markup
626 (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
627 (code . verb)
628 (italic . "\\emph{%s}")
629 (strike-through . "\\st{%s}")
630 (underline . "\\underline{%s}")
631 (verbatim . protectedtexttt))
632 "Alist of LaTeX expressions to convert text markup.
634 The key must be a symbol among `bold', `code', `italic',
635 `strike-through', `underline' and `verbatim'. The value is
636 a formatting string to wrap fontified text with.
638 Value can also be set to the following symbols: `verb' and
639 `protectedtexttt'. For the former, Org will use \"\\verb\" to
640 create a format string and select a delimiter character that
641 isn't in the string. For the latter, Org will use \"\\texttt\"
642 to typeset and try to protect special characters.
644 If no association can be found for a given markup, text will be
645 returned as-is."
646 :group 'org-export-latex
647 :type 'alist
648 :options '(bold code italic strike-through underline verbatim))
651 ;;;; Drawers
653 (defcustom org-latex-format-drawer-function nil
654 "Function called to format a drawer in LaTeX code.
656 The function must accept two parameters:
657 NAME the drawer name, like \"LOGBOOK\"
658 CONTENTS the contents of the drawer.
660 The function should return the string to be exported.
662 For example, the variable could be set to the following function
663 in order to mimic default behaviour:
665 \(defun org-latex-format-drawer-default \(name contents\)
666 \"Format a drawer element for LaTeX export.\"
667 contents\)"
668 :group 'org-export-latex
669 :type 'function)
672 ;;;; Inlinetasks
674 (defcustom org-latex-format-inlinetask-function nil
675 "Function called to format an inlinetask in LaTeX code.
677 The function must accept six parameters:
678 TODO the todo keyword, as a string
679 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
680 PRIORITY the inlinetask priority, as a string
681 NAME the inlinetask name, as a string.
682 TAGS the inlinetask tags, as a list of strings.
683 CONTENTS the contents of the inlinetask, as a string.
685 The function should return the string to be exported.
687 For example, the variable could be set to the following function
688 in order to mimic default behaviour:
690 \(defun org-latex-format-inlinetask \(todo type priority name tags contents\)
691 \"Format an inline task element for LaTeX export.\"
692 \(let ((full-title
693 \(concat
694 \(when todo
695 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
696 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
697 title
698 \(when tags
699 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
700 \(mapconcat 'identity tags \":\")))))
701 \(format (concat \"\\\\begin{center}\\n\"
702 \"\\\\fbox{\\n\"
703 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
704 \"%s\\n\\n\"
705 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
706 \"%s\"
707 \"\\\\end{minipage}}\"
708 \"\\\\end{center}\")
709 full-title contents))"
710 :group 'org-export-latex
711 :type 'function)
714 ;; Src blocks
716 (defcustom org-latex-listings nil
717 "Non-nil means export source code using the listings package.
718 This package will fontify source code, possibly even with color.
719 If you want to use this, you also need to make LaTeX use the
720 listings package, and if you want to have color, the color
721 package. Just add these to `org-latex-packages-alist', for
722 example using customize, or with something like:
724 \(require 'ox-latex)
725 \(add-to-list 'org-latex-packages-alist '\(\"\" \"listings\"))
726 \(add-to-list 'org-latex-packages-alist '\(\"\" \"color\"))
728 Alternatively,
730 \(setq org-latex-listings 'minted)
732 causes source code to be exported using the minted package as
733 opposed to listings. If you want to use minted, you need to add
734 the minted package to `org-latex-packages-alist', for example
735 using customize, or with
737 \(require 'ox-latex)
738 \(add-to-list 'org-latex-packages-alist '\(\"\" \"minted\"))
740 In addition, it is necessary to install pygments
741 \(http://pygments.org), and to configure the variable
742 `org-latex-pdf-process' so that the -shell-escape option is
743 passed to pdflatex."
744 :group 'org-export-latex
745 :type '(choice
746 (const :tag "Use listings" t)
747 (const :tag "Use minted" 'minted)
748 (const :tag "Export verbatim" nil)))
750 (defcustom org-latex-listings-langs
751 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
752 (c "C") (cc "C++")
753 (fortran "fortran")
754 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
755 (html "HTML") (xml "XML")
756 (tex "TeX") (latex "TeX")
757 (shell-script "bash")
758 (gnuplot "Gnuplot")
759 (ocaml "Caml") (caml "Caml")
760 (sql "SQL") (sqlite "sql"))
761 "Alist mapping languages to their listing language counterpart.
762 The key is a symbol, the major mode symbol without the \"-mode\".
763 The value is the string that should be inserted as the language
764 parameter for the listings package. If the mode name and the
765 listings name are the same, the language does not need an entry
766 in this list - but it does not hurt if it is present."
767 :group 'org-export-latex
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."
790 :group 'org-export-latex
791 :type '(repeat
792 (list
793 (string :tag "Listings option name ")
794 (string :tag "Listings option value"))))
796 (defcustom org-latex-minted-langs
797 '((emacs-lisp "common-lisp")
798 (cc "c++")
799 (cperl "perl")
800 (shell-script "bash")
801 (caml "ocaml"))
802 "Alist mapping languages to their minted language counterpart.
803 The key is a symbol, the major mode symbol without the \"-mode\".
804 The value is the string that should be inserted as the language
805 parameter for the minted package. If the mode name and the
806 listings name are the same, the language does not need an entry
807 in this list - but it does not hurt if it is present.
809 Note that minted uses all lower case for language identifiers,
810 and that the full list of language identifiers can be obtained
811 with:
813 pygmentize -L lexers"
814 :group 'org-export-latex
815 :type '(repeat
816 (list
817 (symbol :tag "Major mode ")
818 (string :tag "Minted language"))))
820 (defcustom org-latex-minted-options nil
821 "Association list of options for the latex minted package.
823 These options are supplied within square brackets in
824 \\begin{minted} environments. Each element of the alist should
825 be a list containing two strings: the name of the option, and the
826 value. For example,
828 \(setq org-latex-minted-options
829 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
831 will result in src blocks being exported with
833 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
835 as the start of the minted environment. Note that the same
836 options will be applied to blocks of all languages."
837 :group 'org-export-latex
838 :type '(repeat
839 (list
840 (string :tag "Minted option name ")
841 (string :tag "Minted option value"))))
843 (defcustom org-latex-long-listings nil
844 "When non-nil no listing will be wrapped within a float.
846 Removing floats may break some functionalities. For example, it
847 will be impossible to use cross-references to listings when using
848 `minted' set-up when this variable is non-nil.
850 This value can be locally ignored with \":long-listing t\" and
851 \":long-listing nil\" LaTeX attributes."
852 :group 'org-export-latex
853 :version "24.4"
854 :package-version '(Org . "8.0")
855 :type 'boolean)
857 (defvar org-latex-custom-lang-environments nil
858 "Alist mapping languages to language-specific LaTeX environments.
860 It is used during export of src blocks by the listings and minted
861 latex packages. For example,
863 \(setq org-latex-custom-lang-environments
864 '\(\(python \"pythoncode\"\)\)\)
866 would have the effect that if org encounters begin_src python
867 during latex export it will output
869 \\begin{pythoncode}
870 <src block body>
871 \\end{pythoncode}")
874 ;;;; Compilation
876 (defcustom org-latex-pdf-process
877 '("pdflatex -interaction nonstopmode -output-directory %o %f"
878 "pdflatex -interaction nonstopmode -output-directory %o %f"
879 "pdflatex -interaction nonstopmode -output-directory %o %f")
880 "Commands to process a LaTeX file to a PDF file.
881 This is a list of strings, each of them will be given to the
882 shell as a command. %f in the command will be replaced by the
883 full file name, %b by the file base name (i.e. without directory
884 and extension parts) and %o by the base directory of the file.
886 The reason why this is a list is that it usually takes several
887 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
888 does not have a clever mechanism to detect which of these
889 commands have to be run to get to a stable result, and it also
890 does not do any error checking.
892 By default, Org uses 3 runs of `pdflatex' to do the processing.
893 If you have texi2dvi on your system and if that does not cause
894 the infamous egrep/locale bug:
896 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
898 then `texi2dvi' is the superior choice. Org does offer it as one
899 of the customize options.
901 Alternatively, this may be a Lisp function that does the
902 processing, so you could use this to apply the machinery of
903 AUCTeX or the Emacs LaTeX mode. This function should accept the
904 file name as its single argument."
905 :group 'org-export-pdf
906 :type '(choice
907 (repeat :tag "Shell command sequence"
908 (string :tag "Shell command"))
909 (const :tag "2 runs of pdflatex"
910 ("pdflatex -interaction nonstopmode -output-directory %o %f"
911 "pdflatex -interaction nonstopmode -output-directory %o %f"))
912 (const :tag "3 runs of pdflatex"
913 ("pdflatex -interaction nonstopmode -output-directory %o %f"
914 "pdflatex -interaction nonstopmode -output-directory %o %f"
915 "pdflatex -interaction nonstopmode -output-directory %o %f"))
916 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
917 ("pdflatex -interaction nonstopmode -output-directory %o %f"
918 "bibtex %b"
919 "pdflatex -interaction nonstopmode -output-directory %o %f"
920 "pdflatex -interaction nonstopmode -output-directory %o %f"))
921 (const :tag "2 runs of xelatex"
922 ("xelatex -interaction nonstopmode -output-directory %o %f"
923 "xelatex -interaction nonstopmode -output-directory %o %f"))
924 (const :tag "3 runs of xelatex"
925 ("xelatex -interaction nonstopmode -output-directory %o %f"
926 "xelatex -interaction nonstopmode -output-directory %o %f"
927 "xelatex -interaction nonstopmode -output-directory %o %f"))
928 (const :tag "xelatex,bibtex,xelatex,xelatex"
929 ("xelatex -interaction nonstopmode -output-directory %o %f"
930 "bibtex %b"
931 "xelatex -interaction nonstopmode -output-directory %o %f"
932 "xelatex -interaction nonstopmode -output-directory %o %f"))
933 (const :tag "texi2dvi"
934 ("texi2dvi -p -b -c -V %f"))
935 (const :tag "rubber"
936 ("rubber -d --into %o %f"))
937 (function)))
939 (defcustom org-latex-logfiles-extensions
940 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
941 "The list of file extensions to consider as LaTeX logfiles.
942 The logfiles will be remove if `org-latex-remove-logfiles' is
943 non-nil."
944 :group 'org-export-latex
945 :type '(repeat (string :tag "Extension")))
947 (defcustom org-latex-remove-logfiles t
948 "Non-nil means remove the logfiles produced by PDF production.
949 By default, logfiles are files with these extensions: .aux, .idx,
950 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
951 logfiles to remove, set `org-latex-logfiles-extensions'."
952 :group 'org-export-latex
953 :type 'boolean)
955 (defcustom org-latex-known-errors
956 '(("Reference.*?undefined" . "[undefined reference]")
957 ("Citation.*?undefined" . "[undefined citation]")
958 ("Undefined control sequence" . "[undefined control sequence]")
959 ("^! LaTeX.*?Error" . "[LaTeX error]")
960 ("^! Package.*?Error" . "[package error]")
961 ("Runaway argument" . "Runaway argument"))
962 "Alist of regular expressions and associated messages for the user.
963 The regular expressions are used to find possible errors in the
964 log of a latex-run."
965 :group 'org-export-latex
966 :version "24.4"
967 :package-version '(Org . "8.0")
968 :type '(repeat
969 (cons
970 (string :tag "Regexp")
971 (string :tag "Message"))))
975 ;;; Internal Functions
977 (defun org-latex--caption/label-string (element info)
978 "Return caption and label LaTeX string for ELEMENT.
980 INFO is a plist holding contextual information. If there's no
981 caption nor label, return the empty string.
983 For non-floats, see `org-latex--wrap-label'."
984 (let* ((label (org-element-property :name element))
985 (label-str (if (not (org-string-nw-p label)) ""
986 (format "\\label{%s}"
987 (org-export-solidify-link-text label))))
988 (main (org-export-get-caption element))
989 (short (org-export-get-caption element t)))
990 (cond
991 ((and (not main) (equal label-str "")) "")
992 ((not main) (concat label-str "\n"))
993 ;; Option caption format with short name.
994 (short (format "\\caption[%s]{%s%s}\n"
995 (org-export-data short info)
996 label-str
997 (org-export-data main info)))
998 ;; Standard caption format.
999 (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
1001 (defun org-latex-guess-inputenc (header)
1002 "Set the coding system in inputenc to what the buffer is.
1004 HEADER is the LaTeX header string. This function only applies
1005 when specified inputenc option is \"AUTO\".
1007 Return the new header, as a string."
1008 (let* ((cs (or (ignore-errors
1009 (latexenc-coding-system-to-inputenc
1010 (or org-export-coding-system buffer-file-coding-system)))
1011 "utf8")))
1012 (if (not cs) header
1013 ;; First translate if that is requested.
1014 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
1015 ;; Then find the \usepackage statement and replace the option.
1016 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1017 cs header t nil 1))))
1019 (defun org-latex-guess-babel-language (header info)
1020 "Set Babel's language according to LANGUAGE keyword.
1022 HEADER is the LaTeX header string. INFO is the plist used as
1023 a communication channel.
1025 Insertion of guessed language only happens when Babel package has
1026 explicitly been loaded. Then it is added to the rest of
1027 package's options.
1029 Return the new header."
1030 (let ((language-code (plist-get info :language)))
1031 ;; If no language is set or Babel package is not loaded, return
1032 ;; HEADER as-is.
1033 (if (or (not (stringp language-code))
1034 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
1035 header
1036 (let ((options (save-match-data
1037 (org-split-string (match-string 1 header) ",")))
1038 (language (cdr (assoc language-code
1039 org-latex-babel-language-alist))))
1040 ;; If LANGUAGE is already loaded, return header. Otherwise,
1041 ;; append LANGUAGE to other options.
1042 (if (member language options) header
1043 (replace-match (mapconcat 'identity
1044 (append options (list language))
1045 ",")
1046 nil nil header 1))))))
1048 (defun org-latex--find-verb-separator (s)
1049 "Return a character not used in string S.
1050 This is used to choose a separator for constructs like \\verb."
1051 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1052 (loop for c across ll
1053 when (not (string-match (regexp-quote (char-to-string c)) s))
1054 return (char-to-string c))))
1056 (defun org-latex--make-option-string (options)
1057 "Return a comma separated string of keywords and values.
1058 OPTIONS is an alist where the key is the options keyword as
1059 a string, and the value a list containing the keyword value, or
1060 nil."
1061 (mapconcat (lambda (pair)
1062 (concat (first pair)
1063 (when (> (length (second pair)) 0)
1064 (concat "=" (second pair)))))
1065 options
1066 ","))
1068 (defun org-latex--wrap-label (element output)
1069 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1070 This function shouldn't be used for floats. See
1071 `org-latex--caption/label-string'."
1072 (let ((label (org-element-property :name element)))
1073 (if (not (and (org-string-nw-p output) (org-string-nw-p label))) output
1074 (concat (format "\\label{%s}\n" (org-export-solidify-link-text label))
1075 output))))
1077 (defun org-latex--text-markup (text markup)
1078 "Format TEXT depending on MARKUP text markup.
1079 See `org-latex-text-markup-alist' for details."
1080 (let ((fmt (cdr (assq markup org-latex-text-markup-alist))))
1081 (cond
1082 ;; No format string: Return raw text.
1083 ((not fmt) text)
1084 ;; Handle the `verb' special case: Find and appropriate separator
1085 ;; and use "\\verb" command.
1086 ((eq 'verb fmt)
1087 (let ((separator (org-latex--find-verb-separator text)))
1088 (concat "\\verb" separator text separator)))
1089 ;; Handle the `protectedtexttt' special case: Protect some
1090 ;; special chars and use "\texttt{%s}" format string.
1091 ((eq 'protectedtexttt fmt)
1092 (let ((start 0)
1093 (trans '(("\\" . "\\textbackslash{}")
1094 ("~" . "\\textasciitilde{}")
1095 ("^" . "\\textasciicircum{}")))
1096 (rtn "")
1097 char)
1098 (while (string-match "[\\{}$%&_#~^]" text)
1099 (setq char (match-string 0 text))
1100 (if (> (match-beginning 0) 0)
1101 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
1102 (setq text (substring text (1+ (match-beginning 0))))
1103 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1104 rtn (concat rtn char)))
1105 (setq text (concat rtn text)
1106 fmt "\\texttt{%s}")
1107 (while (string-match "--" text)
1108 (setq text (replace-match "-{}-" t t text)))
1109 (format fmt text)))
1110 ;; Else use format string.
1111 (t (format fmt text)))))
1113 (defun org-latex--delayed-footnotes-definitions (element info)
1114 "Return footnotes definitions in ELEMENT as a string.
1116 INFO is a plist used as a communication channel.
1118 Footnotes definitions are returned within \"\\footnotetxt{}\"
1119 commands.
1121 This function is used within constructs that don't support
1122 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1123 \"\\footnotemark\" is used within the construct and the function
1124 just outside of it."
1125 (mapconcat
1126 (lambda (ref)
1127 (format
1128 "\\footnotetext[%s]{%s}"
1129 (org-export-get-footnote-number ref info)
1130 (org-trim
1131 (org-export-data
1132 (org-export-get-footnote-definition ref info) info))))
1133 ;; Find every footnote reference in ELEMENT.
1134 (let* (all-refs
1135 search-refs ; For byte-compiler.
1136 (search-refs
1137 (function
1138 (lambda (data)
1139 ;; Return a list of all footnote references never seen
1140 ;; before in DATA.
1141 (org-element-map data 'footnote-reference
1142 (lambda (ref)
1143 (when (org-export-footnote-first-reference-p ref info)
1144 (push ref all-refs)
1145 (when (eq (org-element-property :type ref) 'standard)
1146 (funcall search-refs
1147 (org-export-get-footnote-definition ref info)))))
1148 info)
1149 (reverse all-refs)))))
1150 (funcall search-refs element))
1151 ""))
1155 ;;; Template
1157 (defun org-latex-template (contents info)
1158 "Return complete document string after LaTeX conversion.
1159 CONTENTS is the transcoded contents string. INFO is a plist
1160 holding export options."
1161 (let ((title (org-export-data (plist-get info :title) info)))
1162 (concat
1163 ;; Time-stamp.
1164 (and (plist-get info :time-stamp-file)
1165 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1166 ;; Document class and packages.
1167 (let ((class (plist-get info :latex-class))
1168 (class-options (plist-get info :latex-class-options)))
1169 (org-element-normalize-string
1170 (let* ((header (nth 1 (assoc class org-latex-classes)))
1171 (document-class-string
1172 (and (stringp header)
1173 (if (not class-options) header
1174 (replace-regexp-in-string
1175 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1176 class-options header t nil 1)))))
1177 (if (not document-class-string)
1178 (user-error "Unknown LaTeX class `%s'" class)
1179 (org-latex-guess-babel-language
1180 (org-latex-guess-inputenc
1181 (org-splice-latex-header
1182 document-class-string
1183 org-latex-default-packages-alist
1184 org-latex-packages-alist nil
1185 (concat (plist-get info :latex-header)
1186 (plist-get info :latex-header-extra))))
1187 info)))))
1188 ;; Possibly limit depth for headline numbering.
1189 (let ((sec-num (plist-get info :section-numbers)))
1190 (when (integerp sec-num)
1191 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1192 ;; Author.
1193 (let ((author (and (plist-get info :with-author)
1194 (let ((auth (plist-get info :author)))
1195 (and auth (org-export-data auth info)))))
1196 (email (and (plist-get info :with-email)
1197 (org-export-data (plist-get info :email) info))))
1198 (cond ((and author email (not (string= "" email)))
1199 (format "\\author{%s\\thanks{%s}}\n" author email))
1200 ((or author email) (format "\\author{%s}\n" (or author email)))))
1201 ;; Date.
1202 (let ((date (and (plist-get info :with-date) (plist-get info :date))))
1203 (format "\\date{%s}\n"
1204 (cond ((not date) "")
1205 ;; If `:date' consists in a single timestamp and
1206 ;; `:date-format' is provided, apply it.
1207 ((and (plist-get info :date-format)
1208 (not (cdr date))
1209 (eq (org-element-type (car date)) 'timestamp))
1210 (org-timestamp-format
1211 (car date) (plist-get info :date-format)))
1212 (t (org-export-data date info)))))
1213 ;; Title
1214 (format "\\title{%s}\n" title)
1215 ;; Hyperref options.
1216 (when (plist-get info :latex-hyperref-p)
1217 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1218 (or (plist-get info :keywords) "")
1219 (or (plist-get info :description) "")
1220 (if (not (plist-get info :with-creator)) ""
1221 (plist-get info :creator))))
1222 ;; Document start.
1223 "\\begin{document}\n\n"
1224 ;; Title command.
1225 (org-element-normalize-string
1226 (cond ((string= "" title) nil)
1227 ((not (stringp org-latex-title-command)) nil)
1228 ((string-match "\\(?:[^%]\\|^\\)%s"
1229 org-latex-title-command)
1230 (format org-latex-title-command title))
1231 (t org-latex-title-command)))
1232 ;; Table of contents.
1233 (let ((depth (plist-get info :with-toc)))
1234 (when depth
1235 (concat (when (wholenump depth)
1236 (format "\\setcounter{tocdepth}{%d}\n" depth))
1237 org-latex-toc-command)))
1238 ;; Document's body.
1239 contents
1240 ;; Creator.
1241 (let ((creator-info (plist-get info :with-creator)))
1242 (cond
1243 ((not creator-info) "")
1244 ((eq creator-info 'comment)
1245 (format "%% %s\n" (plist-get info :creator)))
1246 (t (concat (plist-get info :creator) "\n"))))
1247 ;; Document end.
1248 "\\end{document}")))
1252 ;;; Transcode Functions
1254 ;;;; Bold
1256 (defun org-latex-bold (bold contents info)
1257 "Transcode BOLD from Org to LaTeX.
1258 CONTENTS is the text with bold markup. INFO is a plist holding
1259 contextual information."
1260 (org-latex--text-markup contents 'bold))
1263 ;;;; Center Block
1265 (defun org-latex-center-block (center-block contents info)
1266 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1267 CONTENTS holds the contents of the center block. INFO is a plist
1268 holding contextual information."
1269 (org-latex--wrap-label
1270 center-block
1271 (format "\\begin{center}\n%s\\end{center}" contents)))
1274 ;;;; Clock
1276 (defun org-latex-clock (clock contents info)
1277 "Transcode a CLOCK element from Org to LaTeX.
1278 CONTENTS is nil. INFO is a plist holding contextual
1279 information."
1280 (concat
1281 "\\noindent"
1282 (format "\\textbf{%s} " org-clock-string)
1283 (format org-latex-inactive-timestamp-format
1284 (concat (org-translate-time
1285 (org-element-property :raw-value
1286 (org-element-property :value clock)))
1287 (let ((time (org-element-property :duration clock)))
1288 (and time (format " (%s)" time)))))
1289 "\\\\"))
1292 ;;;; Code
1294 (defun org-latex-code (code contents info)
1295 "Transcode a CODE object from Org to LaTeX.
1296 CONTENTS is nil. INFO is a plist used as a communication
1297 channel."
1298 (org-latex--text-markup (org-element-property :value code) 'code))
1301 ;;;; Drawer
1303 (defun org-latex-drawer (drawer contents info)
1304 "Transcode a DRAWER element from Org to LaTeX.
1305 CONTENTS holds the contents of the block. INFO is a plist
1306 holding contextual information."
1307 (let* ((name (org-element-property :drawer-name drawer))
1308 (output (if (functionp org-latex-format-drawer-function)
1309 (funcall org-latex-format-drawer-function
1310 name contents)
1311 ;; If there's no user defined function: simply
1312 ;; display contents of the drawer.
1313 contents)))
1314 (org-latex--wrap-label drawer output)))
1317 ;;;; Dynamic Block
1319 (defun org-latex-dynamic-block (dynamic-block contents info)
1320 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1321 CONTENTS holds the contents of the block. INFO is a plist
1322 holding contextual information. See `org-export-data'."
1323 (org-latex--wrap-label dynamic-block contents))
1326 ;;;; Entity
1328 (defun org-latex-entity (entity contents info)
1329 "Transcode an ENTITY object from Org to LaTeX.
1330 CONTENTS are the definition itself. INFO is a plist holding
1331 contextual information."
1332 (let ((ent (org-element-property :latex entity)))
1333 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
1336 ;;;; Example Block
1338 (defun org-latex-example-block (example-block contents info)
1339 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1340 CONTENTS is nil. INFO is a plist holding contextual
1341 information."
1342 (when (org-string-nw-p (org-element-property :value example-block))
1343 (org-latex--wrap-label
1344 example-block
1345 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1346 (org-export-format-code-default example-block info)))))
1349 ;;;; Export Block
1351 (defun org-latex-export-block (export-block contents info)
1352 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1353 CONTENTS is nil. INFO is a plist holding contextual information."
1354 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1355 (org-remove-indentation (org-element-property :value export-block))))
1358 ;;;; Export Snippet
1360 (defun org-latex-export-snippet (export-snippet contents info)
1361 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1362 CONTENTS is nil. INFO is a plist holding contextual information."
1363 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1364 (org-element-property :value export-snippet)))
1367 ;;;; Fixed Width
1369 (defun org-latex-fixed-width (fixed-width contents info)
1370 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1371 CONTENTS is nil. INFO is a plist holding contextual information."
1372 (org-latex--wrap-label
1373 fixed-width
1374 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1375 (org-remove-indentation
1376 (org-element-property :value fixed-width)))))
1379 ;;;; Footnote Reference
1381 ;; Footnote reference export is handled by
1382 ;; `org-latex-footnote-reference'.
1384 ;; Internally, `org-latex--get-footnote-counter' is used to restore
1385 ;; the value of the LaTeX "footnote" counter after a jump due to
1386 ;; a reference to an already defined footnote. It is only needed in
1387 ;; item tags since the optional argument to \footnotemark is not
1388 ;; allowed there.
1390 (defun org-latex--get-footnote-counter (footnote-reference info)
1391 "Return \"footnote\" counter before FOOTNOTE-REFERENCE is encountered.
1392 INFO is a plist used as a communication channel."
1393 ;; Find original counter value by counting number of footnote
1394 ;; references appearing for the first time before the current
1395 ;; footnote reference.
1396 (let* ((label (org-element-property :label footnote-reference))
1397 seen-refs
1398 search-ref ; For byte-compiler.
1399 (search-ref
1400 (function
1401 (lambda (data)
1402 ;; Search footnote references through DATA, filling
1403 ;; SEEN-REFS along the way.
1404 (org-element-map data 'footnote-reference
1405 (lambda (fn)
1406 (let ((fn-lbl (org-element-property :label fn)))
1407 (cond
1408 ;; Anonymous footnote match: return number.
1409 ((eq fn footnote-reference) (length seen-refs))
1410 ;; Anonymous footnote: it's always a new one.
1411 ;; Also, be sure to return nil from the `cond' so
1412 ;; `first-match' doesn't get us out of the loop.
1413 ((not fn-lbl) (push 'inline seen-refs) nil)
1414 ;; Label not seen so far: add it so SEEN-REFS.
1416 ;; Also search for subsequent references in
1417 ;; footnote definition so numbering follows
1418 ;; reading logic. Note that we don't care about
1419 ;; inline definitions, since `org-element-map'
1420 ;; already traverses them at the right time.
1421 ((not (member fn-lbl seen-refs))
1422 (push fn-lbl seen-refs)
1423 (funcall search-ref
1424 (org-export-get-footnote-definition fn info))))))
1425 ;; Don't enter footnote definitions since it will
1426 ;; happen when their first reference is found.
1427 info 'first-match 'footnote-definition)))))
1428 (funcall search-ref (plist-get info :parse-tree))))
1430 (defun org-latex-footnote-reference (footnote-reference contents info)
1431 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1432 CONTENTS is nil. INFO is a plist holding contextual information."
1433 (concat
1434 ;; Insert separator between two footnotes in a row.
1435 (let ((prev (org-export-get-previous-element footnote-reference info)))
1436 (when (eq (org-element-type prev) 'footnote-reference)
1437 org-latex-footnote-separator))
1438 (cond
1439 ;; Use \footnotemark if reference is within an item's tag.
1440 ((eq (org-element-type (org-export-get-parent-element footnote-reference))
1441 'item)
1442 (if (org-export-footnote-first-reference-p footnote-reference info)
1443 "\\footnotemark"
1444 ;; Since we can't specify footnote number as an optional
1445 ;; argument within an item tag, some extra work has to be done
1446 ;; when the footnote has already been referenced. In that
1447 ;; case, set footnote counter to the desired number, use the
1448 ;; footnotemark, then set counter back to its original value.
1449 (format
1450 "\\setcounter{footnote}{%s}\\footnotemark\\setcounter{footnote}{%s}"
1451 (1- (org-export-get-footnote-number footnote-reference info))
1452 (org-latex--get-footnote-counter footnote-reference info))))
1453 ;; Use \footnotemark if the footnote has already been defined.
1454 ((not (org-export-footnote-first-reference-p footnote-reference info))
1455 (format "\\footnotemark[%s]{}"
1456 (org-export-get-footnote-number footnote-reference info)))
1457 ;; Use \footnotemark if reference is within another footnote
1458 ;; reference, footnote definition or table cell.
1459 ((loop for parent in (org-export-get-genealogy footnote-reference)
1460 thereis (memq (org-element-type parent)
1461 '(footnote-reference footnote-definition table-cell)))
1462 "\\footnotemark")
1463 ;; Otherwise, define it with \footnote command.
1465 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1466 (concat
1467 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1468 ;; Retrieve all footnote references within the footnote and
1469 ;; add their definition after it, since LaTeX doesn't support
1470 ;; them inside.
1471 (org-latex--delayed-footnotes-definitions def info)))))))
1474 ;;;; Headline
1476 (defun org-latex-headline (headline contents info)
1477 "Transcode a HEADLINE element from Org to LaTeX.
1478 CONTENTS holds the contents of the headline. INFO is a plist
1479 holding contextual information."
1480 (unless (org-element-property :footnote-section-p headline)
1481 (let* ((class (plist-get info :latex-class))
1482 (level (org-export-get-relative-level headline info))
1483 (numberedp (org-export-numbered-headline-p headline info))
1484 (class-sectionning (assoc class org-latex-classes))
1485 ;; Section formatting will set two placeholders: one for
1486 ;; the title and the other for the contents.
1487 (section-fmt
1488 (let ((sec (if (functionp (nth 2 class-sectionning))
1489 (funcall (nth 2 class-sectionning) level numberedp)
1490 (nth (1+ level) class-sectionning))))
1491 (cond
1492 ;; No section available for that LEVEL.
1493 ((not sec) nil)
1494 ;; Section format directly returned by a function. Add
1495 ;; placeholder for contents.
1496 ((stringp sec) (concat sec "\n%s"))
1497 ;; (numbered-section . unnumbered-section)
1498 ((not (consp (cdr sec)))
1499 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1500 ;; (numbered-open numbered-close)
1501 ((= (length sec) 2)
1502 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1503 ;; (num-in num-out no-num-in no-num-out)
1504 ((= (length sec) 4)
1505 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1506 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1507 (text (org-export-data (org-element-property :title headline) info))
1508 (todo
1509 (and (plist-get info :with-todo-keywords)
1510 (let ((todo (org-element-property :todo-keyword headline)))
1511 (and todo (org-export-data todo info)))))
1512 (todo-type (and todo (org-element-property :todo-type headline)))
1513 (tags (and (plist-get info :with-tags)
1514 (org-export-get-tags headline info)))
1515 (priority (and (plist-get info :with-priority)
1516 (org-element-property :priority headline)))
1517 ;; Create the headline text along with a no-tag version.
1518 ;; The latter is required to remove tags from toc.
1519 (full-text (funcall org-latex-format-headline-function
1520 todo todo-type priority text tags))
1521 ;; Associate \label to the headline for internal links.
1522 (headline-label
1523 (format "\\label{sec-%s}\n"
1524 (mapconcat 'number-to-string
1525 (org-export-get-headline-number headline info)
1526 "-")))
1527 (pre-blanks
1528 (make-string (org-element-property :pre-blank headline) 10)))
1529 (if (or (not section-fmt) (org-export-low-level-p headline info))
1530 ;; This is a deep sub-tree: export it as a list item. Also
1531 ;; export as items headlines for which no section format has
1532 ;; been found.
1533 (let ((low-level-body
1534 (concat
1535 ;; If headline is the first sibling, start a list.
1536 (when (org-export-first-sibling-p headline info)
1537 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1538 ;; Itemize headline
1539 "\\item " full-text "\n" headline-label pre-blanks contents)))
1540 ;; If headline is not the last sibling simply return
1541 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1542 ;; any blank line.
1543 (if (not (org-export-last-sibling-p headline info)) low-level-body
1544 (replace-regexp-in-string
1545 "[ \t\n]*\\'"
1546 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1547 low-level-body)))
1548 ;; This is a standard headline. Export it as a section. Add
1549 ;; an alternative heading when possible.
1550 (let ((opt-title
1551 (funcall org-latex-format-headline-function
1552 todo todo-type priority
1553 (org-export-data
1554 (org-export-get-alt-title headline info) info)
1555 (and (eq (plist-get info :with-tags) t) tags))))
1556 (if (and numberedp opt-title
1557 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
1558 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1559 ;; Replace square brackets with parenthesis
1560 ;; since square brackets are not supported in
1561 ;; optional arguments.
1562 (replace-regexp-in-string
1563 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1564 full-text
1565 (concat headline-label pre-blanks contents))
1566 ;; Impossible to add an alternative heading. Fallback to
1567 ;; regular sectioning format string.
1568 (format section-fmt full-text
1569 (concat headline-label pre-blanks contents))))))))
1571 (defun org-latex-format-headline-default-function
1572 (todo todo-type priority text tags)
1573 "Default format function for a headline.
1574 See `org-latex-format-headline-function' for details."
1575 (concat
1576 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1577 (and priority (format "\\framebox{\\#%c} " priority))
1578 text
1579 (and tags
1580 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags ":")))))
1583 ;;;; Horizontal Rule
1585 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1586 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1587 CONTENTS is nil. INFO is a plist holding contextual information."
1588 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1589 (prev (org-export-get-previous-element horizontal-rule info)))
1590 (concat
1591 ;; Make sure the rule doesn't start at the end of the current
1592 ;; line by separating it with a blank line from previous element.
1593 (when (and prev
1594 (let ((prev-blank (org-element-property :post-blank prev)))
1595 (or (not prev-blank) (zerop prev-blank))))
1596 "\n")
1597 (org-latex--wrap-label
1598 horizontal-rule
1599 (format "\\rule{%s}{%s}"
1600 (or (plist-get attr :width) "\\linewidth")
1601 (or (plist-get attr :thickness) "0.5pt"))))))
1604 ;;;; Inline Src Block
1606 (defun org-latex-inline-src-block (inline-src-block contents info)
1607 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1608 CONTENTS holds the contents of the item. INFO is a plist holding
1609 contextual information."
1610 (let* ((code (org-element-property :value inline-src-block))
1611 (separator (org-latex--find-verb-separator code)))
1612 (cond
1613 ;; Do not use a special package: transcode it verbatim.
1614 ((not org-latex-listings)
1615 (concat "\\verb" separator code separator))
1616 ;; Use minted package.
1617 ((eq org-latex-listings 'minted)
1618 (let* ((org-lang (org-element-property :language inline-src-block))
1619 (mint-lang (or (cadr (assq (intern org-lang)
1620 org-latex-minted-langs))
1621 org-lang))
1622 (options (org-latex--make-option-string
1623 org-latex-minted-options)))
1624 (concat (format "\\mint%s{%s}"
1625 (if (string= options "") "" (format "[%s]" options))
1626 mint-lang)
1627 separator code separator)))
1628 ;; Use listings package.
1630 ;; Maybe translate language's name.
1631 (let* ((org-lang (org-element-property :language inline-src-block))
1632 (lst-lang (or (cadr (assq (intern org-lang)
1633 org-latex-listings-langs))
1634 org-lang))
1635 (options (org-latex--make-option-string
1636 (append org-latex-listings-options
1637 `(("language" ,lst-lang))))))
1638 (concat (format "\\lstinline[%s]" options)
1639 separator code separator))))))
1642 ;;;; Inlinetask
1644 (defun org-latex-inlinetask (inlinetask contents info)
1645 "Transcode an INLINETASK element from Org to LaTeX.
1646 CONTENTS holds the contents of the block. INFO is a plist
1647 holding contextual information."
1648 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1649 (todo (and (plist-get info :with-todo-keywords)
1650 (let ((todo (org-element-property :todo-keyword inlinetask)))
1651 (and todo (org-export-data todo info)))))
1652 (todo-type (org-element-property :todo-type inlinetask))
1653 (tags (and (plist-get info :with-tags)
1654 (org-export-get-tags inlinetask info)))
1655 (priority (and (plist-get info :with-priority)
1656 (org-element-property :priority inlinetask))))
1657 ;; If `org-latex-format-inlinetask-function' is provided, call it
1658 ;; with appropriate arguments.
1659 (if (functionp org-latex-format-inlinetask-function)
1660 (funcall org-latex-format-inlinetask-function
1661 todo todo-type priority title tags contents)
1662 ;; Otherwise, use a default template.
1663 (org-latex--wrap-label
1664 inlinetask
1665 (let ((full-title
1666 (concat
1667 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1668 (when priority (format "\\framebox{\\#%c} " priority))
1669 title
1670 (when tags (format "\\hfill{}\\textsc{:%s:}"
1671 (mapconcat 'identity tags ":"))))))
1672 (format (concat "\\begin{center}\n"
1673 "\\fbox{\n"
1674 "\\begin{minipage}[c]{.6\\textwidth}\n"
1675 "%s\n\n"
1676 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1677 "%s"
1678 "\\end{minipage}\n"
1679 "}\n"
1680 "\\end{center}")
1681 full-title contents))))))
1684 ;;;; Italic
1686 (defun org-latex-italic (italic contents info)
1687 "Transcode ITALIC from Org to LaTeX.
1688 CONTENTS is the text with italic markup. INFO is a plist holding
1689 contextual information."
1690 (org-latex--text-markup contents 'italic))
1693 ;;;; Item
1695 (defun org-latex-item (item contents info)
1696 "Transcode an ITEM element from Org to LaTeX.
1697 CONTENTS holds the contents of the item. INFO is a plist holding
1698 contextual information."
1699 (let* ((counter
1700 (let ((count (org-element-property :counter item))
1701 (level
1702 ;; Determine level of current item to determine the
1703 ;; correct LaTeX counter to use (enumi, enumii...).
1704 (let ((parent item) (level 0))
1705 (while (memq (org-element-type
1706 (setq parent (org-export-get-parent parent)))
1707 '(plain-list item))
1708 (when (and (eq (org-element-type parent) 'plain-list)
1709 (eq (org-element-property :type parent)
1710 'ordered))
1711 (incf level)))
1712 level)))
1713 (and count
1714 (< level 5)
1715 (format "\\setcounter{enum%s}{%s}\n"
1716 (nth (1- level) '("i" "ii" "iii" "iv"))
1717 (1- count)))))
1718 (checkbox (case (org-element-property :checkbox item)
1719 (on "$\\boxtimes$ ")
1720 (off "$\\Box$ ")
1721 (trans "$\\boxminus$ ")))
1722 (tag (let ((tag (org-element-property :tag item)))
1723 ;; Check-boxes must belong to the tag.
1724 (and tag (format "[%s] "
1725 (concat checkbox
1726 (org-export-data tag info)))))))
1727 (concat counter "\\item" (or tag (concat " " checkbox))
1728 (and contents (org-trim contents))
1729 ;; If there are footnotes references in tag, be sure to
1730 ;; add their definition at the end of the item. This
1731 ;; workaround is necessary since "\footnote{}" command is
1732 ;; not supported in tags.
1733 (and tag
1734 (org-latex--delayed-footnotes-definitions
1735 (org-element-property :tag item) info)))))
1738 ;;;; Keyword
1740 (defun org-latex-keyword (keyword contents info)
1741 "Transcode a KEYWORD element from Org to LaTeX.
1742 CONTENTS is nil. INFO is a plist holding contextual information."
1743 (let ((key (org-element-property :key keyword))
1744 (value (org-element-property :value keyword)))
1745 (cond
1746 ((string= key "LATEX") value)
1747 ((string= key "INDEX") (format "\\index{%s}" value))
1748 ;; Invisible targets.
1749 ((string= key "TARGET") nil)
1750 ((string= key "TOC")
1751 (let ((value (downcase value)))
1752 (cond
1753 ((string-match "\\<headlines\\>" value)
1754 (let ((depth (or (and (string-match "[0-9]+" value)
1755 (string-to-number (match-string 0 value)))
1756 (plist-get info :with-toc))))
1757 (concat
1758 (when (wholenump depth)
1759 (format "\\setcounter{tocdepth}{%s}\n" depth))
1760 "\\tableofcontents")))
1761 ((string= "tables" value) "\\listoftables")
1762 ((string= "listings" value)
1763 (cond
1764 ((eq org-latex-listings 'minted) "\\listoflistings")
1765 (org-latex-listings "\\lstlistoflistings")
1766 ;; At the moment, src blocks with a caption are wrapped
1767 ;; into a figure environment.
1768 (t "\\listoffigures")))))))))
1771 ;;;; Latex Environment
1773 (defun org-latex-latex-environment (latex-environment contents info)
1774 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1775 CONTENTS is nil. INFO is a plist holding contextual information."
1776 (when (plist-get info :with-latex)
1777 (let ((label (org-element-property :name latex-environment))
1778 (value (org-remove-indentation
1779 (org-element-property :value latex-environment))))
1780 (if (not (org-string-nw-p label)) value
1781 ;; Environment is labelled: label must be within the environment
1782 ;; (otherwise, a reference pointing to that element will count
1783 ;; the section instead).
1784 (with-temp-buffer
1785 (insert value)
1786 (goto-char (point-min))
1787 (forward-line)
1788 (insert
1789 (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1790 (buffer-string))))))
1793 ;;;; Latex Fragment
1795 (defun org-latex-latex-fragment (latex-fragment contents info)
1796 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1797 CONTENTS is nil. INFO is a plist holding contextual information."
1798 (when (plist-get info :with-latex)
1799 (org-element-property :value latex-fragment)))
1802 ;;;; Line Break
1804 (defun org-latex-line-break (line-break contents info)
1805 "Transcode a LINE-BREAK object from Org to LaTeX.
1806 CONTENTS is nil. INFO is a plist holding contextual information."
1807 "\\\\\n")
1810 ;;;; Link
1812 (defun org-latex--inline-image (link info)
1813 "Return LaTeX code for an inline image.
1814 LINK is the link pointing to the inline image. INFO is a plist
1815 used as a communication channel."
1816 (let* ((parent (org-export-get-parent-element link))
1817 (path (let ((raw-path (org-element-property :path link)))
1818 (if (not (file-name-absolute-p raw-path)) raw-path
1819 (expand-file-name raw-path))))
1820 (filetype (file-name-extension path))
1821 (caption (org-latex--caption/label-string parent info))
1822 ;; Retrieve latex attributes from the element around.
1823 (attr (org-export-read-attribute :attr_latex parent))
1824 (float (let ((float (plist-get attr :float)))
1825 (cond ((string= float "wrap") 'wrap)
1826 ((string= float "multicolumn") 'multicolumn)
1827 ((or (string= float "figure")
1828 (org-element-property :caption parent))
1829 'figure))))
1830 (placement
1831 (let ((place (plist-get attr :placement)))
1832 (cond (place (format "%s" place))
1833 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1834 ((eq float 'figure)
1835 (format "[%s]" org-latex-default-figure-position))
1836 (t ""))))
1837 (comment-include (if (plist-get attr :comment-include) "%" ""))
1838 ;; It is possible to specify width and height in the
1839 ;; ATTR_LATEX line, and also via default variables.
1840 (width (cond ((plist-get attr :width))
1841 ((plist-get attr :height) "")
1842 ((eq float 'figure) "0.7\\textwidth")
1843 ((eq float 'wrap) "0.48\\textwidth")
1844 (t org-latex-image-default-width)))
1845 (height (cond ((plist-get attr :height))
1846 ((or (plist-get attr :width)
1847 (memq float '(figure wrap))) "")
1848 (t org-latex-image-default-height)))
1849 (options (let ((opt (or (plist-get attr :options)
1850 org-latex-image-default-option)))
1851 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
1852 (match-string 1 opt))))
1853 image-code)
1854 (if (equal filetype "tikz")
1855 ;; For tikz images:
1856 ;; - use \input to read in image file.
1857 ;; - if options are present, wrap in a tikzpicture environment.
1858 ;; - if width or height are present, use \resizebox to change
1859 ;; the image size.
1860 (progn
1861 (setq image-code (format "\\input{%s}" path))
1862 (when (org-string-nw-p options)
1863 (setq image-code
1864 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1865 options
1866 image-code)))
1867 (when (or (org-string-nw-p width) (org-string-nw-p height))
1868 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
1869 (if (org-string-nw-p width) width "!")
1870 (if (org-string-nw-p height) height "!")
1871 image-code))))
1872 ;; For other images:
1873 ;; - add width and height to options.
1874 ;; - include the image with \includegraphics.
1875 (when (org-string-nw-p width)
1876 (setq options (concat options ",width=" width)))
1877 (when (org-string-nw-p height)
1878 (setq options (concat options ",height=" height)))
1879 (setq image-code
1880 (format "\\includegraphics%s{%s}"
1881 (cond ((not (org-string-nw-p options)) "")
1882 ((= (aref options 0) ?,)
1883 (format "[%s]"(substring options 1)))
1884 (t (format "[%s]" options)))
1885 path)))
1886 ;; Return proper string, depending on FLOAT.
1887 (case float
1888 (wrap (format "\\begin{wrapfigure}%s
1889 \\centering
1890 %s%s
1891 %s\\end{wrapfigure}" placement comment-include image-code caption))
1892 (multicolumn (format "\\begin{figure*}%s
1893 \\centering
1894 %s%s
1895 %s\\end{figure*}" placement comment-include image-code caption))
1896 (figure (format "\\begin{figure}%s
1897 \\centering
1898 %s%s
1899 %s\\end{figure}" placement comment-include image-code caption))
1900 (otherwise image-code))))
1902 (defun org-latex-link (link desc info)
1903 "Transcode a LINK object from Org to LaTeX.
1905 DESC is the description part of the link, or the empty string.
1906 INFO is a plist holding contextual information. See
1907 `org-export-data'."
1908 (let* ((type (org-element-property :type link))
1909 (raw-path (org-element-property :path link))
1910 ;; Ensure DESC really exists, or set it to nil.
1911 (desc (and (not (string= desc "")) desc))
1912 (imagep (org-export-inline-image-p
1913 link org-latex-inline-image-rules))
1914 (path (cond
1915 ((member type '("http" "https" "ftp" "mailto"))
1916 (concat type ":" raw-path))
1917 ((string= type "file")
1918 (if (not (file-name-absolute-p raw-path)) raw-path
1919 (concat "file://" (expand-file-name raw-path))))
1920 (t raw-path)))
1921 protocol)
1922 (cond
1923 ;; Image file.
1924 (imagep (org-latex--inline-image link info))
1925 ;; Radio link: Transcode target's contents and use them as link's
1926 ;; description.
1927 ((string= type "radio")
1928 (let ((destination (org-export-resolve-radio-link link info)))
1929 (when destination
1930 (format "\\hyperref[%s]{%s}"
1931 (org-export-solidify-link-text path)
1932 (org-export-data (org-element-contents destination) info)))))
1933 ;; Links pointing to a headline: Find destination and build
1934 ;; appropriate referencing command.
1935 ((member type '("custom-id" "fuzzy" "id"))
1936 (let ((destination (if (string= type "fuzzy")
1937 (org-export-resolve-fuzzy-link link info)
1938 (org-export-resolve-id-link link info))))
1939 (case (org-element-type destination)
1940 ;; Id link points to an external file.
1941 (plain-text
1942 (if desc (format "\\href{%s}{%s}" destination desc)
1943 (format "\\url{%s}" destination)))
1944 ;; Fuzzy link points nowhere.
1945 ('nil
1946 (format org-latex-link-with-unknown-path-format
1947 (or desc
1948 (org-export-data
1949 (org-element-property :raw-link link) info))))
1950 ;; Fuzzy link points to an invisible target.
1951 (keyword nil)
1952 ;; LINK points to a headline. If headlines are numbered
1953 ;; and the link has no description, display headline's
1954 ;; number. Otherwise, display description or headline's
1955 ;; title.
1956 (headline
1957 (let ((label
1958 (format "sec-%s"
1959 (mapconcat
1960 'number-to-string
1961 (org-export-get-headline-number destination info)
1962 "-"))))
1963 (if (and (plist-get info :section-numbers) (not desc))
1964 (format "\\ref{%s}" label)
1965 (format "\\hyperref[%s]{%s}" label
1966 (or desc
1967 (org-export-data
1968 (org-element-property :title destination) info))))))
1969 ;; Fuzzy link points to a target. Do as above.
1970 (otherwise
1971 (let ((path (org-export-solidify-link-text path)))
1972 (if (not desc) (format "\\ref{%s}" path)
1973 (format "\\hyperref[%s]{%s}" path desc)))))))
1974 ;; Coderef: replace link with the reference name or the
1975 ;; equivalent line number.
1976 ((string= type "coderef")
1977 (format (org-export-get-coderef-format path desc)
1978 (org-export-resolve-coderef path info)))
1979 ;; Link type is handled by a special function.
1980 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1981 (funcall protocol (org-link-unescape path) desc 'latex))
1982 ;; External link with a description part.
1983 ((and path desc) (format "\\href{%s}{%s}" path desc))
1984 ;; External link without a description part.
1985 (path (format "\\url{%s}" path))
1986 ;; No path, only description. Try to do something useful.
1987 (t (format org-latex-link-with-unknown-path-format desc)))))
1990 ;;;; Paragraph
1992 (defun org-latex-paragraph (paragraph contents info)
1993 "Transcode a PARAGRAPH element from Org to LaTeX.
1994 CONTENTS is the contents of the paragraph, as a string. INFO is
1995 the plist used as a communication channel."
1996 contents)
1999 ;;;; Plain List
2001 (defun org-latex-plain-list (plain-list contents info)
2002 "Transcode a PLAIN-LIST element from Org to LaTeX.
2003 CONTENTS is the contents of the list. INFO is a plist holding
2004 contextual information."
2005 (let* ((type (org-element-property :type plain-list))
2006 (attr (org-export-read-attribute :attr_latex plain-list))
2007 (latex-type (let ((env (plist-get attr :environment)))
2008 (cond (env (format "%s" env))
2009 ((eq type 'ordered) "enumerate")
2010 ((eq type 'unordered) "itemize")
2011 ((eq type 'descriptive) "description")))))
2012 (org-latex--wrap-label
2013 plain-list
2014 (format "\\begin{%s}%s\n%s\\end{%s}"
2015 latex-type
2016 ;; Put optional arguments, if any inside square brackets
2017 ;; when necessary.
2018 (let ((options (format "%s" (or (plist-get attr :options) ""))))
2019 (cond ((equal options "") "")
2020 ((string-match "\\`\\[.*\\]\\'" options) options)
2021 (t (format "[%s]" options))))
2022 contents
2023 latex-type))))
2026 ;;;; Plain Text
2028 (defun org-latex-plain-text (text info)
2029 "Transcode a TEXT string from Org to LaTeX.
2030 TEXT is the string to transcode. INFO is a plist holding
2031 contextual information."
2032 (let ((specialp (plist-get info :with-special-strings))
2033 (output text))
2034 ;; Protect %, #, &, $, ~, ^, _, { and }.
2035 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" output)
2036 (setq output
2037 (replace-match
2038 (format "\\%s" (match-string 2 output)) nil t output 2)))
2039 ;; Protect \. If special strings are used, be careful not to
2040 ;; protect "\" in "\-" constructs.
2041 (let ((symbols (if specialp "-%$#&{}~^_\\" "%$#&{}~^_\\")))
2042 (setq output
2043 (replace-regexp-in-string
2044 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
2045 "$\\backslash$" output nil t 1)))
2046 ;; Activate smart quotes. Be sure to provide original TEXT string
2047 ;; since OUTPUT may have been modified.
2048 (when (plist-get info :with-smart-quotes)
2049 (setq output (org-export-activate-smart-quotes output :latex info text)))
2050 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
2051 (let ((case-fold-search nil)
2052 (start 0))
2053 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
2054 (setq output (replace-match
2055 (format "\\%s{}" (match-string 1 output)) nil t output)
2056 start (match-end 0))))
2057 ;; Convert special strings.
2058 (when specialp
2059 (setq output
2060 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
2061 ;; Handle break preservation if required.
2062 (when (plist-get info :preserve-breaks)
2063 (setq output (replace-regexp-in-string
2064 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
2065 ;; Return value.
2066 output))
2069 ;;;; Planning
2071 (defun org-latex-planning (planning contents info)
2072 "Transcode a PLANNING element from Org to LaTeX.
2073 CONTENTS is nil. INFO is a plist holding contextual
2074 information."
2075 (concat
2076 "\\noindent"
2077 (mapconcat
2078 'identity
2079 (delq nil
2080 (list
2081 (let ((closed (org-element-property :closed planning)))
2082 (when closed
2083 (concat
2084 (format "\\textbf{%s} " org-closed-string)
2085 (format org-latex-inactive-timestamp-format
2086 (org-translate-time
2087 (org-element-property :raw-value closed))))))
2088 (let ((deadline (org-element-property :deadline planning)))
2089 (when deadline
2090 (concat
2091 (format "\\textbf{%s} " org-deadline-string)
2092 (format org-latex-active-timestamp-format
2093 (org-translate-time
2094 (org-element-property :raw-value deadline))))))
2095 (let ((scheduled (org-element-property :scheduled planning)))
2096 (when scheduled
2097 (concat
2098 (format "\\textbf{%s} " org-scheduled-string)
2099 (format org-latex-active-timestamp-format
2100 (org-translate-time
2101 (org-element-property :raw-value scheduled))))))))
2102 " ")
2103 "\\\\"))
2106 ;;;; Quote Block
2108 (defun org-latex-quote-block (quote-block contents info)
2109 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2110 CONTENTS holds the contents of the block. INFO is a plist
2111 holding contextual information."
2112 (org-latex--wrap-label
2113 quote-block
2114 (format "\\begin{quote}\n%s\\end{quote}" contents)))
2117 ;;;; Quote Section
2119 (defun org-latex-quote-section (quote-section contents info)
2120 "Transcode a QUOTE-SECTION element from Org to LaTeX.
2121 CONTENTS is nil. INFO is a plist holding contextual information."
2122 (let ((value (org-remove-indentation
2123 (org-element-property :value quote-section))))
2124 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
2127 ;;;; Radio Target
2129 (defun org-latex-radio-target (radio-target text info)
2130 "Transcode a RADIO-TARGET object from Org to LaTeX.
2131 TEXT is the text of the target. INFO is a plist holding
2132 contextual information."
2133 (format "\\label{%s}%s"
2134 (org-export-solidify-link-text
2135 (org-element-property :value radio-target))
2136 text))
2139 ;;;; Section
2141 (defun org-latex-section (section contents info)
2142 "Transcode a SECTION element from Org to LaTeX.
2143 CONTENTS holds the contents of the section. INFO is a plist
2144 holding contextual information."
2145 contents)
2148 ;;;; Special Block
2150 (defun org-latex-special-block (special-block contents info)
2151 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2152 CONTENTS holds the contents of the block. INFO is a plist
2153 holding contextual information."
2154 (let ((type (downcase (org-element-property :type special-block)))
2155 (opt (org-export-read-attribute :attr_latex special-block :options)))
2156 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2157 ;; Insert any label or caption within the block
2158 ;; (otherwise, a reference pointing to that element will
2159 ;; count the section instead).
2160 (org-latex--caption/label-string special-block info)
2161 contents
2162 (format "\\end{%s}" type))))
2165 ;;;; Src Block
2167 (defun org-latex-src-block (src-block contents info)
2168 "Transcode a SRC-BLOCK element from Org to LaTeX.
2169 CONTENTS holds the contents of the item. INFO is a plist holding
2170 contextual information."
2171 (when (org-string-nw-p (org-element-property :value src-block))
2172 (let* ((lang (org-element-property :language src-block))
2173 (caption (org-element-property :caption src-block))
2174 (label (org-element-property :name src-block))
2175 (custom-env (and lang
2176 (cadr (assq (intern lang)
2177 org-latex-custom-lang-environments))))
2178 (num-start (case (org-element-property :number-lines src-block)
2179 (continued (org-export-get-loc src-block info))
2180 (new 0)))
2181 (retain-labels (org-element-property :retain-labels src-block))
2182 (long-listing
2183 (let ((attr (org-export-read-attribute :attr_latex src-block)))
2184 (if (plist-member attr :long-listing)
2185 (plist-get attr :long-listing)
2186 org-latex-long-listings))))
2187 (cond
2188 ;; Case 1. No source fontification.
2189 ((not org-latex-listings)
2190 (let* ((caption-str (org-latex--caption/label-string src-block info))
2191 (float-env (and (not long-listing)
2192 (or label caption)
2193 (format "\\begin{figure}[H]\n%s%%s\n\\end{figure}"
2194 caption-str))))
2195 (format
2196 (or float-env "%s")
2197 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2198 (org-export-format-code-default src-block info))))))
2199 ;; Case 2. Custom environment.
2200 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2201 custom-env
2202 (org-export-format-code-default src-block info)
2203 custom-env))
2204 ;; Case 3. Use minted package.
2205 ((eq org-latex-listings 'minted)
2206 (let ((float-env
2207 (and (not long-listing)
2208 (or label caption)
2209 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2210 (org-latex--caption/label-string src-block info))))
2211 (body
2212 (format
2213 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2214 ;; Options.
2215 (org-latex--make-option-string
2216 (if (or (not num-start)
2217 (assoc "linenos" org-latex-minted-options))
2218 org-latex-minted-options
2219 (append `(("linenos")
2220 ("firstnumber" ,(number-to-string (1+ num-start))))
2221 org-latex-minted-options)))
2222 ;; Language.
2223 (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
2224 ;; Source code.
2225 (let* ((code-info (org-export-unravel-code src-block))
2226 (max-width
2227 (apply 'max
2228 (mapcar 'length
2229 (org-split-string (car code-info)
2230 "\n")))))
2231 (org-export-format-code
2232 (car code-info)
2233 (lambda (loc num ref)
2234 (concat
2236 (when ref
2237 ;; Ensure references are flushed to the right,
2238 ;; separated with 6 spaces from the widest line
2239 ;; of code.
2240 (concat (make-string (+ (- max-width (length loc)) 6)
2241 ?\s)
2242 (format "(%s)" ref)))))
2243 nil (and retain-labels (cdr code-info)))))))
2244 ;; Return value.
2245 (if float-env (format float-env body) body)))
2246 ;; Case 4. Use listings package.
2248 (let ((lst-lang
2249 (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
2250 (caption-str
2251 (when caption
2252 (let ((main (org-export-get-caption src-block))
2253 (secondary (org-export-get-caption src-block t)))
2254 (if (not secondary)
2255 (format "{%s}" (org-export-data main info))
2256 (format "{[%s]%s}"
2257 (org-export-data secondary info)
2258 (org-export-data main info)))))))
2259 (concat
2260 ;; Options.
2261 (format "\\lstset{%s}\n"
2262 (org-latex--make-option-string
2263 (append
2264 org-latex-listings-options
2265 `(("language" ,lst-lang))
2266 (when label `(("label" ,label)))
2267 (when caption-str `(("caption" ,caption-str)))
2268 (cond ((assoc "numbers" org-latex-listings-options) nil)
2269 ((not num-start) '(("numbers" "none")))
2270 ((zerop num-start) '(("numbers" "left")))
2271 (t `(("numbers" "left")
2272 ("firstnumber"
2273 ,(number-to-string (1+ num-start)))))))))
2274 ;; Source code.
2275 (format
2276 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2277 (let* ((code-info (org-export-unravel-code src-block))
2278 (max-width
2279 (apply 'max
2280 (mapcar 'length
2281 (org-split-string (car code-info) "\n")))))
2282 (org-export-format-code
2283 (car code-info)
2284 (lambda (loc num ref)
2285 (concat
2287 (when ref
2288 ;; Ensure references are flushed to the right,
2289 ;; separated with 6 spaces from the widest line of
2290 ;; code
2291 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2292 (format "(%s)" ref)))))
2293 nil (and retain-labels (cdr code-info))))))))))))
2296 ;;;; Statistics Cookie
2298 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2299 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2300 CONTENTS is nil. INFO is a plist holding contextual information."
2301 (replace-regexp-in-string
2302 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2305 ;;;; Strike-Through
2307 (defun org-latex-strike-through (strike-through contents info)
2308 "Transcode STRIKE-THROUGH from Org to LaTeX.
2309 CONTENTS is the text with strike-through markup. INFO is a plist
2310 holding contextual information."
2311 (org-latex--text-markup contents 'strike-through))
2314 ;;;; Subscript
2316 (defun org-latex-subscript (subscript contents info)
2317 "Transcode a SUBSCRIPT object from Org to LaTeX.
2318 CONTENTS is the contents of the object. INFO is a plist holding
2319 contextual information."
2320 (if (= (length contents) 1) (format "$_%s$" contents)
2321 ;; Handle multiple objects in SUBSCRIPT by creating a subscript
2322 ;; command for each of them.
2323 (let ((prev-blanks 0))
2324 (mapconcat
2325 (lambda (obj)
2326 (case (org-element-type obj)
2327 ((entity latex-fragment)
2328 (setq prev-blanks (org-element-property :post-blank obj))
2329 (let ((data (org-trim (org-export-data obj info))))
2330 (string-match
2331 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2332 data)
2333 (format "$_{%s}$" (match-string 1 data))))
2334 (plain-text
2335 (format "$_\\mathrm{%s}$"
2336 (concat (make-string prev-blanks ? )
2337 ;; mathrm command doesn't handle spaces,
2338 ;; so we have to enforce them.
2339 (replace-regexp-in-string
2340 " " "\\\\ " (org-export-data obj info)))))
2341 (otherwise
2342 (setq prev-blanks (org-element-property :post-blank obj))
2343 (format "$_{%s}$" (org-export-data obj info)))))
2344 (org-element-contents subscript) ""))))
2347 ;;;; Superscript
2349 (defun org-latex-superscript (superscript contents info)
2350 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2351 CONTENTS is the contents of the object. INFO is a plist holding
2352 contextual information."
2353 (if (= (length contents) 1) (format "$^%s$" contents)
2354 ;; Handle multiple objects in SUPERSCRIPT by creating
2355 ;; a superscript command for each of them.
2356 (let ((prev-blanks 0))
2357 (mapconcat
2358 (lambda (obj)
2359 (case (org-element-type obj)
2360 ((entity latex-fragment)
2361 (setq prev-blanks (org-element-property :post-blank obj))
2362 (let ((data (org-trim (org-export-data obj info))))
2363 (string-match
2364 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2365 data)
2366 (format "$^{%s}$" (match-string 1 data))))
2367 (plain-text
2368 (format "$^\\mathrm{%s}$"
2369 (concat (make-string prev-blanks ? )
2370 ;; mathrm command doesn't handle spaces,
2371 ;; so we have to enforce them.
2372 (replace-regexp-in-string
2373 " " "\\\\ " (org-export-data obj info)))))
2374 (otherwise
2375 (setq prev-blanks (org-element-property :post-blank obj))
2376 (format "$^{%s}$" (org-export-data obj info)))))
2377 (org-element-contents superscript) ""))))
2380 ;;;; Table
2382 ;; `org-latex-table' is the entry point for table transcoding. It
2383 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2384 ;; delegates the job to either `org-latex--table.el-table',
2385 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2386 ;; depending of the type of the table and the mode requested.
2388 ;; `org-latex--align-string' is a subroutine used to build alignment
2389 ;; string for Org tables.
2391 (defun org-latex-table (table contents info)
2392 "Transcode a TABLE element from Org to LaTeX.
2393 CONTENTS is the contents of the table. INFO is a plist holding
2394 contextual information."
2395 (if (eq (org-element-property :type table) 'table.el)
2396 ;; "table.el" table. Convert it using appropriate tools.
2397 (org-latex--table.el-table table info)
2398 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2399 org-latex-default-table-mode)))
2400 (cond
2401 ;; Case 1: Verbatim table.
2402 ((string= type "verbatim")
2403 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2404 ;; Re-create table, without affiliated keywords.
2405 (org-trim (org-element-interpret-data
2406 `(table nil ,@(org-element-contents table))))))
2407 ;; Case 2: Matrix.
2408 ((or (string= type "math") (string= type "inline-math"))
2409 (org-latex--math-table table info))
2410 ;; Case 3: Standard table.
2411 (t (concat (org-latex--org-table table contents info)
2412 ;; When there are footnote references within the
2413 ;; table, insert their definition just after it.
2414 (org-latex--delayed-footnotes-definitions table info)))))))
2416 (defun org-latex--align-string (table info)
2417 "Return an appropriate LaTeX alignment string.
2418 TABLE is the considered table. INFO is a plist used as
2419 a communication channel."
2420 (or (org-export-read-attribute :attr_latex table :align)
2421 (let (align)
2422 ;; Extract column groups and alignment from first (non-rule)
2423 ;; row.
2424 (org-element-map
2425 (org-element-map table 'table-row
2426 (lambda (row)
2427 (and (eq (org-element-property :type row) 'standard) row))
2428 info 'first-match)
2429 'table-cell
2430 (lambda (cell)
2431 (let ((borders (org-export-table-cell-borders cell info)))
2432 ;; Check left border for the first cell only.
2433 (when (and (memq 'left borders) (not align))
2434 (push "|" align))
2435 (push (case (org-export-table-cell-alignment cell info)
2436 (left "l")
2437 (right "r")
2438 (center "c"))
2439 align)
2440 (when (memq 'right borders) (push "|" align))))
2441 info)
2442 (apply 'concat (nreverse align)))))
2444 (defun org-latex--org-table (table contents info)
2445 "Return appropriate LaTeX code for an Org table.
2447 TABLE is the table type element to transcode. CONTENTS is its
2448 contents, as a string. INFO is a plist used as a communication
2449 channel.
2451 This function assumes TABLE has `org' as its `:type' property and
2452 `table' as its `:mode' attribute."
2453 (let* ((caption (org-latex--caption/label-string table info))
2454 (attr (org-export-read-attribute :attr_latex table))
2455 ;; Determine alignment string.
2456 (alignment (org-latex--align-string table info))
2457 ;; Determine environment for the table: longtable, tabular...
2458 (table-env (or (plist-get attr :environment)
2459 org-latex-default-table-environment))
2460 ;; If table is a float, determine environment: table, table*
2461 ;; or sidewaystable.
2462 (float-env (unless (member table-env '("longtable" "longtabu"))
2463 (let ((float (plist-get attr :float)))
2464 (cond
2465 ((string= float "sidewaystable") "sidewaystable")
2466 ((string= float "multicolumn") "table*")
2467 ((or (string= float "table")
2468 (org-element-property :caption table))
2469 "table")))))
2470 ;; Extract others display options.
2471 (fontsize (let ((font (plist-get attr :font)))
2472 (and font (concat font "\n"))))
2473 (width (plist-get attr :width))
2474 (spreadp (plist-get attr :spread))
2475 (placement (or (plist-get attr :placement)
2476 (format "[%s]" org-latex-default-figure-position)))
2477 (centerp (if (plist-member attr :center) (plist-get attr :center)
2478 org-latex-tables-centered)))
2479 ;; Prepare the final format string for the table.
2480 (cond
2481 ;; Longtable.
2482 ((equal "longtable" table-env)
2483 (concat (and fontsize (concat "{" fontsize))
2484 (format "\\begin{longtable}{%s}\n" alignment)
2485 (and org-latex-table-caption-above
2486 (org-string-nw-p caption)
2487 (concat caption "\\\\\n"))
2488 contents
2489 (and (not org-latex-table-caption-above)
2490 (org-string-nw-p caption)
2491 (concat caption "\\\\\n"))
2492 "\\end{longtable}\n"
2493 (and fontsize "}")))
2494 ;; Longtabu
2495 ((equal "longtabu" table-env)
2496 (concat (and fontsize (concat "{" fontsize))
2497 (format "\\begin{longtabu}%s{%s}\n"
2498 (if width
2499 (format " %s %s "
2500 (if spreadp "spread" "to") width) "")
2501 alignment)
2502 (and org-latex-table-caption-above
2503 (org-string-nw-p caption)
2504 (concat caption "\\\\\n"))
2505 contents
2506 (and (not org-latex-table-caption-above)
2507 (org-string-nw-p caption)
2508 (concat caption "\\\\\n"))
2509 "\\end{longtabu}\n"
2510 (and fontsize "}")))
2511 ;; Others.
2512 (t (concat (cond
2513 (float-env
2514 (concat (format "\\begin{%s}%s\n" float-env placement)
2515 (if org-latex-table-caption-above caption "")
2516 (when centerp "\\centering\n")
2517 fontsize))
2518 (centerp (concat "\\begin{center}\n" fontsize))
2519 (fontsize (concat "{" fontsize)))
2520 (cond ((equal "tabu" table-env)
2521 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2522 (if width (format
2523 (if spreadp " spread %s " " to %s ")
2524 width) "")
2525 alignment
2526 contents))
2527 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2528 table-env
2529 (if width (format "{%s}" width) "")
2530 alignment
2531 contents
2532 table-env)))
2533 (cond
2534 (float-env
2535 (concat (if org-latex-table-caption-above "" caption)
2536 (format "\n\\end{%s}" float-env)))
2537 (centerp "\n\\end{center}")
2538 (fontsize "}")))))))
2540 (defun org-latex--table.el-table (table info)
2541 "Return appropriate LaTeX code for a table.el table.
2543 TABLE is the table type element to transcode. INFO is a plist
2544 used as a communication channel.
2546 This function assumes TABLE has `table.el' as its `:type'
2547 property."
2548 (require 'table)
2549 ;; Ensure "*org-export-table*" buffer is empty.
2550 (with-current-buffer (get-buffer-create "*org-export-table*")
2551 (erase-buffer))
2552 (let ((output (with-temp-buffer
2553 (insert (org-element-property :value table))
2554 (goto-char 1)
2555 (re-search-forward "^[ \t]*|[^|]" nil t)
2556 (table-generate-source 'latex "*org-export-table*")
2557 (with-current-buffer "*org-export-table*"
2558 (org-trim (buffer-string))))))
2559 (kill-buffer (get-buffer "*org-export-table*"))
2560 ;; Remove left out comments.
2561 (while (string-match "^%.*\n" output)
2562 (setq output (replace-match "" t t output)))
2563 (let ((attr (org-export-read-attribute :attr_latex table)))
2564 (when (plist-get attr :rmlines)
2565 ;; When the "rmlines" attribute is provided, remove all hlines
2566 ;; but the the one separating heading from the table body.
2567 (let ((n 0) (pos 0))
2568 (while (and (< (length output) pos)
2569 (setq pos (string-match "^\\\\hline\n?" output pos)))
2570 (incf n)
2571 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2572 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2573 org-latex-tables-centered)))
2574 (if (not centerp) output
2575 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2577 (defun org-latex--math-table (table info)
2578 "Return appropriate LaTeX code for a matrix.
2580 TABLE is the table type element to transcode. INFO is a plist
2581 used as a communication channel.
2583 This function assumes TABLE has `org' as its `:type' property and
2584 `inline-math' or `math' as its `:mode' attribute.."
2585 (let* ((caption (org-latex--caption/label-string table info))
2586 (attr (org-export-read-attribute :attr_latex table))
2587 (inlinep (eq (plist-get attr :mode) 'inline-math))
2588 (env (or (plist-get attr :environment)
2589 org-latex-default-table-environment))
2590 (contents
2591 (mapconcat
2592 (lambda (row)
2593 ;; Ignore horizontal rules.
2594 (when (eq (org-element-property :type row) 'standard)
2595 ;; Return each cell unmodified.
2596 (concat
2597 (mapconcat
2598 (lambda (cell)
2599 (substring (org-element-interpret-data cell) 0 -1))
2600 (org-element-map row 'table-cell 'identity info) "&")
2601 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2602 "\n")))
2603 (org-element-map table 'table-row 'identity info) ""))
2604 ;; Variables related to math clusters (contiguous math tables
2605 ;; of the same type).
2606 (mode (org-export-read-attribute :attr_latex table :mode))
2607 (prev (org-export-get-previous-element table info))
2608 (next (org-export-get-next-element table info))
2609 (same-mode-p
2610 (lambda (table)
2611 ;; Non-nil when TABLE has the same mode as current table.
2612 (string= (or (org-export-read-attribute :attr_latex table :mode)
2613 org-latex-default-table-mode)
2614 mode))))
2615 (concat
2616 ;; Opening string. If TABLE is in the middle of a table cluster,
2617 ;; do not insert any.
2618 (cond ((and prev
2619 (eq (org-element-type prev) 'table)
2620 (memq (org-element-property :post-blank prev) '(0 nil))
2621 (funcall same-mode-p prev))
2622 nil)
2623 (inlinep "\\(")
2624 ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
2625 (t "\\["))
2626 ;; Prefix.
2627 (or (plist-get attr :math-prefix) "")
2628 ;; Environment. Also treat special cases.
2629 (cond ((equal env "array")
2630 (let ((align (org-latex--align-string table info)))
2631 (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
2632 ((assoc env org-latex-table-matrix-macros)
2633 (format "\\%s%s{\n%s}"
2635 (or (plist-get attr :math-arguments) "")
2636 contents))
2637 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2638 ;; Suffix.
2639 (or (plist-get attr :math-suffix) "")
2640 ;; Closing string. If TABLE is in the middle of a table cluster,
2641 ;; do not insert any. If it closes such a cluster, be sure to
2642 ;; close the cluster with a string matching the opening string.
2643 (cond ((and next
2644 (eq (org-element-type next) 'table)
2645 (memq (org-element-property :post-blank table) '(0 nil))
2646 (funcall same-mode-p next))
2647 nil)
2648 (inlinep "\\)")
2649 ;; Find cluster beginning to know which environment to use.
2650 ((let ((cluster-beg table) prev)
2651 (while (and (setq prev (org-export-get-previous-element
2652 cluster-beg info))
2653 (memq (org-element-property :post-blank prev)
2654 '(0 nil))
2655 (funcall same-mode-p prev))
2656 (setq cluster-beg prev))
2657 (and (or (org-element-property :caption cluster-beg)
2658 (org-element-property :name cluster-beg))
2659 "\n\\end{equation}")))
2660 (t "\\]")))))
2663 ;;;; Table Cell
2665 (defun org-latex-table-cell (table-cell contents info)
2666 "Transcode a TABLE-CELL element from Org to LaTeX.
2667 CONTENTS is the cell contents. INFO is a plist used as
2668 a communication channel."
2669 (concat (if (and contents
2670 org-latex-table-scientific-notation
2671 (string-match orgtbl-exp-regexp contents))
2672 ;; Use appropriate format string for scientific
2673 ;; notation.
2674 (format org-latex-table-scientific-notation
2675 (match-string 1 contents)
2676 (match-string 2 contents))
2677 contents)
2678 (when (org-export-get-next-element table-cell info) " & ")))
2681 ;;;; Table Row
2683 (defun org-latex-table-row (table-row contents info)
2684 "Transcode a TABLE-ROW element from Org to LaTeX.
2685 CONTENTS is the contents of the row. INFO is a plist used as
2686 a communication channel."
2687 ;; Rules are ignored since table separators are deduced from
2688 ;; borders of the current row.
2689 (when (eq (org-element-property :type table-row) 'standard)
2690 (let* ((attr (org-export-read-attribute :attr_latex
2691 (org-export-get-parent table-row)))
2692 (longtablep (member (or (plist-get attr :environment)
2693 org-latex-default-table-environment)
2694 '("longtable" "longtabu")))
2695 (booktabsp (if (plist-member attr :booktabs)
2696 (plist-get attr :booktabs)
2697 org-latex-tables-booktabs))
2698 ;; TABLE-ROW's borders are extracted from its first cell.
2699 (borders (org-export-table-cell-borders
2700 (car (org-element-contents table-row)) info)))
2701 (concat
2702 ;; When BOOKTABS are activated enforce top-rule even when no
2703 ;; hline was specifically marked.
2704 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2705 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2706 contents "\\\\\n"
2707 (cond
2708 ;; Special case for long tables. Define header and footers.
2709 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2710 (format "%s
2711 \\endhead
2712 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2713 \\endfoot
2714 \\endlastfoot"
2715 (if booktabsp "\\midrule" "\\hline")
2716 (if booktabsp "\\midrule" "\\hline")
2717 ;; Number of columns.
2718 (cdr (org-export-table-dimensions
2719 (org-export-get-parent-table table-row) info))))
2720 ;; When BOOKTABS are activated enforce bottom rule even when
2721 ;; no hline was specifically marked.
2722 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2723 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2724 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2727 ;;;; Target
2729 (defun org-latex-target (target contents info)
2730 "Transcode a TARGET object from Org to LaTeX.
2731 CONTENTS is nil. INFO is a plist holding contextual
2732 information."
2733 (format "\\label{%s}"
2734 (org-export-solidify-link-text (org-element-property :value target))))
2737 ;;;; Timestamp
2739 (defun org-latex-timestamp (timestamp contents info)
2740 "Transcode a TIMESTAMP object from Org to LaTeX.
2741 CONTENTS is nil. INFO is a plist holding contextual
2742 information."
2743 (let ((value (org-latex-plain-text
2744 (org-timestamp-translate timestamp) info)))
2745 (case (org-element-property :type timestamp)
2746 ((active active-range) (format org-latex-active-timestamp-format value))
2747 ((inactive inactive-range)
2748 (format org-latex-inactive-timestamp-format value))
2749 (otherwise (format org-latex-diary-timestamp-format value)))))
2752 ;;;; Underline
2754 (defun org-latex-underline (underline contents info)
2755 "Transcode UNDERLINE from Org to LaTeX.
2756 CONTENTS is the text with underline markup. INFO is a plist
2757 holding contextual information."
2758 (org-latex--text-markup contents 'underline))
2761 ;;;; Verbatim
2763 (defun org-latex-verbatim (verbatim contents info)
2764 "Transcode a VERBATIM object from Org to LaTeX.
2765 CONTENTS is nil. INFO is a plist used as a communication
2766 channel."
2767 (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2770 ;;;; Verse Block
2772 (defun org-latex-verse-block (verse-block contents info)
2773 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2774 CONTENTS is verse block contents. INFO is a plist holding
2775 contextual information."
2776 (org-latex--wrap-label
2777 verse-block
2778 ;; In a verse environment, add a line break to each newline
2779 ;; character and change each white space at beginning of a line
2780 ;; into a space of 1 em. Also change each blank line with
2781 ;; a vertical space of 1 em.
2782 (progn
2783 (setq contents (replace-regexp-in-string
2784 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2785 (replace-regexp-in-string
2786 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2787 (while (string-match "^[ \t]+" contents)
2788 (let ((new-str (format "\\hspace*{%dem}"
2789 (length (match-string 0 contents)))))
2790 (setq contents (replace-match new-str nil t contents))))
2791 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2795 ;;; End-user functions
2797 ;;;###autoload
2798 (defun org-latex-export-as-latex
2799 (&optional async subtreep visible-only body-only ext-plist)
2800 "Export current buffer as a LaTeX buffer.
2802 If narrowing is active in the current buffer, only export its
2803 narrowed part.
2805 If a region is active, export that region.
2807 A non-nil optional argument ASYNC means the process should happen
2808 asynchronously. The resulting buffer should be accessible
2809 through the `org-export-stack' interface.
2811 When optional argument SUBTREEP is non-nil, export the sub-tree
2812 at point, extracting information from the headline properties
2813 first.
2815 When optional argument VISIBLE-ONLY is non-nil, don't export
2816 contents of hidden elements.
2818 When optional argument BODY-ONLY is non-nil, only write code
2819 between \"\\begin{document}\" and \"\\end{document}\".
2821 EXT-PLIST, when provided, is a property list with external
2822 parameters overriding Org default settings, but still inferior to
2823 file-local settings.
2825 Export is done in a buffer named \"*Org LATEX Export*\", which
2826 will be displayed when `org-export-show-temporary-export-buffer'
2827 is non-nil."
2828 (interactive)
2829 (if async
2830 (org-export-async-start
2831 (lambda (output)
2832 (with-current-buffer (get-buffer-create "*Org LATEX Export*")
2833 (erase-buffer)
2834 (insert output)
2835 (goto-char (point-min))
2836 (LaTeX-mode)
2837 (org-export-add-to-stack (current-buffer) 'latex)))
2838 `(org-export-as 'latex ,subtreep ,visible-only ,body-only
2839 ',ext-plist))
2840 (let ((outbuf
2841 (org-export-to-buffer 'latex "*Org LATEX Export*"
2842 subtreep visible-only body-only ext-plist)))
2843 (with-current-buffer outbuf (LaTeX-mode))
2844 (when org-export-show-temporary-export-buffer
2845 (switch-to-buffer-other-window outbuf)))))
2847 ;;;###autoload
2848 (defun org-latex-export-to-latex
2849 (&optional async subtreep visible-only body-only ext-plist)
2850 "Export current buffer to a LaTeX file.
2852 If narrowing is active in the current buffer, only export its
2853 narrowed part.
2855 If a region is active, export that region.
2857 A non-nil optional argument ASYNC means the process should happen
2858 asynchronously. The resulting file should be accessible through
2859 the `org-export-stack' interface.
2861 When optional argument SUBTREEP is non-nil, export the sub-tree
2862 at point, extracting information from the headline properties
2863 first.
2865 When optional argument VISIBLE-ONLY is non-nil, don't export
2866 contents of hidden elements.
2868 When optional argument BODY-ONLY is non-nil, only write code
2869 between \"\\begin{document}\" and \"\\end{document}\".
2871 EXT-PLIST, when provided, is a property list with external
2872 parameters overriding Org default settings, but still inferior to
2873 file-local settings.
2875 Return output file's name."
2876 (interactive)
2877 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2878 (if async
2879 (org-export-async-start
2880 (lambda (f) (org-export-add-to-stack f 'latex))
2881 `(expand-file-name
2882 (org-export-to-file
2883 'latex ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))
2884 (org-export-to-file
2885 'latex outfile subtreep visible-only body-only ext-plist))))
2887 ;;;###autoload
2888 (defun org-latex-export-to-pdf
2889 (&optional async subtreep visible-only body-only ext-plist)
2890 "Export current buffer to LaTeX then process through to PDF.
2892 If narrowing is active in the current buffer, only export its
2893 narrowed part.
2895 If a region is active, export that region.
2897 A non-nil optional argument ASYNC means the process should happen
2898 asynchronously. The resulting file should be accessible through
2899 the `org-export-stack' interface.
2901 When optional argument SUBTREEP is non-nil, export the sub-tree
2902 at point, extracting information from the headline properties
2903 first.
2905 When optional argument VISIBLE-ONLY is non-nil, don't export
2906 contents of hidden elements.
2908 When optional argument BODY-ONLY is non-nil, only write code
2909 between \"\\begin{document}\" and \"\\end{document}\".
2911 EXT-PLIST, when provided, is a property list with external
2912 parameters overriding Org default settings, but still inferior to
2913 file-local settings.
2915 Return PDF file's name."
2916 (interactive)
2917 (if async
2918 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2919 (org-export-async-start
2920 (lambda (f) (org-export-add-to-stack f 'latex))
2921 `(expand-file-name
2922 (org-latex-compile
2923 (org-export-to-file
2924 'latex ,outfile ,subtreep ,visible-only ,body-only
2925 ',ext-plist)))))
2926 (org-latex-compile
2927 (org-latex-export-to-latex
2928 nil subtreep visible-only body-only ext-plist))))
2930 (defun org-latex-compile (texfile &optional snippet)
2931 "Compile a TeX file.
2933 TEXFILE is the name of the file being compiled. Processing is
2934 done through the command specified in `org-latex-pdf-process'.
2936 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
2937 file used to preview a LaTeX snippet. In this case, do not
2938 create a log buffer and do not bother removing log files.
2940 Return PDF file name or an error if it couldn't be produced."
2941 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
2942 (full-name (file-truename texfile))
2943 (out-dir (file-name-directory texfile))
2944 ;; Make sure `default-directory' is set to TEXFILE directory,
2945 ;; not to whatever value the current buffer may have.
2946 (default-directory (file-name-directory full-name))
2947 errors)
2948 (unless snippet (message (format "Processing LaTeX file %s ..." texfile)))
2949 (save-window-excursion
2950 (cond
2951 ;; A function is provided: Apply it.
2952 ((functionp org-latex-pdf-process)
2953 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
2954 ;; A list is provided: Replace %b, %f and %o with appropriate
2955 ;; values in each command before applying it. Output is
2956 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2957 ((consp org-latex-pdf-process)
2958 (let ((outbuf (and (not snippet)
2959 (get-buffer-create "*Org PDF LaTeX Output*"))))
2960 (mapc
2961 (lambda (command)
2962 (shell-command
2963 (replace-regexp-in-string
2964 "%b" (shell-quote-argument base-name)
2965 (replace-regexp-in-string
2966 "%f" (shell-quote-argument full-name)
2967 (replace-regexp-in-string
2968 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2969 outbuf))
2970 org-latex-pdf-process)
2971 ;; Collect standard errors from output buffer.
2972 (setq errors (and (not snippet) (org-latex--collect-errors outbuf)))))
2973 (t (error "No valid command to process to PDF")))
2974 (let ((pdffile (concat out-dir base-name ".pdf")))
2975 ;; Check for process failure. Provide collected errors if
2976 ;; possible.
2977 (if (not (file-exists-p pdffile))
2978 (error (concat (format "PDF file %s wasn't produced" pdffile)
2979 (when errors (concat ": " errors))))
2980 ;; Else remove log files, when specified, and signal end of
2981 ;; process to user, along with any error encountered.
2982 (when (and (not snippet) org-latex-remove-logfiles)
2983 (dolist (ext org-latex-logfiles-extensions)
2984 (let ((file (concat out-dir base-name "." ext)))
2985 (when (file-exists-p file) (delete-file file)))))
2986 (message (concat "Process completed"
2987 (if (not errors) "."
2988 (concat " with errors: " errors)))))
2989 ;; Return output file name.
2990 pdffile))))
2992 (defun org-latex--collect-errors (buffer)
2993 "Collect some kind of errors from \"pdflatex\" command output.
2995 BUFFER is the buffer containing output.
2997 Return collected error types as a string, or nil if there was
2998 none."
2999 (with-current-buffer buffer
3000 (save-excursion
3001 (goto-char (point-max))
3002 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
3003 (let ((case-fold-search t)
3004 (errors ""))
3005 (dolist (latex-error org-latex-known-errors)
3006 (when (save-excursion (re-search-forward (car latex-error) nil t))
3007 (setq errors (concat errors " " (cdr latex-error)))))
3008 (and (org-string-nw-p errors) (org-trim errors)))))))
3010 ;;;###autoload
3011 (defun org-latex-publish-to-latex (plist filename pub-dir)
3012 "Publish an Org file to LaTeX.
3014 FILENAME is the filename of the Org file to be published. PLIST
3015 is the property list for the given project. PUB-DIR is the
3016 publishing directory.
3018 Return output file name."
3019 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3021 ;;;###autoload
3022 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3023 "Publish an Org file to PDF (via LaTeX).
3025 FILENAME is the filename of the Org file to be published. PLIST
3026 is the property list for the given project. PUB-DIR is the
3027 publishing directory.
3029 Return output file name."
3030 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3031 ;; in working directory and then moved to publishing directory.
3032 (org-publish-attachment
3033 plist
3034 (org-latex-compile (org-publish-org-to 'latex filename ".tex" plist))
3035 pub-dir))
3038 (provide 'ox-latex)
3040 ;; Local variables:
3041 ;; generated-autoload-file: "org-loaddefs.el"
3042 ;; End:
3044 ;;; ox-latex.el ends here