84ef2a5b61672751140c602125aeb908b798229b
[org-mode.git] / contrib / lisp / org-e-latex.el
blob84ef2a5b61672751140c602125aeb908b798229b
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 ((or author email) (format "\\author{%s}\n" (or author email)))))
1028 ;; Date.
1029 (let ((date (and (plist-get info :with-date)
1030 (org-export-data (plist-get info :date) info))))
1031 (format "\\date{%s}\n" (or 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 ;;;; Bold
1074 (defun org-e-latex-bold (bold contents info)
1075 "Transcode BOLD from Org to LaTeX.
1076 CONTENTS is the text with bold markup. INFO is a plist holding
1077 contextual information."
1078 (org-e-latex--text-markup contents 'bold))
1081 ;;;; Center Block
1083 (defun org-e-latex-center-block (center-block contents info)
1084 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1085 CONTENTS holds the contents of the center block. INFO is a plist
1086 holding contextual information."
1087 (org-e-latex--wrap-label
1088 center-block
1089 (format "\\begin{center}\n%s\\end{center}" contents)))
1092 ;;;; Clock
1094 (defun org-e-latex-clock (clock contents info)
1095 "Transcode a CLOCK element from Org to LaTeX.
1096 CONTENTS is nil. INFO is a plist holding contextual
1097 information."
1098 (concat
1099 "\\noindent"
1100 (format "\\textbf{%s} " org-clock-string)
1101 (format org-e-latex-inactive-timestamp-format
1102 (concat (org-translate-time
1103 (org-element-property :raw-value
1104 (org-element-property :value clock)))
1105 (let ((time (org-element-property :duration clock)))
1106 (and time (format " (%s)" time)))))
1107 "\\\\"))
1110 ;;;; Code
1112 (defun org-e-latex-code (code contents info)
1113 "Transcode a CODE object from Org to LaTeX.
1114 CONTENTS is nil. INFO is a plist used as a communication
1115 channel."
1116 (org-e-latex--text-markup (org-element-property :value code) 'code))
1119 ;;;; Drawer
1121 (defun org-e-latex-drawer (drawer contents info)
1122 "Transcode a DRAWER element from Org to LaTeX.
1123 CONTENTS holds the contents of the block. INFO is a plist
1124 holding contextual information."
1125 (let* ((name (org-element-property :drawer-name drawer))
1126 (output (if (functionp org-e-latex-format-drawer-function)
1127 (funcall org-e-latex-format-drawer-function
1128 name contents)
1129 ;; If there's no user defined function: simply
1130 ;; display contents of the drawer.
1131 contents)))
1132 (org-e-latex--wrap-label drawer output)))
1135 ;;;; Dynamic Block
1137 (defun org-e-latex-dynamic-block (dynamic-block contents info)
1138 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1139 CONTENTS holds the contents of the block. INFO is a plist
1140 holding contextual information. See `org-export-data'."
1141 (org-e-latex--wrap-label dynamic-block contents))
1144 ;;;; Entity
1146 (defun org-e-latex-entity (entity contents info)
1147 "Transcode an ENTITY object from Org to LaTeX.
1148 CONTENTS are the definition itself. INFO is a plist holding
1149 contextual information."
1150 (let ((ent (org-element-property :latex entity)))
1151 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
1154 ;;;; Example Block
1156 (defun org-e-latex-example-block (example-block contents info)
1157 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1158 CONTENTS is nil. INFO is a plist holding contextual
1159 information."
1160 (org-e-latex--wrap-label
1161 example-block
1162 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1163 (org-export-format-code-default example-block info))))
1166 ;;;; Export Block
1168 (defun org-e-latex-export-block (export-block contents info)
1169 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1170 CONTENTS is nil. INFO is a plist holding contextual information."
1171 (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
1172 (org-remove-indentation (org-element-property :value export-block))))
1175 ;;;; Export Snippet
1177 (defun org-e-latex-export-snippet (export-snippet contents info)
1178 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1179 CONTENTS is nil. INFO is a plist holding contextual information."
1180 (when (eq (org-export-snippet-backend export-snippet) 'e-latex)
1181 (org-element-property :value export-snippet)))
1184 ;;;; Fixed Width
1186 (defun org-e-latex-fixed-width (fixed-width contents info)
1187 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1188 CONTENTS is nil. INFO is a plist holding contextual information."
1189 (org-e-latex--wrap-label
1190 fixed-width
1191 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1192 (org-remove-indentation
1193 (org-element-property :value fixed-width)))))
1196 ;;;; Footnote Reference
1198 ;; Footnote reference export is handled by
1199 ;; `org-e-latex-footnote-reference'.
1201 ;; Internally, `org-e-latex--get-footnote-counter' is used to restore
1202 ;; the value of the LaTeX "footnote" counter after a jump due to
1203 ;; a reference to an already defined footnote. It is only needed in
1204 ;; item tags since the optional argument to \footnotemark is not
1205 ;; allowed there.
1207 (defun org-e-latex--get-footnote-counter (footnote-reference info)
1208 "Return \"footnote\" counter before FOOTNOTE-REFERENCE is encountered.
1209 INFO is a plist used as a communication channel."
1210 ;; Find original counter value by counting number of footnote
1211 ;; references appearing for the first time before the current
1212 ;; footnote reference.
1213 (let* ((label (org-element-property :label footnote-reference))
1214 seen-refs
1215 search-ref ; For byte-compiler.
1216 (search-ref
1217 (function
1218 (lambda (data)
1219 ;; Search footnote references through DATA, filling
1220 ;; SEEN-REFS along the way.
1221 (org-element-map
1222 data 'footnote-reference
1223 (lambda (fn)
1224 (let ((fn-lbl (org-element-property :label fn)))
1225 (cond
1226 ;; Anonymous footnote match: return number.
1227 ((eq fn footnote-reference) (length seen-refs))
1228 ;; Anonymous footnote: it's always a new one.
1229 ;; Also, be sure to return nil from the `cond' so
1230 ;; `first-match' doesn't get us out of the loop.
1231 ((not fn-lbl) (push 'inline seen-refs) nil)
1232 ;; Label not seen so far: add it so SEEN-REFS.
1234 ;; Also search for subsequent references in
1235 ;; footnote definition so numbering follows reading
1236 ;; logic. Note that we don't have to care about
1237 ;; inline definitions, since `org-element-map'
1238 ;; already traverse them at the right time.
1239 ((not (member fn-lbl seen-refs))
1240 (push fn-lbl seen-refs)
1241 (funcall search-ref
1242 (org-export-get-footnote-definition fn info))))))
1243 ;; Don't enter footnote definitions since it will happen
1244 ;; when their first reference is found.
1245 info 'first-match 'footnote-definition)))))
1246 (funcall search-ref (plist-get info :parse-tree))))
1248 (defun org-e-latex-footnote-reference (footnote-reference contents info)
1249 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1250 CONTENTS is nil. INFO is a plist holding contextual information."
1251 (concat
1252 ;; Insert separator between two footnotes in a row.
1253 (let ((prev (org-export-get-previous-element footnote-reference info)))
1254 (when (eq (org-element-type prev) 'footnote-reference)
1255 org-e-latex-footnote-separator))
1256 (cond
1257 ;; Use \footnotemark if reference is within an item's tag.
1258 ((eq (org-element-type (org-export-get-parent-element footnote-reference))
1259 'item)
1260 (if (org-export-footnote-first-reference-p footnote-reference info)
1261 "\\footnotemark"
1262 ;; Since we can't specify footnote number as an optional
1263 ;; argument within an item tag, some extra work has to be done
1264 ;; when the footnote has already been referenced. In that
1265 ;; case, set footnote counter to the desired number, use the
1266 ;; footnotemark, then set counter back to its original value.
1267 (format
1268 "\\setcounter{footnote}{%s}\\footnotemark\\setcounter{footnote}{%s}"
1269 (1- (org-export-get-footnote-number footnote-reference info))
1270 (org-e-latex--get-footnote-counter footnote-reference info))))
1271 ;; Use \footnotemark if the footnote has already been defined.
1272 ((not (org-export-footnote-first-reference-p footnote-reference info))
1273 (format "\\footnotemark[%s]{}"
1274 (org-export-get-footnote-number footnote-reference info)))
1275 ;; Use \footnotemark if reference is within another footnote
1276 ;; reference, footnote definition or table cell.
1277 ((loop for parent in (org-export-get-genealogy footnote-reference)
1278 thereis (memq (org-element-type parent)
1279 '(footnote-reference footnote-definition table-cell)))
1280 "\\footnotemark")
1281 ;; Otherwise, define it with \footnote command.
1283 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1284 (unless (eq (org-element-type def) 'org-data)
1285 (setq def (cons 'org-data (cons nil def))))
1286 (concat
1287 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1288 ;; Retrieve all footnote references within the footnote and
1289 ;; add their definition after it, since LaTeX doesn't support
1290 ;; them inside.
1291 (org-e-latex--delayed-footnotes-definitions def info)))))))
1294 ;;;; Headline
1296 (defun org-e-latex-headline (headline contents info)
1297 "Transcode an HEADLINE element from Org to LaTeX.
1298 CONTENTS holds the contents of the headline. INFO is a plist
1299 holding contextual information."
1300 (let* ((class (plist-get info :latex-class))
1301 (level (org-export-get-relative-level headline info))
1302 (numberedp (org-export-numbered-headline-p headline info))
1303 (class-sectionning (assoc class org-e-latex-classes))
1304 ;; Section formatting will set two placeholders: one for the
1305 ;; title and the other for the contents.
1306 (section-fmt
1307 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
1308 (fboundp (nth 2 class-sectionning)))
1309 (funcall (nth 2 class-sectionning) level numberedp)
1310 (nth (1+ level) class-sectionning))))
1311 (cond
1312 ;; No section available for that LEVEL.
1313 ((not sec) nil)
1314 ;; Section format directly returned by a function.
1315 ((stringp sec) sec)
1316 ;; (numbered-section . unnumbered-section)
1317 ((not (consp (cdr sec)))
1318 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1319 ;; (numbered-open numbered-close)
1320 ((= (length sec) 2)
1321 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1322 ;; (num-in num-out no-num-in no-num-out)
1323 ((= (length sec) 4)
1324 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1325 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1326 (text (org-export-data (org-element-property :title headline) info))
1327 (todo
1328 (and (plist-get info :with-todo-keywords)
1329 (let ((todo (org-element-property :todo-keyword headline)))
1330 (and todo (org-export-data todo info)))))
1331 (todo-type (and todo (org-element-property :todo-type headline)))
1332 (tags (and (plist-get info :with-tags)
1333 (org-export-get-tags headline info)))
1334 (priority (and (plist-get info :with-priority)
1335 (org-element-property :priority headline)))
1336 ;; Create the headline text along with a no-tag version. The
1337 ;; latter is required to remove tags from table of contents.
1338 (full-text (if (functionp org-e-latex-format-headline-function)
1339 ;; User-defined formatting function.
1340 (funcall org-e-latex-format-headline-function
1341 todo todo-type priority text tags)
1342 ;; Default formatting.
1343 (concat
1344 (when todo
1345 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1346 (when priority (format "\\framebox{\\#%c} " priority))
1347 text
1348 (when tags
1349 (format "\\hfill{}\\textsc{:%s:}"
1350 (mapconcat 'identity tags ":"))))))
1351 (full-text-no-tag
1352 (if (functionp org-e-latex-format-headline-function)
1353 ;; User-defined formatting function.
1354 (funcall org-e-latex-format-headline-function
1355 todo todo-type priority text nil)
1356 ;; Default formatting.
1357 (concat
1358 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1359 (when priority (format "\\framebox{\\#%c} " priority))
1360 text)))
1361 ;; Associate some \label to the headline for internal links.
1362 (headline-label
1363 (format "\\label{sec-%s}\n"
1364 (mapconcat 'number-to-string
1365 (org-export-get-headline-number headline info)
1366 "-")))
1367 (pre-blanks
1368 (make-string (org-element-property :pre-blank headline) 10)))
1369 (cond
1370 ;; Case 1: This is a footnote section: ignore it.
1371 ((org-element-property :footnote-section-p headline) nil)
1372 ;; Case 2. This is a deep sub-tree: export it as a list item.
1373 ;; Also export as items headlines for which no section
1374 ;; format has been found.
1375 ((or (not section-fmt) (org-export-low-level-p headline info))
1376 ;; Build the real contents of the sub-tree.
1377 (let ((low-level-body
1378 (concat
1379 ;; If the headline is the first sibling, start a list.
1380 (when (org-export-first-sibling-p headline info)
1381 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1382 ;; Itemize headline
1383 "\\item " full-text "\n" headline-label pre-blanks contents)))
1384 ;; If headline is not the last sibling simply return
1385 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1386 ;; blank line.
1387 (if (not (org-export-last-sibling-p headline info)) low-level-body
1388 (replace-regexp-in-string
1389 "[ \t\n]*\\'"
1390 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1391 low-level-body))))
1392 ;; Case 3. Standard headline. Export it as a section.
1394 (cond
1395 ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
1396 ;; Regular section. Use specified format string.
1397 (format section-fmt full-text
1398 (concat headline-label pre-blanks contents)))
1399 ((string-match "\\`\\\\\\(.*?\\){" section-fmt)
1400 ;; If tags should be removed from table of contents, insert
1401 ;; title without tags as an alternative heading in sectioning
1402 ;; command.
1403 (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
1404 nil nil section-fmt 1)
1405 ;; Replace square brackets with parenthesis since
1406 ;; square brackets are not supported in optional
1407 ;; arguments.
1408 (replace-regexp-in-string
1409 "\\[" "("
1410 (replace-regexp-in-string
1411 "\\]" ")"
1412 full-text-no-tag))
1413 full-text
1414 (concat headline-label pre-blanks contents)))
1416 ;; Impossible to add an alternative heading. Fallback to
1417 ;; regular sectioning format string.
1418 (format section-fmt full-text
1419 (concat headline-label pre-blanks contents))))))))
1422 ;;;; Horizontal Rule
1424 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1425 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1426 CONTENTS is nil. INFO is a plist holding contextual information."
1427 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1428 (prev (org-export-get-previous-element horizontal-rule info)))
1429 (concat
1430 ;; Make sure the rule doesn't start at the end of the current
1431 ;; line by separating it with a blank line from previous element.
1432 (when (and prev
1433 (let ((prev-blank (org-element-property :post-blank prev)))
1434 (or (not prev-blank) (zerop prev-blank))))
1435 "\n")
1436 (org-e-latex--wrap-label
1437 horizontal-rule
1438 (format "\\rule{%s}{%s}"
1439 (or (plist-get attr :width) "\\linewidth")
1440 (or (plist-get attr :thickness) "0.5pt"))))))
1443 ;;;; Inline Src Block
1445 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1446 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1447 CONTENTS holds the contents of the item. INFO is a plist holding
1448 contextual information."
1449 (let* ((code (org-element-property :value inline-src-block))
1450 (separator (org-e-latex--find-verb-separator code)))
1451 (cond
1452 ;; Do not use a special package: transcode it verbatim.
1453 ((not org-e-latex-listings)
1454 (concat "\\verb" separator code separator))
1455 ;; Use minted package.
1456 ((eq org-e-latex-listings 'minted)
1457 (let* ((org-lang (org-element-property :language inline-src-block))
1458 (mint-lang (or (cadr (assq (intern org-lang)
1459 org-e-latex-minted-langs))
1460 org-lang))
1461 (options (org-e-latex--make-option-string
1462 org-e-latex-minted-options)))
1463 (concat (format "\\mint%s{%s}"
1464 (if (string= options "") "" (format "[%s]" options))
1465 mint-lang)
1466 separator code separator)))
1467 ;; Use listings package.
1469 ;; Maybe translate language's name.
1470 (let* ((org-lang (org-element-property :language inline-src-block))
1471 (lst-lang (or (cadr (assq (intern org-lang)
1472 org-e-latex-listings-langs))
1473 org-lang))
1474 (options (org-e-latex--make-option-string
1475 (append org-e-latex-listings-options
1476 `(("language" ,lst-lang))))))
1477 (concat (format "\\lstinline[%s]" options)
1478 separator code separator))))))
1481 ;;;; Inlinetask
1483 (defun org-e-latex-inlinetask (inlinetask contents info)
1484 "Transcode an INLINETASK element from Org to LaTeX.
1485 CONTENTS holds the contents of the block. INFO is a plist
1486 holding contextual information."
1487 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1488 (todo (and (plist-get info :with-todo-keywords)
1489 (let ((todo (org-element-property :todo-keyword inlinetask)))
1490 (and todo (org-export-data todo info)))))
1491 (todo-type (org-element-property :todo-type inlinetask))
1492 (tags (and (plist-get info :with-tags)
1493 (org-export-get-tags inlinetask info)))
1494 (priority (and (plist-get info :with-priority)
1495 (org-element-property :priority inlinetask))))
1496 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1497 ;; with appropriate arguments.
1498 (if (functionp org-e-latex-format-inlinetask-function)
1499 (funcall org-e-latex-format-inlinetask-function
1500 todo todo-type priority title tags contents)
1501 ;; Otherwise, use a default template.
1502 (org-e-latex--wrap-label
1503 inlinetask
1504 (let ((full-title
1505 (concat
1506 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1507 (when priority (format "\\framebox{\\#%c} " priority))
1508 title
1509 (when tags (format "\\hfill{}\\textsc{:%s:}"
1510 (mapconcat 'identity tags ":"))))))
1511 (format (concat "\\begin{center}\n"
1512 "\\fbox{\n"
1513 "\\begin{minipage}[c]{.6\\textwidth}\n"
1514 "%s\n\n"
1515 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1516 "%s"
1517 "\\end{minipage}\n"
1518 "}\n"
1519 "\\end{center}")
1520 full-title contents))))))
1523 ;;;; Italic
1525 (defun org-e-latex-italic (italic contents info)
1526 "Transcode ITALIC from Org to LaTeX.
1527 CONTENTS is the text with italic markup. INFO is a plist holding
1528 contextual information."
1529 (org-e-latex--text-markup contents 'italic))
1532 ;;;; Item
1534 (defun org-e-latex-item (item contents info)
1535 "Transcode an ITEM element from Org to LaTeX.
1536 CONTENTS holds the contents of the item. INFO is a plist holding
1537 contextual information."
1538 (let* ((counter
1539 (let ((count (org-element-property :counter item))
1540 (level
1541 (loop for parent in (org-export-get-genealogy item)
1542 count (eq (org-element-type parent) 'plain-list)
1543 until (eq (org-element-type parent) 'headline))))
1544 (and count
1545 (< level 5)
1546 (format "\\setcounter{enum%s}{%s}\n"
1547 (nth (1- level) '("i" "ii" "iii" "iv"))
1548 (1- count)))))
1549 (checkbox (case (org-element-property :checkbox item)
1550 (on "$\\boxtimes$ ")
1551 (off "$\\Box$ ")
1552 (trans "$\\boxminus$ ")))
1553 (tag (let ((tag (org-element-property :tag item)))
1554 ;; Check-boxes must belong to the tag.
1555 (and tag (format "[%s] "
1556 (concat checkbox
1557 (org-export-data tag info)))))))
1558 (concat counter "\\item" (or tag (concat " " checkbox))
1559 (and contents (org-trim contents))
1560 ;; If there are footnotes references in tag, be sure to
1561 ;; add their definition at the end of the item. This
1562 ;; workaround is necessary since "\footnote{}" command is
1563 ;; not supported in tags.
1564 (and tag
1565 (org-e-latex--delayed-footnotes-definitions
1566 (org-element-property :tag item) info)))))
1569 ;;;; Keyword
1571 (defun org-e-latex-keyword (keyword contents info)
1572 "Transcode a KEYWORD element from Org to LaTeX.
1573 CONTENTS is nil. INFO is a plist holding contextual information."
1574 (let ((key (org-element-property :key keyword))
1575 (value (org-element-property :value keyword)))
1576 (cond
1577 ((string= key "LATEX") value)
1578 ((string= key "INDEX") (format "\\index{%s}" value))
1579 ;; Invisible targets.
1580 ((string= key "TARGET") nil)
1581 ((string= key "TOC")
1582 (let ((value (downcase value)))
1583 (cond
1584 ((string-match "\\<headlines\\>" value)
1585 (let ((depth (or (and (string-match "[0-9]+" value)
1586 (string-to-number (match-string 0 value)))
1587 (plist-get info :with-toc))))
1588 (concat
1589 (when (wholenump depth)
1590 (format "\\setcounter{tocdepth}{%s}\n" depth))
1591 "\\tableofcontents")))
1592 ((string= "tables" value) "\\listoftables")
1593 ((string= "figures" value) "\\listoffigures")
1594 ((string= "listings" value)
1595 (cond
1596 ((eq org-e-latex-listings 'minted) "\\listoflistings")
1597 (org-e-latex-listings "\\lstlistoflistings")
1598 ;; At the moment, src blocks with a caption are wrapped
1599 ;; into a figure environment.
1600 (t "\\listoffigures")))))))))
1603 ;;;; Latex Environment
1605 (defun org-e-latex-latex-environment (latex-environment contents info)
1606 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1607 CONTENTS is nil. INFO is a plist holding contextual information."
1608 (let ((label (org-element-property :name latex-environment))
1609 (value (org-remove-indentation
1610 (org-element-property :value latex-environment))))
1611 (if (not (org-string-nw-p label)) value
1612 ;; Environment is labelled: label must be within the environment
1613 ;; (otherwise, a reference pointing to that element will count
1614 ;; the section instead).
1615 (with-temp-buffer
1616 (insert value)
1617 (goto-char (point-min))
1618 (forward-line)
1619 (insert (format "\\label{%s}\n" (org-export-solidify-link-text label)))
1620 (buffer-string)))))
1623 ;;;; Latex Fragment
1625 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1626 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1627 CONTENTS is nil. INFO is a plist holding contextual information."
1628 (org-element-property :value latex-fragment))
1631 ;;;; Line Break
1633 (defun org-e-latex-line-break (line-break contents info)
1634 "Transcode a LINE-BREAK object from Org to LaTeX.
1635 CONTENTS is nil. INFO is a plist holding contextual information."
1636 "\\\\")
1639 ;;;; Link
1641 (defun org-e-latex-link--inline-image (link info)
1642 "Return LaTeX code for an inline image.
1643 LINK is the link pointing to the inline image. INFO is a plist
1644 used as a communication channel."
1645 (let* ((parent (org-export-get-parent-element link))
1646 (path (let ((raw-path (org-element-property :path link)))
1647 (if (not (file-name-absolute-p raw-path)) raw-path
1648 (expand-file-name raw-path))))
1649 (caption (org-e-latex--caption/label-string parent info))
1650 ;; Retrieve latex attributes from the element around.
1651 (attr (let ((raw-attr
1652 (mapconcat #'identity
1653 (org-element-property :attr_latex parent)
1654 " ")))
1655 (unless (string= raw-attr "") raw-attr)))
1656 (disposition
1657 (cond
1658 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1659 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1660 ((or (and attr (string-match "\\<float\\>" attr))
1661 (not (string= caption "")))
1662 'float)))
1663 (placement
1664 (cond
1665 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1666 (org-match-string-no-properties 1 attr))
1667 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1668 ((eq disposition 'float)
1669 (concat "[" org-e-latex-default-figure-position "]"))
1670 (t ""))))
1671 ;; Now clear ATTR from any special keyword and set a default
1672 ;; value if nothing is left.
1673 (setq attr
1674 (if (not attr) ""
1675 (org-trim
1676 (replace-regexp-in-string
1677 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
1678 (setq attr (cond ((not (string= attr "")) attr)
1679 ((eq disposition 'float) "width=0.7\\textwidth")
1680 ((eq disposition 'wrap) "width=0.48\\textwidth")
1681 (t (or org-e-latex-image-default-option ""))))
1682 ;; Return proper string, depending on DISPOSITION.
1683 (case disposition
1684 (wrap (format "\\begin{wrapfigure}%s
1685 \\centering
1686 \\includegraphics[%s]{%s}
1687 %s\\end{wrapfigure}" placement attr path caption))
1688 (multicolumn (format "\\begin{figure*}%s
1689 \\centering
1690 \\includegraphics[%s]{%s}
1691 %s\\end{figure*}" placement attr path caption))
1692 (float (format "\\begin{figure}%s
1693 \\centering
1694 \\includegraphics[%s]{%s}
1695 %s\\end{figure}" placement attr path caption))
1696 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1698 (defun org-e-latex-link (link desc info)
1699 "Transcode a LINK object from Org to LaTeX.
1701 DESC is the description part of the link, or the empty string.
1702 INFO is a plist holding contextual information. See
1703 `org-export-data'."
1704 (let* ((type (org-element-property :type link))
1705 (raw-path (org-element-property :path link))
1706 ;; Ensure DESC really exists, or set it to nil.
1707 (desc (and (not (string= desc "")) desc))
1708 (imagep (org-export-inline-image-p
1709 link org-e-latex-inline-image-rules))
1710 (path (cond
1711 ((member type '("http" "https" "ftp" "mailto"))
1712 (concat type ":" raw-path))
1713 ((string= type "file")
1714 (if (file-name-absolute-p raw-path)
1715 (concat "file://" (expand-file-name raw-path))
1716 (concat "file://" raw-path)))
1717 (t raw-path)))
1718 protocol)
1719 (cond
1720 ;; Image file.
1721 (imagep (org-e-latex-link--inline-image link info))
1722 ;; Radio link: Transcode target's contents and use them as link's
1723 ;; description.
1724 ((string= type "radio")
1725 (let ((destination (org-export-resolve-radio-link link info)))
1726 (when destination
1727 (format "\\hyperref[%s]{%s}"
1728 (org-export-solidify-link-text path)
1729 (org-export-data (org-element-contents destination) info)))))
1730 ;; Links pointing to an headline: Find destination and build
1731 ;; appropriate referencing command.
1732 ((member type '("custom-id" "fuzzy" "id"))
1733 (let ((destination (if (string= type "fuzzy")
1734 (org-export-resolve-fuzzy-link link info)
1735 (org-export-resolve-id-link link info))))
1736 (case (org-element-type destination)
1737 ;; Id link points to an external file.
1738 (plain-text
1739 (if desc (format "\\href{file://%s}{%s}" destination desc)
1740 (format "\\url{file://%s}" destination)))
1741 ;; Fuzzy link points nowhere.
1742 ('nil
1743 (format org-e-latex-link-with-unknown-path-format
1744 (or desc
1745 (org-export-data
1746 (org-element-property :raw-link link) info))))
1747 ;; Fuzzy link points to an invisible target.
1748 (keyword nil)
1749 ;; LINK points to an headline. If headlines are numbered
1750 ;; and the link has no description, display headline's
1751 ;; number. Otherwise, display description or headline's
1752 ;; title.
1753 (headline
1754 (let ((label
1755 (format "sec-%s"
1756 (mapconcat
1757 'number-to-string
1758 (org-export-get-headline-number destination info)
1759 "-"))))
1760 (if (and (plist-get info :section-numbers) (not desc))
1761 (format "\\ref{%s}" label)
1762 (format "\\hyperref[%s]{%s}" label
1763 (or desc
1764 (org-export-data
1765 (org-element-property :title destination) info))))))
1766 ;; Fuzzy link points to a target. Do as above.
1767 (otherwise
1768 (let ((path (org-export-solidify-link-text path)))
1769 (if (not desc) (format "\\ref{%s}" path)
1770 (format "\\hyperref[%s]{%s}" path desc)))))))
1771 ;; Coderef: replace link with the reference name or the
1772 ;; equivalent line number.
1773 ((string= type "coderef")
1774 (format (org-export-get-coderef-format path desc)
1775 (org-export-resolve-coderef path info)))
1776 ;; Link type is handled by a special function.
1777 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1778 (funcall protocol (org-link-unescape path) desc 'latex))
1779 ;; External link with a description part.
1780 ((and path desc) (format "\\href{%s}{%s}" path desc))
1781 ;; External link without a description part.
1782 (path (format "\\url{%s}" path))
1783 ;; No path, only description. Try to do something useful.
1784 (t (format org-e-latex-link-with-unknown-path-format desc)))))
1787 ;;;; Paragraph
1789 (defun org-e-latex-paragraph (paragraph contents info)
1790 "Transcode a PARAGRAPH element from Org to LaTeX.
1791 CONTENTS is the contents of the paragraph, as a string. INFO is
1792 the plist used as a communication channel."
1793 contents)
1796 ;;;; Plain List
1798 (defun org-e-latex-plain-list (plain-list contents info)
1799 "Transcode a PLAIN-LIST element from Org to LaTeX.
1800 CONTENTS is the contents of the list. INFO is a plist holding
1801 contextual information."
1802 (let* ((type (org-element-property :type plain-list))
1803 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1804 "inparadesc" "asparadesc"))
1805 (paralist-regexp (concat
1806 "\\("
1807 (mapconcat 'identity paralist-types "\\|")
1808 "\\)"))
1809 (attr (mapconcat #'identity
1810 (org-element-property :attr_latex plain-list)
1811 " "))
1812 (latex-type (cond
1813 ((and attr
1814 (string-match
1815 (format "\\<%s\\>" paralist-regexp) attr))
1816 (match-string 1 attr))
1817 ((eq type 'ordered) "enumerate")
1818 ((eq type 'unordered) "itemize")
1819 ((eq type 'descriptive) "description"))))
1820 (org-e-latex--wrap-label
1821 plain-list
1822 (format "\\begin{%s}%s\n%s\\end{%s}"
1823 latex-type
1824 ;; Once special environment, if any, has been removed, the
1825 ;; rest of the attributes will be optional arguments.
1826 ;; They will be put inside square brackets if necessary.
1827 (let ((opt (replace-regexp-in-string
1828 (format " *%s *" paralist-regexp) "" attr)))
1829 (cond ((string= opt "") "")
1830 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1831 (t (format "[%s]" opt))))
1832 contents
1833 latex-type))))
1836 ;;;; Plain Text
1838 (defun org-e-latex-plain-text (text info)
1839 "Transcode a TEXT string from Org to LaTeX.
1840 TEXT is the string to transcode. INFO is a plist holding
1841 contextual information."
1842 (let ((specialp (plist-get info :with-special-strings))
1843 (output text))
1844 ;; Protect %, #, &, $, ~, ^, _, { and }.
1845 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" output)
1846 (setq output
1847 (replace-match
1848 (format "\\%s" (match-string 2 output)) nil t output 2)))
1849 ;; Protect \. If special strings are used, be careful not to
1850 ;; protect "\" in "\-" constructs.
1851 (let ((symbols (if specialp "-%$#&{}~^_\\" "%$#&{}~^_\\")))
1852 (setq output
1853 (replace-regexp-in-string
1854 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
1855 "$\\backslash$" output nil t 1)))
1856 ;; Activate smart quotes. Be sure to provide original TEXT string
1857 ;; since OUTPUT may have been modified.
1858 (when (plist-get info :with-smart-quotes)
1859 (setq output (org-export-activate-smart-quotes output :latex info text)))
1860 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1861 (let ((case-fold-search nil)
1862 (start 0))
1863 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
1864 (setq output (replace-match
1865 (format "\\%s{}" (match-string 1 output)) nil t output)
1866 start (match-end 0))))
1867 ;; Convert special strings.
1868 (when specialp
1869 (setq output
1870 (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
1871 ;; Handle break preservation if required.
1872 (when (plist-get info :preserve-breaks)
1873 (setq output (replace-regexp-in-string
1874 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
1875 ;; Return value.
1876 output))
1879 ;;;; Planning
1881 (defun org-e-latex-planning (planning contents info)
1882 "Transcode a PLANNING element from Org to LaTeX.
1883 CONTENTS is nil. INFO is a plist holding contextual
1884 information."
1885 (concat
1886 "\\noindent"
1887 (mapconcat
1888 'identity
1889 (delq nil
1890 (list
1891 (let ((closed (org-element-property :closed planning)))
1892 (when closed
1893 (concat
1894 (format "\\textbf{%s} " org-closed-string)
1895 (format org-e-latex-inactive-timestamp-format
1896 (org-translate-time
1897 (org-element-property :raw-value closed))))))
1898 (let ((deadline (org-element-property :deadline planning)))
1899 (when deadline
1900 (concat
1901 (format "\\textbf{%s} " org-deadline-string)
1902 (format org-e-latex-active-timestamp-format
1903 (org-translate-time
1904 (org-element-property :raw-value deadline))))))
1905 (let ((scheduled (org-element-property :scheduled planning)))
1906 (when scheduled
1907 (concat
1908 (format "\\textbf{%s} " org-scheduled-string)
1909 (format org-e-latex-active-timestamp-format
1910 (org-translate-time
1911 (org-element-property :raw-value scheduled))))))))
1912 " ")
1913 "\\\\"))
1916 ;;;; Property Drawer
1918 (defun org-e-latex-property-drawer (property-drawer contents info)
1919 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1920 CONTENTS is nil. INFO is a plist holding contextual
1921 information."
1922 ;; The property drawer isn't exported but we want separating blank
1923 ;; lines nonetheless.
1927 ;;;; Quote Block
1929 (defun org-e-latex-quote-block (quote-block contents info)
1930 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1931 CONTENTS holds the contents of the block. INFO is a plist
1932 holding contextual information."
1933 (org-e-latex--wrap-label
1934 quote-block
1935 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1938 ;;;; Quote Section
1940 (defun org-e-latex-quote-section (quote-section contents info)
1941 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1942 CONTENTS is nil. INFO is a plist holding contextual information."
1943 (let ((value (org-remove-indentation
1944 (org-element-property :value quote-section))))
1945 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1948 ;;;; Radio Target
1950 (defun org-e-latex-radio-target (radio-target text info)
1951 "Transcode a RADIO-TARGET object from Org to LaTeX.
1952 TEXT is the text of the target. INFO is a plist holding
1953 contextual information."
1954 (format "\\label{%s}%s"
1955 (org-export-solidify-link-text
1956 (org-element-property :value radio-target))
1957 text))
1960 ;;;; Section
1962 (defun org-e-latex-section (section contents info)
1963 "Transcode a SECTION element from Org to LaTeX.
1964 CONTENTS holds the contents of the section. INFO is a plist
1965 holding contextual information."
1966 contents)
1969 ;;;; Special Block
1971 (defun org-e-latex-special-block (special-block contents info)
1972 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1973 CONTENTS holds the contents of the block. INFO is a plist
1974 holding contextual information."
1975 (let ((type (downcase (org-element-property :type special-block))))
1976 (concat (format "\\begin{%s}\n" type)
1977 ;; Insert any label or caption within the block
1978 ;; (otherwise, a reference pointing to that element will
1979 ;; count the section instead).
1980 (org-e-latex--caption/label-string special-block info)
1981 contents
1982 (format "\\end{%s}" type))))
1985 ;;;; Src Block
1987 (defun org-e-latex-src-block (src-block contents info)
1988 "Transcode a SRC-BLOCK element from Org to LaTeX.
1989 CONTENTS holds the contents of the item. INFO is a plist holding
1990 contextual information."
1991 (let* ((lang (org-element-property :language src-block))
1992 (caption (org-element-property :caption src-block))
1993 (label (org-element-property :name src-block))
1994 (custom-env (and lang
1995 (cadr (assq (intern lang)
1996 org-e-latex-custom-lang-environments))))
1997 (num-start (case (org-element-property :number-lines src-block)
1998 (continued (org-export-get-loc src-block info))
1999 (new 0)))
2000 (retain-labels (org-element-property :retain-labels src-block)))
2001 (cond
2002 ;; Case 1. No source fontification.
2003 ((not org-e-latex-listings)
2004 (let ((caption-str (org-e-latex--caption/label-string src-block info))
2005 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
2006 (format
2007 (or float-env "%s")
2008 (concat caption-str
2009 (format "\\begin{verbatim}\n%s\\end{verbatim}"
2010 (org-export-format-code-default src-block info))))))
2011 ;; Case 2. Custom environment.
2012 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2013 custom-env
2014 (org-export-format-code-default src-block info)
2015 custom-env))
2016 ;; Case 3. Use minted package.
2017 ((eq org-e-latex-listings 'minted)
2018 (let ((float-env
2019 (when (or label caption)
2020 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2021 (org-e-latex--caption/label-string src-block info))))
2022 (body
2023 (format
2024 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2025 ;; Options.
2026 (org-e-latex--make-option-string
2027 (if (not num-start) org-e-latex-minted-options
2028 (append `(("linenos")
2029 ("firstnumber" ,(number-to-string (1+ num-start))))
2030 org-e-latex-minted-options)))
2031 ;; Language.
2032 (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
2033 ;; Source code.
2034 (let* ((code-info (org-export-unravel-code src-block))
2035 (max-width
2036 (apply 'max
2037 (mapcar 'length
2038 (org-split-string (car code-info) "\n")))))
2039 (org-export-format-code
2040 (car code-info)
2041 (lambda (loc num ref)
2042 (concat
2044 (when ref
2045 ;; Ensure references are flushed to the right,
2046 ;; separated with 6 spaces from the widest line
2047 ;; of code.
2048 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2049 (format "(%s)" ref)))))
2050 nil (and retain-labels (cdr code-info)))))))
2051 ;; Return value.
2052 (if float-env (format float-env body) body)))
2053 ;; Case 4. Use listings package.
2055 (let ((lst-lang
2056 (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
2057 (caption-str
2058 (when caption
2059 (let ((main (org-export-get-caption src-block))
2060 (secondary (org-export-get-caption src-block t)))
2061 (if (not secondary) (format "{%s}" (org-export-data main info))
2062 (format "{[%s]%s}"
2063 (org-export-data secondary info)
2064 (org-export-data main info)))))))
2065 (concat
2066 ;; Options.
2067 (format "\\lstset{%s}\n"
2068 (org-e-latex--make-option-string
2069 (append org-e-latex-listings-options
2070 `(("language" ,lst-lang))
2071 (when label `(("label" ,label)))
2072 (when caption-str `(("caption" ,caption-str)))
2073 (cond ((not num-start) '(("numbers" "none")))
2074 ((zerop num-start) '(("numbers" "left")))
2075 (t `(("numbers" "left")
2076 ("firstnumber"
2077 ,(number-to-string (1+ num-start)))))))))
2078 ;; Source code.
2079 (format
2080 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2081 (let* ((code-info (org-export-unravel-code src-block))
2082 (max-width
2083 (apply 'max
2084 (mapcar 'length
2085 (org-split-string (car code-info) "\n")))))
2086 (org-export-format-code
2087 (car code-info)
2088 (lambda (loc num ref)
2089 (concat
2091 (when ref
2092 ;; Ensure references are flushed to the right,
2093 ;; separated with 6 spaces from the widest line of
2094 ;; code
2095 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2096 (format "(%s)" ref)))))
2097 nil (and retain-labels (cdr code-info)))))))))))
2100 ;;;; Statistics Cookie
2102 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
2103 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2104 CONTENTS is nil. INFO is a plist holding contextual information."
2105 (replace-regexp-in-string
2106 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2109 ;;;; Strike-Through
2111 (defun org-e-latex-strike-through (strike-through contents info)
2112 "Transcode STRIKE-THROUGH from Org to LaTeX.
2113 CONTENTS is the text with strike-through markup. INFO is a plist
2114 holding contextual information."
2115 (org-e-latex--text-markup contents 'strike-through))
2118 ;;;; Subscript
2120 (defun org-e-latex-subscript (subscript contents info)
2121 "Transcode a SUBSCRIPT object from Org to LaTeX.
2122 CONTENTS is the contents of the object. INFO is a plist holding
2123 contextual information."
2124 (if (= (length contents) 1) (format "$_%s$" contents)
2125 ;; Handle multiple objects in SUBSCRIPT by creating a subscript
2126 ;; command for each of them.
2127 (let ((prev-blanks 0))
2128 (mapconcat
2129 (lambda (obj)
2130 (case (org-element-type obj)
2131 ((entity latex-fragment)
2132 (setq prev-blanks (org-element-property :post-blank obj))
2133 (let ((data (org-trim (org-export-data obj info))))
2134 (string-match
2135 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2136 data)
2137 (format "$_{%s}$" (match-string 1 data))))
2138 (plain-text
2139 (format "$_\\mathrm{%s}$"
2140 (concat (make-string prev-blanks ? )
2141 ;; mathrm command doesn't handle spaces,
2142 ;; so we have to enforce them.
2143 (replace-regexp-in-string
2144 " " "\\\\ " (org-export-data obj info)))))
2145 (otherwise
2146 (setq prev-blanks (org-element-property :post-blank obj))
2147 (format "$_{%s}$" (org-export-data obj info)))))
2148 (org-element-contents subscript) ""))))
2151 ;;;; Superscript
2153 (defun org-e-latex-superscript (superscript contents info)
2154 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2155 CONTENTS is the contents of the object. INFO is a plist holding
2156 contextual information."
2157 (if (= (length contents) 1) (format "$^%s$" contents)
2158 ;; Handle multiple objects in SUPERSCRIPT by creating
2159 ;; a superscript command for each of them.
2160 (let ((prev-blanks 0))
2161 (mapconcat
2162 (lambda (obj)
2163 (case (org-element-type obj)
2164 ((entity latex-fragment)
2165 (setq prev-blanks (org-element-property :post-blank obj))
2166 (let ((data (org-trim (org-export-data obj info))))
2167 (string-match
2168 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2169 data)
2170 (format "$^{%s}$" (match-string 1 data))))
2171 (plain-text
2172 (format "$^\\mathrm{%s}$"
2173 (concat (make-string prev-blanks ? )
2174 ;; mathrm command doesn't handle spaces,
2175 ;; so we have to enforce them.
2176 (replace-regexp-in-string
2177 " " "\\\\ " (org-export-data obj info)))))
2178 (otherwise
2179 (setq prev-blanks (org-element-property :post-blank obj))
2180 (format "$^{%s}$" (org-export-data obj info)))))
2181 (org-element-contents superscript) ""))))
2184 ;;;; Table
2186 ;; `org-e-latex-table' is the entry point for table transcoding. It
2187 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
2188 ;; delegates the job to either `org-e-latex-table--table.el-table' or
2189 ;; `org-e-latex-table--org-table' functions, depending of the type of
2190 ;; the table.
2192 ;; `org-e-latex-table--align-string' is a subroutine used to build
2193 ;; alignment string for Org tables.
2195 (defun org-e-latex-table (table contents info)
2196 "Transcode a TABLE element from Org to LaTeX.
2197 CONTENTS is the contents of the table. INFO is a plist holding
2198 contextual information."
2199 (cond
2200 ;; Case 1: verbatim table.
2201 ((or org-e-latex-tables-verbatim
2202 (let ((attr (mapconcat 'identity
2203 (org-element-property :attr_latex table)
2204 " ")))
2205 (and attr (string-match "\\<verbatim\\>" attr))))
2206 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2207 ;; Re-create table, without affiliated keywords.
2208 (org-trim
2209 (org-element-interpret-data
2210 `(table nil ,@(org-element-contents table))))))
2211 ;; Case 2: table.el table. Convert it using appropriate tools.
2212 ((eq (org-element-property :type table) 'table.el)
2213 (org-e-latex-table--table.el-table table contents info))
2214 ;; Case 3: Standard table.
2215 (t (concat (org-e-latex-table--org-table table contents info)
2216 ;; When there are footnote references within the table,
2217 ;; insert they definitions just after it.
2218 (org-e-latex--delayed-footnotes-definitions table info)))))
2220 (defun org-e-latex-table--align-string (table info)
2221 "Return an appropriate LaTeX alignment string.
2222 TABLE is the considered table. INFO is a plist used as
2223 a communication channel."
2224 (let ((attr (mapconcat 'identity
2225 (org-element-property :attr_latex table)
2226 " ")))
2227 (if (string-match "\\<align=\\(\\S-+\\)" attr) (match-string 1 attr)
2228 (let (alignment)
2229 ;; Extract column groups and alignment from first (non-rule)
2230 ;; row.
2231 (org-element-map
2232 (org-element-map
2233 table 'table-row
2234 (lambda (row)
2235 (and (eq (org-element-property :type row) 'standard) row))
2236 info 'first-match)
2237 'table-cell
2238 (lambda (cell)
2239 (let ((borders (org-export-table-cell-borders cell info)))
2240 ;; Check left border for the first cell only.
2241 (when (and (memq 'left borders) (not alignment))
2242 (push "|" alignment))
2243 (push (case (org-export-table-cell-alignment cell info)
2244 (left "l")
2245 (right "r")
2246 (center "c"))
2247 alignment)
2248 (when (memq 'right borders) (push "|" alignment))))
2249 info)
2250 (apply 'concat (reverse alignment))))))
2252 (defun org-e-latex-table--org-table (table contents info)
2253 "Return appropriate LaTeX code for an Org table.
2255 TABLE is the table type element to transcode. CONTENTS is its
2256 contents, as a string. INFO is a plist used as a communication
2257 channel.
2259 This function assumes TABLE has `org' as its `:type' attribute."
2260 (let* ((label (org-element-property :name table))
2261 (caption (org-e-latex--caption/label-string table info))
2262 (attr (mapconcat 'identity
2263 (org-element-property :attr_latex table)
2264 " "))
2265 ;; Determine alignment string.
2266 (alignment (org-e-latex-table--align-string table info))
2267 ;; Determine environment for the table: longtable, tabular...
2268 (table-env (cond
2269 ((not attr) org-e-latex-default-table-environment)
2270 ((string-match "\\<longtable\\>" attr) "longtable")
2271 ((string-match "\\<tabular.?\\>" attr)
2272 (org-match-string-no-properties 0 attr))
2273 (t org-e-latex-default-table-environment)))
2274 ;; If table is a float, determine environment: table, table*
2275 ;; or sidewaystable.
2276 (float-env (cond
2277 ((string= "longtable" table-env) nil)
2278 ((and attr (string-match "\\<sidewaystable\\>" attr))
2279 "sidewaystable")
2280 ((and attr
2281 (or (string-match (regexp-quote "table*") attr)
2282 (string-match "\\<multicolumn\\>" attr)))
2283 "table*")
2284 ((or (not (string= caption "")) label) "table")))
2285 ;; Extract others display options.
2286 (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
2287 (org-match-string-no-properties 1 attr)))
2288 (placement
2289 (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
2290 (org-match-string-no-properties 1 attr)
2291 (format "[%s]" org-e-latex-default-figure-position))))
2292 ;; Prepare the final format string for the table.
2293 (cond
2294 ;; Longtable.
2295 ((string= "longtable" table-env)
2296 (format
2297 "\\begin{longtable}{%s}\n%s%s%s\\end{longtable}"
2298 alignment
2299 (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
2300 (concat (org-trim caption) "\\\\\n"))
2301 contents
2302 (if (or org-e-latex-table-caption-above (string= "" caption)) ""
2303 (concat (org-trim caption) "\\\\\n"))))
2304 ;; Others.
2305 (t (concat (if float-env
2306 (concat
2307 (format "\\begin{%s}%s\n" float-env placement)
2308 (if org-e-latex-table-caption-above caption "")
2309 (when org-e-latex-tables-centered "\\centering\n"))
2310 (when org-e-latex-tables-centered "\\begin{center}\n"))
2311 (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2312 table-env
2313 (if width (format "{%s}" width) "")
2314 alignment
2315 contents
2316 table-env)
2317 (if float-env
2318 (concat (if org-e-latex-table-caption-above "" caption)
2319 (format "\n\\end{%s}" float-env))
2320 (when org-e-latex-tables-centered "\n\\end{center}")))))))
2322 (defun org-e-latex-table--table.el-table (table contents info)
2323 "Return appropriate LaTeX code for a table.el table.
2325 TABLE is the table type element to transcode. CONTENTS is its
2326 contents, as a string. INFO is a plist used as a communication
2327 channel.
2329 This function assumes TABLE has `table.el' as its `:type'
2330 attribute."
2331 (require 'table)
2332 ;; Ensure "*org-export-table*" buffer is empty.
2333 (with-current-buffer (get-buffer-create "*org-export-table*")
2334 (erase-buffer))
2335 (let ((output (with-temp-buffer
2336 (insert (org-element-property :value table))
2337 (goto-char 1)
2338 (re-search-forward "^[ \t]*|[^|]" nil t)
2339 (table-generate-source 'latex "*org-export-table*")
2340 (with-current-buffer "*org-export-table*"
2341 (org-trim (buffer-string))))))
2342 (kill-buffer (get-buffer "*org-export-table*"))
2343 ;; Remove left out comments.
2344 (while (string-match "^%.*\n" output)
2345 (setq output (replace-match "" t t output)))
2346 ;; When the "rmlines" attribute is provided, remove all hlines but
2347 ;; the the one separating heading from the table body.
2348 (let ((attr (mapconcat 'identity
2349 (org-element-property :attr_latex table)
2350 " ")))
2351 (when (and attr (string-match "\\<rmlines\\>" attr))
2352 (let ((n 0) (pos 0))
2353 (while (and (< (length output) pos)
2354 (setq pos (string-match "^\\\\hline\n?" output pos)))
2355 (incf n)
2356 (unless (= n 2)
2357 (setq output (replace-match "" nil nil output)))))))
2358 (if (not org-e-latex-tables-centered) output
2359 (format "\\begin{center}\n%s\n\\end{center}" output))))
2362 ;;;; Table Cell
2364 (defun org-e-latex-table-cell (table-cell contents info)
2365 "Transcode a TABLE-CELL element from Org to LaTeX.
2366 CONTENTS is the cell contents. INFO is a plist used as
2367 a communication channel."
2368 (concat (if (and contents
2369 org-e-latex-table-scientific-notation
2370 (string-match orgtbl-exp-regexp contents))
2371 ;; Use appropriate format string for scientific
2372 ;; notation.
2373 (format org-e-latex-table-scientific-notation
2374 (match-string 1 contents)
2375 (match-string 2 contents))
2376 contents)
2377 (when (org-export-get-next-element table-cell info) " & ")))
2380 ;;;; Table Row
2382 (defun org-e-latex-table-row (table-row contents info)
2383 "Transcode a TABLE-ROW element from Org to LaTeX.
2384 CONTENTS is the contents of the row. INFO is a plist used as
2385 a communication channel."
2386 ;; Rules are ignored since table separators are deduced from
2387 ;; borders of the current row.
2388 (when (eq (org-element-property :type table-row) 'standard)
2389 (let* ((attr (mapconcat 'identity
2390 (org-element-property
2391 :attr_latex (org-export-get-parent table-row))
2392 " "))
2393 (longtablep (and attr (string-match "\\<longtable\\>" attr)))
2394 (booktabsp
2395 (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
2396 org-e-latex-tables-booktabs))
2397 ;; TABLE-ROW's borders are extracted from its first cell.
2398 (borders
2399 (org-export-table-cell-borders
2400 (car (org-element-contents table-row)) info)))
2401 (concat
2402 ;; When BOOKTABS are activated enforce top-rule even when no
2403 ;; hline was specifically marked.
2404 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2405 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2406 contents "\\\\\n"
2407 (cond
2408 ;; Special case for long tables. Define header and footers.
2409 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2410 (format "%s
2411 \\endhead
2412 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2413 \\endfoot
2414 \\endlastfoot"
2415 (if booktabsp "\\midrule" "\\hline")
2416 (if booktabsp "\\midrule" "\\hline")
2417 ;; Number of columns.
2418 (cdr (org-export-table-dimensions
2419 (org-export-get-parent-table table-row) info))))
2420 ;; When BOOKTABS are activated enforce bottom rule even when
2421 ;; no hline was specifically marked.
2422 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2423 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2424 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2427 ;;;; Target
2429 (defun org-e-latex-target (target contents info)
2430 "Transcode a TARGET object from Org to LaTeX.
2431 CONTENTS is nil. INFO is a plist holding contextual
2432 information."
2433 (format "\\label{%s}"
2434 (org-export-solidify-link-text (org-element-property :value target))))
2437 ;;;; Timestamp
2439 (defun org-e-latex-timestamp (timestamp contents info)
2440 "Transcode a TIMESTAMP object from Org to LaTeX.
2441 CONTENTS is nil. INFO is a plist holding contextual
2442 information."
2443 (let ((value (org-translate-time
2444 (org-element-property :raw-value timestamp))))
2445 (case (org-element-property :type timestamp)
2446 (active (format org-e-latex-active-timestamp-format value))
2447 (active-range
2448 (let ((timestamps (org-split-string value "--")))
2449 (concat
2450 (format org-e-latex-active-timestamp-format (car timestamps))
2451 "--"
2452 (format org-e-latex-active-timestamp-format (cdr timestamps)))))
2453 (inactive (format org-e-latex-inactive-timestamp-format value))
2454 (inactive-range
2455 (let ((timestamps (org-split-string value "--")))
2456 (concat
2457 (format org-e-latex-inactive-timestamp-format (car timestamps))
2458 "--"
2459 (format org-e-latex-inactive-timestamp-format (cdr timestamps)))))
2460 (otherwise (format org-e-latex-diary-timestamp-format value)))))
2463 ;;;; Underline
2465 (defun org-e-latex-underline (underline contents info)
2466 "Transcode UNDERLINE from Org to LaTeX.
2467 CONTENTS is the text with underline markup. INFO is a plist
2468 holding contextual information."
2469 (org-e-latex--text-markup contents 'underline))
2472 ;;;; Verbatim
2474 (defun org-e-latex-verbatim (verbatim contents info)
2475 "Transcode a VERBATIM object from Org to LaTeX.
2476 CONTENTS is nil. INFO is a plist used as a communication
2477 channel."
2478 (org-e-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2481 ;;;; Verse Block
2483 (defun org-e-latex-verse-block (verse-block contents info)
2484 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2485 CONTENTS is verse block contents. INFO is a plist holding
2486 contextual information."
2487 (org-e-latex--wrap-label
2488 verse-block
2489 ;; In a verse environment, add a line break to each newline
2490 ;; character and change each white space at beginning of a line
2491 ;; into a space of 1 em. Also change each blank line with
2492 ;; a vertical space of 1 em.
2493 (progn
2494 (setq contents (replace-regexp-in-string
2495 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2496 (replace-regexp-in-string
2497 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2498 (while (string-match "^[ \t]+" contents)
2499 (let ((new-str (format "\\hspace*{%dem}"
2500 (length (match-string 0 contents)))))
2501 (setq contents (replace-match new-str nil t contents))))
2502 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2506 ;;; End-user functions
2508 ;;;###autoload
2509 (defun org-e-latex-export-as-latex
2510 (&optional subtreep visible-only body-only ext-plist)
2511 "Export current buffer as a LaTeX buffer.
2513 If narrowing is active in the current buffer, only export its
2514 narrowed part.
2516 If a region is active, export that region.
2518 When optional argument SUBTREEP is non-nil, export the sub-tree
2519 at point, extracting information from the headline properties
2520 first.
2522 When optional argument VISIBLE-ONLY is non-nil, don't export
2523 contents of hidden elements.
2525 When optional argument BODY-ONLY is non-nil, only write code
2526 between \"\\begin{document}\" and \"\\end{document}\".
2528 EXT-PLIST, when provided, is a property list with external
2529 parameters overriding Org default settings, but still inferior to
2530 file-local settings.
2532 Export is done in a buffer named \"*Org E-LATEX Export*\", which
2533 will be displayed when `org-export-show-temporary-export-buffer'
2534 is non-nil."
2535 (interactive)
2536 (let ((outbuf (org-export-to-buffer
2537 'e-latex "*Org E-LATEX Export*"
2538 subtreep visible-only body-only ext-plist)))
2539 (with-current-buffer outbuf (LaTeX-mode))
2540 (when org-export-show-temporary-export-buffer
2541 (switch-to-buffer-other-window outbuf))))
2543 ;;;###autoload
2544 (defun org-e-latex-export-to-latex
2545 (&optional subtreep visible-only body-only ext-plist pub-dir)
2546 "Export current buffer to a LaTeX file.
2548 If narrowing is active in the current buffer, only export its
2549 narrowed part.
2551 If a region is active, export that region.
2553 When optional argument SUBTREEP is non-nil, export the sub-tree
2554 at point, extracting information from the headline properties
2555 first.
2557 When optional argument VISIBLE-ONLY is non-nil, don't export
2558 contents of hidden elements.
2560 When optional argument BODY-ONLY is non-nil, only write code
2561 between \"\\begin{document}\" and \"\\end{document}\".
2563 EXT-PLIST, when provided, is a property list with external
2564 parameters overriding Org default settings, but still inferior to
2565 file-local settings.
2567 When optional argument PUB-DIR is set, use it as the publishing
2568 directory.
2570 Return output file's name."
2571 (interactive)
2572 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
2573 (org-export-to-file
2574 'e-latex outfile subtreep visible-only body-only ext-plist)))
2576 ;;;###autoload
2577 (defun org-e-latex-export-to-pdf
2578 (&optional subtreep visible-only body-only ext-plist pub-dir)
2579 "Export current buffer to LaTeX then process through to PDF.
2581 If narrowing is active in the current buffer, only export its
2582 narrowed part.
2584 If a region is active, export that region.
2586 When optional argument SUBTREEP is non-nil, export the sub-tree
2587 at point, extracting information from the headline properties
2588 first.
2590 When optional argument VISIBLE-ONLY is non-nil, don't export
2591 contents of hidden elements.
2593 When optional argument BODY-ONLY is non-nil, only write code
2594 between \"\\begin{document}\" and \"\\end{document}\".
2596 EXT-PLIST, when provided, is a property list with external
2597 parameters overriding Org default settings, but still inferior to
2598 file-local settings.
2600 When optional argument PUB-DIR is set, use it as the publishing
2601 directory.
2603 Return PDF file's name."
2604 (interactive)
2605 (org-e-latex-compile
2606 (org-e-latex-export-to-latex
2607 subtreep visible-only body-only ext-plist pub-dir)))
2609 (defun org-e-latex-compile (texfile)
2610 "Compile a TeX file.
2612 TEXFILE is the name of the file being compiled. Processing is
2613 done through the command specified in `org-e-latex-pdf-process'.
2615 Return PDF file name or an error if it couldn't be produced."
2616 (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
2617 (full-name (file-truename texfile))
2618 (out-dir (file-name-directory texfile))
2619 ;; Make sure `default-directory' is set to TEXFILE directory,
2620 ;; not to whatever value the current buffer may have.
2621 (default-directory (file-name-directory full-name))
2622 errors)
2623 (message (format "Processing LaTeX file %s ..." texfile))
2624 (save-window-excursion
2625 (cond
2626 ;; A function is provided: Apply it.
2627 ((functionp org-e-latex-pdf-process)
2628 (funcall org-e-latex-pdf-process (shell-quote-argument texfile)))
2629 ;; A list is provided: Replace %b, %f and %o with appropriate
2630 ;; values in each command before applying it. Output is
2631 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2632 ((consp org-e-latex-pdf-process)
2633 (let ((outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2634 (mapc
2635 (lambda (command)
2636 (shell-command
2637 (replace-regexp-in-string
2638 "%b" (shell-quote-argument base-name)
2639 (replace-regexp-in-string
2640 "%f" (shell-quote-argument full-name)
2641 (replace-regexp-in-string
2642 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2643 outbuf))
2644 org-e-latex-pdf-process)
2645 ;; Collect standard errors from output buffer.
2646 (setq errors (org-e-latex--collect-errors outbuf))))
2647 (t (error "No valid command to process to PDF")))
2648 (let ((pdffile (concat out-dir base-name ".pdf")))
2649 ;; Check for process failure. Provide collected errors if
2650 ;; possible.
2651 (if (not (file-exists-p pdffile))
2652 (error (concat (format "PDF file %s wasn't produced" pdffile)
2653 (when errors (concat ": " errors))))
2654 ;; Else remove log files, when specified, and signal end of
2655 ;; process to user, along with any error encountered.
2656 (when org-e-latex-remove-logfiles
2657 (dolist (ext org-e-latex-logfiles-extensions)
2658 (let ((file (concat out-dir base-name "." ext)))
2659 (when (file-exists-p file) (delete-file file)))))
2660 (message (concat "Process completed"
2661 (if (not errors) "."
2662 (concat " with errors: " errors)))))
2663 ;; Return output file name.
2664 pdffile))))
2666 (defun org-e-latex--collect-errors (buffer)
2667 "Collect some kind of errors from \"pdflatex\" command output.
2669 BUFFER is the buffer containing output.
2671 Return collected error types as a string, or nil if there was
2672 none."
2673 (with-current-buffer buffer
2674 (save-excursion
2675 (goto-char (point-max))
2676 (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
2677 (let ((case-fold-search t)
2678 (errors ""))
2679 (dolist (latex-error org-e-latex-known-errors)
2680 (when (save-excursion (re-search-forward (car latex-error) nil t))
2681 (setq errors (concat errors " " (cdr latex-error)))))
2682 (and (org-string-nw-p errors) (org-trim errors)))))))
2684 ;;;###autoload
2685 (defun org-e-latex-publish-to-latex (plist filename pub-dir)
2686 "Publish an Org file to LaTeX.
2688 FILENAME is the filename of the Org file to be published. PLIST
2689 is the property list for the given project. PUB-DIR is the
2690 publishing directory.
2692 Return output file name."
2693 (org-e-publish-org-to 'e-latex filename ".tex" plist pub-dir))
2695 ;;;###autoload
2696 (defun org-e-latex-publish-to-pdf (plist filename pub-dir)
2697 "Publish an Org file to PDF (via LaTeX).
2699 FILENAME is the filename of the Org file to be published. PLIST
2700 is the property list for the given project. PUB-DIR is the
2701 publishing directory.
2703 Return output file name."
2704 ;; Unlike to `org-e-latex-publish-to-latex', PDF file is generated
2705 ;; in working directory and then moved to publishing directory.
2706 (org-e-publish-attachment
2707 plist
2708 (org-e-latex-compile (org-e-publish-org-to 'e-latex filename ".tex" plist))
2709 pub-dir))
2712 (provide 'org-e-latex)
2713 ;;; org-e-latex.el ends here