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