org-e-latex: Silence byte-compiler
[org-mode/org-mode-NeilSmithlineMods.git] / EXPERIMENTAL / org-e-latex.el
blob1f7aca0e269b5b676f0da6cfa35f0f8afec6fbf4
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"
621 ("\\(\\s-\\|[[(]\\|^\\)\"" . "«~")
622 ("\\(\\S-\\)\"" . "~»")
623 ("\\(\\s-\\|(\\|^\\)'" . "'"))
624 ("en"
625 ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
626 ("\\(\\S-\\)\"" . "''")
627 ("\\(\\s-\\|(\\|^\\)'" . "`")))
628 "Alist for quotes to use when converting english double-quotes.
630 The CAR of each item in this alist is the language code.
631 The CDR of each item in this alist is a list of three CONS:
632 - the first CONS defines the opening quote;
633 - the second CONS defines the closing quote;
634 - the last CONS defines single quotes.
636 For each item in a CONS, the first string is a regexp
637 for allowed characters before/after the quote, the second
638 string defines the replacement string for this quote."
639 :group 'org-export-e-latex
640 :type '(list
641 (cons :tag "Opening quote"
642 (string :tag "Regexp for char before")
643 (string :tag "Replacement quote "))
644 (cons :tag "Closing quote"
645 (string :tag "Regexp for char after ")
646 (string :tag "Replacement quote "))
647 (cons :tag "Single quote"
648 (string :tag "Regexp for char before")
649 (string :tag "Replacement quote "))))
652 ;;;; Compilation
654 (defcustom org-e-latex-pdf-process
655 '("pdflatex -interaction nonstopmode -output-directory %o %f"
656 "pdflatex -interaction nonstopmode -output-directory %o %f"
657 "pdflatex -interaction nonstopmode -output-directory %o %f")
658 "Commands to process a LaTeX file to a PDF file.
659 This is a list of strings, each of them will be given to the
660 shell as a command. %f in the command will be replaced by the
661 full file name, %b by the file base name \(i.e. without
662 extension) and %o by the base directory of the file.
664 The reason why this is a list is that it usually takes several
665 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
666 does not have a clever mechanism to detect which of these
667 commands have to be run to get to a stable result, and it also
668 does not do any error checking.
670 By default, Org uses 3 runs of `pdflatex' to do the processing.
671 If you have texi2dvi on your system and if that does not cause
672 the infamous egrep/locale bug:
674 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
676 then `texi2dvi' is the superior choice. Org does offer it as one
677 of the customize options.
679 Alternatively, this may be a Lisp function that does the
680 processing, so you could use this to apply the machinery of
681 AUCTeX or the Emacs LaTeX mode. This function should accept the
682 file name as its single argument."
683 :group 'org-export-pdf
684 :type '(choice
685 (repeat :tag "Shell command sequence"
686 (string :tag "Shell command"))
687 (const :tag "2 runs of pdflatex"
688 ("pdflatex -interaction nonstopmode -output-directory %o %f"
689 "pdflatex -interaction nonstopmode -output-directory %o %f"))
690 (const :tag "3 runs of pdflatex"
691 ("pdflatex -interaction nonstopmode -output-directory %o %f"
692 "pdflatex -interaction nonstopmode -output-directory %o %f"
693 "pdflatex -interaction nonstopmode -output-directory %o %f"))
694 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
695 ("pdflatex -interaction nonstopmode -output-directory %o %f"
696 "bibtex %b"
697 "pdflatex -interaction nonstopmode -output-directory %o %f"
698 "pdflatex -interaction nonstopmode -output-directory %o %f"))
699 (const :tag "texi2dvi"
700 ("texi2dvi -p -b -c -V %f"))
701 (const :tag "rubber"
702 ("rubber -d --into %o %f"))
703 (function)))
705 (defcustom org-e-latex-logfiles-extensions
706 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
707 "The list of file extensions to consider as LaTeX logfiles."
708 :group 'org-export-e-latex
709 :type '(repeat (string :tag "Extension")))
711 (defcustom org-e-latex-remove-logfiles t
712 "Non-nil means remove the logfiles produced by PDF production.
713 These are the .aux, .log, .out, and .toc files."
714 :group 'org-export-e-latex
715 :type 'boolean)
719 ;;; Internal Functions
721 (defun org-e-latex--caption/label-string (caption label info)
722 "Return caption and label LaTeX string for floats.
724 CAPTION is a cons cell of secondary strings, the car being the
725 standard caption and the cdr its short form. LABEL is a string
726 representing the label. INFO is a plist holding contextual
727 information.
729 If there's no caption nor label, return the empty string.
731 For non-floats, see `org-e-latex--wrap-label'."
732 (let ((label-str (if label (format "\\label{%s}" label) "")))
733 (cond
734 ((and (not caption) (not label)) "")
735 ((not caption) (format "\\label{%s}\n" label))
736 ;; Option caption format with short name.
737 ((cdr caption)
738 (format "\\caption[%s]{%s%s}\n"
739 (org-export-secondary-string (cdr caption) 'e-latex info)
740 label-str
741 (org-export-secondary-string (car caption) 'e-latex info)))
742 ;; Standard caption format.
743 (t (format "\\caption{%s%s}\n"
744 label-str
745 (org-export-secondary-string (car caption) 'e-latex info))))))
747 (defun org-e-latex--guess-inputenc (header)
748 "Set the coding system in inputenc to what the buffer is.
750 HEADER is the LaTeX header string.
752 Return the new header."
753 (let* ((cs (or (ignore-errors
754 (latexenc-coding-system-to-inputenc
755 buffer-file-coding-system))
756 "utf8")))
757 (if (not cs) header
758 ;; First translate if that is requested.
759 (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
760 ;; Then find the \usepackage statement and replace the option.
761 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
762 cs header t nil 1))))
764 (defun org-e-latex--find-verb-separator (s)
765 "Return a character not used in string S.
766 This is used to choose a separator for constructs like \\verb."
767 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
768 (loop for c across ll
769 when (not (string-match (regexp-quote (char-to-string c)) s))
770 return (char-to-string c))))
772 (defun org-e-latex--make-option-string (options)
773 "Return a comma separated string of keywords and values.
774 OPTIONS is an alist where the key is the options keyword as
775 a string, and the value a list containing the keyword value, or
776 nil."
777 (mapconcat (lambda (pair)
778 (concat (first pair)
779 (when (> (length (second pair)) 0)
780 (concat "=" (second pair)))))
781 options
782 ","))
784 (defun org-e-latex--quotation-marks (text info)
785 "Export quotation marks depending on language conventions.
786 TEXT is a string containing quotation marks to be replaced. INFO
787 is a plist used as a communication channel."
788 (mapc (lambda(l)
789 (let ((start 0))
790 (while (setq start (string-match (car l) text start))
791 (let ((new-quote (concat (match-string 1 text) (cdr l))))
792 (setq text (replace-match new-quote t t text))))))
793 (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
794 ;; Falls back on English.
795 (assoc "en" org-e-latex-quotes))))
796 text)
798 (defun org-e-latex--wrap-label (element output)
799 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
800 This function shouldn't be used for floats. See
801 `org-e-latex--caption/label-string'."
802 (let ((label (org-element-property :name element)))
803 (if (or (not output) (not label) (string= output "") (string= label ""))
804 output
805 (concat (format "\\label{%s}\n" label) output))))
809 ;;; Template
811 (defun org-e-latex-template (contents info)
812 "Return complete document string after LaTeX conversion.
813 CONTENTS is the transcoded contents string. INFO is a plist
814 holding export options."
815 (let ((title (org-export-secondary-string
816 (plist-get info :title) 'e-latex info)))
817 (concat
818 ;; 1. Time-stamp.
819 (and (plist-get info :time-stamp-file)
820 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
821 ;; 2. Document class and packages.
822 (let ((class (plist-get info :latex-class))
823 (class-options (plist-get info :latex-class-options)))
824 (org-element-normalize-string
825 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
826 (document-class-string
827 (and (stringp header)
828 (if class-options
829 (replace-regexp-in-string
830 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
831 class-options header t nil 1)
832 header))))
833 (org-e-latex--guess-inputenc
834 (org-splice-latex-header
835 document-class-string
836 org-export-latex-default-packages-alist ; defined in org.el
837 org-export-latex-packages-alist nil ; defined in org.el
838 (plist-get info :latex-header-extra))))))
839 ;; 3. Define alert if not yet defined.
840 "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
841 ;; 4. Possibly limit depth for headline numbering.
842 (let ((sec-num (plist-get info :section-numbers)))
843 (when (integerp sec-num)
844 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
845 ;; 5. Author.
846 (let ((author (and (plist-get info :with-author)
847 (let ((auth (plist-get info :author)))
848 (and auth (org-export-secondary-string
849 auth 'e-latex info)))))
850 (email (and (plist-get info :with-email)
851 (org-export-secondary-string
852 (plist-get info :email) 'e-latex info))))
853 (cond ((and author email (not (string= "" email)))
854 (format "\\author{%s\\thanks{%s}}\n" author email))
855 (author (format "\\author{%s}\n" author))
856 (t "\\author{}\n")))
857 ;; 6. Date.
858 (let ((date (plist-get info :date)))
859 (and date (format "\\date{%s}\n" date)))
860 ;; 7. Title
861 (format "\\title{%s}\n" title)
862 ;; 8. Hyperref options.
863 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
864 (or (plist-get info :keywords) "")
865 (or (plist-get info :description) "")
866 (if (not (plist-get info :with-creator)) ""
867 (plist-get info :creator)))
868 ;; 9. Document start.
869 "\\begin{document}\n\n"
870 ;; 10. Title command.
871 (org-element-normalize-string
872 (cond ((string= "" title) nil)
873 ((not (stringp org-e-latex-title-command)) nil)
874 ((string-match "\\(?:[^%]\\|^\\)%s"
875 org-e-latex-title-command)
876 (format org-e-latex-title-command title))
877 (t org-e-latex-title-command)))
878 ;; 11. Table of contents.
879 (let ((depth (plist-get info :with-toc)))
880 (when depth
881 (concat (when (wholenump depth)
882 (format "\\setcounter{tocdepth}{%d}\n" depth))
883 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
884 ;; 12. Document's body.
885 contents
886 ;; 13. Creator.
887 (let ((creator-info (plist-get info :with-creator)))
888 (cond
889 ((not creator-info) "")
890 ((eq creator-info 'comment)
891 (format "%% %s\n" (plist-get info :creator)))
892 (t (concat (plist-get info :creator) "\n"))))
893 ;; 14. Document end.
894 "\\end{document}")))
898 ;;; Transcode Functions
900 ;;;; Block
902 (defun org-e-latex-center-block (center-block contents info)
903 "Transcode a CENTER-BLOCK element from Org to LaTeX.
904 CONTENTS holds the contents of the block. INFO is a plist
905 holding contextual information."
906 (org-e-latex--wrap-label
907 center-block
908 (format "\\begin{center}\n%s\\end{center}" contents)))
911 ;;;; Comment
913 ;; Comments are ignored.
916 ;;;; Comment Block
918 ;; Comment Blocks are ignored.
921 ;;;; Drawer
923 (defun org-e-latex-drawer (drawer contents info)
924 "Transcode a DRAWER element from Org to LaTeX.
925 CONTENTS holds the contents of the block. INFO is a plist
926 holding contextual information."
927 (let* ((name (org-element-property :drawer-name drawer))
928 (output (if (functionp org-e-latex-format-drawer-function)
929 (funcall org-e-latex-format-drawer-function
930 name contents)
931 ;; If there's no user defined function: simply
932 ;; display contents of the drawer.
933 contents)))
934 (org-e-latex--wrap-label drawer output)))
937 ;;;; Dynamic Block
939 (defun org-e-latex-dynamic-block (dynamic-block contents info)
940 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
941 CONTENTS holds the contents of the block. INFO is a plist
942 holding contextual information. See
943 `org-export-data'."
944 (org-e-latex--wrap-label dynamic-block contents))
947 ;;;; Emphasis
949 (defun org-e-latex-emphasis (emphasis contents info)
950 "Transcode EMPHASIS from Org to LaTeX.
951 CONTENTS is the contents of the emphasized text. INFO is a plist
952 holding contextual information.."
953 (format (cdr (assoc (org-element-property :marker emphasis)
954 org-e-latex-emphasis-alist))
955 contents))
958 ;;;; Entity
960 (defun org-e-latex-entity (entity contents info)
961 "Transcode an ENTITY object from Org to LaTeX.
962 CONTENTS are the definition itself. INFO is a plist holding
963 contextual information."
964 (let ((ent (org-element-property :latex entity)))
965 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
968 ;;;; Example Block
970 (defun org-e-latex-example-block (example-block contents info)
971 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
972 CONTENTS is nil. INFO is a plist holding contextual
973 information."
974 (org-e-latex--wrap-label
975 example-block
976 (format "\\begin{verbatim}\n%s\\end{verbatim}"
977 (org-export-format-code-default example-block info))))
980 ;;;; Export Snippet
982 (defun org-e-latex-export-snippet (export-snippet contents info)
983 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
984 CONTENTS is nil. INFO is a plist holding contextual information."
985 (when (eq (org-export-snippet-backend export-snippet) 'e-latex)
986 (org-element-property :value export-snippet)))
989 ;;;; Export Block
991 (defun org-e-latex-export-block (export-block contents info)
992 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
993 CONTENTS is nil. INFO is a plist holding contextual information."
994 (when (string= (org-element-property :type export-block) "latex")
995 (org-remove-indentation (org-element-property :value export-block))))
998 ;;;; Fixed Width
1000 (defun org-e-latex-fixed-width (fixed-width contents info)
1001 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1002 CONTENTS is nil. INFO is a plist holding contextual information."
1003 (let* ((value (org-element-normalize-string
1004 (replace-regexp-in-string
1005 "^[ \t]*: ?" ""
1006 (org-element-property :value fixed-width)))))
1007 (org-e-latex--wrap-label
1008 fixed-width (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1011 ;;;; Footnote Definition
1013 ;; Footnote Definitions are ignored.
1016 ;;;; Footnote Reference
1018 (defun org-e-latex-footnote-reference (footnote-reference contents info)
1019 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1020 CONTENTS is nil. INFO is a plist holding contextual information."
1021 (concat
1022 ;; Insert separator between two footnotes in a row.
1023 (let ((prev (org-export-get-previous-element footnote-reference info)))
1024 (when (eq (org-element-type prev) 'footnote-reference)
1025 org-e-latex-footnote-separator))
1026 (cond
1027 ;; Use \footnotemark if the footnote has already been defined.
1028 ((not (org-export-footnote-first-reference-p footnote-reference info))
1029 (format "\\footnotemark[%s]{}"
1030 (org-export-get-footnote-number footnote-reference info)))
1031 ;; Use also \footnotemark if reference is within another footnote
1032 ;; reference or footnote definition.
1033 ((loop for parent in (org-export-get-genealogy footnote-reference info)
1034 thereis (memq (org-element-type parent)
1035 '(footnote-reference footnote-definition)))
1036 (let ((num (org-export-get-footnote-number footnote-reference info)))
1037 (format "\\footnotemark[%s]{}\\setcounter{footnote}{%s}" num num)))
1038 ;; Otherwise, define it with \footnote command.
1040 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1041 (unless (eq (org-element-type def) 'org-data)
1042 (setq def (cons 'org-data (cons nil def))))
1043 (concat
1044 (format "\\footnote{%s}" (org-trim (org-export-data def 'e-latex info)))
1045 ;; Retrieve all footnote references within the footnote and
1046 ;; add their definition after it, since LaTeX doesn't support
1047 ;; them inside.
1048 (let* (all-refs
1049 search-refs ; for byte-compiler
1050 (search-refs
1051 (function
1052 (lambda (data)
1053 ;; Return a list of all footnote references in DATA.
1054 (org-element-map
1055 data 'footnote-reference
1056 (lambda (ref)
1057 (when (org-export-footnote-first-reference-p ref info)
1058 (push ref all-refs)
1059 (when (eq (org-element-property :type ref) 'standard)
1060 (funcall
1061 search-refs
1062 (org-export-get-footnote-definition ref info)))))
1063 info) (reverse all-refs)))))
1064 (mapconcat
1065 (lambda (ref)
1066 (format
1067 "\\footnotetext[%s]{%s}"
1068 (org-export-get-footnote-number ref info)
1069 (org-trim
1070 (funcall
1071 (if (eq (org-element-property :type ref) 'inline)
1072 'org-export-secondary-string
1073 'org-export-data)
1074 (org-export-get-footnote-definition ref info) 'e-latex info))))
1075 (funcall search-refs def) ""))))))))
1078 ;;;; Headline
1080 (defun org-e-latex-headline (headline contents info)
1081 "Transcode an HEADLINE element from Org to LaTeX.
1082 CONTENTS holds the contents of the headline. INFO is a plist
1083 holding contextual information."
1084 (let* ((class (plist-get info :latex-class))
1085 (level (org-export-get-relative-level headline info))
1086 (numberedp (org-export-numbered-headline-p headline info))
1087 (class-sectionning (assoc class org-e-latex-classes))
1088 ;; Section formatting will set two placeholders: one for the
1089 ;; title and the other for the contents.
1090 (section-fmt
1091 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
1092 (fboundp (nth 2 class-sectionning)))
1093 (funcall (nth 2 class-sectionning) level numberedp)
1094 (nth (1+ level) class-sectionning))))
1095 (cond
1096 ;; No section available for that LEVEL.
1097 ((not sec) nil)
1098 ;; Section format directly returned by a function.
1099 ((stringp sec) sec)
1100 ;; (numbered-section . unnumbered-section)
1101 ((not (consp (cdr sec)))
1102 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1103 ;; (numbered-open numbered-close)
1104 ((= (length sec) 2)
1105 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1106 ;; (num-in num-out no-num-in no-num-out)
1107 ((= (length sec) 4)
1108 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1109 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1110 (text (org-export-secondary-string
1111 (org-element-property :title headline) 'e-latex info))
1112 (todo
1113 (and (plist-get info :with-todo-keywords)
1114 (let ((todo (org-element-property :todo-keyword headline)))
1115 (and todo (org-export-secondary-string todo 'e-latex info)))))
1116 (todo-type (and todo (org-element-property :todo-type headline)))
1117 (tags (and (plist-get info :with-tags)
1118 (org-element-property :tags headline)))
1119 (priority (and (plist-get info :with-priority)
1120 (org-element-property :priority headline)))
1121 ;; Create the headline text.
1122 (full-text (if (functionp org-e-latex-format-headline-function)
1123 ;; User-defined formatting function.
1124 (funcall org-e-latex-format-headline-function
1125 todo todo-type priority text tags)
1126 ;; Default formatting.
1127 (concat
1128 (when todo
1129 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1130 (when priority (format "\\framebox{\\#%c} " priority))
1131 text
1132 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1133 ;; Associate some \label to the headline for internal links.
1134 (headline-label
1135 (format "\\label{sec-%s}\n"
1136 (mapconcat 'number-to-string
1137 (org-export-get-headline-number headline info)
1138 "-")))
1139 (pre-blanks
1140 (make-string (org-element-property :pre-blank headline) 10)))
1141 (cond
1142 ;; Case 1: This is a footnote section: ignore it.
1143 ((org-element-property :footnote-section-p headline) nil)
1144 ;; Case 2. This is a deep sub-tree: export it as a list item.
1145 ;; Also export as items headlines for which no section
1146 ;; format has been found.
1147 ((or (not section-fmt) (org-export-low-level-p headline info))
1148 ;; Build the real contents of the sub-tree.
1149 (let ((low-level-body
1150 (concat
1151 ;; If the headline is the first sibling, start a list.
1152 (when (org-export-first-sibling-p headline info)
1153 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1154 ;; Itemize headline
1155 "\\item " full-text "\n" headline-label pre-blanks contents)))
1156 ;; If headline is not the last sibling simply return
1157 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1158 ;; blank line.
1159 (if (not (org-export-last-sibling-p headline info)) low-level-body
1160 (replace-regexp-in-string
1161 "[ \t\n]*\\'"
1162 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1163 low-level-body))))
1164 ;; Case 3. Standard headline. Export it as a section.
1165 (t (format section-fmt full-text
1166 (concat headline-label pre-blanks contents))))))
1169 ;;;; Horizontal Rule
1171 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1172 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1173 CONTENTS is nil. INFO is a plist holding contextual information."
1174 (let ((attr (mapconcat #'identity
1175 (org-element-property :attr_latex horizontal-rule)
1176 " ")))
1177 (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
1180 ;;;; Inline Babel Call
1182 ;; Inline Babel Calls are ignored.
1185 ;;;; Inline Src Block
1187 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1188 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1189 CONTENTS holds the contents of the item. INFO is a plist holding
1190 contextual information."
1191 (let* ((code (org-element-property :value inline-src-block))
1192 (separator (org-e-latex--find-verb-separator code)))
1193 (cond
1194 ;; Do not use a special package: transcode it verbatim.
1195 ((not org-e-latex-listings)
1196 (concat "\\verb" separator code separator))
1197 ;; Use minted package.
1198 ((eq org-e-latex-listings 'minted)
1199 (let* ((org-lang (org-element-property :language inline-src-block))
1200 (mint-lang (or (cadr (assq (intern org-lang)
1201 org-e-latex-minted-langs))
1202 org-lang))
1203 (options (org-e-latex--make-option-string
1204 org-e-latex-minted-options)))
1205 (concat (format "\\mint%s{%s}"
1206 (if (string= options "") "" (format "[%s]" options))
1207 mint-lang)
1208 separator code separator)))
1209 ;; Use listings package.
1211 ;; Maybe translate language's name.
1212 (let* ((org-lang (org-element-property :language inline-src-block))
1213 (lst-lang (or (cadr (assq (intern org-lang)
1214 org-e-latex-listings-langs))
1215 org-lang))
1216 (options (org-e-latex--make-option-string
1217 (append org-e-latex-listings-options
1218 `(("language" ,lst-lang))))))
1219 (concat (format "\\lstinline[%s]" options)
1220 separator code separator))))))
1223 ;;;; Inlinetask
1225 (defun org-e-latex-inlinetask (inlinetask contents info)
1226 "Transcode an INLINETASK element from Org to LaTeX.
1227 CONTENTS holds the contents of the block. INFO is a plist
1228 holding contextual information."
1229 (let ((title (org-export-secondary-string
1230 (org-element-property :title inlinetask) 'e-latex info))
1231 (todo (and (plist-get info :with-todo-keywords)
1232 (let ((todo (org-element-property
1233 :todo-keyword inlinetask)))
1234 (and todo
1235 (org-export-secondary-string todo 'e-latex info)))))
1236 (todo-type (org-element-property :todo-type inlinetask))
1237 (tags (and (plist-get info :with-tags)
1238 (org-element-property :tags inlinetask)))
1239 (priority (and (plist-get info :with-priority)
1240 (org-element-property :priority inlinetask))))
1241 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1242 ;; with appropriate arguments.
1243 (if (functionp org-e-latex-format-inlinetask-function)
1244 (funcall org-e-latex-format-inlinetask-function
1245 todo todo-type priority title tags contents)
1246 ;; Otherwise, use a default template.
1247 (org-e-latex--wrap-label
1248 inlinetask
1249 (let ((full-title
1250 (concat
1251 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1252 (when priority (format "\\framebox{\\#%c} " priority))
1253 title
1254 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1255 (format (concat "\\begin{center}\n"
1256 "\\fbox{\n"
1257 "\\begin{minipage}[c]{.6\\textwidth}\n"
1258 "%s\n\n"
1259 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1260 "%s"
1261 "\\end{minipage}\n"
1262 "}\n"
1263 "\\end{center}")
1264 full-title contents))))))
1267 ;;;; Item
1269 (defun org-e-latex-item (item contents info)
1270 "Transcode an ITEM element from Org to LaTeX.
1271 CONTENTS holds the contents of the item. INFO is a plist holding
1272 contextual information."
1273 ;; Grab `:level' from plain-list properties, which is always the
1274 ;; first element above current item.
1275 (let* ((level (org-element-property :level (org-export-get-parent item info)))
1276 (counter (let ((count (org-element-property :counter item)))
1277 (and count
1278 (< level 4)
1279 (format "\\setcounter{enum%s}{%s}\n"
1280 (nth level '("i" "ii" "iii" "iv"))
1281 (1- count)))))
1282 (checkbox (let ((checkbox (org-element-property :checkbox item)))
1283 (cond ((eq checkbox 'on) "$\\boxtimes$ ")
1284 ((eq checkbox 'off) "$\\Box$ ")
1285 ((eq checkbox 'trans) "$\\boxminus$ "))))
1286 (tag (let ((tag (org-element-property :tag item)))
1287 (and tag
1288 (format "[%s]" (org-export-secondary-string
1289 tag 'e-latex info))))))
1290 (concat counter "\\item" tag " " checkbox contents)))
1293 ;;;; Keyword
1295 (defun org-e-latex-keyword (keyword contents info)
1296 "Transcode a KEYWORD element from Org to LaTeX.
1297 CONTENTS is nil. INFO is a plist holding contextual information."
1298 (let ((key (org-element-property :key keyword))
1299 (value (org-element-property :value keyword)))
1300 (cond
1301 ((string= key "LATEX") value)
1302 ((string= key "INDEX") (format "\\index{%s}" value))
1303 ;; Invisible targets.
1304 ((string= key "TARGET") nil)
1305 ((string= key "TOC")
1306 (let ((value (downcase value)))
1307 (cond
1308 ((string-match "\\<headlines\\>" value)
1309 (let ((depth (or (and (string-match "[0-9]+" value)
1310 (string-to-number (match-string 0 value)))
1311 (plist-get info :with-toc))))
1312 (concat
1313 (when (wholenump depth)
1314 (format "\\setcounter{tocdepth}{%s}\n" depth))
1315 "\\tableofcontents")))
1316 ((string= "tables" value) "\\listoftables")
1317 ((string= "figures" value) "\\listoffigures")
1318 ((string= "listings" value)
1319 (cond
1320 ((eq org-e-latex-listings 'minted) "\\listoflistings")
1321 (org-e-latex-listings "\\lstlistoflistings")
1322 ;; At the moment, src blocks with a caption are wrapped
1323 ;; into a figure environment.
1324 (t "\\listoffigures")))))))))
1327 ;;;; Latex Environment
1329 (defun org-e-latex-latex-environment (latex-environment contents info)
1330 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1331 CONTENTS is nil. INFO is a plist holding contextual information."
1332 (let ((label (org-element-property :name latex-environment))
1333 (value (org-remove-indentation
1334 (org-element-property :value latex-environment))))
1335 (if (not (org-string-nw-p label)) value
1336 ;; Environment is labelled: label must be within the environment
1337 ;; (otherwise, a reference pointing to that element will count
1338 ;; the section instead).
1339 (with-temp-buffer
1340 (insert value)
1341 (goto-char (point-min))
1342 (forward-line)
1343 (insert (format "\\label{%s}\n" label))
1344 (buffer-string)))))
1347 ;;;; Latex Fragment
1349 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1350 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1351 CONTENTS is nil. INFO is a plist holding contextual information."
1352 (org-element-property :value latex-fragment))
1355 ;;;; Line Break
1357 (defun org-e-latex-line-break (line-break contents info)
1358 "Transcode a LINE-BREAK object from Org to LaTeX.
1359 CONTENTS is nil. INFO is a plist holding contextual information."
1360 "\\\\")
1363 ;;;; Link
1365 (defun org-e-latex-link--inline-image (link info)
1366 "Return LaTeX code for an inline image.
1367 LINK is the link pointing to the inline image. INFO is a plist
1368 used as a communication channel."
1369 (let* ((parent (org-export-get-parent-paragraph link info))
1370 (path (let ((raw-path (org-element-property :path link)))
1371 (if (not (file-name-absolute-p raw-path)) raw-path
1372 (expand-file-name raw-path))))
1373 (caption (org-e-latex--caption/label-string
1374 (org-element-property :caption parent)
1375 (org-element-property :name parent)
1376 info))
1377 ;; Retrieve latex attributes from the element around.
1378 (attr (let ((raw-attr
1379 (mapconcat #'identity
1380 (org-element-property :attr_latex parent)
1381 " ")))
1382 (unless (string= raw-attr "") raw-attr)))
1383 (disposition
1384 (cond
1385 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1386 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1387 ((or (and attr (string-match "\\<float\\>" attr))
1388 (not (string= caption "")))
1389 'float)))
1390 (placement
1391 (cond
1392 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1393 (org-match-string-no-properties 1 attr))
1394 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1395 ((eq disposition 'float)
1396 (concat "[" org-e-latex-default-figure-position "]"))
1397 (t ""))))
1398 ;; Now clear ATTR from any special keyword and set a default
1399 ;; value if nothing is left.
1400 (setq attr
1401 (if (not attr) ""
1402 (org-trim
1403 (replace-regexp-in-string
1404 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
1405 (setq attr (cond ((not (string= attr "")) attr)
1406 ((eq disposition 'float) "width=0.7\\textwidth")
1407 ((eq disposition 'wrap) "width=0.48\\textwidth")
1408 (t (or org-e-latex-image-default-option ""))))
1409 ;; Return proper string, depending on DISPOSITION.
1410 (case disposition
1411 (wrap (format "\\begin{wrapfigure}%s
1412 \\centering
1413 \\includegraphics[%s]{%s}
1414 %s\\end{wrapfigure}" placement attr path caption))
1415 (mulicolumn (format "\\begin{figure*}%s
1416 \\centering
1417 \\includegraphics[%s]{%s}
1418 %s\\end{figure*}" placement attr path caption))
1419 (float (format "\\begin{figure}%s
1420 \\centering
1421 \\includegraphics[%s]{%s}
1422 %s\\end{figure}" placement attr path caption))
1423 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1425 (defun org-e-latex-link (link desc info)
1426 "Transcode a LINK object from Org to LaTeX.
1428 DESC is the description part of the link, or the empty string.
1429 INFO is a plist holding contextual information. See
1430 `org-export-data'."
1431 (let* ((type (org-element-property :type link))
1432 (raw-path (org-element-property :path link))
1433 ;; Ensure DESC really exists, or set it to nil.
1434 (desc (and (not (string= desc "")) desc))
1435 (imagep (org-export-inline-image-p
1436 link org-e-latex-inline-image-rules))
1437 (path (cond
1438 ((member type '("http" "https" "ftp" "mailto"))
1439 (concat type ":" raw-path))
1440 ((string= type "file")
1441 (when (string-match "\\(.+\\)::.+" raw-path)
1442 (setq raw-path (match-string 1 raw-path)))
1443 (if (file-name-absolute-p raw-path)
1444 (concat "file://" (expand-file-name raw-path))
1445 ;; TODO: Not implemented yet. Concat also:
1446 ;; (org-export-directory :LaTeX info)
1447 (concat "file://" raw-path)))
1448 (t raw-path)))
1449 protocol)
1450 (cond
1451 ;; Image file.
1452 (imagep (org-e-latex-link--inline-image link info))
1453 ;; Radioed target: Target's name is obtained from original raw
1454 ;; link. Path is parsed and transcoded in order to have a proper
1455 ;; display of the contents.
1456 ((string= type "radio")
1457 (format "\\hyperref[%s]{%s}"
1458 (org-export-solidify-link-text path)
1459 (org-export-secondary-string
1460 (org-element-parse-secondary-string
1461 path (cdr (assq 'radio-target org-element-object-restrictions)))
1462 'e-latex info)))
1463 ;; Links pointing to an headline: Find destination and build
1464 ;; appropriate referencing command.
1465 ((member type '("custom-id" "fuzzy" "id"))
1466 (let ((destination (if (string= type "fuzzy")
1467 (org-export-resolve-fuzzy-link link info)
1468 (org-export-resolve-id-link link info))))
1469 (case (org-element-type destination)
1470 ;; Fuzzy link points nowhere.
1471 ('nil
1472 (format "\\texttt{%s}"
1473 (or desc
1474 (org-export-secondary-string
1475 (org-element-property :raw-link link)
1476 'e-latex info))))
1477 ;; Fuzzy link points to an invisible target.
1478 (keyword nil)
1479 ;; LINK points to an headline. If headlines are numbered
1480 ;; and the link has no description, display headline's
1481 ;; number. Otherwise, display description or headline's
1482 ;; title.
1483 (headline
1484 (let ((label
1485 (format "sec-%s"
1486 (mapconcat
1487 'number-to-string
1488 (org-export-get-headline-number destination info)
1489 "-"))))
1490 (if (and (plist-get info :section-numbers) (not desc))
1491 (format "\\ref{%s}" label)
1492 (format "\\hyperref[%s]{%s}" label
1493 (or desc
1494 (org-export-secondary-string
1495 (org-element-property :title destination)
1496 'e-latex info))))))
1497 ;; Fuzzy link points to a target. Do as above.
1498 (otherwise
1499 (let ((path (org-export-solidify-link-text path)))
1500 (if (not desc) (format "\\ref{%s}" path)
1501 (format "\\hyperref[%s]{%s}" path desc)))))))
1502 ;; Coderef: replace link with the reference name or the
1503 ;; equivalent line number.
1504 ((string= type "coderef")
1505 (format (org-export-get-coderef-format path (or desc ""))
1506 (org-export-resolve-coderef path info)))
1507 ;; Link type is handled by a special function.
1508 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1509 (funcall protocol (org-link-unescape path) desc 'latex))
1510 ;; External link with a description part.
1511 ((and path desc) (format "\\href{%s}{%s}" path desc))
1512 ;; External link without a description part.
1513 (path (format "\\url{%s}" path))
1514 ;; No path, only description. Try to do something useful.
1515 (t (format "\\texttt{%s}" desc)))))
1518 ;;;; Babel Call
1520 ;; Babel Calls are ignored.
1523 ;;;; Macro
1525 (defun org-e-latex-macro (macro contents info)
1526 "Transcode a MACRO element from Org to LaTeX.
1527 CONTENTS is nil. INFO is a plist holding contextual information."
1528 ;; Use available tools.
1529 (org-export-expand-macro macro info))
1532 ;;;; Paragraph
1534 (defun org-e-latex-paragraph (paragraph contents info)
1535 "Transcode a PARAGRAPH element from Org to LaTeX.
1536 CONTENTS is the contents of the paragraph, as a string. INFO is
1537 the plist used as a communication channel."
1538 contents)
1541 ;;;; Plain List
1543 (defun org-e-latex-plain-list (plain-list contents info)
1544 "Transcode a PLAIN-LIST element from Org to LaTeX.
1545 CONTENTS is the contents of the list. INFO is a plist holding
1546 contextual information."
1547 (let* ((type (org-element-property :type plain-list))
1548 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1549 "inparadesc" "asparadesc"))
1550 (paralist-regexp (concat
1551 "\\("
1552 (mapconcat 'identity paralist-types "\\|")
1553 "\\)"))
1554 (attr (mapconcat #'identity
1555 (org-element-property :attr_latex plain-list)
1556 " "))
1557 (latex-type (cond
1558 ((and attr
1559 (string-match
1560 (format "\\<%s\\>" paralist-regexp) attr))
1561 (match-string 1 attr))
1562 ((eq type 'ordered) "enumerate")
1563 ((eq type 'unordered) "itemize")
1564 ((eq type 'descriptive) "description"))))
1565 (org-e-latex--wrap-label
1566 plain-list
1567 (format "\\begin{%s}%s\n%s\\end{%s}"
1568 latex-type
1569 ;; Once special environment, if any, has been removed, the
1570 ;; rest of the attributes will be optional arguments.
1571 ;; They will be put inside square brackets if necessary.
1572 (let ((opt (replace-regexp-in-string
1573 (format " *%s *" paralist-regexp) "" attr)))
1574 (cond ((string= opt "") "")
1575 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1576 (t (format "[%s]" opt))))
1577 contents
1578 latex-type))))
1581 ;;;; Plain Text
1583 (defun org-e-latex-plain-text (text info)
1584 "Transcode a TEXT string from Org to LaTeX.
1585 TEXT is the string to transcode. INFO is a plist holding
1586 contextual information."
1587 ;; Protect %, #, &, $, ~, ^, _, { and }.
1588 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1589 (setq text
1590 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1591 ;; Protect \
1592 (setq text (replace-regexp-in-string
1593 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1594 "$\\backslash$" text nil t 1))
1595 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1596 (let ((case-fold-search nil)
1597 (start 0))
1598 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1599 (setq text (replace-match
1600 (format "\\%s{}" (match-string 1 text)) nil t text)
1601 start (match-end 0))))
1602 ;; Handle quotation marks
1603 (setq text (org-e-latex--quotation-marks text info))
1604 ;; Convert special strings.
1605 (when (plist-get info :with-special-strings)
1606 (while (string-match (regexp-quote "...") text)
1607 (setq text (replace-match "\\ldots{}" nil t text))))
1608 ;; Handle break preservation if required.
1609 (when (plist-get info :preserve-breaks)
1610 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1611 text)))
1612 ;; Return value.
1613 text)
1616 ;;;; Property Drawer
1618 (defun org-e-latex-property-drawer (property-drawer contents info)
1619 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1620 CONTENTS is nil. INFO is a plist holding contextual
1621 information."
1622 ;; The property drawer isn't exported but we want separating blank
1623 ;; lines nonetheless.
1627 ;;;; Quote Block
1629 (defun org-e-latex-quote-block (quote-block contents info)
1630 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1631 CONTENTS holds the contents of the block. INFO is a plist
1632 holding contextual information."
1633 (org-e-latex--wrap-label
1634 quote-block
1635 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1638 ;;;; Quote Section
1640 (defun org-e-latex-quote-section (quote-section contents info)
1641 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1642 CONTENTS is nil. INFO is a plist holding contextual information."
1643 (let ((value (org-remove-indentation
1644 (org-element-property :value quote-section))))
1645 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1648 ;;;; Section
1650 (defun org-e-latex-section (section contents info)
1651 "Transcode a SECTION element from Org to LaTeX.
1652 CONTENTS holds the contents of the section. INFO is a plist
1653 holding contextual information."
1654 contents)
1657 ;;;; Radio Target
1659 (defun org-e-latex-radio-target (radio-target text info)
1660 "Transcode a RADIO-TARGET object from Org to LaTeX.
1661 TEXT is the text of the target. INFO is a plist holding
1662 contextual information."
1663 (format "\\label{%s}%s"
1664 (org-export-solidify-link-text
1665 (org-element-property :value radio-target))
1666 text))
1669 ;;;; Special Block
1671 (defun org-e-latex-special-block (special-block contents info)
1672 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1673 CONTENTS holds the contents of the block. INFO is a plist
1674 holding contextual information."
1675 (let ((type (downcase (org-element-property :type special-block))))
1676 (org-e-latex--wrap-label
1677 special-block
1678 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
1681 ;;;; Src Block
1683 (defun org-e-latex-src-block (src-block contents info)
1684 "Transcode a SRC-BLOCK element from Org to LaTeX.
1685 CONTENTS holds the contents of the item. INFO is a plist holding
1686 contextual information."
1687 (let* ((lang (org-element-property :language src-block))
1688 (caption (org-element-property :caption src-block))
1689 (label (org-element-property :name src-block))
1690 (custom-env (and lang
1691 (cadr (assq (intern lang)
1692 org-e-latex-custom-lang-environments))))
1693 (num-start (case (org-element-property :number-lines src-block)
1694 (continued (org-export-get-loc src-block info))
1695 (new 0)))
1696 (retain-labels (org-element-property :retain-labels src-block)))
1697 (cond
1698 ;; Case 1. No source fontification.
1699 ((not org-e-latex-listings)
1700 (let ((caption-str (org-e-latex--caption/label-string caption label info))
1701 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
1702 (format
1703 (or float-env "%s")
1704 (concat caption-str
1705 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1706 (org-export-format-code-default src-block info))))))
1707 ;; Case 2. Custom environment.
1708 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
1709 custom-env
1710 (org-export-format-code-default src-block info)
1711 custom-env))
1712 ;; Case 3. Use minted package.
1713 ((eq org-e-latex-listings 'minted)
1714 (let ((float-env (when (or label caption)
1715 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1716 (org-e-latex--caption/label-string
1717 caption label info))))
1718 (body
1719 (format
1720 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1721 ;; Options.
1722 (org-e-latex--make-option-string
1723 (if (not num-start) org-e-latex-minted-options
1724 (append `(("linenos")
1725 ("firstnumber" ,(number-to-string (1+ num-start))))
1726 org-e-latex-minted-options)))
1727 ;; Language.
1728 (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
1729 ;; Source code.
1730 (let* ((code-info (org-export-unravel-code src-block))
1731 (max-width
1732 (apply 'max
1733 (mapcar 'length
1734 (org-split-string (car code-info) "\n")))))
1735 (org-export-format-code
1736 (car code-info)
1737 (lambda (loc num ref)
1738 (concat
1740 (when ref
1741 ;; Ensure references are flushed to the right,
1742 ;; separated with 6 spaces from the widest line
1743 ;; of code.
1744 (concat (make-string (+ (- max-width (length loc)) 6) ? )
1745 (format "(%s)" ref)))))
1746 nil (and retain-labels (cdr code-info)))))))
1747 ;; Return value.
1748 (if float-env (format float-env body) body)))
1749 ;; Case 4. Use listings package.
1751 (let ((lst-lang
1752 (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
1753 (caption-str
1754 (when caption
1755 (let ((main (org-export-secondary-string
1756 (car caption) 'e-latex info)))
1757 (if (not (cdr caption)) (format "{%s}" main)
1758 (format
1759 "{[%s]%s}"
1760 (org-export-secondary-string (cdr caption) 'e-latex info)
1761 main))))))
1762 (concat
1763 ;; Options.
1764 (format "\\lstset{%s}\n"
1765 (org-e-latex--make-option-string
1766 (append org-e-latex-listings-options
1767 `(("language" ,lst-lang))
1768 (when label `(("label" ,label)))
1769 (when caption-str `(("caption" ,caption-str)))
1770 (cond ((not num-start) '(("numbers" "none")))
1771 ((zerop num-start) '(("numbers" "left")))
1772 (t `(("numbers" "left")
1773 ("firstnumber"
1774 ,(number-to-string (1+ num-start)))))))))
1775 ;; Source code.
1776 (format
1777 "\\begin{lstlisting}\n%s\\end{lstlisting}"
1778 (let* ((code-info (org-export-unravel-code src-block))
1779 (max-width
1780 (apply 'max
1781 (mapcar 'length
1782 (org-split-string (car code-info) "\n")))))
1783 (org-export-format-code
1784 (car code-info)
1785 (lambda (loc num ref)
1786 (concat
1788 (when ref
1789 ;; Ensure references are flushed to the right,
1790 ;; separated with 6 spaces from the widest line of
1791 ;; code
1792 (concat (make-string (+ (- max-width (length loc)) 6) ? )
1793 (format "(%s)" ref)))))
1794 nil (and retain-labels (cdr code-info)))))))))))
1797 ;;;; Statistics Cookie
1799 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
1800 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1801 CONTENTS is nil. INFO is a plist holding contextual information."
1802 (org-element-property :value statistics-cookie))
1805 ;;;; Subscript
1807 (defun org-e-latex-subscript (subscript contents info)
1808 "Transcode a SUBSCRIPT object from Org to LaTeX.
1809 CONTENTS is the contents of the object. INFO is a plist holding
1810 contextual information."
1811 (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
1814 ;;;; Superscript
1816 (defun org-e-latex-superscript (superscript contents info)
1817 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1818 CONTENTS is the contents of the object. INFO is a plist holding
1819 contextual information."
1820 (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
1823 ;;;; Table
1825 (defun org-e-latex-table--format-string (table table-info info)
1826 "Return an appropriate format string for TABLE.
1828 TABLE-INFO is the plist containing format info about the table,
1829 as returned by `org-export-table-format-info'. INFO is a plist
1830 used as a communication channel.
1832 The format string leaves one placeholder for the body of the
1833 table."
1834 (let* ((label (org-element-property :name table))
1835 (caption (org-e-latex--caption/label-string
1836 (org-element-property :caption table) label info))
1837 (attr (mapconcat 'identity
1838 (org-element-property :attr_latex table)
1839 " "))
1840 ;; Determine alignment string.
1841 (alignment (org-e-latex-table--align-string attr table-info))
1842 ;; Determine environment for the table: longtable, tabular...
1843 (table-env (cond
1844 ((not attr) org-e-latex-default-table-environment)
1845 ((string-match "\\<longtable\\>" attr) "longtable")
1846 ((string-match "\\<tabular.?\\>" attr)
1847 (org-match-string-no-properties 0 attr))
1848 (t org-e-latex-default-table-environment)))
1849 ;; If table is a float, determine environment: table, table*
1850 ;; or sidewaystable.
1851 (float-env (cond
1852 ((string= "longtable" table-env) nil)
1853 ((and attr (string-match "\\<sidewaystable\\>" attr))
1854 "sidewaystables")
1855 ((and attr
1856 (or (string-match (regexp-quote "table*") attr)
1857 (string-match "\\<multicolumn\\>" attr)))
1858 "table*")
1859 ((or (not (string= caption "")) label) "table")))
1860 ;; Extract others display options.
1861 (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
1862 (org-match-string-no-properties 1 attr)))
1863 (placement
1864 (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1865 (org-match-string-no-properties 1 attr)
1866 (format "[%s]" org-e-latex-default-figure-position))))
1867 ;; Prepare the final format string for the table.
1868 (cond
1869 ;; Longtable.
1870 ((string= "longtable" table-env)
1871 (format
1872 "\\begin{longtable}{%s}\n%s\n%%s\n%s\\end{longtable}"
1873 alignment
1874 (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
1875 (concat (org-trim caption) "\\\\"))
1876 (if (or org-e-latex-table-caption-above (string= "" caption)) ""
1877 (concat (org-trim caption) "\\\\\n"))))
1878 ;; Others.
1879 (t (concat (when float-env
1880 (concat
1881 (format "\\begin{%s}%s\n" float-env placement)
1882 (if org-e-latex-table-caption-above caption "")))
1883 (when org-e-latex-tables-centered "\\begin{center}\n")
1884 (format "\\begin{%s}%s{%s}\n%%s\n\\end{%s}"
1885 table-env
1886 (if width (format "{%s}" width) "") alignment table-env)
1887 (when org-e-latex-tables-centered "\n\\end{center}")
1888 (when float-env
1889 (concat (if org-e-latex-table-caption-above "" caption)
1890 (format "\n\\end{%s}" float-env))))))))
1892 (defun org-e-latex-table--align-string (attr table-info)
1893 "Return an appropriate LaTeX alignment string.
1894 ATTR is a string containing table's LaTeX specific attributes.
1895 TABLE-INFO is the plist containing format info about the table,
1896 as returned by `org-export-table-format-info'."
1897 (or (and attr
1898 (string-match "\\<align=\\(\\S-+\\)" attr)
1899 (match-string 1 attr))
1900 (let* ((align (copy-sequence (plist-get table-info :alignment)))
1901 (colgroups (copy-sequence (plist-get table-info :column-groups)))
1902 (cols (length align))
1903 (separators (make-vector (1+ cols) "")))
1904 ;; Ignore the first column if it's special.
1905 (when (plist-get table-info :special-column-p)
1906 (aset align 0 "") (aset colgroups 0 nil))
1907 (let ((col 0))
1908 (mapc (lambda (el)
1909 (let ((gr (aref colgroups col)))
1910 (when (memq gr '(start start-end))
1911 (aset separators col "|"))
1912 (when (memq gr '(end start-end))
1913 (aset separators (1+ col) "|")))
1914 (incf col))
1915 align))
1916 ;; Build the LaTeX specific alignment string.
1917 (loop for al across align
1918 for sep across separators
1919 concat (concat sep al) into output
1920 finally return (concat output (aref separators cols))))))
1922 (defun org-e-latex-table (table contents info)
1923 "Transcode a TABLE element from Org to LaTeX.
1924 CONTENTS is nil. INFO is a plist holding contextual information."
1925 (let ((attr (mapconcat #'identity
1926 (org-element-property :attr_latex table)
1927 " "))
1928 (raw-table (org-element-property :raw-table table)))
1929 (cond
1930 ;; Case 1: verbatim table.
1931 ((or org-e-latex-tables-verbatim
1932 (and attr (string-match "\\<verbatim\\>" attr)))
1933 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
1934 (org-export-clean-table
1935 raw-table
1936 (plist-get (org-export-table-format-info raw-table)
1937 :special-column-p))))
1938 ;; Case 2: table.el table. Convert it using appropriate tools.
1939 ((eq (org-element-property :type table) 'table.el)
1940 (require 'table)
1941 ;; Ensure "*org-export-table*" buffer is empty.
1942 (with-current-buffer (get-buffer-create "*org-export-table*")
1943 (erase-buffer))
1944 (let ((output (with-temp-buffer
1945 (insert raw-table)
1946 (goto-char 1)
1947 (re-search-forward "^[ \t]*|[^|]" nil t)
1948 (table-generate-source 'latex "*org-export-table*")
1949 (with-current-buffer "*org-export-table*"
1950 (org-trim (buffer-string))))))
1951 (kill-buffer (get-buffer "*org-export-table*"))
1952 ;; Remove left out comments.
1953 (while (string-match "^%.*\n" output)
1954 (setq output (replace-match "" t t output)))
1955 ;; When the "rmlines" attribute is provided, remove all hlines
1956 ;; but the the one separating heading from the table body.
1957 (when (and attr (string-match "\\<rmlines\\>" attr))
1958 (let ((n 0) (pos 0))
1959 (while (and (< (length output) pos)
1960 (setq pos (string-match "^\\\\hline\n?" output pos)))
1961 (incf n)
1962 (unless (= n 2)
1963 (setq output (replace-match "" nil nil output))))))
1964 (if (not org-e-latex-tables-centered) output
1965 (format "\\begin{center}\n%s\n\\end{center}" output))))
1966 ;; Case 3: Standard table.
1968 (let* ((table-info (org-export-table-format-info raw-table))
1969 (columns-number (length (plist-get table-info :alignment)))
1970 (longtablep (and attr (string-match "\\<longtable\\>" attr)))
1971 (booktabsp
1972 (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
1973 org-e-latex-tables-booktabs))
1974 ;; CLEAN-TABLE is a table turned into a list, much like
1975 ;; `org-table-to-lisp', with special column and
1976 ;; formatting cookies removed, and cells already
1977 ;; transcoded.
1978 (clean-table
1979 (mapcar
1980 (lambda (row)
1981 (if (string-match org-table-hline-regexp row) 'hline
1982 (mapcar
1983 (lambda (cell)
1984 (org-export-secondary-string
1985 (org-element-parse-secondary-string
1986 cell
1987 (cdr (assq 'table org-element-string-restrictions)))
1988 'e-latex info))
1989 (org-split-string row "[ \t]*|[ \t]*"))))
1990 (org-split-string
1991 (org-export-clean-table
1992 raw-table (plist-get table-info :special-column-p))
1993 "\n"))))
1994 ;; If BOOKTABSP is non-nil, remove any rule at the beginning
1995 ;; and the end of the table, since booktabs' special rules
1996 ;; will be inserted instead.
1997 (when booktabsp
1998 (when (eq (car clean-table) 'hline)
1999 (setq clean-table (cdr clean-table)))
2000 (when (eq (car (last clean-table)) 'hline)
2001 (setq clean-table (butlast clean-table))))
2002 ;; Convert ROWS to send them to `orgtbl-to-latex'. In
2003 ;; particular, send each cell to
2004 ;; `org-element-parse-secondary-string' to expand any Org
2005 ;; object within. Eventually, flesh the format string out
2006 ;; with the table.
2007 (format
2008 (org-e-latex-table--format-string table table-info info)
2009 (orgtbl-to-latex
2010 clean-table
2011 ;; Parameters passed to `orgtbl-to-latex'.
2012 `(:tstart ,(and booktabsp "\\toprule")
2013 :tend ,(and booktabsp "\\bottomrule")
2014 :hline ,(if booktabsp "\\midrule" "\\hline")
2015 ;; Longtable environment requires specific header
2016 ;; lines end string.
2017 :hlend ,(and longtablep
2018 (format "\\\\
2020 \\endhead
2021 %s\\multicolumn{%d}{r}{Continued on next page}\\\\
2022 \\endfoot
2023 \\endlastfoot"
2024 (if booktabsp "\\midrule" "\\hline")
2025 (if booktabsp "\\midrule" "\\hline")
2026 columns-number))))))))))
2029 ;;;; Target
2031 (defun org-e-latex-target (target contents info)
2032 "Transcode a TARGET object from Org to LaTeX.
2033 CONTENTS is nil. INFO is a plist holding contextual
2034 information."
2035 (format "\\label{%s}"
2036 (org-export-solidify-link-text (org-element-property :value target))))
2039 ;;;; Time-stamp
2041 (defun org-e-latex-time-stamp (time-stamp contents info)
2042 "Transcode a TIME-STAMP object from Org to LaTeX.
2043 CONTENTS is nil. INFO is a plist holding contextual
2044 information."
2045 (let ((value (org-element-property :value time-stamp))
2046 (type (org-element-property :type time-stamp))
2047 (appt-type (org-element-property :appt-type time-stamp)))
2048 (concat (cond ((eq appt-type 'scheduled)
2049 (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
2050 ((eq appt-type 'deadline)
2051 (format "\\textbf{\\textsc{%s}} " org-deadline-string))
2052 ((eq appt-type 'closed)
2053 (format "\\textbf{\\textsc{%s}} " org-closed-string)))
2054 (cond ((memq type '(active active-range))
2055 (format org-e-latex-active-timestamp-format value))
2056 ((memq type '(inactive inactive-range))
2057 (format org-e-latex-inactive-timestamp-format value))
2059 (format org-e-latex-diary-timestamp-format value))))))
2062 ;;;; Verbatim
2064 (defun org-e-latex-verbatim (verbatim contents info)
2065 "Transcode a VERBATIM object from Org to LaTeX.
2066 CONTENTS is nil. INFO is a plist used as a communication
2067 channel."
2068 (let ((fmt (cdr (assoc (org-element-property :marker verbatim)
2069 org-e-latex-emphasis-alist)))
2070 (value (org-element-property :value verbatim)))
2071 (cond
2072 ;; Handle the `verb' special case.
2073 ((eq 'verb fmt)
2074 (let ((separator (org-e-latex--find-verb-separator value)))
2075 (concat "\\verb" separator value separator)))
2076 ;; Handle the `protectedtexttt' special case.
2077 ((eq 'protectedtexttt fmt)
2078 (let ((start 0)
2079 (trans '(("\\" . "\\textbackslash{}")
2080 ("~" . "\\textasciitilde{}")
2081 ("^" . "\\textasciicircum{}")))
2082 (rtn "")
2083 char)
2084 (while (string-match "[\\{}$%&_#~^]" value)
2085 (setq char (match-string 0 value))
2086 (if (> (match-beginning 0) 0)
2087 (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
2088 (setq value (substring value (1+ (match-beginning 0))))
2089 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
2090 rtn (concat rtn char)))
2091 (setq value (concat rtn value)
2092 fmt "\\texttt{%s}")
2093 (while (string-match "--" value)
2094 (setq value (replace-match "-{}-" t t value)))
2095 (format fmt value)))
2096 ;; Else use format string.
2097 (t (format fmt value)))))
2100 ;;;; Verse Block
2102 (defun org-e-latex-verse-block (verse-block contents info)
2103 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2104 CONTENTS is nil. INFO is a plist holding contextual information."
2105 (org-e-latex--wrap-label
2106 verse-block
2107 ;; In a verse environment, add a line break to each newline
2108 ;; character and change each white space at beginning of a line
2109 ;; into a space of 1 em. Also change each blank line with
2110 ;; a vertical space of 1 em.
2111 (progn
2112 (setq contents (replace-regexp-in-string
2113 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2114 (replace-regexp-in-string
2115 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
2116 (org-remove-indentation
2117 (org-export-secondary-string
2118 (org-element-property :value verse-block)
2119 'e-latex info)))))
2120 (while (string-match "^[ \t]+" contents)
2121 (let ((new-str (format "\\hspace*{%dem}"
2122 (length (match-string 0 contents)))))
2123 (setq contents (replace-match new-str nil t contents))))
2124 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2128 ;;; Interactive functions
2130 (defun org-e-latex-export-to-latex
2131 (&optional subtreep visible-only body-only ext-plist pub-dir)
2132 "Export current buffer to a LaTeX file.
2134 If narrowing is active in the current buffer, only export its
2135 narrowed part.
2137 If a region is active, export that region.
2139 When optional argument SUBTREEP is non-nil, export the sub-tree
2140 at point, extracting information from the headline properties
2141 first.
2143 When optional argument VISIBLE-ONLY is non-nil, don't export
2144 contents of hidden elements.
2146 When optional argument BODY-ONLY is non-nil, only write code
2147 between \"\\begin{document}\" and \"\\end{document}\".
2149 EXT-PLIST, when provided, is a property list with external
2150 parameters overriding Org default settings, but still inferior to
2151 file-local settings.
2153 When optional argument PUB-DIR is set, use it as the publishing
2154 directory.
2156 Return output file's name."
2157 (interactive)
2158 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
2159 (org-export-to-file
2160 'e-latex outfile subtreep visible-only body-only ext-plist)))
2162 (defun org-e-latex-export-to-pdf
2163 (&optional subtreep visible-only body-only ext-plist pub-dir)
2164 "Export current buffer to LaTeX then process through to PDF.
2166 If narrowing is active in the current buffer, only export its
2167 narrowed part.
2169 If a region is active, export that region.
2171 When optional argument SUBTREEP is non-nil, export the sub-tree
2172 at point, extracting information from the headline properties
2173 first.
2175 When optional argument VISIBLE-ONLY is non-nil, don't export
2176 contents of hidden elements.
2178 When optional argument BODY-ONLY is non-nil, only write code
2179 between \"\\begin{document}\" and \"\\end{document}\".
2181 EXT-PLIST, when provided, is a property list with external
2182 parameters overriding Org default settings, but still inferior to
2183 file-local settings.
2185 When optional argument PUB-DIR is set, use it as the publishing
2186 directory.
2188 Return PDF file's name."
2189 (interactive)
2190 (org-e-latex-compile
2191 (org-e-latex-export-to-latex
2192 subtreep visible-only body-only ext-plist pub-dir)))
2194 (defun org-e-latex-compile (texfile)
2195 "Compile a TeX file.
2197 TEXFILE is the name of the file being compiled. Processing is
2198 done through the command specified in `org-e-latex-pdf-process'.
2200 Return PDF file name or an error if it couldn't be produced."
2201 (let* ((wconfig (current-window-configuration))
2202 (texfile (file-truename texfile))
2203 (base (file-name-sans-extension texfile))
2204 errors)
2205 (message (format "Processing LaTeX file %s ..." texfile))
2206 (unwind-protect
2207 (progn
2208 (cond
2209 ;; A function is provided: Apply it.
2210 ((functionp org-e-latex-pdf-process)
2211 (funcall org-e-latex-pdf-process (shell-quote-argument texfile)))
2212 ;; A list is provided: Replace %b, %f and %o with appropriate
2213 ;; values in each command before applying it. Output is
2214 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2215 ((consp org-e-latex-pdf-process)
2216 (let* ((out-dir (or (file-name-directory texfile) "./"))
2217 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2218 (mapc
2219 (lambda (command)
2220 (shell-command
2221 (replace-regexp-in-string
2222 "%b" (shell-quote-argument base)
2223 (replace-regexp-in-string
2224 "%f" (shell-quote-argument texfile)
2225 (replace-regexp-in-string
2226 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2227 outbuf))
2228 org-e-latex-pdf-process)
2229 ;; Collect standard errors from output buffer.
2230 (setq errors (org-e-latex-collect-errors outbuf))))
2231 (t (error "No valid command to process to PDF")))
2232 (let ((pdffile (concat base ".pdf")))
2233 ;; Check for process failure. Provide collected errors if
2234 ;; possible.
2235 (if (not (file-exists-p pdffile))
2236 (error (concat (format "PDF file %s wasn't produced" pdffile)
2237 (when errors (concat ": " errors))))
2238 ;; Else remove log files, when specified, and signal end of
2239 ;; process to user, along with any error encountered.
2240 (when org-e-latex-remove-logfiles
2241 (dolist (ext org-e-latex-logfiles-extensions)
2242 (let ((file (concat base "." ext)))
2243 (when (file-exists-p file) (delete-file file)))))
2244 (message (concat "Process completed"
2245 (if (not errors) "."
2246 (concat " with errors: " errors)))))
2247 ;; Return output file name.
2248 pdffile))
2249 (set-window-configuration wconfig))))
2251 (defun org-e-latex-collect-errors (buffer)
2252 "Collect some kind of errors from \"pdflatex\" command output.
2254 BUFFER is the buffer containing output.
2256 Return collected error types as a string, or nil if there was
2257 none."
2258 (with-current-buffer buffer
2259 (save-excursion
2260 (goto-char (point-max))
2261 ;; Find final "pdflatex" run.
2262 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
2263 (let ((case-fold-search t)
2264 (errors ""))
2265 (when (save-excursion
2266 (re-search-forward "Reference.*?undefined" nil t))
2267 (setq errors (concat errors " [undefined reference]")))
2268 (when (save-excursion
2269 (re-search-forward "Citation.*?undefined" nil t))
2270 (setq errors (concat errors " [undefined citation]")))
2271 (when (save-excursion
2272 (re-search-forward "Undefined control sequence" nil t))
2273 (setq errors (concat errors " [undefined control sequence]")))
2274 (when (save-excursion
2275 (re-search-forward "^! LaTeX.*?Error" nil t))
2276 (setq errors (concat errors " [LaTeX error]")))
2277 (when (save-excursion
2278 (re-search-forward "^! Package.*?Error" nil t))
2279 (setq errors (concat errors " [package error]")))
2280 (and (org-string-nw-p errors) (org-trim errors)))))))
2283 (provide 'org-e-latex)
2284 ;;; org-e-latex.el ends here