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