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