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