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