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