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