org-e-latex: Add xelatex as a possible processor
[org-mode.git] / contrib / lisp / org-e-latex.el
blobb2551155d8d4d4b0a8e10788cac143cd0ef991af
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 "2 runs of xelatex"
781 ("xelatex -interaction nonstopmode -output-directory %o %f"
782 "xelatex -interaction nonstopmode -output-directory %o %f"))
783 (const :tag "3 runs of xelatex"
784 ("xelatex -interaction nonstopmode -output-directory %o %f"
785 "xelatex -interaction nonstopmode -output-directory %o %f"
786 "xelatex -interaction nonstopmode -output-directory %o %f"))
787 (const :tag "xelatex,bibtex,xelatex,xelatex"
788 ("xelatex -interaction nonstopmode -output-directory %o %f"
789 "bibtex %b"
790 "xelatex -interaction nonstopmode -output-directory %o %f"
791 "xelatex -interaction nonstopmode -output-directory %o %f"))
792 (const :tag "texi2dvi"
793 ("texi2dvi -p -b -c -V %f"))
794 (const :tag "rubber"
795 ("rubber -d --into %o %f"))
796 (function)))
798 (defcustom org-e-latex-logfiles-extensions
799 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
800 "The list of file extensions to consider as LaTeX logfiles."
801 :group 'org-export-e-latex
802 :type '(repeat (string :tag "Extension")))
804 (defcustom org-e-latex-remove-logfiles t
805 "Non-nil means remove the logfiles produced by PDF production.
806 These are the .aux, .log, .out, and .toc files."
807 :group 'org-export-e-latex
808 :type 'boolean)
812 ;;; Internal Functions
814 (defun org-e-latex--caption/label-string (caption label info)
815 "Return caption and label LaTeX string for floats.
817 CAPTION is a cons cell of secondary strings, the car being the
818 standard caption and the cdr its short form. LABEL is a string
819 representing the label. INFO is a plist holding contextual
820 information.
822 If there's no caption nor label, return the empty string.
824 For non-floats, see `org-e-latex--wrap-label'."
825 (let ((label-str (if label (format "\\label{%s}" label) "")))
826 (cond
827 ((and (not caption) (not label)) "")
828 ((not caption) (format "\\label{%s}\n" label))
829 ;; Option caption format with short name.
830 ((cdr caption)
831 (format "\\caption[%s]{%s%s}\n"
832 (org-export-data (cdr caption) info)
833 label-str
834 (org-export-data (car caption) info)))
835 ;; Standard caption format.
836 (t (format "\\caption{%s%s}\n"
837 label-str
838 (org-export-data (car caption) info))))))
840 (defun org-e-latex--guess-babel-language (header info)
841 "Set Babel's language according to LANGUAGE keyword.
843 HEADER is the LaTeX header string. INFO is the plist used as
844 a communication channel.
846 Insertion of guessed language only happens when Babel package has
847 explicitly been loaded. Then it is added to the rest of
848 package's options.
850 Return the new header."
851 (let ((language-code (plist-get info :language)))
852 ;; If no language is set or Babel package is not loaded, return
853 ;; HEADER as-is.
854 (if (or (not (stringp language-code))
855 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
856 header
857 (let ((options (save-match-data
858 (org-split-string (match-string 1 header) ",")))
859 (language (cdr (assoc language-code
860 org-e-latex-babel-language-alist))))
861 ;; If LANGUAGE is already loaded, return header. Otherwise,
862 ;; append LANGUAGE to other options.
863 (if (member language options) header
864 (replace-match (mapconcat 'identity
865 (append options (list language))
866 ",")
867 nil nil header 1))))))
869 (defun org-e-latex--guess-inputenc (header)
870 "Set the coding system in inputenc to what the buffer is.
871 HEADER is the LaTeX header string. Return the new header."
872 (let* ((cs (or (ignore-errors
873 (latexenc-coding-system-to-inputenc
874 buffer-file-coding-system))
875 "utf8")))
876 (if (not cs) header
877 ;; First translate if that is requested.
878 (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
879 ;; Then find the \usepackage statement and replace the option.
880 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
881 cs header t nil 1))))
883 (defun org-e-latex--find-verb-separator (s)
884 "Return a character not used in string S.
885 This is used to choose a separator for constructs like \\verb."
886 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
887 (loop for c across ll
888 when (not (string-match (regexp-quote (char-to-string c)) s))
889 return (char-to-string c))))
891 (defun org-e-latex--make-option-string (options)
892 "Return a comma separated string of keywords and values.
893 OPTIONS is an alist where the key is the options keyword as
894 a string, and the value a list containing the keyword value, or
895 nil."
896 (mapconcat (lambda (pair)
897 (concat (first pair)
898 (when (> (length (second pair)) 0)
899 (concat "=" (second pair)))))
900 options
901 ","))
903 (defun org-e-latex--quotation-marks (text info)
904 "Export quotation marks depending on language conventions.
905 TEXT is a string containing quotation marks to be replaced. INFO
906 is a plist used as a communication channel."
907 (mapc (lambda(l)
908 (let ((start 0))
909 (while (setq start (string-match (car l) text start))
910 (let ((new-quote (concat (match-string 1 text) (cdr l))))
911 (setq text (replace-match new-quote t t text))))))
912 (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
913 ;; Falls back on English.
914 (assoc "en" org-e-latex-quotes))))
915 text)
917 (defun org-e-latex--wrap-label (element output)
918 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
919 This function shouldn't be used for floats. See
920 `org-e-latex--caption/label-string'."
921 (let ((label (org-element-property :name element)))
922 (if (or (not output) (not label) (string= output "") (string= label ""))
923 output
924 (concat (format "\\label{%s}\n" label) output))))
926 (defun org-e-latex--text-markup (text markup)
927 "Format TEXT depending on MARKUP text markup.
928 See `org-e-latex-text-markup-alist' for details."
929 (let ((fmt (cdr (assq markup org-e-latex-text-markup-alist))))
930 (cond
931 ;; No format string: Return raw text.
932 ((not fmt) text)
933 ;; Handle the `verb' special case: Find and appropriate separator
934 ;; and use "\\verb" command.
935 ((eq 'verb fmt)
936 (let ((separator (org-e-latex--find-verb-separator text)))
937 (concat "\\verb" separator text separator)))
938 ;; Handle the `protectedtexttt' special case: Protect some
939 ;; special chars and use "\texttt{%s}" format string.
940 ((eq 'protectedtexttt fmt)
941 (let ((start 0)
942 (trans '(("\\" . "\\textbackslash{}")
943 ("~" . "\\textasciitilde{}")
944 ("^" . "\\textasciicircum{}")))
945 (rtn "")
946 char)
947 (while (string-match "[\\{}$%&_#~^]" text)
948 (setq char (match-string 0 text))
949 (if (> (match-beginning 0) 0)
950 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
951 (setq text (substring text (1+ (match-beginning 0))))
952 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
953 rtn (concat rtn char)))
954 (setq text (concat rtn text)
955 fmt "\\texttt{%s}")
956 (while (string-match "--" text)
957 (setq text (replace-match "-{}-" t t text)))
958 (format fmt text)))
959 ;; Else use format string.
960 (t (format fmt text)))))
962 (defun org-e-latex--delayed-footnotes-definitions (element info)
963 "Return footnotes definitions in ELEMENT as a string.
965 INFO is a plist used as a communication channel.
967 Footnotes definitions are returned within \"\\footnotetxt{}\"
968 commands.
970 This function is used within constructs that don't support
971 \"\\footnote{}\" command (i.e. an item's tag). In that case,
972 \"\\footnotemark\" is used within the construct and the function
973 just outside of it."
974 (mapconcat
975 (lambda (ref)
976 (format
977 "\\footnotetext[%s]{%s}"
978 (org-export-get-footnote-number ref info)
979 (org-trim
980 (org-export-data
981 (org-export-get-footnote-definition ref info) info))))
982 ;; Find every footnote reference in ELEMENT.
983 (let* (all-refs
984 search-refs ; For byte-compiler.
985 (search-refs
986 (function
987 (lambda (data)
988 ;; Return a list of all footnote references never seen
989 ;; before in DATA.
990 (org-element-map
991 data 'footnote-reference
992 (lambda (ref)
993 (when (org-export-footnote-first-reference-p ref info)
994 (push ref all-refs)
995 (when (eq (org-element-property :type ref) 'standard)
996 (funcall search-refs
997 (org-export-get-footnote-definition ref info)))))
998 info)
999 (reverse all-refs)))))
1000 (funcall search-refs element))
1001 ""))
1005 ;;; Template
1007 (defun org-e-latex-template (contents info)
1008 "Return complete document string after LaTeX conversion.
1009 CONTENTS is the transcoded contents string. INFO is a plist
1010 holding export options."
1011 (let ((title (org-export-data (plist-get info :title) info)))
1012 (concat
1013 ;; Time-stamp.
1014 (and (plist-get info :time-stamp-file)
1015 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1016 ;; Document class and packages.
1017 (let ((class (plist-get info :latex-class))
1018 (class-options (plist-get info :latex-class-options)))
1019 (org-element-normalize-string
1020 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
1021 (document-class-string
1022 (and (stringp header)
1023 (if (not class-options) header
1024 (replace-regexp-in-string
1025 "^[ \t]*\\\\documentclass\\(\\(\\[.*\\]\\)?\\)"
1026 class-options header t nil 1)))))
1027 (when document-class-string
1028 (org-e-latex--guess-babel-language
1029 (org-e-latex--guess-inputenc
1030 (org-splice-latex-header
1031 document-class-string
1032 org-export-latex-default-packages-alist ; defined in org.el
1033 org-export-latex-packages-alist nil ; defined in org.el
1034 (plist-get info :latex-header-extra)))
1035 info)))))
1036 ;; Possibly limit depth for headline numbering.
1037 (let ((sec-num (plist-get info :section-numbers)))
1038 (when (integerp sec-num)
1039 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
1040 ;; Author.
1041 (let ((author (and (plist-get info :with-author)
1042 (let ((auth (plist-get info :author)))
1043 (and auth (org-export-data auth info)))))
1044 (email (and (plist-get info :with-email)
1045 (org-export-data (plist-get info :email) info))))
1046 (cond ((and author email (not (string= "" email)))
1047 (format "\\author{%s\\thanks{%s}}\n" author email))
1048 (author (format "\\author{%s}\n" author))
1049 (t "\\author{}\n")))
1050 ;; Date.
1051 (let ((date (org-export-data (plist-get info :date) info)))
1052 (and date (format "\\date{%s}\n" date)))
1053 ;; Title
1054 (format "\\title{%s}\n" title)
1055 ;; Hyperref options.
1056 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1057 (or (plist-get info :keywords) "")
1058 (or (plist-get info :description) "")
1059 (if (not (plist-get info :with-creator)) ""
1060 (plist-get info :creator)))
1061 ;; Document start.
1062 "\\begin{document}\n\n"
1063 ;; Title command.
1064 (org-element-normalize-string
1065 (cond ((string= "" title) nil)
1066 ((not (stringp org-e-latex-title-command)) nil)
1067 ((string-match "\\(?:[^%]\\|^\\)%s"
1068 org-e-latex-title-command)
1069 (format org-e-latex-title-command title))
1070 (t org-e-latex-title-command)))
1071 ;; Table of contents.
1072 (let ((depth (plist-get info :with-toc)))
1073 (when depth
1074 (concat (when (wholenump depth)
1075 (format "\\setcounter{tocdepth}{%d}\n" depth))
1076 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
1077 ;; Document's body.
1078 contents
1079 ;; Creator.
1080 (let ((creator-info (plist-get info :with-creator)))
1081 (cond
1082 ((not creator-info) "")
1083 ((eq creator-info 'comment)
1084 (format "%% %s\n" (plist-get info :creator)))
1085 (t (concat (plist-get info :creator) "\n"))))
1086 ;; Document end.
1087 "\\end{document}")))
1091 ;;; Transcode Functions
1093 ;;;; Babel Call
1095 ;; Babel Calls are ignored.
1098 ;;;; Bold
1100 (defun org-e-latex-bold (bold contents info)
1101 "Transcode BOLD from Org to LaTeX.
1102 CONTENTS is the text with bold markup. INFO is a plist holding
1103 contextual information."
1104 (org-e-latex--text-markup contents 'bold))
1107 ;;;; Center Block
1109 (defun org-e-latex-center-block (center-block contents info)
1110 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1111 CONTENTS holds the contents of the center block. INFO is a plist
1112 holding contextual information."
1113 (org-e-latex--wrap-label
1114 center-block
1115 (format "\\begin{center}\n%s\\end{center}" contents)))
1118 ;;;; Clock
1120 (defun org-e-latex-clock (clock contents info)
1121 "Transcode a CLOCK element from Org to LaTeX.
1122 CONTENTS is nil. INFO is a plist holding contextual
1123 information."
1124 (concat
1125 "\\noindent"
1126 (format "\\textbf{%s} " org-clock-string)
1127 (format org-e-latex-inactive-timestamp-format
1128 (concat (org-translate-time (org-element-property :value clock))
1129 (let ((time (org-element-property :time clock)))
1130 (and time (format " (%s)" time)))))
1131 "\\\\"))
1134 ;;;; Code
1136 (defun org-e-latex-code (code contents info)
1137 "Transcode a CODE object from Org to LaTeX.
1138 CONTENTS is nil. INFO is a plist used as a communication
1139 channel."
1140 (org-e-latex--text-markup (org-element-property :value code) 'code))
1143 ;;;; Comment
1145 ;; Comments are ignored.
1148 ;;;; Comment Block
1150 ;; Comment Blocks are ignored.
1153 ;;;; Drawer
1155 (defun org-e-latex-drawer (drawer contents info)
1156 "Transcode a DRAWER element from Org to LaTeX.
1157 CONTENTS holds the contents of the block. INFO is a plist
1158 holding contextual information."
1159 (let* ((name (org-element-property :drawer-name drawer))
1160 (output (if (functionp org-e-latex-format-drawer-function)
1161 (funcall org-e-latex-format-drawer-function
1162 name contents)
1163 ;; If there's no user defined function: simply
1164 ;; display contents of the drawer.
1165 contents)))
1166 (org-e-latex--wrap-label drawer output)))
1169 ;;;; Dynamic Block
1171 (defun org-e-latex-dynamic-block (dynamic-block contents info)
1172 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1173 CONTENTS holds the contents of the block. INFO is a plist
1174 holding contextual information. See `org-export-data'."
1175 (org-e-latex--wrap-label dynamic-block contents))
1178 ;;;; Entity
1180 (defun org-e-latex-entity (entity contents info)
1181 "Transcode an ENTITY object from Org to LaTeX.
1182 CONTENTS are the definition itself. INFO is a plist holding
1183 contextual information."
1184 (let ((ent (org-element-property :latex entity)))
1185 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
1188 ;;;; Example Block
1190 (defun org-e-latex-example-block (example-block contents info)
1191 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1192 CONTENTS is nil. INFO is a plist holding contextual
1193 information."
1194 (org-e-latex--wrap-label
1195 example-block
1196 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1197 (org-export-format-code-default example-block info))))
1200 ;;;; Export Block
1202 (defun org-e-latex-export-block (export-block contents info)
1203 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1204 CONTENTS is nil. INFO is a plist holding contextual information."
1205 (when (string= (org-element-property :type export-block) "LATEX")
1206 (org-remove-indentation (org-element-property :value export-block))))
1209 ;;;; Export Snippet
1211 (defun org-e-latex-export-snippet (export-snippet contents info)
1212 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1213 CONTENTS is nil. INFO is a plist holding contextual information."
1214 (when (eq (org-export-snippet-backend export-snippet) 'e-latex)
1215 (org-element-property :value export-snippet)))
1218 ;;;; Fixed Width
1220 (defun org-e-latex-fixed-width (fixed-width contents info)
1221 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1222 CONTENTS is nil. INFO is a plist holding contextual information."
1223 (org-e-latex--wrap-label
1224 fixed-width
1225 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1226 (org-remove-indentation
1227 (org-element-property :value fixed-width)))))
1230 ;;;; Footnote Definition
1232 ;; Footnote Definitions are ignored.
1235 ;;;; Footnote Reference
1237 ;; Footnote reference export is handled by
1238 ;; `org-e-latex-footnote-reference'.
1240 ;; Internally, `org-e-latex--get-footnote-counter' is used to restore
1241 ;; the value of the LaTeX "footnote" counter after a jump due to
1242 ;; a reference to an already defined footnote. It is only needed in
1243 ;; item tags since the optional argument to \footnotemark is not
1244 ;; allowed there.
1246 (defun org-e-latex--get-footnote-counter (footnote-reference info)
1247 "Return \"footnote\" counter before FOOTNOTE-REFERENCE is encountered.
1248 INFO is a plist used as a communication channel."
1249 ;; Find original counter value by counting number of footnote
1250 ;; references appearing for the first time before the current
1251 ;; footnote reference.
1252 (let* ((label (org-element-property :label footnote-reference))
1253 seen-refs
1254 search-ref ; For byte-compiler.
1255 (search-ref
1256 (function
1257 (lambda (data)
1258 ;; Search footnote references through DATA, filling
1259 ;; SEEN-REFS along the way.
1260 (org-element-map
1261 data 'footnote-reference
1262 (lambda (fn)
1263 (let ((fn-lbl (org-element-property :label fn)))
1264 (cond
1265 ;; Anonymous footnote match: return number.
1266 ((eq fn footnote-reference) (length seen-refs))
1267 ;; Anonymous footnote: it's always a new one.
1268 ;; Also, be sure to return nil from the `cond' so
1269 ;; `first-match' doesn't get us out of the loop.
1270 ((not fn-lbl) (push 'inline seen-refs) nil)
1271 ;; Label not seen so far: add it so SEEN-REFS.
1273 ;; Also search for subsequent references in
1274 ;; footnote definition so numbering follows reading
1275 ;; logic. Note that we don't have to care about
1276 ;; inline definitions, since `org-element-map'
1277 ;; already traverse them at the right time.
1278 ((not (member fn-lbl seen-refs))
1279 (push fn-lbl seen-refs)
1280 (funcall search-ref
1281 (org-export-get-footnote-definition fn info))))))
1282 ;; Don't enter footnote definitions since it will happen
1283 ;; when their first reference is found.
1284 info 'first-match 'footnote-definition)))))
1285 (funcall search-ref (plist-get info :parse-tree))))
1287 (defun org-e-latex-footnote-reference (footnote-reference contents info)
1288 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1289 CONTENTS is nil. INFO is a plist holding contextual information."
1290 (concat
1291 ;; Insert separator between two footnotes in a row.
1292 (let ((prev (org-export-get-previous-element footnote-reference info)))
1293 (when (eq (org-element-type prev) 'footnote-reference)
1294 org-e-latex-footnote-separator))
1295 (cond
1296 ;; Use \footnotemark if reference is within an item's tag.
1297 ((eq (org-element-type (org-export-get-parent-element footnote-reference))
1298 'item)
1299 (if (org-export-footnote-first-reference-p footnote-reference info)
1300 "\\footnotemark"
1301 ;; Since we can't specify footnote number as an optional
1302 ;; argument within an item tag, some extra work has to be done
1303 ;; when the footnote has already been referenced. In that
1304 ;; case, set footnote counter to the desired number, use the
1305 ;; footnotemark, then set counter back to its original value.
1306 (format
1307 "\\setcounter{footnote}{%s}\\footnotemark\\setcounter{footnote}{%s}"
1308 (1- (org-export-get-footnote-number footnote-reference info))
1309 (org-e-latex--get-footnote-counter footnote-reference info))))
1310 ;; Use \footnotemark if the footnote has already been defined.
1311 ((not (org-export-footnote-first-reference-p footnote-reference info))
1312 (format "\\footnotemark[%s]{}"
1313 (org-export-get-footnote-number footnote-reference info)))
1314 ;; Use \footnotemark if reference is within another footnote
1315 ;; reference or footnote definition.
1316 ((loop for parent in (org-export-get-genealogy footnote-reference)
1317 thereis (memq (org-element-type parent)
1318 '(footnote-reference footnote-definition)))
1319 "\\footnotemark")
1320 ;; Otherwise, define it with \footnote command.
1322 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1323 (unless (eq (org-element-type def) 'org-data)
1324 (setq def (cons 'org-data (cons nil def))))
1325 (concat
1326 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1327 ;; Retrieve all footnote references within the footnote and
1328 ;; add their definition after it, since LaTeX doesn't support
1329 ;; them inside.
1330 (org-e-latex--delayed-footnotes-definitions def info)))))))
1333 ;;;; Headline
1335 (defun org-e-latex-headline (headline contents info)
1336 "Transcode an HEADLINE element from Org to LaTeX.
1337 CONTENTS holds the contents of the headline. INFO is a plist
1338 holding contextual information."
1339 (let* ((class (plist-get info :latex-class))
1340 (level (org-export-get-relative-level headline info))
1341 (numberedp (org-export-numbered-headline-p headline info))
1342 (class-sectionning (assoc class org-e-latex-classes))
1343 ;; Section formatting will set two placeholders: one for the
1344 ;; title and the other for the contents.
1345 (section-fmt
1346 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
1347 (fboundp (nth 2 class-sectionning)))
1348 (funcall (nth 2 class-sectionning) level numberedp)
1349 (nth (1+ level) class-sectionning))))
1350 (cond
1351 ;; No section available for that LEVEL.
1352 ((not sec) nil)
1353 ;; Section format directly returned by a function.
1354 ((stringp sec) sec)
1355 ;; (numbered-section . unnumbered-section)
1356 ((not (consp (cdr sec)))
1357 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1358 ;; (numbered-open numbered-close)
1359 ((= (length sec) 2)
1360 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1361 ;; (num-in num-out no-num-in no-num-out)
1362 ((= (length sec) 4)
1363 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1364 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1365 (text (org-export-data (org-element-property :title headline) info))
1366 (todo
1367 (and (plist-get info :with-todo-keywords)
1368 (let ((todo (org-element-property :todo-keyword headline)))
1369 (and todo (org-export-data todo info)))))
1370 (todo-type (and todo (org-element-property :todo-type headline)))
1371 (tags (and (plist-get info :with-tags)
1372 (org-export-get-tags headline info)))
1373 (priority (and (plist-get info :with-priority)
1374 (org-element-property :priority headline)))
1375 ;; Create the headline text along with a no-tag version. The
1376 ;; latter is required to remove tags from table of contents.
1377 (full-text (if (functionp org-e-latex-format-headline-function)
1378 ;; User-defined formatting function.
1379 (funcall org-e-latex-format-headline-function
1380 todo todo-type priority text tags)
1381 ;; Default formatting.
1382 (concat
1383 (when todo
1384 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1385 (when priority (format "\\framebox{\\#%c} " priority))
1386 text
1387 (when tags
1388 (format "\\hfill{}\\textsc{:%s:}"
1389 (mapconcat 'identity tags ":"))))))
1390 (full-text-no-tag
1391 (if (functionp org-e-latex-format-headline-function)
1392 ;; User-defined formatting function.
1393 (funcall org-e-latex-format-headline-function
1394 todo todo-type priority text nil)
1395 ;; Default formatting.
1396 (concat
1397 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1398 (when priority (format "\\framebox{\\#%c} " priority))
1399 text)))
1400 ;; Associate some \label to the headline for internal links.
1401 (headline-label
1402 (format "\\label{sec-%s}\n"
1403 (mapconcat 'number-to-string
1404 (org-export-get-headline-number headline info)
1405 "-")))
1406 (pre-blanks
1407 (make-string (org-element-property :pre-blank headline) 10)))
1408 (cond
1409 ;; Case 1: This is a footnote section: ignore it.
1410 ((org-element-property :footnote-section-p headline) nil)
1411 ;; Case 2. This is a deep sub-tree: export it as a list item.
1412 ;; Also export as items headlines for which no section
1413 ;; format has been found.
1414 ((or (not section-fmt) (org-export-low-level-p headline info))
1415 ;; Build the real contents of the sub-tree.
1416 (let ((low-level-body
1417 (concat
1418 ;; If the headline is the first sibling, start a list.
1419 (when (org-export-first-sibling-p headline info)
1420 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1421 ;; Itemize headline
1422 "\\item " full-text "\n" headline-label pre-blanks contents)))
1423 ;; If headline is not the last sibling simply return
1424 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1425 ;; blank line.
1426 (if (not (org-export-last-sibling-p headline info)) low-level-body
1427 (replace-regexp-in-string
1428 "[ \t\n]*\\'"
1429 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1430 low-level-body))))
1431 ;; Case 3. Standard headline. Export it as a section.
1433 (cond
1434 ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
1435 ;; Regular section. Use specified format string.
1436 (format section-fmt full-text
1437 (concat headline-label pre-blanks contents)))
1438 ((string-match "\\`\\\\\\(.*?\\){" section-fmt)
1439 ;; If tags should be removed from table of contents, insert
1440 ;; title without tags as an alternative heading in sectioning
1441 ;; command.
1442 (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
1443 nil nil section-fmt 1)
1444 ;; Replace square brackets with parenthesis since
1445 ;; square brackets are not supported in optional
1446 ;; arguments.
1447 (replace-regexp-in-string
1448 "\\[" "("
1449 (replace-regexp-in-string
1450 "\\]" ")"
1451 full-text-no-tag))
1452 full-text
1453 (concat headline-label pre-blanks contents)))
1455 ;; Impossible to add an alternative heading. Fallback to
1456 ;; regular sectioning format string.
1457 (format section-fmt full-text
1458 (concat headline-label pre-blanks contents))))))))
1461 ;;;; Horizontal Rule
1463 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1464 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1465 CONTENTS is nil. INFO is a plist holding contextual information."
1466 (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
1467 (prev (org-export-get-previous-element horizontal-rule info)))
1468 (concat
1469 ;; Make sure the rule doesn't start at the end of the current
1470 ;; line by separating it with a blank line from previous element.
1471 (when (and prev
1472 (let ((prev-blank (org-element-property :post-blank prev)))
1473 (or (not prev-blank) (zerop prev-blank))))
1474 "\n")
1475 (org-e-latex--wrap-label
1476 horizontal-rule
1477 (format "\\rule{%s}{%s}"
1478 (or (plist-get attr :width) "\\linewidth")
1479 (or (plist-get attr :thickness) "0.5pt"))))))
1482 ;;;; Inline Babel Call
1484 ;; Inline Babel Calls are ignored.
1487 ;;;; Inline Src Block
1489 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1490 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1491 CONTENTS holds the contents of the item. INFO is a plist holding
1492 contextual information."
1493 (let* ((code (org-element-property :value inline-src-block))
1494 (separator (org-e-latex--find-verb-separator code)))
1495 (cond
1496 ;; Do not use a special package: transcode it verbatim.
1497 ((not org-e-latex-listings)
1498 (concat "\\verb" separator code separator))
1499 ;; Use minted package.
1500 ((eq org-e-latex-listings 'minted)
1501 (let* ((org-lang (org-element-property :language inline-src-block))
1502 (mint-lang (or (cadr (assq (intern org-lang)
1503 org-e-latex-minted-langs))
1504 org-lang))
1505 (options (org-e-latex--make-option-string
1506 org-e-latex-minted-options)))
1507 (concat (format "\\mint%s{%s}"
1508 (if (string= options "") "" (format "[%s]" options))
1509 mint-lang)
1510 separator code separator)))
1511 ;; Use listings package.
1513 ;; Maybe translate language's name.
1514 (let* ((org-lang (org-element-property :language inline-src-block))
1515 (lst-lang (or (cadr (assq (intern org-lang)
1516 org-e-latex-listings-langs))
1517 org-lang))
1518 (options (org-e-latex--make-option-string
1519 (append org-e-latex-listings-options
1520 `(("language" ,lst-lang))))))
1521 (concat (format "\\lstinline[%s]" options)
1522 separator code separator))))))
1525 ;;;; Inlinetask
1527 (defun org-e-latex-inlinetask (inlinetask contents info)
1528 "Transcode an INLINETASK element from Org to LaTeX.
1529 CONTENTS holds the contents of the block. INFO is a plist
1530 holding contextual information."
1531 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1532 (todo (and (plist-get info :with-todo-keywords)
1533 (let ((todo (org-element-property :todo-keyword inlinetask)))
1534 (and todo (org-export-data todo info)))))
1535 (todo-type (org-element-property :todo-type inlinetask))
1536 (tags (and (plist-get info :with-tags)
1537 (org-export-get-tags inlinetask info)))
1538 (priority (and (plist-get info :with-priority)
1539 (org-element-property :priority inlinetask))))
1540 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1541 ;; with appropriate arguments.
1542 (if (functionp org-e-latex-format-inlinetask-function)
1543 (funcall org-e-latex-format-inlinetask-function
1544 todo todo-type priority title tags contents)
1545 ;; Otherwise, use a default template.
1546 (org-e-latex--wrap-label
1547 inlinetask
1548 (let ((full-title
1549 (concat
1550 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1551 (when priority (format "\\framebox{\\#%c} " priority))
1552 title
1553 (when tags (format "\\hfill{}\\textsc{:%s:}"
1554 (mapconcat 'identity tags ":"))))))
1555 (format (concat "\\begin{center}\n"
1556 "\\fbox{\n"
1557 "\\begin{minipage}[c]{.6\\textwidth}\n"
1558 "%s\n\n"
1559 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1560 "%s"
1561 "\\end{minipage}\n"
1562 "}\n"
1563 "\\end{center}")
1564 full-title contents))))))
1567 ;;;; Italic
1569 (defun org-e-latex-italic (italic contents info)
1570 "Transcode ITALIC from Org to LaTeX.
1571 CONTENTS is the text with italic markup. INFO is a plist holding
1572 contextual information."
1573 (org-e-latex--text-markup contents 'italic))
1576 ;;;; Item
1578 (defun org-e-latex-item (item contents info)
1579 "Transcode an ITEM element from Org to LaTeX.
1580 CONTENTS holds the contents of the item. INFO is a plist holding
1581 contextual information."
1582 (let* ((counter
1583 (let ((count (org-element-property :counter item))
1584 (level
1585 (loop for parent in (org-export-get-genealogy item)
1586 count (eq (org-element-type parent) 'plain-list)
1587 until (eq (org-element-type parent) 'headline))))
1588 (and count
1589 (< level 5)
1590 (format "\\setcounter{enum%s}{%s}\n"
1591 (nth (1- level) '("i" "ii" "iii" "iv"))
1592 (1- count)))))
1593 (checkbox (case (org-element-property :checkbox item)
1594 (on "$\\boxtimes$ ")
1595 (off "$\\Box$ ")
1596 (trans "$\\boxminus$ ")))
1597 (tag (let ((tag (org-element-property :tag item)))
1598 ;; Check-boxes must belong to the tag.
1599 (and tag (format "[%s] "
1600 (concat checkbox
1601 (org-export-data tag info)))))))
1602 (concat counter "\\item" (or tag (concat " " checkbox))
1603 (and contents (org-trim contents))
1604 ;; If there are footnotes references in tag, be sure to
1605 ;; add their definition at the end of the item. This
1606 ;; workaround is necessary since "\footnote{}" command is
1607 ;; not supported in tags.
1608 (and tag
1609 (org-e-latex--delayed-footnotes-definitions
1610 (org-element-property :tag item) info)))))
1613 ;;;; Keyword
1615 (defun org-e-latex-keyword (keyword contents info)
1616 "Transcode a KEYWORD element from Org to LaTeX.
1617 CONTENTS is nil. INFO is a plist holding contextual information."
1618 (let ((key (org-element-property :key keyword))
1619 (value (org-element-property :value keyword)))
1620 (cond
1621 ((string= key "LATEX") value)
1622 ((string= key "INDEX") (format "\\index{%s}" value))
1623 ;; Invisible targets.
1624 ((string= key "TARGET") nil)
1625 ((string= key "TOC")
1626 (let ((value (downcase value)))
1627 (cond
1628 ((string-match "\\<headlines\\>" value)
1629 (let ((depth (or (and (string-match "[0-9]+" value)
1630 (string-to-number (match-string 0 value)))
1631 (plist-get info :with-toc))))
1632 (concat
1633 (when (wholenump depth)
1634 (format "\\setcounter{tocdepth}{%s}\n" depth))
1635 "\\tableofcontents")))
1636 ((string= "tables" value) "\\listoftables")
1637 ((string= "figures" value) "\\listoffigures")
1638 ((string= "listings" value)
1639 (cond
1640 ((eq org-e-latex-listings 'minted) "\\listoflistings")
1641 (org-e-latex-listings "\\lstlistoflistings")
1642 ;; At the moment, src blocks with a caption are wrapped
1643 ;; into a figure environment.
1644 (t "\\listoffigures")))))))))
1647 ;;;; Latex Environment
1649 (defun org-e-latex-latex-environment (latex-environment contents info)
1650 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1651 CONTENTS is nil. INFO is a plist holding contextual information."
1652 (let ((label (org-element-property :name latex-environment))
1653 (value (org-remove-indentation
1654 (org-element-property :value latex-environment))))
1655 (if (not (org-string-nw-p label)) value
1656 ;; Environment is labelled: label must be within the environment
1657 ;; (otherwise, a reference pointing to that element will count
1658 ;; the section instead).
1659 (with-temp-buffer
1660 (insert value)
1661 (goto-char (point-min))
1662 (forward-line)
1663 (insert (format "\\label{%s}\n" label))
1664 (buffer-string)))))
1667 ;;;; Latex Fragment
1669 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1670 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1671 CONTENTS is nil. INFO is a plist holding contextual information."
1672 (org-element-property :value latex-fragment))
1675 ;;;; Line Break
1677 (defun org-e-latex-line-break (line-break contents info)
1678 "Transcode a LINE-BREAK object from Org to LaTeX.
1679 CONTENTS is nil. INFO is a plist holding contextual information."
1680 "\\\\")
1683 ;;;; Link
1685 (defun org-e-latex-link--inline-image (link info)
1686 "Return LaTeX code for an inline image.
1687 LINK is the link pointing to the inline image. INFO is a plist
1688 used as a communication channel."
1689 (let* ((parent (org-export-get-parent-element link))
1690 (path (let ((raw-path (org-element-property :path link)))
1691 (if (not (file-name-absolute-p raw-path)) raw-path
1692 (expand-file-name raw-path))))
1693 (caption (org-e-latex--caption/label-string
1694 (org-element-property :caption parent)
1695 (org-element-property :name parent)
1696 info))
1697 ;; Retrieve latex attributes from the element around.
1698 (attr (let ((raw-attr
1699 (mapconcat #'identity
1700 (org-element-property :attr_latex parent)
1701 " ")))
1702 (unless (string= raw-attr "") raw-attr)))
1703 (disposition
1704 (cond
1705 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1706 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1707 ((or (and attr (string-match "\\<float\\>" attr))
1708 (not (string= caption "")))
1709 'float)))
1710 (placement
1711 (cond
1712 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1713 (org-match-string-no-properties 1 attr))
1714 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1715 ((eq disposition 'float)
1716 (concat "[" org-e-latex-default-figure-position "]"))
1717 (t ""))))
1718 ;; Now clear ATTR from any special keyword and set a default
1719 ;; value if nothing is left.
1720 (setq attr
1721 (if (not attr) ""
1722 (org-trim
1723 (replace-regexp-in-string
1724 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
1725 (setq attr (cond ((not (string= attr "")) attr)
1726 ((eq disposition 'float) "width=0.7\\textwidth")
1727 ((eq disposition 'wrap) "width=0.48\\textwidth")
1728 (t (or org-e-latex-image-default-option ""))))
1729 ;; Return proper string, depending on DISPOSITION.
1730 (case disposition
1731 (wrap (format "\\begin{wrapfigure}%s
1732 \\centering
1733 \\includegraphics[%s]{%s}
1734 %s\\end{wrapfigure}" placement attr path caption))
1735 (multicolumn (format "\\begin{figure*}%s
1736 \\centering
1737 \\includegraphics[%s]{%s}
1738 %s\\end{figure*}" placement attr path caption))
1739 (float (format "\\begin{figure}%s
1740 \\centering
1741 \\includegraphics[%s]{%s}
1742 %s\\end{figure}" placement attr path caption))
1743 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1745 (defun org-e-latex-link (link desc info)
1746 "Transcode a LINK object from Org to LaTeX.
1748 DESC is the description part of the link, or the empty string.
1749 INFO is a plist holding contextual information. See
1750 `org-export-data'."
1751 (let* ((type (org-element-property :type link))
1752 (raw-path (org-element-property :path link))
1753 ;; Ensure DESC really exists, or set it to nil.
1754 (desc (and (not (string= desc "")) desc))
1755 (imagep (org-export-inline-image-p
1756 link org-e-latex-inline-image-rules))
1757 (path (cond
1758 ((member type '("http" "https" "ftp" "mailto"))
1759 (concat type ":" raw-path))
1760 ((string= type "file")
1761 (if (file-name-absolute-p raw-path)
1762 (concat "file://" (expand-file-name raw-path))
1763 (concat "file://" raw-path)))
1764 (t raw-path)))
1765 protocol)
1766 (cond
1767 ;; Image file.
1768 (imagep (org-e-latex-link--inline-image link info))
1769 ;; Radio link: Transcode target's contents and use them as link's
1770 ;; description.
1771 ((string= type "radio")
1772 (let ((destination (org-export-resolve-radio-link link info)))
1773 (when destination
1774 (format "\\hyperref[%s]{%s}"
1775 (org-export-solidify-link-text path)
1776 (org-export-data (org-element-contents destination) info)))))
1777 ;; Links pointing to an headline: Find destination and build
1778 ;; appropriate referencing command.
1779 ((member type '("custom-id" "fuzzy" "id"))
1780 (let ((destination (if (string= type "fuzzy")
1781 (org-export-resolve-fuzzy-link link info)
1782 (org-export-resolve-id-link link info))))
1783 (case (org-element-type destination)
1784 ;; Id link points to an external file.
1785 (plain-text
1786 (if desc (format "\\href{file://%s}{%s}" destination desc)
1787 (format "\\url{file://%s}" destination)))
1788 ;; Fuzzy link points nowhere.
1789 ('nil
1790 (format org-e-latex-link-with-unknown-path-format
1791 (or desc
1792 (org-export-data
1793 (org-element-property :raw-link link) info))))
1794 ;; Fuzzy link points to an invisible target.
1795 (keyword nil)
1796 ;; LINK points to an headline. If headlines are numbered
1797 ;; and the link has no description, display headline's
1798 ;; number. Otherwise, display description or headline's
1799 ;; title.
1800 (headline
1801 (let ((label
1802 (format "sec-%s"
1803 (mapconcat
1804 'number-to-string
1805 (org-export-get-headline-number destination info)
1806 "-"))))
1807 (if (and (plist-get info :section-numbers) (not desc))
1808 (format "\\ref{%s}" label)
1809 (format "\\hyperref[%s]{%s}" label
1810 (or desc
1811 (org-export-data
1812 (org-element-property :title destination) info))))))
1813 ;; Fuzzy link points to a target. Do as above.
1814 (otherwise
1815 (let ((path (org-export-solidify-link-text path)))
1816 (if (not desc) (format "\\ref{%s}" path)
1817 (format "\\hyperref[%s]{%s}" path desc)))))))
1818 ;; Coderef: replace link with the reference name or the
1819 ;; equivalent line number.
1820 ((string= type "coderef")
1821 (format (org-export-get-coderef-format path desc)
1822 (org-export-resolve-coderef path info)))
1823 ;; Link type is handled by a special function.
1824 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1825 (funcall protocol (org-link-unescape path) desc 'latex))
1826 ;; External link with a description part.
1827 ((and path desc) (format "\\href{%s}{%s}" path desc))
1828 ;; External link without a description part.
1829 (path (format "\\url{%s}" path))
1830 ;; No path, only description. Try to do something useful.
1831 (t (format org-e-latex-link-with-unknown-path-format desc)))))
1834 ;;;; Paragraph
1836 (defun org-e-latex-paragraph (paragraph contents info)
1837 "Transcode a PARAGRAPH element from Org to LaTeX.
1838 CONTENTS is the contents of the paragraph, as a string. INFO is
1839 the plist used as a communication channel."
1840 contents)
1843 ;;;; Plain List
1845 (defun org-e-latex-plain-list (plain-list contents info)
1846 "Transcode a PLAIN-LIST element from Org to LaTeX.
1847 CONTENTS is the contents of the list. INFO is a plist holding
1848 contextual information."
1849 (let* ((type (org-element-property :type plain-list))
1850 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1851 "inparadesc" "asparadesc"))
1852 (paralist-regexp (concat
1853 "\\("
1854 (mapconcat 'identity paralist-types "\\|")
1855 "\\)"))
1856 (attr (mapconcat #'identity
1857 (org-element-property :attr_latex plain-list)
1858 " "))
1859 (latex-type (cond
1860 ((and attr
1861 (string-match
1862 (format "\\<%s\\>" paralist-regexp) attr))
1863 (match-string 1 attr))
1864 ((eq type 'ordered) "enumerate")
1865 ((eq type 'unordered) "itemize")
1866 ((eq type 'descriptive) "description"))))
1867 (org-e-latex--wrap-label
1868 plain-list
1869 (format "\\begin{%s}%s\n%s\\end{%s}"
1870 latex-type
1871 ;; Once special environment, if any, has been removed, the
1872 ;; rest of the attributes will be optional arguments.
1873 ;; They will be put inside square brackets if necessary.
1874 (let ((opt (replace-regexp-in-string
1875 (format " *%s *" paralist-regexp) "" attr)))
1876 (cond ((string= opt "") "")
1877 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1878 (t (format "[%s]" opt))))
1879 contents
1880 latex-type))))
1883 ;;;; Plain Text
1885 (defun org-e-latex-plain-text (text info)
1886 "Transcode a TEXT string from Org to LaTeX.
1887 TEXT is the string to transcode. INFO is a plist holding
1888 contextual information."
1889 (let ((specialp (plist-get info :with-special-strings)))
1890 ;; Protect %, #, &, $, ~, ^, _, { and }.
1891 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1892 (setq text
1893 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1894 ;; Protect \. If special strings are used, be careful not to
1895 ;; protect "\" in "\-" constructs.
1896 (let ((symbols (if specialp "-%$#&{}~^_\\" "%$#&{}~^_\\")))
1897 (setq text
1898 (replace-regexp-in-string
1899 (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
1900 "$\\backslash$" text nil t 1)))
1901 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1902 (let ((case-fold-search nil)
1903 (start 0))
1904 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1905 (setq text (replace-match
1906 (format "\\%s{}" (match-string 1 text)) nil t text)
1907 start (match-end 0))))
1908 ;; Handle quotation marks.
1909 (setq text (org-e-latex--quotation-marks text info))
1910 ;; Convert special strings.
1911 (when specialp
1912 (setq text (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" text nil t)))
1913 ;; Handle break preservation if required.
1914 (when (plist-get info :preserve-breaks)
1915 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1916 text)))
1917 ;; Return value.
1918 text))
1921 ;;;; Planning
1923 (defun org-e-latex-planning (planning contents info)
1924 "Transcode a PLANNING element from Org to LaTeX.
1925 CONTENTS is nil. INFO is a plist holding contextual
1926 information."
1927 (concat
1928 "\\noindent"
1929 (mapconcat
1930 'identity
1931 (delq nil
1932 (list
1933 (let ((closed (org-element-property :closed planning)))
1934 (when closed
1935 (concat
1936 (format "\\textbf{%s} " org-closed-string)
1937 (format org-e-latex-inactive-timestamp-format
1938 (org-translate-time closed)))))
1939 (let ((deadline (org-element-property :deadline planning)))
1940 (when deadline
1941 (concat
1942 (format "\\textbf{%s} " org-deadline-string)
1943 (format org-e-latex-active-timestamp-format
1944 (org-translate-time deadline)))))
1945 (let ((scheduled (org-element-property :scheduled planning)))
1946 (when scheduled
1947 (concat
1948 (format "\\textbf{%s} " org-scheduled-string)
1949 (format org-e-latex-active-timestamp-format
1950 (org-translate-time scheduled)))))))
1951 " ")
1952 "\\\\"))
1955 ;;;; Property Drawer
1957 (defun org-e-latex-property-drawer (property-drawer contents info)
1958 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1959 CONTENTS is nil. INFO is a plist holding contextual
1960 information."
1961 ;; The property drawer isn't exported but we want separating blank
1962 ;; lines nonetheless.
1966 ;;;; Quote Block
1968 (defun org-e-latex-quote-block (quote-block contents info)
1969 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1970 CONTENTS holds the contents of the block. INFO is a plist
1971 holding contextual information."
1972 (org-e-latex--wrap-label
1973 quote-block
1974 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1977 ;;;; Quote Section
1979 (defun org-e-latex-quote-section (quote-section contents info)
1980 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1981 CONTENTS is nil. INFO is a plist holding contextual information."
1982 (let ((value (org-remove-indentation
1983 (org-element-property :value quote-section))))
1984 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1987 ;;;; Radio Target
1989 (defun org-e-latex-radio-target (radio-target text info)
1990 "Transcode a RADIO-TARGET object from Org to LaTeX.
1991 TEXT is the text of the target. INFO is a plist holding
1992 contextual information."
1993 (format "\\label{%s}%s"
1994 (org-export-solidify-link-text
1995 (org-element-property :value radio-target))
1996 text))
1999 ;;;; Section
2001 (defun org-e-latex-section (section contents info)
2002 "Transcode a SECTION element from Org to LaTeX.
2003 CONTENTS holds the contents of the section. INFO is a plist
2004 holding contextual information."
2005 contents)
2008 ;;;; Special Block
2010 (defun org-e-latex-special-block (special-block contents info)
2011 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2012 CONTENTS holds the contents of the block. INFO is a plist
2013 holding contextual information."
2014 (let ((type (downcase (org-element-property :type special-block))))
2015 (org-e-latex--wrap-label
2016 special-block
2017 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
2020 ;;;; Src Block
2022 (defun org-e-latex-src-block (src-block contents info)
2023 "Transcode a SRC-BLOCK element from Org to LaTeX.
2024 CONTENTS holds the contents of the item. INFO is a plist holding
2025 contextual information."
2026 (let* ((lang (org-element-property :language src-block))
2027 (caption (org-element-property :caption src-block))
2028 (label (org-element-property :name src-block))
2029 (custom-env (and lang
2030 (cadr (assq (intern lang)
2031 org-e-latex-custom-lang-environments))))
2032 (num-start (case (org-element-property :number-lines src-block)
2033 (continued (org-export-get-loc src-block info))
2034 (new 0)))
2035 (retain-labels (org-element-property :retain-labels src-block)))
2036 (cond
2037 ;; Case 1. No source fontification.
2038 ((not org-e-latex-listings)
2039 (let ((caption-str (org-e-latex--caption/label-string caption label info))
2040 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
2041 (format
2042 (or float-env "%s")
2043 (concat caption-str
2044 (format "\\begin{verbatim}\n%s\\end{verbatim}"
2045 (org-export-format-code-default src-block info))))))
2046 ;; Case 2. Custom environment.
2047 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2048 custom-env
2049 (org-export-format-code-default src-block info)
2050 custom-env))
2051 ;; Case 3. Use minted package.
2052 ((eq org-e-latex-listings 'minted)
2053 (let ((float-env (when (or label caption)
2054 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2055 (org-e-latex--caption/label-string
2056 caption label info))))
2057 (body
2058 (format
2059 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2060 ;; Options.
2061 (org-e-latex--make-option-string
2062 (if (not num-start) org-e-latex-minted-options
2063 (append `(("linenos")
2064 ("firstnumber" ,(number-to-string (1+ num-start))))
2065 org-e-latex-minted-options)))
2066 ;; Language.
2067 (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
2068 ;; Source code.
2069 (let* ((code-info (org-export-unravel-code src-block))
2070 (max-width
2071 (apply 'max
2072 (mapcar 'length
2073 (org-split-string (car code-info) "\n")))))
2074 (org-export-format-code
2075 (car code-info)
2076 (lambda (loc num ref)
2077 (concat
2079 (when ref
2080 ;; Ensure references are flushed to the right,
2081 ;; separated with 6 spaces from the widest line
2082 ;; of code.
2083 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2084 (format "(%s)" ref)))))
2085 nil (and retain-labels (cdr code-info)))))))
2086 ;; Return value.
2087 (if float-env (format float-env body) body)))
2088 ;; Case 4. Use listings package.
2090 (let ((lst-lang
2091 (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
2092 (caption-str
2093 (when caption
2094 (let ((main (org-export-data (car caption) info)))
2095 (if (not (cdr caption)) (format "{%s}" main)
2096 (format "{[%s]%s}"
2097 (org-export-data (cdr caption) info)
2098 main))))))
2099 (concat
2100 ;; Options.
2101 (format "\\lstset{%s}\n"
2102 (org-e-latex--make-option-string
2103 (append org-e-latex-listings-options
2104 `(("language" ,lst-lang))
2105 (when label `(("label" ,label)))
2106 (when caption-str `(("caption" ,caption-str)))
2107 (cond ((not num-start) '(("numbers" "none")))
2108 ((zerop num-start) '(("numbers" "left")))
2109 (t `(("numbers" "left")
2110 ("firstnumber"
2111 ,(number-to-string (1+ num-start)))))))))
2112 ;; Source code.
2113 (format
2114 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2115 (let* ((code-info (org-export-unravel-code src-block))
2116 (max-width
2117 (apply 'max
2118 (mapcar 'length
2119 (org-split-string (car code-info) "\n")))))
2120 (org-export-format-code
2121 (car code-info)
2122 (lambda (loc num ref)
2123 (concat
2125 (when ref
2126 ;; Ensure references are flushed to the right,
2127 ;; separated with 6 spaces from the widest line of
2128 ;; code
2129 (concat (make-string (+ (- max-width (length loc)) 6) ? )
2130 (format "(%s)" ref)))))
2131 nil (and retain-labels (cdr code-info)))))))))))
2134 ;;;; Statistics Cookie
2136 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
2137 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2138 CONTENTS is nil. INFO is a plist holding contextual information."
2139 (replace-regexp-in-string
2140 "%" "\\%" (org-element-property :value statistics-cookie) nil t))
2143 ;;;; Strike-Through
2145 (defun org-e-latex-strike-through (strike-through contents info)
2146 "Transcode STRIKE-THROUGH from Org to LaTeX.
2147 CONTENTS is the text with strike-through markup. INFO is a plist
2148 holding contextual information."
2149 (org-e-latex--text-markup contents 'strike-through))
2152 ;;;; Subscript
2154 (defun org-e-latex-subscript (subscript contents info)
2155 "Transcode a SUBSCRIPT object from Org to LaTeX.
2156 CONTENTS is the contents of the object. INFO is a plist holding
2157 contextual information."
2158 (if (= (length contents) 1) (format "$_%s$" contents)
2159 ;; Handle multiple objects in SUBSCRIPT by creating a subscript
2160 ;; command for each of them.
2161 (let ((prev-blanks 0))
2162 (mapconcat
2163 (lambda (obj)
2164 (case (org-element-type obj)
2165 ((entity latex-fragment)
2166 (setq prev-blanks (org-element-property :post-blank obj))
2167 (let ((data (org-trim (org-export-data obj info))))
2168 (string-match
2169 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2170 data)
2171 (format "$_{%s}$" (match-string 1 data))))
2172 (plain-text
2173 (format "$_\\mathrm{%s}$"
2174 (concat (make-string prev-blanks ? )
2175 ;; mathrm command doesn't handle spaces,
2176 ;; so we have to enforce them.
2177 (replace-regexp-in-string
2178 " " "\\\\ " (org-export-data obj info)))))
2179 (otherwise
2180 (setq prev-blanks (org-element-property :post-blank obj))
2181 (format "$_{%s}$" (org-export-data obj info)))))
2182 (org-element-contents subscript) ""))))
2185 ;;;; Superscript
2187 (defun org-e-latex-superscript (superscript contents info)
2188 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2189 CONTENTS is the contents of the object. INFO is a plist holding
2190 contextual information."
2191 (if (= (length contents) 1) (format "$^%s$" contents)
2192 ;; Handle multiple objects in SUPERSCRIPT by creating
2193 ;; a superscript command for each of them.
2194 (let ((prev-blanks 0))
2195 (mapconcat
2196 (lambda (obj)
2197 (case (org-element-type obj)
2198 ((entity latex-fragment)
2199 (setq prev-blanks (org-element-property :post-blank obj))
2200 (let ((data (org-trim (org-export-data obj info))))
2201 (string-match
2202 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2203 data)
2204 (format "$^{%s}$" (match-string 1 data))))
2205 (plain-text
2206 (format "$^\\mathrm{%s}$"
2207 (concat (make-string prev-blanks ? )
2208 ;; mathrm command doesn't handle spaces,
2209 ;; so we have to enforce them.
2210 (replace-regexp-in-string
2211 " " "\\\\ " (org-export-data obj info)))))
2212 (otherwise
2213 (setq prev-blanks (org-element-property :post-blank obj))
2214 (format "$^{%s}$" (org-export-data obj info)))))
2215 (org-element-contents superscript) ""))))
2218 ;;;; Table
2220 ;; `org-e-latex-table' is the entry point for table transcoding. It
2221 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
2222 ;; delegates the job to either `org-e-latex-table--table.el-table' or
2223 ;; `org-e-latex-table--org-table' functions, depending of the type of
2224 ;; the table.
2226 ;; `org-e-latex-table--align-string' is a subroutine used to build
2227 ;; alignment string for Org tables.
2229 (defun org-e-latex-table (table contents info)
2230 "Transcode a TABLE element from Org to LaTeX.
2231 CONTENTS is the contents of the table. INFO is a plist holding
2232 contextual information."
2233 (cond
2234 ;; Case 1: verbatim table.
2235 ((or org-e-latex-tables-verbatim
2236 (let ((attr (mapconcat 'identity
2237 (org-element-property :attr_latex table)
2238 " ")))
2239 (and attr (string-match "\\<verbatim\\>" attr))))
2240 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2241 ;; Re-create table, without affiliated keywords.
2242 (org-trim
2243 (org-element-interpret-data
2244 `(table nil ,@(org-element-contents table))))))
2245 ;; Case 2: table.el table. Convert it using appropriate tools.
2246 ((eq (org-element-property :type table) 'table.el)
2247 (org-e-latex-table--table.el-table table contents info))
2248 ;; Case 3: Standard table.
2249 (t (org-e-latex-table--org-table table contents info))))
2251 (defun org-e-latex-table--align-string (table info)
2252 "Return an appropriate LaTeX alignment string.
2253 TABLE is the considered table. INFO is a plist used as
2254 a communication channel."
2255 (let ((attr (mapconcat 'identity
2256 (org-element-property :attr_latex table)
2257 " ")))
2258 (if (string-match "\\<align=\\(\\S-+\\)" attr) (match-string 1 attr)
2259 (let (alignment)
2260 ;; Extract column groups and alignment from first (non-rule)
2261 ;; row.
2262 (org-element-map
2263 (org-element-map
2264 table 'table-row
2265 (lambda (row)
2266 (and (eq (org-element-property :type row) 'standard) row))
2267 info 'first-match)
2268 'table-cell
2269 (lambda (cell)
2270 (let ((borders (org-export-table-cell-borders cell info)))
2271 ;; Check left border for the first cell only.
2272 (when (and (memq 'left borders) (not alignment))
2273 (push "|" alignment))
2274 (push (case (org-export-table-cell-alignment cell info)
2275 (left "l")
2276 (right "r")
2277 (center "c"))
2278 alignment)
2279 (when (memq 'right borders) (push "|" alignment))))
2280 info)
2281 (apply 'concat (reverse alignment))))))
2283 (defun org-e-latex-table--org-table (table contents info)
2284 "Return appropriate LaTeX code for an Org table.
2286 TABLE is the table type element to transcode. CONTENTS is its
2287 contents, as a string. INFO is a plist used as a communication
2288 channel.
2290 This function assumes TABLE has `org' as its `:type' attribute."
2291 (let* ((label (org-element-property :name table))
2292 (caption (org-e-latex--caption/label-string
2293 (org-element-property :caption table) label info))
2294 (attr (mapconcat 'identity
2295 (org-element-property :attr_latex table)
2296 " "))
2297 ;; Determine alignment string.
2298 (alignment (org-e-latex-table--align-string table info))
2299 ;; Determine environment for the table: longtable, tabular...
2300 (table-env (cond
2301 ((not attr) org-e-latex-default-table-environment)
2302 ((string-match "\\<longtable\\>" attr) "longtable")
2303 ((string-match "\\<tabular.?\\>" attr)
2304 (org-match-string-no-properties 0 attr))
2305 (t org-e-latex-default-table-environment)))
2306 ;; If table is a float, determine environment: table, table*
2307 ;; or sidewaystable.
2308 (float-env (cond
2309 ((string= "longtable" table-env) nil)
2310 ((and attr (string-match "\\<sidewaystable\\>" attr))
2311 "sidewaystables")
2312 ((and attr
2313 (or (string-match (regexp-quote "table*") attr)
2314 (string-match "\\<multicolumn\\>" attr)))
2315 "table*")
2316 ((or (not (string= caption "")) label) "table")))
2317 ;; Extract others display options.
2318 (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
2319 (org-match-string-no-properties 1 attr)))
2320 (placement
2321 (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
2322 (org-match-string-no-properties 1 attr)
2323 (format "[%s]" org-e-latex-default-figure-position))))
2324 ;; Prepare the final format string for the table.
2325 (cond
2326 ;; Longtable.
2327 ((string= "longtable" table-env)
2328 (format
2329 "\\begin{longtable}{%s}\n%s%s%s\\end{longtable}"
2330 alignment
2331 (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
2332 (concat (org-trim caption) "\\\\\n"))
2333 contents
2334 (if (or org-e-latex-table-caption-above (string= "" caption)) ""
2335 (concat (org-trim caption) "\\\\\n"))))
2336 ;; Others.
2337 (t (concat (when float-env
2338 (concat
2339 (format "\\begin{%s}%s\n" float-env placement)
2340 (if org-e-latex-table-caption-above caption "")))
2341 (when org-e-latex-tables-centered "\\begin{center}\n")
2342 (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2343 table-env
2344 (if width (format "{%s}" width) "")
2345 alignment
2346 contents
2347 table-env)
2348 (when org-e-latex-tables-centered "\n\\end{center}")
2349 (when float-env
2350 (concat (if org-e-latex-table-caption-above "" caption)
2351 (format "\n\\end{%s}" float-env))))))))
2353 (defun org-e-latex-table--table.el-table (table contents info)
2354 "Return appropriate LaTeX code for a table.el table.
2356 TABLE is the table type element to transcode. CONTENTS is its
2357 contents, as a string. INFO is a plist used as a communication
2358 channel.
2360 This function assumes TABLE has `table.el' as its `:type'
2361 attribute."
2362 (require 'table)
2363 ;; Ensure "*org-export-table*" buffer is empty.
2364 (with-current-buffer (get-buffer-create "*org-export-table*")
2365 (erase-buffer))
2366 (let ((output (with-temp-buffer
2367 (insert (org-element-property :value table))
2368 (goto-char 1)
2369 (re-search-forward "^[ \t]*|[^|]" nil t)
2370 (table-generate-source 'latex "*org-export-table*")
2371 (with-current-buffer "*org-export-table*"
2372 (org-trim (buffer-string))))))
2373 (kill-buffer (get-buffer "*org-export-table*"))
2374 ;; Remove left out comments.
2375 (while (string-match "^%.*\n" output)
2376 (setq output (replace-match "" t t output)))
2377 ;; When the "rmlines" attribute is provided, remove all hlines but
2378 ;; the the one separating heading from the table body.
2379 (let ((attr (mapconcat 'identity
2380 (org-element-property :attr_latex table)
2381 " ")))
2382 (when (and attr (string-match "\\<rmlines\\>" attr))
2383 (let ((n 0) (pos 0))
2384 (while (and (< (length output) pos)
2385 (setq pos (string-match "^\\\\hline\n?" output pos)))
2386 (incf n)
2387 (unless (= n 2)
2388 (setq output (replace-match "" nil nil output)))))))
2389 (if (not org-e-latex-tables-centered) output
2390 (format "\\begin{center}\n%s\n\\end{center}" output))))
2393 ;;;; Table Cell
2395 (defun org-e-latex-table-cell (table-cell contents info)
2396 "Transcode a TABLE-CELL element from Org to LaTeX.
2397 CONTENTS is the cell contents. INFO is a plist used as
2398 a communication channel."
2399 (concat (if (and contents
2400 org-e-latex-table-scientific-notation
2401 (string-match orgtbl-exp-regexp contents))
2402 ;; Use appropriate format string for scientific
2403 ;; notation.
2404 (format org-e-latex-table-scientific-notation
2405 (match-string 1 contents)
2406 (match-string 2 contents))
2407 contents)
2408 (when (org-export-get-next-element table-cell info) " & ")))
2411 ;;;; Table Row
2413 (defun org-e-latex-table-row (table-row contents info)
2414 "Transcode a TABLE-ROW element from Org to LaTeX.
2415 CONTENTS is the contents of the row. INFO is a plist used as
2416 a communication channel."
2417 ;; Rules are ignored since table separators are deduced from
2418 ;; borders of the current row.
2419 (when (eq (org-element-property :type table-row) 'standard)
2420 (let* ((attr (mapconcat 'identity
2421 (org-element-property
2422 :attr_latex (org-export-get-parent table-row))
2423 " "))
2424 (longtablep (and attr (string-match "\\<longtable\\>" attr)))
2425 (booktabsp
2426 (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
2427 org-e-latex-tables-booktabs))
2428 ;; TABLE-ROW's borders are extracted from its first cell.
2429 (borders
2430 (org-export-table-cell-borders
2431 (car (org-element-contents table-row)) info)))
2432 (concat
2433 ;; When BOOKTABS are activated enforce top-rule even when no
2434 ;; hline was specifically marked.
2435 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2436 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2437 contents "\\\\\n"
2438 (cond
2439 ;; Special case for long tables. Define header and footers.
2440 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2441 (format "%s
2442 \\endhead
2443 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2444 \\endfoot
2445 \\endlastfoot"
2446 (if booktabsp "\\midrule" "\\hline")
2447 (if booktabsp "\\midrule" "\\hline")
2448 ;; Number of columns.
2449 (cdr (org-export-table-dimensions
2450 (org-export-get-parent-table table-row) info))))
2451 ;; When BOOKTABS are activated enforce bottom rule even when
2452 ;; no hline was specifically marked.
2453 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2454 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2455 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2458 ;;;; Target
2460 (defun org-e-latex-target (target contents info)
2461 "Transcode a TARGET object from Org to LaTeX.
2462 CONTENTS is nil. INFO is a plist holding contextual
2463 information."
2464 (format "\\label{%s}"
2465 (org-export-solidify-link-text (org-element-property :value target))))
2468 ;;;; Timestamp
2470 (defun org-e-latex-timestamp (timestamp contents info)
2471 "Transcode a TIMESTAMP object from Org to LaTeX.
2472 CONTENTS is nil. INFO is a plist holding contextual
2473 information."
2474 (let ((value (org-translate-time (org-element-property :value timestamp)))
2475 (range-end (org-element-property :range-end timestamp)))
2476 (case (org-element-property :type timestamp)
2477 (active (format org-e-latex-active-timestamp-format value))
2478 (active-range
2479 (concat (format org-e-latex-active-timestamp-format value)
2480 "--"
2481 (format org-e-latex-active-timestamp-format
2482 (org-translate-time range-end))))
2483 (inactive (format org-e-latex-inactive-timestamp-format value))
2484 (inactive-range
2485 (concat (format org-e-latex-inactive-timestamp-format value)
2486 "--"
2487 (format org-e-latex-inactive-timestamp-format
2488 (org-translate-time range-end))))
2489 (otherwise (format org-e-latex-diary-timestamp-format value)))))
2492 ;;;; Underline
2494 (defun org-e-latex-underline (underline contents info)
2495 "Transcode UNDERLINE from Org to LaTeX.
2496 CONTENTS is the text with underline markup. INFO is a plist
2497 holding contextual information."
2498 (org-e-latex--text-markup contents 'underline))
2501 ;;;; Verbatim
2503 (defun org-e-latex-verbatim (verbatim contents info)
2504 "Transcode a VERBATIM object from Org to LaTeX.
2505 CONTENTS is nil. INFO is a plist used as a communication
2506 channel."
2507 (org-e-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2510 ;;;; Verse Block
2512 (defun org-e-latex-verse-block (verse-block contents info)
2513 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2514 CONTENTS is verse block contents. INFO is a plist holding
2515 contextual information."
2516 (org-e-latex--wrap-label
2517 verse-block
2518 ;; In a verse environment, add a line break to each newline
2519 ;; character and change each white space at beginning of a line
2520 ;; into a space of 1 em. Also change each blank line with
2521 ;; a vertical space of 1 em.
2522 (progn
2523 (setq contents (replace-regexp-in-string
2524 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2525 (replace-regexp-in-string
2526 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2527 (while (string-match "^[ \t]+" contents)
2528 (let ((new-str (format "\\hspace*{%dem}"
2529 (length (match-string 0 contents)))))
2530 (setq contents (replace-match new-str nil t contents))))
2531 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2535 ;;; Interactive functions
2537 ;;;###autoload
2538 (defun org-e-latex-export-as-latex
2539 (&optional subtreep visible-only body-only ext-plist)
2540 "Export current buffer as a LaTeX buffer.
2542 If narrowing is active in the current buffer, only export its
2543 narrowed part.
2545 If a region is active, export that region.
2547 When optional argument SUBTREEP is non-nil, export the sub-tree
2548 at point, extracting information from the headline properties
2549 first.
2551 When optional argument VISIBLE-ONLY is non-nil, don't export
2552 contents of hidden elements.
2554 When optional argument BODY-ONLY is non-nil, only write code
2555 between \"\\begin{document}\" and \"\\end{document}\".
2557 EXT-PLIST, when provided, is a property list with external
2558 parameters overriding Org default settings, but still inferior to
2559 file-local settings.
2561 Export is done in a buffer named \"*Org E-LATEX Export*\", which
2562 will be displayed when `org-export-show-temporary-export-buffer'
2563 is non-nil."
2564 (interactive)
2565 (let ((outbuf (org-export-to-buffer
2566 'e-latex "*Org E-LATEX Export*"
2567 subtreep visible-only body-only ext-plist)))
2568 (with-current-buffer outbuf (LaTeX-mode))
2569 (when org-export-show-temporary-export-buffer
2570 (switch-to-buffer-other-window outbuf))))
2572 ;;;###autoload
2573 (defun org-e-latex-export-to-latex
2574 (&optional subtreep visible-only body-only ext-plist pub-dir)
2575 "Export current buffer to a LaTeX file.
2577 If narrowing is active in the current buffer, only export its
2578 narrowed part.
2580 If a region is active, export that region.
2582 When optional argument SUBTREEP is non-nil, export the sub-tree
2583 at point, extracting information from the headline properties
2584 first.
2586 When optional argument VISIBLE-ONLY is non-nil, don't export
2587 contents of hidden elements.
2589 When optional argument BODY-ONLY is non-nil, only write code
2590 between \"\\begin{document}\" and \"\\end{document}\".
2592 EXT-PLIST, when provided, is a property list with external
2593 parameters overriding Org default settings, but still inferior to
2594 file-local settings.
2596 When optional argument PUB-DIR is set, use it as the publishing
2597 directory.
2599 Return output file's name."
2600 (interactive)
2601 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
2602 (org-export-to-file
2603 'e-latex outfile subtreep visible-only body-only ext-plist)))
2605 ;;;###autoload
2606 (defun org-e-latex-export-to-pdf
2607 (&optional subtreep visible-only body-only ext-plist pub-dir)
2608 "Export current buffer to LaTeX then process through to PDF.
2610 If narrowing is active in the current buffer, only export its
2611 narrowed part.
2613 If a region is active, export that region.
2615 When optional argument SUBTREEP is non-nil, export the sub-tree
2616 at point, extracting information from the headline properties
2617 first.
2619 When optional argument VISIBLE-ONLY is non-nil, don't export
2620 contents of hidden elements.
2622 When optional argument BODY-ONLY is non-nil, only write code
2623 between \"\\begin{document}\" and \"\\end{document}\".
2625 EXT-PLIST, when provided, is a property list with external
2626 parameters overriding Org default settings, but still inferior to
2627 file-local settings.
2629 When optional argument PUB-DIR is set, use it as the publishing
2630 directory.
2632 Return PDF file's name."
2633 (interactive)
2634 (org-e-latex-compile
2635 (org-e-latex-export-to-latex
2636 subtreep visible-only body-only ext-plist pub-dir)))
2638 (defun org-e-latex-compile (texfile)
2639 "Compile a TeX file.
2641 TEXFILE is the name of the file being compiled. Processing is
2642 done through the command specified in `org-e-latex-pdf-process'.
2644 Return PDF file name or an error if it couldn't be produced."
2645 (let* ((wconfig (current-window-configuration))
2646 (texfile (file-truename texfile))
2647 (base (file-name-sans-extension texfile))
2648 errors)
2649 (message (format "Processing LaTeX file %s ..." texfile))
2650 (unwind-protect
2651 (progn
2652 (cond
2653 ;; A function is provided: Apply it.
2654 ((functionp org-e-latex-pdf-process)
2655 (funcall org-e-latex-pdf-process (shell-quote-argument texfile)))
2656 ;; A list is provided: Replace %b, %f and %o with appropriate
2657 ;; values in each command before applying it. Output is
2658 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2659 ((consp org-e-latex-pdf-process)
2660 (let* ((out-dir (or (file-name-directory texfile) "./"))
2661 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2662 (mapc
2663 (lambda (command)
2664 (shell-command
2665 (replace-regexp-in-string
2666 "%b" (shell-quote-argument base)
2667 (replace-regexp-in-string
2668 "%f" (shell-quote-argument texfile)
2669 (replace-regexp-in-string
2670 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2671 outbuf))
2672 org-e-latex-pdf-process)
2673 ;; Collect standard errors from output buffer.
2674 (setq errors (org-e-latex--collect-errors outbuf))))
2675 (t (error "No valid command to process to PDF")))
2676 (let ((pdffile (concat base ".pdf")))
2677 ;; Check for process failure. Provide collected errors if
2678 ;; possible.
2679 (if (not (file-exists-p pdffile))
2680 (error (concat (format "PDF file %s wasn't produced" pdffile)
2681 (when errors (concat ": " errors))))
2682 ;; Else remove log files, when specified, and signal end of
2683 ;; process to user, along with any error encountered.
2684 (when org-e-latex-remove-logfiles
2685 (dolist (ext org-e-latex-logfiles-extensions)
2686 (let ((file (concat base "." ext)))
2687 (when (file-exists-p file) (delete-file file)))))
2688 (message (concat "Process completed"
2689 (if (not errors) "."
2690 (concat " with errors: " errors)))))
2691 ;; Return output file name.
2692 pdffile))
2693 (set-window-configuration wconfig))))
2695 (defun org-e-latex--collect-errors (buffer)
2696 "Collect some kind of errors from \"pdflatex\" command output.
2698 BUFFER is the buffer containing output.
2700 Return collected error types as a string, or nil if there was
2701 none."
2702 (with-current-buffer buffer
2703 (save-excursion
2704 (goto-char (point-max))
2705 ;; Find final "pdflatex" run.
2706 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
2707 (let ((case-fold-search t)
2708 (errors ""))
2709 (when (save-excursion
2710 (re-search-forward "Reference.*?undefined" nil t))
2711 (setq errors (concat errors " [undefined reference]")))
2712 (when (save-excursion
2713 (re-search-forward "Citation.*?undefined" nil t))
2714 (setq errors (concat errors " [undefined citation]")))
2715 (when (save-excursion
2716 (re-search-forward "Undefined control sequence" nil t))
2717 (setq errors (concat errors " [undefined control sequence]")))
2718 (when (save-excursion
2719 (re-search-forward "^! LaTeX.*?Error" nil t))
2720 (setq errors (concat errors " [LaTeX error]")))
2721 (when (save-excursion
2722 (re-search-forward "^! Package.*?Error" nil t))
2723 (setq errors (concat errors " [package error]")))
2724 (and (org-string-nw-p errors) (org-trim errors)))))))
2727 (provide 'org-e-latex)
2728 ;;; org-e-latex.el ends here