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