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