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