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