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