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