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