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