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