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