org: fix commit 0911edfac8
[org-mode.git] / lisp / ox-latex.el
blob3faeb5340e52f8229b8bb72f144ec444cf7e44a0
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 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; See Org manual for details.
27 ;;; Code:
29 (eval-when-compile (require 'cl))
30 (require 'ox)
31 (require 'ox-publish)
33 (defvar org-latex-default-packages-alist)
34 (defvar org-latex-packages-alist)
35 (defvar orgtbl-exp-regexp)
39 ;;; Define Back-End
41 (org-export-define-backend 'latex
42 '((bold . org-latex-bold)
43 (center-block . org-latex-center-block)
44 (clock . org-latex-clock)
45 (code . org-latex-code)
46 (comment . (lambda (&rest args) ""))
47 (comment-block . (lambda (&rest args) ""))
48 (drawer . org-latex-drawer)
49 (dynamic-block . org-latex-dynamic-block)
50 (entity . org-latex-entity)
51 (example-block . org-latex-example-block)
52 (export-block . org-latex-export-block)
53 (export-snippet . org-latex-export-snippet)
54 (fixed-width . org-latex-fixed-width)
55 (footnote-definition . org-latex-footnote-definition)
56 (footnote-reference . org-latex-footnote-reference)
57 (headline . org-latex-headline)
58 (horizontal-rule . org-latex-horizontal-rule)
59 (inline-src-block . org-latex-inline-src-block)
60 (inlinetask . org-latex-inlinetask)
61 (italic . org-latex-italic)
62 (item . org-latex-item)
63 (keyword . org-latex-keyword)
64 (latex-environment . org-latex-latex-environment)
65 (latex-fragment . org-latex-latex-fragment)
66 (line-break . org-latex-line-break)
67 (link . org-latex-link)
68 (node-property . org-latex-node-property)
69 (paragraph . org-latex-paragraph)
70 (plain-list . org-latex-plain-list)
71 (plain-text . org-latex-plain-text)
72 (planning . org-latex-planning)
73 (property-drawer . org-latex-property-drawer)
74 (quote-block . org-latex-quote-block)
75 (quote-section . org-latex-quote-section)
76 (radio-target . org-latex-radio-target)
77 (section . org-latex-section)
78 (special-block . org-latex-special-block)
79 (src-block . org-latex-src-block)
80 (statistics-cookie . org-latex-statistics-cookie)
81 (strike-through . org-latex-strike-through)
82 (subscript . org-latex-subscript)
83 (superscript . org-latex-superscript)
84 (table . org-latex-table)
85 (table-cell . org-latex-table-cell)
86 (table-row . org-latex-table-row)
87 (target . org-latex-target)
88 (template . org-latex-template)
89 (timestamp . org-latex-timestamp)
90 (underline . org-latex-underline)
91 (verbatim . org-latex-verbatim)
92 (verse-block . org-latex-verse-block))
93 :export-block '("LATEX" "TEX")
94 :menu-entry
95 '(?l "Export to LaTeX"
96 ((?L "As LaTeX buffer" org-latex-export-as-latex)
97 (?l "As LaTeX file" org-latex-export-to-latex)
98 (?p "As PDF file" org-latex-export-to-pdf)
99 (?o "As PDF file and open"
100 (lambda (a s v b)
101 (if a (org-latex-export-to-pdf t s v b)
102 (org-open-file (org-latex-export-to-pdf nil s v b)))))))
103 :options-alist '((:latex-class "LATEX_CLASS" nil org-latex-default-class t)
104 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
105 (:latex-header "LATEX_HEADER" nil nil newline)
106 (:latex-header-extra "LATEX_HEADER_EXTRA" nil nil newline)
107 (:latex-hyperref-p nil "texht" org-latex-with-hyperref t)
108 ;; Redefine regular options.
109 (:date "DATE" nil "\\today" t)))
113 ;;; Internal Variables
115 (defconst org-latex-babel-language-alist
116 '(("af" . "afrikaans")
117 ("bg" . "bulgarian")
118 ("bt-br" . "brazilian")
119 ("ca" . "catalan")
120 ("cs" . "czech")
121 ("cy" . "welsh")
122 ("da" . "danish")
123 ("de" . "germanb")
124 ("de-at" . "naustrian")
125 ("de-de" . "ngerman")
126 ("el" . "greek")
127 ("en" . "english")
128 ("en-au" . "australian")
129 ("en-ca" . "canadian")
130 ("en-gb" . "british")
131 ("en-ie" . "irish")
132 ("en-nz" . "newzealand")
133 ("en-us" . "american")
134 ("es" . "spanish")
135 ("et" . "estonian")
136 ("eu" . "basque")
137 ("fi" . "finnish")
138 ("fr" . "frenchb")
139 ("fr-ca" . "canadien")
140 ("gl" . "galician")
141 ("hr" . "croatian")
142 ("hu" . "hungarian")
143 ("id" . "indonesian")
144 ("is" . "icelandic")
145 ("it" . "italian")
146 ("la" . "latin")
147 ("ms" . "malay")
148 ("nl" . "dutch")
149 ("nb" . "norsk")
150 ("nn" . "nynorsk")
151 ("no" . "norsk")
152 ("pl" . "polish")
153 ("pt" . "portuguese")
154 ("ro" . "romanian")
155 ("ru" . "russian")
156 ("sa" . "sanskrit")
157 ("sb" . "uppersorbian")
158 ("sk" . "slovak")
159 ("sl" . "slovene")
160 ("sq" . "albanian")
161 ("sr" . "serbian")
162 ("sv" . "swedish")
163 ("ta" . "tamil")
164 ("tr" . "turkish")
165 ("uk" . "ukrainian"))
166 "Alist between language code and corresponding Babel option.")
168 (defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
169 ("qbordermatrix" . "\\cr")
170 ("kbordermatrix" . "\\\\"))
171 "Alist between matrix macros and their row ending.")
175 ;;; User Configurable Variables
177 (defgroup org-export-latex nil
178 "Options for exporting Org mode files to LaTeX."
179 :tag "Org Export LaTeX"
180 :group 'org-export)
183 ;;;; Preamble
185 (defcustom org-latex-default-class "article"
186 "The default LaTeX class."
187 :group 'org-export-latex
188 :type '(string :tag "LaTeX class"))
190 (defcustom org-latex-classes
191 '(("article"
192 "\\documentclass[11pt]{article}"
193 ("\\section{%s}" . "\\section*{%s}")
194 ("\\subsection{%s}" . "\\subsection*{%s}")
195 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
196 ("\\paragraph{%s}" . "\\paragraph*{%s}")
197 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
198 ("report"
199 "\\documentclass[11pt]{report}"
200 ("\\part{%s}" . "\\part*{%s}")
201 ("\\chapter{%s}" . "\\chapter*{%s}")
202 ("\\section{%s}" . "\\section*{%s}")
203 ("\\subsection{%s}" . "\\subsection*{%s}")
204 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
205 ("book"
206 "\\documentclass[11pt]{book}"
207 ("\\part{%s}" . "\\part*{%s}")
208 ("\\chapter{%s}" . "\\chapter*{%s}")
209 ("\\section{%s}" . "\\section*{%s}")
210 ("\\subsection{%s}" . "\\subsection*{%s}")
211 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
212 "Alist of LaTeX classes and associated header and structure.
213 If #+LATEX_CLASS is set in the buffer, use its value and the
214 associated information. Here is the structure of each cell:
216 \(class-name
217 header-string
218 \(numbered-section . unnumbered-section)
219 ...)
221 The header string
222 -----------------
224 The HEADER-STRING is the header that will be inserted into the
225 LaTeX file. It should contain the \\documentclass macro, and
226 anything else that is needed for this setup. To this header, the
227 following commands will be added:
229 - Calls to \\usepackage for all packages mentioned in the
230 variables `org-latex-default-packages-alist' and
231 `org-latex-packages-alist'. Thus, your header definitions
232 should avoid to also request these packages.
234 - Lines specified via \"#+LATEX_HEADER:\" and
235 \"#+LATEX_HEADER_EXTRA:\" keywords.
237 If you need more control about the sequence in which the header
238 is built up, or if you want to exclude one of these building
239 blocks for a particular class, you can use the following
240 macro-like placeholders.
242 [DEFAULT-PACKAGES] \\usepackage statements for default packages
243 [NO-DEFAULT-PACKAGES] do not include any of the default packages
244 [PACKAGES] \\usepackage statements for packages
245 [NO-PACKAGES] do not include the packages
246 [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
247 [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
249 So a header like
251 \\documentclass{article}
252 [NO-DEFAULT-PACKAGES]
253 [EXTRA]
254 \\providecommand{\\alert}[1]{\\textbf{#1}}
255 [PACKAGES]
257 will omit the default packages, and will include the
258 #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
259 to \\providecommand, and then place \\usepackage commands based
260 on the content of `org-latex-packages-alist'.
262 If your header, `org-latex-default-packages-alist' or
263 `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
264 AUTO will automatically be replaced with a coding system derived
265 from `buffer-file-coding-system'. See also the variable
266 `org-latex-inputenc-alist' for a way to influence this mechanism.
268 Likewise, if your header contains \"\\usepackage[AUTO]{babel}\",
269 AUTO will be replaced with the language related to the language
270 code specified by `org-export-default-language', which see. Note
271 that constructions such as \"\\usepackage[french,AUTO,english]{babel}\"
272 are permitted.
274 The sectioning structure
275 ------------------------
277 The sectioning structure of the class is given by the elements
278 following the header string. For each sectioning level, a number
279 of strings is specified. A %s formatter is mandatory in each
280 section string and will be replaced by the title of the section.
282 Instead of a cons cell (numbered . unnumbered), you can also
283 provide a list of 2 or 4 elements,
285 \(numbered-open numbered-close)
289 \(numbered-open numbered-close unnumbered-open unnumbered-close)
291 providing opening and closing strings for a LaTeX environment
292 that should represent the document section. The opening clause
293 should have a %s to represent the section title.
295 Instead of a list of sectioning commands, you can also specify
296 a function name. That function will be called with two
297 parameters, the (reduced) level of the headline, and a predicate
298 non-nil when the headline should be numbered. It must return
299 a format string in which the section title will be added."
300 :group 'org-export-latex
301 :type '(repeat
302 (list (string :tag "LaTeX class")
303 (string :tag "LaTeX header")
304 (repeat :tag "Levels" :inline t
305 (choice
306 (cons :tag "Heading"
307 (string :tag " numbered")
308 (string :tag "unnumbered"))
309 (list :tag "Environment"
310 (string :tag "Opening (numbered)")
311 (string :tag "Closing (numbered)")
312 (string :tag "Opening (unnumbered)")
313 (string :tag "Closing (unnumbered)"))
314 (function :tag "Hook computing sectioning"))))))
316 (defcustom org-latex-inputenc-alist nil
317 "Alist of inputenc coding system names, and what should really be used.
318 For example, adding an entry
320 (\"utf8\" . \"utf8x\")
322 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
323 are written as utf8 files."
324 :group 'org-export-latex
325 :type '(repeat
326 (cons
327 (string :tag "Derived from buffer")
328 (string :tag "Use this instead"))))
330 (defcustom org-latex-title-command "\\maketitle"
331 "The command used to insert the title just after \\begin{document}.
332 If this string contains the formatting specification \"%s\" then
333 it will be used as a formatting string, passing the title as an
334 argument."
335 :group 'org-export-latex
336 :type 'string)
338 (defcustom org-latex-toc-command "\\tableofcontents\n\n"
339 "LaTeX command to set the table of contents, list of figures, etc.
340 This command only applies to the table of contents generated with
341 the toc:nil option, not to those generated with #+TOC keyword."
342 :group 'org-export-latex
343 :type 'string)
345 (defcustom org-latex-with-hyperref t
346 "Toggle insertion of \\hypersetup{...} in the preamble."
347 :group 'org-export-latex
348 :type 'boolean
349 :safe #'booleanp)
351 ;;;; Headline
353 (defcustom org-latex-format-headline-function
354 'org-latex-format-headline-default-function
355 "Function for formatting the headline's text.
357 This function will be called with 5 arguments:
358 TODO the todo keyword (string or nil).
359 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
360 PRIORITY the priority of the headline (integer or nil)
361 TEXT the main headline text (string).
362 TAGS the tags as a list of strings (list of strings or nil).
364 The function result will be used in the section format string.
366 Use `org-latex-format-headline-default-function' by default,
367 which format headlines like for Org version prior to 8.0."
368 :group 'org-export-latex
369 :version "24.4"
370 :package-version '(Org . "8.0")
371 :type 'function)
374 ;;;; Footnotes
376 (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
377 "Text used to separate footnotes."
378 :group 'org-export-latex
379 :type 'string)
382 ;;;; Timestamps
384 (defcustom org-latex-active-timestamp-format "\\textit{%s}"
385 "A printf format string to be applied to active timestamps."
386 :group 'org-export-latex
387 :type 'string)
389 (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
390 "A printf format string to be applied to inactive timestamps."
391 :group 'org-export-latex
392 :type 'string)
394 (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
395 "A printf format string to be applied to diary timestamps."
396 :group 'org-export-latex
397 :type 'string)
400 ;;;; Links
402 (defcustom org-latex-image-default-option ""
403 "Default option for images."
404 :group 'org-export-latex
405 :version "24.4"
406 :package-version '(Org . "8.0")
407 :type 'string)
409 (defcustom org-latex-image-default-width ".9\\linewidth"
410 "Default width for images.
411 This value will not be used if a height is provided."
412 :group 'org-export-latex
413 :version "24.4"
414 :package-version '(Org . "8.0")
415 :type 'string)
417 (defcustom org-latex-image-default-height ""
418 "Default height for images.
419 This value will not be used if a width is provided, or if the
420 image is wrapped within a \"figure\" or \"wrapfigure\"
421 environment."
422 :group 'org-export-latex
423 :version "24.4"
424 :package-version '(Org . "8.0")
425 :type 'string)
427 (defcustom org-latex-default-figure-position "htb"
428 "Default position for latex figures."
429 :group 'org-export-latex
430 :type 'string)
432 (defcustom org-latex-inline-image-rules
433 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\|svg\\)\\'"))
434 "Rules characterizing image files that can be inlined into LaTeX.
436 A rule consists in an association whose key is the type of link
437 to consider, and value is a regexp that will be matched against
438 link's path.
440 Note that, by default, the image extension *actually* allowed
441 depend on the way the LaTeX file is processed. When used with
442 pdflatex, pdf, jpg and png images are OK. When processing
443 through dvi to Postscript, only ps and eps are allowed. The
444 default we use here encompasses both."
445 :group 'org-export-latex
446 :version "24.4"
447 :package-version '(Org . "8.0")
448 :type '(alist :key-type (string :tag "Type")
449 :value-type (regexp :tag "Path")))
451 (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
452 "Format string for links with unknown path type."
453 :group 'org-export-latex
454 :type 'string)
457 ;;;; Tables
459 (defcustom org-latex-default-table-environment "tabular"
460 "Default environment used to build tables."
461 :group 'org-export-latex
462 :version "24.4"
463 :package-version '(Org . "8.0")
464 :type 'string)
466 (defcustom org-latex-default-table-mode 'table
467 "Default mode for tables.
469 Value can be a symbol among:
471 `table' Regular LaTeX table.
473 `math' In this mode, every cell is considered as being in math
474 mode and the complete table will be wrapped within a math
475 environment. It is particularly useful to write matrices.
477 `inline-math' This mode is almost the same as `math', but the
478 math environment will be inlined.
480 `verbatim' The table is exported as it appears in the Org
481 buffer, within a verbatim environment.
483 This value can be overridden locally with, i.e. \":mode math\" in
484 LaTeX attributes.
486 When modifying this variable, it may be useful to change
487 `org-latex-default-table-environment' accordingly."
488 :group 'org-export-latex
489 :version "24.4"
490 :package-version '(Org . "8.0")
491 :type '(choice (const :tag "Table" table)
492 (const :tag "Matrix" math)
493 (const :tag "Inline matrix" inline-math)
494 (const :tag "Verbatim" verbatim))
495 :safe (lambda (s) (memq s '(table math inline-math verbatim))))
497 (defcustom org-latex-tables-centered t
498 "When non-nil, tables are exported in a center environment."
499 :group 'org-export-latex
500 :type 'boolean
501 :safe #'booleanp)
503 (defcustom org-latex-tables-booktabs nil
504 "When non-nil, display tables in a formal \"booktabs\" style.
505 This option assumes that the \"booktabs\" package is properly
506 loaded in the header of the document. This value can be ignored
507 locally with \":booktabs t\" and \":booktabs nil\" LaTeX
508 attributes."
509 :group 'org-export-latex
510 :version "24.4"
511 :package-version '(Org . "8.0")
512 :type 'boolean
513 :safe #'booleanp)
515 (defcustom org-latex-table-caption-above t
516 "When non-nil, place caption string at the beginning of the table.
517 Otherwise, place it near the end."
518 :group 'org-export-latex
519 :type 'boolean
520 :safe #'booleanp)
522 (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
523 "Format string to display numbers in scientific notation.
524 The format should have \"%s\" twice, for mantissa and exponent
525 \(i.e., \"%s\\\\times10^{%s}\").
527 When nil, no transformation is made."
528 :group 'org-export-latex
529 :version "24.4"
530 :package-version '(Org . "8.0")
531 :type '(choice
532 (string :tag "Format string")
533 (const :tag "No formatting")))
535 ;;;; Text markup
537 (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
538 (code . verb)
539 (italic . "\\emph{%s}")
540 (strike-through . "\\sout{%s}")
541 (underline . "\\uline{%s}")
542 (verbatim . protectedtexttt))
543 "Alist of LaTeX expressions to convert text markup.
545 The key must be a symbol among `bold', `code', `italic',
546 `strike-through', `underline' and `verbatim'. The value is
547 a formatting string to wrap fontified text with.
549 Value can also be set to the following symbols: `verb' and
550 `protectedtexttt'. For the former, Org will use \"\\verb\" to
551 create a format string and select a delimiter character that
552 isn't in the string. For the latter, Org will use \"\\texttt\"
553 to typeset and try to protect special characters.
555 If no association can be found for a given markup, text will be
556 returned as-is."
557 :group 'org-export-latex
558 :type 'alist
559 :options '(bold code italic strike-through underline verbatim))
562 ;;;; Drawers
564 (defcustom org-latex-format-drawer-function
565 (lambda (name contents) contents)
566 "Function called to format a drawer in LaTeX code.
568 The function must accept two parameters:
569 NAME the drawer name, like \"LOGBOOK\"
570 CONTENTS the contents of the drawer.
572 The function should return the string to be exported.
574 The default function simply returns the value of CONTENTS."
575 :group 'org-export-latex
576 :version "24.4"
577 :package-version '(Org . "8.3")
578 :type 'function)
581 ;;;; Inlinetasks
583 (defcustom org-latex-format-inlinetask-function 'ignore
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 (funcall org-latex-format-drawer-function
1225 name contents)))
1226 (org-latex--wrap-label drawer output)))
1229 ;;;; Dynamic Block
1231 (defun org-latex-dynamic-block (dynamic-block contents info)
1232 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1233 CONTENTS holds the contents of the block. INFO is a plist
1234 holding contextual information. See `org-export-data'."
1235 (org-latex--wrap-label dynamic-block contents))
1238 ;;;; Entity
1240 (defun org-latex-entity (entity contents info)
1241 "Transcode an ENTITY object from Org to LaTeX.
1242 CONTENTS are the definition itself. INFO is a plist holding
1243 contextual information."
1244 (let ((ent (org-element-property :latex entity)))
1245 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
1248 ;;;; Example Block
1250 (defun org-latex-example-block (example-block contents info)
1251 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1252 CONTENTS is nil. INFO is a plist holding contextual
1253 information."
1254 (when (org-string-nw-p (org-element-property :value example-block))
1255 (org-latex--wrap-label
1256 example-block
1257 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1258 (org-export-format-code-default example-block info)))))
1261 ;;;; Export Block
1263 (defun org-latex-export-block (export-block contents info)
1264 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1265 CONTENTS is nil. INFO is a plist holding contextual information."
1266 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1267 (org-remove-indentation (org-element-property :value export-block))))
1270 ;;;; Export Snippet
1272 (defun org-latex-export-snippet (export-snippet contents info)
1273 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1274 CONTENTS is nil. INFO is a plist holding contextual information."
1275 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1276 (org-element-property :value export-snippet)))
1279 ;;;; Fixed Width
1281 (defun org-latex-fixed-width (fixed-width contents info)
1282 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1283 CONTENTS is nil. INFO is a plist holding contextual information."
1284 (org-latex--wrap-label
1285 fixed-width
1286 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1287 (org-remove-indentation
1288 (org-element-property :value fixed-width)))))
1291 ;;;; Footnote Reference
1293 (defun org-latex-footnote-reference (footnote-reference contents info)
1294 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1295 CONTENTS is nil. INFO is a plist holding contextual information."
1296 (concat
1297 ;; Insert separator between two footnotes in a row.
1298 (let ((prev (org-export-get-previous-element footnote-reference info)))
1299 (when (eq (org-element-type prev) 'footnote-reference)
1300 org-latex-footnote-separator))
1301 (cond
1302 ;; Use \footnotemark if the footnote has already been defined.
1303 ((not (org-export-footnote-first-reference-p footnote-reference info))
1304 (format "\\footnotemark[%s]{}"
1305 (org-export-get-footnote-number footnote-reference info)))
1306 ;; Use \footnotemark if reference is within another footnote
1307 ;; reference, footnote definition or table cell.
1308 ((loop for parent in (org-export-get-genealogy footnote-reference)
1309 thereis (memq (org-element-type parent)
1310 '(footnote-reference footnote-definition table-cell)))
1311 "\\footnotemark")
1312 ;; Otherwise, define it with \footnote command.
1314 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1315 (concat
1316 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1317 ;; Retrieve all footnote references within the footnote and
1318 ;; add their definition after it, since LaTeX doesn't support
1319 ;; them inside.
1320 (org-latex--delayed-footnotes-definitions def info)))))))
1323 ;;;; Headline
1325 (defun org-latex-headline (headline contents info)
1326 "Transcode a HEADLINE element from Org to LaTeX.
1327 CONTENTS holds the contents of the headline. INFO is a plist
1328 holding contextual information."
1329 (unless (org-element-property :footnote-section-p headline)
1330 (let* ((class (plist-get info :latex-class))
1331 (level (org-export-get-relative-level headline info))
1332 (numberedp (org-export-numbered-headline-p headline info))
1333 (class-sectioning (assoc class org-latex-classes))
1334 ;; Section formatting will set two placeholders: one for
1335 ;; the title and the other for the contents.
1336 (section-fmt
1337 (let ((sec (if (functionp (nth 2 class-sectioning))
1338 (funcall (nth 2 class-sectioning) level numberedp)
1339 (nth (1+ level) class-sectioning))))
1340 (cond
1341 ;; No section available for that LEVEL.
1342 ((not sec) nil)
1343 ;; Section format directly returned by a function. Add
1344 ;; placeholder for contents.
1345 ((stringp sec) (concat sec "\n%s"))
1346 ;; (numbered-section . unnumbered-section)
1347 ((not (consp (cdr sec)))
1348 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1349 ;; (numbered-open numbered-close)
1350 ((= (length sec) 2)
1351 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1352 ;; (num-in num-out no-num-in no-num-out)
1353 ((= (length sec) 4)
1354 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1355 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1356 (text (org-export-data (org-element-property :title headline) info))
1357 (todo
1358 (and (plist-get info :with-todo-keywords)
1359 (let ((todo (org-element-property :todo-keyword headline)))
1360 (and todo (org-export-data todo info)))))
1361 (todo-type (and todo (org-element-property :todo-type headline)))
1362 (tags (and (plist-get info :with-tags)
1363 (org-export-get-tags headline info)))
1364 (priority (and (plist-get info :with-priority)
1365 (org-element-property :priority headline)))
1366 ;; Create the headline text along with a no-tag version.
1367 ;; The latter is required to remove tags from toc.
1368 (full-text (funcall org-latex-format-headline-function
1369 todo todo-type priority text tags))
1370 ;; Associate \label to the headline for internal links.
1371 (headline-label
1372 (format "\\label{sec-%s}\n"
1373 (mapconcat 'number-to-string
1374 (org-export-get-headline-number headline info)
1375 "-")))
1376 (pre-blanks
1377 (make-string (org-element-property :pre-blank headline) 10)))
1378 (if (or (not section-fmt) (org-export-low-level-p headline info))
1379 ;; This is a deep sub-tree: export it as a list item. Also
1380 ;; export as items headlines for which no section format has
1381 ;; been found.
1382 (let ((low-level-body
1383 (concat
1384 ;; If headline is the first sibling, start a list.
1385 (when (org-export-first-sibling-p headline info)
1386 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1387 ;; Itemize headline
1388 "\\item " full-text "\n" headline-label pre-blanks contents)))
1389 ;; If headline is not the last sibling simply return
1390 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1391 ;; any blank line.
1392 (if (not (org-export-last-sibling-p headline info)) low-level-body
1393 (replace-regexp-in-string
1394 "[ \t\n]*\\'"
1395 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1396 low-level-body)))
1397 ;; This is a standard headline. Export it as a section. Add
1398 ;; an alternative heading when possible, and when this is not
1399 ;; identical to the usual heading.
1400 (let ((opt-title
1401 (funcall org-latex-format-headline-function
1402 todo todo-type priority
1403 (org-export-data
1404 (org-export-get-alt-title headline info) info)
1405 (and (eq (plist-get info :with-tags) t) tags))))
1406 (if (and numberedp opt-title
1407 (not (equal opt-title full-text))
1408 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
1409 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1410 ;; Replace square brackets with parenthesis
1411 ;; since square brackets are not supported in
1412 ;; optional arguments.
1413 (replace-regexp-in-string
1414 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1415 full-text
1416 (concat headline-label pre-blanks contents))
1417 ;; Impossible to add an alternative heading. Fallback to
1418 ;; regular sectioning format string.
1419 (format section-fmt full-text
1420 (concat headline-label pre-blanks contents))))))))
1422 (defun org-latex-format-headline-default-function
1423 (todo todo-type priority text tags)
1424 "Default format function for a headline.
1425 See `org-latex-format-headline-function' for details."
1426 (concat
1427 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1428 (and priority (format "\\framebox{\\#%c} " priority))
1429 text
1430 (and tags
1431 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags ":")))))
1434 ;;;; Horizontal Rule
1436 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1437 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1438 CONTENTS is nil. INFO is a plist holding contextual information."
1439 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1440 (prev (org-export-get-previous-element horizontal-rule info)))
1441 (concat
1442 ;; Make sure the rule doesn't start at the end of the current
1443 ;; line by separating it with a blank line from previous element.
1444 (when (and prev
1445 (let ((prev-blank (org-element-property :post-blank prev)))
1446 (or (not prev-blank) (zerop prev-blank))))
1447 "\n")
1448 (org-latex--wrap-label
1449 horizontal-rule
1450 (format "\\rule{%s}{%s}"
1451 (or (plist-get attr :width) "\\linewidth")
1452 (or (plist-get attr :thickness) "0.5pt"))))))
1455 ;;;; Inline Src Block
1457 (defun org-latex-inline-src-block (inline-src-block contents info)
1458 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1459 CONTENTS holds the contents of the item. INFO is a plist holding
1460 contextual information."
1461 (let* ((code (org-element-property :value inline-src-block))
1462 (separator (org-latex--find-verb-separator code)))
1463 (cond
1464 ;; Do not use a special package: transcode it verbatim.
1465 ((not org-latex-listings)
1466 (concat "\\verb" separator code separator))
1467 ;; Use minted package.
1468 ((eq org-latex-listings 'minted)
1469 (let* ((org-lang (org-element-property :language inline-src-block))
1470 (mint-lang (or (cadr (assq (intern org-lang)
1471 org-latex-minted-langs))
1472 org-lang))
1473 (options (org-latex--make-option-string
1474 org-latex-minted-options)))
1475 (concat (format "\\mint%s{%s}"
1476 (if (string= options "") "" (format "[%s]" options))
1477 mint-lang)
1478 separator code separator)))
1479 ;; Use listings package.
1481 ;; Maybe translate language's name.
1482 (let* ((org-lang (org-element-property :language inline-src-block))
1483 (lst-lang (or (cadr (assq (intern org-lang)
1484 org-latex-listings-langs))
1485 org-lang))
1486 (options (org-latex--make-option-string
1487 (append org-latex-listings-options
1488 `(("language" ,lst-lang))))))
1489 (concat (format "\\lstinline[%s]" options)
1490 separator code separator))))))
1493 ;;;; Inlinetask
1495 (defun org-latex-inlinetask (inlinetask contents info)
1496 "Transcode an INLINETASK element from Org to LaTeX.
1497 CONTENTS holds the contents of the block. INFO is a plist
1498 holding contextual information."
1499 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1500 (todo (and (plist-get info :with-todo-keywords)
1501 (let ((todo (org-element-property :todo-keyword inlinetask)))
1502 (and todo (org-export-data todo info)))))
1503 (todo-type (org-element-property :todo-type inlinetask))
1504 (tags (and (plist-get info :with-tags)
1505 (org-export-get-tags inlinetask info)))
1506 (priority (and (plist-get info :with-priority)
1507 (org-element-property :priority inlinetask))))
1508 ;; If `org-latex-format-inlinetask-function' is provided, call it
1509 ;; with appropriate arguments.
1510 (if (not (eq org-latex-format-inlinetask-function 'ignore))
1511 (funcall org-latex-format-inlinetask-function
1512 todo todo-type priority title tags contents)
1513 ;; Otherwise, use a default template.
1514 (org-latex--wrap-label
1515 inlinetask
1516 (let ((full-title
1517 (concat
1518 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1519 (when priority (format "\\framebox{\\#%c} " priority))
1520 title
1521 (when tags (format "\\hfill{}\\textsc{:%s:}"
1522 (mapconcat 'identity tags ":"))))))
1523 (format (concat "\\begin{center}\n"
1524 "\\fbox{\n"
1525 "\\begin{minipage}[c]{.6\\textwidth}\n"
1526 "%s\n\n"
1527 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1528 "%s"
1529 "\\end{minipage}\n"
1530 "}\n"
1531 "\\end{center}")
1532 full-title contents))))))
1535 ;;;; Italic
1537 (defun org-latex-italic (italic contents info)
1538 "Transcode ITALIC from Org to LaTeX.
1539 CONTENTS is the text with italic markup. INFO is a plist holding
1540 contextual information."
1541 (org-latex--text-markup contents 'italic))
1544 ;;;; Item
1546 (defun org-latex-item (item contents info)
1547 "Transcode an ITEM element from Org to LaTeX.
1548 CONTENTS holds the contents of the item. INFO is a plist holding
1549 contextual information."
1550 (let* ((counter
1551 (let ((count (org-element-property :counter item))
1552 (level
1553 ;; Determine level of current item to determine the
1554 ;; correct LaTeX counter to use (enumi, enumii...).
1555 (let ((parent item) (level 0))
1556 (while (memq (org-element-type
1557 (setq parent (org-export-get-parent parent)))
1558 '(plain-list item))
1559 (when (and (eq (org-element-type parent) 'plain-list)
1560 (eq (org-element-property :type parent)
1561 'ordered))
1562 (incf level)))
1563 level)))
1564 (and count
1565 (< level 5)
1566 (format "\\setcounter{enum%s}{%s}\n"
1567 (nth (1- level) '("i" "ii" "iii" "iv"))
1568 (1- count)))))
1569 (checkbox (case (org-element-property :checkbox item)
1570 (on "$\\boxtimes$ ")
1571 (off "$\\square$ ")
1572 (trans "$\\boxminus$ ")))
1573 (tag (let ((tag (org-element-property :tag item)))
1574 ;; Check-boxes must belong to the tag.
1575 (and tag (format "[{%s}] "
1576 (concat checkbox
1577 (org-export-data tag info)))))))
1578 (concat counter "\\item" (or tag (concat " " checkbox))
1579 (and contents (org-trim contents))
1580 ;; If there are footnotes references in tag, be sure to
1581 ;; add their definition at the end of the item. This
1582 ;; workaround is necessary since "\footnote{}" command is
1583 ;; not supported in tags.
1584 (and tag
1585 (org-latex--delayed-footnotes-definitions
1586 (org-element-property :tag item) info)))))
1589 ;;;; Keyword
1591 (defun org-latex-keyword (keyword contents info)
1592 "Transcode a KEYWORD element from Org to LaTeX.
1593 CONTENTS is nil. INFO is a plist holding contextual information."
1594 (let ((key (org-element-property :key keyword))
1595 (value (org-element-property :value keyword)))
1596 (cond
1597 ((string= key "LATEX") value)
1598 ((string= key "INDEX") (format "\\index{%s}" value))
1599 ((string= key "TOC")
1600 (let ((value (downcase value)))
1601 (cond
1602 ((string-match "\\<headlines\\>" value)
1603 (let ((depth (or (and (string-match "[0-9]+" value)
1604 (string-to-number (match-string 0 value)))
1605 (plist-get info :with-toc))))
1606 (concat
1607 (when (wholenump depth)
1608 (format "\\setcounter{tocdepth}{%s}\n" depth))
1609 "\\tableofcontents")))
1610 ((string= "tables" value) "\\listoftables")
1611 ((string= "listings" value)
1612 (cond
1613 ((eq org-latex-listings 'minted) "\\listoflistings")
1614 (org-latex-listings "\\lstlistoflistings")
1615 ;; At the moment, src blocks with a caption are wrapped
1616 ;; into a figure environment.
1617 (t "\\listoffigures")))))))))
1620 ;;;; Latex Environment
1622 (defun org-latex-latex-environment (latex-environment contents info)
1623 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1624 CONTENTS is nil. INFO is a plist holding contextual information."
1625 (when (plist-get info :with-latex)
1626 (let ((label (org-element-property :name latex-environment))
1627 (value (org-remove-indentation
1628 (org-element-property :value latex-environment))))
1629 (if (not (org-string-nw-p label)) value
1630 ;; Environment is labeled: label must be within the environment
1631 ;; (otherwise, a reference pointing to that element will count
1632 ;; the section instead).
1633 (with-temp-buffer
1634 (insert value)
1635 (goto-char (point-min))
1636 (forward-line)
1637 (insert
1638 (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1639 (buffer-string))))))
1642 ;;;; Latex Fragment
1644 (defun org-latex-latex-fragment (latex-fragment contents info)
1645 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1646 CONTENTS is nil. INFO is a plist holding contextual information."
1647 (when (plist-get info :with-latex)
1648 (org-element-property :value latex-fragment)))
1651 ;;;; Line Break
1653 (defun org-latex-line-break (line-break contents info)
1654 "Transcode a LINE-BREAK object from Org to LaTeX.
1655 CONTENTS is nil. INFO is a plist holding contextual information."
1656 "\\\\\n")
1659 ;;;; Link
1661 (defun org-latex--inline-image (link info)
1662 "Return LaTeX code for an inline image.
1663 LINK is the link pointing to the inline image. INFO is a plist
1664 used as a communication channel."
1665 (let* ((parent (org-export-get-parent-element link))
1666 (path (let ((raw-path (org-element-property :path link)))
1667 (if (not (file-name-absolute-p raw-path)) raw-path
1668 (expand-file-name raw-path))))
1669 (filetype (file-name-extension path))
1670 (caption (org-latex--caption/label-string parent info))
1671 ;; Retrieve latex attributes from the element around.
1672 (attr (org-export-read-attribute :attr_latex parent))
1673 (float (let ((float (plist-get attr :float)))
1674 (cond ((and (not float) (plist-member attr :float)) nil)
1675 ((string= float "wrap") 'wrap)
1676 ((string= float "multicolumn") 'multicolumn)
1677 ((or float
1678 (org-element-property :caption parent)
1679 (org-string-nw-p (plist-get attr :caption)))
1680 'figure))))
1681 (placement
1682 (let ((place (plist-get attr :placement)))
1683 (cond (place (format "%s" place))
1684 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1685 ((eq float 'figure)
1686 (format "[%s]" org-latex-default-figure-position))
1687 (t ""))))
1688 (comment-include (if (plist-get attr :comment-include) "%" ""))
1689 ;; It is possible to specify width and height in the
1690 ;; ATTR_LATEX line, and also via default variables.
1691 (width (cond ((plist-get attr :width))
1692 ((plist-get attr :height) "")
1693 ((eq float 'wrap) "0.48\\textwidth")
1694 (t org-latex-image-default-width)))
1695 (height (cond ((plist-get attr :height))
1696 ((or (plist-get attr :width)
1697 (memq float '(figure wrap))) "")
1698 (t org-latex-image-default-height)))
1699 (options (let ((opt (or (plist-get attr :options)
1700 org-latex-image-default-option)))
1701 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
1702 (match-string 1 opt))))
1703 image-code)
1704 (if (member filetype '("tikz" "pgf"))
1705 ;; For tikz images:
1706 ;; - use \input to read in image file.
1707 ;; - if options are present, wrap in a tikzpicture environment.
1708 ;; - if width or height are present, use \resizebox to change
1709 ;; the image size.
1710 (progn
1711 (setq image-code (format "\\input{%s}" path))
1712 (when (org-string-nw-p options)
1713 (setq image-code
1714 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1715 options
1716 image-code)))
1717 (when (or (org-string-nw-p width) (org-string-nw-p height))
1718 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
1719 (if (org-string-nw-p width) width "!")
1720 (if (org-string-nw-p height) height "!")
1721 image-code))))
1722 ;; For other images:
1723 ;; - add width and height to options.
1724 ;; - include the image with \includegraphics.
1725 (when (org-string-nw-p width)
1726 (setq options (concat options ",width=" width)))
1727 (when (org-string-nw-p height)
1728 (setq options (concat options ",height=" height)))
1729 (setq image-code
1730 (format "\\includegraphics%s{%s}"
1731 (cond ((not (org-string-nw-p options)) "")
1732 ((= (aref options 0) ?,)
1733 (format "[%s]"(substring options 1)))
1734 (t (format "[%s]" options)))
1735 path))
1736 (when (equal filetype "svg")
1737 (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
1738 "\\includesvg"
1739 image-code
1740 nil t))
1741 (setq image-code (replace-regexp-in-string "\\.svg}"
1743 image-code
1744 nil t))))
1745 ;; Return proper string, depending on FLOAT.
1746 (case float
1747 (wrap (format "\\begin{wrapfigure}%s
1748 \\centering
1749 %s%s
1750 %s\\end{wrapfigure}" placement comment-include image-code caption))
1751 (multicolumn (format "\\begin{figure*}%s
1752 \\centering
1753 %s%s
1754 %s\\end{figure*}" placement comment-include image-code caption))
1755 (figure (format "\\begin{figure}%s
1756 \\centering
1757 %s%s
1758 %s\\end{figure}" placement comment-include image-code caption))
1759 (otherwise image-code))))
1761 (defun org-latex-link (link desc info)
1762 "Transcode a LINK object from Org to LaTeX.
1764 DESC is the description part of the link, or the empty string.
1765 INFO is a plist holding contextual information. See
1766 `org-export-data'."
1767 (let* ((type (org-element-property :type link))
1768 (raw-path (org-element-property :path link))
1769 ;; Ensure DESC really exists, or set it to nil.
1770 (desc (and (not (string= desc "")) desc))
1771 (imagep (org-export-inline-image-p
1772 link org-latex-inline-image-rules))
1773 (path (cond
1774 ((member type '("http" "https" "ftp" "mailto"))
1775 (concat type ":" raw-path))
1776 ((string= type "file")
1777 (if (not (file-name-absolute-p raw-path)) raw-path
1778 (concat "file://" (expand-file-name raw-path))))
1779 (t raw-path)))
1780 protocol)
1781 (cond
1782 ;; Image file.
1783 (imagep (org-latex--inline-image link info))
1784 ;; Radio link: Transcode target's contents and use them as link's
1785 ;; description.
1786 ((string= type "radio")
1787 (let ((destination (org-export-resolve-radio-link link info)))
1788 (when destination
1789 (format "\\hyperref[%s]{%s}"
1790 (org-export-solidify-link-text path)
1791 (org-export-data (org-element-contents destination) info)))))
1792 ;; Links pointing to a headline: Find destination and build
1793 ;; appropriate referencing command.
1794 ((member type '("custom-id" "fuzzy" "id"))
1795 (let ((destination (if (string= type "fuzzy")
1796 (org-export-resolve-fuzzy-link link info)
1797 (org-export-resolve-id-link link info))))
1798 (case (org-element-type destination)
1799 ;; Id link points to an external file.
1800 (plain-text
1801 (if desc (format "\\href{%s}{%s}" destination desc)
1802 (format "\\url{%s}" destination)))
1803 ;; Fuzzy link points nowhere.
1804 ('nil
1805 (format org-latex-link-with-unknown-path-format
1806 (or desc
1807 (org-export-data
1808 (org-element-property :raw-link link) info))))
1809 ;; LINK points to a headline. If headlines are numbered
1810 ;; and the link has no description, display headline's
1811 ;; number. Otherwise, display description or headline's
1812 ;; title.
1813 (headline
1814 (let ((label
1815 (format "sec-%s"
1816 (mapconcat
1817 'number-to-string
1818 (org-export-get-headline-number destination info)
1819 "-"))))
1820 (if (and (plist-get info :section-numbers) (not desc))
1821 (format "\\ref{%s}" label)
1822 (format "\\hyperref[%s]{%s}" label
1823 (or desc
1824 (org-export-data
1825 (org-element-property :title destination) info))))))
1826 ;; Fuzzy link points to a target. Do as above.
1827 (otherwise
1828 (let ((path (org-export-solidify-link-text path)))
1829 (if (not desc) (format "\\ref{%s}" path)
1830 (format "\\hyperref[%s]{%s}" path desc)))))))
1831 ;; Coderef: replace link with the reference name or the
1832 ;; equivalent line number.
1833 ((string= type "coderef")
1834 (format (org-export-get-coderef-format path desc)
1835 (org-export-resolve-coderef path info)))
1836 ;; Link type is handled by a special function.
1837 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1838 (funcall protocol (org-link-unescape path) desc 'latex))
1839 ;; External link with a description part.
1840 ((and path desc) (format "\\href{%s}{%s}" path desc))
1841 ;; External link without a description part.
1842 (path (format "\\url{%s}" path))
1843 ;; No path, only description. Try to do something useful.
1844 (t (format org-latex-link-with-unknown-path-format desc)))))
1847 ;;;; Node Property
1849 (defun org-latex-node-property (node-property contents info)
1850 "Transcode a NODE-PROPERTY element from Org to LaTeX.
1851 CONTENTS is nil. INFO is a plist holding contextual
1852 information."
1853 (format "%s:%s"
1854 (org-element-property :key node-property)
1855 (let ((value (org-element-property :value node-property)))
1856 (if value (concat " " value) ""))))
1859 ;;;; Paragraph
1861 (defun org-latex-paragraph (paragraph contents info)
1862 "Transcode a PARAGRAPH element from Org to LaTeX.
1863 CONTENTS is the contents of the paragraph, as a string. INFO is
1864 the plist used as a communication channel."
1865 contents)
1868 ;;;; Plain List
1870 (defun org-latex-plain-list (plain-list contents info)
1871 "Transcode a PLAIN-LIST element from Org to LaTeX.
1872 CONTENTS is the contents of the list. INFO is a plist holding
1873 contextual information."
1874 (let* ((type (org-element-property :type plain-list))
1875 (attr (org-export-read-attribute :attr_latex plain-list))
1876 (latex-type (let ((env (plist-get attr :environment)))
1877 (cond (env (format "%s" env))
1878 ((eq type 'ordered) "enumerate")
1879 ((eq type 'unordered) "itemize")
1880 ((eq type 'descriptive) "description")))))
1881 (org-latex--wrap-label
1882 plain-list
1883 (format "\\begin{%s}%s\n%s\\end{%s}"
1884 latex-type
1885 ;; Put optional arguments, if any inside square brackets
1886 ;; when necessary.
1887 (let ((options (format "%s" (or (plist-get attr :options) ""))))
1888 (cond ((equal options "") "")
1889 ((string-match "\\`\\[.*\\]\\'" options) options)
1890 (t (format "[%s]" options))))
1891 contents
1892 latex-type))))
1895 ;;;; Plain Text
1897 (defun org-latex-plain-text (text info)
1898 "Transcode a TEXT string from Org to LaTeX.
1899 TEXT is the string to transcode. INFO is a plist holding
1900 contextual information."
1901 (let ((specialp (plist-get info :with-special-strings))
1902 (output text))
1903 ;; Protect %, #, &, $, _, { and }.
1904 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output)
1905 (setq output
1906 (replace-match
1907 (format "\\%s" (match-string 2 output)) nil t output 2)))
1908 ;; Protect ^.
1909 (setq output
1910 (replace-regexp-in-string
1911 "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil 2))
1912 ;; Protect \. If special strings are used, be careful not to
1913 ;; protect "\" in "\-" constructs.
1914 (let ((symbols (if specialp "-%$#&{}^_\\" "%$#&{}^_\\")))
1915 (setq output
1916 (replace-regexp-in-string
1917 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
1918 "$\\backslash$" output nil t 1)))
1919 ;; Protect ~.
1920 (setq output
1921 (replace-regexp-in-string
1922 "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t 2))
1923 ;; Activate smart quotes. Be sure to provide original TEXT string
1924 ;; since OUTPUT may have been modified.
1925 (when (plist-get info :with-smart-quotes)
1926 (setq output (org-export-activate-smart-quotes output :latex info text)))
1927 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1928 (let ((case-fold-search nil)
1929 (start 0))
1930 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
1931 (setq output (replace-match
1932 (format "\\%s{}" (match-string 1 output)) nil t output)
1933 start (match-end 0))))
1934 ;; Convert special strings.
1935 (when specialp
1936 (setq output
1937 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
1938 ;; Handle break preservation if required.
1939 (when (plist-get info :preserve-breaks)
1940 (setq output (replace-regexp-in-string
1941 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
1942 ;; Return value.
1943 output))
1946 ;;;; Planning
1948 (defun org-latex-planning (planning contents info)
1949 "Transcode a PLANNING element from Org to LaTeX.
1950 CONTENTS is nil. INFO is a plist holding contextual
1951 information."
1952 (concat
1953 "\\noindent"
1954 (mapconcat
1955 'identity
1956 (delq nil
1957 (list
1958 (let ((closed (org-element-property :closed planning)))
1959 (when closed
1960 (concat
1961 (format "\\textbf{%s} " org-closed-string)
1962 (format org-latex-inactive-timestamp-format
1963 (org-translate-time
1964 (org-element-property :raw-value closed))))))
1965 (let ((deadline (org-element-property :deadline planning)))
1966 (when deadline
1967 (concat
1968 (format "\\textbf{%s} " org-deadline-string)
1969 (format org-latex-active-timestamp-format
1970 (org-translate-time
1971 (org-element-property :raw-value deadline))))))
1972 (let ((scheduled (org-element-property :scheduled planning)))
1973 (when scheduled
1974 (concat
1975 (format "\\textbf{%s} " org-scheduled-string)
1976 (format org-latex-active-timestamp-format
1977 (org-translate-time
1978 (org-element-property :raw-value scheduled))))))))
1979 " ")
1980 "\\\\"))
1983 ;;;; Property Drawer
1985 (defun org-latex-property-drawer (property-drawer contents info)
1986 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1987 CONTENTS holds the contents of the drawer. INFO is a plist
1988 holding contextual information."
1989 (and (org-string-nw-p contents)
1990 (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
1993 ;;;; Quote Block
1995 (defun org-latex-quote-block (quote-block contents info)
1996 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1997 CONTENTS holds the contents of the block. INFO is a plist
1998 holding contextual information."
1999 (org-latex--wrap-label
2000 quote-block
2001 (format "\\begin{quote}\n%s\\end{quote}" contents)))
2004 ;;;; Quote Section
2006 (defun org-latex-quote-section (quote-section contents info)
2007 "Transcode a QUOTE-SECTION element from Org to LaTeX.
2008 CONTENTS is nil. INFO is a plist holding contextual information."
2009 (let ((value (org-remove-indentation
2010 (org-element-property :value quote-section))))
2011 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
2014 ;;;; Radio Target
2016 (defun org-latex-radio-target (radio-target text info)
2017 "Transcode a RADIO-TARGET object from Org to LaTeX.
2018 TEXT is the text of the target. INFO is a plist holding
2019 contextual information."
2020 (format "\\label{%s}%s"
2021 (org-export-solidify-link-text
2022 (org-element-property :value radio-target))
2023 text))
2026 ;;;; Section
2028 (defun org-latex-section (section contents info)
2029 "Transcode a SECTION element from Org to LaTeX.
2030 CONTENTS holds the contents of the section. INFO is a plist
2031 holding contextual information."
2032 contents)
2035 ;;;; Special Block
2037 (defun org-latex-special-block (special-block contents info)
2038 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2039 CONTENTS holds the contents of the block. INFO is a plist
2040 holding contextual information."
2041 (let ((type (downcase (org-element-property :type special-block)))
2042 (opt (org-export-read-attribute :attr_latex special-block :options)))
2043 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2044 ;; Insert any label or caption within the block
2045 ;; (otherwise, a reference pointing to that element will
2046 ;; count the section instead).
2047 (org-latex--caption/label-string special-block info)
2048 contents
2049 (format "\\end{%s}" type))))
2052 ;;;; Src Block
2054 (defun org-latex-src-block (src-block contents info)
2055 "Transcode a SRC-BLOCK element from Org to LaTeX.
2056 CONTENTS holds the contents of the item. INFO is a plist holding
2057 contextual information."
2058 (when (org-string-nw-p (org-element-property :value src-block))
2059 (let* ((lang (org-element-property :language src-block))
2060 (caption (org-element-property :caption src-block))
2061 (label (org-element-property :name src-block))
2062 (custom-env (and lang
2063 (cadr (assq (intern lang)
2064 org-latex-custom-lang-environments))))
2065 (num-start (case (org-element-property :number-lines src-block)
2066 (continued (org-export-get-loc src-block info))
2067 (new 0)))
2068 (retain-labels (org-element-property :retain-labels src-block))
2069 (attributes (org-export-read-attribute :attr_latex src-block))
2070 (float (plist-get attributes :float)))
2071 (cond
2072 ;; Case 1. No source fontification.
2073 ((not org-latex-listings)
2074 (let* ((caption-str (org-latex--caption/label-string src-block info))
2075 (float-env
2076 (cond ((and (not float) (plist-member attributes :float)) "%s")
2077 ((string= "multicolumn" float)
2078 (format "\\begin{figure*}[%s]\n%%s%s\n\\end{figure*}"
2079 org-latex-default-figure-position
2080 caption-str))
2081 ((or caption float)
2082 (format "\\begin{figure}[H]\n%%s%s\n\\end{figure}"
2083 caption-str))
2084 (t "%s"))))
2085 (format
2086 float-env
2087 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2088 (org-export-format-code-default src-block info))))))
2089 ;; Case 2. Custom environment.
2090 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2091 custom-env
2092 (org-export-format-code-default src-block info)
2093 custom-env))
2094 ;; Case 3. Use minted package.
2095 ((eq org-latex-listings 'minted)
2096 (let* ((caption-str (org-latex--caption/label-string src-block info))
2097 (float-env
2098 (cond ((and (not float) (plist-member attributes :float)) "%s")
2099 ((string= "multicolumn" float)
2100 (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
2101 caption-str))
2102 ((or caption float)
2103 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2104 caption-str))
2105 (t "%s")))
2106 (body
2107 (format
2108 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2109 ;; Options.
2110 (org-latex--make-option-string
2111 (if (or (not num-start)
2112 (assoc "linenos" org-latex-minted-options))
2113 org-latex-minted-options
2114 (append
2115 `(("linenos")
2116 ("firstnumber" ,(number-to-string (1+ num-start))))
2117 org-latex-minted-options)))
2118 ;; Language.
2119 (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
2120 ;; Source code.
2121 (let* ((code-info (org-export-unravel-code src-block))
2122 (max-width
2123 (apply 'max
2124 (mapcar 'length
2125 (org-split-string (car code-info)
2126 "\n")))))
2127 (org-export-format-code
2128 (car code-info)
2129 (lambda (loc num ref)
2130 (concat
2132 (when ref
2133 ;; Ensure references are flushed to the right,
2134 ;; separated with 6 spaces from the widest line
2135 ;; of code.
2136 (concat (make-string (+ (- max-width (length loc)) 6)
2137 ?\s)
2138 (format "(%s)" ref)))))
2139 nil (and retain-labels (cdr code-info)))))))
2140 ;; Return value.
2141 (format float-env body)))
2142 ;; Case 4. Use listings package.
2144 (let ((lst-lang
2145 (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
2146 (caption-str
2147 (when caption
2148 (let ((main (org-export-get-caption src-block))
2149 (secondary (org-export-get-caption src-block t)))
2150 (if (not secondary)
2151 (format "{%s}" (org-export-data main info))
2152 (format "{[%s]%s}"
2153 (org-export-data secondary info)
2154 (org-export-data main info)))))))
2155 (concat
2156 ;; Options.
2157 (format
2158 "\\lstset{%s}\n"
2159 (org-latex--make-option-string
2160 (append
2161 org-latex-listings-options
2162 (cond
2163 ((and (not float) (plist-member attributes :float)) nil)
2164 ((string= "multicolumn" float) '(("float" "*")))
2165 ((and float (not (assoc "float" org-latex-listings-options)))
2166 `(("float" ,org-latex-default-figure-position))))
2167 `(("language" ,lst-lang))
2168 (when label `(("label" ,label)))
2169 (when caption-str `(("caption" ,caption-str)))
2170 (cond ((assoc "numbers" org-latex-listings-options) nil)
2171 ((not num-start) '(("numbers" "none")))
2172 ((zerop num-start) '(("numbers" "left")))
2173 (t `(("numbers" "left")
2174 ("firstnumber"
2175 ,(number-to-string (1+ num-start)))))))))
2176 ;; Source code.
2177 (format
2178 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2179 (let* ((code-info (org-export-unravel-code src-block))
2180 (max-width
2181 (apply 'max
2182 (mapcar 'length
2183 (org-split-string (car code-info) "\n")))))
2184 (org-export-format-code
2185 (car code-info)
2186 (lambda (loc num ref)
2187 (concat
2189 (when ref
2190 ;; Ensure references are flushed to the right,
2191 ;; separated with 6 spaces from the widest line of
2192 ;; code
2193 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2194 (format "(%s)" ref)))))
2195 nil (and retain-labels (cdr code-info))))))))))))
2198 ;;;; Statistics Cookie
2200 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2201 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2202 CONTENTS is nil. INFO is a plist holding contextual information."
2203 (replace-regexp-in-string
2204 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2207 ;;;; Strike-Through
2209 (defun org-latex-strike-through (strike-through contents info)
2210 "Transcode STRIKE-THROUGH from Org to LaTeX.
2211 CONTENTS is the text with strike-through markup. INFO is a plist
2212 holding contextual information."
2213 (org-latex--text-markup contents 'strike-through))
2216 ;;;; Subscript
2218 (defun org-latex--script-size (object info)
2219 "Transcode a subscript or superscript object.
2220 OBJECT is an Org object. INFO is a plist used as a communication
2221 channel."
2222 (let ((in-script-p
2223 ;; Non-nil if object is already in a sub/superscript.
2224 (let ((parent object))
2225 (catch 'exit
2226 (while (setq parent (org-export-get-parent parent))
2227 (let ((type (org-element-type parent)))
2228 (cond ((memq type '(subscript superscript))
2229 (throw 'exit t))
2230 ((memq type org-element-all-elements)
2231 (throw 'exit nil))))))))
2232 (type (org-element-type object))
2233 (output ""))
2234 (org-element-map (org-element-contents object)
2235 (cons 'plain-text org-element-all-objects)
2236 (lambda (obj)
2237 (case (org-element-type obj)
2238 ((entity latex-fragment)
2239 (let ((data (org-trim (org-export-data obj info))))
2240 (string-match
2241 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2242 data)
2243 (setq output
2244 (concat output
2245 (match-string 1 data)
2246 (let ((blank (org-element-property :post-blank obj)))
2247 (and blank (> blank 0) "\\ "))))))
2248 (plain-text
2249 (setq output
2250 (format "%s\\text{%s}" output (org-export-data obj info))))
2251 (otherwise
2252 (setq output
2253 (concat output
2254 (org-export-data obj info)
2255 (let ((blank (org-element-property :post-blank obj)))
2256 (and blank (> blank 0) "\\ ")))))))
2257 info nil org-element-recursive-objects)
2258 ;; Result. Do not wrap into math mode if already in a subscript
2259 ;; or superscript. Do not wrap into curly brackets if OUTPUT is
2260 ;; a single character. Also merge consecutive subscript and
2261 ;; superscript into the same math snippet.
2262 (concat (and (not in-script-p)
2263 (let ((prev (org-export-get-previous-element object info)))
2264 (or (not prev)
2265 (not (eq (org-element-type prev)
2266 (if (eq type 'subscript) 'superscript
2267 'subscript)))
2268 (let ((blank (org-element-property :post-blank prev)))
2269 (and blank (> blank 0)))))
2270 "$")
2271 (if (eq (org-element-type object) 'subscript) "_" "^")
2272 (and (> (length output) 1) "{")
2273 output
2274 (and (> (length output) 1) "}")
2275 (and (not in-script-p)
2276 (or (let ((blank (org-element-property :post-blank object)))
2277 (and blank (> blank 0)))
2278 (not (eq (org-element-type
2279 (org-export-get-next-element object info))
2280 (if (eq type 'subscript) 'superscript
2281 'subscript))))
2282 "$"))))
2284 (defun org-latex-subscript (subscript contents info)
2285 "Transcode a SUBSCRIPT object from Org to LaTeX.
2286 CONTENTS is the contents of the object. INFO is a plist holding
2287 contextual information."
2288 (org-latex--script-size subscript info))
2291 ;;;; Superscript
2293 (defun org-latex-superscript (superscript contents info)
2294 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2295 CONTENTS is the contents of the object. INFO is a plist holding
2296 contextual information."
2297 (org-latex--script-size superscript info))
2300 ;;;; Table
2302 ;; `org-latex-table' is the entry point for table transcoding. It
2303 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2304 ;; delegates the job to either `org-latex--table.el-table',
2305 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2306 ;; depending of the type of the table and the mode requested.
2308 ;; `org-latex--align-string' is a subroutine used to build alignment
2309 ;; string for Org tables.
2311 (defun org-latex-table (table contents info)
2312 "Transcode a TABLE element from Org to LaTeX.
2313 CONTENTS is the contents of the table. INFO is a plist holding
2314 contextual information."
2315 (if (eq (org-element-property :type table) 'table.el)
2316 ;; "table.el" table. Convert it using appropriate tools.
2317 (org-latex--table.el-table table info)
2318 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2319 org-latex-default-table-mode)))
2320 (cond
2321 ;; Case 1: Verbatim table.
2322 ((string= type "verbatim")
2323 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2324 ;; Re-create table, without affiliated keywords.
2325 (org-trim (org-element-interpret-data
2326 `(table nil ,@(org-element-contents table))))))
2327 ;; Case 2: Matrix.
2328 ((or (string= type "math") (string= type "inline-math"))
2329 (org-latex--math-table table info))
2330 ;; Case 3: Standard table.
2331 (t (concat (org-latex--org-table table contents info)
2332 ;; When there are footnote references within the
2333 ;; table, insert their definition just after it.
2334 (org-latex--delayed-footnotes-definitions table info)))))))
2336 (defun org-latex--align-string (table info)
2337 "Return an appropriate LaTeX alignment string.
2338 TABLE is the considered table. INFO is a plist used as
2339 a communication channel."
2340 (or (org-export-read-attribute :attr_latex table :align)
2341 (let (align)
2342 ;; Extract column groups and alignment from first (non-rule)
2343 ;; row.
2344 (org-element-map
2345 (org-element-map table 'table-row
2346 (lambda (row)
2347 (and (eq (org-element-property :type row) 'standard) row))
2348 info 'first-match)
2349 'table-cell
2350 (lambda (cell)
2351 (let ((borders (org-export-table-cell-borders cell info)))
2352 ;; Check left border for the first cell only.
2353 (when (and (memq 'left borders) (not align))
2354 (push "|" align))
2355 (push (case (org-export-table-cell-alignment cell info)
2356 (left "l")
2357 (right "r")
2358 (center "c"))
2359 align)
2360 (when (memq 'right borders) (push "|" align))))
2361 info)
2362 (apply 'concat (nreverse align)))))
2364 (defun org-latex--org-table (table contents info)
2365 "Return appropriate LaTeX code for an Org table.
2367 TABLE is the table type element to transcode. CONTENTS is its
2368 contents, as a string. INFO is a plist used as a communication
2369 channel.
2371 This function assumes TABLE has `org' as its `:type' property and
2372 `table' as its `:mode' attribute."
2373 (let* ((caption (org-latex--caption/label-string table info))
2374 (attr (org-export-read-attribute :attr_latex table))
2375 ;; Determine alignment string.
2376 (alignment (org-latex--align-string table info))
2377 ;; Determine environment for the table: longtable, tabular...
2378 (table-env (or (plist-get attr :environment)
2379 org-latex-default-table-environment))
2380 ;; If table is a float, determine environment: table, table*
2381 ;; or sidewaystable.
2382 (float-env (unless (member table-env '("longtable" "longtabu"))
2383 (let ((float (plist-get attr :float)))
2384 (cond
2385 ((and (not float) (plist-member attr :float)) nil)
2386 ((string= float "sidewaystable") "sidewaystable")
2387 ((string= float "multicolumn") "table*")
2388 ((or float
2389 (org-element-property :caption table)
2390 (org-string-nw-p (plist-get attr :caption)))
2391 "table")))))
2392 ;; Extract others display options.
2393 (fontsize (let ((font (plist-get attr :font)))
2394 (and font (concat font "\n"))))
2395 (width (plist-get attr :width))
2396 (spreadp (plist-get attr :spread))
2397 (placement (or (plist-get attr :placement)
2398 (format "[%s]" org-latex-default-figure-position)))
2399 (centerp (if (plist-member attr :center) (plist-get attr :center)
2400 org-latex-tables-centered)))
2401 ;; Prepare the final format string for the table.
2402 (cond
2403 ;; Longtable.
2404 ((equal "longtable" table-env)
2405 (concat (and fontsize (concat "{" fontsize))
2406 (format "\\begin{longtable}{%s}\n" alignment)
2407 (and org-latex-table-caption-above
2408 (org-string-nw-p caption)
2409 (concat caption "\\\\\n"))
2410 contents
2411 (and (not org-latex-table-caption-above)
2412 (org-string-nw-p caption)
2413 (concat caption "\\\\\n"))
2414 "\\end{longtable}\n"
2415 (and fontsize "}")))
2416 ;; Longtabu
2417 ((equal "longtabu" table-env)
2418 (concat (and fontsize (concat "{" fontsize))
2419 (format "\\begin{longtabu}%s{%s}\n"
2420 (if width
2421 (format " %s %s "
2422 (if spreadp "spread" "to") width) "")
2423 alignment)
2424 (and org-latex-table-caption-above
2425 (org-string-nw-p caption)
2426 (concat caption "\\\\\n"))
2427 contents
2428 (and (not org-latex-table-caption-above)
2429 (org-string-nw-p caption)
2430 (concat caption "\\\\\n"))
2431 "\\end{longtabu}\n"
2432 (and fontsize "}")))
2433 ;; Others.
2434 (t (concat (cond
2435 (float-env
2436 (concat (format "\\begin{%s}%s\n" float-env placement)
2437 (if org-latex-table-caption-above caption "")
2438 (when centerp "\\centering\n")
2439 fontsize))
2440 (centerp (concat "\\begin{center}\n" fontsize))
2441 (fontsize (concat "{" fontsize)))
2442 (cond ((equal "tabu" table-env)
2443 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2444 (if width (format
2445 (if spreadp " spread %s " " to %s ")
2446 width) "")
2447 alignment
2448 contents))
2449 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2450 table-env
2451 (if width (format "{%s}" width) "")
2452 alignment
2453 contents
2454 table-env)))
2455 (cond
2456 (float-env
2457 (concat (if org-latex-table-caption-above "" caption)
2458 (format "\n\\end{%s}" float-env)))
2459 (centerp "\n\\end{center}")
2460 (fontsize "}")))))))
2462 (defun org-latex--table.el-table (table info)
2463 "Return appropriate LaTeX code for a table.el table.
2465 TABLE is the table type element to transcode. INFO is a plist
2466 used as a communication channel.
2468 This function assumes TABLE has `table.el' as its `:type'
2469 property."
2470 (require 'table)
2471 ;; Ensure "*org-export-table*" buffer is empty.
2472 (with-current-buffer (get-buffer-create "*org-export-table*")
2473 (erase-buffer))
2474 (let ((output (with-temp-buffer
2475 (insert (org-element-property :value table))
2476 (goto-char 1)
2477 (re-search-forward "^[ \t]*|[^|]" nil t)
2478 (table-generate-source 'latex "*org-export-table*")
2479 (with-current-buffer "*org-export-table*"
2480 (org-trim (buffer-string))))))
2481 (kill-buffer (get-buffer "*org-export-table*"))
2482 ;; Remove left out comments.
2483 (while (string-match "^%.*\n" output)
2484 (setq output (replace-match "" t t output)))
2485 (let ((attr (org-export-read-attribute :attr_latex table)))
2486 (when (plist-get attr :rmlines)
2487 ;; When the "rmlines" attribute is provided, remove all hlines
2488 ;; but the the one separating heading from the table body.
2489 (let ((n 0) (pos 0))
2490 (while (and (< (length output) pos)
2491 (setq pos (string-match "^\\\\hline\n?" output pos)))
2492 (incf n)
2493 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2494 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2495 org-latex-tables-centered)))
2496 (if (not centerp) output
2497 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2499 (defun org-latex--math-table (table info)
2500 "Return appropriate LaTeX code for a matrix.
2502 TABLE is the table type element to transcode. INFO is a plist
2503 used as a communication channel.
2505 This function assumes TABLE has `org' as its `:type' property and
2506 `inline-math' or `math' as its `:mode' attribute.."
2507 (let* ((caption (org-latex--caption/label-string table info))
2508 (attr (org-export-read-attribute :attr_latex table))
2509 (inlinep (equal (plist-get attr :mode) "inline-math"))
2510 (env (or (plist-get attr :environment)
2511 org-latex-default-table-environment))
2512 (contents
2513 (mapconcat
2514 (lambda (row)
2515 ;; Ignore horizontal rules.
2516 (when (eq (org-element-property :type row) 'standard)
2517 ;; Return each cell unmodified.
2518 (concat
2519 (mapconcat
2520 (lambda (cell)
2521 (substring (org-element-interpret-data cell) 0 -1))
2522 (org-element-map row 'table-cell 'identity info) "&")
2523 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2524 "\n")))
2525 (org-element-map table 'table-row 'identity info) ""))
2526 ;; Variables related to math clusters (contiguous math tables
2527 ;; of the same type).
2528 (mode (org-export-read-attribute :attr_latex table :mode))
2529 (prev (org-export-get-previous-element table info))
2530 (next (org-export-get-next-element table info))
2531 (same-mode-p
2532 (lambda (table)
2533 ;; Non-nil when TABLE has the same mode as current table.
2534 (string= (or (org-export-read-attribute :attr_latex table :mode)
2535 org-latex-default-table-mode)
2536 mode))))
2537 (concat
2538 ;; Opening string. If TABLE is in the middle of a table cluster,
2539 ;; do not insert any.
2540 (cond ((and prev
2541 (eq (org-element-type prev) 'table)
2542 (memq (org-element-property :post-blank prev) '(0 nil))
2543 (funcall same-mode-p prev))
2544 nil)
2545 (inlinep "\\(")
2546 ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
2547 (t "\\["))
2548 ;; Prefix.
2549 (or (plist-get attr :math-prefix) "")
2550 ;; Environment. Also treat special cases.
2551 (cond ((equal env "array")
2552 (let ((align (org-latex--align-string table info)))
2553 (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
2554 ((assoc env org-latex-table-matrix-macros)
2555 (format "\\%s%s{\n%s}"
2557 (or (plist-get attr :math-arguments) "")
2558 contents))
2559 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2560 ;; Suffix.
2561 (or (plist-get attr :math-suffix) "")
2562 ;; Closing string. If TABLE is in the middle of a table cluster,
2563 ;; do not insert any. If it closes such a cluster, be sure to
2564 ;; close the cluster with a string matching the opening string.
2565 (cond ((and next
2566 (eq (org-element-type next) 'table)
2567 (memq (org-element-property :post-blank table) '(0 nil))
2568 (funcall same-mode-p next))
2569 nil)
2570 (inlinep "\\)")
2571 ;; Find cluster beginning to know which environment to use.
2572 ((let ((cluster-beg table) prev)
2573 (while (and (setq prev (org-export-get-previous-element
2574 cluster-beg info))
2575 (memq (org-element-property :post-blank prev)
2576 '(0 nil))
2577 (funcall same-mode-p prev))
2578 (setq cluster-beg prev))
2579 (and (or (org-element-property :caption cluster-beg)
2580 (org-element-property :name cluster-beg))
2581 "\n\\end{equation}")))
2582 (t "\\]")))))
2585 ;;;; Table Cell
2587 (defun org-latex-table-cell (table-cell contents info)
2588 "Transcode a TABLE-CELL element from Org to LaTeX.
2589 CONTENTS is the cell contents. INFO is a plist used as
2590 a communication channel."
2591 (concat (if (and contents
2592 org-latex-table-scientific-notation
2593 (string-match orgtbl-exp-regexp contents))
2594 ;; Use appropriate format string for scientific
2595 ;; notation.
2596 (format org-latex-table-scientific-notation
2597 (match-string 1 contents)
2598 (match-string 2 contents))
2599 contents)
2600 (when (org-export-get-next-element table-cell info) " & ")))
2603 ;;;; Table Row
2605 (defun org-latex-table-row (table-row contents info)
2606 "Transcode a TABLE-ROW element from Org to LaTeX.
2607 CONTENTS is the contents of the row. INFO is a plist used as
2608 a communication channel."
2609 ;; Rules are ignored since table separators are deduced from
2610 ;; borders of the current row.
2611 (when (eq (org-element-property :type table-row) 'standard)
2612 (let* ((attr (org-export-read-attribute :attr_latex
2613 (org-export-get-parent table-row)))
2614 (longtablep (member (or (plist-get attr :environment)
2615 org-latex-default-table-environment)
2616 '("longtable" "longtabu")))
2617 (booktabsp (if (plist-member attr :booktabs)
2618 (plist-get attr :booktabs)
2619 org-latex-tables-booktabs))
2620 ;; TABLE-ROW's borders are extracted from its first cell.
2621 (borders (org-export-table-cell-borders
2622 (car (org-element-contents table-row)) info)))
2623 (concat
2624 ;; When BOOKTABS are activated enforce top-rule even when no
2625 ;; hline was specifically marked.
2626 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2627 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2628 contents "\\\\\n"
2629 (cond
2630 ;; Special case for long tables. Define header and footers.
2631 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2632 (format "%s
2633 \\endfirsthead
2634 \\multicolumn{%d}{l}{%s} \\\\
2636 %s \\\\\n
2638 \\endhead
2639 %s\\multicolumn{%d}{r}{%s} \\\\
2640 \\endfoot
2641 \\endlastfoot"
2642 (if booktabsp "\\midrule" "\\hline")
2643 (cdr (org-export-table-dimensions
2644 (org-export-get-parent-table table-row) info))
2645 (org-latex--translate "Continued from previous page" info)
2646 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2647 ((and (memq 'top borders)
2648 (memq 'above borders)) "\\hline\n")
2649 (t ""))
2650 contents
2651 (if booktabsp "\\midrule" "\\hline")
2652 (if booktabsp "\\midrule" "\\hline")
2653 ;; Number of columns.
2654 (cdr (org-export-table-dimensions
2655 (org-export-get-parent-table table-row) info))
2656 (org-latex--translate "Continued on next page" info)))
2657 ;; When BOOKTABS are activated enforce bottom rule even when
2658 ;; no hline was specifically marked.
2659 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2660 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2661 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2664 ;;;; Target
2666 (defun org-latex-target (target contents info)
2667 "Transcode a TARGET object from Org to LaTeX.
2668 CONTENTS is nil. INFO is a plist holding contextual
2669 information."
2670 (format "\\label{%s}"
2671 (org-export-solidify-link-text (org-element-property :value target))))
2674 ;;;; Timestamp
2676 (defun org-latex-timestamp (timestamp contents info)
2677 "Transcode a TIMESTAMP object from Org to LaTeX.
2678 CONTENTS is nil. INFO is a plist holding contextual
2679 information."
2680 (let ((value (org-latex-plain-text
2681 (org-timestamp-translate timestamp) info)))
2682 (case (org-element-property :type timestamp)
2683 ((active active-range) (format org-latex-active-timestamp-format value))
2684 ((inactive inactive-range)
2685 (format org-latex-inactive-timestamp-format value))
2686 (otherwise (format org-latex-diary-timestamp-format value)))))
2689 ;;;; Underline
2691 (defun org-latex-underline (underline contents info)
2692 "Transcode UNDERLINE from Org to LaTeX.
2693 CONTENTS is the text with underline markup. INFO is a plist
2694 holding contextual information."
2695 (org-latex--text-markup contents 'underline))
2698 ;;;; Verbatim
2700 (defun org-latex-verbatim (verbatim contents info)
2701 "Transcode a VERBATIM object from Org to LaTeX.
2702 CONTENTS is nil. INFO is a plist used as a communication
2703 channel."
2704 (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2707 ;;;; Verse Block
2709 (defun org-latex-verse-block (verse-block contents info)
2710 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2711 CONTENTS is verse block contents. INFO is a plist holding
2712 contextual information."
2713 (org-latex--wrap-label
2714 verse-block
2715 ;; In a verse environment, add a line break to each newline
2716 ;; character and change each white space at beginning of a line
2717 ;; into a space of 1 em. Also change each blank line with
2718 ;; a vertical space of 1 em.
2719 (progn
2720 (setq contents (replace-regexp-in-string
2721 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2722 (replace-regexp-in-string
2723 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2724 (while (string-match "^[ \t]+" contents)
2725 (let ((new-str (format "\\hspace*{%dem}"
2726 (length (match-string 0 contents)))))
2727 (setq contents (replace-match new-str nil t contents))))
2728 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2732 ;;; End-user functions
2734 ;;;###autoload
2735 (defun org-latex-export-as-latex
2736 (&optional async subtreep visible-only body-only ext-plist)
2737 "Export current buffer as a LaTeX buffer.
2739 If narrowing is active in the current buffer, only export its
2740 narrowed part.
2742 If a region is active, export that region.
2744 A non-nil optional argument ASYNC means the process should happen
2745 asynchronously. The resulting buffer should be accessible
2746 through the `org-export-stack' interface.
2748 When optional argument SUBTREEP is non-nil, export the sub-tree
2749 at point, extracting information from the headline properties
2750 first.
2752 When optional argument VISIBLE-ONLY is non-nil, don't export
2753 contents of hidden elements.
2755 When optional argument BODY-ONLY is non-nil, only write code
2756 between \"\\begin{document}\" and \"\\end{document}\".
2758 EXT-PLIST, when provided, is a property list with external
2759 parameters overriding Org default settings, but still inferior to
2760 file-local settings.
2762 Export is done in a buffer named \"*Org LATEX Export*\", which
2763 will be displayed when `org-export-show-temporary-export-buffer'
2764 is non-nil."
2765 (interactive)
2766 (org-export-to-buffer 'latex "*Org LATEX Export*"
2767 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
2769 ;;;###autoload
2770 (defun org-latex-convert-region-to-latex ()
2771 "Assume the current region has org-mode syntax, and convert it to LaTeX.
2772 This can be used in any buffer. For example, you can write an
2773 itemized list in org-mode syntax in an LaTeX buffer and use this
2774 command to convert it."
2775 (interactive)
2776 (org-export-replace-region-by 'latex))
2778 ;;;###autoload
2779 (defun org-latex-export-to-latex
2780 (&optional async subtreep visible-only body-only ext-plist)
2781 "Export current buffer to a LaTeX file.
2783 If narrowing is active in the current buffer, only export its
2784 narrowed part.
2786 If a region is active, export that region.
2788 A non-nil optional argument ASYNC means the process should happen
2789 asynchronously. The resulting file should be accessible through
2790 the `org-export-stack' interface.
2792 When optional argument SUBTREEP is non-nil, export the sub-tree
2793 at point, extracting information from the headline properties
2794 first.
2796 When optional argument VISIBLE-ONLY is non-nil, don't export
2797 contents of hidden elements.
2799 When optional argument BODY-ONLY is non-nil, only write code
2800 between \"\\begin{document}\" and \"\\end{document}\".
2802 EXT-PLIST, when provided, is a property list with external
2803 parameters overriding Org default settings, but still inferior to
2804 file-local settings."
2805 (interactive)
2806 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2807 (org-export-to-file 'latex outfile
2808 async subtreep visible-only body-only ext-plist)))
2810 ;;;###autoload
2811 (defun org-latex-export-to-pdf
2812 (&optional async subtreep visible-only body-only ext-plist)
2813 "Export current buffer to LaTeX then process through to PDF.
2815 If narrowing is active in the current buffer, only export its
2816 narrowed part.
2818 If a region is active, export that region.
2820 A non-nil optional argument ASYNC means the process should happen
2821 asynchronously. The resulting file should be accessible through
2822 the `org-export-stack' interface.
2824 When optional argument SUBTREEP is non-nil, export the sub-tree
2825 at point, extracting information from the headline properties
2826 first.
2828 When optional argument VISIBLE-ONLY is non-nil, don't export
2829 contents of hidden elements.
2831 When optional argument BODY-ONLY is non-nil, only write code
2832 between \"\\begin{document}\" and \"\\end{document}\".
2834 EXT-PLIST, when provided, is a property list with external
2835 parameters overriding Org default settings, but still inferior to
2836 file-local settings.
2838 Return PDF file's name."
2839 (interactive)
2840 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2841 (org-export-to-file 'latex outfile
2842 async subtreep visible-only body-only ext-plist
2843 (lambda (file) (org-latex-compile file)))))
2845 (defun org-latex-compile (texfile &optional snippet)
2846 "Compile a TeX file.
2848 TEXFILE is the name of the file being compiled. Processing is
2849 done through the command specified in `org-latex-pdf-process'.
2851 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
2852 file used to preview a LaTeX snippet. In this case, do not
2853 create a log buffer and do not bother removing log files.
2855 Return PDF file name or an error if it couldn't be produced."
2856 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
2857 (full-name (file-truename texfile))
2858 (out-dir (file-name-directory texfile))
2859 ;; Properly set working directory for compilation.
2860 (default-directory (if (file-name-absolute-p texfile)
2861 (file-name-directory full-name)
2862 default-directory))
2863 errors)
2864 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
2865 (save-window-excursion
2866 (cond
2867 ;; A function is provided: Apply it.
2868 ((functionp org-latex-pdf-process)
2869 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
2870 ;; A list is provided: Replace %b, %f and %o with appropriate
2871 ;; values in each command before applying it. Output is
2872 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2873 ((consp org-latex-pdf-process)
2874 (let ((outbuf (and (not snippet)
2875 (get-buffer-create "*Org PDF LaTeX Output*"))))
2876 (mapc
2877 (lambda (command)
2878 (shell-command
2879 (replace-regexp-in-string
2880 "%b" (shell-quote-argument base-name)
2881 (replace-regexp-in-string
2882 "%f" (shell-quote-argument full-name)
2883 (replace-regexp-in-string
2884 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2885 outbuf))
2886 org-latex-pdf-process)
2887 ;; Collect standard errors from output buffer.
2888 (setq errors (and (not snippet) (org-latex--collect-errors outbuf)))))
2889 (t (error "No valid command to process to PDF")))
2890 (let ((pdffile (concat out-dir base-name ".pdf")))
2891 ;; Check for process failure. Provide collected errors if
2892 ;; possible.
2893 (if (not (file-exists-p pdffile))
2894 (error (concat (format "PDF file %s wasn't produced" pdffile)
2895 (when errors (concat ": " errors))))
2896 ;; Else remove log files, when specified, and signal end of
2897 ;; process to user, along with any error encountered.
2898 (when (and (not snippet) org-latex-remove-logfiles)
2899 (dolist (file (directory-files
2900 out-dir t
2901 (concat (regexp-quote base-name)
2902 "\\(?:\\.[0-9]+\\)?"
2903 "\\."
2904 (regexp-opt org-latex-logfiles-extensions))))
2905 (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