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