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