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