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