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