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