Improve converting from ODT to other formats
[org-mode/org-mode-NeilSmithlineMods.git] / EXPERIMENTAL / org-e-latex.el
blob8e92ee0b97a55deb6bc4281e945c38484bb9cee1
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-get-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)
49 (declare-function org-export-clean-table "org-export" (table specialp))
50 (declare-function org-export-data "org-export" (data backend info))
51 (declare-function org-export-directory "org-export" (type plist))
52 (declare-function org-export-expand-macro "org-export" (macro info))
53 (declare-function org-export-first-sibling-p "org-export" (headline info))
54 (declare-function org-export-footnote-first-reference-p "org-export"
55 (footnote-reference info))
56 (declare-function org-export-get-coderef-format "org-export" (path desc))
57 (declare-function org-export-get-footnote-definition "org-export"
58 (footnote-reference info))
59 (declare-function org-export-get-footnote-number "org-export" (footnote info))
60 (declare-function org-export-get-previous-element "org-export" (blob info))
61 (declare-function org-export-get-relative-level "org-export" (headline info))
62 (declare-function org-export-handle-code
63 "org-export" (element info &optional num-fmt ref-fmt delayed))
64 (declare-function org-export-included-file "org-export" (keyword backend info))
65 (declare-function org-export-inline-image-p "org-export"
66 (link &optional extensions))
67 (declare-function org-export-last-sibling-p "org-export" (headline info))
68 (declare-function org-export-low-level-p "org-export" (headline info))
69 (declare-function org-export-output-file-name
70 "org-export" (extension &optional subtreep pub-dir))
71 (declare-function org-export-resolve-coderef "org-export" (ref info))
72 (declare-function org-export-resolve-fuzzy-link "org-export" (link info))
73 (declare-function org-export-secondary-string "org-export"
74 (secondary backend info))
75 (declare-function org-export-solidify-link-text "org-export" (s))
76 (declare-function org-export-table-format-info "org-export" (table))
77 (declare-function
78 org-export-to-buffer "org-export"
79 (backend buffer &optional subtreep visible-only body-only ext-plist))
80 (declare-function
81 org-export-to-file "org-export"
82 (backend file &optional subtreep visible-only body-only ext-plist))
86 ;;; Internal Variables
88 (defconst org-e-latex-option-alist
89 '((:date "DATE" nil org-e-latex-date-format t)
90 (:latex-class "LATEX_CLASS" nil org-e-latex-default-class t)
91 (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
92 (:latex-header-extra "LATEX_HEADER" nil nil newline))
93 "Alist between LaTeX export properties and ways to set them.
94 See `org-export-option-alist' for more information on the
95 structure of the value.")
99 ;;; User Configurable Variables
101 (defgroup org-export-e-latex nil
102 "Options for exporting Org mode files to LaTeX."
103 :tag "Org Export LaTeX"
104 :group 'org-export)
107 ;;;; Preamble
109 (defcustom org-e-latex-default-class "article"
110 "The default LaTeX class."
111 :group 'org-export-e-latex
112 :type '(string :tag "LaTeX class"))
114 (defcustom org-e-latex-classes
115 '(("article"
116 "\\documentclass[11pt]{article}"
117 ("\\section{%s}" . "\\section*{%s}")
118 ("\\subsection{%s}" . "\\subsection*{%s}")
119 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
120 ("\\paragraph{%s}" . "\\paragraph*{%s}")
121 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
122 ("report"
123 "\\documentclass[11pt]{report}"
124 ("\\part{%s}" . "\\part*{%s}")
125 ("\\chapter{%s}" . "\\chapter*{%s}")
126 ("\\section{%s}" . "\\section*{%s}")
127 ("\\subsection{%s}" . "\\subsection*{%s}")
128 ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
129 ("book"
130 "\\documentclass[11pt]{book}"
131 ("\\part{%s}" . "\\part*{%s}")
132 ("\\chapter{%s}" . "\\chapter*{%s}")
133 ("\\section{%s}" . "\\section*{%s}")
134 ("\\subsection{%s}" . "\\subsection*{%s}")
135 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
136 "Alist of LaTeX classes and associated header and structure.
137 If #+LaTeX_CLASS is set in the buffer, use its value and the
138 associated information. Here is the structure of each cell:
140 \(class-name
141 header-string
142 \(numbered-section . unnumbered-section\)
143 ...\)
145 The header string
146 -----------------
148 The HEADER-STRING is the header that will be inserted into the
149 LaTeX file. It should contain the \\documentclass macro, and
150 anything else that is needed for this setup. To this header, the
151 following commands will be added:
153 - Calls to \\usepackage for all packages mentioned in the
154 variables `org-export-latex-default-packages-alist' and
155 `org-export-latex-packages-alist'. Thus, your header
156 definitions should avoid to also request these packages.
158 - Lines specified via \"#+LaTeX_HEADER:\"
160 If you need more control about the sequence in which the header
161 is built up, or if you want to exclude one of these building
162 blocks for a particular class, you can use the following
163 macro-like placeholders.
165 [DEFAULT-PACKAGES] \\usepackage statements for default packages
166 [NO-DEFAULT-PACKAGES] do not include any of the default packages
167 [PACKAGES] \\usepackage statements for packages
168 [NO-PACKAGES] do not include the packages
169 [EXTRA] the stuff from #+LaTeX_HEADER
170 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
171 [BEAMER-HEADER-EXTRA] the beamer extra headers
173 So a header like
175 \\documentclass{article}
176 [NO-DEFAULT-PACKAGES]
177 [EXTRA]
178 \\providecommand{\\alert}[1]{\\textbf{#1}}
179 [PACKAGES]
181 will omit the default packages, and will include the
182 #+LaTeX_HEADER lines, then have a call to \\providecommand, and
183 then place \\usepackage commands based on the content of
184 `org-export-latex-packages-alist'.
186 If your header, `org-export-latex-default-packages-alist' or
187 `org-export-latex-packages-alist' inserts
188 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
189 replaced with a coding system derived from
190 `buffer-file-coding-system'. See also the variable
191 `org-e-latex-inputenc-alist' for a way to influence this
192 mechanism.
194 The sectioning structure
195 ------------------------
197 The sectioning structure of the class is given by the elements
198 following the header string. For each sectioning level, a number
199 of strings is specified. A %s formatter is mandatory in each
200 section string and will be replaced by the title of the section.
202 Instead of a cons cell \(numbered . unnumbered\), you can also
203 provide a list of 2 or 4 elements,
205 \(numbered-open numbered-close\)
209 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
211 providing opening and closing strings for a LaTeX environment
212 that should represent the document section. The opening clause
213 should have a %s to represent the section title.
215 Instead of a list of sectioning commands, you can also specify
216 a function name. That function will be called with two
217 parameters, the \(reduced) level of the headline, and a predicate
218 non-nil when the headline should be numbered. It must return
219 a format string in which the section title will be added."
220 :group 'org-export-e-latex
221 :type '(repeat
222 (list (string :tag "LaTeX class")
223 (string :tag "LaTeX header")
224 (repeat :tag "Levels" :inline t
225 (choice
226 (cons :tag "Heading"
227 (string :tag " numbered")
228 (string :tag "unnumbered"))
229 (list :tag "Environment"
230 (string :tag "Opening (numbered)")
231 (string :tag "Closing (numbered)")
232 (string :tag "Opening (unnumbered)")
233 (string :tag "Closing (unnumbered)"))
234 (function :tag "Hook computing sectioning"))))))
236 (defcustom org-e-latex-inputenc-alist nil
237 "Alist of inputenc coding system names, and what should really be used.
238 For example, adding an entry
240 (\"utf8\" . \"utf8x\")
242 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
243 are written as utf8 files."
244 :group 'org-export-e-latex
245 :type '(repeat
246 (cons
247 (string :tag "Derived from buffer")
248 (string :tag "Use this instead"))))
250 (defcustom org-e-latex-date-format
251 "\\today"
252 "Format string for \\date{...}."
253 :group 'org-export-e-latex
254 :type 'boolean)
256 (defcustom org-e-latex-title-command "\\maketitle"
257 "The command used to insert the title just after \\begin{document}.
258 If this string contains the formatting specification \"%s\" then
259 it will be used as a formatting string, passing the title as an
260 argument."
261 :group 'org-export-e-latex
262 :type 'string)
265 ;;;; Headline
267 (defcustom org-e-latex-format-headline-function nil
268 "Function to format headline text.
270 This function will be called with 5 arguments:
271 TODO the todo keyword \(string or nil\).
272 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
273 PRIORITY the priority of the headline \(integer or nil\)
274 TEXT the main headline text \(string\).
275 TAGS the tags string, separated with colons \(string or nil\).
277 The function result will be used in the section format string.
279 As an example, one could set the variable to the following, in
280 order to reproduce the default set-up:
282 \(defun org-e-latex-format-headline \(todo todo-type priority text tags\)
283 \"Default format function for an headline.\"
284 \(concat \(when todo
285 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
286 \(when priority
287 \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
288 text
289 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
290 :group 'org-export-e-latex
291 :type 'function)
294 ;;;; Emphasis
296 (defcustom org-e-latex-emphasis-alist
297 '(("*" . "\\textbf{%s}")
298 ("/" . "\\emph{%s}")
299 ("_" . "\\underline{%s}")
300 ("+" . "\\st{%s}")
301 ("=" . protectedtexttt)
302 ("~" . verb))
303 "Alist of LaTeX expressions to convert emphasis fontifiers.
305 The key is the character used as a marker for fontification. The
306 value is a formatting string to wrap fontified text with.
308 Value can also be set to the following symbols: `verb' and
309 `protectedtexttt'. For the former, Org will use \"\\verb\" to
310 create a format string and select a delimiter character that
311 isn't in the string. For the latter, Org will use \"\\texttt\"
312 to typeset and try to protect special characters."
313 :group 'org-export-e-latex
314 :type 'alist)
317 ;;;; Footnotes
319 (defcustom org-e-latex-footnote-separator "\\textsuperscript{,}\\,"
320 "Text used to separate footnotes."
321 :group 'org-export-e-latex
322 :type 'string)
325 ;;;; Time-stamps
327 (defcustom org-e-latex-active-timestamp-format "\\textit{%s}"
328 "A printf format string to be applied to active time-stamps."
329 :group 'org-export-e-latex
330 :type 'string)
332 (defcustom org-e-latex-inactive-timestamp-format "\\textit{%s}"
333 "A printf format string to be applied to inactive time-stamps."
334 :group 'org-export-e-latex
335 :type 'string)
337 (defcustom org-e-latex-diary-timestamp-format "\\textit{%s}"
338 "A printf format string to be applied to diary time-stamps."
339 :group 'org-export-e-latex
340 :type 'string)
343 ;;;; Links
345 (defcustom org-e-latex-image-default-option "width=.9\\linewidth"
346 "Default option for images."
347 :group 'org-export-e-latex
348 :type 'string)
350 (defcustom org-e-latex-default-figure-position "htb"
351 "Default position for latex figures."
352 :group 'org-export-e-latex
353 :type 'string)
355 (defcustom org-e-latex-inline-image-extensions
356 '("pdf" "jpeg" "jpg" "png" "ps" "eps")
357 "Extensions of image files that can be inlined into LaTeX.
359 Note that the image extension *actually* allowed depend on the
360 way the LaTeX file is processed. When used with pdflatex, pdf,
361 jpg and png images are OK. When processing through dvi to
362 Postscript, only ps and eps are allowed. The default we use here
363 encompasses both."
364 :group 'org-export-e-latex
365 :type '(repeat (string :tag "Extension")))
368 ;;;; Tables
370 (defcustom org-e-latex-default-table-environment "tabular"
371 "Default environment used to build tables."
372 :group 'org-export-e-latex
373 :type 'string)
375 (defcustom org-e-latex-tables-centered t
376 "When non-nil, tables are exported in a center environment."
377 :group 'org-export-e-latex
378 :type 'boolean)
380 (defcustom org-e-latex-tables-verbatim nil
381 "When non-nil, tables are exported verbatim."
382 :group 'org-export-e-latex
383 :type 'boolean)
385 (defcustom org-e-latex-tables-booktabs nil
386 "When non-nil, display tables in a formal \"booktabs\" style.
387 This option assumes that the \"booktabs\" package is properly
388 loaded in the header of the document. This value can be ignored
389 locally with \"booktabs=yes\" and \"booktabs=no\" LaTeX
390 attributes."
391 :group 'org-export-e-latex
392 :type 'boolean)
394 (defcustom org-e-latex-table-caption-above t
395 "When non-nil, place caption string at the beginning of the table.
396 Otherwise, place it near the end."
397 :group 'org-export-e-latex
398 :type 'boolean)
401 ;;;; Drawers
403 (defcustom org-e-latex-format-drawer-function nil
404 "Function called to format a drawer in LaTeX code.
406 The function must accept two parameters:
407 NAME the drawer name, like \"LOGBOOK\"
408 CONTENTS the contents of the drawer.
410 The function should return the string to be exported.
412 For example, the variable could be set to the following function
413 in order to mimic default behaviour:
415 \(defun org-e-latex-format-drawer-default \(name contents\)
416 \"Format a drawer element for LaTeX export.\"
417 contents\)"
418 :group 'org-export-e-latex
419 :type 'function)
422 ;;;; Inlinetasks
424 (defcustom org-e-latex-format-inlinetask-function nil
425 "Function called to format an inlinetask in LaTeX code.
427 The function must accept six parameters:
428 TODO the todo keyword, as a string
429 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
430 PRIORITY the inlinetask priority, as a string
431 NAME the inlinetask name, as a string.
432 TAGS the inlinetask tags, as a string.
433 CONTENTS the contents of the inlinetask, as a string.
435 The function should return the string to be exported.
437 For example, the variable could be set to the following function
438 in order to mimic default behaviour:
440 \(defun org-e-latex-format-inlinetask \(todo type priority name tags contents\)
441 \"Format an inline task element for LaTeX export.\"
442 \(let \(\(full-title
443 \(concat
444 \(when todo
445 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
446 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
447 title
448 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
449 \(format \(concat \"\\\\begin{center}\\n\"
450 \"\\\\fbox{\\n\"
451 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
452 \"%s\\n\\n\"
453 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
454 \"%s\"
455 \"\\\\end{minipage}}\"
456 \"\\\\end{center}\"\)
457 full-title contents\)\)"
458 :group 'org-export-e-latex
459 :type 'function)
462 ;; Src blocks
464 (defcustom org-e-latex-listings nil
465 "Non-nil means export source code using the listings package.
466 This package will fontify source code, possibly even with color.
467 If you want to use this, you also need to make LaTeX use the
468 listings package, and if you want to have color, the color
469 package. Just add these to `org-export-latex-packages-alist',
470 for example using customize, or with something like:
472 \(require 'org-e-latex)
473 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"listings\"))
474 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"color\"))
476 Alternatively,
478 \(setq org-e-latex-listings 'minted)
480 causes source code to be exported using the minted package as
481 opposed to listings. If you want to use minted, you need to add
482 the minted package to `org-export-latex-packages-alist', for
483 example using customize, or with
485 \(require 'org-e-latex)
486 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"minted\"))
488 In addition, it is necessary to install pygments
489 \(http://pygments.org), and to configure the variable
490 `org-e-latex-pdf-process' so that the -shell-escape option is
491 passed to pdflatex."
492 :group 'org-export-e-latex
493 :type '(choice
494 (const :tag "Use listings" t)
495 (const :tag "Use minted" 'minted)
496 (const :tag "Export verbatim" nil)))
498 (defcustom org-e-latex-listings-langs
499 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
500 (c "C") (cc "C++")
501 (fortran "fortran")
502 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
503 (html "HTML") (xml "XML")
504 (tex "TeX") (latex "TeX")
505 (shell-script "bash")
506 (gnuplot "Gnuplot")
507 (ocaml "Caml") (caml "Caml")
508 (sql "SQL") (sqlite "sql"))
509 "Alist mapping languages to their listing language counterpart.
510 The key is a symbol, the major mode symbol without the \"-mode\".
511 The value is the string that should be inserted as the language
512 parameter for the listings package. If the mode name and the
513 listings name are the same, the language does not need an entry
514 in this list - but it does not hurt if it is present."
515 :group 'org-export-e-latex
516 :type '(repeat
517 (list
518 (symbol :tag "Major mode ")
519 (string :tag "Listings language"))))
521 (defcustom org-e-latex-listings-options nil
522 "Association list of options for the latex listings package.
524 These options are supplied as a comma-separated list to the
525 \\lstset command. Each element of the association list should be
526 a list containing two strings: the name of the option, and the
527 value. For example,
529 (setq org-e-latex-listings-options
530 '((\"basicstyle\" \"\\small\")
531 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
533 will typeset the code in a small size font with underlined, bold
534 black keywords.
536 Note that the same options will be applied to blocks of all
537 languages."
538 :group 'org-export-e-latex
539 :type '(repeat
540 (list
541 (string :tag "Listings option name ")
542 (string :tag "Listings option value"))))
544 (defcustom org-e-latex-minted-langs
545 '((emacs-lisp "common-lisp")
546 (cc "c++")
547 (cperl "perl")
548 (shell-script "bash")
549 (caml "ocaml"))
550 "Alist mapping languages to their minted language counterpart.
551 The key is a symbol, the major mode symbol without the \"-mode\".
552 The value is the string that should be inserted as the language
553 parameter for the minted package. If the mode name and the
554 listings name are the same, the language does not need an entry
555 in this list - but it does not hurt if it is present.
557 Note that minted uses all lower case for language identifiers,
558 and that the full list of language identifiers can be obtained
559 with:
561 pygmentize -L lexers"
562 :group 'org-export-e-latex
563 :type '(repeat
564 (list
565 (symbol :tag "Major mode ")
566 (string :tag "Minted language"))))
568 (defcustom org-e-latex-minted-options nil
569 "Association list of options for the latex minted package.
571 These options are supplied within square brackets in
572 \\begin{minted} environments. Each element of the alist should
573 be a list containing two strings: the name of the option, and the
574 value. For example,
576 \(setq org-e-latex-minted-options
577 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
579 will result in src blocks being exported with
581 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
583 as the start of the minted environment. Note that the same
584 options will be applied to blocks of all languages."
585 :group 'org-export-e-latex
586 :type '(repeat
587 (list
588 (string :tag "Minted option name ")
589 (string :tag "Minted option value"))))
591 (defvar org-e-latex-custom-lang-environments nil
592 "Alist mapping languages to language-specific LaTeX environments.
594 It is used during export of src blocks by the listings and minted
595 latex packages. For example,
597 \(setq org-e-latex-custom-lang-environments
598 '\(\(python \"pythoncode\"\)\)\)
600 would have the effect that if org encounters begin_src python
601 during latex export it will output
603 \\begin{pythoncode}
604 <src block body>
605 \\end{pythoncode}")
608 ;;;; Plain text
610 (defcustom org-e-latex-quotes
611 '(("fr" ("\\(\\s-\\|[[(]\\)\"" . "«~") ("\\(\\S-\\)\"" . "~»") ("\\(\\s-\\|(\\)'" . "'"))
612 ("en" ("\\(\\s-\\|[[(]\\)\"" . "``") ("\\(\\S-\\)\"" . "''") ("\\(\\s-\\|(\\)'" . "`")))
613 "Alist for quotes to use when converting english double-quotes.
615 The CAR of each item in this alist is the language code.
616 The CDR of each item in this alist is a list of three CONS:
617 - the first CONS defines the opening quote;
618 - the second CONS defines the closing quote;
619 - the last CONS defines single quotes.
621 For each item in a CONS, the first string is a regexp
622 for allowed characters before/after the quote, the second
623 string defines the replacement string for this quote."
624 :group 'org-export-e-latex
625 :type '(list
626 (cons :tag "Opening quote"
627 (string :tag "Regexp for char before")
628 (string :tag "Replacement quote "))
629 (cons :tag "Closing quote"
630 (string :tag "Regexp for char after ")
631 (string :tag "Replacement quote "))
632 (cons :tag "Single quote"
633 (string :tag "Regexp for char before")
634 (string :tag "Replacement quote "))))
637 ;;;; Compilation
639 (defcustom org-e-latex-pdf-process
640 '("pdflatex -interaction nonstopmode -output-directory %o %f"
641 "pdflatex -interaction nonstopmode -output-directory %o %f"
642 "pdflatex -interaction nonstopmode -output-directory %o %f")
643 "Commands to process a LaTeX file to a PDF file.
644 This is a list of strings, each of them will be given to the
645 shell as a command. %f in the command will be replaced by the
646 full file name, %b by the file base name \(i.e. without
647 extension) and %o by the base directory of the file.
649 The reason why this is a list is that it usually takes several
650 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
651 does not have a clever mechanism to detect which of these
652 commands have to be run to get to a stable result, and it also
653 does not do any error checking.
655 By default, Org uses 3 runs of `pdflatex' to do the processing.
656 If you have texi2dvi on your system and if that does not cause
657 the infamous egrep/locale bug:
659 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
661 then `texi2dvi' is the superior choice. Org does offer it as one
662 of the customize options.
664 Alternatively, this may be a Lisp function that does the
665 processing, so you could use this to apply the machinery of
666 AUCTeX or the Emacs LaTeX mode. This function should accept the
667 file name as its single argument."
668 :group 'org-export-pdf
669 :type '(choice
670 (repeat :tag "Shell command sequence"
671 (string :tag "Shell command"))
672 (const :tag "2 runs of pdflatex"
673 ("pdflatex -interaction nonstopmode -output-directory %o %f"
674 "pdflatex -interaction nonstopmode -output-directory %o %f"))
675 (const :tag "3 runs of pdflatex"
676 ("pdflatex -interaction nonstopmode -output-directory %o %f"
677 "pdflatex -interaction nonstopmode -output-directory %o %f"
678 "pdflatex -interaction nonstopmode -output-directory %o %f"))
679 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
680 ("pdflatex -interaction nonstopmode -output-directory %o %f"
681 "bibtex %b"
682 "pdflatex -interaction nonstopmode -output-directory %o %f"
683 "pdflatex -interaction nonstopmode -output-directory %o %f"))
684 (const :tag "texi2dvi"
685 ("texi2dvi -p -b -c -V %f"))
686 (const :tag "rubber"
687 ("rubber -d --into %o %f"))
688 (function)))
690 (defcustom org-e-latex-logfiles-extensions
691 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
692 "The list of file extensions to consider as LaTeX logfiles."
693 :group 'org-export-e-latex
694 :type '(repeat (string :tag "Extension")))
696 (defcustom org-e-latex-remove-logfiles t
697 "Non-nil means remove the logfiles produced by PDF production.
698 These are the .aux, .log, .out, and .toc files."
699 :group 'org-export-e-latex
700 :type 'boolean)
704 ;;; Internal Functions
706 (defun org-e-latex--caption/label-string (caption label info)
707 "Return caption and label LaTeX string for floats.
709 CAPTION is a cons cell of secondary strings, the car being the
710 standard caption and the cdr its short form. LABEL is a string
711 representing the label. INFO is a plist holding contextual
712 information.
714 If there's no caption nor label, return the empty string.
716 For non-floats, see `org-e-latex--wrap-label'."
717 (let ((label-str (if label (format "\\label{%s}" label) "")))
718 (cond
719 ((and (not caption) (not label)) "")
720 ((not caption) (format "\\label{%s}\n" label))
721 ;; Option caption format with short name.
722 ((cdr caption)
723 (format "\\caption[%s]{%s%s}\n"
724 (org-export-secondary-string (cdr caption) 'e-latex info)
725 label-str
726 (org-export-secondary-string (car caption) 'e-latex info)))
727 ;; Standard caption format.
728 (t (format "\\caption{%s%s}\n"
729 label-str
730 (org-export-secondary-string (car caption) 'e-latex info))))))
732 (defun org-e-latex--guess-inputenc (header)
733 "Set the coding system in inputenc to what the buffer is.
735 HEADER is the LaTeX header string.
737 Return the new header."
738 (let* ((cs (or (ignore-errors
739 (latexenc-coding-system-to-inputenc
740 buffer-file-coding-system))
741 "utf8")))
742 (if (not cs)
743 header
744 ;; First translate if that is requested.
745 (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
746 ;; Then find the \usepackage statement and replace the option.
747 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
748 cs header t nil 1))))
750 (defun org-e-latex--find-verb-separator (s)
751 "Return a character not used in string S.
752 This is used to choose a separator for constructs like \\verb."
753 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
754 (loop for c across ll
755 when (not (string-match (regexp-quote (char-to-string c)) s))
756 return (char-to-string c))))
758 (defun org-e-latex--make-option-string (options)
759 "Return a comma separated string of keywords and values.
760 OPTIONS is an alist where the key is the options keyword as
761 a string, and the value a list containing the keyword value, or
762 nil."
763 (mapconcat (lambda (pair)
764 (concat (first pair)
765 (when (> (length (second pair)) 0)
766 (concat "=" (second pair)))))
767 options
768 ","))
770 (defun org-e-latex--quotation-marks (text info)
771 "Export quotation marks depending on language conventions.
772 TEXT is a string containing quotation marks to be replaced. INFO
773 is a plist used as a communication channel."
774 (mapc (lambda(l)
775 (let ((start 0))
776 (while (setq start (string-match (car l) text start))
777 (let ((new-quote (concat (match-string 1 text) (cdr l))))
778 (setq text (replace-match new-quote t t text))))))
779 (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
780 ;; Falls back on English.
781 (assoc "en" org-e-latex-quotes))))
782 text)
784 (defun org-e-latex--wrap-label (element output)
785 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
786 This function shouldn't be used for floats. See
787 `org-e-latex--caption/label-string'."
788 (let ((label (org-element-get-property :name element)))
789 (if (or (not output) (not label) (string= output "") (string= label ""))
790 output
791 (concat (format "\\label{%s}\n" label) output))))
795 ;;; Template
797 (defun org-e-latex-template (contents info)
798 "Return complete document string after LaTeX conversion.
799 CONTENTS is the transcoded contents string. INFO is a plist
800 holding export options."
801 (let ((title (org-export-secondary-string
802 (plist-get info :title) 'e-latex info)))
803 (concat
804 ;; 1. Time-stamp.
805 (and (plist-get info :time-stamp-file)
806 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
807 ;; 2. Document class and packages.
808 (let ((class (plist-get info :latex-class))
809 (class-options (plist-get info :latex-class-options)))
810 (org-element-normalize-string
811 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
812 (document-class-string
813 (and (stringp header)
814 (if class-options
815 (replace-regexp-in-string
816 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
817 class-options header t nil 1)
818 header))))
819 (org-e-latex--guess-inputenc
820 (org-splice-latex-header
821 document-class-string
822 org-export-latex-default-packages-alist ; defined in org.el
823 org-export-latex-packages-alist nil ; defined in org.el
824 (plist-get info :latex-header-extra))))))
825 ;; 3. Define alert if not yet defined.
826 "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
827 ;; 4. Possibly limit depth for headline numbering.
828 (let ((sec-num (plist-get info :section-numbers)))
829 (when (integerp sec-num)
830 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
831 ;; 5. Author.
832 (let ((author (and (plist-get info :with-author)
833 (let ((auth (plist-get info :author)))
834 (and auth (org-export-secondary-string
835 auth 'e-latex info)))))
836 (email (and (plist-get info :with-email)
837 (org-export-secondary-string
838 (plist-get info :email) 'e-latex info))))
839 (cond ((and author email (not (string= "" email)))
840 (format "\\author{%s\\thanks{%s}}\n" author email))
841 (author (format "\\author{%s}\n" author))
842 (t "\\author{}\n")))
843 ;; 6. Date.
844 (let ((date (plist-get info :date)))
845 (and date (format "\\date{%s}\n" date)))
846 ;; 7. Title
847 (format "\\title{%s}\n" title)
848 ;; 8. Hyperref options.
849 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
850 (or (plist-get info :keywords) "")
851 (or (plist-get info :description) "")
852 (if (not (plist-get info :with-creator)) ""
853 (plist-get info :creator)))
854 ;; 9. Document start.
855 "\\begin{document}\n\n"
856 ;; 10. Title command.
857 (org-element-normalize-string
858 (cond ((string= "" title) nil)
859 ((not (stringp org-e-latex-title-command)) nil)
860 ((string-match "\\(?:[^%]\\|^\\)%s"
861 org-e-latex-title-command)
862 (format org-e-latex-title-command title))
863 (t org-e-latex-title-command)))
864 ;; 11. Table of contents.
865 (let ((depth (plist-get info :with-toc)))
866 (when depth
867 (concat (when (wholenump depth)
868 (format "\\setcounter{tocdepth}{%d}\n" depth))
869 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
870 ;; 12. Document's body.
871 contents
872 ;; 13. Creator.
873 (let ((creator-info (plist-get info :with-creator)))
874 (cond
875 ((not creator-info) "")
876 ((eq creator-info 'comment)
877 (format "%% %s\n" (plist-get info :creator)))
878 (t (concat (plist-get info :creator) "\n"))))
879 ;; 14. Document end.
880 "\\end{document}")))
884 ;;; Transcode Functions
886 ;;;; Block
888 (defun org-e-latex-center-block (center-block contents info)
889 "Transcode a CENTER-BLOCK element from Org to LaTeX.
890 CONTENTS holds the contents of the block. INFO is a plist
891 holding contextual information."
892 (org-e-latex--wrap-label
893 center-block
894 (format "\\begin{center}\n%s\\end{center}" contents)))
897 ;;;; Comment
899 ;; Comments are ignored.
902 ;;;; Comment Block
904 ;; Comment Blocks are ignored.
907 ;;;; Drawer
909 (defun org-e-latex-drawer (drawer contents info)
910 "Transcode a DRAWER element from Org to LaTeX.
911 CONTENTS holds the contents of the block. INFO is a plist
912 holding contextual information."
913 (let* ((name (org-element-get-property :drawer-name drawer))
914 (output (if (functionp org-e-latex-format-drawer-function)
915 (funcall org-e-latex-format-drawer-function
916 name contents)
917 ;; If there's no user defined function: simply
918 ;; display contents of the drawer.
919 contents)))
920 (org-e-latex--wrap-label drawer output)))
923 ;;;; Dynamic Block
925 (defun org-e-latex-dynamic-block (dynamic-block contents info)
926 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
927 CONTENTS holds the contents of the block. INFO is a plist
928 holding contextual information. See
929 `org-export-data'."
930 (org-e-latex--wrap-label dynamic-block contents))
933 ;;;; Emphasis
935 (defun org-e-latex-emphasis (emphasis contents info)
936 "Transcode EMPHASIS from Org to LaTeX.
937 CONTENTS is the contents of the emphasized text. INFO is a plist
938 holding contextual information.."
939 (format (cdr (assoc (org-element-get-property :marker emphasis)
940 org-e-latex-emphasis-alist))
941 contents))
944 ;;;; Entity
946 (defun org-e-latex-entity (entity contents info)
947 "Transcode an ENTITY object from Org to LaTeX.
948 CONTENTS are the definition itself. INFO is a plist holding
949 contextual information."
950 (let ((ent (org-element-get-property :latex entity)))
951 (if (org-element-get-property :latex-math-p entity)
952 (format "$%s$" ent)
953 ent)))
956 ;;;; Example Block
958 (defun org-e-latex-example-block (example-block contents info)
959 "Transcode a EXAMPLE-BLOCK element from Org to LaTeX.
960 CONTENTS is nil. INFO is a plist holding contextual information."
961 (let* ((options (or (org-element-get-property :options example-block) ""))
962 (value (org-export-handle-code example-block info)))
963 (org-e-latex--wrap-label
964 example-block (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
967 ;;;; Export Snippet
969 (defun org-e-latex-export-snippet (export-snippet contents info)
970 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
971 CONTENTS is nil. INFO is a plist holding contextual information."
972 (org-element-get-property :value export-snippet))
975 ;;;; Export Block
977 (defun org-e-latex-export-block (export-block contents info)
978 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
979 CONTENTS is nil. INFO is a plist holding contextual information."
980 (when (string= (org-element-get-property :type export-block) "latex")
981 (org-remove-indentation (org-element-get-property :value export-block))))
984 ;;;; Fixed Width
986 (defun org-e-latex-fixed-width (fixed-width contents info)
987 "Transcode a FIXED-WIDTH element from Org to LaTeX.
988 CONTENTS is nil. INFO is a plist holding contextual information."
989 (let* ((value (org-element-normalize-string
990 (replace-regexp-in-string
991 "^[ \t]*: ?" ""
992 (org-element-get-property :value fixed-width)))))
993 (org-e-latex--wrap-label
994 fixed-width (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
997 ;;;; Footnote Definition
999 ;; Footnote Definitions are ignored.
1002 ;;;; Footnote Reference
1004 (defun org-e-latex-footnote-reference (footnote-reference contents info)
1005 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1006 CONTENTS is nil. INFO is a plist holding contextual information."
1007 (concat
1008 ;; Insert separator between two footnotes in a row.
1009 (let ((prev (org-export-get-previous-element footnote-reference info)))
1010 (when (and (listp prev) (eq (car prev) 'footnote-reference))
1011 org-e-latex-footnote-separator))
1012 ;; Use \footnotemark if the footnote has already been defined.
1013 ;; Otherwise, define it with \footnote command.
1014 (cond
1015 ((not (org-export-footnote-first-reference-p footnote-reference info))
1016 (format "\\footnotemark[%s]"
1017 (org-export-get-footnote-number footnote-reference info)))
1018 ;; Inline definitions are secondary strings.
1019 ((eq (org-element-get-property :type footnote-reference) 'inline)
1020 (format "\\footnote{%s}"
1021 (org-trim
1022 (org-export-secondary-string
1023 (org-export-get-footnote-definition footnote-reference info)
1024 'e-latex info))))
1025 ;; Non-inline footnotes definitions are full Org data.
1027 (format "\\footnote{%s}"
1028 (org-trim
1029 (org-export-data
1030 (org-export-get-footnote-definition footnote-reference info)
1031 'e-latex info)))))))
1034 ;;;; Headline
1036 (defun org-e-latex-headline (headline contents info)
1037 "Transcode an HEADLINE element from Org to LaTeX.
1038 CONTENTS holds the contents of the headline. INFO is a plist
1039 holding contextual information."
1040 (let* ((class (plist-get info :latex-class))
1041 (numberedp (plist-get info :section-numbers))
1042 ;; Get level relative to current parsed data.
1043 (level (org-export-get-relative-level headline info))
1044 (class-sectionning (assoc class org-e-latex-classes))
1045 ;; Section formatting will set two placeholders: one for the
1046 ;; title and the other for the contents.
1047 (section-fmt
1048 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
1049 (fboundp (nth 2 class-sectionning)))
1050 (funcall (nth 2 class-sectionning) level numberedp)
1051 (nth (1+ level) class-sectionning))))
1052 (cond
1053 ;; No section available for that LEVEL.
1054 ((not sec) nil)
1055 ;; Section format directly returned by a function.
1056 ((stringp sec) sec)
1057 ;; (numbered-section . unnumbered-section)
1058 ((not (consp (cdr sec)))
1059 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1060 ;; (numbered-open numbered-close)
1061 ((= (length sec) 2)
1062 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1063 ;; (num-in num-out no-num-in no-num-out)
1064 ((= (length sec) 4)
1065 (if numberedp
1066 (concat (car sec) "\n%s" (nth 1 sec))
1067 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1068 (text (org-export-secondary-string
1069 (org-element-get-property :title headline) 'e-latex info))
1070 (todo (and (plist-get info :with-todo-keywords)
1071 (let ((todo (org-element-get-property
1072 :todo-keyword headline)))
1073 (and todo
1074 (org-export-secondary-string todo 'e-latex info)))))
1075 (todo-type (and todo (org-element-get-property :todo-type headline)))
1076 (tags (and (plist-get info :with-tags)
1077 (org-element-get-property :tags headline)))
1078 (priority (and (plist-get info :with-priority)
1079 (org-element-get-property :priority headline)))
1080 ;; Create the headline text.
1081 (full-text (if (functionp org-e-latex-format-headline-function)
1082 ;; User-defined formatting function.
1083 (funcall org-e-latex-format-headline-function
1084 todo todo-type priority text tags)
1085 ;; Default formatting.
1086 (concat
1087 (when todo
1088 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1089 (when priority (format "\\framebox{\\#%c} " priority))
1090 text
1091 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1092 ;; Associate some \label to the headline for internal links.
1093 (headline-label
1094 (format "\\label{sec-%s}\n"
1095 (mapconcat 'number-to-string
1096 (org-export-get-headline-number headline info)
1097 "-")))
1098 (pre-blanks (make-string
1099 (org-element-get-property :pre-blank headline) 10)))
1100 (cond
1101 ;; Case 1: This is a footnote section: ignore it.
1102 ((org-element-get-property :footnote-section-p headline) nil)
1103 ;; Case 2. This is a deep sub-tree: export it as a list item.
1104 ;; Also export as items headlines for which no section
1105 ;; format has been found.
1106 ((or (not section-fmt) (org-export-low-level-p headline info))
1107 ;; Build the real contents of the sub-tree.
1108 (let ((low-level-body
1109 (concat
1110 ;; If the headline is the first sibling, start a list.
1111 (when (org-export-first-sibling-p headline info)
1112 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1113 ;; Itemize headline
1114 "\\item " full-text "\n" headline-label pre-blanks contents)))
1115 ;; If headline in the last sibling, close the list, before any
1116 ;; blank line. Otherwise, simply return LOW-LEVEL-BODY.
1117 (if (org-export-last-sibling-p headline info)
1118 (replace-regexp-in-string
1119 "[ \t\n]*\\'"
1120 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1121 low-level-body)
1122 low-level-body)))
1123 ;; Case 3. Standard headline. Export it as a section.
1124 (t (format section-fmt full-text
1125 (concat headline-label pre-blanks contents))))))
1128 ;;;; Horizontal Rule
1130 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1131 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1132 CONTENTS is nil. INFO is a plist holding contextual information."
1133 (let ((attr (mapconcat #'identity
1134 (org-element-get-property :attr_latex horizontal-rule)
1135 " ")))
1136 (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
1139 ;;;; Inline Babel Call
1141 ;; Inline Babel Calls are ignored.
1144 ;;;; Inline Src Block
1146 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1147 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1148 CONTENTS holds the contents of the item. INFO is a plist holding
1149 contextual information."
1150 (let* ((code (org-element-get-property :value inline-src-block))
1151 (separator (org-e-latex--find-verb-separator code)))
1152 (cond
1153 ;; Do not use a special package: transcode it verbatim.
1154 ((not org-e-latex-listings)
1155 (concat "\\verb" separator code separator))
1156 ;; Use minted package.
1157 ((eq org-e-latex-listings 'minted)
1158 (let* ((org-lang (org-element-get-property :language inline-src-block))
1159 (mint-lang (or (cadr (assq (intern org-lang)
1160 org-e-latex-minted-langs))
1161 org-lang))
1162 (options (org-e-latex--make-option-string
1163 org-e-latex-minted-options)))
1164 (concat (format "\\mint%s{%s}"
1165 (if (string= options "") "" (format "[%s]" options))
1166 mint-lang)
1167 separator code separator)))
1168 ;; Use listings package.
1170 ;; Maybe translate language's name.
1171 (let* ((org-lang (org-element-get-property :language inline-src-block))
1172 (lst-lang (or (cadr (assq (intern org-lang)
1173 org-e-latex-listings-langs))
1174 org-lang))
1175 (options (org-e-latex--make-option-string
1176 (append org-e-latex-listings-options
1177 `(("language" ,lst-lang))))))
1178 (concat (format "\\lstinline[%s]" options)
1179 separator code separator))))))
1182 ;;;; Inlinetask
1184 (defun org-e-latex-inlinetask (inlinetask contents info)
1185 "Transcode an INLINETASK element from Org to LaTeX.
1186 CONTENTS holds the contents of the block. INFO is a plist
1187 holding contextual information."
1188 (let ((title (org-export-secondary-string
1189 (org-element-get-property :title inlinetask) 'e-latex info))
1190 (todo (and (plist-get info :with-todo-keywords)
1191 (let ((todo (org-element-get-property
1192 :todo-keyword inlinetask)))
1193 (and todo
1194 (org-export-secondary-string todo 'e-latex info)))))
1195 (todo-type (org-element-get-property :todo-type inlinetask))
1196 (tags (and (plist-get info :with-tags)
1197 (org-element-get-property :tags inlinetask)))
1198 (priority (and (plist-get info :with-priority)
1199 (org-element-get-property :priority inlinetask))))
1200 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1201 ;; with appropriate arguments.
1202 (if (functionp org-e-latex-format-inlinetask-function)
1203 (funcall org-e-latex-format-inlinetask-function
1204 todo todo-type priority title tags contents)
1205 ;; Otherwise, use a default template.
1206 (org-e-latex--wrap-label
1207 inlinetask
1208 (let ((full-title
1209 (concat
1210 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1211 (when priority (format "\\framebox{\\#%c} " priority))
1212 title
1213 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1214 (format (concat "\\begin{center}\n"
1215 "\\fbox{\n"
1216 "\\begin{minipage}[c]{.6\\textwidth}\n"
1217 "%s\n\n"
1218 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1219 "%s"
1220 "\\end{minipage}\n"
1221 "}\n"
1222 "\\end{center}")
1223 full-title contents))))))
1226 ;;;; Item
1228 (defun org-e-latex-item (item contents info)
1229 "Transcode an ITEM element from Org to LaTeX.
1230 CONTENTS holds the contents of the item. INFO is a plist holding
1231 contextual information."
1232 ;; Grab `:level' from plain-list properties, which is always the
1233 ;; first element above current item.
1234 (let* ((level (org-element-get-property
1235 :level (car (plist-get info :genealogy))))
1236 (counter (let ((count (org-element-get-property :counter item)))
1237 (and count
1238 (< level 4)
1239 (format "\\setcounter{enum%s}{%s}\n"
1240 (nth level '("i" "ii" "iii" "iv"))
1241 (1- count)))))
1242 (checkbox (let ((checkbox (org-element-get-property :checkbox item)))
1243 (cond ((eq checkbox 'on) "$\\boxtimes$ ")
1244 ((eq checkbox 'off) "$\\Box$ ")
1245 ((eq checkbox 'trans) "$\\boxminus$ "))))
1246 (tag (let ((tag (org-element-get-property :tag item)))
1247 (and tag
1248 (format "[%s]" (org-export-secondary-string
1249 tag 'e-latex info))))))
1250 (concat counter "\\item" tag " " checkbox contents)))
1253 ;;;; Keyword
1255 (defun org-e-latex-keyword (keyword contents info)
1256 "Transcode a KEYWORD element from Org to LaTeX.
1257 CONTENTS is nil. INFO is a plist holding contextual information."
1258 (let ((key (downcase (org-element-get-property :key keyword)))
1259 (value (org-element-get-property :value keyword)))
1260 (cond
1261 ((string= key "latex") value)
1262 ((string= key "index") (format "\\index{%s}" value))
1263 ((string= key "target")
1264 (format "\\label{%s}" (org-export-solidify-link-text value)))
1265 ((string= key "toc")
1266 (let ((value (downcase value)))
1267 (cond
1268 ((string-match "\\<headlines\\>" value)
1269 (let ((depth (or (and (string-match "[0-9]+" value)
1270 (string-to-number (match-string 0 value)))
1271 (plist-get info :with-toc))))
1272 (concat
1273 (when (wholenump depth)
1274 (format "\\setcounter{tocdepth}{%s}\n" depth))
1275 "\\tableofcontents")))
1276 ((string= "tables" value) "\\listoftables")
1277 ((string= "figures" value) "\\listoffigures")
1278 ((string= "listings" value) "\\listoflistings")))))))
1281 ;;;; Latex Environment
1283 (defun org-e-latex-latex-environment (latex-environment contents info)
1284 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1285 CONTENTS is nil. INFO is a plist holding contextual information."
1286 (org-e-latex--wrap-label
1287 latex-environment
1288 (org-remove-indentation (org-element-get-property :value latex-environment))))
1291 ;;;; Latex Fragment
1293 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1294 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1295 CONTENTS is nil. INFO is a plist holding contextual information."
1296 (org-element-get-property :value latex-fragment))
1299 ;;;; Line Break
1301 (defun org-e-latex-line-break (line-break contents info)
1302 "Transcode a LINE-BREAK object from Org to LaTeX.
1303 CONTENTS is nil. INFO is a plist holding contextual information."
1304 "\\\\")
1307 ;;;; Link
1309 (defun org-e-latex-link--inline-image (path info)
1310 "Return LaTeX code for an image at PATH.
1311 INFO is a plist containing export options."
1312 (let* ((parent-props (nth 1 (car (plist-get info :genealogy))))
1313 (caption (org-e-latex--caption/label-string
1314 (plist-get parent-props :caption)
1315 (plist-get parent-props :name)
1316 info))
1317 ;; Retrieve latex attributes from the element around.
1318 (attr (let ((raw-attr
1319 (mapconcat #'identity
1320 (plist-get parent-props :attr_latex) " ")))
1321 (unless (string= raw-attr "") raw-attr)))
1322 (disposition
1323 (cond
1324 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1325 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1326 ((or (and attr (string-match "\\<float\\>" attr))
1327 (not (string= caption "")))
1328 'float)))
1329 (placement
1330 (cond
1331 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1332 (org-match-string-no-properties 1 attr))
1333 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1334 ((eq disposition 'float)
1335 (concat "[" org-e-latex-default-figure-position "]"))
1336 (t ""))))
1337 ;; Now clear ATTR from any special keyword and set a default
1338 ;; value if nothing is left.
1339 (setq attr
1340 (if (not attr) ""
1341 (org-trim
1342 (replace-regexp-in-string
1343 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
1344 (setq attr (cond ((not (string= attr "")) attr)
1345 ((eq disposition 'float) "width=0.7\\textwidth")
1346 ((eq disposition 'wrap) "width=0.48\\textwidth")
1347 (t (or org-e-latex-image-default-option ""))))
1348 ;; Return proper string, depending on DISPOSITION.
1349 (case disposition
1350 ('wrap (format "\\begin{wrapfigure}%s
1351 \\centering
1352 \\includegraphics[%s]{%s}
1353 %s\\end{wrapfigure}" placement attr path caption))
1354 ('mulicolumn (format "\\begin{figure*}%s
1355 \\centering
1356 \\includegraphics[%s]{%s}
1357 %s\\end{figure*}" placement attr path caption))
1358 ('float (format "\\begin{figure}%s
1359 \\centering
1360 \\includegraphics[%s]{%s}
1361 %s\\end{figure}" placement attr path caption))
1362 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1364 (defun org-e-latex-link (link desc info)
1365 "Transcode a LINK object from Org to LaTeX.
1367 DESC is the description part of the link, or the empty string.
1368 INFO is a plist holding contextual information. See
1369 `org-export-data'."
1370 (let* ((type (org-element-get-property :type link))
1371 (raw-path (org-element-get-property :path link))
1372 ;; Ensure DESC really exists, or set it to nil.
1373 (desc (and (not (string= desc "")) desc))
1374 (imagep (org-export-inline-image-p
1375 link org-e-latex-inline-image-extensions))
1376 (path (cond
1377 ((member type '("http" "https" "ftp" "mailto"))
1378 (concat type ":" raw-path))
1379 (imagep (if (not (file-name-absolute-p raw-path)) raw-path
1380 (expand-file-name raw-path)))
1381 ((string= type "file")
1382 (when (string-match "\\(.+\\)::.+" raw-path)
1383 (setq raw-path (match-string 1 raw-path)))
1384 (if (file-name-absolute-p raw-path)
1385 (concat "file://" (expand-file-name raw-path))
1386 ;; TODO: Not implemented yet. Concat also:
1387 ;; (org-export-directory :LaTeX info)
1388 (concat "file://" raw-path)))
1389 (t raw-path)))
1390 protocol)
1391 (cond
1392 ;; Image file.
1393 (imagep (org-e-latex-link--inline-image path info))
1394 ;; Target or radioed target: replace link with the normalized
1395 ;; custom-id/target name.
1396 ((member type '("target" "radio"))
1397 (format "\\hyperref[%s]{%s}"
1398 (org-export-solidify-link-text path)
1399 (or desc (org-export-secondary-string path 'e-latex info))))
1400 ;; Links pointing to an headline: Find destination and build
1401 ;; appropriate referencing commanding.
1402 ((member type '("custom-id" "fuzzy" "id"))
1403 (let ((destination (if (string= type "fuzzy")
1404 (org-export-resolve-fuzzy-link link info)
1405 (org-export-resolve-id-link link info))))
1406 ;; Fuzzy link points to a target. Do as above.
1407 (case (car destination)
1408 (target
1409 (format "\\hyperref[%s]{%s}"
1410 (org-export-solidify-link-text
1411 (org-element-get-property :raw-value destination))
1412 (or desc
1413 (org-export-secondary-string
1414 (org-element-get-property :raw-link link)
1415 'e-latex info))))
1416 ;; Fuzzy link points to an headline. If headlines are
1417 ;; numbered and the link has no description, display
1418 ;; headline's number. Otherwise, display description or
1419 ;; headline's title.
1420 (headline
1421 (let ((label
1422 (format "sec-%s"
1423 (mapconcat
1424 'number-to-string
1425 (org-export-get-headline-number destination info)
1426 "-"))))
1427 (if (and (plist-get info :section-numbers) (not desc))
1428 (format "\\ref{%s}" label)
1429 (format "\\hyperref[%s]{%s}" label
1430 (or desc
1431 (org-export-secondary-string
1432 (org-element-get-property :title destination)
1433 'e-latex info))))))
1434 ;; Fuzzy link points nowhere.
1435 (otherwise
1436 (format "\\texttt{%s}"
1437 (or desc
1438 (org-export-secondary-string
1439 (org-element-get-property :raw-link link)
1440 'e-latex info)))))))
1441 ;; Coderef: replace link with the reference name or the
1442 ;; equivalent line number.
1443 ((string= type "coderef")
1444 (format (org-export-get-coderef-format path (or desc ""))
1445 (org-export-resolve-coderef path info)))
1446 ;; Link type is handled by a special function.
1447 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1448 (funcall protocol (org-link-unescape path) desc 'latex))
1449 ;; External link with a description part.
1450 ((and path desc) (format "\\href{%s}{%s}" path desc))
1451 ;; External link without a description part.
1452 (path (format "\\url{%s}" path))
1453 ;; No path, only description. Try to do something useful.
1454 (t (format "\\texttt{%s}" desc)))))
1457 ;;;; Babel Call
1459 ;; Babel Calls are ignored.
1462 ;;;; Macro
1464 (defun org-e-latex-macro (macro contents info)
1465 "Transcode a MACRO element from Org to LaTeX.
1466 CONTENTS is nil. INFO is a plist holding contextual information."
1467 ;; Use available tools.
1468 (org-export-expand-macro macro info))
1471 ;;;; Paragraph
1473 (defun org-e-latex-paragraph (paragraph contents info)
1474 "Transcode a PARAGRAPH element from Org to LaTeX.
1475 CONTENTS is the contents of the paragraph, as a string. INFO is
1476 the plist used as a communication channel."
1477 contents)
1480 ;;;; Plain List
1482 (defun org-e-latex-plain-list (plain-list contents info)
1483 "Transcode a PLAIN-LIST element from Org to LaTeX.
1484 CONTENTS is the contents of the list. INFO is a plist holding
1485 contextual information."
1486 (let* ((type (org-element-get-property :type plain-list))
1487 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1488 "inparadesc" "asparadesc"))
1489 (paralist-regexp (concat
1490 "\\("
1491 (mapconcat 'identity paralist-types "\\|")
1492 "\\)"))
1493 (attr (mapconcat #'identity
1494 (org-element-get-property :attr_latex plain-list)
1495 " "))
1496 (latex-type (cond
1497 ((and attr
1498 (string-match
1499 (format "\\<%s\\>" paralist-regexp) attr))
1500 (match-string 1 attr))
1501 ((eq type 'ordered) "enumerate")
1502 ((eq type 'unordered) "itemize")
1503 ((eq type 'descriptive) "description"))))
1504 (org-e-latex--wrap-label
1505 plain-list
1506 (format "\\begin{%s}%s\n%s\\end{%s}"
1507 latex-type
1508 ;; Once special environment, if any, has been removed, the
1509 ;; rest of the attributes will be optional arguments.
1510 ;; They will be put inside square brackets if necessary.
1511 (let ((opt (replace-regexp-in-string
1512 (format " *%s *" paralist-regexp) "" attr)))
1513 (cond ((string= opt "") "")
1514 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1515 (t (format "[%s]" opt))))
1516 contents
1517 latex-type))))
1520 ;;;; Plain Text
1522 (defun org-e-latex-plain-text (text info)
1523 "Transcode a TEXT string from Org to LaTeX.
1524 TEXT is the string to transcode. INFO is a plist holding
1525 contextual information."
1526 ;; Protect %, #, &, $, ~, ^, _, { and }.
1527 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1528 (setq text
1529 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1530 ;; Protect \
1531 (setq text (replace-regexp-in-string
1532 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1533 "$\\backslash$" text nil t 1))
1534 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1535 (let ((case-fold-search nil)
1536 (start 0))
1537 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1538 (setq text (replace-match
1539 (format "\\%s{}" (match-string 1 text)) nil t text)
1540 start (match-end 0))))
1541 ;; Handle quotation marks
1542 (setq text (org-e-latex--quotation-marks text info))
1543 ;; Convert special strings.
1544 (when (plist-get info :with-special-strings)
1545 (while (string-match (regexp-quote "...") text)
1546 (setq text (replace-match "\\ldots{}" nil t text))))
1547 ;; Handle break preservation if required.
1548 (when (plist-get info :preserve-breaks)
1549 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1550 text)))
1551 ;; Return value.
1552 text)
1555 ;;;; Property Drawer
1557 (defun org-e-latex-property-drawer (property-drawer contents info)
1558 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1559 CONTENTS is nil. INFO is a plist holding contextual
1560 information."
1561 ;; The property drawer isn't exported but we want separating blank
1562 ;; lines nonetheless.
1566 ;;;; Quote Block
1568 (defun org-e-latex-quote-block (quote-block contents info)
1569 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1570 CONTENTS holds the contents of the block. INFO is a plist
1571 holding contextual information."
1572 (org-e-latex--wrap-label
1573 quote-block
1574 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1577 ;;;; Quote Section
1579 (defun org-e-latex-quote-section (quote-section contents info)
1580 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1581 CONTENTS is nil. INFO is a plist holding contextual information."
1582 (let ((value (org-remove-indentation
1583 (org-element-get-property :value quote-section))))
1584 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1587 ;;;; Section
1589 (defun org-e-latex-section (section contents info)
1590 "Transcode a SECTION element from Org to LaTeX.
1591 CONTENTS holds the contents of the section. INFO is a plist
1592 holding contextual information."
1593 contents)
1596 ;;;; Radio Target
1598 (defun org-e-latex-radio-target (radio-target text info)
1599 "Transcode a RADIO-TARGET object from Org to LaTeX.
1600 TEXT is the text of the target. INFO is a plist holding
1601 contextual information."
1602 (format "\\label{%s}%s"
1603 (org-export-solidify-link-text
1604 (org-element-get-property :raw-value radio-target))
1605 text))
1608 ;;;; Special Block
1610 (defun org-e-latex-special-block (special-block contents info)
1611 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1612 CONTENTS holds the contents of the block. INFO is a plist
1613 holding contextual information."
1614 (let ((type (downcase (org-element-get-property :type special-block))))
1615 (org-e-latex--wrap-label
1616 special-block
1617 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
1620 ;;;; Src Block
1622 (defun org-e-latex-src-block (src-block contents info)
1623 "Transcode a SRC-BLOCK element from Org to LaTeX.
1624 CONTENTS holds the contents of the item. INFO is a plist holding
1625 contextual information."
1626 (let* ((lang (org-element-get-property :language src-block))
1627 (code (org-export-handle-code src-block info))
1628 (caption (org-element-get-property :caption src-block))
1629 (label (org-element-get-property :name src-block))
1630 (custom-env (and lang
1631 (cadr (assq (intern lang)
1632 org-e-latex-custom-lang-environments)))))
1633 (cond
1634 ;; No source fontification.
1635 ((not org-e-latex-listings)
1636 (let ((caption-str (org-e-latex--caption/label-string
1637 caption label info))
1638 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
1639 (format (or float-env "%s")
1640 (concat
1641 caption-str
1642 (format "\\begin{verbatim}\n%s\\end{verbatim}" code)))))
1643 ;; Custom environment.
1644 (custom-env
1645 (format "\\begin{%s}\n%s\\end{%s}\n" custom-env code custom-env))
1646 ;; Use minted package.
1647 ((eq org-e-latex-listings 'minted)
1648 (let* ((mint-lang (or (cadr (assq (intern lang) org-e-latex-minted-langs))
1649 lang))
1650 (float-env (when (or label caption)
1651 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1652 (org-e-latex--caption/label-string
1653 caption label info))))
1654 (body (format "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1655 (org-e-latex--make-option-string
1656 org-e-latex-minted-options)
1657 mint-lang code)))
1658 (if float-env (format float-env body) body)))
1659 ;; Use listings package.
1661 (let ((lst-lang
1662 (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
1663 (caption-str
1664 (when caption
1665 (let ((main (org-export-secondary-string
1666 (car caption) 'e-latex info)))
1667 (if (not (cdr caption)) (format "{%s}" main)
1668 (format
1669 "{[%s]%s}"
1670 (org-export-secondary-string (cdr caption) 'e-latex info)
1671 main))))))
1672 (concat (format "\\lstset{%s}\n"
1673 (org-e-latex--make-option-string
1674 (append org-e-latex-listings-options
1675 `(("language" ,lst-lang))
1676 (when label `(("label" ,label)))
1677 (when caption-str
1678 `(("caption" ,caption-str))))))
1679 (format "\\begin{lstlisting}\n%s\\end{lstlisting}" code)))))))
1682 ;;;; Statistics Cookie
1684 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
1685 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1686 CONTENTS is nil. INFO is a plist holding contextual information."
1687 (org-element-get-property :value statistics-cookie))
1690 ;;;; Subscript
1692 (defun org-e-latex-subscript (subscript contents info)
1693 "Transcode a SUBSCRIPT object from Org to LaTeX.
1694 CONTENTS is the contents of the object. INFO is a plist holding
1695 contextual information."
1696 (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
1699 ;;;; Superscript
1701 (defun org-e-latex-superscript (superscript contents info)
1702 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1703 CONTENTS is the contents of the object. INFO is a plist holding
1704 contextual information."
1705 (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
1708 ;;;; Table
1710 (defun org-e-latex-table--format-string (table table-info info)
1711 "Return an appropriate format string for TABLE.
1713 TABLE-INFO is the plist containing format info about the table,
1714 as returned by `org-export-table-format-info'. INFO is a plist
1715 used as a communication channel.
1717 The format string leaves one placeholder for the body of the
1718 table."
1719 (let* ((label (org-element-get-property :name table))
1720 (caption (org-e-latex--caption/label-string
1721 (org-element-get-property :caption table) label info))
1722 (attr (mapconcat 'identity
1723 (org-element-get-property :attr_latex table)
1724 " "))
1725 ;; Determine alignment string.
1726 (alignment (org-e-latex-table--align-string attr table-info))
1727 ;; Determine environment for the table: longtable, tabular...
1728 (table-env (cond
1729 ((not attr) org-e-latex-default-table-environment)
1730 ((string-match "\\<longtable\\>" attr) "longtable")
1731 ((string-match "\\<tabular.?\\>" attr)
1732 (org-match-string-no-properties 0 attr))
1733 (t org-e-latex-default-table-environment)))
1734 ;; If table is a float, determine environment: table or table*.
1735 (float-env (cond
1736 ((string= "longtable" table-env) nil)
1737 ((and attr
1738 (or (string-match (regexp-quote "table*") attr)
1739 (string-match "\\<multicolumn\\>" attr)))
1740 "table*")
1741 ((or (not (string= caption "")) label) "table")))
1742 ;; Extract others display options.
1743 (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
1744 (org-match-string-no-properties 1 attr)))
1745 (placement
1746 (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1747 (org-match-string-no-properties 1 attr)
1748 (format "[%s]" org-e-latex-default-figure-position))))
1749 ;; Prepare the final format string for the table.
1750 (cond
1751 ;; Longtable.
1752 ((string= "longtable" table-env)
1753 (format
1754 "\\begin{longtable}{%s}\n%s\n%%s\n%s\\end{longtable}"
1755 alignment
1756 (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
1757 (concat (org-trim caption) "\\\\"))
1758 (if (or org-e-latex-table-caption-above (string= "" caption)) ""
1759 (concat (org-trim caption) "\\\\\n"))))
1760 ;; Others.
1761 (t (concat (when float-env
1762 (concat
1763 (format "\\begin{%s}%s\n" float-env placement)
1764 (if org-e-latex-table-caption-above caption "")))
1765 (when org-e-latex-tables-centered "\\begin{center}\n")
1766 (format "\\begin{%s}%s{%s}\n%%s\n\\end{%s}"
1767 table-env
1768 (if width (format "{%s}" width) "") alignment table-env)
1769 (when org-e-latex-tables-centered "\n\\end{center}")
1770 (when float-env
1771 (concat (if org-e-latex-table-caption-above "" caption)
1772 (format "\n\\end{%s}" float-env))))))))
1774 (defun org-e-latex-table--align-string (attr table-info)
1775 "Return an appropriate LaTeX alignment string.
1776 ATTR is a string containing table's LaTeX specific attributes.
1777 TABLE-INFO is the plist containing format info about the table,
1778 as returned by `org-export-table-format-info'."
1779 (or (and attr
1780 (string-match "\\<align=\\(\\S-+\\)" attr)
1781 (match-string 1 attr))
1782 (let* ((align (copy-sequence (plist-get table-info :alignment)))
1783 (colgroups (copy-sequence (plist-get table-info :column-groups)))
1784 (cols (length align))
1785 (separators (make-vector (1+ cols) "")))
1786 ;; Ignore the first column if it's special.
1787 (when (plist-get table-info :special-column-p)
1788 (aset align 0 "") (aset colgroups 0 nil))
1789 (let ((col 0))
1790 (mapc (lambda (el)
1791 (let ((gr (aref colgroups col)))
1792 (when (memq gr '(start start-end))
1793 (aset separators col "|"))
1794 (when (memq gr '(end start-end))
1795 (aset separators (1+ col) "|")))
1796 (incf col))
1797 align))
1798 ;; Build the LaTeX specific alignment string.
1799 (loop for al across align
1800 for sep across separators
1801 concat (concat sep al) into output
1802 finally return (concat output (aref separators cols))))))
1804 (defun org-e-latex-table (table contents info)
1805 "Transcode a TABLE element from Org to LaTeX.
1806 CONTENTS is nil. INFO is a plist holding contextual information."
1807 (let ((attr (mapconcat #'identity
1808 (org-element-get-property :attr_latex table)
1809 " "))
1810 (raw-table (org-element-get-property :raw-table table)))
1811 (cond
1812 ;; Case 1: verbatim table.
1813 ((or org-e-latex-tables-verbatim
1814 (and attr (string-match "\\<verbatim\\>" attr)))
1815 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
1816 (org-export-clean-table
1817 raw-table
1818 (plist-get (org-export-table-format-info raw-table)
1819 :special-column-p))))
1820 ;; Case 2: table.el table. Convert it using appropriate tools.
1821 ((eq (org-element-get-property :type table) 'table.el)
1822 (require 'table)
1823 ;; Ensure "*org-export-table*" buffer is empty.
1824 (with-current-buffer (get-buffer-create "*org-export-table*")
1825 (erase-buffer))
1826 (let ((output (with-temp-buffer
1827 (insert raw-table)
1828 (goto-char 1)
1829 (re-search-forward "^[ \t]*|[^|]" nil t)
1830 (table-generate-source 'latex "*org-export-table*")
1831 (with-current-buffer "*org-export-table*"
1832 (org-trim (buffer-string))))))
1833 (kill-buffer (get-buffer "*org-export-table*"))
1834 ;; Remove left out comments.
1835 (while (string-match "^%.*\n" output)
1836 (setq output (replace-match "" t t output)))
1837 ;; When the "rmlines" attribute is provided, remove all hlines
1838 ;; but the the one separating heading from the table body.
1839 (when (and attr (string-match "\\<rmlines\\>" attr))
1840 (let ((n 0) (pos 0))
1841 (while (and (< (length output) pos)
1842 (setq pos (string-match "^\\\\hline\n?" output pos)))
1843 (incf n)
1844 (unless (= n 2)
1845 (setq output (replace-match "" nil nil output))))))
1846 (if (not org-e-latex-tables-centered) output
1847 (format "\\begin{center}\n%s\n\\end{center}" output))))
1848 ;; Case 3: Standard table.
1850 (let* ((table-info (org-export-table-format-info raw-table))
1851 (columns-number (length (plist-get table-info :alignment)))
1852 (longtablep (and attr (string-match "\\<longtable\\>" attr)))
1853 (booktabsp
1854 (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
1855 org-e-latex-tables-booktabs))
1856 ;; CLEAN-TABLE is a table turned into a list, much like
1857 ;; `org-table-to-lisp', with special column and
1858 ;; formatting cookies removed, and cells already
1859 ;; transcoded.
1860 (clean-table
1861 (mapcar
1862 (lambda (row)
1863 (if (string-match org-table-hline-regexp row) 'hline
1864 (mapcar
1865 (lambda (cell)
1866 (org-export-secondary-string
1867 (org-element-parse-secondary-string
1868 cell
1869 (cdr (assq 'table org-element-string-restrictions)))
1870 'e-latex info))
1871 (org-split-string row "[ \t]*|[ \t]*"))))
1872 (org-split-string
1873 (org-export-clean-table
1874 raw-table (plist-get table-info :special-column-p))
1875 "\n"))))
1876 ;; If BOOKTABSP is non-nil, remove any rule at the beginning
1877 ;; and the end of the table, since booktabs' special rules
1878 ;; will be inserted instead.
1879 (when booktabsp
1880 (when (eq (car clean-table) 'hline)
1881 (setq clean-table (cdr clean-table)))
1882 (when (eq (car (last clean-table)) 'hline)
1883 (setq clean-table (butlast clean-table))))
1884 ;; Convert ROWS to send them to `orgtbl-to-latex'. In
1885 ;; particular, send each cell to
1886 ;; `org-element-parse-secondary-string' to expand any Org
1887 ;; object within. Eventually, flesh the format string out
1888 ;; with the table.
1889 (format
1890 (org-e-latex-table--format-string table table-info info)
1891 (orgtbl-to-latex
1892 clean-table
1893 ;; Parameters passed to `orgtbl-to-latex'.
1894 `(:tstart ,(and booktabsp "\\toprule")
1895 :tend ,(and booktabsp "\\bottomrule")
1896 :hline ,(if booktabsp "\\midrule" "\\hline")
1897 ;; Longtable environment requires specific header
1898 ;; lines end string.
1899 :hlend ,(and longtablep
1900 (format "\\\\
1902 \\endhead
1903 %s\\multicolumn{%d}{r}{Continued on next page}\\\\
1904 \\endfoot
1905 \\endlastfoot"
1906 (if booktabsp "\\midrule" "\\hline")
1907 (if booktabsp "\\midrule" "\\hline")
1908 columns-number))))))))))
1911 ;;;; Target
1913 (defun org-e-latex-target (target text info)
1914 "Transcode a TARGET object from Org to LaTeX.
1915 TEXT is the text of the target. INFO is a plist holding
1916 contextual information."
1917 (format "\\label{%s}%s"
1918 (org-export-solidify-link-text
1919 (org-element-get-property :raw-value target))
1920 text))
1923 ;;;; Time-stamp
1925 (defun org-e-latex-time-stamp (time-stamp contents info)
1926 "Transcode a TIME-STAMP object from Org to LaTeX.
1927 CONTENTS is nil. INFO is a plist holding contextual
1928 information."
1929 (let ((value (org-element-get-property :value time-stamp))
1930 (type (org-element-get-property :type time-stamp))
1931 (appt-type (org-element-get-property :appt-type time-stamp)))
1932 (concat (cond ((eq appt-type 'scheduled)
1933 (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
1934 ((eq appt-type 'deadline)
1935 (format "\\textbf{\\textsc{%s}} " org-deadline-string))
1936 ((eq appt-type 'closed)
1937 (format "\\textbf{\\textsc{%s}} " org-closed-string)))
1938 (cond ((memq type '(active active-range))
1939 (format org-e-latex-active-timestamp-format value))
1940 ((memq type '(inactive inactive-range))
1941 (format org-e-latex-inactive-timestamp-format value))
1943 (format org-e-latex-diary-timestamp-format value))))))
1946 ;;;; Verbatim
1948 (defun org-e-latex-verbatim (verbatim contents info)
1949 "Transcode a VERBATIM object from Org to LaTeX.
1950 CONTENTS is nil. INFO is a plist used as a communication
1951 channel."
1952 (let ((fmt (cdr (assoc (org-element-get-property :marker verbatim)
1953 org-e-latex-emphasis-alist)))
1954 (value (org-element-get-property :value verbatim)))
1955 (cond
1956 ;; Handle the `verb' special case.
1957 ((eq 'verb fmt)
1958 (let ((separator (org-e-latex--find-verb-separator value)))
1959 (concat "\\verb" separator value separator)))
1960 ;; Handle the `protectedtexttt' special case.
1961 ((eq 'protectedtexttt fmt)
1962 (let ((start 0)
1963 (trans '(("\\" . "\\textbackslash{}")
1964 ("~" . "\\textasciitilde{}")
1965 ("^" . "\\textasciicircum{}")))
1966 (rtn "")
1967 char)
1968 (while (string-match "[\\{}$%&_#~^]" value)
1969 (setq char (match-string 0 value))
1970 (if (> (match-beginning 0) 0)
1971 (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
1972 (setq value (substring value (1+ (match-beginning 0))))
1973 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
1974 rtn (concat rtn char)))
1975 (setq value (concat rtn value)
1976 fmt "\\texttt{%s}")
1977 (while (string-match "--" value)
1978 (setq value (replace-match "-{}-" t t value)))
1979 (format fmt value)))
1980 ;; Else use format string.
1981 (t (format fmt value)))))
1984 ;;;; Verse Block
1986 (defun org-e-latex-verse-block (verse-block contents info)
1987 "Transcode a VERSE-BLOCK element from Org to LaTeX.
1988 CONTENTS is nil. INFO is a plist holding contextual information."
1989 (org-e-latex--wrap-label
1990 verse-block
1991 ;; In a verse environment, add a line break to each newline
1992 ;; character and change each white space at beginning of a line
1993 ;; into a space of 1 em. Also change each blank line with
1994 ;; a vertical space of 1 em.
1995 (progn
1996 (setq contents (replace-regexp-in-string
1997 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1998 (replace-regexp-in-string
1999 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
2000 (org-remove-indentation
2001 (org-export-secondary-string
2002 (org-element-get-property :value verse-block)
2003 'e-latex info)))))
2004 (while (string-match "^[ \t]+" contents)
2005 (let ((new-str (format "\\hspace*{%dem}"
2006 (length (match-string 0 contents)))))
2007 (setq contents (replace-match new-str nil t contents))))
2008 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2012 ;;; Interactive functions
2014 (defun org-e-latex-export-to-latex
2015 (&optional subtreep visible-only body-only ext-plist pub-dir)
2016 "Export current buffer to a LaTeX file.
2018 If narrowing is active in the current buffer, only export its
2019 narrowed part.
2021 If a region is active, export that region.
2023 When optional argument SUBTREEP is non-nil, export the sub-tree
2024 at point, extracting information from the headline properties
2025 first.
2027 When optional argument VISIBLE-ONLY is non-nil, don't export
2028 contents of hidden elements.
2030 When optional argument BODY-ONLY is non-nil, only write code
2031 between \"\\begin{document}\" and \"\\end{document}\".
2033 EXT-PLIST, when provided, is a property list with external
2034 parameters overriding Org default settings, but still inferior to
2035 file-local settings.
2037 When optional argument PUB-DIR is set, use it as the publishing
2038 directory.
2040 Return output file's name."
2041 (interactive)
2042 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
2043 (org-export-to-file
2044 'e-latex outfile subtreep visible-only body-only ext-plist)))
2046 (defun org-e-latex-export-to-pdf
2047 (&optional subtreep visible-only body-only ext-plist pub-dir)
2048 "Export current buffer to LaTeX then process through to PDF.
2050 If narrowing is active in the current buffer, only export its
2051 narrowed part.
2053 If a region is active, export that region.
2055 When optional argument SUBTREEP is non-nil, export the sub-tree
2056 at point, extracting information from the headline properties
2057 first.
2059 When optional argument VISIBLE-ONLY is non-nil, don't export
2060 contents of hidden elements.
2062 When optional argument BODY-ONLY is non-nil, only write code
2063 between \"\\begin{document}\" and \"\\end{document}\".
2065 EXT-PLIST, when provided, is a property list with external
2066 parameters overriding Org default settings, but still inferior to
2067 file-local settings.
2069 When optional argument PUB-DIR is set, use it as the publishing
2070 directory.
2072 Return PDF file's name."
2073 (interactive)
2074 (org-e-latex-compile
2075 (org-e-latex-export-to-latex
2076 subtreep visible-only body-only ext-plist pub-dir)))
2078 (defun org-e-latex-compile (texfile)
2079 "Compile a TeX file.
2081 TEXFILE is the name of the file being compiled. Processing is
2082 done through the command specified in `org-e-latex-pdf-process'.
2084 Return PDF file name or an error if it couldn't be produced."
2085 (let* ((wconfig (current-window-configuration))
2086 (texfile (file-truename texfile))
2087 (base (file-name-sans-extension texfile))
2088 errors)
2089 (message (format "Processing LaTeX file %s ..." texfile))
2090 (unwind-protect
2091 (progn
2092 (cond
2093 ;; A function is provided: Apply it.
2094 ((functionp org-latex-to-pdf-process)
2095 (funcall org-latex-to-pdf-process (shell-quote-argument texfile)))
2096 ;; A list is provided: Replace %b, %f and %o with appropriate
2097 ;; values in each command before applying it. Output is
2098 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2099 ((consp org-e-latex-pdf-process)
2100 (let* ((out-dir (or (file-name-directory texfile) "./"))
2101 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2102 (mapc
2103 (lambda (command)
2104 (shell-command
2105 (replace-regexp-in-string
2106 "%b" (shell-quote-argument base)
2107 (replace-regexp-in-string
2108 "%f" (shell-quote-argument texfile)
2109 (replace-regexp-in-string
2110 "%o" (shell-quote-argument out-dir) command)))
2111 outbuf))
2112 org-e-latex-pdf-process)
2113 ;; Collect standard errors from output buffer.
2114 (setq errors (org-e-latex-collect-errors outbuf))))
2115 (t (error "No valid command to process to PDF")))
2116 (let ((pdffile (concat base ".pdf")))
2117 ;; Check for process failure. Provide collected errors if
2118 ;; possible.
2119 (if (not (file-exists-p pdffile))
2120 (error (concat (format "PDF file %s wasn't produced" pdffile)
2121 (when errors (concat ": " errors))))
2122 ;; Else remove log files, when specified, and signal end of
2123 ;; process to user, along with any error encountered.
2124 (when org-e-latex-remove-logfiles
2125 (dolist (ext org-e-latex-logfiles-extensions)
2126 (let ((file (concat base "." ext)))
2127 (when (file-exists-p file) (delete-file file)))))
2128 (message (concat "Process completed"
2129 (if (not errors) "."
2130 (concat " with errors: " errors)))))
2131 ;; Return output file name.
2132 pdffile))
2133 (set-window-configuration wconfig))))
2135 (defun org-e-latex-collect-errors (buffer)
2136 "Collect some kind of errors from \"pdflatex\" command output.
2138 BUFFER is the buffer containing output.
2140 Return collected error types as a string, or nil if there was
2141 none."
2142 (with-current-buffer buffer
2143 (save-excursion
2144 (goto-char (point-max))
2145 ;; Find final "pdflatex" run.
2146 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
2147 (let ((case-fold-search t)
2148 (errors ""))
2149 (when (save-excursion
2150 (re-search-forward "Reference.*?undefined" nil t))
2151 (setq errors (concat errors " [undefined reference]")))
2152 (when (save-excursion
2153 (re-search-forward "Citation.*?undefined" nil t))
2154 (setq errors (concat errors " [undefined citation]")))
2155 (when (save-excursion
2156 (re-search-forward "Undefined control sequence" nil t))
2157 (setq errors (concat errors " [undefined control sequence]")))
2158 (when (save-excursion
2159 (re-search-forward "^! LaTeX.*?Error" nil t))
2160 (setq errors (concat errors " [LaTeX error]")))
2161 (when (save-excursion
2162 (re-search-forward "^! Package.*?Error" nil t))
2163 (setq errors (concat errors " [package error]")))
2164 (and (org-string-nw-p errors) (org-trim errors)))))))
2167 (provide 'org-e-latex)
2168 ;;; org-e-latex.el ends here