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