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