Merge branch 'maint'
[org-mode.git] / lisp / ox-latex.el
blobf7bb585f356613e390f29854a9026a096c7390d1
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...
413 This command only applies to the table of contents generated with
414 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 :group 'org-export-latex
880 :type '(repeat (string :tag "Extension")))
882 (defcustom org-latex-remove-logfiles t
883 "Non-nil means remove the logfiles produced by PDF production.
884 These are the .aux, .log, .out, and .toc files."
885 :group 'org-export-latex
886 :type 'boolean)
888 (defcustom org-latex-known-errors
889 '(("Reference.*?undefined" . "[undefined reference]")
890 ("Citation.*?undefined" . "[undefined citation]")
891 ("Undefined control sequence" . "[undefined control sequence]")
892 ("^! LaTeX.*?Error" . "[LaTeX error]")
893 ("^! Package.*?Error" . "[package error]")
894 ("Runaway argument" . "Runaway argument"))
895 "Alist of regular expressions and associated messages for the user.
896 The regular expressions are used to find possible errors in the
897 log of a latex-run."
898 :group 'org-export-latex
899 :type '(repeat
900 (cons
901 (string :tag "Regexp")
902 (string :tag "Message"))))
906 ;;; Internal Functions
908 (defun org-latex--caption/label-string (element info)
909 "Return caption and label LaTeX string for ELEMENT.
911 INFO is a plist holding contextual information. If there's no
912 caption nor label, return the empty string.
914 For non-floats, see `org-latex--wrap-label'."
915 (let* ((label (org-element-property :name element))
916 (label-str (if (not (org-string-nw-p label)) ""
917 (format "\\label{%s}"
918 (org-export-solidify-link-text label))))
919 (main (org-export-get-caption element))
920 (short (org-export-get-caption element t)))
921 (cond
922 ((and (not main) (equal label-str "")) "")
923 ((not main) (concat label-str "\n"))
924 ;; Option caption format with short name.
925 (short (format "\\caption[%s]{%s%s}\n"
926 (org-export-data short info)
927 label-str
928 (org-export-data main info)))
929 ;; Standard caption format.
930 (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
932 (defun org-latex-guess-inputenc (header)
933 "Set the coding system in inputenc to what the buffer is.
935 HEADER is the LaTeX header string. This function only applies
936 when specified inputenc option is \"AUTO\".
938 Return the new header, as a string."
939 (let* ((cs (or (ignore-errors
940 (latexenc-coding-system-to-inputenc
941 (or org-export-coding-system buffer-file-coding-system)))
942 "utf8")))
943 (if (not cs) header
944 ;; First translate if that is requested.
945 (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
946 ;; Then find the \usepackage statement and replace the option.
947 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
948 cs header t nil 1))))
950 (defun org-latex-guess-babel-language (header info)
951 "Set Babel's language according to LANGUAGE keyword.
953 HEADER is the LaTeX header string. INFO is the plist used as
954 a communication channel.
956 Insertion of guessed language only happens when Babel package has
957 explicitly been loaded. Then it is added to the rest of
958 package's options.
960 Return the new header."
961 (let ((language-code (plist-get info :language)))
962 ;; If no language is set or Babel package is not loaded, return
963 ;; HEADER as-is.
964 (if (or (not (stringp language-code))
965 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
966 header
967 (let ((options (save-match-data
968 (org-split-string (match-string 1 header) ",")))
969 (language (cdr (assoc language-code
970 org-latex-babel-language-alist))))
971 ;; If LANGUAGE is already loaded, return header. Otherwise,
972 ;; append LANGUAGE to other options.
973 (if (member language options) header
974 (replace-match (mapconcat 'identity
975 (append options (list language))
976 ",")
977 nil nil header 1))))))
979 (defun org-latex--find-verb-separator (s)
980 "Return a character not used in string S.
981 This is used to choose a separator for constructs like \\verb."
982 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
983 (loop for c across ll
984 when (not (string-match (regexp-quote (char-to-string c)) s))
985 return (char-to-string c))))
987 (defun org-latex--make-option-string (options)
988 "Return a comma separated string of keywords and values.
989 OPTIONS is an alist where the key is the options keyword as
990 a string, and the value a list containing the keyword value, or
991 nil."
992 (mapconcat (lambda (pair)
993 (concat (first pair)
994 (when (> (length (second pair)) 0)
995 (concat "=" (second pair)))))
996 options
997 ","))
999 (defun org-latex--wrap-label (element output)
1000 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1001 This function shouldn't be used for floats. See
1002 `org-latex--caption/label-string'."
1003 (let ((label (org-element-property :name element)))
1004 (if (not (and (org-string-nw-p output) (org-string-nw-p label))) output
1005 (concat (format "\\label{%s}\n" (org-export-solidify-link-text label))
1006 output))))
1008 (defun org-latex--text-markup (text markup)
1009 "Format TEXT depending on MARKUP text markup.
1010 See `org-latex-text-markup-alist' for details."
1011 (let ((fmt (cdr (assq markup org-latex-text-markup-alist))))
1012 (cond
1013 ;; No format string: Return raw text.
1014 ((not fmt) text)
1015 ;; Handle the `verb' special case: Find and appropriate separator
1016 ;; and use "\\verb" command.
1017 ((eq 'verb fmt)
1018 (let ((separator (org-latex--find-verb-separator text)))
1019 (concat "\\verb" separator text separator)))
1020 ;; Handle the `protectedtexttt' special case: Protect some
1021 ;; special chars and use "\texttt{%s}" format string.
1022 ((eq 'protectedtexttt fmt)
1023 (let ((start 0)
1024 (trans '(("\\" . "\\textbackslash{}")
1025 ("~" . "\\textasciitilde{}")
1026 ("^" . "\\textasciicircum{}")))
1027 (rtn "")
1028 char)
1029 (while (string-match "[\\{}$%&_#~^]" text)
1030 (setq char (match-string 0 text))
1031 (if (> (match-beginning 0) 0)
1032 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
1033 (setq text (substring text (1+ (match-beginning 0))))
1034 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1035 rtn (concat rtn char)))
1036 (setq text (concat rtn text)
1037 fmt "\\texttt{%s}")
1038 (while (string-match "--" text)
1039 (setq text (replace-match "-{}-" t t text)))
1040 (format fmt text)))
1041 ;; Else use format string.
1042 (t (format fmt text)))))
1044 (defun org-latex--delayed-footnotes-definitions (element info)
1045 "Return footnotes definitions in ELEMENT as a string.
1047 INFO is a plist used as a communication channel.
1049 Footnotes definitions are returned within \"\\footnotetxt{}\"
1050 commands.
1052 This function is used within constructs that don't support
1053 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1054 \"\\footnotemark\" is used within the construct and the function
1055 just outside of it."
1056 (mapconcat
1057 (lambda (ref)
1058 (format
1059 "\\footnotetext[%s]{%s}"
1060 (org-export-get-footnote-number ref info)
1061 (org-trim
1062 (org-export-data
1063 (org-export-get-footnote-definition ref info) info))))
1064 ;; Find every footnote reference in ELEMENT.
1065 (let* (all-refs
1066 search-refs ; For byte-compiler.
1067 (search-refs
1068 (function
1069 (lambda (data)
1070 ;; Return a list of all footnote references never seen
1071 ;; before in DATA.
1072 (org-element-map data 'footnote-reference
1073 (lambda (ref)
1074 (when (org-export-footnote-first-reference-p ref info)
1075 (push ref all-refs)
1076 (when (eq (org-element-property :type ref) 'standard)
1077 (funcall search-refs
1078 (org-export-get-footnote-definition ref info)))))
1079 info)
1080 (reverse all-refs)))))
1081 (funcall search-refs element))
1082 ""))
1086 ;;; Template
1088 (defun org-latex-template (contents info)
1089 "Return complete document string after LaTeX conversion.
1090 CONTENTS is the transcoded contents string. INFO is a plist
1091 holding export options."
1092 (let ((title (org-export-data (plist-get info :title) info)))
1093 (concat
1094 ;; Time-stamp.
1095 (and (plist-get info :time-stamp-file)
1096 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1097 ;; Document class and packages.
1098 (let ((class (plist-get info :latex-class))
1099 (class-options (plist-get info :latex-class-options)))
1100 (org-element-normalize-string
1101 (let* ((header (nth 1 (assoc class org-latex-classes)))
1102 (document-class-string
1103 (and (stringp header)
1104 (if (not class-options) header
1105 (replace-regexp-in-string
1106 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
1107 class-options header t nil 1)))))
1108 (if (not document-class-string)
1109 (user-error "Unknown LaTeX class `%s'" class)
1110 (org-latex-guess-babel-language
1111 (org-latex-guess-inputenc
1112 (org-splice-latex-header
1113 document-class-string
1114 org-latex-default-packages-alist
1115 org-latex-packages-alist nil
1116 (plist-get info :latex-header-extra)))
1117 info)))))
1118 ;; Possibly limit depth for headline numbering.
1119 (let ((sec-num (plist-get info :section-numbers)))
1120 (when (integerp sec-num)
1121 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1122 ;; Author.
1123 (let ((author (and (plist-get info :with-author)
1124 (let ((auth (plist-get info :author)))
1125 (and auth (org-export-data auth info)))))
1126 (email (and (plist-get info :with-email)
1127 (org-export-data (plist-get info :email) info))))
1128 (cond ((and author email (not (string= "" email)))
1129 (format "\\author{%s\\thanks{%s}}\n" author email))
1130 ((or author email) (format "\\author{%s}\n" (or author email)))))
1131 ;; Date.
1132 (let ((date (and (plist-get info :with-date) (plist-get info :date))))
1133 (format "\\date{%s}\n"
1134 (cond ((not date) "")
1135 ;; If `:date' consists in a single timestamp and
1136 ;; `:date-format' is provided, apply it.
1137 ((and (plist-get info :date-format)
1138 (not (cdr date))
1139 (eq (org-element-type (car date)) 'timestamp))
1140 (org-timestamp-format
1141 (car date) (plist-get info :date-format)))
1142 (t (org-export-data date info)))))
1143 ;; Title
1144 (format "\\title{%s}\n" title)
1145 ;; Hyperref options.
1146 (when (plist-get info :latex-hyperref-p)
1147 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1148 (or (plist-get info :keywords) "")
1149 (or (plist-get info :description) "")
1150 (if (not (plist-get info :with-creator)) ""
1151 (plist-get info :creator))))
1152 ;; Document start.
1153 "\\begin{document}\n\n"
1154 ;; Title command.
1155 (org-element-normalize-string
1156 (cond ((string= "" title) nil)
1157 ((not (stringp org-latex-title-command)) nil)
1158 ((string-match "\\(?:[^%]\\|^\\)%s"
1159 org-latex-title-command)
1160 (format org-latex-title-command title))
1161 (t org-latex-title-command)))
1162 ;; Table of contents.
1163 (let ((depth (plist-get info :with-toc)))
1164 (when depth
1165 (concat (when (wholenump depth)
1166 (format "\\setcounter{tocdepth}{%d}\n" depth))
1167 org-latex-toc-command)))
1168 ;; Document's body.
1169 contents
1170 ;; Creator.
1171 (let ((creator-info (plist-get info :with-creator)))
1172 (cond
1173 ((not creator-info) "")
1174 ((eq creator-info 'comment)
1175 (format "%% %s\n" (plist-get info :creator)))
1176 (t (concat (plist-get info :creator) "\n"))))
1177 ;; Document end.
1178 "\\end{document}")))
1182 ;;; Transcode Functions
1184 ;;;; Bold
1186 (defun org-latex-bold (bold contents info)
1187 "Transcode BOLD from Org to LaTeX.
1188 CONTENTS is the text with bold markup. INFO is a plist holding
1189 contextual information."
1190 (org-latex--text-markup contents 'bold))
1193 ;;;; Center Block
1195 (defun org-latex-center-block (center-block contents info)
1196 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1197 CONTENTS holds the contents of the center block. INFO is a plist
1198 holding contextual information."
1199 (org-latex--wrap-label
1200 center-block
1201 (format "\\begin{center}\n%s\\end{center}" contents)))
1204 ;;;; Clock
1206 (defun org-latex-clock (clock contents info)
1207 "Transcode a CLOCK element from Org to LaTeX.
1208 CONTENTS is nil. INFO is a plist holding contextual
1209 information."
1210 (concat
1211 "\\noindent"
1212 (format "\\textbf{%s} " org-clock-string)
1213 (format org-latex-inactive-timestamp-format
1214 (concat (org-translate-time
1215 (org-element-property :raw-value
1216 (org-element-property :value clock)))
1217 (let ((time (org-element-property :duration clock)))
1218 (and time (format " (%s)" time)))))
1219 "\\\\"))
1222 ;;;; Code
1224 (defun org-latex-code (code contents info)
1225 "Transcode a CODE object from Org to LaTeX.
1226 CONTENTS is nil. INFO is a plist used as a communication
1227 channel."
1228 (org-latex--text-markup (org-element-property :value code) 'code))
1231 ;;;; Drawer
1233 (defun org-latex-drawer (drawer contents info)
1234 "Transcode a DRAWER element from Org to LaTeX.
1235 CONTENTS holds the contents of the block. INFO is a plist
1236 holding contextual information."
1237 (let* ((name (org-element-property :drawer-name drawer))
1238 (output (if (functionp org-latex-format-drawer-function)
1239 (funcall org-latex-format-drawer-function
1240 name contents)
1241 ;; If there's no user defined function: simply
1242 ;; display contents of the drawer.
1243 contents)))
1244 (org-latex--wrap-label drawer output)))
1247 ;;;; Dynamic Block
1249 (defun org-latex-dynamic-block (dynamic-block contents info)
1250 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1251 CONTENTS holds the contents of the block. INFO is a plist
1252 holding contextual information. See `org-export-data'."
1253 (org-latex--wrap-label dynamic-block contents))
1256 ;;;; Entity
1258 (defun org-latex-entity (entity contents info)
1259 "Transcode an ENTITY object from Org to LaTeX.
1260 CONTENTS are the definition itself. INFO is a plist holding
1261 contextual information."
1262 (let ((ent (org-element-property :latex entity)))
1263 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
1266 ;;;; Example Block
1268 (defun org-latex-example-block (example-block contents info)
1269 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1270 CONTENTS is nil. INFO is a plist holding contextual
1271 information."
1272 (when (org-string-nw-p (org-element-property :value example-block))
1273 (org-latex--wrap-label
1274 example-block
1275 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1276 (org-export-format-code-default example-block info)))))
1279 ;;;; Export Block
1281 (defun org-latex-export-block (export-block contents info)
1282 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1283 CONTENTS is nil. INFO is a plist holding contextual information."
1284 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1285 (org-remove-indentation (org-element-property :value export-block))))
1288 ;;;; Export Snippet
1290 (defun org-latex-export-snippet (export-snippet contents info)
1291 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1292 CONTENTS is nil. INFO is a plist holding contextual information."
1293 (when (eq (org-export-snippet-backend export-snippet) 'latex)
1294 (org-element-property :value export-snippet)))
1297 ;;;; Fixed Width
1299 (defun org-latex-fixed-width (fixed-width contents info)
1300 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1301 CONTENTS is nil. INFO is a plist holding contextual information."
1302 (org-latex--wrap-label
1303 fixed-width
1304 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1305 (org-remove-indentation
1306 (org-element-property :value fixed-width)))))
1309 ;;;; Footnote Reference
1311 ;; Footnote reference export is handled by
1312 ;; `org-latex-footnote-reference'.
1314 ;; Internally, `org-latex--get-footnote-counter' is used to restore
1315 ;; the value of the LaTeX "footnote" counter after a jump due to
1316 ;; a reference to an already defined footnote. It is only needed in
1317 ;; item tags since the optional argument to \footnotemark is not
1318 ;; allowed there.
1320 (defun org-latex--get-footnote-counter (footnote-reference info)
1321 "Return \"footnote\" counter before FOOTNOTE-REFERENCE is encountered.
1322 INFO is a plist used as a communication channel."
1323 ;; Find original counter value by counting number of footnote
1324 ;; references appearing for the first time before the current
1325 ;; footnote reference.
1326 (let* ((label (org-element-property :label footnote-reference))
1327 seen-refs
1328 search-ref ; For byte-compiler.
1329 (search-ref
1330 (function
1331 (lambda (data)
1332 ;; Search footnote references through DATA, filling
1333 ;; SEEN-REFS along the way.
1334 (org-element-map data 'footnote-reference
1335 (lambda (fn)
1336 (let ((fn-lbl (org-element-property :label fn)))
1337 (cond
1338 ;; Anonymous footnote match: return number.
1339 ((eq fn footnote-reference) (length seen-refs))
1340 ;; Anonymous footnote: it's always a new one.
1341 ;; Also, be sure to return nil from the `cond' so
1342 ;; `first-match' doesn't get us out of the loop.
1343 ((not fn-lbl) (push 'inline seen-refs) nil)
1344 ;; Label not seen so far: add it so SEEN-REFS.
1346 ;; Also search for subsequent references in
1347 ;; footnote definition so numbering follows
1348 ;; reading logic. Note that we don't care about
1349 ;; inline definitions, since `org-element-map'
1350 ;; already traverses them at the right time.
1351 ((not (member fn-lbl seen-refs))
1352 (push fn-lbl seen-refs)
1353 (funcall search-ref
1354 (org-export-get-footnote-definition fn info))))))
1355 ;; Don't enter footnote definitions since it will
1356 ;; happen when their first reference is found.
1357 info 'first-match 'footnote-definition)))))
1358 (funcall search-ref (plist-get info :parse-tree))))
1360 (defun org-latex-footnote-reference (footnote-reference contents info)
1361 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1362 CONTENTS is nil. INFO is a plist holding contextual information."
1363 (concat
1364 ;; Insert separator between two footnotes in a row.
1365 (let ((prev (org-export-get-previous-element footnote-reference info)))
1366 (when (eq (org-element-type prev) 'footnote-reference)
1367 org-latex-footnote-separator))
1368 (cond
1369 ;; Use \footnotemark if reference is within an item's tag.
1370 ((eq (org-element-type (org-export-get-parent-element footnote-reference))
1371 'item)
1372 (if (org-export-footnote-first-reference-p footnote-reference info)
1373 "\\footnotemark"
1374 ;; Since we can't specify footnote number as an optional
1375 ;; argument within an item tag, some extra work has to be done
1376 ;; when the footnote has already been referenced. In that
1377 ;; case, set footnote counter to the desired number, use the
1378 ;; footnotemark, then set counter back to its original value.
1379 (format
1380 "\\setcounter{footnote}{%s}\\footnotemark\\setcounter{footnote}{%s}"
1381 (1- (org-export-get-footnote-number footnote-reference info))
1382 (org-latex--get-footnote-counter footnote-reference info))))
1383 ;; Use \footnotemark if the footnote has already been defined.
1384 ((not (org-export-footnote-first-reference-p footnote-reference info))
1385 (format "\\footnotemark[%s]{}"
1386 (org-export-get-footnote-number footnote-reference info)))
1387 ;; Use \footnotemark if reference is within another footnote
1388 ;; reference, footnote definition or table cell.
1389 ((loop for parent in (org-export-get-genealogy footnote-reference)
1390 thereis (memq (org-element-type parent)
1391 '(footnote-reference footnote-definition table-cell)))
1392 "\\footnotemark")
1393 ;; Otherwise, define it with \footnote command.
1395 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1396 (concat
1397 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1398 ;; Retrieve all footnote references within the footnote and
1399 ;; add their definition after it, since LaTeX doesn't support
1400 ;; them inside.
1401 (org-latex--delayed-footnotes-definitions def info)))))))
1404 ;;;; Headline
1406 (defun org-latex-headline (headline contents info)
1407 "Transcode a HEADLINE element from Org to LaTeX.
1408 CONTENTS holds the contents of the headline. INFO is a plist
1409 holding contextual information."
1410 (unless (org-element-property :footnote-section-p headline)
1411 (let* ((class (plist-get info :latex-class))
1412 (level (org-export-get-relative-level headline info))
1413 (numberedp (org-export-numbered-headline-p headline info))
1414 (class-sectionning (assoc class org-latex-classes))
1415 ;; Section formatting will set two placeholders: one for
1416 ;; the title and the other for the contents.
1417 (section-fmt
1418 (let ((sec (if (functionp (nth 2 class-sectionning))
1419 (funcall (nth 2 class-sectionning) level numberedp)
1420 (nth (1+ level) class-sectionning))))
1421 (cond
1422 ;; No section available for that LEVEL.
1423 ((not sec) nil)
1424 ;; Section format directly returned by a function. Add
1425 ;; placeholder for contents.
1426 ((stringp sec) (concat sec "\n%s"))
1427 ;; (numbered-section . unnumbered-section)
1428 ((not (consp (cdr sec)))
1429 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1430 ;; (numbered-open numbered-close)
1431 ((= (length sec) 2)
1432 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1433 ;; (num-in num-out no-num-in no-num-out)
1434 ((= (length sec) 4)
1435 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1436 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1437 (text (org-export-data (org-element-property :title headline) info))
1438 (todo
1439 (and (plist-get info :with-todo-keywords)
1440 (let ((todo (org-element-property :todo-keyword headline)))
1441 (and todo (org-export-data todo info)))))
1442 (todo-type (and todo (org-element-property :todo-type headline)))
1443 (tags (and (plist-get info :with-tags)
1444 (org-export-get-tags headline info)))
1445 (priority (and (plist-get info :with-priority)
1446 (org-element-property :priority headline)))
1447 ;; Create the headline text along with a no-tag version.
1448 ;; The latter is required to remove tags from toc.
1449 (full-text (funcall org-latex-format-headline-function
1450 todo todo-type priority text tags))
1451 ;; Associate \label to the headline for internal links.
1452 (headline-label
1453 (format "\\label{sec-%s}\n"
1454 (mapconcat 'number-to-string
1455 (org-export-get-headline-number headline info)
1456 "-")))
1457 (pre-blanks
1458 (make-string (org-element-property :pre-blank headline) 10)))
1459 (if (or (not section-fmt) (org-export-low-level-p headline info))
1460 ;; This is a deep sub-tree: export it as a list item. Also
1461 ;; export as items headlines for which no section format has
1462 ;; been found.
1463 (let ((low-level-body
1464 (concat
1465 ;; If headline is the first sibling, start a list.
1466 (when (org-export-first-sibling-p headline info)
1467 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1468 ;; Itemize headline
1469 "\\item " full-text "\n" headline-label pre-blanks contents)))
1470 ;; If headline is not the last sibling simply return
1471 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
1472 ;; any blank line.
1473 (if (not (org-export-last-sibling-p headline info)) low-level-body
1474 (replace-regexp-in-string
1475 "[ \t\n]*\\'"
1476 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1477 low-level-body)))
1478 ;; This is a standard headline. Export it as a section. Add
1479 ;; an alternative heading when possible.
1480 (let ((opt-title
1481 (funcall org-latex-format-headline-function
1482 todo todo-type priority
1483 (org-export-data
1484 (org-export-get-optional-title headline info) info)
1485 (and (eq (plist-get info :with-tags) t) tags))))
1486 (if (and opt-title (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
1487 (format (replace-match "\\1[%s]" nil nil section-fmt 1)
1488 ;; Replace square brackets with parenthesis
1489 ;; since square brackets are not supported in
1490 ;; optional arguments.
1491 (replace-regexp-in-string
1492 "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
1493 full-text
1494 (concat headline-label pre-blanks contents))
1495 ;; Impossible to add an alternative heading. Fallback to
1496 ;; regular sectioning format string.
1497 (format section-fmt full-text
1498 (concat headline-label pre-blanks contents))))))))
1500 (defun org-latex-format-headline-default-function
1501 (todo todo-type priority text tags)
1502 "Default format function for a headline.
1503 See `org-latex-format-headline-function' for details."
1504 (concat
1505 (and todo (format "\\textbf{\\textsc{\\textsf{%s}}} " todo))
1506 (and priority (format "\\framebox{\\#%c} " priority))
1507 text
1508 (and tags
1509 (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags ":")))))
1512 ;;;; Horizontal Rule
1514 (defun org-latex-horizontal-rule (horizontal-rule contents info)
1515 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1516 CONTENTS is nil. INFO is a plist holding contextual information."
1517 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1518 (prev (org-export-get-previous-element horizontal-rule info)))
1519 (concat
1520 ;; Make sure the rule doesn't start at the end of the current
1521 ;; line by separating it with a blank line from previous element.
1522 (when (and prev
1523 (let ((prev-blank (org-element-property :post-blank prev)))
1524 (or (not prev-blank) (zerop prev-blank))))
1525 "\n")
1526 (org-latex--wrap-label
1527 horizontal-rule
1528 (format "\\rule{%s}{%s}"
1529 (or (plist-get attr :width) "\\linewidth")
1530 (or (plist-get attr :thickness) "0.5pt"))))))
1533 ;;;; Inline Src Block
1535 (defun org-latex-inline-src-block (inline-src-block contents info)
1536 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1537 CONTENTS holds the contents of the item. INFO is a plist holding
1538 contextual information."
1539 (let* ((code (org-element-property :value inline-src-block))
1540 (separator (org-latex--find-verb-separator code)))
1541 (cond
1542 ;; Do not use a special package: transcode it verbatim.
1543 ((not org-latex-listings)
1544 (concat "\\verb" separator code separator))
1545 ;; Use minted package.
1546 ((eq org-latex-listings 'minted)
1547 (let* ((org-lang (org-element-property :language inline-src-block))
1548 (mint-lang (or (cadr (assq (intern org-lang)
1549 org-latex-minted-langs))
1550 org-lang))
1551 (options (org-latex--make-option-string
1552 org-latex-minted-options)))
1553 (concat (format "\\mint%s{%s}"
1554 (if (string= options "") "" (format "[%s]" options))
1555 mint-lang)
1556 separator code separator)))
1557 ;; Use listings package.
1559 ;; Maybe translate language's name.
1560 (let* ((org-lang (org-element-property :language inline-src-block))
1561 (lst-lang (or (cadr (assq (intern org-lang)
1562 org-latex-listings-langs))
1563 org-lang))
1564 (options (org-latex--make-option-string
1565 (append org-latex-listings-options
1566 `(("language" ,lst-lang))))))
1567 (concat (format "\\lstinline[%s]" options)
1568 separator code separator))))))
1571 ;;;; Inlinetask
1573 (defun org-latex-inlinetask (inlinetask contents info)
1574 "Transcode an INLINETASK element from Org to LaTeX.
1575 CONTENTS holds the contents of the block. INFO is a plist
1576 holding contextual information."
1577 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1578 (todo (and (plist-get info :with-todo-keywords)
1579 (let ((todo (org-element-property :todo-keyword inlinetask)))
1580 (and todo (org-export-data todo info)))))
1581 (todo-type (org-element-property :todo-type inlinetask))
1582 (tags (and (plist-get info :with-tags)
1583 (org-export-get-tags inlinetask info)))
1584 (priority (and (plist-get info :with-priority)
1585 (org-element-property :priority inlinetask))))
1586 ;; If `org-latex-format-inlinetask-function' is provided, call it
1587 ;; with appropriate arguments.
1588 (if (functionp org-latex-format-inlinetask-function)
1589 (funcall org-latex-format-inlinetask-function
1590 todo todo-type priority title tags contents)
1591 ;; Otherwise, use a default template.
1592 (org-latex--wrap-label
1593 inlinetask
1594 (let ((full-title
1595 (concat
1596 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1597 (when priority (format "\\framebox{\\#%c} " priority))
1598 title
1599 (when tags (format "\\hfill{}\\textsc{:%s:}"
1600 (mapconcat 'identity tags ":"))))))
1601 (format (concat "\\begin{center}\n"
1602 "\\fbox{\n"
1603 "\\begin{minipage}[c]{.6\\textwidth}\n"
1604 "%s\n\n"
1605 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1606 "%s"
1607 "\\end{minipage}\n"
1608 "}\n"
1609 "\\end{center}")
1610 full-title contents))))))
1613 ;;;; Italic
1615 (defun org-latex-italic (italic contents info)
1616 "Transcode ITALIC from Org to LaTeX.
1617 CONTENTS is the text with italic markup. INFO is a plist holding
1618 contextual information."
1619 (org-latex--text-markup contents 'italic))
1622 ;;;; Item
1624 (defun org-latex-item (item contents info)
1625 "Transcode an ITEM element from Org to LaTeX.
1626 CONTENTS holds the contents of the item. INFO is a plist holding
1627 contextual information."
1628 (let* ((counter
1629 (let ((count (org-element-property :counter item))
1630 (level
1631 (loop for parent in (org-export-get-genealogy item)
1632 count (eq (org-element-type parent) 'plain-list)
1633 until (eq (org-element-type parent) 'headline))))
1634 (and count
1635 (< level 5)
1636 (format "\\setcounter{enum%s}{%s}\n"
1637 (nth (1- level) '("i" "ii" "iii" "iv"))
1638 (1- count)))))
1639 (checkbox (case (org-element-property :checkbox item)
1640 (on "$\\boxtimes$ ")
1641 (off "$\\Box$ ")
1642 (trans "$\\boxminus$ ")))
1643 (tag (let ((tag (org-element-property :tag item)))
1644 ;; Check-boxes must belong to the tag.
1645 (and tag (format "[%s] "
1646 (concat checkbox
1647 (org-export-data tag info)))))))
1648 (concat counter "\\item" (or tag (concat " " checkbox))
1649 (and contents (org-trim contents))
1650 ;; If there are footnotes references in tag, be sure to
1651 ;; add their definition at the end of the item. This
1652 ;; workaround is necessary since "\footnote{}" command is
1653 ;; not supported in tags.
1654 (and tag
1655 (org-latex--delayed-footnotes-definitions
1656 (org-element-property :tag item) info)))))
1659 ;;;; Keyword
1661 (defun org-latex-keyword (keyword contents info)
1662 "Transcode a KEYWORD element from Org to LaTeX.
1663 CONTENTS is nil. INFO is a plist holding contextual information."
1664 (let ((key (org-element-property :key keyword))
1665 (value (org-element-property :value keyword)))
1666 (cond
1667 ((string= key "LATEX") value)
1668 ((string= key "INDEX") (format "\\index{%s}" value))
1669 ;; Invisible targets.
1670 ((string= key "TARGET") nil)
1671 ((string= key "TOC")
1672 (let ((value (downcase value)))
1673 (cond
1674 ((string-match "\\<headlines\\>" value)
1675 (let ((depth (or (and (string-match "[0-9]+" value)
1676 (string-to-number (match-string 0 value)))
1677 (plist-get info :with-toc))))
1678 (concat
1679 (when (wholenump depth)
1680 (format "\\setcounter{tocdepth}{%s}\n" depth))
1681 "\\tableofcontents")))
1682 ((string= "tables" value) "\\listoftables")
1683 ((string= "listings" value)
1684 (cond
1685 ((eq org-latex-listings 'minted) "\\listoflistings")
1686 (org-latex-listings "\\lstlistoflistings")
1687 ;; At the moment, src blocks with a caption are wrapped
1688 ;; into a figure environment.
1689 (t "\\listoffigures")))))))))
1692 ;;;; Latex Environment
1694 (defun org-latex-latex-environment (latex-environment contents info)
1695 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1696 CONTENTS is nil. INFO is a plist holding contextual information."
1697 (when (plist-get info :with-latex)
1698 (let ((label (org-element-property :name latex-environment))
1699 (value (org-remove-indentation
1700 (org-element-property :value latex-environment))))
1701 (if (not (org-string-nw-p label)) value
1702 ;; Environment is labelled: label must be within the environment
1703 ;; (otherwise, a reference pointing to that element will count
1704 ;; the section instead).
1705 (with-temp-buffer
1706 (insert value)
1707 (goto-char (point-min))
1708 (forward-line)
1709 (insert
1710 (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1711 (buffer-string))))))
1714 ;;;; Latex Fragment
1716 (defun org-latex-latex-fragment (latex-fragment contents info)
1717 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1718 CONTENTS is nil. INFO is a plist holding contextual information."
1719 (when (plist-get info :with-latex)
1720 (org-element-property :value latex-fragment)))
1723 ;;;; Line Break
1725 (defun org-latex-line-break (line-break contents info)
1726 "Transcode a LINE-BREAK object from Org to LaTeX.
1727 CONTENTS is nil. INFO is a plist holding contextual information."
1728 "\\\\\n")
1731 ;;;; Link
1733 (defun org-latex--inline-image (link info)
1734 "Return LaTeX code for an inline image.
1735 LINK is the link pointing to the inline image. INFO is a plist
1736 used as a communication channel."
1737 (let* ((parent (org-export-get-parent-element link))
1738 (path (let ((raw-path (org-element-property :path link)))
1739 (if (not (file-name-absolute-p raw-path)) raw-path
1740 (expand-file-name raw-path))))
1741 (caption (org-latex--caption/label-string parent info))
1742 ;; Retrieve latex attributes from the element around.
1743 (attr (org-export-read-attribute :attr_latex parent))
1744 (float (let ((float (plist-get attr :float)))
1745 (cond ((string= float "wrap") 'wrap)
1746 ((string= float "multicolumn") 'multicolumn)
1747 ((or (string= float "figure")
1748 (org-element-property :caption parent))
1749 'figure))))
1750 (placement
1751 (let ((place (plist-get attr :placement)))
1752 (cond (place (format "%s" place))
1753 ((eq float 'wrap) "{l}{0.5\\textwidth}")
1754 ((eq float 'figure)
1755 (format "[%s]" org-latex-default-figure-position))
1756 (t ""))))
1757 (comment-include (if (plist-get attr :comment-include) "%" ""))
1758 ;; Options for "includegraphics" macro. Make sure it is
1759 ;; a string with square brackets when non empty. Default to
1760 ;; `org-latex-image-default-option' when possible.
1761 (options (let ((opt (format "%s"
1762 (or (plist-get attr :options)
1763 org-latex-image-default-option))))
1764 (cond ((string-match "\\`\\[.*\\]" opt) opt)
1765 ((org-string-nw-p opt) (format "[%s]" opt))
1766 ((eq float 'float) "[width=0.7\\textwidth]")
1767 ((eq float 'wrap) "[width=0.48\\textwidth]")
1768 (t "")))))
1769 ;; Return proper string, depending on FLOAT.
1770 (case float
1771 (wrap (format "\\begin{wrapfigure}%s
1772 \\centering
1773 %s\\includegraphics%s{%s}
1774 %s\\end{wrapfigure}" placement comment-include options path caption))
1775 (multicolumn (format "\\begin{figure*}%s
1776 \\centering
1777 %s\\includegraphics%s{%s}
1778 %s\\end{figure*}" placement comment-include options path caption))
1779 (figure (format "\\begin{figure}%s
1780 \\centering
1781 %s\\includegraphics%s{%s}
1782 %s\\end{figure}" placement comment-include options path caption))
1783 (t (format "\\includegraphics%s{%s}" options path)))))
1785 (defun org-latex-link (link desc info)
1786 "Transcode a LINK object from Org to LaTeX.
1788 DESC is the description part of the link, or the empty string.
1789 INFO is a plist holding contextual information. See
1790 `org-export-data'."
1791 (let* ((type (org-element-property :type link))
1792 (raw-path (org-element-property :path link))
1793 ;; Ensure DESC really exists, or set it to nil.
1794 (desc (and (not (string= desc "")) desc))
1795 (imagep (org-export-inline-image-p
1796 link org-latex-inline-image-rules))
1797 (path (cond
1798 ((member type '("http" "https" "ftp" "mailto"))
1799 (concat type ":" raw-path))
1800 ((string= type "file")
1801 (if (file-name-absolute-p raw-path)
1802 (concat "file://" (expand-file-name raw-path))
1803 (concat "file://" raw-path)))
1804 (t raw-path)))
1805 protocol)
1806 (cond
1807 ;; Image file.
1808 (imagep (org-latex--inline-image link info))
1809 ;; Radio link: Transcode target's contents and use them as link's
1810 ;; description.
1811 ((string= type "radio")
1812 (let ((destination (org-export-resolve-radio-link link info)))
1813 (when destination
1814 (format "\\hyperref[%s]{%s}"
1815 (org-export-solidify-link-text path)
1816 (org-export-data (org-element-contents destination) info)))))
1817 ;; Links pointing to a headline: Find destination and build
1818 ;; appropriate referencing command.
1819 ((member type '("custom-id" "fuzzy" "id"))
1820 (let ((destination (if (string= type "fuzzy")
1821 (org-export-resolve-fuzzy-link link info)
1822 (org-export-resolve-id-link link info))))
1823 (case (org-element-type destination)
1824 ;; Id link points to an external file.
1825 (plain-text
1826 (if desc (format "\\href{file://%s}{%s}" destination desc)
1827 (format "\\url{file://%s}" destination)))
1828 ;; Fuzzy link points nowhere.
1829 ('nil
1830 (format org-latex-link-with-unknown-path-format
1831 (or desc
1832 (org-export-data
1833 (org-element-property :raw-link link) info))))
1834 ;; Fuzzy link points to an invisible target.
1835 (keyword nil)
1836 ;; LINK points to a headline. If headlines are numbered
1837 ;; and the link has no description, display headline's
1838 ;; number. Otherwise, display description or headline's
1839 ;; title.
1840 (headline
1841 (let ((label
1842 (format "sec-%s"
1843 (mapconcat
1844 'number-to-string
1845 (org-export-get-headline-number destination info)
1846 "-"))))
1847 (if (and (plist-get info :section-numbers) (not desc))
1848 (format "\\ref{%s}" label)
1849 (format "\\hyperref[%s]{%s}" label
1850 (or desc
1851 (org-export-data
1852 (org-element-property :title destination) info))))))
1853 ;; Fuzzy link points to a target. Do as above.
1854 (otherwise
1855 (let ((path (org-export-solidify-link-text path)))
1856 (if (not desc) (format "\\ref{%s}" path)
1857 (format "\\hyperref[%s]{%s}" path desc)))))))
1858 ;; Coderef: replace link with the reference name or the
1859 ;; equivalent line number.
1860 ((string= type "coderef")
1861 (format (org-export-get-coderef-format path desc)
1862 (org-export-resolve-coderef path info)))
1863 ;; Link type is handled by a special function.
1864 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1865 (funcall protocol (org-link-unescape path) desc 'latex))
1866 ;; External link with a description part.
1867 ((and path desc) (format "\\href{%s}{%s}" path desc))
1868 ;; External link without a description part.
1869 (path (format "\\url{%s}" path))
1870 ;; No path, only description. Try to do something useful.
1871 (t (format org-latex-link-with-unknown-path-format desc)))))
1874 ;;;; Paragraph
1876 (defun org-latex-paragraph (paragraph contents info)
1877 "Transcode a PARAGRAPH element from Org to LaTeX.
1878 CONTENTS is the contents of the paragraph, as a string. INFO is
1879 the plist used as a communication channel."
1880 contents)
1883 ;;;; Plain List
1885 (defun org-latex-plain-list (plain-list contents info)
1886 "Transcode a PLAIN-LIST element from Org to LaTeX.
1887 CONTENTS is the contents of the list. INFO is a plist holding
1888 contextual information."
1889 (let* ((type (org-element-property :type plain-list))
1890 (attr (org-export-read-attribute :attr_latex plain-list))
1891 (latex-type (let ((env (plist-get attr :environment)))
1892 (cond (env (format "%s" env))
1893 ((eq type 'ordered) "enumerate")
1894 ((eq type 'unordered) "itemize")
1895 ((eq type 'descriptive) "description")))))
1896 (org-latex--wrap-label
1897 plain-list
1898 (format "\\begin{%s}%s\n%s\\end{%s}"
1899 latex-type
1900 ;; Put optional arguments, if any inside square brackets
1901 ;; when necessary.
1902 (let ((options (format "%s" (or (plist-get attr :options) ""))))
1903 (cond ((equal options "") "")
1904 ((string-match "\\`\\[.*\\]\\'" options) options)
1905 (t (format "[%s]" options))))
1906 contents
1907 latex-type))))
1910 ;;;; Plain Text
1912 (defun org-latex-plain-text (text info)
1913 "Transcode a TEXT string from Org to LaTeX.
1914 TEXT is the string to transcode. INFO is a plist holding
1915 contextual information."
1916 (let ((specialp (plist-get info :with-special-strings))
1917 (output text))
1918 ;; Protect %, #, &, $, ~, ^, _, { and }.
1919 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" output)
1920 (setq output
1921 (replace-match
1922 (format "\\%s" (match-string 2 output)) nil t output 2)))
1923 ;; Protect \. If special strings are used, be careful not to
1924 ;; protect "\" in "\-" constructs.
1925 (let ((symbols (if specialp "-%$#&{}~^_\\" "%$#&{}~^_\\")))
1926 (setq output
1927 (replace-regexp-in-string
1928 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
1929 "$\\backslash$" output nil t 1)))
1930 ;; Activate smart quotes. Be sure to provide original TEXT string
1931 ;; since OUTPUT may have been modified.
1932 (when (plist-get info :with-smart-quotes)
1933 (setq output (org-export-activate-smart-quotes output :latex info text)))
1934 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1935 (let ((case-fold-search nil)
1936 (start 0))
1937 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
1938 (setq output (replace-match
1939 (format "\\%s{}" (match-string 1 output)) nil t output)
1940 start (match-end 0))))
1941 ;; Convert special strings.
1942 (when specialp
1943 (setq output
1944 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
1945 ;; Handle break preservation if required.
1946 (when (plist-get info :preserve-breaks)
1947 (setq output (replace-regexp-in-string
1948 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
1949 ;; Return value.
1950 output))
1953 ;;;; Planning
1955 (defun org-latex-planning (planning contents info)
1956 "Transcode a PLANNING element from Org to LaTeX.
1957 CONTENTS is nil. INFO is a plist holding contextual
1958 information."
1959 (concat
1960 "\\noindent"
1961 (mapconcat
1962 'identity
1963 (delq nil
1964 (list
1965 (let ((closed (org-element-property :closed planning)))
1966 (when closed
1967 (concat
1968 (format "\\textbf{%s} " org-closed-string)
1969 (format org-latex-inactive-timestamp-format
1970 (org-translate-time
1971 (org-element-property :raw-value closed))))))
1972 (let ((deadline (org-element-property :deadline planning)))
1973 (when deadline
1974 (concat
1975 (format "\\textbf{%s} " org-deadline-string)
1976 (format org-latex-active-timestamp-format
1977 (org-translate-time
1978 (org-element-property :raw-value deadline))))))
1979 (let ((scheduled (org-element-property :scheduled planning)))
1980 (when scheduled
1981 (concat
1982 (format "\\textbf{%s} " org-scheduled-string)
1983 (format org-latex-active-timestamp-format
1984 (org-translate-time
1985 (org-element-property :raw-value scheduled))))))))
1986 " ")
1987 "\\\\"))
1990 ;;;; Quote Block
1992 (defun org-latex-quote-block (quote-block contents info)
1993 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1994 CONTENTS holds the contents of the block. INFO is a plist
1995 holding contextual information."
1996 (org-latex--wrap-label
1997 quote-block
1998 (format "\\begin{quote}\n%s\\end{quote}" contents)))
2001 ;;;; Quote Section
2003 (defun org-latex-quote-section (quote-section contents info)
2004 "Transcode a QUOTE-SECTION element from Org to LaTeX.
2005 CONTENTS is nil. INFO is a plist holding contextual information."
2006 (let ((value (org-remove-indentation
2007 (org-element-property :value quote-section))))
2008 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
2011 ;;;; Radio Target
2013 (defun org-latex-radio-target (radio-target text info)
2014 "Transcode a RADIO-TARGET object from Org to LaTeX.
2015 TEXT is the text of the target. INFO is a plist holding
2016 contextual information."
2017 (format "\\label{%s}%s"
2018 (org-export-solidify-link-text
2019 (org-element-property :value radio-target))
2020 text))
2023 ;;;; Section
2025 (defun org-latex-section (section contents info)
2026 "Transcode a SECTION element from Org to LaTeX.
2027 CONTENTS holds the contents of the section. INFO is a plist
2028 holding contextual information."
2029 contents)
2032 ;;;; Special Block
2034 (defun org-latex-special-block (special-block contents info)
2035 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2036 CONTENTS holds the contents of the block. INFO is a plist
2037 holding contextual information."
2038 (let ((type (downcase (org-element-property :type special-block)))
2039 (opt (org-export-read-attribute :attr_latex special-block :options)))
2040 (concat (format "\\begin{%s}%s\n" type (or opt ""))
2041 ;; Insert any label or caption within the block
2042 ;; (otherwise, a reference pointing to that element will
2043 ;; count the section instead).
2044 (org-latex--caption/label-string special-block info)
2045 contents
2046 (format "\\end{%s}" type))))
2049 ;;;; Src Block
2051 (defun org-latex-src-block (src-block contents info)
2052 "Transcode a SRC-BLOCK element from Org to LaTeX.
2053 CONTENTS holds the contents of the item. INFO is a plist holding
2054 contextual information."
2055 (when (org-string-nw-p (org-element-property :value src-block))
2056 (let* ((lang (org-element-property :language src-block))
2057 (caption (org-element-property :caption src-block))
2058 (label (org-element-property :name src-block))
2059 (custom-env (and lang
2060 (cadr (assq (intern lang)
2061 org-latex-custom-lang-environments))))
2062 (num-start (case (org-element-property :number-lines src-block)
2063 (continued (org-export-get-loc src-block info))
2064 (new 0)))
2065 (retain-labels (org-element-property :retain-labels src-block)))
2066 (cond
2067 ;; Case 1. No source fontification.
2068 ((not org-latex-listings)
2069 (let ((caption-str (org-latex--caption/label-string src-block info))
2070 (float-env (and caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
2071 (format
2072 (or float-env "%s")
2073 (concat caption-str
2074 (format "\\begin{verbatim}\n%s\\end{verbatim}"
2075 (org-export-format-code-default src-block info))))))
2076 ;; Case 2. Custom environment.
2077 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2078 custom-env
2079 (org-export-format-code-default src-block info)
2080 custom-env))
2081 ;; Case 3. Use minted package.
2082 ((eq org-latex-listings 'minted)
2083 (let ((float-env
2084 (when (or label caption)
2085 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2086 (org-latex--caption/label-string src-block info))))
2087 (body
2088 (format
2089 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2090 ;; Options.
2091 (org-latex--make-option-string
2092 (if (not num-start) org-latex-minted-options
2093 (append `(("linenos")
2094 ("firstnumber" ,(number-to-string (1+ num-start))))
2095 org-latex-minted-options)))
2096 ;; Language.
2097 (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
2098 ;; Source code.
2099 (let* ((code-info (org-export-unravel-code src-block))
2100 (max-width
2101 (apply 'max
2102 (mapcar 'length
2103 (org-split-string (car code-info)
2104 "\n")))))
2105 (org-export-format-code
2106 (car code-info)
2107 (lambda (loc num ref)
2108 (concat
2110 (when ref
2111 ;; Ensure references are flushed to the right,
2112 ;; separated with 6 spaces from the widest line
2113 ;; of code.
2114 (concat (make-string (+ (- max-width (length loc)) 6)
2115 ?\s)
2116 (format "(%s)" ref)))))
2117 nil (and retain-labels (cdr code-info)))))))
2118 ;; Return value.
2119 (if float-env (format float-env body) body)))
2120 ;; Case 4. Use listings package.
2122 (let ((lst-lang
2123 (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
2124 (caption-str
2125 (when caption
2126 (let ((main (org-export-get-caption src-block))
2127 (secondary (org-export-get-caption src-block t)))
2128 (if (not secondary)
2129 (format "{%s}" (org-export-data main info))
2130 (format "{[%s]%s}"
2131 (org-export-data secondary info)
2132 (org-export-data main info)))))))
2133 (concat
2134 ;; Options.
2135 (format "\\lstset{%s}\n"
2136 (org-latex--make-option-string
2137 (append org-latex-listings-options
2138 `(("language" ,lst-lang))
2139 (when label `(("label" ,label)))
2140 (when caption-str `(("caption" ,caption-str)))
2141 (cond ((not num-start) '(("numbers" "none")))
2142 ((zerop num-start) '(("numbers" "left")))
2143 (t `(("numbers" "left")
2144 ("firstnumber"
2145 ,(number-to-string (1+ num-start)))))))))
2146 ;; Source code.
2147 (format
2148 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2149 (let* ((code-info (org-export-unravel-code src-block))
2150 (max-width
2151 (apply 'max
2152 (mapcar 'length
2153 (org-split-string (car code-info) "\n")))))
2154 (org-export-format-code
2155 (car code-info)
2156 (lambda (loc num ref)
2157 (concat
2159 (when ref
2160 ;; Ensure references are flushed to the right,
2161 ;; separated with 6 spaces from the widest line of
2162 ;; code
2163 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2164 (format "(%s)" ref)))))
2165 nil (and retain-labels (cdr code-info))))))))))))
2168 ;;;; Statistics Cookie
2170 (defun org-latex-statistics-cookie (statistics-cookie contents info)
2171 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2172 CONTENTS is nil. INFO is a plist holding contextual information."
2173 (replace-regexp-in-string
2174 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2177 ;;;; Strike-Through
2179 (defun org-latex-strike-through (strike-through contents info)
2180 "Transcode STRIKE-THROUGH from Org to LaTeX.
2181 CONTENTS is the text with strike-through markup. INFO is a plist
2182 holding contextual information."
2183 (org-latex--text-markup contents 'strike-through))
2186 ;;;; Subscript
2188 (defun org-latex-subscript (subscript contents info)
2189 "Transcode a SUBSCRIPT object from Org to LaTeX.
2190 CONTENTS is the contents of the object. INFO is a plist holding
2191 contextual information."
2192 (if (= (length contents) 1) (format "$_%s$" contents)
2193 ;; Handle multiple objects in SUBSCRIPT by creating a subscript
2194 ;; command for each of them.
2195 (let ((prev-blanks 0))
2196 (mapconcat
2197 (lambda (obj)
2198 (case (org-element-type obj)
2199 ((entity latex-fragment)
2200 (setq prev-blanks (org-element-property :post-blank obj))
2201 (let ((data (org-trim (org-export-data obj info))))
2202 (string-match
2203 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2204 data)
2205 (format "$_{%s}$" (match-string 1 data))))
2206 (plain-text
2207 (format "$_\\mathrm{%s}$"
2208 (concat (make-string prev-blanks ? )
2209 ;; mathrm command doesn't handle spaces,
2210 ;; so we have to enforce them.
2211 (replace-regexp-in-string
2212 " " "\\\\ " (org-export-data obj info)))))
2213 (otherwise
2214 (setq prev-blanks (org-element-property :post-blank obj))
2215 (format "$_{%s}$" (org-export-data obj info)))))
2216 (org-element-contents subscript) ""))))
2219 ;;;; Superscript
2221 (defun org-latex-superscript (superscript contents info)
2222 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2223 CONTENTS is the contents of the object. INFO is a plist holding
2224 contextual information."
2225 (if (= (length contents) 1) (format "$^%s$" contents)
2226 ;; Handle multiple objects in SUPERSCRIPT by creating
2227 ;; a superscript command for each of them.
2228 (let ((prev-blanks 0))
2229 (mapconcat
2230 (lambda (obj)
2231 (case (org-element-type obj)
2232 ((entity latex-fragment)
2233 (setq prev-blanks (org-element-property :post-blank obj))
2234 (let ((data (org-trim (org-export-data obj info))))
2235 (string-match
2236 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2237 data)
2238 (format "$^{%s}$" (match-string 1 data))))
2239 (plain-text
2240 (format "$^\\mathrm{%s}$"
2241 (concat (make-string prev-blanks ? )
2242 ;; mathrm command doesn't handle spaces,
2243 ;; so we have to enforce them.
2244 (replace-regexp-in-string
2245 " " "\\\\ " (org-export-data obj info)))))
2246 (otherwise
2247 (setq prev-blanks (org-element-property :post-blank obj))
2248 (format "$^{%s}$" (org-export-data obj info)))))
2249 (org-element-contents superscript) ""))))
2252 ;;;; Table
2254 ;; `org-latex-table' is the entry point for table transcoding. It
2255 ;; takes care of tables with a "verbatim" mode. Otherwise, it
2256 ;; delegates the job to either `org-latex--table.el-table',
2257 ;; `org-latex--org-table' or `org-latex--math-table' functions,
2258 ;; depending of the type of the table and the mode requested.
2260 ;; `org-latex--align-string' is a subroutine used to build alignment
2261 ;; string for Org tables.
2263 (defun org-latex-table (table contents info)
2264 "Transcode a TABLE element from Org to LaTeX.
2265 CONTENTS is the contents of the table. INFO is a plist holding
2266 contextual information."
2267 (if (eq (org-element-property :type table) 'table.el)
2268 ;; "table.el" table. Convert it using appropriate tools.
2269 (org-latex--table.el-table table info)
2270 (let ((type (or (org-export-read-attribute :attr_latex table :mode)
2271 org-latex-default-table-mode)))
2272 (cond
2273 ;; Case 1: Verbatim table.
2274 ((string= type "verbatim")
2275 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2276 ;; Re-create table, without affiliated keywords.
2277 (org-trim (org-element-interpret-data
2278 `(table nil ,@(org-element-contents table))))))
2279 ;; Case 2: Matrix.
2280 ((or (string= type "math") (string= type "inline-math"))
2281 (org-latex--math-table table info))
2282 ;; Case 3: Standard table.
2283 (t (concat (org-latex--org-table table contents info)
2284 ;; When there are footnote references within the
2285 ;; table, insert their definition just after it.
2286 (org-latex--delayed-footnotes-definitions table info)))))))
2288 (defun org-latex--align-string (table info)
2289 "Return an appropriate LaTeX alignment string.
2290 TABLE is the considered table. INFO is a plist used as
2291 a communication channel."
2292 (or (org-export-read-attribute :attr_latex table :align)
2293 (let (align)
2294 ;; Extract column groups and alignment from first (non-rule)
2295 ;; row.
2296 (org-element-map
2297 (org-element-map table 'table-row
2298 (lambda (row)
2299 (and (eq (org-element-property :type row) 'standard) row))
2300 info 'first-match)
2301 'table-cell
2302 (lambda (cell)
2303 (let ((borders (org-export-table-cell-borders cell info)))
2304 ;; Check left border for the first cell only.
2305 (when (and (memq 'left borders) (not align))
2306 (push "|" align))
2307 (push (case (org-export-table-cell-alignment cell info)
2308 (left "l")
2309 (right "r")
2310 (center "c"))
2311 align)
2312 (when (memq 'right borders) (push "|" align))))
2313 info)
2314 (apply 'concat (nreverse align)))))
2316 (defun org-latex--org-table (table contents info)
2317 "Return appropriate LaTeX code for an Org table.
2319 TABLE is the table type element to transcode. CONTENTS is its
2320 contents, as a string. INFO is a plist used as a communication
2321 channel.
2323 This function assumes TABLE has `org' as its `:type' property and
2324 `table' as its `:mode' attribute."
2325 (let* ((caption (org-latex--caption/label-string table info))
2326 (attr (org-export-read-attribute :attr_latex table))
2327 ;; Determine alignment string.
2328 (alignment (org-latex--align-string table info))
2329 ;; Determine environment for the table: longtable, tabular...
2330 (table-env (let ((env (plist-get attr :environment)))
2331 (if env (format "%s" env)
2332 org-latex-default-table-environment)))
2333 ;; If table is a float, determine environment: table, table*
2334 ;; or sidewaystable.
2335 (float-env (unless (equal "longtable" table-env)
2336 (let ((float (plist-get attr :float)))
2337 (cond
2338 ((string= float "sidewaystable") "sidewaystable")
2339 ((string= float "multicolumn") "table*")
2340 ((or (string= float "table")
2341 (org-element-property :caption table))
2342 "table")))))
2343 ;; Extract others display options.
2344 (fontsize (let ((font (plist-get attr :font)))
2345 (and font (concat (org-trim (format "%s" font)) "\n"))))
2346 (width (plist-get attr :width))
2347 (placement (or (plist-get attr :placement)
2348 (format "[%s]" org-latex-default-figure-position)))
2349 (centerp (if (plist-member attr :center) (plist-get attr :center)
2350 org-latex-tables-centered)))
2351 ;; Prepare the final format string for the table.
2352 (cond
2353 ;; Longtable.
2354 ((equal "longtable" table-env)
2355 (concat (and fontsize (concat "{" fontsize))
2356 (format "\\begin{longtable}{%s}\n" alignment)
2357 (and org-latex-table-caption-above
2358 (org-string-nw-p caption)
2359 (concat caption "\\\\\n"))
2360 contents
2361 (and (not org-latex-table-caption-above)
2362 (org-string-nw-p caption)
2363 (concat caption "\\\\\n"))
2364 "\\end{longtable}\n"
2365 (and fontsize "}")))
2366 ;; Others.
2367 (t (concat (cond
2368 (float-env
2369 (concat (format "\\begin{%s}%s\n" float-env placement)
2370 (if org-latex-table-caption-above caption "")
2371 (when centerp "\\centering\n")
2372 fontsize))
2373 (centerp (concat "\\begin{center}\n" fontsize))
2374 (fontsize (concat "{" fontsize)))
2375 (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2376 table-env
2377 (if width (format "{%s}" width) "")
2378 alignment
2379 contents
2380 table-env)
2381 (cond
2382 (float-env
2383 (concat (if org-latex-table-caption-above "" caption)
2384 (format "\n\\end{%s}" float-env)))
2385 (centerp "\n\\end{center}")
2386 (fontsize "}")))))))
2388 (defun org-latex--table.el-table (table info)
2389 "Return appropriate LaTeX code for a table.el table.
2391 TABLE is the table type element to transcode. INFO is a plist
2392 used as a communication channel.
2394 This function assumes TABLE has `table.el' as its `:type'
2395 property."
2396 (require 'table)
2397 ;; Ensure "*org-export-table*" buffer is empty.
2398 (with-current-buffer (get-buffer-create "*org-export-table*")
2399 (erase-buffer))
2400 (let ((output (with-temp-buffer
2401 (insert (org-element-property :value table))
2402 (goto-char 1)
2403 (re-search-forward "^[ \t]*|[^|]" nil t)
2404 (table-generate-source 'latex "*org-export-table*")
2405 (with-current-buffer "*org-export-table*"
2406 (org-trim (buffer-string))))))
2407 (kill-buffer (get-buffer "*org-export-table*"))
2408 ;; Remove left out comments.
2409 (while (string-match "^%.*\n" output)
2410 (setq output (replace-match "" t t output)))
2411 (let ((attr (org-export-read-attribute :attr_latex table)))
2412 (when (plist-get attr :rmlines)
2413 ;; When the "rmlines" attribute is provided, remove all hlines
2414 ;; but the the one separating heading from the table body.
2415 (let ((n 0) (pos 0))
2416 (while (and (< (length output) pos)
2417 (setq pos (string-match "^\\\\hline\n?" output pos)))
2418 (incf n)
2419 (unless (= n 2) (setq output (replace-match "" nil nil output))))))
2420 (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
2421 org-latex-tables-centered)))
2422 (if (not centerp) output
2423 (format "\\begin{center}\n%s\n\\end{center}" output))))))
2425 (defun org-latex--math-table (table info)
2426 "Return appropriate LaTeX code for a matrix.
2428 TABLE is the table type element to transcode. INFO is a plist
2429 used as a communication channel.
2431 This function assumes TABLE has `org' as its `:type' property and
2432 `inline-math' or `math' as its `:mode' attribute.."
2433 (let* ((caption (org-latex--caption/label-string table info))
2434 (attr (org-export-read-attribute :attr_latex table))
2435 (inlinep (eq (plist-get attr :mode) 'inline-math))
2436 (env (let ((env (plist-get attr :environment)))
2437 (if env (format "%s" env)
2438 org-latex-default-table-environment)))
2439 (contents
2440 (mapconcat
2441 (lambda (row)
2442 ;; Ignore horizontal rules.
2443 (when (eq (org-element-property :type row) 'standard)
2444 ;; Return each cell unmodified.
2445 (concat
2446 (mapconcat
2447 (lambda (cell)
2448 (substring (org-element-interpret-data cell) 0 -1))
2449 (org-element-map row 'table-cell 'identity info) "&")
2450 (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
2451 "\n")))
2452 (org-element-map table 'table-row 'identity info) ""))
2453 ;; Variables related to math clusters (contiguous math tables
2454 ;; of the same type).
2455 (mode (org-export-read-attribute :attr_latex table :mode))
2456 (prev (org-export-get-previous-element table info))
2457 (next (org-export-get-next-element table info))
2458 (same-mode-p
2459 (lambda (table)
2460 ;; Non-nil when TABLE has the same mode as current table.
2461 (string= (or (org-export-read-attribute :attr_latex table :mode)
2462 org-latex-default-table-mode)
2463 mode))))
2464 (concat
2465 ;; Opening string. If TABLE is in the middle of a table cluster,
2466 ;; do not insert any.
2467 (cond ((and prev
2468 (eq (org-element-type prev) 'table)
2469 (memq (org-element-property :post-blank prev) '(0 nil))
2470 (funcall same-mode-p prev))
2471 nil)
2472 (inlinep "\\(")
2473 ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
2474 (t "\\["))
2475 ;; Prefix (make sure it is a string).
2476 (format "%s" (or (plist-get attr :math-prefix) ""))
2477 ;; Environment. Also treat special cases.
2478 (cond ((equal env "array")
2479 (let ((align (org-latex--align-string table info)))
2480 (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
2481 ((assoc env org-latex-table-matrix-macros)
2482 (format "\\%s%s{\n%s}" env
2483 (format "%s" (or (plist-get attr :math-arguments) ""))
2484 contents))
2485 (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
2486 ;; Suffix (make sure it is a string).
2487 (format "%s" (or (plist-get attr :math-suffix) ""))
2488 ;; Closing string. If TABLE is in the middle of a table cluster,
2489 ;; do not insert any. If it closes such a cluster, be sure to
2490 ;; close the cluster with a string matching the opening string.
2491 (cond ((and next
2492 (eq (org-element-type next) 'table)
2493 (memq (org-element-property :post-blank table) '(0 nil))
2494 (funcall same-mode-p next))
2495 nil)
2496 (inlinep "\\)")
2497 ;; Find cluster beginning to know which environment to use.
2498 ((let ((cluster-beg table) prev)
2499 (while (and (setq prev (org-export-get-previous-element
2500 cluster-beg info))
2501 (memq (org-element-property :post-blank prev)
2502 '(0 nil))
2503 (funcall same-mode-p prev))
2504 (setq cluster-beg prev))
2505 (and (or (org-element-property :caption cluster-beg)
2506 (org-element-property :name cluster-beg))
2507 "\n\\end{equation}")))
2508 (t "\\]")))))
2511 ;;;; Table Cell
2513 (defun org-latex-table-cell (table-cell contents info)
2514 "Transcode a TABLE-CELL element from Org to LaTeX.
2515 CONTENTS is the cell contents. INFO is a plist used as
2516 a communication channel."
2517 (concat (if (and contents
2518 org-latex-table-scientific-notation
2519 (string-match orgtbl-exp-regexp contents))
2520 ;; Use appropriate format string for scientific
2521 ;; notation.
2522 (format org-latex-table-scientific-notation
2523 (match-string 1 contents)
2524 (match-string 2 contents))
2525 contents)
2526 (when (org-export-get-next-element table-cell info) " & ")))
2529 ;;;; Table Row
2531 (defun org-latex-table-row (table-row contents info)
2532 "Transcode a TABLE-ROW element from Org to LaTeX.
2533 CONTENTS is the contents of the row. INFO is a plist used as
2534 a communication channel."
2535 ;; Rules are ignored since table separators are deduced from
2536 ;; borders of the current row.
2537 (when (eq (org-element-property :type table-row) 'standard)
2538 (let* ((attr (org-export-read-attribute :attr_latex
2539 (org-export-get-parent table-row)))
2540 (longtablep (string= (or (plist-get attr :environment)
2541 org-latex-default-table-environment)
2542 "longtable"))
2543 (booktabsp (if (plist-member attr :booktabs)
2544 (plist-get attr :booktabs)
2545 org-latex-tables-booktabs))
2546 ;; TABLE-ROW's borders are extracted from its first cell.
2547 (borders (org-export-table-cell-borders
2548 (car (org-element-contents table-row)) info)))
2549 (concat
2550 ;; When BOOKTABS are activated enforce top-rule even when no
2551 ;; hline was specifically marked.
2552 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2553 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2554 contents "\\\\\n"
2555 (cond
2556 ;; Special case for long tables. Define header and footers.
2557 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2558 (format "%s
2559 \\endhead
2560 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2561 \\endfoot
2562 \\endlastfoot"
2563 (if booktabsp "\\midrule" "\\hline")
2564 (if booktabsp "\\midrule" "\\hline")
2565 ;; Number of columns.
2566 (cdr (org-export-table-dimensions
2567 (org-export-get-parent-table table-row) info))))
2568 ;; When BOOKTABS are activated enforce bottom rule even when
2569 ;; no hline was specifically marked.
2570 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2571 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2572 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2575 ;;;; Target
2577 (defun org-latex-target (target contents info)
2578 "Transcode a TARGET object from Org to LaTeX.
2579 CONTENTS is nil. INFO is a plist holding contextual
2580 information."
2581 (format "\\label{%s}"
2582 (org-export-solidify-link-text (org-element-property :value target))))
2585 ;;;; Timestamp
2587 (defun org-latex-timestamp (timestamp contents info)
2588 "Transcode a TIMESTAMP object from Org to LaTeX.
2589 CONTENTS is nil. INFO is a plist holding contextual
2590 information."
2591 (let ((value (org-latex-plain-text
2592 (org-timestamp-translate timestamp) info)))
2593 (case (org-element-property :type timestamp)
2594 ((active active-range) (format org-latex-active-timestamp-format value))
2595 ((inactive inactive-range)
2596 (format org-latex-inactive-timestamp-format value))
2597 (otherwise (format org-latex-diary-timestamp-format value)))))
2600 ;;;; Underline
2602 (defun org-latex-underline (underline contents info)
2603 "Transcode UNDERLINE from Org to LaTeX.
2604 CONTENTS is the text with underline markup. INFO is a plist
2605 holding contextual information."
2606 (org-latex--text-markup contents 'underline))
2609 ;;;; Verbatim
2611 (defun org-latex-verbatim (verbatim contents info)
2612 "Transcode a VERBATIM object from Org to LaTeX.
2613 CONTENTS is nil. INFO is a plist used as a communication
2614 channel."
2615 (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2618 ;;;; Verse Block
2620 (defun org-latex-verse-block (verse-block contents info)
2621 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2622 CONTENTS is verse block contents. INFO is a plist holding
2623 contextual information."
2624 (org-latex--wrap-label
2625 verse-block
2626 ;; In a verse environment, add a line break to each newline
2627 ;; character and change each white space at beginning of a line
2628 ;; into a space of 1 em. Also change each blank line with
2629 ;; a vertical space of 1 em.
2630 (progn
2631 (setq contents (replace-regexp-in-string
2632 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2633 (replace-regexp-in-string
2634 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2635 (while (string-match "^[ \t]+" contents)
2636 (let ((new-str (format "\\hspace*{%dem}"
2637 (length (match-string 0 contents)))))
2638 (setq contents (replace-match new-str nil t contents))))
2639 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2643 ;;; End-user functions
2645 ;;;###autoload
2646 (defun org-latex-export-as-latex
2647 (&optional async subtreep visible-only body-only ext-plist)
2648 "Export current buffer as a LaTeX buffer.
2650 If narrowing is active in the current buffer, only export its
2651 narrowed part.
2653 If a region is active, export that region.
2655 A non-nil optional argument ASYNC means the process should happen
2656 asynchronously. The resulting buffer should be accessible
2657 through the `org-export-stack' interface.
2659 When optional argument SUBTREEP is non-nil, export the sub-tree
2660 at point, extracting information from the headline properties
2661 first.
2663 When optional argument VISIBLE-ONLY is non-nil, don't export
2664 contents of hidden elements.
2666 When optional argument BODY-ONLY is non-nil, only write code
2667 between \"\\begin{document}\" and \"\\end{document}\".
2669 EXT-PLIST, when provided, is a property list with external
2670 parameters overriding Org default settings, but still inferior to
2671 file-local settings.
2673 Export is done in a buffer named \"*Org LATEX Export*\", which
2674 will be displayed when `org-export-show-temporary-export-buffer'
2675 is non-nil."
2676 (interactive)
2677 (if async
2678 (org-export-async-start
2679 (lambda (output)
2680 (with-current-buffer (get-buffer-create "*Org LATEX Export*")
2681 (erase-buffer)
2682 (insert output)
2683 (goto-char (point-min))
2684 (LaTeX-mode)
2685 (org-export-add-to-stack (current-buffer) 'latex)))
2686 `(org-export-as 'latex ,subtreep ,visible-only ,body-only
2687 ',ext-plist))
2688 (let ((outbuf
2689 (org-export-to-buffer 'latex "*Org LATEX Export*"
2690 subtreep visible-only body-only ext-plist)))
2691 (with-current-buffer outbuf (LaTeX-mode))
2692 (when org-export-show-temporary-export-buffer
2693 (switch-to-buffer-other-window outbuf)))))
2695 ;;;###autoload
2696 (defun org-latex-export-to-latex
2697 (&optional async subtreep visible-only body-only ext-plist)
2698 "Export current buffer to a LaTeX file.
2700 If narrowing is active in the current buffer, only export its
2701 narrowed part.
2703 If a region is active, export that region.
2705 A non-nil optional argument ASYNC means the process should happen
2706 asynchronously. The resulting file should be accessible through
2707 the `org-export-stack' interface.
2709 When optional argument SUBTREEP is non-nil, export the sub-tree
2710 at point, extracting information from the headline properties
2711 first.
2713 When optional argument VISIBLE-ONLY is non-nil, don't export
2714 contents of hidden elements.
2716 When optional argument BODY-ONLY is non-nil, only write code
2717 between \"\\begin{document}\" and \"\\end{document}\".
2719 EXT-PLIST, when provided, is a property list with external
2720 parameters overriding Org default settings, but still inferior to
2721 file-local settings.
2723 Return output file's name."
2724 (interactive)
2725 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2726 (if async
2727 (org-export-async-start
2728 (lambda (f) (org-export-add-to-stack f 'latex))
2729 `(expand-file-name
2730 (org-export-to-file
2731 'latex ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))
2732 (org-export-to-file
2733 'latex outfile subtreep visible-only body-only ext-plist))))
2735 ;;;###autoload
2736 (defun org-latex-export-to-pdf
2737 (&optional async subtreep visible-only body-only ext-plist)
2738 "Export current buffer to LaTeX then process through to PDF.
2740 If narrowing is active in the current buffer, only export its
2741 narrowed part.
2743 If a region is active, export that region.
2745 A non-nil optional argument ASYNC means the process should happen
2746 asynchronously. The resulting file should be accessible through
2747 the `org-export-stack' interface.
2749 When optional argument SUBTREEP is non-nil, export the sub-tree
2750 at point, extracting information from the headline properties
2751 first.
2753 When optional argument VISIBLE-ONLY is non-nil, don't export
2754 contents of hidden elements.
2756 When optional argument BODY-ONLY is non-nil, only write code
2757 between \"\\begin{document}\" and \"\\end{document}\".
2759 EXT-PLIST, when provided, is a property list with external
2760 parameters overriding Org default settings, but still inferior to
2761 file-local settings.
2763 Return PDF file's name."
2764 (interactive)
2765 (if async
2766 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
2767 (org-export-async-start
2768 (lambda (f) (org-export-add-to-stack f 'latex))
2769 `(expand-file-name
2770 (org-latex-compile
2771 (org-export-to-file
2772 'latex ,outfile ,subtreep ,visible-only ,body-only
2773 ',ext-plist)))))
2774 (org-latex-compile
2775 (org-latex-export-to-latex
2776 nil subtreep visible-only body-only ext-plist))))
2778 (defun org-latex-compile (texfile &optional snippet)
2779 "Compile a TeX file.
2781 TEXFILE is the name of the file being compiled. Processing is
2782 done through the command specified in `org-latex-pdf-process'.
2784 When optional argument SNIPPET is non-nil, TEXFILE is a temporary
2785 file used to preview a LaTeX snippet. In this case, do not
2786 create a log buffer and do not bother removing log files.
2788 Return PDF file name or an error if it couldn't be produced."
2789 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
2790 (full-name (file-truename texfile))
2791 (out-dir (file-name-directory texfile))
2792 ;; Make sure `default-directory' is set to TEXFILE directory,
2793 ;; not to whatever value the current buffer may have.
2794 (default-directory (file-name-directory full-name))
2795 errors)
2796 (unless snippet (message (format "Processing LaTeX file %s ..." texfile)))
2797 (save-window-excursion
2798 (cond
2799 ;; A function is provided: Apply it.
2800 ((functionp org-latex-pdf-process)
2801 (funcall org-latex-pdf-process (shell-quote-argument texfile)))
2802 ;; A list is provided: Replace %b, %f and %o with appropriate
2803 ;; values in each command before applying it. Output is
2804 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2805 ((consp org-latex-pdf-process)
2806 (let ((outbuf (and (not snippet)
2807 (get-buffer-create "*Org PDF LaTeX Output*"))))
2808 (mapc
2809 (lambda (command)
2810 (shell-command
2811 (replace-regexp-in-string
2812 "%b" (shell-quote-argument base-name)
2813 (replace-regexp-in-string
2814 "%f" (shell-quote-argument full-name)
2815 (replace-regexp-in-string
2816 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2817 outbuf))
2818 org-latex-pdf-process)
2819 ;; Collect standard errors from output buffer.
2820 (setq errors (and (not snippet) (org-latex--collect-errors outbuf)))))
2821 (t (error "No valid command to process to PDF")))
2822 (let ((pdffile (concat out-dir base-name ".pdf")))
2823 ;; Check for process failure. Provide collected errors if
2824 ;; possible.
2825 (if (not (file-exists-p pdffile))
2826 (error (concat (format "PDF file %s wasn't produced" pdffile)
2827 (when errors (concat ": " errors))))
2828 ;; Else remove log files, when specified, and signal end of
2829 ;; process to user, along with any error encountered.
2830 (when (and (not snippet) org-latex-remove-logfiles)
2831 (dolist (ext org-latex-logfiles-extensions)
2832 (let ((file (concat out-dir base-name "." ext)))
2833 (when (file-exists-p file) (delete-file file)))))
2834 (message (concat "Process completed"
2835 (if (not errors) "."
2836 (concat " with errors: " errors)))))
2837 ;; Return output file name.
2838 pdffile))))
2840 (defun org-latex--collect-errors (buffer)
2841 "Collect some kind of errors from \"pdflatex\" command output.
2843 BUFFER is the buffer containing output.
2845 Return collected error types as a string, or nil if there was
2846 none."
2847 (with-current-buffer buffer
2848 (save-excursion
2849 (goto-char (point-max))
2850 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
2851 (let ((case-fold-search t)
2852 (errors ""))
2853 (dolist (latex-error org-latex-known-errors)
2854 (when (save-excursion (re-search-forward (car latex-error) nil t))
2855 (setq errors (concat errors " " (cdr latex-error)))))
2856 (and (org-string-nw-p errors) (org-trim errors)))))))
2858 ;;;###autoload
2859 (defun org-latex-publish-to-latex (plist filename pub-dir)
2860 "Publish an Org file to LaTeX.
2862 FILENAME is the filename of the Org file to be published. PLIST
2863 is the property list for the given project. PUB-DIR is the
2864 publishing directory.
2866 Return output file name."
2867 (org-publish-org-to 'latex filename ".tex" plist pub-dir))
2869 ;;;###autoload
2870 (defun org-latex-publish-to-pdf (plist filename pub-dir)
2871 "Publish an Org file to PDF (via LaTeX).
2873 FILENAME is the filename of the Org file to be published. PLIST
2874 is the property list for the given project. PUB-DIR is the
2875 publishing directory.
2877 Return output file name."
2878 ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
2879 ;; in working directory and then moved to publishing directory.
2880 (org-publish-attachment
2881 plist
2882 (org-latex-compile (org-publish-org-to 'latex filename ".tex" plist))
2883 pub-dir))
2886 (provide 'ox-latex)
2888 ;; Local variables:
2889 ;; generated-autoload-file: "org-loaddefs.el"
2890 ;; End:
2892 ;;; ox-latex.el ends here