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