org-agenda.el: Re-align leaders to the left
[org-mode/org-tableheadings.git] / lisp / ox-latex.el
blob715212b4ed63c8e7ce541c015f66351a9586d2d3
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 (setq image-code
1857 (format "\\includegraphics%s{%s}"
1858 (cond ((not (org-string-nw-p options)) "")
1859 ((= (aref options 0) ?,)
1860 (format "[%s]"(substring options 1)))
1861 (t (format "[%s]" options)))
1862 path)))
1863 ;; Return proper string, depending on FLOAT.
1864 (case float
1865 (wrap (format "\\begin{wrapfigure}%s
1866 \\centering
1867 %s%s
1868 %s\\end{wrapfigure}" placement comment-include image-code caption))
1869 (multicolumn (format "\\begin{figure*}%s
1870 \\centering
1871 %s%s
1872 %s\\end{figure*}" placement comment-include image-code caption))
1873 (figure (format "\\begin{figure}%s
1874 \\centering
1875 %s%s
1876 %s\\end{figure}" placement comment-include image-code caption))
1877 (otherwise image-code))))
1879 (defun org-latex-link (link desc info)
1880 "Transcode a LINK object from Org to LaTeX.
1882 DESC is the description part of the link, or the empty string.
1883 INFO is a plist holding contextual information. See
1884 `org-export-data'."
1885 (let* ((type (org-element-property :type link))
1886 (raw-path (org-element-property :path link))
1887 ;; Ensure DESC really exists, or set it to nil.
1888 (desc (and (not (string= desc "")) desc))
1889 (imagep (org-export-inline-image-p
1890 link org-latex-inline-image-rules))
1891 (path (cond
1892 ((member type '("http" "https" "ftp" "mailto"))
1893 (concat type ":" raw-path))
1894 ((string= type "file")
1895 (if (file-name-absolute-p raw-path)
1896 (concat "file://" (expand-file-name raw-path))
1897 (concat "file://" raw-path)))
1898 (t raw-path)))
1899 protocol)
1900 (cond
1901 ;; Image file.
1902 (imagep (org-latex--inline-image link info))
1903 ;; Radio link: Transcode target's contents and use them as link's
1904 ;; description.
1905 ((string= type "radio")
1906 (let ((destination (org-export-resolve-radio-link link info)))
1907 (when destination
1908 (format "\\hyperref[%s]{%s}"
1909 (org-export-solidify-link-text path)
1910 (org-export-data (org-element-contents destination) info)))))
1911 ;; Links pointing to a headline: Find destination and build
1912 ;; appropriate referencing command.
1913 ((member type '("custom-id" "fuzzy" "id"))
1914 (let ((destination (if (string= type "fuzzy")
1915 (org-export-resolve-fuzzy-link link info)
1916 (org-export-resolve-id-link link info))))
1917 (case (org-element-type destination)
1918 ;; Id link points to an external file.
1919 (plain-text
1920 (if desc (format "\\href{file://%s}{%s}" destination desc)
1921 (format "\\url{file://%s}" destination)))
1922 ;; Fuzzy link points nowhere.
1923 ('nil
1924 (format org-latex-link-with-unknown-path-format
1925 (or desc
1926 (org-export-data
1927 (org-element-property :raw-link link) info))))
1928 ;; Fuzzy link points to an invisible target.
1929 (keyword nil)
1930 ;; LINK points to a headline. If headlines are numbered
1931 ;; and the link has no description, display headline's
1932 ;; number. Otherwise, display description or headline's
1933 ;; title.
1934 (headline
1935 (let ((label
1936 (format "sec-%s"
1937 (mapconcat
1938 'number-to-string
1939 (org-export-get-headline-number destination info)
1940 "-"))))
1941 (if (and (plist-get info :section-numbers) (not desc))
1942 (format "\\ref{%s}" label)
1943 (format "\\hyperref[%s]{%s}" label
1944 (or desc
1945 (org-export-data
1946 (org-element-property :title destination) info))))))
1947 ;; Fuzzy link points to a target. Do as above.
1948 (otherwise
1949 (let ((path (org-export-solidify-link-text path)))
1950 (if (not desc) (format "\\ref{%s}" path)
1951 (format "\\hyperref[%s]{%s}" path desc)))))))
1952 ;; Coderef: replace link with the reference name or the
1953 ;; equivalent line number.
1954 ((string= type "coderef")
1955 (format (org-export-get-coderef-format path desc)
1956 (org-export-resolve-coderef path info)))
1957 ;; Link type is handled by a special function.
1958 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1959 (funcall protocol (org-link-unescape path) desc 'latex))
1960 ;; External link with a description part.
1961 ((and path desc) (format "\\href{%s}{%s}" path desc))
1962 ;; External link without a description part.
1963 (path (format "\\url{%s}" path))
1964 ;; No path, only description. Try to do something useful.
1965 (t (format org-latex-link-with-unknown-path-format desc)))))
1968 ;;;; Paragraph
1970 (defun org-latex-paragraph (paragraph contents info)
1971 "Transcode a PARAGRAPH element from Org to LaTeX.
1972 CONTENTS is the contents of the paragraph, as a string. INFO is
1973 the plist used as a communication channel."
1974 contents)
1977 ;;;; Plain List
1979 (defun org-latex-plain-list (plain-list contents info)
1980 "Transcode a PLAIN-LIST element from Org to LaTeX.
1981 CONTENTS is the contents of the list. INFO is a plist holding
1982 contextual information."
1983 (let* ((type (org-element-property :type plain-list))
1984 (attr (org-export-read-attribute :attr_latex plain-list))
1985 (latex-type (let ((env (plist-get attr :environment)))
1986 (cond (env (format "%s" env))
1987 ((eq type 'ordered) "enumerate")
1988 ((eq type 'unordered) "itemize")
1989 ((eq type 'descriptive) "description")))))
1990 (org-latex--wrap-label
1991 plain-list
1992 (format "\\begin{%s}%s\n%s\\end{%s}"
1993 latex-type
1994 ;; Put optional arguments, if any inside square brackets
1995 ;; when necessary.
1996 (let ((options (format "%s" (or (plist-get attr :options) ""))))
1997 (cond ((equal options "") "")
1998 ((string-match "\\`\\[.*\\]\\'" options) options)
1999 (t (format "[%s]" options))))
2000 contents
2001 latex-type))))
2004 ;;;; Plain Text
2006 (defun org-latex-plain-text (text info)
2007 "Transcode a TEXT string from Org to LaTeX.
2008 TEXT is the string to transcode. INFO is a plist holding
2009 contextual information."
2010 (let ((specialp (plist-get info :with-special-strings))
2011 (output text))
2012 ;; Protect %, #, &, $, ~, ^, _, { and }.
2013 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" output)
2014 (setq output
2015 (replace-match
2016 (format "\\%s" (match-string 2 output)) nil t output 2)))
2017 ;; Protect \. If special strings are used, be careful not to
2018 ;; protect "\" in "\-" constructs.
2019 (let ((symbols (if specialp "-%$#&{}~^_\\" "%$#&{}~^_\\")))
2020 (setq output
2021 (replace-regexp-in-string
2022 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
2023 "$\\backslash$" output nil t 1)))
2024 ;; Activate smart quotes. Be sure to provide original TEXT string
2025 ;; since OUTPUT may have been modified.
2026 (when (plist-get info :with-smart-quotes)
2027 (setq output (org-export-activate-smart-quotes output :latex info text)))
2028 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
2029 (let ((case-fold-search nil)
2030 (start 0))
2031 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
2032 (setq output (replace-match
2033 (format "\\%s{}" (match-string 1 output)) nil t output)
2034 start (match-end 0))))
2035 ;; Convert special strings.
2036 (when specialp
2037 (setq output
2038 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
2039 ;; Handle break preservation if required.
2040 (when (plist-get info :preserve-breaks)
2041 (setq output (replace-regexp-in-string
2042 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
2043 ;; Return value.
2044 output))
2047 ;;;; Planning
2049 (defun org-latex-planning (planning contents info)
2050 "Transcode a PLANNING element from Org to LaTeX.
2051 CONTENTS is nil. INFO is a plist holding contextual
2052 information."
2053 (concat
2054 "\\noindent"
2055 (mapconcat
2056 'identity
2057 (delq nil
2058 (list
2059 (let ((closed (org-element-property :closed planning)))
2060 (when closed
2061 (concat
2062 (format "\\textbf{%s} " org-closed-string)
2063 (format org-latex-inactive-timestamp-format
2064 (org-translate-time
2065 (org-element-property :raw-value closed))))))
2066 (let ((deadline (org-element-property :deadline planning)))
2067 (when deadline
2068 (concat
2069 (format "\\textbf{%s} " org-deadline-string)
2070 (format org-latex-active-timestamp-format
2071 (org-translate-time
2072 (org-element-property :raw-value deadline))))))
2073 (let ((scheduled (org-element-property :scheduled planning)))
2074 (when scheduled
2075 (concat
2076 (format "\\textbf{%s} " org-scheduled-string)
2077 (format org-latex-active-timestamp-format
2078 (org-translate-time
2079 (org-element-property :raw-value scheduled))))))))
2080 " ")
2081 "\\\\"))
2084 ;;;; Quote Block
2086 (defun org-latex-quote-block (quote-block contents info)
2087 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2088 CONTENTS holds the contents of the block. INFO is a plist
2089 holding contextual information."
2090 (org-latex--wrap-label
2091 quote-block
2092 (format "\\begin{quote}\n%s\\end{quote}" contents)))
2095 ;;;; Quote Section
2097 (defun org-latex-quote-section (quote-section contents info)
2098 "Transcode a QUOTE-SECTION element from Org to LaTeX.
2099 CONTENTS is nil. INFO is a plist holding contextual information."
2100 (let ((value (org-remove-indentation
2101 (org-element-property :value quote-section))))
2102 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
2105 ;;;; Radio Target
2107 (defun org-latex-radio-target (radio-target text info)
2108 "Transcode a RADIO-TARGET object from Org to LaTeX.
2109 TEXT is the text of the target. INFO is a plist holding
2110 contextual information."
2111 (format "\\label{%s}%s"
2112 (org-export-solidify-link-text
2113 (org-element-property :value radio-target))
2114 text))
2117 ;;;; Section
2119 (defun org-latex-section (section contents info)
2120 "Transcode a SECTION element from Org to LaTeX.
2121 CONTENTS holds the contents of the section. INFO is a plist
2122 holding contextual information."
2123 contents)
2126 ;;;; Special Block
2128 (defun org-latex-special-block (special-block contents info)
2129 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2130 CONTENTS holds the contents of the block. INFO is a plist
2131 holding contextual information."
2132 (let ((type (downcase (org-element-property :type special-block)))
2133 (opt (org-export-read-attribute :attr_latex special-block :options)))
2134 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2135 ;; Insert any label or caption within the block
2136 ;; (otherwise, a reference pointing to that element will
2137 ;; count the section instead).
2138 (org-latex--caption/label-string special-block info)
2139 contents
2140 (format "\\end{%s}" type))))
2143 ;;;; Src Block
2145 (defun org-latex-src-block (src-block contents info)
2146 "Transcode a SRC-BLOCK element from Org to LaTeX.
2147 CONTENTS holds the contents of the item. INFO is a plist holding
2148 contextual information."
2149 (when (org-string-nw-p (org-element-property :value src-block))
2150 (let* ((lang (org-element-property :language src-block))
2151 (caption (org-element-property :caption src-block))
2152 (label (org-element-property :name src-block))
2153 (custom-env (and lang
2154 (cadr (assq (intern lang)
2155 org-latex-custom-lang-environments))))
2156 (num-start (case (org-element-property :number-lines src-block)
2157 (continued (org-export-get-loc src-block info))
2158 (new 0)))
2159 (retain-labels (org-element-property :retain-labels src-block)))
2160 (cond
2161 ;; Case 1. No source fontification.
2162 ((not org-latex-listings)
2163 (let ((caption-str (org-latex--caption/label-string src-block info))
2164 (float-env (and caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
2165 (format
2166 (or float-env "%s")
2167 (concat caption-str
2168 (format "\\begin{verbatim}\n%s\\end{verbatim}"
2169 (org-export-format-code-default src-block info))))))
2170 ;; Case 2. Custom environment.
2171 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2172 custom-env
2173 (org-export-format-code-default src-block info)
2174 custom-env))
2175 ;; Case 3. Use minted package.
2176 ((eq org-latex-listings 'minted)
2177 (let ((float-env
2178 (when (or label caption)
2179 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2180 (org-latex--caption/label-string src-block info))))
2181 (body
2182 (format
2183 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2184 ;; Options.
2185 (org-latex--make-option-string
2186 (if (or (not num-start)
2187 (assoc "linenos" org-latex-minted-options))
2188 org-latex-minted-options
2189 (append `(("linenos")
2190 ("firstnumber" ,(number-to-string (1+ num-start))))
2191 org-latex-minted-options)))
2192 ;; Language.
2193 (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
2194 ;; Source code.
2195 (let* ((code-info (org-export-unravel-code src-block))
2196 (max-width
2197 (apply 'max
2198 (mapcar 'length
2199 (org-split-string (car code-info)
2200 "\n")))))
2201 (org-export-format-code
2202 (car code-info)
2203 (lambda (loc num ref)
2204 (concat
2206 (when ref
2207 ;; Ensure references are flushed to the right,
2208 ;; separated with 6 spaces from the widest line
2209 ;; of code.
2210 (concat (make-string (+ (- max-width (length loc)) 6)
2211 ?\s)
2212 (format "(%s)" ref)))))
2213 nil (and retain-labels (cdr code-info)))))))
2214 ;; Return value.
2215 (if float-env (format float-env body) body)))
2216 ;; Case 4. Use listings package.
2218 (let ((lst-lang
2219 (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
2220 (caption-str
2221 (when caption
2222 (let ((main (org-export-get-caption src-block))
2223 (secondary (org-export-get-caption src-block t)))
2224 (if (not secondary)
2225 (format "{%s}" (org-export-data main info))
2226 (format "{[%s]%s}"
2227 (org-export-data secondary info)
2228 (org-export-data main info)))))))
2229 (concat
2230 ;; Options.
2231 (format "\\lstset{%s}\n"
2232 (org-latex--make-option-string
2233 (append
2234 org-latex-listings-options
2235 `(("language" ,lst-lang))
2236 (when label `(("label" ,label)))
2237 (when caption-str `(("caption" ,caption-str)))
2238 (cond ((assoc "numbers" org-latex-listings-options) nil)
2239 ((not num-start) '(("numbers" "none")))
2240 ((zerop num-start) '(("numbers" "left")))
2241 (t `(("numbers" "left")
2242 ("firstnumber"
2243 ,(number-to-string (1+ num-start)))))))))
2244 ;; Source code.
2245 (format
2246 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2247 (let* ((code-info (org-export-unravel-code src-block))
2248 (max-width
2249 (apply 'max
2250 (mapcar 'length
2251 (org-split-string (car code-info) "\n")))))
2252 (org-export-format-code
2253 (car code-info)
2254 (lambda (loc num ref)
2255 (concat
2257 (when ref
2258 ;; Ensure references are flushed to the right,
2259 ;; separated with 6 spaces from the widest line of
2260 ;; code
2261 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2262 (format "(%s)" ref)))))
2263 nil (and retain-labels (cdr code-info))))))))))))
2266 ;;;; Statistics Cookie
2268 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2269 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2270 CONTENTS is nil. INFO is a plist holding contextual information."
2271 (replace-regexp-in-string
2272 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2275 ;;;; Strike-Through
2277 (defun org-latex-strike-through (strike-through contents info)
2278 "Transcode STRIKE-THROUGH from Org to LaTeX.
2279 CONTENTS is the text with strike-through markup. INFO is a plist
2280 holding contextual information."
2281 (org-latex--text-markup contents 'strike-through))
2284 ;;;; Subscript
2286 (defun org-latex-subscript (subscript contents info)
2287 "Transcode a SUBSCRIPT object from Org to LaTeX.
2288 CONTENTS is the contents of the object. INFO is a plist holding
2289 contextual information."
2290 (if (= (length contents) 1) (format "$_%s$" contents)
2291 ;; Handle multiple objects in SUBSCRIPT by creating a subscript
2292 ;; command for each of them.
2293 (let ((prev-blanks 0))
2294 (mapconcat
2295 (lambda (obj)
2296 (case (org-element-type obj)
2297 ((entity latex-fragment)
2298 (setq prev-blanks (org-element-property :post-blank obj))
2299 (let ((data (org-trim (org-export-data obj info))))
2300 (string-match
2301 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2302 data)
2303 (format "$_{%s}$" (match-string 1 data))))
2304 (plain-text
2305 (format "$_\\mathrm{%s}$"
2306 (concat (make-string prev-blanks ? )
2307 ;; mathrm command doesn't handle spaces,
2308 ;; so we have to enforce them.
2309 (replace-regexp-in-string
2310 " " "\\\\ " (org-export-data obj info)))))
2311 (otherwise
2312 (setq prev-blanks (org-element-property :post-blank obj))
2313 (format "$_{%s}$" (org-export-data obj info)))))
2314 (org-element-contents subscript) ""))))
2317 ;;;; Superscript
2319 (defun org-latex-superscript (superscript contents info)
2320 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2321 CONTENTS is the contents of the object. INFO is a plist holding
2322 contextual information."
2323 (if (= (length contents) 1) (format "$^%s$" contents)
2324 ;; Handle multiple objects in SUPERSCRIPT by creating
2325 ;; a superscript command for each of them.
2326 (let ((prev-blanks 0))
2327 (mapconcat
2328 (lambda (obj)
2329 (case (org-element-type obj)
2330 ((entity latex-fragment)
2331 (setq prev-blanks (org-element-property :post-blank obj))
2332 (let ((data (org-trim (org-export-data obj info))))
2333 (string-match
2334 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2335 data)
2336 (format "$^{%s}$" (match-string 1 data))))
2337 (plain-text
2338 (format "$^\\mathrm{%s}$"
2339 (concat (make-string prev-blanks ? )
2340 ;; mathrm command doesn't handle spaces,
2341 ;; so we have to enforce them.
2342 (replace-regexp-in-string
2343 " " "\\\\ " (org-export-data obj info)))))
2344 (otherwise
2345 (setq prev-blanks (org-element-property :post-blank obj))
2346 (format "$^{%s}$" (org-export-data obj info)))))
2347 (org-element-contents superscript) ""))))
2350 ;;;; Table
2352 ;; `org-latex-table' is the entry point for table transcoding. It
2353 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2354 ;; delegates the job to either `org-latex--table.el-table',
2355 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2356 ;; depending of the type of the table and the mode requested.
2358 ;; `org-latex--align-string' is a subroutine used to build alignment
2359 ;; string for Org tables.
2361 (defun org-latex-table (table contents info)
2362 "Transcode a TABLE element from Org to LaTeX.
2363 CONTENTS is the contents of the table. INFO is a plist holding
2364 contextual information."
2365 (if (eq (org-element-property :type table) 'table.el)
2366 ;; "table.el" table. Convert it using appropriate tools.
2367 (org-latex--table.el-table table info)
2368 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2369 org-latex-default-table-mode)))
2370 (cond
2371 ;; Case 1: Verbatim table.
2372 ((string= type "verbatim")
2373 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2374 ;; Re-create table, without affiliated keywords.
2375 (org-trim (org-element-interpret-data
2376 `(table nil ,@(org-element-contents table))))))
2377 ;; Case 2: Matrix.
2378 ((or (string= type "math") (string= type "inline-math"))
2379 (org-latex--math-table table info))
2380 ;; Case 3: Standard table.
2381 (t (concat (org-latex--org-table table contents info)
2382 ;; When there are footnote references within the
2383 ;; table, insert their definition just after it.
2384 (org-latex--delayed-footnotes-definitions table info)))))))
2386 (defun org-latex--align-string (table info)
2387 "Return an appropriate LaTeX alignment string.
2388 TABLE is the considered table. INFO is a plist used as
2389 a communication channel."
2390 (or (org-export-read-attribute :attr_latex table :align)
2391 (let (align)
2392 ;; Extract column groups and alignment from first (non-rule)
2393 ;; row.
2394 (org-element-map
2395 (org-element-map table 'table-row
2396 (lambda (row)
2397 (and (eq (org-element-property :type row) 'standard) row))
2398 info 'first-match)
2399 'table-cell
2400 (lambda (cell)
2401 (let ((borders (org-export-table-cell-borders cell info)))
2402 ;; Check left border for the first cell only.
2403 (when (and (memq 'left borders) (not align))
2404 (push "|" align))
2405 (push (case (org-export-table-cell-alignment cell info)
2406 (left "l")
2407 (right "r")
2408 (center "c"))
2409 align)
2410 (when (memq 'right borders) (push "|" align))))
2411 info)
2412 (apply 'concat (nreverse align)))))
2414 (defun org-latex--org-table (table contents info)
2415 "Return appropriate LaTeX code for an Org table.
2417 TABLE is the table type element to transcode. CONTENTS is its
2418 contents, as a string. INFO is a plist used as a communication
2419 channel.
2421 This function assumes TABLE has `org' as its `:type' property and
2422 `table' as its `:mode' attribute."
2423 (let* ((caption (org-latex--caption/label-string table info))
2424 (attr (org-export-read-attribute :attr_latex table))
2425 ;; Determine alignment string.
2426 (alignment (org-latex--align-string table info))
2427 ;; Determine environment for the table: longtable, tabular...
2428 (table-env (or (plist-get attr :environment)
2429 org-latex-default-table-environment))
2430 ;; If table is a float, determine environment: table, table*
2431 ;; or sidewaystable.
2432 (float-env (unless (equal "longtable" table-env)
2433 (let ((float (plist-get attr :float)))
2434 (cond
2435 ((string= float "sidewaystable") "sidewaystable")
2436 ((string= float "multicolumn") "table*")
2437 ((or (string= float "table")
2438 (org-element-property :caption table))
2439 "table")))))
2440 ;; Extract others display options.
2441 (fontsize (let ((font (plist-get attr :font)))
2442 (and font (concat font "\n"))))
2443 (width (plist-get attr :width))
2444 (placement (or (plist-get attr :placement)
2445 (format "[%s]" org-latex-default-figure-position)))
2446 (centerp (if (plist-member attr :center) (plist-get attr :center)
2447 org-latex-tables-centered)))
2448 ;; Prepare the final format string for the table.
2449 (cond
2450 ;; Longtable.
2451 ((equal "longtable" table-env)
2452 (concat (and fontsize (concat "{" fontsize))
2453 (format "\\begin{longtable}{%s}\n" alignment)
2454 (and org-latex-table-caption-above
2455 (org-string-nw-p caption)
2456 (concat caption "\\\\\n"))
2457 contents
2458 (and (not org-latex-table-caption-above)
2459 (org-string-nw-p caption)
2460 (concat caption "\\\\\n"))
2461 "\\end{longtable}\n"
2462 (and fontsize "}")))
2463 ;; Others.
2464 (t (concat (cond
2465 (float-env
2466 (concat (format "\\begin{%s}%s\n" float-env placement)
2467 (if org-latex-table-caption-above caption "")
2468 (when centerp "\\centering\n")
2469 fontsize))
2470 (centerp (concat "\\begin{center}\n" fontsize))
2471 (fontsize (concat "{" fontsize)))
2472 (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2473 table-env
2474 (if width (format "{%s}" width) "")
2475 alignment
2476 contents
2477 table-env)
2478 (cond
2479 (float-env
2480 (concat (if org-latex-table-caption-above "" caption)
2481 (format "\n\\end{%s}" float-env)))
2482 (centerp "\n\\end{center}")
2483 (fontsize "}")))))))
2485 (defun org-latex--table.el-table (table info)
2486 "Return appropriate LaTeX code for a table.el table.
2488 TABLE is the table type element to transcode. INFO is a plist
2489 used as a communication channel.
2491 This function assumes TABLE has `table.el' as its `:type'
2492 property."
2493 (require 'table)
2494 ;; Ensure "*org-export-table*" buffer is empty.
2495 (with-current-buffer (get-buffer-create "*org-export-table*")
2496 (erase-buffer))
2497 (let ((output (with-temp-buffer
2498 (insert (org-element-property :value table))
2499 (goto-char 1)
2500 (re-search-forward "^[ \t]*|[^|]" nil t)
2501 (table-generate-source 'latex "*org-export-table*")
2502 (with-current-buffer "*org-export-table*"
2503 (org-trim (buffer-string))))))
2504 (kill-buffer (get-buffer "*org-export-table*"))
2505 ;; Remove left out comments.
2506 (while (string-match "^%.*\n" output)
2507 (setq output (replace-match "" t t output)))
2508 (let ((attr (org-export-read-attribute :attr_latex table)))
2509 (when (plist-get attr :rmlines)
2510 ;; When the "rmlines" attribute is provided, remove all hlines
2511 ;; but the the one separating heading from the table body.
2512 (let ((n 0) (pos 0))
2513 (while (and (< (length output) pos)
2514 (setq pos (string-match "^\\\\hline\n?" output pos)))
2515 (incf n)
2516 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2517 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2518 org-latex-tables-centered)))
2519 (if (not centerp) output
2520 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2522 (defun org-latex--math-table (table info)
2523 "Return appropriate LaTeX code for a matrix.
2525 TABLE is the table type element to transcode. INFO is a plist
2526 used as a communication channel.
2528 This function assumes TABLE has `org' as its `:type' property and
2529 `inline-math' or `math' as its `:mode' attribute.."
2530 (let* ((caption (org-latex--caption/label-string table info))
2531 (attr (org-export-read-attribute :attr_latex table))
2532 (inlinep (eq (plist-get attr :mode) 'inline-math))
2533 (env (or (plist-get attr :environment)
2534 org-latex-default-table-environment))
2535 (contents
2536 (mapconcat
2537 (lambda (row)
2538 ;; Ignore horizontal rules.
2539 (when (eq (org-element-property :type row) 'standard)
2540 ;; Return each cell unmodified.
2541 (concat
2542 (mapconcat
2543 (lambda (cell)
2544 (substring (org-element-interpret-data cell) 0 -1))
2545 (org-element-map row 'table-cell 'identity info) "&")
2546 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2547 "\n")))
2548 (org-element-map table 'table-row 'identity info) ""))
2549 ;; Variables related to math clusters (contiguous math tables
2550 ;; of the same type).
2551 (mode (org-export-read-attribute :attr_latex table :mode))
2552 (prev (org-export-get-previous-element table info))
2553 (next (org-export-get-next-element table info))
2554 (same-mode-p
2555 (lambda (table)
2556 ;; Non-nil when TABLE has the same mode as current table.
2557 (string= (or (org-export-read-attribute :attr_latex table :mode)
2558 org-latex-default-table-mode)
2559 mode))))
2560 (concat
2561 ;; Opening string. If TABLE is in the middle of a table cluster,
2562 ;; do not insert any.
2563 (cond ((and prev
2564 (eq (org-element-type prev) 'table)
2565 (memq (org-element-property :post-blank prev) '(0 nil))
2566 (funcall same-mode-p prev))
2567 nil)
2568 (inlinep "\\(")
2569 ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
2570 (t "\\["))
2571 ;; Prefix.
2572 (or (plist-get attr :math-prefix) "")
2573 ;; Environment. Also treat special cases.
2574 (cond ((equal env "array")
2575 (let ((align (org-latex--align-string table info)))
2576 (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
2577 ((assoc env org-latex-table-matrix-macros)
2578 (format "\\%s%s{\n%s}"
2580 (or (plist-get attr :math-arguments) "")
2581 contents))
2582 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2583 ;; Suffix.
2584 (or (plist-get attr :math-suffix) "")
2585 ;; Closing string. If TABLE is in the middle of a table cluster,
2586 ;; do not insert any. If it closes such a cluster, be sure to
2587 ;; close the cluster with a string matching the opening string.
2588 (cond ((and next
2589 (eq (org-element-type next) 'table)
2590 (memq (org-element-property :post-blank table) '(0 nil))
2591 (funcall same-mode-p next))
2592 nil)
2593 (inlinep "\\)")
2594 ;; Find cluster beginning to know which environment to use.
2595 ((let ((cluster-beg table) prev)
2596 (while (and (setq prev (org-export-get-previous-element
2597 cluster-beg info))
2598 (memq (org-element-property :post-blank prev)
2599 '(0 nil))
2600 (funcall same-mode-p prev))
2601 (setq cluster-beg prev))
2602 (and (or (org-element-property :caption cluster-beg)
2603 (org-element-property :name cluster-beg))
2604 "\n\\end{equation}")))
2605 (t "\\]")))))
2608 ;;;; Table Cell
2610 (defun org-latex-table-cell (table-cell contents info)
2611 "Transcode a TABLE-CELL element from Org to LaTeX.
2612 CONTENTS is the cell contents. INFO is a plist used as
2613 a communication channel."
2614 (concat (if (and contents
2615 org-latex-table-scientific-notation
2616 (string-match orgtbl-exp-regexp contents))
2617 ;; Use appropriate format string for scientific
2618 ;; notation.
2619 (format org-latex-table-scientific-notation
2620 (match-string 1 contents)
2621 (match-string 2 contents))
2622 contents)
2623 (when (org-export-get-next-element table-cell info) " & ")))
2626 ;;;; Table Row
2628 (defun org-latex-table-row (table-row contents info)
2629 "Transcode a TABLE-ROW element from Org to LaTeX.
2630 CONTENTS is the contents of the row. INFO is a plist used as
2631 a communication channel."
2632 ;; Rules are ignored since table separators are deduced from
2633 ;; borders of the current row.
2634 (when (eq (org-element-property :type table-row) 'standard)
2635 (let* ((attr (org-export-read-attribute :attr_latex
2636 (org-export-get-parent table-row)))
2637 (longtablep (string= (or (plist-get attr :environment)
2638 org-latex-default-table-environment)
2639 "longtable"))
2640 (booktabsp (if (plist-member attr :booktabs)
2641 (plist-get attr :booktabs)
2642 org-latex-tables-booktabs))
2643 ;; TABLE-ROW's borders are extracted from its first cell.
2644 (borders (org-export-table-cell-borders
2645 (car (org-element-contents table-row)) info)))
2646 (concat
2647 ;; When BOOKTABS are activated enforce top-rule even when no
2648 ;; hline was specifically marked.
2649 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2650 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2651 contents "\\\\\n"
2652 (cond
2653 ;; Special case for long tables. Define header and footers.
2654 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2655 (format "%s
2656 \\endhead
2657 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2658 \\endfoot
2659 \\endlastfoot"
2660 (if booktabsp "\\midrule" "\\hline")
2661 (if booktabsp "\\midrule" "\\hline")
2662 ;; Number of columns.
2663 (cdr (org-export-table-dimensions
2664 (org-export-get-parent-table table-row) info))))
2665 ;; When BOOKTABS are activated enforce bottom rule even when
2666 ;; no hline was specifically marked.
2667 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2668 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2669 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2672 ;;;; Target
2674 (defun org-latex-target (target contents info)
2675 "Transcode a TARGET object from Org to LaTeX.
2676 CONTENTS is nil. INFO is a plist holding contextual
2677 information."
2678 (format "\\label{%s}"
2679 (org-export-solidify-link-text (org-element-property :value target))))
2682 ;;;; Timestamp
2684 (defun org-latex-timestamp (timestamp contents info)
2685 "Transcode a TIMESTAMP object from Org to LaTeX.
2686 CONTENTS is nil. INFO is a plist holding contextual
2687 information."
2688 (let ((value (org-latex-plain-text
2689 (org-timestamp-translate timestamp) info)))
2690 (case (org-element-property :type timestamp)
2691 ((active active-range) (format org-latex-active-timestamp-format value))
2692 ((inactive inactive-range)
2693 (format org-latex-inactive-timestamp-format value))
2694 (otherwise (format org-latex-diary-timestamp-format value)))))
2697 ;;;; Underline
2699 (defun org-latex-underline (underline contents info)
2700 "Transcode UNDERLINE from Org to LaTeX.
2701 CONTENTS is the text with underline markup. INFO is a plist
2702 holding contextual information."
2703 (org-latex--text-markup contents 'underline))
2706 ;;;; Verbatim
2708 (defun org-latex-verbatim (verbatim contents info)
2709 "Transcode a VERBATIM object from Org to LaTeX.
2710 CONTENTS is nil. INFO is a plist used as a communication
2711 channel."
2712 (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2715 ;;;; Verse Block
2717 (defun org-latex-verse-block (verse-block contents info)
2718 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2719 CONTENTS is verse block contents. INFO is a plist holding
2720 contextual information."
2721 (org-latex--wrap-label
2722 verse-block
2723 ;; In a verse environment, add a line break to each newline
2724 ;; character and change each white space at beginning of a line
2725 ;; into a space of 1 em. Also change each blank line with
2726 ;; a vertical space of 1 em.
2727 (progn
2728 (setq contents (replace-regexp-in-string
2729 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2730 (replace-regexp-in-string
2731 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2732 (while (string-match "^[ \t]+" contents)
2733 (let ((new-str (format "\\hspace*{%dem}"
2734 (length (match-string 0 contents)))))
2735 (setq contents (replace-match new-str nil t contents))))
2736 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2740 ;;; End-user functions
2742 ;;;###autoload
2743 (defun org-latex-export-as-latex
2744 (&optional async subtreep visible-only body-only ext-plist)
2745 "Export current buffer as a LaTeX buffer.
2747 If narrowing is active in the current buffer, only export its
2748 narrowed part.
2750 If a region is active, export that region.
2752 A non-nil optional argument ASYNC means the process should happen
2753 asynchronously. The resulting buffer should be accessible
2754 through the `org-export-stack' interface.
2756 When optional argument SUBTREEP is non-nil, export the sub-tree
2757 at point, extracting information from the headline properties
2758 first.
2760 When optional argument VISIBLE-ONLY is non-nil, don't export
2761 contents of hidden elements.
2763 When optional argument BODY-ONLY is non-nil, only write code
2764 between \"\\begin{document}\" and \"\\end{document}\".
2766 EXT-PLIST, when provided, is a property list with external
2767 parameters overriding Org default settings, but still inferior to
2768 file-local settings.
2770 Export is done in a buffer named \"*Org LATEX Export*\", which
2771 will be displayed when `org-export-show-temporary-export-buffer'
2772 is non-nil."
2773 (interactive)
2774 (if async
2775 (org-export-async-start
2776 (lambda (output)
2777 (with-current-buffer (get-buffer-create "*Org LATEX Export*")
2778 (erase-buffer)
2779 (insert output)
2780 (goto-char (point-min))
2781 (LaTeX-mode)
2782 (org-export-add-to-stack (current-buffer) 'latex)))
2783 `(org-export-as 'latex ,subtreep ,visible-only ,body-only
2784 ',ext-plist))
2785 (let ((outbuf
2786 (org-export-to-buffer 'latex "*Org LATEX Export*"
2787 subtreep visible-only body-only ext-plist)))
2788 (with-current-buffer outbuf (LaTeX-mode))
2789 (when org-export-show-temporary-export-buffer
2790 (switch-to-buffer-other-window outbuf)))))
2792 ;;;###autoload
2793 (defun org-latex-export-to-latex
2794 (&optional async subtreep visible-only body-only ext-plist)
2795 "Export current buffer to a LaTeX file.
2797 If narrowing is active in the current buffer, only export its
2798 narrowed part.
2800 If a region is active, export that region.
2802 A non-nil optional argument ASYNC means the process should happen
2803 asynchronously. The resulting file should be accessible through
2804 the `org-export-stack' interface.
2806 When optional argument SUBTREEP is non-nil, export the sub-tree
2807 at point, extracting information from the headline properties
2808 first.
2810 When optional argument VISIBLE-ONLY is non-nil, don't export
2811 contents of hidden elements.
2813 When optional argument BODY-ONLY is non-nil, only write code
2814 between \"\\begin{document}\" and \"\\end{document}\".
2816 EXT-PLIST, when provided, is a property list with external
2817 parameters overriding Org default settings, but still inferior to
2818 file-local settings.
2820 Return output file's name."
2821 (interactive)
2822 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2823 (if async
2824 (org-export-async-start
2825 (lambda (f) (org-export-add-to-stack f 'latex))
2826 `(expand-file-name
2827 (org-export-to-file
2828 'latex ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))
2829 (org-export-to-file
2830 'latex outfile subtreep visible-only body-only ext-plist))))
2832 ;;;###autoload
2833 (defun org-latex-export-to-pdf
2834 (&optional async subtreep visible-only body-only ext-plist)
2835 "Export current buffer to LaTeX then process through to PDF.
2837 If narrowing is active in the current buffer, only export its
2838 narrowed part.
2840 If a region is active, export that region.
2842 A non-nil optional argument ASYNC means the process should happen
2843 asynchronously. The resulting file should be accessible through
2844 the `org-export-stack' interface.
2846 When optional argument SUBTREEP is non-nil, export the sub-tree
2847 at point, extracting information from the headline properties
2848 first.
2850 When optional argument VISIBLE-ONLY is non-nil, don't export
2851 contents of hidden elements.
2853 When optional argument BODY-ONLY is non-nil, only write code
2854 between \"\\begin{document}\" and \"\\end{document}\".
2856 EXT-PLIST, when provided, is a property list with external
2857 parameters overriding Org default settings, but still inferior to
2858 file-local settings.
2860 Return PDF file's name."
2861 (interactive)
2862 (if async
2863 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2864 (org-export-async-start
2865 (lambda (f) (org-export-add-to-stack f 'latex))
2866 `(expand-file-name
2867 (org-latex-compile
2868 (org-export-to-file
2869 'latex ,outfile ,subtreep ,visible-only ,body-only
2870 ',ext-plist)))))
2871 (org-latex-compile
2872 (org-latex-export-to-latex
2873 nil subtreep visible-only body-only ext-plist))))
2875 (defun org-latex-compile (texfile &optional snippet)
2876 "Compile a TeX file.
2878 TEXFILE is the name of the file being compiled. Processing is
2879 done through the command specified in `org-latex-pdf-process'.
2881 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
2882 file used to preview a LaTeX snippet. In this case, do not
2883 create a log buffer and do not bother removing log files.
2885 Return PDF file name or an error if it couldn't be produced."
2886 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
2887 (full-name (file-truename texfile))
2888 (out-dir (file-name-directory texfile))
2889 ;; Make sure `default-directory' is set to TEXFILE directory,
2890 ;; not to whatever value the current buffer may have.
2891 (default-directory (file-name-directory full-name))
2892 errors)
2893 (unless snippet (message (format "Processing LaTeX file %s ..." texfile)))
2894 (save-window-excursion
2895 (cond
2896 ;; A function is provided: Apply it.
2897 ((functionp org-latex-pdf-process)
2898 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
2899 ;; A list is provided: Replace %b, %f and %o with appropriate
2900 ;; values in each command before applying it. Output is
2901 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2902 ((consp org-latex-pdf-process)
2903 (let ((outbuf (and (not snippet)
2904 (get-buffer-create "*Org PDF LaTeX Output*"))))
2905 (mapc
2906 (lambda (command)
2907 (shell-command
2908 (replace-regexp-in-string
2909 "%b" (shell-quote-argument base-name)
2910 (replace-regexp-in-string
2911 "%f" (shell-quote-argument full-name)
2912 (replace-regexp-in-string
2913 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2914 outbuf))
2915 org-latex-pdf-process)
2916 ;; Collect standard errors from output buffer.
2917 (setq errors (and (not snippet) (org-latex--collect-errors outbuf)))))
2918 (t (error "No valid command to process to PDF")))
2919 (let ((pdffile (concat out-dir base-name ".pdf")))
2920 ;; Check for process failure. Provide collected errors if
2921 ;; possible.
2922 (if (not (file-exists-p pdffile))
2923 (error (concat (format "PDF file %s wasn't produced" pdffile)
2924 (when errors (concat ": " errors))))
2925 ;; Else remove log files, when specified, and signal end of
2926 ;; process to user, along with any error encountered.
2927 (when (and (not snippet) org-latex-remove-logfiles)
2928 (dolist (ext org-latex-logfiles-extensions)
2929 (let ((file (concat out-dir base-name "." ext)))
2930 (when (file-exists-p file) (delete-file file)))))
2931 (message (concat "Process completed"
2932 (if (not errors) "."
2933 (concat " with errors: " errors)))))
2934 ;; Return output file name.
2935 pdffile))))
2937 (defun org-latex--collect-errors (buffer)
2938 "Collect some kind of errors from \"pdflatex\" command output.
2940 BUFFER is the buffer containing output.
2942 Return collected error types as a string, or nil if there was
2943 none."
2944 (with-current-buffer buffer
2945 (save-excursion
2946 (goto-char (point-max))
2947 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
2948 (let ((case-fold-search t)
2949 (errors ""))
2950 (dolist (latex-error org-latex-known-errors)
2951 (when (save-excursion (re-search-forward (car latex-error) nil t))
2952 (setq errors (concat errors " " (cdr latex-error)))))
2953 (and (org-string-nw-p errors) (org-trim errors)))))))
2955 ;;;###autoload
2956 (defun org-latex-publish-to-latex (plist filename pub-dir)
2957 "Publish an Org file to LaTeX.
2959 FILENAME is the filename of the Org file to be published. PLIST
2960 is the property list for the given project. PUB-DIR is the
2961 publishing directory.
2963 Return output file name."
2964 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
2966 ;;;###autoload
2967 (defun org-latex-publish-to-pdf (plist filename pub-dir)
2968 "Publish an Org file to PDF (via LaTeX).
2970 FILENAME is the filename of the Org file to be published. PLIST
2971 is the property list for the given project. PUB-DIR is the
2972 publishing directory.
2974 Return output file name."
2975 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
2976 ;; in working directory and then moved to publishing directory.
2977 (org-publish-attachment
2978 plist
2979 (org-latex-compile (org-publish-org-to 'latex filename ".tex" plist))
2980 pub-dir))
2983 (provide 'ox-latex)
2985 ;; Local variables:
2986 ;; generated-autoload-file: "org-loaddefs.el"
2987 ;; End:
2989 ;;; ox-latex.el ends here