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