org-export: Secondary strings are transcoded with `org-export-data'
[org-mode/org-mode-NeilSmithlineMods.git] / contrib / lisp / org-e-latex.el
blob70f8ae216c2c80a6ba8c14a21b86c799af0cc42c
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 string, separated with colons \(string 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 \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
288 :group 'org-export-e-latex
289 :type 'function)
292 ;;;; Footnotes
294 (defcustom org-e-latex-footnote-separator "\\textsuperscript{,}\\,"
295 "Text used to separate footnotes."
296 :group 'org-export-e-latex
297 :type 'string)
300 ;;;; Timestamps
302 (defcustom org-e-latex-active-timestamp-format "\\textit{%s}"
303 "A printf format string to be applied to active timestamps."
304 :group 'org-export-e-latex
305 :type 'string)
307 (defcustom org-e-latex-inactive-timestamp-format "\\textit{%s}"
308 "A printf format string to be applied to inactive timestamps."
309 :group 'org-export-e-latex
310 :type 'string)
312 (defcustom org-e-latex-diary-timestamp-format "\\textit{%s}"
313 "A printf format string to be applied to diary timestamps."
314 :group 'org-export-e-latex
315 :type 'string)
318 ;;;; Links
320 (defcustom org-e-latex-image-default-option "width=.9\\linewidth"
321 "Default option for images."
322 :group 'org-export-e-latex
323 :type 'string)
325 (defcustom org-e-latex-default-figure-position "htb"
326 "Default position for latex figures."
327 :group 'org-export-e-latex
328 :type 'string)
330 (defcustom org-e-latex-inline-image-rules
331 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\)\\'"))
332 "Rules characterizing image files that can be inlined into LaTeX.
334 A rule consists in an association whose key is the type of link
335 to consider, and value is a regexp that will be matched against
336 link's path.
338 Note that, by default, the image extension *actually* allowed
339 depend on the way the LaTeX file is processed. When used with
340 pdflatex, pdf, jpg and png images are OK. When processing
341 through dvi to Postscript, only ps and eps are allowed. The
342 default we use here encompasses both."
343 :group 'org-export-e-latex
344 :type '(alist :key-type (string :tag "Type")
345 :value-type (regexp :tag "Path")))
348 ;;;; Tables
350 (defcustom org-e-latex-default-table-environment "tabular"
351 "Default environment used to build tables."
352 :group 'org-export-e-latex
353 :type 'string)
355 (defcustom org-e-latex-tables-centered t
356 "When non-nil, tables are exported in a center environment."
357 :group 'org-export-e-latex
358 :type 'boolean)
360 (defcustom org-e-latex-tables-verbatim nil
361 "When non-nil, tables are exported verbatim."
362 :group 'org-export-e-latex
363 :type 'boolean)
365 (defcustom org-e-latex-tables-booktabs nil
366 "When non-nil, display tables in a formal \"booktabs\" style.
367 This option assumes that the \"booktabs\" package is properly
368 loaded in the header of the document. This value can be ignored
369 locally with \"booktabs=yes\" and \"booktabs=no\" LaTeX
370 attributes."
371 :group 'org-export-e-latex
372 :type 'boolean)
374 (defcustom org-e-latex-table-caption-above t
375 "When non-nil, place caption string at the beginning of the table.
376 Otherwise, place it near the end."
377 :group 'org-export-e-latex
378 :type 'boolean)
380 (defcustom org-e-latex-table-scientific-notation "%s\\,(%s)"
381 "Format string to display numbers in scientific notation.
382 The format should have \"%s\" twice, for mantissa and exponent
383 \(i.e. \"%s\\\\times10^{%s}\").
385 When nil, no transformation is made."
386 :group 'org-export-e-latex
387 :type '(choice
388 (string :tag "Format string")
389 (const :tag "No formatting")))
392 ;;;; Text markup
394 (defcustom org-e-latex-text-markup-alist '((bold . "\\textbf{%s}")
395 (code . verb)
396 (italic . "\\emph{%s}")
397 (strike-through . "\\st{%s}")
398 (underline . "\\underline{%s}")
399 (verbatim . protectedtexttt))
400 "Alist of LaTeX expressions to convert text markup.
402 The key must be a symbol among `bold', `code', `italic',
403 `strike-through', `underline' and `verbatim'. The value is
404 a formatting string to wrap fontified text with.
406 Value can also be set to the following symbols: `verb' and
407 `protectedtexttt'. For the former, Org will use \"\\verb\" to
408 create a format string and select a delimiter character that
409 isn't in the string. For the latter, Org will use \"\\texttt\"
410 to typeset and try to protect special characters.
412 If no association can be found for a given markup, text will be
413 returned as-is."
414 :group 'org-export-e-latex
415 :type 'alist
416 :options '(bold code italic strike-through underline verbatim))
419 ;;;; Drawers
421 (defcustom org-e-latex-format-drawer-function nil
422 "Function called to format a drawer in LaTeX code.
424 The function must accept two parameters:
425 NAME the drawer name, like \"LOGBOOK\"
426 CONTENTS the contents of the drawer.
428 The function should return the string to be exported.
430 For example, the variable could be set to the following function
431 in order to mimic default behaviour:
433 \(defun org-e-latex-format-drawer-default \(name contents\)
434 \"Format a drawer element for LaTeX export.\"
435 contents\)"
436 :group 'org-export-e-latex
437 :type 'function)
440 ;;;; Inlinetasks
442 (defcustom org-e-latex-format-inlinetask-function nil
443 "Function called to format an inlinetask in LaTeX code.
445 The function must accept six parameters:
446 TODO the todo keyword, as a string
447 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
448 PRIORITY the inlinetask priority, as a string
449 NAME the inlinetask name, as a string.
450 TAGS the inlinetask tags, as a string.
451 CONTENTS the contents of the inlinetask, as a string.
453 The function should return the string to be exported.
455 For example, the variable could be set to the following function
456 in order to mimic default behaviour:
458 \(defun org-e-latex-format-inlinetask \(todo type priority name tags contents\)
459 \"Format an inline task element for LaTeX export.\"
460 \(let \(\(full-title
461 \(concat
462 \(when todo
463 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
464 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
465 title
466 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
467 \(format \(concat \"\\\\begin{center}\\n\"
468 \"\\\\fbox{\\n\"
469 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
470 \"%s\\n\\n\"
471 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
472 \"%s\"
473 \"\\\\end{minipage}}\"
474 \"\\\\end{center}\"\)
475 full-title contents\)\)"
476 :group 'org-export-e-latex
477 :type 'function)
480 ;; Src blocks
482 (defcustom org-e-latex-listings nil
483 "Non-nil means export source code using the listings package.
484 This package will fontify source code, possibly even with color.
485 If you want to use this, you also need to make LaTeX use the
486 listings package, and if you want to have color, the color
487 package. Just add these to `org-export-latex-packages-alist',
488 for example using customize, or with something like:
490 \(require 'org-e-latex)
491 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"listings\"))
492 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"color\"))
494 Alternatively,
496 \(setq org-e-latex-listings 'minted)
498 causes source code to be exported using the minted package as
499 opposed to listings. If you want to use minted, you need to add
500 the minted package to `org-export-latex-packages-alist', for
501 example using customize, or with
503 \(require 'org-e-latex)
504 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"minted\"))
506 In addition, it is necessary to install pygments
507 \(http://pygments.org), and to configure the variable
508 `org-e-latex-pdf-process' so that the -shell-escape option is
509 passed to pdflatex."
510 :group 'org-export-e-latex
511 :type '(choice
512 (const :tag "Use listings" t)
513 (const :tag "Use minted" 'minted)
514 (const :tag "Export verbatim" nil)))
516 (defcustom org-e-latex-listings-langs
517 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
518 (c "C") (cc "C++")
519 (fortran "fortran")
520 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
521 (html "HTML") (xml "XML")
522 (tex "TeX") (latex "TeX")
523 (shell-script "bash")
524 (gnuplot "Gnuplot")
525 (ocaml "Caml") (caml "Caml")
526 (sql "SQL") (sqlite "sql"))
527 "Alist mapping languages to their listing language counterpart.
528 The key is a symbol, the major mode symbol without the \"-mode\".
529 The value is the string that should be inserted as the language
530 parameter for the listings package. If the mode name and the
531 listings name are the same, the language does not need an entry
532 in this list - but it does not hurt if it is present."
533 :group 'org-export-e-latex
534 :type '(repeat
535 (list
536 (symbol :tag "Major mode ")
537 (string :tag "Listings language"))))
539 (defcustom org-e-latex-listings-options nil
540 "Association list of options for the latex listings package.
542 These options are supplied as a comma-separated list to the
543 \\lstset command. Each element of the association list should be
544 a list containing two strings: the name of the option, and the
545 value. For example,
547 (setq org-e-latex-listings-options
548 '((\"basicstyle\" \"\\small\")
549 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
551 will typeset the code in a small size font with underlined, bold
552 black keywords.
554 Note that the same options will be applied to blocks of all
555 languages."
556 :group 'org-export-e-latex
557 :type '(repeat
558 (list
559 (string :tag "Listings option name ")
560 (string :tag "Listings option value"))))
562 (defcustom org-e-latex-minted-langs
563 '((emacs-lisp "common-lisp")
564 (cc "c++")
565 (cperl "perl")
566 (shell-script "bash")
567 (caml "ocaml"))
568 "Alist mapping languages to their minted language counterpart.
569 The key is a symbol, the major mode symbol without the \"-mode\".
570 The value is the string that should be inserted as the language
571 parameter for the minted package. If the mode name and the
572 listings name are the same, the language does not need an entry
573 in this list - but it does not hurt if it is present.
575 Note that minted uses all lower case for language identifiers,
576 and that the full list of language identifiers can be obtained
577 with:
579 pygmentize -L lexers"
580 :group 'org-export-e-latex
581 :type '(repeat
582 (list
583 (symbol :tag "Major mode ")
584 (string :tag "Minted language"))))
586 (defcustom org-e-latex-minted-options nil
587 "Association list of options for the latex minted package.
589 These options are supplied within square brackets in
590 \\begin{minted} environments. Each element of the alist should
591 be a list containing two strings: the name of the option, and the
592 value. For example,
594 \(setq org-e-latex-minted-options
595 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
597 will result in src blocks being exported with
599 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
601 as the start of the minted environment. Note that the same
602 options will be applied to blocks of all languages."
603 :group 'org-export-e-latex
604 :type '(repeat
605 (list
606 (string :tag "Minted option name ")
607 (string :tag "Minted option value"))))
609 (defvar org-e-latex-custom-lang-environments nil
610 "Alist mapping languages to language-specific LaTeX environments.
612 It is used during export of src blocks by the listings and minted
613 latex packages. For example,
615 \(setq org-e-latex-custom-lang-environments
616 '\(\(python \"pythoncode\"\)\)\)
618 would have the effect that if org encounters begin_src python
619 during latex export it will output
621 \\begin{pythoncode}
622 <src block body>
623 \\end{pythoncode}")
626 ;;;; Plain text
628 (defcustom org-e-latex-quotes
629 '(("fr"
630 ("\\(\\s-\\|[[(]\\|^\\)\"" . "«~")
631 ("\\(\\S-\\)\"" . "~»")
632 ("\\(\\s-\\|(\\|^\\)'" . "'"))
633 ("en"
634 ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
635 ("\\(\\S-\\)\"" . "''")
636 ("\\(\\s-\\|(\\|^\\)'" . "`")))
637 "Alist for quotes to use when converting english double-quotes.
639 The CAR of each item in this alist is the language code.
640 The CDR of each item in this alist is a list of three CONS:
641 - the first CONS defines the opening quote;
642 - the second CONS defines the closing quote;
643 - the last CONS defines single quotes.
645 For each item in a CONS, the first string is a regexp
646 for allowed characters before/after the quote, the second
647 string defines the replacement string for this quote."
648 :group 'org-export-e-latex
649 :type '(list
650 (cons :tag "Opening quote"
651 (string :tag "Regexp for char before")
652 (string :tag "Replacement quote "))
653 (cons :tag "Closing quote"
654 (string :tag "Regexp for char after ")
655 (string :tag "Replacement quote "))
656 (cons :tag "Single quote"
657 (string :tag "Regexp for char before")
658 (string :tag "Replacement quote "))))
661 ;;;; Compilation
663 (defcustom org-e-latex-pdf-process
664 '("pdflatex -interaction nonstopmode -output-directory %o %f"
665 "pdflatex -interaction nonstopmode -output-directory %o %f"
666 "pdflatex -interaction nonstopmode -output-directory %o %f")
667 "Commands to process a LaTeX file to a PDF file.
668 This is a list of strings, each of them will be given to the
669 shell as a command. %f in the command will be replaced by the
670 full file name, %b by the file base name \(i.e. without
671 extension) and %o by the base directory of the file.
673 The reason why this is a list is that it usually takes several
674 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
675 does not have a clever mechanism to detect which of these
676 commands have to be run to get to a stable result, and it also
677 does not do any error checking.
679 By default, Org uses 3 runs of `pdflatex' to do the processing.
680 If you have texi2dvi on your system and if that does not cause
681 the infamous egrep/locale bug:
683 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
685 then `texi2dvi' is the superior choice. Org does offer it as one
686 of the customize options.
688 Alternatively, this may be a Lisp function that does the
689 processing, so you could use this to apply the machinery of
690 AUCTeX or the Emacs LaTeX mode. This function should accept the
691 file name as its single argument."
692 :group 'org-export-pdf
693 :type '(choice
694 (repeat :tag "Shell command sequence"
695 (string :tag "Shell command"))
696 (const :tag "2 runs of pdflatex"
697 ("pdflatex -interaction nonstopmode -output-directory %o %f"
698 "pdflatex -interaction nonstopmode -output-directory %o %f"))
699 (const :tag "3 runs of pdflatex"
700 ("pdflatex -interaction nonstopmode -output-directory %o %f"
701 "pdflatex -interaction nonstopmode -output-directory %o %f"
702 "pdflatex -interaction nonstopmode -output-directory %o %f"))
703 (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
704 ("pdflatex -interaction nonstopmode -output-directory %o %f"
705 "bibtex %b"
706 "pdflatex -interaction nonstopmode -output-directory %o %f"
707 "pdflatex -interaction nonstopmode -output-directory %o %f"))
708 (const :tag "texi2dvi"
709 ("texi2dvi -p -b -c -V %f"))
710 (const :tag "rubber"
711 ("rubber -d --into %o %f"))
712 (function)))
714 (defcustom org-e-latex-logfiles-extensions
715 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
716 "The list of file extensions to consider as LaTeX logfiles."
717 :group 'org-export-e-latex
718 :type '(repeat (string :tag "Extension")))
720 (defcustom org-e-latex-remove-logfiles t
721 "Non-nil means remove the logfiles produced by PDF production.
722 These are the .aux, .log, .out, and .toc files."
723 :group 'org-export-e-latex
724 :type 'boolean)
728 ;;; Internal Functions
730 (defun org-e-latex--caption/label-string (caption label info)
731 "Return caption and label LaTeX string for floats.
733 CAPTION is a cons cell of secondary strings, the car being the
734 standard caption and the cdr its short form. LABEL is a string
735 representing the label. INFO is a plist holding contextual
736 information.
738 If there's no caption nor label, return the empty string.
740 For non-floats, see `org-e-latex--wrap-label'."
741 (let ((label-str (if label (format "\\label{%s}" label) "")))
742 (cond
743 ((and (not caption) (not label)) "")
744 ((not caption) (format "\\label{%s}\n" label))
745 ;; Option caption format with short name.
746 ((cdr caption)
747 (format "\\caption[%s]{%s%s}\n"
748 (org-export-data (cdr caption) info)
749 label-str
750 (org-export-data (car caption) info)))
751 ;; Standard caption format.
752 (t (format "\\caption{%s%s}\n"
753 label-str
754 (org-export-data (car caption) info))))))
756 (defun org-e-latex--guess-inputenc (header)
757 "Set the coding system in inputenc to what the buffer is.
759 HEADER is the LaTeX header string.
761 Return the new header."
762 (let* ((cs (or (ignore-errors
763 (latexenc-coding-system-to-inputenc
764 buffer-file-coding-system))
765 "utf8")))
766 (if (not cs) header
767 ;; First translate if that is requested.
768 (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
769 ;; Then find the \usepackage statement and replace the option.
770 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
771 cs header t nil 1))))
773 (defun org-e-latex--find-verb-separator (s)
774 "Return a character not used in string S.
775 This is used to choose a separator for constructs like \\verb."
776 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
777 (loop for c across ll
778 when (not (string-match (regexp-quote (char-to-string c)) s))
779 return (char-to-string c))))
781 (defun org-e-latex--make-option-string (options)
782 "Return a comma separated string of keywords and values.
783 OPTIONS is an alist where the key is the options keyword as
784 a string, and the value a list containing the keyword value, or
785 nil."
786 (mapconcat (lambda (pair)
787 (concat (first pair)
788 (when (> (length (second pair)) 0)
789 (concat "=" (second pair)))))
790 options
791 ","))
793 (defun org-e-latex--quotation-marks (text info)
794 "Export quotation marks depending on language conventions.
795 TEXT is a string containing quotation marks to be replaced. INFO
796 is a plist used as a communication channel."
797 (mapc (lambda(l)
798 (let ((start 0))
799 (while (setq start (string-match (car l) text start))
800 (let ((new-quote (concat (match-string 1 text) (cdr l))))
801 (setq text (replace-match new-quote t t text))))))
802 (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
803 ;; Falls back on English.
804 (assoc "en" org-e-latex-quotes))))
805 text)
807 (defun org-e-latex--wrap-label (element output)
808 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
809 This function shouldn't be used for floats. See
810 `org-e-latex--caption/label-string'."
811 (let ((label (org-element-property :name element)))
812 (if (or (not output) (not label) (string= output "") (string= label ""))
813 output
814 (concat (format "\\label{%s}\n" label) output))))
816 (defun org-e-latex--text-markup (text markup)
817 "Format text depending on MARKUP text markup.
818 See `org-e-latex-text-markup-alist' for details."
819 (let ((fmt (cdr (assq markup org-e-latex-text-markup-alist))))
820 (cond
821 ;; No format string: Return raw text.
822 ((not fmt) text)
823 ;; Handle the `verb' special case: Find and appropriate separator
824 ;; and use "\\verb" command.
825 ((eq 'verb fmt)
826 (let ((separator (org-e-latex--find-verb-separator text)))
827 (concat "\\verb" separator text separator)))
828 ;; Handle the `protectedtexttt' special case: Protect some
829 ;; special chars and use "\texttt{%s}" format string.
830 ((eq 'protectedtexttt fmt)
831 (let ((start 0)
832 (trans '(("\\" . "\\textbackslash{}")
833 ("~" . "\\textasciitilde{}")
834 ("^" . "\\textasciicircum{}")))
835 (rtn "")
836 char)
837 (while (string-match "[\\{}$%&_#~^]" text)
838 (setq char (match-string 0 text))
839 (if (> (match-beginning 0) 0)
840 (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
841 (setq text (substring text (1+ (match-beginning 0))))
842 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
843 rtn (concat rtn char)))
844 (setq text (concat rtn text)
845 fmt "\\texttt{%s}")
846 (while (string-match "--" text)
847 (setq text (replace-match "-{}-" t t text)))
848 (format fmt text)))
849 ;; Else use format string.
850 (t (format fmt text)))))
854 ;;; Template
856 (defun org-e-latex-template (contents info)
857 "Return complete document string after LaTeX conversion.
858 CONTENTS is the transcoded contents string. INFO is a plist
859 holding export options."
860 (let ((title (org-export-data (plist-get info :title) info)))
861 (concat
862 ;; 1. Time-stamp.
863 (and (plist-get info :time-stamp-file)
864 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
865 ;; 2. Document class and packages.
866 (let ((class (plist-get info :latex-class))
867 (class-options (plist-get info :latex-class-options)))
868 (org-element-normalize-string
869 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
870 (document-class-string
871 (and (stringp header)
872 (if class-options
873 (replace-regexp-in-string
874 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
875 class-options header t nil 1)
876 header))))
877 (org-e-latex--guess-inputenc
878 (org-splice-latex-header
879 document-class-string
880 org-export-latex-default-packages-alist ; defined in org.el
881 org-export-latex-packages-alist nil ; defined in org.el
882 (plist-get info :latex-header-extra))))))
883 ;; 3. Define alert if not yet defined.
884 "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
885 ;; 4. Possibly limit depth for headline numbering.
886 (let ((sec-num (plist-get info :section-numbers)))
887 (when (integerp sec-num)
888 (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
889 ;; 5. Author.
890 (let ((author (and (plist-get info :with-author)
891 (let ((auth (plist-get info :author)))
892 (and auth (org-export-data auth info)))))
893 (email (and (plist-get info :with-email)
894 (org-export-data (plist-get info :email) info))))
895 (cond ((and author email (not (string= "" email)))
896 (format "\\author{%s\\thanks{%s}}\n" author email))
897 (author (format "\\author{%s}\n" author))
898 (t "\\author{}\n")))
899 ;; 6. Date.
900 (let ((date (plist-get info :date)))
901 (and date (format "\\date{%s}\n" date)))
902 ;; 7. Title
903 (format "\\title{%s}\n" title)
904 ;; 8. Hyperref options.
905 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
906 (or (plist-get info :keywords) "")
907 (or (plist-get info :description) "")
908 (if (not (plist-get info :with-creator)) ""
909 (plist-get info :creator)))
910 ;; 9. Document start.
911 "\\begin{document}\n\n"
912 ;; 10. Title command.
913 (org-element-normalize-string
914 (cond ((string= "" title) nil)
915 ((not (stringp org-e-latex-title-command)) nil)
916 ((string-match "\\(?:[^%]\\|^\\)%s"
917 org-e-latex-title-command)
918 (format org-e-latex-title-command title))
919 (t org-e-latex-title-command)))
920 ;; 11. Table of contents.
921 (let ((depth (plist-get info :with-toc)))
922 (when depth
923 (concat (when (wholenump depth)
924 (format "\\setcounter{tocdepth}{%d}\n" depth))
925 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
926 ;; 12. Document's body.
927 contents
928 ;; 13. Creator.
929 (let ((creator-info (plist-get info :with-creator)))
930 (cond
931 ((not creator-info) "")
932 ((eq creator-info 'comment)
933 (format "%% %s\n" (plist-get info :creator)))
934 (t (concat (plist-get info :creator) "\n"))))
935 ;; 14. Document end.
936 "\\end{document}")))
940 ;;; Transcode Functions
942 ;;;; Babel Call
944 ;; Babel Calls are ignored.
947 ;;;; Bold
949 (defun org-e-latex-bold (bold contents info)
950 "Transcode BOLD from Org to LaTeX.
951 CONTENTS is the text with bold markup. INFO is a plist holding
952 contextual information."
953 (org-e-latex--text-markup contents 'bold))
956 ;;;; Center Block
958 (defun org-e-latex-center-block (center-block contents info)
959 "Transcode a CENTER-BLOCK element from Org to LaTeX.
960 CONTENTS holds the contents of the center block. INFO is a plist
961 holding contextual information."
962 (org-e-latex--wrap-label
963 center-block
964 (format "\\begin{center}\n%s\\end{center}" contents)))
967 ;;;; Clock
969 (defun org-e-latex-clock (clock contents info)
970 "Transcode a CLOCK element from Org to LaTeX.
971 CONTENTS is nil. INFO is a plist holding contextual
972 information."
973 (concat
974 "\\noindent"
975 (format "\\textbf{%s} " org-clock-string)
976 (format org-e-latex-inactive-timestamp-format
977 (concat (org-translate-time (org-element-property :value clock))
978 (let ((time (org-element-property :time clock)))
979 (and time (format " (%s)" time)))))
980 "\\\\"))
983 ;;;; Code
985 (defun org-e-latex-code (code contents info)
986 "Transcode a CODE object from Org to LaTeX.
987 CONTENTS is nil. INFO is a plist used as a communication
988 channel."
989 (org-e-latex--text-markup (org-element-property :value code) 'code))
992 ;;;; Comment
994 ;; Comments are ignored.
997 ;;;; Comment Block
999 ;; Comment Blocks are ignored.
1002 ;;;; Drawer
1004 (defun org-e-latex-drawer (drawer contents info)
1005 "Transcode a DRAWER element from Org to LaTeX.
1006 CONTENTS holds the contents of the block. INFO is a plist
1007 holding contextual information."
1008 (let* ((name (org-element-property :drawer-name drawer))
1009 (output (if (functionp org-e-latex-format-drawer-function)
1010 (funcall org-e-latex-format-drawer-function
1011 name contents)
1012 ;; If there's no user defined function: simply
1013 ;; display contents of the drawer.
1014 contents)))
1015 (org-e-latex--wrap-label drawer output)))
1018 ;;;; Dynamic Block
1020 (defun org-e-latex-dynamic-block (dynamic-block contents info)
1021 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1022 CONTENTS holds the contents of the block. INFO is a plist
1023 holding contextual information. See `org-export-data'."
1024 (org-e-latex--wrap-label dynamic-block contents))
1027 ;;;; Entity
1029 (defun org-e-latex-entity (entity contents info)
1030 "Transcode an ENTITY object from Org to LaTeX.
1031 CONTENTS are the definition itself. INFO is a plist holding
1032 contextual information."
1033 (let ((ent (org-element-property :latex entity)))
1034 (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
1037 ;;;; Example Block
1039 (defun org-e-latex-example-block (example-block contents info)
1040 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1041 CONTENTS is nil. INFO is a plist holding contextual
1042 information."
1043 (org-e-latex--wrap-label
1044 example-block
1045 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1046 (org-export-format-code-default example-block info))))
1049 ;;;; Export Snippet
1051 (defun org-e-latex-export-snippet (export-snippet contents info)
1052 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1053 CONTENTS is nil. INFO is a plist holding contextual information."
1054 (when (eq (org-export-snippet-backend export-snippet) 'e-latex)
1055 (org-element-property :value export-snippet)))
1058 ;;;; Export Block
1060 (defun org-e-latex-export-block (export-block contents info)
1061 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1062 CONTENTS is nil. INFO is a plist holding contextual information."
1063 (when (string= (org-element-property :type export-block) "latex")
1064 (org-remove-indentation (org-element-property :value export-block))))
1067 ;;;; Fixed Width
1069 (defun org-e-latex-fixed-width (fixed-width contents info)
1070 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1071 CONTENTS is nil. INFO is a plist holding contextual information."
1072 (let* ((value (org-element-normalize-string
1073 (replace-regexp-in-string
1074 "^[ \t]*: ?" ""
1075 (org-element-property :value fixed-width)))))
1076 (org-e-latex--wrap-label
1077 fixed-width (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1080 ;;;; Footnote Definition
1082 ;; Footnote Definitions are ignored.
1085 ;;;; Footnote Reference
1087 (defun org-e-latex-footnote-reference (footnote-reference contents info)
1088 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1089 CONTENTS is nil. INFO is a plist holding contextual information."
1090 (concat
1091 ;; Insert separator between two footnotes in a row.
1092 (let ((prev (org-export-get-previous-element footnote-reference info)))
1093 (when (eq (org-element-type prev) 'footnote-reference)
1094 org-e-latex-footnote-separator))
1095 (cond
1096 ;; Use \footnotemark if the footnote has already been defined.
1097 ((not (org-export-footnote-first-reference-p footnote-reference info))
1098 (format "\\footnotemark[%s]{}"
1099 (org-export-get-footnote-number footnote-reference info)))
1100 ;; Use also \footnotemark if reference is within another footnote
1101 ;; reference or footnote definition.
1102 ((loop for parent in (org-export-get-genealogy footnote-reference info)
1103 thereis (memq (org-element-type parent)
1104 '(footnote-reference footnote-definition)))
1105 (let ((num (org-export-get-footnote-number footnote-reference info)))
1106 (format "\\footnotemark[%s]{}\\setcounter{footnote}{%s}" num num)))
1107 ;; Otherwise, define it with \footnote command.
1109 (let ((def (org-export-get-footnote-definition footnote-reference info)))
1110 (unless (eq (org-element-type def) 'org-data)
1111 (setq def (cons 'org-data (cons nil def))))
1112 (concat
1113 (format "\\footnote{%s}" (org-trim (org-export-data def info)))
1114 ;; Retrieve all footnote references within the footnote and
1115 ;; add their definition after it, since LaTeX doesn't support
1116 ;; them inside.
1117 (let* (all-refs
1118 search-refs ; for byte-compiler
1119 (search-refs
1120 (function
1121 (lambda (data)
1122 ;; Return a list of all footnote references in DATA.
1123 (org-element-map
1124 data 'footnote-reference
1125 (lambda (ref)
1126 (when (org-export-footnote-first-reference-p ref info)
1127 (push ref all-refs)
1128 (when (eq (org-element-property :type ref) 'standard)
1129 (funcall
1130 search-refs
1131 (org-export-get-footnote-definition ref info)))))
1132 info) (reverse all-refs)))))
1133 (mapconcat
1134 (lambda (ref)
1135 (format
1136 "\\footnotetext[%s]{%s}"
1137 (org-export-get-footnote-number ref info)
1138 (org-trim
1139 (org-export-data
1140 (org-export-get-footnote-definition ref info) info))))
1141 (funcall search-refs def) ""))))))))
1144 ;;;; Headline
1146 (defun org-e-latex-headline (headline contents info)
1147 "Transcode an HEADLINE element from Org to LaTeX.
1148 CONTENTS holds the contents of the headline. INFO is a plist
1149 holding contextual information."
1150 (let* ((class (plist-get info :latex-class))
1151 (level (org-export-get-relative-level headline info))
1152 (numberedp (org-export-numbered-headline-p headline info))
1153 (class-sectionning (assoc class org-e-latex-classes))
1154 ;; Section formatting will set two placeholders: one for the
1155 ;; title and the other for the contents.
1156 (section-fmt
1157 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
1158 (fboundp (nth 2 class-sectionning)))
1159 (funcall (nth 2 class-sectionning) level numberedp)
1160 (nth (1+ level) class-sectionning))))
1161 (cond
1162 ;; No section available for that LEVEL.
1163 ((not sec) nil)
1164 ;; Section format directly returned by a function.
1165 ((stringp sec) sec)
1166 ;; (numbered-section . unnumbered-section)
1167 ((not (consp (cdr sec)))
1168 (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
1169 ;; (numbered-open numbered-close)
1170 ((= (length sec) 2)
1171 (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
1172 ;; (num-in num-out no-num-in no-num-out)
1173 ((= (length sec) 4)
1174 (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
1175 (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
1176 (text (org-export-data (org-element-property :title headline) info))
1177 (todo
1178 (and (plist-get info :with-todo-keywords)
1179 (let ((todo (org-element-property :todo-keyword headline)))
1180 (and todo (org-export-data todo info)))))
1181 (todo-type (and todo (org-element-property :todo-type headline)))
1182 (tags (and (plist-get info :with-tags)
1183 (org-element-property :tags headline)))
1184 (priority (and (plist-get info :with-priority)
1185 (org-element-property :priority headline)))
1186 ;; Create the headline text.
1187 (full-text (if (functionp org-e-latex-format-headline-function)
1188 ;; User-defined formatting function.
1189 (funcall org-e-latex-format-headline-function
1190 todo todo-type priority text tags)
1191 ;; Default formatting.
1192 (concat
1193 (when todo
1194 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1195 (when priority (format "\\framebox{\\#%c} " priority))
1196 text
1197 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1198 ;; Associate some \label to the headline for internal links.
1199 (headline-label
1200 (format "\\label{sec-%s}\n"
1201 (mapconcat 'number-to-string
1202 (org-export-get-headline-number headline info)
1203 "-")))
1204 (pre-blanks
1205 (make-string (org-element-property :pre-blank headline) 10)))
1206 (cond
1207 ;; Case 1: This is a footnote section: ignore it.
1208 ((org-element-property :footnote-section-p headline) nil)
1209 ;; Case 2. This is a deep sub-tree: export it as a list item.
1210 ;; Also export as items headlines for which no section
1211 ;; format has been found.
1212 ((or (not section-fmt) (org-export-low-level-p headline info))
1213 ;; Build the real contents of the sub-tree.
1214 (let ((low-level-body
1215 (concat
1216 ;; If the headline is the first sibling, start a list.
1217 (when (org-export-first-sibling-p headline info)
1218 (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
1219 ;; Itemize headline
1220 "\\item " full-text "\n" headline-label pre-blanks contents)))
1221 ;; If headline is not the last sibling simply return
1222 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1223 ;; blank line.
1224 (if (not (org-export-last-sibling-p headline info)) low-level-body
1225 (replace-regexp-in-string
1226 "[ \t\n]*\\'"
1227 (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
1228 low-level-body))))
1229 ;; Case 3. Standard headline. Export it as a section.
1230 (t (format section-fmt full-text
1231 (concat headline-label pre-blanks contents))))))
1234 ;;;; Horizontal Rule
1236 (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
1237 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1238 CONTENTS is nil. INFO is a plist holding contextual information."
1239 (let ((attr (mapconcat #'identity
1240 (org-element-property :attr_latex horizontal-rule)
1241 " ")))
1242 (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
1245 ;;;; Inline Babel Call
1247 ;; Inline Babel Calls are ignored.
1250 ;;;; Inline Src Block
1252 (defun org-e-latex-inline-src-block (inline-src-block contents info)
1253 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1254 CONTENTS holds the contents of the item. INFO is a plist holding
1255 contextual information."
1256 (let* ((code (org-element-property :value inline-src-block))
1257 (separator (org-e-latex--find-verb-separator code)))
1258 (cond
1259 ;; Do not use a special package: transcode it verbatim.
1260 ((not org-e-latex-listings)
1261 (concat "\\verb" separator code separator))
1262 ;; Use minted package.
1263 ((eq org-e-latex-listings 'minted)
1264 (let* ((org-lang (org-element-property :language inline-src-block))
1265 (mint-lang (or (cadr (assq (intern org-lang)
1266 org-e-latex-minted-langs))
1267 org-lang))
1268 (options (org-e-latex--make-option-string
1269 org-e-latex-minted-options)))
1270 (concat (format "\\mint%s{%s}"
1271 (if (string= options "") "" (format "[%s]" options))
1272 mint-lang)
1273 separator code separator)))
1274 ;; Use listings package.
1276 ;; Maybe translate language's name.
1277 (let* ((org-lang (org-element-property :language inline-src-block))
1278 (lst-lang (or (cadr (assq (intern org-lang)
1279 org-e-latex-listings-langs))
1280 org-lang))
1281 (options (org-e-latex--make-option-string
1282 (append org-e-latex-listings-options
1283 `(("language" ,lst-lang))))))
1284 (concat (format "\\lstinline[%s]" options)
1285 separator code separator))))))
1288 ;;;; Inlinetask
1290 (defun org-e-latex-inlinetask (inlinetask contents info)
1291 "Transcode an INLINETASK element from Org to LaTeX.
1292 CONTENTS holds the contents of the block. INFO is a plist
1293 holding contextual information."
1294 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1295 (todo (and (plist-get info :with-todo-keywords)
1296 (let ((todo (org-element-property :todo-keyword inlinetask)))
1297 (and todo (org-export-data todo info)))))
1298 (todo-type (org-element-property :todo-type inlinetask))
1299 (tags (and (plist-get info :with-tags)
1300 (org-element-property :tags inlinetask)))
1301 (priority (and (plist-get info :with-priority)
1302 (org-element-property :priority inlinetask))))
1303 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1304 ;; with appropriate arguments.
1305 (if (functionp org-e-latex-format-inlinetask-function)
1306 (funcall org-e-latex-format-inlinetask-function
1307 todo todo-type priority title tags contents)
1308 ;; Otherwise, use a default template.
1309 (org-e-latex--wrap-label
1310 inlinetask
1311 (let ((full-title
1312 (concat
1313 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
1314 (when priority (format "\\framebox{\\#%c} " priority))
1315 title
1316 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
1317 (format (concat "\\begin{center}\n"
1318 "\\fbox{\n"
1319 "\\begin{minipage}[c]{.6\\textwidth}\n"
1320 "%s\n\n"
1321 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1322 "%s"
1323 "\\end{minipage}\n"
1324 "}\n"
1325 "\\end{center}")
1326 full-title contents))))))
1329 ;;;; Italic
1331 (defun org-e-latex-italic (italic contents info)
1332 "Transcode ITALIC from Org to LaTeX.
1333 CONTENTS is the text with italic markup. INFO is a plist holding
1334 contextual information."
1335 (org-e-latex--text-markup contents 'italic))
1338 ;;;; Item
1340 (defun org-e-latex-item (item contents info)
1341 "Transcode an ITEM element from Org to LaTeX.
1342 CONTENTS holds the contents of the item. INFO is a plist holding
1343 contextual information."
1344 ;; Grab `:level' from plain-list properties, which is always the
1345 ;; first element above current item.
1346 (let* ((level (org-element-property :level (org-export-get-parent item info)))
1347 (counter (let ((count (org-element-property :counter item)))
1348 (and count
1349 (< level 4)
1350 (format "\\setcounter{enum%s}{%s}\n"
1351 (nth level '("i" "ii" "iii" "iv"))
1352 (1- count)))))
1353 (checkbox (let ((checkbox (org-element-property :checkbox item)))
1354 (cond ((eq checkbox 'on) "$\\boxtimes$ ")
1355 ((eq checkbox 'off) "$\\Box$ ")
1356 ((eq checkbox 'trans) "$\\boxminus$ "))))
1357 (tag (let ((tag (org-element-property :tag item)))
1358 (and tag (format "[%s]" (org-export-data tag info))))))
1359 (concat counter "\\item" tag " " checkbox contents)))
1362 ;;;; Keyword
1364 (defun org-e-latex-keyword (keyword contents info)
1365 "Transcode a KEYWORD element from Org to LaTeX.
1366 CONTENTS is nil. INFO is a plist holding contextual information."
1367 (let ((key (org-element-property :key keyword))
1368 (value (org-element-property :value keyword)))
1369 (cond
1370 ((string= key "LATEX") value)
1371 ((string= key "INDEX") (format "\\index{%s}" value))
1372 ;; Invisible targets.
1373 ((string= key "TARGET") nil)
1374 ((string= key "TOC")
1375 (let ((value (downcase value)))
1376 (cond
1377 ((string-match "\\<headlines\\>" value)
1378 (let ((depth (or (and (string-match "[0-9]+" value)
1379 (string-to-number (match-string 0 value)))
1380 (plist-get info :with-toc))))
1381 (concat
1382 (when (wholenump depth)
1383 (format "\\setcounter{tocdepth}{%s}\n" depth))
1384 "\\tableofcontents")))
1385 ((string= "tables" value) "\\listoftables")
1386 ((string= "figures" value) "\\listoffigures")
1387 ((string= "listings" value)
1388 (cond
1389 ((eq org-e-latex-listings 'minted) "\\listoflistings")
1390 (org-e-latex-listings "\\lstlistoflistings")
1391 ;; At the moment, src blocks with a caption are wrapped
1392 ;; into a figure environment.
1393 (t "\\listoffigures")))))))))
1396 ;;;; Latex Environment
1398 (defun org-e-latex-latex-environment (latex-environment contents info)
1399 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1400 CONTENTS is nil. INFO is a plist holding contextual information."
1401 (let ((label (org-element-property :name latex-environment))
1402 (value (org-remove-indentation
1403 (org-element-property :value latex-environment))))
1404 (if (not (org-string-nw-p label)) value
1405 ;; Environment is labelled: label must be within the environment
1406 ;; (otherwise, a reference pointing to that element will count
1407 ;; the section instead).
1408 (with-temp-buffer
1409 (insert value)
1410 (goto-char (point-min))
1411 (forward-line)
1412 (insert (format "\\label{%s}\n" label))
1413 (buffer-string)))))
1416 ;;;; Latex Fragment
1418 (defun org-e-latex-latex-fragment (latex-fragment contents info)
1419 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1420 CONTENTS is nil. INFO is a plist holding contextual information."
1421 (org-element-property :value latex-fragment))
1424 ;;;; Line Break
1426 (defun org-e-latex-line-break (line-break contents info)
1427 "Transcode a LINE-BREAK object from Org to LaTeX.
1428 CONTENTS is nil. INFO is a plist holding contextual information."
1429 "\\\\")
1432 ;;;; Link
1434 (defun org-e-latex-link--inline-image (link info)
1435 "Return LaTeX code for an inline image.
1436 LINK is the link pointing to the inline image. INFO is a plist
1437 used as a communication channel."
1438 (let* ((parent (org-export-get-parent-paragraph link info))
1439 (path (let ((raw-path (org-element-property :path link)))
1440 (if (not (file-name-absolute-p raw-path)) raw-path
1441 (expand-file-name raw-path))))
1442 (caption (org-e-latex--caption/label-string
1443 (org-element-property :caption parent)
1444 (org-element-property :name parent)
1445 info))
1446 ;; Retrieve latex attributes from the element around.
1447 (attr (let ((raw-attr
1448 (mapconcat #'identity
1449 (org-element-property :attr_latex parent)
1450 " ")))
1451 (unless (string= raw-attr "") raw-attr)))
1452 (disposition
1453 (cond
1454 ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
1455 ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
1456 ((or (and attr (string-match "\\<float\\>" attr))
1457 (not (string= caption "")))
1458 'float)))
1459 (placement
1460 (cond
1461 ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1462 (org-match-string-no-properties 1 attr))
1463 ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
1464 ((eq disposition 'float)
1465 (concat "[" org-e-latex-default-figure-position "]"))
1466 (t ""))))
1467 ;; Now clear ATTR from any special keyword and set a default
1468 ;; value if nothing is left.
1469 (setq attr
1470 (if (not attr) ""
1471 (org-trim
1472 (replace-regexp-in-string
1473 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
1474 (setq attr (cond ((not (string= attr "")) attr)
1475 ((eq disposition 'float) "width=0.7\\textwidth")
1476 ((eq disposition 'wrap) "width=0.48\\textwidth")
1477 (t (or org-e-latex-image-default-option ""))))
1478 ;; Return proper string, depending on DISPOSITION.
1479 (case disposition
1480 (wrap (format "\\begin{wrapfigure}%s
1481 \\centering
1482 \\includegraphics[%s]{%s}
1483 %s\\end{wrapfigure}" placement attr path caption))
1484 (mulicolumn (format "\\begin{figure*}%s
1485 \\centering
1486 \\includegraphics[%s]{%s}
1487 %s\\end{figure*}" placement attr path caption))
1488 (float (format "\\begin{figure}%s
1489 \\centering
1490 \\includegraphics[%s]{%s}
1491 %s\\end{figure}" placement attr path caption))
1492 (t (format "\\includegraphics[%s]{%s}" attr path)))))
1494 (defun org-e-latex-link (link desc info)
1495 "Transcode a LINK object from Org to LaTeX.
1497 DESC is the description part of the link, or the empty string.
1498 INFO is a plist holding contextual information. See
1499 `org-export-data'."
1500 (let* ((type (org-element-property :type link))
1501 (raw-path (org-element-property :path link))
1502 ;; Ensure DESC really exists, or set it to nil.
1503 (desc (and (not (string= desc "")) desc))
1504 (imagep (org-export-inline-image-p
1505 link org-e-latex-inline-image-rules))
1506 (path (cond
1507 ((member type '("http" "https" "ftp" "mailto"))
1508 (concat type ":" raw-path))
1509 ((string= type "file")
1510 (when (string-match "\\(.+\\)::.+" raw-path)
1511 (setq raw-path (match-string 1 raw-path)))
1512 (if (file-name-absolute-p raw-path)
1513 (concat "file://" (expand-file-name raw-path))
1514 ;; TODO: Not implemented yet. Concat also:
1515 ;; (org-export-directory :LaTeX info)
1516 (concat "file://" raw-path)))
1517 (t raw-path)))
1518 protocol)
1519 (cond
1520 ;; Image file.
1521 (imagep (org-e-latex-link--inline-image link info))
1522 ;; Radioed target: Target's name is obtained from original raw
1523 ;; link. Path is parsed and transcoded in order to have a proper
1524 ;; display of the contents.
1525 ((string= type "radio")
1526 (format "\\hyperref[%s]{%s}"
1527 (org-export-solidify-link-text path)
1528 (org-export-data
1529 (org-element-parse-secondary-string
1530 path (cdr (assq 'radio-target org-element-object-restrictions)))
1531 info)))
1532 ;; Links pointing to an headline: Find destination and build
1533 ;; appropriate referencing command.
1534 ((member type '("custom-id" "fuzzy" "id"))
1535 (let ((destination (if (string= type "fuzzy")
1536 (org-export-resolve-fuzzy-link link info)
1537 (org-export-resolve-id-link link info))))
1538 (case (org-element-type destination)
1539 ;; Fuzzy link points nowhere.
1540 ('nil
1541 (format "\\texttt{%s}"
1542 (or desc
1543 (org-export-data
1544 (org-element-property :raw-link link) info))))
1545 ;; Fuzzy link points to an invisible target.
1546 (keyword nil)
1547 ;; LINK points to an headline. If headlines are numbered
1548 ;; and the link has no description, display headline's
1549 ;; number. Otherwise, display description or headline's
1550 ;; title.
1551 (headline
1552 (let ((label
1553 (format "sec-%s"
1554 (mapconcat
1555 'number-to-string
1556 (org-export-get-headline-number destination info)
1557 "-"))))
1558 (if (and (plist-get info :section-numbers) (not desc))
1559 (format "\\ref{%s}" label)
1560 (format "\\hyperref[%s]{%s}" label
1561 (or desc
1562 (org-export-data
1563 (org-element-property :title destination) info))))))
1564 ;; Fuzzy link points to a target. Do as above.
1565 (otherwise
1566 (let ((path (org-export-solidify-link-text path)))
1567 (if (not desc) (format "\\ref{%s}" path)
1568 (format "\\hyperref[%s]{%s}" path desc)))))))
1569 ;; Coderef: replace link with the reference name or the
1570 ;; equivalent line number.
1571 ((string= type "coderef")
1572 (format (org-export-get-coderef-format path desc)
1573 (org-export-resolve-coderef path info)))
1574 ;; Link type is handled by a special function.
1575 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
1576 (funcall protocol (org-link-unescape path) desc 'latex))
1577 ;; External link with a description part.
1578 ((and path desc) (format "\\href{%s}{%s}" path desc))
1579 ;; External link without a description part.
1580 (path (format "\\url{%s}" path))
1581 ;; No path, only description. Try to do something useful.
1582 (t (format "\\texttt{%s}" desc)))))
1585 ;;;; Macro
1587 (defun org-e-latex-macro (macro contents info)
1588 "Transcode a MACRO element from Org to LaTeX.
1589 CONTENTS is nil. INFO is a plist holding contextual information."
1590 ;; Use available tools.
1591 (org-export-expand-macro macro info))
1594 ;;;; Paragraph
1596 (defun org-e-latex-paragraph (paragraph contents info)
1597 "Transcode a PARAGRAPH element from Org to LaTeX.
1598 CONTENTS is the contents of the paragraph, as a string. INFO is
1599 the plist used as a communication channel."
1600 contents)
1603 ;;;; Plain List
1605 (defun org-e-latex-plain-list (plain-list contents info)
1606 "Transcode a PLAIN-LIST element from Org to LaTeX.
1607 CONTENTS is the contents of the list. INFO is a plist holding
1608 contextual information."
1609 (let* ((type (org-element-property :type plain-list))
1610 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1611 "inparadesc" "asparadesc"))
1612 (paralist-regexp (concat
1613 "\\("
1614 (mapconcat 'identity paralist-types "\\|")
1615 "\\)"))
1616 (attr (mapconcat #'identity
1617 (org-element-property :attr_latex plain-list)
1618 " "))
1619 (latex-type (cond
1620 ((and attr
1621 (string-match
1622 (format "\\<%s\\>" paralist-regexp) attr))
1623 (match-string 1 attr))
1624 ((eq type 'ordered) "enumerate")
1625 ((eq type 'unordered) "itemize")
1626 ((eq type 'descriptive) "description"))))
1627 (org-e-latex--wrap-label
1628 plain-list
1629 (format "\\begin{%s}%s\n%s\\end{%s}"
1630 latex-type
1631 ;; Once special environment, if any, has been removed, the
1632 ;; rest of the attributes will be optional arguments.
1633 ;; They will be put inside square brackets if necessary.
1634 (let ((opt (replace-regexp-in-string
1635 (format " *%s *" paralist-regexp) "" attr)))
1636 (cond ((string= opt "") "")
1637 ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
1638 (t (format "[%s]" opt))))
1639 contents
1640 latex-type))))
1643 ;;;; Plain Text
1645 (defun org-e-latex-plain-text (text info)
1646 "Transcode a TEXT string from Org to LaTeX.
1647 TEXT is the string to transcode. INFO is a plist holding
1648 contextual information."
1649 ;; Protect %, #, &, $, ~, ^, _, { and }.
1650 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
1651 (setq text
1652 (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
1653 ;; Protect \
1654 (setq text (replace-regexp-in-string
1655 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1656 "$\\backslash$" text nil t 1))
1657 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1658 (let ((case-fold-search nil)
1659 (start 0))
1660 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
1661 (setq text (replace-match
1662 (format "\\%s{}" (match-string 1 text)) nil t text)
1663 start (match-end 0))))
1664 ;; Handle quotation marks
1665 (setq text (org-e-latex--quotation-marks text info))
1666 ;; Convert special strings.
1667 (when (plist-get info :with-special-strings)
1668 (while (string-match (regexp-quote "...") text)
1669 (setq text (replace-match "\\ldots{}" nil t text))))
1670 ;; Handle break preservation if required.
1671 (when (plist-get info :preserve-breaks)
1672 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1673 text)))
1674 ;; Return value.
1675 text)
1678 ;;;; Planning
1680 (defun org-e-latex-planning (planning contents info)
1681 "Transcode a PLANNING element from Org to LaTeX.
1682 CONTENTS is nil. INFO is a plist holding contextual
1683 information."
1684 (concat
1685 "\\noindent"
1686 (mapconcat
1687 'identity
1688 (delq nil
1689 (list
1690 (let ((closed (org-element-property :closed planning)))
1691 (when closed
1692 (concat
1693 (format "\\textbf{%s} " org-closed-string)
1694 (format org-e-latex-inactive-timestamp-format
1695 (org-translate-time closed)))))
1696 (let ((deadline (org-element-property :deadline planning)))
1697 (when deadline
1698 (concat
1699 (format "\\textbf{%s} " org-deadline-string)
1700 (format org-e-latex-active-timestamp-format
1701 (org-translate-time deadline)))))
1702 (let ((scheduled (org-element-property :scheduled planning)))
1703 (when scheduled
1704 (concat
1705 (format "\\textbf{%s} " org-scheduled-string)
1706 (format org-e-latex-active-timestamp-format
1707 (org-translate-time scheduled)))))))
1708 " ")
1709 "\\\\"))
1712 ;;;; Property Drawer
1714 (defun org-e-latex-property-drawer (property-drawer contents info)
1715 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1716 CONTENTS is nil. INFO is a plist holding contextual
1717 information."
1718 ;; The property drawer isn't exported but we want separating blank
1719 ;; lines nonetheless.
1723 ;;;; Quote Block
1725 (defun org-e-latex-quote-block (quote-block contents info)
1726 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1727 CONTENTS holds the contents of the block. INFO is a plist
1728 holding contextual information."
1729 (org-e-latex--wrap-label
1730 quote-block
1731 (format "\\begin{quote}\n%s\\end{quote}" contents)))
1734 ;;;; Quote Section
1736 (defun org-e-latex-quote-section (quote-section contents info)
1737 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1738 CONTENTS is nil. INFO is a plist holding contextual information."
1739 (let ((value (org-remove-indentation
1740 (org-element-property :value quote-section))))
1741 (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
1744 ;;;; Section
1746 (defun org-e-latex-section (section contents info)
1747 "Transcode a SECTION element from Org to LaTeX.
1748 CONTENTS holds the contents of the section. INFO is a plist
1749 holding contextual information."
1750 contents)
1753 ;;;; Radio Target
1755 (defun org-e-latex-radio-target (radio-target text info)
1756 "Transcode a RADIO-TARGET object from Org to LaTeX.
1757 TEXT is the text of the target. INFO is a plist holding
1758 contextual information."
1759 (format "\\label{%s}%s"
1760 (org-export-solidify-link-text
1761 (org-element-property :value radio-target))
1762 text))
1765 ;;;; Special Block
1767 (defun org-e-latex-special-block (special-block contents info)
1768 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1769 CONTENTS holds the contents of the block. INFO is a plist
1770 holding contextual information."
1771 (let ((type (downcase (org-element-property :type special-block))))
1772 (org-e-latex--wrap-label
1773 special-block
1774 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
1777 ;;;; Src Block
1779 (defun org-e-latex-src-block (src-block contents info)
1780 "Transcode a SRC-BLOCK element from Org to LaTeX.
1781 CONTENTS holds the contents of the item. INFO is a plist holding
1782 contextual information."
1783 (let* ((lang (org-element-property :language src-block))
1784 (caption (org-element-property :caption src-block))
1785 (label (org-element-property :name src-block))
1786 (custom-env (and lang
1787 (cadr (assq (intern lang)
1788 org-e-latex-custom-lang-environments))))
1789 (num-start (case (org-element-property :number-lines src-block)
1790 (continued (org-export-get-loc src-block info))
1791 (new 0)))
1792 (retain-labels (org-element-property :retain-labels src-block)))
1793 (cond
1794 ;; Case 1. No source fontification.
1795 ((not org-e-latex-listings)
1796 (let ((caption-str (org-e-latex--caption/label-string caption label info))
1797 (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
1798 (format
1799 (or float-env "%s")
1800 (concat caption-str
1801 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1802 (org-export-format-code-default src-block info))))))
1803 ;; Case 2. Custom environment.
1804 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
1805 custom-env
1806 (org-export-format-code-default src-block info)
1807 custom-env))
1808 ;; Case 3. Use minted package.
1809 ((eq org-e-latex-listings 'minted)
1810 (let ((float-env (when (or label caption)
1811 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1812 (org-e-latex--caption/label-string
1813 caption label info))))
1814 (body
1815 (format
1816 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1817 ;; Options.
1818 (org-e-latex--make-option-string
1819 (if (not num-start) org-e-latex-minted-options
1820 (append `(("linenos")
1821 ("firstnumber" ,(number-to-string (1+ num-start))))
1822 org-e-latex-minted-options)))
1823 ;; Language.
1824 (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
1825 ;; Source code.
1826 (let* ((code-info (org-export-unravel-code src-block))
1827 (max-width
1828 (apply 'max
1829 (mapcar 'length
1830 (org-split-string (car code-info) "\n")))))
1831 (org-export-format-code
1832 (car code-info)
1833 (lambda (loc num ref)
1834 (concat
1836 (when ref
1837 ;; Ensure references are flushed to the right,
1838 ;; separated with 6 spaces from the widest line
1839 ;; of code.
1840 (concat (make-string (+ (- max-width (length loc)) 6) ? )
1841 (format "(%s)" ref)))))
1842 nil (and retain-labels (cdr code-info)))))))
1843 ;; Return value.
1844 (if float-env (format float-env body) body)))
1845 ;; Case 4. Use listings package.
1847 (let ((lst-lang
1848 (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
1849 (caption-str
1850 (when caption
1851 (let ((main (org-export-data (car caption) info)))
1852 (if (not (cdr caption)) (format "{%s}" main)
1853 (format "{[%s]%s}"
1854 (org-export-data (cdr caption) info)
1855 main))))))
1856 (concat
1857 ;; Options.
1858 (format "\\lstset{%s}\n"
1859 (org-e-latex--make-option-string
1860 (append org-e-latex-listings-options
1861 `(("language" ,lst-lang))
1862 (when label `(("label" ,label)))
1863 (when caption-str `(("caption" ,caption-str)))
1864 (cond ((not num-start) '(("numbers" "none")))
1865 ((zerop num-start) '(("numbers" "left")))
1866 (t `(("numbers" "left")
1867 ("firstnumber"
1868 ,(number-to-string (1+ num-start)))))))))
1869 ;; Source code.
1870 (format
1871 "\\begin{lstlisting}\n%s\\end{lstlisting}"
1872 (let* ((code-info (org-export-unravel-code src-block))
1873 (max-width
1874 (apply 'max
1875 (mapcar 'length
1876 (org-split-string (car code-info) "\n")))))
1877 (org-export-format-code
1878 (car code-info)
1879 (lambda (loc num ref)
1880 (concat
1882 (when ref
1883 ;; Ensure references are flushed to the right,
1884 ;; separated with 6 spaces from the widest line of
1885 ;; code
1886 (concat (make-string (+ (- max-width (length loc)) 6) ? )
1887 (format "(%s)" ref)))))
1888 nil (and retain-labels (cdr code-info)))))))))))
1891 ;;;; Statistics Cookie
1893 (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
1894 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1895 CONTENTS is nil. INFO is a plist holding contextual information."
1896 (org-element-property :value statistics-cookie))
1899 ;;;; Strike-Through
1901 (defun org-e-latex-strike-through (strike-through contents info)
1902 "Transcode STRIKE-THROUGH from Org to LaTeX.
1903 CONTENTS is the text with strike-through markup. INFO is a plist
1904 holding contextual information."
1905 (org-e-latex--text-markup contents 'strike-through))
1908 ;;;; Subscript
1910 (defun org-e-latex-subscript (subscript contents info)
1911 "Transcode a SUBSCRIPT object from Org to LaTeX.
1912 CONTENTS is the contents of the object. INFO is a plist holding
1913 contextual information."
1914 (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
1917 ;;;; Superscript
1919 (defun org-e-latex-superscript (superscript contents info)
1920 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1921 CONTENTS is the contents of the object. INFO is a plist holding
1922 contextual information."
1923 (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
1926 ;;;; Table
1928 (defun org-e-latex-table--format-string (table info)
1929 "Return an appropriate format string for TABLE.
1931 TABLE-INFO is the plist containing format info about the table,
1932 as returned by `org-export-table-format-info'. INFO is a plist
1933 used as a communication channel.
1935 The format string leaves one placeholder for the body of the
1936 table."
1937 (let* ((label (org-element-property :name table))
1938 (caption (org-e-latex--caption/label-string
1939 (org-element-property :caption table) label info))
1940 (attr (mapconcat 'identity
1941 (org-element-property :attr_latex table)
1942 " "))
1943 ;; Determine alignment string.
1944 (alignment (org-e-latex-table--align-string table info))
1945 ;; Determine environment for the table: longtable, tabular...
1946 (table-env (cond
1947 ((not attr) org-e-latex-default-table-environment)
1948 ((string-match "\\<longtable\\>" attr) "longtable")
1949 ((string-match "\\<tabular.?\\>" attr)
1950 (org-match-string-no-properties 0 attr))
1951 (t org-e-latex-default-table-environment)))
1952 ;; If table is a float, determine environment: table, table*
1953 ;; or sidewaystable.
1954 (float-env (cond
1955 ((string= "longtable" table-env) nil)
1956 ((and attr (string-match "\\<sidewaystable\\>" attr))
1957 "sidewaystables")
1958 ((and attr
1959 (or (string-match (regexp-quote "table*") attr)
1960 (string-match "\\<multicolumn\\>" attr)))
1961 "table*")
1962 ((or (not (string= caption "")) label) "table")))
1963 ;; Extract others display options.
1964 (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
1965 (org-match-string-no-properties 1 attr)))
1966 (placement
1967 (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
1968 (org-match-string-no-properties 1 attr)
1969 (format "[%s]" org-e-latex-default-figure-position))))
1970 ;; Prepare the final format string for the table.
1971 (cond
1972 ;; Longtable.
1973 ((string= "longtable" table-env)
1974 (format
1975 "\\begin{longtable}{%s}\n%s%%s%s\\end{longtable}"
1976 alignment
1977 (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
1978 (concat (org-trim caption) "\\\\\n"))
1979 (if (or org-e-latex-table-caption-above (string= "" caption)) ""
1980 (concat (org-trim caption) "\\\\\n"))))
1981 ;; Others.
1982 (t (concat (when float-env
1983 (concat
1984 (format "\\begin{%s}%s\n" float-env placement)
1985 (if org-e-latex-table-caption-above caption "")))
1986 (when org-e-latex-tables-centered "\\begin{center}\n")
1987 (format "\\begin{%s}%s{%s}\n%%s\\end{%s}"
1988 table-env
1989 (if width (format "{%s}" width) "") alignment table-env)
1990 (when org-e-latex-tables-centered "\n\\end{center}")
1991 (when float-env
1992 (concat (if org-e-latex-table-caption-above "" caption)
1993 (format "\n\\end{%s}" float-env))))))))
1995 (defun org-e-latex-table--align-string (table info)
1996 "Return an appropriate LaTeX alignment string.
1997 TABLE is the considered table. INFO is a plist used as
1998 a communication channel."
1999 (let ((attr (mapconcat 'identity
2000 (org-element-property :attr_latex table)
2001 " ")))
2002 (if (and attr (string-match "\\<align=\\(\\S-+\\)" attr))
2003 (match-string 1 attr)
2004 (let (alignment)
2005 ;; Extract column groups and alignment from first (non-rule)
2006 ;; row.
2007 (org-element-map
2008 (org-element-map table 'table-row 'identity info 'first-match)
2009 'table-cell
2010 (lambda (cell)
2011 (let ((borders (org-export-table-cell-borders cell info)))
2012 ;; Check left border for the first cell only.
2013 (when (and (memq 'left borders) (not alignment))
2014 (push "|" alignment))
2015 (push (case (org-export-table-cell-alignment cell info)
2016 (left "l")
2017 (right "r")
2018 (center "c"))
2019 alignment)
2020 (when (memq 'right borders) (push "|" alignment))))
2021 info)
2022 (apply 'concat (reverse alignment))))))
2024 (defun org-e-latex-table (table contents info)
2025 "Transcode a TABLE element from Org to LaTeX.
2026 CONTENTS is nil. INFO is a plist holding contextual information."
2027 (cond
2028 ;; Case 1: verbatim table.
2029 ((or org-e-latex-tables-verbatim
2030 (let ((attr (mapconcat 'identity
2031 (org-element-property :attr_latex table)
2032 " ")))
2033 (and attr (string-match "\\<verbatim\\>" attr))))
2034 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2035 ;; Re-create table, without affiliated keywords.
2036 (org-trim
2037 (org-element-interpret-data
2038 `(org-data nil (table nil ,@(org-element-contents table)))))))
2039 ;; Case 2: table.el table. Convert it using appropriate tools.
2040 ((eq (org-element-property :type table) 'table.el)
2041 (require 'table)
2042 ;; Ensure "*org-export-table*" buffer is empty.
2043 (with-current-buffer (get-buffer-create "*org-export-table*")
2044 (erase-buffer))
2045 (let ((output (with-temp-buffer
2046 (insert (org-element-property :value table))
2047 (goto-char 1)
2048 (re-search-forward "^[ \t]*|[^|]" nil t)
2049 (table-generate-source 'latex "*org-export-table*")
2050 (with-current-buffer "*org-export-table*"
2051 (org-trim (buffer-string))))))
2052 (kill-buffer (get-buffer "*org-export-table*"))
2053 ;; Remove left out comments.
2054 (while (string-match "^%.*\n" output)
2055 (setq output (replace-match "" t t output)))
2056 ;; When the "rmlines" attribute is provided, remove all hlines
2057 ;; but the the one separating heading from the table body.
2058 (let ((attr (mapconcat 'identity
2059 (org-element-property :attr_latex table)
2060 " ")))
2061 (when (and attr (string-match "\\<rmlines\\>" attr))
2062 (let ((n 0) (pos 0))
2063 (while (and (< (length output) pos)
2064 (setq pos (string-match "^\\\\hline\n?" output pos)))
2065 (incf n)
2066 (unless (= n 2)
2067 (setq output (replace-match "" nil nil output)))))))
2068 (if (not org-e-latex-tables-centered) output
2069 (format "\\begin{center}\n%s\n\\end{center}" output))))
2070 ;; Case 3: Standard table.
2071 (t (format (org-e-latex-table--format-string table info) contents))))
2074 ;;;; Table Cell
2076 (defun org-e-latex-table-cell (table-cell contents info)
2077 "Transcode a TABLE-CELL element from Org to LaTeX.
2078 CONTENTS is the cell contents. INFO is a plist used as
2079 a communication channel."
2080 (concat (if (and contents
2081 org-e-latex-table-scientific-notation
2082 (string-match orgtbl-exp-regexp contents))
2083 ;; Use appropriate format string for scientific
2084 ;; notation.
2085 (format org-e-latex-table-scientific-notation
2086 (match-string 1 contents)
2087 (match-string 2 contents))
2088 contents)
2089 (when (org-export-get-next-element table-cell info) " & ")))
2092 ;;;; Table Row
2094 (defun org-e-latex-table-row (table-row contents info)
2095 "Transcode a TABLE-ROW element from Org to LaTeX.
2096 CONTENTS is the contents of the row. INFO is a plist used as
2097 a communication channel."
2098 ;; Rules are ignored since table separators are deduced from
2099 ;; borders of the current row.
2100 (when (eq (org-element-property :type table-row) 'standard)
2101 (let* ((attr (mapconcat 'identity
2102 (org-element-property
2103 :attr_latex (org-export-get-parent table-row info))
2104 " "))
2105 (longtablep (and attr (string-match "\\<longtable\\>" attr)))
2106 (booktabsp
2107 (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
2108 org-e-latex-tables-booktabs))
2109 ;; TABLE-ROW's borders are extracted from its first cell.
2110 (borders
2111 (org-export-table-cell-borders
2112 (car (org-element-contents table-row)) info)))
2113 (concat
2114 ;; When BOOKTABS are activated enforce top-rule even when no
2115 ;; hline was specifically marked.
2116 (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
2117 ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
2118 contents "\\\\\n"
2119 (cond
2120 ;; Special case for long tables. Define header and footers.
2121 ((and longtablep (org-export-table-row-ends-header-p table-row info))
2122 (format "%s
2123 \\endhead
2124 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2125 \\endfoot
2126 \\endlastfoot"
2127 (if booktabsp "\\midrule" "\\hline")
2128 (if booktabsp "\\midrule" "\\hline")
2129 ;; Number of columns.
2130 (cdr (org-export-table-dimensions
2131 (org-export-get-parent-table table-row info) info))))
2132 ;; When BOOKTABS are activated enforce bottom rule even when
2133 ;; no hline was specifically marked.
2134 ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
2135 ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
2136 ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
2139 ;;;; Target
2141 (defun org-e-latex-target (target contents info)
2142 "Transcode a TARGET object from Org to LaTeX.
2143 CONTENTS is nil. INFO is a plist holding contextual
2144 information."
2145 (format "\\label{%s}"
2146 (org-export-solidify-link-text (org-element-property :value target))))
2149 ;;;; Timestamp
2151 (defun org-e-latex-timestamp (timestamp contents info)
2152 "Transcode a TIMESTAMP object from Org to LaTeX.
2153 CONTENTS is nil. INFO is a plist holding contextual
2154 information."
2155 (let ((value (org-translate-time (org-element-property :value timestamp)))
2156 (type (org-element-property :type timestamp)))
2157 (cond ((memq type '(active active-range))
2158 (format org-e-latex-active-timestamp-format value))
2159 ((memq type '(inactive inactive-range))
2160 (format org-e-latex-inactive-timestamp-format value))
2161 (t (format org-e-latex-diary-timestamp-format value)))))
2164 ;;;; Underline
2166 (defun org-e-latex-underline (underline contents info)
2167 "Transcode UNDERLINE from Org to LaTeX.
2168 CONTENTS is the text with underline markup. INFO is a plist
2169 holding contextual information."
2170 (org-e-latex--text-markup contents 'underline))
2173 ;;;; Verbatim
2175 (defun org-e-latex-verbatim (verbatim contents info)
2176 "Transcode a VERBATIM object from Org to LaTeX.
2177 CONTENTS is nil. INFO is a plist used as a communication
2178 channel."
2179 (org-e-latex--text-markup (org-element-property :value verbatim) 'verbatim))
2182 ;;;; Verse Block
2184 (defun org-e-latex-verse-block (verse-block contents info)
2185 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2186 CONTENTS is verse block contents. INFO is a plist holding
2187 contextual information."
2188 (org-e-latex--wrap-label
2189 verse-block
2190 ;; In a verse environment, add a line break to each newline
2191 ;; character and change each white space at beginning of a line
2192 ;; into a space of 1 em. Also change each blank line with
2193 ;; a vertical space of 1 em.
2194 (progn
2195 (setq contents (replace-regexp-in-string
2196 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2197 (replace-regexp-in-string
2198 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
2199 (while (string-match "^[ \t]+" contents)
2200 (let ((new-str (format "\\hspace*{%dem}"
2201 (length (match-string 0 contents)))))
2202 (setq contents (replace-match new-str nil t contents))))
2203 (format "\\begin{verse}\n%s\\end{verse}" contents))))
2207 ;;; Interactive functions
2209 (defun org-e-latex-export-to-latex
2210 (&optional subtreep visible-only body-only ext-plist pub-dir)
2211 "Export current buffer to a LaTeX file.
2213 If narrowing is active in the current buffer, only export its
2214 narrowed part.
2216 If a region is active, export that region.
2218 When optional argument SUBTREEP is non-nil, export the sub-tree
2219 at point, extracting information from the headline properties
2220 first.
2222 When optional argument VISIBLE-ONLY is non-nil, don't export
2223 contents of hidden elements.
2225 When optional argument BODY-ONLY is non-nil, only write code
2226 between \"\\begin{document}\" and \"\\end{document}\".
2228 EXT-PLIST, when provided, is a property list with external
2229 parameters overriding Org default settings, but still inferior to
2230 file-local settings.
2232 When optional argument PUB-DIR is set, use it as the publishing
2233 directory.
2235 Return output file's name."
2236 (interactive)
2237 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
2238 (org-export-to-file
2239 'e-latex outfile subtreep visible-only body-only ext-plist)))
2241 (defun org-e-latex-export-to-pdf
2242 (&optional subtreep visible-only body-only ext-plist pub-dir)
2243 "Export current buffer to LaTeX then process through to PDF.
2245 If narrowing is active in the current buffer, only export its
2246 narrowed part.
2248 If a region is active, export that region.
2250 When optional argument SUBTREEP is non-nil, export the sub-tree
2251 at point, extracting information from the headline properties
2252 first.
2254 When optional argument VISIBLE-ONLY is non-nil, don't export
2255 contents of hidden elements.
2257 When optional argument BODY-ONLY is non-nil, only write code
2258 between \"\\begin{document}\" and \"\\end{document}\".
2260 EXT-PLIST, when provided, is a property list with external
2261 parameters overriding Org default settings, but still inferior to
2262 file-local settings.
2264 When optional argument PUB-DIR is set, use it as the publishing
2265 directory.
2267 Return PDF file's name."
2268 (interactive)
2269 (org-e-latex-compile
2270 (org-e-latex-export-to-latex
2271 subtreep visible-only body-only ext-plist pub-dir)))
2273 (defun org-e-latex-compile (texfile)
2274 "Compile a TeX file.
2276 TEXFILE is the name of the file being compiled. Processing is
2277 done through the command specified in `org-e-latex-pdf-process'.
2279 Return PDF file name or an error if it couldn't be produced."
2280 (let* ((wconfig (current-window-configuration))
2281 (texfile (file-truename texfile))
2282 (base (file-name-sans-extension texfile))
2283 errors)
2284 (message (format "Processing LaTeX file %s ..." texfile))
2285 (unwind-protect
2286 (progn
2287 (cond
2288 ;; A function is provided: Apply it.
2289 ((functionp org-e-latex-pdf-process)
2290 (funcall org-e-latex-pdf-process (shell-quote-argument texfile)))
2291 ;; A list is provided: Replace %b, %f and %o with appropriate
2292 ;; values in each command before applying it. Output is
2293 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2294 ((consp org-e-latex-pdf-process)
2295 (let* ((out-dir (or (file-name-directory texfile) "./"))
2296 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2297 (mapc
2298 (lambda (command)
2299 (shell-command
2300 (replace-regexp-in-string
2301 "%b" (shell-quote-argument base)
2302 (replace-regexp-in-string
2303 "%f" (shell-quote-argument texfile)
2304 (replace-regexp-in-string
2305 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
2306 outbuf))
2307 org-e-latex-pdf-process)
2308 ;; Collect standard errors from output buffer.
2309 (setq errors (org-e-latex-collect-errors outbuf))))
2310 (t (error "No valid command to process to PDF")))
2311 (let ((pdffile (concat base ".pdf")))
2312 ;; Check for process failure. Provide collected errors if
2313 ;; possible.
2314 (if (not (file-exists-p pdffile))
2315 (error (concat (format "PDF file %s wasn't produced" pdffile)
2316 (when errors (concat ": " errors))))
2317 ;; Else remove log files, when specified, and signal end of
2318 ;; process to user, along with any error encountered.
2319 (when org-e-latex-remove-logfiles
2320 (dolist (ext org-e-latex-logfiles-extensions)
2321 (let ((file (concat base "." ext)))
2322 (when (file-exists-p file) (delete-file file)))))
2323 (message (concat "Process completed"
2324 (if (not errors) "."
2325 (concat " with errors: " errors)))))
2326 ;; Return output file name.
2327 pdffile))
2328 (set-window-configuration wconfig))))
2330 (defun org-e-latex-collect-errors (buffer)
2331 "Collect some kind of errors from \"pdflatex\" command output.
2333 BUFFER is the buffer containing output.
2335 Return collected error types as a string, or nil if there was
2336 none."
2337 (with-current-buffer buffer
2338 (save-excursion
2339 (goto-char (point-max))
2340 ;; Find final "pdflatex" run.
2341 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
2342 (let ((case-fold-search t)
2343 (errors ""))
2344 (when (save-excursion
2345 (re-search-forward "Reference.*?undefined" nil t))
2346 (setq errors (concat errors " [undefined reference]")))
2347 (when (save-excursion
2348 (re-search-forward "Citation.*?undefined" nil t))
2349 (setq errors (concat errors " [undefined citation]")))
2350 (when (save-excursion
2351 (re-search-forward "Undefined control sequence" nil t))
2352 (setq errors (concat errors " [undefined control sequence]")))
2353 (when (save-excursion
2354 (re-search-forward "^! LaTeX.*?Error" nil t))
2355 (setq errors (concat errors " [LaTeX error]")))
2356 (when (save-excursion
2357 (re-search-forward "^! Package.*?Error" nil t))
2358 (setq errors (concat errors " [package error]")))
2359 (and (org-string-nw-p errors) (org-trim errors)))))))
2362 (provide 'org-e-latex)
2363 ;;; org-e-latex.el ends here