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