org-element: Verse blocks now contain objects
[org-mode/org-mode-NeilSmithlineMods.git] / contrib / lisp / org-e-latex.el
blob3c78b645330a95df5d7238159f977597f70b9403
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))
40 (defvar org-export-latex-default-packages-alist)
41 (defvar org-export-latex-packages-alist)
43 (declare-function org-element-property "org-element" (property element))
44 (declare-function org-element-normalize-string "org-element" (s))
45 (declare-function org-element-parse-secondary-string
46 "org-element" (string restriction &optional buffer))
47 (defvar org-element-string-restrictions)
48 (defvar org-element-object-restrictions)
50 (declare-function org-export-clean-table "org-export" (table specialp))
51 (declare-function org-export-data "org-export" (data backend info))
52 (declare-function org-export-directory "org-export" (type plist))
53 (declare-function org-export-expand-macro "org-export" (macro info))
54 (declare-function org-export-first-sibling-p "org-export" (headline info))
55 (declare-function org-export-footnote-first-reference-p "org-export"
56 (footnote-reference info))
57 (declare-function org-export-format-code "org-export"
58 (code fun &optional num-lines ref-alist))
59 (declare-function org-export-format-code-default "org-export" (element info))
60 (declare-function org-export-get-coderef-format "org-export" (path desc))
61 (declare-function org-export-get-footnote-definition "org-export"
62 (footnote-reference info))
63 (declare-function org-export-get-footnote-number "org-export" (footnote info))
64 (declare-function org-export-get-previous-element "org-export" (blob info))
65 (declare-function org-export-get-relative-level "org-export" (headline info))
66 (declare-function org-export-unravel-code "org-export" (element))
67 (declare-function org-export-included-file "org-export" (keyword backend info))
68 (declare-function org-export-inline-image-p "org-export"
69 (link &optional extensions))
70 (declare-function org-export-last-sibling-p "org-export" (headline info))
71 (declare-function org-export-low-level-p "org-export" (headline info))
72 (declare-function org-export-output-file-name
73 "org-export" (extension &optional subtreep pub-dir))
74 (declare-function org-export-resolve-coderef "org-export" (ref info))
75 (declare-function org-export-resolve-fuzzy-link "org-export" (link info))
76 (declare-function org-export-secondary-string "org-export"
77 (secondary backend info))
78 (declare-function org-export-solidify-link-text "org-export" (s))
79 (declare-function org-export-table-format-info "org-export" (table))
80 (declare-function
81 org-export-to-buffer "org-export"
82 (backend buffer &optional subtreep visible-only body-only ext-plist))
83 (declare-function
84 org-export-to-file "org-export"
85 (backend file &optional subtreep visible-only body-only ext-plist))
89 ;;; Internal Variables
91 (defconst org-e-latex-option-alist
92 '((:date "DATE" nil org-e-latex-date-format t)
93 (:latex-class "LATEX_CLASS" nil org-e-latex-default-class t)
94 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
95 (:latex-header-extra "LATEX_HEADER" nil nil newline))
96 "Alist between LaTeX export properties and ways to set them.
97 See `org-export-option-alist' for more information on the
98 structure of the value.")
102 ;;; User Configurable Variables
104 (defgroup org-export-e-latex nil
105 "Options for exporting Org mode files to LaTeX."
106 :tag "Org Export LaTeX"
107 :group 'org-export)
110 ;;;; Preamble
112 (defcustom org-e-latex-default-class "article"
113 "The default LaTeX class."
114 :group 'org-export-e-latex
115 :type '(string :tag "LaTeX class"))
117 (defcustom org-e-latex-classes
118 '(("article"
119 "\\documentclass[11pt]{article}"
120 ("\\section{%s}" . "\\section*{%s}")
121 ("\\subsection{%s}" . "\\subsection*{%s}")
122 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
123 ("\\paragraph{%s}" . "\\paragraph*{%s}")
124 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
125 ("report"
126 "\\documentclass[11pt]{report}"
127 ("\\part{%s}" . "\\part*{%s}")
128 ("\\chapter{%s}" . "\\chapter*{%s}")
129 ("\\section{%s}" . "\\section*{%s}")
130 ("\\subsection{%s}" . "\\subsection*{%s}")
131 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
132 ("book"
133 "\\documentclass[11pt]{book}"
134 ("\\part{%s}" . "\\part*{%s}")
135 ("\\chapter{%s}" . "\\chapter*{%s}")
136 ("\\section{%s}" . "\\section*{%s}")
137 ("\\subsection{%s}" . "\\subsection*{%s}")
138 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
139 "Alist of LaTeX classes and associated header and structure.
140 If #+LaTeX_CLASS is set in the buffer, use its value and the
141 associated information. Here is the structure of each cell:
143 \(class-name
144 header-string
145 \(numbered-section . unnumbered-section\)
146 ...\)
148 The header string
149 -----------------
151 The HEADER-STRING is the header that will be inserted into the
152 LaTeX file. It should contain the \\documentclass macro, and
153 anything else that is needed for this setup. To this header, the
154 following commands will be added:
156 - Calls to \\usepackage for all packages mentioned in the
157 variables `org-export-latex-default-packages-alist' and
158 `org-export-latex-packages-alist'. Thus, your header
159 definitions should avoid to also request these packages.
161 - Lines specified via \"#+LaTeX_HEADER:\"
163 If you need more control about the sequence in which the header
164 is built up, or if you want to exclude one of these building
165 blocks for a particular class, you can use the following
166 macro-like placeholders.
168 [DEFAULT-PACKAGES] \\usepackage statements for default packages
169 [NO-DEFAULT-PACKAGES] do not include any of the default packages
170 [PACKAGES] \\usepackage statements for packages
171 [NO-PACKAGES] do not include the packages
172 [EXTRA] the stuff from #+LaTeX_HEADER
173 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
174 [BEAMER-HEADER-EXTRA] the beamer extra headers
176 So a header like
178 \\documentclass{article}
179 [NO-DEFAULT-PACKAGES]
180 [EXTRA]
181 \\providecommand{\\alert}[1]{\\textbf{#1}}
182 [PACKAGES]
184 will omit the default packages, and will include the
185 #+LaTeX_HEADER lines, then have a call to \\providecommand, and
186 then place \\usepackage commands based on the content of
187 `org-export-latex-packages-alist'.
189 If your header, `org-export-latex-default-packages-alist' or
190 `org-export-latex-packages-alist' inserts
191 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
192 replaced with a coding system derived from
193 `buffer-file-coding-system'. See also the variable
194 `org-e-latex-inputenc-alist' for a way to influence this
195 mechanism.
197 The sectioning structure
198 ------------------------
200 The sectioning structure of the class is given by the elements
201 following the header string. For each sectioning level, a number
202 of strings is specified. A %s formatter is mandatory in each
203 section string and will be replaced by the title of the section.
205 Instead of a cons cell \(numbered . unnumbered\), you can also
206 provide a list of 2 or 4 elements,
208 \(numbered-open numbered-close\)
212 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
214 providing opening and closing strings for a LaTeX environment
215 that should represent the document section. The opening clause
216 should have a %s to represent the section title.
218 Instead of a list of sectioning commands, you can also specify
219 a function name. That function will be called with two
220 parameters, the \(reduced) level of the headline, and a predicate
221 non-nil when the headline should be numbered. It must return
222 a format string in which the section title will be added."
223 :group 'org-export-e-latex
224 :type '(repeat
225 (list (string :tag "LaTeX class")
226 (string :tag "LaTeX header")
227 (repeat :tag "Levels" :inline t
228 (choice
229 (cons :tag "Heading"
230 (string :tag " numbered")
231 (string :tag "unnumbered"))
232 (list :tag "Environment"
233 (string :tag "Opening (numbered)")
234 (string :tag "Closing (numbered)")
235 (string :tag "Opening (unnumbered)")
236 (string :tag "Closing (unnumbered)"))
237 (function :tag "Hook computing sectioning"))))))
239 (defcustom org-e-latex-inputenc-alist nil
240 "Alist of inputenc coding system names, and what should really be used.
241 For example, adding an entry
243 (\"utf8\" . \"utf8x\")
245 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
246 are written as utf8 files."
247 :group 'org-export-e-latex
248 :type '(repeat
249 (cons
250 (string :tag "Derived from buffer")
251 (string :tag "Use this instead"))))
253 (defcustom org-e-latex-date-format
254 "\\today"
255 "Format string for \\date{...}."
256 :group 'org-export-e-latex
257 :type 'boolean)
259 (defcustom org-e-latex-title-command "\\maketitle"
260 "The command used to insert the title just after \\begin{document}.
261 If this string contains the formatting specification \"%s\" then
262 it will be used as a formatting string, passing the title as an
263 argument."
264 :group 'org-export-e-latex
265 :type 'string)
268 ;;;; Headline
270 (defcustom org-e-latex-format-headline-function nil
271 "Function to format headline text.
273 This function will be called with 5 arguments:
274 TODO the todo keyword \(string or nil\).
275 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
276 PRIORITY the priority of the headline \(integer or nil\)
277 TEXT the main headline text \(string\).
278 TAGS the tags string, separated with colons \(string or nil\).
280 The function result will be used in the section format string.
282 As an example, one could set the variable to the following, in
283 order to reproduce the default set-up:
285 \(defun org-e-latex-format-headline \(todo todo-type priority text tags\)
286 \"Default format function for an headline.\"
287 \(concat \(when todo
288 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
289 \(when priority
290 \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
291 text
292 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
293 :group 'org-export-e-latex
294 :type 'function)
297 ;;;; Emphasis
299 (defcustom org-e-latex-emphasis-alist
300 '(("*" . "\\textbf{%s}")
301 ("/" . "\\emph{%s}")
302 ("_" . "\\underline{%s}")
303 ("+" . "\\st{%s}")
304 ("=" . protectedtexttt)
305 ("~" . verb))
306 "Alist of LaTeX expressions to convert emphasis fontifiers.
308 The key is the character used as a marker for fontification. The
309 value is a formatting string to wrap fontified text with.
311 Value can also be set to the following symbols: `verb' and
312 `protectedtexttt'. For the former, Org will use \"\\verb\" to
313 create a format string and select a delimiter character that
314 isn't in the string. For the latter, Org will use \"\\texttt\"
315 to typeset and try to protect special characters."
316 :group 'org-export-e-latex
317 :type 'alist)
320 ;;;; Footnotes
322 (defcustom org-e-latex-footnote-separator "\\textsuperscript{,}\\,"
323 "Text used to separate footnotes."
324 :group 'org-export-e-latex
325 :type 'string)
328 ;;;; Time-stamps
330 (defcustom org-e-latex-active-timestamp-format "\\textit{%s}"
331 "A printf format string to be applied to active time-stamps."
332 :group 'org-export-e-latex
333 :type 'string)
335 (defcustom org-e-latex-inactive-timestamp-format "\\textit{%s}"
336 "A printf format string to be applied to inactive time-stamps."
337 :group 'org-export-e-latex
338 :type 'string)
340 (defcustom org-e-latex-diary-timestamp-format "\\textit{%s}"
341 "A printf format string to be applied to diary time-stamps."
342 :group 'org-export-e-latex
343 :type 'string)
346 ;;;; Links
348 (defcustom org-e-latex-image-default-option "width=.9\\linewidth"
349 "Default option for images."
350 :group 'org-export-e-latex
351 :type 'string)
353 (defcustom org-e-latex-default-figure-position "htb"
354 "Default position for latex figures."
355 :group 'org-export-e-latex
356 :type 'string)
358 (defcustom org-e-latex-inline-image-rules
359 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\)\\'"))
360 "Rules characterizing image files that can be inlined into LaTeX.
362 A rule consists in an association whose key is the type of link
363 to consider, and value is a regexp that will be matched against
364 link's path.
366 Note that, by default, the image extension *actually* allowed
367 depend on the way the LaTeX file is processed. When used with
368 pdflatex, pdf, jpg and png images are OK. When processing
369 through dvi to Postscript, only ps and eps are allowed. The
370 default we use here encompasses both."
371 :group 'org-export-e-latex
372 :type '(alist :key-type (string :tag "Type")
373 :value-type (regexp :tag "Path")))
376 ;;;; Tables
378 (defcustom org-e-latex-default-table-environment "tabular"
379 "Default environment used to build tables."
380 :group 'org-export-e-latex
381 :type 'string)
383 (defcustom org-e-latex-tables-centered t
384 "When non-nil, tables are exported in a center environment."
385 :group 'org-export-e-latex
386 :type 'boolean)
388 (defcustom org-e-latex-tables-verbatim nil
389 "When non-nil, tables are exported verbatim."
390 :group 'org-export-e-latex
391 :type 'boolean)
393 (defcustom org-e-latex-tables-booktabs nil
394 "When non-nil, display tables in a formal \"booktabs\" style.
395 This option assumes that the \"booktabs\" package is properly
396 loaded in the header of the document. This value can be ignored
397 locally with \"booktabs=yes\" and \"booktabs=no\" LaTeX
398 attributes."
399 :group 'org-export-e-latex
400 :type 'boolean)
402 (defcustom org-e-latex-table-caption-above t
403 "When non-nil, place caption string at the beginning of the table.
404 Otherwise, place it near the end."
405 :group 'org-export-e-latex
406 :type 'boolean)
408 (defcustom org-e-latex-table-scientific-notation "%s\\,(%s)"
409 "Format string to display numbers in scientific notation.
410 The format should have \"%s\" twice, for mantissa and exponent
411 \(i.e. \"%s\\\\times10^{%s}\").
413 When nil, no transformation is made."
414 :group 'org-export-e-latex
415 :type '(choice
416 (string :tag "Format string")
417 (const :tag "No formatting")))
420 ;;;; Drawers
422 (defcustom org-e-latex-format-drawer-function nil
423 "Function called to format a drawer in LaTeX code.
425 The function must accept two parameters:
426 NAME the drawer name, like \"LOGBOOK\"
427 CONTENTS the contents of the drawer.
429 The function should return the string to be exported.
431 For example, the variable could be set to the following function
432 in order to mimic default behaviour:
434 \(defun org-e-latex-format-drawer-default \(name contents\)
435 \"Format a drawer element for LaTeX export.\"
436 contents\)"
437 :group 'org-export-e-latex
438 :type 'function)
441 ;;;; Inlinetasks
443 (defcustom org-e-latex-format-inlinetask-function nil
444 "Function called to format an inlinetask in LaTeX code.
446 The function must accept six parameters:
447 TODO the todo keyword, as a string
448 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
449 PRIORITY the inlinetask priority, as a string
450 NAME the inlinetask name, as a string.
451 TAGS the inlinetask tags, as a string.
452 CONTENTS the contents of the inlinetask, as a string.
454 The function should return the string to be exported.
456 For example, the variable could be set to the following function
457 in order to mimic default behaviour:
459 \(defun org-e-latex-format-inlinetask \(todo type priority name tags contents\)
460 \"Format an inline task element for LaTeX export.\"
461 \(let \(\(full-title
462 \(concat
463 \(when todo
464 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
465 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
466 title
467 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
468 \(format \(concat \"\\\\begin{center}\\n\"
469 \"\\\\fbox{\\n\"
470 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
471 \"%s\\n\\n\"
472 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
473 \"%s\"
474 \"\\\\end{minipage}}\"
475 \"\\\\end{center}\"\)
476 full-title contents\)\)"
477 :group 'org-export-e-latex
478 :type 'function)
481 ;; Src blocks
483 (defcustom org-e-latex-listings nil
484 "Non-nil means export source code using the listings package.
485 This package will fontify source code, possibly even with color.
486 If you want to use this, you also need to make LaTeX use the
487 listings package, and if you want to have color, the color
488 package. Just add these to `org-export-latex-packages-alist',
489 for example using customize, or with something like:
491 \(require 'org-e-latex)
492 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"listings\"))
493 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"color\"))
495 Alternatively,
497 \(setq org-e-latex-listings 'minted)
499 causes source code to be exported using the minted package as
500 opposed to listings. If you want to use minted, you need to add
501 the minted package to `org-export-latex-packages-alist', for
502 example using customize, or with
504 \(require 'org-e-latex)
505 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"minted\"))
507 In addition, it is necessary to install pygments
508 \(http://pygments.org), and to configure the variable
509 `org-e-latex-pdf-process' so that the -shell-escape option is
510 passed to pdflatex."
511 :group 'org-export-e-latex
512 :type '(choice
513 (const :tag "Use listings" t)
514 (const :tag "Use minted" 'minted)
515 (const :tag "Export verbatim" nil)))
517 (defcustom org-e-latex-listings-langs
518 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
519 (c "C") (cc "C++")
520 (fortran "fortran")
521 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
522 (html "HTML") (xml "XML")
523 (tex "TeX") (latex "TeX")
524 (shell-script "bash")
525 (gnuplot "Gnuplot")
526 (ocaml "Caml") (caml "Caml")
527 (sql "SQL") (sqlite "sql"))
528 "Alist mapping languages to their listing language counterpart.
529 The key is a symbol, the major mode symbol without the \"-mode\".
530 The value is the string that should be inserted as the language
531 parameter for the listings package. If the mode name and the
532 listings name are the same, the language does not need an entry
533 in this list - but it does not hurt if it is present."
534 :group 'org-export-e-latex
535 :type '(repeat
536 (list
537 (symbol :tag "Major mode ")
538 (string :tag "Listings language"))))
540 (defcustom org-e-latex-listings-options nil
541 "Association list of options for the latex listings package.
543 These options are supplied as a comma-separated list to the
544 \\lstset command. Each element of the association list should be
545 a list containing two strings: the name of the option, and the
546 value. For example,
548 (setq org-e-latex-listings-options
549 '((\"basicstyle\" \"\\small\")
550 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
552 will typeset the code in a small size font with underlined, bold
553 black keywords.
555 Note that the same options will be applied to blocks of all
556 languages."
557 :group 'org-export-e-latex
558 :type '(repeat
559 (list
560 (string :tag "Listings option name ")
561 (string :tag "Listings option value"))))
563 (defcustom org-e-latex-minted-langs
564 '((emacs-lisp "common-lisp")
565 (cc "c++")
566 (cperl "perl")
567 (shell-script "bash")
568 (caml "ocaml"))
569 "Alist mapping languages to their minted language counterpart.
570 The key is a symbol, the major mode symbol without the \"-mode\".
571 The value is the string that should be inserted as the language
572 parameter for the minted package. If the mode name and the
573 listings name are the same, the language does not need an entry
574 in this list - but it does not hurt if it is present.
576 Note that minted uses all lower case for language identifiers,
577 and that the full list of language identifiers can be obtained
578 with:
580 pygmentize -L lexers"
581 :group 'org-export-e-latex
582 :type '(repeat
583 (list
584 (symbol :tag "Major mode ")
585 (string :tag "Minted language"))))
587 (defcustom org-e-latex-minted-options nil
588 "Association list of options for the latex minted package.
590 These options are supplied within square brackets in
591 \\begin{minted} environments. Each element of the alist should
592 be a list containing two strings: the name of the option, and the
593 value. For example,
595 \(setq org-e-latex-minted-options
596 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
598 will result in src blocks being exported with
600 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
602 as the start of the minted environment. Note that the same
603 options will be applied to blocks of all languages."
604 :group 'org-export-e-latex
605 :type '(repeat
606 (list
607 (string :tag "Minted option name ")
608 (string :tag "Minted option value"))))
610 (defvar org-e-latex-custom-lang-environments nil
611 "Alist mapping languages to language-specific LaTeX environments.
613 It is used during export of src blocks by the listings and minted
614 latex packages. For example,
616 \(setq org-e-latex-custom-lang-environments
617 '\(\(python \"pythoncode\"\)\)\)
619 would have the effect that if org encounters begin_src python
620 during latex export it will output
622 \\begin{pythoncode}
623 <src block body>
624 \\end{pythoncode}")
627 ;;;; Plain text
629 (defcustom org-e-latex-quotes
630 '(("fr"
631 ("\\(\\s-\\|[[(]\\|^\\)\"" . "«~")
632 ("\\(\\S-\\)\"" . "~»")
633 ("\\(\\s-\\|(\\|^\\)'" . "'"))
634 ("en"
635 ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
636 ("\\(\\S-\\)\"" . "''")
637 ("\\(\\s-\\|(\\|^\\)'" . "`")))
638 "Alist for quotes to use when converting english double-quotes.
640 The CAR of each item in this alist is the language code.
641 The CDR of each item in this alist is a list of three CONS:
642 - the first CONS defines the opening quote;
643 - the second CONS defines the closing quote;
644 - the last CONS defines single quotes.
646 For each item in a CONS, the first string is a regexp
647 for allowed characters before/after the quote, the second
648 string defines the replacement string for this quote."
649 :group 'org-export-e-latex
650 :type '(list
651 (cons :tag "Opening quote"
652 (string :tag "Regexp for char before")
653 (string :tag "Replacement quote "))
654 (cons :tag "Closing quote"
655 (string :tag "Regexp for char after ")
656 (string :tag "Replacement quote "))
657 (cons :tag "Single quote"
658 (string :tag "Regexp for char before")
659 (string :tag "Replacement quote "))))
662 ;;;; Compilation
664 (defcustom org-e-latex-pdf-process
665 '("pdflatex -interaction nonstopmode -output-directory %o %f"
666 "pdflatex -interaction nonstopmode -output-directory %o %f"
667 "pdflatex -interaction nonstopmode -output-directory %o %f")
668 "Commands to process a LaTeX file to a PDF file.
669 This is a list of strings, each of them will be given to the
670 shell as a command. %f in the command will be replaced by the
671 full file name, %b by the file base name \(i.e. without
672 extension) and %o by the base directory of the file.
674 The reason why this is a list is that it usually takes several
675 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
676 does not have a clever mechanism to detect which of these
677 commands have to be run to get to a stable result, and it also
678 does not do any error checking.
680 By default, Org uses 3 runs of `pdflatex' to do the processing.
681 If you have texi2dvi on your system and if that does not cause
682 the infamous egrep/locale bug:
684 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
686 then `texi2dvi' is the superior choice. Org does offer it as one
687 of the customize options.
689 Alternatively, this may be a Lisp function that does the
690 processing, so you could use this to apply the machinery of
691 AUCTeX or the Emacs LaTeX mode. This function should accept the
692 file name as its single argument."
693 :group 'org-export-pdf
694 :type '(choice
695 (repeat :tag "Shell command sequence"
696 (string :tag "Shell command"))
697 (const :tag "2 runs of pdflatex"
698 ("pdflatex -interaction nonstopmode -output-directory %o %f"
699 "pdflatex -interaction nonstopmode -output-directory %o %f"))
700 (const :tag "3 runs of pdflatex"
701 ("pdflatex -interaction nonstopmode -output-directory %o %f"
702 "pdflatex -interaction nonstopmode -output-directory %o %f"
703 "pdflatex -interaction nonstopmode -output-directory %o %f"))
704 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
705 ("pdflatex -interaction nonstopmode -output-directory %o %f"
706 "bibtex %b"
707 "pdflatex -interaction nonstopmode -output-directory %o %f"
708 "pdflatex -interaction nonstopmode -output-directory %o %f"))
709 (const :tag "texi2dvi"
710 ("texi2dvi -p -b -c -V %f"))
711 (const :tag "rubber"
712 ("rubber -d --into %o %f"))
713 (function)))
715 (defcustom org-e-latex-logfiles-extensions
716 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
717 "The list of file extensions to consider as LaTeX logfiles."
718 :group 'org-export-e-latex
719 :type '(repeat (string :tag "Extension")))
721 (defcustom org-e-latex-remove-logfiles t
722 "Non-nil means remove the logfiles produced by PDF production.
723 These are the .aux, .log, .out, and .toc files."
724 :group 'org-export-e-latex
725 :type 'boolean)
729 ;;; Internal Functions
731 (defun org-e-latex--caption/label-string (caption label info)
732 "Return caption and label LaTeX string for floats.
734 CAPTION is a cons cell of secondary strings, the car being the
735 standard caption and the cdr its short form. LABEL is a string
736 representing the label. INFO is a plist holding contextual
737 information.
739 If there's no caption nor label, return the empty string.
741 For non-floats, see `org-e-latex--wrap-label'."
742 (let ((label-str (if label (format "\\label{%s}" label) "")))
743 (cond
744 ((and (not caption) (not label)) "")
745 ((not caption) (format "\\label{%s}\n" label))
746 ;; Option caption format with short name.
747 ((cdr caption)
748 (format "\\caption[%s]{%s%s}\n"
749 (org-export-secondary-string (cdr caption) 'e-latex info)
750 label-str
751 (org-export-secondary-string (car caption) 'e-latex info)))
752 ;; Standard caption format.
753 (t (format "\\caption{%s%s}\n"
754 label-str
755 (org-export-secondary-string (car caption) 'e-latex info))))))
757 (defun org-e-latex--guess-inputenc (header)
758 "Set the coding system in inputenc to what the buffer is.
760 HEADER is the LaTeX header string.
762 Return the new header."
763 (let* ((cs (or (ignore-errors
764 (latexenc-coding-system-to-inputenc
765 buffer-file-coding-system))
766 "utf8")))
767 (if (not cs) header
768 ;; First translate if that is requested.
769 (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
770 ;; Then find the \usepackage statement and replace the option.
771 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
772 cs header t nil 1))))
774 (defun org-e-latex--find-verb-separator (s)
775 "Return a character not used in string S.
776 This is used to choose a separator for constructs like \\verb."
777 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
778 (loop for c across ll
779 when (not (string-match (regexp-quote (char-to-string c)) s))
780 return (char-to-string c))))
782 (defun org-e-latex--make-option-string (options)
783 "Return a comma separated string of keywords and values.
784 OPTIONS is an alist where the key is the options keyword as
785 a string, and the value a list containing the keyword value, or
786 nil."
787 (mapconcat (lambda (pair)
788 (concat (first pair)
789 (when (> (length (second pair)) 0)
790 (concat "=" (second pair)))))
791 options
792 ","))
794 (defun org-e-latex--quotation-marks (text info)
795 "Export quotation marks depending on language conventions.
796 TEXT is a string containing quotation marks to be replaced. INFO
797 is a plist used as a communication channel."
798 (mapc (lambda(l)
799 (let ((start 0))
800 (while (setq start (string-match (car l) text start))
801 (let ((new-quote (concat (match-string 1 text) (cdr l))))
802 (setq text (replace-match new-quote t t text))))))
803 (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
804 ;; Falls back on English.
805 (assoc "en" org-e-latex-quotes))))
806 text)
808 (defun org-e-latex--wrap-label (element output)
809 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
810 This function shouldn't be used for floats. See
811 `org-e-latex--caption/label-string'."
812 (let ((label (org-element-property :name element)))
813 (if (or (not output) (not label) (string= output "") (string= label ""))
814 output
815 (concat (format "\\label{%s}\n" label) output))))
819 ;;; Template
821 (defun org-e-latex-template (contents info)
822 "Return complete document string after LaTeX conversion.
823 CONTENTS is the transcoded contents string. INFO is a plist
824 holding export options."
825 (let ((title (org-export-secondary-string
826 (plist-get info :title) 'e-latex info)))
827 (concat
828 ;; 1. Time-stamp.
829 (and (plist-get info :time-stamp-file)
830 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
831 ;; 2. Document class and packages.
832 (let ((class (plist-get info :latex-class))
833 (class-options (plist-get info :latex-class-options)))
834 (org-element-normalize-string
835 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
836 (document-class-string
837 (and (stringp header)
838 (if class-options
839 (replace-regexp-in-string
840 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
841 class-options header t nil 1)
842 header))))
843 (org-e-latex--guess-inputenc
844 (org-splice-latex-header
845 document-class-string
846 org-export-latex-default-packages-alist ; defined in org.el
847 org-export-latex-packages-alist nil ; defined in org.el
848 (plist-get info :latex-header-extra))))))
849 ;; 3. Define alert if not yet defined.
850 "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
851 ;; 4. Possibly limit depth for headline numbering.
852 (let ((sec-num (plist-get info :section-numbers)))
853 (when (integerp sec-num)
854 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
855 ;; 5. Author.
856 (let ((author (and (plist-get info :with-author)
857 (let ((auth (plist-get info :author)))
858 (and auth (org-export-secondary-string
859 auth 'e-latex info)))))
860 (email (and (plist-get info :with-email)
861 (org-export-secondary-string
862 (plist-get info :email) 'e-latex info))))
863 (cond ((and author email (not (string= "" email)))
864 (format "\\author{%s\\thanks{%s}}\n" author email))
865 (author (format "\\author{%s}\n" author))
866 (t "\\author{}\n")))
867 ;; 6. Date.
868 (let ((date (plist-get info :date)))
869 (and date (format "\\date{%s}\n" date)))
870 ;; 7. Title
871 (format "\\title{%s}\n" title)
872 ;; 8. Hyperref options.
873 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
874 (or (plist-get info :keywords) "")
875 (or (plist-get info :description) "")
876 (if (not (plist-get info :with-creator)) ""
877 (plist-get info :creator)))
878 ;; 9. Document start.
879 "\\begin{document}\n\n"
880 ;; 10. Title command.
881 (org-element-normalize-string
882 (cond ((string= "" title) nil)
883 ((not (stringp org-e-latex-title-command)) nil)
884 ((string-match "\\(?:[^%]\\|^\\)%s"
885 org-e-latex-title-command)
886 (format org-e-latex-title-command title))
887 (t org-e-latex-title-command)))
888 ;; 11. Table of contents.
889 (let ((depth (plist-get info :with-toc)))
890 (when depth
891 (concat (when (wholenump depth)
892 (format "\\setcounter{tocdepth}{%d}\n" depth))
893 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
894 ;; 12. Document's body.
895 contents
896 ;; 13. Creator.
897 (let ((creator-info (plist-get info :with-creator)))
898 (cond
899 ((not creator-info) "")
900 ((eq creator-info 'comment)
901 (format "%% %s\n" (plist-get info :creator)))
902 (t (concat (plist-get info :creator) "\n"))))
903 ;; 14. Document end.
904 "\\end{document}")))
908 ;;; Transcode Functions
910 ;;;; Block
912 (defun org-e-latex-center-block (center-block contents info)
913 "Transcode a CENTER-BLOCK element from Org to LaTeX.
914 CONTENTS holds the contents of the block. INFO is a plist
915 holding contextual information."
916 (org-e-latex--wrap-label
917 center-block
918 (format "\\begin{center}\n%s\\end{center}" contents)))
921 ;;;; Comment
923 ;; Comments are ignored.
926 ;;;; Comment Block
928 ;; Comment Blocks are ignored.
931 ;;;; Drawer
933 (defun org-e-latex-drawer (drawer contents info)
934 "Transcode a DRAWER element from Org to LaTeX.
935 CONTENTS holds the contents of the block. INFO is a plist
936 holding contextual information."
937 (let* ((name (org-element-property :drawer-name drawer))
938 (output (if (functionp org-e-latex-format-drawer-function)
939 (funcall org-e-latex-format-drawer-function
940 name contents)
941 ;; If there's no user defined function: simply
942 ;; display contents of the drawer.
943 contents)))
944 (org-e-latex--wrap-label drawer output)))
947 ;;;; Dynamic Block
949 (defun org-e-latex-dynamic-block (dynamic-block contents info)
950 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
951 CONTENTS holds the contents of the block. INFO is a plist
952 holding contextual information. See
953 `org-export-data'."
954 (org-e-latex--wrap-label dynamic-block contents))
957 ;;;; Emphasis
959 (defun org-e-latex-emphasis (emphasis contents info)
960 "Transcode EMPHASIS from Org to LaTeX.
961 CONTENTS is the contents of the emphasized text. INFO is a plist
962 holding contextual information.."
963 (format (cdr (assoc (org-element-property :marker emphasis)
964 org-e-latex-emphasis-alist))
965 contents))
968 ;;;; Entity
970 (defun org-e-latex-entity (entity contents info)
971 "Transcode an ENTITY object from Org to LaTeX.
972 CONTENTS are the definition itself. INFO is a plist holding
973 contextual information."
974 (let ((ent (org-element-property :latex entity)))
975 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
978 ;;;; Example Block
980 (defun org-e-latex-example-block (example-block contents info)
981 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
982 CONTENTS is nil. INFO is a plist holding contextual
983 information."
984 (org-e-latex--wrap-label
985 example-block
986 (format "\\begin{verbatim}\n%s\\end{verbatim}"
987 (org-export-format-code-default example-block info))))
990 ;;;; Export Snippet
992 (defun org-e-latex-export-snippet (export-snippet contents info)
993 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
994 CONTENTS is nil. INFO is a plist holding contextual information."
995 (when (eq (org-export-snippet-backend export-snippet) 'e-latex)
996 (org-element-property :value export-snippet)))
999 ;;;; Export Block
1001 (defun org-e-latex-export-block (export-block contents info)
1002 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1003 CONTENTS is nil. INFO is a plist holding contextual information."
1004 (when (string= (org-element-property :type export-block) "latex")
1005 (org-remove-indentation (org-element-property :value export-block))))
1008 ;;;; Fixed Width
1010 (defun org-e-latex-fixed-width (fixed-width contents info)
1011 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1012 CONTENTS is nil. INFO is a plist holding contextual information."
1013 (let* ((value (org-element-normalize-string
1014 (replace-regexp-in-string
1015 "^[ \t]*: ?" ""
1016 (org-element-property :value fixed-width)))))
1017 (org-e-latex--wrap-label
1018 fixed-width (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1021 ;;;; Footnote Definition
1023 ;; Footnote Definitions are ignored.
1026 ;;;; Footnote Reference
1028 (defun org-e-latex-footnote-reference (footnote-reference contents info)
1029 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1030 CONTENTS is nil. INFO is a plist holding contextual information."
1031 (concat
1032 ;; Insert separator between two footnotes in a row.
1033 (let ((prev (org-export-get-previous-element footnote-reference info)))
1034 (when (eq (org-element-type prev) 'footnote-reference)
1035 org-e-latex-footnote-separator))
1036 (cond
1037 ;; Use \footnotemark if the footnote has already been defined.
1038 ((not (org-export-footnote-first-reference-p footnote-reference info))
1039 (format "\\footnotemark[%s]{}"
1040 (org-export-get-footnote-number footnote-reference info)))
1041 ;; Use also \footnotemark if reference is within another footnote
1042 ;; reference or footnote definition.
1043 ((loop for parent in (org-export-get-genealogy footnote-reference info)
1044 thereis (memq (org-element-type parent)
1045 '(footnote-reference footnote-definition)))
1046 (let ((num (org-export-get-footnote-number footnote-reference info)))
1047 (format "\\footnotemark[%s]{}\\setcounter{footnote}{%s}" num num)))
1048 ;; Otherwise, define it with \footnote command.
1050 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1051 (unless (eq (org-element-type def) 'org-data)
1052 (setq def (cons 'org-data (cons nil def))))
1053 (concat
1054 (format "\\footnote{%s}" (org-trim (org-export-data def 'e-latex info)))
1055 ;; Retrieve all footnote references within the footnote and
1056 ;; add their definition after it, since LaTeX doesn't support
1057 ;; them inside.
1058 (let* (all-refs
1059 search-refs ; for byte-compiler
1060 (search-refs
1061 (function
1062 (lambda (data)
1063 ;; Return a list of all footnote references in DATA.
1064 (org-element-map
1065 data 'footnote-reference
1066 (lambda (ref)
1067 (when (org-export-footnote-first-reference-p ref info)
1068 (push ref all-refs)
1069 (when (eq (org-element-property :type ref) 'standard)
1070 (funcall
1071 search-refs
1072 (org-export-get-footnote-definition ref info)))))
1073 info) (reverse all-refs)))))
1074 (mapconcat
1075 (lambda (ref)
1076 (format
1077 "\\footnotetext[%s]{%s}"
1078 (org-export-get-footnote-number ref info)
1079 (org-trim
1080 (funcall
1081 (if (eq (org-element-property :type ref) 'inline)
1082 'org-export-secondary-string
1083 'org-export-data)
1084 (org-export-get-footnote-definition ref info) 'e-latex info))))
1085 (funcall search-refs def) ""))))))))
1088 ;;;; Headline
1090 (defun org-e-latex-headline (headline contents info)
1091 "Transcode an HEADLINE element from Org to LaTeX.
1092 CONTENTS holds the contents of the headline. INFO is a plist
1093 holding contextual information."
1094 (let* ((class (plist-get info :latex-class))
1095 (level (org-export-get-relative-level headline info))
1096 (numberedp (org-export-numbered-headline-p headline info))
1097 (class-sectionning (assoc class org-e-latex-classes))
1098 ;; Section formatting will set two placeholders: one for the
1099 ;; title and the other for the contents.
1100 (section-fmt
1101 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
1102 (fboundp (nth 2 class-sectionning)))
1103 (funcall (nth 2 class-sectionning) level numberedp)
1104 (nth (1+ level) class-sectionning))))
1105 (cond
1106 ;; No section available for that LEVEL.
1107 ((not sec) nil)
1108 ;; Section format directly returned by a function.
1109 ((stringp sec) sec)
1110 ;; (numbered-section . unnumbered-section)
1111 ((not (consp (cdr sec)))
1112 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1113 ;; (numbered-open numbered-close)
1114 ((= (length sec) 2)
1115 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1116 ;; (num-in num-out no-num-in no-num-out)
1117 ((= (length sec) 4)
1118 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1119 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1120 (text (org-export-secondary-string
1121 (org-element-property :title headline) 'e-latex info))
1122 (todo
1123 (and (plist-get info :with-todo-keywords)
1124 (let ((todo (org-element-property :todo-keyword headline)))
1125 (and todo (org-export-secondary-string todo 'e-latex info)))))
1126 (todo-type (and todo (org-element-property :todo-type headline)))
1127 (tags (and (plist-get info :with-tags)
1128 (org-element-property :tags headline)))
1129 (priority (and (plist-get info :with-priority)
1130 (org-element-property :priority headline)))
1131 ;; Create the headline text.
1132 (full-text (if (functionp org-e-latex-format-headline-function)
1133 ;; User-defined formatting function.
1134 (funcall org-e-latex-format-headline-function
1135 todo todo-type priority text tags)
1136 ;; Default formatting.
1137 (concat
1138 (when todo
1139 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1140 (when priority (format "\\framebox{\\#%c} " priority))
1141 text
1142 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1143 ;; Associate some \label to the headline for internal links.
1144 (headline-label
1145 (format "\\label{sec-%s}\n"
1146 (mapconcat 'number-to-string
1147 (org-export-get-headline-number headline info)
1148 "-")))
1149 (pre-blanks
1150 (make-string (org-element-property :pre-blank headline) 10)))
1151 (cond
1152 ;; Case 1: This is a footnote section: ignore it.
1153 ((org-element-property :footnote-section-p headline) nil)
1154 ;; Case 2. This is a deep sub-tree: export it as a list item.
1155 ;; Also export as items headlines for which no section
1156 ;; format has been found.
1157 ((or (not section-fmt) (org-export-low-level-p headline info))
1158 ;; Build the real contents of the sub-tree.
1159 (let ((low-level-body
1160 (concat
1161 ;; If the headline is the first sibling, start a list.
1162 (when (org-export-first-sibling-p headline info)
1163 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1164 ;; Itemize headline
1165 "\\item " full-text "\n" headline-label pre-blanks contents)))
1166 ;; If headline is not the last sibling simply return
1167 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1168 ;; blank line.
1169 (if (not (org-export-last-sibling-p headline info)) low-level-body
1170 (replace-regexp-in-string
1171 "[ \t\n]*\\'"
1172 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1173 low-level-body))))
1174 ;; Case 3. Standard headline. Export it as a section.
1175 (t (format section-fmt full-text
1176 (concat headline-label pre-blanks contents))))))
1179 ;;;; Horizontal Rule
1181 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1182 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1183 CONTENTS is nil. INFO is a plist holding contextual information."
1184 (let ((attr (mapconcat #'identity
1185 (org-element-property :attr_latex horizontal-rule)
1186 " ")))
1187 (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
1190 ;;;; Inline Babel Call
1192 ;; Inline Babel Calls are ignored.
1195 ;;;; Inline Src Block
1197 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1198 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1199 CONTENTS holds the contents of the item. INFO is a plist holding
1200 contextual information."
1201 (let* ((code (org-element-property :value inline-src-block))
1202 (separator (org-e-latex--find-verb-separator code)))
1203 (cond
1204 ;; Do not use a special package: transcode it verbatim.
1205 ((not org-e-latex-listings)
1206 (concat "\\verb" separator code separator))
1207 ;; Use minted package.
1208 ((eq org-e-latex-listings 'minted)
1209 (let* ((org-lang (org-element-property :language inline-src-block))
1210 (mint-lang (or (cadr (assq (intern org-lang)
1211 org-e-latex-minted-langs))
1212 org-lang))
1213 (options (org-e-latex--make-option-string
1214 org-e-latex-minted-options)))
1215 (concat (format "\\mint%s{%s}"
1216 (if (string= options "") "" (format "[%s]" options))
1217 mint-lang)
1218 separator code separator)))
1219 ;; Use listings package.
1221 ;; Maybe translate language's name.
1222 (let* ((org-lang (org-element-property :language inline-src-block))
1223 (lst-lang (or (cadr (assq (intern org-lang)
1224 org-e-latex-listings-langs))
1225 org-lang))
1226 (options (org-e-latex--make-option-string
1227 (append org-e-latex-listings-options
1228 `(("language" ,lst-lang))))))
1229 (concat (format "\\lstinline[%s]" options)
1230 separator code separator))))))
1233 ;;;; Inlinetask
1235 (defun org-e-latex-inlinetask (inlinetask contents info)
1236 "Transcode an INLINETASK element from Org to LaTeX.
1237 CONTENTS holds the contents of the block. INFO is a plist
1238 holding contextual information."
1239 (let ((title (org-export-secondary-string
1240 (org-element-property :title inlinetask) 'e-latex info))
1241 (todo (and (plist-get info :with-todo-keywords)
1242 (let ((todo (org-element-property
1243 :todo-keyword inlinetask)))
1244 (and todo
1245 (org-export-secondary-string todo 'e-latex info)))))
1246 (todo-type (org-element-property :todo-type inlinetask))
1247 (tags (and (plist-get info :with-tags)
1248 (org-element-property :tags inlinetask)))
1249 (priority (and (plist-get info :with-priority)
1250 (org-element-property :priority inlinetask))))
1251 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1252 ;; with appropriate arguments.
1253 (if (functionp org-e-latex-format-inlinetask-function)
1254 (funcall org-e-latex-format-inlinetask-function
1255 todo todo-type priority title tags contents)
1256 ;; Otherwise, use a default template.
1257 (org-e-latex--wrap-label
1258 inlinetask
1259 (let ((full-title
1260 (concat
1261 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1262 (when priority (format "\\framebox{\\#%c} " priority))
1263 title
1264 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1265 (format (concat "\\begin{center}\n"
1266 "\\fbox{\n"
1267 "\\begin{minipage}[c]{.6\\textwidth}\n"
1268 "%s\n\n"
1269 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1270 "%s"
1271 "\\end{minipage}\n"
1272 "}\n"
1273 "\\end{center}")
1274 full-title contents))))))
1277 ;;;; Item
1279 (defun org-e-latex-item (item contents info)
1280 "Transcode an ITEM element from Org to LaTeX.
1281 CONTENTS holds the contents of the item. INFO is a plist holding
1282 contextual information."
1283 ;; Grab `:level' from plain-list properties, which is always the
1284 ;; first element above current item.
1285 (let* ((level (org-element-property :level (org-export-get-parent item info)))
1286 (counter (let ((count (org-element-property :counter item)))
1287 (and count
1288 (< level 4)
1289 (format "\\setcounter{enum%s}{%s}\n"
1290 (nth level '("i" "ii" "iii" "iv"))
1291 (1- count)))))
1292 (checkbox (let ((checkbox (org-element-property :checkbox item)))
1293 (cond ((eq checkbox 'on) "$\\boxtimes$ ")
1294 ((eq checkbox 'off) "$\\Box$ ")
1295 ((eq checkbox 'trans) "$\\boxminus$ "))))
1296 (tag (let ((tag (org-element-property :tag item)))
1297 (and tag
1298 (format "[%s]" (org-export-secondary-string
1299 tag 'e-latex info))))))
1300 (concat counter "\\item" tag " " checkbox contents)))
1303 ;;;; Keyword
1305 (defun org-e-latex-keyword (keyword contents info)
1306 "Transcode a KEYWORD element from Org to LaTeX.
1307 CONTENTS is nil. INFO is a plist holding contextual information."
1308 (let ((key (org-element-property :key keyword))
1309 (value (org-element-property :value keyword)))
1310 (cond
1311 ((string= key "LATEX") value)
1312 ((string= key "INDEX") (format "\\index{%s}" value))
1313 ;; Invisible targets.
1314 ((string= key "TARGET") nil)
1315 ((string= key "TOC")
1316 (let ((value (downcase value)))
1317 (cond
1318 ((string-match "\\<headlines\\>" value)
1319 (let ((depth (or (and (string-match "[0-9]+" value)
1320 (string-to-number (match-string 0 value)))
1321 (plist-get info :with-toc))))
1322 (concat
1323 (when (wholenump depth)
1324 (format "\\setcounter{tocdepth}{%s}\n" depth))
1325 "\\tableofcontents")))
1326 ((string= "tables" value) "\\listoftables")
1327 ((string= "figures" value) "\\listoffigures")
1328 ((string= "listings" value)
1329 (cond
1330 ((eq org-e-latex-listings 'minted) "\\listoflistings")
1331 (org-e-latex-listings "\\lstlistoflistings")
1332 ;; At the moment, src blocks with a caption are wrapped
1333 ;; into a figure environment.
1334 (t "\\listoffigures")))))))))
1337 ;;;; Latex Environment
1339 (defun org-e-latex-latex-environment (latex-environment contents info)
1340 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1341 CONTENTS is nil. INFO is a plist holding contextual information."
1342 (let ((label (org-element-property :name latex-environment))
1343 (value (org-remove-indentation
1344 (org-element-property :value latex-environment))))
1345 (if (not (org-string-nw-p label)) value
1346 ;; Environment is labelled: label must be within the environment
1347 ;; (otherwise, a reference pointing to that element will count
1348 ;; the section instead).
1349 (with-temp-buffer
1350 (insert value)
1351 (goto-char (point-min))
1352 (forward-line)
1353 (insert (format "\\label{%s}\n" label))
1354 (buffer-string)))))
1357 ;;;; Latex Fragment
1359 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1360 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1361 CONTENTS is nil. INFO is a plist holding contextual information."
1362 (org-element-property :value latex-fragment))
1365 ;;;; Line Break
1367 (defun org-e-latex-line-break (line-break contents info)
1368 "Transcode a LINE-BREAK object from Org to LaTeX.
1369 CONTENTS is nil. INFO is a plist holding contextual information."
1370 "\\\\")
1373 ;;;; Link
1375 (defun org-e-latex-link--inline-image (link info)
1376 "Return LaTeX code for an inline image.
1377 LINK is the link pointing to the inline image. INFO is a plist
1378 used as a communication channel."
1379 (let* ((parent (org-export-get-parent-paragraph link info))
1380 (path (let ((raw-path (org-element-property :path link)))
1381 (if (not (file-name-absolute-p raw-path)) raw-path
1382 (expand-file-name raw-path))))
1383 (caption (org-e-latex--caption/label-string
1384 (org-element-property :caption parent)
1385 (org-element-property :name parent)
1386 info))
1387 ;; Retrieve latex attributes from the element around.
1388 (attr (let ((raw-attr
1389 (mapconcat #'identity
1390 (org-element-property :attr_latex parent)
1391 " ")))
1392 (unless (string= raw-attr "") raw-attr)))
1393 (disposition
1394 (cond
1395 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1396 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1397 ((or (and attr (string-match "\\<float\\>" attr))
1398 (not (string= caption "")))
1399 'float)))
1400 (placement
1401 (cond
1402 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1403 (org-match-string-no-properties 1 attr))
1404 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1405 ((eq disposition 'float)
1406 (concat "[" org-e-latex-default-figure-position "]"))
1407 (t ""))))
1408 ;; Now clear ATTR from any special keyword and set a default
1409 ;; value if nothing is left.
1410 (setq attr
1411 (if (not attr) ""
1412 (org-trim
1413 (replace-regexp-in-string
1414 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
1415 (setq attr (cond ((not (string= attr "")) attr)
1416 ((eq disposition 'float) "width=0.7\\textwidth")
1417 ((eq disposition 'wrap) "width=0.48\\textwidth")
1418 (t (or org-e-latex-image-default-option ""))))
1419 ;; Return proper string, depending on DISPOSITION.
1420 (case disposition
1421 (wrap (format "\\begin{wrapfigure}%s
1422 \\centering
1423 \\includegraphics[%s]{%s}
1424 %s\\end{wrapfigure}" placement attr path caption))
1425 (mulicolumn (format "\\begin{figure*}%s
1426 \\centering
1427 \\includegraphics[%s]{%s}
1428 %s\\end{figure*}" placement attr path caption))
1429 (float (format "\\begin{figure}%s
1430 \\centering
1431 \\includegraphics[%s]{%s}
1432 %s\\end{figure}" placement attr path caption))
1433 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1435 (defun org-e-latex-link (link desc info)
1436 "Transcode a LINK object from Org to LaTeX.
1438 DESC is the description part of the link, or the empty string.
1439 INFO is a plist holding contextual information. See
1440 `org-export-data'."
1441 (let* ((type (org-element-property :type link))
1442 (raw-path (org-element-property :path link))
1443 ;; Ensure DESC really exists, or set it to nil.
1444 (desc (and (not (string= desc "")) desc))
1445 (imagep (org-export-inline-image-p
1446 link org-e-latex-inline-image-rules))
1447 (path (cond
1448 ((member type '("http" "https" "ftp" "mailto"))
1449 (concat type ":" raw-path))
1450 ((string= type "file")
1451 (when (string-match "\\(.+\\)::.+" raw-path)
1452 (setq raw-path (match-string 1 raw-path)))
1453 (if (file-name-absolute-p raw-path)
1454 (concat "file://" (expand-file-name raw-path))
1455 ;; TODO: Not implemented yet. Concat also:
1456 ;; (org-export-directory :LaTeX info)
1457 (concat "file://" raw-path)))
1458 (t raw-path)))
1459 protocol)
1460 (cond
1461 ;; Image file.
1462 (imagep (org-e-latex-link--inline-image link info))
1463 ;; Radioed target: Target's name is obtained from original raw
1464 ;; link. Path is parsed and transcoded in order to have a proper
1465 ;; display of the contents.
1466 ((string= type "radio")
1467 (format "\\hyperref[%s]{%s}"
1468 (org-export-solidify-link-text path)
1469 (org-export-secondary-string
1470 (org-element-parse-secondary-string
1471 path (cdr (assq 'radio-target org-element-object-restrictions)))
1472 'e-latex info)))
1473 ;; Links pointing to an headline: Find destination and build
1474 ;; appropriate referencing command.
1475 ((member type '("custom-id" "fuzzy" "id"))
1476 (let ((destination (if (string= type "fuzzy")
1477 (org-export-resolve-fuzzy-link link info)
1478 (org-export-resolve-id-link link info))))
1479 (case (org-element-type destination)
1480 ;; Fuzzy link points nowhere.
1481 ('nil
1482 (format "\\texttt{%s}"
1483 (or desc
1484 (org-export-secondary-string
1485 (org-element-property :raw-link link)
1486 'e-latex info))))
1487 ;; Fuzzy link points to an invisible target.
1488 (keyword nil)
1489 ;; LINK points to an headline. If headlines are numbered
1490 ;; and the link has no description, display headline's
1491 ;; number. Otherwise, display description or headline's
1492 ;; title.
1493 (headline
1494 (let ((label
1495 (format "sec-%s"
1496 (mapconcat
1497 'number-to-string
1498 (org-export-get-headline-number destination info)
1499 "-"))))
1500 (if (and (plist-get info :section-numbers) (not desc))
1501 (format "\\ref{%s}" label)
1502 (format "\\hyperref[%s]{%s}" label
1503 (or desc
1504 (org-export-secondary-string
1505 (org-element-property :title destination)
1506 'e-latex info))))))
1507 ;; Fuzzy link points to a target. Do as above.
1508 (otherwise
1509 (let ((path (org-export-solidify-link-text path)))
1510 (if (not desc) (format "\\ref{%s}" path)
1511 (format "\\hyperref[%s]{%s}" path desc)))))))
1512 ;; Coderef: replace link with the reference name or the
1513 ;; equivalent line number.
1514 ((string= type "coderef")
1515 (format (org-export-get-coderef-format path desc)
1516 (org-export-resolve-coderef path info)))
1517 ;; Link type is handled by a special function.
1518 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1519 (funcall protocol (org-link-unescape path) desc 'latex))
1520 ;; External link with a description part.
1521 ((and path desc) (format "\\href{%s}{%s}" path desc))
1522 ;; External link without a description part.
1523 (path (format "\\url{%s}" path))
1524 ;; No path, only description. Try to do something useful.
1525 (t (format "\\texttt{%s}" desc)))))
1528 ;;;; Babel Call
1530 ;; Babel Calls are ignored.
1533 ;;;; Macro
1535 (defun org-e-latex-macro (macro contents info)
1536 "Transcode a MACRO element from Org to LaTeX.
1537 CONTENTS is nil. INFO is a plist holding contextual information."
1538 ;; Use available tools.
1539 (org-export-expand-macro macro info))
1542 ;;;; Paragraph
1544 (defun org-e-latex-paragraph (paragraph contents info)
1545 "Transcode a PARAGRAPH element from Org to LaTeX.
1546 CONTENTS is the contents of the paragraph, as a string. INFO is
1547 the plist used as a communication channel."
1548 contents)
1551 ;;;; Plain List
1553 (defun org-e-latex-plain-list (plain-list contents info)
1554 "Transcode a PLAIN-LIST element from Org to LaTeX.
1555 CONTENTS is the contents of the list. INFO is a plist holding
1556 contextual information."
1557 (let* ((type (org-element-property :type plain-list))
1558 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1559 "inparadesc" "asparadesc"))
1560 (paralist-regexp (concat
1561 "\\("
1562 (mapconcat 'identity paralist-types "\\|")
1563 "\\)"))
1564 (attr (mapconcat #'identity
1565 (org-element-property :attr_latex plain-list)
1566 " "))
1567 (latex-type (cond
1568 ((and attr
1569 (string-match
1570 (format "\\<%s\\>" paralist-regexp) attr))
1571 (match-string 1 attr))
1572 ((eq type 'ordered) "enumerate")
1573 ((eq type 'unordered) "itemize")
1574 ((eq type 'descriptive) "description"))))
1575 (org-e-latex--wrap-label
1576 plain-list
1577 (format "\\begin{%s}%s\n%s\\end{%s}"
1578 latex-type
1579 ;; Once special environment, if any, has been removed, the
1580 ;; rest of the attributes will be optional arguments.
1581 ;; They will be put inside square brackets if necessary.
1582 (let ((opt (replace-regexp-in-string
1583 (format " *%s *" paralist-regexp) "" attr)))
1584 (cond ((string= opt "") "")
1585 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1586 (t (format "[%s]" opt))))
1587 contents
1588 latex-type))))
1591 ;;;; Plain Text
1593 (defun org-e-latex-plain-text (text info)
1594 "Transcode a TEXT string from Org to LaTeX.
1595 TEXT is the string to transcode. INFO is a plist holding
1596 contextual information."
1597 ;; Protect %, #, &, $, ~, ^, _, { and }.
1598 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1599 (setq text
1600 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1601 ;; Protect \
1602 (setq text (replace-regexp-in-string
1603 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1604 "$\\backslash$" text nil t 1))
1605 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1606 (let ((case-fold-search nil)
1607 (start 0))
1608 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1609 (setq text (replace-match
1610 (format "\\%s{}" (match-string 1 text)) nil t text)
1611 start (match-end 0))))
1612 ;; Handle quotation marks
1613 (setq text (org-e-latex--quotation-marks text info))
1614 ;; Convert special strings.
1615 (when (plist-get info :with-special-strings)
1616 (while (string-match (regexp-quote "...") text)
1617 (setq text (replace-match "\\ldots{}" nil t text))))
1618 ;; Handle break preservation if required.
1619 (when (plist-get info :preserve-breaks)
1620 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1621 text)))
1622 ;; Return value.
1623 text)
1626 ;;;; Property Drawer
1628 (defun org-e-latex-property-drawer (property-drawer contents info)
1629 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1630 CONTENTS is nil. INFO is a plist holding contextual
1631 information."
1632 ;; The property drawer isn't exported but we want separating blank
1633 ;; lines nonetheless.
1637 ;;;; Quote Block
1639 (defun org-e-latex-quote-block (quote-block contents info)
1640 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1641 CONTENTS holds the contents of the block. INFO is a plist
1642 holding contextual information."
1643 (org-e-latex--wrap-label
1644 quote-block
1645 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1648 ;;;; Quote Section
1650 (defun org-e-latex-quote-section (quote-section contents info)
1651 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1652 CONTENTS is nil. INFO is a plist holding contextual information."
1653 (let ((value (org-remove-indentation
1654 (org-element-property :value quote-section))))
1655 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1658 ;;;; Section
1660 (defun org-e-latex-section (section contents info)
1661 "Transcode a SECTION element from Org to LaTeX.
1662 CONTENTS holds the contents of the section. INFO is a plist
1663 holding contextual information."
1664 contents)
1667 ;;;; Radio Target
1669 (defun org-e-latex-radio-target (radio-target text info)
1670 "Transcode a RADIO-TARGET object from Org to LaTeX.
1671 TEXT is the text of the target. INFO is a plist holding
1672 contextual information."
1673 (format "\\label{%s}%s"
1674 (org-export-solidify-link-text
1675 (org-element-property :value radio-target))
1676 text))
1679 ;;;; Special Block
1681 (defun org-e-latex-special-block (special-block contents info)
1682 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1683 CONTENTS holds the contents of the block. INFO is a plist
1684 holding contextual information."
1685 (let ((type (downcase (org-element-property :type special-block))))
1686 (org-e-latex--wrap-label
1687 special-block
1688 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
1691 ;;;; Src Block
1693 (defun org-e-latex-src-block (src-block contents info)
1694 "Transcode a SRC-BLOCK element from Org to LaTeX.
1695 CONTENTS holds the contents of the item. INFO is a plist holding
1696 contextual information."
1697 (let* ((lang (org-element-property :language src-block))
1698 (caption (org-element-property :caption src-block))
1699 (label (org-element-property :name src-block))
1700 (custom-env (and lang
1701 (cadr (assq (intern lang)
1702 org-e-latex-custom-lang-environments))))
1703 (num-start (case (org-element-property :number-lines src-block)
1704 (continued (org-export-get-loc src-block info))
1705 (new 0)))
1706 (retain-labels (org-element-property :retain-labels src-block)))
1707 (cond
1708 ;; Case 1. No source fontification.
1709 ((not org-e-latex-listings)
1710 (let ((caption-str (org-e-latex--caption/label-string caption label info))
1711 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
1712 (format
1713 (or float-env "%s")
1714 (concat caption-str
1715 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1716 (org-export-format-code-default src-block info))))))
1717 ;; Case 2. Custom environment.
1718 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
1719 custom-env
1720 (org-export-format-code-default src-block info)
1721 custom-env))
1722 ;; Case 3. Use minted package.
1723 ((eq org-e-latex-listings 'minted)
1724 (let ((float-env (when (or label caption)
1725 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1726 (org-e-latex--caption/label-string
1727 caption label info))))
1728 (body
1729 (format
1730 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1731 ;; Options.
1732 (org-e-latex--make-option-string
1733 (if (not num-start) org-e-latex-minted-options
1734 (append `(("linenos")
1735 ("firstnumber" ,(number-to-string (1+ num-start))))
1736 org-e-latex-minted-options)))
1737 ;; Language.
1738 (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
1739 ;; Source code.
1740 (let* ((code-info (org-export-unravel-code src-block))
1741 (max-width
1742 (apply 'max
1743 (mapcar 'length
1744 (org-split-string (car code-info) "\n")))))
1745 (org-export-format-code
1746 (car code-info)
1747 (lambda (loc num ref)
1748 (concat
1750 (when ref
1751 ;; Ensure references are flushed to the right,
1752 ;; separated with 6 spaces from the widest line
1753 ;; of code.
1754 (concat (make-string (+ (- max-width (length loc)) 6) ? )
1755 (format "(%s)" ref)))))
1756 nil (and retain-labels (cdr code-info)))))))
1757 ;; Return value.
1758 (if float-env (format float-env body) body)))
1759 ;; Case 4. Use listings package.
1761 (let ((lst-lang
1762 (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
1763 (caption-str
1764 (when caption
1765 (let ((main (org-export-secondary-string
1766 (car caption) 'e-latex info)))
1767 (if (not (cdr caption)) (format "{%s}" main)
1768 (format
1769 "{[%s]%s}"
1770 (org-export-secondary-string (cdr caption) 'e-latex info)
1771 main))))))
1772 (concat
1773 ;; Options.
1774 (format "\\lstset{%s}\n"
1775 (org-e-latex--make-option-string
1776 (append org-e-latex-listings-options
1777 `(("language" ,lst-lang))
1778 (when label `(("label" ,label)))
1779 (when caption-str `(("caption" ,caption-str)))
1780 (cond ((not num-start) '(("numbers" "none")))
1781 ((zerop num-start) '(("numbers" "left")))
1782 (t `(("numbers" "left")
1783 ("firstnumber"
1784 ,(number-to-string (1+ num-start)))))))))
1785 ;; Source code.
1786 (format
1787 "\\begin{lstlisting}\n%s\\end{lstlisting}"
1788 (let* ((code-info (org-export-unravel-code src-block))
1789 (max-width
1790 (apply 'max
1791 (mapcar 'length
1792 (org-split-string (car code-info) "\n")))))
1793 (org-export-format-code
1794 (car code-info)
1795 (lambda (loc num ref)
1796 (concat
1798 (when ref
1799 ;; Ensure references are flushed to the right,
1800 ;; separated with 6 spaces from the widest line of
1801 ;; code
1802 (concat (make-string (+ (- max-width (length loc)) 6) ? )
1803 (format "(%s)" ref)))))
1804 nil (and retain-labels (cdr code-info)))))))))))
1807 ;;;; Statistics Cookie
1809 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
1810 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1811 CONTENTS is nil. INFO is a plist holding contextual information."
1812 (org-element-property :value statistics-cookie))
1815 ;;;; Subscript
1817 (defun org-e-latex-subscript (subscript contents info)
1818 "Transcode a SUBSCRIPT object from Org to LaTeX.
1819 CONTENTS is the contents of the object. INFO is a plist holding
1820 contextual information."
1821 (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
1824 ;;;; Superscript
1826 (defun org-e-latex-superscript (superscript contents info)
1827 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1828 CONTENTS is the contents of the object. INFO is a plist holding
1829 contextual information."
1830 (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
1833 ;;;; Table
1835 (defun org-e-latex-table--format-string (table info)
1836 "Return an appropriate format string for TABLE.
1838 TABLE-INFO is the plist containing format info about the table,
1839 as returned by `org-export-table-format-info'. INFO is a plist
1840 used as a communication channel.
1842 The format string leaves one placeholder for the body of the
1843 table."
1844 (let* ((label (org-element-property :name table))
1845 (caption (org-e-latex--caption/label-string
1846 (org-element-property :caption table) label info))
1847 (attr (mapconcat 'identity
1848 (org-element-property :attr_latex table)
1849 " "))
1850 ;; Determine alignment string.
1851 (alignment (org-e-latex-table--align-string table info))
1852 ;; Determine environment for the table: longtable, tabular...
1853 (table-env (cond
1854 ((not attr) org-e-latex-default-table-environment)
1855 ((string-match "\\<longtable\\>" attr) "longtable")
1856 ((string-match "\\<tabular.?\\>" attr)
1857 (org-match-string-no-properties 0 attr))
1858 (t org-e-latex-default-table-environment)))
1859 ;; If table is a float, determine environment: table, table*
1860 ;; or sidewaystable.
1861 (float-env (cond
1862 ((string= "longtable" table-env) nil)
1863 ((and attr (string-match "\\<sidewaystable\\>" attr))
1864 "sidewaystables")
1865 ((and attr
1866 (or (string-match (regexp-quote "table*") attr)
1867 (string-match "\\<multicolumn\\>" attr)))
1868 "table*")
1869 ((or (not (string= caption "")) label) "table")))
1870 ;; Extract others display options.
1871 (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
1872 (org-match-string-no-properties 1 attr)))
1873 (placement
1874 (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1875 (org-match-string-no-properties 1 attr)
1876 (format "[%s]" org-e-latex-default-figure-position))))
1877 ;; Prepare the final format string for the table.
1878 (cond
1879 ;; Longtable.
1880 ((string= "longtable" table-env)
1881 (format
1882 "\\begin{longtable}{%s}\n%s%%s%s\\end{longtable}"
1883 alignment
1884 (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
1885 (concat (org-trim caption) "\\\\\n"))
1886 (if (or org-e-latex-table-caption-above (string= "" caption)) ""
1887 (concat (org-trim caption) "\\\\\n"))))
1888 ;; Others.
1889 (t (concat (when float-env
1890 (concat
1891 (format "\\begin{%s}%s\n" float-env placement)
1892 (if org-e-latex-table-caption-above caption "")))
1893 (when org-e-latex-tables-centered "\\begin{center}\n")
1894 (format "\\begin{%s}%s{%s}\n%%s\\end{%s}"
1895 table-env
1896 (if width (format "{%s}" width) "") alignment table-env)
1897 (when org-e-latex-tables-centered "\n\\end{center}")
1898 (when float-env
1899 (concat (if org-e-latex-table-caption-above "" caption)
1900 (format "\n\\end{%s}" float-env))))))))
1902 (defun org-e-latex-table--align-string (table info)
1903 "Return an appropriate LaTeX alignment string.
1904 TABLE is the considered table. INFO is a plist used as
1905 a communication channel."
1906 (let ((attr (mapconcat 'identity
1907 (org-element-property :attr_latex table)
1908 " ")))
1909 (if (and attr (string-match "\\<align=\\(\\S-+\\)" attr))
1910 (match-string 1 attr)
1911 (let (alignment)
1912 ;; Extract column groups and alignment from first (non-rule)
1913 ;; row.
1914 (org-element-map
1915 (org-element-map table 'table-row 'identity info 'first-match)
1916 'table-cell
1917 (lambda (cell)
1918 (let ((borders (org-export-table-cell-borders cell info)))
1919 ;; Check left border for the first cell only.
1920 (when (and (memq 'left borders) (not alignment))
1921 (push "|" alignment))
1922 (push (case (org-export-table-cell-alignment cell info)
1923 (left "l")
1924 (right "r")
1925 (center "c"))
1926 alignment)
1927 (when (memq 'right borders) (push "|" alignment))))
1928 info)
1929 (apply 'concat (reverse alignment))))))
1931 (defun org-e-latex-table (table contents info)
1932 "Transcode a TABLE element from Org to LaTeX.
1933 CONTENTS is nil. INFO is a plist holding contextual information."
1934 (cond
1935 ;; Case 1: verbatim table.
1936 ((or org-e-latex-tables-verbatim
1937 (let ((attr (mapconcat 'identity
1938 (org-element-property :attr_latex table)
1939 " ")))
1940 (and attr (string-match "\\<verbatim\\>" attr))))
1941 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
1942 ;; Re-create table, without affiliated keywords.
1943 (org-trim
1944 (org-element-interpret-data
1945 `(org-data nil (table nil ,@(org-element-contents table)))))))
1946 ;; Case 2: table.el table. Convert it using appropriate tools.
1947 ((eq (org-element-property :type table) 'table.el)
1948 (require 'table)
1949 ;; Ensure "*org-export-table*" buffer is empty.
1950 (with-current-buffer (get-buffer-create "*org-export-table*")
1951 (erase-buffer))
1952 (let ((output (with-temp-buffer
1953 (insert (org-element-property :value table))
1954 (goto-char 1)
1955 (re-search-forward "^[ \t]*|[^|]" nil t)
1956 (table-generate-source 'latex "*org-export-table*")
1957 (with-current-buffer "*org-export-table*"
1958 (org-trim (buffer-string))))))
1959 (kill-buffer (get-buffer "*org-export-table*"))
1960 ;; Remove left out comments.
1961 (while (string-match "^%.*\n" output)
1962 (setq output (replace-match "" t t output)))
1963 ;; When the "rmlines" attribute is provided, remove all hlines
1964 ;; but the the one separating heading from the table body.
1965 (let ((attr (mapconcat 'identity
1966 (org-element-property :attr_latex table)
1967 " ")))
1968 (when (and attr (string-match "\\<rmlines\\>" attr))
1969 (let ((n 0) (pos 0))
1970 (while (and (< (length output) pos)
1971 (setq pos (string-match "^\\\\hline\n?" output pos)))
1972 (incf n)
1973 (unless (= n 2)
1974 (setq output (replace-match "" nil nil output)))))))
1975 (if (not org-e-latex-tables-centered) output
1976 (format "\\begin{center}\n%s\n\\end{center}" output))))
1977 ;; Case 3: Standard table.
1978 (t (format (org-e-latex-table--format-string table info) contents))))
1981 ;;;; Table Cell
1983 (defun org-e-latex-table-cell (table-cell contents info)
1984 "Transcode a TABLE-CELL element from Org to LaTeX.
1985 CONTENTS is the cell contents. INFO is a plist used as
1986 a communication channel."
1987 (concat (if (and contents
1988 org-e-latex-table-scientific-notation
1989 (string-match orgtbl-exp-regexp contents))
1990 ;; Use appropriate format string for scientific
1991 ;; notation.
1992 (format org-e-latex-table-scientific-notation
1993 (match-string 1 contents)
1994 (match-string 2 contents))
1995 contents)
1996 (when (org-export-get-next-element table-cell info) " & ")))
1999 ;;;; Table Row
2001 (defun org-e-latex-table-row (table-row contents info)
2002 "Transcode a TABLE-ROW element from Org to LaTeX.
2003 CONTENTS is the contents of the row. INFO is a plist used as
2004 a communication channel."
2005 ;; Rules are ignored since table separators are deduced from
2006 ;; borders of the current row.
2007 (when (eq (org-element-property :type table-row) 'standard)
2008 (let* ((attr (mapconcat 'identity
2009 (org-element-property
2010 :attr_latex (org-export-get-parent table-row info))
2011 " "))
2012 (longtablep (and attr (string-match "\\<longtable\\>" attr)))
2013 (booktabsp
2014 (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
2015 org-e-latex-tables-booktabs))
2016 ;; TABLE-ROW's borders are extracted from its first cell.
2017 (borders
2018 (org-export-table-cell-borders
2019 (car (org-element-contents table-row)) info)))
2020 (concat
2021 ;; When BOOKTABS are activated enforce top-rule even when no
2022 ;; hline was specifically marked.
2023 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2024 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2025 contents "\\\\\n"
2026 (cond
2027 ;; Special case for long tables. Define header and footers.
2028 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2029 (format "%s
2030 \\endhead
2031 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2032 \\endfoot
2033 \\endlastfoot"
2034 (if booktabsp "\\midrule" "\\hline")
2035 (if booktabsp "\\midrule" "\\hline")
2036 ;; Number of columns.
2037 (cdr (org-export-table-dimensions
2038 (org-export-get-parent-table table-row info) info))))
2039 ;; When BOOKTABS are activated enforce bottom rule even when
2040 ;; no hline was specifically marked.
2041 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2042 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2043 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2046 ;;;; Target
2048 (defun org-e-latex-target (target contents info)
2049 "Transcode a TARGET object from Org to LaTeX.
2050 CONTENTS is nil. INFO is a plist holding contextual
2051 information."
2052 (format "\\label{%s}"
2053 (org-export-solidify-link-text (org-element-property :value target))))
2056 ;;;; Time-stamp
2058 (defun org-e-latex-time-stamp (time-stamp contents info)
2059 "Transcode a TIME-STAMP object from Org to LaTeX.
2060 CONTENTS is nil. INFO is a plist holding contextual
2061 information."
2062 (let ((value (org-element-property :value time-stamp))
2063 (type (org-element-property :type time-stamp))
2064 (appt-type (org-element-property :appt-type time-stamp)))
2065 (concat (cond ((eq appt-type 'scheduled)
2066 (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
2067 ((eq appt-type 'deadline)
2068 (format "\\textbf{\\textsc{%s}} " org-deadline-string))
2069 ((eq appt-type 'closed)
2070 (format "\\textbf{\\textsc{%s}} " org-closed-string)))
2071 (cond ((memq type '(active active-range))
2072 (format org-e-latex-active-timestamp-format value))
2073 ((memq type '(inactive inactive-range))
2074 (format org-e-latex-inactive-timestamp-format value))
2076 (format org-e-latex-diary-timestamp-format value))))))
2079 ;;;; Verbatim
2081 (defun org-e-latex-verbatim (verbatim contents info)
2082 "Transcode a VERBATIM object from Org to LaTeX.
2083 CONTENTS is nil. INFO is a plist used as a communication
2084 channel."
2085 (let ((fmt (cdr (assoc (org-element-property :marker verbatim)
2086 org-e-latex-emphasis-alist)))
2087 (value (org-element-property :value verbatim)))
2088 (cond
2089 ;; Handle the `verb' special case.
2090 ((eq 'verb fmt)
2091 (let ((separator (org-e-latex--find-verb-separator value)))
2092 (concat "\\verb" separator value separator)))
2093 ;; Handle the `protectedtexttt' special case.
2094 ((eq 'protectedtexttt fmt)
2095 (let ((start 0)
2096 (trans '(("\\" . "\\textbackslash{}")
2097 ("~" . "\\textasciitilde{}")
2098 ("^" . "\\textasciicircum{}")))
2099 (rtn "")
2100 char)
2101 (while (string-match "[\\{}$%&_#~^]" value)
2102 (setq char (match-string 0 value))
2103 (if (> (match-beginning 0) 0)
2104 (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
2105 (setq value (substring value (1+ (match-beginning 0))))
2106 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
2107 rtn (concat rtn char)))
2108 (setq value (concat rtn value)
2109 fmt "\\texttt{%s}")
2110 (while (string-match "--" value)
2111 (setq value (replace-match "-{}-" t t value)))
2112 (format fmt value)))
2113 ;; Else use format string.
2114 (t (format fmt value)))))
2117 ;;;; Verse Block
2119 (defun org-e-latex-verse-block (verse-block contents info)
2120 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2121 CONTENTS is verse block contents. INFO is a plist holding
2122 contextual information."
2123 (org-e-latex--wrap-label
2124 verse-block
2125 ;; In a verse environment, add a line break to each newline
2126 ;; character and change each white space at beginning of a line
2127 ;; into a space of 1 em. Also change each blank line with
2128 ;; a vertical space of 1 em.
2129 (progn
2130 (setq contents (replace-regexp-in-string
2131 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2132 (replace-regexp-in-string
2133 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2134 (while (string-match "^[ \t]+" contents)
2135 (let ((new-str (format "\\hspace*{%dem}"
2136 (length (match-string 0 contents)))))
2137 (setq contents (replace-match new-str nil t contents))))
2138 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2142 ;;; Interactive functions
2144 (defun org-e-latex-export-to-latex
2145 (&optional subtreep visible-only body-only ext-plist pub-dir)
2146 "Export current buffer to a LaTeX file.
2148 If narrowing is active in the current buffer, only export its
2149 narrowed part.
2151 If a region is active, export that region.
2153 When optional argument SUBTREEP is non-nil, export the sub-tree
2154 at point, extracting information from the headline properties
2155 first.
2157 When optional argument VISIBLE-ONLY is non-nil, don't export
2158 contents of hidden elements.
2160 When optional argument BODY-ONLY is non-nil, only write code
2161 between \"\\begin{document}\" and \"\\end{document}\".
2163 EXT-PLIST, when provided, is a property list with external
2164 parameters overriding Org default settings, but still inferior to
2165 file-local settings.
2167 When optional argument PUB-DIR is set, use it as the publishing
2168 directory.
2170 Return output file's name."
2171 (interactive)
2172 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
2173 (org-export-to-file
2174 'e-latex outfile subtreep visible-only body-only ext-plist)))
2176 (defun org-e-latex-export-to-pdf
2177 (&optional subtreep visible-only body-only ext-plist pub-dir)
2178 "Export current buffer to LaTeX then process through to PDF.
2180 If narrowing is active in the current buffer, only export its
2181 narrowed part.
2183 If a region is active, export that region.
2185 When optional argument SUBTREEP is non-nil, export the sub-tree
2186 at point, extracting information from the headline properties
2187 first.
2189 When optional argument VISIBLE-ONLY is non-nil, don't export
2190 contents of hidden elements.
2192 When optional argument BODY-ONLY is non-nil, only write code
2193 between \"\\begin{document}\" and \"\\end{document}\".
2195 EXT-PLIST, when provided, is a property list with external
2196 parameters overriding Org default settings, but still inferior to
2197 file-local settings.
2199 When optional argument PUB-DIR is set, use it as the publishing
2200 directory.
2202 Return PDF file's name."
2203 (interactive)
2204 (org-e-latex-compile
2205 (org-e-latex-export-to-latex
2206 subtreep visible-only body-only ext-plist pub-dir)))
2208 (defun org-e-latex-compile (texfile)
2209 "Compile a TeX file.
2211 TEXFILE is the name of the file being compiled. Processing is
2212 done through the command specified in `org-e-latex-pdf-process'.
2214 Return PDF file name or an error if it couldn't be produced."
2215 (let* ((wconfig (current-window-configuration))
2216 (texfile (file-truename texfile))
2217 (base (file-name-sans-extension texfile))
2218 errors)
2219 (message (format "Processing LaTeX file %s ..." texfile))
2220 (unwind-protect
2221 (progn
2222 (cond
2223 ;; A function is provided: Apply it.
2224 ((functionp org-e-latex-pdf-process)
2225 (funcall org-e-latex-pdf-process (shell-quote-argument texfile)))
2226 ;; A list is provided: Replace %b, %f and %o with appropriate
2227 ;; values in each command before applying it. Output is
2228 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2229 ((consp org-e-latex-pdf-process)
2230 (let* ((out-dir (or (file-name-directory texfile) "./"))
2231 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2232 (mapc
2233 (lambda (command)
2234 (shell-command
2235 (replace-regexp-in-string
2236 "%b" (shell-quote-argument base)
2237 (replace-regexp-in-string
2238 "%f" (shell-quote-argument texfile)
2239 (replace-regexp-in-string
2240 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2241 outbuf))
2242 org-e-latex-pdf-process)
2243 ;; Collect standard errors from output buffer.
2244 (setq errors (org-e-latex-collect-errors outbuf))))
2245 (t (error "No valid command to process to PDF")))
2246 (let ((pdffile (concat base ".pdf")))
2247 ;; Check for process failure. Provide collected errors if
2248 ;; possible.
2249 (if (not (file-exists-p pdffile))
2250 (error (concat (format "PDF file %s wasn't produced" pdffile)
2251 (when errors (concat ": " errors))))
2252 ;; Else remove log files, when specified, and signal end of
2253 ;; process to user, along with any error encountered.
2254 (when org-e-latex-remove-logfiles
2255 (dolist (ext org-e-latex-logfiles-extensions)
2256 (let ((file (concat base "." ext)))
2257 (when (file-exists-p file) (delete-file file)))))
2258 (message (concat "Process completed"
2259 (if (not errors) "."
2260 (concat " with errors: " errors)))))
2261 ;; Return output file name.
2262 pdffile))
2263 (set-window-configuration wconfig))))
2265 (defun org-e-latex-collect-errors (buffer)
2266 "Collect some kind of errors from \"pdflatex\" command output.
2268 BUFFER is the buffer containing output.
2270 Return collected error types as a string, or nil if there was
2271 none."
2272 (with-current-buffer buffer
2273 (save-excursion
2274 (goto-char (point-max))
2275 ;; Find final "pdflatex" run.
2276 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
2277 (let ((case-fold-search t)
2278 (errors ""))
2279 (when (save-excursion
2280 (re-search-forward "Reference.*?undefined" nil t))
2281 (setq errors (concat errors " [undefined reference]")))
2282 (when (save-excursion
2283 (re-search-forward "Citation.*?undefined" nil t))
2284 (setq errors (concat errors " [undefined citation]")))
2285 (when (save-excursion
2286 (re-search-forward "Undefined control sequence" nil t))
2287 (setq errors (concat errors " [undefined control sequence]")))
2288 (when (save-excursion
2289 (re-search-forward "^! LaTeX.*?Error" nil t))
2290 (setq errors (concat errors " [LaTeX error]")))
2291 (when (save-excursion
2292 (re-search-forward "^! Package.*?Error" nil t))
2293 (setq errors (concat errors " [package error]")))
2294 (and (org-string-nw-p errors) (org-trim errors)))))))
2297 (provide 'org-e-latex)
2298 ;;; org-e-latex.el ends here