org-e-latex: Refactor code
[org-mode.git] / contrib / lisp / org-e-latex.el
blob6de426661b7a3626784cb3ba6af8d7a10950c6b2
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))
40 (defvar org-export-latex-default-packages-alist)
41 (defvar org-export-latex-packages-alist)
43 (declare-function org-element-property "org-element" (property element))
44 (declare-function org-element-normalize-string "org-element" (s))
46 (declare-function org-export-data "org-export" (data info))
47 (declare-function org-export-directory "org-export" (type plist))
48 (declare-function org-export-expand-macro "org-export" (macro info))
49 (declare-function org-export-first-sibling-p "org-export" (headline))
50 (declare-function org-export-footnote-first-reference-p "org-export"
51 (footnote-reference info))
52 (declare-function org-export-format-code "org-export"
53 (code fun &optional num-lines ref-alist))
54 (declare-function org-export-format-code-default "org-export" (element info))
55 (declare-function org-export-get-coderef-format "org-export" (path desc))
56 (declare-function org-export-get-footnote-definition "org-export"
57 (footnote-reference info))
58 (declare-function org-export-get-footnote-number "org-export" (footnote info))
59 (declare-function org-export-get-previous-element "org-export" (blob))
60 (declare-function org-export-get-relative-level "org-export" (headline info))
61 (declare-function org-export-unravel-code "org-export" (element))
62 (declare-function org-export-inline-image-p "org-export"
63 (link &optional extensions))
64 (declare-function org-export-last-sibling-p "org-export" (headline))
65 (declare-function org-export-low-level-p "org-export" (headline info))
66 (declare-function org-export-output-file-name
67 "org-export" (extension &optional subtreep pub-dir))
68 (declare-function org-export-resolve-coderef "org-export" (ref info))
69 (declare-function org-export-resolve-fuzzy-link "org-export" (link info))
70 (declare-function org-export-resolve-radio-link "org-export" (link info))
71 (declare-function org-export-solidify-link-text "org-export" (s))
72 (declare-function
73 org-export-to-buffer "org-export"
74 (backend buffer &optional subtreep visible-only body-only ext-plist))
75 (declare-function
76 org-export-to-file "org-export"
77 (backend file &optional subtreep visible-only body-only ext-plist))
81 ;;; Define Back-End
83 (defvar org-e-latex-translate-alist
84 '((babel-call . org-e-latex-babel-call)
85 (bold . org-e-latex-bold)
86 (center-block . org-e-latex-center-block)
87 (clock . org-e-latex-clock)
88 (code . org-e-latex-code)
89 (comment . org-e-latex-comment)
90 (comment-block . org-e-latex-comment-block)
91 (drawer . org-e-latex-drawer)
92 (dynamic-block . org-e-latex-dynamic-block)
93 (entity . org-e-latex-entity)
94 (example-block . org-e-latex-example-block)
95 (export-block . org-e-latex-export-block)
96 (export-snippet . org-e-latex-export-snippet)
97 (fixed-width . org-e-latex-fixed-width)
98 (footnote-definition . org-e-latex-footnote-definition)
99 (footnote-reference . org-e-latex-footnote-reference)
100 (headline . org-e-latex-headline)
101 (horizontal-rule . org-e-latex-horizontal-rule)
102 (inline-babel-call . org-e-latex-inline-babel-call)
103 (inline-src-block . org-e-latex-inline-src-block)
104 (inlinetask . org-e-latex-inlinetask)
105 (italic . org-e-latex-italic)
106 (item . org-e-latex-item)
107 (keyword . org-e-latex-keyword)
108 (latex-environment . org-e-latex-latex-environment)
109 (latex-fragment . org-e-latex-latex-fragment)
110 (line-break . org-e-latex-line-break)
111 (link . org-e-latex-link)
112 (macro . org-e-latex-macro)
113 (paragraph . org-e-latex-paragraph)
114 (plain-list . org-e-latex-plain-list)
115 (plain-text . org-e-latex-plain-text)
116 (planning . org-e-latex-planning)
117 (property-drawer . org-e-latex-property-drawer)
118 (quote-block . org-e-latex-quote-block)
119 (quote-section . org-e-latex-quote-section)
120 (radio-target . org-e-latex-radio-target)
121 (section . org-e-latex-section)
122 (special-block . org-e-latex-special-block)
123 (src-block . org-e-latex-src-block)
124 (statistics-cookie . org-e-latex-statistics-cookie)
125 (strike-through . org-e-latex-strike-through)
126 (subscript . org-e-latex-subscript)
127 (superscript . org-e-latex-superscript)
128 (table . org-e-latex-table)
129 (table-cell . org-e-latex-table-cell)
130 (table-row . org-e-latex-table-row)
131 (target . org-e-latex-target)
132 (template . org-e-latex-template)
133 (timestamp . org-e-latex-timestamp)
134 (underline . org-e-latex-underline)
135 (verbatim . org-e-latex-verbatim)
136 (verse-block . org-e-latex-verse-block))
137 "Alist between element or object types and translators.")
139 (defconst org-e-latex-options-alist
140 '((:date "DATE" nil org-e-latex-date-format t)
141 (:latex-class "LATEX_CLASS" nil org-e-latex-default-class t)
142 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
143 (:latex-header-extra "LATEX_HEADER" nil nil newline))
144 "Alist between LaTeX export properties and ways to set them.
145 See `org-export-options-alist' for more information on the
146 structure of the values.")
150 ;;; Internal Variables
152 (defconst org-e-latex-babel-language-alist
153 '(("af" . "afrikaans")
154 ("bg" . "bulgarian")
155 ("bt-br" . "brazilian")
156 ("ca" . "catalan")
157 ("cs" . "czech")
158 ("cy" . "welsh")
159 ("da" . "danish")
160 ("de" . "germanb")
161 ("de-at" . "naustrian")
162 ("de-de" . "ngerman")
163 ("el" . "greek")
164 ("en" . "english")
165 ("en-au" . "australian")
166 ("en-ca" . "canadian")
167 ("en-gb" . "british")
168 ("en-ie" . "irish")
169 ("en-nz" . "newzealand")
170 ("en-us" . "american")
171 ("es" . "spanish")
172 ("et" . "estonian")
173 ("eu" . "basque")
174 ("fi" . "finnish")
175 ("fr" . "frenchb")
176 ("fr-ca" . "canadien")
177 ("gl" . "galician")
178 ("hr" . "croatian")
179 ("hu" . "hungarian")
180 ("id" . "indonesian")
181 ("is" . "icelandic")
182 ("it" . "italian")
183 ("la" . "latin")
184 ("ms" . "malay")
185 ("nl" . "dutch")
186 ("no-no" . "nynorsk")
187 ("pl" . "polish")
188 ("pt" . "portuguese")
189 ("ro" . "romanian")
190 ("ru" . "russian")
191 ("sa" . "sanskrit")
192 ("sb" . "uppersorbian")
193 ("sk" . "slovak")
194 ("sl" . "slovene")
195 ("sq" . "albanian")
196 ("sr" . "serbian")
197 ("sv" . "swedish")
198 ("ta" . "tamil")
199 ("tr" . "turkish")
200 ("uk" . "ukrainian"))
201 "Alist between language code and corresponding Babel option.")
205 ;;; User Configurable Variables
207 (defgroup org-export-e-latex nil
208 "Options for exporting Org mode files to LaTeX."
209 :tag "Org Export LaTeX"
210 :group 'org-export)
213 ;;;; Preamble
215 (defcustom org-e-latex-default-class "article"
216 "The default LaTeX class."
217 :group 'org-export-e-latex
218 :type '(string :tag "LaTeX class"))
220 (defcustom org-e-latex-classes
221 '(("article"
222 "\\documentclass[11pt]{article}"
223 ("\\section{%s}" . "\\section*{%s}")
224 ("\\subsection{%s}" . "\\subsection*{%s}")
225 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
226 ("\\paragraph{%s}" . "\\paragraph*{%s}")
227 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
228 ("report"
229 "\\documentclass[11pt]{report}"
230 ("\\part{%s}" . "\\part*{%s}")
231 ("\\chapter{%s}" . "\\chapter*{%s}")
232 ("\\section{%s}" . "\\section*{%s}")
233 ("\\subsection{%s}" . "\\subsection*{%s}")
234 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
235 ("book"
236 "\\documentclass[11pt]{book}"
237 ("\\part{%s}" . "\\part*{%s}")
238 ("\\chapter{%s}" . "\\chapter*{%s}")
239 ("\\section{%s}" . "\\section*{%s}")
240 ("\\subsection{%s}" . "\\subsection*{%s}")
241 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
242 "Alist of LaTeX classes and associated header and structure.
243 If #+LaTeX_CLASS is set in the buffer, use its value and the
244 associated information. Here is the structure of each cell:
246 \(class-name
247 header-string
248 \(numbered-section . unnumbered-section\)
249 ...\)
251 The header string
252 -----------------
254 The HEADER-STRING is the header that will be inserted into the
255 LaTeX file. It should contain the \\documentclass macro, and
256 anything else that is needed for this setup. To this header, the
257 following commands will be added:
259 - Calls to \\usepackage for all packages mentioned in the
260 variables `org-export-latex-default-packages-alist' and
261 `org-export-latex-packages-alist'. Thus, your header
262 definitions should avoid to also request these packages.
264 - Lines specified via \"#+LaTeX_HEADER:\"
266 If you need more control about the sequence in which the header
267 is built up, or if you want to exclude one of these building
268 blocks for a particular class, you can use the following
269 macro-like placeholders.
271 [DEFAULT-PACKAGES] \\usepackage statements for default packages
272 [NO-DEFAULT-PACKAGES] do not include any of the default packages
273 [PACKAGES] \\usepackage statements for packages
274 [NO-PACKAGES] do not include the packages
275 [EXTRA] the stuff from #+LaTeX_HEADER
276 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
277 [BEAMER-HEADER-EXTRA] the beamer extra headers
279 So a header like
281 \\documentclass{article}
282 [NO-DEFAULT-PACKAGES]
283 [EXTRA]
284 \\providecommand{\\alert}[1]{\\textbf{#1}}
285 [PACKAGES]
287 will omit the default packages, and will include the
288 #+LaTeX_HEADER lines, then have a call to \\providecommand, and
289 then place \\usepackage commands based on the content of
290 `org-export-latex-packages-alist'.
292 If your header, `org-export-latex-default-packages-alist' or
293 `org-export-latex-packages-alist' inserts
294 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
295 replaced with a coding system derived from
296 `buffer-file-coding-system'. See also the variable
297 `org-e-latex-inputenc-alist' for a way to influence this
298 mechanism.
300 The sectioning structure
301 ------------------------
303 The sectioning structure of the class is given by the elements
304 following the header string. For each sectioning level, a number
305 of strings is specified. A %s formatter is mandatory in each
306 section string and will be replaced by the title of the section.
308 Instead of a cons cell \(numbered . unnumbered\), you can also
309 provide a list of 2 or 4 elements,
311 \(numbered-open numbered-close\)
315 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
317 providing opening and closing strings for a LaTeX environment
318 that should represent the document section. The opening clause
319 should have a %s to represent the section title.
321 Instead of a list of sectioning commands, you can also specify
322 a function name. That function will be called with two
323 parameters, the \(reduced) level of the headline, and a predicate
324 non-nil when the headline should be numbered. It must return
325 a format string in which the section title will be added."
326 :group 'org-export-e-latex
327 :type '(repeat
328 (list (string :tag "LaTeX class")
329 (string :tag "LaTeX header")
330 (repeat :tag "Levels" :inline t
331 (choice
332 (cons :tag "Heading"
333 (string :tag " numbered")
334 (string :tag "unnumbered"))
335 (list :tag "Environment"
336 (string :tag "Opening (numbered)")
337 (string :tag "Closing (numbered)")
338 (string :tag "Opening (unnumbered)")
339 (string :tag "Closing (unnumbered)"))
340 (function :tag "Hook computing sectioning"))))))
342 (defcustom org-e-latex-inputenc-alist nil
343 "Alist of inputenc coding system names, and what should really be used.
344 For example, adding an entry
346 (\"utf8\" . \"utf8x\")
348 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
349 are written as utf8 files."
350 :group 'org-export-e-latex
351 :type '(repeat
352 (cons
353 (string :tag "Derived from buffer")
354 (string :tag "Use this instead"))))
356 (defcustom org-e-latex-date-format
357 "\\today"
358 "Format string for \\date{...}."
359 :group 'org-export-e-latex
360 :type 'boolean)
362 (defcustom org-e-latex-title-command "\\maketitle"
363 "The command used to insert the title just after \\begin{document}.
364 If this string contains the formatting specification \"%s\" then
365 it will be used as a formatting string, passing the title as an
366 argument."
367 :group 'org-export-e-latex
368 :type 'string)
371 ;;;; Headline
373 (defcustom org-e-latex-format-headline-function nil
374 "Function to format headline text.
376 This function will be called with 5 arguments:
377 TODO the todo keyword (string or nil).
378 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
379 PRIORITY the priority of the headline (integer or nil)
380 TEXT the main headline text (string).
381 TAGS the tags as a list of strings (list of strings or nil).
383 The function result will be used in the section format string.
385 As an example, one could set the variable to the following, in
386 order to reproduce the default set-up:
388 \(defun org-e-latex-format-headline (todo todo-type priority text tags)
389 \"Default format function for an headline.\"
390 \(concat (when todo
391 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo))
392 \(when priority
393 \(format \"\\\\framebox{\\\\#%c} \" priority))
394 text
395 \(when tags
396 \(format \"\\\\hfill{}\\\\textsc{%s}\"
397 \(mapconcat 'identity tags \":\"))))"
398 :group 'org-export-e-latex
399 :type 'function)
402 ;;;; Footnotes
404 (defcustom org-e-latex-footnote-separator "\\textsuperscript{,}\\,"
405 "Text used to separate footnotes."
406 :group 'org-export-e-latex
407 :type 'string)
410 ;;;; Timestamps
412 (defcustom org-e-latex-active-timestamp-format "\\textit{%s}"
413 "A printf format string to be applied to active timestamps."
414 :group 'org-export-e-latex
415 :type 'string)
417 (defcustom org-e-latex-inactive-timestamp-format "\\textit{%s}"
418 "A printf format string to be applied to inactive timestamps."
419 :group 'org-export-e-latex
420 :type 'string)
422 (defcustom org-e-latex-diary-timestamp-format "\\textit{%s}"
423 "A printf format string to be applied to diary timestamps."
424 :group 'org-export-e-latex
425 :type 'string)
428 ;;;; Links
430 (defcustom org-e-latex-image-default-option "width=.9\\linewidth"
431 "Default option for images."
432 :group 'org-export-e-latex
433 :type 'string)
435 (defcustom org-e-latex-default-figure-position "htb"
436 "Default position for latex figures."
437 :group 'org-export-e-latex
438 :type 'string)
440 (defcustom org-e-latex-inline-image-rules
441 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\)\\'"))
442 "Rules characterizing image files that can be inlined into LaTeX.
444 A rule consists in an association whose key is the type of link
445 to consider, and value is a regexp that will be matched against
446 link's path.
448 Note that, by default, the image extension *actually* allowed
449 depend on the way the LaTeX file is processed. When used with
450 pdflatex, pdf, jpg and png images are OK. When processing
451 through dvi to Postscript, only ps and eps are allowed. The
452 default we use here encompasses both."
453 :group 'org-export-e-latex
454 :type '(alist :key-type (string :tag "Type")
455 :value-type (regexp :tag "Path")))
457 (defcustom org-e-latex-link-with-unknown-path-format "\\texttt{%s}"
458 "Format string for links with unknown path type."
459 :group 'org-export-latex
460 :type 'string)
463 ;;;; Tables
465 (defcustom org-e-latex-default-table-environment "tabular"
466 "Default environment used to build tables."
467 :group 'org-export-e-latex
468 :type 'string)
470 (defcustom org-e-latex-tables-centered t
471 "When non-nil, tables are exported in a center environment."
472 :group 'org-export-e-latex
473 :type 'boolean)
475 (defcustom org-e-latex-tables-verbatim nil
476 "When non-nil, tables are exported verbatim."
477 :group 'org-export-e-latex
478 :type 'boolean)
480 (defcustom org-e-latex-tables-booktabs nil
481 "When non-nil, display tables in a formal \"booktabs\" style.
482 This option assumes that the \"booktabs\" package is properly
483 loaded in the header of the document. This value can be ignored
484 locally with \"booktabs=yes\" and \"booktabs=no\" LaTeX
485 attributes."
486 :group 'org-export-e-latex
487 :type 'boolean)
489 (defcustom org-e-latex-table-caption-above t
490 "When non-nil, place caption string at the beginning of the table.
491 Otherwise, place it near the end."
492 :group 'org-export-e-latex
493 :type 'boolean)
495 (defcustom org-e-latex-table-scientific-notation "%s\\,(%s)"
496 "Format string to display numbers in scientific notation.
497 The format should have \"%s\" twice, for mantissa and exponent
498 \(i.e. \"%s\\\\times10^{%s}\").
500 When nil, no transformation is made."
501 :group 'org-export-e-latex
502 :type '(choice
503 (string :tag "Format string")
504 (const :tag "No formatting")))
507 ;;;; Text markup
509 (defcustom org-e-latex-text-markup-alist '((bold . "\\textbf{%s}")
510 (code . verb)
511 (italic . "\\emph{%s}")
512 (strike-through . "\\st{%s}")
513 (underline . "\\underline{%s}")
514 (verbatim . protectedtexttt))
515 "Alist of LaTeX expressions to convert text markup.
517 The key must be a symbol among `bold', `code', `italic',
518 `strike-through', `underline' and `verbatim'. The value is
519 a formatting string to wrap fontified text with.
521 Value can also be set to the following symbols: `verb' and
522 `protectedtexttt'. For the former, Org will use \"\\verb\" to
523 create a format string and select a delimiter character that
524 isn't in the string. For the latter, Org will use \"\\texttt\"
525 to typeset and try to protect special characters.
527 If no association can be found for a given markup, text will be
528 returned as-is."
529 :group 'org-export-e-latex
530 :type 'alist
531 :options '(bold code italic strike-through underline verbatim))
534 ;;;; Drawers
536 (defcustom org-e-latex-format-drawer-function nil
537 "Function called to format a drawer in LaTeX code.
539 The function must accept two parameters:
540 NAME the drawer name, like \"LOGBOOK\"
541 CONTENTS the contents of the drawer.
543 The function should return the string to be exported.
545 For example, the variable could be set to the following function
546 in order to mimic default behaviour:
548 \(defun org-e-latex-format-drawer-default \(name contents\)
549 \"Format a drawer element for LaTeX export.\"
550 contents\)"
551 :group 'org-export-e-latex
552 :type 'function)
555 ;;;; Inlinetasks
557 (defcustom org-e-latex-format-inlinetask-function nil
558 "Function called to format an inlinetask in LaTeX code.
560 The function must accept six parameters:
561 TODO the todo keyword, as a string
562 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
563 PRIORITY the inlinetask priority, as a string
564 NAME the inlinetask name, as a string.
565 TAGS the inlinetask tags, as a list of strings.
566 CONTENTS the contents of the inlinetask, as a string.
568 The function should return the string to be exported.
570 For example, the variable could be set to the following function
571 in order to mimic default behaviour:
573 \(defun org-e-latex-format-inlinetask \(todo type priority name tags contents\)
574 \"Format an inline task element for LaTeX export.\"
575 \(let ((full-title
576 \(concat
577 \(when todo
578 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
579 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
580 title
581 \(when tags
582 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
583 \(mapconcat 'identity tags \":\")))))
584 \(format (concat \"\\\\begin{center}\\n\"
585 \"\\\\fbox{\\n\"
586 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
587 \"%s\\n\\n\"
588 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
589 \"%s\"
590 \"\\\\end{minipage}}\"
591 \"\\\\end{center}\")
592 full-title contents))"
593 :group 'org-export-e-latex
594 :type 'function)
597 ;; Src blocks
599 (defcustom org-e-latex-listings nil
600 "Non-nil means export source code using the listings package.
601 This package will fontify source code, possibly even with color.
602 If you want to use this, you also need to make LaTeX use the
603 listings package, and if you want to have color, the color
604 package. Just add these to `org-export-latex-packages-alist',
605 for example using customize, or with something like:
607 \(require 'org-e-latex)
608 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"listings\"))
609 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"color\"))
611 Alternatively,
613 \(setq org-e-latex-listings 'minted)
615 causes source code to be exported using the minted package as
616 opposed to listings. If you want to use minted, you need to add
617 the minted package to `org-export-latex-packages-alist', for
618 example using customize, or with
620 \(require 'org-e-latex)
621 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"minted\"))
623 In addition, it is necessary to install pygments
624 \(http://pygments.org), and to configure the variable
625 `org-e-latex-pdf-process' so that the -shell-escape option is
626 passed to pdflatex."
627 :group 'org-export-e-latex
628 :type '(choice
629 (const :tag "Use listings" t)
630 (const :tag "Use minted" 'minted)
631 (const :tag "Export verbatim" nil)))
633 (defcustom org-e-latex-listings-langs
634 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
635 (c "C") (cc "C++")
636 (fortran "fortran")
637 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
638 (html "HTML") (xml "XML")
639 (tex "TeX") (latex "TeX")
640 (shell-script "bash")
641 (gnuplot "Gnuplot")
642 (ocaml "Caml") (caml "Caml")
643 (sql "SQL") (sqlite "sql"))
644 "Alist mapping languages to their listing language counterpart.
645 The key is a symbol, the major mode symbol without the \"-mode\".
646 The value is the string that should be inserted as the language
647 parameter for the listings package. If the mode name and the
648 listings name are the same, the language does not need an entry
649 in this list - but it does not hurt if it is present."
650 :group 'org-export-e-latex
651 :type '(repeat
652 (list
653 (symbol :tag "Major mode ")
654 (string :tag "Listings language"))))
656 (defcustom org-e-latex-listings-options nil
657 "Association list of options for the latex listings package.
659 These options are supplied as a comma-separated list to the
660 \\lstset command. Each element of the association list should be
661 a list containing two strings: the name of the option, and the
662 value. For example,
664 (setq org-e-latex-listings-options
665 '((\"basicstyle\" \"\\small\")
666 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
668 will typeset the code in a small size font with underlined, bold
669 black keywords.
671 Note that the same options will be applied to blocks of all
672 languages."
673 :group 'org-export-e-latex
674 :type '(repeat
675 (list
676 (string :tag "Listings option name ")
677 (string :tag "Listings option value"))))
679 (defcustom org-e-latex-minted-langs
680 '((emacs-lisp "common-lisp")
681 (cc "c++")
682 (cperl "perl")
683 (shell-script "bash")
684 (caml "ocaml"))
685 "Alist mapping languages to their minted language counterpart.
686 The key is a symbol, the major mode symbol without the \"-mode\".
687 The value is the string that should be inserted as the language
688 parameter for the minted package. If the mode name and the
689 listings name are the same, the language does not need an entry
690 in this list - but it does not hurt if it is present.
692 Note that minted uses all lower case for language identifiers,
693 and that the full list of language identifiers can be obtained
694 with:
696 pygmentize -L lexers"
697 :group 'org-export-e-latex
698 :type '(repeat
699 (list
700 (symbol :tag "Major mode ")
701 (string :tag "Minted language"))))
703 (defcustom org-e-latex-minted-options nil
704 "Association list of options for the latex minted package.
706 These options are supplied within square brackets in
707 \\begin{minted} environments. Each element of the alist should
708 be a list containing two strings: the name of the option, and the
709 value. For example,
711 \(setq org-e-latex-minted-options
712 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
714 will result in src blocks being exported with
716 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
718 as the start of the minted environment. Note that the same
719 options will be applied to blocks of all languages."
720 :group 'org-export-e-latex
721 :type '(repeat
722 (list
723 (string :tag "Minted option name ")
724 (string :tag "Minted option value"))))
726 (defvar org-e-latex-custom-lang-environments nil
727 "Alist mapping languages to language-specific LaTeX environments.
729 It is used during export of src blocks by the listings and minted
730 latex packages. For example,
732 \(setq org-e-latex-custom-lang-environments
733 '\(\(python \"pythoncode\"\)\)\)
735 would have the effect that if org encounters begin_src python
736 during latex export it will output
738 \\begin{pythoncode}
739 <src block body>
740 \\end{pythoncode}")
743 ;;;; Plain text
745 (defcustom org-e-latex-quotes
746 '(("fr"
747 ("\\(\\s-\\|[[(]\\|^\\)\"" . "«~")
748 ("\\(\\S-\\)\"" . "~»")
749 ("\\(\\s-\\|(\\|^\\)'" . "'"))
750 ("en"
751 ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
752 ("\\(\\S-\\)\"" . "''")
753 ("\\(\\s-\\|(\\|^\\)'" . "`")))
754 "Alist for quotes to use when converting english double-quotes.
756 The CAR of each item in this alist is the language code.
757 The CDR of each item in this alist is a list of three CONS:
758 - the first CONS defines the opening quote;
759 - the second CONS defines the closing quote;
760 - the last CONS defines single quotes.
762 For each item in a CONS, the first string is a regexp
763 for allowed characters before/after the quote, the second
764 string defines the replacement string for this quote."
765 :group 'org-export-e-latex
766 :type '(list
767 (cons :tag "Opening quote"
768 (string :tag "Regexp for char before")
769 (string :tag "Replacement quote "))
770 (cons :tag "Closing quote"
771 (string :tag "Regexp for char after ")
772 (string :tag "Replacement quote "))
773 (cons :tag "Single quote"
774 (string :tag "Regexp for char before")
775 (string :tag "Replacement quote "))))
778 ;;;; Compilation
780 (defcustom org-e-latex-pdf-process
781 '("pdflatex -interaction nonstopmode -output-directory %o %f"
782 "pdflatex -interaction nonstopmode -output-directory %o %f"
783 "pdflatex -interaction nonstopmode -output-directory %o %f")
784 "Commands to process a LaTeX file to a PDF file.
785 This is a list of strings, each of them will be given to the
786 shell as a command. %f in the command will be replaced by the
787 full file name, %b by the file base name \(i.e. without
788 extension) and %o by the base directory of the file.
790 The reason why this is a list is that it usually takes several
791 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
792 does not have a clever mechanism to detect which of these
793 commands have to be run to get to a stable result, and it also
794 does not do any error checking.
796 By default, Org uses 3 runs of `pdflatex' to do the processing.
797 If you have texi2dvi on your system and if that does not cause
798 the infamous egrep/locale bug:
800 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
802 then `texi2dvi' is the superior choice. Org does offer it as one
803 of the customize options.
805 Alternatively, this may be a Lisp function that does the
806 processing, so you could use this to apply the machinery of
807 AUCTeX or the Emacs LaTeX mode. This function should accept the
808 file name as its single argument."
809 :group 'org-export-pdf
810 :type '(choice
811 (repeat :tag "Shell command sequence"
812 (string :tag "Shell command"))
813 (const :tag "2 runs of pdflatex"
814 ("pdflatex -interaction nonstopmode -output-directory %o %f"
815 "pdflatex -interaction nonstopmode -output-directory %o %f"))
816 (const :tag "3 runs of pdflatex"
817 ("pdflatex -interaction nonstopmode -output-directory %o %f"
818 "pdflatex -interaction nonstopmode -output-directory %o %f"
819 "pdflatex -interaction nonstopmode -output-directory %o %f"))
820 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
821 ("pdflatex -interaction nonstopmode -output-directory %o %f"
822 "bibtex %b"
823 "pdflatex -interaction nonstopmode -output-directory %o %f"
824 "pdflatex -interaction nonstopmode -output-directory %o %f"))
825 (const :tag "texi2dvi"
826 ("texi2dvi -p -b -c -V %f"))
827 (const :tag "rubber"
828 ("rubber -d --into %o %f"))
829 (function)))
831 (defcustom org-e-latex-logfiles-extensions
832 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
833 "The list of file extensions to consider as LaTeX logfiles."
834 :group 'org-export-e-latex
835 :type '(repeat (string :tag "Extension")))
837 (defcustom org-e-latex-remove-logfiles t
838 "Non-nil means remove the logfiles produced by PDF production.
839 These are the .aux, .log, .out, and .toc files."
840 :group 'org-export-e-latex
841 :type 'boolean)
845 ;;; Internal Functions
847 (defun org-e-latex--caption/label-string (caption label info)
848 "Return caption and label LaTeX string for floats.
850 CAPTION is a cons cell of secondary strings, the car being the
851 standard caption and the cdr its short form. LABEL is a string
852 representing the label. INFO is a plist holding contextual
853 information.
855 If there's no caption nor label, return the empty string.
857 For non-floats, see `org-e-latex--wrap-label'."
858 (let ((label-str (if label (format "\\label{%s}" label) "")))
859 (cond
860 ((and (not caption) (not label)) "")
861 ((not caption) (format "\\label{%s}\n" label))
862 ;; Option caption format with short name.
863 ((cdr caption)
864 (format "\\caption[%s]{%s%s}\n"
865 (org-export-data (cdr caption) info)
866 label-str
867 (org-export-data (car caption) info)))
868 ;; Standard caption format.
869 (t (format "\\caption{%s%s}\n"
870 label-str
871 (org-export-data (car caption) info))))))
873 (defun org-e-latex--guess-babel-language (header info)
874 "Set Babel's language according to LANGUAGE keyword.
876 HEADER is the LaTeX header string. INFO is the plist used as
877 a communication channel.
879 Insertion of guessed language only happens when Babel package has
880 explicitly been loaded. Then it is added to the rest of
881 package's options.
883 Return the new header."
884 (let ((language-code (plist-get info :language)))
885 ;; If no language is set or Babel package is not loaded, return
886 ;; HEADER as-is.
887 (if (or (not (stringp language-code))
888 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
889 header
890 (let ((options (save-match-data
891 (org-split-string (match-string 1 header) ",")))
892 (language (cdr (assoc language-code
893 org-e-latex-babel-language-alist))))
894 ;; If LANGUAGE is already loaded, return header. Otherwise,
895 ;; append LANGUAGE to other options.
896 (if (member language options) header
897 (replace-match (mapconcat 'identity
898 (append options (list language))
899 ",")
900 nil nil header 1))))))
902 (defun org-e-latex--guess-inputenc (header)
903 "Set the coding system in inputenc to what the buffer is.
904 HEADER is the LaTeX header string. Return the new header."
905 (let* ((cs (or (ignore-errors
906 (latexenc-coding-system-to-inputenc
907 buffer-file-coding-system))
908 "utf8")))
909 (if (not cs) header
910 ;; First translate if that is requested.
911 (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
912 ;; Then find the \usepackage statement and replace the option.
913 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
914 cs header t nil 1))))
916 (defun org-e-latex--find-verb-separator (s)
917 "Return a character not used in string S.
918 This is used to choose a separator for constructs like \\verb."
919 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
920 (loop for c across ll
921 when (not (string-match (regexp-quote (char-to-string c)) s))
922 return (char-to-string c))))
924 (defun org-e-latex--make-option-string (options)
925 "Return a comma separated string of keywords and values.
926 OPTIONS is an alist where the key is the options keyword as
927 a string, and the value a list containing the keyword value, or
928 nil."
929 (mapconcat (lambda (pair)
930 (concat (first pair)
931 (when (> (length (second pair)) 0)
932 (concat "=" (second pair)))))
933 options
934 ","))
936 (defun org-e-latex--quotation-marks (text info)
937 "Export quotation marks depending on language conventions.
938 TEXT is a string containing quotation marks to be replaced. INFO
939 is a plist used as a communication channel."
940 (mapc (lambda(l)
941 (let ((start 0))
942 (while (setq start (string-match (car l) text start))
943 (let ((new-quote (concat (match-string 1 text) (cdr l))))
944 (setq text (replace-match new-quote t t text))))))
945 (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
946 ;; Falls back on English.
947 (assoc "en" org-e-latex-quotes))))
948 text)
950 (defun org-e-latex--wrap-label (element output)
951 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
952 This function shouldn't be used for floats. See
953 `org-e-latex--caption/label-string'."
954 (let ((label (org-element-property :name element)))
955 (if (or (not output) (not label) (string= output "") (string= label ""))
956 output
957 (concat (format "\\label{%s}\n" label) output))))
959 (defun org-e-latex--text-markup (text markup)
960 "Format TEXT depending on MARKUP text markup.
961 See `org-e-latex-text-markup-alist' for details."
962 (let ((fmt (cdr (assq markup org-e-latex-text-markup-alist))))
963 (cond
964 ;; No format string: Return raw text.
965 ((not fmt) text)
966 ;; Handle the `verb' special case: Find and appropriate separator
967 ;; and use "\\verb" command.
968 ((eq 'verb fmt)
969 (let ((separator (org-e-latex--find-verb-separator text)))
970 (concat "\\verb" separator text separator)))
971 ;; Handle the `protectedtexttt' special case: Protect some
972 ;; special chars and use "\texttt{%s}" format string.
973 ((eq 'protectedtexttt fmt)
974 (let ((start 0)
975 (trans '(("\\" . "\\textbackslash{}")
976 ("~" . "\\textasciitilde{}")
977 ("^" . "\\textasciicircum{}")))
978 (rtn "")
979 char)
980 (while (string-match "[\\{}$%&_#~^]" text)
981 (setq char (match-string 0 text))
982 (if (> (match-beginning 0) 0)
983 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
984 (setq text (substring text (1+ (match-beginning 0))))
985 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
986 rtn (concat rtn char)))
987 (setq text (concat rtn text)
988 fmt "\\texttt{%s}")
989 (while (string-match "--" text)
990 (setq text (replace-match "-{}-" t t text)))
991 (format fmt text)))
992 ;; Else use format string.
993 (t (format fmt text)))))
995 (defun org-e-latex--delayed-footnotes-definitions (element info)
996 "Return footnotes definitions in ELEMENT as a string.
998 INFO is a plist used as a communication channel.
1000 Footnotes definitions are returned within \"\\footnotetxt{}\"
1001 commands.
1003 This functions is used within constructs that don't support
1004 \"\\footnote{}\" command (i.e. an item's tag). In that case,
1005 \"\\footnotemark\" is used within the construct and this function
1006 outside of it."
1007 (mapconcat
1008 (lambda (ref)
1009 (format
1010 "\\footnotetext[%s]{%s}"
1011 (org-export-get-footnote-number ref info)
1012 (org-trim
1013 (org-export-data
1014 (org-export-get-footnote-definition ref info) info))))
1015 ;; Find every footnote reference in ELEMENT.
1016 (let* (all-refs
1017 search-refs ; For byte-compiler.
1018 (search-refs
1019 (function
1020 (lambda (data)
1021 ;; Return a list of all footnote references never seen
1022 ;; before in DATA.
1023 (org-element-map
1024 data 'footnote-reference
1025 (lambda (ref)
1026 (when (org-export-footnote-first-reference-p ref info)
1027 (push ref all-refs)
1028 (when (eq (org-element-property :type ref) 'standard)
1029 (funcall search-refs
1030 (org-export-get-footnote-definition ref info)))))
1031 info)
1032 (reverse all-refs)))))
1033 (funcall search-refs element))
1034 ""))
1038 ;;; Template
1040 (defun org-e-latex-template (contents info)
1041 "Return complete document string after LaTeX conversion.
1042 CONTENTS is the transcoded contents string. INFO is a plist
1043 holding export options."
1044 (let ((title (org-export-data (plist-get info :title) info)))
1045 (concat
1046 ;; 1. Time-stamp.
1047 (and (plist-get info :time-stamp-file)
1048 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1049 ;; 2. Document class and packages.
1050 (let ((class (plist-get info :latex-class))
1051 (class-options (plist-get info :latex-class-options)))
1052 (org-element-normalize-string
1053 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
1054 (document-class-string
1055 (and (stringp header)
1056 (if class-options
1057 (replace-regexp-in-string
1058 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
1059 class-options header t nil 1)
1060 header))))
1061 (when document-class-string
1062 (org-e-latex--guess-babel-language
1063 (org-e-latex--guess-inputenc
1064 (org-splice-latex-header
1065 document-class-string
1066 org-export-latex-default-packages-alist ; defined in org.el
1067 org-export-latex-packages-alist nil ; defined in org.el
1068 (plist-get info :latex-header-extra)))
1069 info)))))
1070 ;; 3. Define alert if not yet defined.
1071 "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
1072 ;; 4. Possibly limit depth for headline numbering.
1073 (let ((sec-num (plist-get info :section-numbers)))
1074 (when (integerp sec-num)
1075 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1076 ;; 5. Author.
1077 (let ((author (and (plist-get info :with-author)
1078 (let ((auth (plist-get info :author)))
1079 (and auth (org-export-data auth info)))))
1080 (email (and (plist-get info :with-email)
1081 (org-export-data (plist-get info :email) info))))
1082 (cond ((and author email (not (string= "" email)))
1083 (format "\\author{%s\\thanks{%s}}\n" author email))
1084 (author (format "\\author{%s}\n" author))
1085 (t "\\author{}\n")))
1086 ;; 6. Date.
1087 (let ((date (plist-get info :date)))
1088 (and date (format "\\date{%s}\n" date)))
1089 ;; 7. Title
1090 (format "\\title{%s}\n" title)
1091 ;; 8. Hyperref options.
1092 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1093 (or (plist-get info :keywords) "")
1094 (or (plist-get info :description) "")
1095 (if (not (plist-get info :with-creator)) ""
1096 (plist-get info :creator)))
1097 ;; 9. Document start.
1098 "\\begin{document}\n\n"
1099 ;; 10. Title command.
1100 (org-element-normalize-string
1101 (cond ((string= "" title) nil)
1102 ((not (stringp org-e-latex-title-command)) nil)
1103 ((string-match "\\(?:[^%]\\|^\\)%s"
1104 org-e-latex-title-command)
1105 (format org-e-latex-title-command title))
1106 (t org-e-latex-title-command)))
1107 ;; 11. Table of contents.
1108 (let ((depth (plist-get info :with-toc)))
1109 (when depth
1110 (concat (when (wholenump depth)
1111 (format "\\setcounter{tocdepth}{%d}\n" depth))
1112 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
1113 ;; 12. Document's body.
1114 contents
1115 ;; 13. Creator.
1116 (let ((creator-info (plist-get info :with-creator)))
1117 (cond
1118 ((not creator-info) "")
1119 ((eq creator-info 'comment)
1120 (format "%% %s\n" (plist-get info :creator)))
1121 (t (concat (plist-get info :creator) "\n"))))
1122 ;; 14. Document end.
1123 "\\end{document}")))
1127 ;;; Transcode Functions
1129 ;;;; Babel Call
1131 ;; Babel Calls are ignored.
1134 ;;;; Bold
1136 (defun org-e-latex-bold (bold contents info)
1137 "Transcode BOLD from Org to LaTeX.
1138 CONTENTS is the text with bold markup. INFO is a plist holding
1139 contextual information."
1140 (org-e-latex--text-markup contents 'bold))
1143 ;;;; Center Block
1145 (defun org-e-latex-center-block (center-block contents info)
1146 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1147 CONTENTS holds the contents of the center block. INFO is a plist
1148 holding contextual information."
1149 (org-e-latex--wrap-label
1150 center-block
1151 (format "\\begin{center}\n%s\\end{center}" contents)))
1154 ;;;; Clock
1156 (defun org-e-latex-clock (clock contents info)
1157 "Transcode a CLOCK element from Org to LaTeX.
1158 CONTENTS is nil. INFO is a plist holding contextual
1159 information."
1160 (concat
1161 "\\noindent"
1162 (format "\\textbf{%s} " org-clock-string)
1163 (format org-e-latex-inactive-timestamp-format
1164 (concat (org-translate-time (org-element-property :value clock))
1165 (let ((time (org-element-property :time clock)))
1166 (and time (format " (%s)" time)))))
1167 "\\\\"))
1170 ;;;; Code
1172 (defun org-e-latex-code (code contents info)
1173 "Transcode a CODE object from Org to LaTeX.
1174 CONTENTS is nil. INFO is a plist used as a communication
1175 channel."
1176 (org-e-latex--text-markup (org-element-property :value code) 'code))
1179 ;;;; Comment
1181 ;; Comments are ignored.
1184 ;;;; Comment Block
1186 ;; Comment Blocks are ignored.
1189 ;;;; Drawer
1191 (defun org-e-latex-drawer (drawer contents info)
1192 "Transcode a DRAWER element from Org to LaTeX.
1193 CONTENTS holds the contents of the block. INFO is a plist
1194 holding contextual information."
1195 (let* ((name (org-element-property :drawer-name drawer))
1196 (output (if (functionp org-e-latex-format-drawer-function)
1197 (funcall org-e-latex-format-drawer-function
1198 name contents)
1199 ;; If there's no user defined function: simply
1200 ;; display contents of the drawer.
1201 contents)))
1202 (org-e-latex--wrap-label drawer output)))
1205 ;;;; Dynamic Block
1207 (defun org-e-latex-dynamic-block (dynamic-block contents info)
1208 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1209 CONTENTS holds the contents of the block. INFO is a plist
1210 holding contextual information. See `org-export-data'."
1211 (org-e-latex--wrap-label dynamic-block contents))
1214 ;;;; Entity
1216 (defun org-e-latex-entity (entity contents info)
1217 "Transcode an ENTITY object from Org to LaTeX.
1218 CONTENTS are the definition itself. INFO is a plist holding
1219 contextual information."
1220 (let ((ent (org-element-property :latex entity)))
1221 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
1224 ;;;; Example Block
1226 (defun org-e-latex-example-block (example-block contents info)
1227 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1228 CONTENTS is nil. INFO is a plist holding contextual
1229 information."
1230 (org-e-latex--wrap-label
1231 example-block
1232 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1233 (org-export-format-code-default example-block info))))
1236 ;;;; Export Block
1238 (defun org-e-latex-export-block (export-block contents info)
1239 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1240 CONTENTS is nil. INFO is a plist holding contextual information."
1241 (when (string= (org-element-property :type export-block) "LATEX")
1242 (org-remove-indentation (org-element-property :value export-block))))
1245 ;;;; Export Snippet
1247 (defun org-e-latex-export-snippet (export-snippet contents info)
1248 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1249 CONTENTS is nil. INFO is a plist holding contextual information."
1250 (when (eq (org-export-snippet-backend export-snippet) 'e-latex)
1251 (org-element-property :value export-snippet)))
1254 ;;;; Fixed Width
1256 (defun org-e-latex-fixed-width (fixed-width contents info)
1257 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1258 CONTENTS is nil. INFO is a plist holding contextual information."
1259 (org-e-latex--wrap-label
1260 fixed-width
1261 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1262 (org-remove-indentation
1263 (org-element-property :value fixed-width)))))
1266 ;;;; Footnote Definition
1268 ;; Footnote Definitions are ignored.
1271 ;;;; Footnote Reference
1273 (defun org-e-latex-footnote-reference (footnote-reference contents info)
1274 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1275 CONTENTS is nil. INFO is a plist holding contextual information."
1276 (concat
1277 ;; Insert separator between two footnotes in a row.
1278 (let ((prev (org-export-get-previous-element footnote-reference)))
1279 (when (eq (org-element-type prev) 'footnote-reference)
1280 org-e-latex-footnote-separator))
1281 (cond
1282 ;; Use \footnotemark if the footnote has already been defined.
1283 ((not (org-export-footnote-first-reference-p footnote-reference info))
1284 (format "\\footnotemark[%s]{}"
1285 (org-export-get-footnote-number footnote-reference info)))
1286 ;; Use also \footnotemark if reference is within another footnote
1287 ;; reference or footnote definition.
1288 ((loop for parent in (org-export-get-genealogy footnote-reference)
1289 thereis (memq (org-element-type parent)
1290 '(footnote-reference footnote-definition)))
1291 (let ((num (org-export-get-footnote-number footnote-reference info)))
1292 (format "\\footnotemark[%s]{}\\setcounter{footnote}{%s}" num num)))
1293 ;; Use also \footnotemark if reference is within an item's tag.
1294 ;; Note: this won't work if reference has already been defined
1295 ;; since we cannot specify footnote number through square
1296 ;; brackets, forbidden in an optional argument.
1297 ((eq (org-element-type (org-export-get-parent-element footnote-reference))
1298 'item)
1299 (format "\\footnotemark\\setcounter{footnote}{%s}"
1300 (org-export-get-footnote-number footnote-reference info)))
1301 ;; Otherwise, define it with \footnote command.
1303 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1304 (unless (eq (org-element-type def) 'org-data)
1305 (setq def (cons 'org-data (cons nil def))))
1306 (concat
1307 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1308 ;; Retrieve all footnote references within the footnote and
1309 ;; add their definition after it, since LaTeX doesn't support
1310 ;; them inside.
1311 (org-e-latex--delayed-footnotes-definitions def info)))))))
1314 ;;;; Headline
1316 (defun org-e-latex-headline (headline contents info)
1317 "Transcode an HEADLINE element from Org to LaTeX.
1318 CONTENTS holds the contents of the headline. INFO is a plist
1319 holding contextual information."
1320 (let* ((class (plist-get info :latex-class))
1321 (level (org-export-get-relative-level headline info))
1322 (numberedp (org-export-numbered-headline-p headline info))
1323 (class-sectionning (assoc class org-e-latex-classes))
1324 ;; Section formatting will set two placeholders: one for the
1325 ;; title and the other for the contents.
1326 (section-fmt
1327 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
1328 (fboundp (nth 2 class-sectionning)))
1329 (funcall (nth 2 class-sectionning) level numberedp)
1330 (nth (1+ level) class-sectionning))))
1331 (cond
1332 ;; No section available for that LEVEL.
1333 ((not sec) nil)
1334 ;; Section format directly returned by a function.
1335 ((stringp sec) sec)
1336 ;; (numbered-section . unnumbered-section)
1337 ((not (consp (cdr sec)))
1338 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1339 ;; (numbered-open numbered-close)
1340 ((= (length sec) 2)
1341 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1342 ;; (num-in num-out no-num-in no-num-out)
1343 ((= (length sec) 4)
1344 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1345 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1346 (text (org-export-data (org-element-property :title headline) info))
1347 (todo
1348 (and (plist-get info :with-todo-keywords)
1349 (let ((todo (org-element-property :todo-keyword headline)))
1350 (and todo (org-export-data todo info)))))
1351 (todo-type (and todo (org-element-property :todo-type headline)))
1352 (tags (and (plist-get info :with-tags)
1353 (org-export-get-tags headline info)))
1354 (priority (and (plist-get info :with-priority)
1355 (org-element-property :priority headline)))
1356 ;; Create the headline text along with a no-tag version. The
1357 ;; latter is required to remove tags from table of contents.
1358 (full-text (if (functionp org-e-latex-format-headline-function)
1359 ;; User-defined formatting function.
1360 (funcall org-e-latex-format-headline-function
1361 todo todo-type priority text tags)
1362 ;; Default formatting.
1363 (concat
1364 (when todo
1365 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1366 (when priority (format "\\framebox{\\#%c} " priority))
1367 text
1368 (when tags
1369 (format "\\hfill{}\\textsc{:%s:}"
1370 (mapconcat 'identity tags ":"))))))
1371 (full-text-no-tag
1372 (if (functionp org-e-latex-format-headline-function)
1373 ;; User-defined formatting function.
1374 (funcall org-e-latex-format-headline-function
1375 todo todo-type priority text nil)
1376 ;; Default formatting.
1377 (concat
1378 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1379 (when priority (format "\\framebox{\\#%c} " priority))
1380 text)))
1381 ;; Associate some \label to the headline for internal links.
1382 (headline-label
1383 (format "\\label{sec-%s}\n"
1384 (mapconcat 'number-to-string
1385 (org-export-get-headline-number headline info)
1386 "-")))
1387 (pre-blanks
1388 (make-string (org-element-property :pre-blank headline) 10)))
1389 (cond
1390 ;; Case 1: This is a footnote section: ignore it.
1391 ((org-element-property :footnote-section-p headline) nil)
1392 ;; Case 2. This is a deep sub-tree: export it as a list item.
1393 ;; Also export as items headlines for which no section
1394 ;; format has been found.
1395 ((or (not section-fmt) (org-export-low-level-p headline info))
1396 ;; Build the real contents of the sub-tree.
1397 (let ((low-level-body
1398 (concat
1399 ;; If the headline is the first sibling, start a list.
1400 (when (org-export-first-sibling-p headline)
1401 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1402 ;; Itemize headline
1403 "\\item " full-text "\n" headline-label pre-blanks contents)))
1404 ;; If headline is not the last sibling simply return
1405 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1406 ;; blank line.
1407 (if (not (org-export-last-sibling-p headline)) low-level-body
1408 (replace-regexp-in-string
1409 "[ \t\n]*\\'"
1410 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1411 low-level-body))))
1412 ;; Case 3. Standard headline. Export it as a section.
1414 (cond
1415 ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
1416 ;; Regular section. Use specified format string.
1417 (format section-fmt full-text
1418 (concat headline-label pre-blanks contents)))
1419 ((string-match "\\`\\\\\\(.*?\\){" section-fmt)
1420 ;; If tags should be removed from table of contents, insert
1421 ;; title without tags as an alternative heading in sectioning
1422 ;; command.
1423 (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
1424 nil nil section-fmt 1)
1425 ;; Replace square brackets with parenthesis since
1426 ;; square brackets are not supported in optional
1427 ;; arguments.
1428 (replace-regexp-in-string
1429 "\\[" "("
1430 (replace-regexp-in-string
1431 "\\]" ")"
1432 full-text-no-tag))
1433 full-text
1434 (concat headline-label pre-blanks contents)))
1436 ;; Impossible to add an alternative heading. Fallback to
1437 ;; regular sectioning format string.
1438 (format section-fmt full-text
1439 (concat headline-label pre-blanks contents))))))))
1442 ;;;; Horizontal Rule
1444 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1445 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1446 CONTENTS is nil. INFO is a plist holding contextual information."
1447 (let ((attr (mapconcat #'identity
1448 (org-element-property :attr_latex horizontal-rule)
1449 " ")))
1450 (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
1453 ;;;; Inline Babel Call
1455 ;; Inline Babel Calls are ignored.
1458 ;;;; Inline Src Block
1460 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1461 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1462 CONTENTS holds the contents of the item. INFO is a plist holding
1463 contextual information."
1464 (let* ((code (org-element-property :value inline-src-block))
1465 (separator (org-e-latex--find-verb-separator code)))
1466 (cond
1467 ;; Do not use a special package: transcode it verbatim.
1468 ((not org-e-latex-listings)
1469 (concat "\\verb" separator code separator))
1470 ;; Use minted package.
1471 ((eq org-e-latex-listings 'minted)
1472 (let* ((org-lang (org-element-property :language inline-src-block))
1473 (mint-lang (or (cadr (assq (intern org-lang)
1474 org-e-latex-minted-langs))
1475 org-lang))
1476 (options (org-e-latex--make-option-string
1477 org-e-latex-minted-options)))
1478 (concat (format "\\mint%s{%s}"
1479 (if (string= options "") "" (format "[%s]" options))
1480 mint-lang)
1481 separator code separator)))
1482 ;; Use listings package.
1484 ;; Maybe translate language's name.
1485 (let* ((org-lang (org-element-property :language inline-src-block))
1486 (lst-lang (or (cadr (assq (intern org-lang)
1487 org-e-latex-listings-langs))
1488 org-lang))
1489 (options (org-e-latex--make-option-string
1490 (append org-e-latex-listings-options
1491 `(("language" ,lst-lang))))))
1492 (concat (format "\\lstinline[%s]" options)
1493 separator code separator))))))
1496 ;;;; Inlinetask
1498 (defun org-e-latex-inlinetask (inlinetask contents info)
1499 "Transcode an INLINETASK element from Org to LaTeX.
1500 CONTENTS holds the contents of the block. INFO is a plist
1501 holding contextual information."
1502 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1503 (todo (and (plist-get info :with-todo-keywords)
1504 (let ((todo (org-element-property :todo-keyword inlinetask)))
1505 (and todo (org-export-data todo info)))))
1506 (todo-type (org-element-property :todo-type inlinetask))
1507 (tags (and (plist-get info :with-tags)
1508 (org-export-get-tags inlinetask info)))
1509 (priority (and (plist-get info :with-priority)
1510 (org-element-property :priority inlinetask))))
1511 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1512 ;; with appropriate arguments.
1513 (if (functionp org-e-latex-format-inlinetask-function)
1514 (funcall org-e-latex-format-inlinetask-function
1515 todo todo-type priority title tags contents)
1516 ;; Otherwise, use a default template.
1517 (org-e-latex--wrap-label
1518 inlinetask
1519 (let ((full-title
1520 (concat
1521 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1522 (when priority (format "\\framebox{\\#%c} " priority))
1523 title
1524 (when tags (format "\\hfill{}\\textsc{:%s:}"
1525 (mapconcat 'identity tags ":"))))))
1526 (format (concat "\\begin{center}\n"
1527 "\\fbox{\n"
1528 "\\begin{minipage}[c]{.6\\textwidth}\n"
1529 "%s\n\n"
1530 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1531 "%s"
1532 "\\end{minipage}\n"
1533 "}\n"
1534 "\\end{center}")
1535 full-title contents))))))
1538 ;;;; Italic
1540 (defun org-e-latex-italic (italic contents info)
1541 "Transcode ITALIC from Org to LaTeX.
1542 CONTENTS is the text with italic markup. INFO is a plist holding
1543 contextual information."
1544 (org-e-latex--text-markup contents 'italic))
1547 ;;;; Item
1549 (defun org-e-latex-item (item contents info)
1550 "Transcode an ITEM element from Org to LaTeX.
1551 CONTENTS holds the contents of the item. INFO is a plist holding
1552 contextual information."
1553 (let* ((counter
1554 (let ((count (org-element-property :counter item))
1555 (level
1556 (loop for parent in (org-export-get-genealogy item)
1557 count (eq (org-element-type parent) 'plain-list)
1558 until (eq (org-element-type parent) 'headline))))
1559 (and count
1560 (< level 5)
1561 (format "\\setcounter{enum%s}{%s}\n"
1562 (nth (1- level) '("i" "ii" "iii" "iv"))
1563 (1- count)))))
1564 (checkbox (case (org-element-property :checkbox item)
1565 (on "$\\boxtimes$ ")
1566 (off "$\\Box$ ")
1567 (trans "$\\boxminus$ ")))
1568 (tag (let ((tag (org-element-property :tag item)))
1569 ;; Check-boxes must belong to the tag.
1570 (and tag (format "[%s] "
1571 (concat checkbox
1572 (org-export-data tag info)))))))
1573 (concat counter "\\item" (or tag (concat " " checkbox))
1574 (org-trim contents)
1575 ;; If there are footnotes references in tag, be sure to
1576 ;; add their definition at the end of the item. This
1577 ;; workaround is necessary since "\footnote{}" command is
1578 ;; not supported in tags.
1579 (and tag
1580 (org-e-latex--delayed-footnotes-definitions
1581 (org-element-property :tag item) info)))))
1584 ;;;; Keyword
1586 (defun org-e-latex-keyword (keyword contents info)
1587 "Transcode a KEYWORD element from Org to LaTeX.
1588 CONTENTS is nil. INFO is a plist holding contextual information."
1589 (let ((key (org-element-property :key keyword))
1590 (value (org-element-property :value keyword)))
1591 (cond
1592 ((string= key "LATEX") value)
1593 ((string= key "INDEX") (format "\\index{%s}" value))
1594 ;; Invisible targets.
1595 ((string= key "TARGET") nil)
1596 ((string= key "TOC")
1597 (let ((value (downcase value)))
1598 (cond
1599 ((string-match "\\<headlines\\>" value)
1600 (let ((depth (or (and (string-match "[0-9]+" value)
1601 (string-to-number (match-string 0 value)))
1602 (plist-get info :with-toc))))
1603 (concat
1604 (when (wholenump depth)
1605 (format "\\setcounter{tocdepth}{%s}\n" depth))
1606 "\\tableofcontents")))
1607 ((string= "tables" value) "\\listoftables")
1608 ((string= "figures" value) "\\listoffigures")
1609 ((string= "listings" value)
1610 (cond
1611 ((eq org-e-latex-listings 'minted) "\\listoflistings")
1612 (org-e-latex-listings "\\lstlistoflistings")
1613 ;; At the moment, src blocks with a caption are wrapped
1614 ;; into a figure environment.
1615 (t "\\listoffigures")))))))))
1618 ;;;; Latex Environment
1620 (defun org-e-latex-latex-environment (latex-environment contents info)
1621 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1622 CONTENTS is nil. INFO is a plist holding contextual information."
1623 (let ((label (org-element-property :name latex-environment))
1624 (value (org-remove-indentation
1625 (org-element-property :value latex-environment))))
1626 (if (not (org-string-nw-p label)) value
1627 ;; Environment is labelled: label must be within the environment
1628 ;; (otherwise, a reference pointing to that element will count
1629 ;; the section instead).
1630 (with-temp-buffer
1631 (insert value)
1632 (goto-char (point-min))
1633 (forward-line)
1634 (insert (format "\\label{%s}\n" label))
1635 (buffer-string)))))
1638 ;;;; Latex Fragment
1640 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1641 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1642 CONTENTS is nil. INFO is a plist holding contextual information."
1643 (org-element-property :value latex-fragment))
1646 ;;;; Line Break
1648 (defun org-e-latex-line-break (line-break contents info)
1649 "Transcode a LINE-BREAK object from Org to LaTeX.
1650 CONTENTS is nil. INFO is a plist holding contextual information."
1651 "\\\\")
1654 ;;;; Link
1656 (defun org-e-latex-link--inline-image (link info)
1657 "Return LaTeX code for an inline image.
1658 LINK is the link pointing to the inline image. INFO is a plist
1659 used as a communication channel."
1660 (let* ((parent (org-export-get-parent-element link))
1661 (path (let ((raw-path (org-element-property :path link)))
1662 (if (not (file-name-absolute-p raw-path)) raw-path
1663 (expand-file-name raw-path))))
1664 (caption (org-e-latex--caption/label-string
1665 (org-element-property :caption parent)
1666 (org-element-property :name parent)
1667 info))
1668 ;; Retrieve latex attributes from the element around.
1669 (attr (let ((raw-attr
1670 (mapconcat #'identity
1671 (org-element-property :attr_latex parent)
1672 " ")))
1673 (unless (string= raw-attr "") raw-attr)))
1674 (disposition
1675 (cond
1676 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1677 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1678 ((or (and attr (string-match "\\<float\\>" attr))
1679 (not (string= caption "")))
1680 'float)))
1681 (placement
1682 (cond
1683 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1684 (org-match-string-no-properties 1 attr))
1685 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1686 ((eq disposition 'float)
1687 (concat "[" org-e-latex-default-figure-position "]"))
1688 (t ""))))
1689 ;; Now clear ATTR from any special keyword and set a default
1690 ;; value if nothing is left.
1691 (setq attr
1692 (if (not attr) ""
1693 (org-trim
1694 (replace-regexp-in-string
1695 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
1696 (setq attr (cond ((not (string= attr "")) attr)
1697 ((eq disposition 'float) "width=0.7\\textwidth")
1698 ((eq disposition 'wrap) "width=0.48\\textwidth")
1699 (t (or org-e-latex-image-default-option ""))))
1700 ;; Return proper string, depending on DISPOSITION.
1701 (case disposition
1702 (wrap (format "\\begin{wrapfigure}%s
1703 \\centering
1704 \\includegraphics[%s]{%s}
1705 %s\\end{wrapfigure}" placement attr path caption))
1706 (multicolumn (format "\\begin{figure*}%s
1707 \\centering
1708 \\includegraphics[%s]{%s}
1709 %s\\end{figure*}" placement attr path caption))
1710 (float (format "\\begin{figure}%s
1711 \\centering
1712 \\includegraphics[%s]{%s}
1713 %s\\end{figure}" placement attr path caption))
1714 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1716 (defun org-e-latex-link (link desc info)
1717 "Transcode a LINK object from Org to LaTeX.
1719 DESC is the description part of the link, or the empty string.
1720 INFO is a plist holding contextual information. See
1721 `org-export-data'."
1722 (let* ((type (org-element-property :type link))
1723 (raw-path (org-element-property :path link))
1724 ;; Ensure DESC really exists, or set it to nil.
1725 (desc (and (not (string= desc "")) desc))
1726 (imagep (org-export-inline-image-p
1727 link org-e-latex-inline-image-rules))
1728 (path (cond
1729 ((member type '("http" "https" "ftp" "mailto"))
1730 (concat type ":" raw-path))
1731 ((string= type "file")
1732 (when (string-match "\\(.+\\)::.+" raw-path)
1733 (setq raw-path (match-string 1 raw-path)))
1734 (if (file-name-absolute-p raw-path)
1735 (concat "file://" (expand-file-name raw-path))
1736 (concat "file://" raw-path)))
1737 (t raw-path)))
1738 protocol)
1739 (cond
1740 ;; Image file.
1741 (imagep (org-e-latex-link--inline-image link info))
1742 ;; Radio link: Transcode target's contents and use them as link's
1743 ;; description.
1744 ((string= type "radio")
1745 (let ((destination (org-export-resolve-radio-link link info)))
1746 (when destination
1747 (format "\\hyperref[%s]{%s}"
1748 (org-export-solidify-link-text path)
1749 (org-export-data (org-element-contents destination) info)))))
1750 ;; Links pointing to an headline: Find destination and build
1751 ;; appropriate referencing command.
1752 ((member type '("custom-id" "fuzzy" "id"))
1753 (let ((destination (if (string= type "fuzzy")
1754 (org-export-resolve-fuzzy-link link info)
1755 (org-export-resolve-id-link link info))))
1756 (case (org-element-type destination)
1757 ;; Id link points to an external file.
1758 (plain-text
1759 (if desc (format "\\href{file://%s}{%s}" destination desc)
1760 (format "\\url{file://%s}" destination)))
1761 ;; Fuzzy link points nowhere.
1762 ('nil
1763 (format org-e-latex-link-with-unknown-path-format
1764 (or desc
1765 (org-export-data
1766 (org-element-property :raw-link link) info))))
1767 ;; Fuzzy link points to an invisible target.
1768 (keyword nil)
1769 ;; LINK points to an headline. If headlines are numbered
1770 ;; and the link has no description, display headline's
1771 ;; number. Otherwise, display description or headline's
1772 ;; title.
1773 (headline
1774 (let ((label
1775 (format "sec-%s"
1776 (mapconcat
1777 'number-to-string
1778 (org-export-get-headline-number destination info)
1779 "-"))))
1780 (if (and (plist-get info :section-numbers) (not desc))
1781 (format "\\ref{%s}" label)
1782 (format "\\hyperref[%s]{%s}" label
1783 (or desc
1784 (org-export-data
1785 (org-element-property :title destination) info))))))
1786 ;; Fuzzy link points to a target. Do as above.
1787 (otherwise
1788 (let ((path (org-export-solidify-link-text path)))
1789 (if (not desc) (format "\\ref{%s}" path)
1790 (format "\\hyperref[%s]{%s}" path desc)))))))
1791 ;; Coderef: replace link with the reference name or the
1792 ;; equivalent line number.
1793 ((string= type "coderef")
1794 (format (org-export-get-coderef-format path desc)
1795 (org-export-resolve-coderef path info)))
1796 ;; Link type is handled by a special function.
1797 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1798 (funcall protocol (org-link-unescape path) desc 'latex))
1799 ;; External link with a description part.
1800 ((and path desc) (format "\\href{%s}{%s}" path desc))
1801 ;; External link without a description part.
1802 (path (format "\\url{%s}" path))
1803 ;; No path, only description. Try to do something useful.
1804 (t (format org-e-latex-link-with-unknown-path-format desc)))))
1807 ;;;; Macro
1809 (defun org-e-latex-macro (macro contents info)
1810 "Transcode a MACRO element from Org to LaTeX.
1811 CONTENTS is nil. INFO is a plist holding contextual information."
1812 ;; Use available tools.
1813 (org-export-expand-macro macro info))
1816 ;;;; Paragraph
1818 (defun org-e-latex-paragraph (paragraph contents info)
1819 "Transcode a PARAGRAPH element from Org to LaTeX.
1820 CONTENTS is the contents of the paragraph, as a string. INFO is
1821 the plist used as a communication channel."
1822 contents)
1825 ;;;; Plain List
1827 (defun org-e-latex-plain-list (plain-list contents info)
1828 "Transcode a PLAIN-LIST element from Org to LaTeX.
1829 CONTENTS is the contents of the list. INFO is a plist holding
1830 contextual information."
1831 (let* ((type (org-element-property :type plain-list))
1832 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1833 "inparadesc" "asparadesc"))
1834 (paralist-regexp (concat
1835 "\\("
1836 (mapconcat 'identity paralist-types "\\|")
1837 "\\)"))
1838 (attr (mapconcat #'identity
1839 (org-element-property :attr_latex plain-list)
1840 " "))
1841 (latex-type (cond
1842 ((and attr
1843 (string-match
1844 (format "\\<%s\\>" paralist-regexp) attr))
1845 (match-string 1 attr))
1846 ((eq type 'ordered) "enumerate")
1847 ((eq type 'unordered) "itemize")
1848 ((eq type 'descriptive) "description"))))
1849 (org-e-latex--wrap-label
1850 plain-list
1851 (format "\\begin{%s}%s\n%s\\end{%s}"
1852 latex-type
1853 ;; Once special environment, if any, has been removed, the
1854 ;; rest of the attributes will be optional arguments.
1855 ;; They will be put inside square brackets if necessary.
1856 (let ((opt (replace-regexp-in-string
1857 (format " *%s *" paralist-regexp) "" attr)))
1858 (cond ((string= opt "") "")
1859 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1860 (t (format "[%s]" opt))))
1861 contents
1862 latex-type))))
1865 ;;;; Plain Text
1867 (defun org-e-latex-plain-text (text info)
1868 "Transcode a TEXT string from Org to LaTeX.
1869 TEXT is the string to transcode. INFO is a plist holding
1870 contextual information."
1871 ;; Protect %, #, &, $, ~, ^, _, { and }.
1872 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1873 (setq text
1874 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1875 ;; Protect \
1876 (setq text (replace-regexp-in-string
1877 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1878 "$\\backslash$" text nil t 1))
1879 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1880 (let ((case-fold-search nil)
1881 (start 0))
1882 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1883 (setq text (replace-match
1884 (format "\\%s{}" (match-string 1 text)) nil t text)
1885 start (match-end 0))))
1886 ;; Handle quotation marks
1887 (setq text (org-e-latex--quotation-marks text info))
1888 ;; Convert special strings.
1889 (when (plist-get info :with-special-strings)
1890 (while (string-match (regexp-quote "...") text)
1891 (setq text (replace-match "\\ldots{}" nil t text))))
1892 ;; Handle break preservation if required.
1893 (when (plist-get info :preserve-breaks)
1894 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1895 text)))
1896 ;; Return value.
1897 text)
1900 ;;;; Planning
1902 (defun org-e-latex-planning (planning contents info)
1903 "Transcode a PLANNING element from Org to LaTeX.
1904 CONTENTS is nil. INFO is a plist holding contextual
1905 information."
1906 (concat
1907 "\\noindent"
1908 (mapconcat
1909 'identity
1910 (delq nil
1911 (list
1912 (let ((closed (org-element-property :closed planning)))
1913 (when closed
1914 (concat
1915 (format "\\textbf{%s} " org-closed-string)
1916 (format org-e-latex-inactive-timestamp-format
1917 (org-translate-time closed)))))
1918 (let ((deadline (org-element-property :deadline planning)))
1919 (when deadline
1920 (concat
1921 (format "\\textbf{%s} " org-deadline-string)
1922 (format org-e-latex-active-timestamp-format
1923 (org-translate-time deadline)))))
1924 (let ((scheduled (org-element-property :scheduled planning)))
1925 (when scheduled
1926 (concat
1927 (format "\\textbf{%s} " org-scheduled-string)
1928 (format org-e-latex-active-timestamp-format
1929 (org-translate-time scheduled)))))))
1930 " ")
1931 "\\\\"))
1934 ;;;; Property Drawer
1936 (defun org-e-latex-property-drawer (property-drawer contents info)
1937 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1938 CONTENTS is nil. INFO is a plist holding contextual
1939 information."
1940 ;; The property drawer isn't exported but we want separating blank
1941 ;; lines nonetheless.
1945 ;;;; Quote Block
1947 (defun org-e-latex-quote-block (quote-block contents info)
1948 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1949 CONTENTS holds the contents of the block. INFO is a plist
1950 holding contextual information."
1951 (org-e-latex--wrap-label
1952 quote-block
1953 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1956 ;;;; Quote Section
1958 (defun org-e-latex-quote-section (quote-section contents info)
1959 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1960 CONTENTS is nil. INFO is a plist holding contextual information."
1961 (let ((value (org-remove-indentation
1962 (org-element-property :value quote-section))))
1963 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1966 ;;;; Radio Target
1968 (defun org-e-latex-radio-target (radio-target text info)
1969 "Transcode a RADIO-TARGET object from Org to LaTeX.
1970 TEXT is the text of the target. INFO is a plist holding
1971 contextual information."
1972 (format "\\label{%s}%s"
1973 (org-export-solidify-link-text
1974 (org-element-property :value radio-target))
1975 text))
1978 ;;;; Section
1980 (defun org-e-latex-section (section contents info)
1981 "Transcode a SECTION element from Org to LaTeX.
1982 CONTENTS holds the contents of the section. INFO is a plist
1983 holding contextual information."
1984 contents)
1987 ;;;; Special Block
1989 (defun org-e-latex-special-block (special-block contents info)
1990 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1991 CONTENTS holds the contents of the block. INFO is a plist
1992 holding contextual information."
1993 (let ((type (downcase (org-element-property :type special-block))))
1994 (org-e-latex--wrap-label
1995 special-block
1996 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
1999 ;;;; Src Block
2001 (defun org-e-latex-src-block (src-block contents info)
2002 "Transcode a SRC-BLOCK element from Org to LaTeX.
2003 CONTENTS holds the contents of the item. INFO is a plist holding
2004 contextual information."
2005 (let* ((lang (org-element-property :language src-block))
2006 (caption (org-element-property :caption src-block))
2007 (label (org-element-property :name src-block))
2008 (custom-env (and lang
2009 (cadr (assq (intern lang)
2010 org-e-latex-custom-lang-environments))))
2011 (num-start (case (org-element-property :number-lines src-block)
2012 (continued (org-export-get-loc src-block info))
2013 (new 0)))
2014 (retain-labels (org-element-property :retain-labels src-block)))
2015 (cond
2016 ;; Case 1. No source fontification.
2017 ((not org-e-latex-listings)
2018 (let ((caption-str (org-e-latex--caption/label-string caption label info))
2019 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
2020 (format
2021 (or float-env "%s")
2022 (concat caption-str
2023 (format "\\begin{verbatim}\n%s\\end{verbatim}"
2024 (org-export-format-code-default src-block info))))))
2025 ;; Case 2. Custom environment.
2026 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2027 custom-env
2028 (org-export-format-code-default src-block info)
2029 custom-env))
2030 ;; Case 3. Use minted package.
2031 ((eq org-e-latex-listings 'minted)
2032 (let ((float-env (when (or label caption)
2033 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2034 (org-e-latex--caption/label-string
2035 caption label info))))
2036 (body
2037 (format
2038 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2039 ;; Options.
2040 (org-e-latex--make-option-string
2041 (if (not num-start) org-e-latex-minted-options
2042 (append `(("linenos")
2043 ("firstnumber" ,(number-to-string (1+ num-start))))
2044 org-e-latex-minted-options)))
2045 ;; Language.
2046 (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
2047 ;; Source code.
2048 (let* ((code-info (org-export-unravel-code src-block))
2049 (max-width
2050 (apply 'max
2051 (mapcar 'length
2052 (org-split-string (car code-info) "\n")))))
2053 (org-export-format-code
2054 (car code-info)
2055 (lambda (loc num ref)
2056 (concat
2058 (when ref
2059 ;; Ensure references are flushed to the right,
2060 ;; separated with 6 spaces from the widest line
2061 ;; of code.
2062 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2063 (format "(%s)" ref)))))
2064 nil (and retain-labels (cdr code-info)))))))
2065 ;; Return value.
2066 (if float-env (format float-env body) body)))
2067 ;; Case 4. Use listings package.
2069 (let ((lst-lang
2070 (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
2071 (caption-str
2072 (when caption
2073 (let ((main (org-export-data (car caption) info)))
2074 (if (not (cdr caption)) (format "{%s}" main)
2075 (format "{[%s]%s}"
2076 (org-export-data (cdr caption) info)
2077 main))))))
2078 (concat
2079 ;; Options.
2080 (format "\\lstset{%s}\n"
2081 (org-e-latex--make-option-string
2082 (append org-e-latex-listings-options
2083 `(("language" ,lst-lang))
2084 (when label `(("label" ,label)))
2085 (when caption-str `(("caption" ,caption-str)))
2086 (cond ((not num-start) '(("numbers" "none")))
2087 ((zerop num-start) '(("numbers" "left")))
2088 (t `(("numbers" "left")
2089 ("firstnumber"
2090 ,(number-to-string (1+ num-start)))))))))
2091 ;; Source code.
2092 (format
2093 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2094 (let* ((code-info (org-export-unravel-code src-block))
2095 (max-width
2096 (apply 'max
2097 (mapcar 'length
2098 (org-split-string (car code-info) "\n")))))
2099 (org-export-format-code
2100 (car code-info)
2101 (lambda (loc num ref)
2102 (concat
2104 (when ref
2105 ;; Ensure references are flushed to the right,
2106 ;; separated with 6 spaces from the widest line of
2107 ;; code
2108 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2109 (format "(%s)" ref)))))
2110 nil (and retain-labels (cdr code-info)))))))))))
2113 ;;;; Statistics Cookie
2115 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
2116 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2117 CONTENTS is nil. INFO is a plist holding contextual information."
2118 (org-element-property :value statistics-cookie))
2121 ;;;; Strike-Through
2123 (defun org-e-latex-strike-through (strike-through contents info)
2124 "Transcode STRIKE-THROUGH from Org to LaTeX.
2125 CONTENTS is the text with strike-through markup. INFO is a plist
2126 holding contextual information."
2127 (org-e-latex--text-markup contents 'strike-through))
2130 ;;;; Subscript
2132 (defun org-e-latex-subscript (subscript contents info)
2133 "Transcode a SUBSCRIPT object from Org to LaTeX.
2134 CONTENTS is the contents of the object. INFO is a plist holding
2135 contextual information."
2136 (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
2139 ;;;; Superscript
2141 (defun org-e-latex-superscript (superscript contents info)
2142 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2143 CONTENTS is the contents of the object. INFO is a plist holding
2144 contextual information."
2145 (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
2148 ;;;; Table
2150 ;; `org-e-latex-table' is the entry point for table transcoding. It
2151 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
2152 ;; delegates the job to either `org-e-latex-table--table.el-table' or
2153 ;; `org-e-latex-table--org-table' functions, depending of the type of
2154 ;; the table.
2156 ;; `org-e-latex-table--align-string' is a subroutine used to build
2157 ;; alignment string for Org tables.
2159 (defun org-e-latex-table (table contents info)
2160 "Transcode a TABLE element from Org to LaTeX.
2161 CONTENTS is the contents of the table. INFO is a plist holding
2162 contextual information."
2163 (cond
2164 ;; Case 1: verbatim table.
2165 ((or org-e-latex-tables-verbatim
2166 (let ((attr (mapconcat 'identity
2167 (org-element-property :attr_latex table)
2168 " ")))
2169 (and attr (string-match "\\<verbatim\\>" attr))))
2170 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2171 ;; Re-create table, without affiliated keywords.
2172 (org-trim
2173 (org-element-interpret-data
2174 `(table nil ,@(org-element-contents table))))))
2175 ;; Case 2: table.el table. Convert it using appropriate tools.
2176 ((eq (org-element-property :type table) 'table.el)
2177 (org-e-latex-table--table.el-table table contents info))
2178 ;; Case 3: Standard table.
2179 (t (org-e-latex-table--org-table table contents info))))
2181 (defun org-e-latex-table--align-string (table info)
2182 "Return an appropriate LaTeX alignment string.
2183 TABLE is the considered table. INFO is a plist used as
2184 a communication channel."
2185 (let ((attr (mapconcat 'identity
2186 (org-element-property :attr_latex table)
2187 " ")))
2188 (if (string-match "\\<align=\\(\\S-+\\)" attr) (match-string 1 attr)
2189 (let (alignment)
2190 ;; Extract column groups and alignment from first (non-rule)
2191 ;; row.
2192 (org-element-map
2193 (org-element-map
2194 table 'table-row
2195 (lambda (row)
2196 (and (eq (org-element-property :type row) 'standard) row))
2197 info 'first-match)
2198 'table-cell
2199 (lambda (cell)
2200 (let ((borders (org-export-table-cell-borders cell info)))
2201 ;; Check left border for the first cell only.
2202 (when (and (memq 'left borders) (not alignment))
2203 (push "|" alignment))
2204 (push (case (org-export-table-cell-alignment cell info)
2205 (left "l")
2206 (right "r")
2207 (center "c"))
2208 alignment)
2209 (when (memq 'right borders) (push "|" alignment))))
2210 info)
2211 (apply 'concat (reverse alignment))))))
2213 (defun org-e-latex-table--org-table (table contents info)
2214 "Return appropriate LaTeX code for an Org table.
2216 TABLE is the table type element to transcode. CONTENTS is its
2217 contents, as a string. INFO is a plist used as a communication
2218 channel.
2220 This function assumes TABLE has `org' as its `:type' attribute."
2221 (let* ((label (org-element-property :name table))
2222 (caption (org-e-latex--caption/label-string
2223 (org-element-property :caption table) label info))
2224 (attr (mapconcat 'identity
2225 (org-element-property :attr_latex table)
2226 " "))
2227 ;; Determine alignment string.
2228 (alignment (org-e-latex-table--align-string table info))
2229 ;; Determine environment for the table: longtable, tabular...
2230 (table-env (cond
2231 ((not attr) org-e-latex-default-table-environment)
2232 ((string-match "\\<longtable\\>" attr) "longtable")
2233 ((string-match "\\<tabular.?\\>" attr)
2234 (org-match-string-no-properties 0 attr))
2235 (t org-e-latex-default-table-environment)))
2236 ;; If table is a float, determine environment: table, table*
2237 ;; or sidewaystable.
2238 (float-env (cond
2239 ((string= "longtable" table-env) nil)
2240 ((and attr (string-match "\\<sidewaystable\\>" attr))
2241 "sidewaystables")
2242 ((and attr
2243 (or (string-match (regexp-quote "table*") attr)
2244 (string-match "\\<multicolumn\\>" attr)))
2245 "table*")
2246 ((or (not (string= caption "")) label) "table")))
2247 ;; Extract others display options.
2248 (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
2249 (org-match-string-no-properties 1 attr)))
2250 (placement
2251 (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
2252 (org-match-string-no-properties 1 attr)
2253 (format "[%s]" org-e-latex-default-figure-position))))
2254 ;; Prepare the final format string for the table.
2255 (cond
2256 ;; Longtable.
2257 ((string= "longtable" table-env)
2258 (format
2259 "\\begin{longtable}{%s}\n%s%s%s\\end{longtable}"
2260 alignment
2261 (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
2262 (concat (org-trim caption) "\\\\\n"))
2263 contents
2264 (if (or org-e-latex-table-caption-above (string= "" caption)) ""
2265 (concat (org-trim caption) "\\\\\n"))))
2266 ;; Others.
2267 (t (concat (when float-env
2268 (concat
2269 (format "\\begin{%s}%s\n" float-env placement)
2270 (if org-e-latex-table-caption-above caption "")))
2271 (when org-e-latex-tables-centered "\\begin{center}\n")
2272 (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2273 table-env
2274 (if width (format "{%s}" width) "")
2275 alignment
2276 contents
2277 table-env)
2278 (when org-e-latex-tables-centered "\n\\end{center}")
2279 (when float-env
2280 (concat (if org-e-latex-table-caption-above "" caption)
2281 (format "\n\\end{%s}" float-env))))))))
2283 (defun org-e-latex-table--table.el-table (table contents info)
2284 "Return appropriate LaTeX code for a table.el table.
2286 TABLE is the table type element to transcode. CONTENTS is its
2287 contents, as a string. INFO is a plist used as a communication
2288 channel.
2290 This function assumes TABLE has `table.el' as its `:type'
2291 attribute."
2292 (require 'table)
2293 ;; Ensure "*org-export-table*" buffer is empty.
2294 (with-current-buffer (get-buffer-create "*org-export-table*")
2295 (erase-buffer))
2296 (let ((output (with-temp-buffer
2297 (insert (org-element-property :value table))
2298 (goto-char 1)
2299 (re-search-forward "^[ \t]*|[^|]" nil t)
2300 (table-generate-source 'latex "*org-export-table*")
2301 (with-current-buffer "*org-export-table*"
2302 (org-trim (buffer-string))))))
2303 (kill-buffer (get-buffer "*org-export-table*"))
2304 ;; Remove left out comments.
2305 (while (string-match "^%.*\n" output)
2306 (setq output (replace-match "" t t output)))
2307 ;; When the "rmlines" attribute is provided, remove all hlines but
2308 ;; the the one separating heading from the table body.
2309 (let ((attr (mapconcat 'identity
2310 (org-element-property :attr_latex table)
2311 " ")))
2312 (when (and attr (string-match "\\<rmlines\\>" attr))
2313 (let ((n 0) (pos 0))
2314 (while (and (< (length output) pos)
2315 (setq pos (string-match "^\\\\hline\n?" output pos)))
2316 (incf n)
2317 (unless (= n 2)
2318 (setq output (replace-match "" nil nil output)))))))
2319 (if (not org-e-latex-tables-centered) output
2320 (format "\\begin{center}\n%s\n\\end{center}" output))))
2323 ;;;; Table Cell
2325 (defun org-e-latex-table-cell (table-cell contents info)
2326 "Transcode a TABLE-CELL element from Org to LaTeX.
2327 CONTENTS is the cell contents. INFO is a plist used as
2328 a communication channel."
2329 (concat (if (and contents
2330 org-e-latex-table-scientific-notation
2331 (string-match orgtbl-exp-regexp contents))
2332 ;; Use appropriate format string for scientific
2333 ;; notation.
2334 (format org-e-latex-table-scientific-notation
2335 (match-string 1 contents)
2336 (match-string 2 contents))
2337 contents)
2338 (when (org-export-get-next-element table-cell) " & ")))
2341 ;;;; Table Row
2343 (defun org-e-latex-table-row (table-row contents info)
2344 "Transcode a TABLE-ROW element from Org to LaTeX.
2345 CONTENTS is the contents of the row. INFO is a plist used as
2346 a communication channel."
2347 ;; Rules are ignored since table separators are deduced from
2348 ;; borders of the current row.
2349 (when (eq (org-element-property :type table-row) 'standard)
2350 (let* ((attr (mapconcat 'identity
2351 (org-element-property
2352 :attr_latex (org-export-get-parent table-row))
2353 " "))
2354 (longtablep (and attr (string-match "\\<longtable\\>" attr)))
2355 (booktabsp
2356 (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
2357 org-e-latex-tables-booktabs))
2358 ;; TABLE-ROW's borders are extracted from its first cell.
2359 (borders
2360 (org-export-table-cell-borders
2361 (car (org-element-contents table-row)) info)))
2362 (concat
2363 ;; When BOOKTABS are activated enforce top-rule even when no
2364 ;; hline was specifically marked.
2365 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2366 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2367 contents "\\\\\n"
2368 (cond
2369 ;; Special case for long tables. Define header and footers.
2370 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2371 (format "%s
2372 \\endhead
2373 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2374 \\endfoot
2375 \\endlastfoot"
2376 (if booktabsp "\\midrule" "\\hline")
2377 (if booktabsp "\\midrule" "\\hline")
2378 ;; Number of columns.
2379 (cdr (org-export-table-dimensions
2380 (org-export-get-parent-table table-row) info))))
2381 ;; When BOOKTABS are activated enforce bottom rule even when
2382 ;; no hline was specifically marked.
2383 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2384 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2385 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2388 ;;;; Target
2390 (defun org-e-latex-target (target contents info)
2391 "Transcode a TARGET object from Org to LaTeX.
2392 CONTENTS is nil. INFO is a plist holding contextual
2393 information."
2394 (format "\\label{%s}"
2395 (org-export-solidify-link-text (org-element-property :value target))))
2398 ;;;; Timestamp
2400 (defun org-e-latex-timestamp (timestamp contents info)
2401 "Transcode a TIMESTAMP object from Org to LaTeX.
2402 CONTENTS is nil. INFO is a plist holding contextual
2403 information."
2404 (let ((value (org-translate-time (org-element-property :value timestamp)))
2405 (type (org-element-property :type timestamp)))
2406 (cond ((memq type '(active active-range))
2407 (format org-e-latex-active-timestamp-format value))
2408 ((memq type '(inactive inactive-range))
2409 (format org-e-latex-inactive-timestamp-format value))
2410 (t (format org-e-latex-diary-timestamp-format value)))))
2413 ;;;; Underline
2415 (defun org-e-latex-underline (underline contents info)
2416 "Transcode UNDERLINE from Org to LaTeX.
2417 CONTENTS is the text with underline markup. INFO is a plist
2418 holding contextual information."
2419 (org-e-latex--text-markup contents 'underline))
2422 ;;;; Verbatim
2424 (defun org-e-latex-verbatim (verbatim contents info)
2425 "Transcode a VERBATIM object from Org to LaTeX.
2426 CONTENTS is nil. INFO is a plist used as a communication
2427 channel."
2428 (org-e-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2431 ;;;; Verse Block
2433 (defun org-e-latex-verse-block (verse-block contents info)
2434 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2435 CONTENTS is verse block contents. INFO is a plist holding
2436 contextual information."
2437 (org-e-latex--wrap-label
2438 verse-block
2439 ;; In a verse environment, add a line break to each newline
2440 ;; character and change each white space at beginning of a line
2441 ;; into a space of 1 em. Also change each blank line with
2442 ;; a vertical space of 1 em.
2443 (progn
2444 (setq contents (replace-regexp-in-string
2445 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2446 (replace-regexp-in-string
2447 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2448 (while (string-match "^[ \t]+" contents)
2449 (let ((new-str (format "\\hspace*{%dem}"
2450 (length (match-string 0 contents)))))
2451 (setq contents (replace-match new-str nil t contents))))
2452 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2456 ;;; Interactive functions
2458 (defun org-e-latex-export-to-latex
2459 (&optional subtreep visible-only body-only ext-plist pub-dir)
2460 "Export current buffer to a LaTeX file.
2462 If narrowing is active in the current buffer, only export its
2463 narrowed part.
2465 If a region is active, export that region.
2467 When optional argument SUBTREEP is non-nil, export the sub-tree
2468 at point, extracting information from the headline properties
2469 first.
2471 When optional argument VISIBLE-ONLY is non-nil, don't export
2472 contents of hidden elements.
2474 When optional argument BODY-ONLY is non-nil, only write code
2475 between \"\\begin{document}\" and \"\\end{document}\".
2477 EXT-PLIST, when provided, is a property list with external
2478 parameters overriding Org default settings, but still inferior to
2479 file-local settings.
2481 When optional argument PUB-DIR is set, use it as the publishing
2482 directory.
2484 Return output file's name."
2485 (interactive)
2486 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
2487 (org-export-to-file
2488 'e-latex outfile subtreep visible-only body-only ext-plist)))
2490 (defun org-e-latex-export-to-pdf
2491 (&optional subtreep visible-only body-only ext-plist pub-dir)
2492 "Export current buffer to LaTeX then process through to PDF.
2494 If narrowing is active in the current buffer, only export its
2495 narrowed part.
2497 If a region is active, export that region.
2499 When optional argument SUBTREEP is non-nil, export the sub-tree
2500 at point, extracting information from the headline properties
2501 first.
2503 When optional argument VISIBLE-ONLY is non-nil, don't export
2504 contents of hidden elements.
2506 When optional argument BODY-ONLY is non-nil, only write code
2507 between \"\\begin{document}\" and \"\\end{document}\".
2509 EXT-PLIST, when provided, is a property list with external
2510 parameters overriding Org default settings, but still inferior to
2511 file-local settings.
2513 When optional argument PUB-DIR is set, use it as the publishing
2514 directory.
2516 Return PDF file's name."
2517 (interactive)
2518 (org-e-latex-compile
2519 (org-e-latex-export-to-latex
2520 subtreep visible-only body-only ext-plist pub-dir)))
2522 (defun org-e-latex-compile (texfile)
2523 "Compile a TeX file.
2525 TEXFILE is the name of the file being compiled. Processing is
2526 done through the command specified in `org-e-latex-pdf-process'.
2528 Return PDF file name or an error if it couldn't be produced."
2529 (let* ((wconfig (current-window-configuration))
2530 (texfile (file-truename texfile))
2531 (base (file-name-sans-extension texfile))
2532 errors)
2533 (message (format "Processing LaTeX file %s ..." texfile))
2534 (unwind-protect
2535 (progn
2536 (cond
2537 ;; A function is provided: Apply it.
2538 ((functionp org-e-latex-pdf-process)
2539 (funcall org-e-latex-pdf-process (shell-quote-argument texfile)))
2540 ;; A list is provided: Replace %b, %f and %o with appropriate
2541 ;; values in each command before applying it. Output is
2542 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2543 ((consp org-e-latex-pdf-process)
2544 (let* ((out-dir (or (file-name-directory texfile) "./"))
2545 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2546 (mapc
2547 (lambda (command)
2548 (shell-command
2549 (replace-regexp-in-string
2550 "%b" (shell-quote-argument base)
2551 (replace-regexp-in-string
2552 "%f" (shell-quote-argument texfile)
2553 (replace-regexp-in-string
2554 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2555 outbuf))
2556 org-e-latex-pdf-process)
2557 ;; Collect standard errors from output buffer.
2558 (setq errors (org-e-latex-collect-errors outbuf))))
2559 (t (error "No valid command to process to PDF")))
2560 (let ((pdffile (concat base ".pdf")))
2561 ;; Check for process failure. Provide collected errors if
2562 ;; possible.
2563 (if (not (file-exists-p pdffile))
2564 (error (concat (format "PDF file %s wasn't produced" pdffile)
2565 (when errors (concat ": " errors))))
2566 ;; Else remove log files, when specified, and signal end of
2567 ;; process to user, along with any error encountered.
2568 (when org-e-latex-remove-logfiles
2569 (dolist (ext org-e-latex-logfiles-extensions)
2570 (let ((file (concat base "." ext)))
2571 (when (file-exists-p file) (delete-file file)))))
2572 (message (concat "Process completed"
2573 (if (not errors) "."
2574 (concat " with errors: " errors)))))
2575 ;; Return output file name.
2576 pdffile))
2577 (set-window-configuration wconfig))))
2579 (defun org-e-latex-collect-errors (buffer)
2580 "Collect some kind of errors from \"pdflatex\" command output.
2582 BUFFER is the buffer containing output.
2584 Return collected error types as a string, or nil if there was
2585 none."
2586 (with-current-buffer buffer
2587 (save-excursion
2588 (goto-char (point-max))
2589 ;; Find final "pdflatex" run.
2590 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
2591 (let ((case-fold-search t)
2592 (errors ""))
2593 (when (save-excursion
2594 (re-search-forward "Reference.*?undefined" nil t))
2595 (setq errors (concat errors " [undefined reference]")))
2596 (when (save-excursion
2597 (re-search-forward "Citation.*?undefined" nil t))
2598 (setq errors (concat errors " [undefined citation]")))
2599 (when (save-excursion
2600 (re-search-forward "Undefined control sequence" nil t))
2601 (setq errors (concat errors " [undefined control sequence]")))
2602 (when (save-excursion
2603 (re-search-forward "^! LaTeX.*?Error" nil t))
2604 (setq errors (concat errors " [LaTeX error]")))
2605 (when (save-excursion
2606 (re-search-forward "^! Package.*?Error" nil t))
2607 (setq errors (concat errors " [package error]")))
2608 (and (org-string-nw-p errors) (org-trim errors)))))))
2611 (provide 'org-e-latex)
2612 ;;; org-e-latex.el ends here