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