ox-latex: Remove specific default image width for floats
[org-mode.git] / lisp / ox-latex.el
blobd806e637efaecfb3d6c542a7d75f7417805470f7
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:\"
320 If you need more control about the sequence in which the header
321 is built up, or if you want to exclude one of these building
322 blocks for a particular class, you can use the following
323 macro-like placeholders.
325 [DEFAULT-PACKAGES] \\usepackage statements for default packages
326 [NO-DEFAULT-PACKAGES] do not include any of the default packages
327 [PACKAGES] \\usepackage statements for packages
328 [NO-PACKAGES] do not include the packages
329 [EXTRA] the stuff from #+LaTeX_HEADER
330 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
332 So a header like
334 \\documentclass{article}
335 [NO-DEFAULT-PACKAGES]
336 [EXTRA]
337 \\providecommand{\\alert}[1]{\\textbf{#1}}
338 [PACKAGES]
340 will omit the default packages, and will include the
341 #+LaTeX_HEADER lines, then have a call to \\providecommand, and
342 then place \\usepackage commands based on the content of
343 `org-latex-packages-alist'.
345 If your header, `org-latex-default-packages-alist' or
346 `org-latex-packages-alist' inserts
347 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
348 replaced with a coding system derived from
349 `buffer-file-coding-system'. See also the variable
350 `org-latex-inputenc-alist' for a way to influence this mechanism.
352 The sectioning structure
353 ------------------------
355 The sectioning structure of the class is given by the elements
356 following the header string. For each sectioning level, a number
357 of strings is specified. A %s formatter is mandatory in each
358 section string and will be replaced by the title of the section.
360 Instead of a cons cell \(numbered . unnumbered\), you can also
361 provide a list of 2 or 4 elements,
363 \(numbered-open numbered-close\)
367 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
369 providing opening and closing strings for a LaTeX environment
370 that should represent the document section. The opening clause
371 should have a %s to represent the section title.
373 Instead of a list of sectioning commands, you can also specify
374 a function name. That function will be called with two
375 parameters, the \(reduced) level of the headline, and a predicate
376 non-nil when the headline should be numbered. It must return
377 a format string in which the section title will be added."
378 :group 'org-export-latex
379 :type '(repeat
380 (list (string :tag "LaTeX class")
381 (string :tag "LaTeX header")
382 (repeat :tag "Levels" :inline t
383 (choice
384 (cons :tag "Heading"
385 (string :tag " numbered")
386 (string :tag "unnumbered"))
387 (list :tag "Environment"
388 (string :tag "Opening (numbered)")
389 (string :tag "Closing (numbered)")
390 (string :tag "Opening (unnumbered)")
391 (string :tag "Closing (unnumbered)"))
392 (function :tag "Hook computing sectioning"))))))
394 (defcustom org-latex-inputenc-alist nil
395 "Alist of inputenc coding system names, and what should really be used.
396 For example, adding an entry
398 (\"utf8\" . \"utf8x\")
400 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
401 are written as utf8 files."
402 :group 'org-export-latex
403 :type '(repeat
404 (cons
405 (string :tag "Derived from buffer")
406 (string :tag "Use this instead"))))
408 (defcustom org-latex-title-command "\\maketitle"
409 "The command used to insert the title just after \\begin{document}.
410 If this string contains the formatting specification \"%s\" then
411 it will be used as a formatting string, passing the title as an
412 argument."
413 :group 'org-export-latex
414 :type 'string)
416 (defcustom org-latex-toc-command "\\tableofcontents\n\n"
417 "LaTeX command to set the table of contents, list of figures, etc.
418 This command only applies to the table of contents generated with
419 the toc:nil option, not to those generated with #+TOC keyword."
420 :group 'org-export-latex
421 :type 'string)
423 (defcustom org-latex-with-hyperref t
424 "Toggle insertion of \\hypersetup{...} in the preamble."
425 :group 'org-export-latex
426 :type 'boolean)
429 ;;;; Headline
431 (defcustom org-latex-format-headline-function
432 'org-latex-format-headline-default-function
433 "Function for formatting the headline's text.
435 This function will be called with 5 arguments:
436 TODO the todo keyword (string or nil).
437 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
438 PRIORITY the priority of the headline (integer or nil)
439 TEXT the main headline text (string).
440 TAGS the tags as a list of strings (list of strings or nil).
442 The function result will be used in the section format string.
444 Use `org-latex-format-headline-default-function' by default,
445 which format headlines like for Org version prior to 8.0."
446 :group 'org-export-latex
447 :version "24.4"
448 :package-version '(Org . "8.0")
449 :type 'function)
452 ;;;; Footnotes
454 (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
455 "Text used to separate footnotes."
456 :group 'org-export-latex
457 :type 'string)
460 ;;;; Timestamps
462 (defcustom org-latex-active-timestamp-format "\\textit{%s}"
463 "A printf format string to be applied to active timestamps."
464 :group 'org-export-latex
465 :type 'string)
467 (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
468 "A printf format string to be applied to inactive timestamps."
469 :group 'org-export-latex
470 :type 'string)
472 (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
473 "A printf format string to be applied to diary timestamps."
474 :group 'org-export-latex
475 :type 'string)
478 ;;;; Links
480 (defcustom org-latex-image-default-option ""
481 "Default option for images."
482 :group 'org-export-latex
483 :version "24.4"
484 :package-version '(Org . "8.0")
485 :type 'string)
487 (defcustom org-latex-image-default-width ".9\\linewidth"
488 "Default width for images.
489 This value will not be used if a height is provided."
490 :group 'org-export-latex
491 :version "24.4"
492 :package-version '(Org . "8.0")
493 :type 'string)
495 (defcustom org-latex-image-default-height ""
496 "Default height for images.
497 This value will not be used if a width is provided, or if the
498 image is wrapped within a \"figure\" or \"wrapfigure\"
499 environment."
500 :group 'org-export-latex
501 :version "24.4"
502 :package-version '(Org . "8.0")
503 :type 'string)
505 (defcustom org-latex-default-figure-position "htb"
506 "Default position for latex figures."
507 :group 'org-export-latex
508 :type 'string)
510 (defcustom org-latex-inline-image-rules
511 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\)\\'"))
512 "Rules characterizing image files that can be inlined into LaTeX.
514 A rule consists in an association whose key is the type of link
515 to consider, and value is a regexp that will be matched against
516 link's path.
518 Note that, by default, the image extension *actually* allowed
519 depend on the way the LaTeX file is processed. When used with
520 pdflatex, pdf, jpg and png images are OK. When processing
521 through dvi to Postscript, only ps and eps are allowed. The
522 default we use here encompasses both."
523 :group 'org-export-latex
524 :version "24.4"
525 :package-version '(Org . "8.0")
526 :type '(alist :key-type (string :tag "Type")
527 :value-type (regexp :tag "Path")))
529 (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
530 "Format string for links with unknown path type."
531 :group 'org-export-latex
532 :type 'string)
535 ;;;; Tables
537 (defcustom org-latex-default-table-environment "tabular"
538 "Default environment used to build tables."
539 :group 'org-export-latex
540 :version "24.4"
541 :package-version '(Org . "8.0")
542 :type 'string)
544 (defcustom org-latex-default-table-mode 'table
545 "Default mode for tables.
547 Value can be a symbol among:
549 `table' Regular LaTeX table.
551 `math' In this mode, every cell is considered as being in math
552 mode and the complete table will be wrapped within a math
553 environment. It is particularly useful to write matrices.
555 `inline-math' This mode is almost the same as `math', but the
556 math environment will be inlined.
558 `verbatim' The table is exported as it appears in the Org
559 buffer, within a verbatim environment.
561 This value can be overridden locally with, i.e. \":mode math\" in
562 LaTeX attributes.
564 When modifying this variable, it may be useful to change
565 `org-latex-default-table-environment' accordingly."
566 :group 'org-export-latex
567 :version "24.4"
568 :package-version '(Org . "8.0")
569 :type '(choice (const :tag "Table" table)
570 (const :tag "Matrix" math)
571 (const :tag "Inline matrix" inline-math)
572 (const :tag "Verbatim" verbatim)))
574 (defcustom org-latex-tables-centered t
575 "When non-nil, tables are exported in a center environment."
576 :group 'org-export-latex
577 :type 'boolean)
579 (defcustom org-latex-tables-booktabs nil
580 "When non-nil, display tables in a formal \"booktabs\" style.
581 This option assumes that the \"booktabs\" package is properly
582 loaded in the header of the document. This value can be ignored
583 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
584 attributes."
585 :group 'org-export-latex
586 :version "24.4"
587 :package-version '(Org . "8.0")
588 :type 'boolean)
590 (defcustom org-latex-table-caption-above t
591 "When non-nil, place caption string at the beginning of the table.
592 Otherwise, place it near the end."
593 :group 'org-export-latex
594 :type 'boolean)
596 (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
597 "Format string to display numbers in scientific notation.
598 The format should have \"%s\" twice, for mantissa and exponent
599 \(i.e., \"%s\\\\times10^{%s}\").
601 When nil, no transformation is made."
602 :group 'org-export-latex
603 :version "24.4"
604 :package-version '(Org . "8.0")
605 :type '(choice
606 (string :tag "Format string")
607 (const :tag "No formatting")))
610 ;;;; Text markup
612 (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
613 (code . verb)
614 (italic . "\\emph{%s}")
615 (strike-through . "\\st{%s}")
616 (underline . "\\underline{%s}")
617 (verbatim . protectedtexttt))
618 "Alist of LaTeX expressions to convert text markup.
620 The key must be a symbol among `bold', `code', `italic',
621 `strike-through', `underline' and `verbatim'. The value is
622 a formatting string to wrap fontified text with.
624 Value can also be set to the following symbols: `verb' and
625 `protectedtexttt'. For the former, Org will use \"\\verb\" to
626 create a format string and select a delimiter character that
627 isn't in the string. For the latter, Org will use \"\\texttt\"
628 to typeset and try to protect special characters.
630 If no association can be found for a given markup, text will be
631 returned as-is."
632 :group 'org-export-latex
633 :type 'alist
634 :options '(bold code italic strike-through underline verbatim))
637 ;;;; Drawers
639 (defcustom org-latex-format-drawer-function nil
640 "Function called to format a drawer in LaTeX code.
642 The function must accept two parameters:
643 NAME the drawer name, like \"LOGBOOK\"
644 CONTENTS the contents of the drawer.
646 The function should return the string to be exported.
648 For example, the variable could be set to the following function
649 in order to mimic default behaviour:
651 \(defun org-latex-format-drawer-default \(name contents\)
652 \"Format a drawer element for LaTeX export.\"
653 contents\)"
654 :group 'org-export-latex
655 :type 'function)
658 ;;;; Inlinetasks
660 (defcustom org-latex-format-inlinetask-function nil
661 "Function called to format an inlinetask in LaTeX code.
663 The function must accept six parameters:
664 TODO the todo keyword, as a string
665 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
666 PRIORITY the inlinetask priority, as a string
667 NAME the inlinetask name, as a string.
668 TAGS the inlinetask tags, as a list of strings.
669 CONTENTS the contents of the inlinetask, as a string.
671 The function should return the string to be exported.
673 For example, the variable could be set to the following function
674 in order to mimic default behaviour:
676 \(defun org-latex-format-inlinetask \(todo type priority name tags contents\)
677 \"Format an inline task element for LaTeX export.\"
678 \(let ((full-title
679 \(concat
680 \(when todo
681 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
682 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
683 title
684 \(when tags
685 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
686 \(mapconcat 'identity tags \":\")))))
687 \(format (concat \"\\\\begin{center}\\n\"
688 \"\\\\fbox{\\n\"
689 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
690 \"%s\\n\\n\"
691 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
692 \"%s\"
693 \"\\\\end{minipage}}\"
694 \"\\\\end{center}\")
695 full-title contents))"
696 :group 'org-export-latex
697 :type 'function)
700 ;; Src blocks
702 (defcustom org-latex-listings nil
703 "Non-nil means export source code using the listings package.
704 This package will fontify source code, possibly even with color.
705 If you want to use this, you also need to make LaTeX use the
706 listings package, and if you want to have color, the color
707 package. Just add these to `org-latex-packages-alist', for
708 example using customize, or with something like:
710 \(require 'ox-latex)
711 \(add-to-list 'org-latex-packages-alist '\(\"\" \"listings\"))
712 \(add-to-list 'org-latex-packages-alist '\(\"\" \"color\"))
714 Alternatively,
716 \(setq org-latex-listings 'minted)
718 causes source code to be exported using the minted package as
719 opposed to listings. If you want to use minted, you need to add
720 the minted package to `org-latex-packages-alist', for example
721 using customize, or with
723 \(require 'ox-latex)
724 \(add-to-list 'org-latex-packages-alist '\(\"\" \"minted\"))
726 In addition, it is necessary to install pygments
727 \(http://pygments.org), and to configure the variable
728 `org-latex-pdf-process' so that the -shell-escape option is
729 passed to pdflatex."
730 :group 'org-export-latex
731 :type '(choice
732 (const :tag "Use listings" t)
733 (const :tag "Use minted" 'minted)
734 (const :tag "Export verbatim" nil)))
736 (defcustom org-latex-listings-langs
737 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
738 (c "C") (cc "C++")
739 (fortran "fortran")
740 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
741 (html "HTML") (xml "XML")
742 (tex "TeX") (latex "TeX")
743 (shell-script "bash")
744 (gnuplot "Gnuplot")
745 (ocaml "Caml") (caml "Caml")
746 (sql "SQL") (sqlite "sql"))
747 "Alist mapping languages to their listing language counterpart.
748 The key is a symbol, the major mode symbol without the \"-mode\".
749 The value is the string that should be inserted as the language
750 parameter for the listings package. If the mode name and the
751 listings name are the same, the language does not need an entry
752 in this list - but it does not hurt if it is present."
753 :group 'org-export-latex
754 :type '(repeat
755 (list
756 (symbol :tag "Major mode ")
757 (string :tag "Listings language"))))
759 (defcustom org-latex-listings-options nil
760 "Association list of options for the latex listings package.
762 These options are supplied as a comma-separated list to the
763 \\lstset command. Each element of the association list should be
764 a list containing two strings: the name of the option, and the
765 value. For example,
767 (setq org-latex-listings-options
768 '((\"basicstyle\" \"\\small\")
769 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
771 will typeset the code in a small size font with underlined, bold
772 black keywords.
774 Note that the same options will be applied to blocks of all
775 languages."
776 :group 'org-export-latex
777 :type '(repeat
778 (list
779 (string :tag "Listings option name ")
780 (string :tag "Listings option value"))))
782 (defcustom org-latex-minted-langs
783 '((emacs-lisp "common-lisp")
784 (cc "c++")
785 (cperl "perl")
786 (shell-script "bash")
787 (caml "ocaml"))
788 "Alist mapping languages to their minted language counterpart.
789 The key is a symbol, the major mode symbol without the \"-mode\".
790 The value is the string that should be inserted as the language
791 parameter for the minted package. If the mode name and the
792 listings name are the same, the language does not need an entry
793 in this list - but it does not hurt if it is present.
795 Note that minted uses all lower case for language identifiers,
796 and that the full list of language identifiers can be obtained
797 with:
799 pygmentize -L lexers"
800 :group 'org-export-latex
801 :type '(repeat
802 (list
803 (symbol :tag "Major mode ")
804 (string :tag "Minted language"))))
806 (defcustom org-latex-minted-options nil
807 "Association list of options for the latex minted package.
809 These options are supplied within square brackets in
810 \\begin{minted} environments. Each element of the alist should
811 be a list containing two strings: the name of the option, and the
812 value. For example,
814 \(setq org-latex-minted-options
815 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
817 will result in src blocks being exported with
819 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
821 as the start of the minted environment. Note that the same
822 options will be applied to blocks of all languages."
823 :group 'org-export-latex
824 :type '(repeat
825 (list
826 (string :tag "Minted option name ")
827 (string :tag "Minted option value"))))
829 (defcustom org-latex-long-listings nil
830 "When non-nil no listing will be wrapped within a float.
832 Removing floats may break some functionalities. For example, it
833 will be impossible to use cross-references to listings when using
834 `minted' set-up when this variable is non-nil.
836 This value can be locally ignored with \":long-listing t\" and
837 \":long-listing nil\" LaTeX attributes."
838 :group 'org-export-latex
839 :version "24.4"
840 :package-version '(Org . "8.0")
841 :type 'boolean)
843 (defvar org-latex-custom-lang-environments nil
844 "Alist mapping languages to language-specific LaTeX environments.
846 It is used during export of src blocks by the listings and minted
847 latex packages. For example,
849 \(setq org-latex-custom-lang-environments
850 '\(\(python \"pythoncode\"\)\)\)
852 would have the effect that if org encounters begin_src python
853 during latex export it will output
855 \\begin{pythoncode}
856 <src block body>
857 \\end{pythoncode}")
860 ;;;; Compilation
862 (defcustom org-latex-pdf-process
863 '("pdflatex -interaction nonstopmode -output-directory %o %f"
864 "pdflatex -interaction nonstopmode -output-directory %o %f"
865 "pdflatex -interaction nonstopmode -output-directory %o %f")
866 "Commands to process a LaTeX file to a PDF file.
867 This is a list of strings, each of them will be given to the
868 shell as a command. %f in the command will be replaced by the
869 full file name, %b by the file base name (i.e. without directory
870 and extension parts) and %o by the base directory of the file.
872 The reason why this is a list is that it usually takes several
873 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
874 does not have a clever mechanism to detect which of these
875 commands have to be run to get to a stable result, and it also
876 does not do any error checking.
878 By default, Org uses 3 runs of `pdflatex' to do the processing.
879 If you have texi2dvi on your system and if that does not cause
880 the infamous egrep/locale bug:
882 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
884 then `texi2dvi' is the superior choice. Org does offer it as one
885 of the customize options.
887 Alternatively, this may be a Lisp function that does the
888 processing, so you could use this to apply the machinery of
889 AUCTeX or the Emacs LaTeX mode. This function should accept the
890 file name as its single argument."
891 :group 'org-export-pdf
892 :type '(choice
893 (repeat :tag "Shell command sequence"
894 (string :tag "Shell command"))
895 (const :tag "2 runs of pdflatex"
896 ("pdflatex -interaction nonstopmode -output-directory %o %f"
897 "pdflatex -interaction nonstopmode -output-directory %o %f"))
898 (const :tag "3 runs of pdflatex"
899 ("pdflatex -interaction nonstopmode -output-directory %o %f"
900 "pdflatex -interaction nonstopmode -output-directory %o %f"
901 "pdflatex -interaction nonstopmode -output-directory %o %f"))
902 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
903 ("pdflatex -interaction nonstopmode -output-directory %o %f"
904 "bibtex %b"
905 "pdflatex -interaction nonstopmode -output-directory %o %f"
906 "pdflatex -interaction nonstopmode -output-directory %o %f"))
907 (const :tag "2 runs of xelatex"
908 ("xelatex -interaction nonstopmode -output-directory %o %f"
909 "xelatex -interaction nonstopmode -output-directory %o %f"))
910 (const :tag "3 runs of xelatex"
911 ("xelatex -interaction nonstopmode -output-directory %o %f"
912 "xelatex -interaction nonstopmode -output-directory %o %f"
913 "xelatex -interaction nonstopmode -output-directory %o %f"))
914 (const :tag "xelatex,bibtex,xelatex,xelatex"
915 ("xelatex -interaction nonstopmode -output-directory %o %f"
916 "bibtex %b"
917 "xelatex -interaction nonstopmode -output-directory %o %f"
918 "xelatex -interaction nonstopmode -output-directory %o %f"))
919 (const :tag "texi2dvi"
920 ("texi2dvi -p -b -c -V %f"))
921 (const :tag "rubber"
922 ("rubber -d --into %o %f"))
923 (function)))
925 (defcustom org-latex-logfiles-extensions
926 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
927 "The list of file extensions to consider as LaTeX logfiles.
928 The logfiles will be remove if `org-latex-remove-logfiles' is
929 non-nil."
930 :group 'org-export-latex
931 :type '(repeat (string :tag "Extension")))
933 (defcustom org-latex-remove-logfiles t
934 "Non-nil means remove the logfiles produced by PDF production.
935 By default, logfiles are files with these extensions: .aux, .idx,
936 .log, .out, .toc, .nav, .snm and .vrb. To define the set of
937 logfiles to remove, set `org-latex-logfiles-extensions'."
938 :group 'org-export-latex
939 :type 'boolean)
941 (defcustom org-latex-known-errors
942 '(("Reference.*?undefined" . "[undefined reference]")
943 ("Citation.*?undefined" . "[undefined citation]")
944 ("Undefined control sequence" . "[undefined control sequence]")
945 ("^! LaTeX.*?Error" . "[LaTeX error]")
946 ("^! Package.*?Error" . "[package error]")
947 ("Runaway argument" . "Runaway argument"))
948 "Alist of regular expressions and associated messages for the user.
949 The regular expressions are used to find possible errors in the
950 log of a latex-run."
951 :group 'org-export-latex
952 :version "24.4"
953 :package-version '(Org . "8.0")
954 :type '(repeat
955 (cons
956 (string :tag "Regexp")
957 (string :tag "Message"))))
961 ;;; Internal Functions
963 (defun org-latex--caption/label-string (element info)
964 "Return caption and label LaTeX string for ELEMENT.
966 INFO is a plist holding contextual information. If there's no
967 caption nor label, return the empty string.
969 For non-floats, see `org-latex--wrap-label'."
970 (let* ((label (org-element-property :name element))
971 (label-str (if (not (org-string-nw-p label)) ""
972 (format "\\label{%s}"
973 (org-export-solidify-link-text label))))
974 (main (org-export-get-caption element))
975 (short (org-export-get-caption element t)))
976 (cond
977 ((and (not main) (equal label-str "")) "")
978 ((not main) (concat label-str "\n"))
979 ;; Option caption format with short name.
980 (short (format "\\caption[%s]{%s%s}\n"
981 (org-export-data short info)
982 label-str
983 (org-export-data main info)))
984 ;; Standard caption format.
985 (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
987 (defun org-latex-guess-inputenc (header)
988 "Set the coding system in inputenc to what the buffer is.
990 HEADER is the LaTeX header string. This function only applies
991 when specified inputenc option is \"AUTO\".
993 Return the new header, as a string."
994 (let* ((cs (or (ignore-errors
995 (latexenc-coding-system-to-inputenc
996 (or org-export-coding-system buffer-file-coding-system)))
997 "utf8")))
998 (if (not cs) header
999 ;; First translate if that is requested.
1000 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
1001 ;; Then find the \usepackage statement and replace the option.
1002 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
1003 cs header t nil 1))))
1005 (defun org-latex-guess-babel-language (header info)
1006 "Set Babel's language according to LANGUAGE keyword.
1008 HEADER is the LaTeX header string. INFO is the plist used as
1009 a communication channel.
1011 Insertion of guessed language only happens when Babel package has
1012 explicitly been loaded. Then it is added to the rest of
1013 package's options.
1015 Return the new header."
1016 (let ((language-code (plist-get info :language)))
1017 ;; If no language is set or Babel package is not loaded, return
1018 ;; HEADER as-is.
1019 (if (or (not (stringp language-code))
1020 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
1021 header
1022 (let ((options (save-match-data
1023 (org-split-string (match-string 1 header) ",")))
1024 (language (cdr (assoc language-code
1025 org-latex-babel-language-alist))))
1026 ;; If LANGUAGE is already loaded, return header. Otherwise,
1027 ;; append LANGUAGE to other options.
1028 (if (member language options) header
1029 (replace-match (mapconcat 'identity
1030 (append options (list language))
1031 ",")
1032 nil nil header 1))))))
1034 (defun org-latex--find-verb-separator (s)
1035 "Return a character not used in string S.
1036 This is used to choose a separator for constructs like \\verb."
1037 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1038 (loop for c across ll
1039 when (not (string-match (regexp-quote (char-to-string c)) s))
1040 return (char-to-string c))))
1042 (defun org-latex--make-option-string (options)
1043 "Return a comma separated string of keywords and values.
1044 OPTIONS is an alist where the key is the options keyword as
1045 a string, and the value a list containing the keyword value, or
1046 nil."
1047 (mapconcat (lambda (pair)
1048 (concat (first pair)
1049 (when (> (length (second pair)) 0)
1050 (concat "=" (second pair)))))
1051 options
1052 ","))
1054 (defun org-latex--wrap-label (element output)
1055 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1056 This function shouldn't be used for floats. See
1057 `org-latex--caption/label-string'."
1058 (let ((label (org-element-property :name element)))
1059 (if (not (and (org-string-nw-p output) (org-string-nw-p label))) output
1060 (concat (format "\\label{%s}\n" (org-export-solidify-link-text label))
1061 output))))
1063 (defun org-latex--text-markup (text markup)
1064 "Format TEXT depending on MARKUP text markup.
1065 See `org-latex-text-markup-alist' for details."
1066 (let ((fmt (cdr (assq markup org-latex-text-markup-alist))))
1067 (cond
1068 ;; No format string: Return raw text.
1069 ((not fmt) text)
1070 ;; Handle the `verb' special case: Find and appropriate separator
1071 ;; and use "\\verb" command.
1072 ((eq 'verb fmt)
1073 (let ((separator (org-latex--find-verb-separator text)))
1074 (concat "\\verb" separator text separator)))
1075 ;; Handle the `protectedtexttt' special case: Protect some
1076 ;; special chars and use "\texttt{%s}" format string.
1077 ((eq 'protectedtexttt fmt)
1078 (let ((start 0)
1079 (trans '(("\\" . "\\textbackslash{}")
1080 ("~" . "\\textasciitilde{}")
1081 ("^" . "\\textasciicircum{}")))
1082 (rtn "")
1083 char)
1084 (while (string-match "[\\{}$%&_#~^]" text)
1085 (setq char (match-string 0 text))
1086 (if (> (match-beginning 0) 0)
1087 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
1088 (setq text (substring text (1+ (match-beginning 0))))
1089 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1090 rtn (concat rtn char)))
1091 (setq text (concat rtn text)
1092 fmt "\\texttt{%s}")
1093 (while (string-match "--" text)
1094 (setq text (replace-match "-{}-" t t text)))
1095 (format fmt text)))
1096 ;; Else use format string.
1097 (t (format fmt text)))))
1099 (defun org-latex--delayed-footnotes-definitions (element info)
1100 "Return footnotes definitions in ELEMENT as a string.
1102 INFO is a plist used as a communication channel.
1104 Footnotes definitions are returned within \"\\footnotetxt{}\"
1105 commands.
1107 This function is used within constructs that don't support
1108 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1109 \"\\footnotemark\" is used within the construct and the function
1110 just outside of it."
1111 (mapconcat
1112 (lambda (ref)
1113 (format
1114 "\\footnotetext[%s]{%s}"
1115 (org-export-get-footnote-number ref info)
1116 (org-trim
1117 (org-export-data
1118 (org-export-get-footnote-definition ref info) info))))
1119 ;; Find every footnote reference in ELEMENT.
1120 (let* (all-refs
1121 search-refs ; For byte-compiler.
1122 (search-refs
1123 (function
1124 (lambda (data)
1125 ;; Return a list of all footnote references never seen
1126 ;; before in DATA.
1127 (org-element-map data 'footnote-reference
1128 (lambda (ref)
1129 (when (org-export-footnote-first-reference-p ref info)
1130 (push ref all-refs)
1131 (when (eq (org-element-property :type ref) 'standard)
1132 (funcall search-refs
1133 (org-export-get-footnote-definition ref info)))))
1134 info)
1135 (reverse all-refs)))))
1136 (funcall search-refs element))
1137 ""))
1141 ;;; Template
1143 (defun org-latex-template (contents info)
1144 "Return complete document string after LaTeX conversion.
1145 CONTENTS is the transcoded contents string. INFO is a plist
1146 holding export options."
1147 (let ((title (org-export-data (plist-get info :title) info)))
1148 (concat
1149 ;; Time-stamp.
1150 (and (plist-get info :time-stamp-file)
1151 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1152 ;; Document class and packages.
1153 (let ((class (plist-get info :latex-class))
1154 (class-options (plist-get info :latex-class-options)))
1155 (org-element-normalize-string
1156 (let* ((header (nth 1 (assoc class org-latex-classes)))
1157 (document-class-string
1158 (and (stringp header)
1159 (if (not class-options) header
1160 (replace-regexp-in-string
1161 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1162 class-options header t nil 1)))))
1163 (if (not document-class-string)
1164 (user-error "Unknown LaTeX class `%s'" class)
1165 (org-latex-guess-babel-language
1166 (org-latex-guess-inputenc
1167 (org-splice-latex-header
1168 document-class-string
1169 org-latex-default-packages-alist
1170 org-latex-packages-alist nil
1171 (concat (plist-get info :latex-header)
1172 (plist-get info :latex-header-extra))))
1173 info)))))
1174 ;; Possibly limit depth for headline numbering.
1175 (let ((sec-num (plist-get info :section-numbers)))
1176 (when (integerp sec-num)
1177 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1178 ;; Author.
1179 (let ((author (and (plist-get info :with-author)
1180 (let ((auth (plist-get info :author)))
1181 (and auth (org-export-data auth info)))))
1182 (email (and (plist-get info :with-email)
1183 (org-export-data (plist-get info :email) info))))
1184 (cond ((and author email (not (string= "" email)))
1185 (format "\\author{%s\\thanks{%s}}\n" author email))
1186 ((or author email) (format "\\author{%s}\n" (or author email)))))
1187 ;; Date.
1188 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1189 (format "\\date{%s}\n" (org-export-data date info)))
1190 ;; Title
1191 (format "\\title{%s}\n" title)
1192 ;; Hyperref options.
1193 (when (plist-get info :latex-hyperref-p)
1194 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1195 (or (plist-get info :keywords) "")
1196 (or (plist-get info :description) "")
1197 (if (not (plist-get info :with-creator)) ""
1198 (plist-get info :creator))))
1199 ;; Document start.
1200 "\\begin{document}\n\n"
1201 ;; Title command.
1202 (org-element-normalize-string
1203 (cond ((string= "" title) nil)
1204 ((not (stringp org-latex-title-command)) nil)
1205 ((string-match "\\(?:[^%]\\|^\\)%s"
1206 org-latex-title-command)
1207 (format org-latex-title-command title))
1208 (t org-latex-title-command)))
1209 ;; Table of contents.
1210 (let ((depth (plist-get info :with-toc)))
1211 (when depth
1212 (concat (when (wholenump depth)
1213 (format "\\setcounter{tocdepth}{%d}\n" depth))
1214 org-latex-toc-command)))
1215 ;; Document's body.
1216 contents
1217 ;; Creator.
1218 (let ((creator-info (plist-get info :with-creator)))
1219 (cond
1220 ((not creator-info) "")
1221 ((eq creator-info 'comment)
1222 (format "%% %s\n" (plist-get info :creator)))
1223 (t (concat (plist-get info :creator) "\n"))))
1224 ;; Document end.
1225 "\\end{document}")))
1229 ;;; Transcode Functions
1231 ;;;; Bold
1233 (defun org-latex-bold (bold contents info)
1234 "Transcode BOLD from Org to LaTeX.
1235 CONTENTS is the text with bold markup. INFO is a plist holding
1236 contextual information."
1237 (org-latex--text-markup contents 'bold))
1240 ;;;; Center Block
1242 (defun org-latex-center-block (center-block contents info)
1243 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1244 CONTENTS holds the contents of the center block. INFO is a plist
1245 holding contextual information."
1246 (org-latex--wrap-label
1247 center-block
1248 (format "\\begin{center}\n%s\\end{center}" contents)))
1251 ;;;; Clock
1253 (defun org-latex-clock (clock contents info)
1254 "Transcode a CLOCK element from Org to LaTeX.
1255 CONTENTS is nil. INFO is a plist holding contextual
1256 information."
1257 (concat
1258 "\\noindent"
1259 (format "\\textbf{%s} " org-clock-string)
1260 (format org-latex-inactive-timestamp-format
1261 (concat (org-translate-time
1262 (org-element-property :raw-value
1263 (org-element-property :value clock)))
1264 (let ((time (org-element-property :duration clock)))
1265 (and time (format " (%s)" time)))))
1266 "\\\\"))
1269 ;;;; Code
1271 (defun org-latex-code (code contents info)
1272 "Transcode a CODE object from Org to LaTeX.
1273 CONTENTS is nil. INFO is a plist used as a communication
1274 channel."
1275 (org-latex--text-markup (org-element-property :value code) 'code))
1278 ;;;; Drawer
1280 (defun org-latex-drawer (drawer contents info)
1281 "Transcode a DRAWER element from Org to LaTeX.
1282 CONTENTS holds the contents of the block. INFO is a plist
1283 holding contextual information."
1284 (let* ((name (org-element-property :drawer-name drawer))
1285 (output (if (functionp org-latex-format-drawer-function)
1286 (funcall org-latex-format-drawer-function
1287 name contents)
1288 ;; If there's no user defined function: simply
1289 ;; display contents of the drawer.
1290 contents)))
1291 (org-latex--wrap-label drawer output)))
1294 ;;;; Dynamic Block
1296 (defun org-latex-dynamic-block (dynamic-block contents info)
1297 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1298 CONTENTS holds the contents of the block. INFO is a plist
1299 holding contextual information. See `org-export-data'."
1300 (org-latex--wrap-label dynamic-block contents))
1303 ;;;; Entity
1305 (defun org-latex-entity (entity contents info)
1306 "Transcode an ENTITY object from Org to LaTeX.
1307 CONTENTS are the definition itself. INFO is a plist holding
1308 contextual information."
1309 (let ((ent (org-element-property :latex entity)))
1310 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
1313 ;;;; Example Block
1315 (defun org-latex-example-block (example-block contents info)
1316 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1317 CONTENTS is nil. INFO is a plist holding contextual
1318 information."
1319 (when (org-string-nw-p (org-element-property :value example-block))
1320 (org-latex--wrap-label
1321 example-block
1322 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1323 (org-export-format-code-default example-block info)))))
1326 ;;;; Export Block
1328 (defun org-latex-export-block (export-block contents info)
1329 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1330 CONTENTS is nil. INFO is a plist holding contextual information."
1331 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1332 (org-remove-indentation (org-element-property :value export-block))))
1335 ;;;; Export Snippet
1337 (defun org-latex-export-snippet (export-snippet contents info)
1338 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1339 CONTENTS is nil. INFO is a plist holding contextual information."
1340 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1341 (org-element-property :value export-snippet)))
1344 ;;;; Fixed Width
1346 (defun org-latex-fixed-width (fixed-width contents info)
1347 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1348 CONTENTS is nil. INFO is a plist holding contextual information."
1349 (org-latex--wrap-label
1350 fixed-width
1351 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1352 (org-remove-indentation
1353 (org-element-property :value fixed-width)))))
1356 ;;;; Footnote Reference
1358 ;; Footnote reference export is handled by
1359 ;; `org-latex-footnote-reference'.
1361 ;; Internally, `org-latex--get-footnote-counter' is used to restore
1362 ;; the value of the LaTeX "footnote" counter after a jump due to
1363 ;; a reference to an already defined footnote. It is only needed in
1364 ;; item tags since the optional argument to \footnotemark is not
1365 ;; allowed there.
1367 (defun org-latex--get-footnote-counter (footnote-reference info)
1368 "Return \"footnote\" counter before FOOTNOTE-REFERENCE is encountered.
1369 INFO is a plist used as a communication channel."
1370 ;; Find original counter value by counting number of footnote
1371 ;; references appearing for the first time before the current
1372 ;; footnote reference.
1373 (let* ((label (org-element-property :label footnote-reference))
1374 seen-refs
1375 search-ref ; For byte-compiler.
1376 (search-ref
1377 (function
1378 (lambda (data)
1379 ;; Search footnote references through DATA, filling
1380 ;; SEEN-REFS along the way.
1381 (org-element-map data 'footnote-reference
1382 (lambda (fn)
1383 (let ((fn-lbl (org-element-property :label fn)))
1384 (cond
1385 ;; Anonymous footnote match: return number.
1386 ((eq fn footnote-reference) (length seen-refs))
1387 ;; Anonymous footnote: it's always a new one.
1388 ;; Also, be sure to return nil from the `cond' so
1389 ;; `first-match' doesn't get us out of the loop.
1390 ((not fn-lbl) (push 'inline seen-refs) nil)
1391 ;; Label not seen so far: add it so SEEN-REFS.
1393 ;; Also search for subsequent references in
1394 ;; footnote definition so numbering follows
1395 ;; reading logic. Note that we don't care about
1396 ;; inline definitions, since `org-element-map'
1397 ;; already traverses them at the right time.
1398 ((not (member fn-lbl seen-refs))
1399 (push fn-lbl seen-refs)
1400 (funcall search-ref
1401 (org-export-get-footnote-definition fn info))))))
1402 ;; Don't enter footnote definitions since it will
1403 ;; happen when their first reference is found.
1404 info 'first-match 'footnote-definition)))))
1405 (funcall search-ref (plist-get info :parse-tree))))
1407 (defun org-latex-footnote-reference (footnote-reference contents info)
1408 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1409 CONTENTS is nil. INFO is a plist holding contextual information."
1410 (concat
1411 ;; Insert separator between two footnotes in a row.
1412 (let ((prev (org-export-get-previous-element footnote-reference info)))
1413 (when (eq (org-element-type prev) 'footnote-reference)
1414 org-latex-footnote-separator))
1415 (cond
1416 ;; Use \footnotemark if reference is within an item's tag.
1417 ((eq (org-element-type (org-export-get-parent-element footnote-reference))
1418 'item)
1419 (if (org-export-footnote-first-reference-p footnote-reference info)
1420 "\\footnotemark"
1421 ;; Since we can't specify footnote number as an optional
1422 ;; argument within an item tag, some extra work has to be done
1423 ;; when the footnote has already been referenced. In that
1424 ;; case, set footnote counter to the desired number, use the
1425 ;; footnotemark, then set counter back to its original value.
1426 (format
1427 "\\setcounter{footnote}{%s}\\footnotemark\\setcounter{footnote}{%s}"
1428 (1- (org-export-get-footnote-number footnote-reference info))
1429 (org-latex--get-footnote-counter footnote-reference info))))
1430 ;; Use \footnotemark if the footnote has already been defined.
1431 ((not (org-export-footnote-first-reference-p footnote-reference info))
1432 (format "\\footnotemark[%s]{}"
1433 (org-export-get-footnote-number footnote-reference info)))
1434 ;; Use \footnotemark if reference is within another footnote
1435 ;; reference, footnote definition or table cell.
1436 ((loop for parent in (org-export-get-genealogy footnote-reference)
1437 thereis (memq (org-element-type parent)
1438 '(footnote-reference footnote-definition table-cell)))
1439 "\\footnotemark")
1440 ;; Otherwise, define it with \footnote command.
1442 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1443 (concat
1444 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1445 ;; Retrieve all footnote references within the footnote and
1446 ;; add their definition after it, since LaTeX doesn't support
1447 ;; them inside.
1448 (org-latex--delayed-footnotes-definitions def info)))))))
1451 ;;;; Headline
1453 (defun org-latex-headline (headline contents info)
1454 "Transcode a HEADLINE element from Org to LaTeX.
1455 CONTENTS holds the contents of the headline. INFO is a plist
1456 holding contextual information."
1457 (unless (org-element-property :footnote-section-p headline)
1458 (let* ((class (plist-get info :latex-class))
1459 (level (org-export-get-relative-level headline info))
1460 (numberedp (org-export-numbered-headline-p headline info))
1461 (class-sectionning (assoc class org-latex-classes))
1462 ;; Section formatting will set two placeholders: one for
1463 ;; the title and the other for the contents.
1464 (section-fmt
1465 (let ((sec (if (functionp (nth 2 class-sectionning))
1466 (funcall (nth 2 class-sectionning) level numberedp)
1467 (nth (1+ level) class-sectionning))))
1468 (cond
1469 ;; No section available for that LEVEL.
1470 ((not sec) nil)
1471 ;; Section format directly returned by a function. Add
1472 ;; placeholder for contents.
1473 ((stringp sec) (concat sec "\n%s"))
1474 ;; (numbered-section . unnumbered-section)
1475 ((not (consp (cdr sec)))
1476 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1477 ;; (numbered-open numbered-close)
1478 ((= (length sec) 2)
1479 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1480 ;; (num-in num-out no-num-in no-num-out)
1481 ((= (length sec) 4)
1482 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1483 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1484 (text (org-export-data (org-element-property :title headline) info))
1485 (todo
1486 (and (plist-get info :with-todo-keywords)
1487 (let ((todo (org-element-property :todo-keyword headline)))
1488 (and todo (org-export-data todo info)))))
1489 (todo-type (and todo (org-element-property :todo-type headline)))
1490 (tags (and (plist-get info :with-tags)
1491 (org-export-get-tags headline info)))
1492 (priority (and (plist-get info :with-priority)
1493 (org-element-property :priority headline)))
1494 ;; Create the headline text along with a no-tag version.
1495 ;; The latter is required to remove tags from toc.
1496 (full-text (funcall org-latex-format-headline-function
1497 todo todo-type priority text tags))
1498 ;; Associate \label to the headline for internal links.
1499 (headline-label
1500 (format "\\label{sec-%s}\n"
1501 (mapconcat 'number-to-string
1502 (org-export-get-headline-number headline info)
1503 "-")))
1504 (pre-blanks
1505 (make-string (org-element-property :pre-blank headline) 10)))
1506 (if (or (not section-fmt) (org-export-low-level-p headline info))
1507 ;; This is a deep sub-tree: export it as a list item. Also
1508 ;; export as items headlines for which no section format has
1509 ;; been found.
1510 (let ((low-level-body
1511 (concat
1512 ;; If headline is the first sibling, start a list.
1513 (when (org-export-first-sibling-p headline info)
1514 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1515 ;; Itemize headline
1516 "\\item " full-text "\n" headline-label pre-blanks contents)))
1517 ;; If headline is not the last sibling simply return
1518 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1519 ;; any blank line.
1520 (if (not (org-export-last-sibling-p headline info)) low-level-body
1521 (replace-regexp-in-string
1522 "[ \t\n]*\\'"
1523 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1524 low-level-body)))
1525 ;; This is a standard headline. Export it as a section. Add
1526 ;; an alternative heading when possible.
1527 (let ((opt-title
1528 (funcall org-latex-format-headline-function
1529 todo todo-type priority
1530 (org-export-data
1531 (org-export-get-alt-title headline info) info)
1532 (and (eq (plist-get info :with-tags) t) tags))))
1533 (if (and numberedp opt-title
1534 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
1535 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1536 ;; Replace square brackets with parenthesis
1537 ;; since square brackets are not supported in
1538 ;; optional arguments.
1539 (replace-regexp-in-string
1540 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1541 full-text
1542 (concat headline-label pre-blanks contents))
1543 ;; Impossible to add an alternative heading. Fallback to
1544 ;; regular sectioning format string.
1545 (format section-fmt full-text
1546 (concat headline-label pre-blanks contents))))))))
1548 (defun org-latex-format-headline-default-function
1549 (todo todo-type priority text tags)
1550 "Default format function for a headline.
1551 See `org-latex-format-headline-function' for details."
1552 (concat
1553 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1554 (and priority (format "\\framebox{\\#%c} " priority))
1555 text
1556 (and tags
1557 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags ":")))))
1560 ;;;; Horizontal Rule
1562 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1563 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1564 CONTENTS is nil. INFO is a plist holding contextual information."
1565 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1566 (prev (org-export-get-previous-element horizontal-rule info)))
1567 (concat
1568 ;; Make sure the rule doesn't start at the end of the current
1569 ;; line by separating it with a blank line from previous element.
1570 (when (and prev
1571 (let ((prev-blank (org-element-property :post-blank prev)))
1572 (or (not prev-blank) (zerop prev-blank))))
1573 "\n")
1574 (org-latex--wrap-label
1575 horizontal-rule
1576 (format "\\rule{%s}{%s}"
1577 (or (plist-get attr :width) "\\linewidth")
1578 (or (plist-get attr :thickness) "0.5pt"))))))
1581 ;;;; Inline Src Block
1583 (defun org-latex-inline-src-block (inline-src-block contents info)
1584 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1585 CONTENTS holds the contents of the item. INFO is a plist holding
1586 contextual information."
1587 (let* ((code (org-element-property :value inline-src-block))
1588 (separator (org-latex--find-verb-separator code)))
1589 (cond
1590 ;; Do not use a special package: transcode it verbatim.
1591 ((not org-latex-listings)
1592 (concat "\\verb" separator code separator))
1593 ;; Use minted package.
1594 ((eq org-latex-listings 'minted)
1595 (let* ((org-lang (org-element-property :language inline-src-block))
1596 (mint-lang (or (cadr (assq (intern org-lang)
1597 org-latex-minted-langs))
1598 org-lang))
1599 (options (org-latex--make-option-string
1600 org-latex-minted-options)))
1601 (concat (format "\\mint%s{%s}"
1602 (if (string= options "") "" (format "[%s]" options))
1603 mint-lang)
1604 separator code separator)))
1605 ;; Use listings package.
1607 ;; Maybe translate language's name.
1608 (let* ((org-lang (org-element-property :language inline-src-block))
1609 (lst-lang (or (cadr (assq (intern org-lang)
1610 org-latex-listings-langs))
1611 org-lang))
1612 (options (org-latex--make-option-string
1613 (append org-latex-listings-options
1614 `(("language" ,lst-lang))))))
1615 (concat (format "\\lstinline[%s]" options)
1616 separator code separator))))))
1619 ;;;; Inlinetask
1621 (defun org-latex-inlinetask (inlinetask contents info)
1622 "Transcode an INLINETASK element from Org to LaTeX.
1623 CONTENTS holds the contents of the block. INFO is a plist
1624 holding contextual information."
1625 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1626 (todo (and (plist-get info :with-todo-keywords)
1627 (let ((todo (org-element-property :todo-keyword inlinetask)))
1628 (and todo (org-export-data todo info)))))
1629 (todo-type (org-element-property :todo-type inlinetask))
1630 (tags (and (plist-get info :with-tags)
1631 (org-export-get-tags inlinetask info)))
1632 (priority (and (plist-get info :with-priority)
1633 (org-element-property :priority inlinetask))))
1634 ;; If `org-latex-format-inlinetask-function' is provided, call it
1635 ;; with appropriate arguments.
1636 (if (functionp org-latex-format-inlinetask-function)
1637 (funcall org-latex-format-inlinetask-function
1638 todo todo-type priority title tags contents)
1639 ;; Otherwise, use a default template.
1640 (org-latex--wrap-label
1641 inlinetask
1642 (let ((full-title
1643 (concat
1644 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1645 (when priority (format "\\framebox{\\#%c} " priority))
1646 title
1647 (when tags (format "\\hfill{}\\textsc{:%s:}"
1648 (mapconcat 'identity tags ":"))))))
1649 (format (concat "\\begin{center}\n"
1650 "\\fbox{\n"
1651 "\\begin{minipage}[c]{.6\\textwidth}\n"
1652 "%s\n\n"
1653 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1654 "%s"
1655 "\\end{minipage}\n"
1656 "}\n"
1657 "\\end{center}")
1658 full-title contents))))))
1661 ;;;; Italic
1663 (defun org-latex-italic (italic contents info)
1664 "Transcode ITALIC from Org to LaTeX.
1665 CONTENTS is the text with italic markup. INFO is a plist holding
1666 contextual information."
1667 (org-latex--text-markup contents 'italic))
1670 ;;;; Item
1672 (defun org-latex-item (item contents info)
1673 "Transcode an ITEM element from Org to LaTeX.
1674 CONTENTS holds the contents of the item. INFO is a plist holding
1675 contextual information."
1676 (let* ((counter
1677 (let ((count (org-element-property :counter item))
1678 (level
1679 ;; Determine level of current item to determine the
1680 ;; correct LaTeX counter to use (enumi, enumii...).
1681 (let ((parent item) (level 0))
1682 (while (memq (org-element-type
1683 (setq parent (org-export-get-parent parent)))
1684 '(plain-list item))
1685 (when (and (eq (org-element-type parent) 'plain-list)
1686 (eq (org-element-property :type parent)
1687 'ordered))
1688 (incf level)))
1689 level)))
1690 (and count
1691 (< level 5)
1692 (format "\\setcounter{enum%s}{%s}\n"
1693 (nth (1- level) '("i" "ii" "iii" "iv"))
1694 (1- count)))))
1695 (checkbox (case (org-element-property :checkbox item)
1696 (on "$\\boxtimes$ ")
1697 (off "$\\Box$ ")
1698 (trans "$\\boxminus$ ")))
1699 (tag (let ((tag (org-element-property :tag item)))
1700 ;; Check-boxes must belong to the tag.
1701 (and tag (format "[%s] "
1702 (concat checkbox
1703 (org-export-data tag info)))))))
1704 (concat counter "\\item" (or tag (concat " " checkbox))
1705 (and contents (org-trim contents))
1706 ;; If there are footnotes references in tag, be sure to
1707 ;; add their definition at the end of the item. This
1708 ;; workaround is necessary since "\footnote{}" command is
1709 ;; not supported in tags.
1710 (and tag
1711 (org-latex--delayed-footnotes-definitions
1712 (org-element-property :tag item) info)))))
1715 ;;;; Keyword
1717 (defun org-latex-keyword (keyword contents info)
1718 "Transcode a KEYWORD element from Org to LaTeX.
1719 CONTENTS is nil. INFO is a plist holding contextual information."
1720 (let ((key (org-element-property :key keyword))
1721 (value (org-element-property :value keyword)))
1722 (cond
1723 ((string= key "LATEX") value)
1724 ((string= key "INDEX") (format "\\index{%s}" value))
1725 ((string= key "TOC")
1726 (let ((value (downcase value)))
1727 (cond
1728 ((string-match "\\<headlines\\>" value)
1729 (let ((depth (or (and (string-match "[0-9]+" value)
1730 (string-to-number (match-string 0 value)))
1731 (plist-get info :with-toc))))
1732 (concat
1733 (when (wholenump depth)
1734 (format "\\setcounter{tocdepth}{%s}\n" depth))
1735 "\\tableofcontents")))
1736 ((string= "tables" value) "\\listoftables")
1737 ((string= "listings" value)
1738 (cond
1739 ((eq org-latex-listings 'minted) "\\listoflistings")
1740 (org-latex-listings "\\lstlistoflistings")
1741 ;; At the moment, src blocks with a caption are wrapped
1742 ;; into a figure environment.
1743 (t "\\listoffigures")))))))))
1746 ;;;; Latex Environment
1748 (defun org-latex-latex-environment (latex-environment contents info)
1749 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1750 CONTENTS is nil. INFO is a plist holding contextual information."
1751 (when (plist-get info :with-latex)
1752 (let ((label (org-element-property :name latex-environment))
1753 (value (org-remove-indentation
1754 (org-element-property :value latex-environment))))
1755 (if (not (org-string-nw-p label)) value
1756 ;; Environment is labelled: label must be within the environment
1757 ;; (otherwise, a reference pointing to that element will count
1758 ;; the section instead).
1759 (with-temp-buffer
1760 (insert value)
1761 (goto-char (point-min))
1762 (forward-line)
1763 (insert
1764 (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1765 (buffer-string))))))
1768 ;;;; Latex Fragment
1770 (defun org-latex-latex-fragment (latex-fragment contents info)
1771 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1772 CONTENTS is nil. INFO is a plist holding contextual information."
1773 (when (plist-get info :with-latex)
1774 (org-element-property :value latex-fragment)))
1777 ;;;; Line Break
1779 (defun org-latex-line-break (line-break contents info)
1780 "Transcode a LINE-BREAK object from Org to LaTeX.
1781 CONTENTS is nil. INFO is a plist holding contextual information."
1782 "\\\\\n")
1785 ;;;; Link
1787 (defun org-latex--inline-image (link info)
1788 "Return LaTeX code for an inline image.
1789 LINK is the link pointing to the inline image. INFO is a plist
1790 used as a communication channel."
1791 (let* ((parent (org-export-get-parent-element link))
1792 (path (let ((raw-path (org-element-property :path link)))
1793 (if (not (file-name-absolute-p raw-path)) raw-path
1794 (expand-file-name raw-path))))
1795 (filetype (file-name-extension path))
1796 (caption (org-latex--caption/label-string parent info))
1797 ;; Retrieve latex attributes from the element around.
1798 (attr (org-export-read-attribute :attr_latex parent))
1799 (float (let ((float (plist-get attr :float)))
1800 (cond ((string= float "wrap") 'wrap)
1801 ((string= float "multicolumn") 'multicolumn)
1802 ((or (string= float "figure")
1803 (org-element-property :caption parent))
1804 'figure))))
1805 (placement
1806 (let ((place (plist-get attr :placement)))
1807 (cond (place (format "%s" place))
1808 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1809 ((eq float 'figure)
1810 (format "[%s]" org-latex-default-figure-position))
1811 (t ""))))
1812 (comment-include (if (plist-get attr :comment-include) "%" ""))
1813 ;; It is possible to specify width and height in the
1814 ;; ATTR_LATEX line, and also via default variables.
1815 (width (cond ((plist-get attr :width))
1816 ((plist-get attr :height) "")
1817 ((eq float 'wrap) "0.48\\textwidth")
1818 (t org-latex-image-default-width)))
1819 (height (cond ((plist-get attr :height))
1820 ((or (plist-get attr :width)
1821 (memq float '(figure wrap))) "")
1822 (t org-latex-image-default-height)))
1823 (options (let ((opt (or (plist-get attr :options)
1824 org-latex-image-default-option)))
1825 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
1826 (match-string 1 opt))))
1827 image-code)
1828 (if (equal filetype "tikz")
1829 ;; For tikz images:
1830 ;; - use \input to read in image file.
1831 ;; - if options are present, wrap in a tikzpicture environment.
1832 ;; - if width or height are present, use \resizebox to change
1833 ;; the image size.
1834 (progn
1835 (setq image-code (format "\\input{%s}" path))
1836 (when (org-string-nw-p options)
1837 (setq image-code
1838 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1839 options
1840 image-code)))
1841 (when (or (org-string-nw-p width) (org-string-nw-p height))
1842 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
1843 (if (org-string-nw-p width) width "!")
1844 (if (org-string-nw-p height) height "!")
1845 image-code))))
1846 ;; For other images:
1847 ;; - add width and height to options.
1848 ;; - include the image with \includegraphics.
1849 (when (org-string-nw-p width)
1850 (setq options (concat options ",width=" width)))
1851 (when (org-string-nw-p height)
1852 (setq options (concat options ",height=" height)))
1853 (setq image-code
1854 (format "\\includegraphics%s{%s}"
1855 (cond ((not (org-string-nw-p options)) "")
1856 ((= (aref options 0) ?,)
1857 (format "[%s]"(substring options 1)))
1858 (t (format "[%s]" options)))
1859 path)))
1860 ;; Return proper string, depending on FLOAT.
1861 (case float
1862 (wrap (format "\\begin{wrapfigure}%s
1863 \\centering
1864 %s%s
1865 %s\\end{wrapfigure}" placement comment-include image-code caption))
1866 (multicolumn (format "\\begin{figure*}%s
1867 \\centering
1868 %s%s
1869 %s\\end{figure*}" placement comment-include image-code caption))
1870 (figure (format "\\begin{figure}%s
1871 \\centering
1872 %s%s
1873 %s\\end{figure}" placement comment-include image-code caption))
1874 (otherwise image-code))))
1876 (defun org-latex-link (link desc info)
1877 "Transcode a LINK object from Org to LaTeX.
1879 DESC is the description part of the link, or the empty string.
1880 INFO is a plist holding contextual information. See
1881 `org-export-data'."
1882 (let* ((type (org-element-property :type link))
1883 (raw-path (org-element-property :path link))
1884 ;; Ensure DESC really exists, or set it to nil.
1885 (desc (and (not (string= desc "")) desc))
1886 (imagep (org-export-inline-image-p
1887 link org-latex-inline-image-rules))
1888 (path (cond
1889 ((member type '("http" "https" "ftp" "mailto"))
1890 (concat type ":" raw-path))
1891 ((string= type "file")
1892 (if (not (file-name-absolute-p raw-path)) raw-path
1893 (concat "file://" (expand-file-name raw-path))))
1894 (t raw-path)))
1895 protocol)
1896 (cond
1897 ;; Image file.
1898 (imagep (org-latex--inline-image link info))
1899 ;; Radio link: Transcode target's contents and use them as link's
1900 ;; description.
1901 ((string= type "radio")
1902 (let ((destination (org-export-resolve-radio-link link info)))
1903 (when destination
1904 (format "\\hyperref[%s]{%s}"
1905 (org-export-solidify-link-text path)
1906 (org-export-data (org-element-contents destination) info)))))
1907 ;; Links pointing to a headline: Find destination and build
1908 ;; appropriate referencing command.
1909 ((member type '("custom-id" "fuzzy" "id"))
1910 (let ((destination (if (string= type "fuzzy")
1911 (org-export-resolve-fuzzy-link link info)
1912 (org-export-resolve-id-link link info))))
1913 (case (org-element-type destination)
1914 ;; Id link points to an external file.
1915 (plain-text
1916 (if desc (format "\\href{%s}{%s}" destination desc)
1917 (format "\\url{%s}" destination)))
1918 ;; Fuzzy link points nowhere.
1919 ('nil
1920 (format org-latex-link-with-unknown-path-format
1921 (or desc
1922 (org-export-data
1923 (org-element-property :raw-link link) info))))
1924 ;; LINK points to a headline. If headlines are numbered
1925 ;; and the link has no description, display headline's
1926 ;; number. Otherwise, display description or headline's
1927 ;; title.
1928 (headline
1929 (let ((label
1930 (format "sec-%s"
1931 (mapconcat
1932 'number-to-string
1933 (org-export-get-headline-number destination info)
1934 "-"))))
1935 (if (and (plist-get info :section-numbers) (not desc))
1936 (format "\\ref{%s}" label)
1937 (format "\\hyperref[%s]{%s}" label
1938 (or desc
1939 (org-export-data
1940 (org-element-property :title destination) info))))))
1941 ;; Fuzzy link points to a target. Do as above.
1942 (otherwise
1943 (let ((path (org-export-solidify-link-text path)))
1944 (if (not desc) (format "\\ref{%s}" path)
1945 (format "\\hyperref[%s]{%s}" path desc)))))))
1946 ;; Coderef: replace link with the reference name or the
1947 ;; equivalent line number.
1948 ((string= type "coderef")
1949 (format (org-export-get-coderef-format path desc)
1950 (org-export-resolve-coderef path info)))
1951 ;; Link type is handled by a special function.
1952 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1953 (funcall protocol (org-link-unescape path) desc 'latex))
1954 ;; External link with a description part.
1955 ((and path desc) (format "\\href{%s}{%s}" path desc))
1956 ;; External link without a description part.
1957 (path (format "\\url{%s}" path))
1958 ;; No path, only description. Try to do something useful.
1959 (t (format org-latex-link-with-unknown-path-format desc)))))
1962 ;;;; Paragraph
1964 (defun org-latex-paragraph (paragraph contents info)
1965 "Transcode a PARAGRAPH element from Org to LaTeX.
1966 CONTENTS is the contents of the paragraph, as a string. INFO is
1967 the plist used as a communication channel."
1968 contents)
1971 ;;;; Plain List
1973 (defun org-latex-plain-list (plain-list contents info)
1974 "Transcode a PLAIN-LIST element from Org to LaTeX.
1975 CONTENTS is the contents of the list. INFO is a plist holding
1976 contextual information."
1977 (let* ((type (org-element-property :type plain-list))
1978 (attr (org-export-read-attribute :attr_latex plain-list))
1979 (latex-type (let ((env (plist-get attr :environment)))
1980 (cond (env (format "%s" env))
1981 ((eq type 'ordered) "enumerate")
1982 ((eq type 'unordered) "itemize")
1983 ((eq type 'descriptive) "description")))))
1984 (org-latex--wrap-label
1985 plain-list
1986 (format "\\begin{%s}%s\n%s\\end{%s}"
1987 latex-type
1988 ;; Put optional arguments, if any inside square brackets
1989 ;; when necessary.
1990 (let ((options (format "%s" (or (plist-get attr :options) ""))))
1991 (cond ((equal options "") "")
1992 ((string-match "\\`\\[.*\\]\\'" options) options)
1993 (t (format "[%s]" options))))
1994 contents
1995 latex-type))))
1998 ;;;; Plain Text
2000 (defun org-latex-plain-text (text info)
2001 "Transcode a TEXT string from Org to LaTeX.
2002 TEXT is the string to transcode. INFO is a plist holding
2003 contextual information."
2004 (let ((specialp (plist-get info :with-special-strings))
2005 (output text))
2006 ;; Protect %, #, &, $, ^, _, { and }.
2007 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}^_]\\)" output)
2008 (setq output
2009 (replace-match
2010 (format "\\%s" (match-string 2 output)) nil t output 2)))
2011 ;; Protect \. If special strings are used, be careful not to
2012 ;; protect "\" in "\-" constructs.
2013 (let ((symbols (if specialp "-%$#&{}^_\\" "%$#&{}^_\\")))
2014 (setq output
2015 (replace-regexp-in-string
2016 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
2017 "$\\backslash$" output nil t 1)))
2018 ;; Protect ~.
2019 (setq output
2020 (replace-regexp-in-string
2021 "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t 2))
2022 ;; Activate smart quotes. Be sure to provide original TEXT string
2023 ;; since OUTPUT may have been modified.
2024 (when (plist-get info :with-smart-quotes)
2025 (setq output (org-export-activate-smart-quotes output :latex info text)))
2026 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
2027 (let ((case-fold-search nil)
2028 (start 0))
2029 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
2030 (setq output (replace-match
2031 (format "\\%s{}" (match-string 1 output)) nil t output)
2032 start (match-end 0))))
2033 ;; Convert special strings.
2034 (when specialp
2035 (setq output
2036 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
2037 ;; Handle break preservation if required.
2038 (when (plist-get info :preserve-breaks)
2039 (setq output (replace-regexp-in-string
2040 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
2041 ;; Return value.
2042 output))
2045 ;;;; Planning
2047 (defun org-latex-planning (planning contents info)
2048 "Transcode a PLANNING element from Org to LaTeX.
2049 CONTENTS is nil. INFO is a plist holding contextual
2050 information."
2051 (concat
2052 "\\noindent"
2053 (mapconcat
2054 'identity
2055 (delq nil
2056 (list
2057 (let ((closed (org-element-property :closed planning)))
2058 (when closed
2059 (concat
2060 (format "\\textbf{%s} " org-closed-string)
2061 (format org-latex-inactive-timestamp-format
2062 (org-translate-time
2063 (org-element-property :raw-value closed))))))
2064 (let ((deadline (org-element-property :deadline planning)))
2065 (when deadline
2066 (concat
2067 (format "\\textbf{%s} " org-deadline-string)
2068 (format org-latex-active-timestamp-format
2069 (org-translate-time
2070 (org-element-property :raw-value deadline))))))
2071 (let ((scheduled (org-element-property :scheduled planning)))
2072 (when scheduled
2073 (concat
2074 (format "\\textbf{%s} " org-scheduled-string)
2075 (format org-latex-active-timestamp-format
2076 (org-translate-time
2077 (org-element-property :raw-value scheduled))))))))
2078 " ")
2079 "\\\\"))
2082 ;;;; Quote Block
2084 (defun org-latex-quote-block (quote-block contents info)
2085 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
2086 CONTENTS holds the contents of the block. INFO is a plist
2087 holding contextual information."
2088 (org-latex--wrap-label
2089 quote-block
2090 (format "\\begin{quote}\n%s\\end{quote}" contents)))
2093 ;;;; Quote Section
2095 (defun org-latex-quote-section (quote-section contents info)
2096 "Transcode a QUOTE-SECTION element from Org to LaTeX.
2097 CONTENTS is nil. INFO is a plist holding contextual information."
2098 (let ((value (org-remove-indentation
2099 (org-element-property :value quote-section))))
2100 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
2103 ;;;; Radio Target
2105 (defun org-latex-radio-target (radio-target text info)
2106 "Transcode a RADIO-TARGET object from Org to LaTeX.
2107 TEXT is the text of the target. INFO is a plist holding
2108 contextual information."
2109 (format "\\label{%s}%s"
2110 (org-export-solidify-link-text
2111 (org-element-property :value radio-target))
2112 text))
2115 ;;;; Section
2117 (defun org-latex-section (section contents info)
2118 "Transcode a SECTION element from Org to LaTeX.
2119 CONTENTS holds the contents of the section. INFO is a plist
2120 holding contextual information."
2121 contents)
2124 ;;;; Special Block
2126 (defun org-latex-special-block (special-block contents info)
2127 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2128 CONTENTS holds the contents of the block. INFO is a plist
2129 holding contextual information."
2130 (let ((type (downcase (org-element-property :type special-block)))
2131 (opt (org-export-read-attribute :attr_latex special-block :options)))
2132 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2133 ;; Insert any label or caption within the block
2134 ;; (otherwise, a reference pointing to that element will
2135 ;; count the section instead).
2136 (org-latex--caption/label-string special-block info)
2137 contents
2138 (format "\\end{%s}" type))))
2141 ;;;; Src Block
2143 (defun org-latex-src-block (src-block contents info)
2144 "Transcode a SRC-BLOCK element from Org to LaTeX.
2145 CONTENTS holds the contents of the item. INFO is a plist holding
2146 contextual information."
2147 (when (org-string-nw-p (org-element-property :value src-block))
2148 (let* ((lang (org-element-property :language src-block))
2149 (caption (org-element-property :caption src-block))
2150 (label (org-element-property :name src-block))
2151 (custom-env (and lang
2152 (cadr (assq (intern lang)
2153 org-latex-custom-lang-environments))))
2154 (num-start (case (org-element-property :number-lines src-block)
2155 (continued (org-export-get-loc src-block info))
2156 (new 0)))
2157 (retain-labels (org-element-property :retain-labels src-block))
2158 (long-listing
2159 (let ((attr (org-export-read-attribute :attr_latex src-block)))
2160 (if (plist-member attr :long-listing)
2161 (plist-get attr :long-listing)
2162 org-latex-long-listings))))
2163 (cond
2164 ;; Case 1. No source fontification.
2165 ((not org-latex-listings)
2166 (let* ((caption-str (org-latex--caption/label-string src-block info))
2167 (float-env (and (not long-listing)
2168 (or label caption)
2169 (format "\\begin{figure}[H]\n%s%%s\n\\end{figure}"
2170 caption-str))))
2171 (format
2172 (or float-env "%s")
2173 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2174 (org-export-format-code-default src-block info))))))
2175 ;; Case 2. Custom environment.
2176 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2177 custom-env
2178 (org-export-format-code-default src-block info)
2179 custom-env))
2180 ;; Case 3. Use minted package.
2181 ((eq org-latex-listings 'minted)
2182 (let ((float-env
2183 (and (not long-listing)
2184 (or label caption)
2185 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2186 (org-latex--caption/label-string src-block info))))
2187 (body
2188 (format
2189 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2190 ;; Options.
2191 (org-latex--make-option-string
2192 (if (or (not num-start)
2193 (assoc "linenos" org-latex-minted-options))
2194 org-latex-minted-options
2195 (append `(("linenos")
2196 ("firstnumber" ,(number-to-string (1+ num-start))))
2197 org-latex-minted-options)))
2198 ;; Language.
2199 (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
2200 ;; Source code.
2201 (let* ((code-info (org-export-unravel-code src-block))
2202 (max-width
2203 (apply 'max
2204 (mapcar 'length
2205 (org-split-string (car code-info)
2206 "\n")))))
2207 (org-export-format-code
2208 (car code-info)
2209 (lambda (loc num ref)
2210 (concat
2212 (when ref
2213 ;; Ensure references are flushed to the right,
2214 ;; separated with 6 spaces from the widest line
2215 ;; of code.
2216 (concat (make-string (+ (- max-width (length loc)) 6)
2217 ?\s)
2218 (format "(%s)" ref)))))
2219 nil (and retain-labels (cdr code-info)))))))
2220 ;; Return value.
2221 (if float-env (format float-env body) body)))
2222 ;; Case 4. Use listings package.
2224 (let ((lst-lang
2225 (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
2226 (caption-str
2227 (when caption
2228 (let ((main (org-export-get-caption src-block))
2229 (secondary (org-export-get-caption src-block t)))
2230 (if (not secondary)
2231 (format "{%s}" (org-export-data main info))
2232 (format "{[%s]%s}"
2233 (org-export-data secondary info)
2234 (org-export-data main info)))))))
2235 (concat
2236 ;; Options.
2237 (format "\\lstset{%s}\n"
2238 (org-latex--make-option-string
2239 (append
2240 org-latex-listings-options
2241 `(("language" ,lst-lang))
2242 (when label `(("label" ,label)))
2243 (when caption-str `(("caption" ,caption-str)))
2244 (cond ((assoc "numbers" org-latex-listings-options) nil)
2245 ((not num-start) '(("numbers" "none")))
2246 ((zerop num-start) '(("numbers" "left")))
2247 (t `(("numbers" "left")
2248 ("firstnumber"
2249 ,(number-to-string (1+ num-start)))))))))
2250 ;; Source code.
2251 (format
2252 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2253 (let* ((code-info (org-export-unravel-code src-block))
2254 (max-width
2255 (apply 'max
2256 (mapcar 'length
2257 (org-split-string (car code-info) "\n")))))
2258 (org-export-format-code
2259 (car code-info)
2260 (lambda (loc num ref)
2261 (concat
2263 (when ref
2264 ;; Ensure references are flushed to the right,
2265 ;; separated with 6 spaces from the widest line of
2266 ;; code
2267 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2268 (format "(%s)" ref)))))
2269 nil (and retain-labels (cdr code-info))))))))))))
2272 ;;;; Statistics Cookie
2274 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2275 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2276 CONTENTS is nil. INFO is a plist holding contextual information."
2277 (replace-regexp-in-string
2278 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2281 ;;;; Strike-Through
2283 (defun org-latex-strike-through (strike-through contents info)
2284 "Transcode STRIKE-THROUGH from Org to LaTeX.
2285 CONTENTS is the text with strike-through markup. INFO is a plist
2286 holding contextual information."
2287 (org-latex--text-markup contents 'strike-through))
2290 ;;;; Subscript
2292 (defun org-latex-subscript (subscript contents info)
2293 "Transcode a SUBSCRIPT object from Org to LaTeX.
2294 CONTENTS is the contents of the object. INFO is a plist holding
2295 contextual information."
2296 (if (= (length contents) 1) (format "$_%s$" contents)
2297 ;; Handle multiple objects in SUBSCRIPT by creating a subscript
2298 ;; command for each of them.
2299 (let ((prev-blanks 0))
2300 (mapconcat
2301 (lambda (obj)
2302 (case (org-element-type obj)
2303 ((entity latex-fragment)
2304 (setq prev-blanks (org-element-property :post-blank obj))
2305 (let ((data (org-trim (org-export-data obj info))))
2306 (string-match
2307 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2308 data)
2309 (format "$_{%s}$" (match-string 1 data))))
2310 (plain-text
2311 (format "$_\\mathrm{%s}$"
2312 (concat (make-string prev-blanks ? )
2313 ;; mathrm command doesn't handle spaces,
2314 ;; so we have to enforce them.
2315 (replace-regexp-in-string
2316 " " "\\\\ " (org-export-data obj info)))))
2317 (otherwise
2318 (setq prev-blanks (org-element-property :post-blank obj))
2319 (format "$_{%s}$" (org-export-data obj info)))))
2320 (org-element-contents subscript) ""))))
2323 ;;;; Superscript
2325 (defun org-latex-superscript (superscript contents info)
2326 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2327 CONTENTS is the contents of the object. INFO is a plist holding
2328 contextual information."
2329 (if (= (length contents) 1) (format "$^%s$" contents)
2330 ;; Handle multiple objects in SUPERSCRIPT by creating
2331 ;; a superscript command for each of them.
2332 (let ((prev-blanks 0))
2333 (mapconcat
2334 (lambda (obj)
2335 (case (org-element-type obj)
2336 ((entity latex-fragment)
2337 (setq prev-blanks (org-element-property :post-blank obj))
2338 (let ((data (org-trim (org-export-data obj info))))
2339 (string-match
2340 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2341 data)
2342 (format "$^{%s}$" (match-string 1 data))))
2343 (plain-text
2344 (format "$^\\mathrm{%s}$"
2345 (concat (make-string prev-blanks ? )
2346 ;; mathrm command doesn't handle spaces,
2347 ;; so we have to enforce them.
2348 (replace-regexp-in-string
2349 " " "\\\\ " (org-export-data obj info)))))
2350 (otherwise
2351 (setq prev-blanks (org-element-property :post-blank obj))
2352 (format "$^{%s}$" (org-export-data obj info)))))
2353 (org-element-contents superscript) ""))))
2356 ;;;; Table
2358 ;; `org-latex-table' is the entry point for table transcoding. It
2359 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2360 ;; delegates the job to either `org-latex--table.el-table',
2361 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2362 ;; depending of the type of the table and the mode requested.
2364 ;; `org-latex--align-string' is a subroutine used to build alignment
2365 ;; string for Org tables.
2367 (defun org-latex-table (table contents info)
2368 "Transcode a TABLE element from Org to LaTeX.
2369 CONTENTS is the contents of the table. INFO is a plist holding
2370 contextual information."
2371 (if (eq (org-element-property :type table) 'table.el)
2372 ;; "table.el" table. Convert it using appropriate tools.
2373 (org-latex--table.el-table table info)
2374 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2375 org-latex-default-table-mode)))
2376 (cond
2377 ;; Case 1: Verbatim table.
2378 ((string= type "verbatim")
2379 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2380 ;; Re-create table, without affiliated keywords.
2381 (org-trim (org-element-interpret-data
2382 `(table nil ,@(org-element-contents table))))))
2383 ;; Case 2: Matrix.
2384 ((or (string= type "math") (string= type "inline-math"))
2385 (org-latex--math-table table info))
2386 ;; Case 3: Standard table.
2387 (t (concat (org-latex--org-table table contents info)
2388 ;; When there are footnote references within the
2389 ;; table, insert their definition just after it.
2390 (org-latex--delayed-footnotes-definitions table info)))))))
2392 (defun org-latex--align-string (table info)
2393 "Return an appropriate LaTeX alignment string.
2394 TABLE is the considered table. INFO is a plist used as
2395 a communication channel."
2396 (or (org-export-read-attribute :attr_latex table :align)
2397 (let (align)
2398 ;; Extract column groups and alignment from first (non-rule)
2399 ;; row.
2400 (org-element-map
2401 (org-element-map table 'table-row
2402 (lambda (row)
2403 (and (eq (org-element-property :type row) 'standard) row))
2404 info 'first-match)
2405 'table-cell
2406 (lambda (cell)
2407 (let ((borders (org-export-table-cell-borders cell info)))
2408 ;; Check left border for the first cell only.
2409 (when (and (memq 'left borders) (not align))
2410 (push "|" align))
2411 (push (case (org-export-table-cell-alignment cell info)
2412 (left "l")
2413 (right "r")
2414 (center "c"))
2415 align)
2416 (when (memq 'right borders) (push "|" align))))
2417 info)
2418 (apply 'concat (nreverse align)))))
2420 (defun org-latex--org-table (table contents info)
2421 "Return appropriate LaTeX code for an Org table.
2423 TABLE is the table type element to transcode. CONTENTS is its
2424 contents, as a string. INFO is a plist used as a communication
2425 channel.
2427 This function assumes TABLE has `org' as its `:type' property and
2428 `table' as its `:mode' attribute."
2429 (let* ((caption (org-latex--caption/label-string table info))
2430 (attr (org-export-read-attribute :attr_latex table))
2431 ;; Determine alignment string.
2432 (alignment (org-latex--align-string table info))
2433 ;; Determine environment for the table: longtable, tabular...
2434 (table-env (or (plist-get attr :environment)
2435 org-latex-default-table-environment))
2436 ;; If table is a float, determine environment: table, table*
2437 ;; or sidewaystable.
2438 (float-env (unless (member table-env '("longtable" "longtabu"))
2439 (let ((float (plist-get attr :float)))
2440 (cond
2441 ((string= float "sidewaystable") "sidewaystable")
2442 ((string= float "multicolumn") "table*")
2443 ((or (string= float "table")
2444 (org-element-property :caption table))
2445 "table")))))
2446 ;; Extract others display options.
2447 (fontsize (let ((font (plist-get attr :font)))
2448 (and font (concat font "\n"))))
2449 (width (plist-get attr :width))
2450 (spreadp (plist-get attr :spread))
2451 (placement (or (plist-get attr :placement)
2452 (format "[%s]" org-latex-default-figure-position)))
2453 (centerp (if (plist-member attr :center) (plist-get attr :center)
2454 org-latex-tables-centered)))
2455 ;; Prepare the final format string for the table.
2456 (cond
2457 ;; Longtable.
2458 ((equal "longtable" table-env)
2459 (concat (and fontsize (concat "{" fontsize))
2460 (format "\\begin{longtable}{%s}\n" alignment)
2461 (and org-latex-table-caption-above
2462 (org-string-nw-p caption)
2463 (concat caption "\\\\\n"))
2464 contents
2465 (and (not org-latex-table-caption-above)
2466 (org-string-nw-p caption)
2467 (concat caption "\\\\\n"))
2468 "\\end{longtable}\n"
2469 (and fontsize "}")))
2470 ;; Longtabu
2471 ((equal "longtabu" table-env)
2472 (concat (and fontsize (concat "{" fontsize))
2473 (format "\\begin{longtabu}%s{%s}\n"
2474 (if width
2475 (format " %s %s "
2476 (if spreadp "spread" "to") width) "")
2477 alignment)
2478 (and org-latex-table-caption-above
2479 (org-string-nw-p caption)
2480 (concat caption "\\\\\n"))
2481 contents
2482 (and (not org-latex-table-caption-above)
2483 (org-string-nw-p caption)
2484 (concat caption "\\\\\n"))
2485 "\\end{longtabu}\n"
2486 (and fontsize "}")))
2487 ;; Others.
2488 (t (concat (cond
2489 (float-env
2490 (concat (format "\\begin{%s}%s\n" float-env placement)
2491 (if org-latex-table-caption-above caption "")
2492 (when centerp "\\centering\n")
2493 fontsize))
2494 (centerp (concat "\\begin{center}\n" fontsize))
2495 (fontsize (concat "{" fontsize)))
2496 (cond ((equal "tabu" table-env)
2497 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2498 (if width (format
2499 (if spreadp " spread %s " " to %s ")
2500 width) "")
2501 alignment
2502 contents))
2503 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2504 table-env
2505 (if width (format "{%s}" width) "")
2506 alignment
2507 contents
2508 table-env)))
2509 (cond
2510 (float-env
2511 (concat (if org-latex-table-caption-above "" caption)
2512 (format "\n\\end{%s}" float-env)))
2513 (centerp "\n\\end{center}")
2514 (fontsize "}")))))))
2516 (defun org-latex--table.el-table (table info)
2517 "Return appropriate LaTeX code for a table.el table.
2519 TABLE is the table type element to transcode. INFO is a plist
2520 used as a communication channel.
2522 This function assumes TABLE has `table.el' as its `:type'
2523 property."
2524 (require 'table)
2525 ;; Ensure "*org-export-table*" buffer is empty.
2526 (with-current-buffer (get-buffer-create "*org-export-table*")
2527 (erase-buffer))
2528 (let ((output (with-temp-buffer
2529 (insert (org-element-property :value table))
2530 (goto-char 1)
2531 (re-search-forward "^[ \t]*|[^|]" nil t)
2532 (table-generate-source 'latex "*org-export-table*")
2533 (with-current-buffer "*org-export-table*"
2534 (org-trim (buffer-string))))))
2535 (kill-buffer (get-buffer "*org-export-table*"))
2536 ;; Remove left out comments.
2537 (while (string-match "^%.*\n" output)
2538 (setq output (replace-match "" t t output)))
2539 (let ((attr (org-export-read-attribute :attr_latex table)))
2540 (when (plist-get attr :rmlines)
2541 ;; When the "rmlines" attribute is provided, remove all hlines
2542 ;; but the the one separating heading from the table body.
2543 (let ((n 0) (pos 0))
2544 (while (and (< (length output) pos)
2545 (setq pos (string-match "^\\\\hline\n?" output pos)))
2546 (incf n)
2547 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2548 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2549 org-latex-tables-centered)))
2550 (if (not centerp) output
2551 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2553 (defun org-latex--math-table (table info)
2554 "Return appropriate LaTeX code for a matrix.
2556 TABLE is the table type element to transcode. INFO is a plist
2557 used as a communication channel.
2559 This function assumes TABLE has `org' as its `:type' property and
2560 `inline-math' or `math' as its `:mode' attribute.."
2561 (let* ((caption (org-latex--caption/label-string table info))
2562 (attr (org-export-read-attribute :attr_latex table))
2563 (inlinep (equal (plist-get attr :mode) "inline-math"))
2564 (env (or (plist-get attr :environment)
2565 org-latex-default-table-environment))
2566 (contents
2567 (mapconcat
2568 (lambda (row)
2569 ;; Ignore horizontal rules.
2570 (when (eq (org-element-property :type row) 'standard)
2571 ;; Return each cell unmodified.
2572 (concat
2573 (mapconcat
2574 (lambda (cell)
2575 (substring (org-element-interpret-data cell) 0 -1))
2576 (org-element-map row 'table-cell 'identity info) "&")
2577 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2578 "\n")))
2579 (org-element-map table 'table-row 'identity info) ""))
2580 ;; Variables related to math clusters (contiguous math tables
2581 ;; of the same type).
2582 (mode (org-export-read-attribute :attr_latex table :mode))
2583 (prev (org-export-get-previous-element table info))
2584 (next (org-export-get-next-element table info))
2585 (same-mode-p
2586 (lambda (table)
2587 ;; Non-nil when TABLE has the same mode as current table.
2588 (string= (or (org-export-read-attribute :attr_latex table :mode)
2589 org-latex-default-table-mode)
2590 mode))))
2591 (concat
2592 ;; Opening string. If TABLE is in the middle of a table cluster,
2593 ;; do not insert any.
2594 (cond ((and prev
2595 (eq (org-element-type prev) 'table)
2596 (memq (org-element-property :post-blank prev) '(0 nil))
2597 (funcall same-mode-p prev))
2598 nil)
2599 (inlinep "\\(")
2600 ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
2601 (t "\\["))
2602 ;; Prefix.
2603 (or (plist-get attr :math-prefix) "")
2604 ;; Environment. Also treat special cases.
2605 (cond ((equal env "array")
2606 (let ((align (org-latex--align-string table info)))
2607 (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
2608 ((assoc env org-latex-table-matrix-macros)
2609 (format "\\%s%s{\n%s}"
2611 (or (plist-get attr :math-arguments) "")
2612 contents))
2613 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2614 ;; Suffix.
2615 (or (plist-get attr :math-suffix) "")
2616 ;; Closing string. If TABLE is in the middle of a table cluster,
2617 ;; do not insert any. If it closes such a cluster, be sure to
2618 ;; close the cluster with a string matching the opening string.
2619 (cond ((and next
2620 (eq (org-element-type next) 'table)
2621 (memq (org-element-property :post-blank table) '(0 nil))
2622 (funcall same-mode-p next))
2623 nil)
2624 (inlinep "\\)")
2625 ;; Find cluster beginning to know which environment to use.
2626 ((let ((cluster-beg table) prev)
2627 (while (and (setq prev (org-export-get-previous-element
2628 cluster-beg info))
2629 (memq (org-element-property :post-blank prev)
2630 '(0 nil))
2631 (funcall same-mode-p prev))
2632 (setq cluster-beg prev))
2633 (and (or (org-element-property :caption cluster-beg)
2634 (org-element-property :name cluster-beg))
2635 "\n\\end{equation}")))
2636 (t "\\]")))))
2639 ;;;; Table Cell
2641 (defun org-latex-table-cell (table-cell contents info)
2642 "Transcode a TABLE-CELL element from Org to LaTeX.
2643 CONTENTS is the cell contents. INFO is a plist used as
2644 a communication channel."
2645 (concat (if (and contents
2646 org-latex-table-scientific-notation
2647 (string-match orgtbl-exp-regexp contents))
2648 ;; Use appropriate format string for scientific
2649 ;; notation.
2650 (format org-latex-table-scientific-notation
2651 (match-string 1 contents)
2652 (match-string 2 contents))
2653 contents)
2654 (when (org-export-get-next-element table-cell info) " & ")))
2657 ;;;; Table Row
2659 (defun org-latex-table-row (table-row contents info)
2660 "Transcode a TABLE-ROW element from Org to LaTeX.
2661 CONTENTS is the contents of the row. INFO is a plist used as
2662 a communication channel."
2663 ;; Rules are ignored since table separators are deduced from
2664 ;; borders of the current row.
2665 (when (eq (org-element-property :type table-row) 'standard)
2666 (let* ((attr (org-export-read-attribute :attr_latex
2667 (org-export-get-parent table-row)))
2668 (longtablep (member (or (plist-get attr :environment)
2669 org-latex-default-table-environment)
2670 '("longtable" "longtabu")))
2671 (booktabsp (if (plist-member attr :booktabs)
2672 (plist-get attr :booktabs)
2673 org-latex-tables-booktabs))
2674 ;; TABLE-ROW's borders are extracted from its first cell.
2675 (borders (org-export-table-cell-borders
2676 (car (org-element-contents table-row)) info)))
2677 (concat
2678 ;; When BOOKTABS are activated enforce top-rule even when no
2679 ;; hline was specifically marked.
2680 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2681 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2682 contents "\\\\\n"
2683 (cond
2684 ;; Special case for long tables. Define header and footers.
2685 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2686 (format "%s
2687 \\endhead
2688 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2689 \\endfoot
2690 \\endlastfoot"
2691 (if booktabsp "\\midrule" "\\hline")
2692 (if booktabsp "\\midrule" "\\hline")
2693 ;; Number of columns.
2694 (cdr (org-export-table-dimensions
2695 (org-export-get-parent-table table-row) info))))
2696 ;; When BOOKTABS are activated enforce bottom rule even when
2697 ;; no hline was specifically marked.
2698 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2699 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2700 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2703 ;;;; Target
2705 (defun org-latex-target (target contents info)
2706 "Transcode a TARGET object from Org to LaTeX.
2707 CONTENTS is nil. INFO is a plist holding contextual
2708 information."
2709 (format "\\label{%s}"
2710 (org-export-solidify-link-text (org-element-property :value target))))
2713 ;;;; Timestamp
2715 (defun org-latex-timestamp (timestamp contents info)
2716 "Transcode a TIMESTAMP object from Org to LaTeX.
2717 CONTENTS is nil. INFO is a plist holding contextual
2718 information."
2719 (let ((value (org-latex-plain-text
2720 (org-timestamp-translate timestamp) info)))
2721 (case (org-element-property :type timestamp)
2722 ((active active-range) (format org-latex-active-timestamp-format value))
2723 ((inactive inactive-range)
2724 (format org-latex-inactive-timestamp-format value))
2725 (otherwise (format org-latex-diary-timestamp-format value)))))
2728 ;;;; Underline
2730 (defun org-latex-underline (underline contents info)
2731 "Transcode UNDERLINE from Org to LaTeX.
2732 CONTENTS is the text with underline markup. INFO is a plist
2733 holding contextual information."
2734 (org-latex--text-markup contents 'underline))
2737 ;;;; Verbatim
2739 (defun org-latex-verbatim (verbatim contents info)
2740 "Transcode a VERBATIM object from Org to LaTeX.
2741 CONTENTS is nil. INFO is a plist used as a communication
2742 channel."
2743 (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2746 ;;;; Verse Block
2748 (defun org-latex-verse-block (verse-block contents info)
2749 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2750 CONTENTS is verse block contents. INFO is a plist holding
2751 contextual information."
2752 (org-latex--wrap-label
2753 verse-block
2754 ;; In a verse environment, add a line break to each newline
2755 ;; character and change each white space at beginning of a line
2756 ;; into a space of 1 em. Also change each blank line with
2757 ;; a vertical space of 1 em.
2758 (progn
2759 (setq contents (replace-regexp-in-string
2760 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2761 (replace-regexp-in-string
2762 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2763 (while (string-match "^[ \t]+" contents)
2764 (let ((new-str (format "\\hspace*{%dem}"
2765 (length (match-string 0 contents)))))
2766 (setq contents (replace-match new-str nil t contents))))
2767 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2771 ;;; End-user functions
2773 ;;;###autoload
2774 (defun org-latex-export-as-latex
2775 (&optional async subtreep visible-only body-only ext-plist)
2776 "Export current buffer as a LaTeX buffer.
2778 If narrowing is active in the current buffer, only export its
2779 narrowed part.
2781 If a region is active, export that region.
2783 A non-nil optional argument ASYNC means the process should happen
2784 asynchronously. The resulting buffer should be accessible
2785 through the `org-export-stack' interface.
2787 When optional argument SUBTREEP is non-nil, export the sub-tree
2788 at point, extracting information from the headline properties
2789 first.
2791 When optional argument VISIBLE-ONLY is non-nil, don't export
2792 contents of hidden elements.
2794 When optional argument BODY-ONLY is non-nil, only write code
2795 between \"\\begin{document}\" and \"\\end{document}\".
2797 EXT-PLIST, when provided, is a property list with external
2798 parameters overriding Org default settings, but still inferior to
2799 file-local settings.
2801 Export is done in a buffer named \"*Org LATEX Export*\", which
2802 will be displayed when `org-export-show-temporary-export-buffer'
2803 is non-nil."
2804 (interactive)
2805 (if async
2806 (org-export-async-start
2807 (lambda (output)
2808 (with-current-buffer (get-buffer-create "*Org LATEX Export*")
2809 (erase-buffer)
2810 (insert output)
2811 (goto-char (point-min))
2812 (LaTeX-mode)
2813 (org-export-add-to-stack (current-buffer) 'latex)))
2814 `(org-export-as 'latex ,subtreep ,visible-only ,body-only
2815 ',ext-plist))
2816 (let ((outbuf
2817 (org-export-to-buffer 'latex "*Org LATEX Export*"
2818 subtreep visible-only body-only ext-plist)))
2819 (with-current-buffer outbuf (LaTeX-mode))
2820 (when org-export-show-temporary-export-buffer
2821 (switch-to-buffer-other-window outbuf)))))
2823 ;;;###autoload
2824 (defun org-latex-convert-region-to-latex ()
2825 "Assume the current region has org-mode syntax, and convert it to LaTeX.
2826 This can be used in any buffer. For example, you can write an
2827 itemized list in org-mode syntax in an LaTeX buffer and use this
2828 command to convert it."
2829 (interactive)
2830 (org-export-replace-region-by 'latex))
2832 ;;;###autoload
2833 (defun org-latex-export-to-latex
2834 (&optional async subtreep visible-only body-only ext-plist)
2835 "Export current buffer to a LaTeX file.
2837 If narrowing is active in the current buffer, only export its
2838 narrowed part.
2840 If a region is active, export that region.
2842 A non-nil optional argument ASYNC means the process should happen
2843 asynchronously. The resulting file should be accessible through
2844 the `org-export-stack' interface.
2846 When optional argument SUBTREEP is non-nil, export the sub-tree
2847 at point, extracting information from the headline properties
2848 first.
2850 When optional argument VISIBLE-ONLY is non-nil, don't export
2851 contents of hidden elements.
2853 When optional argument BODY-ONLY is non-nil, only write code
2854 between \"\\begin{document}\" and \"\\end{document}\".
2856 EXT-PLIST, when provided, is a property list with external
2857 parameters overriding Org default settings, but still inferior to
2858 file-local settings.
2860 Return output file's name."
2861 (interactive)
2862 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2863 (if async
2864 (org-export-async-start
2865 (lambda (f) (org-export-add-to-stack f 'latex))
2866 `(expand-file-name
2867 (org-export-to-file
2868 'latex ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))
2869 (org-export-to-file
2870 'latex outfile subtreep visible-only body-only ext-plist))))
2872 ;;;###autoload
2873 (defun org-latex-export-to-pdf
2874 (&optional async subtreep visible-only body-only ext-plist)
2875 "Export current buffer to LaTeX then process through to PDF.
2877 If narrowing is active in the current buffer, only export its
2878 narrowed part.
2880 If a region is active, export that region.
2882 A non-nil optional argument ASYNC means the process should happen
2883 asynchronously. The resulting file should be accessible through
2884 the `org-export-stack' interface.
2886 When optional argument SUBTREEP is non-nil, export the sub-tree
2887 at point, extracting information from the headline properties
2888 first.
2890 When optional argument VISIBLE-ONLY is non-nil, don't export
2891 contents of hidden elements.
2893 When optional argument BODY-ONLY is non-nil, only write code
2894 between \"\\begin{document}\" and \"\\end{document}\".
2896 EXT-PLIST, when provided, is a property list with external
2897 parameters overriding Org default settings, but still inferior to
2898 file-local settings.
2900 Return PDF file's name."
2901 (interactive)
2902 (if async
2903 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2904 (org-export-async-start
2905 (lambda (f) (org-export-add-to-stack f 'latex))
2906 `(expand-file-name
2907 (org-latex-compile
2908 (org-export-to-file
2909 'latex ,outfile ,subtreep ,visible-only ,body-only
2910 ',ext-plist)))))
2911 (org-latex-compile
2912 (org-latex-export-to-latex
2913 nil subtreep visible-only body-only ext-plist))))
2915 (defun org-latex-compile (texfile &optional snippet)
2916 "Compile a TeX file.
2918 TEXFILE is the name of the file being compiled. Processing is
2919 done through the command specified in `org-latex-pdf-process'.
2921 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
2922 file used to preview a LaTeX snippet. In this case, do not
2923 create a log buffer and do not bother removing log files.
2925 Return PDF file name or an error if it couldn't be produced."
2926 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
2927 (full-name (file-truename texfile))
2928 (out-dir (file-name-directory texfile))
2929 ;; Make sure `default-directory' is set to TEXFILE directory,
2930 ;; not to whatever value the current buffer may have.
2931 (default-directory (file-name-directory full-name))
2932 errors)
2933 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
2934 (save-window-excursion
2935 (cond
2936 ;; A function is provided: Apply it.
2937 ((functionp org-latex-pdf-process)
2938 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
2939 ;; A list is provided: Replace %b, %f and %o with appropriate
2940 ;; values in each command before applying it. Output is
2941 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2942 ((consp org-latex-pdf-process)
2943 (let ((outbuf (and (not snippet)
2944 (get-buffer-create "*Org PDF LaTeX Output*"))))
2945 (mapc
2946 (lambda (command)
2947 (shell-command
2948 (replace-regexp-in-string
2949 "%b" (shell-quote-argument base-name)
2950 (replace-regexp-in-string
2951 "%f" (shell-quote-argument full-name)
2952 (replace-regexp-in-string
2953 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2954 outbuf))
2955 org-latex-pdf-process)
2956 ;; Collect standard errors from output buffer.
2957 (setq errors (and (not snippet) (org-latex--collect-errors outbuf)))))
2958 (t (error "No valid command to process to PDF")))
2959 (let ((pdffile (concat out-dir base-name ".pdf")))
2960 ;; Check for process failure. Provide collected errors if
2961 ;; possible.
2962 (if (not (file-exists-p pdffile))
2963 (error (concat (format "PDF file %s wasn't produced" pdffile)
2964 (when errors (concat ": " errors))))
2965 ;; Else remove log files, when specified, and signal end of
2966 ;; process to user, along with any error encountered.
2967 (when (and (not snippet) org-latex-remove-logfiles)
2968 (dolist (ext org-latex-logfiles-extensions)
2969 (let ((file (concat out-dir base-name "." ext)))
2970 (when (file-exists-p file) (delete-file file)))))
2971 (message (concat "Process completed"
2972 (if (not errors) "."
2973 (concat " with errors: " errors)))))
2974 ;; Return output file name.
2975 pdffile))))
2977 (defun org-latex--collect-errors (buffer)
2978 "Collect some kind of errors from \"pdflatex\" command output.
2980 BUFFER is the buffer containing output.
2982 Return collected error types as a string, or nil if there was
2983 none."
2984 (with-current-buffer buffer
2985 (save-excursion
2986 (goto-char (point-max))
2987 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
2988 (let ((case-fold-search t)
2989 (errors ""))
2990 (dolist (latex-error org-latex-known-errors)
2991 (when (save-excursion (re-search-forward (car latex-error) nil t))
2992 (setq errors (concat errors " " (cdr latex-error)))))
2993 (and (org-string-nw-p errors) (org-trim errors)))))))
2995 ;;;###autoload
2996 (defun org-latex-publish-to-latex (plist filename pub-dir)
2997 "Publish an Org file to LaTeX.
2999 FILENAME is the filename of the Org file to be published. PLIST
3000 is the property list for the given project. PUB-DIR is the
3001 publishing directory.
3003 Return output file name."
3004 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
3006 ;;;###autoload
3007 (defun org-latex-publish-to-pdf (plist filename pub-dir)
3008 "Publish an Org file to PDF (via LaTeX).
3010 FILENAME is the filename of the Org file to be published. PLIST
3011 is the property list for the given project. PUB-DIR is the
3012 publishing directory.
3014 Return output file name."
3015 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
3016 ;; in working directory and then moved to publishing directory.
3017 (org-publish-attachment
3018 plist
3019 (org-latex-compile (org-publish-org-to 'latex filename ".tex" plist))
3020 pub-dir))
3023 (provide 'ox-latex)
3025 ;; Local variables:
3026 ;; generated-autoload-file: "org-loaddefs.el"
3027 ;; End:
3029 ;;; ox-latex.el ends here