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