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