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