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