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