org-src-lang-modes in block demarcation options
[org-mode.git] / lisp / ox-latex.el
blobcc741bf4ab5c2c1406107cee9be7d1ccb1c9c67c
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 (header (nth 1 (assoc class org-latex-classes)))
1083 (document-class-string
1084 (and (stringp header)
1085 (if (not class-options) header
1086 (replace-regexp-in-string
1087 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1088 class-options header t nil 1)))))
1089 (if (not document-class-string)
1090 (user-error "Unknown LaTeX class `%s'" class)
1091 (org-latex-guess-babel-language
1092 (org-latex-guess-inputenc
1093 (org-element-normalize-string
1094 (org-splice-latex-header
1095 document-class-string
1096 org-latex-default-packages-alist
1097 org-latex-packages-alist nil
1098 (concat (org-element-normalize-string
1099 (plist-get info :latex-header))
1100 (plist-get info :latex-header-extra)))))
1101 info)))
1102 ;; Possibly limit depth for headline numbering.
1103 (let ((sec-num (plist-get info :section-numbers)))
1104 (when (integerp sec-num)
1105 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1106 ;; Author.
1107 (let ((author (and (plist-get info :with-author)
1108 (let ((auth (plist-get info :author)))
1109 (and auth (org-export-data auth info)))))
1110 (email (and (plist-get info :with-email)
1111 (org-export-data (plist-get info :email) info))))
1112 (cond ((and author email (not (string= "" email)))
1113 (format "\\author{%s\\thanks{%s}}\n" author email))
1114 ((or author email) (format "\\author{%s}\n" (or author email)))))
1115 ;; Date.
1116 (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
1117 (format "\\date{%s}\n" (org-export-data date info)))
1118 ;; Title
1119 (format "\\title{%s}\n" title)
1120 ;; Hyperref options.
1121 (when (plist-get info :latex-hyperref-p)
1122 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1123 (or (plist-get info :keywords) "")
1124 (or (plist-get info :description) "")
1125 (if (not (plist-get info :with-creator)) ""
1126 (plist-get info :creator))))
1127 ;; Document start.
1128 "\\begin{document}\n\n"
1129 ;; Title command.
1130 (org-element-normalize-string
1131 (cond ((string= "" title) nil)
1132 ((not (stringp org-latex-title-command)) nil)
1133 ((string-match "\\(?:[^%]\\|^\\)%s"
1134 org-latex-title-command)
1135 (format org-latex-title-command title))
1136 (t org-latex-title-command)))
1137 ;; Table of contents.
1138 (let ((depth (plist-get info :with-toc)))
1139 (when depth
1140 (concat (when (wholenump depth)
1141 (format "\\setcounter{tocdepth}{%d}\n" depth))
1142 org-latex-toc-command)))
1143 ;; Document's body.
1144 contents
1145 ;; Creator.
1146 (let ((creator-info (plist-get info :with-creator)))
1147 (cond
1148 ((not creator-info) "")
1149 ((eq creator-info 'comment)
1150 (format "%% %s\n" (plist-get info :creator)))
1151 (t (concat (plist-get info :creator) "\n"))))
1152 ;; Document end.
1153 "\\end{document}")))
1157 ;;; Transcode Functions
1159 ;;;; Bold
1161 (defun org-latex-bold (bold contents info)
1162 "Transcode BOLD from Org to LaTeX.
1163 CONTENTS is the text with bold markup. INFO is a plist holding
1164 contextual information."
1165 (org-latex--text-markup contents 'bold))
1168 ;;;; Center Block
1170 (defun org-latex-center-block (center-block contents info)
1171 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1172 CONTENTS holds the contents of the center block. INFO is a plist
1173 holding contextual information."
1174 (org-latex--wrap-label
1175 center-block
1176 (format "\\begin{center}\n%s\\end{center}" contents)))
1179 ;;;; Clock
1181 (defun org-latex-clock (clock contents info)
1182 "Transcode a CLOCK element from Org to LaTeX.
1183 CONTENTS is nil. INFO is a plist holding contextual
1184 information."
1185 (concat
1186 "\\noindent"
1187 (format "\\textbf{%s} " org-clock-string)
1188 (format org-latex-inactive-timestamp-format
1189 (concat (org-translate-time
1190 (org-element-property :raw-value
1191 (org-element-property :value clock)))
1192 (let ((time (org-element-property :duration clock)))
1193 (and time (format " (%s)" time)))))
1194 "\\\\"))
1197 ;;;; Code
1199 (defun org-latex-code (code contents info)
1200 "Transcode a CODE object from Org to LaTeX.
1201 CONTENTS is nil. INFO is a plist used as a communication
1202 channel."
1203 (org-latex--text-markup (org-element-property :value code) 'code))
1206 ;;;; Drawer
1208 (defun org-latex-drawer (drawer contents info)
1209 "Transcode a DRAWER element from Org to LaTeX.
1210 CONTENTS holds the contents of the block. INFO is a plist
1211 holding contextual information."
1212 (let* ((name (org-element-property :drawer-name drawer))
1213 (output (if (functionp org-latex-format-drawer-function)
1214 (funcall org-latex-format-drawer-function
1215 name contents)
1216 ;; If there's no user defined function: simply
1217 ;; display contents of the drawer.
1218 contents)))
1219 (org-latex--wrap-label drawer output)))
1222 ;;;; Dynamic Block
1224 (defun org-latex-dynamic-block (dynamic-block contents info)
1225 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1226 CONTENTS holds the contents of the block. INFO is a plist
1227 holding contextual information. See `org-export-data'."
1228 (org-latex--wrap-label dynamic-block contents))
1231 ;;;; Entity
1233 (defun org-latex-entity (entity contents info)
1234 "Transcode an ENTITY object from Org to LaTeX.
1235 CONTENTS are the definition itself. INFO is a plist holding
1236 contextual information."
1237 (let ((ent (org-element-property :latex entity)))
1238 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
1241 ;;;; Example Block
1243 (defun org-latex-example-block (example-block contents info)
1244 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1245 CONTENTS is nil. INFO is a plist holding contextual
1246 information."
1247 (when (org-string-nw-p (org-element-property :value example-block))
1248 (org-latex--wrap-label
1249 example-block
1250 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1251 (org-export-format-code-default example-block info)))))
1254 ;;;; Export Block
1256 (defun org-latex-export-block (export-block contents info)
1257 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1258 CONTENTS is nil. INFO is a plist holding contextual information."
1259 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1260 (org-remove-indentation (org-element-property :value export-block))))
1263 ;;;; Export Snippet
1265 (defun org-latex-export-snippet (export-snippet contents info)
1266 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1267 CONTENTS is nil. INFO is a plist holding contextual information."
1268 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1269 (org-element-property :value export-snippet)))
1272 ;;;; Fixed Width
1274 (defun org-latex-fixed-width (fixed-width contents info)
1275 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1276 CONTENTS is nil. INFO is a plist holding contextual information."
1277 (org-latex--wrap-label
1278 fixed-width
1279 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1280 (org-remove-indentation
1281 (org-element-property :value fixed-width)))))
1284 ;;;; Footnote Reference
1286 (defun org-latex-footnote-reference (footnote-reference contents info)
1287 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1288 CONTENTS is nil. INFO is a plist holding contextual information."
1289 (concat
1290 ;; Insert separator between two footnotes in a row.
1291 (let ((prev (org-export-get-previous-element footnote-reference info)))
1292 (when (eq (org-element-type prev) 'footnote-reference)
1293 org-latex-footnote-separator))
1294 (cond
1295 ;; Use \footnotemark if the footnote has already been defined.
1296 ((not (org-export-footnote-first-reference-p footnote-reference info))
1297 (format "\\footnotemark[%s]{}"
1298 (org-export-get-footnote-number footnote-reference info)))
1299 ;; Use \footnotemark if reference is within another footnote
1300 ;; reference, footnote definition or table cell.
1301 ((loop for parent in (org-export-get-genealogy footnote-reference)
1302 thereis (memq (org-element-type parent)
1303 '(footnote-reference footnote-definition table-cell)))
1304 "\\footnotemark")
1305 ;; Otherwise, define it with \footnote command.
1307 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1308 (concat
1309 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1310 ;; Retrieve all footnote references within the footnote and
1311 ;; add their definition after it, since LaTeX doesn't support
1312 ;; them inside.
1313 (org-latex--delayed-footnotes-definitions def info)))))))
1316 ;;;; Headline
1318 (defun org-latex-headline (headline contents info)
1319 "Transcode a HEADLINE element from Org to LaTeX.
1320 CONTENTS holds the contents of the headline. INFO is a plist
1321 holding contextual information."
1322 (unless (org-element-property :footnote-section-p headline)
1323 (let* ((class (plist-get info :latex-class))
1324 (level (org-export-get-relative-level headline info))
1325 (numberedp (org-export-numbered-headline-p headline info))
1326 (class-sectionning (assoc class org-latex-classes))
1327 ;; Section formatting will set two placeholders: one for
1328 ;; the title and the other for the contents.
1329 (section-fmt
1330 (let ((sec (if (functionp (nth 2 class-sectionning))
1331 (funcall (nth 2 class-sectionning) level numberedp)
1332 (nth (1+ level) class-sectionning))))
1333 (cond
1334 ;; No section available for that LEVEL.
1335 ((not sec) nil)
1336 ;; Section format directly returned by a function. Add
1337 ;; placeholder for contents.
1338 ((stringp sec) (concat sec "\n%s"))
1339 ;; (numbered-section . unnumbered-section)
1340 ((not (consp (cdr sec)))
1341 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1342 ;; (numbered-open numbered-close)
1343 ((= (length sec) 2)
1344 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1345 ;; (num-in num-out no-num-in no-num-out)
1346 ((= (length sec) 4)
1347 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1348 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1349 (text (org-export-data (org-element-property :title headline) info))
1350 (todo
1351 (and (plist-get info :with-todo-keywords)
1352 (let ((todo (org-element-property :todo-keyword headline)))
1353 (and todo (org-export-data todo info)))))
1354 (todo-type (and todo (org-element-property :todo-type headline)))
1355 (tags (and (plist-get info :with-tags)
1356 (org-export-get-tags headline info)))
1357 (priority (and (plist-get info :with-priority)
1358 (org-element-property :priority headline)))
1359 ;; Create the headline text along with a no-tag version.
1360 ;; The latter is required to remove tags from toc.
1361 (full-text (funcall org-latex-format-headline-function
1362 todo todo-type priority text tags))
1363 ;; Associate \label to the headline for internal links.
1364 (headline-label
1365 (format "\\label{sec-%s}\n"
1366 (mapconcat 'number-to-string
1367 (org-export-get-headline-number headline info)
1368 "-")))
1369 (pre-blanks
1370 (make-string (org-element-property :pre-blank headline) 10)))
1371 (if (or (not section-fmt) (org-export-low-level-p headline info))
1372 ;; This is a deep sub-tree: export it as a list item. Also
1373 ;; export as items headlines for which no section format has
1374 ;; been found.
1375 (let ((low-level-body
1376 (concat
1377 ;; If headline is the first sibling, start a list.
1378 (when (org-export-first-sibling-p headline info)
1379 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1380 ;; Itemize headline
1381 "\\item " full-text "\n" headline-label pre-blanks contents)))
1382 ;; If headline is not the last sibling simply return
1383 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1384 ;; any blank line.
1385 (if (not (org-export-last-sibling-p headline info)) low-level-body
1386 (replace-regexp-in-string
1387 "[ \t\n]*\\'"
1388 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1389 low-level-body)))
1390 ;; This is a standard headline. Export it as a section. Add
1391 ;; an alternative heading when possible, and when this is not
1392 ;; identical to the usual heading.
1393 (let ((opt-title
1394 (funcall org-latex-format-headline-function
1395 todo todo-type priority
1396 (org-export-data
1397 (org-export-get-alt-title headline info) info)
1398 (and (eq (plist-get info :with-tags) t) tags))))
1399 (if (and numberedp opt-title
1400 (not (equal opt-title full-text))
1401 (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
1402 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1403 ;; Replace square brackets with parenthesis
1404 ;; since square brackets are not supported in
1405 ;; optional arguments.
1406 (replace-regexp-in-string
1407 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1408 full-text
1409 (concat headline-label pre-blanks contents))
1410 ;; Impossible to add an alternative heading. Fallback to
1411 ;; regular sectioning format string.
1412 (format section-fmt full-text
1413 (concat headline-label pre-blanks contents))))))))
1415 (defun org-latex-format-headline-default-function
1416 (todo todo-type priority text tags)
1417 "Default format function for a headline.
1418 See `org-latex-format-headline-function' for details."
1419 (concat
1420 (and todo (format "{\\bfseries\\sffamily %s} " todo))
1421 (and priority (format "\\framebox{\\#%c} " priority))
1422 text
1423 (and tags
1424 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags ":")))))
1427 ;;;; Horizontal Rule
1429 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1430 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1431 CONTENTS is nil. INFO is a plist holding contextual information."
1432 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1433 (prev (org-export-get-previous-element horizontal-rule info)))
1434 (concat
1435 ;; Make sure the rule doesn't start at the end of the current
1436 ;; line by separating it with a blank line from previous element.
1437 (when (and prev
1438 (let ((prev-blank (org-element-property :post-blank prev)))
1439 (or (not prev-blank) (zerop prev-blank))))
1440 "\n")
1441 (org-latex--wrap-label
1442 horizontal-rule
1443 (format "\\rule{%s}{%s}"
1444 (or (plist-get attr :width) "\\linewidth")
1445 (or (plist-get attr :thickness) "0.5pt"))))))
1448 ;;;; Inline Src Block
1450 (defun org-latex-inline-src-block (inline-src-block contents info)
1451 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1452 CONTENTS holds the contents of the item. INFO is a plist holding
1453 contextual information."
1454 (let* ((code (org-element-property :value inline-src-block))
1455 (separator (org-latex--find-verb-separator code)))
1456 (cond
1457 ;; Do not use a special package: transcode it verbatim.
1458 ((not org-latex-listings)
1459 (concat "\\verb" separator code separator))
1460 ;; Use minted package.
1461 ((eq org-latex-listings 'minted)
1462 (let* ((org-lang (org-element-property :language inline-src-block))
1463 (mint-lang (or (cadr (assq (intern org-lang)
1464 org-latex-minted-langs))
1465 org-lang))
1466 (options (org-latex--make-option-string
1467 org-latex-minted-options)))
1468 (concat (format "\\mint%s{%s}"
1469 (if (string= options "") "" (format "[%s]" options))
1470 mint-lang)
1471 separator code separator)))
1472 ;; Use listings package.
1474 ;; Maybe translate language's name.
1475 (let* ((org-lang (org-element-property :language inline-src-block))
1476 (lst-lang (or (cadr (assq (intern org-lang)
1477 org-latex-listings-langs))
1478 org-lang))
1479 (options (org-latex--make-option-string
1480 (append org-latex-listings-options
1481 `(("language" ,lst-lang))))))
1482 (concat (format "\\lstinline[%s]" options)
1483 separator code separator))))))
1486 ;;;; Inlinetask
1488 (defun org-latex-inlinetask (inlinetask contents info)
1489 "Transcode an INLINETASK element from Org to LaTeX.
1490 CONTENTS holds the contents of the block. INFO is a plist
1491 holding contextual information."
1492 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1493 (todo (and (plist-get info :with-todo-keywords)
1494 (let ((todo (org-element-property :todo-keyword inlinetask)))
1495 (and todo (org-export-data todo info)))))
1496 (todo-type (org-element-property :todo-type inlinetask))
1497 (tags (and (plist-get info :with-tags)
1498 (org-export-get-tags inlinetask info)))
1499 (priority (and (plist-get info :with-priority)
1500 (org-element-property :priority inlinetask))))
1501 ;; If `org-latex-format-inlinetask-function' is provided, call it
1502 ;; with appropriate arguments.
1503 (if (functionp org-latex-format-inlinetask-function)
1504 (funcall org-latex-format-inlinetask-function
1505 todo todo-type priority title tags contents)
1506 ;; Otherwise, use a default template.
1507 (org-latex--wrap-label
1508 inlinetask
1509 (let ((full-title
1510 (concat
1511 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1512 (when priority (format "\\framebox{\\#%c} " priority))
1513 title
1514 (when tags (format "\\hfill{}\\textsc{:%s:}"
1515 (mapconcat 'identity tags ":"))))))
1516 (format (concat "\\begin{center}\n"
1517 "\\fbox{\n"
1518 "\\begin{minipage}[c]{.6\\textwidth}\n"
1519 "%s\n\n"
1520 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1521 "%s"
1522 "\\end{minipage}\n"
1523 "}\n"
1524 "\\end{center}")
1525 full-title contents))))))
1528 ;;;; Italic
1530 (defun org-latex-italic (italic contents info)
1531 "Transcode ITALIC from Org to LaTeX.
1532 CONTENTS is the text with italic markup. INFO is a plist holding
1533 contextual information."
1534 (org-latex--text-markup contents 'italic))
1537 ;;;; Item
1539 (defun org-latex-item (item contents info)
1540 "Transcode an ITEM element from Org to LaTeX.
1541 CONTENTS holds the contents of the item. INFO is a plist holding
1542 contextual information."
1543 (let* ((counter
1544 (let ((count (org-element-property :counter item))
1545 (level
1546 ;; Determine level of current item to determine the
1547 ;; correct LaTeX counter to use (enumi, enumii...).
1548 (let ((parent item) (level 0))
1549 (while (memq (org-element-type
1550 (setq parent (org-export-get-parent parent)))
1551 '(plain-list item))
1552 (when (and (eq (org-element-type parent) 'plain-list)
1553 (eq (org-element-property :type parent)
1554 'ordered))
1555 (incf level)))
1556 level)))
1557 (and count
1558 (< level 5)
1559 (format "\\setcounter{enum%s}{%s}\n"
1560 (nth (1- level) '("i" "ii" "iii" "iv"))
1561 (1- count)))))
1562 (checkbox (case (org-element-property :checkbox item)
1563 (on "$\\boxtimes$ ")
1564 (off "$\\Box$ ")
1565 (trans "$\\boxminus$ ")))
1566 (tag (let ((tag (org-element-property :tag item)))
1567 ;; Check-boxes must belong to the tag.
1568 (and tag (format "[{%s}] "
1569 (concat checkbox
1570 (org-export-data tag info)))))))
1571 (concat counter "\\item" (or tag (concat " " checkbox))
1572 (and contents (org-trim contents))
1573 ;; If there are footnotes references in tag, be sure to
1574 ;; add their definition at the end of the item. This
1575 ;; workaround is necessary since "\footnote{}" command is
1576 ;; not supported in tags.
1577 (and tag
1578 (org-latex--delayed-footnotes-definitions
1579 (org-element-property :tag item) info)))))
1582 ;;;; Keyword
1584 (defun org-latex-keyword (keyword contents info)
1585 "Transcode a KEYWORD element from Org to LaTeX.
1586 CONTENTS is nil. INFO is a plist holding contextual information."
1587 (let ((key (org-element-property :key keyword))
1588 (value (org-element-property :value keyword)))
1589 (cond
1590 ((string= key "LATEX") value)
1591 ((string= key "INDEX") (format "\\index{%s}" value))
1592 ((string= key "TOC")
1593 (let ((value (downcase value)))
1594 (cond
1595 ((string-match "\\<headlines\\>" value)
1596 (let ((depth (or (and (string-match "[0-9]+" value)
1597 (string-to-number (match-string 0 value)))
1598 (plist-get info :with-toc))))
1599 (concat
1600 (when (wholenump depth)
1601 (format "\\setcounter{tocdepth}{%s}\n" depth))
1602 "\\tableofcontents")))
1603 ((string= "tables" value) "\\listoftables")
1604 ((string= "listings" value)
1605 (cond
1606 ((eq org-latex-listings 'minted) "\\listoflistings")
1607 (org-latex-listings "\\lstlistoflistings")
1608 ;; At the moment, src blocks with a caption are wrapped
1609 ;; into a figure environment.
1610 (t "\\listoffigures")))))))))
1613 ;;;; Latex Environment
1615 (defun org-latex-latex-environment (latex-environment contents info)
1616 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1617 CONTENTS is nil. INFO is a plist holding contextual information."
1618 (when (plist-get info :with-latex)
1619 (let ((label (org-element-property :name latex-environment))
1620 (value (org-remove-indentation
1621 (org-element-property :value latex-environment))))
1622 (if (not (org-string-nw-p label)) value
1623 ;; Environment is labelled: label must be within the environment
1624 ;; (otherwise, a reference pointing to that element will count
1625 ;; the section instead).
1626 (with-temp-buffer
1627 (insert value)
1628 (goto-char (point-min))
1629 (forward-line)
1630 (insert
1631 (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1632 (buffer-string))))))
1635 ;;;; Latex Fragment
1637 (defun org-latex-latex-fragment (latex-fragment contents info)
1638 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1639 CONTENTS is nil. INFO is a plist holding contextual information."
1640 (when (plist-get info :with-latex)
1641 (org-element-property :value latex-fragment)))
1644 ;;;; Line Break
1646 (defun org-latex-line-break (line-break contents info)
1647 "Transcode a LINE-BREAK object from Org to LaTeX.
1648 CONTENTS is nil. INFO is a plist holding contextual information."
1649 "\\\\\n")
1652 ;;;; Link
1654 (defun org-latex--inline-image (link info)
1655 "Return LaTeX code for an inline image.
1656 LINK is the link pointing to the inline image. INFO is a plist
1657 used as a communication channel."
1658 (let* ((parent (org-export-get-parent-element link))
1659 (path (let ((raw-path (org-element-property :path link)))
1660 (if (not (file-name-absolute-p raw-path)) raw-path
1661 (expand-file-name raw-path))))
1662 (filetype (file-name-extension path))
1663 (caption (org-latex--caption/label-string parent info))
1664 ;; Retrieve latex attributes from the element around.
1665 (attr (org-export-read-attribute :attr_latex parent))
1666 (float (let ((float (plist-get attr :float)))
1667 (cond ((and (not float) (plist-member attr :float)) nil)
1668 ((string= float "wrap") 'wrap)
1669 ((string= float "multicolumn") 'multicolumn)
1670 ((or float
1671 (org-element-property :caption parent)
1672 (org-string-nw-p (plist-get attr :caption)))
1673 'figure))))
1674 (placement
1675 (let ((place (plist-get attr :placement)))
1676 (cond (place (format "%s" place))
1677 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1678 ((eq float 'figure)
1679 (format "[%s]" org-latex-default-figure-position))
1680 (t ""))))
1681 (comment-include (if (plist-get attr :comment-include) "%" ""))
1682 ;; It is possible to specify width and height in the
1683 ;; ATTR_LATEX line, and also via default variables.
1684 (width (cond ((plist-get attr :width))
1685 ((plist-get attr :height) "")
1686 ((eq float 'wrap) "0.48\\textwidth")
1687 (t org-latex-image-default-width)))
1688 (height (cond ((plist-get attr :height))
1689 ((or (plist-get attr :width)
1690 (memq float '(figure wrap))) "")
1691 (t org-latex-image-default-height)))
1692 (options (let ((opt (or (plist-get attr :options)
1693 org-latex-image-default-option)))
1694 (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
1695 (match-string 1 opt))))
1696 image-code)
1697 (if (member filetype '("tikz" "pgf"))
1698 ;; For tikz images:
1699 ;; - use \input to read in image file.
1700 ;; - if options are present, wrap in a tikzpicture environment.
1701 ;; - if width or height are present, use \resizebox to change
1702 ;; the image size.
1703 (progn
1704 (setq image-code (format "\\input{%s}" path))
1705 (when (org-string-nw-p options)
1706 (setq image-code
1707 (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
1708 options
1709 image-code)))
1710 (when (or (org-string-nw-p width) (org-string-nw-p height))
1711 (setq image-code (format "\\resizebox{%s}{%s}{%s}"
1712 (if (org-string-nw-p width) width "!")
1713 (if (org-string-nw-p height) height "!")
1714 image-code))))
1715 ;; For other images:
1716 ;; - add width and height to options.
1717 ;; - include the image with \includegraphics.
1718 (when (org-string-nw-p width)
1719 (setq options (concat options ",width=" width)))
1720 (when (org-string-nw-p height)
1721 (setq options (concat options ",height=" height)))
1722 (setq image-code
1723 (format "\\includegraphics%s{%s}"
1724 (cond ((not (org-string-nw-p options)) "")
1725 ((= (aref options 0) ?,)
1726 (format "[%s]"(substring options 1)))
1727 (t (format "[%s]" options)))
1728 path)))
1729 ;; Return proper string, depending on FLOAT.
1730 (case float
1731 (wrap (format "\\begin{wrapfigure}%s
1732 \\centering
1733 %s%s
1734 %s\\end{wrapfigure}" placement comment-include image-code caption))
1735 (multicolumn (format "\\begin{figure*}%s
1736 \\centering
1737 %s%s
1738 %s\\end{figure*}" placement comment-include image-code caption))
1739 (figure (format "\\begin{figure}%s
1740 \\centering
1741 %s%s
1742 %s\\end{figure}" placement comment-include image-code caption))
1743 (otherwise image-code))))
1745 (defun org-latex-link (link desc info)
1746 "Transcode a LINK object from Org to LaTeX.
1748 DESC is the description part of the link, or the empty string.
1749 INFO is a plist holding contextual information. See
1750 `org-export-data'."
1751 (let* ((type (org-element-property :type link))
1752 (raw-path (org-element-property :path link))
1753 ;; Ensure DESC really exists, or set it to nil.
1754 (desc (and (not (string= desc "")) desc))
1755 (imagep (org-export-inline-image-p
1756 link org-latex-inline-image-rules))
1757 (path (cond
1758 ((member type '("http" "https" "ftp" "mailto"))
1759 (concat type ":" raw-path))
1760 ((string= type "file")
1761 (if (not (file-name-absolute-p raw-path)) raw-path
1762 (concat "file://" (expand-file-name raw-path))))
1763 (t raw-path)))
1764 protocol)
1765 (cond
1766 ;; Image file.
1767 (imagep (org-latex--inline-image link info))
1768 ;; Radio link: Transcode target's contents and use them as link's
1769 ;; description.
1770 ((string= type "radio")
1771 (let ((destination (org-export-resolve-radio-link link info)))
1772 (when destination
1773 (format "\\hyperref[%s]{%s}"
1774 (org-export-solidify-link-text path)
1775 (org-export-data (org-element-contents destination) info)))))
1776 ;; Links pointing to a headline: Find destination and build
1777 ;; appropriate referencing command.
1778 ((member type '("custom-id" "fuzzy" "id"))
1779 (let ((destination (if (string= type "fuzzy")
1780 (org-export-resolve-fuzzy-link link info)
1781 (org-export-resolve-id-link link info))))
1782 (case (org-element-type destination)
1783 ;; Id link points to an external file.
1784 (plain-text
1785 (if desc (format "\\href{%s}{%s}" destination desc)
1786 (format "\\url{%s}" destination)))
1787 ;; Fuzzy link points nowhere.
1788 ('nil
1789 (format org-latex-link-with-unknown-path-format
1790 (or desc
1791 (org-export-data
1792 (org-element-property :raw-link link) info))))
1793 ;; LINK points to a headline. If headlines are numbered
1794 ;; and the link has no description, display headline's
1795 ;; number. Otherwise, display description or headline's
1796 ;; title.
1797 (headline
1798 (let ((label
1799 (format "sec-%s"
1800 (mapconcat
1801 'number-to-string
1802 (org-export-get-headline-number destination info)
1803 "-"))))
1804 (if (and (plist-get info :section-numbers) (not desc))
1805 (format "\\ref{%s}" label)
1806 (format "\\hyperref[%s]{%s}" label
1807 (or desc
1808 (org-export-data
1809 (org-element-property :title destination) info))))))
1810 ;; Fuzzy link points to a target. Do as above.
1811 (otherwise
1812 (let ((path (org-export-solidify-link-text path)))
1813 (if (not desc) (format "\\ref{%s}" path)
1814 (format "\\hyperref[%s]{%s}" path desc)))))))
1815 ;; Coderef: replace link with the reference name or the
1816 ;; equivalent line number.
1817 ((string= type "coderef")
1818 (format (org-export-get-coderef-format path desc)
1819 (org-export-resolve-coderef path info)))
1820 ;; Link type is handled by a special function.
1821 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1822 (funcall protocol (org-link-unescape path) desc 'latex))
1823 ;; External link with a description part.
1824 ((and path desc) (format "\\href{%s}{%s}" path desc))
1825 ;; External link without a description part.
1826 (path (format "\\url{%s}" path))
1827 ;; No path, only description. Try to do something useful.
1828 (t (format org-latex-link-with-unknown-path-format desc)))))
1831 ;;;; Paragraph
1833 (defun org-latex-paragraph (paragraph contents info)
1834 "Transcode a PARAGRAPH element from Org to LaTeX.
1835 CONTENTS is the contents of the paragraph, as a string. INFO is
1836 the plist used as a communication channel."
1837 contents)
1840 ;;;; Plain List
1842 (defun org-latex-plain-list (plain-list contents info)
1843 "Transcode a PLAIN-LIST element from Org to LaTeX.
1844 CONTENTS is the contents of the list. INFO is a plist holding
1845 contextual information."
1846 (let* ((type (org-element-property :type plain-list))
1847 (attr (org-export-read-attribute :attr_latex plain-list))
1848 (latex-type (let ((env (plist-get attr :environment)))
1849 (cond (env (format "%s" env))
1850 ((eq type 'ordered) "enumerate")
1851 ((eq type 'unordered) "itemize")
1852 ((eq type 'descriptive) "description")))))
1853 (org-latex--wrap-label
1854 plain-list
1855 (format "\\begin{%s}%s\n%s\\end{%s}"
1856 latex-type
1857 ;; Put optional arguments, if any inside square brackets
1858 ;; when necessary.
1859 (let ((options (format "%s" (or (plist-get attr :options) ""))))
1860 (cond ((equal options "") "")
1861 ((string-match "\\`\\[.*\\]\\'" options) options)
1862 (t (format "[%s]" options))))
1863 contents
1864 latex-type))))
1867 ;;;; Plain Text
1869 (defun org-latex-plain-text (text info)
1870 "Transcode a TEXT string from Org to LaTeX.
1871 TEXT is the string to transcode. INFO is a plist holding
1872 contextual information."
1873 (let ((specialp (plist-get info :with-special-strings))
1874 (output text))
1875 ;; Protect %, #, &, $, _, { and }.
1876 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output)
1877 (setq output
1878 (replace-match
1879 (format "\\%s" (match-string 2 output)) nil t output 2)))
1880 ;; Protect ^.
1881 (setq output
1882 (replace-regexp-in-string
1883 "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil 2))
1884 ;; Protect \. If special strings are used, be careful not to
1885 ;; protect "\" in "\-" constructs.
1886 (let ((symbols (if specialp "-%$#&{}^_\\" "%$#&{}^_\\")))
1887 (setq output
1888 (replace-regexp-in-string
1889 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
1890 "$\\backslash$" output nil t 1)))
1891 ;; Protect ~.
1892 (setq output
1893 (replace-regexp-in-string
1894 "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t 2))
1895 ;; Activate smart quotes. Be sure to provide original TEXT string
1896 ;; since OUTPUT may have been modified.
1897 (when (plist-get info :with-smart-quotes)
1898 (setq output (org-export-activate-smart-quotes output :latex info text)))
1899 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1900 (let ((case-fold-search nil)
1901 (start 0))
1902 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
1903 (setq output (replace-match
1904 (format "\\%s{}" (match-string 1 output)) nil t output)
1905 start (match-end 0))))
1906 ;; Convert special strings.
1907 (when specialp
1908 (setq output
1909 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
1910 ;; Handle break preservation if required.
1911 (when (plist-get info :preserve-breaks)
1912 (setq output (replace-regexp-in-string
1913 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
1914 ;; Return value.
1915 output))
1918 ;;;; Planning
1920 (defun org-latex-planning (planning contents info)
1921 "Transcode a PLANNING element from Org to LaTeX.
1922 CONTENTS is nil. INFO is a plist holding contextual
1923 information."
1924 (concat
1925 "\\noindent"
1926 (mapconcat
1927 'identity
1928 (delq nil
1929 (list
1930 (let ((closed (org-element-property :closed planning)))
1931 (when closed
1932 (concat
1933 (format "\\textbf{%s} " org-closed-string)
1934 (format org-latex-inactive-timestamp-format
1935 (org-translate-time
1936 (org-element-property :raw-value closed))))))
1937 (let ((deadline (org-element-property :deadline planning)))
1938 (when deadline
1939 (concat
1940 (format "\\textbf{%s} " org-deadline-string)
1941 (format org-latex-active-timestamp-format
1942 (org-translate-time
1943 (org-element-property :raw-value deadline))))))
1944 (let ((scheduled (org-element-property :scheduled planning)))
1945 (when scheduled
1946 (concat
1947 (format "\\textbf{%s} " org-scheduled-string)
1948 (format org-latex-active-timestamp-format
1949 (org-translate-time
1950 (org-element-property :raw-value scheduled))))))))
1951 " ")
1952 "\\\\"))
1955 ;;;; Quote Block
1957 (defun org-latex-quote-block (quote-block contents info)
1958 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1959 CONTENTS holds the contents of the block. INFO is a plist
1960 holding contextual information."
1961 (org-latex--wrap-label
1962 quote-block
1963 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1966 ;;;; Quote Section
1968 (defun org-latex-quote-section (quote-section contents info)
1969 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1970 CONTENTS is nil. INFO is a plist holding contextual information."
1971 (let ((value (org-remove-indentation
1972 (org-element-property :value quote-section))))
1973 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1976 ;;;; Radio Target
1978 (defun org-latex-radio-target (radio-target text info)
1979 "Transcode a RADIO-TARGET object from Org to LaTeX.
1980 TEXT is the text of the target. INFO is a plist holding
1981 contextual information."
1982 (format "\\label{%s}%s"
1983 (org-export-solidify-link-text
1984 (org-element-property :value radio-target))
1985 text))
1988 ;;;; Section
1990 (defun org-latex-section (section contents info)
1991 "Transcode a SECTION element from Org to LaTeX.
1992 CONTENTS holds the contents of the section. INFO is a plist
1993 holding contextual information."
1994 contents)
1997 ;;;; Special Block
1999 (defun org-latex-special-block (special-block contents info)
2000 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2001 CONTENTS holds the contents of the block. INFO is a plist
2002 holding contextual information."
2003 (let ((type (downcase (org-element-property :type special-block)))
2004 (opt (org-export-read-attribute :attr_latex special-block :options)))
2005 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2006 ;; Insert any label or caption within the block
2007 ;; (otherwise, a reference pointing to that element will
2008 ;; count the section instead).
2009 (org-latex--caption/label-string special-block info)
2010 contents
2011 (format "\\end{%s}" type))))
2014 ;;;; Src Block
2016 (defun org-latex-src-block (src-block contents info)
2017 "Transcode a SRC-BLOCK element from Org to LaTeX.
2018 CONTENTS holds the contents of the item. INFO is a plist holding
2019 contextual information."
2020 (when (org-string-nw-p (org-element-property :value src-block))
2021 (let* ((lang (org-element-property :language src-block))
2022 (caption (org-element-property :caption src-block))
2023 (label (org-element-property :name src-block))
2024 (custom-env (and lang
2025 (cadr (assq (intern lang)
2026 org-latex-custom-lang-environments))))
2027 (num-start (case (org-element-property :number-lines src-block)
2028 (continued (org-export-get-loc src-block info))
2029 (new 0)))
2030 (retain-labels (org-element-property :retain-labels src-block))
2031 (attributes (org-export-read-attribute :attr_latex src-block))
2032 (float (plist-get attributes :float)))
2033 (cond
2034 ;; Case 1. No source fontification.
2035 ((not org-latex-listings)
2036 (let* ((caption-str (org-latex--caption/label-string src-block info))
2037 (float-env
2038 (cond ((and (not float) (plist-member attributes :float)) "%s")
2039 ((string= "multicolumn" float)
2040 (format "\\begin{figure*}[%s]\n%s%%s\n\\end{figure*}"
2041 org-latex-default-figure-position
2042 caption-str))
2043 ((or caption float)
2044 (format "\\begin{figure}[H]\n%s%%s\n\\end{figure}"
2045 caption-str))
2046 (t "%s"))))
2047 (format
2048 float-env
2049 (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
2050 (org-export-format-code-default src-block info))))))
2051 ;; Case 2. Custom environment.
2052 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2053 custom-env
2054 (org-export-format-code-default src-block info)
2055 custom-env))
2056 ;; Case 3. Use minted package.
2057 ((eq org-latex-listings 'minted)
2058 (let* ((caption-str (org-latex--caption/label-string src-block info))
2059 (float-env
2060 (cond ((and (not float) (plist-member attributes :float)) "%s")
2061 ((string= "multicolumn" float)
2062 (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
2063 caption-str))
2064 ((or caption float)
2065 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2066 caption-str))
2067 (t "%s")))
2068 (body
2069 (format
2070 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2071 ;; Options.
2072 (org-latex--make-option-string
2073 (if (or (not num-start)
2074 (assoc "linenos" org-latex-minted-options))
2075 org-latex-minted-options
2076 (append
2077 `(("linenos")
2078 ("firstnumber" ,(number-to-string (1+ num-start))))
2079 org-latex-minted-options)))
2080 ;; Language.
2081 (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
2082 ;; Source code.
2083 (let* ((code-info (org-export-unravel-code src-block))
2084 (max-width
2085 (apply 'max
2086 (mapcar 'length
2087 (org-split-string (car code-info)
2088 "\n")))))
2089 (org-export-format-code
2090 (car code-info)
2091 (lambda (loc num ref)
2092 (concat
2094 (when ref
2095 ;; Ensure references are flushed to the right,
2096 ;; separated with 6 spaces from the widest line
2097 ;; of code.
2098 (concat (make-string (+ (- max-width (length loc)) 6)
2099 ?\s)
2100 (format "(%s)" ref)))))
2101 nil (and retain-labels (cdr code-info)))))))
2102 ;; Return value.
2103 (format float-env body)))
2104 ;; Case 4. Use listings package.
2106 (let ((lst-lang
2107 (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
2108 (caption-str
2109 (when caption
2110 (let ((main (org-export-get-caption src-block))
2111 (secondary (org-export-get-caption src-block t)))
2112 (if (not secondary)
2113 (format "{%s}" (org-export-data main info))
2114 (format "{[%s]%s}"
2115 (org-export-data secondary info)
2116 (org-export-data main info)))))))
2117 (concat
2118 ;; Options.
2119 (format
2120 "\\lstset{%s}\n"
2121 (org-latex--make-option-string
2122 (append
2123 org-latex-listings-options
2124 (cond
2125 ((and (not float) (plist-member attributes :float)) nil)
2126 ((string= "multicolumn" float) '(("float" "*")))
2127 ((and float (not (assoc "float" org-latex-listings-options)))
2128 `(("float" ,org-latex-default-figure-position))))
2129 `(("language" ,lst-lang))
2130 (when label `(("label" ,label)))
2131 (when caption-str `(("caption" ,caption-str)))
2132 (cond ((assoc "numbers" org-latex-listings-options) nil)
2133 ((not num-start) '(("numbers" "none")))
2134 ((zerop num-start) '(("numbers" "left")))
2135 (t `(("numbers" "left")
2136 ("firstnumber"
2137 ,(number-to-string (1+ num-start)))))))))
2138 ;; Source code.
2139 (format
2140 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2141 (let* ((code-info (org-export-unravel-code src-block))
2142 (max-width
2143 (apply 'max
2144 (mapcar 'length
2145 (org-split-string (car code-info) "\n")))))
2146 (org-export-format-code
2147 (car code-info)
2148 (lambda (loc num ref)
2149 (concat
2151 (when ref
2152 ;; Ensure references are flushed to the right,
2153 ;; separated with 6 spaces from the widest line of
2154 ;; code
2155 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2156 (format "(%s)" ref)))))
2157 nil (and retain-labels (cdr code-info))))))))))))
2160 ;;;; Statistics Cookie
2162 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2163 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2164 CONTENTS is nil. INFO is a plist holding contextual information."
2165 (replace-regexp-in-string
2166 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2169 ;;;; Strike-Through
2171 (defun org-latex-strike-through (strike-through contents info)
2172 "Transcode STRIKE-THROUGH from Org to LaTeX.
2173 CONTENTS is the text with strike-through markup. INFO is a plist
2174 holding contextual information."
2175 (org-latex--text-markup contents 'strike-through))
2178 ;;;; Subscript
2180 (defun org-latex--script-size (object info)
2181 "Transcode a subscript or superscript object.
2182 OBJECT is an Org object. INFO is a plist used as a communication
2183 channel."
2184 (let ((in-script-p
2185 ;; Non-nil if object is already in a sub/superscript.
2186 (let ((parent object))
2187 (catch 'exit
2188 (while (setq parent (org-export-get-parent parent))
2189 (let ((type (org-element-type parent)))
2190 (cond ((memq type '(subscript superscript))
2191 (throw 'exit t))
2192 ((memq type org-element-all-elements)
2193 (throw 'exit nil))))))))
2194 (type (org-element-type object))
2195 (output ""))
2196 (org-element-map (org-element-contents object)
2197 (cons 'plain-text org-element-all-objects)
2198 (lambda (obj)
2199 (case (org-element-type obj)
2200 ((entity latex-fragment)
2201 (let ((data (org-trim (org-export-data obj info))))
2202 (string-match
2203 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2204 data)
2205 (setq output
2206 (concat output
2207 (match-string 1 data)
2208 (let ((blank (org-element-property :post-blank obj)))
2209 (and blank (> blank 0) "\\ "))))))
2210 (plain-text
2211 (setq output
2212 (format "%s\\text{%s}" output (org-export-data obj info))))
2213 (otherwise
2214 (setq output
2215 (concat output
2216 (org-export-data obj info)
2217 (let ((blank (org-element-property :post-blank obj)))
2218 (and blank (> blank 0) "\\ ")))))))
2219 info nil org-element-recursive-objects)
2220 ;; Result. Do not wrap into math mode if already in a subscript
2221 ;; or superscript. Do not wrap into curly brackets if OUTPUT is
2222 ;; a single character. Also merge consecutive subscript and
2223 ;; superscript into the same math snippet.
2224 (concat (and (not in-script-p)
2225 (let ((prev (org-export-get-previous-element object info)))
2226 (or (not prev)
2227 (not (eq (org-element-type prev)
2228 (if (eq type 'subscript) 'superscript
2229 'subscript)))
2230 (let ((blank (org-element-property :post-blank prev)))
2231 (and blank (> blank 0)))))
2232 "$")
2233 (if (eq (org-element-type object) 'subscript) "_" "^")
2234 (and (> (length output) 1) "{")
2235 output
2236 (and (> (length output) 1) "}")
2237 (and (not in-script-p)
2238 (or (let ((blank (org-element-property :post-blank object)))
2239 (and blank (> blank 0)))
2240 (not (eq (org-element-type
2241 (org-export-get-next-element object info))
2242 (if (eq type 'subscript) 'superscript
2243 'subscript))))
2244 "$"))))
2246 (defun org-latex-subscript (subscript contents info)
2247 "Transcode a SUBSCRIPT object from Org to LaTeX.
2248 CONTENTS is the contents of the object. INFO is a plist holding
2249 contextual information."
2250 (org-latex--script-size subscript info))
2253 ;;;; Superscript
2255 (defun org-latex-superscript (superscript contents info)
2256 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2257 CONTENTS is the contents of the object. INFO is a plist holding
2258 contextual information."
2259 (org-latex--script-size superscript info))
2262 ;;;; Table
2264 ;; `org-latex-table' is the entry point for table transcoding. It
2265 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2266 ;; delegates the job to either `org-latex--table.el-table',
2267 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2268 ;; depending of the type of the table and the mode requested.
2270 ;; `org-latex--align-string' is a subroutine used to build alignment
2271 ;; string for Org tables.
2273 (defun org-latex-table (table contents info)
2274 "Transcode a TABLE element from Org to LaTeX.
2275 CONTENTS is the contents of the table. INFO is a plist holding
2276 contextual information."
2277 (if (eq (org-element-property :type table) 'table.el)
2278 ;; "table.el" table. Convert it using appropriate tools.
2279 (org-latex--table.el-table table info)
2280 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2281 org-latex-default-table-mode)))
2282 (cond
2283 ;; Case 1: Verbatim table.
2284 ((string= type "verbatim")
2285 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2286 ;; Re-create table, without affiliated keywords.
2287 (org-trim (org-element-interpret-data
2288 `(table nil ,@(org-element-contents table))))))
2289 ;; Case 2: Matrix.
2290 ((or (string= type "math") (string= type "inline-math"))
2291 (org-latex--math-table table info))
2292 ;; Case 3: Standard table.
2293 (t (concat (org-latex--org-table table contents info)
2294 ;; When there are footnote references within the
2295 ;; table, insert their definition just after it.
2296 (org-latex--delayed-footnotes-definitions table info)))))))
2298 (defun org-latex--align-string (table info)
2299 "Return an appropriate LaTeX alignment string.
2300 TABLE is the considered table. INFO is a plist used as
2301 a communication channel."
2302 (or (org-export-read-attribute :attr_latex table :align)
2303 (let (align)
2304 ;; Extract column groups and alignment from first (non-rule)
2305 ;; row.
2306 (org-element-map
2307 (org-element-map table 'table-row
2308 (lambda (row)
2309 (and (eq (org-element-property :type row) 'standard) row))
2310 info 'first-match)
2311 'table-cell
2312 (lambda (cell)
2313 (let ((borders (org-export-table-cell-borders cell info)))
2314 ;; Check left border for the first cell only.
2315 (when (and (memq 'left borders) (not align))
2316 (push "|" align))
2317 (push (case (org-export-table-cell-alignment cell info)
2318 (left "l")
2319 (right "r")
2320 (center "c"))
2321 align)
2322 (when (memq 'right borders) (push "|" align))))
2323 info)
2324 (apply 'concat (nreverse align)))))
2326 (defun org-latex--org-table (table contents info)
2327 "Return appropriate LaTeX code for an Org table.
2329 TABLE is the table type element to transcode. CONTENTS is its
2330 contents, as a string. INFO is a plist used as a communication
2331 channel.
2333 This function assumes TABLE has `org' as its `:type' property and
2334 `table' as its `:mode' attribute."
2335 (let* ((caption (org-latex--caption/label-string table info))
2336 (attr (org-export-read-attribute :attr_latex table))
2337 ;; Determine alignment string.
2338 (alignment (org-latex--align-string table info))
2339 ;; Determine environment for the table: longtable, tabular...
2340 (table-env (or (plist-get attr :environment)
2341 org-latex-default-table-environment))
2342 ;; If table is a float, determine environment: table, table*
2343 ;; or sidewaystable.
2344 (float-env (unless (member table-env '("longtable" "longtabu"))
2345 (let ((float (plist-get attr :float)))
2346 (cond
2347 ((and (not float) (plist-member attr :float)) nil)
2348 ((string= float "sidewaystable") "sidewaystable")
2349 ((string= float "multicolumn") "table*")
2350 ((or float
2351 (org-element-property :caption table)
2352 (org-string-nw-p (plist-get attr :caption)))
2353 "table")))))
2354 ;; Extract others display options.
2355 (fontsize (let ((font (plist-get attr :font)))
2356 (and font (concat font "\n"))))
2357 (width (plist-get attr :width))
2358 (spreadp (plist-get attr :spread))
2359 (placement (or (plist-get attr :placement)
2360 (format "[%s]" org-latex-default-figure-position)))
2361 (centerp (if (plist-member attr :center) (plist-get attr :center)
2362 org-latex-tables-centered)))
2363 ;; Prepare the final format string for the table.
2364 (cond
2365 ;; Longtable.
2366 ((equal "longtable" table-env)
2367 (concat (and fontsize (concat "{" fontsize))
2368 (format "\\begin{longtable}{%s}\n" alignment)
2369 (and org-latex-table-caption-above
2370 (org-string-nw-p caption)
2371 (concat caption "\\\\\n"))
2372 contents
2373 (and (not org-latex-table-caption-above)
2374 (org-string-nw-p caption)
2375 (concat caption "\\\\\n"))
2376 "\\end{longtable}\n"
2377 (and fontsize "}")))
2378 ;; Longtabu
2379 ((equal "longtabu" table-env)
2380 (concat (and fontsize (concat "{" fontsize))
2381 (format "\\begin{longtabu}%s{%s}\n"
2382 (if width
2383 (format " %s %s "
2384 (if spreadp "spread" "to") width) "")
2385 alignment)
2386 (and org-latex-table-caption-above
2387 (org-string-nw-p caption)
2388 (concat caption "\\\\\n"))
2389 contents
2390 (and (not org-latex-table-caption-above)
2391 (org-string-nw-p caption)
2392 (concat caption "\\\\\n"))
2393 "\\end{longtabu}\n"
2394 (and fontsize "}")))
2395 ;; Others.
2396 (t (concat (cond
2397 (float-env
2398 (concat (format "\\begin{%s}%s\n" float-env placement)
2399 (if org-latex-table-caption-above caption "")
2400 (when centerp "\\centering\n")
2401 fontsize))
2402 (centerp (concat "\\begin{center}\n" fontsize))
2403 (fontsize (concat "{" fontsize)))
2404 (cond ((equal "tabu" table-env)
2405 (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
2406 (if width (format
2407 (if spreadp " spread %s " " to %s ")
2408 width) "")
2409 alignment
2410 contents))
2411 (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2412 table-env
2413 (if width (format "{%s}" width) "")
2414 alignment
2415 contents
2416 table-env)))
2417 (cond
2418 (float-env
2419 (concat (if org-latex-table-caption-above "" caption)
2420 (format "\n\\end{%s}" float-env)))
2421 (centerp "\n\\end{center}")
2422 (fontsize "}")))))))
2424 (defun org-latex--table.el-table (table info)
2425 "Return appropriate LaTeX code for a table.el table.
2427 TABLE is the table type element to transcode. INFO is a plist
2428 used as a communication channel.
2430 This function assumes TABLE has `table.el' as its `:type'
2431 property."
2432 (require 'table)
2433 ;; Ensure "*org-export-table*" buffer is empty.
2434 (with-current-buffer (get-buffer-create "*org-export-table*")
2435 (erase-buffer))
2436 (let ((output (with-temp-buffer
2437 (insert (org-element-property :value table))
2438 (goto-char 1)
2439 (re-search-forward "^[ \t]*|[^|]" nil t)
2440 (table-generate-source 'latex "*org-export-table*")
2441 (with-current-buffer "*org-export-table*"
2442 (org-trim (buffer-string))))))
2443 (kill-buffer (get-buffer "*org-export-table*"))
2444 ;; Remove left out comments.
2445 (while (string-match "^%.*\n" output)
2446 (setq output (replace-match "" t t output)))
2447 (let ((attr (org-export-read-attribute :attr_latex table)))
2448 (when (plist-get attr :rmlines)
2449 ;; When the "rmlines" attribute is provided, remove all hlines
2450 ;; but the the one separating heading from the table body.
2451 (let ((n 0) (pos 0))
2452 (while (and (< (length output) pos)
2453 (setq pos (string-match "^\\\\hline\n?" output pos)))
2454 (incf n)
2455 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2456 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2457 org-latex-tables-centered)))
2458 (if (not centerp) output
2459 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2461 (defun org-latex--math-table (table info)
2462 "Return appropriate LaTeX code for a matrix.
2464 TABLE is the table type element to transcode. INFO is a plist
2465 used as a communication channel.
2467 This function assumes TABLE has `org' as its `:type' property and
2468 `inline-math' or `math' as its `:mode' attribute.."
2469 (let* ((caption (org-latex--caption/label-string table info))
2470 (attr (org-export-read-attribute :attr_latex table))
2471 (inlinep (equal (plist-get attr :mode) "inline-math"))
2472 (env (or (plist-get attr :environment)
2473 org-latex-default-table-environment))
2474 (contents
2475 (mapconcat
2476 (lambda (row)
2477 ;; Ignore horizontal rules.
2478 (when (eq (org-element-property :type row) 'standard)
2479 ;; Return each cell unmodified.
2480 (concat
2481 (mapconcat
2482 (lambda (cell)
2483 (substring (org-element-interpret-data cell) 0 -1))
2484 (org-element-map row 'table-cell 'identity info) "&")
2485 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2486 "\n")))
2487 (org-element-map table 'table-row 'identity info) ""))
2488 ;; Variables related to math clusters (contiguous math tables
2489 ;; of the same type).
2490 (mode (org-export-read-attribute :attr_latex table :mode))
2491 (prev (org-export-get-previous-element table info))
2492 (next (org-export-get-next-element table info))
2493 (same-mode-p
2494 (lambda (table)
2495 ;; Non-nil when TABLE has the same mode as current table.
2496 (string= (or (org-export-read-attribute :attr_latex table :mode)
2497 org-latex-default-table-mode)
2498 mode))))
2499 (concat
2500 ;; Opening string. If TABLE is in the middle of a table cluster,
2501 ;; do not insert any.
2502 (cond ((and prev
2503 (eq (org-element-type prev) 'table)
2504 (memq (org-element-property :post-blank prev) '(0 nil))
2505 (funcall same-mode-p prev))
2506 nil)
2507 (inlinep "\\(")
2508 ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
2509 (t "\\["))
2510 ;; Prefix.
2511 (or (plist-get attr :math-prefix) "")
2512 ;; Environment. Also treat special cases.
2513 (cond ((equal env "array")
2514 (let ((align (org-latex--align-string table info)))
2515 (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
2516 ((assoc env org-latex-table-matrix-macros)
2517 (format "\\%s%s{\n%s}"
2519 (or (plist-get attr :math-arguments) "")
2520 contents))
2521 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2522 ;; Suffix.
2523 (or (plist-get attr :math-suffix) "")
2524 ;; Closing string. If TABLE is in the middle of a table cluster,
2525 ;; do not insert any. If it closes such a cluster, be sure to
2526 ;; close the cluster with a string matching the opening string.
2527 (cond ((and next
2528 (eq (org-element-type next) 'table)
2529 (memq (org-element-property :post-blank table) '(0 nil))
2530 (funcall same-mode-p next))
2531 nil)
2532 (inlinep "\\)")
2533 ;; Find cluster beginning to know which environment to use.
2534 ((let ((cluster-beg table) prev)
2535 (while (and (setq prev (org-export-get-previous-element
2536 cluster-beg info))
2537 (memq (org-element-property :post-blank prev)
2538 '(0 nil))
2539 (funcall same-mode-p prev))
2540 (setq cluster-beg prev))
2541 (and (or (org-element-property :caption cluster-beg)
2542 (org-element-property :name cluster-beg))
2543 "\n\\end{equation}")))
2544 (t "\\]")))))
2547 ;;;; Table Cell
2549 (defun org-latex-table-cell (table-cell contents info)
2550 "Transcode a TABLE-CELL element from Org to LaTeX.
2551 CONTENTS is the cell contents. INFO is a plist used as
2552 a communication channel."
2553 (concat (if (and contents
2554 org-latex-table-scientific-notation
2555 (string-match orgtbl-exp-regexp contents))
2556 ;; Use appropriate format string for scientific
2557 ;; notation.
2558 (format org-latex-table-scientific-notation
2559 (match-string 1 contents)
2560 (match-string 2 contents))
2561 contents)
2562 (when (org-export-get-next-element table-cell info) " & ")))
2565 ;;;; Table Row
2567 (defun org-latex-table-row (table-row contents info)
2568 "Transcode a TABLE-ROW element from Org to LaTeX.
2569 CONTENTS is the contents of the row. INFO is a plist used as
2570 a communication channel."
2571 ;; Rules are ignored since table separators are deduced from
2572 ;; borders of the current row.
2573 (when (eq (org-element-property :type table-row) 'standard)
2574 (let* ((attr (org-export-read-attribute :attr_latex
2575 (org-export-get-parent table-row)))
2576 (longtablep (member (or (plist-get attr :environment)
2577 org-latex-default-table-environment)
2578 '("longtable" "longtabu")))
2579 (booktabsp (if (plist-member attr :booktabs)
2580 (plist-get attr :booktabs)
2581 org-latex-tables-booktabs))
2582 ;; TABLE-ROW's borders are extracted from its first cell.
2583 (borders (org-export-table-cell-borders
2584 (car (org-element-contents table-row)) info)))
2585 (concat
2586 ;; When BOOKTABS are activated enforce top-rule even when no
2587 ;; hline was specifically marked.
2588 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2589 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2590 contents "\\\\\n"
2591 (cond
2592 ;; Special case for long tables. Define header and footers.
2593 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2594 (format "%s
2595 \\endhead
2596 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2597 \\endfoot
2598 \\endlastfoot"
2599 (if booktabsp "\\midrule" "\\hline")
2600 (if booktabsp "\\midrule" "\\hline")
2601 ;; Number of columns.
2602 (cdr (org-export-table-dimensions
2603 (org-export-get-parent-table table-row) info))))
2604 ;; When BOOKTABS are activated enforce bottom rule even when
2605 ;; no hline was specifically marked.
2606 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2607 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2608 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2611 ;;;; Target
2613 (defun org-latex-target (target contents info)
2614 "Transcode a TARGET object from Org to LaTeX.
2615 CONTENTS is nil. INFO is a plist holding contextual
2616 information."
2617 (format "\\label{%s}"
2618 (org-export-solidify-link-text (org-element-property :value target))))
2621 ;;;; Timestamp
2623 (defun org-latex-timestamp (timestamp contents info)
2624 "Transcode a TIMESTAMP object from Org to LaTeX.
2625 CONTENTS is nil. INFO is a plist holding contextual
2626 information."
2627 (let ((value (org-latex-plain-text
2628 (org-timestamp-translate timestamp) info)))
2629 (case (org-element-property :type timestamp)
2630 ((active active-range) (format org-latex-active-timestamp-format value))
2631 ((inactive inactive-range)
2632 (format org-latex-inactive-timestamp-format value))
2633 (otherwise (format org-latex-diary-timestamp-format value)))))
2636 ;;;; Underline
2638 (defun org-latex-underline (underline contents info)
2639 "Transcode UNDERLINE from Org to LaTeX.
2640 CONTENTS is the text with underline markup. INFO is a plist
2641 holding contextual information."
2642 (org-latex--text-markup contents 'underline))
2645 ;;;; Verbatim
2647 (defun org-latex-verbatim (verbatim contents info)
2648 "Transcode a VERBATIM object from Org to LaTeX.
2649 CONTENTS is nil. INFO is a plist used as a communication
2650 channel."
2651 (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2654 ;;;; Verse Block
2656 (defun org-latex-verse-block (verse-block contents info)
2657 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2658 CONTENTS is verse block contents. INFO is a plist holding
2659 contextual information."
2660 (org-latex--wrap-label
2661 verse-block
2662 ;; In a verse environment, add a line break to each newline
2663 ;; character and change each white space at beginning of a line
2664 ;; into a space of 1 em. Also change each blank line with
2665 ;; a vertical space of 1 em.
2666 (progn
2667 (setq contents (replace-regexp-in-string
2668 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2669 (replace-regexp-in-string
2670 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2671 (while (string-match "^[ \t]+" contents)
2672 (let ((new-str (format "\\hspace*{%dem}"
2673 (length (match-string 0 contents)))))
2674 (setq contents (replace-match new-str nil t contents))))
2675 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2679 ;;; End-user functions
2681 ;;;###autoload
2682 (defun org-latex-export-as-latex
2683 (&optional async subtreep visible-only body-only ext-plist)
2684 "Export current buffer as a LaTeX buffer.
2686 If narrowing is active in the current buffer, only export its
2687 narrowed part.
2689 If a region is active, export that region.
2691 A non-nil optional argument ASYNC means the process should happen
2692 asynchronously. The resulting buffer should be accessible
2693 through the `org-export-stack' interface.
2695 When optional argument SUBTREEP is non-nil, export the sub-tree
2696 at point, extracting information from the headline properties
2697 first.
2699 When optional argument VISIBLE-ONLY is non-nil, don't export
2700 contents of hidden elements.
2702 When optional argument BODY-ONLY is non-nil, only write code
2703 between \"\\begin{document}\" and \"\\end{document}\".
2705 EXT-PLIST, when provided, is a property list with external
2706 parameters overriding Org default settings, but still inferior to
2707 file-local settings.
2709 Export is done in a buffer named \"*Org LATEX Export*\", which
2710 will be displayed when `org-export-show-temporary-export-buffer'
2711 is non-nil."
2712 (interactive)
2713 (if async
2714 (org-export-async-start
2715 (lambda (output)
2716 (with-current-buffer (get-buffer-create "*Org LATEX Export*")
2717 (erase-buffer)
2718 (insert output)
2719 (goto-char (point-min))
2720 (LaTeX-mode)
2721 (org-export-add-to-stack (current-buffer) 'latex)))
2722 `(org-export-as 'latex ,subtreep ,visible-only ,body-only
2723 ',ext-plist))
2724 (let ((outbuf
2725 (org-export-to-buffer 'latex "*Org LATEX Export*"
2726 subtreep visible-only body-only ext-plist)))
2727 (with-current-buffer outbuf (LaTeX-mode))
2728 (when org-export-show-temporary-export-buffer
2729 (switch-to-buffer-other-window outbuf)))))
2731 ;;;###autoload
2732 (defun org-latex-convert-region-to-latex ()
2733 "Assume the current region has org-mode syntax, and convert it to LaTeX.
2734 This can be used in any buffer. For example, you can write an
2735 itemized list in org-mode syntax in an LaTeX buffer and use this
2736 command to convert it."
2737 (interactive)
2738 (org-export-replace-region-by 'latex))
2740 ;;;###autoload
2741 (defun org-latex-export-to-latex
2742 (&optional async subtreep visible-only body-only ext-plist)
2743 "Export current buffer to a LaTeX file.
2745 If narrowing is active in the current buffer, only export its
2746 narrowed part.
2748 If a region is active, export that region.
2750 A non-nil optional argument ASYNC means the process should happen
2751 asynchronously. The resulting file should be accessible through
2752 the `org-export-stack' interface.
2754 When optional argument SUBTREEP is non-nil, export the sub-tree
2755 at point, extracting information from the headline properties
2756 first.
2758 When optional argument VISIBLE-ONLY is non-nil, don't export
2759 contents of hidden elements.
2761 When optional argument BODY-ONLY is non-nil, only write code
2762 between \"\\begin{document}\" and \"\\end{document}\".
2764 EXT-PLIST, when provided, is a property list with external
2765 parameters overriding Org default settings, but still inferior to
2766 file-local settings.
2768 Return output file's name."
2769 (interactive)
2770 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2771 (if async
2772 (org-export-async-start
2773 (lambda (f) (org-export-add-to-stack f 'latex))
2774 `(expand-file-name
2775 (org-export-to-file
2776 'latex ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))
2777 (org-export-to-file
2778 'latex outfile subtreep visible-only body-only ext-plist))))
2780 ;;;###autoload
2781 (defun org-latex-export-to-pdf
2782 (&optional async subtreep visible-only body-only ext-plist)
2783 "Export current buffer to LaTeX then process through to PDF.
2785 If narrowing is active in the current buffer, only export its
2786 narrowed part.
2788 If a region is active, export that region.
2790 A non-nil optional argument ASYNC means the process should happen
2791 asynchronously. The resulting file should be accessible through
2792 the `org-export-stack' interface.
2794 When optional argument SUBTREEP is non-nil, export the sub-tree
2795 at point, extracting information from the headline properties
2796 first.
2798 When optional argument VISIBLE-ONLY is non-nil, don't export
2799 contents of hidden elements.
2801 When optional argument BODY-ONLY is non-nil, only write code
2802 between \"\\begin{document}\" and \"\\end{document}\".
2804 EXT-PLIST, when provided, is a property list with external
2805 parameters overriding Org default settings, but still inferior to
2806 file-local settings.
2808 Return PDF file's name."
2809 (interactive)
2810 (if async
2811 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2812 (org-export-async-start
2813 (lambda (f) (org-export-add-to-stack f 'latex))
2814 `(expand-file-name
2815 (org-latex-compile
2816 (org-export-to-file
2817 'latex ,outfile ,subtreep ,visible-only ,body-only
2818 ',ext-plist)))))
2819 (org-latex-compile
2820 (org-latex-export-to-latex
2821 nil subtreep visible-only body-only ext-plist))))
2823 (defun org-latex-compile (texfile &optional snippet)
2824 "Compile a TeX file.
2826 TEXFILE is the name of the file being compiled. Processing is
2827 done through the command specified in `org-latex-pdf-process'.
2829 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
2830 file used to preview a LaTeX snippet. In this case, do not
2831 create a log buffer and do not bother removing log files.
2833 Return PDF file name or an error if it couldn't be produced."
2834 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
2835 (full-name (file-truename texfile))
2836 (out-dir (file-name-directory texfile))
2837 ;; Properly set working directory for compilation.
2838 (default-directory (if (file-name-absolute-p texfile)
2839 (file-name-directory full-name)
2840 default-directory))
2841 errors)
2842 (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
2843 (save-window-excursion
2844 (cond
2845 ;; A function is provided: Apply it.
2846 ((functionp org-latex-pdf-process)
2847 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
2848 ;; A list is provided: Replace %b, %f and %o with appropriate
2849 ;; values in each command before applying it. Output is
2850 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2851 ((consp org-latex-pdf-process)
2852 (let ((outbuf (and (not snippet)
2853 (get-buffer-create "*Org PDF LaTeX Output*"))))
2854 (mapc
2855 (lambda (command)
2856 (shell-command
2857 (replace-regexp-in-string
2858 "%b" (shell-quote-argument base-name)
2859 (replace-regexp-in-string
2860 "%f" (shell-quote-argument full-name)
2861 (replace-regexp-in-string
2862 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2863 outbuf))
2864 org-latex-pdf-process)
2865 ;; Collect standard errors from output buffer.
2866 (setq errors (and (not snippet) (org-latex--collect-errors outbuf)))))
2867 (t (error "No valid command to process to PDF")))
2868 (let ((pdffile (concat out-dir base-name ".pdf")))
2869 ;; Check for process failure. Provide collected errors if
2870 ;; possible.
2871 (if (not (file-exists-p pdffile))
2872 (error (concat (format "PDF file %s wasn't produced" pdffile)
2873 (when errors (concat ": " errors))))
2874 ;; Else remove log files, when specified, and signal end of
2875 ;; process to user, along with any error encountered.
2876 (when (and (not snippet) org-latex-remove-logfiles)
2877 (dolist (ext org-latex-logfiles-extensions)
2878 (let ((file (concat out-dir base-name "." ext)))
2879 (when (file-exists-p file) (delete-file file)))))
2880 (message (concat "Process completed"
2881 (if (not errors) "."
2882 (concat " with errors: " errors)))))
2883 ;; Return output file name.
2884 pdffile))))
2886 (defun org-latex--collect-errors (buffer)
2887 "Collect some kind of errors from \"pdflatex\" command output.
2889 BUFFER is the buffer containing output.
2891 Return collected error types as a string, or nil if there was
2892 none."
2893 (with-current-buffer buffer
2894 (save-excursion
2895 (goto-char (point-max))
2896 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
2897 (let ((case-fold-search t)
2898 (errors ""))
2899 (dolist (latex-error org-latex-known-errors)
2900 (when (save-excursion (re-search-forward (car latex-error) nil t))
2901 (setq errors (concat errors " " (cdr latex-error)))))
2902 (and (org-string-nw-p errors) (org-trim errors)))))))
2904 ;;;###autoload
2905 (defun org-latex-publish-to-latex (plist filename pub-dir)
2906 "Publish an Org file to LaTeX.
2908 FILENAME is the filename of the Org file to be published. PLIST
2909 is the property list for the given project. PUB-DIR is the
2910 publishing directory.
2912 Return output file name."
2913 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
2915 ;;;###autoload
2916 (defun org-latex-publish-to-pdf (plist filename pub-dir)
2917 "Publish an Org file to PDF (via LaTeX).
2919 FILENAME is the filename of the Org file to be published. PLIST
2920 is the property list for the given project. PUB-DIR is the
2921 publishing directory.
2923 Return output file name."
2924 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
2925 ;; in working directory and then moved to publishing directory.
2926 (org-publish-attachment
2927 plist
2928 (org-latex-compile (org-publish-org-to 'latex filename ".tex" plist))
2929 pub-dir))
2932 (provide 'ox-latex)
2934 ;; Local variables:
2935 ;; generated-autoload-file: "org-loaddefs.el"
2936 ;; End:
2938 ;;; ox-latex.el ends here